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