]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - cddl/contrib/opensolaris/cmd/zinject/zinject.c
Merge ZFS feature flags support and related bugfixes:
[FreeBSD/stable/9.git] / cddl / contrib / opensolaris / cmd / zinject / zinject.c
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
9  * or http://www.opensolaris.org/os/licensing.
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 /*
22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24
25 /*
26  * ZFS Fault Injector
27  *
28  * This userland component takes a set of options and uses libzpool to translate
29  * from a user-visible object type and name to an internal representation.
30  * There are two basic types of faults: device faults and data faults.
31  *
32  *
33  * DEVICE FAULTS
34  *
35  * Errors can be injected into a particular vdev using the '-d' option.  This
36  * option takes a path or vdev GUID to uniquely identify the device within a
37  * pool.  There are two types of errors that can be injected, EIO and ENXIO,
38  * that can be controlled through the '-e' option.  The default is ENXIO.  For
39  * EIO failures, any attempt to read data from the device will return EIO, but
40  * subsequent attempt to reopen the device will succeed.  For ENXIO failures,
41  * any attempt to read from the device will return EIO, but any attempt to
42  * reopen the device will also return ENXIO.
43  * For label faults, the -L option must be specified. This allows faults
44  * to be injected into either the nvlist, uberblock, pad1, or pad2 region
45  * of all the labels for the specified device.
46  *
47  * This form of the command looks like:
48  *
49  *      zinject -d device [-e errno] [-L <uber | nvlist | pad1 | pad2>] pool
50  *
51  *
52  * DATA FAULTS
53  *
54  * We begin with a tuple of the form:
55  *
56  *      <type,level,range,object>
57  *
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:
61  *
62  *              data            User data for a file
63  *              dnode           Dnode for a file or directory
64  *
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.
68  *
69  *              mos             Any data in the MOS
70  *              mosdir          object directory
71  *              config          pool configuration
72  *              bpobj           blkptr list
73  *              spacemap        spacemap
74  *              metaslab        metaslab
75  *              errlog          persistent error log
76  *
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.
80  *
81  *      range   A numerical range [start,end) within the object.  Defaults to
82  *              the full size of the file.
83  *
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.
87  *
88  * This is translated, via libzpool, into the following internal representation:
89  *
90  *      <type,objset,object,level,range>
91  *
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.
96  *
97  *
98  * The command itself takes one of the forms:
99  *
100  *      zinject
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
106  *
107  * With no arguments, the command prints all currently registered injection
108  * handlers, with their numeric identifiers.
109  *
110  * The '-c' option will clear the given handler, or all handlers if 'all' is
111  * specified.
112  *
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
116  * situation.
117  *
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
123  * cache.
124  *
125  * The '-f' flag controls the frequency of errors injected, expressed as a
126  * integer percentage between 1 and 100.  The default is 100.
127  *
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
131  * handler.
132  *
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.
136  */
137
138 #include <errno.h>
139 #include <fcntl.h>
140 #include <stdio.h>
141 #include <stdlib.h>
142 #include <strings.h>
143 #include <unistd.h>
144
145 #include <sys/fs/zfs.h>
146 #include <sys/param.h>
147 #include <sys/mount.h>
148
149 #include <libzfs.h>
150
151 #undef verify   /* both libzfs.h and zfs_context.h want to define this */
152
153 #include "zinject.h"
154
155 libzfs_handle_t *g_zfs;
156 int zfs_fd;
157
158 #ifndef ECKSUM
159 #define ECKSUM  EBADE
160 #endif
161
162 static const char *errtable[TYPE_INVAL] = {
163         "data",
164         "dnode",
165         "mos",
166         "mosdir",
167         "metaslab",
168         "config",
169         "bpobj",
170         "spacemap",
171         "errlog",
172         "uber",
173         "nvlist",
174         "pad1",
175         "pad2"
176 };
177
178 static err_type_t
179 name_to_type(const char *arg)
180 {
181         int i;
182         for (i = 0; i < TYPE_INVAL; i++)
183                 if (strcmp(errtable[i], arg) == 0)
184                         return (i);
185
186         return (TYPE_INVAL);
187 }
188
189 static const char *
190 type_to_name(uint64_t type)
191 {
192         switch (type) {
193         case DMU_OT_OBJECT_DIRECTORY:
194                 return ("mosdir");
195         case DMU_OT_OBJECT_ARRAY:
196                 return ("metaslab");
197         case DMU_OT_PACKED_NVLIST:
198                 return ("config");
199         case DMU_OT_BPOBJ:
200                 return ("bpobj");
201         case DMU_OT_SPACE_MAP:
202                 return ("spacemap");
203         case DMU_OT_ERROR_LOG:
204                 return ("errlog");
205         default:
206                 return ("-");
207         }
208 }
209
210
211 /*
212  * Print usage message.
213  */
214 void
215 usage(void)
216 {
217         (void) printf(
218             "usage:\n"
219             "\n"
220             "\tzinject\n"
221             "\n"
222             "\t\tList all active injection records.\n"
223             "\n"
224             "\tzinject -c <id|all>\n"
225             "\n"
226             "\t\tClear the particular record (if given a numeric ID), or\n"
227             "\t\tall records if 'all' is specificed.\n"
228             "\n"
229             "\tzinject -p <function name> pool\n"
230             "\t\tInject a panic fault at the specified function. Only \n"
231             "\t\tfunctions which call spa_vdev_config_exit(), or \n"
232             "\t\tspa_vdev_exit() will trigger a panic.\n"
233             "\n"
234             "\tzinject -d device [-e errno] [-L <nvlist|uber|pad1|pad2>] [-F]\n"
235             "\t    [-T <read|write|free|claim|all> pool\n"
236             "\t\tInject a fault into a particular device or the device's\n"
237             "\t\tlabel.  Label injection can either be 'nvlist', 'uber',\n "
238             "\t\t'pad1', or 'pad2'.\n"
239             "\t\t'errno' can be 'nxio' (the default), 'io', or 'dtl'.\n"
240             "\n"
241             "\tzinject -d device -A <degrade|fault> pool\n"
242             "\t\tPerform a specific action on a particular device\n"
243             "\n"
244             "\tzinject -I [-s <seconds> | -g <txgs>] pool\n"
245             "\t\tCause the pool to stop writing blocks yet not\n"
246             "\t\treport errors for a duration.  Simulates buggy hardware\n"
247             "\t\tthat fails to honor cache flush requests.\n"
248             "\t\tDefault duration is 30 seconds.  The machine is panicked\n"
249             "\t\tat the end of the duration.\n"
250             "\n"
251             "\tzinject -b objset:object:level:blkid pool\n"
252             "\n"
253             "\t\tInject an error into pool 'pool' with the numeric bookmark\n"
254             "\t\tspecified by the remaining tuple.  Each number is in\n"
255             "\t\thexidecimal, and only one block can be specified.\n"
256             "\n"
257             "\tzinject [-q] <-t type> [-e errno] [-l level] [-r range]\n"
258             "\t    [-a] [-m] [-u] [-f freq] <object>\n"
259             "\n"
260             "\t\tInject an error into the object specified by the '-t' option\n"
261             "\t\tand the object descriptor.  The 'object' parameter is\n"
262             "\t\tinterperted depending on the '-t' option.\n"
263             "\n"
264             "\t\t-q\tQuiet mode.  Only print out the handler number added.\n"
265             "\t\t-e\tInject a specific error.  Must be either 'io' or\n"
266             "\t\t\t'checksum'.  Default is 'io'.\n"
267             "\t\t-l\tInject error at a particular block level. Default is "
268             "0.\n"
269             "\t\t-m\tAutomatically remount underlying filesystem.\n"
270             "\t\t-r\tInject error over a particular logical range of an\n"
271             "\t\t\tobject.  Will be translated to the appropriate blkid\n"
272             "\t\t\trange according to the object's properties.\n"
273             "\t\t-a\tFlush the ARC cache.  Can be specified without any\n"
274             "\t\t\tassociated object.\n"
275             "\t\t-u\tUnload the associated pool.  Can be specified with only\n"
276             "\t\t\ta pool object.\n"
277             "\t\t-f\tOnly inject errors a fraction of the time.  Expressed as\n"
278             "\t\t\ta percentage between 1 and 100.\n"
279             "\n"
280             "\t-t data\t\tInject an error into the plain file contents of a\n"
281             "\t\t\tfile.  The object must be specified as a complete path\n"
282             "\t\t\tto a file on a ZFS filesystem.\n"
283             "\n"
284             "\t-t dnode\tInject an error into the metadnode in the block\n"
285             "\t\t\tcorresponding to the dnode for a file or directory.  The\n"
286             "\t\t\t'-r' option is incompatible with this mode.  The object\n"
287             "\t\t\tis specified as a complete path to a file or directory\n"
288             "\t\t\ton a ZFS filesystem.\n"
289             "\n"
290             "\t-t <mos>\tInject errors into the MOS for objects of the given\n"
291             "\t\t\ttype.  Valid types are: mos, mosdir, config, bpobj,\n"
292             "\t\t\tspacemap, metaslab, errlog.  The only valid <object> is\n"
293             "\t\t\tthe poolname.\n");
294 }
295
296 static int
297 iter_handlers(int (*func)(int, const char *, zinject_record_t *, void *),
298     void *data)
299 {
300         zfs_cmd_t zc = { 0 };
301         int ret;
302
303         while (ioctl(zfs_fd, ZFS_IOC_INJECT_LIST_NEXT, &zc) == 0)
304                 if ((ret = func((int)zc.zc_guid, zc.zc_name,
305                     &zc.zc_inject_record, data)) != 0)
306                         return (ret);
307
308         if (errno != ENOENT) {
309                 (void) fprintf(stderr, "Unable to list handlers: %s\n",
310                     strerror(errno));
311                 return (-1);
312         }
313
314         return (0);
315 }
316
317 static int
318 print_data_handler(int id, const char *pool, zinject_record_t *record,
319     void *data)
320 {
321         int *count = data;
322
323         if (record->zi_guid != 0 || record->zi_func[0] != '\0')
324                 return (0);
325
326         if (*count == 0) {
327                 (void) printf("%3s  %-15s  %-6s  %-6s  %-8s  %3s  %-15s\n",
328                     "ID", "POOL", "OBJSET", "OBJECT", "TYPE", "LVL",  "RANGE");
329                 (void) printf("---  ---------------  ------  "
330                     "------  --------  ---  ---------------\n");
331         }
332
333         *count += 1;
334
335         (void) printf("%3d  %-15s  %-6llu  %-6llu  %-8s  %3d  ", id, pool,
336             (u_longlong_t)record->zi_objset, (u_longlong_t)record->zi_object,
337             type_to_name(record->zi_type), record->zi_level);
338
339         if (record->zi_start == 0 &&
340             record->zi_end == -1ULL)
341                 (void) printf("all\n");
342         else
343                 (void) printf("[%llu, %llu]\n", (u_longlong_t)record->zi_start,
344                     (u_longlong_t)record->zi_end);
345
346         return (0);
347 }
348
349 static int
350 print_device_handler(int id, const char *pool, zinject_record_t *record,
351     void *data)
352 {
353         int *count = data;
354
355         if (record->zi_guid == 0 || record->zi_func[0] != '\0')
356                 return (0);
357
358         if (*count == 0) {
359                 (void) printf("%3s  %-15s  %s\n", "ID", "POOL", "GUID");
360                 (void) printf("---  ---------------  ----------------\n");
361         }
362
363         *count += 1;
364
365         (void) printf("%3d  %-15s  %llx\n", id, pool,
366             (u_longlong_t)record->zi_guid);
367
368         return (0);
369 }
370
371 static int
372 print_panic_handler(int id, const char *pool, zinject_record_t *record,
373     void *data)
374 {
375         int *count = data;
376
377         if (record->zi_func[0] == '\0')
378                 return (0);
379
380         if (*count == 0) {
381                 (void) printf("%3s  %-15s  %s\n", "ID", "POOL", "FUNCTION");
382                 (void) printf("---  ---------------  ----------------\n");
383         }
384
385         *count += 1;
386
387         (void) printf("%3d  %-15s  %s\n", id, pool, record->zi_func);
388
389         return (0);
390 }
391
392 /*
393  * Print all registered error handlers.  Returns the number of handlers
394  * registered.
395  */
396 static int
397 print_all_handlers(void)
398 {
399         int count = 0, total = 0;
400
401         (void) iter_handlers(print_device_handler, &count);
402         if (count > 0) {
403                 total += count;
404                 (void) printf("\n");
405                 count = 0;
406         }
407
408         (void) iter_handlers(print_data_handler, &count);
409         if (count > 0) {
410                 total += count;
411                 (void) printf("\n");
412                 count = 0;
413         }
414
415         (void) iter_handlers(print_panic_handler, &count);
416
417         return (count + total);
418 }
419
420 /* ARGSUSED */
421 static int
422 cancel_one_handler(int id, const char *pool, zinject_record_t *record,
423     void *data)
424 {
425         zfs_cmd_t zc = { 0 };
426
427         zc.zc_guid = (uint64_t)id;
428
429         if (ioctl(zfs_fd, ZFS_IOC_CLEAR_FAULT, &zc) != 0) {
430                 (void) fprintf(stderr, "failed to remove handler %d: %s\n",
431                     id, strerror(errno));
432                 return (1);
433         }
434
435         return (0);
436 }
437
438 /*
439  * Remove all fault injection handlers.
440  */
441 static int
442 cancel_all_handlers(void)
443 {
444         int ret = iter_handlers(cancel_one_handler, NULL);
445
446         if (ret == 0)
447                 (void) printf("removed all registered handlers\n");
448
449         return (ret);
450 }
451
452 /*
453  * Remove a specific fault injection handler.
454  */
455 static int
456 cancel_handler(int id)
457 {
458         zfs_cmd_t zc = { 0 };
459
460         zc.zc_guid = (uint64_t)id;
461
462         if (ioctl(zfs_fd, ZFS_IOC_CLEAR_FAULT, &zc) != 0) {
463                 (void) fprintf(stderr, "failed to remove handler %d: %s\n",
464                     id, strerror(errno));
465                 return (1);
466         }
467
468         (void) printf("removed handler %d\n", id);
469
470         return (0);
471 }
472
473 /*
474  * Register a new fault injection handler.
475  */
476 static int
477 register_handler(const char *pool, int flags, zinject_record_t *record,
478     int quiet)
479 {
480         zfs_cmd_t zc = { 0 };
481
482         (void) strcpy(zc.zc_name, pool);
483         zc.zc_inject_record = *record;
484         zc.zc_guid = flags;
485
486         if (ioctl(zfs_fd, ZFS_IOC_INJECT_FAULT, &zc) != 0) {
487                 (void) fprintf(stderr, "failed to add handler: %s\n",
488                     strerror(errno));
489                 return (1);
490         }
491
492         if (flags & ZINJECT_NULL)
493                 return (0);
494
495         if (quiet) {
496                 (void) printf("%llu\n", (u_longlong_t)zc.zc_guid);
497         } else {
498                 (void) printf("Added handler %llu with the following "
499                     "properties:\n", (u_longlong_t)zc.zc_guid);
500                 (void) printf("  pool: %s\n", pool);
501                 if (record->zi_guid) {
502                         (void) printf("  vdev: %llx\n",
503                             (u_longlong_t)record->zi_guid);
504                 } else if (record->zi_func[0] != '\0') {
505                         (void) printf("  panic function: %s\n",
506                             record->zi_func);
507                 } else if (record->zi_duration > 0) {
508                         (void) printf(" time: %lld seconds\n",
509                             (u_longlong_t)record->zi_duration);
510                 } else if (record->zi_duration < 0) {
511                         (void) printf(" txgs: %lld \n",
512                             (u_longlong_t)-record->zi_duration);
513                 } else {
514                         (void) printf("objset: %llu\n",
515                             (u_longlong_t)record->zi_objset);
516                         (void) printf("object: %llu\n",
517                             (u_longlong_t)record->zi_object);
518                         (void) printf("  type: %llu\n",
519                             (u_longlong_t)record->zi_type);
520                         (void) printf(" level: %d\n", record->zi_level);
521                         if (record->zi_start == 0 &&
522                             record->zi_end == -1ULL)
523                                 (void) printf(" range: all\n");
524                         else
525                                 (void) printf(" range: [%llu, %llu)\n",
526                                     (u_longlong_t)record->zi_start,
527                                     (u_longlong_t)record->zi_end);
528                 }
529         }
530
531         return (0);
532 }
533
534 int
535 perform_action(const char *pool, zinject_record_t *record, int cmd)
536 {
537         zfs_cmd_t zc = { 0 };
538
539         ASSERT(cmd == VDEV_STATE_DEGRADED || cmd == VDEV_STATE_FAULTED);
540         (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
541         zc.zc_guid = record->zi_guid;
542         zc.zc_cookie = cmd;
543
544         if (ioctl(zfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
545                 return (0);
546
547         return (1);
548 }
549
550 int
551 main(int argc, char **argv)
552 {
553         int c;
554         char *range = NULL;
555         char *cancel = NULL;
556         char *end;
557         char *raw = NULL;
558         char *device = NULL;
559         int level = 0;
560         int quiet = 0;
561         int error = 0;
562         int domount = 0;
563         int io_type = ZIO_TYPES;
564         int action = VDEV_STATE_UNKNOWN;
565         err_type_t type = TYPE_INVAL;
566         err_type_t label = TYPE_INVAL;
567         zinject_record_t record = { 0 };
568         char pool[MAXNAMELEN];
569         char dataset[MAXNAMELEN];
570         zfs_handle_t *zhp;
571         int nowrites = 0;
572         int dur_txg = 0;
573         int dur_secs = 0;
574         int ret;
575         int flags = 0;
576
577         if ((g_zfs = libzfs_init()) == NULL) {
578                 (void) fprintf(stderr, "internal error: failed to "
579                     "initialize ZFS library\n");
580                 return (1);
581         }
582
583         libzfs_print_on_error(g_zfs, B_TRUE);
584
585         if ((zfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
586                 (void) fprintf(stderr, "failed to open ZFS device\n");
587                 return (1);
588         }
589
590         if (argc == 1) {
591                 /*
592                  * No arguments.  Print the available handlers.  If there are no
593                  * available handlers, direct the user to '-h' for help
594                  * information.
595                  */
596                 if (print_all_handlers() == 0) {
597                         (void) printf("No handlers registered.\n");
598                         (void) printf("Run 'zinject -h' for usage "
599                             "information.\n");
600                 }
601
602                 return (0);
603         }
604
605         while ((c = getopt(argc, argv,
606             ":aA:b:d:f:Fg:qhIc:t:T:l:mr:s:e:uL:p:")) != -1) {
607                 switch (c) {
608                 case 'a':
609                         flags |= ZINJECT_FLUSH_ARC;
610                         break;
611                 case 'A':
612                         if (strcasecmp(optarg, "degrade") == 0) {
613                                 action = VDEV_STATE_DEGRADED;
614                         } else if (strcasecmp(optarg, "fault") == 0) {
615                                 action = VDEV_STATE_FAULTED;
616                         } else {
617                                 (void) fprintf(stderr, "invalid action '%s': "
618                                     "must be 'degrade' or 'fault'\n", optarg);
619                                 usage();
620                                 return (1);
621                         }
622                         break;
623                 case 'b':
624                         raw = optarg;
625                         break;
626                 case 'c':
627                         cancel = optarg;
628                         break;
629                 case 'd':
630                         device = optarg;
631                         break;
632                 case 'e':
633                         if (strcasecmp(optarg, "io") == 0) {
634                                 error = EIO;
635                         } else if (strcasecmp(optarg, "checksum") == 0) {
636                                 error = ECKSUM;
637                         } else if (strcasecmp(optarg, "nxio") == 0) {
638                                 error = ENXIO;
639                         } else if (strcasecmp(optarg, "dtl") == 0) {
640                                 error = ECHILD;
641                         } else {
642                                 (void) fprintf(stderr, "invalid error type "
643                                     "'%s': must be 'io', 'checksum' or "
644                                     "'nxio'\n", optarg);
645                                 usage();
646                                 return (1);
647                         }
648                         break;
649                 case 'f':
650                         record.zi_freq = atoi(optarg);
651                         if (record.zi_freq < 1 || record.zi_freq > 100) {
652                                 (void) fprintf(stderr, "frequency range must "
653                                     "be in the range (0, 100]\n");
654                                 return (1);
655                         }
656                         break;
657                 case 'F':
658                         record.zi_failfast = B_TRUE;
659                         break;
660                 case 'g':
661                         dur_txg = 1;
662                         record.zi_duration = (int)strtol(optarg, &end, 10);
663                         if (record.zi_duration <= 0 || *end != '\0') {
664                                 (void) fprintf(stderr, "invalid duration '%s': "
665                                     "must be a positive integer\n", optarg);
666                                 usage();
667                                 return (1);
668                         }
669                         /* store duration of txgs as its negative */
670                         record.zi_duration *= -1;
671                         break;
672                 case 'h':
673                         usage();
674                         return (0);
675                 case 'I':
676                         /* default duration, if one hasn't yet been defined */
677                         nowrites = 1;
678                         if (dur_secs == 0 && dur_txg == 0)
679                                 record.zi_duration = 30;
680                         break;
681                 case 'l':
682                         level = (int)strtol(optarg, &end, 10);
683                         if (*end != '\0') {
684                                 (void) fprintf(stderr, "invalid level '%s': "
685                                     "must be an integer\n", optarg);
686                                 usage();
687                                 return (1);
688                         }
689                         break;
690                 case 'm':
691                         domount = 1;
692                         break;
693                 case 'p':
694                         (void) strlcpy(record.zi_func, optarg,
695                             sizeof (record.zi_func));
696                         break;
697                 case 'q':
698                         quiet = 1;
699                         break;
700                 case 'r':
701                         range = optarg;
702                         break;
703                 case 's':
704                         dur_secs = 1;
705                         record.zi_duration = (int)strtol(optarg, &end, 10);
706                         if (record.zi_duration <= 0 || *end != '\0') {
707                                 (void) fprintf(stderr, "invalid duration '%s': "
708                                     "must be a positive integer\n", optarg);
709                                 usage();
710                                 return (1);
711                         }
712                         break;
713                 case 'T':
714                         if (strcasecmp(optarg, "read") == 0) {
715                                 io_type = ZIO_TYPE_READ;
716                         } else if (strcasecmp(optarg, "write") == 0) {
717                                 io_type = ZIO_TYPE_WRITE;
718                         } else if (strcasecmp(optarg, "free") == 0) {
719                                 io_type = ZIO_TYPE_FREE;
720                         } else if (strcasecmp(optarg, "claim") == 0) {
721                                 io_type = ZIO_TYPE_CLAIM;
722                         } else if (strcasecmp(optarg, "all") == 0) {
723                                 io_type = ZIO_TYPES;
724                         } else {
725                                 (void) fprintf(stderr, "invalid I/O type "
726                                     "'%s': must be 'read', 'write', 'free', "
727                                     "'claim' or 'all'\n", optarg);
728                                 usage();
729                                 return (1);
730                         }
731                         break;
732                 case 't':
733                         if ((type = name_to_type(optarg)) == TYPE_INVAL &&
734                             !MOS_TYPE(type)) {
735                                 (void) fprintf(stderr, "invalid type '%s'\n",
736                                     optarg);
737                                 usage();
738                                 return (1);
739                         }
740                         break;
741                 case 'u':
742                         flags |= ZINJECT_UNLOAD_SPA;
743                         break;
744                 case 'L':
745                         if ((label = name_to_type(optarg)) == TYPE_INVAL &&
746                             !LABEL_TYPE(type)) {
747                                 (void) fprintf(stderr, "invalid label type "
748                                     "'%s'\n", optarg);
749                                 usage();
750                                 return (1);
751                         }
752                         break;
753                 case ':':
754                         (void) fprintf(stderr, "option -%c requires an "
755                             "operand\n", optopt);
756                         usage();
757                         return (1);
758                 case '?':
759                         (void) fprintf(stderr, "invalid option '%c'\n",
760                             optopt);
761                         usage();
762                         return (2);
763                 }
764         }
765
766         argc -= optind;
767         argv += optind;
768
769         if (cancel != NULL) {
770                 /*
771                  * '-c' is invalid with any other options.
772                  */
773                 if (raw != NULL || range != NULL || type != TYPE_INVAL ||
774                     level != 0 || record.zi_func[0] != '\0' ||
775                     record.zi_duration != 0) {
776                         (void) fprintf(stderr, "cancel (-c) incompatible with "
777                             "any other options\n");
778                         usage();
779                         return (2);
780                 }
781                 if (argc != 0) {
782                         (void) fprintf(stderr, "extraneous argument to '-c'\n");
783                         usage();
784                         return (2);
785                 }
786
787                 if (strcmp(cancel, "all") == 0) {
788                         return (cancel_all_handlers());
789                 } else {
790                         int id = (int)strtol(cancel, &end, 10);
791                         if (*end != '\0') {
792                                 (void) fprintf(stderr, "invalid handle id '%s':"
793                                     " must be an integer or 'all'\n", cancel);
794                                 usage();
795                                 return (1);
796                         }
797                         return (cancel_handler(id));
798                 }
799         }
800
801         if (device != NULL) {
802                 /*
803                  * Device (-d) injection uses a completely different mechanism
804                  * for doing injection, so handle it separately here.
805                  */
806                 if (raw != NULL || range != NULL || type != TYPE_INVAL ||
807                     level != 0 || record.zi_func[0] != '\0' ||
808                     record.zi_duration != 0) {
809                         (void) fprintf(stderr, "device (-d) incompatible with "
810                             "data error injection\n");
811                         usage();
812                         return (2);
813                 }
814
815                 if (argc != 1) {
816                         (void) fprintf(stderr, "device (-d) injection requires "
817                             "a single pool name\n");
818                         usage();
819                         return (2);
820                 }
821
822                 (void) strcpy(pool, argv[0]);
823                 dataset[0] = '\0';
824
825                 if (error == ECKSUM) {
826                         (void) fprintf(stderr, "device error type must be "
827                             "'io' or 'nxio'\n");
828                         return (1);
829                 }
830
831                 record.zi_iotype = io_type;
832                 if (translate_device(pool, device, label, &record) != 0)
833                         return (1);
834                 if (!error)
835                         error = ENXIO;
836
837                 if (action != VDEV_STATE_UNKNOWN)
838                         return (perform_action(pool, &record, action));
839
840         } else if (raw != NULL) {
841                 if (range != NULL || type != TYPE_INVAL || level != 0 ||
842                     record.zi_func[0] != '\0' || record.zi_duration != 0) {
843                         (void) fprintf(stderr, "raw (-b) format with "
844                             "any other options\n");
845                         usage();
846                         return (2);
847                 }
848
849                 if (argc != 1) {
850                         (void) fprintf(stderr, "raw (-b) format expects a "
851                             "single pool name\n");
852                         usage();
853                         return (2);
854                 }
855
856                 (void) strcpy(pool, argv[0]);
857                 dataset[0] = '\0';
858
859                 if (error == ENXIO) {
860                         (void) fprintf(stderr, "data error type must be "
861                             "'checksum' or 'io'\n");
862                         return (1);
863                 }
864
865                 if (translate_raw(raw, &record) != 0)
866                         return (1);
867                 if (!error)
868                         error = EIO;
869         } else if (record.zi_func[0] != '\0') {
870                 if (raw != NULL || range != NULL || type != TYPE_INVAL ||
871                     level != 0 || device != NULL || record.zi_duration != 0) {
872                         (void) fprintf(stderr, "panic (-p) incompatible with "
873                             "other options\n");
874                         usage();
875                         return (2);
876                 }
877
878                 if (argc < 1 || argc > 2) {
879                         (void) fprintf(stderr, "panic (-p) injection requires "
880                             "a single pool name and an optional id\n");
881                         usage();
882                         return (2);
883                 }
884
885                 (void) strcpy(pool, argv[0]);
886                 if (argv[1] != NULL)
887                         record.zi_type = atoi(argv[1]);
888                 dataset[0] = '\0';
889         } else if (record.zi_duration != 0) {
890                 if (nowrites == 0) {
891                         (void) fprintf(stderr, "-s or -g meaningless "
892                             "without -I (ignore writes)\n");
893                         usage();
894                         return (2);
895                 } else if (dur_secs && dur_txg) {
896                         (void) fprintf(stderr, "choose a duration either "
897                             "in seconds (-s) or a number of txgs (-g) "
898                             "but not both\n");
899                         usage();
900                         return (2);
901                 } else if (argc != 1) {
902                         (void) fprintf(stderr, "ignore writes (-I) "
903                             "injection requires a single pool name\n");
904                         usage();
905                         return (2);
906                 }
907
908                 (void) strcpy(pool, argv[0]);
909                 dataset[0] = '\0';
910         } else if (type == TYPE_INVAL) {
911                 if (flags == 0) {
912                         (void) fprintf(stderr, "at least one of '-b', '-d', "
913                             "'-t', '-a', '-p', '-I' or '-u' "
914                             "must be specified\n");
915                         usage();
916                         return (2);
917                 }
918
919                 if (argc == 1 && (flags & ZINJECT_UNLOAD_SPA)) {
920                         (void) strcpy(pool, argv[0]);
921                         dataset[0] = '\0';
922                 } else if (argc != 0) {
923                         (void) fprintf(stderr, "extraneous argument for "
924                             "'-f'\n");
925                         usage();
926                         return (2);
927                 }
928
929                 flags |= ZINJECT_NULL;
930         } else {
931                 if (argc != 1) {
932                         (void) fprintf(stderr, "missing object\n");
933                         usage();
934                         return (2);
935                 }
936
937                 if (error == ENXIO) {
938                         (void) fprintf(stderr, "data error type must be "
939                             "'checksum' or 'io'\n");
940                         return (1);
941                 }
942
943                 if (translate_record(type, argv[0], range, level, &record, pool,
944                     dataset) != 0)
945                         return (1);
946                 if (!error)
947                         error = EIO;
948         }
949
950         /*
951          * If this is pool-wide metadata, unmount everything.  The ioctl() will
952          * unload the pool, so that we trigger spa-wide reopen of metadata next
953          * time we access the pool.
954          */
955         if (dataset[0] != '\0' && domount) {
956                 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_DATASET)) == NULL)
957                         return (1);
958
959                 if (zfs_unmount(zhp, NULL, 0) != 0)
960                         return (1);
961         }
962
963         record.zi_error = error;
964
965         ret = register_handler(pool, flags, &record, quiet);
966
967         if (dataset[0] != '\0' && domount)
968                 ret = (zfs_mount(zhp, NULL, 0) != 0);
969
970         libzfs_fini(g_zfs);
971
972         return (ret);
973 }