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 else if (f->filetype == FIO_TYPE_CHAR)
353 td_verror(td, errno, "invalidate_cache");
360 void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
366 int generic_open_file(struct thread_data *td, struct fio_file *f)
371 flags |= OS_O_DIRECT;
375 if (td_write(td) || td_rw(td)) {
378 if (f->filetype == FIO_TYPE_FILE)
381 f->fd = open(f->file_name, flags, 0600);
383 if (f->filetype == FIO_TYPE_CHAR)
388 f->fd = open(f->file_name, flags);
392 char buf[FIO_VERROR_SIZE];
395 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
397 td_verror(td, __e, buf);
398 if (__e == EINVAL && td->o.odirect)
399 log_err("fio: destination does not support O_DIRECT\n");
401 log_err("fio: try reducing/setting openfiles (failed at %u of %u)\n", td->nr_open_files, td->o.nr_files);
405 if (get_file_size(td, f))
408 if (td->o.invalidate_cache && file_invalidate_cache(td, f))
411 if (!td->o.fadvise_hint)
414 if (!td_random(td)) {
415 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
416 td_verror(td, errno, "fadvise");
420 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
421 td_verror(td, errno, "fadvise");
432 int open_files(struct thread_data *td)
438 for_each_file(td, f, i) {
439 err = td_io_open_file(td, f);
443 if (td->o.open_files == td->nr_open_files)
450 for_each_file(td, f, i)
451 td_io_close_file(td, f);
456 int setup_files(struct thread_data *td)
463 * if ioengine defines a setup() method, it's responsible for
464 * setting up everything in the td->files[] area.
466 if (td->io_ops->setup)
467 return td->io_ops->setup(td);
469 if (create_files(td))
472 err = open_files(td);
477 * Recalculate the total file size now that files are set up.
480 for_each_file(td, f, i)
481 td->o.size += f->file_size;
483 td->io_size = td->o.size;
484 if (td->io_size == 0) {
485 log_err("%s: no io blocks\n", td->o.name);
486 td_verror(td, EINVAL, "total_file_size");
490 if (!td->o.zone_size)
491 td->o.zone_size = td->io_size;
493 td->total_io_size = td->io_size * td->o.loops;
495 for_each_file(td, f, i)
496 td_io_close_file(td, f);
501 int init_random_map(struct thread_data *td)
503 int num_maps, blocks;
507 if (td->o.norandommap)
510 for_each_file(td, f, i) {
511 blocks = (f->real_file_size + td->o.rw_min_bs - 1) / td->o.rw_min_bs;
512 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
513 f->file_map = malloc(num_maps * sizeof(long));
515 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
518 f->num_maps = num_maps;
519 memset(f->file_map, 0, num_maps * sizeof(long));
525 void close_files(struct thread_data *td)
530 for_each_file(td, f, i) {
531 if ((f->flags & FIO_FILE_UNLINK) &&
532 f->filetype == FIO_TYPE_FILE)
533 unlink(f->file_name);
535 td_io_close_file(td, f);
546 td->o.filename = NULL;
552 static void get_file_type(struct fio_file *f)
556 f->filetype = FIO_TYPE_FILE;
558 if (!lstat(f->file_name, &sb)) {
559 f->flags |= FIO_FILE_EXISTS;
561 if (S_ISBLK(sb.st_mode))
562 f->filetype = FIO_TYPE_BD;
563 else if (S_ISCHR(sb.st_mode))
564 f->filetype = FIO_TYPE_CHAR;
567 * might as well do this here, and save a stat later on
569 f->real_file_size = sb.st_size;
570 f->file_size = f->real_file_size;
575 void add_file(struct thread_data *td, const char *fname)
577 int cur_files = td->files_index;
578 char file_name[PATH_MAX];
582 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
584 f = &td->files[cur_files];
585 memset(f, 0, sizeof(*f));
589 len = sprintf(file_name, "%s/", td->o.directory);
591 sprintf(file_name + len, "%s", fname);
592 f->file_name = strdup(file_name);
597 if (f->filetype == FIO_TYPE_FILE)
598 td->nr_normal_files++;
601 void get_file(struct fio_file *f)
606 void put_file(struct thread_data *td, struct fio_file *f)
608 if (!(f->flags & FIO_FILE_OPEN))
611 assert(f->references);
615 if (should_fsync(td) && td->o.fsync_on_close)
618 if (td->io_ops->close_file)
619 td->io_ops->close_file(td, f);
621 f->flags &= ~FIO_FILE_OPEN;
624 static int recurse_dir(struct thread_data *td, const char *dirname)
630 D = opendir(dirname);
632 td_verror(td, errno, "opendir");
636 while ((dir = readdir(D)) != NULL) {
637 char full_path[PATH_MAX];
640 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
643 sprintf(full_path, "%s/%s", dirname, dir->d_name);
645 if (lstat(full_path, &sb) == -1) {
646 if (errno != ENOENT) {
647 td_verror(td, errno, "stat");
652 if (S_ISREG(sb.st_mode)) {
653 add_file(td, full_path);
658 if ((ret = recurse_dir(td, full_path)) != 0)
666 int add_dir_files(struct thread_data *td, const char *path)
668 return recurse_dir(td, path);
671 void dup_files(struct thread_data *td, struct thread_data *org)
680 bytes = org->files_index * sizeof(*f);
681 td->files = malloc(bytes);
682 memcpy(td->files, org->files, bytes);
684 for_each_file(td, f, i) {
686 f->file_name = strdup(f->file_name);