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.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24 * Copyright (c) 2017, Intel Corporation.
30 * This userland component takes a set of options and uses libzpool to translate
31 * from a user-visible object type and name to an internal representation.
32 * There are two basic types of faults: device faults and data faults.
37 * Errors can be injected into a particular vdev using the '-d' option. This
38 * option takes a path or vdev GUID to uniquely identify the device within a
39 * pool. There are four types of errors that can be injected, IO, ENXIO,
40 * ECHILD, and EILSEQ. These can be controlled through the '-e' option and the
41 * default is ENXIO. For EIO failures, any attempt to read data from the device
42 * will return EIO, but a subsequent attempt to reopen the device will succeed.
43 * For ENXIO failures, any attempt to read from the device will return EIO, but
44 * any attempt to reopen the device will also return ENXIO. The EILSEQ failures
45 * only apply to read operations (-T read) and will flip a bit after the device
46 * has read the original data.
48 * For label faults, the -L option must be specified. This allows faults
49 * to be injected into either the nvlist, uberblock, pad1, or pad2 region
50 * of all the labels for the specified device.
52 * This form of the command looks like:
54 * zinject -d device [-e errno] [-L <uber | nvlist | pad1 | pad2>] pool
59 * We begin with a tuple of the form:
61 * <type,level,range,object>
63 * type A string describing the type of data to target. Each type
64 * implicitly describes how to interpret 'object'. Currently,
65 * the following values are supported:
67 * data User data for a file
68 * dnode Dnode for a file or directory
70 * The following MOS objects are special. Instead of injecting
71 * errors on a particular object or blkid, we inject errors across
72 * all objects of the given type.
74 * mos Any data in the MOS
75 * mosdir object directory
76 * config pool configuration
80 * errlog persistent error log
82 * level Object level. Defaults to '0', not applicable to all types. If
83 * a range is given, this corresponds to the indirect block
84 * corresponding to the specific range.
86 * range A numerical range [start,end) within the object. Defaults to
87 * the full size of the file.
89 * object A string describing the logical location of the object. For
90 * files and directories (currently the only supported types),
91 * this is the path of the object on disk.
93 * This is translated, via libzpool, into the following internal representation:
95 * <type,objset,object,level,range>
97 * These types should be self-explanatory. This tuple is then passed to the
98 * kernel via a special ioctl() to initiate fault injection for the given
99 * object. Note that 'type' is not strictly necessary for fault injection, but
100 * is used when translating existing faults into a human-readable string.
103 * The command itself takes one of the forms:
106 * zinject <-a | -u pool>
107 * zinject -c <id|all>
108 * zinject [-q] <-t type> [-f freq] [-u] [-a] [-m] [-e errno] [-l level]
109 * [-r range] <object>
110 * zinject [-f freq] [-a] [-m] [-u] -b objset:object:level:start:end pool
112 * With no arguments, the command prints all currently registered injection
113 * handlers, with their numeric identifiers.
115 * The '-c' option will clear the given handler, or all handlers if 'all' is
118 * The '-e' option takes a string describing the errno to simulate. This must
119 * be one of 'io', 'checksum', 'decompress', or 'decrypt'. In most cases this
120 * will result in the same behavior, but RAID-Z will produce a different set of
121 * ereports for this situation.
123 * The '-a', '-u', and '-m' flags toggle internal flush behavior. If '-a' is
124 * specified, then the ARC cache is flushed appropriately. If '-u' is
125 * specified, then the underlying SPA is unloaded. Either of these flags can be
126 * specified independently of any other handlers. The '-m' flag automatically
127 * does an unmount and remount of the underlying dataset to aid in flushing the
130 * The '-f' flag controls the frequency of errors injected, expressed as a
131 * real number percentage between 0.0001 and 100. The default is 100.
133 * The this form is responsible for actually injecting the handler into the
134 * framework. It takes the arguments described above, translates them to the
135 * internal tuple using libzpool, and then issues an ioctl() to register the
138 * The final form can target a specific bookmark, regardless of whether a
139 * human-readable interface has been designed. It allows developers to specify
140 * a particular block by number.
151 #include <sys/fs/zfs.h>
152 #include <sys/mount.h>
156 #undef verify /* both libzfs.h and zfs_context.h want to define this */
160 libzfs_handle_t
*g_zfs
;
163 static const char *const errtable
[TYPE_INVAL
] = {
180 name_to_type(const char *arg
)
183 for (i
= 0; i
< TYPE_INVAL
; i
++)
184 if (strcmp(errtable
[i
], arg
) == 0)
191 type_to_name(uint64_t type
)
194 case DMU_OT_OBJECT_DIRECTORY
:
196 case DMU_OT_OBJECT_ARRAY
:
198 case DMU_OT_PACKED_NVLIST
:
202 case DMU_OT_SPACE_MAP
:
204 case DMU_OT_ERROR_LOG
:
213 * Print usage message.
223 "\t\tList all active injection records.\n"
225 "\tzinject -c <id|all>\n"
227 "\t\tClear the particular record (if given a numeric ID), or\n"
228 "\t\tall records if 'all' is specified.\n"
230 "\tzinject -p <function name> pool\n"
231 "\t\tInject a panic fault at the specified function. Only \n"
232 "\t\tfunctions which call spa_vdev_config_exit(), or \n"
233 "\t\tspa_vdev_exit() will trigger a panic.\n"
235 "\tzinject -d device [-e errno] [-L <nvlist|uber|pad1|pad2>] [-F]\n"
236 "\t\t[-T <read|write|free|claim|all>] [-f frequency] pool\n\n"
237 "\t\tInject a fault into a particular device or the device's\n"
238 "\t\tlabel. Label injection can either be 'nvlist', 'uber',\n "
239 "\t\t'pad1', or 'pad2'.\n"
240 "\t\t'errno' can be 'nxio' (the default), 'io', 'dtl', or\n"
241 "\t\t'corrupt' (bit flip).\n"
242 "\t\t'frequency' is a value between 0.0001 and 100.0 that limits\n"
243 "\t\tdevice error injection to a percentage of the IOs.\n"
245 "\tzinject -d device -A <degrade|fault> -D <delay secs> pool\n"
246 "\t\tPerform a specific action on a particular device.\n"
248 "\tzinject -d device -D latency:lanes pool\n"
250 "\t\tAdd an artificial delay to IO requests on a particular\n"
251 "\t\tdevice, such that the requests take a minimum of 'latency'\n"
252 "\t\tmilliseconds to complete. Each delay has an associated\n"
253 "\t\tnumber of 'lanes' which defines the number of concurrent\n"
254 "\t\tIO requests that can be processed.\n"
256 "\t\tFor example, with a single lane delay of 10 ms (-D 10:1),\n"
257 "\t\tthe device will only be able to service a single IO request\n"
258 "\t\tat a time with each request taking 10 ms to complete. So,\n"
259 "\t\tif only a single request is submitted every 10 ms, the\n"
260 "\t\taverage latency will be 10 ms; but if more than one request\n"
261 "\t\tis submitted every 10 ms, the average latency will be more\n"
264 "\t\tSimilarly, if a delay of 10 ms is specified to have two\n"
265 "\t\tlanes (-D 10:2), then the device will be able to service\n"
266 "\t\ttwo requests at a time, each with a minimum latency of\n"
267 "\t\t10 ms. So, if two requests are submitted every 10 ms, then\n"
268 "\t\tthe average latency will be 10 ms; but if more than two\n"
269 "\t\trequests are submitted every 10 ms, the average latency\n"
270 "\t\twill be more than 10 ms.\n"
272 "\t\tAlso note, these delays are additive. So two invocations\n"
273 "\t\tof '-D 10:1', is roughly equivalent to a single invocation\n"
274 "\t\tof '-D 10:2'. This also means, one can specify multiple\n"
275 "\t\tlanes with differing target latencies. For example, an\n"
276 "\t\tinvocation of '-D 10:1' followed by '-D 25:2' will\n"
277 "\t\tcreate 3 lanes on the device; one lane with a latency\n"
278 "\t\tof 10 ms and two lanes with a 25 ms latency.\n"
280 "\tzinject -I [-s <seconds> | -g <txgs>] pool\n"
281 "\t\tCause the pool to stop writing blocks yet not\n"
282 "\t\treport errors for a duration. Simulates buggy hardware\n"
283 "\t\tthat fails to honor cache flush requests.\n"
284 "\t\tDefault duration is 30 seconds. The machine is panicked\n"
285 "\t\tat the end of the duration.\n"
287 "\tzinject -b objset:object:level:blkid pool\n"
289 "\t\tInject an error into pool 'pool' with the numeric bookmark\n"
290 "\t\tspecified by the remaining tuple. Each number is in\n"
291 "\t\thexadecimal, and only one block can be specified.\n"
293 "\tzinject [-q] <-t type> [-C dvas] [-e errno] [-l level]\n"
294 "\t\t[-r range] [-a] [-m] [-u] [-f freq] <object>\n"
296 "\t\tInject an error into the object specified by the '-t' option\n"
297 "\t\tand the object descriptor. The 'object' parameter is\n"
298 "\t\tinterpreted depending on the '-t' option.\n"
300 "\t\t-q\tQuiet mode. Only print out the handler number added.\n"
301 "\t\t-e\tInject a specific error. Must be one of 'io',\n"
302 "\t\t\t'checksum', 'decompress', or 'decrypt'. Default is 'io'.\n"
303 "\t\t-C\tInject the given error only into specific DVAs. The\n"
304 "\t\t\tDVAs should be specified as a list of 0-indexed DVAs\n"
305 "\t\t\tseparated by commas (ex. '0,2').\n"
306 "\t\t-l\tInject error at a particular block level. Default is "
308 "\t\t-m\tAutomatically remount underlying filesystem.\n"
309 "\t\t-r\tInject error over a particular logical range of an\n"
310 "\t\t\tobject. Will be translated to the appropriate blkid\n"
311 "\t\t\trange according to the object's properties.\n"
312 "\t\t-a\tFlush the ARC cache. Can be specified without any\n"
313 "\t\t\tassociated object.\n"
314 "\t\t-u\tUnload the associated pool. Can be specified with only\n"
315 "\t\t\ta pool object.\n"
316 "\t\t-f\tOnly inject errors a fraction of the time. Expressed as\n"
317 "\t\t\ta percentage between 0.0001 and 100.\n"
319 "\t-t data\t\tInject an error into the plain file contents of a\n"
320 "\t\t\tfile. The object must be specified as a complete path\n"
321 "\t\t\tto a file on a ZFS filesystem.\n"
323 "\t-t dnode\tInject an error into the metadnode in the block\n"
324 "\t\t\tcorresponding to the dnode for a file or directory. The\n"
325 "\t\t\t'-r' option is incompatible with this mode. The object\n"
326 "\t\t\tis specified as a complete path to a file or directory\n"
327 "\t\t\ton a ZFS filesystem.\n"
329 "\t-t <mos>\tInject errors into the MOS for objects of the given\n"
330 "\t\t\ttype. Valid types are: mos, mosdir, config, bpobj,\n"
331 "\t\t\tspacemap, metaslab, errlog. The only valid <object> is\n"
332 "\t\t\tthe poolname.\n");
336 iter_handlers(int (*func
)(int, const char *, zinject_record_t
*, void *),
339 zfs_cmd_t zc
= {"\0"};
342 while (zfs_ioctl(g_zfs
, ZFS_IOC_INJECT_LIST_NEXT
, &zc
) == 0)
343 if ((ret
= func((int)zc
.zc_guid
, zc
.zc_name
,
344 &zc
.zc_inject_record
, data
)) != 0)
347 if (errno
!= ENOENT
) {
348 (void) fprintf(stderr
, "Unable to list handlers: %s\n",
357 print_data_handler(int id
, const char *pool
, zinject_record_t
*record
,
362 if (record
->zi_guid
!= 0 || record
->zi_func
[0] != '\0')
366 (void) printf("%3s %-15s %-6s %-6s %-8s %3s %-4s "
367 "%-15s\n", "ID", "POOL", "OBJSET", "OBJECT", "TYPE",
368 "LVL", "DVAs", "RANGE");
369 (void) printf("--- --------------- ------ "
370 "------ -------- --- ---- ---------------\n");
375 (void) printf("%3d %-15s %-6llu %-6llu %-8s %-3d 0x%02x ",
376 id
, pool
, (u_longlong_t
)record
->zi_objset
,
377 (u_longlong_t
)record
->zi_object
, type_to_name(record
->zi_type
),
378 record
->zi_level
, record
->zi_dvas
);
381 if (record
->zi_start
== 0 &&
382 record
->zi_end
== -1ULL)
383 (void) printf("all\n");
385 (void) printf("[%llu, %llu]\n", (u_longlong_t
)record
->zi_start
,
386 (u_longlong_t
)record
->zi_end
);
392 print_device_handler(int id
, const char *pool
, zinject_record_t
*record
,
397 if (record
->zi_guid
== 0 || record
->zi_func
[0] != '\0')
400 if (record
->zi_cmd
== ZINJECT_DELAY_IO
)
404 (void) printf("%3s %-15s %s\n", "ID", "POOL", "GUID");
405 (void) printf("--- --------------- ----------------\n");
410 (void) printf("%3d %-15s %llx\n", id
, pool
,
411 (u_longlong_t
)record
->zi_guid
);
417 print_delay_handler(int id
, const char *pool
, zinject_record_t
*record
,
422 if (record
->zi_guid
== 0 || record
->zi_func
[0] != '\0')
425 if (record
->zi_cmd
!= ZINJECT_DELAY_IO
)
429 (void) printf("%3s %-15s %-15s %-15s %s\n",
430 "ID", "POOL", "DELAY (ms)", "LANES", "GUID");
431 (void) printf("--- --------------- --------------- "
432 "--------------- ----------------\n");
437 (void) printf("%3d %-15s %-15llu %-15llu %llx\n", id
, pool
,
438 (u_longlong_t
)NSEC2MSEC(record
->zi_timer
),
439 (u_longlong_t
)record
->zi_nlanes
,
440 (u_longlong_t
)record
->zi_guid
);
446 print_panic_handler(int id
, const char *pool
, zinject_record_t
*record
,
451 if (record
->zi_func
[0] == '\0')
455 (void) printf("%3s %-15s %s\n", "ID", "POOL", "FUNCTION");
456 (void) printf("--- --------------- ----------------\n");
461 (void) printf("%3d %-15s %s\n", id
, pool
, record
->zi_func
);
467 * Print all registered error handlers. Returns the number of handlers
471 print_all_handlers(void)
473 int count
= 0, total
= 0;
475 (void) iter_handlers(print_device_handler
, &count
);
482 (void) iter_handlers(print_delay_handler
, &count
);
489 (void) iter_handlers(print_data_handler
, &count
);
496 (void) iter_handlers(print_panic_handler
, &count
);
498 return (count
+ total
);
502 cancel_one_handler(int id
, const char *pool
, zinject_record_t
*record
,
505 (void) pool
, (void) record
, (void) data
;
506 zfs_cmd_t zc
= {"\0"};
508 zc
.zc_guid
= (uint64_t)id
;
510 if (zfs_ioctl(g_zfs
, ZFS_IOC_CLEAR_FAULT
, &zc
) != 0) {
511 (void) fprintf(stderr
, "failed to remove handler %d: %s\n",
512 id
, strerror(errno
));
520 * Remove all fault injection handlers.
523 cancel_all_handlers(void)
525 int ret
= iter_handlers(cancel_one_handler
, NULL
);
528 (void) printf("removed all registered handlers\n");
534 * Remove a specific fault injection handler.
537 cancel_handler(int id
)
539 zfs_cmd_t zc
= {"\0"};
541 zc
.zc_guid
= (uint64_t)id
;
543 if (zfs_ioctl(g_zfs
, ZFS_IOC_CLEAR_FAULT
, &zc
) != 0) {
544 (void) fprintf(stderr
, "failed to remove handler %d: %s\n",
545 id
, strerror(errno
));
549 (void) printf("removed handler %d\n", id
);
555 * Register a new fault injection handler.
558 register_handler(const char *pool
, int flags
, zinject_record_t
*record
,
561 zfs_cmd_t zc
= {"\0"};
563 (void) strlcpy(zc
.zc_name
, pool
, sizeof (zc
.zc_name
));
564 zc
.zc_inject_record
= *record
;
567 if (zfs_ioctl(g_zfs
, ZFS_IOC_INJECT_FAULT
, &zc
) != 0) {
568 (void) fprintf(stderr
, "failed to add handler: %s\n",
569 errno
== EDOM
? "block level exceeds max level of object" :
574 if (flags
& ZINJECT_NULL
)
578 (void) printf("%llu\n", (u_longlong_t
)zc
.zc_guid
);
580 (void) printf("Added handler %llu with the following "
581 "properties:\n", (u_longlong_t
)zc
.zc_guid
);
582 (void) printf(" pool: %s\n", pool
);
583 if (record
->zi_guid
) {
584 (void) printf(" vdev: %llx\n",
585 (u_longlong_t
)record
->zi_guid
);
586 } else if (record
->zi_func
[0] != '\0') {
587 (void) printf(" panic function: %s\n",
589 } else if (record
->zi_duration
> 0) {
590 (void) printf(" time: %lld seconds\n",
591 (u_longlong_t
)record
->zi_duration
);
592 } else if (record
->zi_duration
< 0) {
593 (void) printf(" txgs: %lld \n",
594 (u_longlong_t
)-record
->zi_duration
);
596 (void) printf("objset: %llu\n",
597 (u_longlong_t
)record
->zi_objset
);
598 (void) printf("object: %llu\n",
599 (u_longlong_t
)record
->zi_object
);
600 (void) printf(" type: %llu\n",
601 (u_longlong_t
)record
->zi_type
);
602 (void) printf(" level: %d\n", record
->zi_level
);
603 if (record
->zi_start
== 0 &&
604 record
->zi_end
== -1ULL)
605 (void) printf(" range: all\n");
607 (void) printf(" range: [%llu, %llu)\n",
608 (u_longlong_t
)record
->zi_start
,
609 (u_longlong_t
)record
->zi_end
);
610 (void) printf(" dvas: 0x%x\n", record
->zi_dvas
);
618 perform_action(const char *pool
, zinject_record_t
*record
, int cmd
)
620 zfs_cmd_t zc
= {"\0"};
622 ASSERT(cmd
== VDEV_STATE_DEGRADED
|| cmd
== VDEV_STATE_FAULTED
);
623 (void) strlcpy(zc
.zc_name
, pool
, sizeof (zc
.zc_name
));
624 zc
.zc_guid
= record
->zi_guid
;
627 if (zfs_ioctl(g_zfs
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
634 parse_delay(char *str
, uint64_t *delay
, uint64_t *nlanes
)
636 unsigned long scan_delay
;
637 unsigned long scan_nlanes
;
639 if (sscanf(str
, "%lu:%lu", &scan_delay
, &scan_nlanes
) != 2)
643 * We explicitly disallow a delay of zero here, because we key
644 * off this value being non-zero in translate_device(), to
645 * determine if the fault is a ZINJECT_DELAY_IO fault or not.
651 * The units for the CLI delay parameter is milliseconds, but
652 * the data passed to the kernel is interpreted as nanoseconds.
653 * Thus we scale the milliseconds to nanoseconds here, and this
654 * nanosecond value is used to pass the delay to the kernel.
656 *delay
= MSEC2NSEC(scan_delay
);
657 *nlanes
= scan_nlanes
;
663 parse_frequency(const char *str
, uint32_t *percent
)
668 val
= strtod(str
, &post
);
669 if (post
== NULL
|| *post
!= '\0')
672 /* valid range is [0.0001, 100.0] */
674 if (val
< 0.000001f
|| val
> 1.0f
)
677 /* convert to an integer for use by kernel */
678 *percent
= ((uint32_t)(val
* ZI_PERCENTAGE_MAX
));
684 * This function converts a string specifier for DVAs into a bit mask.
685 * The dva's provided by the user should be 0 indexed and separated by
686 * a comma. For example:
687 * "1" -> 0b0010 (0x2)
688 * "0,1" -> 0b0011 (0x3)
689 * "0,1,2" -> 0b0111 (0x7)
692 parse_dvas(const char *str
, uint32_t *dvas_out
)
696 boolean_t need_delim
= B_FALSE
;
698 /* max string length is 5 ("0,1,2") */
699 if (strlen(str
) > 5 || strlen(str
) == 0)
707 /* check for pipe between DVAs */
711 /* check if this DVA has been set already */
712 if (mask
& (1 << ((*c
) - '0')))
715 mask
|= (1 << ((*c
) - '0'));
719 need_delim
= B_FALSE
;
722 /* check for invalid character */
728 /* check for dangling delimiter */
737 main(int argc
, char **argv
)
749 int io_type
= ZIO_TYPES
;
750 int action
= VDEV_STATE_UNKNOWN
;
751 err_type_t type
= TYPE_INVAL
;
752 err_type_t label
= TYPE_INVAL
;
753 zinject_record_t record
= { 0 };
754 char pool
[MAXNAMELEN
] = "";
755 char dataset
[MAXNAMELEN
] = "";
756 zfs_handle_t
*zhp
= NULL
;
764 if ((g_zfs
= libzfs_init()) == NULL
) {
765 (void) fprintf(stderr
, "%s\n", libzfs_error_init(errno
));
769 libzfs_print_on_error(g_zfs
, B_TRUE
);
771 if ((zfs_fd
= open(ZFS_DEV
, O_RDWR
)) < 0) {
772 (void) fprintf(stderr
, "failed to open ZFS device\n");
779 * No arguments. Print the available handlers. If there are no
780 * available handlers, direct the user to '-h' for help
783 if (print_all_handlers() == 0) {
784 (void) printf("No handlers registered.\n");
785 (void) printf("Run 'zinject -h' for usage "
792 while ((c
= getopt(argc
, argv
,
793 ":aA:b:C:d:D:f:Fg:qhIc:t:T:l:mr:s:e:uL:p:")) != -1) {
796 flags
|= ZINJECT_FLUSH_ARC
;
799 if (strcasecmp(optarg
, "degrade") == 0) {
800 action
= VDEV_STATE_DEGRADED
;
801 } else if (strcasecmp(optarg
, "fault") == 0) {
802 action
= VDEV_STATE_FAULTED
;
804 (void) fprintf(stderr
, "invalid action '%s': "
805 "must be 'degrade' or 'fault'\n", optarg
);
818 ret
= parse_dvas(optarg
, &dvas
);
820 (void) fprintf(stderr
, "invalid DVA list '%s': "
821 "DVAs should be 0 indexed and separated by "
822 "commas.\n", optarg
);
833 ret
= parse_delay(optarg
, &record
.zi_timer
,
837 (void) fprintf(stderr
, "invalid i/o delay "
838 "value: '%s'\n", optarg
);
845 if (strcasecmp(optarg
, "io") == 0) {
847 } else if (strcasecmp(optarg
, "checksum") == 0) {
849 } else if (strcasecmp(optarg
, "decompress") == 0) {
851 } else if (strcasecmp(optarg
, "decrypt") == 0) {
853 } else if (strcasecmp(optarg
, "nxio") == 0) {
855 } else if (strcasecmp(optarg
, "dtl") == 0) {
857 } else if (strcasecmp(optarg
, "corrupt") == 0) {
860 (void) fprintf(stderr
, "invalid error type "
861 "'%s': must be 'io', 'checksum' or "
869 ret
= parse_frequency(optarg
, &record
.zi_freq
);
871 (void) fprintf(stderr
, "%sfrequency value must "
872 "be in the range [0.0001, 100.0]\n",
873 ret
== EINVAL
? "invalid value: " :
874 ret
== ERANGE
? "out of range: " : "");
880 record
.zi_failfast
= B_TRUE
;
884 record
.zi_duration
= (int)strtol(optarg
, &end
, 10);
885 if (record
.zi_duration
<= 0 || *end
!= '\0') {
886 (void) fprintf(stderr
, "invalid duration '%s': "
887 "must be a positive integer\n", optarg
);
892 /* store duration of txgs as its negative */
893 record
.zi_duration
*= -1;
900 /* default duration, if one hasn't yet been defined */
902 if (dur_secs
== 0 && dur_txg
== 0)
903 record
.zi_duration
= 30;
906 level
= (int)strtol(optarg
, &end
, 10);
908 (void) fprintf(stderr
, "invalid level '%s': "
909 "must be an integer\n", optarg
);
919 (void) strlcpy(record
.zi_func
, optarg
,
920 sizeof (record
.zi_func
));
921 record
.zi_cmd
= ZINJECT_PANIC
;
928 flags
|= ZINJECT_CALC_RANGE
;
932 record
.zi_duration
= (int)strtol(optarg
, &end
, 10);
933 if (record
.zi_duration
<= 0 || *end
!= '\0') {
934 (void) fprintf(stderr
, "invalid duration '%s': "
935 "must be a positive integer\n", optarg
);
942 if (strcasecmp(optarg
, "read") == 0) {
943 io_type
= ZIO_TYPE_READ
;
944 } else if (strcasecmp(optarg
, "write") == 0) {
945 io_type
= ZIO_TYPE_WRITE
;
946 } else if (strcasecmp(optarg
, "free") == 0) {
947 io_type
= ZIO_TYPE_FREE
;
948 } else if (strcasecmp(optarg
, "claim") == 0) {
949 io_type
= ZIO_TYPE_CLAIM
;
950 } else if (strcasecmp(optarg
, "all") == 0) {
953 (void) fprintf(stderr
, "invalid I/O type "
954 "'%s': must be 'read', 'write', 'free', "
955 "'claim' or 'all'\n", optarg
);
962 if ((type
= name_to_type(optarg
)) == TYPE_INVAL
&&
964 (void) fprintf(stderr
, "invalid type '%s'\n",
972 flags
|= ZINJECT_UNLOAD_SPA
;
975 if ((label
= name_to_type(optarg
)) == TYPE_INVAL
&&
977 (void) fprintf(stderr
, "invalid label type "
985 (void) fprintf(stderr
, "option -%c requires an "
986 "operand\n", optopt
);
991 (void) fprintf(stderr
, "invalid option '%c'\n",
1002 if (record
.zi_duration
!= 0)
1003 record
.zi_cmd
= ZINJECT_IGNORED_WRITES
;
1005 if (cancel
!= NULL
) {
1007 * '-c' is invalid with any other options.
1009 if (raw
!= NULL
|| range
!= NULL
|| type
!= TYPE_INVAL
||
1010 level
!= 0 || record
.zi_cmd
!= ZINJECT_UNINITIALIZED
||
1011 record
.zi_freq
> 0 || dvas
!= 0) {
1012 (void) fprintf(stderr
, "cancel (-c) incompatible with "
1013 "any other options\n");
1019 (void) fprintf(stderr
, "extraneous argument to '-c'\n");
1025 if (strcmp(cancel
, "all") == 0) {
1026 return (cancel_all_handlers());
1028 int id
= (int)strtol(cancel
, &end
, 10);
1030 (void) fprintf(stderr
, "invalid handle id '%s':"
1031 " must be an integer or 'all'\n", cancel
);
1036 return (cancel_handler(id
));
1040 if (device
!= NULL
) {
1042 * Device (-d) injection uses a completely different mechanism
1043 * for doing injection, so handle it separately here.
1045 if (raw
!= NULL
|| range
!= NULL
|| type
!= TYPE_INVAL
||
1046 level
!= 0 || record
.zi_cmd
!= ZINJECT_UNINITIALIZED
||
1048 (void) fprintf(stderr
, "device (-d) incompatible with "
1049 "data error injection\n");
1056 (void) fprintf(stderr
, "device (-d) injection requires "
1057 "a single pool name\n");
1063 (void) strlcpy(pool
, argv
[0], sizeof (pool
));
1066 if (error
== ECKSUM
) {
1067 (void) fprintf(stderr
, "device error type must be "
1068 "'io', 'nxio' or 'corrupt'\n");
1073 if (error
== EILSEQ
&&
1074 (record
.zi_freq
== 0 || io_type
!= ZIO_TYPE_READ
)) {
1075 (void) fprintf(stderr
, "device corrupt errors require "
1076 "io type read and a frequency value\n");
1081 record
.zi_iotype
= io_type
;
1082 if (translate_device(pool
, device
, label
, &record
) != 0) {
1089 if (action
!= VDEV_STATE_UNKNOWN
)
1090 return (perform_action(pool
, &record
, action
));
1092 } else if (raw
!= NULL
) {
1093 if (range
!= NULL
|| type
!= TYPE_INVAL
|| level
!= 0 ||
1094 record
.zi_cmd
!= ZINJECT_UNINITIALIZED
||
1095 record
.zi_freq
> 0 || dvas
!= 0) {
1096 (void) fprintf(stderr
, "raw (-b) format with "
1097 "any other options\n");
1104 (void) fprintf(stderr
, "raw (-b) format expects a "
1105 "single pool name\n");
1111 (void) strlcpy(pool
, argv
[0], sizeof (pool
));
1114 if (error
== ENXIO
) {
1115 (void) fprintf(stderr
, "data error type must be "
1116 "'checksum' or 'io'\n");
1121 record
.zi_cmd
= ZINJECT_DATA_FAULT
;
1122 if (translate_raw(raw
, &record
) != 0) {
1128 } else if (record
.zi_cmd
== ZINJECT_PANIC
) {
1129 if (raw
!= NULL
|| range
!= NULL
|| type
!= TYPE_INVAL
||
1130 level
!= 0 || device
!= NULL
|| record
.zi_freq
> 0 ||
1132 (void) fprintf(stderr
, "panic (-p) incompatible with "
1139 if (argc
< 1 || argc
> 2) {
1140 (void) fprintf(stderr
, "panic (-p) injection requires "
1141 "a single pool name and an optional id\n");
1147 (void) strlcpy(pool
, argv
[0], sizeof (pool
));
1148 if (argv
[1] != NULL
)
1149 record
.zi_type
= atoi(argv
[1]);
1151 } else if (record
.zi_cmd
== ZINJECT_IGNORED_WRITES
) {
1152 if (raw
!= NULL
|| range
!= NULL
|| type
!= TYPE_INVAL
||
1153 level
!= 0 || record
.zi_freq
> 0 || dvas
!= 0) {
1154 (void) fprintf(stderr
, "hardware failure (-I) "
1155 "incompatible with other options\n");
1161 if (nowrites
== 0) {
1162 (void) fprintf(stderr
, "-s or -g meaningless "
1163 "without -I (ignore writes)\n");
1167 } else if (dur_secs
&& dur_txg
) {
1168 (void) fprintf(stderr
, "choose a duration either "
1169 "in seconds (-s) or a number of txgs (-g) "
1174 } else if (argc
!= 1) {
1175 (void) fprintf(stderr
, "ignore writes (-I) "
1176 "injection requires a single pool name\n");
1182 (void) strlcpy(pool
, argv
[0], sizeof (pool
));
1184 } else if (type
== TYPE_INVAL
) {
1186 (void) fprintf(stderr
, "at least one of '-b', '-d', "
1187 "'-t', '-a', '-p', '-I' or '-u' "
1188 "must be specified\n");
1194 if (argc
== 1 && (flags
& ZINJECT_UNLOAD_SPA
)) {
1195 (void) strlcpy(pool
, argv
[0], sizeof (pool
));
1197 } else if (argc
!= 0) {
1198 (void) fprintf(stderr
, "extraneous argument for "
1205 flags
|= ZINJECT_NULL
;
1208 (void) fprintf(stderr
, "missing object\n");
1214 if (error
== ENXIO
|| error
== EILSEQ
) {
1215 (void) fprintf(stderr
, "data error type must be "
1216 "'checksum' or 'io'\n");
1222 if (error
== EACCES
|| error
== EINVAL
) {
1223 (void) fprintf(stderr
, "the '-C' option may "
1224 "not be used with logical data errors "
1225 "'decrypt' and 'decompress'\n");
1230 record
.zi_dvas
= dvas
;
1233 if (error
== EACCES
) {
1234 if (type
!= TYPE_DATA
) {
1235 (void) fprintf(stderr
, "decryption errors "
1236 "may only be injected for 'data' types\n");
1241 record
.zi_cmd
= ZINJECT_DECRYPT_FAULT
;
1243 * Internally, ZFS actually uses ECKSUM for decryption
1244 * errors since EACCES is used to indicate the key was
1249 record
.zi_cmd
= ZINJECT_DATA_FAULT
;
1252 if (translate_record(type
, argv
[0], range
, level
, &record
, pool
,
1262 * If this is pool-wide metadata, unmount everything. The ioctl() will
1263 * unload the pool, so that we trigger spa-wide reopen of metadata next
1264 * time we access the pool.
1266 if (dataset
[0] != '\0' && domount
) {
1267 if ((zhp
= zfs_open(g_zfs
, dataset
,
1268 ZFS_TYPE_DATASET
)) == NULL
) {
1272 if (zfs_unmount(zhp
, NULL
, 0) != 0) {
1278 record
.zi_error
= error
;
1280 ret
= register_handler(pool
, flags
, &record
, quiet
);
1282 if (dataset
[0] != '\0' && domount
)
1283 ret
= (zfs_mount(zhp
, NULL
, 0) != 0);