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 if (f->filetype != FIO_TYPE_FILE)
108 f->file_size = td->total_file_size / td->nr_normal_files;
109 f->file_offset = td->start_offset;
113 * unless specifically asked for overwrite, let normal io extend it
115 can_extend = !td->overwrite && !(td->io_ops->flags & FIO_NOEXTEND);
120 for_each_file(td, f, i) {
123 if (f->filetype != FIO_TYPE_FILE)
126 file_there = !file_ok(td, f);
128 if (file_there && td_write(td) && !td->overwrite) {
129 unlink(f->file_name);
133 need_create += !file_there;
139 if (!td->total_file_size) {
140 log_err("Need size for create\n");
141 td_verror(td, EINVAL, "file_size");
146 fprintf(f_out, "%s: Laying out IO file(s) (%u x %LuMiB == %LuMiB)\n",
147 td->name, td->nr_normal_files,
148 (td->total_file_size >> 20) / td->nr_normal_files,
149 td->total_file_size >> 20);
152 for_each_file(td, f, i) {
154 * Only unlink files that we created.
156 f->flags &= ~FIO_FILE_UNLINK;
157 if (file_ok(td, f)) {
159 f->flags |= FIO_FILE_UNLINK;
161 err = create_file(td, f);
171 static int file_size(struct thread_data *td, struct fio_file *f)
176 if (fstat(f->fd, &st) == -1) {
177 td_verror(td, errno, "fstat");
181 f->real_file_size = st.st_size;
183 if (!f->file_size || f->file_size > f->real_file_size)
184 f->file_size = f->real_file_size;
186 f->real_file_size = f->file_size;
191 static int bdev_size(struct thread_data *td, struct fio_file *f)
193 unsigned long long bytes;
196 r = blockdev_size(f->fd, &bytes);
198 td_verror(td, r, "blockdev_size");
202 f->real_file_size = bytes;
205 * no extend possibilities, so limit size to device size if too large
207 if (!f->file_size || f->file_size > f->real_file_size)
208 f->file_size = f->real_file_size;
210 f->file_size -= f->file_offset;
214 static int get_file_size(struct thread_data *td, struct fio_file *f)
218 if (f->filetype == FIO_TYPE_FILE)
219 ret = file_size(td, f);
220 else if (f->filetype == FIO_TYPE_BD)
221 ret = bdev_size(td, f);
223 f->real_file_size = -1;
228 if (f->file_offset > f->real_file_size) {
229 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
236 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
244 * FIXME: add blockdev flushing too
247 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
248 else if (f->filetype == FIO_TYPE_FILE) {
249 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
250 } else if (f->filetype == FIO_TYPE_BD) {
251 ret = blockdev_invalidate_cache(f->fd);
252 } else if (f->filetype == FIO_TYPE_CHAR)
256 td_verror(td, errno, "invalidate_cache");
263 void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
269 int generic_open_file(struct thread_data *td, struct fio_file *f)
274 flags |= OS_O_DIRECT;
278 if (td_write(td) || td_rw(td)) {
281 if (f->filetype == FIO_TYPE_FILE)
284 f->fd = open(f->file_name, flags, 0600);
286 if (f->filetype == FIO_TYPE_CHAR)
291 f->fd = open(f->file_name, flags);
297 td_verror(td, __e, "open");
298 if (__e == EINVAL && td->odirect)
299 log_err("fio: destination does not support O_DIRECT\n");
303 if (get_file_size(td, f))
306 if (td->invalidate_cache && file_invalidate_cache(td, f))
309 if (!td_random(td)) {
310 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
311 td_verror(td, errno, "fadvise");
315 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
316 td_verror(td, errno, "fadvise");
327 int open_files(struct thread_data *td)
333 for_each_file(td, f, i) {
334 err = td_io_open_file(td, f);
338 if (td->open_files == td->nr_open_files)
345 for_each_file(td, f, i)
346 td_io_close_file(td, f);
351 int setup_files(struct thread_data *td)
358 * if ioengine defines a setup() method, it's responsible for
359 * setting up everything in the td->files[] area.
361 if (td->io_ops->setup)
362 return td->io_ops->setup(td);
364 if (create_files(td))
367 err = open_files(td);
372 * Recalculate the total file size now that files are set up.
374 td->total_file_size = 0;
375 for_each_file(td, f, i)
376 td->total_file_size += f->file_size;
378 td->io_size = td->total_file_size;
379 if (td->io_size == 0) {
380 log_err("%s: no io blocks\n", td->name);
381 td_verror(td, EINVAL, "total_file_size");
386 td->zone_size = td->io_size;
388 td->total_io_size = td->io_size * td->loops;
390 for_each_file(td, f, i)
391 td_io_close_file(td, f);
396 void close_files(struct thread_data *td)
401 for_each_file(td, f, i) {
402 if (!f->file_name && (f->flags & FIO_FILE_UNLINK) &&
403 f->filetype == FIO_TYPE_FILE) {
404 unlink(f->file_name);
409 td_io_close_file(td, f);
421 static void get_file_type(struct fio_file *f)
425 f->filetype = FIO_TYPE_FILE;
427 if (!lstat(f->file_name, &sb)) {
428 if (S_ISBLK(sb.st_mode))
429 f->filetype = FIO_TYPE_BD;
430 else if (S_ISCHR(sb.st_mode))
431 f->filetype = FIO_TYPE_CHAR;
435 void add_file(struct thread_data *td, const char *fname)
437 int cur_files = td->files_index;
440 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
442 f = &td->files[cur_files];
443 memset(f, 0, sizeof(*f));
445 f->file_name = strdup(fname);
450 if (f->filetype == FIO_TYPE_FILE)
451 td->nr_normal_files++;
454 void get_file(struct fio_file *f)
459 void put_file(struct thread_data *td, struct fio_file *f)
461 if (!(f->flags & FIO_FILE_OPEN))
464 assert(f->references);
468 if (td->io_ops->close_file)
469 td->io_ops->close_file(td, f);
471 f->flags &= ~FIO_FILE_OPEN;