17 #include "lib/axmap.h"
19 #ifdef CONFIG_LINUX_FALLOCATE
20 #include <linux/falloc.h>
25 static FLIST_HEAD(filename_list);
27 static inline void clear_error(struct thread_data *td)
34 * Leaves f->fd open on success, caller must close
36 static int extend_file(struct thread_data *td, struct fio_file *f)
38 int r, new_layout = 0, unlink_file = 0, flags;
39 unsigned long long left;
44 log_err("fio: refusing extend of file due to read-only\n");
49 * check if we need to lay the file out complete again. fio
50 * does that for operations involving reads, or for writes
51 * where overwrite is set
53 if (td_read(td) || (td_write(td) && td->o.overwrite) ||
54 (td_write(td) && td->io_ops->flags & FIO_NOEXTEND))
56 if (td_write(td) && !td->o.overwrite)
59 if (unlink_file || new_layout) {
60 dprint(FD_FILE, "layout unlink %s\n", f->file_name);
61 if ((unlink(f->file_name) < 0) && (errno != ENOENT)) {
62 td_verror(td, errno, "unlink");
67 flags = O_WRONLY | O_CREAT;
71 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
72 f->fd = open(f->file_name, flags, 0644);
74 td_verror(td, errno, "open");
78 #ifdef CONFIG_POSIX_FALLOCATE
79 if (!td->o.fill_device) {
80 switch (td->o.fallocate_mode) {
81 case FIO_FALLOCATE_NONE:
83 case FIO_FALLOCATE_POSIX:
84 dprint(FD_FILE, "posix_fallocate file %s size %llu\n",
86 (unsigned long long) f->real_file_size);
88 r = posix_fallocate(f->fd, 0, f->real_file_size);
90 log_err("fio: posix_fallocate fails: %s\n",
94 #ifdef CONFIG_LINUX_FALLOCATE
95 case FIO_FALLOCATE_KEEP_SIZE:
97 "fallocate(FALLOC_FL_KEEP_SIZE) "
98 "file %s size %llu\n", f->file_name,
99 (unsigned long long) f->real_file_size);
101 r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0,
104 td_verror(td, errno, "fallocate");
107 #endif /* CONFIG_LINUX_FALLOCATE */
109 log_err("fio: unknown fallocate mode: %d\n",
110 td->o.fallocate_mode);
114 #endif /* CONFIG_POSIX_FALLOCATE */
120 * The size will be -1ULL when fill_device is used, so don't truncate
121 * or fallocate this file, just write it
123 if (!td->o.fill_device) {
124 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
125 (unsigned long long) f->real_file_size);
126 if (ftruncate(f->fd, f->real_file_size) == -1) {
127 if (errno != EFBIG) {
128 td_verror(td, errno, "ftruncate");
134 b = malloc(td->o.max_bs[DDIR_WRITE]);
136 left = f->real_file_size;
137 while (left && !td->terminate) {
138 bs = td->o.max_bs[DDIR_WRITE];
142 fill_io_buffer(td, b, bs, bs);
144 r = write(f->fd, b, bs);
154 if (td->o.fill_device)
156 log_info("fio: ENOSPC on laying out "
160 td_verror(td, errno, "write");
162 td_verror(td, EIO, "write");
169 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
170 unlink(f->file_name);
171 } else if (td->o.create_fsync) {
172 if (fsync(f->fd) < 0) {
173 td_verror(td, errno, "fsync");
177 if (td->o.fill_device && !td_write(td)) {
178 fio_file_clear_size_known(f);
179 if (td_io_get_file_size(td, f))
181 if (f->io_size > f->real_file_size)
182 f->io_size = f->real_file_size;
194 static int pre_read_file(struct thread_data *td, struct fio_file *f)
196 int r, did_open = 0, old_runstate;
197 unsigned long long left;
201 if (td->io_ops->flags & FIO_PIPEIO)
204 if (!fio_file_open(f)) {
205 if (td->io_ops->open_file(td, f)) {
206 log_err("fio: cannot pre-read, failed to open file\n");
212 old_runstate = td_bump_runstate(td, TD_PRE_READING);
214 bs = td->o.max_bs[DDIR_READ];
218 lseek(f->fd, f->file_offset, SEEK_SET);
221 while (left && !td->terminate) {
225 r = read(f->fd, b, bs);
231 td_verror(td, EIO, "pre_read");
236 td_restore_runstate(td, old_runstate);
239 td->io_ops->close_file(td, f);
244 static unsigned long long get_rand_file_size(struct thread_data *td)
246 unsigned long long ret, sized;
249 if (td->o.use_os_rand) {
250 r = os_random_long(&td->file_size_state);
251 sized = td->o.file_size_high - td->o.file_size_low;
252 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
254 r = __rand(&td->__file_size_state);
255 sized = td->o.file_size_high - td->o.file_size_low;
256 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0)));
259 ret += td->o.file_size_low;
260 ret -= (ret % td->o.rw_min_bs);
264 static int file_size(struct thread_data *td, struct fio_file *f)
268 if (stat(f->file_name, &st) == -1) {
269 td_verror(td, errno, "fstat");
273 f->real_file_size = st.st_size;
277 static int bdev_size(struct thread_data *td, struct fio_file *f)
279 unsigned long long bytes = 0;
282 if (td->io_ops->open_file(td, f)) {
283 log_err("fio: failed opening blockdev %s for size check\n",
288 r = blockdev_size(f, &bytes);
290 td_verror(td, r, "blockdev_size");
295 log_err("%s: zero sized block device?\n", f->file_name);
299 f->real_file_size = bytes;
300 td->io_ops->close_file(td, f);
303 td->io_ops->close_file(td, f);
307 static int char_size(struct thread_data *td, struct fio_file *f)
309 #ifdef FIO_HAVE_CHARDEV_SIZE
310 unsigned long long bytes = 0;
313 if (td->io_ops->open_file(td, f)) {
314 log_err("fio: failed opening blockdev %s for size check\n",
319 r = chardev_size(f, &bytes);
321 td_verror(td, r, "chardev_size");
326 log_err("%s: zero sized char device?\n", f->file_name);
330 f->real_file_size = bytes;
331 td->io_ops->close_file(td, f);
334 td->io_ops->close_file(td, f);
337 f->real_file_size = -1ULL;
342 static int get_file_size(struct thread_data *td, struct fio_file *f)
346 if (fio_file_size_known(f))
349 if (f->filetype == FIO_TYPE_FILE)
350 ret = file_size(td, f);
351 else if (f->filetype == FIO_TYPE_BD)
352 ret = bdev_size(td, f);
353 else if (f->filetype == FIO_TYPE_CHAR)
354 ret = char_size(td, f);
356 f->real_file_size = -1;
361 if (f->file_offset > f->real_file_size) {
362 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
363 (unsigned long long) f->file_offset,
364 (unsigned long long) f->real_file_size);
368 fio_file_set_size_known(f);
372 static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
373 unsigned long long off,
374 unsigned long long len)
381 off = f->file_offset;
383 if (len == -1ULL || off == -1ULL)
386 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
390 * FIXME: add blockdev flushing too
393 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
395 if (f->filetype == FIO_TYPE_BD)
396 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
398 } else if (f->filetype == FIO_TYPE_FILE) {
399 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
400 } else if (f->filetype == FIO_TYPE_BD) {
401 ret = blockdev_invalidate_cache(f);
402 if (ret < 0 && errno == EACCES && geteuid()) {
404 log_err("fio: only root may flush block "
405 "devices. Cache flush bypassed!\n");
410 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
414 td_verror(td, errno, "invalidate_cache");
416 } else if (ret > 0) {
417 td_verror(td, ret, "invalidate_cache");
425 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
427 if (!fio_file_open(f))
430 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
433 int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
437 dprint(FD_FILE, "fd close %s\n", f->file_name);
441 if (close(f->fd) < 0)
446 if (f->shadow_fd != -1) {
455 int file_lookup_open(struct fio_file *f, int flags)
457 struct fio_file *__f;
460 __f = lookup_file_hash(f->file_name);
462 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
464 * racy, need the __f->lock locked
469 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
473 f->fd = open(f->file_name, flags, 0600);
477 static int file_close_shadow_fds(struct thread_data *td)
483 for_each_file(td, f, i) {
484 if (f->shadow_fd == -1)
495 int generic_open_file(struct thread_data *td, struct fio_file *f)
501 dprint(FD_FILE, "fd open %s\n", f->file_name);
503 if (td_trim(td) && f->filetype != FIO_TYPE_BD) {
504 log_err("fio: trim only applies to block device\n");
508 if (!strcmp(f->file_name, "-")) {
510 log_err("fio: can't read/write to stdin/out\n");
516 * move output logging to stderr, if we are writing to stdout
525 flags |= OS_O_DIRECT;
528 td_verror(td, EINVAL, "OS does not support atomic IO");
531 flags |= OS_O_DIRECT | FIO_O_ATOMIC;
535 if (td->o.create_on_open)
538 if (f->filetype != FIO_TYPE_FILE)
539 flags |= FIO_O_NOATIME;
546 if (f->filetype == FIO_TYPE_FILE)
550 f->fd = dup(STDOUT_FILENO);
552 from_hash = file_lookup_open(f, flags);
553 } else if (td_read(td)) {
554 if (f->filetype == FIO_TYPE_CHAR && !read_only)
560 f->fd = dup(STDIN_FILENO);
562 from_hash = file_lookup_open(f, flags);
565 from_hash = file_lookup_open(f, flags);
569 char buf[FIO_VERROR_SIZE];
572 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
573 flags &= ~FIO_O_NOATIME;
576 if (__e == EMFILE && file_close_shadow_fds(td))
579 snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
581 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
582 log_err("fio: looks like your file system does not " \
583 "support direct=1/buffered=0\n");
586 td_verror(td, __e, buf);
589 if (!from_hash && f->fd != -1) {
590 if (add_file_hash(f)) {
594 * Stash away descriptor for later close. This is to
595 * work-around a "feature" on Linux, where a close of
596 * an fd that has been opened for write will trigger
597 * udev to call blkid to check partitions, fs id, etc.
598 * That pollutes the device cache, which can slow down
599 * unbuffered accesses.
601 if (f->shadow_fd == -1)
602 f->shadow_fd = f->fd;
605 * OK to ignore, we haven't done anything
608 ret = generic_close_file(td, f);
617 int generic_get_file_size(struct thread_data *td, struct fio_file *f)
619 return get_file_size(td, f);
623 * open/close all files, so that ->real_file_size gets set
625 static int get_file_sizes(struct thread_data *td)
631 for_each_file(td, f, i) {
632 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
635 if (td_io_get_file_size(td, f)) {
636 if (td->error != ENOENT) {
637 log_err("%s\n", td->verror);
643 if (f->real_file_size == -1ULL && td->o.size)
644 f->real_file_size = td->o.size / td->o.nr_files;
651 struct flist_head list;
658 * Get free number of bytes for each file on each unique mount.
660 static unsigned long long get_fs_free_counts(struct thread_data *td)
662 struct flist_head *n, *tmp;
663 unsigned long long ret = 0;
664 struct fio_mount *fm;
669 for_each_file(td, f, i) {
673 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
674 if (f->real_file_size != -1ULL)
675 ret += f->real_file_size;
677 } else if (f->filetype != FIO_TYPE_FILE)
680 strcpy(buf, f->file_name);
682 if (stat(buf, &sb) < 0) {
686 if (stat(buf, &sb) < 0)
691 flist_for_each(n, &list) {
692 fm = flist_entry(n, struct fio_mount, list);
693 if (fm->key == sb.st_dev)
702 fm = malloc(sizeof(*fm));
703 strcpy(fm->__base, buf);
704 fm->base = basename(fm->__base);
706 flist_add(&fm->list, &list);
709 flist_for_each_safe(n, tmp, &list) {
710 unsigned long long sz;
712 fm = flist_entry(n, struct fio_mount, list);
713 flist_del(&fm->list);
715 sz = get_fs_size(fm->base);
716 if (sz && sz != -1ULL)
725 uint64_t get_start_offset(struct thread_data *td)
727 return td->o.start_offset +
728 (td->thread_number - 1) * td->o.offset_increment;
732 * Open the files and setup files sizes, creating files if necessary.
734 int setup_files(struct thread_data *td)
736 unsigned long long total_size, extend_size;
737 struct thread_options *o = &td->o;
739 unsigned int i, nr_fs_extra = 0;
740 int err = 0, need_extend;
742 const unsigned int bs = td_min_bs(td);
745 dprint(FD_FILE, "setup files\n");
747 old_state = td_bump_runstate(td, TD_SETTING_UP);
749 if (o->read_iolog_file)
753 * if ioengine defines a setup() method, it's responsible for
754 * opening the files and setting f->real_file_size to indicate
755 * the valid range for that file.
757 if (td->io_ops->setup)
758 err = td->io_ops->setup(td);
760 err = get_file_sizes(td);
766 * check sizes. if the files/devices do not exist and the size
767 * isn't passed to fio, abort.
770 for_each_file(td, f, i) {
771 if (f->real_file_size == -1ULL)
774 total_size += f->real_file_size;
778 td->fill_device_size = get_fs_free_counts(td);
781 * device/file sizes are zero and no size given, punt
783 if ((!total_size || total_size == -1ULL) && !o->size &&
784 !(td->io_ops->flags & FIO_NOIO) && !o->fill_device &&
785 !(o->nr_files && (o->file_size_low || o->file_size_high))) {
786 log_err("%s: you need to specify size=\n", o->name);
787 td_verror(td, EINVAL, "total_file_size");
792 * Calculate per-file size and potential extra size for the
793 * first files, if needed.
795 if (!o->file_size_low) {
798 fs = o->size / o->nr_files;
799 all_fs = fs * o->nr_files;
801 if (all_fs < o->size)
802 nr_fs_extra = (o->size - all_fs) / bs;
806 * now file sizes are known, so we can set ->io_size. if size= is
807 * not given, ->io_size is just equal to ->real_file_size. if size
808 * is given, ->io_size is size / nr_files.
810 extend_size = total_size = 0;
812 for_each_file(td, f, i) {
813 f->file_offset = get_start_offset(td);
815 if (!o->file_size_low) {
817 * no file size range given, file size is equal to
818 * total size divided by number of files. If that is
819 * zero, set it to the real file size. If the size
820 * doesn't divide nicely with the min blocksize,
821 * make the first files bigger.
830 f->io_size = f->real_file_size - f->file_offset;
831 } else if (f->real_file_size < o->file_size_low ||
832 f->real_file_size > o->file_size_high) {
833 if (f->file_offset > o->file_size_low)
836 * file size given. if it's fixed, use that. if it's a
837 * range, generate a random size in-between.
839 if (o->file_size_low == o->file_size_high)
840 f->io_size = o->file_size_low - f->file_offset;
842 f->io_size = get_rand_file_size(td)
846 f->io_size = f->real_file_size - f->file_offset;
848 if (f->io_size == -1ULL)
852 f->io_size = (f->io_size * o->size_percent) / 100;
853 total_size += f->io_size;
856 if (f->filetype == FIO_TYPE_FILE &&
857 (f->io_size + f->file_offset) > f->real_file_size &&
858 !(td->io_ops->flags & FIO_DISKLESSIO)) {
859 if (!o->create_on_open) {
861 extend_size += (f->io_size + f->file_offset);
863 f->real_file_size = f->io_size + f->file_offset;
864 fio_file_set_extend(f);
868 if (!o->size || o->size > total_size)
869 o->size = total_size;
872 * See if we need to extend some files
876 if (output_format == FIO_OUTPUT_NORMAL)
877 log_info("%s: Laying out IO file(s) (%u file(s) /"
878 " %lluMB)\n", o->name, need_extend,
881 for_each_file(td, f, i) {
882 unsigned long long old_len = -1ULL, extend_len = -1ULL;
884 if (!fio_file_extend(f))
887 assert(f->filetype == FIO_TYPE_FILE);
888 fio_file_clear_extend(f);
889 if (!o->fill_device) {
890 old_len = f->real_file_size;
891 extend_len = f->io_size + f->file_offset -
894 f->real_file_size = (f->io_size + f->file_offset);
895 err = extend_file(td, f);
899 err = __file_invalidate_cache(td, f, old_len,
913 o->zone_size = o->size;
916 * iolog already set the total io size, if we read back
919 if (!o->read_iolog_file)
920 td->total_io_size = o->size * o->loops;
926 td_restore_runstate(td, old_state);
929 log_err("%s: you need to specify valid offset=\n", o->name);
931 td_restore_runstate(td, old_state);
935 int pre_read_files(struct thread_data *td)
940 dprint(FD_FILE, "pre_read files\n");
942 for_each_file(td, f, i) {
943 pre_read_file(td, f);
949 static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
951 unsigned int range_size, seed;
952 unsigned long nranges;
955 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
956 file_size = min(f->real_file_size, f->io_size);
958 nranges = (file_size + range_size - 1) / range_size;
960 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
961 if (!td->o.rand_repeatable)
962 seed = td->rand_seeds[4];
964 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
965 zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed);
967 pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed);
972 static int init_rand_distribution(struct thread_data *td)
978 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
981 state = td_bump_runstate(td, TD_SETTING_UP);
983 for_each_file(td, f, i)
984 __init_rand_distribution(td, f);
986 td_restore_runstate(td, state);
991 int init_random_map(struct thread_data *td)
993 unsigned long long blocks;
997 if (init_rand_distribution(td))
1002 for_each_file(td, f, i) {
1003 uint64_t file_size = min(f->real_file_size, f->io_size);
1005 blocks = file_size / (unsigned long long) td->o.rw_min_bs;
1007 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
1010 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
1012 if (!lfsr_init(&f->lfsr, blocks, seed, 0))
1014 } else if (!td->o.norandommap) {
1015 f->io_axmap = axmap_new(blocks);
1018 } else if (td->o.norandommap)
1021 if (!td->o.softrandommap) {
1022 log_err("fio: failed allocating random map. If running"
1023 " a large number of jobs, try the 'norandommap'"
1024 " option or set 'softrandommap'. Or give"
1025 " a larger --alloc-size to fio.\n");
1029 log_info("fio: file %s failed allocating random map. Running "
1030 "job without.\n", f->file_name);
1036 void close_files(struct thread_data *td)
1041 for_each_file(td, f, i) {
1042 if (fio_file_open(f))
1043 td_io_close_file(td, f);
1047 void close_and_free_files(struct thread_data *td)
1052 dprint(FD_FILE, "close files\n");
1054 for_each_file(td, f, i) {
1055 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1056 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1057 unlink(f->file_name);
1060 if (fio_file_open(f))
1061 td_io_close_file(td, f);
1063 remove_file_hash(f);
1065 sfree(f->file_name);
1066 f->file_name = NULL;
1067 axmap_free(f->io_axmap);
1072 td->o.filename = NULL;
1074 free(td->file_locks);
1075 td->files_index = 0;
1077 td->file_locks = NULL;
1078 td->o.file_lock_mode = FILE_LOCK_NONE;
1082 static void get_file_type(struct fio_file *f)
1086 if (!strcmp(f->file_name, "-"))
1087 f->filetype = FIO_TYPE_PIPE;
1089 f->filetype = FIO_TYPE_FILE;
1091 /* \\.\ is the device namespace in Windows, where every file is
1093 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1094 f->filetype = FIO_TYPE_BD;
1096 if (!stat(f->file_name, &sb)) {
1097 if (S_ISBLK(sb.st_mode))
1098 f->filetype = FIO_TYPE_BD;
1099 else if (S_ISCHR(sb.st_mode))
1100 f->filetype = FIO_TYPE_CHAR;
1101 else if (S_ISFIFO(sb.st_mode))
1102 f->filetype = FIO_TYPE_PIPE;
1106 static void set_already_allocated(const char *fname) {
1107 struct file_name *fn;
1109 fn = malloc(sizeof(struct file_name));
1110 fn->filename = strdup(fname);
1111 flist_add_tail(&fn->list, &filename_list);
1114 static int is_already_allocated(const char *fname)
1116 struct flist_head *entry;
1119 if (!flist_empty(&filename_list))
1121 flist_for_each(entry, &filename_list) {
1122 filename = flist_entry(entry, struct file_name, list)->filename;
1124 if (strcmp(filename, fname) == 0)
1132 static void free_already_allocated() {
1133 struct flist_head *entry, *tmp;
1134 struct file_name *fn;
1136 if (!flist_empty(&filename_list))
1138 flist_for_each_safe(entry, tmp, &filename_list) {
1139 fn = flist_entry(entry, struct file_name, list);
1141 flist_del(&fn->list);
1147 int add_file(struct thread_data *td, const char *fname, int numjob, int inc)
1149 int cur_files = td->files_index;
1150 char file_name[PATH_MAX];
1154 dprint(FD_FILE, "add file %s\n", fname);
1156 if (td->o.directory)
1157 len = set_name_idx(file_name, td->o.directory, numjob);
1159 sprintf(file_name + len, "%s", fname);
1161 /* clean cloned siblings using existing files */
1162 if (numjob && is_already_allocated(file_name))
1165 f = smalloc(sizeof(*f));
1167 log_err("fio: smalloc OOM\n");
1173 fio_file_reset(td, f);
1175 if (td->files_size <= td->files_index) {
1176 unsigned int new_size = td->o.nr_files + 1;
1178 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1180 td->files = realloc(td->files, new_size * sizeof(f));
1181 if (td->files == NULL) {
1182 log_err("fio: realloc OOM\n");
1185 if (td->o.file_lock_mode != FILE_LOCK_NONE) {
1186 td->file_locks = realloc(td->file_locks, new_size);
1187 if (!td->file_locks) {
1188 log_err("fio: realloc OOM\n");
1191 td->file_locks[cur_files] = FILE_LOCK_NONE;
1193 td->files_size = new_size;
1195 td->files[cur_files] = f;
1196 f->fileno = cur_files;
1199 * init function, io engine may not be loaded yet
1201 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1202 f->real_file_size = -1ULL;
1204 f->file_name = smalloc_strdup(file_name);
1205 if (!f->file_name) {
1206 log_err("fio: smalloc OOM\n");
1212 switch (td->o.file_lock_mode) {
1213 case FILE_LOCK_NONE:
1215 case FILE_LOCK_READWRITE:
1216 f->rwlock = fio_rwlock_init();
1218 case FILE_LOCK_EXCLUSIVE:
1219 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
1222 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1227 if (f->filetype == FIO_TYPE_FILE)
1228 td->nr_normal_files++;
1230 set_already_allocated(file_name);
1233 * For adding files after the fact - if openfiles= isn't
1234 * given as an option, ensure we allow at least one file open
1236 if (!td->o.open_files)
1237 td->o.open_files = 1;
1242 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1248 int add_file_exclusive(struct thread_data *td, const char *fname)
1253 for_each_file(td, f, i) {
1254 if (!strcmp(f->file_name, fname))
1258 return add_file(td, fname, 0, 1);
1261 void get_file(struct fio_file *f)
1263 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
1264 assert(fio_file_open(f));
1268 int put_file(struct thread_data *td, struct fio_file *f)
1270 int f_ret = 0, ret = 0;
1272 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
1274 if (!fio_file_open(f)) {
1275 assert(f->fd == -1);
1279 assert(f->references);
1280 if (--f->references)
1283 if (should_fsync(td) && td->o.fsync_on_close)
1284 f_ret = fsync(f->fd);
1286 if (td->io_ops->close_file)
1287 ret = td->io_ops->close_file(td, f);
1292 td->nr_open_files--;
1293 fio_file_clear_open(f);
1294 assert(f->fd == -1);
1298 void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
1300 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1303 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1304 if (ddir == DDIR_READ)
1305 fio_rwlock_read(f->rwlock);
1307 fio_rwlock_write(f->rwlock);
1308 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1309 fio_mutex_down(f->lock);
1311 td->file_locks[f->fileno] = td->o.file_lock_mode;
1314 void unlock_file(struct thread_data *td, struct fio_file *f)
1316 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1319 if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1320 fio_rwlock_unlock(f->rwlock);
1321 else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1322 fio_mutex_up(f->lock);
1324 td->file_locks[f->fileno] = FILE_LOCK_NONE;
1327 void unlock_file_all(struct thread_data *td, struct fio_file *f)
1329 if (td->o.file_lock_mode == FILE_LOCK_NONE || !td->file_locks)
1331 if (td->file_locks[f->fileno] != FILE_LOCK_NONE)
1335 static int recurse_dir(struct thread_data *td, const char *dirname)
1341 D = opendir(dirname);
1343 char buf[FIO_VERROR_SIZE];
1345 snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
1346 td_verror(td, errno, buf);
1350 while ((dir = readdir(D)) != NULL) {
1351 char full_path[PATH_MAX];
1354 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1357 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
1359 if (lstat(full_path, &sb) == -1) {
1360 if (errno != ENOENT) {
1361 td_verror(td, errno, "stat");
1366 if (S_ISREG(sb.st_mode)) {
1367 add_file(td, full_path, 0, 1);
1370 if (!S_ISDIR(sb.st_mode))
1373 ret = recurse_dir(td, full_path);
1382 int add_dir_files(struct thread_data *td, const char *path)
1384 int ret = recurse_dir(td, path);
1387 log_info("fio: opendir added %d files\n", td->o.nr_files);
1392 void dup_files(struct thread_data *td, struct thread_data *org)
1397 dprint(FD_FILE, "dup files: %d\n", org->files_index);
1402 td->files = malloc(org->files_index * sizeof(f));
1404 if (td->o.file_lock_mode != FILE_LOCK_NONE)
1405 td->file_locks = malloc(org->files_index);
1407 for_each_file(org, f, i) {
1408 struct fio_file *__f;
1410 __f = smalloc(sizeof(*__f));
1412 log_err("fio: smalloc OOM\n");
1416 fio_file_reset(td, __f);
1419 __f->file_name = smalloc_strdup(f->file_name);
1420 if (!__f->file_name) {
1421 log_err("fio: smalloc OOM\n");
1425 __f->filetype = f->filetype;
1428 if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1429 __f->lock = f->lock;
1430 else if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1431 __f->rwlock = f->rwlock;
1438 * Returns the index that matches the filename, or -1 if not there
1440 int get_fileno(struct thread_data *td, const char *fname)
1445 for_each_file(td, f, i)
1446 if (!strcmp(f->file_name, fname))
1453 * For log usage, where we add/open/close files automatically
1455 void free_release_files(struct thread_data *td)
1458 td->files_index = 0;
1459 td->nr_normal_files = 0;
1462 void fio_file_reset(struct thread_data *td, struct fio_file *f)
1464 f->last_pos = f->file_offset;
1465 f->last_start = -1ULL;
1467 axmap_reset(f->io_axmap);
1468 if (td->o.random_generator == FIO_RAND_GEN_LFSR)
1469 lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
1472 int fio_files_done(struct thread_data *td)
1477 for_each_file(td, f, i)
1478 if (!fio_file_done(f))
1484 /* free memory used in initialization phase only */
1485 void filesetup_mem_free() {
1486 free_already_allocated();