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