16 static inline void clear_error(struct thread_data *td)
23 * Leaves f->fd open on success, caller must close
25 static int extend_file(struct thread_data *td, struct fio_file *f)
27 int r, new_layout = 0, unlink_file = 0, flags;
28 unsigned long long left;
33 log_err("fio: refusing extend of file due to read-only\n");
38 * check if we need to lay the file out complete again. fio
39 * does that for operations involving reads, or for writes
40 * where overwrite is set
42 if (td_read(td) || (td_write(td) && td->o.overwrite) ||
43 (td_write(td) && td->io_ops->flags & FIO_NOEXTEND))
45 if (td_write(td) && !td->o.overwrite)
48 if (unlink_file || new_layout) {
49 dprint(FD_FILE, "layout unlink %s\n", f->file_name);
50 if ((unlink(f->file_name) < 0) && (errno != ENOENT)) {
51 td_verror(td, errno, "unlink");
56 flags = O_WRONLY | O_CREAT;
60 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
61 f->fd = open(f->file_name, flags, 0644);
63 td_verror(td, errno, "open");
71 * The size will be -1ULL when fill_device is used, so don't truncate
72 * or fallocate this file, just write it
74 if (!td->o.fill_device) {
75 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
77 if (ftruncate(f->fd, f->real_file_size) == -1) {
78 td_verror(td, errno, "ftruncate");
82 #ifdef FIO_HAVE_FALLOCATE
83 dprint(FD_FILE, "fallocate file %s, size %llu\n", f->file_name,
85 r = posix_fallocate(f->fd, 0, f->real_file_size);
87 log_err("fio: posix_fallocate fails: %s\n",
93 b = malloc(td->o.max_bs[DDIR_WRITE]);
94 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
96 left = f->real_file_size;
97 while (left && !td->terminate) {
98 bs = td->o.max_bs[DDIR_WRITE];
102 r = write(f->fd, b, bs);
112 if (td->o.fill_device)
114 log_info("fio: ENOSPC on laying out "
118 td_verror(td, errno, "write");
120 td_verror(td, EIO, "write");
127 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
128 unlink(f->file_name);
129 } else if (td->o.create_fsync) {
130 if (fsync(f->fd) < 0) {
131 td_verror(td, errno, "fsync");
135 if (td->o.fill_device && !td_write(td)) {
136 fio_file_clear_size_known(f);
137 if (td_io_get_file_size(td, f))
139 if (f->io_size > f->real_file_size)
140 f->io_size = f->real_file_size;
152 static int pre_read_file(struct thread_data *td, struct fio_file *f)
154 int r, did_open = 0, old_runstate;
155 unsigned long long left;
159 if (!fio_file_open(f)) {
160 if (td->io_ops->open_file(td, f)) {
161 log_err("fio: cannot pre-read, failed to open file\n");
167 old_runstate = td->runstate;
168 td_set_runstate(td, TD_PRE_READING);
170 bs = td->o.max_bs[DDIR_READ];
174 lseek(f->fd, f->file_offset, SEEK_SET);
177 while (left && !td->terminate) {
181 r = read(f->fd, b, bs);
187 td_verror(td, EIO, "pre_read");
192 td_set_runstate(td, old_runstate);
195 td->io_ops->close_file(td, f);
200 static unsigned long long get_rand_file_size(struct thread_data *td)
202 unsigned long long ret, sized;
205 r = os_random_long(&td->file_size_state);
206 sized = td->o.file_size_high - td->o.file_size_low;
207 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
208 ret += td->o.file_size_low;
209 ret -= (ret % td->o.rw_min_bs);
213 static int file_size(struct thread_data *td, struct fio_file *f)
217 if (stat(f->file_name, &st) == -1) {
218 td_verror(td, errno, "fstat");
222 f->real_file_size = st.st_size;
226 static int bdev_size(struct thread_data *td, struct fio_file *f)
228 unsigned long long bytes;
231 if (td->io_ops->open_file(td, f)) {
232 log_err("fio: failed opening blockdev %s for size check\n",
237 r = blockdev_size(f->fd, &bytes);
239 td_verror(td, r, "blockdev_size");
244 log_err("%s: zero sized block device?\n", f->file_name);
248 f->real_file_size = bytes;
249 td->io_ops->close_file(td, f);
252 td->io_ops->close_file(td, f);
256 static int get_file_size(struct thread_data *td, struct fio_file *f)
260 if (fio_file_size_known(f))
263 if (f->filetype == FIO_TYPE_FILE)
264 ret = file_size(td, f);
265 else if (f->filetype == FIO_TYPE_BD)
266 ret = bdev_size(td, f);
268 f->real_file_size = -1;
273 if (f->file_offset > f->real_file_size) {
274 log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name,
275 f->file_offset, f->real_file_size);
279 fio_file_set_size_known(f);
283 static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
284 unsigned long long off,
285 unsigned long long len)
292 off = f->file_offset;
294 if (len == -1ULL || off == -1ULL)
297 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
301 * FIXME: add blockdev flushing too
304 ret = madvise(f->mmap_ptr, f->mmap_sz, MADV_DONTNEED);
305 else if (f->filetype == FIO_TYPE_FILE) {
306 ret = fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
307 } else if (f->filetype == FIO_TYPE_BD) {
308 ret = blockdev_invalidate_cache(f->fd);
309 if (ret < 0 && errno == EACCES && geteuid()) {
311 log_err("fio: only root may flush block "
312 "devices. Cache flush bypassed!\n");
317 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
321 td_verror(td, errno, "invalidate_cache");
323 } else if (ret > 0) {
324 td_verror(td, ret, "invalidate_cache");
332 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
334 if (!fio_file_open(f))
337 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
340 int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
344 dprint(FD_FILE, "fd close %s\n", f->file_name);
348 if (close(f->fd) < 0)
355 static int file_lookup_open(struct fio_file *f, int flags)
357 struct fio_file *__f;
360 __f = lookup_file_hash(f->file_name);
362 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
364 * racy, need the __f->lock locked
367 f->lock_owner = __f->lock_owner;
368 f->lock_batch = __f->lock_batch;
369 f->lock_ddir = __f->lock_ddir;
372 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
376 f->fd = open(f->file_name, flags, 0600);
380 int generic_open_file(struct thread_data *td, struct fio_file *f)
386 dprint(FD_FILE, "fd open %s\n", f->file_name);
388 if (!strcmp(f->file_name, "-")) {
390 log_err("fio: can't read/write to stdin/out\n");
396 * move output logging to stderr, if we are writing to stdout
403 flags |= OS_O_DIRECT;
406 if (f->filetype != FIO_TYPE_FILE)
407 flags |= FIO_O_NOATIME;
408 if (td->o.create_on_open)
416 if (f->filetype == FIO_TYPE_FILE)
420 f->fd = dup(STDOUT_FILENO);
422 from_hash = file_lookup_open(f, flags);
424 if (f->filetype == FIO_TYPE_CHAR && !read_only)
430 f->fd = dup(STDIN_FILENO);
432 from_hash = file_lookup_open(f, flags);
436 char buf[FIO_VERROR_SIZE];
439 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
440 flags &= ~FIO_O_NOATIME;
444 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
446 td_verror(td, __e, buf);
449 if (!from_hash && f->fd != -1) {
450 if (add_file_hash(f)) {
454 * OK to ignore, we haven't done anything with it
456 ret = generic_close_file(td, f);
464 int generic_get_file_size(struct thread_data *td, struct fio_file *f)
466 return get_file_size(td, f);
470 * open/close all files, so that ->real_file_size gets set
472 static int get_file_sizes(struct thread_data *td)
478 for_each_file(td, f, i) {
479 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
482 if (td_io_get_file_size(td, f)) {
483 if (td->error != ENOENT) {
484 log_err("%s\n", td->verror);
490 if (f->real_file_size == -1ULL && td->o.size)
491 f->real_file_size = td->o.size / td->o.nr_files;
498 * Open the files and setup files sizes, creating files if necessary.
500 int setup_files(struct thread_data *td)
502 unsigned long long total_size, extend_size;
505 int err = 0, need_extend;
507 dprint(FD_FILE, "setup files\n");
509 if (td->o.read_iolog_file)
513 * if ioengine defines a setup() method, it's responsible for
514 * opening the files and setting f->real_file_size to indicate
515 * the valid range for that file.
517 if (td->io_ops->setup)
518 err = td->io_ops->setup(td);
520 err = get_file_sizes(td);
526 * check sizes. if the files/devices do not exist and the size
527 * isn't passed to fio, abort.
530 for_each_file(td, f, i) {
531 if (f->real_file_size == -1ULL)
534 total_size += f->real_file_size;
538 * device/file sizes are zero and no size given, punt
540 if ((!total_size || total_size == -1ULL) && !td->o.size &&
541 !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
542 log_err("%s: you need to specify size=\n", td->o.name);
543 td_verror(td, EINVAL, "total_file_size");
548 * now file sizes are known, so we can set ->io_size. if size= is
549 * not given, ->io_size is just equal to ->real_file_size. if size
550 * is given, ->io_size is size / nr_files.
552 extend_size = total_size = 0;
554 for_each_file(td, f, i) {
555 f->file_offset = td->o.start_offset;
557 if (!td->o.file_size_low) {
559 * no file size range given, file size is equal to
560 * total size divided by number of files. if that is
561 * zero, set it to the real file size.
563 f->io_size = td->o.size / td->o.nr_files;
565 f->io_size = f->real_file_size - f->file_offset;
566 } else if (f->real_file_size < td->o.file_size_low ||
567 f->real_file_size > td->o.file_size_high) {
568 if (f->file_offset > td->o.file_size_low)
571 * file size given. if it's fixed, use that. if it's a
572 * range, generate a random size in-between.
574 if (td->o.file_size_low == td->o.file_size_high) {
575 f->io_size = td->o.file_size_low
578 f->io_size = get_rand_file_size(td)
582 f->io_size = f->real_file_size - f->file_offset;
584 if (f->io_size == -1ULL)
587 total_size += f->io_size;
589 if (f->filetype == FIO_TYPE_FILE &&
590 (f->io_size + f->file_offset) > f->real_file_size &&
591 !(td->io_ops->flags & FIO_DISKLESSIO)) {
592 if (!td->o.create_on_open) {
594 extend_size += (f->io_size + f->file_offset);
596 f->real_file_size = f->io_size + f->file_offset;
597 fio_file_set_extend(f);
601 if (!td->o.size || td->o.size > total_size)
602 td->o.size = total_size;
605 * See if we need to extend some files
610 log_info("%s: Laying out IO file(s) (%u file(s) /"
611 " %LuMiB)\n", td->o.name, need_extend,
614 for_each_file(td, f, i) {
615 unsigned long long old_len = -1ULL, extend_len = -1ULL;
617 if (!fio_file_extend(f))
620 assert(f->filetype == FIO_TYPE_FILE);
621 fio_file_clear_extend(f);
622 if (!td->o.fill_device) {
623 old_len = f->real_file_size;
624 extend_len = f->io_size + f->file_offset - old_len;
626 f->real_file_size = (f->io_size + f->file_offset);
627 err = extend_file(td, f);
631 err = __file_invalidate_cache(td, f, old_len,
644 if (!td->o.zone_size)
645 td->o.zone_size = td->o.size;
648 * iolog already set the total io size, if we read back
651 if (!td->o.read_iolog_file)
652 td->total_io_size = td->o.size * td->o.loops;
655 log_err("%s: you need to specify valid offset=\n", td->o.name);
659 int pre_read_files(struct thread_data *td)
664 dprint(FD_FILE, "pre_read files\n");
666 for_each_file(td, f, i) {
667 pre_read_file(td, f);
673 int init_random_map(struct thread_data *td)
675 unsigned long long blocks, num_maps;
679 if (td->o.norandommap || !td_random(td))
682 for_each_file(td, f, i) {
683 blocks = (f->real_file_size + td->o.rw_min_bs - 1) /
684 (unsigned long long) td->o.rw_min_bs;
685 num_maps = (blocks + BLOCKS_PER_MAP - 1) /
686 (unsigned long long) BLOCKS_PER_MAP;
687 f->file_map = smalloc(num_maps * sizeof(int));
689 f->num_maps = num_maps;
692 if (!td->o.softrandommap) {
693 log_err("fio: failed allocating random map. If running"
694 " a large number of jobs, try the 'norandommap'"
695 " option or set 'softrandommap'. Or give"
696 " a larger --alloc-size to fio.\n");
700 log_info("fio: file %s failed allocating random map. Running "
701 "job without.\n", f->file_name);
708 void close_files(struct thread_data *td)
713 for_each_file(td, f, i) {
714 if (fio_file_open(f))
715 td_io_close_file(td, f);
719 void close_and_free_files(struct thread_data *td)
724 dprint(FD_FILE, "close files\n");
726 for_each_file(td, f, i) {
727 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
728 dprint(FD_FILE, "free unlink %s\n", f->file_name);
729 unlink(f->file_name);
732 if (fio_file_open(f))
733 td_io_close_file(td, f);
747 td->o.filename = NULL;
754 static void get_file_type(struct fio_file *f)
758 if (!strcmp(f->file_name, "-"))
759 f->filetype = FIO_TYPE_PIPE;
761 f->filetype = FIO_TYPE_FILE;
763 if (!lstat(f->file_name, &sb)) {
764 if (S_ISBLK(sb.st_mode))
765 f->filetype = FIO_TYPE_BD;
766 else if (S_ISCHR(sb.st_mode))
767 f->filetype = FIO_TYPE_CHAR;
768 else if (S_ISFIFO(sb.st_mode))
769 f->filetype = FIO_TYPE_PIPE;
773 int add_file(struct thread_data *td, const char *fname)
775 int cur_files = td->files_index;
776 char file_name[PATH_MAX];
780 dprint(FD_FILE, "add file %s\n", fname);
782 f = smalloc(sizeof(*f));
784 log_err("fio: smalloc OOM\n");
790 if (td->files_size <= td->files_index) {
791 int new_size = td->o.nr_files + 1;
793 dprint(FD_FILE, "resize file array to %d files\n", new_size);
795 td->files = realloc(td->files, new_size * sizeof(f));
796 td->files_size = new_size;
798 td->files[cur_files] = f;
801 * init function, io engine may not be loaded yet
803 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
804 f->real_file_size = -1ULL;
807 len = sprintf(file_name, "%s/", td->o.directory);
809 sprintf(file_name + len, "%s", fname);
810 f->file_name = smalloc_strdup(file_name);
812 log_err("fio: smalloc OOM\n");
818 switch (td->o.file_lock_mode) {
821 case FILE_LOCK_READWRITE:
822 f->lock = fio_mutex_rw_init();
824 case FILE_LOCK_EXCLUSIVE:
825 f->lock = fio_mutex_init(1);
828 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
833 if (f->filetype == FIO_TYPE_FILE)
834 td->nr_normal_files++;
836 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
842 void get_file(struct fio_file *f)
844 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
845 assert(fio_file_open(f));
849 int put_file(struct thread_data *td, struct fio_file *f)
851 int f_ret = 0, ret = 0;
853 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
855 if (!fio_file_open(f)) {
860 assert(f->references);
864 if (should_fsync(td) && td->o.fsync_on_close)
865 f_ret = fsync(f->fd);
867 if (td->io_ops->close_file)
868 ret = td->io_ops->close_file(td, f);
874 fio_file_clear_open(f);
879 void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
881 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
884 if (f->lock_owner == td && f->lock_batch--)
887 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
888 if (ddir == DDIR_READ)
889 fio_mutex_down_read(f->lock);
891 fio_mutex_down_write(f->lock);
892 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
893 fio_mutex_down(f->lock);
896 f->lock_batch = td->o.lockfile_batch;
900 void unlock_file(struct thread_data *td, struct fio_file *f)
902 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
907 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
908 const int is_read = f->lock_ddir == DDIR_READ;
909 int val = fio_mutex_getval(f->lock);
911 if ((is_read && val == 1) || (!is_read && val == -1))
912 f->lock_owner = NULL;
915 fio_mutex_up_read(f->lock);
917 fio_mutex_up_write(f->lock);
918 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
919 int val = fio_mutex_getval(f->lock);
922 f->lock_owner = NULL;
924 fio_mutex_up(f->lock);
928 void unlock_file_all(struct thread_data *td, struct fio_file *f)
930 if (f->lock_owner != td)
937 static int recurse_dir(struct thread_data *td, const char *dirname)
943 D = opendir(dirname);
945 char buf[FIO_VERROR_SIZE];
947 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
948 td_verror(td, errno, buf);
952 while ((dir = readdir(D)) != NULL) {
953 char full_path[PATH_MAX];
956 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
959 sprintf(full_path, "%s/%s", dirname, dir->d_name);
961 if (lstat(full_path, &sb) == -1) {
962 if (errno != ENOENT) {
963 td_verror(td, errno, "stat");
968 if (S_ISREG(sb.st_mode)) {
969 add_file(td, full_path);
973 if (!S_ISDIR(sb.st_mode))
976 ret = recurse_dir(td, full_path);
985 int add_dir_files(struct thread_data *td, const char *path)
987 int ret = recurse_dir(td, path);
990 log_info("fio: opendir added %d files\n", td->o.nr_files);
995 void dup_files(struct thread_data *td, struct thread_data *org)
1000 dprint(FD_FILE, "dup files: %d\n", org->files_index);
1005 td->files = malloc(org->files_index * sizeof(f));
1007 for_each_file(org, f, i) {
1008 struct fio_file *__f;
1010 __f = smalloc(sizeof(*__f));
1012 log_err("fio: smalloc OOM\n");
1018 __f->file_name = smalloc_strdup(f->file_name);
1019 if (!__f->file_name) {
1020 log_err("fio: smalloc OOM\n");
1024 __f->filetype = f->filetype;
1032 * Returns the index that matches the filename, or -1 if not there
1034 int get_fileno(struct thread_data *td, const char *fname)
1039 for_each_file(td, f, i)
1040 if (!strcmp(f->file_name, fname))
1047 * For log usage, where we add/open/close files automatically
1049 void free_release_files(struct thread_data *td)
1052 td->files_index = 0;
1053 td->nr_normal_files = 0;