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 (stat(f->file_name, &st) == -1)
23 else if (st.st_size < (off_t) f->file_size)
29 static int create_file(struct thread_data *td, struct fio_file *f)
31 unsigned long long left;
36 f->fd = open(f->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
42 if (ftruncate(f->fd, f->file_size) == -1) {
47 if (posix_fallocate(f->fd, 0, f->file_size) < 0) {
52 b = malloc(td->max_bs[DDIR_WRITE]);
53 memset(b, 0, td->max_bs[DDIR_WRITE]);
56 while (left && !td->terminate) {
57 bs = td->max_bs[DDIR_WRITE];
61 r = write(f->fd, b, bs);
78 else if (td->create_fsync)
91 static int create_files(struct thread_data *td)
94 int i, err, need_create;
96 for_each_file(td, f, i)
97 f->file_size = td->total_file_size / td->nr_files;
100 * unless specifically asked for overwrite, let normal io extend it
106 if (td->filetype == FIO_TYPE_FILE) {
107 for_each_file(td, f, i) {
108 int file_there = !file_ok(td, f);
110 if (file_there && td->ddir == DDIR_WRITE &&
112 unlink(f->file_name);
116 need_create += !file_there;
123 if (!td->total_file_size) {
124 log_err("Need size for create\n");
125 td_verror(td, EINVAL);
130 fprintf(f_out, "%s: Laying out IO file(s) (%u x %LuMiB == %LuMiB)\n",
131 td->name, td->nr_uniq_files,
132 (td->total_file_size >> 20) / td->nr_uniq_files,
133 td->total_file_size >> 20);
136 for_each_file(td, f, i) {
138 * Only unlink files that we created.
141 if (file_ok(td, f)) {
142 f->unlink = td->unlink;
143 err = create_file(td, f);
153 static int file_size(struct thread_data *td, struct fio_file *f)
158 * if we are not doing real io, just pretend the file is as large
159 * as the size= given. this works fine with nrfiles > 1 as well,
160 * we only really care about it being at least as big as size=
162 if (td->io_ops->flags & FIO_NULLIO) {
163 f->real_file_size = f->file_size = td->total_file_size;
168 if (fstat(f->fd, &st) == -1) {
169 td_verror(td, errno);
173 f->real_file_size = st.st_size;
175 if (!f->file_size || f->file_size > f->real_file_size)
176 f->file_size = f->real_file_size;
178 f->real_file_size = f->file_size;
183 static int bdev_size(struct thread_data *td, struct fio_file *f)
185 unsigned long long bytes;
188 r = blockdev_size(f->fd, &bytes);
194 f->real_file_size = bytes;
197 * no extend possibilities, so limit size to device size if too large
199 if (!f->file_size || f->file_size > f->real_file_size)
200 f->file_size = f->real_file_size;
202 f->file_size -= f->file_offset;
206 static int get_file_size(struct thread_data *td, struct fio_file *f)
210 if (td->filetype == FIO_TYPE_FILE)
211 ret = file_size(td, f);
212 else if (td->filetype == FIO_TYPE_BD)
213 ret = bdev_size(td, f);
215 f->real_file_size = -1;
220 if (f->file_offset > f->real_file_size) {
221 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
228 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
233 * FIXME: add blockdev flushing too
235 if (td->io_ops->flags & FIO_MMAPIO)
236 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
237 else if (td->filetype == FIO_TYPE_FILE)
238 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
239 else if (td->filetype == FIO_TYPE_BD)
240 ret = blockdev_invalidate_cache(f->fd);
241 else if (td->filetype == FIO_TYPE_CHAR)
245 td_verror(td, errno);
252 static int __setup_file_mmap(struct thread_data *td, struct fio_file *f)
257 flags = PROT_READ | PROT_WRITE;
258 else if (td_write(td)) {
261 if (td->verify != VERIFY_NONE)
266 f->mmap = mmap(NULL, f->file_size, flags, MAP_SHARED, f->fd, f->file_offset);
267 if (f->mmap == MAP_FAILED) {
269 td_verror(td, errno);
273 if (td->invalidate_cache && file_invalidate_cache(td, f))
276 if (td->sequential) {
277 if (madvise(f->mmap, f->file_size, MADV_SEQUENTIAL) < 0) {
278 td_verror(td, errno);
282 if (madvise(f->mmap, f->file_size, MADV_RANDOM) < 0) {
283 td_verror(td, errno);
291 static int setup_files_mmap(struct thread_data *td)
296 for_each_file(td, f, i) {
297 err = __setup_file_mmap(td, f);
305 static int __setup_file_plain(struct thread_data *td, struct fio_file *f)
307 if (td->invalidate_cache && file_invalidate_cache(td, f))
310 if (td->sequential) {
311 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
312 td_verror(td, errno);
316 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
317 td_verror(td, errno);
325 static int setup_files_plain(struct thread_data *td)
330 for_each_file(td, f, i) {
331 err = __setup_file_plain(td, f);
339 static int setup_file(struct thread_data *td, struct fio_file *f)
343 if (td->io_ops->flags & FIO_NETIO)
347 * we need a valid file descriptor, but don't create a real file.
348 * lets just dup stdout, seems like a sensible approach.
350 if (td->io_ops->flags & FIO_NULLIO)
351 f->fd = dup(STDOUT_FILENO);
354 flags |= OS_O_DIRECT;
358 if (td_write(td) || td_rw(td)) {
361 if (td->filetype == FIO_TYPE_FILE) {
368 f->fd = open(f->file_name, flags, 0600);
370 if (td->filetype == FIO_TYPE_CHAR)
375 f->fd = open(f->file_name, flags);
380 td_verror(td, errno);
384 if (get_file_size(td, f))
390 int open_files(struct thread_data *td)
395 for_each_file(td, f, i) {
396 err = setup_file(td, f);
404 for_each_file(td, f, i) {
414 int setup_files(struct thread_data *td)
420 * if ioengine defines a setup() method, it's responsible for
421 * setting up everything in the td->files[] area.
423 if (td->io_ops->setup)
424 return td->io_ops->setup(td);
426 if (create_files(td))
429 err = open_files(td);
434 * Recalculate the total file size now that files are set up.
436 td->total_file_size = 0;
437 for_each_file(td, f, i)
438 td->total_file_size += f->file_size;
440 td->io_size = td->total_file_size;
441 if (td->io_size == 0) {
442 log_err("%s: no io blocks\n", td->name);
443 td_verror(td, EINVAL);
448 td->zone_size = td->io_size;
450 td->total_io_size = td->io_size * td->loops;
452 if (td->io_ops->flags & FIO_MMAPIO)
453 err = setup_files_mmap(td);
455 err = setup_files_plain(td);
457 for_each_file(td, f, i) {
467 void close_files(struct thread_data *td)
472 for_each_file(td, f, i) {
473 if (!td->filename && f->unlink &&
474 td->filetype == FIO_TYPE_FILE) {
475 unlink(f->file_name);
484 munmap(f->mmap, f->file_size);