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