Be more careful about catching fsync() errors
[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 <sys/stat.h>
7 #include <sys/mman.h>
8 #include <sys/types.h>
9
10 #include "fio.h"
11 #include "smalloc.h"
12 #include "filehash.h"
13
14 static int root_warn;
15
16 static int extend_file(struct thread_data *td, struct fio_file *f)
17 {
18         int r, new_layout = 0, unlink_file = 0, flags;
19         unsigned long long left;
20         unsigned int bs;
21         char *b;
22
23         if (read_only) {
24                 log_err("fio: refusing extend of file due to read-only\n");
25                 return 0;
26         }
27
28         /*
29          * check if we need to lay the file out complete again. fio
30          * does that for operations involving reads, or for writes
31          * where overwrite is set
32          */
33         if (td_read(td) || (td_write(td) && td->o.overwrite))
34                 new_layout = 1;
35         if (td_write(td) && !td->o.overwrite)
36                 unlink_file = 1;
37
38         if (unlink_file || new_layout) {
39                 if ((unlink(f->file_name) < 0) && (errno != ENOENT)) {
40                         td_verror(td, errno, "unlink");
41                         return 1;
42                 }
43         }
44
45         flags = O_WRONLY | O_CREAT;
46         if (new_layout)
47                 flags |= O_TRUNC;
48
49         dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
50         f->fd = open(f->file_name, flags, 0644);
51         if (f->fd < 0) {
52                 td_verror(td, errno, "open");
53                 return 1;
54         }
55
56         if (!new_layout)
57                 goto done;
58
59         dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
60                                                         f->real_file_size);
61         if (ftruncate(f->fd, f->real_file_size) == -1) {
62                 td_verror(td, errno, "ftruncate");
63                 goto err;
64         }
65
66         dprint(FD_FILE, "fallocate file %s, size %llu\n", f->file_name,
67                                                         f->real_file_size);
68         if (posix_fallocate(f->fd, 0, f->real_file_size) < 0) {
69                 td_verror(td, errno, "posix_fallocate");
70                 goto err;
71         }
72
73         b = malloc(td->o.max_bs[DDIR_WRITE]);
74         memset(b, 0, td->o.max_bs[DDIR_WRITE]);
75
76         left = f->real_file_size;
77         while (left && !td->terminate) {
78                 bs = td->o.max_bs[DDIR_WRITE];
79                 if (bs > left)
80                         bs = left;
81
82                 r = write(f->fd, b, bs);
83
84                 if (r == (int) bs) {
85                         left -= bs;
86                         continue;
87                 } else {
88                         if (r < 0)
89                                 td_verror(td, errno, "write");
90                         else
91                                 td_verror(td, EIO, "write");
92
93                         break;
94                 }
95         }
96
97         if (td->terminate)
98                 unlink(f->file_name);
99         else if (td->o.create_fsync) {
100                 if (fsync(f->fd) < 0) {
101                         td_verror(td, errno, "fsync");
102                         goto err;
103                 }
104         }
105
106         free(b);
107 done:
108         close(f->fd);
109         f->fd = -1;
110         return 0;
111 err:
112         close(f->fd);
113         f->fd = -1;
114         return 1;
115 }
116
117 static unsigned long long get_rand_file_size(struct thread_data *td)
118 {
119         unsigned long long ret;
120         long r;
121
122         r = os_random_long(&td->file_size_state);
123         ret = td->o.file_size_low + (unsigned long long) ((double) (td->o.file_size_high - td->o.file_size_low) * (r / (RAND_MAX + 1.0)));
124         ret -= (ret % td->o.rw_min_bs);
125         return ret;
126 }
127
128 static int file_size(struct thread_data *td, struct fio_file *f)
129 {
130         struct stat st;
131
132         if (fstat(f->fd, &st) == -1) {
133                 td_verror(td, errno, "fstat");
134                 return 1;
135         }
136
137         f->real_file_size = st.st_size;
138         return 0;
139 }
140
141 static int bdev_size(struct thread_data *td, struct fio_file *f)
142 {
143         unsigned long long bytes;
144         int r;
145
146         r = blockdev_size(f->fd, &bytes);
147         if (r) {
148                 td_verror(td, r, "blockdev_size");
149                 return 1;
150         }
151
152         f->real_file_size = bytes;
153         return 0;
154 }
155
156 static int get_file_size(struct thread_data *td, struct fio_file *f)
157 {
158         int ret = 0;
159
160         if (f->flags & FIO_SIZE_KNOWN)
161                 return 0;
162
163         if (f->filetype == FIO_TYPE_FILE)
164                 ret = file_size(td, f);
165         else if (f->filetype == FIO_TYPE_BD)
166                 ret = bdev_size(td, f);
167         else
168                 f->real_file_size = -1;
169
170         if (ret)
171                 return ret;
172
173         if (f->file_offset > f->real_file_size) {
174                 log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name, f->file_offset, f->real_file_size);
175                 return 1;
176         }
177
178         f->flags |= FIO_SIZE_KNOWN;
179         return 0;
180 }
181
182 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
183 {
184         int ret = 0;
185
186         dprint(FD_IO, "invalidate cache (%d)\n", td->o.odirect);
187
188         if (td->o.odirect)
189                 return 0;
190
191         /*
192          * FIXME: add blockdev flushing too
193          */
194         if (f->mmap)
195                 ret = madvise(f->mmap, f->io_size, MADV_DONTNEED);
196         else if (f->filetype == FIO_TYPE_FILE)
197                 ret = fadvise(f->fd, f->file_offset, f->io_size, POSIX_FADV_DONTNEED);
198         else if (f->filetype == FIO_TYPE_BD) {
199                 ret = blockdev_invalidate_cache(f->fd);
200                 if (ret < 0 && errno == EACCES && geteuid()) {
201                         if (!root_warn) {
202                                 log_err("fio: only root may flush block devices. Cache flush bypassed!\n");
203                                 root_warn = 1;
204                         }
205                         ret = 0;
206                 }
207         } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
208                 ret = 0;
209
210         if (ret < 0) {
211                 td_verror(td, errno, "invalidate_cache");
212                 return 1;
213         }
214
215         return ret;
216 }
217
218 int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
219 {
220         int ret = 0;
221
222         dprint(FD_FILE, "fd close %s\n", f->file_name);
223
224         remove_file_hash(f);
225
226         if (close(f->fd) < 0)
227                 ret = errno;
228
229         f->fd = -1;
230         return ret;
231 }
232
233 static int file_lookup_open(struct fio_file *f, int flags)
234 {
235         struct fio_file *__f;
236         int from_hash;
237
238         __f = lookup_file_hash(f->file_name);
239         if (__f) {
240                 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
241                 /*
242                  * racy, need the __f->lock locked
243                  */
244                 f->lock = __f->lock;
245                 f->lock_owner = __f->lock_owner;
246                 f->lock_batch = __f->lock_batch;
247                 f->lock_ddir = __f->lock_ddir;
248                 f->fd = dup(__f->fd);
249                 f->references++;
250                 from_hash = 1;
251         } else {
252                 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
253                 f->fd = open(f->file_name, flags, 0600);
254                 from_hash = 0;
255         }
256
257         return from_hash;
258 }
259
260 int generic_open_file(struct thread_data *td, struct fio_file *f)
261 {
262         int is_std = 0;
263         int flags = 0;
264         int from_hash = 0;
265
266         dprint(FD_FILE, "fd open %s\n", f->file_name);
267
268         if (!strcmp(f->file_name, "-")) {
269                 if (td_rw(td)) {
270                         log_err("fio: can't read/write to stdin/out\n");
271                         return 1;
272                 }
273                 is_std = 1;
274
275                 /*
276                  * move output logging to stderr, if we are writing to stdout
277                  */
278                 if (td_write(td))
279                         f_out = stderr;
280         }
281
282         if (td->o.odirect)
283                 flags |= OS_O_DIRECT;
284         if (td->o.sync_io)
285                 flags |= O_SYNC;
286         if (f->filetype != FIO_TYPE_FILE)
287                 flags |= O_NOATIME;
288
289 open_again:
290         if (td_write(td)) {
291                 assert(!read_only);
292
293                 flags |= O_RDWR;
294
295                 if (f->filetype == FIO_TYPE_FILE)
296                         flags |= O_CREAT;
297
298                 if (is_std)
299                         f->fd = dup(STDOUT_FILENO);
300                 else
301                         from_hash = file_lookup_open(f, flags);
302         } else {
303                 if (f->filetype == FIO_TYPE_CHAR && !read_only)
304                         flags |= O_RDWR;
305                 else
306                         flags |= O_RDONLY;
307
308                 if (is_std)
309                         f->fd = dup(STDIN_FILENO);
310                 else
311                         from_hash = file_lookup_open(f, flags);
312         }
313
314         if (f->fd == -1) {
315                 char buf[FIO_VERROR_SIZE];
316                 int __e = errno;
317
318                 if (errno == EPERM && (flags & O_NOATIME)) {
319                         flags &= ~O_NOATIME;
320                         goto open_again;
321                 }
322
323                 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
324
325                 td_verror(td, __e, buf);
326         }
327
328         if (get_file_size(td, f))
329                 goto err;
330
331         if (!from_hash && f->fd != -1) {
332                 if (add_file_hash(f)) {
333                         int ret;
334
335                         /*
336                          * OK to ignore, we haven't done anything with it
337                          */
338                         ret = generic_close_file(td, f);
339                         goto open_again;
340                 }
341         }
342
343         return 0;
344 err:
345         close(f->fd);
346         return 1;
347 }
348
349 int open_files(struct thread_data *td)
350 {
351         struct fio_file *f;
352         unsigned int i;
353         int err = 0;
354
355         dprint(FD_FILE, "open files\n");
356
357         for_each_file(td, f, i) {
358                 err = td_io_open_file(td, f);
359                 if (err) {
360                         if (td->error == EMFILE) {
361                                 log_err("fio: limited open files to: %d\n", td->nr_open_files);
362                                 td->o.open_files = td->nr_open_files;
363                                 err = 0;
364                                 clear_error(td);
365                         }
366                         break;
367                 }
368
369                 if (td->o.open_files == td->nr_open_files)
370                         break;
371         }
372
373         if (!err)
374                 return 0;
375
376         for_each_file(td, f, i)
377                 td_io_close_file(td, f);
378
379         return err;
380 }
381
382 /*
383  * open/close all files, so that ->real_file_size gets set
384  */
385 static int get_file_sizes(struct thread_data *td)
386 {
387         struct fio_file *f;
388         unsigned int i;
389         int err = 0;
390
391         for_each_file(td, f, i) {
392                 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i, f->file_name);
393
394                 if (td->io_ops->open_file(td, f)) {
395                         if (td->error != ENOENT) {
396                                 log_err("%s\n", td->verror);
397                                 err = 1;
398                         }
399                         clear_error(td);
400                 } else {
401                         if (td->io_ops->close_file)
402                                 td->io_ops->close_file(td, f);
403                 }
404
405                 if (f->real_file_size == -1ULL && td->o.size)
406                         f->real_file_size = td->o.size / td->o.nr_files;
407         }
408
409         return err;
410 }
411
412 /*
413  * Open the files and setup files sizes, creating files if necessary.
414  */
415 int setup_files(struct thread_data *td)
416 {
417         unsigned long long total_size, extend_size;
418         struct fio_file *f;
419         unsigned int i;
420         int err = 0, need_extend;
421
422         dprint(FD_FILE, "setup files\n");
423
424         /*
425          * if ioengine defines a setup() method, it's responsible for
426          * opening the files and setting f->real_file_size to indicate
427          * the valid range for that file.
428          */
429         if (td->io_ops->setup)
430                 err = td->io_ops->setup(td);
431         else
432                 err = get_file_sizes(td);
433
434         if (err)
435                 return err;
436
437         /*
438          * check sizes. if the files/devices do not exist and the size
439          * isn't passed to fio, abort.
440          */
441         total_size = 0;
442         for_each_file(td, f, i) {
443                 if (f->real_file_size == -1ULL)
444                         total_size = -1ULL;
445                 else
446                         total_size += f->real_file_size;
447         }
448
449         /*
450          * device/file sizes are zero and no size given, punt
451          */
452         if ((!total_size || total_size == -1ULL) && !td->o.size &&
453             !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
454                 log_err("%s: you need to specify size=\n", td->o.name);
455                 td_verror(td, EINVAL, "total_file_size");
456                 return 1;
457         }
458
459         /*
460          * now file sizes are known, so we can set ->io_size. if size= is
461          * not given, ->io_size is just equal to ->real_file_size. if size
462          * is given, ->io_size is size / nr_files.
463          */
464         extend_size = total_size = 0;
465         need_extend = 0;
466         for_each_file(td, f, i) {
467                 f->file_offset = td->o.start_offset;
468
469                 if (!td->o.file_size_low) {
470                         /*
471                          * no file size range given, file size is equal to
472                          * total size divided by number of files. if that is
473                          * zero, set it to the real file size.
474                          */
475                         f->io_size = td->o.size / td->o.nr_files;
476                         if (!f->io_size)
477                                 f->io_size = f->real_file_size - f->file_offset;
478                 } else if (f->real_file_size < td->o.file_size_low ||
479                            f->real_file_size > td->o.file_size_high) {
480                         if (f->file_offset > td->o.file_size_low) 
481                                 goto err_offset;
482                         /*
483                          * file size given. if it's fixed, use that. if it's a
484                          * range, generate a random size in-between.
485                          */
486                         if (td->o.file_size_low == td->o.file_size_high)
487                                 f->io_size = td->o.file_size_low - f->file_offset;
488                         else
489                                 f->io_size = get_rand_file_size(td) - f->file_offset;
490                 } else
491                         f->io_size = f->real_file_size - f->file_offset;
492
493                 if (f->io_size == -1ULL)
494                         total_size = -1ULL;
495                 else
496                         total_size += f->io_size;
497
498                 if (f->filetype == FIO_TYPE_FILE &&
499                     (f->io_size + f->file_offset) > f->real_file_size &&
500                     !(td->io_ops->flags & FIO_DISKLESSIO)) {
501                         need_extend++;
502                         extend_size += (f->io_size + f->file_offset);
503                         f->flags |= FIO_FILE_EXTEND;
504                 }       
505         }
506
507         if (!td->o.size || td->o.size > total_size)
508                 td->o.size = total_size;
509
510         /*
511          * See if we need to extend some files
512          */
513         if (need_extend) {
514                 temp_stall_ts = 1;
515                 log_info("%s: Laying out IO file(s) (%u file(s) / %LuMiB)\n",
516                         td->o.name, need_extend, extend_size >> 20);
517
518                 for_each_file(td, f, i) {
519                         if (!(f->flags & FIO_FILE_EXTEND))
520                                 continue;
521
522                         assert(f->filetype == FIO_TYPE_FILE);
523                         f->flags &= ~FIO_FILE_EXTEND;
524                         f->real_file_size = (f->io_size + f->file_offset);
525                         err = extend_file(td, f);
526                         if (err)
527                                 break;
528                 }
529                 temp_stall_ts = 0;
530         }
531
532         if (err)
533                 return err;
534
535         if (!td->o.zone_size)
536                 td->o.zone_size = td->o.size;
537
538         /*
539          * iolog already set the total io size, if we read back
540          * stored entries.
541          */
542         if (!td->o.read_iolog_file)
543                 td->total_io_size = td->o.size * td->o.loops;
544         return 0;
545 err_offset:
546         log_err("%s: you need to specify valid offset=\n", td->o.name);
547         return 1;
548 }
549
550 int init_random_map(struct thread_data *td)
551 {
552         unsigned long long blocks, num_maps;
553         struct fio_file *f;
554         unsigned int i;
555
556         if (td->o.norandommap || !td_random(td))
557                 return 0;
558
559         for_each_file(td, f, i) {
560                 blocks = (f->real_file_size + td->o.rw_min_bs - 1) / (unsigned long long) td->o.rw_min_bs;
561                 num_maps = (blocks + BLOCKS_PER_MAP-1)/ (unsigned long long) BLOCKS_PER_MAP;
562                 f->file_map = smalloc(num_maps * sizeof(long));
563                 if (!f->file_map) {
564                         log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
565                         return 1;
566                 }
567                 f->num_maps = num_maps;
568         }
569
570         return 0;
571 }
572
573 void close_files(struct thread_data *td)
574 {
575         struct fio_file *f;
576         unsigned int i;
577
578         for_each_file(td, f, i)
579                 td_io_close_file(td, f);
580 }
581
582 void close_and_free_files(struct thread_data *td)
583 {
584         struct fio_file *f;
585         unsigned int i;
586
587         dprint(FD_FILE, "close files\n");
588
589         for_each_file(td, f, i) {
590                 if (td->o.unlink && f->filetype == FIO_TYPE_FILE)
591                         unlink(f->file_name);
592
593                 td_io_close_file(td, f);
594
595                 sfree(f->file_name);
596                 f->file_name = NULL;
597
598                 if (f->file_map) {
599                         sfree(f->file_map);
600                         f->file_map = NULL;
601                 }
602                 sfree(f);
603         }
604
605         td->o.filename = NULL;
606         free(td->files);
607         td->files_index = 0;
608         td->files = NULL;
609         td->o.nr_files = 0;
610 }
611
612 static void get_file_type(struct fio_file *f)
613 {
614         struct stat sb;
615
616         if (!strcmp(f->file_name, "-"))
617                 f->filetype = FIO_TYPE_PIPE;
618         else
619                 f->filetype = FIO_TYPE_FILE;
620
621         if (!lstat(f->file_name, &sb)) {
622                 if (S_ISBLK(sb.st_mode))
623                         f->filetype = FIO_TYPE_BD;
624                 else if (S_ISCHR(sb.st_mode))
625                         f->filetype = FIO_TYPE_CHAR;
626                 else if (S_ISFIFO(sb.st_mode))
627                         f->filetype = FIO_TYPE_PIPE;
628         }
629 }
630
631 int add_file(struct thread_data *td, const char *fname)
632 {
633         int cur_files = td->files_index;
634         char file_name[PATH_MAX];
635         struct fio_file *f;
636         int len = 0;
637
638         dprint(FD_FILE, "add file %s\n", fname);
639
640         f = smalloc(sizeof(*f));
641         f->fd = -1;
642
643         dprint(FD_FILE, "resize file array to %d files\n", cur_files + 1);
644
645         td->files = realloc(td->files, (cur_files + 1) * sizeof(f));
646         td->files[cur_files] = f;
647
648         /*
649          * init function, io engine may not be loaded yet
650          */
651         if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
652                 f->real_file_size = -1ULL;
653
654         if (td->o.directory)
655                 len = sprintf(file_name, "%s/", td->o.directory);
656
657         sprintf(file_name + len, "%s", fname);
658         f->file_name = smalloc_strdup(file_name);
659
660         get_file_type(f);
661
662         switch (td->o.file_lock_mode) {
663         case FILE_LOCK_NONE:
664                 break;
665         case FILE_LOCK_READWRITE:
666                 f->lock = fio_mutex_rw_init();
667                 break;
668         case FILE_LOCK_EXCLUSIVE:
669                 f->lock = fio_mutex_init(1);
670                 break;
671         default:
672                 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
673                 assert(0);
674         }
675
676         td->files_index++;
677         if (f->filetype == FIO_TYPE_FILE)
678                 td->nr_normal_files++;
679
680         dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name, cur_files);
681
682         return cur_files;
683 }
684
685 void get_file(struct fio_file *f)
686 {
687         dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
688         assert(f->flags & FIO_FILE_OPEN);
689         f->references++;
690 }
691
692 int put_file(struct thread_data *td, struct fio_file *f)
693 {
694         int f_ret = 0, ret = 0;
695
696         dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
697
698         if (!(f->flags & FIO_FILE_OPEN))
699                 return 0;
700
701         assert(f->references);
702         if (--f->references)
703                 return 0;
704
705         if (should_fsync(td) && td->o.fsync_on_close)
706                 f_ret = fsync(f->fd);
707
708         if (td->io_ops->close_file)
709                 ret = td->io_ops->close_file(td, f);
710
711         if (!ret)
712                 ret = !f_ret;
713
714         td->nr_open_files--;
715         f->flags &= ~FIO_FILE_OPEN;
716         return ret;
717 }
718
719 void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
720 {
721         if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
722                 return;
723
724         if (f->lock_owner == td && f->lock_batch--)
725                 return;
726
727         if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
728                 if (ddir == DDIR_READ)
729                         fio_mutex_down_read(f->lock);
730                 else
731                         fio_mutex_down_write(f->lock);
732         } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
733                 fio_mutex_down(f->lock);
734
735         f->lock_owner = td;
736         f->lock_batch = td->o.lockfile_batch;
737         f->lock_ddir = ddir;
738 }
739
740 void unlock_file(struct thread_data *td, struct fio_file *f)
741 {
742         if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
743                 return;
744         if (f->lock_batch)
745                 return;
746
747         if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
748                 const int is_read = f->lock_ddir == DDIR_READ;
749                 int val = fio_mutex_getval(f->lock);
750
751                 if ((is_read && val == 1) || (!is_read && val == -1))
752                         f->lock_owner = NULL;
753
754                 if (is_read)
755                         fio_mutex_up_read(f->lock);
756                 else
757                         fio_mutex_up_write(f->lock);
758         } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
759                 int val = fio_mutex_getval(f->lock);
760
761                 if (val == 0)
762                         f->lock_owner = NULL;
763
764                 fio_mutex_up(f->lock);
765         }
766 }
767
768 void unlock_file_all(struct thread_data *td, struct fio_file *f)
769 {
770         if (f->lock_owner != td)
771                 return;
772
773         f->lock_batch = 0;
774         unlock_file(td, f);
775 }
776
777 static int recurse_dir(struct thread_data *td, const char *dirname)
778 {
779         struct dirent *dir;
780         int ret = 0;
781         DIR *D;
782
783         D = opendir(dirname);
784         if (!D) {
785                 char buf[FIO_VERROR_SIZE];
786
787                 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
788                 td_verror(td, errno, buf);
789                 return 1;
790         }
791
792         while ((dir = readdir(D)) != NULL) {
793                 char full_path[PATH_MAX];
794                 struct stat sb;
795
796                 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
797                         continue;
798
799                 sprintf(full_path, "%s/%s", dirname, dir->d_name);
800
801                 if (lstat(full_path, &sb) == -1) {
802                         if (errno != ENOENT) {
803                                 td_verror(td, errno, "stat");
804                                 return 1;
805                         }
806                 }
807
808                 if (S_ISREG(sb.st_mode)) {
809                         add_file(td, full_path);
810                         td->o.nr_files++;
811                         continue;
812                 }
813                 if (!S_ISDIR(sb.st_mode))
814                         continue;
815
816                 if ((ret = recurse_dir(td, full_path)) != 0)
817                         break;
818         }
819
820         closedir(D);
821         return ret;
822 }
823
824 int add_dir_files(struct thread_data *td, const char *path)
825 {
826         int ret = recurse_dir(td, path);
827
828         if (!ret)
829                 log_info("fio: opendir added %d files\n", td->o.nr_files);
830
831         return ret;
832 }
833
834 void dup_files(struct thread_data *td, struct thread_data *org)
835 {
836         struct fio_file *f;
837         unsigned int i;
838
839         dprint(FD_FILE, "dup files: %d\n", org->files_index);
840
841         if (!org->files)
842                 return;
843
844         td->files = malloc(org->files_index * sizeof(f));
845
846         for_each_file(org, f, i) {
847                 struct fio_file *__f;
848
849                 __f = smalloc(sizeof(*__f));
850
851                 if (f->file_name)
852                         __f->file_name = smalloc_strdup(f->file_name);
853
854                 td->files[i] = __f;
855         }
856 }
857
858 /*
859  * Returns the index that matches the filename, or -1 if not there
860  */
861 int get_fileno(struct thread_data *td, const char *fname)
862 {
863         struct fio_file *f;
864         unsigned int i;
865
866         for_each_file(td, f, i)
867                 if (!strcmp(f->file_name, fname))
868                         return i;
869
870         return -1;
871 }
872
873 /*
874  * For log usage, where we add/open/close files automatically
875  */
876 void free_release_files(struct thread_data *td)
877 {
878         close_files(td);
879         td->files_index = 0;
880         td->nr_normal_files = 0;
881 }