17 static inline void clear_error(struct thread_data *td)
24 * Leaves f->fd open on success, caller must close
26 static int extend_file(struct thread_data *td, struct fio_file *f)
28 int r, new_layout = 0, unlink_file = 0, flags;
29 unsigned long long left;
34 log_err("fio: refusing extend of file due to read-only\n");
39 * check if we need to lay the file out complete again. fio
40 * does that for operations involving reads, or for writes
41 * where overwrite is set
43 if (td_read(td) || (td_write(td) && td->o.overwrite) ||
44 (td_write(td) && td->io_ops->flags & FIO_NOEXTEND))
46 if (td_write(td) && !td->o.overwrite)
49 if (unlink_file || new_layout) {
50 dprint(FD_FILE, "layout unlink %s\n", f->file_name);
51 if ((unlink(f->file_name) < 0) && (errno != ENOENT)) {
52 td_verror(td, errno, "unlink");
57 flags = O_WRONLY | O_CREAT;
61 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
62 f->fd = open(f->file_name, flags, 0644);
64 td_verror(td, errno, "open");
68 #ifdef FIO_HAVE_FALLOCATE
69 if (td->o.fallocate && !td->o.fill_device) {
70 dprint(FD_FILE, "fallocate file %s size %llu\n", f->file_name,
73 r = posix_fallocate(f->fd, 0, f->real_file_size);
75 log_err("fio: posix_fallocate fails: %s\n",
85 * The size will be -1ULL when fill_device is used, so don't truncate
86 * or fallocate this file, just write it
88 if (!td->o.fill_device) {
89 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
91 if (ftruncate(f->fd, f->real_file_size) == -1) {
92 td_verror(td, errno, "ftruncate");
97 b = malloc(td->o.max_bs[DDIR_WRITE]);
98 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
100 left = f->real_file_size;
101 while (left && !td->terminate) {
102 bs = td->o.max_bs[DDIR_WRITE];
106 r = write(f->fd, b, bs);
116 if (td->o.fill_device)
118 log_info("fio: ENOSPC on laying out "
122 td_verror(td, errno, "write");
124 td_verror(td, EIO, "write");
131 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
132 unlink(f->file_name);
133 } else if (td->o.create_fsync) {
134 if (fsync(f->fd) < 0) {
135 td_verror(td, errno, "fsync");
139 if (td->o.fill_device && !td_write(td)) {
140 fio_file_clear_size_known(f);
141 if (td_io_get_file_size(td, f))
143 if (f->io_size > f->real_file_size)
144 f->io_size = f->real_file_size;
156 static int pre_read_file(struct thread_data *td, struct fio_file *f)
158 int r, did_open = 0, old_runstate;
159 unsigned long long left;
163 if (td->io_ops->flags & FIO_PIPEIO)
166 if (!fio_file_open(f)) {
167 if (td->io_ops->open_file(td, f)) {
168 log_err("fio: cannot pre-read, failed to open file\n");
174 old_runstate = td->runstate;
175 td_set_runstate(td, TD_PRE_READING);
177 bs = td->o.max_bs[DDIR_READ];
181 lseek(f->fd, f->file_offset, SEEK_SET);
184 while (left && !td->terminate) {
188 r = read(f->fd, b, bs);
194 td_verror(td, EIO, "pre_read");
199 td_set_runstate(td, old_runstate);
202 td->io_ops->close_file(td, f);
207 static unsigned long long get_rand_file_size(struct thread_data *td)
209 unsigned long long ret, sized;
212 r = os_random_long(&td->file_size_state);
213 sized = td->o.file_size_high - td->o.file_size_low;
214 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
215 ret += td->o.file_size_low;
216 ret -= (ret % td->o.rw_min_bs);
220 static int file_size(struct thread_data *td, struct fio_file *f)
224 if (stat(f->file_name, &st) == -1) {
225 td_verror(td, errno, "fstat");
229 f->real_file_size = st.st_size;
233 static int bdev_size(struct thread_data *td, struct fio_file *f)
235 unsigned long long bytes;
238 if (td->io_ops->open_file(td, f)) {
239 log_err("fio: failed opening blockdev %s for size check\n",
244 r = blockdev_size(f->fd, &bytes);
246 td_verror(td, r, "blockdev_size");
251 log_err("%s: zero sized block device?\n", f->file_name);
255 f->real_file_size = bytes;
256 td->io_ops->close_file(td, f);
259 td->io_ops->close_file(td, f);
263 static int char_size(struct thread_data *td, struct fio_file *f)
265 #ifdef FIO_HAVE_CHARDEV_SIZE
266 unsigned long long bytes;
269 if (td->io_ops->open_file(td, f)) {
270 log_err("fio: failed opening blockdev %s for size check\n",
275 r = chardev_size(f->fd, &bytes);
277 td_verror(td, r, "chardev_size");
282 log_err("%s: zero sized char device?\n", f->file_name);
286 f->real_file_size = bytes;
287 td->io_ops->close_file(td, f);
290 td->io_ops->close_file(td, f);
293 f->real_file_size = -1ULL;
298 static int get_file_size(struct thread_data *td, struct fio_file *f)
302 if (fio_file_size_known(f))
305 if (f->filetype == FIO_TYPE_FILE)
306 ret = file_size(td, f);
307 else if (f->filetype == FIO_TYPE_BD)
308 ret = bdev_size(td, f);
309 else if (f->filetype == FIO_TYPE_CHAR)
310 ret = char_size(td, f);
312 f->real_file_size = -1;
317 if (f->file_offset > f->real_file_size) {
318 log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name,
319 f->file_offset, f->real_file_size);
323 fio_file_set_size_known(f);
327 static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
328 unsigned long long off,
329 unsigned long long len)
336 off = f->file_offset;
338 if (len == -1ULL || off == -1ULL)
341 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
345 * FIXME: add blockdev flushing too
348 ret = madvise(f->mmap_ptr, f->mmap_sz, MADV_DONTNEED);
350 (void) madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
352 } else if (f->filetype == FIO_TYPE_FILE) {
353 ret = fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
354 } else if (f->filetype == FIO_TYPE_BD) {
355 ret = blockdev_invalidate_cache(f->fd);
356 if (ret < 0 && errno == EACCES && geteuid()) {
358 log_err("fio: only root may flush block "
359 "devices. Cache flush bypassed!\n");
364 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
368 td_verror(td, errno, "invalidate_cache");
370 } else if (ret > 0) {
371 td_verror(td, ret, "invalidate_cache");
379 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
381 if (!fio_file_open(f))
384 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
387 int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
391 dprint(FD_FILE, "fd close %s\n", f->file_name);
395 if (close(f->fd) < 0)
402 static int file_lookup_open(struct fio_file *f, int flags)
404 struct fio_file *__f;
407 __f = lookup_file_hash(f->file_name);
409 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
411 * racy, need the __f->lock locked
414 f->lock_owner = __f->lock_owner;
415 f->lock_batch = __f->lock_batch;
416 f->lock_ddir = __f->lock_ddir;
419 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
423 f->fd = open(f->file_name, flags, 0600);
427 int generic_open_file(struct thread_data *td, struct fio_file *f)
433 dprint(FD_FILE, "fd open %s\n", f->file_name);
435 if (!strcmp(f->file_name, "-")) {
437 log_err("fio: can't read/write to stdin/out\n");
443 * move output logging to stderr, if we are writing to stdout
450 flags |= OS_O_DIRECT;
453 if (f->filetype != FIO_TYPE_FILE)
454 flags |= FIO_O_NOATIME;
455 if (td->o.create_on_open)
463 if (f->filetype == FIO_TYPE_FILE)
467 f->fd = dup(STDOUT_FILENO);
469 from_hash = file_lookup_open(f, flags);
471 if (f->filetype == FIO_TYPE_CHAR && !read_only)
477 f->fd = dup(STDIN_FILENO);
479 from_hash = file_lookup_open(f, flags);
483 char buf[FIO_VERROR_SIZE];
486 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
487 flags &= ~FIO_O_NOATIME;
491 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
493 td_verror(td, __e, buf);
496 if (!from_hash && f->fd != -1) {
497 if (add_file_hash(f)) {
501 * OK to ignore, we haven't done anything with it
503 ret = generic_close_file(td, f);
511 int generic_get_file_size(struct thread_data *td, struct fio_file *f)
513 return get_file_size(td, f);
517 * open/close all files, so that ->real_file_size gets set
519 static int get_file_sizes(struct thread_data *td)
525 for_each_file(td, f, i) {
526 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
529 if (td_io_get_file_size(td, f)) {
530 if (td->error != ENOENT) {
531 log_err("%s\n", td->verror);
537 if (f->real_file_size == -1ULL && td->o.size)
538 f->real_file_size = td->o.size / td->o.nr_files;
545 struct flist_head list;
552 * Get free number of bytes for each file on each unique mount.
554 static unsigned long long get_fs_free_counts(struct thread_data *td)
556 struct flist_head *n, *tmp;
557 unsigned long long ret = 0;
558 struct fio_mount *fm;
563 for_each_file(td, f, i) {
567 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
568 if (f->real_file_size != -1ULL)
569 ret += f->real_file_size;
571 } else if (f->filetype != FIO_TYPE_FILE)
574 strcpy(buf, f->file_name);
576 if (stat(buf, &sb) < 0) {
580 if (stat(buf, &sb) < 0)
585 flist_for_each(n, &list) {
586 fm = flist_entry(n, struct fio_mount, list);
587 if (fm->key == sb.st_dev)
596 fm = malloc(sizeof(*fm));
597 strcpy(fm->__base, buf);
598 fm->base = basename(fm->__base);
600 flist_add(&fm->list, &list);
603 flist_for_each_safe(n, tmp, &list) {
604 unsigned long long sz;
606 fm = flist_entry(n, struct fio_mount, list);
607 flist_del(&fm->list);
609 sz = get_fs_size(fm->base);
610 if (sz && sz != -1ULL)
620 * Open the files and setup files sizes, creating files if necessary.
622 int setup_files(struct thread_data *td)
624 unsigned long long total_size, extend_size;
627 int err = 0, need_extend;
629 dprint(FD_FILE, "setup files\n");
631 if (td->o.read_iolog_file)
635 * if ioengine defines a setup() method, it's responsible for
636 * opening the files and setting f->real_file_size to indicate
637 * the valid range for that file.
639 if (td->io_ops->setup)
640 err = td->io_ops->setup(td);
642 err = get_file_sizes(td);
648 * check sizes. if the files/devices do not exist and the size
649 * isn't passed to fio, abort.
652 for_each_file(td, f, i) {
653 if (f->real_file_size == -1ULL)
656 total_size += f->real_file_size;
659 if (td->o.fill_device)
660 td->fill_device_size = get_fs_free_counts(td);
663 * device/file sizes are zero and no size given, punt
665 if ((!total_size || total_size == -1ULL) && !td->o.size &&
666 !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
667 log_err("%s: you need to specify size=\n", td->o.name);
668 td_verror(td, EINVAL, "total_file_size");
673 * now file sizes are known, so we can set ->io_size. if size= is
674 * not given, ->io_size is just equal to ->real_file_size. if size
675 * is given, ->io_size is size / nr_files.
677 extend_size = total_size = 0;
679 for_each_file(td, f, i) {
680 f->file_offset = td->o.start_offset;
682 if (!td->o.file_size_low) {
684 * no file size range given, file size is equal to
685 * total size divided by number of files. if that is
686 * zero, set it to the real file size.
688 f->io_size = td->o.size / td->o.nr_files;
690 f->io_size = f->real_file_size - f->file_offset;
691 } else if (f->real_file_size < td->o.file_size_low ||
692 f->real_file_size > td->o.file_size_high) {
693 if (f->file_offset > td->o.file_size_low)
696 * file size given. if it's fixed, use that. if it's a
697 * range, generate a random size in-between.
699 if (td->o.file_size_low == td->o.file_size_high) {
700 f->io_size = td->o.file_size_low
703 f->io_size = get_rand_file_size(td)
707 f->io_size = f->real_file_size - f->file_offset;
709 if (f->io_size == -1ULL)
712 total_size += f->io_size;
714 if (f->filetype == FIO_TYPE_FILE &&
715 (f->io_size + f->file_offset) > f->real_file_size &&
716 !(td->io_ops->flags & FIO_DISKLESSIO)) {
717 if (!td->o.create_on_open) {
719 extend_size += (f->io_size + f->file_offset);
721 f->real_file_size = f->io_size + f->file_offset;
722 fio_file_set_extend(f);
726 if (!td->o.size || td->o.size > total_size)
727 td->o.size = total_size;
730 * See if we need to extend some files
735 log_info("%s: Laying out IO file(s) (%u file(s) /"
736 " %LuMB)\n", td->o.name, need_extend,
739 for_each_file(td, f, i) {
740 unsigned long long old_len = -1ULL, extend_len = -1ULL;
742 if (!fio_file_extend(f))
745 assert(f->filetype == FIO_TYPE_FILE);
746 fio_file_clear_extend(f);
747 if (!td->o.fill_device) {
748 old_len = f->real_file_size;
749 extend_len = f->io_size + f->file_offset -
752 f->real_file_size = (f->io_size + f->file_offset);
753 err = extend_file(td, f);
757 err = __file_invalidate_cache(td, f, old_len,
770 if (!td->o.zone_size)
771 td->o.zone_size = td->o.size;
774 * iolog already set the total io size, if we read back
777 if (!td->o.read_iolog_file)
778 td->total_io_size = td->o.size * td->o.loops;
781 log_err("%s: you need to specify valid offset=\n", td->o.name);
785 int pre_read_files(struct thread_data *td)
790 dprint(FD_FILE, "pre_read files\n");
792 for_each_file(td, f, i) {
793 pre_read_file(td, f);
799 int init_random_map(struct thread_data *td)
801 unsigned long long blocks, num_maps;
805 if (td->o.norandommap || !td_random(td))
808 for_each_file(td, f, i) {
809 blocks = (f->real_file_size + td->o.rw_min_bs - 1) /
810 (unsigned long long) td->o.rw_min_bs;
811 num_maps = (blocks + BLOCKS_PER_MAP - 1) /
812 (unsigned long long) BLOCKS_PER_MAP;
813 f->file_map = smalloc(num_maps * sizeof(int));
815 f->num_maps = num_maps;
818 if (!td->o.softrandommap) {
819 log_err("fio: failed allocating random map. If running"
820 " a large number of jobs, try the 'norandommap'"
821 " option or set 'softrandommap'. Or give"
822 " a larger --alloc-size to fio.\n");
826 log_info("fio: file %s failed allocating random map. Running "
827 "job without.\n", f->file_name);
834 void close_files(struct thread_data *td)
839 for_each_file(td, f, i) {
840 if (fio_file_open(f))
841 td_io_close_file(td, f);
845 void close_and_free_files(struct thread_data *td)
850 dprint(FD_FILE, "close files\n");
852 for_each_file(td, f, i) {
853 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
854 dprint(FD_FILE, "free unlink %s\n", f->file_name);
855 unlink(f->file_name);
858 if (fio_file_open(f))
859 td_io_close_file(td, f);
870 td->o.filename = NULL;
877 static void get_file_type(struct fio_file *f)
881 if (!strcmp(f->file_name, "-"))
882 f->filetype = FIO_TYPE_PIPE;
884 f->filetype = FIO_TYPE_FILE;
886 if (!stat(f->file_name, &sb)) {
887 if (S_ISBLK(sb.st_mode))
888 f->filetype = FIO_TYPE_BD;
889 else if (S_ISCHR(sb.st_mode))
890 f->filetype = FIO_TYPE_CHAR;
891 else if (S_ISFIFO(sb.st_mode))
892 f->filetype = FIO_TYPE_PIPE;
896 int add_file(struct thread_data *td, const char *fname)
898 int cur_files = td->files_index;
899 char file_name[PATH_MAX];
903 dprint(FD_FILE, "add file %s\n", fname);
905 f = smalloc(sizeof(*f));
907 log_err("fio: smalloc OOM\n");
914 if (td->files_size <= td->files_index) {
915 int new_size = td->o.nr_files + 1;
917 dprint(FD_FILE, "resize file array to %d files\n", new_size);
919 td->files = realloc(td->files, new_size * sizeof(f));
920 td->files_size = new_size;
922 td->files[cur_files] = f;
925 * init function, io engine may not be loaded yet
927 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
928 f->real_file_size = -1ULL;
931 len = sprintf(file_name, "%s/", td->o.directory);
933 sprintf(file_name + len, "%s", fname);
934 f->file_name = smalloc_strdup(file_name);
936 log_err("fio: smalloc OOM\n");
942 switch (td->o.file_lock_mode) {
945 case FILE_LOCK_READWRITE:
946 f->lock = fio_mutex_rw_init();
948 case FILE_LOCK_EXCLUSIVE:
949 f->lock = fio_mutex_init(1);
952 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
957 if (f->filetype == FIO_TYPE_FILE)
958 td->nr_normal_files++;
960 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
966 int add_file_exclusive(struct thread_data *td, const char *fname)
971 for_each_file(td, f, i) {
972 if (!strcmp(f->file_name, fname))
976 return add_file(td, fname);
979 void get_file(struct fio_file *f)
981 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
982 assert(fio_file_open(f));
986 int put_file(struct thread_data *td, struct fio_file *f)
988 int f_ret = 0, ret = 0;
990 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
992 if (!fio_file_open(f)) {
997 assert(f->references);
1001 if (should_fsync(td) && td->o.fsync_on_close)
1002 f_ret = fsync(f->fd);
1004 if (td->io_ops->close_file)
1005 ret = td->io_ops->close_file(td, f);
1010 td->nr_open_files--;
1011 fio_file_clear_open(f);
1012 assert(f->fd == -1);
1016 void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
1018 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1021 if (f->lock_owner == td && f->lock_batch--)
1024 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1025 if (ddir == DDIR_READ)
1026 fio_mutex_down_read(f->lock);
1028 fio_mutex_down_write(f->lock);
1029 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1030 fio_mutex_down(f->lock);
1033 f->lock_batch = td->o.lockfile_batch;
1034 f->lock_ddir = ddir;
1037 void unlock_file(struct thread_data *td, struct fio_file *f)
1039 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1044 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1045 const int is_read = f->lock_ddir == DDIR_READ;
1046 int val = fio_mutex_getval(f->lock);
1048 if ((is_read && val == 1) || (!is_read && val == -1))
1049 f->lock_owner = NULL;
1052 fio_mutex_up_read(f->lock);
1054 fio_mutex_up_write(f->lock);
1055 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
1056 int val = fio_mutex_getval(f->lock);
1059 f->lock_owner = NULL;
1061 fio_mutex_up(f->lock);
1065 void unlock_file_all(struct thread_data *td, struct fio_file *f)
1067 if (f->lock_owner != td)
1074 static int recurse_dir(struct thread_data *td, const char *dirname)
1080 D = opendir(dirname);
1082 char buf[FIO_VERROR_SIZE];
1084 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
1085 td_verror(td, errno, buf);
1089 while ((dir = readdir(D)) != NULL) {
1090 char full_path[PATH_MAX];
1093 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1096 sprintf(full_path, "%s/%s", dirname, dir->d_name);
1098 if (lstat(full_path, &sb) == -1) {
1099 if (errno != ENOENT) {
1100 td_verror(td, errno, "stat");
1105 if (S_ISREG(sb.st_mode)) {
1106 add_file(td, full_path);
1110 if (!S_ISDIR(sb.st_mode))
1113 ret = recurse_dir(td, full_path);
1122 int add_dir_files(struct thread_data *td, const char *path)
1124 int ret = recurse_dir(td, path);
1127 log_info("fio: opendir added %d files\n", td->o.nr_files);
1132 void dup_files(struct thread_data *td, struct thread_data *org)
1137 dprint(FD_FILE, "dup files: %d\n", org->files_index);
1142 td->files = malloc(org->files_index * sizeof(f));
1144 for_each_file(org, f, i) {
1145 struct fio_file *__f;
1147 __f = smalloc(sizeof(*__f));
1149 log_err("fio: smalloc OOM\n");
1153 fio_file_reset(__f);
1156 __f->file_name = smalloc_strdup(f->file_name);
1157 if (!__f->file_name) {
1158 log_err("fio: smalloc OOM\n");
1162 __f->filetype = f->filetype;
1170 * Returns the index that matches the filename, or -1 if not there
1172 int get_fileno(struct thread_data *td, const char *fname)
1177 for_each_file(td, f, i)
1178 if (!strcmp(f->file_name, fname))
1185 * For log usage, where we add/open/close files automatically
1187 void free_release_files(struct thread_data *td)
1190 td->files_index = 0;
1191 td->nr_normal_files = 0;