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 (f->filetype != FIO_TYPE_FILE)
160 if (!total_file_size)
163 if (f->flags & FIO_FILE_EXISTS) {
164 if ((f->file_size > td->o.size / td->o.nr_files) ||
166 f->file_size = td->o.size / td->o.nr_files;
169 if (s > total_file_size)
172 total_file_size -= s;
178 * unless specifically asked for overwrite, let normal io extend it
180 can_extend = !td->o.overwrite && !(td->io_ops->flags & FIO_NOEXTEND);
181 if (can_extend && new_files) {
182 for_each_file(td, f, i) {
183 if (fill_file_size(td, f, &total_file_size, new_files)) {
184 log_info("fio: limited to %d files\n", i);
193 local_file_size = total_file_size;
194 if (!local_file_size)
195 local_file_size = -1;
200 for_each_file(td, f, i) {
203 if (f->filetype != FIO_TYPE_FILE)
205 if (f->flags & FIO_FILE_EXISTS) {
206 total_file_size += f->file_size;
210 if (fill_file_size(td, f, &local_file_size, new_files)) {
211 log_info("fio: limited to %d files\n", i);
212 new_files -= (td->o.nr_files - i);
217 total_file_size += f->file_size;
218 create_size += f->file_size;
219 file_there = !file_ok(td, f);
221 if (file_there && td_write(td) && !td->o.overwrite) {
222 unlink(f->file_name);
226 need_create += !file_there;
232 if (!td->o.size && !total_file_size) {
233 log_err("Need size for create\n");
234 td_verror(td, EINVAL, "file_size");
239 log_info("%s: Laying out IO file(s) (%u files / %LuMiB)\n",
240 td->o.name, new_files, create_size >> 20);
243 for_each_file(td, f, i) {
245 * Only unlink files that we created.
247 f->flags &= ~FIO_FILE_UNLINK;
248 if (file_ok(td, f)) {
250 f->flags |= FIO_FILE_UNLINK;
252 f->flags |= FIO_FILE_NOSORT;
253 err = create_file(td, f);
263 static int file_size(struct thread_data *td, struct fio_file *f)
267 if (td->o.overwrite) {
268 if (fstat(f->fd, &st) == -1) {
269 td_verror(td, errno, "fstat");
273 f->real_file_size = st.st_size;
275 if (!f->file_size || f->file_size > f->real_file_size)
276 f->file_size = f->real_file_size;
278 f->real_file_size = f->file_size;
283 static int bdev_size(struct thread_data *td, struct fio_file *f)
285 unsigned long long bytes;
288 r = blockdev_size(f->fd, &bytes);
290 td_verror(td, r, "blockdev_size");
294 f->real_file_size = bytes;
297 * no extend possibilities, so limit size to device size if too large
299 if (!f->file_size || f->file_size > f->real_file_size)
300 f->file_size = f->real_file_size;
302 f->file_size -= f->file_offset;
306 static int get_file_size(struct thread_data *td, struct fio_file *f)
310 if (f->filetype == FIO_TYPE_FILE) {
311 if (!(f->flags & FIO_FILE_EXISTS))
312 ret = file_size(td, f);
313 } else if (f->filetype == FIO_TYPE_BD)
314 ret = bdev_size(td, f);
316 f->real_file_size = -1;
321 if (f->file_offset > f->real_file_size) {
322 log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name, f->file_offset, f->real_file_size);
329 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
337 * FIXME: add blockdev flushing too
340 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
341 else if (f->filetype == FIO_TYPE_FILE)
342 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
343 else if (f->filetype == FIO_TYPE_BD)
344 ret = blockdev_invalidate_cache(f->fd);
345 else if (f->filetype == FIO_TYPE_CHAR)
349 td_verror(td, errno, "invalidate_cache");
356 void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
362 int generic_open_file(struct thread_data *td, struct fio_file *f)
367 flags |= OS_O_DIRECT;
371 if (td_write(td) || td_rw(td)) {
374 if (f->filetype == FIO_TYPE_FILE)
377 f->fd = open(f->file_name, flags, 0600);
379 if (f->filetype == FIO_TYPE_CHAR)
384 f->fd = open(f->file_name, flags);
388 char buf[FIO_VERROR_SIZE];
391 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
393 td_verror(td, __e, buf);
394 if (__e == EINVAL && td->o.odirect)
395 log_err("fio: destination does not support O_DIRECT\n");
397 log_err("fio: try reducing/setting openfiles (failed at %u of %u)\n", td->nr_open_files, td->o.nr_files);
401 if (get_file_size(td, f))
404 if (td->o.invalidate_cache && file_invalidate_cache(td, f))
407 if (!td->o.fadvise_hint)
410 if (!td_random(td)) {
411 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
412 td_verror(td, errno, "fadvise");
416 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
417 td_verror(td, errno, "fadvise");
428 int open_files(struct thread_data *td)
434 for_each_file(td, f, i) {
435 err = td_io_open_file(td, f);
439 if (td->o.open_files == td->nr_open_files)
446 for_each_file(td, f, i)
447 td_io_close_file(td, f);
452 int setup_files(struct thread_data *td)
459 * if ioengine defines a setup() method, it's responsible for
460 * setting up everything in the td->files[] area.
462 if (td->io_ops->setup)
463 return td->io_ops->setup(td);
465 if (create_files(td))
468 err = open_files(td);
473 * Recalculate the total file size now that files are set up.
476 for_each_file(td, f, i)
477 td->o.size += f->file_size;
479 td->io_size = td->o.size;
480 if (td->io_size == 0) {
481 log_err("%s: no io blocks\n", td->o.name);
482 td_verror(td, EINVAL, "total_file_size");
486 if (!td->o.zone_size)
487 td->o.zone_size = td->io_size;
489 td->total_io_size = td->io_size * td->o.loops;
491 for_each_file(td, f, i)
492 td_io_close_file(td, f);
497 int init_random_map(struct thread_data *td)
499 int num_maps, blocks;
503 if (td->o.norandommap)
506 for_each_file(td, f, i) {
507 blocks = (f->real_file_size + td->o.rw_min_bs - 1) / td->o.rw_min_bs;
508 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
509 f->file_map = malloc(num_maps * sizeof(long));
511 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
514 f->num_maps = num_maps;
515 memset(f->file_map, 0, num_maps * sizeof(long));
521 void close_files(struct thread_data *td)
526 for_each_file(td, f, i) {
527 if ((f->flags & FIO_FILE_UNLINK) &&
528 f->filetype == FIO_TYPE_FILE)
529 unlink(f->file_name);
531 td_io_close_file(td, f);
542 td->o.filename = NULL;
548 static void get_file_type(struct fio_file *f)
552 f->filetype = FIO_TYPE_FILE;
554 if (!lstat(f->file_name, &sb)) {
555 f->flags |= FIO_FILE_EXISTS;
557 if (S_ISBLK(sb.st_mode))
558 f->filetype = FIO_TYPE_BD;
559 else if (S_ISCHR(sb.st_mode))
560 f->filetype = FIO_TYPE_CHAR;
563 * might as well do this here, and save a stat later on
565 f->real_file_size = sb.st_size;
566 f->file_size = f->real_file_size;
571 void add_file(struct thread_data *td, const char *fname)
573 int cur_files = td->files_index;
574 char file_name[PATH_MAX];
578 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
580 f = &td->files[cur_files];
581 memset(f, 0, sizeof(*f));
585 len = sprintf(file_name, "%s/", td->o.directory);
587 sprintf(file_name + len, "%s", fname);
588 f->file_name = strdup(file_name);
593 if (f->filetype == FIO_TYPE_FILE)
594 td->nr_normal_files++;
597 void get_file(struct fio_file *f)
602 void put_file(struct thread_data *td, struct fio_file *f)
604 if (!(f->flags & FIO_FILE_OPEN))
607 assert(f->references);
611 if (should_fsync(td) && td->o.fsync_on_close)
614 if (td->io_ops->close_file)
615 td->io_ops->close_file(td, f);
617 f->flags &= ~FIO_FILE_OPEN;
620 static int recurse_dir(struct thread_data *td, const char *dirname)
626 D = opendir(dirname);
628 td_verror(td, errno, "opendir");
632 while ((dir = readdir(D)) != NULL) {
633 char full_path[PATH_MAX];
636 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
639 sprintf(full_path, "%s/%s", dirname, dir->d_name);
641 if (lstat(full_path, &sb) == -1) {
642 if (errno != ENOENT) {
643 td_verror(td, errno, "stat");
648 if (S_ISREG(sb.st_mode)) {
649 add_file(td, full_path);
654 if ((ret = recurse_dir(td, full_path)) != 0)
662 int add_dir_files(struct thread_data *td, const char *path)
664 return recurse_dir(td, path);
667 void dup_files(struct thread_data *td, struct thread_data *org)
676 bytes = org->files_index * sizeof(*f);
677 td->files = malloc(bytes);
678 memcpy(td->files, org->files, bytes);
680 for_each_file(td, f, i) {
682 f->file_name = strdup(f->file_name);