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 b = malloc(td->max_bs);
48 memset(b, 0, td->max_bs);
51 while (left && !td->terminate) {
56 r = write(f->fd, b, bs);
73 else if (td->create_fsync)
86 static int create_files(struct thread_data *td)
89 int i, err, need_create;
92 * unless specifically asked for overwrite, let normal io extend it
95 for_each_file(td, f, i)
96 f->file_size = td->total_file_size / td->nr_files;
101 if (!td->total_file_size) {
102 log_err("Need size for create\n");
103 td_verror(td, EINVAL);
108 for_each_file(td, f, i) {
109 f->file_size = td->total_file_size / td->nr_files;
110 need_create += file_ok(td, f);
113 td->io_size = td->total_file_size;
119 fprintf(f_out, "%s: Laying out IO file(s) (%d x %LuMiB == %LuMiB)\n",
120 td->name, td->nr_files,
121 (td->total_file_size >> 20) / td->nr_files,
122 td->total_file_size >> 20);
125 for_each_file(td, f, i) {
126 if (file_ok(td, f)) {
127 err = create_file(td, f);
137 static int file_size(struct thread_data *td, struct fio_file *f)
142 if (fstat(f->fd, &st) == -1) {
143 td_verror(td, errno);
147 f->real_file_size = st.st_size;
149 if (!f->file_size || f->file_size > f->real_file_size)
150 f->file_size = f->real_file_size;
153 f->file_size -= f->file_offset;
157 static int bdev_size(struct thread_data *td, struct fio_file *f)
159 unsigned long long bytes;
162 r = blockdev_size(f->fd, &bytes);
168 f->real_file_size = bytes;
171 * no extend possibilities, so limit size to device size if too large
173 if (!f->file_size || f->file_size > f->real_file_size)
174 f->file_size = f->real_file_size;
176 f->file_size -= f->file_offset;
180 static int get_file_size(struct thread_data *td, struct fio_file *f)
184 if (td->filetype == FIO_TYPE_FILE)
185 ret = file_size(td, f);
186 else if (td->filetype == FIO_TYPE_BD)
187 ret = bdev_size(td, f);
189 f->real_file_size = -1;
194 if (f->file_offset > f->real_file_size) {
195 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
202 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
207 * FIXME: add blockdev flushing too
209 if (td->io_ops->flags & FIO_MMAPIO)
210 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
211 else if (td->filetype == FIO_TYPE_FILE)
212 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
213 else if (td->filetype == FIO_TYPE_BD)
214 ret = blockdev_invalidate_cache(f->fd);
215 else if (td->filetype == FIO_TYPE_CHAR)
219 td_verror(td, errno);
226 static int __setup_file_mmap(struct thread_data *td, struct fio_file *f)
231 flags = PROT_READ | PROT_WRITE;
232 else if (td_write(td)) {
235 if (td->verify != VERIFY_NONE)
240 f->mmap = mmap(NULL, f->file_size, flags, MAP_SHARED, f->fd, f->file_offset);
241 if (f->mmap == MAP_FAILED) {
243 td_verror(td, errno);
247 if (td->invalidate_cache && file_invalidate_cache(td, f))
250 if (td->sequential) {
251 if (madvise(f->mmap, f->file_size, MADV_SEQUENTIAL) < 0) {
252 td_verror(td, errno);
256 if (madvise(f->mmap, f->file_size, MADV_RANDOM) < 0) {
257 td_verror(td, errno);
265 static int setup_files_mmap(struct thread_data *td)
270 for_each_file(td, f, i) {
271 err = __setup_file_mmap(td, f);
279 static int __setup_file_plain(struct thread_data *td, struct fio_file *f)
281 if (td->invalidate_cache && file_invalidate_cache(td, f))
284 if (td->sequential) {
285 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
286 td_verror(td, errno);
290 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
291 td_verror(td, errno);
299 static int setup_files_plain(struct thread_data *td)
304 for_each_file(td, f, i) {
305 err = __setup_file_plain(td, f);
313 static int setup_file(struct thread_data *td, struct fio_file *f)
318 flags |= OS_O_DIRECT;
320 if (td_write(td) || td_rw(td)) {
321 if (td->filetype == FIO_TYPE_FILE) {
332 f->fd = open(f->file_name, flags, 0600);
334 if (td->filetype == FIO_TYPE_CHAR)
339 f->fd = open(f->file_name, flags);
343 td_verror(td, errno);
347 if (get_file_size(td, f))
353 int setup_files(struct thread_data *td)
359 * if ioengine defines a setup() method, it's responsible for
360 * setting up everything in the td->files[] area.
362 if (td->io_ops->setup)
363 return td->io_ops->setup(td);
365 if (create_files(td))
368 for_each_file(td, f, i) {
369 err = setup_file(td, f);
374 if (td->io_size == 0) {
375 log_err("%s: no io blocks\n", td->name);
376 td_verror(td, EINVAL);
381 td->zone_size = td->io_size;
383 td->total_io_size = td->io_size * td->loops;
385 if (td->io_ops->flags & FIO_MMAPIO)
386 return setup_files_mmap(td);
388 return setup_files_plain(td);
391 void close_files(struct thread_data *td)
396 for_each_file(td, f, i) {
398 if (td->unlink && td->filetype == FIO_TYPE_FILE)
399 unlink(f->file_name);
404 munmap(f->mmap, f->file_size);