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