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 need_create += file_ok(td, f);
113 if (!td->total_file_size) {
114 log_err("Need size for create\n");
115 td_verror(td, EINVAL);
120 fprintf(f_out, "%s: Laying out IO file(s) (%d x %LuMiB == %LuMiB)\n",
121 td->name, td->nr_uniq_files,
122 (td->total_file_size >> 20) / td->nr_uniq_files,
123 td->total_file_size >> 20);
126 for_each_file(td, f, i) {
127 if (file_ok(td, f)) {
128 err = create_file(td, f);
138 static int file_size(struct thread_data *td, struct fio_file *f)
143 if (fstat(f->fd, &st) == -1) {
144 td_verror(td, errno);
148 f->real_file_size = st.st_size;
150 if (!f->file_size || f->file_size > f->real_file_size)
151 f->file_size = f->real_file_size;
154 f->file_size -= f->file_offset;
158 static int bdev_size(struct thread_data *td, struct fio_file *f)
160 unsigned long long bytes;
163 r = blockdev_size(f->fd, &bytes);
169 f->real_file_size = bytes;
172 * no extend possibilities, so limit size to device size if too large
174 if (!f->file_size || f->file_size > f->real_file_size)
175 f->file_size = f->real_file_size;
177 f->file_size -= f->file_offset;
181 static int get_file_size(struct thread_data *td, struct fio_file *f)
185 if (td->filetype == FIO_TYPE_FILE)
186 ret = file_size(td, f);
187 else if (td->filetype == FIO_TYPE_BD)
188 ret = bdev_size(td, f);
190 f->real_file_size = -1;
195 if (f->file_offset > f->real_file_size) {
196 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
203 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
208 * FIXME: add blockdev flushing too
210 if (td->io_ops->flags & FIO_MMAPIO)
211 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
212 else if (td->filetype == FIO_TYPE_FILE)
213 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
214 else if (td->filetype == FIO_TYPE_BD)
215 ret = blockdev_invalidate_cache(f->fd);
216 else if (td->filetype == FIO_TYPE_CHAR)
220 td_verror(td, errno);
227 static int __setup_file_mmap(struct thread_data *td, struct fio_file *f)
232 flags = PROT_READ | PROT_WRITE;
233 else if (td_write(td)) {
236 if (td->verify != VERIFY_NONE)
241 f->mmap = mmap(NULL, f->file_size, flags, MAP_SHARED, f->fd, f->file_offset);
242 if (f->mmap == MAP_FAILED) {
244 td_verror(td, errno);
248 if (td->invalidate_cache && file_invalidate_cache(td, f))
251 if (td->sequential) {
252 if (madvise(f->mmap, f->file_size, MADV_SEQUENTIAL) < 0) {
253 td_verror(td, errno);
257 if (madvise(f->mmap, f->file_size, MADV_RANDOM) < 0) {
258 td_verror(td, errno);
266 static int setup_files_mmap(struct thread_data *td)
271 for_each_file(td, f, i) {
272 err = __setup_file_mmap(td, f);
280 static int __setup_file_plain(struct thread_data *td, struct fio_file *f)
282 if (td->invalidate_cache && file_invalidate_cache(td, f))
285 if (td->sequential) {
286 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
287 td_verror(td, errno);
291 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
292 td_verror(td, errno);
300 static int setup_files_plain(struct thread_data *td)
305 for_each_file(td, f, i) {
306 err = __setup_file_plain(td, f);
314 static int setup_file(struct thread_data *td, struct fio_file *f)
319 flags |= OS_O_DIRECT;
321 if (td_write(td) || td_rw(td)) {
322 if (td->filetype == FIO_TYPE_FILE) {
333 f->fd = open(f->file_name, flags, 0600);
335 if (td->filetype == FIO_TYPE_CHAR)
340 f->fd = open(f->file_name, flags);
344 td_verror(td, errno);
348 if (get_file_size(td, f))
354 int setup_files(struct thread_data *td)
360 * if ioengine defines a setup() method, it's responsible for
361 * setting up everything in the td->files[] area.
363 if (td->io_ops->setup)
364 return td->io_ops->setup(td);
366 if (create_files(td))
370 for_each_file(td, f, i) {
371 err = setup_file(td, f);
380 * Recalculate the total file size now that files are set up.
382 td->total_file_size = 0;
383 for_each_file(td, f, i)
384 td->total_file_size += f->file_size;
386 td->io_size = td->total_file_size;
387 if (td->io_size == 0) {
388 log_err("%s: no io blocks\n", td->name);
389 td_verror(td, EINVAL);
394 td->zone_size = td->io_size;
396 td->total_io_size = td->io_size * td->loops;
398 if (td->io_ops->flags & FIO_MMAPIO)
399 return setup_files_mmap(td);
401 return setup_files_plain(td);
404 void close_files(struct thread_data *td)
409 for_each_file(td, f, i) {
411 if (td->unlink && td->filetype == FIO_TYPE_FILE &&
413 unlink(f->file_name);
422 munmap(f->mmap, f->file_size);