12 * Check if the file exists and it's large enough.
14 static int file_ok(struct thread_data *td, struct fio_file *f)
18 if (td->filetype != FIO_TYPE_FILE || (td->io_ops->flags & FIO_NULLIO))
21 if (lstat(f->file_name, &st) == -1)
25 * if it's a special file, size is always ok for now
27 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))
29 if (st.st_size < (off_t) f->file_size)
35 static int create_file(struct thread_data *td, struct fio_file *f)
37 unsigned long long left;
42 f->fd = open(f->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
44 td_verror(td, errno, "open");
48 if (ftruncate(f->fd, f->file_size) == -1) {
49 td_verror(td, errno, "ftruncate");
53 if (posix_fallocate(f->fd, 0, f->file_size) < 0) {
54 td_verror(td, errno, "posix_fallocate");
58 b = malloc(td->max_bs[DDIR_WRITE]);
59 memset(b, 0, td->max_bs[DDIR_WRITE]);
62 while (left && !td->terminate) {
63 bs = td->max_bs[DDIR_WRITE];
67 r = write(f->fd, b, bs);
74 td_verror(td, errno, "write");
76 td_verror(td, EIO, "write");
84 else if (td->create_fsync)
97 static int create_files(struct thread_data *td)
100 int i, err, need_create;
102 for_each_file(td, f, i)
103 f->file_size = td->total_file_size / td->nr_files;
106 * unless specifically asked for overwrite, let normal io extend it
112 if (td->filetype == FIO_TYPE_FILE) {
113 for_each_file(td, f, i) {
114 int file_there = !file_ok(td, f);
116 if (file_there && td_write(td) && !td->overwrite) {
117 unlink(f->file_name);
121 need_create += !file_there;
128 if (!td->total_file_size) {
129 log_err("Need size for create\n");
130 td_verror(td, EINVAL, "file_size");
135 fprintf(f_out, "%s: Laying out IO file(s) (%u x %LuMiB == %LuMiB)\n",
136 td->name, td->nr_uniq_files,
137 (td->total_file_size >> 20) / td->nr_uniq_files,
138 td->total_file_size >> 20);
141 for_each_file(td, f, i) {
143 * Only unlink files that we created.
146 if (file_ok(td, f)) {
147 f->unlink = td->unlink;
148 err = create_file(td, f);
158 static int file_size(struct thread_data *td, struct fio_file *f)
163 * if we are not doing real io, just pretend the file is as large
164 * as the size= given. this works fine with nrfiles > 1 as well,
165 * we only really care about it being at least as big as size=
167 if (td->io_ops->flags & FIO_NULLIO) {
168 f->real_file_size = f->file_size = td->total_file_size;
173 if (fstat(f->fd, &st) == -1) {
174 td_verror(td, errno, "fstat");
178 f->real_file_size = st.st_size;
180 if (!f->file_size || f->file_size > f->real_file_size)
181 f->file_size = f->real_file_size;
183 f->real_file_size = f->file_size;
188 static int bdev_size(struct thread_data *td, struct fio_file *f)
190 unsigned long long bytes;
193 r = blockdev_size(f->fd, &bytes);
195 td_verror(td, r, "blockdev_size");
199 f->real_file_size = bytes;
202 * no extend possibilities, so limit size to device size if too large
204 if (!f->file_size || f->file_size > f->real_file_size)
205 f->file_size = f->real_file_size;
207 f->file_size -= f->file_offset;
211 static int get_file_size(struct thread_data *td, struct fio_file *f)
215 if (td->filetype == FIO_TYPE_FILE)
216 ret = file_size(td, f);
217 else if (td->filetype == FIO_TYPE_BD)
218 ret = bdev_size(td, f);
220 f->real_file_size = -1;
225 if (f->file_offset > f->real_file_size) {
226 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
233 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
238 * FIXME: add blockdev flushing too
240 if (td->io_ops->flags & FIO_MMAPIO)
241 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
242 else if (td->filetype == FIO_TYPE_FILE) {
244 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
245 } else if (td->filetype == FIO_TYPE_BD) {
247 ret = blockdev_invalidate_cache(f->fd);
248 } else if (td->filetype == FIO_TYPE_CHAR)
252 td_verror(td, errno, "invalidate_cache");
259 static int __setup_file_mmap(struct thread_data *td, struct fio_file *f)
264 flags = PROT_READ | PROT_WRITE;
265 else if (td_write(td)) {
268 if (td->verify != VERIFY_NONE)
273 f->mmap = mmap(NULL, f->file_size, flags, MAP_SHARED, f->fd, f->file_offset);
274 if (f->mmap == MAP_FAILED) {
276 td_verror(td, errno, "mmap");
280 if (td->invalidate_cache && file_invalidate_cache(td, f))
283 if (!td_random(td)) {
284 if (madvise(f->mmap, f->file_size, MADV_SEQUENTIAL) < 0) {
285 td_verror(td, errno, "madvise");
289 if (madvise(f->mmap, f->file_size, MADV_RANDOM) < 0) {
290 td_verror(td, errno, "madvise");
298 static int setup_files_mmap(struct thread_data *td)
303 for_each_file(td, f, i) {
304 err = __setup_file_mmap(td, f);
312 static int __setup_file_plain(struct thread_data *td, struct fio_file *f)
314 if (td->invalidate_cache && file_invalidate_cache(td, f))
317 if (!td_random(td)) {
318 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
319 td_verror(td, errno, "fadvise");
323 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
324 td_verror(td, errno, "fadvise");
332 static int setup_files_plain(struct thread_data *td)
337 for_each_file(td, f, i) {
338 err = __setup_file_plain(td, f);
346 static int setup_file(struct thread_data *td, struct fio_file *f)
350 if (td->io_ops->flags & FIO_NETIO)
354 * we need a valid file descriptor, but don't create a real file.
355 * lets just dup stdout, seems like a sensible approach.
357 if (td->io_ops->flags & FIO_NULLIO)
358 f->fd = dup(STDOUT_FILENO);
361 flags |= OS_O_DIRECT;
365 if (td_write(td) || td_rw(td)) {
368 if (td->filetype == FIO_TYPE_FILE) {
375 f->fd = open(f->file_name, flags, 0600);
377 if (td->filetype == FIO_TYPE_CHAR)
382 f->fd = open(f->file_name, flags);
389 td_verror(td, __e, "open");
390 if (__e == EINVAL && td->odirect)
391 log_err("fio: destination does not support O_DIRECT\n");
395 if (get_file_size(td, f))
401 int open_files(struct thread_data *td)
406 for_each_file(td, f, i) {
407 err = setup_file(td, f);
415 for_each_file(td, f, i) {
425 int setup_files(struct thread_data *td)
431 * if ioengine defines a setup() method, it's responsible for
432 * setting up everything in the td->files[] area.
434 if (td->io_ops->setup)
435 return td->io_ops->setup(td);
437 if (create_files(td))
440 err = open_files(td);
445 * Recalculate the total file size now that files are set up.
447 td->total_file_size = 0;
448 for_each_file(td, f, i)
449 td->total_file_size += f->file_size;
451 td->io_size = td->total_file_size;
452 if (td->io_size == 0) {
453 log_err("%s: no io blocks\n", td->name);
454 td_verror(td, EINVAL, "total_file_size");
459 td->zone_size = td->io_size;
461 td->total_io_size = td->io_size * td->loops;
463 if (td->io_ops->flags & FIO_MMAPIO)
464 err = setup_files_mmap(td);
466 err = setup_files_plain(td);
468 for_each_file(td, f, i) {
478 void close_files(struct thread_data *td)
483 for_each_file(td, f, i) {
484 if (!td->filename && f->unlink &&
485 td->filetype == FIO_TYPE_FILE) {
486 unlink(f->file_name);
495 munmap(f->mmap, f->file_size);