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