16 #include "lib/axmap.h"
18 #ifdef FIO_HAVE_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 FIO_HAVE_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",
82 f->file_name, f->real_file_size);
84 r = posix_fallocate(f->fd, 0, f->real_file_size);
86 log_err("fio: posix_fallocate fails: %s\n",
90 #ifdef FIO_HAVE_LINUX_FALLOCATE
91 case FIO_FALLOCATE_KEEP_SIZE:
93 "fallocate(FALLOC_FL_KEEP_SIZE) "
94 "file %s size %llu\n",
95 f->file_name, f->real_file_size);
97 r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0,
100 td_verror(td, errno, "fallocate");
103 #endif /* FIO_HAVE_LINUX_FALLOCATE */
105 log_err("fio: unknown fallocate mode: %d\n",
106 td->o.fallocate_mode);
110 #endif /* FIO_HAVE_FALLOCATE */
116 * The size will be -1ULL when fill_device is used, so don't truncate
117 * or fallocate this file, just write it
119 if (!td->o.fill_device) {
120 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
122 if (ftruncate(f->fd, f->real_file_size) == -1) {
123 td_verror(td, errno, "ftruncate");
128 b = malloc(td->o.max_bs[DDIR_WRITE]);
129 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
131 left = f->real_file_size;
132 while (left && !td->terminate) {
133 bs = td->o.max_bs[DDIR_WRITE];
137 r = write(f->fd, b, bs);
147 if (td->o.fill_device)
149 log_info("fio: ENOSPC on laying out "
153 td_verror(td, errno, "write");
155 td_verror(td, EIO, "write");
162 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
163 unlink(f->file_name);
164 } else if (td->o.create_fsync) {
165 if (fsync(f->fd) < 0) {
166 td_verror(td, errno, "fsync");
170 if (td->o.fill_device && !td_write(td)) {
171 fio_file_clear_size_known(f);
172 if (td_io_get_file_size(td, f))
174 if (f->io_size > f->real_file_size)
175 f->io_size = f->real_file_size;
187 static int pre_read_file(struct thread_data *td, struct fio_file *f)
189 int r, did_open = 0, old_runstate;
190 unsigned long long left;
194 if (td->io_ops->flags & FIO_PIPEIO)
197 if (!fio_file_open(f)) {
198 if (td->io_ops->open_file(td, f)) {
199 log_err("fio: cannot pre-read, failed to open file\n");
205 old_runstate = td->runstate;
206 td_set_runstate(td, TD_PRE_READING);
208 bs = td->o.max_bs[DDIR_READ];
212 lseek(f->fd, f->file_offset, SEEK_SET);
215 while (left && !td->terminate) {
219 r = read(f->fd, b, bs);
225 td_verror(td, EIO, "pre_read");
230 td_set_runstate(td, old_runstate);
233 td->io_ops->close_file(td, f);
238 static unsigned long long get_rand_file_size(struct thread_data *td)
240 unsigned long long ret, sized;
243 if (td->o.use_os_rand) {
244 r = os_random_long(&td->file_size_state);
245 sized = td->o.file_size_high - td->o.file_size_low;
246 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
248 r = __rand(&td->__file_size_state);
249 sized = td->o.file_size_high - td->o.file_size_low;
250 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0)));
253 ret += td->o.file_size_low;
254 ret -= (ret % td->o.rw_min_bs);
258 static int file_size(struct thread_data *td, struct fio_file *f)
262 if (stat(f->file_name, &st) == -1) {
263 td_verror(td, errno, "fstat");
267 f->real_file_size = st.st_size;
271 static int bdev_size(struct thread_data *td, struct fio_file *f)
273 unsigned long long bytes = 0;
276 if (td->io_ops->open_file(td, f)) {
277 log_err("fio: failed opening blockdev %s for size check\n",
282 r = blockdev_size(f, &bytes);
284 td_verror(td, r, "blockdev_size");
289 log_err("%s: zero sized block device?\n", f->file_name);
293 f->real_file_size = bytes;
294 td->io_ops->close_file(td, f);
297 td->io_ops->close_file(td, f);
301 static int char_size(struct thread_data *td, struct fio_file *f)
303 #ifdef FIO_HAVE_CHARDEV_SIZE
304 unsigned long long bytes = 0;
307 if (td->io_ops->open_file(td, f)) {
308 log_err("fio: failed opening blockdev %s for size check\n",
313 r = chardev_size(f, &bytes);
315 td_verror(td, r, "chardev_size");
320 log_err("%s: zero sized char device?\n", f->file_name);
324 f->real_file_size = bytes;
325 td->io_ops->close_file(td, f);
328 td->io_ops->close_file(td, f);
331 f->real_file_size = -1ULL;
336 static int get_file_size(struct thread_data *td, struct fio_file *f)
340 if (fio_file_size_known(f))
343 if (f->filetype == FIO_TYPE_FILE)
344 ret = file_size(td, f);
345 else if (f->filetype == FIO_TYPE_BD)
346 ret = bdev_size(td, f);
347 else if (f->filetype == FIO_TYPE_CHAR)
348 ret = char_size(td, f);
350 f->real_file_size = -1;
355 if (f->file_offset > f->real_file_size) {
356 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
357 f->file_offset, f->real_file_size);
361 fio_file_set_size_known(f);
365 static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
366 unsigned long long off,
367 unsigned long long len)
374 off = f->file_offset;
376 if (len == -1ULL || off == -1ULL)
379 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
383 * FIXME: add blockdev flushing too
386 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
388 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
390 } else if (f->filetype == FIO_TYPE_FILE) {
391 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
392 } else if (f->filetype == FIO_TYPE_BD) {
393 ret = blockdev_invalidate_cache(f);
394 if (ret < 0 && errno == EACCES && geteuid()) {
396 log_err("fio: only root may flush block "
397 "devices. Cache flush bypassed!\n");
402 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
406 td_verror(td, errno, "invalidate_cache");
408 } else if (ret > 0) {
409 td_verror(td, ret, "invalidate_cache");
417 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
419 if (!fio_file_open(f))
422 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
425 int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
429 dprint(FD_FILE, "fd close %s\n", f->file_name);
433 if (close(f->fd) < 0)
438 if (f->shadow_fd != -1) {
446 int file_lookup_open(struct fio_file *f, int flags)
448 struct fio_file *__f;
451 __f = lookup_file_hash(f->file_name);
453 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
455 * racy, need the __f->lock locked
458 f->lock_owner = __f->lock_owner;
459 f->lock_batch = __f->lock_batch;
460 f->lock_ddir = __f->lock_ddir;
463 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
467 f->fd = open(f->file_name, flags, 0600);
471 static int file_close_shadow_fds(struct thread_data *td)
477 for_each_file(td, f, i) {
478 if (f->shadow_fd == -1)
489 int generic_open_file(struct thread_data *td, struct fio_file *f)
495 dprint(FD_FILE, "fd open %s\n", f->file_name);
497 if (td_trim(td) && f->filetype != FIO_TYPE_BD) {
498 log_err("fio: trim only applies to block device\n");
502 if (!strcmp(f->file_name, "-")) {
504 log_err("fio: can't read/write to stdin/out\n");
510 * move output logging to stderr, if we are writing to stdout
519 flags |= OS_O_DIRECT;
522 if (td->o.create_on_open)
525 if (f->filetype != FIO_TYPE_FILE)
526 flags |= FIO_O_NOATIME;
533 if (f->filetype == FIO_TYPE_FILE)
537 f->fd = dup(STDOUT_FILENO);
539 from_hash = file_lookup_open(f, flags);
540 } else if (td_read(td)) {
541 if (f->filetype == FIO_TYPE_CHAR && !read_only)
547 f->fd = dup(STDIN_FILENO);
549 from_hash = file_lookup_open(f, flags);
552 from_hash = file_lookup_open(f, flags);
556 char buf[FIO_VERROR_SIZE];
559 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
560 flags &= ~FIO_O_NOATIME;
563 if (__e == EMFILE && file_close_shadow_fds(td))
566 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
568 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
569 log_err("fio: looks like your file system does not " \
570 "support direct=1/buffered=0\n");
573 td_verror(td, __e, buf);
576 if (!from_hash && f->fd != -1) {
577 if (add_file_hash(f)) {
581 * Stash away descriptor for later close. This is to
582 * work-around a "feature" on Linux, where a close of
583 * an fd that has been opened for write will trigger
584 * udev to call blkid to check partitions, fs id, etc.
585 * That polutes the device cache, which can slow down
586 * unbuffered accesses.
588 if (f->shadow_fd == -1)
589 f->shadow_fd = f->fd;
592 * OK to ignore, we haven't done anything
595 ret = generic_close_file(td, f);
604 int generic_get_file_size(struct thread_data *td, struct fio_file *f)
606 return get_file_size(td, f);
610 * open/close all files, so that ->real_file_size gets set
612 static int get_file_sizes(struct thread_data *td)
618 for_each_file(td, f, i) {
619 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
622 if (td_io_get_file_size(td, f)) {
623 if (td->error != ENOENT) {
624 log_err("%s\n", td->verror);
630 if (f->real_file_size == -1ULL && td->o.size)
631 f->real_file_size = td->o.size / td->o.nr_files;
638 struct flist_head list;
645 * Get free number of bytes for each file on each unique mount.
647 static unsigned long long get_fs_free_counts(struct thread_data *td)
649 struct flist_head *n, *tmp;
650 unsigned long long ret = 0;
651 struct fio_mount *fm;
656 for_each_file(td, f, i) {
660 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
661 if (f->real_file_size != -1ULL)
662 ret += f->real_file_size;
664 } else if (f->filetype != FIO_TYPE_FILE)
667 strcpy(buf, f->file_name);
669 if (stat(buf, &sb) < 0) {
673 if (stat(buf, &sb) < 0)
678 flist_for_each(n, &list) {
679 fm = flist_entry(n, struct fio_mount, list);
680 if (fm->key == sb.st_dev)
689 fm = malloc(sizeof(*fm));
690 strcpy(fm->__base, buf);
691 fm->base = basename(fm->__base);
693 flist_add(&fm->list, &list);
696 flist_for_each_safe(n, tmp, &list) {
697 unsigned long long sz;
699 fm = flist_entry(n, struct fio_mount, list);
700 flist_del(&fm->list);
702 sz = get_fs_size(fm->base);
703 if (sz && sz != -1ULL)
712 uint64_t get_start_offset(struct thread_data *td)
714 return td->o.start_offset +
715 (td->thread_number - 1) * td->o.offset_increment;
719 * Open the files and setup files sizes, creating files if necessary.
721 int setup_files(struct thread_data *td)
723 unsigned long long total_size, extend_size;
726 int err = 0, need_extend;
728 dprint(FD_FILE, "setup files\n");
730 if (td->o.read_iolog_file)
734 * if ioengine defines a setup() method, it's responsible for
735 * opening the files and setting f->real_file_size to indicate
736 * the valid range for that file.
738 if (td->io_ops->setup)
739 err = td->io_ops->setup(td);
741 err = get_file_sizes(td);
747 * check sizes. if the files/devices do not exist and the size
748 * isn't passed to fio, abort.
751 for_each_file(td, f, i) {
752 if (f->real_file_size == -1ULL)
755 total_size += f->real_file_size;
758 if (td->o.fill_device)
759 td->fill_device_size = get_fs_free_counts(td);
762 * device/file sizes are zero and no size given, punt
764 if ((!total_size || total_size == -1ULL) && !td->o.size &&
765 !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
766 log_err("%s: you need to specify size=\n", td->o.name);
767 td_verror(td, EINVAL, "total_file_size");
772 * now file sizes are known, so we can set ->io_size. if size= is
773 * not given, ->io_size is just equal to ->real_file_size. if size
774 * is given, ->io_size is size / nr_files.
776 extend_size = total_size = 0;
778 for_each_file(td, f, i) {
779 f->file_offset = get_start_offset(td);
781 if (!td->o.file_size_low) {
783 * no file size range given, file size is equal to
784 * total size divided by number of files. if that is
785 * zero, set it to the real file size.
787 f->io_size = td->o.size / td->o.nr_files;
789 f->io_size = f->real_file_size - f->file_offset;
790 } else if (f->real_file_size < td->o.file_size_low ||
791 f->real_file_size > td->o.file_size_high) {
792 if (f->file_offset > td->o.file_size_low)
795 * file size given. if it's fixed, use that. if it's a
796 * range, generate a random size in-between.
798 if (td->o.file_size_low == td->o.file_size_high) {
799 f->io_size = td->o.file_size_low
802 f->io_size = get_rand_file_size(td)
806 f->io_size = f->real_file_size - f->file_offset;
808 if (f->io_size == -1ULL)
811 if (td->o.size_percent)
812 f->io_size = (f->io_size * td->o.size_percent) / 100;
813 total_size += f->io_size;
816 if (f->filetype == FIO_TYPE_FILE &&
817 (f->io_size + f->file_offset) > f->real_file_size &&
818 !(td->io_ops->flags & FIO_DISKLESSIO)) {
819 if (!td->o.create_on_open) {
821 extend_size += (f->io_size + f->file_offset);
823 f->real_file_size = f->io_size + f->file_offset;
824 fio_file_set_extend(f);
828 if (!td->o.size || td->o.size > total_size)
829 td->o.size = total_size;
832 * See if we need to extend some files
836 if (output_format == FIO_OUTPUT_NORMAL)
837 log_info("%s: Laying out IO file(s) (%u file(s) /"
838 " %lluMB)\n", td->o.name, need_extend,
841 for_each_file(td, f, i) {
842 unsigned long long old_len = -1ULL, extend_len = -1ULL;
844 if (!fio_file_extend(f))
847 assert(f->filetype == FIO_TYPE_FILE);
848 fio_file_clear_extend(f);
849 if (!td->o.fill_device) {
850 old_len = f->real_file_size;
851 extend_len = f->io_size + f->file_offset -
854 f->real_file_size = (f->io_size + f->file_offset);
855 err = extend_file(td, f);
859 err = __file_invalidate_cache(td, f, old_len,
872 if (!td->o.zone_size)
873 td->o.zone_size = td->o.size;
876 * iolog already set the total io size, if we read back
879 if (!td->o.read_iolog_file)
880 td->total_io_size = td->o.size * td->o.loops;
883 if (td->o.create_only)
888 log_err("%s: you need to specify valid offset=\n", td->o.name);
892 int pre_read_files(struct thread_data *td)
897 dprint(FD_FILE, "pre_read files\n");
899 for_each_file(td, f, i) {
900 pre_read_file(td, f);
906 static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
908 unsigned int range_size, seed;
909 unsigned long nranges;
912 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
913 file_size = min(f->real_file_size, f->io_size);
915 nranges = (file_size + range_size - 1) / range_size;
917 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
918 if (!td->o.rand_repeatable)
919 seed = td->rand_seeds[4];
921 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
922 zipf_init(&f->zipf, nranges, td->o.zipf_theta, seed);
924 pareto_init(&f->zipf, nranges, td->o.pareto_h, seed);
929 static int init_rand_distribution(struct thread_data *td)
935 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
938 state = td->runstate;
939 td_set_runstate(td, TD_SETTING_UP);
940 for_each_file(td, f, i)
941 __init_rand_distribution(td, f);
942 td_set_runstate(td, state);
947 int init_random_map(struct thread_data *td)
949 unsigned long long blocks;
953 if (init_rand_distribution(td))
958 for_each_file(td, f, i) {
959 uint64_t file_size = min(f->real_file_size, f->io_size);
961 blocks = (file_size + td->o.rw_min_bs - 1) /
962 (unsigned long long) td->o.rw_min_bs;
963 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
966 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
968 if (!lfsr_init(&f->lfsr, blocks, seed))
970 } else if (!td->o.norandommap) {
971 f->io_axmap = axmap_new(blocks);
974 } else if (td->o.norandommap)
977 if (!td->o.softrandommap) {
978 log_err("fio: failed allocating random map. If running"
979 " a large number of jobs, try the 'norandommap'"
980 " option or set 'softrandommap'. Or give"
981 " a larger --alloc-size to fio.\n");
985 log_info("fio: file %s failed allocating random map. Running "
986 "job without.\n", f->file_name);
992 void close_files(struct thread_data *td)
997 for_each_file(td, f, i) {
998 if (fio_file_open(f))
999 td_io_close_file(td, f);
1003 void close_and_free_files(struct thread_data *td)
1008 dprint(FD_FILE, "close files\n");
1010 for_each_file(td, f, i) {
1011 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1012 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1013 unlink(f->file_name);
1016 if (fio_file_open(f))
1017 td_io_close_file(td, f);
1019 remove_file_hash(f);
1021 sfree(f->file_name);
1022 f->file_name = NULL;
1023 axmap_free(f->io_axmap);
1028 td->o.filename = NULL;
1030 td->files_index = 0;
1035 static void get_file_type(struct fio_file *f)
1039 if (!strcmp(f->file_name, "-"))
1040 f->filetype = FIO_TYPE_PIPE;
1042 f->filetype = FIO_TYPE_FILE;
1044 /* \\.\ is the device namespace in Windows, where every file is
1046 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1047 f->filetype = FIO_TYPE_BD;
1049 if (!stat(f->file_name, &sb)) {
1050 if (S_ISBLK(sb.st_mode))
1051 f->filetype = FIO_TYPE_BD;
1052 else if (S_ISCHR(sb.st_mode))
1053 f->filetype = FIO_TYPE_CHAR;
1054 else if (S_ISFIFO(sb.st_mode))
1055 f->filetype = FIO_TYPE_PIPE;
1059 int add_file(struct thread_data *td, const char *fname)
1061 int cur_files = td->files_index;
1062 char file_name[PATH_MAX];
1066 dprint(FD_FILE, "add file %s\n", fname);
1068 f = smalloc(sizeof(*f));
1070 log_err("fio: smalloc OOM\n");
1076 fio_file_reset(td, f);
1078 if (td->files_size <= td->files_index) {
1079 unsigned int new_size = td->o.nr_files + 1;
1081 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1083 td->files = realloc(td->files, new_size * sizeof(f));
1084 if (td->files == NULL) {
1085 log_err("fio: realloc OOM\n");
1088 td->files_size = new_size;
1090 td->files[cur_files] = f;
1091 f->fileno = cur_files;
1094 * init function, io engine may not be loaded yet
1096 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1097 f->real_file_size = -1ULL;
1099 if (td->o.directory)
1100 len = sprintf(file_name, "%s/", td->o.directory);
1102 sprintf(file_name + len, "%s", fname);
1103 f->file_name = smalloc_strdup(file_name);
1104 if (!f->file_name) {
1105 log_err("fio: smalloc OOM\n");
1111 switch (td->o.file_lock_mode) {
1112 case FILE_LOCK_NONE:
1114 case FILE_LOCK_READWRITE:
1115 f->lock = fio_mutex_rw_init();
1117 case FILE_LOCK_EXCLUSIVE:
1118 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
1121 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1126 if (f->filetype == FIO_TYPE_FILE)
1127 td->nr_normal_files++;
1129 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1135 int add_file_exclusive(struct thread_data *td, const char *fname)
1140 for_each_file(td, f, i) {
1141 if (!strcmp(f->file_name, fname))
1145 return add_file(td, fname);
1148 void get_file(struct fio_file *f)
1150 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
1151 assert(fio_file_open(f));
1155 int put_file(struct thread_data *td, struct fio_file *f)
1157 int f_ret = 0, ret = 0;
1159 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
1161 if (!fio_file_open(f)) {
1162 assert(f->fd == -1);
1166 assert(f->references);
1167 if (--f->references)
1170 if (should_fsync(td) && td->o.fsync_on_close)
1171 f_ret = fsync(f->fd);
1173 if (td->io_ops->close_file)
1174 ret = td->io_ops->close_file(td, f);
1179 td->nr_open_files--;
1180 fio_file_clear_open(f);
1181 assert(f->fd == -1);
1185 void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
1187 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1190 if (f->lock_owner == td && f->lock_batch--)
1193 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1194 if (ddir == DDIR_READ)
1195 fio_mutex_down_read(f->lock);
1197 fio_mutex_down_write(f->lock);
1198 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1199 fio_mutex_down(f->lock);
1202 f->lock_batch = td->o.lockfile_batch;
1203 f->lock_ddir = ddir;
1206 void unlock_file(struct thread_data *td, struct fio_file *f)
1208 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1213 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1214 const int is_read = f->lock_ddir == DDIR_READ;
1215 int val = fio_mutex_getval(f->lock);
1217 if ((is_read && val == 1) || (!is_read && val == -1))
1218 f->lock_owner = NULL;
1221 fio_mutex_up_read(f->lock);
1223 fio_mutex_up_write(f->lock);
1224 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
1225 int val = fio_mutex_getval(f->lock);
1228 f->lock_owner = NULL;
1230 fio_mutex_up(f->lock);
1234 void unlock_file_all(struct thread_data *td, struct fio_file *f)
1236 if (f->lock_owner != td)
1243 static int recurse_dir(struct thread_data *td, const char *dirname)
1249 D = opendir(dirname);
1251 char buf[FIO_VERROR_SIZE];
1253 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
1254 td_verror(td, errno, buf);
1258 while ((dir = readdir(D)) != NULL) {
1259 char full_path[PATH_MAX];
1262 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1265 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
1267 if (lstat(full_path, &sb) == -1) {
1268 if (errno != ENOENT) {
1269 td_verror(td, errno, "stat");
1274 if (S_ISREG(sb.st_mode)) {
1275 add_file(td, full_path);
1279 if (!S_ISDIR(sb.st_mode))
1282 ret = recurse_dir(td, full_path);
1291 int add_dir_files(struct thread_data *td, const char *path)
1293 int ret = recurse_dir(td, path);
1296 log_info("fio: opendir added %d files\n", td->o.nr_files);
1301 void dup_files(struct thread_data *td, struct thread_data *org)
1306 dprint(FD_FILE, "dup files: %d\n", org->files_index);
1311 td->files = malloc(org->files_index * sizeof(f));
1313 for_each_file(org, f, i) {
1314 struct fio_file *__f;
1316 __f = smalloc(sizeof(*__f));
1318 log_err("fio: smalloc OOM\n");
1322 fio_file_reset(td, __f);
1325 __f->file_name = smalloc_strdup(f->file_name);
1326 if (!__f->file_name) {
1327 log_err("fio: smalloc OOM\n");
1331 __f->filetype = f->filetype;
1339 * Returns the index that matches the filename, or -1 if not there
1341 int get_fileno(struct thread_data *td, const char *fname)
1346 for_each_file(td, f, i)
1347 if (!strcmp(f->file_name, fname))
1354 * For log usage, where we add/open/close files automatically
1356 void free_release_files(struct thread_data *td)
1359 td->files_index = 0;
1360 td->nr_normal_files = 0;
1363 void fio_file_reset(struct thread_data *td, struct fio_file *f)
1365 f->last_pos = f->file_offset;
1366 f->last_start = -1ULL;
1368 axmap_reset(f->io_axmap);
1369 if (td->o.random_generator == FIO_RAND_GEN_LFSR)
1370 lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);