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