howto: Fix redundant entries
[fio.git] / filesetup.c
CommitLineData
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"
ecc314ba 14#include "os/os.h"
2316296a 15#include "hash.h"
7ebd796f 16#include "lib/axmap.h"
53cdc686 17
97ac992c 18#ifdef CONFIG_LINUX_FALLOCATE
a596f047
EG
19#include <linux/falloc.h>
20#endif
21
7172cfe8
JA
22static int root_warn;
23
c592b9fe
JA
24static inline void clear_error(struct thread_data *td)
25{
26 td->error = 0;
27 td->verror[0] = '\0';
28}
29
3baddf24
JA
30/*
31 * Leaves f->fd open on success, caller must close
32 */
7bb48f84 33static int extend_file(struct thread_data *td, struct fio_file *f)
25205e97 34{
ea443657 35 int r, new_layout = 0, unlink_file = 0, flags;
25205e97
JA
36 unsigned long long left;
37 unsigned int bs;
38 char *b;
b2a15192 39
4241ea8f
JA
40 if (read_only) {
41 log_err("fio: refusing extend of file due to read-only\n");
42 return 0;
43 }
44
507a702f
JA
45 /*
46 * check if we need to lay the file out complete again. fio
47 * does that for operations involving reads, or for writes
48 * where overwrite is set
49 */
ffdfabd4
JA
50 if (td_read(td) || (td_write(td) && td->o.overwrite) ||
51 (td_write(td) && td->io_ops->flags & FIO_NOEXTEND))
507a702f 52 new_layout = 1;
ea443657
JA
53 if (td_write(td) && !td->o.overwrite)
54 unlink_file = 1;
507a702f 55
6ae1f57f 56 if (unlink_file || new_layout) {
bd199f2b 57 dprint(FD_FILE, "layout unlink %s\n", f->file_name);
982016d6 58 if ((unlink(f->file_name) < 0) && (errno != ENOENT)) {
7bb48f84
JA
59 td_verror(td, errno, "unlink");
60 return 1;
61 }
62 }
63
507a702f
JA
64 flags = O_WRONLY | O_CREAT;
65 if (new_layout)
66 flags |= O_TRUNC;
67
ee56ad50 68 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
507a702f 69 f->fd = open(f->file_name, flags, 0644);
53cdc686 70 if (f->fd < 0) {
e1161c32 71 td_verror(td, errno, "open");
53cdc686
JA
72 return 1;
73 }
74
97ac992c 75#ifdef CONFIG_POSIX_FALLOCATE
a596f047
EG
76 if (!td->o.fill_device) {
77 switch (td->o.fallocate_mode) {
78 case FIO_FALLOCATE_NONE:
79 break;
80 case FIO_FALLOCATE_POSIX:
81 dprint(FD_FILE, "posix_fallocate file %s size %llu\n",
4b91ee8f
JA
82 f->file_name,
83 (unsigned long long) f->real_file_size);
a596f047
EG
84
85 r = posix_fallocate(f->fd, 0, f->real_file_size);
86 if (r > 0) {
87 log_err("fio: posix_fallocate fails: %s\n",
88 strerror(r));
89 }
90 break;
97ac992c 91#ifdef CONFIG_LINUX_FALLOCATE
a596f047
EG
92 case FIO_FALLOCATE_KEEP_SIZE:
93 dprint(FD_FILE,
94 "fallocate(FALLOC_FL_KEEP_SIZE) "
4b91ee8f
JA
95 "file %s size %llu\n", f->file_name,
96 (unsigned long long) f->real_file_size);
7bc8c2cf 97
a596f047
EG
98 r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0,
99 f->real_file_size);
888677a4 100 if (r != 0)
a596f047 101 td_verror(td, errno, "fallocate");
888677a4 102
a596f047 103 break;
97ac992c 104#endif /* CONFIG_LINUX_FALLOCATE */
a596f047
EG
105 default:
106 log_err("fio: unknown fallocate mode: %d\n",
107 td->o.fallocate_mode);
108 assert(0);
7bc8c2cf
JA
109 }
110 }
97ac992c 111#endif /* CONFIG_POSIX_FALLOCATE */
9b836561 112
fc74ac1d
SL
113 if (!new_layout)
114 goto done;
115
5e0074c2
JA
116 /*
117 * The size will be -1ULL when fill_device is used, so don't truncate
118 * or fallocate this file, just write it
119 */
120 if (!td->o.fill_device) {
121 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
4b91ee8f 122 (unsigned long long) f->real_file_size);
5e0074c2
JA
123 if (ftruncate(f->fd, f->real_file_size) == -1) {
124 td_verror(td, errno, "ftruncate");
125 goto err;
126 }
5e0074c2 127 }
40f8298c 128
2dc1bbeb 129 b = malloc(td->o.max_bs[DDIR_WRITE]);
53cdc686 130
7bb48f84 131 left = f->real_file_size;
53cdc686 132 while (left && !td->terminate) {
2dc1bbeb 133 bs = td->o.max_bs[DDIR_WRITE];
53cdc686
JA
134 if (bs > left)
135 bs = left;
136
cc86c395
JA
137 fill_io_buffer(td, b, bs, bs);
138
53cdc686
JA
139 r = write(f->fd, b, bs);
140
5e0074c2
JA
141 if (r > 0) {
142 left -= r;
53cdc686
JA
143 continue;
144 } else {
5e0074c2
JA
145 if (r < 0) {
146 int __e = errno;
147
148 if (__e == ENOSPC) {
149 if (td->o.fill_device)
150 break;
151 log_info("fio: ENOSPC on laying out "
152 "file, stopping\n");
153 break;
154 }
e1161c32 155 td_verror(td, errno, "write");
5e0074c2 156 } else
e1161c32 157 td_verror(td, EIO, "write");
53cdc686
JA
158
159 break;
160 }
161 }
162
ffdfabd4
JA
163 if (td->terminate) {
164 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
53cdc686 165 unlink(f->file_name);
ffdfabd4 166 } else if (td->o.create_fsync) {
98e1ac4e
JA
167 if (fsync(f->fd) < 0) {
168 td_verror(td, errno, "fsync");
169 goto err;
170 }
171 }
0d1cd207 172 if (td->o.fill_device && !td_write(td)) {
d6aed795 173 fio_file_clear_size_known(f);
5e0074c2
JA
174 if (td_io_get_file_size(td, f))
175 goto err;
176 if (f->io_size > f->real_file_size)
177 f->io_size = f->real_file_size;
178 }
53cdc686
JA
179
180 free(b);
507a702f 181done:
53cdc686
JA
182 return 0;
183err:
184 close(f->fd);
185 f->fd = -1;
186 return 1;
187}
188
afad68f7
ZY
189static int pre_read_file(struct thread_data *td, struct fio_file *f)
190{
b0f65863 191 int r, did_open = 0, old_runstate;
afad68f7
ZY
192 unsigned long long left;
193 unsigned int bs;
194 char *b;
195
9c0d2241
JA
196 if (td->io_ops->flags & FIO_PIPEIO)
197 return 0;
198
d6aed795 199 if (!fio_file_open(f)) {
b0f65863
JA
200 if (td->io_ops->open_file(td, f)) {
201 log_err("fio: cannot pre-read, failed to open file\n");
202 return 1;
203 }
204 did_open = 1;
205 }
206
207 old_runstate = td->runstate;
208 td_set_runstate(td, TD_PRE_READING);
209
afad68f7
ZY
210 bs = td->o.max_bs[DDIR_READ];
211 b = malloc(bs);
212 memset(b, 0, bs);
213
214 lseek(f->fd, f->file_offset, SEEK_SET);
215 left = f->io_size;
216
217 while (left && !td->terminate) {
218 if (bs > left)
219 bs = left;
220
221 r = read(f->fd, b, bs);
222
223 if (r == (int) bs) {
224 left -= bs;
225 continue;
226 } else {
227 td_verror(td, EIO, "pre_read");
228 break;
229 }
230 }
231
b0f65863
JA
232 td_set_runstate(td, old_runstate);
233
234 if (did_open)
235 td->io_ops->close_file(td, f);
afad68f7
ZY
236 free(b);
237 return 0;
238}
239
7bb48f84 240static unsigned long long get_rand_file_size(struct thread_data *td)
9c60ce64 241{
dc873b6f 242 unsigned long long ret, sized;
1294c3ec 243 unsigned long r;
9c60ce64 244
4c07ad86
JA
245 if (td->o.use_os_rand) {
246 r = os_random_long(&td->file_size_state);
247 sized = td->o.file_size_high - td->o.file_size_low;
248 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
249 } else {
250 r = __rand(&td->__file_size_state);
251 sized = td->o.file_size_high - td->o.file_size_low;
252 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0)));
253 }
254
5ec10eaa 255 ret += td->o.file_size_low;
2dc1bbeb 256 ret -= (ret % td->o.rw_min_bs);
9c60ce64
JA
257 return ret;
258}
259
53cdc686
JA
260static int file_size(struct thread_data *td, struct fio_file *f)
261{
262 struct stat st;
263
df9c26b1 264 if (stat(f->file_name, &st) == -1) {
7bb48f84
JA
265 td_verror(td, errno, "fstat");
266 return 1;
267 }
53cdc686 268
7bb48f84 269 f->real_file_size = st.st_size;
53cdc686
JA
270 return 0;
271}
272
273static int bdev_size(struct thread_data *td, struct fio_file *f)
274{
9b836561 275 unsigned long long bytes = 0;
53cdc686
JA
276 int r;
277
df9c26b1
JA
278 if (td->io_ops->open_file(td, f)) {
279 log_err("fio: failed opening blockdev %s for size check\n",
280 f->file_name);
281 return 1;
282 }
283
ecc314ba 284 r = blockdev_size(f, &bytes);
53cdc686 285 if (r) {
e1161c32 286 td_verror(td, r, "blockdev_size");
df9c26b1 287 goto err;
53cdc686
JA
288 }
289
7ab077ab
JA
290 if (!bytes) {
291 log_err("%s: zero sized block device?\n", f->file_name);
df9c26b1 292 goto err;
7ab077ab
JA
293 }
294
53cdc686 295 f->real_file_size = bytes;
22a57ba8 296 td->io_ops->close_file(td, f);
53cdc686 297 return 0;
df9c26b1
JA
298err:
299 td->io_ops->close_file(td, f);
300 return 1;
53cdc686
JA
301}
302
4ccdccd1
JA
303static int char_size(struct thread_data *td, struct fio_file *f)
304{
305#ifdef FIO_HAVE_CHARDEV_SIZE
9b836561 306 unsigned long long bytes = 0;
4ccdccd1
JA
307 int r;
308
309 if (td->io_ops->open_file(td, f)) {
310 log_err("fio: failed opening blockdev %s for size check\n",
311 f->file_name);
312 return 1;
313 }
314
ecc314ba 315 r = chardev_size(f, &bytes);
4ccdccd1
JA
316 if (r) {
317 td_verror(td, r, "chardev_size");
318 goto err;
319 }
320
321 if (!bytes) {
322 log_err("%s: zero sized char device?\n", f->file_name);
323 goto err;
324 }
325
326 f->real_file_size = bytes;
327 td->io_ops->close_file(td, f);
328 return 0;
329err:
330 td->io_ops->close_file(td, f);
331 return 1;
332#else
333 f->real_file_size = -1ULL;
334 return 0;
335#endif
336}
337
53cdc686
JA
338static int get_file_size(struct thread_data *td, struct fio_file *f)
339{
340 int ret = 0;
341
d6aed795 342 if (fio_file_size_known(f))
409b3417
JA
343 return 0;
344
7bb48f84
JA
345 if (f->filetype == FIO_TYPE_FILE)
346 ret = file_size(td, f);
347 else if (f->filetype == FIO_TYPE_BD)
53cdc686 348 ret = bdev_size(td, f);
4ccdccd1
JA
349 else if (f->filetype == FIO_TYPE_CHAR)
350 ret = char_size(td, f);
53cdc686
JA
351 else
352 f->real_file_size = -1;
353
354 if (ret)
355 return ret;
356
357 if (f->file_offset > f->real_file_size) {
0f2152c1 358 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
4e0a8fa2
JA
359 (unsigned long long) f->file_offset,
360 (unsigned long long) f->real_file_size);
53cdc686
JA
361 return 1;
362 }
363
d6aed795 364 fio_file_set_size_known(f);
53cdc686
JA
365 return 0;
366}
367
3baddf24
JA
368static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
369 unsigned long long off,
370 unsigned long long len)
e5b401d4
JA
371{
372 int ret = 0;
373
5e0074c2 374 if (len == -1ULL)
3baddf24
JA
375 len = f->io_size;
376 if (off == -1ULL)
377 off = f->file_offset;
ee56ad50 378
0d1cd207
JA
379 if (len == -1ULL || off == -1ULL)
380 return 0;
381
3baddf24
JA
382 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
383 len);
b5af8293 384
e5b401d4
JA
385 /*
386 * FIXME: add blockdev flushing too
387 */
a1c58075 388 if (f->mmap_ptr) {
03e20d68 389 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
a1c58075 390#ifdef FIO_MADV_FREE
3e10fb83
JA
391 if (f->filetype == FIO_TYPE_BD)
392 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
a1c58075
JA
393#endif
394 } else if (f->filetype == FIO_TYPE_FILE) {
ecc314ba 395 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
5ec10eaa 396 } else if (f->filetype == FIO_TYPE_BD) {
ecc314ba 397 ret = blockdev_invalidate_cache(f);
7e0e25c9 398 if (ret < 0 && errno == EACCES && geteuid()) {
7172cfe8 399 if (!root_warn) {
5ec10eaa
JA
400 log_err("fio: only root may flush block "
401 "devices. Cache flush bypassed!\n");
7172cfe8
JA
402 root_warn = 1;
403 }
7e0e25c9
JA
404 ret = 0;
405 }
b5605e9d 406 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
e5b401d4
JA
407 ret = 0;
408
409 if (ret < 0) {
e1161c32 410 td_verror(td, errno, "invalidate_cache");
e5b401d4 411 return 1;
3baddf24
JA
412 } else if (ret > 0) {
413 td_verror(td, ret, "invalidate_cache");
414 return 1;
e5b401d4
JA
415 }
416
ad2da605 417 return ret;
3baddf24
JA
418
419}
420
421int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
422{
d6aed795 423 if (!fio_file_open(f))
a5fb461f
JA
424 return 0;
425
5e0074c2 426 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
e5b401d4
JA
427}
428
6977bcd0 429int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
53cdc686 430{
6977bcd0
JA
431 int ret = 0;
432
ee56ad50 433 dprint(FD_FILE, "fd close %s\n", f->file_name);
4906e0b5
JA
434
435 remove_file_hash(f);
436
6977bcd0
JA
437 if (close(f->fd) < 0)
438 ret = errno;
439
b5af8293 440 f->fd = -1;
e6c4d732
JA
441
442 if (f->shadow_fd != -1) {
443 close(f->shadow_fd);
444 f->shadow_fd = -1;
445 }
446
57e54e08 447 f->engine_data = 0;
6977bcd0 448 return ret;
53cdc686
JA
449}
450
1ccc6dc7 451int file_lookup_open(struct fio_file *f, int flags)
53cdc686 452{
29c1349f 453 struct fio_file *__f;
4d4e80f2
JA
454 int from_hash;
455
456 __f = lookup_file_hash(f->file_name);
457 if (__f) {
9efef3c4 458 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
4d4e80f2
JA
459 /*
460 * racy, need the __f->lock locked
461 */
462 f->lock = __f->lock;
4d4e80f2
JA
463 from_hash = 1;
464 } else {
9efef3c4 465 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
4d4e80f2
JA
466 from_hash = 0;
467 }
468
e8670ef8 469 f->fd = open(f->file_name, flags, 0600);
4d4e80f2
JA
470 return from_hash;
471}
472
e6c4d732
JA
473static int file_close_shadow_fds(struct thread_data *td)
474{
475 struct fio_file *f;
476 int num_closed = 0;
477 unsigned int i;
478
479 for_each_file(td, f, i) {
480 if (f->shadow_fd == -1)
481 continue;
482
483 close(f->shadow_fd);
484 f->shadow_fd = -1;
485 num_closed++;
486 }
487
488 return num_closed;
489}
490
4d4e80f2
JA
491int generic_open_file(struct thread_data *td, struct fio_file *f)
492{
66159828 493 int is_std = 0;
53cdc686 494 int flags = 0;
29c1349f 495 int from_hash = 0;
53cdc686 496
ee56ad50
JA
497 dprint(FD_FILE, "fd open %s\n", f->file_name);
498
6eaf09d6
SL
499 if (td_trim(td) && f->filetype != FIO_TYPE_BD) {
500 log_err("fio: trim only applies to block device\n");
501 return 1;
502 }
503
66159828
JA
504 if (!strcmp(f->file_name, "-")) {
505 if (td_rw(td)) {
506 log_err("fio: can't read/write to stdin/out\n");
507 return 1;
508 }
509 is_std = 1;
ce98fa66
JA
510
511 /*
512 * move output logging to stderr, if we are writing to stdout
513 */
514 if (td_write(td))
515 f_out = stderr;
66159828
JA
516 }
517
6eaf09d6
SL
518 if (td_trim(td))
519 goto skip_flags;
2dc1bbeb 520 if (td->o.odirect)
2fd233b7 521 flags |= OS_O_DIRECT;
2dc1bbeb 522 if (td->o.sync_io)
2fd233b7 523 flags |= O_SYNC;
814452bd
JA
524 if (td->o.create_on_open)
525 flags |= O_CREAT;
6eaf09d6
SL
526skip_flags:
527 if (f->filetype != FIO_TYPE_FILE)
528 flags |= FIO_O_NOATIME;
53cdc686 529
056f3459 530open_again:
660a1cb5 531 if (td_write(td)) {
17308158
JA
532 if (!read_only)
533 flags |= O_RDWR;
53cdc686 534
af52b345 535 if (f->filetype == FIO_TYPE_FILE)
2fd233b7 536 flags |= O_CREAT;
2fd233b7 537
66159828
JA
538 if (is_std)
539 f->fd = dup(STDOUT_FILENO);
4d4e80f2
JA
540 else
541 from_hash = file_lookup_open(f, flags);
6eaf09d6 542 } else if (td_read(td)) {
4241ea8f 543 if (f->filetype == FIO_TYPE_CHAR && !read_only)
2fd233b7
JA
544 flags |= O_RDWR;
545 else
546 flags |= O_RDONLY;
547
66159828
JA
548 if (is_std)
549 f->fd = dup(STDIN_FILENO);
4d4e80f2
JA
550 else
551 from_hash = file_lookup_open(f, flags);
6eaf09d6
SL
552 } else { //td trim
553 flags |= O_RDWR;
554 from_hash = file_lookup_open(f, flags);
53cdc686
JA
555 }
556
557 if (f->fd == -1) {
e4e33258 558 char buf[FIO_VERROR_SIZE];
e1161c32
JA
559 int __e = errno;
560
835d9b9e 561 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
5921e80c 562 flags &= ~FIO_O_NOATIME;
056f3459
AC
563 goto open_again;
564 }
e6c4d732
JA
565 if (__e == EMFILE && file_close_shadow_fds(td))
566 goto open_again;
056f3459 567
98ffb8f3 568 snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
e4e33258 569
a93c5f04
JA
570 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
571 log_err("fio: looks like your file system does not " \
572 "support direct=1/buffered=0\n");
573 }
574
e4e33258 575 td_verror(td, __e, buf);
53cdc686
JA
576 }
577
29c1349f
JA
578 if (!from_hash && f->fd != -1) {
579 if (add_file_hash(f)) {
3f0ca9b9 580 int fio_unused ret;
29c1349f
JA
581
582 /*
e6c4d732
JA
583 * Stash away descriptor for later close. This is to
584 * work-around a "feature" on Linux, where a close of
585 * an fd that has been opened for write will trigger
586 * udev to call blkid to check partitions, fs id, etc.
de8f6de9 587 * That pollutes the device cache, which can slow down
e6c4d732 588 * unbuffered accesses.
29c1349f 589 */
e6c4d732
JA
590 if (f->shadow_fd == -1)
591 f->shadow_fd = f->fd;
592 else {
593 /*
594 * OK to ignore, we haven't done anything
595 * with it
596 */
597 ret = generic_close_file(td, f);
598 }
29c1349f
JA
599 goto open_again;
600 }
601 }
4906e0b5 602
53cdc686 603 return 0;
b5af8293
JA
604}
605
df9c26b1 606int generic_get_file_size(struct thread_data *td, struct fio_file *f)
21972cde 607{
df9c26b1 608 return get_file_size(td, f);
21972cde
JA
609}
610
7bb48f84
JA
611/*
612 * open/close all files, so that ->real_file_size gets set
613 */
bab3fd58 614static int get_file_sizes(struct thread_data *td)
7bb48f84
JA
615{
616 struct fio_file *f;
617 unsigned int i;
bab3fd58 618 int err = 0;
7bb48f84
JA
619
620 for_each_file(td, f, i) {
5ec10eaa
JA
621 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
622 f->file_name);
9efef3c4 623
99a47c69 624 if (td_io_get_file_size(td, f)) {
40b44f4a
JA
625 if (td->error != ENOENT) {
626 log_err("%s\n", td->verror);
627 err = 1;
628 }
541d66d7 629 clear_error(td);
07eb79df 630 }
409b3417
JA
631
632 if (f->real_file_size == -1ULL && td->o.size)
633 f->real_file_size = td->o.size / td->o.nr_files;
7bb48f84 634 }
bab3fd58
JA
635
636 return err;
7bb48f84
JA
637}
638
2e3bd4c2
JA
639struct fio_mount {
640 struct flist_head list;
641 const char *base;
642 char __base[256];
643 unsigned int key;
644};
645
646/*
647 * Get free number of bytes for each file on each unique mount.
648 */
649static unsigned long long get_fs_free_counts(struct thread_data *td)
650{
651 struct flist_head *n, *tmp;
68b0316f 652 unsigned long long ret = 0;
2e3bd4c2
JA
653 struct fio_mount *fm;
654 FLIST_HEAD(list);
655 struct fio_file *f;
656 unsigned int i;
657
658 for_each_file(td, f, i) {
659 struct stat sb;
660 char buf[256];
661
4ccdccd1
JA
662 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
663 if (f->real_file_size != -1ULL)
664 ret += f->real_file_size;
68b0316f
JA
665 continue;
666 } else if (f->filetype != FIO_TYPE_FILE)
667 continue;
668
2e3bd4c2
JA
669 strcpy(buf, f->file_name);
670
671 if (stat(buf, &sb) < 0) {
672 if (errno != ENOENT)
673 break;
674 strcpy(buf, ".");
675 if (stat(buf, &sb) < 0)
676 break;
677 }
678
679 fm = NULL;
680 flist_for_each(n, &list) {
681 fm = flist_entry(n, struct fio_mount, list);
682 if (fm->key == sb.st_dev)
683 break;
684
685 fm = NULL;
686 }
687
688 if (fm)
689 continue;
690
691 fm = malloc(sizeof(*fm));
692 strcpy(fm->__base, buf);
693 fm->base = basename(fm->__base);
694 fm->key = sb.st_dev;
695 flist_add(&fm->list, &list);
696 }
697
2e3bd4c2
JA
698 flist_for_each_safe(n, tmp, &list) {
699 unsigned long long sz;
700
701 fm = flist_entry(n, struct fio_mount, list);
702 flist_del(&fm->list);
703
704 sz = get_fs_size(fm->base);
705 if (sz && sz != -1ULL)
706 ret += sz;
707
708 free(fm);
709 }
710
711 return ret;
712}
713
293b8c1f 714uint64_t get_start_offset(struct thread_data *td)
ce95d651
DE
715{
716 return td->o.start_offset +
717 (td->thread_number - 1) * td->o.offset_increment;
718}
719
7bb48f84
JA
720/*
721 * Open the files and setup files sizes, creating files if necessary.
722 */
53cdc686
JA
723int setup_files(struct thread_data *td)
724{
7bb48f84 725 unsigned long long total_size, extend_size;
de98bd30 726 struct thread_options *o = &td->o;
53cdc686 727 struct fio_file *f;
af52b345 728 unsigned int i;
000b0803 729 int err = 0, need_extend;
e90391eb 730 int old_state;
53cdc686 731
ee56ad50
JA
732 dprint(FD_FILE, "setup files\n");
733
e90391eb
JA
734 old_state = td->runstate;
735 td_set_runstate(td, TD_SETTING_UP);
736
de98bd30 737 if (o->read_iolog_file)
25460cf6 738 goto done;
691c8fb0 739
53cdc686
JA
740 /*
741 * if ioengine defines a setup() method, it's responsible for
7bb48f84
JA
742 * opening the files and setting f->real_file_size to indicate
743 * the valid range for that file.
53cdc686
JA
744 */
745 if (td->io_ops->setup)
7bb48f84
JA
746 err = td->io_ops->setup(td);
747 else
bab3fd58 748 err = get_file_sizes(td);
53cdc686 749
f1027063 750 if (err)
e90391eb 751 goto err_out;
f1027063 752
0a7eb121 753 /*
7bb48f84
JA
754 * check sizes. if the files/devices do not exist and the size
755 * isn't passed to fio, abort.
0a7eb121 756 */
7bb48f84
JA
757 total_size = 0;
758 for_each_file(td, f, i) {
759 if (f->real_file_size == -1ULL)
760 total_size = -1ULL;
761 else
762 total_size += f->real_file_size;
763 }
0a7eb121 764
de98bd30 765 if (o->fill_device)
2e3bd4c2
JA
766 td->fill_device_size = get_fs_free_counts(td);
767
7bb48f84
JA
768 /*
769 * device/file sizes are zero and no size given, punt
770 */
de98bd30
JA
771 if ((!total_size || total_size == -1ULL) && !o->size &&
772 !(td->io_ops->flags & FIO_NOIO) && !o->fill_device &&
773 !(o->nr_files && (o->file_size_low || o->file_size_high))) {
774 log_err("%s: you need to specify size=\n", o->name);
e1161c32 775 td_verror(td, EINVAL, "total_file_size");
e90391eb 776 goto err_out;
53cdc686
JA
777 }
778
7bb48f84
JA
779 /*
780 * now file sizes are known, so we can set ->io_size. if size= is
781 * not given, ->io_size is just equal to ->real_file_size. if size
782 * is given, ->io_size is size / nr_files.
783 */
784 extend_size = total_size = 0;
785 need_extend = 0;
786 for_each_file(td, f, i) {
ce95d651 787 f->file_offset = get_start_offset(td);
bcdedd0a 788
de98bd30 789 if (!o->file_size_low) {
7bb48f84
JA
790 /*
791 * no file size range given, file size is equal to
792 * total size divided by number of files. if that is
793 * zero, set it to the real file size.
794 */
de98bd30 795 f->io_size = o->size / o->nr_files;
65bdb10a 796 if (!f->io_size)
273f8c91 797 f->io_size = f->real_file_size - f->file_offset;
de98bd30
JA
798 } else if (f->real_file_size < o->file_size_low ||
799 f->real_file_size > o->file_size_high) {
800 if (f->file_offset > o->file_size_low)
bcdedd0a 801 goto err_offset;
7bb48f84
JA
802 /*
803 * file size given. if it's fixed, use that. if it's a
804 * range, generate a random size in-between.
805 */
de98bd30
JA
806 if (o->file_size_low == o->file_size_high)
807 f->io_size = o->file_size_low - f->file_offset;
808 else {
5ec10eaa
JA
809 f->io_size = get_rand_file_size(td)
810 - f->file_offset;
811 }
65bdb10a 812 } else
bcdedd0a 813 f->io_size = f->real_file_size - f->file_offset;
53cdc686 814
7bb48f84
JA
815 if (f->io_size == -1ULL)
816 total_size = -1ULL;
4d002569 817 else {
de98bd30
JA
818 if (o->size_percent)
819 f->io_size = (f->io_size * o->size_percent) / 100;
7bb48f84 820 total_size += f->io_size;
4d002569 821 }
7bb48f84
JA
822
823 if (f->filetype == FIO_TYPE_FILE &&
bcdedd0a 824 (f->io_size + f->file_offset) > f->real_file_size &&
7bb48f84 825 !(td->io_ops->flags & FIO_DISKLESSIO)) {
de98bd30 826 if (!o->create_on_open) {
814452bd
JA
827 need_extend++;
828 extend_size += (f->io_size + f->file_offset);
829 } else
830 f->real_file_size = f->io_size + f->file_offset;
d6aed795 831 fio_file_set_extend(f);
5ec10eaa 832 }
7bb48f84 833 }
53cdc686 834
de98bd30
JA
835 if (!o->size || o->size > total_size)
836 o->size = total_size;
21972cde 837
7bb48f84
JA
838 /*
839 * See if we need to extend some files
840 */
841 if (need_extend) {
842 temp_stall_ts = 1;
f3afa57e 843 if (output_format == FIO_OUTPUT_NORMAL)
a7ba8c5f 844 log_info("%s: Laying out IO file(s) (%u file(s) /"
de98bd30 845 " %lluMB)\n", o->name, need_extend,
a7ba8c5f 846 extend_size >> 20);
7bb48f84
JA
847
848 for_each_file(td, f, i) {
5e0074c2 849 unsigned long long old_len = -1ULL, extend_len = -1ULL;
3baddf24 850
d6aed795 851 if (!fio_file_extend(f))
7bb48f84
JA
852 continue;
853
409b3417 854 assert(f->filetype == FIO_TYPE_FILE);
d6aed795 855 fio_file_clear_extend(f);
de98bd30 856 if (!o->fill_device) {
5e0074c2 857 old_len = f->real_file_size;
0b9d69ec
JA
858 extend_len = f->io_size + f->file_offset -
859 old_len;
5e0074c2 860 }
bcdedd0a 861 f->real_file_size = (f->io_size + f->file_offset);
7bb48f84
JA
862 err = extend_file(td, f);
863 if (err)
3baddf24 864 break;
5e0074c2 865
3baddf24
JA
866 err = __file_invalidate_cache(td, f, old_len,
867 extend_len);
868 close(f->fd);
869 f->fd = -1;
870 if (err)
7bb48f84
JA
871 break;
872 }
873 temp_stall_ts = 0;
874 }
875
876 if (err)
e90391eb 877 goto err_out;
7bb48f84 878
de98bd30
JA
879 if (!o->zone_size)
880 o->zone_size = o->size;
7bb48f84 881
ea966f81
JA
882 /*
883 * iolog already set the total io size, if we read back
884 * stored entries.
885 */
de98bd30
JA
886 if (!o->read_iolog_file)
887 td->total_io_size = o->size * o->loops;
25460cf6
JA
888
889done:
de98bd30 890 if (o->create_only)
25460cf6
JA
891 td->done = 1;
892
e90391eb 893 td_set_runstate(td, old_state);
7bb48f84 894 return 0;
bcdedd0a 895err_offset:
de98bd30 896 log_err("%s: you need to specify valid offset=\n", o->name);
e90391eb
JA
897err_out:
898 td_set_runstate(td, old_state);
bcdedd0a 899 return 1;
53cdc686
JA
900}
901
afad68f7
ZY
902int pre_read_files(struct thread_data *td)
903{
904 struct fio_file *f;
905 unsigned int i;
906
907 dprint(FD_FILE, "pre_read files\n");
908
909 for_each_file(td, f, i) {
910 pre_read_file(td, f);
911 }
912
913 return 1;
914}
915
9c6f6316
JA
916static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
917{
2316296a 918 unsigned int range_size, seed;
9c6f6316 919 unsigned long nranges;
21415db9 920 uint64_t file_size;
9c6f6316
JA
921
922 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
21415db9 923 file_size = min(f->real_file_size, f->io_size);
9c6f6316 924
21415db9 925 nranges = (file_size + range_size - 1) / range_size;
9c6f6316 926
2316296a 927 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
8425687e
JA
928 if (!td->o.rand_repeatable)
929 seed = td->rand_seeds[4];
930
9c6f6316 931 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
888677a4 932 zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed);
9c6f6316 933 else
888677a4 934 pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed);
9c6f6316
JA
935
936 return 1;
937}
938
939static int init_rand_distribution(struct thread_data *td)
940{
941 struct fio_file *f;
942 unsigned int i;
943 int state;
944
945 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
946 return 0;
947
948 state = td->runstate;
949 td_set_runstate(td, TD_SETTING_UP);
950 for_each_file(td, f, i)
951 __init_rand_distribution(td, f);
952 td_set_runstate(td, state);
953
954 return 1;
955}
956
68727076
JA
957int init_random_map(struct thread_data *td)
958{
51ede0b1 959 unsigned long long blocks;
68727076
JA
960 struct fio_file *f;
961 unsigned int i;
962
9c6f6316
JA
963 if (init_rand_distribution(td))
964 return 0;
3831a843 965 if (!td_random(td))
68727076
JA
966 return 0;
967
968 for_each_file(td, f, i) {
38f30c81
JA
969 uint64_t file_size = min(f->real_file_size, f->io_size);
970
53737ae0
JA
971 blocks = file_size / (unsigned long long) td->o.rw_min_bs;
972
8055e41d 973 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
82af46be
JA
974 unsigned long seed;
975
976 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
977
d474cbc9 978 if (!lfsr_init(&f->lfsr, blocks, seed, seed & 0xF))
8055e41d 979 continue;
3831a843 980 } else if (!td->o.norandommap) {
7ebd796f
JA
981 f->io_axmap = axmap_new(blocks);
982 if (f->io_axmap)
8055e41d 983 continue;
1cad7121
JA
984 } else if (td->o.norandommap)
985 continue;
ceadd59e 986
2b386d25 987 if (!td->o.softrandommap) {
5ec10eaa
JA
988 log_err("fio: failed allocating random map. If running"
989 " a large number of jobs, try the 'norandommap'"
2b386d25
JA
990 " option or set 'softrandommap'. Or give"
991 " a larger --alloc-size to fio.\n");
68727076
JA
992 return 1;
993 }
303032ae
JA
994
995 log_info("fio: file %s failed allocating random map. Running "
996 "job without.\n", f->file_name);
68727076
JA
997 }
998
999 return 0;
1000}
1001
53cdc686
JA
1002void close_files(struct thread_data *td)
1003{
0ab8db89 1004 struct fio_file *f;
af52b345 1005 unsigned int i;
53cdc686 1006
2be3eec3
JA
1007 for_each_file(td, f, i) {
1008 if (fio_file_open(f))
1009 td_io_close_file(td, f);
1010 }
24ffd2c2
JA
1011}
1012
1013void close_and_free_files(struct thread_data *td)
1014{
1015 struct fio_file *f;
1016 unsigned int i;
1017
ee56ad50
JA
1018 dprint(FD_FILE, "close files\n");
1019
0ab8db89 1020 for_each_file(td, f, i) {
ffdfabd4
JA
1021 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1022 dprint(FD_FILE, "free unlink %s\n", f->file_name);
132ad46d 1023 unlink(f->file_name);
ffdfabd4 1024 }
bdb4e2e9 1025
22a57ba8
JA
1026 if (fio_file_open(f))
1027 td_io_close_file(td, f);
1028
b9fbcf21 1029 remove_file_hash(f);
b3dc7f07 1030
f17c4392 1031 sfree(f->file_name);
fa1da865 1032 f->file_name = NULL;
7ebd796f
JA
1033 axmap_free(f->io_axmap);
1034 f->io_axmap = NULL;
78d99e6a 1035 sfree(f);
53cdc686 1036 }
b4a6a59a 1037
2dc1bbeb 1038 td->o.filename = NULL;
cade3ef4 1039 free(td->files);
d7df1d13 1040 free(td->file_locks);
9efef3c4 1041 td->files_index = 0;
b4a6a59a 1042 td->files = NULL;
d7df1d13 1043 td->file_locks = NULL;
2dc1bbeb 1044 td->o.nr_files = 0;
53cdc686 1045}
af52b345 1046
e3bab463 1047static void get_file_type(struct fio_file *f)
af52b345
JA
1048{
1049 struct stat sb;
1050
66159828
JA
1051 if (!strcmp(f->file_name, "-"))
1052 f->filetype = FIO_TYPE_PIPE;
1053 else
1054 f->filetype = FIO_TYPE_FILE;
af52b345 1055
3892182a
BC
1056 /* \\.\ is the device namespace in Windows, where every file is
1057 * a block device */
1058 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1059 f->filetype = FIO_TYPE_BD;
1060
b30d395e 1061 if (!stat(f->file_name, &sb)) {
3892182a 1062 if (S_ISBLK(sb.st_mode))
af52b345
JA
1063 f->filetype = FIO_TYPE_BD;
1064 else if (S_ISCHR(sb.st_mode))
1065 f->filetype = FIO_TYPE_CHAR;
b5605e9d
JA
1066 else if (S_ISFIFO(sb.st_mode))
1067 f->filetype = FIO_TYPE_PIPE;
af52b345
JA
1068 }
1069}
1070
f29b25a3 1071int add_file(struct thread_data *td, const char *fname)
af52b345 1072{
7b4e4fe5 1073 int cur_files = td->files_index;
bd0ee748 1074 char file_name[PATH_MAX];
af52b345 1075 struct fio_file *f;
bd0ee748 1076 int len = 0;
af52b345 1077
ee56ad50
JA
1078 dprint(FD_FILE, "add file %s\n", fname);
1079
f17c4392 1080 f = smalloc(sizeof(*f));
c48c0be7
JA
1081 if (!f) {
1082 log_err("fio: smalloc OOM\n");
1083 assert(0);
1084 }
0b9d69ec 1085
af52b345 1086 f->fd = -1;
e6c4d732 1087 f->shadow_fd = -1;
33c48814 1088 fio_file_reset(td, f);
bd0ee748 1089
fc99bc0d 1090 if (td->files_size <= td->files_index) {
1983e327 1091 unsigned int new_size = td->o.nr_files + 1;
126d65c6 1092
fc99bc0d
JA
1093 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1094
1095 td->files = realloc(td->files, new_size * sizeof(f));
d537c08b
JM
1096 if (td->files == NULL) {
1097 log_err("fio: realloc OOM\n");
1098 assert(0);
1099 }
d7df1d13
JA
1100 if (td->o.file_lock_mode != FILE_LOCK_NONE) {
1101 td->file_locks = realloc(td->file_locks, new_size);
1102 if (!td->file_locks) {
1103 log_err("fio: realloc OOM\n");
1104 assert(0);
1105 }
1106 td->file_locks[cur_files] = FILE_LOCK_NONE;
1107 }
fc99bc0d
JA
1108 td->files_size = new_size;
1109 }
8bb7679e 1110 td->files[cur_files] = f;
89ac1d48 1111 f->fileno = cur_files;
126d65c6 1112
07eb79df
JA
1113 /*
1114 * init function, io engine may not be loaded yet
1115 */
1116 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1117 f->real_file_size = -1ULL;
1118
bd0ee748
JA
1119 if (td->o.directory)
1120 len = sprintf(file_name, "%s/", td->o.directory);
1121
1122 sprintf(file_name + len, "%s", fname);
f17c4392 1123 f->file_name = smalloc_strdup(file_name);
c48c0be7
JA
1124 if (!f->file_name) {
1125 log_err("fio: smalloc OOM\n");
1126 assert(0);
1127 }
0b9d69ec 1128
e3bab463 1129 get_file_type(f);
af52b345 1130
4d4e80f2
JA
1131 switch (td->o.file_lock_mode) {
1132 case FILE_LOCK_NONE:
1133 break;
1134 case FILE_LOCK_READWRITE:
d7df1d13 1135 f->rwlock = fio_rwlock_init();
4d4e80f2
JA
1136 break;
1137 case FILE_LOCK_EXCLUSIVE:
521da527 1138 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
4d4e80f2
JA
1139 break;
1140 default:
1141 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1142 assert(0);
1143 }
29c1349f 1144
7b4e4fe5 1145 td->files_index++;
1549441c
JA
1146 if (f->filetype == FIO_TYPE_FILE)
1147 td->nr_normal_files++;
f29b25a3 1148
5ec10eaa
JA
1149 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1150 cur_files);
9efef3c4 1151
f29b25a3 1152 return cur_files;
af52b345 1153}
0ad920e7 1154
49ffb4a2
JA
1155int add_file_exclusive(struct thread_data *td, const char *fname)
1156{
1157 struct fio_file *f;
1158 unsigned int i;
1159
1160 for_each_file(td, f, i) {
1161 if (!strcmp(f->file_name, fname))
1162 return i;
1163 }
1164
1165 return add_file(td, fname);
1166}
1167
0ad920e7
JA
1168void get_file(struct fio_file *f)
1169{
8172fe97 1170 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
d6aed795 1171 assert(fio_file_open(f));
0ad920e7
JA
1172 f->references++;
1173}
1174
6977bcd0 1175int put_file(struct thread_data *td, struct fio_file *f)
0ad920e7 1176{
98e1ac4e 1177 int f_ret = 0, ret = 0;
6977bcd0 1178
8172fe97 1179 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
ee56ad50 1180
22a57ba8
JA
1181 if (!fio_file_open(f)) {
1182 assert(f->fd == -1);
6977bcd0 1183 return 0;
22a57ba8 1184 }
0ad920e7
JA
1185
1186 assert(f->references);
1187 if (--f->references)
6977bcd0 1188 return 0;
0ad920e7 1189
d424d4dd 1190 if (should_fsync(td) && td->o.fsync_on_close)
98e1ac4e 1191 f_ret = fsync(f->fd);
ebb1415f 1192
0ad920e7 1193 if (td->io_ops->close_file)
6977bcd0 1194 ret = td->io_ops->close_file(td, f);
1020a139 1195
98e1ac4e 1196 if (!ret)
a5fb461f 1197 ret = f_ret;
98e1ac4e 1198
0ad920e7 1199 td->nr_open_files--;
d6aed795 1200 fio_file_clear_open(f);
22a57ba8 1201 assert(f->fd == -1);
6977bcd0 1202 return ret;
0ad920e7 1203}
bbf6b540 1204
4d4e80f2 1205void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
b2bd2bd9 1206{
4d4e80f2
JA
1207 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1208 return;
29c1349f 1209
4d4e80f2
JA
1210 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1211 if (ddir == DDIR_READ)
d7df1d13 1212 fio_rwlock_read(f->rwlock);
4d4e80f2 1213 else
d7df1d13 1214 fio_rwlock_write(f->rwlock);
4d4e80f2
JA
1215 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1216 fio_mutex_down(f->lock);
1217
d7df1d13 1218 td->file_locks[f->fileno] = td->o.file_lock_mode;
b2bd2bd9
JA
1219}
1220
4d4e80f2 1221void unlock_file(struct thread_data *td, struct fio_file *f)
b2bd2bd9 1222{
4d4e80f2
JA
1223 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1224 return;
4d4e80f2 1225
d7df1d13
JA
1226 if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1227 fio_rwlock_unlock(f->rwlock);
1228 else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
4d4e80f2 1229 fio_mutex_up(f->lock);
d7df1d13
JA
1230
1231 td->file_locks[f->fileno] = FILE_LOCK_NONE;
b2bd2bd9
JA
1232}
1233
4d4e80f2
JA
1234void unlock_file_all(struct thread_data *td, struct fio_file *f)
1235{
d7df1d13
JA
1236 if (td->file_locks[f->fileno] != FILE_LOCK_NONE)
1237 unlock_file(td, f);
4d4e80f2
JA
1238}
1239
bbf6b540
JA
1240static int recurse_dir(struct thread_data *td, const char *dirname)
1241{
1242 struct dirent *dir;
1243 int ret = 0;
1244 DIR *D;
1245
1246 D = opendir(dirname);
1247 if (!D) {
0ddb270c
JA
1248 char buf[FIO_VERROR_SIZE];
1249
98ffb8f3 1250 snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
0ddb270c 1251 td_verror(td, errno, buf);
bbf6b540
JA
1252 return 1;
1253 }
1254
1255 while ((dir = readdir(D)) != NULL) {
1256 char full_path[PATH_MAX];
1257 struct stat sb;
1258
e85b2b83
JA
1259 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1260 continue;
96d32d51 1261
b9fd788f 1262 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
bbf6b540
JA
1263
1264 if (lstat(full_path, &sb) == -1) {
1265 if (errno != ENOENT) {
1266 td_verror(td, errno, "stat");
1267 return 1;
1268 }
1269 }
1270
1271 if (S_ISREG(sb.st_mode)) {
1272 add_file(td, full_path);
2dc1bbeb 1273 td->o.nr_files++;
bbf6b540
JA
1274 continue;
1275 }
0ddb270c
JA
1276 if (!S_ISDIR(sb.st_mode))
1277 continue;
bbf6b540 1278
5ec10eaa
JA
1279 ret = recurse_dir(td, full_path);
1280 if (ret)
bbf6b540
JA
1281 break;
1282 }
1283
1284 closedir(D);
1285 return ret;
1286}
1287
1288int add_dir_files(struct thread_data *td, const char *path)
1289{
0ddb270c
JA
1290 int ret = recurse_dir(td, path);
1291
1292 if (!ret)
1293 log_info("fio: opendir added %d files\n", td->o.nr_files);
1294
1295 return ret;
bbf6b540 1296}
cade3ef4
JA
1297
1298void dup_files(struct thread_data *td, struct thread_data *org)
1299{
1300 struct fio_file *f;
1301 unsigned int i;
9efef3c4
JA
1302
1303 dprint(FD_FILE, "dup files: %d\n", org->files_index);
cade3ef4
JA
1304
1305 if (!org->files)
1306 return;
1307
9efef3c4 1308 td->files = malloc(org->files_index * sizeof(f));
cade3ef4 1309
d7df1d13
JA
1310 if (td->o.file_lock_mode != FILE_LOCK_NONE)
1311 td->file_locks = malloc(org->files_index);
1312
9efef3c4 1313 for_each_file(org, f, i) {
b0fe421a
JA
1314 struct fio_file *__f;
1315
f17c4392 1316 __f = smalloc(sizeof(*__f));
c48c0be7
JA
1317 if (!__f) {
1318 log_err("fio: smalloc OOM\n");
1319 assert(0);
1320 }
22a57ba8 1321 __f->fd = -1;
33c48814 1322 fio_file_reset(td, __f);
0b9d69ec 1323
bc3456fa 1324 if (f->file_name) {
f17c4392 1325 __f->file_name = smalloc_strdup(f->file_name);
c48c0be7
JA
1326 if (!__f->file_name) {
1327 log_err("fio: smalloc OOM\n");
1328 assert(0);
1329 }
0b9d69ec 1330
bc3456fa
AC
1331 __f->filetype = f->filetype;
1332 }
b0fe421a
JA
1333
1334 td->files[i] = __f;
cade3ef4
JA
1335 }
1336}
f29b25a3
JA
1337
1338/*
1339 * Returns the index that matches the filename, or -1 if not there
1340 */
1341int get_fileno(struct thread_data *td, const char *fname)
1342{
1343 struct fio_file *f;
1344 unsigned int i;
1345
1346 for_each_file(td, f, i)
1347 if (!strcmp(f->file_name, fname))
1348 return i;
1349
1350 return -1;
1351}
1352
1353/*
1354 * For log usage, where we add/open/close files automatically
1355 */
1356void free_release_files(struct thread_data *td)
1357{
1358 close_files(td);
1359 td->files_index = 0;
1360 td->nr_normal_files = 0;
1361}
33c48814
JA
1362
1363void fio_file_reset(struct thread_data *td, struct fio_file *f)
1364{
1365 f->last_pos = f->file_offset;
1366 f->last_start = -1ULL;
1367 if (f->io_axmap)
1368 axmap_reset(f->io_axmap);
1369 if (td->o.random_generator == FIO_RAND_GEN_LFSR)
1370 lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
1371}