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 http://www.opensolaris.org/os/licensing.
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 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "@(#)zinject.c 1.4 07/09/17 SMI"
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 two types of errors that can be injected, EIO and ENXIO,
41 * that can be controlled through the '-t' option. The default is ENXIO. For
42 * EIO failures, any attempt to read data from the device will return EIO, but
43 * subsequent attempt to reopen the device will succeed. For ENXIO failures,
44 * any attempt to read from the device will return EIO, but any attempt to
45 * reopen the device will also return ENXIO.
47 * This form of the command looks like:
49 * zinject -d device [-t type] pool
54 * We begin with a tuple of the form:
56 * <type,level,range,object>
58 * type A string describing the type of data to target. Each type
59 * implicitly describes how to interpret 'object'. Currently,
60 * the following values are supported:
62 * data User data for a file
63 * dnode Dnode for a file or directory
65 * The following MOS objects are special. Instead of injecting
66 * errors on a particular object or blkid, we inject errors across
67 * all objects of the given type.
69 * mos Any data in the MOS
70 * mosdir object directory
71 * config pool configuration
75 * errlog persistent error log
77 * level Object level. Defaults to '0', not applicable to all types. If
78 * a range is given, this corresponds to the indirect block
79 * corresponding to the specific range.
81 * range A numerical range [start,end) within the object. Defaults to
82 * the full size of the file.
84 * object A string describing the logical location of the object. For
85 * files and directories (currently the only supported types),
86 * this is the path of the object on disk.
88 * This is translated, via libzpool, into the following internal representation:
90 * <type,objset,object,level,range>
92 * These types should be self-explanatory. This tuple is then passed to the
93 * kernel via a special ioctl() to initiate fault injection for the given
94 * object. Note that 'type' is not strictly necessary for fault injection, but
95 * is used when translating existing faults into a human-readable string.
98 * The command itself takes one of the forms:
101 * zinject <-a | -u pool>
102 * zinject -c <id|all>
103 * zinject [-q] <-t type> [-f freq] [-u] [-a] [-m] [-e errno] [-l level]
104 * [-r range] <object>
105 * zinject [-f freq] [-a] [-m] [-u] -b objset:object:level:start:end pool
107 * With no arguments, the command prints all currently registered injection
108 * handlers, with their numeric identifiers.
110 * The '-c' option will clear the given handler, or all handlers if 'all' is
113 * The '-e' option takes a string describing the errno to simulate. This must
114 * be either 'io' or 'checksum'. In most cases this will result in the same
115 * behavior, but RAID-Z will produce a different set of ereports for this
118 * The '-a', '-u', and '-m' flags toggle internal flush behavior. If '-a' is
119 * specified, then the ARC cache is flushed appropriately. If '-u' is
120 * specified, then the underlying SPA is unloaded. Either of these flags can be
121 * specified independently of any other handlers. The '-m' flag automatically
122 * does an unmount and remount of the underlying dataset to aid in flushing the
125 * The '-f' flag controls the frequency of errors injected, expressed as a
126 * integer percentage between 1 and 100. The default is 100.
128 * The this form is responsible for actually injecting the handler into the
129 * framework. It takes the arguments described above, translates them to the
130 * internal tuple using libzpool, and then issues an ioctl() to register the
133 * The final form can target a specific bookmark, regardless of whether a
134 * human-readable interface has been designed. It allows developers to specify
135 * a particular block by number.
145 #include <sys/fs/zfs.h>
146 #include <sys/mount.h>
150 #undef verify /* both libzfs.h and zfs_context.h want to define this */
154 libzfs_handle_t *g_zfs;
159 static const char *errtable[TYPE_INVAL] = {
172 name_to_type(const char *arg)
175 for (i = 0; i < TYPE_INVAL; i++)
176 if (strcmp(errtable[i], arg) == 0)
183 type_to_name(uint64_t type)
186 case DMU_OT_OBJECT_DIRECTORY:
188 case DMU_OT_OBJECT_ARRAY:
190 case DMU_OT_PACKED_NVLIST:
194 case DMU_OT_SPACE_MAP:
196 case DMU_OT_ERROR_LOG:
205 * Print usage message.
215 "\t\tList all active injection records.\n"
217 "\tzinject -c <id|all>\n"
219 "\t\tClear the particular record (if given a numeric ID), or\n"
220 "\t\tall records if 'all' is specificed.\n"
222 "\tzinject -d device [-e errno] pool\n"
223 "\t\tInject a fault into a particular device. 'errno' can either\n"
224 "\t\tbe 'nxio' (the default) or 'io'.\n"
226 "\tzinject -b objset:object:level:blkid pool\n"
228 "\t\tInject an error into pool 'pool' with the numeric bookmark\n"
229 "\t\tspecified by the remaining tuple. Each number is in\n"
230 "\t\thexidecimal, and only one block can be specified.\n"
232 "\tzinject [-q] <-t type> [-e errno] [-l level] [-r range]\n"
233 "\t [-a] [-m] [-u] [-f freq] <object>\n"
235 "\t\tInject an error into the object specified by the '-t' option\n"
236 "\t\tand the object descriptor. The 'object' parameter is\n"
237 "\t\tinterperted depending on the '-t' option.\n"
239 "\t\t-q\tQuiet mode. Only print out the handler number added.\n"
240 "\t\t-e\tInject a specific error. Must be either 'io' or\n"
241 "\t\t\t'checksum'. Default is 'io'.\n"
242 "\t\t-l\tInject error at a particular block level. Default is "
244 "\t\t-m\tAutomatically remount underlying filesystem.\n"
245 "\t\t-r\tInject error over a particular logical range of an\n"
246 "\t\t\tobject. Will be translated to the appropriate blkid\n"
247 "\t\t\trange according to the object's properties.\n"
248 "\t\t-a\tFlush the ARC cache. Can be specified without any\n"
249 "\t\t\tassociated object.\n"
250 "\t\t-u\tUnload the associated pool. Can be specified with only\n"
251 "\t\t\ta pool object.\n"
252 "\t\t-f\tOnly inject errors a fraction of the time. Expressed as\n"
253 "\t\t\ta percentage between 1 and 100.\n"
255 "\t-t data\t\tInject an error into the plain file contents of a\n"
256 "\t\t\tfile. The object must be specified as a complete path\n"
257 "\t\t\tto a file on a ZFS filesystem.\n"
259 "\t-t dnode\tInject an error into the metadnode in the block\n"
260 "\t\t\tcorresponding to the dnode for a file or directory. The\n"
261 "\t\t\t'-r' option is incompatible with this mode. The object\n"
262 "\t\t\tis specified as a complete path to a file or directory\n"
263 "\t\t\ton a ZFS filesystem.\n"
265 "\t-t <mos>\tInject errors into the MOS for objects of the given\n"
266 "\t\t\ttype. Valid types are: mos, mosdir, config, bplist,\n"
267 "\t\t\tspacemap, metaslab, errlog. The only valid <object> is\n"
268 "\t\t\tthe poolname.\n");
272 iter_handlers(int (*func)(int, const char *, zinject_record_t *, void *),
280 while (ioctl(zfs_fd, ZFS_IOC_INJECT_LIST_NEXT, &zc) == 0)
281 if ((ret = func((int)zc.zc_guid, zc.zc_name,
282 &zc.zc_inject_record, data)) != 0)
289 print_data_handler(int id, const char *pool, zinject_record_t *record,
294 if (record->zi_guid != 0)
298 (void) printf("%3s %-15s %-6s %-6s %-8s %3s %-15s\n",
299 "ID", "POOL", "OBJSET", "OBJECT", "TYPE", "LVL", "RANGE");
300 (void) printf("--- --------------- ------ "
301 "------ -------- --- ---------------\n");
306 (void) printf("%3d %-15s %-6llu %-6llu %-8s %3d ", id, pool,
307 (u_longlong_t)record->zi_objset, (u_longlong_t)record->zi_object,
308 type_to_name(record->zi_type), record->zi_level);
310 if (record->zi_start == 0 &&
311 record->zi_end == -1ULL)
312 (void) printf("all\n");
314 (void) printf("[%llu, %llu]\n", (u_longlong_t)record->zi_start,
315 (u_longlong_t)record->zi_end);
321 print_device_handler(int id, const char *pool, zinject_record_t *record,
326 if (record->zi_guid == 0)
330 (void) printf("%3s %-15s %s\n", "ID", "POOL", "GUID");
331 (void) printf("--- --------------- ----------------\n");
336 (void) printf("%3d %-15s %llx\n", id, pool,
337 (u_longlong_t)record->zi_guid);
343 * Print all registered error handlers. Returns the number of handlers
347 print_all_handlers(void)
351 (void) iter_handlers(print_device_handler, &count);
354 (void) iter_handlers(print_data_handler, &count);
361 cancel_one_handler(int id, const char *pool, zinject_record_t *record,
366 zc.zc_guid = (uint64_t)id;
368 if (ioctl(zfs_fd, ZFS_IOC_CLEAR_FAULT, &zc) != 0) {
369 (void) fprintf(stderr, "failed to remove handler %d: %s\n",
370 id, strerror(errno));
378 * Remove all fault injection handlers.
381 cancel_all_handlers(void)
383 int ret = iter_handlers(cancel_one_handler, NULL);
385 (void) printf("removed all registered handlers\n");
391 * Remove a specific fault injection handler.
394 cancel_handler(int id)
398 zc.zc_guid = (uint64_t)id;
400 if (ioctl(zfs_fd, ZFS_IOC_CLEAR_FAULT, &zc) != 0) {
401 (void) fprintf(stderr, "failed to remove handler %d: %s\n",
402 id, strerror(errno));
406 (void) printf("removed handler %d\n", id);
412 * Register a new fault injection handler.
415 register_handler(const char *pool, int flags, zinject_record_t *record,
420 (void) strcpy(zc.zc_name, pool);
421 zc.zc_inject_record = *record;
424 if (ioctl(zfs_fd, ZFS_IOC_INJECT_FAULT, &zc) != 0) {
425 (void) fprintf(stderr, "failed to add handler: %s\n",
430 if (flags & ZINJECT_NULL)
434 (void) printf("%llu\n", (u_longlong_t)zc.zc_guid);
436 (void) printf("Added handler %llu with the following "
437 "properties:\n", (u_longlong_t)zc.zc_guid);
438 (void) printf(" pool: %s\n", pool);
439 if (record->zi_guid) {
440 (void) printf(" vdev: %llx\n",
441 (u_longlong_t)record->zi_guid);
443 (void) printf("objset: %llu\n",
444 (u_longlong_t)record->zi_objset);
445 (void) printf("object: %llu\n",
446 (u_longlong_t)record->zi_object);
447 (void) printf(" type: %llu\n",
448 (u_longlong_t)record->zi_type);
449 (void) printf(" level: %d\n", record->zi_level);
450 if (record->zi_start == 0 &&
451 record->zi_end == -1ULL)
452 (void) printf(" range: all\n");
454 (void) printf(" range: [%llu, %llu)\n",
455 (u_longlong_t)record->zi_start,
456 (u_longlong_t)record->zi_end);
464 main(int argc, char **argv)
476 err_type_t type = TYPE_INVAL;
477 zinject_record_t record = { 0 };
478 char pool[MAXNAMELEN];
479 char dataset[MAXNAMELEN];
484 if ((g_zfs = libzfs_init()) == NULL) {
485 (void) fprintf(stderr, "internal error: failed to "
486 "initialize ZFS library\n");
490 libzfs_print_on_error(g_zfs, B_TRUE);
492 if ((zfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
493 (void) fprintf(stderr, "failed to open ZFS device\n");
499 * No arguments. Print the available handlers. If there are no
500 * available handlers, direct the user to '-h' for help
503 if (print_all_handlers() == 0) {
504 (void) printf("No handlers registered.\n");
505 (void) printf("Run 'zinject -h' for usage "
512 while ((c = getopt(argc, argv, ":ab:d:f:qhc:t:l:mr:e:u")) != -1) {
515 flags |= ZINJECT_FLUSH_ARC;
527 if (strcasecmp(optarg, "io") == 0) {
529 } else if (strcasecmp(optarg, "checksum") == 0) {
531 } else if (strcasecmp(optarg, "nxio") == 0) {
534 (void) fprintf(stderr, "invalid error type "
535 "'%s': must be 'io', 'checksum' or "
542 record.zi_freq = atoi(optarg);
543 if (record.zi_freq < 1 || record.zi_freq > 100) {
544 (void) fprintf(stderr, "frequency range must "
545 "be in the range (0, 100]\n");
553 level = (int)strtol(optarg, &end, 10);
555 (void) fprintf(stderr, "invalid level '%s': "
556 "must be an integer\n", optarg);
571 if ((type = name_to_type(optarg)) == TYPE_INVAL) {
572 (void) fprintf(stderr, "invalid type '%s'\n",
579 flags |= ZINJECT_UNLOAD_SPA;
582 (void) fprintf(stderr, "option -%c requires an "
583 "operand\n", optopt);
587 (void) fprintf(stderr, "invalid option '%c'\n",
597 if (cancel != NULL) {
599 * '-c' is invalid with any other options.
601 if (raw != NULL || range != NULL || type != TYPE_INVAL ||
603 (void) fprintf(stderr, "cancel (-c) incompatible with "
604 "any other options\n");
609 (void) fprintf(stderr, "extraneous argument to '-c'\n");
614 if (strcmp(cancel, "all") == 0) {
615 return (cancel_all_handlers());
617 int id = (int)strtol(cancel, &end, 10);
619 (void) fprintf(stderr, "invalid handle id '%s':"
620 " must be an integer or 'all'\n", cancel);
624 return (cancel_handler(id));
628 if (device != NULL) {
630 * Device (-d) injection uses a completely different mechanism
631 * for doing injection, so handle it separately here.
633 if (raw != NULL || range != NULL || type != TYPE_INVAL ||
635 (void) fprintf(stderr, "device (-d) incompatible with "
636 "data error injection\n");
642 (void) fprintf(stderr, "device (-d) injection requires "
643 "a single pool name\n");
648 (void) strcpy(pool, argv[0]);
651 if (error == ECKSUM) {
652 (void) fprintf(stderr, "device error type must be "
657 if (translate_device(pool, device, &record) != 0)
661 } else if (raw != NULL) {
662 if (range != NULL || type != TYPE_INVAL || level != 0) {
663 (void) fprintf(stderr, "raw (-b) format with "
664 "any other options\n");
670 (void) fprintf(stderr, "raw (-b) format expects a "
671 "single pool name\n");
676 (void) strcpy(pool, argv[0]);
679 if (error == ENXIO) {
680 (void) fprintf(stderr, "data error type must be "
681 "'checksum' or 'io'\n");
685 if (translate_raw(raw, &record) != 0)
689 } else if (type == TYPE_INVAL) {
691 (void) fprintf(stderr, "at least one of '-b', '-d', "
692 "'-t', '-a', or '-u' must be specified\n");
697 if (argc == 1 && (flags & ZINJECT_UNLOAD_SPA)) {
698 (void) strcpy(pool, argv[0]);
700 } else if (argc != 0) {
701 (void) fprintf(stderr, "extraneous argument for "
707 flags |= ZINJECT_NULL;
710 (void) fprintf(stderr, "missing object\n");
715 if (error == ENXIO) {
716 (void) fprintf(stderr, "data error type must be "
717 "'checksum' or 'io'\n");
721 if (translate_record(type, argv[0], range, level, &record, pool,
729 * If this is pool-wide metadata, unmount everything. The ioctl() will
730 * unload the pool, so that we trigger spa-wide reopen of metadata next
731 * time we access the pool.
733 if (dataset[0] != '\0' && domount) {
734 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_DATASET)) == NULL)
737 if (zfs_unmount(zhp, NULL, 0) != 0)
741 record.zi_error = error;
743 ret = register_handler(pool, flags, &record, quiet);
745 if (dataset[0] != '\0' && domount)
746 ret = (zfs_mount(zhp, NULL, 0) != 0);