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);
53 memset(b, 0, td->max_bs);
56 while (left && !td->terminate) {
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;
97 * unless specifically asked for overwrite, let normal io extend it
100 for_each_file(td, f, i)
101 f->file_size = td->total_file_size / td->nr_files;
107 if (td->filetype == FIO_TYPE_FILE)
108 for_each_file(td, f, i)
109 need_create += file_ok(td, f);
114 if (!td->total_file_size) {
115 log_err("Need size for create\n");
116 td_verror(td, EINVAL);
121 fprintf(f_out, "%s: Laying out IO file(s) (%d x %LuMiB == %LuMiB)\n",
122 td->name, td->nr_uniq_files,
123 (td->total_file_size >> 20) / td->nr_uniq_files,
124 td->total_file_size >> 20);
127 for_each_file(td, f, i) {
128 if (file_ok(td, f)) {
129 err = create_file(td, f);
139 static int file_size(struct thread_data *td, struct fio_file *f)
144 if (fstat(f->fd, &st) == -1) {
145 td_verror(td, errno);
149 f->real_file_size = st.st_size;
151 if (!f->file_size || f->file_size > f->real_file_size)
152 f->file_size = f->real_file_size;
155 f->file_size -= f->file_offset;
159 static int bdev_size(struct thread_data *td, struct fio_file *f)
161 unsigned long long bytes;
164 r = blockdev_size(f->fd, &bytes);
170 f->real_file_size = bytes;
173 * no extend possibilities, so limit size to device size if too large
175 if (!f->file_size || f->file_size > f->real_file_size)
176 f->file_size = f->real_file_size;
178 f->file_size -= f->file_offset;
182 static int get_file_size(struct thread_data *td, struct fio_file *f)
186 if (td->filetype == FIO_TYPE_FILE)
187 ret = file_size(td, f);
188 else if (td->filetype == FIO_TYPE_BD)
189 ret = bdev_size(td, f);
191 f->real_file_size = -1;
196 if (f->file_offset > f->real_file_size) {
197 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
204 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
209 * FIXME: add blockdev flushing too
211 if (td->io_ops->flags & FIO_MMAPIO)
212 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
213 else if (td->filetype == FIO_TYPE_FILE)
214 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
215 else if (td->filetype == FIO_TYPE_BD)
216 ret = blockdev_invalidate_cache(f->fd);
217 else if (td->filetype == FIO_TYPE_CHAR)
221 td_verror(td, errno);
228 static int __setup_file_mmap(struct thread_data *td, struct fio_file *f)
233 flags = PROT_READ | PROT_WRITE;
234 else if (td_write(td)) {
237 if (td->verify != VERIFY_NONE)
242 f->mmap = mmap(NULL, f->file_size, flags, MAP_SHARED, f->fd, f->file_offset);
243 if (f->mmap == MAP_FAILED) {
245 td_verror(td, errno);
249 if (td->invalidate_cache && file_invalidate_cache(td, f))
252 if (td->sequential) {
253 if (madvise(f->mmap, f->file_size, MADV_SEQUENTIAL) < 0) {
254 td_verror(td, errno);
258 if (madvise(f->mmap, f->file_size, MADV_RANDOM) < 0) {
259 td_verror(td, errno);
267 static int setup_files_mmap(struct thread_data *td)
272 for_each_file(td, f, i) {
273 err = __setup_file_mmap(td, f);
281 static int __setup_file_plain(struct thread_data *td, struct fio_file *f)
283 if (td->invalidate_cache && file_invalidate_cache(td, f))
286 if (td->sequential) {
287 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
288 td_verror(td, errno);
292 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
293 td_verror(td, errno);
301 static int setup_files_plain(struct thread_data *td)
306 for_each_file(td, f, i) {
307 err = __setup_file_plain(td, f);
315 static int setup_file(struct thread_data *td, struct fio_file *f)
320 flags |= OS_O_DIRECT;
322 if (td_write(td) || td_rw(td)) {
323 if (td->filetype == FIO_TYPE_FILE) {
334 f->fd = open(f->file_name, flags, 0600);
336 if (td->filetype == FIO_TYPE_CHAR)
341 f->fd = open(f->file_name, flags);
345 td_verror(td, errno);
349 if (get_file_size(td, f))
355 int setup_files(struct thread_data *td)
361 * if ioengine defines a setup() method, it's responsible for
362 * setting up everything in the td->files[] area.
364 if (td->io_ops->setup)
365 return td->io_ops->setup(td);
367 if (create_files(td))
371 for_each_file(td, f, i) {
372 err = setup_file(td, f);
381 * Recalculate the total file size now that files are set up.
383 td->total_file_size = 0;
384 for_each_file(td, f, i)
385 td->total_file_size += f->file_size;
387 td->io_size = td->total_file_size;
388 if (td->io_size == 0) {
389 log_err("%s: no io blocks\n", td->name);
390 td_verror(td, EINVAL);
395 td->zone_size = td->io_size;
397 td->total_io_size = td->io_size * td->loops;
399 if (td->io_ops->flags & FIO_MMAPIO)
400 return setup_files_mmap(td);
402 return setup_files_plain(td);
405 void close_files(struct thread_data *td)
410 for_each_file(td, f, i) {
412 if (td->unlink && td->filetype == FIO_TYPE_FILE &&
414 unlink(f->file_name);
423 munmap(f->mmap, f->file_size);