17 #ifdef FIO_HAVE_LINUX_FALLOCATE
18 #include <linux/falloc.h>
23 static inline void clear_error(struct thread_data *td)
30 * Leaves f->fd open on success, caller must close
32 static int extend_file(struct thread_data *td, struct fio_file *f)
34 int r, new_layout = 0, unlink_file = 0, flags;
35 unsigned long long left;
40 log_err("fio: refusing extend of file due to read-only\n");
45 * check if we need to lay the file out complete again. fio
46 * does that for operations involving reads, or for writes
47 * where overwrite is set
49 if (td_read(td) || (td_write(td) && td->o.overwrite) ||
50 (td_write(td) && td->io_ops->flags & FIO_NOEXTEND))
52 if (td_write(td) && !td->o.overwrite)
55 if (unlink_file || new_layout) {
56 dprint(FD_FILE, "layout unlink %s\n", f->file_name);
57 if ((unlink(f->file_name) < 0) && (errno != ENOENT)) {
58 td_verror(td, errno, "unlink");
63 flags = O_WRONLY | O_CREAT;
67 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
68 f->fd = open(f->file_name, flags, 0644);
70 td_verror(td, errno, "open");
74 #ifdef FIO_HAVE_FALLOCATE
75 if (!td->o.fill_device) {
76 switch (td->o.fallocate_mode) {
77 case FIO_FALLOCATE_NONE:
79 case FIO_FALLOCATE_POSIX:
80 dprint(FD_FILE, "posix_fallocate file %s size %llu\n",
81 f->file_name, f->real_file_size);
83 r = posix_fallocate(f->fd, 0, f->real_file_size);
85 log_err("fio: posix_fallocate fails: %s\n",
89 #ifdef FIO_HAVE_LINUX_FALLOCATE
90 case FIO_FALLOCATE_KEEP_SIZE:
92 "fallocate(FALLOC_FL_KEEP_SIZE) "
93 "file %s size %llu\n",
94 f->file_name, f->real_file_size);
96 r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0,
99 td_verror(td, errno, "fallocate");
102 #endif /* FIO_HAVE_LINUX_FALLOCATE */
104 log_err("fio: unknown fallocate mode: %d\n",
105 td->o.fallocate_mode);
109 #endif /* FIO_HAVE_FALLOCATE */
115 * The size will be -1ULL when fill_device is used, so don't truncate
116 * or fallocate this file, just write it
118 if (!td->o.fill_device) {
119 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
121 if (ftruncate(f->fd, f->real_file_size) == -1) {
122 td_verror(td, errno, "ftruncate");
127 b = malloc(td->o.max_bs[DDIR_WRITE]);
128 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
130 left = f->real_file_size;
131 while (left && !td->terminate) {
132 bs = td->o.max_bs[DDIR_WRITE];
136 r = write(f->fd, b, bs);
146 if (td->o.fill_device)
148 log_info("fio: ENOSPC on laying out "
152 td_verror(td, errno, "write");
154 td_verror(td, EIO, "write");
161 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
162 unlink(f->file_name);
163 } else if (td->o.create_fsync) {
164 if (fsync(f->fd) < 0) {
165 td_verror(td, errno, "fsync");
169 if (td->o.fill_device && !td_write(td)) {
170 fio_file_clear_size_known(f);
171 if (td_io_get_file_size(td, f))
173 if (f->io_size > f->real_file_size)
174 f->io_size = f->real_file_size;
186 static int pre_read_file(struct thread_data *td, struct fio_file *f)
188 int r, did_open = 0, old_runstate;
189 unsigned long long left;
193 if (td->io_ops->flags & FIO_PIPEIO)
196 if (!fio_file_open(f)) {
197 if (td->io_ops->open_file(td, f)) {
198 log_err("fio: cannot pre-read, failed to open file\n");
204 old_runstate = td->runstate;
205 td_set_runstate(td, TD_PRE_READING);
207 bs = td->o.max_bs[DDIR_READ];
211 lseek(f->fd, f->file_offset, SEEK_SET);
214 while (left && !td->terminate) {
218 r = read(f->fd, b, bs);
224 td_verror(td, EIO, "pre_read");
229 td_set_runstate(td, old_runstate);
232 td->io_ops->close_file(td, f);
237 static unsigned long long get_rand_file_size(struct thread_data *td)
239 unsigned long long ret, sized;
242 if (td->o.use_os_rand) {
243 r = os_random_long(&td->file_size_state);
244 sized = td->o.file_size_high - td->o.file_size_low;
245 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
247 r = __rand(&td->__file_size_state);
248 sized = td->o.file_size_high - td->o.file_size_low;
249 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0)));
252 ret += td->o.file_size_low;
253 ret -= (ret % td->o.rw_min_bs);
257 static int file_size(struct thread_data *td, struct fio_file *f)
261 if (stat(f->file_name, &st) == -1) {
262 td_verror(td, errno, "fstat");
266 f->real_file_size = st.st_size;
270 static int bdev_size(struct thread_data *td, struct fio_file *f)
272 unsigned long long bytes = 0;
275 if (td->io_ops->open_file(td, f)) {
276 log_err("fio: failed opening blockdev %s for size check\n",
281 r = blockdev_size(f, &bytes);
283 td_verror(td, r, "blockdev_size");
288 log_err("%s: zero sized block device?\n", f->file_name);
292 f->real_file_size = bytes;
293 td->io_ops->close_file(td, f);
296 td->io_ops->close_file(td, f);
300 static int char_size(struct thread_data *td, struct fio_file *f)
302 #ifdef FIO_HAVE_CHARDEV_SIZE
303 unsigned long long bytes = 0;
306 if (td->io_ops->open_file(td, f)) {
307 log_err("fio: failed opening blockdev %s for size check\n",
312 r = chardev_size(f, &bytes);
314 td_verror(td, r, "chardev_size");
319 log_err("%s: zero sized char device?\n", f->file_name);
323 f->real_file_size = bytes;
324 td->io_ops->close_file(td, f);
327 td->io_ops->close_file(td, f);
330 f->real_file_size = -1ULL;
335 static int get_file_size(struct thread_data *td, struct fio_file *f)
339 if (fio_file_size_known(f))
342 if (f->filetype == FIO_TYPE_FILE)
343 ret = file_size(td, f);
344 else if (f->filetype == FIO_TYPE_BD)
345 ret = bdev_size(td, f);
346 else if (f->filetype == FIO_TYPE_CHAR)
347 ret = char_size(td, f);
349 f->real_file_size = -1;
354 if (f->file_offset > f->real_file_size) {
355 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
356 f->file_offset, f->real_file_size);
360 fio_file_set_size_known(f);
364 static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
365 unsigned long long off,
366 unsigned long long len)
373 off = f->file_offset;
375 if (len == -1ULL || off == -1ULL)
378 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
382 * FIXME: add blockdev flushing too
385 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
387 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
389 } else if (f->filetype == FIO_TYPE_FILE) {
390 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
391 } else if (f->filetype == FIO_TYPE_BD) {
392 ret = blockdev_invalidate_cache(f);
393 if (ret < 0 && errno == EACCES && geteuid()) {
395 log_err("fio: only root may flush block "
396 "devices. Cache flush bypassed!\n");
401 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
405 td_verror(td, errno, "invalidate_cache");
407 } else if (ret > 0) {
408 td_verror(td, ret, "invalidate_cache");
416 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
418 if (!fio_file_open(f))
421 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
424 int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
428 dprint(FD_FILE, "fd close %s\n", f->file_name);
432 if (close(f->fd) < 0)
439 int file_lookup_open(struct fio_file *f, int flags)
441 struct fio_file *__f;
444 __f = lookup_file_hash(f->file_name);
446 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
448 * racy, need the __f->lock locked
451 f->lock_owner = __f->lock_owner;
452 f->lock_batch = __f->lock_batch;
453 f->lock_ddir = __f->lock_ddir;
456 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
460 f->fd = open(f->file_name, flags, 0600);
464 int generic_open_file(struct thread_data *td, struct fio_file *f)
470 dprint(FD_FILE, "fd open %s\n", f->file_name);
472 if (td_trim(td) && f->filetype != FIO_TYPE_BD) {
473 log_err("fio: trim only applies to block device\n");
477 if (!strcmp(f->file_name, "-")) {
479 log_err("fio: can't read/write to stdin/out\n");
485 * move output logging to stderr, if we are writing to stdout
494 flags |= OS_O_DIRECT;
497 if (td->o.create_on_open)
500 if (f->filetype != FIO_TYPE_FILE)
501 flags |= FIO_O_NOATIME;
508 if (f->filetype == FIO_TYPE_FILE)
512 f->fd = dup(STDOUT_FILENO);
514 from_hash = file_lookup_open(f, flags);
515 } else if (td_read(td)) {
516 if (f->filetype == FIO_TYPE_CHAR && !read_only)
522 f->fd = dup(STDIN_FILENO);
524 from_hash = file_lookup_open(f, flags);
527 from_hash = file_lookup_open(f, flags);
531 char buf[FIO_VERROR_SIZE];
534 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
535 flags &= ~FIO_O_NOATIME;
539 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
541 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
542 log_err("fio: looks like your file system does not " \
543 "support direct=1/buffered=0\n");
546 td_verror(td, __e, buf);
549 if (!from_hash && f->fd != -1) {
550 if (add_file_hash(f)) {
554 * OK to ignore, we haven't done anything with it
556 ret = generic_close_file(td, f);
564 int generic_get_file_size(struct thread_data *td, struct fio_file *f)
566 return get_file_size(td, f);
570 * open/close all files, so that ->real_file_size gets set
572 static int get_file_sizes(struct thread_data *td)
578 for_each_file(td, f, i) {
579 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
582 if (td_io_get_file_size(td, f)) {
583 if (td->error != ENOENT) {
584 log_err("%s\n", td->verror);
590 if (f->real_file_size == -1ULL && td->o.size)
591 f->real_file_size = td->o.size / td->o.nr_files;
598 struct flist_head list;
605 * Get free number of bytes for each file on each unique mount.
607 static unsigned long long get_fs_free_counts(struct thread_data *td)
609 struct flist_head *n, *tmp;
610 unsigned long long ret = 0;
611 struct fio_mount *fm;
616 for_each_file(td, f, i) {
620 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
621 if (f->real_file_size != -1ULL)
622 ret += f->real_file_size;
624 } else if (f->filetype != FIO_TYPE_FILE)
627 strcpy(buf, f->file_name);
629 if (stat(buf, &sb) < 0) {
633 if (stat(buf, &sb) < 0)
638 flist_for_each(n, &list) {
639 fm = flist_entry(n, struct fio_mount, list);
640 if (fm->key == sb.st_dev)
649 fm = malloc(sizeof(*fm));
650 strcpy(fm->__base, buf);
651 fm->base = basename(fm->__base);
653 flist_add(&fm->list, &list);
656 flist_for_each_safe(n, tmp, &list) {
657 unsigned long long sz;
659 fm = flist_entry(n, struct fio_mount, list);
660 flist_del(&fm->list);
662 sz = get_fs_size(fm->base);
663 if (sz && sz != -1ULL)
672 unsigned long long get_start_offset(struct thread_data *td)
674 return td->o.start_offset +
675 (td->thread_number - 1) * td->o.offset_increment;
679 * Open the files and setup files sizes, creating files if necessary.
681 int setup_files(struct thread_data *td)
683 unsigned long long total_size, extend_size;
686 int err = 0, need_extend;
688 dprint(FD_FILE, "setup files\n");
690 if (td->o.read_iolog_file)
694 * if ioengine defines a setup() method, it's responsible for
695 * opening the files and setting f->real_file_size to indicate
696 * the valid range for that file.
698 if (td->io_ops->setup)
699 err = td->io_ops->setup(td);
701 err = get_file_sizes(td);
707 * check sizes. if the files/devices do not exist and the size
708 * isn't passed to fio, abort.
711 for_each_file(td, f, i) {
712 if (f->real_file_size == -1ULL)
715 total_size += f->real_file_size;
718 if (td->o.fill_device)
719 td->fill_device_size = get_fs_free_counts(td);
722 * device/file sizes are zero and no size given, punt
724 if ((!total_size || total_size == -1ULL) && !td->o.size &&
725 !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
726 log_err("%s: you need to specify size=\n", td->o.name);
727 td_verror(td, EINVAL, "total_file_size");
732 * now file sizes are known, so we can set ->io_size. if size= is
733 * not given, ->io_size is just equal to ->real_file_size. if size
734 * is given, ->io_size is size / nr_files.
736 extend_size = total_size = 0;
738 for_each_file(td, f, i) {
739 f->file_offset = get_start_offset(td);
741 if (!td->o.file_size_low) {
743 * no file size range given, file size is equal to
744 * total size divided by number of files. if that is
745 * zero, set it to the real file size.
747 f->io_size = td->o.size / td->o.nr_files;
749 f->io_size = f->real_file_size - f->file_offset;
750 } else if (f->real_file_size < td->o.file_size_low ||
751 f->real_file_size > td->o.file_size_high) {
752 if (f->file_offset > td->o.file_size_low)
755 * file size given. if it's fixed, use that. if it's a
756 * range, generate a random size in-between.
758 if (td->o.file_size_low == td->o.file_size_high) {
759 f->io_size = td->o.file_size_low
762 f->io_size = get_rand_file_size(td)
766 f->io_size = f->real_file_size - f->file_offset;
768 if (f->io_size == -1ULL)
771 if (td->o.size_percent)
772 f->io_size = (f->io_size * td->o.size_percent) / 100;
773 total_size += f->io_size;
776 if (f->filetype == FIO_TYPE_FILE &&
777 (f->io_size + f->file_offset) > f->real_file_size &&
778 !(td->io_ops->flags & FIO_DISKLESSIO)) {
779 if (!td->o.create_on_open) {
781 extend_size += (f->io_size + f->file_offset);
783 f->real_file_size = f->io_size + f->file_offset;
784 fio_file_set_extend(f);
788 if (!td->o.size || td->o.size > total_size)
789 td->o.size = total_size;
792 * See if we need to extend some files
796 if (output_format == FIO_OUTPUT_NORMAL)
797 log_info("%s: Laying out IO file(s) (%u file(s) /"
798 " %lluMB)\n", td->o.name, need_extend,
801 for_each_file(td, f, i) {
802 unsigned long long old_len = -1ULL, extend_len = -1ULL;
804 if (!fio_file_extend(f))
807 assert(f->filetype == FIO_TYPE_FILE);
808 fio_file_clear_extend(f);
809 if (!td->o.fill_device) {
810 old_len = f->real_file_size;
811 extend_len = f->io_size + f->file_offset -
814 f->real_file_size = (f->io_size + f->file_offset);
815 err = extend_file(td, f);
819 err = __file_invalidate_cache(td, f, old_len,
832 if (!td->o.zone_size)
833 td->o.zone_size = td->o.size;
836 * iolog already set the total io size, if we read back
839 if (!td->o.read_iolog_file)
840 td->total_io_size = td->o.size * td->o.loops;
843 if (td->o.create_only)
848 log_err("%s: you need to specify valid offset=\n", td->o.name);
852 int pre_read_files(struct thread_data *td)
857 dprint(FD_FILE, "pre_read files\n");
859 for_each_file(td, f, i) {
860 pre_read_file(td, f);
866 static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
868 unsigned int range_size, seed;
869 unsigned long nranges;
871 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
873 nranges = (f->real_file_size + range_size - 1) / range_size;
875 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
876 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
877 zipf_init(&f->zipf, nranges, td->o.zipf_theta, seed);
879 pareto_init(&f->zipf, nranges, td->o.pareto_h, seed);
884 static int init_rand_distribution(struct thread_data *td)
890 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
893 state = td->runstate;
894 td_set_runstate(td, TD_SETTING_UP);
895 for_each_file(td, f, i)
896 __init_rand_distribution(td, f);
897 td_set_runstate(td, state);
902 int init_random_map(struct thread_data *td)
904 unsigned long long blocks, num_maps;
908 if (init_rand_distribution(td))
910 if (td->o.norandommap || !td_random(td))
913 for_each_file(td, f, i) {
914 blocks = (f->real_file_size + td->o.rw_min_bs - 1) /
915 (unsigned long long) td->o.rw_min_bs;
916 num_maps = (blocks + BLOCKS_PER_MAP - 1) /
917 (unsigned long long) BLOCKS_PER_MAP;
918 if (num_maps == (unsigned long) num_maps) {
919 f->file_map = smalloc(num_maps * sizeof(unsigned long));
921 f->num_maps = num_maps;
927 if (!td->o.softrandommap) {
928 log_err("fio: failed allocating random map. If running"
929 " a large number of jobs, try the 'norandommap'"
930 " option or set 'softrandommap'. Or give"
931 " a larger --alloc-size to fio.\n");
935 log_info("fio: file %s failed allocating random map. Running "
936 "job without.\n", f->file_name);
943 void close_files(struct thread_data *td)
948 for_each_file(td, f, i) {
949 if (fio_file_open(f))
950 td_io_close_file(td, f);
954 void close_and_free_files(struct thread_data *td)
959 dprint(FD_FILE, "close files\n");
961 for_each_file(td, f, i) {
962 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
963 dprint(FD_FILE, "free unlink %s\n", f->file_name);
964 unlink(f->file_name);
967 if (fio_file_open(f))
968 td_io_close_file(td, f);
979 td->o.filename = NULL;
986 static void get_file_type(struct fio_file *f)
990 if (!strcmp(f->file_name, "-"))
991 f->filetype = FIO_TYPE_PIPE;
993 f->filetype = FIO_TYPE_FILE;
995 /* \\.\ is the device namespace in Windows, where every file is
997 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
998 f->filetype = FIO_TYPE_BD;
1000 if (!stat(f->file_name, &sb)) {
1001 if (S_ISBLK(sb.st_mode))
1002 f->filetype = FIO_TYPE_BD;
1003 else if (S_ISCHR(sb.st_mode))
1004 f->filetype = FIO_TYPE_CHAR;
1005 else if (S_ISFIFO(sb.st_mode))
1006 f->filetype = FIO_TYPE_PIPE;
1010 int add_file(struct thread_data *td, const char *fname)
1012 int cur_files = td->files_index;
1013 char file_name[PATH_MAX];
1017 dprint(FD_FILE, "add file %s\n", fname);
1019 f = smalloc(sizeof(*f));
1021 log_err("fio: smalloc OOM\n");
1028 if (td->files_size <= td->files_index) {
1029 int new_size = td->o.nr_files + 1;
1031 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1033 td->files = realloc(td->files, new_size * sizeof(f));
1034 td->files_size = new_size;
1036 td->files[cur_files] = f;
1037 f->fileno = cur_files;
1040 * init function, io engine may not be loaded yet
1042 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1043 f->real_file_size = -1ULL;
1045 if (td->o.directory)
1046 len = sprintf(file_name, "%s/", td->o.directory);
1048 sprintf(file_name + len, "%s", fname);
1049 f->file_name = smalloc_strdup(file_name);
1050 if (!f->file_name) {
1051 log_err("fio: smalloc OOM\n");
1057 switch (td->o.file_lock_mode) {
1058 case FILE_LOCK_NONE:
1060 case FILE_LOCK_READWRITE:
1061 f->lock = fio_mutex_rw_init();
1063 case FILE_LOCK_EXCLUSIVE:
1064 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
1067 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1072 if (f->filetype == FIO_TYPE_FILE)
1073 td->nr_normal_files++;
1075 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1081 int add_file_exclusive(struct thread_data *td, const char *fname)
1086 for_each_file(td, f, i) {
1087 if (!strcmp(f->file_name, fname))
1091 return add_file(td, fname);
1094 void get_file(struct fio_file *f)
1096 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
1097 assert(fio_file_open(f));
1101 int put_file(struct thread_data *td, struct fio_file *f)
1103 int f_ret = 0, ret = 0;
1105 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
1107 if (!fio_file_open(f)) {
1108 assert(f->fd == -1);
1112 assert(f->references);
1113 if (--f->references)
1116 if (should_fsync(td) && td->o.fsync_on_close)
1117 f_ret = fsync(f->fd);
1119 if (td->io_ops->close_file)
1120 ret = td->io_ops->close_file(td, f);
1125 td->nr_open_files--;
1126 fio_file_clear_open(f);
1127 assert(f->fd == -1);
1131 void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
1133 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1136 if (f->lock_owner == td && f->lock_batch--)
1139 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1140 if (ddir == DDIR_READ)
1141 fio_mutex_down_read(f->lock);
1143 fio_mutex_down_write(f->lock);
1144 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1145 fio_mutex_down(f->lock);
1148 f->lock_batch = td->o.lockfile_batch;
1149 f->lock_ddir = ddir;
1152 void unlock_file(struct thread_data *td, struct fio_file *f)
1154 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1159 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1160 const int is_read = f->lock_ddir == DDIR_READ;
1161 int val = fio_mutex_getval(f->lock);
1163 if ((is_read && val == 1) || (!is_read && val == -1))
1164 f->lock_owner = NULL;
1167 fio_mutex_up_read(f->lock);
1169 fio_mutex_up_write(f->lock);
1170 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
1171 int val = fio_mutex_getval(f->lock);
1174 f->lock_owner = NULL;
1176 fio_mutex_up(f->lock);
1180 void unlock_file_all(struct thread_data *td, struct fio_file *f)
1182 if (f->lock_owner != td)
1189 static int recurse_dir(struct thread_data *td, const char *dirname)
1195 D = opendir(dirname);
1197 char buf[FIO_VERROR_SIZE];
1199 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
1200 td_verror(td, errno, buf);
1204 while ((dir = readdir(D)) != NULL) {
1205 char full_path[PATH_MAX];
1208 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1211 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
1213 if (lstat(full_path, &sb) == -1) {
1214 if (errno != ENOENT) {
1215 td_verror(td, errno, "stat");
1220 if (S_ISREG(sb.st_mode)) {
1221 add_file(td, full_path);
1225 if (!S_ISDIR(sb.st_mode))
1228 ret = recurse_dir(td, full_path);
1237 int add_dir_files(struct thread_data *td, const char *path)
1239 int ret = recurse_dir(td, path);
1242 log_info("fio: opendir added %d files\n", td->o.nr_files);
1247 void dup_files(struct thread_data *td, struct thread_data *org)
1252 dprint(FD_FILE, "dup files: %d\n", org->files_index);
1257 td->files = malloc(org->files_index * sizeof(f));
1259 for_each_file(org, f, i) {
1260 struct fio_file *__f;
1262 __f = smalloc(sizeof(*__f));
1264 log_err("fio: smalloc OOM\n");
1268 fio_file_reset(__f);
1271 __f->file_name = smalloc_strdup(f->file_name);
1272 if (!__f->file_name) {
1273 log_err("fio: smalloc OOM\n");
1277 __f->filetype = f->filetype;
1285 * Returns the index that matches the filename, or -1 if not there
1287 int get_fileno(struct thread_data *td, const char *fname)
1292 for_each_file(td, f, i)
1293 if (!strcmp(f->file_name, fname))
1300 * For log usage, where we add/open/close files automatically
1302 void free_release_files(struct thread_data *td)
1305 td->files_index = 0;
1306 td->nr_normal_files = 0;