18 static inline void clear_error(struct thread_data *td)
25 * Leaves f->fd open on success, caller must close
27 static int extend_file(struct thread_data *td, struct fio_file *f)
29 int r, new_layout = 0, unlink_file = 0, flags;
30 unsigned long long left;
35 log_err("fio: refusing extend of file due to read-only\n");
40 * check if we need to lay the file out complete again. fio
41 * does that for operations involving reads, or for writes
42 * where overwrite is set
44 if (td_read(td) || (td_write(td) && td->o.overwrite) ||
45 (td_write(td) && td->io_ops->flags & FIO_NOEXTEND))
47 if (td_write(td) && !td->o.overwrite)
50 if (unlink_file || new_layout) {
51 dprint(FD_FILE, "layout unlink %s\n", f->file_name);
52 if ((unlink(f->file_name) < 0) && (errno != ENOENT)) {
53 td_verror(td, errno, "unlink");
58 flags = O_WRONLY | O_CREAT;
62 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
63 f->fd = open(f->file_name, flags, 0644);
65 td_verror(td, errno, "open");
69 #ifdef FIO_HAVE_FALLOCATE
70 if (td->o.fallocate && !td->o.fill_device) {
71 dprint(FD_FILE, "fallocate file %s size %llu\n", f->file_name,
74 r = posix_fallocate(f->fd, 0, f->real_file_size);
76 log_err("fio: posix_fallocate fails: %s\n",
86 * The size will be -1ULL when fill_device is used, so don't truncate
87 * or fallocate this file, just write it
89 if (!td->o.fill_device) {
90 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
92 if (ftruncate(f->fd, f->real_file_size) == -1) {
93 td_verror(td, errno, "ftruncate");
98 b = malloc(td->o.max_bs[DDIR_WRITE]);
99 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
101 left = f->real_file_size;
102 while (left && !td->terminate) {
103 bs = td->o.max_bs[DDIR_WRITE];
107 r = write(f->fd, b, bs);
117 if (td->o.fill_device)
119 log_info("fio: ENOSPC on laying out "
123 td_verror(td, errno, "write");
125 td_verror(td, EIO, "write");
132 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
133 unlink(f->file_name);
134 } else if (td->o.create_fsync) {
135 if (fsync(f->fd) < 0) {
136 td_verror(td, errno, "fsync");
140 if (td->o.fill_device && !td_write(td)) {
141 fio_file_clear_size_known(f);
142 if (td_io_get_file_size(td, f))
144 if (f->io_size > f->real_file_size)
145 f->io_size = f->real_file_size;
157 static int pre_read_file(struct thread_data *td, struct fio_file *f)
159 int r, did_open = 0, old_runstate;
160 unsigned long long left;
164 if (td->io_ops->flags & FIO_PIPEIO)
167 if (!fio_file_open(f)) {
168 if (td->io_ops->open_file(td, f)) {
169 log_err("fio: cannot pre-read, failed to open file\n");
175 old_runstate = td->runstate;
176 td_set_runstate(td, TD_PRE_READING);
178 bs = td->o.max_bs[DDIR_READ];
182 lseek(f->fd, f->file_offset, SEEK_SET);
185 while (left && !td->terminate) {
189 r = read(f->fd, b, bs);
195 td_verror(td, EIO, "pre_read");
200 td_set_runstate(td, old_runstate);
203 td->io_ops->close_file(td, f);
208 static unsigned long long get_rand_file_size(struct thread_data *td)
210 unsigned long long ret, sized;
213 if (td->o.use_os_rand) {
214 r = os_random_long(&td->file_size_state);
215 sized = td->o.file_size_high - td->o.file_size_low;
216 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
218 r = __rand(&td->__file_size_state);
219 sized = td->o.file_size_high - td->o.file_size_low;
220 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0)));
223 ret += td->o.file_size_low;
224 ret -= (ret % td->o.rw_min_bs);
228 static int file_size(struct thread_data *td, struct fio_file *f)
232 if (stat(f->file_name, &st) == -1) {
233 td_verror(td, errno, "fstat");
237 f->real_file_size = st.st_size;
241 static int bdev_size(struct thread_data *td, struct fio_file *f)
243 unsigned long long bytes = 0;
246 if (td->io_ops->open_file(td, f)) {
247 log_err("fio: failed opening blockdev %s for size check\n",
252 r = blockdev_size(f, &bytes);
254 td_verror(td, r, "blockdev_size");
255 printf("fd is %d\n", f->fd);
260 log_err("%s: zero sized block device?\n", f->file_name);
264 f->real_file_size = bytes;
265 td->io_ops->close_file(td, f);
268 td->io_ops->close_file(td, f);
272 static int char_size(struct thread_data *td, struct fio_file *f)
274 #ifdef FIO_HAVE_CHARDEV_SIZE
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 = chardev_size(f, &bytes);
286 td_verror(td, r, "chardev_size");
291 log_err("%s: zero sized char 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);
302 f->real_file_size = -1ULL;
307 static int get_file_size(struct thread_data *td, struct fio_file *f)
311 if (fio_file_size_known(f))
314 if (f->filetype == FIO_TYPE_FILE)
315 ret = file_size(td, f);
316 else if (f->filetype == FIO_TYPE_BD)
317 ret = bdev_size(td, f);
318 else if (f->filetype == FIO_TYPE_CHAR)
319 ret = char_size(td, f);
321 f->real_file_size = -1;
326 if (f->file_offset > f->real_file_size) {
327 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
328 f->file_offset, f->real_file_size);
332 fio_file_set_size_known(f);
336 static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
337 unsigned long long off,
338 unsigned long long len)
345 off = f->file_offset;
347 if (len == -1ULL || off == -1ULL)
350 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
354 * FIXME: add blockdev flushing too
357 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
359 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
361 } else if (f->filetype == FIO_TYPE_FILE) {
362 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
363 } else if (f->filetype == FIO_TYPE_BD) {
364 ret = blockdev_invalidate_cache(f);
365 if (ret < 0 && errno == EACCES && geteuid()) {
367 log_err("fio: only root may flush block "
368 "devices. Cache flush bypassed!\n");
373 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
377 td_verror(td, errno, "invalidate_cache");
379 } else if (ret > 0) {
380 td_verror(td, ret, "invalidate_cache");
388 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
390 if (!fio_file_open(f))
393 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
396 int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
400 dprint(FD_FILE, "fd close %s\n", f->file_name);
404 if (close(f->fd) < 0)
411 static int file_lookup_open(struct fio_file *f, int flags)
413 struct fio_file *__f;
416 __f = lookup_file_hash(f->file_name);
418 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
420 * racy, need the __f->lock locked
423 f->lock_owner = __f->lock_owner;
424 f->lock_batch = __f->lock_batch;
425 f->lock_ddir = __f->lock_ddir;
428 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
432 f->fd = open(f->file_name, flags, 0600);
436 int generic_open_file(struct thread_data *td, struct fio_file *f)
442 dprint(FD_FILE, "fd open %s\n", f->file_name);
444 if (!strcmp(f->file_name, "-")) {
446 log_err("fio: can't read/write to stdin/out\n");
452 * move output logging to stderr, if we are writing to stdout
459 flags |= OS_O_DIRECT;
462 if (f->filetype != FIO_TYPE_FILE)
463 flags |= FIO_O_NOATIME;
464 if (td->o.create_on_open)
472 if (f->filetype == FIO_TYPE_FILE)
476 f->fd = dup(STDOUT_FILENO);
478 from_hash = file_lookup_open(f, flags);
480 if (f->filetype == FIO_TYPE_CHAR && !read_only)
486 f->fd = dup(STDIN_FILENO);
488 from_hash = file_lookup_open(f, flags);
492 char buf[FIO_VERROR_SIZE];
495 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
496 flags &= ~FIO_O_NOATIME;
500 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
502 td_verror(td, __e, buf);
505 if (!from_hash && f->fd != -1) {
506 if (add_file_hash(f)) {
510 * OK to ignore, we haven't done anything with it
512 ret = generic_close_file(td, f);
520 int generic_get_file_size(struct thread_data *td, struct fio_file *f)
522 return get_file_size(td, f);
526 * open/close all files, so that ->real_file_size gets set
528 static int get_file_sizes(struct thread_data *td)
534 for_each_file(td, f, i) {
535 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
538 if (td_io_get_file_size(td, f)) {
539 if (td->error != ENOENT) {
540 log_err("%s\n", td->verror);
546 if (f->real_file_size == -1ULL && td->o.size)
547 f->real_file_size = td->o.size / td->o.nr_files;
554 struct flist_head list;
561 * Get free number of bytes for each file on each unique mount.
563 static unsigned long long get_fs_free_counts(struct thread_data *td)
565 struct flist_head *n, *tmp;
566 unsigned long long ret = 0;
567 struct fio_mount *fm;
572 for_each_file(td, f, i) {
576 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
577 if (f->real_file_size != -1ULL)
578 ret += f->real_file_size;
580 } else if (f->filetype != FIO_TYPE_FILE)
583 strcpy(buf, f->file_name);
585 if (stat(buf, &sb) < 0) {
589 if (stat(buf, &sb) < 0)
594 flist_for_each(n, &list) {
595 fm = flist_entry(n, struct fio_mount, list);
596 if (fm->key == sb.st_dev)
605 fm = malloc(sizeof(*fm));
606 strcpy(fm->__base, buf);
607 fm->base = basename(fm->__base);
609 flist_add(&fm->list, &list);
612 flist_for_each_safe(n, tmp, &list) {
613 unsigned long long sz;
615 fm = flist_entry(n, struct fio_mount, list);
616 flist_del(&fm->list);
618 sz = get_fs_size(fm->base);
619 if (sz && sz != -1ULL)
629 * Open the files and setup files sizes, creating files if necessary.
631 int setup_files(struct thread_data *td)
633 unsigned long long total_size, extend_size;
636 int err = 0, need_extend;
638 dprint(FD_FILE, "setup files\n");
640 if (td->o.read_iolog_file)
644 * if ioengine defines a setup() method, it's responsible for
645 * opening the files and setting f->real_file_size to indicate
646 * the valid range for that file.
648 if (td->io_ops->setup)
649 err = td->io_ops->setup(td);
651 err = get_file_sizes(td);
657 * check sizes. if the files/devices do not exist and the size
658 * isn't passed to fio, abort.
661 for_each_file(td, f, i) {
662 if (f->real_file_size == -1ULL)
665 total_size += f->real_file_size;
668 if (td->o.fill_device)
669 td->fill_device_size = get_fs_free_counts(td);
672 * device/file sizes are zero and no size given, punt
674 if ((!total_size || total_size == -1ULL) && !td->o.size &&
675 !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
676 log_err("%s: you need to specify size=\n", td->o.name);
677 td_verror(td, EINVAL, "total_file_size");
682 * now file sizes are known, so we can set ->io_size. if size= is
683 * not given, ->io_size is just equal to ->real_file_size. if size
684 * is given, ->io_size is size / nr_files.
686 extend_size = total_size = 0;
688 for_each_file(td, f, i) {
689 f->file_offset = td->o.start_offset;
691 if (!td->o.file_size_low) {
693 * no file size range given, file size is equal to
694 * total size divided by number of files. if that is
695 * zero, set it to the real file size.
697 f->io_size = td->o.size / td->o.nr_files;
699 f->io_size = f->real_file_size - f->file_offset;
700 } else if (f->real_file_size < td->o.file_size_low ||
701 f->real_file_size > td->o.file_size_high) {
702 if (f->file_offset > td->o.file_size_low)
705 * file size given. if it's fixed, use that. if it's a
706 * range, generate a random size in-between.
708 if (td->o.file_size_low == td->o.file_size_high) {
709 f->io_size = td->o.file_size_low
712 f->io_size = get_rand_file_size(td)
716 f->io_size = f->real_file_size - f->file_offset;
718 if (f->io_size == -1ULL)
721 total_size += f->io_size;
723 if (f->filetype == FIO_TYPE_FILE &&
724 (f->io_size + f->file_offset) > f->real_file_size &&
725 !(td->io_ops->flags & FIO_DISKLESSIO)) {
726 if (!td->o.create_on_open) {
728 extend_size += (f->io_size + f->file_offset);
730 f->real_file_size = f->io_size + f->file_offset;
731 fio_file_set_extend(f);
735 if (!td->o.size || td->o.size > total_size)
736 td->o.size = total_size;
739 * See if we need to extend some files
744 log_info("%s: Laying out IO file(s) (%u file(s) /"
745 " %lluMB)\n", td->o.name, need_extend,
748 for_each_file(td, f, i) {
749 unsigned long long old_len = -1ULL, extend_len = -1ULL;
751 if (!fio_file_extend(f))
754 assert(f->filetype == FIO_TYPE_FILE);
755 fio_file_clear_extend(f);
756 if (!td->o.fill_device) {
757 old_len = f->real_file_size;
758 extend_len = f->io_size + f->file_offset -
761 f->real_file_size = (f->io_size + f->file_offset);
762 err = extend_file(td, f);
766 err = __file_invalidate_cache(td, f, old_len,
779 if (!td->o.zone_size)
780 td->o.zone_size = td->o.size;
783 * iolog already set the total io size, if we read back
786 if (!td->o.read_iolog_file)
787 td->total_io_size = td->o.size * td->o.loops;
790 log_err("%s: you need to specify valid offset=\n", td->o.name);
794 int pre_read_files(struct thread_data *td)
799 dprint(FD_FILE, "pre_read files\n");
801 for_each_file(td, f, i) {
802 pre_read_file(td, f);
808 int init_random_map(struct thread_data *td)
810 unsigned long long blocks, num_maps;
814 if (td->o.norandommap || !td_random(td))
817 for_each_file(td, f, i) {
818 blocks = (f->real_file_size + td->o.rw_min_bs - 1) /
819 (unsigned long long) td->o.rw_min_bs;
820 num_maps = (blocks + BLOCKS_PER_MAP - 1) /
821 (unsigned long long) BLOCKS_PER_MAP;
822 f->file_map = smalloc(num_maps * sizeof(unsigned long));
824 f->num_maps = num_maps;
827 if (!td->o.softrandommap) {
828 log_err("fio: failed allocating random map. If running"
829 " a large number of jobs, try the 'norandommap'"
830 " option or set 'softrandommap'. Or give"
831 " a larger --alloc-size to fio.\n");
835 log_info("fio: file %s failed allocating random map. Running "
836 "job without.\n", f->file_name);
843 void close_files(struct thread_data *td)
848 for_each_file(td, f, i) {
849 if (fio_file_open(f))
850 td_io_close_file(td, f);
854 void close_and_free_files(struct thread_data *td)
859 dprint(FD_FILE, "close files\n");
861 for_each_file(td, f, i) {
862 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
863 dprint(FD_FILE, "free unlink %s\n", f->file_name);
864 unlink(f->file_name);
867 if (fio_file_open(f))
868 td_io_close_file(td, f);
879 td->o.filename = NULL;
886 static void get_file_type(struct fio_file *f)
890 if (!strcmp(f->file_name, "-"))
891 f->filetype = FIO_TYPE_PIPE;
893 f->filetype = FIO_TYPE_FILE;
895 if (!stat(f->file_name, &sb)) {
896 /* \\.\ is the device namespace in Windows, where every file is
898 if (S_ISBLK(sb.st_mode) || strncmp(f->file_name, "\\\\.\\", 4) == 0)
899 f->filetype = FIO_TYPE_BD;
900 else if (S_ISCHR(sb.st_mode))
901 f->filetype = FIO_TYPE_CHAR;
902 else if (S_ISFIFO(sb.st_mode))
903 f->filetype = FIO_TYPE_PIPE;
907 int add_file(struct thread_data *td, const char *fname)
909 int cur_files = td->files_index;
910 char file_name[PATH_MAX];
914 dprint(FD_FILE, "add file %s\n", fname);
916 f = smalloc(sizeof(*f));
918 log_err("fio: smalloc OOM\n");
925 if (td->files_size <= td->files_index) {
926 int new_size = td->o.nr_files + 1;
928 dprint(FD_FILE, "resize file array to %d files\n", new_size);
930 td->files = realloc(td->files, new_size * sizeof(f));
931 td->files_size = new_size;
933 td->files[cur_files] = f;
936 * init function, io engine may not be loaded yet
938 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
939 f->real_file_size = -1ULL;
942 len = sprintf(file_name, "%s/", td->o.directory);
944 sprintf(file_name + len, "%s", fname);
945 f->file_name = smalloc_strdup(file_name);
947 log_err("fio: smalloc OOM\n");
953 switch (td->o.file_lock_mode) {
956 case FILE_LOCK_READWRITE:
957 f->lock = fio_mutex_rw_init();
959 case FILE_LOCK_EXCLUSIVE:
960 f->lock = fio_mutex_init(1);
963 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
968 if (f->filetype == FIO_TYPE_FILE)
969 td->nr_normal_files++;
971 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
977 int add_file_exclusive(struct thread_data *td, const char *fname)
982 for_each_file(td, f, i) {
983 if (!strcmp(f->file_name, fname))
987 return add_file(td, fname);
990 void get_file(struct fio_file *f)
992 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
993 assert(fio_file_open(f));
997 int put_file(struct thread_data *td, struct fio_file *f)
999 int f_ret = 0, ret = 0;
1001 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
1003 if (!fio_file_open(f)) {
1004 assert(f->fd == -1);
1008 assert(f->references);
1009 if (--f->references)
1012 if (should_fsync(td) && td->o.fsync_on_close)
1013 f_ret = fsync(f->fd);
1015 if (td->io_ops->close_file)
1016 ret = td->io_ops->close_file(td, f);
1021 td->nr_open_files--;
1022 fio_file_clear_open(f);
1023 assert(f->fd == -1);
1027 void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
1029 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1032 if (f->lock_owner == td && f->lock_batch--)
1035 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1036 if (ddir == DDIR_READ)
1037 fio_mutex_down_read(f->lock);
1039 fio_mutex_down_write(f->lock);
1040 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1041 fio_mutex_down(f->lock);
1044 f->lock_batch = td->o.lockfile_batch;
1045 f->lock_ddir = ddir;
1048 void unlock_file(struct thread_data *td, struct fio_file *f)
1050 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1055 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1056 const int is_read = f->lock_ddir == DDIR_READ;
1057 int val = fio_mutex_getval(f->lock);
1059 if ((is_read && val == 1) || (!is_read && val == -1))
1060 f->lock_owner = NULL;
1063 fio_mutex_up_read(f->lock);
1065 fio_mutex_up_write(f->lock);
1066 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
1067 int val = fio_mutex_getval(f->lock);
1070 f->lock_owner = NULL;
1072 fio_mutex_up(f->lock);
1076 void unlock_file_all(struct thread_data *td, struct fio_file *f)
1078 if (f->lock_owner != td)
1085 static int recurse_dir(struct thread_data *td, const char *dirname)
1091 D = opendir(dirname);
1093 char buf[FIO_VERROR_SIZE];
1095 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
1096 td_verror(td, errno, buf);
1100 while ((dir = readdir(D)) != NULL) {
1101 char full_path[PATH_MAX];
1104 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1107 sprintf(full_path, "%s/%s", dirname, dir->d_name);
1109 if (lstat(full_path, &sb) == -1) {
1110 if (errno != ENOENT) {
1111 td_verror(td, errno, "stat");
1116 if (S_ISREG(sb.st_mode)) {
1117 add_file(td, full_path);
1121 if (!S_ISDIR(sb.st_mode))
1124 ret = recurse_dir(td, full_path);
1133 int add_dir_files(struct thread_data *td, const char *path)
1135 int ret = recurse_dir(td, path);
1138 log_info("fio: opendir added %d files\n", td->o.nr_files);
1143 void dup_files(struct thread_data *td, struct thread_data *org)
1148 dprint(FD_FILE, "dup files: %d\n", org->files_index);
1153 td->files = malloc(org->files_index * sizeof(f));
1155 for_each_file(org, f, i) {
1156 struct fio_file *__f;
1158 __f = smalloc(sizeof(*__f));
1160 log_err("fio: smalloc OOM\n");
1164 fio_file_reset(__f);
1167 __f->file_name = smalloc_strdup(f->file_name);
1168 if (!__f->file_name) {
1169 log_err("fio: smalloc OOM\n");
1173 __f->filetype = f->filetype;
1181 * Returns the index that matches the filename, or -1 if not there
1183 int get_fileno(struct thread_data *td, const char *fname)
1188 for_each_file(td, f, i)
1189 if (!strcmp(f->file_name, fname))
1196 * For log usage, where we add/open/close files automatically
1198 void free_release_files(struct thread_data *td)
1201 td->files_index = 0;
1202 td->nr_normal_files = 0;