]> git.proxmox.com Git - mirror_zfs.git/blame - cmd/zinject/zinject.c
zinject: inject device errors into ioctls
[mirror_zfs.git] / cmd / zinject / zinject.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1d3ba0bf 9 * or https://opensource.org/licenses/CDDL-1.0.
34dc7c2f
BB
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
428870ff 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
26ef0cc7 23 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
0241e491 24 * Copyright (c) 2017, Intel Corporation.
fa480fe5 25 * Copyright (c) 2024, Klara Inc.
34dc7c2f
BB
26 */
27
34dc7c2f
BB
28/*
29 * ZFS Fault Injector
30 *
31 * This userland component takes a set of options and uses libzpool to translate
32 * from a user-visible object type and name to an internal representation.
33 * There are two basic types of faults: device faults and data faults.
34 *
35 *
36 * DEVICE FAULTS
37 *
38 * Errors can be injected into a particular vdev using the '-d' option. This
39 * option takes a path or vdev GUID to uniquely identify the device within a
d977122d
DB
40 * pool. There are four types of errors that can be injected, IO, ENXIO,
41 * ECHILD, and EILSEQ. These can be controlled through the '-e' option and the
42 * default is ENXIO. For EIO failures, any attempt to read data from the device
43 * will return EIO, but a subsequent attempt to reopen the device will succeed.
44 * For ENXIO failures, any attempt to read from the device will return EIO, but
45 * any attempt to reopen the device will also return ENXIO. The EILSEQ failures
46 * only apply to read operations (-T read) and will flip a bit after the device
47 * has read the original data.
48 *
b128c09f 49 * For label faults, the -L option must be specified. This allows faults
428870ff
BB
50 * to be injected into either the nvlist, uberblock, pad1, or pad2 region
51 * of all the labels for the specified device.
34dc7c2f
BB
52 *
53 * This form of the command looks like:
54 *
428870ff 55 * zinject -d device [-e errno] [-L <uber | nvlist | pad1 | pad2>] pool
34dc7c2f
BB
56 *
57 *
58 * DATA FAULTS
59 *
60 * We begin with a tuple of the form:
61 *
62 * <type,level,range,object>
63 *
64 * type A string describing the type of data to target. Each type
65 * implicitly describes how to interpret 'object'. Currently,
66 * the following values are supported:
67 *
68 * data User data for a file
69 * dnode Dnode for a file or directory
70 *
71 * The following MOS objects are special. Instead of injecting
72 * errors on a particular object or blkid, we inject errors across
73 * all objects of the given type.
74 *
75 * mos Any data in the MOS
76 * mosdir object directory
77 * config pool configuration
428870ff 78 * bpobj blkptr list
34dc7c2f
BB
79 * spacemap spacemap
80 * metaslab metaslab
81 * errlog persistent error log
82 *
83 * level Object level. Defaults to '0', not applicable to all types. If
84 * a range is given, this corresponds to the indirect block
85 * corresponding to the specific range.
86 *
87 * range A numerical range [start,end) within the object. Defaults to
88 * the full size of the file.
89 *
90 * object A string describing the logical location of the object. For
91 * files and directories (currently the only supported types),
92 * this is the path of the object on disk.
93 *
94 * This is translated, via libzpool, into the following internal representation:
95 *
96 * <type,objset,object,level,range>
97 *
98 * These types should be self-explanatory. This tuple is then passed to the
99 * kernel via a special ioctl() to initiate fault injection for the given
100 * object. Note that 'type' is not strictly necessary for fault injection, but
101 * is used when translating existing faults into a human-readable string.
102 *
103 *
104 * The command itself takes one of the forms:
105 *
106 * zinject
107 * zinject <-a | -u pool>
108 * zinject -c <id|all>
109 * zinject [-q] <-t type> [-f freq] [-u] [-a] [-m] [-e errno] [-l level]
110 * [-r range] <object>
111 * zinject [-f freq] [-a] [-m] [-u] -b objset:object:level:start:end pool
112 *
113 * With no arguments, the command prints all currently registered injection
114 * handlers, with their numeric identifiers.
115 *
116 * The '-c' option will clear the given handler, or all handlers if 'all' is
117 * specified.
118 *
119 * The '-e' option takes a string describing the errno to simulate. This must
c3bd3fb4
TC
120 * be one of 'io', 'checksum', 'decompress', or 'decrypt'. In most cases this
121 * will result in the same behavior, but RAID-Z will produce a different set of
122 * ereports for this situation.
34dc7c2f
BB
123 *
124 * The '-a', '-u', and '-m' flags toggle internal flush behavior. If '-a' is
125 * specified, then the ARC cache is flushed appropriately. If '-u' is
126 * specified, then the underlying SPA is unloaded. Either of these flags can be
127 * specified independently of any other handlers. The '-m' flag automatically
128 * does an unmount and remount of the underlying dataset to aid in flushing the
129 * cache.
130 *
131 * The '-f' flag controls the frequency of errors injected, expressed as a
0241e491 132 * real number percentage between 0.0001 and 100. The default is 100.
34dc7c2f
BB
133 *
134 * The this form is responsible for actually injecting the handler into the
135 * framework. It takes the arguments described above, translates them to the
136 * internal tuple using libzpool, and then issues an ioctl() to register the
137 * handler.
138 *
139 * The final form can target a specific bookmark, regardless of whether a
140 * human-readable interface has been designed. It allows developers to specify
141 * a particular block by number.
142 */
143
144#include <errno.h>
145#include <fcntl.h>
146#include <stdio.h>
147#include <stdlib.h>
d465fc58 148#include <string.h>
34dc7c2f
BB
149#include <strings.h>
150#include <unistd.h>
151
152#include <sys/fs/zfs.h>
153#include <sys/mount.h>
154
155#include <libzfs.h>
156
157#undef verify /* both libzfs.h and zfs_context.h want to define this */
158
159#include "zinject.h"
160
161libzfs_handle_t *g_zfs;
162int zfs_fd;
163
d465fc58 164static const char *const errtable[TYPE_INVAL] = {
34dc7c2f
BB
165 "data",
166 "dnode",
167 "mos",
168 "mosdir",
169 "metaslab",
170 "config",
428870ff 171 "bpobj",
34dc7c2f 172 "spacemap",
b128c09f
BB
173 "errlog",
174 "uber",
428870ff
BB
175 "nvlist",
176 "pad1",
177 "pad2"
34dc7c2f
BB
178};
179
180static err_type_t
181name_to_type(const char *arg)
182{
183 int i;
184 for (i = 0; i < TYPE_INVAL; i++)
185 if (strcmp(errtable[i], arg) == 0)
186 return (i);
187
188 return (TYPE_INVAL);
189}
190
191static const char *
192type_to_name(uint64_t type)
193{
194 switch (type) {
195 case DMU_OT_OBJECT_DIRECTORY:
196 return ("mosdir");
197 case DMU_OT_OBJECT_ARRAY:
198 return ("metaslab");
199 case DMU_OT_PACKED_NVLIST:
200 return ("config");
428870ff
BB
201 case DMU_OT_BPOBJ:
202 return ("bpobj");
34dc7c2f
BB
203 case DMU_OT_SPACE_MAP:
204 return ("spacemap");
205 case DMU_OT_ERROR_LOG:
206 return ("errlog");
207 default:
208 return ("-");
209 }
210}
211
fa480fe5
RN
212struct errstr {
213 int err;
214 const char *str;
215};
216static const struct errstr errstrtable[] = {
217 { EIO, "io" },
218 { ECKSUM, "checksum" },
219 { EINVAL, "decompress" },
220 { EACCES, "decrypt" },
221 { ENXIO, "nxio" },
222 { ECHILD, "dtl" },
223 { EILSEQ, "corrupt" },
224 { 0, NULL },
225};
226
227static int
228str_to_err(const char *str)
229{
230 for (int i = 0; errstrtable[i].str != NULL; i++)
231 if (strcasecmp(errstrtable[i].str, str) == 0)
232 return (errstrtable[i].err);
233 return (-1);
234}
235static const char *
236err_to_str(int err)
237{
238 for (int i = 0; errstrtable[i].str != NULL; i++)
239 if (errstrtable[i].err == err)
240 return (errstrtable[i].str);
241 return ("[unknown]");
242}
34dc7c2f
BB
243
244/*
245 * Print usage message.
246 */
247void
248usage(void)
249{
250 (void) printf(
251 "usage:\n"
252 "\n"
253 "\tzinject\n"
254 "\n"
255 "\t\tList all active injection records.\n"
256 "\n"
257 "\tzinject -c <id|all>\n"
258 "\n"
259 "\t\tClear the particular record (if given a numeric ID), or\n"
2627e752 260 "\t\tall records if 'all' is specified.\n"
34dc7c2f 261 "\n"
428870ff
BB
262 "\tzinject -p <function name> pool\n"
263 "\t\tInject a panic fault at the specified function. Only \n"
264 "\t\tfunctions which call spa_vdev_config_exit(), or \n"
265 "\t\tspa_vdev_exit() will trigger a panic.\n"
266 "\n"
267 "\tzinject -d device [-e errno] [-L <nvlist|uber|pad1|pad2>] [-F]\n"
76d1dde9 268 "\t\t[-T <read|write|free|claim|ioctl|all>] [-f frequency] pool\n\n"
b128c09f 269 "\t\tInject a fault into a particular device or the device's\n"
428870ff
BB
270 "\t\tlabel. Label injection can either be 'nvlist', 'uber',\n "
271 "\t\t'pad1', or 'pad2'.\n"
d977122d
DB
272 "\t\t'errno' can be 'nxio' (the default), 'io', 'dtl', or\n"
273 "\t\t'corrupt' (bit flip).\n"
0241e491
DB
274 "\t\t'frequency' is a value between 0.0001 and 100.0 that limits\n"
275 "\t\tdevice error injection to a percentage of the IOs.\n"
34dc7c2f 276 "\n"
cc92e9d0
GW
277 "\tzinject -d device -A <degrade|fault> -D <delay secs> pool\n"
278 "\t\tPerform a specific action on a particular device.\n"
428870ff 279 "\n"
26ef0cc7
TH
280 "\tzinject -d device -D latency:lanes pool\n"
281 "\n"
282 "\t\tAdd an artificial delay to IO requests on a particular\n"
283 "\t\tdevice, such that the requests take a minimum of 'latency'\n"
284 "\t\tmilliseconds to complete. Each delay has an associated\n"
285 "\t\tnumber of 'lanes' which defines the number of concurrent\n"
286 "\t\tIO requests that can be processed.\n"
287 "\n"
288 "\t\tFor example, with a single lane delay of 10 ms (-D 10:1),\n"
289 "\t\tthe device will only be able to service a single IO request\n"
290 "\t\tat a time with each request taking 10 ms to complete. So,\n"
291 "\t\tif only a single request is submitted every 10 ms, the\n"
292 "\t\taverage latency will be 10 ms; but if more than one request\n"
293 "\t\tis submitted every 10 ms, the average latency will be more\n"
294 "\t\tthan 10 ms.\n"
295 "\n"
296 "\t\tSimilarly, if a delay of 10 ms is specified to have two\n"
297 "\t\tlanes (-D 10:2), then the device will be able to service\n"
298 "\t\ttwo requests at a time, each with a minimum latency of\n"
299 "\t\t10 ms. So, if two requests are submitted every 10 ms, then\n"
300 "\t\tthe average latency will be 10 ms; but if more than two\n"
301 "\t\trequests are submitted every 10 ms, the average latency\n"
302 "\t\twill be more than 10 ms.\n"
303 "\n"
304 "\t\tAlso note, these delays are additive. So two invocations\n"
305 "\t\tof '-D 10:1', is roughly equivalent to a single invocation\n"
306 "\t\tof '-D 10:2'. This also means, one can specify multiple\n"
307 "\t\tlanes with differing target latencies. For example, an\n"
308 "\t\tinvocation of '-D 10:1' followed by '-D 25:2' will\n"
309 "\t\tcreate 3 lanes on the device; one lane with a latency\n"
310 "\t\tof 10 ms and two lanes with a 25 ms latency.\n"
311 "\n"
428870ff
BB
312 "\tzinject -I [-s <seconds> | -g <txgs>] pool\n"
313 "\t\tCause the pool to stop writing blocks yet not\n"
314 "\t\treport errors for a duration. Simulates buggy hardware\n"
315 "\t\tthat fails to honor cache flush requests.\n"
316 "\t\tDefault duration is 30 seconds. The machine is panicked\n"
317 "\t\tat the end of the duration.\n"
318 "\n"
34dc7c2f
BB
319 "\tzinject -b objset:object:level:blkid pool\n"
320 "\n"
321 "\t\tInject an error into pool 'pool' with the numeric bookmark\n"
322 "\t\tspecified by the remaining tuple. Each number is in\n"
4e33ba4c 323 "\t\thexadecimal, and only one block can be specified.\n"
34dc7c2f 324 "\n"
ab7615d9
TC
325 "\tzinject [-q] <-t type> [-C dvas] [-e errno] [-l level]\n"
326 "\t\t[-r range] [-a] [-m] [-u] [-f freq] <object>\n"
34dc7c2f
BB
327 "\n"
328 "\t\tInject an error into the object specified by the '-t' option\n"
329 "\t\tand the object descriptor. The 'object' parameter is\n"
2627e752 330 "\t\tinterpreted depending on the '-t' option.\n"
34dc7c2f
BB
331 "\n"
332 "\t\t-q\tQuiet mode. Only print out the handler number added.\n"
be9a5c35 333 "\t\t-e\tInject a specific error. Must be one of 'io',\n"
ab7615d9
TC
334 "\t\t\t'checksum', 'decompress', or 'decrypt'. Default is 'io'.\n"
335 "\t\t-C\tInject the given error only into specific DVAs. The\n"
336 "\t\t\tDVAs should be specified as a list of 0-indexed DVAs\n"
337 "\t\t\tseparated by commas (ex. '0,2').\n"
34dc7c2f
BB
338 "\t\t-l\tInject error at a particular block level. Default is "
339 "0.\n"
340 "\t\t-m\tAutomatically remount underlying filesystem.\n"
341 "\t\t-r\tInject error over a particular logical range of an\n"
342 "\t\t\tobject. Will be translated to the appropriate blkid\n"
343 "\t\t\trange according to the object's properties.\n"
344 "\t\t-a\tFlush the ARC cache. Can be specified without any\n"
345 "\t\t\tassociated object.\n"
346 "\t\t-u\tUnload the associated pool. Can be specified with only\n"
347 "\t\t\ta pool object.\n"
348 "\t\t-f\tOnly inject errors a fraction of the time. Expressed as\n"
0241e491 349 "\t\t\ta percentage between 0.0001 and 100.\n"
34dc7c2f
BB
350 "\n"
351 "\t-t data\t\tInject an error into the plain file contents of a\n"
352 "\t\t\tfile. The object must be specified as a complete path\n"
353 "\t\t\tto a file on a ZFS filesystem.\n"
354 "\n"
355 "\t-t dnode\tInject an error into the metadnode in the block\n"
356 "\t\t\tcorresponding to the dnode for a file or directory. The\n"
357 "\t\t\t'-r' option is incompatible with this mode. The object\n"
358 "\t\t\tis specified as a complete path to a file or directory\n"
359 "\t\t\ton a ZFS filesystem.\n"
360 "\n"
361 "\t-t <mos>\tInject errors into the MOS for objects of the given\n"
428870ff 362 "\t\t\ttype. Valid types are: mos, mosdir, config, bpobj,\n"
34dc7c2f
BB
363 "\t\t\tspacemap, metaslab, errlog. The only valid <object> is\n"
364 "\t\t\tthe poolname.\n");
365}
366
367static int
368iter_handlers(int (*func)(int, const char *, zinject_record_t *, void *),
369 void *data)
370{
d3773fda 371 zfs_cmd_t zc = {"\0"};
34dc7c2f
BB
372 int ret;
373
24cf9f4e 374 while (zfs_ioctl(g_zfs, ZFS_IOC_INJECT_LIST_NEXT, &zc) == 0)
34dc7c2f
BB
375 if ((ret = func((int)zc.zc_guid, zc.zc_name,
376 &zc.zc_inject_record, data)) != 0)
377 return (ret);
378
428870ff
BB
379 if (errno != ENOENT) {
380 (void) fprintf(stderr, "Unable to list handlers: %s\n",
381 strerror(errno));
382 return (-1);
383 }
384
34dc7c2f
BB
385 return (0);
386}
387
388static int
389print_data_handler(int id, const char *pool, zinject_record_t *record,
390 void *data)
391{
392 int *count = data;
393
428870ff 394 if (record->zi_guid != 0 || record->zi_func[0] != '\0')
34dc7c2f
BB
395 return (0);
396
397 if (*count == 0) {
ab7615d9
TC
398 (void) printf("%3s %-15s %-6s %-6s %-8s %3s %-4s "
399 "%-15s\n", "ID", "POOL", "OBJSET", "OBJECT", "TYPE",
400 "LVL", "DVAs", "RANGE");
34dc7c2f 401 (void) printf("--- --------------- ------ "
ab7615d9 402 "------ -------- --- ---- ---------------\n");
34dc7c2f
BB
403 }
404
405 *count += 1;
406
ab7615d9
TC
407 (void) printf("%3d %-15s %-6llu %-6llu %-8s %-3d 0x%02x ",
408 id, pool, (u_longlong_t)record->zi_objset,
409 (u_longlong_t)record->zi_object, type_to_name(record->zi_type),
410 record->zi_level, record->zi_dvas);
411
34dc7c2f
BB
412
413 if (record->zi_start == 0 &&
414 record->zi_end == -1ULL)
415 (void) printf("all\n");
416 else
417 (void) printf("[%llu, %llu]\n", (u_longlong_t)record->zi_start,
418 (u_longlong_t)record->zi_end);
419
420 return (0);
421}
422
423static int
424print_device_handler(int id, const char *pool, zinject_record_t *record,
425 void *data)
426{
fa480fe5
RN
427 static const char *iotypestr[] = {
428 "null", "read", "write", "free", "claim", "ioctl", "trim", "all",
429 };
430
34dc7c2f
BB
431 int *count = data;
432
428870ff 433 if (record->zi_guid == 0 || record->zi_func[0] != '\0')
34dc7c2f
BB
434 return (0);
435
26ef0cc7
TH
436 if (record->zi_cmd == ZINJECT_DELAY_IO)
437 return (0);
438
34dc7c2f 439 if (*count == 0) {
fa480fe5
RN
440 (void) printf("%3s %-15s %-16s %-5s %-10s %-9s\n",
441 "ID", "POOL", "GUID", "TYPE", "ERROR", "FREQ");
442 (void) printf(
443 "--- --------------- ---------------- "
444 "----- ---------- ---------\n");
34dc7c2f
BB
445 }
446
447 *count += 1;
448
fa480fe5
RN
449 double freq = record->zi_freq == 0 ? 100.0f :
450 (((double)record->zi_freq) / ZI_PERCENTAGE_MAX) * 100.0f;
451
452 (void) printf("%3d %-15s %llx %-5s %-10s %8.4f%%\n", id, pool,
453 (u_longlong_t)record->zi_guid, iotypestr[record->zi_iotype],
454 err_to_str(record->zi_error), freq);
34dc7c2f
BB
455
456 return (0);
457}
458
26ef0cc7
TH
459static int
460print_delay_handler(int id, const char *pool, zinject_record_t *record,
461 void *data)
462{
463 int *count = data;
464
465 if (record->zi_guid == 0 || record->zi_func[0] != '\0')
466 return (0);
467
468 if (record->zi_cmd != ZINJECT_DELAY_IO)
469 return (0);
470
471 if (*count == 0) {
472 (void) printf("%3s %-15s %-15s %-15s %s\n",
473 "ID", "POOL", "DELAY (ms)", "LANES", "GUID");
474 (void) printf("--- --------------- --------------- "
475 "--------------- ----------------\n");
476 }
477
478 *count += 1;
479
480 (void) printf("%3d %-15s %-15llu %-15llu %llx\n", id, pool,
481 (u_longlong_t)NSEC2MSEC(record->zi_timer),
482 (u_longlong_t)record->zi_nlanes,
483 (u_longlong_t)record->zi_guid);
484
485 return (0);
486}
487
428870ff
BB
488static int
489print_panic_handler(int id, const char *pool, zinject_record_t *record,
490 void *data)
491{
492 int *count = data;
493
494 if (record->zi_func[0] == '\0')
495 return (0);
496
497 if (*count == 0) {
498 (void) printf("%3s %-15s %s\n", "ID", "POOL", "FUNCTION");
499 (void) printf("--- --------------- ----------------\n");
500 }
501
502 *count += 1;
503
504 (void) printf("%3d %-15s %s\n", id, pool, record->zi_func);
505
506 return (0);
507}
508
34dc7c2f
BB
509/*
510 * Print all registered error handlers. Returns the number of handlers
511 * registered.
512 */
513static int
514print_all_handlers(void)
515{
572e2857 516 int count = 0, total = 0;
34dc7c2f
BB
517
518 (void) iter_handlers(print_device_handler, &count);
572e2857
BB
519 if (count > 0) {
520 total += count;
521 (void) printf("\n");
522 count = 0;
523 }
524
26ef0cc7
TH
525 (void) iter_handlers(print_delay_handler, &count);
526 if (count > 0) {
527 total += count;
528 (void) printf("\n");
529 count = 0;
530 }
531
34dc7c2f 532 (void) iter_handlers(print_data_handler, &count);
572e2857
BB
533 if (count > 0) {
534 total += count;
535 (void) printf("\n");
536 count = 0;
537 }
538
428870ff 539 (void) iter_handlers(print_panic_handler, &count);
34dc7c2f 540
572e2857 541 return (count + total);
34dc7c2f
BB
542}
543
34dc7c2f
BB
544static int
545cancel_one_handler(int id, const char *pool, zinject_record_t *record,
546 void *data)
547{
964e6a49 548 (void) pool, (void) record, (void) data;
d3773fda 549 zfs_cmd_t zc = {"\0"};
34dc7c2f
BB
550
551 zc.zc_guid = (uint64_t)id;
552
24cf9f4e 553 if (zfs_ioctl(g_zfs, ZFS_IOC_CLEAR_FAULT, &zc) != 0) {
34dc7c2f
BB
554 (void) fprintf(stderr, "failed to remove handler %d: %s\n",
555 id, strerror(errno));
556 return (1);
557 }
558
559 return (0);
560}
561
562/*
563 * Remove all fault injection handlers.
564 */
565static int
566cancel_all_handlers(void)
567{
568 int ret = iter_handlers(cancel_one_handler, NULL);
569
428870ff
BB
570 if (ret == 0)
571 (void) printf("removed all registered handlers\n");
34dc7c2f
BB
572
573 return (ret);
574}
575
576/*
577 * Remove a specific fault injection handler.
578 */
579static int
580cancel_handler(int id)
581{
d3773fda 582 zfs_cmd_t zc = {"\0"};
34dc7c2f
BB
583
584 zc.zc_guid = (uint64_t)id;
585
24cf9f4e 586 if (zfs_ioctl(g_zfs, ZFS_IOC_CLEAR_FAULT, &zc) != 0) {
34dc7c2f
BB
587 (void) fprintf(stderr, "failed to remove handler %d: %s\n",
588 id, strerror(errno));
589 return (1);
590 }
591
592 (void) printf("removed handler %d\n", id);
593
594 return (0);
595}
596
597/*
598 * Register a new fault injection handler.
599 */
600static int
601register_handler(const char *pool, int flags, zinject_record_t *record,
602 int quiet)
603{
d3773fda 604 zfs_cmd_t zc = {"\0"};
34dc7c2f 605
0b78aeae 606 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
34dc7c2f
BB
607 zc.zc_inject_record = *record;
608 zc.zc_guid = flags;
609
24cf9f4e 610 if (zfs_ioctl(g_zfs, ZFS_IOC_INJECT_FAULT, &zc) != 0) {
34dc7c2f 611 (void) fprintf(stderr, "failed to add handler: %s\n",
e89f1295 612 errno == EDOM ? "block level exceeds max level of object" :
34dc7c2f
BB
613 strerror(errno));
614 return (1);
615 }
616
617 if (flags & ZINJECT_NULL)
618 return (0);
619
620 if (quiet) {
621 (void) printf("%llu\n", (u_longlong_t)zc.zc_guid);
622 } else {
623 (void) printf("Added handler %llu with the following "
624 "properties:\n", (u_longlong_t)zc.zc_guid);
625 (void) printf(" pool: %s\n", pool);
626 if (record->zi_guid) {
627 (void) printf(" vdev: %llx\n",
628 (u_longlong_t)record->zi_guid);
428870ff
BB
629 } else if (record->zi_func[0] != '\0') {
630 (void) printf(" panic function: %s\n",
631 record->zi_func);
632 } else if (record->zi_duration > 0) {
633 (void) printf(" time: %lld seconds\n",
634 (u_longlong_t)record->zi_duration);
635 } else if (record->zi_duration < 0) {
636 (void) printf(" txgs: %lld \n",
637 (u_longlong_t)-record->zi_duration);
34dc7c2f
BB
638 } else {
639 (void) printf("objset: %llu\n",
640 (u_longlong_t)record->zi_objset);
641 (void) printf("object: %llu\n",
642 (u_longlong_t)record->zi_object);
643 (void) printf(" type: %llu\n",
644 (u_longlong_t)record->zi_type);
645 (void) printf(" level: %d\n", record->zi_level);
646 if (record->zi_start == 0 &&
647 record->zi_end == -1ULL)
648 (void) printf(" range: all\n");
649 else
650 (void) printf(" range: [%llu, %llu)\n",
651 (u_longlong_t)record->zi_start,
652 (u_longlong_t)record->zi_end);
ab7615d9 653 (void) printf(" dvas: 0x%x\n", record->zi_dvas);
34dc7c2f
BB
654 }
655 }
656
657 return (0);
658}
659
65c7cc49 660static int
428870ff
BB
661perform_action(const char *pool, zinject_record_t *record, int cmd)
662{
d3773fda 663 zfs_cmd_t zc = {"\0"};
428870ff
BB
664
665 ASSERT(cmd == VDEV_STATE_DEGRADED || cmd == VDEV_STATE_FAULTED);
666 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
667 zc.zc_guid = record->zi_guid;
668 zc.zc_cookie = cmd;
669
24cf9f4e 670 if (zfs_ioctl(g_zfs, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
428870ff
BB
671 return (0);
672
673 return (1);
674}
675
26ef0cc7
TH
676static int
677parse_delay(char *str, uint64_t *delay, uint64_t *nlanes)
678{
679 unsigned long scan_delay;
680 unsigned long scan_nlanes;
681
682 if (sscanf(str, "%lu:%lu", &scan_delay, &scan_nlanes) != 2)
683 return (1);
684
685 /*
686 * We explicitly disallow a delay of zero here, because we key
687 * off this value being non-zero in translate_device(), to
688 * determine if the fault is a ZINJECT_DELAY_IO fault or not.
689 */
690 if (scan_delay == 0)
691 return (1);
692
693 /*
694 * The units for the CLI delay parameter is milliseconds, but
695 * the data passed to the kernel is interpreted as nanoseconds.
696 * Thus we scale the milliseconds to nanoseconds here, and this
697 * nanosecond value is used to pass the delay to the kernel.
698 */
699 *delay = MSEC2NSEC(scan_delay);
700 *nlanes = scan_nlanes;
701
702 return (0);
703}
704
0241e491
DB
705static int
706parse_frequency(const char *str, uint32_t *percent)
707{
708 double val;
709 char *post;
710
711 val = strtod(str, &post);
712 if (post == NULL || *post != '\0')
713 return (EINVAL);
714
715 /* valid range is [0.0001, 100.0] */
716 val /= 100.0f;
717 if (val < 0.000001f || val > 1.0f)
718 return (ERANGE);
719
720 /* convert to an integer for use by kernel */
721 *percent = ((uint32_t)(val * ZI_PERCENTAGE_MAX));
722
723 return (0);
724}
725
ab7615d9
TC
726/*
727 * This function converts a string specifier for DVAs into a bit mask.
728 * The dva's provided by the user should be 0 indexed and separated by
729 * a comma. For example:
730 * "1" -> 0b0010 (0x2)
731 * "0,1" -> 0b0011 (0x3)
732 * "0,1,2" -> 0b0111 (0x7)
733 */
734static int
735parse_dvas(const char *str, uint32_t *dvas_out)
736{
737 const char *c = str;
738 uint32_t mask = 0;
739 boolean_t need_delim = B_FALSE;
740
741 /* max string length is 5 ("0,1,2") */
742 if (strlen(str) > 5 || strlen(str) == 0)
743 return (EINVAL);
744
745 while (*c != '\0') {
746 switch (*c) {
747 case '0':
748 case '1':
749 case '2':
750 /* check for pipe between DVAs */
751 if (need_delim)
752 return (EINVAL);
753
754 /* check if this DVA has been set already */
755 if (mask & (1 << ((*c) - '0')))
756 return (EINVAL);
757
758 mask |= (1 << ((*c) - '0'));
759 need_delim = B_TRUE;
760 break;
761 case ',':
762 need_delim = B_FALSE;
763 break;
764 default:
765 /* check for invalid character */
766 return (EINVAL);
767 }
768 c++;
769 }
770
771 /* check for dangling delimiter */
772 if (!need_delim)
773 return (EINVAL);
774
775 *dvas_out = mask;
776 return (0);
777}
778
34dc7c2f
BB
779int
780main(int argc, char **argv)
781{
782 int c;
783 char *range = NULL;
784 char *cancel = NULL;
785 char *end;
786 char *raw = NULL;
787 char *device = NULL;
788 int level = 0;
789 int quiet = 0;
790 int error = 0;
791 int domount = 0;
428870ff
BB
792 int io_type = ZIO_TYPES;
793 int action = VDEV_STATE_UNKNOWN;
34dc7c2f 794 err_type_t type = TYPE_INVAL;
b128c09f 795 err_type_t label = TYPE_INVAL;
34dc7c2f 796 zinject_record_t record = { 0 };
a64f903b
GN
797 char pool[MAXNAMELEN] = "";
798 char dataset[MAXNAMELEN] = "";
149e873a 799 zfs_handle_t *zhp = NULL;
428870ff
BB
800 int nowrites = 0;
801 int dur_txg = 0;
802 int dur_secs = 0;
34dc7c2f
BB
803 int ret;
804 int flags = 0;
ab7615d9 805 uint32_t dvas = 0;
34dc7c2f 806
65037d9b 807 if ((g_zfs = libzfs_init()) == NULL) {
afc8f0a6 808 (void) fprintf(stderr, "%s\n", libzfs_error_init(errno));
937210a5 809 return (1);
65037d9b 810 }
937210a5
BB
811
812 libzfs_print_on_error(g_zfs, B_TRUE);
813
814 if ((zfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
815 (void) fprintf(stderr, "failed to open ZFS device\n");
5df39c1e 816 libzfs_fini(g_zfs);
937210a5
BB
817 return (1);
818 }
819
34dc7c2f
BB
820 if (argc == 1) {
821 /*
822 * No arguments. Print the available handlers. If there are no
823 * available handlers, direct the user to '-h' for help
824 * information.
825 */
826 if (print_all_handlers() == 0) {
827 (void) printf("No handlers registered.\n");
828 (void) printf("Run 'zinject -h' for usage "
829 "information.\n");
830 }
5df39c1e 831 libzfs_fini(g_zfs);
34dc7c2f
BB
832 return (0);
833 }
834
428870ff 835 while ((c = getopt(argc, argv,
ab7615d9 836 ":aA:b:C:d:D:f:Fg:qhIc:t:T:l:mr:s:e:uL:p:")) != -1) {
34dc7c2f
BB
837 switch (c) {
838 case 'a':
839 flags |= ZINJECT_FLUSH_ARC;
840 break;
428870ff
BB
841 case 'A':
842 if (strcasecmp(optarg, "degrade") == 0) {
843 action = VDEV_STATE_DEGRADED;
844 } else if (strcasecmp(optarg, "fault") == 0) {
845 action = VDEV_STATE_FAULTED;
846 } else {
847 (void) fprintf(stderr, "invalid action '%s': "
848 "must be 'degrade' or 'fault'\n", optarg);
849 usage();
5df39c1e 850 libzfs_fini(g_zfs);
428870ff
BB
851 return (1);
852 }
853 break;
34dc7c2f
BB
854 case 'b':
855 raw = optarg;
856 break;
857 case 'c':
858 cancel = optarg;
859 break;
ab7615d9
TC
860 case 'C':
861 ret = parse_dvas(optarg, &dvas);
862 if (ret != 0) {
863 (void) fprintf(stderr, "invalid DVA list '%s': "
864 "DVAs should be 0 indexed and separated by "
865 "commas.\n", optarg);
866 usage();
867 libzfs_fini(g_zfs);
868 return (1);
869 }
870 break;
34dc7c2f
BB
871 case 'd':
872 device = optarg;
873 break;
cc92e9d0
GW
874 case 'D':
875 errno = 0;
26ef0cc7
TH
876 ret = parse_delay(optarg, &record.zi_timer,
877 &record.zi_nlanes);
878 if (ret != 0) {
879
cc92e9d0
GW
880 (void) fprintf(stderr, "invalid i/o delay "
881 "value: '%s'\n", optarg);
882 usage();
5df39c1e 883 libzfs_fini(g_zfs);
cc92e9d0
GW
884 return (1);
885 }
886 break;
34dc7c2f 887 case 'e':
fa480fe5
RN
888 error = str_to_err(optarg);
889 if (error < 0) {
34dc7c2f 890 (void) fprintf(stderr, "invalid error type "
fa480fe5
RN
891 "'%s': must be one of: io decompress "
892 "decrypt nxio dtl corrupt\n",
893 optarg);
34dc7c2f 894 usage();
5df39c1e 895 libzfs_fini(g_zfs);
34dc7c2f
BB
896 return (1);
897 }
898 break;
899 case 'f':
0241e491
DB
900 ret = parse_frequency(optarg, &record.zi_freq);
901 if (ret != 0) {
902 (void) fprintf(stderr, "%sfrequency value must "
903 "be in the range [0.0001, 100.0]\n",
904 ret == EINVAL ? "invalid value: " :
905 ret == ERANGE ? "out of range: " : "");
5df39c1e 906 libzfs_fini(g_zfs);
34dc7c2f
BB
907 return (1);
908 }
909 break;
9babb374
BB
910 case 'F':
911 record.zi_failfast = B_TRUE;
912 break;
428870ff
BB
913 case 'g':
914 dur_txg = 1;
915 record.zi_duration = (int)strtol(optarg, &end, 10);
916 if (record.zi_duration <= 0 || *end != '\0') {
917 (void) fprintf(stderr, "invalid duration '%s': "
918 "must be a positive integer\n", optarg);
919 usage();
5df39c1e 920 libzfs_fini(g_zfs);
428870ff
BB
921 return (1);
922 }
923 /* store duration of txgs as its negative */
924 record.zi_duration *= -1;
925 break;
34dc7c2f
BB
926 case 'h':
927 usage();
5df39c1e 928 libzfs_fini(g_zfs);
34dc7c2f 929 return (0);
428870ff
BB
930 case 'I':
931 /* default duration, if one hasn't yet been defined */
932 nowrites = 1;
933 if (dur_secs == 0 && dur_txg == 0)
934 record.zi_duration = 30;
935 break;
34dc7c2f
BB
936 case 'l':
937 level = (int)strtol(optarg, &end, 10);
938 if (*end != '\0') {
939 (void) fprintf(stderr, "invalid level '%s': "
940 "must be an integer\n", optarg);
941 usage();
5df39c1e 942 libzfs_fini(g_zfs);
34dc7c2f
BB
943 return (1);
944 }
945 break;
946 case 'm':
947 domount = 1;
948 break;
428870ff
BB
949 case 'p':
950 (void) strlcpy(record.zi_func, optarg,
951 sizeof (record.zi_func));
cc92e9d0 952 record.zi_cmd = ZINJECT_PANIC;
428870ff 953 break;
34dc7c2f
BB
954 case 'q':
955 quiet = 1;
956 break;
957 case 'r':
958 range = optarg;
e89f1295 959 flags |= ZINJECT_CALC_RANGE;
34dc7c2f 960 break;
428870ff
BB
961 case 's':
962 dur_secs = 1;
963 record.zi_duration = (int)strtol(optarg, &end, 10);
964 if (record.zi_duration <= 0 || *end != '\0') {
965 (void) fprintf(stderr, "invalid duration '%s': "
966 "must be a positive integer\n", optarg);
967 usage();
5df39c1e 968 libzfs_fini(g_zfs);
428870ff
BB
969 return (1);
970 }
971 break;
972 case 'T':
973 if (strcasecmp(optarg, "read") == 0) {
974 io_type = ZIO_TYPE_READ;
975 } else if (strcasecmp(optarg, "write") == 0) {
976 io_type = ZIO_TYPE_WRITE;
977 } else if (strcasecmp(optarg, "free") == 0) {
978 io_type = ZIO_TYPE_FREE;
979 } else if (strcasecmp(optarg, "claim") == 0) {
980 io_type = ZIO_TYPE_CLAIM;
76d1dde9
RN
981 } else if (strcasecmp(optarg, "ioctl") == 0) {
982 io_type = ZIO_TYPE_IOCTL;
428870ff
BB
983 } else if (strcasecmp(optarg, "all") == 0) {
984 io_type = ZIO_TYPES;
985 } else {
986 (void) fprintf(stderr, "invalid I/O type "
987 "'%s': must be 'read', 'write', 'free', "
76d1dde9 988 "'claim', 'ioctl' or 'all'\n", optarg);
428870ff 989 usage();
5df39c1e 990 libzfs_fini(g_zfs);
428870ff
BB
991 return (1);
992 }
993 break;
34dc7c2f 994 case 't':
b128c09f
BB
995 if ((type = name_to_type(optarg)) == TYPE_INVAL &&
996 !MOS_TYPE(type)) {
34dc7c2f
BB
997 (void) fprintf(stderr, "invalid type '%s'\n",
998 optarg);
999 usage();
5df39c1e 1000 libzfs_fini(g_zfs);
34dc7c2f
BB
1001 return (1);
1002 }
1003 break;
1004 case 'u':
1005 flags |= ZINJECT_UNLOAD_SPA;
1006 break;
b128c09f
BB
1007 case 'L':
1008 if ((label = name_to_type(optarg)) == TYPE_INVAL &&
1009 !LABEL_TYPE(type)) {
1010 (void) fprintf(stderr, "invalid label type "
1011 "'%s'\n", optarg);
1012 usage();
5df39c1e 1013 libzfs_fini(g_zfs);
b128c09f
BB
1014 return (1);
1015 }
1016 break;
34dc7c2f
BB
1017 case ':':
1018 (void) fprintf(stderr, "option -%c requires an "
1019 "operand\n", optopt);
1020 usage();
5df39c1e 1021 libzfs_fini(g_zfs);
34dc7c2f
BB
1022 return (1);
1023 case '?':
1024 (void) fprintf(stderr, "invalid option '%c'\n",
1025 optopt);
1026 usage();
5df39c1e 1027 libzfs_fini(g_zfs);
34dc7c2f
BB
1028 return (2);
1029 }
1030 }
1031
1032 argc -= optind;
1033 argv += optind;
1034
cc92e9d0
GW
1035 if (record.zi_duration != 0)
1036 record.zi_cmd = ZINJECT_IGNORED_WRITES;
1037
34dc7c2f
BB
1038 if (cancel != NULL) {
1039 /*
1040 * '-c' is invalid with any other options.
1041 */
1042 if (raw != NULL || range != NULL || type != TYPE_INVAL ||
0241e491 1043 level != 0 || record.zi_cmd != ZINJECT_UNINITIALIZED ||
ab7615d9 1044 record.zi_freq > 0 || dvas != 0) {
34dc7c2f
BB
1045 (void) fprintf(stderr, "cancel (-c) incompatible with "
1046 "any other options\n");
1047 usage();
5df39c1e 1048 libzfs_fini(g_zfs);
34dc7c2f
BB
1049 return (2);
1050 }
1051 if (argc != 0) {
1052 (void) fprintf(stderr, "extraneous argument to '-c'\n");
1053 usage();
5df39c1e 1054 libzfs_fini(g_zfs);
34dc7c2f
BB
1055 return (2);
1056 }
1057
1058 if (strcmp(cancel, "all") == 0) {
1059 return (cancel_all_handlers());
1060 } else {
1061 int id = (int)strtol(cancel, &end, 10);
1062 if (*end != '\0') {
1063 (void) fprintf(stderr, "invalid handle id '%s':"
1064 " must be an integer or 'all'\n", cancel);
1065 usage();
5df39c1e 1066 libzfs_fini(g_zfs);
34dc7c2f
BB
1067 return (1);
1068 }
1069 return (cancel_handler(id));
1070 }
1071 }
1072
1073 if (device != NULL) {
1074 /*
1075 * Device (-d) injection uses a completely different mechanism
1076 * for doing injection, so handle it separately here.
1077 */
1078 if (raw != NULL || range != NULL || type != TYPE_INVAL ||
ab7615d9
TC
1079 level != 0 || record.zi_cmd != ZINJECT_UNINITIALIZED ||
1080 dvas != 0) {
34dc7c2f
BB
1081 (void) fprintf(stderr, "device (-d) incompatible with "
1082 "data error injection\n");
1083 usage();
5df39c1e 1084 libzfs_fini(g_zfs);
34dc7c2f
BB
1085 return (2);
1086 }
1087
1088 if (argc != 1) {
1089 (void) fprintf(stderr, "device (-d) injection requires "
1090 "a single pool name\n");
1091 usage();
5df39c1e 1092 libzfs_fini(g_zfs);
34dc7c2f
BB
1093 return (2);
1094 }
1095
5df39c1e 1096 (void) strlcpy(pool, argv[0], sizeof (pool));
34dc7c2f
BB
1097 dataset[0] = '\0';
1098
1099 if (error == ECKSUM) {
1100 (void) fprintf(stderr, "device error type must be "
d977122d
DB
1101 "'io', 'nxio' or 'corrupt'\n");
1102 libzfs_fini(g_zfs);
1103 return (1);
1104 }
1105
1106 if (error == EILSEQ &&
1107 (record.zi_freq == 0 || io_type != ZIO_TYPE_READ)) {
1108 (void) fprintf(stderr, "device corrupt errors require "
1109 "io type read and a frequency value\n");
5df39c1e 1110 libzfs_fini(g_zfs);
34dc7c2f
BB
1111 return (1);
1112 }
1113
428870ff 1114 record.zi_iotype = io_type;
5df39c1e 1115 if (translate_device(pool, device, label, &record) != 0) {
1116 libzfs_fini(g_zfs);
34dc7c2f 1117 return (1);
5df39c1e 1118 }
cbe88229
DB
1119
1120 if (record.zi_nlanes) {
1121 switch (io_type) {
1122 case ZIO_TYPE_READ:
1123 case ZIO_TYPE_WRITE:
1124 case ZIO_TYPES:
1125 break;
1126 default:
1127 (void) fprintf(stderr, "I/O type for a delay "
1128 "must be 'read' or 'write'\n");
1129 usage();
1130 libzfs_fini(g_zfs);
1131 return (1);
1132 }
1133 }
1134
34dc7c2f
BB
1135 if (!error)
1136 error = ENXIO;
428870ff
BB
1137
1138 if (action != VDEV_STATE_UNKNOWN)
1139 return (perform_action(pool, &record, action));
1140
34dc7c2f 1141 } else if (raw != NULL) {
428870ff 1142 if (range != NULL || type != TYPE_INVAL || level != 0 ||
0241e491 1143 record.zi_cmd != ZINJECT_UNINITIALIZED ||
ab7615d9 1144 record.zi_freq > 0 || dvas != 0) {
34dc7c2f
BB
1145 (void) fprintf(stderr, "raw (-b) format with "
1146 "any other options\n");
1147 usage();
5df39c1e 1148 libzfs_fini(g_zfs);
34dc7c2f
BB
1149 return (2);
1150 }
1151
1152 if (argc != 1) {
1153 (void) fprintf(stderr, "raw (-b) format expects a "
1154 "single pool name\n");
1155 usage();
5df39c1e 1156 libzfs_fini(g_zfs);
34dc7c2f
BB
1157 return (2);
1158 }
1159
5df39c1e 1160 (void) strlcpy(pool, argv[0], sizeof (pool));
34dc7c2f
BB
1161 dataset[0] = '\0';
1162
1163 if (error == ENXIO) {
1164 (void) fprintf(stderr, "data error type must be "
1165 "'checksum' or 'io'\n");
5df39c1e 1166 libzfs_fini(g_zfs);
34dc7c2f
BB
1167 return (1);
1168 }
1169
cc92e9d0 1170 record.zi_cmd = ZINJECT_DATA_FAULT;
5df39c1e 1171 if (translate_raw(raw, &record) != 0) {
1172 libzfs_fini(g_zfs);
34dc7c2f 1173 return (1);
5df39c1e 1174 }
34dc7c2f
BB
1175 if (!error)
1176 error = EIO;
cc92e9d0 1177 } else if (record.zi_cmd == ZINJECT_PANIC) {
428870ff 1178 if (raw != NULL || range != NULL || type != TYPE_INVAL ||
ab7615d9
TC
1179 level != 0 || device != NULL || record.zi_freq > 0 ||
1180 dvas != 0) {
428870ff
BB
1181 (void) fprintf(stderr, "panic (-p) incompatible with "
1182 "other options\n");
1183 usage();
5df39c1e 1184 libzfs_fini(g_zfs);
428870ff
BB
1185 return (2);
1186 }
1187
1188 if (argc < 1 || argc > 2) {
1189 (void) fprintf(stderr, "panic (-p) injection requires "
1190 "a single pool name and an optional id\n");
1191 usage();
5df39c1e 1192 libzfs_fini(g_zfs);
428870ff
BB
1193 return (2);
1194 }
1195
5df39c1e 1196 (void) strlcpy(pool, argv[0], sizeof (pool));
428870ff
BB
1197 if (argv[1] != NULL)
1198 record.zi_type = atoi(argv[1]);
1199 dataset[0] = '\0';
cc92e9d0 1200 } else if (record.zi_cmd == ZINJECT_IGNORED_WRITES) {
ab7615d9
TC
1201 if (raw != NULL || range != NULL || type != TYPE_INVAL ||
1202 level != 0 || record.zi_freq > 0 || dvas != 0) {
1203 (void) fprintf(stderr, "hardware failure (-I) "
1204 "incompatible with other options\n");
1205 usage();
1206 libzfs_fini(g_zfs);
1207 return (2);
1208 }
1209
428870ff
BB
1210 if (nowrites == 0) {
1211 (void) fprintf(stderr, "-s or -g meaningless "
1212 "without -I (ignore writes)\n");
1213 usage();
5df39c1e 1214 libzfs_fini(g_zfs);
428870ff
BB
1215 return (2);
1216 } else if (dur_secs && dur_txg) {
1217 (void) fprintf(stderr, "choose a duration either "
1218 "in seconds (-s) or a number of txgs (-g) "
1219 "but not both\n");
1220 usage();
5df39c1e 1221 libzfs_fini(g_zfs);
428870ff
BB
1222 return (2);
1223 } else if (argc != 1) {
1224 (void) fprintf(stderr, "ignore writes (-I) "
1225 "injection requires a single pool name\n");
1226 usage();
5df39c1e 1227 libzfs_fini(g_zfs);
428870ff
BB
1228 return (2);
1229 }
1230
5df39c1e 1231 (void) strlcpy(pool, argv[0], sizeof (pool));
428870ff 1232 dataset[0] = '\0';
34dc7c2f
BB
1233 } else if (type == TYPE_INVAL) {
1234 if (flags == 0) {
1235 (void) fprintf(stderr, "at least one of '-b', '-d', "
428870ff
BB
1236 "'-t', '-a', '-p', '-I' or '-u' "
1237 "must be specified\n");
34dc7c2f 1238 usage();
5df39c1e 1239 libzfs_fini(g_zfs);
34dc7c2f
BB
1240 return (2);
1241 }
1242
1243 if (argc == 1 && (flags & ZINJECT_UNLOAD_SPA)) {
5df39c1e 1244 (void) strlcpy(pool, argv[0], sizeof (pool));
34dc7c2f
BB
1245 dataset[0] = '\0';
1246 } else if (argc != 0) {
1247 (void) fprintf(stderr, "extraneous argument for "
1248 "'-f'\n");
1249 usage();
5df39c1e 1250 libzfs_fini(g_zfs);
34dc7c2f
BB
1251 return (2);
1252 }
1253
1254 flags |= ZINJECT_NULL;
1255 } else {
1256 if (argc != 1) {
1257 (void) fprintf(stderr, "missing object\n");
1258 usage();
5df39c1e 1259 libzfs_fini(g_zfs);
34dc7c2f
BB
1260 return (2);
1261 }
1262
d977122d 1263 if (error == ENXIO || error == EILSEQ) {
34dc7c2f
BB
1264 (void) fprintf(stderr, "data error type must be "
1265 "'checksum' or 'io'\n");
5df39c1e 1266 libzfs_fini(g_zfs);
34dc7c2f
BB
1267 return (1);
1268 }
1269
ab7615d9
TC
1270 if (dvas != 0) {
1271 if (error == EACCES || error == EINVAL) {
7dcd3188 1272 (void) fprintf(stderr, "the '-C' option may "
ab7615d9
TC
1273 "not be used with logical data errors "
1274 "'decrypt' and 'decompress'\n");
1275 libzfs_fini(g_zfs);
1276 return (1);
1277 }
1278
1279 record.zi_dvas = dvas;
1280 }
1281
be9a5c35
TC
1282 if (error == EACCES) {
1283 if (type != TYPE_DATA) {
1284 (void) fprintf(stderr, "decryption errors "
1285 "may only be injected for 'data' types\n");
1286 libzfs_fini(g_zfs);
1287 return (1);
1288 }
1289
1290 record.zi_cmd = ZINJECT_DECRYPT_FAULT;
1291 /*
1292 * Internally, ZFS actually uses ECKSUM for decryption
1293 * errors since EACCES is used to indicate the key was
1294 * not found.
1295 */
1296 error = ECKSUM;
1297 } else {
1298 record.zi_cmd = ZINJECT_DATA_FAULT;
1299 }
1300
34dc7c2f 1301 if (translate_record(type, argv[0], range, level, &record, pool,
5df39c1e 1302 dataset) != 0) {
02730c33 1303 libzfs_fini(g_zfs);
34dc7c2f 1304 return (1);
5df39c1e 1305 }
34dc7c2f
BB
1306 if (!error)
1307 error = EIO;
1308 }
1309
1310 /*
1311 * If this is pool-wide metadata, unmount everything. The ioctl() will
1312 * unload the pool, so that we trigger spa-wide reopen of metadata next
1313 * time we access the pool.
1314 */
1315 if (dataset[0] != '\0' && domount) {
5df39c1e 1316 if ((zhp = zfs_open(g_zfs, dataset,
02730c33 1317 ZFS_TYPE_DATASET)) == NULL) {
5df39c1e 1318 libzfs_fini(g_zfs);
34dc7c2f 1319 return (1);
5df39c1e 1320 }
1321 if (zfs_unmount(zhp, NULL, 0) != 0) {
1322 libzfs_fini(g_zfs);
34dc7c2f 1323 return (1);
5df39c1e 1324 }
34dc7c2f
BB
1325 }
1326
1327 record.zi_error = error;
1328
1329 ret = register_handler(pool, flags, &record, quiet);
1330
1331 if (dataset[0] != '\0' && domount)
1332 ret = (zfs_mount(zhp, NULL, 0) != 0);
1333
1334 libzfs_fini(g_zfs);
1335
1336 return (ret);
1337}