xref: /illumos-gate/usr/src/cmd/zinject/zinject.c (revision 12a8814c)
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 /*
2298d1cbfeSGeorge Wilson  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2397e81309SPrakash Surya  * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
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,
3921bf64a7Sgw  * 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.
4421bf64a7Sgw  * For label faults, the -L option must be specified. This allows faults
4598d1cbfeSGeorge Wilson  * to be injected into either the nvlist, uberblock, pad1, or pad2 region
4698d1cbfeSGeorge Wilson  * of all the labels for the specified device.
47ea8dc4b6Seschrock  *
48ea8dc4b6Seschrock  * This form of the command looks like:
49ea8dc4b6Seschrock  *
50*12a8814cSTom Caputi  *	zinject -d device [-e errno] [-L <uber | nvlist | pad1 | pad2>] pool
51ea8dc4b6Seschrock  *
52ea8dc4b6Seschrock  *
53ea8dc4b6Seschrock  * DATA FAULTS
54ea8dc4b6Seschrock  *
55ea8dc4b6Seschrock  * We begin with a tuple of the form:
56ea8dc4b6Seschrock  *
57*12a8814cSTom Caputi  *	<type,level,range,object>
58ea8dc4b6Seschrock  *
59*12a8814cSTom Caputi  *	type	A string describing the type of data to target.  Each type
60*12a8814cSTom Caputi  *		implicitly describes how to interpret 'object'. Currently,
61*12a8814cSTom Caputi  *		the following values are supported:
62ea8dc4b6Seschrock  *
63*12a8814cSTom Caputi  *		data		User data for a file
64*12a8814cSTom Caputi  *		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  *
70*12a8814cSTom Caputi  *		mos		Any data in the MOS
71*12a8814cSTom Caputi  *		mosdir		object directory
72*12a8814cSTom Caputi  *		config		pool configuration
73*12a8814cSTom Caputi  *		bpobj		blkptr list
74*12a8814cSTom Caputi  *		spacemap	spacemap
75*12a8814cSTom Caputi  *		metaslab	metaslab
76*12a8814cSTom Caputi  *		errlog		persistent error log
77ea8dc4b6Seschrock  *
78*12a8814cSTom Caputi  *	level	Object level.  Defaults to '0', not applicable to all types.  If
79*12a8814cSTom Caputi  *		a range is given, this corresponds to the indirect block
80*12a8814cSTom Caputi  *		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  *
85*12a8814cSTom Caputi  *	object	A string describing the logical location of the object.  For
86*12a8814cSTom Caputi  *		files and directories (currently the only supported types),
87*12a8814cSTom Caputi  *		this is the path of the object on disk.
88ea8dc4b6Seschrock  *
89ea8dc4b6Seschrock  * This is translated, via libzpool, into the following internal representation:
90ea8dc4b6Seschrock  *
91*12a8814cSTom Caputi  *	<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  *
101*12a8814cSTom Caputi  *	zinject
102*12a8814cSTom Caputi  *	zinject <-a | -u pool>
103*12a8814cSTom Caputi  *	zinject -c <id|all>
104*12a8814cSTom Caputi  *	zinject [-q] <-t type> [-f freq] [-u] [-a] [-m] [-e errno] [-l level]
105ea8dc4b6Seschrock  *	    [-r range] <object>
106*12a8814cSTom Caputi  *	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",
167cde58dbcSMatthew Ahrens 	"bpobj",
168ea8dc4b6Seschrock 	"spacemap",
16921bf64a7Sgw 	"errlog",
17021bf64a7Sgw 	"uber",
17198d1cbfeSGeorge Wilson 	"nvlist",
17298d1cbfeSGeorge Wilson 	"pad1",
17398d1cbfeSGeorge Wilson 	"pad2"
174ea8dc4b6Seschrock };
175ea8dc4b6Seschrock 
176ea8dc4b6Seschrock static err_type_t
177ea8dc4b6Seschrock name_to_type(const char *arg)
178ea8dc4b6Seschrock {
179ea8dc4b6Seschrock 	int i;
180ea8dc4b6Seschrock 	for (i = 0; i < TYPE_INVAL; i++)
181ea8dc4b6Seschrock 		if (strcmp(errtable[i], arg) == 0)
182ea8dc4b6Seschrock 			return (i);
183ea8dc4b6Seschrock 
184ea8dc4b6Seschrock 	return (TYPE_INVAL);
185ea8dc4b6Seschrock }
186ea8dc4b6Seschrock 
187ea8dc4b6Seschrock static const char *
188ea8dc4b6Seschrock type_to_name(uint64_t type)
189ea8dc4b6Seschrock {
190ea8dc4b6Seschrock 	switch (type) {
191ea8dc4b6Seschrock 	case DMU_OT_OBJECT_DIRECTORY:
192ea8dc4b6Seschrock 		return ("mosdir");
193ea8dc4b6Seschrock 	case DMU_OT_OBJECT_ARRAY:
194ea8dc4b6Seschrock 		return ("metaslab");
195ea8dc4b6Seschrock 	case DMU_OT_PACKED_NVLIST:
196ea8dc4b6Seschrock 		return ("config");
197cde58dbcSMatthew Ahrens 	case DMU_OT_BPOBJ:
198cde58dbcSMatthew Ahrens 		return ("bpobj");
199ea8dc4b6Seschrock 	case DMU_OT_SPACE_MAP:
200ea8dc4b6Seschrock 		return ("spacemap");
201ea8dc4b6Seschrock 	case DMU_OT_ERROR_LOG:
202ea8dc4b6Seschrock 		return ("errlog");
203ea8dc4b6Seschrock 	default:
204ea8dc4b6Seschrock 		return ("-");
205ea8dc4b6Seschrock 	}
206ea8dc4b6Seschrock }
207ea8dc4b6Seschrock 
208ea8dc4b6Seschrock 
209ea8dc4b6Seschrock /*
210ea8dc4b6Seschrock  * Print usage message.
211ea8dc4b6Seschrock  */
212ea8dc4b6Seschrock void
213ea8dc4b6Seschrock usage(void)
214ea8dc4b6Seschrock {
215ea8dc4b6Seschrock 	(void) printf(
216ea8dc4b6Seschrock 	    "usage:\n"
217ea8dc4b6Seschrock 	    "\n"
218ea8dc4b6Seschrock 	    "\tzinject\n"
219ea8dc4b6Seschrock 	    "\n"
220ea8dc4b6Seschrock 	    "\t\tList all active injection records.\n"
221ea8dc4b6Seschrock 	    "\n"
222ea8dc4b6Seschrock 	    "\tzinject -c <id|all>\n"
223ea8dc4b6Seschrock 	    "\n"
224ea8dc4b6Seschrock 	    "\t\tClear the particular record (if given a numeric ID), or\n"
225ea8dc4b6Seschrock 	    "\t\tall records if 'all' is specificed.\n"
226ea8dc4b6Seschrock 	    "\n"
22788ecc943SGeorge Wilson 	    "\tzinject -p <function name> pool\n"
22897e81309SPrakash Surya 	    "\n"
22988ecc943SGeorge Wilson 	    "\t\tInject a panic fault at the specified function. Only \n"
23088ecc943SGeorge Wilson 	    "\t\tfunctions which call spa_vdev_config_exit(), or \n"
23188ecc943SGeorge Wilson 	    "\t\tspa_vdev_exit() will trigger a panic.\n"
23288ecc943SGeorge Wilson 	    "\n"
23398d1cbfeSGeorge Wilson 	    "\tzinject -d device [-e errno] [-L <nvlist|uber|pad1|pad2>] [-F]\n"
2348f18d1faSGeorge Wilson 	    "\t    [-T <read|write|free|claim|all> pool\n"
23597e81309SPrakash Surya 	    "\n"
23621bf64a7Sgw 	    "\t\tInject a fault into a particular device or the device's\n"
23798d1cbfeSGeorge Wilson 	    "\t\tlabel.  Label injection can either be 'nvlist', 'uber',\n "
23898d1cbfeSGeorge Wilson 	    "\t\t'pad1', or 'pad2'.\n"
239cb04b873SMark J Musante 	    "\t\t'errno' can be 'nxio' (the default), 'io', or 'dtl'.\n"
240ea8dc4b6Seschrock 	    "\n"
2418f18d1faSGeorge Wilson 	    "\tzinject -d device -A <degrade|fault> pool\n"
24297e81309SPrakash Surya 	    "\n"
2438f18d1faSGeorge Wilson 	    "\t\tPerform a specific action on a particular device\n"
2448f18d1faSGeorge Wilson 	    "\n"
24597e81309SPrakash Surya 	    "\tzinject -d device -D latency:lanes pool\n"
24697e81309SPrakash Surya 	    "\n"
24797e81309SPrakash Surya 	    "\t\tAdd an artificial delay to IO requests on a particular\n"
24897e81309SPrakash Surya 	    "\t\tdevice, such that the requests take a minimum of 'latency'\n"
24997e81309SPrakash Surya 	    "\t\tmilliseconds to complete. Each delay has an associated\n"
25097e81309SPrakash Surya 	    "\t\tnumber of 'lanes' which defines the number of concurrent\n"
25197e81309SPrakash Surya 	    "\t\tIO requests that can be processed.\n"
25297e81309SPrakash Surya 	    "\n"
25397e81309SPrakash Surya 	    "\t\tFor example, with a single lane delay of 10 ms (-D 10:1),\n"
25497e81309SPrakash Surya 	    "\t\tthe device will only be able to service a single IO request\n"
25597e81309SPrakash Surya 	    "\t\tat a time with each request taking 10 ms to complete. So,\n"
25697e81309SPrakash Surya 	    "\t\tif only a single request is submitted every 10 ms, the\n"
25797e81309SPrakash Surya 	    "\t\taverage latency will be 10 ms; but if more than one request\n"
25897e81309SPrakash Surya 	    "\t\tis submitted every 10 ms, the average latency will be more\n"
25997e81309SPrakash Surya 	    "\t\tthan 10 ms.\n"
26097e81309SPrakash Surya 	    "\n"
26197e81309SPrakash Surya 	    "\t\tSimilarly, if a delay of 10 ms is specified to have two\n"
26297e81309SPrakash Surya 	    "\t\tlanes (-D 10:2), then the device will be able to service\n"
26397e81309SPrakash Surya 	    "\t\ttwo requests at a time, each with a minimum latency of\n"
26497e81309SPrakash Surya 	    "\t\t10 ms. So, if two requests are submitted every 10 ms, then\n"
26597e81309SPrakash Surya 	    "\t\tthe average latency will be 10 ms; but if more than two\n"
26697e81309SPrakash Surya 	    "\t\trequests are submitted every 10 ms, the average latency\n"
26797e81309SPrakash Surya 	    "\t\twill be more than 10 ms.\n"
26897e81309SPrakash Surya 	    "\n"
26997e81309SPrakash Surya 	    "\t\tAlso note, these delays are additive. So two invocations\n"
27097e81309SPrakash Surya 	    "\t\tof '-D 10:1', is roughly equivalent to a single invocation\n"
27197e81309SPrakash Surya 	    "\t\tof '-D 10:2'. This also means, one can specify multiple\n"
27297e81309SPrakash Surya 	    "\t\tlanes with differing target latencies. For example, an\n"
27397e81309SPrakash Surya 	    "\t\tinvocation of '-D 10:1' followed by '-D 25:2' will\n"
27497e81309SPrakash Surya 	    "\t\tcreate 3 lanes on the device; one lane with a latency\n"
27597e81309SPrakash Surya 	    "\t\tof 10 ms and two lanes with a 25 ms latency.\n"
27697e81309SPrakash Surya 	    "\n"
277468c413aSTim Haley 	    "\tzinject -I [-s <seconds> | -g <txgs>] pool\n"
27897e81309SPrakash Surya 	    "\n"
279468c413aSTim Haley 	    "\t\tCause the pool to stop writing blocks yet not\n"
280468c413aSTim Haley 	    "\t\treport errors for a duration.  Simulates buggy hardware\n"
281468c413aSTim Haley 	    "\t\tthat fails to honor cache flush requests.\n"
282468c413aSTim Haley 	    "\t\tDefault duration is 30 seconds.  The machine is panicked\n"
283468c413aSTim Haley 	    "\t\tat the end of the duration.\n"
284468c413aSTim Haley 	    "\n"
285ea8dc4b6Seschrock 	    "\tzinject -b objset:object:level:blkid pool\n"
286ea8dc4b6Seschrock 	    "\n"
287ea8dc4b6Seschrock 	    "\t\tInject an error into pool 'pool' with the numeric bookmark\n"
288ea8dc4b6Seschrock 	    "\t\tspecified by the remaining tuple.  Each number is in\n"
289ea8dc4b6Seschrock 	    "\t\thexidecimal, and only one block can be specified.\n"
290ea8dc4b6Seschrock 	    "\n"
291*12a8814cSTom Caputi 	    "\tzinject [-q] <-t type> [-C dvas] [-e errno] [-l level]\n"
292*12a8814cSTom Caputi 	    "\t\t[-r range] [-a] [-m] [-u] [-f freq] <object>\n"
293ea8dc4b6Seschrock 	    "\n"
294ea8dc4b6Seschrock 	    "\t\tInject an error into the object specified by the '-t' option\n"
295ea8dc4b6Seschrock 	    "\t\tand the object descriptor.  The 'object' parameter is\n"
296ea8dc4b6Seschrock 	    "\t\tinterperted depending on the '-t' option.\n"
297ea8dc4b6Seschrock 	    "\n"
298ea8dc4b6Seschrock 	    "\t\t-q\tQuiet mode.  Only print out the handler number added.\n"
299ea8dc4b6Seschrock 	    "\t\t-e\tInject a specific error.  Must be either 'io' or\n"
300*12a8814cSTom Caputi 	    "\t\t\t'checksum', or 'decompress'.  Default is 'io'.\n"
301*12a8814cSTom Caputi 	    "\t\t-C\tInject the given error only into specific DVAs. The\n"
302*12a8814cSTom Caputi 	    "\t\t\tDVAs should be specified as a list of 0-indexed DVAs\n"
303*12a8814cSTom Caputi 	    "\t\t\tseparated by commas (ex. '0,2').\n"
304ea8dc4b6Seschrock 	    "\t\t-l\tInject error at a particular block level. Default is "
305ea8dc4b6Seschrock 	    "0.\n"
306ea8dc4b6Seschrock 	    "\t\t-m\tAutomatically remount underlying filesystem.\n"
307ea8dc4b6Seschrock 	    "\t\t-r\tInject error over a particular logical range of an\n"
308ea8dc4b6Seschrock 	    "\t\t\tobject.  Will be translated to the appropriate blkid\n"
309ea8dc4b6Seschrock 	    "\t\t\trange according to the object's properties.\n"
310ea8dc4b6Seschrock 	    "\t\t-a\tFlush the ARC cache.  Can be specified without any\n"
311ea8dc4b6Seschrock 	    "\t\t\tassociated object.\n"
312ea8dc4b6Seschrock 	    "\t\t-u\tUnload the associated pool.  Can be specified with only\n"
313ea8dc4b6Seschrock 	    "\t\t\ta pool object.\n"
314ea8dc4b6Seschrock 	    "\t\t-f\tOnly inject errors a fraction of the time.  Expressed as\n"
315ea8dc4b6Seschrock 	    "\t\t\ta percentage between 1 and 100.\n"
316ea8dc4b6Seschrock 	    "\n"
317ea8dc4b6Seschrock 	    "\t-t data\t\tInject an error into the plain file contents of a\n"
318ea8dc4b6Seschrock 	    "\t\t\tfile.  The object must be specified as a complete path\n"
319ea8dc4b6Seschrock 	    "\t\t\tto a file on a ZFS filesystem.\n"
320ea8dc4b6Seschrock 	    "\n"
321ea8dc4b6Seschrock 	    "\t-t dnode\tInject an error into the metadnode in the block\n"
322ea8dc4b6Seschrock 	    "\t\t\tcorresponding to the dnode for a file or directory.  The\n"
323ea8dc4b6Seschrock 	    "\t\t\t'-r' option is incompatible with this mode.  The object\n"
324ea8dc4b6Seschrock 	    "\t\t\tis specified as a complete path to a file or directory\n"
325ea8dc4b6Seschrock 	    "\t\t\ton a ZFS filesystem.\n"
326ea8dc4b6Seschrock 	    "\n"
327ea8dc4b6Seschrock 	    "\t-t <mos>\tInject errors into the MOS for objects of the given\n"
328cde58dbcSMatthew Ahrens 	    "\t\t\ttype.  Valid types are: mos, mosdir, config, bpobj,\n"
32955434c77Sek 	    "\t\t\tspacemap, metaslab, errlog.  The only valid <object> is\n"
33055434c77Sek 	    "\t\t\tthe poolname.\n");
331ea8dc4b6Seschrock }
332ea8dc4b6Seschrock 
333ea8dc4b6Seschrock static int
334ea8dc4b6Seschrock iter_handlers(int (*func)(int, const char *, zinject_record_t *, void *),
335ea8dc4b6Seschrock     void *data)
336ea8dc4b6Seschrock {
337f4c46b1eSYuri Pankov 	zfs_cmd_t zc = { 0 };
338ea8dc4b6Seschrock 	int ret;
339ea8dc4b6Seschrock 
340ea8dc4b6Seschrock 	while (ioctl(zfs_fd, ZFS_IOC_INJECT_LIST_NEXT, &zc) == 0)
341ea8dc4b6Seschrock 		if ((ret = func((int)zc.zc_guid, zc.zc_name,
342ea8dc4b6Seschrock 		    &zc.zc_inject_record, data)) != 0)
343ea8dc4b6Seschrock 			return (ret);
344ea8dc4b6Seschrock 
34554a91118SChris Kirby 	if (errno != ENOENT) {
34654a91118SChris Kirby 		(void) fprintf(stderr, "Unable to list handlers: %s\n",
34754a91118SChris Kirby 		    strerror(errno));
34854a91118SChris Kirby 		return (-1);
34954a91118SChris Kirby 	}
35054a91118SChris Kirby 
351ea8dc4b6Seschrock 	return (0);
352ea8dc4b6Seschrock }
353ea8dc4b6Seschrock 
354ea8dc4b6Seschrock static int
355ea8dc4b6Seschrock print_data_handler(int id, const char *pool, zinject_record_t *record,
356ea8dc4b6Seschrock     void *data)
357ea8dc4b6Seschrock {
358ea8dc4b6Seschrock 	int *count = data;
359ea8dc4b6Seschrock 
36088ecc943SGeorge Wilson 	if (record->zi_guid != 0 || record->zi_func[0] != '\0')
361ea8dc4b6Seschrock 		return (0);
362ea8dc4b6Seschrock 
363ea8dc4b6Seschrock 	if (*count == 0) {
364*12a8814cSTom Caputi 		(void) printf("%3s  %-15s  %-6s  %-6s  %-8s  %3s  %-4s  ",
365*12a8814cSTom Caputi 		    "%-15s\n", "ID", "POOL", "OBJSET", "OBJECT", "TYPE",
366*12a8814cSTom Caputi 		    "LVL", "DVAs", "RANGE");
367ea8dc4b6Seschrock 		(void) printf("---  ---------------  ------  "
368*12a8814cSTom Caputi 		    "------  --------  ---  ---- ----------------\n");
369ea8dc4b6Seschrock 	}
370ea8dc4b6Seschrock 
371ea8dc4b6Seschrock 	*count += 1;
372ea8dc4b6Seschrock 
373*12a8814cSTom Caputi 	(void) printf("%3d  %-15s  %-6llu  %-6llu  %-8s  %-3d  0x%02x  ",
374*12a8814cSTom Caputi 	    id, pool, (u_longlong_t)record->zi_objset,
375*12a8814cSTom Caputi 	    (u_longlong_t)record->zi_object, type_to_name(record->zi_type),
376*12a8814cSTom Caputi 	    record->zi_level, record->zi_dvas);
377ea8dc4b6Seschrock 
378ea8dc4b6Seschrock 	if (record->zi_start == 0 &&
379ea8dc4b6Seschrock 	    record->zi_end == -1ULL)
380ea8dc4b6Seschrock 		(void) printf("all\n");
381ea8dc4b6Seschrock 	else
382ea8dc4b6Seschrock 		(void) printf("[%llu, %llu]\n", (u_longlong_t)record->zi_start,
383ea8dc4b6Seschrock 		    (u_longlong_t)record->zi_end);
384ea8dc4b6Seschrock 
385ea8dc4b6Seschrock 	return (0);
386ea8dc4b6Seschrock }
387ea8dc4b6Seschrock 
388ea8dc4b6Seschrock static int
389ea8dc4b6Seschrock print_device_handler(int id, const char *pool, zinject_record_t *record,
390ea8dc4b6Seschrock     void *data)
391ea8dc4b6Seschrock {
392ea8dc4b6Seschrock 	int *count = data;
393ea8dc4b6Seschrock 
39488ecc943SGeorge Wilson 	if (record->zi_guid == 0 || record->zi_func[0] != '\0')
395ea8dc4b6Seschrock 		return (0);
396ea8dc4b6Seschrock 
39797e81309SPrakash Surya 	if (record->zi_cmd == ZINJECT_DELAY_IO)
39897e81309SPrakash Surya 		return (0);
39997e81309SPrakash Surya 
400ea8dc4b6Seschrock 	if (*count == 0) {
401ea8dc4b6Seschrock 		(void) printf("%3s  %-15s  %s\n", "ID", "POOL", "GUID");
402ea8dc4b6Seschrock 		(void) printf("---  ---------------  ----------------\n");
403ea8dc4b6Seschrock 	}
404ea8dc4b6Seschrock 
405ea8dc4b6Seschrock 	*count += 1;
406ea8dc4b6Seschrock 
407ea8dc4b6Seschrock 	(void) printf("%3d  %-15s  %llx\n", id, pool,
408ea8dc4b6Seschrock 	    (u_longlong_t)record->zi_guid);
409ea8dc4b6Seschrock 
410ea8dc4b6Seschrock 	return (0);
411ea8dc4b6Seschrock }
412ea8dc4b6Seschrock 
41397e81309SPrakash Surya static int
41497e81309SPrakash Surya print_delay_handler(int id, const char *pool, zinject_record_t *record,
41597e81309SPrakash Surya     void *data)
41697e81309SPrakash Surya {
41797e81309SPrakash Surya 	int *count = data;
41897e81309SPrakash Surya 
41997e81309SPrakash Surya 	if (record->zi_guid == 0 || record->zi_func[0] != '\0')
42097e81309SPrakash Surya 		return (0);
42197e81309SPrakash Surya 
42297e81309SPrakash Surya 	if (record->zi_cmd != ZINJECT_DELAY_IO)
42397e81309SPrakash Surya 		return (0);
42497e81309SPrakash Surya 
42597e81309SPrakash Surya 	if (*count == 0) {
42697e81309SPrakash Surya 		(void) printf("%3s  %-15s  %-15s  %-15s  %s\n",
42797e81309SPrakash Surya 		    "ID", "POOL", "DELAY (ms)", "LANES", "GUID");
42897e81309SPrakash Surya 		(void) printf("---  ---------------  ---------------  "
42997e81309SPrakash Surya 		    "---------------  ----------------\n");
43097e81309SPrakash Surya 	}
43197e81309SPrakash Surya 
43297e81309SPrakash Surya 	*count += 1;
43397e81309SPrakash Surya 
43497e81309SPrakash Surya 	(void) printf("%3d  %-15s  %-15llu  %-15llu  %llx\n", id, pool,
43597e81309SPrakash Surya 	    (u_longlong_t)NSEC2MSEC(record->zi_timer),
43697e81309SPrakash Surya 	    (u_longlong_t)record->zi_nlanes,
43797e81309SPrakash Surya 	    (u_longlong_t)record->zi_guid);
43897e81309SPrakash Surya 
43997e81309SPrakash Surya 	return (0);
44097e81309SPrakash Surya }
44197e81309SPrakash Surya 
44288ecc943SGeorge Wilson static int
44388ecc943SGeorge Wilson print_panic_handler(int id, const char *pool, zinject_record_t *record,
44488ecc943SGeorge Wilson     void *data)
44588ecc943SGeorge Wilson {
44688ecc943SGeorge Wilson 	int *count = data;
44788ecc943SGeorge Wilson 
44888ecc943SGeorge Wilson 	if (record->zi_func[0] == '\0')
44988ecc943SGeorge Wilson 		return (0);
45088ecc943SGeorge Wilson 
45188ecc943SGeorge Wilson 	if (*count == 0) {
45288ecc943SGeorge Wilson 		(void) printf("%3s  %-15s  %s\n", "ID", "POOL", "FUNCTION");
45388ecc943SGeorge Wilson 		(void) printf("---  ---------------  ----------------\n");
45488ecc943SGeorge Wilson 	}
45588ecc943SGeorge Wilson 
45688ecc943SGeorge Wilson 	*count += 1;
45788ecc943SGeorge Wilson 
45888ecc943SGeorge Wilson 	(void) printf("%3d  %-15s  %s\n", id, pool, record->zi_func);
45988ecc943SGeorge Wilson 
46088ecc943SGeorge Wilson 	return (0);
46188ecc943SGeorge Wilson }
46288ecc943SGeorge Wilson 
463ea8dc4b6Seschrock /*
464ea8dc4b6Seschrock  * Print all registered error handlers.  Returns the number of handlers
465ea8dc4b6Seschrock  * registered.
466ea8dc4b6Seschrock  */
467ea8dc4b6Seschrock static int
468ea8dc4b6Seschrock print_all_handlers(void)
469ea8dc4b6Seschrock {
470cb04b873SMark J Musante 	int count = 0, total = 0;
471ea8dc4b6Seschrock 
472ea8dc4b6Seschrock 	(void) iter_handlers(print_device_handler, &count);
473cb04b873SMark J Musante 	if (count > 0) {
474cb04b873SMark J Musante 		total += count;
475cb04b873SMark J Musante 		(void) printf("\n");
476cb04b873SMark J Musante 		count = 0;
477cb04b873SMark J Musante 	}
478cb04b873SMark J Musante 
47997e81309SPrakash Surya 	(void) iter_handlers(print_delay_handler, &count);
48097e81309SPrakash Surya 	if (count > 0) {
48197e81309SPrakash Surya 		total += count;
48297e81309SPrakash Surya 		(void) printf("\n");
48397e81309SPrakash Surya 		count = 0;
48497e81309SPrakash Surya 	}
48597e81309SPrakash Surya 
486ea8dc4b6Seschrock 	(void) iter_handlers(print_data_handler, &count);
487cb04b873SMark J Musante 	if (count > 0) {
488cb04b873SMark J Musante 		total += count;
489cb04b873SMark J Musante 		(void) printf("\n");
490cb04b873SMark J Musante 		count = 0;
491cb04b873SMark J Musante 	}
492cb04b873SMark J Musante 
49388ecc943SGeorge Wilson 	(void) iter_handlers(print_panic_handler, &count);
494ea8dc4b6Seschrock 
495cb04b873SMark J Musante 	return (count + total);
496ea8dc4b6Seschrock }
497ea8dc4b6Seschrock 
498ea8dc4b6Seschrock /* ARGSUSED */
499ea8dc4b6Seschrock static int
500ea8dc4b6Seschrock cancel_one_handler(int id, const char *pool, zinject_record_t *record,
501ea8dc4b6Seschrock     void *data)
502ea8dc4b6Seschrock {
503f4c46b1eSYuri Pankov 	zfs_cmd_t zc = { 0 };
504ea8dc4b6Seschrock 
505ea8dc4b6Seschrock 	zc.zc_guid = (uint64_t)id;
506ea8dc4b6Seschrock 
507ea8dc4b6Seschrock 	if (ioctl(zfs_fd, ZFS_IOC_CLEAR_FAULT, &zc) != 0) {
508ea8dc4b6Seschrock 		(void) fprintf(stderr, "failed to remove handler %d: %s\n",
509ea8dc4b6Seschrock 		    id, strerror(errno));
510ea8dc4b6Seschrock 		return (1);
511ea8dc4b6Seschrock 	}
512ea8dc4b6Seschrock 
513ea8dc4b6Seschrock 	return (0);
514ea8dc4b6Seschrock }
515ea8dc4b6Seschrock 
516ea8dc4b6Seschrock /*
517ea8dc4b6Seschrock  * Remove all fault injection handlers.
518ea8dc4b6Seschrock  */
519ea8dc4b6Seschrock static int
520ea8dc4b6Seschrock cancel_all_handlers(void)
521ea8dc4b6Seschrock {
522ea8dc4b6Seschrock 	int ret = iter_handlers(cancel_one_handler, NULL);
523ea8dc4b6Seschrock 
52454a91118SChris Kirby 	if (ret == 0)
52554a91118SChris Kirby 		(void) printf("removed all registered handlers\n");
526ea8dc4b6Seschrock 
527ea8dc4b6Seschrock 	return (ret);
528ea8dc4b6Seschrock }
529ea8dc4b6Seschrock 
530ea8dc4b6Seschrock /*
531ea8dc4b6Seschrock  * Remove a specific fault injection handler.
532ea8dc4b6Seschrock  */
533ea8dc4b6Seschrock static int
534ea8dc4b6Seschrock cancel_handler(int id)
535ea8dc4b6Seschrock {
536f4c46b1eSYuri Pankov 	zfs_cmd_t zc = { 0 };
537ea8dc4b6Seschrock 
538ea8dc4b6Seschrock 	zc.zc_guid = (uint64_t)id;
539ea8dc4b6Seschrock 
540ea8dc4b6Seschrock 	if (ioctl(zfs_fd, ZFS_IOC_CLEAR_FAULT, &zc) != 0) {
541ea8dc4b6Seschrock 		(void) fprintf(stderr, "failed to remove handler %d: %s\n",
542ea8dc4b6Seschrock 		    id, strerror(errno));
543ea8dc4b6Seschrock 		return (1);
544ea8dc4b6Seschrock 	}
545ea8dc4b6Seschrock 
546ea8dc4b6Seschrock 	(void) printf("removed handler %d\n", id);
547ea8dc4b6Seschrock 
548ea8dc4b6Seschrock 	return (0);
549ea8dc4b6Seschrock }
550ea8dc4b6Seschrock 
551ea8dc4b6Seschrock /*
552ea8dc4b6Seschrock  * Register a new fault injection handler.
553ea8dc4b6Seschrock  */
554ea8dc4b6Seschrock static int
555ea8dc4b6Seschrock register_handler(const char *pool, int flags, zinject_record_t *record,
556ea8dc4b6Seschrock     int quiet)
557ea8dc4b6Seschrock {
558f4c46b1eSYuri Pankov 	zfs_cmd_t zc = { 0 };
559ea8dc4b6Seschrock 
560ea8dc4b6Seschrock 	(void) strcpy(zc.zc_name, pool);
561ea8dc4b6Seschrock 	zc.zc_inject_record = *record;
562ea8dc4b6Seschrock 	zc.zc_guid = flags;
563ea8dc4b6Seschrock 
564ea8dc4b6Seschrock 	if (ioctl(zfs_fd, ZFS_IOC_INJECT_FAULT, &zc) != 0) {
565ea8dc4b6Seschrock 		(void) fprintf(stderr, "failed to add handler: %s\n",
566ea8dc4b6Seschrock 		    strerror(errno));
567ea8dc4b6Seschrock 		return (1);
568ea8dc4b6Seschrock 	}
569ea8dc4b6Seschrock 
570ea8dc4b6Seschrock 	if (flags & ZINJECT_NULL)
571ea8dc4b6Seschrock 		return (0);
572ea8dc4b6Seschrock 
573ea8dc4b6Seschrock 	if (quiet) {
574ea8dc4b6Seschrock 		(void) printf("%llu\n", (u_longlong_t)zc.zc_guid);
575ea8dc4b6Seschrock 	} else {
576ea8dc4b6Seschrock 		(void) printf("Added handler %llu with the following "
577ea8dc4b6Seschrock 		    "properties:\n", (u_longlong_t)zc.zc_guid);
578ea8dc4b6Seschrock 		(void) printf("  pool: %s\n", pool);
579ea8dc4b6Seschrock 		if (record->zi_guid) {
580ea8dc4b6Seschrock 			(void) printf("  vdev: %llx\n",
581ea8dc4b6Seschrock 			    (u_longlong_t)record->zi_guid);
58288ecc943SGeorge Wilson 		} else if (record->zi_func[0] != '\0') {
58388ecc943SGeorge Wilson 			(void) printf("  panic function: %s\n",
58488ecc943SGeorge Wilson 			    record->zi_func);
585468c413aSTim Haley 		} else if (record->zi_duration > 0) {
586468c413aSTim Haley 			(void) printf(" time: %lld seconds\n",
587468c413aSTim Haley 			    (u_longlong_t)record->zi_duration);
588468c413aSTim Haley 		} else if (record->zi_duration < 0) {
589468c413aSTim Haley 			(void) printf(" txgs: %lld \n",
590468c413aSTim Haley 			    (u_longlong_t)-record->zi_duration);
591ea8dc4b6Seschrock 		} else {
592ea8dc4b6Seschrock 			(void) printf("objset: %llu\n",
593ea8dc4b6Seschrock 			    (u_longlong_t)record->zi_objset);
594ea8dc4b6Seschrock 			(void) printf("object: %llu\n",
595ea8dc4b6Seschrock 			    (u_longlong_t)record->zi_object);
596ea8dc4b6Seschrock 			(void) printf("  type: %llu\n",
597ea8dc4b6Seschrock 			    (u_longlong_t)record->zi_type);
598ea8dc4b6Seschrock 			(void) printf(" level: %d\n", record->zi_level);
599ea8dc4b6Seschrock 			if (record->zi_start == 0 &&
600ea8dc4b6Seschrock 			    record->zi_end == -1ULL)
601ea8dc4b6Seschrock 				(void) printf(" range: all\n");
602ea8dc4b6Seschrock 			else
603ea8dc4b6Seschrock 				(void) printf(" range: [%llu, %llu)\n",
604ea8dc4b6Seschrock 				    (u_longlong_t)record->zi_start,
605ea8dc4b6Seschrock 				    (u_longlong_t)record->zi_end);
606*12a8814cSTom Caputi 			(void) printf("  dvas: 0x%x\n", record->zi_dvas);
607ea8dc4b6Seschrock 		}
608ea8dc4b6Seschrock 	}
609ea8dc4b6Seschrock 
610ea8dc4b6Seschrock 	return (0);
611ea8dc4b6Seschrock }
612ea8dc4b6Seschrock 
6138f18d1faSGeorge Wilson int
6148f18d1faSGeorge Wilson perform_action(const char *pool, zinject_record_t *record, int cmd)
6158f18d1faSGeorge Wilson {
616f4c46b1eSYuri Pankov 	zfs_cmd_t zc = { 0 };
6178f18d1faSGeorge Wilson 
6188f18d1faSGeorge Wilson 	ASSERT(cmd == VDEV_STATE_DEGRADED || cmd == VDEV_STATE_FAULTED);
6198f18d1faSGeorge Wilson 	(void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
6208f18d1faSGeorge Wilson 	zc.zc_guid = record->zi_guid;
6218f18d1faSGeorge Wilson 	zc.zc_cookie = cmd;
6228f18d1faSGeorge Wilson 
6238f18d1faSGeorge Wilson 	if (ioctl(zfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
6248f18d1faSGeorge Wilson 		return (0);
6258f18d1faSGeorge Wilson 
6268f18d1faSGeorge Wilson 	return (1);
6278f18d1faSGeorge Wilson }
6288f18d1faSGeorge Wilson 
62997e81309SPrakash Surya static int
63097e81309SPrakash Surya parse_delay(char *str, uint64_t *delay, uint64_t *nlanes)
63197e81309SPrakash Surya {
63297e81309SPrakash Surya 	unsigned long scan_delay;
63397e81309SPrakash Surya 	unsigned long scan_nlanes;
63497e81309SPrakash Surya 
63597e81309SPrakash Surya 	if (sscanf(str, "%lu:%lu", &scan_delay, &scan_nlanes) != 2)
63697e81309SPrakash Surya 		return (1);
63797e81309SPrakash Surya 
63897e81309SPrakash Surya 	/*
63997e81309SPrakash Surya 	 * We explicitly disallow a delay of zero here, because we key
64097e81309SPrakash Surya 	 * off this value being non-zero in translate_device(), to
64197e81309SPrakash Surya 	 * determine if the fault is a ZINJECT_DELAY_IO fault or not.
64297e81309SPrakash Surya 	 */
64397e81309SPrakash Surya 	if (scan_delay == 0)
64497e81309SPrakash Surya 		return (1);
64597e81309SPrakash Surya 
64697e81309SPrakash Surya 	/*
64797e81309SPrakash Surya 	 * The units for the CLI delay parameter is milliseconds, but
64897e81309SPrakash Surya 	 * the data passed to the kernel is interpreted as nanoseconds.
64997e81309SPrakash Surya 	 * Thus we scale the milliseconds to nanoseconds here, and this
65097e81309SPrakash Surya 	 * nanosecond value is used to pass the delay to the kernel.
65197e81309SPrakash Surya 	 */
65297e81309SPrakash Surya 	*delay = MSEC2NSEC(scan_delay);
65397e81309SPrakash Surya 	*nlanes = scan_nlanes;
65497e81309SPrakash Surya 
65597e81309SPrakash Surya 	return (0);
65697e81309SPrakash Surya }
65797e81309SPrakash Surya 
658*12a8814cSTom Caputi /*
659*12a8814cSTom Caputi  * This function converts a string specifier for DVAs into a bit mask.
660*12a8814cSTom Caputi  * The dva's provided by the user should be 0 indexed and separated by
661*12a8814cSTom Caputi  * a comma. For example:
662*12a8814cSTom Caputi  *     "1"     -> 0b0010  (0x2)
663*12a8814cSTom Caputi  *     "0,1"   -> 0b0011  (0x3)
664*12a8814cSTom Caputi  *     "0,1,2" -> 0b0111  (0x7)
665*12a8814cSTom Caputi  */
666*12a8814cSTom Caputi static int
667*12a8814cSTom Caputi parse_dvas(const char *str, uint32_t *dvas_out)
668*12a8814cSTom Caputi {
669*12a8814cSTom Caputi 	const char *c = str;
670*12a8814cSTom Caputi 	uint32_t mask = 0;
671*12a8814cSTom Caputi 	boolean_t need_delim = B_FALSE;
672*12a8814cSTom Caputi 
673*12a8814cSTom Caputi 	/* max string length is 5 ("0,1,2") */
674*12a8814cSTom Caputi 	if (strlen(str) > 5 || strlen(str) == 0)
675*12a8814cSTom Caputi 		return (EINVAL);
676*12a8814cSTom Caputi 
677*12a8814cSTom Caputi 	while (*c != '\0') {
678*12a8814cSTom Caputi 		switch (*c) {
679*12a8814cSTom Caputi 		case '0':
680*12a8814cSTom Caputi 		case '1':
681*12a8814cSTom Caputi 		case '2':
682*12a8814cSTom Caputi 			/* check for pipe between DVAs */
683*12a8814cSTom Caputi 			if (need_delim)
684*12a8814cSTom Caputi 				return (EINVAL);
685*12a8814cSTom Caputi 
686*12a8814cSTom Caputi 			/* check if this DVA has been set already */
687*12a8814cSTom Caputi 			if (mask & (1 << ((*c) - '0')))
688*12a8814cSTom Caputi 				return (EINVAL);
689*12a8814cSTom Caputi 
690*12a8814cSTom Caputi 			mask |= (1 << ((*c) - '0'));
691*12a8814cSTom Caputi 			need_delim = B_TRUE;
692*12a8814cSTom Caputi 			break;
693*12a8814cSTom Caputi 		case ',':
694*12a8814cSTom Caputi 			need_delim = B_FALSE;
695*12a8814cSTom Caputi 			break;
696*12a8814cSTom Caputi 		default:
697*12a8814cSTom Caputi 			/* check for invalid character */
698*12a8814cSTom Caputi 			return (EINVAL);
699*12a8814cSTom Caputi 		}
700*12a8814cSTom Caputi 		c++;
701*12a8814cSTom Caputi 	}
702*12a8814cSTom Caputi 
703*12a8814cSTom Caputi 	/* check for dangling delimiter */
704*12a8814cSTom Caputi 	if (!need_delim)
705*12a8814cSTom Caputi 		return (EINVAL);
706*12a8814cSTom Caputi 
707*12a8814cSTom Caputi 	*dvas_out = mask;
708*12a8814cSTom Caputi 	return (0);
709*12a8814cSTom Caputi }
710*12a8814cSTom Caputi 
711ea8dc4b6Seschrock int
712ea8dc4b6Seschrock main(int argc, char **argv)
713ea8dc4b6Seschrock {
714ea8dc4b6Seschrock 	int c;
715ea8dc4b6Seschrock 	char *range = NULL;
716ea8dc4b6Seschrock 	char *cancel = NULL;
717ea8dc4b6Seschrock 	char *end;
718ea8dc4b6Seschrock 	char *raw = NULL;
719ea8dc4b6Seschrock 	char *device = NULL;
720ea8dc4b6Seschrock 	int level = 0;
721ea8dc4b6Seschrock 	int quiet = 0;
722ea8dc4b6Seschrock 	int error = 0;
723ea8dc4b6Seschrock 	int domount = 0;
7248f18d1faSGeorge Wilson 	int io_type = ZIO_TYPES;
7258f18d1faSGeorge Wilson 	int action = VDEV_STATE_UNKNOWN;
726ea8dc4b6Seschrock 	err_type_t type = TYPE_INVAL;
72721bf64a7Sgw 	err_type_t label = TYPE_INVAL;
728ea8dc4b6Seschrock 	zinject_record_t record = { 0 };
729ea8dc4b6Seschrock 	char pool[MAXNAMELEN];
730ea8dc4b6Seschrock 	char dataset[MAXNAMELEN];
731ea8dc4b6Seschrock 	zfs_handle_t *zhp;
732468c413aSTim Haley 	int nowrites = 0;
733468c413aSTim Haley 	int dur_txg = 0;
734468c413aSTim Haley 	int dur_secs = 0;
735ea8dc4b6Seschrock 	int ret;
736ea8dc4b6Seschrock 	int flags = 0;
737*12a8814cSTom Caputi 	uint32_t dvas = 0;
738ea8dc4b6Seschrock 
73999653d4eSeschrock 	if ((g_zfs = libzfs_init()) == NULL) {
74099653d4eSeschrock 		(void) fprintf(stderr, "internal error: failed to "
74199653d4eSeschrock 		    "initialize ZFS library\n");
74299653d4eSeschrock 		return (1);
74399653d4eSeschrock 	}
74499653d4eSeschrock 
74599653d4eSeschrock 	libzfs_print_on_error(g_zfs, B_TRUE);
74699653d4eSeschrock 
747ea8dc4b6Seschrock 	if ((zfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
748ea8dc4b6Seschrock 		(void) fprintf(stderr, "failed to open ZFS device\n");
749ea8dc4b6Seschrock 		return (1);
750ea8dc4b6Seschrock 	}
751ea8dc4b6Seschrock 
752ea8dc4b6Seschrock 	if (argc == 1) {
753ea8dc4b6Seschrock 		/*
754ea8dc4b6Seschrock 		 * No arguments.  Print the available handlers.  If there are no
755ea8dc4b6Seschrock 		 * available handlers, direct the user to '-h' for help
756ea8dc4b6Seschrock 		 * information.
757ea8dc4b6Seschrock 		 */
758ea8dc4b6Seschrock 		if (print_all_handlers() == 0) {
759ea8dc4b6Seschrock 			(void) printf("No handlers registered.\n");
760ea8dc4b6Seschrock 			(void) printf("Run 'zinject -h' for usage "
761ea8dc4b6Seschrock 			    "information.\n");
762ea8dc4b6Seschrock 		}
763ea8dc4b6Seschrock 
764ea8dc4b6Seschrock 		return (0);
765ea8dc4b6Seschrock 	}
766ea8dc4b6Seschrock 
7678f18d1faSGeorge Wilson 	while ((c = getopt(argc, argv,
768*12a8814cSTom Caputi 	    ":aA:b:C:d:D:f:Fg:qhIc:t:T:l:mr:s:e:uL:p:")) != -1) {
769ea8dc4b6Seschrock 		switch (c) {
770ea8dc4b6Seschrock 		case 'a':
771ea8dc4b6Seschrock 			flags |= ZINJECT_FLUSH_ARC;
772ea8dc4b6Seschrock 			break;
7738f18d1faSGeorge Wilson 		case 'A':
7748f18d1faSGeorge Wilson 			if (strcasecmp(optarg, "degrade") == 0) {
7758f18d1faSGeorge Wilson 				action = VDEV_STATE_DEGRADED;
7768f18d1faSGeorge Wilson 			} else if (strcasecmp(optarg, "fault") == 0) {
7778f18d1faSGeorge Wilson 				action = VDEV_STATE_FAULTED;
7788f18d1faSGeorge Wilson 			} else {
7798f18d1faSGeorge Wilson 				(void) fprintf(stderr, "invalid action '%s': "
7808f18d1faSGeorge Wilson 				    "must be 'degrade' or 'fault'\n", optarg);
7818f18d1faSGeorge Wilson 				usage();
7828f18d1faSGeorge Wilson 				return (1);
7838f18d1faSGeorge Wilson 			}
7848f18d1faSGeorge Wilson 			break;
785ea8dc4b6Seschrock 		case 'b':
786ea8dc4b6Seschrock 			raw = optarg;
787ea8dc4b6Seschrock 			break;
788ea8dc4b6Seschrock 		case 'c':
789ea8dc4b6Seschrock 			cancel = optarg;
790ea8dc4b6Seschrock 			break;
791*12a8814cSTom Caputi 		case 'C':
792*12a8814cSTom Caputi 			ret = parse_dvas(optarg, &dvas);
793*12a8814cSTom Caputi 			if (ret != 0) {
794*12a8814cSTom Caputi 				(void) fprintf(stderr, "invalid DVA list '%s': "
795*12a8814cSTom Caputi 				    "DVAs should be 0 indexed and separated by "
796*12a8814cSTom Caputi 				    "commas.\n", optarg);
797*12a8814cSTom Caputi 				usage();
798*12a8814cSTom Caputi 				libzfs_fini(g_zfs);
799*12a8814cSTom Caputi 				return (1);
800*12a8814cSTom Caputi 			}
801*12a8814cSTom Caputi 			break;
802ea8dc4b6Seschrock 		case 'd':
803ea8dc4b6Seschrock 			device = optarg;
804ea8dc4b6Seschrock 			break;
805283b8460SGeorge.Wilson 		case 'D':
80697e81309SPrakash Surya 			ret = parse_delay(optarg, &record.zi_timer,
80797e81309SPrakash Surya 			    &record.zi_nlanes);
80897e81309SPrakash Surya 			if (ret != 0) {
809283b8460SGeorge.Wilson 				(void) fprintf(stderr, "invalid i/o delay "
810283b8460SGeorge.Wilson 				    "value: '%s'\n", optarg);
811283b8460SGeorge.Wilson 				usage();
812283b8460SGeorge.Wilson 				return (1);
813283b8460SGeorge.Wilson 			}
814283b8460SGeorge.Wilson 			break;
815ea8dc4b6Seschrock 		case 'e':
816ea8dc4b6Seschrock 			if (strcasecmp(optarg, "io") == 0) {
817ea8dc4b6Seschrock 				error = EIO;
818ea8dc4b6Seschrock 			} else if (strcasecmp(optarg, "checksum") == 0) {
819ea8dc4b6Seschrock 				error = ECKSUM;
820ea8dc4b6Seschrock 			} else if (strcasecmp(optarg, "nxio") == 0) {
821ea8dc4b6Seschrock 				error = ENXIO;
822cb04b873SMark J Musante 			} else if (strcasecmp(optarg, "dtl") == 0) {
823cb04b873SMark J Musante 				error = ECHILD;
824ea8dc4b6Seschrock 			} else {
825ea8dc4b6Seschrock 				(void) fprintf(stderr, "invalid error type "
826ea8dc4b6Seschrock 				    "'%s': must be 'io', 'checksum' or "
827ea8dc4b6Seschrock 				    "'nxio'\n", optarg);
828ea8dc4b6Seschrock 				usage();
829ea8dc4b6Seschrock 				return (1);
830ea8dc4b6Seschrock 			}
831ea8dc4b6Seschrock 			break;
832ea8dc4b6Seschrock 		case 'f':
833ea8dc4b6Seschrock 			record.zi_freq = atoi(optarg);
834ea8dc4b6Seschrock 			if (record.zi_freq < 1 || record.zi_freq > 100) {
835ea8dc4b6Seschrock 				(void) fprintf(stderr, "frequency range must "
836ea8dc4b6Seschrock 				    "be in the range (0, 100]\n");
837ea8dc4b6Seschrock 				return (1);
838ea8dc4b6Seschrock 			}
839ea8dc4b6Seschrock 			break;
8408956713aSEric Schrock 		case 'F':
8418956713aSEric Schrock 			record.zi_failfast = B_TRUE;
8428956713aSEric Schrock 			break;
843468c413aSTim Haley 		case 'g':
844468c413aSTim Haley 			dur_txg = 1;
845468c413aSTim Haley 			record.zi_duration = (int)strtol(optarg, &end, 10);
846468c413aSTim Haley 			if (record.zi_duration <= 0 || *end != '\0') {
847468c413aSTim Haley 				(void) fprintf(stderr, "invalid duration '%s': "
848468c413aSTim Haley 				    "must be a positive integer\n", optarg);
849468c413aSTim Haley 				usage();
850468c413aSTim Haley 				return (1);
851468c413aSTim Haley 			}
852468c413aSTim Haley 			/* store duration of txgs as its negative */
853468c413aSTim Haley 			record.zi_duration *= -1;
854468c413aSTim Haley 			break;
855ea8dc4b6Seschrock 		case 'h':
856ea8dc4b6Seschrock 			usage();
857ea8dc4b6Seschrock 			return (0);
858468c413aSTim Haley 		case 'I':
859468c413aSTim Haley 			/* default duration, if one hasn't yet been defined */
860468c413aSTim Haley 			nowrites = 1;
861468c413aSTim Haley 			if (dur_secs == 0 && dur_txg == 0)
862468c413aSTim Haley 				record.zi_duration = 30;
863468c413aSTim Haley 			break;
864ea8dc4b6Seschrock 		case 'l':
865ea8dc4b6Seschrock 			level = (int)strtol(optarg, &end, 10);
866ea8dc4b6Seschrock 			if (*end != '\0') {
867ea8dc4b6Seschrock 				(void) fprintf(stderr, "invalid level '%s': "
868ea8dc4b6Seschrock 				    "must be an integer\n", optarg);
869ea8dc4b6Seschrock 				usage();
870ea8dc4b6Seschrock 				return (1);
871ea8dc4b6Seschrock 			}
872ea8dc4b6Seschrock 			break;
873ea8dc4b6Seschrock 		case 'm':
874ea8dc4b6Seschrock 			domount = 1;
875ea8dc4b6Seschrock 			break;
87688ecc943SGeorge Wilson 		case 'p':
87788ecc943SGeorge Wilson 			(void) strlcpy(record.zi_func, optarg,
87888ecc943SGeorge Wilson 			    sizeof (record.zi_func));
879283b8460SGeorge.Wilson 			record.zi_cmd = ZINJECT_PANIC;
88088ecc943SGeorge Wilson 			break;
881ea8dc4b6Seschrock 		case 'q':
882ea8dc4b6Seschrock 			quiet = 1;
883ea8dc4b6Seschrock 			break;
884ea8dc4b6Seschrock 		case 'r':
885ea8dc4b6Seschrock 			range = optarg;
886ea8dc4b6Seschrock 			break;
887468c413aSTim Haley 		case 's':
888468c413aSTim Haley 			dur_secs = 1;
889468c413aSTim Haley 			record.zi_duration = (int)strtol(optarg, &end, 10);
890468c413aSTim Haley 			if (record.zi_duration <= 0 || *end != '\0') {
891468c413aSTim Haley 				(void) fprintf(stderr, "invalid duration '%s': "
892468c413aSTim Haley 				    "must be a positive integer\n", optarg);
893468c413aSTim Haley 				usage();
894468c413aSTim Haley 				return (1);
895468c413aSTim Haley 			}
896468c413aSTim Haley 			break;
8978f18d1faSGeorge Wilson 		case 'T':
8988f18d1faSGeorge Wilson 			if (strcasecmp(optarg, "read") == 0) {
8998f18d1faSGeorge Wilson 				io_type = ZIO_TYPE_READ;
9008f18d1faSGeorge Wilson 			} else if (strcasecmp(optarg, "write") == 0) {
9018f18d1faSGeorge Wilson 				io_type = ZIO_TYPE_WRITE;
9028f18d1faSGeorge Wilson 			} else if (strcasecmp(optarg, "free") == 0) {
9038f18d1faSGeorge Wilson 				io_type = ZIO_TYPE_FREE;
9048f18d1faSGeorge Wilson 			} else if (strcasecmp(optarg, "claim") == 0) {
9058f18d1faSGeorge Wilson 				io_type = ZIO_TYPE_CLAIM;
9068f18d1faSGeorge Wilson 			} else if (strcasecmp(optarg, "all") == 0) {
9078f18d1faSGeorge Wilson 				io_type = ZIO_TYPES;
9088f18d1faSGeorge Wilson 			} else {
9098f18d1faSGeorge Wilson 				(void) fprintf(stderr, "invalid I/O type "
9108f18d1faSGeorge Wilson 				    "'%s': must be 'read', 'write', 'free', "
9118f18d1faSGeorge Wilson 				    "'claim' or 'all'\n", optarg);
9128f18d1faSGeorge Wilson 				usage();
9138f18d1faSGeorge Wilson 				return (1);
9148f18d1faSGeorge Wilson 			}
9158f18d1faSGeorge Wilson 			break;
916ea8dc4b6Seschrock 		case 't':
91721bf64a7Sgw 			if ((type = name_to_type(optarg)) == TYPE_INVAL &&
91821bf64a7Sgw 			    !MOS_TYPE(type)) {
919ea8dc4b6Seschrock 				(void) fprintf(stderr, "invalid type '%s'\n",
920ea8dc4b6Seschrock 				    optarg);
921ea8dc4b6Seschrock 				usage();
922ea8dc4b6Seschrock 				return (1);
923ea8dc4b6Seschrock 			}
924ea8dc4b6Seschrock 			break;
925ea8dc4b6Seschrock 		case 'u':
926ea8dc4b6Seschrock 			flags |= ZINJECT_UNLOAD_SPA;
927ea8dc4b6Seschrock 			break;
92821bf64a7Sgw 		case 'L':
92921bf64a7Sgw 			if ((label = name_to_type(optarg)) == TYPE_INVAL &&
93021bf64a7Sgw 			    !LABEL_TYPE(type)) {
93121bf64a7Sgw 				(void) fprintf(stderr, "invalid label type "
93221bf64a7Sgw 				    "'%s'\n", optarg);
93321bf64a7Sgw 				usage();
93421bf64a7Sgw 				return (1);
93521bf64a7Sgw 			}
93621bf64a7Sgw 			break;
937ea8dc4b6Seschrock 		case ':':
938ea8dc4b6Seschrock 			(void) fprintf(stderr, "option -%c requires an "
939ea8dc4b6Seschrock 			    "operand\n", optopt);
940ea8dc4b6Seschrock 			usage();
941ea8dc4b6Seschrock 			return (1);
942ea8dc4b6Seschrock 		case '?':
943ea8dc4b6Seschrock 			(void) fprintf(stderr, "invalid option '%c'\n",
944ea8dc4b6Seschrock 			    optopt);
945ea8dc4b6Seschrock 			usage();
946ea8dc4b6Seschrock 			return (2);
947ea8dc4b6Seschrock 		}
948ea8dc4b6Seschrock 	}
949ea8dc4b6Seschrock 
950ea8dc4b6Seschrock 	argc -= optind;
951ea8dc4b6Seschrock 	argv += optind;
952ea8dc4b6Seschrock 
953283b8460SGeorge.Wilson 	if (record.zi_duration != 0)
954283b8460SGeorge.Wilson 		record.zi_cmd = ZINJECT_IGNORED_WRITES;
955283b8460SGeorge.Wilson 
956ea8dc4b6Seschrock 	if (cancel != NULL) {
957ea8dc4b6Seschrock 		/*
958ea8dc4b6Seschrock 		 * '-c' is invalid with any other options.
959ea8dc4b6Seschrock 		 */
960ea8dc4b6Seschrock 		if (raw != NULL || range != NULL || type != TYPE_INVAL ||
961*12a8814cSTom Caputi 		    level != 0 || record.zi_cmd != ZINJECT_UNINITIALIZED ||
962*12a8814cSTom Caputi 		    record.zi_freq > 0 || dvas != 0) {
963ea8dc4b6Seschrock 			(void) fprintf(stderr, "cancel (-c) incompatible with "
964ea8dc4b6Seschrock 			    "any other options\n");
965ea8dc4b6Seschrock 			usage();
966ea8dc4b6Seschrock 			return (2);
967ea8dc4b6Seschrock 		}
968ea8dc4b6Seschrock 		if (argc != 0) {
969ea8dc4b6Seschrock 			(void) fprintf(stderr, "extraneous argument to '-c'\n");
970ea8dc4b6Seschrock 			usage();
971ea8dc4b6Seschrock 			return (2);
972ea8dc4b6Seschrock 		}
973ea8dc4b6Seschrock 
974ea8dc4b6Seschrock 		if (strcmp(cancel, "all") == 0) {
975ea8dc4b6Seschrock 			return (cancel_all_handlers());
976ea8dc4b6Seschrock 		} else {
977ea8dc4b6Seschrock 			int id = (int)strtol(cancel, &end, 10);
978ea8dc4b6Seschrock 			if (*end != '\0') {
979ea8dc4b6Seschrock 				(void) fprintf(stderr, "invalid handle id '%s':"
980ea8dc4b6Seschrock 				    " must be an integer or 'all'\n", cancel);
981ea8dc4b6Seschrock 				usage();
982ea8dc4b6Seschrock 				return (1);
983ea8dc4b6Seschrock 			}
984ea8dc4b6Seschrock 			return (cancel_handler(id));
985ea8dc4b6Seschrock 		}
986ea8dc4b6Seschrock 	}
987ea8dc4b6Seschrock 
988ea8dc4b6Seschrock 	if (device != NULL) {
989ea8dc4b6Seschrock 		/*
990ea8dc4b6Seschrock 		 * Device (-d) injection uses a completely different mechanism
991ea8dc4b6Seschrock 		 * for doing injection, so handle it separately here.
992ea8dc4b6Seschrock 		 */
993ea8dc4b6Seschrock 		if (raw != NULL || range != NULL || type != TYPE_INVAL ||
994*12a8814cSTom Caputi 		    level != 0 || record.zi_cmd != ZINJECT_UNINITIALIZED ||
995*12a8814cSTom Caputi 		    dvas != 0) {
996ea8dc4b6Seschrock 			(void) fprintf(stderr, "device (-d) incompatible with "
997ea8dc4b6Seschrock 			    "data error injection\n");
998ea8dc4b6Seschrock 			usage();
999ea8dc4b6Seschrock 			return (2);
1000ea8dc4b6Seschrock 		}
1001ea8dc4b6Seschrock 
1002ea8dc4b6Seschrock 		if (argc != 1) {
1003ea8dc4b6Seschrock 			(void) fprintf(stderr, "device (-d) injection requires "
1004ea8dc4b6Seschrock 			    "a single pool name\n");
1005ea8dc4b6Seschrock 			usage();
1006ea8dc4b6Seschrock 			return (2);
1007ea8dc4b6Seschrock 		}
1008ea8dc4b6Seschrock 
1009ea8dc4b6Seschrock 		(void) strcpy(pool, argv[0]);
1010ea8dc4b6Seschrock 		dataset[0] = '\0';
1011ea8dc4b6Seschrock 
1012ea8dc4b6Seschrock 		if (error == ECKSUM) {
1013ea8dc4b6Seschrock 			(void) fprintf(stderr, "device error type must be "
1014ea8dc4b6Seschrock 			    "'io' or 'nxio'\n");
1015ea8dc4b6Seschrock 			return (1);
1016ea8dc4b6Seschrock 		}
1017ea8dc4b6Seschrock 
10188f18d1faSGeorge Wilson 		record.zi_iotype = io_type;
101921bf64a7Sgw 		if (translate_device(pool, device, label, &record) != 0)
1020ea8dc4b6Seschrock 			return (1);
1021ea8dc4b6Seschrock 		if (!error)
1022ea8dc4b6Seschrock 			error = ENXIO;
10238f18d1faSGeorge Wilson 
10248f18d1faSGeorge Wilson 		if (action != VDEV_STATE_UNKNOWN)
10258f18d1faSGeorge Wilson 			return (perform_action(pool, &record, action));
10268f18d1faSGeorge Wilson 
1027ea8dc4b6Seschrock 	} else if (raw != NULL) {
102888ecc943SGeorge Wilson 		if (range != NULL || type != TYPE_INVAL || level != 0 ||
1029*12a8814cSTom Caputi 		    record.zi_cmd != ZINJECT_UNINITIALIZED ||
1030*12a8814cSTom Caputi 		    record.zi_freq > 0 || dvas != 0) {
1031ea8dc4b6Seschrock 			(void) fprintf(stderr, "raw (-b) format with "
1032ea8dc4b6Seschrock 			    "any other options\n");
1033ea8dc4b6Seschrock 			usage();
1034ea8dc4b6Seschrock 			return (2);
1035ea8dc4b6Seschrock 		}
1036ea8dc4b6Seschrock 
1037ea8dc4b6Seschrock 		if (argc != 1) {
1038ea8dc4b6Seschrock 			(void) fprintf(stderr, "raw (-b) format expects a "
1039ea8dc4b6Seschrock 			    "single pool name\n");
1040ea8dc4b6Seschrock 			usage();
1041ea8dc4b6Seschrock 			return (2);
1042ea8dc4b6Seschrock 		}
1043ea8dc4b6Seschrock 
1044ea8dc4b6Seschrock 		(void) strcpy(pool, argv[0]);
1045ea8dc4b6Seschrock 		dataset[0] = '\0';
1046ea8dc4b6Seschrock 
1047ea8dc4b6Seschrock 		if (error == ENXIO) {
1048ea8dc4b6Seschrock 			(void) fprintf(stderr, "data error type must be "
1049ea8dc4b6Seschrock 			    "'checksum' or 'io'\n");
1050ea8dc4b6Seschrock 			return (1);
1051ea8dc4b6Seschrock 		}
1052ea8dc4b6Seschrock 
1053283b8460SGeorge.Wilson 		record.zi_cmd = ZINJECT_DATA_FAULT;
1054ea8dc4b6Seschrock 		if (translate_raw(raw, &record) != 0)
1055ea8dc4b6Seschrock 			return (1);
1056ea8dc4b6Seschrock 		if (!error)
1057ea8dc4b6Seschrock 			error = EIO;
1058283b8460SGeorge.Wilson 	} else if (record.zi_cmd == ZINJECT_PANIC) {
105988ecc943SGeorge Wilson 		if (raw != NULL || range != NULL || type != TYPE_INVAL ||
1060*12a8814cSTom Caputi 		    level != 0 || device != NULL || record.zi_freq > 0 ||
1061*12a8814cSTom Caputi 		    dvas != 0) {
106288ecc943SGeorge Wilson 			(void) fprintf(stderr, "panic (-p) incompatible with "
106388ecc943SGeorge Wilson 			    "other options\n");
106488ecc943SGeorge Wilson 			usage();
106588ecc943SGeorge Wilson 			return (2);
106688ecc943SGeorge Wilson 		}
106788ecc943SGeorge Wilson 
10681195e687SMark J Musante 		if (argc < 1 || argc > 2) {
106988ecc943SGeorge Wilson 			(void) fprintf(stderr, "panic (-p) injection requires "
10701195e687SMark J Musante 			    "a single pool name and an optional id\n");
107188ecc943SGeorge Wilson 			usage();
107288ecc943SGeorge Wilson 			return (2);
107388ecc943SGeorge Wilson 		}
107488ecc943SGeorge Wilson 
1075468c413aSTim Haley 		(void) strcpy(pool, argv[0]);
10761195e687SMark J Musante 		if (argv[1] != NULL)
10771195e687SMark J Musante 			record.zi_type = atoi(argv[1]);
1078468c413aSTim Haley 		dataset[0] = '\0';
1079283b8460SGeorge.Wilson 	} else if (record.zi_cmd == ZINJECT_IGNORED_WRITES) {
1080*12a8814cSTom Caputi 		if (raw != NULL || range != NULL || type != TYPE_INVAL ||
1081*12a8814cSTom Caputi 		    level != 0 || record.zi_freq > 0 || dvas != 0) {
1082*12a8814cSTom Caputi 			(void) fprintf(stderr, "hardware failure (-I) "
1083*12a8814cSTom Caputi 			    "incompatible with other options\n");
1084*12a8814cSTom Caputi 			usage();
1085*12a8814cSTom Caputi 			libzfs_fini(g_zfs);
1086*12a8814cSTom Caputi 			return (2);
1087*12a8814cSTom Caputi 		}
1088*12a8814cSTom Caputi 
1089468c413aSTim Haley 		if (nowrites == 0) {
1090468c413aSTim Haley 			(void) fprintf(stderr, "-s or -g meaningless "
1091468c413aSTim Haley 			    "without -I (ignore writes)\n");
1092468c413aSTim Haley 			usage();
1093468c413aSTim Haley 			return (2);
1094468c413aSTim Haley 		} else if (dur_secs && dur_txg) {
1095468c413aSTim Haley 			(void) fprintf(stderr, "choose a duration either "
1096468c413aSTim Haley 			    "in seconds (-s) or a number of txgs (-g) "
1097468c413aSTim Haley 			    "but not both\n");
1098468c413aSTim Haley 			usage();
1099468c413aSTim Haley 			return (2);
1100468c413aSTim Haley 		} else if (argc != 1) {
1101468c413aSTim Haley 			(void) fprintf(stderr, "ignore writes (-I) "
1102468c413aSTim Haley 			    "injection requires a single pool name\n");
1103468c413aSTim Haley 			usage();
1104468c413aSTim Haley 			return (2);
1105468c413aSTim Haley 		}
1106468c413aSTim Haley 
110788ecc943SGeorge Wilson 		(void) strcpy(pool, argv[0]);
110888ecc943SGeorge Wilson 		dataset[0] = '\0';
1109ea8dc4b6Seschrock 	} else if (type == TYPE_INVAL) {
1110ea8dc4b6Seschrock 		if (flags == 0) {
1111ea8dc4b6Seschrock 			(void) fprintf(stderr, "at least one of '-b', '-d', "
1112468c413aSTim Haley 			    "'-t', '-a', '-p', '-I' or '-u' "
1113468c413aSTim Haley 			    "must be specified\n");
1114ea8dc4b6Seschrock 			usage();
1115ea8dc4b6Seschrock 			return (2);
1116ea8dc4b6Seschrock 		}
1117ea8dc4b6Seschrock 
1118ea8dc4b6Seschrock 		if (argc == 1 && (flags & ZINJECT_UNLOAD_SPA)) {
1119ea8dc4b6Seschrock 			(void) strcpy(pool, argv[0]);
1120ea8dc4b6Seschrock 			dataset[0] = '\0';
1121ea8dc4b6Seschrock 		} else if (argc != 0) {
1122ea8dc4b6Seschrock 			(void) fprintf(stderr, "extraneous argument for "
1123ea8dc4b6Seschrock 			    "'-f'\n");
1124ea8dc4b6Seschrock 			usage();
1125ea8dc4b6Seschrock 			return (2);
1126ea8dc4b6Seschrock 		}
1127ea8dc4b6Seschrock 
1128ea8dc4b6Seschrock 		flags |= ZINJECT_NULL;
1129ea8dc4b6Seschrock 	} else {
1130ea8dc4b6Seschrock 		if (argc != 1) {
1131ea8dc4b6Seschrock 			(void) fprintf(stderr, "missing object\n");
1132ea8dc4b6Seschrock 			usage();
1133ea8dc4b6Seschrock 			return (2);
1134ea8dc4b6Seschrock 		}
1135ea8dc4b6Seschrock 
1136ea8dc4b6Seschrock 		if (error == ENXIO) {
1137ea8dc4b6Seschrock 			(void) fprintf(stderr, "data error type must be "
1138ea8dc4b6Seschrock 			    "'checksum' or 'io'\n");
1139ea8dc4b6Seschrock 			return (1);
1140ea8dc4b6Seschrock 		}
1141ea8dc4b6Seschrock 
1142*12a8814cSTom Caputi 		if (dvas != 0) {
1143*12a8814cSTom Caputi 			if (error == EACCES || error == EINVAL) {
1144*12a8814cSTom Caputi 				(void) fprintf(stderr, "the '-C' option may "
1145*12a8814cSTom Caputi 				    "not be used with logical data errors "
1146*12a8814cSTom Caputi 				    "'decrypt' and 'decompress'\n");
1147*12a8814cSTom Caputi 				libzfs_fini(g_zfs);
1148*12a8814cSTom Caputi 				return (1);
1149*12a8814cSTom Caputi 			}
1150*12a8814cSTom Caputi 
1151*12a8814cSTom Caputi 			record.zi_dvas = dvas;
1152*12a8814cSTom Caputi 		}
1153*12a8814cSTom Caputi 
1154283b8460SGeorge.Wilson 		record.zi_cmd = ZINJECT_DATA_FAULT;
1155ea8dc4b6Seschrock 		if (translate_record(type, argv[0], range, level, &record, pool,
1156ea8dc4b6Seschrock 		    dataset) != 0)
1157ea8dc4b6Seschrock 			return (1);
1158ea8dc4b6Seschrock 		if (!error)
1159ea8dc4b6Seschrock 			error = EIO;
1160ea8dc4b6Seschrock 	}
1161ea8dc4b6Seschrock 
1162ea8dc4b6Seschrock 	/*
1163ea8dc4b6Seschrock 	 * If this is pool-wide metadata, unmount everything.  The ioctl() will
1164ea8dc4b6Seschrock 	 * unload the pool, so that we trigger spa-wide reopen of metadata next
1165ea8dc4b6Seschrock 	 * time we access the pool.
1166ea8dc4b6Seschrock 	 */
1167ea8dc4b6Seschrock 	if (dataset[0] != '\0' && domount) {
1168990b4856Slling 		if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_DATASET)) == NULL)
1169ea8dc4b6Seschrock 			return (1);
1170ea8dc4b6Seschrock 
1171ea8dc4b6Seschrock 		if (zfs_unmount(zhp, NULL, 0) != 0)
1172ea8dc4b6Seschrock 			return (1);
1173ea8dc4b6Seschrock 	}
1174ea8dc4b6Seschrock 
1175ea8dc4b6Seschrock 	record.zi_error = error;
1176ea8dc4b6Seschrock 
1177ea8dc4b6Seschrock 	ret = register_handler(pool, flags, &record, quiet);
1178ea8dc4b6Seschrock 
1179ea8dc4b6Seschrock 	if (dataset[0] != '\0' && domount)
1180ea8dc4b6Seschrock 		ret = (zfs_mount(zhp, NULL, 0) != 0);
1181ea8dc4b6Seschrock 
118299653d4eSeschrock 	libzfs_fini(g_zfs);
118399653d4eSeschrock 
1184ea8dc4b6Seschrock 	return (ret);
1185ea8dc4b6Seschrock }
1186