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