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->max_bs[DDIR_WRITE]);
62 memset(b, 0, td->max_bs[DDIR_WRITE]);
65 while (left && !td->terminate) {
66 bs = td->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->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->file_size_high)
108 upper = td->file_size_high;
109 else if (upper < td->file_size_low)
114 r = os_random_long(&td->file_size_state);
115 ret = td->file_size_low + (unsigned long long) ((double) upper * (r / (RAND_MAX + 1.0)));
116 ret -= (ret % td->rw_min_bs);
122 static int create_files(struct thread_data *td)
125 int err, need_create, can_extend;
126 unsigned long long total_file_size, local_file_size, create_size;
127 unsigned int i, new_files;
130 total_file_size = td->total_file_size;
131 for_each_file(td, f, i) {
132 unsigned long long s;
134 f->file_offset = td->start_offset;
136 if (f->filetype != FIO_TYPE_FILE)
139 if (f->flags & FIO_FILE_EXISTS) {
141 if (s > total_file_size)
144 total_file_size -= s;
150 * unless specifically asked for overwrite, let normal io extend it
152 can_extend = !td->overwrite && !(td->io_ops->flags & FIO_NOEXTEND);
156 local_file_size = total_file_size;
157 if (!local_file_size)
158 local_file_size = -1;
163 for_each_file(td, f, i) {
166 if (f->filetype != FIO_TYPE_FILE)
168 if (f->flags & FIO_FILE_EXISTS) {
169 total_file_size += f->file_size;
173 if (!td->file_size_low)
174 f->file_size = total_file_size / new_files;
177 * If we don't have enough space left for a file
178 * of the minimum size, bail.
180 if (local_file_size < td->file_size_low) {
181 log_info("fio: limited to %d files\n", i);
182 new_files -= (td->nr_files - i);
187 f->file_size = set_rand_file_size(td, local_file_size);
188 local_file_size -= f->file_size;
191 total_file_size += f->file_size;
192 create_size += f->file_size;
193 file_there = !file_ok(td, f);
195 if (file_there && td_write(td) && !td->overwrite) {
196 unlink(f->file_name);
200 need_create += !file_there;
206 if (!td->total_file_size && !total_file_size) {
207 log_err("Need size for create\n");
208 td_verror(td, EINVAL, "file_size");
213 log_info("%s: Laying out IO file(s) (%u files / %LuMiB)\n",
214 td->name, new_files, create_size >> 20);
217 for_each_file(td, f, i) {
219 * Only unlink files that we created.
221 f->flags &= ~FIO_FILE_UNLINK;
222 if (file_ok(td, f)) {
224 f->flags |= FIO_FILE_UNLINK;
226 err = create_file(td, f);
236 static int file_size(struct thread_data *td, struct fio_file *f)
241 if (fstat(f->fd, &st) == -1) {
242 td_verror(td, errno, "fstat");
246 f->real_file_size = st.st_size;
248 if (!f->file_size || f->file_size > f->real_file_size)
249 f->file_size = f->real_file_size;
251 f->real_file_size = f->file_size;
256 static int bdev_size(struct thread_data *td, struct fio_file *f)
258 unsigned long long bytes;
261 r = blockdev_size(f->fd, &bytes);
263 td_verror(td, r, "blockdev_size");
267 f->real_file_size = bytes;
270 * no extend possibilities, so limit size to device size if too large
272 if (!f->file_size || f->file_size > f->real_file_size)
273 f->file_size = f->real_file_size;
275 f->file_size -= f->file_offset;
279 static int get_file_size(struct thread_data *td, struct fio_file *f)
283 if (f->filetype == FIO_TYPE_FILE) {
284 if (!(f->flags & FIO_FILE_EXISTS))
285 ret = file_size(td, f);
286 } else if (f->filetype == FIO_TYPE_BD)
287 ret = bdev_size(td, f);
289 f->real_file_size = -1;
294 if (f->file_offset > f->real_file_size) {
295 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
302 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
310 * FIXME: add blockdev flushing too
313 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
314 else if (f->filetype == FIO_TYPE_FILE) {
315 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
316 } else if (f->filetype == FIO_TYPE_BD) {
317 ret = blockdev_invalidate_cache(f->fd);
318 } else if (f->filetype == FIO_TYPE_CHAR)
322 td_verror(td, errno, "invalidate_cache");
329 void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
335 int generic_open_file(struct thread_data *td, struct fio_file *f)
340 flags |= OS_O_DIRECT;
344 if (td_write(td) || td_rw(td)) {
347 if (f->filetype == FIO_TYPE_FILE)
350 f->fd = open(f->file_name, flags, 0600);
352 if (f->filetype == FIO_TYPE_CHAR)
357 f->fd = open(f->file_name, flags);
363 td_verror(td, __e, "open");
364 if (__e == EINVAL && td->odirect)
365 log_err("fio: destination does not support O_DIRECT\n");
367 log_err("fio: try reducing/setting openfiles (failed at %u of %u)\n", td->nr_open_files, td->nr_files);
371 if (get_file_size(td, f))
374 if (td->invalidate_cache && file_invalidate_cache(td, f))
377 if (!td_random(td)) {
378 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
379 td_verror(td, errno, "fadvise");
383 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
384 td_verror(td, errno, "fadvise");
395 int open_files(struct thread_data *td)
401 for_each_file(td, f, i) {
402 err = td_io_open_file(td, f);
406 if (td->open_files == td->nr_open_files)
413 for_each_file(td, f, i)
414 td_io_close_file(td, f);
419 int setup_files(struct thread_data *td)
426 * if ioengine defines a setup() method, it's responsible for
427 * setting up everything in the td->files[] area.
429 if (td->io_ops->setup)
430 return td->io_ops->setup(td);
432 if (create_files(td))
435 err = open_files(td);
440 * Recalculate the total file size now that files are set up.
442 td->total_file_size = 0;
443 for_each_file(td, f, i)
444 td->total_file_size += f->file_size;
446 td->io_size = td->total_file_size;
447 if (td->io_size == 0) {
448 log_err("%s: no io blocks\n", td->name);
449 td_verror(td, EINVAL, "total_file_size");
454 td->zone_size = td->io_size;
456 td->total_io_size = td->io_size * td->loops;
458 for_each_file(td, f, i)
459 td_io_close_file(td, f);
464 void close_files(struct thread_data *td)
469 for_each_file(td, f, i) {
470 if (!f->file_name && (f->flags & FIO_FILE_UNLINK) &&
471 f->filetype == FIO_TYPE_FILE) {
472 unlink(f->file_name);
477 td_io_close_file(td, f);
488 static void get_file_type(struct fio_file *f)
492 f->filetype = FIO_TYPE_FILE;
494 if (!lstat(f->file_name, &sb)) {
495 f->flags |= FIO_FILE_EXISTS;
497 if (S_ISBLK(sb.st_mode))
498 f->filetype = FIO_TYPE_BD;
499 else if (S_ISCHR(sb.st_mode))
500 f->filetype = FIO_TYPE_CHAR;
503 * might as well do this here, and save a stat later on
505 f->real_file_size = sb.st_size;
506 f->file_size = f->real_file_size;
511 void add_file(struct thread_data *td, const char *fname)
513 int cur_files = td->files_index;
516 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
518 f = &td->files[cur_files];
519 memset(f, 0, sizeof(*f));
521 f->file_name = strdup(fname);
526 if (f->filetype == FIO_TYPE_FILE)
527 td->nr_normal_files++;
530 void get_file(struct fio_file *f)
535 void put_file(struct thread_data *td, struct fio_file *f)
537 if (!(f->flags & FIO_FILE_OPEN))
540 assert(f->references);
544 if (should_fsync(td) && td->fsync_on_close)
547 if (td->io_ops->close_file)
548 td->io_ops->close_file(td, f);
550 f->flags &= ~FIO_FILE_OPEN;
553 static int recurse_dir(struct thread_data *td, const char *dirname)
559 D = opendir(dirname);
561 td_verror(td, errno, "opendir");
565 while ((dir = readdir(D)) != NULL) {
566 char full_path[PATH_MAX];
569 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
572 sprintf(full_path, "%s/%s", dirname, dir->d_name);
574 if (lstat(full_path, &sb) == -1) {
575 if (errno != ENOENT) {
576 td_verror(td, errno, "stat");
581 if (S_ISREG(sb.st_mode)) {
582 add_file(td, full_path);
587 if ((ret = recurse_dir(td, full_path)) != 0)
595 int add_dir_files(struct thread_data *td, const char *path)
597 return recurse_dir(td, path);