16 #include "lib/axmap.h"
18 #ifdef CONFIG_LINUX_FALLOCATE
19 #include <linux/falloc.h>
24 static inline void clear_error(struct thread_data *td)
31 * Leaves f->fd open on success, caller must close
33 static int extend_file(struct thread_data *td, struct fio_file *f)
35 int r, new_layout = 0, unlink_file = 0, flags;
36 unsigned long long left;
41 log_err("fio: refusing extend of file due to read-only\n");
46 * check if we need to lay the file out complete again. fio
47 * does that for operations involving reads, or for writes
48 * where overwrite is set
50 if (td_read(td) || (td_write(td) && td->o.overwrite) ||
51 (td_write(td) && td->io_ops->flags & FIO_NOEXTEND))
53 if (td_write(td) && !td->o.overwrite)
56 if (unlink_file || new_layout) {
57 dprint(FD_FILE, "layout unlink %s\n", f->file_name);
58 if ((unlink(f->file_name) < 0) && (errno != ENOENT)) {
59 td_verror(td, errno, "unlink");
64 flags = O_WRONLY | O_CREAT;
68 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
69 f->fd = open(f->file_name, flags, 0644);
71 td_verror(td, errno, "open");
75 #ifdef CONFIG_POSIX_FALLOCATE
76 if (!td->o.fill_device) {
77 switch (td->o.fallocate_mode) {
78 case FIO_FALLOCATE_NONE:
80 case FIO_FALLOCATE_POSIX:
81 dprint(FD_FILE, "posix_fallocate file %s size %llu\n",
83 (unsigned long long) f->real_file_size);
85 r = posix_fallocate(f->fd, 0, f->real_file_size);
87 log_err("fio: posix_fallocate fails: %s\n",
91 #ifdef CONFIG_LINUX_FALLOCATE
92 case FIO_FALLOCATE_KEEP_SIZE:
94 "fallocate(FALLOC_FL_KEEP_SIZE) "
95 "file %s size %llu\n", f->file_name,
96 (unsigned long long) f->real_file_size);
98 r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0,
101 td_verror(td, errno, "fallocate");
104 #endif /* CONFIG_LINUX_FALLOCATE */
106 log_err("fio: unknown fallocate mode: %d\n",
107 td->o.fallocate_mode);
111 #endif /* CONFIG_POSIX_FALLOCATE */
117 * The size will be -1ULL when fill_device is used, so don't truncate
118 * or fallocate this file, just write it
120 if (!td->o.fill_device) {
121 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
122 (unsigned long long) f->real_file_size);
123 if (ftruncate(f->fd, f->real_file_size) == -1) {
124 if (errno != EFBIG) {
125 td_verror(td, errno, "ftruncate");
131 b = malloc(td->o.max_bs[DDIR_WRITE]);
133 left = f->real_file_size;
134 while (left && !td->terminate) {
135 bs = td->o.max_bs[DDIR_WRITE];
139 fill_io_buffer(td, b, bs, bs);
141 r = write(f->fd, b, bs);
151 if (td->o.fill_device)
153 log_info("fio: ENOSPC on laying out "
157 td_verror(td, errno, "write");
159 td_verror(td, EIO, "write");
166 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
167 unlink(f->file_name);
168 } else if (td->o.create_fsync) {
169 if (fsync(f->fd) < 0) {
170 td_verror(td, errno, "fsync");
174 if (td->o.fill_device && !td_write(td)) {
175 fio_file_clear_size_known(f);
176 if (td_io_get_file_size(td, f))
178 if (f->io_size > f->real_file_size)
179 f->io_size = f->real_file_size;
191 static int pre_read_file(struct thread_data *td, struct fio_file *f)
193 int r, did_open = 0, old_runstate;
194 unsigned long long left;
198 if (td->io_ops->flags & FIO_PIPEIO)
201 if (!fio_file_open(f)) {
202 if (td->io_ops->open_file(td, f)) {
203 log_err("fio: cannot pre-read, failed to open file\n");
209 old_runstate = td->runstate;
210 td_set_runstate(td, TD_PRE_READING);
212 bs = td->o.max_bs[DDIR_READ];
216 lseek(f->fd, f->file_offset, SEEK_SET);
219 while (left && !td->terminate) {
223 r = read(f->fd, b, bs);
229 td_verror(td, EIO, "pre_read");
234 td_set_runstate(td, old_runstate);
237 td->io_ops->close_file(td, f);
242 static unsigned long long get_rand_file_size(struct thread_data *td)
244 unsigned long long ret, sized;
247 if (td->o.use_os_rand) {
248 r = os_random_long(&td->file_size_state);
249 sized = td->o.file_size_high - td->o.file_size_low;
250 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
252 r = __rand(&td->__file_size_state);
253 sized = td->o.file_size_high - td->o.file_size_low;
254 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0)));
257 ret += td->o.file_size_low;
258 ret -= (ret % td->o.rw_min_bs);
262 static int file_size(struct thread_data *td, struct fio_file *f)
266 if (stat(f->file_name, &st) == -1) {
267 td_verror(td, errno, "fstat");
271 f->real_file_size = st.st_size;
275 static int bdev_size(struct thread_data *td, struct fio_file *f)
277 unsigned long long bytes = 0;
280 if (td->io_ops->open_file(td, f)) {
281 log_err("fio: failed opening blockdev %s for size check\n",
286 r = blockdev_size(f, &bytes);
288 td_verror(td, r, "blockdev_size");
293 log_err("%s: zero sized block device?\n", f->file_name);
297 f->real_file_size = bytes;
298 td->io_ops->close_file(td, f);
301 td->io_ops->close_file(td, f);
305 static int char_size(struct thread_data *td, struct fio_file *f)
307 #ifdef FIO_HAVE_CHARDEV_SIZE
308 unsigned long long bytes = 0;
311 if (td->io_ops->open_file(td, f)) {
312 log_err("fio: failed opening blockdev %s for size check\n",
317 r = chardev_size(f, &bytes);
319 td_verror(td, r, "chardev_size");
324 log_err("%s: zero sized char device?\n", f->file_name);
328 f->real_file_size = bytes;
329 td->io_ops->close_file(td, f);
332 td->io_ops->close_file(td, f);
335 f->real_file_size = -1ULL;
340 static int get_file_size(struct thread_data *td, struct fio_file *f)
344 if (fio_file_size_known(f))
347 if (f->filetype == FIO_TYPE_FILE)
348 ret = file_size(td, f);
349 else if (f->filetype == FIO_TYPE_BD)
350 ret = bdev_size(td, f);
351 else if (f->filetype == FIO_TYPE_CHAR)
352 ret = char_size(td, f);
354 f->real_file_size = -1;
359 if (f->file_offset > f->real_file_size) {
360 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
361 (unsigned long long) f->file_offset,
362 (unsigned long long) f->real_file_size);
366 fio_file_set_size_known(f);
370 static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
371 unsigned long long off,
372 unsigned long long len)
379 off = f->file_offset;
381 if (len == -1ULL || off == -1ULL)
384 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
388 * FIXME: add blockdev flushing too
391 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
393 if (f->filetype == FIO_TYPE_BD)
394 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
396 } else if (f->filetype == FIO_TYPE_FILE) {
397 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
398 } else if (f->filetype == FIO_TYPE_BD) {
399 ret = blockdev_invalidate_cache(f);
400 if (ret < 0 && errno == EACCES && geteuid()) {
402 log_err("fio: only root may flush block "
403 "devices. Cache flush bypassed!\n");
408 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
412 td_verror(td, errno, "invalidate_cache");
414 } else if (ret > 0) {
415 td_verror(td, ret, "invalidate_cache");
423 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
425 if (!fio_file_open(f))
428 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
431 int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
435 dprint(FD_FILE, "fd close %s\n", f->file_name);
439 if (close(f->fd) < 0)
444 if (f->shadow_fd != -1) {
453 int file_lookup_open(struct fio_file *f, int flags)
455 struct fio_file *__f;
458 __f = lookup_file_hash(f->file_name);
460 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
462 * racy, need the __f->lock locked
467 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
471 f->fd = open(f->file_name, flags, 0600);
475 static int file_close_shadow_fds(struct thread_data *td)
481 for_each_file(td, f, i) {
482 if (f->shadow_fd == -1)
493 int generic_open_file(struct thread_data *td, struct fio_file *f)
499 dprint(FD_FILE, "fd open %s\n", f->file_name);
501 if (td_trim(td) && f->filetype != FIO_TYPE_BD) {
502 log_err("fio: trim only applies to block device\n");
506 if (!strcmp(f->file_name, "-")) {
508 log_err("fio: can't read/write to stdin/out\n");
514 * move output logging to stderr, if we are writing to stdout
523 flags |= OS_O_DIRECT;
526 td_verror(td, EINVAL, "OS does not support atomic IO");
529 flags |= OS_O_DIRECT | FIO_O_ATOMIC;
533 if (td->o.create_on_open)
536 if (f->filetype != FIO_TYPE_FILE)
537 flags |= FIO_O_NOATIME;
544 if (f->filetype == FIO_TYPE_FILE)
548 f->fd = dup(STDOUT_FILENO);
550 from_hash = file_lookup_open(f, flags);
551 } else if (td_read(td)) {
552 if (f->filetype == FIO_TYPE_CHAR && !read_only)
558 f->fd = dup(STDIN_FILENO);
560 from_hash = file_lookup_open(f, flags);
563 from_hash = file_lookup_open(f, flags);
567 char buf[FIO_VERROR_SIZE];
570 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
571 flags &= ~FIO_O_NOATIME;
574 if (__e == EMFILE && file_close_shadow_fds(td))
577 snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
579 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
580 log_err("fio: looks like your file system does not " \
581 "support direct=1/buffered=0\n");
584 td_verror(td, __e, buf);
587 if (!from_hash && f->fd != -1) {
588 if (add_file_hash(f)) {
592 * Stash away descriptor for later close. This is to
593 * work-around a "feature" on Linux, where a close of
594 * an fd that has been opened for write will trigger
595 * udev to call blkid to check partitions, fs id, etc.
596 * That pollutes the device cache, which can slow down
597 * unbuffered accesses.
599 if (f->shadow_fd == -1)
600 f->shadow_fd = f->fd;
603 * OK to ignore, we haven't done anything
606 ret = generic_close_file(td, f);
615 int generic_get_file_size(struct thread_data *td, struct fio_file *f)
617 return get_file_size(td, f);
621 * open/close all files, so that ->real_file_size gets set
623 static int get_file_sizes(struct thread_data *td)
629 for_each_file(td, f, i) {
630 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
633 if (td_io_get_file_size(td, f)) {
634 if (td->error != ENOENT) {
635 log_err("%s\n", td->verror);
641 if (f->real_file_size == -1ULL && td->o.size)
642 f->real_file_size = td->o.size / td->o.nr_files;
649 struct flist_head list;
656 * Get free number of bytes for each file on each unique mount.
658 static unsigned long long get_fs_free_counts(struct thread_data *td)
660 struct flist_head *n, *tmp;
661 unsigned long long ret = 0;
662 struct fio_mount *fm;
667 for_each_file(td, f, i) {
671 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
672 if (f->real_file_size != -1ULL)
673 ret += f->real_file_size;
675 } else if (f->filetype != FIO_TYPE_FILE)
678 strcpy(buf, f->file_name);
680 if (stat(buf, &sb) < 0) {
684 if (stat(buf, &sb) < 0)
689 flist_for_each(n, &list) {
690 fm = flist_entry(n, struct fio_mount, list);
691 if (fm->key == sb.st_dev)
700 fm = malloc(sizeof(*fm));
701 strcpy(fm->__base, buf);
702 fm->base = basename(fm->__base);
704 flist_add(&fm->list, &list);
707 flist_for_each_safe(n, tmp, &list) {
708 unsigned long long sz;
710 fm = flist_entry(n, struct fio_mount, list);
711 flist_del(&fm->list);
713 sz = get_fs_size(fm->base);
714 if (sz && sz != -1ULL)
723 uint64_t get_start_offset(struct thread_data *td)
725 return td->o.start_offset +
726 (td->thread_number - 1) * td->o.offset_increment;
730 * Open the files and setup files sizes, creating files if necessary.
732 int setup_files(struct thread_data *td)
734 unsigned long long total_size, extend_size;
735 struct thread_options *o = &td->o;
738 int err = 0, need_extend;
741 dprint(FD_FILE, "setup files\n");
743 old_state = td->runstate;
744 td_set_runstate(td, TD_SETTING_UP);
746 if (o->read_iolog_file)
750 * if ioengine defines a setup() method, it's responsible for
751 * opening the files and setting f->real_file_size to indicate
752 * the valid range for that file.
754 if (td->io_ops->setup)
755 err = td->io_ops->setup(td);
757 err = get_file_sizes(td);
763 * check sizes. if the files/devices do not exist and the size
764 * isn't passed to fio, abort.
767 for_each_file(td, f, i) {
768 if (f->real_file_size == -1ULL)
771 total_size += f->real_file_size;
775 td->fill_device_size = get_fs_free_counts(td);
778 * device/file sizes are zero and no size given, punt
780 if ((!total_size || total_size == -1ULL) && !o->size &&
781 !(td->io_ops->flags & FIO_NOIO) && !o->fill_device &&
782 !(o->nr_files && (o->file_size_low || o->file_size_high))) {
783 log_err("%s: you need to specify size=\n", o->name);
784 td_verror(td, EINVAL, "total_file_size");
789 * now file sizes are known, so we can set ->io_size. if size= is
790 * not given, ->io_size is just equal to ->real_file_size. if size
791 * is given, ->io_size is size / nr_files.
793 extend_size = total_size = 0;
795 for_each_file(td, f, i) {
796 f->file_offset = get_start_offset(td);
798 if (!o->file_size_low) {
800 * no file size range given, file size is equal to
801 * total size divided by number of files. if that is
802 * zero, set it to the real file size.
804 f->io_size = o->size / o->nr_files;
806 f->io_size = f->real_file_size - f->file_offset;
807 } else if (f->real_file_size < o->file_size_low ||
808 f->real_file_size > o->file_size_high) {
809 if (f->file_offset > o->file_size_low)
812 * file size given. if it's fixed, use that. if it's a
813 * range, generate a random size in-between.
815 if (o->file_size_low == o->file_size_high)
816 f->io_size = o->file_size_low - f->file_offset;
818 f->io_size = get_rand_file_size(td)
822 f->io_size = f->real_file_size - f->file_offset;
824 if (f->io_size == -1ULL)
828 f->io_size = (f->io_size * o->size_percent) / 100;
829 total_size += f->io_size;
832 if (f->filetype == FIO_TYPE_FILE &&
833 (f->io_size + f->file_offset) > f->real_file_size &&
834 !(td->io_ops->flags & FIO_DISKLESSIO)) {
835 if (!o->create_on_open) {
837 extend_size += (f->io_size + f->file_offset);
839 f->real_file_size = f->io_size + f->file_offset;
840 fio_file_set_extend(f);
844 if (!o->size || o->size > total_size)
845 o->size = total_size;
848 * See if we need to extend some files
852 if (output_format == FIO_OUTPUT_NORMAL)
853 log_info("%s: Laying out IO file(s) (%u file(s) /"
854 " %lluMB)\n", o->name, need_extend,
857 for_each_file(td, f, i) {
858 unsigned long long old_len = -1ULL, extend_len = -1ULL;
860 if (!fio_file_extend(f))
863 assert(f->filetype == FIO_TYPE_FILE);
864 fio_file_clear_extend(f);
865 if (!o->fill_device) {
866 old_len = f->real_file_size;
867 extend_len = f->io_size + f->file_offset -
870 f->real_file_size = (f->io_size + f->file_offset);
871 err = extend_file(td, f);
875 err = __file_invalidate_cache(td, f, old_len,
889 o->zone_size = o->size;
892 * iolog already set the total io size, if we read back
895 if (!o->read_iolog_file)
896 td->total_io_size = o->size * o->loops;
902 td_set_runstate(td, old_state);
905 log_err("%s: you need to specify valid offset=\n", o->name);
907 td_set_runstate(td, old_state);
911 int pre_read_files(struct thread_data *td)
916 dprint(FD_FILE, "pre_read files\n");
918 for_each_file(td, f, i) {
919 pre_read_file(td, f);
925 static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
927 unsigned int range_size, seed;
928 unsigned long nranges;
931 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
932 file_size = min(f->real_file_size, f->io_size);
934 nranges = (file_size + range_size - 1) / range_size;
936 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
937 if (!td->o.rand_repeatable)
938 seed = td->rand_seeds[4];
940 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
941 zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed);
943 pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed);
948 static int init_rand_distribution(struct thread_data *td)
954 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
957 state = td->runstate;
958 td_set_runstate(td, TD_SETTING_UP);
959 for_each_file(td, f, i)
960 __init_rand_distribution(td, f);
961 td_set_runstate(td, state);
966 int init_random_map(struct thread_data *td)
968 unsigned long long blocks;
972 if (init_rand_distribution(td))
977 for_each_file(td, f, i) {
978 uint64_t file_size = min(f->real_file_size, f->io_size);
980 blocks = file_size / (unsigned long long) td->o.rw_min_bs;
982 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
985 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
987 if (!lfsr_init(&f->lfsr, blocks, seed, seed & 0xF))
989 } else if (!td->o.norandommap) {
990 f->io_axmap = axmap_new(blocks);
993 } else if (td->o.norandommap)
996 if (!td->o.softrandommap) {
997 log_err("fio: failed allocating random map. If running"
998 " a large number of jobs, try the 'norandommap'"
999 " option or set 'softrandommap'. Or give"
1000 " a larger --alloc-size to fio.\n");
1004 log_info("fio: file %s failed allocating random map. Running "
1005 "job without.\n", f->file_name);
1011 void close_files(struct thread_data *td)
1016 for_each_file(td, f, i) {
1017 if (fio_file_open(f))
1018 td_io_close_file(td, f);
1022 void close_and_free_files(struct thread_data *td)
1027 dprint(FD_FILE, "close files\n");
1029 for_each_file(td, f, i) {
1030 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1031 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1032 unlink(f->file_name);
1035 if (fio_file_open(f))
1036 td_io_close_file(td, f);
1038 remove_file_hash(f);
1040 sfree(f->file_name);
1041 f->file_name = NULL;
1042 axmap_free(f->io_axmap);
1047 td->o.filename = NULL;
1049 free(td->file_locks);
1050 td->files_index = 0;
1052 td->file_locks = NULL;
1053 td->o.file_lock_mode = FILE_LOCK_NONE;
1057 static void get_file_type(struct fio_file *f)
1061 if (!strcmp(f->file_name, "-"))
1062 f->filetype = FIO_TYPE_PIPE;
1064 f->filetype = FIO_TYPE_FILE;
1066 /* \\.\ is the device namespace in Windows, where every file is
1068 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1069 f->filetype = FIO_TYPE_BD;
1071 if (!stat(f->file_name, &sb)) {
1072 if (S_ISBLK(sb.st_mode))
1073 f->filetype = FIO_TYPE_BD;
1074 else if (S_ISCHR(sb.st_mode))
1075 f->filetype = FIO_TYPE_CHAR;
1076 else if (S_ISFIFO(sb.st_mode))
1077 f->filetype = FIO_TYPE_PIPE;
1081 int add_file(struct thread_data *td, const char *fname)
1083 int cur_files = td->files_index;
1084 char file_name[PATH_MAX];
1088 dprint(FD_FILE, "add file %s\n", fname);
1090 f = smalloc(sizeof(*f));
1092 log_err("fio: smalloc OOM\n");
1098 fio_file_reset(td, f);
1100 if (td->files_size <= td->files_index) {
1101 unsigned int new_size = td->o.nr_files + 1;
1103 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1105 td->files = realloc(td->files, new_size * sizeof(f));
1106 if (td->files == NULL) {
1107 log_err("fio: realloc OOM\n");
1110 if (td->o.file_lock_mode != FILE_LOCK_NONE) {
1111 td->file_locks = realloc(td->file_locks, new_size);
1112 if (!td->file_locks) {
1113 log_err("fio: realloc OOM\n");
1116 td->file_locks[cur_files] = FILE_LOCK_NONE;
1118 td->files_size = new_size;
1120 td->files[cur_files] = f;
1121 f->fileno = cur_files;
1124 * init function, io engine may not be loaded yet
1126 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1127 f->real_file_size = -1ULL;
1129 if (td->o.directory)
1130 len = sprintf(file_name, "%s/", td->o.directory);
1132 sprintf(file_name + len, "%s", fname);
1133 f->file_name = smalloc_strdup(file_name);
1134 if (!f->file_name) {
1135 log_err("fio: smalloc OOM\n");
1141 switch (td->o.file_lock_mode) {
1142 case FILE_LOCK_NONE:
1144 case FILE_LOCK_READWRITE:
1145 f->rwlock = fio_rwlock_init();
1147 case FILE_LOCK_EXCLUSIVE:
1148 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
1151 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1156 if (f->filetype == FIO_TYPE_FILE)
1157 td->nr_normal_files++;
1159 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1165 int add_file_exclusive(struct thread_data *td, const char *fname)
1170 for_each_file(td, f, i) {
1171 if (!strcmp(f->file_name, fname))
1175 return add_file(td, fname);
1178 void get_file(struct fio_file *f)
1180 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
1181 assert(fio_file_open(f));
1185 int put_file(struct thread_data *td, struct fio_file *f)
1187 int f_ret = 0, ret = 0;
1189 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
1191 if (!fio_file_open(f)) {
1192 assert(f->fd == -1);
1196 assert(f->references);
1197 if (--f->references)
1200 if (should_fsync(td) && td->o.fsync_on_close)
1201 f_ret = fsync(f->fd);
1203 if (td->io_ops->close_file)
1204 ret = td->io_ops->close_file(td, f);
1209 td->nr_open_files--;
1210 fio_file_clear_open(f);
1211 assert(f->fd == -1);
1215 void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
1217 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1220 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1221 if (ddir == DDIR_READ)
1222 fio_rwlock_read(f->rwlock);
1224 fio_rwlock_write(f->rwlock);
1225 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1226 fio_mutex_down(f->lock);
1228 td->file_locks[f->fileno] = td->o.file_lock_mode;
1231 void unlock_file(struct thread_data *td, struct fio_file *f)
1233 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1236 if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1237 fio_rwlock_unlock(f->rwlock);
1238 else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1239 fio_mutex_up(f->lock);
1241 td->file_locks[f->fileno] = FILE_LOCK_NONE;
1244 void unlock_file_all(struct thread_data *td, struct fio_file *f)
1246 if (td->o.file_lock_mode == FILE_LOCK_NONE)
1248 if (td->file_locks[f->fileno] != FILE_LOCK_NONE)
1252 static int recurse_dir(struct thread_data *td, const char *dirname)
1258 D = opendir(dirname);
1260 char buf[FIO_VERROR_SIZE];
1262 snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
1263 td_verror(td, errno, buf);
1267 while ((dir = readdir(D)) != NULL) {
1268 char full_path[PATH_MAX];
1271 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1274 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
1276 if (lstat(full_path, &sb) == -1) {
1277 if (errno != ENOENT) {
1278 td_verror(td, errno, "stat");
1283 if (S_ISREG(sb.st_mode)) {
1284 add_file(td, full_path);
1288 if (!S_ISDIR(sb.st_mode))
1291 ret = recurse_dir(td, full_path);
1300 int add_dir_files(struct thread_data *td, const char *path)
1302 int ret = recurse_dir(td, path);
1305 log_info("fio: opendir added %d files\n", td->o.nr_files);
1310 void dup_files(struct thread_data *td, struct thread_data *org)
1315 dprint(FD_FILE, "dup files: %d\n", org->files_index);
1320 td->files = malloc(org->files_index * sizeof(f));
1322 if (td->o.file_lock_mode != FILE_LOCK_NONE)
1323 td->file_locks = malloc(org->files_index);
1325 for_each_file(org, f, i) {
1326 struct fio_file *__f;
1328 __f = smalloc(sizeof(*__f));
1330 log_err("fio: smalloc OOM\n");
1334 fio_file_reset(td, __f);
1337 __f->file_name = smalloc_strdup(f->file_name);
1338 if (!__f->file_name) {
1339 log_err("fio: smalloc OOM\n");
1343 __f->filetype = f->filetype;
1346 if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1347 __f->lock = f->lock;
1348 else if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1349 __f->rwlock = f->rwlock;
1356 * Returns the index that matches the filename, or -1 if not there
1358 int get_fileno(struct thread_data *td, const char *fname)
1363 for_each_file(td, f, i)
1364 if (!strcmp(f->file_name, fname))
1371 * For log usage, where we add/open/close files automatically
1373 void free_release_files(struct thread_data *td)
1376 td->files_index = 0;
1377 td->nr_normal_files = 0;
1380 void fio_file_reset(struct thread_data *td, struct fio_file *f)
1382 f->last_pos = f->file_offset;
1383 f->last_start = -1ULL;
1385 axmap_reset(f->io_axmap);
1386 if (td->o.random_generator == FIO_RAND_GEN_LFSR)
1387 lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);