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");
305 if (get_file_size(td, f))
308 if (td->invalidate_cache && file_invalidate_cache(td, f))
311 if (!td_random(td)) {
312 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
313 td_verror(td, errno, "fadvise");
317 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
318 td_verror(td, errno, "fadvise");
329 int open_files(struct thread_data *td)
335 for_each_file(td, f, i) {
336 err = td_io_open_file(td, f);
340 if (td->open_files == td->nr_open_files)
347 for_each_file(td, f, i)
348 td_io_close_file(td, f);
353 int setup_files(struct thread_data *td)
360 * if ioengine defines a setup() method, it's responsible for
361 * setting up everything in the td->files[] area.
363 if (td->io_ops->setup)
364 return td->io_ops->setup(td);
366 if (create_files(td))
369 err = open_files(td);
374 * Recalculate the total file size now that files are set up.
376 td->total_file_size = 0;
377 for_each_file(td, f, i)
378 td->total_file_size += f->file_size;
380 td->io_size = td->total_file_size;
381 if (td->io_size == 0) {
382 log_err("%s: no io blocks\n", td->name);
383 td_verror(td, EINVAL, "total_file_size");
388 td->zone_size = td->io_size;
390 td->total_io_size = td->io_size * td->loops;
392 for_each_file(td, f, i)
393 td_io_close_file(td, f);
398 void close_files(struct thread_data *td)
403 for_each_file(td, f, i) {
404 if (!f->file_name && (f->flags & FIO_FILE_UNLINK) &&
405 f->filetype == FIO_TYPE_FILE) {
406 unlink(f->file_name);
411 td_io_close_file(td, f);
423 static void get_file_type(struct fio_file *f)
427 f->filetype = FIO_TYPE_FILE;
429 if (!lstat(f->file_name, &sb)) {
430 if (S_ISBLK(sb.st_mode))
431 f->filetype = FIO_TYPE_BD;
432 else if (S_ISCHR(sb.st_mode))
433 f->filetype = FIO_TYPE_CHAR;
437 void add_file(struct thread_data *td, const char *fname)
439 int cur_files = td->files_index;
442 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
444 f = &td->files[cur_files];
445 memset(f, 0, sizeof(*f));
447 f->file_name = strdup(fname);
452 if (f->filetype == FIO_TYPE_FILE)
453 td->nr_normal_files++;
456 void get_file(struct fio_file *f)
461 void put_file(struct thread_data *td, struct fio_file *f)
463 if (!(f->flags & FIO_FILE_OPEN))
466 assert(f->references);
470 if (should_fsync(td) && td->fsync_on_close)
473 if (td->io_ops->close_file)
474 td->io_ops->close_file(td, f);
476 f->flags &= ~FIO_FILE_OPEN;
479 static int recurse_dir(struct thread_data *td, const char *dirname)
485 D = opendir(dirname);
487 td_verror(td, errno, "opendir");
491 while ((dir = readdir(D)) != NULL) {
492 char full_path[PATH_MAX];
495 sprintf(full_path, "%s/%s", dirname, dir->d_name);
497 if (lstat(full_path, &sb) == -1) {
498 if (errno != ENOENT) {
499 td_verror(td, errno, "stat");
504 if (S_ISREG(sb.st_mode)) {
505 add_file(td, full_path);
510 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
513 if ((ret = recurse_dir(td, full_path)) != 0)
521 int add_dir_files(struct thread_data *td, const char *path)
523 return recurse_dir(td, path);