16 #include "lib/axmap.h"
18 #ifdef FIO_HAVE_LINUX_FALLOCATE
19 #include <linux/falloc.h>
24 static inline void clear_error(struct thread_data *td)
31 * Leaves f->fd open on success, caller must close
33 static int extend_file(struct thread_data *td, struct fio_file *f)
35 int r, new_layout = 0, unlink_file = 0, flags;
36 unsigned long long left;
41 log_err("fio: refusing extend of file due to read-only\n");
46 * check if we need to lay the file out complete again. fio
47 * does that for operations involving reads, or for writes
48 * where overwrite is set
50 if (td_read(td) || (td_write(td) && td->o.overwrite) ||
51 (td_write(td) && td->io_ops->flags & FIO_NOEXTEND))
53 if (td_write(td) && !td->o.overwrite)
56 if (unlink_file || new_layout) {
57 dprint(FD_FILE, "layout unlink %s\n", f->file_name);
58 if ((unlink(f->file_name) < 0) && (errno != ENOENT)) {
59 td_verror(td, errno, "unlink");
64 flags = O_WRONLY | O_CREAT;
68 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
69 f->fd = open(f->file_name, flags, 0644);
71 td_verror(td, errno, "open");
75 #ifdef FIO_HAVE_FALLOCATE
76 if (!td->o.fill_device) {
77 switch (td->o.fallocate_mode) {
78 case FIO_FALLOCATE_NONE:
80 case FIO_FALLOCATE_POSIX:
81 dprint(FD_FILE, "posix_fallocate file %s size %llu\n",
82 f->file_name, f->real_file_size);
84 r = posix_fallocate(f->fd, 0, f->real_file_size);
86 log_err("fio: posix_fallocate fails: %s\n",
90 #ifdef FIO_HAVE_LINUX_FALLOCATE
91 case FIO_FALLOCATE_KEEP_SIZE:
93 "fallocate(FALLOC_FL_KEEP_SIZE) "
94 "file %s size %llu\n",
95 f->file_name, f->real_file_size);
97 r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0,
100 td_verror(td, errno, "fallocate");
103 #endif /* FIO_HAVE_LINUX_FALLOCATE */
105 log_err("fio: unknown fallocate mode: %d\n",
106 td->o.fallocate_mode);
110 #endif /* FIO_HAVE_FALLOCATE */
116 * The size will be -1ULL when fill_device is used, so don't truncate
117 * or fallocate this file, just write it
119 if (!td->o.fill_device) {
120 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
122 if (ftruncate(f->fd, f->real_file_size) == -1) {
123 td_verror(td, errno, "ftruncate");
128 b = malloc(td->o.max_bs[DDIR_WRITE]);
129 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
131 left = f->real_file_size;
132 while (left && !td->terminate) {
133 bs = td->o.max_bs[DDIR_WRITE];
137 r = write(f->fd, b, bs);
147 if (td->o.fill_device)
149 log_info("fio: ENOSPC on laying out "
153 td_verror(td, errno, "write");
155 td_verror(td, EIO, "write");
162 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
163 unlink(f->file_name);
164 } else if (td->o.create_fsync) {
165 if (fsync(f->fd) < 0) {
166 td_verror(td, errno, "fsync");
170 if (td->o.fill_device && !td_write(td)) {
171 fio_file_clear_size_known(f);
172 if (td_io_get_file_size(td, f))
174 if (f->io_size > f->real_file_size)
175 f->io_size = f->real_file_size;
187 static int pre_read_file(struct thread_data *td, struct fio_file *f)
189 int r, did_open = 0, old_runstate;
190 unsigned long long left;
194 if (td->io_ops->flags & FIO_PIPEIO)
197 if (!fio_file_open(f)) {
198 if (td->io_ops->open_file(td, f)) {
199 log_err("fio: cannot pre-read, failed to open file\n");
205 old_runstate = td->runstate;
206 td_set_runstate(td, TD_PRE_READING);
208 bs = td->o.max_bs[DDIR_READ];
212 lseek(f->fd, f->file_offset, SEEK_SET);
215 while (left && !td->terminate) {
219 r = read(f->fd, b, bs);
225 td_verror(td, EIO, "pre_read");
230 td_set_runstate(td, old_runstate);
233 td->io_ops->close_file(td, f);
238 static unsigned long long get_rand_file_size(struct thread_data *td)
240 unsigned long long ret, sized;
243 if (td->o.use_os_rand) {
244 r = os_random_long(&td->file_size_state);
245 sized = td->o.file_size_high - td->o.file_size_low;
246 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
248 r = __rand(&td->__file_size_state);
249 sized = td->o.file_size_high - td->o.file_size_low;
250 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0)));
253 ret += td->o.file_size_low;
254 ret -= (ret % td->o.rw_min_bs);
258 static int file_size(struct thread_data *td, struct fio_file *f)
262 if (stat(f->file_name, &st) == -1) {
263 td_verror(td, errno, "fstat");
267 f->real_file_size = st.st_size;
271 static int bdev_size(struct thread_data *td, struct fio_file *f)
273 unsigned long long bytes = 0;
276 if (td->io_ops->open_file(td, f)) {
277 log_err("fio: failed opening blockdev %s for size check\n",
282 r = blockdev_size(f, &bytes);
284 td_verror(td, r, "blockdev_size");
289 log_err("%s: zero sized block device?\n", f->file_name);
293 f->real_file_size = bytes;
294 td->io_ops->close_file(td, f);
297 td->io_ops->close_file(td, f);
301 static int char_size(struct thread_data *td, struct fio_file *f)
303 #ifdef FIO_HAVE_CHARDEV_SIZE
304 unsigned long long bytes = 0;
307 if (td->io_ops->open_file(td, f)) {
308 log_err("fio: failed opening blockdev %s for size check\n",
313 r = chardev_size(f, &bytes);
315 td_verror(td, r, "chardev_size");
320 log_err("%s: zero sized char device?\n", f->file_name);
324 f->real_file_size = bytes;
325 td->io_ops->close_file(td, f);
328 td->io_ops->close_file(td, f);
331 f->real_file_size = -1ULL;
336 static int get_file_size(struct thread_data *td, struct fio_file *f)
340 if (fio_file_size_known(f))
343 if (f->filetype == FIO_TYPE_FILE)
344 ret = file_size(td, f);
345 else if (f->filetype == FIO_TYPE_BD)
346 ret = bdev_size(td, f);
347 else if (f->filetype == FIO_TYPE_CHAR)
348 ret = char_size(td, f);
350 f->real_file_size = -1;
355 if (f->file_offset > f->real_file_size) {
356 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
357 f->file_offset, f->real_file_size);
361 fio_file_set_size_known(f);
365 static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
366 unsigned long long off,
367 unsigned long long len)
374 off = f->file_offset;
376 if (len == -1ULL || off == -1ULL)
379 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
383 * FIXME: add blockdev flushing too
386 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
388 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
390 } else if (f->filetype == FIO_TYPE_FILE) {
391 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
392 } else if (f->filetype == FIO_TYPE_BD) {
393 ret = blockdev_invalidate_cache(f);
394 if (ret < 0 && errno == EACCES && geteuid()) {
396 log_err("fio: only root may flush block "
397 "devices. Cache flush bypassed!\n");
402 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
406 td_verror(td, errno, "invalidate_cache");
408 } else if (ret > 0) {
409 td_verror(td, ret, "invalidate_cache");
417 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
419 if (!fio_file_open(f))
422 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
425 int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
429 dprint(FD_FILE, "fd close %s\n", f->file_name);
433 if (close(f->fd) < 0)
440 int file_lookup_open(struct fio_file *f, int flags)
442 struct fio_file *__f;
445 __f = lookup_file_hash(f->file_name);
447 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
449 * racy, need the __f->lock locked
452 f->lock_owner = __f->lock_owner;
453 f->lock_batch = __f->lock_batch;
454 f->lock_ddir = __f->lock_ddir;
457 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
461 f->fd = open(f->file_name, flags, 0600);
465 int generic_open_file(struct thread_data *td, struct fio_file *f)
471 dprint(FD_FILE, "fd open %s\n", f->file_name);
473 if (td_trim(td) && f->filetype != FIO_TYPE_BD) {
474 log_err("fio: trim only applies to block device\n");
478 if (!strcmp(f->file_name, "-")) {
480 log_err("fio: can't read/write to stdin/out\n");
486 * move output logging to stderr, if we are writing to stdout
495 flags |= OS_O_DIRECT;
498 if (td->o.create_on_open)
501 if (f->filetype != FIO_TYPE_FILE)
502 flags |= FIO_O_NOATIME;
509 if (f->filetype == FIO_TYPE_FILE)
513 f->fd = dup(STDOUT_FILENO);
515 from_hash = file_lookup_open(f, flags);
516 } else if (td_read(td)) {
517 if (f->filetype == FIO_TYPE_CHAR && !read_only)
523 f->fd = dup(STDIN_FILENO);
525 from_hash = file_lookup_open(f, flags);
528 from_hash = file_lookup_open(f, flags);
532 char buf[FIO_VERROR_SIZE];
535 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
536 flags &= ~FIO_O_NOATIME;
540 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
542 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
543 log_err("fio: looks like your file system does not " \
544 "support direct=1/buffered=0\n");
547 td_verror(td, __e, buf);
550 if (!from_hash && f->fd != -1) {
551 if (add_file_hash(f)) {
555 * OK to ignore, we haven't done anything with it
557 ret = generic_close_file(td, f);
565 int generic_get_file_size(struct thread_data *td, struct fio_file *f)
567 return get_file_size(td, f);
571 * open/close all files, so that ->real_file_size gets set
573 static int get_file_sizes(struct thread_data *td)
579 for_each_file(td, f, i) {
580 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
583 if (td_io_get_file_size(td, f)) {
584 if (td->error != ENOENT) {
585 log_err("%s\n", td->verror);
591 if (f->real_file_size == -1ULL && td->o.size)
592 f->real_file_size = td->o.size / td->o.nr_files;
599 struct flist_head list;
606 * Get free number of bytes for each file on each unique mount.
608 static unsigned long long get_fs_free_counts(struct thread_data *td)
610 struct flist_head *n, *tmp;
611 unsigned long long ret = 0;
612 struct fio_mount *fm;
617 for_each_file(td, f, i) {
621 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
622 if (f->real_file_size != -1ULL)
623 ret += f->real_file_size;
625 } else if (f->filetype != FIO_TYPE_FILE)
628 strcpy(buf, f->file_name);
630 if (stat(buf, &sb) < 0) {
634 if (stat(buf, &sb) < 0)
639 flist_for_each(n, &list) {
640 fm = flist_entry(n, struct fio_mount, list);
641 if (fm->key == sb.st_dev)
650 fm = malloc(sizeof(*fm));
651 strcpy(fm->__base, buf);
652 fm->base = basename(fm->__base);
654 flist_add(&fm->list, &list);
657 flist_for_each_safe(n, tmp, &list) {
658 unsigned long long sz;
660 fm = flist_entry(n, struct fio_mount, list);
661 flist_del(&fm->list);
663 sz = get_fs_size(fm->base);
664 if (sz && sz != -1ULL)
673 unsigned long long get_start_offset(struct thread_data *td)
675 return td->o.start_offset +
676 (td->thread_number - 1) * td->o.offset_increment;
680 * Open the files and setup files sizes, creating files if necessary.
682 int setup_files(struct thread_data *td)
684 unsigned long long total_size, extend_size;
687 int err = 0, need_extend;
689 dprint(FD_FILE, "setup files\n");
691 if (td->o.read_iolog_file)
695 * if ioengine defines a setup() method, it's responsible for
696 * opening the files and setting f->real_file_size to indicate
697 * the valid range for that file.
699 if (td->io_ops->setup)
700 err = td->io_ops->setup(td);
702 err = get_file_sizes(td);
708 * check sizes. if the files/devices do not exist and the size
709 * isn't passed to fio, abort.
712 for_each_file(td, f, i) {
713 if (f->real_file_size == -1ULL)
716 total_size += f->real_file_size;
719 if (td->o.fill_device)
720 td->fill_device_size = get_fs_free_counts(td);
723 * device/file sizes are zero and no size given, punt
725 if ((!total_size || total_size == -1ULL) && !td->o.size &&
726 !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
727 log_err("%s: you need to specify size=\n", td->o.name);
728 td_verror(td, EINVAL, "total_file_size");
733 * now file sizes are known, so we can set ->io_size. if size= is
734 * not given, ->io_size is just equal to ->real_file_size. if size
735 * is given, ->io_size is size / nr_files.
737 extend_size = total_size = 0;
739 for_each_file(td, f, i) {
740 f->file_offset = get_start_offset(td);
742 if (!td->o.file_size_low) {
744 * no file size range given, file size is equal to
745 * total size divided by number of files. if that is
746 * zero, set it to the real file size.
748 f->io_size = td->o.size / td->o.nr_files;
750 f->io_size = f->real_file_size - f->file_offset;
751 } else if (f->real_file_size < td->o.file_size_low ||
752 f->real_file_size > td->o.file_size_high) {
753 if (f->file_offset > td->o.file_size_low)
756 * file size given. if it's fixed, use that. if it's a
757 * range, generate a random size in-between.
759 if (td->o.file_size_low == td->o.file_size_high) {
760 f->io_size = td->o.file_size_low
763 f->io_size = get_rand_file_size(td)
767 f->io_size = f->real_file_size - f->file_offset;
769 if (f->io_size == -1ULL)
772 if (td->o.size_percent)
773 f->io_size = (f->io_size * td->o.size_percent) / 100;
774 total_size += f->io_size;
777 if (f->filetype == FIO_TYPE_FILE &&
778 (f->io_size + f->file_offset) > f->real_file_size &&
779 !(td->io_ops->flags & FIO_DISKLESSIO)) {
780 if (!td->o.create_on_open) {
782 extend_size += (f->io_size + f->file_offset);
784 f->real_file_size = f->io_size + f->file_offset;
785 fio_file_set_extend(f);
789 if (!td->o.size || td->o.size > total_size)
790 td->o.size = total_size;
793 * See if we need to extend some files
797 if (output_format == FIO_OUTPUT_NORMAL)
798 log_info("%s: Laying out IO file(s) (%u file(s) /"
799 " %lluMB)\n", td->o.name, need_extend,
802 for_each_file(td, f, i) {
803 unsigned long long old_len = -1ULL, extend_len = -1ULL;
805 if (!fio_file_extend(f))
808 assert(f->filetype == FIO_TYPE_FILE);
809 fio_file_clear_extend(f);
810 if (!td->o.fill_device) {
811 old_len = f->real_file_size;
812 extend_len = f->io_size + f->file_offset -
815 f->real_file_size = (f->io_size + f->file_offset);
816 err = extend_file(td, f);
820 err = __file_invalidate_cache(td, f, old_len,
833 if (!td->o.zone_size)
834 td->o.zone_size = td->o.size;
837 * iolog already set the total io size, if we read back
840 if (!td->o.read_iolog_file)
841 td->total_io_size = td->o.size * td->o.loops;
844 if (td->o.create_only)
849 log_err("%s: you need to specify valid offset=\n", td->o.name);
853 int pre_read_files(struct thread_data *td)
858 dprint(FD_FILE, "pre_read files\n");
860 for_each_file(td, f, i) {
861 pre_read_file(td, f);
867 static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
869 unsigned int range_size, seed;
870 unsigned long nranges;
872 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
874 nranges = (f->real_file_size + range_size - 1) / range_size;
876 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
877 if (!td->o.rand_repeatable)
878 seed = td->rand_seeds[4];
880 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
881 zipf_init(&f->zipf, nranges, td->o.zipf_theta, seed);
883 pareto_init(&f->zipf, nranges, td->o.pareto_h, seed);
888 static int init_rand_distribution(struct thread_data *td)
894 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
897 state = td->runstate;
898 td_set_runstate(td, TD_SETTING_UP);
899 for_each_file(td, f, i)
900 __init_rand_distribution(td, f);
901 td_set_runstate(td, state);
906 int init_random_map(struct thread_data *td)
908 unsigned long long blocks;
912 if (init_rand_distribution(td))
917 for_each_file(td, f, i) {
918 blocks = (f->real_file_size + td->o.rw_min_bs - 1) /
919 (unsigned long long) td->o.rw_min_bs;
920 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
921 if (!lfsr_init(&f->lfsr, blocks))
923 } else if (!td->o.norandommap) {
924 f->io_axmap = axmap_new(blocks);
927 } else if (td->o.norandommap)
930 if (!td->o.softrandommap) {
931 log_err("fio: failed allocating random map. If running"
932 " a large number of jobs, try the 'norandommap'"
933 " option or set 'softrandommap'. Or give"
934 " a larger --alloc-size to fio.\n");
938 log_info("fio: file %s failed allocating random map. Running "
939 "job without.\n", f->file_name);
945 void close_files(struct thread_data *td)
950 for_each_file(td, f, i) {
951 if (fio_file_open(f))
952 td_io_close_file(td, f);
956 void close_and_free_files(struct thread_data *td)
961 dprint(FD_FILE, "close files\n");
963 for_each_file(td, f, i) {
964 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
965 dprint(FD_FILE, "free unlink %s\n", f->file_name);
966 unlink(f->file_name);
969 if (fio_file_open(f))
970 td_io_close_file(td, f);
976 axmap_free(f->io_axmap);
981 td->o.filename = NULL;
988 static void get_file_type(struct fio_file *f)
992 if (!strcmp(f->file_name, "-"))
993 f->filetype = FIO_TYPE_PIPE;
995 f->filetype = FIO_TYPE_FILE;
997 /* \\.\ is the device namespace in Windows, where every file is
999 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1000 f->filetype = FIO_TYPE_BD;
1002 if (!stat(f->file_name, &sb)) {
1003 if (S_ISBLK(sb.st_mode))
1004 f->filetype = FIO_TYPE_BD;
1005 else if (S_ISCHR(sb.st_mode))
1006 f->filetype = FIO_TYPE_CHAR;
1007 else if (S_ISFIFO(sb.st_mode))
1008 f->filetype = FIO_TYPE_PIPE;
1012 int add_file(struct thread_data *td, const char *fname)
1014 int cur_files = td->files_index;
1015 char file_name[PATH_MAX];
1019 dprint(FD_FILE, "add file %s\n", fname);
1021 f = smalloc(sizeof(*f));
1023 log_err("fio: smalloc OOM\n");
1030 if (td->files_size <= td->files_index) {
1031 int new_size = td->o.nr_files + 1;
1033 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1035 td->files = realloc(td->files, new_size * sizeof(f));
1036 td->files_size = new_size;
1038 td->files[cur_files] = f;
1039 f->fileno = cur_files;
1042 * init function, io engine may not be loaded yet
1044 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1045 f->real_file_size = -1ULL;
1047 if (td->o.directory)
1048 len = sprintf(file_name, "%s/", td->o.directory);
1050 sprintf(file_name + len, "%s", fname);
1051 f->file_name = smalloc_strdup(file_name);
1052 if (!f->file_name) {
1053 log_err("fio: smalloc OOM\n");
1059 switch (td->o.file_lock_mode) {
1060 case FILE_LOCK_NONE:
1062 case FILE_LOCK_READWRITE:
1063 f->lock = fio_mutex_rw_init();
1065 case FILE_LOCK_EXCLUSIVE:
1066 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
1069 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1074 if (f->filetype == FIO_TYPE_FILE)
1075 td->nr_normal_files++;
1077 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1083 int add_file_exclusive(struct thread_data *td, const char *fname)
1088 for_each_file(td, f, i) {
1089 if (!strcmp(f->file_name, fname))
1093 return add_file(td, fname);
1096 void get_file(struct fio_file *f)
1098 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
1099 assert(fio_file_open(f));
1103 int put_file(struct thread_data *td, struct fio_file *f)
1105 int f_ret = 0, ret = 0;
1107 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
1109 if (!fio_file_open(f)) {
1110 assert(f->fd == -1);
1114 assert(f->references);
1115 if (--f->references)
1118 if (should_fsync(td) && td->o.fsync_on_close)
1119 f_ret = fsync(f->fd);
1121 if (td->io_ops->close_file)
1122 ret = td->io_ops->close_file(td, f);
1127 td->nr_open_files--;
1128 fio_file_clear_open(f);
1129 assert(f->fd == -1);
1133 void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
1135 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1138 if (f->lock_owner == td && f->lock_batch--)
1141 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1142 if (ddir == DDIR_READ)
1143 fio_mutex_down_read(f->lock);
1145 fio_mutex_down_write(f->lock);
1146 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1147 fio_mutex_down(f->lock);
1150 f->lock_batch = td->o.lockfile_batch;
1151 f->lock_ddir = ddir;
1154 void unlock_file(struct thread_data *td, struct fio_file *f)
1156 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1161 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1162 const int is_read = f->lock_ddir == DDIR_READ;
1163 int val = fio_mutex_getval(f->lock);
1165 if ((is_read && val == 1) || (!is_read && val == -1))
1166 f->lock_owner = NULL;
1169 fio_mutex_up_read(f->lock);
1171 fio_mutex_up_write(f->lock);
1172 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
1173 int val = fio_mutex_getval(f->lock);
1176 f->lock_owner = NULL;
1178 fio_mutex_up(f->lock);
1182 void unlock_file_all(struct thread_data *td, struct fio_file *f)
1184 if (f->lock_owner != td)
1191 static int recurse_dir(struct thread_data *td, const char *dirname)
1197 D = opendir(dirname);
1199 char buf[FIO_VERROR_SIZE];
1201 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
1202 td_verror(td, errno, buf);
1206 while ((dir = readdir(D)) != NULL) {
1207 char full_path[PATH_MAX];
1210 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1213 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
1215 if (lstat(full_path, &sb) == -1) {
1216 if (errno != ENOENT) {
1217 td_verror(td, errno, "stat");
1222 if (S_ISREG(sb.st_mode)) {
1223 add_file(td, full_path);
1227 if (!S_ISDIR(sb.st_mode))
1230 ret = recurse_dir(td, full_path);
1239 int add_dir_files(struct thread_data *td, const char *path)
1241 int ret = recurse_dir(td, path);
1244 log_info("fio: opendir added %d files\n", td->o.nr_files);
1249 void dup_files(struct thread_data *td, struct thread_data *org)
1254 dprint(FD_FILE, "dup files: %d\n", org->files_index);
1259 td->files = malloc(org->files_index * sizeof(f));
1261 for_each_file(org, f, i) {
1262 struct fio_file *__f;
1264 __f = smalloc(sizeof(*__f));
1266 log_err("fio: smalloc OOM\n");
1270 fio_file_reset(__f);
1273 __f->file_name = smalloc_strdup(f->file_name);
1274 if (!__f->file_name) {
1275 log_err("fio: smalloc OOM\n");
1279 __f->filetype = f->filetype;
1287 * Returns the index that matches the filename, or -1 if not there
1289 int get_fileno(struct thread_data *td, const char *fname)
1294 for_each_file(td, f, i)
1295 if (!strcmp(f->file_name, fname))
1302 * For log usage, where we add/open/close files automatically
1304 void free_release_files(struct thread_data *td)
1307 td->files_index = 0;
1308 td->nr_normal_files = 0;