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 ||
19 (td->io_ops->flags & FIO_DISKLESSIO))
22 if (lstat(f->file_name, &st) == -1)
26 * if it's a special file, size is always ok for now
28 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))
30 if (st.st_size < (off_t) f->file_size)
36 static int create_file(struct thread_data *td, struct fio_file *f)
38 unsigned long long left;
43 f->fd = open(f->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
45 td_verror(td, errno, "open");
49 if (ftruncate(f->fd, f->file_size) == -1) {
50 td_verror(td, errno, "ftruncate");
54 if (posix_fallocate(f->fd, 0, f->file_size) < 0) {
55 td_verror(td, errno, "posix_fallocate");
59 b = malloc(td->max_bs[DDIR_WRITE]);
60 memset(b, 0, td->max_bs[DDIR_WRITE]);
63 while (left && !td->terminate) {
64 bs = td->max_bs[DDIR_WRITE];
68 r = write(f->fd, b, bs);
75 td_verror(td, errno, "write");
77 td_verror(td, EIO, "write");
85 else if (td->create_fsync)
98 static int create_files(struct thread_data *td)
101 int i, err, need_create, can_extend;
103 for_each_file(td, f, i)
104 f->file_size = td->total_file_size / td->nr_files;
107 * unless specifically asked for overwrite, let normal io extend it
109 can_extend = !td->overwrite && !(td->io_ops->flags & FIO_NOEXTEND);
114 if (td->filetype == FIO_TYPE_FILE) {
115 for_each_file(td, f, i) {
116 int file_there = !file_ok(td, f);
118 if (file_there && td_write(td) && !td->overwrite) {
119 unlink(f->file_name);
123 need_create += !file_there;
130 if (!td->total_file_size) {
131 log_err("Need size for create\n");
132 td_verror(td, EINVAL, "file_size");
137 fprintf(f_out, "%s: Laying out IO file(s) (%u x %LuMiB == %LuMiB)\n",
138 td->name, td->nr_uniq_files,
139 (td->total_file_size >> 20) / td->nr_uniq_files,
140 td->total_file_size >> 20);
143 for_each_file(td, f, i) {
145 * Only unlink files that we created.
148 if (file_ok(td, f)) {
149 f->unlink = td->unlink;
150 err = create_file(td, f);
160 static int file_size(struct thread_data *td, struct fio_file *f)
165 if (fstat(f->fd, &st) == -1) {
166 td_verror(td, errno, "fstat");
170 f->real_file_size = st.st_size;
172 if (!f->file_size || f->file_size > f->real_file_size)
173 f->file_size = f->real_file_size;
175 f->real_file_size = f->file_size;
180 static int bdev_size(struct thread_data *td, struct fio_file *f)
182 unsigned long long bytes;
185 r = blockdev_size(f->fd, &bytes);
187 td_verror(td, r, "blockdev_size");
191 f->real_file_size = bytes;
194 * no extend possibilities, so limit size to device size if too large
196 if (!f->file_size || f->file_size > f->real_file_size)
197 f->file_size = f->real_file_size;
199 f->file_size -= f->file_offset;
203 static int get_file_size(struct thread_data *td, struct fio_file *f)
207 if (td->filetype == FIO_TYPE_FILE)
208 ret = file_size(td, f);
209 else if (td->filetype == FIO_TYPE_BD)
210 ret = bdev_size(td, f);
212 f->real_file_size = -1;
217 if (f->file_offset > f->real_file_size) {
218 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
225 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
233 * FIXME: add blockdev flushing too
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, "invalidate_cache");
252 void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
258 int generic_open_file(struct thread_data *td, struct fio_file *f)
263 flags |= OS_O_DIRECT;
267 if (td_write(td) || td_rw(td)) {
270 if (td->filetype == FIO_TYPE_FILE)
273 f->fd = open(f->file_name, flags, 0600);
275 if (td->filetype == FIO_TYPE_CHAR)
280 f->fd = open(f->file_name, flags);
286 td_verror(td, __e, "open");
287 if (__e == EINVAL && td->odirect)
288 log_err("fio: destination does not support O_DIRECT\n");
292 if (get_file_size(td, f))
295 if (td->invalidate_cache && file_invalidate_cache(td, f))
298 if (!td_random(td)) {
299 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
300 td_verror(td, errno, "fadvise");
304 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
305 td_verror(td, errno, "fadvise");
316 int open_files(struct thread_data *td)
321 for_each_file(td, f, i) {
322 err = td_io_open_file(td, f);
326 if (td->open_files == td->nr_open_files)
333 for_each_file(td, f, i)
334 td_io_close_file(td, f);
339 int setup_files(struct thread_data *td)
345 * if ioengine defines a setup() method, it's responsible for
346 * setting up everything in the td->files[] area.
348 if (td->io_ops->setup)
349 return td->io_ops->setup(td);
351 if (create_files(td))
354 err = open_files(td);
359 * Recalculate the total file size now that files are set up.
361 td->total_file_size = 0;
362 for_each_file(td, f, i)
363 td->total_file_size += f->file_size;
365 td->total_file_size = (td->total_file_size * td->nr_files) / td->open_files;
367 td->io_size = td->total_file_size;
368 if (td->io_size == 0) {
369 log_err("%s: no io blocks\n", td->name);
370 td_verror(td, EINVAL, "total_file_size");
375 td->zone_size = td->io_size;
377 td->total_io_size = td->io_size * td->loops;
379 for_each_file(td, f, i)
380 td_io_close_file(td, f);
385 void close_files(struct thread_data *td)
390 for_each_file(td, f, i) {
391 if (!td->filename && f->unlink &&
392 td->filetype == FIO_TYPE_FILE) {
393 unlink(f->file_name);
398 td_io_close_file(td, f);