17 * Leaves f->fd open on success, caller must close
19 static int extend_file(struct thread_data *td, struct fio_file *f)
21 int r, new_layout = 0, unlink_file = 0, flags;
22 unsigned long long left;
27 log_err("fio: refusing extend of file due to read-only\n");
32 * check if we need to lay the file out complete again. fio
33 * does that for operations involving reads, or for writes
34 * where overwrite is set
36 if (td_read(td) || (td_write(td) && td->o.overwrite) ||
37 (td_write(td) && td->io_ops->flags & FIO_NOEXTEND))
39 if (td_write(td) && !td->o.overwrite)
42 if (unlink_file || new_layout) {
43 dprint(FD_FILE, "layout unlink %s\n", f->file_name);
44 if ((unlink(f->file_name) < 0) && (errno != ENOENT)) {
45 td_verror(td, errno, "unlink");
50 flags = O_WRONLY | O_CREAT;
54 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
55 f->fd = open(f->file_name, flags, 0644);
57 td_verror(td, errno, "open");
65 * The size will be -1ULL when fill_device is used, so don't truncate
66 * or fallocate this file, just write it
68 if (!td->o.fill_device) {
69 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
71 if (ftruncate(f->fd, f->real_file_size) == -1) {
72 td_verror(td, errno, "ftruncate");
76 #ifdef FIO_HAVE_FALLOCATE
77 dprint(FD_FILE, "fallocate file %s, size %llu\n", f->file_name,
79 r = posix_fallocate(f->fd, 0, f->real_file_size);
81 log_err("fio: posix_fallocate fails: %s\n",
87 b = malloc(td->o.max_bs[DDIR_WRITE]);
88 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
90 left = f->real_file_size;
91 while (left && !td->terminate) {
92 bs = td->o.max_bs[DDIR_WRITE];
96 r = write(f->fd, b, bs);
106 if (td->o.fill_device)
108 log_info("fio: ENOSPC on laying out "
112 td_verror(td, errno, "write");
114 td_verror(td, EIO, "write");
121 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
122 unlink(f->file_name);
123 } else if (td->o.create_fsync) {
124 if (fsync(f->fd) < 0) {
125 td_verror(td, errno, "fsync");
129 if (td->o.fill_device) {
130 fio_file_clear_size_known(f);
131 if (td_io_get_file_size(td, f))
133 if (f->io_size > f->real_file_size)
134 f->io_size = f->real_file_size;
146 static int pre_read_file(struct thread_data *td, struct fio_file *f)
148 int r, did_open = 0, old_runstate;
149 unsigned long long left;
153 if (!fio_file_open(f)) {
154 if (td->io_ops->open_file(td, f)) {
155 log_err("fio: cannot pre-read, failed to open file\n");
161 old_runstate = td->runstate;
162 td_set_runstate(td, TD_PRE_READING);
164 bs = td->o.max_bs[DDIR_READ];
168 lseek(f->fd, f->file_offset, SEEK_SET);
171 while (left && !td->terminate) {
175 r = read(f->fd, b, bs);
181 td_verror(td, EIO, "pre_read");
186 td_set_runstate(td, old_runstate);
189 td->io_ops->close_file(td, f);
194 static unsigned long long get_rand_file_size(struct thread_data *td)
196 unsigned long long ret, sized;
199 r = os_random_long(&td->file_size_state);
200 sized = td->o.file_size_high - td->o.file_size_low;
201 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
202 ret += td->o.file_size_low;
203 ret -= (ret % td->o.rw_min_bs);
207 static int file_size(struct thread_data *td, struct fio_file *f)
211 if (stat(f->file_name, &st) == -1) {
212 td_verror(td, errno, "fstat");
216 f->real_file_size = st.st_size;
220 static int bdev_size(struct thread_data *td, struct fio_file *f)
222 unsigned long long bytes;
225 if (td->io_ops->open_file(td, f)) {
226 log_err("fio: failed opening blockdev %s for size check\n",
231 r = blockdev_size(f->fd, &bytes);
233 td_verror(td, r, "blockdev_size");
238 log_err("%s: zero sized block device?\n", f->file_name);
242 f->real_file_size = bytes;
245 td->io_ops->close_file(td, f);
249 static int get_file_size(struct thread_data *td, struct fio_file *f)
253 if (fio_file_size_known(f))
256 if (f->filetype == FIO_TYPE_FILE)
257 ret = file_size(td, f);
258 else if (f->filetype == FIO_TYPE_BD)
259 ret = bdev_size(td, f);
261 f->real_file_size = -1;
266 if (f->file_offset > f->real_file_size) {
267 log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name,
268 f->file_offset, f->real_file_size);
272 fio_file_set_size_known(f);
276 static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
277 unsigned long long off,
278 unsigned long long len)
285 off = f->file_offset;
287 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
291 * FIXME: add blockdev flushing too
294 ret = madvise(f->mmap_ptr, f->mmap_sz, MADV_DONTNEED);
295 else if (f->filetype == FIO_TYPE_FILE) {
296 ret = fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
297 } else if (f->filetype == FIO_TYPE_BD) {
298 ret = blockdev_invalidate_cache(f->fd);
299 if (ret < 0 && errno == EACCES && geteuid()) {
301 log_err("fio: only root may flush block "
302 "devices. Cache flush bypassed!\n");
307 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
311 td_verror(td, errno, "invalidate_cache");
313 } else if (ret > 0) {
314 td_verror(td, ret, "invalidate_cache");
322 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
324 if (!fio_file_open(f))
327 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
330 int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
334 dprint(FD_FILE, "fd close %s\n", f->file_name);
338 if (close(f->fd) < 0)
345 static int file_lookup_open(struct fio_file *f, int flags)
347 struct fio_file *__f;
350 __f = lookup_file_hash(f->file_name);
352 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
354 * racy, need the __f->lock locked
357 f->lock_owner = __f->lock_owner;
358 f->lock_batch = __f->lock_batch;
359 f->lock_ddir = __f->lock_ddir;
362 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
366 f->fd = open(f->file_name, flags, 0600);
370 int generic_open_file(struct thread_data *td, struct fio_file *f)
376 dprint(FD_FILE, "fd open %s\n", f->file_name);
378 if (!strcmp(f->file_name, "-")) {
380 log_err("fio: can't read/write to stdin/out\n");
386 * move output logging to stderr, if we are writing to stdout
393 flags |= OS_O_DIRECT;
396 if (f->filetype != FIO_TYPE_FILE)
397 flags |= FIO_O_NOATIME;
398 if (td->o.create_on_open)
406 if (f->filetype == FIO_TYPE_FILE)
410 f->fd = dup(STDOUT_FILENO);
412 from_hash = file_lookup_open(f, flags);
414 if (f->filetype == FIO_TYPE_CHAR && !read_only)
420 f->fd = dup(STDIN_FILENO);
422 from_hash = file_lookup_open(f, flags);
426 char buf[FIO_VERROR_SIZE];
429 if (errno == EPERM && (flags & FIO_O_NOATIME)) {
430 flags &= ~FIO_O_NOATIME;
434 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
436 td_verror(td, __e, buf);
439 if (!from_hash && f->fd != -1) {
440 if (add_file_hash(f)) {
444 * OK to ignore, we haven't done anything with it
446 ret = generic_close_file(td, f);
454 int generic_get_file_size(struct thread_data *td, struct fio_file *f)
456 return get_file_size(td, f);
460 * open/close all files, so that ->real_file_size gets set
462 static int get_file_sizes(struct thread_data *td)
468 for_each_file(td, f, i) {
469 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
472 if (td_io_get_file_size(td, f)) {
473 if (td->error != ENOENT) {
474 log_err("%s\n", td->verror);
480 if (f->real_file_size == -1ULL && td->o.size)
481 f->real_file_size = td->o.size / td->o.nr_files;
488 * Open the files and setup files sizes, creating files if necessary.
490 int setup_files(struct thread_data *td)
492 unsigned long long total_size, extend_size;
495 int err = 0, need_extend;
497 dprint(FD_FILE, "setup files\n");
499 if (td->o.read_iolog_file)
503 * if ioengine defines a setup() method, it's responsible for
504 * opening the files and setting f->real_file_size to indicate
505 * the valid range for that file.
507 if (td->io_ops->setup)
508 err = td->io_ops->setup(td);
510 err = get_file_sizes(td);
516 * check sizes. if the files/devices do not exist and the size
517 * isn't passed to fio, abort.
520 for_each_file(td, f, i) {
521 if (f->real_file_size == -1ULL)
524 total_size += f->real_file_size;
528 * device/file sizes are zero and no size given, punt
530 if ((!total_size || total_size == -1ULL) && !td->o.size &&
531 !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
532 log_err("%s: you need to specify size=\n", td->o.name);
533 td_verror(td, EINVAL, "total_file_size");
538 * now file sizes are known, so we can set ->io_size. if size= is
539 * not given, ->io_size is just equal to ->real_file_size. if size
540 * is given, ->io_size is size / nr_files.
542 extend_size = total_size = 0;
544 for_each_file(td, f, i) {
545 f->file_offset = td->o.start_offset;
547 if (!td->o.file_size_low) {
549 * no file size range given, file size is equal to
550 * total size divided by number of files. if that is
551 * zero, set it to the real file size.
553 f->io_size = td->o.size / td->o.nr_files;
555 f->io_size = f->real_file_size - f->file_offset;
556 } else if (f->real_file_size < td->o.file_size_low ||
557 f->real_file_size > td->o.file_size_high) {
558 if (f->file_offset > td->o.file_size_low)
561 * file size given. if it's fixed, use that. if it's a
562 * range, generate a random size in-between.
564 if (td->o.file_size_low == td->o.file_size_high) {
565 f->io_size = td->o.file_size_low
568 f->io_size = get_rand_file_size(td)
572 f->io_size = f->real_file_size - f->file_offset;
574 if (f->io_size == -1ULL)
577 total_size += f->io_size;
579 if (f->filetype == FIO_TYPE_FILE &&
580 (f->io_size + f->file_offset) > f->real_file_size &&
581 !(td->io_ops->flags & FIO_DISKLESSIO)) {
582 if (!td->o.create_on_open) {
584 extend_size += (f->io_size + f->file_offset);
586 f->real_file_size = f->io_size + f->file_offset;
587 fio_file_set_extend(f);
591 if (!td->o.size || td->o.size > total_size)
592 td->o.size = total_size;
595 * See if we need to extend some files
600 log_info("%s: Laying out IO file(s) (%u file(s) /"
601 " %LuMiB)\n", td->o.name, need_extend,
604 for_each_file(td, f, i) {
605 unsigned long long old_len = -1ULL, extend_len = -1ULL;
607 if (!fio_file_extend(f))
610 assert(f->filetype == FIO_TYPE_FILE);
611 fio_file_clear_extend(f);
612 if (!td->o.fill_device) {
613 old_len = f->real_file_size;
614 extend_len = f->io_size + f->file_offset - old_len;
616 f->real_file_size = (f->io_size + f->file_offset);
617 err = extend_file(td, f);
621 err = __file_invalidate_cache(td, f, old_len,
634 if (!td->o.zone_size)
635 td->o.zone_size = td->o.size;
638 * iolog already set the total io size, if we read back
641 if (!td->o.read_iolog_file)
642 td->total_io_size = td->o.size * td->o.loops;
645 log_err("%s: you need to specify valid offset=\n", td->o.name);
649 int pre_read_files(struct thread_data *td)
654 dprint(FD_FILE, "pre_read files\n");
656 for_each_file(td, f, i) {
657 pre_read_file(td, f);
663 int init_random_map(struct thread_data *td)
665 unsigned long long blocks, num_maps;
669 if (td->o.norandommap || !td_random(td))
672 for_each_file(td, f, i) {
673 blocks = (f->real_file_size + td->o.rw_min_bs - 1) /
674 (unsigned long long) td->o.rw_min_bs;
675 num_maps = (blocks + BLOCKS_PER_MAP - 1) /
676 (unsigned long long) BLOCKS_PER_MAP;
677 f->file_map = smalloc(num_maps * sizeof(int));
679 f->num_maps = num_maps;
682 if (!td->o.softrandommap) {
683 log_err("fio: failed allocating random map. If running"
684 " a large number of jobs, try the 'norandommap'"
685 " option or set 'softrandommap'. Or give"
686 " a larger --alloc-size to fio.\n");
690 log_info("fio: file %s failed allocating random map. Running "
691 "job without.\n", f->file_name);
698 void close_files(struct thread_data *td)
703 for_each_file(td, f, i)
704 td_io_close_file(td, f);
707 void close_and_free_files(struct thread_data *td)
712 dprint(FD_FILE, "close files\n");
714 for_each_file(td, f, i) {
715 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
716 dprint(FD_FILE, "free unlink %s\n", f->file_name);
717 unlink(f->file_name);
720 td_io_close_file(td, f);
733 td->o.filename = NULL;
740 static void get_file_type(struct fio_file *f)
744 if (!strcmp(f->file_name, "-"))
745 f->filetype = FIO_TYPE_PIPE;
747 f->filetype = FIO_TYPE_FILE;
749 if (!lstat(f->file_name, &sb)) {
750 if (S_ISBLK(sb.st_mode))
751 f->filetype = FIO_TYPE_BD;
752 else if (S_ISCHR(sb.st_mode))
753 f->filetype = FIO_TYPE_CHAR;
754 else if (S_ISFIFO(sb.st_mode))
755 f->filetype = FIO_TYPE_PIPE;
759 int add_file(struct thread_data *td, const char *fname)
761 int cur_files = td->files_index;
762 char file_name[PATH_MAX];
766 dprint(FD_FILE, "add file %s\n", fname);
768 f = smalloc(sizeof(*f));
770 log_err("fio: smalloc OOM\n");
776 if (td->files_size <= td->files_index) {
777 int new_size = td->o.nr_files + 1;
779 dprint(FD_FILE, "resize file array to %d files\n", new_size);
781 td->files = realloc(td->files, new_size * sizeof(f));
782 td->files_size = new_size;
784 td->files[cur_files] = f;
787 * init function, io engine may not be loaded yet
789 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
790 f->real_file_size = -1ULL;
793 len = sprintf(file_name, "%s/", td->o.directory);
795 sprintf(file_name + len, "%s", fname);
796 f->file_name = smalloc_strdup(file_name);
798 log_err("fio: smalloc OOM\n");
804 switch (td->o.file_lock_mode) {
807 case FILE_LOCK_READWRITE:
808 f->lock = fio_mutex_rw_init();
810 case FILE_LOCK_EXCLUSIVE:
811 f->lock = fio_mutex_init(1);
814 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
819 if (f->filetype == FIO_TYPE_FILE)
820 td->nr_normal_files++;
822 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
828 void get_file(struct fio_file *f)
830 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
831 assert(fio_file_open(f));
835 int put_file(struct thread_data *td, struct fio_file *f)
837 int f_ret = 0, ret = 0;
839 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
841 if (!fio_file_open(f))
844 assert(f->references);
848 if (should_fsync(td) && td->o.fsync_on_close)
849 f_ret = fsync(f->fd);
851 if (td->io_ops->close_file)
852 ret = td->io_ops->close_file(td, f);
858 fio_file_clear_open(f);
862 void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
864 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
867 if (f->lock_owner == td && f->lock_batch--)
870 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
871 if (ddir == DDIR_READ)
872 fio_mutex_down_read(f->lock);
874 fio_mutex_down_write(f->lock);
875 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
876 fio_mutex_down(f->lock);
879 f->lock_batch = td->o.lockfile_batch;
883 void unlock_file(struct thread_data *td, struct fio_file *f)
885 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
890 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
891 const int is_read = f->lock_ddir == DDIR_READ;
892 int val = fio_mutex_getval(f->lock);
894 if ((is_read && val == 1) || (!is_read && val == -1))
895 f->lock_owner = NULL;
898 fio_mutex_up_read(f->lock);
900 fio_mutex_up_write(f->lock);
901 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
902 int val = fio_mutex_getval(f->lock);
905 f->lock_owner = NULL;
907 fio_mutex_up(f->lock);
911 void unlock_file_all(struct thread_data *td, struct fio_file *f)
913 if (f->lock_owner != td)
920 static int recurse_dir(struct thread_data *td, const char *dirname)
926 D = opendir(dirname);
928 char buf[FIO_VERROR_SIZE];
930 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
931 td_verror(td, errno, buf);
935 while ((dir = readdir(D)) != NULL) {
936 char full_path[PATH_MAX];
939 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
942 sprintf(full_path, "%s/%s", dirname, dir->d_name);
944 if (lstat(full_path, &sb) == -1) {
945 if (errno != ENOENT) {
946 td_verror(td, errno, "stat");
951 if (S_ISREG(sb.st_mode)) {
952 add_file(td, full_path);
956 if (!S_ISDIR(sb.st_mode))
959 ret = recurse_dir(td, full_path);
968 int add_dir_files(struct thread_data *td, const char *path)
970 int ret = recurse_dir(td, path);
973 log_info("fio: opendir added %d files\n", td->o.nr_files);
978 void dup_files(struct thread_data *td, struct thread_data *org)
983 dprint(FD_FILE, "dup files: %d\n", org->files_index);
988 td->files = malloc(org->files_index * sizeof(f));
990 for_each_file(org, f, i) {
991 struct fio_file *__f;
993 __f = smalloc(sizeof(*__f));
995 log_err("fio: smalloc OOM\n");
1000 __f->file_name = smalloc_strdup(f->file_name);
1001 if (!__f->file_name) {
1002 log_err("fio: smalloc OOM\n");
1006 __f->filetype = f->filetype;
1014 * Returns the index that matches the filename, or -1 if not there
1016 int get_fileno(struct thread_data *td, const char *fname)
1021 for_each_file(td, f, i)
1022 if (!strcmp(f->file_name, fname))
1029 * For log usage, where we add/open/close files automatically
1031 void free_release_files(struct thread_data *td)
1034 td->files_index = 0;
1035 td->nr_normal_files = 0;