]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - cddl/contrib/opensolaris/cmd/zinject/zinject.c
Copy stable/8 to releng/8.2 in preparation for FreeBSD-8.2 release.
[FreeBSD/releng/8.2.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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25
26 /*
27  * ZFS Fault Injector
28  *
29  * This userland component takes a set of options and uses libzpool to translate
30  * from a user-visible object type and name to an internal representation.
31  * There are two basic types of faults: device faults and data faults.
32  *
33  *
34  * DEVICE FAULTS
35  *
36  * Errors can be injected into a particular vdev using the '-d' option.  This
37  * option takes a path or vdev GUID to uniquely identify the device within a
38  * pool.  There are two types of errors that can be injected, EIO and ENXIO,
39  * that can be controlled through the '-e' option.  The default is ENXIO.  For
40  * EIO failures, any attempt to read data from the device will return EIO, but
41  * subsequent attempt to reopen the device will succeed.  For ENXIO failures,
42  * any attempt to read from the device will return EIO, but any attempt to
43  * reopen the device will also return ENXIO.
44  * For label faults, the -L option must be specified. This allows faults
45  * to be injected into either the nvlist or uberblock region of all the labels
46  * for the specified device.
47  *
48  * This form of the command looks like:
49  *
50  *      zinject -d device [-e errno] [-L <uber | nvlist>] pool
51  *
52  *
53  * DATA FAULTS
54  *
55  * We begin with a tuple of the form:
56  *
57  *      <type,level,range,object>
58  *
59  *      type    A string describing the type of data to target.  Each type
60  *              implicitly describes how to interpret 'object'. Currently,
61  *              the following values are supported:
62  *
63  *              data            User data for a file
64  *              dnode           Dnode for a file or directory
65  *
66  *              The following MOS objects are special.  Instead of injecting
67  *              errors on a particular object or blkid, we inject errors across
68  *              all objects of the given type.
69  *
70  *              mos             Any data in the MOS
71  *              mosdir          object directory
72  *              config          pool configuration
73  *              bplist          blkptr list
74  *              spacemap        spacemap
75  *              metaslab        metaslab
76  *              errlog          persistent error log
77  *
78  *      level   Object level.  Defaults to '0', not applicable to all types.  If
79  *              a range is given, this corresponds to the indirect block
80  *              corresponding to the specific range.
81  *
82  *      range   A numerical range [start,end) within the object.  Defaults to
83  *              the full size of the file.
84  *
85  *      object  A string describing the logical location of the object.  For
86  *              files and directories (currently the only supported types),
87  *              this is the path of the object on disk.
88  *
89  * This is translated, via libzpool, into the following internal representation:
90  *
91  *      <type,objset,object,level,range>
92  *
93  * These types should be self-explanatory.  This tuple is then passed to the
94  * kernel via a special ioctl() to initiate fault injection for the given
95  * object.  Note that 'type' is not strictly necessary for fault injection, but
96  * is used when translating existing faults into a human-readable string.
97  *
98  *
99  * The command itself takes one of the forms:
100  *
101  *      zinject
102  *      zinject <-a | -u pool>
103  *      zinject -c <id|all>
104  *      zinject [-q] <-t type> [-f freq] [-u] [-a] [-m] [-e errno] [-l level]
105  *          [-r range] <object>
106  *      zinject [-f freq] [-a] [-m] [-u] -b objset:object:level:start:end pool
107  *
108  * With no arguments, the command prints all currently registered injection
109  * handlers, with their numeric identifiers.
110  *
111  * The '-c' option will clear the given handler, or all handlers if 'all' is
112  * specified.
113  *
114  * The '-e' option takes a string describing the errno to simulate.  This must
115  * be either 'io' or 'checksum'.  In most cases this will result in the same
116  * behavior, but RAID-Z will produce a different set of ereports for this
117  * situation.
118  *
119  * The '-a', '-u', and '-m' flags toggle internal flush behavior.  If '-a' is
120  * specified, then the ARC cache is flushed appropriately.  If '-u' is
121  * specified, then the underlying SPA is unloaded.  Either of these flags can be
122  * specified independently of any other handlers.  The '-m' flag automatically
123  * does an unmount and remount of the underlying dataset to aid in flushing the
124  * cache.
125  *
126  * The '-f' flag controls the frequency of errors injected, expressed as a
127  * integer percentage between 1 and 100.  The default is 100.
128  *
129  * The this form is responsible for actually injecting the handler into the
130  * framework.  It takes the arguments described above, translates them to the
131  * internal tuple using libzpool, and then issues an ioctl() to register the
132  * handler.
133  *
134  * The final form can target a specific bookmark, regardless of whether a
135  * human-readable interface has been designed.  It allows developers to specify
136  * a particular block by number.
137  */
138
139 #include <errno.h>
140 #include <fcntl.h>
141 #include <stdio.h>
142 #include <stdlib.h>
143 #include <strings.h>
144 #include <unistd.h>
145
146 #include <sys/fs/zfs.h>
147 #include <sys/param.h>
148 #include <sys/mount.h>
149
150 #include <libzfs.h>
151
152 #undef verify   /* both libzfs.h and zfs_context.h want to define this */
153
154 #include "zinject.h"
155
156 libzfs_handle_t *g_zfs;
157 int zfs_fd;
158
159 #ifndef ECKSUM
160 #define ECKSUM  EBADE
161 #endif
162
163 static const char *errtable[TYPE_INVAL] = {
164         "data",
165         "dnode",
166         "mos",
167         "mosdir",
168         "metaslab",
169         "config",
170         "bplist",
171         "spacemap",
172         "errlog",
173         "uber",
174         "nvlist"
175 };
176
177 static err_type_t
178 name_to_type(const char *arg)
179 {
180         int i;
181         for (i = 0; i < TYPE_INVAL; i++)
182                 if (strcmp(errtable[i], arg) == 0)
183                         return (i);
184
185         return (TYPE_INVAL);
186 }
187
188 static const char *
189 type_to_name(uint64_t type)
190 {
191         switch (type) {
192         case DMU_OT_OBJECT_DIRECTORY:
193                 return ("mosdir");
194         case DMU_OT_OBJECT_ARRAY:
195                 return ("metaslab");
196         case DMU_OT_PACKED_NVLIST:
197                 return ("config");
198         case DMU_OT_BPLIST:
199                 return ("bplist");
200         case DMU_OT_SPACE_MAP:
201                 return ("spacemap");
202         case DMU_OT_ERROR_LOG:
203                 return ("errlog");
204         default:
205                 return ("-");
206         }
207 }
208
209
210 /*
211  * Print usage message.
212  */
213 void
214 usage(void)
215 {
216         (void) printf(
217             "usage:\n"
218             "\n"
219             "\tzinject\n"
220             "\n"
221             "\t\tList all active injection records.\n"
222             "\n"
223             "\tzinject -c <id|all>\n"
224             "\n"
225             "\t\tClear the particular record (if given a numeric ID), or\n"
226             "\t\tall records if 'all' is specificed.\n"
227             "\n"
228             "\tzinject -d device [-e errno] [-L <nvlist|uber>] [-F] pool\n"
229             "\t\tInject a fault into a particular device or the device's\n"
230             "\t\tlabel.  Label injection can either be 'nvlist' or 'uber'.\n"
231             "\t\t'errno' can either be 'nxio' (the default) or 'io'.\n"
232             "\n"
233             "\tzinject -b objset:object:level:blkid pool\n"
234             "\n"
235             "\t\tInject an error into pool 'pool' with the numeric bookmark\n"
236             "\t\tspecified by the remaining tuple.  Each number is in\n"
237             "\t\thexidecimal, and only one block can be specified.\n"
238             "\n"
239             "\tzinject [-q] <-t type> [-e errno] [-l level] [-r range]\n"
240             "\t    [-a] [-m] [-u] [-f freq] <object>\n"
241             "\n"
242             "\t\tInject an error into the object specified by the '-t' option\n"
243             "\t\tand the object descriptor.  The 'object' parameter is\n"
244             "\t\tinterperted depending on the '-t' option.\n"
245             "\n"
246             "\t\t-q\tQuiet mode.  Only print out the handler number added.\n"
247             "\t\t-e\tInject a specific error.  Must be either 'io' or\n"
248             "\t\t\t'checksum'.  Default is 'io'.\n"
249             "\t\t-l\tInject error at a particular block level. Default is "
250             "0.\n"
251             "\t\t-m\tAutomatically remount underlying filesystem.\n"
252             "\t\t-r\tInject error over a particular logical range of an\n"
253             "\t\t\tobject.  Will be translated to the appropriate blkid\n"
254             "\t\t\trange according to the object's properties.\n"
255             "\t\t-a\tFlush the ARC cache.  Can be specified without any\n"
256             "\t\t\tassociated object.\n"
257             "\t\t-u\tUnload the associated pool.  Can be specified with only\n"
258             "\t\t\ta pool object.\n"
259             "\t\t-f\tOnly inject errors a fraction of the time.  Expressed as\n"
260             "\t\t\ta percentage between 1 and 100.\n"
261             "\n"
262             "\t-t data\t\tInject an error into the plain file contents of a\n"
263             "\t\t\tfile.  The object must be specified as a complete path\n"
264             "\t\t\tto a file on a ZFS filesystem.\n"
265             "\n"
266             "\t-t dnode\tInject an error into the metadnode in the block\n"
267             "\t\t\tcorresponding to the dnode for a file or directory.  The\n"
268             "\t\t\t'-r' option is incompatible with this mode.  The object\n"
269             "\t\t\tis specified as a complete path to a file or directory\n"
270             "\t\t\ton a ZFS filesystem.\n"
271             "\n"
272             "\t-t <mos>\tInject errors into the MOS for objects of the given\n"
273             "\t\t\ttype.  Valid types are: mos, mosdir, config, bplist,\n"
274             "\t\t\tspacemap, metaslab, errlog.  The only valid <object> is\n"
275             "\t\t\tthe poolname.\n");
276 }
277
278 static int
279 iter_handlers(int (*func)(int, const char *, zinject_record_t *, void *),
280     void *data)
281 {
282         zfs_cmd_t zc;
283         int ret;
284
285         zc.zc_guid = 0;
286
287         while (ioctl(zfs_fd, ZFS_IOC_INJECT_LIST_NEXT, &zc) == 0)
288                 if ((ret = func((int)zc.zc_guid, zc.zc_name,
289                     &zc.zc_inject_record, data)) != 0)
290                         return (ret);
291
292         return (0);
293 }
294
295 static int
296 print_data_handler(int id, const char *pool, zinject_record_t *record,
297     void *data)
298 {
299         int *count = data;
300
301         if (record->zi_guid != 0)
302                 return (0);
303
304         if (*count == 0) {
305                 (void) printf("%3s  %-15s  %-6s  %-6s  %-8s  %3s  %-15s\n",
306                     "ID", "POOL", "OBJSET", "OBJECT", "TYPE", "LVL",  "RANGE");
307                 (void) printf("---  ---------------  ------  "
308                     "------  --------  ---  ---------------\n");
309         }
310
311         *count += 1;
312
313         (void) printf("%3d  %-15s  %-6llu  %-6llu  %-8s  %3d  ", id, pool,
314             (u_longlong_t)record->zi_objset, (u_longlong_t)record->zi_object,
315             type_to_name(record->zi_type), record->zi_level);
316
317         if (record->zi_start == 0 &&
318             record->zi_end == -1ULL)
319                 (void) printf("all\n");
320         else
321                 (void) printf("[%llu, %llu]\n", (u_longlong_t)record->zi_start,
322                     (u_longlong_t)record->zi_end);
323
324         return (0);
325 }
326
327 static int
328 print_device_handler(int id, const char *pool, zinject_record_t *record,
329     void *data)
330 {
331         int *count = data;
332
333         if (record->zi_guid == 0)
334                 return (0);
335
336         if (*count == 0) {
337                 (void) printf("%3s  %-15s  %s\n", "ID", "POOL", "GUID");
338                 (void) printf("---  ---------------  ----------------\n");
339         }
340
341         *count += 1;
342
343         (void) printf("%3d  %-15s  %llx\n", id, pool,
344             (u_longlong_t)record->zi_guid);
345
346         return (0);
347 }
348
349 /*
350  * Print all registered error handlers.  Returns the number of handlers
351  * registered.
352  */
353 static int
354 print_all_handlers(void)
355 {
356         int count = 0;
357
358         (void) iter_handlers(print_device_handler, &count);
359         (void) printf("\n");
360         count = 0;
361         (void) iter_handlers(print_data_handler, &count);
362
363         return (count);
364 }
365
366 /* ARGSUSED */
367 static int
368 cancel_one_handler(int id, const char *pool, zinject_record_t *record,
369     void *data)
370 {
371         zfs_cmd_t zc;
372
373         zc.zc_guid = (uint64_t)id;
374
375         if (ioctl(zfs_fd, ZFS_IOC_CLEAR_FAULT, &zc) != 0) {
376                 (void) fprintf(stderr, "failed to remove handler %d: %s\n",
377                     id, strerror(errno));
378                 return (1);
379         }
380
381         return (0);
382 }
383
384 /*
385  * Remove all fault injection handlers.
386  */
387 static int
388 cancel_all_handlers(void)
389 {
390         int ret = iter_handlers(cancel_one_handler, NULL);
391
392         (void) printf("removed all registered handlers\n");
393
394         return (ret);
395 }
396
397 /*
398  * Remove a specific fault injection handler.
399  */
400 static int
401 cancel_handler(int id)
402 {
403         zfs_cmd_t zc;
404
405         zc.zc_guid = (uint64_t)id;
406
407         if (ioctl(zfs_fd, ZFS_IOC_CLEAR_FAULT, &zc) != 0) {
408                 (void) fprintf(stderr, "failed to remove handler %d: %s\n",
409                     id, strerror(errno));
410                 return (1);
411         }
412
413         (void) printf("removed handler %d\n", id);
414
415         return (0);
416 }
417
418 /*
419  * Register a new fault injection handler.
420  */
421 static int
422 register_handler(const char *pool, int flags, zinject_record_t *record,
423     int quiet)
424 {
425         zfs_cmd_t zc;
426
427         (void) strcpy(zc.zc_name, pool);
428         zc.zc_inject_record = *record;
429         zc.zc_guid = flags;
430
431         if (ioctl(zfs_fd, ZFS_IOC_INJECT_FAULT, &zc) != 0) {
432                 (void) fprintf(stderr, "failed to add handler: %s\n",
433                     strerror(errno));
434                 return (1);
435         }
436
437         if (flags & ZINJECT_NULL)
438                 return (0);
439
440         if (quiet) {
441                 (void) printf("%llu\n", (u_longlong_t)zc.zc_guid);
442         } else {
443                 (void) printf("Added handler %llu with the following "
444                     "properties:\n", (u_longlong_t)zc.zc_guid);
445                 (void) printf("  pool: %s\n", pool);
446                 if (record->zi_guid) {
447                         (void) printf("  vdev: %llx\n",
448                             (u_longlong_t)record->zi_guid);
449                 } else {
450                         (void) printf("objset: %llu\n",
451                             (u_longlong_t)record->zi_objset);
452                         (void) printf("object: %llu\n",
453                             (u_longlong_t)record->zi_object);
454                         (void) printf("  type: %llu\n",
455                             (u_longlong_t)record->zi_type);
456                         (void) printf(" level: %d\n", record->zi_level);
457                         if (record->zi_start == 0 &&
458                             record->zi_end == -1ULL)
459                                 (void) printf(" range: all\n");
460                         else
461                                 (void) printf(" range: [%llu, %llu)\n",
462                                     (u_longlong_t)record->zi_start,
463                                     (u_longlong_t)record->zi_end);
464                 }
465         }
466
467         return (0);
468 }
469
470 int
471 main(int argc, char **argv)
472 {
473         int c;
474         char *range = NULL;
475         char *cancel = NULL;
476         char *end;
477         char *raw = NULL;
478         char *device = NULL;
479         int level = 0;
480         int quiet = 0;
481         int error = 0;
482         int domount = 0;
483         err_type_t type = TYPE_INVAL;
484         err_type_t label = TYPE_INVAL;
485         zinject_record_t record = { 0 };
486         char pool[MAXNAMELEN];
487         char dataset[MAXNAMELEN];
488         zfs_handle_t *zhp;
489         int ret;
490         int flags = 0;
491
492         if ((g_zfs = libzfs_init()) == NULL) {
493                 (void) fprintf(stderr, "internal error: failed to "
494                     "initialize ZFS library\n");
495                 return (1);
496         }
497
498         libzfs_print_on_error(g_zfs, B_TRUE);
499
500         if ((zfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
501                 (void) fprintf(stderr, "failed to open ZFS device\n");
502                 return (1);
503         }
504
505         if (argc == 1) {
506                 /*
507                  * No arguments.  Print the available handlers.  If there are no
508                  * available handlers, direct the user to '-h' for help
509                  * information.
510                  */
511                 if (print_all_handlers() == 0) {
512                         (void) printf("No handlers registered.\n");
513                         (void) printf("Run 'zinject -h' for usage "
514                             "information.\n");
515                 }
516
517                 return (0);
518         }
519
520         while ((c = getopt(argc, argv, ":ab:d:f:Fqhc:t:l:mr:e:uL:")) != -1) {
521                 switch (c) {
522                 case 'a':
523                         flags |= ZINJECT_FLUSH_ARC;
524                         break;
525                 case 'b':
526                         raw = optarg;
527                         break;
528                 case 'c':
529                         cancel = optarg;
530                         break;
531                 case 'd':
532                         device = optarg;
533                         break;
534                 case 'e':
535                         if (strcasecmp(optarg, "io") == 0) {
536                                 error = EIO;
537                         } else if (strcasecmp(optarg, "checksum") == 0) {
538                                 error = ECKSUM;
539                         } else if (strcasecmp(optarg, "nxio") == 0) {
540                                 error = ENXIO;
541                         } else {
542                                 (void) fprintf(stderr, "invalid error type "
543                                     "'%s': must be 'io', 'checksum' or "
544                                     "'nxio'\n", optarg);
545                                 usage();
546                                 return (1);
547                         }
548                         break;
549                 case 'f':
550                         record.zi_freq = atoi(optarg);
551                         if (record.zi_freq < 1 || record.zi_freq > 100) {
552                                 (void) fprintf(stderr, "frequency range must "
553                                     "be in the range (0, 100]\n");
554                                 return (1);
555                         }
556                         break;
557                 case 'F':
558                         record.zi_failfast = B_TRUE;
559                         break;
560                 case 'h':
561                         usage();
562                         return (0);
563                 case 'l':
564                         level = (int)strtol(optarg, &end, 10);
565                         if (*end != '\0') {
566                                 (void) fprintf(stderr, "invalid level '%s': "
567                                     "must be an integer\n", optarg);
568                                 usage();
569                                 return (1);
570                         }
571                         break;
572                 case 'm':
573                         domount = 1;
574                         break;
575                 case 'q':
576                         quiet = 1;
577                         break;
578                 case 'r':
579                         range = optarg;
580                         break;
581                 case 't':
582                         if ((type = name_to_type(optarg)) == TYPE_INVAL &&
583                             !MOS_TYPE(type)) {
584                                 (void) fprintf(stderr, "invalid type '%s'\n",
585                                     optarg);
586                                 usage();
587                                 return (1);
588                         }
589                         break;
590                 case 'u':
591                         flags |= ZINJECT_UNLOAD_SPA;
592                         break;
593                 case 'L':
594                         if ((label = name_to_type(optarg)) == TYPE_INVAL &&
595                             !LABEL_TYPE(type)) {
596                                 (void) fprintf(stderr, "invalid label type "
597                                     "'%s'\n", optarg);
598                                 usage();
599                                 return (1);
600                         }
601                         break;
602                 case ':':
603                         (void) fprintf(stderr, "option -%c requires an "
604                             "operand\n", optopt);
605                         usage();
606                         return (1);
607                 case '?':
608                         (void) fprintf(stderr, "invalid option '%c'\n",
609                             optopt);
610                         usage();
611                         return (2);
612                 }
613         }
614
615         argc -= optind;
616         argv += optind;
617
618         if (cancel != NULL) {
619                 /*
620                  * '-c' is invalid with any other options.
621                  */
622                 if (raw != NULL || range != NULL || type != TYPE_INVAL ||
623                     level != 0) {
624                         (void) fprintf(stderr, "cancel (-c) incompatible with "
625                             "any other options\n");
626                         usage();
627                         return (2);
628                 }
629                 if (argc != 0) {
630                         (void) fprintf(stderr, "extraneous argument to '-c'\n");
631                         usage();
632                         return (2);
633                 }
634
635                 if (strcmp(cancel, "all") == 0) {
636                         return (cancel_all_handlers());
637                 } else {
638                         int id = (int)strtol(cancel, &end, 10);
639                         if (*end != '\0') {
640                                 (void) fprintf(stderr, "invalid handle id '%s':"
641                                     " must be an integer or 'all'\n", cancel);
642                                 usage();
643                                 return (1);
644                         }
645                         return (cancel_handler(id));
646                 }
647         }
648
649         if (device != NULL) {
650                 /*
651                  * Device (-d) injection uses a completely different mechanism
652                  * for doing injection, so handle it separately here.
653                  */
654                 if (raw != NULL || range != NULL || type != TYPE_INVAL ||
655                     level != 0) {
656                         (void) fprintf(stderr, "device (-d) incompatible with "
657                             "data error injection\n");
658                         usage();
659                         return (2);
660                 }
661
662                 if (argc != 1) {
663                         (void) fprintf(stderr, "device (-d) injection requires "
664                             "a single pool name\n");
665                         usage();
666                         return (2);
667                 }
668
669                 (void) strcpy(pool, argv[0]);
670                 dataset[0] = '\0';
671
672                 if (error == ECKSUM) {
673                         (void) fprintf(stderr, "device error type must be "
674                             "'io' or 'nxio'\n");
675                         return (1);
676                 }
677
678                 if (translate_device(pool, device, label, &record) != 0)
679                         return (1);
680                 if (!error)
681                         error = ENXIO;
682         } else if (raw != NULL) {
683                 if (range != NULL || type != TYPE_INVAL || level != 0) {
684                         (void) fprintf(stderr, "raw (-b) format with "
685                             "any other options\n");
686                         usage();
687                         return (2);
688                 }
689
690                 if (argc != 1) {
691                         (void) fprintf(stderr, "raw (-b) format expects a "
692                             "single pool name\n");
693                         usage();
694                         return (2);
695                 }
696
697                 (void) strcpy(pool, argv[0]);
698                 dataset[0] = '\0';
699
700                 if (error == ENXIO) {
701                         (void) fprintf(stderr, "data error type must be "
702                             "'checksum' or 'io'\n");
703                         return (1);
704                 }
705
706                 if (translate_raw(raw, &record) != 0)
707                         return (1);
708                 if (!error)
709                         error = EIO;
710         } else if (type == TYPE_INVAL) {
711                 if (flags == 0) {
712                         (void) fprintf(stderr, "at least one of '-b', '-d', "
713                             "'-t', '-a', or '-u' must be specified\n");
714                         usage();
715                         return (2);
716                 }
717
718                 if (argc == 1 && (flags & ZINJECT_UNLOAD_SPA)) {
719                         (void) strcpy(pool, argv[0]);
720                         dataset[0] = '\0';
721                 } else if (argc != 0) {
722                         (void) fprintf(stderr, "extraneous argument for "
723                             "'-f'\n");
724                         usage();
725                         return (2);
726                 }
727
728                 flags |= ZINJECT_NULL;
729         } else {
730                 if (argc != 1) {
731                         (void) fprintf(stderr, "missing object\n");
732                         usage();
733                         return (2);
734                 }
735
736                 if (error == ENXIO) {
737                         (void) fprintf(stderr, "data error type must be "
738                             "'checksum' or 'io'\n");
739                         return (1);
740                 }
741
742                 if (translate_record(type, argv[0], range, level, &record, pool,
743                     dataset) != 0)
744                         return (1);
745                 if (!error)
746                         error = EIO;
747         }
748
749         /*
750          * If this is pool-wide metadata, unmount everything.  The ioctl() will
751          * unload the pool, so that we trigger spa-wide reopen of metadata next
752          * time we access the pool.
753          */
754         if (dataset[0] != '\0' && domount) {
755                 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_DATASET)) == NULL)
756                         return (1);
757
758                 if (zfs_unmount(zhp, NULL, 0) != 0)
759                         return (1);
760         }
761
762         record.zi_error = error;
763
764         ret = register_handler(pool, flags, &record, quiet);
765
766         if (dataset[0] != '\0' && domount)
767                 ret = (zfs_mount(zhp, NULL, 0) != 0);
768
769         libzfs_fini(g_zfs);
770
771         return (ret);
772 }