14 * Check if the file exists and it's large enough.
16 static int file_ok(struct thread_data *td, struct fio_file *f)
20 if (f->filetype != FIO_TYPE_FILE ||
21 (td->io_ops->flags & FIO_DISKLESSIO))
24 if (lstat(f->file_name, &st) == -1)
28 * if it's a special file, size is always ok for now
30 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))
32 if (st.st_size < (off_t) f->file_size)
38 static int create_file(struct thread_data *td, struct fio_file *f)
40 unsigned long long left;
45 f->fd = open(f->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
47 td_verror(td, errno, "open");
51 if (ftruncate(f->fd, f->file_size) == -1) {
52 td_verror(td, errno, "ftruncate");
56 if (posix_fallocate(f->fd, 0, f->file_size) < 0) {
57 td_verror(td, errno, "posix_fallocate");
61 b = malloc(td->o.max_bs[DDIR_WRITE]);
62 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
65 while (left && !td->terminate) {
66 bs = td->o.max_bs[DDIR_WRITE];
70 r = write(f->fd, b, bs);
77 td_verror(td, errno, "write");
79 td_verror(td, EIO, "write");
87 else if (td->o.create_fsync)
100 static unsigned long long set_rand_file_size(struct thread_data *td,
101 unsigned long long total_size)
103 unsigned long long upper = total_size;
104 unsigned long long ret;
107 if (upper > td->o.file_size_high)
108 upper = td->o.file_size_high;
109 else if (upper < td->o.file_size_low)
114 r = os_random_long(&td->file_size_state);
115 ret = td->o.file_size_low + (unsigned long long) ((double) upper * (r / (RAND_MAX + 1.0)));
116 ret -= (ret % td->o.rw_min_bs);
122 static int fill_file_size(struct thread_data *td, struct fio_file *f,
123 unsigned long long *file_size, int new_files)
125 if (!td->o.file_size_low) {
126 f->file_size = *file_size / new_files;
127 f->real_file_size = f->file_size;
130 * If we don't have enough space left for a file
131 * of the minimum size, bail.
133 if (*file_size < td->o.file_size_low)
136 f->file_size = set_rand_file_size(td, *file_size);
137 f->real_file_size = f->file_size;
138 *file_size -= f->file_size;
144 static int create_files(struct thread_data *td)
147 int err, need_create, can_extend;
148 unsigned long long total_file_size, local_file_size, create_size;
149 unsigned int i, new_files;
152 total_file_size = td->o.size;
153 for_each_file(td, f, i) {
154 unsigned long long s;
156 f->file_offset = td->o.start_offset;
158 if (!total_file_size)
161 if (f->filetype != FIO_TYPE_FILE) {
163 f->file_size = total_file_size / td->o.nr_files;
167 if (f->flags & FIO_FILE_EXISTS) {
168 if ((f->file_size > td->o.size / td->o.nr_files) ||
170 f->file_size = td->o.size / td->o.nr_files;
173 if (s > total_file_size)
176 total_file_size -= s;
182 * unless specifically asked for overwrite, let normal io extend it
184 can_extend = !td->o.overwrite && !(td->io_ops->flags & FIO_NOEXTEND);
185 if (can_extend && new_files) {
186 for_each_file(td, f, i) {
187 if (fill_file_size(td, f, &total_file_size, new_files)) {
188 log_info("fio: limited to %d files\n", i);
197 local_file_size = total_file_size;
198 if (!local_file_size)
199 local_file_size = -1;
204 for_each_file(td, f, i) {
207 if (f->filetype != FIO_TYPE_FILE)
209 if (f->flags & FIO_FILE_EXISTS) {
210 total_file_size += f->file_size;
214 if (fill_file_size(td, f, &local_file_size, new_files)) {
215 log_info("fio: limited to %d files\n", i);
216 new_files -= (td->o.nr_files - i);
221 total_file_size += f->file_size;
222 create_size += f->file_size;
223 file_there = !file_ok(td, f);
225 if (file_there && td_write(td) && !td->o.overwrite) {
226 unlink(f->file_name);
230 need_create += !file_there;
236 if (!td->o.size && !total_file_size) {
237 log_err("Need size for create\n");
238 td_verror(td, EINVAL, "file_size");
243 log_info("%s: Laying out IO file(s) (%u files / %LuMiB)\n",
244 td->o.name, new_files, create_size >> 20);
247 for_each_file(td, f, i) {
249 * Only unlink files that we created.
251 f->flags &= ~FIO_FILE_UNLINK;
252 if (file_ok(td, f)) {
254 f->flags |= FIO_FILE_UNLINK;
256 f->flags |= FIO_FILE_NOSORT;
257 err = create_file(td, f);
267 static int file_size(struct thread_data *td, struct fio_file *f)
271 if (td->o.overwrite) {
272 if (fstat(f->fd, &st) == -1) {
273 td_verror(td, errno, "fstat");
277 f->real_file_size = st.st_size;
279 if (!f->file_size || f->file_size > f->real_file_size)
280 f->file_size = f->real_file_size;
282 f->real_file_size = f->file_size;
287 static int bdev_size(struct thread_data *td, struct fio_file *f)
289 unsigned long long bytes;
292 r = blockdev_size(f->fd, &bytes);
294 td_verror(td, r, "blockdev_size");
298 f->real_file_size = bytes;
301 * no extend possibilities, so limit size to device size if too large
303 if (!f->file_size || f->file_size > f->real_file_size)
304 f->file_size = f->real_file_size;
306 f->file_size -= f->file_offset;
310 static int get_file_size(struct thread_data *td, struct fio_file *f)
314 if (f->filetype == FIO_TYPE_FILE) {
315 if (!(f->flags & FIO_FILE_EXISTS))
316 ret = file_size(td, f);
317 } else if (f->filetype == FIO_TYPE_BD)
318 ret = bdev_size(td, f);
320 f->real_file_size = -1;
325 if (f->file_offset > f->real_file_size) {
326 log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name, f->file_offset, f->real_file_size);
333 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
341 * FIXME: add blockdev flushing too
344 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
345 else if (f->filetype == FIO_TYPE_FILE)
346 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
347 else if (f->filetype == FIO_TYPE_BD) {
348 ret = blockdev_invalidate_cache(f->fd);
349 if (ret < 0 && errno == EACCES && geteuid()) {
350 log_err("fio: only root may flush block devices. Cache flush bypassed!\n");
353 } else if (f->filetype == FIO_TYPE_CHAR)
357 td_verror(td, errno, "invalidate_cache");
364 void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
370 int generic_open_file(struct thread_data *td, struct fio_file *f)
375 flags |= OS_O_DIRECT;
379 if (td_write(td) || td_rw(td)) {
382 if (f->filetype == FIO_TYPE_FILE)
385 f->fd = open(f->file_name, flags, 0600);
387 if (f->filetype == FIO_TYPE_CHAR)
392 f->fd = open(f->file_name, flags);
396 char buf[FIO_VERROR_SIZE];
399 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
401 td_verror(td, __e, buf);
402 if (__e == EINVAL && td->o.odirect)
403 log_err("fio: destination does not support O_DIRECT\n");
405 log_err("fio: try reducing/setting openfiles (failed at %u of %u)\n", td->nr_open_files, td->o.nr_files);
409 if (get_file_size(td, f))
412 if (td->o.invalidate_cache && file_invalidate_cache(td, f))
415 if (td->o.fadvise_hint) {
417 flags = POSIX_FADV_RANDOM;
419 flags = POSIX_FADV_SEQUENTIAL;
421 if (fadvise(f->fd, f->file_offset, f->file_size, flags) < 0) {
422 td_verror(td, errno, "fadvise");
433 int open_files(struct thread_data *td)
439 for_each_file(td, f, i) {
440 err = td_io_open_file(td, f);
444 if (td->o.open_files == td->nr_open_files)
451 for_each_file(td, f, i)
452 td_io_close_file(td, f);
457 int setup_files(struct thread_data *td)
464 * if ioengine defines a setup() method, it's responsible for
465 * setting up everything in the td->files[] area.
467 if (td->io_ops->setup)
468 return td->io_ops->setup(td);
470 if (create_files(td))
473 err = open_files(td);
478 * Recalculate the total file size now that files are set up.
481 for_each_file(td, f, i)
482 td->o.size += f->file_size;
484 td->io_size = td->o.size;
485 if (td->io_size == 0) {
486 log_err("%s: no io blocks\n", td->o.name);
487 td_verror(td, EINVAL, "total_file_size");
491 if (!td->o.zone_size)
492 td->o.zone_size = td->io_size;
494 td->total_io_size = td->io_size * td->o.loops;
496 for_each_file(td, f, i)
497 td_io_close_file(td, f);
502 int init_random_map(struct thread_data *td)
504 int num_maps, blocks;
508 if (td->o.norandommap)
511 for_each_file(td, f, i) {
512 blocks = (f->real_file_size + td->o.rw_min_bs - 1) / td->o.rw_min_bs;
513 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
514 f->file_map = malloc(num_maps * sizeof(long));
516 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
519 f->num_maps = num_maps;
520 memset(f->file_map, 0, num_maps * sizeof(long));
526 void close_files(struct thread_data *td)
531 for_each_file(td, f, i) {
532 if ((f->flags & FIO_FILE_UNLINK) &&
533 f->filetype == FIO_TYPE_FILE)
534 unlink(f->file_name);
536 td_io_close_file(td, f);
547 td->o.filename = NULL;
553 static void get_file_type(struct fio_file *f)
557 f->filetype = FIO_TYPE_FILE;
559 if (!lstat(f->file_name, &sb)) {
560 f->flags |= FIO_FILE_EXISTS;
562 if (S_ISBLK(sb.st_mode))
563 f->filetype = FIO_TYPE_BD;
564 else if (S_ISCHR(sb.st_mode))
565 f->filetype = FIO_TYPE_CHAR;
568 * might as well do this here, and save a stat later on
570 f->real_file_size = sb.st_size;
571 f->file_size = f->real_file_size;
576 void add_file(struct thread_data *td, const char *fname)
578 int cur_files = td->files_index;
579 char file_name[PATH_MAX];
583 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
585 f = &td->files[cur_files];
586 memset(f, 0, sizeof(*f));
590 len = sprintf(file_name, "%s/", td->o.directory);
592 sprintf(file_name + len, "%s", fname);
593 f->file_name = strdup(file_name);
598 if (f->filetype == FIO_TYPE_FILE)
599 td->nr_normal_files++;
602 void get_file(struct fio_file *f)
607 void put_file(struct thread_data *td, struct fio_file *f)
609 if (!(f->flags & FIO_FILE_OPEN))
612 assert(f->references);
616 if (should_fsync(td) && td->o.fsync_on_close)
619 if (td->io_ops->close_file)
620 td->io_ops->close_file(td, f);
622 f->flags &= ~FIO_FILE_OPEN;
625 static int recurse_dir(struct thread_data *td, const char *dirname)
631 D = opendir(dirname);
633 td_verror(td, errno, "opendir");
637 while ((dir = readdir(D)) != NULL) {
638 char full_path[PATH_MAX];
641 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
644 sprintf(full_path, "%s/%s", dirname, dir->d_name);
646 if (lstat(full_path, &sb) == -1) {
647 if (errno != ENOENT) {
648 td_verror(td, errno, "stat");
653 if (S_ISREG(sb.st_mode)) {
654 add_file(td, full_path);
659 if ((ret = recurse_dir(td, full_path)) != 0)
667 int add_dir_files(struct thread_data *td, const char *path)
669 return recurse_dir(td, path);
672 void dup_files(struct thread_data *td, struct thread_data *org)
681 bytes = org->files_index * sizeof(*f);
682 td->files = malloc(bytes);
683 memcpy(td->files, org->files, bytes);
685 for_each_file(td, f, i) {
687 f->file_name = strdup(f->file_name);