sfree(NULL) is ok
[fio.git] / filesetup.c
... / ...
CommitLineData
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
14static int root_warn;
15
16static 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 */
25static 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);
147done:
148 return 0;
149err:
150 close(f->fd);
151 f->fd = -1;
152 return 1;
153}
154
155static 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
206static 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
219static 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
232static 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;
257err:
258 td->io_ops->close_file(td, f);
259 return 1;
260}
261
262static 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
289static 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
341int 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
349int 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
364static 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
389int 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
420open_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
473int 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 */
481static 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/*
507 * Open the files and setup files sizes, creating files if necessary.
508 */
509int setup_files(struct thread_data *td)
510{
511 unsigned long long total_size, extend_size;
512 struct fio_file *f;
513 unsigned int i;
514 int err = 0, need_extend;
515
516 dprint(FD_FILE, "setup files\n");
517
518 if (td->o.read_iolog_file)
519 return 0;
520
521 /*
522 * if ioengine defines a setup() method, it's responsible for
523 * opening the files and setting f->real_file_size to indicate
524 * the valid range for that file.
525 */
526 if (td->io_ops->setup)
527 err = td->io_ops->setup(td);
528 else
529 err = get_file_sizes(td);
530
531 if (err)
532 return err;
533
534 /*
535 * check sizes. if the files/devices do not exist and the size
536 * isn't passed to fio, abort.
537 */
538 total_size = 0;
539 for_each_file(td, f, i) {
540 if (f->real_file_size == -1ULL)
541 total_size = -1ULL;
542 else
543 total_size += f->real_file_size;
544 }
545
546 /*
547 * device/file sizes are zero and no size given, punt
548 */
549 if ((!total_size || total_size == -1ULL) && !td->o.size &&
550 !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
551 log_err("%s: you need to specify size=\n", td->o.name);
552 td_verror(td, EINVAL, "total_file_size");
553 return 1;
554 }
555
556 /*
557 * now file sizes are known, so we can set ->io_size. if size= is
558 * not given, ->io_size is just equal to ->real_file_size. if size
559 * is given, ->io_size is size / nr_files.
560 */
561 extend_size = total_size = 0;
562 need_extend = 0;
563 for_each_file(td, f, i) {
564 f->file_offset = td->o.start_offset;
565
566 if (!td->o.file_size_low) {
567 /*
568 * no file size range given, file size is equal to
569 * total size divided by number of files. if that is
570 * zero, set it to the real file size.
571 */
572 f->io_size = td->o.size / td->o.nr_files;
573 if (!f->io_size)
574 f->io_size = f->real_file_size - f->file_offset;
575 } else if (f->real_file_size < td->o.file_size_low ||
576 f->real_file_size > td->o.file_size_high) {
577 if (f->file_offset > td->o.file_size_low)
578 goto err_offset;
579 /*
580 * file size given. if it's fixed, use that. if it's a
581 * range, generate a random size in-between.
582 */
583 if (td->o.file_size_low == td->o.file_size_high) {
584 f->io_size = td->o.file_size_low
585 - f->file_offset;
586 } else {
587 f->io_size = get_rand_file_size(td)
588 - f->file_offset;
589 }
590 } else
591 f->io_size = f->real_file_size - f->file_offset;
592
593 if (f->io_size == -1ULL)
594 total_size = -1ULL;
595 else
596 total_size += f->io_size;
597
598 if (f->filetype == FIO_TYPE_FILE &&
599 (f->io_size + f->file_offset) > f->real_file_size &&
600 !(td->io_ops->flags & FIO_DISKLESSIO)) {
601 if (!td->o.create_on_open) {
602 need_extend++;
603 extend_size += (f->io_size + f->file_offset);
604 } else
605 f->real_file_size = f->io_size + f->file_offset;
606 fio_file_set_extend(f);
607 }
608 }
609
610 if (!td->o.size || td->o.size > total_size)
611 td->o.size = total_size;
612
613 /*
614 * See if we need to extend some files
615 */
616 if (need_extend) {
617 temp_stall_ts = 1;
618 if (!terse_output)
619 log_info("%s: Laying out IO file(s) (%u file(s) /"
620 " %LuMB)\n", td->o.name, need_extend,
621 extend_size >> 20);
622
623 for_each_file(td, f, i) {
624 unsigned long long old_len = -1ULL, extend_len = -1ULL;
625
626 if (!fio_file_extend(f))
627 continue;
628
629 assert(f->filetype == FIO_TYPE_FILE);
630 fio_file_clear_extend(f);
631 if (!td->o.fill_device) {
632 old_len = f->real_file_size;
633 extend_len = f->io_size + f->file_offset -
634 old_len;
635 }
636 f->real_file_size = (f->io_size + f->file_offset);
637 err = extend_file(td, f);
638 if (err)
639 break;
640
641 err = __file_invalidate_cache(td, f, old_len,
642 extend_len);
643 close(f->fd);
644 f->fd = -1;
645 if (err)
646 break;
647 }
648 temp_stall_ts = 0;
649 }
650
651 if (err)
652 return err;
653
654 if (!td->o.zone_size)
655 td->o.zone_size = td->o.size;
656
657 /*
658 * iolog already set the total io size, if we read back
659 * stored entries.
660 */
661 if (!td->o.read_iolog_file)
662 td->total_io_size = td->o.size * td->o.loops;
663 return 0;
664err_offset:
665 log_err("%s: you need to specify valid offset=\n", td->o.name);
666 return 1;
667}
668
669int pre_read_files(struct thread_data *td)
670{
671 struct fio_file *f;
672 unsigned int i;
673
674 dprint(FD_FILE, "pre_read files\n");
675
676 for_each_file(td, f, i) {
677 pre_read_file(td, f);
678 }
679
680 return 1;
681}
682
683int init_random_map(struct thread_data *td)
684{
685 unsigned long long blocks, num_maps;
686 struct fio_file *f;
687 unsigned int i;
688
689 if (td->o.norandommap || !td_random(td))
690 return 0;
691
692 for_each_file(td, f, i) {
693 blocks = (f->real_file_size + td->o.rw_min_bs - 1) /
694 (unsigned long long) td->o.rw_min_bs;
695 num_maps = (blocks + BLOCKS_PER_MAP - 1) /
696 (unsigned long long) BLOCKS_PER_MAP;
697 f->file_map = smalloc(num_maps * sizeof(int));
698 if (f->file_map) {
699 f->num_maps = num_maps;
700 continue;
701 }
702 if (!td->o.softrandommap) {
703 log_err("fio: failed allocating random map. If running"
704 " a large number of jobs, try the 'norandommap'"
705 " option or set 'softrandommap'. Or give"
706 " a larger --alloc-size to fio.\n");
707 return 1;
708 }
709
710 log_info("fio: file %s failed allocating random map. Running "
711 "job without.\n", f->file_name);
712 f->num_maps = 0;
713 }
714
715 return 0;
716}
717
718void close_files(struct thread_data *td)
719{
720 struct fio_file *f;
721 unsigned int i;
722
723 for_each_file(td, f, i) {
724 if (fio_file_open(f))
725 td_io_close_file(td, f);
726 }
727}
728
729void close_and_free_files(struct thread_data *td)
730{
731 struct fio_file *f;
732 unsigned int i;
733
734 dprint(FD_FILE, "close files\n");
735
736 for_each_file(td, f, i) {
737 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
738 dprint(FD_FILE, "free unlink %s\n", f->file_name);
739 unlink(f->file_name);
740 }
741
742 if (fio_file_open(f))
743 td_io_close_file(td, f);
744
745 remove_file_hash(f);
746
747 sfree(f->file_name);
748 f->file_name = NULL;
749 sfree(f->file_map);
750 f->file_map = NULL;
751 sfree(f);
752 }
753
754 td->o.filename = NULL;
755 free(td->files);
756 td->files_index = 0;
757 td->files = NULL;
758 td->o.nr_files = 0;
759}
760
761static void get_file_type(struct fio_file *f)
762{
763 struct stat sb;
764
765 if (!strcmp(f->file_name, "-"))
766 f->filetype = FIO_TYPE_PIPE;
767 else
768 f->filetype = FIO_TYPE_FILE;
769
770 if (!stat(f->file_name, &sb)) {
771 if (S_ISBLK(sb.st_mode))
772 f->filetype = FIO_TYPE_BD;
773 else if (S_ISCHR(sb.st_mode))
774 f->filetype = FIO_TYPE_CHAR;
775 else if (S_ISFIFO(sb.st_mode))
776 f->filetype = FIO_TYPE_PIPE;
777 }
778}
779
780int add_file(struct thread_data *td, const char *fname)
781{
782 int cur_files = td->files_index;
783 char file_name[PATH_MAX];
784 struct fio_file *f;
785 int len = 0;
786
787 dprint(FD_FILE, "add file %s\n", fname);
788
789 f = smalloc(sizeof(*f));
790 if (!f) {
791 log_err("fio: smalloc OOM\n");
792 assert(0);
793 }
794
795 f->fd = -1;
796
797 if (td->files_size <= td->files_index) {
798 int new_size = td->o.nr_files + 1;
799
800 dprint(FD_FILE, "resize file array to %d files\n", new_size);
801
802 td->files = realloc(td->files, new_size * sizeof(f));
803 td->files_size = new_size;
804 }
805 td->files[cur_files] = f;
806
807 /*
808 * init function, io engine may not be loaded yet
809 */
810 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
811 f->real_file_size = -1ULL;
812
813 if (td->o.directory)
814 len = sprintf(file_name, "%s/", td->o.directory);
815
816 sprintf(file_name + len, "%s", fname);
817 f->file_name = smalloc_strdup(file_name);
818 if (!f->file_name) {
819 log_err("fio: smalloc OOM\n");
820 assert(0);
821 }
822
823 get_file_type(f);
824
825 switch (td->o.file_lock_mode) {
826 case FILE_LOCK_NONE:
827 break;
828 case FILE_LOCK_READWRITE:
829 f->lock = fio_mutex_rw_init();
830 break;
831 case FILE_LOCK_EXCLUSIVE:
832 f->lock = fio_mutex_init(1);
833 break;
834 default:
835 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
836 assert(0);
837 }
838
839 td->files_index++;
840 if (f->filetype == FIO_TYPE_FILE)
841 td->nr_normal_files++;
842
843 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
844 cur_files);
845
846 return cur_files;
847}
848
849void get_file(struct fio_file *f)
850{
851 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
852 assert(fio_file_open(f));
853 f->references++;
854}
855
856int put_file(struct thread_data *td, struct fio_file *f)
857{
858 int f_ret = 0, ret = 0;
859
860 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
861
862 if (!fio_file_open(f)) {
863 assert(f->fd == -1);
864 return 0;
865 }
866
867 assert(f->references);
868 if (--f->references)
869 return 0;
870
871 if (should_fsync(td) && td->o.fsync_on_close)
872 f_ret = fsync(f->fd);
873
874 if (td->io_ops->close_file)
875 ret = td->io_ops->close_file(td, f);
876
877 if (!ret)
878 ret = f_ret;
879
880 td->nr_open_files--;
881 fio_file_clear_open(f);
882 assert(f->fd == -1);
883 return ret;
884}
885
886void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
887{
888 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
889 return;
890
891 if (f->lock_owner == td && f->lock_batch--)
892 return;
893
894 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
895 if (ddir == DDIR_READ)
896 fio_mutex_down_read(f->lock);
897 else
898 fio_mutex_down_write(f->lock);
899 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
900 fio_mutex_down(f->lock);
901
902 f->lock_owner = td;
903 f->lock_batch = td->o.lockfile_batch;
904 f->lock_ddir = ddir;
905}
906
907void unlock_file(struct thread_data *td, struct fio_file *f)
908{
909 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
910 return;
911 if (f->lock_batch)
912 return;
913
914 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
915 const int is_read = f->lock_ddir == DDIR_READ;
916 int val = fio_mutex_getval(f->lock);
917
918 if ((is_read && val == 1) || (!is_read && val == -1))
919 f->lock_owner = NULL;
920
921 if (is_read)
922 fio_mutex_up_read(f->lock);
923 else
924 fio_mutex_up_write(f->lock);
925 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
926 int val = fio_mutex_getval(f->lock);
927
928 if (val == 0)
929 f->lock_owner = NULL;
930
931 fio_mutex_up(f->lock);
932 }
933}
934
935void unlock_file_all(struct thread_data *td, struct fio_file *f)
936{
937 if (f->lock_owner != td)
938 return;
939
940 f->lock_batch = 0;
941 unlock_file(td, f);
942}
943
944static int recurse_dir(struct thread_data *td, const char *dirname)
945{
946 struct dirent *dir;
947 int ret = 0;
948 DIR *D;
949
950 D = opendir(dirname);
951 if (!D) {
952 char buf[FIO_VERROR_SIZE];
953
954 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
955 td_verror(td, errno, buf);
956 return 1;
957 }
958
959 while ((dir = readdir(D)) != NULL) {
960 char full_path[PATH_MAX];
961 struct stat sb;
962
963 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
964 continue;
965
966 sprintf(full_path, "%s/%s", dirname, dir->d_name);
967
968 if (lstat(full_path, &sb) == -1) {
969 if (errno != ENOENT) {
970 td_verror(td, errno, "stat");
971 return 1;
972 }
973 }
974
975 if (S_ISREG(sb.st_mode)) {
976 add_file(td, full_path);
977 td->o.nr_files++;
978 continue;
979 }
980 if (!S_ISDIR(sb.st_mode))
981 continue;
982
983 ret = recurse_dir(td, full_path);
984 if (ret)
985 break;
986 }
987
988 closedir(D);
989 return ret;
990}
991
992int add_dir_files(struct thread_data *td, const char *path)
993{
994 int ret = recurse_dir(td, path);
995
996 if (!ret)
997 log_info("fio: opendir added %d files\n", td->o.nr_files);
998
999 return ret;
1000}
1001
1002void dup_files(struct thread_data *td, struct thread_data *org)
1003{
1004 struct fio_file *f;
1005 unsigned int i;
1006
1007 dprint(FD_FILE, "dup files: %d\n", org->files_index);
1008
1009 if (!org->files)
1010 return;
1011
1012 td->files = malloc(org->files_index * sizeof(f));
1013
1014 for_each_file(org, f, i) {
1015 struct fio_file *__f;
1016
1017 __f = smalloc(sizeof(*__f));
1018 if (!__f) {
1019 log_err("fio: smalloc OOM\n");
1020 assert(0);
1021 }
1022 __f->fd = -1;
1023
1024 if (f->file_name) {
1025 __f->file_name = smalloc_strdup(f->file_name);
1026 if (!__f->file_name) {
1027 log_err("fio: smalloc OOM\n");
1028 assert(0);
1029 }
1030
1031 __f->filetype = f->filetype;
1032 }
1033
1034 td->files[i] = __f;
1035 }
1036}
1037
1038/*
1039 * Returns the index that matches the filename, or -1 if not there
1040 */
1041int get_fileno(struct thread_data *td, const char *fname)
1042{
1043 struct fio_file *f;
1044 unsigned int i;
1045
1046 for_each_file(td, f, i)
1047 if (!strcmp(f->file_name, fname))
1048 return i;
1049
1050 return -1;
1051}
1052
1053/*
1054 * For log usage, where we add/open/close files automatically
1055 */
1056void free_release_files(struct thread_data *td)
1057{
1058 close_files(td);
1059 td->files_index = 0;
1060 td->nr_normal_files = 0;
1061}