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