11 int open_file(struct thread_data *td, struct fio_file *f, int flags, int perm)
14 f->fd = open(f->file_name, flags, perm);
16 f->fd = open(f->file_name, flags);
26 void close_file(struct thread_data *td, struct fio_file *f)
36 * Check if the file exists and it's large enough.
38 static int file_ok(struct thread_data *td, struct fio_file *f)
42 if (td->filetype != FIO_TYPE_FILE || (td->io_ops->flags & FIO_NULLIO))
45 if (lstat(f->file_name, &st) == -1)
49 * if it's a special file, size is always ok for now
51 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))
53 if (st.st_size < (off_t) f->file_size)
59 static int create_file(struct thread_data *td, struct fio_file *f)
61 unsigned long long left;
66 f->fd = open(f->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
68 td_verror(td, errno, "open");
72 if (ftruncate(f->fd, f->file_size) == -1) {
73 td_verror(td, errno, "ftruncate");
77 if (posix_fallocate(f->fd, 0, f->file_size) < 0) {
78 td_verror(td, errno, "posix_fallocate");
82 b = malloc(td->max_bs[DDIR_WRITE]);
83 memset(b, 0, td->max_bs[DDIR_WRITE]);
86 while (left && !td->terminate) {
87 bs = td->max_bs[DDIR_WRITE];
91 r = write(f->fd, b, bs);
98 td_verror(td, errno, "write");
100 td_verror(td, EIO, "write");
107 unlink(f->file_name);
108 else if (td->create_fsync)
121 static int create_files(struct thread_data *td)
124 int i, err, need_create;
126 for_each_file(td, f, i)
127 f->file_size = td->total_file_size / td->nr_files;
130 * unless specifically asked for overwrite, let normal io extend it
136 if (td->filetype == FIO_TYPE_FILE) {
137 for_each_file(td, f, i) {
138 int file_there = !file_ok(td, f);
140 if (file_there && td_write(td) && !td->overwrite) {
141 unlink(f->file_name);
145 need_create += !file_there;
152 if (!td->total_file_size) {
153 log_err("Need size for create\n");
154 td_verror(td, EINVAL, "file_size");
159 fprintf(f_out, "%s: Laying out IO file(s) (%u x %LuMiB == %LuMiB)\n",
160 td->name, td->nr_uniq_files,
161 (td->total_file_size >> 20) / td->nr_uniq_files,
162 td->total_file_size >> 20);
165 for_each_file(td, f, i) {
167 * Only unlink files that we created.
170 if (file_ok(td, f)) {
171 f->unlink = td->unlink;
172 err = create_file(td, f);
182 static int file_size(struct thread_data *td, struct fio_file *f)
187 * if we are not doing real io, just pretend the file is as large
188 * as the size= given. this works fine with nrfiles > 1 as well,
189 * we only really care about it being at least as big as size=
191 if (td->io_ops->flags & FIO_NULLIO) {
192 f->real_file_size = f->file_size = td->total_file_size;
197 if (fstat(f->fd, &st) == -1) {
198 td_verror(td, errno, "fstat");
202 f->real_file_size = st.st_size;
204 if (!f->file_size || f->file_size > f->real_file_size)
205 f->file_size = f->real_file_size;
207 f->real_file_size = f->file_size;
212 static int bdev_size(struct thread_data *td, struct fio_file *f)
214 unsigned long long bytes;
217 r = blockdev_size(f->fd, &bytes);
219 td_verror(td, r, "blockdev_size");
223 f->real_file_size = bytes;
226 * no extend possibilities, so limit size to device size if too large
228 if (!f->file_size || f->file_size > f->real_file_size)
229 f->file_size = f->real_file_size;
231 f->file_size -= f->file_offset;
235 static int get_file_size(struct thread_data *td, struct fio_file *f)
239 if (td->filetype == FIO_TYPE_FILE)
240 ret = file_size(td, f);
241 else if (td->filetype == FIO_TYPE_BD)
242 ret = bdev_size(td, f);
244 f->real_file_size = -1;
249 if (f->file_offset > f->real_file_size) {
250 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
257 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
262 * FIXME: add blockdev flushing too
264 if (td->io_ops->flags & FIO_MMAPIO)
265 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
266 else if (td->filetype == FIO_TYPE_FILE) {
268 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
269 } else if (td->filetype == FIO_TYPE_BD) {
271 ret = blockdev_invalidate_cache(f->fd);
272 } else if (td->filetype == FIO_TYPE_CHAR)
276 td_verror(td, errno, "invalidate_cache");
283 static int __setup_file_mmap(struct thread_data *td, struct fio_file *f)
288 flags = PROT_READ | PROT_WRITE;
289 else if (td_write(td)) {
292 if (td->verify != VERIFY_NONE)
297 f->mmap = mmap(NULL, f->file_size, flags, MAP_SHARED, f->fd, f->file_offset);
298 if (f->mmap == MAP_FAILED) {
300 td_verror(td, errno, "mmap");
304 if (td->invalidate_cache && file_invalidate_cache(td, f))
307 if (!td_random(td)) {
308 if (madvise(f->mmap, f->file_size, MADV_SEQUENTIAL) < 0) {
309 td_verror(td, errno, "madvise");
313 if (madvise(f->mmap, f->file_size, MADV_RANDOM) < 0) {
314 td_verror(td, errno, "madvise");
322 static int setup_files_mmap(struct thread_data *td)
327 for_each_file(td, f, i) {
328 err = __setup_file_mmap(td, f);
336 static int __setup_file_plain(struct thread_data *td, struct fio_file *f)
338 if (td->invalidate_cache && file_invalidate_cache(td, f))
341 if (!td_random(td)) {
342 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
343 td_verror(td, errno, "fadvise");
347 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
348 td_verror(td, errno, "fadvise");
356 static int setup_files_plain(struct thread_data *td)
361 for_each_file(td, f, i) {
362 err = __setup_file_plain(td, f);
370 static int setup_file(struct thread_data *td, struct fio_file *f)
374 if (td->io_ops->flags & FIO_SELFOPEN)
378 * we need a valid file descriptor, but don't create a real file.
379 * lets just dup stdout, seems like a sensible approach.
381 if (td->io_ops->flags & FIO_NULLIO)
382 f->fd = dup(STDOUT_FILENO);
385 flags |= OS_O_DIRECT;
389 if (td_write(td) || td_rw(td)) {
392 if (td->filetype == FIO_TYPE_FILE) {
399 open_file(td, f, flags, 0600);
401 if (td->filetype == FIO_TYPE_CHAR)
406 open_file(td, f, flags, 0);
413 td_verror(td, __e, "open");
414 if (__e == EINVAL && td->odirect)
415 log_err("fio: destination does not support O_DIRECT\n");
419 if (get_file_size(td, f)) {
427 int open_files(struct thread_data *td)
432 for_each_file(td, f, i) {
433 err = setup_file(td, f);
441 for_each_file(td, f, i)
447 int setup_files(struct thread_data *td)
453 * if ioengine defines a setup() method, it's responsible for
454 * setting up everything in the td->files[] area.
456 if (td->io_ops->setup)
457 return td->io_ops->setup(td);
459 if (create_files(td))
462 err = open_files(td);
467 * Recalculate the total file size now that files are set up.
469 td->total_file_size = 0;
470 for_each_file(td, f, i)
471 td->total_file_size += f->file_size;
473 td->io_size = td->total_file_size;
474 if (td->io_size == 0) {
475 log_err("%s: no io blocks\n", td->name);
476 td_verror(td, EINVAL, "total_file_size");
481 td->zone_size = td->io_size;
483 td->total_io_size = td->io_size * td->loops;
485 if (td->io_ops->flags & FIO_MMAPIO)
486 err = setup_files_mmap(td);
488 err = setup_files_plain(td);
490 for_each_file(td, f, i)
496 void close_files(struct thread_data *td)
501 for_each_file(td, f, i) {
502 if (!td->filename && f->unlink &&
503 td->filetype == FIO_TYPE_FILE) {
504 unlink(f->file_name);
512 munmap(f->mmap, f->file_size);