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 if (td->o.use_os_rand) {
265 r = os_random_long(&td->file_size_state);
266 sized = td->o.file_size_high - td->o.file_size_low;
267 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
269 r = __rand(&td->__file_size_state);
270 sized = td->o.file_size_high - td->o.file_size_low;
271 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0)));
274 ret += td->o.file_size_low;
275 ret -= (ret % td->o.rw_min_bs);
279 static int file_size(struct thread_data *td, struct fio_file *f)
283 if (stat(f->file_name, &st) == -1) {
284 td_verror(td, errno, "fstat");
288 f->real_file_size = st.st_size;
292 static int bdev_size(struct thread_data *td, struct fio_file *f)
294 unsigned long long bytes = 0;
297 if (td->io_ops->open_file(td, f)) {
298 log_err("fio: failed opening blockdev %s for size check\n",
303 r = blockdev_size(f, &bytes);
305 td_verror(td, r, "blockdev_size");
310 log_err("%s: zero sized block device?\n", f->file_name);
314 f->real_file_size = bytes;
315 td->io_ops->close_file(td, f);
318 td->io_ops->close_file(td, f);
322 static int char_size(struct thread_data *td, struct fio_file *f)
324 #ifdef FIO_HAVE_CHARDEV_SIZE
325 unsigned long long bytes = 0;
328 if (td->io_ops->open_file(td, f)) {
329 log_err("fio: failed opening blockdev %s for size check\n",
334 r = chardev_size(f, &bytes);
336 td_verror(td, r, "chardev_size");
341 log_err("%s: zero sized char device?\n", f->file_name);
345 f->real_file_size = bytes;
346 td->io_ops->close_file(td, f);
349 td->io_ops->close_file(td, f);
352 f->real_file_size = -1ULL;
357 static int get_file_size(struct thread_data *td, struct fio_file *f)
361 if (fio_file_size_known(f))
364 if (f->filetype == FIO_TYPE_FILE)
365 ret = file_size(td, f);
366 else if (f->filetype == FIO_TYPE_BD)
367 ret = bdev_size(td, f);
368 else if (f->filetype == FIO_TYPE_CHAR)
369 ret = char_size(td, f);
371 f->real_file_size = -1;
376 if (f->file_offset > f->real_file_size) {
377 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
378 (unsigned long long) f->file_offset,
379 (unsigned long long) f->real_file_size);
383 fio_file_set_size_known(f);
387 static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
388 unsigned long long off,
389 unsigned long long len)
400 off = f->file_offset;
402 if (len == -1ULL || off == -1ULL)
405 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
408 if (td->io_ops->invalidate)
409 ret = td->io_ops->invalidate(td, f);
410 else if (f->mmap_ptr) {
411 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
413 if (f->filetype == FIO_TYPE_BD)
414 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
416 } else if (f->filetype == FIO_TYPE_FILE) {
417 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
418 } else if (f->filetype == FIO_TYPE_BD) {
419 ret = blockdev_invalidate_cache(f);
420 if (ret < 0 && errno == EACCES && geteuid()) {
422 log_err("fio: only root may flush block "
423 "devices. Cache flush bypassed!\n");
428 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
432 * Cache flushing isn't a fatal condition, and we know it will
433 * happen on some platforms where we don't have the proper
434 * function to flush eg block device caches. So just warn and
435 * continue on our way.
438 log_info("fio: cache invalidation of %s failed: %s\n", f->file_name, strerror(errno));
446 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
448 if (!fio_file_open(f))
451 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
454 int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
458 dprint(FD_FILE, "fd close %s\n", f->file_name);
462 if (close(f->fd) < 0)
467 if (f->shadow_fd != -1) {
476 int file_lookup_open(struct fio_file *f, int flags)
478 struct fio_file *__f;
481 __f = lookup_file_hash(f->file_name);
483 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
485 * racy, need the __f->lock locked
490 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
498 f->fd = open(f->file_name, flags, 0600);
502 static int file_close_shadow_fds(struct thread_data *td)
508 for_each_file(td, f, i) {
509 if (f->shadow_fd == -1)
520 int generic_open_file(struct thread_data *td, struct fio_file *f)
526 dprint(FD_FILE, "fd open %s\n", f->file_name);
528 if (td_trim(td) && f->filetype != FIO_TYPE_BD) {
529 log_err("fio: trim only applies to block device\n");
533 if (!strcmp(f->file_name, "-")) {
535 log_err("fio: can't read/write to stdin/out\n");
541 * move output logging to stderr, if we are writing to stdout
550 flags |= OS_O_DIRECT;
553 td_verror(td, EINVAL, "OS does not support atomic IO");
556 flags |= OS_O_DIRECT | FIO_O_ATOMIC;
560 if (td->o.create_on_open)
563 if (f->filetype != FIO_TYPE_FILE)
564 flags |= FIO_O_NOATIME;
571 if (f->filetype == FIO_TYPE_FILE)
575 f->fd = dup(STDOUT_FILENO);
577 from_hash = file_lookup_open(f, flags);
578 } else if (td_read(td)) {
579 if (f->filetype == FIO_TYPE_CHAR && !read_only)
585 f->fd = dup(STDIN_FILENO);
587 from_hash = file_lookup_open(f, flags);
590 from_hash = file_lookup_open(f, flags);
594 char buf[FIO_VERROR_SIZE];
597 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
598 flags &= ~FIO_O_NOATIME;
601 if (__e == EMFILE && file_close_shadow_fds(td))
604 snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
606 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
607 log_err("fio: looks like your file system does not " \
608 "support direct=1/buffered=0\n");
611 td_verror(td, __e, buf);
615 if (!from_hash && f->fd != -1) {
616 if (add_file_hash(f)) {
620 * Stash away descriptor for later close. This is to
621 * work-around a "feature" on Linux, where a close of
622 * an fd that has been opened for write will trigger
623 * udev to call blkid to check partitions, fs id, etc.
624 * That pollutes the device cache, which can slow down
625 * unbuffered accesses.
627 if (f->shadow_fd == -1)
628 f->shadow_fd = f->fd;
631 * OK to ignore, we haven't done anything
634 ret = generic_close_file(td, f);
643 int generic_get_file_size(struct thread_data *td, struct fio_file *f)
645 return get_file_size(td, f);
649 * open/close all files, so that ->real_file_size gets set
651 static int get_file_sizes(struct thread_data *td)
657 for_each_file(td, f, i) {
658 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
661 if (td_io_get_file_size(td, f)) {
662 if (td->error != ENOENT) {
663 log_err("%s\n", td->verror);
670 if (f->real_file_size == -1ULL && td->o.size)
671 f->real_file_size = td->o.size / td->o.nr_files;
678 struct flist_head list;
685 * Get free number of bytes for each file on each unique mount.
687 static unsigned long long get_fs_free_counts(struct thread_data *td)
689 struct flist_head *n, *tmp;
690 unsigned long long ret = 0;
691 struct fio_mount *fm;
696 for_each_file(td, f, i) {
700 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
701 if (f->real_file_size != -1ULL)
702 ret += f->real_file_size;
704 } else if (f->filetype != FIO_TYPE_FILE)
708 strncpy(buf, f->file_name, 255);
710 if (stat(buf, &sb) < 0) {
714 if (stat(buf, &sb) < 0)
719 flist_for_each(n, &list) {
720 fm = flist_entry(n, struct fio_mount, list);
721 if (fm->key == sb.st_dev)
730 fm = calloc(1, sizeof(*fm));
731 strncpy(fm->__base, buf, sizeof(fm->__base) - 1);
732 fm->base = basename(fm->__base);
734 flist_add(&fm->list, &list);
737 flist_for_each_safe(n, tmp, &list) {
738 unsigned long long sz;
740 fm = flist_entry(n, struct fio_mount, list);
741 flist_del(&fm->list);
743 sz = get_fs_size(fm->base);
744 if (sz && sz != -1ULL)
753 uint64_t get_start_offset(struct thread_data *td, struct fio_file *f)
755 struct thread_options *o = &td->o;
757 if (o->file_append && f->filetype == FIO_TYPE_FILE)
758 return f->real_file_size;
760 return td->o.start_offset +
761 td->subjob_number * td->o.offset_increment;
765 * Open the files and setup files sizes, creating files if necessary.
767 int setup_files(struct thread_data *td)
769 unsigned long long total_size, extend_size;
770 struct thread_options *o = &td->o;
772 unsigned int i, nr_fs_extra = 0;
773 int err = 0, need_extend;
775 const unsigned int bs = td_min_bs(td);
778 dprint(FD_FILE, "setup files\n");
780 old_state = td_bump_runstate(td, TD_SETTING_UP);
782 if (o->read_iolog_file)
786 * if ioengine defines a setup() method, it's responsible for
787 * opening the files and setting f->real_file_size to indicate
788 * the valid range for that file.
790 if (td->io_ops->setup)
791 err = td->io_ops->setup(td);
793 err = get_file_sizes(td);
799 * check sizes. if the files/devices do not exist and the size
800 * isn't passed to fio, abort.
803 for_each_file(td, f, i) {
804 if (f->real_file_size == -1ULL)
807 total_size += f->real_file_size;
811 td->fill_device_size = get_fs_free_counts(td);
814 * device/file sizes are zero and no size given, punt
816 if ((!total_size || total_size == -1ULL) && !o->size &&
817 !(td->io_ops->flags & FIO_NOIO) && !o->fill_device &&
818 !(o->nr_files && (o->file_size_low || o->file_size_high))) {
819 log_err("%s: you need to specify size=\n", o->name);
820 td_verror(td, EINVAL, "total_file_size");
825 * Calculate per-file size and potential extra size for the
826 * first files, if needed.
828 if (!o->file_size_low && o->nr_files) {
831 fs = o->size / o->nr_files;
832 all_fs = fs * o->nr_files;
834 if (all_fs < o->size)
835 nr_fs_extra = (o->size - all_fs) / bs;
839 * now file sizes are known, so we can set ->io_size. if size= is
840 * not given, ->io_size is just equal to ->real_file_size. if size
841 * is given, ->io_size is size / nr_files.
843 extend_size = total_size = 0;
845 for_each_file(td, f, i) {
846 f->file_offset = get_start_offset(td, f);
848 if (!o->file_size_low) {
850 * no file size range given, file size is equal to
851 * total size divided by number of files. If that is
852 * zero, set it to the real file size. If the size
853 * doesn't divide nicely with the min blocksize,
854 * make the first files bigger.
863 f->io_size = f->real_file_size - f->file_offset;
864 } else if (f->real_file_size < o->file_size_low ||
865 f->real_file_size > o->file_size_high) {
866 if (f->file_offset > o->file_size_low)
869 * file size given. if it's fixed, use that. if it's a
870 * range, generate a random size in-between.
872 if (o->file_size_low == o->file_size_high)
873 f->io_size = o->file_size_low - f->file_offset;
875 f->io_size = get_rand_file_size(td)
879 f->io_size = f->real_file_size - f->file_offset;
881 if (f->io_size == -1ULL)
885 f->io_size = (f->io_size * o->size_percent) / 100;
886 total_size += f->io_size;
889 if (f->filetype == FIO_TYPE_FILE &&
890 (f->io_size + f->file_offset) > f->real_file_size &&
891 !(td->io_ops->flags & FIO_DISKLESSIO)) {
892 if (!o->create_on_open) {
894 extend_size += (f->io_size + f->file_offset);
896 f->real_file_size = f->io_size + f->file_offset;
897 fio_file_set_extend(f);
901 if (!o->size || o->size > total_size)
902 o->size = total_size;
904 if (o->size < td_min_bs(td)) {
905 log_err("fio: blocksize too large for data set\n");
910 * See if we need to extend some files
914 if (output_format == FIO_OUTPUT_NORMAL)
915 log_info("%s: Laying out IO file(s) (%u file(s) /"
916 " %lluMB)\n", o->name, need_extend,
919 for_each_file(td, f, i) {
920 unsigned long long old_len = -1ULL, extend_len = -1ULL;
922 if (!fio_file_extend(f))
925 assert(f->filetype == FIO_TYPE_FILE);
926 fio_file_clear_extend(f);
927 if (!o->fill_device) {
928 old_len = f->real_file_size;
929 extend_len = f->io_size + f->file_offset -
932 f->real_file_size = (f->io_size + f->file_offset);
933 err = extend_file(td, f);
937 err = __file_invalidate_cache(td, f, old_len,
941 * Shut up static checker
957 o->zone_size = o->size;
960 * iolog already set the total io size, if we read back
963 if (!o->read_iolog_file) {
965 td->total_io_size = o->io_limit * o->loops;
967 td->total_io_size = o->size * o->loops;
974 td_restore_runstate(td, old_state);
977 log_err("%s: you need to specify valid offset=\n", o->name);
979 td_restore_runstate(td, old_state);
983 int pre_read_files(struct thread_data *td)
988 dprint(FD_FILE, "pre_read files\n");
990 for_each_file(td, f, i) {
991 pre_read_file(td, f);
997 static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
999 unsigned int range_size, seed;
1000 unsigned long nranges;
1003 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
1004 file_size = min(f->real_file_size, f->io_size);
1006 nranges = (file_size + range_size - 1) / range_size;
1008 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
1009 if (!td->o.rand_repeatable)
1010 seed = td->rand_seeds[4];
1012 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
1013 zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed);
1015 pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed);
1020 static int init_rand_distribution(struct thread_data *td)
1026 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
1029 state = td_bump_runstate(td, TD_SETTING_UP);
1031 for_each_file(td, f, i)
1032 __init_rand_distribution(td, f);
1034 td_restore_runstate(td, state);
1039 int init_random_map(struct thread_data *td)
1041 unsigned long long blocks;
1045 if (init_rand_distribution(td))
1050 for_each_file(td, f, i) {
1051 uint64_t file_size = min(f->real_file_size, f->io_size);
1053 blocks = file_size / (unsigned long long) td->o.rw_min_bs;
1055 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
1058 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
1060 if (!lfsr_init(&f->lfsr, blocks, seed, 0))
1062 } else if (!td->o.norandommap) {
1063 f->io_axmap = axmap_new(blocks);
1066 } else if (td->o.norandommap)
1069 if (!td->o.softrandommap) {
1070 log_err("fio: failed allocating random map. If running"
1071 " a large number of jobs, try the 'norandommap'"
1072 " option or set 'softrandommap'. Or give"
1073 " a larger --alloc-size to fio.\n");
1077 log_info("fio: file %s failed allocating random map. Running "
1078 "job without.\n", f->file_name);
1084 void close_files(struct thread_data *td)
1089 for_each_file(td, f, i) {
1090 if (fio_file_open(f))
1091 td_io_close_file(td, f);
1095 void close_and_free_files(struct thread_data *td)
1100 dprint(FD_FILE, "close files\n");
1102 for_each_file(td, f, i) {
1103 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1104 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1105 td_io_unlink_file(td, f);
1108 if (fio_file_open(f))
1109 td_io_close_file(td, f);
1111 remove_file_hash(f);
1113 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1114 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1115 td_io_unlink_file(td, f);
1118 sfree(f->file_name);
1119 f->file_name = NULL;
1120 axmap_free(f->io_axmap);
1125 td->o.filename = NULL;
1127 free(td->file_locks);
1128 td->files_index = 0;
1130 td->file_locks = NULL;
1131 td->o.file_lock_mode = FILE_LOCK_NONE;
1135 static void get_file_type(struct fio_file *f)
1139 if (!strcmp(f->file_name, "-"))
1140 f->filetype = FIO_TYPE_PIPE;
1142 f->filetype = FIO_TYPE_FILE;
1144 /* \\.\ is the device namespace in Windows, where every file is
1146 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1147 f->filetype = FIO_TYPE_BD;
1149 if (!stat(f->file_name, &sb)) {
1150 if (S_ISBLK(sb.st_mode))
1151 f->filetype = FIO_TYPE_BD;
1152 else if (S_ISCHR(sb.st_mode))
1153 f->filetype = FIO_TYPE_CHAR;
1154 else if (S_ISFIFO(sb.st_mode))
1155 f->filetype = FIO_TYPE_PIPE;
1159 static int __is_already_allocated(const char *fname)
1161 struct flist_head *entry;
1164 if (flist_empty(&filename_list))
1167 flist_for_each(entry, &filename_list) {
1168 filename = flist_entry(entry, struct file_name, list)->filename;
1170 if (strcmp(filename, fname) == 0)
1177 static int is_already_allocated(const char *fname)
1181 fio_file_hash_lock();
1182 ret = __is_already_allocated(fname);
1183 fio_file_hash_unlock();
1187 static void set_already_allocated(const char *fname)
1189 struct file_name *fn;
1191 fn = malloc(sizeof(struct file_name));
1192 fn->filename = strdup(fname);
1194 fio_file_hash_lock();
1195 if (!__is_already_allocated(fname)) {
1196 flist_add_tail(&fn->list, &filename_list);
1199 fio_file_hash_unlock();
1208 static void free_already_allocated(void)
1210 struct flist_head *entry, *tmp;
1211 struct file_name *fn;
1213 if (flist_empty(&filename_list))
1216 fio_file_hash_lock();
1217 flist_for_each_safe(entry, tmp, &filename_list) {
1218 fn = flist_entry(entry, struct file_name, list);
1220 flist_del(&fn->list);
1224 fio_file_hash_unlock();
1227 static struct fio_file *alloc_new_file(struct thread_data *td)
1231 f = smalloc(sizeof(*f));
1233 log_err("fio: smalloc OOM\n");
1240 fio_file_reset(td, f);
1244 int add_file(struct thread_data *td, const char *fname, int numjob, int inc)
1246 int cur_files = td->files_index;
1247 char file_name[PATH_MAX];
1251 dprint(FD_FILE, "add file %s\n", fname);
1253 if (td->o.directory)
1254 len = set_name_idx(file_name, td->o.directory, numjob);
1256 sprintf(file_name + len, "%s", fname);
1258 /* clean cloned siblings using existing files */
1259 if (numjob && is_already_allocated(file_name))
1262 f = alloc_new_file(td);
1264 if (td->files_size <= td->files_index) {
1265 unsigned int new_size = td->o.nr_files + 1;
1267 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1269 td->files = realloc(td->files, new_size * sizeof(f));
1270 if (td->files == NULL) {
1271 log_err("fio: realloc OOM\n");
1274 if (td->o.file_lock_mode != FILE_LOCK_NONE) {
1275 td->file_locks = realloc(td->file_locks, new_size);
1276 if (!td->file_locks) {
1277 log_err("fio: realloc OOM\n");
1280 td->file_locks[cur_files] = FILE_LOCK_NONE;
1282 td->files_size = new_size;
1284 td->files[cur_files] = f;
1285 f->fileno = cur_files;
1288 * init function, io engine may not be loaded yet
1290 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1291 f->real_file_size = -1ULL;
1293 f->file_name = smalloc_strdup(file_name);
1294 if (!f->file_name) {
1295 log_err("fio: smalloc OOM\n");
1301 switch (td->o.file_lock_mode) {
1302 case FILE_LOCK_NONE:
1304 case FILE_LOCK_READWRITE:
1305 f->rwlock = fio_rwlock_init();
1307 case FILE_LOCK_EXCLUSIVE:
1308 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
1311 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1316 if (f->filetype == FIO_TYPE_FILE)
1317 td->nr_normal_files++;
1319 set_already_allocated(file_name);
1324 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1330 int add_file_exclusive(struct thread_data *td, const char *fname)
1335 for_each_file(td, f, i) {
1336 if (!strcmp(f->file_name, fname))
1340 return add_file(td, fname, 0, 1);
1343 void get_file(struct fio_file *f)
1345 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
1346 assert(fio_file_open(f));
1350 int put_file(struct thread_data *td, struct fio_file *f)
1352 int f_ret = 0, ret = 0;
1354 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
1356 if (!fio_file_open(f)) {
1357 assert(f->fd == -1);
1361 assert(f->references);
1362 if (--f->references)
1365 if (should_fsync(td) && td->o.fsync_on_close) {
1366 f_ret = fsync(f->fd);
1371 if (td->io_ops->close_file)
1372 ret = td->io_ops->close_file(td, f);
1377 td->nr_open_files--;
1378 fio_file_clear_open(f);
1379 assert(f->fd == -1);
1383 void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
1385 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1388 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1389 if (ddir == DDIR_READ)
1390 fio_rwlock_read(f->rwlock);
1392 fio_rwlock_write(f->rwlock);
1393 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1394 fio_mutex_down(f->lock);
1396 td->file_locks[f->fileno] = td->o.file_lock_mode;
1399 void unlock_file(struct thread_data *td, struct fio_file *f)
1401 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1404 if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1405 fio_rwlock_unlock(f->rwlock);
1406 else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1407 fio_mutex_up(f->lock);
1409 td->file_locks[f->fileno] = FILE_LOCK_NONE;
1412 void unlock_file_all(struct thread_data *td, struct fio_file *f)
1414 if (td->o.file_lock_mode == FILE_LOCK_NONE || !td->file_locks)
1416 if (td->file_locks[f->fileno] != FILE_LOCK_NONE)
1420 static int recurse_dir(struct thread_data *td, const char *dirname)
1426 D = opendir(dirname);
1428 char buf[FIO_VERROR_SIZE];
1430 snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
1431 td_verror(td, errno, buf);
1435 while ((dir = readdir(D)) != NULL) {
1436 char full_path[PATH_MAX];
1439 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1442 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
1444 if (lstat(full_path, &sb) == -1) {
1445 if (errno != ENOENT) {
1446 td_verror(td, errno, "stat");
1452 if (S_ISREG(sb.st_mode)) {
1453 add_file(td, full_path, 0, 1);
1456 if (!S_ISDIR(sb.st_mode))
1459 ret = recurse_dir(td, full_path);
1468 int add_dir_files(struct thread_data *td, const char *path)
1470 int ret = recurse_dir(td, path);
1473 log_info("fio: opendir added %d files\n", td->o.nr_files);
1478 void dup_files(struct thread_data *td, struct thread_data *org)
1483 dprint(FD_FILE, "dup files: %d\n", org->files_index);
1488 td->files = malloc(org->files_index * sizeof(f));
1490 if (td->o.file_lock_mode != FILE_LOCK_NONE)
1491 td->file_locks = malloc(org->files_index);
1493 for_each_file(org, f, i) {
1494 struct fio_file *__f;
1496 __f = alloc_new_file(td);
1499 __f->file_name = smalloc_strdup(f->file_name);
1500 if (!__f->file_name) {
1501 log_err("fio: smalloc OOM\n");
1505 __f->filetype = f->filetype;
1508 if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1509 __f->lock = f->lock;
1510 else if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1511 __f->rwlock = f->rwlock;
1518 * Returns the index that matches the filename, or -1 if not there
1520 int get_fileno(struct thread_data *td, const char *fname)
1525 for_each_file(td, f, i)
1526 if (!strcmp(f->file_name, fname))
1533 * For log usage, where we add/open/close files automatically
1535 void free_release_files(struct thread_data *td)
1539 td->o.open_files = 0;
1540 td->files_index = 0;
1541 td->nr_normal_files = 0;
1544 void fio_file_reset(struct thread_data *td, struct fio_file *f)
1546 f->last_pos = f->file_offset;
1547 f->last_start = -1ULL;
1549 axmap_reset(f->io_axmap);
1550 if (td->o.random_generator == FIO_RAND_GEN_LFSR)
1551 lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
1554 int fio_files_done(struct thread_data *td)
1559 for_each_file(td, f, i)
1560 if (!fio_file_done(f))
1566 /* free memory used in initialization phase only */
1567 void filesetup_mem_free(void)
1569 free_already_allocated();