b356b447ddaeadffa01f4d551ea868621cff3220
[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                         f->num_maps = num_maps;
580                         continue;
581                 }
582                 if (!td->o.softrandommap) {
583                         log_err("fio: failed allocating random map. If running"
584                                 " a large number of jobs, try the 'norandommap'"
585                                 " option or set 'softrandommap'. Or give"
586                                 " a larger --alloc-size to fio.\n");
587                         return 1;
588                 }
589
590                 log_info("fio: file %s failed allocating random map. Running "
591                          "job without.\n", f->file_name);
592                 f->num_maps = 0;
593         }
594
595         return 0;
596 }
597
598 void close_files(struct thread_data *td)
599 {
600         struct fio_file *f;
601         unsigned int i;
602
603         for_each_file(td, f, i)
604                 td_io_close_file(td, f);
605 }
606
607 void close_and_free_files(struct thread_data *td)
608 {
609         struct fio_file *f;
610         unsigned int i;
611
612         dprint(FD_FILE, "close files\n");
613
614         for_each_file(td, f, i) {
615                 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
616                         dprint(FD_FILE, "free unlink %s\n", f->file_name);
617                         unlink(f->file_name);
618                 }
619
620                 td_io_close_file(td, f);
621
622                 sfree(f->file_name);
623                 f->file_name = NULL;
624
625                 if (f->file_map) {
626                         sfree(f->file_map);
627                         f->file_map = NULL;
628                 }
629                 sfree(f);
630         }
631
632         td->o.filename = NULL;
633         free(td->files);
634         td->files_index = 0;
635         td->files = NULL;
636         td->o.nr_files = 0;
637 }
638
639 static void get_file_type(struct fio_file *f)
640 {
641         struct stat sb;
642
643         if (!strcmp(f->file_name, "-"))
644                 f->filetype = FIO_TYPE_PIPE;
645         else
646                 f->filetype = FIO_TYPE_FILE;
647
648         if (!lstat(f->file_name, &sb)) {
649                 if (S_ISBLK(sb.st_mode))
650                         f->filetype = FIO_TYPE_BD;
651                 else if (S_ISCHR(sb.st_mode))
652                         f->filetype = FIO_TYPE_CHAR;
653                 else if (S_ISFIFO(sb.st_mode))
654                         f->filetype = FIO_TYPE_PIPE;
655         }
656 }
657
658 int add_file(struct thread_data *td, const char *fname)
659 {
660         int cur_files = td->files_index;
661         char file_name[PATH_MAX];
662         struct fio_file *f;
663         int len = 0;
664
665         dprint(FD_FILE, "add file %s\n", fname);
666
667         f = smalloc(sizeof(*f));
668         f->fd = -1;
669
670         dprint(FD_FILE, "resize file array to %d files\n", cur_files + 1);
671
672         td->files = realloc(td->files, (cur_files + 1) * sizeof(f));
673         td->files[cur_files] = f;
674
675         /*
676          * init function, io engine may not be loaded yet
677          */
678         if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
679                 f->real_file_size = -1ULL;
680
681         if (td->o.directory)
682                 len = sprintf(file_name, "%s/", td->o.directory);
683
684         sprintf(file_name + len, "%s", fname);
685         f->file_name = smalloc_strdup(file_name);
686
687         get_file_type(f);
688
689         switch (td->o.file_lock_mode) {
690         case FILE_LOCK_NONE:
691                 break;
692         case FILE_LOCK_READWRITE:
693                 f->lock = fio_mutex_rw_init();
694                 break;
695         case FILE_LOCK_EXCLUSIVE:
696                 f->lock = fio_mutex_init(1);
697                 break;
698         default:
699                 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
700                 assert(0);
701         }
702
703         td->files_index++;
704         if (f->filetype == FIO_TYPE_FILE)
705                 td->nr_normal_files++;
706
707         dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
708                                                         cur_files);
709
710         return cur_files;
711 }
712
713 void get_file(struct fio_file *f)
714 {
715         dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
716         assert(f->flags & FIO_FILE_OPEN);
717         f->references++;
718 }
719
720 int put_file(struct thread_data *td, struct fio_file *f)
721 {
722         int f_ret = 0, ret = 0;
723
724         dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
725
726         if (!(f->flags & FIO_FILE_OPEN))
727                 return 0;
728
729         assert(f->references);
730         if (--f->references)
731                 return 0;
732
733         if (should_fsync(td) && td->o.fsync_on_close)
734                 f_ret = fsync(f->fd);
735
736         if (td->io_ops->close_file)
737                 ret = td->io_ops->close_file(td, f);
738
739         if (!ret)
740                 ret = !f_ret;
741
742         td->nr_open_files--;
743         f->flags &= ~FIO_FILE_OPEN;
744         return ret;
745 }
746
747 void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
748 {
749         if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
750                 return;
751
752         if (f->lock_owner == td && f->lock_batch--)
753                 return;
754
755         if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
756                 if (ddir == DDIR_READ)
757                         fio_mutex_down_read(f->lock);
758                 else
759                         fio_mutex_down_write(f->lock);
760         } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
761                 fio_mutex_down(f->lock);
762
763         f->lock_owner = td;
764         f->lock_batch = td->o.lockfile_batch;
765         f->lock_ddir = ddir;
766 }
767
768 void unlock_file(struct thread_data *td, struct fio_file *f)
769 {
770         if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
771                 return;
772         if (f->lock_batch)
773                 return;
774
775         if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
776                 const int is_read = f->lock_ddir == DDIR_READ;
777                 int val = fio_mutex_getval(f->lock);
778
779                 if ((is_read && val == 1) || (!is_read && val == -1))
780                         f->lock_owner = NULL;
781
782                 if (is_read)
783                         fio_mutex_up_read(f->lock);
784                 else
785                         fio_mutex_up_write(f->lock);
786         } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
787                 int val = fio_mutex_getval(f->lock);
788
789                 if (val == 0)
790                         f->lock_owner = NULL;
791
792                 fio_mutex_up(f->lock);
793         }
794 }
795
796 void unlock_file_all(struct thread_data *td, struct fio_file *f)
797 {
798         if (f->lock_owner != td)
799                 return;
800
801         f->lock_batch = 0;
802         unlock_file(td, f);
803 }
804
805 static int recurse_dir(struct thread_data *td, const char *dirname)
806 {
807         struct dirent *dir;
808         int ret = 0;
809         DIR *D;
810
811         D = opendir(dirname);
812         if (!D) {
813                 char buf[FIO_VERROR_SIZE];
814
815                 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
816                 td_verror(td, errno, buf);
817                 return 1;
818         }
819
820         while ((dir = readdir(D)) != NULL) {
821                 char full_path[PATH_MAX];
822                 struct stat sb;
823
824                 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
825                         continue;
826
827                 sprintf(full_path, "%s/%s", dirname, dir->d_name);
828
829                 if (lstat(full_path, &sb) == -1) {
830                         if (errno != ENOENT) {
831                                 td_verror(td, errno, "stat");
832                                 return 1;
833                         }
834                 }
835
836                 if (S_ISREG(sb.st_mode)) {
837                         add_file(td, full_path);
838                         td->o.nr_files++;
839                         continue;
840                 }
841                 if (!S_ISDIR(sb.st_mode))
842                         continue;
843
844                 ret = recurse_dir(td, full_path);
845                 if (ret)
846                         break;
847         }
848
849         closedir(D);
850         return ret;
851 }
852
853 int add_dir_files(struct thread_data *td, const char *path)
854 {
855         int ret = recurse_dir(td, path);
856
857         if (!ret)
858                 log_info("fio: opendir added %d files\n", td->o.nr_files);
859
860         return ret;
861 }
862
863 void dup_files(struct thread_data *td, struct thread_data *org)
864 {
865         struct fio_file *f;
866         unsigned int i;
867
868         dprint(FD_FILE, "dup files: %d\n", org->files_index);
869
870         if (!org->files)
871                 return;
872
873         td->files = malloc(org->files_index * sizeof(f));
874
875         for_each_file(org, f, i) {
876                 struct fio_file *__f;
877
878                 __f = smalloc(sizeof(*__f));
879
880                 if (f->file_name)
881                         __f->file_name = smalloc_strdup(f->file_name);
882
883                 td->files[i] = __f;
884         }
885 }
886
887 /*
888  * Returns the index that matches the filename, or -1 if not there
889  */
890 int get_fileno(struct thread_data *td, const char *fname)
891 {
892         struct fio_file *f;
893         unsigned int i;
894
895         for_each_file(td, f, i)
896                 if (!strcmp(f->file_name, fname))
897                         return i;
898
899         return -1;
900 }
901
902 /*
903  * For log usage, where we add/open/close files automatically
904  */
905 void free_release_files(struct thread_data *td)
906 {
907         close_files(td);
908         td->files_index = 0;
909         td->nr_normal_files = 0;
910 }