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