xref: /illumos-gate/usr/src/uts/common/fs/zfs/zio_inject.c (revision 12a8814c13fbb1d6d58616cf090ea5815dc107f9)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24  */
25 
26 /*
27  * ZFS fault injection
28  *
29  * To handle fault injection, we keep track of a series of zinject_record_t
30  * structures which describe which logical block(s) should be injected with a
31  * fault.  These are kept in a global list.  Each record corresponds to a given
32  * spa_t and maintains a special hold on the spa_t so that it cannot be deleted
33  * or exported while the injection record exists.
34  *
35  * Device level injection is done using the 'zi_guid' field.  If this is set, it
36  * means that the error is destined for a particular device, not a piece of
37  * data.
38  *
39  * This is a rather poor data structure and algorithm, but we don't expect more
40  * than a few faults at any one time, so it should be sufficient for our needs.
41  */
42 
43 #include <sys/arc.h>
44 #include <sys/zio_impl.h>
45 #include <sys/zfs_ioctl.h>
46 #include <sys/vdev_impl.h>
47 #include <sys/dmu_objset.h>
48 #include <sys/fs/zfs.h>
49 
50 uint32_t zio_injection_enabled;
51 
52 /*
53  * Data describing each zinject handler registered on the system, and
54  * contains the list node linking the handler in the global zinject
55  * handler list.
56  */
57 typedef struct inject_handler {
58 	int			zi_id;
59 	spa_t			*zi_spa;
60 	zinject_record_t	zi_record;
61 	uint64_t		*zi_lanes;
62 	int			zi_next_lane;
63 	list_node_t		zi_link;
64 } inject_handler_t;
65 
66 /*
67  * List of all zinject handlers registered on the system, protected by
68  * the inject_lock defined below.
69  */
70 static list_t inject_handlers;
71 
72 /*
73  * This protects insertion into, and traversal of, the inject handler
74  * list defined above; as well as the inject_delay_count. Any time a
75  * handler is inserted or removed from the list, this lock should be
76  * taken as a RW_WRITER; and any time traversal is done over the list
77  * (without modification to it) this lock should be taken as a RW_READER.
78  */
79 static krwlock_t inject_lock;
80 
81 /*
82  * This holds the number of zinject delay handlers that have been
83  * registered on the system. It is protected by the inject_lock defined
84  * above. Thus modifications to this count must be a RW_WRITER of the
85  * inject_lock, and reads of this count must be (at least) a RW_READER
86  * of the lock.
87  */
88 static int inject_delay_count = 0;
89 
90 /*
91  * This lock is used only in zio_handle_io_delay(), refer to the comment
92  * in that function for more details.
93  */
94 static kmutex_t inject_delay_mtx;
95 
96 /*
97  * Used to assign unique identifying numbers to each new zinject handler.
98  */
99 static int inject_next_id = 1;
100 
101 /*
102  * Returns true if the given record matches the I/O in progress.
103  */
104 static boolean_t
105 zio_match_handler(zbookmark_phys_t *zb, uint64_t type, int dva,
106     zinject_record_t *record, int error)
107 {
108 	/*
109 	 * Check for a match against the MOS, which is based on type
110 	 */
111 	if (zb->zb_objset == DMU_META_OBJSET &&
112 	    record->zi_objset == DMU_META_OBJSET &&
113 	    record->zi_object == DMU_META_DNODE_OBJECT) {
114 		if (record->zi_type == DMU_OT_NONE ||
115 		    type == record->zi_type)
116 			return (record->zi_freq == 0 ||
117 			    spa_get_random(100) < record->zi_freq);
118 		else
119 			return (B_FALSE);
120 	}
121 
122 	/*
123 	 * Check for an exact match.
124 	 */
125 	if (zb->zb_objset == record->zi_objset &&
126 	    zb->zb_object == record->zi_object &&
127 	    zb->zb_level == record->zi_level &&
128 	    zb->zb_blkid >= record->zi_start &&
129 	    zb->zb_blkid <= record->zi_end &&
130 	    (record->zi_dvas == 0 || (record->zi_dvas & (1ULL << dva))) &&
131 	    error == record->zi_error) {
132 		return (record->zi_freq == 0 ||
133 		    spa_get_random(100) < record->zi_freq);
134 	}
135 
136 	return (B_FALSE);
137 }
138 
139 /*
140  * Panic the system when a config change happens in the function
141  * specified by tag.
142  */
143 void
144 zio_handle_panic_injection(spa_t *spa, char *tag, uint64_t type)
145 {
146 	inject_handler_t *handler;
147 
148 	rw_enter(&inject_lock, RW_READER);
149 
150 	for (handler = list_head(&inject_handlers); handler != NULL;
151 	    handler = list_next(&inject_handlers, handler)) {
152 
153 		if (spa != handler->zi_spa)
154 			continue;
155 
156 		if (handler->zi_record.zi_type == type &&
157 		    strcmp(tag, handler->zi_record.zi_func) == 0)
158 			panic("Panic requested in function %s\n", tag);
159 	}
160 
161 	rw_exit(&inject_lock);
162 }
163 
164 
165 /*
166  * If this is a physical I/O for a vdev child determine which DVA it is
167  * for. We iterate backwards through the DVAs matching on the offset so
168  * that we end up with ZI_NO_DVA (-1) if we don't find a match.
169  */
170 static int
171 zio_match_dva(zio_t *zio)
172 {
173 	int i = ZI_NO_DVA;
174 
175 	if (zio->io_bp != NULL && zio->io_vd != NULL &&
176 	    zio->io_child_type == ZIO_CHILD_VDEV) {
177 		for (i = BP_GET_NDVAS(zio->io_bp) - 1; i >= 0; i--) {
178 			dva_t *dva = &zio->io_bp->blk_dva[i];
179 			uint64_t off = DVA_GET_OFFSET(dva);
180 			vdev_t *vd = vdev_lookup_top(zio->io_spa,
181 			    DVA_GET_VDEV(dva));
182 
183 			/* Compensate for vdev label added to leaves */
184 			if (zio->io_vd->vdev_ops->vdev_op_leaf)
185 				off += VDEV_LABEL_START_SIZE;
186 
187 			if (zio->io_vd == vd && zio->io_offset == off)
188 				break;
189 		}
190 	}
191 
192 	return (i);
193 }
194 
195 
196 /*
197  * Determine if the I/O in question should return failure.  Returns the errno
198  * to be returned to the caller.
199  */
200 int
201 zio_handle_fault_injection(zio_t *zio, int error)
202 {
203 	int ret = 0;
204 	inject_handler_t *handler;
205 
206 	/*
207 	 * Ignore I/O not associated with any logical data.
208 	 */
209 	if (zio->io_logical == NULL)
210 		return (0);
211 
212 	/*
213 	 * Currently, we only support fault injection on reads.
214 	 */
215 	if (zio->io_type != ZIO_TYPE_READ)
216 		return (0);
217 
218 	rw_enter(&inject_lock, RW_READER);
219 
220 	for (handler = list_head(&inject_handlers); handler != NULL;
221 	    handler = list_next(&inject_handlers, handler)) {
222 
223 		if (zio->io_spa != handler->zi_spa ||
224 		    handler->zi_record.zi_cmd != ZINJECT_DATA_FAULT)
225 			continue;
226 
227 		/* If this handler matches, return the specified error */
228 		if (zio_match_handler(&zio->io_logical->io_bookmark,
229 		    zio->io_bp ? BP_GET_TYPE(zio->io_bp) : DMU_OT_NONE,
230 		    zio_match_dva(zio), &handler->zi_record, error)) {
231 			ret = error;
232 			break;
233 		}
234 	}
235 
236 	rw_exit(&inject_lock);
237 
238 	return (ret);
239 }
240 
241 /*
242  * Determine if the zio is part of a label update and has an injection
243  * handler associated with that portion of the label. Currently, we
244  * allow error injection in either the nvlist or the uberblock region of
245  * of the vdev label.
246  */
247 int
248 zio_handle_label_injection(zio_t *zio, int error)
249 {
250 	inject_handler_t *handler;
251 	vdev_t *vd = zio->io_vd;
252 	uint64_t offset = zio->io_offset;
253 	int label;
254 	int ret = 0;
255 
256 	if (offset >= VDEV_LABEL_START_SIZE &&
257 	    offset < vd->vdev_psize - VDEV_LABEL_END_SIZE)
258 		return (0);
259 
260 	rw_enter(&inject_lock, RW_READER);
261 
262 	for (handler = list_head(&inject_handlers); handler != NULL;
263 	    handler = list_next(&inject_handlers, handler)) {
264 		uint64_t start = handler->zi_record.zi_start;
265 		uint64_t end = handler->zi_record.zi_end;
266 
267 		if (handler->zi_record.zi_cmd != ZINJECT_LABEL_FAULT)
268 			continue;
269 
270 		/*
271 		 * The injection region is the relative offsets within a
272 		 * vdev label. We must determine the label which is being
273 		 * updated and adjust our region accordingly.
274 		 */
275 		label = vdev_label_number(vd->vdev_psize, offset);
276 		start = vdev_label_offset(vd->vdev_psize, label, start);
277 		end = vdev_label_offset(vd->vdev_psize, label, end);
278 
279 		if (zio->io_vd->vdev_guid == handler->zi_record.zi_guid &&
280 		    (offset >= start && offset <= end)) {
281 			ret = error;
282 			break;
283 		}
284 	}
285 	rw_exit(&inject_lock);
286 	return (ret);
287 }
288 
289 
290 int
291 zio_handle_device_injection(vdev_t *vd, zio_t *zio, int error)
292 {
293 	inject_handler_t *handler;
294 	int ret = 0;
295 
296 	/*
297 	 * We skip over faults in the labels unless it's during
298 	 * device open (i.e. zio == NULL).
299 	 */
300 	if (zio != NULL) {
301 		uint64_t offset = zio->io_offset;
302 
303 		if (offset < VDEV_LABEL_START_SIZE ||
304 		    offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE)
305 			return (0);
306 	}
307 
308 	rw_enter(&inject_lock, RW_READER);
309 
310 	for (handler = list_head(&inject_handlers); handler != NULL;
311 	    handler = list_next(&inject_handlers, handler)) {
312 
313 		if (handler->zi_record.zi_cmd != ZINJECT_DEVICE_FAULT)
314 			continue;
315 
316 		if (vd->vdev_guid == handler->zi_record.zi_guid) {
317 			if (handler->zi_record.zi_failfast &&
318 			    (zio == NULL || (zio->io_flags &
319 			    (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD)))) {
320 				continue;
321 			}
322 
323 			/* Handle type specific I/O failures */
324 			if (zio != NULL &&
325 			    handler->zi_record.zi_iotype != ZIO_TYPES &&
326 			    handler->zi_record.zi_iotype != zio->io_type)
327 				continue;
328 
329 			if (handler->zi_record.zi_error == error) {
330 				/*
331 				 * For a failed open, pretend like the device
332 				 * has gone away.
333 				 */
334 				if (error == ENXIO)
335 					vd->vdev_stat.vs_aux =
336 					    VDEV_AUX_OPEN_FAILED;
337 
338 				/*
339 				 * Treat these errors as if they had been
340 				 * retried so that all the appropriate stats
341 				 * and FMA events are generated.
342 				 */
343 				if (!handler->zi_record.zi_failfast &&
344 				    zio != NULL)
345 					zio->io_flags |= ZIO_FLAG_IO_RETRY;
346 
347 				ret = error;
348 				break;
349 			}
350 			if (handler->zi_record.zi_error == ENXIO) {
351 				ret = SET_ERROR(EIO);
352 				break;
353 			}
354 		}
355 	}
356 
357 	rw_exit(&inject_lock);
358 
359 	return (ret);
360 }
361 
362 /*
363  * Simulate hardware that ignores cache flushes.  For requested number
364  * of seconds nix the actual writing to disk.
365  */
366 void
367 zio_handle_ignored_writes(zio_t *zio)
368 {
369 	inject_handler_t *handler;
370 
371 	rw_enter(&inject_lock, RW_READER);
372 
373 	for (handler = list_head(&inject_handlers); handler != NULL;
374 	    handler = list_next(&inject_handlers, handler)) {
375 
376 		/* Ignore errors not destined for this pool */
377 		if (zio->io_spa != handler->zi_spa ||
378 		    handler->zi_record.zi_cmd != ZINJECT_IGNORED_WRITES)
379 			continue;
380 
381 		/*
382 		 * Positive duration implies # of seconds, negative
383 		 * a number of txgs
384 		 */
385 		if (handler->zi_record.zi_timer == 0) {
386 			if (handler->zi_record.zi_duration > 0)
387 				handler->zi_record.zi_timer = ddi_get_lbolt64();
388 			else
389 				handler->zi_record.zi_timer = zio->io_txg;
390 		}
391 
392 		/* Have a "problem" writing 60% of the time */
393 		if (spa_get_random(100) < 60)
394 			zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
395 		break;
396 	}
397 
398 	rw_exit(&inject_lock);
399 }
400 
401 void
402 spa_handle_ignored_writes(spa_t *spa)
403 {
404 	inject_handler_t *handler;
405 
406 	if (zio_injection_enabled == 0)
407 		return;
408 
409 	rw_enter(&inject_lock, RW_READER);
410 
411 	for (handler = list_head(&inject_handlers); handler != NULL;
412 	    handler = list_next(&inject_handlers, handler)) {
413 
414 		if (spa != handler->zi_spa ||
415 		    handler->zi_record.zi_cmd != ZINJECT_IGNORED_WRITES)
416 			continue;
417 
418 		if (handler->zi_record.zi_duration > 0) {
419 			VERIFY(handler->zi_record.zi_timer == 0 ||
420 			    handler->zi_record.zi_timer +
421 			    handler->zi_record.zi_duration * hz >
422 			    ddi_get_lbolt64());
423 		} else {
424 			/* duration is negative so the subtraction here adds */
425 			VERIFY(handler->zi_record.zi_timer == 0 ||
426 			    handler->zi_record.zi_timer -
427 			    handler->zi_record.zi_duration >=
428 			    spa_syncing_txg(spa));
429 		}
430 	}
431 
432 	rw_exit(&inject_lock);
433 }
434 
435 hrtime_t
436 zio_handle_io_delay(zio_t *zio)
437 {
438 	vdev_t *vd = zio->io_vd;
439 	inject_handler_t *min_handler = NULL;
440 	hrtime_t min_target = 0;
441 
442 	rw_enter(&inject_lock, RW_READER);
443 
444 	/*
445 	 * inject_delay_count is a subset of zio_injection_enabled that
446 	 * is only incremented for delay handlers. These checks are
447 	 * mainly added to remind the reader why we're not explicitly
448 	 * checking zio_injection_enabled like the other functions.
449 	 */
450 	IMPLY(inject_delay_count > 0, zio_injection_enabled > 0);
451 	IMPLY(zio_injection_enabled == 0, inject_delay_count == 0);
452 
453 	/*
454 	 * If there aren't any inject delay handlers registered, then we
455 	 * can short circuit and simply return 0 here. A value of zero
456 	 * informs zio_delay_interrupt() that this request should not be
457 	 * delayed. This short circuit keeps us from acquiring the
458 	 * inject_delay_mutex unnecessarily.
459 	 */
460 	if (inject_delay_count == 0) {
461 		rw_exit(&inject_lock);
462 		return (0);
463 	}
464 
465 	/*
466 	 * Each inject handler has a number of "lanes" associated with
467 	 * it. Each lane is able to handle requests independently of one
468 	 * another, and at a latency defined by the inject handler
469 	 * record's zi_timer field. Thus if a handler in configured with
470 	 * a single lane with a 10ms latency, it will delay requests
471 	 * such that only a single request is completed every 10ms. So,
472 	 * if more than one request is attempted per each 10ms interval,
473 	 * the average latency of the requests will be greater than
474 	 * 10ms; but if only a single request is submitted each 10ms
475 	 * interval the average latency will be 10ms.
476 	 *
477 	 * We need to acquire this mutex to prevent multiple concurrent
478 	 * threads being assigned to the same lane of a given inject
479 	 * handler. The mutex allows us to perform the following two
480 	 * operations atomically:
481 	 *
482 	 *	1. determine the minimum handler and minimum target
483 	 *	   value of all the possible handlers
484 	 *	2. update that minimum handler's lane array
485 	 *
486 	 * Without atomicity, two (or more) threads could pick the same
487 	 * lane in step (1), and then conflict with each other in step
488 	 * (2). This could allow a single lane handler to process
489 	 * multiple requests simultaneously, which shouldn't be possible.
490 	 */
491 	mutex_enter(&inject_delay_mtx);
492 
493 	for (inject_handler_t *handler = list_head(&inject_handlers);
494 	    handler != NULL; handler = list_next(&inject_handlers, handler)) {
495 		if (handler->zi_record.zi_cmd != ZINJECT_DELAY_IO)
496 			continue;
497 
498 		if (vd->vdev_guid != handler->zi_record.zi_guid)
499 			continue;
500 
501 		/*
502 		 * Defensive; should never happen as the array allocation
503 		 * occurs prior to inserting this handler on the list.
504 		 */
505 		ASSERT3P(handler->zi_lanes, !=, NULL);
506 
507 		/*
508 		 * This should never happen, the zinject command should
509 		 * prevent a user from setting an IO delay with zero lanes.
510 		 */
511 		ASSERT3U(handler->zi_record.zi_nlanes, !=, 0);
512 
513 		ASSERT3U(handler->zi_record.zi_nlanes, >,
514 		    handler->zi_next_lane);
515 
516 		/*
517 		 * We want to issue this IO to the lane that will become
518 		 * idle the soonest, so we compare the soonest this
519 		 * specific handler can complete the IO with all other
520 		 * handlers, to find the lowest value of all possible
521 		 * lanes. We then use this lane to submit the request.
522 		 *
523 		 * Since each handler has a constant value for its
524 		 * delay, we can just use the "next" lane for that
525 		 * handler; as it will always be the lane with the
526 		 * lowest value for that particular handler (i.e. the
527 		 * lane that will become idle the soonest). This saves a
528 		 * scan of each handler's lanes array.
529 		 *
530 		 * There's two cases to consider when determining when
531 		 * this specific IO request should complete. If this
532 		 * lane is idle, we want to "submit" the request now so
533 		 * it will complete after zi_timer milliseconds. Thus,
534 		 * we set the target to now + zi_timer.
535 		 *
536 		 * If the lane is busy, we want this request to complete
537 		 * zi_timer milliseconds after the lane becomes idle.
538 		 * Since the 'zi_lanes' array holds the time at which
539 		 * each lane will become idle, we use that value to
540 		 * determine when this request should complete.
541 		 */
542 		hrtime_t idle = handler->zi_record.zi_timer + gethrtime();
543 		hrtime_t busy = handler->zi_record.zi_timer +
544 		    handler->zi_lanes[handler->zi_next_lane];
545 		hrtime_t target = MAX(idle, busy);
546 
547 		if (min_handler == NULL) {
548 			min_handler = handler;
549 			min_target = target;
550 			continue;
551 		}
552 
553 		ASSERT3P(min_handler, !=, NULL);
554 		ASSERT3U(min_target, !=, 0);
555 
556 		/*
557 		 * We don't yet increment the "next lane" variable since
558 		 * we still might find a lower value lane in another
559 		 * handler during any remaining iterations. Once we're
560 		 * sure we've selected the absolute minimum, we'll claim
561 		 * the lane and increment the handler's "next lane"
562 		 * field below.
563 		 */
564 
565 		if (target < min_target) {
566 			min_handler = handler;
567 			min_target = target;
568 		}
569 	}
570 
571 	/*
572 	 * 'min_handler' will be NULL if no IO delays are registered for
573 	 * this vdev, otherwise it will point to the handler containing
574 	 * the lane that will become idle the soonest.
575 	 */
576 	if (min_handler != NULL) {
577 		ASSERT3U(min_target, !=, 0);
578 		min_handler->zi_lanes[min_handler->zi_next_lane] = min_target;
579 
580 		/*
581 		 * If we've used all possible lanes for this handler,
582 		 * loop back and start using the first lane again;
583 		 * otherwise, just increment the lane index.
584 		 */
585 		min_handler->zi_next_lane = (min_handler->zi_next_lane + 1) %
586 		    min_handler->zi_record.zi_nlanes;
587 	}
588 
589 	mutex_exit(&inject_delay_mtx);
590 	rw_exit(&inject_lock);
591 
592 	return (min_target);
593 }
594 
595 /*
596  * Create a new handler for the given record.  We add it to the list, adding
597  * a reference to the spa_t in the process.  We increment zio_injection_enabled,
598  * which is the switch to trigger all fault injection.
599  */
600 int
601 zio_inject_fault(char *name, int flags, int *id, zinject_record_t *record)
602 {
603 	inject_handler_t *handler;
604 	int error;
605 	spa_t *spa;
606 
607 	/*
608 	 * If this is pool-wide metadata, make sure we unload the corresponding
609 	 * spa_t, so that the next attempt to load it will trigger the fault.
610 	 * We call spa_reset() to unload the pool appropriately.
611 	 */
612 	if (flags & ZINJECT_UNLOAD_SPA)
613 		if ((error = spa_reset(name)) != 0)
614 			return (error);
615 
616 	if (record->zi_cmd == ZINJECT_DELAY_IO) {
617 		/*
618 		 * A value of zero for the number of lanes or for the
619 		 * delay time doesn't make sense.
620 		 */
621 		if (record->zi_timer == 0 || record->zi_nlanes == 0)
622 			return (SET_ERROR(EINVAL));
623 
624 		/*
625 		 * The number of lanes is directly mapped to the size of
626 		 * an array used by the handler. Thus, to ensure the
627 		 * user doesn't trigger an allocation that's "too large"
628 		 * we cap the number of lanes here.
629 		 */
630 		if (record->zi_nlanes >= UINT16_MAX)
631 			return (SET_ERROR(EINVAL));
632 	}
633 
634 	if (!(flags & ZINJECT_NULL)) {
635 		/*
636 		 * spa_inject_ref() will add an injection reference, which will
637 		 * prevent the pool from being removed from the namespace while
638 		 * still allowing it to be unloaded.
639 		 */
640 		if ((spa = spa_inject_addref(name)) == NULL)
641 			return (SET_ERROR(ENOENT));
642 
643 		handler = kmem_alloc(sizeof (inject_handler_t), KM_SLEEP);
644 
645 		handler->zi_spa = spa;
646 		handler->zi_record = *record;
647 
648 		if (handler->zi_record.zi_cmd == ZINJECT_DELAY_IO) {
649 			handler->zi_lanes = kmem_zalloc(
650 			    sizeof (*handler->zi_lanes) *
651 			    handler->zi_record.zi_nlanes, KM_SLEEP);
652 			handler->zi_next_lane = 0;
653 		} else {
654 			handler->zi_lanes = NULL;
655 			handler->zi_next_lane = 0;
656 		}
657 
658 		rw_enter(&inject_lock, RW_WRITER);
659 
660 		/*
661 		 * We can't move this increment into the conditional
662 		 * above because we need to hold the RW_WRITER lock of
663 		 * inject_lock, and we don't want to hold that while
664 		 * allocating the handler's zi_lanes array.
665 		 */
666 		if (handler->zi_record.zi_cmd == ZINJECT_DELAY_IO) {
667 			ASSERT3S(inject_delay_count, >=, 0);
668 			inject_delay_count++;
669 			ASSERT3S(inject_delay_count, >, 0);
670 		}
671 
672 		*id = handler->zi_id = inject_next_id++;
673 		list_insert_tail(&inject_handlers, handler);
674 		atomic_inc_32(&zio_injection_enabled);
675 
676 		rw_exit(&inject_lock);
677 	}
678 
679 	/*
680 	 * Flush the ARC, so that any attempts to read this data will end up
681 	 * going to the ZIO layer.  Note that this is a little overkill, but
682 	 * we don't have the necessary ARC interfaces to do anything else, and
683 	 * fault injection isn't a performance critical path.
684 	 */
685 	if (flags & ZINJECT_FLUSH_ARC)
686 		/*
687 		 * We must use FALSE to ensure arc_flush returns, since
688 		 * we're not preventing concurrent ARC insertions.
689 		 */
690 		arc_flush(NULL, FALSE);
691 
692 	return (0);
693 }
694 
695 /*
696  * Returns the next record with an ID greater than that supplied to the
697  * function.  Used to iterate over all handlers in the system.
698  */
699 int
700 zio_inject_list_next(int *id, char *name, size_t buflen,
701     zinject_record_t *record)
702 {
703 	inject_handler_t *handler;
704 	int ret;
705 
706 	mutex_enter(&spa_namespace_lock);
707 	rw_enter(&inject_lock, RW_READER);
708 
709 	for (handler = list_head(&inject_handlers); handler != NULL;
710 	    handler = list_next(&inject_handlers, handler))
711 		if (handler->zi_id > *id)
712 			break;
713 
714 	if (handler) {
715 		*record = handler->zi_record;
716 		*id = handler->zi_id;
717 		(void) strncpy(name, spa_name(handler->zi_spa), buflen);
718 		ret = 0;
719 	} else {
720 		ret = SET_ERROR(ENOENT);
721 	}
722 
723 	rw_exit(&inject_lock);
724 	mutex_exit(&spa_namespace_lock);
725 
726 	return (ret);
727 }
728 
729 /*
730  * Clear the fault handler with the given identifier, or return ENOENT if none
731  * exists.
732  */
733 int
734 zio_clear_fault(int id)
735 {
736 	inject_handler_t *handler;
737 
738 	rw_enter(&inject_lock, RW_WRITER);
739 
740 	for (handler = list_head(&inject_handlers); handler != NULL;
741 	    handler = list_next(&inject_handlers, handler))
742 		if (handler->zi_id == id)
743 			break;
744 
745 	if (handler == NULL) {
746 		rw_exit(&inject_lock);
747 		return (SET_ERROR(ENOENT));
748 	}
749 
750 	if (handler->zi_record.zi_cmd == ZINJECT_DELAY_IO) {
751 		ASSERT3S(inject_delay_count, >, 0);
752 		inject_delay_count--;
753 		ASSERT3S(inject_delay_count, >=, 0);
754 	}
755 
756 	list_remove(&inject_handlers, handler);
757 	rw_exit(&inject_lock);
758 
759 	if (handler->zi_record.zi_cmd == ZINJECT_DELAY_IO) {
760 		ASSERT3P(handler->zi_lanes, !=, NULL);
761 		kmem_free(handler->zi_lanes, sizeof (*handler->zi_lanes) *
762 		    handler->zi_record.zi_nlanes);
763 	} else {
764 		ASSERT3P(handler->zi_lanes, ==, NULL);
765 	}
766 
767 	spa_inject_delref(handler->zi_spa);
768 	kmem_free(handler, sizeof (inject_handler_t));
769 	atomic_dec_32(&zio_injection_enabled);
770 
771 	return (0);
772 }
773 
774 void
775 zio_inject_init(void)
776 {
777 	rw_init(&inject_lock, NULL, RW_DEFAULT, NULL);
778 	mutex_init(&inject_delay_mtx, NULL, MUTEX_DEFAULT, NULL);
779 	list_create(&inject_handlers, sizeof (inject_handler_t),
780 	    offsetof(inject_handler_t, zi_link));
781 }
782 
783 void
784 zio_inject_fini(void)
785 {
786 	list_destroy(&inject_handlers);
787 	mutex_destroy(&inject_delay_mtx);
788 	rw_destroy(&inject_lock);
789 }
790