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 td_verror(td, errno, "ftruncate");
129 b = malloc(td->o.max_bs[DDIR_WRITE]);
130 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
132 left = f->real_file_size;
133 while (left && !td->terminate) {
134 bs = td->o.max_bs[DDIR_WRITE];
138 r = write(f->fd, b, bs);
148 if (td->o.fill_device)
150 log_info("fio: ENOSPC on laying out "
154 td_verror(td, errno, "write");
156 td_verror(td, EIO, "write");
163 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
164 unlink(f->file_name);
165 } else if (td->o.create_fsync) {
166 if (fsync(f->fd) < 0) {
167 td_verror(td, errno, "fsync");
171 if (td->o.fill_device && !td_write(td)) {
172 fio_file_clear_size_known(f);
173 if (td_io_get_file_size(td, f))
175 if (f->io_size > f->real_file_size)
176 f->io_size = f->real_file_size;
188 static int pre_read_file(struct thread_data *td, struct fio_file *f)
190 int r, did_open = 0, old_runstate;
191 unsigned long long left;
195 if (td->io_ops->flags & FIO_PIPEIO)
198 if (!fio_file_open(f)) {
199 if (td->io_ops->open_file(td, f)) {
200 log_err("fio: cannot pre-read, failed to open file\n");
206 old_runstate = td->runstate;
207 td_set_runstate(td, TD_PRE_READING);
209 bs = td->o.max_bs[DDIR_READ];
213 lseek(f->fd, f->file_offset, SEEK_SET);
216 while (left && !td->terminate) {
220 r = read(f->fd, b, bs);
226 td_verror(td, EIO, "pre_read");
231 td_set_runstate(td, old_runstate);
234 td->io_ops->close_file(td, f);
239 static unsigned long long get_rand_file_size(struct thread_data *td)
241 unsigned long long ret, sized;
244 if (td->o.use_os_rand) {
245 r = os_random_long(&td->file_size_state);
246 sized = td->o.file_size_high - td->o.file_size_low;
247 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
249 r = __rand(&td->__file_size_state);
250 sized = td->o.file_size_high - td->o.file_size_low;
251 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0)));
254 ret += td->o.file_size_low;
255 ret -= (ret % td->o.rw_min_bs);
259 static int file_size(struct thread_data *td, struct fio_file *f)
263 if (stat(f->file_name, &st) == -1) {
264 td_verror(td, errno, "fstat");
268 f->real_file_size = st.st_size;
272 static int bdev_size(struct thread_data *td, struct fio_file *f)
274 unsigned long long bytes = 0;
277 if (td->io_ops->open_file(td, f)) {
278 log_err("fio: failed opening blockdev %s for size check\n",
283 r = blockdev_size(f, &bytes);
285 td_verror(td, r, "blockdev_size");
290 log_err("%s: zero sized block device?\n", f->file_name);
294 f->real_file_size = bytes;
295 td->io_ops->close_file(td, f);
298 td->io_ops->close_file(td, f);
302 static int char_size(struct thread_data *td, struct fio_file *f)
304 #ifdef FIO_HAVE_CHARDEV_SIZE
305 unsigned long long bytes = 0;
308 if (td->io_ops->open_file(td, f)) {
309 log_err("fio: failed opening blockdev %s for size check\n",
314 r = chardev_size(f, &bytes);
316 td_verror(td, r, "chardev_size");
321 log_err("%s: zero sized char device?\n", f->file_name);
325 f->real_file_size = bytes;
326 td->io_ops->close_file(td, f);
329 td->io_ops->close_file(td, f);
332 f->real_file_size = -1ULL;
337 static int get_file_size(struct thread_data *td, struct fio_file *f)
341 if (fio_file_size_known(f))
344 if (f->filetype == FIO_TYPE_FILE)
345 ret = file_size(td, f);
346 else if (f->filetype == FIO_TYPE_BD)
347 ret = bdev_size(td, f);
348 else if (f->filetype == FIO_TYPE_CHAR)
349 ret = char_size(td, f);
351 f->real_file_size = -1;
356 if (f->file_offset > f->real_file_size) {
357 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
358 f->file_offset, f->real_file_size);
362 fio_file_set_size_known(f);
366 static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
367 unsigned long long off,
368 unsigned long long len)
375 off = f->file_offset;
377 if (len == -1ULL || off == -1ULL)
380 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
384 * FIXME: add blockdev flushing too
387 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
389 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
391 } else if (f->filetype == FIO_TYPE_FILE) {
392 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
393 } else if (f->filetype == FIO_TYPE_BD) {
394 ret = blockdev_invalidate_cache(f);
395 if (ret < 0 && errno == EACCES && geteuid()) {
397 log_err("fio: only root may flush block "
398 "devices. Cache flush bypassed!\n");
403 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
407 td_verror(td, errno, "invalidate_cache");
409 } else if (ret > 0) {
410 td_verror(td, ret, "invalidate_cache");
418 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
420 if (!fio_file_open(f))
423 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
426 int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
430 dprint(FD_FILE, "fd close %s\n", f->file_name);
434 if (close(f->fd) < 0)
439 if (f->shadow_fd != -1) {
447 int file_lookup_open(struct fio_file *f, int flags)
449 struct fio_file *__f;
452 __f = lookup_file_hash(f->file_name);
454 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
456 * racy, need the __f->lock locked
459 f->lock_owner = __f->lock_owner;
460 f->lock_batch = __f->lock_batch;
461 f->lock_ddir = __f->lock_ddir;
464 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
468 f->fd = open(f->file_name, flags, 0600);
472 static int file_close_shadow_fds(struct thread_data *td)
478 for_each_file(td, f, i) {
479 if (f->shadow_fd == -1)
490 int generic_open_file(struct thread_data *td, struct fio_file *f)
496 dprint(FD_FILE, "fd open %s\n", f->file_name);
498 if (td_trim(td) && f->filetype != FIO_TYPE_BD) {
499 log_err("fio: trim only applies to block device\n");
503 if (!strcmp(f->file_name, "-")) {
505 log_err("fio: can't read/write to stdin/out\n");
511 * move output logging to stderr, if we are writing to stdout
520 flags |= OS_O_DIRECT;
523 if (td->o.create_on_open)
526 if (f->filetype != FIO_TYPE_FILE)
527 flags |= FIO_O_NOATIME;
534 if (f->filetype == FIO_TYPE_FILE)
538 f->fd = dup(STDOUT_FILENO);
540 from_hash = file_lookup_open(f, flags);
541 } else if (td_read(td)) {
542 if (f->filetype == FIO_TYPE_CHAR && !read_only)
548 f->fd = dup(STDIN_FILENO);
550 from_hash = file_lookup_open(f, flags);
553 from_hash = file_lookup_open(f, flags);
557 char buf[FIO_VERROR_SIZE];
560 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
561 flags &= ~FIO_O_NOATIME;
564 if (__e == EMFILE && file_close_shadow_fds(td))
567 snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
569 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
570 log_err("fio: looks like your file system does not " \
571 "support direct=1/buffered=0\n");
574 td_verror(td, __e, buf);
577 if (!from_hash && f->fd != -1) {
578 if (add_file_hash(f)) {
582 * Stash away descriptor for later close. This is to
583 * work-around a "feature" on Linux, where a close of
584 * an fd that has been opened for write will trigger
585 * udev to call blkid to check partitions, fs id, etc.
586 * That polutes the device cache, which can slow down
587 * unbuffered accesses.
589 if (f->shadow_fd == -1)
590 f->shadow_fd = f->fd;
593 * OK to ignore, we haven't done anything
596 ret = generic_close_file(td, f);
605 int generic_get_file_size(struct thread_data *td, struct fio_file *f)
607 return get_file_size(td, f);
611 * open/close all files, so that ->real_file_size gets set
613 static int get_file_sizes(struct thread_data *td)
619 for_each_file(td, f, i) {
620 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
623 if (td_io_get_file_size(td, f)) {
624 if (td->error != ENOENT) {
625 log_err("%s\n", td->verror);
631 if (f->real_file_size == -1ULL && td->o.size)
632 f->real_file_size = td->o.size / td->o.nr_files;
639 struct flist_head list;
646 * Get free number of bytes for each file on each unique mount.
648 static unsigned long long get_fs_free_counts(struct thread_data *td)
650 struct flist_head *n, *tmp;
651 unsigned long long ret = 0;
652 struct fio_mount *fm;
657 for_each_file(td, f, i) {
661 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
662 if (f->real_file_size != -1ULL)
663 ret += f->real_file_size;
665 } else if (f->filetype != FIO_TYPE_FILE)
668 strcpy(buf, f->file_name);
670 if (stat(buf, &sb) < 0) {
674 if (stat(buf, &sb) < 0)
679 flist_for_each(n, &list) {
680 fm = flist_entry(n, struct fio_mount, list);
681 if (fm->key == sb.st_dev)
690 fm = malloc(sizeof(*fm));
691 strcpy(fm->__base, buf);
692 fm->base = basename(fm->__base);
694 flist_add(&fm->list, &list);
697 flist_for_each_safe(n, tmp, &list) {
698 unsigned long long sz;
700 fm = flist_entry(n, struct fio_mount, list);
701 flist_del(&fm->list);
703 sz = get_fs_size(fm->base);
704 if (sz && sz != -1ULL)
713 uint64_t get_start_offset(struct thread_data *td)
715 return td->o.start_offset +
716 (td->thread_number - 1) * td->o.offset_increment;
720 * Open the files and setup files sizes, creating files if necessary.
722 int setup_files(struct thread_data *td)
724 unsigned long long total_size, extend_size;
727 int err = 0, need_extend;
729 dprint(FD_FILE, "setup files\n");
731 if (td->o.read_iolog_file)
735 * if ioengine defines a setup() method, it's responsible for
736 * opening the files and setting f->real_file_size to indicate
737 * the valid range for that file.
739 if (td->io_ops->setup)
740 err = td->io_ops->setup(td);
742 err = get_file_sizes(td);
748 * check sizes. if the files/devices do not exist and the size
749 * isn't passed to fio, abort.
752 for_each_file(td, f, i) {
753 if (f->real_file_size == -1ULL)
756 total_size += f->real_file_size;
759 if (td->o.fill_device)
760 td->fill_device_size = get_fs_free_counts(td);
763 * device/file sizes are zero and no size given, punt
765 if ((!total_size || total_size == -1ULL) && !td->o.size &&
766 !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
767 log_err("%s: you need to specify size=\n", td->o.name);
768 td_verror(td, EINVAL, "total_file_size");
773 * now file sizes are known, so we can set ->io_size. if size= is
774 * not given, ->io_size is just equal to ->real_file_size. if size
775 * is given, ->io_size is size / nr_files.
777 extend_size = total_size = 0;
779 for_each_file(td, f, i) {
780 f->file_offset = get_start_offset(td);
782 if (!td->o.file_size_low) {
784 * no file size range given, file size is equal to
785 * total size divided by number of files. if that is
786 * zero, set it to the real file size.
788 f->io_size = td->o.size / td->o.nr_files;
790 f->io_size = f->real_file_size - f->file_offset;
791 } else if (f->real_file_size < td->o.file_size_low ||
792 f->real_file_size > td->o.file_size_high) {
793 if (f->file_offset > td->o.file_size_low)
796 * file size given. if it's fixed, use that. if it's a
797 * range, generate a random size in-between.
799 if (td->o.file_size_low == td->o.file_size_high) {
800 f->io_size = td->o.file_size_low
803 f->io_size = get_rand_file_size(td)
807 f->io_size = f->real_file_size - f->file_offset;
809 if (f->io_size == -1ULL)
812 if (td->o.size_percent)
813 f->io_size = (f->io_size * td->o.size_percent) / 100;
814 total_size += f->io_size;
817 if (f->filetype == FIO_TYPE_FILE &&
818 (f->io_size + f->file_offset) > f->real_file_size &&
819 !(td->io_ops->flags & FIO_DISKLESSIO)) {
820 if (!td->o.create_on_open) {
822 extend_size += (f->io_size + f->file_offset);
824 f->real_file_size = f->io_size + f->file_offset;
825 fio_file_set_extend(f);
829 if (!td->o.size || td->o.size > total_size)
830 td->o.size = total_size;
833 * See if we need to extend some files
837 if (output_format == FIO_OUTPUT_NORMAL)
838 log_info("%s: Laying out IO file(s) (%u file(s) /"
839 " %lluMB)\n", td->o.name, need_extend,
842 for_each_file(td, f, i) {
843 unsigned long long old_len = -1ULL, extend_len = -1ULL;
845 if (!fio_file_extend(f))
848 assert(f->filetype == FIO_TYPE_FILE);
849 fio_file_clear_extend(f);
850 if (!td->o.fill_device) {
851 old_len = f->real_file_size;
852 extend_len = f->io_size + f->file_offset -
855 f->real_file_size = (f->io_size + f->file_offset);
856 err = extend_file(td, f);
860 err = __file_invalidate_cache(td, f, old_len,
873 if (!td->o.zone_size)
874 td->o.zone_size = td->o.size;
877 * iolog already set the total io size, if we read back
880 if (!td->o.read_iolog_file)
881 td->total_io_size = td->o.size * td->o.loops;
884 if (td->o.create_only)
889 log_err("%s: you need to specify valid offset=\n", td->o.name);
893 int pre_read_files(struct thread_data *td)
898 dprint(FD_FILE, "pre_read files\n");
900 for_each_file(td, f, i) {
901 pre_read_file(td, f);
907 static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
909 unsigned int range_size, seed;
910 unsigned long nranges;
913 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
914 file_size = min(f->real_file_size, f->io_size);
916 nranges = (file_size + range_size - 1) / range_size;
918 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
919 if (!td->o.rand_repeatable)
920 seed = td->rand_seeds[4];
922 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
923 zipf_init(&f->zipf, nranges, td->o.zipf_theta, seed);
925 pareto_init(&f->zipf, nranges, td->o.pareto_h, seed);
930 static int init_rand_distribution(struct thread_data *td)
936 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
939 state = td->runstate;
940 td_set_runstate(td, TD_SETTING_UP);
941 for_each_file(td, f, i)
942 __init_rand_distribution(td, f);
943 td_set_runstate(td, state);
948 int init_random_map(struct thread_data *td)
950 unsigned long long blocks;
954 if (init_rand_distribution(td))
959 for_each_file(td, f, i) {
960 uint64_t file_size = min(f->real_file_size, f->io_size);
962 blocks = file_size / (unsigned long long) td->o.rw_min_bs;
964 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
967 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
969 if (!lfsr_init(&f->lfsr, blocks, seed, seed & 0xF))
971 } else if (!td->o.norandommap) {
972 f->io_axmap = axmap_new(blocks);
975 } else if (td->o.norandommap)
978 if (!td->o.softrandommap) {
979 log_err("fio: failed allocating random map. If running"
980 " a large number of jobs, try the 'norandommap'"
981 " option or set 'softrandommap'. Or give"
982 " a larger --alloc-size to fio.\n");
986 log_info("fio: file %s failed allocating random map. Running "
987 "job without.\n", f->file_name);
993 void close_files(struct thread_data *td)
998 for_each_file(td, f, i) {
999 if (fio_file_open(f))
1000 td_io_close_file(td, f);
1004 void close_and_free_files(struct thread_data *td)
1009 dprint(FD_FILE, "close files\n");
1011 for_each_file(td, f, i) {
1012 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1013 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1014 unlink(f->file_name);
1017 if (fio_file_open(f))
1018 td_io_close_file(td, f);
1020 remove_file_hash(f);
1022 sfree(f->file_name);
1023 f->file_name = NULL;
1024 axmap_free(f->io_axmap);
1029 td->o.filename = NULL;
1031 td->files_index = 0;
1036 static void get_file_type(struct fio_file *f)
1040 if (!strcmp(f->file_name, "-"))
1041 f->filetype = FIO_TYPE_PIPE;
1043 f->filetype = FIO_TYPE_FILE;
1045 /* \\.\ is the device namespace in Windows, where every file is
1047 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1048 f->filetype = FIO_TYPE_BD;
1050 if (!stat(f->file_name, &sb)) {
1051 if (S_ISBLK(sb.st_mode))
1052 f->filetype = FIO_TYPE_BD;
1053 else if (S_ISCHR(sb.st_mode))
1054 f->filetype = FIO_TYPE_CHAR;
1055 else if (S_ISFIFO(sb.st_mode))
1056 f->filetype = FIO_TYPE_PIPE;
1060 int add_file(struct thread_data *td, const char *fname)
1062 int cur_files = td->files_index;
1063 char file_name[PATH_MAX];
1067 dprint(FD_FILE, "add file %s\n", fname);
1069 f = smalloc(sizeof(*f));
1071 log_err("fio: smalloc OOM\n");
1077 fio_file_reset(td, f);
1079 if (td->files_size <= td->files_index) {
1080 unsigned int new_size = td->o.nr_files + 1;
1082 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1084 td->files = realloc(td->files, new_size * sizeof(f));
1085 if (td->files == NULL) {
1086 log_err("fio: realloc OOM\n");
1089 td->files_size = new_size;
1091 td->files[cur_files] = f;
1092 f->fileno = cur_files;
1095 * init function, io engine may not be loaded yet
1097 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1098 f->real_file_size = -1ULL;
1100 if (td->o.directory)
1101 len = sprintf(file_name, "%s/", td->o.directory);
1103 sprintf(file_name + len, "%s", fname);
1104 f->file_name = smalloc_strdup(file_name);
1105 if (!f->file_name) {
1106 log_err("fio: smalloc OOM\n");
1112 switch (td->o.file_lock_mode) {
1113 case FILE_LOCK_NONE:
1115 case FILE_LOCK_READWRITE:
1116 f->lock = fio_mutex_rw_init();
1118 case FILE_LOCK_EXCLUSIVE:
1119 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
1122 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1127 if (f->filetype == FIO_TYPE_FILE)
1128 td->nr_normal_files++;
1130 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1136 int add_file_exclusive(struct thread_data *td, const char *fname)
1141 for_each_file(td, f, i) {
1142 if (!strcmp(f->file_name, fname))
1146 return add_file(td, fname);
1149 void get_file(struct fio_file *f)
1151 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
1152 assert(fio_file_open(f));
1156 int put_file(struct thread_data *td, struct fio_file *f)
1158 int f_ret = 0, ret = 0;
1160 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
1162 if (!fio_file_open(f)) {
1163 assert(f->fd == -1);
1167 assert(f->references);
1168 if (--f->references)
1171 if (should_fsync(td) && td->o.fsync_on_close)
1172 f_ret = fsync(f->fd);
1174 if (td->io_ops->close_file)
1175 ret = td->io_ops->close_file(td, f);
1180 td->nr_open_files--;
1181 fio_file_clear_open(f);
1182 assert(f->fd == -1);
1186 void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
1188 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1191 if (f->lock_owner == td && f->lock_batch--)
1194 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1195 if (ddir == DDIR_READ)
1196 fio_mutex_down_read(f->lock);
1198 fio_mutex_down_write(f->lock);
1199 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1200 fio_mutex_down(f->lock);
1203 f->lock_batch = td->o.lockfile_batch;
1204 f->lock_ddir = ddir;
1207 void unlock_file(struct thread_data *td, struct fio_file *f)
1209 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1214 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1215 const int is_read = f->lock_ddir == DDIR_READ;
1216 int val = fio_mutex_getval(f->lock);
1218 if ((is_read && val == 1) || (!is_read && val == -1))
1219 f->lock_owner = NULL;
1222 fio_mutex_up_read(f->lock);
1224 fio_mutex_up_write(f->lock);
1225 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
1226 int val = fio_mutex_getval(f->lock);
1229 f->lock_owner = NULL;
1231 fio_mutex_up(f->lock);
1235 void unlock_file_all(struct thread_data *td, struct fio_file *f)
1237 if (f->lock_owner != td)
1244 static int recurse_dir(struct thread_data *td, const char *dirname)
1250 D = opendir(dirname);
1252 char buf[FIO_VERROR_SIZE];
1254 snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
1255 td_verror(td, errno, buf);
1259 while ((dir = readdir(D)) != NULL) {
1260 char full_path[PATH_MAX];
1263 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1266 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
1268 if (lstat(full_path, &sb) == -1) {
1269 if (errno != ENOENT) {
1270 td_verror(td, errno, "stat");
1275 if (S_ISREG(sb.st_mode)) {
1276 add_file(td, full_path);
1280 if (!S_ISDIR(sb.st_mode))
1283 ret = recurse_dir(td, full_path);
1292 int add_dir_files(struct thread_data *td, const char *path)
1294 int ret = recurse_dir(td, path);
1297 log_info("fio: opendir added %d files\n", td->o.nr_files);
1302 void dup_files(struct thread_data *td, struct thread_data *org)
1307 dprint(FD_FILE, "dup files: %d\n", org->files_index);
1312 td->files = malloc(org->files_index * sizeof(f));
1314 for_each_file(org, f, i) {
1315 struct fio_file *__f;
1317 __f = smalloc(sizeof(*__f));
1319 log_err("fio: smalloc OOM\n");
1323 fio_file_reset(td, __f);
1326 __f->file_name = smalloc_strdup(f->file_name);
1327 if (!__f->file_name) {
1328 log_err("fio: smalloc OOM\n");
1332 __f->filetype = f->filetype;
1340 * Returns the index that matches the filename, or -1 if not there
1342 int get_fileno(struct thread_data *td, const char *fname)
1347 for_each_file(td, f, i)
1348 if (!strcmp(f->file_name, fname))
1355 * For log usage, where we add/open/close files automatically
1357 void free_release_files(struct thread_data *td)
1360 td->files_index = 0;
1361 td->nr_normal_files = 0;
1364 void fio_file_reset(struct thread_data *td, struct fio_file *f)
1366 f->last_pos = f->file_offset;
1367 f->last_start = -1ULL;
1369 axmap_reset(f->io_axmap);
1370 if (td->o.random_generator == FIO_RAND_GEN_LFSR)
1371 lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);