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