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;
107 total_file_size = td->total_file_size;
108 for_each_file(td, f, i) {
109 unsigned long long s;
111 f->file_offset = td->start_offset;
113 if (f->filetype != FIO_TYPE_FILE)
116 if (f->flags & FIO_FILE_EXISTS) {
118 if (s > total_file_size)
121 total_file_size -= s;
126 * unless specifically asked for overwrite, let normal io extend it
128 can_extend = !td->overwrite && !(td->io_ops->flags & FIO_NOEXTEND);
133 for_each_file(td, f, i) {
136 if (f->filetype != FIO_TYPE_FILE)
138 if (f->flags & FIO_FILE_EXISTS)
141 f->file_size = total_file_size / td->nr_normal_files;
143 file_there = !file_ok(td, f);
145 if (file_there && td_write(td) && !td->overwrite) {
146 unlink(f->file_name);
150 need_create += !file_there;
156 if (!td->total_file_size) {
157 log_err("Need size for create\n");
158 td_verror(td, EINVAL, "file_size");
163 fprintf(f_out, "%s: Laying out IO file(s) (%u x %LuMiB == %LuMiB)\n",
164 td->name, td->nr_normal_files,
165 (td->total_file_size >> 20) / td->nr_normal_files,
166 td->total_file_size >> 20);
169 for_each_file(td, f, i) {
171 * Only unlink files that we created.
173 f->flags &= ~FIO_FILE_UNLINK;
174 if (file_ok(td, f)) {
176 f->flags |= FIO_FILE_UNLINK;
178 err = create_file(td, f);
188 static int file_size(struct thread_data *td, struct fio_file *f)
193 if (fstat(f->fd, &st) == -1) {
194 td_verror(td, errno, "fstat");
198 f->real_file_size = st.st_size;
200 if (!f->file_size || f->file_size > f->real_file_size)
201 f->file_size = f->real_file_size;
203 f->real_file_size = f->file_size;
208 static int bdev_size(struct thread_data *td, struct fio_file *f)
210 unsigned long long bytes;
213 r = blockdev_size(f->fd, &bytes);
215 td_verror(td, r, "blockdev_size");
219 f->real_file_size = bytes;
222 * no extend possibilities, so limit size to device size if too large
224 if (!f->file_size || f->file_size > f->real_file_size)
225 f->file_size = f->real_file_size;
227 f->file_size -= f->file_offset;
231 static int get_file_size(struct thread_data *td, struct fio_file *f)
235 if (f->filetype == FIO_TYPE_FILE) {
236 if (!(f->flags & FIO_FILE_EXISTS))
237 ret = file_size(td, f);
238 } else if (f->filetype == FIO_TYPE_BD)
239 ret = bdev_size(td, f);
241 f->real_file_size = -1;
246 if (f->file_offset > f->real_file_size) {
247 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
254 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
262 * FIXME: add blockdev flushing too
265 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
266 else if (f->filetype == FIO_TYPE_FILE) {
267 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
268 } else if (f->filetype == FIO_TYPE_BD) {
269 ret = blockdev_invalidate_cache(f->fd);
270 } else if (f->filetype == FIO_TYPE_CHAR)
274 td_verror(td, errno, "invalidate_cache");
281 void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
287 int generic_open_file(struct thread_data *td, struct fio_file *f)
292 flags |= OS_O_DIRECT;
296 if (td_write(td) || td_rw(td)) {
299 if (f->filetype == FIO_TYPE_FILE)
302 f->fd = open(f->file_name, flags, 0600);
304 if (f->filetype == FIO_TYPE_CHAR)
309 f->fd = open(f->file_name, flags);
315 td_verror(td, __e, "open");
316 if (__e == EINVAL && td->odirect)
317 log_err("fio: destination does not support O_DIRECT\n");
319 log_err("fio: try reducing/setting openfiles (failed at %u of %u)\n", td->nr_open_files, td->nr_files);
323 if (get_file_size(td, f))
326 if (td->invalidate_cache && file_invalidate_cache(td, f))
329 if (!td_random(td)) {
330 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
331 td_verror(td, errno, "fadvise");
335 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
336 td_verror(td, errno, "fadvise");
347 int open_files(struct thread_data *td)
353 for_each_file(td, f, i) {
354 err = td_io_open_file(td, f);
358 if (td->open_files == td->nr_open_files)
365 for_each_file(td, f, i)
366 td_io_close_file(td, f);
371 int setup_files(struct thread_data *td)
378 * if ioengine defines a setup() method, it's responsible for
379 * setting up everything in the td->files[] area.
381 if (td->io_ops->setup)
382 return td->io_ops->setup(td);
384 if (create_files(td))
387 err = open_files(td);
392 * Recalculate the total file size now that files are set up.
394 td->total_file_size = 0;
395 for_each_file(td, f, i)
396 td->total_file_size += f->file_size;
398 td->io_size = td->total_file_size;
399 if (td->io_size == 0) {
400 log_err("%s: no io blocks\n", td->name);
401 td_verror(td, EINVAL, "total_file_size");
406 td->zone_size = td->io_size;
408 td->total_io_size = td->io_size * td->loops;
410 for_each_file(td, f, i)
411 td_io_close_file(td, f);
416 void close_files(struct thread_data *td)
421 for_each_file(td, f, i) {
422 if (!f->file_name && (f->flags & FIO_FILE_UNLINK) &&
423 f->filetype == FIO_TYPE_FILE) {
424 unlink(f->file_name);
429 td_io_close_file(td, f);
441 static void get_file_type(struct fio_file *f)
445 f->filetype = FIO_TYPE_FILE;
447 if (!lstat(f->file_name, &sb)) {
448 f->flags |= FIO_FILE_EXISTS;
450 if (S_ISBLK(sb.st_mode))
451 f->filetype = FIO_TYPE_BD;
452 else if (S_ISCHR(sb.st_mode))
453 f->filetype = FIO_TYPE_CHAR;
456 * might as well do this here, and save a stat later on
458 f->real_file_size = sb.st_size;
459 f->file_size = f->real_file_size;
464 void add_file(struct thread_data *td, const char *fname)
466 int cur_files = td->files_index;
469 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
471 f = &td->files[cur_files];
472 memset(f, 0, sizeof(*f));
474 f->file_name = strdup(fname);
479 if (f->filetype == FIO_TYPE_FILE)
480 td->nr_normal_files++;
483 void get_file(struct fio_file *f)
488 void put_file(struct thread_data *td, struct fio_file *f)
490 if (!(f->flags & FIO_FILE_OPEN))
493 assert(f->references);
497 if (should_fsync(td) && td->fsync_on_close)
500 if (td->io_ops->close_file)
501 td->io_ops->close_file(td, f);
503 f->flags &= ~FIO_FILE_OPEN;
506 static int recurse_dir(struct thread_data *td, const char *dirname)
512 D = opendir(dirname);
514 td_verror(td, errno, "opendir");
518 while ((dir = readdir(D)) != NULL) {
519 char full_path[PATH_MAX];
522 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
525 sprintf(full_path, "%s/%s", dirname, dir->d_name);
527 if (lstat(full_path, &sb) == -1) {
528 if (errno != ENOENT) {
529 td_verror(td, errno, "stat");
534 if (S_ISREG(sb.st_mode)) {
535 add_file(td, full_path);
540 if ((ret = recurse_dir(td, full_path)) != 0)
548 int add_dir_files(struct thread_data *td, const char *path)
550 return recurse_dir(td, path);