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->runstate;
213 td_set_runstate(td, TD_PRE_READING);
215 bs = td->o.max_bs[DDIR_READ];
219 lseek(f->fd, f->file_offset, SEEK_SET);
222 while (left && !td->terminate) {
226 r = read(f->fd, b, bs);
232 td_verror(td, EIO, "pre_read");
237 td_set_runstate(td, old_runstate);
240 td->io_ops->close_file(td, f);
245 static unsigned long long get_rand_file_size(struct thread_data *td)
247 unsigned long long ret, sized;
250 if (td->o.use_os_rand) {
251 r = os_random_long(&td->file_size_state);
252 sized = td->o.file_size_high - td->o.file_size_low;
253 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
255 r = __rand(&td->__file_size_state);
256 sized = td->o.file_size_high - td->o.file_size_low;
257 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0)));
260 ret += td->o.file_size_low;
261 ret -= (ret % td->o.rw_min_bs);
265 static int file_size(struct thread_data *td, struct fio_file *f)
269 if (stat(f->file_name, &st) == -1) {
270 td_verror(td, errno, "fstat");
274 f->real_file_size = st.st_size;
278 static int bdev_size(struct thread_data *td, struct fio_file *f)
280 unsigned long long bytes = 0;
283 if (td->io_ops->open_file(td, f)) {
284 log_err("fio: failed opening blockdev %s for size check\n",
289 r = blockdev_size(f, &bytes);
291 td_verror(td, r, "blockdev_size");
296 log_err("%s: zero sized block device?\n", f->file_name);
300 f->real_file_size = bytes;
301 td->io_ops->close_file(td, f);
304 td->io_ops->close_file(td, f);
308 static int char_size(struct thread_data *td, struct fio_file *f)
310 #ifdef FIO_HAVE_CHARDEV_SIZE
311 unsigned long long bytes = 0;
314 if (td->io_ops->open_file(td, f)) {
315 log_err("fio: failed opening blockdev %s for size check\n",
320 r = chardev_size(f, &bytes);
322 td_verror(td, r, "chardev_size");
327 log_err("%s: zero sized char device?\n", f->file_name);
331 f->real_file_size = bytes;
332 td->io_ops->close_file(td, f);
335 td->io_ops->close_file(td, f);
338 f->real_file_size = -1ULL;
343 static int get_file_size(struct thread_data *td, struct fio_file *f)
347 if (fio_file_size_known(f))
350 if (f->filetype == FIO_TYPE_FILE)
351 ret = file_size(td, f);
352 else if (f->filetype == FIO_TYPE_BD)
353 ret = bdev_size(td, f);
354 else if (f->filetype == FIO_TYPE_CHAR)
355 ret = char_size(td, f);
357 f->real_file_size = -1;
362 if (f->file_offset > f->real_file_size) {
363 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
364 (unsigned long long) f->file_offset,
365 (unsigned long long) f->real_file_size);
369 fio_file_set_size_known(f);
373 static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
374 unsigned long long off,
375 unsigned long long len)
382 off = f->file_offset;
384 if (len == -1ULL || off == -1ULL)
387 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
391 * FIXME: add blockdev flushing too
394 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
396 if (f->filetype == FIO_TYPE_BD)
397 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
399 } else if (f->filetype == FIO_TYPE_FILE) {
400 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
401 } else if (f->filetype == FIO_TYPE_BD) {
402 ret = blockdev_invalidate_cache(f);
403 if (ret < 0 && errno == EACCES && geteuid()) {
405 log_err("fio: only root may flush block "
406 "devices. Cache flush bypassed!\n");
411 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
415 td_verror(td, errno, "invalidate_cache");
417 } else if (ret > 0) {
418 td_verror(td, ret, "invalidate_cache");
426 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
428 if (!fio_file_open(f))
431 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
434 int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
438 dprint(FD_FILE, "fd close %s\n", f->file_name);
442 if (close(f->fd) < 0)
447 if (f->shadow_fd != -1) {
456 int file_lookup_open(struct fio_file *f, int flags)
458 struct fio_file *__f;
461 __f = lookup_file_hash(f->file_name);
463 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
465 * racy, need the __f->lock locked
470 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
474 f->fd = open(f->file_name, flags, 0600);
478 static int file_close_shadow_fds(struct thread_data *td)
484 for_each_file(td, f, i) {
485 if (f->shadow_fd == -1)
496 int generic_open_file(struct thread_data *td, struct fio_file *f)
502 dprint(FD_FILE, "fd open %s\n", f->file_name);
504 if (td_trim(td) && f->filetype != FIO_TYPE_BD) {
505 log_err("fio: trim only applies to block device\n");
509 if (!strcmp(f->file_name, "-")) {
511 log_err("fio: can't read/write to stdin/out\n");
517 * move output logging to stderr, if we are writing to stdout
526 flags |= OS_O_DIRECT;
529 td_verror(td, EINVAL, "OS does not support atomic IO");
532 flags |= OS_O_DIRECT | FIO_O_ATOMIC;
536 if (td->o.create_on_open)
539 if (f->filetype != FIO_TYPE_FILE)
540 flags |= FIO_O_NOATIME;
547 if (f->filetype == FIO_TYPE_FILE)
551 f->fd = dup(STDOUT_FILENO);
553 from_hash = file_lookup_open(f, flags);
554 } else if (td_read(td)) {
555 if (f->filetype == FIO_TYPE_CHAR && !read_only)
561 f->fd = dup(STDIN_FILENO);
563 from_hash = file_lookup_open(f, flags);
566 from_hash = file_lookup_open(f, flags);
570 char buf[FIO_VERROR_SIZE];
573 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
574 flags &= ~FIO_O_NOATIME;
577 if (__e == EMFILE && file_close_shadow_fds(td))
580 snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
582 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
583 log_err("fio: looks like your file system does not " \
584 "support direct=1/buffered=0\n");
587 td_verror(td, __e, buf);
590 if (!from_hash && f->fd != -1) {
591 if (add_file_hash(f)) {
595 * Stash away descriptor for later close. This is to
596 * work-around a "feature" on Linux, where a close of
597 * an fd that has been opened for write will trigger
598 * udev to call blkid to check partitions, fs id, etc.
599 * That pollutes the device cache, which can slow down
600 * unbuffered accesses.
602 if (f->shadow_fd == -1)
603 f->shadow_fd = f->fd;
606 * OK to ignore, we haven't done anything
609 ret = generic_close_file(td, f);
618 int generic_get_file_size(struct thread_data *td, struct fio_file *f)
620 return get_file_size(td, f);
624 * open/close all files, so that ->real_file_size gets set
626 static int get_file_sizes(struct thread_data *td)
632 for_each_file(td, f, i) {
633 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
636 if (td_io_get_file_size(td, f)) {
637 if (td->error != ENOENT) {
638 log_err("%s\n", td->verror);
644 if (f->real_file_size == -1ULL && td->o.size)
645 f->real_file_size = td->o.size / td->o.nr_files;
652 struct flist_head list;
659 * Get free number of bytes for each file on each unique mount.
661 static unsigned long long get_fs_free_counts(struct thread_data *td)
663 struct flist_head *n, *tmp;
664 unsigned long long ret = 0;
665 struct fio_mount *fm;
670 for_each_file(td, f, i) {
674 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
675 if (f->real_file_size != -1ULL)
676 ret += f->real_file_size;
678 } else if (f->filetype != FIO_TYPE_FILE)
681 strcpy(buf, f->file_name);
683 if (stat(buf, &sb) < 0) {
687 if (stat(buf, &sb) < 0)
692 flist_for_each(n, &list) {
693 fm = flist_entry(n, struct fio_mount, list);
694 if (fm->key == sb.st_dev)
703 fm = malloc(sizeof(*fm));
704 strcpy(fm->__base, buf);
705 fm->base = basename(fm->__base);
707 flist_add(&fm->list, &list);
710 flist_for_each_safe(n, tmp, &list) {
711 unsigned long long sz;
713 fm = flist_entry(n, struct fio_mount, list);
714 flist_del(&fm->list);
716 sz = get_fs_size(fm->base);
717 if (sz && sz != -1ULL)
726 uint64_t get_start_offset(struct thread_data *td)
728 return td->o.start_offset +
729 (td->thread_number - 1) * td->o.offset_increment;
733 * Open the files and setup files sizes, creating files if necessary.
735 int setup_files(struct thread_data *td)
737 unsigned long long total_size, extend_size;
738 struct thread_options *o = &td->o;
740 unsigned int i, nr_fs_extra = 0;
741 int err = 0, need_extend;
743 const unsigned int bs = td_min_bs(td);
746 dprint(FD_FILE, "setup files\n");
748 old_state = td->runstate;
749 td_set_runstate(td, TD_SETTING_UP);
751 if (o->read_iolog_file)
755 * if ioengine defines a setup() method, it's responsible for
756 * opening the files and setting f->real_file_size to indicate
757 * the valid range for that file.
759 if (td->io_ops->setup)
760 err = td->io_ops->setup(td);
762 err = get_file_sizes(td);
768 * check sizes. if the files/devices do not exist and the size
769 * isn't passed to fio, abort.
772 for_each_file(td, f, i) {
773 if (f->real_file_size == -1ULL)
776 total_size += f->real_file_size;
780 td->fill_device_size = get_fs_free_counts(td);
783 * device/file sizes are zero and no size given, punt
785 if ((!total_size || total_size == -1ULL) && !o->size &&
786 !(td->io_ops->flags & FIO_NOIO) && !o->fill_device &&
787 !(o->nr_files && (o->file_size_low || o->file_size_high))) {
788 log_err("%s: you need to specify size=\n", o->name);
789 td_verror(td, EINVAL, "total_file_size");
794 * Calculate per-file size and potential extra size for the
795 * first files, if needed.
797 if (!o->file_size_low) {
800 fs = o->size / o->nr_files;
801 all_fs = fs * o->nr_files;
803 if (all_fs < o->size)
804 nr_fs_extra = (o->size - all_fs) / bs;
808 * now file sizes are known, so we can set ->io_size. if size= is
809 * not given, ->io_size is just equal to ->real_file_size. if size
810 * is given, ->io_size is size / nr_files.
812 extend_size = total_size = 0;
814 for_each_file(td, f, i) {
815 f->file_offset = get_start_offset(td);
817 if (!o->file_size_low) {
819 * no file size range given, file size is equal to
820 * total size divided by number of files. If that is
821 * zero, set it to the real file size. If the size
822 * doesn't divide nicely with the min blocksize,
823 * make the first files bigger.
832 f->io_size = f->real_file_size - f->file_offset;
833 } else if (f->real_file_size < o->file_size_low ||
834 f->real_file_size > o->file_size_high) {
835 if (f->file_offset > o->file_size_low)
838 * file size given. if it's fixed, use that. if it's a
839 * range, generate a random size in-between.
841 if (o->file_size_low == o->file_size_high)
842 f->io_size = o->file_size_low - f->file_offset;
844 f->io_size = get_rand_file_size(td)
848 f->io_size = f->real_file_size - f->file_offset;
850 if (f->io_size == -1ULL)
854 f->io_size = (f->io_size * o->size_percent) / 100;
855 total_size += f->io_size;
858 if (f->filetype == FIO_TYPE_FILE &&
859 (f->io_size + f->file_offset) > f->real_file_size &&
860 !(td->io_ops->flags & FIO_DISKLESSIO)) {
861 if (!o->create_on_open) {
863 extend_size += (f->io_size + f->file_offset);
865 f->real_file_size = f->io_size + f->file_offset;
866 fio_file_set_extend(f);
870 if (!o->size || o->size > total_size)
871 o->size = total_size;
874 * See if we need to extend some files
878 if (output_format == FIO_OUTPUT_NORMAL)
879 log_info("%s: Laying out IO file(s) (%u file(s) /"
880 " %lluMB)\n", o->name, need_extend,
883 for_each_file(td, f, i) {
884 unsigned long long old_len = -1ULL, extend_len = -1ULL;
886 if (!fio_file_extend(f))
889 assert(f->filetype == FIO_TYPE_FILE);
890 fio_file_clear_extend(f);
891 if (!o->fill_device) {
892 old_len = f->real_file_size;
893 extend_len = f->io_size + f->file_offset -
896 f->real_file_size = (f->io_size + f->file_offset);
897 err = extend_file(td, f);
901 err = __file_invalidate_cache(td, f, old_len,
915 o->zone_size = o->size;
918 * iolog already set the total io size, if we read back
921 if (!o->read_iolog_file)
922 td->total_io_size = o->size * o->loops;
928 td_set_runstate(td, old_state);
931 log_err("%s: you need to specify valid offset=\n", o->name);
933 td_set_runstate(td, old_state);
937 int pre_read_files(struct thread_data *td)
942 dprint(FD_FILE, "pre_read files\n");
944 for_each_file(td, f, i) {
945 pre_read_file(td, f);
951 static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
953 unsigned int range_size, seed;
954 unsigned long nranges;
957 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
958 file_size = min(f->real_file_size, f->io_size);
960 nranges = (file_size + range_size - 1) / range_size;
962 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
963 if (!td->o.rand_repeatable)
964 seed = td->rand_seeds[4];
966 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
967 zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed);
969 pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed);
974 static int init_rand_distribution(struct thread_data *td)
980 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
983 state = td->runstate;
984 td_set_runstate(td, TD_SETTING_UP);
985 for_each_file(td, f, i)
986 __init_rand_distribution(td, f);
987 td_set_runstate(td, state);
992 int init_random_map(struct thread_data *td)
994 unsigned long long blocks;
998 if (init_rand_distribution(td))
1003 for_each_file(td, f, i) {
1004 uint64_t file_size = min(f->real_file_size, f->io_size);
1006 blocks = file_size / (unsigned long long) td->o.rw_min_bs;
1008 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
1011 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
1013 if (!lfsr_init(&f->lfsr, blocks, seed, 0))
1015 } else if (!td->o.norandommap) {
1016 f->io_axmap = axmap_new(blocks);
1019 } else if (td->o.norandommap)
1022 if (!td->o.softrandommap) {
1023 log_err("fio: failed allocating random map. If running"
1024 " a large number of jobs, try the 'norandommap'"
1025 " option or set 'softrandommap'. Or give"
1026 " a larger --alloc-size to fio.\n");
1030 log_info("fio: file %s failed allocating random map. Running "
1031 "job without.\n", f->file_name);
1037 void close_files(struct thread_data *td)
1042 for_each_file(td, f, i) {
1043 if (fio_file_open(f))
1044 td_io_close_file(td, f);
1048 void close_and_free_files(struct thread_data *td)
1053 dprint(FD_FILE, "close files\n");
1055 for_each_file(td, f, i) {
1056 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1057 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1058 unlink(f->file_name);
1061 if (fio_file_open(f))
1062 td_io_close_file(td, f);
1064 remove_file_hash(f);
1066 sfree(f->file_name);
1067 f->file_name = NULL;
1068 axmap_free(f->io_axmap);
1073 td->o.filename = NULL;
1075 free(td->file_locks);
1076 td->files_index = 0;
1078 td->file_locks = NULL;
1079 td->o.file_lock_mode = FILE_LOCK_NONE;
1083 static void get_file_type(struct fio_file *f)
1087 if (!strcmp(f->file_name, "-"))
1088 f->filetype = FIO_TYPE_PIPE;
1090 f->filetype = FIO_TYPE_FILE;
1092 /* \\.\ is the device namespace in Windows, where every file is
1094 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1095 f->filetype = FIO_TYPE_BD;
1097 if (!stat(f->file_name, &sb)) {
1098 if (S_ISBLK(sb.st_mode))
1099 f->filetype = FIO_TYPE_BD;
1100 else if (S_ISCHR(sb.st_mode))
1101 f->filetype = FIO_TYPE_CHAR;
1102 else if (S_ISFIFO(sb.st_mode))
1103 f->filetype = FIO_TYPE_PIPE;
1107 static void set_already_allocated(const char *fname) {
1108 struct file_name *fn;
1110 fn = malloc(sizeof(struct file_name));
1111 fn->filename = strdup(fname);
1112 flist_add_tail(&fn->list, &filename_list);
1115 static int is_already_allocated(const char *fname)
1117 struct flist_head *entry;
1120 if (!flist_empty(&filename_list))
1122 flist_for_each(entry, &filename_list) {
1123 filename = flist_entry(entry, struct file_name, list)->filename;
1125 if (strcmp(filename, fname) == 0)
1133 static void free_already_allocated() {
1134 struct flist_head *entry, *tmp;
1135 struct file_name *fn;
1137 if (!flist_empty(&filename_list))
1139 flist_for_each_safe(entry, tmp, &filename_list) {
1140 fn = flist_entry(entry, struct file_name, list);
1142 flist_del(&fn->list);
1148 int add_file(struct thread_data *td, const char *fname, int numjob, int inc)
1150 int cur_files = td->files_index;
1151 char file_name[PATH_MAX];
1155 dprint(FD_FILE, "add file %s\n", fname);
1157 if (td->o.directory)
1158 len = set_name_idx(file_name, td->o.directory, numjob);
1160 sprintf(file_name + len, "%s", fname);
1162 /* clean cloned siblings using existing files */
1163 if (numjob && is_already_allocated(file_name))
1166 f = smalloc(sizeof(*f));
1168 log_err("fio: smalloc OOM\n");
1174 fio_file_reset(td, f);
1176 if (td->files_size <= td->files_index) {
1177 unsigned int new_size = td->o.nr_files + 1;
1179 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1181 td->files = realloc(td->files, new_size * sizeof(f));
1182 if (td->files == NULL) {
1183 log_err("fio: realloc OOM\n");
1186 if (td->o.file_lock_mode != FILE_LOCK_NONE) {
1187 td->file_locks = realloc(td->file_locks, new_size);
1188 if (!td->file_locks) {
1189 log_err("fio: realloc OOM\n");
1192 td->file_locks[cur_files] = FILE_LOCK_NONE;
1194 td->files_size = new_size;
1196 td->files[cur_files] = f;
1197 f->fileno = cur_files;
1200 * init function, io engine may not be loaded yet
1202 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1203 f->real_file_size = -1ULL;
1205 f->file_name = smalloc_strdup(file_name);
1206 if (!f->file_name) {
1207 log_err("fio: smalloc OOM\n");
1213 switch (td->o.file_lock_mode) {
1214 case FILE_LOCK_NONE:
1216 case FILE_LOCK_READWRITE:
1217 f->rwlock = fio_rwlock_init();
1219 case FILE_LOCK_EXCLUSIVE:
1220 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
1223 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1228 if (f->filetype == FIO_TYPE_FILE)
1229 td->nr_normal_files++;
1231 set_already_allocated(file_name);
1234 * For adding files after the fact - if openfiles= isn't
1235 * given as an option, ensure we allow at least one file open
1237 if (!td->o.open_files)
1238 td->o.open_files = 1;
1243 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1249 int add_file_exclusive(struct thread_data *td, const char *fname)
1254 for_each_file(td, f, i) {
1255 if (!strcmp(f->file_name, fname))
1259 return add_file(td, fname, 0, 1);
1262 void get_file(struct fio_file *f)
1264 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
1265 assert(fio_file_open(f));
1269 int put_file(struct thread_data *td, struct fio_file *f)
1271 int f_ret = 0, ret = 0;
1273 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
1275 if (!fio_file_open(f)) {
1276 assert(f->fd == -1);
1280 assert(f->references);
1281 if (--f->references)
1284 if (should_fsync(td) && td->o.fsync_on_close)
1285 f_ret = fsync(f->fd);
1287 if (td->io_ops->close_file)
1288 ret = td->io_ops->close_file(td, f);
1293 td->nr_open_files--;
1294 fio_file_clear_open(f);
1295 assert(f->fd == -1);
1299 void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
1301 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1304 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1305 if (ddir == DDIR_READ)
1306 fio_rwlock_read(f->rwlock);
1308 fio_rwlock_write(f->rwlock);
1309 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1310 fio_mutex_down(f->lock);
1312 td->file_locks[f->fileno] = td->o.file_lock_mode;
1315 void unlock_file(struct thread_data *td, struct fio_file *f)
1317 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1320 if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1321 fio_rwlock_unlock(f->rwlock);
1322 else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1323 fio_mutex_up(f->lock);
1325 td->file_locks[f->fileno] = FILE_LOCK_NONE;
1328 void unlock_file_all(struct thread_data *td, struct fio_file *f)
1330 if (td->o.file_lock_mode == FILE_LOCK_NONE || !td->file_locks)
1332 if (td->file_locks[f->fileno] != FILE_LOCK_NONE)
1336 static int recurse_dir(struct thread_data *td, const char *dirname)
1342 D = opendir(dirname);
1344 char buf[FIO_VERROR_SIZE];
1346 snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
1347 td_verror(td, errno, buf);
1351 while ((dir = readdir(D)) != NULL) {
1352 char full_path[PATH_MAX];
1355 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1358 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
1360 if (lstat(full_path, &sb) == -1) {
1361 if (errno != ENOENT) {
1362 td_verror(td, errno, "stat");
1367 if (S_ISREG(sb.st_mode)) {
1368 add_file(td, full_path, 0, 1);
1371 if (!S_ISDIR(sb.st_mode))
1374 ret = recurse_dir(td, full_path);
1383 int add_dir_files(struct thread_data *td, const char *path)
1385 int ret = recurse_dir(td, path);
1388 log_info("fio: opendir added %d files\n", td->o.nr_files);
1393 void dup_files(struct thread_data *td, struct thread_data *org)
1398 dprint(FD_FILE, "dup files: %d\n", org->files_index);
1403 td->files = malloc(org->files_index * sizeof(f));
1405 if (td->o.file_lock_mode != FILE_LOCK_NONE)
1406 td->file_locks = malloc(org->files_index);
1408 for_each_file(org, f, i) {
1409 struct fio_file *__f;
1411 __f = smalloc(sizeof(*__f));
1413 log_err("fio: smalloc OOM\n");
1417 fio_file_reset(td, __f);
1420 __f->file_name = smalloc_strdup(f->file_name);
1421 if (!__f->file_name) {
1422 log_err("fio: smalloc OOM\n");
1426 __f->filetype = f->filetype;
1429 if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1430 __f->lock = f->lock;
1431 else if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1432 __f->rwlock = f->rwlock;
1439 * Returns the index that matches the filename, or -1 if not there
1441 int get_fileno(struct thread_data *td, const char *fname)
1446 for_each_file(td, f, i)
1447 if (!strcmp(f->file_name, fname))
1454 * For log usage, where we add/open/close files automatically
1456 void free_release_files(struct thread_data *td)
1459 td->files_index = 0;
1460 td->nr_normal_files = 0;
1463 void fio_file_reset(struct thread_data *td, struct fio_file *f)
1465 f->last_pos = f->file_offset;
1466 f->last_start = -1ULL;
1468 axmap_reset(f->io_axmap);
1469 if (td->o.random_generator == FIO_RAND_GEN_LFSR)
1470 lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
1473 int fio_files_done(struct thread_data *td)
1478 for_each_file(td, f, i)
1479 if (!fio_file_done(f))
1485 /* free memory used in initialization phase only */
1486 void filesetup_mem_free() {
1487 free_already_allocated();