11 static int create_file(struct thread_data *td, struct fio_file *f)
13 unsigned long long left;
18 f->fd = open(f->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
24 if (ftruncate(f->fd, f->file_size) == -1) {
29 b = malloc(td->max_bs);
30 memset(b, 0, td->max_bs);
33 while (left && !td->terminate) {
38 r = write(f->fd, b, bs);
55 else if (td->create_fsync)
68 static int create_files(struct thread_data *td)
74 * unless specifically asked for overwrite, let normal io extend it
77 td->io_size = td->total_file_size;
78 for_each_file(td, f, i)
79 f->file_size = td->total_file_size / td->nr_files;
84 if (!td->total_file_size) {
85 log_err("Need size for create\n");
86 td_verror(td, EINVAL);
91 fprintf(f_out, "%s: Laying out IO file(s) (%LuMiB)\n",
92 td->name, td->total_file_size >> 20);
95 for_each_file(td, f, i) {
96 f->file_size = td->total_file_size / td->nr_files;
97 err = create_file(td, f);
100 td->io_size += f->file_size;
107 static int file_size(struct thread_data *td, struct fio_file *f)
112 if (fstat(f->fd, &st) == -1) {
113 td_verror(td, errno);
117 f->real_file_size = st.st_size;
119 if (!f->file_size || f->file_size > f->real_file_size)
120 f->file_size = f->real_file_size;
123 f->file_size -= f->file_offset;
127 static int bdev_size(struct thread_data *td, struct fio_file *f)
129 unsigned long long bytes;
132 r = blockdev_size(f->fd, &bytes);
138 f->real_file_size = bytes;
141 * no extend possibilities, so limit size to device size if too large
143 if (!f->file_size || f->file_size > f->real_file_size)
144 f->file_size = f->real_file_size;
146 f->file_size -= f->file_offset;
150 static int get_file_size(struct thread_data *td, struct fio_file *f)
154 if (td->filetype == FIO_TYPE_FILE)
155 ret = file_size(td, f);
156 else if (td->filetype == FIO_TYPE_BD)
157 ret = bdev_size(td, f);
159 f->real_file_size = -1;
164 if (f->file_offset > f->real_file_size) {
165 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
169 td->io_size += f->file_size;
173 static int __setup_file_mmap(struct thread_data *td, struct fio_file *f)
178 flags = PROT_READ | PROT_WRITE;
179 else if (td_write(td)) {
182 if (td->verify != VERIFY_NONE)
187 f->mmap = mmap(NULL, f->file_size, flags, MAP_SHARED, f->fd, f->file_offset);
188 if (f->mmap == MAP_FAILED) {
190 td_verror(td, errno);
194 if (td->invalidate_cache) {
195 if (madvise(f->mmap, f->file_size, MADV_DONTNEED) < 0) {
196 td_verror(td, errno);
201 if (td->sequential) {
202 if (madvise(f->mmap, f->file_size, MADV_SEQUENTIAL) < 0) {
203 td_verror(td, errno);
207 if (madvise(f->mmap, f->file_size, MADV_RANDOM) < 0) {
208 td_verror(td, errno);
216 static int setup_files_mmap(struct thread_data *td)
221 for_each_file(td, f, i) {
222 err = __setup_file_mmap(td, f);
230 static int __setup_file_plain(struct thread_data *td, struct fio_file *f)
232 if (td->invalidate_cache) {
233 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED) < 0) {
234 td_verror(td, errno);
239 if (td->sequential) {
240 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
241 td_verror(td, errno);
245 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
246 td_verror(td, errno);
254 static int setup_files_plain(struct thread_data *td)
259 for_each_file(td, f, i) {
260 err = __setup_file_plain(td, f);
268 static int setup_file(struct thread_data *td, struct fio_file *f)
273 if (stat(f->file_name, &st) == -1) {
274 if (errno != ENOENT) {
275 td_verror(td, errno);
278 if (!td->create_file) {
279 td_verror(td, ENOENT);
282 if (create_file(td, f))
284 } else if (td->filetype == FIO_TYPE_FILE &&
285 st.st_size < (off_t) f->file_size) {
286 if (create_file(td, f))
291 flags |= OS_O_DIRECT;
293 if (td_write(td) || td_rw(td)) {
294 if (td->filetype == FIO_TYPE_FILE) {
305 f->fd = open(f->file_name, flags, 0600);
307 if (td->filetype == FIO_TYPE_CHAR)
312 f->fd = open(f->file_name, flags);
316 td_verror(td, errno);
320 if (get_file_size(td, f))
326 int setup_files(struct thread_data *td)
332 * if ioengine defines a setup() method, it's responsible for
333 * setting up everything in the td->files[] area.
335 if (td->io_ops->setup)
336 return td->io_ops->setup(td);
338 if (create_files(td))
341 for_each_file(td, f, i) {
342 err = setup_file(td, f);
347 if (td->io_size == 0) {
348 log_err("%s: no io blocks\n", td->name);
349 td_verror(td, EINVAL);
354 td->zone_size = td->io_size;
356 td->total_io_size = td->io_size * td->loops;
358 if (td->io_ops->flags & FIO_MMAPIO)
359 return setup_files_mmap(td);
361 return setup_files_plain(td);
364 void close_files(struct thread_data *td)
368 for (i = 0; i < td->nr_files; i++) {
369 struct fio_file *f = &td->files[i];
376 munmap(f->mmap, f->file_size);