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 ((td_io_unlink_file(td, f) < 0) && (errno != ENOENT)) {
63 td_verror(td, errno, "unlink");
68 flags = O_WRONLY | O_CREAT;
76 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
77 f->fd = open(f->file_name, flags, 0644);
79 td_verror(td, errno, "open");
83 #ifdef CONFIG_POSIX_FALLOCATE
84 if (!td->o.fill_device) {
85 switch (td->o.fallocate_mode) {
86 case FIO_FALLOCATE_NONE:
88 case FIO_FALLOCATE_POSIX:
89 dprint(FD_FILE, "posix_fallocate file %s size %llu\n",
91 (unsigned long long) f->real_file_size);
93 r = posix_fallocate(f->fd, 0, f->real_file_size);
95 log_err("fio: posix_fallocate fails: %s\n",
99 #ifdef CONFIG_LINUX_FALLOCATE
100 case FIO_FALLOCATE_KEEP_SIZE:
102 "fallocate(FALLOC_FL_KEEP_SIZE) "
103 "file %s size %llu\n", f->file_name,
104 (unsigned long long) f->real_file_size);
106 r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0,
109 td_verror(td, errno, "fallocate");
112 #endif /* CONFIG_LINUX_FALLOCATE */
114 log_err("fio: unknown fallocate mode: %d\n",
115 td->o.fallocate_mode);
119 #endif /* CONFIG_POSIX_FALLOCATE */
125 * The size will be -1ULL when fill_device is used, so don't truncate
126 * or fallocate this file, just write it
128 if (!td->o.fill_device) {
129 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
130 (unsigned long long) f->real_file_size);
131 if (ftruncate(f->fd, f->real_file_size) == -1) {
132 if (errno != EFBIG) {
133 td_verror(td, errno, "ftruncate");
139 b = malloc(td->o.max_bs[DDIR_WRITE]);
141 left = f->real_file_size;
142 while (left && !td->terminate) {
143 bs = td->o.max_bs[DDIR_WRITE];
147 fill_io_buffer(td, b, bs, bs);
149 r = write(f->fd, b, bs);
159 if (td->o.fill_device)
161 log_info("fio: ENOSPC on laying out "
165 td_verror(td, errno, "write");
167 td_verror(td, EIO, "write");
174 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
175 td_io_unlink_file(td, f);
176 } else if (td->o.create_fsync) {
177 if (fsync(f->fd) < 0) {
178 td_verror(td, errno, "fsync");
182 if (td->o.fill_device && !td_write(td)) {
183 fio_file_clear_size_known(f);
184 if (td_io_get_file_size(td, f))
186 if (f->io_size > f->real_file_size)
187 f->io_size = f->real_file_size;
201 static int pre_read_file(struct thread_data *td, struct fio_file *f)
203 int ret = 0, r, did_open = 0, old_runstate;
204 unsigned long long left;
208 if (td->io_ops->flags & FIO_PIPEIO)
211 if (!fio_file_open(f)) {
212 if (td->io_ops->open_file(td, f)) {
213 log_err("fio: cannot pre-read, failed to open file\n");
219 old_runstate = td_bump_runstate(td, TD_PRE_READING);
221 bs = td->o.max_bs[DDIR_READ];
225 if (lseek(f->fd, f->file_offset, SEEK_SET) < 0) {
226 td_verror(td, errno, "lseek");
227 log_err("fio: failed to lseek pre-read file\n");
234 while (left && !td->terminate) {
238 r = read(f->fd, b, bs);
244 td_verror(td, EIO, "pre_read");
250 td_restore_runstate(td, old_runstate);
253 td->io_ops->close_file(td, f);
259 static unsigned long long get_rand_file_size(struct thread_data *td)
261 unsigned long long ret, sized;
264 r = __rand(&td->file_size_state);
265 sized = td->o.file_size_high - td->o.file_size_low;
266 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0)));
267 ret += td->o.file_size_low;
268 ret -= (ret % td->o.rw_min_bs);
272 static int file_size(struct thread_data *td, struct fio_file *f)
276 if (stat(f->file_name, &st) == -1) {
277 td_verror(td, errno, "fstat");
281 f->real_file_size = st.st_size;
285 static int bdev_size(struct thread_data *td, struct fio_file *f)
287 unsigned long long bytes = 0;
290 if (td->io_ops->open_file(td, f)) {
291 log_err("fio: failed opening blockdev %s for size check\n",
296 r = blockdev_size(f, &bytes);
298 td_verror(td, r, "blockdev_size");
303 log_err("%s: zero sized block device?\n", f->file_name);
307 f->real_file_size = bytes;
308 td->io_ops->close_file(td, f);
311 td->io_ops->close_file(td, f);
315 static int char_size(struct thread_data *td, struct fio_file *f)
317 #ifdef FIO_HAVE_CHARDEV_SIZE
318 unsigned long long bytes = 0;
321 if (td->io_ops->open_file(td, f)) {
322 log_err("fio: failed opening blockdev %s for size check\n",
327 r = chardev_size(f, &bytes);
329 td_verror(td, r, "chardev_size");
334 log_err("%s: zero sized char device?\n", f->file_name);
338 f->real_file_size = bytes;
339 td->io_ops->close_file(td, f);
342 td->io_ops->close_file(td, f);
345 f->real_file_size = -1ULL;
350 static int get_file_size(struct thread_data *td, struct fio_file *f)
354 if (fio_file_size_known(f))
357 if (f->filetype == FIO_TYPE_FILE)
358 ret = file_size(td, f);
359 else if (f->filetype == FIO_TYPE_BD)
360 ret = bdev_size(td, f);
361 else if (f->filetype == FIO_TYPE_CHAR)
362 ret = char_size(td, f);
364 f->real_file_size = -1;
369 if (f->file_offset > f->real_file_size) {
370 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
371 (unsigned long long) f->file_offset,
372 (unsigned long long) f->real_file_size);
376 fio_file_set_size_known(f);
380 static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
381 unsigned long long off,
382 unsigned long long len)
393 off = f->file_offset;
395 if (len == -1ULL || off == -1ULL)
398 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
401 if (td->io_ops->invalidate)
402 ret = td->io_ops->invalidate(td, f);
403 else if (f->filetype == FIO_TYPE_FILE)
404 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
405 else if (f->filetype == FIO_TYPE_BD) {
406 ret = blockdev_invalidate_cache(f);
407 if (ret < 0 && errno == EACCES && geteuid()) {
409 log_err("fio: only root may flush block "
410 "devices. Cache flush bypassed!\n");
415 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
419 * Cache flushing isn't a fatal condition, and we know it will
420 * happen on some platforms where we don't have the proper
421 * function to flush eg block device caches. So just warn and
422 * continue on our way.
425 log_info("fio: cache invalidation of %s failed: %s\n", f->file_name, strerror(errno));
433 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
435 if (!fio_file_open(f))
438 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
441 int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
445 dprint(FD_FILE, "fd close %s\n", f->file_name);
449 if (close(f->fd) < 0)
454 if (f->shadow_fd != -1) {
463 int file_lookup_open(struct fio_file *f, int flags)
465 struct fio_file *__f;
468 __f = lookup_file_hash(f->file_name);
470 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
472 * racy, need the __f->lock locked
477 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
485 f->fd = open(f->file_name, flags, 0600);
489 static int file_close_shadow_fds(struct thread_data *td)
495 for_each_file(td, f, i) {
496 if (f->shadow_fd == -1)
507 int generic_open_file(struct thread_data *td, struct fio_file *f)
513 dprint(FD_FILE, "fd open %s\n", f->file_name);
515 if (!strcmp(f->file_name, "-")) {
517 log_err("fio: can't read/write to stdin/out\n");
523 * move output logging to stderr, if we are writing to stdout
532 flags |= OS_O_DIRECT;
535 td_verror(td, EINVAL, "OS does not support atomic IO");
538 flags |= OS_O_DIRECT | FIO_O_ATOMIC;
542 if (td->o.create_on_open)
545 if (f->filetype != FIO_TYPE_FILE)
546 flags |= FIO_O_NOATIME;
553 if (f->filetype == FIO_TYPE_FILE)
557 f->fd = dup(STDOUT_FILENO);
559 from_hash = file_lookup_open(f, flags);
560 } else if (td_read(td)) {
561 if (f->filetype == FIO_TYPE_CHAR && !read_only)
567 f->fd = dup(STDIN_FILENO);
569 from_hash = file_lookup_open(f, flags);
572 from_hash = file_lookup_open(f, flags);
576 char buf[FIO_VERROR_SIZE];
579 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
580 flags &= ~FIO_O_NOATIME;
583 if (__e == EMFILE && file_close_shadow_fds(td))
586 snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
588 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
589 log_err("fio: looks like your file system does not " \
590 "support direct=1/buffered=0\n");
593 td_verror(td, __e, buf);
597 if (!from_hash && f->fd != -1) {
598 if (add_file_hash(f)) {
602 * Stash away descriptor for later close. This is to
603 * work-around a "feature" on Linux, where a close of
604 * an fd that has been opened for write will trigger
605 * udev to call blkid to check partitions, fs id, etc.
606 * That pollutes the device cache, which can slow down
607 * unbuffered accesses.
609 if (f->shadow_fd == -1)
610 f->shadow_fd = f->fd;
613 * OK to ignore, we haven't done anything
616 ret = generic_close_file(td, f);
625 int generic_get_file_size(struct thread_data *td, struct fio_file *f)
627 return get_file_size(td, f);
631 * open/close all files, so that ->real_file_size gets set
633 static int get_file_sizes(struct thread_data *td)
639 for_each_file(td, f, i) {
640 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
643 if (td_io_get_file_size(td, f)) {
644 if (td->error != ENOENT) {
645 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)
690 strncpy(buf, f->file_name, 255);
692 if (stat(buf, &sb) < 0) {
696 if (stat(buf, &sb) < 0)
701 flist_for_each(n, &list) {
702 fm = flist_entry(n, struct fio_mount, list);
703 if (fm->key == sb.st_dev)
712 fm = calloc(1, sizeof(*fm));
713 strncpy(fm->__base, buf, sizeof(fm->__base) - 1);
714 fm->base = basename(fm->__base);
716 flist_add(&fm->list, &list);
719 flist_for_each_safe(n, tmp, &list) {
720 unsigned long long sz;
722 fm = flist_entry(n, struct fio_mount, list);
723 flist_del(&fm->list);
725 sz = get_fs_size(fm->base);
726 if (sz && sz != -1ULL)
735 uint64_t get_start_offset(struct thread_data *td, struct fio_file *f)
737 struct thread_options *o = &td->o;
739 if (o->file_append && f->filetype == FIO_TYPE_FILE)
740 return f->real_file_size;
742 return td->o.start_offset +
743 td->subjob_number * td->o.offset_increment;
747 * Open the files and setup files sizes, creating files if necessary.
749 int setup_files(struct thread_data *td)
751 unsigned long long total_size, extend_size;
752 struct thread_options *o = &td->o;
754 unsigned int i, nr_fs_extra = 0;
755 int err = 0, need_extend;
757 const unsigned int bs = td_min_bs(td);
760 dprint(FD_FILE, "setup files\n");
762 old_state = td_bump_runstate(td, TD_SETTING_UP);
764 if (o->read_iolog_file)
768 * if ioengine defines a setup() method, it's responsible for
769 * opening the files and setting f->real_file_size to indicate
770 * the valid range for that file.
772 if (td->io_ops->setup)
773 err = td->io_ops->setup(td);
775 err = get_file_sizes(td);
781 * check sizes. if the files/devices do not exist and the size
782 * isn't passed to fio, abort.
785 for_each_file(td, f, i) {
786 if (f->real_file_size == -1ULL)
789 total_size += f->real_file_size;
793 td->fill_device_size = get_fs_free_counts(td);
796 * device/file sizes are zero and no size given, punt
798 if ((!total_size || total_size == -1ULL) && !o->size &&
799 !(td->io_ops->flags & FIO_NOIO) && !o->fill_device &&
800 !(o->nr_files && (o->file_size_low || o->file_size_high))) {
801 log_err("%s: you need to specify size=\n", o->name);
802 td_verror(td, EINVAL, "total_file_size");
807 * Calculate per-file size and potential extra size for the
808 * first files, if needed.
810 if (!o->file_size_low && o->nr_files) {
813 fs = o->size / o->nr_files;
814 all_fs = fs * o->nr_files;
816 if (all_fs < o->size)
817 nr_fs_extra = (o->size - all_fs) / bs;
821 * now file sizes are known, so we can set ->io_size. if size= is
822 * not given, ->io_size is just equal to ->real_file_size. if size
823 * is given, ->io_size is size / nr_files.
825 extend_size = total_size = 0;
827 for_each_file(td, f, i) {
828 f->file_offset = get_start_offset(td, f);
830 if (!o->file_size_low) {
832 * no file size range given, file size is equal to
833 * total size divided by number of files. If that is
834 * zero, set it to the real file size. If the size
835 * doesn't divide nicely with the min blocksize,
836 * make the first files bigger.
845 f->io_size = f->real_file_size - f->file_offset;
846 } else if (f->real_file_size < o->file_size_low ||
847 f->real_file_size > o->file_size_high) {
848 if (f->file_offset > o->file_size_low)
851 * file size given. if it's fixed, use that. if it's a
852 * range, generate a random size in-between.
854 if (o->file_size_low == o->file_size_high)
855 f->io_size = o->file_size_low - f->file_offset;
857 f->io_size = get_rand_file_size(td)
861 f->io_size = f->real_file_size - f->file_offset;
863 if (f->io_size == -1ULL)
867 f->io_size = (f->io_size * o->size_percent) / 100;
868 total_size += f->io_size;
871 if (f->filetype == FIO_TYPE_FILE &&
872 (f->io_size + f->file_offset) > f->real_file_size &&
873 !(td->io_ops->flags & FIO_DISKLESSIO)) {
874 if (!o->create_on_open) {
876 extend_size += (f->io_size + f->file_offset);
878 f->real_file_size = f->io_size + f->file_offset;
879 fio_file_set_extend(f);
883 if (td->o.block_error_hist) {
886 assert(td->o.nr_files == 1); /* checked in fixup_options */
888 len = f->io_size / td->o.bs[DDIR_TRIM];
889 if (len > MAX_NR_BLOCK_INFOS || len <= 0) {
890 log_err("fio: cannot calculate block histogram with "
891 "%d trim blocks, maximum %d\n",
892 len, MAX_NR_BLOCK_INFOS);
893 td_verror(td, EINVAL, "block_error_hist");
897 td->ts.nr_block_infos = len;
898 for (int i = 0; i < len; i++)
899 td->ts.block_infos[i] =
900 BLOCK_INFO(0, BLOCK_STATE_UNINIT);
902 td->ts.nr_block_infos = 0;
904 if (!o->size || (total_size && o->size > total_size))
905 o->size = total_size;
907 if (o->size < td_min_bs(td)) {
908 log_err("fio: blocksize too large for data set\n");
913 * See if we need to extend some files
917 if (output_format == FIO_OUTPUT_NORMAL)
918 log_info("%s: Laying out IO file(s) (%u file(s) /"
919 " %lluMB)\n", o->name, need_extend,
922 for_each_file(td, f, i) {
923 unsigned long long old_len = -1ULL, extend_len = -1ULL;
925 if (!fio_file_extend(f))
928 assert(f->filetype == FIO_TYPE_FILE);
929 fio_file_clear_extend(f);
930 if (!o->fill_device) {
931 old_len = f->real_file_size;
932 extend_len = f->io_size + f->file_offset -
935 f->real_file_size = (f->io_size + f->file_offset);
936 err = extend_file(td, f);
940 err = __file_invalidate_cache(td, f, old_len,
944 * Shut up static checker
960 o->zone_size = o->size;
963 * iolog already set the total io size, if we read back
966 if (!o->read_iolog_file) {
968 td->total_io_size = o->io_limit * o->loops;
970 td->total_io_size = o->size * o->loops;
977 td_restore_runstate(td, old_state);
980 log_err("%s: you need to specify valid offset=\n", o->name);
982 td_restore_runstate(td, old_state);
986 int pre_read_files(struct thread_data *td)
991 dprint(FD_FILE, "pre_read files\n");
993 for_each_file(td, f, i) {
994 pre_read_file(td, f);
1000 static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
1002 unsigned int range_size, seed;
1003 unsigned long nranges;
1006 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
1007 fsize = min(f->real_file_size, f->io_size);
1009 nranges = (fsize + range_size - 1) / range_size;
1011 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
1012 if (!td->o.rand_repeatable)
1013 seed = td->rand_seeds[4];
1015 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
1016 zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed);
1017 else if (td->o.random_distribution == FIO_RAND_DIST_PARETO)
1018 pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed);
1019 else if (td->o.random_distribution == FIO_RAND_DIST_GAUSS)
1020 gauss_init(&f->gauss, nranges, td->o.gauss_dev.u.f, seed);
1025 static int init_rand_distribution(struct thread_data *td)
1031 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
1034 state = td_bump_runstate(td, TD_SETTING_UP);
1036 for_each_file(td, f, i)
1037 __init_rand_distribution(td, f);
1039 td_restore_runstate(td, state);
1044 int init_random_map(struct thread_data *td)
1046 unsigned long long blocks;
1050 if (init_rand_distribution(td))
1055 for_each_file(td, f, i) {
1056 uint64_t fsize = min(f->real_file_size, f->io_size);
1058 blocks = fsize / (unsigned long long) td->o.rw_min_bs;
1060 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
1063 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
1065 if (!lfsr_init(&f->lfsr, blocks, seed, 0)) {
1066 fio_file_set_lfsr(f);
1069 } else if (!td->o.norandommap) {
1070 f->io_axmap = axmap_new(blocks);
1072 fio_file_set_axmap(f);
1075 } else if (td->o.norandommap)
1078 if (!td->o.softrandommap) {
1079 log_err("fio: failed allocating random map. If running"
1080 " a large number of jobs, try the 'norandommap'"
1081 " option or set 'softrandommap'. Or give"
1082 " a larger --alloc-size to fio.\n");
1086 log_info("fio: file %s failed allocating random map. Running "
1087 "job without.\n", f->file_name);
1093 void close_files(struct thread_data *td)
1098 for_each_file(td, f, i) {
1099 if (fio_file_open(f))
1100 td_io_close_file(td, f);
1104 void close_and_free_files(struct thread_data *td)
1109 dprint(FD_FILE, "close files\n");
1111 for_each_file(td, f, i) {
1112 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1113 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1114 td_io_unlink_file(td, f);
1117 if (fio_file_open(f))
1118 td_io_close_file(td, f);
1120 remove_file_hash(f);
1122 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1123 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1124 td_io_unlink_file(td, f);
1127 sfree(f->file_name);
1128 f->file_name = NULL;
1129 if (fio_file_axmap(f)) {
1130 axmap_free(f->io_axmap);
1136 td->o.filename = NULL;
1138 free(td->file_locks);
1139 td->files_index = 0;
1141 td->file_locks = NULL;
1142 td->o.file_lock_mode = FILE_LOCK_NONE;
1146 static void get_file_type(struct fio_file *f)
1150 if (!strcmp(f->file_name, "-"))
1151 f->filetype = FIO_TYPE_PIPE;
1153 f->filetype = FIO_TYPE_FILE;
1155 /* \\.\ is the device namespace in Windows, where every file is
1157 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1158 f->filetype = FIO_TYPE_BD;
1160 if (!stat(f->file_name, &sb)) {
1161 if (S_ISBLK(sb.st_mode))
1162 f->filetype = FIO_TYPE_BD;
1163 else if (S_ISCHR(sb.st_mode))
1164 f->filetype = FIO_TYPE_CHAR;
1165 else if (S_ISFIFO(sb.st_mode))
1166 f->filetype = FIO_TYPE_PIPE;
1170 static int __is_already_allocated(const char *fname)
1172 struct flist_head *entry;
1175 if (flist_empty(&filename_list))
1178 flist_for_each(entry, &filename_list) {
1179 filename = flist_entry(entry, struct file_name, list)->filename;
1181 if (strcmp(filename, fname) == 0)
1188 static int is_already_allocated(const char *fname)
1192 fio_file_hash_lock();
1193 ret = __is_already_allocated(fname);
1194 fio_file_hash_unlock();
1198 static void set_already_allocated(const char *fname)
1200 struct file_name *fn;
1202 fn = malloc(sizeof(struct file_name));
1203 fn->filename = strdup(fname);
1205 fio_file_hash_lock();
1206 if (!__is_already_allocated(fname)) {
1207 flist_add_tail(&fn->list, &filename_list);
1210 fio_file_hash_unlock();
1219 static void free_already_allocated(void)
1221 struct flist_head *entry, *tmp;
1222 struct file_name *fn;
1224 if (flist_empty(&filename_list))
1227 fio_file_hash_lock();
1228 flist_for_each_safe(entry, tmp, &filename_list) {
1229 fn = flist_entry(entry, struct file_name, list);
1231 flist_del(&fn->list);
1235 fio_file_hash_unlock();
1238 static struct fio_file *alloc_new_file(struct thread_data *td)
1242 f = smalloc(sizeof(*f));
1244 log_err("fio: smalloc OOM\n");
1251 fio_file_reset(td, f);
1255 int add_file(struct thread_data *td, const char *fname, int numjob, int inc)
1257 int cur_files = td->files_index;
1258 char file_name[PATH_MAX];
1262 dprint(FD_FILE, "add file %s\n", fname);
1264 if (td->o.directory)
1265 len = set_name_idx(file_name, td->o.directory, numjob);
1267 sprintf(file_name + len, "%s", fname);
1269 /* clean cloned siblings using existing files */
1270 if (numjob && is_already_allocated(file_name))
1273 f = alloc_new_file(td);
1275 if (td->files_size <= td->files_index) {
1276 unsigned int new_size = td->o.nr_files + 1;
1278 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1280 td->files = realloc(td->files, new_size * sizeof(f));
1281 if (td->files == NULL) {
1282 log_err("fio: realloc OOM\n");
1285 if (td->o.file_lock_mode != FILE_LOCK_NONE) {
1286 td->file_locks = realloc(td->file_locks, new_size);
1287 if (!td->file_locks) {
1288 log_err("fio: realloc OOM\n");
1291 td->file_locks[cur_files] = FILE_LOCK_NONE;
1293 td->files_size = new_size;
1295 td->files[cur_files] = f;
1296 f->fileno = cur_files;
1299 * init function, io engine may not be loaded yet
1301 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1302 f->real_file_size = -1ULL;
1304 f->file_name = smalloc_strdup(file_name);
1305 if (!f->file_name) {
1306 log_err("fio: smalloc OOM\n");
1312 switch (td->o.file_lock_mode) {
1313 case FILE_LOCK_NONE:
1315 case FILE_LOCK_READWRITE:
1316 f->rwlock = fio_rwlock_init();
1318 case FILE_LOCK_EXCLUSIVE:
1319 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
1322 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1327 if (f->filetype == FIO_TYPE_FILE)
1328 td->nr_normal_files++;
1330 set_already_allocated(file_name);
1335 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1341 int add_file_exclusive(struct thread_data *td, const char *fname)
1346 for_each_file(td, f, i) {
1347 if (!strcmp(f->file_name, fname))
1351 return add_file(td, fname, 0, 1);
1354 void get_file(struct fio_file *f)
1356 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
1357 assert(fio_file_open(f));
1361 int put_file(struct thread_data *td, struct fio_file *f)
1363 int f_ret = 0, ret = 0;
1365 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
1367 if (!fio_file_open(f)) {
1368 assert(f->fd == -1);
1372 assert(f->references);
1373 if (--f->references)
1376 if (should_fsync(td) && td->o.fsync_on_close) {
1377 f_ret = fsync(f->fd);
1382 if (td->io_ops->close_file)
1383 ret = td->io_ops->close_file(td, f);
1388 td->nr_open_files--;
1389 fio_file_clear_open(f);
1390 assert(f->fd == -1);
1394 void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
1396 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1399 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1400 if (ddir == DDIR_READ)
1401 fio_rwlock_read(f->rwlock);
1403 fio_rwlock_write(f->rwlock);
1404 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1405 fio_mutex_down(f->lock);
1407 td->file_locks[f->fileno] = td->o.file_lock_mode;
1410 void unlock_file(struct thread_data *td, struct fio_file *f)
1412 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1415 if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1416 fio_rwlock_unlock(f->rwlock);
1417 else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1418 fio_mutex_up(f->lock);
1420 td->file_locks[f->fileno] = FILE_LOCK_NONE;
1423 void unlock_file_all(struct thread_data *td, struct fio_file *f)
1425 if (td->o.file_lock_mode == FILE_LOCK_NONE || !td->file_locks)
1427 if (td->file_locks[f->fileno] != FILE_LOCK_NONE)
1431 static int recurse_dir(struct thread_data *td, const char *dirname)
1437 D = opendir(dirname);
1439 char buf[FIO_VERROR_SIZE];
1441 snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
1442 td_verror(td, errno, buf);
1446 while ((dir = readdir(D)) != NULL) {
1447 char full_path[PATH_MAX];
1450 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1453 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
1455 if (lstat(full_path, &sb) == -1) {
1456 if (errno != ENOENT) {
1457 td_verror(td, errno, "stat");
1463 if (S_ISREG(sb.st_mode)) {
1464 add_file(td, full_path, 0, 1);
1467 if (!S_ISDIR(sb.st_mode))
1470 ret = recurse_dir(td, full_path);
1479 int add_dir_files(struct thread_data *td, const char *path)
1481 int ret = recurse_dir(td, path);
1484 log_info("fio: opendir added %d files\n", td->o.nr_files);
1489 void dup_files(struct thread_data *td, struct thread_data *org)
1494 dprint(FD_FILE, "dup files: %d\n", org->files_index);
1499 td->files = malloc(org->files_index * sizeof(f));
1501 if (td->o.file_lock_mode != FILE_LOCK_NONE)
1502 td->file_locks = malloc(org->files_index);
1504 for_each_file(org, f, i) {
1505 struct fio_file *__f;
1507 __f = alloc_new_file(td);
1510 __f->file_name = smalloc_strdup(f->file_name);
1511 if (!__f->file_name) {
1512 log_err("fio: smalloc OOM\n");
1516 __f->filetype = f->filetype;
1519 if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1520 __f->lock = f->lock;
1521 else if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1522 __f->rwlock = f->rwlock;
1529 * Returns the index that matches the filename, or -1 if not there
1531 int get_fileno(struct thread_data *td, const char *fname)
1536 for_each_file(td, f, i)
1537 if (!strcmp(f->file_name, fname))
1544 * For log usage, where we add/open/close files automatically
1546 void free_release_files(struct thread_data *td)
1550 td->o.open_files = 0;
1551 td->files_index = 0;
1552 td->nr_normal_files = 0;
1555 void fio_file_reset(struct thread_data *td, struct fio_file *f)
1559 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1560 f->last_pos[i] = f->file_offset;
1561 f->last_start[i] = -1ULL;
1564 if (fio_file_axmap(f))
1565 axmap_reset(f->io_axmap);
1566 else if (fio_file_lfsr(f))
1567 lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
1570 int fio_files_done(struct thread_data *td)
1575 for_each_file(td, f, i)
1576 if (!fio_file_done(f))
1582 /* free memory used in initialization phase only */
1583 void filesetup_mem_free(void)
1585 free_already_allocated();