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 JA |
871 | /* |
872 | * if blockalign is provided, find the min across read, write, | |
873 | * and trim | |
874 | */ | |
89978a6b BW |
875 | if (fio_option_is_set(o, ba)) { |
876 | align_bs = (unsigned long long) min(o->ba[DDIR_READ], o->ba[DDIR_WRITE]); | |
877 | align_bs = min((unsigned long long) o->ba[DDIR_TRIM], align_bs); | |
872e0415 JA |
878 | } else { |
879 | /* else take the minimum block size */ | |
89978a6b BW |
880 | align_bs = td_min_bs(td); |
881 | } | |
882 | ||
883 | /* calculate the raw offset */ | |
884 | offset = (f->real_file_size * o->start_offset_percent / 100) + | |
885 | (td->subjob_number * o->offset_increment); | |
886 | ||
872e0415 JA |
887 | /* |
888 | * block align the offset at the next available boundary at | |
889 | * ceiling(offset / align_bs) * align_bs | |
890 | */ | |
89978a6b BW |
891 | offset = (offset / align_bs + (offset % align_bs != 0)) * align_bs; |
892 | ||
872e0415 JA |
893 | } else { |
894 | /* start_offset_percent not set */ | |
f0d13935 JA |
895 | offset = o->start_offset + |
896 | td->subjob_number * o->offset_increment; | |
89978a6b | 897 | } |
872e0415 | 898 | |
89978a6b | 899 | return offset; |
ce95d651 DE |
900 | } |
901 | ||
7bb48f84 JA |
902 | /* |
903 | * Open the files and setup files sizes, creating files if necessary. | |
904 | */ | |
53cdc686 JA |
905 | int setup_files(struct thread_data *td) |
906 | { | |
7bb48f84 | 907 | unsigned long long total_size, extend_size; |
de98bd30 | 908 | struct thread_options *o = &td->o; |
53cdc686 | 909 | struct fio_file *f; |
002fe734 | 910 | unsigned int i, nr_fs_extra = 0; |
000b0803 | 911 | int err = 0, need_extend; |
e90391eb | 912 | int old_state; |
002fe734 JA |
913 | const unsigned int bs = td_min_bs(td); |
914 | uint64_t fs = 0; | |
53cdc686 | 915 | |
ee56ad50 JA |
916 | dprint(FD_FILE, "setup files\n"); |
917 | ||
8edd973d | 918 | old_state = td_bump_runstate(td, TD_SETTING_UP); |
e90391eb | 919 | |
de98bd30 | 920 | if (o->read_iolog_file) |
25460cf6 | 921 | goto done; |
691c8fb0 | 922 | |
53cdc686 | 923 | /* |
79591fa9 TK |
924 | * Find out physical size of files or devices for this thread, |
925 | * before we determine I/O size and range of our targets. | |
926 | * If ioengine defines a setup() method, it's responsible for | |
7bb48f84 JA |
927 | * opening the files and setting f->real_file_size to indicate |
928 | * the valid range for that file. | |
53cdc686 JA |
929 | */ |
930 | if (td->io_ops->setup) | |
7bb48f84 JA |
931 | err = td->io_ops->setup(td); |
932 | else | |
bab3fd58 | 933 | err = get_file_sizes(td); |
53cdc686 | 934 | |
f1027063 | 935 | if (err) |
e90391eb | 936 | goto err_out; |
f1027063 | 937 | |
0a7eb121 | 938 | /* |
7bb48f84 JA |
939 | * check sizes. if the files/devices do not exist and the size |
940 | * isn't passed to fio, abort. | |
0a7eb121 | 941 | */ |
7bb48f84 JA |
942 | total_size = 0; |
943 | for_each_file(td, f, i) { | |
49a416bd | 944 | f->fileno = i; |
7bb48f84 JA |
945 | if (f->real_file_size == -1ULL) |
946 | total_size = -1ULL; | |
947 | else | |
948 | total_size += f->real_file_size; | |
949 | } | |
0a7eb121 | 950 | |
de98bd30 | 951 | if (o->fill_device) |
2e3bd4c2 JA |
952 | td->fill_device_size = get_fs_free_counts(td); |
953 | ||
7bb48f84 JA |
954 | /* |
955 | * device/file sizes are zero and no size given, punt | |
956 | */ | |
de98bd30 | 957 | if ((!total_size || total_size == -1ULL) && !o->size && |
9b87f09b | 958 | !td_ioengine_flagged(td, FIO_NOIO) && !o->fill_device && |
de98bd30 JA |
959 | !(o->nr_files && (o->file_size_low || o->file_size_high))) { |
960 | log_err("%s: you need to specify size=\n", o->name); | |
e1161c32 | 961 | td_verror(td, EINVAL, "total_file_size"); |
e90391eb | 962 | goto err_out; |
53cdc686 JA |
963 | } |
964 | ||
002fe734 JA |
965 | /* |
966 | * Calculate per-file size and potential extra size for the | |
79591fa9 | 967 | * first files, if needed (i.e. if we don't have a fixed size). |
002fe734 | 968 | */ |
d1e78e6b | 969 | if (!o->file_size_low && o->nr_files) { |
002fe734 JA |
970 | uint64_t all_fs; |
971 | ||
972 | fs = o->size / o->nr_files; | |
973 | all_fs = fs * o->nr_files; | |
974 | ||
975 | if (all_fs < o->size) | |
976 | nr_fs_extra = (o->size - all_fs) / bs; | |
977 | } | |
978 | ||
7bb48f84 JA |
979 | /* |
980 | * now file sizes are known, so we can set ->io_size. if size= is | |
981 | * not given, ->io_size is just equal to ->real_file_size. if size | |
982 | * is given, ->io_size is size / nr_files. | |
983 | */ | |
984 | extend_size = total_size = 0; | |
985 | need_extend = 0; | |
986 | for_each_file(td, f, i) { | |
bedc9dc2 | 987 | f->file_offset = get_start_offset(td, f); |
bcdedd0a | 988 | |
79591fa9 TK |
989 | /* |
990 | * Update ->io_size depending on options specified. | |
991 | * ->file_size_low being 0 means filesize option isn't set. | |
992 | * Non zero ->file_size_low equals ->file_size_high means | |
993 | * filesize option is set in a fixed size format. | |
994 | * Non zero ->file_size_low not equals ->file_size_high means | |
995 | * filesize option is set in a range format. | |
996 | */ | |
de98bd30 | 997 | if (!o->file_size_low) { |
7bb48f84 | 998 | /* |
79591fa9 | 999 | * no file size or range given, file size is equal to |
c4aa2d08 | 1000 | * total size divided by number of files. If the size |
002fe734 JA |
1001 | * doesn't divide nicely with the min blocksize, |
1002 | * make the first files bigger. | |
7bb48f84 | 1003 | */ |
002fe734 JA |
1004 | f->io_size = fs; |
1005 | if (nr_fs_extra) { | |
1006 | nr_fs_extra--; | |
1007 | f->io_size += bs; | |
1008 | } | |
1009 | ||
c4aa2d08 | 1010 | /* |
c6dfd837 TK |
1011 | * We normally don't come here for regular files, but |
1012 | * if the result is 0 for a regular file, set it to the | |
1013 | * real file size. This could be size of the existing | |
1014 | * one if it already exists, but otherwise will be set | |
1015 | * to 0. A new file won't be created because | |
c4aa2d08 TK |
1016 | * ->io_size + ->file_offset equals ->real_file_size. |
1017 | */ | |
1018 | if (!f->io_size) { | |
1019 | if (f->file_offset > f->real_file_size) | |
1020 | goto err_offset; | |
273f8c91 | 1021 | f->io_size = f->real_file_size - f->file_offset; |
c4aa2d08 TK |
1022 | if (!f->io_size) |
1023 | log_info("fio: file %s may be ignored\n", | |
1024 | f->file_name); | |
1025 | } | |
de98bd30 JA |
1026 | } else if (f->real_file_size < o->file_size_low || |
1027 | f->real_file_size > o->file_size_high) { | |
1028 | if (f->file_offset > o->file_size_low) | |
bcdedd0a | 1029 | goto err_offset; |
7bb48f84 JA |
1030 | /* |
1031 | * file size given. if it's fixed, use that. if it's a | |
1032 | * range, generate a random size in-between. | |
1033 | */ | |
de98bd30 JA |
1034 | if (o->file_size_low == o->file_size_high) |
1035 | f->io_size = o->file_size_low - f->file_offset; | |
1036 | else { | |
5ec10eaa JA |
1037 | f->io_size = get_rand_file_size(td) |
1038 | - f->file_offset; | |
1039 | } | |
65bdb10a | 1040 | } else |
bcdedd0a | 1041 | f->io_size = f->real_file_size - f->file_offset; |
53cdc686 | 1042 | |
7bb48f84 JA |
1043 | if (f->io_size == -1ULL) |
1044 | total_size = -1ULL; | |
4d002569 | 1045 | else { |
e391c704 | 1046 | if (o->size_percent) { |
76e4bb4f SW |
1047 | uint64_t file_size; |
1048 | ||
1049 | file_size = f->io_size + f->file_offset; | |
1050 | f->io_size = (file_size * | |
1051 | o->size_percent) / 100; | |
1052 | if (f->io_size > (file_size - f->file_offset)) | |
1053 | f->io_size = file_size - f->file_offset; | |
1054 | ||
e391c704 JA |
1055 | f->io_size -= (f->io_size % td_min_bs(td)); |
1056 | } | |
7bb48f84 | 1057 | total_size += f->io_size; |
4d002569 | 1058 | } |
7bb48f84 JA |
1059 | |
1060 | if (f->filetype == FIO_TYPE_FILE && | |
bcdedd0a | 1061 | (f->io_size + f->file_offset) > f->real_file_size && |
9b87f09b | 1062 | !td_ioengine_flagged(td, FIO_DISKLESSIO)) { |
5fd31680 JA |
1063 | if (!o->create_on_open) { |
1064 | need_extend++; | |
814452bd | 1065 | extend_size += (f->io_size + f->file_offset); |
43558406 | 1066 | fio_file_set_extend(f); |
5fd31680 | 1067 | } else |
814452bd | 1068 | f->real_file_size = f->io_size + f->file_offset; |
5ec10eaa | 1069 | } |
7bb48f84 | 1070 | } |
53cdc686 | 1071 | |
66347cfa DE |
1072 | if (td->o.block_error_hist) { |
1073 | int len; | |
1074 | ||
1075 | assert(td->o.nr_files == 1); /* checked in fixup_options */ | |
1076 | f = td->files[0]; | |
1077 | len = f->io_size / td->o.bs[DDIR_TRIM]; | |
1078 | if (len > MAX_NR_BLOCK_INFOS || len <= 0) { | |
1079 | log_err("fio: cannot calculate block histogram with " | |
1080 | "%d trim blocks, maximum %d\n", | |
1081 | len, MAX_NR_BLOCK_INFOS); | |
1082 | td_verror(td, EINVAL, "block_error_hist"); | |
1083 | goto err_out; | |
1084 | } | |
1085 | ||
1086 | td->ts.nr_block_infos = len; | |
8a68c41c | 1087 | for (i = 0; i < len; i++) |
66347cfa DE |
1088 | td->ts.block_infos[i] = |
1089 | BLOCK_INFO(0, BLOCK_STATE_UNINIT); | |
1090 | } else | |
1091 | td->ts.nr_block_infos = 0; | |
1092 | ||
0bc27b0b | 1093 | if (!o->size || (total_size && o->size > total_size)) |
de98bd30 | 1094 | o->size = total_size; |
21972cde | 1095 | |
d1faa06d JA |
1096 | if (o->size < td_min_bs(td)) { |
1097 | log_err("fio: blocksize too large for data set\n"); | |
1098 | goto err_out; | |
1099 | } | |
1100 | ||
7bb48f84 | 1101 | /* |
79591fa9 TK |
1102 | * See if we need to extend some files, typically needed when our |
1103 | * target regular files don't exist yet, but our jobs require them | |
1104 | * initially due to read I/Os. | |
7bb48f84 JA |
1105 | */ |
1106 | if (need_extend) { | |
1107 | temp_stall_ts = 1; | |
7c5f4cdb TK |
1108 | if (output_format & FIO_OUTPUT_NORMAL) { |
1109 | log_info("%s: Laying out IO file%s (%u file%s / %s%lluMiB)\n", | |
1110 | o->name, | |
1111 | need_extend > 1 ? "s" : "", | |
1112 | need_extend, | |
1113 | need_extend > 1 ? "s" : "", | |
1114 | need_extend > 1 ? "total " : "", | |
1115 | extend_size >> 20); | |
1116 | } | |
7bb48f84 JA |
1117 | |
1118 | for_each_file(td, f, i) { | |
5e0074c2 | 1119 | unsigned long long old_len = -1ULL, extend_len = -1ULL; |
3baddf24 | 1120 | |
d6aed795 | 1121 | if (!fio_file_extend(f)) |
7bb48f84 JA |
1122 | continue; |
1123 | ||
409b3417 | 1124 | assert(f->filetype == FIO_TYPE_FILE); |
d6aed795 | 1125 | fio_file_clear_extend(f); |
de98bd30 | 1126 | if (!o->fill_device) { |
5e0074c2 | 1127 | old_len = f->real_file_size; |
0b9d69ec JA |
1128 | extend_len = f->io_size + f->file_offset - |
1129 | old_len; | |
5e0074c2 | 1130 | } |
bcdedd0a | 1131 | f->real_file_size = (f->io_size + f->file_offset); |
7bb48f84 JA |
1132 | err = extend_file(td, f); |
1133 | if (err) | |
3baddf24 | 1134 | break; |
5e0074c2 | 1135 | |
3baddf24 JA |
1136 | err = __file_invalidate_cache(td, f, old_len, |
1137 | extend_len); | |
9824f73b JA |
1138 | |
1139 | /* | |
1140 | * Shut up static checker | |
1141 | */ | |
1142 | if (f->fd != -1) | |
1143 | close(f->fd); | |
1144 | ||
3baddf24 JA |
1145 | f->fd = -1; |
1146 | if (err) | |
7bb48f84 JA |
1147 | break; |
1148 | } | |
1149 | temp_stall_ts = 0; | |
1150 | } | |
1151 | ||
1152 | if (err) | |
e90391eb | 1153 | goto err_out; |
7bb48f84 | 1154 | |
de98bd30 JA |
1155 | if (!o->zone_size) |
1156 | o->zone_size = o->size; | |
7bb48f84 | 1157 | |
ea966f81 JA |
1158 | /* |
1159 | * iolog already set the total io size, if we read back | |
1160 | * stored entries. | |
1161 | */ | |
77731b29 | 1162 | if (!o->read_iolog_file) { |
5be9bf09 TK |
1163 | if (o->io_size) |
1164 | td->total_io_size = o->io_size * o->loops; | |
77731b29 JA |
1165 | else |
1166 | td->total_io_size = o->size * o->loops; | |
1167 | } | |
25460cf6 JA |
1168 | |
1169 | done: | |
de98bd30 | 1170 | if (o->create_only) |
25460cf6 JA |
1171 | td->done = 1; |
1172 | ||
8edd973d | 1173 | td_restore_runstate(td, old_state); |
7bb48f84 | 1174 | return 0; |
bcdedd0a | 1175 | err_offset: |
de98bd30 | 1176 | log_err("%s: you need to specify valid offset=\n", o->name); |
e90391eb | 1177 | err_out: |
8edd973d | 1178 | td_restore_runstate(td, old_state); |
bcdedd0a | 1179 | return 1; |
53cdc686 JA |
1180 | } |
1181 | ||
afad68f7 ZY |
1182 | int pre_read_files(struct thread_data *td) |
1183 | { | |
1184 | struct fio_file *f; | |
1185 | unsigned int i; | |
1186 | ||
1187 | dprint(FD_FILE, "pre_read files\n"); | |
1188 | ||
1189 | for_each_file(td, f, i) { | |
afe0d59a TK |
1190 | if (pre_read_file(td, f)) |
1191 | return -1; | |
afad68f7 ZY |
1192 | } |
1193 | ||
afe0d59a | 1194 | return 0; |
afad68f7 ZY |
1195 | } |
1196 | ||
9c6f6316 JA |
1197 | static int __init_rand_distribution(struct thread_data *td, struct fio_file *f) |
1198 | { | |
2316296a | 1199 | unsigned int range_size, seed; |
9c6f6316 | 1200 | unsigned long nranges; |
42da5c8b | 1201 | uint64_t fsize; |
9c6f6316 JA |
1202 | |
1203 | range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]); | |
42da5c8b | 1204 | fsize = min(f->real_file_size, f->io_size); |
9c6f6316 | 1205 | |
42da5c8b | 1206 | nranges = (fsize + range_size - 1) / range_size; |
9c6f6316 | 1207 | |
2316296a | 1208 | seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number; |
8425687e JA |
1209 | if (!td->o.rand_repeatable) |
1210 | seed = td->rand_seeds[4]; | |
1211 | ||
9c6f6316 | 1212 | if (td->o.random_distribution == FIO_RAND_DIST_ZIPF) |
888677a4 | 1213 | zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed); |
56d9fa4b | 1214 | else if (td->o.random_distribution == FIO_RAND_DIST_PARETO) |
888677a4 | 1215 | pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed); |
56d9fa4b | 1216 | else if (td->o.random_distribution == FIO_RAND_DIST_GAUSS) |
f88cd222 | 1217 | gauss_init(&f->gauss, nranges, td->o.gauss_dev.u.f, seed); |
9c6f6316 JA |
1218 | |
1219 | return 1; | |
1220 | } | |
1221 | ||
1222 | static int init_rand_distribution(struct thread_data *td) | |
1223 | { | |
1224 | struct fio_file *f; | |
1225 | unsigned int i; | |
1226 | int state; | |
1227 | ||
1228 | if (td->o.random_distribution == FIO_RAND_DIST_RANDOM) | |
1229 | return 0; | |
1230 | ||
8edd973d JA |
1231 | state = td_bump_runstate(td, TD_SETTING_UP); |
1232 | ||
9c6f6316 JA |
1233 | for_each_file(td, f, i) |
1234 | __init_rand_distribution(td, f); | |
8edd973d JA |
1235 | |
1236 | td_restore_runstate(td, state); | |
9c6f6316 JA |
1237 | |
1238 | return 1; | |
1239 | } | |
1240 | ||
75f90d5f JA |
1241 | /* |
1242 | * Check if the number of blocks exceeds the randomness capability of | |
1243 | * the selected generator. Tausworthe is 32-bit, the others are fullly | |
1244 | * 64-bit capable. | |
1245 | */ | |
1246 | static int check_rand_gen_limits(struct thread_data *td, struct fio_file *f, | |
1247 | uint64_t blocks) | |
1248 | { | |
1249 | if (blocks <= FRAND32_MAX) | |
1250 | return 0; | |
1251 | if (td->o.random_generator != FIO_RAND_GEN_TAUSWORTHE) | |
1252 | return 0; | |
1253 | ||
1254 | /* | |
1255 | * If the user hasn't specified a random generator, switch | |
1256 | * to tausworthe64 with informational warning. If the user did | |
1257 | * specify one, just warn. | |
1258 | */ | |
1259 | log_info("fio: file %s exceeds 32-bit tausworthe random generator.\n", | |
1260 | f->file_name); | |
1261 | ||
1262 | if (!fio_option_is_set(&td->o, random_generator)) { | |
1263 | log_info("fio: Switching to tausworthe64. Use the " | |
1264 | "random_generator= option to get rid of this " | |
4e795a3e | 1265 | "warning.\n"); |
75f90d5f JA |
1266 | td->o.random_generator = FIO_RAND_GEN_TAUSWORTHE64; |
1267 | return 0; | |
1268 | } | |
1269 | ||
1270 | /* | |
1271 | * Just make this information to avoid breaking scripts. | |
1272 | */ | |
1273 | log_info("fio: Use the random_generator= option to switch to lfsr or " | |
1274 | "tausworthe64.\n"); | |
1275 | return 0; | |
1276 | } | |
1277 | ||
68727076 JA |
1278 | int init_random_map(struct thread_data *td) |
1279 | { | |
51ede0b1 | 1280 | unsigned long long blocks; |
68727076 JA |
1281 | struct fio_file *f; |
1282 | unsigned int i; | |
1283 | ||
9c6f6316 JA |
1284 | if (init_rand_distribution(td)) |
1285 | return 0; | |
3831a843 | 1286 | if (!td_random(td)) |
68727076 JA |
1287 | return 0; |
1288 | ||
1289 | for_each_file(td, f, i) { | |
42da5c8b | 1290 | uint64_t fsize = min(f->real_file_size, f->io_size); |
38f30c81 | 1291 | |
42da5c8b | 1292 | blocks = fsize / (unsigned long long) td->o.rw_min_bs; |
53737ae0 | 1293 | |
75f90d5f | 1294 | if (check_rand_gen_limits(td, f, blocks)) |
37fbd7e9 | 1295 | return 1; |
37fbd7e9 | 1296 | |
8055e41d | 1297 | if (td->o.random_generator == FIO_RAND_GEN_LFSR) { |
82af46be JA |
1298 | unsigned long seed; |
1299 | ||
1300 | seed = td->rand_seeds[FIO_RAND_BLOCK_OFF]; | |
9c0f3f32 | 1301 | |
967d1b63 JA |
1302 | if (!lfsr_init(&f->lfsr, blocks, seed, 0)) { |
1303 | fio_file_set_lfsr(f); | |
8055e41d | 1304 | continue; |
967d1b63 | 1305 | } |
3831a843 | 1306 | } else if (!td->o.norandommap) { |
7ebd796f | 1307 | f->io_axmap = axmap_new(blocks); |
967d1b63 JA |
1308 | if (f->io_axmap) { |
1309 | fio_file_set_axmap(f); | |
8055e41d | 1310 | continue; |
967d1b63 | 1311 | } |
1cad7121 JA |
1312 | } else if (td->o.norandommap) |
1313 | continue; | |
ceadd59e | 1314 | |
2b386d25 | 1315 | if (!td->o.softrandommap) { |
5ec10eaa JA |
1316 | log_err("fio: failed allocating random map. If running" |
1317 | " a large number of jobs, try the 'norandommap'" | |
2b386d25 JA |
1318 | " option or set 'softrandommap'. Or give" |
1319 | " a larger --alloc-size to fio.\n"); | |
68727076 JA |
1320 | return 1; |
1321 | } | |
303032ae JA |
1322 | |
1323 | log_info("fio: file %s failed allocating random map. Running " | |
1324 | "job without.\n", f->file_name); | |
68727076 JA |
1325 | } |
1326 | ||
1327 | return 0; | |
1328 | } | |
1329 | ||
53cdc686 JA |
1330 | void close_files(struct thread_data *td) |
1331 | { | |
0ab8db89 | 1332 | struct fio_file *f; |
af52b345 | 1333 | unsigned int i; |
53cdc686 | 1334 | |
2be3eec3 JA |
1335 | for_each_file(td, f, i) { |
1336 | if (fio_file_open(f)) | |
1337 | td_io_close_file(td, f); | |
1338 | } | |
24ffd2c2 JA |
1339 | } |
1340 | ||
1341 | void close_and_free_files(struct thread_data *td) | |
1342 | { | |
1343 | struct fio_file *f; | |
1344 | unsigned int i; | |
1345 | ||
ee56ad50 JA |
1346 | dprint(FD_FILE, "close files\n"); |
1347 | ||
0ab8db89 | 1348 | for_each_file(td, f, i) { |
38ef9c90 CF |
1349 | if (td->o.unlink && f->filetype == FIO_TYPE_FILE) { |
1350 | dprint(FD_FILE, "free unlink %s\n", f->file_name); | |
1351 | td_io_unlink_file(td, f); | |
1352 | } | |
1353 | ||
22a57ba8 JA |
1354 | if (fio_file_open(f)) |
1355 | td_io_close_file(td, f); | |
1356 | ||
b9fbcf21 | 1357 | remove_file_hash(f); |
b3dc7f07 | 1358 | |
b5f4d8ba JA |
1359 | if (td->o.unlink && f->filetype == FIO_TYPE_FILE) { |
1360 | dprint(FD_FILE, "free unlink %s\n", f->file_name); | |
38ef9c90 | 1361 | td_io_unlink_file(td, f); |
b5f4d8ba JA |
1362 | } |
1363 | ||
f17c4392 | 1364 | sfree(f->file_name); |
fa1da865 | 1365 | f->file_name = NULL; |
967d1b63 JA |
1366 | if (fio_file_axmap(f)) { |
1367 | axmap_free(f->io_axmap); | |
1368 | f->io_axmap = NULL; | |
1369 | } | |
78d99e6a | 1370 | sfree(f); |
53cdc686 | 1371 | } |
b4a6a59a | 1372 | |
2dc1bbeb | 1373 | td->o.filename = NULL; |
cade3ef4 | 1374 | free(td->files); |
d7df1d13 | 1375 | free(td->file_locks); |
9efef3c4 | 1376 | td->files_index = 0; |
b4a6a59a | 1377 | td->files = NULL; |
d7df1d13 | 1378 | td->file_locks = NULL; |
27ddbfa0 | 1379 | td->o.file_lock_mode = FILE_LOCK_NONE; |
2dc1bbeb | 1380 | td->o.nr_files = 0; |
53cdc686 | 1381 | } |
af52b345 | 1382 | |
e3bab463 | 1383 | static void get_file_type(struct fio_file *f) |
af52b345 JA |
1384 | { |
1385 | struct stat sb; | |
1386 | ||
66159828 JA |
1387 | if (!strcmp(f->file_name, "-")) |
1388 | f->filetype = FIO_TYPE_PIPE; | |
1389 | else | |
1390 | f->filetype = FIO_TYPE_FILE; | |
af52b345 | 1391 | |
d5b78f5a | 1392 | #ifdef WIN32 |
3892182a BC |
1393 | /* \\.\ is the device namespace in Windows, where every file is |
1394 | * a block device */ | |
1395 | if (strncmp(f->file_name, "\\\\.\\", 4) == 0) | |
686fbd31 | 1396 | f->filetype = FIO_TYPE_BLOCK; |
d5b78f5a | 1397 | #endif |
3892182a | 1398 | |
b30d395e | 1399 | if (!stat(f->file_name, &sb)) { |
3892182a | 1400 | if (S_ISBLK(sb.st_mode)) |
686fbd31 | 1401 | f->filetype = FIO_TYPE_BLOCK; |
af52b345 JA |
1402 | else if (S_ISCHR(sb.st_mode)) |
1403 | f->filetype = FIO_TYPE_CHAR; | |
b5605e9d JA |
1404 | else if (S_ISFIFO(sb.st_mode)) |
1405 | f->filetype = FIO_TYPE_PIPE; | |
af52b345 JA |
1406 | } |
1407 | } | |
1408 | ||
1b2a83dc | 1409 | static bool __is_already_allocated(const char *fname, bool set) |
190b8f0c | 1410 | { |
90426237 | 1411 | struct flist_head *entry; |
1b2a83dc | 1412 | bool ret; |
bcbfeefa | 1413 | |
1b2a83dc JA |
1414 | ret = file_bloom_exists(fname, set); |
1415 | if (!ret) | |
1416 | return ret; | |
90426237 JA |
1417 | |
1418 | flist_for_each(entry, &filename_list) { | |
04d6530f | 1419 | struct file_name *fn; |
90426237 | 1420 | |
04d6530f JA |
1421 | fn = flist_entry(entry, struct file_name, list); |
1422 | ||
1423 | if (!strcmp(fn->filename, fname)) | |
1424 | return true; | |
90426237 JA |
1425 | } |
1426 | ||
04d6530f | 1427 | return false; |
bcbfeefa CE |
1428 | } |
1429 | ||
04d6530f | 1430 | static bool is_already_allocated(const char *fname) |
bcbfeefa | 1431 | { |
04d6530f | 1432 | bool ret; |
bcbfeefa | 1433 | |
90426237 | 1434 | fio_file_hash_lock(); |
1b2a83dc | 1435 | ret = __is_already_allocated(fname, false); |
90426237 | 1436 | fio_file_hash_unlock(); |
04d6530f | 1437 | |
90426237 JA |
1438 | return ret; |
1439 | } | |
bcbfeefa | 1440 | |
90426237 JA |
1441 | static void set_already_allocated(const char *fname) |
1442 | { | |
1443 | struct file_name *fn; | |
1444 | ||
1445 | fn = malloc(sizeof(struct file_name)); | |
1446 | fn->filename = strdup(fname); | |
1447 | ||
1448 | fio_file_hash_lock(); | |
1b2a83dc | 1449 | if (!__is_already_allocated(fname, true)) { |
90426237 JA |
1450 | flist_add_tail(&fn->list, &filename_list); |
1451 | fn = NULL; | |
bcbfeefa | 1452 | } |
90426237 | 1453 | fio_file_hash_unlock(); |
bcbfeefa | 1454 | |
90426237 JA |
1455 | if (fn) { |
1456 | free(fn->filename); | |
1457 | free(fn); | |
1458 | } | |
bcbfeefa CE |
1459 | } |
1460 | ||
190b8f0c CF |
1461 | static void free_already_allocated(void) |
1462 | { | |
bcbfeefa CE |
1463 | struct flist_head *entry, *tmp; |
1464 | struct file_name *fn; | |
1465 | ||
90426237 JA |
1466 | if (flist_empty(&filename_list)) |
1467 | return; | |
1468 | ||
1469 | fio_file_hash_lock(); | |
1470 | flist_for_each_safe(entry, tmp, &filename_list) { | |
1471 | fn = flist_entry(entry, struct file_name, list); | |
1472 | free(fn->filename); | |
1473 | flist_del(&fn->list); | |
1474 | free(fn); | |
bcbfeefa | 1475 | } |
90426237 JA |
1476 | |
1477 | fio_file_hash_unlock(); | |
bcbfeefa CE |
1478 | } |
1479 | ||
7b5cb700 JA |
1480 | static struct fio_file *alloc_new_file(struct thread_data *td) |
1481 | { | |
1482 | struct fio_file *f; | |
1483 | ||
1484 | f = smalloc(sizeof(*f)); | |
1485 | if (!f) { | |
7b5cb700 JA |
1486 | assert(0); |
1487 | return NULL; | |
1488 | } | |
1489 | ||
1490 | f->fd = -1; | |
1491 | f->shadow_fd = -1; | |
1492 | fio_file_reset(td, f); | |
1493 | return f; | |
1494 | } | |
1495 | ||
04d6530f JA |
1496 | bool exists_and_not_regfile(const char *filename) |
1497 | { | |
1498 | struct stat sb; | |
1499 | ||
1500 | if (lstat(filename, &sb) == -1) | |
1501 | return false; | |
1502 | ||
1503 | #ifndef WIN32 /* NOT Windows */ | |
1504 | if (S_ISREG(sb.st_mode)) | |
1505 | return false; | |
1506 | #else | |
1507 | /* \\.\ is the device namespace in Windows, where every file | |
1508 | * is a device node */ | |
1509 | if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0) | |
1510 | return false; | |
1511 | #endif | |
1512 | ||
1513 | return true; | |
1514 | } | |
1515 | ||
5903e7b7 | 1516 | int add_file(struct thread_data *td, const char *fname, int numjob, int inc) |
af52b345 | 1517 | { |
7b4e4fe5 | 1518 | int cur_files = td->files_index; |
bd0ee748 | 1519 | char file_name[PATH_MAX]; |
af52b345 | 1520 | struct fio_file *f; |
bd0ee748 | 1521 | int len = 0; |
af52b345 | 1522 | |
ee56ad50 JA |
1523 | dprint(FD_FILE, "add file %s\n", fname); |
1524 | ||
bcbfeefa | 1525 | if (td->o.directory) |
922a5be8 JA |
1526 | len = set_name_idx(file_name, PATH_MAX, td->o.directory, numjob, |
1527 | td->o.unique_filename); | |
bcbfeefa CE |
1528 | |
1529 | sprintf(file_name + len, "%s", fname); | |
1530 | ||
1531 | /* clean cloned siblings using existing files */ | |
04d6530f JA |
1532 | if (numjob && is_already_allocated(file_name) && |
1533 | !exists_and_not_regfile(fname)) | |
bcbfeefa CE |
1534 | return 0; |
1535 | ||
7b5cb700 | 1536 | f = alloc_new_file(td); |
bd0ee748 | 1537 | |
fc99bc0d | 1538 | if (td->files_size <= td->files_index) { |
1983e327 | 1539 | unsigned int new_size = td->o.nr_files + 1; |
126d65c6 | 1540 | |
fc99bc0d JA |
1541 | dprint(FD_FILE, "resize file array to %d files\n", new_size); |
1542 | ||
1543 | td->files = realloc(td->files, new_size * sizeof(f)); | |
d537c08b JM |
1544 | if (td->files == NULL) { |
1545 | log_err("fio: realloc OOM\n"); | |
1546 | assert(0); | |
1547 | } | |
d7df1d13 JA |
1548 | if (td->o.file_lock_mode != FILE_LOCK_NONE) { |
1549 | td->file_locks = realloc(td->file_locks, new_size); | |
1550 | if (!td->file_locks) { | |
1551 | log_err("fio: realloc OOM\n"); | |
1552 | assert(0); | |
1553 | } | |
1554 | td->file_locks[cur_files] = FILE_LOCK_NONE; | |
1555 | } | |
fc99bc0d JA |
1556 | td->files_size = new_size; |
1557 | } | |
8bb7679e | 1558 | td->files[cur_files] = f; |
89ac1d48 | 1559 | f->fileno = cur_files; |
126d65c6 | 1560 | |
07eb79df JA |
1561 | /* |
1562 | * init function, io engine may not be loaded yet | |
1563 | */ | |
9b87f09b | 1564 | if (td->io_ops && td_ioengine_flagged(td, FIO_DISKLESSIO)) |
07eb79df JA |
1565 | f->real_file_size = -1ULL; |
1566 | ||
f17c4392 | 1567 | f->file_name = smalloc_strdup(file_name); |
81b3c86f | 1568 | if (!f->file_name) |
c48c0be7 | 1569 | assert(0); |
0b9d69ec | 1570 | |
e3bab463 | 1571 | get_file_type(f); |
af52b345 | 1572 | |
4d4e80f2 JA |
1573 | switch (td->o.file_lock_mode) { |
1574 | case FILE_LOCK_NONE: | |
1575 | break; | |
1576 | case FILE_LOCK_READWRITE: | |
d7df1d13 | 1577 | f->rwlock = fio_rwlock_init(); |
4d4e80f2 JA |
1578 | break; |
1579 | case FILE_LOCK_EXCLUSIVE: | |
521da527 | 1580 | f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED); |
4d4e80f2 JA |
1581 | break; |
1582 | default: | |
1583 | log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode); | |
1584 | assert(0); | |
1585 | } | |
29c1349f | 1586 | |
7b4e4fe5 | 1587 | td->files_index++; |
1549441c JA |
1588 | if (f->filetype == FIO_TYPE_FILE) |
1589 | td->nr_normal_files++; | |
f29b25a3 | 1590 | |
bcbfeefa CE |
1591 | set_already_allocated(file_name); |
1592 | ||
5903e7b7 JA |
1593 | if (inc) |
1594 | td->o.nr_files++; | |
1595 | ||
5ec10eaa JA |
1596 | dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name, |
1597 | cur_files); | |
9efef3c4 | 1598 | |
f29b25a3 | 1599 | return cur_files; |
af52b345 | 1600 | } |
0ad920e7 | 1601 | |
49ffb4a2 JA |
1602 | int add_file_exclusive(struct thread_data *td, const char *fname) |
1603 | { | |
1604 | struct fio_file *f; | |
1605 | unsigned int i; | |
1606 | ||
1607 | for_each_file(td, f, i) { | |
1608 | if (!strcmp(f->file_name, fname)) | |
1609 | return i; | |
1610 | } | |
1611 | ||
5903e7b7 | 1612 | return add_file(td, fname, 0, 1); |
49ffb4a2 JA |
1613 | } |
1614 | ||
0ad920e7 JA |
1615 | void get_file(struct fio_file *f) |
1616 | { | |
8172fe97 | 1617 | dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references); |
d6aed795 | 1618 | assert(fio_file_open(f)); |
0ad920e7 JA |
1619 | f->references++; |
1620 | } | |
1621 | ||
6977bcd0 | 1622 | int put_file(struct thread_data *td, struct fio_file *f) |
0ad920e7 | 1623 | { |
98e1ac4e | 1624 | int f_ret = 0, ret = 0; |
6977bcd0 | 1625 | |
8172fe97 | 1626 | dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references); |
ee56ad50 | 1627 | |
22a57ba8 JA |
1628 | if (!fio_file_open(f)) { |
1629 | assert(f->fd == -1); | |
6977bcd0 | 1630 | return 0; |
22a57ba8 | 1631 | } |
0ad920e7 JA |
1632 | |
1633 | assert(f->references); | |
1634 | if (--f->references) | |
6977bcd0 | 1635 | return 0; |
0ad920e7 | 1636 | |
71b84caa | 1637 | if (should_fsync(td) && td->o.fsync_on_close) { |
98e1ac4e | 1638 | f_ret = fsync(f->fd); |
71b84caa JA |
1639 | if (f_ret < 0) |
1640 | f_ret = errno; | |
1641 | } | |
ebb1415f | 1642 | |
0ad920e7 | 1643 | if (td->io_ops->close_file) |
6977bcd0 | 1644 | ret = td->io_ops->close_file(td, f); |
1020a139 | 1645 | |
98e1ac4e | 1646 | if (!ret) |
a5fb461f | 1647 | ret = f_ret; |
98e1ac4e | 1648 | |
0ad920e7 | 1649 | td->nr_open_files--; |
d6aed795 | 1650 | fio_file_clear_open(f); |
22a57ba8 | 1651 | assert(f->fd == -1); |
6977bcd0 | 1652 | return ret; |
0ad920e7 | 1653 | } |
bbf6b540 | 1654 | |
4d4e80f2 | 1655 | void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir) |
b2bd2bd9 | 1656 | { |
4d4e80f2 JA |
1657 | if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE) |
1658 | return; | |
29c1349f | 1659 | |
4d4e80f2 JA |
1660 | if (td->o.file_lock_mode == FILE_LOCK_READWRITE) { |
1661 | if (ddir == DDIR_READ) | |
d7df1d13 | 1662 | fio_rwlock_read(f->rwlock); |
4d4e80f2 | 1663 | else |
d7df1d13 | 1664 | fio_rwlock_write(f->rwlock); |
4d4e80f2 JA |
1665 | } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) |
1666 | fio_mutex_down(f->lock); | |
1667 | ||
d7df1d13 | 1668 | td->file_locks[f->fileno] = td->o.file_lock_mode; |
b2bd2bd9 JA |
1669 | } |
1670 | ||
4d4e80f2 | 1671 | void unlock_file(struct thread_data *td, struct fio_file *f) |
b2bd2bd9 | 1672 | { |
4d4e80f2 JA |
1673 | if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE) |
1674 | return; | |
4d4e80f2 | 1675 | |
d7df1d13 JA |
1676 | if (td->o.file_lock_mode == FILE_LOCK_READWRITE) |
1677 | fio_rwlock_unlock(f->rwlock); | |
1678 | else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) | |
4d4e80f2 | 1679 | fio_mutex_up(f->lock); |
d7df1d13 JA |
1680 | |
1681 | td->file_locks[f->fileno] = FILE_LOCK_NONE; | |
b2bd2bd9 JA |
1682 | } |
1683 | ||
4d4e80f2 JA |
1684 | void unlock_file_all(struct thread_data *td, struct fio_file *f) |
1685 | { | |
f2a2803a | 1686 | if (td->o.file_lock_mode == FILE_LOCK_NONE || !td->file_locks) |
27ddbfa0 | 1687 | return; |
d7df1d13 JA |
1688 | if (td->file_locks[f->fileno] != FILE_LOCK_NONE) |
1689 | unlock_file(td, f); | |
4d4e80f2 JA |
1690 | } |
1691 | ||
bbf6b540 JA |
1692 | static int recurse_dir(struct thread_data *td, const char *dirname) |
1693 | { | |
1694 | struct dirent *dir; | |
1695 | int ret = 0; | |
1696 | DIR *D; | |
1697 | ||
1698 | D = opendir(dirname); | |
1699 | if (!D) { | |
0ddb270c JA |
1700 | char buf[FIO_VERROR_SIZE]; |
1701 | ||
98ffb8f3 | 1702 | snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname); |
0ddb270c | 1703 | td_verror(td, errno, buf); |
bbf6b540 JA |
1704 | return 1; |
1705 | } | |
1706 | ||
1707 | while ((dir = readdir(D)) != NULL) { | |
1708 | char full_path[PATH_MAX]; | |
1709 | struct stat sb; | |
1710 | ||
e85b2b83 JA |
1711 | if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) |
1712 | continue; | |
96d32d51 | 1713 | |
b9fd788f | 1714 | sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name); |
bbf6b540 JA |
1715 | |
1716 | if (lstat(full_path, &sb) == -1) { | |
1717 | if (errno != ENOENT) { | |
1718 | td_verror(td, errno, "stat"); | |
68ace9e0 JA |
1719 | ret = 1; |
1720 | break; | |
bbf6b540 JA |
1721 | } |
1722 | } | |
1723 | ||
1724 | if (S_ISREG(sb.st_mode)) { | |
5903e7b7 | 1725 | add_file(td, full_path, 0, 1); |
bbf6b540 JA |
1726 | continue; |
1727 | } | |
0ddb270c JA |
1728 | if (!S_ISDIR(sb.st_mode)) |
1729 | continue; | |
bbf6b540 | 1730 | |
5ec10eaa JA |
1731 | ret = recurse_dir(td, full_path); |
1732 | if (ret) | |
bbf6b540 JA |
1733 | break; |
1734 | } | |
1735 | ||
1736 | closedir(D); | |
1737 | return ret; | |
1738 | } | |
1739 | ||
1740 | int add_dir_files(struct thread_data *td, const char *path) | |
1741 | { | |
0ddb270c JA |
1742 | int ret = recurse_dir(td, path); |
1743 | ||
1744 | if (!ret) | |
1745 | log_info("fio: opendir added %d files\n", td->o.nr_files); | |
1746 | ||
1747 | return ret; | |
bbf6b540 | 1748 | } |
cade3ef4 JA |
1749 | |
1750 | void dup_files(struct thread_data *td, struct thread_data *org) | |
1751 | { | |
1752 | struct fio_file *f; | |
1753 | unsigned int i; | |
9efef3c4 JA |
1754 | |
1755 | dprint(FD_FILE, "dup files: %d\n", org->files_index); | |
cade3ef4 JA |
1756 | |
1757 | if (!org->files) | |
1758 | return; | |
1759 | ||
9efef3c4 | 1760 | td->files = malloc(org->files_index * sizeof(f)); |
cade3ef4 | 1761 | |
d7df1d13 JA |
1762 | if (td->o.file_lock_mode != FILE_LOCK_NONE) |
1763 | td->file_locks = malloc(org->files_index); | |
1764 | ||
9efef3c4 | 1765 | for_each_file(org, f, i) { |
b0fe421a JA |
1766 | struct fio_file *__f; |
1767 | ||
7b5cb700 | 1768 | __f = alloc_new_file(td); |
0b9d69ec | 1769 | |
bc3456fa | 1770 | if (f->file_name) { |
f17c4392 | 1771 | __f->file_name = smalloc_strdup(f->file_name); |
81b3c86f | 1772 | if (!__f->file_name) |
c48c0be7 | 1773 | assert(0); |
0b9d69ec | 1774 | |
bc3456fa AC |
1775 | __f->filetype = f->filetype; |
1776 | } | |
b0fe421a | 1777 | |
67ad9242 JA |
1778 | if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) |
1779 | __f->lock = f->lock; | |
1780 | else if (td->o.file_lock_mode == FILE_LOCK_READWRITE) | |
1781 | __f->rwlock = f->rwlock; | |
1782 | ||
b0fe421a | 1783 | td->files[i] = __f; |
cade3ef4 JA |
1784 | } |
1785 | } | |
f29b25a3 JA |
1786 | |
1787 | /* | |
1788 | * Returns the index that matches the filename, or -1 if not there | |
1789 | */ | |
1790 | int get_fileno(struct thread_data *td, const char *fname) | |
1791 | { | |
1792 | struct fio_file *f; | |
1793 | unsigned int i; | |
1794 | ||
1795 | for_each_file(td, f, i) | |
1796 | if (!strcmp(f->file_name, fname)) | |
1797 | return i; | |
1798 | ||
1799 | return -1; | |
1800 | } | |
1801 | ||
1802 | /* | |
1803 | * For log usage, where we add/open/close files automatically | |
1804 | */ | |
1805 | void free_release_files(struct thread_data *td) | |
1806 | { | |
1807 | close_files(td); | |
66ee4002 JA |
1808 | td->o.nr_files = 0; |
1809 | td->o.open_files = 0; | |
f29b25a3 JA |
1810 | td->files_index = 0; |
1811 | td->nr_normal_files = 0; | |
1812 | } | |
33c48814 JA |
1813 | |
1814 | void fio_file_reset(struct thread_data *td, struct fio_file *f) | |
1815 | { | |
f1dfb668 JA |
1816 | int i; |
1817 | ||
1818 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { | |
1819 | f->last_pos[i] = f->file_offset; | |
1820 | f->last_start[i] = -1ULL; | |
1821 | } | |
1822 | ||
967d1b63 | 1823 | if (fio_file_axmap(f)) |
33c48814 | 1824 | axmap_reset(f->io_axmap); |
967d1b63 | 1825 | else if (fio_file_lfsr(f)) |
33c48814 JA |
1826 | lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]); |
1827 | } | |
002fe734 | 1828 | |
747b3105 | 1829 | bool fio_files_done(struct thread_data *td) |
002fe734 JA |
1830 | { |
1831 | struct fio_file *f; | |
1832 | unsigned int i; | |
1833 | ||
1834 | for_each_file(td, f, i) | |
1835 | if (!fio_file_done(f)) | |
747b3105 | 1836 | return false; |
002fe734 | 1837 | |
747b3105 | 1838 | return true; |
002fe734 | 1839 | } |
bcbfeefa CE |
1840 | |
1841 | /* free memory used in initialization phase only */ | |
190b8f0c CF |
1842 | void filesetup_mem_free(void) |
1843 | { | |
bcbfeefa CE |
1844 | free_already_allocated(); |
1845 | } | |
47534cda TK |
1846 | |
1847 | /* | |
1848 | * This function is for platforms which support direct I/O but not O_DIRECT. | |
1849 | */ | |
1850 | int fio_set_directio(struct thread_data *td, struct fio_file *f) | |
1851 | { | |
1852 | #ifdef FIO_OS_DIRECTIO | |
2905de74 | 1853 | int ret = fio_set_odirect(f); |
47534cda TK |
1854 | |
1855 | if (ret) { | |
1856 | td_verror(td, ret, "fio_set_directio"); | |
1857 | #if defined(__sun__) | |
1858 | if (ret == ENOTTY) { /* ENOTTY suggests RAW device or ZFS */ | |
1859 | log_err("fio: doing directIO to RAW devices or ZFS not supported\n"); | |
1860 | } else { | |
1861 | log_err("fio: the file system does not seem to support direct IO\n"); | |
1862 | } | |
1863 | #else | |
1864 | log_err("fio: the file system does not seem to support direct IO\n"); | |
1865 | #endif | |
1866 | return -1; | |
1867 | } | |
1868 | ||
1869 | return 0; | |
1870 | #else | |
51102e0d | 1871 | log_err("fio: direct IO is not supported on this host operating system\n"); |
47534cda TK |
1872 | return -1; |
1873 | #endif | |
1874 | } |