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