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