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");
247 printf("fd is %d\n", f->fd);
252 log_err("%s: zero sized block device?\n", f->file_name);
256 f->real_file_size = bytes;
257 td->io_ops->close_file(td, f);
260 td->io_ops->close_file(td, f);
264 static int char_size(struct thread_data *td, struct fio_file *f)
266 #ifdef FIO_HAVE_CHARDEV_SIZE
267 unsigned long long bytes;
270 if (td->io_ops->open_file(td, f)) {
271 log_err("fio: failed opening blockdev %s for size check\n",
276 r = chardev_size(f->fd, &bytes);
278 td_verror(td, r, "chardev_size");
283 log_err("%s: zero sized char device?\n", f->file_name);
287 f->real_file_size = bytes;
288 td->io_ops->close_file(td, f);
291 td->io_ops->close_file(td, f);
294 f->real_file_size = -1ULL;
299 static int get_file_size(struct thread_data *td, struct fio_file *f)
303 if (fio_file_size_known(f))
306 if (f->filetype == FIO_TYPE_FILE)
307 ret = file_size(td, f);
308 else if (f->filetype == FIO_TYPE_BD)
309 ret = bdev_size(td, f);
310 else if (f->filetype == FIO_TYPE_CHAR)
311 ret = char_size(td, f);
313 f->real_file_size = -1;
318 if (f->file_offset > f->real_file_size) {
319 log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name,
320 f->file_offset, f->real_file_size);
324 fio_file_set_size_known(f);
328 static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
329 unsigned long long off,
330 unsigned long long len)
337 off = f->file_offset;
339 if (len == -1ULL || off == -1ULL)
342 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
346 * FIXME: add blockdev flushing too
349 ret = madvise(f->mmap_ptr, f->mmap_sz, MADV_DONTNEED);
351 (void) madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
353 } else if (f->filetype == FIO_TYPE_FILE) {
354 ret = fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
355 } else if (f->filetype == FIO_TYPE_BD) {
356 ret = blockdev_invalidate_cache(f->fd);
357 if (ret < 0 && errno == EACCES && geteuid()) {
359 log_err("fio: only root may flush block "
360 "devices. Cache flush bypassed!\n");
365 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
369 td_verror(td, errno, "invalidate_cache");
371 } else if (ret > 0) {
372 td_verror(td, ret, "invalidate_cache");
380 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
382 if (!fio_file_open(f))
385 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
388 int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
392 dprint(FD_FILE, "fd close %s\n", f->file_name);
396 if (close(f->fd) < 0)
403 static int file_lookup_open(struct fio_file *f, int flags)
405 struct fio_file *__f;
408 __f = lookup_file_hash(f->file_name);
410 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
412 * racy, need the __f->lock locked
415 f->lock_owner = __f->lock_owner;
416 f->lock_batch = __f->lock_batch;
417 f->lock_ddir = __f->lock_ddir;
420 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
424 f->fd = open(f->file_name, flags, 0600);
428 int generic_open_file(struct thread_data *td, struct fio_file *f)
434 dprint(FD_FILE, "fd open %s\n", f->file_name);
436 if (!strcmp(f->file_name, "-")) {
438 log_err("fio: can't read/write to stdin/out\n");
444 * move output logging to stderr, if we are writing to stdout
451 flags |= OS_O_DIRECT;
454 if (f->filetype != FIO_TYPE_FILE)
455 flags |= FIO_O_NOATIME;
456 if (td->o.create_on_open)
464 if (f->filetype == FIO_TYPE_FILE)
468 f->fd = dup(STDOUT_FILENO);
470 from_hash = file_lookup_open(f, flags);
472 if (f->filetype == FIO_TYPE_CHAR && !read_only)
478 f->fd = dup(STDIN_FILENO);
480 from_hash = file_lookup_open(f, flags);
484 char buf[FIO_VERROR_SIZE];
487 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
488 flags &= ~FIO_O_NOATIME;
492 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
494 td_verror(td, __e, buf);
497 if (!from_hash && f->fd != -1) {
498 if (add_file_hash(f)) {
502 * OK to ignore, we haven't done anything with it
504 ret = generic_close_file(td, f);
512 int generic_get_file_size(struct thread_data *td, struct fio_file *f)
514 return get_file_size(td, f);
518 * open/close all files, so that ->real_file_size gets set
520 static int get_file_sizes(struct thread_data *td)
526 for_each_file(td, f, i) {
527 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
530 if (td_io_get_file_size(td, f)) {
531 if (td->error != ENOENT) {
532 log_err("%s\n", td->verror);
538 if (f->real_file_size == -1ULL && td->o.size)
539 f->real_file_size = td->o.size / td->o.nr_files;
546 struct flist_head list;
553 * Get free number of bytes for each file on each unique mount.
555 static unsigned long long get_fs_free_counts(struct thread_data *td)
557 struct flist_head *n, *tmp;
558 unsigned long long ret = 0;
559 struct fio_mount *fm;
564 for_each_file(td, f, i) {
568 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
569 if (f->real_file_size != -1ULL)
570 ret += f->real_file_size;
572 } else if (f->filetype != FIO_TYPE_FILE)
575 strcpy(buf, f->file_name);
577 if (stat(buf, &sb) < 0) {
581 if (stat(buf, &sb) < 0)
586 flist_for_each(n, &list) {
587 fm = flist_entry(n, struct fio_mount, list);
588 if (fm->key == sb.st_dev)
597 fm = malloc(sizeof(*fm));
598 strcpy(fm->__base, buf);
599 fm->base = basename(fm->__base);
601 flist_add(&fm->list, &list);
604 flist_for_each_safe(n, tmp, &list) {
605 unsigned long long sz;
607 fm = flist_entry(n, struct fio_mount, list);
608 flist_del(&fm->list);
610 sz = get_fs_size(fm->base);
611 if (sz && sz != -1ULL)
621 * Open the files and setup files sizes, creating files if necessary.
623 int setup_files(struct thread_data *td)
625 unsigned long long total_size, extend_size;
628 int err = 0, need_extend;
630 dprint(FD_FILE, "setup files\n");
632 if (td->o.read_iolog_file)
636 * if ioengine defines a setup() method, it's responsible for
637 * opening the files and setting f->real_file_size to indicate
638 * the valid range for that file.
640 if (td->io_ops->setup)
641 err = td->io_ops->setup(td);
643 err = get_file_sizes(td);
649 * check sizes. if the files/devices do not exist and the size
650 * isn't passed to fio, abort.
653 for_each_file(td, f, i) {
654 if (f->real_file_size == -1ULL)
657 total_size += f->real_file_size;
660 if (td->o.fill_device)
661 td->fill_device_size = get_fs_free_counts(td);
664 * device/file sizes are zero and no size given, punt
666 if ((!total_size || total_size == -1ULL) && !td->o.size &&
667 !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
668 log_err("%s: you need to specify size=\n", td->o.name);
669 td_verror(td, EINVAL, "total_file_size");
674 * now file sizes are known, so we can set ->io_size. if size= is
675 * not given, ->io_size is just equal to ->real_file_size. if size
676 * is given, ->io_size is size / nr_files.
678 extend_size = total_size = 0;
680 for_each_file(td, f, i) {
681 f->file_offset = td->o.start_offset;
683 if (!td->o.file_size_low) {
685 * no file size range given, file size is equal to
686 * total size divided by number of files. if that is
687 * zero, set it to the real file size.
689 f->io_size = td->o.size / td->o.nr_files;
691 f->io_size = f->real_file_size - f->file_offset;
692 } else if (f->real_file_size < td->o.file_size_low ||
693 f->real_file_size > td->o.file_size_high) {
694 if (f->file_offset > td->o.file_size_low)
697 * file size given. if it's fixed, use that. if it's a
698 * range, generate a random size in-between.
700 if (td->o.file_size_low == td->o.file_size_high) {
701 f->io_size = td->o.file_size_low
704 f->io_size = get_rand_file_size(td)
708 f->io_size = f->real_file_size - f->file_offset;
710 if (f->io_size == -1ULL)
713 total_size += f->io_size;
715 if (f->filetype == FIO_TYPE_FILE &&
716 (f->io_size + f->file_offset) > f->real_file_size &&
717 !(td->io_ops->flags & FIO_DISKLESSIO)) {
718 if (!td->o.create_on_open) {
720 extend_size += (f->io_size + f->file_offset);
722 f->real_file_size = f->io_size + f->file_offset;
723 fio_file_set_extend(f);
727 if (!td->o.size || td->o.size > total_size)
728 td->o.size = total_size;
731 * See if we need to extend some files
736 log_info("%s: Laying out IO file(s) (%u file(s) /"
737 " %LuMB)\n", td->o.name, need_extend,
740 for_each_file(td, f, i) {
741 unsigned long long old_len = -1ULL, extend_len = -1ULL;
743 if (!fio_file_extend(f))
746 assert(f->filetype == FIO_TYPE_FILE);
747 fio_file_clear_extend(f);
748 if (!td->o.fill_device) {
749 old_len = f->real_file_size;
750 extend_len = f->io_size + f->file_offset -
753 f->real_file_size = (f->io_size + f->file_offset);
754 err = extend_file(td, f);
758 err = __file_invalidate_cache(td, f, old_len,
771 if (!td->o.zone_size)
772 td->o.zone_size = td->o.size;
775 * iolog already set the total io size, if we read back
778 if (!td->o.read_iolog_file)
779 td->total_io_size = td->o.size * td->o.loops;
782 log_err("%s: you need to specify valid offset=\n", td->o.name);
786 int pre_read_files(struct thread_data *td)
791 dprint(FD_FILE, "pre_read files\n");
793 for_each_file(td, f, i) {
794 pre_read_file(td, f);
800 int init_random_map(struct thread_data *td)
802 unsigned long long blocks, num_maps;
806 if (td->o.norandommap || !td_random(td))
809 for_each_file(td, f, i) {
810 blocks = (f->real_file_size + td->o.rw_min_bs - 1) /
811 (unsigned long long) td->o.rw_min_bs;
812 num_maps = (blocks + BLOCKS_PER_MAP - 1) /
813 (unsigned long long) BLOCKS_PER_MAP;
814 f->file_map = smalloc(num_maps * sizeof(int));
816 f->num_maps = num_maps;
819 if (!td->o.softrandommap) {
820 log_err("fio: failed allocating random map. If running"
821 " a large number of jobs, try the 'norandommap'"
822 " option or set 'softrandommap'. Or give"
823 " a larger --alloc-size to fio.\n");
827 log_info("fio: file %s failed allocating random map. Running "
828 "job without.\n", f->file_name);
835 void close_files(struct thread_data *td)
840 for_each_file(td, f, i) {
841 if (fio_file_open(f))
842 td_io_close_file(td, f);
846 void close_and_free_files(struct thread_data *td)
851 dprint(FD_FILE, "close files\n");
853 for_each_file(td, f, i) {
854 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
855 dprint(FD_FILE, "free unlink %s\n", f->file_name);
856 unlink(f->file_name);
859 if (fio_file_open(f))
860 td_io_close_file(td, f);
871 td->o.filename = NULL;
878 static void get_file_type(struct fio_file *f)
882 if (!strcmp(f->file_name, "-"))
883 f->filetype = FIO_TYPE_PIPE;
885 f->filetype = FIO_TYPE_FILE;
887 if (!stat(f->file_name, &sb)) {
888 if (S_ISBLK(sb.st_mode))
889 f->filetype = FIO_TYPE_BD;
890 else if (S_ISCHR(sb.st_mode))
891 f->filetype = FIO_TYPE_CHAR;
892 else if (S_ISFIFO(sb.st_mode))
893 f->filetype = FIO_TYPE_PIPE;
897 int add_file(struct thread_data *td, const char *fname)
899 int cur_files = td->files_index;
900 char file_name[PATH_MAX];
904 dprint(FD_FILE, "add file %s\n", fname);
906 f = smalloc(sizeof(*f));
908 log_err("fio: smalloc OOM\n");
915 if (td->files_size <= td->files_index) {
916 int new_size = td->o.nr_files + 1;
918 dprint(FD_FILE, "resize file array to %d files\n", new_size);
920 td->files = realloc(td->files, new_size * sizeof(f));
921 td->files_size = new_size;
923 td->files[cur_files] = f;
926 * init function, io engine may not be loaded yet
928 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
929 f->real_file_size = -1ULL;
932 len = sprintf(file_name, "%s/", td->o.directory);
934 sprintf(file_name + len, "%s", fname);
935 f->file_name = smalloc_strdup(file_name);
937 log_err("fio: smalloc OOM\n");
943 switch (td->o.file_lock_mode) {
946 case FILE_LOCK_READWRITE:
947 f->lock = fio_mutex_rw_init();
949 case FILE_LOCK_EXCLUSIVE:
950 f->lock = fio_mutex_init(1);
953 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
958 if (f->filetype == FIO_TYPE_FILE)
959 td->nr_normal_files++;
961 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
967 int add_file_exclusive(struct thread_data *td, const char *fname)
972 for_each_file(td, f, i) {
973 if (!strcmp(f->file_name, fname))
977 return add_file(td, fname);
980 void get_file(struct fio_file *f)
982 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
983 assert(fio_file_open(f));
987 int put_file(struct thread_data *td, struct fio_file *f)
989 int f_ret = 0, ret = 0;
991 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
993 if (!fio_file_open(f)) {
998 assert(f->references);
1002 if (should_fsync(td) && td->o.fsync_on_close)
1003 f_ret = fsync(f->fd);
1005 if (td->io_ops->close_file)
1006 ret = td->io_ops->close_file(td, f);
1011 td->nr_open_files--;
1012 fio_file_clear_open(f);
1013 assert(f->fd == -1);
1017 void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
1019 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1022 if (f->lock_owner == td && f->lock_batch--)
1025 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1026 if (ddir == DDIR_READ)
1027 fio_mutex_down_read(f->lock);
1029 fio_mutex_down_write(f->lock);
1030 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1031 fio_mutex_down(f->lock);
1034 f->lock_batch = td->o.lockfile_batch;
1035 f->lock_ddir = ddir;
1038 void unlock_file(struct thread_data *td, struct fio_file *f)
1040 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1045 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1046 const int is_read = f->lock_ddir == DDIR_READ;
1047 int val = fio_mutex_getval(f->lock);
1049 if ((is_read && val == 1) || (!is_read && val == -1))
1050 f->lock_owner = NULL;
1053 fio_mutex_up_read(f->lock);
1055 fio_mutex_up_write(f->lock);
1056 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
1057 int val = fio_mutex_getval(f->lock);
1060 f->lock_owner = NULL;
1062 fio_mutex_up(f->lock);
1066 void unlock_file_all(struct thread_data *td, struct fio_file *f)
1068 if (f->lock_owner != td)
1075 static int recurse_dir(struct thread_data *td, const char *dirname)
1081 D = opendir(dirname);
1083 char buf[FIO_VERROR_SIZE];
1085 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
1086 td_verror(td, errno, buf);
1090 while ((dir = readdir(D)) != NULL) {
1091 char full_path[PATH_MAX];
1094 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1097 sprintf(full_path, "%s/%s", dirname, dir->d_name);
1099 if (lstat(full_path, &sb) == -1) {
1100 if (errno != ENOENT) {
1101 td_verror(td, errno, "stat");
1106 if (S_ISREG(sb.st_mode)) {
1107 add_file(td, full_path);
1111 if (!S_ISDIR(sb.st_mode))
1114 ret = recurse_dir(td, full_path);
1123 int add_dir_files(struct thread_data *td, const char *path)
1125 int ret = recurse_dir(td, path);
1128 log_info("fio: opendir added %d files\n", td->o.nr_files);
1133 void dup_files(struct thread_data *td, struct thread_data *org)
1138 dprint(FD_FILE, "dup files: %d\n", org->files_index);
1143 td->files = malloc(org->files_index * sizeof(f));
1145 for_each_file(org, f, i) {
1146 struct fio_file *__f;
1148 __f = smalloc(sizeof(*__f));
1150 log_err("fio: smalloc OOM\n");
1154 fio_file_reset(__f);
1157 __f->file_name = smalloc_strdup(f->file_name);
1158 if (!__f->file_name) {
1159 log_err("fio: smalloc OOM\n");
1163 __f->filetype = f->filetype;
1171 * Returns the index that matches the filename, or -1 if not there
1173 int get_fileno(struct thread_data *td, const char *fname)
1178 for_each_file(td, f, i)
1179 if (!strcmp(f->file_name, fname))
1186 * For log usage, where we add/open/close files automatically
1188 void free_release_files(struct thread_data *td)
1191 td->files_index = 0;
1192 td->nr_normal_files = 0;