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