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;
197 static int pre_read_file(struct thread_data *td, struct fio_file *f)
199 int ret = 0, r, did_open = 0, old_runstate;
200 unsigned long long left;
204 if (td->io_ops->flags & FIO_PIPEIO)
207 if (!fio_file_open(f)) {
208 if (td->io_ops->open_file(td, f)) {
209 log_err("fio: cannot pre-read, failed to open file\n");
215 old_runstate = td_bump_runstate(td, TD_PRE_READING);
217 bs = td->o.max_bs[DDIR_READ];
221 if (lseek(f->fd, f->file_offset, SEEK_SET) < 0) {
222 td_verror(td, errno, "lseek");
223 log_err("fio: failed to lseek pre-read file\n");
230 while (left && !td->terminate) {
234 r = read(f->fd, b, bs);
240 td_verror(td, EIO, "pre_read");
246 td_restore_runstate(td, old_runstate);
249 td->io_ops->close_file(td, f);
255 static unsigned long long get_rand_file_size(struct thread_data *td)
257 unsigned long long ret, sized;
260 if (td->o.use_os_rand) {
261 r = os_random_long(&td->file_size_state);
262 sized = td->o.file_size_high - td->o.file_size_low;
263 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
265 r = __rand(&td->__file_size_state);
266 sized = td->o.file_size_high - td->o.file_size_low;
267 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0)));
270 ret += td->o.file_size_low;
271 ret -= (ret % td->o.rw_min_bs);
275 static int file_size(struct thread_data *td, struct fio_file *f)
279 if (stat(f->file_name, &st) == -1) {
280 td_verror(td, errno, "fstat");
284 f->real_file_size = st.st_size;
288 static int bdev_size(struct thread_data *td, struct fio_file *f)
290 unsigned long long bytes = 0;
293 if (td->io_ops->open_file(td, f)) {
294 log_err("fio: failed opening blockdev %s for size check\n",
299 r = blockdev_size(f, &bytes);
301 td_verror(td, r, "blockdev_size");
306 log_err("%s: zero sized block device?\n", f->file_name);
310 f->real_file_size = bytes;
311 td->io_ops->close_file(td, f);
314 td->io_ops->close_file(td, f);
318 static int char_size(struct thread_data *td, struct fio_file *f)
320 #ifdef FIO_HAVE_CHARDEV_SIZE
321 unsigned long long bytes = 0;
324 if (td->io_ops->open_file(td, f)) {
325 log_err("fio: failed opening blockdev %s for size check\n",
330 r = chardev_size(f, &bytes);
332 td_verror(td, r, "chardev_size");
337 log_err("%s: zero sized char device?\n", f->file_name);
341 f->real_file_size = bytes;
342 td->io_ops->close_file(td, f);
345 td->io_ops->close_file(td, f);
348 f->real_file_size = -1ULL;
353 static int get_file_size(struct thread_data *td, struct fio_file *f)
357 if (fio_file_size_known(f))
360 if (f->filetype == FIO_TYPE_FILE)
361 ret = file_size(td, f);
362 else if (f->filetype == FIO_TYPE_BD)
363 ret = bdev_size(td, f);
364 else if (f->filetype == FIO_TYPE_CHAR)
365 ret = char_size(td, f);
367 f->real_file_size = -1;
372 if (f->file_offset > f->real_file_size) {
373 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
374 (unsigned long long) f->file_offset,
375 (unsigned long long) f->real_file_size);
379 fio_file_set_size_known(f);
383 static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
384 unsigned long long off,
385 unsigned long long len)
392 off = f->file_offset;
394 if (len == -1ULL || off == -1ULL)
397 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
401 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
403 if (f->filetype == FIO_TYPE_BD)
404 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
406 } else if (f->filetype == FIO_TYPE_FILE) {
407 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
408 } else if (f->filetype == FIO_TYPE_BD) {
409 ret = blockdev_invalidate_cache(f);
410 if (ret < 0 && errno == EACCES && geteuid()) {
412 log_err("fio: only root may flush block "
413 "devices. Cache flush bypassed!\n");
418 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
422 * Cache flushing isn't a fatal condition, and we know it will
423 * happen on some platforms where we don't have the proper
424 * function to flush eg block device caches. So just warn and
425 * continue on our way.
428 log_info("fio: cache invalidation of %s failed: %s\n", f->file_name, strerror(errno));
436 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
438 if (!fio_file_open(f))
441 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
444 int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
448 dprint(FD_FILE, "fd close %s\n", f->file_name);
452 if (close(f->fd) < 0)
457 if (f->shadow_fd != -1) {
466 int file_lookup_open(struct fio_file *f, int flags)
468 struct fio_file *__f;
471 __f = lookup_file_hash(f->file_name);
473 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
475 * racy, need the __f->lock locked
480 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
484 f->fd = open(f->file_name, flags, 0600);
488 static int file_close_shadow_fds(struct thread_data *td)
494 for_each_file(td, f, i) {
495 if (f->shadow_fd == -1)
506 int generic_open_file(struct thread_data *td, struct fio_file *f)
512 dprint(FD_FILE, "fd open %s\n", f->file_name);
514 if (td_trim(td) && f->filetype != FIO_TYPE_BD) {
515 log_err("fio: trim only applies to block device\n");
519 if (!strcmp(f->file_name, "-")) {
521 log_err("fio: can't read/write to stdin/out\n");
527 * move output logging to stderr, if we are writing to stdout
536 flags |= OS_O_DIRECT;
539 td_verror(td, EINVAL, "OS does not support atomic IO");
542 flags |= OS_O_DIRECT | FIO_O_ATOMIC;
546 if (td->o.create_on_open)
549 if (f->filetype != FIO_TYPE_FILE)
550 flags |= FIO_O_NOATIME;
557 if (f->filetype == FIO_TYPE_FILE)
561 f->fd = dup(STDOUT_FILENO);
563 from_hash = file_lookup_open(f, flags);
564 } else if (td_read(td)) {
565 if (f->filetype == FIO_TYPE_CHAR && !read_only)
571 f->fd = dup(STDIN_FILENO);
573 from_hash = file_lookup_open(f, flags);
576 from_hash = file_lookup_open(f, flags);
580 char buf[FIO_VERROR_SIZE];
583 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
584 flags &= ~FIO_O_NOATIME;
587 if (__e == EMFILE && file_close_shadow_fds(td))
590 snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
592 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
593 log_err("fio: looks like your file system does not " \
594 "support direct=1/buffered=0\n");
597 td_verror(td, __e, buf);
600 if (!from_hash && f->fd != -1) {
601 if (add_file_hash(f)) {
605 * Stash away descriptor for later close. This is to
606 * work-around a "feature" on Linux, where a close of
607 * an fd that has been opened for write will trigger
608 * udev to call blkid to check partitions, fs id, etc.
609 * That pollutes the device cache, which can slow down
610 * unbuffered accesses.
612 if (f->shadow_fd == -1)
613 f->shadow_fd = f->fd;
616 * OK to ignore, we haven't done anything
619 ret = generic_close_file(td, f);
628 int generic_get_file_size(struct thread_data *td, struct fio_file *f)
630 return get_file_size(td, f);
634 * open/close all files, so that ->real_file_size gets set
636 static int get_file_sizes(struct thread_data *td)
642 for_each_file(td, f, i) {
643 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
646 if (td_io_get_file_size(td, f)) {
647 if (td->error != ENOENT) {
648 log_err("%s\n", td->verror);
654 if (f->real_file_size == -1ULL && td->o.size)
655 f->real_file_size = td->o.size / td->o.nr_files;
662 struct flist_head list;
669 * Get free number of bytes for each file on each unique mount.
671 static unsigned long long get_fs_free_counts(struct thread_data *td)
673 struct flist_head *n, *tmp;
674 unsigned long long ret = 0;
675 struct fio_mount *fm;
680 for_each_file(td, f, i) {
684 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
685 if (f->real_file_size != -1ULL)
686 ret += f->real_file_size;
688 } else if (f->filetype != FIO_TYPE_FILE)
691 strcpy(buf, f->file_name);
693 if (stat(buf, &sb) < 0) {
697 if (stat(buf, &sb) < 0)
702 flist_for_each(n, &list) {
703 fm = flist_entry(n, struct fio_mount, list);
704 if (fm->key == sb.st_dev)
713 fm = malloc(sizeof(*fm));
714 strcpy(fm->__base, buf);
715 fm->base = basename(fm->__base);
717 flist_add(&fm->list, &list);
720 flist_for_each_safe(n, tmp, &list) {
721 unsigned long long sz;
723 fm = flist_entry(n, struct fio_mount, list);
724 flist_del(&fm->list);
726 sz = get_fs_size(fm->base);
727 if (sz && sz != -1ULL)
736 uint64_t get_start_offset(struct thread_data *td, struct fio_file *f)
738 struct thread_options *o = &td->o;
740 if (o->file_append && f->filetype == FIO_TYPE_FILE)
741 return f->real_file_size;
743 return td->o.start_offset +
744 (td->thread_number - 1) * td->o.offset_increment;
748 * Open the files and setup files sizes, creating files if necessary.
750 int setup_files(struct thread_data *td)
752 unsigned long long total_size, extend_size;
753 struct thread_options *o = &td->o;
755 unsigned int i, nr_fs_extra = 0;
756 int err = 0, need_extend;
758 const unsigned int bs = td_min_bs(td);
761 dprint(FD_FILE, "setup files\n");
763 old_state = td_bump_runstate(td, TD_SETTING_UP);
765 if (o->read_iolog_file)
769 * if ioengine defines a setup() method, it's responsible for
770 * opening the files and setting f->real_file_size to indicate
771 * the valid range for that file.
773 if (td->io_ops->setup)
774 err = td->io_ops->setup(td);
776 err = get_file_sizes(td);
782 * check sizes. if the files/devices do not exist and the size
783 * isn't passed to fio, abort.
786 for_each_file(td, f, i) {
787 if (f->real_file_size == -1ULL)
790 total_size += f->real_file_size;
794 td->fill_device_size = get_fs_free_counts(td);
797 * device/file sizes are zero and no size given, punt
799 if ((!total_size || total_size == -1ULL) && !o->size &&
800 !(td->io_ops->flags & FIO_NOIO) && !o->fill_device &&
801 !(o->nr_files && (o->file_size_low || o->file_size_high))) {
802 log_err("%s: you need to specify size=\n", o->name);
803 td_verror(td, EINVAL, "total_file_size");
808 * Calculate per-file size and potential extra size for the
809 * first files, if needed.
811 if (!o->file_size_low && o->nr_files) {
814 fs = o->size / o->nr_files;
815 all_fs = fs * o->nr_files;
817 if (all_fs < o->size)
818 nr_fs_extra = (o->size - all_fs) / bs;
822 * now file sizes are known, so we can set ->io_size. if size= is
823 * not given, ->io_size is just equal to ->real_file_size. if size
824 * is given, ->io_size is size / nr_files.
826 extend_size = total_size = 0;
828 for_each_file(td, f, i) {
829 f->file_offset = get_start_offset(td, f);
831 if (!o->file_size_low) {
833 * no file size range given, file size is equal to
834 * total size divided by number of files. If that is
835 * zero, set it to the real file size. If the size
836 * doesn't divide nicely with the min blocksize,
837 * make the first files bigger.
846 f->io_size = f->real_file_size - f->file_offset;
847 } else if (f->real_file_size < o->file_size_low ||
848 f->real_file_size > o->file_size_high) {
849 if (f->file_offset > o->file_size_low)
852 * file size given. if it's fixed, use that. if it's a
853 * range, generate a random size in-between.
855 if (o->file_size_low == o->file_size_high)
856 f->io_size = o->file_size_low - f->file_offset;
858 f->io_size = get_rand_file_size(td)
862 f->io_size = f->real_file_size - f->file_offset;
864 if (f->io_size == -1ULL)
868 f->io_size = (f->io_size * o->size_percent) / 100;
869 total_size += f->io_size;
872 if (f->filetype == FIO_TYPE_FILE &&
873 (f->io_size + f->file_offset) > f->real_file_size &&
874 !(td->io_ops->flags & FIO_DISKLESSIO)) {
875 if (!o->create_on_open) {
877 extend_size += (f->io_size + f->file_offset);
879 f->real_file_size = f->io_size + f->file_offset;
880 fio_file_set_extend(f);
884 if (!o->size || o->size > total_size)
885 o->size = total_size;
888 * See if we need to extend some files
892 if (output_format == FIO_OUTPUT_NORMAL)
893 log_info("%s: Laying out IO file(s) (%u file(s) /"
894 " %lluMB)\n", o->name, need_extend,
897 for_each_file(td, f, i) {
898 unsigned long long old_len = -1ULL, extend_len = -1ULL;
900 if (!fio_file_extend(f))
903 assert(f->filetype == FIO_TYPE_FILE);
904 fio_file_clear_extend(f);
905 if (!o->fill_device) {
906 old_len = f->real_file_size;
907 extend_len = f->io_size + f->file_offset -
910 f->real_file_size = (f->io_size + f->file_offset);
911 err = extend_file(td, f);
915 err = __file_invalidate_cache(td, f, old_len,
929 o->zone_size = o->size;
932 * iolog already set the total io size, if we read back
935 if (!o->read_iolog_file)
936 td->total_io_size = o->size * o->loops;
942 td_restore_runstate(td, old_state);
945 log_err("%s: you need to specify valid offset=\n", o->name);
947 td_restore_runstate(td, old_state);
951 int pre_read_files(struct thread_data *td)
956 dprint(FD_FILE, "pre_read files\n");
958 for_each_file(td, f, i) {
959 pre_read_file(td, f);
965 static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
967 unsigned int range_size, seed;
968 unsigned long nranges;
971 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
972 file_size = min(f->real_file_size, f->io_size);
974 nranges = (file_size + range_size - 1) / range_size;
976 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
977 if (!td->o.rand_repeatable)
978 seed = td->rand_seeds[4];
980 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
981 zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed);
983 pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed);
988 static int init_rand_distribution(struct thread_data *td)
994 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
997 state = td_bump_runstate(td, TD_SETTING_UP);
999 for_each_file(td, f, i)
1000 __init_rand_distribution(td, f);
1002 td_restore_runstate(td, state);
1007 int init_random_map(struct thread_data *td)
1009 unsigned long long blocks;
1013 if (init_rand_distribution(td))
1018 for_each_file(td, f, i) {
1019 uint64_t file_size = min(f->real_file_size, f->io_size);
1021 blocks = file_size / (unsigned long long) td->o.rw_min_bs;
1023 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
1026 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
1028 if (!lfsr_init(&f->lfsr, blocks, seed, 0))
1030 } else if (!td->o.norandommap) {
1031 f->io_axmap = axmap_new(blocks);
1034 } else if (td->o.norandommap)
1037 if (!td->o.softrandommap) {
1038 log_err("fio: failed allocating random map. If running"
1039 " a large number of jobs, try the 'norandommap'"
1040 " option or set 'softrandommap'. Or give"
1041 " a larger --alloc-size to fio.\n");
1045 log_info("fio: file %s failed allocating random map. Running "
1046 "job without.\n", f->file_name);
1052 void close_files(struct thread_data *td)
1057 for_each_file(td, f, i) {
1058 if (fio_file_open(f))
1059 td_io_close_file(td, f);
1063 void close_and_free_files(struct thread_data *td)
1068 dprint(FD_FILE, "close files\n");
1070 for_each_file(td, f, i) {
1071 if (fio_file_open(f))
1072 td_io_close_file(td, f);
1074 remove_file_hash(f);
1076 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1077 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1078 unlink(f->file_name);
1081 sfree(f->file_name);
1082 f->file_name = NULL;
1083 axmap_free(f->io_axmap);
1088 td->o.filename = NULL;
1090 free(td->file_locks);
1091 td->files_index = 0;
1093 td->file_locks = NULL;
1094 td->o.file_lock_mode = FILE_LOCK_NONE;
1098 static void get_file_type(struct fio_file *f)
1102 if (!strcmp(f->file_name, "-"))
1103 f->filetype = FIO_TYPE_PIPE;
1105 f->filetype = FIO_TYPE_FILE;
1107 /* \\.\ is the device namespace in Windows, where every file is
1109 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1110 f->filetype = FIO_TYPE_BD;
1112 if (!stat(f->file_name, &sb)) {
1113 if (S_ISBLK(sb.st_mode))
1114 f->filetype = FIO_TYPE_BD;
1115 else if (S_ISCHR(sb.st_mode))
1116 f->filetype = FIO_TYPE_CHAR;
1117 else if (S_ISFIFO(sb.st_mode))
1118 f->filetype = FIO_TYPE_PIPE;
1122 static int __is_already_allocated(const char *fname)
1124 struct flist_head *entry;
1127 if (flist_empty(&filename_list))
1130 flist_for_each(entry, &filename_list) {
1131 filename = flist_entry(entry, struct file_name, list)->filename;
1133 if (strcmp(filename, fname) == 0)
1140 static int is_already_allocated(const char *fname)
1144 fio_file_hash_lock();
1145 ret = __is_already_allocated(fname);
1146 fio_file_hash_unlock();
1150 static void set_already_allocated(const char *fname)
1152 struct file_name *fn;
1154 fn = malloc(sizeof(struct file_name));
1155 fn->filename = strdup(fname);
1157 fio_file_hash_lock();
1158 if (!__is_already_allocated(fname)) {
1159 flist_add_tail(&fn->list, &filename_list);
1162 fio_file_hash_unlock();
1171 static void free_already_allocated(void)
1173 struct flist_head *entry, *tmp;
1174 struct file_name *fn;
1176 if (flist_empty(&filename_list))
1179 fio_file_hash_lock();
1180 flist_for_each_safe(entry, tmp, &filename_list) {
1181 fn = flist_entry(entry, struct file_name, list);
1183 flist_del(&fn->list);
1187 fio_file_hash_unlock();
1190 static struct fio_file *alloc_new_file(struct thread_data *td)
1194 f = smalloc(sizeof(*f));
1196 log_err("fio: smalloc OOM\n");
1203 fio_file_reset(td, f);
1207 int add_file(struct thread_data *td, const char *fname, int numjob, int inc)
1209 int cur_files = td->files_index;
1210 char file_name[PATH_MAX];
1214 dprint(FD_FILE, "add file %s\n", fname);
1216 if (td->o.directory)
1217 len = set_name_idx(file_name, td->o.directory, numjob);
1219 sprintf(file_name + len, "%s", fname);
1221 /* clean cloned siblings using existing files */
1222 if (numjob && is_already_allocated(file_name))
1225 f = alloc_new_file(td);
1227 if (td->files_size <= td->files_index) {
1228 unsigned int new_size = td->o.nr_files + 1;
1230 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1232 td->files = realloc(td->files, new_size * sizeof(f));
1233 if (td->files == NULL) {
1234 log_err("fio: realloc OOM\n");
1237 if (td->o.file_lock_mode != FILE_LOCK_NONE) {
1238 td->file_locks = realloc(td->file_locks, new_size);
1239 if (!td->file_locks) {
1240 log_err("fio: realloc OOM\n");
1243 td->file_locks[cur_files] = FILE_LOCK_NONE;
1245 td->files_size = new_size;
1247 td->files[cur_files] = f;
1248 f->fileno = cur_files;
1251 * init function, io engine may not be loaded yet
1253 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1254 f->real_file_size = -1ULL;
1256 f->file_name = smalloc_strdup(file_name);
1257 if (!f->file_name) {
1258 log_err("fio: smalloc OOM\n");
1264 switch (td->o.file_lock_mode) {
1265 case FILE_LOCK_NONE:
1267 case FILE_LOCK_READWRITE:
1268 f->rwlock = fio_rwlock_init();
1270 case FILE_LOCK_EXCLUSIVE:
1271 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
1274 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1279 if (f->filetype == FIO_TYPE_FILE)
1280 td->nr_normal_files++;
1282 set_already_allocated(file_name);
1287 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1293 int add_file_exclusive(struct thread_data *td, const char *fname)
1298 for_each_file(td, f, i) {
1299 if (!strcmp(f->file_name, fname))
1303 return add_file(td, fname, 0, 1);
1306 void get_file(struct fio_file *f)
1308 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
1309 assert(fio_file_open(f));
1313 int put_file(struct thread_data *td, struct fio_file *f)
1315 int f_ret = 0, ret = 0;
1317 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
1319 if (!fio_file_open(f)) {
1320 assert(f->fd == -1);
1324 assert(f->references);
1325 if (--f->references)
1328 if (should_fsync(td) && td->o.fsync_on_close)
1329 f_ret = fsync(f->fd);
1331 if (td->io_ops->close_file)
1332 ret = td->io_ops->close_file(td, f);
1337 td->nr_open_files--;
1338 fio_file_clear_open(f);
1339 assert(f->fd == -1);
1343 void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
1345 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1348 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1349 if (ddir == DDIR_READ)
1350 fio_rwlock_read(f->rwlock);
1352 fio_rwlock_write(f->rwlock);
1353 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1354 fio_mutex_down(f->lock);
1356 td->file_locks[f->fileno] = td->o.file_lock_mode;
1359 void unlock_file(struct thread_data *td, struct fio_file *f)
1361 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1364 if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1365 fio_rwlock_unlock(f->rwlock);
1366 else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1367 fio_mutex_up(f->lock);
1369 td->file_locks[f->fileno] = FILE_LOCK_NONE;
1372 void unlock_file_all(struct thread_data *td, struct fio_file *f)
1374 if (td->o.file_lock_mode == FILE_LOCK_NONE || !td->file_locks)
1376 if (td->file_locks[f->fileno] != FILE_LOCK_NONE)
1380 static int recurse_dir(struct thread_data *td, const char *dirname)
1386 D = opendir(dirname);
1388 char buf[FIO_VERROR_SIZE];
1390 snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
1391 td_verror(td, errno, buf);
1395 while ((dir = readdir(D)) != NULL) {
1396 char full_path[PATH_MAX];
1399 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1402 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
1404 if (lstat(full_path, &sb) == -1) {
1405 if (errno != ENOENT) {
1406 td_verror(td, errno, "stat");
1412 if (S_ISREG(sb.st_mode)) {
1413 add_file(td, full_path, 0, 1);
1416 if (!S_ISDIR(sb.st_mode))
1419 ret = recurse_dir(td, full_path);
1428 int add_dir_files(struct thread_data *td, const char *path)
1430 int ret = recurse_dir(td, path);
1433 log_info("fio: opendir added %d files\n", td->o.nr_files);
1438 void dup_files(struct thread_data *td, struct thread_data *org)
1443 dprint(FD_FILE, "dup files: %d\n", org->files_index);
1448 td->files = malloc(org->files_index * sizeof(f));
1450 if (td->o.file_lock_mode != FILE_LOCK_NONE)
1451 td->file_locks = malloc(org->files_index);
1453 for_each_file(org, f, i) {
1454 struct fio_file *__f;
1456 __f = alloc_new_file(td);
1459 __f->file_name = smalloc_strdup(f->file_name);
1460 if (!__f->file_name) {
1461 log_err("fio: smalloc OOM\n");
1465 __f->filetype = f->filetype;
1468 if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1469 __f->lock = f->lock;
1470 else if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1471 __f->rwlock = f->rwlock;
1478 * Returns the index that matches the filename, or -1 if not there
1480 int get_fileno(struct thread_data *td, const char *fname)
1485 for_each_file(td, f, i)
1486 if (!strcmp(f->file_name, fname))
1493 * For log usage, where we add/open/close files automatically
1495 void free_release_files(struct thread_data *td)
1498 td->files_index = 0;
1499 td->nr_normal_files = 0;
1502 void fio_file_reset(struct thread_data *td, struct fio_file *f)
1504 f->last_pos = f->file_offset;
1505 f->last_start = -1ULL;
1507 axmap_reset(f->io_axmap);
1508 if (td->o.random_generator == FIO_RAND_GEN_LFSR)
1509 lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
1512 int fio_files_done(struct thread_data *td)
1517 for_each_file(td, f, i)
1518 if (!fio_file_done(f))
1524 /* free memory used in initialization phase only */
1525 void filesetup_mem_free(void)
1527 free_already_allocated();