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 int create_files(struct thread_data *td)
103 int err, need_create, can_extend;
104 unsigned long long total_file_size;
105 unsigned int i, new_files;
108 total_file_size = td->total_file_size;
109 for_each_file(td, f, i) {
110 unsigned long long s;
112 f->file_offset = td->start_offset;
114 if (f->filetype != FIO_TYPE_FILE)
117 if (f->flags & FIO_FILE_EXISTS) {
119 if (s > total_file_size)
122 total_file_size -= s;
128 * unless specifically asked for overwrite, let normal io extend it
130 can_extend = !td->overwrite && !(td->io_ops->flags & FIO_NOEXTEND);
135 for_each_file(td, f, i) {
138 if (f->filetype != FIO_TYPE_FILE)
140 if (f->flags & FIO_FILE_EXISTS)
143 f->file_size = total_file_size / new_files;
145 file_there = !file_ok(td, f);
147 if (file_there && td_write(td) && !td->overwrite) {
148 unlink(f->file_name);
152 need_create += !file_there;
158 if (!td->total_file_size) {
159 log_err("Need size for create\n");
160 td_verror(td, EINVAL, "file_size");
165 fprintf(f_out, "%s: Laying out IO file(s) (%u x %LuMiB == %LuMiB)\n",
167 (total_file_size >> 20) / new_files,
168 total_file_size >> 20);
171 for_each_file(td, f, i) {
173 * Only unlink files that we created.
175 f->flags &= ~FIO_FILE_UNLINK;
176 if (file_ok(td, f)) {
178 f->flags |= FIO_FILE_UNLINK;
180 err = create_file(td, f);
190 static int file_size(struct thread_data *td, struct fio_file *f)
195 if (fstat(f->fd, &st) == -1) {
196 td_verror(td, errno, "fstat");
200 f->real_file_size = st.st_size;
202 if (!f->file_size || f->file_size > f->real_file_size)
203 f->file_size = f->real_file_size;
205 f->real_file_size = f->file_size;
210 static int bdev_size(struct thread_data *td, struct fio_file *f)
212 unsigned long long bytes;
215 r = blockdev_size(f->fd, &bytes);
217 td_verror(td, r, "blockdev_size");
221 f->real_file_size = bytes;
224 * no extend possibilities, so limit size to device size if too large
226 if (!f->file_size || f->file_size > f->real_file_size)
227 f->file_size = f->real_file_size;
229 f->file_size -= f->file_offset;
233 static int get_file_size(struct thread_data *td, struct fio_file *f)
237 if (f->filetype == FIO_TYPE_FILE) {
238 if (!(f->flags & FIO_FILE_EXISTS))
239 ret = file_size(td, f);
240 } else if (f->filetype == FIO_TYPE_BD)
241 ret = bdev_size(td, f);
243 f->real_file_size = -1;
248 if (f->file_offset > f->real_file_size) {
249 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
256 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
264 * FIXME: add blockdev flushing too
267 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
268 else if (f->filetype == FIO_TYPE_FILE) {
269 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
270 } else if (f->filetype == FIO_TYPE_BD) {
271 ret = blockdev_invalidate_cache(f->fd);
272 } else if (f->filetype == FIO_TYPE_CHAR)
276 td_verror(td, errno, "invalidate_cache");
283 void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
289 int generic_open_file(struct thread_data *td, struct fio_file *f)
294 flags |= OS_O_DIRECT;
298 if (td_write(td) || td_rw(td)) {
301 if (f->filetype == FIO_TYPE_FILE)
304 f->fd = open(f->file_name, flags, 0600);
306 if (f->filetype == FIO_TYPE_CHAR)
311 f->fd = open(f->file_name, flags);
317 td_verror(td, __e, "open");
318 if (__e == EINVAL && td->odirect)
319 log_err("fio: destination does not support O_DIRECT\n");
321 log_err("fio: try reducing/setting openfiles (failed at %u of %u)\n", td->nr_open_files, td->nr_files);
325 if (get_file_size(td, f))
328 if (td->invalidate_cache && file_invalidate_cache(td, f))
331 if (!td_random(td)) {
332 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
333 td_verror(td, errno, "fadvise");
337 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
338 td_verror(td, errno, "fadvise");
349 int open_files(struct thread_data *td)
355 for_each_file(td, f, i) {
356 err = td_io_open_file(td, f);
360 if (td->open_files == td->nr_open_files)
367 for_each_file(td, f, i)
368 td_io_close_file(td, f);
373 int setup_files(struct thread_data *td)
380 * if ioengine defines a setup() method, it's responsible for
381 * setting up everything in the td->files[] area.
383 if (td->io_ops->setup)
384 return td->io_ops->setup(td);
386 if (create_files(td))
389 err = open_files(td);
394 * Recalculate the total file size now that files are set up.
396 td->total_file_size = 0;
397 for_each_file(td, f, i)
398 td->total_file_size += f->file_size;
400 td->io_size = td->total_file_size;
401 if (td->io_size == 0) {
402 log_err("%s: no io blocks\n", td->name);
403 td_verror(td, EINVAL, "total_file_size");
408 td->zone_size = td->io_size;
410 td->total_io_size = td->io_size * td->loops;
412 for_each_file(td, f, i)
413 td_io_close_file(td, f);
418 void close_files(struct thread_data *td)
423 for_each_file(td, f, i) {
424 if (!f->file_name && (f->flags & FIO_FILE_UNLINK) &&
425 f->filetype == FIO_TYPE_FILE) {
426 unlink(f->file_name);
431 td_io_close_file(td, f);
442 static void get_file_type(struct fio_file *f)
446 f->filetype = FIO_TYPE_FILE;
448 if (!lstat(f->file_name, &sb)) {
449 f->flags |= FIO_FILE_EXISTS;
451 if (S_ISBLK(sb.st_mode))
452 f->filetype = FIO_TYPE_BD;
453 else if (S_ISCHR(sb.st_mode))
454 f->filetype = FIO_TYPE_CHAR;
457 * might as well do this here, and save a stat later on
459 f->real_file_size = sb.st_size;
460 f->file_size = f->real_file_size;
465 void add_file(struct thread_data *td, const char *fname)
467 int cur_files = td->files_index;
470 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
472 f = &td->files[cur_files];
473 memset(f, 0, sizeof(*f));
475 f->file_name = strdup(fname);
480 if (f->filetype == FIO_TYPE_FILE)
481 td->nr_normal_files++;
484 void get_file(struct fio_file *f)
489 void put_file(struct thread_data *td, struct fio_file *f)
491 if (!(f->flags & FIO_FILE_OPEN))
494 assert(f->references);
498 if (should_fsync(td) && td->fsync_on_close)
501 if (td->io_ops->close_file)
502 td->io_ops->close_file(td, f);
504 f->flags &= ~FIO_FILE_OPEN;
507 static int recurse_dir(struct thread_data *td, const char *dirname)
513 D = opendir(dirname);
515 td_verror(td, errno, "opendir");
519 while ((dir = readdir(D)) != NULL) {
520 char full_path[PATH_MAX];
523 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
526 sprintf(full_path, "%s/%s", dirname, dir->d_name);
528 if (lstat(full_path, &sb) == -1) {
529 if (errno != ENOENT) {
530 td_verror(td, errno, "stat");
535 if (S_ISREG(sb.st_mode)) {
536 add_file(td, full_path);
541 if ((ret = recurse_dir(td, full_path)) != 0)
549 int add_dir_files(struct thread_data *td, const char *path)
551 return recurse_dir(td, path);