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 * check if we need to lay the file out complete again. fio
23 * does that for operations involving reads, or for writes
24 * where overwrite is set
26 if (td_read(td) || (td_write(td) && td->o.overwrite))
28 if (td_write(td) && !td->o.overwrite)
31 if ((unlink_file || new_layout) && (f->flags & FIO_FILE_EXISTS)) {
32 if (unlink(f->file_name) < 0) {
33 td_verror(td, errno, "unlink");
38 flags = O_WRONLY | O_CREAT;
42 f->fd = open(f->file_name, flags, 0644);
44 td_verror(td, errno, "open");
48 if (ftruncate(f->fd, f->real_file_size) == -1) {
49 td_verror(td, errno, "ftruncate");
56 if (posix_fallocate(f->fd, 0, f->real_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]);
64 left = f->real_file_size;
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)
101 static unsigned long long get_rand_file_size(struct thread_data *td)
103 unsigned long long ret;
106 r = os_random_long(&td->file_size_state);
107 ret = td->o.file_size_low + (unsigned long long) ((double) td->o.file_size_high * (r / (RAND_MAX + 1.0)));
108 ret -= (ret % td->o.rw_min_bs);
112 static int file_size(struct thread_data *td, struct fio_file *f)
116 if (fstat(f->fd, &st) == -1) {
117 td_verror(td, errno, "fstat");
121 f->real_file_size = st.st_size;
125 static int bdev_size(struct thread_data *td, struct fio_file *f)
127 unsigned long long bytes;
130 r = blockdev_size(f->fd, &bytes);
132 td_verror(td, r, "blockdev_size");
136 f->real_file_size = bytes;
140 static int get_file_size(struct thread_data *td, struct fio_file *f)
144 if (f->flags & FIO_SIZE_KNOWN)
147 if (f->filetype == FIO_TYPE_FILE)
148 ret = file_size(td, f);
149 else if (f->filetype == FIO_TYPE_BD)
150 ret = bdev_size(td, f);
152 f->real_file_size = -1;
157 if (f->file_offset > f->real_file_size) {
158 log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name, f->file_offset, f->real_file_size);
162 f->flags |= FIO_SIZE_KNOWN;
166 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
174 * FIXME: add blockdev flushing too
177 ret = madvise(f->mmap, f->io_size, MADV_DONTNEED);
178 else if (f->filetype == FIO_TYPE_FILE)
179 ret = fadvise(f->fd, f->file_offset, f->io_size, POSIX_FADV_DONTNEED);
180 else if (f->filetype == FIO_TYPE_BD) {
181 ret = blockdev_invalidate_cache(f->fd);
182 if (ret < 0 && errno == EACCES && geteuid()) {
184 log_err("fio: only root may flush block devices. Cache flush bypassed!\n");
189 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
193 td_verror(td, errno, "invalidate_cache");
200 void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
206 int generic_open_file(struct thread_data *td, struct fio_file *f)
211 if (!strcmp(f->file_name, "-")) {
213 log_err("fio: can't read/write to stdin/out\n");
219 * move output logging to stderr, if we are writing to stdout
226 flags |= OS_O_DIRECT;
233 if (f->filetype == FIO_TYPE_FILE)
237 f->fd = dup(STDOUT_FILENO);
239 f->fd = open(f->file_name, flags, 0600);
241 if (f->filetype == FIO_TYPE_CHAR)
247 f->fd = dup(STDIN_FILENO);
249 f->fd = open(f->file_name, flags);
253 char buf[FIO_VERROR_SIZE];
256 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
258 td_verror(td, __e, buf);
261 if (get_file_size(td, f))
270 int open_files(struct thread_data *td)
276 for_each_file(td, f, i) {
277 err = td_io_open_file(td, f);
279 if (td->error == EMFILE) {
280 log_err("fio: limited open files to: %d\n", td->nr_open_files);
281 td->o.open_files = td->nr_open_files;
288 if (td->o.open_files == td->nr_open_files)
295 for_each_file(td, f, i)
296 td_io_close_file(td, f);
302 * open/close all files, so that ->real_file_size gets set
304 static int get_file_sizes(struct thread_data *td)
310 for_each_file(td, f, i) {
311 if (td->io_ops->open_file(td, f)) {
312 if (td->error != ENOENT) {
313 log_err("%s\n", td->verror);
318 if (td->io_ops->close_file)
319 td->io_ops->close_file(td, f);
322 if (f->real_file_size == -1ULL && td->o.size)
323 f->real_file_size = td->o.size / td->o.nr_files;
330 * Open the files and setup files sizes, creating files if necessary.
332 int setup_files(struct thread_data *td)
334 unsigned long long total_size, extend_size;
337 int err = 0, need_extend;
340 * if ioengine defines a setup() method, it's responsible for
341 * opening the files and setting f->real_file_size to indicate
342 * the valid range for that file.
344 if (td->io_ops->setup)
345 err = td->io_ops->setup(td);
347 err = get_file_sizes(td);
353 * check sizes. if the files/devices do not exist and the size
354 * isn't passed to fio, abort.
357 for_each_file(td, f, i) {
358 if (f->real_file_size == -1ULL)
361 total_size += f->real_file_size;
365 * device/file sizes are zero and no size given, punt
367 if ((!total_size || total_size == -1ULL) && !td->o.size) {
368 log_err("%s: you need to specify size=\n", td->o.name);
369 td_verror(td, EINVAL, "total_file_size");
374 * now file sizes are known, so we can set ->io_size. if size= is
375 * not given, ->io_size is just equal to ->real_file_size. if size
376 * is given, ->io_size is size / nr_files.
378 extend_size = total_size = 0;
380 for_each_file(td, f, i) {
381 f->file_offset = td->o.start_offset;
383 if (!td->o.file_size_low) {
385 * no file size range given, file size is equal to
386 * total size divided by number of files. if that is
387 * zero, set it to the real file size.
389 f->io_size = td->o.size / td->o.nr_files;
391 if (f->file_offset > f->real_file_size)
393 f->io_size = f->real_file_size - f->file_offset;
395 } else if (f->real_file_size < td->o.file_size_low ||
396 f->real_file_size > td->o.file_size_high) {
397 if (f->file_offset > td->o.file_size_low)
400 * file size given. if it's fixed, use that. if it's a
401 * range, generate a random size in-between.
403 if (td->o.file_size_low == td->o.file_size_high)
404 f->io_size = td->o.file_size_low - f->file_offset;
406 f->io_size = get_rand_file_size(td) - f->file_offset;
407 } else if (f->file_offset > f->real_file_size)
410 f->io_size = f->real_file_size - f->file_offset;
412 if (f->io_size == -1ULL)
415 total_size += f->io_size;
417 if (f->filetype == FIO_TYPE_FILE &&
418 (f->io_size + f->file_offset) > f->real_file_size &&
419 !(td->io_ops->flags & FIO_DISKLESSIO)) {
421 extend_size += (f->io_size + f->file_offset);
422 f->flags |= FIO_FILE_EXTEND;
426 if (!td->o.size || td->o.size > total_size)
427 td->o.size = total_size;
430 * See if we need to extend some files
434 log_info("%s: Laying out IO file(s) (%u files / %LuMiB)\n",
435 td->o.name, need_extend, extend_size >> 20);
437 for_each_file(td, f, i) {
438 if (!(f->flags & FIO_FILE_EXTEND))
441 assert(f->filetype == FIO_TYPE_FILE);
442 f->flags &= ~FIO_FILE_EXTEND;
443 f->real_file_size = (f->io_size + f->file_offset);
444 err = extend_file(td, f);
454 if (!td->o.zone_size)
455 td->o.zone_size = td->o.size;
457 td->total_io_size = td->o.size * td->o.loops;
460 log_err("%s: you need to specify valid offset=\n", td->o.name);
464 int init_random_map(struct thread_data *td)
466 int num_maps, blocks;
470 if (td->o.norandommap)
473 for_each_file(td, f, i) {
474 blocks = (f->real_file_size + td->o.rw_min_bs - 1) / td->o.rw_min_bs;
475 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
476 f->file_map = malloc(num_maps * sizeof(long));
478 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
481 f->num_maps = num_maps;
482 memset(f->file_map, 0, num_maps * sizeof(long));
488 void close_files(struct thread_data *td)
493 for_each_file(td, f, i) {
494 if (td->o.unlink && f->filetype == FIO_TYPE_FILE)
495 unlink(f->file_name);
497 td_io_close_file(td, f);
508 td->o.filename = NULL;
514 static void get_file_type(struct fio_file *f)
518 if (!strcmp(f->file_name, "-"))
519 f->filetype = FIO_TYPE_PIPE;
521 f->filetype = FIO_TYPE_FILE;
523 if (!lstat(f->file_name, &sb)) {
524 if (S_ISBLK(sb.st_mode))
525 f->filetype = FIO_TYPE_BD;
526 else if (S_ISCHR(sb.st_mode))
527 f->filetype = FIO_TYPE_CHAR;
528 else if (S_ISFIFO(sb.st_mode))
529 f->filetype = FIO_TYPE_PIPE;
533 int add_file(struct thread_data *td, const char *fname)
535 int cur_files = td->files_index;
536 char file_name[PATH_MAX];
540 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
542 f = &td->files[cur_files];
543 memset(f, 0, sizeof(*f));
547 * init function, io engine may not be loaded yet
549 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
550 f->real_file_size = -1ULL;
553 len = sprintf(file_name, "%s/", td->o.directory);
555 sprintf(file_name + len, "%s", fname);
556 f->file_name = strdup(file_name);
561 if (f->filetype == FIO_TYPE_FILE)
562 td->nr_normal_files++;
567 void get_file(struct fio_file *f)
569 assert(f->flags & FIO_FILE_OPEN);
573 void put_file(struct thread_data *td, struct fio_file *f)
575 if (!(f->flags & FIO_FILE_OPEN))
578 assert(f->references);
582 if (should_fsync(td) && td->o.fsync_on_close)
585 if (td->io_ops->close_file)
586 td->io_ops->close_file(td, f);
589 f->flags &= ~FIO_FILE_OPEN;
592 static int recurse_dir(struct thread_data *td, const char *dirname)
598 D = opendir(dirname);
600 char buf[FIO_VERROR_SIZE];
602 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
603 td_verror(td, errno, buf);
607 while ((dir = readdir(D)) != NULL) {
608 char full_path[PATH_MAX];
611 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
614 sprintf(full_path, "%s/%s", dirname, dir->d_name);
616 if (lstat(full_path, &sb) == -1) {
617 if (errno != ENOENT) {
618 td_verror(td, errno, "stat");
623 if (S_ISREG(sb.st_mode)) {
624 add_file(td, full_path);
628 if (!S_ISDIR(sb.st_mode))
631 if ((ret = recurse_dir(td, full_path)) != 0)
639 int add_dir_files(struct thread_data *td, const char *path)
641 int ret = recurse_dir(td, path);
644 log_info("fio: opendir added %d files\n", td->o.nr_files);
649 void dup_files(struct thread_data *td, struct thread_data *org)
658 bytes = org->files_index * sizeof(*f);
659 td->files = malloc(bytes);
660 memcpy(td->files, org->files, bytes);
662 for_each_file(td, f, i) {
664 f->file_name = strdup(f->file_name);
669 * Returns the index that matches the filename, or -1 if not there
671 int get_fileno(struct thread_data *td, const char *fname)
676 for_each_file(td, f, i)
677 if (!strcmp(f->file_name, fname))
684 * For log usage, where we add/open/close files automatically
686 void free_release_files(struct thread_data *td)
690 td->nr_normal_files = 0;