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