14 static int extend_file(struct thread_data *td, struct fio_file *f)
16 int r, new_layout = 0, unlink_file = 0, flags;
17 unsigned long long left;
22 log_err("fio: refusing extend of file due to read-only\n");
27 * check if we need to lay the file out complete again. fio
28 * does that for operations involving reads, or for writes
29 * where overwrite is set
31 if (td_read(td) || (td_write(td) && td->o.overwrite))
33 if (td_write(td) && !td->o.overwrite)
36 if ((unlink_file || new_layout) && (f->flags & FIO_FILE_EXISTS)) {
37 if (unlink(f->file_name) < 0) {
38 td_verror(td, errno, "unlink");
43 flags = O_WRONLY | O_CREAT;
47 f->fd = open(f->file_name, flags, 0644);
49 td_verror(td, errno, "open");
53 if (ftruncate(f->fd, f->real_file_size) == -1) {
54 td_verror(td, errno, "ftruncate");
61 if (posix_fallocate(f->fd, 0, f->real_file_size) < 0) {
62 td_verror(td, errno, "posix_fallocate");
66 b = malloc(td->o.max_bs[DDIR_WRITE]);
67 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
69 left = f->real_file_size;
70 while (left && !td->terminate) {
71 bs = td->o.max_bs[DDIR_WRITE];
75 r = write(f->fd, b, bs);
82 td_verror(td, errno, "write");
84 td_verror(td, EIO, "write");
92 else if (td->o.create_fsync)
106 static unsigned long long get_rand_file_size(struct thread_data *td)
108 unsigned long long ret;
111 r = os_random_long(&td->file_size_state);
112 ret = td->o.file_size_low + (unsigned long long) ((double) (td->o.file_size_high - td->o.file_size_low) * (r / (RAND_MAX + 1.0)));
113 ret -= (ret % td->o.rw_min_bs);
117 static int file_size(struct thread_data *td, struct fio_file *f)
121 if (fstat(f->fd, &st) == -1) {
122 td_verror(td, errno, "fstat");
126 f->real_file_size = st.st_size;
130 static int bdev_size(struct thread_data *td, struct fio_file *f)
132 unsigned long long bytes;
135 r = blockdev_size(f->fd, &bytes);
137 td_verror(td, r, "blockdev_size");
141 f->real_file_size = bytes;
145 static int get_file_size(struct thread_data *td, struct fio_file *f)
149 if (f->flags & FIO_SIZE_KNOWN)
152 if (f->filetype == FIO_TYPE_FILE)
153 ret = file_size(td, f);
154 else if (f->filetype == FIO_TYPE_BD)
155 ret = bdev_size(td, f);
157 f->real_file_size = -1;
162 if (f->file_offset > f->real_file_size) {
163 log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name, f->file_offset, f->real_file_size);
167 f->flags |= FIO_SIZE_KNOWN;
171 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
179 * FIXME: add blockdev flushing too
182 ret = madvise(f->mmap, f->io_size, MADV_DONTNEED);
183 else if (f->filetype == FIO_TYPE_FILE)
184 ret = fadvise(f->fd, f->file_offset, f->io_size, POSIX_FADV_DONTNEED);
185 else if (f->filetype == FIO_TYPE_BD) {
186 ret = blockdev_invalidate_cache(f->fd);
187 if (ret < 0 && errno == EACCES && geteuid()) {
189 log_err("fio: only root may flush block devices. Cache flush bypassed!\n");
194 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
198 td_verror(td, errno, "invalidate_cache");
205 void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
211 int generic_open_file(struct thread_data *td, struct fio_file *f)
216 if (!strcmp(f->file_name, "-")) {
218 log_err("fio: can't read/write to stdin/out\n");
224 * move output logging to stderr, if we are writing to stdout
231 flags |= OS_O_DIRECT;
234 if (f->filetype != FIO_TYPE_FILE)
242 if (f->filetype == FIO_TYPE_FILE)
246 f->fd = dup(STDOUT_FILENO);
248 f->fd = open(f->file_name, flags, 0600);
250 if (f->filetype == FIO_TYPE_CHAR && !read_only)
256 f->fd = dup(STDIN_FILENO);
258 f->fd = open(f->file_name, flags);
262 char buf[FIO_VERROR_SIZE];
265 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
267 td_verror(td, __e, buf);
270 if (get_file_size(td, f))
279 int open_files(struct thread_data *td)
285 for_each_file(td, f, i) {
286 err = td_io_open_file(td, f);
288 if (td->error == EMFILE) {
289 log_err("fio: limited open files to: %d\n", td->nr_open_files);
290 td->o.open_files = td->nr_open_files;
297 if (td->o.open_files == td->nr_open_files)
304 for_each_file(td, f, i)
305 td_io_close_file(td, f);
311 * open/close all files, so that ->real_file_size gets set
313 static int get_file_sizes(struct thread_data *td)
319 for_each_file(td, f, i) {
320 if (td->io_ops->open_file(td, f)) {
321 if (td->error != ENOENT) {
322 log_err("%s\n", td->verror);
327 if (td->io_ops->close_file)
328 td->io_ops->close_file(td, f);
331 if (f->real_file_size == -1ULL && td->o.size)
332 f->real_file_size = td->o.size / td->o.nr_files;
339 * Open the files and setup files sizes, creating files if necessary.
341 int setup_files(struct thread_data *td)
343 unsigned long long total_size, extend_size;
346 int err = 0, need_extend;
349 * if ioengine defines a setup() method, it's responsible for
350 * opening the files and setting f->real_file_size to indicate
351 * the valid range for that file.
353 if (td->io_ops->setup)
354 err = td->io_ops->setup(td);
356 err = get_file_sizes(td);
362 * check sizes. if the files/devices do not exist and the size
363 * isn't passed to fio, abort.
366 for_each_file(td, f, i) {
367 if (f->real_file_size == -1ULL)
370 total_size += f->real_file_size;
374 * device/file sizes are zero and no size given, punt
376 if ((!total_size || total_size == -1ULL) && !td->o.size &&
377 !(td->io_ops->flags & FIO_NOIO)) {
378 log_err("%s: you need to specify size=\n", td->o.name);
379 td_verror(td, EINVAL, "total_file_size");
384 * now file sizes are known, so we can set ->io_size. if size= is
385 * not given, ->io_size is just equal to ->real_file_size. if size
386 * is given, ->io_size is size / nr_files.
388 extend_size = total_size = 0;
390 for_each_file(td, f, i) {
391 f->file_offset = td->o.start_offset;
393 if (!td->o.file_size_low) {
395 * no file size range given, file size is equal to
396 * total size divided by number of files. if that is
397 * zero, set it to the real file size.
399 f->io_size = td->o.size / td->o.nr_files;
401 if (f->file_offset > f->real_file_size)
403 f->io_size = f->real_file_size - f->file_offset;
405 } else if (f->real_file_size < td->o.file_size_low ||
406 f->real_file_size > td->o.file_size_high) {
407 if (f->file_offset > td->o.file_size_low)
410 * file size given. if it's fixed, use that. if it's a
411 * range, generate a random size in-between.
413 if (td->o.file_size_low == td->o.file_size_high)
414 f->io_size = td->o.file_size_low - f->file_offset;
416 f->io_size = get_rand_file_size(td) - f->file_offset;
417 } else if (f->file_offset > f->real_file_size)
420 f->io_size = f->real_file_size - f->file_offset;
422 if (f->io_size == -1ULL)
425 total_size += f->io_size;
427 if (f->filetype == FIO_TYPE_FILE &&
428 (f->io_size + f->file_offset) > f->real_file_size &&
429 !(td->io_ops->flags & FIO_DISKLESSIO)) {
431 extend_size += (f->io_size + f->file_offset);
432 f->flags |= FIO_FILE_EXTEND;
436 if (!td->o.size || td->o.size > total_size)
437 td->o.size = total_size;
440 * See if we need to extend some files
444 log_info("%s: Laying out IO file(s) (%u file(s) / %LuMiB)\n",
445 td->o.name, need_extend, extend_size >> 20);
447 for_each_file(td, f, i) {
448 if (!(f->flags & FIO_FILE_EXTEND))
451 assert(f->filetype == FIO_TYPE_FILE);
452 f->flags &= ~FIO_FILE_EXTEND;
453 f->real_file_size = (f->io_size + f->file_offset);
454 err = extend_file(td, f);
464 if (!td->o.zone_size)
465 td->o.zone_size = td->o.size;
468 * iolog already set the total io size, if we read back
471 if (!td->o.read_iolog_file)
472 td->total_io_size = td->o.size * td->o.loops;
475 log_err("%s: you need to specify valid offset=\n", td->o.name);
479 int init_random_map(struct thread_data *td)
481 int num_maps, blocks;
485 if (td->o.norandommap)
488 for_each_file(td, f, i) {
489 blocks = (f->real_file_size + td->o.rw_min_bs - 1) / td->o.rw_min_bs;
490 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
491 f->file_map = malloc(num_maps * sizeof(long));
493 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
496 f->num_maps = num_maps;
497 memset(f->file_map, 0, num_maps * sizeof(long));
503 void close_files(struct thread_data *td)
508 for_each_file(td, f, i) {
509 if (td->o.unlink && f->filetype == FIO_TYPE_FILE)
510 unlink(f->file_name);
512 td_io_close_file(td, f);
523 td->o.filename = NULL;
529 static void get_file_type(struct fio_file *f)
533 if (!strcmp(f->file_name, "-"))
534 f->filetype = FIO_TYPE_PIPE;
536 f->filetype = FIO_TYPE_FILE;
538 if (!lstat(f->file_name, &sb)) {
539 if (S_ISBLK(sb.st_mode))
540 f->filetype = FIO_TYPE_BD;
541 else if (S_ISCHR(sb.st_mode))
542 f->filetype = FIO_TYPE_CHAR;
543 else if (S_ISFIFO(sb.st_mode))
544 f->filetype = FIO_TYPE_PIPE;
548 int add_file(struct thread_data *td, const char *fname)
550 int cur_files = td->files_index;
551 char file_name[PATH_MAX];
555 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
557 f = &td->files[cur_files];
558 memset(f, 0, sizeof(*f));
562 * init function, io engine may not be loaded yet
564 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
565 f->real_file_size = -1ULL;
568 len = sprintf(file_name, "%s/", td->o.directory);
570 sprintf(file_name + len, "%s", fname);
571 f->file_name = strdup(file_name);
576 if (f->filetype == FIO_TYPE_FILE)
577 td->nr_normal_files++;
582 void get_file(struct fio_file *f)
584 assert(f->flags & FIO_FILE_OPEN);
588 void put_file(struct thread_data *td, struct fio_file *f)
590 if (!(f->flags & FIO_FILE_OPEN))
593 assert(f->references);
597 if (should_fsync(td) && td->o.fsync_on_close)
600 if (td->io_ops->close_file)
601 td->io_ops->close_file(td, f);
604 f->flags &= ~FIO_FILE_OPEN;
607 static int recurse_dir(struct thread_data *td, const char *dirname)
613 D = opendir(dirname);
615 char buf[FIO_VERROR_SIZE];
617 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
618 td_verror(td, errno, buf);
622 while ((dir = readdir(D)) != NULL) {
623 char full_path[PATH_MAX];
626 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
629 sprintf(full_path, "%s/%s", dirname, dir->d_name);
631 if (lstat(full_path, &sb) == -1) {
632 if (errno != ENOENT) {
633 td_verror(td, errno, "stat");
638 if (S_ISREG(sb.st_mode)) {
639 add_file(td, full_path);
643 if (!S_ISDIR(sb.st_mode))
646 if ((ret = recurse_dir(td, full_path)) != 0)
654 int add_dir_files(struct thread_data *td, const char *path)
656 int ret = recurse_dir(td, path);
659 log_info("fio: opendir added %d files\n", td->o.nr_files);
664 void dup_files(struct thread_data *td, struct thread_data *org)
673 bytes = org->files_index * sizeof(*f);
674 td->files = malloc(bytes);
675 memcpy(td->files, org->files, bytes);
677 for_each_file(td, f, i) {
679 f->file_name = strdup(f->file_name);
684 * Returns the index that matches the filename, or -1 if not there
686 int get_fileno(struct thread_data *td, const char *fname)
691 for_each_file(td, f, i)
692 if (!strcmp(f->file_name, fname))
699 * For log usage, where we add/open/close files automatically
701 void free_release_files(struct thread_data *td)
705 td->nr_normal_files = 0;