Add new writetrim rw= mode for trims preceding writes
[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->filetype == FIO_TYPE_FILE)
404 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
405 else if (f->filetype == FIO_TYPE_BD) {
406 ret = blockdev_invalidate_cache(f);
407 if (ret < 0 && errno == EACCES && geteuid()) {
408 if (!root_warn) {
409 log_err("fio: only root may flush block "
410 "devices. Cache flush bypassed!\n");
411 root_warn = 1;
412 }
413 ret = 0;
414 }
415 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
416 ret = 0;
417
418 /*
419 * Cache flushing isn't a fatal condition, and we know it will
420 * happen on some platforms where we don't have the proper
421 * function to flush eg block device caches. So just warn and
422 * continue on our way.
423 */
424 if (ret) {
425 log_info("fio: cache invalidation of %s failed: %s\n", f->file_name, strerror(errno));
426 ret = 0;
427 }
428
429 return 0;
430
431}
432
433int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
434{
435 if (!fio_file_open(f))
436 return 0;
437
438 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
439}
440
441int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
442{
443 int ret = 0;
444
445 dprint(FD_FILE, "fd close %s\n", f->file_name);
446
447 remove_file_hash(f);
448
449 if (close(f->fd) < 0)
450 ret = errno;
451
452 f->fd = -1;
453
454 if (f->shadow_fd != -1) {
455 close(f->shadow_fd);
456 f->shadow_fd = -1;
457 }
458
459 f->engine_data = 0;
460 return ret;
461}
462
463int file_lookup_open(struct fio_file *f, int flags)
464{
465 struct fio_file *__f;
466 int from_hash;
467
468 __f = lookup_file_hash(f->file_name);
469 if (__f) {
470 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
471 /*
472 * racy, need the __f->lock locked
473 */
474 f->lock = __f->lock;
475 from_hash = 1;
476 } else {
477 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
478 from_hash = 0;
479 }
480
481#ifdef WIN32
482 flags |= _O_BINARY;
483#endif
484
485 f->fd = open(f->file_name, flags, 0600);
486 return from_hash;
487}
488
489static int file_close_shadow_fds(struct thread_data *td)
490{
491 struct fio_file *f;
492 int num_closed = 0;
493 unsigned int i;
494
495 for_each_file(td, f, i) {
496 if (f->shadow_fd == -1)
497 continue;
498
499 close(f->shadow_fd);
500 f->shadow_fd = -1;
501 num_closed++;
502 }
503
504 return num_closed;
505}
506
507int generic_open_file(struct thread_data *td, struct fio_file *f)
508{
509 int is_std = 0;
510 int flags = 0;
511 int from_hash = 0;
512
513 dprint(FD_FILE, "fd open %s\n", f->file_name);
514
515 if (!strcmp(f->file_name, "-")) {
516 if (td_rw(td)) {
517 log_err("fio: can't read/write to stdin/out\n");
518 return 1;
519 }
520 is_std = 1;
521
522 /*
523 * move output logging to stderr, if we are writing to stdout
524 */
525 if (td_write(td))
526 f_out = stderr;
527 }
528
529 if (td_trim(td))
530 goto skip_flags;
531 if (td->o.odirect)
532 flags |= OS_O_DIRECT;
533 if (td->o.oatomic) {
534 if (!FIO_O_ATOMIC) {
535 td_verror(td, EINVAL, "OS does not support atomic IO");
536 return 1;
537 }
538 flags |= OS_O_DIRECT | FIO_O_ATOMIC;
539 }
540 if (td->o.sync_io)
541 flags |= O_SYNC;
542 if (td->o.create_on_open)
543 flags |= O_CREAT;
544skip_flags:
545 if (f->filetype != FIO_TYPE_FILE)
546 flags |= FIO_O_NOATIME;
547
548open_again:
549 if (td_write(td)) {
550 if (!read_only)
551 flags |= O_RDWR;
552
553 if (f->filetype == FIO_TYPE_FILE)
554 flags |= O_CREAT;
555
556 if (is_std)
557 f->fd = dup(STDOUT_FILENO);
558 else
559 from_hash = file_lookup_open(f, flags);
560 } else if (td_read(td)) {
561 if (f->filetype == FIO_TYPE_CHAR && !read_only)
562 flags |= O_RDWR;
563 else
564 flags |= O_RDONLY;
565
566 if (is_std)
567 f->fd = dup(STDIN_FILENO);
568 else
569 from_hash = file_lookup_open(f, flags);
570 } else { //td trim
571 flags |= O_RDWR;
572 from_hash = file_lookup_open(f, flags);
573 }
574
575 if (f->fd == -1) {
576 char buf[FIO_VERROR_SIZE];
577 int __e = errno;
578
579 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
580 flags &= ~FIO_O_NOATIME;
581 goto open_again;
582 }
583 if (__e == EMFILE && file_close_shadow_fds(td))
584 goto open_again;
585
586 snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
587
588 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
589 log_err("fio: looks like your file system does not " \
590 "support direct=1/buffered=0\n");
591 }
592
593 td_verror(td, __e, buf);
594 return 1;
595 }
596
597 if (!from_hash && f->fd != -1) {
598 if (add_file_hash(f)) {
599 int fio_unused ret;
600
601 /*
602 * Stash away descriptor for later close. This is to
603 * work-around a "feature" on Linux, where a close of
604 * an fd that has been opened for write will trigger
605 * udev to call blkid to check partitions, fs id, etc.
606 * That pollutes the device cache, which can slow down
607 * unbuffered accesses.
608 */
609 if (f->shadow_fd == -1)
610 f->shadow_fd = f->fd;
611 else {
612 /*
613 * OK to ignore, we haven't done anything
614 * with it
615 */
616 ret = generic_close_file(td, f);
617 }
618 goto open_again;
619 }
620 }
621
622 return 0;
623}
624
625int generic_get_file_size(struct thread_data *td, struct fio_file *f)
626{
627 return get_file_size(td, f);
628}
629
630/*
631 * open/close all files, so that ->real_file_size gets set
632 */
633static int get_file_sizes(struct thread_data *td)
634{
635 struct fio_file *f;
636 unsigned int i;
637 int err = 0;
638
639 for_each_file(td, f, i) {
640 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
641 f->file_name);
642
643 if (td_io_get_file_size(td, f)) {
644 if (td->error != ENOENT) {
645 log_err("%s\n", td->verror);
646 err = 1;
647 break;
648 }
649 clear_error(td);
650 }
651
652 if (f->real_file_size == -1ULL && td->o.size)
653 f->real_file_size = td->o.size / td->o.nr_files;
654 }
655
656 return err;
657}
658
659struct fio_mount {
660 struct flist_head list;
661 const char *base;
662 char __base[256];
663 unsigned int key;
664};
665
666/*
667 * Get free number of bytes for each file on each unique mount.
668 */
669static unsigned long long get_fs_free_counts(struct thread_data *td)
670{
671 struct flist_head *n, *tmp;
672 unsigned long long ret = 0;
673 struct fio_mount *fm;
674 FLIST_HEAD(list);
675 struct fio_file *f;
676 unsigned int i;
677
678 for_each_file(td, f, i) {
679 struct stat sb;
680 char buf[256];
681
682 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
683 if (f->real_file_size != -1ULL)
684 ret += f->real_file_size;
685 continue;
686 } else if (f->filetype != FIO_TYPE_FILE)
687 continue;
688
689 buf[255] = '\0';
690 strncpy(buf, f->file_name, 255);
691
692 if (stat(buf, &sb) < 0) {
693 if (errno != ENOENT)
694 break;
695 strcpy(buf, ".");
696 if (stat(buf, &sb) < 0)
697 break;
698 }
699
700 fm = NULL;
701 flist_for_each(n, &list) {
702 fm = flist_entry(n, struct fio_mount, list);
703 if (fm->key == sb.st_dev)
704 break;
705
706 fm = NULL;
707 }
708
709 if (fm)
710 continue;
711
712 fm = calloc(1, sizeof(*fm));
713 strncpy(fm->__base, buf, sizeof(fm->__base) - 1);
714 fm->base = basename(fm->__base);
715 fm->key = sb.st_dev;
716 flist_add(&fm->list, &list);
717 }
718
719 flist_for_each_safe(n, tmp, &list) {
720 unsigned long long sz;
721
722 fm = flist_entry(n, struct fio_mount, list);
723 flist_del(&fm->list);
724
725 sz = get_fs_size(fm->base);
726 if (sz && sz != -1ULL)
727 ret += sz;
728
729 free(fm);
730 }
731
732 return ret;
733}
734
735uint64_t get_start_offset(struct thread_data *td, struct fio_file *f)
736{
737 struct thread_options *o = &td->o;
738
739 if (o->file_append && f->filetype == FIO_TYPE_FILE)
740 return f->real_file_size;
741
742 return td->o.start_offset +
743 td->subjob_number * td->o.offset_increment;
744}
745
746/*
747 * Open the files and setup files sizes, creating files if necessary.
748 */
749int setup_files(struct thread_data *td)
750{
751 unsigned long long total_size, extend_size;
752 struct thread_options *o = &td->o;
753 struct fio_file *f;
754 unsigned int i, nr_fs_extra = 0;
755 int err = 0, need_extend;
756 int old_state;
757 const unsigned int bs = td_min_bs(td);
758 uint64_t fs = 0;
759
760 dprint(FD_FILE, "setup files\n");
761
762 old_state = td_bump_runstate(td, TD_SETTING_UP);
763
764 if (o->read_iolog_file)
765 goto done;
766
767 /*
768 * if ioengine defines a setup() method, it's responsible for
769 * opening the files and setting f->real_file_size to indicate
770 * the valid range for that file.
771 */
772 if (td->io_ops->setup)
773 err = td->io_ops->setup(td);
774 else
775 err = get_file_sizes(td);
776
777 if (err)
778 goto err_out;
779
780 /*
781 * check sizes. if the files/devices do not exist and the size
782 * isn't passed to fio, abort.
783 */
784 total_size = 0;
785 for_each_file(td, f, i) {
786 if (f->real_file_size == -1ULL)
787 total_size = -1ULL;
788 else
789 total_size += f->real_file_size;
790 }
791
792 if (o->fill_device)
793 td->fill_device_size = get_fs_free_counts(td);
794
795 /*
796 * device/file sizes are zero and no size given, punt
797 */
798 if ((!total_size || total_size == -1ULL) && !o->size &&
799 !(td->io_ops->flags & FIO_NOIO) && !o->fill_device &&
800 !(o->nr_files && (o->file_size_low || o->file_size_high))) {
801 log_err("%s: you need to specify size=\n", o->name);
802 td_verror(td, EINVAL, "total_file_size");
803 goto err_out;
804 }
805
806 /*
807 * Calculate per-file size and potential extra size for the
808 * first files, if needed.
809 */
810 if (!o->file_size_low && o->nr_files) {
811 uint64_t all_fs;
812
813 fs = o->size / o->nr_files;
814 all_fs = fs * o->nr_files;
815
816 if (all_fs < o->size)
817 nr_fs_extra = (o->size - all_fs) / bs;
818 }
819
820 /*
821 * now file sizes are known, so we can set ->io_size. if size= is
822 * not given, ->io_size is just equal to ->real_file_size. if size
823 * is given, ->io_size is size / nr_files.
824 */
825 extend_size = total_size = 0;
826 need_extend = 0;
827 for_each_file(td, f, i) {
828 f->file_offset = get_start_offset(td, f);
829
830 if (!o->file_size_low) {
831 /*
832 * no file size range given, file size is equal to
833 * total size divided by number of files. If that is
834 * zero, set it to the real file size. If the size
835 * doesn't divide nicely with the min blocksize,
836 * make the first files bigger.
837 */
838 f->io_size = fs;
839 if (nr_fs_extra) {
840 nr_fs_extra--;
841 f->io_size += bs;
842 }
843
844 if (!f->io_size)
845 f->io_size = f->real_file_size - f->file_offset;
846 } else if (f->real_file_size < o->file_size_low ||
847 f->real_file_size > o->file_size_high) {
848 if (f->file_offset > o->file_size_low)
849 goto err_offset;
850 /*
851 * file size given. if it's fixed, use that. if it's a
852 * range, generate a random size in-between.
853 */
854 if (o->file_size_low == o->file_size_high)
855 f->io_size = o->file_size_low - f->file_offset;
856 else {
857 f->io_size = get_rand_file_size(td)
858 - f->file_offset;
859 }
860 } else
861 f->io_size = f->real_file_size - f->file_offset;
862
863 if (f->io_size == -1ULL)
864 total_size = -1ULL;
865 else {
866 if (o->size_percent)
867 f->io_size = (f->io_size * o->size_percent) / 100;
868 total_size += f->io_size;
869 }
870
871 if (f->filetype == FIO_TYPE_FILE &&
872 (f->io_size + f->file_offset) > f->real_file_size &&
873 !(td->io_ops->flags & FIO_DISKLESSIO)) {
874 if (!o->create_on_open) {
875 need_extend++;
876 extend_size += (f->io_size + f->file_offset);
877 } else
878 f->real_file_size = f->io_size + f->file_offset;
879 fio_file_set_extend(f);
880 }
881 }
882
883 if (!o->size || (total_size && o->size > total_size))
884 o->size = total_size;
885
886 if (o->size < td_min_bs(td)) {
887 log_err("fio: blocksize too large for data set\n");
888 goto err_out;
889 }
890
891 /*
892 * See if we need to extend some files
893 */
894 if (need_extend) {
895 temp_stall_ts = 1;
896 if (output_format == FIO_OUTPUT_NORMAL)
897 log_info("%s: Laying out IO file(s) (%u file(s) /"
898 " %lluMB)\n", o->name, need_extend,
899 extend_size >> 20);
900
901 for_each_file(td, f, i) {
902 unsigned long long old_len = -1ULL, extend_len = -1ULL;
903
904 if (!fio_file_extend(f))
905 continue;
906
907 assert(f->filetype == FIO_TYPE_FILE);
908 fio_file_clear_extend(f);
909 if (!o->fill_device) {
910 old_len = f->real_file_size;
911 extend_len = f->io_size + f->file_offset -
912 old_len;
913 }
914 f->real_file_size = (f->io_size + f->file_offset);
915 err = extend_file(td, f);
916 if (err)
917 break;
918
919 err = __file_invalidate_cache(td, f, old_len,
920 extend_len);
921
922 /*
923 * Shut up static checker
924 */
925 if (f->fd != -1)
926 close(f->fd);
927
928 f->fd = -1;
929 if (err)
930 break;
931 }
932 temp_stall_ts = 0;
933 }
934
935 if (err)
936 goto err_out;
937
938 if (!o->zone_size)
939 o->zone_size = o->size;
940
941 /*
942 * iolog already set the total io size, if we read back
943 * stored entries.
944 */
945 if (!o->read_iolog_file) {
946 if (o->io_limit)
947 td->total_io_size = o->io_limit * o->loops;
948 else
949 td->total_io_size = o->size * o->loops;
950 }
951
952done:
953 if (o->create_only)
954 td->done = 1;
955
956 td_restore_runstate(td, old_state);
957 return 0;
958err_offset:
959 log_err("%s: you need to specify valid offset=\n", o->name);
960err_out:
961 td_restore_runstate(td, old_state);
962 return 1;
963}
964
965int pre_read_files(struct thread_data *td)
966{
967 struct fio_file *f;
968 unsigned int i;
969
970 dprint(FD_FILE, "pre_read files\n");
971
972 for_each_file(td, f, i) {
973 pre_read_file(td, f);
974 }
975
976 return 1;
977}
978
979static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
980{
981 unsigned int range_size, seed;
982 unsigned long nranges;
983 uint64_t fsize;
984
985 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
986 fsize = min(f->real_file_size, f->io_size);
987
988 nranges = (fsize + range_size - 1) / range_size;
989
990 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
991 if (!td->o.rand_repeatable)
992 seed = td->rand_seeds[4];
993
994 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
995 zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed);
996 else if (td->o.random_distribution == FIO_RAND_DIST_PARETO)
997 pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed);
998 else if (td->o.random_distribution == FIO_RAND_DIST_GAUSS)
999 gauss_init(&f->gauss, nranges, td->o.gauss_dev.u.f, seed);
1000
1001 return 1;
1002}
1003
1004static int init_rand_distribution(struct thread_data *td)
1005{
1006 struct fio_file *f;
1007 unsigned int i;
1008 int state;
1009
1010 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
1011 return 0;
1012
1013 state = td_bump_runstate(td, TD_SETTING_UP);
1014
1015 for_each_file(td, f, i)
1016 __init_rand_distribution(td, f);
1017
1018 td_restore_runstate(td, state);
1019
1020 return 1;
1021}
1022
1023int init_random_map(struct thread_data *td)
1024{
1025 unsigned long long blocks;
1026 struct fio_file *f;
1027 unsigned int i;
1028
1029 if (init_rand_distribution(td))
1030 return 0;
1031 if (!td_random(td))
1032 return 0;
1033
1034 for_each_file(td, f, i) {
1035 uint64_t fsize = min(f->real_file_size, f->io_size);
1036
1037 blocks = fsize / (unsigned long long) td->o.rw_min_bs;
1038
1039 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
1040 unsigned long seed;
1041
1042 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
1043
1044 if (!lfsr_init(&f->lfsr, blocks, seed, 0)) {
1045 fio_file_set_lfsr(f);
1046 continue;
1047 }
1048 } else if (!td->o.norandommap) {
1049 f->io_axmap = axmap_new(blocks);
1050 if (f->io_axmap) {
1051 fio_file_set_axmap(f);
1052 continue;
1053 }
1054 } else if (td->o.norandommap)
1055 continue;
1056
1057 if (!td->o.softrandommap) {
1058 log_err("fio: failed allocating random map. If running"
1059 " a large number of jobs, try the 'norandommap'"
1060 " option or set 'softrandommap'. Or give"
1061 " a larger --alloc-size to fio.\n");
1062 return 1;
1063 }
1064
1065 log_info("fio: file %s failed allocating random map. Running "
1066 "job without.\n", f->file_name);
1067 }
1068
1069 return 0;
1070}
1071
1072void close_files(struct thread_data *td)
1073{
1074 struct fio_file *f;
1075 unsigned int i;
1076
1077 for_each_file(td, f, i) {
1078 if (fio_file_open(f))
1079 td_io_close_file(td, f);
1080 }
1081}
1082
1083void close_and_free_files(struct thread_data *td)
1084{
1085 struct fio_file *f;
1086 unsigned int i;
1087
1088 dprint(FD_FILE, "close files\n");
1089
1090 for_each_file(td, f, i) {
1091 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1092 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1093 td_io_unlink_file(td, f);
1094 }
1095
1096 if (fio_file_open(f))
1097 td_io_close_file(td, f);
1098
1099 remove_file_hash(f);
1100
1101 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1102 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1103 td_io_unlink_file(td, f);
1104 }
1105
1106 sfree(f->file_name);
1107 f->file_name = NULL;
1108 if (fio_file_axmap(f)) {
1109 axmap_free(f->io_axmap);
1110 f->io_axmap = NULL;
1111 }
1112 sfree(f);
1113 }
1114
1115 td->o.filename = NULL;
1116 free(td->files);
1117 free(td->file_locks);
1118 td->files_index = 0;
1119 td->files = NULL;
1120 td->file_locks = NULL;
1121 td->o.file_lock_mode = FILE_LOCK_NONE;
1122 td->o.nr_files = 0;
1123}
1124
1125static void get_file_type(struct fio_file *f)
1126{
1127 struct stat sb;
1128
1129 if (!strcmp(f->file_name, "-"))
1130 f->filetype = FIO_TYPE_PIPE;
1131 else
1132 f->filetype = FIO_TYPE_FILE;
1133
1134 /* \\.\ is the device namespace in Windows, where every file is
1135 * a block device */
1136 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1137 f->filetype = FIO_TYPE_BD;
1138
1139 if (!stat(f->file_name, &sb)) {
1140 if (S_ISBLK(sb.st_mode))
1141 f->filetype = FIO_TYPE_BD;
1142 else if (S_ISCHR(sb.st_mode))
1143 f->filetype = FIO_TYPE_CHAR;
1144 else if (S_ISFIFO(sb.st_mode))
1145 f->filetype = FIO_TYPE_PIPE;
1146 }
1147}
1148
1149static int __is_already_allocated(const char *fname)
1150{
1151 struct flist_head *entry;
1152 char *filename;
1153
1154 if (flist_empty(&filename_list))
1155 return 0;
1156
1157 flist_for_each(entry, &filename_list) {
1158 filename = flist_entry(entry, struct file_name, list)->filename;
1159
1160 if (strcmp(filename, fname) == 0)
1161 return 1;
1162 }
1163
1164 return 0;
1165}
1166
1167static int is_already_allocated(const char *fname)
1168{
1169 int ret;
1170
1171 fio_file_hash_lock();
1172 ret = __is_already_allocated(fname);
1173 fio_file_hash_unlock();
1174 return ret;
1175}
1176
1177static void set_already_allocated(const char *fname)
1178{
1179 struct file_name *fn;
1180
1181 fn = malloc(sizeof(struct file_name));
1182 fn->filename = strdup(fname);
1183
1184 fio_file_hash_lock();
1185 if (!__is_already_allocated(fname)) {
1186 flist_add_tail(&fn->list, &filename_list);
1187 fn = NULL;
1188 }
1189 fio_file_hash_unlock();
1190
1191 if (fn) {
1192 free(fn->filename);
1193 free(fn);
1194 }
1195}
1196
1197
1198static void free_already_allocated(void)
1199{
1200 struct flist_head *entry, *tmp;
1201 struct file_name *fn;
1202
1203 if (flist_empty(&filename_list))
1204 return;
1205
1206 fio_file_hash_lock();
1207 flist_for_each_safe(entry, tmp, &filename_list) {
1208 fn = flist_entry(entry, struct file_name, list);
1209 free(fn->filename);
1210 flist_del(&fn->list);
1211 free(fn);
1212 }
1213
1214 fio_file_hash_unlock();
1215}
1216
1217static struct fio_file *alloc_new_file(struct thread_data *td)
1218{
1219 struct fio_file *f;
1220
1221 f = smalloc(sizeof(*f));
1222 if (!f) {
1223 log_err("fio: smalloc OOM\n");
1224 assert(0);
1225 return NULL;
1226 }
1227
1228 f->fd = -1;
1229 f->shadow_fd = -1;
1230 fio_file_reset(td, f);
1231 return f;
1232}
1233
1234int add_file(struct thread_data *td, const char *fname, int numjob, int inc)
1235{
1236 int cur_files = td->files_index;
1237 char file_name[PATH_MAX];
1238 struct fio_file *f;
1239 int len = 0;
1240
1241 dprint(FD_FILE, "add file %s\n", fname);
1242
1243 if (td->o.directory)
1244 len = set_name_idx(file_name, td->o.directory, numjob);
1245
1246 sprintf(file_name + len, "%s", fname);
1247
1248 /* clean cloned siblings using existing files */
1249 if (numjob && is_already_allocated(file_name))
1250 return 0;
1251
1252 f = alloc_new_file(td);
1253
1254 if (td->files_size <= td->files_index) {
1255 unsigned int new_size = td->o.nr_files + 1;
1256
1257 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1258
1259 td->files = realloc(td->files, new_size * sizeof(f));
1260 if (td->files == NULL) {
1261 log_err("fio: realloc OOM\n");
1262 assert(0);
1263 }
1264 if (td->o.file_lock_mode != FILE_LOCK_NONE) {
1265 td->file_locks = realloc(td->file_locks, new_size);
1266 if (!td->file_locks) {
1267 log_err("fio: realloc OOM\n");
1268 assert(0);
1269 }
1270 td->file_locks[cur_files] = FILE_LOCK_NONE;
1271 }
1272 td->files_size = new_size;
1273 }
1274 td->files[cur_files] = f;
1275 f->fileno = cur_files;
1276
1277 /*
1278 * init function, io engine may not be loaded yet
1279 */
1280 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1281 f->real_file_size = -1ULL;
1282
1283 f->file_name = smalloc_strdup(file_name);
1284 if (!f->file_name) {
1285 log_err("fio: smalloc OOM\n");
1286 assert(0);
1287 }
1288
1289 get_file_type(f);
1290
1291 switch (td->o.file_lock_mode) {
1292 case FILE_LOCK_NONE:
1293 break;
1294 case FILE_LOCK_READWRITE:
1295 f->rwlock = fio_rwlock_init();
1296 break;
1297 case FILE_LOCK_EXCLUSIVE:
1298 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
1299 break;
1300 default:
1301 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1302 assert(0);
1303 }
1304
1305 td->files_index++;
1306 if (f->filetype == FIO_TYPE_FILE)
1307 td->nr_normal_files++;
1308
1309 set_already_allocated(file_name);
1310
1311 if (inc)
1312 td->o.nr_files++;
1313
1314 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1315 cur_files);
1316
1317 return cur_files;
1318}
1319
1320int add_file_exclusive(struct thread_data *td, const char *fname)
1321{
1322 struct fio_file *f;
1323 unsigned int i;
1324
1325 for_each_file(td, f, i) {
1326 if (!strcmp(f->file_name, fname))
1327 return i;
1328 }
1329
1330 return add_file(td, fname, 0, 1);
1331}
1332
1333void get_file(struct fio_file *f)
1334{
1335 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
1336 assert(fio_file_open(f));
1337 f->references++;
1338}
1339
1340int put_file(struct thread_data *td, struct fio_file *f)
1341{
1342 int f_ret = 0, ret = 0;
1343
1344 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
1345
1346 if (!fio_file_open(f)) {
1347 assert(f->fd == -1);
1348 return 0;
1349 }
1350
1351 assert(f->references);
1352 if (--f->references)
1353 return 0;
1354
1355 if (should_fsync(td) && td->o.fsync_on_close) {
1356 f_ret = fsync(f->fd);
1357 if (f_ret < 0)
1358 f_ret = errno;
1359 }
1360
1361 if (td->io_ops->close_file)
1362 ret = td->io_ops->close_file(td, f);
1363
1364 if (!ret)
1365 ret = f_ret;
1366
1367 td->nr_open_files--;
1368 fio_file_clear_open(f);
1369 assert(f->fd == -1);
1370 return ret;
1371}
1372
1373void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
1374{
1375 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1376 return;
1377
1378 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1379 if (ddir == DDIR_READ)
1380 fio_rwlock_read(f->rwlock);
1381 else
1382 fio_rwlock_write(f->rwlock);
1383 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1384 fio_mutex_down(f->lock);
1385
1386 td->file_locks[f->fileno] = td->o.file_lock_mode;
1387}
1388
1389void unlock_file(struct thread_data *td, struct fio_file *f)
1390{
1391 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1392 return;
1393
1394 if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1395 fio_rwlock_unlock(f->rwlock);
1396 else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1397 fio_mutex_up(f->lock);
1398
1399 td->file_locks[f->fileno] = FILE_LOCK_NONE;
1400}
1401
1402void unlock_file_all(struct thread_data *td, struct fio_file *f)
1403{
1404 if (td->o.file_lock_mode == FILE_LOCK_NONE || !td->file_locks)
1405 return;
1406 if (td->file_locks[f->fileno] != FILE_LOCK_NONE)
1407 unlock_file(td, f);
1408}
1409
1410static int recurse_dir(struct thread_data *td, const char *dirname)
1411{
1412 struct dirent *dir;
1413 int ret = 0;
1414 DIR *D;
1415
1416 D = opendir(dirname);
1417 if (!D) {
1418 char buf[FIO_VERROR_SIZE];
1419
1420 snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
1421 td_verror(td, errno, buf);
1422 return 1;
1423 }
1424
1425 while ((dir = readdir(D)) != NULL) {
1426 char full_path[PATH_MAX];
1427 struct stat sb;
1428
1429 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1430 continue;
1431
1432 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
1433
1434 if (lstat(full_path, &sb) == -1) {
1435 if (errno != ENOENT) {
1436 td_verror(td, errno, "stat");
1437 ret = 1;
1438 break;
1439 }
1440 }
1441
1442 if (S_ISREG(sb.st_mode)) {
1443 add_file(td, full_path, 0, 1);
1444 continue;
1445 }
1446 if (!S_ISDIR(sb.st_mode))
1447 continue;
1448
1449 ret = recurse_dir(td, full_path);
1450 if (ret)
1451 break;
1452 }
1453
1454 closedir(D);
1455 return ret;
1456}
1457
1458int add_dir_files(struct thread_data *td, const char *path)
1459{
1460 int ret = recurse_dir(td, path);
1461
1462 if (!ret)
1463 log_info("fio: opendir added %d files\n", td->o.nr_files);
1464
1465 return ret;
1466}
1467
1468void dup_files(struct thread_data *td, struct thread_data *org)
1469{
1470 struct fio_file *f;
1471 unsigned int i;
1472
1473 dprint(FD_FILE, "dup files: %d\n", org->files_index);
1474
1475 if (!org->files)
1476 return;
1477
1478 td->files = malloc(org->files_index * sizeof(f));
1479
1480 if (td->o.file_lock_mode != FILE_LOCK_NONE)
1481 td->file_locks = malloc(org->files_index);
1482
1483 for_each_file(org, f, i) {
1484 struct fio_file *__f;
1485
1486 __f = alloc_new_file(td);
1487
1488 if (f->file_name) {
1489 __f->file_name = smalloc_strdup(f->file_name);
1490 if (!__f->file_name) {
1491 log_err("fio: smalloc OOM\n");
1492 assert(0);
1493 }
1494
1495 __f->filetype = f->filetype;
1496 }
1497
1498 if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1499 __f->lock = f->lock;
1500 else if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1501 __f->rwlock = f->rwlock;
1502
1503 td->files[i] = __f;
1504 }
1505}
1506
1507/*
1508 * Returns the index that matches the filename, or -1 if not there
1509 */
1510int get_fileno(struct thread_data *td, const char *fname)
1511{
1512 struct fio_file *f;
1513 unsigned int i;
1514
1515 for_each_file(td, f, i)
1516 if (!strcmp(f->file_name, fname))
1517 return i;
1518
1519 return -1;
1520}
1521
1522/*
1523 * For log usage, where we add/open/close files automatically
1524 */
1525void free_release_files(struct thread_data *td)
1526{
1527 close_files(td);
1528 td->o.nr_files = 0;
1529 td->o.open_files = 0;
1530 td->files_index = 0;
1531 td->nr_normal_files = 0;
1532}
1533
1534void fio_file_reset(struct thread_data *td, struct fio_file *f)
1535{
1536 int i;
1537
1538 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1539 f->last_pos[i] = f->file_offset;
1540 f->last_start[i] = -1ULL;
1541 }
1542
1543 if (fio_file_axmap(f))
1544 axmap_reset(f->io_axmap);
1545 else if (fio_file_lfsr(f))
1546 lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
1547}
1548
1549int fio_files_done(struct thread_data *td)
1550{
1551 struct fio_file *f;
1552 unsigned int i;
1553
1554 for_each_file(td, f, i)
1555 if (!fio_file_done(f))
1556 return 0;
1557
1558 return 1;
1559}
1560
1561/* free memory used in initialization phase only */
1562void filesetup_mem_free(void)
1563{
1564 free_already_allocated();
1565}