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]);
131 left = f->real_file_size;
132 while (left && !td->terminate) {
133 bs = td->o.max_bs[DDIR_WRITE];
137 fill_io_buffer(td, b, bs, bs);
139 r = write(f->fd, b, bs);
149 if (td->o.fill_device)
151 log_info("fio: ENOSPC on laying out "
155 td_verror(td, errno, "write");
157 td_verror(td, EIO, "write");
164 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
165 unlink(f->file_name);
166 } else if (td->o.create_fsync) {
167 if (fsync(f->fd) < 0) {
168 td_verror(td, errno, "fsync");
172 if (td->o.fill_device && !td_write(td)) {
173 fio_file_clear_size_known(f);
174 if (td_io_get_file_size(td, f))
176 if (f->io_size > f->real_file_size)
177 f->io_size = f->real_file_size;
189 static int pre_read_file(struct thread_data *td, struct fio_file *f)
191 int r, did_open = 0, old_runstate;
192 unsigned long long left;
196 if (td->io_ops->flags & FIO_PIPEIO)
199 if (!fio_file_open(f)) {
200 if (td->io_ops->open_file(td, f)) {
201 log_err("fio: cannot pre-read, failed to open file\n");
207 old_runstate = td->runstate;
208 td_set_runstate(td, TD_PRE_READING);
210 bs = td->o.max_bs[DDIR_READ];
214 lseek(f->fd, f->file_offset, SEEK_SET);
217 while (left && !td->terminate) {
221 r = read(f->fd, b, bs);
227 td_verror(td, EIO, "pre_read");
232 td_set_runstate(td, old_runstate);
235 td->io_ops->close_file(td, f);
240 static unsigned long long get_rand_file_size(struct thread_data *td)
242 unsigned long long ret, sized;
245 if (td->o.use_os_rand) {
246 r = os_random_long(&td->file_size_state);
247 sized = td->o.file_size_high - td->o.file_size_low;
248 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
250 r = __rand(&td->__file_size_state);
251 sized = td->o.file_size_high - td->o.file_size_low;
252 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0)));
255 ret += td->o.file_size_low;
256 ret -= (ret % td->o.rw_min_bs);
260 static int file_size(struct thread_data *td, struct fio_file *f)
264 if (stat(f->file_name, &st) == -1) {
265 td_verror(td, errno, "fstat");
269 f->real_file_size = st.st_size;
273 static int bdev_size(struct thread_data *td, struct fio_file *f)
275 unsigned long long bytes = 0;
278 if (td->io_ops->open_file(td, f)) {
279 log_err("fio: failed opening blockdev %s for size check\n",
284 r = blockdev_size(f, &bytes);
286 td_verror(td, r, "blockdev_size");
291 log_err("%s: zero sized block device?\n", f->file_name);
295 f->real_file_size = bytes;
296 td->io_ops->close_file(td, f);
299 td->io_ops->close_file(td, f);
303 static int char_size(struct thread_data *td, struct fio_file *f)
305 #ifdef FIO_HAVE_CHARDEV_SIZE
306 unsigned long long bytes = 0;
309 if (td->io_ops->open_file(td, f)) {
310 log_err("fio: failed opening blockdev %s for size check\n",
315 r = chardev_size(f, &bytes);
317 td_verror(td, r, "chardev_size");
322 log_err("%s: zero sized char device?\n", f->file_name);
326 f->real_file_size = bytes;
327 td->io_ops->close_file(td, f);
330 td->io_ops->close_file(td, f);
333 f->real_file_size = -1ULL;
338 static int get_file_size(struct thread_data *td, struct fio_file *f)
342 if (fio_file_size_known(f))
345 if (f->filetype == FIO_TYPE_FILE)
346 ret = file_size(td, f);
347 else if (f->filetype == FIO_TYPE_BD)
348 ret = bdev_size(td, f);
349 else if (f->filetype == FIO_TYPE_CHAR)
350 ret = char_size(td, f);
352 f->real_file_size = -1;
357 if (f->file_offset > f->real_file_size) {
358 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
359 (unsigned long long) f->file_offset,
360 (unsigned long long) f->real_file_size);
364 fio_file_set_size_known(f);
368 static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
369 unsigned long long off,
370 unsigned long long len)
377 off = f->file_offset;
379 if (len == -1ULL || off == -1ULL)
382 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
386 * FIXME: add blockdev flushing too
389 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
391 if (f->filetype == FIO_TYPE_BD)
392 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
394 } else if (f->filetype == FIO_TYPE_FILE) {
395 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
396 } else if (f->filetype == FIO_TYPE_BD) {
397 ret = blockdev_invalidate_cache(f);
398 if (ret < 0 && errno == EACCES && geteuid()) {
400 log_err("fio: only root may flush block "
401 "devices. Cache flush bypassed!\n");
406 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
410 td_verror(td, errno, "invalidate_cache");
412 } else if (ret > 0) {
413 td_verror(td, ret, "invalidate_cache");
421 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
423 if (!fio_file_open(f))
426 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
429 int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
433 dprint(FD_FILE, "fd close %s\n", f->file_name);
437 if (close(f->fd) < 0)
442 if (f->shadow_fd != -1) {
451 int file_lookup_open(struct fio_file *f, int flags)
453 struct fio_file *__f;
456 __f = lookup_file_hash(f->file_name);
458 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
460 * racy, need the __f->lock locked
465 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
469 f->fd = open(f->file_name, flags, 0600);
473 static int file_close_shadow_fds(struct thread_data *td)
479 for_each_file(td, f, i) {
480 if (f->shadow_fd == -1)
491 int generic_open_file(struct thread_data *td, struct fio_file *f)
497 dprint(FD_FILE, "fd open %s\n", f->file_name);
499 if (td_trim(td) && f->filetype != FIO_TYPE_BD) {
500 log_err("fio: trim only applies to block device\n");
504 if (!strcmp(f->file_name, "-")) {
506 log_err("fio: can't read/write to stdin/out\n");
512 * move output logging to stderr, if we are writing to stdout
521 flags |= OS_O_DIRECT;
524 if (td->o.create_on_open)
527 if (f->filetype != FIO_TYPE_FILE)
528 flags |= FIO_O_NOATIME;
535 if (f->filetype == FIO_TYPE_FILE)
539 f->fd = dup(STDOUT_FILENO);
541 from_hash = file_lookup_open(f, flags);
542 } else if (td_read(td)) {
543 if (f->filetype == FIO_TYPE_CHAR && !read_only)
549 f->fd = dup(STDIN_FILENO);
551 from_hash = file_lookup_open(f, flags);
554 from_hash = file_lookup_open(f, flags);
558 char buf[FIO_VERROR_SIZE];
561 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
562 flags &= ~FIO_O_NOATIME;
565 if (__e == EMFILE && file_close_shadow_fds(td))
568 snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
570 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
571 log_err("fio: looks like your file system does not " \
572 "support direct=1/buffered=0\n");
575 td_verror(td, __e, buf);
578 if (!from_hash && f->fd != -1) {
579 if (add_file_hash(f)) {
583 * Stash away descriptor for later close. This is to
584 * work-around a "feature" on Linux, where a close of
585 * an fd that has been opened for write will trigger
586 * udev to call blkid to check partitions, fs id, etc.
587 * That pollutes the device cache, which can slow down
588 * unbuffered accesses.
590 if (f->shadow_fd == -1)
591 f->shadow_fd = f->fd;
594 * OK to ignore, we haven't done anything
597 ret = generic_close_file(td, f);
606 int generic_get_file_size(struct thread_data *td, struct fio_file *f)
608 return get_file_size(td, f);
612 * open/close all files, so that ->real_file_size gets set
614 static int get_file_sizes(struct thread_data *td)
620 for_each_file(td, f, i) {
621 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
624 if (td_io_get_file_size(td, f)) {
625 if (td->error != ENOENT) {
626 log_err("%s\n", td->verror);
632 if (f->real_file_size == -1ULL && td->o.size)
633 f->real_file_size = td->o.size / td->o.nr_files;
640 struct flist_head list;
647 * Get free number of bytes for each file on each unique mount.
649 static unsigned long long get_fs_free_counts(struct thread_data *td)
651 struct flist_head *n, *tmp;
652 unsigned long long ret = 0;
653 struct fio_mount *fm;
658 for_each_file(td, f, i) {
662 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
663 if (f->real_file_size != -1ULL)
664 ret += f->real_file_size;
666 } else if (f->filetype != FIO_TYPE_FILE)
669 strcpy(buf, f->file_name);
671 if (stat(buf, &sb) < 0) {
675 if (stat(buf, &sb) < 0)
680 flist_for_each(n, &list) {
681 fm = flist_entry(n, struct fio_mount, list);
682 if (fm->key == sb.st_dev)
691 fm = malloc(sizeof(*fm));
692 strcpy(fm->__base, buf);
693 fm->base = basename(fm->__base);
695 flist_add(&fm->list, &list);
698 flist_for_each_safe(n, tmp, &list) {
699 unsigned long long sz;
701 fm = flist_entry(n, struct fio_mount, list);
702 flist_del(&fm->list);
704 sz = get_fs_size(fm->base);
705 if (sz && sz != -1ULL)
714 uint64_t get_start_offset(struct thread_data *td)
716 return td->o.start_offset +
717 (td->thread_number - 1) * td->o.offset_increment;
721 * Open the files and setup files sizes, creating files if necessary.
723 int setup_files(struct thread_data *td)
725 unsigned long long total_size, extend_size;
726 struct thread_options *o = &td->o;
729 int err = 0, need_extend;
732 dprint(FD_FILE, "setup files\n");
734 old_state = td->runstate;
735 td_set_runstate(td, TD_SETTING_UP);
737 if (o->read_iolog_file)
741 * if ioengine defines a setup() method, it's responsible for
742 * opening the files and setting f->real_file_size to indicate
743 * the valid range for that file.
745 if (td->io_ops->setup)
746 err = td->io_ops->setup(td);
748 err = get_file_sizes(td);
754 * check sizes. if the files/devices do not exist and the size
755 * isn't passed to fio, abort.
758 for_each_file(td, f, i) {
759 if (f->real_file_size == -1ULL)
762 total_size += f->real_file_size;
766 td->fill_device_size = get_fs_free_counts(td);
769 * device/file sizes are zero and no size given, punt
771 if ((!total_size || total_size == -1ULL) && !o->size &&
772 !(td->io_ops->flags & FIO_NOIO) && !o->fill_device &&
773 !(o->nr_files && (o->file_size_low || o->file_size_high))) {
774 log_err("%s: you need to specify size=\n", o->name);
775 td_verror(td, EINVAL, "total_file_size");
780 * now file sizes are known, so we can set ->io_size. if size= is
781 * not given, ->io_size is just equal to ->real_file_size. if size
782 * is given, ->io_size is size / nr_files.
784 extend_size = total_size = 0;
786 for_each_file(td, f, i) {
787 f->file_offset = get_start_offset(td);
789 if (!o->file_size_low) {
791 * no file size range given, file size is equal to
792 * total size divided by number of files. if that is
793 * zero, set it to the real file size.
795 f->io_size = o->size / o->nr_files;
797 f->io_size = f->real_file_size - f->file_offset;
798 } else if (f->real_file_size < o->file_size_low ||
799 f->real_file_size > o->file_size_high) {
800 if (f->file_offset > o->file_size_low)
803 * file size given. if it's fixed, use that. if it's a
804 * range, generate a random size in-between.
806 if (o->file_size_low == o->file_size_high)
807 f->io_size = o->file_size_low - f->file_offset;
809 f->io_size = get_rand_file_size(td)
813 f->io_size = f->real_file_size - f->file_offset;
815 if (f->io_size == -1ULL)
819 f->io_size = (f->io_size * o->size_percent) / 100;
820 total_size += f->io_size;
823 if (f->filetype == FIO_TYPE_FILE &&
824 (f->io_size + f->file_offset) > f->real_file_size &&
825 !(td->io_ops->flags & FIO_DISKLESSIO)) {
826 if (!o->create_on_open) {
828 extend_size += (f->io_size + f->file_offset);
830 f->real_file_size = f->io_size + f->file_offset;
831 fio_file_set_extend(f);
835 if (!o->size || o->size > total_size)
836 o->size = total_size;
839 * See if we need to extend some files
843 if (output_format == FIO_OUTPUT_NORMAL)
844 log_info("%s: Laying out IO file(s) (%u file(s) /"
845 " %lluMB)\n", o->name, need_extend,
848 for_each_file(td, f, i) {
849 unsigned long long old_len = -1ULL, extend_len = -1ULL;
851 if (!fio_file_extend(f))
854 assert(f->filetype == FIO_TYPE_FILE);
855 fio_file_clear_extend(f);
856 if (!o->fill_device) {
857 old_len = f->real_file_size;
858 extend_len = f->io_size + f->file_offset -
861 f->real_file_size = (f->io_size + f->file_offset);
862 err = extend_file(td, f);
866 err = __file_invalidate_cache(td, f, old_len,
880 o->zone_size = o->size;
883 * iolog already set the total io size, if we read back
886 if (!o->read_iolog_file)
887 td->total_io_size = o->size * o->loops;
893 td_set_runstate(td, old_state);
896 log_err("%s: you need to specify valid offset=\n", o->name);
898 td_set_runstate(td, old_state);
902 int pre_read_files(struct thread_data *td)
907 dprint(FD_FILE, "pre_read files\n");
909 for_each_file(td, f, i) {
910 pre_read_file(td, f);
916 static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
918 unsigned int range_size, seed;
919 unsigned long nranges;
922 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
923 file_size = min(f->real_file_size, f->io_size);
925 nranges = (file_size + range_size - 1) / range_size;
927 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
928 if (!td->o.rand_repeatable)
929 seed = td->rand_seeds[4];
931 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
932 zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed);
934 pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed);
939 static int init_rand_distribution(struct thread_data *td)
945 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
948 state = td->runstate;
949 td_set_runstate(td, TD_SETTING_UP);
950 for_each_file(td, f, i)
951 __init_rand_distribution(td, f);
952 td_set_runstate(td, state);
957 int init_random_map(struct thread_data *td)
959 unsigned long long blocks;
963 if (init_rand_distribution(td))
968 for_each_file(td, f, i) {
969 uint64_t file_size = min(f->real_file_size, f->io_size);
971 blocks = file_size / (unsigned long long) td->o.rw_min_bs;
973 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
976 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
978 if (!lfsr_init(&f->lfsr, blocks, seed, seed & 0xF))
980 } else if (!td->o.norandommap) {
981 f->io_axmap = axmap_new(blocks);
984 } else if (td->o.norandommap)
987 if (!td->o.softrandommap) {
988 log_err("fio: failed allocating random map. If running"
989 " a large number of jobs, try the 'norandommap'"
990 " option or set 'softrandommap'. Or give"
991 " a larger --alloc-size to fio.\n");
995 log_info("fio: file %s failed allocating random map. Running "
996 "job without.\n", f->file_name);
1002 void close_files(struct thread_data *td)
1007 for_each_file(td, f, i) {
1008 if (fio_file_open(f))
1009 td_io_close_file(td, f);
1013 void close_and_free_files(struct thread_data *td)
1018 dprint(FD_FILE, "close files\n");
1020 for_each_file(td, f, i) {
1021 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1022 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1023 unlink(f->file_name);
1026 if (fio_file_open(f))
1027 td_io_close_file(td, f);
1029 remove_file_hash(f);
1031 sfree(f->file_name);
1032 f->file_name = NULL;
1033 axmap_free(f->io_axmap);
1038 td->o.filename = NULL;
1040 free(td->file_locks);
1041 td->files_index = 0;
1043 td->file_locks = NULL;
1047 static void get_file_type(struct fio_file *f)
1051 if (!strcmp(f->file_name, "-"))
1052 f->filetype = FIO_TYPE_PIPE;
1054 f->filetype = FIO_TYPE_FILE;
1056 /* \\.\ is the device namespace in Windows, where every file is
1058 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1059 f->filetype = FIO_TYPE_BD;
1061 if (!stat(f->file_name, &sb)) {
1062 if (S_ISBLK(sb.st_mode))
1063 f->filetype = FIO_TYPE_BD;
1064 else if (S_ISCHR(sb.st_mode))
1065 f->filetype = FIO_TYPE_CHAR;
1066 else if (S_ISFIFO(sb.st_mode))
1067 f->filetype = FIO_TYPE_PIPE;
1071 int add_file(struct thread_data *td, const char *fname)
1073 int cur_files = td->files_index;
1074 char file_name[PATH_MAX];
1078 dprint(FD_FILE, "add file %s\n", fname);
1080 f = smalloc(sizeof(*f));
1082 log_err("fio: smalloc OOM\n");
1088 fio_file_reset(td, f);
1090 if (td->files_size <= td->files_index) {
1091 unsigned int new_size = td->o.nr_files + 1;
1093 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1095 td->files = realloc(td->files, new_size * sizeof(f));
1096 if (td->files == NULL) {
1097 log_err("fio: realloc OOM\n");
1100 if (td->o.file_lock_mode != FILE_LOCK_NONE) {
1101 td->file_locks = realloc(td->file_locks, new_size);
1102 if (!td->file_locks) {
1103 log_err("fio: realloc OOM\n");
1106 td->file_locks[cur_files] = FILE_LOCK_NONE;
1108 td->files_size = new_size;
1110 td->files[cur_files] = f;
1111 f->fileno = cur_files;
1114 * init function, io engine may not be loaded yet
1116 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1117 f->real_file_size = -1ULL;
1119 if (td->o.directory)
1120 len = sprintf(file_name, "%s/", td->o.directory);
1122 sprintf(file_name + len, "%s", fname);
1123 f->file_name = smalloc_strdup(file_name);
1124 if (!f->file_name) {
1125 log_err("fio: smalloc OOM\n");
1131 switch (td->o.file_lock_mode) {
1132 case FILE_LOCK_NONE:
1134 case FILE_LOCK_READWRITE:
1135 f->rwlock = fio_rwlock_init();
1137 case FILE_LOCK_EXCLUSIVE:
1138 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
1141 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1146 if (f->filetype == FIO_TYPE_FILE)
1147 td->nr_normal_files++;
1149 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1155 int add_file_exclusive(struct thread_data *td, const char *fname)
1160 for_each_file(td, f, i) {
1161 if (!strcmp(f->file_name, fname))
1165 return add_file(td, fname);
1168 void get_file(struct fio_file *f)
1170 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
1171 assert(fio_file_open(f));
1175 int put_file(struct thread_data *td, struct fio_file *f)
1177 int f_ret = 0, ret = 0;
1179 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
1181 if (!fio_file_open(f)) {
1182 assert(f->fd == -1);
1186 assert(f->references);
1187 if (--f->references)
1190 if (should_fsync(td) && td->o.fsync_on_close)
1191 f_ret = fsync(f->fd);
1193 if (td->io_ops->close_file)
1194 ret = td->io_ops->close_file(td, f);
1199 td->nr_open_files--;
1200 fio_file_clear_open(f);
1201 assert(f->fd == -1);
1205 void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
1207 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1210 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1211 if (ddir == DDIR_READ)
1212 fio_rwlock_read(f->rwlock);
1214 fio_rwlock_write(f->rwlock);
1215 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1216 fio_mutex_down(f->lock);
1218 td->file_locks[f->fileno] = td->o.file_lock_mode;
1221 void unlock_file(struct thread_data *td, struct fio_file *f)
1223 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1226 if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1227 fio_rwlock_unlock(f->rwlock);
1228 else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1229 fio_mutex_up(f->lock);
1231 td->file_locks[f->fileno] = FILE_LOCK_NONE;
1234 void unlock_file_all(struct thread_data *td, struct fio_file *f)
1236 if (td->file_locks[f->fileno] != FILE_LOCK_NONE)
1240 static int recurse_dir(struct thread_data *td, const char *dirname)
1246 D = opendir(dirname);
1248 char buf[FIO_VERROR_SIZE];
1250 snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
1251 td_verror(td, errno, buf);
1255 while ((dir = readdir(D)) != NULL) {
1256 char full_path[PATH_MAX];
1259 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1262 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
1264 if (lstat(full_path, &sb) == -1) {
1265 if (errno != ENOENT) {
1266 td_verror(td, errno, "stat");
1271 if (S_ISREG(sb.st_mode)) {
1272 add_file(td, full_path);
1276 if (!S_ISDIR(sb.st_mode))
1279 ret = recurse_dir(td, full_path);
1288 int add_dir_files(struct thread_data *td, const char *path)
1290 int ret = recurse_dir(td, path);
1293 log_info("fio: opendir added %d files\n", td->o.nr_files);
1298 void dup_files(struct thread_data *td, struct thread_data *org)
1303 dprint(FD_FILE, "dup files: %d\n", org->files_index);
1308 td->files = malloc(org->files_index * sizeof(f));
1310 if (td->o.file_lock_mode != FILE_LOCK_NONE)
1311 td->file_locks = malloc(org->files_index);
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]);