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;
102 if (td->filetype == FIO_TYPE_FILE)
103 for_each_file(td, f, i)
104 need_create += file_ok(td, f);
109 if (!td->total_file_size) {
110 log_err("Need size for create\n");
111 td_verror(td, EINVAL);
116 fprintf(f_out, "%s: Laying out IO file(s) (%d x %LuMiB == %LuMiB)\n",
117 td->name, td->nr_uniq_files,
118 (td->total_file_size >> 20) / td->nr_uniq_files,
119 td->total_file_size >> 20);
122 for_each_file(td, f, i) {
123 if (file_ok(td, f)) {
124 err = create_file(td, f);
134 static int file_size(struct thread_data *td, struct fio_file *f)
139 if (fstat(f->fd, &st) == -1) {
140 td_verror(td, errno);
144 f->real_file_size = st.st_size;
146 if (!f->file_size || f->file_size > f->real_file_size)
147 f->file_size = f->real_file_size;
150 f->file_size -= f->file_offset;
154 static int bdev_size(struct thread_data *td, struct fio_file *f)
156 unsigned long long bytes;
159 r = blockdev_size(f->fd, &bytes);
165 f->real_file_size = bytes;
168 * no extend possibilities, so limit size to device size if too large
170 if (!f->file_size || f->file_size > f->real_file_size)
171 f->file_size = f->real_file_size;
173 f->file_size -= f->file_offset;
177 static int get_file_size(struct thread_data *td, struct fio_file *f)
181 if (td->filetype == FIO_TYPE_FILE)
182 ret = file_size(td, f);
183 else if (td->filetype == FIO_TYPE_BD)
184 ret = bdev_size(td, f);
186 f->real_file_size = -1;
191 if (f->file_offset > f->real_file_size) {
192 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
199 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
204 * FIXME: add blockdev flushing too
206 if (td->io_ops->flags & FIO_MMAPIO)
207 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
208 else if (td->filetype == FIO_TYPE_FILE)
209 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
210 else if (td->filetype == FIO_TYPE_BD)
211 ret = blockdev_invalidate_cache(f->fd);
212 else if (td->filetype == FIO_TYPE_CHAR)
216 td_verror(td, errno);
223 static int __setup_file_mmap(struct thread_data *td, struct fio_file *f)
228 flags = PROT_READ | PROT_WRITE;
229 else if (td_write(td)) {
232 if (td->verify != VERIFY_NONE)
237 f->mmap = mmap(NULL, f->file_size, flags, MAP_SHARED, f->fd, f->file_offset);
238 if (f->mmap == MAP_FAILED) {
240 td_verror(td, errno);
244 if (td->invalidate_cache && file_invalidate_cache(td, f))
247 if (td->sequential) {
248 if (madvise(f->mmap, f->file_size, MADV_SEQUENTIAL) < 0) {
249 td_verror(td, errno);
253 if (madvise(f->mmap, f->file_size, MADV_RANDOM) < 0) {
254 td_verror(td, errno);
262 static int setup_files_mmap(struct thread_data *td)
267 for_each_file(td, f, i) {
268 err = __setup_file_mmap(td, f);
276 static int __setup_file_plain(struct thread_data *td, struct fio_file *f)
278 if (td->invalidate_cache && file_invalidate_cache(td, f))
281 if (td->sequential) {
282 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
283 td_verror(td, errno);
287 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
288 td_verror(td, errno);
296 static int setup_files_plain(struct thread_data *td)
301 for_each_file(td, f, i) {
302 err = __setup_file_plain(td, f);
310 static int setup_file(struct thread_data *td, struct fio_file *f)
315 flags |= OS_O_DIRECT;
317 if (td_write(td) || td_rw(td)) {
318 if (td->filetype == FIO_TYPE_FILE) {
329 f->fd = open(f->file_name, flags, 0600);
331 if (td->filetype == FIO_TYPE_CHAR)
336 f->fd = open(f->file_name, flags);
340 td_verror(td, errno);
344 if (get_file_size(td, f))
350 int setup_files(struct thread_data *td)
356 * if ioengine defines a setup() method, it's responsible for
357 * setting up everything in the td->files[] area.
359 if (td->io_ops->setup)
360 return td->io_ops->setup(td);
362 if (create_files(td))
366 for_each_file(td, f, i) {
367 err = setup_file(td, f);
376 * Recalculate the total file size now that files are set up.
378 td->total_file_size = 0;
379 for_each_file(td, f, i)
380 td->total_file_size += f->file_size;
382 td->io_size = td->total_file_size;
383 if (td->io_size == 0) {
384 log_err("%s: no io blocks\n", td->name);
385 td_verror(td, EINVAL);
390 td->zone_size = td->io_size;
392 td->total_io_size = td->io_size * td->loops;
394 if (td->io_ops->flags & FIO_MMAPIO)
395 return setup_files_mmap(td);
397 return setup_files_plain(td);
400 void close_files(struct thread_data *td)
405 for_each_file(td, f, i) {
407 if (td->unlink && td->filetype == FIO_TYPE_FILE &&
409 unlink(f->file_name);
418 munmap(f->mmap, f->file_size);