xref: /illumos-gate/usr/src/uts/common/fs/zfs/zio_inject.c (revision 98d1cbfec254295273b6a761bc1861c0374bdf02)
1ea8dc4b6Seschrock /*
2ea8dc4b6Seschrock  * CDDL HEADER START
3ea8dc4b6Seschrock  *
4ea8dc4b6Seschrock  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7ea8dc4b6Seschrock  *
8ea8dc4b6Seschrock  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9ea8dc4b6Seschrock  * or http://www.opensolaris.org/os/licensing.
10ea8dc4b6Seschrock  * See the License for the specific language governing permissions
11ea8dc4b6Seschrock  * and limitations under the License.
12ea8dc4b6Seschrock  *
13ea8dc4b6Seschrock  * When distributing Covered Code, include this CDDL HEADER in each
14ea8dc4b6Seschrock  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15ea8dc4b6Seschrock  * If applicable, add the following below this CDDL HEADER, with the
16ea8dc4b6Seschrock  * fields enclosed by brackets "[]" replaced with your own identifying
17ea8dc4b6Seschrock  * information: Portions Copyright [yyyy] [name of copyright owner]
18ea8dc4b6Seschrock  *
19ea8dc4b6Seschrock  * CDDL HEADER END
20ea8dc4b6Seschrock  */
21ea8dc4b6Seschrock /*
22*98d1cbfeSGeorge Wilson  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23ea8dc4b6Seschrock  */
24ea8dc4b6Seschrock 
25ea8dc4b6Seschrock /*
26ea8dc4b6Seschrock  * ZFS fault injection
27ea8dc4b6Seschrock  *
28ea8dc4b6Seschrock  * To handle fault injection, we keep track of a series of zinject_record_t
29ea8dc4b6Seschrock  * structures which describe which logical block(s) should be injected with a
30ea8dc4b6Seschrock  * fault.  These are kept in a global list.  Each record corresponds to a given
31ea8dc4b6Seschrock  * spa_t and maintains a special hold on the spa_t so that it cannot be deleted
32ea8dc4b6Seschrock  * or exported while the injection record exists.
33ea8dc4b6Seschrock  *
34ea8dc4b6Seschrock  * Device level injection is done using the 'zi_guid' field.  If this is set, it
35ea8dc4b6Seschrock  * means that the error is destined for a particular device, not a piece of
36ea8dc4b6Seschrock  * data.
37ea8dc4b6Seschrock  *
38ea8dc4b6Seschrock  * This is a rather poor data structure and algorithm, but we don't expect more
39ea8dc4b6Seschrock  * than a few faults at any one time, so it should be sufficient for our needs.
40ea8dc4b6Seschrock  */
41ea8dc4b6Seschrock 
42ea8dc4b6Seschrock #include <sys/arc.h>
43ea8dc4b6Seschrock #include <sys/zio_impl.h>
44ea8dc4b6Seschrock #include <sys/zfs_ioctl.h>
45ea8dc4b6Seschrock #include <sys/vdev_impl.h>
46b24ab676SJeff Bonwick #include <sys/dmu_objset.h>
4721bf64a7Sgw #include <sys/fs/zfs.h>
48ea8dc4b6Seschrock 
49ea8dc4b6Seschrock uint32_t zio_injection_enabled;
50ea8dc4b6Seschrock 
51ea8dc4b6Seschrock typedef struct inject_handler {
52ea8dc4b6Seschrock 	int			zi_id;
53ea8dc4b6Seschrock 	spa_t			*zi_spa;
54ea8dc4b6Seschrock 	zinject_record_t	zi_record;
55ea8dc4b6Seschrock 	list_node_t		zi_link;
56ea8dc4b6Seschrock } inject_handler_t;
57ea8dc4b6Seschrock 
58ea8dc4b6Seschrock static list_t inject_handlers;
59ea8dc4b6Seschrock static krwlock_t inject_lock;
60ea8dc4b6Seschrock static int inject_next_id = 1;
61ea8dc4b6Seschrock 
62ea8dc4b6Seschrock /*
63ea8dc4b6Seschrock  * Returns true if the given record matches the I/O in progress.
64ea8dc4b6Seschrock  */
65ea8dc4b6Seschrock static boolean_t
66ea8dc4b6Seschrock zio_match_handler(zbookmark_t *zb, uint64_t type,
67ea8dc4b6Seschrock     zinject_record_t *record, int error)
68ea8dc4b6Seschrock {
69ea8dc4b6Seschrock 	/*
70ea8dc4b6Seschrock 	 * Check for a match against the MOS, which is based on type
71ea8dc4b6Seschrock 	 */
72b24ab676SJeff Bonwick 	if (zb->zb_objset == DMU_META_OBJSET &&
73b24ab676SJeff Bonwick 	    record->zi_objset == DMU_META_OBJSET &&
74b24ab676SJeff Bonwick 	    record->zi_object == DMU_META_DNODE_OBJECT) {
75ea8dc4b6Seschrock 		if (record->zi_type == DMU_OT_NONE ||
76ea8dc4b6Seschrock 		    type == record->zi_type)
77ea8dc4b6Seschrock 			return (record->zi_freq == 0 ||
78ea8dc4b6Seschrock 			    spa_get_random(100) < record->zi_freq);
79ea8dc4b6Seschrock 		else
80ea8dc4b6Seschrock 			return (B_FALSE);
81ea8dc4b6Seschrock 	}
82ea8dc4b6Seschrock 
83ea8dc4b6Seschrock 	/*
84ea8dc4b6Seschrock 	 * Check for an exact match.
85ea8dc4b6Seschrock 	 */
86ea8dc4b6Seschrock 	if (zb->zb_objset == record->zi_objset &&
87ea8dc4b6Seschrock 	    zb->zb_object == record->zi_object &&
88ea8dc4b6Seschrock 	    zb->zb_level == record->zi_level &&
89ea8dc4b6Seschrock 	    zb->zb_blkid >= record->zi_start &&
90ea8dc4b6Seschrock 	    zb->zb_blkid <= record->zi_end &&
91ea8dc4b6Seschrock 	    error == record->zi_error)
92ea8dc4b6Seschrock 		return (record->zi_freq == 0 ||
93ea8dc4b6Seschrock 		    spa_get_random(100) < record->zi_freq);
94ea8dc4b6Seschrock 
95ea8dc4b6Seschrock 	return (B_FALSE);
96ea8dc4b6Seschrock }
97ea8dc4b6Seschrock 
9888ecc943SGeorge Wilson /*
9988ecc943SGeorge Wilson  * Panic the system when a config change happens in the function
10088ecc943SGeorge Wilson  * specified by tag.
10188ecc943SGeorge Wilson  */
10288ecc943SGeorge Wilson void
1031195e687SMark J Musante zio_handle_panic_injection(spa_t *spa, char *tag, uint64_t type)
10488ecc943SGeorge Wilson {
10588ecc943SGeorge Wilson 	inject_handler_t *handler;
10688ecc943SGeorge Wilson 
10788ecc943SGeorge Wilson 	rw_enter(&inject_lock, RW_READER);
10888ecc943SGeorge Wilson 
10988ecc943SGeorge Wilson 	for (handler = list_head(&inject_handlers); handler != NULL;
11088ecc943SGeorge Wilson 	    handler = list_next(&inject_handlers, handler)) {
11188ecc943SGeorge Wilson 
11288ecc943SGeorge Wilson 		if (spa != handler->zi_spa)
11388ecc943SGeorge Wilson 			continue;
11488ecc943SGeorge Wilson 
1151195e687SMark J Musante 		if (handler->zi_record.zi_type == type &&
1161195e687SMark J Musante 		    strcmp(tag, handler->zi_record.zi_func) == 0)
11788ecc943SGeorge Wilson 			panic("Panic requested in function %s\n", tag);
11888ecc943SGeorge Wilson 	}
11988ecc943SGeorge Wilson 
12088ecc943SGeorge Wilson 	rw_exit(&inject_lock);
12188ecc943SGeorge Wilson }
12288ecc943SGeorge Wilson 
123ea8dc4b6Seschrock /*
124ea8dc4b6Seschrock  * Determine if the I/O in question should return failure.  Returns the errno
125ea8dc4b6Seschrock  * to be returned to the caller.
126ea8dc4b6Seschrock  */
127ea8dc4b6Seschrock int
128ea8dc4b6Seschrock zio_handle_fault_injection(zio_t *zio, int error)
129ea8dc4b6Seschrock {
130ea8dc4b6Seschrock 	int ret = 0;
131ea8dc4b6Seschrock 	inject_handler_t *handler;
132ea8dc4b6Seschrock 
133ea8dc4b6Seschrock 	/*
134ea8dc4b6Seschrock 	 * Ignore I/O not associated with any logical data.
135ea8dc4b6Seschrock 	 */
136ea8dc4b6Seschrock 	if (zio->io_logical == NULL)
137ea8dc4b6Seschrock 		return (0);
138ea8dc4b6Seschrock 
139ea8dc4b6Seschrock 	/*
140ea8dc4b6Seschrock 	 * Currently, we only support fault injection on reads.
141ea8dc4b6Seschrock 	 */
142ea8dc4b6Seschrock 	if (zio->io_type != ZIO_TYPE_READ)
143ea8dc4b6Seschrock 		return (0);
144ea8dc4b6Seschrock 
145ea8dc4b6Seschrock 	rw_enter(&inject_lock, RW_READER);
146ea8dc4b6Seschrock 
147ea8dc4b6Seschrock 	for (handler = list_head(&inject_handlers); handler != NULL;
148ea8dc4b6Seschrock 	    handler = list_next(&inject_handlers, handler)) {
149ea8dc4b6Seschrock 
150ea8dc4b6Seschrock 		/* Ignore errors not destined for this pool */
151ea8dc4b6Seschrock 		if (zio->io_spa != handler->zi_spa)
152ea8dc4b6Seschrock 			continue;
153ea8dc4b6Seschrock 
15488ecc943SGeorge Wilson 		/* Ignore device errors and panic injection */
15588ecc943SGeorge Wilson 		if (handler->zi_record.zi_guid != 0 ||
156468c413aSTim Haley 		    handler->zi_record.zi_func[0] != '\0' ||
157468c413aSTim Haley 		    handler->zi_record.zi_duration != 0)
158ea8dc4b6Seschrock 			continue;
159ea8dc4b6Seschrock 
160ea8dc4b6Seschrock 		/* If this handler matches, return EIO */
161ea8dc4b6Seschrock 		if (zio_match_handler(&zio->io_logical->io_bookmark,
162ea8dc4b6Seschrock 		    zio->io_bp ? BP_GET_TYPE(zio->io_bp) : DMU_OT_NONE,
163ea8dc4b6Seschrock 		    &handler->zi_record, error)) {
164ea8dc4b6Seschrock 			ret = error;
165ea8dc4b6Seschrock 			break;
166ea8dc4b6Seschrock 		}
167ea8dc4b6Seschrock 	}
168ea8dc4b6Seschrock 
169ea8dc4b6Seschrock 	rw_exit(&inject_lock);
170ea8dc4b6Seschrock 
171ea8dc4b6Seschrock 	return (ret);
172ea8dc4b6Seschrock }
173ea8dc4b6Seschrock 
17421bf64a7Sgw /*
17521bf64a7Sgw  * Determine if the zio is part of a label update and has an injection
17621bf64a7Sgw  * handler associated with that portion of the label. Currently, we
17721bf64a7Sgw  * allow error injection in either the nvlist or the uberblock region of
17821bf64a7Sgw  * of the vdev label.
17921bf64a7Sgw  */
18021bf64a7Sgw int
18121bf64a7Sgw zio_handle_label_injection(zio_t *zio, int error)
18221bf64a7Sgw {
18321bf64a7Sgw 	inject_handler_t *handler;
18421bf64a7Sgw 	vdev_t *vd = zio->io_vd;
18521bf64a7Sgw 	uint64_t offset = zio->io_offset;
18621bf64a7Sgw 	int label;
18721bf64a7Sgw 	int ret = 0;
18821bf64a7Sgw 
1898f18d1faSGeorge Wilson 	if (offset >= VDEV_LABEL_START_SIZE &&
19021bf64a7Sgw 	    offset < vd->vdev_psize - VDEV_LABEL_END_SIZE)
19121bf64a7Sgw 		return (0);
19221bf64a7Sgw 
19321bf64a7Sgw 	rw_enter(&inject_lock, RW_READER);
19421bf64a7Sgw 
19521bf64a7Sgw 	for (handler = list_head(&inject_handlers); handler != NULL;
19621bf64a7Sgw 	    handler = list_next(&inject_handlers, handler)) {
19721bf64a7Sgw 		uint64_t start = handler->zi_record.zi_start;
19821bf64a7Sgw 		uint64_t end = handler->zi_record.zi_end;
19921bf64a7Sgw 
20088ecc943SGeorge Wilson 		/* Ignore device only faults or panic injection */
20188ecc943SGeorge Wilson 		if (handler->zi_record.zi_start == 0 ||
202468c413aSTim Haley 		    handler->zi_record.zi_func[0] != '\0' ||
203468c413aSTim Haley 		    handler->zi_record.zi_duration != 0)
20421bf64a7Sgw 			continue;
20521bf64a7Sgw 
20621bf64a7Sgw 		/*
20721bf64a7Sgw 		 * The injection region is the relative offsets within a
20821bf64a7Sgw 		 * vdev label. We must determine the label which is being
20921bf64a7Sgw 		 * updated and adjust our region accordingly.
21021bf64a7Sgw 		 */
21121bf64a7Sgw 		label = vdev_label_number(vd->vdev_psize, offset);
21221bf64a7Sgw 		start = vdev_label_offset(vd->vdev_psize, label, start);
21321bf64a7Sgw 		end = vdev_label_offset(vd->vdev_psize, label, end);
21421bf64a7Sgw 
21521bf64a7Sgw 		if (zio->io_vd->vdev_guid == handler->zi_record.zi_guid &&
21621bf64a7Sgw 		    (offset >= start && offset <= end)) {
21721bf64a7Sgw 			ret = error;
21821bf64a7Sgw 			break;
21921bf64a7Sgw 		}
22021bf64a7Sgw 	}
22121bf64a7Sgw 	rw_exit(&inject_lock);
22221bf64a7Sgw 	return (ret);
22321bf64a7Sgw }
22421bf64a7Sgw 
22521bf64a7Sgw 
226ea8dc4b6Seschrock int
2278956713aSEric Schrock zio_handle_device_injection(vdev_t *vd, zio_t *zio, int error)
228ea8dc4b6Seschrock {
229ea8dc4b6Seschrock 	inject_handler_t *handler;
230ea8dc4b6Seschrock 	int ret = 0;
231ea8dc4b6Seschrock 
2328f18d1faSGeorge Wilson 	/*
2338f18d1faSGeorge Wilson 	 * We skip over faults in the labels unless it's during
2348f18d1faSGeorge Wilson 	 * device open (i.e. zio == NULL).
2358f18d1faSGeorge Wilson 	 */
2368f18d1faSGeorge Wilson 	if (zio != NULL) {
2378f18d1faSGeorge Wilson 		uint64_t offset = zio->io_offset;
2388f18d1faSGeorge Wilson 
2398f18d1faSGeorge Wilson 		if (offset < VDEV_LABEL_START_SIZE ||
2408f18d1faSGeorge Wilson 		    offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE)
2413c708518SMark J Musante 			return (0);
2428f18d1faSGeorge Wilson 	}
2438f18d1faSGeorge Wilson 
244ea8dc4b6Seschrock 	rw_enter(&inject_lock, RW_READER);
245ea8dc4b6Seschrock 
246ea8dc4b6Seschrock 	for (handler = list_head(&inject_handlers); handler != NULL;
247ea8dc4b6Seschrock 	    handler = list_next(&inject_handlers, handler)) {
248ea8dc4b6Seschrock 
249468c413aSTim Haley 		/*
250468c413aSTim Haley 		 * Ignore label specific faults, panic injection
251468c413aSTim Haley 		 * or fake writes
252468c413aSTim Haley 		 */
25388ecc943SGeorge Wilson 		if (handler->zi_record.zi_start != 0 ||
254468c413aSTim Haley 		    handler->zi_record.zi_func[0] != '\0' ||
255468c413aSTim Haley 		    handler->zi_record.zi_duration != 0)
25621bf64a7Sgw 			continue;
25721bf64a7Sgw 
258ea8dc4b6Seschrock 		if (vd->vdev_guid == handler->zi_record.zi_guid) {
2598956713aSEric Schrock 			if (handler->zi_record.zi_failfast &&
2608956713aSEric Schrock 			    (zio == NULL || (zio->io_flags &
2618956713aSEric Schrock 			    (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD)))) {
2628956713aSEric Schrock 				continue;
2638956713aSEric Schrock 			}
2648956713aSEric Schrock 
2658f18d1faSGeorge Wilson 			/* Handle type specific I/O failures */
2668f18d1faSGeorge Wilson 			if (zio != NULL &&
2678f18d1faSGeorge Wilson 			    handler->zi_record.zi_iotype != ZIO_TYPES &&
2688f18d1faSGeorge Wilson 			    handler->zi_record.zi_iotype != zio->io_type)
2698f18d1faSGeorge Wilson 				continue;
2708f18d1faSGeorge Wilson 
271ea8dc4b6Seschrock 			if (handler->zi_record.zi_error == error) {
272ea8dc4b6Seschrock 				/*
273ea8dc4b6Seschrock 				 * For a failed open, pretend like the device
274ea8dc4b6Seschrock 				 * has gone away.
275ea8dc4b6Seschrock 				 */
276ea8dc4b6Seschrock 				if (error == ENXIO)
277ea8dc4b6Seschrock 					vd->vdev_stat.vs_aux =
278ea8dc4b6Seschrock 					    VDEV_AUX_OPEN_FAILED;
279*98d1cbfeSGeorge Wilson 
280*98d1cbfeSGeorge Wilson 				/*
281*98d1cbfeSGeorge Wilson 				 * Treat these errors as if they had been
282*98d1cbfeSGeorge Wilson 				 * retried so that all the appropriate stats
283*98d1cbfeSGeorge Wilson 				 * and FMA events are generated.
284*98d1cbfeSGeorge Wilson 				 */
285*98d1cbfeSGeorge Wilson 				if (!handler->zi_record.zi_failfast &&
286*98d1cbfeSGeorge Wilson 				    zio != NULL)
287*98d1cbfeSGeorge Wilson 					zio->io_flags |= ZIO_FLAG_IO_RETRY;
288*98d1cbfeSGeorge Wilson 
289ea8dc4b6Seschrock 				ret = error;
290ea8dc4b6Seschrock 				break;
291ea8dc4b6Seschrock 			}
292ea8dc4b6Seschrock 			if (handler->zi_record.zi_error == ENXIO) {
293ea8dc4b6Seschrock 				ret = EIO;
294ea8dc4b6Seschrock 				break;
295ea8dc4b6Seschrock 			}
296ea8dc4b6Seschrock 		}
297ea8dc4b6Seschrock 	}
298ea8dc4b6Seschrock 
299ea8dc4b6Seschrock 	rw_exit(&inject_lock);
300ea8dc4b6Seschrock 
301ea8dc4b6Seschrock 	return (ret);
302ea8dc4b6Seschrock }
303ea8dc4b6Seschrock 
304468c413aSTim Haley /*
305468c413aSTim Haley  * Simulate hardware that ignores cache flushes.  For requested number
306468c413aSTim Haley  * of seconds nix the actual writing to disk.
307468c413aSTim Haley  */
308468c413aSTim Haley void
309468c413aSTim Haley zio_handle_ignored_writes(zio_t *zio)
310468c413aSTim Haley {
311468c413aSTim Haley 	inject_handler_t *handler;
312468c413aSTim Haley 
313468c413aSTim Haley 	rw_enter(&inject_lock, RW_READER);
314468c413aSTim Haley 
315468c413aSTim Haley 	for (handler = list_head(&inject_handlers); handler != NULL;
316468c413aSTim Haley 	    handler = list_next(&inject_handlers, handler)) {
317468c413aSTim Haley 
318468c413aSTim Haley 		/* Ignore errors not destined for this pool */
319468c413aSTim Haley 		if (zio->io_spa != handler->zi_spa)
320468c413aSTim Haley 			continue;
321468c413aSTim Haley 
322468c413aSTim Haley 		if (handler->zi_record.zi_duration == 0)
323468c413aSTim Haley 			continue;
324468c413aSTim Haley 
325468c413aSTim Haley 		/*
326468c413aSTim Haley 		 * Positive duration implies # of seconds, negative
327468c413aSTim Haley 		 * a number of txgs
328468c413aSTim Haley 		 */
329468c413aSTim Haley 		if (handler->zi_record.zi_timer == 0) {
330468c413aSTim Haley 			if (handler->zi_record.zi_duration > 0)
331d3d50737SRafael Vanoni 				handler->zi_record.zi_timer = ddi_get_lbolt64();
332468c413aSTim Haley 			else
333468c413aSTim Haley 				handler->zi_record.zi_timer = zio->io_txg;
334468c413aSTim Haley 		}
335a33cae98STim Haley 
336a33cae98STim Haley 		/* Have a "problem" writing 60% of the time */
337a33cae98STim Haley 		if (spa_get_random(100) < 60)
338a33cae98STim Haley 			zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
339468c413aSTim Haley 		break;
340468c413aSTim Haley 	}
341468c413aSTim Haley 
342468c413aSTim Haley 	rw_exit(&inject_lock);
343468c413aSTim Haley }
344468c413aSTim Haley 
345468c413aSTim Haley void
346468c413aSTim Haley spa_handle_ignored_writes(spa_t *spa)
347468c413aSTim Haley {
348468c413aSTim Haley 	inject_handler_t *handler;
349468c413aSTim Haley 
350468c413aSTim Haley 	if (zio_injection_enabled == 0)
351468c413aSTim Haley 		return;
352468c413aSTim Haley 
353468c413aSTim Haley 	rw_enter(&inject_lock, RW_READER);
354468c413aSTim Haley 
355468c413aSTim Haley 	for (handler = list_head(&inject_handlers); handler != NULL;
356468c413aSTim Haley 	    handler = list_next(&inject_handlers, handler)) {
357468c413aSTim Haley 
358468c413aSTim Haley 		/* Ignore errors not destined for this pool */
359468c413aSTim Haley 		if (spa != handler->zi_spa)
360468c413aSTim Haley 			continue;
361468c413aSTim Haley 
362468c413aSTim Haley 		if (handler->zi_record.zi_duration == 0)
363468c413aSTim Haley 			continue;
364468c413aSTim Haley 
365468c413aSTim Haley 		if (handler->zi_record.zi_duration > 0) {
366468c413aSTim Haley 			VERIFY(handler->zi_record.zi_timer == 0 ||
367468c413aSTim Haley 			    handler->zi_record.zi_timer +
368d3d50737SRafael Vanoni 			    handler->zi_record.zi_duration * hz >
369d3d50737SRafael Vanoni 			    ddi_get_lbolt64());
370468c413aSTim Haley 		} else {
371468c413aSTim Haley 			/* duration is negative so the subtraction here adds */
372468c413aSTim Haley 			VERIFY(handler->zi_record.zi_timer == 0 ||
373468c413aSTim Haley 			    handler->zi_record.zi_timer -
374468c413aSTim Haley 			    handler->zi_record.zi_duration >=
375b24ab676SJeff Bonwick 			    spa_syncing_txg(spa));
376468c413aSTim Haley 		}
377468c413aSTim Haley 	}
378468c413aSTim Haley 
379468c413aSTim Haley 	rw_exit(&inject_lock);
380468c413aSTim Haley }
381468c413aSTim Haley 
382ea8dc4b6Seschrock /*
383ea8dc4b6Seschrock  * Create a new handler for the given record.  We add it to the list, adding
384ea8dc4b6Seschrock  * a reference to the spa_t in the process.  We increment zio_injection_enabled,
385ea8dc4b6Seschrock  * which is the switch to trigger all fault injection.
386ea8dc4b6Seschrock  */
387ea8dc4b6Seschrock int
388ea8dc4b6Seschrock zio_inject_fault(char *name, int flags, int *id, zinject_record_t *record)
389ea8dc4b6Seschrock {
390ea8dc4b6Seschrock 	inject_handler_t *handler;
391ea8dc4b6Seschrock 	int error;
392ea8dc4b6Seschrock 	spa_t *spa;
393ea8dc4b6Seschrock 
394ea8dc4b6Seschrock 	/*
395ea8dc4b6Seschrock 	 * If this is pool-wide metadata, make sure we unload the corresponding
396ea8dc4b6Seschrock 	 * spa_t, so that the next attempt to load it will trigger the fault.
397ea8dc4b6Seschrock 	 * We call spa_reset() to unload the pool appropriately.
398ea8dc4b6Seschrock 	 */
399ea8dc4b6Seschrock 	if (flags & ZINJECT_UNLOAD_SPA)
400ea8dc4b6Seschrock 		if ((error = spa_reset(name)) != 0)
401ea8dc4b6Seschrock 			return (error);
402ea8dc4b6Seschrock 
403ea8dc4b6Seschrock 	if (!(flags & ZINJECT_NULL)) {
404ea8dc4b6Seschrock 		/*
405ea8dc4b6Seschrock 		 * spa_inject_ref() will add an injection reference, which will
406ea8dc4b6Seschrock 		 * prevent the pool from being removed from the namespace while
407ea8dc4b6Seschrock 		 * still allowing it to be unloaded.
408ea8dc4b6Seschrock 		 */
409ea8dc4b6Seschrock 		if ((spa = spa_inject_addref(name)) == NULL)
410ea8dc4b6Seschrock 			return (ENOENT);
411ea8dc4b6Seschrock 
412ea8dc4b6Seschrock 		handler = kmem_alloc(sizeof (inject_handler_t), KM_SLEEP);
413ea8dc4b6Seschrock 
414ea8dc4b6Seschrock 		rw_enter(&inject_lock, RW_WRITER);
415ea8dc4b6Seschrock 
416ea8dc4b6Seschrock 		*id = handler->zi_id = inject_next_id++;
417ea8dc4b6Seschrock 		handler->zi_spa = spa;
418ea8dc4b6Seschrock 		handler->zi_record = *record;
419ea8dc4b6Seschrock 		list_insert_tail(&inject_handlers, handler);
420ea8dc4b6Seschrock 		atomic_add_32(&zio_injection_enabled, 1);
421ea8dc4b6Seschrock 
422ea8dc4b6Seschrock 		rw_exit(&inject_lock);
423ea8dc4b6Seschrock 	}
424ea8dc4b6Seschrock 
425ea8dc4b6Seschrock 	/*
426ea8dc4b6Seschrock 	 * Flush the ARC, so that any attempts to read this data will end up
427ea8dc4b6Seschrock 	 * going to the ZIO layer.  Note that this is a little overkill, but
428ea8dc4b6Seschrock 	 * we don't have the necessary ARC interfaces to do anything else, and
429ea8dc4b6Seschrock 	 * fault injection isn't a performance critical path.
430ea8dc4b6Seschrock 	 */
431ea8dc4b6Seschrock 	if (flags & ZINJECT_FLUSH_ARC)
432874395d5Smaybee 		arc_flush(NULL);
433ea8dc4b6Seschrock 
434ea8dc4b6Seschrock 	return (0);
435ea8dc4b6Seschrock }
436ea8dc4b6Seschrock 
437ea8dc4b6Seschrock /*
438ea8dc4b6Seschrock  * Returns the next record with an ID greater than that supplied to the
439ea8dc4b6Seschrock  * function.  Used to iterate over all handlers in the system.
440ea8dc4b6Seschrock  */
441ea8dc4b6Seschrock int
442ea8dc4b6Seschrock zio_inject_list_next(int *id, char *name, size_t buflen,
443ea8dc4b6Seschrock     zinject_record_t *record)
444ea8dc4b6Seschrock {
445ea8dc4b6Seschrock 	inject_handler_t *handler;
446ea8dc4b6Seschrock 	int ret;
447ea8dc4b6Seschrock 
448ea8dc4b6Seschrock 	mutex_enter(&spa_namespace_lock);
449ea8dc4b6Seschrock 	rw_enter(&inject_lock, RW_READER);
450ea8dc4b6Seschrock 
451ea8dc4b6Seschrock 	for (handler = list_head(&inject_handlers); handler != NULL;
452ea8dc4b6Seschrock 	    handler = list_next(&inject_handlers, handler))
453ea8dc4b6Seschrock 		if (handler->zi_id > *id)
454ea8dc4b6Seschrock 			break;
455ea8dc4b6Seschrock 
456ea8dc4b6Seschrock 	if (handler) {
457ea8dc4b6Seschrock 		*record = handler->zi_record;
458ea8dc4b6Seschrock 		*id = handler->zi_id;
459ea8dc4b6Seschrock 		(void) strncpy(name, spa_name(handler->zi_spa), buflen);
460ea8dc4b6Seschrock 		ret = 0;
461ea8dc4b6Seschrock 	} else {
462ea8dc4b6Seschrock 		ret = ENOENT;
463ea8dc4b6Seschrock 	}
464ea8dc4b6Seschrock 
465ea8dc4b6Seschrock 	rw_exit(&inject_lock);
466ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
467ea8dc4b6Seschrock 
468ea8dc4b6Seschrock 	return (ret);
469ea8dc4b6Seschrock }
470ea8dc4b6Seschrock 
471ea8dc4b6Seschrock /*
472ea8dc4b6Seschrock  * Clear the fault handler with the given identifier, or return ENOENT if none
473ea8dc4b6Seschrock  * exists.
474ea8dc4b6Seschrock  */
475ea8dc4b6Seschrock int
476ea8dc4b6Seschrock zio_clear_fault(int id)
477ea8dc4b6Seschrock {
478ea8dc4b6Seschrock 	inject_handler_t *handler;
479ea8dc4b6Seschrock 	int ret;
480ea8dc4b6Seschrock 
481ea8dc4b6Seschrock 	rw_enter(&inject_lock, RW_WRITER);
482ea8dc4b6Seschrock 
483ea8dc4b6Seschrock 	for (handler = list_head(&inject_handlers); handler != NULL;
484ea8dc4b6Seschrock 	    handler = list_next(&inject_handlers, handler))
485ea8dc4b6Seschrock 		if (handler->zi_id == id)
486ea8dc4b6Seschrock 			break;
487ea8dc4b6Seschrock 
488ea8dc4b6Seschrock 	if (handler == NULL) {
489ea8dc4b6Seschrock 		ret = ENOENT;
490ea8dc4b6Seschrock 	} else {
491ea8dc4b6Seschrock 		list_remove(&inject_handlers, handler);
492ea8dc4b6Seschrock 		spa_inject_delref(handler->zi_spa);
493ea8dc4b6Seschrock 		kmem_free(handler, sizeof (inject_handler_t));
494ea8dc4b6Seschrock 		atomic_add_32(&zio_injection_enabled, -1);
495ea8dc4b6Seschrock 		ret = 0;
496ea8dc4b6Seschrock 	}
497ea8dc4b6Seschrock 
498ea8dc4b6Seschrock 	rw_exit(&inject_lock);
499ea8dc4b6Seschrock 
500ea8dc4b6Seschrock 	return (ret);
501ea8dc4b6Seschrock }
502ea8dc4b6Seschrock 
503ea8dc4b6Seschrock void
504ea8dc4b6Seschrock zio_inject_init(void)
505ea8dc4b6Seschrock {
5069b3f6b42SEric Kustarz 	rw_init(&inject_lock, NULL, RW_DEFAULT, NULL);
507ea8dc4b6Seschrock 	list_create(&inject_handlers, sizeof (inject_handler_t),
508ea8dc4b6Seschrock 	    offsetof(inject_handler_t, zi_link));
509ea8dc4b6Seschrock }
510ea8dc4b6Seschrock 
511ea8dc4b6Seschrock void
512ea8dc4b6Seschrock zio_inject_fini(void)
513ea8dc4b6Seschrock {
514ea8dc4b6Seschrock 	list_destroy(&inject_handlers);
5159b3f6b42SEric Kustarz 	rw_destroy(&inject_lock);
516ea8dc4b6Seschrock }
517