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 (f->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 err, need_create, can_extend;
104 for_each_file(td, f, i)
105 f->file_size = td->total_file_size / td->nr_files;
108 * unless specifically asked for overwrite, let normal io extend it
110 can_extend = !td->overwrite && !(td->io_ops->flags & FIO_NOEXTEND);
115 for_each_file(td, f, i) {
118 if (f->filetype != FIO_TYPE_FILE)
121 file_there = !file_ok(td, f);
123 if (file_there && td_write(td) && !td->overwrite) {
124 unlink(f->file_name);
128 need_create += !file_there;
134 if (!td->total_file_size) {
135 log_err("Need size for create\n");
136 td_verror(td, EINVAL, "file_size");
141 fprintf(f_out, "%s: Laying out IO file(s) (%u x %LuMiB == %LuMiB)\n",
142 td->name, td->nr_uniq_files,
143 (td->total_file_size >> 20) / td->nr_uniq_files,
144 td->total_file_size >> 20);
147 for_each_file(td, f, i) {
149 * Only unlink files that we created.
151 f->flags &= ~FIO_FILE_UNLINK;
152 if (file_ok(td, f)) {
154 f->flags |= FIO_FILE_UNLINK;
156 err = create_file(td, f);
166 static int file_size(struct thread_data *td, struct fio_file *f)
171 if (fstat(f->fd, &st) == -1) {
172 td_verror(td, errno, "fstat");
176 f->real_file_size = st.st_size;
178 if (!f->file_size || f->file_size > f->real_file_size)
179 f->file_size = f->real_file_size;
181 f->real_file_size = f->file_size;
186 static int bdev_size(struct thread_data *td, struct fio_file *f)
188 unsigned long long bytes;
191 r = blockdev_size(f->fd, &bytes);
193 td_verror(td, r, "blockdev_size");
197 f->real_file_size = bytes;
200 * no extend possibilities, so limit size to device size if too large
202 if (!f->file_size || f->file_size > f->real_file_size)
203 f->file_size = f->real_file_size;
205 f->file_size -= f->file_offset;
209 static int get_file_size(struct thread_data *td, struct fio_file *f)
213 if (f->filetype == FIO_TYPE_FILE)
214 ret = file_size(td, f);
215 else if (f->filetype == FIO_TYPE_BD)
216 ret = bdev_size(td, f);
218 f->real_file_size = -1;
223 if (f->file_offset > f->real_file_size) {
224 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
231 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
239 * FIXME: add blockdev flushing too
242 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
243 else if (f->filetype == FIO_TYPE_FILE) {
244 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
245 } else if (f->filetype == FIO_TYPE_BD) {
246 ret = blockdev_invalidate_cache(f->fd);
247 } else if (f->filetype == FIO_TYPE_CHAR)
251 td_verror(td, errno, "invalidate_cache");
258 void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
264 int generic_open_file(struct thread_data *td, struct fio_file *f)
269 flags |= OS_O_DIRECT;
273 if (td_write(td) || td_rw(td)) {
276 if (f->filetype == FIO_TYPE_FILE)
279 f->fd = open(f->file_name, flags, 0600);
281 if (f->filetype == FIO_TYPE_CHAR)
286 f->fd = open(f->file_name, flags);
292 td_verror(td, __e, "open");
293 if (__e == EINVAL && td->odirect)
294 log_err("fio: destination does not support O_DIRECT\n");
298 if (get_file_size(td, f))
301 if (td->invalidate_cache && file_invalidate_cache(td, f))
304 if (!td_random(td)) {
305 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
306 td_verror(td, errno, "fadvise");
310 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
311 td_verror(td, errno, "fadvise");
322 int open_files(struct thread_data *td)
328 for_each_file(td, f, i) {
329 err = td_io_open_file(td, f);
333 if (td->open_files == td->nr_open_files)
340 for_each_file(td, f, i)
341 td_io_close_file(td, f);
346 int setup_files(struct thread_data *td)
353 * if ioengine defines a setup() method, it's responsible for
354 * setting up everything in the td->files[] area.
356 if (td->io_ops->setup)
357 return td->io_ops->setup(td);
359 if (create_files(td))
362 err = open_files(td);
367 * Recalculate the total file size now that files are set up.
369 td->total_file_size = 0;
370 for_each_file(td, f, i)
371 td->total_file_size += f->file_size;
373 td->total_file_size = (td->total_file_size * td->nr_files) / td->open_files;
375 td->io_size = td->total_file_size;
376 if (td->io_size == 0) {
377 log_err("%s: no io blocks\n", td->name);
378 td_verror(td, EINVAL, "total_file_size");
383 td->zone_size = td->io_size;
385 td->total_io_size = td->io_size * td->loops;
387 for_each_file(td, f, i)
388 td_io_close_file(td, f);
393 void close_files(struct thread_data *td)
398 for_each_file(td, f, i) {
399 if (!td->filename && (f->flags & FIO_FILE_UNLINK) &&
400 f->filetype == FIO_TYPE_FILE) {
401 unlink(f->file_name);
405 td_io_close_file(td, f);
417 static void get_file_type(struct thread_data *td, struct fio_file *f)
421 f->filetype = FIO_TYPE_FILE;
423 if (!lstat(td->filename, &sb)) {
424 if (S_ISBLK(sb.st_mode))
425 f->filetype = FIO_TYPE_BD;
426 else if (S_ISCHR(sb.st_mode))
427 f->filetype = FIO_TYPE_CHAR;
431 void add_file(struct thread_data *td, const char *fname)
433 int cur_files = td->open_files;
436 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
438 f = &td->files[cur_files];
439 memset(f, 0, sizeof(*f));
441 f->file_name = strdup(fname);
443 get_file_type(td, f);
446 td->nr_uniq_files = td->open_files;
449 void get_file(struct fio_file *f)
454 void put_file(struct thread_data *td, struct fio_file *f)
456 if (!(f->flags & FIO_FILE_OPEN))
459 assert(f->references);
463 if (td->io_ops->close_file)
464 td->io_ops->close_file(td, f);
466 f->flags &= ~FIO_FILE_OPEN;