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)
243 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
244 else if (td->filetype == FIO_TYPE_BD)
245 ret = blockdev_invalidate_cache(f->fd);
246 else if (td->filetype == FIO_TYPE_CHAR)
250 td_verror(td, errno, "invalidate_cache");
257 static int __setup_file_mmap(struct thread_data *td, struct fio_file *f)
262 flags = PROT_READ | PROT_WRITE;
263 else if (td_write(td)) {
266 if (td->verify != VERIFY_NONE)
271 f->mmap = mmap(NULL, f->file_size, flags, MAP_SHARED, f->fd, f->file_offset);
272 if (f->mmap == MAP_FAILED) {
274 td_verror(td, errno, "mmap");
278 if (td->invalidate_cache && file_invalidate_cache(td, f))
281 if (!td_random(td)) {
282 if (madvise(f->mmap, f->file_size, MADV_SEQUENTIAL) < 0) {
283 td_verror(td, errno, "madvise");
287 if (madvise(f->mmap, f->file_size, MADV_RANDOM) < 0) {
288 td_verror(td, errno, "madvise");
296 static int setup_files_mmap(struct thread_data *td)
301 for_each_file(td, f, i) {
302 err = __setup_file_mmap(td, f);
310 static int __setup_file_plain(struct thread_data *td, struct fio_file *f)
312 if (td->invalidate_cache && file_invalidate_cache(td, f))
315 if (!td_random(td)) {
316 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
317 td_verror(td, errno, "fadvise");
321 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
322 td_verror(td, errno, "fadvise");
330 static int setup_files_plain(struct thread_data *td)
335 for_each_file(td, f, i) {
336 err = __setup_file_plain(td, f);
344 static int setup_file(struct thread_data *td, struct fio_file *f)
348 if (td->io_ops->flags & FIO_NETIO)
352 * we need a valid file descriptor, but don't create a real file.
353 * lets just dup stdout, seems like a sensible approach.
355 if (td->io_ops->flags & FIO_NULLIO)
356 f->fd = dup(STDOUT_FILENO);
359 flags |= OS_O_DIRECT;
363 if (td_write(td) || td_rw(td)) {
366 if (td->filetype == FIO_TYPE_FILE) {
373 f->fd = open(f->file_name, flags, 0600);
375 if (td->filetype == FIO_TYPE_CHAR)
380 f->fd = open(f->file_name, flags);
387 td_verror(td, __e, "open");
388 if (__e == EINVAL && td->odirect)
389 log_err("fio: destination does not support O_DIRECT\n");
393 if (get_file_size(td, f))
399 int open_files(struct thread_data *td)
404 for_each_file(td, f, i) {
405 err = setup_file(td, f);
413 for_each_file(td, f, i) {
423 int setup_files(struct thread_data *td)
429 * if ioengine defines a setup() method, it's responsible for
430 * setting up everything in the td->files[] area.
432 if (td->io_ops->setup)
433 return td->io_ops->setup(td);
435 if (create_files(td))
438 err = open_files(td);
443 * Recalculate the total file size now that files are set up.
445 td->total_file_size = 0;
446 for_each_file(td, f, i)
447 td->total_file_size += f->file_size;
449 td->io_size = td->total_file_size;
450 if (td->io_size == 0) {
451 log_err("%s: no io blocks\n", td->name);
452 td_verror(td, EINVAL, "total_file_size");
457 td->zone_size = td->io_size;
459 td->total_io_size = td->io_size * td->loops;
461 if (td->io_ops->flags & FIO_MMAPIO)
462 err = setup_files_mmap(td);
464 err = setup_files_plain(td);
466 for_each_file(td, f, i) {
476 void close_files(struct thread_data *td)
481 for_each_file(td, f, i) {
482 if (!td->filename && f->unlink &&
483 td->filetype == FIO_TYPE_FILE) {
484 unlink(f->file_name);
493 munmap(f->mmap, f->file_size);