]> granicus.if.org Git - zfs/blob - cmd/zpool/zpool_main.c
Improve handling of filesystem versions
[zfs] / cmd / zpool / zpool_main.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 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25  * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
26  * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27  * Copyright (c) 2012 by Cyril Plisko. All rights reserved.
28  */
29
30 #include <assert.h>
31 #include <ctype.h>
32 #include <dirent.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <libgen.h>
36 #include <libintl.h>
37 #include <libuutil.h>
38 #include <locale.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <strings.h>
43 #include <unistd.h>
44 #include <priv.h>
45 #include <pwd.h>
46 #include <zone.h>
47 #include <zfs_prop.h>
48 #include <sys/fs/zfs.h>
49 #include <sys/stat.h>
50 #include <sys/fm/util.h>
51 #include <sys/fm/protocol.h>
52 #include <sys/zfs_ioctl.h>
53
54 #include <libzfs.h>
55
56 #include "zpool_util.h"
57 #include "zfs_comutil.h"
58 #include "zfeature_common.h"
59
60 #include "statcommon.h"
61
62 static int zpool_do_create(int, char **);
63 static int zpool_do_destroy(int, char **);
64
65 static int zpool_do_add(int, char **);
66 static int zpool_do_remove(int, char **);
67 static int zpool_do_labelclear(int, char **);
68
69 static int zpool_do_list(int, char **);
70 static int zpool_do_iostat(int, char **);
71 static int zpool_do_status(int, char **);
72
73 static int zpool_do_online(int, char **);
74 static int zpool_do_offline(int, char **);
75 static int zpool_do_clear(int, char **);
76 static int zpool_do_reopen(int, char **);
77
78 static int zpool_do_reguid(int, char **);
79
80 static int zpool_do_attach(int, char **);
81 static int zpool_do_detach(int, char **);
82 static int zpool_do_replace(int, char **);
83 static int zpool_do_split(int, char **);
84
85 static int zpool_do_scrub(int, char **);
86
87 static int zpool_do_import(int, char **);
88 static int zpool_do_export(int, char **);
89
90 static int zpool_do_upgrade(int, char **);
91
92 static int zpool_do_history(int, char **);
93 static int zpool_do_events(int, char **);
94
95 static int zpool_do_get(int, char **);
96 static int zpool_do_set(int, char **);
97
98 /*
99  * These libumem hooks provide a reasonable set of defaults for the allocator's
100  * debugging facilities.
101  */
102
103 #ifdef DEBUG
104 const char *
105 _umem_debug_init(void)
106 {
107         return ("default,verbose"); /* $UMEM_DEBUG setting */
108 }
109
110 const char *
111 _umem_logging_init(void)
112 {
113         return ("fail,contents"); /* $UMEM_LOGGING setting */
114 }
115 #endif
116
117 typedef enum {
118         HELP_ADD,
119         HELP_ATTACH,
120         HELP_CLEAR,
121         HELP_CREATE,
122         HELP_DESTROY,
123         HELP_DETACH,
124         HELP_EXPORT,
125         HELP_HISTORY,
126         HELP_IMPORT,
127         HELP_IOSTAT,
128         HELP_LABELCLEAR,
129         HELP_LIST,
130         HELP_OFFLINE,
131         HELP_ONLINE,
132         HELP_REPLACE,
133         HELP_REMOVE,
134         HELP_SCRUB,
135         HELP_STATUS,
136         HELP_UPGRADE,
137         HELP_EVENTS,
138         HELP_GET,
139         HELP_SET,
140         HELP_SPLIT,
141         HELP_REGUID,
142         HELP_REOPEN
143 } zpool_help_t;
144
145
146 typedef struct zpool_command {
147         const char      *name;
148         int             (*func)(int, char **);
149         zpool_help_t    usage;
150 } zpool_command_t;
151
152 /*
153  * Master command table.  Each ZFS command has a name, associated function, and
154  * usage message.  The usage messages need to be internationalized, so we have
155  * to have a function to return the usage message based on a command index.
156  *
157  * These commands are organized according to how they are displayed in the usage
158  * message.  An empty command (one with a NULL name) indicates an empty line in
159  * the generic usage message.
160  */
161 static zpool_command_t command_table[] = {
162         { "create",     zpool_do_create,        HELP_CREATE             },
163         { "destroy",    zpool_do_destroy,       HELP_DESTROY            },
164         { NULL },
165         { "add",        zpool_do_add,           HELP_ADD                },
166         { "remove",     zpool_do_remove,        HELP_REMOVE             },
167         { NULL },
168         { "labelclear", zpool_do_labelclear,    HELP_LABELCLEAR         },
169         { NULL },
170         { "list",       zpool_do_list,          HELP_LIST               },
171         { "iostat",     zpool_do_iostat,        HELP_IOSTAT             },
172         { "status",     zpool_do_status,        HELP_STATUS             },
173         { NULL },
174         { "online",     zpool_do_online,        HELP_ONLINE             },
175         { "offline",    zpool_do_offline,       HELP_OFFLINE            },
176         { "clear",      zpool_do_clear,         HELP_CLEAR              },
177         { "reopen",     zpool_do_reopen,        HELP_REOPEN             },
178         { NULL },
179         { "attach",     zpool_do_attach,        HELP_ATTACH             },
180         { "detach",     zpool_do_detach,        HELP_DETACH             },
181         { "replace",    zpool_do_replace,       HELP_REPLACE            },
182         { "split",      zpool_do_split,         HELP_SPLIT              },
183         { NULL },
184         { "scrub",      zpool_do_scrub,         HELP_SCRUB              },
185         { NULL },
186         { "import",     zpool_do_import,        HELP_IMPORT             },
187         { "export",     zpool_do_export,        HELP_EXPORT             },
188         { "upgrade",    zpool_do_upgrade,       HELP_UPGRADE            },
189         { "reguid",     zpool_do_reguid,        HELP_REGUID             },
190         { NULL },
191         { "history",    zpool_do_history,       HELP_HISTORY            },
192         { "events",     zpool_do_events,        HELP_EVENTS             },
193         { NULL },
194         { "get",        zpool_do_get,           HELP_GET                },
195         { "set",        zpool_do_set,           HELP_SET                },
196 };
197
198 #define NCOMMAND        (sizeof (command_table) / sizeof (command_table[0]))
199
200 static zpool_command_t *current_command;
201 static char history_str[HIS_MAX_RECORD_LEN];
202 static boolean_t log_history = B_TRUE;
203 static uint_t timestamp_fmt = NODATE;
204
205 static const char *
206 get_usage(zpool_help_t idx) {
207         switch (idx) {
208         case HELP_ADD:
209                 return (gettext("\tadd [-fn] [-o property=value] "
210                     "<pool> <vdev> ...\n"));
211         case HELP_ATTACH:
212                 return (gettext("\tattach [-f] [-o property=value] "
213                     "<pool> <device> <new-device>\n"));
214         case HELP_CLEAR:
215                 return (gettext("\tclear [-nF] <pool> [device]\n"));
216         case HELP_CREATE:
217                 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
218                     "\t    [-O file-system-property=value] ... \n"
219                     "\t    [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
220         case HELP_DESTROY:
221                 return (gettext("\tdestroy [-f] <pool>\n"));
222         case HELP_DETACH:
223                 return (gettext("\tdetach <pool> <device>\n"));
224         case HELP_EXPORT:
225                 return (gettext("\texport [-f] <pool> ...\n"));
226         case HELP_HISTORY:
227                 return (gettext("\thistory [-il] [<pool>] ...\n"));
228         case HELP_IMPORT:
229                 return (gettext("\timport [-d dir] [-D]\n"
230                     "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
231                     "\timport [-o mntopts] [-o property=value] ... \n"
232                     "\t    [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
233                     "[-R root] [-F [-n]] -a\n"
234                     "\timport [-o mntopts] [-o property=value] ... \n"
235                     "\t    [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
236                     "[-R root] [-F [-n]]\n"
237                     "\t    <pool | id> [newpool]\n"));
238         case HELP_IOSTAT:
239                 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
240                     "[count]]\n"));
241         case HELP_LABELCLEAR:
242                 return (gettext("\tlabelclear [-f] <vdev>\n"));
243         case HELP_LIST:
244                 return (gettext("\tlist [-Hv] [-o property[,...]] "
245                     "[-T d|u] [pool] ... [interval [count]]\n"));
246         case HELP_OFFLINE:
247                 return (gettext("\toffline [-t] <pool> <device> ...\n"));
248         case HELP_ONLINE:
249                 return (gettext("\tonline <pool> <device> ...\n"));
250         case HELP_REPLACE:
251                 return (gettext("\treplace [-f] [-o property=value] "
252                     "<pool> <device> [new-device]\n"));
253         case HELP_REMOVE:
254                 return (gettext("\tremove <pool> <device> ...\n"));
255         case HELP_REOPEN:
256                 return (gettext("\treopen <pool>\n"));
257         case HELP_SCRUB:
258                 return (gettext("\tscrub [-s] <pool> ...\n"));
259         case HELP_STATUS:
260                 return (gettext("\tstatus [-vxD] [-T d|u] [pool] ... [interval "
261                     "[count]]\n"));
262         case HELP_UPGRADE:
263                 return (gettext("\tupgrade\n"
264                     "\tupgrade -v\n"
265                     "\tupgrade [-V version] <-a | pool ...>\n"));
266         case HELP_EVENTS:
267                 return (gettext("\tevents [-vHfc]\n"));
268         case HELP_GET:
269                 return (gettext("\tget [-pH] <\"all\" | property[,...]> "
270                     "<pool> ...\n"));
271         case HELP_SET:
272                 return (gettext("\tset <property=value> <pool> \n"));
273         case HELP_SPLIT:
274                 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
275                     "\t    [-o property=value] <pool> <newpool> "
276                     "[<device> ...]\n"));
277         case HELP_REGUID:
278                 return (gettext("\treguid <pool>\n"));
279         }
280
281         abort();
282         /* NOTREACHED */
283 }
284
285
286 /*
287  * Callback routine that will print out a pool property value.
288  */
289 static int
290 print_prop_cb(int prop, void *cb)
291 {
292         FILE *fp = cb;
293
294         (void) fprintf(fp, "\t%-15s  ", zpool_prop_to_name(prop));
295
296         if (zpool_prop_readonly(prop))
297                 (void) fprintf(fp, "  NO   ");
298         else
299                 (void) fprintf(fp, " YES   ");
300
301         if (zpool_prop_values(prop) == NULL)
302                 (void) fprintf(fp, "-\n");
303         else
304                 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
305
306         return (ZPROP_CONT);
307 }
308
309 /*
310  * Display usage message.  If we're inside a command, display only the usage for
311  * that command.  Otherwise, iterate over the entire command table and display
312  * a complete usage message.
313  */
314 void
315 usage(boolean_t requested)
316 {
317         FILE *fp = requested ? stdout : stderr;
318
319         if (current_command == NULL) {
320                 int i;
321
322                 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
323                 (void) fprintf(fp,
324                     gettext("where 'command' is one of the following:\n\n"));
325
326                 for (i = 0; i < NCOMMAND; i++) {
327                         if (command_table[i].name == NULL)
328                                 (void) fprintf(fp, "\n");
329                         else
330                                 (void) fprintf(fp, "%s",
331                                     get_usage(command_table[i].usage));
332                 }
333         } else {
334                 (void) fprintf(fp, gettext("usage:\n"));
335                 (void) fprintf(fp, "%s", get_usage(current_command->usage));
336         }
337
338         if (current_command != NULL &&
339             ((strcmp(current_command->name, "set") == 0) ||
340             (strcmp(current_command->name, "get") == 0) ||
341             (strcmp(current_command->name, "list") == 0))) {
342
343                 (void) fprintf(fp,
344                     gettext("\nthe following properties are supported:\n"));
345
346                 (void) fprintf(fp, "\n\t%-15s  %s   %s\n\n",
347                     "PROPERTY", "EDIT", "VALUES");
348
349                 /* Iterate over all properties */
350                 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
351                     ZFS_TYPE_POOL);
352
353                 (void) fprintf(fp, "\t%-15s   ", "feature@...");
354                 (void) fprintf(fp, "YES   disabled | enabled | active\n");
355
356                 (void) fprintf(fp, gettext("\nThe feature@ properties must be "
357                     "appended with a feature name.\nSee zpool-features(5).\n"));
358         }
359
360         /*
361          * See comments at end of main().
362          */
363         if (getenv("ZFS_ABORT") != NULL) {
364                 (void) printf("dumping core by request\n");
365                 abort();
366         }
367
368         exit(requested ? 0 : 2);
369 }
370
371 void
372 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
373     boolean_t print_logs)
374 {
375         nvlist_t **child;
376         uint_t c, children;
377         char *vname;
378
379         if (name != NULL)
380                 (void) printf("\t%*s%s\n", indent, "", name);
381
382         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
383             &child, &children) != 0)
384                 return;
385
386         for (c = 0; c < children; c++) {
387                 uint64_t is_log = B_FALSE;
388
389                 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
390                     &is_log);
391                 if ((is_log && !print_logs) || (!is_log && print_logs))
392                         continue;
393
394                 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
395                 print_vdev_tree(zhp, vname, child[c], indent + 2,
396                     B_FALSE);
397                 free(vname);
398         }
399 }
400
401 static boolean_t
402 prop_list_contains_feature(nvlist_t *proplist)
403 {
404         nvpair_t *nvp;
405         for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
406             nvp = nvlist_next_nvpair(proplist, nvp)) {
407                 if (zpool_prop_feature(nvpair_name(nvp)))
408                         return (B_TRUE);
409         }
410         return (B_FALSE);
411 }
412
413 /*
414  * Add a property pair (name, string-value) into a property nvlist.
415  */
416 static int
417 add_prop_list(const char *propname, char *propval, nvlist_t **props,
418     boolean_t poolprop)
419 {
420         zpool_prop_t prop = ZPROP_INVAL;
421         zfs_prop_t fprop;
422         nvlist_t *proplist;
423         const char *normnm;
424         char *strval;
425
426         if (*props == NULL &&
427             nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
428                 (void) fprintf(stderr,
429                     gettext("internal error: out of memory\n"));
430                 return (1);
431         }
432
433         proplist = *props;
434
435         if (poolprop) {
436                 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
437
438                 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
439                     !zpool_prop_feature(propname)) {
440                         (void) fprintf(stderr, gettext("property '%s' is "
441                             "not a valid pool property\n"), propname);
442                         return (2);
443                 }
444
445                 /*
446                  * feature@ properties and version should not be specified
447                  * at the same time.
448                  */
449                 if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) &&
450                     nvlist_exists(proplist, vname)) ||
451                     (prop == ZPOOL_PROP_VERSION &&
452                     prop_list_contains_feature(proplist))) {
453                         (void) fprintf(stderr, gettext("'feature@' and "
454                             "'version' properties cannot be specified "
455                             "together\n"));
456                         return (2);
457                 }
458
459
460                 if (zpool_prop_feature(propname))
461                         normnm = propname;
462                 else
463                         normnm = zpool_prop_to_name(prop);
464         } else {
465                 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
466                         normnm = zfs_prop_to_name(fprop);
467                 } else {
468                         normnm = propname;
469                 }
470         }
471
472         if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
473             prop != ZPOOL_PROP_CACHEFILE) {
474                 (void) fprintf(stderr, gettext("property '%s' "
475                     "specified multiple times\n"), propname);
476                 return (2);
477         }
478
479         if (nvlist_add_string(proplist, normnm, propval) != 0) {
480                 (void) fprintf(stderr, gettext("internal "
481                     "error: out of memory\n"));
482                 return (1);
483         }
484
485         return (0);
486 }
487
488 /*
489  * zpool add [-fn] [-o property=value] <pool> <vdev> ...
490  *
491  *      -f      Force addition of devices, even if they appear in use
492  *      -n      Do not add the devices, but display the resulting layout if
493  *              they were to be added.
494  *      -o      Set property=value.
495  *
496  * Adds the given vdevs to 'pool'.  As with create, the bulk of this work is
497  * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
498  * libzfs.
499  */
500 int
501 zpool_do_add(int argc, char **argv)
502 {
503         boolean_t force = B_FALSE;
504         boolean_t dryrun = B_FALSE;
505         int c;
506         nvlist_t *nvroot;
507         char *poolname;
508         int ret;
509         zpool_handle_t *zhp;
510         nvlist_t *config;
511         nvlist_t *props = NULL;
512         char *propval;
513
514         /* check options */
515         while ((c = getopt(argc, argv, "fno:")) != -1) {
516                 switch (c) {
517                 case 'f':
518                         force = B_TRUE;
519                         break;
520                 case 'n':
521                         dryrun = B_TRUE;
522                         break;
523                 case 'o':
524                         if ((propval = strchr(optarg, '=')) == NULL) {
525                                 (void) fprintf(stderr, gettext("missing "
526                                     "'=' for -o option\n"));
527                                 usage(B_FALSE);
528                         }
529                         *propval = '\0';
530                         propval++;
531
532                         if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
533                             (add_prop_list(optarg, propval, &props, B_TRUE)))
534                                 usage(B_FALSE);
535                         break;
536                 case '?':
537                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
538                             optopt);
539                         usage(B_FALSE);
540                 }
541         }
542
543         argc -= optind;
544         argv += optind;
545
546         /* get pool name and check number of arguments */
547         if (argc < 1) {
548                 (void) fprintf(stderr, gettext("missing pool name argument\n"));
549                 usage(B_FALSE);
550         }
551         if (argc < 2) {
552                 (void) fprintf(stderr, gettext("missing vdev specification\n"));
553                 usage(B_FALSE);
554         }
555
556         poolname = argv[0];
557
558         argc--;
559         argv++;
560
561         if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
562                 return (1);
563
564         if ((config = zpool_get_config(zhp, NULL)) == NULL) {
565                 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
566                     poolname);
567                 zpool_close(zhp);
568                 return (1);
569         }
570
571         /* pass off to get_vdev_spec for processing */
572         nvroot = make_root_vdev(zhp, props, force, !force, B_FALSE, dryrun,
573             argc, argv);
574         if (nvroot == NULL) {
575                 zpool_close(zhp);
576                 return (1);
577         }
578
579         if (dryrun) {
580                 nvlist_t *poolnvroot;
581
582                 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
583                     &poolnvroot) == 0);
584
585                 (void) printf(gettext("would update '%s' to the following "
586                     "configuration:\n"), zpool_get_name(zhp));
587
588                 /* print original main pool and new tree */
589                 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
590                 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
591
592                 /* Do the same for the logs */
593                 if (num_logs(poolnvroot) > 0) {
594                         print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
595                         print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
596                 } else if (num_logs(nvroot) > 0) {
597                         print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
598                 }
599
600                 ret = 0;
601         } else {
602                 ret = (zpool_add(zhp, nvroot) != 0);
603         }
604
605         nvlist_free(props);
606         nvlist_free(nvroot);
607         zpool_close(zhp);
608
609         return (ret);
610 }
611
612 /*
613  * zpool remove  <pool> <vdev> ...
614  *
615  * Removes the given vdev from the pool.  Currently, this supports removing
616  * spares, cache, and log devices from the pool.
617  */
618 int
619 zpool_do_remove(int argc, char **argv)
620 {
621         char *poolname;
622         int i, ret = 0;
623         zpool_handle_t *zhp;
624
625         argc--;
626         argv++;
627
628         /* get pool name and check number of arguments */
629         if (argc < 1) {
630                 (void) fprintf(stderr, gettext("missing pool name argument\n"));
631                 usage(B_FALSE);
632         }
633         if (argc < 2) {
634                 (void) fprintf(stderr, gettext("missing device\n"));
635                 usage(B_FALSE);
636         }
637
638         poolname = argv[0];
639
640         if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
641                 return (1);
642
643         for (i = 1; i < argc; i++) {
644                 if (zpool_vdev_remove(zhp, argv[i]) != 0)
645                         ret = 1;
646         }
647
648         return (ret);
649 }
650
651 /*
652  * zpool labelclear <vdev>
653  *
654  * Verifies that the vdev is not active and zeros out the label information
655  * on the device.
656  */
657 int
658 zpool_do_labelclear(int argc, char **argv)
659 {
660         char *vdev, *name;
661         int c, fd = -1, ret = 0;
662         pool_state_t state;
663         boolean_t inuse = B_FALSE;
664         boolean_t force = B_FALSE;
665
666         /* check options */
667         while ((c = getopt(argc, argv, "f")) != -1) {
668                 switch (c) {
669                 case 'f':
670                         force = B_TRUE;
671                         break;
672                 default:
673                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
674                             optopt);
675                         usage(B_FALSE);
676                 }
677         }
678
679         argc -= optind;
680         argv += optind;
681
682         /* get vdev name */
683         if (argc < 1) {
684                 (void) fprintf(stderr, gettext("missing vdev device name\n"));
685                 usage(B_FALSE);
686         }
687
688         vdev = argv[0];
689         if ((fd = open(vdev, O_RDWR)) < 0) {
690                 (void) fprintf(stderr, gettext("Unable to open %s\n"), vdev);
691                 return (B_FALSE);
692         }
693
694         name = NULL;
695         if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0) {
696                 if (force)
697                         goto wipe_label;
698
699                 (void) fprintf(stderr,
700                     gettext("Unable to determine pool state for %s\n"
701                     "Use -f to force the clearing any label data\n"), vdev);
702
703                 return (1);
704         }
705
706         if (inuse) {
707                 switch (state) {
708                 default:
709                 case POOL_STATE_ACTIVE:
710                 case POOL_STATE_SPARE:
711                 case POOL_STATE_L2CACHE:
712                         (void) fprintf(stderr,
713                             gettext("labelclear operation failed.\n"
714                             "\tVdev %s is a member (%s), of pool \"%s\".\n"
715                             "\tTo remove label information from this device, "
716                             "export or destroy\n\tthe pool, or remove %s from "
717                             "the configuration of this pool\n\tand retry the "
718                             "labelclear operation.\n"),
719                             vdev, zpool_pool_state_to_name(state), name, vdev);
720                         ret = 1;
721                         goto errout;
722
723                 case POOL_STATE_EXPORTED:
724                         if (force)
725                                 break;
726
727                         (void) fprintf(stderr,
728                             gettext("labelclear operation failed.\n\tVdev "
729                             "%s is a member of the exported pool \"%s\".\n"
730                             "\tUse \"zpool labelclear -f %s\" to force the "
731                             "removal of label\n\tinformation.\n"),
732                             vdev, name, vdev);
733                         ret = 1;
734                         goto errout;
735
736                 case POOL_STATE_POTENTIALLY_ACTIVE:
737                         if (force)
738                                 break;
739
740                         (void) fprintf(stderr,
741                             gettext("labelclear operation failed.\n"
742                             "\tVdev %s is a member of the pool \"%s\".\n"
743                             "\tThis pool is unknown to this system, but may "
744                             "be active on\n\tanother system. Use "
745                             "\'zpool labelclear -f %s\' to force the\n"
746                             "\tremoval of label information.\n"),
747                             vdev, name, vdev);
748                         ret = 1;
749                         goto errout;
750
751                 case POOL_STATE_DESTROYED:
752                         /* inuse should never be set for a destroyed pool... */
753                         break;
754                 }
755         }
756
757 wipe_label:
758         if (zpool_clear_label(fd) != 0) {
759                 (void) fprintf(stderr,
760                     gettext("Label clear failed on vdev %s\n"), vdev);
761                 ret = 1;
762         }
763
764 errout:
765         close(fd);
766         if (name != NULL)
767                 free(name);
768
769         return (ret);
770 }
771
772 /*
773  * zpool create [-fnd] [-o property=value] ...
774  *              [-O file-system-property=value] ...
775  *              [-R root] [-m mountpoint] <pool> <dev> ...
776  *
777  *      -f      Force creation, even if devices appear in use
778  *      -n      Do not create the pool, but display the resulting layout if it
779  *              were to be created.
780  *      -R      Create a pool under an alternate root
781  *      -m      Set default mountpoint for the root dataset.  By default it's
782  *              '/<pool>'
783  *      -o      Set property=value.
784  *      -d      Don't automatically enable all supported pool features
785  *              (individual features can be enabled with -o).
786  *      -O      Set fsproperty=value in the pool's root file system
787  *
788  * Creates the named pool according to the given vdev specification.  The
789  * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c.  Once
790  * we get the nvlist back from get_vdev_spec(), we either print out the contents
791  * (if '-n' was specified), or pass it to libzfs to do the creation.
792  */
793 int
794 zpool_do_create(int argc, char **argv)
795 {
796         boolean_t force = B_FALSE;
797         boolean_t dryrun = B_FALSE;
798         boolean_t enable_all_pool_feat = B_TRUE;
799         int c;
800         nvlist_t *nvroot = NULL;
801         char *poolname;
802         int ret = 1;
803         char *altroot = NULL;
804         char *mountpoint = NULL;
805         nvlist_t *fsprops = NULL;
806         nvlist_t *props = NULL;
807         char *propval;
808
809         /* check options */
810         while ((c = getopt(argc, argv, ":fndR:m:o:O:")) != -1) {
811                 switch (c) {
812                 case 'f':
813                         force = B_TRUE;
814                         break;
815                 case 'n':
816                         dryrun = B_TRUE;
817                         break;
818                 case 'd':
819                         enable_all_pool_feat = B_FALSE;
820                         break;
821                 case 'R':
822                         altroot = optarg;
823                         if (add_prop_list(zpool_prop_to_name(
824                             ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
825                                 goto errout;
826                         if (nvlist_lookup_string(props,
827                             zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
828                             &propval) == 0)
829                                 break;
830                         if (add_prop_list(zpool_prop_to_name(
831                             ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
832                                 goto errout;
833                         break;
834                 case 'm':
835                         /* Equivalent to -O mountpoint=optarg */
836                         mountpoint = optarg;
837                         break;
838                 case 'o':
839                         if ((propval = strchr(optarg, '=')) == NULL) {
840                                 (void) fprintf(stderr, gettext("missing "
841                                     "'=' for -o option\n"));
842                                 goto errout;
843                         }
844                         *propval = '\0';
845                         propval++;
846
847                         if (add_prop_list(optarg, propval, &props, B_TRUE))
848                                 goto errout;
849
850                         /*
851                          * If the user is creating a pool that doesn't support
852                          * feature flags, don't enable any features.
853                          */
854                         if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
855                                 char *end;
856                                 u_longlong_t ver;
857
858                                 ver = strtoull(propval, &end, 10);
859                                 if (*end == '\0' &&
860                                     ver < SPA_VERSION_FEATURES) {
861                                         enable_all_pool_feat = B_FALSE;
862                                 }
863                         }
864                         break;
865                 case 'O':
866                         if ((propval = strchr(optarg, '=')) == NULL) {
867                                 (void) fprintf(stderr, gettext("missing "
868                                     "'=' for -O option\n"));
869                                 goto errout;
870                         }
871                         *propval = '\0';
872                         propval++;
873
874                         /*
875                          * Mountpoints are checked and then added later.
876                          * Uniquely among properties, they can be specified
877                          * more than once, to avoid conflict with -m.
878                          */
879                         if (0 == strcmp(optarg,
880                             zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
881                                 mountpoint = propval;
882                         } else if (add_prop_list(optarg, propval, &fsprops,
883                             B_FALSE)) {
884                                 goto errout;
885                         }
886                         break;
887                 case ':':
888                         (void) fprintf(stderr, gettext("missing argument for "
889                             "'%c' option\n"), optopt);
890                         goto badusage;
891                 case '?':
892                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
893                             optopt);
894                         goto badusage;
895                 }
896         }
897
898         argc -= optind;
899         argv += optind;
900
901         /* get pool name and check number of arguments */
902         if (argc < 1) {
903                 (void) fprintf(stderr, gettext("missing pool name argument\n"));
904                 goto badusage;
905         }
906         if (argc < 2) {
907                 (void) fprintf(stderr, gettext("missing vdev specification\n"));
908                 goto badusage;
909         }
910
911         poolname = argv[0];
912
913         /*
914          * As a special case, check for use of '/' in the name, and direct the
915          * user to use 'zfs create' instead.
916          */
917         if (strchr(poolname, '/') != NULL) {
918                 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
919                     "character '/' in pool name\n"), poolname);
920                 (void) fprintf(stderr, gettext("use 'zfs create' to "
921                     "create a dataset\n"));
922                 goto errout;
923         }
924
925         /* pass off to get_vdev_spec for bulk processing */
926         nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun,
927             argc - 1, argv + 1);
928         if (nvroot == NULL)
929                 goto errout;
930
931         /* make_root_vdev() allows 0 toplevel children if there are spares */
932         if (!zfs_allocatable_devs(nvroot)) {
933                 (void) fprintf(stderr, gettext("invalid vdev "
934                     "specification: at least one toplevel vdev must be "
935                     "specified\n"));
936                 goto errout;
937         }
938
939         if (altroot != NULL && altroot[0] != '/') {
940                 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
941                     "must be an absolute path\n"), altroot);
942                 goto errout;
943         }
944
945         /*
946          * Check the validity of the mountpoint and direct the user to use the
947          * '-m' mountpoint option if it looks like its in use.
948          */
949         if (mountpoint == NULL ||
950             (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
951             strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
952                 char buf[MAXPATHLEN];
953                 DIR *dirp;
954
955                 if (mountpoint && mountpoint[0] != '/') {
956                         (void) fprintf(stderr, gettext("invalid mountpoint "
957                             "'%s': must be an absolute path, 'legacy', or "
958                             "'none'\n"), mountpoint);
959                         goto errout;
960                 }
961
962                 if (mountpoint == NULL) {
963                         if (altroot != NULL)
964                                 (void) snprintf(buf, sizeof (buf), "%s/%s",
965                                     altroot, poolname);
966                         else
967                                 (void) snprintf(buf, sizeof (buf), "/%s",
968                                     poolname);
969                 } else {
970                         if (altroot != NULL)
971                                 (void) snprintf(buf, sizeof (buf), "%s%s",
972                                     altroot, mountpoint);
973                         else
974                                 (void) snprintf(buf, sizeof (buf), "%s",
975                                     mountpoint);
976                 }
977
978                 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
979                         (void) fprintf(stderr, gettext("mountpoint '%s' : "
980                             "%s\n"), buf, strerror(errno));
981                         (void) fprintf(stderr, gettext("use '-m' "
982                             "option to provide a different default\n"));
983                         goto errout;
984                 } else if (dirp) {
985                         int count = 0;
986
987                         while (count < 3 && readdir(dirp) != NULL)
988                                 count++;
989                         (void) closedir(dirp);
990
991                         if (count > 2) {
992                                 (void) fprintf(stderr, gettext("mountpoint "
993                                     "'%s' exists and is not empty\n"), buf);
994                                 (void) fprintf(stderr, gettext("use '-m' "
995                                     "option to provide a "
996                                     "different default\n"));
997                                 goto errout;
998                         }
999                 }
1000         }
1001
1002         /*
1003          * Now that the mountpoint's validity has been checked, ensure that
1004          * the property is set appropriately prior to creating the pool.
1005          */
1006         if (mountpoint != NULL) {
1007                 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1008                     mountpoint, &fsprops, B_FALSE);
1009                 if (ret != 0)
1010                         goto errout;
1011         }
1012
1013         ret = 1;
1014         if (dryrun) {
1015                 /*
1016                  * For a dry run invocation, print out a basic message and run
1017                  * through all the vdevs in the list and print out in an
1018                  * appropriate hierarchy.
1019                  */
1020                 (void) printf(gettext("would create '%s' with the "
1021                     "following layout:\n\n"), poolname);
1022
1023                 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
1024                 if (num_logs(nvroot) > 0)
1025                         print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
1026
1027                 ret = 0;
1028         } else {
1029                 /*
1030                  * Hand off to libzfs.
1031                  */
1032                 if (enable_all_pool_feat) {
1033                         spa_feature_t i;
1034                         for (i = 0; i < SPA_FEATURES; i++) {
1035                                 char propname[MAXPATHLEN];
1036                                 zfeature_info_t *feat = &spa_feature_table[i];
1037
1038                                 (void) snprintf(propname, sizeof (propname),
1039                                     "feature@%s", feat->fi_uname);
1040
1041                                 /*
1042                                  * Skip feature if user specified it manually
1043                                  * on the command line.
1044                                  */
1045                                 if (nvlist_exists(props, propname))
1046                                         continue;
1047
1048                                 ret = add_prop_list(propname,
1049                                     ZFS_FEATURE_ENABLED, &props, B_TRUE);
1050                                 if (ret != 0)
1051                                         goto errout;
1052                         }
1053                 }
1054
1055                 ret = 1;
1056                 if (zpool_create(g_zfs, poolname,
1057                     nvroot, props, fsprops) == 0) {
1058                         zfs_handle_t *pool = zfs_open(g_zfs, poolname,
1059                             ZFS_TYPE_FILESYSTEM);
1060                         if (pool != NULL) {
1061                                 if (zfs_mount(pool, NULL, 0) == 0)
1062                                         ret = zfs_shareall(pool);
1063                                 zfs_close(pool);
1064                         }
1065                 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1066                         (void) fprintf(stderr, gettext("pool name may have "
1067                             "been omitted\n"));
1068                 }
1069         }
1070
1071 errout:
1072         nvlist_free(nvroot);
1073         nvlist_free(fsprops);
1074         nvlist_free(props);
1075         return (ret);
1076 badusage:
1077         nvlist_free(fsprops);
1078         nvlist_free(props);
1079         usage(B_FALSE);
1080         return (2);
1081 }
1082
1083 /*
1084  * zpool destroy <pool>
1085  *
1086  *      -f      Forcefully unmount any datasets
1087  *
1088  * Destroy the given pool.  Automatically unmounts any datasets in the pool.
1089  */
1090 int
1091 zpool_do_destroy(int argc, char **argv)
1092 {
1093         boolean_t force = B_FALSE;
1094         int c;
1095         char *pool;
1096         zpool_handle_t *zhp;
1097         int ret;
1098
1099         /* check options */
1100         while ((c = getopt(argc, argv, "f")) != -1) {
1101                 switch (c) {
1102                 case 'f':
1103                         force = B_TRUE;
1104                         break;
1105                 case '?':
1106                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1107                             optopt);
1108                         usage(B_FALSE);
1109                 }
1110         }
1111
1112         argc -= optind;
1113         argv += optind;
1114
1115         /* check arguments */
1116         if (argc < 1) {
1117                 (void) fprintf(stderr, gettext("missing pool argument\n"));
1118                 usage(B_FALSE);
1119         }
1120         if (argc > 1) {
1121                 (void) fprintf(stderr, gettext("too many arguments\n"));
1122                 usage(B_FALSE);
1123         }
1124
1125         pool = argv[0];
1126
1127         if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1128                 /*
1129                  * As a special case, check for use of '/' in the name, and
1130                  * direct the user to use 'zfs destroy' instead.
1131                  */
1132                 if (strchr(pool, '/') != NULL)
1133                         (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1134                             "destroy a dataset\n"));
1135                 return (1);
1136         }
1137
1138         if (zpool_disable_datasets(zhp, force) != 0) {
1139                 (void) fprintf(stderr, gettext("could not destroy '%s': "
1140                     "could not unmount datasets\n"), zpool_get_name(zhp));
1141                 return (1);
1142         }
1143
1144         /* The history must be logged as part of the export */
1145         log_history = B_FALSE;
1146
1147         ret = (zpool_destroy(zhp, history_str) != 0);
1148
1149         zpool_close(zhp);
1150
1151         return (ret);
1152 }
1153
1154 /*
1155  * zpool export [-f] <pool> ...
1156  *
1157  *      -f      Forcefully unmount datasets
1158  *
1159  * Export the given pools.  By default, the command will attempt to cleanly
1160  * unmount any active datasets within the pool.  If the '-f' flag is specified,
1161  * then the datasets will be forcefully unmounted.
1162  */
1163 int
1164 zpool_do_export(int argc, char **argv)
1165 {
1166         boolean_t force = B_FALSE;
1167         boolean_t hardforce = B_FALSE;
1168         int c;
1169         zpool_handle_t *zhp;
1170         int ret;
1171         int i;
1172
1173         /* check options */
1174         while ((c = getopt(argc, argv, "fF")) != -1) {
1175                 switch (c) {
1176                 case 'f':
1177                         force = B_TRUE;
1178                         break;
1179                 case 'F':
1180                         hardforce = B_TRUE;
1181                         break;
1182                 case '?':
1183                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1184                             optopt);
1185                         usage(B_FALSE);
1186                 }
1187         }
1188
1189         argc -= optind;
1190         argv += optind;
1191
1192         /* check arguments */
1193         if (argc < 1) {
1194                 (void) fprintf(stderr, gettext("missing pool argument\n"));
1195                 usage(B_FALSE);
1196         }
1197
1198         ret = 0;
1199         for (i = 0; i < argc; i++) {
1200                 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1201                         ret = 1;
1202                         continue;
1203                 }
1204
1205                 if (zpool_disable_datasets(zhp, force) != 0) {
1206                         ret = 1;
1207                         zpool_close(zhp);
1208                         continue;
1209                 }
1210
1211                 /* The history must be logged as part of the export */
1212                 log_history = B_FALSE;
1213
1214                 if (hardforce) {
1215                         if (zpool_export_force(zhp, history_str) != 0)
1216                                 ret = 1;
1217                 } else if (zpool_export(zhp, force, history_str) != 0) {
1218                         ret = 1;
1219                 }
1220
1221                 zpool_close(zhp);
1222         }
1223
1224         return (ret);
1225 }
1226
1227 /*
1228  * Given a vdev configuration, determine the maximum width needed for the device
1229  * name column.
1230  */
1231 static int
1232 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1233 {
1234         char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1235         nvlist_t **child;
1236         uint_t c, children;
1237         int ret;
1238
1239         if (strlen(name) + depth > max)
1240                 max = strlen(name) + depth;
1241
1242         free(name);
1243
1244         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1245             &child, &children) == 0) {
1246                 for (c = 0; c < children; c++)
1247                         if ((ret = max_width(zhp, child[c], depth + 2,
1248                             max)) > max)
1249                                 max = ret;
1250         }
1251
1252         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1253             &child, &children) == 0) {
1254                 for (c = 0; c < children; c++)
1255                         if ((ret = max_width(zhp, child[c], depth + 2,
1256                             max)) > max)
1257                                 max = ret;
1258         }
1259
1260         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1261             &child, &children) == 0) {
1262                 for (c = 0; c < children; c++)
1263                         if ((ret = max_width(zhp, child[c], depth + 2,
1264                             max)) > max)
1265                                 max = ret;
1266         }
1267
1268
1269         return (max);
1270 }
1271
1272 typedef struct spare_cbdata {
1273         uint64_t        cb_guid;
1274         zpool_handle_t  *cb_zhp;
1275 } spare_cbdata_t;
1276
1277 static boolean_t
1278 find_vdev(nvlist_t *nv, uint64_t search)
1279 {
1280         uint64_t guid;
1281         nvlist_t **child;
1282         uint_t c, children;
1283
1284         if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1285             search == guid)
1286                 return (B_TRUE);
1287
1288         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1289             &child, &children) == 0) {
1290                 for (c = 0; c < children; c++)
1291                         if (find_vdev(child[c], search))
1292                                 return (B_TRUE);
1293         }
1294
1295         return (B_FALSE);
1296 }
1297
1298 static int
1299 find_spare(zpool_handle_t *zhp, void *data)
1300 {
1301         spare_cbdata_t *cbp = data;
1302         nvlist_t *config, *nvroot;
1303
1304         config = zpool_get_config(zhp, NULL);
1305         verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1306             &nvroot) == 0);
1307
1308         if (find_vdev(nvroot, cbp->cb_guid)) {
1309                 cbp->cb_zhp = zhp;
1310                 return (1);
1311         }
1312
1313         zpool_close(zhp);
1314         return (0);
1315 }
1316
1317 /*
1318  * Print out configuration state as requested by status_callback.
1319  */
1320 void
1321 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1322     int namewidth, int depth, boolean_t isspare)
1323 {
1324         nvlist_t **child;
1325         uint_t c, children;
1326         pool_scan_stat_t *ps = NULL;
1327         vdev_stat_t *vs;
1328         char rbuf[6], wbuf[6], cbuf[6];
1329         char *vname;
1330         uint64_t notpresent;
1331         spare_cbdata_t cb;
1332         char *state;
1333
1334         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1335             &child, &children) != 0)
1336                 children = 0;
1337
1338         verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1339             (uint64_t **)&vs, &c) == 0);
1340
1341         state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1342         if (isspare) {
1343                 /*
1344                  * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1345                  * online drives.
1346                  */
1347                 if (vs->vs_aux == VDEV_AUX_SPARED)
1348                         state = "INUSE";
1349                 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1350                         state = "AVAIL";
1351         }
1352
1353         (void) printf("\t%*s%-*s  %-8s", depth, "", namewidth - depth,
1354             name, state);
1355
1356         if (!isspare) {
1357                 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1358                 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1359                 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1360                 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1361         }
1362
1363         if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1364             &notpresent) == 0) {
1365                 char *path;
1366                 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1367                 (void) printf("  was %s", path);
1368         } else if (vs->vs_aux != 0) {
1369                 (void) printf("  ");
1370
1371                 switch (vs->vs_aux) {
1372                 case VDEV_AUX_OPEN_FAILED:
1373                         (void) printf(gettext("cannot open"));
1374                         break;
1375
1376                 case VDEV_AUX_BAD_GUID_SUM:
1377                         (void) printf(gettext("missing device"));
1378                         break;
1379
1380                 case VDEV_AUX_NO_REPLICAS:
1381                         (void) printf(gettext("insufficient replicas"));
1382                         break;
1383
1384                 case VDEV_AUX_VERSION_NEWER:
1385                         (void) printf(gettext("newer version"));
1386                         break;
1387
1388                 case VDEV_AUX_UNSUP_FEAT:
1389                         (void) printf(gettext("unsupported feature(s)"));
1390                         break;
1391
1392                 case VDEV_AUX_SPARED:
1393                         verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1394                             &cb.cb_guid) == 0);
1395                         if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1396                                 if (strcmp(zpool_get_name(cb.cb_zhp),
1397                                     zpool_get_name(zhp)) == 0)
1398                                         (void) printf(gettext("currently in "
1399                                             "use"));
1400                                 else
1401                                         (void) printf(gettext("in use by "
1402                                             "pool '%s'"),
1403                                             zpool_get_name(cb.cb_zhp));
1404                                 zpool_close(cb.cb_zhp);
1405                         } else {
1406                                 (void) printf(gettext("currently in use"));
1407                         }
1408                         break;
1409
1410                 case VDEV_AUX_ERR_EXCEEDED:
1411                         (void) printf(gettext("too many errors"));
1412                         break;
1413
1414                 case VDEV_AUX_IO_FAILURE:
1415                         (void) printf(gettext("experienced I/O failures"));
1416                         break;
1417
1418                 case VDEV_AUX_BAD_LOG:
1419                         (void) printf(gettext("bad intent log"));
1420                         break;
1421
1422                 case VDEV_AUX_EXTERNAL:
1423                         (void) printf(gettext("external device fault"));
1424                         break;
1425
1426                 case VDEV_AUX_SPLIT_POOL:
1427                         (void) printf(gettext("split into new pool"));
1428                         break;
1429
1430                 default:
1431                         (void) printf(gettext("corrupted data"));
1432                         break;
1433                 }
1434         }
1435
1436         (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1437             (uint64_t **)&ps, &c);
1438
1439         if (ps && ps->pss_state == DSS_SCANNING &&
1440             vs->vs_scan_processed != 0 && children == 0) {
1441                 (void) printf(gettext("  (%s)"),
1442                     (ps->pss_func == POOL_SCAN_RESILVER) ?
1443                     "resilvering" : "repairing");
1444         }
1445
1446         (void) printf("\n");
1447
1448         for (c = 0; c < children; c++) {
1449                 uint64_t islog = B_FALSE, ishole = B_FALSE;
1450
1451                 /* Don't print logs or holes here */
1452                 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1453                     &islog);
1454                 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1455                     &ishole);
1456                 if (islog || ishole)
1457                         continue;
1458                 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1459                 print_status_config(zhp, vname, child[c],
1460                     namewidth, depth + 2, isspare);
1461                 free(vname);
1462         }
1463 }
1464
1465
1466 /*
1467  * Print the configuration of an exported pool.  Iterate over all vdevs in the
1468  * pool, printing out the name and status for each one.
1469  */
1470 void
1471 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1472 {
1473         nvlist_t **child;
1474         uint_t c, children;
1475         vdev_stat_t *vs;
1476         char *type, *vname;
1477
1478         verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1479         if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1480             strcmp(type, VDEV_TYPE_HOLE) == 0)
1481                 return;
1482
1483         verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1484             (uint64_t **)&vs, &c) == 0);
1485
1486         (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1487         (void) printf("  %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1488
1489         if (vs->vs_aux != 0) {
1490                 (void) printf("  ");
1491
1492                 switch (vs->vs_aux) {
1493                 case VDEV_AUX_OPEN_FAILED:
1494                         (void) printf(gettext("cannot open"));
1495                         break;
1496
1497                 case VDEV_AUX_BAD_GUID_SUM:
1498                         (void) printf(gettext("missing device"));
1499                         break;
1500
1501                 case VDEV_AUX_NO_REPLICAS:
1502                         (void) printf(gettext("insufficient replicas"));
1503                         break;
1504
1505                 case VDEV_AUX_VERSION_NEWER:
1506                         (void) printf(gettext("newer version"));
1507                         break;
1508
1509                 case VDEV_AUX_UNSUP_FEAT:
1510                         (void) printf(gettext("unsupported feature(s)"));
1511                         break;
1512
1513                 case VDEV_AUX_ERR_EXCEEDED:
1514                         (void) printf(gettext("too many errors"));
1515                         break;
1516
1517                 default:
1518                         (void) printf(gettext("corrupted data"));
1519                         break;
1520                 }
1521         }
1522         (void) printf("\n");
1523
1524         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1525             &child, &children) != 0)
1526                 return;
1527
1528         for (c = 0; c < children; c++) {
1529                 uint64_t is_log = B_FALSE;
1530
1531                 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1532                     &is_log);
1533                 if (is_log)
1534                         continue;
1535
1536                 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1537                 print_import_config(vname, child[c], namewidth, depth + 2);
1538                 free(vname);
1539         }
1540
1541         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1542             &child, &children) == 0) {
1543                 (void) printf(gettext("\tcache\n"));
1544                 for (c = 0; c < children; c++) {
1545                         vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1546                         (void) printf("\t  %s\n", vname);
1547                         free(vname);
1548                 }
1549         }
1550
1551         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1552             &child, &children) == 0) {
1553                 (void) printf(gettext("\tspares\n"));
1554                 for (c = 0; c < children; c++) {
1555                         vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1556                         (void) printf("\t  %s\n", vname);
1557                         free(vname);
1558                 }
1559         }
1560 }
1561
1562 /*
1563  * Print log vdevs.
1564  * Logs are recorded as top level vdevs in the main pool child array
1565  * but with "is_log" set to 1. We use either print_status_config() or
1566  * print_import_config() to print the top level logs then any log
1567  * children (eg mirrored slogs) are printed recursively - which
1568  * works because only the top level vdev is marked "is_log"
1569  */
1570 static void
1571 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1572 {
1573         uint_t c, children;
1574         nvlist_t **child;
1575
1576         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1577             &children) != 0)
1578                 return;
1579
1580         (void) printf(gettext("\tlogs\n"));
1581
1582         for (c = 0; c < children; c++) {
1583                 uint64_t is_log = B_FALSE;
1584                 char *name;
1585
1586                 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1587                     &is_log);
1588                 if (!is_log)
1589                         continue;
1590                 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1591                 if (verbose)
1592                         print_status_config(zhp, name, child[c], namewidth,
1593                             2, B_FALSE);
1594                 else
1595                         print_import_config(name, child[c], namewidth, 2);
1596                 free(name);
1597         }
1598 }
1599
1600 /*
1601  * Display the status for the given pool.
1602  */
1603 static void
1604 show_import(nvlist_t *config)
1605 {
1606         uint64_t pool_state;
1607         vdev_stat_t *vs;
1608         char *name;
1609         uint64_t guid;
1610         char *msgid;
1611         nvlist_t *nvroot;
1612         zpool_status_t reason;
1613         zpool_errata_t errata;
1614         const char *health;
1615         uint_t vsc;
1616         int namewidth;
1617         char *comment;
1618
1619         verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1620             &name) == 0);
1621         verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1622             &guid) == 0);
1623         verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1624             &pool_state) == 0);
1625         verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1626             &nvroot) == 0);
1627
1628         verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1629             (uint64_t **)&vs, &vsc) == 0);
1630         health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1631
1632         reason = zpool_import_status(config, &msgid, &errata);
1633
1634         (void) printf(gettext("   pool: %s\n"), name);
1635         (void) printf(gettext("     id: %llu\n"), (u_longlong_t)guid);
1636         (void) printf(gettext("  state: %s"), health);
1637         if (pool_state == POOL_STATE_DESTROYED)
1638                 (void) printf(gettext(" (DESTROYED)"));
1639         (void) printf("\n");
1640
1641         switch (reason) {
1642         case ZPOOL_STATUS_MISSING_DEV_R:
1643         case ZPOOL_STATUS_MISSING_DEV_NR:
1644         case ZPOOL_STATUS_BAD_GUID_SUM:
1645                 (void) printf(gettext(" status: One or more devices are "
1646                     "missing from the system.\n"));
1647                 break;
1648
1649         case ZPOOL_STATUS_CORRUPT_LABEL_R:
1650         case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1651                 (void) printf(gettext(" status: One or more devices contains "
1652                     "corrupted data.\n"));
1653                 break;
1654
1655         case ZPOOL_STATUS_CORRUPT_DATA:
1656                 (void) printf(
1657                     gettext(" status: The pool data is corrupted.\n"));
1658                 break;
1659
1660         case ZPOOL_STATUS_OFFLINE_DEV:
1661                 (void) printf(gettext(" status: One or more devices "
1662                     "are offlined.\n"));
1663                 break;
1664
1665         case ZPOOL_STATUS_CORRUPT_POOL:
1666                 (void) printf(gettext(" status: The pool metadata is "
1667                     "corrupted.\n"));
1668                 break;
1669
1670         case ZPOOL_STATUS_VERSION_OLDER:
1671                 (void) printf(gettext(" status: The pool is formatted using a "
1672                     "legacy on-disk version.\n"));
1673                 break;
1674
1675         case ZPOOL_STATUS_VERSION_NEWER:
1676                 (void) printf(gettext(" status: The pool is formatted using an "
1677                     "incompatible version.\n"));
1678                 break;
1679
1680         case ZPOOL_STATUS_FEAT_DISABLED:
1681                 (void) printf(gettext(" status: Some supported features are "
1682                     "not enabled on the pool.\n"));
1683                 break;
1684
1685         case ZPOOL_STATUS_UNSUP_FEAT_READ:
1686                 (void) printf(gettext("status: The pool uses the following "
1687                     "feature(s) not supported on this sytem:\n"));
1688                 zpool_print_unsup_feat(config);
1689                 break;
1690
1691         case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1692                 (void) printf(gettext("status: The pool can only be accessed "
1693                     "in read-only mode on this system. It\n\tcannot be "
1694                     "accessed in read-write mode because it uses the "
1695                     "following\n\tfeature(s) not supported on this system:\n"));
1696                 zpool_print_unsup_feat(config);
1697                 break;
1698
1699         case ZPOOL_STATUS_HOSTID_MISMATCH:
1700                 (void) printf(gettext(" status: The pool was last accessed by "
1701                     "another system.\n"));
1702                 break;
1703
1704         case ZPOOL_STATUS_FAULTED_DEV_R:
1705         case ZPOOL_STATUS_FAULTED_DEV_NR:
1706                 (void) printf(gettext(" status: One or more devices are "
1707                     "faulted.\n"));
1708                 break;
1709
1710         case ZPOOL_STATUS_BAD_LOG:
1711                 (void) printf(gettext(" status: An intent log record cannot be "
1712                     "read.\n"));
1713                 break;
1714
1715         case ZPOOL_STATUS_RESILVERING:
1716                 (void) printf(gettext(" status: One or more devices were being "
1717                     "resilvered.\n"));
1718                 break;
1719
1720         case ZPOOL_STATUS_ERRATA:
1721                 (void) printf(gettext(" status: Errata #%d detected.\n"),
1722                     errata);
1723                 break;
1724
1725         default:
1726                 /*
1727                  * No other status can be seen when importing pools.
1728                  */
1729                 assert(reason == ZPOOL_STATUS_OK);
1730         }
1731
1732         /*
1733          * Print out an action according to the overall state of the pool.
1734          */
1735         if (vs->vs_state == VDEV_STATE_HEALTHY) {
1736                 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1737                     reason == ZPOOL_STATUS_FEAT_DISABLED) {
1738                         (void) printf(gettext(" action: The pool can be "
1739                             "imported using its name or numeric identifier, "
1740                             "though\n\tsome features will not be available "
1741                             "without an explicit 'zpool upgrade'.\n"));
1742                 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1743                         (void) printf(gettext(" action: The pool can be "
1744                             "imported using its name or numeric "
1745                             "identifier and\n\tthe '-f' flag.\n"));
1746                 } else if (reason == ZPOOL_STATUS_ERRATA) {
1747                         switch (errata) {
1748                         case ZPOOL_ERRATA_NONE:
1749                                 break;
1750
1751                         case ZPOOL_ERRATA_ZOL_2094_SCRUB:
1752                                 (void) printf(gettext(" action: The pool can "
1753                                     "be imported using its name or numeric "
1754                                     "identifier,\n\thowever there is a compat"
1755                                     "ibility issue which should be corrected"
1756                                     "\n\tby running 'zpool scrub'\n"));
1757                                 break;
1758
1759                         case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY:
1760                                 (void) printf(gettext(" action: The pool can"
1761                                     "not be imported with this version of ZFS "
1762                                     "due to\n\tan active asynchronous destroy. "
1763                                     "Revert to an earlier version\n\tand "
1764                                     "allow the destroy to complete before "
1765                                     "updating.\n"));
1766                                 break;
1767
1768                         default:
1769                                 /*
1770                                  * All errata must contain an action message.
1771                                  */
1772                                 assert(0);
1773                         }
1774                 } else {
1775                         (void) printf(gettext(" action: The pool can be "
1776                             "imported using its name or numeric "
1777                             "identifier.\n"));
1778                 }
1779         } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1780                 (void) printf(gettext(" action: The pool can be imported "
1781                     "despite missing or damaged devices.  The\n\tfault "
1782                     "tolerance of the pool may be compromised if imported.\n"));
1783         } else {
1784                 switch (reason) {
1785                 case ZPOOL_STATUS_VERSION_NEWER:
1786                         (void) printf(gettext(" action: The pool cannot be "
1787                             "imported.  Access the pool on a system running "
1788                             "newer\n\tsoftware, or recreate the pool from "
1789                             "backup.\n"));
1790                         break;
1791                 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1792                         (void) printf(gettext("action: The pool cannot be "
1793                             "imported. Access the pool on a system that "
1794                             "supports\n\tthe required feature(s), or recreate "
1795                             "the pool from backup.\n"));
1796                         break;
1797                 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1798                         (void) printf(gettext("action: The pool cannot be "
1799                             "imported in read-write mode. Import the pool "
1800                             "with\n"
1801                             "\t\"-o readonly=on\", access the pool on a system "
1802                             "that supports the\n\trequired feature(s), or "
1803                             "recreate the pool from backup.\n"));
1804                         break;
1805                 case ZPOOL_STATUS_MISSING_DEV_R:
1806                 case ZPOOL_STATUS_MISSING_DEV_NR:
1807                 case ZPOOL_STATUS_BAD_GUID_SUM:
1808                         (void) printf(gettext(" action: The pool cannot be "
1809                             "imported. Attach the missing\n\tdevices and try "
1810                             "again.\n"));
1811                         break;
1812                 default:
1813                         (void) printf(gettext(" action: The pool cannot be "
1814                             "imported due to damaged devices or data.\n"));
1815                 }
1816         }
1817
1818         /* Print the comment attached to the pool. */
1819         if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1820                 (void) printf(gettext("comment: %s\n"), comment);
1821
1822         /*
1823          * If the state is "closed" or "can't open", and the aux state
1824          * is "corrupt data":
1825          */
1826         if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1827             (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1828             (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1829                 if (pool_state == POOL_STATE_DESTROYED)
1830                         (void) printf(gettext("\tThe pool was destroyed, "
1831                             "but can be imported using the '-Df' flags.\n"));
1832                 else if (pool_state != POOL_STATE_EXPORTED)
1833                         (void) printf(gettext("\tThe pool may be active on "
1834                             "another system, but can be imported using\n\t"
1835                             "the '-f' flag.\n"));
1836         }
1837
1838         if (msgid != NULL)
1839                 (void) printf(gettext("   see: http://zfsonlinux.org/msg/%s\n"),
1840                     msgid);
1841
1842         (void) printf(gettext(" config:\n\n"));
1843
1844         namewidth = max_width(NULL, nvroot, 0, 0);
1845         if (namewidth < 10)
1846                 namewidth = 10;
1847
1848         print_import_config(name, nvroot, namewidth, 0);
1849         if (num_logs(nvroot) > 0)
1850                 print_logs(NULL, nvroot, namewidth, B_FALSE);
1851
1852         if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1853                 (void) printf(gettext("\n\tAdditional devices are known to "
1854                     "be part of this pool, though their\n\texact "
1855                     "configuration cannot be determined.\n"));
1856         }
1857 }
1858
1859 /*
1860  * Perform the import for the given configuration.  This passes the heavy
1861  * lifting off to zpool_import_props(), and then mounts the datasets contained
1862  * within the pool.
1863  */
1864 static int
1865 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1866     nvlist_t *props, int flags)
1867 {
1868         zpool_handle_t *zhp;
1869         char *name;
1870         uint64_t state;
1871         uint64_t version;
1872
1873         verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1874             &name) == 0);
1875
1876         verify(nvlist_lookup_uint64(config,
1877             ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1878         verify(nvlist_lookup_uint64(config,
1879             ZPOOL_CONFIG_VERSION, &version) == 0);
1880         if (!SPA_VERSION_IS_SUPPORTED(version)) {
1881                 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1882                     "is formatted using an unsupported ZFS version\n"), name);
1883                 return (1);
1884         } else if (state != POOL_STATE_EXPORTED &&
1885             !(flags & ZFS_IMPORT_ANY_HOST)) {
1886                 uint64_t hostid;
1887
1888                 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1889                     &hostid) == 0) {
1890                         unsigned long system_hostid = gethostid() & 0xffffffff;
1891
1892                         if ((unsigned long)hostid != system_hostid) {
1893                                 char *hostname;
1894                                 uint64_t timestamp;
1895                                 time_t t;
1896
1897                                 verify(nvlist_lookup_string(config,
1898                                     ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1899                                 verify(nvlist_lookup_uint64(config,
1900                                     ZPOOL_CONFIG_TIMESTAMP, &timestamp) == 0);
1901                                 t = timestamp;
1902                                 (void) fprintf(stderr, gettext("cannot import "
1903                                     "'%s': pool may be in use from other "
1904                                     "system, it was last accessed by %s "
1905                                     "(hostid: 0x%lx) on %s"), name, hostname,
1906                                     (unsigned long)hostid,
1907                                     asctime(localtime(&t)));
1908                                 (void) fprintf(stderr, gettext("use '-f' to "
1909                                     "import anyway\n"));
1910                                 return (1);
1911                         }
1912                 } else {
1913                         (void) fprintf(stderr, gettext("cannot import '%s': "
1914                             "pool may be in use from other system\n"), name);
1915                         (void) fprintf(stderr, gettext("use '-f' to import "
1916                             "anyway\n"));
1917                         return (1);
1918                 }
1919         }
1920
1921         if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1922                 return (1);
1923
1924         if (newname != NULL)
1925                 name = (char *)newname;
1926
1927         if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1928                 return (1);
1929
1930         if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1931             !(flags & ZFS_IMPORT_ONLY) &&
1932             zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1933                 zpool_close(zhp);
1934                 return (1);
1935         }
1936
1937         zpool_close(zhp);
1938         return (0);
1939 }
1940
1941 /*
1942  * zpool import [-d dir] [-D]
1943  *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1944  *              [-d dir | -c cachefile] [-f] -a
1945  *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1946  *              [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1947  *
1948  *       -c     Read pool information from a cachefile instead of searching
1949  *              devices.
1950  *
1951  *       -d     Scan in a specific directory, other than /dev/.  More than
1952  *              one directory can be specified using multiple '-d' options.
1953  *
1954  *       -D     Scan for previously destroyed pools or import all or only
1955  *              specified destroyed pools.
1956  *
1957  *       -R     Temporarily import the pool, with all mountpoints relative to
1958  *              the given root.  The pool will remain exported when the machine
1959  *              is rebooted.
1960  *
1961  *       -V     Import even in the presence of faulted vdevs.  This is an
1962  *              intentionally undocumented option for testing purposes, and
1963  *              treats the pool configuration as complete, leaving any bad
1964  *              vdevs in the FAULTED state. In other words, it does verbatim
1965  *              import.
1966  *
1967  *       -f     Force import, even if it appears that the pool is active.
1968  *
1969  *       -F     Attempt rewind if necessary.
1970  *
1971  *       -n     See if rewind would work, but don't actually rewind.
1972  *
1973  *       -N     Import the pool but don't mount datasets.
1974  *
1975  *       -T     Specify a starting txg to use for import. This option is
1976  *              intentionally undocumented option for testing purposes.
1977  *
1978  *       -a     Import all pools found.
1979  *
1980  *       -o     Set property=value and/or temporary mount options (without '=').
1981  *
1982  * The import command scans for pools to import, and import pools based on pool
1983  * name and GUID.  The pool can also be renamed as part of the import process.
1984  */
1985 int
1986 zpool_do_import(int argc, char **argv)
1987 {
1988         char **searchdirs = NULL;
1989         char *env, *envdup = NULL;
1990         int nsearch = 0;
1991         int c;
1992         int err = 0;
1993         nvlist_t *pools = NULL;
1994         boolean_t do_all = B_FALSE;
1995         boolean_t do_destroyed = B_FALSE;
1996         char *mntopts = NULL;
1997         nvpair_t *elem;
1998         nvlist_t *config;
1999         uint64_t searchguid = 0;
2000         char *searchname = NULL;
2001         char *propval;
2002         nvlist_t *found_config;
2003         nvlist_t *policy = NULL;
2004         nvlist_t *props = NULL;
2005         boolean_t first;
2006         int flags = ZFS_IMPORT_NORMAL;
2007         uint32_t rewind_policy = ZPOOL_NO_REWIND;
2008         boolean_t dryrun = B_FALSE;
2009         boolean_t do_rewind = B_FALSE;
2010         boolean_t xtreme_rewind = B_FALSE;
2011         uint64_t pool_state, txg = -1ULL;
2012         char *cachefile = NULL;
2013         importargs_t idata = { 0 };
2014         char *endptr;
2015
2016         /* check options */
2017         while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:R:tT:VX")) != -1) {
2018                 switch (c) {
2019                 case 'a':
2020                         do_all = B_TRUE;
2021                         break;
2022                 case 'c':
2023                         cachefile = optarg;
2024                         break;
2025                 case 'd':
2026                         if (searchdirs == NULL) {
2027                                 searchdirs = safe_malloc(sizeof (char *));
2028                         } else {
2029                                 char **tmp = safe_malloc((nsearch + 1) *
2030                                     sizeof (char *));
2031                                 bcopy(searchdirs, tmp, nsearch *
2032                                     sizeof (char *));
2033                                 free(searchdirs);
2034                                 searchdirs = tmp;
2035                         }
2036                         searchdirs[nsearch++] = optarg;
2037                         break;
2038                 case 'D':
2039                         do_destroyed = B_TRUE;
2040                         break;
2041                 case 'f':
2042                         flags |= ZFS_IMPORT_ANY_HOST;
2043                         break;
2044                 case 'F':
2045                         do_rewind = B_TRUE;
2046                         break;
2047                 case 'm':
2048                         flags |= ZFS_IMPORT_MISSING_LOG;
2049                         break;
2050                 case 'n':
2051                         dryrun = B_TRUE;
2052                         break;
2053                 case 'N':
2054                         flags |= ZFS_IMPORT_ONLY;
2055                         break;
2056                 case 'o':
2057                         if ((propval = strchr(optarg, '=')) != NULL) {
2058                                 *propval = '\0';
2059                                 propval++;
2060                                 if (add_prop_list(optarg, propval,
2061                                     &props, B_TRUE))
2062                                         goto error;
2063                         } else {
2064                                 mntopts = optarg;
2065                         }
2066                         break;
2067                 case 'R':
2068                         if (add_prop_list(zpool_prop_to_name(
2069                             ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2070                                 goto error;
2071                         if (nvlist_lookup_string(props,
2072                             zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
2073                             &propval) == 0)
2074                                 break;
2075                         if (add_prop_list(zpool_prop_to_name(
2076                             ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2077                                 goto error;
2078                         break;
2079                 case 't':
2080                         flags |= ZFS_IMPORT_TEMP_NAME;
2081                         break;
2082
2083                 case 'T':
2084                         errno = 0;
2085                         txg = strtoull(optarg, &endptr, 0);
2086                         if (errno != 0 || *endptr != '\0') {
2087                                 (void) fprintf(stderr,
2088                                     gettext("invalid txg value\n"));
2089                                 usage(B_FALSE);
2090                         }
2091                         rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2092                         break;
2093                 case 'V':
2094                         flags |= ZFS_IMPORT_VERBATIM;
2095                         break;
2096                 case 'X':
2097                         xtreme_rewind = B_TRUE;
2098                         break;
2099                 case ':':
2100                         (void) fprintf(stderr, gettext("missing argument for "
2101                             "'%c' option\n"), optopt);
2102                         usage(B_FALSE);
2103                         break;
2104                 case '?':
2105                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2106                             optopt);
2107                         usage(B_FALSE);
2108                 }
2109         }
2110
2111         argc -= optind;
2112         argv += optind;
2113
2114         if (cachefile && nsearch != 0) {
2115                 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2116                 usage(B_FALSE);
2117         }
2118
2119         if ((dryrun || xtreme_rewind) && !do_rewind) {
2120                 (void) fprintf(stderr,
2121                     gettext("-n or -X only meaningful with -F\n"));
2122                 usage(B_FALSE);
2123         }
2124         if (dryrun)
2125                 rewind_policy = ZPOOL_TRY_REWIND;
2126         else if (do_rewind)
2127                 rewind_policy = ZPOOL_DO_REWIND;
2128         if (xtreme_rewind)
2129                 rewind_policy |= ZPOOL_EXTREME_REWIND;
2130
2131         /* In the future, we can capture further policy and include it here */
2132         if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2133             nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2134             nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2135                 goto error;
2136
2137         /* check argument count */
2138         if (do_all) {
2139                 if (argc != 0) {
2140                         (void) fprintf(stderr, gettext("too many arguments\n"));
2141                         usage(B_FALSE);
2142                 }
2143         } else {
2144                 if (argc > 2) {
2145                         (void) fprintf(stderr, gettext("too many arguments\n"));
2146                         usage(B_FALSE);
2147                 }
2148
2149                 /*
2150                  * Check for the SYS_CONFIG privilege.  We do this explicitly
2151                  * here because otherwise any attempt to discover pools will
2152                  * silently fail.
2153                  */
2154                 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2155                         (void) fprintf(stderr, gettext("cannot "
2156                             "discover pools: permission denied\n"));
2157                         if (searchdirs != NULL)
2158                                 free(searchdirs);
2159
2160                         nvlist_free(policy);
2161                         return (1);
2162                 }
2163         }
2164
2165         /*
2166          * Depending on the arguments given, we do one of the following:
2167          *
2168          *      <none>  Iterate through all pools and display information about
2169          *              each one.
2170          *
2171          *      -a      Iterate through all pools and try to import each one.
2172          *
2173          *      <id>    Find the pool that corresponds to the given GUID/pool
2174          *              name and import that one.
2175          *
2176          *      -D      Above options applies only to destroyed pools.
2177          */
2178         if (argc != 0) {
2179                 char *endptr;
2180
2181                 errno = 0;
2182                 searchguid = strtoull(argv[0], &endptr, 10);
2183                 if (errno != 0 || *endptr != '\0')
2184                         searchname = argv[0];
2185                 found_config = NULL;
2186
2187                 /*
2188                  * User specified a name or guid.  Ensure it's unique.
2189                  */
2190                 idata.unique = B_TRUE;
2191         }
2192
2193         /*
2194          * Check the environment for the preferred search path.
2195          */
2196         if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) {
2197                 char *dir;
2198
2199                 envdup = strdup(env);
2200
2201                 dir = strtok(envdup, ":");
2202                 while (dir != NULL) {
2203                         if (searchdirs == NULL) {
2204                                 searchdirs = safe_malloc(sizeof (char *));
2205                         } else {
2206                                 char **tmp = safe_malloc((nsearch + 1) *
2207                                     sizeof (char *));
2208                                 bcopy(searchdirs, tmp, nsearch *
2209                                     sizeof (char *));
2210                                 free(searchdirs);
2211                                 searchdirs = tmp;
2212                         }
2213                         searchdirs[nsearch++] = dir;
2214                         dir = strtok(NULL, ":");
2215                 }
2216         }
2217
2218         idata.path = searchdirs;
2219         idata.paths = nsearch;
2220         idata.poolname = searchname;
2221         idata.guid = searchguid;
2222         idata.cachefile = cachefile;
2223
2224         pools = zpool_search_import(g_zfs, &idata);
2225
2226         if (pools != NULL && idata.exists &&
2227             (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2228                 (void) fprintf(stderr, gettext("cannot import '%s': "
2229                     "a pool with that name already exists\n"),
2230                     argv[0]);
2231                 (void) fprintf(stderr, gettext("use the form '%s "
2232                     "<pool | id> <newpool>' to give it a new name\n"),
2233                     "zpool import");
2234                 err = 1;
2235         } else if (pools == NULL && idata.exists) {
2236                 (void) fprintf(stderr, gettext("cannot import '%s': "
2237                     "a pool with that name is already created/imported,\n"),
2238                     argv[0]);
2239                 (void) fprintf(stderr, gettext("and no additional pools "
2240                     "with that name were found\n"));
2241                 err = 1;
2242         } else if (pools == NULL) {
2243                 if (argc != 0) {
2244                         (void) fprintf(stderr, gettext("cannot import '%s': "
2245                             "no such pool available\n"), argv[0]);
2246                 }
2247                 err = 1;
2248         }
2249
2250         if (err == 1) {
2251                 if (searchdirs != NULL)
2252                         free(searchdirs);
2253                 if (envdup != NULL)
2254                         free(envdup);
2255                 nvlist_free(policy);
2256                 return (1);
2257         }
2258
2259         /*
2260          * At this point we have a list of import candidate configs. Even if
2261          * we were searching by pool name or guid, we still need to
2262          * post-process the list to deal with pool state and possible
2263          * duplicate names.
2264          */
2265         err = 0;
2266         elem = NULL;
2267         first = B_TRUE;
2268         while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2269
2270                 verify(nvpair_value_nvlist(elem, &config) == 0);
2271
2272                 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2273                     &pool_state) == 0);
2274                 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2275                         continue;
2276                 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2277                         continue;
2278
2279                 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2280                     policy) == 0);
2281
2282                 if (argc == 0) {
2283                         if (first)
2284                                 first = B_FALSE;
2285                         else if (!do_all)
2286                                 (void) printf("\n");
2287
2288                         if (do_all) {
2289                                 err |= do_import(config, NULL, mntopts,
2290                                     props, flags);
2291                         } else {
2292                                 show_import(config);
2293                         }
2294                 } else if (searchname != NULL) {
2295                         char *name;
2296
2297                         /*
2298                          * We are searching for a pool based on name.
2299                          */
2300                         verify(nvlist_lookup_string(config,
2301                             ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2302
2303                         if (strcmp(name, searchname) == 0) {
2304                                 if (found_config != NULL) {
2305                                         (void) fprintf(stderr, gettext(
2306                                             "cannot import '%s': more than "
2307                                             "one matching pool\n"), searchname);
2308                                         (void) fprintf(stderr, gettext(
2309                                             "import by numeric ID instead\n"));
2310                                         err = B_TRUE;
2311                                 }
2312                                 found_config = config;
2313                         }
2314                 } else {
2315                         uint64_t guid;
2316
2317                         /*
2318                          * Search for a pool by guid.
2319                          */
2320                         verify(nvlist_lookup_uint64(config,
2321                             ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2322
2323                         if (guid == searchguid)
2324                                 found_config = config;
2325                 }
2326         }
2327
2328         /*
2329          * If we were searching for a specific pool, verify that we found a
2330          * pool, and then do the import.
2331          */
2332         if (argc != 0 && err == 0) {
2333                 if (found_config == NULL) {
2334                         (void) fprintf(stderr, gettext("cannot import '%s': "
2335                             "no such pool available\n"), argv[0]);
2336                         err = B_TRUE;
2337                 } else {
2338                         err |= do_import(found_config, argc == 1 ? NULL :
2339                             argv[1], mntopts, props, flags);
2340                 }
2341         }
2342
2343         /*
2344          * If we were just looking for pools, report an error if none were
2345          * found.
2346          */
2347         if (argc == 0 && first)
2348                 (void) fprintf(stderr,
2349                     gettext("no pools available to import\n"));
2350
2351 error:
2352         nvlist_free(props);
2353         nvlist_free(pools);
2354         nvlist_free(policy);
2355         if (searchdirs != NULL)
2356                 free(searchdirs);
2357         if (envdup != NULL)
2358                 free(envdup);
2359
2360         return (err ? 1 : 0);
2361 }
2362
2363 typedef struct iostat_cbdata {
2364         boolean_t cb_verbose;
2365         int cb_namewidth;
2366         int cb_iteration;
2367         zpool_list_t *cb_list;
2368 } iostat_cbdata_t;
2369
2370 static void
2371 print_iostat_separator(iostat_cbdata_t *cb)
2372 {
2373         int i = 0;
2374
2375         for (i = 0; i < cb->cb_namewidth; i++)
2376                 (void) printf("-");
2377         (void) printf("  -----  -----  -----  -----  -----  -----\n");
2378 }
2379
2380 static void
2381 print_iostat_header(iostat_cbdata_t *cb)
2382 {
2383         (void) printf("%*s     capacity     operations    bandwidth\n",
2384             cb->cb_namewidth, "");
2385         (void) printf("%-*s  alloc   free   read  write   read  write\n",
2386             cb->cb_namewidth, "pool");
2387         print_iostat_separator(cb);
2388 }
2389
2390 /*
2391  * Display a single statistic.
2392  */
2393 static void
2394 print_one_stat(uint64_t value)
2395 {
2396         char buf[64];
2397
2398         zfs_nicenum(value, buf, sizeof (buf));
2399         (void) printf("  %5s", buf);
2400 }
2401
2402 /*
2403  * Print out all the statistics for the given vdev.  This can either be the
2404  * toplevel configuration, or called recursively.  If 'name' is NULL, then this
2405  * is a verbose output, and we don't want to display the toplevel pool stats.
2406  */
2407 void
2408 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2409     nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2410 {
2411         nvlist_t **oldchild, **newchild;
2412         uint_t c, children;
2413         vdev_stat_t *oldvs, *newvs;
2414         vdev_stat_t zerovs = { 0 };
2415         uint64_t tdelta;
2416         double scale;
2417         char *vname;
2418
2419         if (oldnv != NULL) {
2420                 verify(nvlist_lookup_uint64_array(oldnv,
2421                     ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2422         } else {
2423                 oldvs = &zerovs;
2424         }
2425
2426         verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2427             (uint64_t **)&newvs, &c) == 0);
2428
2429         if (strlen(name) + depth > cb->cb_namewidth)
2430                 (void) printf("%*s%s", depth, "", name);
2431         else
2432                 (void) printf("%*s%s%*s", depth, "", name,
2433                     (int)(cb->cb_namewidth - strlen(name) - depth), "");
2434
2435         tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2436
2437         if (tdelta == 0)
2438                 scale = 1.0;
2439         else
2440                 scale = (double)NANOSEC / tdelta;
2441
2442         /* only toplevel vdevs have capacity stats */
2443         if (newvs->vs_space == 0) {
2444                 (void) printf("      -      -");
2445         } else {
2446                 print_one_stat(newvs->vs_alloc);
2447                 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2448         }
2449
2450         print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2451             oldvs->vs_ops[ZIO_TYPE_READ])));
2452
2453         print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2454             oldvs->vs_ops[ZIO_TYPE_WRITE])));
2455
2456         print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2457             oldvs->vs_bytes[ZIO_TYPE_READ])));
2458
2459         print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2460             oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2461
2462         (void) printf("\n");
2463
2464         if (!cb->cb_verbose)
2465                 return;
2466
2467         if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2468             &newchild, &children) != 0)
2469                 return;
2470
2471         if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2472             &oldchild, &c) != 0)
2473                 return;
2474
2475         for (c = 0; c < children; c++) {
2476                 uint64_t ishole = B_FALSE, islog = B_FALSE;
2477
2478                 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2479                     &ishole);
2480
2481                 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2482                     &islog);
2483
2484                 if (ishole || islog)
2485                         continue;
2486
2487                 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2488                 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2489                     newchild[c], cb, depth + 2);
2490                 free(vname);
2491         }
2492
2493         /*
2494          * Log device section
2495          */
2496
2497         if (num_logs(newnv) > 0) {
2498                 (void) printf("%-*s      -      -      -      -      -      "
2499                     "-\n", cb->cb_namewidth, "logs");
2500
2501                 for (c = 0; c < children; c++) {
2502                         uint64_t islog = B_FALSE;
2503                         (void) nvlist_lookup_uint64(newchild[c],
2504                             ZPOOL_CONFIG_IS_LOG, &islog);
2505
2506                         if (islog) {
2507                                 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2508                                     B_FALSE);
2509                                 print_vdev_stats(zhp, vname, oldnv ?
2510                                     oldchild[c] : NULL, newchild[c],
2511                                     cb, depth + 2);
2512                                 free(vname);
2513                         }
2514                 }
2515
2516         }
2517
2518         /*
2519          * Include level 2 ARC devices in iostat output
2520          */
2521         if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2522             &newchild, &children) != 0)
2523                 return;
2524
2525         if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2526             &oldchild, &c) != 0)
2527                 return;
2528
2529         if (children > 0) {
2530                 (void) printf("%-*s      -      -      -      -      -      "
2531                     "-\n", cb->cb_namewidth, "cache");
2532                 for (c = 0; c < children; c++) {
2533                         vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2534                             B_FALSE);
2535                         print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2536                             newchild[c], cb, depth + 2);
2537                         free(vname);
2538                 }
2539         }
2540 }
2541
2542 static int
2543 refresh_iostat(zpool_handle_t *zhp, void *data)
2544 {
2545         iostat_cbdata_t *cb = data;
2546         boolean_t missing;
2547
2548         /*
2549          * If the pool has disappeared, remove it from the list and continue.
2550          */
2551         if (zpool_refresh_stats(zhp, &missing) != 0)
2552                 return (-1);
2553
2554         if (missing)
2555                 pool_list_remove(cb->cb_list, zhp);
2556
2557         return (0);
2558 }
2559
2560 /*
2561  * Callback to print out the iostats for the given pool.
2562  */
2563 int
2564 print_iostat(zpool_handle_t *zhp, void *data)
2565 {
2566         iostat_cbdata_t *cb = data;
2567         nvlist_t *oldconfig, *newconfig;
2568         nvlist_t *oldnvroot, *newnvroot;
2569
2570         newconfig = zpool_get_config(zhp, &oldconfig);
2571
2572         if (cb->cb_iteration == 1)
2573                 oldconfig = NULL;
2574
2575         verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2576             &newnvroot) == 0);
2577
2578         if (oldconfig == NULL)
2579                 oldnvroot = NULL;
2580         else
2581                 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2582                     &oldnvroot) == 0);
2583
2584         /*
2585          * Print out the statistics for the pool.
2586          */
2587         print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2588
2589         if (cb->cb_verbose)
2590                 print_iostat_separator(cb);
2591
2592         return (0);
2593 }
2594
2595 static int
2596 get_columns(void)
2597 {
2598         struct winsize ws;
2599         int columns = 80;
2600         int error;
2601
2602         if (isatty(STDOUT_FILENO)) {
2603                 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
2604                 if (error == 0)
2605                         columns = ws.ws_col;
2606         } else {
2607                 columns = 999;
2608         }
2609
2610         return (columns);
2611 }
2612
2613 int
2614 get_namewidth(zpool_handle_t *zhp, void *data)
2615 {
2616         iostat_cbdata_t *cb = data;
2617         nvlist_t *config, *nvroot;
2618         int columns;
2619
2620         if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2621                 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2622                     &nvroot) == 0);
2623                 if (!cb->cb_verbose)
2624                         cb->cb_namewidth = strlen(zpool_get_name(zhp));
2625                 else
2626                         cb->cb_namewidth = max_width(zhp, nvroot, 0,
2627                             cb->cb_namewidth);
2628         }
2629
2630         /*
2631          * The width must be at least 10, but may be as large as the
2632          * column width - 42 so that we can still fit in one line.
2633          */
2634         columns = get_columns();
2635
2636         if (cb->cb_namewidth < 10)
2637                 cb->cb_namewidth = 10;
2638         if (cb->cb_namewidth > columns - 42)
2639                 cb->cb_namewidth = columns - 42;
2640
2641         return (0);
2642 }
2643
2644 /*
2645  * Parse the input string, get the 'interval' and 'count' value if there is one.
2646  */
2647 static void
2648 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2649     unsigned long *cnt)
2650 {
2651         unsigned long interval = 0, count = 0;
2652         int argc = *argcp;
2653
2654         /*
2655          * Determine if the last argument is an integer or a pool name
2656          */
2657         if (argc > 0 && isdigit(argv[argc - 1][0])) {
2658                 char *end;
2659
2660                 errno = 0;
2661                 interval = strtoul(argv[argc - 1], &end, 10);
2662
2663                 if (*end == '\0' && errno == 0) {
2664                         if (interval == 0) {
2665                                 (void) fprintf(stderr, gettext("interval "
2666                                     "cannot be zero\n"));
2667                                 usage(B_FALSE);
2668                         }
2669                         /*
2670                          * Ignore the last parameter
2671                          */
2672                         argc--;
2673                 } else {
2674                         /*
2675                          * If this is not a valid number, just plow on.  The
2676                          * user will get a more informative error message later
2677                          * on.
2678                          */
2679                         interval = 0;
2680                 }
2681         }
2682
2683         /*
2684          * If the last argument is also an integer, then we have both a count
2685          * and an interval.
2686          */
2687         if (argc > 0 && isdigit(argv[argc - 1][0])) {
2688                 char *end;
2689
2690                 errno = 0;
2691                 count = interval;
2692                 interval = strtoul(argv[argc - 1], &end, 10);
2693
2694                 if (*end == '\0' && errno == 0) {
2695                         if (interval == 0) {
2696                                 (void) fprintf(stderr, gettext("interval "
2697                                     "cannot be zero\n"));
2698                                 usage(B_FALSE);
2699                         }
2700
2701                         /*
2702                          * Ignore the last parameter
2703                          */
2704                         argc--;
2705                 } else {
2706                         interval = 0;
2707                 }
2708         }
2709
2710         *iv = interval;
2711         *cnt = count;
2712         *argcp = argc;
2713 }
2714
2715 static void
2716 get_timestamp_arg(char c)
2717 {
2718         if (c == 'u')
2719                 timestamp_fmt = UDATE;
2720         else if (c == 'd')
2721                 timestamp_fmt = DDATE;
2722         else
2723                 usage(B_FALSE);
2724 }
2725
2726 /*
2727  * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2728  *
2729  *      -v      Display statistics for individual vdevs
2730  *      -T      Display a timestamp in date(1) or Unix format
2731  *
2732  * This command can be tricky because we want to be able to deal with pool
2733  * creation/destruction as well as vdev configuration changes.  The bulk of this
2734  * processing is handled by the pool_list_* routines in zpool_iter.c.  We rely
2735  * on pool_list_update() to detect the addition of new pools.  Configuration
2736  * changes are all handled within libzfs.
2737  */
2738 int
2739 zpool_do_iostat(int argc, char **argv)
2740 {
2741         int c;
2742         int ret;
2743         int npools;
2744         unsigned long interval = 0, count = 0;
2745         zpool_list_t *list;
2746         boolean_t verbose = B_FALSE;
2747         iostat_cbdata_t cb;
2748
2749         /* check options */
2750         while ((c = getopt(argc, argv, "T:v")) != -1) {
2751                 switch (c) {
2752                 case 'T':
2753                         get_timestamp_arg(*optarg);
2754                         break;
2755                 case 'v':
2756                         verbose = B_TRUE;
2757                         break;
2758                 case '?':
2759                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2760                             optopt);
2761                         usage(B_FALSE);
2762                 }
2763         }
2764
2765         argc -= optind;
2766         argv += optind;
2767
2768         get_interval_count(&argc, argv, &interval, &count);
2769
2770         /*
2771          * Construct the list of all interesting pools.
2772          */
2773         ret = 0;
2774         if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2775                 return (1);
2776
2777         if (pool_list_count(list) == 0 && argc != 0) {
2778                 pool_list_free(list);
2779                 return (1);
2780         }
2781
2782         if (pool_list_count(list) == 0 && interval == 0) {
2783                 pool_list_free(list);
2784                 (void) fprintf(stderr, gettext("no pools available\n"));
2785                 return (1);
2786         }
2787
2788         /*
2789          * Enter the main iostat loop.
2790          */
2791         cb.cb_list = list;
2792         cb.cb_verbose = verbose;
2793         cb.cb_iteration = 0;
2794         cb.cb_namewidth = 0;
2795
2796         for (;;) {
2797                 pool_list_update(list);
2798
2799                 if ((npools = pool_list_count(list)) == 0)
2800                         (void) fprintf(stderr, gettext("no pools available\n"));
2801                 else {
2802                         /*
2803                          * Refresh all statistics.  This is done as an
2804                          * explicit step before calculating the maximum name
2805                          * width, so that any * configuration changes are
2806                          * properly accounted for.
2807                          */
2808                         (void) pool_list_iter(list, B_FALSE, refresh_iostat,
2809                                 &cb);
2810
2811                         /*
2812                          * Iterate over all pools to determine the maximum width
2813                          * for the pool / device name column across all pools.
2814                          */
2815                         cb.cb_namewidth = 0;
2816                         (void) pool_list_iter(list, B_FALSE, get_namewidth,
2817                                 &cb);
2818
2819                         if (timestamp_fmt != NODATE)
2820                                 print_timestamp(timestamp_fmt);
2821
2822                         /*
2823                          * If it's the first time, or verbose mode, print the
2824                          * header.
2825                          */
2826                         if (++cb.cb_iteration == 1 || verbose)
2827                                 print_iostat_header(&cb);
2828
2829                         (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2830
2831                         /*
2832                          * If there's more than one pool, and we're not in
2833                          * verbose mode (which prints a separator for us),
2834                          * then print a separator.
2835                          */
2836                         if (npools > 1 && !verbose)
2837                                 print_iostat_separator(&cb);
2838
2839                         if (verbose)
2840                                 (void) printf("\n");
2841                 }
2842
2843                 /*
2844                  * Flush the output so that redirection to a file isn't buffered
2845                  * indefinitely.
2846                  */
2847                 (void) fflush(stdout);
2848
2849                 if (interval == 0)
2850                         break;
2851
2852                 if (count != 0 && --count == 0)
2853                         break;
2854
2855                 (void) sleep(interval);
2856         }
2857
2858         pool_list_free(list);
2859
2860         return (ret);
2861 }
2862
2863 typedef struct list_cbdata {
2864         boolean_t       cb_verbose;
2865         int             cb_namewidth;
2866         boolean_t       cb_scripted;
2867         zprop_list_t    *cb_proplist;
2868 } list_cbdata_t;
2869
2870 /*
2871  * Given a list of columns to display, output appropriate headers for each one.
2872  */
2873 static void
2874 print_header(list_cbdata_t *cb)
2875 {
2876         zprop_list_t *pl = cb->cb_proplist;
2877         char headerbuf[ZPOOL_MAXPROPLEN];
2878         const char *header;
2879         boolean_t first = B_TRUE;
2880         boolean_t right_justify;
2881         size_t width = 0;
2882
2883         for (; pl != NULL; pl = pl->pl_next) {
2884                 width = pl->pl_width;
2885                 if (first && cb->cb_verbose) {
2886                         /*
2887                          * Reset the width to accommodate the verbose listing
2888                          * of devices.
2889                          */
2890                         width = cb->cb_namewidth;
2891                 }
2892
2893                 if (!first)
2894                         (void) printf("  ");
2895                 else
2896                         first = B_FALSE;
2897
2898                 right_justify = B_FALSE;
2899                 if (pl->pl_prop != ZPROP_INVAL) {
2900                         header = zpool_prop_column_name(pl->pl_prop);
2901                         right_justify = zpool_prop_align_right(pl->pl_prop);
2902                 } else {
2903                         int i;
2904
2905                         for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2906                                 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2907                         headerbuf[i] = '\0';
2908                         header = headerbuf;
2909                 }
2910
2911                 if (pl->pl_next == NULL && !right_justify)
2912                         (void) printf("%s", header);
2913                 else if (right_justify)
2914                         (void) printf("%*s", (int)width, header);
2915                 else
2916                         (void) printf("%-*s", (int)width, header);
2917         }
2918
2919         (void) printf("\n");
2920 }
2921
2922 /*
2923  * Given a pool and a list of properties, print out all the properties according
2924  * to the described layout.
2925  */
2926 static void
2927 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2928 {
2929         zprop_list_t *pl = cb->cb_proplist;
2930         boolean_t first = B_TRUE;
2931         char property[ZPOOL_MAXPROPLEN];
2932         char *propstr;
2933         boolean_t right_justify;
2934         size_t width;
2935
2936         for (; pl != NULL; pl = pl->pl_next) {
2937
2938                 width = pl->pl_width;
2939                 if (first && cb->cb_verbose) {
2940                         /*
2941                          * Reset the width to accommodate the verbose listing
2942                          * of devices.
2943                          */
2944                         width = cb->cb_namewidth;
2945                 }
2946
2947                 if (!first) {
2948                         if (cb->cb_scripted)
2949                                 (void) printf("\t");
2950                         else
2951                                 (void) printf("  ");
2952                 } else {
2953                         first = B_FALSE;
2954                 }
2955
2956                 right_justify = B_FALSE;
2957                 if (pl->pl_prop != ZPROP_INVAL) {
2958                         if (pl->pl_prop == ZPOOL_PROP_EXPANDSZ &&
2959                             zpool_get_prop_int(zhp, pl->pl_prop, NULL) == 0)
2960                                 propstr = "-";
2961                         else if (zpool_get_prop(zhp, pl->pl_prop, property,
2962                             sizeof (property), NULL) != 0)
2963                                 propstr = "-";
2964                         else
2965                                 propstr = property;
2966
2967                         right_justify = zpool_prop_align_right(pl->pl_prop);
2968                 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
2969                     zpool_prop_unsupported(pl->pl_user_prop)) &&
2970                     zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2971                     sizeof (property)) == 0) {
2972                         propstr = property;
2973                 } else {
2974                         propstr = "-";
2975                 }
2976
2977
2978                 /*
2979                  * If this is being called in scripted mode, or if this is the
2980                  * last column and it is left-justified, don't include a width
2981                  * format specifier.
2982                  */
2983                 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2984                         (void) printf("%s", propstr);
2985                 else if (right_justify)
2986                         (void) printf("%*s", (int)width, propstr);
2987                 else
2988                         (void) printf("%-*s", (int)width, propstr);
2989         }
2990
2991         (void) printf("\n");
2992 }
2993
2994 static void
2995 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted)
2996 {
2997         char propval[64];
2998         boolean_t fixed;
2999         size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
3000
3001
3002         if (prop == ZPOOL_PROP_EXPANDSZ && value == 0)
3003                 (void) strlcpy(propval, "-", sizeof (propval));
3004         else if (prop == ZPOOL_PROP_FRAGMENTATION && value == ZFS_FRAG_INVALID)
3005                 (void) strlcpy(propval, "-", sizeof (propval));
3006         else if (prop == ZPOOL_PROP_FRAGMENTATION)
3007                 (void) snprintf(propval, sizeof (propval), "%llu%%",
3008                     (unsigned long long)value);
3009         else
3010                 zfs_nicenum(value, propval, sizeof (propval));
3011
3012         if (scripted)
3013                 (void) printf("\t%s", propval);
3014         else
3015                 (void) printf("  %*s", (int)width, propval);
3016 }
3017
3018 void
3019 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
3020     list_cbdata_t *cb, int depth)
3021 {
3022         nvlist_t **child;
3023         vdev_stat_t *vs;
3024         uint_t c, children;
3025         char *vname;
3026         boolean_t scripted = cb->cb_scripted;
3027
3028         verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3029             (uint64_t **)&vs, &c) == 0);
3030
3031         if (name != NULL) {
3032                 if (scripted)
3033                         (void) printf("\t%s", name);
3034                 else if (strlen(name) + depth > cb->cb_namewidth)
3035                         (void) printf("%*s%s", depth, "", name);
3036                 else
3037                         (void) printf("%*s%s%*s", depth, "", name,
3038                             (int)(cb->cb_namewidth - strlen(name) - depth), "");
3039
3040                 /* only toplevel vdevs have capacity stats */
3041                 if (vs->vs_space == 0) {
3042                         if (scripted)
3043                                 (void) printf("\t-\t-\t-\t-");
3044                         else
3045                                 (void) printf("      -      -      -      -");
3046                 } else {
3047                         print_one_column(ZPOOL_PROP_SIZE, vs->vs_space,
3048                             scripted);
3049                         print_one_column(ZPOOL_PROP_CAPACITY, vs->vs_alloc,
3050                             scripted);
3051                         print_one_column(ZPOOL_PROP_FREE,
3052                             vs->vs_space - vs->vs_alloc, scripted);
3053                         print_one_column(ZPOOL_PROP_FRAGMENTATION,
3054                             vs->vs_fragmentation, scripted);
3055                 }
3056                 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize,
3057                     scripted);
3058                 (void) printf("\n");
3059         }
3060
3061         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3062             &child, &children) != 0)
3063                 return;
3064
3065         for (c = 0; c < children; c++) {
3066                 uint64_t ishole = B_FALSE;
3067
3068                 if (nvlist_lookup_uint64(child[c],
3069                     ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
3070                         continue;
3071
3072                 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3073                 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3074                 free(vname);
3075         }
3076
3077         /*
3078          * Include level 2 ARC devices in iostat output
3079          */
3080         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
3081             &child, &children) != 0)
3082                 return;
3083
3084         if (children > 0) {
3085                 (void) printf("%-*s      -      -      -      -      -      "
3086                     "-\n", cb->cb_namewidth, "cache");
3087                 for (c = 0; c < children; c++) {
3088                         vname = zpool_vdev_name(g_zfs, zhp, child[c],
3089                             B_FALSE);
3090                         print_list_stats(zhp, vname, child[c], cb, depth + 2);
3091                         free(vname);
3092                 }
3093         }
3094 }
3095
3096
3097 /*
3098  * Generic callback function to list a pool.
3099  */
3100 int
3101 list_callback(zpool_handle_t *zhp, void *data)
3102 {
3103         list_cbdata_t *cbp = data;
3104         nvlist_t *config;
3105         nvlist_t *nvroot;
3106
3107         config = zpool_get_config(zhp, NULL);
3108
3109         print_pool(zhp, cbp);
3110         if (!cbp->cb_verbose)
3111                 return (0);
3112
3113         verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3114             &nvroot) == 0);
3115         print_list_stats(zhp, NULL, nvroot, cbp, 0);
3116
3117         return (0);
3118 }
3119
3120 /*
3121  * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3122  *
3123  *      -H      Scripted mode.  Don't display headers, and separate properties
3124  *              by a single tab.
3125  *      -o      List of properties to display.  Defaults to
3126  *              "name,size,allocated,free,capacity,health,altroot"
3127  *      -T      Display a timestamp in date(1) or Unix format
3128  *
3129  * List all pools in the system, whether or not they're healthy.  Output space
3130  * statistics for each one, as well as health status summary.
3131  */
3132 int
3133 zpool_do_list(int argc, char **argv)
3134 {
3135         int c;
3136         int ret = 0;
3137         list_cbdata_t cb = { 0 };
3138         static char default_props[] =
3139             "name,size,allocated,free,fragmentation,capacity,"
3140             "dedupratio,health,altroot";
3141         char *props = default_props;
3142         unsigned long interval = 0, count = 0;
3143         zpool_list_t *list;
3144         boolean_t first = B_TRUE;
3145
3146         /* check options */
3147         while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) {
3148                 switch (c) {
3149                 case 'H':
3150                         cb.cb_scripted = B_TRUE;
3151                         break;
3152                 case 'o':
3153                         props = optarg;
3154                         break;
3155                 case 'T':
3156                         get_timestamp_arg(*optarg);
3157                         break;
3158                 case 'v':
3159                         cb.cb_verbose = B_TRUE;
3160                         break;
3161                 case ':':
3162                         (void) fprintf(stderr, gettext("missing argument for "
3163                             "'%c' option\n"), optopt);
3164                         usage(B_FALSE);
3165                         break;
3166                 case '?':
3167                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3168                             optopt);
3169                         usage(B_FALSE);
3170                 }
3171         }
3172
3173         argc -= optind;
3174         argv += optind;
3175
3176         get_interval_count(&argc, argv, &interval, &count);
3177
3178         if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3179                 usage(B_FALSE);
3180
3181         if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL)
3182                 return (1);
3183
3184         if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3185                 (void) printf(gettext("no pools available\n"));
3186                 zprop_free_list(cb.cb_proplist);
3187                 return (0);
3188         }
3189
3190         for (;;) {
3191                 pool_list_update(list);
3192
3193                 if (pool_list_count(list) == 0)
3194                         break;
3195
3196                 if (timestamp_fmt != NODATE)
3197                         print_timestamp(timestamp_fmt);
3198
3199                 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3200                         print_header(&cb);
3201                         first = B_FALSE;
3202                 }
3203                 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
3204
3205                 if (interval == 0)
3206                         break;
3207
3208                 if (count != 0 && --count == 0)
3209                         break;
3210
3211                 (void) sleep(interval);
3212         }
3213
3214         zprop_free_list(cb.cb_proplist);
3215         return (ret);
3216 }
3217
3218 static int
3219 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3220 {
3221         boolean_t force = B_FALSE;
3222         int c;
3223         nvlist_t *nvroot;
3224         char *poolname, *old_disk, *new_disk;
3225         zpool_handle_t *zhp;
3226         nvlist_t *props = NULL;
3227         char *propval;
3228         int ret;
3229
3230         /* check options */
3231         while ((c = getopt(argc, argv, "fo:")) != -1) {
3232                 switch (c) {
3233                 case 'f':
3234                         force = B_TRUE;
3235                         break;
3236                 case 'o':
3237                         if ((propval = strchr(optarg, '=')) == NULL) {
3238                                 (void) fprintf(stderr, gettext("missing "
3239                                     "'=' for -o option\n"));
3240                                 usage(B_FALSE);
3241                         }
3242                         *propval = '\0';
3243                         propval++;
3244
3245                         if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
3246                             (add_prop_list(optarg, propval, &props, B_TRUE)))
3247                                 usage(B_FALSE);
3248                         break;
3249                 case '?':
3250                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3251                             optopt);
3252                         usage(B_FALSE);
3253                 }
3254         }
3255
3256         argc -= optind;
3257         argv += optind;
3258
3259         /* get pool name and check number of arguments */
3260         if (argc < 1) {
3261                 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3262                 usage(B_FALSE);
3263         }
3264
3265         poolname = argv[0];
3266
3267         if (argc < 2) {
3268                 (void) fprintf(stderr,
3269                     gettext("missing <device> specification\n"));
3270                 usage(B_FALSE);
3271         }
3272
3273         old_disk = argv[1];
3274
3275         if (argc < 3) {
3276                 if (!replacing) {
3277                         (void) fprintf(stderr,
3278                             gettext("missing <new_device> specification\n"));
3279                         usage(B_FALSE);
3280                 }
3281                 new_disk = old_disk;
3282                 argc -= 1;
3283                 argv += 1;
3284         } else {
3285                 new_disk = argv[2];
3286                 argc -= 2;
3287                 argv += 2;
3288         }
3289
3290         if (argc > 1) {
3291                 (void) fprintf(stderr, gettext("too many arguments\n"));
3292                 usage(B_FALSE);
3293         }
3294
3295         if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3296                 return (1);
3297
3298         if (zpool_get_config(zhp, NULL) == NULL) {
3299                 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3300                     poolname);
3301                 zpool_close(zhp);
3302                 return (1);
3303         }
3304
3305         nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE,
3306             argc, argv);
3307         if (nvroot == NULL) {
3308                 zpool_close(zhp);
3309                 return (1);
3310         }
3311
3312         ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3313
3314         nvlist_free(nvroot);
3315         zpool_close(zhp);
3316
3317         return (ret);
3318 }
3319
3320 /*
3321  * zpool replace [-f] <pool> <device> <new_device>
3322  *
3323  *      -f      Force attach, even if <new_device> appears to be in use.
3324  *
3325  * Replace <device> with <new_device>.
3326  */
3327 /* ARGSUSED */
3328 int
3329 zpool_do_replace(int argc, char **argv)
3330 {
3331         return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3332 }
3333
3334 /*
3335  * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
3336  *
3337  *      -f      Force attach, even if <new_device> appears to be in use.
3338  *      -o      Set property=value.
3339  *
3340  * Attach <new_device> to the mirror containing <device>.  If <device> is not
3341  * part of a mirror, then <device> will be transformed into a mirror of
3342  * <device> and <new_device>.  In either case, <new_device> will begin life
3343  * with a DTL of [0, now], and will immediately begin to resilver itself.
3344  */
3345 int
3346 zpool_do_attach(int argc, char **argv)
3347 {
3348         return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3349 }
3350
3351 /*
3352  * zpool detach [-f] <pool> <device>
3353  *
3354  *      -f      Force detach of <device>, even if DTLs argue against it
3355  *              (not supported yet)
3356  *
3357  * Detach a device from a mirror.  The operation will be refused if <device>
3358  * is the last device in the mirror, or if the DTLs indicate that this device
3359  * has the only valid copy of some data.
3360  */
3361 /* ARGSUSED */
3362 int
3363 zpool_do_detach(int argc, char **argv)
3364 {
3365         int c;
3366         char *poolname, *path;
3367         zpool_handle_t *zhp;
3368         int ret;
3369
3370         /* check options */
3371         while ((c = getopt(argc, argv, "f")) != -1) {
3372                 switch (c) {
3373                 case 'f':
3374                 case '?':
3375                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3376                             optopt);
3377                         usage(B_FALSE);
3378                 }
3379         }
3380
3381         argc -= optind;
3382         argv += optind;
3383
3384         /* get pool name and check number of arguments */
3385         if (argc < 1) {
3386                 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3387                 usage(B_FALSE);
3388         }
3389
3390         if (argc < 2) {
3391                 (void) fprintf(stderr,
3392                     gettext("missing <device> specification\n"));
3393                 usage(B_FALSE);
3394         }
3395
3396         poolname = argv[0];
3397         path = argv[1];
3398
3399         if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3400                 return (1);
3401
3402         ret = zpool_vdev_detach(zhp, path);
3403
3404         zpool_close(zhp);
3405
3406         return (ret);
3407 }
3408
3409 /*
3410  * zpool split [-n] [-o prop=val] ...
3411  *              [-o mntopt] ...
3412  *              [-R altroot] <pool> <newpool> [<device> ...]
3413  *
3414  *      -n      Do not split the pool, but display the resulting layout if
3415  *              it were to be split.
3416  *      -o      Set property=value, or set mount options.
3417  *      -R      Mount the split-off pool under an alternate root.
3418  *
3419  * Splits the named pool and gives it the new pool name.  Devices to be split
3420  * off may be listed, provided that no more than one device is specified
3421  * per top-level vdev mirror.  The newly split pool is left in an exported
3422  * state unless -R is specified.
3423  *
3424  * Restrictions: the top-level of the pool pool must only be made up of
3425  * mirrors; all devices in the pool must be healthy; no device may be
3426  * undergoing a resilvering operation.
3427  */
3428 int
3429 zpool_do_split(int argc, char **argv)
3430 {
3431         char *srcpool, *newpool, *propval;
3432         char *mntopts = NULL;
3433         splitflags_t flags;
3434         int c, ret = 0;
3435         zpool_handle_t *zhp;
3436         nvlist_t *config, *props = NULL;
3437
3438         flags.dryrun = B_FALSE;
3439         flags.import = B_FALSE;
3440
3441         /* check options */
3442         while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3443                 switch (c) {
3444                 case 'R':
3445                         flags.import = B_TRUE;
3446                         if (add_prop_list(
3447                             zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3448                             &props, B_TRUE) != 0) {
3449                                 if (props)
3450                                         nvlist_free(props);
3451                                 usage(B_FALSE);
3452                         }
3453                         break;
3454                 case 'n':
3455                         flags.dryrun = B_TRUE;
3456                         break;
3457                 case 'o':
3458                         if ((propval = strchr(optarg, '=')) != NULL) {
3459                                 *propval = '\0';
3460                                 propval++;
3461                                 if (add_prop_list(optarg, propval,
3462                                     &props, B_TRUE) != 0) {
3463                                         if (props)
3464                                                 nvlist_free(props);
3465                                         usage(B_FALSE);
3466                                 }
3467                         } else {
3468                                 mntopts = optarg;
3469                         }
3470                         break;
3471                 case ':':
3472                         (void) fprintf(stderr, gettext("missing argument for "
3473                             "'%c' option\n"), optopt);
3474                         usage(B_FALSE);
3475                         break;
3476                 case '?':
3477                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3478                             optopt);
3479                         usage(B_FALSE);
3480                         break;
3481                 }
3482         }
3483
3484         if (!flags.import && mntopts != NULL) {
3485                 (void) fprintf(stderr, gettext("setting mntopts is only "
3486                     "valid when importing the pool\n"));
3487                 usage(B_FALSE);
3488         }
3489
3490         argc -= optind;
3491         argv += optind;
3492
3493         if (argc < 1) {
3494                 (void) fprintf(stderr, gettext("Missing pool name\n"));
3495                 usage(B_FALSE);
3496         }
3497         if (argc < 2) {
3498                 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3499                 usage(B_FALSE);
3500         }
3501
3502         srcpool = argv[0];
3503         newpool = argv[1];
3504
3505         argc -= 2;
3506         argv += 2;
3507
3508         if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3509                 return (1);
3510
3511         config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3512         if (config == NULL) {
3513                 ret = 1;
3514         } else {
3515                 if (flags.dryrun) {
3516                         (void) printf(gettext("would create '%s' with the "
3517                             "following layout:\n\n"), newpool);
3518                         print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3519                 }
3520                 nvlist_free(config);
3521         }
3522
3523         zpool_close(zhp);
3524
3525         if (ret != 0 || flags.dryrun || !flags.import)
3526                 return (ret);
3527
3528         /*
3529          * The split was successful. Now we need to open the new
3530          * pool and import it.
3531          */
3532         if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3533                 return (1);
3534         if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3535             zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3536                 ret = 1;
3537                 (void) fprintf(stderr, gettext("Split was successful, but "
3538                     "the datasets could not all be mounted\n"));
3539                 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3540                     "different altroot\n"), "zpool import");
3541         }
3542         zpool_close(zhp);
3543
3544         return (ret);
3545 }
3546
3547
3548
3549 /*
3550  * zpool online <pool> <device> ...
3551  */
3552 int
3553 zpool_do_online(int argc, char **argv)
3554 {
3555         int c, i;
3556         char *poolname;
3557         zpool_handle_t *zhp;
3558         int ret = 0;
3559         vdev_state_t newstate;
3560         int flags = 0;
3561
3562         /* check options */
3563         while ((c = getopt(argc, argv, "et")) != -1) {
3564                 switch (c) {
3565                 case 'e':
3566                         flags |= ZFS_ONLINE_EXPAND;
3567                         break;
3568                 case 't':
3569                 case '?':
3570                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3571                             optopt);
3572                         usage(B_FALSE);
3573                 }
3574         }
3575
3576         argc -= optind;
3577         argv += optind;
3578
3579         /* get pool name and check number of arguments */
3580         if (argc < 1) {
3581                 (void) fprintf(stderr, gettext("missing pool name\n"));
3582                 usage(B_FALSE);
3583         }
3584         if (argc < 2) {
3585                 (void) fprintf(stderr, gettext("missing device name\n"));
3586                 usage(B_FALSE);
3587         }
3588
3589         poolname = argv[0];
3590
3591         if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3592                 return (1);
3593
3594         for (i = 1; i < argc; i++) {
3595                 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3596                         if (newstate != VDEV_STATE_HEALTHY) {
3597                                 (void) printf(gettext("warning: device '%s' "
3598                                     "onlined, but remains in faulted state\n"),
3599                                     argv[i]);
3600                                 if (newstate == VDEV_STATE_FAULTED)
3601                                         (void) printf(gettext("use 'zpool "
3602                                             "clear' to restore a faulted "
3603                                             "device\n"));
3604                                 else
3605                                         (void) printf(gettext("use 'zpool "
3606                                             "replace' to replace devices "
3607                                             "that are no longer present\n"));
3608                         }
3609                 } else {
3610                         ret = 1;
3611                 }
3612         }
3613
3614         zpool_close(zhp);
3615
3616         return (ret);
3617 }
3618
3619 /*
3620  * zpool offline [-ft] <pool> <device> ...
3621  *
3622  *      -f      Force the device into the offline state, even if doing
3623  *              so would appear to compromise pool availability.
3624  *              (not supported yet)
3625  *
3626  *      -t      Only take the device off-line temporarily.  The offline
3627  *              state will not be persistent across reboots.
3628  */
3629 /* ARGSUSED */
3630 int
3631 zpool_do_offline(int argc, char **argv)
3632 {
3633         int c, i;
3634         char *poolname;
3635         zpool_handle_t *zhp;
3636         int ret = 0;
3637         boolean_t istmp = B_FALSE;
3638
3639         /* check options */
3640         while ((c = getopt(argc, argv, "ft")) != -1) {
3641                 switch (c) {
3642                 case 't':
3643                         istmp = B_TRUE;
3644                         break;
3645                 case 'f':
3646                 case '?':
3647                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3648                             optopt);
3649                         usage(B_FALSE);
3650                 }
3651         }
3652
3653         argc -= optind;
3654         argv += optind;
3655
3656         /* get pool name and check number of arguments */
3657         if (argc < 1) {
3658                 (void) fprintf(stderr, gettext("missing pool name\n"));
3659                 usage(B_FALSE);
3660         }
3661         if (argc < 2) {
3662                 (void) fprintf(stderr, gettext("missing device name\n"));
3663                 usage(B_FALSE);
3664         }
3665
3666         poolname = argv[0];
3667
3668         if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3669                 return (1);
3670
3671         for (i = 1; i < argc; i++) {
3672                 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3673                         ret = 1;
3674         }
3675
3676         zpool_close(zhp);
3677
3678         return (ret);
3679 }
3680
3681 /*
3682  * zpool clear <pool> [device]
3683  *
3684  * Clear all errors associated with a pool or a particular device.
3685  */
3686 int
3687 zpool_do_clear(int argc, char **argv)
3688 {
3689         int c;
3690         int ret = 0;
3691         boolean_t dryrun = B_FALSE;
3692         boolean_t do_rewind = B_FALSE;
3693         boolean_t xtreme_rewind = B_FALSE;
3694         uint32_t rewind_policy = ZPOOL_NO_REWIND;
3695         nvlist_t *policy = NULL;
3696         zpool_handle_t *zhp;
3697         char *pool, *device;
3698
3699         /* check options */
3700         while ((c = getopt(argc, argv, "FnX")) != -1) {
3701                 switch (c) {
3702                 case 'F':
3703                         do_rewind = B_TRUE;
3704                         break;
3705                 case 'n':
3706                         dryrun = B_TRUE;
3707                         break;
3708                 case 'X':
3709                         xtreme_rewind = B_TRUE;
3710                         break;
3711                 case '?':
3712                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3713                             optopt);
3714                         usage(B_FALSE);
3715                 }
3716         }
3717
3718         argc -= optind;
3719         argv += optind;
3720
3721         if (argc < 1) {
3722                 (void) fprintf(stderr, gettext("missing pool name\n"));
3723                 usage(B_FALSE);
3724         }
3725
3726         if (argc > 2) {
3727                 (void) fprintf(stderr, gettext("too many arguments\n"));
3728                 usage(B_FALSE);
3729         }
3730
3731         if ((dryrun || xtreme_rewind) && !do_rewind) {
3732                 (void) fprintf(stderr,
3733                     gettext("-n or -X only meaningful with -F\n"));
3734                 usage(B_FALSE);
3735         }
3736         if (dryrun)
3737                 rewind_policy = ZPOOL_TRY_REWIND;
3738         else if (do_rewind)
3739                 rewind_policy = ZPOOL_DO_REWIND;
3740         if (xtreme_rewind)
3741                 rewind_policy |= ZPOOL_EXTREME_REWIND;
3742
3743         /* In future, further rewind policy choices can be passed along here */
3744         if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3745             nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3746                 return (1);
3747
3748         pool = argv[0];
3749         device = argc == 2 ? argv[1] : NULL;
3750
3751         if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3752                 nvlist_free(policy);
3753                 return (1);
3754         }
3755
3756         if (zpool_clear(zhp, device, policy) != 0)
3757                 ret = 1;
3758
3759         zpool_close(zhp);
3760
3761         nvlist_free(policy);
3762
3763         return (ret);
3764 }
3765
3766 /*
3767  * zpool reguid <pool>
3768  */
3769 int
3770 zpool_do_reguid(int argc, char **argv)
3771 {
3772         int c;
3773         char *poolname;
3774         zpool_handle_t *zhp;
3775         int ret = 0;
3776
3777         /* check options */
3778         while ((c = getopt(argc, argv, "")) != -1) {
3779                 switch (c) {
3780                 case '?':
3781                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3782                             optopt);
3783                         usage(B_FALSE);
3784                 }
3785         }
3786
3787         argc -= optind;
3788         argv += optind;
3789
3790         /* get pool name and check number of arguments */
3791         if (argc < 1) {
3792                 (void) fprintf(stderr, gettext("missing pool name\n"));
3793                 usage(B_FALSE);
3794         }
3795
3796         if (argc > 1) {
3797                 (void) fprintf(stderr, gettext("too many arguments\n"));
3798                 usage(B_FALSE);
3799         }
3800
3801         poolname = argv[0];
3802         if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3803                 return (1);
3804
3805         ret = zpool_reguid(zhp);
3806
3807         zpool_close(zhp);
3808         return (ret);
3809 }
3810
3811
3812 /*
3813  * zpool reopen <pool>
3814  *
3815  * Reopen the pool so that the kernel can update the sizes of all vdevs.
3816  */
3817 int
3818 zpool_do_reopen(int argc, char **argv)
3819 {
3820         int c;
3821         int ret = 0;
3822         zpool_handle_t *zhp;
3823         char *pool;
3824
3825         /* check options */
3826         while ((c = getopt(argc, argv, "")) != -1) {
3827                 switch (c) {
3828                 case '?':
3829                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3830                             optopt);
3831                         usage(B_FALSE);
3832                 }
3833         }
3834
3835         argc--;
3836         argv++;
3837
3838         if (argc < 1) {
3839                 (void) fprintf(stderr, gettext("missing pool name\n"));
3840                 usage(B_FALSE);
3841         }
3842
3843         if (argc > 1) {
3844                 (void) fprintf(stderr, gettext("too many arguments\n"));
3845                 usage(B_FALSE);
3846         }
3847
3848         pool = argv[0];
3849         if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3850                 return (1);
3851
3852         ret = zpool_reopen(zhp);
3853         zpool_close(zhp);
3854         return (ret);
3855 }
3856
3857 typedef struct scrub_cbdata {
3858         int     cb_type;
3859         int     cb_argc;
3860         char    **cb_argv;
3861 } scrub_cbdata_t;
3862
3863 int
3864 scrub_callback(zpool_handle_t *zhp, void *data)
3865 {
3866         scrub_cbdata_t *cb = data;
3867         int err;
3868
3869         /*
3870          * Ignore faulted pools.
3871          */
3872         if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3873                 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3874                     "currently unavailable\n"), zpool_get_name(zhp));
3875                 return (1);
3876         }
3877
3878         err = zpool_scan(zhp, cb->cb_type);
3879
3880         return (err != 0);
3881 }
3882
3883 /*
3884  * zpool scrub [-s] <pool> ...
3885  *
3886  *      -s      Stop.  Stops any in-progress scrub.
3887  */
3888 int
3889 zpool_do_scrub(int argc, char **argv)
3890 {
3891         int c;
3892         scrub_cbdata_t cb;
3893
3894         cb.cb_type = POOL_SCAN_SCRUB;
3895
3896         /* check options */
3897         while ((c = getopt(argc, argv, "s")) != -1) {
3898                 switch (c) {
3899                 case 's':
3900                         cb.cb_type = POOL_SCAN_NONE;
3901                         break;
3902                 case '?':
3903                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3904                             optopt);
3905                         usage(B_FALSE);
3906                 }
3907         }
3908
3909         cb.cb_argc = argc;
3910         cb.cb_argv = argv;
3911         argc -= optind;
3912         argv += optind;
3913
3914         if (argc < 1) {
3915                 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3916                 usage(B_FALSE);
3917         }
3918
3919         return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3920 }
3921
3922 typedef struct status_cbdata {
3923         int             cb_count;
3924         boolean_t       cb_allpools;
3925         boolean_t       cb_verbose;
3926         boolean_t       cb_explain;
3927         boolean_t       cb_first;
3928         boolean_t       cb_dedup_stats;
3929 } status_cbdata_t;
3930
3931 /*
3932  * Print out detailed scrub status.
3933  */
3934 void
3935 print_scan_status(pool_scan_stat_t *ps)
3936 {
3937         time_t start, end;
3938         uint64_t elapsed, mins_left, hours_left;
3939         uint64_t pass_exam, examined, total;
3940         uint_t rate;
3941         double fraction_done;
3942         char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3943
3944         (void) printf(gettext("  scan: "));
3945
3946         /* If there's never been a scan, there's not much to say. */
3947         if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3948             ps->pss_func >= POOL_SCAN_FUNCS) {
3949                 (void) printf(gettext("none requested\n"));
3950                 return;
3951         }
3952
3953         start = ps->pss_start_time;
3954         end = ps->pss_end_time;
3955         zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3956
3957         assert(ps->pss_func == POOL_SCAN_SCRUB ||
3958             ps->pss_func == POOL_SCAN_RESILVER);
3959         /*
3960          * Scan is finished or canceled.
3961          */
3962         if (ps->pss_state == DSS_FINISHED) {
3963                 uint64_t minutes_taken = (end - start) / 60;
3964                 char *fmt = NULL;
3965
3966                 if (ps->pss_func == POOL_SCAN_SCRUB) {
3967                         fmt = gettext("scrub repaired %s in %lluh%um with "
3968                             "%llu errors on %s");
3969                 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3970                         fmt = gettext("resilvered %s in %lluh%um with "
3971                             "%llu errors on %s");
3972                 }
3973                 /* LINTED */
3974                 (void) printf(fmt, processed_buf,
3975                     (u_longlong_t)(minutes_taken / 60),
3976                     (uint_t)(minutes_taken % 60),
3977                     (u_longlong_t)ps->pss_errors,
3978                     ctime((time_t *)&end));
3979                 return;
3980         } else if (ps->pss_state == DSS_CANCELED) {
3981                 if (ps->pss_func == POOL_SCAN_SCRUB) {
3982                         (void) printf(gettext("scrub canceled on %s"),
3983                             ctime(&end));
3984                 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3985                         (void) printf(gettext("resilver canceled on %s"),
3986                             ctime(&end));
3987                 }
3988                 return;
3989         }
3990
3991         assert(ps->pss_state == DSS_SCANNING);
3992
3993         /*
3994          * Scan is in progress.
3995          */
3996         if (ps->pss_func == POOL_SCAN_SCRUB) {
3997                 (void) printf(gettext("scrub in progress since %s"),
3998                     ctime(&start));
3999         } else if (ps->pss_func == POOL_SCAN_RESILVER) {
4000                 (void) printf(gettext("resilver in progress since %s"),
4001                     ctime(&start));
4002         }
4003
4004         examined = ps->pss_examined ? ps->pss_examined : 1;
4005         total = ps->pss_to_examine;
4006         fraction_done = (double)examined / total;
4007
4008         /* elapsed time for this pass */
4009         elapsed = time(NULL) - ps->pss_pass_start;
4010         elapsed = elapsed ? elapsed : 1;
4011         pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
4012         rate = pass_exam / elapsed;
4013         rate = rate ? rate : 1;
4014         mins_left = ((total - examined) / rate) / 60;
4015         hours_left = mins_left / 60;
4016
4017         zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
4018         zfs_nicenum(total, total_buf, sizeof (total_buf));
4019         zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
4020
4021         /*
4022          * do not print estimated time if hours_left is more than 30 days
4023          */
4024         (void) printf(gettext("    %s scanned out of %s at %s/s"),
4025             examined_buf, total_buf, rate_buf);
4026         if (hours_left < (30 * 24)) {
4027                 (void) printf(gettext(", %lluh%um to go\n"),
4028                     (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
4029         } else {
4030                 (void) printf(gettext(
4031                     ", (scan is slow, no estimated time)\n"));
4032         }
4033
4034         if (ps->pss_func == POOL_SCAN_RESILVER) {
4035                 (void) printf(gettext("    %s resilvered, %.2f%% done\n"),
4036                     processed_buf, 100 * fraction_done);
4037         } else if (ps->pss_func == POOL_SCAN_SCRUB) {
4038                 (void) printf(gettext("    %s repaired, %.2f%% done\n"),
4039                     processed_buf, 100 * fraction_done);
4040         }
4041 }
4042
4043 static void
4044 print_error_log(zpool_handle_t *zhp)
4045 {
4046         nvlist_t *nverrlist = NULL;
4047         nvpair_t *elem;
4048         char *pathname;
4049         size_t len = MAXPATHLEN * 2;
4050
4051         if (zpool_get_errlog(zhp, &nverrlist) != 0) {
4052                 (void) printf("errors: List of errors unavailable "
4053                     "(insufficient privileges)\n");
4054                 return;
4055         }
4056
4057         (void) printf("errors: Permanent errors have been "
4058             "detected in the following files:\n\n");
4059
4060         pathname = safe_malloc(len);
4061         elem = NULL;
4062         while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
4063                 nvlist_t *nv;
4064                 uint64_t dsobj, obj;
4065
4066                 verify(nvpair_value_nvlist(elem, &nv) == 0);
4067                 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
4068                     &dsobj) == 0);
4069                 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
4070                     &obj) == 0);
4071                 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
4072                 (void) printf("%7s %s\n", "", pathname);
4073         }
4074         free(pathname);
4075         nvlist_free(nverrlist);
4076 }
4077
4078 static void
4079 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
4080     int namewidth)
4081 {
4082         uint_t i;
4083         char *name;
4084
4085         if (nspares == 0)
4086                 return;
4087
4088         (void) printf(gettext("\tspares\n"));
4089
4090         for (i = 0; i < nspares; i++) {
4091                 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
4092                 print_status_config(zhp, name, spares[i],
4093                     namewidth, 2, B_TRUE);
4094                 free(name);
4095         }
4096 }
4097
4098 static void
4099 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
4100     int namewidth)
4101 {
4102         uint_t i;
4103         char *name;
4104
4105         if (nl2cache == 0)
4106                 return;
4107
4108         (void) printf(gettext("\tcache\n"));
4109
4110         for (i = 0; i < nl2cache; i++) {
4111                 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
4112                 print_status_config(zhp, name, l2cache[i],
4113                     namewidth, 2, B_FALSE);
4114                 free(name);
4115         }
4116 }
4117
4118 static void
4119 print_dedup_stats(nvlist_t *config)
4120 {
4121         ddt_histogram_t *ddh;
4122         ddt_stat_t *dds;
4123         ddt_object_t *ddo;
4124         uint_t c;
4125
4126         /*
4127          * If the pool was faulted then we may not have been able to
4128          * obtain the config. Otherwise, if we have anything in the dedup
4129          * table continue processing the stats.
4130          */
4131         if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
4132             (uint64_t **)&ddo, &c) != 0)
4133                 return;
4134
4135         (void) printf("\n");
4136         (void) printf(gettext(" dedup: "));
4137         if (ddo->ddo_count == 0) {
4138                 (void) printf(gettext("no DDT entries\n"));
4139                 return;
4140         }
4141
4142         (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4143             (u_longlong_t)ddo->ddo_count,
4144             (u_longlong_t)ddo->ddo_dspace,
4145             (u_longlong_t)ddo->ddo_mspace);
4146
4147         verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
4148             (uint64_t **)&dds, &c) == 0);
4149         verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
4150             (uint64_t **)&ddh, &c) == 0);
4151         zpool_dump_ddt(dds, ddh);
4152 }
4153
4154 /*
4155  * Display a summary of pool status.  Displays a summary such as:
4156  *
4157  *        pool: tank
4158  *      status: DEGRADED
4159  *      reason: One or more devices ...
4160  *         see: http://zfsonlinux.org/msg/ZFS-xxxx-01
4161  *      config:
4162  *              mirror          DEGRADED
4163  *                c1t0d0        OK
4164  *                c2t0d0        UNAVAIL
4165  *
4166  * When given the '-v' option, we print out the complete config.  If the '-e'
4167  * option is specified, then we print out error rate information as well.
4168  */
4169 int
4170 status_callback(zpool_handle_t *zhp, void *data)
4171 {
4172         status_cbdata_t *cbp = data;
4173         nvlist_t *config, *nvroot;
4174         char *msgid;
4175         zpool_status_t reason;
4176         zpool_errata_t errata;
4177         const char *health;
4178         uint_t c;
4179         vdev_stat_t *vs;
4180
4181         config = zpool_get_config(zhp, NULL);
4182         reason = zpool_get_status(zhp, &msgid, &errata);
4183
4184         cbp->cb_count++;
4185
4186         /*
4187          * If we were given 'zpool status -x', only report those pools with
4188          * problems.
4189          */
4190         if (cbp->cb_explain &&
4191             (reason == ZPOOL_STATUS_OK ||
4192             reason == ZPOOL_STATUS_VERSION_OLDER ||
4193             reason == ZPOOL_STATUS_FEAT_DISABLED)) {
4194                 if (!cbp->cb_allpools) {
4195                         (void) printf(gettext("pool '%s' is healthy\n"),
4196                             zpool_get_name(zhp));
4197                         if (cbp->cb_first)
4198                                 cbp->cb_first = B_FALSE;
4199                 }
4200                 return (0);
4201         }
4202
4203         if (cbp->cb_first)
4204                 cbp->cb_first = B_FALSE;
4205         else
4206                 (void) printf("\n");
4207
4208         verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4209             &nvroot) == 0);
4210         verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
4211             (uint64_t **)&vs, &c) == 0);
4212         health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4213
4214         (void) printf(gettext("  pool: %s\n"), zpool_get_name(zhp));
4215         (void) printf(gettext(" state: %s\n"), health);
4216
4217         switch (reason) {
4218         case ZPOOL_STATUS_MISSING_DEV_R:
4219                 (void) printf(gettext("status: One or more devices could not "
4220                     "be opened.  Sufficient replicas exist for\n\tthe pool to "
4221                     "continue functioning in a degraded state.\n"));
4222                 (void) printf(gettext("action: Attach the missing device and "
4223                     "online it using 'zpool online'.\n"));
4224                 break;
4225
4226         case ZPOOL_STATUS_MISSING_DEV_NR:
4227                 (void) printf(gettext("status: One or more devices could not "
4228                     "be opened.  There are insufficient\n\treplicas for the "
4229                     "pool to continue functioning.\n"));
4230                 (void) printf(gettext("action: Attach the missing device and "
4231                     "online it using 'zpool online'.\n"));
4232                 break;
4233
4234         case ZPOOL_STATUS_CORRUPT_LABEL_R:
4235                 (void) printf(gettext("status: One or more devices could not "
4236                     "be used because the label is missing or\n\tinvalid.  "
4237                     "Sufficient replicas exist for the pool to continue\n\t"
4238                     "functioning in a degraded state.\n"));
4239                 (void) printf(gettext("action: Replace the device using "
4240                     "'zpool replace'.\n"));
4241                 break;
4242
4243         case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4244                 (void) printf(gettext("status: One or more devices could not "
4245                     "be used because the label is missing \n\tor invalid.  "
4246                     "There are insufficient replicas for the pool to "
4247                     "continue\n\tfunctioning.\n"));
4248                 zpool_explain_recover(zpool_get_handle(zhp),
4249                     zpool_get_name(zhp), reason, config);
4250                 break;
4251
4252         case ZPOOL_STATUS_FAILING_DEV:
4253                 (void) printf(gettext("status: One or more devices has "
4254                     "experienced an unrecoverable error.  An\n\tattempt was "
4255                     "made to correct the error.  Applications are "
4256                     "unaffected.\n"));
4257                 (void) printf(gettext("action: Determine if the device needs "
4258                     "to be replaced, and clear the errors\n\tusing "
4259                     "'zpool clear' or replace the device with 'zpool "
4260                     "replace'.\n"));
4261                 break;
4262
4263         case ZPOOL_STATUS_OFFLINE_DEV:
4264                 (void) printf(gettext("status: One or more devices has "
4265                     "been taken offline by the administrator.\n\tSufficient "
4266                     "replicas exist for the pool to continue functioning in "
4267                     "a\n\tdegraded state.\n"));
4268                 (void) printf(gettext("action: Online the device using "
4269                     "'zpool online' or replace the device with\n\t'zpool "
4270                     "replace'.\n"));
4271                 break;
4272
4273         case ZPOOL_STATUS_REMOVED_DEV:
4274                 (void) printf(gettext("status: One or more devices has "
4275                     "been removed by the administrator.\n\tSufficient "
4276                     "replicas exist for the pool to continue functioning in "
4277                     "a\n\tdegraded state.\n"));
4278                 (void) printf(gettext("action: Online the device using "
4279                     "'zpool online' or replace the device with\n\t'zpool "
4280                     "replace'.\n"));
4281                 break;
4282
4283         case ZPOOL_STATUS_RESILVERING:
4284                 (void) printf(gettext("status: One or more devices is "
4285                     "currently being resilvered.  The pool will\n\tcontinue "
4286                     "to function, possibly in a degraded state.\n"));
4287                 (void) printf(gettext("action: Wait for the resilver to "
4288                     "complete.\n"));
4289                 break;
4290
4291         case ZPOOL_STATUS_CORRUPT_DATA:
4292                 (void) printf(gettext("status: One or more devices has "
4293                     "experienced an error resulting in data\n\tcorruption.  "
4294                     "Applications may be affected.\n"));
4295                 (void) printf(gettext("action: Restore the file in question "
4296                     "if possible.  Otherwise restore the\n\tentire pool from "
4297                     "backup.\n"));
4298                 break;
4299
4300         case ZPOOL_STATUS_CORRUPT_POOL:
4301                 (void) printf(gettext("status: The pool metadata is corrupted "
4302                     "and the pool cannot be opened.\n"));
4303                 zpool_explain_recover(zpool_get_handle(zhp),
4304                     zpool_get_name(zhp), reason, config);
4305                 break;
4306
4307         case ZPOOL_STATUS_VERSION_OLDER:
4308                 (void) printf(gettext("status: The pool is formatted using a "
4309                     "legacy on-disk format.  The pool can\n\tstill be used, "
4310                     "but some features are unavailable.\n"));
4311                 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4312                     "upgrade'.  Once this is done, the\n\tpool will no longer "
4313                     "be accessible on software that does not support\n\t"
4314                     "feature flags.\n"));
4315                 break;
4316
4317         case ZPOOL_STATUS_VERSION_NEWER:
4318                 (void) printf(gettext("status: The pool has been upgraded to a "
4319                     "newer, incompatible on-disk version.\n\tThe pool cannot "
4320                     "be accessed on this system.\n"));
4321                 (void) printf(gettext("action: Access the pool from a system "
4322                     "running more recent software, or\n\trestore the pool from "
4323                     "backup.\n"));
4324                 break;
4325
4326         case ZPOOL_STATUS_FEAT_DISABLED:
4327                 (void) printf(gettext("status: Some supported features are not "
4328                     "enabled on the pool. The pool can\n\tstill be used, but "
4329                     "some features are unavailable.\n"));
4330                 (void) printf(gettext("action: Enable all features using "
4331                     "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4332                     "longer be accessible by software that does not support\n\t"
4333                     "the features. See zpool-features(5) for details.\n"));
4334                 break;
4335
4336         case ZPOOL_STATUS_UNSUP_FEAT_READ:
4337                 (void) printf(gettext("status: The pool cannot be accessed on "
4338                     "this system because it uses the\n\tfollowing feature(s) "
4339                     "not supported on this system:\n"));
4340                 zpool_print_unsup_feat(config);
4341                 (void) printf("\n");
4342                 (void) printf(gettext("action: Access the pool from a system "
4343                     "that supports the required feature(s),\n\tor restore the "
4344                     "pool from backup.\n"));
4345                 break;
4346
4347         case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4348                 (void) printf(gettext("status: The pool can only be accessed "
4349                     "in read-only mode on this system. It\n\tcannot be "
4350                     "accessed in read-write mode because it uses the "
4351                     "following\n\tfeature(s) not supported on this system:\n"));
4352                 zpool_print_unsup_feat(config);
4353                 (void) printf("\n");
4354                 (void) printf(gettext("action: The pool cannot be accessed in "
4355                     "read-write mode. Import the pool with\n"
4356                     "\t\"-o readonly=on\", access the pool from a system that "
4357                     "supports the\n\trequired feature(s), or restore the "
4358                     "pool from backup.\n"));
4359                 break;
4360
4361         case ZPOOL_STATUS_FAULTED_DEV_R:
4362                 (void) printf(gettext("status: One or more devices are "
4363                     "faulted in response to persistent errors.\n\tSufficient "
4364                     "replicas exist for the pool to continue functioning "
4365                     "in a\n\tdegraded state.\n"));
4366                 (void) printf(gettext("action: Replace the faulted device, "
4367                     "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4368                 break;
4369
4370         case ZPOOL_STATUS_FAULTED_DEV_NR:
4371                 (void) printf(gettext("status: One or more devices are "
4372                     "faulted in response to persistent errors.  There are "
4373                     "insufficient replicas for the pool to\n\tcontinue "
4374                     "functioning.\n"));
4375                 (void) printf(gettext("action: Destroy and re-create the pool "
4376                     "from a backup source.  Manually marking the device\n"
4377                     "\trepaired using 'zpool clear' may allow some data "
4378                     "to be recovered.\n"));
4379                 break;
4380
4381         case ZPOOL_STATUS_IO_FAILURE_WAIT:
4382         case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4383                 (void) printf(gettext("status: One or more devices are "
4384                     "faulted in response to IO failures.\n"));
4385                 (void) printf(gettext("action: Make sure the affected devices "
4386                     "are connected, then run 'zpool clear'.\n"));
4387                 break;
4388
4389         case ZPOOL_STATUS_BAD_LOG:
4390                 (void) printf(gettext("status: An intent log record "
4391                     "could not be read.\n"
4392                     "\tWaiting for adminstrator intervention to fix the "
4393                     "faulted pool.\n"));
4394                 (void) printf(gettext("action: Either restore the affected "
4395                     "device(s) and run 'zpool online',\n"
4396                     "\tor ignore the intent log records by running "
4397                     "'zpool clear'.\n"));
4398                 break;
4399
4400         case ZPOOL_STATUS_HOSTID_MISMATCH:
4401                 (void) printf(gettext("status: Mismatch between pool hostid "
4402                     "and system hostid on imported pool.\n\tThis pool was "
4403                     "previously imported into a system with a different "
4404                     "hostid,\n\tand then was verbatim imported into this "
4405                     "system.\n"));
4406                 (void) printf(gettext("action: Export this pool on all systems "
4407                     "on which it is imported.\n"
4408                     "\tThen import it to correct the mismatch.\n"));
4409                 break;
4410
4411         case ZPOOL_STATUS_ERRATA:
4412                 (void) printf(gettext("status: Errata #%d detected.\n"),
4413                     errata);
4414
4415                 switch (errata) {
4416                 case ZPOOL_ERRATA_NONE:
4417                         break;
4418
4419                 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
4420                         (void) printf(gettext("action: To correct the issue "
4421                             "run 'zpool scrub'.\n"));
4422                         break;
4423
4424                 default:
4425                         /*
4426                          * All errata which allow the pool to be imported
4427                          * must contain an action message.
4428                          */
4429                         assert(0);
4430                 }
4431                 break;
4432
4433         default:
4434                 /*
4435                  * The remaining errors can't actually be generated, yet.
4436                  */
4437                 assert(reason == ZPOOL_STATUS_OK);
4438         }
4439
4440         if (msgid != NULL)
4441                 (void) printf(gettext("   see: http://zfsonlinux.org/msg/%s\n"),
4442                     msgid);
4443
4444         if (config != NULL) {
4445                 int namewidth;
4446                 uint64_t nerr;
4447                 nvlist_t **spares, **l2cache;
4448                 uint_t nspares, nl2cache;
4449                 pool_scan_stat_t *ps = NULL;
4450
4451                 (void) nvlist_lookup_uint64_array(nvroot,
4452                     ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4453                 print_scan_status(ps);
4454
4455                 namewidth = max_width(zhp, nvroot, 0, 0);
4456                 if (namewidth < 10)
4457                         namewidth = 10;
4458
4459                 (void) printf(gettext("config:\n\n"));
4460                 (void) printf(gettext("\t%-*s  %-8s %5s %5s %5s\n"), namewidth,
4461                     "NAME", "STATE", "READ", "WRITE", "CKSUM");
4462                 print_status_config(zhp, zpool_get_name(zhp), nvroot,
4463                     namewidth, 0, B_FALSE);
4464
4465                 if (num_logs(nvroot) > 0)
4466                         print_logs(zhp, nvroot, namewidth, B_TRUE);
4467                 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4468                     &l2cache, &nl2cache) == 0)
4469                         print_l2cache(zhp, l2cache, nl2cache, namewidth);
4470
4471                 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4472                     &spares, &nspares) == 0)
4473                         print_spares(zhp, spares, nspares, namewidth);
4474
4475                 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4476                     &nerr) == 0) {
4477                         nvlist_t *nverrlist = NULL;
4478
4479                         /*
4480                          * If the approximate error count is small, get a
4481                          * precise count by fetching the entire log and
4482                          * uniquifying the results.
4483                          */
4484                         if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4485                             zpool_get_errlog(zhp, &nverrlist) == 0) {
4486                                 nvpair_t *elem;
4487
4488                                 elem = NULL;
4489                                 nerr = 0;
4490                                 while ((elem = nvlist_next_nvpair(nverrlist,
4491                                     elem)) != NULL) {
4492                                         nerr++;
4493                                 }
4494                         }
4495                         nvlist_free(nverrlist);
4496
4497                         (void) printf("\n");
4498
4499                         if (nerr == 0)
4500                                 (void) printf(gettext("errors: No known data "
4501                                     "errors\n"));
4502                         else if (!cbp->cb_verbose)
4503                                 (void) printf(gettext("errors: %llu data "
4504                                     "errors, use '-v' for a list\n"),
4505                                     (u_longlong_t)nerr);
4506                         else
4507                                 print_error_log(zhp);
4508                 }
4509
4510                 if (cbp->cb_dedup_stats)
4511                         print_dedup_stats(config);
4512         } else {
4513                 (void) printf(gettext("config: The configuration cannot be "
4514                     "determined.\n"));
4515         }
4516
4517         return (0);
4518 }
4519
4520 /*
4521  * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4522  *
4523  *      -v      Display complete error logs
4524  *      -x      Display only pools with potential problems
4525  *      -D      Display dedup status (undocumented)
4526  *      -T      Display a timestamp in date(1) or Unix format
4527  *
4528  * Describes the health status of all pools or some subset.
4529  */
4530 int
4531 zpool_do_status(int argc, char **argv)
4532 {
4533         int c;
4534         int ret;
4535         unsigned long interval = 0, count = 0;
4536         status_cbdata_t cb = { 0 };
4537
4538         /* check options */
4539         while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4540                 switch (c) {
4541                 case 'v':
4542                         cb.cb_verbose = B_TRUE;
4543                         break;
4544                 case 'x':
4545                         cb.cb_explain = B_TRUE;
4546                         break;
4547                 case 'D':
4548                         cb.cb_dedup_stats = B_TRUE;
4549                         break;
4550                 case 'T':
4551                         get_timestamp_arg(*optarg);
4552                         break;
4553                 case '?':
4554                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4555                             optopt);
4556                         usage(B_FALSE);
4557                 }
4558         }
4559
4560         argc -= optind;
4561         argv += optind;
4562
4563         get_interval_count(&argc, argv, &interval, &count);
4564
4565         if (argc == 0)
4566                 cb.cb_allpools = B_TRUE;
4567
4568         cb.cb_first = B_TRUE;
4569
4570         for (;;) {
4571                 if (timestamp_fmt != NODATE)
4572                         print_timestamp(timestamp_fmt);
4573
4574                 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4575                     status_callback, &cb);
4576
4577                 if (argc == 0 && cb.cb_count == 0)
4578                         (void) fprintf(stderr, gettext("no pools available\n"));
4579                 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4580                         (void) printf(gettext("all pools are healthy\n"));
4581
4582                 if (ret != 0)
4583                         return (ret);
4584
4585                 if (interval == 0)
4586                         break;
4587
4588                 if (count != 0 && --count == 0)
4589                         break;
4590
4591                 (void) sleep(interval);
4592         }
4593
4594         return (0);
4595 }
4596
4597 typedef struct upgrade_cbdata {
4598         int     cb_first;
4599         int     cb_argc;
4600         uint64_t cb_version;
4601         char    **cb_argv;
4602 } upgrade_cbdata_t;
4603
4604 static int
4605 check_unsupp_fs(zfs_handle_t *zhp, void *unsupp_fs)
4606 {
4607         int zfs_version = (int) zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
4608         int *count = (int *)unsupp_fs;
4609
4610         if (zfs_version > ZPL_VERSION) {
4611                 (void) printf(gettext("%s (v%d) is not supported by this "
4612                     "implementation of ZFS.\n"),
4613                     zfs_get_name(zhp), zfs_version);
4614                 (*count)++;
4615         }
4616
4617         zfs_iter_filesystems(zhp, check_unsupp_fs, unsupp_fs);
4618
4619         zfs_close(zhp);
4620
4621         return (0);
4622 }
4623
4624 static int
4625 upgrade_version(zpool_handle_t *zhp, uint64_t version)
4626 {
4627         int ret;
4628         nvlist_t *config;
4629         uint64_t oldversion;
4630         int unsupp_fs = 0;
4631
4632         config = zpool_get_config(zhp, NULL);
4633         verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4634             &oldversion) == 0);
4635
4636         assert(SPA_VERSION_IS_SUPPORTED(oldversion));
4637         assert(oldversion < version);
4638
4639         ret = zfs_iter_root(zpool_get_handle(zhp), check_unsupp_fs, &unsupp_fs);
4640         if (ret != 0)
4641                 return (ret);
4642
4643         if (unsupp_fs) {
4644                 (void) printf(gettext("Upgrade not performed due to %d "
4645                     "unsupported filesystems (max v%d).\n"),
4646                     unsupp_fs, (int) ZPL_VERSION);
4647                 return (1);
4648         }
4649
4650         ret = zpool_upgrade(zhp, version);
4651         if (ret != 0)
4652                 return (ret);
4653
4654         if (version >= SPA_VERSION_FEATURES) {
4655                 (void) printf(gettext("Successfully upgraded "
4656                     "'%s' from version %llu to feature flags.\n"),
4657                     zpool_get_name(zhp), (u_longlong_t) oldversion);
4658         } else {
4659                 (void) printf(gettext("Successfully upgraded "
4660                     "'%s' from version %llu to version %llu.\n"),
4661                     zpool_get_name(zhp), (u_longlong_t) oldversion,
4662                     (u_longlong_t) version);
4663         }
4664
4665         return (0);
4666 }
4667
4668 static int
4669 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
4670 {
4671         int i, ret, count;
4672         boolean_t firstff = B_TRUE;
4673         nvlist_t *enabled = zpool_get_features(zhp);
4674
4675         count = 0;
4676         for (i = 0; i < SPA_FEATURES; i++) {
4677                 const char *fname = spa_feature_table[i].fi_uname;
4678                 const char *fguid = spa_feature_table[i].fi_guid;
4679                 if (!nvlist_exists(enabled, fguid)) {
4680                         char *propname;
4681                         verify(-1 != asprintf(&propname, "feature@%s", fname));
4682                         ret = zpool_set_prop(zhp, propname,
4683                             ZFS_FEATURE_ENABLED);
4684                         if (ret != 0) {
4685                                 free(propname);
4686                                 return (ret);
4687                         }
4688                         count++;
4689
4690                         if (firstff) {
4691                                 (void) printf(gettext("Enabled the "
4692                                     "following features on '%s':\n"),
4693                                     zpool_get_name(zhp));
4694                                 firstff = B_FALSE;
4695                         }
4696                         (void) printf(gettext("  %s\n"), fname);
4697                         free(propname);
4698                 }
4699         }
4700
4701         if (countp != NULL)
4702                 *countp = count;
4703         return (0);
4704 }
4705
4706 static int
4707 upgrade_cb(zpool_handle_t *zhp, void *arg)
4708 {
4709         upgrade_cbdata_t *cbp = arg;
4710         nvlist_t *config;
4711         uint64_t version;
4712         boolean_t printnl = B_FALSE;
4713         int ret;
4714
4715         config = zpool_get_config(zhp, NULL);
4716         verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4717             &version) == 0);
4718
4719         assert(SPA_VERSION_IS_SUPPORTED(version));
4720
4721         if (version < cbp->cb_version) {
4722                 cbp->cb_first = B_FALSE;
4723                 ret = upgrade_version(zhp, cbp->cb_version);
4724                 if (ret != 0)
4725                         return (ret);
4726                 printnl = B_TRUE;
4727
4728                 /*
4729                  * If they did "zpool upgrade -a", then we could
4730                  * be doing ioctls to different pools.  We need
4731                  * to log this history once to each pool, and bypass
4732                  * the normal history logging that happens in main().
4733                  */
4734                 (void) zpool_log_history(g_zfs, history_str);
4735                 log_history = B_FALSE;
4736         }
4737
4738         if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4739                 int count;
4740                 ret = upgrade_enable_all(zhp, &count);
4741                 if (ret != 0)
4742                         return (ret);
4743
4744                 if (count > 0) {
4745                         cbp->cb_first = B_FALSE;
4746                         printnl = B_TRUE;
4747                 }
4748         }
4749
4750         if (printnl) {
4751                 (void) printf(gettext("\n"));
4752         }
4753
4754         return (0);
4755 }
4756
4757 static int
4758 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
4759 {
4760         upgrade_cbdata_t *cbp = arg;
4761         nvlist_t *config;
4762         uint64_t version;
4763
4764         config = zpool_get_config(zhp, NULL);
4765         verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4766             &version) == 0);
4767
4768         assert(SPA_VERSION_IS_SUPPORTED(version));
4769
4770         if (version < SPA_VERSION_FEATURES) {
4771                 if (cbp->cb_first) {
4772                         (void) printf(gettext("The following pools are "
4773                             "formatted with legacy version numbers and can\n"
4774                             "be upgraded to use feature flags.  After "
4775                             "being upgraded, these pools\nwill no "
4776                             "longer be accessible by software that does not "
4777                             "support feature\nflags.\n\n"));
4778                         (void) printf(gettext("VER  POOL\n"));
4779                         (void) printf(gettext("---  ------------\n"));
4780                         cbp->cb_first = B_FALSE;
4781                 }
4782
4783                 (void) printf("%2llu   %s\n", (u_longlong_t)version,
4784                     zpool_get_name(zhp));
4785         }
4786
4787         return (0);
4788 }
4789
4790 static int
4791 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
4792 {
4793         upgrade_cbdata_t *cbp = arg;
4794         nvlist_t *config;
4795         uint64_t version;
4796
4797         config = zpool_get_config(zhp, NULL);
4798         verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4799             &version) == 0);
4800
4801         if (version >= SPA_VERSION_FEATURES) {
4802                 int i;
4803                 boolean_t poolfirst = B_TRUE;
4804                 nvlist_t *enabled = zpool_get_features(zhp);
4805
4806                 for (i = 0; i < SPA_FEATURES; i++) {
4807                         const char *fguid = spa_feature_table[i].fi_guid;
4808                         const char *fname = spa_feature_table[i].fi_uname;
4809                         if (!nvlist_exists(enabled, fguid)) {
4810                                 if (cbp->cb_first) {
4811                                         (void) printf(gettext("\nSome "
4812                                             "supported features are not "
4813                                             "enabled on the following pools. "
4814                                             "Once a\nfeature is enabled the "
4815                                             "pool may become incompatible with "
4816                                             "software\nthat does not support "
4817                                             "the feature. See "
4818                                             "zpool-features(5) for "
4819                                             "details.\n\n"));
4820                                         (void) printf(gettext("POOL  "
4821                                             "FEATURE\n"));
4822                                         (void) printf(gettext("------"
4823                                             "---------\n"));
4824                                         cbp->cb_first = B_FALSE;
4825                                 }
4826
4827                                 if (poolfirst) {
4828                                         (void) printf(gettext("%s\n"),
4829                                             zpool_get_name(zhp));
4830                                         poolfirst = B_FALSE;
4831                                 }
4832
4833                                 (void) printf(gettext("      %s\n"), fname);
4834                         }
4835                         /*
4836                          * If they did "zpool upgrade -a", then we could
4837                          * be doing ioctls to different pools.  We need
4838                          * to log this history once to each pool, and bypass
4839                          * the normal history logging that happens in main().
4840                          */
4841                         (void) zpool_log_history(g_zfs, history_str);
4842                         log_history = B_FALSE;
4843                 }
4844         }
4845
4846         return (0);
4847 }
4848
4849 /* ARGSUSED */
4850 static int
4851 upgrade_one(zpool_handle_t *zhp, void *data)
4852 {
4853         boolean_t printnl = B_FALSE;
4854         upgrade_cbdata_t *cbp = data;
4855         uint64_t cur_version;
4856         int ret;
4857
4858         if (strcmp("log", zpool_get_name(zhp)) == 0) {
4859                 (void) printf(gettext("'log' is now a reserved word\n"
4860                     "Pool 'log' must be renamed using export and import"
4861                     " to upgrade.\n"));
4862                 return (1);
4863         }
4864
4865         cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4866         if (cur_version > cbp->cb_version) {
4867                 (void) printf(gettext("Pool '%s' is already formatted "
4868                     "using more current version '%llu'.\n\n"),
4869                     zpool_get_name(zhp), (u_longlong_t) cur_version);
4870                 return (0);
4871         }
4872
4873         if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
4874                 (void) printf(gettext("Pool '%s' is already formatted "
4875                     "using version %llu.\n\n"), zpool_get_name(zhp),
4876                     (u_longlong_t) cbp->cb_version);
4877                 return (0);
4878         }
4879
4880         if (cur_version != cbp->cb_version) {
4881                 printnl = B_TRUE;
4882                 ret = upgrade_version(zhp, cbp->cb_version);
4883                 if (ret != 0)
4884                         return (ret);
4885         }
4886
4887         if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4888                 int count = 0;
4889                 ret = upgrade_enable_all(zhp, &count);
4890                 if (ret != 0)
4891                         return (ret);
4892
4893                 if (count != 0) {
4894                         printnl = B_TRUE;
4895                 } else if (cur_version == SPA_VERSION) {
4896                         (void) printf(gettext("Pool '%s' already has all "
4897                             "supported features enabled.\n"),
4898                             zpool_get_name(zhp));
4899                 }
4900         }
4901
4902         if (printnl) {
4903                 (void) printf(gettext("\n"));
4904         }
4905
4906         return (0);
4907 }
4908
4909 /*
4910  * zpool upgrade
4911  * zpool upgrade -v
4912  * zpool upgrade [-V version] <-a | pool ...>
4913  *
4914  * With no arguments, display downrev'd ZFS pool available for upgrade.
4915  * Individual pools can be upgraded by specifying the pool, and '-a' will
4916  * upgrade all pools.
4917  */
4918 int
4919 zpool_do_upgrade(int argc, char **argv)
4920 {
4921         int c;
4922         upgrade_cbdata_t cb = { 0 };
4923         int ret = 0;
4924         boolean_t showversions = B_FALSE;
4925         boolean_t upgradeall = B_FALSE;
4926         char *end;
4927
4928
4929         /* check options */
4930         while ((c = getopt(argc, argv, ":avV:")) != -1) {
4931                 switch (c) {
4932                 case 'a':
4933                         upgradeall = B_TRUE;
4934                         break;
4935                 case 'v':
4936                         showversions = B_TRUE;
4937                         break;
4938                 case 'V':
4939                         cb.cb_version = strtoll(optarg, &end, 10);
4940                         if (*end != '\0' ||
4941                             !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
4942                                 (void) fprintf(stderr,
4943                                     gettext("invalid version '%s'\n"), optarg);
4944                                 usage(B_FALSE);
4945                         }
4946                         break;
4947                 case ':':
4948                         (void) fprintf(stderr, gettext("missing argument for "
4949                             "'%c' option\n"), optopt);
4950                         usage(B_FALSE);
4951                         break;
4952                 case '?':
4953                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4954                             optopt);
4955                         usage(B_FALSE);
4956                 }
4957         }
4958
4959         cb.cb_argc = argc;
4960         cb.cb_argv = argv;
4961         argc -= optind;
4962         argv += optind;
4963
4964         if (cb.cb_version == 0) {
4965                 cb.cb_version = SPA_VERSION;
4966         } else if (!upgradeall && argc == 0) {
4967                 (void) fprintf(stderr, gettext("-V option is "
4968                     "incompatible with other arguments\n"));
4969                 usage(B_FALSE);
4970         }
4971
4972         if (showversions) {
4973                 if (upgradeall || argc != 0) {
4974                         (void) fprintf(stderr, gettext("-v option is "
4975                             "incompatible with other arguments\n"));
4976                         usage(B_FALSE);
4977                 }
4978         } else if (upgradeall) {
4979                 if (argc != 0) {
4980                         (void) fprintf(stderr, gettext("-a option should not "
4981                             "be used along with a pool name\n"));
4982                         usage(B_FALSE);
4983                 }
4984         }
4985
4986         (void) printf(gettext("This system supports ZFS pool feature "
4987             "flags.\n\n"));
4988         if (showversions) {
4989                 int i;
4990
4991                 (void) printf(gettext("The following features are "
4992                     "supported:\n\n"));
4993                 (void) printf(gettext("FEAT DESCRIPTION\n"));
4994                 (void) printf("----------------------------------------------"
4995                     "---------------\n");
4996                 for (i = 0; i < SPA_FEATURES; i++) {
4997                         zfeature_info_t *fi = &spa_feature_table[i];
4998                         const char *ro = fi->fi_can_readonly ?
4999                             " (read-only compatible)" : "";
5000
5001                         (void) printf("%-37s%s\n", fi->fi_uname, ro);
5002                         (void) printf("     %s\n", fi->fi_desc);
5003                 }
5004                 (void) printf("\n");
5005
5006                 (void) printf(gettext("The following legacy versions are also "
5007                     "supported:\n\n"));
5008                 (void) printf(gettext("VER  DESCRIPTION\n"));
5009                 (void) printf("---  -----------------------------------------"
5010                     "---------------\n");
5011                 (void) printf(gettext(" 1   Initial ZFS version\n"));
5012                 (void) printf(gettext(" 2   Ditto blocks "
5013                     "(replicated metadata)\n"));
5014                 (void) printf(gettext(" 3   Hot spares and double parity "
5015                     "RAID-Z\n"));
5016                 (void) printf(gettext(" 4   zpool history\n"));
5017                 (void) printf(gettext(" 5   Compression using the gzip "
5018                     "algorithm\n"));
5019                 (void) printf(gettext(" 6   bootfs pool property\n"));
5020                 (void) printf(gettext(" 7   Separate intent log devices\n"));
5021                 (void) printf(gettext(" 8   Delegated administration\n"));
5022                 (void) printf(gettext(" 9   refquota and refreservation "
5023                     "properties\n"));
5024                 (void) printf(gettext(" 10  Cache devices\n"));
5025                 (void) printf(gettext(" 11  Improved scrub performance\n"));
5026                 (void) printf(gettext(" 12  Snapshot properties\n"));
5027                 (void) printf(gettext(" 13  snapused property\n"));
5028                 (void) printf(gettext(" 14  passthrough-x aclinherit\n"));
5029                 (void) printf(gettext(" 15  user/group space accounting\n"));
5030                 (void) printf(gettext(" 16  stmf property support\n"));
5031                 (void) printf(gettext(" 17  Triple-parity RAID-Z\n"));
5032                 (void) printf(gettext(" 18  Snapshot user holds\n"));
5033                 (void) printf(gettext(" 19  Log device removal\n"));
5034                 (void) printf(gettext(" 20  Compression using zle "
5035                     "(zero-length encoding)\n"));
5036                 (void) printf(gettext(" 21  Deduplication\n"));
5037                 (void) printf(gettext(" 22  Received properties\n"));
5038                 (void) printf(gettext(" 23  Slim ZIL\n"));
5039                 (void) printf(gettext(" 24  System attributes\n"));
5040                 (void) printf(gettext(" 25  Improved scrub stats\n"));
5041                 (void) printf(gettext(" 26  Improved snapshot deletion "
5042                     "performance\n"));
5043                 (void) printf(gettext(" 27  Improved snapshot creation "
5044                     "performance\n"));
5045                 (void) printf(gettext(" 28  Multiple vdev replacements\n"));
5046                 (void) printf(gettext("\nFor more information on a particular "
5047                     "version, including supported releases,\n"));
5048                 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
5049         } else if (argc == 0 && upgradeall) {
5050                 cb.cb_first = B_TRUE;
5051                 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
5052                 if (ret == 0 && cb.cb_first) {
5053                         if (cb.cb_version == SPA_VERSION) {
5054                                 (void) printf(gettext("All pools are already "
5055                                     "formatted using feature flags.\n\n"));
5056                                 (void) printf(gettext("Every feature flags "
5057                                     "pool already has all supported features "
5058                                     "enabled.\n"));
5059                         } else {
5060                                 (void) printf(gettext("All pools are already "
5061                                     "formatted with version %llu or higher.\n"),
5062                                     (u_longlong_t) cb.cb_version);
5063                         }
5064                 }
5065         } else if (argc == 0) {
5066                 cb.cb_first = B_TRUE;
5067                 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
5068                 assert(ret == 0);
5069
5070                 if (cb.cb_first) {
5071                         (void) printf(gettext("All pools are formatted "
5072                             "using feature flags.\n\n"));
5073                 } else {
5074                         (void) printf(gettext("\nUse 'zpool upgrade -v' "
5075                             "for a list of available legacy versions.\n"));
5076                 }
5077
5078                 cb.cb_first = B_TRUE;
5079                 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
5080                 assert(ret == 0);
5081
5082                 if (cb.cb_first) {
5083                         (void) printf(gettext("Every feature flags pool has "
5084                             "all supported features enabled.\n"));
5085                 } else {
5086                         (void) printf(gettext("\n"));
5087                 }
5088         } else {
5089                 ret = for_each_pool(argc, argv, B_FALSE, NULL,
5090                     upgrade_one, &cb);
5091         }
5092
5093         return (ret);
5094 }
5095
5096 typedef struct hist_cbdata {
5097         boolean_t first;
5098         boolean_t longfmt;
5099         boolean_t internal;
5100 } hist_cbdata_t;
5101
5102 /*
5103  * Print out the command history for a specific pool.
5104  */
5105 static int
5106 get_history_one(zpool_handle_t *zhp, void *data)
5107 {
5108         nvlist_t *nvhis;
5109         nvlist_t **records;
5110         uint_t numrecords;
5111         int ret, i;
5112         hist_cbdata_t *cb = (hist_cbdata_t *)data;
5113
5114         cb->first = B_FALSE;
5115
5116         (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
5117
5118         if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
5119                 return (ret);
5120
5121         verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
5122             &records, &numrecords) == 0);
5123         for (i = 0; i < numrecords; i++) {
5124                 nvlist_t *rec = records[i];
5125                 char tbuf[30] = "";
5126
5127                 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
5128                         time_t tsec;
5129                         struct tm t;
5130
5131                         tsec = fnvlist_lookup_uint64(records[i],
5132                             ZPOOL_HIST_TIME);
5133                         (void) localtime_r(&tsec, &t);
5134                         (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
5135                 }
5136
5137                 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
5138                         (void) printf("%s %s", tbuf,
5139                             fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
5140                 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
5141                         int ievent =
5142                             fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
5143                         if (!cb->internal)
5144                                 continue;
5145                         if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
5146                                 (void) printf("%s unrecognized record:\n",
5147                                     tbuf);
5148                                 dump_nvlist(rec, 4);
5149                                 continue;
5150                         }
5151                         (void) printf("%s [internal %s txg:%lld] %s", tbuf,
5152                             zfs_history_event_names[ievent],
5153                             (longlong_t) fnvlist_lookup_uint64(
5154                             rec, ZPOOL_HIST_TXG),
5155                             fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
5156                 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
5157                         if (!cb->internal)
5158                                 continue;
5159                         (void) printf("%s [txg:%lld] %s", tbuf,
5160                             (longlong_t) fnvlist_lookup_uint64(
5161                             rec, ZPOOL_HIST_TXG),
5162                             fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
5163                         if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
5164                                 (void) printf(" %s (%llu)",
5165                                     fnvlist_lookup_string(rec,
5166                                     ZPOOL_HIST_DSNAME),
5167                                     (u_longlong_t)fnvlist_lookup_uint64(rec,
5168                                     ZPOOL_HIST_DSID));
5169                         }
5170                         (void) printf(" %s", fnvlist_lookup_string(rec,
5171                             ZPOOL_HIST_INT_STR));
5172                 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
5173                         if (!cb->internal)
5174                                 continue;
5175                         (void) printf("%s ioctl %s\n", tbuf,
5176                             fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
5177                         if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
5178                                 (void) printf("    input:\n");
5179                                 dump_nvlist(fnvlist_lookup_nvlist(rec,
5180                                     ZPOOL_HIST_INPUT_NVL), 8);
5181                         }
5182                         if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
5183                                 (void) printf("    output:\n");
5184                                 dump_nvlist(fnvlist_lookup_nvlist(rec,
5185                                     ZPOOL_HIST_OUTPUT_NVL), 8);
5186                         }
5187                 } else {
5188                         if (!cb->internal)
5189                                 continue;
5190                         (void) printf("%s unrecognized record:\n", tbuf);
5191                         dump_nvlist(rec, 4);
5192                 }
5193
5194                 if (!cb->longfmt) {
5195                         (void) printf("\n");
5196                         continue;
5197                 }
5198                 (void) printf(" [");
5199                 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
5200                         uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
5201                         struct passwd *pwd = getpwuid(who);
5202                         (void) printf("user %d ", (int)who);
5203                         if (pwd != NULL)
5204                                 (void) printf("(%s) ", pwd->pw_name);
5205                 }
5206                 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
5207                         (void) printf("on %s",
5208                             fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
5209                 }
5210                 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
5211                         (void) printf(":%s",
5212                             fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
5213                 }
5214
5215                 (void) printf("]");
5216                 (void) printf("\n");
5217         }
5218         (void) printf("\n");
5219         nvlist_free(nvhis);
5220
5221         return (ret);
5222 }
5223
5224 /*
5225  * zpool history <pool>
5226  *
5227  * Displays the history of commands that modified pools.
5228  */
5229 int
5230 zpool_do_history(int argc, char **argv)
5231 {
5232         hist_cbdata_t cbdata = { 0 };
5233         int ret;
5234         int c;
5235
5236         cbdata.first = B_TRUE;
5237         /* check options */
5238         while ((c = getopt(argc, argv, "li")) != -1) {
5239                 switch (c) {
5240                 case 'l':
5241                         cbdata.longfmt = B_TRUE;
5242                         break;
5243                 case 'i':
5244                         cbdata.internal = B_TRUE;
5245                         break;
5246                 case '?':
5247                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5248                             optopt);
5249                         usage(B_FALSE);
5250                 }
5251         }
5252         argc -= optind;
5253         argv += optind;
5254
5255         ret = for_each_pool(argc, argv, B_FALSE,  NULL, get_history_one,
5256             &cbdata);
5257
5258         if (argc == 0 && cbdata.first == B_TRUE) {
5259                 (void) fprintf(stderr, gettext("no pools available\n"));
5260                 return (0);
5261         }
5262
5263         return (ret);
5264 }
5265
5266 typedef struct ev_opts {
5267         int verbose;
5268         int scripted;
5269         int follow;
5270         int clear;
5271 } ev_opts_t;
5272
5273 static void
5274 zpool_do_events_short(nvlist_t *nvl)
5275 {
5276         char ctime_str[26], str[32], *ptr;
5277         int64_t *tv;
5278         uint_t n;
5279
5280         verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
5281         memset(str, ' ', 32);
5282         (void) ctime_r((const time_t *)&tv[0], ctime_str);
5283         (void) strncpy(str, ctime_str+4,  6);           /* 'Jun 30' */
5284         (void) strncpy(str+7, ctime_str+20, 4);         /* '1993' */
5285         (void) strncpy(str+12, ctime_str+11, 8);        /* '21:49:08' */
5286         (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]); /* '.123456789' */
5287         (void) printf(gettext("%s "), str);
5288
5289         verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
5290         (void) printf(gettext("%s\n"), ptr);
5291 }
5292
5293 static void
5294 zpool_do_events_nvprint(nvlist_t *nvl, int depth)
5295 {
5296         nvpair_t *nvp;
5297
5298         for (nvp = nvlist_next_nvpair(nvl, NULL);
5299             nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
5300
5301                 data_type_t type = nvpair_type(nvp);
5302                 const char *name = nvpair_name(nvp);
5303
5304                 boolean_t b;
5305                 uint8_t i8;
5306                 uint16_t i16;
5307                 uint32_t i32;
5308                 uint64_t i64;
5309                 char *str;
5310                 nvlist_t *cnv;
5311
5312                 printf(gettext("%*s%s = "), depth, "", name);
5313
5314                 switch (type) {
5315                 case DATA_TYPE_BOOLEAN:
5316                         printf(gettext("%s"), "1");
5317                         break;
5318
5319                 case DATA_TYPE_BOOLEAN_VALUE:
5320                         (void) nvpair_value_boolean_value(nvp, &b);
5321                         printf(gettext("%s"), b ? "1" : "0");
5322                         break;
5323
5324                 case DATA_TYPE_BYTE:
5325                         (void) nvpair_value_byte(nvp, &i8);
5326                         printf(gettext("0x%x"), i8);
5327                         break;
5328
5329                 case DATA_TYPE_INT8:
5330                         (void) nvpair_value_int8(nvp, (void *)&i8);
5331                         printf(gettext("0x%x"), i8);
5332                         break;
5333
5334                 case DATA_TYPE_UINT8:
5335                         (void) nvpair_value_uint8(nvp, &i8);
5336                         printf(gettext("0x%x"), i8);
5337                         break;
5338
5339                 case DATA_TYPE_INT16:
5340                         (void) nvpair_value_int16(nvp, (void *)&i16);
5341                         printf(gettext("0x%x"), i16);
5342                         break;
5343
5344                 case DATA_TYPE_UINT16:
5345                         (void) nvpair_value_uint16(nvp, &i16);
5346                         printf(gettext("0x%x"), i16);
5347                         break;
5348
5349                 case DATA_TYPE_INT32:
5350                         (void) nvpair_value_int32(nvp, (void *)&i32);
5351                         printf(gettext("0x%x"), i32);
5352                         break;
5353
5354                 case DATA_TYPE_UINT32:
5355                         (void) nvpair_value_uint32(nvp, &i32);
5356                         printf(gettext("0x%x"), i32);
5357                         break;
5358
5359                 case DATA_TYPE_INT64:
5360                         (void) nvpair_value_int64(nvp, (void *)&i64);
5361                         printf(gettext("0x%llx"), (u_longlong_t)i64);
5362                         break;
5363
5364                 case DATA_TYPE_UINT64:
5365                         (void) nvpair_value_uint64(nvp, &i64);
5366                         printf(gettext("0x%llx"), (u_longlong_t)i64);
5367                         break;
5368
5369                 case DATA_TYPE_HRTIME:
5370                         (void) nvpair_value_hrtime(nvp, (void *)&i64);
5371                         printf(gettext("0x%llx"), (u_longlong_t)i64);
5372                         break;
5373
5374                 case DATA_TYPE_STRING:
5375                         (void) nvpair_value_string(nvp, &str);
5376                         printf(gettext("\"%s\""), str ? str : "<NULL>");
5377                         break;
5378
5379                 case DATA_TYPE_NVLIST:
5380                         printf(gettext("(embedded nvlist)\n"));
5381                         (void) nvpair_value_nvlist(nvp, &cnv);
5382                         zpool_do_events_nvprint(cnv, depth + 8);
5383                         printf(gettext("%*s(end %s)"), depth, "", name);
5384                         break;
5385
5386                 case DATA_TYPE_NVLIST_ARRAY: {
5387                         nvlist_t **val;
5388                         uint_t i, nelem;
5389
5390                         (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
5391                         printf(gettext("(%d embedded nvlists)\n"), nelem);
5392                         for (i = 0; i < nelem; i++) {
5393                                 printf(gettext("%*s%s[%d] = %s\n"),
5394                                     depth, "", name, i, "(embedded nvlist)");
5395                                 zpool_do_events_nvprint(val[i], depth + 8);
5396                                 printf(gettext("%*s(end %s[%i])\n"),
5397                                     depth, "", name, i);
5398                         }
5399                         printf(gettext("%*s(end %s)\n"), depth, "", name);
5400                         }
5401                         break;
5402
5403                 case DATA_TYPE_INT8_ARRAY: {
5404                         int8_t *val;
5405                         uint_t i, nelem;
5406
5407                         (void) nvpair_value_int8_array(nvp, &val, &nelem);
5408                         for (i = 0; i < nelem; i++)
5409                                 printf(gettext("0x%x "), val[i]);
5410
5411                         break;
5412                         }
5413
5414                 case DATA_TYPE_UINT8_ARRAY: {
5415                         uint8_t *val;
5416                         uint_t i, nelem;
5417
5418                         (void) nvpair_value_uint8_array(nvp, &val, &nelem);
5419                         for (i = 0; i < nelem; i++)
5420                                 printf(gettext("0x%x "), val[i]);
5421
5422                         break;
5423                         }
5424
5425                 case DATA_TYPE_INT16_ARRAY: {
5426                         int16_t *val;
5427                         uint_t i, nelem;
5428
5429                         (void) nvpair_value_int16_array(nvp, &val, &nelem);
5430                         for (i = 0; i < nelem; i++)
5431                                 printf(gettext("0x%x "), val[i]);
5432
5433                         break;
5434                         }
5435
5436                 case DATA_TYPE_UINT16_ARRAY: {
5437                         uint16_t *val;
5438                         uint_t i, nelem;
5439
5440                         (void) nvpair_value_uint16_array(nvp, &val, &nelem);
5441                         for (i = 0; i < nelem; i++)
5442                                 printf(gettext("0x%x "), val[i]);
5443
5444                         break;
5445                         }
5446
5447                 case DATA_TYPE_INT32_ARRAY: {
5448                         int32_t *val;
5449                         uint_t i, nelem;
5450
5451                         (void) nvpair_value_int32_array(nvp, &val, &nelem);
5452                         for (i = 0; i < nelem; i++)
5453                                 printf(gettext("0x%x "), val[i]);
5454
5455                         break;
5456                         }
5457
5458                 case DATA_TYPE_UINT32_ARRAY: {
5459                         uint32_t *val;
5460                         uint_t i, nelem;
5461
5462                         (void) nvpair_value_uint32_array(nvp, &val, &nelem);
5463                         for (i = 0; i < nelem; i++)
5464                                 printf(gettext("0x%x "), val[i]);
5465
5466                         break;
5467                         }
5468
5469                 case DATA_TYPE_INT64_ARRAY: {
5470                         int64_t *val;
5471                         uint_t i, nelem;
5472
5473                         (void) nvpair_value_int64_array(nvp, &val, &nelem);
5474                         for (i = 0; i < nelem; i++)
5475                                 printf(gettext("0x%llx "),
5476                                     (u_longlong_t)val[i]);
5477
5478                         break;
5479                         }
5480
5481                 case DATA_TYPE_UINT64_ARRAY: {
5482                         uint64_t *val;
5483                         uint_t i, nelem;
5484
5485                         (void) nvpair_value_uint64_array(nvp, &val, &nelem);
5486                         for (i = 0; i < nelem; i++)
5487                                 printf(gettext("0x%llx "),
5488                                     (u_longlong_t)val[i]);
5489
5490                         break;
5491                         }
5492
5493                 case DATA_TYPE_STRING_ARRAY: {
5494                         char **str;
5495                         uint_t i, nelem;
5496
5497                         (void) nvpair_value_string_array(nvp, &str, &nelem);
5498                         for (i = 0; i < nelem; i++)
5499                                 printf(gettext("\"%s\" "),
5500                                     str[i] ? str[i] : "<NULL>");
5501
5502                         break;
5503                         }
5504
5505                 case DATA_TYPE_BOOLEAN_ARRAY:
5506                 case DATA_TYPE_BYTE_ARRAY:
5507                 case DATA_TYPE_DOUBLE:
5508                 case DATA_TYPE_UNKNOWN:
5509                         printf(gettext("<unknown>"));
5510                         break;
5511                 }
5512
5513                 printf(gettext("\n"));
5514         }
5515 }
5516
5517 static int
5518 zpool_do_events_next(ev_opts_t *opts)
5519 {
5520         nvlist_t *nvl;
5521         int zevent_fd, ret, dropped;
5522
5523         zevent_fd = open(ZFS_DEV, O_RDWR);
5524         VERIFY(zevent_fd >= 0);
5525
5526         if (!opts->scripted)
5527                 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
5528
5529         while (1) {
5530                 ret = zpool_events_next(g_zfs, &nvl, &dropped,
5531                     (opts->follow ? ZEVENT_NONE : ZEVENT_NONBLOCK), zevent_fd);
5532                 if (ret || nvl == NULL)
5533                         break;
5534
5535                 if (dropped > 0)
5536                         (void) printf(gettext("dropped %d events\n"), dropped);
5537
5538                 zpool_do_events_short(nvl);
5539
5540                 if (opts->verbose) {
5541                         zpool_do_events_nvprint(nvl, 8);
5542                         printf(gettext("\n"));
5543                 }
5544                 (void) fflush(stdout);
5545
5546                 nvlist_free(nvl);
5547         }
5548
5549         VERIFY(0 == close(zevent_fd));
5550
5551         return (ret);
5552 }
5553
5554 static int
5555 zpool_do_events_clear(ev_opts_t *opts)
5556 {
5557         int count, ret;
5558
5559         ret = zpool_events_clear(g_zfs, &count);
5560         if (!ret)
5561                 (void) printf(gettext("cleared %d events\n"), count);
5562
5563         return (ret);
5564 }
5565
5566 /*
5567  * zpool events [-vfc]
5568  *
5569  * Displays events logs by ZFS.
5570  */
5571 int
5572 zpool_do_events(int argc, char **argv)
5573 {
5574         ev_opts_t opts = { 0 };
5575         int ret;
5576         int c;
5577
5578         /* check options */
5579         while ((c = getopt(argc, argv, "vHfc")) != -1) {
5580                 switch (c) {
5581                 case 'v':
5582                         opts.verbose = 1;
5583                         break;
5584                 case 'H':
5585                         opts.scripted = 1;
5586                         break;
5587                 case 'f':
5588                         opts.follow = 1;
5589                         break;
5590                 case 'c':
5591                         opts.clear = 1;
5592                         break;
5593                 case '?':
5594                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5595                             optopt);
5596                         usage(B_FALSE);
5597                 }
5598         }
5599         argc -= optind;
5600         argv += optind;
5601
5602         if (opts.clear)
5603                 ret = zpool_do_events_clear(&opts);
5604         else
5605                 ret = zpool_do_events_next(&opts);
5606
5607         return (ret);
5608 }
5609
5610 static int
5611 get_callback(zpool_handle_t *zhp, void *data)
5612 {
5613         zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5614         char value[MAXNAMELEN];
5615         zprop_source_t srctype;
5616         zprop_list_t *pl;
5617
5618         for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5619
5620                 /*
5621                  * Skip the special fake placeholder. This will also skip
5622                  * over the name property when 'all' is specified.
5623                  */
5624                 if (pl->pl_prop == ZPOOL_PROP_NAME &&
5625                     pl == cbp->cb_proplist)
5626                         continue;
5627
5628                 if (pl->pl_prop == ZPROP_INVAL &&
5629                     (zpool_prop_feature(pl->pl_user_prop) ||
5630                     zpool_prop_unsupported(pl->pl_user_prop))) {
5631                         srctype = ZPROP_SRC_LOCAL;
5632
5633                         if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5634                             value, sizeof (value)) == 0) {
5635                                 zprop_print_one_property(zpool_get_name(zhp),
5636                                     cbp, pl->pl_user_prop, value, srctype,
5637                                     NULL, NULL);
5638                         }
5639                 } else {
5640                         if (zpool_get_prop_literal(zhp, pl->pl_prop, value,
5641                             sizeof (value), &srctype, cbp->cb_literal) != 0)
5642                                 continue;
5643
5644                         zprop_print_one_property(zpool_get_name(zhp), cbp,
5645                             zpool_prop_to_name(pl->pl_prop), value, srctype,
5646                             NULL, NULL);
5647                 }
5648         }
5649         return (0);
5650 }
5651
5652 int
5653 zpool_do_get(int argc, char **argv)
5654 {
5655         zprop_get_cbdata_t cb = { 0 };
5656         zprop_list_t fake_name = { 0 };
5657         int c, ret;
5658
5659         /* check options */
5660         while ((c = getopt(argc, argv, "pH")) != -1) {
5661                 switch (c) {
5662                 case 'p':
5663                         cb.cb_literal = B_TRUE;
5664                         break;
5665
5666                 case 'H':
5667                         cb.cb_scripted = B_TRUE;
5668                         break;
5669
5670                 case '?':
5671                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5672                             optopt);
5673                         usage(B_FALSE);
5674                 }
5675         }
5676
5677         argc -= optind;
5678         argv += optind;
5679
5680         if (argc < 1) {
5681                 (void) fprintf(stderr, gettext("missing property "
5682                     "argument\n"));
5683                 usage(B_FALSE);
5684         }
5685
5686         cb.cb_first = B_TRUE;
5687         cb.cb_sources = ZPROP_SRC_ALL;
5688         cb.cb_columns[0] = GET_COL_NAME;
5689         cb.cb_columns[1] = GET_COL_PROPERTY;
5690         cb.cb_columns[2] = GET_COL_VALUE;
5691         cb.cb_columns[3] = GET_COL_SOURCE;
5692         cb.cb_type = ZFS_TYPE_POOL;
5693
5694         if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
5695                 usage(B_FALSE);
5696
5697         argc--;
5698         argv++;
5699
5700         if (cb.cb_proplist != NULL) {
5701                 fake_name.pl_prop = ZPOOL_PROP_NAME;
5702                 fake_name.pl_width = strlen(gettext("NAME"));
5703                 fake_name.pl_next = cb.cb_proplist;
5704                 cb.cb_proplist = &fake_name;
5705         }
5706
5707         ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
5708             get_callback, &cb);
5709
5710         if (cb.cb_proplist == &fake_name)
5711                 zprop_free_list(fake_name.pl_next);
5712         else
5713                 zprop_free_list(cb.cb_proplist);
5714
5715         return (ret);
5716 }
5717
5718 typedef struct set_cbdata {
5719         char *cb_propname;
5720         char *cb_value;
5721         boolean_t cb_any_successful;
5722 } set_cbdata_t;
5723
5724 int
5725 set_callback(zpool_handle_t *zhp, void *data)
5726 {
5727         int error;
5728         set_cbdata_t *cb = (set_cbdata_t *)data;
5729
5730         error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5731
5732         if (!error)
5733                 cb->cb_any_successful = B_TRUE;
5734
5735         return (error);
5736 }
5737
5738 int
5739 zpool_do_set(int argc, char **argv)
5740 {
5741         set_cbdata_t cb = { 0 };
5742         int error;
5743
5744         if (argc > 1 && argv[1][0] == '-') {
5745                 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5746                     argv[1][1]);
5747                 usage(B_FALSE);
5748         }
5749
5750         if (argc < 2) {
5751                 (void) fprintf(stderr, gettext("missing property=value "
5752                     "argument\n"));
5753                 usage(B_FALSE);
5754         }
5755
5756         if (argc < 3) {
5757                 (void) fprintf(stderr, gettext("missing pool name\n"));
5758                 usage(B_FALSE);
5759         }
5760
5761         if (argc > 3) {
5762                 (void) fprintf(stderr, gettext("too many pool names\n"));
5763                 usage(B_FALSE);
5764         }
5765
5766         cb.cb_propname = argv[1];
5767         cb.cb_value = strchr(cb.cb_propname, '=');
5768         if (cb.cb_value == NULL) {
5769                 (void) fprintf(stderr, gettext("missing value in "
5770                     "property=value argument\n"));
5771                 usage(B_FALSE);
5772         }
5773
5774         *(cb.cb_value) = '\0';
5775         cb.cb_value++;
5776
5777         error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5778             set_callback, &cb);
5779
5780         return (error);
5781 }
5782
5783 static int
5784 find_command_idx(char *command, int *idx)
5785 {
5786         int i;
5787
5788         for (i = 0; i < NCOMMAND; i++) {
5789                 if (command_table[i].name == NULL)
5790                         continue;
5791
5792                 if (strcmp(command, command_table[i].name) == 0) {
5793                         *idx = i;
5794                         return (0);
5795                 }
5796         }
5797         return (1);
5798 }
5799
5800 int
5801 main(int argc, char **argv)
5802 {
5803         int ret;
5804         int i = 0;
5805         char *cmdname;
5806
5807         (void) setlocale(LC_ALL, "");
5808         (void) textdomain(TEXT_DOMAIN);
5809
5810         opterr = 0;
5811
5812         /*
5813          * Make sure the user has specified some command.
5814          */
5815         if (argc < 2) {
5816                 (void) fprintf(stderr, gettext("missing command\n"));
5817                 usage(B_FALSE);
5818         }
5819
5820         cmdname = argv[1];
5821
5822         /*
5823          * Special case '-?'
5824          */
5825         if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0)
5826                 usage(B_TRUE);
5827
5828         if ((g_zfs = libzfs_init()) == NULL)
5829                 return (1);
5830
5831         libzfs_print_on_error(g_zfs, B_TRUE);
5832
5833         zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
5834
5835         /*
5836          * Run the appropriate command.
5837          */
5838         if (find_command_idx(cmdname, &i) == 0) {
5839                 current_command = &command_table[i];
5840                 ret = command_table[i].func(argc - 1, argv + 1);
5841         } else if (strchr(cmdname, '=')) {
5842                 verify(find_command_idx("set", &i) == 0);
5843                 current_command = &command_table[i];
5844                 ret = command_table[i].func(argc, argv);
5845         } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5846                 /*
5847                  * 'freeze' is a vile debugging abomination, so we treat
5848                  * it as such.
5849                  */
5850                 char buf[16384];
5851                 int fd = open(ZFS_DEV, O_RDWR);
5852                 (void) strcpy((void *)buf, argv[2]);
5853                 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
5854         } else {
5855                 (void) fprintf(stderr, gettext("unrecognized "
5856                     "command '%s'\n"), cmdname);
5857                 usage(B_FALSE);
5858                 ret = 1;
5859         }
5860
5861         if (ret == 0 && log_history)
5862                 (void) zpool_log_history(g_zfs, history_str);
5863
5864         libzfs_fini(g_zfs);
5865
5866         /*
5867          * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5868          * for the purposes of running ::findleaks.
5869          */
5870         if (getenv("ZFS_ABORT") != NULL) {
5871                 (void) printf("dumping core by request\n");
5872                 abort();
5873         }
5874
5875         return (ret);
5876 }