xref: /titanic_44/usr/src/cmd/zinject/zinject.c (revision 8f18d1fadf6a0c20fac9ff7259a5368faa3c3bfb)
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 /*
228956713aSEric Schrock  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23ea8dc4b6Seschrock  * Use is subject to license terms.
24ea8dc4b6Seschrock  */
25ea8dc4b6Seschrock 
26ea8dc4b6Seschrock /*
27ea8dc4b6Seschrock  * ZFS Fault Injector
28ea8dc4b6Seschrock  *
29ea8dc4b6Seschrock  * This userland component takes a set of options and uses libzpool to translate
30ea8dc4b6Seschrock  * from a user-visible object type and name to an internal representation.
31ea8dc4b6Seschrock  * There are two basic types of faults: device faults and data faults.
32ea8dc4b6Seschrock  *
33ea8dc4b6Seschrock  *
34ea8dc4b6Seschrock  * DEVICE FAULTS
35ea8dc4b6Seschrock  *
36ea8dc4b6Seschrock  * Errors can be injected into a particular vdev using the '-d' option.  This
37ea8dc4b6Seschrock  * option takes a path or vdev GUID to uniquely identify the device within a
38ea8dc4b6Seschrock  * pool.  There are two types of errors that can be injected, EIO and ENXIO,
3921bf64a7Sgw25295  * that can be controlled through the '-e' option.  The default is ENXIO.  For
40ea8dc4b6Seschrock  * EIO failures, any attempt to read data from the device will return EIO, but
41ea8dc4b6Seschrock  * subsequent attempt to reopen the device will succeed.  For ENXIO failures,
42ea8dc4b6Seschrock  * any attempt to read from the device will return EIO, but any attempt to
43ea8dc4b6Seschrock  * reopen the device will also return ENXIO.
4421bf64a7Sgw25295  * For label faults, the -L option must be specified. This allows faults
4521bf64a7Sgw25295  * to be injected into either the nvlist or uberblock region of all the labels
4621bf64a7Sgw25295  * for the specified device.
47ea8dc4b6Seschrock  *
48ea8dc4b6Seschrock  * This form of the command looks like:
49ea8dc4b6Seschrock  *
5021bf64a7Sgw25295  * 	zinject -d device [-e errno] [-L <uber | nvlist>] pool
51ea8dc4b6Seschrock  *
52ea8dc4b6Seschrock  *
53ea8dc4b6Seschrock  * DATA FAULTS
54ea8dc4b6Seschrock  *
55ea8dc4b6Seschrock  * We begin with a tuple of the form:
56ea8dc4b6Seschrock  *
57ea8dc4b6Seschrock  * 	<type,level,range,object>
58ea8dc4b6Seschrock  *
59ea8dc4b6Seschrock  * 	type	A string describing the type of data to target.  Each type
60ea8dc4b6Seschrock  * 		implicitly describes how to interpret 'object'. Currently,
61ea8dc4b6Seschrock  * 		the following values are supported:
62ea8dc4b6Seschrock  *
63ea8dc4b6Seschrock  * 		data		User data for a file
64ea8dc4b6Seschrock  * 		dnode		Dnode for a file or directory
65ea8dc4b6Seschrock  *
66ea8dc4b6Seschrock  *		The following MOS objects are special.  Instead of injecting
67ea8dc4b6Seschrock  *		errors on a particular object or blkid, we inject errors across
68ea8dc4b6Seschrock  *		all objects of the given type.
69ea8dc4b6Seschrock  *
70ea8dc4b6Seschrock  * 		mos		Any data in the MOS
71ea8dc4b6Seschrock  * 		mosdir		object directory
72ea8dc4b6Seschrock  * 		config		pool configuration
73ea8dc4b6Seschrock  * 		bplist		blkptr list
74ea8dc4b6Seschrock  * 		spacemap	spacemap
75ea8dc4b6Seschrock  * 		metaslab	metaslab
76ea8dc4b6Seschrock  * 		errlog		persistent error log
77ea8dc4b6Seschrock  *
78ea8dc4b6Seschrock  * 	level	Object level.  Defaults to '0', not applicable to all types.  If
79ea8dc4b6Seschrock  * 		a range is given, this corresponds to the indirect block
80ea8dc4b6Seschrock  * 		corresponding to the specific range.
81ea8dc4b6Seschrock  *
82ea8dc4b6Seschrock  *	range	A numerical range [start,end) within the object.  Defaults to
83ea8dc4b6Seschrock  *		the full size of the file.
84ea8dc4b6Seschrock  *
85ea8dc4b6Seschrock  * 	object	A string describing the logical location of the object.  For
86ea8dc4b6Seschrock  * 		files and directories (currently the only supported types),
87ea8dc4b6Seschrock  * 		this is the path of the object on disk.
88ea8dc4b6Seschrock  *
89ea8dc4b6Seschrock  * This is translated, via libzpool, into the following internal representation:
90ea8dc4b6Seschrock  *
91ea8dc4b6Seschrock  * 	<type,objset,object,level,range>
92ea8dc4b6Seschrock  *
93ea8dc4b6Seschrock  * These types should be self-explanatory.  This tuple is then passed to the
94ea8dc4b6Seschrock  * kernel via a special ioctl() to initiate fault injection for the given
95ea8dc4b6Seschrock  * object.  Note that 'type' is not strictly necessary for fault injection, but
96ea8dc4b6Seschrock  * is used when translating existing faults into a human-readable string.
97ea8dc4b6Seschrock  *
98ea8dc4b6Seschrock  *
99ea8dc4b6Seschrock  * The command itself takes one of the forms:
100ea8dc4b6Seschrock  *
101ea8dc4b6Seschrock  * 	zinject
102ea8dc4b6Seschrock  * 	zinject <-a | -u pool>
103ea8dc4b6Seschrock  * 	zinject -c <id|all>
104ea8dc4b6Seschrock  * 	zinject [-q] <-t type> [-f freq] [-u] [-a] [-m] [-e errno] [-l level]
105ea8dc4b6Seschrock  *	    [-r range] <object>
106ea8dc4b6Seschrock  * 	zinject [-f freq] [-a] [-m] [-u] -b objset:object:level:start:end pool
107ea8dc4b6Seschrock  *
108ea8dc4b6Seschrock  * With no arguments, the command prints all currently registered injection
109ea8dc4b6Seschrock  * handlers, with their numeric identifiers.
110ea8dc4b6Seschrock  *
111ea8dc4b6Seschrock  * The '-c' option will clear the given handler, or all handlers if 'all' is
112ea8dc4b6Seschrock  * specified.
113ea8dc4b6Seschrock  *
114ea8dc4b6Seschrock  * The '-e' option takes a string describing the errno to simulate.  This must
115ea8dc4b6Seschrock  * be either 'io' or 'checksum'.  In most cases this will result in the same
116ea8dc4b6Seschrock  * behavior, but RAID-Z will produce a different set of ereports for this
117ea8dc4b6Seschrock  * situation.
118ea8dc4b6Seschrock  *
119ea8dc4b6Seschrock  * The '-a', '-u', and '-m' flags toggle internal flush behavior.  If '-a' is
120ea8dc4b6Seschrock  * specified, then the ARC cache is flushed appropriately.  If '-u' is
121ea8dc4b6Seschrock  * specified, then the underlying SPA is unloaded.  Either of these flags can be
122ea8dc4b6Seschrock  * specified independently of any other handlers.  The '-m' flag automatically
123ea8dc4b6Seschrock  * does an unmount and remount of the underlying dataset to aid in flushing the
124ea8dc4b6Seschrock  * cache.
125ea8dc4b6Seschrock  *
126ea8dc4b6Seschrock  * The '-f' flag controls the frequency of errors injected, expressed as a
127ea8dc4b6Seschrock  * integer percentage between 1 and 100.  The default is 100.
128ea8dc4b6Seschrock  *
129ea8dc4b6Seschrock  * The this form is responsible for actually injecting the handler into the
130ea8dc4b6Seschrock  * framework.  It takes the arguments described above, translates them to the
131ea8dc4b6Seschrock  * internal tuple using libzpool, and then issues an ioctl() to register the
132ea8dc4b6Seschrock  * handler.
133ea8dc4b6Seschrock  *
134ea8dc4b6Seschrock  * The final form can target a specific bookmark, regardless of whether a
135ea8dc4b6Seschrock  * human-readable interface has been designed.  It allows developers to specify
136ea8dc4b6Seschrock  * a particular block by number.
137ea8dc4b6Seschrock  */
138ea8dc4b6Seschrock 
139ea8dc4b6Seschrock #include <errno.h>
140ea8dc4b6Seschrock #include <fcntl.h>
141ea8dc4b6Seschrock #include <stdio.h>
142ea8dc4b6Seschrock #include <stdlib.h>
143ea8dc4b6Seschrock #include <strings.h>
144ea8dc4b6Seschrock #include <unistd.h>
145ea8dc4b6Seschrock 
146ea8dc4b6Seschrock #include <sys/fs/zfs.h>
147ea8dc4b6Seschrock #include <sys/mount.h>
148ea8dc4b6Seschrock 
149ea8dc4b6Seschrock #include <libzfs.h>
150ea8dc4b6Seschrock 
151ea8dc4b6Seschrock #undef verify	/* both libzfs.h and zfs_context.h want to define this */
152ea8dc4b6Seschrock 
153ea8dc4b6Seschrock #include "zinject.h"
154ea8dc4b6Seschrock 
15599653d4eSeschrock libzfs_handle_t *g_zfs;
156ea8dc4b6Seschrock int zfs_fd;
157ea8dc4b6Seschrock 
158ea8dc4b6Seschrock #define	ECKSUM	EBADE
159ea8dc4b6Seschrock 
160ea8dc4b6Seschrock static const char *errtable[TYPE_INVAL] = {
161ea8dc4b6Seschrock 	"data",
162ea8dc4b6Seschrock 	"dnode",
163ea8dc4b6Seschrock 	"mos",
164ea8dc4b6Seschrock 	"mosdir",
165ea8dc4b6Seschrock 	"metaslab",
166ea8dc4b6Seschrock 	"config",
167ea8dc4b6Seschrock 	"bplist",
168ea8dc4b6Seschrock 	"spacemap",
16921bf64a7Sgw25295 	"errlog",
17021bf64a7Sgw25295 	"uber",
17121bf64a7Sgw25295 	"nvlist"
172ea8dc4b6Seschrock };
173ea8dc4b6Seschrock 
174ea8dc4b6Seschrock static err_type_t
175ea8dc4b6Seschrock name_to_type(const char *arg)
176ea8dc4b6Seschrock {
177ea8dc4b6Seschrock 	int i;
178ea8dc4b6Seschrock 	for (i = 0; i < TYPE_INVAL; i++)
179ea8dc4b6Seschrock 		if (strcmp(errtable[i], arg) == 0)
180ea8dc4b6Seschrock 			return (i);
181ea8dc4b6Seschrock 
182ea8dc4b6Seschrock 	return (TYPE_INVAL);
183ea8dc4b6Seschrock }
184ea8dc4b6Seschrock 
185ea8dc4b6Seschrock static const char *
186ea8dc4b6Seschrock type_to_name(uint64_t type)
187ea8dc4b6Seschrock {
188ea8dc4b6Seschrock 	switch (type) {
189ea8dc4b6Seschrock 	case DMU_OT_OBJECT_DIRECTORY:
190ea8dc4b6Seschrock 		return ("mosdir");
191ea8dc4b6Seschrock 	case DMU_OT_OBJECT_ARRAY:
192ea8dc4b6Seschrock 		return ("metaslab");
193ea8dc4b6Seschrock 	case DMU_OT_PACKED_NVLIST:
194ea8dc4b6Seschrock 		return ("config");
195ea8dc4b6Seschrock 	case DMU_OT_BPLIST:
196ea8dc4b6Seschrock 		return ("bplist");
197ea8dc4b6Seschrock 	case DMU_OT_SPACE_MAP:
198ea8dc4b6Seschrock 		return ("spacemap");
199ea8dc4b6Seschrock 	case DMU_OT_ERROR_LOG:
200ea8dc4b6Seschrock 		return ("errlog");
201ea8dc4b6Seschrock 	default:
202ea8dc4b6Seschrock 		return ("-");
203ea8dc4b6Seschrock 	}
204ea8dc4b6Seschrock }
205ea8dc4b6Seschrock 
206ea8dc4b6Seschrock 
207ea8dc4b6Seschrock /*
208ea8dc4b6Seschrock  * Print usage message.
209ea8dc4b6Seschrock  */
210ea8dc4b6Seschrock void
211ea8dc4b6Seschrock usage(void)
212ea8dc4b6Seschrock {
213ea8dc4b6Seschrock 	(void) printf(
214ea8dc4b6Seschrock 	    "usage:\n"
215ea8dc4b6Seschrock 	    "\n"
216ea8dc4b6Seschrock 	    "\tzinject\n"
217ea8dc4b6Seschrock 	    "\n"
218ea8dc4b6Seschrock 	    "\t\tList all active injection records.\n"
219ea8dc4b6Seschrock 	    "\n"
220ea8dc4b6Seschrock 	    "\tzinject -c <id|all>\n"
221ea8dc4b6Seschrock 	    "\n"
222ea8dc4b6Seschrock 	    "\t\tClear the particular record (if given a numeric ID), or\n"
223ea8dc4b6Seschrock 	    "\t\tall records if 'all' is specificed.\n"
224ea8dc4b6Seschrock 	    "\n"
22588ecc943SGeorge Wilson 	    "\tzinject -p <function name> pool\n"
22688ecc943SGeorge Wilson 	    "\t\tInject a panic fault at the specified function. Only \n"
22788ecc943SGeorge Wilson 	    "\t\tfunctions which call spa_vdev_config_exit(), or \n"
22888ecc943SGeorge Wilson 	    "\t\tspa_vdev_exit() will trigger a panic.\n"
22988ecc943SGeorge Wilson 	    "\n"
230*8f18d1faSGeorge Wilson 	    "\tzinject -d device [-e errno] [-L <nvlist|uber>] [-F]\n"
231*8f18d1faSGeorge Wilson 	    "\t    [-T <read|write|free|claim|all> pool\n"
23221bf64a7Sgw25295 	    "\t\tInject a fault into a particular device or the device's\n"
23321bf64a7Sgw25295 	    "\t\tlabel.  Label injection can either be 'nvlist' or 'uber'.\n"
23421bf64a7Sgw25295 	    "\t\t'errno' can either be 'nxio' (the default) or 'io'.\n"
235ea8dc4b6Seschrock 	    "\n"
236*8f18d1faSGeorge Wilson 	    "\tzinject -d device -A <degrade|fault> pool\n"
237*8f18d1faSGeorge Wilson 	    "\t\tPerform a specific action on a particular device\n"
238*8f18d1faSGeorge Wilson 	    "\n"
239ea8dc4b6Seschrock 	    "\tzinject -b objset:object:level:blkid pool\n"
240ea8dc4b6Seschrock 	    "\n"
241ea8dc4b6Seschrock 	    "\t\tInject an error into pool 'pool' with the numeric bookmark\n"
242ea8dc4b6Seschrock 	    "\t\tspecified by the remaining tuple.  Each number is in\n"
243ea8dc4b6Seschrock 	    "\t\thexidecimal, and only one block can be specified.\n"
244ea8dc4b6Seschrock 	    "\n"
245ea8dc4b6Seschrock 	    "\tzinject [-q] <-t type> [-e errno] [-l level] [-r range]\n"
246ea8dc4b6Seschrock 	    "\t    [-a] [-m] [-u] [-f freq] <object>\n"
247ea8dc4b6Seschrock 	    "\n"
248ea8dc4b6Seschrock 	    "\t\tInject an error into the object specified by the '-t' option\n"
249ea8dc4b6Seschrock 	    "\t\tand the object descriptor.  The 'object' parameter is\n"
250ea8dc4b6Seschrock 	    "\t\tinterperted depending on the '-t' option.\n"
251ea8dc4b6Seschrock 	    "\n"
252ea8dc4b6Seschrock 	    "\t\t-q\tQuiet mode.  Only print out the handler number added.\n"
253ea8dc4b6Seschrock 	    "\t\t-e\tInject a specific error.  Must be either 'io' or\n"
254ea8dc4b6Seschrock 	    "\t\t\t'checksum'.  Default is 'io'.\n"
255ea8dc4b6Seschrock 	    "\t\t-l\tInject error at a particular block level. Default is "
256ea8dc4b6Seschrock 	    "0.\n"
257ea8dc4b6Seschrock 	    "\t\t-m\tAutomatically remount underlying filesystem.\n"
258ea8dc4b6Seschrock 	    "\t\t-r\tInject error over a particular logical range of an\n"
259ea8dc4b6Seschrock 	    "\t\t\tobject.  Will be translated to the appropriate blkid\n"
260ea8dc4b6Seschrock 	    "\t\t\trange according to the object's properties.\n"
261ea8dc4b6Seschrock 	    "\t\t-a\tFlush the ARC cache.  Can be specified without any\n"
262ea8dc4b6Seschrock 	    "\t\t\tassociated object.\n"
263ea8dc4b6Seschrock 	    "\t\t-u\tUnload the associated pool.  Can be specified with only\n"
264ea8dc4b6Seschrock 	    "\t\t\ta pool object.\n"
265ea8dc4b6Seschrock 	    "\t\t-f\tOnly inject errors a fraction of the time.  Expressed as\n"
266ea8dc4b6Seschrock 	    "\t\t\ta percentage between 1 and 100.\n"
267ea8dc4b6Seschrock 	    "\n"
268ea8dc4b6Seschrock 	    "\t-t data\t\tInject an error into the plain file contents of a\n"
269ea8dc4b6Seschrock 	    "\t\t\tfile.  The object must be specified as a complete path\n"
270ea8dc4b6Seschrock 	    "\t\t\tto a file on a ZFS filesystem.\n"
271ea8dc4b6Seschrock 	    "\n"
272ea8dc4b6Seschrock 	    "\t-t dnode\tInject an error into the metadnode in the block\n"
273ea8dc4b6Seschrock 	    "\t\t\tcorresponding to the dnode for a file or directory.  The\n"
274ea8dc4b6Seschrock 	    "\t\t\t'-r' option is incompatible with this mode.  The object\n"
275ea8dc4b6Seschrock 	    "\t\t\tis specified as a complete path to a file or directory\n"
276ea8dc4b6Seschrock 	    "\t\t\ton a ZFS filesystem.\n"
277ea8dc4b6Seschrock 	    "\n"
278ea8dc4b6Seschrock 	    "\t-t <mos>\tInject errors into the MOS for objects of the given\n"
279ea8dc4b6Seschrock 	    "\t\t\ttype.  Valid types are: mos, mosdir, config, bplist,\n"
28055434c77Sek110237 	    "\t\t\tspacemap, metaslab, errlog.  The only valid <object> is\n"
28155434c77Sek110237 	    "\t\t\tthe poolname.\n");
282ea8dc4b6Seschrock }
283ea8dc4b6Seschrock 
284ea8dc4b6Seschrock static int
285ea8dc4b6Seschrock iter_handlers(int (*func)(int, const char *, zinject_record_t *, void *),
286ea8dc4b6Seschrock     void *data)
287ea8dc4b6Seschrock {
288ea8dc4b6Seschrock 	zfs_cmd_t zc;
289ea8dc4b6Seschrock 	int ret;
290ea8dc4b6Seschrock 
291ea8dc4b6Seschrock 	zc.zc_guid = 0;
292ea8dc4b6Seschrock 
293ea8dc4b6Seschrock 	while (ioctl(zfs_fd, ZFS_IOC_INJECT_LIST_NEXT, &zc) == 0)
294ea8dc4b6Seschrock 		if ((ret = func((int)zc.zc_guid, zc.zc_name,
295ea8dc4b6Seschrock 		    &zc.zc_inject_record, data)) != 0)
296ea8dc4b6Seschrock 			return (ret);
297ea8dc4b6Seschrock 
298ea8dc4b6Seschrock 	return (0);
299ea8dc4b6Seschrock }
300ea8dc4b6Seschrock 
301ea8dc4b6Seschrock static int
302ea8dc4b6Seschrock print_data_handler(int id, const char *pool, zinject_record_t *record,
303ea8dc4b6Seschrock     void *data)
304ea8dc4b6Seschrock {
305ea8dc4b6Seschrock 	int *count = data;
306ea8dc4b6Seschrock 
30788ecc943SGeorge Wilson 	if (record->zi_guid != 0 || record->zi_func[0] != '\0')
308ea8dc4b6Seschrock 		return (0);
309ea8dc4b6Seschrock 
310ea8dc4b6Seschrock 	if (*count == 0) {
311ea8dc4b6Seschrock 		(void) printf("%3s  %-15s  %-6s  %-6s  %-8s  %3s  %-15s\n",
312ea8dc4b6Seschrock 		    "ID", "POOL", "OBJSET", "OBJECT", "TYPE", "LVL",  "RANGE");
313ea8dc4b6Seschrock 		(void) printf("---  ---------------  ------  "
314ea8dc4b6Seschrock 		    "------  --------  ---  ---------------\n");
315ea8dc4b6Seschrock 	}
316ea8dc4b6Seschrock 
317ea8dc4b6Seschrock 	*count += 1;
318ea8dc4b6Seschrock 
319ea8dc4b6Seschrock 	(void) printf("%3d  %-15s  %-6llu  %-6llu  %-8s  %3d  ", id, pool,
320ea8dc4b6Seschrock 	    (u_longlong_t)record->zi_objset, (u_longlong_t)record->zi_object,
321ea8dc4b6Seschrock 	    type_to_name(record->zi_type), record->zi_level);
322ea8dc4b6Seschrock 
323ea8dc4b6Seschrock 	if (record->zi_start == 0 &&
324ea8dc4b6Seschrock 	    record->zi_end == -1ULL)
325ea8dc4b6Seschrock 		(void) printf("all\n");
326ea8dc4b6Seschrock 	else
327ea8dc4b6Seschrock 		(void) printf("[%llu, %llu]\n", (u_longlong_t)record->zi_start,
328ea8dc4b6Seschrock 		    (u_longlong_t)record->zi_end);
329ea8dc4b6Seschrock 
330ea8dc4b6Seschrock 	return (0);
331ea8dc4b6Seschrock }
332ea8dc4b6Seschrock 
333ea8dc4b6Seschrock static int
334ea8dc4b6Seschrock print_device_handler(int id, const char *pool, zinject_record_t *record,
335ea8dc4b6Seschrock     void *data)
336ea8dc4b6Seschrock {
337ea8dc4b6Seschrock 	int *count = data;
338ea8dc4b6Seschrock 
33988ecc943SGeorge Wilson 	if (record->zi_guid == 0 || record->zi_func[0] != '\0')
340ea8dc4b6Seschrock 		return (0);
341ea8dc4b6Seschrock 
342ea8dc4b6Seschrock 	if (*count == 0) {
343ea8dc4b6Seschrock 		(void) printf("%3s  %-15s  %s\n", "ID", "POOL", "GUID");
344ea8dc4b6Seschrock 		(void) printf("---  ---------------  ----------------\n");
345ea8dc4b6Seschrock 	}
346ea8dc4b6Seschrock 
347ea8dc4b6Seschrock 	*count += 1;
348ea8dc4b6Seschrock 
349ea8dc4b6Seschrock 	(void) printf("%3d  %-15s  %llx\n", id, pool,
350ea8dc4b6Seschrock 	    (u_longlong_t)record->zi_guid);
351ea8dc4b6Seschrock 
352ea8dc4b6Seschrock 	return (0);
353ea8dc4b6Seschrock }
354ea8dc4b6Seschrock 
35588ecc943SGeorge Wilson static int
35688ecc943SGeorge Wilson print_panic_handler(int id, const char *pool, zinject_record_t *record,
35788ecc943SGeorge Wilson     void *data)
35888ecc943SGeorge Wilson {
35988ecc943SGeorge Wilson 	int *count = data;
36088ecc943SGeorge Wilson 
36188ecc943SGeorge Wilson 	if (record->zi_func[0] == '\0')
36288ecc943SGeorge Wilson 		return (0);
36388ecc943SGeorge Wilson 
36488ecc943SGeorge Wilson 	if (*count == 0) {
36588ecc943SGeorge Wilson 		(void) printf("%3s  %-15s  %s\n", "ID", "POOL", "FUNCTION");
36688ecc943SGeorge Wilson 		(void) printf("---  ---------------  ----------------\n");
36788ecc943SGeorge Wilson 	}
36888ecc943SGeorge Wilson 
36988ecc943SGeorge Wilson 	*count += 1;
37088ecc943SGeorge Wilson 
37188ecc943SGeorge Wilson 	(void) printf("%3d  %-15s  %s\n", id, pool, record->zi_func);
37288ecc943SGeorge Wilson 
37388ecc943SGeorge Wilson 	return (0);
37488ecc943SGeorge Wilson }
37588ecc943SGeorge Wilson 
376ea8dc4b6Seschrock /*
377ea8dc4b6Seschrock  * Print all registered error handlers.  Returns the number of handlers
378ea8dc4b6Seschrock  * registered.
379ea8dc4b6Seschrock  */
380ea8dc4b6Seschrock static int
381ea8dc4b6Seschrock print_all_handlers(void)
382ea8dc4b6Seschrock {
383ea8dc4b6Seschrock 	int count = 0;
384ea8dc4b6Seschrock 
385ea8dc4b6Seschrock 	(void) iter_handlers(print_device_handler, &count);
386ea8dc4b6Seschrock 	(void) printf("\n");
387ea8dc4b6Seschrock 	count = 0;
388ea8dc4b6Seschrock 	(void) iter_handlers(print_data_handler, &count);
38988ecc943SGeorge Wilson 	(void) printf("\n");
39088ecc943SGeorge Wilson 	count = 0;
39188ecc943SGeorge Wilson 	(void) iter_handlers(print_panic_handler, &count);
392ea8dc4b6Seschrock 
393ea8dc4b6Seschrock 	return (count);
394ea8dc4b6Seschrock }
395ea8dc4b6Seschrock 
396ea8dc4b6Seschrock /* ARGSUSED */
397ea8dc4b6Seschrock static int
398ea8dc4b6Seschrock cancel_one_handler(int id, const char *pool, zinject_record_t *record,
399ea8dc4b6Seschrock     void *data)
400ea8dc4b6Seschrock {
401ea8dc4b6Seschrock 	zfs_cmd_t zc;
402ea8dc4b6Seschrock 
403ea8dc4b6Seschrock 	zc.zc_guid = (uint64_t)id;
404ea8dc4b6Seschrock 
405ea8dc4b6Seschrock 	if (ioctl(zfs_fd, ZFS_IOC_CLEAR_FAULT, &zc) != 0) {
406ea8dc4b6Seschrock 		(void) fprintf(stderr, "failed to remove handler %d: %s\n",
407ea8dc4b6Seschrock 		    id, strerror(errno));
408ea8dc4b6Seschrock 		return (1);
409ea8dc4b6Seschrock 	}
410ea8dc4b6Seschrock 
411ea8dc4b6Seschrock 	return (0);
412ea8dc4b6Seschrock }
413ea8dc4b6Seschrock 
414ea8dc4b6Seschrock /*
415ea8dc4b6Seschrock  * Remove all fault injection handlers.
416ea8dc4b6Seschrock  */
417ea8dc4b6Seschrock static int
418ea8dc4b6Seschrock cancel_all_handlers(void)
419ea8dc4b6Seschrock {
420ea8dc4b6Seschrock 	int ret = iter_handlers(cancel_one_handler, NULL);
421ea8dc4b6Seschrock 
422ea8dc4b6Seschrock 	(void) printf("removed all registered handlers\n");
423ea8dc4b6Seschrock 
424ea8dc4b6Seschrock 	return (ret);
425ea8dc4b6Seschrock }
426ea8dc4b6Seschrock 
427ea8dc4b6Seschrock /*
428ea8dc4b6Seschrock  * Remove a specific fault injection handler.
429ea8dc4b6Seschrock  */
430ea8dc4b6Seschrock static int
431ea8dc4b6Seschrock cancel_handler(int id)
432ea8dc4b6Seschrock {
433ea8dc4b6Seschrock 	zfs_cmd_t zc;
434ea8dc4b6Seschrock 
435ea8dc4b6Seschrock 	zc.zc_guid = (uint64_t)id;
436ea8dc4b6Seschrock 
437ea8dc4b6Seschrock 	if (ioctl(zfs_fd, ZFS_IOC_CLEAR_FAULT, &zc) != 0) {
438ea8dc4b6Seschrock 		(void) fprintf(stderr, "failed to remove handler %d: %s\n",
439ea8dc4b6Seschrock 		    id, strerror(errno));
440ea8dc4b6Seschrock 		return (1);
441ea8dc4b6Seschrock 	}
442ea8dc4b6Seschrock 
443ea8dc4b6Seschrock 	(void) printf("removed handler %d\n", id);
444ea8dc4b6Seschrock 
445ea8dc4b6Seschrock 	return (0);
446ea8dc4b6Seschrock }
447ea8dc4b6Seschrock 
448ea8dc4b6Seschrock /*
449ea8dc4b6Seschrock  * Register a new fault injection handler.
450ea8dc4b6Seschrock  */
451ea8dc4b6Seschrock static int
452ea8dc4b6Seschrock register_handler(const char *pool, int flags, zinject_record_t *record,
453ea8dc4b6Seschrock     int quiet)
454ea8dc4b6Seschrock {
455ea8dc4b6Seschrock 	zfs_cmd_t zc;
456ea8dc4b6Seschrock 
457ea8dc4b6Seschrock 	(void) strcpy(zc.zc_name, pool);
458ea8dc4b6Seschrock 	zc.zc_inject_record = *record;
459ea8dc4b6Seschrock 	zc.zc_guid = flags;
460ea8dc4b6Seschrock 
461ea8dc4b6Seschrock 	if (ioctl(zfs_fd, ZFS_IOC_INJECT_FAULT, &zc) != 0) {
462ea8dc4b6Seschrock 		(void) fprintf(stderr, "failed to add handler: %s\n",
463ea8dc4b6Seschrock 		    strerror(errno));
464ea8dc4b6Seschrock 		return (1);
465ea8dc4b6Seschrock 	}
466ea8dc4b6Seschrock 
467ea8dc4b6Seschrock 	if (flags & ZINJECT_NULL)
468ea8dc4b6Seschrock 		return (0);
469ea8dc4b6Seschrock 
470ea8dc4b6Seschrock 	if (quiet) {
471ea8dc4b6Seschrock 		(void) printf("%llu\n", (u_longlong_t)zc.zc_guid);
472ea8dc4b6Seschrock 	} else {
473ea8dc4b6Seschrock 		(void) printf("Added handler %llu with the following "
474ea8dc4b6Seschrock 		    "properties:\n", (u_longlong_t)zc.zc_guid);
475ea8dc4b6Seschrock 		(void) printf("  pool: %s\n", pool);
476ea8dc4b6Seschrock 		if (record->zi_guid) {
477ea8dc4b6Seschrock 			(void) printf("  vdev: %llx\n",
478ea8dc4b6Seschrock 			    (u_longlong_t)record->zi_guid);
47988ecc943SGeorge Wilson 		} else if (record->zi_func[0] != '\0') {
48088ecc943SGeorge Wilson 			(void) printf("  panic function: %s\n",
48188ecc943SGeorge Wilson 			    record->zi_func);
482ea8dc4b6Seschrock 		} else {
483ea8dc4b6Seschrock 			(void) printf("objset: %llu\n",
484ea8dc4b6Seschrock 			    (u_longlong_t)record->zi_objset);
485ea8dc4b6Seschrock 			(void) printf("object: %llu\n",
486ea8dc4b6Seschrock 			    (u_longlong_t)record->zi_object);
487ea8dc4b6Seschrock 			(void) printf("  type: %llu\n",
488ea8dc4b6Seschrock 			    (u_longlong_t)record->zi_type);
489ea8dc4b6Seschrock 			(void) printf(" level: %d\n", record->zi_level);
490ea8dc4b6Seschrock 			if (record->zi_start == 0 &&
491ea8dc4b6Seschrock 			    record->zi_end == -1ULL)
492ea8dc4b6Seschrock 				(void) printf(" range: all\n");
493ea8dc4b6Seschrock 			else
494ea8dc4b6Seschrock 				(void) printf(" range: [%llu, %llu)\n",
495ea8dc4b6Seschrock 				    (u_longlong_t)record->zi_start,
496ea8dc4b6Seschrock 				    (u_longlong_t)record->zi_end);
497ea8dc4b6Seschrock 		}
498ea8dc4b6Seschrock 	}
499ea8dc4b6Seschrock 
500ea8dc4b6Seschrock 	return (0);
501ea8dc4b6Seschrock }
502ea8dc4b6Seschrock 
503ea8dc4b6Seschrock int
504*8f18d1faSGeorge Wilson perform_action(const char *pool, zinject_record_t *record, int cmd)
505*8f18d1faSGeorge Wilson {
506*8f18d1faSGeorge Wilson 	zfs_cmd_t zc;
507*8f18d1faSGeorge Wilson 
508*8f18d1faSGeorge Wilson 	ASSERT(cmd == VDEV_STATE_DEGRADED || cmd == VDEV_STATE_FAULTED);
509*8f18d1faSGeorge Wilson 	(void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
510*8f18d1faSGeorge Wilson 	zc.zc_guid = record->zi_guid;
511*8f18d1faSGeorge Wilson 	zc.zc_cookie = cmd;
512*8f18d1faSGeorge Wilson 
513*8f18d1faSGeorge Wilson 	if (ioctl(zfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
514*8f18d1faSGeorge Wilson 		return (0);
515*8f18d1faSGeorge Wilson 
516*8f18d1faSGeorge Wilson 	return (1);
517*8f18d1faSGeorge Wilson }
518*8f18d1faSGeorge Wilson 
519*8f18d1faSGeorge Wilson int
520ea8dc4b6Seschrock main(int argc, char **argv)
521ea8dc4b6Seschrock {
522ea8dc4b6Seschrock 	int c;
523ea8dc4b6Seschrock 	char *range = NULL;
524ea8dc4b6Seschrock 	char *cancel = NULL;
525ea8dc4b6Seschrock 	char *end;
526ea8dc4b6Seschrock 	char *raw = NULL;
527ea8dc4b6Seschrock 	char *device = NULL;
528ea8dc4b6Seschrock 	int level = 0;
529ea8dc4b6Seschrock 	int quiet = 0;
530ea8dc4b6Seschrock 	int error = 0;
531ea8dc4b6Seschrock 	int domount = 0;
532*8f18d1faSGeorge Wilson 	int io_type = ZIO_TYPES;
533*8f18d1faSGeorge Wilson 	int action = VDEV_STATE_UNKNOWN;
534ea8dc4b6Seschrock 	err_type_t type = TYPE_INVAL;
53521bf64a7Sgw25295 	err_type_t label = TYPE_INVAL;
536ea8dc4b6Seschrock 	zinject_record_t record = { 0 };
537ea8dc4b6Seschrock 	char pool[MAXNAMELEN];
538ea8dc4b6Seschrock 	char dataset[MAXNAMELEN];
539ea8dc4b6Seschrock 	zfs_handle_t *zhp;
540ea8dc4b6Seschrock 	int ret;
541ea8dc4b6Seschrock 	int flags = 0;
542ea8dc4b6Seschrock 
54399653d4eSeschrock 	if ((g_zfs = libzfs_init()) == NULL) {
54499653d4eSeschrock 		(void) fprintf(stderr, "internal error: failed to "
54599653d4eSeschrock 		    "initialize ZFS library\n");
54699653d4eSeschrock 		return (1);
54799653d4eSeschrock 	}
54899653d4eSeschrock 
54999653d4eSeschrock 	libzfs_print_on_error(g_zfs, B_TRUE);
55099653d4eSeschrock 
551ea8dc4b6Seschrock 	if ((zfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
552ea8dc4b6Seschrock 		(void) fprintf(stderr, "failed to open ZFS device\n");
553ea8dc4b6Seschrock 		return (1);
554ea8dc4b6Seschrock 	}
555ea8dc4b6Seschrock 
556ea8dc4b6Seschrock 	if (argc == 1) {
557ea8dc4b6Seschrock 		/*
558ea8dc4b6Seschrock 		 * No arguments.  Print the available handlers.  If there are no
559ea8dc4b6Seschrock 		 * available handlers, direct the user to '-h' for help
560ea8dc4b6Seschrock 		 * information.
561ea8dc4b6Seschrock 		 */
562ea8dc4b6Seschrock 		if (print_all_handlers() == 0) {
563ea8dc4b6Seschrock 			(void) printf("No handlers registered.\n");
564ea8dc4b6Seschrock 			(void) printf("Run 'zinject -h' for usage "
565ea8dc4b6Seschrock 			    "information.\n");
566ea8dc4b6Seschrock 		}
567ea8dc4b6Seschrock 
568ea8dc4b6Seschrock 		return (0);
569ea8dc4b6Seschrock 	}
570ea8dc4b6Seschrock 
571*8f18d1faSGeorge Wilson 	while ((c = getopt(argc, argv,
572*8f18d1faSGeorge Wilson 	    ":aA:b:d:f:Fqhc:t:T:l:mr:e:uL:p:")) != -1) {
573ea8dc4b6Seschrock 		switch (c) {
574ea8dc4b6Seschrock 		case 'a':
575ea8dc4b6Seschrock 			flags |= ZINJECT_FLUSH_ARC;
576ea8dc4b6Seschrock 			break;
577*8f18d1faSGeorge Wilson 		case 'A':
578*8f18d1faSGeorge Wilson 			if (strcasecmp(optarg, "degrade") == 0) {
579*8f18d1faSGeorge Wilson 				action = VDEV_STATE_DEGRADED;
580*8f18d1faSGeorge Wilson 			} else if (strcasecmp(optarg, "fault") == 0) {
581*8f18d1faSGeorge Wilson 				action = VDEV_STATE_FAULTED;
582*8f18d1faSGeorge Wilson 			} else {
583*8f18d1faSGeorge Wilson 				(void) fprintf(stderr, "invalid action '%s': "
584*8f18d1faSGeorge Wilson 				    "must be 'degrade' or 'fault'\n", optarg);
585*8f18d1faSGeorge Wilson 				usage();
586*8f18d1faSGeorge Wilson 				return (1);
587*8f18d1faSGeorge Wilson 			}
588*8f18d1faSGeorge Wilson 			break;
589ea8dc4b6Seschrock 		case 'b':
590ea8dc4b6Seschrock 			raw = optarg;
591ea8dc4b6Seschrock 			break;
592ea8dc4b6Seschrock 		case 'c':
593ea8dc4b6Seschrock 			cancel = optarg;
594ea8dc4b6Seschrock 			break;
595ea8dc4b6Seschrock 		case 'd':
596ea8dc4b6Seschrock 			device = optarg;
597ea8dc4b6Seschrock 			break;
598ea8dc4b6Seschrock 		case 'e':
599ea8dc4b6Seschrock 			if (strcasecmp(optarg, "io") == 0) {
600ea8dc4b6Seschrock 				error = EIO;
601ea8dc4b6Seschrock 			} else if (strcasecmp(optarg, "checksum") == 0) {
602ea8dc4b6Seschrock 				error = ECKSUM;
603ea8dc4b6Seschrock 			} else if (strcasecmp(optarg, "nxio") == 0) {
604ea8dc4b6Seschrock 				error = ENXIO;
605ea8dc4b6Seschrock 			} else {
606ea8dc4b6Seschrock 				(void) fprintf(stderr, "invalid error type "
607ea8dc4b6Seschrock 				    "'%s': must be 'io', 'checksum' or "
608ea8dc4b6Seschrock 				    "'nxio'\n", optarg);
609ea8dc4b6Seschrock 				usage();
610ea8dc4b6Seschrock 				return (1);
611ea8dc4b6Seschrock 			}
612ea8dc4b6Seschrock 			break;
613ea8dc4b6Seschrock 		case 'f':
614ea8dc4b6Seschrock 			record.zi_freq = atoi(optarg);
615ea8dc4b6Seschrock 			if (record.zi_freq < 1 || record.zi_freq > 100) {
616ea8dc4b6Seschrock 				(void) fprintf(stderr, "frequency range must "
617ea8dc4b6Seschrock 				    "be in the range (0, 100]\n");
618ea8dc4b6Seschrock 				return (1);
619ea8dc4b6Seschrock 			}
620ea8dc4b6Seschrock 			break;
6218956713aSEric Schrock 		case 'F':
6228956713aSEric Schrock 			record.zi_failfast = B_TRUE;
6238956713aSEric Schrock 			break;
624ea8dc4b6Seschrock 		case 'h':
625ea8dc4b6Seschrock 			usage();
626ea8dc4b6Seschrock 			return (0);
627ea8dc4b6Seschrock 		case 'l':
628ea8dc4b6Seschrock 			level = (int)strtol(optarg, &end, 10);
629ea8dc4b6Seschrock 			if (*end != '\0') {
630ea8dc4b6Seschrock 				(void) fprintf(stderr, "invalid level '%s': "
631ea8dc4b6Seschrock 				    "must be an integer\n", optarg);
632ea8dc4b6Seschrock 				usage();
633ea8dc4b6Seschrock 				return (1);
634ea8dc4b6Seschrock 			}
635ea8dc4b6Seschrock 			break;
636ea8dc4b6Seschrock 		case 'm':
637ea8dc4b6Seschrock 			domount = 1;
638ea8dc4b6Seschrock 			break;
63988ecc943SGeorge Wilson 		case 'p':
64088ecc943SGeorge Wilson 			(void) strlcpy(record.zi_func, optarg,
64188ecc943SGeorge Wilson 			    sizeof (record.zi_func));
64288ecc943SGeorge Wilson 			break;
643ea8dc4b6Seschrock 		case 'q':
644ea8dc4b6Seschrock 			quiet = 1;
645ea8dc4b6Seschrock 			break;
646ea8dc4b6Seschrock 		case 'r':
647ea8dc4b6Seschrock 			range = optarg;
648ea8dc4b6Seschrock 			break;
649*8f18d1faSGeorge Wilson 		case 'T':
650*8f18d1faSGeorge Wilson 			if (strcasecmp(optarg, "read") == 0) {
651*8f18d1faSGeorge Wilson 				io_type = ZIO_TYPE_READ;
652*8f18d1faSGeorge Wilson 			} else if (strcasecmp(optarg, "write") == 0) {
653*8f18d1faSGeorge Wilson 				io_type = ZIO_TYPE_WRITE;
654*8f18d1faSGeorge Wilson 			} else if (strcasecmp(optarg, "free") == 0) {
655*8f18d1faSGeorge Wilson 				io_type = ZIO_TYPE_FREE;
656*8f18d1faSGeorge Wilson 			} else if (strcasecmp(optarg, "claim") == 0) {
657*8f18d1faSGeorge Wilson 				io_type = ZIO_TYPE_CLAIM;
658*8f18d1faSGeorge Wilson 			} else if (strcasecmp(optarg, "all") == 0) {
659*8f18d1faSGeorge Wilson 				io_type = ZIO_TYPES;
660*8f18d1faSGeorge Wilson 			} else {
661*8f18d1faSGeorge Wilson 				(void) fprintf(stderr, "invalid I/O type "
662*8f18d1faSGeorge Wilson 				    "'%s': must be 'read', 'write', 'free', "
663*8f18d1faSGeorge Wilson 				    "'claim' or 'all'\n", optarg);
664*8f18d1faSGeorge Wilson 				usage();
665*8f18d1faSGeorge Wilson 				return (1);
666*8f18d1faSGeorge Wilson 			}
667*8f18d1faSGeorge Wilson 			break;
668ea8dc4b6Seschrock 		case 't':
66921bf64a7Sgw25295 			if ((type = name_to_type(optarg)) == TYPE_INVAL &&
67021bf64a7Sgw25295 			    !MOS_TYPE(type)) {
671ea8dc4b6Seschrock 				(void) fprintf(stderr, "invalid type '%s'\n",
672ea8dc4b6Seschrock 				    optarg);
673ea8dc4b6Seschrock 				usage();
674ea8dc4b6Seschrock 				return (1);
675ea8dc4b6Seschrock 			}
676ea8dc4b6Seschrock 			break;
677ea8dc4b6Seschrock 		case 'u':
678ea8dc4b6Seschrock 			flags |= ZINJECT_UNLOAD_SPA;
679ea8dc4b6Seschrock 			break;
68021bf64a7Sgw25295 		case 'L':
68121bf64a7Sgw25295 			if ((label = name_to_type(optarg)) == TYPE_INVAL &&
68221bf64a7Sgw25295 			    !LABEL_TYPE(type)) {
68321bf64a7Sgw25295 				(void) fprintf(stderr, "invalid label type "
68421bf64a7Sgw25295 				    "'%s'\n", optarg);
68521bf64a7Sgw25295 				usage();
68621bf64a7Sgw25295 				return (1);
68721bf64a7Sgw25295 			}
68821bf64a7Sgw25295 			break;
689ea8dc4b6Seschrock 		case ':':
690ea8dc4b6Seschrock 			(void) fprintf(stderr, "option -%c requires an "
691ea8dc4b6Seschrock 			    "operand\n", optopt);
692ea8dc4b6Seschrock 			usage();
693ea8dc4b6Seschrock 			return (1);
694ea8dc4b6Seschrock 		case '?':
695ea8dc4b6Seschrock 			(void) fprintf(stderr, "invalid option '%c'\n",
696ea8dc4b6Seschrock 			    optopt);
697ea8dc4b6Seschrock 			usage();
698ea8dc4b6Seschrock 			return (2);
699ea8dc4b6Seschrock 		}
700ea8dc4b6Seschrock 	}
701ea8dc4b6Seschrock 
702ea8dc4b6Seschrock 	argc -= optind;
703ea8dc4b6Seschrock 	argv += optind;
704ea8dc4b6Seschrock 
705ea8dc4b6Seschrock 	if (cancel != NULL) {
706ea8dc4b6Seschrock 		/*
707ea8dc4b6Seschrock 		 * '-c' is invalid with any other options.
708ea8dc4b6Seschrock 		 */
709ea8dc4b6Seschrock 		if (raw != NULL || range != NULL || type != TYPE_INVAL ||
71088ecc943SGeorge Wilson 		    level != 0 || record.zi_func[0] != '\0') {
711ea8dc4b6Seschrock 			(void) fprintf(stderr, "cancel (-c) incompatible with "
712ea8dc4b6Seschrock 			    "any other options\n");
713ea8dc4b6Seschrock 			usage();
714ea8dc4b6Seschrock 			return (2);
715ea8dc4b6Seschrock 		}
716ea8dc4b6Seschrock 		if (argc != 0) {
717ea8dc4b6Seschrock 			(void) fprintf(stderr, "extraneous argument to '-c'\n");
718ea8dc4b6Seschrock 			usage();
719ea8dc4b6Seschrock 			return (2);
720ea8dc4b6Seschrock 		}
721ea8dc4b6Seschrock 
722ea8dc4b6Seschrock 		if (strcmp(cancel, "all") == 0) {
723ea8dc4b6Seschrock 			return (cancel_all_handlers());
724ea8dc4b6Seschrock 		} else {
725ea8dc4b6Seschrock 			int id = (int)strtol(cancel, &end, 10);
726ea8dc4b6Seschrock 			if (*end != '\0') {
727ea8dc4b6Seschrock 				(void) fprintf(stderr, "invalid handle id '%s':"
728ea8dc4b6Seschrock 				    " must be an integer or 'all'\n", cancel);
729ea8dc4b6Seschrock 				usage();
730ea8dc4b6Seschrock 				return (1);
731ea8dc4b6Seschrock 			}
732ea8dc4b6Seschrock 			return (cancel_handler(id));
733ea8dc4b6Seschrock 		}
734ea8dc4b6Seschrock 	}
735ea8dc4b6Seschrock 
736ea8dc4b6Seschrock 	if (device != NULL) {
737ea8dc4b6Seschrock 		/*
738ea8dc4b6Seschrock 		 * Device (-d) injection uses a completely different mechanism
739ea8dc4b6Seschrock 		 * for doing injection, so handle it separately here.
740ea8dc4b6Seschrock 		 */
741ea8dc4b6Seschrock 		if (raw != NULL || range != NULL || type != TYPE_INVAL ||
74288ecc943SGeorge Wilson 		    level != 0 || record.zi_func[0] != '\0') {
743ea8dc4b6Seschrock 			(void) fprintf(stderr, "device (-d) incompatible with "
744ea8dc4b6Seschrock 			    "data error injection\n");
745ea8dc4b6Seschrock 			usage();
746ea8dc4b6Seschrock 			return (2);
747ea8dc4b6Seschrock 		}
748ea8dc4b6Seschrock 
749ea8dc4b6Seschrock 		if (argc != 1) {
750ea8dc4b6Seschrock 			(void) fprintf(stderr, "device (-d) injection requires "
751ea8dc4b6Seschrock 			    "a single pool name\n");
752ea8dc4b6Seschrock 			usage();
753ea8dc4b6Seschrock 			return (2);
754ea8dc4b6Seschrock 		}
755ea8dc4b6Seschrock 
756ea8dc4b6Seschrock 		(void) strcpy(pool, argv[0]);
757ea8dc4b6Seschrock 		dataset[0] = '\0';
758ea8dc4b6Seschrock 
759ea8dc4b6Seschrock 		if (error == ECKSUM) {
760ea8dc4b6Seschrock 			(void) fprintf(stderr, "device error type must be "
761ea8dc4b6Seschrock 			    "'io' or 'nxio'\n");
762ea8dc4b6Seschrock 			return (1);
763ea8dc4b6Seschrock 		}
764ea8dc4b6Seschrock 
765*8f18d1faSGeorge Wilson 		record.zi_iotype = io_type;
76621bf64a7Sgw25295 		if (translate_device(pool, device, label, &record) != 0)
767ea8dc4b6Seschrock 			return (1);
768ea8dc4b6Seschrock 		if (!error)
769ea8dc4b6Seschrock 			error = ENXIO;
770*8f18d1faSGeorge Wilson 
771*8f18d1faSGeorge Wilson 		if (action != VDEV_STATE_UNKNOWN)
772*8f18d1faSGeorge Wilson 			return (perform_action(pool, &record, action));
773*8f18d1faSGeorge Wilson 
774ea8dc4b6Seschrock 	} else if (raw != NULL) {
77588ecc943SGeorge Wilson 		if (range != NULL || type != TYPE_INVAL || level != 0 ||
77688ecc943SGeorge Wilson 		    record.zi_func[0] != '\0') {
777ea8dc4b6Seschrock 			(void) fprintf(stderr, "raw (-b) format with "
778ea8dc4b6Seschrock 			    "any other options\n");
779ea8dc4b6Seschrock 			usage();
780ea8dc4b6Seschrock 			return (2);
781ea8dc4b6Seschrock 		}
782ea8dc4b6Seschrock 
783ea8dc4b6Seschrock 		if (argc != 1) {
784ea8dc4b6Seschrock 			(void) fprintf(stderr, "raw (-b) format expects a "
785ea8dc4b6Seschrock 			    "single pool name\n");
786ea8dc4b6Seschrock 			usage();
787ea8dc4b6Seschrock 			return (2);
788ea8dc4b6Seschrock 		}
789ea8dc4b6Seschrock 
790ea8dc4b6Seschrock 		(void) strcpy(pool, argv[0]);
791ea8dc4b6Seschrock 		dataset[0] = '\0';
792ea8dc4b6Seschrock 
793ea8dc4b6Seschrock 		if (error == ENXIO) {
794ea8dc4b6Seschrock 			(void) fprintf(stderr, "data error type must be "
795ea8dc4b6Seschrock 			    "'checksum' or 'io'\n");
796ea8dc4b6Seschrock 			return (1);
797ea8dc4b6Seschrock 		}
798ea8dc4b6Seschrock 
799ea8dc4b6Seschrock 		if (translate_raw(raw, &record) != 0)
800ea8dc4b6Seschrock 			return (1);
801ea8dc4b6Seschrock 		if (!error)
802ea8dc4b6Seschrock 			error = EIO;
80388ecc943SGeorge Wilson 	} else if (record.zi_func[0] != '\0') {
80488ecc943SGeorge Wilson 		if (raw != NULL || range != NULL || type != TYPE_INVAL ||
80588ecc943SGeorge Wilson 		    level != 0 || device != NULL) {
80688ecc943SGeorge Wilson 			(void) fprintf(stderr, "panic (-p) incompatible with "
80788ecc943SGeorge Wilson 			    "other options\n");
80888ecc943SGeorge Wilson 			usage();
80988ecc943SGeorge Wilson 			return (2);
81088ecc943SGeorge Wilson 		}
81188ecc943SGeorge Wilson 
81288ecc943SGeorge Wilson 		if (argc != 1) {
81388ecc943SGeorge Wilson 			(void) fprintf(stderr, "panic (-p) injection requires "
81488ecc943SGeorge Wilson 			    "a single pool name\n");
81588ecc943SGeorge Wilson 			usage();
81688ecc943SGeorge Wilson 			return (2);
81788ecc943SGeorge Wilson 		}
81888ecc943SGeorge Wilson 
81988ecc943SGeorge Wilson 		(void) strcpy(pool, argv[0]);
82088ecc943SGeorge Wilson 		dataset[0] = '\0';
821ea8dc4b6Seschrock 	} else if (type == TYPE_INVAL) {
822ea8dc4b6Seschrock 		if (flags == 0) {
823ea8dc4b6Seschrock 			(void) fprintf(stderr, "at least one of '-b', '-d', "
82488ecc943SGeorge Wilson 			    "'-t', '-a', '-p', or '-u' must be specified\n");
825ea8dc4b6Seschrock 			usage();
826ea8dc4b6Seschrock 			return (2);
827ea8dc4b6Seschrock 		}
828ea8dc4b6Seschrock 
829ea8dc4b6Seschrock 		if (argc == 1 && (flags & ZINJECT_UNLOAD_SPA)) {
830ea8dc4b6Seschrock 			(void) strcpy(pool, argv[0]);
831ea8dc4b6Seschrock 			dataset[0] = '\0';
832ea8dc4b6Seschrock 		} else if (argc != 0) {
833ea8dc4b6Seschrock 			(void) fprintf(stderr, "extraneous argument for "
834ea8dc4b6Seschrock 			    "'-f'\n");
835ea8dc4b6Seschrock 			usage();
836ea8dc4b6Seschrock 			return (2);
837ea8dc4b6Seschrock 		}
838ea8dc4b6Seschrock 
839ea8dc4b6Seschrock 		flags |= ZINJECT_NULL;
840ea8dc4b6Seschrock 	} else {
841ea8dc4b6Seschrock 		if (argc != 1) {
842ea8dc4b6Seschrock 			(void) fprintf(stderr, "missing object\n");
843ea8dc4b6Seschrock 			usage();
844ea8dc4b6Seschrock 			return (2);
845ea8dc4b6Seschrock 		}
846ea8dc4b6Seschrock 
847ea8dc4b6Seschrock 		if (error == ENXIO) {
848ea8dc4b6Seschrock 			(void) fprintf(stderr, "data error type must be "
849ea8dc4b6Seschrock 			    "'checksum' or 'io'\n");
850ea8dc4b6Seschrock 			return (1);
851ea8dc4b6Seschrock 		}
852ea8dc4b6Seschrock 
853ea8dc4b6Seschrock 		if (translate_record(type, argv[0], range, level, &record, pool,
854ea8dc4b6Seschrock 		    dataset) != 0)
855ea8dc4b6Seschrock 			return (1);
856ea8dc4b6Seschrock 		if (!error)
857ea8dc4b6Seschrock 			error = EIO;
858ea8dc4b6Seschrock 	}
859ea8dc4b6Seschrock 
860ea8dc4b6Seschrock 	/*
861ea8dc4b6Seschrock 	 * If this is pool-wide metadata, unmount everything.  The ioctl() will
862ea8dc4b6Seschrock 	 * unload the pool, so that we trigger spa-wide reopen of metadata next
863ea8dc4b6Seschrock 	 * time we access the pool.
864ea8dc4b6Seschrock 	 */
865ea8dc4b6Seschrock 	if (dataset[0] != '\0' && domount) {
866990b4856Slling 		if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_DATASET)) == NULL)
867ea8dc4b6Seschrock 			return (1);
868ea8dc4b6Seschrock 
869ea8dc4b6Seschrock 		if (zfs_unmount(zhp, NULL, 0) != 0)
870ea8dc4b6Seschrock 			return (1);
871ea8dc4b6Seschrock 	}
872ea8dc4b6Seschrock 
873ea8dc4b6Seschrock 	record.zi_error = error;
874ea8dc4b6Seschrock 
875ea8dc4b6Seschrock 	ret = register_handler(pool, flags, &record, quiet);
876ea8dc4b6Seschrock 
877ea8dc4b6Seschrock 	if (dataset[0] != '\0' && domount)
878ea8dc4b6Seschrock 		ret = (zfs_mount(zhp, NULL, 0) != 0);
879ea8dc4b6Seschrock 
88099653d4eSeschrock 	libzfs_fini(g_zfs);
88199653d4eSeschrock 
882ea8dc4b6Seschrock 	return (ret);
883ea8dc4b6Seschrock }
884