Commit | Line | Data |
---|---|---|
53cdc686 JA |
1 | #include <unistd.h> |
2 | #include <fcntl.h> | |
3 | #include <string.h> | |
4 | #include <assert.h> | |
bbf6b540 | 5 | #include <dirent.h> |
d74ac843 | 6 | #include <libgen.h> |
53cdc686 JA |
7 | #include <sys/stat.h> |
8 | #include <sys/mman.h> | |
bbf6b540 | 9 | #include <sys/types.h> |
53cdc686 JA |
10 | |
11 | #include "fio.h" | |
f17c4392 | 12 | #include "smalloc.h" |
4906e0b5 | 13 | #include "filehash.h" |
bcbfeefa | 14 | #include "options.h" |
ecc314ba | 15 | #include "os/os.h" |
2316296a | 16 | #include "hash.h" |
7ebd796f | 17 | #include "lib/axmap.h" |
8c43ba62 | 18 | #include "lib/memalign.h" |
53cdc686 | 19 | |
97ac992c | 20 | #ifdef CONFIG_LINUX_FALLOCATE |
a596f047 EG |
21 | #include <linux/falloc.h> |
22 | #endif | |
23 | ||
7172cfe8 JA |
24 | static int root_warn; |
25 | ||
bcbfeefa CE |
26 | static FLIST_HEAD(filename_list); |
27 | ||
bf651609 TK |
28 | /* |
29 | * List entry for filename_list | |
30 | */ | |
31 | struct file_name { | |
32 | struct flist_head list; | |
33 | char *filename; | |
34 | }; | |
35 | ||
c592b9fe JA |
36 | static inline void clear_error(struct thread_data *td) |
37 | { | |
38 | td->error = 0; | |
39 | td->verror[0] = '\0'; | |
40 | } | |
41 | ||
2c3e17be SW |
42 | static inline int native_fallocate(struct thread_data *td, struct fio_file *f) |
43 | { | |
44 | bool success; | |
45 | ||
46 | success = fio_fallocate(f, 0, f->real_file_size); | |
47 | dprint(FD_FILE, "native fallocate of file %s size %llu was " | |
48 | "%ssuccessful\n", f->file_name, | |
49 | (unsigned long long) f->real_file_size, | |
50 | !success ? "un": ""); | |
51 | ||
52 | if (success) | |
53 | return 0; | |
54 | ||
55 | if (errno == ENOSYS) | |
56 | dprint(FD_FILE, "native fallocate is not implemented\n"); | |
57 | ||
58 | return -1; | |
59 | } | |
60 | ||
005eb303 JA |
61 | static void fallocate_file(struct thread_data *td, struct fio_file *f) |
62 | { | |
63 | int r; | |
64 | ||
65 | if (td->o.fill_device) | |
66 | return; | |
67 | ||
005eb303 | 68 | switch (td->o.fallocate_mode) { |
2c3e17be SW |
69 | case FIO_FALLOCATE_NATIVE: |
70 | r = native_fallocate(td, f); | |
f271a3f2 | 71 | if (r != 0 && errno != ENOSYS) |
2c3e17be SW |
72 | log_err("fio: native_fallocate call failed: %s\n", |
73 | strerror(errno)); | |
74 | break; | |
005eb303 JA |
75 | case FIO_FALLOCATE_NONE: |
76 | break; | |
2c3e17be | 77 | #ifdef CONFIG_POSIX_FALLOCATE |
005eb303 JA |
78 | case FIO_FALLOCATE_POSIX: |
79 | dprint(FD_FILE, "posix_fallocate file %s size %llu\n", | |
80 | f->file_name, | |
81 | (unsigned long long) f->real_file_size); | |
82 | ||
83 | r = posix_fallocate(f->fd, 0, f->real_file_size); | |
84 | if (r > 0) | |
85 | log_err("fio: posix_fallocate fails: %s\n", strerror(r)); | |
86 | break; | |
2c3e17be | 87 | #endif /* CONFIG_POSIX_FALLOCATE */ |
005eb303 JA |
88 | #ifdef CONFIG_LINUX_FALLOCATE |
89 | case FIO_FALLOCATE_KEEP_SIZE: | |
90 | dprint(FD_FILE, "fallocate(FALLOC_FL_KEEP_SIZE) " | |
91 | "file %s size %llu\n", f->file_name, | |
92 | (unsigned long long) f->real_file_size); | |
93 | ||
94 | r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0, f->real_file_size); | |
95 | if (r != 0) | |
96 | td_verror(td, errno, "fallocate"); | |
97 | ||
98 | break; | |
99 | #endif /* CONFIG_LINUX_FALLOCATE */ | |
100 | default: | |
101 | log_err("fio: unknown fallocate mode: %d\n", td->o.fallocate_mode); | |
102 | assert(0); | |
103 | } | |
005eb303 JA |
104 | } |
105 | ||
3baddf24 JA |
106 | /* |
107 | * Leaves f->fd open on success, caller must close | |
108 | */ | |
7bb48f84 | 109 | static int extend_file(struct thread_data *td, struct fio_file *f) |
25205e97 | 110 | { |
005eb303 | 111 | int new_layout = 0, unlink_file = 0, flags; |
25205e97 JA |
112 | unsigned long long left; |
113 | unsigned int bs; | |
f3e1eb23 | 114 | char *b = NULL; |
b2a15192 | 115 | |
4241ea8f JA |
116 | if (read_only) { |
117 | log_err("fio: refusing extend of file due to read-only\n"); | |
118 | return 0; | |
119 | } | |
120 | ||
507a702f JA |
121 | /* |
122 | * check if we need to lay the file out complete again. fio | |
123 | * does that for operations involving reads, or for writes | |
124 | * where overwrite is set | |
125 | */ | |
1417daeb JA |
126 | if (td_read(td) || |
127 | (td_write(td) && td->o.overwrite && !td->o.file_append) || | |
9b87f09b | 128 | (td_write(td) && td_ioengine_flagged(td, FIO_NOEXTEND))) |
507a702f | 129 | new_layout = 1; |
1417daeb | 130 | if (td_write(td) && !td->o.overwrite && !td->o.file_append) |
ea443657 | 131 | unlink_file = 1; |
507a702f | 132 | |
6ae1f57f | 133 | if (unlink_file || new_layout) { |
2442c935 JA |
134 | int ret; |
135 | ||
bd199f2b | 136 | dprint(FD_FILE, "layout unlink %s\n", f->file_name); |
2442c935 JA |
137 | |
138 | ret = td_io_unlink_file(td, f); | |
139 | if (ret != 0 && ret != ENOENT) { | |
7bb48f84 JA |
140 | td_verror(td, errno, "unlink"); |
141 | return 1; | |
142 | } | |
143 | } | |
144 | ||
2378826d JA |
145 | flags = O_WRONLY; |
146 | if (td->o.allow_create) | |
147 | flags |= O_CREAT; | |
507a702f JA |
148 | if (new_layout) |
149 | flags |= O_TRUNC; | |
6e344dc3 | 150 | if (td->o.odirect) |
151 | flags |= OS_O_DIRECT; | |
507a702f | 152 | |
9c0f3f32 BC |
153 | #ifdef WIN32 |
154 | flags |= _O_BINARY; | |
155 | #endif | |
156 | ||
ee56ad50 | 157 | dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags); |
507a702f | 158 | f->fd = open(f->file_name, flags, 0644); |
53cdc686 | 159 | if (f->fd < 0) { |
2378826d JA |
160 | int err = errno; |
161 | ||
162 | if (err == ENOENT && !td->o.allow_create) | |
163 | log_err("fio: file creation disallowed by " | |
164 | "allow_file_create=0\n"); | |
6c3169f9 SW |
165 | else { |
166 | if (err == EINVAL && (flags & OS_O_DIRECT)) | |
167 | log_err("fio: looks like your filesystem " | |
168 | "does not support " | |
169 | "direct=1/buffered=0\n"); | |
170 | ||
2378826d | 171 | td_verror(td, err, "open"); |
6c3169f9 | 172 | } |
53cdc686 JA |
173 | return 1; |
174 | } | |
175 | ||
005eb303 | 176 | fallocate_file(td, f); |
9b836561 | 177 | |
79591fa9 TK |
178 | /* |
179 | * If our jobs don't require regular files initially, we're done. | |
180 | */ | |
fc74ac1d SL |
181 | if (!new_layout) |
182 | goto done; | |
183 | ||
5e0074c2 JA |
184 | /* |
185 | * The size will be -1ULL when fill_device is used, so don't truncate | |
186 | * or fallocate this file, just write it | |
187 | */ | |
188 | if (!td->o.fill_device) { | |
189 | dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name, | |
4b91ee8f | 190 | (unsigned long long) f->real_file_size); |
5e0074c2 | 191 | if (ftruncate(f->fd, f->real_file_size) == -1) { |
3cd4c66f J |
192 | if (errno != EFBIG) { |
193 | td_verror(td, errno, "ftruncate"); | |
194 | goto err; | |
195 | } | |
5e0074c2 | 196 | } |
5e0074c2 | 197 | } |
40f8298c | 198 | |
47534cda TK |
199 | if (td->o.odirect && !OS_O_DIRECT && fio_set_directio(td, f)) |
200 | goto err; | |
201 | ||
82d8de31 TK |
202 | left = f->real_file_size; |
203 | bs = td->o.max_bs[DDIR_WRITE]; | |
204 | if (bs > left) | |
205 | bs = left; | |
206 | ||
8c43ba62 | 207 | b = fio_memalign(page_size, bs); |
84af30a1 | 208 | if (!b) { |
8c43ba62 | 209 | td_verror(td, errno, "fio_memalign"); |
84af30a1 TK |
210 | goto err; |
211 | } | |
53cdc686 | 212 | |
53cdc686 | 213 | while (left && !td->terminate) { |
005eb303 JA |
214 | ssize_t r; |
215 | ||
53cdc686 JA |
216 | if (bs > left) |
217 | bs = left; | |
218 | ||
cc86c395 JA |
219 | fill_io_buffer(td, b, bs, bs); |
220 | ||
53cdc686 JA |
221 | r = write(f->fd, b, bs); |
222 | ||
5e0074c2 JA |
223 | if (r > 0) { |
224 | left -= r; | |
53cdc686 JA |
225 | continue; |
226 | } else { | |
5e0074c2 JA |
227 | if (r < 0) { |
228 | int __e = errno; | |
229 | ||
230 | if (__e == ENOSPC) { | |
231 | if (td->o.fill_device) | |
232 | break; | |
233 | log_info("fio: ENOSPC on laying out " | |
234 | "file, stopping\n"); | |
235 | break; | |
236 | } | |
e1161c32 | 237 | td_verror(td, errno, "write"); |
5e0074c2 | 238 | } else |
e1161c32 | 239 | td_verror(td, EIO, "write"); |
53cdc686 JA |
240 | |
241 | break; | |
242 | } | |
243 | } | |
244 | ||
ffdfabd4 JA |
245 | if (td->terminate) { |
246 | dprint(FD_FILE, "terminate unlink %s\n", f->file_name); | |
38ef9c90 | 247 | td_io_unlink_file(td, f); |
ffdfabd4 | 248 | } else if (td->o.create_fsync) { |
98e1ac4e JA |
249 | if (fsync(f->fd) < 0) { |
250 | td_verror(td, errno, "fsync"); | |
251 | goto err; | |
252 | } | |
253 | } | |
0d1cd207 | 254 | if (td->o.fill_device && !td_write(td)) { |
d6aed795 | 255 | fio_file_clear_size_known(f); |
5e0074c2 JA |
256 | if (td_io_get_file_size(td, f)) |
257 | goto err; | |
258 | if (f->io_size > f->real_file_size) | |
259 | f->io_size = f->real_file_size; | |
260 | } | |
53cdc686 | 261 | |
8c43ba62 | 262 | fio_memfree(b, bs); |
507a702f | 263 | done: |
53cdc686 JA |
264 | return 0; |
265 | err: | |
266 | close(f->fd); | |
267 | f->fd = -1; | |
f3e1eb23 | 268 | if (b) |
8c43ba62 | 269 | fio_memfree(b, bs); |
53cdc686 JA |
270 | return 1; |
271 | } | |
272 | ||
afad68f7 ZY |
273 | static int pre_read_file(struct thread_data *td, struct fio_file *f) |
274 | { | |
ef799a64 | 275 | int ret = 0, r, did_open = 0, old_runstate; |
afad68f7 ZY |
276 | unsigned long long left; |
277 | unsigned int bs; | |
278 | char *b; | |
279 | ||
736f1188 TK |
280 | if (td_ioengine_flagged(td, FIO_PIPEIO) || |
281 | td_ioengine_flagged(td, FIO_NOIO)) | |
9c0d2241 JA |
282 | return 0; |
283 | ||
e658078b TK |
284 | if (f->filetype == FIO_TYPE_CHAR) |
285 | return 0; | |
286 | ||
d6aed795 | 287 | if (!fio_file_open(f)) { |
b0f65863 JA |
288 | if (td->io_ops->open_file(td, f)) { |
289 | log_err("fio: cannot pre-read, failed to open file\n"); | |
290 | return 1; | |
291 | } | |
292 | did_open = 1; | |
293 | } | |
294 | ||
8edd973d | 295 | old_runstate = td_bump_runstate(td, TD_PRE_READING); |
b0f65863 | 296 | |
82d8de31 | 297 | left = f->io_size; |
afad68f7 | 298 | bs = td->o.max_bs[DDIR_READ]; |
82d8de31 TK |
299 | if (bs > left) |
300 | bs = left; | |
301 | ||
afad68f7 | 302 | b = malloc(bs); |
84af30a1 TK |
303 | if (!b) { |
304 | td_verror(td, errno, "malloc"); | |
305 | ret = 1; | |
306 | goto error; | |
307 | } | |
afad68f7 ZY |
308 | memset(b, 0, bs); |
309 | ||
ef799a64 JA |
310 | if (lseek(f->fd, f->file_offset, SEEK_SET) < 0) { |
311 | td_verror(td, errno, "lseek"); | |
312 | log_err("fio: failed to lseek pre-read file\n"); | |
313 | ret = 1; | |
314 | goto error; | |
315 | } | |
316 | ||
afad68f7 ZY |
317 | while (left && !td->terminate) { |
318 | if (bs > left) | |
319 | bs = left; | |
320 | ||
321 | r = read(f->fd, b, bs); | |
322 | ||
323 | if (r == (int) bs) { | |
324 | left -= bs; | |
325 | continue; | |
326 | } else { | |
327 | td_verror(td, EIO, "pre_read"); | |
328 | break; | |
329 | } | |
330 | } | |
331 | ||
ef799a64 | 332 | error: |
8edd973d | 333 | td_restore_runstate(td, old_runstate); |
b0f65863 JA |
334 | |
335 | if (did_open) | |
336 | td->io_ops->close_file(td, f); | |
ef799a64 | 337 | |
afad68f7 | 338 | free(b); |
ef799a64 | 339 | return ret; |
afad68f7 ZY |
340 | } |
341 | ||
a3f001f5 | 342 | unsigned long long get_rand_file_size(struct thread_data *td) |
9c60ce64 | 343 | { |
dc873b6f | 344 | unsigned long long ret, sized; |
c3546b53 | 345 | uint64_t frand_max; |
1294c3ec | 346 | unsigned long r; |
9c60ce64 | 347 | |
c3546b53 | 348 | frand_max = rand_max(&td->file_size_state); |
d6b72507 | 349 | r = __rand(&td->file_size_state); |
54a21917 | 350 | sized = td->o.file_size_high - td->o.file_size_low; |
c3546b53 | 351 | ret = (unsigned long long) ((double) sized * (r / (frand_max + 1.0))); |
5ec10eaa | 352 | ret += td->o.file_size_low; |
2dc1bbeb | 353 | ret -= (ret % td->o.rw_min_bs); |
9c60ce64 JA |
354 | return ret; |
355 | } | |
356 | ||
53cdc686 JA |
357 | static int file_size(struct thread_data *td, struct fio_file *f) |
358 | { | |
359 | struct stat st; | |
360 | ||
df9c26b1 | 361 | if (stat(f->file_name, &st) == -1) { |
7bb48f84 JA |
362 | td_verror(td, errno, "fstat"); |
363 | return 1; | |
364 | } | |
53cdc686 | 365 | |
7bb48f84 | 366 | f->real_file_size = st.st_size; |
53cdc686 JA |
367 | return 0; |
368 | } | |
369 | ||
370 | static int bdev_size(struct thread_data *td, struct fio_file *f) | |
371 | { | |
9b836561 | 372 | unsigned long long bytes = 0; |
53cdc686 JA |
373 | int r; |
374 | ||
df9c26b1 JA |
375 | if (td->io_ops->open_file(td, f)) { |
376 | log_err("fio: failed opening blockdev %s for size check\n", | |
377 | f->file_name); | |
378 | return 1; | |
379 | } | |
380 | ||
ecc314ba | 381 | r = blockdev_size(f, &bytes); |
53cdc686 | 382 | if (r) { |
e1161c32 | 383 | td_verror(td, r, "blockdev_size"); |
df9c26b1 | 384 | goto err; |
53cdc686 JA |
385 | } |
386 | ||
7ab077ab JA |
387 | if (!bytes) { |
388 | log_err("%s: zero sized block device?\n", f->file_name); | |
df9c26b1 | 389 | goto err; |
7ab077ab JA |
390 | } |
391 | ||
53cdc686 | 392 | f->real_file_size = bytes; |
22a57ba8 | 393 | td->io_ops->close_file(td, f); |
53cdc686 | 394 | return 0; |
df9c26b1 JA |
395 | err: |
396 | td->io_ops->close_file(td, f); | |
397 | return 1; | |
53cdc686 JA |
398 | } |
399 | ||
4ccdccd1 JA |
400 | static int char_size(struct thread_data *td, struct fio_file *f) |
401 | { | |
402 | #ifdef FIO_HAVE_CHARDEV_SIZE | |
9b836561 | 403 | unsigned long long bytes = 0; |
4ccdccd1 JA |
404 | int r; |
405 | ||
406 | if (td->io_ops->open_file(td, f)) { | |
b3ec877c | 407 | log_err("fio: failed opening chardev %s for size check\n", |
4ccdccd1 JA |
408 | f->file_name); |
409 | return 1; | |
410 | } | |
411 | ||
ecc314ba | 412 | r = chardev_size(f, &bytes); |
4ccdccd1 JA |
413 | if (r) { |
414 | td_verror(td, r, "chardev_size"); | |
415 | goto err; | |
416 | } | |
417 | ||
418 | if (!bytes) { | |
419 | log_err("%s: zero sized char device?\n", f->file_name); | |
420 | goto err; | |
421 | } | |
422 | ||
423 | f->real_file_size = bytes; | |
424 | td->io_ops->close_file(td, f); | |
425 | return 0; | |
426 | err: | |
427 | td->io_ops->close_file(td, f); | |
428 | return 1; | |
429 | #else | |
430 | f->real_file_size = -1ULL; | |
431 | return 0; | |
432 | #endif | |
433 | } | |
434 | ||
53cdc686 JA |
435 | static int get_file_size(struct thread_data *td, struct fio_file *f) |
436 | { | |
437 | int ret = 0; | |
438 | ||
d6aed795 | 439 | if (fio_file_size_known(f)) |
409b3417 JA |
440 | return 0; |
441 | ||
7bb48f84 JA |
442 | if (f->filetype == FIO_TYPE_FILE) |
443 | ret = file_size(td, f); | |
686fbd31 | 444 | else if (f->filetype == FIO_TYPE_BLOCK) |
53cdc686 | 445 | ret = bdev_size(td, f); |
4ccdccd1 JA |
446 | else if (f->filetype == FIO_TYPE_CHAR) |
447 | ret = char_size(td, f); | |
0f34169a TK |
448 | else |
449 | f->real_file_size = -1ULL; | |
53cdc686 | 450 | |
79591fa9 TK |
451 | /* |
452 | * Leave ->real_file_size with 0 since it could be expectation | |
453 | * of initial setup for regular files. | |
454 | */ | |
a0cb220b | 455 | if (ret) |
53cdc686 JA |
456 | return ret; |
457 | ||
0f34169a TK |
458 | /* |
459 | * If ->real_file_size is -1, a conditional for the message | |
460 | * "offset extends end" is always true, but it makes no sense, | |
461 | * so just return the same value here. | |
462 | */ | |
463 | if (f->real_file_size == -1ULL) { | |
464 | log_info("%s: failed to get file size of %s\n", td->o.name, | |
465 | f->file_name); | |
466 | return 1; | |
467 | } | |
468 | ||
469 | if (td->o.start_offset && f->file_offset == 0) | |
470 | dprint(FD_FILE, "offset of file %s not initialized yet\n", | |
471 | f->file_name); | |
957c81b9 TK |
472 | /* |
473 | * ->file_offset normally hasn't been initialized yet, so this | |
0f34169a | 474 | * is basically always false. |
957c81b9 | 475 | */ |
53cdc686 | 476 | if (f->file_offset > f->real_file_size) { |
0f2152c1 | 477 | log_err("%s: offset extends end (%llu > %llu)\n", td->o.name, |
4e0a8fa2 JA |
478 | (unsigned long long) f->file_offset, |
479 | (unsigned long long) f->real_file_size); | |
53cdc686 JA |
480 | return 1; |
481 | } | |
482 | ||
d6aed795 | 483 | fio_file_set_size_known(f); |
53cdc686 JA |
484 | return 0; |
485 | } | |
486 | ||
3baddf24 JA |
487 | static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f, |
488 | unsigned long long off, | |
489 | unsigned long long len) | |
e5b401d4 | 490 | { |
942a851a | 491 | int errval = 0, ret = 0; |
e5b401d4 | 492 | |
c8931876 JA |
493 | #ifdef CONFIG_ESX |
494 | return 0; | |
495 | #endif | |
496 | ||
5e0074c2 | 497 | if (len == -1ULL) |
3baddf24 JA |
498 | len = f->io_size; |
499 | if (off == -1ULL) | |
500 | off = f->file_offset; | |
ee56ad50 | 501 | |
0d1cd207 JA |
502 | if (len == -1ULL || off == -1ULL) |
503 | return 0; | |
504 | ||
942a851a | 505 | if (td->io_ops->invalidate) { |
4499dbef TK |
506 | dprint(FD_IO, "invalidate %s cache %s\n", td->io_ops->name, |
507 | f->file_name); | |
d9b100fc | 508 | ret = td->io_ops->invalidate(td, f); |
942a851a | 509 | if (ret < 0) |
afd118db | 510 | errval = -ret; |
942a851a | 511 | } else if (f->filetype == FIO_TYPE_FILE) { |
4499dbef TK |
512 | dprint(FD_IO, "declare unneeded cache %s: %llu/%llu\n", |
513 | f->file_name, off, len); | |
ecc314ba | 514 | ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED); |
0b139881 | 515 | if (ret) |
942a851a | 516 | errval = ret; |
686fbd31 | 517 | } else if (f->filetype == FIO_TYPE_BLOCK) { |
2b9f9141 KR |
518 | int retry_count = 0; |
519 | ||
4499dbef | 520 | dprint(FD_IO, "drop page cache %s\n", f->file_name); |
ecc314ba | 521 | ret = blockdev_invalidate_cache(f); |
2b9f9141 KR |
522 | while (ret < 0 && errno == EAGAIN && retry_count++ < 25) { |
523 | /* | |
524 | * Linux multipath devices reject ioctl while | |
525 | * the maps are being updated. That window can | |
526 | * last tens of milliseconds; we'll try up to | |
527 | * a quarter of a second. | |
528 | */ | |
529 | usleep(10000); | |
530 | ret = blockdev_invalidate_cache(f); | |
531 | } | |
7e0e25c9 | 532 | if (ret < 0 && errno == EACCES && geteuid()) { |
7172cfe8 | 533 | if (!root_warn) { |
5ec10eaa JA |
534 | log_err("fio: only root may flush block " |
535 | "devices. Cache flush bypassed!\n"); | |
7172cfe8 JA |
536 | root_warn = 1; |
537 | } | |
7e0e25c9 JA |
538 | ret = 0; |
539 | } | |
942a851a JA |
540 | if (ret < 0) |
541 | errval = errno; | |
4499dbef TK |
542 | } else if (f->filetype == FIO_TYPE_CHAR || |
543 | f->filetype == FIO_TYPE_PIPE) { | |
544 | dprint(FD_IO, "invalidate not supported %s\n", f->file_name); | |
e5b401d4 | 545 | ret = 0; |
4499dbef | 546 | } |
e5b401d4 | 547 | |
dfe11fd1 JA |
548 | /* |
549 | * Cache flushing isn't a fatal condition, and we know it will | |
550 | * happen on some platforms where we don't have the proper | |
551 | * function to flush eg block device caches. So just warn and | |
552 | * continue on our way. | |
553 | */ | |
942a851a | 554 | if (errval) |
22de5d77 TK |
555 | log_info("fio: cache invalidation of %s failed: %s\n", |
556 | f->file_name, strerror(errval)); | |
e5b401d4 | 557 | |
dfe11fd1 | 558 | return 0; |
3baddf24 JA |
559 | |
560 | } | |
561 | ||
562 | int file_invalidate_cache(struct thread_data *td, struct fio_file *f) | |
563 | { | |
d6aed795 | 564 | if (!fio_file_open(f)) |
a5fb461f JA |
565 | return 0; |
566 | ||
5e0074c2 | 567 | return __file_invalidate_cache(td, f, -1ULL, -1ULL); |
e5b401d4 JA |
568 | } |
569 | ||
6977bcd0 | 570 | int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f) |
53cdc686 | 571 | { |
6977bcd0 JA |
572 | int ret = 0; |
573 | ||
ee56ad50 | 574 | dprint(FD_FILE, "fd close %s\n", f->file_name); |
4906e0b5 JA |
575 | |
576 | remove_file_hash(f); | |
577 | ||
6977bcd0 JA |
578 | if (close(f->fd) < 0) |
579 | ret = errno; | |
580 | ||
b5af8293 | 581 | f->fd = -1; |
e6c4d732 JA |
582 | |
583 | if (f->shadow_fd != -1) { | |
584 | close(f->shadow_fd); | |
585 | f->shadow_fd = -1; | |
586 | } | |
587 | ||
710bf9c5 | 588 | f->engine_pos = 0; |
6977bcd0 | 589 | return ret; |
53cdc686 JA |
590 | } |
591 | ||
1ccc6dc7 | 592 | int file_lookup_open(struct fio_file *f, int flags) |
53cdc686 | 593 | { |
29c1349f | 594 | struct fio_file *__f; |
4d4e80f2 JA |
595 | int from_hash; |
596 | ||
597 | __f = lookup_file_hash(f->file_name); | |
598 | if (__f) { | |
9efef3c4 | 599 | dprint(FD_FILE, "found file in hash %s\n", f->file_name); |
4d4e80f2 | 600 | f->lock = __f->lock; |
4d4e80f2 JA |
601 | from_hash = 1; |
602 | } else { | |
9efef3c4 | 603 | dprint(FD_FILE, "file not found in hash %s\n", f->file_name); |
4d4e80f2 JA |
604 | from_hash = 0; |
605 | } | |
606 | ||
9c0f3f32 BC |
607 | #ifdef WIN32 |
608 | flags |= _O_BINARY; | |
609 | #endif | |
610 | ||
e8670ef8 | 611 | f->fd = open(f->file_name, flags, 0600); |
4d4e80f2 JA |
612 | return from_hash; |
613 | } | |
614 | ||
e6c4d732 JA |
615 | static int file_close_shadow_fds(struct thread_data *td) |
616 | { | |
617 | struct fio_file *f; | |
618 | int num_closed = 0; | |
619 | unsigned int i; | |
620 | ||
621 | for_each_file(td, f, i) { | |
622 | if (f->shadow_fd == -1) | |
623 | continue; | |
624 | ||
625 | close(f->shadow_fd); | |
626 | f->shadow_fd = -1; | |
627 | num_closed++; | |
628 | } | |
629 | ||
630 | return num_closed; | |
631 | } | |
632 | ||
4d4e80f2 JA |
633 | int generic_open_file(struct thread_data *td, struct fio_file *f) |
634 | { | |
66159828 | 635 | int is_std = 0; |
53cdc686 | 636 | int flags = 0; |
29c1349f | 637 | int from_hash = 0; |
53cdc686 | 638 | |
ee56ad50 JA |
639 | dprint(FD_FILE, "fd open %s\n", f->file_name); |
640 | ||
66159828 JA |
641 | if (!strcmp(f->file_name, "-")) { |
642 | if (td_rw(td)) { | |
643 | log_err("fio: can't read/write to stdin/out\n"); | |
644 | return 1; | |
645 | } | |
646 | is_std = 1; | |
ce98fa66 JA |
647 | |
648 | /* | |
649 | * move output logging to stderr, if we are writing to stdout | |
650 | */ | |
651 | if (td_write(td)) | |
652 | f_out = stderr; | |
66159828 JA |
653 | } |
654 | ||
6eaf09d6 SL |
655 | if (td_trim(td)) |
656 | goto skip_flags; | |
2dc1bbeb | 657 | if (td->o.odirect) |
2fd233b7 | 658 | flags |= OS_O_DIRECT; |
d01612f3 CM |
659 | if (td->o.oatomic) { |
660 | if (!FIO_O_ATOMIC) { | |
661 | td_verror(td, EINVAL, "OS does not support atomic IO"); | |
662 | return 1; | |
663 | } | |
664 | flags |= OS_O_DIRECT | FIO_O_ATOMIC; | |
665 | } | |
2dc1bbeb | 666 | if (td->o.sync_io) |
2fd233b7 | 667 | flags |= O_SYNC; |
2378826d | 668 | if (td->o.create_on_open && td->o.allow_create) |
814452bd | 669 | flags |= O_CREAT; |
6eaf09d6 SL |
670 | skip_flags: |
671 | if (f->filetype != FIO_TYPE_FILE) | |
672 | flags |= FIO_O_NOATIME; | |
53cdc686 | 673 | |
056f3459 | 674 | open_again: |
660a1cb5 | 675 | if (td_write(td)) { |
17308158 JA |
676 | if (!read_only) |
677 | flags |= O_RDWR; | |
53cdc686 | 678 | |
2378826d | 679 | if (f->filetype == FIO_TYPE_FILE && td->o.allow_create) |
2fd233b7 | 680 | flags |= O_CREAT; |
2fd233b7 | 681 | |
66159828 JA |
682 | if (is_std) |
683 | f->fd = dup(STDOUT_FILENO); | |
4d4e80f2 JA |
684 | else |
685 | from_hash = file_lookup_open(f, flags); | |
6eaf09d6 | 686 | } else if (td_read(td)) { |
4241ea8f | 687 | if (f->filetype == FIO_TYPE_CHAR && !read_only) |
2fd233b7 JA |
688 | flags |= O_RDWR; |
689 | else | |
690 | flags |= O_RDONLY; | |
691 | ||
66159828 JA |
692 | if (is_std) |
693 | f->fd = dup(STDIN_FILENO); | |
4d4e80f2 JA |
694 | else |
695 | from_hash = file_lookup_open(f, flags); | |
49cff020 TK |
696 | } else if (td_trim(td)) { |
697 | assert(!td_rw(td)); /* should have matched above */ | |
6eaf09d6 SL |
698 | flags |= O_RDWR; |
699 | from_hash = file_lookup_open(f, flags); | |
53cdc686 JA |
700 | } |
701 | ||
702 | if (f->fd == -1) { | |
e4e33258 | 703 | char buf[FIO_VERROR_SIZE]; |
e1161c32 JA |
704 | int __e = errno; |
705 | ||
835d9b9e | 706 | if (__e == EPERM && (flags & FIO_O_NOATIME)) { |
5921e80c | 707 | flags &= ~FIO_O_NOATIME; |
056f3459 AC |
708 | goto open_again; |
709 | } | |
e6c4d732 JA |
710 | if (__e == EMFILE && file_close_shadow_fds(td)) |
711 | goto open_again; | |
056f3459 | 712 | |
98ffb8f3 | 713 | snprintf(buf, sizeof(buf), "open(%s)", f->file_name); |
e4e33258 | 714 | |
a93c5f04 JA |
715 | if (__e == EINVAL && (flags & OS_O_DIRECT)) { |
716 | log_err("fio: looks like your file system does not " \ | |
717 | "support direct=1/buffered=0\n"); | |
718 | } | |
719 | ||
e4e33258 | 720 | td_verror(td, __e, buf); |
3ce3ad33 | 721 | return 1; |
53cdc686 JA |
722 | } |
723 | ||
29c1349f JA |
724 | if (!from_hash && f->fd != -1) { |
725 | if (add_file_hash(f)) { | |
3f0ca9b9 | 726 | int fio_unused ret; |
29c1349f JA |
727 | |
728 | /* | |
e6c4d732 JA |
729 | * Stash away descriptor for later close. This is to |
730 | * work-around a "feature" on Linux, where a close of | |
731 | * an fd that has been opened for write will trigger | |
732 | * udev to call blkid to check partitions, fs id, etc. | |
de8f6de9 | 733 | * That pollutes the device cache, which can slow down |
e6c4d732 | 734 | * unbuffered accesses. |
29c1349f | 735 | */ |
e6c4d732 JA |
736 | if (f->shadow_fd == -1) |
737 | f->shadow_fd = f->fd; | |
738 | else { | |
739 | /* | |
740 | * OK to ignore, we haven't done anything | |
741 | * with it | |
742 | */ | |
743 | ret = generic_close_file(td, f); | |
744 | } | |
29c1349f JA |
745 | goto open_again; |
746 | } | |
747 | } | |
4906e0b5 | 748 | |
53cdc686 | 749 | return 0; |
b5af8293 JA |
750 | } |
751 | ||
79591fa9 TK |
752 | /* |
753 | * This function i.e. get_file_size() is the default .get_file_size | |
754 | * implementation of majority of I/O engines. | |
755 | */ | |
df9c26b1 | 756 | int generic_get_file_size(struct thread_data *td, struct fio_file *f) |
21972cde | 757 | { |
df9c26b1 | 758 | return get_file_size(td, f); |
21972cde JA |
759 | } |
760 | ||
7bb48f84 JA |
761 | /* |
762 | * open/close all files, so that ->real_file_size gets set | |
763 | */ | |
bab3fd58 | 764 | static int get_file_sizes(struct thread_data *td) |
7bb48f84 JA |
765 | { |
766 | struct fio_file *f; | |
767 | unsigned int i; | |
bab3fd58 | 768 | int err = 0; |
7bb48f84 JA |
769 | |
770 | for_each_file(td, f, i) { | |
08c44a55 | 771 | dprint(FD_FILE, "get file size for %p/%d/%s\n", f, i, |
5ec10eaa | 772 | f->file_name); |
9efef3c4 | 773 | |
99a47c69 | 774 | if (td_io_get_file_size(td, f)) { |
40b44f4a JA |
775 | if (td->error != ENOENT) { |
776 | log_err("%s\n", td->verror); | |
777 | err = 1; | |
3ce3ad33 | 778 | break; |
40b44f4a | 779 | } |
541d66d7 | 780 | clear_error(td); |
07eb79df | 781 | } |
409b3417 | 782 | |
79591fa9 TK |
783 | /* |
784 | * There are corner cases where we end up with -1 for | |
785 | * ->real_file_size due to unsupported file type, etc. | |
786 | * We then just set to size option value divided by number | |
787 | * of files, similar to the way file ->io_size is set. | |
788 | * stat(2) failure doesn't set ->real_file_size to -1. | |
789 | */ | |
409b3417 JA |
790 | if (f->real_file_size == -1ULL && td->o.size) |
791 | f->real_file_size = td->o.size / td->o.nr_files; | |
7bb48f84 | 792 | } |
bab3fd58 JA |
793 | |
794 | return err; | |
7bb48f84 JA |
795 | } |
796 | ||
2e3bd4c2 JA |
797 | struct fio_mount { |
798 | struct flist_head list; | |
799 | const char *base; | |
800 | char __base[256]; | |
801 | unsigned int key; | |
802 | }; | |
803 | ||
804 | /* | |
805 | * Get free number of bytes for each file on each unique mount. | |
806 | */ | |
807 | static unsigned long long get_fs_free_counts(struct thread_data *td) | |
808 | { | |
809 | struct flist_head *n, *tmp; | |
68b0316f | 810 | unsigned long long ret = 0; |
2e3bd4c2 JA |
811 | struct fio_mount *fm; |
812 | FLIST_HEAD(list); | |
813 | struct fio_file *f; | |
814 | unsigned int i; | |
815 | ||
816 | for_each_file(td, f, i) { | |
817 | struct stat sb; | |
818 | char buf[256]; | |
819 | ||
686fbd31 | 820 | if (f->filetype == FIO_TYPE_BLOCK || f->filetype == FIO_TYPE_CHAR) { |
4ccdccd1 JA |
821 | if (f->real_file_size != -1ULL) |
822 | ret += f->real_file_size; | |
68b0316f JA |
823 | continue; |
824 | } else if (f->filetype != FIO_TYPE_FILE) | |
825 | continue; | |
826 | ||
da27a4bf JA |
827 | buf[255] = '\0'; |
828 | strncpy(buf, f->file_name, 255); | |
2e3bd4c2 JA |
829 | |
830 | if (stat(buf, &sb) < 0) { | |
831 | if (errno != ENOENT) | |
832 | break; | |
833 | strcpy(buf, "."); | |
834 | if (stat(buf, &sb) < 0) | |
835 | break; | |
836 | } | |
837 | ||
838 | fm = NULL; | |
839 | flist_for_each(n, &list) { | |
840 | fm = flist_entry(n, struct fio_mount, list); | |
841 | if (fm->key == sb.st_dev) | |
842 | break; | |
843 | ||
844 | fm = NULL; | |
845 | } | |
846 | ||
847 | if (fm) | |
848 | continue; | |
849 | ||
3660ceae JA |
850 | fm = calloc(1, sizeof(*fm)); |
851 | strncpy(fm->__base, buf, sizeof(fm->__base) - 1); | |
2e3bd4c2 JA |
852 | fm->base = basename(fm->__base); |
853 | fm->key = sb.st_dev; | |
854 | flist_add(&fm->list, &list); | |
855 | } | |
856 | ||
2e3bd4c2 JA |
857 | flist_for_each_safe(n, tmp, &list) { |
858 | unsigned long long sz; | |
859 | ||
860 | fm = flist_entry(n, struct fio_mount, list); | |
861 | flist_del(&fm->list); | |
862 | ||
c08ad04c | 863 | sz = get_fs_free_size(fm->base); |
2e3bd4c2 JA |
864 | if (sz && sz != -1ULL) |
865 | ret += sz; | |
866 | ||
867 | free(fm); | |
868 | } | |
869 | ||
870 | return ret; | |
871 | } | |
872 | ||
bedc9dc2 | 873 | uint64_t get_start_offset(struct thread_data *td, struct fio_file *f) |
ce95d651 | 874 | { |
bedc9dc2 | 875 | struct thread_options *o = &td->o; |
872e0415 JA |
876 | unsigned long long align_bs; |
877 | unsigned long long offset; | |
bedc9dc2 JA |
878 | |
879 | if (o->file_append && f->filetype == FIO_TYPE_FILE) | |
880 | return f->real_file_size; | |
881 | ||
89978a6b | 882 | if (o->start_offset_percent > 0) { |
872e0415 JA |
883 | /* |
884 | * if blockalign is provided, find the min across read, write, | |
885 | * and trim | |
886 | */ | |
89978a6b BW |
887 | if (fio_option_is_set(o, ba)) { |
888 | align_bs = (unsigned long long) min(o->ba[DDIR_READ], o->ba[DDIR_WRITE]); | |
889 | align_bs = min((unsigned long long) o->ba[DDIR_TRIM], align_bs); | |
872e0415 JA |
890 | } else { |
891 | /* else take the minimum block size */ | |
89978a6b BW |
892 | align_bs = td_min_bs(td); |
893 | } | |
894 | ||
895 | /* calculate the raw offset */ | |
896 | offset = (f->real_file_size * o->start_offset_percent / 100) + | |
897 | (td->subjob_number * o->offset_increment); | |
898 | ||
872e0415 JA |
899 | /* |
900 | * block align the offset at the next available boundary at | |
901 | * ceiling(offset / align_bs) * align_bs | |
902 | */ | |
89978a6b BW |
903 | offset = (offset / align_bs + (offset % align_bs != 0)) * align_bs; |
904 | ||
872e0415 JA |
905 | } else { |
906 | /* start_offset_percent not set */ | |
f0d13935 JA |
907 | offset = o->start_offset + |
908 | td->subjob_number * o->offset_increment; | |
89978a6b | 909 | } |
872e0415 | 910 | |
89978a6b | 911 | return offset; |
ce95d651 DE |
912 | } |
913 | ||
7bb48f84 JA |
914 | /* |
915 | * Open the files and setup files sizes, creating files if necessary. | |
916 | */ | |
53cdc686 JA |
917 | int setup_files(struct thread_data *td) |
918 | { | |
7bb48f84 | 919 | unsigned long long total_size, extend_size; |
de98bd30 | 920 | struct thread_options *o = &td->o; |
53cdc686 | 921 | struct fio_file *f; |
002fe734 | 922 | unsigned int i, nr_fs_extra = 0; |
000b0803 | 923 | int err = 0, need_extend; |
e90391eb | 924 | int old_state; |
002fe734 JA |
925 | const unsigned int bs = td_min_bs(td); |
926 | uint64_t fs = 0; | |
53cdc686 | 927 | |
ee56ad50 JA |
928 | dprint(FD_FILE, "setup files\n"); |
929 | ||
8edd973d | 930 | old_state = td_bump_runstate(td, TD_SETTING_UP); |
e90391eb | 931 | |
de98bd30 | 932 | if (o->read_iolog_file) |
25460cf6 | 933 | goto done; |
691c8fb0 | 934 | |
53cdc686 | 935 | /* |
79591fa9 TK |
936 | * Find out physical size of files or devices for this thread, |
937 | * before we determine I/O size and range of our targets. | |
938 | * If ioengine defines a setup() method, it's responsible for | |
7bb48f84 JA |
939 | * opening the files and setting f->real_file_size to indicate |
940 | * the valid range for that file. | |
53cdc686 JA |
941 | */ |
942 | if (td->io_ops->setup) | |
7bb48f84 JA |
943 | err = td->io_ops->setup(td); |
944 | else | |
bab3fd58 | 945 | err = get_file_sizes(td); |
53cdc686 | 946 | |
f1027063 | 947 | if (err) |
e90391eb | 948 | goto err_out; |
f1027063 | 949 | |
0a7eb121 | 950 | /* |
7bb48f84 JA |
951 | * check sizes. if the files/devices do not exist and the size |
952 | * isn't passed to fio, abort. | |
0a7eb121 | 953 | */ |
7bb48f84 JA |
954 | total_size = 0; |
955 | for_each_file(td, f, i) { | |
49a416bd | 956 | f->fileno = i; |
7bb48f84 JA |
957 | if (f->real_file_size == -1ULL) |
958 | total_size = -1ULL; | |
959 | else | |
960 | total_size += f->real_file_size; | |
961 | } | |
0a7eb121 | 962 | |
de98bd30 | 963 | if (o->fill_device) |
2e3bd4c2 JA |
964 | td->fill_device_size = get_fs_free_counts(td); |
965 | ||
7bb48f84 JA |
966 | /* |
967 | * device/file sizes are zero and no size given, punt | |
968 | */ | |
de98bd30 | 969 | if ((!total_size || total_size == -1ULL) && !o->size && |
9b87f09b | 970 | !td_ioengine_flagged(td, FIO_NOIO) && !o->fill_device && |
de98bd30 JA |
971 | !(o->nr_files && (o->file_size_low || o->file_size_high))) { |
972 | log_err("%s: you need to specify size=\n", o->name); | |
e1161c32 | 973 | td_verror(td, EINVAL, "total_file_size"); |
e90391eb | 974 | goto err_out; |
53cdc686 JA |
975 | } |
976 | ||
002fe734 JA |
977 | /* |
978 | * Calculate per-file size and potential extra size for the | |
79591fa9 | 979 | * first files, if needed (i.e. if we don't have a fixed size). |
002fe734 | 980 | */ |
d1e78e6b | 981 | if (!o->file_size_low && o->nr_files) { |
002fe734 JA |
982 | uint64_t all_fs; |
983 | ||
984 | fs = o->size / o->nr_files; | |
985 | all_fs = fs * o->nr_files; | |
986 | ||
987 | if (all_fs < o->size) | |
988 | nr_fs_extra = (o->size - all_fs) / bs; | |
989 | } | |
990 | ||
7bb48f84 JA |
991 | /* |
992 | * now file sizes are known, so we can set ->io_size. if size= is | |
993 | * not given, ->io_size is just equal to ->real_file_size. if size | |
994 | * is given, ->io_size is size / nr_files. | |
995 | */ | |
996 | extend_size = total_size = 0; | |
997 | need_extend = 0; | |
998 | for_each_file(td, f, i) { | |
bedc9dc2 | 999 | f->file_offset = get_start_offset(td, f); |
bcdedd0a | 1000 | |
79591fa9 TK |
1001 | /* |
1002 | * Update ->io_size depending on options specified. | |
1003 | * ->file_size_low being 0 means filesize option isn't set. | |
1004 | * Non zero ->file_size_low equals ->file_size_high means | |
1005 | * filesize option is set in a fixed size format. | |
1006 | * Non zero ->file_size_low not equals ->file_size_high means | |
1007 | * filesize option is set in a range format. | |
1008 | */ | |
de98bd30 | 1009 | if (!o->file_size_low) { |
7bb48f84 | 1010 | /* |
79591fa9 | 1011 | * no file size or range given, file size is equal to |
c4aa2d08 | 1012 | * total size divided by number of files. If the size |
002fe734 JA |
1013 | * doesn't divide nicely with the min blocksize, |
1014 | * make the first files bigger. | |
7bb48f84 | 1015 | */ |
002fe734 JA |
1016 | f->io_size = fs; |
1017 | if (nr_fs_extra) { | |
1018 | nr_fs_extra--; | |
1019 | f->io_size += bs; | |
1020 | } | |
1021 | ||
c4aa2d08 | 1022 | /* |
c6dfd837 TK |
1023 | * We normally don't come here for regular files, but |
1024 | * if the result is 0 for a regular file, set it to the | |
1025 | * real file size. This could be size of the existing | |
1026 | * one if it already exists, but otherwise will be set | |
1027 | * to 0. A new file won't be created because | |
c4aa2d08 TK |
1028 | * ->io_size + ->file_offset equals ->real_file_size. |
1029 | */ | |
1030 | if (!f->io_size) { | |
1031 | if (f->file_offset > f->real_file_size) | |
1032 | goto err_offset; | |
273f8c91 | 1033 | f->io_size = f->real_file_size - f->file_offset; |
c4aa2d08 TK |
1034 | if (!f->io_size) |
1035 | log_info("fio: file %s may be ignored\n", | |
1036 | f->file_name); | |
1037 | } | |
de98bd30 JA |
1038 | } else if (f->real_file_size < o->file_size_low || |
1039 | f->real_file_size > o->file_size_high) { | |
1040 | if (f->file_offset > o->file_size_low) | |
bcdedd0a | 1041 | goto err_offset; |
7bb48f84 JA |
1042 | /* |
1043 | * file size given. if it's fixed, use that. if it's a | |
1044 | * range, generate a random size in-between. | |
1045 | */ | |
de98bd30 JA |
1046 | if (o->file_size_low == o->file_size_high) |
1047 | f->io_size = o->file_size_low - f->file_offset; | |
1048 | else { | |
5ec10eaa JA |
1049 | f->io_size = get_rand_file_size(td) |
1050 | - f->file_offset; | |
1051 | } | |
65bdb10a | 1052 | } else |
bcdedd0a | 1053 | f->io_size = f->real_file_size - f->file_offset; |
53cdc686 | 1054 | |
7bb48f84 JA |
1055 | if (f->io_size == -1ULL) |
1056 | total_size = -1ULL; | |
4d002569 | 1057 | else { |
e391c704 | 1058 | if (o->size_percent) { |
76e4bb4f SW |
1059 | uint64_t file_size; |
1060 | ||
1061 | file_size = f->io_size + f->file_offset; | |
1062 | f->io_size = (file_size * | |
1063 | o->size_percent) / 100; | |
1064 | if (f->io_size > (file_size - f->file_offset)) | |
1065 | f->io_size = file_size - f->file_offset; | |
1066 | ||
e391c704 JA |
1067 | f->io_size -= (f->io_size % td_min_bs(td)); |
1068 | } | |
7bb48f84 | 1069 | total_size += f->io_size; |
4d002569 | 1070 | } |
7bb48f84 JA |
1071 | |
1072 | if (f->filetype == FIO_TYPE_FILE && | |
bcdedd0a | 1073 | (f->io_size + f->file_offset) > f->real_file_size && |
9b87f09b | 1074 | !td_ioengine_flagged(td, FIO_DISKLESSIO)) { |
5fd31680 JA |
1075 | if (!o->create_on_open) { |
1076 | need_extend++; | |
814452bd | 1077 | extend_size += (f->io_size + f->file_offset); |
43558406 | 1078 | fio_file_set_extend(f); |
5fd31680 | 1079 | } else |
814452bd | 1080 | f->real_file_size = f->io_size + f->file_offset; |
5ec10eaa | 1081 | } |
7bb48f84 | 1082 | } |
53cdc686 | 1083 | |
66347cfa DE |
1084 | if (td->o.block_error_hist) { |
1085 | int len; | |
1086 | ||
1087 | assert(td->o.nr_files == 1); /* checked in fixup_options */ | |
1088 | f = td->files[0]; | |
1089 | len = f->io_size / td->o.bs[DDIR_TRIM]; | |
1090 | if (len > MAX_NR_BLOCK_INFOS || len <= 0) { | |
1091 | log_err("fio: cannot calculate block histogram with " | |
1092 | "%d trim blocks, maximum %d\n", | |
1093 | len, MAX_NR_BLOCK_INFOS); | |
1094 | td_verror(td, EINVAL, "block_error_hist"); | |
1095 | goto err_out; | |
1096 | } | |
1097 | ||
1098 | td->ts.nr_block_infos = len; | |
8a68c41c | 1099 | for (i = 0; i < len; i++) |
66347cfa DE |
1100 | td->ts.block_infos[i] = |
1101 | BLOCK_INFO(0, BLOCK_STATE_UNINIT); | |
1102 | } else | |
1103 | td->ts.nr_block_infos = 0; | |
1104 | ||
0bc27b0b | 1105 | if (!o->size || (total_size && o->size > total_size)) |
de98bd30 | 1106 | o->size = total_size; |
21972cde | 1107 | |
d1faa06d JA |
1108 | if (o->size < td_min_bs(td)) { |
1109 | log_err("fio: blocksize too large for data set\n"); | |
1110 | goto err_out; | |
1111 | } | |
1112 | ||
7bb48f84 | 1113 | /* |
79591fa9 TK |
1114 | * See if we need to extend some files, typically needed when our |
1115 | * target regular files don't exist yet, but our jobs require them | |
1116 | * initially due to read I/Os. | |
7bb48f84 JA |
1117 | */ |
1118 | if (need_extend) { | |
1119 | temp_stall_ts = 1; | |
7c5f4cdb TK |
1120 | if (output_format & FIO_OUTPUT_NORMAL) { |
1121 | log_info("%s: Laying out IO file%s (%u file%s / %s%lluMiB)\n", | |
1122 | o->name, | |
1123 | need_extend > 1 ? "s" : "", | |
1124 | need_extend, | |
1125 | need_extend > 1 ? "s" : "", | |
1126 | need_extend > 1 ? "total " : "", | |
1127 | extend_size >> 20); | |
1128 | } | |
7bb48f84 JA |
1129 | |
1130 | for_each_file(td, f, i) { | |
5e0074c2 | 1131 | unsigned long long old_len = -1ULL, extend_len = -1ULL; |
3baddf24 | 1132 | |
d6aed795 | 1133 | if (!fio_file_extend(f)) |
7bb48f84 JA |
1134 | continue; |
1135 | ||
409b3417 | 1136 | assert(f->filetype == FIO_TYPE_FILE); |
d6aed795 | 1137 | fio_file_clear_extend(f); |
de98bd30 | 1138 | if (!o->fill_device) { |
5e0074c2 | 1139 | old_len = f->real_file_size; |
0b9d69ec JA |
1140 | extend_len = f->io_size + f->file_offset - |
1141 | old_len; | |
5e0074c2 | 1142 | } |
bcdedd0a | 1143 | f->real_file_size = (f->io_size + f->file_offset); |
7bb48f84 JA |
1144 | err = extend_file(td, f); |
1145 | if (err) | |
3baddf24 | 1146 | break; |
5e0074c2 | 1147 | |
3baddf24 JA |
1148 | err = __file_invalidate_cache(td, f, old_len, |
1149 | extend_len); | |
9824f73b JA |
1150 | |
1151 | /* | |
1152 | * Shut up static checker | |
1153 | */ | |
1154 | if (f->fd != -1) | |
1155 | close(f->fd); | |
1156 | ||
3baddf24 JA |
1157 | f->fd = -1; |
1158 | if (err) | |
7bb48f84 JA |
1159 | break; |
1160 | } | |
1161 | temp_stall_ts = 0; | |
1162 | } | |
1163 | ||
1164 | if (err) | |
e90391eb | 1165 | goto err_out; |
7bb48f84 | 1166 | |
de98bd30 JA |
1167 | if (!o->zone_size) |
1168 | o->zone_size = o->size; | |
7bb48f84 | 1169 | |
ea966f81 JA |
1170 | /* |
1171 | * iolog already set the total io size, if we read back | |
1172 | * stored entries. | |
1173 | */ | |
77731b29 | 1174 | if (!o->read_iolog_file) { |
5be9bf09 TK |
1175 | if (o->io_size) |
1176 | td->total_io_size = o->io_size * o->loops; | |
77731b29 JA |
1177 | else |
1178 | td->total_io_size = o->size * o->loops; | |
1179 | } | |
25460cf6 JA |
1180 | |
1181 | done: | |
de98bd30 | 1182 | if (o->create_only) |
25460cf6 JA |
1183 | td->done = 1; |
1184 | ||
8edd973d | 1185 | td_restore_runstate(td, old_state); |
7bb48f84 | 1186 | return 0; |
bcdedd0a | 1187 | err_offset: |
de98bd30 | 1188 | log_err("%s: you need to specify valid offset=\n", o->name); |
e90391eb | 1189 | err_out: |
8edd973d | 1190 | td_restore_runstate(td, old_state); |
bcdedd0a | 1191 | return 1; |
53cdc686 JA |
1192 | } |
1193 | ||
afad68f7 ZY |
1194 | int pre_read_files(struct thread_data *td) |
1195 | { | |
1196 | struct fio_file *f; | |
1197 | unsigned int i; | |
1198 | ||
1199 | dprint(FD_FILE, "pre_read files\n"); | |
1200 | ||
1201 | for_each_file(td, f, i) { | |
afe0d59a TK |
1202 | if (pre_read_file(td, f)) |
1203 | return -1; | |
afad68f7 ZY |
1204 | } |
1205 | ||
afe0d59a | 1206 | return 0; |
afad68f7 ZY |
1207 | } |
1208 | ||
9c6f6316 JA |
1209 | static int __init_rand_distribution(struct thread_data *td, struct fio_file *f) |
1210 | { | |
2316296a | 1211 | unsigned int range_size, seed; |
9c6f6316 | 1212 | unsigned long nranges; |
42da5c8b | 1213 | uint64_t fsize; |
9c6f6316 JA |
1214 | |
1215 | range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]); | |
42da5c8b | 1216 | fsize = min(f->real_file_size, f->io_size); |
9c6f6316 | 1217 | |
42da5c8b | 1218 | nranges = (fsize + range_size - 1) / range_size; |
9c6f6316 | 1219 | |
2316296a | 1220 | seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number; |
8425687e JA |
1221 | if (!td->o.rand_repeatable) |
1222 | seed = td->rand_seeds[4]; | |
1223 | ||
9c6f6316 | 1224 | if (td->o.random_distribution == FIO_RAND_DIST_ZIPF) |
888677a4 | 1225 | zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed); |
56d9fa4b | 1226 | else if (td->o.random_distribution == FIO_RAND_DIST_PARETO) |
888677a4 | 1227 | pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed); |
56d9fa4b | 1228 | else if (td->o.random_distribution == FIO_RAND_DIST_GAUSS) |
f88cd222 | 1229 | gauss_init(&f->gauss, nranges, td->o.gauss_dev.u.f, seed); |
9c6f6316 JA |
1230 | |
1231 | return 1; | |
1232 | } | |
1233 | ||
1234 | static int init_rand_distribution(struct thread_data *td) | |
1235 | { | |
1236 | struct fio_file *f; | |
1237 | unsigned int i; | |
1238 | int state; | |
1239 | ||
1240 | if (td->o.random_distribution == FIO_RAND_DIST_RANDOM) | |
1241 | return 0; | |
1242 | ||
8edd973d JA |
1243 | state = td_bump_runstate(td, TD_SETTING_UP); |
1244 | ||
9c6f6316 JA |
1245 | for_each_file(td, f, i) |
1246 | __init_rand_distribution(td, f); | |
8edd973d JA |
1247 | |
1248 | td_restore_runstate(td, state); | |
9c6f6316 JA |
1249 | |
1250 | return 1; | |
1251 | } | |
1252 | ||
75f90d5f JA |
1253 | /* |
1254 | * Check if the number of blocks exceeds the randomness capability of | |
1255 | * the selected generator. Tausworthe is 32-bit, the others are fullly | |
1256 | * 64-bit capable. | |
1257 | */ | |
1258 | static int check_rand_gen_limits(struct thread_data *td, struct fio_file *f, | |
1259 | uint64_t blocks) | |
1260 | { | |
1261 | if (blocks <= FRAND32_MAX) | |
1262 | return 0; | |
1263 | if (td->o.random_generator != FIO_RAND_GEN_TAUSWORTHE) | |
1264 | return 0; | |
1265 | ||
1266 | /* | |
1267 | * If the user hasn't specified a random generator, switch | |
1268 | * to tausworthe64 with informational warning. If the user did | |
1269 | * specify one, just warn. | |
1270 | */ | |
1271 | log_info("fio: file %s exceeds 32-bit tausworthe random generator.\n", | |
1272 | f->file_name); | |
1273 | ||
1274 | if (!fio_option_is_set(&td->o, random_generator)) { | |
1275 | log_info("fio: Switching to tausworthe64. Use the " | |
1276 | "random_generator= option to get rid of this " | |
4e795a3e | 1277 | "warning.\n"); |
75f90d5f JA |
1278 | td->o.random_generator = FIO_RAND_GEN_TAUSWORTHE64; |
1279 | return 0; | |
1280 | } | |
1281 | ||
1282 | /* | |
1283 | * Just make this information to avoid breaking scripts. | |
1284 | */ | |
1285 | log_info("fio: Use the random_generator= option to switch to lfsr or " | |
1286 | "tausworthe64.\n"); | |
1287 | return 0; | |
1288 | } | |
1289 | ||
68727076 JA |
1290 | int init_random_map(struct thread_data *td) |
1291 | { | |
51ede0b1 | 1292 | unsigned long long blocks; |
68727076 JA |
1293 | struct fio_file *f; |
1294 | unsigned int i; | |
1295 | ||
9c6f6316 JA |
1296 | if (init_rand_distribution(td)) |
1297 | return 0; | |
3831a843 | 1298 | if (!td_random(td)) |
68727076 JA |
1299 | return 0; |
1300 | ||
1301 | for_each_file(td, f, i) { | |
42da5c8b | 1302 | uint64_t fsize = min(f->real_file_size, f->io_size); |
38f30c81 | 1303 | |
42da5c8b | 1304 | blocks = fsize / (unsigned long long) td->o.rw_min_bs; |
53737ae0 | 1305 | |
75f90d5f | 1306 | if (check_rand_gen_limits(td, f, blocks)) |
37fbd7e9 | 1307 | return 1; |
37fbd7e9 | 1308 | |
8055e41d | 1309 | if (td->o.random_generator == FIO_RAND_GEN_LFSR) { |
82af46be JA |
1310 | unsigned long seed; |
1311 | ||
1312 | seed = td->rand_seeds[FIO_RAND_BLOCK_OFF]; | |
9c0f3f32 | 1313 | |
967d1b63 JA |
1314 | if (!lfsr_init(&f->lfsr, blocks, seed, 0)) { |
1315 | fio_file_set_lfsr(f); | |
8055e41d | 1316 | continue; |
967d1b63 | 1317 | } |
3831a843 | 1318 | } else if (!td->o.norandommap) { |
7ebd796f | 1319 | f->io_axmap = axmap_new(blocks); |
967d1b63 JA |
1320 | if (f->io_axmap) { |
1321 | fio_file_set_axmap(f); | |
8055e41d | 1322 | continue; |
967d1b63 | 1323 | } |
1cad7121 JA |
1324 | } else if (td->o.norandommap) |
1325 | continue; | |
ceadd59e | 1326 | |
2b386d25 | 1327 | if (!td->o.softrandommap) { |
5ec10eaa JA |
1328 | log_err("fio: failed allocating random map. If running" |
1329 | " a large number of jobs, try the 'norandommap'" | |
2b386d25 JA |
1330 | " option or set 'softrandommap'. Or give" |
1331 | " a larger --alloc-size to fio.\n"); | |
68727076 JA |
1332 | return 1; |
1333 | } | |
303032ae JA |
1334 | |
1335 | log_info("fio: file %s failed allocating random map. Running " | |
1336 | "job without.\n", f->file_name); | |
68727076 JA |
1337 | } |
1338 | ||
1339 | return 0; | |
1340 | } | |
1341 | ||
53cdc686 JA |
1342 | void close_files(struct thread_data *td) |
1343 | { | |
0ab8db89 | 1344 | struct fio_file *f; |
af52b345 | 1345 | unsigned int i; |
53cdc686 | 1346 | |
2be3eec3 JA |
1347 | for_each_file(td, f, i) { |
1348 | if (fio_file_open(f)) | |
1349 | td_io_close_file(td, f); | |
1350 | } | |
24ffd2c2 JA |
1351 | } |
1352 | ||
1353 | void close_and_free_files(struct thread_data *td) | |
1354 | { | |
1355 | struct fio_file *f; | |
1356 | unsigned int i; | |
1357 | ||
ee56ad50 JA |
1358 | dprint(FD_FILE, "close files\n"); |
1359 | ||
0ab8db89 | 1360 | for_each_file(td, f, i) { |
38ef9c90 CF |
1361 | if (td->o.unlink && f->filetype == FIO_TYPE_FILE) { |
1362 | dprint(FD_FILE, "free unlink %s\n", f->file_name); | |
1363 | td_io_unlink_file(td, f); | |
1364 | } | |
1365 | ||
22a57ba8 JA |
1366 | if (fio_file_open(f)) |
1367 | td_io_close_file(td, f); | |
1368 | ||
b9fbcf21 | 1369 | remove_file_hash(f); |
b3dc7f07 | 1370 | |
b5f4d8ba JA |
1371 | if (td->o.unlink && f->filetype == FIO_TYPE_FILE) { |
1372 | dprint(FD_FILE, "free unlink %s\n", f->file_name); | |
38ef9c90 | 1373 | td_io_unlink_file(td, f); |
b5f4d8ba JA |
1374 | } |
1375 | ||
f17c4392 | 1376 | sfree(f->file_name); |
fa1da865 | 1377 | f->file_name = NULL; |
967d1b63 JA |
1378 | if (fio_file_axmap(f)) { |
1379 | axmap_free(f->io_axmap); | |
1380 | f->io_axmap = NULL; | |
1381 | } | |
78d99e6a | 1382 | sfree(f); |
53cdc686 | 1383 | } |
b4a6a59a | 1384 | |
2dc1bbeb | 1385 | td->o.filename = NULL; |
cade3ef4 | 1386 | free(td->files); |
d7df1d13 | 1387 | free(td->file_locks); |
9efef3c4 | 1388 | td->files_index = 0; |
b4a6a59a | 1389 | td->files = NULL; |
d7df1d13 | 1390 | td->file_locks = NULL; |
27ddbfa0 | 1391 | td->o.file_lock_mode = FILE_LOCK_NONE; |
2dc1bbeb | 1392 | td->o.nr_files = 0; |
53cdc686 | 1393 | } |
af52b345 | 1394 | |
e3bab463 | 1395 | static void get_file_type(struct fio_file *f) |
af52b345 JA |
1396 | { |
1397 | struct stat sb; | |
1398 | ||
66159828 JA |
1399 | if (!strcmp(f->file_name, "-")) |
1400 | f->filetype = FIO_TYPE_PIPE; | |
1401 | else | |
1402 | f->filetype = FIO_TYPE_FILE; | |
af52b345 | 1403 | |
d5b78f5a | 1404 | #ifdef WIN32 |
3892182a BC |
1405 | /* \\.\ is the device namespace in Windows, where every file is |
1406 | * a block device */ | |
1407 | if (strncmp(f->file_name, "\\\\.\\", 4) == 0) | |
686fbd31 | 1408 | f->filetype = FIO_TYPE_BLOCK; |
d5b78f5a | 1409 | #endif |
3892182a | 1410 | |
b30d395e | 1411 | if (!stat(f->file_name, &sb)) { |
3892182a | 1412 | if (S_ISBLK(sb.st_mode)) |
686fbd31 | 1413 | f->filetype = FIO_TYPE_BLOCK; |
af52b345 JA |
1414 | else if (S_ISCHR(sb.st_mode)) |
1415 | f->filetype = FIO_TYPE_CHAR; | |
b5605e9d JA |
1416 | else if (S_ISFIFO(sb.st_mode)) |
1417 | f->filetype = FIO_TYPE_PIPE; | |
af52b345 JA |
1418 | } |
1419 | } | |
1420 | ||
1b2a83dc | 1421 | static bool __is_already_allocated(const char *fname, bool set) |
190b8f0c | 1422 | { |
90426237 | 1423 | struct flist_head *entry; |
1b2a83dc | 1424 | bool ret; |
bcbfeefa | 1425 | |
1b2a83dc JA |
1426 | ret = file_bloom_exists(fname, set); |
1427 | if (!ret) | |
1428 | return ret; | |
90426237 JA |
1429 | |
1430 | flist_for_each(entry, &filename_list) { | |
04d6530f | 1431 | struct file_name *fn; |
90426237 | 1432 | |
04d6530f JA |
1433 | fn = flist_entry(entry, struct file_name, list); |
1434 | ||
1435 | if (!strcmp(fn->filename, fname)) | |
1436 | return true; | |
90426237 JA |
1437 | } |
1438 | ||
04d6530f | 1439 | return false; |
bcbfeefa CE |
1440 | } |
1441 | ||
04d6530f | 1442 | static bool is_already_allocated(const char *fname) |
bcbfeefa | 1443 | { |
04d6530f | 1444 | bool ret; |
bcbfeefa | 1445 | |
90426237 | 1446 | fio_file_hash_lock(); |
1b2a83dc | 1447 | ret = __is_already_allocated(fname, false); |
90426237 | 1448 | fio_file_hash_unlock(); |
04d6530f | 1449 | |
90426237 JA |
1450 | return ret; |
1451 | } | |
bcbfeefa | 1452 | |
90426237 JA |
1453 | static void set_already_allocated(const char *fname) |
1454 | { | |
1455 | struct file_name *fn; | |
1456 | ||
1457 | fn = malloc(sizeof(struct file_name)); | |
1458 | fn->filename = strdup(fname); | |
1459 | ||
1460 | fio_file_hash_lock(); | |
1b2a83dc | 1461 | if (!__is_already_allocated(fname, true)) { |
90426237 JA |
1462 | flist_add_tail(&fn->list, &filename_list); |
1463 | fn = NULL; | |
bcbfeefa | 1464 | } |
90426237 | 1465 | fio_file_hash_unlock(); |
bcbfeefa | 1466 | |
90426237 JA |
1467 | if (fn) { |
1468 | free(fn->filename); | |
1469 | free(fn); | |
1470 | } | |
bcbfeefa CE |
1471 | } |
1472 | ||
190b8f0c CF |
1473 | static void free_already_allocated(void) |
1474 | { | |
bcbfeefa CE |
1475 | struct flist_head *entry, *tmp; |
1476 | struct file_name *fn; | |
1477 | ||
90426237 JA |
1478 | if (flist_empty(&filename_list)) |
1479 | return; | |
1480 | ||
1481 | fio_file_hash_lock(); | |
1482 | flist_for_each_safe(entry, tmp, &filename_list) { | |
1483 | fn = flist_entry(entry, struct file_name, list); | |
1484 | free(fn->filename); | |
1485 | flist_del(&fn->list); | |
1486 | free(fn); | |
bcbfeefa | 1487 | } |
90426237 JA |
1488 | |
1489 | fio_file_hash_unlock(); | |
bcbfeefa CE |
1490 | } |
1491 | ||
7b5cb700 JA |
1492 | static struct fio_file *alloc_new_file(struct thread_data *td) |
1493 | { | |
1494 | struct fio_file *f; | |
1495 | ||
1496 | f = smalloc(sizeof(*f)); | |
1497 | if (!f) { | |
7b5cb700 JA |
1498 | assert(0); |
1499 | return NULL; | |
1500 | } | |
1501 | ||
1502 | f->fd = -1; | |
1503 | f->shadow_fd = -1; | |
1504 | fio_file_reset(td, f); | |
1505 | return f; | |
1506 | } | |
1507 | ||
04d6530f JA |
1508 | bool exists_and_not_regfile(const char *filename) |
1509 | { | |
1510 | struct stat sb; | |
1511 | ||
1512 | if (lstat(filename, &sb) == -1) | |
1513 | return false; | |
1514 | ||
1515 | #ifndef WIN32 /* NOT Windows */ | |
1516 | if (S_ISREG(sb.st_mode)) | |
1517 | return false; | |
1518 | #else | |
1519 | /* \\.\ is the device namespace in Windows, where every file | |
1520 | * is a device node */ | |
1521 | if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0) | |
1522 | return false; | |
1523 | #endif | |
1524 | ||
1525 | return true; | |
1526 | } | |
1527 | ||
5903e7b7 | 1528 | int add_file(struct thread_data *td, const char *fname, int numjob, int inc) |
af52b345 | 1529 | { |
7b4e4fe5 | 1530 | int cur_files = td->files_index; |
bd0ee748 | 1531 | char file_name[PATH_MAX]; |
af52b345 | 1532 | struct fio_file *f; |
bd0ee748 | 1533 | int len = 0; |
af52b345 | 1534 | |
ee56ad50 JA |
1535 | dprint(FD_FILE, "add file %s\n", fname); |
1536 | ||
bcbfeefa | 1537 | if (td->o.directory) |
922a5be8 JA |
1538 | len = set_name_idx(file_name, PATH_MAX, td->o.directory, numjob, |
1539 | td->o.unique_filename); | |
bcbfeefa CE |
1540 | |
1541 | sprintf(file_name + len, "%s", fname); | |
1542 | ||
1543 | /* clean cloned siblings using existing files */ | |
04d6530f JA |
1544 | if (numjob && is_already_allocated(file_name) && |
1545 | !exists_and_not_regfile(fname)) | |
bcbfeefa CE |
1546 | return 0; |
1547 | ||
7b5cb700 | 1548 | f = alloc_new_file(td); |
bd0ee748 | 1549 | |
fc99bc0d | 1550 | if (td->files_size <= td->files_index) { |
1983e327 | 1551 | unsigned int new_size = td->o.nr_files + 1; |
126d65c6 | 1552 | |
fc99bc0d JA |
1553 | dprint(FD_FILE, "resize file array to %d files\n", new_size); |
1554 | ||
1555 | td->files = realloc(td->files, new_size * sizeof(f)); | |
d537c08b JM |
1556 | if (td->files == NULL) { |
1557 | log_err("fio: realloc OOM\n"); | |
1558 | assert(0); | |
1559 | } | |
d7df1d13 JA |
1560 | if (td->o.file_lock_mode != FILE_LOCK_NONE) { |
1561 | td->file_locks = realloc(td->file_locks, new_size); | |
1562 | if (!td->file_locks) { | |
1563 | log_err("fio: realloc OOM\n"); | |
1564 | assert(0); | |
1565 | } | |
1566 | td->file_locks[cur_files] = FILE_LOCK_NONE; | |
1567 | } | |
fc99bc0d JA |
1568 | td->files_size = new_size; |
1569 | } | |
8bb7679e | 1570 | td->files[cur_files] = f; |
89ac1d48 | 1571 | f->fileno = cur_files; |
126d65c6 | 1572 | |
07eb79df JA |
1573 | /* |
1574 | * init function, io engine may not be loaded yet | |
1575 | */ | |
9b87f09b | 1576 | if (td->io_ops && td_ioengine_flagged(td, FIO_DISKLESSIO)) |
07eb79df JA |
1577 | f->real_file_size = -1ULL; |
1578 | ||
f17c4392 | 1579 | f->file_name = smalloc_strdup(file_name); |
81b3c86f | 1580 | if (!f->file_name) |
c48c0be7 | 1581 | assert(0); |
0b9d69ec | 1582 | |
e3bab463 | 1583 | get_file_type(f); |
af52b345 | 1584 | |
4d4e80f2 JA |
1585 | switch (td->o.file_lock_mode) { |
1586 | case FILE_LOCK_NONE: | |
1587 | break; | |
1588 | case FILE_LOCK_READWRITE: | |
d7df1d13 | 1589 | f->rwlock = fio_rwlock_init(); |
4d4e80f2 JA |
1590 | break; |
1591 | case FILE_LOCK_EXCLUSIVE: | |
521da527 | 1592 | f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED); |
4d4e80f2 JA |
1593 | break; |
1594 | default: | |
1595 | log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode); | |
1596 | assert(0); | |
1597 | } | |
29c1349f | 1598 | |
7b4e4fe5 | 1599 | td->files_index++; |
1549441c JA |
1600 | if (f->filetype == FIO_TYPE_FILE) |
1601 | td->nr_normal_files++; | |
f29b25a3 | 1602 | |
bcbfeefa CE |
1603 | set_already_allocated(file_name); |
1604 | ||
5903e7b7 JA |
1605 | if (inc) |
1606 | td->o.nr_files++; | |
1607 | ||
5ec10eaa JA |
1608 | dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name, |
1609 | cur_files); | |
9efef3c4 | 1610 | |
f29b25a3 | 1611 | return cur_files; |
af52b345 | 1612 | } |
0ad920e7 | 1613 | |
49ffb4a2 JA |
1614 | int add_file_exclusive(struct thread_data *td, const char *fname) |
1615 | { | |
1616 | struct fio_file *f; | |
1617 | unsigned int i; | |
1618 | ||
1619 | for_each_file(td, f, i) { | |
1620 | if (!strcmp(f->file_name, fname)) | |
1621 | return i; | |
1622 | } | |
1623 | ||
5903e7b7 | 1624 | return add_file(td, fname, 0, 1); |
49ffb4a2 JA |
1625 | } |
1626 | ||
0ad920e7 JA |
1627 | void get_file(struct fio_file *f) |
1628 | { | |
8172fe97 | 1629 | dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references); |
d6aed795 | 1630 | assert(fio_file_open(f)); |
0ad920e7 JA |
1631 | f->references++; |
1632 | } | |
1633 | ||
6977bcd0 | 1634 | int put_file(struct thread_data *td, struct fio_file *f) |
0ad920e7 | 1635 | { |
98e1ac4e | 1636 | int f_ret = 0, ret = 0; |
6977bcd0 | 1637 | |
8172fe97 | 1638 | dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references); |
ee56ad50 | 1639 | |
22a57ba8 JA |
1640 | if (!fio_file_open(f)) { |
1641 | assert(f->fd == -1); | |
6977bcd0 | 1642 | return 0; |
22a57ba8 | 1643 | } |
0ad920e7 JA |
1644 | |
1645 | assert(f->references); | |
1646 | if (--f->references) | |
6977bcd0 | 1647 | return 0; |
0ad920e7 | 1648 | |
71b84caa | 1649 | if (should_fsync(td) && td->o.fsync_on_close) { |
98e1ac4e | 1650 | f_ret = fsync(f->fd); |
71b84caa JA |
1651 | if (f_ret < 0) |
1652 | f_ret = errno; | |
1653 | } | |
ebb1415f | 1654 | |
0ad920e7 | 1655 | if (td->io_ops->close_file) |
6977bcd0 | 1656 | ret = td->io_ops->close_file(td, f); |
1020a139 | 1657 | |
98e1ac4e | 1658 | if (!ret) |
a5fb461f | 1659 | ret = f_ret; |
98e1ac4e | 1660 | |
0ad920e7 | 1661 | td->nr_open_files--; |
d6aed795 | 1662 | fio_file_clear_open(f); |
22a57ba8 | 1663 | assert(f->fd == -1); |
6977bcd0 | 1664 | return ret; |
0ad920e7 | 1665 | } |
bbf6b540 | 1666 | |
4d4e80f2 | 1667 | void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir) |
b2bd2bd9 | 1668 | { |
4d4e80f2 JA |
1669 | if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE) |
1670 | return; | |
29c1349f | 1671 | |
4d4e80f2 JA |
1672 | if (td->o.file_lock_mode == FILE_LOCK_READWRITE) { |
1673 | if (ddir == DDIR_READ) | |
d7df1d13 | 1674 | fio_rwlock_read(f->rwlock); |
4d4e80f2 | 1675 | else |
d7df1d13 | 1676 | fio_rwlock_write(f->rwlock); |
4d4e80f2 JA |
1677 | } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) |
1678 | fio_mutex_down(f->lock); | |
1679 | ||
d7df1d13 | 1680 | td->file_locks[f->fileno] = td->o.file_lock_mode; |
b2bd2bd9 JA |
1681 | } |
1682 | ||
4d4e80f2 | 1683 | void unlock_file(struct thread_data *td, struct fio_file *f) |
b2bd2bd9 | 1684 | { |
4d4e80f2 JA |
1685 | if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE) |
1686 | return; | |
4d4e80f2 | 1687 | |
d7df1d13 JA |
1688 | if (td->o.file_lock_mode == FILE_LOCK_READWRITE) |
1689 | fio_rwlock_unlock(f->rwlock); | |
1690 | else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) | |
4d4e80f2 | 1691 | fio_mutex_up(f->lock); |
d7df1d13 JA |
1692 | |
1693 | td->file_locks[f->fileno] = FILE_LOCK_NONE; | |
b2bd2bd9 JA |
1694 | } |
1695 | ||
4d4e80f2 JA |
1696 | void unlock_file_all(struct thread_data *td, struct fio_file *f) |
1697 | { | |
f2a2803a | 1698 | if (td->o.file_lock_mode == FILE_LOCK_NONE || !td->file_locks) |
27ddbfa0 | 1699 | return; |
d7df1d13 JA |
1700 | if (td->file_locks[f->fileno] != FILE_LOCK_NONE) |
1701 | unlock_file(td, f); | |
4d4e80f2 JA |
1702 | } |
1703 | ||
bbf6b540 JA |
1704 | static int recurse_dir(struct thread_data *td, const char *dirname) |
1705 | { | |
1706 | struct dirent *dir; | |
1707 | int ret = 0; | |
1708 | DIR *D; | |
1709 | ||
1710 | D = opendir(dirname); | |
1711 | if (!D) { | |
0ddb270c JA |
1712 | char buf[FIO_VERROR_SIZE]; |
1713 | ||
98ffb8f3 | 1714 | snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname); |
0ddb270c | 1715 | td_verror(td, errno, buf); |
bbf6b540 JA |
1716 | return 1; |
1717 | } | |
1718 | ||
1719 | while ((dir = readdir(D)) != NULL) { | |
1720 | char full_path[PATH_MAX]; | |
1721 | struct stat sb; | |
1722 | ||
e85b2b83 JA |
1723 | if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) |
1724 | continue; | |
96d32d51 | 1725 | |
b9fd788f | 1726 | sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name); |
bbf6b540 JA |
1727 | |
1728 | if (lstat(full_path, &sb) == -1) { | |
1729 | if (errno != ENOENT) { | |
1730 | td_verror(td, errno, "stat"); | |
68ace9e0 JA |
1731 | ret = 1; |
1732 | break; | |
bbf6b540 JA |
1733 | } |
1734 | } | |
1735 | ||
1736 | if (S_ISREG(sb.st_mode)) { | |
5903e7b7 | 1737 | add_file(td, full_path, 0, 1); |
bbf6b540 JA |
1738 | continue; |
1739 | } | |
0ddb270c JA |
1740 | if (!S_ISDIR(sb.st_mode)) |
1741 | continue; | |
bbf6b540 | 1742 | |
5ec10eaa JA |
1743 | ret = recurse_dir(td, full_path); |
1744 | if (ret) | |
bbf6b540 JA |
1745 | break; |
1746 | } | |
1747 | ||
1748 | closedir(D); | |
1749 | return ret; | |
1750 | } | |
1751 | ||
1752 | int add_dir_files(struct thread_data *td, const char *path) | |
1753 | { | |
0ddb270c JA |
1754 | int ret = recurse_dir(td, path); |
1755 | ||
1756 | if (!ret) | |
1757 | log_info("fio: opendir added %d files\n", td->o.nr_files); | |
1758 | ||
1759 | return ret; | |
bbf6b540 | 1760 | } |
cade3ef4 JA |
1761 | |
1762 | void dup_files(struct thread_data *td, struct thread_data *org) | |
1763 | { | |
1764 | struct fio_file *f; | |
1765 | unsigned int i; | |
9efef3c4 JA |
1766 | |
1767 | dprint(FD_FILE, "dup files: %d\n", org->files_index); | |
cade3ef4 JA |
1768 | |
1769 | if (!org->files) | |
1770 | return; | |
1771 | ||
9efef3c4 | 1772 | td->files = malloc(org->files_index * sizeof(f)); |
cade3ef4 | 1773 | |
d7df1d13 JA |
1774 | if (td->o.file_lock_mode != FILE_LOCK_NONE) |
1775 | td->file_locks = malloc(org->files_index); | |
1776 | ||
9efef3c4 | 1777 | for_each_file(org, f, i) { |
b0fe421a JA |
1778 | struct fio_file *__f; |
1779 | ||
7b5cb700 | 1780 | __f = alloc_new_file(td); |
0b9d69ec | 1781 | |
bc3456fa | 1782 | if (f->file_name) { |
f17c4392 | 1783 | __f->file_name = smalloc_strdup(f->file_name); |
81b3c86f | 1784 | if (!__f->file_name) |
c48c0be7 | 1785 | assert(0); |
0b9d69ec | 1786 | |
bc3456fa AC |
1787 | __f->filetype = f->filetype; |
1788 | } | |
b0fe421a | 1789 | |
67ad9242 JA |
1790 | if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) |
1791 | __f->lock = f->lock; | |
1792 | else if (td->o.file_lock_mode == FILE_LOCK_READWRITE) | |
1793 | __f->rwlock = f->rwlock; | |
1794 | ||
b0fe421a | 1795 | td->files[i] = __f; |
cade3ef4 JA |
1796 | } |
1797 | } | |
f29b25a3 JA |
1798 | |
1799 | /* | |
1800 | * Returns the index that matches the filename, or -1 if not there | |
1801 | */ | |
1802 | int get_fileno(struct thread_data *td, const char *fname) | |
1803 | { | |
1804 | struct fio_file *f; | |
1805 | unsigned int i; | |
1806 | ||
1807 | for_each_file(td, f, i) | |
1808 | if (!strcmp(f->file_name, fname)) | |
1809 | return i; | |
1810 | ||
1811 | return -1; | |
1812 | } | |
1813 | ||
1814 | /* | |
1815 | * For log usage, where we add/open/close files automatically | |
1816 | */ | |
1817 | void free_release_files(struct thread_data *td) | |
1818 | { | |
1819 | close_files(td); | |
66ee4002 JA |
1820 | td->o.nr_files = 0; |
1821 | td->o.open_files = 0; | |
f29b25a3 JA |
1822 | td->files_index = 0; |
1823 | td->nr_normal_files = 0; | |
1824 | } | |
33c48814 JA |
1825 | |
1826 | void fio_file_reset(struct thread_data *td, struct fio_file *f) | |
1827 | { | |
f1dfb668 JA |
1828 | int i; |
1829 | ||
1830 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { | |
1831 | f->last_pos[i] = f->file_offset; | |
1832 | f->last_start[i] = -1ULL; | |
1833 | } | |
1834 | ||
967d1b63 | 1835 | if (fio_file_axmap(f)) |
33c48814 | 1836 | axmap_reset(f->io_axmap); |
967d1b63 | 1837 | else if (fio_file_lfsr(f)) |
33c48814 JA |
1838 | lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]); |
1839 | } | |
002fe734 | 1840 | |
747b3105 | 1841 | bool fio_files_done(struct thread_data *td) |
002fe734 JA |
1842 | { |
1843 | struct fio_file *f; | |
1844 | unsigned int i; | |
1845 | ||
1846 | for_each_file(td, f, i) | |
1847 | if (!fio_file_done(f)) | |
747b3105 | 1848 | return false; |
002fe734 | 1849 | |
747b3105 | 1850 | return true; |
002fe734 | 1851 | } |
bcbfeefa CE |
1852 | |
1853 | /* free memory used in initialization phase only */ | |
190b8f0c CF |
1854 | void filesetup_mem_free(void) |
1855 | { | |
bcbfeefa CE |
1856 | free_already_allocated(); |
1857 | } | |
47534cda TK |
1858 | |
1859 | /* | |
1860 | * This function is for platforms which support direct I/O but not O_DIRECT. | |
1861 | */ | |
1862 | int fio_set_directio(struct thread_data *td, struct fio_file *f) | |
1863 | { | |
1864 | #ifdef FIO_OS_DIRECTIO | |
2905de74 | 1865 | int ret = fio_set_odirect(f); |
47534cda TK |
1866 | |
1867 | if (ret) { | |
1868 | td_verror(td, ret, "fio_set_directio"); | |
1869 | #if defined(__sun__) | |
1870 | if (ret == ENOTTY) { /* ENOTTY suggests RAW device or ZFS */ | |
1871 | log_err("fio: doing directIO to RAW devices or ZFS not supported\n"); | |
1872 | } else { | |
1873 | log_err("fio: the file system does not seem to support direct IO\n"); | |
1874 | } | |
1875 | #else | |
1876 | log_err("fio: the file system does not seem to support direct IO\n"); | |
1877 | #endif | |
1878 | return -1; | |
1879 | } | |
1880 | ||
1881 | return 0; | |
1882 | #else | |
1883 | return -1; | |
1884 | #endif | |
1885 | } |