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