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)
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 (fstat(f->fd, &st) == -1) {
188 td_verror(td, errno, "fstat");
192 f->real_file_size = st.st_size;
194 if (!f->file_size || f->file_size > f->real_file_size)
195 f->file_size = f->real_file_size;
197 f->real_file_size = f->file_size;
202 static int bdev_size(struct thread_data *td, struct fio_file *f)
204 unsigned long long bytes;
207 r = blockdev_size(f->fd, &bytes);
209 td_verror(td, r, "blockdev_size");
213 f->real_file_size = bytes;
216 * no extend possibilities, so limit size to device size if too large
218 if (!f->file_size || f->file_size > f->real_file_size)
219 f->file_size = f->real_file_size;
221 f->file_size -= f->file_offset;
225 static int get_file_size(struct thread_data *td, struct fio_file *f)
229 if (td->filetype == FIO_TYPE_FILE)
230 ret = file_size(td, f);
231 else if (td->filetype == FIO_TYPE_BD)
232 ret = bdev_size(td, f);
234 f->real_file_size = -1;
239 if (f->file_offset > f->real_file_size) {
240 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
247 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
252 * FIXME: add blockdev flushing too
254 if (td->io_ops->flags & FIO_MMAPIO)
255 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
256 else if (td->filetype == FIO_TYPE_FILE) {
258 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
259 } else if (td->filetype == FIO_TYPE_BD) {
261 ret = blockdev_invalidate_cache(f->fd);
262 } else if (td->filetype == FIO_TYPE_CHAR)
266 td_verror(td, errno, "invalidate_cache");
273 static int __setup_file_mmap(struct thread_data *td, struct fio_file *f)
278 flags = PROT_READ | PROT_WRITE;
279 else if (td_write(td)) {
282 if (td->verify != VERIFY_NONE)
287 f->mmap = mmap(NULL, f->file_size, flags, MAP_SHARED, f->fd, f->file_offset);
288 if (f->mmap == MAP_FAILED) {
290 td_verror(td, errno, "mmap");
294 if (td->invalidate_cache && file_invalidate_cache(td, f))
297 if (!td_random(td)) {
298 if (madvise(f->mmap, f->file_size, MADV_SEQUENTIAL) < 0) {
299 td_verror(td, errno, "madvise");
303 if (madvise(f->mmap, f->file_size, MADV_RANDOM) < 0) {
304 td_verror(td, errno, "madvise");
312 static int setup_files_mmap(struct thread_data *td)
317 for_each_file(td, f, i) {
318 err = __setup_file_mmap(td, f);
326 static int __setup_file_plain(struct thread_data *td, struct fio_file *f)
328 if (td->invalidate_cache && file_invalidate_cache(td, f))
331 if (!td_random(td)) {
332 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
333 td_verror(td, errno, "fadvise");
337 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
338 td_verror(td, errno, "fadvise");
346 static int setup_files_plain(struct thread_data *td)
351 for_each_file(td, f, i) {
352 err = __setup_file_plain(td, f);
360 static int setup_file(struct thread_data *td, struct fio_file *f)
364 if (td->io_ops->flags & FIO_SELFOPEN)
368 flags |= OS_O_DIRECT;
372 if (td_write(td) || td_rw(td)) {
375 if (td->filetype == FIO_TYPE_FILE) {
382 open_file(td, f, flags, 0600);
384 if (td->filetype == FIO_TYPE_CHAR)
389 open_file(td, f, flags, 0);
395 td_verror(td, __e, "open");
396 if (__e == EINVAL && td->odirect)
397 log_err("fio: destination does not support O_DIRECT\n");
401 if (get_file_size(td, f)) {
409 int open_files(struct thread_data *td)
414 for_each_file(td, f, i) {
415 err = setup_file(td, f);
423 for_each_file(td, f, i)
429 int setup_files(struct thread_data *td)
435 * if ioengine defines a setup() method, it's responsible for
436 * setting up everything in the td->files[] area.
438 if (td->io_ops->setup)
439 return td->io_ops->setup(td);
441 if (create_files(td))
444 err = open_files(td);
449 * Recalculate the total file size now that files are set up.
451 td->total_file_size = 0;
452 for_each_file(td, f, i)
453 td->total_file_size += f->file_size;
455 td->io_size = td->total_file_size;
456 if (td->io_size == 0) {
457 log_err("%s: no io blocks\n", td->name);
458 td_verror(td, EINVAL, "total_file_size");
463 td->zone_size = td->io_size;
465 td->total_io_size = td->io_size * td->loops;
467 if (td->io_ops->flags & FIO_MMAPIO)
468 err = setup_files_mmap(td);
470 err = setup_files_plain(td);
472 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);
494 munmap(f->mmap, f->file_size);