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