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->ddir == DDIR_WRITE &&
118 unlink(f->file_name);
122 need_create += !file_there;
129 if (!td->total_file_size) {
130 log_err("Need size for create\n");
131 td_verror(td, EINVAL, "file_size");
136 fprintf(f_out, "%s: Laying out IO file(s) (%u x %LuMiB == %LuMiB)\n",
137 td->name, td->nr_uniq_files,
138 (td->total_file_size >> 20) / td->nr_uniq_files,
139 td->total_file_size >> 20);
142 for_each_file(td, f, i) {
144 * Only unlink files that we created.
147 if (file_ok(td, f)) {
148 f->unlink = td->unlink;
149 err = create_file(td, f);
159 static int file_size(struct thread_data *td, struct fio_file *f)
164 * if we are not doing real io, just pretend the file is as large
165 * as the size= given. this works fine with nrfiles > 1 as well,
166 * we only really care about it being at least as big as size=
168 if (td->io_ops->flags & FIO_NULLIO) {
169 f->real_file_size = f->file_size = td->total_file_size;
174 if (fstat(f->fd, &st) == -1) {
175 td_verror(td, errno, "fstat");
179 f->real_file_size = st.st_size;
181 if (!f->file_size || f->file_size > f->real_file_size)
182 f->file_size = f->real_file_size;
184 f->real_file_size = f->file_size;
189 static int bdev_size(struct thread_data *td, struct fio_file *f)
191 unsigned long long bytes;
194 r = blockdev_size(f->fd, &bytes);
196 td_verror(td, r, "blockdev_size");
200 f->real_file_size = bytes;
203 * no extend possibilities, so limit size to device size if too large
205 if (!f->file_size || f->file_size > f->real_file_size)
206 f->file_size = f->real_file_size;
208 f->file_size -= f->file_offset;
212 static int get_file_size(struct thread_data *td, struct fio_file *f)
216 if (td->filetype == FIO_TYPE_FILE)
217 ret = file_size(td, f);
218 else if (td->filetype == FIO_TYPE_BD)
219 ret = bdev_size(td, f);
221 f->real_file_size = -1;
226 if (f->file_offset > f->real_file_size) {
227 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
234 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
239 * FIXME: add blockdev flushing too
241 if (td->io_ops->flags & FIO_MMAPIO)
242 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
243 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)
246 ret = blockdev_invalidate_cache(f->fd);
247 else if (td->filetype == FIO_TYPE_CHAR)
251 td_verror(td, errno, "invalidate_cache");
258 static int __setup_file_mmap(struct thread_data *td, struct fio_file *f)
263 flags = PROT_READ | PROT_WRITE;
264 else if (td_write(td)) {
267 if (td->verify != VERIFY_NONE)
272 f->mmap = mmap(NULL, f->file_size, flags, MAP_SHARED, f->fd, f->file_offset);
273 if (f->mmap == MAP_FAILED) {
275 td_verror(td, errno, "mmap");
279 if (td->invalidate_cache && file_invalidate_cache(td, f))
282 if (td->sequential) {
283 if (madvise(f->mmap, f->file_size, MADV_SEQUENTIAL) < 0) {
284 td_verror(td, errno, "madvise");
288 if (madvise(f->mmap, f->file_size, MADV_RANDOM) < 0) {
289 td_verror(td, errno, "madvise");
297 static int setup_files_mmap(struct thread_data *td)
302 for_each_file(td, f, i) {
303 err = __setup_file_mmap(td, f);
311 static int __setup_file_plain(struct thread_data *td, struct fio_file *f)
313 if (td->invalidate_cache && file_invalidate_cache(td, f))
316 if (td->sequential) {
317 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
318 td_verror(td, errno, "fadvise");
322 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
323 td_verror(td, errno, "fadvise");
331 static int setup_files_plain(struct thread_data *td)
336 for_each_file(td, f, i) {
337 err = __setup_file_plain(td, f);
345 static int setup_file(struct thread_data *td, struct fio_file *f)
349 if (td->io_ops->flags & FIO_NETIO)
353 * we need a valid file descriptor, but don't create a real file.
354 * lets just dup stdout, seems like a sensible approach.
356 if (td->io_ops->flags & FIO_NULLIO)
357 f->fd = dup(STDOUT_FILENO);
360 flags |= OS_O_DIRECT;
364 if (td_write(td) || td_rw(td)) {
367 if (td->filetype == FIO_TYPE_FILE) {
374 f->fd = open(f->file_name, flags, 0600);
376 if (td->filetype == FIO_TYPE_CHAR)
381 f->fd = open(f->file_name, flags);
388 td_verror(td, __e, "open");
389 if (__e == EINVAL && td->odirect)
390 log_err("fio: destinations does not support O_DIRECT\n");
394 if (get_file_size(td, f))
400 int open_files(struct thread_data *td)
405 for_each_file(td, f, i) {
406 err = setup_file(td, f);
414 for_each_file(td, f, i) {
424 int setup_files(struct thread_data *td)
430 * if ioengine defines a setup() method, it's responsible for
431 * setting up everything in the td->files[] area.
433 if (td->io_ops->setup)
434 return td->io_ops->setup(td);
436 if (create_files(td))
439 err = open_files(td);
444 * Recalculate the total file size now that files are set up.
446 td->total_file_size = 0;
447 for_each_file(td, f, i)
448 td->total_file_size += f->file_size;
450 td->io_size = td->total_file_size;
451 if (td->io_size == 0) {
452 log_err("%s: no io blocks\n", td->name);
453 td_verror(td, EINVAL, "total_file_size");
458 td->zone_size = td->io_size;
460 td->total_io_size = td->io_size * td->loops;
462 if (td->io_ops->flags & FIO_MMAPIO)
463 err = setup_files_mmap(td);
465 err = setup_files_plain(td);
467 for_each_file(td, f, i) {
477 void close_files(struct thread_data *td)
482 for_each_file(td, f, i) {
483 if (!td->filename && f->unlink &&
484 td->filetype == FIO_TYPE_FILE) {
485 unlink(f->file_name);
494 munmap(f->mmap, f->file_size);