]>
git.proxmox.com Git - mirror_zfs.git/blob - cmd/zinject/zinject.c
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.
25 * Copyright (c) 2024, Klara Inc.
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.
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
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.
49 * For label faults, the -L option must be specified. This allows faults
50 * to be injected into either the nvlist, uberblock, pad1, or pad2 region
51 * of all the labels for the specified device.
53 * This form of the command looks like:
55 * zinject -d device [-e errno] [-L <uber | nvlist | pad1 | pad2>] pool
60 * We begin with a tuple of the form:
62 * <type,level,range,object>
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:
68 * data User data for a file
69 * dnode Dnode for a file or directory
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.
75 * mos Any data in the MOS
76 * mosdir object directory
77 * config pool configuration
81 * errlog persistent error log
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.
87 * range A numerical range [start,end) within the object. Defaults to
88 * the full size of the file.
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.
94 * This is translated, via libzpool, into the following internal representation:
96 * <type,objset,object,level,range>
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.
104 * The command itself takes one of the forms:
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
113 * With no arguments, the command prints all currently registered injection
114 * handlers, with their numeric identifiers.
116 * The '-c' option will clear the given handler, or all handlers if 'all' is
119 * The '-e' option takes a string describing the errno to simulate. This must
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.
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
131 * The '-f' flag controls the frequency of errors injected, expressed as a
132 * real number percentage between 0.0001 and 100. The default is 100.
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
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.
152 #include <sys/fs/zfs.h>
153 #include <sys/mount.h>
157 #undef verify /* both libzfs.h and zfs_context.h want to define this */
161 libzfs_handle_t
*g_zfs
;
164 static const char *const errtable
[TYPE_INVAL
] = {
181 name_to_type(const char *arg
)
184 for (i
= 0; i
< TYPE_INVAL
; i
++)
185 if (strcmp(errtable
[i
], arg
) == 0)
192 type_to_name(uint64_t type
)
195 case DMU_OT_OBJECT_DIRECTORY
:
197 case DMU_OT_OBJECT_ARRAY
:
199 case DMU_OT_PACKED_NVLIST
:
203 case DMU_OT_SPACE_MAP
:
205 case DMU_OT_ERROR_LOG
:
216 static const struct errstr errstrtable
[] = {
218 { ECKSUM
, "checksum" },
219 { EINVAL
, "decompress" },
220 { EACCES
, "decrypt" },
223 { EILSEQ
, "corrupt" },
228 str_to_err(const char *str
)
230 for (int i
= 0; errstrtable
[i
].str
!= NULL
; i
++)
231 if (strcasecmp(errstrtable
[i
].str
, str
) == 0)
232 return (errstrtable
[i
].err
);
238 for (int i
= 0; errstrtable
[i
].str
!= NULL
; i
++)
239 if (errstrtable
[i
].err
== err
)
240 return (errstrtable
[i
].str
);
241 return ("[unknown]");
245 * Print usage message.
255 "\t\tList all active injection records.\n"
257 "\tzinject -c <id|all>\n"
259 "\t\tClear the particular record (if given a numeric ID), or\n"
260 "\t\tall records if 'all' is specified.\n"
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"
267 "\tzinject -d device [-e errno] [-L <nvlist|uber|pad1|pad2>] [-F]\n"
268 "\t\t[-T <read|write|free|claim|ioctl|all>] [-f frequency] pool\n\n"
269 "\t\tInject a fault into a particular device or the device's\n"
270 "\t\tlabel. Label injection can either be 'nvlist', 'uber',\n "
271 "\t\t'pad1', or 'pad2'.\n"
272 "\t\t'errno' can be 'nxio' (the default), 'io', 'dtl', or\n"
273 "\t\t'corrupt' (bit flip).\n"
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"
277 "\tzinject -d device -A <degrade|fault> -D <delay secs> pool\n"
278 "\t\tPerform a specific action on a particular device.\n"
280 "\tzinject -d device -D latency:lanes pool\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"
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"
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"
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"
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"
319 "\tzinject -b objset:object:level:blkid pool\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"
323 "\t\thexadecimal, and only one block can be specified.\n"
325 "\tzinject [-q] <-t type> [-C dvas] [-e errno] [-l level]\n"
326 "\t\t[-r range] [-a] [-m] [-u] [-f freq] <object>\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"
330 "\t\tinterpreted depending on the '-t' option.\n"
332 "\t\t-q\tQuiet mode. Only print out the handler number added.\n"
333 "\t\t-e\tInject a specific error. Must be one of 'io',\n"
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"
338 "\t\t-l\tInject error at a particular block level. Default is "
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"
349 "\t\t\ta percentage between 0.0001 and 100.\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"
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"
361 "\t-t <mos>\tInject errors into the MOS for objects of the given\n"
362 "\t\t\ttype. Valid types are: mos, mosdir, config, bpobj,\n"
363 "\t\t\tspacemap, metaslab, errlog. The only valid <object> is\n"
364 "\t\t\tthe poolname.\n");
368 iter_handlers(int (*func
)(int, const char *, zinject_record_t
*, void *),
371 zfs_cmd_t zc
= {"\0"};
374 while (zfs_ioctl(g_zfs
, ZFS_IOC_INJECT_LIST_NEXT
, &zc
) == 0)
375 if ((ret
= func((int)zc
.zc_guid
, zc
.zc_name
,
376 &zc
.zc_inject_record
, data
)) != 0)
379 if (errno
!= ENOENT
) {
380 (void) fprintf(stderr
, "Unable to list handlers: %s\n",
389 print_data_handler(int id
, const char *pool
, zinject_record_t
*record
,
394 if (record
->zi_guid
!= 0 || record
->zi_func
[0] != '\0')
398 (void) printf("%3s %-15s %-6s %-6s %-8s %3s %-4s "
399 "%-15s\n", "ID", "POOL", "OBJSET", "OBJECT", "TYPE",
400 "LVL", "DVAs", "RANGE");
401 (void) printf("--- --------------- ------ "
402 "------ -------- --- ---- ---------------\n");
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
);
413 if (record
->zi_start
== 0 &&
414 record
->zi_end
== -1ULL)
415 (void) printf("all\n");
417 (void) printf("[%llu, %llu]\n", (u_longlong_t
)record
->zi_start
,
418 (u_longlong_t
)record
->zi_end
);
424 print_device_handler(int id
, const char *pool
, zinject_record_t
*record
,
427 static const char *iotypestr
[] = {
428 "null", "read", "write", "free", "claim", "ioctl", "trim", "all",
433 if (record
->zi_guid
== 0 || record
->zi_func
[0] != '\0')
436 if (record
->zi_cmd
== ZINJECT_DELAY_IO
)
440 (void) printf("%3s %-15s %-16s %-5s %-10s %-9s\n",
441 "ID", "POOL", "GUID", "TYPE", "ERROR", "FREQ");
443 "--- --------------- ---------------- "
444 "----- ---------- ---------\n");
449 double freq
= record
->zi_freq
== 0 ? 100.0f
:
450 (((double)record
->zi_freq
) / ZI_PERCENTAGE_MAX
) * 100.0f
;
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
);
460 print_delay_handler(int id
, const char *pool
, zinject_record_t
*record
,
465 if (record
->zi_guid
== 0 || record
->zi_func
[0] != '\0')
468 if (record
->zi_cmd
!= ZINJECT_DELAY_IO
)
472 (void) printf("%3s %-15s %-15s %-15s %s\n",
473 "ID", "POOL", "DELAY (ms)", "LANES", "GUID");
474 (void) printf("--- --------------- --------------- "
475 "--------------- ----------------\n");
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
);
489 print_panic_handler(int id
, const char *pool
, zinject_record_t
*record
,
494 if (record
->zi_func
[0] == '\0')
498 (void) printf("%3s %-15s %s\n", "ID", "POOL", "FUNCTION");
499 (void) printf("--- --------------- ----------------\n");
504 (void) printf("%3d %-15s %s\n", id
, pool
, record
->zi_func
);
510 * Print all registered error handlers. Returns the number of handlers
514 print_all_handlers(void)
516 int count
= 0, total
= 0;
518 (void) iter_handlers(print_device_handler
, &count
);
525 (void) iter_handlers(print_delay_handler
, &count
);
532 (void) iter_handlers(print_data_handler
, &count
);
539 (void) iter_handlers(print_panic_handler
, &count
);
541 return (count
+ total
);
545 cancel_one_handler(int id
, const char *pool
, zinject_record_t
*record
,
548 (void) pool
, (void) record
, (void) data
;
549 zfs_cmd_t zc
= {"\0"};
551 zc
.zc_guid
= (uint64_t)id
;
553 if (zfs_ioctl(g_zfs
, ZFS_IOC_CLEAR_FAULT
, &zc
) != 0) {
554 (void) fprintf(stderr
, "failed to remove handler %d: %s\n",
555 id
, strerror(errno
));
563 * Remove all fault injection handlers.
566 cancel_all_handlers(void)
568 int ret
= iter_handlers(cancel_one_handler
, NULL
);
571 (void) printf("removed all registered handlers\n");
577 * Remove a specific fault injection handler.
580 cancel_handler(int id
)
582 zfs_cmd_t zc
= {"\0"};
584 zc
.zc_guid
= (uint64_t)id
;
586 if (zfs_ioctl(g_zfs
, ZFS_IOC_CLEAR_FAULT
, &zc
) != 0) {
587 (void) fprintf(stderr
, "failed to remove handler %d: %s\n",
588 id
, strerror(errno
));
592 (void) printf("removed handler %d\n", id
);
598 * Register a new fault injection handler.
601 register_handler(const char *pool
, int flags
, zinject_record_t
*record
,
604 zfs_cmd_t zc
= {"\0"};
606 (void) strlcpy(zc
.zc_name
, pool
, sizeof (zc
.zc_name
));
607 zc
.zc_inject_record
= *record
;
610 if (zfs_ioctl(g_zfs
, ZFS_IOC_INJECT_FAULT
, &zc
) != 0) {
611 (void) fprintf(stderr
, "failed to add handler: %s\n",
612 errno
== EDOM
? "block level exceeds max level of object" :
617 if (flags
& ZINJECT_NULL
)
621 (void) printf("%llu\n", (u_longlong_t
)zc
.zc_guid
);
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
);
629 } else if (record
->zi_func
[0] != '\0') {
630 (void) printf(" panic function: %s\n",
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
);
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");
650 (void) printf(" range: [%llu, %llu)\n",
651 (u_longlong_t
)record
->zi_start
,
652 (u_longlong_t
)record
->zi_end
);
653 (void) printf(" dvas: 0x%x\n", record
->zi_dvas
);
661 perform_action(const char *pool
, zinject_record_t
*record
, int cmd
)
663 zfs_cmd_t zc
= {"\0"};
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
;
670 if (zfs_ioctl(g_zfs
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
677 parse_delay(char *str
, uint64_t *delay
, uint64_t *nlanes
)
679 unsigned long scan_delay
;
680 unsigned long scan_nlanes
;
682 if (sscanf(str
, "%lu:%lu", &scan_delay
, &scan_nlanes
) != 2)
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.
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.
699 *delay
= MSEC2NSEC(scan_delay
);
700 *nlanes
= scan_nlanes
;
706 parse_frequency(const char *str
, uint32_t *percent
)
711 val
= strtod(str
, &post
);
712 if (post
== NULL
|| *post
!= '\0')
715 /* valid range is [0.0001, 100.0] */
717 if (val
< 0.000001f
|| val
> 1.0f
)
720 /* convert to an integer for use by kernel */
721 *percent
= ((uint32_t)(val
* ZI_PERCENTAGE_MAX
));
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)
735 parse_dvas(const char *str
, uint32_t *dvas_out
)
739 boolean_t need_delim
= B_FALSE
;
741 /* max string length is 5 ("0,1,2") */
742 if (strlen(str
) > 5 || strlen(str
) == 0)
750 /* check for pipe between DVAs */
754 /* check if this DVA has been set already */
755 if (mask
& (1 << ((*c
) - '0')))
758 mask
|= (1 << ((*c
) - '0'));
762 need_delim
= B_FALSE
;
765 /* check for invalid character */
771 /* check for dangling delimiter */
780 main(int argc
, char **argv
)
792 int io_type
= ZIO_TYPES
;
793 int action
= VDEV_STATE_UNKNOWN
;
794 err_type_t type
= TYPE_INVAL
;
795 err_type_t label
= TYPE_INVAL
;
796 zinject_record_t record
= { 0 };
797 char pool
[MAXNAMELEN
] = "";
798 char dataset
[MAXNAMELEN
] = "";
799 zfs_handle_t
*zhp
= NULL
;
807 if ((g_zfs
= libzfs_init()) == NULL
) {
808 (void) fprintf(stderr
, "%s\n", libzfs_error_init(errno
));
812 libzfs_print_on_error(g_zfs
, B_TRUE
);
814 if ((zfs_fd
= open(ZFS_DEV
, O_RDWR
)) < 0) {
815 (void) fprintf(stderr
, "failed to open ZFS device\n");
822 * No arguments. Print the available handlers. If there are no
823 * available handlers, direct the user to '-h' for help
826 if (print_all_handlers() == 0) {
827 (void) printf("No handlers registered.\n");
828 (void) printf("Run 'zinject -h' for usage "
835 while ((c
= getopt(argc
, argv
,
836 ":aA:b:C:d:D:f:Fg:qhIc:t:T:l:mr:s:e:uL:p:")) != -1) {
839 flags
|= ZINJECT_FLUSH_ARC
;
842 if (strcasecmp(optarg
, "degrade") == 0) {
843 action
= VDEV_STATE_DEGRADED
;
844 } else if (strcasecmp(optarg
, "fault") == 0) {
845 action
= VDEV_STATE_FAULTED
;
847 (void) fprintf(stderr
, "invalid action '%s': "
848 "must be 'degrade' or 'fault'\n", optarg
);
861 ret
= parse_dvas(optarg
, &dvas
);
863 (void) fprintf(stderr
, "invalid DVA list '%s': "
864 "DVAs should be 0 indexed and separated by "
865 "commas.\n", optarg
);
876 ret
= parse_delay(optarg
, &record
.zi_timer
,
880 (void) fprintf(stderr
, "invalid i/o delay "
881 "value: '%s'\n", optarg
);
888 error
= str_to_err(optarg
);
890 (void) fprintf(stderr
, "invalid error type "
891 "'%s': must be one of: io decompress "
892 "decrypt nxio dtl corrupt\n",
900 ret
= parse_frequency(optarg
, &record
.zi_freq
);
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: " : "");
911 record
.zi_failfast
= B_TRUE
;
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
);
923 /* store duration of txgs as its negative */
924 record
.zi_duration
*= -1;
931 /* default duration, if one hasn't yet been defined */
933 if (dur_secs
== 0 && dur_txg
== 0)
934 record
.zi_duration
= 30;
937 level
= (int)strtol(optarg
, &end
, 10);
939 (void) fprintf(stderr
, "invalid level '%s': "
940 "must be an integer\n", optarg
);
950 (void) strlcpy(record
.zi_func
, optarg
,
951 sizeof (record
.zi_func
));
952 record
.zi_cmd
= ZINJECT_PANIC
;
959 flags
|= ZINJECT_CALC_RANGE
;
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
);
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
, "ioctl") == 0) {
982 io_type
= ZIO_TYPE_IOCTL
;
983 } else if (strcasecmp(optarg
, "all") == 0) {
986 (void) fprintf(stderr
, "invalid I/O type "
987 "'%s': must be 'read', 'write', 'free', "
988 "'claim', 'ioctl' or 'all'\n", optarg
);
995 if ((type
= name_to_type(optarg
)) == TYPE_INVAL
&&
997 (void) fprintf(stderr
, "invalid type '%s'\n",
1005 flags
|= ZINJECT_UNLOAD_SPA
;
1008 if ((label
= name_to_type(optarg
)) == TYPE_INVAL
&&
1009 !LABEL_TYPE(type
)) {
1010 (void) fprintf(stderr
, "invalid label type "
1018 (void) fprintf(stderr
, "option -%c requires an "
1019 "operand\n", optopt
);
1024 (void) fprintf(stderr
, "invalid option '%c'\n",
1035 if (record
.zi_duration
!= 0)
1036 record
.zi_cmd
= ZINJECT_IGNORED_WRITES
;
1038 if (cancel
!= NULL
) {
1040 * '-c' is invalid with any other options.
1042 if (raw
!= NULL
|| range
!= NULL
|| type
!= TYPE_INVAL
||
1043 level
!= 0 || record
.zi_cmd
!= ZINJECT_UNINITIALIZED
||
1044 record
.zi_freq
> 0 || dvas
!= 0) {
1045 (void) fprintf(stderr
, "cancel (-c) incompatible with "
1046 "any other options\n");
1052 (void) fprintf(stderr
, "extraneous argument to '-c'\n");
1058 if (strcmp(cancel
, "all") == 0) {
1059 return (cancel_all_handlers());
1061 int id
= (int)strtol(cancel
, &end
, 10);
1063 (void) fprintf(stderr
, "invalid handle id '%s':"
1064 " must be an integer or 'all'\n", cancel
);
1069 return (cancel_handler(id
));
1073 if (device
!= NULL
) {
1075 * Device (-d) injection uses a completely different mechanism
1076 * for doing injection, so handle it separately here.
1078 if (raw
!= NULL
|| range
!= NULL
|| type
!= TYPE_INVAL
||
1079 level
!= 0 || record
.zi_cmd
!= ZINJECT_UNINITIALIZED
||
1081 (void) fprintf(stderr
, "device (-d) incompatible with "
1082 "data error injection\n");
1089 (void) fprintf(stderr
, "device (-d) injection requires "
1090 "a single pool name\n");
1096 (void) strlcpy(pool
, argv
[0], sizeof (pool
));
1099 if (error
== ECKSUM
) {
1100 (void) fprintf(stderr
, "device error type must be "
1101 "'io', 'nxio' or 'corrupt'\n");
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");
1114 record
.zi_iotype
= io_type
;
1115 if (translate_device(pool
, device
, label
, &record
) != 0) {
1120 if (record
.zi_nlanes
) {
1123 case ZIO_TYPE_WRITE
:
1127 (void) fprintf(stderr
, "I/O type for a delay "
1128 "must be 'read' or 'write'\n");
1138 if (action
!= VDEV_STATE_UNKNOWN
)
1139 return (perform_action(pool
, &record
, action
));
1141 } else if (raw
!= NULL
) {
1142 if (range
!= NULL
|| type
!= TYPE_INVAL
|| level
!= 0 ||
1143 record
.zi_cmd
!= ZINJECT_UNINITIALIZED
||
1144 record
.zi_freq
> 0 || dvas
!= 0) {
1145 (void) fprintf(stderr
, "raw (-b) format with "
1146 "any other options\n");
1153 (void) fprintf(stderr
, "raw (-b) format expects a "
1154 "single pool name\n");
1160 (void) strlcpy(pool
, argv
[0], sizeof (pool
));
1163 if (error
== ENXIO
) {
1164 (void) fprintf(stderr
, "data error type must be "
1165 "'checksum' or 'io'\n");
1170 record
.zi_cmd
= ZINJECT_DATA_FAULT
;
1171 if (translate_raw(raw
, &record
) != 0) {
1177 } else if (record
.zi_cmd
== ZINJECT_PANIC
) {
1178 if (raw
!= NULL
|| range
!= NULL
|| type
!= TYPE_INVAL
||
1179 level
!= 0 || device
!= NULL
|| record
.zi_freq
> 0 ||
1181 (void) fprintf(stderr
, "panic (-p) incompatible with "
1188 if (argc
< 1 || argc
> 2) {
1189 (void) fprintf(stderr
, "panic (-p) injection requires "
1190 "a single pool name and an optional id\n");
1196 (void) strlcpy(pool
, argv
[0], sizeof (pool
));
1197 if (argv
[1] != NULL
)
1198 record
.zi_type
= atoi(argv
[1]);
1200 } else if (record
.zi_cmd
== ZINJECT_IGNORED_WRITES
) {
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");
1210 if (nowrites
== 0) {
1211 (void) fprintf(stderr
, "-s or -g meaningless "
1212 "without -I (ignore writes)\n");
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) "
1223 } else if (argc
!= 1) {
1224 (void) fprintf(stderr
, "ignore writes (-I) "
1225 "injection requires a single pool name\n");
1231 (void) strlcpy(pool
, argv
[0], sizeof (pool
));
1233 } else if (type
== TYPE_INVAL
) {
1235 (void) fprintf(stderr
, "at least one of '-b', '-d', "
1236 "'-t', '-a', '-p', '-I' or '-u' "
1237 "must be specified\n");
1243 if (argc
== 1 && (flags
& ZINJECT_UNLOAD_SPA
)) {
1244 (void) strlcpy(pool
, argv
[0], sizeof (pool
));
1246 } else if (argc
!= 0) {
1247 (void) fprintf(stderr
, "extraneous argument for "
1254 flags
|= ZINJECT_NULL
;
1257 (void) fprintf(stderr
, "missing object\n");
1263 if (error
== ENXIO
|| error
== EILSEQ
) {
1264 (void) fprintf(stderr
, "data error type must be "
1265 "'checksum' or 'io'\n");
1271 if (error
== EACCES
|| error
== EINVAL
) {
1272 (void) fprintf(stderr
, "the '-C' option may "
1273 "not be used with logical data errors "
1274 "'decrypt' and 'decompress'\n");
1279 record
.zi_dvas
= dvas
;
1282 if (error
== EACCES
) {
1283 if (type
!= TYPE_DATA
) {
1284 (void) fprintf(stderr
, "decryption errors "
1285 "may only be injected for 'data' types\n");
1290 record
.zi_cmd
= ZINJECT_DECRYPT_FAULT
;
1292 * Internally, ZFS actually uses ECKSUM for decryption
1293 * errors since EACCES is used to indicate the key was
1298 record
.zi_cmd
= ZINJECT_DATA_FAULT
;
1301 if (translate_record(type
, argv
[0], range
, level
, &record
, pool
,
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.
1315 if (dataset
[0] != '\0' && domount
) {
1316 if ((zhp
= zfs_open(g_zfs
, dataset
,
1317 ZFS_TYPE_DATASET
)) == NULL
) {
1321 if (zfs_unmount(zhp
, NULL
, 0) != 0) {
1327 record
.zi_error
= error
;
1329 ret
= register_handler(pool
, flags
, &record
, quiet
);
1331 if (dataset
[0] != '\0' && domount
)
1332 ret
= (zfs_mount(zhp
, NULL
, 0) != 0);