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 r = os_random_long(&td->file_size_state);
214 sized = td->o.file_size_high - td->o.file_size_low;
215 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
216 ret += td->o.file_size_low;
217 ret -= (ret % td->o.rw_min_bs);
221 static int file_size(struct thread_data *td, struct fio_file *f)
225 if (stat(f->file_name, &st) == -1) {
226 td_verror(td, errno, "fstat");
230 f->real_file_size = st.st_size;
234 static int bdev_size(struct thread_data *td, struct fio_file *f)
236 unsigned long long bytes = 0;
239 if (td->io_ops->open_file(td, f)) {
240 log_err("fio: failed opening blockdev %s for size check\n",
245 r = blockdev_size(f, &bytes);
247 td_verror(td, r, "blockdev_size");
248 printf("fd is %d\n", f->fd);
253 log_err("%s: zero sized block device?\n", f->file_name);
257 f->real_file_size = bytes;
258 td->io_ops->close_file(td, f);
261 td->io_ops->close_file(td, f);
265 static int char_size(struct thread_data *td, struct fio_file *f)
267 #ifdef FIO_HAVE_CHARDEV_SIZE
268 unsigned long long bytes = 0;
271 if (td->io_ops->open_file(td, f)) {
272 log_err("fio: failed opening blockdev %s for size check\n",
277 r = chardev_size(f, &bytes);
279 td_verror(td, r, "chardev_size");
284 log_err("%s: zero sized char device?\n", f->file_name);
288 f->real_file_size = bytes;
289 td->io_ops->close_file(td, f);
292 td->io_ops->close_file(td, f);
295 f->real_file_size = -1ULL;
300 static int get_file_size(struct thread_data *td, struct fio_file *f)
304 if (fio_file_size_known(f))
307 if (f->filetype == FIO_TYPE_FILE)
308 ret = file_size(td, f);
309 else if (f->filetype == FIO_TYPE_BD)
310 ret = bdev_size(td, f);
311 else if (f->filetype == FIO_TYPE_CHAR)
312 ret = char_size(td, f);
314 f->real_file_size = -1;
319 if (f->file_offset > f->real_file_size) {
320 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
321 f->file_offset, f->real_file_size);
325 fio_file_set_size_known(f);
329 static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
330 unsigned long long off,
331 unsigned long long len)
338 off = f->file_offset;
340 if (len == -1ULL || off == -1ULL)
343 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
347 * FIXME: add blockdev flushing too
350 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
352 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
354 } else if (f->filetype == FIO_TYPE_FILE) {
355 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
356 } else if (f->filetype == FIO_TYPE_BD) {
357 ret = blockdev_invalidate_cache(f);
358 if (ret < 0 && errno == EACCES && geteuid()) {
360 log_err("fio: only root may flush block "
361 "devices. Cache flush bypassed!\n");
366 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
370 td_verror(td, errno, "invalidate_cache");
372 } else if (ret > 0) {
373 td_verror(td, ret, "invalidate_cache");
381 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
383 if (!fio_file_open(f))
386 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
389 int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
393 dprint(FD_FILE, "fd close %s\n", f->file_name);
397 if (close(f->fd) < 0)
404 static int file_lookup_open(struct fio_file *f, int flags)
406 struct fio_file *__f;
409 __f = lookup_file_hash(f->file_name);
411 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
413 * racy, need the __f->lock locked
416 f->lock_owner = __f->lock_owner;
417 f->lock_batch = __f->lock_batch;
418 f->lock_ddir = __f->lock_ddir;
421 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
425 f->fd = open(f->file_name, flags, 0600);
429 int generic_open_file(struct thread_data *td, struct fio_file *f)
435 dprint(FD_FILE, "fd open %s\n", f->file_name);
437 if (!strcmp(f->file_name, "-")) {
439 log_err("fio: can't read/write to stdin/out\n");
445 * move output logging to stderr, if we are writing to stdout
452 flags |= OS_O_DIRECT;
455 if (f->filetype != FIO_TYPE_FILE)
456 flags |= FIO_O_NOATIME;
457 if (td->o.create_on_open)
465 if (f->filetype == FIO_TYPE_FILE)
469 f->fd = dup(STDOUT_FILENO);
471 from_hash = file_lookup_open(f, flags);
473 if (f->filetype == FIO_TYPE_CHAR && !read_only)
479 f->fd = dup(STDIN_FILENO);
481 from_hash = file_lookup_open(f, flags);
485 char buf[FIO_VERROR_SIZE];
488 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
489 flags &= ~FIO_O_NOATIME;
493 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
495 td_verror(td, __e, buf);
498 if (!from_hash && f->fd != -1) {
499 if (add_file_hash(f)) {
503 * OK to ignore, we haven't done anything with it
505 ret = generic_close_file(td, f);
513 int generic_get_file_size(struct thread_data *td, struct fio_file *f)
515 return get_file_size(td, f);
519 * open/close all files, so that ->real_file_size gets set
521 static int get_file_sizes(struct thread_data *td)
527 for_each_file(td, f, i) {
528 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
531 if (td_io_get_file_size(td, f)) {
532 if (td->error != ENOENT) {
533 log_err("%s\n", td->verror);
539 if (f->real_file_size == -1ULL && td->o.size)
540 f->real_file_size = td->o.size / td->o.nr_files;
547 struct flist_head list;
554 * Get free number of bytes for each file on each unique mount.
556 static unsigned long long get_fs_free_counts(struct thread_data *td)
558 struct flist_head *n, *tmp;
559 unsigned long long ret = 0;
560 struct fio_mount *fm;
565 for_each_file(td, f, i) {
569 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
570 if (f->real_file_size != -1ULL)
571 ret += f->real_file_size;
573 } else if (f->filetype != FIO_TYPE_FILE)
576 strcpy(buf, f->file_name);
578 if (stat(buf, &sb) < 0) {
582 if (stat(buf, &sb) < 0)
587 flist_for_each(n, &list) {
588 fm = flist_entry(n, struct fio_mount, list);
589 if (fm->key == sb.st_dev)
598 fm = malloc(sizeof(*fm));
599 strcpy(fm->__base, buf);
600 fm->base = basename(fm->__base);
602 flist_add(&fm->list, &list);
605 flist_for_each_safe(n, tmp, &list) {
606 unsigned long long sz;
608 fm = flist_entry(n, struct fio_mount, list);
609 flist_del(&fm->list);
611 sz = get_fs_size(fm->base);
612 if (sz && sz != -1ULL)
622 * Open the files and setup files sizes, creating files if necessary.
624 int setup_files(struct thread_data *td)
626 unsigned long long total_size, extend_size;
629 int err = 0, need_extend;
631 dprint(FD_FILE, "setup files\n");
633 if (td->o.read_iolog_file)
637 * if ioengine defines a setup() method, it's responsible for
638 * opening the files and setting f->real_file_size to indicate
639 * the valid range for that file.
641 if (td->io_ops->setup)
642 err = td->io_ops->setup(td);
644 err = get_file_sizes(td);
650 * check sizes. if the files/devices do not exist and the size
651 * isn't passed to fio, abort.
654 for_each_file(td, f, i) {
655 if (f->real_file_size == -1ULL)
658 total_size += f->real_file_size;
661 if (td->o.fill_device)
662 td->fill_device_size = get_fs_free_counts(td);
665 * device/file sizes are zero and no size given, punt
667 if ((!total_size || total_size == -1ULL) && !td->o.size &&
668 !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
669 log_err("%s: you need to specify size=\n", td->o.name);
670 td_verror(td, EINVAL, "total_file_size");
675 * now file sizes are known, so we can set ->io_size. if size= is
676 * not given, ->io_size is just equal to ->real_file_size. if size
677 * is given, ->io_size is size / nr_files.
679 extend_size = total_size = 0;
681 for_each_file(td, f, i) {
682 f->file_offset = td->o.start_offset;
684 if (!td->o.file_size_low) {
686 * no file size range given, file size is equal to
687 * total size divided by number of files. if that is
688 * zero, set it to the real file size.
690 f->io_size = td->o.size / td->o.nr_files;
692 f->io_size = f->real_file_size - f->file_offset;
693 } else if (f->real_file_size < td->o.file_size_low ||
694 f->real_file_size > td->o.file_size_high) {
695 if (f->file_offset > td->o.file_size_low)
698 * file size given. if it's fixed, use that. if it's a
699 * range, generate a random size in-between.
701 if (td->o.file_size_low == td->o.file_size_high) {
702 f->io_size = td->o.file_size_low
705 f->io_size = get_rand_file_size(td)
709 f->io_size = f->real_file_size - f->file_offset;
711 if (f->io_size == -1ULL)
714 total_size += f->io_size;
716 if (f->filetype == FIO_TYPE_FILE &&
717 (f->io_size + f->file_offset) > f->real_file_size &&
718 !(td->io_ops->flags & FIO_DISKLESSIO)) {
719 if (!td->o.create_on_open) {
721 extend_size += (f->io_size + f->file_offset);
723 f->real_file_size = f->io_size + f->file_offset;
724 fio_file_set_extend(f);
728 if (!td->o.size || td->o.size > total_size)
729 td->o.size = total_size;
732 * See if we need to extend some files
737 log_info("%s: Laying out IO file(s) (%u file(s) /"
738 " %lluMB)\n", td->o.name, need_extend,
741 for_each_file(td, f, i) {
742 unsigned long long old_len = -1ULL, extend_len = -1ULL;
744 if (!fio_file_extend(f))
747 assert(f->filetype == FIO_TYPE_FILE);
748 fio_file_clear_extend(f);
749 if (!td->o.fill_device) {
750 old_len = f->real_file_size;
751 extend_len = f->io_size + f->file_offset -
754 f->real_file_size = (f->io_size + f->file_offset);
755 err = extend_file(td, f);
759 err = __file_invalidate_cache(td, f, old_len,
772 if (!td->o.zone_size)
773 td->o.zone_size = td->o.size;
776 * iolog already set the total io size, if we read back
779 if (!td->o.read_iolog_file)
780 td->total_io_size = td->o.size * td->o.loops;
783 log_err("%s: you need to specify valid offset=\n", td->o.name);
787 int pre_read_files(struct thread_data *td)
792 dprint(FD_FILE, "pre_read files\n");
794 for_each_file(td, f, i) {
795 pre_read_file(td, f);
801 int init_random_map(struct thread_data *td)
803 unsigned long long blocks, num_maps;
807 if (td->o.norandommap || !td_random(td))
810 for_each_file(td, f, i) {
811 blocks = (f->real_file_size + td->o.rw_min_bs - 1) /
812 (unsigned long long) td->o.rw_min_bs;
813 num_maps = (blocks + BLOCKS_PER_MAP - 1) /
814 (unsigned long long) BLOCKS_PER_MAP;
815 f->file_map = smalloc(num_maps * sizeof(int));
817 f->num_maps = num_maps;
820 if (!td->o.softrandommap) {
821 log_err("fio: failed allocating random map. If running"
822 " a large number of jobs, try the 'norandommap'"
823 " option or set 'softrandommap'. Or give"
824 " a larger --alloc-size to fio.\n");
828 log_info("fio: file %s failed allocating random map. Running "
829 "job without.\n", f->file_name);
836 void close_files(struct thread_data *td)
841 for_each_file(td, f, i) {
842 if (fio_file_open(f))
843 td_io_close_file(td, f);
847 void close_and_free_files(struct thread_data *td)
852 dprint(FD_FILE, "close files\n");
854 for_each_file(td, f, i) {
855 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
856 dprint(FD_FILE, "free unlink %s\n", f->file_name);
857 unlink(f->file_name);
860 if (fio_file_open(f))
861 td_io_close_file(td, f);
872 td->o.filename = NULL;
879 static void get_file_type(struct fio_file *f)
883 if (!strcmp(f->file_name, "-"))
884 f->filetype = FIO_TYPE_PIPE;
886 f->filetype = FIO_TYPE_FILE;
888 if (!stat(f->file_name, &sb)) {
889 /* \\.\ is the device namespace in Windows, where every file is
891 if (S_ISBLK(sb.st_mode) || strncmp(f->file_name, "\\\\.\\", 4) == 0)
892 f->filetype = FIO_TYPE_BD;
893 else if (S_ISCHR(sb.st_mode))
894 f->filetype = FIO_TYPE_CHAR;
895 else if (S_ISFIFO(sb.st_mode))
896 f->filetype = FIO_TYPE_PIPE;
900 int add_file(struct thread_data *td, const char *fname)
902 int cur_files = td->files_index;
903 char file_name[PATH_MAX];
907 dprint(FD_FILE, "add file %s\n", fname);
909 f = smalloc(sizeof(*f));
911 log_err("fio: smalloc OOM\n");
918 if (td->files_size <= td->files_index) {
919 int new_size = td->o.nr_files + 1;
921 dprint(FD_FILE, "resize file array to %d files\n", new_size);
923 td->files = realloc(td->files, new_size * sizeof(f));
924 td->files_size = new_size;
926 td->files[cur_files] = f;
929 * init function, io engine may not be loaded yet
931 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
932 f->real_file_size = -1ULL;
935 len = sprintf(file_name, "%s/", td->o.directory);
937 sprintf(file_name + len, "%s", fname);
938 f->file_name = smalloc_strdup(file_name);
940 log_err("fio: smalloc OOM\n");
946 switch (td->o.file_lock_mode) {
949 case FILE_LOCK_READWRITE:
950 f->lock = fio_mutex_rw_init();
952 case FILE_LOCK_EXCLUSIVE:
953 f->lock = fio_mutex_init(1);
956 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
961 if (f->filetype == FIO_TYPE_FILE)
962 td->nr_normal_files++;
964 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
970 int add_file_exclusive(struct thread_data *td, const char *fname)
975 for_each_file(td, f, i) {
976 if (!strcmp(f->file_name, fname))
980 return add_file(td, fname);
983 void get_file(struct fio_file *f)
985 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
986 assert(fio_file_open(f));
990 int put_file(struct thread_data *td, struct fio_file *f)
992 int f_ret = 0, ret = 0;
994 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
996 if (!fio_file_open(f)) {
1001 assert(f->references);
1002 if (--f->references)
1005 if (should_fsync(td) && td->o.fsync_on_close)
1006 f_ret = fsync(f->fd);
1008 if (td->io_ops->close_file)
1009 ret = td->io_ops->close_file(td, f);
1014 td->nr_open_files--;
1015 fio_file_clear_open(f);
1016 assert(f->fd == -1);
1020 void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
1022 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1025 if (f->lock_owner == td && f->lock_batch--)
1028 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1029 if (ddir == DDIR_READ)
1030 fio_mutex_down_read(f->lock);
1032 fio_mutex_down_write(f->lock);
1033 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1034 fio_mutex_down(f->lock);
1037 f->lock_batch = td->o.lockfile_batch;
1038 f->lock_ddir = ddir;
1041 void unlock_file(struct thread_data *td, struct fio_file *f)
1043 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1048 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1049 const int is_read = f->lock_ddir == DDIR_READ;
1050 int val = fio_mutex_getval(f->lock);
1052 if ((is_read && val == 1) || (!is_read && val == -1))
1053 f->lock_owner = NULL;
1056 fio_mutex_up_read(f->lock);
1058 fio_mutex_up_write(f->lock);
1059 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
1060 int val = fio_mutex_getval(f->lock);
1063 f->lock_owner = NULL;
1065 fio_mutex_up(f->lock);
1069 void unlock_file_all(struct thread_data *td, struct fio_file *f)
1071 if (f->lock_owner != td)
1078 static int recurse_dir(struct thread_data *td, const char *dirname)
1084 D = opendir(dirname);
1086 char buf[FIO_VERROR_SIZE];
1088 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
1089 td_verror(td, errno, buf);
1093 while ((dir = readdir(D)) != NULL) {
1094 char full_path[PATH_MAX];
1097 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1100 sprintf(full_path, "%s/%s", dirname, dir->d_name);
1102 if (lstat(full_path, &sb) == -1) {
1103 if (errno != ENOENT) {
1104 td_verror(td, errno, "stat");
1109 if (S_ISREG(sb.st_mode)) {
1110 add_file(td, full_path);
1114 if (!S_ISDIR(sb.st_mode))
1117 ret = recurse_dir(td, full_path);
1126 int add_dir_files(struct thread_data *td, const char *path)
1128 int ret = recurse_dir(td, path);
1131 log_info("fio: opendir added %d files\n", td->o.nr_files);
1136 void dup_files(struct thread_data *td, struct thread_data *org)
1141 dprint(FD_FILE, "dup files: %d\n", org->files_index);
1146 td->files = malloc(org->files_index * sizeof(f));
1148 for_each_file(org, f, i) {
1149 struct fio_file *__f;
1151 __f = smalloc(sizeof(*__f));
1153 log_err("fio: smalloc OOM\n");
1157 fio_file_reset(__f);
1160 __f->file_name = smalloc_strdup(f->file_name);
1161 if (!__f->file_name) {
1162 log_err("fio: smalloc OOM\n");
1166 __f->filetype = f->filetype;
1174 * Returns the index that matches the filename, or -1 if not there
1176 int get_fileno(struct thread_data *td, const char *fname)
1181 for_each_file(td, f, i)
1182 if (!strcmp(f->file_name, fname))
1189 * For log usage, where we add/open/close files automatically
1191 void free_release_files(struct thread_data *td)
1194 td->files_index = 0;
1195 td->nr_normal_files = 0;