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