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