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)
692 strncpy(buf, f->file_name, 255);
694 if (stat(buf, &sb) < 0) {
698 if (stat(buf, &sb) < 0)
703 flist_for_each(n, &list) {
704 fm = flist_entry(n, struct fio_mount, list);
705 if (fm->key == sb.st_dev)
714 fm = malloc(sizeof(*fm));
715 strcpy(fm->__base, buf);
716 fm->base = basename(fm->__base);
718 flist_add(&fm->list, &list);
721 flist_for_each_safe(n, tmp, &list) {
722 unsigned long long sz;
724 fm = flist_entry(n, struct fio_mount, list);
725 flist_del(&fm->list);
727 sz = get_fs_size(fm->base);
728 if (sz && sz != -1ULL)
737 uint64_t get_start_offset(struct thread_data *td, struct fio_file *f)
739 struct thread_options *o = &td->o;
741 if (o->file_append && f->filetype == FIO_TYPE_FILE)
742 return f->real_file_size;
744 return td->o.start_offset +
745 (td->thread_number - 1) * td->o.offset_increment;
749 * Open the files and setup files sizes, creating files if necessary.
751 int setup_files(struct thread_data *td)
753 unsigned long long total_size, extend_size;
754 struct thread_options *o = &td->o;
756 unsigned int i, nr_fs_extra = 0;
757 int err = 0, need_extend;
759 const unsigned int bs = td_min_bs(td);
762 dprint(FD_FILE, "setup files\n");
764 old_state = td_bump_runstate(td, TD_SETTING_UP);
766 if (o->read_iolog_file)
770 * if ioengine defines a setup() method, it's responsible for
771 * opening the files and setting f->real_file_size to indicate
772 * the valid range for that file.
774 if (td->io_ops->setup)
775 err = td->io_ops->setup(td);
777 err = get_file_sizes(td);
783 * check sizes. if the files/devices do not exist and the size
784 * isn't passed to fio, abort.
787 for_each_file(td, f, i) {
788 if (f->real_file_size == -1ULL)
791 total_size += f->real_file_size;
795 td->fill_device_size = get_fs_free_counts(td);
798 * device/file sizes are zero and no size given, punt
800 if ((!total_size || total_size == -1ULL) && !o->size &&
801 !(td->io_ops->flags & FIO_NOIO) && !o->fill_device &&
802 !(o->nr_files && (o->file_size_low || o->file_size_high))) {
803 log_err("%s: you need to specify size=\n", o->name);
804 td_verror(td, EINVAL, "total_file_size");
809 * Calculate per-file size and potential extra size for the
810 * first files, if needed.
812 if (!o->file_size_low && o->nr_files) {
815 fs = o->size / o->nr_files;
816 all_fs = fs * o->nr_files;
818 if (all_fs < o->size)
819 nr_fs_extra = (o->size - all_fs) / bs;
823 * now file sizes are known, so we can set ->io_size. if size= is
824 * not given, ->io_size is just equal to ->real_file_size. if size
825 * is given, ->io_size is size / nr_files.
827 extend_size = total_size = 0;
829 for_each_file(td, f, i) {
830 f->file_offset = get_start_offset(td, f);
832 if (!o->file_size_low) {
834 * no file size range given, file size is equal to
835 * total size divided by number of files. If that is
836 * zero, set it to the real file size. If the size
837 * doesn't divide nicely with the min blocksize,
838 * make the first files bigger.
847 f->io_size = f->real_file_size - f->file_offset;
848 } else if (f->real_file_size < o->file_size_low ||
849 f->real_file_size > o->file_size_high) {
850 if (f->file_offset > o->file_size_low)
853 * file size given. if it's fixed, use that. if it's a
854 * range, generate a random size in-between.
856 if (o->file_size_low == o->file_size_high)
857 f->io_size = o->file_size_low - f->file_offset;
859 f->io_size = get_rand_file_size(td)
863 f->io_size = f->real_file_size - f->file_offset;
865 if (f->io_size == -1ULL)
869 f->io_size = (f->io_size * o->size_percent) / 100;
870 total_size += f->io_size;
873 if (f->filetype == FIO_TYPE_FILE &&
874 (f->io_size + f->file_offset) > f->real_file_size &&
875 !(td->io_ops->flags & FIO_DISKLESSIO)) {
876 if (!o->create_on_open) {
878 extend_size += (f->io_size + f->file_offset);
880 f->real_file_size = f->io_size + f->file_offset;
881 fio_file_set_extend(f);
885 if (!o->size || o->size > total_size)
886 o->size = total_size;
889 * See if we need to extend some files
893 if (output_format == FIO_OUTPUT_NORMAL)
894 log_info("%s: Laying out IO file(s) (%u file(s) /"
895 " %lluMB)\n", o->name, need_extend,
898 for_each_file(td, f, i) {
899 unsigned long long old_len = -1ULL, extend_len = -1ULL;
901 if (!fio_file_extend(f))
904 assert(f->filetype == FIO_TYPE_FILE);
905 fio_file_clear_extend(f);
906 if (!o->fill_device) {
907 old_len = f->real_file_size;
908 extend_len = f->io_size + f->file_offset -
911 f->real_file_size = (f->io_size + f->file_offset);
912 err = extend_file(td, f);
916 err = __file_invalidate_cache(td, f, old_len,
930 o->zone_size = o->size;
933 * iolog already set the total io size, if we read back
936 if (!o->read_iolog_file)
937 td->total_io_size = o->size * o->loops;
943 td_restore_runstate(td, old_state);
946 log_err("%s: you need to specify valid offset=\n", o->name);
948 td_restore_runstate(td, old_state);
952 int pre_read_files(struct thread_data *td)
957 dprint(FD_FILE, "pre_read files\n");
959 for_each_file(td, f, i) {
960 pre_read_file(td, f);
966 static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
968 unsigned int range_size, seed;
969 unsigned long nranges;
972 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
973 file_size = min(f->real_file_size, f->io_size);
975 nranges = (file_size + range_size - 1) / range_size;
977 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
978 if (!td->o.rand_repeatable)
979 seed = td->rand_seeds[4];
981 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
982 zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed);
984 pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed);
989 static int init_rand_distribution(struct thread_data *td)
995 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
998 state = td_bump_runstate(td, TD_SETTING_UP);
1000 for_each_file(td, f, i)
1001 __init_rand_distribution(td, f);
1003 td_restore_runstate(td, state);
1008 int init_random_map(struct thread_data *td)
1010 unsigned long long blocks;
1014 if (init_rand_distribution(td))
1019 for_each_file(td, f, i) {
1020 uint64_t file_size = min(f->real_file_size, f->io_size);
1022 blocks = file_size / (unsigned long long) td->o.rw_min_bs;
1024 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
1027 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
1029 if (!lfsr_init(&f->lfsr, blocks, seed, 0))
1031 } else if (!td->o.norandommap) {
1032 f->io_axmap = axmap_new(blocks);
1035 } else if (td->o.norandommap)
1038 if (!td->o.softrandommap) {
1039 log_err("fio: failed allocating random map. If running"
1040 " a large number of jobs, try the 'norandommap'"
1041 " option or set 'softrandommap'. Or give"
1042 " a larger --alloc-size to fio.\n");
1046 log_info("fio: file %s failed allocating random map. Running "
1047 "job without.\n", f->file_name);
1053 void close_files(struct thread_data *td)
1058 for_each_file(td, f, i) {
1059 if (fio_file_open(f))
1060 td_io_close_file(td, f);
1064 void close_and_free_files(struct thread_data *td)
1069 dprint(FD_FILE, "close files\n");
1071 for_each_file(td, f, i) {
1072 if (fio_file_open(f))
1073 td_io_close_file(td, f);
1075 remove_file_hash(f);
1077 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1078 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1079 unlink(f->file_name);
1082 sfree(f->file_name);
1083 f->file_name = NULL;
1084 axmap_free(f->io_axmap);
1089 td->o.filename = NULL;
1091 free(td->file_locks);
1092 td->files_index = 0;
1094 td->file_locks = NULL;
1095 td->o.file_lock_mode = FILE_LOCK_NONE;
1099 static void get_file_type(struct fio_file *f)
1103 if (!strcmp(f->file_name, "-"))
1104 f->filetype = FIO_TYPE_PIPE;
1106 f->filetype = FIO_TYPE_FILE;
1108 /* \\.\ is the device namespace in Windows, where every file is
1110 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1111 f->filetype = FIO_TYPE_BD;
1113 if (!stat(f->file_name, &sb)) {
1114 if (S_ISBLK(sb.st_mode))
1115 f->filetype = FIO_TYPE_BD;
1116 else if (S_ISCHR(sb.st_mode))
1117 f->filetype = FIO_TYPE_CHAR;
1118 else if (S_ISFIFO(sb.st_mode))
1119 f->filetype = FIO_TYPE_PIPE;
1123 static int __is_already_allocated(const char *fname)
1125 struct flist_head *entry;
1128 if (flist_empty(&filename_list))
1131 flist_for_each(entry, &filename_list) {
1132 filename = flist_entry(entry, struct file_name, list)->filename;
1134 if (strcmp(filename, fname) == 0)
1141 static int is_already_allocated(const char *fname)
1145 fio_file_hash_lock();
1146 ret = __is_already_allocated(fname);
1147 fio_file_hash_unlock();
1151 static void set_already_allocated(const char *fname)
1153 struct file_name *fn;
1155 fn = malloc(sizeof(struct file_name));
1156 fn->filename = strdup(fname);
1158 fio_file_hash_lock();
1159 if (!__is_already_allocated(fname)) {
1160 flist_add_tail(&fn->list, &filename_list);
1163 fio_file_hash_unlock();
1172 static void free_already_allocated(void)
1174 struct flist_head *entry, *tmp;
1175 struct file_name *fn;
1177 if (flist_empty(&filename_list))
1180 fio_file_hash_lock();
1181 flist_for_each_safe(entry, tmp, &filename_list) {
1182 fn = flist_entry(entry, struct file_name, list);
1184 flist_del(&fn->list);
1188 fio_file_hash_unlock();
1191 static struct fio_file *alloc_new_file(struct thread_data *td)
1195 f = smalloc(sizeof(*f));
1197 log_err("fio: smalloc OOM\n");
1204 fio_file_reset(td, f);
1208 int add_file(struct thread_data *td, const char *fname, int numjob, int inc)
1210 int cur_files = td->files_index;
1211 char file_name[PATH_MAX];
1215 dprint(FD_FILE, "add file %s\n", fname);
1217 if (td->o.directory)
1218 len = set_name_idx(file_name, td->o.directory, numjob);
1220 sprintf(file_name + len, "%s", fname);
1222 /* clean cloned siblings using existing files */
1223 if (numjob && is_already_allocated(file_name))
1226 f = alloc_new_file(td);
1228 if (td->files_size <= td->files_index) {
1229 unsigned int new_size = td->o.nr_files + 1;
1231 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1233 td->files = realloc(td->files, new_size * sizeof(f));
1234 if (td->files == NULL) {
1235 log_err("fio: realloc OOM\n");
1238 if (td->o.file_lock_mode != FILE_LOCK_NONE) {
1239 td->file_locks = realloc(td->file_locks, new_size);
1240 if (!td->file_locks) {
1241 log_err("fio: realloc OOM\n");
1244 td->file_locks[cur_files] = FILE_LOCK_NONE;
1246 td->files_size = new_size;
1248 td->files[cur_files] = f;
1249 f->fileno = cur_files;
1252 * init function, io engine may not be loaded yet
1254 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1255 f->real_file_size = -1ULL;
1257 f->file_name = smalloc_strdup(file_name);
1258 if (!f->file_name) {
1259 log_err("fio: smalloc OOM\n");
1265 switch (td->o.file_lock_mode) {
1266 case FILE_LOCK_NONE:
1268 case FILE_LOCK_READWRITE:
1269 f->rwlock = fio_rwlock_init();
1271 case FILE_LOCK_EXCLUSIVE:
1272 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
1275 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1280 if (f->filetype == FIO_TYPE_FILE)
1281 td->nr_normal_files++;
1283 set_already_allocated(file_name);
1288 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1294 int add_file_exclusive(struct thread_data *td, const char *fname)
1299 for_each_file(td, f, i) {
1300 if (!strcmp(f->file_name, fname))
1304 return add_file(td, fname, 0, 1);
1307 void get_file(struct fio_file *f)
1309 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
1310 assert(fio_file_open(f));
1314 int put_file(struct thread_data *td, struct fio_file *f)
1316 int f_ret = 0, ret = 0;
1318 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
1320 if (!fio_file_open(f)) {
1321 assert(f->fd == -1);
1325 assert(f->references);
1326 if (--f->references)
1329 if (should_fsync(td) && td->o.fsync_on_close)
1330 f_ret = fsync(f->fd);
1332 if (td->io_ops->close_file)
1333 ret = td->io_ops->close_file(td, f);
1338 td->nr_open_files--;
1339 fio_file_clear_open(f);
1340 assert(f->fd == -1);
1344 void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
1346 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1349 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1350 if (ddir == DDIR_READ)
1351 fio_rwlock_read(f->rwlock);
1353 fio_rwlock_write(f->rwlock);
1354 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1355 fio_mutex_down(f->lock);
1357 td->file_locks[f->fileno] = td->o.file_lock_mode;
1360 void unlock_file(struct thread_data *td, struct fio_file *f)
1362 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1365 if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1366 fio_rwlock_unlock(f->rwlock);
1367 else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1368 fio_mutex_up(f->lock);
1370 td->file_locks[f->fileno] = FILE_LOCK_NONE;
1373 void unlock_file_all(struct thread_data *td, struct fio_file *f)
1375 if (td->o.file_lock_mode == FILE_LOCK_NONE || !td->file_locks)
1377 if (td->file_locks[f->fileno] != FILE_LOCK_NONE)
1381 static int recurse_dir(struct thread_data *td, const char *dirname)
1387 D = opendir(dirname);
1389 char buf[FIO_VERROR_SIZE];
1391 snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
1392 td_verror(td, errno, buf);
1396 while ((dir = readdir(D)) != NULL) {
1397 char full_path[PATH_MAX];
1400 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1403 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
1405 if (lstat(full_path, &sb) == -1) {
1406 if (errno != ENOENT) {
1407 td_verror(td, errno, "stat");
1413 if (S_ISREG(sb.st_mode)) {
1414 add_file(td, full_path, 0, 1);
1417 if (!S_ISDIR(sb.st_mode))
1420 ret = recurse_dir(td, full_path);
1429 int add_dir_files(struct thread_data *td, const char *path)
1431 int ret = recurse_dir(td, path);
1434 log_info("fio: opendir added %d files\n", td->o.nr_files);
1439 void dup_files(struct thread_data *td, struct thread_data *org)
1444 dprint(FD_FILE, "dup files: %d\n", org->files_index);
1449 td->files = malloc(org->files_index * sizeof(f));
1451 if (td->o.file_lock_mode != FILE_LOCK_NONE)
1452 td->file_locks = malloc(org->files_index);
1454 for_each_file(org, f, i) {
1455 struct fio_file *__f;
1457 __f = alloc_new_file(td);
1460 __f->file_name = smalloc_strdup(f->file_name);
1461 if (!__f->file_name) {
1462 log_err("fio: smalloc OOM\n");
1466 __f->filetype = f->filetype;
1469 if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1470 __f->lock = f->lock;
1471 else if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1472 __f->rwlock = f->rwlock;
1479 * Returns the index that matches the filename, or -1 if not there
1481 int get_fileno(struct thread_data *td, const char *fname)
1486 for_each_file(td, f, i)
1487 if (!strcmp(f->file_name, fname))
1494 * For log usage, where we add/open/close files automatically
1496 void free_release_files(struct thread_data *td)
1499 td->files_index = 0;
1500 td->nr_normal_files = 0;
1503 void fio_file_reset(struct thread_data *td, struct fio_file *f)
1505 f->last_pos = f->file_offset;
1506 f->last_start = -1ULL;
1508 axmap_reset(f->io_axmap);
1509 if (td->o.random_generator == FIO_RAND_GEN_LFSR)
1510 lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
1513 int fio_files_done(struct thread_data *td)
1518 for_each_file(td, f, i)
1519 if (!fio_file_done(f))
1525 /* free memory used in initialization phase only */
1526 void filesetup_mem_free(void)
1528 free_already_allocated();