verify: verify_state_gen_name() should check size
[fio.git] / filesetup.c
... / ...
CommitLineData
1#include <unistd.h>
2#include <fcntl.h>
3#include <string.h>
4#include <assert.h>
5#include <dirent.h>
6#include <libgen.h>
7#include <sys/stat.h>
8#include <sys/mman.h>
9#include <sys/types.h>
10
11#include "fio.h"
12#include "smalloc.h"
13#include "filehash.h"
14#include "options.h"
15#include "os/os.h"
16#include "hash.h"
17#include "lib/axmap.h"
18
19#ifdef CONFIG_LINUX_FALLOCATE
20#include <linux/falloc.h>
21#endif
22
23static int root_warn;
24
25static FLIST_HEAD(filename_list);
26
27static inline void clear_error(struct thread_data *td)
28{
29 td->error = 0;
30 td->verror[0] = '\0';
31}
32
33/*
34 * Leaves f->fd open on success, caller must close
35 */
36static int extend_file(struct thread_data *td, struct fio_file *f)
37{
38 int r, new_layout = 0, unlink_file = 0, flags;
39 unsigned long long left;
40 unsigned int bs;
41 char *b = NULL;
42
43 if (read_only) {
44 log_err("fio: refusing extend of file due to read-only\n");
45 return 0;
46 }
47
48 /*
49 * check if we need to lay the file out complete again. fio
50 * does that for operations involving reads, or for writes
51 * where overwrite is set
52 */
53 if (td_read(td) ||
54 (td_write(td) && td->o.overwrite && !td->o.file_append) ||
55 (td_write(td) && td->io_ops->flags & FIO_NOEXTEND))
56 new_layout = 1;
57 if (td_write(td) && !td->o.overwrite && !td->o.file_append)
58 unlink_file = 1;
59
60 if (unlink_file || new_layout) {
61 dprint(FD_FILE, "layout unlink %s\n", f->file_name);
62 if ((td_io_unlink_file(td, f) < 0) && (errno != ENOENT)) {
63 td_verror(td, errno, "unlink");
64 return 1;
65 }
66 }
67
68 flags = O_WRONLY | O_CREAT;
69 if (new_layout)
70 flags |= O_TRUNC;
71
72#ifdef WIN32
73 flags |= _O_BINARY;
74#endif
75
76 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
77 f->fd = open(f->file_name, flags, 0644);
78 if (f->fd < 0) {
79 td_verror(td, errno, "open");
80 return 1;
81 }
82
83#ifdef CONFIG_POSIX_FALLOCATE
84 if (!td->o.fill_device) {
85 switch (td->o.fallocate_mode) {
86 case FIO_FALLOCATE_NONE:
87 break;
88 case FIO_FALLOCATE_POSIX:
89 dprint(FD_FILE, "posix_fallocate file %s size %llu\n",
90 f->file_name,
91 (unsigned long long) f->real_file_size);
92
93 r = posix_fallocate(f->fd, 0, f->real_file_size);
94 if (r > 0) {
95 log_err("fio: posix_fallocate fails: %s\n",
96 strerror(r));
97 }
98 break;
99#ifdef CONFIG_LINUX_FALLOCATE
100 case FIO_FALLOCATE_KEEP_SIZE:
101 dprint(FD_FILE,
102 "fallocate(FALLOC_FL_KEEP_SIZE) "
103 "file %s size %llu\n", f->file_name,
104 (unsigned long long) f->real_file_size);
105
106 r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0,
107 f->real_file_size);
108 if (r != 0)
109 td_verror(td, errno, "fallocate");
110
111 break;
112#endif /* CONFIG_LINUX_FALLOCATE */
113 default:
114 log_err("fio: unknown fallocate mode: %d\n",
115 td->o.fallocate_mode);
116 assert(0);
117 }
118 }
119#endif /* CONFIG_POSIX_FALLOCATE */
120
121 if (!new_layout)
122 goto done;
123
124 /*
125 * The size will be -1ULL when fill_device is used, so don't truncate
126 * or fallocate this file, just write it
127 */
128 if (!td->o.fill_device) {
129 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
130 (unsigned long long) f->real_file_size);
131 if (ftruncate(f->fd, f->real_file_size) == -1) {
132 if (errno != EFBIG) {
133 td_verror(td, errno, "ftruncate");
134 goto err;
135 }
136 }
137 }
138
139 b = malloc(td->o.max_bs[DDIR_WRITE]);
140
141 left = f->real_file_size;
142 while (left && !td->terminate) {
143 bs = td->o.max_bs[DDIR_WRITE];
144 if (bs > left)
145 bs = left;
146
147 fill_io_buffer(td, b, bs, bs);
148
149 r = write(f->fd, b, bs);
150
151 if (r > 0) {
152 left -= r;
153 continue;
154 } else {
155 if (r < 0) {
156 int __e = errno;
157
158 if (__e == ENOSPC) {
159 if (td->o.fill_device)
160 break;
161 log_info("fio: ENOSPC on laying out "
162 "file, stopping\n");
163 break;
164 }
165 td_verror(td, errno, "write");
166 } else
167 td_verror(td, EIO, "write");
168
169 break;
170 }
171 }
172
173 if (td->terminate) {
174 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
175 td_io_unlink_file(td, f);
176 } else if (td->o.create_fsync) {
177 if (fsync(f->fd) < 0) {
178 td_verror(td, errno, "fsync");
179 goto err;
180 }
181 }
182 if (td->o.fill_device && !td_write(td)) {
183 fio_file_clear_size_known(f);
184 if (td_io_get_file_size(td, f))
185 goto err;
186 if (f->io_size > f->real_file_size)
187 f->io_size = f->real_file_size;
188 }
189
190 free(b);
191done:
192 return 0;
193err:
194 close(f->fd);
195 f->fd = -1;
196 if (b)
197 free(b);
198 return 1;
199}
200
201static int pre_read_file(struct thread_data *td, struct fio_file *f)
202{
203 int ret = 0, r, did_open = 0, old_runstate;
204 unsigned long long left;
205 unsigned int bs;
206 char *b;
207
208 if (td->io_ops->flags & FIO_PIPEIO)
209 return 0;
210
211 if (!fio_file_open(f)) {
212 if (td->io_ops->open_file(td, f)) {
213 log_err("fio: cannot pre-read, failed to open file\n");
214 return 1;
215 }
216 did_open = 1;
217 }
218
219 old_runstate = td_bump_runstate(td, TD_PRE_READING);
220
221 bs = td->o.max_bs[DDIR_READ];
222 b = malloc(bs);
223 memset(b, 0, bs);
224
225 if (lseek(f->fd, f->file_offset, SEEK_SET) < 0) {
226 td_verror(td, errno, "lseek");
227 log_err("fio: failed to lseek pre-read file\n");
228 ret = 1;
229 goto error;
230 }
231
232 left = f->io_size;
233
234 while (left && !td->terminate) {
235 if (bs > left)
236 bs = left;
237
238 r = read(f->fd, b, bs);
239
240 if (r == (int) bs) {
241 left -= bs;
242 continue;
243 } else {
244 td_verror(td, EIO, "pre_read");
245 break;
246 }
247 }
248
249error:
250 td_restore_runstate(td, old_runstate);
251
252 if (did_open)
253 td->io_ops->close_file(td, f);
254
255 free(b);
256 return ret;
257}
258
259static unsigned long long get_rand_file_size(struct thread_data *td)
260{
261 unsigned long long ret, sized;
262 unsigned long r;
263
264 r = __rand(&td->file_size_state);
265 sized = td->o.file_size_high - td->o.file_size_low;
266 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0)));
267 ret += td->o.file_size_low;
268 ret -= (ret % td->o.rw_min_bs);
269 return ret;
270}
271
272static int file_size(struct thread_data *td, struct fio_file *f)
273{
274 struct stat st;
275
276 if (stat(f->file_name, &st) == -1) {
277 td_verror(td, errno, "fstat");
278 return 1;
279 }
280
281 f->real_file_size = st.st_size;
282 return 0;
283}
284
285static int bdev_size(struct thread_data *td, struct fio_file *f)
286{
287 unsigned long long bytes = 0;
288 int r;
289
290 if (td->io_ops->open_file(td, f)) {
291 log_err("fio: failed opening blockdev %s for size check\n",
292 f->file_name);
293 return 1;
294 }
295
296 r = blockdev_size(f, &bytes);
297 if (r) {
298 td_verror(td, r, "blockdev_size");
299 goto err;
300 }
301
302 if (!bytes) {
303 log_err("%s: zero sized block device?\n", f->file_name);
304 goto err;
305 }
306
307 f->real_file_size = bytes;
308 td->io_ops->close_file(td, f);
309 return 0;
310err:
311 td->io_ops->close_file(td, f);
312 return 1;
313}
314
315static int char_size(struct thread_data *td, struct fio_file *f)
316{
317#ifdef FIO_HAVE_CHARDEV_SIZE
318 unsigned long long bytes = 0;
319 int r;
320
321 if (td->io_ops->open_file(td, f)) {
322 log_err("fio: failed opening blockdev %s for size check\n",
323 f->file_name);
324 return 1;
325 }
326
327 r = chardev_size(f, &bytes);
328 if (r) {
329 td_verror(td, r, "chardev_size");
330 goto err;
331 }
332
333 if (!bytes) {
334 log_err("%s: zero sized char device?\n", f->file_name);
335 goto err;
336 }
337
338 f->real_file_size = bytes;
339 td->io_ops->close_file(td, f);
340 return 0;
341err:
342 td->io_ops->close_file(td, f);
343 return 1;
344#else
345 f->real_file_size = -1ULL;
346 return 0;
347#endif
348}
349
350static int get_file_size(struct thread_data *td, struct fio_file *f)
351{
352 int ret = 0;
353
354 if (fio_file_size_known(f))
355 return 0;
356
357 if (f->filetype == FIO_TYPE_FILE)
358 ret = file_size(td, f);
359 else if (f->filetype == FIO_TYPE_BD)
360 ret = bdev_size(td, f);
361 else if (f->filetype == FIO_TYPE_CHAR)
362 ret = char_size(td, f);
363 else
364 f->real_file_size = -1;
365
366 if (ret)
367 return ret;
368
369 if (f->file_offset > f->real_file_size) {
370 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
371 (unsigned long long) f->file_offset,
372 (unsigned long long) f->real_file_size);
373 return 1;
374 }
375
376 fio_file_set_size_known(f);
377 return 0;
378}
379
380static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
381 unsigned long long off,
382 unsigned long long len)
383{
384 int ret = 0;
385
386#ifdef CONFIG_ESX
387 return 0;
388#endif
389
390 if (len == -1ULL)
391 len = f->io_size;
392 if (off == -1ULL)
393 off = f->file_offset;
394
395 if (len == -1ULL || off == -1ULL)
396 return 0;
397
398 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
399 len);
400
401 if (td->io_ops->invalidate)
402 ret = td->io_ops->invalidate(td, f);
403 else if (f->mmap_ptr) {
404 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
405#ifdef FIO_MADV_FREE
406 if (f->filetype == FIO_TYPE_BD)
407 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
408#endif
409 } else if (f->filetype == FIO_TYPE_FILE) {
410 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
411 } else if (f->filetype == FIO_TYPE_BD) {
412 ret = blockdev_invalidate_cache(f);
413 if (ret < 0 && errno == EACCES && geteuid()) {
414 if (!root_warn) {
415 log_err("fio: only root may flush block "
416 "devices. Cache flush bypassed!\n");
417 root_warn = 1;
418 }
419 ret = 0;
420 }
421 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
422 ret = 0;
423
424 /*
425 * Cache flushing isn't a fatal condition, and we know it will
426 * happen on some platforms where we don't have the proper
427 * function to flush eg block device caches. So just warn and
428 * continue on our way.
429 */
430 if (ret) {
431 log_info("fio: cache invalidation of %s failed: %s\n", f->file_name, strerror(errno));
432 ret = 0;
433 }
434
435 return 0;
436
437}
438
439int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
440{
441 if (!fio_file_open(f))
442 return 0;
443
444 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
445}
446
447int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
448{
449 int ret = 0;
450
451 dprint(FD_FILE, "fd close %s\n", f->file_name);
452
453 remove_file_hash(f);
454
455 if (close(f->fd) < 0)
456 ret = errno;
457
458 f->fd = -1;
459
460 if (f->shadow_fd != -1) {
461 close(f->shadow_fd);
462 f->shadow_fd = -1;
463 }
464
465 f->engine_data = 0;
466 return ret;
467}
468
469int file_lookup_open(struct fio_file *f, int flags)
470{
471 struct fio_file *__f;
472 int from_hash;
473
474 __f = lookup_file_hash(f->file_name);
475 if (__f) {
476 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
477 /*
478 * racy, need the __f->lock locked
479 */
480 f->lock = __f->lock;
481 from_hash = 1;
482 } else {
483 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
484 from_hash = 0;
485 }
486
487#ifdef WIN32
488 flags |= _O_BINARY;
489#endif
490
491 f->fd = open(f->file_name, flags, 0600);
492 return from_hash;
493}
494
495static int file_close_shadow_fds(struct thread_data *td)
496{
497 struct fio_file *f;
498 int num_closed = 0;
499 unsigned int i;
500
501 for_each_file(td, f, i) {
502 if (f->shadow_fd == -1)
503 continue;
504
505 close(f->shadow_fd);
506 f->shadow_fd = -1;
507 num_closed++;
508 }
509
510 return num_closed;
511}
512
513int generic_open_file(struct thread_data *td, struct fio_file *f)
514{
515 int is_std = 0;
516 int flags = 0;
517 int from_hash = 0;
518
519 dprint(FD_FILE, "fd open %s\n", f->file_name);
520
521 if (td_trim(td) && f->filetype != FIO_TYPE_BD) {
522 log_err("fio: trim only applies to block device\n");
523 return 1;
524 }
525
526 if (!strcmp(f->file_name, "-")) {
527 if (td_rw(td)) {
528 log_err("fio: can't read/write to stdin/out\n");
529 return 1;
530 }
531 is_std = 1;
532
533 /*
534 * move output logging to stderr, if we are writing to stdout
535 */
536 if (td_write(td))
537 f_out = stderr;
538 }
539
540 if (td_trim(td))
541 goto skip_flags;
542 if (td->o.odirect)
543 flags |= OS_O_DIRECT;
544 if (td->o.oatomic) {
545 if (!FIO_O_ATOMIC) {
546 td_verror(td, EINVAL, "OS does not support atomic IO");
547 return 1;
548 }
549 flags |= OS_O_DIRECT | FIO_O_ATOMIC;
550 }
551 if (td->o.sync_io)
552 flags |= O_SYNC;
553 if (td->o.create_on_open)
554 flags |= O_CREAT;
555skip_flags:
556 if (f->filetype != FIO_TYPE_FILE)
557 flags |= FIO_O_NOATIME;
558
559open_again:
560 if (td_write(td)) {
561 if (!read_only)
562 flags |= O_RDWR;
563
564 if (f->filetype == FIO_TYPE_FILE)
565 flags |= O_CREAT;
566
567 if (is_std)
568 f->fd = dup(STDOUT_FILENO);
569 else
570 from_hash = file_lookup_open(f, flags);
571 } else if (td_read(td)) {
572 if (f->filetype == FIO_TYPE_CHAR && !read_only)
573 flags |= O_RDWR;
574 else
575 flags |= O_RDONLY;
576
577 if (is_std)
578 f->fd = dup(STDIN_FILENO);
579 else
580 from_hash = file_lookup_open(f, flags);
581 } else { //td trim
582 flags |= O_RDWR;
583 from_hash = file_lookup_open(f, flags);
584 }
585
586 if (f->fd == -1) {
587 char buf[FIO_VERROR_SIZE];
588 int __e = errno;
589
590 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
591 flags &= ~FIO_O_NOATIME;
592 goto open_again;
593 }
594 if (__e == EMFILE && file_close_shadow_fds(td))
595 goto open_again;
596
597 snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
598
599 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
600 log_err("fio: looks like your file system does not " \
601 "support direct=1/buffered=0\n");
602 }
603
604 td_verror(td, __e, buf);
605 return 1;
606 }
607
608 if (!from_hash && f->fd != -1) {
609 if (add_file_hash(f)) {
610 int fio_unused ret;
611
612 /*
613 * Stash away descriptor for later close. This is to
614 * work-around a "feature" on Linux, where a close of
615 * an fd that has been opened for write will trigger
616 * udev to call blkid to check partitions, fs id, etc.
617 * That pollutes the device cache, which can slow down
618 * unbuffered accesses.
619 */
620 if (f->shadow_fd == -1)
621 f->shadow_fd = f->fd;
622 else {
623 /*
624 * OK to ignore, we haven't done anything
625 * with it
626 */
627 ret = generic_close_file(td, f);
628 }
629 goto open_again;
630 }
631 }
632
633 return 0;
634}
635
636int generic_get_file_size(struct thread_data *td, struct fio_file *f)
637{
638 return get_file_size(td, f);
639}
640
641/*
642 * open/close all files, so that ->real_file_size gets set
643 */
644static int get_file_sizes(struct thread_data *td)
645{
646 struct fio_file *f;
647 unsigned int i;
648 int err = 0;
649
650 for_each_file(td, f, i) {
651 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
652 f->file_name);
653
654 if (td_io_get_file_size(td, f)) {
655 if (td->error != ENOENT) {
656 log_err("%s\n", td->verror);
657 err = 1;
658 break;
659 }
660 clear_error(td);
661 }
662
663 if (f->real_file_size == -1ULL && td->o.size)
664 f->real_file_size = td->o.size / td->o.nr_files;
665 }
666
667 return err;
668}
669
670struct fio_mount {
671 struct flist_head list;
672 const char *base;
673 char __base[256];
674 unsigned int key;
675};
676
677/*
678 * Get free number of bytes for each file on each unique mount.
679 */
680static unsigned long long get_fs_free_counts(struct thread_data *td)
681{
682 struct flist_head *n, *tmp;
683 unsigned long long ret = 0;
684 struct fio_mount *fm;
685 FLIST_HEAD(list);
686 struct fio_file *f;
687 unsigned int i;
688
689 for_each_file(td, f, i) {
690 struct stat sb;
691 char buf[256];
692
693 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
694 if (f->real_file_size != -1ULL)
695 ret += f->real_file_size;
696 continue;
697 } else if (f->filetype != FIO_TYPE_FILE)
698 continue;
699
700 buf[255] = '\0';
701 strncpy(buf, f->file_name, 255);
702
703 if (stat(buf, &sb) < 0) {
704 if (errno != ENOENT)
705 break;
706 strcpy(buf, ".");
707 if (stat(buf, &sb) < 0)
708 break;
709 }
710
711 fm = NULL;
712 flist_for_each(n, &list) {
713 fm = flist_entry(n, struct fio_mount, list);
714 if (fm->key == sb.st_dev)
715 break;
716
717 fm = NULL;
718 }
719
720 if (fm)
721 continue;
722
723 fm = calloc(1, sizeof(*fm));
724 strncpy(fm->__base, buf, sizeof(fm->__base) - 1);
725 fm->base = basename(fm->__base);
726 fm->key = sb.st_dev;
727 flist_add(&fm->list, &list);
728 }
729
730 flist_for_each_safe(n, tmp, &list) {
731 unsigned long long sz;
732
733 fm = flist_entry(n, struct fio_mount, list);
734 flist_del(&fm->list);
735
736 sz = get_fs_size(fm->base);
737 if (sz && sz != -1ULL)
738 ret += sz;
739
740 free(fm);
741 }
742
743 return ret;
744}
745
746uint64_t get_start_offset(struct thread_data *td, struct fio_file *f)
747{
748 struct thread_options *o = &td->o;
749
750 if (o->file_append && f->filetype == FIO_TYPE_FILE)
751 return f->real_file_size;
752
753 return td->o.start_offset +
754 td->subjob_number * td->o.offset_increment;
755}
756
757/*
758 * Open the files and setup files sizes, creating files if necessary.
759 */
760int setup_files(struct thread_data *td)
761{
762 unsigned long long total_size, extend_size;
763 struct thread_options *o = &td->o;
764 struct fio_file *f;
765 unsigned int i, nr_fs_extra = 0;
766 int err = 0, need_extend;
767 int old_state;
768 const unsigned int bs = td_min_bs(td);
769 uint64_t fs = 0;
770
771 dprint(FD_FILE, "setup files\n");
772
773 old_state = td_bump_runstate(td, TD_SETTING_UP);
774
775 if (o->read_iolog_file)
776 goto done;
777
778 /*
779 * if ioengine defines a setup() method, it's responsible for
780 * opening the files and setting f->real_file_size to indicate
781 * the valid range for that file.
782 */
783 if (td->io_ops->setup)
784 err = td->io_ops->setup(td);
785 else
786 err = get_file_sizes(td);
787
788 if (err)
789 goto err_out;
790
791 /*
792 * check sizes. if the files/devices do not exist and the size
793 * isn't passed to fio, abort.
794 */
795 total_size = 0;
796 for_each_file(td, f, i) {
797 if (f->real_file_size == -1ULL)
798 total_size = -1ULL;
799 else
800 total_size += f->real_file_size;
801 }
802
803 if (o->fill_device)
804 td->fill_device_size = get_fs_free_counts(td);
805
806 /*
807 * device/file sizes are zero and no size given, punt
808 */
809 if ((!total_size || total_size == -1ULL) && !o->size &&
810 !(td->io_ops->flags & FIO_NOIO) && !o->fill_device &&
811 !(o->nr_files && (o->file_size_low || o->file_size_high))) {
812 log_err("%s: you need to specify size=\n", o->name);
813 td_verror(td, EINVAL, "total_file_size");
814 goto err_out;
815 }
816
817 /*
818 * Calculate per-file size and potential extra size for the
819 * first files, if needed.
820 */
821 if (!o->file_size_low && o->nr_files) {
822 uint64_t all_fs;
823
824 fs = o->size / o->nr_files;
825 all_fs = fs * o->nr_files;
826
827 if (all_fs < o->size)
828 nr_fs_extra = (o->size - all_fs) / bs;
829 }
830
831 /*
832 * now file sizes are known, so we can set ->io_size. if size= is
833 * not given, ->io_size is just equal to ->real_file_size. if size
834 * is given, ->io_size is size / nr_files.
835 */
836 extend_size = total_size = 0;
837 need_extend = 0;
838 for_each_file(td, f, i) {
839 f->file_offset = get_start_offset(td, f);
840
841 if (!o->file_size_low) {
842 /*
843 * no file size range given, file size is equal to
844 * total size divided by number of files. If that is
845 * zero, set it to the real file size. If the size
846 * doesn't divide nicely with the min blocksize,
847 * make the first files bigger.
848 */
849 f->io_size = fs;
850 if (nr_fs_extra) {
851 nr_fs_extra--;
852 f->io_size += bs;
853 }
854
855 if (!f->io_size)
856 f->io_size = f->real_file_size - f->file_offset;
857 } else if (f->real_file_size < o->file_size_low ||
858 f->real_file_size > o->file_size_high) {
859 if (f->file_offset > o->file_size_low)
860 goto err_offset;
861 /*
862 * file size given. if it's fixed, use that. if it's a
863 * range, generate a random size in-between.
864 */
865 if (o->file_size_low == o->file_size_high)
866 f->io_size = o->file_size_low - f->file_offset;
867 else {
868 f->io_size = get_rand_file_size(td)
869 - f->file_offset;
870 }
871 } else
872 f->io_size = f->real_file_size - f->file_offset;
873
874 if (f->io_size == -1ULL)
875 total_size = -1ULL;
876 else {
877 if (o->size_percent)
878 f->io_size = (f->io_size * o->size_percent) / 100;
879 total_size += f->io_size;
880 }
881
882 if (f->filetype == FIO_TYPE_FILE &&
883 (f->io_size + f->file_offset) > f->real_file_size &&
884 !(td->io_ops->flags & FIO_DISKLESSIO)) {
885 if (!o->create_on_open) {
886 need_extend++;
887 extend_size += (f->io_size + f->file_offset);
888 } else
889 f->real_file_size = f->io_size + f->file_offset;
890 fio_file_set_extend(f);
891 }
892 }
893
894 if (!o->size || (total_size && o->size > total_size))
895 o->size = total_size;
896
897 if (o->size < td_min_bs(td)) {
898 log_err("fio: blocksize too large for data set\n");
899 goto err_out;
900 }
901
902 /*
903 * See if we need to extend some files
904 */
905 if (need_extend) {
906 temp_stall_ts = 1;
907 if (output_format == FIO_OUTPUT_NORMAL)
908 log_info("%s: Laying out IO file(s) (%u file(s) /"
909 " %lluMB)\n", o->name, need_extend,
910 extend_size >> 20);
911
912 for_each_file(td, f, i) {
913 unsigned long long old_len = -1ULL, extend_len = -1ULL;
914
915 if (!fio_file_extend(f))
916 continue;
917
918 assert(f->filetype == FIO_TYPE_FILE);
919 fio_file_clear_extend(f);
920 if (!o->fill_device) {
921 old_len = f->real_file_size;
922 extend_len = f->io_size + f->file_offset -
923 old_len;
924 }
925 f->real_file_size = (f->io_size + f->file_offset);
926 err = extend_file(td, f);
927 if (err)
928 break;
929
930 err = __file_invalidate_cache(td, f, old_len,
931 extend_len);
932
933 /*
934 * Shut up static checker
935 */
936 if (f->fd != -1)
937 close(f->fd);
938
939 f->fd = -1;
940 if (err)
941 break;
942 }
943 temp_stall_ts = 0;
944 }
945
946 if (err)
947 goto err_out;
948
949 if (!o->zone_size)
950 o->zone_size = o->size;
951
952 /*
953 * iolog already set the total io size, if we read back
954 * stored entries.
955 */
956 if (!o->read_iolog_file) {
957 if (o->io_limit)
958 td->total_io_size = o->io_limit * o->loops;
959 else
960 td->total_io_size = o->size * o->loops;
961 }
962
963done:
964 if (o->create_only)
965 td->done = 1;
966
967 td_restore_runstate(td, old_state);
968 return 0;
969err_offset:
970 log_err("%s: you need to specify valid offset=\n", o->name);
971err_out:
972 td_restore_runstate(td, old_state);
973 return 1;
974}
975
976int pre_read_files(struct thread_data *td)
977{
978 struct fio_file *f;
979 unsigned int i;
980
981 dprint(FD_FILE, "pre_read files\n");
982
983 for_each_file(td, f, i) {
984 pre_read_file(td, f);
985 }
986
987 return 1;
988}
989
990static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
991{
992 unsigned int range_size, seed;
993 unsigned long nranges;
994 uint64_t file_size;
995
996 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
997 file_size = min(f->real_file_size, f->io_size);
998
999 nranges = (file_size + range_size - 1) / range_size;
1000
1001 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
1002 if (!td->o.rand_repeatable)
1003 seed = td->rand_seeds[4];
1004
1005 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
1006 zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed);
1007 else
1008 pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed);
1009
1010 return 1;
1011}
1012
1013static int init_rand_distribution(struct thread_data *td)
1014{
1015 struct fio_file *f;
1016 unsigned int i;
1017 int state;
1018
1019 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
1020 return 0;
1021
1022 state = td_bump_runstate(td, TD_SETTING_UP);
1023
1024 for_each_file(td, f, i)
1025 __init_rand_distribution(td, f);
1026
1027 td_restore_runstate(td, state);
1028
1029 return 1;
1030}
1031
1032int init_random_map(struct thread_data *td)
1033{
1034 unsigned long long blocks;
1035 struct fio_file *f;
1036 unsigned int i;
1037
1038 if (init_rand_distribution(td))
1039 return 0;
1040 if (!td_random(td))
1041 return 0;
1042
1043 for_each_file(td, f, i) {
1044 uint64_t file_size = min(f->real_file_size, f->io_size);
1045
1046 blocks = file_size / (unsigned long long) td->o.rw_min_bs;
1047
1048 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
1049 unsigned long seed;
1050
1051 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
1052
1053 if (!lfsr_init(&f->lfsr, blocks, seed, 0))
1054 continue;
1055 } else if (!td->o.norandommap) {
1056 f->io_axmap = axmap_new(blocks);
1057 if (f->io_axmap)
1058 continue;
1059 } else if (td->o.norandommap)
1060 continue;
1061
1062 if (!td->o.softrandommap) {
1063 log_err("fio: failed allocating random map. If running"
1064 " a large number of jobs, try the 'norandommap'"
1065 " option or set 'softrandommap'. Or give"
1066 " a larger --alloc-size to fio.\n");
1067 return 1;
1068 }
1069
1070 log_info("fio: file %s failed allocating random map. Running "
1071 "job without.\n", f->file_name);
1072 }
1073
1074 return 0;
1075}
1076
1077void close_files(struct thread_data *td)
1078{
1079 struct fio_file *f;
1080 unsigned int i;
1081
1082 for_each_file(td, f, i) {
1083 if (fio_file_open(f))
1084 td_io_close_file(td, f);
1085 }
1086}
1087
1088void close_and_free_files(struct thread_data *td)
1089{
1090 struct fio_file *f;
1091 unsigned int i;
1092
1093 dprint(FD_FILE, "close files\n");
1094
1095 for_each_file(td, f, i) {
1096 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1097 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1098 td_io_unlink_file(td, f);
1099 }
1100
1101 if (fio_file_open(f))
1102 td_io_close_file(td, f);
1103
1104 remove_file_hash(f);
1105
1106 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1107 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1108 td_io_unlink_file(td, f);
1109 }
1110
1111 sfree(f->file_name);
1112 f->file_name = NULL;
1113 axmap_free(f->io_axmap);
1114 f->io_axmap = NULL;
1115 sfree(f);
1116 }
1117
1118 td->o.filename = NULL;
1119 free(td->files);
1120 free(td->file_locks);
1121 td->files_index = 0;
1122 td->files = NULL;
1123 td->file_locks = NULL;
1124 td->o.file_lock_mode = FILE_LOCK_NONE;
1125 td->o.nr_files = 0;
1126}
1127
1128static void get_file_type(struct fio_file *f)
1129{
1130 struct stat sb;
1131
1132 if (!strcmp(f->file_name, "-"))
1133 f->filetype = FIO_TYPE_PIPE;
1134 else
1135 f->filetype = FIO_TYPE_FILE;
1136
1137 /* \\.\ is the device namespace in Windows, where every file is
1138 * a block device */
1139 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1140 f->filetype = FIO_TYPE_BD;
1141
1142 if (!stat(f->file_name, &sb)) {
1143 if (S_ISBLK(sb.st_mode))
1144 f->filetype = FIO_TYPE_BD;
1145 else if (S_ISCHR(sb.st_mode))
1146 f->filetype = FIO_TYPE_CHAR;
1147 else if (S_ISFIFO(sb.st_mode))
1148 f->filetype = FIO_TYPE_PIPE;
1149 }
1150}
1151
1152static int __is_already_allocated(const char *fname)
1153{
1154 struct flist_head *entry;
1155 char *filename;
1156
1157 if (flist_empty(&filename_list))
1158 return 0;
1159
1160 flist_for_each(entry, &filename_list) {
1161 filename = flist_entry(entry, struct file_name, list)->filename;
1162
1163 if (strcmp(filename, fname) == 0)
1164 return 1;
1165 }
1166
1167 return 0;
1168}
1169
1170static int is_already_allocated(const char *fname)
1171{
1172 int ret;
1173
1174 fio_file_hash_lock();
1175 ret = __is_already_allocated(fname);
1176 fio_file_hash_unlock();
1177 return ret;
1178}
1179
1180static void set_already_allocated(const char *fname)
1181{
1182 struct file_name *fn;
1183
1184 fn = malloc(sizeof(struct file_name));
1185 fn->filename = strdup(fname);
1186
1187 fio_file_hash_lock();
1188 if (!__is_already_allocated(fname)) {
1189 flist_add_tail(&fn->list, &filename_list);
1190 fn = NULL;
1191 }
1192 fio_file_hash_unlock();
1193
1194 if (fn) {
1195 free(fn->filename);
1196 free(fn);
1197 }
1198}
1199
1200
1201static void free_already_allocated(void)
1202{
1203 struct flist_head *entry, *tmp;
1204 struct file_name *fn;
1205
1206 if (flist_empty(&filename_list))
1207 return;
1208
1209 fio_file_hash_lock();
1210 flist_for_each_safe(entry, tmp, &filename_list) {
1211 fn = flist_entry(entry, struct file_name, list);
1212 free(fn->filename);
1213 flist_del(&fn->list);
1214 free(fn);
1215 }
1216
1217 fio_file_hash_unlock();
1218}
1219
1220static struct fio_file *alloc_new_file(struct thread_data *td)
1221{
1222 struct fio_file *f;
1223
1224 f = smalloc(sizeof(*f));
1225 if (!f) {
1226 log_err("fio: smalloc OOM\n");
1227 assert(0);
1228 return NULL;
1229 }
1230
1231 f->fd = -1;
1232 f->shadow_fd = -1;
1233 fio_file_reset(td, f);
1234 return f;
1235}
1236
1237int add_file(struct thread_data *td, const char *fname, int numjob, int inc)
1238{
1239 int cur_files = td->files_index;
1240 char file_name[PATH_MAX];
1241 struct fio_file *f;
1242 int len = 0;
1243
1244 dprint(FD_FILE, "add file %s\n", fname);
1245
1246 if (td->o.directory)
1247 len = set_name_idx(file_name, td->o.directory, numjob);
1248
1249 sprintf(file_name + len, "%s", fname);
1250
1251 /* clean cloned siblings using existing files */
1252 if (numjob && is_already_allocated(file_name))
1253 return 0;
1254
1255 f = alloc_new_file(td);
1256
1257 if (td->files_size <= td->files_index) {
1258 unsigned int new_size = td->o.nr_files + 1;
1259
1260 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1261
1262 td->files = realloc(td->files, new_size * sizeof(f));
1263 if (td->files == NULL) {
1264 log_err("fio: realloc OOM\n");
1265 assert(0);
1266 }
1267 if (td->o.file_lock_mode != FILE_LOCK_NONE) {
1268 td->file_locks = realloc(td->file_locks, new_size);
1269 if (!td->file_locks) {
1270 log_err("fio: realloc OOM\n");
1271 assert(0);
1272 }
1273 td->file_locks[cur_files] = FILE_LOCK_NONE;
1274 }
1275 td->files_size = new_size;
1276 }
1277 td->files[cur_files] = f;
1278 f->fileno = cur_files;
1279
1280 /*
1281 * init function, io engine may not be loaded yet
1282 */
1283 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1284 f->real_file_size = -1ULL;
1285
1286 f->file_name = smalloc_strdup(file_name);
1287 if (!f->file_name) {
1288 log_err("fio: smalloc OOM\n");
1289 assert(0);
1290 }
1291
1292 get_file_type(f);
1293
1294 switch (td->o.file_lock_mode) {
1295 case FILE_LOCK_NONE:
1296 break;
1297 case FILE_LOCK_READWRITE:
1298 f->rwlock = fio_rwlock_init();
1299 break;
1300 case FILE_LOCK_EXCLUSIVE:
1301 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
1302 break;
1303 default:
1304 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1305 assert(0);
1306 }
1307
1308 td->files_index++;
1309 if (f->filetype == FIO_TYPE_FILE)
1310 td->nr_normal_files++;
1311
1312 set_already_allocated(file_name);
1313
1314 if (inc)
1315 td->o.nr_files++;
1316
1317 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1318 cur_files);
1319
1320 return cur_files;
1321}
1322
1323int add_file_exclusive(struct thread_data *td, const char *fname)
1324{
1325 struct fio_file *f;
1326 unsigned int i;
1327
1328 for_each_file(td, f, i) {
1329 if (!strcmp(f->file_name, fname))
1330 return i;
1331 }
1332
1333 return add_file(td, fname, 0, 1);
1334}
1335
1336void get_file(struct fio_file *f)
1337{
1338 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
1339 assert(fio_file_open(f));
1340 f->references++;
1341}
1342
1343int put_file(struct thread_data *td, struct fio_file *f)
1344{
1345 int f_ret = 0, ret = 0;
1346
1347 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
1348
1349 if (!fio_file_open(f)) {
1350 assert(f->fd == -1);
1351 return 0;
1352 }
1353
1354 assert(f->references);
1355 if (--f->references)
1356 return 0;
1357
1358 if (should_fsync(td) && td->o.fsync_on_close) {
1359 f_ret = fsync(f->fd);
1360 if (f_ret < 0)
1361 f_ret = errno;
1362 }
1363
1364 if (td->io_ops->close_file)
1365 ret = td->io_ops->close_file(td, f);
1366
1367 if (!ret)
1368 ret = f_ret;
1369
1370 td->nr_open_files--;
1371 fio_file_clear_open(f);
1372 assert(f->fd == -1);
1373 return ret;
1374}
1375
1376void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
1377{
1378 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1379 return;
1380
1381 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1382 if (ddir == DDIR_READ)
1383 fio_rwlock_read(f->rwlock);
1384 else
1385 fio_rwlock_write(f->rwlock);
1386 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1387 fio_mutex_down(f->lock);
1388
1389 td->file_locks[f->fileno] = td->o.file_lock_mode;
1390}
1391
1392void unlock_file(struct thread_data *td, struct fio_file *f)
1393{
1394 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1395 return;
1396
1397 if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1398 fio_rwlock_unlock(f->rwlock);
1399 else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1400 fio_mutex_up(f->lock);
1401
1402 td->file_locks[f->fileno] = FILE_LOCK_NONE;
1403}
1404
1405void unlock_file_all(struct thread_data *td, struct fio_file *f)
1406{
1407 if (td->o.file_lock_mode == FILE_LOCK_NONE || !td->file_locks)
1408 return;
1409 if (td->file_locks[f->fileno] != FILE_LOCK_NONE)
1410 unlock_file(td, f);
1411}
1412
1413static int recurse_dir(struct thread_data *td, const char *dirname)
1414{
1415 struct dirent *dir;
1416 int ret = 0;
1417 DIR *D;
1418
1419 D = opendir(dirname);
1420 if (!D) {
1421 char buf[FIO_VERROR_SIZE];
1422
1423 snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
1424 td_verror(td, errno, buf);
1425 return 1;
1426 }
1427
1428 while ((dir = readdir(D)) != NULL) {
1429 char full_path[PATH_MAX];
1430 struct stat sb;
1431
1432 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1433 continue;
1434
1435 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
1436
1437 if (lstat(full_path, &sb) == -1) {
1438 if (errno != ENOENT) {
1439 td_verror(td, errno, "stat");
1440 ret = 1;
1441 break;
1442 }
1443 }
1444
1445 if (S_ISREG(sb.st_mode)) {
1446 add_file(td, full_path, 0, 1);
1447 continue;
1448 }
1449 if (!S_ISDIR(sb.st_mode))
1450 continue;
1451
1452 ret = recurse_dir(td, full_path);
1453 if (ret)
1454 break;
1455 }
1456
1457 closedir(D);
1458 return ret;
1459}
1460
1461int add_dir_files(struct thread_data *td, const char *path)
1462{
1463 int ret = recurse_dir(td, path);
1464
1465 if (!ret)
1466 log_info("fio: opendir added %d files\n", td->o.nr_files);
1467
1468 return ret;
1469}
1470
1471void dup_files(struct thread_data *td, struct thread_data *org)
1472{
1473 struct fio_file *f;
1474 unsigned int i;
1475
1476 dprint(FD_FILE, "dup files: %d\n", org->files_index);
1477
1478 if (!org->files)
1479 return;
1480
1481 td->files = malloc(org->files_index * sizeof(f));
1482
1483 if (td->o.file_lock_mode != FILE_LOCK_NONE)
1484 td->file_locks = malloc(org->files_index);
1485
1486 for_each_file(org, f, i) {
1487 struct fio_file *__f;
1488
1489 __f = alloc_new_file(td);
1490
1491 if (f->file_name) {
1492 __f->file_name = smalloc_strdup(f->file_name);
1493 if (!__f->file_name) {
1494 log_err("fio: smalloc OOM\n");
1495 assert(0);
1496 }
1497
1498 __f->filetype = f->filetype;
1499 }
1500
1501 if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1502 __f->lock = f->lock;
1503 else if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1504 __f->rwlock = f->rwlock;
1505
1506 td->files[i] = __f;
1507 }
1508}
1509
1510/*
1511 * Returns the index that matches the filename, or -1 if not there
1512 */
1513int get_fileno(struct thread_data *td, const char *fname)
1514{
1515 struct fio_file *f;
1516 unsigned int i;
1517
1518 for_each_file(td, f, i)
1519 if (!strcmp(f->file_name, fname))
1520 return i;
1521
1522 return -1;
1523}
1524
1525/*
1526 * For log usage, where we add/open/close files automatically
1527 */
1528void free_release_files(struct thread_data *td)
1529{
1530 close_files(td);
1531 td->o.nr_files = 0;
1532 td->o.open_files = 0;
1533 td->files_index = 0;
1534 td->nr_normal_files = 0;
1535}
1536
1537void fio_file_reset(struct thread_data *td, struct fio_file *f)
1538{
1539 f->last_pos = f->file_offset;
1540 f->last_start = -1ULL;
1541 if (f->io_axmap)
1542 axmap_reset(f->io_axmap);
1543 if (td->o.random_generator == FIO_RAND_GEN_LFSR)
1544 lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
1545}
1546
1547int fio_files_done(struct thread_data *td)
1548{
1549 struct fio_file *f;
1550 unsigned int i;
1551
1552 for_each_file(td, f, i)
1553 if (!fio_file_done(f))
1554 return 0;
1555
1556 return 1;
1557}
1558
1559/* free memory used in initialization phase only */
1560void filesetup_mem_free(void)
1561{
1562 free_already_allocated();
1563}