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 (td->io_ops->flags & FIO_PIPEIO)
162 if (!fio_file_open(f)) {
163 if (td->io_ops->open_file(td, f)) {
164 log_err("fio: cannot pre-read, failed to open file\n");
170 old_runstate = td->runstate;
171 td_set_runstate(td, TD_PRE_READING);
173 bs = td->o.max_bs[DDIR_READ];
177 lseek(f->fd, f->file_offset, SEEK_SET);
180 while (left && !td->terminate) {
184 r = read(f->fd, b, bs);
190 td_verror(td, EIO, "pre_read");
195 td_set_runstate(td, old_runstate);
198 td->io_ops->close_file(td, f);
203 static unsigned long long get_rand_file_size(struct thread_data *td)
205 unsigned long long ret, sized;
208 r = os_random_long(&td->file_size_state);
209 sized = td->o.file_size_high - td->o.file_size_low;
210 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
211 ret += td->o.file_size_low;
212 ret -= (ret % td->o.rw_min_bs);
216 static int file_size(struct thread_data *td, struct fio_file *f)
220 if (stat(f->file_name, &st) == -1) {
221 td_verror(td, errno, "fstat");
225 f->real_file_size = st.st_size;
229 static int bdev_size(struct thread_data *td, struct fio_file *f)
231 unsigned long long bytes;
234 if (td->io_ops->open_file(td, f)) {
235 log_err("fio: failed opening blockdev %s for size check\n",
240 r = blockdev_size(f->fd, &bytes);
242 td_verror(td, r, "blockdev_size");
247 log_err("%s: zero sized block device?\n", f->file_name);
251 f->real_file_size = bytes;
252 td->io_ops->close_file(td, f);
255 td->io_ops->close_file(td, f);
259 static int get_file_size(struct thread_data *td, struct fio_file *f)
263 if (fio_file_size_known(f))
266 if (f->filetype == FIO_TYPE_FILE)
267 ret = file_size(td, f);
268 else if (f->filetype == FIO_TYPE_BD)
269 ret = bdev_size(td, f);
271 f->real_file_size = -1;
276 if (f->file_offset > f->real_file_size) {
277 log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name,
278 f->file_offset, f->real_file_size);
282 fio_file_set_size_known(f);
286 static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
287 unsigned long long off,
288 unsigned long long len)
295 off = f->file_offset;
297 if (len == -1ULL || off == -1ULL)
300 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
304 * FIXME: add blockdev flushing too
307 ret = madvise(f->mmap_ptr, f->mmap_sz, MADV_DONTNEED);
309 (void) madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
311 } else if (f->filetype == FIO_TYPE_FILE) {
312 ret = fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
313 } else if (f->filetype == FIO_TYPE_BD) {
314 ret = blockdev_invalidate_cache(f->fd);
315 if (ret < 0 && errno == EACCES && geteuid()) {
317 log_err("fio: only root may flush block "
318 "devices. Cache flush bypassed!\n");
323 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
327 td_verror(td, errno, "invalidate_cache");
329 } else if (ret > 0) {
330 td_verror(td, ret, "invalidate_cache");
338 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
340 if (!fio_file_open(f))
343 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
346 int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
350 dprint(FD_FILE, "fd close %s\n", f->file_name);
354 if (close(f->fd) < 0)
361 static int file_lookup_open(struct fio_file *f, int flags)
363 struct fio_file *__f;
366 __f = lookup_file_hash(f->file_name);
368 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
370 * racy, need the __f->lock locked
373 f->lock_owner = __f->lock_owner;
374 f->lock_batch = __f->lock_batch;
375 f->lock_ddir = __f->lock_ddir;
378 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
382 f->fd = open(f->file_name, flags, 0600);
386 int generic_open_file(struct thread_data *td, struct fio_file *f)
392 dprint(FD_FILE, "fd open %s\n", f->file_name);
394 if (!strcmp(f->file_name, "-")) {
396 log_err("fio: can't read/write to stdin/out\n");
402 * move output logging to stderr, if we are writing to stdout
409 flags |= OS_O_DIRECT;
412 if (f->filetype != FIO_TYPE_FILE)
413 flags |= FIO_O_NOATIME;
414 if (td->o.create_on_open)
422 if (f->filetype == FIO_TYPE_FILE)
426 f->fd = dup(STDOUT_FILENO);
428 from_hash = file_lookup_open(f, flags);
430 if (f->filetype == FIO_TYPE_CHAR && !read_only)
436 f->fd = dup(STDIN_FILENO);
438 from_hash = file_lookup_open(f, flags);
442 char buf[FIO_VERROR_SIZE];
445 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
446 flags &= ~FIO_O_NOATIME;
450 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
452 td_verror(td, __e, buf);
455 if (!from_hash && f->fd != -1) {
456 if (add_file_hash(f)) {
460 * OK to ignore, we haven't done anything with it
462 ret = generic_close_file(td, f);
470 int generic_get_file_size(struct thread_data *td, struct fio_file *f)
472 return get_file_size(td, f);
476 * open/close all files, so that ->real_file_size gets set
478 static int get_file_sizes(struct thread_data *td)
484 for_each_file(td, f, i) {
485 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
488 if (td_io_get_file_size(td, f)) {
489 if (td->error != ENOENT) {
490 log_err("%s\n", td->verror);
496 if (f->real_file_size == -1ULL && td->o.size)
497 f->real_file_size = td->o.size / td->o.nr_files;
504 * Open the files and setup files sizes, creating files if necessary.
506 int setup_files(struct thread_data *td)
508 unsigned long long total_size, extend_size;
511 int err = 0, need_extend;
513 dprint(FD_FILE, "setup files\n");
515 if (td->o.read_iolog_file)
519 * if ioengine defines a setup() method, it's responsible for
520 * opening the files and setting f->real_file_size to indicate
521 * the valid range for that file.
523 if (td->io_ops->setup)
524 err = td->io_ops->setup(td);
526 err = get_file_sizes(td);
532 * check sizes. if the files/devices do not exist and the size
533 * isn't passed to fio, abort.
536 for_each_file(td, f, i) {
537 if (f->real_file_size == -1ULL)
540 total_size += f->real_file_size;
544 * device/file sizes are zero and no size given, punt
546 if ((!total_size || total_size == -1ULL) && !td->o.size &&
547 !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
548 log_err("%s: you need to specify size=\n", td->o.name);
549 td_verror(td, EINVAL, "total_file_size");
554 * now file sizes are known, so we can set ->io_size. if size= is
555 * not given, ->io_size is just equal to ->real_file_size. if size
556 * is given, ->io_size is size / nr_files.
558 extend_size = total_size = 0;
560 for_each_file(td, f, i) {
561 f->file_offset = td->o.start_offset;
563 if (!td->o.file_size_low) {
565 * no file size range given, file size is equal to
566 * total size divided by number of files. if that is
567 * zero, set it to the real file size.
569 f->io_size = td->o.size / td->o.nr_files;
571 f->io_size = f->real_file_size - f->file_offset;
572 } else if (f->real_file_size < td->o.file_size_low ||
573 f->real_file_size > td->o.file_size_high) {
574 if (f->file_offset > td->o.file_size_low)
577 * file size given. if it's fixed, use that. if it's a
578 * range, generate a random size in-between.
580 if (td->o.file_size_low == td->o.file_size_high) {
581 f->io_size = td->o.file_size_low
584 f->io_size = get_rand_file_size(td)
588 f->io_size = f->real_file_size - f->file_offset;
590 if (f->io_size == -1ULL)
593 total_size += f->io_size;
595 if (f->filetype == FIO_TYPE_FILE &&
596 (f->io_size + f->file_offset) > f->real_file_size &&
597 !(td->io_ops->flags & FIO_DISKLESSIO)) {
598 if (!td->o.create_on_open) {
600 extend_size += (f->io_size + f->file_offset);
602 f->real_file_size = f->io_size + f->file_offset;
603 fio_file_set_extend(f);
607 if (!td->o.size || td->o.size > total_size)
608 td->o.size = total_size;
611 * See if we need to extend some files
616 log_info("%s: Laying out IO file(s) (%u file(s) /"
617 " %LuMB)\n", td->o.name, need_extend,
620 for_each_file(td, f, i) {
621 unsigned long long old_len = -1ULL, extend_len = -1ULL;
623 if (!fio_file_extend(f))
626 assert(f->filetype == FIO_TYPE_FILE);
627 fio_file_clear_extend(f);
628 if (!td->o.fill_device) {
629 old_len = f->real_file_size;
630 extend_len = f->io_size + f->file_offset -
633 f->real_file_size = (f->io_size + f->file_offset);
634 err = extend_file(td, f);
638 err = __file_invalidate_cache(td, f, old_len,
651 if (!td->o.zone_size)
652 td->o.zone_size = td->o.size;
655 * iolog already set the total io size, if we read back
658 if (!td->o.read_iolog_file)
659 td->total_io_size = td->o.size * td->o.loops;
662 log_err("%s: you need to specify valid offset=\n", td->o.name);
666 int pre_read_files(struct thread_data *td)
671 dprint(FD_FILE, "pre_read files\n");
673 for_each_file(td, f, i) {
674 pre_read_file(td, f);
680 int init_random_map(struct thread_data *td)
682 unsigned long long blocks, num_maps;
686 if (td->o.norandommap || !td_random(td))
689 for_each_file(td, f, i) {
690 blocks = (f->real_file_size + td->o.rw_min_bs - 1) /
691 (unsigned long long) td->o.rw_min_bs;
692 num_maps = (blocks + BLOCKS_PER_MAP - 1) /
693 (unsigned long long) BLOCKS_PER_MAP;
694 f->file_map = smalloc(num_maps * sizeof(int));
696 f->num_maps = num_maps;
699 if (!td->o.softrandommap) {
700 log_err("fio: failed allocating random map. If running"
701 " a large number of jobs, try the 'norandommap'"
702 " option or set 'softrandommap'. Or give"
703 " a larger --alloc-size to fio.\n");
707 log_info("fio: file %s failed allocating random map. Running "
708 "job without.\n", f->file_name);
715 void close_files(struct thread_data *td)
720 for_each_file(td, f, i) {
721 if (fio_file_open(f))
722 td_io_close_file(td, f);
726 void close_and_free_files(struct thread_data *td)
731 dprint(FD_FILE, "close files\n");
733 for_each_file(td, f, i) {
734 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
735 dprint(FD_FILE, "free unlink %s\n", f->file_name);
736 unlink(f->file_name);
739 if (fio_file_open(f))
740 td_io_close_file(td, f);
754 td->o.filename = NULL;
761 static void get_file_type(struct fio_file *f)
765 if (!strcmp(f->file_name, "-"))
766 f->filetype = FIO_TYPE_PIPE;
768 f->filetype = FIO_TYPE_FILE;
770 if (!lstat(f->file_name, &sb)) {
771 if (S_ISBLK(sb.st_mode))
772 f->filetype = FIO_TYPE_BD;
773 else if (S_ISCHR(sb.st_mode))
774 f->filetype = FIO_TYPE_CHAR;
775 else if (S_ISFIFO(sb.st_mode))
776 f->filetype = FIO_TYPE_PIPE;
780 int add_file(struct thread_data *td, const char *fname)
782 int cur_files = td->files_index;
783 char file_name[PATH_MAX];
787 dprint(FD_FILE, "add file %s\n", fname);
789 f = smalloc(sizeof(*f));
791 log_err("fio: smalloc OOM\n");
797 if (td->files_size <= td->files_index) {
798 int new_size = td->o.nr_files + 1;
800 dprint(FD_FILE, "resize file array to %d files\n", new_size);
802 td->files = realloc(td->files, new_size * sizeof(f));
803 td->files_size = new_size;
805 td->files[cur_files] = f;
808 * init function, io engine may not be loaded yet
810 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
811 f->real_file_size = -1ULL;
814 len = sprintf(file_name, "%s/", td->o.directory);
816 sprintf(file_name + len, "%s", fname);
817 f->file_name = smalloc_strdup(file_name);
819 log_err("fio: smalloc OOM\n");
825 switch (td->o.file_lock_mode) {
828 case FILE_LOCK_READWRITE:
829 f->lock = fio_mutex_rw_init();
831 case FILE_LOCK_EXCLUSIVE:
832 f->lock = fio_mutex_init(1);
835 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
840 if (f->filetype == FIO_TYPE_FILE)
841 td->nr_normal_files++;
843 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
849 void get_file(struct fio_file *f)
851 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
852 assert(fio_file_open(f));
856 int put_file(struct thread_data *td, struct fio_file *f)
858 int f_ret = 0, ret = 0;
860 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
862 if (!fio_file_open(f)) {
867 assert(f->references);
871 if (should_fsync(td) && td->o.fsync_on_close)
872 f_ret = fsync(f->fd);
874 if (td->io_ops->close_file)
875 ret = td->io_ops->close_file(td, f);
881 fio_file_clear_open(f);
886 void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
888 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
891 if (f->lock_owner == td && f->lock_batch--)
894 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
895 if (ddir == DDIR_READ)
896 fio_mutex_down_read(f->lock);
898 fio_mutex_down_write(f->lock);
899 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
900 fio_mutex_down(f->lock);
903 f->lock_batch = td->o.lockfile_batch;
907 void unlock_file(struct thread_data *td, struct fio_file *f)
909 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
914 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
915 const int is_read = f->lock_ddir == DDIR_READ;
916 int val = fio_mutex_getval(f->lock);
918 if ((is_read && val == 1) || (!is_read && val == -1))
919 f->lock_owner = NULL;
922 fio_mutex_up_read(f->lock);
924 fio_mutex_up_write(f->lock);
925 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
926 int val = fio_mutex_getval(f->lock);
929 f->lock_owner = NULL;
931 fio_mutex_up(f->lock);
935 void unlock_file_all(struct thread_data *td, struct fio_file *f)
937 if (f->lock_owner != td)
944 static int recurse_dir(struct thread_data *td, const char *dirname)
950 D = opendir(dirname);
952 char buf[FIO_VERROR_SIZE];
954 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
955 td_verror(td, errno, buf);
959 while ((dir = readdir(D)) != NULL) {
960 char full_path[PATH_MAX];
963 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
966 sprintf(full_path, "%s/%s", dirname, dir->d_name);
968 if (lstat(full_path, &sb) == -1) {
969 if (errno != ENOENT) {
970 td_verror(td, errno, "stat");
975 if (S_ISREG(sb.st_mode)) {
976 add_file(td, full_path);
980 if (!S_ISDIR(sb.st_mode))
983 ret = recurse_dir(td, full_path);
992 int add_dir_files(struct thread_data *td, const char *path)
994 int ret = recurse_dir(td, path);
997 log_info("fio: opendir added %d files\n", td->o.nr_files);
1002 void dup_files(struct thread_data *td, struct thread_data *org)
1007 dprint(FD_FILE, "dup files: %d\n", org->files_index);
1012 td->files = malloc(org->files_index * sizeof(f));
1014 for_each_file(org, f, i) {
1015 struct fio_file *__f;
1017 __f = smalloc(sizeof(*__f));
1019 log_err("fio: smalloc OOM\n");
1025 __f->file_name = smalloc_strdup(f->file_name);
1026 if (!__f->file_name) {
1027 log_err("fio: smalloc OOM\n");
1031 __f->filetype = f->filetype;
1039 * Returns the index that matches the filename, or -1 if not there
1041 int get_fileno(struct thread_data *td, const char *fname)
1046 for_each_file(td, f, i)
1047 if (!strcmp(f->file_name, fname))
1054 * For log usage, where we add/open/close files automatically
1056 void free_release_files(struct thread_data *td)
1059 td->files_index = 0;
1060 td->nr_normal_files = 0;