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