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
54 (td_write(td) && td->o.overwrite && !td->o.file_append) ||
55 (td_write(td) && td->io_ops->flags & FIO_NOEXTEND))
57 if (td_write(td) && !td->o.overwrite && !td->o.file_append)
60 if (unlink_file || new_layout) {
61 dprint(FD_FILE, "layout unlink %s\n", f->file_name);
62 if ((unlink(f->file_name) < 0) && (errno != ENOENT)) {
63 td_verror(td, errno, "unlink");
68 flags = O_WRONLY | O_CREAT;
72 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
73 f->fd = open(f->file_name, flags, 0644);
75 td_verror(td, errno, "open");
79 #ifdef CONFIG_POSIX_FALLOCATE
80 if (!td->o.fill_device) {
81 switch (td->o.fallocate_mode) {
82 case FIO_FALLOCATE_NONE:
84 case FIO_FALLOCATE_POSIX:
85 dprint(FD_FILE, "posix_fallocate file %s size %llu\n",
87 (unsigned long long) f->real_file_size);
89 r = posix_fallocate(f->fd, 0, f->real_file_size);
91 log_err("fio: posix_fallocate fails: %s\n",
95 #ifdef CONFIG_LINUX_FALLOCATE
96 case FIO_FALLOCATE_KEEP_SIZE:
98 "fallocate(FALLOC_FL_KEEP_SIZE) "
99 "file %s size %llu\n", f->file_name,
100 (unsigned long long) f->real_file_size);
102 r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0,
105 td_verror(td, errno, "fallocate");
108 #endif /* CONFIG_LINUX_FALLOCATE */
110 log_err("fio: unknown fallocate mode: %d\n",
111 td->o.fallocate_mode);
115 #endif /* CONFIG_POSIX_FALLOCATE */
121 * The size will be -1ULL when fill_device is used, so don't truncate
122 * or fallocate this file, just write it
124 if (!td->o.fill_device) {
125 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
126 (unsigned long long) f->real_file_size);
127 if (ftruncate(f->fd, f->real_file_size) == -1) {
128 if (errno != EFBIG) {
129 td_verror(td, errno, "ftruncate");
135 b = malloc(td->o.max_bs[DDIR_WRITE]);
137 left = f->real_file_size;
138 while (left && !td->terminate) {
139 bs = td->o.max_bs[DDIR_WRITE];
143 fill_io_buffer(td, b, bs, bs);
145 r = write(f->fd, b, bs);
155 if (td->o.fill_device)
157 log_info("fio: ENOSPC on laying out "
161 td_verror(td, errno, "write");
163 td_verror(td, EIO, "write");
170 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
171 unlink(f->file_name);
172 } else if (td->o.create_fsync) {
173 if (fsync(f->fd) < 0) {
174 td_verror(td, errno, "fsync");
178 if (td->o.fill_device && !td_write(td)) {
179 fio_file_clear_size_known(f);
180 if (td_io_get_file_size(td, f))
182 if (f->io_size > f->real_file_size)
183 f->io_size = f->real_file_size;
195 static int pre_read_file(struct thread_data *td, struct fio_file *f)
197 int ret = 0, r, did_open = 0, old_runstate;
198 unsigned long long left;
202 if (td->io_ops->flags & FIO_PIPEIO)
205 if (!fio_file_open(f)) {
206 if (td->io_ops->open_file(td, f)) {
207 log_err("fio: cannot pre-read, failed to open file\n");
213 old_runstate = td_bump_runstate(td, TD_PRE_READING);
215 bs = td->o.max_bs[DDIR_READ];
219 if (lseek(f->fd, f->file_offset, SEEK_SET) < 0) {
220 td_verror(td, errno, "lseek");
221 log_err("fio: failed to lseek pre-read file\n");
228 while (left && !td->terminate) {
232 r = read(f->fd, b, bs);
238 td_verror(td, EIO, "pre_read");
244 td_restore_runstate(td, old_runstate);
247 td->io_ops->close_file(td, f);
253 static unsigned long long get_rand_file_size(struct thread_data *td)
255 unsigned long long ret, sized;
258 if (td->o.use_os_rand) {
259 r = os_random_long(&td->file_size_state);
260 sized = td->o.file_size_high - td->o.file_size_low;
261 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
263 r = __rand(&td->__file_size_state);
264 sized = td->o.file_size_high - td->o.file_size_low;
265 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0)));
268 ret += td->o.file_size_low;
269 ret -= (ret % td->o.rw_min_bs);
273 static int file_size(struct thread_data *td, struct fio_file *f)
277 if (stat(f->file_name, &st) == -1) {
278 td_verror(td, errno, "fstat");
282 f->real_file_size = st.st_size;
286 static int bdev_size(struct thread_data *td, struct fio_file *f)
288 unsigned long long bytes = 0;
291 if (td->io_ops->open_file(td, f)) {
292 log_err("fio: failed opening blockdev %s for size check\n",
297 r = blockdev_size(f, &bytes);
299 td_verror(td, r, "blockdev_size");
304 log_err("%s: zero sized block device?\n", f->file_name);
308 f->real_file_size = bytes;
309 td->io_ops->close_file(td, f);
312 td->io_ops->close_file(td, f);
316 static int char_size(struct thread_data *td, struct fio_file *f)
318 #ifdef FIO_HAVE_CHARDEV_SIZE
319 unsigned long long bytes = 0;
322 if (td->io_ops->open_file(td, f)) {
323 log_err("fio: failed opening blockdev %s for size check\n",
328 r = chardev_size(f, &bytes);
330 td_verror(td, r, "chardev_size");
335 log_err("%s: zero sized char device?\n", f->file_name);
339 f->real_file_size = bytes;
340 td->io_ops->close_file(td, f);
343 td->io_ops->close_file(td, f);
346 f->real_file_size = -1ULL;
351 static int get_file_size(struct thread_data *td, struct fio_file *f)
355 if (fio_file_size_known(f))
358 if (f->filetype == FIO_TYPE_FILE)
359 ret = file_size(td, f);
360 else if (f->filetype == FIO_TYPE_BD)
361 ret = bdev_size(td, f);
362 else if (f->filetype == FIO_TYPE_CHAR)
363 ret = char_size(td, f);
365 f->real_file_size = -1;
370 if (f->file_offset > f->real_file_size) {
371 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
372 (unsigned long long) f->file_offset,
373 (unsigned long long) f->real_file_size);
377 fio_file_set_size_known(f);
381 static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
382 unsigned long long off,
383 unsigned long long len)
390 off = f->file_offset;
392 if (len == -1ULL || off == -1ULL)
395 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
399 * FIXME: add blockdev flushing too
402 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
404 if (f->filetype == FIO_TYPE_BD)
405 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
407 } else if (f->filetype == FIO_TYPE_FILE) {
408 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
409 } else if (f->filetype == FIO_TYPE_BD) {
410 ret = blockdev_invalidate_cache(f);
411 if (ret < 0 && errno == EACCES && geteuid()) {
413 log_err("fio: only root may flush block "
414 "devices. Cache flush bypassed!\n");
419 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
423 td_verror(td, errno, "invalidate_cache");
425 } else if (ret > 0) {
426 td_verror(td, ret, "invalidate_cache");
434 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
436 if (!fio_file_open(f))
439 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
442 int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
446 dprint(FD_FILE, "fd close %s\n", f->file_name);
450 if (close(f->fd) < 0)
455 if (f->shadow_fd != -1) {
464 int file_lookup_open(struct fio_file *f, int flags)
466 struct fio_file *__f;
469 __f = lookup_file_hash(f->file_name);
471 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
473 * racy, need the __f->lock locked
478 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
482 f->fd = open(f->file_name, flags, 0600);
486 static int file_close_shadow_fds(struct thread_data *td)
492 for_each_file(td, f, i) {
493 if (f->shadow_fd == -1)
504 int generic_open_file(struct thread_data *td, struct fio_file *f)
510 dprint(FD_FILE, "fd open %s\n", f->file_name);
512 if (td_trim(td) && f->filetype != FIO_TYPE_BD) {
513 log_err("fio: trim only applies to block device\n");
517 if (!strcmp(f->file_name, "-")) {
519 log_err("fio: can't read/write to stdin/out\n");
525 * move output logging to stderr, if we are writing to stdout
534 flags |= OS_O_DIRECT;
537 td_verror(td, EINVAL, "OS does not support atomic IO");
540 flags |= OS_O_DIRECT | FIO_O_ATOMIC;
544 if (td->o.create_on_open)
547 if (f->filetype != FIO_TYPE_FILE)
548 flags |= FIO_O_NOATIME;
555 if (f->filetype == FIO_TYPE_FILE)
559 f->fd = dup(STDOUT_FILENO);
561 from_hash = file_lookup_open(f, flags);
562 } else if (td_read(td)) {
563 if (f->filetype == FIO_TYPE_CHAR && !read_only)
569 f->fd = dup(STDIN_FILENO);
571 from_hash = file_lookup_open(f, flags);
574 from_hash = file_lookup_open(f, flags);
578 char buf[FIO_VERROR_SIZE];
581 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
582 flags &= ~FIO_O_NOATIME;
585 if (__e == EMFILE && file_close_shadow_fds(td))
588 snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
590 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
591 log_err("fio: looks like your file system does not " \
592 "support direct=1/buffered=0\n");
595 td_verror(td, __e, buf);
598 if (!from_hash && f->fd != -1) {
599 if (add_file_hash(f)) {
603 * Stash away descriptor for later close. This is to
604 * work-around a "feature" on Linux, where a close of
605 * an fd that has been opened for write will trigger
606 * udev to call blkid to check partitions, fs id, etc.
607 * That pollutes the device cache, which can slow down
608 * unbuffered accesses.
610 if (f->shadow_fd == -1)
611 f->shadow_fd = f->fd;
614 * OK to ignore, we haven't done anything
617 ret = generic_close_file(td, f);
626 int generic_get_file_size(struct thread_data *td, struct fio_file *f)
628 return get_file_size(td, f);
632 * open/close all files, so that ->real_file_size gets set
634 static int get_file_sizes(struct thread_data *td)
640 for_each_file(td, f, i) {
641 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
644 if (td_io_get_file_size(td, f)) {
645 if (td->error != ENOENT) {
646 log_err("%s\n", td->verror);
652 if (f->real_file_size == -1ULL && td->o.size)
653 f->real_file_size = td->o.size / td->o.nr_files;
660 struct flist_head list;
667 * Get free number of bytes for each file on each unique mount.
669 static unsigned long long get_fs_free_counts(struct thread_data *td)
671 struct flist_head *n, *tmp;
672 unsigned long long ret = 0;
673 struct fio_mount *fm;
678 for_each_file(td, f, i) {
682 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
683 if (f->real_file_size != -1ULL)
684 ret += f->real_file_size;
686 } else if (f->filetype != FIO_TYPE_FILE)
689 strcpy(buf, f->file_name);
691 if (stat(buf, &sb) < 0) {
695 if (stat(buf, &sb) < 0)
700 flist_for_each(n, &list) {
701 fm = flist_entry(n, struct fio_mount, list);
702 if (fm->key == sb.st_dev)
711 fm = malloc(sizeof(*fm));
712 strcpy(fm->__base, buf);
713 fm->base = basename(fm->__base);
715 flist_add(&fm->list, &list);
718 flist_for_each_safe(n, tmp, &list) {
719 unsigned long long sz;
721 fm = flist_entry(n, struct fio_mount, list);
722 flist_del(&fm->list);
724 sz = get_fs_size(fm->base);
725 if (sz && sz != -1ULL)
734 uint64_t get_start_offset(struct thread_data *td, struct fio_file *f)
736 struct thread_options *o = &td->o;
738 if (o->file_append && f->filetype == FIO_TYPE_FILE)
739 return f->real_file_size;
741 return td->o.start_offset +
742 (td->thread_number - 1) * td->o.offset_increment;
746 * Open the files and setup files sizes, creating files if necessary.
748 int setup_files(struct thread_data *td)
750 unsigned long long total_size, extend_size;
751 struct thread_options *o = &td->o;
753 unsigned int i, nr_fs_extra = 0;
754 int err = 0, need_extend;
756 const unsigned int bs = td_min_bs(td);
759 dprint(FD_FILE, "setup files\n");
761 old_state = td_bump_runstate(td, TD_SETTING_UP);
763 if (o->read_iolog_file)
767 * if ioengine defines a setup() method, it's responsible for
768 * opening the files and setting f->real_file_size to indicate
769 * the valid range for that file.
771 if (td->io_ops->setup)
772 err = td->io_ops->setup(td);
774 err = get_file_sizes(td);
780 * check sizes. if the files/devices do not exist and the size
781 * isn't passed to fio, abort.
784 for_each_file(td, f, i) {
785 if (f->real_file_size == -1ULL)
788 total_size += f->real_file_size;
792 td->fill_device_size = get_fs_free_counts(td);
795 * device/file sizes are zero and no size given, punt
797 if ((!total_size || total_size == -1ULL) && !o->size &&
798 !(td->io_ops->flags & FIO_NOIO) && !o->fill_device &&
799 !(o->nr_files && (o->file_size_low || o->file_size_high))) {
800 log_err("%s: you need to specify size=\n", o->name);
801 td_verror(td, EINVAL, "total_file_size");
806 * Calculate per-file size and potential extra size for the
807 * first files, if needed.
809 if (!o->file_size_low && o->nr_files) {
812 fs = o->size / o->nr_files;
813 all_fs = fs * o->nr_files;
815 if (all_fs < o->size)
816 nr_fs_extra = (o->size - all_fs) / bs;
820 * now file sizes are known, so we can set ->io_size. if size= is
821 * not given, ->io_size is just equal to ->real_file_size. if size
822 * is given, ->io_size is size / nr_files.
824 extend_size = total_size = 0;
826 for_each_file(td, f, i) {
827 f->file_offset = get_start_offset(td, f);
829 if (!o->file_size_low) {
831 * no file size range given, file size is equal to
832 * total size divided by number of files. If that is
833 * zero, set it to the real file size. If the size
834 * doesn't divide nicely with the min blocksize,
835 * make the first files bigger.
844 f->io_size = f->real_file_size - f->file_offset;
845 } else if (f->real_file_size < o->file_size_low ||
846 f->real_file_size > o->file_size_high) {
847 if (f->file_offset > o->file_size_low)
850 * file size given. if it's fixed, use that. if it's a
851 * range, generate a random size in-between.
853 if (o->file_size_low == o->file_size_high)
854 f->io_size = o->file_size_low - f->file_offset;
856 f->io_size = get_rand_file_size(td)
860 f->io_size = f->real_file_size - f->file_offset;
862 if (f->io_size == -1ULL)
866 f->io_size = (f->io_size * o->size_percent) / 100;
867 total_size += f->io_size;
870 if (f->filetype == FIO_TYPE_FILE &&
871 (f->io_size + f->file_offset) > f->real_file_size &&
872 !(td->io_ops->flags & FIO_DISKLESSIO)) {
873 if (!o->create_on_open) {
875 extend_size += (f->io_size + f->file_offset);
877 f->real_file_size = f->io_size + f->file_offset;
878 fio_file_set_extend(f);
882 if (!o->size || o->size > total_size)
883 o->size = total_size;
886 * See if we need to extend some files
890 if (output_format == FIO_OUTPUT_NORMAL)
891 log_info("%s: Laying out IO file(s) (%u file(s) /"
892 " %lluMB)\n", o->name, need_extend,
895 for_each_file(td, f, i) {
896 unsigned long long old_len = -1ULL, extend_len = -1ULL;
898 if (!fio_file_extend(f))
901 assert(f->filetype == FIO_TYPE_FILE);
902 fio_file_clear_extend(f);
903 if (!o->fill_device) {
904 old_len = f->real_file_size;
905 extend_len = f->io_size + f->file_offset -
908 f->real_file_size = (f->io_size + f->file_offset);
909 err = extend_file(td, f);
913 err = __file_invalidate_cache(td, f, old_len,
927 o->zone_size = o->size;
930 * iolog already set the total io size, if we read back
933 if (!o->read_iolog_file)
934 td->total_io_size = o->size * o->loops;
940 td_restore_runstate(td, old_state);
943 log_err("%s: you need to specify valid offset=\n", o->name);
945 td_restore_runstate(td, old_state);
949 int pre_read_files(struct thread_data *td)
954 dprint(FD_FILE, "pre_read files\n");
956 for_each_file(td, f, i) {
957 pre_read_file(td, f);
963 static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
965 unsigned int range_size, seed;
966 unsigned long nranges;
969 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
970 file_size = min(f->real_file_size, f->io_size);
972 nranges = (file_size + range_size - 1) / range_size;
974 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
975 if (!td->o.rand_repeatable)
976 seed = td->rand_seeds[4];
978 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
979 zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed);
981 pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed);
986 static int init_rand_distribution(struct thread_data *td)
992 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
995 state = td_bump_runstate(td, TD_SETTING_UP);
997 for_each_file(td, f, i)
998 __init_rand_distribution(td, f);
1000 td_restore_runstate(td, state);
1005 int init_random_map(struct thread_data *td)
1007 unsigned long long blocks;
1011 if (init_rand_distribution(td))
1016 for_each_file(td, f, i) {
1017 uint64_t file_size = min(f->real_file_size, f->io_size);
1019 blocks = file_size / (unsigned long long) td->o.rw_min_bs;
1021 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
1024 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
1026 if (!lfsr_init(&f->lfsr, blocks, seed, 0))
1028 } else if (!td->o.norandommap) {
1029 f->io_axmap = axmap_new(blocks);
1032 } else if (td->o.norandommap)
1035 if (!td->o.softrandommap) {
1036 log_err("fio: failed allocating random map. If running"
1037 " a large number of jobs, try the 'norandommap'"
1038 " option or set 'softrandommap'. Or give"
1039 " a larger --alloc-size to fio.\n");
1043 log_info("fio: file %s failed allocating random map. Running "
1044 "job without.\n", f->file_name);
1050 void close_files(struct thread_data *td)
1055 for_each_file(td, f, i) {
1056 if (fio_file_open(f))
1057 td_io_close_file(td, f);
1061 void close_and_free_files(struct thread_data *td)
1066 dprint(FD_FILE, "close files\n");
1068 for_each_file(td, f, i) {
1069 if (fio_file_open(f))
1070 td_io_close_file(td, f);
1072 remove_file_hash(f);
1074 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1075 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1076 unlink(f->file_name);
1079 sfree(f->file_name);
1080 f->file_name = NULL;
1081 axmap_free(f->io_axmap);
1086 td->o.filename = NULL;
1088 free(td->file_locks);
1089 td->files_index = 0;
1091 td->file_locks = NULL;
1092 td->o.file_lock_mode = FILE_LOCK_NONE;
1096 static void get_file_type(struct fio_file *f)
1100 if (!strcmp(f->file_name, "-"))
1101 f->filetype = FIO_TYPE_PIPE;
1103 f->filetype = FIO_TYPE_FILE;
1105 /* \\.\ is the device namespace in Windows, where every file is
1107 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1108 f->filetype = FIO_TYPE_BD;
1110 if (!stat(f->file_name, &sb)) {
1111 if (S_ISBLK(sb.st_mode))
1112 f->filetype = FIO_TYPE_BD;
1113 else if (S_ISCHR(sb.st_mode))
1114 f->filetype = FIO_TYPE_CHAR;
1115 else if (S_ISFIFO(sb.st_mode))
1116 f->filetype = FIO_TYPE_PIPE;
1120 static int __is_already_allocated(const char *fname)
1122 struct flist_head *entry;
1125 if (flist_empty(&filename_list))
1128 flist_for_each(entry, &filename_list) {
1129 filename = flist_entry(entry, struct file_name, list)->filename;
1131 if (strcmp(filename, fname) == 0)
1138 static int is_already_allocated(const char *fname)
1142 fio_file_hash_lock();
1143 ret = __is_already_allocated(fname);
1144 fio_file_hash_unlock();
1148 static void set_already_allocated(const char *fname)
1150 struct file_name *fn;
1152 fn = malloc(sizeof(struct file_name));
1153 fn->filename = strdup(fname);
1155 fio_file_hash_lock();
1156 if (!__is_already_allocated(fname)) {
1157 flist_add_tail(&fn->list, &filename_list);
1160 fio_file_hash_unlock();
1169 static void free_already_allocated(void)
1171 struct flist_head *entry, *tmp;
1172 struct file_name *fn;
1174 if (flist_empty(&filename_list))
1177 fio_file_hash_lock();
1178 flist_for_each_safe(entry, tmp, &filename_list) {
1179 fn = flist_entry(entry, struct file_name, list);
1181 flist_del(&fn->list);
1185 fio_file_hash_unlock();
1188 static struct fio_file *alloc_new_file(struct thread_data *td)
1192 f = smalloc(sizeof(*f));
1194 log_err("fio: smalloc OOM\n");
1201 fio_file_reset(td, f);
1205 int add_file(struct thread_data *td, const char *fname, int numjob, int inc)
1207 int cur_files = td->files_index;
1208 char file_name[PATH_MAX];
1212 dprint(FD_FILE, "add file %s\n", fname);
1214 if (td->o.directory)
1215 len = set_name_idx(file_name, td->o.directory, numjob);
1217 sprintf(file_name + len, "%s", fname);
1219 /* clean cloned siblings using existing files */
1220 if (numjob && is_already_allocated(file_name))
1223 f = alloc_new_file(td);
1225 if (td->files_size <= td->files_index) {
1226 unsigned int new_size = td->o.nr_files + 1;
1228 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1230 td->files = realloc(td->files, new_size * sizeof(f));
1231 if (td->files == NULL) {
1232 log_err("fio: realloc OOM\n");
1235 if (td->o.file_lock_mode != FILE_LOCK_NONE) {
1236 td->file_locks = realloc(td->file_locks, new_size);
1237 if (!td->file_locks) {
1238 log_err("fio: realloc OOM\n");
1241 td->file_locks[cur_files] = FILE_LOCK_NONE;
1243 td->files_size = new_size;
1245 td->files[cur_files] = f;
1246 f->fileno = cur_files;
1249 * init function, io engine may not be loaded yet
1251 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1252 f->real_file_size = -1ULL;
1254 f->file_name = smalloc_strdup(file_name);
1255 if (!f->file_name) {
1256 log_err("fio: smalloc OOM\n");
1262 switch (td->o.file_lock_mode) {
1263 case FILE_LOCK_NONE:
1265 case FILE_LOCK_READWRITE:
1266 f->rwlock = fio_rwlock_init();
1268 case FILE_LOCK_EXCLUSIVE:
1269 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
1272 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1277 if (f->filetype == FIO_TYPE_FILE)
1278 td->nr_normal_files++;
1280 set_already_allocated(file_name);
1285 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1291 int add_file_exclusive(struct thread_data *td, const char *fname)
1296 for_each_file(td, f, i) {
1297 if (!strcmp(f->file_name, fname))
1301 return add_file(td, fname, 0, 1);
1304 void get_file(struct fio_file *f)
1306 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
1307 assert(fio_file_open(f));
1311 int put_file(struct thread_data *td, struct fio_file *f)
1313 int f_ret = 0, ret = 0;
1315 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
1317 if (!fio_file_open(f)) {
1318 assert(f->fd == -1);
1322 assert(f->references);
1323 if (--f->references)
1326 if (should_fsync(td) && td->o.fsync_on_close)
1327 f_ret = fsync(f->fd);
1329 if (td->io_ops->close_file)
1330 ret = td->io_ops->close_file(td, f);
1335 td->nr_open_files--;
1336 fio_file_clear_open(f);
1337 assert(f->fd == -1);
1341 void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
1343 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1346 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1347 if (ddir == DDIR_READ)
1348 fio_rwlock_read(f->rwlock);
1350 fio_rwlock_write(f->rwlock);
1351 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1352 fio_mutex_down(f->lock);
1354 td->file_locks[f->fileno] = td->o.file_lock_mode;
1357 void unlock_file(struct thread_data *td, struct fio_file *f)
1359 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1362 if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1363 fio_rwlock_unlock(f->rwlock);
1364 else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1365 fio_mutex_up(f->lock);
1367 td->file_locks[f->fileno] = FILE_LOCK_NONE;
1370 void unlock_file_all(struct thread_data *td, struct fio_file *f)
1372 if (td->o.file_lock_mode == FILE_LOCK_NONE || !td->file_locks)
1374 if (td->file_locks[f->fileno] != FILE_LOCK_NONE)
1378 static int recurse_dir(struct thread_data *td, const char *dirname)
1384 D = opendir(dirname);
1386 char buf[FIO_VERROR_SIZE];
1388 snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
1389 td_verror(td, errno, buf);
1393 while ((dir = readdir(D)) != NULL) {
1394 char full_path[PATH_MAX];
1397 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1400 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
1402 if (lstat(full_path, &sb) == -1) {
1403 if (errno != ENOENT) {
1404 td_verror(td, errno, "stat");
1409 if (S_ISREG(sb.st_mode)) {
1410 add_file(td, full_path, 0, 1);
1413 if (!S_ISDIR(sb.st_mode))
1416 ret = recurse_dir(td, full_path);
1425 int add_dir_files(struct thread_data *td, const char *path)
1427 int ret = recurse_dir(td, path);
1430 log_info("fio: opendir added %d files\n", td->o.nr_files);
1435 void dup_files(struct thread_data *td, struct thread_data *org)
1440 dprint(FD_FILE, "dup files: %d\n", org->files_index);
1445 td->files = malloc(org->files_index * sizeof(f));
1447 if (td->o.file_lock_mode != FILE_LOCK_NONE)
1448 td->file_locks = malloc(org->files_index);
1450 for_each_file(org, f, i) {
1451 struct fio_file *__f;
1453 __f = alloc_new_file(td);
1456 __f->file_name = smalloc_strdup(f->file_name);
1457 if (!__f->file_name) {
1458 log_err("fio: smalloc OOM\n");
1462 __f->filetype = f->filetype;
1465 if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1466 __f->lock = f->lock;
1467 else if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1468 __f->rwlock = f->rwlock;
1475 * Returns the index that matches the filename, or -1 if not there
1477 int get_fileno(struct thread_data *td, const char *fname)
1482 for_each_file(td, f, i)
1483 if (!strcmp(f->file_name, fname))
1490 * For log usage, where we add/open/close files automatically
1492 void free_release_files(struct thread_data *td)
1495 td->files_index = 0;
1496 td->nr_normal_files = 0;
1499 void fio_file_reset(struct thread_data *td, struct fio_file *f)
1501 f->last_pos = f->file_offset;
1502 f->last_start = -1ULL;
1504 axmap_reset(f->io_axmap);
1505 if (td->o.random_generator == FIO_RAND_GEN_LFSR)
1506 lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
1509 int fio_files_done(struct thread_data *td)
1514 for_each_file(td, f, i)
1515 if (!fio_file_done(f))
1521 /* free memory used in initialization phase only */
1522 void filesetup_mem_free(void)
1524 free_already_allocated();