]> granicus.if.org Git - zfs/blob - cmd/zpool/zpool_main.c
Fix unused variable warnings
[zfs] / cmd / zpool / zpool_main.c
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25  * Copyright (c) 2011, 2015 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  * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
29  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
30  * Copyright (c) 2017 Datto Inc.
31  * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
32  */
33
34 #include <assert.h>
35 #include <ctype.h>
36 #include <dirent.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <libgen.h>
40 #include <libintl.h>
41 #include <libuutil.h>
42 #include <locale.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <strings.h>
47 #include <unistd.h>
48 #include <pwd.h>
49 #include <zone.h>
50 #include <sys/wait.h>
51 #include <zfs_prop.h>
52 #include <sys/fs/zfs.h>
53 #include <sys/stat.h>
54 #include <sys/systeminfo.h>
55 #include <sys/fm/fs/zfs.h>
56 #include <sys/fm/util.h>
57 #include <sys/fm/protocol.h>
58 #include <sys/zfs_ioctl.h>
59 #include <sys/mount.h>
60 #include <sys/sysmacros.h>
61
62 #include <math.h>
63
64 #include <libzfs.h>
65
66 #include "zpool_util.h"
67 #include "zfs_comutil.h"
68 #include "zfeature_common.h"
69
70 #include "statcommon.h"
71
72 static int zpool_do_create(int, char **);
73 static int zpool_do_destroy(int, char **);
74
75 static int zpool_do_add(int, char **);
76 static int zpool_do_remove(int, char **);
77 static int zpool_do_labelclear(int, char **);
78
79 static int zpool_do_list(int, char **);
80 static int zpool_do_iostat(int, char **);
81 static int zpool_do_status(int, char **);
82
83 static int zpool_do_online(int, char **);
84 static int zpool_do_offline(int, char **);
85 static int zpool_do_clear(int, char **);
86 static int zpool_do_reopen(int, char **);
87
88 static int zpool_do_reguid(int, char **);
89
90 static int zpool_do_attach(int, char **);
91 static int zpool_do_detach(int, char **);
92 static int zpool_do_replace(int, char **);
93 static int zpool_do_split(int, char **);
94
95 static int zpool_do_scrub(int, char **);
96
97 static int zpool_do_import(int, char **);
98 static int zpool_do_export(int, char **);
99
100 static int zpool_do_upgrade(int, char **);
101
102 static int zpool_do_history(int, char **);
103 static int zpool_do_events(int, char **);
104
105 static int zpool_do_get(int, char **);
106 static int zpool_do_set(int, char **);
107
108 static int zpool_do_sync(int, char **);
109
110 /*
111  * These libumem hooks provide a reasonable set of defaults for the allocator's
112  * debugging facilities.
113  */
114
115 #ifdef DEBUG
116 const char *
117 _umem_debug_init(void)
118 {
119         return ("default,verbose"); /* $UMEM_DEBUG setting */
120 }
121
122 const char *
123 _umem_logging_init(void)
124 {
125         return ("fail,contents"); /* $UMEM_LOGGING setting */
126 }
127 #endif
128
129 typedef enum {
130         HELP_ADD,
131         HELP_ATTACH,
132         HELP_CLEAR,
133         HELP_CREATE,
134         HELP_DESTROY,
135         HELP_DETACH,
136         HELP_EXPORT,
137         HELP_HISTORY,
138         HELP_IMPORT,
139         HELP_IOSTAT,
140         HELP_LABELCLEAR,
141         HELP_LIST,
142         HELP_OFFLINE,
143         HELP_ONLINE,
144         HELP_REPLACE,
145         HELP_REMOVE,
146         HELP_SCRUB,
147         HELP_STATUS,
148         HELP_UPGRADE,
149         HELP_EVENTS,
150         HELP_GET,
151         HELP_SET,
152         HELP_SPLIT,
153         HELP_SYNC,
154         HELP_REGUID,
155         HELP_REOPEN
156 } zpool_help_t;
157
158
159 /*
160  * Flags for stats to display with "zpool iostats"
161  */
162 enum iostat_type {
163         IOS_DEFAULT = 0,
164         IOS_LATENCY = 1,
165         IOS_QUEUES = 2,
166         IOS_L_HISTO = 3,
167         IOS_RQ_HISTO = 4,
168         IOS_COUNT,      /* always last element */
169 };
170
171 /* iostat_type entries as bitmasks */
172 #define IOS_DEFAULT_M   (1ULL << IOS_DEFAULT)
173 #define IOS_LATENCY_M   (1ULL << IOS_LATENCY)
174 #define IOS_QUEUES_M    (1ULL << IOS_QUEUES)
175 #define IOS_L_HISTO_M   (1ULL << IOS_L_HISTO)
176 #define IOS_RQ_HISTO_M  (1ULL << IOS_RQ_HISTO)
177
178 /* Mask of all the histo bits */
179 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
180
181 /*
182  * Lookup table for iostat flags to nvlist names.  Basically a list
183  * of all the nvlists a flag requires.  Also specifies the order in
184  * which data gets printed in zpool iostat.
185  */
186 static const char *vsx_type_to_nvlist[IOS_COUNT][11] = {
187         [IOS_L_HISTO] = {
188             ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
189             ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
190             ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
191             ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
192             ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
193             ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
194             ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
195             ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
196             ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
197             NULL},
198         [IOS_LATENCY] = {
199             ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
200             ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
201             ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
202             ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
203             NULL},
204         [IOS_QUEUES] = {
205             ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
206             ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
207             ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
208             ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
209             ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
210             NULL},
211         [IOS_RQ_HISTO] = {
212             ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO,
213             ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO,
214             ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO,
215             ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO,
216             ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO,
217             ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO,
218             ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO,
219             ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO,
220             ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO,
221             ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO,
222             NULL},
223 };
224
225
226 /*
227  * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
228  * Right now, only one histo bit is ever set at one time, so we can
229  * just do a highbit64(a)
230  */
231 #define IOS_HISTO_IDX(a)        (highbit64(a & IOS_ANYHISTO_M) - 1)
232
233 typedef struct zpool_command {
234         const char      *name;
235         int             (*func)(int, char **);
236         zpool_help_t    usage;
237 } zpool_command_t;
238
239 /*
240  * Master command table.  Each ZFS command has a name, associated function, and
241  * usage message.  The usage messages need to be internationalized, so we have
242  * to have a function to return the usage message based on a command index.
243  *
244  * These commands are organized according to how they are displayed in the usage
245  * message.  An empty command (one with a NULL name) indicates an empty line in
246  * the generic usage message.
247  */
248 static zpool_command_t command_table[] = {
249         { "create",     zpool_do_create,        HELP_CREATE             },
250         { "destroy",    zpool_do_destroy,       HELP_DESTROY            },
251         { NULL },
252         { "add",        zpool_do_add,           HELP_ADD                },
253         { "remove",     zpool_do_remove,        HELP_REMOVE             },
254         { NULL },
255         { "labelclear", zpool_do_labelclear,    HELP_LABELCLEAR         },
256         { NULL },
257         { "list",       zpool_do_list,          HELP_LIST               },
258         { "iostat",     zpool_do_iostat,        HELP_IOSTAT             },
259         { "status",     zpool_do_status,        HELP_STATUS             },
260         { NULL },
261         { "online",     zpool_do_online,        HELP_ONLINE             },
262         { "offline",    zpool_do_offline,       HELP_OFFLINE            },
263         { "clear",      zpool_do_clear,         HELP_CLEAR              },
264         { "reopen",     zpool_do_reopen,        HELP_REOPEN             },
265         { NULL },
266         { "attach",     zpool_do_attach,        HELP_ATTACH             },
267         { "detach",     zpool_do_detach,        HELP_DETACH             },
268         { "replace",    zpool_do_replace,       HELP_REPLACE            },
269         { "split",      zpool_do_split,         HELP_SPLIT              },
270         { NULL },
271         { "scrub",      zpool_do_scrub,         HELP_SCRUB              },
272         { NULL },
273         { "import",     zpool_do_import,        HELP_IMPORT             },
274         { "export",     zpool_do_export,        HELP_EXPORT             },
275         { "upgrade",    zpool_do_upgrade,       HELP_UPGRADE            },
276         { "reguid",     zpool_do_reguid,        HELP_REGUID             },
277         { NULL },
278         { "history",    zpool_do_history,       HELP_HISTORY            },
279         { "events",     zpool_do_events,        HELP_EVENTS             },
280         { NULL },
281         { "get",        zpool_do_get,           HELP_GET                },
282         { "set",        zpool_do_set,           HELP_SET                },
283         { "sync",       zpool_do_sync,          HELP_SYNC               },
284 };
285
286 #define NCOMMAND        (ARRAY_SIZE(command_table))
287
288 static zpool_command_t *current_command;
289 static char history_str[HIS_MAX_RECORD_LEN];
290 static boolean_t log_history = B_TRUE;
291 static uint_t timestamp_fmt = NODATE;
292
293 static const char *
294 get_usage(zpool_help_t idx)
295 {
296         switch (idx) {
297         case HELP_ADD:
298                 return (gettext("\tadd [-fgLnP] [-o property=value] "
299                     "<pool> <vdev> ...\n"));
300         case HELP_ATTACH:
301                 return (gettext("\tattach [-f] [-o property=value] "
302                     "<pool> <device> <new-device>\n"));
303         case HELP_CLEAR:
304                 return (gettext("\tclear [-nF] <pool> [device]\n"));
305         case HELP_CREATE:
306                 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
307                     "\t    [-O file-system-property=value] ... \n"
308                     "\t    [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
309         case HELP_DESTROY:
310                 return (gettext("\tdestroy [-f] <pool>\n"));
311         case HELP_DETACH:
312                 return (gettext("\tdetach <pool> <device>\n"));
313         case HELP_EXPORT:
314                 return (gettext("\texport [-af] <pool> ...\n"));
315         case HELP_HISTORY:
316                 return (gettext("\thistory [-il] [<pool>] ...\n"));
317         case HELP_IMPORT:
318                 return (gettext("\timport [-d dir] [-D]\n"
319                     "\timport [-d dir | -c cachefile] [-F [-n]] [-l] "
320                     "<pool | id>\n"
321                     "\timport [-o mntopts] [-o property=value] ... \n"
322                     "\t    [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
323                     "[-R root] [-F [-n]] -a\n"
324                     "\timport [-o mntopts] [-o property=value] ... \n"
325                     "\t    [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
326                     "[-R root] [-F [-n]]\n"
327                     "\t    <pool | id> [newpool]\n"));
328         case HELP_IOSTAT:
329                 return (gettext("\tiostat [[[-c [script1,script2,...]"
330                     "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n"
331                     "\t    [[pool ...]|[pool vdev ...]|[vdev ...]]"
332                     " [interval [count]]\n"));
333         case HELP_LABELCLEAR:
334                 return (gettext("\tlabelclear [-f] <vdev>\n"));
335         case HELP_LIST:
336                 return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
337                     "[-T d|u] [pool] ... [interval [count]]\n"));
338         case HELP_OFFLINE:
339                 return (gettext("\toffline [-f] [-t] <pool> <device> ...\n"));
340         case HELP_ONLINE:
341                 return (gettext("\tonline <pool> <device> ...\n"));
342         case HELP_REPLACE:
343                 return (gettext("\treplace [-f] [-o property=value] "
344                     "<pool> <device> [new-device]\n"));
345         case HELP_REMOVE:
346                 return (gettext("\tremove <pool> <device> ...\n"));
347         case HELP_REOPEN:
348                 return (gettext("\treopen [-n] <pool>\n"));
349         case HELP_SCRUB:
350                 return (gettext("\tscrub [-s | -p] <pool> ...\n"));
351         case HELP_STATUS:
352                 return (gettext("\tstatus [-c [script1,script2,...]] [-gLPvxD]"
353                     "[-T d|u] [pool] ... [interval [count]]\n"));
354         case HELP_UPGRADE:
355                 return (gettext("\tupgrade\n"
356                     "\tupgrade -v\n"
357                     "\tupgrade [-V version] <-a | pool ...>\n"));
358         case HELP_EVENTS:
359                 return (gettext("\tevents [-vHf [pool] | -c]\n"));
360         case HELP_GET:
361                 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
362                     "<\"all\" | property[,...]> <pool> ...\n"));
363         case HELP_SET:
364                 return (gettext("\tset <property=value> <pool> \n"));
365         case HELP_SPLIT:
366                 return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n"
367                     "\t    [-o property=value] <pool> <newpool> "
368                     "[<device> ...]\n"));
369         case HELP_REGUID:
370                 return (gettext("\treguid <pool>\n"));
371         case HELP_SYNC:
372                 return (gettext("\tsync [pool] ...\n"));
373         }
374
375         abort();
376         /* NOTREACHED */
377 }
378
379
380 /*
381  * Callback routine that will print out a pool property value.
382  */
383 static int
384 print_prop_cb(int prop, void *cb)
385 {
386         FILE *fp = cb;
387
388         (void) fprintf(fp, "\t%-15s  ", zpool_prop_to_name(prop));
389
390         if (zpool_prop_readonly(prop))
391                 (void) fprintf(fp, "  NO   ");
392         else
393                 (void) fprintf(fp, " YES   ");
394
395         if (zpool_prop_values(prop) == NULL)
396                 (void) fprintf(fp, "-\n");
397         else
398                 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
399
400         return (ZPROP_CONT);
401 }
402
403 /*
404  * Display usage message.  If we're inside a command, display only the usage for
405  * that command.  Otherwise, iterate over the entire command table and display
406  * a complete usage message.
407  */
408 void
409 usage(boolean_t requested)
410 {
411         FILE *fp = requested ? stdout : stderr;
412
413         if (current_command == NULL) {
414                 int i;
415
416                 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
417                 (void) fprintf(fp,
418                     gettext("where 'command' is one of the following:\n\n"));
419
420                 for (i = 0; i < NCOMMAND; i++) {
421                         if (command_table[i].name == NULL)
422                                 (void) fprintf(fp, "\n");
423                         else
424                                 (void) fprintf(fp, "%s",
425                                     get_usage(command_table[i].usage));
426                 }
427         } else {
428                 (void) fprintf(fp, gettext("usage:\n"));
429                 (void) fprintf(fp, "%s", get_usage(current_command->usage));
430         }
431
432         if (current_command != NULL &&
433             ((strcmp(current_command->name, "set") == 0) ||
434             (strcmp(current_command->name, "get") == 0) ||
435             (strcmp(current_command->name, "list") == 0))) {
436
437                 (void) fprintf(fp,
438                     gettext("\nthe following properties are supported:\n"));
439
440                 (void) fprintf(fp, "\n\t%-15s  %s   %s\n\n",
441                     "PROPERTY", "EDIT", "VALUES");
442
443                 /* Iterate over all properties */
444                 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
445                     ZFS_TYPE_POOL);
446
447                 (void) fprintf(fp, "\t%-15s   ", "feature@...");
448                 (void) fprintf(fp, "YES   disabled | enabled | active\n");
449
450                 (void) fprintf(fp, gettext("\nThe feature@ properties must be "
451                     "appended with a feature name.\nSee zpool-features(5).\n"));
452         }
453
454         /*
455          * See comments at end of main().
456          */
457         if (getenv("ZFS_ABORT") != NULL) {
458                 (void) printf("dumping core by request\n");
459                 abort();
460         }
461
462         exit(requested ? 0 : 2);
463 }
464
465 void
466 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
467     boolean_t print_logs, int name_flags)
468 {
469         nvlist_t **child;
470         uint_t c, children;
471         char *vname;
472
473         if (name != NULL)
474                 (void) printf("\t%*s%s\n", indent, "", name);
475
476         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
477             &child, &children) != 0)
478                 return;
479
480         for (c = 0; c < children; c++) {
481                 uint64_t is_log = B_FALSE;
482
483                 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
484                     &is_log);
485                 if ((is_log && !print_logs) || (!is_log && print_logs))
486                         continue;
487
488                 vname = zpool_vdev_name(g_zfs, zhp, child[c], name_flags);
489                 print_vdev_tree(zhp, vname, child[c], indent + 2,
490                     B_FALSE, name_flags);
491                 free(vname);
492         }
493 }
494
495 static boolean_t
496 prop_list_contains_feature(nvlist_t *proplist)
497 {
498         nvpair_t *nvp;
499         for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
500             nvp = nvlist_next_nvpair(proplist, nvp)) {
501                 if (zpool_prop_feature(nvpair_name(nvp)))
502                         return (B_TRUE);
503         }
504         return (B_FALSE);
505 }
506
507 /*
508  * Add a property pair (name, string-value) into a property nvlist.
509  */
510 static int
511 add_prop_list(const char *propname, char *propval, nvlist_t **props,
512     boolean_t poolprop)
513 {
514         zpool_prop_t prop = ZPROP_INVAL;
515         zfs_prop_t fprop;
516         nvlist_t *proplist;
517         const char *normnm;
518         char *strval;
519
520         if (*props == NULL &&
521             nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
522                 (void) fprintf(stderr,
523                     gettext("internal error: out of memory\n"));
524                 return (1);
525         }
526
527         proplist = *props;
528
529         if (poolprop) {
530                 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
531
532                 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
533                     !zpool_prop_feature(propname)) {
534                         (void) fprintf(stderr, gettext("property '%s' is "
535                             "not a valid pool property\n"), propname);
536                         return (2);
537                 }
538
539                 /*
540                  * feature@ properties and version should not be specified
541                  * at the same time.
542                  */
543                 if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) &&
544                     nvlist_exists(proplist, vname)) ||
545                     (prop == ZPOOL_PROP_VERSION &&
546                     prop_list_contains_feature(proplist))) {
547                         (void) fprintf(stderr, gettext("'feature@' and "
548                             "'version' properties cannot be specified "
549                             "together\n"));
550                         return (2);
551                 }
552
553
554                 if (zpool_prop_feature(propname))
555                         normnm = propname;
556                 else
557                         normnm = zpool_prop_to_name(prop);
558         } else {
559                 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
560                         normnm = zfs_prop_to_name(fprop);
561                 } else {
562                         normnm = propname;
563                 }
564         }
565
566         if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
567             prop != ZPOOL_PROP_CACHEFILE) {
568                 (void) fprintf(stderr, gettext("property '%s' "
569                     "specified multiple times\n"), propname);
570                 return (2);
571         }
572
573         if (nvlist_add_string(proplist, normnm, propval) != 0) {
574                 (void) fprintf(stderr, gettext("internal "
575                     "error: out of memory\n"));
576                 return (1);
577         }
578
579         return (0);
580 }
581
582 /*
583  * Set a default property pair (name, string-value) in a property nvlist
584  */
585 static int
586 add_prop_list_default(const char *propname, char *propval, nvlist_t **props,
587     boolean_t poolprop)
588 {
589         char *pval;
590
591         if (nvlist_lookup_string(*props, propname, &pval) == 0)
592                 return (0);
593
594         return (add_prop_list(propname, propval, props, B_TRUE));
595 }
596
597 /*
598  * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
599  *
600  *      -f      Force addition of devices, even if they appear in use
601  *      -g      Display guid for individual vdev name.
602  *      -L      Follow links when resolving vdev path name.
603  *      -n      Do not add the devices, but display the resulting layout if
604  *              they were to be added.
605  *      -o      Set property=value.
606  *      -P      Display full path for vdev name.
607  *
608  * Adds the given vdevs to 'pool'.  As with create, the bulk of this work is
609  * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
610  * libzfs.
611  */
612 int
613 zpool_do_add(int argc, char **argv)
614 {
615         boolean_t force = B_FALSE;
616         boolean_t dryrun = B_FALSE;
617         int name_flags = 0;
618         int c;
619         nvlist_t *nvroot;
620         char *poolname;
621         int ret;
622         zpool_handle_t *zhp;
623         nvlist_t *config;
624         nvlist_t *props = NULL;
625         char *propval;
626
627         /* check options */
628         while ((c = getopt(argc, argv, "fgLno:P")) != -1) {
629                 switch (c) {
630                 case 'f':
631                         force = B_TRUE;
632                         break;
633                 case 'g':
634                         name_flags |= VDEV_NAME_GUID;
635                         break;
636                 case 'L':
637                         name_flags |= VDEV_NAME_FOLLOW_LINKS;
638                         break;
639                 case 'n':
640                         dryrun = B_TRUE;
641                         break;
642                 case 'o':
643                         if ((propval = strchr(optarg, '=')) == NULL) {
644                                 (void) fprintf(stderr, gettext("missing "
645                                     "'=' for -o option\n"));
646                                 usage(B_FALSE);
647                         }
648                         *propval = '\0';
649                         propval++;
650
651                         if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
652                             (add_prop_list(optarg, propval, &props, B_TRUE)))
653                                 usage(B_FALSE);
654                         break;
655                 case 'P':
656                         name_flags |= VDEV_NAME_PATH;
657                         break;
658                 case '?':
659                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
660                             optopt);
661                         usage(B_FALSE);
662                 }
663         }
664
665         argc -= optind;
666         argv += optind;
667
668         /* get pool name and check number of arguments */
669         if (argc < 1) {
670                 (void) fprintf(stderr, gettext("missing pool name argument\n"));
671                 usage(B_FALSE);
672         }
673         if (argc < 2) {
674                 (void) fprintf(stderr, gettext("missing vdev specification\n"));
675                 usage(B_FALSE);
676         }
677
678         poolname = argv[0];
679
680         argc--;
681         argv++;
682
683         if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
684                 return (1);
685
686         if ((config = zpool_get_config(zhp, NULL)) == NULL) {
687                 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
688                     poolname);
689                 zpool_close(zhp);
690                 return (1);
691         }
692
693         /* unless manually specified use "ashift" pool property (if set) */
694         if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) {
695                 int intval;
696                 zprop_source_t src;
697                 char strval[ZPOOL_MAXPROPLEN];
698
699                 intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src);
700                 if (src != ZPROP_SRC_DEFAULT) {
701                         (void) sprintf(strval, "%" PRId32, intval);
702                         verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval,
703                             &props, B_TRUE) == 0);
704                 }
705         }
706
707         /* pass off to get_vdev_spec for processing */
708         nvroot = make_root_vdev(zhp, props, force, !force, B_FALSE, dryrun,
709             argc, argv);
710         if (nvroot == NULL) {
711                 zpool_close(zhp);
712                 return (1);
713         }
714
715         if (dryrun) {
716                 nvlist_t *poolnvroot;
717                 nvlist_t **l2child;
718                 uint_t l2children, c;
719                 char *vname;
720                 boolean_t hadcache = B_FALSE;
721
722                 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
723                     &poolnvroot) == 0);
724
725                 (void) printf(gettext("would update '%s' to the following "
726                     "configuration:\n"), zpool_get_name(zhp));
727
728                 /* print original main pool and new tree */
729                 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE,
730                     name_flags);
731                 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE, name_flags);
732
733                 /* Do the same for the logs */
734                 if (num_logs(poolnvroot) > 0) {
735                         print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE,
736                             name_flags);
737                         print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE,
738                             name_flags);
739                 } else if (num_logs(nvroot) > 0) {
740                         print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE,
741                             name_flags);
742                 }
743
744                 /* Do the same for the caches */
745                 if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_L2CACHE,
746                     &l2child, &l2children) == 0 && l2children) {
747                         hadcache = B_TRUE;
748                         (void) printf(gettext("\tcache\n"));
749                         for (c = 0; c < l2children; c++) {
750                                 vname = zpool_vdev_name(g_zfs, NULL,
751                                     l2child[c], name_flags);
752                                 (void) printf("\t  %s\n", vname);
753                                 free(vname);
754                         }
755                 }
756                 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
757                     &l2child, &l2children) == 0 && l2children) {
758                         if (!hadcache)
759                                 (void) printf(gettext("\tcache\n"));
760                         for (c = 0; c < l2children; c++) {
761                                 vname = zpool_vdev_name(g_zfs, NULL,
762                                     l2child[c], name_flags);
763                                 (void) printf("\t  %s\n", vname);
764                                 free(vname);
765                         }
766                 }
767
768                 ret = 0;
769         } else {
770                 ret = (zpool_add(zhp, nvroot) != 0);
771         }
772
773         nvlist_free(props);
774         nvlist_free(nvroot);
775         zpool_close(zhp);
776
777         return (ret);
778 }
779
780 /*
781  * zpool remove  <pool> <vdev> ...
782  *
783  * Removes the given vdev from the pool.  Currently, this supports removing
784  * spares, cache, and log devices from the pool.
785  */
786 int
787 zpool_do_remove(int argc, char **argv)
788 {
789         char *poolname;
790         int i, ret = 0;
791         zpool_handle_t *zhp = NULL;
792
793         argc--;
794         argv++;
795
796         /* get pool name and check number of arguments */
797         if (argc < 1) {
798                 (void) fprintf(stderr, gettext("missing pool name argument\n"));
799                 usage(B_FALSE);
800         }
801         if (argc < 2) {
802                 (void) fprintf(stderr, gettext("missing device\n"));
803                 usage(B_FALSE);
804         }
805
806         poolname = argv[0];
807
808         if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
809                 return (1);
810
811         for (i = 1; i < argc; i++) {
812                 if (zpool_vdev_remove(zhp, argv[i]) != 0)
813                         ret = 1;
814         }
815         zpool_close(zhp);
816
817         return (ret);
818 }
819
820 /*
821  * zpool labelclear [-f] <vdev>
822  *
823  *      -f      Force clearing the label for the vdevs which are members of
824  *              the exported or foreign pools.
825  *
826  * Verifies that the vdev is not active and zeros out the label information
827  * on the device.
828  */
829 int
830 zpool_do_labelclear(int argc, char **argv)
831 {
832         char vdev[MAXPATHLEN];
833         char *name = NULL;
834         struct stat st;
835         int c, fd = -1, ret = 0;
836         nvlist_t *config;
837         pool_state_t state;
838         boolean_t inuse = B_FALSE;
839         boolean_t force = B_FALSE;
840
841         /* check options */
842         while ((c = getopt(argc, argv, "f")) != -1) {
843                 switch (c) {
844                 case 'f':
845                         force = B_TRUE;
846                         break;
847                 default:
848                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
849                             optopt);
850                         usage(B_FALSE);
851                 }
852         }
853
854         argc -= optind;
855         argv += optind;
856
857         /* get vdev name */
858         if (argc < 1) {
859                 (void) fprintf(stderr, gettext("missing vdev name\n"));
860                 usage(B_FALSE);
861         }
862         if (argc > 1) {
863                 (void) fprintf(stderr, gettext("too many arguments\n"));
864                 usage(B_FALSE);
865         }
866
867         /*
868          * Check if we were given absolute path and use it as is.
869          * Otherwise if the provided vdev name doesn't point to a file,
870          * try prepending expected disk paths and partition numbers.
871          */
872         (void) strlcpy(vdev, argv[0], sizeof (vdev));
873         if (vdev[0] != '/' && stat(vdev, &st) != 0) {
874                 int error;
875
876                 error = zfs_resolve_shortname(argv[0], vdev, MAXPATHLEN);
877                 if (error == 0 && zfs_dev_is_whole_disk(vdev)) {
878                         if (zfs_append_partition(vdev, MAXPATHLEN) == -1)
879                                 error = ENOENT;
880                 }
881
882                 if (error || (stat(vdev, &st) != 0)) {
883                         (void) fprintf(stderr, gettext(
884                             "failed to find device %s, try specifying absolute "
885                             "path instead\n"), argv[0]);
886                         return (1);
887                 }
888         }
889
890         if ((fd = open(vdev, O_RDWR)) < 0) {
891                 (void) fprintf(stderr, gettext("failed to open %s: %s\n"),
892                     vdev, strerror(errno));
893                 return (1);
894         }
895
896         if (ioctl(fd, BLKFLSBUF) != 0)
897                 (void) fprintf(stderr, gettext("failed to invalidate "
898                     "cache for %s: %s\n"), vdev, strerror(errno));
899
900         if (zpool_read_label(fd, &config, NULL) != 0 || config == NULL) {
901                 (void) fprintf(stderr,
902                     gettext("failed to check state for %s\n"), vdev);
903                 ret = 1;
904                 goto errout;
905         }
906         nvlist_free(config);
907
908         ret = zpool_in_use(g_zfs, fd, &state, &name, &inuse);
909         if (ret != 0) {
910                 (void) fprintf(stderr,
911                     gettext("failed to check state for %s\n"), vdev);
912                 ret = 1;
913                 goto errout;
914         }
915
916         if (!inuse)
917                 goto wipe_label;
918
919         switch (state) {
920         default:
921         case POOL_STATE_ACTIVE:
922         case POOL_STATE_SPARE:
923         case POOL_STATE_L2CACHE:
924                 (void) fprintf(stderr, gettext(
925                     "%s is a member (%s) of pool \"%s\"\n"),
926                     vdev, zpool_pool_state_to_name(state), name);
927                 ret = 1;
928                 goto errout;
929
930         case POOL_STATE_EXPORTED:
931                 if (force)
932                         break;
933                 (void) fprintf(stderr, gettext(
934                     "use '-f' to override the following error:\n"
935                     "%s is a member of exported pool \"%s\"\n"),
936                     vdev, name);
937                 ret = 1;
938                 goto errout;
939
940         case POOL_STATE_POTENTIALLY_ACTIVE:
941                 if (force)
942                         break;
943                 (void) fprintf(stderr, gettext(
944                     "use '-f' to override the following error:\n"
945                     "%s is a member of potentially active pool \"%s\"\n"),
946                     vdev, name);
947                 ret = 1;
948                 goto errout;
949
950         case POOL_STATE_DESTROYED:
951                 /* inuse should never be set for a destroyed pool */
952                 assert(0);
953                 break;
954         }
955
956 wipe_label:
957         ret = zpool_clear_label(fd);
958         if (ret != 0) {
959                 (void) fprintf(stderr,
960                     gettext("failed to clear label for %s\n"), vdev);
961         }
962
963 errout:
964         free(name);
965         (void) close(fd);
966
967         return (ret);
968 }
969
970 /*
971  * zpool create [-fnd] [-o property=value] ...
972  *              [-O file-system-property=value] ...
973  *              [-R root] [-m mountpoint] <pool> <dev> ...
974  *
975  *      -f      Force creation, even if devices appear in use
976  *      -n      Do not create the pool, but display the resulting layout if it
977  *              were to be created.
978  *      -R      Create a pool under an alternate root
979  *      -m      Set default mountpoint for the root dataset.  By default it's
980  *              '/<pool>'
981  *      -o      Set property=value.
982  *      -o      Set feature@feature=enabled|disabled.
983  *      -d      Don't automatically enable all supported pool features
984  *              (individual features can be enabled with -o).
985  *      -O      Set fsproperty=value in the pool's root file system
986  *
987  * Creates the named pool according to the given vdev specification.  The
988  * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c.  Once
989  * we get the nvlist back from get_vdev_spec(), we either print out the contents
990  * (if '-n' was specified), or pass it to libzfs to do the creation.
991  */
992 int
993 zpool_do_create(int argc, char **argv)
994 {
995         boolean_t force = B_FALSE;
996         boolean_t dryrun = B_FALSE;
997         boolean_t enable_all_pool_feat = B_TRUE;
998         int c;
999         nvlist_t *nvroot = NULL;
1000         char *poolname;
1001         char *tname = NULL;
1002         int ret = 1;
1003         char *altroot = NULL;
1004         char *mountpoint = NULL;
1005         nvlist_t *fsprops = NULL;
1006         nvlist_t *props = NULL;
1007         char *propval;
1008
1009         /* check options */
1010         while ((c = getopt(argc, argv, ":fndR:m:o:O:t:")) != -1) {
1011                 switch (c) {
1012                 case 'f':
1013                         force = B_TRUE;
1014                         break;
1015                 case 'n':
1016                         dryrun = B_TRUE;
1017                         break;
1018                 case 'd':
1019                         enable_all_pool_feat = B_FALSE;
1020                         break;
1021                 case 'R':
1022                         altroot = optarg;
1023                         if (add_prop_list(zpool_prop_to_name(
1024                             ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1025                                 goto errout;
1026                         if (add_prop_list_default(zpool_prop_to_name(
1027                             ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1028                                 goto errout;
1029                         break;
1030                 case 'm':
1031                         /* Equivalent to -O mountpoint=optarg */
1032                         mountpoint = optarg;
1033                         break;
1034                 case 'o':
1035                         if ((propval = strchr(optarg, '=')) == NULL) {
1036                                 (void) fprintf(stderr, gettext("missing "
1037                                     "'=' for -o option\n"));
1038                                 goto errout;
1039                         }
1040                         *propval = '\0';
1041                         propval++;
1042
1043                         if (add_prop_list(optarg, propval, &props, B_TRUE))
1044                                 goto errout;
1045
1046                         /*
1047                          * If the user is creating a pool that doesn't support
1048                          * feature flags, don't enable any features.
1049                          */
1050                         if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
1051                                 char *end;
1052                                 u_longlong_t ver;
1053
1054                                 ver = strtoull(propval, &end, 10);
1055                                 if (*end == '\0' &&
1056                                     ver < SPA_VERSION_FEATURES) {
1057                                         enable_all_pool_feat = B_FALSE;
1058                                 }
1059                         }
1060                         if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT)
1061                                 altroot = propval;
1062                         break;
1063                 case 'O':
1064                         if ((propval = strchr(optarg, '=')) == NULL) {
1065                                 (void) fprintf(stderr, gettext("missing "
1066                                     "'=' for -O option\n"));
1067                                 goto errout;
1068                         }
1069                         *propval = '\0';
1070                         propval++;
1071
1072                         /*
1073                          * Mountpoints are checked and then added later.
1074                          * Uniquely among properties, they can be specified
1075                          * more than once, to avoid conflict with -m.
1076                          */
1077                         if (0 == strcmp(optarg,
1078                             zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
1079                                 mountpoint = propval;
1080                         } else if (add_prop_list(optarg, propval, &fsprops,
1081                             B_FALSE)) {
1082                                 goto errout;
1083                         }
1084                         break;
1085                 case 't':
1086                         /*
1087                          * Sanity check temporary pool name.
1088                          */
1089                         if (strchr(optarg, '/') != NULL) {
1090                                 (void) fprintf(stderr, gettext("cannot create "
1091                                     "'%s': invalid character '/' in temporary "
1092                                     "name\n"), optarg);
1093                                 (void) fprintf(stderr, gettext("use 'zfs "
1094                                     "create' to create a dataset\n"));
1095                                 goto errout;
1096                         }
1097
1098                         if (add_prop_list(zpool_prop_to_name(
1099                             ZPOOL_PROP_TNAME), optarg, &props, B_TRUE))
1100                                 goto errout;
1101                         if (add_prop_list_default(zpool_prop_to_name(
1102                             ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1103                                 goto errout;
1104                         tname = optarg;
1105                         break;
1106                 case ':':
1107                         (void) fprintf(stderr, gettext("missing argument for "
1108                             "'%c' option\n"), optopt);
1109                         goto badusage;
1110                 case '?':
1111                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1112                             optopt);
1113                         goto badusage;
1114                 }
1115         }
1116
1117         argc -= optind;
1118         argv += optind;
1119
1120         /* get pool name and check number of arguments */
1121         if (argc < 1) {
1122                 (void) fprintf(stderr, gettext("missing pool name argument\n"));
1123                 goto badusage;
1124         }
1125         if (argc < 2) {
1126                 (void) fprintf(stderr, gettext("missing vdev specification\n"));
1127                 goto badusage;
1128         }
1129
1130         poolname = argv[0];
1131
1132         /*
1133          * As a special case, check for use of '/' in the name, and direct the
1134          * user to use 'zfs create' instead.
1135          */
1136         if (strchr(poolname, '/') != NULL) {
1137                 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
1138                     "character '/' in pool name\n"), poolname);
1139                 (void) fprintf(stderr, gettext("use 'zfs create' to "
1140                     "create a dataset\n"));
1141                 goto errout;
1142         }
1143
1144         /* pass off to get_vdev_spec for bulk processing */
1145         nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun,
1146             argc - 1, argv + 1);
1147         if (nvroot == NULL)
1148                 goto errout;
1149
1150         /* make_root_vdev() allows 0 toplevel children if there are spares */
1151         if (!zfs_allocatable_devs(nvroot)) {
1152                 (void) fprintf(stderr, gettext("invalid vdev "
1153                     "specification: at least one toplevel vdev must be "
1154                     "specified\n"));
1155                 goto errout;
1156         }
1157
1158         if (altroot != NULL && altroot[0] != '/') {
1159                 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
1160                     "must be an absolute path\n"), altroot);
1161                 goto errout;
1162         }
1163
1164         /*
1165          * Check the validity of the mountpoint and direct the user to use the
1166          * '-m' mountpoint option if it looks like its in use.
1167          */
1168         if (mountpoint == NULL ||
1169             (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
1170             strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
1171                 char buf[MAXPATHLEN];
1172                 DIR *dirp;
1173
1174                 if (mountpoint && mountpoint[0] != '/') {
1175                         (void) fprintf(stderr, gettext("invalid mountpoint "
1176                             "'%s': must be an absolute path, 'legacy', or "
1177                             "'none'\n"), mountpoint);
1178                         goto errout;
1179                 }
1180
1181                 if (mountpoint == NULL) {
1182                         if (altroot != NULL)
1183                                 (void) snprintf(buf, sizeof (buf), "%s/%s",
1184                                     altroot, poolname);
1185                         else
1186                                 (void) snprintf(buf, sizeof (buf), "/%s",
1187                                     poolname);
1188                 } else {
1189                         if (altroot != NULL)
1190                                 (void) snprintf(buf, sizeof (buf), "%s%s",
1191                                     altroot, mountpoint);
1192                         else
1193                                 (void) snprintf(buf, sizeof (buf), "%s",
1194                                     mountpoint);
1195                 }
1196
1197                 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
1198                         (void) fprintf(stderr, gettext("mountpoint '%s' : "
1199                             "%s\n"), buf, strerror(errno));
1200                         (void) fprintf(stderr, gettext("use '-m' "
1201                             "option to provide a different default\n"));
1202                         goto errout;
1203                 } else if (dirp) {
1204                         int count = 0;
1205
1206                         while (count < 3 && readdir(dirp) != NULL)
1207                                 count++;
1208                         (void) closedir(dirp);
1209
1210                         if (count > 2) {
1211                                 (void) fprintf(stderr, gettext("mountpoint "
1212                                     "'%s' exists and is not empty\n"), buf);
1213                                 (void) fprintf(stderr, gettext("use '-m' "
1214                                     "option to provide a "
1215                                     "different default\n"));
1216                                 goto errout;
1217                         }
1218                 }
1219         }
1220
1221         /*
1222          * Now that the mountpoint's validity has been checked, ensure that
1223          * the property is set appropriately prior to creating the pool.
1224          */
1225         if (mountpoint != NULL) {
1226                 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1227                     mountpoint, &fsprops, B_FALSE);
1228                 if (ret != 0)
1229                         goto errout;
1230         }
1231
1232         ret = 1;
1233         if (dryrun) {
1234                 /*
1235                  * For a dry run invocation, print out a basic message and run
1236                  * through all the vdevs in the list and print out in an
1237                  * appropriate hierarchy.
1238                  */
1239                 (void) printf(gettext("would create '%s' with the "
1240                     "following layout:\n\n"), poolname);
1241
1242                 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE, 0);
1243                 if (num_logs(nvroot) > 0)
1244                         print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE, 0);
1245
1246                 ret = 0;
1247         } else {
1248                 /*
1249                  * Hand off to libzfs.
1250                  */
1251                 spa_feature_t i;
1252                 for (i = 0; i < SPA_FEATURES; i++) {
1253                         char propname[MAXPATHLEN];
1254                         char *propval;
1255                         zfeature_info_t *feat = &spa_feature_table[i];
1256
1257                         (void) snprintf(propname, sizeof (propname),
1258                             "feature@%s", feat->fi_uname);
1259
1260                         /*
1261                          * Only features contained in props will be enabled:
1262                          * remove from the nvlist every ZFS_FEATURE_DISABLED
1263                          * value and add every missing ZFS_FEATURE_ENABLED if
1264                          * enable_all_pool_feat is set.
1265                          */
1266                         if (!nvlist_lookup_string(props, propname, &propval)) {
1267                                 if (strcmp(propval, ZFS_FEATURE_DISABLED) == 0)
1268                                         (void) nvlist_remove_all(props,
1269                                             propname);
1270                         } else if (enable_all_pool_feat) {
1271                                 ret = add_prop_list(propname,
1272                                     ZFS_FEATURE_ENABLED, &props, B_TRUE);
1273                                 if (ret != 0)
1274                                         goto errout;
1275                         }
1276                 }
1277
1278                 ret = 1;
1279                 if (zpool_create(g_zfs, poolname,
1280                     nvroot, props, fsprops) == 0) {
1281                         zfs_handle_t *pool = zfs_open(g_zfs,
1282                             tname ? tname : poolname, ZFS_TYPE_FILESYSTEM);
1283                         if (pool != NULL) {
1284                                 if (zfs_mount(pool, NULL, 0) == 0)
1285                                         ret = zfs_shareall(pool);
1286                                 zfs_close(pool);
1287                         }
1288                 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1289                         (void) fprintf(stderr, gettext("pool name may have "
1290                             "been omitted\n"));
1291                 }
1292         }
1293
1294 errout:
1295         nvlist_free(nvroot);
1296         nvlist_free(fsprops);
1297         nvlist_free(props);
1298         return (ret);
1299 badusage:
1300         nvlist_free(fsprops);
1301         nvlist_free(props);
1302         usage(B_FALSE);
1303         return (2);
1304 }
1305
1306 /*
1307  * zpool destroy <pool>
1308  *
1309  *      -f      Forcefully unmount any datasets
1310  *
1311  * Destroy the given pool.  Automatically unmounts any datasets in the pool.
1312  */
1313 int
1314 zpool_do_destroy(int argc, char **argv)
1315 {
1316         boolean_t force = B_FALSE;
1317         int c;
1318         char *pool;
1319         zpool_handle_t *zhp;
1320         int ret;
1321
1322         /* check options */
1323         while ((c = getopt(argc, argv, "f")) != -1) {
1324                 switch (c) {
1325                 case 'f':
1326                         force = B_TRUE;
1327                         break;
1328                 case '?':
1329                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1330                             optopt);
1331                         usage(B_FALSE);
1332                 }
1333         }
1334
1335         argc -= optind;
1336         argv += optind;
1337
1338         /* check arguments */
1339         if (argc < 1) {
1340                 (void) fprintf(stderr, gettext("missing pool argument\n"));
1341                 usage(B_FALSE);
1342         }
1343         if (argc > 1) {
1344                 (void) fprintf(stderr, gettext("too many arguments\n"));
1345                 usage(B_FALSE);
1346         }
1347
1348         pool = argv[0];
1349
1350         if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1351                 /*
1352                  * As a special case, check for use of '/' in the name, and
1353                  * direct the user to use 'zfs destroy' instead.
1354                  */
1355                 if (strchr(pool, '/') != NULL)
1356                         (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1357                             "destroy a dataset\n"));
1358                 return (1);
1359         }
1360
1361         if (zpool_disable_datasets(zhp, force) != 0) {
1362                 (void) fprintf(stderr, gettext("could not destroy '%s': "
1363                     "could not unmount datasets\n"), zpool_get_name(zhp));
1364                 zpool_close(zhp);
1365                 return (1);
1366         }
1367
1368         /* The history must be logged as part of the export */
1369         log_history = B_FALSE;
1370
1371         ret = (zpool_destroy(zhp, history_str) != 0);
1372
1373         zpool_close(zhp);
1374
1375         return (ret);
1376 }
1377
1378 typedef struct export_cbdata {
1379         boolean_t force;
1380         boolean_t hardforce;
1381 } export_cbdata_t;
1382
1383 /*
1384  * Export one pool
1385  */
1386 int
1387 zpool_export_one(zpool_handle_t *zhp, void *data)
1388 {
1389         export_cbdata_t *cb = data;
1390
1391         if (zpool_disable_datasets(zhp, cb->force) != 0)
1392                 return (1);
1393
1394         /* The history must be logged as part of the export */
1395         log_history = B_FALSE;
1396
1397         if (cb->hardforce) {
1398                 if (zpool_export_force(zhp, history_str) != 0)
1399                         return (1);
1400         } else if (zpool_export(zhp, cb->force, history_str) != 0) {
1401                 return (1);
1402         }
1403
1404         return (0);
1405 }
1406
1407 /*
1408  * zpool export [-f] <pool> ...
1409  *
1410  *      -a      Export all pools
1411  *      -f      Forcefully unmount datasets
1412  *
1413  * Export the given pools.  By default, the command will attempt to cleanly
1414  * unmount any active datasets within the pool.  If the '-f' flag is specified,
1415  * then the datasets will be forcefully unmounted.
1416  */
1417 int
1418 zpool_do_export(int argc, char **argv)
1419 {
1420         export_cbdata_t cb;
1421         boolean_t do_all = B_FALSE;
1422         boolean_t force = B_FALSE;
1423         boolean_t hardforce = B_FALSE;
1424         int c, ret;
1425
1426         /* check options */
1427         while ((c = getopt(argc, argv, "afF")) != -1) {
1428                 switch (c) {
1429                 case 'a':
1430                         do_all = B_TRUE;
1431                         break;
1432                 case 'f':
1433                         force = B_TRUE;
1434                         break;
1435                 case 'F':
1436                         hardforce = B_TRUE;
1437                         break;
1438                 case '?':
1439                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1440                             optopt);
1441                         usage(B_FALSE);
1442                 }
1443         }
1444
1445         cb.force = force;
1446         cb.hardforce = hardforce;
1447         argc -= optind;
1448         argv += optind;
1449
1450         if (do_all) {
1451                 if (argc != 0) {
1452                         (void) fprintf(stderr, gettext("too many arguments\n"));
1453                         usage(B_FALSE);
1454                 }
1455
1456                 return (for_each_pool(argc, argv, B_TRUE, NULL,
1457                     zpool_export_one, &cb));
1458         }
1459
1460         /* check arguments */
1461         if (argc < 1) {
1462                 (void) fprintf(stderr, gettext("missing pool argument\n"));
1463                 usage(B_FALSE);
1464         }
1465
1466         ret = for_each_pool(argc, argv, B_TRUE, NULL, zpool_export_one, &cb);
1467
1468         return (ret);
1469 }
1470
1471 /*
1472  * Given a vdev configuration, determine the maximum width needed for the device
1473  * name column.
1474  */
1475 static int
1476 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max,
1477     int name_flags)
1478 {
1479         char *name;
1480         nvlist_t **child;
1481         uint_t c, children;
1482         int ret;
1483
1484         name = zpool_vdev_name(g_zfs, zhp, nv, name_flags);
1485         if (strlen(name) + depth > max)
1486                 max = strlen(name) + depth;
1487
1488         free(name);
1489
1490         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1491             &child, &children) == 0) {
1492                 for (c = 0; c < children; c++)
1493                         if ((ret = max_width(zhp, child[c], depth + 2,
1494                             max, name_flags)) > max)
1495                                 max = ret;
1496         }
1497
1498         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1499             &child, &children) == 0) {
1500                 for (c = 0; c < children; c++)
1501                         if ((ret = max_width(zhp, child[c], depth + 2,
1502                             max, name_flags)) > max)
1503                                 max = ret;
1504         }
1505
1506         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1507             &child, &children) == 0) {
1508                 for (c = 0; c < children; c++)
1509                         if ((ret = max_width(zhp, child[c], depth + 2,
1510                             max, name_flags)) > max)
1511                                 max = ret;
1512         }
1513
1514         return (max);
1515 }
1516
1517 typedef struct spare_cbdata {
1518         uint64_t        cb_guid;
1519         zpool_handle_t  *cb_zhp;
1520 } spare_cbdata_t;
1521
1522 static boolean_t
1523 find_vdev(nvlist_t *nv, uint64_t search)
1524 {
1525         uint64_t guid;
1526         nvlist_t **child;
1527         uint_t c, children;
1528
1529         if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1530             search == guid)
1531                 return (B_TRUE);
1532
1533         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1534             &child, &children) == 0) {
1535                 for (c = 0; c < children; c++)
1536                         if (find_vdev(child[c], search))
1537                                 return (B_TRUE);
1538         }
1539
1540         return (B_FALSE);
1541 }
1542
1543 static int
1544 find_spare(zpool_handle_t *zhp, void *data)
1545 {
1546         spare_cbdata_t *cbp = data;
1547         nvlist_t *config, *nvroot;
1548
1549         config = zpool_get_config(zhp, NULL);
1550         verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1551             &nvroot) == 0);
1552
1553         if (find_vdev(nvroot, cbp->cb_guid)) {
1554                 cbp->cb_zhp = zhp;
1555                 return (1);
1556         }
1557
1558         zpool_close(zhp);
1559         return (0);
1560 }
1561
1562 typedef struct status_cbdata {
1563         int             cb_count;
1564         int             cb_name_flags;
1565         int             cb_namewidth;
1566         boolean_t       cb_allpools;
1567         boolean_t       cb_verbose;
1568         boolean_t       cb_explain;
1569         boolean_t       cb_first;
1570         boolean_t       cb_dedup_stats;
1571         boolean_t       cb_print_status;
1572         vdev_cmd_data_list_t    *vcdl;
1573 } status_cbdata_t;
1574
1575 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
1576 static int
1577 is_blank_str(char *str)
1578 {
1579         while (str != NULL && *str != '\0') {
1580                 if (!isblank(*str))
1581                         return (0);
1582                 str++;
1583         }
1584         return (1);
1585 }
1586
1587 /* Print command output lines for specific vdev in a specific pool */
1588 static void
1589 zpool_print_cmd(vdev_cmd_data_list_t *vcdl, const char *pool, char *path)
1590 {
1591         vdev_cmd_data_t *data;
1592         int i, j;
1593         char *val;
1594
1595         for (i = 0; i < vcdl->count; i++) {
1596                 if ((strcmp(vcdl->data[i].path, path) != 0) ||
1597                     (strcmp(vcdl->data[i].pool, pool) != 0)) {
1598                         /* Not the vdev we're looking for */
1599                         continue;
1600                 }
1601
1602                 data = &vcdl->data[i];
1603                 /* Print out all the output values for this vdev */
1604                 for (j = 0; j < vcdl->uniq_cols_cnt; j++) {
1605                         val = NULL;
1606                         /* Does this vdev have values for this column? */
1607                         for (int k = 0; k < data->cols_cnt; k++) {
1608                                 if (strcmp(data->cols[k],
1609                                     vcdl->uniq_cols[j]) == 0) {
1610                                         /* yes it does, record the value */
1611                                         val = data->lines[k];
1612                                         break;
1613                                 }
1614                         }
1615                         /*
1616                          * Mark empty values with dashes to make output
1617                          * awk-able.
1618                          */
1619                         if (is_blank_str(val))
1620                                 val = "-";
1621
1622                         printf("%*s", vcdl->uniq_cols_width[j], val);
1623                         if (j < vcdl->uniq_cols_cnt - 1)
1624                                 printf("  ");
1625                 }
1626
1627                 /* Print out any values that aren't in a column at the end */
1628                 for (j = data->cols_cnt; j < data->lines_cnt; j++) {
1629                         /* Did we have any columns?  If so print a spacer. */
1630                         if (vcdl->uniq_cols_cnt > 0)
1631                                 printf("  ");
1632
1633                         val = data->lines[j];
1634                         printf("%s", val ? val : "");
1635                 }
1636                 break;
1637         }
1638 }
1639
1640 /*
1641  * Print out configuration state as requested by status_callback.
1642  */
1643 static void
1644 print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
1645     nvlist_t *nv, int depth, boolean_t isspare)
1646 {
1647         nvlist_t **child;
1648         uint_t c, children;
1649         pool_scan_stat_t *ps = NULL;
1650         vdev_stat_t *vs;
1651         char rbuf[6], wbuf[6], cbuf[6];
1652         char *vname;
1653         uint64_t notpresent;
1654         spare_cbdata_t spare_cb;
1655         char *state;
1656         char *path = NULL;
1657
1658         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1659             &child, &children) != 0)
1660                 children = 0;
1661
1662         verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1663             (uint64_t **)&vs, &c) == 0);
1664
1665         state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1666         if (isspare) {
1667                 /*
1668                  * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1669                  * online drives.
1670                  */
1671                 if (vs->vs_aux == VDEV_AUX_SPARED)
1672                         state = "INUSE";
1673                 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1674                         state = "AVAIL";
1675         }
1676
1677         (void) printf("\t%*s%-*s  %-8s", depth, "", cb->cb_namewidth - depth,
1678             name, state);
1679
1680         if (!isspare) {
1681                 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1682                 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1683                 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1684                 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1685         }
1686
1687         if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1688             &notpresent) == 0) {
1689                 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1690                 (void) printf("  was %s", path);
1691         } else if (vs->vs_aux != 0) {
1692                 (void) printf("  ");
1693
1694                 switch (vs->vs_aux) {
1695                 case VDEV_AUX_OPEN_FAILED:
1696                         (void) printf(gettext("cannot open"));
1697                         break;
1698
1699                 case VDEV_AUX_BAD_GUID_SUM:
1700                         (void) printf(gettext("missing device"));
1701                         break;
1702
1703                 case VDEV_AUX_NO_REPLICAS:
1704                         (void) printf(gettext("insufficient replicas"));
1705                         break;
1706
1707                 case VDEV_AUX_VERSION_NEWER:
1708                         (void) printf(gettext("newer version"));
1709                         break;
1710
1711                 case VDEV_AUX_UNSUP_FEAT:
1712                         (void) printf(gettext("unsupported feature(s)"));
1713                         break;
1714
1715                 case VDEV_AUX_SPARED:
1716                         verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1717                             &spare_cb.cb_guid) == 0);
1718                         if (zpool_iter(g_zfs, find_spare, &spare_cb) == 1) {
1719                                 if (strcmp(zpool_get_name(spare_cb.cb_zhp),
1720                                     zpool_get_name(zhp)) == 0)
1721                                         (void) printf(gettext("currently in "
1722                                             "use"));
1723                                 else
1724                                         (void) printf(gettext("in use by "
1725                                             "pool '%s'"),
1726                                             zpool_get_name(spare_cb.cb_zhp));
1727                                 zpool_close(spare_cb.cb_zhp);
1728                         } else {
1729                                 (void) printf(gettext("currently in use"));
1730                         }
1731                         break;
1732
1733                 case VDEV_AUX_ERR_EXCEEDED:
1734                         (void) printf(gettext("too many errors"));
1735                         break;
1736
1737                 case VDEV_AUX_IO_FAILURE:
1738                         (void) printf(gettext("experienced I/O failures"));
1739                         break;
1740
1741                 case VDEV_AUX_BAD_LOG:
1742                         (void) printf(gettext("bad intent log"));
1743                         break;
1744
1745                 case VDEV_AUX_EXTERNAL:
1746                         (void) printf(gettext("external device fault"));
1747                         break;
1748
1749                 case VDEV_AUX_SPLIT_POOL:
1750                         (void) printf(gettext("split into new pool"));
1751                         break;
1752
1753                 case VDEV_AUX_ACTIVE:
1754                         (void) printf(gettext("currently in use"));
1755                         break;
1756
1757                 default:
1758                         (void) printf(gettext("corrupted data"));
1759                         break;
1760                 }
1761         }
1762
1763         (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1764             (uint64_t **)&ps, &c);
1765
1766         if (ps != NULL && ps->pss_state == DSS_SCANNING &&
1767             vs->vs_scan_processed != 0 && children == 0) {
1768                 (void) printf(gettext("  (%s)"),
1769                     (ps->pss_func == POOL_SCAN_RESILVER) ?
1770                     "resilvering" : "repairing");
1771         }
1772
1773         if (cb->vcdl != NULL) {
1774                 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
1775                         printf("  ");
1776                         zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
1777                 }
1778         }
1779
1780         (void) printf("\n");
1781
1782         for (c = 0; c < children; c++) {
1783                 uint64_t islog = B_FALSE, ishole = B_FALSE;
1784
1785                 /* Don't print logs or holes here */
1786                 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1787                     &islog);
1788                 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1789                     &ishole);
1790                 if (islog || ishole)
1791                         continue;
1792                 vname = zpool_vdev_name(g_zfs, zhp, child[c],
1793                     cb->cb_name_flags | VDEV_NAME_TYPE_ID);
1794                 print_status_config(zhp, cb, vname, child[c], depth + 2,
1795                     isspare);
1796                 free(vname);
1797         }
1798 }
1799
1800 /*
1801  * Print the configuration of an exported pool.  Iterate over all vdevs in the
1802  * pool, printing out the name and status for each one.
1803  */
1804 static void
1805 print_import_config(status_cbdata_t *cb, const char *name, nvlist_t *nv,
1806     int depth)
1807 {
1808         nvlist_t **child;
1809         uint_t c, children;
1810         vdev_stat_t *vs;
1811         char *type, *vname;
1812
1813         verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1814         if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1815             strcmp(type, VDEV_TYPE_HOLE) == 0)
1816                 return;
1817
1818         verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1819             (uint64_t **)&vs, &c) == 0);
1820
1821         (void) printf("\t%*s%-*s", depth, "", cb->cb_namewidth - depth, name);
1822         (void) printf("  %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1823
1824         if (vs->vs_aux != 0) {
1825                 (void) printf("  ");
1826
1827                 switch (vs->vs_aux) {
1828                 case VDEV_AUX_OPEN_FAILED:
1829                         (void) printf(gettext("cannot open"));
1830                         break;
1831
1832                 case VDEV_AUX_BAD_GUID_SUM:
1833                         (void) printf(gettext("missing device"));
1834                         break;
1835
1836                 case VDEV_AUX_NO_REPLICAS:
1837                         (void) printf(gettext("insufficient replicas"));
1838                         break;
1839
1840                 case VDEV_AUX_VERSION_NEWER:
1841                         (void) printf(gettext("newer version"));
1842                         break;
1843
1844                 case VDEV_AUX_UNSUP_FEAT:
1845                         (void) printf(gettext("unsupported feature(s)"));
1846                         break;
1847
1848                 case VDEV_AUX_ERR_EXCEEDED:
1849                         (void) printf(gettext("too many errors"));
1850                         break;
1851
1852                 case VDEV_AUX_ACTIVE:
1853                         (void) printf(gettext("currently in use"));
1854                         break;
1855
1856                 default:
1857                         (void) printf(gettext("corrupted data"));
1858                         break;
1859                 }
1860         }
1861         (void) printf("\n");
1862
1863         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1864             &child, &children) != 0)
1865                 return;
1866
1867         for (c = 0; c < children; c++) {
1868                 uint64_t is_log = B_FALSE;
1869
1870                 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1871                     &is_log);
1872                 if (is_log)
1873                         continue;
1874
1875                 vname = zpool_vdev_name(g_zfs, NULL, child[c],
1876                     cb->cb_name_flags | VDEV_NAME_TYPE_ID);
1877                 print_import_config(cb, vname, child[c], depth + 2);
1878                 free(vname);
1879         }
1880
1881         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1882             &child, &children) == 0) {
1883                 (void) printf(gettext("\tcache\n"));
1884                 for (c = 0; c < children; c++) {
1885                         vname = zpool_vdev_name(g_zfs, NULL, child[c],
1886                             cb->cb_name_flags);
1887                         (void) printf("\t  %s\n", vname);
1888                         free(vname);
1889                 }
1890         }
1891
1892         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1893             &child, &children) == 0) {
1894                 (void) printf(gettext("\tspares\n"));
1895                 for (c = 0; c < children; c++) {
1896                         vname = zpool_vdev_name(g_zfs, NULL, child[c],
1897                             cb->cb_name_flags);
1898                         (void) printf("\t  %s\n", vname);
1899                         free(vname);
1900                 }
1901         }
1902 }
1903
1904 /*
1905  * Print log vdevs.
1906  * Logs are recorded as top level vdevs in the main pool child array
1907  * but with "is_log" set to 1. We use either print_status_config() or
1908  * print_import_config() to print the top level logs then any log
1909  * children (eg mirrored slogs) are printed recursively - which
1910  * works because only the top level vdev is marked "is_log"
1911  */
1912 static void
1913 print_logs(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv)
1914 {
1915         uint_t c, children;
1916         nvlist_t **child;
1917
1918         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1919             &children) != 0)
1920                 return;
1921
1922         (void) printf(gettext("\tlogs\n"));
1923
1924         for (c = 0; c < children; c++) {
1925                 uint64_t is_log = B_FALSE;
1926                 char *name;
1927
1928                 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1929                     &is_log);
1930                 if (!is_log)
1931                         continue;
1932                 name = zpool_vdev_name(g_zfs, zhp, child[c],
1933                     cb->cb_name_flags | VDEV_NAME_TYPE_ID);
1934                 if (cb->cb_print_status)
1935                         print_status_config(zhp, cb, name, child[c], 2,
1936                             B_FALSE);
1937                 else
1938                         print_import_config(cb, name, child[c], 2);
1939                 free(name);
1940         }
1941 }
1942
1943 /*
1944  * Display the status for the given pool.
1945  */
1946 static void
1947 show_import(nvlist_t *config)
1948 {
1949         uint64_t pool_state;
1950         vdev_stat_t *vs;
1951         char *name;
1952         uint64_t guid;
1953         uint64_t hostid = 0;
1954         char *msgid;
1955         char *hostname = "unknown";
1956         nvlist_t *nvroot, *nvinfo;
1957         zpool_status_t reason;
1958         zpool_errata_t errata;
1959         const char *health;
1960         uint_t vsc;
1961         char *comment;
1962         status_cbdata_t cb = { 0 };
1963
1964         verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1965             &name) == 0);
1966         verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1967             &guid) == 0);
1968         verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1969             &pool_state) == 0);
1970         verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1971             &nvroot) == 0);
1972
1973         verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1974             (uint64_t **)&vs, &vsc) == 0);
1975         health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1976
1977         reason = zpool_import_status(config, &msgid, &errata);
1978
1979         (void) printf(gettext("   pool: %s\n"), name);
1980         (void) printf(gettext("     id: %llu\n"), (u_longlong_t)guid);
1981         (void) printf(gettext("  state: %s"), health);
1982         if (pool_state == POOL_STATE_DESTROYED)
1983                 (void) printf(gettext(" (DESTROYED)"));
1984         (void) printf("\n");
1985
1986         switch (reason) {
1987         case ZPOOL_STATUS_MISSING_DEV_R:
1988         case ZPOOL_STATUS_MISSING_DEV_NR:
1989         case ZPOOL_STATUS_BAD_GUID_SUM:
1990                 (void) printf(gettext(" status: One or more devices are "
1991                     "missing from the system.\n"));
1992                 break;
1993
1994         case ZPOOL_STATUS_CORRUPT_LABEL_R:
1995         case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1996                 (void) printf(gettext(" status: One or more devices contains "
1997                     "corrupted data.\n"));
1998                 break;
1999
2000         case ZPOOL_STATUS_CORRUPT_DATA:
2001                 (void) printf(
2002                     gettext(" status: The pool data is corrupted.\n"));
2003                 break;
2004
2005         case ZPOOL_STATUS_OFFLINE_DEV:
2006                 (void) printf(gettext(" status: One or more devices "
2007                     "are offlined.\n"));
2008                 break;
2009
2010         case ZPOOL_STATUS_CORRUPT_POOL:
2011                 (void) printf(gettext(" status: The pool metadata is "
2012                     "corrupted.\n"));
2013                 break;
2014
2015         case ZPOOL_STATUS_VERSION_OLDER:
2016                 (void) printf(gettext(" status: The pool is formatted using a "
2017                     "legacy on-disk version.\n"));
2018                 break;
2019
2020         case ZPOOL_STATUS_VERSION_NEWER:
2021                 (void) printf(gettext(" status: The pool is formatted using an "
2022                     "incompatible version.\n"));
2023                 break;
2024
2025         case ZPOOL_STATUS_FEAT_DISABLED:
2026                 (void) printf(gettext(" status: Some supported features are "
2027                     "not enabled on the pool.\n"));
2028                 break;
2029
2030         case ZPOOL_STATUS_UNSUP_FEAT_READ:
2031                 (void) printf(gettext("status: The pool uses the following "
2032                     "feature(s) not supported on this system:\n"));
2033                 zpool_print_unsup_feat(config);
2034                 break;
2035
2036         case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
2037                 (void) printf(gettext("status: The pool can only be accessed "
2038                     "in read-only mode on this system. It\n\tcannot be "
2039                     "accessed in read-write mode because it uses the "
2040                     "following\n\tfeature(s) not supported on this system:\n"));
2041                 zpool_print_unsup_feat(config);
2042                 break;
2043
2044         case ZPOOL_STATUS_HOSTID_ACTIVE:
2045                 (void) printf(gettext(" status: The pool is currently "
2046                     "imported by another system.\n"));
2047                 break;
2048
2049         case ZPOOL_STATUS_HOSTID_REQUIRED:
2050                 (void) printf(gettext(" status: The pool has the "
2051                     "multihost property on.  It cannot\n\tbe safely imported "
2052                     "when the system hostid is not set.\n"));
2053                 break;
2054
2055         case ZPOOL_STATUS_HOSTID_MISMATCH:
2056                 (void) printf(gettext(" status: The pool was last accessed by "
2057                     "another system.\n"));
2058                 break;
2059
2060         case ZPOOL_STATUS_FAULTED_DEV_R:
2061         case ZPOOL_STATUS_FAULTED_DEV_NR:
2062                 (void) printf(gettext(" status: One or more devices are "
2063                     "faulted.\n"));
2064                 break;
2065
2066         case ZPOOL_STATUS_BAD_LOG:
2067                 (void) printf(gettext(" status: An intent log record cannot be "
2068                     "read.\n"));
2069                 break;
2070
2071         case ZPOOL_STATUS_RESILVERING:
2072                 (void) printf(gettext(" status: One or more devices were being "
2073                     "resilvered.\n"));
2074                 break;
2075
2076         case ZPOOL_STATUS_ERRATA:
2077                 (void) printf(gettext(" status: Errata #%d detected.\n"),
2078                     errata);
2079                 break;
2080
2081         default:
2082                 /*
2083                  * No other status can be seen when importing pools.
2084                  */
2085                 assert(reason == ZPOOL_STATUS_OK);
2086         }
2087
2088         /*
2089          * Print out an action according to the overall state of the pool.
2090          */
2091         if (vs->vs_state == VDEV_STATE_HEALTHY) {
2092                 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
2093                     reason == ZPOOL_STATUS_FEAT_DISABLED) {
2094                         (void) printf(gettext(" action: The pool can be "
2095                             "imported using its name or numeric identifier, "
2096                             "though\n\tsome features will not be available "
2097                             "without an explicit 'zpool upgrade'.\n"));
2098                 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
2099                         (void) printf(gettext(" action: The pool can be "
2100                             "imported using its name or numeric "
2101                             "identifier and\n\tthe '-f' flag.\n"));
2102                 } else if (reason == ZPOOL_STATUS_ERRATA) {
2103                         switch (errata) {
2104                         case ZPOOL_ERRATA_NONE:
2105                                 break;
2106
2107                         case ZPOOL_ERRATA_ZOL_2094_SCRUB:
2108                                 (void) printf(gettext(" action: The pool can "
2109                                     "be imported using its name or numeric "
2110                                     "identifier,\n\thowever there is a compat"
2111                                     "ibility issue which should be corrected"
2112                                     "\n\tby running 'zpool scrub'\n"));
2113                                 break;
2114
2115                         case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY:
2116                                 (void) printf(gettext(" action: The pool can"
2117                                     "not be imported with this version of ZFS "
2118                                     "due to\n\tan active asynchronous destroy. "
2119                                     "Revert to an earlier version\n\tand "
2120                                     "allow the destroy to complete before "
2121                                     "updating.\n"));
2122                                 break;
2123
2124                         default:
2125                                 /*
2126                                  * All errata must contain an action message.
2127                                  */
2128                                 assert(0);
2129                         }
2130                 } else {
2131                         (void) printf(gettext(" action: The pool can be "
2132                             "imported using its name or numeric "
2133                             "identifier.\n"));
2134                 }
2135         } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
2136                 (void) printf(gettext(" action: The pool can be imported "
2137                     "despite missing or damaged devices.  The\n\tfault "
2138                     "tolerance of the pool may be compromised if imported.\n"));
2139         } else {
2140                 switch (reason) {
2141                 case ZPOOL_STATUS_VERSION_NEWER:
2142                         (void) printf(gettext(" action: The pool cannot be "
2143                             "imported.  Access the pool on a system running "
2144                             "newer\n\tsoftware, or recreate the pool from "
2145                             "backup.\n"));
2146                         break;
2147                 case ZPOOL_STATUS_UNSUP_FEAT_READ:
2148                         (void) printf(gettext("action: The pool cannot be "
2149                             "imported. Access the pool on a system that "
2150                             "supports\n\tthe required feature(s), or recreate "
2151                             "the pool from backup.\n"));
2152                         break;
2153                 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
2154                         (void) printf(gettext("action: The pool cannot be "
2155                             "imported in read-write mode. Import the pool "
2156                             "with\n"
2157                             "\t\"-o readonly=on\", access the pool on a system "
2158                             "that supports the\n\trequired feature(s), or "
2159                             "recreate the pool from backup.\n"));
2160                         break;
2161                 case ZPOOL_STATUS_MISSING_DEV_R:
2162                 case ZPOOL_STATUS_MISSING_DEV_NR:
2163                 case ZPOOL_STATUS_BAD_GUID_SUM:
2164                         (void) printf(gettext(" action: The pool cannot be "
2165                             "imported. Attach the missing\n\tdevices and try "
2166                             "again.\n"));
2167                         break;
2168                 case ZPOOL_STATUS_HOSTID_ACTIVE:
2169                         VERIFY0(nvlist_lookup_nvlist(config,
2170                             ZPOOL_CONFIG_LOAD_INFO, &nvinfo));
2171
2172                         if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
2173                                 hostname = fnvlist_lookup_string(nvinfo,
2174                                     ZPOOL_CONFIG_MMP_HOSTNAME);
2175
2176                         if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID))
2177                                 hostid = fnvlist_lookup_uint64(nvinfo,
2178                                     ZPOOL_CONFIG_MMP_HOSTID);
2179
2180                         (void) printf(gettext(" action: The pool must be "
2181                             "exported from %s (hostid=%lx)\n\tbefore it "
2182                             "can be safely imported.\n"), hostname,
2183                             (unsigned long) hostid);
2184                         break;
2185                 case ZPOOL_STATUS_HOSTID_REQUIRED:
2186                         (void) printf(gettext(" action: Set a unique system "
2187                             "hostid with the zgenhostid(8) command.\n"));
2188                         break;
2189                 default:
2190                         (void) printf(gettext(" action: The pool cannot be "
2191                             "imported due to damaged devices or data.\n"));
2192                 }
2193         }
2194
2195         /* Print the comment attached to the pool. */
2196         if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2197                 (void) printf(gettext("comment: %s\n"), comment);
2198
2199         /*
2200          * If the state is "closed" or "can't open", and the aux state
2201          * is "corrupt data":
2202          */
2203         if (((vs->vs_state == VDEV_STATE_CLOSED) ||
2204             (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
2205             (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
2206                 if (pool_state == POOL_STATE_DESTROYED)
2207                         (void) printf(gettext("\tThe pool was destroyed, "
2208                             "but can be imported using the '-Df' flags.\n"));
2209                 else if (pool_state != POOL_STATE_EXPORTED)
2210                         (void) printf(gettext("\tThe pool may be active on "
2211                             "another system, but can be imported using\n\t"
2212                             "the '-f' flag.\n"));
2213         }
2214
2215         if (msgid != NULL)
2216                 (void) printf(gettext("   see: http://zfsonlinux.org/msg/%s\n"),
2217                     msgid);
2218
2219         (void) printf(gettext(" config:\n\n"));
2220
2221         cb.cb_namewidth = max_width(NULL, nvroot, 0, strlen(name),
2222             VDEV_NAME_TYPE_ID);
2223         if (cb.cb_namewidth < 10)
2224                 cb.cb_namewidth = 10;
2225
2226         print_import_config(&cb, name, nvroot, 0);
2227         if (num_logs(nvroot) > 0)
2228                 print_logs(NULL, &cb, nvroot);
2229
2230         if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
2231                 (void) printf(gettext("\n\tAdditional devices are known to "
2232                     "be part of this pool, though their\n\texact "
2233                     "configuration cannot be determined.\n"));
2234         }
2235 }
2236
2237 static boolean_t
2238 zfs_force_import_required(nvlist_t *config)
2239 {
2240         uint64_t state;
2241         uint64_t hostid = 0;
2242         nvlist_t *nvinfo;
2243
2244         state = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE);
2245         (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, &hostid);
2246
2247         if (state != POOL_STATE_EXPORTED && hostid != get_system_hostid())
2248                 return (B_TRUE);
2249
2250         nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
2251         if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE)) {
2252                 mmp_state_t mmp_state = fnvlist_lookup_uint64(nvinfo,
2253                     ZPOOL_CONFIG_MMP_STATE);
2254
2255                 if (mmp_state != MMP_STATE_INACTIVE)
2256                         return (B_TRUE);
2257         }
2258
2259         return (B_FALSE);
2260 }
2261
2262 /*
2263  * Perform the import for the given configuration.  This passes the heavy
2264  * lifting off to zpool_import_props(), and then mounts the datasets contained
2265  * within the pool.
2266  */
2267 static int
2268 do_import(nvlist_t *config, const char *newname, const char *mntopts,
2269     nvlist_t *props, int flags)
2270 {
2271         int ret = 0;
2272         zpool_handle_t *zhp;
2273         char *name;
2274         uint64_t version;
2275
2276         name = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME);
2277         version = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION);
2278
2279         if (!SPA_VERSION_IS_SUPPORTED(version)) {
2280                 (void) fprintf(stderr, gettext("cannot import '%s': pool "
2281                     "is formatted using an unsupported ZFS version\n"), name);
2282                 return (1);
2283         } else if (zfs_force_import_required(config) &&
2284             !(flags & ZFS_IMPORT_ANY_HOST)) {
2285                 mmp_state_t mmp_state = MMP_STATE_INACTIVE;
2286                 nvlist_t *nvinfo;
2287
2288                 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
2289                 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE))
2290                         mmp_state = fnvlist_lookup_uint64(nvinfo,
2291                             ZPOOL_CONFIG_MMP_STATE);
2292
2293                 if (mmp_state == MMP_STATE_ACTIVE) {
2294                         char *hostname = "<unknown>";
2295                         uint64_t hostid = 0;
2296
2297                         if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
2298                                 hostname = fnvlist_lookup_string(nvinfo,
2299                                     ZPOOL_CONFIG_MMP_HOSTNAME);
2300
2301                         if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID))
2302                                 hostid = fnvlist_lookup_uint64(nvinfo,
2303                                     ZPOOL_CONFIG_MMP_HOSTID);
2304
2305                         (void) fprintf(stderr, gettext("cannot import '%s': "
2306                             "pool is imported on %s (hostid: "
2307                             "0x%lx)\nExport the pool on the other system, "
2308                             "then run 'zpool import'.\n"),
2309                             name, hostname, (unsigned long) hostid);
2310                 } else if (mmp_state == MMP_STATE_NO_HOSTID) {
2311                         (void) fprintf(stderr, gettext("Cannot import '%s': "
2312                             "pool has the multihost property on and the\n"
2313                             "system's hostid is not set. Set a unique hostid "
2314                             "with the zgenhostid(8) command.\n"), name);
2315                 } else {
2316                         char *hostname = "<unknown>";
2317                         uint64_t timestamp = 0;
2318                         uint64_t hostid = 0;
2319
2320                         if (nvlist_exists(config, ZPOOL_CONFIG_HOSTNAME))
2321                                 hostname = fnvlist_lookup_string(config,
2322                                     ZPOOL_CONFIG_HOSTNAME);
2323
2324                         if (nvlist_exists(config, ZPOOL_CONFIG_TIMESTAMP))
2325                                 timestamp = fnvlist_lookup_uint64(config,
2326                                     ZPOOL_CONFIG_TIMESTAMP);
2327
2328                         if (nvlist_exists(config, ZPOOL_CONFIG_HOSTID))
2329                                 hostid = fnvlist_lookup_uint64(config,
2330                                     ZPOOL_CONFIG_HOSTID);
2331
2332                         (void) fprintf(stderr, gettext("cannot import '%s': "
2333                             "pool was previously in use from another system.\n"
2334                             "Last accessed by %s (hostid=%lx) at %s"
2335                             "The pool can be imported, use 'zpool import -f' "
2336                             "to import the pool.\n"), name, hostname,
2337                             (unsigned long)hostid, ctime((time_t *)&timestamp));
2338                 }
2339
2340                 return (1);
2341         }
2342
2343         if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
2344                 return (1);
2345
2346         if (newname != NULL)
2347                 name = (char *)newname;
2348
2349         if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
2350                 return (1);
2351
2352         /*
2353          * Loading keys is best effort. We don't want to return immediately
2354          * if it fails but we do want to give the error to the caller.
2355          */
2356         if (flags & ZFS_IMPORT_LOAD_KEYS) {
2357                 ret = zfs_crypto_attempt_load_keys(g_zfs, name);
2358                 if (ret != 0)
2359                         ret = 1;
2360         }
2361
2362         if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
2363             !(flags & ZFS_IMPORT_ONLY) &&
2364             zpool_enable_datasets(zhp, mntopts, 0) != 0) {
2365                 zpool_close(zhp);
2366                 return (1);
2367         }
2368
2369         zpool_close(zhp);
2370         return (ret);
2371 }
2372
2373 /*
2374  * zpool import [-d dir] [-D]
2375  *       import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
2376  *              [-d dir | -c cachefile] [-f] -a
2377  *       import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
2378  *              [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
2379  *
2380  *       -c     Read pool information from a cachefile instead of searching
2381  *              devices.
2382  *
2383  *       -d     Scan in a specific directory, other than /dev/.  More than
2384  *              one directory can be specified using multiple '-d' options.
2385  *
2386  *       -D     Scan for previously destroyed pools or import all or only
2387  *              specified destroyed pools.
2388  *
2389  *       -R     Temporarily import the pool, with all mountpoints relative to
2390  *              the given root.  The pool will remain exported when the machine
2391  *              is rebooted.
2392  *
2393  *       -V     Import even in the presence of faulted vdevs.  This is an
2394  *              intentionally undocumented option for testing purposes, and
2395  *              treats the pool configuration as complete, leaving any bad
2396  *              vdevs in the FAULTED state. In other words, it does verbatim
2397  *              import.
2398  *
2399  *       -f     Force import, even if it appears that the pool is active.
2400  *
2401  *       -F     Attempt rewind if necessary.
2402  *
2403  *       -n     See if rewind would work, but don't actually rewind.
2404  *
2405  *       -N     Import the pool but don't mount datasets.
2406  *
2407  *       -T     Specify a starting txg to use for import. This option is
2408  *              intentionally undocumented option for testing purposes.
2409  *
2410  *       -a     Import all pools found.
2411  *
2412  *       -l     Load encryption keys while importing.
2413  *
2414  *       -o     Set property=value and/or temporary mount options (without '=').
2415  *
2416  *       -s     Scan using the default search path, the libblkid cache will
2417  *              not be consulted.
2418  *
2419  * The import command scans for pools to import, and import pools based on pool
2420  * name and GUID.  The pool can also be renamed as part of the import process.
2421  */
2422 int
2423 zpool_do_import(int argc, char **argv)
2424 {
2425         char **searchdirs = NULL;
2426         char *env, *envdup = NULL;
2427         int nsearch = 0;
2428         int c;
2429         int err = 0;
2430         nvlist_t *pools = NULL;
2431         boolean_t do_all = B_FALSE;
2432         boolean_t do_destroyed = B_FALSE;
2433         char *mntopts = NULL;
2434         nvpair_t *elem;
2435         nvlist_t *config;
2436         uint64_t searchguid = 0;
2437         char *searchname = NULL;
2438         char *propval;
2439         nvlist_t *found_config;
2440         nvlist_t *policy = NULL;
2441         nvlist_t *props = NULL;
2442         boolean_t first;
2443         int flags = ZFS_IMPORT_NORMAL;
2444         uint32_t rewind_policy = ZPOOL_NO_REWIND;
2445         boolean_t dryrun = B_FALSE;
2446         boolean_t do_rewind = B_FALSE;
2447         boolean_t xtreme_rewind = B_FALSE;
2448         boolean_t do_scan = B_FALSE;
2449         uint64_t pool_state, txg = -1ULL;
2450         char *cachefile = NULL;
2451         importargs_t idata = { 0 };
2452         char *endptr;
2453
2454         /* check options */
2455         while ((c = getopt(argc, argv, ":aCc:d:DEfFlmnNo:R:stT:VX")) != -1) {
2456                 switch (c) {
2457                 case 'a':
2458                         do_all = B_TRUE;
2459                         break;
2460                 case 'c':
2461                         cachefile = optarg;
2462                         break;
2463                 case 'd':
2464                         if (searchdirs == NULL) {
2465                                 searchdirs = safe_malloc(sizeof (char *));
2466                         } else {
2467                                 char **tmp = safe_malloc((nsearch + 1) *
2468                                     sizeof (char *));
2469                                 bcopy(searchdirs, tmp, nsearch *
2470                                     sizeof (char *));
2471                                 free(searchdirs);
2472                                 searchdirs = tmp;
2473                         }
2474                         searchdirs[nsearch++] = optarg;
2475                         break;
2476                 case 'D':
2477                         do_destroyed = B_TRUE;
2478                         break;
2479                 case 'f':
2480                         flags |= ZFS_IMPORT_ANY_HOST;
2481                         break;
2482                 case 'F':
2483                         do_rewind = B_TRUE;
2484                         break;
2485                 case 'l':
2486                         flags |= ZFS_IMPORT_LOAD_KEYS;
2487                         break;
2488                 case 'm':
2489                         flags |= ZFS_IMPORT_MISSING_LOG;
2490                         break;
2491                 case 'n':
2492                         dryrun = B_TRUE;
2493                         break;
2494                 case 'N':
2495                         flags |= ZFS_IMPORT_ONLY;
2496                         break;
2497                 case 'o':
2498                         if ((propval = strchr(optarg, '=')) != NULL) {
2499                                 *propval = '\0';
2500                                 propval++;
2501                                 if (add_prop_list(optarg, propval,
2502                                     &props, B_TRUE))
2503                                         goto error;
2504                         } else {
2505                                 mntopts = optarg;
2506                         }
2507                         break;
2508                 case 'R':
2509                         if (add_prop_list(zpool_prop_to_name(
2510                             ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2511                                 goto error;
2512                         if (add_prop_list_default(zpool_prop_to_name(
2513                             ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2514                                 goto error;
2515                         break;
2516                 case 's':
2517                         do_scan = B_TRUE;
2518                         break;
2519                 case 't':
2520                         flags |= ZFS_IMPORT_TEMP_NAME;
2521                         if (add_prop_list_default(zpool_prop_to_name(
2522                             ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2523                                 goto error;
2524                         break;
2525
2526                 case 'T':
2527                         errno = 0;
2528                         txg = strtoull(optarg, &endptr, 0);
2529                         if (errno != 0 || *endptr != '\0') {
2530                                 (void) fprintf(stderr,
2531                                     gettext("invalid txg value\n"));
2532                                 usage(B_FALSE);
2533                         }
2534                         rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2535                         break;
2536                 case 'V':
2537                         flags |= ZFS_IMPORT_VERBATIM;
2538                         break;
2539                 case 'X':
2540                         xtreme_rewind = B_TRUE;
2541                         break;
2542                 case ':':
2543                         (void) fprintf(stderr, gettext("missing argument for "
2544                             "'%c' option\n"), optopt);
2545                         usage(B_FALSE);
2546                         break;
2547                 case '?':
2548                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2549                             optopt);
2550                         usage(B_FALSE);
2551                 }
2552         }
2553
2554         argc -= optind;
2555         argv += optind;
2556
2557         if (cachefile && nsearch != 0) {
2558                 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2559                 usage(B_FALSE);
2560         }
2561
2562         if ((flags & ZFS_IMPORT_LOAD_KEYS) && (flags & ZFS_IMPORT_ONLY)) {
2563                 (void) fprintf(stderr, gettext("-l is incompatible with -N\n"));
2564                 usage(B_FALSE);
2565         }
2566
2567         if ((flags & ZFS_IMPORT_LOAD_KEYS) && !do_all && argc == 0) {
2568                 (void) fprintf(stderr, gettext("-l is only meaningful during "
2569                     "an import\n"));
2570                 usage(B_FALSE);
2571         }
2572
2573         if ((dryrun || xtreme_rewind) && !do_rewind) {
2574                 (void) fprintf(stderr,
2575                     gettext("-n or -X only meaningful with -F\n"));
2576                 usage(B_FALSE);
2577         }
2578         if (dryrun)
2579                 rewind_policy = ZPOOL_TRY_REWIND;
2580         else if (do_rewind)
2581                 rewind_policy = ZPOOL_DO_REWIND;
2582         if (xtreme_rewind)
2583                 rewind_policy |= ZPOOL_EXTREME_REWIND;
2584
2585         /* In the future, we can capture further policy and include it here */
2586         if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2587             nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2588             nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2589                 goto error;
2590
2591         /* check argument count */
2592         if (do_all) {
2593                 if (argc != 0) {
2594                         (void) fprintf(stderr, gettext("too many arguments\n"));
2595                         usage(B_FALSE);
2596                 }
2597         } else {
2598                 if (argc > 2) {
2599                         (void) fprintf(stderr, gettext("too many arguments\n"));
2600                         usage(B_FALSE);
2601                 }
2602         }
2603
2604         /*
2605          * Check for the effective uid.  We do this explicitly here because
2606          * otherwise any attempt to discover pools will silently fail.
2607          */
2608         if (argc == 0 && geteuid() != 0) {
2609                 (void) fprintf(stderr, gettext("cannot "
2610                     "discover pools: permission denied\n"));
2611                 if (searchdirs != NULL)
2612                         free(searchdirs);
2613
2614                 nvlist_free(props);
2615                 nvlist_free(policy);
2616                 return (1);
2617         }
2618
2619         /*
2620          * Depending on the arguments given, we do one of the following:
2621          *
2622          *      <none>  Iterate through all pools and display information about
2623          *              each one.
2624          *
2625          *      -a      Iterate through all pools and try to import each one.
2626          *
2627          *      <id>    Find the pool that corresponds to the given GUID/pool
2628          *              name and import that one.
2629          *
2630          *      -D      Above options applies only to destroyed pools.
2631          */
2632         if (argc != 0) {
2633                 char *endptr;
2634
2635                 errno = 0;
2636                 searchguid = strtoull(argv[0], &endptr, 10);
2637                 if (errno != 0 || *endptr != '\0') {
2638                         searchname = argv[0];
2639                         searchguid = 0;
2640                 }
2641                 found_config = NULL;
2642
2643                 /*
2644                  * User specified a name or guid.  Ensure it's unique.
2645                  */
2646                 idata.unique = B_TRUE;
2647         }
2648
2649         /*
2650          * Check the environment for the preferred search path.
2651          */
2652         if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) {
2653                 char *dir;
2654
2655                 envdup = strdup(env);
2656
2657                 dir = strtok(envdup, ":");
2658                 while (dir != NULL) {
2659                         if (searchdirs == NULL) {
2660                                 searchdirs = safe_malloc(sizeof (char *));
2661                         } else {
2662                                 char **tmp = safe_malloc((nsearch + 1) *
2663                                     sizeof (char *));
2664                                 bcopy(searchdirs, tmp, nsearch *
2665                                     sizeof (char *));
2666                                 free(searchdirs);
2667                                 searchdirs = tmp;
2668                         }
2669                         searchdirs[nsearch++] = dir;
2670                         dir = strtok(NULL, ":");
2671                 }
2672         }
2673
2674         idata.path = searchdirs;
2675         idata.paths = nsearch;
2676         idata.poolname = searchname;
2677         idata.guid = searchguid;
2678         idata.cachefile = cachefile;
2679         idata.scan = do_scan;
2680
2681         pools = zpool_search_import(g_zfs, &idata);
2682
2683         if (pools != NULL && idata.exists &&
2684             (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2685                 (void) fprintf(stderr, gettext("cannot import '%s': "
2686                     "a pool with that name already exists\n"),
2687                     argv[0]);
2688                 (void) fprintf(stderr, gettext("use the form '%s "
2689                     "<pool | id> <newpool>' to give it a new name\n"),
2690                     "zpool import");
2691                 err = 1;
2692         } else if (pools == NULL && idata.exists) {
2693                 (void) fprintf(stderr, gettext("cannot import '%s': "
2694                     "a pool with that name is already created/imported,\n"),
2695                     argv[0]);
2696                 (void) fprintf(stderr, gettext("and no additional pools "
2697                     "with that name were found\n"));
2698                 err = 1;
2699         } else if (pools == NULL) {
2700                 if (argc != 0) {
2701                         (void) fprintf(stderr, gettext("cannot import '%s': "
2702                             "no such pool available\n"), argv[0]);
2703                 }
2704                 err = 1;
2705         }
2706
2707         if (err == 1) {
2708                 if (searchdirs != NULL)
2709                         free(searchdirs);
2710                 if (envdup != NULL)
2711                         free(envdup);
2712                 nvlist_free(policy);
2713                 nvlist_free(pools);
2714                 nvlist_free(props);
2715                 return (1);
2716         }
2717
2718         /*
2719          * At this point we have a list of import candidate configs. Even if
2720          * we were searching by pool name or guid, we still need to
2721          * post-process the list to deal with pool state and possible
2722          * duplicate names.
2723          */
2724         err = 0;
2725         elem = NULL;
2726         first = B_TRUE;
2727         while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2728
2729                 verify(nvpair_value_nvlist(elem, &config) == 0);
2730
2731                 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2732                     &pool_state) == 0);
2733                 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2734                         continue;
2735                 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2736                         continue;
2737
2738                 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2739                     policy) == 0);
2740
2741                 if (argc == 0) {
2742                         if (first)
2743                                 first = B_FALSE;
2744                         else if (!do_all)
2745                                 (void) printf("\n");
2746
2747                         if (do_all) {
2748                                 err |= do_import(config, NULL, mntopts,
2749                                     props, flags);
2750                         } else {
2751                                 show_import(config);
2752                         }
2753                 } else if (searchname != NULL) {
2754                         char *name;
2755
2756                         /*
2757                          * We are searching for a pool based on name.
2758                          */
2759                         verify(nvlist_lookup_string(config,
2760                             ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2761
2762                         if (strcmp(name, searchname) == 0) {
2763                                 if (found_config != NULL) {
2764                                         (void) fprintf(stderr, gettext(
2765                                             "cannot import '%s': more than "
2766                                             "one matching pool\n"), searchname);
2767                                         (void) fprintf(stderr, gettext(
2768                                             "import by numeric ID instead\n"));
2769                                         err = B_TRUE;
2770                                 }
2771                                 found_config = config;
2772                         }
2773                 } else {
2774                         uint64_t guid;
2775
2776                         /*
2777                          * Search for a pool by guid.
2778                          */
2779                         verify(nvlist_lookup_uint64(config,
2780                             ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2781
2782                         if (guid == searchguid)
2783                                 found_config = config;
2784                 }
2785         }
2786
2787         /*
2788          * If we were searching for a specific pool, verify that we found a
2789          * pool, and then do the import.
2790          */
2791         if (argc != 0 && err == 0) {
2792                 if (found_config == NULL) {
2793                         (void) fprintf(stderr, gettext("cannot import '%s': "
2794                             "no such pool available\n"), argv[0]);
2795                         err = B_TRUE;
2796                 } else {
2797                         err |= do_import(found_config, argc == 1 ? NULL :
2798                             argv[1], mntopts, props, flags);
2799                 }
2800         }
2801
2802         /*
2803          * If we were just looking for pools, report an error if none were
2804          * found.
2805          */
2806         if (argc == 0 && first)
2807                 (void) fprintf(stderr,
2808                     gettext("no pools available to import\n"));
2809
2810 error:
2811         nvlist_free(props);
2812         nvlist_free(pools);
2813         nvlist_free(policy);
2814         if (searchdirs != NULL)
2815                 free(searchdirs);
2816         if (envdup != NULL)
2817                 free(envdup);
2818
2819         return (err ? 1 : 0);
2820 }
2821
2822 /*
2823  * zpool sync [-f] [pool] ...
2824  *
2825  * -f (undocumented) force uberblock (and config including zpool cache file)
2826  *    update.
2827  *
2828  * Sync the specified pool(s).
2829  * Without arguments "zpool sync" will sync all pools.
2830  * This command initiates TXG sync(s) and will return after the TXG(s) commit.
2831  *
2832  */
2833 static int
2834 zpool_do_sync(int argc, char **argv)
2835 {
2836         int ret;
2837         boolean_t force = B_FALSE;
2838
2839         /* check options */
2840         while ((ret  = getopt(argc, argv, "f")) != -1) {
2841                 switch (ret) {
2842                 case 'f':
2843                         force = B_TRUE;
2844                         break;
2845                 case '?':
2846                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2847                             optopt);
2848                         usage(B_FALSE);
2849                 }
2850         }
2851
2852         argc -= optind;
2853         argv += optind;
2854
2855         /* if argc == 0 we will execute zpool_sync_one on all pools */
2856         ret = for_each_pool(argc, argv, B_FALSE, NULL, zpool_sync_one, &force);
2857
2858         return (ret);
2859 }
2860
2861 typedef struct iostat_cbdata {
2862         uint64_t cb_flags;
2863         int cb_name_flags;
2864         int cb_namewidth;
2865         int cb_iteration;
2866         char **cb_vdev_names; /* Only show these vdevs */
2867         unsigned int cb_vdev_names_count;
2868         boolean_t cb_verbose;
2869         boolean_t cb_literal;
2870         boolean_t cb_scripted;
2871         zpool_list_t *cb_list;
2872         vdev_cmd_data_list_t *vcdl;
2873 } iostat_cbdata_t;
2874
2875 /*  iostat labels */
2876 typedef struct name_and_columns {
2877         const char *name;       /* Column name */
2878         unsigned int columns;   /* Center name to this number of columns */
2879 } name_and_columns_t;
2880
2881 #define IOSTAT_MAX_LABELS       11      /* Max number of labels on one line */
2882
2883 static const name_and_columns_t iostat_top_labels[][IOSTAT_MAX_LABELS] =
2884 {
2885         [IOS_DEFAULT] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
2886             {NULL}},
2887         [IOS_LATENCY] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
2888             {"asyncq_wait", 2}, {"scrub"}},
2889         [IOS_QUEUES] = {{"syncq_read", 2}, {"syncq_write", 2},
2890             {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
2891             {NULL}},
2892         [IOS_L_HISTO] = {{"total_wait", 2}, {"disk_wait", 2},
2893             {"sync_queue", 2}, {"async_queue", 2}, {NULL}},
2894         [IOS_RQ_HISTO] = {{"sync_read", 2}, {"sync_write", 2},
2895             {"async_read", 2}, {"async_write", 2}, {"scrub", 2}, {NULL}},
2896
2897 };
2898
2899 /* Shorthand - if "columns" field not set, default to 1 column */
2900 static const name_and_columns_t iostat_bottom_labels[][IOSTAT_MAX_LABELS] =
2901 {
2902         [IOS_DEFAULT] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
2903             {"write"}, {NULL}},
2904         [IOS_LATENCY] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
2905             {"write"}, {"read"}, {"write"}, {"wait"}, {NULL}},
2906         [IOS_QUEUES] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
2907             {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL}},
2908         [IOS_L_HISTO] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
2909             {"write"}, {"read"}, {"write"}, {"scrub"}, {NULL}},
2910         [IOS_RQ_HISTO] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
2911             {"ind"}, {"agg"}, {"ind"}, {"agg"}, {NULL}},
2912 };
2913
2914 static const char *histo_to_title[] = {
2915         [IOS_L_HISTO] = "latency",
2916         [IOS_RQ_HISTO] = "req_size",
2917 };
2918
2919 /*
2920  * Return the number of labels in a null-terminated name_and_columns_t
2921  * array.
2922  *
2923  */
2924 static unsigned int
2925 label_array_len(const name_and_columns_t *labels)
2926 {
2927         int i = 0;
2928
2929         while (labels[i].name)
2930                 i++;
2931
2932         return (i);
2933 }
2934
2935 /*
2936  * Return the number of strings in a null-terminated string array.
2937  * For example:
2938  *
2939  *     const char foo[] = {"bar", "baz", NULL}
2940  *
2941  * returns 2
2942  */
2943 static uint64_t
2944 str_array_len(const char *array[])
2945 {
2946         uint64_t i = 0;
2947         while (array[i])
2948                 i++;
2949
2950         return (i);
2951 }
2952
2953
2954 /*
2955  * Return a default column width for default/latency/queue columns. This does
2956  * not include histograms, which have their columns autosized.
2957  */
2958 static unsigned int
2959 default_column_width(iostat_cbdata_t *cb, enum iostat_type type)
2960 {
2961         unsigned long column_width = 5; /* Normal niceprint */
2962         static unsigned long widths[] = {
2963                 /*
2964                  * Choose some sane default column sizes for printing the
2965                  * raw numbers.
2966                  */
2967                 [IOS_DEFAULT] = 15, /* 1PB capacity */
2968                 [IOS_LATENCY] = 10, /* 1B ns = 10sec */
2969                 [IOS_QUEUES] = 6,   /* 1M queue entries */
2970         };
2971
2972         if (cb->cb_literal)
2973                 column_width = widths[type];
2974
2975         return (column_width);
2976 }
2977
2978 /*
2979  * Print the column labels, i.e:
2980  *
2981  *   capacity     operations     bandwidth
2982  * alloc   free   read  write   read  write  ...
2983  *
2984  * If force_column_width is set, use it for the column width.  If not set, use
2985  * the default column width.
2986  */
2987 void
2988 print_iostat_labels(iostat_cbdata_t *cb, unsigned int force_column_width,
2989     const name_and_columns_t labels[][IOSTAT_MAX_LABELS])
2990 {
2991         int i, idx, s;
2992         unsigned int text_start, rw_column_width, spaces_to_end;
2993         uint64_t flags = cb->cb_flags;
2994         uint64_t f;
2995         unsigned int column_width = force_column_width;
2996
2997         /* For each bit set in flags */
2998         for (f = flags; f; f &= ~(1ULL << idx)) {
2999                 idx = lowbit64(f) - 1;
3000                 if (!force_column_width)
3001                         column_width = default_column_width(cb, idx);
3002                 /* Print our top labels centered over "read  write" label. */
3003                 for (i = 0; i < label_array_len(labels[idx]); i++) {
3004                         const char *name = labels[idx][i].name;
3005                         /*
3006                          * We treat labels[][].columns == 0 as shorthand
3007                          * for one column.  It makes writing out the label
3008                          * tables more concise.
3009                          */
3010                         unsigned int columns = MAX(1, labels[idx][i].columns);
3011                         unsigned int slen = strlen(name);
3012
3013                         rw_column_width = (column_width * columns) +
3014                             (2 * (columns - 1));
3015
3016                         text_start = (int)((rw_column_width)/columns -
3017                             slen/columns);
3018
3019                         printf("  ");   /* Two spaces between columns */
3020
3021                         /* Space from beginning of column to label */
3022                         for (s = 0; s < text_start; s++)
3023                                 printf(" ");
3024
3025                         printf("%s", name);
3026
3027                         /* Print space after label to end of column */
3028                         spaces_to_end = rw_column_width - text_start - slen;
3029                         for (s = 0; s < spaces_to_end; s++)
3030                                 printf(" ");
3031
3032                 }
3033         }
3034 }
3035
3036
3037 /*
3038  * print_cmd_columns - Print custom column titles from -c
3039  *
3040  * If the user specified the "zpool status|iostat -c" then print their custom
3041  * column titles in the header.  For example, print_cmd_columns() would print
3042  * the "  col1  col2" part of this:
3043  *
3044  * $ zpool iostat -vc 'echo col1=val1; echo col2=val2'
3045  * ...
3046  *            capacity     operations     bandwidth
3047  * pool        alloc   free   read  write   read  write  col1  col2
3048  * ----------  -----  -----  -----  -----  -----  -----  ----  ----
3049  * mypool       269K  1008M      0      0    107    946
3050  *   mirror     269K  1008M      0      0    107    946
3051  *     sdb         -      -      0      0    102    473  val1  val2
3052  *     sdc         -      -      0      0      5    473  val1  val2
3053  * ----------  -----  -----  -----  -----  -----  -----  ----  ----
3054  */
3055 void
3056 print_cmd_columns(vdev_cmd_data_list_t *vcdl, int use_dashes)
3057 {
3058         int i, j;
3059         vdev_cmd_data_t *data = &vcdl->data[0];
3060
3061         if (vcdl->count == 0 || data == NULL)
3062                 return;
3063
3064         /*
3065          * Each vdev cmd should have the same column names unless the user did
3066          * something weird with their cmd.  Just take the column names from the
3067          * first vdev and assume it works for all of them.
3068          */
3069         for (i = 0; i < vcdl->uniq_cols_cnt; i++) {
3070                 printf("  ");
3071                 if (use_dashes) {
3072                         for (j = 0; j < vcdl->uniq_cols_width[i]; j++)
3073                                 printf("-");
3074                 } else {
3075                         printf("%*s", vcdl->uniq_cols_width[i],
3076                             vcdl->uniq_cols[i]);
3077                 }
3078         }
3079 }
3080
3081
3082 /*
3083  * Utility function to print out a line of dashes like:
3084  *
3085  *      --------------------------------  -----  -----  -----  -----  -----
3086  *
3087  * ...or a dashed named-row line like:
3088  *
3089  *      logs                                  -      -      -      -      -
3090  *
3091  * @cb:                         iostat data
3092  *
3093  * @force_column_width          If non-zero, use the value as the column width.
3094  *                              Otherwise use the default column widths.
3095  *
3096  * @name:                       Print a dashed named-row line starting
3097  *                              with @name.  Otherwise, print a regular
3098  *                              dashed line.
3099  */
3100 static void
3101 print_iostat_dashes(iostat_cbdata_t *cb, unsigned int force_column_width,
3102     const char *name)
3103 {
3104         int i;
3105         unsigned int namewidth;
3106         uint64_t flags = cb->cb_flags;
3107         uint64_t f;
3108         int idx;
3109         const name_and_columns_t *labels;
3110         const char *title;
3111
3112
3113         if (cb->cb_flags & IOS_ANYHISTO_M) {
3114                 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
3115         } else if (cb->cb_vdev_names_count) {
3116                 title = "vdev";
3117         } else  {
3118                 title = "pool";
3119         }
3120
3121         namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
3122             name ? strlen(name) : 0);
3123
3124
3125         if (name) {
3126                 printf("%-*s", namewidth, name);
3127         } else {
3128                 for (i = 0; i < namewidth; i++)
3129                         (void) printf("-");
3130         }
3131
3132         /* For each bit in flags */
3133         for (f = flags; f; f &= ~(1ULL << idx)) {
3134                 unsigned int column_width;
3135                 idx = lowbit64(f) - 1;
3136                 if (force_column_width)
3137                         column_width = force_column_width;
3138                 else
3139                         column_width = default_column_width(cb, idx);
3140
3141                 labels = iostat_bottom_labels[idx];
3142                 for (i = 0; i < label_array_len(labels); i++) {
3143                         if (name)
3144                                 printf("  %*s-", column_width - 1, " ");
3145                         else
3146                                 printf("  %.*s", column_width,
3147                                     "--------------------");
3148                 }
3149         }
3150 }
3151
3152
3153 static void
3154 print_iostat_separator_impl(iostat_cbdata_t *cb,
3155     unsigned int force_column_width)
3156 {
3157         print_iostat_dashes(cb, force_column_width, NULL);
3158 }
3159
3160 static void
3161 print_iostat_separator(iostat_cbdata_t *cb)
3162 {
3163         print_iostat_separator_impl(cb, 0);
3164 }
3165
3166 static void
3167 print_iostat_header_impl(iostat_cbdata_t *cb, unsigned int force_column_width,
3168     const char *histo_vdev_name)
3169 {
3170         unsigned int namewidth;
3171         const char *title;
3172
3173         if (cb->cb_flags & IOS_ANYHISTO_M) {
3174                 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
3175         } else if (cb->cb_vdev_names_count) {
3176                 title = "vdev";
3177         } else  {
3178                 title = "pool";
3179         }
3180
3181         namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
3182             histo_vdev_name ? strlen(histo_vdev_name) : 0);
3183
3184         if (histo_vdev_name)
3185                 printf("%-*s", namewidth, histo_vdev_name);
3186         else
3187                 printf("%*s", namewidth, "");
3188
3189
3190         print_iostat_labels(cb, force_column_width, iostat_top_labels);
3191         printf("\n");
3192
3193         printf("%-*s", namewidth, title);
3194
3195         print_iostat_labels(cb, force_column_width, iostat_bottom_labels);
3196         if (cb->vcdl != NULL)
3197                 print_cmd_columns(cb->vcdl, 0);
3198
3199         printf("\n");
3200
3201         print_iostat_separator_impl(cb, force_column_width);
3202
3203         if (cb->vcdl != NULL)
3204                 print_cmd_columns(cb->vcdl, 1);
3205
3206         printf("\n");
3207 }
3208
3209 static void
3210 print_iostat_header(iostat_cbdata_t *cb)
3211 {
3212         print_iostat_header_impl(cb, 0, NULL);
3213 }
3214
3215
3216 /*
3217  * Display a single statistic.
3218  */
3219 static void
3220 print_one_stat(uint64_t value, enum zfs_nicenum_format format,
3221     unsigned int column_size, boolean_t scripted)
3222 {
3223         char buf[64];
3224
3225         zfs_nicenum_format(value, buf, sizeof (buf), format);
3226
3227         if (scripted)
3228                 printf("\t%s", buf);
3229         else
3230                 printf("  %*s", column_size, buf);
3231 }
3232
3233 /*
3234  * Calculate the default vdev stats
3235  *
3236  * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
3237  * stats into calcvs.
3238  */
3239 static void
3240 calc_default_iostats(vdev_stat_t *oldvs, vdev_stat_t *newvs,
3241     vdev_stat_t *calcvs)
3242 {
3243         int i;
3244
3245         memcpy(calcvs, newvs, sizeof (*calcvs));
3246         for (i = 0; i < ARRAY_SIZE(calcvs->vs_ops); i++)
3247                 calcvs->vs_ops[i] = (newvs->vs_ops[i] - oldvs->vs_ops[i]);
3248
3249         for (i = 0; i < ARRAY_SIZE(calcvs->vs_bytes); i++)
3250                 calcvs->vs_bytes[i] = (newvs->vs_bytes[i] - oldvs->vs_bytes[i]);
3251 }
3252
3253 /*
3254  * Internal representation of the extended iostats data.
3255  *
3256  * The extended iostat stats are exported in nvlists as either uint64_t arrays
3257  * or single uint64_t's.  We make both look like arrays to make them easier
3258  * to process.  In order to make single uint64_t's look like arrays, we set
3259  * __data to the stat data, and then set *data = &__data with count = 1.  Then,
3260  * we can just use *data and count.
3261  */
3262 struct stat_array {
3263         uint64_t *data;
3264         uint_t count;   /* Number of entries in data[] */
3265         uint64_t __data; /* Only used when data is a single uint64_t */
3266 };
3267
3268 static uint64_t
3269 stat_histo_max(struct stat_array *nva, unsigned int len)
3270 {
3271         uint64_t max = 0;
3272         int i;
3273         for (i = 0; i < len; i++)
3274                 max = MAX(max, array64_max(nva[i].data, nva[i].count));
3275
3276         return (max);
3277 }
3278
3279 /*
3280  * Helper function to lookup a uint64_t array or uint64_t value and store its
3281  * data as a stat_array.  If the nvpair is a single uint64_t value, then we make
3282  * it look like a one element array to make it easier to process.
3283  */
3284 static int
3285 nvpair64_to_stat_array(nvlist_t *nvl, const char *name,
3286     struct stat_array *nva)
3287 {
3288         nvpair_t *tmp;
3289         int ret;
3290
3291         verify(nvlist_lookup_nvpair(nvl, name, &tmp) == 0);
3292         switch (nvpair_type(tmp)) {
3293         case DATA_TYPE_UINT64_ARRAY:
3294                 ret = nvpair_value_uint64_array(tmp, &nva->data, &nva->count);
3295                 break;
3296         case DATA_TYPE_UINT64:
3297                 ret = nvpair_value_uint64(tmp, &nva->__data);
3298                 nva->data = &nva->__data;
3299                 nva->count = 1;
3300                 break;
3301         default:
3302                 /* Not a uint64_t */
3303                 ret = EINVAL;
3304                 break;
3305         }
3306
3307         return (ret);
3308 }
3309
3310 /*
3311  * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
3312  * subtract them, and return the results in a newly allocated stat_array.
3313  * You must free the returned array after you are done with it with
3314  * free_calc_stats().
3315  *
3316  * Additionally, you can set "oldnv" to NULL if you simply want the newnv
3317  * values.
3318  */
3319 static struct stat_array *
3320 calc_and_alloc_stats_ex(const char **names, unsigned int len, nvlist_t *oldnv,
3321     nvlist_t *newnv)
3322 {
3323         nvlist_t *oldnvx = NULL, *newnvx;
3324         struct stat_array *oldnva, *newnva, *calcnva;
3325         int i, j;
3326         unsigned int alloc_size = (sizeof (struct stat_array)) * len;
3327
3328         /* Extract our extended stats nvlist from the main list */
3329         verify(nvlist_lookup_nvlist(newnv, ZPOOL_CONFIG_VDEV_STATS_EX,
3330             &newnvx) == 0);
3331         if (oldnv) {
3332                 verify(nvlist_lookup_nvlist(oldnv, ZPOOL_CONFIG_VDEV_STATS_EX,
3333                     &oldnvx) == 0);
3334         }
3335
3336         newnva = safe_malloc(alloc_size);
3337         oldnva = safe_malloc(alloc_size);
3338         calcnva = safe_malloc(alloc_size);
3339
3340         for (j = 0; j < len; j++) {
3341                 verify(nvpair64_to_stat_array(newnvx, names[j],
3342                     &newnva[j]) == 0);
3343                 calcnva[j].count = newnva[j].count;
3344                 alloc_size = calcnva[j].count * sizeof (calcnva[j].data[0]);
3345                 calcnva[j].data = safe_malloc(alloc_size);
3346                 memcpy(calcnva[j].data, newnva[j].data, alloc_size);
3347
3348                 if (oldnvx) {
3349                         verify(nvpair64_to_stat_array(oldnvx, names[j],
3350                             &oldnva[j]) == 0);
3351                         for (i = 0; i < oldnva[j].count; i++)
3352                                 calcnva[j].data[i] -= oldnva[j].data[i];
3353                 }
3354         }
3355         free(newnva);
3356         free(oldnva);
3357         return (calcnva);
3358 }
3359
3360 static void
3361 free_calc_stats(struct stat_array *nva, unsigned int len)
3362 {
3363         int i;
3364         for (i = 0; i < len; i++)
3365                 free(nva[i].data);
3366
3367         free(nva);
3368 }
3369
3370 static void
3371 print_iostat_histo(struct stat_array *nva, unsigned int len,
3372     iostat_cbdata_t *cb, unsigned int column_width, unsigned int namewidth,
3373     double scale)
3374 {
3375         int i, j;
3376         char buf[6];
3377         uint64_t val;
3378         enum zfs_nicenum_format format;
3379         unsigned int buckets;
3380         unsigned int start_bucket;
3381
3382         if (cb->cb_literal)
3383                 format = ZFS_NICENUM_RAW;
3384         else
3385                 format = ZFS_NICENUM_1024;
3386
3387         /* All these histos are the same size, so just use nva[0].count */
3388         buckets = nva[0].count;
3389
3390         if (cb->cb_flags & IOS_RQ_HISTO_M) {
3391                 /* Start at 512 - req size should never be lower than this */
3392                 start_bucket = 9;
3393         } else {
3394                 start_bucket = 0;
3395         }
3396
3397         for (j = start_bucket; j < buckets; j++) {
3398                 /* Print histogram bucket label */
3399                 if (cb->cb_flags & IOS_L_HISTO_M) {
3400                         /* Ending range of this bucket */
3401                         val = (1UL << (j + 1)) - 1;
3402                         zfs_nicetime(val, buf, sizeof (buf));
3403                 } else {
3404                         /* Request size (starting range of bucket) */
3405                         val = (1UL << j);
3406                         zfs_nicenum(val, buf, sizeof (buf));
3407                 }
3408
3409                 if (cb->cb_scripted)
3410                         printf("%llu", (u_longlong_t)val);
3411                 else
3412                         printf("%-*s", namewidth, buf);
3413
3414                 /* Print the values on the line */
3415                 for (i = 0; i < len; i++) {
3416                         print_one_stat(nva[i].data[j] * scale, format,
3417                             column_width, cb->cb_scripted);
3418                 }
3419                 printf("\n");
3420         }
3421 }
3422
3423 static void
3424 print_solid_separator(unsigned int length)
3425 {
3426         while (length--)
3427                 printf("-");
3428         printf("\n");
3429 }
3430
3431 static void
3432 print_iostat_histos(iostat_cbdata_t *cb, nvlist_t *oldnv,
3433     nvlist_t *newnv, double scale, const char *name)
3434 {
3435         unsigned int column_width;
3436         unsigned int namewidth;
3437         unsigned int entire_width;
3438         enum iostat_type type;
3439         struct stat_array *nva;
3440         const char **names;
3441         unsigned int names_len;
3442
3443         /* What type of histo are we? */
3444         type = IOS_HISTO_IDX(cb->cb_flags);
3445
3446         /* Get NULL-terminated array of nvlist names for our histo */
3447         names = vsx_type_to_nvlist[type];
3448         names_len = str_array_len(names); /* num of names */
3449
3450         nva = calc_and_alloc_stats_ex(names, names_len, oldnv, newnv);
3451
3452         if (cb->cb_literal) {
3453                 column_width = MAX(5,
3454                     (unsigned int) log10(stat_histo_max(nva, names_len)) + 1);
3455         } else {
3456                 column_width = 5;
3457         }
3458
3459         namewidth = MAX(cb->cb_namewidth,
3460             strlen(histo_to_title[IOS_HISTO_IDX(cb->cb_flags)]));
3461
3462         /*
3463          * Calculate the entire line width of what we're printing.  The
3464          * +2 is for the two spaces between columns:
3465          */
3466         /*       read  write                            */
3467         /*      -----  -----                            */
3468         /*      |___|  <---------- column_width         */
3469         /*                                              */
3470         /*      |__________|  <--- entire_width         */
3471         /*                                              */
3472         entire_width = namewidth + (column_width + 2) *
3473             label_array_len(iostat_bottom_labels[type]);
3474
3475         if (cb->cb_scripted)
3476                 printf("%s\n", name);
3477         else
3478                 print_iostat_header_impl(cb, column_width, name);
3479
3480         print_iostat_histo(nva, names_len, cb, column_width,
3481             namewidth, scale);
3482
3483         free_calc_stats(nva, names_len);
3484         if (!cb->cb_scripted)
3485                 print_solid_separator(entire_width);
3486 }
3487
3488 /*
3489  * Calculate the average latency of a power-of-two latency histogram
3490  */
3491 static uint64_t
3492 single_histo_average(uint64_t *histo, unsigned int buckets)
3493 {
3494         int i;
3495         uint64_t count = 0, total = 0;
3496
3497         for (i = 0; i < buckets; i++) {
3498                 /*
3499                  * Our buckets are power-of-two latency ranges.  Use the
3500                  * midpoint latency of each bucket to calculate the average.
3501                  * For example:
3502                  *
3503                  * Bucket          Midpoint
3504                  * 8ns-15ns:       12ns
3505                  * 16ns-31ns:      24ns
3506                  * ...
3507                  */
3508                 if (histo[i] != 0) {
3509                         total += histo[i] * (((1UL << i) + ((1UL << i)/2)));
3510                         count += histo[i];
3511                 }
3512         }
3513
3514         /* Prevent divide by zero */
3515         return (count == 0 ? 0 : total / count);
3516 }
3517
3518 static void
3519 print_iostat_queues(iostat_cbdata_t *cb, nvlist_t *oldnv,
3520     nvlist_t *newnv, double scale)
3521 {
3522         int i;
3523         uint64_t val;
3524         const char *names[] = {
3525                 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE,
3526                 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
3527                 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE,
3528                 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
3529                 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE,
3530                 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
3531                 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE,
3532                 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
3533                 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE,
3534                 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
3535         };
3536
3537         struct stat_array *nva;
3538
3539         unsigned int column_width = default_column_width(cb, IOS_QUEUES);
3540         enum zfs_nicenum_format format;
3541
3542         nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), NULL, newnv);
3543
3544         if (cb->cb_literal)
3545                 format = ZFS_NICENUM_RAW;
3546         else
3547                 format = ZFS_NICENUM_1024;
3548
3549         for (i = 0; i < ARRAY_SIZE(names); i++) {
3550                 val = nva[i].data[0] * scale;
3551                 print_one_stat(val, format, column_width, cb->cb_scripted);
3552         }
3553
3554         free_calc_stats(nva, ARRAY_SIZE(names));
3555 }
3556
3557 static void
3558 print_iostat_latency(iostat_cbdata_t *cb, nvlist_t *oldnv,
3559     nvlist_t *newnv, double scale)
3560 {
3561         int i;
3562         uint64_t val;
3563         const char *names[] = {
3564                 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
3565                 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
3566                 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
3567                 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
3568                 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
3569                 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
3570                 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
3571                 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
3572                 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
3573         };
3574         struct stat_array *nva;
3575
3576         unsigned int column_width = default_column_width(cb, IOS_LATENCY);
3577         enum zfs_nicenum_format format;
3578
3579         nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), oldnv, newnv);
3580
3581         if (cb->cb_literal)
3582                 format = ZFS_NICENUM_RAWTIME;
3583         else
3584                 format = ZFS_NICENUM_TIME;
3585
3586         /* Print our avg latencies on the line */
3587         for (i = 0; i < ARRAY_SIZE(names); i++) {
3588                 /* Compute average latency for a latency histo */
3589                 val = single_histo_average(nva[i].data, nva[i].count) * scale;
3590                 print_one_stat(val, format, column_width, cb->cb_scripted);
3591         }
3592         free_calc_stats(nva, ARRAY_SIZE(names));
3593 }
3594
3595 /*
3596  * Print default statistics (capacity/operations/bandwidth)
3597  */
3598 static void
3599 print_iostat_default(vdev_stat_t *vs, iostat_cbdata_t *cb, double scale)
3600 {
3601         unsigned int column_width = default_column_width(cb, IOS_DEFAULT);
3602         enum zfs_nicenum_format format;
3603         char na;        /* char to print for "not applicable" values */
3604
3605         if (cb->cb_literal) {
3606                 format = ZFS_NICENUM_RAW;
3607                 na = '0';
3608         } else {
3609                 format = ZFS_NICENUM_1024;
3610                 na = '-';
3611         }
3612
3613         /* only toplevel vdevs have capacity stats */
3614         if (vs->vs_space == 0) {
3615                 if (cb->cb_scripted)
3616                         printf("\t%c\t%c", na, na);
3617                 else
3618                         printf("  %*c  %*c", column_width, na, column_width,
3619                             na);
3620         } else {
3621                 print_one_stat(vs->vs_alloc, format, column_width,
3622                     cb->cb_scripted);
3623                 print_one_stat(vs->vs_space - vs->vs_alloc, format,
3624                     column_width, cb->cb_scripted);
3625         }
3626
3627         print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_READ] * scale),
3628             format, column_width, cb->cb_scripted);
3629         print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_WRITE] * scale),
3630             format, column_width, cb->cb_scripted);
3631         print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_READ] * scale),
3632             format, column_width, cb->cb_scripted);
3633         print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_WRITE] * scale),
3634             format, column_width, cb->cb_scripted);
3635 }
3636
3637 /*
3638  * Print out all the statistics for the given vdev.  This can either be the
3639  * toplevel configuration, or called recursively.  If 'name' is NULL, then this
3640  * is a verbose output, and we don't want to display the toplevel pool stats.
3641  *
3642  * Returns the number of stat lines printed.
3643  */
3644 unsigned int
3645 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
3646     nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
3647 {
3648         nvlist_t **oldchild, **newchild;
3649         uint_t c, children, oldchildren;
3650         vdev_stat_t *oldvs, *newvs, *calcvs;
3651         vdev_stat_t zerovs = { 0 };
3652         char *vname;
3653         int i;
3654         int ret = 0;
3655         uint64_t tdelta;
3656         double scale;
3657
3658         calcvs = safe_malloc(sizeof (*calcvs));
3659
3660         if (oldnv != NULL) {
3661                 verify(nvlist_lookup_uint64_array(oldnv,
3662                     ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
3663         } else {
3664                 oldvs = &zerovs;
3665         }
3666
3667         /* Do we only want to see a specific vdev? */
3668         for (i = 0; i < cb->cb_vdev_names_count; i++) {
3669                 /* Yes we do.  Is this the vdev? */
3670                 if (strcmp(name, cb->cb_vdev_names[i]) == 0) {
3671                         /*
3672                          * This is our vdev.  Since it is the only vdev we
3673                          * will be displaying, make depth = 0 so that it
3674                          * doesn't get indented.
3675                          */
3676                         depth = 0;
3677                         break;
3678                 }
3679         }
3680
3681         if (cb->cb_vdev_names_count && (i == cb->cb_vdev_names_count)) {
3682                 /* Couldn't match the name */
3683                 goto children;
3684         }
3685
3686
3687         verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
3688             (uint64_t **)&newvs, &c) == 0);
3689
3690         /*
3691          * Print the vdev name unless it's is a histogram.  Histograms
3692          * display the vdev name in the header itself.
3693          */
3694         if (!(cb->cb_flags & IOS_ANYHISTO_M)) {
3695                 if (cb->cb_scripted) {
3696                         printf("%s", name);
3697                 } else {
3698                         if (strlen(name) + depth > cb->cb_namewidth)
3699                                 (void) printf("%*s%s", depth, "", name);
3700                         else
3701                                 (void) printf("%*s%s%*s", depth, "", name,
3702                                     (int)(cb->cb_namewidth - strlen(name) -
3703                                     depth), "");
3704                 }
3705         }
3706
3707         /* Calculate our scaling factor */
3708         tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
3709         if ((oldvs->vs_timestamp == 0) && (cb->cb_flags & IOS_ANYHISTO_M)) {
3710                 /*
3711                  * If we specify printing histograms with no time interval, then
3712                  * print the histogram numbers over the entire lifetime of the
3713                  * vdev.
3714                  */
3715                 scale = 1;
3716         } else {
3717                 if (tdelta == 0)
3718                         scale = 1.0;
3719                 else
3720                         scale = (double)NANOSEC / tdelta;
3721         }
3722
3723         if (cb->cb_flags & IOS_DEFAULT_M) {
3724                 calc_default_iostats(oldvs, newvs, calcvs);
3725                 print_iostat_default(calcvs, cb, scale);
3726         }
3727         if (cb->cb_flags & IOS_LATENCY_M)
3728                 print_iostat_latency(cb, oldnv, newnv, scale);
3729         if (cb->cb_flags & IOS_QUEUES_M)
3730                 print_iostat_queues(cb, oldnv, newnv, scale);
3731         if (cb->cb_flags & IOS_ANYHISTO_M) {
3732                 printf("\n");
3733                 print_iostat_histos(cb, oldnv, newnv, scale, name);
3734         }
3735
3736         if (cb->vcdl != NULL) {
3737                 char *path;
3738                 if (nvlist_lookup_string(newnv, ZPOOL_CONFIG_PATH,
3739                     &path) == 0) {
3740                         printf("  ");
3741                         zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
3742                 }
3743         }
3744
3745         if (!(cb->cb_flags & IOS_ANYHISTO_M))
3746                 printf("\n");
3747
3748         ret++;
3749
3750 children:
3751
3752         free(calcvs);
3753
3754         if (!cb->cb_verbose)
3755                 return (ret);
3756
3757         if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
3758             &newchild, &children) != 0)
3759                 return (ret);
3760
3761         if (oldnv) {
3762                 if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
3763                     &oldchild, &oldchildren) != 0)
3764                         return (ret);
3765
3766                 children = MIN(oldchildren, children);
3767         }
3768
3769         for (c = 0; c < children; c++) {
3770                 uint64_t ishole = B_FALSE, islog = B_FALSE;
3771
3772                 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
3773                     &ishole);
3774
3775                 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
3776                     &islog);
3777
3778                 if (ishole || islog)
3779                         continue;
3780
3781                 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
3782                     cb->cb_name_flags);
3783                 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
3784                     newchild[c], cb, depth + 2);
3785                 free(vname);
3786         }
3787
3788         /*
3789          * Log device section
3790          */
3791
3792         if (num_logs(newnv) > 0) {
3793                 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted &&
3794                     !cb->cb_vdev_names) {
3795                         print_iostat_dashes(cb, 0, "logs");
3796                 }
3797                 printf("\n");
3798
3799                 for (c = 0; c < children; c++) {
3800                         uint64_t islog = B_FALSE;
3801                         (void) nvlist_lookup_uint64(newchild[c],
3802                             ZPOOL_CONFIG_IS_LOG, &islog);
3803
3804                         if (islog) {
3805                                 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
3806                                     cb->cb_name_flags);
3807                                 ret += print_vdev_stats(zhp, vname, oldnv ?
3808                                     oldchild[c] : NULL, newchild[c],
3809                                     cb, depth + 2);
3810                                 free(vname);
3811                         }
3812                 }
3813
3814         }
3815
3816         /*
3817          * Include level 2 ARC devices in iostat output
3818          */
3819         if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
3820             &newchild, &children) != 0)
3821                 return (ret);
3822
3823         if (oldnv) {
3824                 if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
3825                     &oldchild, &oldchildren) != 0)
3826                         return (ret);
3827
3828                 children = MIN(oldchildren, children);
3829         }
3830
3831         if (children > 0) {
3832                 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted &&
3833                     !cb->cb_vdev_names) {
3834                         print_iostat_dashes(cb, 0, "cache");
3835                 }
3836                 printf("\n");
3837
3838                 for (c = 0; c < children; c++) {
3839                         vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
3840                             cb->cb_name_flags);
3841                         ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c]
3842                             : NULL, newchild[c], cb, depth + 2);
3843                         free(vname);
3844                 }
3845         }
3846
3847         return (ret);
3848 }
3849
3850 static int
3851 refresh_iostat(zpool_handle_t *zhp, void *data)
3852 {
3853         iostat_cbdata_t *cb = data;
3854         boolean_t missing;
3855
3856         /*
3857          * If the pool has disappeared, remove it from the list and continue.
3858          */
3859         if (zpool_refresh_stats(zhp, &missing) != 0)
3860                 return (-1);
3861
3862         if (missing)
3863                 pool_list_remove(cb->cb_list, zhp);
3864
3865         return (0);
3866 }
3867
3868 /*
3869  * Callback to print out the iostats for the given pool.
3870  */
3871 int
3872 print_iostat(zpool_handle_t *zhp, void *data)
3873 {
3874         iostat_cbdata_t *cb = data;
3875         nvlist_t *oldconfig, *newconfig;
3876         nvlist_t *oldnvroot, *newnvroot;
3877         int ret;
3878
3879         newconfig = zpool_get_config(zhp, &oldconfig);
3880
3881         if (cb->cb_iteration == 1)
3882                 oldconfig = NULL;
3883
3884         verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
3885             &newnvroot) == 0);
3886
3887         if (oldconfig == NULL)
3888                 oldnvroot = NULL;
3889         else
3890                 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
3891                     &oldnvroot) == 0);
3892
3893         ret = print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot,
3894             cb, 0);
3895         if ((ret != 0) && !(cb->cb_flags & IOS_ANYHISTO_M) &&
3896             !cb->cb_scripted && cb->cb_verbose && !cb->cb_vdev_names_count) {
3897                 print_iostat_separator(cb);
3898                 if (cb->vcdl != NULL) {
3899                         print_cmd_columns(cb->vcdl, 1);
3900                 }
3901                 printf("\n");
3902         }
3903
3904         return (ret);
3905 }
3906
3907 static int
3908 get_columns(void)
3909 {
3910         struct winsize ws;
3911         int columns = 80;
3912         int error;
3913
3914         if (isatty(STDOUT_FILENO)) {
3915                 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
3916                 if (error == 0)
3917                         columns = ws.ws_col;
3918         } else {
3919                 columns = 999;
3920         }
3921
3922         return (columns);
3923 }
3924
3925 int
3926 get_namewidth(zpool_handle_t *zhp, void *data)
3927 {
3928         iostat_cbdata_t *cb = data;
3929         nvlist_t *config, *nvroot;
3930         int columns;
3931
3932         if ((config = zpool_get_config(zhp, NULL)) != NULL) {
3933                 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3934                     &nvroot) == 0);
3935                 unsigned int poolname_len = strlen(zpool_get_name(zhp));
3936                 if (!cb->cb_verbose)
3937                         cb->cb_namewidth = MAX(poolname_len, cb->cb_namewidth);
3938                 else
3939                         cb->cb_namewidth = MAX(poolname_len,
3940                             max_width(zhp, nvroot, 0, cb->cb_namewidth,
3941                             cb->cb_name_flags));
3942         }
3943         /*
3944          * The width must be at least 10, but may be as large as the
3945          * column width - 42 so that we can still fit in one line.
3946          */
3947         columns = get_columns();
3948
3949         if (cb->cb_namewidth < 10)
3950                 cb->cb_namewidth = 10;
3951         if (cb->cb_namewidth > columns - 42)
3952                 cb->cb_namewidth = columns - 42;
3953
3954         return (0);
3955 }
3956
3957 /*
3958  * Parse the input string, get the 'interval' and 'count' value if there is one.
3959  */
3960 static void
3961 get_interval_count(int *argcp, char **argv, float *iv,
3962     unsigned long *cnt)
3963 {
3964         float interval = 0;
3965         unsigned long count = 0;
3966         int argc = *argcp;
3967
3968         /*
3969          * Determine if the last argument is an integer or a pool name
3970          */
3971         if (argc > 0 && isnumber(argv[argc - 1])) {
3972                 char *end;
3973
3974                 errno = 0;
3975                 interval = strtof(argv[argc - 1], &end);
3976
3977                 if (*end == '\0' && errno == 0) {
3978                         if (interval == 0) {
3979                                 (void) fprintf(stderr, gettext("interval "
3980                                     "cannot be zero\n"));
3981                                 usage(B_FALSE);
3982                         }
3983                         /*
3984                          * Ignore the last parameter
3985                          */
3986                         argc--;
3987                 } else {
3988                         /*
3989                          * If this is not a valid number, just plow on.  The
3990                          * user will get a more informative error message later
3991                          * on.
3992                          */
3993                         interval = 0;
3994                 }
3995         }
3996
3997         /*
3998          * If the last argument is also an integer, then we have both a count
3999          * and an interval.
4000          */
4001         if (argc > 0 && isnumber(argv[argc - 1])) {
4002                 char *end;
4003
4004                 errno = 0;
4005                 count = interval;
4006                 interval = strtof(argv[argc - 1], &end);
4007
4008                 if (*end == '\0' && errno == 0) {
4009                         if (interval == 0) {
4010                                 (void) fprintf(stderr, gettext("interval "
4011                                     "cannot be zero\n"));
4012                                 usage(B_FALSE);
4013                         }
4014
4015                         /*
4016                          * Ignore the last parameter
4017                          */
4018                         argc--;
4019                 } else {
4020                         interval = 0;
4021                 }
4022         }
4023
4024         *iv = interval;
4025         *cnt = count;
4026         *argcp = argc;
4027 }
4028
4029 static void
4030 get_timestamp_arg(char c)
4031 {
4032         if (c == 'u')
4033                 timestamp_fmt = UDATE;
4034         else if (c == 'd')
4035                 timestamp_fmt = DDATE;
4036         else
4037                 usage(B_FALSE);
4038 }
4039
4040 /*
4041  * Return stat flags that are supported by all pools by both the module and
4042  * zpool iostat.  "*data" should be initialized to all 0xFFs before running.
4043  * It will get ANDed down until only the flags that are supported on all pools
4044  * remain.
4045  */
4046 static int
4047 get_stat_flags_cb(zpool_handle_t *zhp, void *data)
4048 {
4049         uint64_t *mask = data;
4050         nvlist_t *config, *nvroot, *nvx;
4051         uint64_t flags = 0;
4052         int i, j;
4053
4054         config = zpool_get_config(zhp, NULL);
4055         verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4056             &nvroot) == 0);
4057
4058         /* Default stats are always supported, but for completeness.. */
4059         if (nvlist_exists(nvroot, ZPOOL_CONFIG_VDEV_STATS))
4060                 flags |= IOS_DEFAULT_M;
4061
4062         /* Get our extended stats nvlist from the main list */
4063         if (nvlist_lookup_nvlist(nvroot, ZPOOL_CONFIG_VDEV_STATS_EX,
4064             &nvx) != 0) {
4065                 /*
4066                  * No extended stats; they're probably running an older
4067                  * module.  No big deal, we support that too.
4068                  */
4069                 goto end;
4070         }
4071
4072         /* For each extended stat, make sure all its nvpairs are supported */
4073         for (j = 0; j < ARRAY_SIZE(vsx_type_to_nvlist); j++) {
4074                 if (!vsx_type_to_nvlist[j][0])
4075                         continue;
4076
4077                 /* Start off by assuming the flag is supported, then check */
4078                 flags |= (1ULL << j);
4079                 for (i = 0; vsx_type_to_nvlist[j][i]; i++) {
4080                         if (!nvlist_exists(nvx, vsx_type_to_nvlist[j][i])) {
4081                                 /* flag isn't supported */
4082                                 flags = flags & ~(1ULL  << j);
4083                                 break;
4084                         }
4085                 }
4086         }
4087 end:
4088         *mask = *mask & flags;
4089         return (0);
4090 }
4091
4092 /*
4093  * Return a bitmask of stats that are supported on all pools by both the module
4094  * and zpool iostat.
4095  */
4096 static uint64_t
4097 get_stat_flags(zpool_list_t *list)
4098 {
4099         uint64_t mask = -1;
4100
4101         /*
4102          * get_stat_flags_cb() will lop off bits from "mask" until only the
4103          * flags that are supported on all pools remain.
4104          */
4105         pool_list_iter(list, B_FALSE, get_stat_flags_cb, &mask);
4106         return (mask);
4107 }
4108
4109 /*
4110  * Return 1 if cb_data->cb_vdev_names[0] is this vdev's name, 0 otherwise.
4111  */
4112 static int
4113 is_vdev_cb(zpool_handle_t *zhp, nvlist_t *nv, void *cb_data)
4114 {
4115         iostat_cbdata_t *cb = cb_data;
4116         char *name = NULL;
4117         int ret = 0;
4118
4119         name = zpool_vdev_name(g_zfs, zhp, nv, cb->cb_name_flags);
4120
4121         if (strcmp(name, cb->cb_vdev_names[0]) == 0)
4122                 ret = 1; /* match */
4123         free(name);
4124
4125         return (ret);
4126 }
4127
4128 /*
4129  * Returns 1 if cb_data->cb_vdev_names[0] is a vdev name, 0 otherwise.
4130  */
4131 static int
4132 is_vdev(zpool_handle_t *zhp, void *cb_data)
4133 {
4134         return (for_each_vdev(zhp, is_vdev_cb, cb_data));
4135 }
4136
4137 /*
4138  * Check if vdevs are in a pool
4139  *
4140  * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
4141  * return 0.  If pool_name is NULL, then search all pools.
4142  */
4143 static int
4144 are_vdevs_in_pool(int argc, char **argv, char *pool_name,
4145     iostat_cbdata_t *cb)
4146 {
4147         char **tmp_name;
4148         int ret = 0;
4149         int i;
4150         int pool_count = 0;
4151
4152         if ((argc == 0) || !*argv)
4153                 return (0);
4154
4155         if (pool_name)
4156                 pool_count = 1;
4157
4158         /* Temporarily hijack cb_vdev_names for a second... */
4159         tmp_name = cb->cb_vdev_names;
4160
4161         /* Go though our list of prospective vdev names */
4162         for (i = 0; i < argc; i++) {
4163                 cb->cb_vdev_names = argv + i;
4164
4165                 /* Is this name a vdev in our pools? */
4166                 ret = for_each_pool(pool_count, &pool_name, B_TRUE, NULL,
4167                     is_vdev, cb);
4168                 if (!ret) {
4169                         /* No match */
4170                         break;
4171                 }
4172         }
4173
4174         cb->cb_vdev_names = tmp_name;
4175
4176         return (ret);
4177 }
4178
4179 static int
4180 is_pool_cb(zpool_handle_t *zhp, void *data)
4181 {
4182         char *name = data;
4183         if (strcmp(name, zpool_get_name(zhp)) == 0)
4184                 return (1);
4185
4186         return (0);
4187 }
4188
4189 /*
4190  * Do we have a pool named *name?  If so, return 1, otherwise 0.
4191  */
4192 static int
4193 is_pool(char *name)
4194 {
4195         return (for_each_pool(0, NULL, B_TRUE, NULL,  is_pool_cb, name));
4196 }
4197
4198 /* Are all our argv[] strings pool names?  If so return 1, 0 otherwise. */
4199 static int
4200 are_all_pools(int argc, char **argv)
4201 {
4202         if ((argc == 0) || !*argv)
4203                 return (0);
4204
4205         while (--argc >= 0)
4206                 if (!is_pool(argv[argc]))
4207                         return (0);
4208
4209         return (1);
4210 }
4211
4212 /*
4213  * Helper function to print out vdev/pool names we can't resolve.  Used for an
4214  * error message.
4215  */
4216 static void
4217 error_list_unresolved_vdevs(int argc, char **argv, char *pool_name,
4218     iostat_cbdata_t *cb)
4219 {
4220         int i;
4221         char *name;
4222         char *str;
4223         for (i = 0; i < argc; i++) {
4224                 name = argv[i];
4225
4226                 if (is_pool(name))
4227                         str = gettext("pool");
4228                 else if (are_vdevs_in_pool(1, &name, pool_name, cb))
4229                         str = gettext("vdev in this pool");
4230                 else if (are_vdevs_in_pool(1, &name, NULL, cb))
4231                         str = gettext("vdev in another pool");
4232                 else
4233                         str = gettext("unknown");
4234
4235                 fprintf(stderr, "\t%s (%s)\n", name, str);
4236         }
4237 }
4238
4239 /*
4240  * Same as get_interval_count(), but with additional checks to not misinterpret
4241  * guids as interval/count values.  Assumes VDEV_NAME_GUID is set in
4242  * cb.cb_name_flags.
4243  */
4244 static void
4245 get_interval_count_filter_guids(int *argc, char **argv, float *interval,
4246     unsigned long *count, iostat_cbdata_t *cb)
4247 {
4248         char **tmpargv = argv;
4249         int argc_for_interval = 0;
4250
4251         /* Is the last arg an interval value?  Or a guid? */
4252         if (*argc >= 1 && !are_vdevs_in_pool(1, &argv[*argc - 1], NULL, cb)) {
4253                 /*
4254                  * The last arg is not a guid, so it's probably an
4255                  * interval value.
4256                  */
4257                 argc_for_interval++;
4258
4259                 if (*argc >= 2 &&
4260                     !are_vdevs_in_pool(1, &argv[*argc - 2], NULL, cb)) {
4261                         /*
4262                          * The 2nd to last arg is not a guid, so it's probably
4263                          * an interval value.
4264                          */
4265                         argc_for_interval++;
4266                 }
4267         }
4268
4269         /* Point to our list of possible intervals */
4270         tmpargv = &argv[*argc - argc_for_interval];
4271
4272         *argc = *argc - argc_for_interval;
4273         get_interval_count(&argc_for_interval, tmpargv,
4274             interval, count);
4275 }
4276
4277 /*
4278  * Floating point sleep().  Allows you to pass in a floating point value for
4279  * seconds.
4280  */
4281 static void
4282 fsleep(float sec)
4283 {
4284         struct timespec req;
4285         req.tv_sec = floor(sec);
4286         req.tv_nsec = (sec - (float)req.tv_sec) * NANOSEC;
4287         nanosleep(&req, NULL);
4288 }
4289
4290 /*
4291  * Run one of the zpool status/iostat -c scripts with the help (-h) option and
4292  * print the result.
4293  *
4294  * name:        Short name of the script ('iostat').
4295  * path:        Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
4296  */
4297 static void
4298 print_zpool_script_help(char *name, char *path)
4299 {
4300         char *argv[] = {path, "-h", NULL};
4301         char **lines = NULL;
4302         int lines_cnt = 0;
4303         int rc;
4304
4305         rc = libzfs_run_process_get_stdout_nopath(path, argv, NULL, &lines,
4306             &lines_cnt);
4307         if (rc != 0 || lines == NULL || lines_cnt <= 0) {
4308                 if (lines != NULL)
4309                         libzfs_free_str_array(lines, lines_cnt);
4310                 return;
4311         }
4312
4313         for (int i = 0; i < lines_cnt; i++)
4314                 if (!is_blank_str(lines[i]))
4315                         printf("  %-14s  %s\n", name, lines[i]);
4316
4317         libzfs_free_str_array(lines, lines_cnt);
4318 }
4319
4320 /*
4321  * Go though the zpool status/iostat -c scripts in the user's path, run their
4322  * help option (-h), and print out the results.
4323  */
4324 static void
4325 print_zpool_dir_scripts(char *dirpath)
4326 {
4327         DIR *dir;
4328         struct dirent *ent;
4329         char fullpath[MAXPATHLEN];
4330         struct stat dir_stat;
4331
4332         if ((dir = opendir(dirpath)) != NULL) {
4333                 /* print all the files and directories within directory */
4334                 while ((ent = readdir(dir)) != NULL) {
4335                         sprintf(fullpath, "%s/%s", dirpath, ent->d_name);
4336
4337                         /* Print the scripts */
4338                         if (stat(fullpath, &dir_stat) == 0)
4339                                 if (dir_stat.st_mode & S_IXUSR &&
4340                                     S_ISREG(dir_stat.st_mode))
4341                                         print_zpool_script_help(ent->d_name,
4342                                             fullpath);
4343                 }
4344                 closedir(dir);
4345         }
4346 }
4347
4348 /*
4349  * Print out help text for all zpool status/iostat -c scripts.
4350  */
4351 static void
4352 print_zpool_script_list(char *subcommand)
4353 {
4354         char *dir, *sp;
4355
4356         printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand);
4357
4358         sp = zpool_get_cmd_search_path();
4359         if (sp == NULL)
4360                 return;
4361
4362         dir = strtok(sp, ":");
4363         while (dir != NULL) {
4364                 print_zpool_dir_scripts(dir);
4365                 dir = strtok(NULL, ":");
4366         }
4367
4368         free(sp);
4369 }
4370
4371 /*
4372  * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
4373  *              [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
4374  *              [interval [count]]
4375  *
4376  *      -c CMD  For each vdev, run command CMD
4377  *      -g      Display guid for individual vdev name.
4378  *      -L      Follow links when resolving vdev path name.
4379  *      -P      Display full path for vdev name.
4380  *      -v      Display statistics for individual vdevs
4381  *      -h      Display help
4382  *      -p      Display values in parsable (exact) format.
4383  *      -H      Scripted mode.  Don't display headers, and separate properties
4384  *              by a single tab.
4385  *      -l      Display average latency
4386  *      -q      Display queue depths
4387  *      -w      Display latency histograms
4388  *      -r      Display request size histogram
4389  *      -T      Display a timestamp in date(1) or Unix format
4390  *
4391  * This command can be tricky because we want to be able to deal with pool
4392  * creation/destruction as well as vdev configuration changes.  The bulk of this
4393  * processing is handled by the pool_list_* routines in zpool_iter.c.  We rely
4394  * on pool_list_update() to detect the addition of new pools.  Configuration
4395  * changes are all handled within libzfs.
4396  */
4397 int
4398 zpool_do_iostat(int argc, char **argv)
4399 {
4400         int c;
4401         int ret;
4402         int npools;
4403         float interval = 0;
4404         unsigned long count = 0;
4405         zpool_list_t *list;
4406         boolean_t verbose = B_FALSE;
4407         boolean_t latency = B_FALSE, l_histo = B_FALSE, rq_histo = B_FALSE;
4408         boolean_t queues = B_FALSE, parsable = B_FALSE, scripted = B_FALSE;
4409         boolean_t omit_since_boot = B_FALSE;
4410         boolean_t guid = B_FALSE;
4411         boolean_t follow_links = B_FALSE;
4412         boolean_t full_name = B_FALSE;
4413         iostat_cbdata_t cb = { 0 };
4414         char *cmd = NULL;
4415
4416         /* Used for printing error message */
4417         const char flag_to_arg[] = {[IOS_LATENCY] = 'l', [IOS_QUEUES] = 'q',
4418             [IOS_L_HISTO] = 'w', [IOS_RQ_HISTO] = 'r'};
4419
4420         uint64_t unsupported_flags;
4421
4422         /* check options */
4423         while ((c = getopt(argc, argv, "c:gLPT:vyhplqrwH")) != -1) {
4424                 switch (c) {
4425                 case 'c':
4426                         if (cmd != NULL) {
4427                                 fprintf(stderr,
4428                                     gettext("Can't set -c flag twice\n"));
4429                                 exit(1);
4430                         }
4431
4432                         if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL &&
4433                             !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
4434                                 fprintf(stderr, gettext(
4435                                     "Can't run -c, disabled by "
4436                                     "ZPOOL_SCRIPTS_ENABLED.\n"));
4437                                 exit(1);
4438                         }
4439
4440                         if ((getuid() <= 0 || geteuid() <= 0) &&
4441                             !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
4442                                 fprintf(stderr, gettext(
4443                                     "Can't run -c with root privileges "
4444                                     "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
4445                                 exit(1);
4446                         }
4447                         cmd = optarg;
4448                         verbose = B_TRUE;
4449                         break;
4450                 case 'g':
4451                         guid = B_TRUE;
4452                         break;
4453                 case 'L':
4454                         follow_links = B_TRUE;
4455                         break;
4456                 case 'P':
4457                         full_name = B_TRUE;
4458                         break;
4459                 case 'T':
4460                         get_timestamp_arg(*optarg);
4461                         break;
4462                 case 'v':
4463                         verbose = B_TRUE;
4464                         break;
4465                 case 'p':
4466                         parsable = B_TRUE;
4467                         break;
4468                 case 'l':
4469                         latency = B_TRUE;
4470                         break;
4471                 case 'q':
4472                         queues = B_TRUE;
4473                         break;
4474                 case 'H':
4475                         scripted = B_TRUE;
4476                         break;
4477                 case 'w':
4478                         l_histo = B_TRUE;
4479                         break;
4480                 case 'r':
4481                         rq_histo = B_TRUE;
4482                         break;
4483                 case 'y':
4484                         omit_since_boot = B_TRUE;
4485                         break;
4486                 case 'h':
4487                         usage(B_FALSE);
4488                         break;
4489                 case '?':
4490                         if (optopt == 'c') {
4491                                 print_zpool_script_list("iostat");
4492                                 exit(0);
4493                         } else {
4494                                 fprintf(stderr,
4495                                     gettext("invalid option '%c'\n"), optopt);
4496                         }
4497                         usage(B_FALSE);
4498                 }
4499         }
4500
4501         argc -= optind;
4502         argv += optind;
4503
4504         cb.cb_literal = parsable;
4505         cb.cb_scripted = scripted;
4506
4507         if (guid)
4508                 cb.cb_name_flags |= VDEV_NAME_GUID;
4509         if (follow_links)
4510                 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
4511         if (full_name)
4512                 cb.cb_name_flags |= VDEV_NAME_PATH;
4513         cb.cb_iteration = 0;
4514         cb.cb_namewidth = 0;
4515         cb.cb_verbose = verbose;
4516
4517         /* Get our interval and count values (if any) */
4518         if (guid) {
4519                 get_interval_count_filter_guids(&argc, argv, &interval,
4520                     &count, &cb);
4521         } else {
4522                 get_interval_count(&argc, argv, &interval, &count);
4523         }
4524
4525         if (argc == 0) {
4526                 /* No args, so just print the defaults. */
4527         } else if (are_all_pools(argc, argv)) {
4528                 /* All the args are pool names */
4529         } else if (are_vdevs_in_pool(argc, argv, NULL, &cb)) {
4530                 /* All the args are vdevs */
4531                 cb.cb_vdev_names = argv;
4532                 cb.cb_vdev_names_count = argc;
4533                 argc = 0; /* No pools to process */
4534         } else if (are_all_pools(1, argv)) {
4535                 /* The first arg is a pool name */
4536                 if (are_vdevs_in_pool(argc - 1, argv + 1, argv[0], &cb)) {
4537                         /* ...and the rest are vdev names */
4538                         cb.cb_vdev_names = argv + 1;
4539                         cb.cb_vdev_names_count = argc - 1;
4540                         argc = 1; /* One pool to process */
4541                 } else {
4542                         fprintf(stderr, gettext("Expected either a list of "));
4543                         fprintf(stderr, gettext("pools, or list of vdevs in"));
4544                         fprintf(stderr, " \"%s\", ", argv[0]);
4545                         fprintf(stderr, gettext("but got:\n"));
4546                         error_list_unresolved_vdevs(argc - 1, argv + 1,
4547                             argv[0], &cb);
4548                         fprintf(stderr, "\n");
4549                         usage(B_FALSE);
4550                         return (1);
4551                 }
4552         } else {
4553                 /*
4554                  * The args don't make sense. The first arg isn't a pool name,
4555                  * nor are all the args vdevs.
4556                  */
4557                 fprintf(stderr, gettext("Unable to parse pools/vdevs list.\n"));
4558                 fprintf(stderr, "\n");
4559                 return (1);
4560         }
4561
4562         if (cb.cb_vdev_names_count != 0) {
4563                 /*
4564                  * If user specified vdevs, it implies verbose.
4565                  */
4566                 cb.cb_verbose = B_TRUE;
4567         }
4568
4569         /*
4570          * Construct the list of all interesting pools.
4571          */
4572         ret = 0;
4573         if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
4574                 return (1);
4575
4576         if (pool_list_count(list) == 0 && argc != 0) {
4577                 pool_list_free(list);
4578                 return (1);
4579         }
4580
4581         if (pool_list_count(list) == 0 && interval == 0) {
4582                 pool_list_free(list);
4583                 (void) fprintf(stderr, gettext("no pools available\n"));
4584                 return (1);
4585         }
4586
4587         if ((l_histo || rq_histo) && (cmd != NULL || latency || queues)) {
4588                 pool_list_free(list);
4589                 (void) fprintf(stderr,
4590                     gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
4591                 usage(B_FALSE);
4592                 return (1);
4593         }
4594
4595         if (l_histo && rq_histo) {
4596                 pool_list_free(list);
4597                 (void) fprintf(stderr,
4598                     gettext("Only one of [-r|-w] can be passed at a time\n"));
4599                 usage(B_FALSE);
4600                 return (1);
4601         }
4602
4603         /*
4604          * Enter the main iostat loop.
4605          */
4606         cb.cb_list = list;
4607
4608         if (l_histo) {
4609                 /*
4610                  * Histograms tables look out of place when you try to display
4611                  * them with the other stats, so make a rule that you can only
4612                  * print histograms by themselves.
4613                  */
4614                 cb.cb_flags = IOS_L_HISTO_M;
4615         } else if (rq_histo) {
4616                 cb.cb_flags = IOS_RQ_HISTO_M;
4617         } else {
4618                 cb.cb_flags = IOS_DEFAULT_M;
4619                 if (latency)
4620                         cb.cb_flags |= IOS_LATENCY_M;
4621                 if (queues)
4622                         cb.cb_flags |= IOS_QUEUES_M;
4623         }
4624
4625         /*
4626          * See if the module supports all the stats we want to display.
4627          */
4628         unsupported_flags = cb.cb_flags & ~get_stat_flags(list);
4629         if (unsupported_flags) {
4630                 uint64_t f;
4631                 int idx;
4632                 fprintf(stderr,
4633                     gettext("The loaded zfs module doesn't support:"));
4634
4635                 /* for each bit set in unsupported_flags */
4636                 for (f = unsupported_flags; f; f &= ~(1ULL << idx)) {
4637                         idx = lowbit64(f) - 1;
4638                         fprintf(stderr, " -%c", flag_to_arg[idx]);
4639                 }
4640
4641                 fprintf(stderr, ".  Try running a newer module.\n");
4642                 pool_list_free(list);
4643
4644                 return (1);
4645         }
4646
4647         for (;;) {
4648                 if ((npools = pool_list_count(list)) == 0)
4649                         (void) fprintf(stderr, gettext("no pools available\n"));
4650                 else {
4651                         /*
4652                          * If this is the first iteration and -y was supplied
4653                          * we skip any printing.
4654                          */
4655                         boolean_t skip = (omit_since_boot &&
4656                             cb.cb_iteration == 0);
4657
4658                         /*
4659                          * Refresh all statistics.  This is done as an
4660                          * explicit step before calculating the maximum name
4661                          * width, so that any * configuration changes are
4662                          * properly accounted for.
4663                          */
4664                         (void) pool_list_iter(list, B_FALSE, refresh_iostat,
4665                             &cb);
4666
4667                         /*
4668                          * Iterate over all pools to determine the maximum width
4669                          * for the pool / device name column across all pools.
4670                          */
4671                         cb.cb_namewidth = 0;
4672                         (void) pool_list_iter(list, B_FALSE, get_namewidth,
4673                             &cb);
4674
4675                         if (timestamp_fmt != NODATE)
4676                                 print_timestamp(timestamp_fmt);
4677
4678                         if (cmd != NULL && cb.cb_verbose &&
4679                             !(cb.cb_flags & IOS_ANYHISTO_M)) {
4680                                 cb.vcdl = all_pools_for_each_vdev_run(argc,
4681                                     argv, cmd, g_zfs, cb.cb_vdev_names,
4682                                     cb.cb_vdev_names_count, cb.cb_name_flags);
4683                         } else {
4684                                 cb.vcdl = NULL;
4685                         }
4686
4687                         /*
4688                          * If it's the first time and we're not skipping it,
4689                          * or either skip or verbose mode, print the header.
4690                          *
4691                          * The histogram code explicitly prints its header on
4692                          * every vdev, so skip this for histograms.
4693                          */
4694                         if (((++cb.cb_iteration == 1 && !skip) ||
4695                             (skip != verbose)) &&
4696                             (!(cb.cb_flags & IOS_ANYHISTO_M)) &&
4697                             !cb.cb_scripted)
4698                                 print_iostat_header(&cb);
4699
4700                         if (skip) {
4701                                 (void) fsleep(interval);
4702                                 continue;
4703                         }
4704
4705
4706                         pool_list_iter(list, B_FALSE, print_iostat, &cb);
4707
4708                         /*
4709                          * If there's more than one pool, and we're not in
4710                          * verbose mode (which prints a separator for us),
4711                          * then print a separator.
4712                          *
4713                          * In addition, if we're printing specific vdevs then
4714                          * we also want an ending separator.
4715                          */
4716                         if (((npools > 1 && !verbose &&
4717                             !(cb.cb_flags & IOS_ANYHISTO_M)) ||
4718                             (!(cb.cb_flags & IOS_ANYHISTO_M) &&
4719                             cb.cb_vdev_names_count)) &&
4720                             !cb.cb_scripted) {
4721                                 print_iostat_separator(&cb);
4722                                 if (cb.vcdl != NULL)
4723                                         print_cmd_columns(cb.vcdl, 1);
4724                                 printf("\n");
4725                         }
4726
4727                         if (cb.vcdl != NULL)
4728                                 free_vdev_cmd_data_list(cb.vcdl);
4729
4730                 }
4731
4732                 /*
4733                  * Flush the output so that redirection to a file isn't buffered
4734                  * indefinitely.
4735                  */
4736                 (void) fflush(stdout);
4737
4738                 if (interval == 0)
4739                         break;
4740
4741                 if (count != 0 && --count == 0)
4742                         break;
4743
4744                 (void) fsleep(interval);
4745         }
4746
4747         pool_list_free(list);
4748
4749         return (ret);
4750 }
4751
4752 typedef struct list_cbdata {
4753         boolean_t       cb_verbose;
4754         int             cb_name_flags;
4755         int             cb_namewidth;
4756         boolean_t       cb_scripted;
4757         zprop_list_t    *cb_proplist;
4758         boolean_t       cb_literal;
4759 } list_cbdata_t;
4760
4761 /*
4762  * Given a list of columns to display, output appropriate headers for each one.
4763  */
4764 static void
4765 print_header(list_cbdata_t *cb)
4766 {
4767         zprop_list_t *pl = cb->cb_proplist;
4768         char headerbuf[ZPOOL_MAXPROPLEN];
4769         const char *header;
4770         boolean_t first = B_TRUE;
4771         boolean_t right_justify;
4772         size_t width = 0;
4773
4774         for (; pl != NULL; pl = pl->pl_next) {
4775                 width = pl->pl_width;
4776                 if (first && cb->cb_verbose) {
4777                         /*
4778                          * Reset the width to accommodate the verbose listing
4779                          * of devices.
4780                          */
4781                         width = cb->cb_namewidth;
4782                 }
4783
4784                 if (!first)
4785                         (void) printf("  ");
4786                 else
4787                         first = B_FALSE;
4788
4789                 right_justify = B_FALSE;
4790                 if (pl->pl_prop != ZPROP_INVAL) {
4791                         header = zpool_prop_column_name(pl->pl_prop);
4792                         right_justify = zpool_prop_align_right(pl->pl_prop);
4793                 } else {
4794                         int i;
4795
4796                         for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
4797                                 headerbuf[i] = toupper(pl->pl_user_prop[i]);
4798                         headerbuf[i] = '\0';
4799                         header = headerbuf;
4800                 }
4801
4802                 if (pl->pl_next == NULL && !right_justify)
4803                         (void) printf("%s", header);
4804                 else if (right_justify)
4805                         (void) printf("%*s", (int)width, header);
4806                 else
4807                         (void) printf("%-*s", (int)width, header);
4808         }
4809
4810         (void) printf("\n");
4811 }
4812
4813 /*
4814  * Given a pool and a list of properties, print out all the properties according
4815  * to the described layout.
4816  */
4817 static void
4818 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
4819 {
4820         zprop_list_t *pl = cb->cb_proplist;
4821         boolean_t first = B_TRUE;
4822         char property[ZPOOL_MAXPROPLEN];
4823         char *propstr;
4824         boolean_t right_justify;
4825         size_t width;
4826
4827         for (; pl != NULL; pl = pl->pl_next) {
4828
4829                 width = pl->pl_width;
4830                 if (first && cb->cb_verbose) {
4831                         /*
4832                          * Reset the width to accommodate the verbose listing
4833                          * of devices.
4834                          */
4835                         width = cb->cb_namewidth;
4836                 }
4837
4838                 if (!first) {
4839                         if (cb->cb_scripted)
4840                                 (void) printf("\t");
4841                         else
4842                                 (void) printf("  ");
4843                 } else {
4844                         first = B_FALSE;
4845                 }
4846
4847                 right_justify = B_FALSE;
4848                 if (pl->pl_prop != ZPROP_INVAL) {
4849                         if (zpool_get_prop(zhp, pl->pl_prop, property,
4850                             sizeof (property), NULL, cb->cb_literal) != 0)
4851                                 propstr = "-";
4852                         else
4853                                 propstr = property;
4854
4855                         right_justify = zpool_prop_align_right(pl->pl_prop);
4856                 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
4857                     zpool_prop_unsupported(pl->pl_user_prop)) &&
4858                     zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
4859                     sizeof (property)) == 0) {
4860                         propstr = property;
4861                 } else {
4862                         propstr = "-";
4863                 }
4864
4865
4866                 /*
4867                  * If this is being called in scripted mode, or if this is the
4868                  * last column and it is left-justified, don't include a width
4869                  * format specifier.
4870                  */
4871                 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
4872                         (void) printf("%s", propstr);
4873                 else if (right_justify)
4874                         (void) printf("%*s", (int)width, propstr);
4875                 else
4876                         (void) printf("%-*s", (int)width, propstr);
4877         }
4878
4879         (void) printf("\n");
4880 }
4881
4882 static void
4883 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
4884     boolean_t valid, enum zfs_nicenum_format format)
4885 {
4886         char propval[64];
4887         boolean_t fixed;
4888         size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
4889
4890         switch (prop) {
4891         case ZPOOL_PROP_EXPANDSZ:
4892                 if (value == 0)
4893                         (void) strlcpy(propval, "-", sizeof (propval));
4894                 else
4895                         zfs_nicenum_format(value, propval, sizeof (propval),
4896                             format);
4897                 break;
4898         case ZPOOL_PROP_FRAGMENTATION:
4899                 if (value == ZFS_FRAG_INVALID) {
4900                         (void) strlcpy(propval, "-", sizeof (propval));
4901                 } else if (format == ZFS_NICENUM_RAW) {
4902                         (void) snprintf(propval, sizeof (propval), "%llu",
4903                             (unsigned long long)value);
4904                 } else {
4905                         (void) snprintf(propval, sizeof (propval), "%llu%%",
4906                             (unsigned long long)value);
4907                 }
4908                 break;
4909         case ZPOOL_PROP_CAPACITY:
4910                 if (format == ZFS_NICENUM_RAW)
4911                         (void) snprintf(propval, sizeof (propval), "%llu",
4912                             (unsigned long long)value);
4913                 else
4914                         (void) snprintf(propval, sizeof (propval), "%llu%%",
4915                             (unsigned long long)value);
4916                 break;
4917         default:
4918                 zfs_nicenum_format(value, propval, sizeof (propval), format);
4919         }
4920
4921         if (!valid)
4922                 (void) strlcpy(propval, "-", sizeof (propval));
4923
4924         if (scripted)
4925                 (void) printf("\t%s", propval);
4926         else
4927                 (void) printf("  %*s", (int)width, propval);
4928 }
4929
4930 void
4931 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
4932     list_cbdata_t *cb, int depth)
4933 {
4934         nvlist_t **child;
4935         vdev_stat_t *vs;
4936         uint_t c, children;
4937         char *vname;
4938         boolean_t scripted = cb->cb_scripted;
4939         uint64_t islog = B_FALSE;
4940         boolean_t haslog = B_FALSE;
4941         char *dashes = "%-*s      -      -      -         -      -      -\n";
4942
4943         verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
4944             (uint64_t **)&vs, &c) == 0);
4945
4946         if (name != NULL) {
4947                 boolean_t toplevel = (vs->vs_space != 0);
4948                 uint64_t cap;
4949                 enum zfs_nicenum_format format;
4950
4951                 if (cb->cb_literal)
4952                         format = ZFS_NICENUM_RAW;
4953                 else
4954                         format = ZFS_NICENUM_1024;
4955
4956                 if (scripted)
4957                         (void) printf("\t%s", name);
4958                 else if (strlen(name) + depth > cb->cb_namewidth)
4959                         (void) printf("%*s%s", depth, "", name);
4960                 else
4961                         (void) printf("%*s%s%*s", depth, "", name,
4962                             (int)(cb->cb_namewidth - strlen(name) - depth), "");
4963
4964                 /*
4965                  * Print the properties for the individual vdevs. Some
4966                  * properties are only applicable to toplevel vdevs. The
4967                  * 'toplevel' boolean value is passed to the print_one_column()
4968                  * to indicate that the value is valid.
4969                  */
4970                 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted,
4971                     toplevel, format);
4972                 print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
4973                     toplevel, format);
4974                 print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
4975                     scripted, toplevel, format);
4976                 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
4977                     B_TRUE, format);
4978                 print_one_column(ZPOOL_PROP_FRAGMENTATION,
4979                     vs->vs_fragmentation, scripted,
4980                     (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel),
4981                     format);
4982                 cap = (vs->vs_space == 0) ? 0 :
4983                     (vs->vs_alloc * 100 / vs->vs_space);
4984                 print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel,
4985                     format);
4986                 (void) printf("\n");
4987         }
4988
4989         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
4990             &child, &children) != 0)
4991                 return;
4992
4993         for (c = 0; c < children; c++) {
4994                 uint64_t ishole = B_FALSE;
4995
4996                 if (nvlist_lookup_uint64(child[c],
4997                     ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
4998                         continue;
4999
5000                 if (nvlist_lookup_uint64(child[c],
5001                     ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog) {
5002                         haslog = B_TRUE;
5003                         continue;
5004                 }
5005
5006                 vname = zpool_vdev_name(g_zfs, zhp, child[c],
5007                     cb->cb_name_flags);
5008                 print_list_stats(zhp, vname, child[c], cb, depth + 2);
5009                 free(vname);
5010         }
5011
5012         if (haslog == B_TRUE) {
5013                 /* LINTED E_SEC_PRINTF_VAR_FMT */
5014                 (void) printf(dashes, cb->cb_namewidth, "log");
5015                 for (c = 0; c < children; c++) {
5016                         if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
5017                             &islog) != 0 || !islog)
5018                                 continue;
5019                         vname = zpool_vdev_name(g_zfs, zhp, child[c],
5020                             cb->cb_name_flags);
5021                         print_list_stats(zhp, vname, child[c], cb, depth + 2);
5022                         free(vname);
5023                 }
5024         }
5025
5026         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
5027             &child, &children) == 0 && children > 0) {
5028                 /* LINTED E_SEC_PRINTF_VAR_FMT */
5029                 (void) printf(dashes, cb->cb_namewidth, "cache");
5030                 for (c = 0; c < children; c++) {
5031                         vname = zpool_vdev_name(g_zfs, zhp, child[c],
5032                             cb->cb_name_flags);
5033                         print_list_stats(zhp, vname, child[c], cb, depth + 2);
5034                         free(vname);
5035                 }
5036         }
5037
5038         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child,
5039             &children) == 0 && children > 0) {
5040                 /* LINTED E_SEC_PRINTF_VAR_FMT */
5041                 (void) printf(dashes, cb->cb_namewidth, "spare");
5042                 for (c = 0; c < children; c++) {
5043                         vname = zpool_vdev_name(g_zfs, zhp, child[c],
5044                             cb->cb_name_flags);
5045                         print_list_stats(zhp, vname, child[c], cb, depth + 2);
5046                         free(vname);
5047                 }
5048         }
5049 }
5050
5051
5052 /*
5053  * Generic callback function to list a pool.
5054  */
5055 int
5056 list_callback(zpool_handle_t *zhp, void *data)
5057 {
5058         list_cbdata_t *cbp = data;
5059         nvlist_t *config;
5060         nvlist_t *nvroot;
5061
5062         config = zpool_get_config(zhp, NULL);
5063
5064         print_pool(zhp, cbp);
5065         if (!cbp->cb_verbose)
5066                 return (0);
5067
5068         verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
5069             &nvroot) == 0);
5070         print_list_stats(zhp, NULL, nvroot, cbp, 0);
5071
5072         return (0);
5073 }
5074
5075 /*
5076  * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
5077  *
5078  *      -g      Display guid for individual vdev name.
5079  *      -H      Scripted mode.  Don't display headers, and separate properties
5080  *              by a single tab.
5081  *      -L      Follow links when resolving vdev path name.
5082  *      -o      List of properties to display.  Defaults to
5083  *              "name,size,allocated,free,expandsize,fragmentation,capacity,"
5084  *              "dedupratio,health,altroot"
5085  *      -p      Display values in parsable (exact) format.
5086  *      -P      Display full path for vdev name.
5087  *      -T      Display a timestamp in date(1) or Unix format
5088  *
5089  * List all pools in the system, whether or not they're healthy.  Output space
5090  * statistics for each one, as well as health status summary.
5091  */
5092 int
5093 zpool_do_list(int argc, char **argv)
5094 {
5095         int c;
5096         int ret = 0;
5097         list_cbdata_t cb = { 0 };
5098         static char default_props[] =
5099             "name,size,allocated,free,expandsize,fragmentation,capacity,"
5100             "dedupratio,health,altroot";
5101         char *props = default_props;
5102         float interval = 0;
5103         unsigned long count = 0;
5104         zpool_list_t *list;
5105         boolean_t first = B_TRUE;
5106
5107         /* check options */
5108         while ((c = getopt(argc, argv, ":gHLo:pPT:v")) != -1) {
5109                 switch (c) {
5110                 case 'g':
5111                         cb.cb_name_flags |= VDEV_NAME_GUID;
5112                         break;
5113                 case 'H':
5114                         cb.cb_scripted = B_TRUE;
5115                         break;
5116                 case 'L':
5117                         cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
5118                         break;
5119                 case 'o':
5120                         props = optarg;
5121                         break;
5122                 case 'P':
5123                         cb.cb_name_flags |= VDEV_NAME_PATH;
5124                         break;
5125                 case 'p':
5126                         cb.cb_literal = B_TRUE;
5127                         break;
5128                 case 'T':
5129                         get_timestamp_arg(*optarg);
5130                         break;
5131                 case 'v':
5132                         cb.cb_verbose = B_TRUE;
5133                         break;
5134                 case ':':
5135                         (void) fprintf(stderr, gettext("missing argument for "
5136                             "'%c' option\n"), optopt);
5137                         usage(B_FALSE);
5138                         break;
5139                 case '?':
5140                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5141                             optopt);
5142                         usage(B_FALSE);
5143                 }
5144         }
5145
5146         argc -= optind;
5147         argv += optind;
5148
5149         get_interval_count(&argc, argv, &interval, &count);
5150
5151         if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
5152                 usage(B_FALSE);
5153
5154         for (;;) {
5155                 if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
5156                     &ret)) == NULL)
5157                         return (1);
5158
5159                 if (pool_list_count(list) == 0)
5160                         break;
5161
5162                 if (timestamp_fmt != NODATE)
5163                         print_timestamp(timestamp_fmt);
5164
5165                 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
5166                         print_header(&cb);
5167                         first = B_FALSE;
5168                 }
5169                 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
5170
5171                 if (interval == 0)
5172                         break;
5173
5174                 if (count != 0 && --count == 0)
5175                         break;
5176
5177                 pool_list_free(list);
5178                 (void) fsleep(interval);
5179         }
5180
5181         if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
5182                 (void) printf(gettext("no pools available\n"));
5183                 ret = 0;
5184         }
5185
5186         pool_list_free(list);
5187         zprop_free_list(cb.cb_proplist);
5188         return (ret);
5189 }
5190
5191 static int
5192 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
5193 {
5194         boolean_t force = B_FALSE;
5195         int c;
5196         nvlist_t *nvroot;
5197         char *poolname, *old_disk, *new_disk;
5198         zpool_handle_t *zhp;
5199         nvlist_t *props = NULL;
5200         char *propval;
5201         int ret;
5202
5203         /* check options */
5204         while ((c = getopt(argc, argv, "fo:")) != -1) {
5205                 switch (c) {
5206                 case 'f':
5207                         force = B_TRUE;
5208                         break;
5209                 case 'o':
5210                         if ((propval = strchr(optarg, '=')) == NULL) {
5211                                 (void) fprintf(stderr, gettext("missing "
5212                                     "'=' for -o option\n"));
5213                                 usage(B_FALSE);
5214                         }
5215                         *propval = '\0';
5216                         propval++;
5217
5218                         if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
5219                             (add_prop_list(optarg, propval, &props, B_TRUE)))
5220                                 usage(B_FALSE);
5221                         break;
5222                 case '?':
5223                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5224                             optopt);
5225                         usage(B_FALSE);
5226                 }
5227         }
5228
5229         argc -= optind;
5230         argv += optind;
5231
5232         /* get pool name and check number of arguments */
5233         if (argc < 1) {
5234                 (void) fprintf(stderr, gettext("missing pool name argument\n"));
5235                 usage(B_FALSE);
5236         }
5237
5238         poolname = argv[0];
5239
5240         if (argc < 2) {
5241                 (void) fprintf(stderr,
5242                     gettext("missing <device> specification\n"));
5243                 usage(B_FALSE);
5244         }
5245
5246         old_disk = argv[1];
5247
5248         if (argc < 3) {
5249                 if (!replacing) {
5250                         (void) fprintf(stderr,
5251                             gettext("missing <new_device> specification\n"));
5252                         usage(B_FALSE);
5253                 }
5254                 new_disk = old_disk;
5255                 argc -= 1;
5256                 argv += 1;
5257         } else {
5258                 new_disk = argv[2];
5259                 argc -= 2;
5260                 argv += 2;
5261         }
5262
5263         if (argc > 1) {
5264                 (void) fprintf(stderr, gettext("too many arguments\n"));
5265                 usage(B_FALSE);
5266         }
5267
5268         if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
5269                 nvlist_free(props);
5270                 return (1);
5271         }
5272
5273         if (zpool_get_config(zhp, NULL) == NULL) {
5274                 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
5275                     poolname);
5276                 zpool_close(zhp);
5277                 nvlist_free(props);
5278                 return (1);
5279         }
5280
5281         /* unless manually specified use "ashift" pool property (if set) */
5282         if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) {
5283                 int intval;
5284                 zprop_source_t src;
5285                 char strval[ZPOOL_MAXPROPLEN];
5286
5287                 intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src);
5288                 if (src != ZPROP_SRC_DEFAULT) {
5289                         (void) sprintf(strval, "%" PRId32, intval);
5290                         verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval,
5291                             &props, B_TRUE) == 0);
5292                 }
5293         }
5294
5295         nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE,
5296             argc, argv);
5297         if (nvroot == NULL) {
5298                 zpool_close(zhp);
5299                 nvlist_free(props);
5300                 return (1);
5301         }
5302
5303         ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
5304
5305         nvlist_free(props);
5306         nvlist_free(nvroot);
5307         zpool_close(zhp);
5308
5309         return (ret);
5310 }
5311
5312 /*
5313  * zpool replace [-f] <pool> <device> <new_device>
5314  *
5315  *      -f      Force attach, even if <new_device> appears to be in use.
5316  *
5317  * Replace <device> with <new_device>.
5318  */
5319 /* ARGSUSED */
5320 int
5321 zpool_do_replace(int argc, char **argv)
5322 {
5323         return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
5324 }
5325
5326 /*
5327  * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
5328  *
5329  *      -f      Force attach, even if <new_device> appears to be in use.
5330  *      -o      Set property=value.
5331  *
5332  * Attach <new_device> to the mirror containing <device>.  If <device> is not
5333  * part of a mirror, then <device> will be transformed into a mirror of
5334  * <device> and <new_device>.  In either case, <new_device> will begin life
5335  * with a DTL of [0, now], and will immediately begin to resilver itself.
5336  */
5337 int
5338 zpool_do_attach(int argc, char **argv)
5339 {
5340         return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
5341 }
5342
5343 /*
5344  * zpool detach [-f] <pool> <device>
5345  *
5346  *      -f      Force detach of <device>, even if DTLs argue against it
5347  *              (not supported yet)
5348  *
5349  * Detach a device from a mirror.  The operation will be refused if <device>
5350  * is the last device in the mirror, or if the DTLs indicate that this device
5351  * has the only valid copy of some data.
5352  */
5353 /* ARGSUSED */
5354 int
5355 zpool_do_detach(int argc, char **argv)
5356 {
5357         int c;
5358         char *poolname, *path;
5359         zpool_handle_t *zhp;
5360         int ret;
5361
5362         /* check options */
5363         while ((c = getopt(argc, argv, "f")) != -1) {
5364                 switch (c) {
5365                 case 'f':
5366                 case '?':
5367                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5368                             optopt);
5369                         usage(B_FALSE);
5370                 }
5371         }
5372
5373         argc -= optind;
5374         argv += optind;
5375
5376         /* get pool name and check number of arguments */
5377         if (argc < 1) {
5378                 (void) fprintf(stderr, gettext("missing pool name argument\n"));
5379                 usage(B_FALSE);
5380         }
5381
5382         if (argc < 2) {
5383                 (void) fprintf(stderr,
5384                     gettext("missing <device> specification\n"));
5385                 usage(B_FALSE);
5386         }
5387
5388         poolname = argv[0];
5389         path = argv[1];
5390
5391         if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5392                 return (1);
5393
5394         ret = zpool_vdev_detach(zhp, path);
5395
5396         zpool_close(zhp);
5397
5398         return (ret);
5399 }
5400
5401 /*
5402  * zpool split [-gLnP] [-o prop=val] ...
5403  *              [-o mntopt] ...
5404  *              [-R altroot] <pool> <newpool> [<device> ...]
5405  *
5406  *      -g      Display guid for individual vdev name.
5407  *      -L      Follow links when resolving vdev path name.
5408  *      -n      Do not split the pool, but display the resulting layout if
5409  *              it were to be split.
5410  *      -o      Set property=value, or set mount options.
5411  *      -P      Display full path for vdev name.
5412  *      -R      Mount the split-off pool under an alternate root.
5413  *      -l      Load encryption keys while importing.
5414  *
5415  * Splits the named pool and gives it the new pool name.  Devices to be split
5416  * off may be listed, provided that no more than one device is specified
5417  * per top-level vdev mirror.  The newly split pool is left in an exported
5418  * state unless -R is specified.
5419  *
5420  * Restrictions: the top-level of the pool pool must only be made up of
5421  * mirrors; all devices in the pool must be healthy; no device may be
5422  * undergoing a resilvering operation.
5423  */
5424 int
5425 zpool_do_split(int argc, char **argv)
5426 {
5427         char *srcpool, *newpool, *propval;
5428         char *mntopts = NULL;
5429         splitflags_t flags;
5430         int c, ret = 0;
5431         boolean_t loadkeys = B_FALSE;
5432         zpool_handle_t *zhp;
5433         nvlist_t *config, *props = NULL;
5434
5435         flags.dryrun = B_FALSE;
5436         flags.import = B_FALSE;
5437         flags.name_flags = 0;
5438
5439         /* check options */
5440         while ((c = getopt(argc, argv, ":gLR:lno:P")) != -1) {
5441                 switch (c) {
5442                 case 'g':
5443                         flags.name_flags |= VDEV_NAME_GUID;
5444                         break;
5445                 case 'L':
5446                         flags.name_flags |= VDEV_NAME_FOLLOW_LINKS;
5447                         break;
5448                 case 'R':
5449                         flags.import = B_TRUE;
5450                         if (add_prop_list(
5451                             zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
5452                             &props, B_TRUE) != 0) {
5453                                 nvlist_free(props);
5454                                 usage(B_FALSE);
5455                         }
5456                         break;
5457                 case 'l':
5458                         loadkeys = B_TRUE;
5459                         break;
5460                 case 'n':
5461                         flags.dryrun = B_TRUE;
5462                         break;
5463                 case 'o':
5464                         if ((propval = strchr(optarg, '=')) != NULL) {
5465                                 *propval = '\0';
5466                                 propval++;
5467                                 if (add_prop_list(optarg, propval,
5468                                     &props, B_TRUE) != 0) {
5469                                         nvlist_free(props);
5470                                         usage(B_FALSE);
5471                                 }
5472                         } else {
5473                                 mntopts = optarg;
5474                         }
5475                         break;
5476                 case 'P':
5477                         flags.name_flags |= VDEV_NAME_PATH;
5478                         break;
5479                 case ':':
5480                         (void) fprintf(stderr, gettext("missing argument for "
5481                             "'%c' option\n"), optopt);
5482                         usage(B_FALSE);
5483                         break;
5484                 case '?':
5485                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5486                             optopt);
5487                         usage(B_FALSE);
5488                         break;
5489                 }
5490         }
5491
5492         if (!flags.import && mntopts != NULL) {
5493                 (void) fprintf(stderr, gettext("setting mntopts is only "
5494                     "valid when importing the pool\n"));
5495                 usage(B_FALSE);
5496         }
5497
5498         if (!flags.import && loadkeys) {
5499                 (void) fprintf(stderr, gettext("loading keys is only "
5500                     "valid when importing the pool\n"));
5501                 usage(B_FALSE);
5502         }
5503
5504         argc -= optind;
5505         argv += optind;
5506
5507         if (argc < 1) {
5508                 (void) fprintf(stderr, gettext("Missing pool name\n"));
5509                 usage(B_FALSE);
5510         }
5511         if (argc < 2) {
5512                 (void) fprintf(stderr, gettext("Missing new pool name\n"));
5513                 usage(B_FALSE);
5514         }
5515
5516         srcpool = argv[0];
5517         newpool = argv[1];
5518
5519         argc -= 2;
5520         argv += 2;
5521
5522         if ((zhp = zpool_open(g_zfs, srcpool)) == NULL) {
5523                 nvlist_free(props);
5524                 return (1);
5525         }
5526
5527         config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
5528         if (config == NULL) {
5529                 ret = 1;
5530         } else {
5531                 if (flags.dryrun) {
5532                         (void) printf(gettext("would create '%s' with the "
5533                             "following layout:\n\n"), newpool);
5534                         print_vdev_tree(NULL, newpool, config, 0, B_FALSE,
5535                             flags.name_flags);
5536                 }
5537         }
5538
5539         zpool_close(zhp);
5540
5541         if (ret != 0 || flags.dryrun || !flags.import) {
5542                 nvlist_free(config);
5543                 nvlist_free(props);
5544                 return (ret);
5545         }
5546
5547         /*
5548          * The split was successful. Now we need to open the new
5549          * pool and import it.
5550          */
5551         if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL) {
5552                 nvlist_free(config);
5553                 nvlist_free(props);
5554                 return (1);
5555         }
5556
5557         if (loadkeys) {
5558                 ret = zfs_crypto_attempt_load_keys(g_zfs, newpool);
5559                 if (ret != 0)
5560                         ret = 1;
5561         }
5562
5563         if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
5564             zpool_enable_datasets(zhp, mntopts, 0) != 0) {
5565                 ret = 1;
5566                 (void) fprintf(stderr, gettext("Split was successful, but "
5567                     "the datasets could not all be mounted\n"));
5568                 (void) fprintf(stderr, gettext("Try doing '%s' with a "
5569                     "different altroot\n"), "zpool import");
5570         }
5571         zpool_close(zhp);
5572         nvlist_free(config);
5573         nvlist_free(props);
5574
5575         return (ret);
5576 }
5577
5578
5579
5580 /*
5581  * zpool online <pool> <device> ...
5582  */
5583 int
5584 zpool_do_online(int argc, char **argv)
5585 {
5586         int c, i;
5587         char *poolname;
5588         zpool_handle_t *zhp;
5589         int ret = 0;
5590         vdev_state_t newstate;
5591         int flags = 0;
5592
5593         /* check options */
5594         while ((c = getopt(argc, argv, "et")) != -1) {
5595                 switch (c) {
5596                 case 'e':
5597                         flags |= ZFS_ONLINE_EXPAND;
5598                         break;
5599                 case 't':
5600                 case '?':
5601                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5602                             optopt);
5603                         usage(B_FALSE);
5604                 }
5605         }
5606
5607         argc -= optind;
5608         argv += optind;
5609
5610         /* get pool name and check number of arguments */
5611         if (argc < 1) {
5612                 (void) fprintf(stderr, gettext("missing pool name\n"));
5613                 usage(B_FALSE);
5614         }
5615         if (argc < 2) {
5616                 (void) fprintf(stderr, gettext("missing device name\n"));
5617                 usage(B_FALSE);
5618         }
5619
5620         poolname = argv[0];
5621
5622         if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5623                 return (1);
5624
5625         for (i = 1; i < argc; i++) {
5626                 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
5627                         if (newstate != VDEV_STATE_HEALTHY) {
5628                                 (void) printf(gettext("warning: device '%s' "
5629                                     "onlined, but remains in faulted state\n"),
5630                                     argv[i]);
5631                                 if (newstate == VDEV_STATE_FAULTED)
5632                                         (void) printf(gettext("use 'zpool "
5633                                             "clear' to restore a faulted "
5634                                             "device\n"));
5635                                 else
5636                                         (void) printf(gettext("use 'zpool "
5637                                             "replace' to replace devices "
5638                                             "that are no longer present\n"));
5639                         }
5640                 } else {
5641                         ret = 1;
5642                 }
5643         }
5644
5645         zpool_close(zhp);
5646
5647         return (ret);
5648 }
5649
5650 /*
5651  * zpool offline [-ft] <pool> <device> ...
5652  *
5653  *      -f      Force the device into a faulted state.
5654  *
5655  *      -t      Only take the device off-line temporarily.  The offline/faulted
5656  *              state will not be persistent across reboots.
5657  */
5658 /* ARGSUSED */
5659 int
5660 zpool_do_offline(int argc, char **argv)
5661 {
5662         int c, i;
5663         char *poolname;
5664         zpool_handle_t *zhp;
5665         int ret = 0;
5666         boolean_t istmp = B_FALSE;
5667         boolean_t fault = B_FALSE;
5668
5669         /* check options */
5670         while ((c = getopt(argc, argv, "ft")) != -1) {
5671                 switch (c) {
5672                 case 'f':
5673                         fault = B_TRUE;
5674                         break;
5675                 case 't':
5676                         istmp = B_TRUE;
5677                         break;
5678                 case '?':
5679                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5680                             optopt);
5681                         usage(B_FALSE);
5682                 }
5683         }
5684
5685         argc -= optind;
5686         argv += optind;
5687
5688         /* get pool name and check number of arguments */
5689         if (argc < 1) {
5690                 (void) fprintf(stderr, gettext("missing pool name\n"));
5691                 usage(B_FALSE);
5692         }
5693         if (argc < 2) {
5694                 (void) fprintf(stderr, gettext("missing device name\n"));
5695                 usage(B_FALSE);
5696         }
5697
5698         poolname = argv[0];
5699
5700         if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5701                 return (1);
5702
5703         for (i = 1; i < argc; i++) {
5704                 if (fault) {
5705                         uint64_t guid = zpool_vdev_path_to_guid(zhp, argv[i]);
5706                         vdev_aux_t aux;
5707                         if (istmp == B_FALSE) {
5708                                 /* Force the fault to persist across imports */
5709                                 aux = VDEV_AUX_EXTERNAL_PERSIST;
5710                         } else {
5711                                 aux = VDEV_AUX_EXTERNAL;
5712                         }
5713
5714                         if (guid == 0 || zpool_vdev_fault(zhp, guid, aux) != 0)
5715                                 ret = 1;
5716                 } else {
5717                         if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
5718                                 ret = 1;
5719                 }
5720         }
5721
5722         zpool_close(zhp);
5723
5724         return (ret);
5725 }
5726
5727 /*
5728  * zpool clear <pool> [device]
5729  *
5730  * Clear all errors associated with a pool or a particular device.
5731  */
5732 int
5733 zpool_do_clear(int argc, char **argv)
5734 {
5735         int c;
5736         int ret = 0;
5737         boolean_t dryrun = B_FALSE;
5738         boolean_t do_rewind = B_FALSE;
5739         boolean_t xtreme_rewind = B_FALSE;
5740         uint32_t rewind_policy = ZPOOL_NO_REWIND;
5741         nvlist_t *policy = NULL;
5742         zpool_handle_t *zhp;
5743         char *pool, *device;
5744
5745         /* check options */
5746         while ((c = getopt(argc, argv, "FnX")) != -1) {
5747                 switch (c) {
5748                 case 'F':
5749                         do_rewind = B_TRUE;
5750                         break;
5751                 case 'n':
5752                         dryrun = B_TRUE;
5753                         break;
5754                 case 'X':
5755                         xtreme_rewind = B_TRUE;
5756                         break;
5757                 case '?':
5758                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5759                             optopt);
5760                         usage(B_FALSE);
5761                 }
5762         }
5763
5764         argc -= optind;
5765         argv += optind;
5766
5767         if (argc < 1) {
5768                 (void) fprintf(stderr, gettext("missing pool name\n"));
5769                 usage(B_FALSE);
5770         }
5771
5772         if (argc > 2) {
5773                 (void) fprintf(stderr, gettext("too many arguments\n"));
5774                 usage(B_FALSE);
5775         }
5776
5777         if ((dryrun || xtreme_rewind) && !do_rewind) {
5778                 (void) fprintf(stderr,
5779                     gettext("-n or -X only meaningful with -F\n"));
5780                 usage(B_FALSE);
5781         }
5782         if (dryrun)
5783                 rewind_policy = ZPOOL_TRY_REWIND;
5784         else if (do_rewind)
5785                 rewind_policy = ZPOOL_DO_REWIND;
5786         if (xtreme_rewind)
5787                 rewind_policy |= ZPOOL_EXTREME_REWIND;
5788
5789         /* In future, further rewind policy choices can be passed along here */
5790         if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
5791             nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
5792                 return (1);
5793
5794         pool = argv[0];
5795         device = argc == 2 ? argv[1] : NULL;
5796
5797         if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
5798                 nvlist_free(policy);
5799                 return (1);
5800         }
5801
5802         if (zpool_clear(zhp, device, policy) != 0)
5803                 ret = 1;
5804
5805         zpool_close(zhp);
5806
5807         nvlist_free(policy);
5808
5809         return (ret);
5810 }
5811
5812 /*
5813  * zpool reguid <pool>
5814  */
5815 int
5816 zpool_do_reguid(int argc, char **argv)
5817 {
5818         int c;
5819         char *poolname;
5820         zpool_handle_t *zhp;
5821         int ret = 0;
5822
5823         /* check options */
5824         while ((c = getopt(argc, argv, "")) != -1) {
5825                 switch (c) {
5826                 case '?':
5827                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5828                             optopt);
5829                         usage(B_FALSE);
5830                 }
5831         }
5832
5833         argc -= optind;
5834         argv += optind;
5835
5836         /* get pool name and check number of arguments */
5837         if (argc < 1) {
5838                 (void) fprintf(stderr, gettext("missing pool name\n"));
5839                 usage(B_FALSE);
5840         }
5841
5842         if (argc > 1) {
5843                 (void) fprintf(stderr, gettext("too many arguments\n"));
5844                 usage(B_FALSE);
5845         }
5846
5847         poolname = argv[0];
5848         if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
5849                 return (1);
5850
5851         ret = zpool_reguid(zhp);
5852
5853         zpool_close(zhp);
5854         return (ret);
5855 }
5856
5857
5858 /*
5859  * zpool reopen <pool>
5860  *
5861  * Reopen the pool so that the kernel can update the sizes of all vdevs.
5862  */
5863 int
5864 zpool_do_reopen(int argc, char **argv)
5865 {
5866         int c;
5867         int ret = 0;
5868         boolean_t scrub_restart = B_TRUE;
5869
5870         /* check options */
5871         while ((c = getopt(argc, argv, "n")) != -1) {
5872                 switch (c) {
5873                 case 'n':
5874                         scrub_restart = B_FALSE;
5875                         break;
5876                 case '?':
5877                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5878                             optopt);
5879                         usage(B_FALSE);
5880                 }
5881         }
5882
5883         argc -= optind;
5884         argv += optind;
5885
5886         /* if argc == 0 we will execute zpool_reopen_one on all pools */
5887         ret = for_each_pool(argc, argv, B_TRUE, NULL, zpool_reopen_one,
5888             &scrub_restart);
5889
5890         return (ret);
5891 }
5892
5893 typedef struct scrub_cbdata {
5894         int     cb_type;
5895         int     cb_argc;
5896         char    **cb_argv;
5897         pool_scrub_cmd_t cb_scrub_cmd;
5898 } scrub_cbdata_t;
5899
5900 int
5901 scrub_callback(zpool_handle_t *zhp, void *data)
5902 {
5903         scrub_cbdata_t *cb = data;
5904         int err;
5905
5906         /*
5907          * Ignore faulted pools.
5908          */
5909         if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5910                 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
5911                     "currently unavailable\n"), zpool_get_name(zhp));
5912                 return (1);
5913         }
5914
5915         err = zpool_scan(zhp, cb->cb_type, cb->cb_scrub_cmd);
5916
5917         return (err != 0);
5918 }
5919
5920 /*
5921  * zpool scrub [-s | -p] <pool> ...
5922  *
5923  *      -s      Stop.  Stops any in-progress scrub.
5924  *      -p      Pause. Pause in-progress scrub.
5925  */
5926 int
5927 zpool_do_scrub(int argc, char **argv)
5928 {
5929         int c;
5930         scrub_cbdata_t cb;
5931
5932         cb.cb_type = POOL_SCAN_SCRUB;
5933         cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
5934
5935         /* check options */
5936         while ((c = getopt(argc, argv, "sp")) != -1) {
5937                 switch (c) {
5938                 case 's':
5939                         cb.cb_type = POOL_SCAN_NONE;
5940                         break;
5941                 case 'p':
5942                         cb.cb_scrub_cmd = POOL_SCRUB_PAUSE;
5943                         break;
5944                 case '?':
5945                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5946                             optopt);
5947                         usage(B_FALSE);
5948                 }
5949         }
5950
5951         if (cb.cb_type == POOL_SCAN_NONE &&
5952             cb.cb_scrub_cmd == POOL_SCRUB_PAUSE) {
5953                 (void) fprintf(stderr, gettext("invalid option combination: "
5954                     "-s and -p are mutually exclusive\n"));
5955                 usage(B_FALSE);
5956         }
5957
5958         cb.cb_argc = argc;
5959         cb.cb_argv = argv;
5960         argc -= optind;
5961         argv += optind;
5962
5963         if (argc < 1) {
5964                 (void) fprintf(stderr, gettext("missing pool name argument\n"));
5965                 usage(B_FALSE);
5966         }
5967
5968         return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
5969 }
5970
5971 /*
5972  * Print out detailed scrub status.
5973  */
5974 void
5975 print_scan_status(pool_scan_stat_t *ps)
5976 {
5977         time_t start, end, pause;
5978         uint64_t total_secs_left;
5979         uint64_t elapsed, secs_left, mins_left, hours_left, days_left;
5980         uint64_t pass_scanned, scanned, pass_issued, issued, total;
5981         uint_t scan_rate, issue_rate;
5982         double fraction_done;
5983         char processed_buf[7], scanned_buf[7], issued_buf[7], total_buf[7];
5984         char srate_buf[7], irate_buf[7];
5985
5986         (void) printf(gettext("  scan: "));
5987
5988         /* If there's never been a scan, there's not much to say. */
5989         if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
5990             ps->pss_func >= POOL_SCAN_FUNCS) {
5991                 (void) printf(gettext("none requested\n"));
5992                 return;
5993         }
5994
5995         start = ps->pss_start_time;
5996         end = ps->pss_end_time;
5997         pause = ps->pss_pass_scrub_pause;
5998
5999         zfs_nicebytes(ps->pss_processed, processed_buf, sizeof (processed_buf));
6000
6001         assert(ps->pss_func == POOL_SCAN_SCRUB ||
6002             ps->pss_func == POOL_SCAN_RESILVER);
6003
6004         /* Scan is finished or canceled. */
6005         if (ps->pss_state == DSS_FINISHED) {
6006                 total_secs_left = end - start;
6007                 days_left = total_secs_left / 60 / 60 / 24;
6008                 hours_left = (total_secs_left / 60 / 60) % 24;
6009                 mins_left = (total_secs_left / 60) % 60;
6010                 secs_left = (total_secs_left % 60);
6011
6012                 if (ps->pss_func == POOL_SCAN_SCRUB) {
6013                         (void) printf(gettext("scrub repaired %s "
6014                             "in %llu days %02llu:%02llu:%02llu "
6015                             "with %llu errors on %s"), processed_buf,
6016                             (u_longlong_t)days_left, (u_longlong_t)hours_left,
6017                             (u_longlong_t)mins_left, (u_longlong_t)secs_left,
6018                             (u_longlong_t)ps->pss_errors, ctime(&end));
6019                 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
6020                         (void) printf(gettext("resilvered %s "
6021                             "in %llu days %02llu:%02llu:%02llu "
6022                             "with %llu errors on %s"), processed_buf,
6023                             (u_longlong_t)days_left, (u_longlong_t)hours_left,
6024                             (u_longlong_t)mins_left, (u_longlong_t)secs_left,
6025                             (u_longlong_t)ps->pss_errors, ctime(&end));
6026                 }
6027                 return;
6028         } else if (ps->pss_state == DSS_CANCELED) {
6029                 if (ps->pss_func == POOL_SCAN_SCRUB) {
6030                         (void) printf(gettext("scrub canceled on %s"),
6031                             ctime(&end));
6032                 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
6033                         (void) printf(gettext("resilver canceled on %s"),
6034                             ctime(&end));
6035                 }
6036                 return;
6037         }
6038
6039         assert(ps->pss_state == DSS_SCANNING);
6040
6041         /* Scan is in progress. Resilvers can't be paused. */
6042         if (ps->pss_func == POOL_SCAN_SCRUB) {
6043                 if (pause == 0) {
6044                         (void) printf(gettext("scrub in progress since %s"),
6045                             ctime(&start));
6046                 } else {
6047                         (void) printf(gettext("scrub paused since %s"),
6048                             ctime(&pause));
6049                         (void) printf(gettext("\tscrub started on %s"),
6050                             ctime(&start));
6051                 }
6052         } else if (ps->pss_func == POOL_SCAN_RESILVER) {
6053                 (void) printf(gettext("resilver in progress since %s"),
6054                     ctime(&start));
6055         }
6056
6057         scanned = ps->pss_examined;
6058         pass_scanned = ps->pss_pass_exam;
6059         issued = ps->pss_issued;
6060         pass_issued = ps->pss_pass_issued;
6061         total = ps->pss_to_examine;
6062
6063         /* we are only done with a block once we have issued the IO for it */
6064         fraction_done = (double)issued / total;
6065
6066         /* elapsed time for this pass, rounding up to 1 if it's 0 */
6067         elapsed = time(NULL) - ps->pss_pass_start;
6068         elapsed -= ps->pss_pass_scrub_spent_paused;
6069         elapsed = (elapsed != 0) ? elapsed : 1;
6070
6071         scan_rate = pass_scanned / elapsed;
6072         issue_rate = pass_issued / elapsed;
6073         total_secs_left = (issue_rate != 0) ?
6074             ((total - issued) / issue_rate) : UINT64_MAX;
6075
6076         days_left = total_secs_left / 60 / 60 / 24;
6077         hours_left = (total_secs_left / 60 / 60) % 24;
6078         mins_left = (total_secs_left / 60) % 60;
6079         secs_left = (total_secs_left % 60);
6080
6081         /* format all of the numbers we will be reporting */
6082         zfs_nicebytes(scanned, scanned_buf, sizeof (scanned_buf));
6083         zfs_nicebytes(issued, issued_buf, sizeof (issued_buf));
6084         zfs_nicebytes(total, total_buf, sizeof (total_buf));
6085         zfs_nicebytes(scan_rate, srate_buf, sizeof (srate_buf));
6086         zfs_nicebytes(issue_rate, irate_buf, sizeof (irate_buf));
6087
6088         /* do not print estimated time if we have a paused scrub */
6089         if (pause == 0) {
6090                 (void) printf(gettext("\t%s scanned at %s/s, "
6091                     "%s issued at %s/s, %s total\n"),
6092                     scanned_buf, srate_buf, issued_buf, irate_buf, total_buf);
6093         } else {
6094                 (void) printf(gettext("\t%s scanned, %s issued, %s total\n"),
6095                     scanned_buf, issued_buf, total_buf);
6096         }
6097
6098         if (ps->pss_func == POOL_SCAN_RESILVER) {
6099                 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
6100                     processed_buf, 100 * fraction_done);
6101         } else if (ps->pss_func == POOL_SCAN_SCRUB) {
6102                 (void) printf(gettext("\t%s repaired, %.2f%% done"),
6103                     processed_buf, 100 * fraction_done);
6104         }
6105
6106         if (pause == 0) {
6107                 if (issue_rate >= 10 * 1024 * 1024) {
6108                         (void) printf(gettext(", %llu days "
6109                             "%02llu:%02llu:%02llu to go\n"),
6110                             (u_longlong_t)days_left, (u_longlong_t)hours_left,
6111                             (u_longlong_t)mins_left, (u_longlong_t)secs_left);
6112                 } else {
6113                         (void) printf(gettext(", no estimated "
6114                             "completion time\n"));
6115                 }
6116         } else {
6117                 (void) printf(gettext("\n"));
6118         }
6119 }
6120
6121 static void
6122 print_error_log(zpool_handle_t *zhp)
6123 {
6124         nvlist_t *nverrlist = NULL;
6125         nvpair_t *elem;
6126         char *pathname;
6127         size_t len = MAXPATHLEN * 2;
6128
6129         if (zpool_get_errlog(zhp, &nverrlist) != 0)
6130                 return;
6131
6132         (void) printf("errors: Permanent errors have been "
6133             "detected in the following files:\n\n");
6134
6135         pathname = safe_malloc(len);
6136         elem = NULL;
6137         while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
6138                 nvlist_t *nv;
6139                 uint64_t dsobj, obj;
6140
6141                 verify(nvpair_value_nvlist(elem, &nv) == 0);
6142                 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
6143                     &dsobj) == 0);
6144                 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
6145                     &obj) == 0);
6146                 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
6147                 (void) printf("%7s %s\n", "", pathname);
6148         }
6149         free(pathname);
6150         nvlist_free(nverrlist);
6151 }
6152
6153 static void
6154 print_spares(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **spares,
6155     uint_t nspares)
6156 {
6157         uint_t i;
6158         char *name;
6159
6160         if (nspares == 0)
6161                 return;
6162
6163         (void) printf(gettext("\tspares\n"));
6164
6165         for (i = 0; i < nspares; i++) {
6166                 name = zpool_vdev_name(g_zfs, zhp, spares[i],
6167                     cb->cb_name_flags);
6168                 print_status_config(zhp, cb, name, spares[i], 2, B_TRUE);
6169                 free(name);
6170         }
6171 }
6172
6173 static void
6174 print_l2cache(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **l2cache,
6175     uint_t nl2cache)
6176 {
6177         uint_t i;
6178         char *name;
6179
6180         if (nl2cache == 0)
6181                 return;
6182
6183         (void) printf(gettext("\tcache\n"));
6184
6185         for (i = 0; i < nl2cache; i++) {
6186                 name = zpool_vdev_name(g_zfs, zhp, l2cache[i],
6187                     cb->cb_name_flags);
6188                 print_status_config(zhp, cb, name, l2cache[i], 2, B_FALSE);
6189                 free(name);
6190         }
6191 }
6192
6193 static void
6194 print_dedup_stats(nvlist_t *config)
6195 {
6196         ddt_histogram_t *ddh;
6197         ddt_stat_t *dds;
6198         ddt_object_t *ddo;
6199         uint_t c;
6200         char dspace[6], mspace[6];
6201
6202         /*
6203          * If the pool was faulted then we may not have been able to
6204          * obtain the config. Otherwise, if we have anything in the dedup
6205          * table continue processing the stats.
6206          */
6207         if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
6208             (uint64_t **)&ddo, &c) != 0)
6209                 return;
6210
6211         (void) printf("\n");
6212         (void) printf(gettext(" dedup: "));
6213         if (ddo->ddo_count == 0) {
6214                 (void) printf(gettext("no DDT entries\n"));
6215                 return;
6216         }
6217
6218         zfs_nicebytes(ddo->ddo_dspace, dspace, sizeof (dspace));
6219         zfs_nicebytes(ddo->ddo_mspace, mspace, sizeof (mspace));
6220         (void) printf("DDT entries %llu, size %s on disk, %s in core\n",
6221             (u_longlong_t)ddo->ddo_count,
6222             dspace,
6223             mspace);
6224
6225         verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
6226             (uint64_t **)&dds, &c) == 0);
6227         verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
6228             (uint64_t **)&ddh, &c) == 0);
6229         zpool_dump_ddt(dds, ddh);
6230 }
6231
6232 /*
6233  * Display a summary of pool status.  Displays a summary such as:
6234  *
6235  *        pool: tank
6236  *      status: DEGRADED
6237  *      reason: One or more devices ...
6238  *         see: http://zfsonlinux.org/msg/ZFS-xxxx-01
6239  *      config:
6240  *              mirror          DEGRADED
6241  *                c1t0d0        OK
6242  *                c2t0d0        UNAVAIL
6243  *
6244  * When given the '-v' option, we print out the complete config.  If the '-e'
6245  * option is specified, then we print out error rate information as well.
6246  */
6247 int
6248 status_callback(zpool_handle_t *zhp, void *data)
6249 {
6250         status_cbdata_t *cbp = data;
6251         nvlist_t *config, *nvroot;
6252         char *msgid;
6253         zpool_status_t reason;
6254         zpool_errata_t errata;
6255         const char *health;
6256         uint_t c;
6257         vdev_stat_t *vs;
6258
6259         config = zpool_get_config(zhp, NULL);
6260         reason = zpool_get_status(zhp, &msgid, &errata);
6261
6262         cbp->cb_count++;
6263
6264         /*
6265          * If we were given 'zpool status -x', only report those pools with
6266          * problems.
6267          */
6268         if (cbp->cb_explain &&
6269             (reason == ZPOOL_STATUS_OK ||
6270             reason == ZPOOL_STATUS_VERSION_OLDER ||
6271             reason == ZPOOL_STATUS_FEAT_DISABLED)) {
6272                 if (!cbp->cb_allpools) {
6273                         (void) printf(gettext("pool '%s' is healthy\n"),
6274                             zpool_get_name(zhp));
6275                         if (cbp->cb_first)
6276                                 cbp->cb_first = B_FALSE;
6277                 }
6278                 return (0);
6279         }
6280
6281         if (cbp->cb_first)
6282                 cbp->cb_first = B_FALSE;
6283         else
6284                 (void) printf("\n");
6285
6286         verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
6287             &nvroot) == 0);
6288         verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
6289             (uint64_t **)&vs, &c) == 0);
6290         health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
6291
6292         (void) printf(gettext("  pool: %s\n"), zpool_get_name(zhp));
6293         (void) printf(gettext(" state: %s\n"), health);
6294
6295         switch (reason) {
6296         case ZPOOL_STATUS_MISSING_DEV_R:
6297                 (void) printf(gettext("status: One or more devices could not "
6298                     "be opened.  Sufficient replicas exist for\n\tthe pool to "
6299                     "continue functioning in a degraded state.\n"));
6300                 (void) printf(gettext("action: Attach the missing device and "
6301                     "online it using 'zpool online'.\n"));
6302                 break;
6303
6304         case ZPOOL_STATUS_MISSING_DEV_NR:
6305                 (void) printf(gettext("status: One or more devices could not "
6306                     "be opened.  There are insufficient\n\treplicas for the "
6307                     "pool to continue functioning.\n"));
6308                 (void) printf(gettext("action: Attach the missing device and "
6309                     "online it using 'zpool online'.\n"));
6310                 break;
6311
6312         case ZPOOL_STATUS_CORRUPT_LABEL_R:
6313                 (void) printf(gettext("status: One or more devices could not "
6314                     "be used because the label is missing or\n\tinvalid.  "
6315                     "Sufficient replicas exist for the pool to continue\n\t"
6316                     "functioning in a degraded state.\n"));
6317                 (void) printf(gettext("action: Replace the device using "
6318                     "'zpool replace'.\n"));
6319                 break;
6320
6321         case ZPOOL_STATUS_CORRUPT_LABEL_NR:
6322                 (void) printf(gettext("status: One or more devices could not "
6323                     "be used because the label is missing \n\tor invalid.  "
6324                     "There are insufficient replicas for the pool to "
6325                     "continue\n\tfunctioning.\n"));
6326                 zpool_explain_recover(zpool_get_handle(zhp),
6327                     zpool_get_name(zhp), reason, config);
6328                 break;
6329
6330         case ZPOOL_STATUS_FAILING_DEV:
6331                 (void) printf(gettext("status: One or more devices has "
6332                     "experienced an unrecoverable error.  An\n\tattempt was "
6333                     "made to correct the error.  Applications are "
6334                     "unaffected.\n"));
6335                 (void) printf(gettext("action: Determine if the device needs "
6336                     "to be replaced, and clear the errors\n\tusing "
6337                     "'zpool clear' or replace the device with 'zpool "
6338                     "replace'.\n"));
6339                 break;
6340
6341         case ZPOOL_STATUS_OFFLINE_DEV:
6342                 (void) printf(gettext("status: One or more devices has "
6343                     "been taken offline by the administrator.\n\tSufficient "
6344                     "replicas exist for the pool to continue functioning in "
6345                     "a\n\tdegraded state.\n"));
6346                 (void) printf(gettext("action: Online the device using "
6347                     "'zpool online' or replace the device with\n\t'zpool "
6348                     "replace'.\n"));
6349                 break;
6350
6351         case ZPOOL_STATUS_REMOVED_DEV:
6352                 (void) printf(gettext("status: One or more devices has "
6353                     "been removed by the administrator.\n\tSufficient "
6354                     "replicas exist for the pool to continue functioning in "
6355                     "a\n\tdegraded state.\n"));
6356                 (void) printf(gettext("action: Online the device using "
6357                     "'zpool online' or replace the device with\n\t'zpool "
6358                     "replace'.\n"));
6359                 break;
6360
6361         case ZPOOL_STATUS_RESILVERING:
6362                 (void) printf(gettext("status: One or more devices is "
6363                     "currently being resilvered.  The pool will\n\tcontinue "
6364                     "to function, possibly in a degraded state.\n"));
6365                 (void) printf(gettext("action: Wait for the resilver to "
6366                     "complete.\n"));
6367                 break;
6368
6369         case ZPOOL_STATUS_CORRUPT_DATA:
6370                 (void) printf(gettext("status: One or more devices has "
6371                     "experienced an error resulting in data\n\tcorruption.  "
6372                     "Applications may be affected.\n"));
6373                 (void) printf(gettext("action: Restore the file in question "
6374                     "if possible.  Otherwise restore the\n\tentire pool from "
6375                     "backup.\n"));
6376                 break;
6377
6378         case ZPOOL_STATUS_CORRUPT_POOL:
6379                 (void) printf(gettext("status: The pool metadata is corrupted "
6380                     "and the pool cannot be opened.\n"));
6381                 zpool_explain_recover(zpool_get_handle(zhp),
6382                     zpool_get_name(zhp), reason, config);
6383                 break;
6384
6385         case ZPOOL_STATUS_VERSION_OLDER:
6386                 (void) printf(gettext("status: The pool is formatted using a "
6387                     "legacy on-disk format.  The pool can\n\tstill be used, "
6388                     "but some features are unavailable.\n"));
6389                 (void) printf(gettext("action: Upgrade the pool using 'zpool "
6390                     "upgrade'.  Once this is done, the\n\tpool will no longer "
6391                     "be accessible on software that does not support\n\t"
6392                     "feature flags.\n"));
6393                 break;
6394
6395         case ZPOOL_STATUS_VERSION_NEWER:
6396                 (void) printf(gettext("status: The pool has been upgraded to a "
6397                     "newer, incompatible on-disk version.\n\tThe pool cannot "
6398                     "be accessed on this system.\n"));
6399                 (void) printf(gettext("action: Access the pool from a system "
6400                     "running more recent software, or\n\trestore the pool from "
6401                     "backup.\n"));
6402                 break;
6403
6404         case ZPOOL_STATUS_FEAT_DISABLED:
6405                 (void) printf(gettext("status: Some supported features are not "
6406                     "enabled on the pool. The pool can\n\tstill be used, but "
6407                     "some features are unavailable.\n"));
6408                 (void) printf(gettext("action: Enable all features using "
6409                     "'zpool upgrade'. Once this is done,\n\tthe pool may no "
6410                     "longer be accessible by software that does not support\n\t"
6411                     "the features. See zpool-features(5) for details.\n"));
6412                 break;
6413
6414         case ZPOOL_STATUS_UNSUP_FEAT_READ:
6415                 (void) printf(gettext("status: The pool cannot be accessed on "
6416                     "this system because it uses the\n\tfollowing feature(s) "
6417                     "not supported on this system:\n"));
6418                 zpool_print_unsup_feat(config);
6419                 (void) printf("\n");
6420                 (void) printf(gettext("action: Access the pool from a system "
6421                     "that supports the required feature(s),\n\tor restore the "
6422                     "pool from backup.\n"));
6423                 break;
6424
6425         case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
6426                 (void) printf(gettext("status: The pool can only be accessed "
6427                     "in read-only mode on this system. It\n\tcannot be "
6428                     "accessed in read-write mode because it uses the "
6429                     "following\n\tfeature(s) not supported on this system:\n"));
6430                 zpool_print_unsup_feat(config);
6431                 (void) printf("\n");
6432                 (void) printf(gettext("action: The pool cannot be accessed in "
6433                     "read-write mode. Import the pool with\n"
6434                     "\t\"-o readonly=on\", access the pool from a system that "
6435                     "supports the\n\trequired feature(s), or restore the "
6436                     "pool from backup.\n"));
6437                 break;
6438
6439         case ZPOOL_STATUS_FAULTED_DEV_R:
6440                 (void) printf(gettext("status: One or more devices are "
6441                     "faulted in response to persistent errors.\n\tSufficient "
6442                     "replicas exist for the pool to continue functioning "
6443                     "in a\n\tdegraded state.\n"));
6444                 (void) printf(gettext("action: Replace the faulted device, "
6445                     "or use 'zpool clear' to mark the device\n\trepaired.\n"));
6446                 break;
6447
6448         case ZPOOL_STATUS_FAULTED_DEV_NR:
6449                 (void) printf(gettext("status: One or more devices are "
6450                     "faulted in response to persistent errors.  There are "
6451                     "insufficient replicas for the pool to\n\tcontinue "
6452                     "functioning.\n"));
6453                 (void) printf(gettext("action: Destroy and re-create the pool "
6454                     "from a backup source.  Manually marking the device\n"
6455                     "\trepaired using 'zpool clear' may allow some data "
6456                     "to be recovered.\n"));
6457                 break;
6458
6459         case ZPOOL_STATUS_IO_FAILURE_WAIT:
6460         case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
6461                 (void) printf(gettext("status: One or more devices are "
6462                     "faulted in response to IO failures.\n"));
6463                 (void) printf(gettext("action: Make sure the affected devices "
6464                     "are connected, then run 'zpool clear'.\n"));
6465                 break;
6466
6467         case ZPOOL_STATUS_BAD_LOG:
6468                 (void) printf(gettext("status: An intent log record "
6469                     "could not be read.\n"
6470                     "\tWaiting for administrator intervention to fix the "
6471                     "faulted pool.\n"));
6472                 (void) printf(gettext("action: Either restore the affected "
6473                     "device(s) and run 'zpool online',\n"
6474                     "\tor ignore the intent log records by running "
6475                     "'zpool clear'.\n"));
6476                 break;
6477
6478         case ZPOOL_STATUS_HOSTID_MISMATCH:
6479                 (void) printf(gettext("status: Mismatch between pool hostid "
6480                     "and system hostid on imported pool.\n\tThis pool was "
6481                     "previously imported into a system with a different "
6482                     "hostid,\n\tand then was verbatim imported into this "
6483                     "system.\n"));
6484                 (void) printf(gettext("action: Export this pool on all systems "
6485                     "on which it is imported.\n"
6486                     "\tThen import it to correct the mismatch.\n"));
6487                 break;
6488
6489         case ZPOOL_STATUS_ERRATA:
6490                 (void) printf(gettext("status: Errata #%d detected.\n"),
6491                     errata);
6492
6493                 switch (errata) {
6494                 case ZPOOL_ERRATA_NONE:
6495                         break;
6496
6497                 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
6498                         (void) printf(gettext("action: To correct the issue "
6499                             "run 'zpool scrub'.\n"));
6500                         break;
6501
6502                 default:
6503                         /*
6504                          * All errata which allow the pool to be imported
6505                          * must contain an action message.
6506                          */
6507                         assert(0);
6508                 }
6509                 break;
6510
6511         default:
6512                 /*
6513                  * The remaining errors can't actually be generated, yet.
6514                  */
6515                 assert(reason == ZPOOL_STATUS_OK);
6516         }
6517
6518         if (msgid != NULL)
6519                 (void) printf(gettext("   see: http://zfsonlinux.org/msg/%s\n"),
6520                     msgid);
6521
6522         if (config != NULL) {
6523                 uint64_t nerr;
6524                 nvlist_t **spares, **l2cache;
6525                 uint_t nspares, nl2cache;
6526                 pool_scan_stat_t *ps = NULL;
6527
6528                 (void) nvlist_lookup_uint64_array(nvroot,
6529                     ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
6530                 print_scan_status(ps);
6531
6532                 cbp->cb_namewidth = max_width(zhp, nvroot, 0, 0,
6533                     cbp->cb_name_flags | VDEV_NAME_TYPE_ID);
6534                 if (cbp->cb_namewidth < 10)
6535                         cbp->cb_namewidth = 10;
6536
6537                 (void) printf(gettext("config:\n\n"));
6538                 (void) printf(gettext("\t%-*s  %-8s %5s %5s %5s"),
6539                     cbp->cb_namewidth, "NAME", "STATE", "READ", "WRITE",
6540                     "CKSUM");
6541
6542                 if (cbp->vcdl != NULL)
6543                         print_cmd_columns(cbp->vcdl, 0);
6544
6545                 printf("\n");
6546                 print_status_config(zhp, cbp, zpool_get_name(zhp), nvroot, 0,
6547                     B_FALSE);
6548
6549                 if (num_logs(nvroot) > 0)
6550                         print_logs(zhp, cbp, nvroot);
6551                 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
6552                     &l2cache, &nl2cache) == 0)
6553                         print_l2cache(zhp, cbp, l2cache, nl2cache);
6554
6555                 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
6556                     &spares, &nspares) == 0)
6557                         print_spares(zhp, cbp, spares, nspares);
6558
6559                 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
6560                     &nerr) == 0) {
6561                         nvlist_t *nverrlist = NULL;
6562
6563                         /*
6564                          * If the approximate error count is small, get a
6565                          * precise count by fetching the entire log and
6566                          * uniquifying the results.
6567                          */
6568                         if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
6569                             zpool_get_errlog(zhp, &nverrlist) == 0) {
6570                                 nvpair_t *elem;
6571
6572                                 elem = NULL;
6573                                 nerr = 0;
6574                                 while ((elem = nvlist_next_nvpair(nverrlist,
6575                                     elem)) != NULL) {
6576                                         nerr++;
6577                                 }
6578                         }
6579                         nvlist_free(nverrlist);
6580
6581                         (void) printf("\n");
6582
6583                         if (nerr == 0)
6584                                 (void) printf(gettext("errors: No known data "
6585                                     "errors\n"));
6586                         else if (!cbp->cb_verbose)
6587                                 (void) printf(gettext("errors: %llu data "
6588                                     "errors, use '-v' for a list\n"),
6589                                     (u_longlong_t)nerr);
6590                         else
6591                                 print_error_log(zhp);
6592                 }
6593
6594                 if (cbp->cb_dedup_stats)
6595                         print_dedup_stats(config);
6596         } else {
6597                 (void) printf(gettext("config: The configuration cannot be "
6598                     "determined.\n"));
6599         }
6600
6601         return (0);
6602 }
6603
6604 /*
6605  * zpool status [-c [script1,script2,...]] [-gLPvx] [-T d|u] [pool] ...
6606  *              [interval [count]]
6607  *
6608  *      -c CMD  For each vdev, run command CMD
6609  *      -g      Display guid for individual vdev name.
6610  *      -L      Follow links when resolving vdev path name.
6611  *      -P      Display full path for vdev name.
6612  *      -v      Display complete error logs
6613  *      -x      Display only pools with potential problems
6614  *      -D      Display dedup status (undocumented)
6615  *      -T      Display a timestamp in date(1) or Unix format
6616  *
6617  * Describes the health status of all pools or some subset.
6618  */
6619 int
6620 zpool_do_status(int argc, char **argv)
6621 {
6622         int c;
6623         int ret;
6624         float interval = 0;
6625         unsigned long count = 0;
6626         status_cbdata_t cb = { 0 };
6627         char *cmd = NULL;
6628
6629         /* check options */
6630         while ((c = getopt(argc, argv, "c:gLPvxDT:")) != -1) {
6631                 switch (c) {
6632                 case 'c':
6633                         if (cmd != NULL) {
6634                                 fprintf(stderr,
6635                                     gettext("Can't set -c flag twice\n"));
6636                                 exit(1);
6637                         }
6638
6639                         if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL &&
6640                             !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
6641                                 fprintf(stderr, gettext(
6642                                     "Can't run -c, disabled by "
6643                                     "ZPOOL_SCRIPTS_ENABLED.\n"));
6644                                 exit(1);
6645                         }
6646
6647                         if ((getuid() <= 0 || geteuid() <= 0) &&
6648                             !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
6649                                 fprintf(stderr, gettext(
6650                                     "Can't run -c with root privileges "
6651                                     "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
6652                                 exit(1);
6653                         }
6654                         cmd = optarg;
6655                         break;
6656                 case 'g':
6657                         cb.cb_name_flags |= VDEV_NAME_GUID;
6658                         break;
6659                 case 'L':
6660                         cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
6661                         break;
6662                 case 'P':
6663                         cb.cb_name_flags |= VDEV_NAME_PATH;
6664                         break;
6665                 case 'v':
6666                         cb.cb_verbose = B_TRUE;
6667                         break;
6668                 case 'x':
6669                         cb.cb_explain = B_TRUE;
6670                         break;
6671                 case 'D':
6672                         cb.cb_dedup_stats = B_TRUE;
6673                         break;
6674                 case 'T':
6675                         get_timestamp_arg(*optarg);
6676                         break;
6677                 case '?':
6678                         if (optopt == 'c') {
6679                                 print_zpool_script_list("status");
6680                                 exit(0);
6681                         } else {
6682                                 fprintf(stderr,
6683                                     gettext("invalid option '%c'\n"), optopt);
6684                         }
6685                         usage(B_FALSE);
6686                 }
6687         }
6688
6689         argc -= optind;
6690         argv += optind;
6691
6692         get_interval_count(&argc, argv, &interval, &count);
6693
6694         if (argc == 0)
6695                 cb.cb_allpools = B_TRUE;
6696
6697         cb.cb_first = B_TRUE;
6698         cb.cb_print_status = B_TRUE;
6699
6700         for (;;) {
6701                 if (timestamp_fmt != NODATE)
6702                         print_timestamp(timestamp_fmt);
6703
6704                 if (cmd != NULL)
6705                         cb.vcdl = all_pools_for_each_vdev_run(argc, argv, cmd,
6706                             NULL, NULL, 0, 0);
6707
6708                 ret = for_each_pool(argc, argv, B_TRUE, NULL,
6709                     status_callback, &cb);
6710
6711                 if (cb.vcdl != NULL)
6712                         free_vdev_cmd_data_list(cb.vcdl);
6713
6714                 if (argc == 0 && cb.cb_count == 0)
6715                         (void) fprintf(stderr, gettext("no pools available\n"));
6716                 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
6717                         (void) printf(gettext("all pools are healthy\n"));
6718
6719                 if (ret != 0)
6720                         return (ret);
6721
6722                 if (interval == 0)
6723                         break;
6724
6725                 if (count != 0 && --count == 0)
6726                         break;
6727
6728                 (void) fsleep(interval);
6729         }
6730
6731         return (0);
6732 }
6733
6734 typedef struct upgrade_cbdata {
6735         int     cb_first;
6736         int     cb_argc;
6737         uint64_t cb_version;
6738         char    **cb_argv;
6739 } upgrade_cbdata_t;
6740
6741 static int
6742 check_unsupp_fs(zfs_handle_t *zhp, void *unsupp_fs)
6743 {
6744         int zfs_version = (int)zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
6745         int *count = (int *)unsupp_fs;
6746
6747         if (zfs_version > ZPL_VERSION) {
6748                 (void) printf(gettext("%s (v%d) is not supported by this "
6749                     "implementation of ZFS.\n"),
6750                     zfs_get_name(zhp), zfs_version);
6751                 (*count)++;
6752         }
6753
6754         zfs_iter_filesystems(zhp, check_unsupp_fs, unsupp_fs);
6755
6756         zfs_close(zhp);
6757
6758         return (0);
6759 }
6760
6761 static int
6762 upgrade_version(zpool_handle_t *zhp, uint64_t version)
6763 {
6764         int ret;
6765         nvlist_t *config;
6766         uint64_t oldversion;
6767         int unsupp_fs = 0;
6768
6769         config = zpool_get_config(zhp, NULL);
6770         verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
6771             &oldversion) == 0);
6772
6773         assert(SPA_VERSION_IS_SUPPORTED(oldversion));
6774         assert(oldversion < version);
6775
6776         ret = zfs_iter_root(zpool_get_handle(zhp), check_unsupp_fs, &unsupp_fs);
6777         if (ret != 0)
6778                 return (ret);
6779
6780         if (unsupp_fs) {
6781                 (void) fprintf(stderr, gettext("Upgrade not performed due "
6782                     "to %d unsupported filesystems (max v%d).\n"),
6783                     unsupp_fs, (int)ZPL_VERSION);
6784                 return (1);
6785         }
6786
6787         ret = zpool_upgrade(zhp, version);
6788         if (ret != 0)
6789                 return (ret);
6790
6791         if (version >= SPA_VERSION_FEATURES) {
6792                 (void) printf(gettext("Successfully upgraded "
6793                     "'%s' from version %llu to feature flags.\n"),
6794                     zpool_get_name(zhp), (u_longlong_t)oldversion);
6795         } else {
6796                 (void) printf(gettext("Successfully upgraded "
6797                     "'%s' from version %llu to version %llu.\n"),
6798                     zpool_get_name(zhp), (u_longlong_t)oldversion,
6799                     (u_longlong_t)version);
6800         }
6801
6802         return (0);
6803 }
6804
6805 static int
6806 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
6807 {
6808         int i, ret, count;
6809         boolean_t firstff = B_TRUE;
6810         nvlist_t *enabled = zpool_get_features(zhp);
6811
6812         count = 0;
6813         for (i = 0; i < SPA_FEATURES; i++) {
6814                 const char *fname = spa_feature_table[i].fi_uname;
6815                 const char *fguid = spa_feature_table[i].fi_guid;
6816                 if (!nvlist_exists(enabled, fguid)) {
6817                         char *propname;
6818                         verify(-1 != asprintf(&propname, "feature@%s", fname));
6819                         ret = zpool_set_prop(zhp, propname,
6820                             ZFS_FEATURE_ENABLED);
6821                         if (ret != 0) {
6822                                 free(propname);
6823                                 return (ret);
6824                         }
6825                         count++;
6826
6827                         if (firstff) {
6828                                 (void) printf(gettext("Enabled the "
6829                                     "following features on '%s':\n"),
6830                                     zpool_get_name(zhp));
6831                                 firstff = B_FALSE;
6832                         }
6833                         (void) printf(gettext("  %s\n"), fname);
6834                         free(propname);
6835                 }
6836         }
6837
6838         if (countp != NULL)
6839                 *countp = count;
6840         return (0);
6841 }
6842
6843 static int
6844 upgrade_cb(zpool_handle_t *zhp, void *arg)
6845 {
6846         upgrade_cbdata_t *cbp = arg;
6847         nvlist_t *config;
6848         uint64_t version;
6849         boolean_t printnl = B_FALSE;
6850         int ret;
6851
6852         config = zpool_get_config(zhp, NULL);
6853         verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
6854             &version) == 0);
6855
6856         assert(SPA_VERSION_IS_SUPPORTED(version));
6857
6858         if (version < cbp->cb_version) {
6859                 cbp->cb_first = B_FALSE;
6860                 ret = upgrade_version(zhp, cbp->cb_version);
6861                 if (ret != 0)
6862                         return (ret);
6863                 printnl = B_TRUE;
6864
6865                 /*
6866                  * If they did "zpool upgrade -a", then we could
6867                  * be doing ioctls to different pools.  We need
6868                  * to log this history once to each pool, and bypass
6869                  * the normal history logging that happens in main().
6870                  */
6871                 (void) zpool_log_history(g_zfs, history_str);
6872                 log_history = B_FALSE;
6873         }
6874
6875         if (cbp->cb_version >= SPA_VERSION_FEATURES) {
6876                 int count;
6877                 ret = upgrade_enable_all(zhp, &count);
6878                 if (ret != 0)
6879                         return (ret);
6880
6881                 if (count > 0) {
6882                         cbp->cb_first = B_FALSE;
6883                         printnl = B_TRUE;
6884                 }
6885         }
6886
6887         if (printnl) {
6888                 (void) printf(gettext("\n"));
6889         }
6890
6891         return (0);
6892 }
6893
6894 static int
6895 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
6896 {
6897         upgrade_cbdata_t *cbp = arg;
6898         nvlist_t *config;
6899         uint64_t version;
6900
6901         config = zpool_get_config(zhp, NULL);
6902         verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
6903             &version) == 0);
6904
6905         assert(SPA_VERSION_IS_SUPPORTED(version));
6906
6907         if (version < SPA_VERSION_FEATURES) {
6908                 if (cbp->cb_first) {
6909                         (void) printf(gettext("The following pools are "
6910                             "formatted with legacy version numbers and can\n"
6911                             "be upgraded to use feature flags.  After "
6912                             "being upgraded, these pools\nwill no "
6913                             "longer be accessible by software that does not "
6914                             "support feature\nflags.\n\n"));
6915                         (void) printf(gettext("VER  POOL\n"));
6916                         (void) printf(gettext("---  ------------\n"));
6917                         cbp->cb_first = B_FALSE;
6918                 }
6919
6920                 (void) printf("%2llu   %s\n", (u_longlong_t)version,
6921                     zpool_get_name(zhp));
6922         }
6923
6924         return (0);
6925 }
6926
6927 static int
6928 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
6929 {
6930         upgrade_cbdata_t *cbp = arg;
6931         nvlist_t *config;
6932         uint64_t version;
6933
6934         config = zpool_get_config(zhp, NULL);
6935         verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
6936             &version) == 0);
6937
6938         if (version >= SPA_VERSION_FEATURES) {
6939                 int i;
6940                 boolean_t poolfirst = B_TRUE;
6941                 nvlist_t *enabled = zpool_get_features(zhp);
6942
6943                 for (i = 0; i < SPA_FEATURES; i++) {
6944                         const char *fguid = spa_feature_table[i].fi_guid;
6945                         const char *fname = spa_feature_table[i].fi_uname;
6946                         if (!nvlist_exists(enabled, fguid)) {
6947                                 if (cbp->cb_first) {
6948                                         (void) printf(gettext("\nSome "
6949                                             "supported features are not "
6950                                             "enabled on the following pools. "
6951                                             "Once a\nfeature is enabled the "
6952                                             "pool may become incompatible with "
6953                                             "software\nthat does not support "
6954                                             "the feature. See "
6955                                             "zpool-features(5) for "
6956                                             "details.\n\n"));
6957                                         (void) printf(gettext("POOL  "
6958                                             "FEATURE\n"));
6959                                         (void) printf(gettext("------"
6960                                             "---------\n"));
6961                                         cbp->cb_first = B_FALSE;
6962                                 }
6963
6964                                 if (poolfirst) {
6965                                         (void) printf(gettext("%s\n"),
6966                                             zpool_get_name(zhp));
6967                                         poolfirst = B_FALSE;
6968                                 }
6969
6970                                 (void) printf(gettext("      %s\n"), fname);
6971                         }
6972                         /*
6973                          * If they did "zpool upgrade -a", then we could
6974                          * be doing ioctls to different pools.  We need
6975                          * to log this history once to each pool, and bypass
6976                          * the normal history logging that happens in main().
6977                          */
6978                         (void) zpool_log_history(g_zfs, history_str);
6979                         log_history = B_FALSE;
6980                 }
6981         }
6982
6983         return (0);
6984 }
6985
6986 /* ARGSUSED */
6987 static int
6988 upgrade_one(zpool_handle_t *zhp, void *data)
6989 {
6990         boolean_t printnl = B_FALSE;
6991         upgrade_cbdata_t *cbp = data;
6992         uint64_t cur_version;
6993         int ret;
6994
6995         if (strcmp("log", zpool_get_name(zhp)) == 0) {
6996                 (void) fprintf(stderr, gettext("'log' is now a reserved word\n"
6997                     "Pool 'log' must be renamed using export and import"
6998                     " to upgrade.\n"));
6999                 return (1);
7000         }
7001
7002         cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
7003         if (cur_version > cbp->cb_version) {
7004                 (void) printf(gettext("Pool '%s' is already formatted "
7005                     "using more current version '%llu'.\n\n"),
7006                     zpool_get_name(zhp), (u_longlong_t)cur_version);
7007                 return (0);
7008         }
7009
7010         if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
7011                 (void) printf(gettext("Pool '%s' is already formatted "
7012                     "using version %llu.\n\n"), zpool_get_name(zhp),
7013                     (u_longlong_t)cbp->cb_version);
7014                 return (0);
7015         }
7016
7017         if (cur_version != cbp->cb_version) {
7018                 printnl = B_TRUE;
7019                 ret = upgrade_version(zhp, cbp->cb_version);
7020                 if (ret != 0)
7021                         return (ret);
7022         }
7023
7024         if (cbp->cb_version >= SPA_VERSION_FEATURES) {
7025                 int count = 0;
7026                 ret = upgrade_enable_all(zhp, &count);
7027                 if (ret != 0)
7028                         return (ret);
7029
7030                 if (count != 0) {
7031                         printnl = B_TRUE;
7032                 } else if (cur_version == SPA_VERSION) {
7033                         (void) printf(gettext("Pool '%s' already has all "
7034                             "supported features enabled.\n"),
7035                             zpool_get_name(zhp));
7036                 }
7037         }
7038
7039         if (printnl) {
7040                 (void) printf(gettext("\n"));
7041         }
7042
7043         return (0);
7044 }
7045
7046 /*
7047  * zpool upgrade
7048  * zpool upgrade -v
7049  * zpool upgrade [-V version] <-a | pool ...>
7050  *
7051  * With no arguments, display downrev'd ZFS pool available for upgrade.
7052  * Individual pools can be upgraded by specifying the pool, and '-a' will
7053  * upgrade all pools.
7054  */
7055 int
7056 zpool_do_upgrade(int argc, char **argv)
7057 {
7058         int c;
7059         upgrade_cbdata_t cb = { 0 };
7060         int ret = 0;
7061         boolean_t showversions = B_FALSE;
7062         boolean_t upgradeall = B_FALSE;
7063         char *end;
7064
7065
7066         /* check options */
7067         while ((c = getopt(argc, argv, ":avV:")) != -1) {
7068                 switch (c) {
7069                 case 'a':
7070                         upgradeall = B_TRUE;
7071                         break;
7072                 case 'v':
7073                         showversions = B_TRUE;
7074                         break;
7075                 case 'V':
7076                         cb.cb_version = strtoll(optarg, &end, 10);
7077                         if (*end != '\0' ||
7078                             !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
7079                                 (void) fprintf(stderr,
7080                                     gettext("invalid version '%s'\n"), optarg);
7081                                 usage(B_FALSE);
7082                         }
7083                         break;
7084                 case ':':
7085                         (void) fprintf(stderr, gettext("missing argument for "
7086                             "'%c' option\n"), optopt);
7087                         usage(B_FALSE);
7088                         break;
7089                 case '?':
7090                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7091                             optopt);
7092                         usage(B_FALSE);
7093                 }
7094         }
7095
7096         cb.cb_argc = argc;
7097         cb.cb_argv = argv;
7098         argc -= optind;
7099         argv += optind;
7100
7101         if (cb.cb_version == 0) {
7102                 cb.cb_version = SPA_VERSION;
7103         } else if (!upgradeall && argc == 0) {
7104                 (void) fprintf(stderr, gettext("-V option is "
7105                     "incompatible with other arguments\n"));
7106                 usage(B_FALSE);
7107         }
7108
7109         if (showversions) {
7110                 if (upgradeall || argc != 0) {
7111                         (void) fprintf(stderr, gettext("-v option is "
7112                             "incompatible with other arguments\n"));
7113                         usage(B_FALSE);
7114                 }
7115         } else if (upgradeall) {
7116                 if (argc != 0) {
7117                         (void) fprintf(stderr, gettext("-a option should not "
7118                             "be used along with a pool name\n"));
7119                         usage(B_FALSE);
7120                 }
7121         }
7122
7123         (void) printf(gettext("This system supports ZFS pool feature "
7124             "flags.\n\n"));
7125         if (showversions) {
7126                 int i;
7127
7128                 (void) printf(gettext("The following features are "
7129                     "supported:\n\n"));
7130                 (void) printf(gettext("FEAT DESCRIPTION\n"));
7131                 (void) printf("----------------------------------------------"
7132                     "---------------\n");
7133                 for (i = 0; i < SPA_FEATURES; i++) {
7134                         zfeature_info_t *fi = &spa_feature_table[i];
7135                         const char *ro =
7136                             (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
7137                             " (read-only compatible)" : "";
7138
7139                         (void) printf("%-37s%s\n", fi->fi_uname, ro);
7140                         (void) printf("     %s\n", fi->fi_desc);
7141                 }
7142                 (void) printf("\n");
7143
7144                 (void) printf(gettext("The following legacy versions are also "
7145                     "supported:\n\n"));
7146                 (void) printf(gettext("VER  DESCRIPTION\n"));
7147                 (void) printf("---  -----------------------------------------"
7148                     "---------------\n");
7149                 (void) printf(gettext(" 1   Initial ZFS version\n"));
7150                 (void) printf(gettext(" 2   Ditto blocks "
7151                     "(replicated metadata)\n"));
7152                 (void) printf(gettext(" 3   Hot spares and double parity "
7153                     "RAID-Z\n"));
7154                 (void) printf(gettext(" 4   zpool history\n"));
7155                 (void) printf(gettext(" 5   Compression using the gzip "
7156                     "algorithm\n"));
7157                 (void) printf(gettext(" 6   bootfs pool property\n"));
7158                 (void) printf(gettext(" 7   Separate intent log devices\n"));
7159                 (void) printf(gettext(" 8   Delegated administration\n"));
7160                 (void) printf(gettext(" 9   refquota and refreservation "
7161                     "properties\n"));
7162                 (void) printf(gettext(" 10  Cache devices\n"));
7163                 (void) printf(gettext(" 11  Improved scrub performance\n"));
7164                 (void) printf(gettext(" 12  Snapshot properties\n"));
7165                 (void) printf(gettext(" 13  snapused property\n"));
7166                 (void) printf(gettext(" 14  passthrough-x aclinherit\n"));
7167                 (void) printf(gettext(" 15  user/group space accounting\n"));
7168                 (void) printf(gettext(" 16  stmf property support\n"));
7169                 (void) printf(gettext(" 17  Triple-parity RAID-Z\n"));
7170                 (void) printf(gettext(" 18  Snapshot user holds\n"));
7171                 (void) printf(gettext(" 19  Log device removal\n"));
7172                 (void) printf(gettext(" 20  Compression using zle "
7173                     "(zero-length encoding)\n"));
7174                 (void) printf(gettext(" 21  Deduplication\n"));
7175                 (void) printf(gettext(" 22  Received properties\n"));
7176                 (void) printf(gettext(" 23  Slim ZIL\n"));
7177                 (void) printf(gettext(" 24  System attributes\n"));
7178                 (void) printf(gettext(" 25  Improved scrub stats\n"));
7179                 (void) printf(gettext(" 26  Improved snapshot deletion "
7180                     "performance\n"));
7181                 (void) printf(gettext(" 27  Improved snapshot creation "
7182                     "performance\n"));
7183                 (void) printf(gettext(" 28  Multiple vdev replacements\n"));
7184                 (void) printf(gettext("\nFor more information on a particular "
7185                     "version, including supported releases,\n"));
7186                 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
7187         } else if (argc == 0 && upgradeall) {
7188                 cb.cb_first = B_TRUE;
7189                 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
7190                 if (ret == 0 && cb.cb_first) {
7191                         if (cb.cb_version == SPA_VERSION) {
7192                                 (void) printf(gettext("All pools are already "
7193                                     "formatted using feature flags.\n\n"));
7194                                 (void) printf(gettext("Every feature flags "
7195                                     "pool already has all supported features "
7196                                     "enabled.\n"));
7197                         } else {
7198                                 (void) printf(gettext("All pools are already "
7199                                     "formatted with version %llu or higher.\n"),
7200                                     (u_longlong_t)cb.cb_version);
7201                         }
7202                 }
7203         } else if (argc == 0) {
7204                 cb.cb_first = B_TRUE;
7205                 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
7206                 assert(ret == 0);
7207
7208                 if (cb.cb_first) {
7209                         (void) printf(gettext("All pools are formatted "
7210                             "using feature flags.\n\n"));
7211                 } else {
7212                         (void) printf(gettext("\nUse 'zpool upgrade -v' "
7213                             "for a list of available legacy versions.\n"));
7214                 }
7215
7216                 cb.cb_first = B_TRUE;
7217                 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
7218                 assert(ret == 0);
7219
7220                 if (cb.cb_first) {
7221                         (void) printf(gettext("Every feature flags pool has "
7222                             "all supported features enabled.\n"));
7223                 } else {
7224                         (void) printf(gettext("\n"));
7225                 }
7226         } else {
7227                 ret = for_each_pool(argc, argv, B_FALSE, NULL,
7228                     upgrade_one, &cb);
7229         }
7230
7231         return (ret);
7232 }
7233
7234 typedef struct hist_cbdata {
7235         boolean_t first;
7236         boolean_t longfmt;
7237         boolean_t internal;
7238 } hist_cbdata_t;
7239
7240 /*
7241  * Print out the command history for a specific pool.
7242  */
7243 static int
7244 get_history_one(zpool_handle_t *zhp, void *data)
7245 {
7246         nvlist_t *nvhis;
7247         nvlist_t **records;
7248         uint_t numrecords;
7249         int ret, i;
7250         hist_cbdata_t *cb = (hist_cbdata_t *)data;
7251
7252         cb->first = B_FALSE;
7253
7254         (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
7255
7256         if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
7257                 return (ret);
7258
7259         verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
7260             &records, &numrecords) == 0);
7261         for (i = 0; i < numrecords; i++) {
7262                 nvlist_t *rec = records[i];
7263                 char tbuf[30] = "";
7264
7265                 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
7266                         time_t tsec;
7267                         struct tm t;
7268
7269                         tsec = fnvlist_lookup_uint64(records[i],
7270                             ZPOOL_HIST_TIME);
7271                         (void) localtime_r(&tsec, &t);
7272                         (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
7273                 }
7274
7275                 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
7276                         (void) printf("%s %s", tbuf,
7277                             fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
7278                 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
7279                         int ievent =
7280                             fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
7281                         if (!cb->internal)
7282                                 continue;
7283                         if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
7284                                 (void) printf("%s unrecognized record:\n",
7285                                     tbuf);
7286                                 dump_nvlist(rec, 4);
7287                                 continue;
7288                         }
7289                         (void) printf("%s [internal %s txg:%lld] %s", tbuf,
7290                             zfs_history_event_names[ievent],
7291                             (longlong_t)fnvlist_lookup_uint64(
7292                             rec, ZPOOL_HIST_TXG),
7293                             fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
7294                 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
7295                         if (!cb->internal)
7296                                 continue;
7297                         (void) printf("%s [txg:%lld] %s", tbuf,
7298                             (longlong_t)fnvlist_lookup_uint64(
7299                             rec, ZPOOL_HIST_TXG),
7300                             fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
7301                         if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
7302                                 (void) printf(" %s (%llu)",
7303                                     fnvlist_lookup_string(rec,
7304                                     ZPOOL_HIST_DSNAME),
7305                                     (u_longlong_t)fnvlist_lookup_uint64(rec,
7306                                     ZPOOL_HIST_DSID));
7307                         }
7308                         (void) printf(" %s", fnvlist_lookup_string(rec,
7309                             ZPOOL_HIST_INT_STR));
7310                 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
7311                         if (!cb->internal)
7312                                 continue;
7313                         (void) printf("%s ioctl %s\n", tbuf,
7314                             fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
7315                         if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
7316                                 (void) printf("    input:\n");
7317                                 dump_nvlist(fnvlist_lookup_nvlist(rec,
7318                                     ZPOOL_HIST_INPUT_NVL), 8);
7319                         }
7320                         if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
7321                                 (void) printf("    output:\n");
7322                                 dump_nvlist(fnvlist_lookup_nvlist(rec,
7323                                     ZPOOL_HIST_OUTPUT_NVL), 8);
7324                         }
7325                 } else {
7326                         if (!cb->internal)
7327                                 continue;
7328                         (void) printf("%s unrecognized record:\n", tbuf);
7329                         dump_nvlist(rec, 4);
7330                 }
7331
7332                 if (!cb->longfmt) {
7333                         (void) printf("\n");
7334                         continue;
7335                 }
7336                 (void) printf(" [");
7337                 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
7338                         uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
7339                         struct passwd *pwd = getpwuid(who);
7340                         (void) printf("user %d ", (int)who);
7341                         if (pwd != NULL)
7342                                 (void) printf("(%s) ", pwd->pw_name);
7343                 }
7344                 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
7345                         (void) printf("on %s",
7346                             fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
7347                 }
7348                 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
7349                         (void) printf(":%s",
7350                             fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
7351                 }
7352
7353                 (void) printf("]");
7354                 (void) printf("\n");
7355         }
7356         (void) printf("\n");
7357         nvlist_free(nvhis);
7358
7359         return (ret);
7360 }
7361
7362 /*
7363  * zpool history <pool>
7364  *
7365  * Displays the history of commands that modified pools.
7366  */
7367 int
7368 zpool_do_history(int argc, char **argv)
7369 {
7370         hist_cbdata_t cbdata = { 0 };
7371         int ret;
7372         int c;
7373
7374         cbdata.first = B_TRUE;
7375         /* check options */
7376         while ((c = getopt(argc, argv, "li")) != -1) {
7377                 switch (c) {
7378                 case 'l':
7379                         cbdata.longfmt = B_TRUE;
7380                         break;
7381                 case 'i':
7382                         cbdata.internal = B_TRUE;
7383                         break;
7384                 case '?':
7385                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7386                             optopt);
7387                         usage(B_FALSE);
7388                 }
7389         }
7390         argc -= optind;
7391         argv += optind;
7392
7393         ret = for_each_pool(argc, argv, B_FALSE,  NULL, get_history_one,
7394             &cbdata);
7395
7396         if (argc == 0 && cbdata.first == B_TRUE) {
7397                 (void) fprintf(stderr, gettext("no pools available\n"));
7398                 return (0);
7399         }
7400
7401         return (ret);
7402 }
7403
7404 typedef struct ev_opts {
7405         int verbose;
7406         int scripted;
7407         int follow;
7408         int clear;
7409         char poolname[ZFS_MAX_DATASET_NAME_LEN];
7410 } ev_opts_t;
7411
7412 static void
7413 zpool_do_events_short(nvlist_t *nvl, ev_opts_t *opts)
7414 {
7415         char ctime_str[26], str[32], *ptr;
7416         int64_t *tv;
7417         uint_t n;
7418
7419         verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
7420         memset(str, ' ', 32);
7421         (void) ctime_r((const time_t *)&tv[0], ctime_str);
7422         (void) strncpy(str, ctime_str+4,  6);           /* 'Jun 30' */
7423         (void) strncpy(str+7, ctime_str+20, 4);         /* '1993' */
7424         (void) strncpy(str+12, ctime_str+11, 8);        /* '21:49:08' */
7425         (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]); /* '.123456789' */
7426         if (opts->scripted)
7427                 (void) printf(gettext("%s\t"), str);
7428         else
7429                 (void) printf(gettext("%s "), str);
7430
7431         verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
7432         (void) printf(gettext("%s\n"), ptr);
7433 }
7434
7435 static void
7436 zpool_do_events_nvprint(nvlist_t *nvl, int depth)
7437 {
7438         nvpair_t *nvp;
7439
7440         for (nvp = nvlist_next_nvpair(nvl, NULL);
7441             nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
7442
7443                 data_type_t type = nvpair_type(nvp);
7444                 const char *name = nvpair_name(nvp);
7445
7446                 boolean_t b;
7447                 uint8_t i8;
7448                 uint16_t i16;
7449                 uint32_t i32;
7450                 uint64_t i64;
7451                 char *str;
7452                 nvlist_t *cnv;
7453
7454                 printf(gettext("%*s%s = "), depth, "", name);
7455
7456                 switch (type) {
7457                 case DATA_TYPE_BOOLEAN:
7458                         printf(gettext("%s"), "1");
7459                         break;
7460
7461                 case DATA_TYPE_BOOLEAN_VALUE:
7462                         (void) nvpair_value_boolean_value(nvp, &b);
7463                         printf(gettext("%s"), b ? "1" : "0");
7464                         break;
7465
7466                 case DATA_TYPE_BYTE:
7467                         (void) nvpair_value_byte(nvp, &i8);
7468                         printf(gettext("0x%x"), i8);
7469                         break;
7470
7471                 case DATA_TYPE_INT8:
7472                         (void) nvpair_value_int8(nvp, (void *)&i8);
7473                         printf(gettext("0x%x"), i8);
7474                         break;
7475
7476                 case DATA_TYPE_UINT8:
7477                         (void) nvpair_value_uint8(nvp, &i8);
7478                         printf(gettext("0x%x"), i8);
7479                         break;
7480
7481                 case DATA_TYPE_INT16:
7482                         (void) nvpair_value_int16(nvp, (void *)&i16);
7483                         printf(gettext("0x%x"), i16);
7484                         break;
7485
7486                 case DATA_TYPE_UINT16:
7487                         (void) nvpair_value_uint16(nvp, &i16);
7488                         printf(gettext("0x%x"), i16);
7489                         break;
7490
7491                 case DATA_TYPE_INT32:
7492                         (void) nvpair_value_int32(nvp, (void *)&i32);
7493                         printf(gettext("0x%x"), i32);
7494                         break;
7495
7496                 case DATA_TYPE_UINT32:
7497                         (void) nvpair_value_uint32(nvp, &i32);
7498                         printf(gettext("0x%x"), i32);
7499                         break;
7500
7501                 case DATA_TYPE_INT64:
7502                         (void) nvpair_value_int64(nvp, (void *)&i64);
7503                         printf(gettext("0x%llx"), (u_longlong_t)i64);
7504                         break;
7505
7506                 case DATA_TYPE_UINT64:
7507                         (void) nvpair_value_uint64(nvp, &i64);
7508                         /*
7509                          * translate vdev state values to readable
7510                          * strings to aide zpool events consumers
7511                          */
7512                         if (strcmp(name,
7513                             FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE) == 0 ||
7514                             strcmp(name,
7515                             FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE) == 0) {
7516                                 printf(gettext("\"%s\" (0x%llx)"),
7517                                     zpool_state_to_name(i64, VDEV_AUX_NONE),
7518                                     (u_longlong_t)i64);
7519                         } else {
7520                                 printf(gettext("0x%llx"), (u_longlong_t)i64);
7521                         }
7522                         break;
7523
7524                 case DATA_TYPE_HRTIME:
7525                         (void) nvpair_value_hrtime(nvp, (void *)&i64);
7526                         printf(gettext("0x%llx"), (u_longlong_t)i64);
7527                         break;
7528
7529                 case DATA_TYPE_STRING:
7530                         (void) nvpair_value_string(nvp, &str);
7531                         printf(gettext("\"%s\""), str ? str : "<NULL>");
7532                         break;
7533
7534                 case DATA_TYPE_NVLIST:
7535                         printf(gettext("(embedded nvlist)\n"));
7536                         (void) nvpair_value_nvlist(nvp, &cnv);
7537                         zpool_do_events_nvprint(cnv, depth + 8);
7538                         printf(gettext("%*s(end %s)"), depth, "", name);
7539                         break;
7540
7541                 case DATA_TYPE_NVLIST_ARRAY: {
7542                         nvlist_t **val;
7543                         uint_t i, nelem;
7544
7545                         (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
7546                         printf(gettext("(%d embedded nvlists)\n"), nelem);
7547                         for (i = 0; i < nelem; i++) {
7548                                 printf(gettext("%*s%s[%d] = %s\n"),
7549                                     depth, "", name, i, "(embedded nvlist)");
7550                                 zpool_do_events_nvprint(val[i], depth + 8);
7551                                 printf(gettext("%*s(end %s[%i])\n"),
7552                                     depth, "", name, i);
7553                         }
7554                         printf(gettext("%*s(end %s)\n"), depth, "", name);
7555                         }
7556                         break;
7557
7558                 case DATA_TYPE_INT8_ARRAY: {
7559                         int8_t *val;
7560                         uint_t i, nelem;
7561
7562                         (void) nvpair_value_int8_array(nvp, &val, &nelem);
7563                         for (i = 0; i < nelem; i++)
7564                                 printf(gettext("0x%x "), val[i]);
7565
7566                         break;
7567                         }
7568
7569                 case DATA_TYPE_UINT8_ARRAY: {
7570                         uint8_t *val;
7571                         uint_t i, nelem;
7572
7573                         (void) nvpair_value_uint8_array(nvp, &val, &nelem);
7574                         for (i = 0; i < nelem; i++)
7575                                 printf(gettext("0x%x "), val[i]);
7576
7577                         break;
7578                         }
7579
7580                 case DATA_TYPE_INT16_ARRAY: {
7581                         int16_t *val;
7582                         uint_t i, nelem;
7583
7584                         (void) nvpair_value_int16_array(nvp, &val, &nelem);
7585                         for (i = 0; i < nelem; i++)
7586                                 printf(gettext("0x%x "), val[i]);
7587
7588                         break;
7589                         }
7590
7591                 case DATA_TYPE_UINT16_ARRAY: {
7592                         uint16_t *val;
7593                         uint_t i, nelem;
7594
7595                         (void) nvpair_value_uint16_array(nvp, &val, &nelem);
7596                         for (i = 0; i < nelem; i++)
7597                                 printf(gettext("0x%x "), val[i]);
7598
7599                         break;
7600                         }
7601
7602                 case DATA_TYPE_INT32_ARRAY: {
7603                         int32_t *val;
7604                         uint_t i, nelem;
7605
7606                         (void) nvpair_value_int32_array(nvp, &val, &nelem);
7607                         for (i = 0; i < nelem; i++)
7608                                 printf(gettext("0x%x "), val[i]);
7609
7610                         break;
7611                         }
7612
7613                 case DATA_TYPE_UINT32_ARRAY: {
7614                         uint32_t *val;
7615                         uint_t i, nelem;
7616
7617                         (void) nvpair_value_uint32_array(nvp, &val, &nelem);
7618                         for (i = 0; i < nelem; i++)
7619                                 printf(gettext("0x%x "), val[i]);
7620
7621                         break;
7622                         }
7623
7624                 case DATA_TYPE_INT64_ARRAY: {
7625                         int64_t *val;
7626                         uint_t i, nelem;
7627
7628                         (void) nvpair_value_int64_array(nvp, &val, &nelem);
7629                         for (i = 0; i < nelem; i++)
7630                                 printf(gettext("0x%llx "),
7631                                     (u_longlong_t)val[i]);
7632
7633                         break;
7634                         }
7635
7636                 case DATA_TYPE_UINT64_ARRAY: {
7637                         uint64_t *val;
7638                         uint_t i, nelem;
7639
7640                         (void) nvpair_value_uint64_array(nvp, &val, &nelem);
7641                         for (i = 0; i < nelem; i++)
7642                                 printf(gettext("0x%llx "),
7643                                     (u_longlong_t)val[i]);
7644
7645                         break;
7646                         }
7647
7648                 case DATA_TYPE_STRING_ARRAY: {
7649                         char **str;
7650                         uint_t i, nelem;
7651
7652                         (void) nvpair_value_string_array(nvp, &str, &nelem);
7653                         for (i = 0; i < nelem; i++)
7654                                 printf(gettext("\"%s\" "),
7655                                     str[i] ? str[i] : "<NULL>");
7656
7657                         break;
7658                         }
7659
7660                 case DATA_TYPE_BOOLEAN_ARRAY:
7661                 case DATA_TYPE_BYTE_ARRAY:
7662                 case DATA_TYPE_DOUBLE:
7663                 case DATA_TYPE_UNKNOWN:
7664                         printf(gettext("<unknown>"));
7665                         break;
7666                 }
7667
7668                 printf(gettext("\n"));
7669         }
7670 }
7671
7672 static int
7673 zpool_do_events_next(ev_opts_t *opts)
7674 {
7675         nvlist_t *nvl;
7676         int zevent_fd, ret, dropped;
7677         char *pool;
7678
7679         zevent_fd = open(ZFS_DEV, O_RDWR);
7680         VERIFY(zevent_fd >= 0);
7681
7682         if (!opts->scripted)
7683                 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
7684
7685         while (1) {
7686                 ret = zpool_events_next(g_zfs, &nvl, &dropped,
7687                     (opts->follow ? ZEVENT_NONE : ZEVENT_NONBLOCK), zevent_fd);
7688                 if (ret || nvl == NULL)
7689                         break;
7690
7691                 if (dropped > 0)
7692                         (void) printf(gettext("dropped %d events\n"), dropped);
7693
7694                 if (strlen(opts->poolname) > 0 &&
7695                     nvlist_lookup_string(nvl, FM_FMRI_ZFS_POOL, &pool) == 0 &&
7696                     strcmp(opts->poolname, pool) != 0)
7697                         continue;
7698
7699                 zpool_do_events_short(nvl, opts);
7700
7701                 if (opts->verbose) {
7702                         zpool_do_events_nvprint(nvl, 8);
7703                         printf(gettext("\n"));
7704                 }
7705                 (void) fflush(stdout);
7706
7707                 nvlist_free(nvl);
7708         }
7709
7710         VERIFY(0 == close(zevent_fd));
7711
7712         return (ret);
7713 }
7714
7715 static int
7716 zpool_do_events_clear(ev_opts_t *opts)
7717 {
7718         int count, ret;
7719
7720         ret = zpool_events_clear(g_zfs, &count);
7721         if (!ret)
7722                 (void) printf(gettext("cleared %d events\n"), count);
7723
7724         return (ret);
7725 }
7726
7727 /*
7728  * zpool events [-vHf [pool] | -c]
7729  *
7730  * Displays events logs by ZFS.
7731  */
7732 int
7733 zpool_do_events(int argc, char **argv)
7734 {
7735         ev_opts_t opts = { 0 };
7736         int ret;
7737         int c;
7738
7739         /* check options */
7740         while ((c = getopt(argc, argv, "vHfc")) != -1) {
7741                 switch (c) {
7742                 case 'v':
7743                         opts.verbose = 1;
7744                         break;
7745                 case 'H':
7746                         opts.scripted = 1;
7747                         break;
7748                 case 'f':
7749                         opts.follow = 1;
7750                         break;
7751                 case 'c':
7752                         opts.clear = 1;
7753                         break;
7754                 case '?':
7755                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7756                             optopt);
7757                         usage(B_FALSE);
7758                 }
7759         }
7760         argc -= optind;
7761         argv += optind;
7762
7763         if (argc > 1) {
7764                 (void) fprintf(stderr, gettext("too many arguments\n"));
7765                 usage(B_FALSE);
7766         } else if (argc == 1) {
7767                 (void) strlcpy(opts.poolname, argv[0], sizeof (opts.poolname));
7768                 if (!zfs_name_valid(opts.poolname, ZFS_TYPE_POOL)) {
7769                         (void) fprintf(stderr,
7770                             gettext("invalid pool name '%s'\n"), opts.poolname);
7771                         usage(B_FALSE);
7772                 }
7773         }
7774
7775         if ((argc == 1 || opts.verbose || opts.scripted || opts.follow) &&
7776             opts.clear) {
7777                 (void) fprintf(stderr,
7778                     gettext("invalid options combined with -c\n"));
7779                 usage(B_FALSE);
7780         }
7781
7782         if (opts.clear)
7783                 ret = zpool_do_events_clear(&opts);
7784         else
7785                 ret = zpool_do_events_next(&opts);
7786
7787         return (ret);
7788 }
7789
7790 static int
7791 get_callback(zpool_handle_t *zhp, void *data)
7792 {
7793         zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
7794         char value[MAXNAMELEN];
7795         zprop_source_t srctype;
7796         zprop_list_t *pl;
7797
7798         for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
7799
7800                 /*
7801                  * Skip the special fake placeholder. This will also skip
7802                  * over the name property when 'all' is specified.
7803                  */
7804                 if (pl->pl_prop == ZPOOL_PROP_NAME &&
7805                     pl == cbp->cb_proplist)
7806                         continue;
7807
7808                 if (pl->pl_prop == ZPROP_INVAL &&
7809                     (zpool_prop_feature(pl->pl_user_prop) ||
7810                     zpool_prop_unsupported(pl->pl_user_prop))) {
7811                         srctype = ZPROP_SRC_LOCAL;
7812
7813                         if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
7814                             value, sizeof (value)) == 0) {
7815                                 zprop_print_one_property(zpool_get_name(zhp),
7816                                     cbp, pl->pl_user_prop, value, srctype,
7817                                     NULL, NULL);
7818                         }
7819                 } else {
7820                         if (zpool_get_prop(zhp, pl->pl_prop, value,
7821                             sizeof (value), &srctype, cbp->cb_literal) != 0)
7822                                 continue;
7823
7824                         zprop_print_one_property(zpool_get_name(zhp), cbp,
7825                             zpool_prop_to_name(pl->pl_prop), value, srctype,
7826                             NULL, NULL);
7827                 }
7828         }
7829         return (0);
7830 }
7831
7832 /*
7833  * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
7834  *
7835  *      -H      Scripted mode.  Don't display headers, and separate properties
7836  *              by a single tab.
7837  *      -o      List of columns to display.  Defaults to
7838  *              "name,property,value,source".
7839  *      -p      Display values in parsable (exact) format.
7840  *
7841  * Get properties of pools in the system. Output space statistics
7842  * for each one as well as other attributes.
7843  */
7844 int
7845 zpool_do_get(int argc, char **argv)
7846 {
7847         zprop_get_cbdata_t cb = { 0 };
7848         zprop_list_t fake_name = { 0 };
7849         int ret;
7850         int c, i;
7851         char *value;
7852
7853         cb.cb_first = B_TRUE;
7854
7855         /*
7856          * Set up default columns and sources.
7857          */
7858         cb.cb_sources = ZPROP_SRC_ALL;
7859         cb.cb_columns[0] = GET_COL_NAME;
7860         cb.cb_columns[1] = GET_COL_PROPERTY;
7861         cb.cb_columns[2] = GET_COL_VALUE;
7862         cb.cb_columns[3] = GET_COL_SOURCE;
7863         cb.cb_type = ZFS_TYPE_POOL;
7864
7865         /* check options */
7866         while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
7867                 switch (c) {
7868                 case 'p':
7869                         cb.cb_literal = B_TRUE;
7870                         break;
7871                 case 'H':
7872                         cb.cb_scripted = B_TRUE;
7873                         break;
7874                 case 'o':
7875                         bzero(&cb.cb_columns, sizeof (cb.cb_columns));
7876                         i = 0;
7877                         while (*optarg != '\0') {
7878                                 static char *col_subopts[] =
7879                                 { "name", "property", "value", "source",
7880                                 "all", NULL };
7881
7882                                 if (i == ZFS_GET_NCOLS) {
7883                                         (void) fprintf(stderr, gettext("too "
7884                                         "many fields given to -o "
7885                                         "option\n"));
7886                                         usage(B_FALSE);
7887                                 }
7888
7889                                 switch (getsubopt(&optarg, col_subopts,
7890                                     &value)) {
7891                                 case 0:
7892                                         cb.cb_columns[i++] = GET_COL_NAME;
7893                                         break;
7894                                 case 1:
7895                                         cb.cb_columns[i++] = GET_COL_PROPERTY;
7896                                         break;
7897                                 case 2:
7898                                         cb.cb_columns[i++] = GET_COL_VALUE;
7899                                         break;
7900                                 case 3:
7901                                         cb.cb_columns[i++] = GET_COL_SOURCE;
7902                                         break;
7903                                 case 4:
7904                                         if (i > 0) {
7905                                                 (void) fprintf(stderr,
7906                                                     gettext("\"all\" conflicts "
7907                                                     "with specific fields "
7908                                                     "given to -o option\n"));
7909                                                 usage(B_FALSE);
7910                                         }
7911                                         cb.cb_columns[0] = GET_COL_NAME;
7912                                         cb.cb_columns[1] = GET_COL_PROPERTY;
7913                                         cb.cb_columns[2] = GET_COL_VALUE;
7914                                         cb.cb_columns[3] = GET_COL_SOURCE;
7915                                         i = ZFS_GET_NCOLS;
7916                                         break;
7917                                 default:
7918                                         (void) fprintf(stderr,
7919                                             gettext("invalid column name "
7920                                             "'%s'\n"), value);
7921                                         usage(B_FALSE);
7922                                 }
7923                         }
7924                         break;
7925                 case '?':
7926                         (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7927                             optopt);
7928                         usage(B_FALSE);
7929                 }
7930         }
7931
7932         argc -= optind;
7933         argv += optind;
7934
7935         if (argc < 1) {
7936                 (void) fprintf(stderr, gettext("missing property "
7937                     "argument\n"));
7938                 usage(B_FALSE);
7939         }
7940
7941         if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
7942             ZFS_TYPE_POOL) != 0)
7943                 usage(B_FALSE);
7944
7945         argc--;
7946         argv++;
7947
7948         if (cb.cb_proplist != NULL) {
7949                 fake_name.pl_prop = ZPOOL_PROP_NAME;
7950                 fake_name.pl_width = strlen(gettext("NAME"));
7951                 fake_name.pl_next = cb.cb_proplist;
7952                 cb.cb_proplist = &fake_name;
7953         }
7954
7955         ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
7956             get_callback, &cb);
7957
7958         if (cb.cb_proplist == &fake_name)
7959                 zprop_free_list(fake_name.pl_next);
7960         else
7961                 zprop_free_list(cb.cb_proplist);
7962
7963         return (ret);
7964 }
7965
7966 typedef struct set_cbdata {
7967         char *cb_propname;
7968         char *cb_value;
7969         boolean_t cb_any_successful;
7970 } set_cbdata_t;
7971
7972 int
7973 set_callback(zpool_handle_t *zhp, void *data)
7974 {
7975         int error;
7976         set_cbdata_t *cb = (set_cbdata_t *)data;
7977
7978         error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
7979
7980         if (!error)
7981                 cb->cb_any_successful = B_TRUE;
7982
7983         return (error);
7984 }
7985
7986 int
7987 zpool_do_set(int argc, char **argv)
7988 {
7989         set_cbdata_t cb = { 0 };
7990         int error;
7991
7992         if (argc > 1 && argv[1][0] == '-') {
7993                 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7994                     argv[1][1]);
7995                 usage(B_FALSE);
7996         }
7997
7998         if (argc < 2) {
7999                 (void) fprintf(stderr, gettext("missing property=value "
8000                     "argument\n"));
8001                 usage(B_FALSE);
8002         }
8003
8004         if (argc < 3) {
8005                 (void) fprintf(stderr, gettext("missing pool name\n"));
8006                 usage(B_FALSE);
8007         }
8008
8009         if (argc > 3) {
8010                 (void) fprintf(stderr, gettext("too many pool names\n"));
8011                 usage(B_FALSE);
8012         }
8013
8014         cb.cb_propname = argv[1];
8015         cb.cb_value = strchr(cb.cb_propname, '=');
8016         if (cb.cb_value == NULL) {
8017                 (void) fprintf(stderr, gettext("missing value in "
8018                     "property=value argument\n"));
8019                 usage(B_FALSE);
8020         }
8021
8022         *(cb.cb_value) = '\0';
8023         cb.cb_value++;
8024
8025         error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
8026             set_callback, &cb);
8027
8028         return (error);
8029 }
8030
8031 static int
8032 find_command_idx(char *command, int *idx)
8033 {
8034         int i;
8035
8036         for (i = 0; i < NCOMMAND; i++) {
8037                 if (command_table[i].name == NULL)
8038                         continue;
8039
8040                 if (strcmp(command, command_table[i].name) == 0) {
8041                         *idx = i;
8042                         return (0);
8043                 }
8044         }
8045         return (1);
8046 }
8047
8048 int
8049 main(int argc, char **argv)
8050 {
8051         int ret = 0;
8052         int i = 0;
8053         char *cmdname;
8054
8055         (void) setlocale(LC_ALL, "");
8056         (void) textdomain(TEXT_DOMAIN);
8057         srand(time(NULL));
8058
8059         opterr = 0;
8060
8061         /*
8062          * Make sure the user has specified some command.
8063          */
8064         if (argc < 2) {
8065                 (void) fprintf(stderr, gettext("missing command\n"));
8066                 usage(B_FALSE);
8067         }
8068
8069         cmdname = argv[1];
8070
8071         /*
8072          * Special case '-?'
8073          */
8074         if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0)
8075                 usage(B_TRUE);
8076
8077         if ((g_zfs = libzfs_init()) == NULL) {
8078                 (void) fprintf(stderr, "%s", libzfs_error_init(errno));
8079                 return (1);
8080         }
8081
8082         libzfs_print_on_error(g_zfs, B_TRUE);
8083
8084         zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
8085
8086         /*
8087          * Run the appropriate command.
8088          */
8089         if (find_command_idx(cmdname, &i) == 0) {
8090                 current_command = &command_table[i];
8091                 ret = command_table[i].func(argc - 1, argv + 1);
8092         } else if (strchr(cmdname, '=')) {
8093                 verify(find_command_idx("set", &i) == 0);
8094                 current_command = &command_table[i];
8095                 ret = command_table[i].func(argc, argv);
8096         } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
8097                 /*
8098                  * 'freeze' is a vile debugging abomination, so we treat
8099                  * it as such.
8100                  */
8101                 zfs_cmd_t zc = {"\0"};
8102
8103                 (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name));
8104                 ret = zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc);
8105                 if (ret != 0) {
8106                         (void) fprintf(stderr,
8107                         gettext("failed to freeze pool: %d\n"), errno);
8108                         ret = 1;
8109                 }
8110
8111                 log_history = 0;
8112         } else {
8113                 (void) fprintf(stderr, gettext("unrecognized "
8114                     "command '%s'\n"), cmdname);
8115                 usage(B_FALSE);
8116                 ret = 1;
8117         }
8118
8119         if (ret == 0 && log_history)
8120                 (void) zpool_log_history(g_zfs, history_str);
8121
8122         libzfs_fini(g_zfs);
8123
8124         /*
8125          * The 'ZFS_ABORT' environment variable causes us to dump core on exit
8126          * for the purposes of running ::findleaks.
8127          */
8128         if (getenv("ZFS_ABORT") != NULL) {
8129                 (void) printf("dumping core by request\n");
8130                 abort();
8131         }
8132
8133         return (ret);
8134 }