filesetup.c revision 90426237af4f3c9c7628aebfd4421fbe43d68c2a
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#include "options.h" 15#include "os/os.h" 16#include "hash.h" 17#include "lib/axmap.h" 18 19#ifdef CONFIG_LINUX_FALLOCATE 20#include <linux/falloc.h> 21#endif 22 23static int root_warn; 24 25static FLIST_HEAD(filename_list); 26 27static inline void clear_error(struct thread_data *td) 28{ 29 td->error = 0; 30 td->verror[0] = '\0'; 31} 32 33/* 34 * Leaves f->fd open on success, caller must close 35 */ 36static int extend_file(struct thread_data *td, struct fio_file *f) 37{ 38 int r, new_layout = 0, unlink_file = 0, flags; 39 unsigned long long left; 40 unsigned int bs; 41 char *b; 42 43 if (read_only) { 44 log_err("fio: refusing extend of file due to read-only\n"); 45 return 0; 46 } 47 48 /* 49 * check if we need to lay the file out complete again. fio 50 * does that for operations involving reads, or for writes 51 * where overwrite is set 52 */ 53 if (td_read(td) || 54 (td_write(td) && td->o.overwrite && !td->o.file_append) || 55 (td_write(td) && td->io_ops->flags & FIO_NOEXTEND)) 56 new_layout = 1; 57 if (td_write(td) && !td->o.overwrite && !td->o.file_append) 58 unlink_file = 1; 59 60 if (unlink_file || new_layout) { 61 dprint(FD_FILE, "layout unlink %s\n", f->file_name); 62 if ((unlink(f->file_name) < 0) && (errno != ENOENT)) { 63 td_verror(td, errno, "unlink"); 64 return 1; 65 } 66 } 67 68 flags = O_WRONLY | O_CREAT; 69 if (new_layout) 70 flags |= O_TRUNC; 71 72 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags); 73 f->fd = open(f->file_name, flags, 0644); 74 if (f->fd < 0) { 75 td_verror(td, errno, "open"); 76 return 1; 77 } 78 79#ifdef CONFIG_POSIX_FALLOCATE 80 if (!td->o.fill_device) { 81 switch (td->o.fallocate_mode) { 82 case FIO_FALLOCATE_NONE: 83 break; 84 case FIO_FALLOCATE_POSIX: 85 dprint(FD_FILE, "posix_fallocate file %s size %llu\n", 86 f->file_name, 87 (unsigned long long) f->real_file_size); 88 89 r = posix_fallocate(f->fd, 0, f->real_file_size); 90 if (r > 0) { 91 log_err("fio: posix_fallocate fails: %s\n", 92 strerror(r)); 93 } 94 break; 95#ifdef CONFIG_LINUX_FALLOCATE 96 case FIO_FALLOCATE_KEEP_SIZE: 97 dprint(FD_FILE, 98 "fallocate(FALLOC_FL_KEEP_SIZE) " 99 "file %s size %llu\n", f->file_name, 100 (unsigned long long) f->real_file_size); 101 102 r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0, 103 f->real_file_size); 104 if (r != 0) 105 td_verror(td, errno, "fallocate"); 106 107 break; 108#endif /* CONFIG_LINUX_FALLOCATE */ 109 default: 110 log_err("fio: unknown fallocate mode: %d\n", 111 td->o.fallocate_mode); 112 assert(0); 113 } 114 } 115#endif /* CONFIG_POSIX_FALLOCATE */ 116 117 if (!new_layout) 118 goto done; 119 120 /* 121 * The size will be -1ULL when fill_device is used, so don't truncate 122 * or fallocate this file, just write it 123 */ 124 if (!td->o.fill_device) { 125 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name, 126 (unsigned long long) f->real_file_size); 127 if (ftruncate(f->fd, f->real_file_size) == -1) { 128 if (errno != EFBIG) { 129 td_verror(td, errno, "ftruncate"); 130 goto err; 131 } 132 } 133 } 134 135 b = malloc(td->o.max_bs[DDIR_WRITE]); 136 137 left = f->real_file_size; 138 while (left && !td->terminate) { 139 bs = td->o.max_bs[DDIR_WRITE]; 140 if (bs > left) 141 bs = left; 142 143 fill_io_buffer(td, b, bs, bs); 144 145 r = write(f->fd, b, bs); 146 147 if (r > 0) { 148 left -= r; 149 continue; 150 } else { 151 if (r < 0) { 152 int __e = errno; 153 154 if (__e == ENOSPC) { 155 if (td->o.fill_device) 156 break; 157 log_info("fio: ENOSPC on laying out " 158 "file, stopping\n"); 159 break; 160 } 161 td_verror(td, errno, "write"); 162 } else 163 td_verror(td, EIO, "write"); 164 165 break; 166 } 167 } 168 169 if (td->terminate) { 170 dprint(FD_FILE, "terminate unlink %s\n", f->file_name); 171 unlink(f->file_name); 172 } else if (td->o.create_fsync) { 173 if (fsync(f->fd) < 0) { 174 td_verror(td, errno, "fsync"); 175 goto err; 176 } 177 } 178 if (td->o.fill_device && !td_write(td)) { 179 fio_file_clear_size_known(f); 180 if (td_io_get_file_size(td, f)) 181 goto err; 182 if (f->io_size > f->real_file_size) 183 f->io_size = f->real_file_size; 184 } 185 186 free(b); 187done: 188 return 0; 189err: 190 close(f->fd); 191 f->fd = -1; 192 return 1; 193} 194 195static int pre_read_file(struct thread_data *td, struct fio_file *f) 196{ 197 int r, did_open = 0, old_runstate; 198 unsigned long long left; 199 unsigned int bs; 200 char *b; 201 202 if (td->io_ops->flags & FIO_PIPEIO) 203 return 0; 204 205 if (!fio_file_open(f)) { 206 if (td->io_ops->open_file(td, f)) { 207 log_err("fio: cannot pre-read, failed to open file\n"); 208 return 1; 209 } 210 did_open = 1; 211 } 212 213 old_runstate = td_bump_runstate(td, TD_PRE_READING); 214 215 bs = td->o.max_bs[DDIR_READ]; 216 b = malloc(bs); 217 memset(b, 0, bs); 218 219 lseek(f->fd, f->file_offset, SEEK_SET); 220 left = f->io_size; 221 222 while (left && !td->terminate) { 223 if (bs > left) 224 bs = left; 225 226 r = read(f->fd, b, bs); 227 228 if (r == (int) bs) { 229 left -= bs; 230 continue; 231 } else { 232 td_verror(td, EIO, "pre_read"); 233 break; 234 } 235 } 236 237 td_restore_runstate(td, old_runstate); 238 239 if (did_open) 240 td->io_ops->close_file(td, f); 241 free(b); 242 return 0; 243} 244 245static unsigned long long get_rand_file_size(struct thread_data *td) 246{ 247 unsigned long long ret, sized; 248 unsigned long r; 249 250 if (td->o.use_os_rand) { 251 r = os_random_long(&td->file_size_state); 252 sized = td->o.file_size_high - td->o.file_size_low; 253 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0))); 254 } else { 255 r = __rand(&td->__file_size_state); 256 sized = td->o.file_size_high - td->o.file_size_low; 257 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0))); 258 } 259 260 ret += td->o.file_size_low; 261 ret -= (ret % td->o.rw_min_bs); 262 return ret; 263} 264 265static int file_size(struct thread_data *td, struct fio_file *f) 266{ 267 struct stat st; 268 269 if (stat(f->file_name, &st) == -1) { 270 td_verror(td, errno, "fstat"); 271 return 1; 272 } 273 274 f->real_file_size = st.st_size; 275 return 0; 276} 277 278static int bdev_size(struct thread_data *td, struct fio_file *f) 279{ 280 unsigned long long bytes = 0; 281 int r; 282 283 if (td->io_ops->open_file(td, f)) { 284 log_err("fio: failed opening blockdev %s for size check\n", 285 f->file_name); 286 return 1; 287 } 288 289 r = blockdev_size(f, &bytes); 290 if (r) { 291 td_verror(td, r, "blockdev_size"); 292 goto err; 293 } 294 295 if (!bytes) { 296 log_err("%s: zero sized block device?\n", f->file_name); 297 goto err; 298 } 299 300 f->real_file_size = bytes; 301 td->io_ops->close_file(td, f); 302 return 0; 303err: 304 td->io_ops->close_file(td, f); 305 return 1; 306} 307 308static int char_size(struct thread_data *td, struct fio_file *f) 309{ 310#ifdef FIO_HAVE_CHARDEV_SIZE 311 unsigned long long bytes = 0; 312 int r; 313 314 if (td->io_ops->open_file(td, f)) { 315 log_err("fio: failed opening blockdev %s for size check\n", 316 f->file_name); 317 return 1; 318 } 319 320 r = chardev_size(f, &bytes); 321 if (r) { 322 td_verror(td, r, "chardev_size"); 323 goto err; 324 } 325 326 if (!bytes) { 327 log_err("%s: zero sized char device?\n", f->file_name); 328 goto err; 329 } 330 331 f->real_file_size = bytes; 332 td->io_ops->close_file(td, f); 333 return 0; 334err: 335 td->io_ops->close_file(td, f); 336 return 1; 337#else 338 f->real_file_size = -1ULL; 339 return 0; 340#endif 341} 342 343static int get_file_size(struct thread_data *td, struct fio_file *f) 344{ 345 int ret = 0; 346 347 if (fio_file_size_known(f)) 348 return 0; 349 350 if (f->filetype == FIO_TYPE_FILE) 351 ret = file_size(td, f); 352 else if (f->filetype == FIO_TYPE_BD) 353 ret = bdev_size(td, f); 354 else if (f->filetype == FIO_TYPE_CHAR) 355 ret = char_size(td, f); 356 else 357 f->real_file_size = -1; 358 359 if (ret) 360 return ret; 361 362 if (f->file_offset > f->real_file_size) { 363 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name, 364 (unsigned long long) f->file_offset, 365 (unsigned long long) f->real_file_size); 366 return 1; 367 } 368 369 fio_file_set_size_known(f); 370 return 0; 371} 372 373static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f, 374 unsigned long long off, 375 unsigned long long len) 376{ 377 int ret = 0; 378 379 if (len == -1ULL) 380 len = f->io_size; 381 if (off == -1ULL) 382 off = f->file_offset; 383 384 if (len == -1ULL || off == -1ULL) 385 return 0; 386 387 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off, 388 len); 389 390 /* 391 * FIXME: add blockdev flushing too 392 */ 393 if (f->mmap_ptr) { 394 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED); 395#ifdef FIO_MADV_FREE 396 if (f->filetype == FIO_TYPE_BD) 397 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE); 398#endif 399 } else if (f->filetype == FIO_TYPE_FILE) { 400 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED); 401 } else if (f->filetype == FIO_TYPE_BD) { 402 ret = blockdev_invalidate_cache(f); 403 if (ret < 0 && errno == EACCES && geteuid()) { 404 if (!root_warn) { 405 log_err("fio: only root may flush block " 406 "devices. Cache flush bypassed!\n"); 407 root_warn = 1; 408 } 409 ret = 0; 410 } 411 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE) 412 ret = 0; 413 414 if (ret < 0) { 415 td_verror(td, errno, "invalidate_cache"); 416 return 1; 417 } else if (ret > 0) { 418 td_verror(td, ret, "invalidate_cache"); 419 return 1; 420 } 421 422 return ret; 423 424} 425 426int file_invalidate_cache(struct thread_data *td, struct fio_file *f) 427{ 428 if (!fio_file_open(f)) 429 return 0; 430 431 return __file_invalidate_cache(td, f, -1ULL, -1ULL); 432} 433 434int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f) 435{ 436 int ret = 0; 437 438 dprint(FD_FILE, "fd close %s\n", f->file_name); 439 440 remove_file_hash(f); 441 442 if (close(f->fd) < 0) 443 ret = errno; 444 445 f->fd = -1; 446 447 if (f->shadow_fd != -1) { 448 close(f->shadow_fd); 449 f->shadow_fd = -1; 450 } 451 452 f->engine_data = 0; 453 return ret; 454} 455 456int file_lookup_open(struct fio_file *f, int flags) 457{ 458 struct fio_file *__f; 459 int from_hash; 460 461 __f = lookup_file_hash(f->file_name); 462 if (__f) { 463 dprint(FD_FILE, "found file in hash %s\n", f->file_name); 464 /* 465 * racy, need the __f->lock locked 466 */ 467 f->lock = __f->lock; 468 from_hash = 1; 469 } else { 470 dprint(FD_FILE, "file not found in hash %s\n", f->file_name); 471 from_hash = 0; 472 } 473 474 f->fd = open(f->file_name, flags, 0600); 475 return from_hash; 476} 477 478static int file_close_shadow_fds(struct thread_data *td) 479{ 480 struct fio_file *f; 481 int num_closed = 0; 482 unsigned int i; 483 484 for_each_file(td, f, i) { 485 if (f->shadow_fd == -1) 486 continue; 487 488 close(f->shadow_fd); 489 f->shadow_fd = -1; 490 num_closed++; 491 } 492 493 return num_closed; 494} 495 496int generic_open_file(struct thread_data *td, struct fio_file *f) 497{ 498 int is_std = 0; 499 int flags = 0; 500 int from_hash = 0; 501 502 dprint(FD_FILE, "fd open %s\n", f->file_name); 503 504 if (td_trim(td) && f->filetype != FIO_TYPE_BD) { 505 log_err("fio: trim only applies to block device\n"); 506 return 1; 507 } 508 509 if (!strcmp(f->file_name, "-")) { 510 if (td_rw(td)) { 511 log_err("fio: can't read/write to stdin/out\n"); 512 return 1; 513 } 514 is_std = 1; 515 516 /* 517 * move output logging to stderr, if we are writing to stdout 518 */ 519 if (td_write(td)) 520 f_out = stderr; 521 } 522 523 if (td_trim(td)) 524 goto skip_flags; 525 if (td->o.odirect) 526 flags |= OS_O_DIRECT; 527 if (td->o.oatomic) { 528 if (!FIO_O_ATOMIC) { 529 td_verror(td, EINVAL, "OS does not support atomic IO"); 530 return 1; 531 } 532 flags |= OS_O_DIRECT | FIO_O_ATOMIC; 533 } 534 if (td->o.sync_io) 535 flags |= O_SYNC; 536 if (td->o.create_on_open) 537 flags |= O_CREAT; 538skip_flags: 539 if (f->filetype != FIO_TYPE_FILE) 540 flags |= FIO_O_NOATIME; 541 542open_again: 543 if (td_write(td)) { 544 if (!read_only) 545 flags |= O_RDWR; 546 547 if (f->filetype == FIO_TYPE_FILE) 548 flags |= O_CREAT; 549 550 if (is_std) 551 f->fd = dup(STDOUT_FILENO); 552 else 553 from_hash = file_lookup_open(f, flags); 554 } else if (td_read(td)) { 555 if (f->filetype == FIO_TYPE_CHAR && !read_only) 556 flags |= O_RDWR; 557 else 558 flags |= O_RDONLY; 559 560 if (is_std) 561 f->fd = dup(STDIN_FILENO); 562 else 563 from_hash = file_lookup_open(f, flags); 564 } else { //td trim 565 flags |= O_RDWR; 566 from_hash = file_lookup_open(f, flags); 567 } 568 569 if (f->fd == -1) { 570 char buf[FIO_VERROR_SIZE]; 571 int __e = errno; 572 573 if (__e == EPERM && (flags & FIO_O_NOATIME)) { 574 flags &= ~FIO_O_NOATIME; 575 goto open_again; 576 } 577 if (__e == EMFILE && file_close_shadow_fds(td)) 578 goto open_again; 579 580 snprintf(buf, sizeof(buf), "open(%s)", f->file_name); 581 582 if (__e == EINVAL && (flags & OS_O_DIRECT)) { 583 log_err("fio: looks like your file system does not " \ 584 "support direct=1/buffered=0\n"); 585 } 586 587 td_verror(td, __e, buf); 588 } 589 590 if (!from_hash && f->fd != -1) { 591 if (add_file_hash(f)) { 592 int fio_unused ret; 593 594 /* 595 * Stash away descriptor for later close. This is to 596 * work-around a "feature" on Linux, where a close of 597 * an fd that has been opened for write will trigger 598 * udev to call blkid to check partitions, fs id, etc. 599 * That pollutes the device cache, which can slow down 600 * unbuffered accesses. 601 */ 602 if (f->shadow_fd == -1) 603 f->shadow_fd = f->fd; 604 else { 605 /* 606 * OK to ignore, we haven't done anything 607 * with it 608 */ 609 ret = generic_close_file(td, f); 610 } 611 goto open_again; 612 } 613 } 614 615 return 0; 616} 617 618int generic_get_file_size(struct thread_data *td, struct fio_file *f) 619{ 620 return get_file_size(td, f); 621} 622 623/* 624 * open/close all files, so that ->real_file_size gets set 625 */ 626static int get_file_sizes(struct thread_data *td) 627{ 628 struct fio_file *f; 629 unsigned int i; 630 int err = 0; 631 632 for_each_file(td, f, i) { 633 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i, 634 f->file_name); 635 636 if (td_io_get_file_size(td, f)) { 637 if (td->error != ENOENT) { 638 log_err("%s\n", td->verror); 639 err = 1; 640 } 641 clear_error(td); 642 } 643 644 if (f->real_file_size == -1ULL && td->o.size) 645 f->real_file_size = td->o.size / td->o.nr_files; 646 } 647 648 return err; 649} 650 651struct fio_mount { 652 struct flist_head list; 653 const char *base; 654 char __base[256]; 655 unsigned int key; 656}; 657 658/* 659 * Get free number of bytes for each file on each unique mount. 660 */ 661static unsigned long long get_fs_free_counts(struct thread_data *td) 662{ 663 struct flist_head *n, *tmp; 664 unsigned long long ret = 0; 665 struct fio_mount *fm; 666 FLIST_HEAD(list); 667 struct fio_file *f; 668 unsigned int i; 669 670 for_each_file(td, f, i) { 671 struct stat sb; 672 char buf[256]; 673 674 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) { 675 if (f->real_file_size != -1ULL) 676 ret += f->real_file_size; 677 continue; 678 } else if (f->filetype != FIO_TYPE_FILE) 679 continue; 680 681 strcpy(buf, f->file_name); 682 683 if (stat(buf, &sb) < 0) { 684 if (errno != ENOENT) 685 break; 686 strcpy(buf, "."); 687 if (stat(buf, &sb) < 0) 688 break; 689 } 690 691 fm = NULL; 692 flist_for_each(n, &list) { 693 fm = flist_entry(n, struct fio_mount, list); 694 if (fm->key == sb.st_dev) 695 break; 696 697 fm = NULL; 698 } 699 700 if (fm) 701 continue; 702 703 fm = malloc(sizeof(*fm)); 704 strcpy(fm->__base, buf); 705 fm->base = basename(fm->__base); 706 fm->key = sb.st_dev; 707 flist_add(&fm->list, &list); 708 } 709 710 flist_for_each_safe(n, tmp, &list) { 711 unsigned long long sz; 712 713 fm = flist_entry(n, struct fio_mount, list); 714 flist_del(&fm->list); 715 716 sz = get_fs_size(fm->base); 717 if (sz && sz != -1ULL) 718 ret += sz; 719 720 free(fm); 721 } 722 723 return ret; 724} 725 726uint64_t get_start_offset(struct thread_data *td, struct fio_file *f) 727{ 728 struct thread_options *o = &td->o; 729 730 if (o->file_append && f->filetype == FIO_TYPE_FILE) 731 return f->real_file_size; 732 733 return td->o.start_offset + 734 (td->thread_number - 1) * td->o.offset_increment; 735} 736 737/* 738 * Open the files and setup files sizes, creating files if necessary. 739 */ 740int setup_files(struct thread_data *td) 741{ 742 unsigned long long total_size, extend_size; 743 struct thread_options *o = &td->o; 744 struct fio_file *f; 745 unsigned int i, nr_fs_extra = 0; 746 int err = 0, need_extend; 747 int old_state; 748 const unsigned int bs = td_min_bs(td); 749 uint64_t fs = 0; 750 751 dprint(FD_FILE, "setup files\n"); 752 753 old_state = td_bump_runstate(td, TD_SETTING_UP); 754 755 if (o->read_iolog_file) 756 goto done; 757 758 /* 759 * if ioengine defines a setup() method, it's responsible for 760 * opening the files and setting f->real_file_size to indicate 761 * the valid range for that file. 762 */ 763 if (td->io_ops->setup) 764 err = td->io_ops->setup(td); 765 else 766 err = get_file_sizes(td); 767 768 if (err) 769 goto err_out; 770 771 /* 772 * check sizes. if the files/devices do not exist and the size 773 * isn't passed to fio, abort. 774 */ 775 total_size = 0; 776 for_each_file(td, f, i) { 777 if (f->real_file_size == -1ULL) 778 total_size = -1ULL; 779 else 780 total_size += f->real_file_size; 781 } 782 783 if (o->fill_device) 784 td->fill_device_size = get_fs_free_counts(td); 785 786 /* 787 * device/file sizes are zero and no size given, punt 788 */ 789 if ((!total_size || total_size == -1ULL) && !o->size && 790 !(td->io_ops->flags & FIO_NOIO) && !o->fill_device && 791 !(o->nr_files && (o->file_size_low || o->file_size_high))) { 792 log_err("%s: you need to specify size=\n", o->name); 793 td_verror(td, EINVAL, "total_file_size"); 794 goto err_out; 795 } 796 797 /* 798 * Calculate per-file size and potential extra size for the 799 * first files, if needed. 800 */ 801 if (!o->file_size_low) { 802 uint64_t all_fs; 803 804 fs = o->size / o->nr_files; 805 all_fs = fs * o->nr_files; 806 807 if (all_fs < o->size) 808 nr_fs_extra = (o->size - all_fs) / bs; 809 } 810 811 /* 812 * now file sizes are known, so we can set ->io_size. if size= is 813 * not given, ->io_size is just equal to ->real_file_size. if size 814 * is given, ->io_size is size / nr_files. 815 */ 816 extend_size = total_size = 0; 817 need_extend = 0; 818 for_each_file(td, f, i) { 819 f->file_offset = get_start_offset(td, f); 820 821 if (!o->file_size_low) { 822 /* 823 * no file size range given, file size is equal to 824 * total size divided by number of files. If that is 825 * zero, set it to the real file size. If the size 826 * doesn't divide nicely with the min blocksize, 827 * make the first files bigger. 828 */ 829 f->io_size = fs; 830 if (nr_fs_extra) { 831 nr_fs_extra--; 832 f->io_size += bs; 833 } 834 835 if (!f->io_size) 836 f->io_size = f->real_file_size - f->file_offset; 837 } else if (f->real_file_size < o->file_size_low || 838 f->real_file_size > o->file_size_high) { 839 if (f->file_offset > o->file_size_low) 840 goto err_offset; 841 /* 842 * file size given. if it's fixed, use that. if it's a 843 * range, generate a random size in-between. 844 */ 845 if (o->file_size_low == o->file_size_high) 846 f->io_size = o->file_size_low - f->file_offset; 847 else { 848 f->io_size = get_rand_file_size(td) 849 - f->file_offset; 850 } 851 } else 852 f->io_size = f->real_file_size - f->file_offset; 853 854 if (f->io_size == -1ULL) 855 total_size = -1ULL; 856 else { 857 if (o->size_percent) 858 f->io_size = (f->io_size * o->size_percent) / 100; 859 total_size += f->io_size; 860 } 861 862 if (f->filetype == FIO_TYPE_FILE && 863 (f->io_size + f->file_offset) > f->real_file_size && 864 !(td->io_ops->flags & FIO_DISKLESSIO)) { 865 if (!o->create_on_open) { 866 need_extend++; 867 extend_size += (f->io_size + f->file_offset); 868 } else 869 f->real_file_size = f->io_size + f->file_offset; 870 fio_file_set_extend(f); 871 } 872 } 873 874 if (!o->size || o->size > total_size) 875 o->size = total_size; 876 877 /* 878 * See if we need to extend some files 879 */ 880 if (need_extend) { 881 temp_stall_ts = 1; 882 if (output_format == FIO_OUTPUT_NORMAL) 883 log_info("%s: Laying out IO file(s) (%u file(s) /" 884 " %lluMB)\n", o->name, need_extend, 885 extend_size >> 20); 886 887 for_each_file(td, f, i) { 888 unsigned long long old_len = -1ULL, extend_len = -1ULL; 889 890 if (!fio_file_extend(f)) 891 continue; 892 893 assert(f->filetype == FIO_TYPE_FILE); 894 fio_file_clear_extend(f); 895 if (!o->fill_device) { 896 old_len = f->real_file_size; 897 extend_len = f->io_size + f->file_offset - 898 old_len; 899 } 900 f->real_file_size = (f->io_size + f->file_offset); 901 err = extend_file(td, f); 902 if (err) 903 break; 904 905 err = __file_invalidate_cache(td, f, old_len, 906 extend_len); 907 close(f->fd); 908 f->fd = -1; 909 if (err) 910 break; 911 } 912 temp_stall_ts = 0; 913 } 914 915 if (err) 916 goto err_out; 917 918 if (!o->zone_size) 919 o->zone_size = o->size; 920 921 /* 922 * iolog already set the total io size, if we read back 923 * stored entries. 924 */ 925 if (!o->read_iolog_file) 926 td->total_io_size = o->size * o->loops; 927 928done: 929 if (o->create_only) 930 td->done = 1; 931 932 td_restore_runstate(td, old_state); 933 return 0; 934err_offset: 935 log_err("%s: you need to specify valid offset=\n", o->name); 936err_out: 937 td_restore_runstate(td, old_state); 938 return 1; 939} 940 941int pre_read_files(struct thread_data *td) 942{ 943 struct fio_file *f; 944 unsigned int i; 945 946 dprint(FD_FILE, "pre_read files\n"); 947 948 for_each_file(td, f, i) { 949 pre_read_file(td, f); 950 } 951 952 return 1; 953} 954 955static int __init_rand_distribution(struct thread_data *td, struct fio_file *f) 956{ 957 unsigned int range_size, seed; 958 unsigned long nranges; 959 uint64_t file_size; 960 961 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]); 962 file_size = min(f->real_file_size, f->io_size); 963 964 nranges = (file_size + range_size - 1) / range_size; 965 966 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number; 967 if (!td->o.rand_repeatable) 968 seed = td->rand_seeds[4]; 969 970 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF) 971 zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed); 972 else 973 pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed); 974 975 return 1; 976} 977 978static int init_rand_distribution(struct thread_data *td) 979{ 980 struct fio_file *f; 981 unsigned int i; 982 int state; 983 984 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM) 985 return 0; 986 987 state = td_bump_runstate(td, TD_SETTING_UP); 988 989 for_each_file(td, f, i) 990 __init_rand_distribution(td, f); 991 992 td_restore_runstate(td, state); 993 994 return 1; 995} 996 997int init_random_map(struct thread_data *td) 998{ 999 unsigned long long blocks; 1000 struct fio_file *f; 1001 unsigned int i; 1002 1003 if (init_rand_distribution(td)) 1004 return 0; 1005 if (!td_random(td)) 1006 return 0; 1007 1008 for_each_file(td, f, i) { 1009 uint64_t file_size = min(f->real_file_size, f->io_size); 1010 1011 blocks = file_size / (unsigned long long) td->o.rw_min_bs; 1012 1013 if (td->o.random_generator == FIO_RAND_GEN_LFSR) { 1014 unsigned long seed; 1015 1016 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF]; 1017 1018 if (!lfsr_init(&f->lfsr, blocks, seed, 0)) 1019 continue; 1020 } else if (!td->o.norandommap) { 1021 f->io_axmap = axmap_new(blocks); 1022 if (f->io_axmap) 1023 continue; 1024 } else if (td->o.norandommap) 1025 continue; 1026 1027 if (!td->o.softrandommap) { 1028 log_err("fio: failed allocating random map. If running" 1029 " a large number of jobs, try the 'norandommap'" 1030 " option or set 'softrandommap'. Or give" 1031 " a larger --alloc-size to fio.\n"); 1032 return 1; 1033 } 1034 1035 log_info("fio: file %s failed allocating random map. Running " 1036 "job without.\n", f->file_name); 1037 } 1038 1039 return 0; 1040} 1041 1042void close_files(struct thread_data *td) 1043{ 1044 struct fio_file *f; 1045 unsigned int i; 1046 1047 for_each_file(td, f, i) { 1048 if (fio_file_open(f)) 1049 td_io_close_file(td, f); 1050 } 1051} 1052 1053void close_and_free_files(struct thread_data *td) 1054{ 1055 struct fio_file *f; 1056 unsigned int i; 1057 1058 dprint(FD_FILE, "close files\n"); 1059 1060 for_each_file(td, f, i) { 1061 if (fio_file_open(f)) 1062 td_io_close_file(td, f); 1063 1064 remove_file_hash(f); 1065 1066 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) { 1067 dprint(FD_FILE, "free unlink %s\n", f->file_name); 1068 unlink(f->file_name); 1069 } 1070 1071 sfree(f->file_name); 1072 f->file_name = NULL; 1073 axmap_free(f->io_axmap); 1074 f->io_axmap = NULL; 1075 sfree(f); 1076 } 1077 1078 td->o.filename = NULL; 1079 free(td->files); 1080 free(td->file_locks); 1081 td->files_index = 0; 1082 td->files = NULL; 1083 td->file_locks = NULL; 1084 td->o.file_lock_mode = FILE_LOCK_NONE; 1085 td->o.nr_files = 0; 1086} 1087 1088static void get_file_type(struct fio_file *f) 1089{ 1090 struct stat sb; 1091 1092 if (!strcmp(f->file_name, "-")) 1093 f->filetype = FIO_TYPE_PIPE; 1094 else 1095 f->filetype = FIO_TYPE_FILE; 1096 1097 /* \\.\ is the device namespace in Windows, where every file is 1098 * a block device */ 1099 if (strncmp(f->file_name, "\\\\.\\", 4) == 0) 1100 f->filetype = FIO_TYPE_BD; 1101 1102 if (!stat(f->file_name, &sb)) { 1103 if (S_ISBLK(sb.st_mode)) 1104 f->filetype = FIO_TYPE_BD; 1105 else if (S_ISCHR(sb.st_mode)) 1106 f->filetype = FIO_TYPE_CHAR; 1107 else if (S_ISFIFO(sb.st_mode)) 1108 f->filetype = FIO_TYPE_PIPE; 1109 } 1110} 1111 1112static int __is_already_allocated(const char *fname) 1113{ 1114 struct flist_head *entry; 1115 char *filename; 1116 1117 if (flist_empty(&filename_list)) 1118 return 0; 1119 1120 flist_for_each(entry, &filename_list) { 1121 filename = flist_entry(entry, struct file_name, list)->filename; 1122 1123 if (strcmp(filename, fname) == 0) 1124 return 1; 1125 } 1126 1127 return 0; 1128} 1129 1130static int is_already_allocated(const char *fname) 1131{ 1132 int ret; 1133 1134 fio_file_hash_lock(); 1135 ret = __is_already_allocated(fname); 1136 fio_file_hash_unlock(); 1137 return ret; 1138} 1139 1140static void set_already_allocated(const char *fname) 1141{ 1142 struct file_name *fn; 1143 1144 fn = malloc(sizeof(struct file_name)); 1145 fn->filename = strdup(fname); 1146 1147 fio_file_hash_lock(); 1148 if (!__is_already_allocated(fname)) { 1149 flist_add_tail(&fn->list, &filename_list); 1150 fn = NULL; 1151 } 1152 fio_file_hash_unlock(); 1153 1154 if (fn) { 1155 free(fn->filename); 1156 free(fn); 1157 } 1158} 1159 1160 1161static void free_already_allocated(void) 1162{ 1163 struct flist_head *entry, *tmp; 1164 struct file_name *fn; 1165 1166 if (flist_empty(&filename_list)) 1167 return; 1168 1169 fio_file_hash_lock(); 1170 flist_for_each_safe(entry, tmp, &filename_list) { 1171 fn = flist_entry(entry, struct file_name, list); 1172 free(fn->filename); 1173 flist_del(&fn->list); 1174 free(fn); 1175 } 1176 1177 fio_file_hash_unlock(); 1178} 1179 1180int add_file(struct thread_data *td, const char *fname, int numjob, int inc) 1181{ 1182 int cur_files = td->files_index; 1183 char file_name[PATH_MAX]; 1184 struct fio_file *f; 1185 int len = 0; 1186 1187 dprint(FD_FILE, "add file %s\n", fname); 1188 1189 if (td->o.directory) 1190 len = set_name_idx(file_name, td->o.directory, numjob); 1191 1192 sprintf(file_name + len, "%s", fname); 1193 1194 /* clean cloned siblings using existing files */ 1195 if (numjob && is_already_allocated(file_name)) 1196 return 0; 1197 1198 f = smalloc(sizeof(*f)); 1199 if (!f) { 1200 log_err("fio: smalloc OOM\n"); 1201 assert(0); 1202 } 1203 1204 f->fd = -1; 1205 f->shadow_fd = -1; 1206 fio_file_reset(td, f); 1207 1208 if (td->files_size <= td->files_index) { 1209 unsigned int new_size = td->o.nr_files + 1; 1210 1211 dprint(FD_FILE, "resize file array to %d files\n", new_size); 1212 1213 td->files = realloc(td->files, new_size * sizeof(f)); 1214 if (td->files == NULL) { 1215 log_err("fio: realloc OOM\n"); 1216 assert(0); 1217 } 1218 if (td->o.file_lock_mode != FILE_LOCK_NONE) { 1219 td->file_locks = realloc(td->file_locks, new_size); 1220 if (!td->file_locks) { 1221 log_err("fio: realloc OOM\n"); 1222 assert(0); 1223 } 1224 td->file_locks[cur_files] = FILE_LOCK_NONE; 1225 } 1226 td->files_size = new_size; 1227 } 1228 td->files[cur_files] = f; 1229 f->fileno = cur_files; 1230 1231 /* 1232 * init function, io engine may not be loaded yet 1233 */ 1234 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO)) 1235 f->real_file_size = -1ULL; 1236 1237 f->file_name = smalloc_strdup(file_name); 1238 if (!f->file_name) { 1239 log_err("fio: smalloc OOM\n"); 1240 assert(0); 1241 } 1242 1243 get_file_type(f); 1244 1245 switch (td->o.file_lock_mode) { 1246 case FILE_LOCK_NONE: 1247 break; 1248 case FILE_LOCK_READWRITE: 1249 f->rwlock = fio_rwlock_init(); 1250 break; 1251 case FILE_LOCK_EXCLUSIVE: 1252 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED); 1253 break; 1254 default: 1255 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode); 1256 assert(0); 1257 } 1258 1259 td->files_index++; 1260 if (f->filetype == FIO_TYPE_FILE) 1261 td->nr_normal_files++; 1262 1263 set_already_allocated(file_name); 1264 1265 /* 1266 * For adding files after the fact - if openfiles= isn't 1267 * given as an option, ensure we allow at least one file open 1268 */ 1269 if (!td->o.open_files) 1270 td->o.open_files = 1; 1271 1272 if (inc) 1273 td->o.nr_files++; 1274 1275 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name, 1276 cur_files); 1277 1278 return cur_files; 1279} 1280 1281int add_file_exclusive(struct thread_data *td, const char *fname) 1282{ 1283 struct fio_file *f; 1284 unsigned int i; 1285 1286 for_each_file(td, f, i) { 1287 if (!strcmp(f->file_name, fname)) 1288 return i; 1289 } 1290 1291 return add_file(td, fname, 0, 1); 1292} 1293 1294void get_file(struct fio_file *f) 1295{ 1296 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references); 1297 assert(fio_file_open(f)); 1298 f->references++; 1299} 1300 1301int put_file(struct thread_data *td, struct fio_file *f) 1302{ 1303 int f_ret = 0, ret = 0; 1304 1305 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references); 1306 1307 if (!fio_file_open(f)) { 1308 assert(f->fd == -1); 1309 return 0; 1310 } 1311 1312 assert(f->references); 1313 if (--f->references) 1314 return 0; 1315 1316 if (should_fsync(td) && td->o.fsync_on_close) 1317 f_ret = fsync(f->fd); 1318 1319 if (td->io_ops->close_file) 1320 ret = td->io_ops->close_file(td, f); 1321 1322 if (!ret) 1323 ret = f_ret; 1324 1325 td->nr_open_files--; 1326 fio_file_clear_open(f); 1327 assert(f->fd == -1); 1328 return ret; 1329} 1330 1331void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir) 1332{ 1333 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE) 1334 return; 1335 1336 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) { 1337 if (ddir == DDIR_READ) 1338 fio_rwlock_read(f->rwlock); 1339 else 1340 fio_rwlock_write(f->rwlock); 1341 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) 1342 fio_mutex_down(f->lock); 1343 1344 td->file_locks[f->fileno] = td->o.file_lock_mode; 1345} 1346 1347void unlock_file(struct thread_data *td, struct fio_file *f) 1348{ 1349 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE) 1350 return; 1351 1352 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) 1353 fio_rwlock_unlock(f->rwlock); 1354 else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) 1355 fio_mutex_up(f->lock); 1356 1357 td->file_locks[f->fileno] = FILE_LOCK_NONE; 1358} 1359 1360void unlock_file_all(struct thread_data *td, struct fio_file *f) 1361{ 1362 if (td->o.file_lock_mode == FILE_LOCK_NONE || !td->file_locks) 1363 return; 1364 if (td->file_locks[f->fileno] != FILE_LOCK_NONE) 1365 unlock_file(td, f); 1366} 1367 1368static int recurse_dir(struct thread_data *td, const char *dirname) 1369{ 1370 struct dirent *dir; 1371 int ret = 0; 1372 DIR *D; 1373 1374 D = opendir(dirname); 1375 if (!D) { 1376 char buf[FIO_VERROR_SIZE]; 1377 1378 snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname); 1379 td_verror(td, errno, buf); 1380 return 1; 1381 } 1382 1383 while ((dir = readdir(D)) != NULL) { 1384 char full_path[PATH_MAX]; 1385 struct stat sb; 1386 1387 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) 1388 continue; 1389 1390 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name); 1391 1392 if (lstat(full_path, &sb) == -1) { 1393 if (errno != ENOENT) { 1394 td_verror(td, errno, "stat"); 1395 return 1; 1396 } 1397 } 1398 1399 if (S_ISREG(sb.st_mode)) { 1400 add_file(td, full_path, 0, 1); 1401 continue; 1402 } 1403 if (!S_ISDIR(sb.st_mode)) 1404 continue; 1405 1406 ret = recurse_dir(td, full_path); 1407 if (ret) 1408 break; 1409 } 1410 1411 closedir(D); 1412 return ret; 1413} 1414 1415int add_dir_files(struct thread_data *td, const char *path) 1416{ 1417 int ret = recurse_dir(td, path); 1418 1419 if (!ret) 1420 log_info("fio: opendir added %d files\n", td->o.nr_files); 1421 1422 return ret; 1423} 1424 1425void dup_files(struct thread_data *td, struct thread_data *org) 1426{ 1427 struct fio_file *f; 1428 unsigned int i; 1429 1430 dprint(FD_FILE, "dup files: %d\n", org->files_index); 1431 1432 if (!org->files) 1433 return; 1434 1435 td->files = malloc(org->files_index * sizeof(f)); 1436 1437 if (td->o.file_lock_mode != FILE_LOCK_NONE) 1438 td->file_locks = malloc(org->files_index); 1439 1440 for_each_file(org, f, i) { 1441 struct fio_file *__f; 1442 1443 __f = smalloc(sizeof(*__f)); 1444 if (!__f) { 1445 log_err("fio: smalloc OOM\n"); 1446 assert(0); 1447 } 1448 __f->fd = -1; 1449 __f->shadow_fd = -1; 1450 fio_file_reset(td, __f); 1451 1452 if (f->file_name) { 1453 __f->file_name = smalloc_strdup(f->file_name); 1454 if (!__f->file_name) { 1455 log_err("fio: smalloc OOM\n"); 1456 assert(0); 1457 } 1458 1459 __f->filetype = f->filetype; 1460 } 1461 1462 if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) 1463 __f->lock = f->lock; 1464 else if (td->o.file_lock_mode == FILE_LOCK_READWRITE) 1465 __f->rwlock = f->rwlock; 1466 1467 td->files[i] = __f; 1468 } 1469} 1470 1471/* 1472 * Returns the index that matches the filename, or -1 if not there 1473 */ 1474int get_fileno(struct thread_data *td, const char *fname) 1475{ 1476 struct fio_file *f; 1477 unsigned int i; 1478 1479 for_each_file(td, f, i) 1480 if (!strcmp(f->file_name, fname)) 1481 return i; 1482 1483 return -1; 1484} 1485 1486/* 1487 * For log usage, where we add/open/close files automatically 1488 */ 1489void free_release_files(struct thread_data *td) 1490{ 1491 close_files(td); 1492 td->files_index = 0; 1493 td->nr_normal_files = 0; 1494} 1495 1496void fio_file_reset(struct thread_data *td, struct fio_file *f) 1497{ 1498 f->last_pos = f->file_offset; 1499 f->last_start = -1ULL; 1500 if (f->io_axmap) 1501 axmap_reset(f->io_axmap); 1502 if (td->o.random_generator == FIO_RAND_GEN_LFSR) 1503 lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]); 1504} 1505 1506int fio_files_done(struct thread_data *td) 1507{ 1508 struct fio_file *f; 1509 unsigned int i; 1510 1511 for_each_file(td, f, i) 1512 if (!fio_file_done(f)) 1513 return 0; 1514 1515 return 1; 1516} 1517 1518/* free memory used in initialization phase only */ 1519void filesetup_mem_free(void) 1520{ 1521 free_already_allocated(); 1522} 1523