1 // SPDX-License-Identifier: CDDL-1.0 2 /* 3 * This file and its contents are supplied under the terms of the 4 * Common Development and Distribution License ("CDDL"), version 1.0. 5 * You may only use this file in accordance with the terms of version 6 * 1.0 of the CDDL. 7 * 8 * A full copy of the text of the CDDL should have accompanied this 9 * source. A copy of the CDDL is also available via the Internet at 10 * https://opensource.org/license/CDDL-1.0. 11 */ 12 /* 13 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 14 * Copyright (c) 2012, 2015 by Delphix. All rights reserved. 15 * Copyright (c) 2017, Intel Corporation. 16 * Copyright (c) 2024-2025, Klara, Inc. 17 */ 18 19 /* 20 * ZFS fault injection 21 * 22 * To handle fault injection, we keep track of a series of zinject_record_t 23 * structures which describe which logical block(s) should be injected with a 24 * fault. These are kept in a global list. Each record corresponds to a given 25 * spa_t and maintains a special hold on the spa_t so that it cannot be deleted 26 * or exported while the injection record exists. 27 * 28 * Device level injection is done using the 'zi_guid' field. If this is set, it 29 * means that the error is destined for a particular device, not a piece of 30 * data. 31 * 32 * This is a rather poor data structure and algorithm, but we don't expect more 33 * than a few faults at any one time, so it should be sufficient for our needs. 34 */ 35 36 #include <sys/arc.h> 37 #include <sys/zio.h> 38 #include <sys/zfs_ioctl.h> 39 #include <sys/vdev_impl.h> 40 #include <sys/dmu_objset.h> 41 #include <sys/dsl_dataset.h> 42 #include <sys/fs/zfs.h> 43 44 uint32_t zio_injection_enabled = 0; 45 46 /* 47 * Data describing each zinject handler registered on the system, and 48 * contains the list node linking the handler in the global zinject 49 * handler list. 50 */ 51 typedef struct inject_handler { 52 int zi_id; 53 spa_t *zi_spa; 54 char *zi_spa_name; /* ZINJECT_DELAY_IMPORT only */ 55 zinject_record_t zi_record; 56 uint64_t *zi_lanes; 57 int zi_next_lane; 58 list_node_t zi_link; 59 } inject_handler_t; 60 61 /* 62 * List of all zinject handlers registered on the system, protected by 63 * the inject_lock defined below. 64 */ 65 static list_t inject_handlers; 66 67 /* 68 * This protects insertion into, and traversal of, the inject handler 69 * list defined above; as well as the inject_delay_count. Any time a 70 * handler is inserted or removed from the list, this lock should be 71 * taken as a RW_WRITER; and any time traversal is done over the list 72 * (without modification to it) this lock should be taken as a RW_READER. 73 */ 74 static krwlock_t inject_lock; 75 76 /* 77 * This holds the number of zinject delay handlers that have been 78 * registered on the system. It is protected by the inject_lock defined 79 * above. Thus modifications to this count must be a RW_WRITER of the 80 * inject_lock, and reads of this count must be (at least) a RW_READER 81 * of the lock. 82 */ 83 static int inject_delay_count = 0; 84 85 /* 86 * This lock is used only in zio_handle_io_delay(), refer to the comment 87 * in that function for more details. 88 */ 89 static kmutex_t inject_delay_mtx; 90 91 /* 92 * Used to assign unique identifying numbers to each new zinject handler. 93 */ 94 static int inject_next_id = 1; 95 96 /* 97 * Test if the requested frequency was triggered 98 */ 99 static boolean_t 100 freq_triggered(uint32_t frequency) 101 { 102 /* 103 * zero implies always (100%) 104 */ 105 if (frequency == 0) 106 return (B_TRUE); 107 108 /* 109 * Note: we still handle legacy (unscaled) frequency values 110 */ 111 uint32_t maximum = (frequency <= 100) ? 100 : ZI_PERCENTAGE_MAX; 112 113 return (random_in_range(maximum) < frequency); 114 } 115 116 /* 117 * Returns true if the given record matches the I/O in progress. 118 */ 119 static boolean_t 120 zio_match_handler(const zbookmark_phys_t *zb, uint64_t type, int dva, 121 zinject_record_t *record, int error) 122 { 123 boolean_t matched = B_FALSE; 124 boolean_t injected = B_FALSE; 125 126 /* 127 * Check for a match against the MOS, which is based on type 128 */ 129 if (zb->zb_objset == DMU_META_OBJSET && 130 record->zi_objset == DMU_META_OBJSET && 131 record->zi_object == DMU_META_DNODE_OBJECT) { 132 if (record->zi_type == DMU_OT_NONE || 133 type == record->zi_type) 134 matched = B_TRUE; 135 goto done; 136 } 137 138 /* 139 * Check for an exact match. 140 */ 141 if (zb->zb_objset == record->zi_objset && 142 zb->zb_object == record->zi_object && 143 zb->zb_level == record->zi_level && 144 zb->zb_blkid >= record->zi_start && 145 zb->zb_blkid <= record->zi_end && 146 (record->zi_dvas == 0 || 147 (dva != ZI_NO_DVA && (record->zi_dvas & (1ULL << dva)))) && 148 error == record->zi_error) { 149 matched = B_TRUE; 150 goto done; 151 } 152 153 done: 154 if (matched) { 155 record->zi_match_count++; 156 injected = freq_triggered(record->zi_freq); 157 } 158 159 if (injected) 160 record->zi_inject_count++; 161 162 return (injected); 163 } 164 165 /* 166 * Panic the system when a config change happens in the function 167 * specified by tag. 168 */ 169 void 170 zio_handle_panic_injection(spa_t *spa, const char *tag, uint64_t type) 171 { 172 inject_handler_t *handler; 173 174 rw_enter(&inject_lock, RW_READER); 175 176 for (handler = list_head(&inject_handlers); handler != NULL; 177 handler = list_next(&inject_handlers, handler)) { 178 179 if (spa != handler->zi_spa) 180 continue; 181 182 if (handler->zi_record.zi_type == type && 183 strcmp(tag, handler->zi_record.zi_func) == 0) { 184 handler->zi_record.zi_match_count++; 185 handler->zi_record.zi_inject_count++; 186 panic("Panic requested in function %s\n", tag); 187 } 188 } 189 190 rw_exit(&inject_lock); 191 } 192 193 /* 194 * Inject a decryption failure. Decryption failures can occur in 195 * both the ARC and the ZIO layers. 196 */ 197 int 198 zio_handle_decrypt_injection(spa_t *spa, const zbookmark_phys_t *zb, 199 uint64_t type, int error) 200 { 201 int ret = 0; 202 inject_handler_t *handler; 203 204 rw_enter(&inject_lock, RW_READER); 205 206 for (handler = list_head(&inject_handlers); handler != NULL; 207 handler = list_next(&inject_handlers, handler)) { 208 209 if (spa != handler->zi_spa || 210 handler->zi_record.zi_cmd != ZINJECT_DECRYPT_FAULT) 211 continue; 212 213 if (zio_match_handler(zb, type, ZI_NO_DVA, 214 &handler->zi_record, error)) { 215 ret = error; 216 break; 217 } 218 } 219 220 rw_exit(&inject_lock); 221 return (ret); 222 } 223 224 /* 225 * If this is a physical I/O for a vdev child determine which DVA it is 226 * for. We iterate backwards through the DVAs matching on the offset so 227 * that we end up with ZI_NO_DVA (-1) if we don't find a match. 228 */ 229 static int 230 zio_match_dva(zio_t *zio) 231 { 232 int i = ZI_NO_DVA; 233 234 if (zio->io_bp != NULL && zio->io_vd != NULL && 235 zio->io_child_type == ZIO_CHILD_VDEV) { 236 for (i = BP_GET_NDVAS(zio->io_bp) - 1; i >= 0; i--) { 237 dva_t *dva = &zio->io_bp->blk_dva[i]; 238 uint64_t off = DVA_GET_OFFSET(dva); 239 vdev_t *vd = vdev_lookup_top(zio->io_spa, 240 DVA_GET_VDEV(dva)); 241 242 /* Compensate for vdev label added to leaves */ 243 if (zio->io_vd->vdev_ops->vdev_op_leaf) 244 off += VDEV_LABEL_START_SIZE; 245 246 if (zio->io_vd == vd && zio->io_offset == off) 247 break; 248 } 249 } 250 251 return (i); 252 } 253 254 255 /* 256 * Determine if the I/O in question should return failure. Returns the errno 257 * to be returned to the caller. 258 */ 259 int 260 zio_handle_fault_injection(zio_t *zio, int error) 261 { 262 int ret = 0; 263 inject_handler_t *handler; 264 265 /* 266 * Ignore I/O not associated with any logical data. 267 */ 268 if (zio->io_logical == NULL) 269 return (0); 270 271 /* 272 * Currently, we only support fault injection on reads. 273 */ 274 if (zio->io_type != ZIO_TYPE_READ) 275 return (0); 276 277 /* 278 * A rebuild I/O has no checksum to verify. 279 */ 280 if (zio->io_priority == ZIO_PRIORITY_REBUILD && error == ECKSUM) 281 return (0); 282 283 rw_enter(&inject_lock, RW_READER); 284 285 for (handler = list_head(&inject_handlers); handler != NULL; 286 handler = list_next(&inject_handlers, handler)) { 287 if (zio->io_spa != handler->zi_spa || 288 handler->zi_record.zi_cmd != ZINJECT_DATA_FAULT) 289 continue; 290 291 /* If this handler matches, return the specified error */ 292 if (zio_match_handler(&zio->io_logical->io_bookmark, 293 zio->io_bp ? BP_GET_TYPE(zio->io_bp) : DMU_OT_NONE, 294 zio_match_dva(zio), &handler->zi_record, error)) { 295 ret = error; 296 break; 297 } 298 } 299 300 rw_exit(&inject_lock); 301 302 return (ret); 303 } 304 305 /* 306 * Determine if the zio is part of a label update and has an injection 307 * handler associated with that portion of the label. Currently, we 308 * allow error injection in either the nvlist or the uberblock region of 309 * of the vdev label. 310 */ 311 int 312 zio_handle_label_injection(zio_t *zio, int error) 313 { 314 inject_handler_t *handler; 315 vdev_t *vd = zio->io_vd; 316 uint64_t offset = zio->io_offset; 317 int label; 318 int ret = 0; 319 320 if (offset >= VDEV_LABEL_START_SIZE && 321 offset < vd->vdev_psize - VDEV_LABEL_END_SIZE) 322 return (0); 323 324 rw_enter(&inject_lock, RW_READER); 325 326 for (handler = list_head(&inject_handlers); handler != NULL; 327 handler = list_next(&inject_handlers, handler)) { 328 uint64_t start = handler->zi_record.zi_start; 329 uint64_t end = handler->zi_record.zi_end; 330 331 if (handler->zi_record.zi_cmd != ZINJECT_LABEL_FAULT) 332 continue; 333 334 /* 335 * The injection region is the relative offsets within a 336 * vdev label. We must determine the label which is being 337 * updated and adjust our region accordingly. 338 */ 339 label = vdev_label_number(vd->vdev_psize, offset); 340 start = vdev_label_offset(vd->vdev_psize, label, start); 341 end = vdev_label_offset(vd->vdev_psize, label, end); 342 343 if (zio->io_vd->vdev_guid == handler->zi_record.zi_guid && 344 (offset >= start && offset <= end)) { 345 handler->zi_record.zi_match_count++; 346 handler->zi_record.zi_inject_count++; 347 ret = error; 348 break; 349 } 350 } 351 rw_exit(&inject_lock); 352 return (ret); 353 } 354 355 static int 356 zio_inject_bitflip_cb(void *data, size_t len, void *private) 357 { 358 zio_t *zio = private; 359 uint8_t *buffer = data; 360 uint_t byte = random_in_range(len); 361 362 ASSERT3U(zio->io_type, ==, ZIO_TYPE_READ); 363 364 /* flip a single random bit in an abd data buffer */ 365 buffer[byte] ^= 1 << random_in_range(8); 366 367 return (1); /* stop after first flip */ 368 } 369 370 /* Test if this zio matches the iotype from the injection record. */ 371 static boolean_t 372 zio_match_iotype(zio_t *zio, uint32_t iotype) 373 { 374 ASSERT3P(zio, !=, NULL); 375 376 /* Unknown iotype, maybe from a newer version of zinject. Reject it. */ 377 if (iotype >= ZINJECT_IOTYPES) 378 return (B_FALSE); 379 380 /* Probe IOs only match IOTYPE_PROBE, regardless of their type. */ 381 if (zio->io_flags & ZIO_FLAG_PROBE) 382 return (iotype == ZINJECT_IOTYPE_PROBE); 383 384 /* Standard IO types, match against ZIO type. */ 385 if (iotype < ZINJECT_IOTYPE_ALL) 386 return (iotype == zio->io_type); 387 388 /* Match any standard IO type. */ 389 if (iotype == ZINJECT_IOTYPE_ALL) 390 return (B_TRUE); 391 392 return (B_FALSE); 393 } 394 395 static int 396 zio_handle_device_injection_impl(vdev_t *vd, zio_t *zio, int err1, int err2) 397 { 398 inject_handler_t *handler; 399 int ret = 0; 400 401 /* 402 * We skip over faults in the labels unless it's during device open 403 * (i.e. zio == NULL) or a device flush (offset is meaningless). We let 404 * probe IOs through so we can match them to probe inject records. 405 */ 406 if (zio != NULL && zio->io_type != ZIO_TYPE_FLUSH && 407 !(zio->io_flags & ZIO_FLAG_PROBE)) { 408 uint64_t offset = zio->io_offset; 409 410 if (offset < VDEV_LABEL_START_SIZE || 411 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE) 412 return (0); 413 } 414 415 rw_enter(&inject_lock, RW_READER); 416 417 for (handler = list_head(&inject_handlers); handler != NULL; 418 handler = list_next(&inject_handlers, handler)) { 419 420 if (handler->zi_record.zi_cmd != ZINJECT_DEVICE_FAULT) 421 continue; 422 423 if (vd->vdev_guid == handler->zi_record.zi_guid) { 424 if (handler->zi_record.zi_failfast && 425 (zio == NULL || (zio->io_flags & 426 (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD)))) { 427 continue; 428 } 429 430 /* Handle type specific I/O failures */ 431 if (zio != NULL && !zio_match_iotype(zio, 432 handler->zi_record.zi_iotype)) 433 continue; 434 435 if (handler->zi_record.zi_error == err1 || 436 handler->zi_record.zi_error == err2) { 437 handler->zi_record.zi_match_count++; 438 439 /* 440 * limit error injection if requested 441 */ 442 if (!freq_triggered(handler->zi_record.zi_freq)) 443 continue; 444 445 handler->zi_record.zi_inject_count++; 446 447 /* 448 * For a failed open, pretend like the device 449 * has gone away. 450 */ 451 if (err1 == ENXIO) 452 vd->vdev_stat.vs_aux = 453 VDEV_AUX_OPEN_FAILED; 454 455 /* 456 * Treat these errors as if they had been 457 * retried so that all the appropriate stats 458 * and FMA events are generated. 459 */ 460 if (!handler->zi_record.zi_failfast && 461 zio != NULL) 462 zio->io_flags |= ZIO_FLAG_IO_RETRY; 463 464 /* 465 * EILSEQ means flip a bit after a read 466 */ 467 if (handler->zi_record.zi_error == EILSEQ) { 468 if (zio == NULL) 469 break; 470 471 /* locate buffer data and flip a bit */ 472 (void) abd_iterate_func(zio->io_abd, 0, 473 zio->io_size, zio_inject_bitflip_cb, 474 zio); 475 break; 476 } 477 478 ret = handler->zi_record.zi_error; 479 break; 480 } 481 if (handler->zi_record.zi_error == ENXIO) { 482 handler->zi_record.zi_match_count++; 483 handler->zi_record.zi_inject_count++; 484 ret = SET_ERROR(EIO); 485 break; 486 } 487 } 488 } 489 490 rw_exit(&inject_lock); 491 492 return (ret); 493 } 494 495 int 496 zio_handle_device_injection(vdev_t *vd, zio_t *zio, int error) 497 { 498 return (zio_handle_device_injection_impl(vd, zio, error, INT_MAX)); 499 } 500 501 int 502 zio_handle_device_injections(vdev_t *vd, zio_t *zio, int err1, int err2) 503 { 504 return (zio_handle_device_injection_impl(vd, zio, err1, err2)); 505 } 506 507 /* 508 * Simulate hardware that ignores cache flushes. For requested number 509 * of seconds nix the actual writing to disk. 510 */ 511 void 512 zio_handle_ignored_writes(zio_t *zio) 513 { 514 inject_handler_t *handler; 515 516 rw_enter(&inject_lock, RW_READER); 517 518 for (handler = list_head(&inject_handlers); handler != NULL; 519 handler = list_next(&inject_handlers, handler)) { 520 521 /* Ignore errors not destined for this pool */ 522 if (zio->io_spa != handler->zi_spa || 523 handler->zi_record.zi_cmd != ZINJECT_IGNORED_WRITES) 524 continue; 525 526 handler->zi_record.zi_match_count++; 527 528 /* 529 * Positive duration implies # of seconds, negative 530 * a number of txgs 531 */ 532 if (handler->zi_record.zi_timer == 0) { 533 if (handler->zi_record.zi_duration > 0) 534 handler->zi_record.zi_timer = ddi_get_lbolt64(); 535 else 536 handler->zi_record.zi_timer = zio->io_txg; 537 } 538 539 /* Have a "problem" writing 60% of the time */ 540 if (random_in_range(100) < 60) { 541 handler->zi_record.zi_inject_count++; 542 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES; 543 } 544 break; 545 } 546 547 rw_exit(&inject_lock); 548 } 549 550 void 551 spa_handle_ignored_writes(spa_t *spa) 552 { 553 inject_handler_t *handler; 554 555 if (zio_injection_enabled == 0) 556 return; 557 558 rw_enter(&inject_lock, RW_READER); 559 560 for (handler = list_head(&inject_handlers); handler != NULL; 561 handler = list_next(&inject_handlers, handler)) { 562 563 if (spa != handler->zi_spa || 564 handler->zi_record.zi_cmd != ZINJECT_IGNORED_WRITES) 565 continue; 566 567 handler->zi_record.zi_match_count++; 568 handler->zi_record.zi_inject_count++; 569 570 if (handler->zi_record.zi_duration > 0) { 571 VERIFY(handler->zi_record.zi_timer == 0 || 572 ddi_time_after64( 573 (int64_t)handler->zi_record.zi_timer + 574 handler->zi_record.zi_duration * hz, 575 ddi_get_lbolt64())); 576 } else { 577 /* duration is negative so the subtraction here adds */ 578 VERIFY(handler->zi_record.zi_timer == 0 || 579 handler->zi_record.zi_timer - 580 handler->zi_record.zi_duration >= 581 spa_syncing_txg(spa)); 582 } 583 } 584 585 rw_exit(&inject_lock); 586 } 587 588 hrtime_t 589 zio_handle_io_delay(zio_t *zio) 590 { 591 vdev_t *vd = zio->io_vd; 592 inject_handler_t *min_handler = NULL; 593 hrtime_t min_target = 0; 594 595 rw_enter(&inject_lock, RW_READER); 596 597 /* 598 * inject_delay_count is a subset of zio_injection_enabled that 599 * is only incremented for delay handlers. These checks are 600 * mainly added to remind the reader why we're not explicitly 601 * checking zio_injection_enabled like the other functions. 602 */ 603 IMPLY(inject_delay_count > 0, zio_injection_enabled > 0); 604 IMPLY(zio_injection_enabled == 0, inject_delay_count == 0); 605 606 /* 607 * If there aren't any inject delay handlers registered, then we 608 * can short circuit and simply return 0 here. A value of zero 609 * informs zio_delay_interrupt() that this request should not be 610 * delayed. This short circuit keeps us from acquiring the 611 * inject_delay_mutex unnecessarily. 612 */ 613 if (inject_delay_count == 0) { 614 rw_exit(&inject_lock); 615 return (0); 616 } 617 618 /* 619 * Each inject handler has a number of "lanes" associated with 620 * it. Each lane is able to handle requests independently of one 621 * another, and at a latency defined by the inject handler 622 * record's zi_timer field. Thus if a handler in configured with 623 * a single lane with a 10ms latency, it will delay requests 624 * such that only a single request is completed every 10ms. So, 625 * if more than one request is attempted per each 10ms interval, 626 * the average latency of the requests will be greater than 627 * 10ms; but if only a single request is submitted each 10ms 628 * interval the average latency will be 10ms. 629 * 630 * We need to acquire this mutex to prevent multiple concurrent 631 * threads being assigned to the same lane of a given inject 632 * handler. The mutex allows us to perform the following two 633 * operations atomically: 634 * 635 * 1. determine the minimum handler and minimum target 636 * value of all the possible handlers 637 * 2. update that minimum handler's lane array 638 * 639 * Without atomicity, two (or more) threads could pick the same 640 * lane in step (1), and then conflict with each other in step 641 * (2). This could allow a single lane handler to process 642 * multiple requests simultaneously, which shouldn't be possible. 643 */ 644 mutex_enter(&inject_delay_mtx); 645 646 for (inject_handler_t *handler = list_head(&inject_handlers); 647 handler != NULL; handler = list_next(&inject_handlers, handler)) { 648 if (handler->zi_record.zi_cmd != ZINJECT_DELAY_IO) 649 continue; 650 651 if (vd->vdev_guid != handler->zi_record.zi_guid) 652 continue; 653 654 /* also match on I/O type (e.g., -T read) */ 655 if (!zio_match_iotype(zio, handler->zi_record.zi_iotype)) 656 continue; 657 658 /* 659 * Defensive; should never happen as the array allocation 660 * occurs prior to inserting this handler on the list. 661 */ 662 ASSERT3P(handler->zi_lanes, !=, NULL); 663 664 /* 665 * This should never happen, the zinject command should 666 * prevent a user from setting an IO delay with zero lanes. 667 */ 668 ASSERT3U(handler->zi_record.zi_nlanes, !=, 0); 669 670 ASSERT3U(handler->zi_record.zi_nlanes, >, 671 handler->zi_next_lane); 672 673 handler->zi_record.zi_match_count++; 674 675 /* Limit the use of this handler if requested */ 676 if (!freq_triggered(handler->zi_record.zi_freq)) 677 continue; 678 679 /* 680 * We want to issue this IO to the lane that will become 681 * idle the soonest, so we compare the soonest this 682 * specific handler can complete the IO with all other 683 * handlers, to find the lowest value of all possible 684 * lanes. We then use this lane to submit the request. 685 * 686 * Since each handler has a constant value for its 687 * delay, we can just use the "next" lane for that 688 * handler; as it will always be the lane with the 689 * lowest value for that particular handler (i.e. the 690 * lane that will become idle the soonest). This saves a 691 * scan of each handler's lanes array. 692 * 693 * There's two cases to consider when determining when 694 * this specific IO request should complete. If this 695 * lane is idle, we want to "submit" the request now so 696 * it will complete after zi_timer milliseconds. Thus, 697 * we set the target to now + zi_timer. 698 * 699 * If the lane is busy, we want this request to complete 700 * zi_timer milliseconds after the lane becomes idle. 701 * Since the 'zi_lanes' array holds the time at which 702 * each lane will become idle, we use that value to 703 * determine when this request should complete. 704 */ 705 hrtime_t idle = handler->zi_record.zi_timer + gethrtime(); 706 hrtime_t busy = handler->zi_record.zi_timer + 707 handler->zi_lanes[handler->zi_next_lane]; 708 hrtime_t target = MAX(idle, busy); 709 710 if (min_handler == NULL) { 711 min_handler = handler; 712 min_target = target; 713 continue; 714 } 715 716 ASSERT3P(min_handler, !=, NULL); 717 ASSERT3U(min_target, !=, 0); 718 719 /* 720 * We don't yet increment the "next lane" variable since 721 * we still might find a lower value lane in another 722 * handler during any remaining iterations. Once we're 723 * sure we've selected the absolute minimum, we'll claim 724 * the lane and increment the handler's "next lane" 725 * field below. 726 */ 727 728 if (target < min_target) { 729 min_handler = handler; 730 min_target = target; 731 } 732 } 733 734 /* 735 * 'min_handler' will be NULL if no IO delays are registered for 736 * this vdev, otherwise it will point to the handler containing 737 * the lane that will become idle the soonest. 738 */ 739 if (min_handler != NULL) { 740 ASSERT3U(min_target, !=, 0); 741 min_handler->zi_lanes[min_handler->zi_next_lane] = min_target; 742 743 /* 744 * If we've used all possible lanes for this handler, 745 * loop back and start using the first lane again; 746 * otherwise, just increment the lane index. 747 */ 748 min_handler->zi_next_lane = (min_handler->zi_next_lane + 1) % 749 min_handler->zi_record.zi_nlanes; 750 751 min_handler->zi_record.zi_inject_count++; 752 753 } 754 755 mutex_exit(&inject_delay_mtx); 756 rw_exit(&inject_lock); 757 758 return (min_target); 759 } 760 761 static void 762 zio_handle_pool_delay(spa_t *spa, hrtime_t elapsed, zinject_type_t command) 763 { 764 inject_handler_t *handler; 765 hrtime_t delay = 0; 766 int id = 0; 767 768 rw_enter(&inject_lock, RW_READER); 769 770 for (handler = list_head(&inject_handlers); 771 handler != NULL && handler->zi_record.zi_cmd == command; 772 handler = list_next(&inject_handlers, handler)) { 773 ASSERT3P(handler->zi_spa_name, !=, NULL); 774 if (strcmp(spa_name(spa), handler->zi_spa_name) == 0) { 775 handler->zi_record.zi_match_count++; 776 uint64_t pause = 777 SEC2NSEC(handler->zi_record.zi_duration); 778 if (pause > elapsed) { 779 handler->zi_record.zi_inject_count++; 780 delay = pause - elapsed; 781 } 782 id = handler->zi_id; 783 break; 784 } 785 } 786 787 rw_exit(&inject_lock); 788 789 if (delay) { 790 if (command == ZINJECT_DELAY_IMPORT) { 791 spa_import_progress_set_notes(spa, "injecting %llu " 792 "sec delay", (u_longlong_t)NSEC2SEC(delay)); 793 } 794 zfs_sleep_until(gethrtime() + delay); 795 } 796 if (id) { 797 /* all done with this one-shot handler */ 798 zio_clear_fault(id); 799 } 800 } 801 802 /* 803 * For testing, inject a delay during an import 804 */ 805 void 806 zio_handle_import_delay(spa_t *spa, hrtime_t elapsed) 807 { 808 zio_handle_pool_delay(spa, elapsed, ZINJECT_DELAY_IMPORT); 809 } 810 811 /* 812 * For testing, inject a delay during an export 813 */ 814 void 815 zio_handle_export_delay(spa_t *spa, hrtime_t elapsed) 816 { 817 zio_handle_pool_delay(spa, elapsed, ZINJECT_DELAY_EXPORT); 818 } 819 820 /* 821 * For testing, inject a delay before ready state. 822 */ 823 hrtime_t 824 zio_handle_ready_delay(zio_t *zio) 825 { 826 inject_handler_t *handler; 827 hrtime_t now = gethrtime(); 828 hrtime_t target = 0; 829 830 /* 831 * Ignore I/O not associated with any logical data. 832 */ 833 if (zio->io_logical == NULL) 834 return (0); 835 836 rw_enter(&inject_lock, RW_READER); 837 838 for (handler = list_head(&inject_handlers); handler != NULL; 839 handler = list_next(&inject_handlers, handler)) { 840 if (zio->io_spa != handler->zi_spa || 841 handler->zi_record.zi_cmd != ZINJECT_DELAY_READY) 842 continue; 843 844 /* If this handler matches, inject the delay */ 845 if (zio_match_iotype(zio, handler->zi_record.zi_iotype) && 846 zio_match_handler(&zio->io_logical->io_bookmark, 847 zio->io_bp ? BP_GET_TYPE(zio->io_bp) : DMU_OT_NONE, 848 zio_match_dva(zio), &handler->zi_record, zio->io_error)) { 849 target = now + (hrtime_t)handler->zi_record.zi_timer; 850 break; 851 } 852 } 853 854 rw_exit(&inject_lock); 855 return (target); 856 } 857 858 static int 859 zio_calculate_range(const char *pool, zinject_record_t *record) 860 { 861 dsl_pool_t *dp; 862 dsl_dataset_t *ds; 863 objset_t *os = NULL; 864 dnode_t *dn = NULL; 865 int error; 866 867 /* 868 * Obtain the dnode for object using pool, objset, and object 869 */ 870 error = dsl_pool_hold(pool, FTAG, &dp); 871 if (error) 872 return (error); 873 874 error = dsl_dataset_hold_obj(dp, record->zi_objset, FTAG, &ds); 875 dsl_pool_rele(dp, FTAG); 876 if (error) 877 return (error); 878 879 error = dmu_objset_from_ds(ds, &os); 880 dsl_dataset_rele(ds, FTAG); 881 if (error) 882 return (error); 883 884 error = dnode_hold(os, record->zi_object, FTAG, &dn); 885 if (error) 886 return (error); 887 888 /* 889 * Translate the range into block IDs 890 */ 891 if (record->zi_start != 0 || record->zi_end != -1ULL) { 892 record->zi_start >>= dn->dn_datablkshift; 893 record->zi_end >>= dn->dn_datablkshift; 894 } 895 if (record->zi_level > 0) { 896 if (record->zi_level >= dn->dn_nlevels) { 897 dnode_rele(dn, FTAG); 898 return (SET_ERROR(EDOM)); 899 } 900 901 if (record->zi_start != 0 || record->zi_end != 0) { 902 int shift = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 903 904 for (int level = record->zi_level; level > 0; level--) { 905 record->zi_start >>= shift; 906 record->zi_end >>= shift; 907 } 908 } 909 } 910 911 dnode_rele(dn, FTAG); 912 return (0); 913 } 914 915 static boolean_t 916 zio_pool_handler_exists(const char *name, zinject_type_t command) 917 { 918 boolean_t exists = B_FALSE; 919 920 rw_enter(&inject_lock, RW_READER); 921 for (inject_handler_t *handler = list_head(&inject_handlers); 922 handler != NULL; handler = list_next(&inject_handlers, handler)) { 923 if (command != handler->zi_record.zi_cmd) 924 continue; 925 926 const char *pool = (handler->zi_spa_name != NULL) ? 927 handler->zi_spa_name : spa_name(handler->zi_spa); 928 if (strcmp(name, pool) == 0) { 929 exists = B_TRUE; 930 break; 931 } 932 } 933 rw_exit(&inject_lock); 934 935 return (exists); 936 } 937 /* 938 * Create a new handler for the given record. We add it to the list, adding 939 * a reference to the spa_t in the process. We increment zio_injection_enabled, 940 * which is the switch to trigger all fault injection. 941 */ 942 int 943 zio_inject_fault(char *name, int flags, int *id, zinject_record_t *record) 944 { 945 inject_handler_t *handler; 946 int error; 947 spa_t *spa; 948 949 /* 950 * If this is pool-wide metadata, make sure we unload the corresponding 951 * spa_t, so that the next attempt to load it will trigger the fault. 952 * We call spa_reset() to unload the pool appropriately. 953 */ 954 if (flags & ZINJECT_UNLOAD_SPA) 955 if ((error = spa_reset(name)) != 0) 956 return (error); 957 958 if (record->zi_cmd == ZINJECT_DELAY_IO) { 959 /* 960 * A value of zero for the number of lanes or for the 961 * delay time doesn't make sense. 962 */ 963 if (record->zi_timer == 0 || record->zi_nlanes == 0) 964 return (SET_ERROR(EINVAL)); 965 966 /* 967 * The number of lanes is directly mapped to the size of 968 * an array used by the handler. Thus, to ensure the 969 * user doesn't trigger an allocation that's "too large" 970 * we cap the number of lanes here. 971 */ 972 if (record->zi_nlanes >= UINT16_MAX) 973 return (SET_ERROR(EINVAL)); 974 } 975 976 /* 977 * If the supplied range was in bytes -- calculate the actual blkid 978 */ 979 if (flags & ZINJECT_CALC_RANGE) { 980 error = zio_calculate_range(name, record); 981 if (error != 0) 982 return (error); 983 } 984 985 if (!(flags & ZINJECT_NULL)) { 986 /* 987 * Pool delays for import or export don't take an 988 * injection reference on the spa. Instead they 989 * rely on matching by name. 990 */ 991 if (record->zi_cmd == ZINJECT_DELAY_IMPORT || 992 record->zi_cmd == ZINJECT_DELAY_EXPORT) { 993 if (record->zi_duration <= 0) 994 return (SET_ERROR(EINVAL)); 995 /* 996 * Only one import | export delay handler per pool. 997 */ 998 if (zio_pool_handler_exists(name, record->zi_cmd)) 999 return (SET_ERROR(EEXIST)); 1000 1001 spa_namespace_enter(FTAG); 1002 boolean_t has_spa = spa_lookup(name) != NULL; 1003 spa_namespace_exit(FTAG); 1004 1005 if (record->zi_cmd == ZINJECT_DELAY_IMPORT && has_spa) 1006 return (SET_ERROR(EEXIST)); 1007 if (record->zi_cmd == ZINJECT_DELAY_EXPORT && !has_spa) 1008 return (SET_ERROR(ENOENT)); 1009 spa = NULL; 1010 } else { 1011 /* 1012 * spa_inject_ref() will add an injection reference, 1013 * which will prevent the pool from being removed 1014 * from the namespace while still allowing it to be 1015 * unloaded. 1016 */ 1017 if ((spa = spa_inject_addref(name)) == NULL) 1018 return (SET_ERROR(ENOENT)); 1019 } 1020 1021 handler = kmem_alloc(sizeof (inject_handler_t), KM_SLEEP); 1022 handler->zi_spa = spa; /* note: can be NULL */ 1023 handler->zi_record = *record; 1024 1025 if (handler->zi_record.zi_cmd == ZINJECT_DELAY_IO) { 1026 handler->zi_lanes = kmem_zalloc( 1027 sizeof (*handler->zi_lanes) * 1028 handler->zi_record.zi_nlanes, KM_SLEEP); 1029 handler->zi_next_lane = 0; 1030 } else { 1031 handler->zi_lanes = NULL; 1032 handler->zi_next_lane = 0; 1033 } 1034 1035 if (handler->zi_spa == NULL) 1036 handler->zi_spa_name = spa_strdup(name); 1037 else 1038 handler->zi_spa_name = NULL; 1039 1040 rw_enter(&inject_lock, RW_WRITER); 1041 1042 /* 1043 * We can't move this increment into the conditional 1044 * above because we need to hold the RW_WRITER lock of 1045 * inject_lock, and we don't want to hold that while 1046 * allocating the handler's zi_lanes array. 1047 */ 1048 if (handler->zi_record.zi_cmd == ZINJECT_DELAY_IO) { 1049 ASSERT3S(inject_delay_count, >=, 0); 1050 inject_delay_count++; 1051 ASSERT3S(inject_delay_count, >, 0); 1052 } 1053 1054 *id = handler->zi_id = inject_next_id++; 1055 list_insert_tail(&inject_handlers, handler); 1056 atomic_inc_32(&zio_injection_enabled); 1057 1058 rw_exit(&inject_lock); 1059 } 1060 1061 /* 1062 * Flush the ARC, so that any attempts to read this data will end up 1063 * going to the ZIO layer. Note that this is a little overkill, but 1064 * we don't have the necessary ARC interfaces to do anything else, and 1065 * fault injection isn't a performance critical path. 1066 */ 1067 if (flags & ZINJECT_FLUSH_ARC) 1068 /* 1069 * We must use FALSE to ensure arc_flush returns, since 1070 * we're not preventing concurrent ARC insertions. 1071 */ 1072 arc_flush(NULL, FALSE); 1073 1074 return (0); 1075 } 1076 1077 /* 1078 * Returns the next record with an ID greater than that supplied to the 1079 * function. Used to iterate over all handlers in the system. 1080 */ 1081 int 1082 zio_inject_list_next(int *id, char *name, size_t buflen, 1083 zinject_record_t *record) 1084 { 1085 inject_handler_t *handler; 1086 int ret; 1087 1088 spa_namespace_enter(FTAG); 1089 rw_enter(&inject_lock, RW_READER); 1090 1091 for (handler = list_head(&inject_handlers); handler != NULL; 1092 handler = list_next(&inject_handlers, handler)) 1093 if (handler->zi_id > *id) 1094 break; 1095 1096 if (handler) { 1097 *record = handler->zi_record; 1098 *id = handler->zi_id; 1099 ASSERT(handler->zi_spa || handler->zi_spa_name); 1100 if (handler->zi_spa != NULL) 1101 (void) strlcpy(name, spa_name(handler->zi_spa), buflen); 1102 else 1103 (void) strlcpy(name, handler->zi_spa_name, buflen); 1104 ret = 0; 1105 } else { 1106 ret = SET_ERROR(ENOENT); 1107 } 1108 1109 rw_exit(&inject_lock); 1110 spa_namespace_exit(FTAG); 1111 1112 return (ret); 1113 } 1114 1115 /* 1116 * Clear the fault handler with the given identifier, or return ENOENT if none 1117 * exists. 1118 */ 1119 int 1120 zio_clear_fault(int id) 1121 { 1122 inject_handler_t *handler; 1123 1124 rw_enter(&inject_lock, RW_WRITER); 1125 1126 for (handler = list_head(&inject_handlers); handler != NULL; 1127 handler = list_next(&inject_handlers, handler)) 1128 if (handler->zi_id == id) 1129 break; 1130 1131 if (handler == NULL) { 1132 rw_exit(&inject_lock); 1133 return (SET_ERROR(ENOENT)); 1134 } 1135 1136 if (handler->zi_record.zi_cmd == ZINJECT_DELAY_IO) { 1137 ASSERT3S(inject_delay_count, >, 0); 1138 inject_delay_count--; 1139 ASSERT3S(inject_delay_count, >=, 0); 1140 } 1141 1142 list_remove(&inject_handlers, handler); 1143 rw_exit(&inject_lock); 1144 1145 if (handler->zi_record.zi_cmd == ZINJECT_DELAY_IO) { 1146 ASSERT3P(handler->zi_lanes, !=, NULL); 1147 kmem_free(handler->zi_lanes, sizeof (*handler->zi_lanes) * 1148 handler->zi_record.zi_nlanes); 1149 } else { 1150 ASSERT0P(handler->zi_lanes); 1151 } 1152 1153 if (handler->zi_spa_name != NULL) 1154 spa_strfree(handler->zi_spa_name); 1155 1156 if (handler->zi_spa != NULL) 1157 spa_inject_delref(handler->zi_spa); 1158 kmem_free(handler, sizeof (inject_handler_t)); 1159 atomic_dec_32(&zio_injection_enabled); 1160 1161 return (0); 1162 } 1163 1164 void 1165 zio_inject_init(void) 1166 { 1167 rw_init(&inject_lock, NULL, RW_DEFAULT, NULL); 1168 mutex_init(&inject_delay_mtx, NULL, MUTEX_DEFAULT, NULL); 1169 list_create(&inject_handlers, sizeof (inject_handler_t), 1170 offsetof(inject_handler_t, zi_link)); 1171 } 1172 1173 void 1174 zio_inject_fini(void) 1175 { 1176 list_destroy(&inject_handlers); 1177 mutex_destroy(&inject_delay_mtx); 1178 rw_destroy(&inject_lock); 1179 } 1180 1181 EXPORT_SYMBOL(zio_injection_enabled); 1182 EXPORT_SYMBOL(zio_inject_fault); 1183 EXPORT_SYMBOL(zio_inject_list_next); 1184 EXPORT_SYMBOL(zio_clear_fault); 1185 EXPORT_SYMBOL(zio_handle_fault_injection); 1186 EXPORT_SYMBOL(zio_handle_device_injection); 1187 EXPORT_SYMBOL(zio_handle_label_injection); 1188