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;
106 for_each_file(td, f, i) {
107 if (f->filetype != FIO_TYPE_FILE)
110 f->file_size = td->total_file_size / td->nr_normal_files;
111 f->file_offset = td->start_offset;
115 * unless specifically asked for overwrite, let normal io extend it
117 can_extend = !td->overwrite && !(td->io_ops->flags & FIO_NOEXTEND);
122 for_each_file(td, f, i) {
125 if (f->filetype != FIO_TYPE_FILE)
128 file_there = !file_ok(td, f);
130 if (file_there && td_write(td) && !td->overwrite) {
131 unlink(f->file_name);
135 need_create += !file_there;
141 if (!td->total_file_size) {
142 log_err("Need size for create\n");
143 td_verror(td, EINVAL, "file_size");
148 fprintf(f_out, "%s: Laying out IO file(s) (%u x %LuMiB == %LuMiB)\n",
149 td->name, td->nr_normal_files,
150 (td->total_file_size >> 20) / td->nr_normal_files,
151 td->total_file_size >> 20);
154 for_each_file(td, f, i) {
156 * Only unlink files that we created.
158 f->flags &= ~FIO_FILE_UNLINK;
159 if (file_ok(td, f)) {
161 f->flags |= FIO_FILE_UNLINK;
163 err = create_file(td, f);
173 static int file_size(struct thread_data *td, struct fio_file *f)
178 if (fstat(f->fd, &st) == -1) {
179 td_verror(td, errno, "fstat");
183 f->real_file_size = st.st_size;
185 if (!f->file_size || f->file_size > f->real_file_size)
186 f->file_size = f->real_file_size;
188 f->real_file_size = f->file_size;
193 static int bdev_size(struct thread_data *td, struct fio_file *f)
195 unsigned long long bytes;
198 r = blockdev_size(f->fd, &bytes);
200 td_verror(td, r, "blockdev_size");
204 f->real_file_size = bytes;
207 * no extend possibilities, so limit size to device size if too large
209 if (!f->file_size || f->file_size > f->real_file_size)
210 f->file_size = f->real_file_size;
212 f->file_size -= f->file_offset;
216 static int get_file_size(struct thread_data *td, struct fio_file *f)
220 if (f->filetype == FIO_TYPE_FILE)
221 ret = file_size(td, f);
222 else if (f->filetype == FIO_TYPE_BD)
223 ret = bdev_size(td, f);
225 f->real_file_size = -1;
230 if (f->file_offset > f->real_file_size) {
231 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
238 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
246 * FIXME: add blockdev flushing too
249 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
250 else if (f->filetype == FIO_TYPE_FILE) {
251 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
252 } else if (f->filetype == FIO_TYPE_BD) {
253 ret = blockdev_invalidate_cache(f->fd);
254 } else if (f->filetype == FIO_TYPE_CHAR)
258 td_verror(td, errno, "invalidate_cache");
265 void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
271 int generic_open_file(struct thread_data *td, struct fio_file *f)
276 flags |= OS_O_DIRECT;
280 if (td_write(td) || td_rw(td)) {
283 if (f->filetype == FIO_TYPE_FILE)
286 f->fd = open(f->file_name, flags, 0600);
288 if (f->filetype == FIO_TYPE_CHAR)
293 f->fd = open(f->file_name, flags);
299 td_verror(td, __e, "open");
300 if (__e == EINVAL && td->odirect)
301 log_err("fio: destination does not support O_DIRECT\n");
303 log_err("fio: try reducing/setting openfiles (failed at %u of %u)\n", td->nr_open_files, td->nr_files);
307 if (get_file_size(td, f))
310 if (td->invalidate_cache && file_invalidate_cache(td, f))
313 if (!td_random(td)) {
314 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
315 td_verror(td, errno, "fadvise");
319 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
320 td_verror(td, errno, "fadvise");
331 int open_files(struct thread_data *td)
337 for_each_file(td, f, i) {
338 err = td_io_open_file(td, f);
342 if (td->open_files == td->nr_open_files)
349 for_each_file(td, f, i)
350 td_io_close_file(td, f);
355 int setup_files(struct thread_data *td)
362 * if ioengine defines a setup() method, it's responsible for
363 * setting up everything in the td->files[] area.
365 if (td->io_ops->setup)
366 return td->io_ops->setup(td);
368 if (create_files(td))
371 err = open_files(td);
376 * Recalculate the total file size now that files are set up.
378 td->total_file_size = 0;
379 for_each_file(td, f, i)
380 td->total_file_size += f->file_size;
382 td->io_size = td->total_file_size;
383 if (td->io_size == 0) {
384 log_err("%s: no io blocks\n", td->name);
385 td_verror(td, EINVAL, "total_file_size");
390 td->zone_size = td->io_size;
392 td->total_io_size = td->io_size * td->loops;
394 for_each_file(td, f, i)
395 td_io_close_file(td, f);
400 void close_files(struct thread_data *td)
405 for_each_file(td, f, i) {
406 if (!f->file_name && (f->flags & FIO_FILE_UNLINK) &&
407 f->filetype == FIO_TYPE_FILE) {
408 unlink(f->file_name);
413 td_io_close_file(td, f);
425 static void get_file_type(struct fio_file *f)
429 f->filetype = FIO_TYPE_FILE;
431 if (!lstat(f->file_name, &sb)) {
432 if (S_ISBLK(sb.st_mode))
433 f->filetype = FIO_TYPE_BD;
434 else if (S_ISCHR(sb.st_mode))
435 f->filetype = FIO_TYPE_CHAR;
439 void add_file(struct thread_data *td, const char *fname)
441 int cur_files = td->files_index;
444 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
446 f = &td->files[cur_files];
447 memset(f, 0, sizeof(*f));
449 f->file_name = strdup(fname);
454 if (f->filetype == FIO_TYPE_FILE)
455 td->nr_normal_files++;
458 void get_file(struct fio_file *f)
463 void put_file(struct thread_data *td, struct fio_file *f)
465 if (!(f->flags & FIO_FILE_OPEN))
468 assert(f->references);
472 if (should_fsync(td) && td->fsync_on_close)
475 if (td->io_ops->close_file)
476 td->io_ops->close_file(td, f);
478 f->flags &= ~FIO_FILE_OPEN;
481 static int recurse_dir(struct thread_data *td, const char *dirname)
487 D = opendir(dirname);
489 td_verror(td, errno, "opendir");
493 while ((dir = readdir(D)) != NULL) {
494 char full_path[PATH_MAX];
497 sprintf(full_path, "%s/%s", dirname, dir->d_name);
499 if (lstat(full_path, &sb) == -1) {
500 if (errno != ENOENT) {
501 td_verror(td, errno, "stat");
506 if (S_ISREG(sb.st_mode)) {
507 add_file(td, full_path);
512 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
515 if ((ret = recurse_dir(td, full_path)) != 0)
523 int add_dir_files(struct thread_data *td, const char *path)
525 return recurse_dir(td, path);