fio Makefile improvement - don't override $(CC)
[fio.git] / filesetup.c
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 "os/os.h"
15
16 #ifdef FIO_HAVE_LINUX_FALLOCATE
17 #include <linux/falloc.h>
18 #endif
19
20 static int root_warn;
21
22 static inline void clear_error(struct thread_data *td)
23 {
24         td->error = 0;
25         td->verror[0] = '\0';
26 }
27
28 /*
29  * Leaves f->fd open on success, caller must close
30  */
31 static int extend_file(struct thread_data *td, struct fio_file *f)
32 {
33         int r, new_layout = 0, unlink_file = 0, flags;
34         unsigned long long left;
35         unsigned int bs;
36         char *b;
37
38         if (read_only) {
39                 log_err("fio: refusing extend of file due to read-only\n");
40                 return 0;
41         }
42
43         /*
44          * check if we need to lay the file out complete again. fio
45          * does that for operations involving reads, or for writes
46          * where overwrite is set
47          */
48         if (td_read(td) || (td_write(td) && td->o.overwrite) ||
49             (td_write(td) && td->io_ops->flags & FIO_NOEXTEND))
50                 new_layout = 1;
51         if (td_write(td) && !td->o.overwrite)
52                 unlink_file = 1;
53
54         if (unlink_file || new_layout) {
55                 dprint(FD_FILE, "layout unlink %s\n", f->file_name);
56                 if ((unlink(f->file_name) < 0) && (errno != ENOENT)) {
57                         td_verror(td, errno, "unlink");
58                         return 1;
59                 }
60         }
61
62         flags = O_WRONLY | O_CREAT;
63         if (new_layout)
64                 flags |= O_TRUNC;
65
66         dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
67         f->fd = open(f->file_name, flags, 0644);
68         if (f->fd < 0) {
69                 td_verror(td, errno, "open");
70                 return 1;
71         }
72
73 #ifdef FIO_HAVE_FALLOCATE
74         if (!td->o.fill_device) {
75                 switch (td->o.fallocate_mode) {
76                 case FIO_FALLOCATE_NONE:
77                         break;
78                 case FIO_FALLOCATE_POSIX:
79                         dprint(FD_FILE, "posix_fallocate file %s size %llu\n",
80                                  f->file_name, f->real_file_size);
81
82                         r = posix_fallocate(f->fd, 0, f->real_file_size);
83                         if (r > 0) {
84                                 log_err("fio: posix_fallocate fails: %s\n",
85                                                 strerror(r));
86                         }
87                         break;
88 #ifdef FIO_HAVE_LINUX_FALLOCATE
89                 case FIO_FALLOCATE_KEEP_SIZE:
90                         dprint(FD_FILE,
91                                 "fallocate(FALLOC_FL_KEEP_SIZE) "
92                                 "file %s size %llu\n",
93                                 f->file_name, f->real_file_size);
94
95                         r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0,
96                                         f->real_file_size);
97                         if (r != 0) {
98                                 td_verror(td, errno, "fallocate");
99                         }
100                         break;
101 #endif /* FIO_HAVE_LINUX_FALLOCATE */
102                 default:
103                         log_err("fio: unknown fallocate mode: %d\n",
104                                 td->o.fallocate_mode);
105                         assert(0);
106                 }
107         }
108 #endif /* FIO_HAVE_FALLOCATE */
109
110         if (!new_layout)
111                 goto done;
112
113         /*
114          * The size will be -1ULL when fill_device is used, so don't truncate
115          * or fallocate this file, just write it
116          */
117         if (!td->o.fill_device) {
118                 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
119                                                         f->real_file_size);
120                 if (ftruncate(f->fd, f->real_file_size) == -1) {
121                         td_verror(td, errno, "ftruncate");
122                         goto err;
123                 }
124         }
125
126         b = malloc(td->o.max_bs[DDIR_WRITE]);
127         memset(b, 0, td->o.max_bs[DDIR_WRITE]);
128
129         left = f->real_file_size;
130         while (left && !td->terminate) {
131                 bs = td->o.max_bs[DDIR_WRITE];
132                 if (bs > left)
133                         bs = left;
134
135                 r = write(f->fd, b, bs);
136
137                 if (r > 0) {
138                         left -= r;
139                         continue;
140                 } else {
141                         if (r < 0) {
142                                 int __e = errno;
143
144                                 if (__e == ENOSPC) {
145                                         if (td->o.fill_device)
146                                                 break;
147                                         log_info("fio: ENOSPC on laying out "
148                                                  "file, stopping\n");
149                                         break;
150                                 }
151                                 td_verror(td, errno, "write");
152                         } else
153                                 td_verror(td, EIO, "write");
154
155                         break;
156                 }
157         }
158
159         if (td->terminate) {
160                 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
161                 unlink(f->file_name);
162         } else if (td->o.create_fsync) {
163                 if (fsync(f->fd) < 0) {
164                         td_verror(td, errno, "fsync");
165                         goto err;
166                 }
167         }
168         if (td->o.fill_device && !td_write(td)) {
169                 fio_file_clear_size_known(f);
170                 if (td_io_get_file_size(td, f))
171                         goto err;
172                 if (f->io_size > f->real_file_size)
173                         f->io_size = f->real_file_size;
174         }
175
176         free(b);
177 done:
178         return 0;
179 err:
180         close(f->fd);
181         f->fd = -1;
182         return 1;
183 }
184
185 static int pre_read_file(struct thread_data *td, struct fio_file *f)
186 {
187         int r, did_open = 0, old_runstate;
188         unsigned long long left;
189         unsigned int bs;
190         char *b;
191
192         if (td->io_ops->flags & FIO_PIPEIO)
193                 return 0;
194
195         if (!fio_file_open(f)) {
196                 if (td->io_ops->open_file(td, f)) {
197                         log_err("fio: cannot pre-read, failed to open file\n");
198                         return 1;
199                 }
200                 did_open = 1;
201         }
202
203         old_runstate = td->runstate;
204         td_set_runstate(td, TD_PRE_READING);
205
206         bs = td->o.max_bs[DDIR_READ];
207         b = malloc(bs);
208         memset(b, 0, bs);
209
210         lseek(f->fd, f->file_offset, SEEK_SET);
211         left = f->io_size;
212
213         while (left && !td->terminate) {
214                 if (bs > left)
215                         bs = left;
216
217                 r = read(f->fd, b, bs);
218
219                 if (r == (int) bs) {
220                         left -= bs;
221                         continue;
222                 } else {
223                         td_verror(td, EIO, "pre_read");
224                         break;
225                 }
226         }
227
228         td_set_runstate(td, old_runstate);
229
230         if (did_open)
231                 td->io_ops->close_file(td, f);
232         free(b);
233         return 0;
234 }
235
236 static unsigned long long get_rand_file_size(struct thread_data *td)
237 {
238         unsigned long long ret, sized;
239         unsigned long r;
240
241         if (td->o.use_os_rand) {
242                 r = os_random_long(&td->file_size_state);
243                 sized = td->o.file_size_high - td->o.file_size_low;
244                 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
245         } else {
246                 r = __rand(&td->__file_size_state);
247                 sized = td->o.file_size_high - td->o.file_size_low;
248                 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0)));
249         }
250
251         ret += td->o.file_size_low;
252         ret -= (ret % td->o.rw_min_bs);
253         return ret;
254 }
255
256 static int file_size(struct thread_data *td, struct fio_file *f)
257 {
258         struct stat st;
259
260         if (stat(f->file_name, &st) == -1) {
261                 td_verror(td, errno, "fstat");
262                 return 1;
263         }
264
265         f->real_file_size = st.st_size;
266         return 0;
267 }
268
269 static int bdev_size(struct thread_data *td, struct fio_file *f)
270 {
271         unsigned long long bytes = 0;
272         int r;
273
274         if (td->io_ops->open_file(td, f)) {
275                 log_err("fio: failed opening blockdev %s for size check\n",
276                         f->file_name);
277                 return 1;
278         }
279
280         r = blockdev_size(f, &bytes);
281         if (r) {
282                 td_verror(td, r, "blockdev_size");
283                 goto err;
284         }
285
286         if (!bytes) {
287                 log_err("%s: zero sized block device?\n", f->file_name);
288                 goto err;
289         }
290
291         f->real_file_size = bytes;
292         td->io_ops->close_file(td, f);
293         return 0;
294 err:
295         td->io_ops->close_file(td, f);
296         return 1;
297 }
298
299 static int char_size(struct thread_data *td, struct fio_file *f)
300 {
301 #ifdef FIO_HAVE_CHARDEV_SIZE
302         unsigned long long bytes = 0;
303         int r;
304
305         if (td->io_ops->open_file(td, f)) {
306                 log_err("fio: failed opening blockdev %s for size check\n",
307                         f->file_name);
308                 return 1;
309         }
310
311         r = chardev_size(f, &bytes);
312         if (r) {
313                 td_verror(td, r, "chardev_size");
314                 goto err;
315         }
316
317         if (!bytes) {
318                 log_err("%s: zero sized char device?\n", f->file_name);
319                 goto err;
320         }
321
322         f->real_file_size = bytes;
323         td->io_ops->close_file(td, f);
324         return 0;
325 err:
326         td->io_ops->close_file(td, f);
327         return 1;
328 #else
329         f->real_file_size = -1ULL;
330         return 0;
331 #endif
332 }
333
334 static int get_file_size(struct thread_data *td, struct fio_file *f)
335 {
336         int ret = 0;
337
338         if (fio_file_size_known(f))
339                 return 0;
340
341         if (f->filetype == FIO_TYPE_FILE)
342                 ret = file_size(td, f);
343         else if (f->filetype == FIO_TYPE_BD)
344                 ret = bdev_size(td, f);
345         else if (f->filetype == FIO_TYPE_CHAR)
346                 ret = char_size(td, f);
347         else
348                 f->real_file_size = -1;
349
350         if (ret)
351                 return ret;
352
353         if (f->file_offset > f->real_file_size) {
354                 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
355                                         f->file_offset, f->real_file_size);
356                 return 1;
357         }
358
359         fio_file_set_size_known(f);
360         return 0;
361 }
362
363 static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
364                                    unsigned long long off,
365                                    unsigned long long len)
366 {
367         int ret = 0;
368
369         if (len == -1ULL)
370                 len = f->io_size;
371         if (off == -1ULL)
372                 off = f->file_offset;
373
374         if (len == -1ULL || off == -1ULL)
375                 return 0;
376
377         dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
378                                                                 len);
379
380         /*
381          * FIXME: add blockdev flushing too
382          */
383         if (f->mmap_ptr) {
384                 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
385 #ifdef FIO_MADV_FREE
386                 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
387 #endif
388         } else if (f->filetype == FIO_TYPE_FILE) {
389                 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
390         } else if (f->filetype == FIO_TYPE_BD) {
391                 ret = blockdev_invalidate_cache(f);
392                 if (ret < 0 && errno == EACCES && geteuid()) {
393                         if (!root_warn) {
394                                 log_err("fio: only root may flush block "
395                                         "devices. Cache flush bypassed!\n");
396                                 root_warn = 1;
397                         }
398                         ret = 0;
399                 }
400         } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
401                 ret = 0;
402
403         if (ret < 0) {
404                 td_verror(td, errno, "invalidate_cache");
405                 return 1;
406         } else if (ret > 0) {
407                 td_verror(td, ret, "invalidate_cache");
408                 return 1;
409         }
410
411         return ret;
412
413 }
414
415 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
416 {
417         if (!fio_file_open(f))
418                 return 0;
419
420         return __file_invalidate_cache(td, f, -1ULL, -1ULL);
421 }
422
423 int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
424 {
425         int ret = 0;
426
427         dprint(FD_FILE, "fd close %s\n", f->file_name);
428
429         remove_file_hash(f);
430
431         if (close(f->fd) < 0)
432                 ret = errno;
433
434         f->fd = -1;
435         return ret;
436 }
437
438 static int file_lookup_open(struct fio_file *f, int flags)
439 {
440         struct fio_file *__f;
441         int from_hash;
442
443         __f = lookup_file_hash(f->file_name);
444         if (__f) {
445                 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
446                 /*
447                  * racy, need the __f->lock locked
448                  */
449                 f->lock = __f->lock;
450                 f->lock_owner = __f->lock_owner;
451                 f->lock_batch = __f->lock_batch;
452                 f->lock_ddir = __f->lock_ddir;
453                 from_hash = 1;
454         } else {
455                 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
456                 from_hash = 0;
457         }
458
459         f->fd = open(f->file_name, flags, 0600);
460         return from_hash;
461 }
462
463 int generic_open_file(struct thread_data *td, struct fio_file *f)
464 {
465         int is_std = 0;
466         int flags = 0;
467         int from_hash = 0;
468
469         dprint(FD_FILE, "fd open %s\n", f->file_name);
470
471         if (!strcmp(f->file_name, "-")) {
472                 if (td_rw(td)) {
473                         log_err("fio: can't read/write to stdin/out\n");
474                         return 1;
475                 }
476                 is_std = 1;
477
478                 /*
479                  * move output logging to stderr, if we are writing to stdout
480                  */
481                 if (td_write(td))
482                         f_out = stderr;
483         }
484
485         if (td->o.odirect)
486                 flags |= OS_O_DIRECT;
487         if (td->o.sync_io)
488                 flags |= O_SYNC;
489         if (f->filetype != FIO_TYPE_FILE)
490                 flags |= FIO_O_NOATIME;
491         if (td->o.create_on_open)
492                 flags |= O_CREAT;
493
494 open_again:
495         if (td_write(td)) {
496                 if (!read_only)
497                         flags |= O_RDWR;
498
499                 if (f->filetype == FIO_TYPE_FILE)
500                         flags |= O_CREAT;
501
502                 if (is_std)
503                         f->fd = dup(STDOUT_FILENO);
504                 else
505                         from_hash = file_lookup_open(f, flags);
506         } else {
507                 if (f->filetype == FIO_TYPE_CHAR && !read_only)
508                         flags |= O_RDWR;
509                 else
510                         flags |= O_RDONLY;
511
512                 if (is_std)
513                         f->fd = dup(STDIN_FILENO);
514                 else
515                         from_hash = file_lookup_open(f, flags);
516         }
517
518         if (f->fd == -1) {
519                 char buf[FIO_VERROR_SIZE];
520                 int __e = errno;
521
522                 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
523                         flags &= ~FIO_O_NOATIME;
524                         goto open_again;
525                 }
526
527                 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
528
529                 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
530                         log_err("fio: looks like your file system does not " \
531                                 "support direct=1/buffered=0\n");
532                 }
533
534                 td_verror(td, __e, buf);
535         }
536
537         if (!from_hash && f->fd != -1) {
538                 if (add_file_hash(f)) {
539                         int fio_unused ret;
540
541                         /*
542                          * OK to ignore, we haven't done anything with it
543                          */
544                         ret = generic_close_file(td, f);
545                         goto open_again;
546                 }
547         }
548
549         return 0;
550 }
551
552 int generic_get_file_size(struct thread_data *td, struct fio_file *f)
553 {
554         return get_file_size(td, f);
555 }
556
557 /*
558  * open/close all files, so that ->real_file_size gets set
559  */
560 static int get_file_sizes(struct thread_data *td)
561 {
562         struct fio_file *f;
563         unsigned int i;
564         int err = 0;
565
566         for_each_file(td, f, i) {
567                 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
568                                                                 f->file_name);
569
570                 if (td_io_get_file_size(td, f)) {
571                         if (td->error != ENOENT) {
572                                 log_err("%s\n", td->verror);
573                                 err = 1;
574                         }
575                         clear_error(td);
576                 }
577
578                 if (f->real_file_size == -1ULL && td->o.size)
579                         f->real_file_size = td->o.size / td->o.nr_files;
580         }
581
582         return err;
583 }
584
585 struct fio_mount {
586         struct flist_head list;
587         const char *base;
588         char __base[256];
589         unsigned int key;
590 };
591
592 /*
593  * Get free number of bytes for each file on each unique mount.
594  */
595 static unsigned long long get_fs_free_counts(struct thread_data *td)
596 {
597         struct flist_head *n, *tmp;
598         unsigned long long ret = 0;
599         struct fio_mount *fm;
600         FLIST_HEAD(list);
601         struct fio_file *f;
602         unsigned int i;
603
604         for_each_file(td, f, i) {
605                 struct stat sb;
606                 char buf[256];
607
608                 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
609                         if (f->real_file_size != -1ULL)
610                                 ret += f->real_file_size;
611                         continue;
612                 } else if (f->filetype != FIO_TYPE_FILE)
613                         continue;
614
615                 strcpy(buf, f->file_name);
616
617                 if (stat(buf, &sb) < 0) {
618                         if (errno != ENOENT)
619                                 break;
620                         strcpy(buf, ".");
621                         if (stat(buf, &sb) < 0)
622                                 break;
623                 }
624
625                 fm = NULL;
626                 flist_for_each(n, &list) {
627                         fm = flist_entry(n, struct fio_mount, list);
628                         if (fm->key == sb.st_dev)
629                                 break;
630
631                         fm = NULL;
632                 }
633
634                 if (fm)
635                         continue;
636
637                 fm = malloc(sizeof(*fm));
638                 strcpy(fm->__base, buf);
639                 fm->base = basename(fm->__base);
640                 fm->key = sb.st_dev;
641                 flist_add(&fm->list, &list);
642         }
643
644         flist_for_each_safe(n, tmp, &list) {
645                 unsigned long long sz;
646
647                 fm = flist_entry(n, struct fio_mount, list);
648                 flist_del(&fm->list);
649
650                 sz = get_fs_size(fm->base);
651                 if (sz && sz != -1ULL)
652                         ret += sz;
653
654                 free(fm);
655         }
656
657         return ret;
658 }
659
660 /*
661  * Open the files and setup files sizes, creating files if necessary.
662  */
663 int setup_files(struct thread_data *td)
664 {
665         unsigned long long total_size, extend_size;
666         struct fio_file *f;
667         unsigned int i;
668         int err = 0, need_extend;
669
670         dprint(FD_FILE, "setup files\n");
671
672         if (td->o.read_iolog_file)
673                 goto done;
674
675         /*
676          * if ioengine defines a setup() method, it's responsible for
677          * opening the files and setting f->real_file_size to indicate
678          * the valid range for that file.
679          */
680         if (td->io_ops->setup)
681                 err = td->io_ops->setup(td);
682         else
683                 err = get_file_sizes(td);
684
685         if (err)
686                 return err;
687
688         /*
689          * check sizes. if the files/devices do not exist and the size
690          * isn't passed to fio, abort.
691          */
692         total_size = 0;
693         for_each_file(td, f, i) {
694                 if (f->real_file_size == -1ULL)
695                         total_size = -1ULL;
696                 else
697                         total_size += f->real_file_size;
698         }
699
700         if (td->o.fill_device)
701                 td->fill_device_size = get_fs_free_counts(td);
702
703         /*
704          * device/file sizes are zero and no size given, punt
705          */
706         if ((!total_size || total_size == -1ULL) && !td->o.size &&
707             !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
708                 log_err("%s: you need to specify size=\n", td->o.name);
709                 td_verror(td, EINVAL, "total_file_size");
710                 return 1;
711         }
712
713         /*
714          * now file sizes are known, so we can set ->io_size. if size= is
715          * not given, ->io_size is just equal to ->real_file_size. if size
716          * is given, ->io_size is size / nr_files.
717          */
718         extend_size = total_size = 0;
719         need_extend = 0;
720         for_each_file(td, f, i) {
721                 f->file_offset = td->o.start_offset +
722                         (td->thread_number - 1) * td->o.offset_increment;
723
724                 if (!td->o.file_size_low) {
725                         /*
726                          * no file size range given, file size is equal to
727                          * total size divided by number of files. if that is
728                          * zero, set it to the real file size.
729                          */
730                         f->io_size = td->o.size / td->o.nr_files;
731                         if (!f->io_size)
732                                 f->io_size = f->real_file_size - f->file_offset;
733                 } else if (f->real_file_size < td->o.file_size_low ||
734                            f->real_file_size > td->o.file_size_high) {
735                         if (f->file_offset > td->o.file_size_low)
736                                 goto err_offset;
737                         /*
738                          * file size given. if it's fixed, use that. if it's a
739                          * range, generate a random size in-between.
740                          */
741                         if (td->o.file_size_low == td->o.file_size_high) {
742                                 f->io_size = td->o.file_size_low
743                                                 - f->file_offset;
744                         } else {
745                                 f->io_size = get_rand_file_size(td)
746                                                 - f->file_offset;
747                         }
748                 } else
749                         f->io_size = f->real_file_size - f->file_offset;
750
751                 if (f->io_size == -1ULL)
752                         total_size = -1ULL;
753                 else
754                         total_size += f->io_size;
755
756                 if (f->filetype == FIO_TYPE_FILE &&
757                     (f->io_size + f->file_offset) > f->real_file_size &&
758                     !(td->io_ops->flags & FIO_DISKLESSIO)) {
759                         if (!td->o.create_on_open) {
760                                 need_extend++;
761                                 extend_size += (f->io_size + f->file_offset);
762                         } else
763                                 f->real_file_size = f->io_size + f->file_offset;
764                         fio_file_set_extend(f);
765                 }
766         }
767
768         if (td->o.size_percent)
769                 total_size = (total_size * td->o.size_percent) / 100;
770
771         if (!td->o.size || td->o.size > total_size)
772                 td->o.size = total_size;
773
774         /*
775          * See if we need to extend some files
776          */
777         if (need_extend) {
778                 temp_stall_ts = 1;
779                 if (!terse_output)
780                         log_info("%s: Laying out IO file(s) (%u file(s) /"
781                                  " %lluMB)\n", td->o.name, need_extend,
782                                         extend_size >> 20);
783
784                 for_each_file(td, f, i) {
785                         unsigned long long old_len = -1ULL, extend_len = -1ULL;
786
787                         if (!fio_file_extend(f))
788                                 continue;
789
790                         assert(f->filetype == FIO_TYPE_FILE);
791                         fio_file_clear_extend(f);
792                         if (!td->o.fill_device) {
793                                 old_len = f->real_file_size;
794                                 extend_len = f->io_size + f->file_offset -
795                                                 old_len;
796                         }
797                         f->real_file_size = (f->io_size + f->file_offset);
798                         err = extend_file(td, f);
799                         if (err)
800                                 break;
801
802                         err = __file_invalidate_cache(td, f, old_len,
803                                                                 extend_len);
804                         close(f->fd);
805                         f->fd = -1;
806                         if (err)
807                                 break;
808                 }
809                 temp_stall_ts = 0;
810         }
811
812         if (err)
813                 return err;
814
815         if (!td->o.zone_size)
816                 td->o.zone_size = td->o.size;
817
818         /*
819          * iolog already set the total io size, if we read back
820          * stored entries.
821          */
822         if (!td->o.read_iolog_file)
823                 td->total_io_size = td->o.size * td->o.loops;
824
825 done:
826         if (td->o.create_only)
827                 td->done = 1;
828
829         return 0;
830 err_offset:
831         log_err("%s: you need to specify valid offset=\n", td->o.name);
832         return 1;
833 }
834
835 int pre_read_files(struct thread_data *td)
836 {
837         struct fio_file *f;
838         unsigned int i;
839
840         dprint(FD_FILE, "pre_read files\n");
841
842         for_each_file(td, f, i) {
843                 pre_read_file(td, f);
844         }
845
846         return 1;
847 }
848
849 int init_random_map(struct thread_data *td)
850 {
851         unsigned long long blocks, num_maps;
852         struct fio_file *f;
853         unsigned int i;
854
855         if (td->o.norandommap || !td_random(td))
856                 return 0;
857
858         for_each_file(td, f, i) {
859                 blocks = (f->real_file_size + td->o.rw_min_bs - 1) /
860                                 (unsigned long long) td->o.rw_min_bs;
861                 num_maps = (blocks + BLOCKS_PER_MAP - 1) /
862                                 (unsigned long long) BLOCKS_PER_MAP;
863                 if (num_maps == (unsigned long) num_maps) {
864                         f->file_map = smalloc(num_maps * sizeof(unsigned long));
865                         if (f->file_map) {
866                                 f->num_maps = num_maps;
867                                 continue;
868                         }
869                 } else
870                         f->file_map = NULL;
871
872                 if (!td->o.softrandommap) {
873                         log_err("fio: failed allocating random map. If running"
874                                 " a large number of jobs, try the 'norandommap'"
875                                 " option or set 'softrandommap'. Or give"
876                                 " a larger --alloc-size to fio.\n");
877                         return 1;
878                 }
879
880                 log_info("fio: file %s failed allocating random map. Running "
881                          "job without.\n", f->file_name);
882                 f->num_maps = 0;
883         }
884
885         return 0;
886 }
887
888 void close_files(struct thread_data *td)
889 {
890         struct fio_file *f;
891         unsigned int i;
892
893         for_each_file(td, f, i) {
894                 if (fio_file_open(f))
895                         td_io_close_file(td, f);
896         }
897 }
898
899 void close_and_free_files(struct thread_data *td)
900 {
901         struct fio_file *f;
902         unsigned int i;
903
904         dprint(FD_FILE, "close files\n");
905
906         for_each_file(td, f, i) {
907                 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
908                         dprint(FD_FILE, "free unlink %s\n", f->file_name);
909                         unlink(f->file_name);
910                 }
911
912                 if (fio_file_open(f))
913                         td_io_close_file(td, f);
914
915                 remove_file_hash(f);
916
917                 sfree(f->file_name);
918                 f->file_name = NULL;
919                 sfree(f->file_map);
920                 f->file_map = NULL;
921                 sfree(f);
922         }
923
924         td->o.filename = NULL;
925         free(td->files);
926         td->files_index = 0;
927         td->files = NULL;
928         td->o.nr_files = 0;
929 }
930
931 static void get_file_type(struct fio_file *f)
932 {
933         struct stat sb;
934
935         if (!strcmp(f->file_name, "-"))
936                 f->filetype = FIO_TYPE_PIPE;
937         else
938                 f->filetype = FIO_TYPE_FILE;
939
940         /* \\.\ is the device namespace in Windows, where every file is
941          * a block device */
942         if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
943                 f->filetype = FIO_TYPE_BD;
944
945         if (!stat(f->file_name, &sb)) {
946                 if (S_ISBLK(sb.st_mode))
947                         f->filetype = FIO_TYPE_BD;
948                 else if (S_ISCHR(sb.st_mode))
949                         f->filetype = FIO_TYPE_CHAR;
950                 else if (S_ISFIFO(sb.st_mode))
951                         f->filetype = FIO_TYPE_PIPE;
952         }
953 }
954
955 int add_file(struct thread_data *td, const char *fname)
956 {
957         int cur_files = td->files_index;
958         char file_name[PATH_MAX];
959         struct fio_file *f;
960         int len = 0;
961
962         dprint(FD_FILE, "add file %s\n", fname);
963
964         f = smalloc(sizeof(*f));
965         if (!f) {
966                 log_err("fio: smalloc OOM\n");
967                 assert(0);
968         }
969
970         f->fd = -1;
971         fio_file_reset(f);
972
973         if (td->files_size <= td->files_index) {
974                 int new_size = td->o.nr_files + 1;
975
976                 dprint(FD_FILE, "resize file array to %d files\n", new_size);
977
978                 td->files = realloc(td->files, new_size * sizeof(f));
979                 td->files_size = new_size;
980         }
981         td->files[cur_files] = f;
982         f->fileno = cur_files;
983
984         /*
985          * init function, io engine may not be loaded yet
986          */
987         if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
988                 f->real_file_size = -1ULL;
989
990         if (td->o.directory)
991                 len = sprintf(file_name, "%s/", td->o.directory);
992
993         sprintf(file_name + len, "%s", fname);
994         f->file_name = smalloc_strdup(file_name);
995         if (!f->file_name) {
996                 log_err("fio: smalloc OOM\n");
997                 assert(0);
998         }
999
1000         get_file_type(f);
1001
1002         switch (td->o.file_lock_mode) {
1003         case FILE_LOCK_NONE:
1004                 break;
1005         case FILE_LOCK_READWRITE:
1006                 f->lock = fio_mutex_rw_init();
1007                 break;
1008         case FILE_LOCK_EXCLUSIVE:
1009                 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
1010                 break;
1011         default:
1012                 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1013                 assert(0);
1014         }
1015
1016         td->files_index++;
1017         if (f->filetype == FIO_TYPE_FILE)
1018                 td->nr_normal_files++;
1019
1020         dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1021                                                         cur_files);
1022
1023         return cur_files;
1024 }
1025
1026 int add_file_exclusive(struct thread_data *td, const char *fname)
1027 {
1028         struct fio_file *f;
1029         unsigned int i;
1030
1031         for_each_file(td, f, i) {
1032                 if (!strcmp(f->file_name, fname))
1033                         return i;
1034         }
1035
1036         return add_file(td, fname);
1037 }
1038
1039 void get_file(struct fio_file *f)
1040 {
1041         dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
1042         assert(fio_file_open(f));
1043         f->references++;
1044 }
1045
1046 int put_file(struct thread_data *td, struct fio_file *f)
1047 {
1048         int f_ret = 0, ret = 0;
1049
1050         dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
1051
1052         if (!fio_file_open(f)) {
1053                 assert(f->fd == -1);
1054                 return 0;
1055         }
1056
1057         assert(f->references);
1058         if (--f->references)
1059                 return 0;
1060
1061         if (should_fsync(td) && td->o.fsync_on_close)
1062                 f_ret = fsync(f->fd);
1063
1064         if (td->io_ops->close_file)
1065                 ret = td->io_ops->close_file(td, f);
1066
1067         if (!ret)
1068                 ret = f_ret;
1069
1070         td->nr_open_files--;
1071         fio_file_clear_open(f);
1072         assert(f->fd == -1);
1073         return ret;
1074 }
1075
1076 void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
1077 {
1078         if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1079                 return;
1080
1081         if (f->lock_owner == td && f->lock_batch--)
1082                 return;
1083
1084         if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1085                 if (ddir == DDIR_READ)
1086                         fio_mutex_down_read(f->lock);
1087                 else
1088                         fio_mutex_down_write(f->lock);
1089         } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1090                 fio_mutex_down(f->lock);
1091
1092         f->lock_owner = td;
1093         f->lock_batch = td->o.lockfile_batch;
1094         f->lock_ddir = ddir;
1095 }
1096
1097 void unlock_file(struct thread_data *td, struct fio_file *f)
1098 {
1099         if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1100                 return;
1101         if (f->lock_batch)
1102                 return;
1103
1104         if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1105                 const int is_read = f->lock_ddir == DDIR_READ;
1106                 int val = fio_mutex_getval(f->lock);
1107
1108                 if ((is_read && val == 1) || (!is_read && val == -1))
1109                         f->lock_owner = NULL;
1110
1111                 if (is_read)
1112                         fio_mutex_up_read(f->lock);
1113                 else
1114                         fio_mutex_up_write(f->lock);
1115         } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
1116                 int val = fio_mutex_getval(f->lock);
1117
1118                 if (val == 0)
1119                         f->lock_owner = NULL;
1120
1121                 fio_mutex_up(f->lock);
1122         }
1123 }
1124
1125 void unlock_file_all(struct thread_data *td, struct fio_file *f)
1126 {
1127         if (f->lock_owner != td)
1128                 return;
1129
1130         f->lock_batch = 0;
1131         unlock_file(td, f);
1132 }
1133
1134 static int recurse_dir(struct thread_data *td, const char *dirname)
1135 {
1136         struct dirent *dir;
1137         int ret = 0;
1138         DIR *D;
1139
1140         D = opendir(dirname);
1141         if (!D) {
1142                 char buf[FIO_VERROR_SIZE];
1143
1144                 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
1145                 td_verror(td, errno, buf);
1146                 return 1;
1147         }
1148
1149         while ((dir = readdir(D)) != NULL) {
1150                 char full_path[PATH_MAX];
1151                 struct stat sb;
1152
1153                 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1154                         continue;
1155
1156                 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
1157
1158                 if (lstat(full_path, &sb) == -1) {
1159                         if (errno != ENOENT) {
1160                                 td_verror(td, errno, "stat");
1161                                 return 1;
1162                         }
1163                 }
1164
1165                 if (S_ISREG(sb.st_mode)) {
1166                         add_file(td, full_path);
1167                         td->o.nr_files++;
1168                         continue;
1169                 }
1170                 if (!S_ISDIR(sb.st_mode))
1171                         continue;
1172
1173                 ret = recurse_dir(td, full_path);
1174                 if (ret)
1175                         break;
1176         }
1177
1178         closedir(D);
1179         return ret;
1180 }
1181
1182 int add_dir_files(struct thread_data *td, const char *path)
1183 {
1184         int ret = recurse_dir(td, path);
1185
1186         if (!ret)
1187                 log_info("fio: opendir added %d files\n", td->o.nr_files);
1188
1189         return ret;
1190 }
1191
1192 void dup_files(struct thread_data *td, struct thread_data *org)
1193 {
1194         struct fio_file *f;
1195         unsigned int i;
1196
1197         dprint(FD_FILE, "dup files: %d\n", org->files_index);
1198
1199         if (!org->files)
1200                 return;
1201
1202         td->files = malloc(org->files_index * sizeof(f));
1203
1204         for_each_file(org, f, i) {
1205                 struct fio_file *__f;
1206
1207                 __f = smalloc(sizeof(*__f));
1208                 if (!__f) {
1209                         log_err("fio: smalloc OOM\n");
1210                         assert(0);
1211                 }
1212                 __f->fd = -1;
1213                 fio_file_reset(__f);
1214
1215                 if (f->file_name) {
1216                         __f->file_name = smalloc_strdup(f->file_name);
1217                         if (!__f->file_name) {
1218                                 log_err("fio: smalloc OOM\n");
1219                                 assert(0);
1220                         }
1221
1222                         __f->filetype = f->filetype;
1223                 }
1224
1225                 td->files[i] = __f;
1226         }
1227 }
1228
1229 /*
1230  * Returns the index that matches the filename, or -1 if not there
1231  */
1232 int get_fileno(struct thread_data *td, const char *fname)
1233 {
1234         struct fio_file *f;
1235         unsigned int i;
1236
1237         for_each_file(td, f, i)
1238                 if (!strcmp(f->file_name, fname))
1239                         return i;
1240
1241         return -1;
1242 }
1243
1244 /*
1245  * For log usage, where we add/open/close files automatically
1246  */
1247 void free_release_files(struct thread_data *td)
1248 {
1249         close_files(td);
1250         td->files_index = 0;
1251         td->nr_normal_files = 0;
1252 }