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