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