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 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
48 f->fd = open(f->file_name, flags, 0644);
50 td_verror(td, errno, "open");
57 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
59 if (ftruncate(f->fd, f->real_file_size) == -1) {
60 td_verror(td, errno, "ftruncate");
64 dprint(FD_FILE, "fallocate file %s, size %llu\n", f->file_name,
66 if (posix_fallocate(f->fd, 0, f->real_file_size) < 0) {
67 td_verror(td, errno, "posix_fallocate");
71 b = malloc(td->o.max_bs[DDIR_WRITE]);
72 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
74 left = f->real_file_size;
75 while (left && !td->terminate) {
76 bs = td->o.max_bs[DDIR_WRITE];
80 r = write(f->fd, b, bs);
87 td_verror(td, errno, "write");
89 td_verror(td, EIO, "write");
97 else if (td->o.create_fsync)
111 static unsigned long long get_rand_file_size(struct thread_data *td)
113 unsigned long long ret;
116 r = os_random_long(&td->file_size_state);
117 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)));
118 ret -= (ret % td->o.rw_min_bs);
122 static int file_size(struct thread_data *td, struct fio_file *f)
126 if (fstat(f->fd, &st) == -1) {
127 td_verror(td, errno, "fstat");
131 f->real_file_size = st.st_size;
135 static int bdev_size(struct thread_data *td, struct fio_file *f)
137 unsigned long long bytes;
140 r = blockdev_size(f->fd, &bytes);
142 td_verror(td, r, "blockdev_size");
146 f->real_file_size = bytes;
150 static int get_file_size(struct thread_data *td, struct fio_file *f)
154 if (f->flags & FIO_SIZE_KNOWN)
157 if (f->filetype == FIO_TYPE_FILE)
158 ret = file_size(td, f);
159 else if (f->filetype == FIO_TYPE_BD)
160 ret = bdev_size(td, f);
162 f->real_file_size = -1;
167 if (f->file_offset > f->real_file_size) {
168 log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name, f->file_offset, f->real_file_size);
172 f->flags |= FIO_SIZE_KNOWN;
176 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
180 dprint(FD_IO, "invalidate cache (%d)\n", td->o.odirect);
186 * FIXME: add blockdev flushing too
189 ret = madvise(f->mmap, f->io_size, MADV_DONTNEED);
190 else if (f->filetype == FIO_TYPE_FILE)
191 ret = fadvise(f->fd, f->file_offset, f->io_size, POSIX_FADV_DONTNEED);
192 else if (f->filetype == FIO_TYPE_BD) {
193 ret = blockdev_invalidate_cache(f->fd);
194 if (ret < 0 && errno == EACCES && geteuid()) {
196 log_err("fio: only root may flush block devices. Cache flush bypassed!\n");
201 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
205 td_verror(td, errno, "invalidate_cache");
212 void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
214 dprint(FD_FILE, "fd close %s\n", f->file_name);
219 int generic_open_file(struct thread_data *td, struct fio_file *f)
224 dprint(FD_FILE, "fd open %s\n", f->file_name);
226 if (!strcmp(f->file_name, "-")) {
228 log_err("fio: can't read/write to stdin/out\n");
234 * move output logging to stderr, if we are writing to stdout
241 flags |= OS_O_DIRECT;
244 if (f->filetype != FIO_TYPE_FILE)
253 if (f->filetype == FIO_TYPE_FILE)
257 f->fd = dup(STDOUT_FILENO);
259 f->fd = open(f->file_name, flags, 0600);
261 if (f->filetype == FIO_TYPE_CHAR && !read_only)
267 f->fd = dup(STDIN_FILENO);
269 f->fd = open(f->file_name, flags);
273 char buf[FIO_VERROR_SIZE];
276 if (errno == EPERM && (flags & O_NOATIME)) {
281 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
283 td_verror(td, __e, buf);
286 if (get_file_size(td, f))
295 int open_files(struct thread_data *td)
301 dprint(FD_FILE, "open files\n");
303 for_each_file(td, f, i) {
304 err = td_io_open_file(td, f);
306 if (td->error == EMFILE) {
307 log_err("fio: limited open files to: %d\n", td->nr_open_files);
308 td->o.open_files = td->nr_open_files;
315 if (td->o.open_files == td->nr_open_files)
322 for_each_file(td, f, i)
323 td_io_close_file(td, f);
329 * open/close all files, so that ->real_file_size gets set
331 static int get_file_sizes(struct thread_data *td)
337 for_each_file(td, f, i) {
338 if (td->io_ops->open_file(td, f)) {
339 if (td->error != ENOENT) {
340 log_err("%s\n", td->verror);
345 if (td->io_ops->close_file)
346 td->io_ops->close_file(td, f);
349 if (f->real_file_size == -1ULL && td->o.size)
350 f->real_file_size = td->o.size / td->o.nr_files;
357 * Open the files and setup files sizes, creating files if necessary.
359 int setup_files(struct thread_data *td)
361 unsigned long long total_size, extend_size;
364 int err = 0, need_extend;
366 dprint(FD_FILE, "setup files\n");
369 * if ioengine defines a setup() method, it's responsible for
370 * opening the files and setting f->real_file_size to indicate
371 * the valid range for that file.
373 if (td->io_ops->setup)
374 err = td->io_ops->setup(td);
376 err = get_file_sizes(td);
382 * check sizes. if the files/devices do not exist and the size
383 * isn't passed to fio, abort.
386 for_each_file(td, f, i) {
387 if (f->real_file_size == -1ULL)
390 total_size += f->real_file_size;
394 * device/file sizes are zero and no size given, punt
396 if ((!total_size || total_size == -1ULL) && !td->o.size &&
397 !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
398 log_err("%s: you need to specify size=\n", td->o.name);
399 td_verror(td, EINVAL, "total_file_size");
404 * now file sizes are known, so we can set ->io_size. if size= is
405 * not given, ->io_size is just equal to ->real_file_size. if size
406 * is given, ->io_size is size / nr_files.
408 extend_size = total_size = 0;
410 for_each_file(td, f, i) {
411 f->file_offset = td->o.start_offset;
413 if (!td->o.file_size_low) {
415 * no file size range given, file size is equal to
416 * total size divided by number of files. if that is
417 * zero, set it to the real file size.
419 f->io_size = td->o.size / td->o.nr_files;
421 f->io_size = f->real_file_size - f->file_offset;
422 } else if (f->real_file_size < td->o.file_size_low ||
423 f->real_file_size > td->o.file_size_high) {
424 if (f->file_offset > td->o.file_size_low)
427 * file size given. if it's fixed, use that. if it's a
428 * range, generate a random size in-between.
430 if (td->o.file_size_low == td->o.file_size_high)
431 f->io_size = td->o.file_size_low - f->file_offset;
433 f->io_size = get_rand_file_size(td) - f->file_offset;
435 f->io_size = f->real_file_size - f->file_offset;
437 if (f->io_size == -1ULL)
440 total_size += f->io_size;
442 if (f->filetype == FIO_TYPE_FILE &&
443 (f->io_size + f->file_offset) > f->real_file_size &&
444 !(td->io_ops->flags & FIO_DISKLESSIO)) {
446 extend_size += (f->io_size + f->file_offset);
447 f->flags |= FIO_FILE_EXTEND;
451 if (!td->o.size || td->o.size > total_size)
452 td->o.size = total_size;
455 * See if we need to extend some files
459 log_info("%s: Laying out IO file(s) (%u file(s) / %LuMiB)\n",
460 td->o.name, need_extend, extend_size >> 20);
462 for_each_file(td, f, i) {
463 if (!(f->flags & FIO_FILE_EXTEND))
466 assert(f->filetype == FIO_TYPE_FILE);
467 f->flags &= ~FIO_FILE_EXTEND;
468 f->real_file_size = (f->io_size + f->file_offset);
469 err = extend_file(td, f);
479 if (!td->o.zone_size)
480 td->o.zone_size = td->o.size;
483 * iolog already set the total io size, if we read back
486 if (!td->o.read_iolog_file)
487 td->total_io_size = td->o.size * td->o.loops;
490 log_err("%s: you need to specify valid offset=\n", td->o.name);
494 int init_random_map(struct thread_data *td)
496 unsigned long long blocks, num_maps;
500 if (td->o.norandommap)
503 for_each_file(td, f, i) {
504 blocks = (f->real_file_size + td->o.rw_min_bs - 1) / (unsigned long long) td->o.rw_min_bs;
505 num_maps = (blocks + BLOCKS_PER_MAP-1)/ (unsigned long long) BLOCKS_PER_MAP;
506 f->file_map = malloc(num_maps * sizeof(long));
508 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
511 f->num_maps = num_maps;
512 memset(f->file_map, 0, num_maps * sizeof(long));
518 void close_files(struct thread_data *td)
523 dprint(FD_FILE, "close files\n");
525 for_each_file(td, f, i) {
526 if (td->o.unlink && f->filetype == FIO_TYPE_FILE)
527 unlink(f->file_name);
529 td_io_close_file(td, f);
540 td->o.filename = NULL;
546 static void get_file_type(struct fio_file *f)
550 if (!strcmp(f->file_name, "-"))
551 f->filetype = FIO_TYPE_PIPE;
553 f->filetype = FIO_TYPE_FILE;
555 if (!lstat(f->file_name, &sb)) {
556 if (S_ISBLK(sb.st_mode))
557 f->filetype = FIO_TYPE_BD;
558 else if (S_ISCHR(sb.st_mode))
559 f->filetype = FIO_TYPE_CHAR;
560 else if (S_ISFIFO(sb.st_mode))
561 f->filetype = FIO_TYPE_PIPE;
565 int add_file(struct thread_data *td, const char *fname)
567 int cur_files = td->files_index;
568 char file_name[PATH_MAX];
572 dprint(FD_FILE, "add file %s\n", fname);
574 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
576 f = &td->files[cur_files];
577 memset(f, 0, sizeof(*f));
581 * init function, io engine may not be loaded yet
583 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
584 f->real_file_size = -1ULL;
587 len = sprintf(file_name, "%s/", td->o.directory);
589 sprintf(file_name + len, "%s", fname);
590 f->file_name = strdup(file_name);
595 if (f->filetype == FIO_TYPE_FILE)
596 td->nr_normal_files++;
601 void get_file(struct fio_file *f)
603 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
604 assert(f->flags & FIO_FILE_OPEN);
608 void put_file(struct thread_data *td, struct fio_file *f)
610 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
612 if (!(f->flags & FIO_FILE_OPEN))
615 assert(f->references);
619 if (should_fsync(td) && td->o.fsync_on_close)
622 if (td->io_ops->close_file)
623 td->io_ops->close_file(td, f);
626 f->flags &= ~FIO_FILE_OPEN;
629 static int recurse_dir(struct thread_data *td, const char *dirname)
635 D = opendir(dirname);
637 char buf[FIO_VERROR_SIZE];
639 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
640 td_verror(td, errno, buf);
644 while ((dir = readdir(D)) != NULL) {
645 char full_path[PATH_MAX];
648 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
651 sprintf(full_path, "%s/%s", dirname, dir->d_name);
653 if (lstat(full_path, &sb) == -1) {
654 if (errno != ENOENT) {
655 td_verror(td, errno, "stat");
660 if (S_ISREG(sb.st_mode)) {
661 add_file(td, full_path);
665 if (!S_ISDIR(sb.st_mode))
668 if ((ret = recurse_dir(td, full_path)) != 0)
676 int add_dir_files(struct thread_data *td, const char *path)
678 int ret = recurse_dir(td, path);
681 log_info("fio: opendir added %d files\n", td->o.nr_files);
686 void dup_files(struct thread_data *td, struct thread_data *org)
695 bytes = org->files_index * sizeof(*f);
696 td->files = malloc(bytes);
697 memcpy(td->files, org->files, bytes);
699 for_each_file(td, f, i) {
701 f->file_name = strdup(f->file_name);
706 * Returns the index that matches the filename, or -1 if not there
708 int get_fileno(struct thread_data *td, const char *fname)
713 for_each_file(td, f, i)
714 if (!strcmp(f->file_name, fname))
721 * For log usage, where we add/open/close files automatically
723 void free_release_files(struct thread_data *td)
727 td->nr_normal_files = 0;