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