]> git.proxmox.com Git - mirror_zfs.git/blame - cmd/zinject/zinject.c
zinject: show more device fault fields
[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"
d977122d 268 "\t\t[-T <read|write|free|claim|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;
981 } else if (strcasecmp(optarg, "all") == 0) {
982 io_type = ZIO_TYPES;
983 } else {
984 (void) fprintf(stderr, "invalid I/O type "
985 "'%s': must be 'read', 'write', 'free', "
986 "'claim' or 'all'\n", optarg);
987 usage();
5df39c1e 988 libzfs_fini(g_zfs);
428870ff
BB
989 return (1);
990 }
991 break;
34dc7c2f 992 case 't':
b128c09f
BB
993 if ((type = name_to_type(optarg)) == TYPE_INVAL &&
994 !MOS_TYPE(type)) {
34dc7c2f
BB
995 (void) fprintf(stderr, "invalid type '%s'\n",
996 optarg);
997 usage();
5df39c1e 998 libzfs_fini(g_zfs);
34dc7c2f
BB
999 return (1);
1000 }
1001 break;
1002 case 'u':
1003 flags |= ZINJECT_UNLOAD_SPA;
1004 break;
b128c09f
BB
1005 case 'L':
1006 if ((label = name_to_type(optarg)) == TYPE_INVAL &&
1007 !LABEL_TYPE(type)) {
1008 (void) fprintf(stderr, "invalid label type "
1009 "'%s'\n", optarg);
1010 usage();
5df39c1e 1011 libzfs_fini(g_zfs);
b128c09f
BB
1012 return (1);
1013 }
1014 break;
34dc7c2f
BB
1015 case ':':
1016 (void) fprintf(stderr, "option -%c requires an "
1017 "operand\n", optopt);
1018 usage();
5df39c1e 1019 libzfs_fini(g_zfs);
34dc7c2f
BB
1020 return (1);
1021 case '?':
1022 (void) fprintf(stderr, "invalid option '%c'\n",
1023 optopt);
1024 usage();
5df39c1e 1025 libzfs_fini(g_zfs);
34dc7c2f
BB
1026 return (2);
1027 }
1028 }
1029
1030 argc -= optind;
1031 argv += optind;
1032
cc92e9d0
GW
1033 if (record.zi_duration != 0)
1034 record.zi_cmd = ZINJECT_IGNORED_WRITES;
1035
34dc7c2f
BB
1036 if (cancel != NULL) {
1037 /*
1038 * '-c' is invalid with any other options.
1039 */
1040 if (raw != NULL || range != NULL || type != TYPE_INVAL ||
0241e491 1041 level != 0 || record.zi_cmd != ZINJECT_UNINITIALIZED ||
ab7615d9 1042 record.zi_freq > 0 || dvas != 0) {
34dc7c2f
BB
1043 (void) fprintf(stderr, "cancel (-c) incompatible with "
1044 "any other options\n");
1045 usage();
5df39c1e 1046 libzfs_fini(g_zfs);
34dc7c2f
BB
1047 return (2);
1048 }
1049 if (argc != 0) {
1050 (void) fprintf(stderr, "extraneous argument to '-c'\n");
1051 usage();
5df39c1e 1052 libzfs_fini(g_zfs);
34dc7c2f
BB
1053 return (2);
1054 }
1055
1056 if (strcmp(cancel, "all") == 0) {
1057 return (cancel_all_handlers());
1058 } else {
1059 int id = (int)strtol(cancel, &end, 10);
1060 if (*end != '\0') {
1061 (void) fprintf(stderr, "invalid handle id '%s':"
1062 " must be an integer or 'all'\n", cancel);
1063 usage();
5df39c1e 1064 libzfs_fini(g_zfs);
34dc7c2f
BB
1065 return (1);
1066 }
1067 return (cancel_handler(id));
1068 }
1069 }
1070
1071 if (device != NULL) {
1072 /*
1073 * Device (-d) injection uses a completely different mechanism
1074 * for doing injection, so handle it separately here.
1075 */
1076 if (raw != NULL || range != NULL || type != TYPE_INVAL ||
ab7615d9
TC
1077 level != 0 || record.zi_cmd != ZINJECT_UNINITIALIZED ||
1078 dvas != 0) {
34dc7c2f
BB
1079 (void) fprintf(stderr, "device (-d) incompatible with "
1080 "data error injection\n");
1081 usage();
5df39c1e 1082 libzfs_fini(g_zfs);
34dc7c2f
BB
1083 return (2);
1084 }
1085
1086 if (argc != 1) {
1087 (void) fprintf(stderr, "device (-d) injection requires "
1088 "a single pool name\n");
1089 usage();
5df39c1e 1090 libzfs_fini(g_zfs);
34dc7c2f
BB
1091 return (2);
1092 }
1093
5df39c1e 1094 (void) strlcpy(pool, argv[0], sizeof (pool));
34dc7c2f
BB
1095 dataset[0] = '\0';
1096
1097 if (error == ECKSUM) {
1098 (void) fprintf(stderr, "device error type must be "
d977122d
DB
1099 "'io', 'nxio' or 'corrupt'\n");
1100 libzfs_fini(g_zfs);
1101 return (1);
1102 }
1103
1104 if (error == EILSEQ &&
1105 (record.zi_freq == 0 || io_type != ZIO_TYPE_READ)) {
1106 (void) fprintf(stderr, "device corrupt errors require "
1107 "io type read and a frequency value\n");
5df39c1e 1108 libzfs_fini(g_zfs);
34dc7c2f
BB
1109 return (1);
1110 }
1111
428870ff 1112 record.zi_iotype = io_type;
5df39c1e 1113 if (translate_device(pool, device, label, &record) != 0) {
1114 libzfs_fini(g_zfs);
34dc7c2f 1115 return (1);
5df39c1e 1116 }
cbe88229
DB
1117
1118 if (record.zi_nlanes) {
1119 switch (io_type) {
1120 case ZIO_TYPE_READ:
1121 case ZIO_TYPE_WRITE:
1122 case ZIO_TYPES:
1123 break;
1124 default:
1125 (void) fprintf(stderr, "I/O type for a delay "
1126 "must be 'read' or 'write'\n");
1127 usage();
1128 libzfs_fini(g_zfs);
1129 return (1);
1130 }
1131 }
1132
34dc7c2f
BB
1133 if (!error)
1134 error = ENXIO;
428870ff
BB
1135
1136 if (action != VDEV_STATE_UNKNOWN)
1137 return (perform_action(pool, &record, action));
1138
34dc7c2f 1139 } else if (raw != NULL) {
428870ff 1140 if (range != NULL || type != TYPE_INVAL || level != 0 ||
0241e491 1141 record.zi_cmd != ZINJECT_UNINITIALIZED ||
ab7615d9 1142 record.zi_freq > 0 || dvas != 0) {
34dc7c2f
BB
1143 (void) fprintf(stderr, "raw (-b) format with "
1144 "any other options\n");
1145 usage();
5df39c1e 1146 libzfs_fini(g_zfs);
34dc7c2f
BB
1147 return (2);
1148 }
1149
1150 if (argc != 1) {
1151 (void) fprintf(stderr, "raw (-b) format expects a "
1152 "single pool name\n");
1153 usage();
5df39c1e 1154 libzfs_fini(g_zfs);
34dc7c2f
BB
1155 return (2);
1156 }
1157
5df39c1e 1158 (void) strlcpy(pool, argv[0], sizeof (pool));
34dc7c2f
BB
1159 dataset[0] = '\0';
1160
1161 if (error == ENXIO) {
1162 (void) fprintf(stderr, "data error type must be "
1163 "'checksum' or 'io'\n");
5df39c1e 1164 libzfs_fini(g_zfs);
34dc7c2f
BB
1165 return (1);
1166 }
1167
cc92e9d0 1168 record.zi_cmd = ZINJECT_DATA_FAULT;
5df39c1e 1169 if (translate_raw(raw, &record) != 0) {
1170 libzfs_fini(g_zfs);
34dc7c2f 1171 return (1);
5df39c1e 1172 }
34dc7c2f
BB
1173 if (!error)
1174 error = EIO;
cc92e9d0 1175 } else if (record.zi_cmd == ZINJECT_PANIC) {
428870ff 1176 if (raw != NULL || range != NULL || type != TYPE_INVAL ||
ab7615d9
TC
1177 level != 0 || device != NULL || record.zi_freq > 0 ||
1178 dvas != 0) {
428870ff
BB
1179 (void) fprintf(stderr, "panic (-p) incompatible with "
1180 "other options\n");
1181 usage();
5df39c1e 1182 libzfs_fini(g_zfs);
428870ff
BB
1183 return (2);
1184 }
1185
1186 if (argc < 1 || argc > 2) {
1187 (void) fprintf(stderr, "panic (-p) injection requires "
1188 "a single pool name and an optional id\n");
1189 usage();
5df39c1e 1190 libzfs_fini(g_zfs);
428870ff
BB
1191 return (2);
1192 }
1193
5df39c1e 1194 (void) strlcpy(pool, argv[0], sizeof (pool));
428870ff
BB
1195 if (argv[1] != NULL)
1196 record.zi_type = atoi(argv[1]);
1197 dataset[0] = '\0';
cc92e9d0 1198 } else if (record.zi_cmd == ZINJECT_IGNORED_WRITES) {
ab7615d9
TC
1199 if (raw != NULL || range != NULL || type != TYPE_INVAL ||
1200 level != 0 || record.zi_freq > 0 || dvas != 0) {
1201 (void) fprintf(stderr, "hardware failure (-I) "
1202 "incompatible with other options\n");
1203 usage();
1204 libzfs_fini(g_zfs);
1205 return (2);
1206 }
1207
428870ff
BB
1208 if (nowrites == 0) {
1209 (void) fprintf(stderr, "-s or -g meaningless "
1210 "without -I (ignore writes)\n");
1211 usage();
5df39c1e 1212 libzfs_fini(g_zfs);
428870ff
BB
1213 return (2);
1214 } else if (dur_secs && dur_txg) {
1215 (void) fprintf(stderr, "choose a duration either "
1216 "in seconds (-s) or a number of txgs (-g) "
1217 "but not both\n");
1218 usage();
5df39c1e 1219 libzfs_fini(g_zfs);
428870ff
BB
1220 return (2);
1221 } else if (argc != 1) {
1222 (void) fprintf(stderr, "ignore writes (-I) "
1223 "injection requires a single pool name\n");
1224 usage();
5df39c1e 1225 libzfs_fini(g_zfs);
428870ff
BB
1226 return (2);
1227 }
1228
5df39c1e 1229 (void) strlcpy(pool, argv[0], sizeof (pool));
428870ff 1230 dataset[0] = '\0';
34dc7c2f
BB
1231 } else if (type == TYPE_INVAL) {
1232 if (flags == 0) {
1233 (void) fprintf(stderr, "at least one of '-b', '-d', "
428870ff
BB
1234 "'-t', '-a', '-p', '-I' or '-u' "
1235 "must be specified\n");
34dc7c2f 1236 usage();
5df39c1e 1237 libzfs_fini(g_zfs);
34dc7c2f
BB
1238 return (2);
1239 }
1240
1241 if (argc == 1 && (flags & ZINJECT_UNLOAD_SPA)) {
5df39c1e 1242 (void) strlcpy(pool, argv[0], sizeof (pool));
34dc7c2f
BB
1243 dataset[0] = '\0';
1244 } else if (argc != 0) {
1245 (void) fprintf(stderr, "extraneous argument for "
1246 "'-f'\n");
1247 usage();
5df39c1e 1248 libzfs_fini(g_zfs);
34dc7c2f
BB
1249 return (2);
1250 }
1251
1252 flags |= ZINJECT_NULL;
1253 } else {
1254 if (argc != 1) {
1255 (void) fprintf(stderr, "missing object\n");
1256 usage();
5df39c1e 1257 libzfs_fini(g_zfs);
34dc7c2f
BB
1258 return (2);
1259 }
1260
d977122d 1261 if (error == ENXIO || error == EILSEQ) {
34dc7c2f
BB
1262 (void) fprintf(stderr, "data error type must be "
1263 "'checksum' or 'io'\n");
5df39c1e 1264 libzfs_fini(g_zfs);
34dc7c2f
BB
1265 return (1);
1266 }
1267
ab7615d9
TC
1268 if (dvas != 0) {
1269 if (error == EACCES || error == EINVAL) {
7dcd3188 1270 (void) fprintf(stderr, "the '-C' option may "
ab7615d9
TC
1271 "not be used with logical data errors "
1272 "'decrypt' and 'decompress'\n");
1273 libzfs_fini(g_zfs);
1274 return (1);
1275 }
1276
1277 record.zi_dvas = dvas;
1278 }
1279
be9a5c35
TC
1280 if (error == EACCES) {
1281 if (type != TYPE_DATA) {
1282 (void) fprintf(stderr, "decryption errors "
1283 "may only be injected for 'data' types\n");
1284 libzfs_fini(g_zfs);
1285 return (1);
1286 }
1287
1288 record.zi_cmd = ZINJECT_DECRYPT_FAULT;
1289 /*
1290 * Internally, ZFS actually uses ECKSUM for decryption
1291 * errors since EACCES is used to indicate the key was
1292 * not found.
1293 */
1294 error = ECKSUM;
1295 } else {
1296 record.zi_cmd = ZINJECT_DATA_FAULT;
1297 }
1298
34dc7c2f 1299 if (translate_record(type, argv[0], range, level, &record, pool,
5df39c1e 1300 dataset) != 0) {
02730c33 1301 libzfs_fini(g_zfs);
34dc7c2f 1302 return (1);
5df39c1e 1303 }
34dc7c2f
BB
1304 if (!error)
1305 error = EIO;
1306 }
1307
1308 /*
1309 * If this is pool-wide metadata, unmount everything. The ioctl() will
1310 * unload the pool, so that we trigger spa-wide reopen of metadata next
1311 * time we access the pool.
1312 */
1313 if (dataset[0] != '\0' && domount) {
5df39c1e 1314 if ((zhp = zfs_open(g_zfs, dataset,
02730c33 1315 ZFS_TYPE_DATASET)) == NULL) {
5df39c1e 1316 libzfs_fini(g_zfs);
34dc7c2f 1317 return (1);
5df39c1e 1318 }
1319 if (zfs_unmount(zhp, NULL, 0) != 0) {
1320 libzfs_fini(g_zfs);
34dc7c2f 1321 return (1);
5df39c1e 1322 }
34dc7c2f
BB
1323 }
1324
1325 record.zi_error = error;
1326
1327 ret = register_handler(pool, flags, &record, quiet);
1328
1329 if (dataset[0] != '\0' && domount)
1330 ret = (zfs_mount(zhp, NULL, 0) != 0);
1331
1332 libzfs_fini(g_zfs);
1333
1334 return (ret);
1335}