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)
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 == WRITE && !td->overwrite) {
111 unlink(f->file_name);
115 need_create += !file_there;
122 if (!td->total_file_size) {
123 log_err("Need size for create\n");
124 td_verror(td, EINVAL);
129 fprintf(f_out, "%s: Laying out IO file(s) (%u x %LuMiB == %LuMiB)\n",
130 td->name, td->nr_uniq_files,
131 (td->total_file_size >> 20) / td->nr_uniq_files,
132 td->total_file_size >> 20);
135 for_each_file(td, f, i) {
137 * Only unlink files that we created.
140 if (file_ok(td, f)) {
141 f->unlink = td->unlink;
142 err = create_file(td, f);
152 static int file_size(struct thread_data *td, struct fio_file *f)
157 if (fstat(f->fd, &st) == -1) {
158 td_verror(td, errno);
162 f->real_file_size = st.st_size;
164 if (!f->file_size || f->file_size > f->real_file_size)
165 f->file_size = f->real_file_size;
167 f->real_file_size = f->file_size;
172 static int bdev_size(struct thread_data *td, struct fio_file *f)
174 unsigned long long bytes;
177 r = blockdev_size(f->fd, &bytes);
183 f->real_file_size = bytes;
186 * no extend possibilities, so limit size to device size if too large
188 if (!f->file_size || f->file_size > f->real_file_size)
189 f->file_size = f->real_file_size;
191 f->file_size -= f->file_offset;
195 static int get_file_size(struct thread_data *td, struct fio_file *f)
199 if (td->filetype == FIO_TYPE_FILE)
200 ret = file_size(td, f);
201 else if (td->filetype == FIO_TYPE_BD)
202 ret = bdev_size(td, f);
204 f->real_file_size = -1;
209 if (f->file_offset > f->real_file_size) {
210 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
217 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
222 * FIXME: add blockdev flushing too
224 if (td->io_ops->flags & FIO_MMAPIO)
225 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
226 else if (td->filetype == FIO_TYPE_FILE)
227 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
228 else if (td->filetype == FIO_TYPE_BD)
229 ret = blockdev_invalidate_cache(f->fd);
230 else if (td->filetype == FIO_TYPE_CHAR)
234 td_verror(td, errno);
241 static int __setup_file_mmap(struct thread_data *td, struct fio_file *f)
246 flags = PROT_READ | PROT_WRITE;
247 else if (td_write(td)) {
250 if (td->verify != VERIFY_NONE)
255 f->mmap = mmap(NULL, f->file_size, flags, MAP_SHARED, f->fd, f->file_offset);
256 if (f->mmap == MAP_FAILED) {
258 td_verror(td, errno);
262 if (td->invalidate_cache && file_invalidate_cache(td, f))
265 if (td->sequential) {
266 if (madvise(f->mmap, f->file_size, MADV_SEQUENTIAL) < 0) {
267 td_verror(td, errno);
271 if (madvise(f->mmap, f->file_size, MADV_RANDOM) < 0) {
272 td_verror(td, errno);
280 static int setup_files_mmap(struct thread_data *td)
285 for_each_file(td, f, i) {
286 err = __setup_file_mmap(td, f);
294 static int __setup_file_plain(struct thread_data *td, struct fio_file *f)
296 if (td->invalidate_cache && file_invalidate_cache(td, f))
299 if (td->sequential) {
300 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
301 td_verror(td, errno);
305 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
306 td_verror(td, errno);
314 static int setup_files_plain(struct thread_data *td)
319 for_each_file(td, f, i) {
320 err = __setup_file_plain(td, f);
328 static int setup_file(struct thread_data *td, struct fio_file *f)
333 flags |= OS_O_DIRECT;
337 if (td_write(td) || td_rw(td)) {
340 if (td->filetype == FIO_TYPE_FILE) {
347 f->fd = open(f->file_name, flags, 0600);
349 if (td->filetype == FIO_TYPE_CHAR)
354 f->fd = open(f->file_name, flags);
358 td_verror(td, errno);
362 if (get_file_size(td, f))
368 int open_files(struct thread_data *td)
373 for_each_file(td, f, i) {
374 err = setup_file(td, f);
382 for_each_file(td, f, i) {
392 int setup_files(struct thread_data *td)
398 * if ioengine defines a setup() method, it's responsible for
399 * setting up everything in the td->files[] area.
401 if (td->io_ops->setup)
402 return td->io_ops->setup(td);
404 if (create_files(td))
407 err = open_files(td);
412 * Recalculate the total file size now that files are set up.
414 td->total_file_size = 0;
415 for_each_file(td, f, i)
416 td->total_file_size += f->file_size;
418 td->io_size = td->total_file_size;
419 if (td->io_size == 0) {
420 log_err("%s: no io blocks\n", td->name);
421 td_verror(td, EINVAL);
426 td->zone_size = td->io_size;
428 td->total_io_size = td->io_size * td->loops;
430 if (td->io_ops->flags & FIO_MMAPIO)
431 err = setup_files_mmap(td);
433 err = setup_files_plain(td);
435 for_each_file(td, f, i) {
445 void close_files(struct thread_data *td)
450 for_each_file(td, f, i) {
451 if (!td->filename && f->unlink &&
452 td->filetype == FIO_TYPE_FILE) {
453 unlink(f->file_name);
462 munmap(f->mmap, f->file_size);