xref: /freebsd/sys/contrib/openzfs/cmd/zpool/zpool_main.c (revision d9a42747950146bf03cda7f6e25d219253f8a57a)
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 https://opensource.org/licenses/CDDL-1.0.
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, 2020 by Delphix. All rights reserved.
26  * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27  * Copyright (c) 2012 by Cyril Plisko. All rights reserved.
28  * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
29  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
30  * Copyright (c) 2017 Datto Inc.
31  * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
32  * Copyright (c) 2017, Intel Corporation.
33  * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>
34  * Copyright (c) 2021, Colm Buckley <colm@tuatha.org>
35  * Copyright (c) 2021, Klara Inc.
36  * Copyright [2021] Hewlett Packard Enterprise Development LP
37  */
38 
39 #include <assert.h>
40 #include <ctype.h>
41 #include <dirent.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <getopt.h>
45 #include <libgen.h>
46 #include <libintl.h>
47 #include <libuutil.h>
48 #include <locale.h>
49 #include <pthread.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <time.h>
54 #include <unistd.h>
55 #include <pwd.h>
56 #include <zone.h>
57 #include <sys/wait.h>
58 #include <zfs_prop.h>
59 #include <sys/fs/zfs.h>
60 #include <sys/stat.h>
61 #include <sys/systeminfo.h>
62 #include <sys/fm/fs/zfs.h>
63 #include <sys/fm/util.h>
64 #include <sys/fm/protocol.h>
65 #include <sys/zfs_ioctl.h>
66 #include <sys/mount.h>
67 #include <sys/sysmacros.h>
68 
69 #include <math.h>
70 
71 #include <libzfs.h>
72 #include <libzutil.h>
73 
74 #include "zpool_util.h"
75 #include "zfs_comutil.h"
76 #include "zfeature_common.h"
77 
78 #include "statcommon.h"
79 
80 libzfs_handle_t *g_zfs;
81 
82 static int zpool_do_create(int, char **);
83 static int zpool_do_destroy(int, char **);
84 
85 static int zpool_do_add(int, char **);
86 static int zpool_do_remove(int, char **);
87 static int zpool_do_labelclear(int, char **);
88 
89 static int zpool_do_checkpoint(int, char **);
90 
91 static int zpool_do_list(int, char **);
92 static int zpool_do_iostat(int, char **);
93 static int zpool_do_status(int, char **);
94 
95 static int zpool_do_online(int, char **);
96 static int zpool_do_offline(int, char **);
97 static int zpool_do_clear(int, char **);
98 static int zpool_do_reopen(int, char **);
99 
100 static int zpool_do_reguid(int, char **);
101 
102 static int zpool_do_attach(int, char **);
103 static int zpool_do_detach(int, char **);
104 static int zpool_do_replace(int, char **);
105 static int zpool_do_split(int, char **);
106 
107 static int zpool_do_initialize(int, char **);
108 static int zpool_do_scrub(int, char **);
109 static int zpool_do_resilver(int, char **);
110 static int zpool_do_trim(int, char **);
111 
112 static int zpool_do_import(int, char **);
113 static int zpool_do_export(int, char **);
114 
115 static int zpool_do_upgrade(int, char **);
116 
117 static int zpool_do_history(int, char **);
118 static int zpool_do_events(int, char **);
119 
120 static int zpool_do_get(int, char **);
121 static int zpool_do_set(int, char **);
122 
123 static int zpool_do_sync(int, char **);
124 
125 static int zpool_do_version(int, char **);
126 
127 static int zpool_do_wait(int, char **);
128 
129 static zpool_compat_status_t zpool_do_load_compat(
130     const char *, boolean_t *);
131 
132 /*
133  * These libumem hooks provide a reasonable set of defaults for the allocator's
134  * debugging facilities.
135  */
136 
137 #ifdef DEBUG
138 const char *
139 _umem_debug_init(void)
140 {
141 	return ("default,verbose"); /* $UMEM_DEBUG setting */
142 }
143 
144 const char *
145 _umem_logging_init(void)
146 {
147 	return ("fail,contents"); /* $UMEM_LOGGING setting */
148 }
149 #endif
150 
151 typedef enum {
152 	HELP_ADD,
153 	HELP_ATTACH,
154 	HELP_CLEAR,
155 	HELP_CREATE,
156 	HELP_CHECKPOINT,
157 	HELP_DESTROY,
158 	HELP_DETACH,
159 	HELP_EXPORT,
160 	HELP_HISTORY,
161 	HELP_IMPORT,
162 	HELP_IOSTAT,
163 	HELP_LABELCLEAR,
164 	HELP_LIST,
165 	HELP_OFFLINE,
166 	HELP_ONLINE,
167 	HELP_REPLACE,
168 	HELP_REMOVE,
169 	HELP_INITIALIZE,
170 	HELP_SCRUB,
171 	HELP_RESILVER,
172 	HELP_TRIM,
173 	HELP_STATUS,
174 	HELP_UPGRADE,
175 	HELP_EVENTS,
176 	HELP_GET,
177 	HELP_SET,
178 	HELP_SPLIT,
179 	HELP_SYNC,
180 	HELP_REGUID,
181 	HELP_REOPEN,
182 	HELP_VERSION,
183 	HELP_WAIT
184 } zpool_help_t;
185 
186 
187 /*
188  * Flags for stats to display with "zpool iostats"
189  */
190 enum iostat_type {
191 	IOS_DEFAULT = 0,
192 	IOS_LATENCY = 1,
193 	IOS_QUEUES = 2,
194 	IOS_L_HISTO = 3,
195 	IOS_RQ_HISTO = 4,
196 	IOS_COUNT,	/* always last element */
197 };
198 
199 /* iostat_type entries as bitmasks */
200 #define	IOS_DEFAULT_M	(1ULL << IOS_DEFAULT)
201 #define	IOS_LATENCY_M	(1ULL << IOS_LATENCY)
202 #define	IOS_QUEUES_M	(1ULL << IOS_QUEUES)
203 #define	IOS_L_HISTO_M	(1ULL << IOS_L_HISTO)
204 #define	IOS_RQ_HISTO_M	(1ULL << IOS_RQ_HISTO)
205 
206 /* Mask of all the histo bits */
207 #define	IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
208 
209 /*
210  * Lookup table for iostat flags to nvlist names.  Basically a list
211  * of all the nvlists a flag requires.  Also specifies the order in
212  * which data gets printed in zpool iostat.
213  */
214 static const char *vsx_type_to_nvlist[IOS_COUNT][15] = {
215 	[IOS_L_HISTO] = {
216 	    ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
217 	    ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
218 	    ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
219 	    ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
220 	    ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
221 	    ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
222 	    ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
223 	    ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
224 	    ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
225 	    ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO,
226 	    ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO,
227 	    NULL},
228 	[IOS_LATENCY] = {
229 	    ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
230 	    ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
231 	    ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
232 	    ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
233 	    ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO,
234 	    ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO,
235 	    NULL},
236 	[IOS_QUEUES] = {
237 	    ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
238 	    ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
239 	    ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
240 	    ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
241 	    ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
242 	    ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE,
243 	    ZPOOL_CONFIG_VDEV_REBUILD_ACTIVE_QUEUE,
244 	    NULL},
245 	[IOS_RQ_HISTO] = {
246 	    ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO,
247 	    ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO,
248 	    ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO,
249 	    ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO,
250 	    ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO,
251 	    ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO,
252 	    ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO,
253 	    ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO,
254 	    ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO,
255 	    ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO,
256 	    ZPOOL_CONFIG_VDEV_IND_TRIM_HISTO,
257 	    ZPOOL_CONFIG_VDEV_AGG_TRIM_HISTO,
258 	    ZPOOL_CONFIG_VDEV_IND_REBUILD_HISTO,
259 	    ZPOOL_CONFIG_VDEV_AGG_REBUILD_HISTO,
260 	    NULL},
261 };
262 
263 
264 /*
265  * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
266  * Right now, only one histo bit is ever set at one time, so we can
267  * just do a highbit64(a)
268  */
269 #define	IOS_HISTO_IDX(a)	(highbit64(a & IOS_ANYHISTO_M) - 1)
270 
271 typedef struct zpool_command {
272 	const char	*name;
273 	int		(*func)(int, char **);
274 	zpool_help_t	usage;
275 } zpool_command_t;
276 
277 /*
278  * Master command table.  Each ZFS command has a name, associated function, and
279  * usage message.  The usage messages need to be internationalized, so we have
280  * to have a function to return the usage message based on a command index.
281  *
282  * These commands are organized according to how they are displayed in the usage
283  * message.  An empty command (one with a NULL name) indicates an empty line in
284  * the generic usage message.
285  */
286 static zpool_command_t command_table[] = {
287 	{ "version",	zpool_do_version,	HELP_VERSION		},
288 	{ NULL },
289 	{ "create",	zpool_do_create,	HELP_CREATE		},
290 	{ "destroy",	zpool_do_destroy,	HELP_DESTROY		},
291 	{ NULL },
292 	{ "add",	zpool_do_add,		HELP_ADD		},
293 	{ "remove",	zpool_do_remove,	HELP_REMOVE		},
294 	{ NULL },
295 	{ "labelclear",	zpool_do_labelclear,	HELP_LABELCLEAR		},
296 	{ NULL },
297 	{ "checkpoint",	zpool_do_checkpoint,	HELP_CHECKPOINT		},
298 	{ NULL },
299 	{ "list",	zpool_do_list,		HELP_LIST		},
300 	{ "iostat",	zpool_do_iostat,	HELP_IOSTAT		},
301 	{ "status",	zpool_do_status,	HELP_STATUS		},
302 	{ NULL },
303 	{ "online",	zpool_do_online,	HELP_ONLINE		},
304 	{ "offline",	zpool_do_offline,	HELP_OFFLINE		},
305 	{ "clear",	zpool_do_clear,		HELP_CLEAR		},
306 	{ "reopen",	zpool_do_reopen,	HELP_REOPEN		},
307 	{ NULL },
308 	{ "attach",	zpool_do_attach,	HELP_ATTACH		},
309 	{ "detach",	zpool_do_detach,	HELP_DETACH		},
310 	{ "replace",	zpool_do_replace,	HELP_REPLACE		},
311 	{ "split",	zpool_do_split,		HELP_SPLIT		},
312 	{ NULL },
313 	{ "initialize",	zpool_do_initialize,	HELP_INITIALIZE		},
314 	{ "resilver",	zpool_do_resilver,	HELP_RESILVER		},
315 	{ "scrub",	zpool_do_scrub,		HELP_SCRUB		},
316 	{ "trim",	zpool_do_trim,		HELP_TRIM		},
317 	{ NULL },
318 	{ "import",	zpool_do_import,	HELP_IMPORT		},
319 	{ "export",	zpool_do_export,	HELP_EXPORT		},
320 	{ "upgrade",	zpool_do_upgrade,	HELP_UPGRADE		},
321 	{ "reguid",	zpool_do_reguid,	HELP_REGUID		},
322 	{ NULL },
323 	{ "history",	zpool_do_history,	HELP_HISTORY		},
324 	{ "events",	zpool_do_events,	HELP_EVENTS		},
325 	{ NULL },
326 	{ "get",	zpool_do_get,		HELP_GET		},
327 	{ "set",	zpool_do_set,		HELP_SET		},
328 	{ "sync",	zpool_do_sync,		HELP_SYNC		},
329 	{ NULL },
330 	{ "wait",	zpool_do_wait,		HELP_WAIT		},
331 };
332 
333 #define	NCOMMAND	(ARRAY_SIZE(command_table))
334 
335 #define	VDEV_ALLOC_CLASS_LOGS	"logs"
336 
337 static zpool_command_t *current_command;
338 static zfs_type_t current_prop_type = (ZFS_TYPE_POOL | ZFS_TYPE_VDEV);
339 static char history_str[HIS_MAX_RECORD_LEN];
340 static boolean_t log_history = B_TRUE;
341 static uint_t timestamp_fmt = NODATE;
342 
343 static const char *
344 get_usage(zpool_help_t idx)
345 {
346 	switch (idx) {
347 	case HELP_ADD:
348 		return (gettext("\tadd [-fgLnP] [-o property=value] "
349 		    "<pool> <vdev> ...\n"));
350 	case HELP_ATTACH:
351 		return (gettext("\tattach [-fsw] [-o property=value] "
352 		    "<pool> <device> <new-device>\n"));
353 	case HELP_CLEAR:
354 		return (gettext("\tclear [-nF] <pool> [device]\n"));
355 	case HELP_CREATE:
356 		return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
357 		    "\t    [-O file-system-property=value] ... \n"
358 		    "\t    [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
359 	case HELP_CHECKPOINT:
360 		return (gettext("\tcheckpoint [-d [-w]] <pool> ...\n"));
361 	case HELP_DESTROY:
362 		return (gettext("\tdestroy [-f] <pool>\n"));
363 	case HELP_DETACH:
364 		return (gettext("\tdetach <pool> <device>\n"));
365 	case HELP_EXPORT:
366 		return (gettext("\texport [-af] <pool> ...\n"));
367 	case HELP_HISTORY:
368 		return (gettext("\thistory [-il] [<pool>] ...\n"));
369 	case HELP_IMPORT:
370 		return (gettext("\timport [-d dir] [-D]\n"
371 		    "\timport [-o mntopts] [-o property=value] ... \n"
372 		    "\t    [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
373 		    "[-R root] [-F [-n]] -a\n"
374 		    "\timport [-o mntopts] [-o property=value] ... \n"
375 		    "\t    [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
376 		    "[-R root] [-F [-n]]\n"
377 		    "\t    [--rewind-to-checkpoint] <pool | id> [newpool]\n"));
378 	case HELP_IOSTAT:
379 		return (gettext("\tiostat [[[-c [script1,script2,...]"
380 		    "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n"
381 		    "\t    [[pool ...]|[pool vdev ...]|[vdev ...]]"
382 		    " [[-n] interval [count]]\n"));
383 	case HELP_LABELCLEAR:
384 		return (gettext("\tlabelclear [-f] <vdev>\n"));
385 	case HELP_LIST:
386 		return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
387 		    "[-T d|u] [pool] ... \n"
388 		    "\t    [interval [count]]\n"));
389 	case HELP_OFFLINE:
390 		return (gettext("\toffline [-f] [-t] <pool> <device> ...\n"));
391 	case HELP_ONLINE:
392 		return (gettext("\tonline [-e] <pool> <device> ...\n"));
393 	case HELP_REPLACE:
394 		return (gettext("\treplace [-fsw] [-o property=value] "
395 		    "<pool> <device> [new-device]\n"));
396 	case HELP_REMOVE:
397 		return (gettext("\tremove [-npsw] <pool> <device> ...\n"));
398 	case HELP_REOPEN:
399 		return (gettext("\treopen [-n] <pool>\n"));
400 	case HELP_INITIALIZE:
401 		return (gettext("\tinitialize [-c | -s] [-w] <pool> "
402 		    "[<device> ...]\n"));
403 	case HELP_SCRUB:
404 		return (gettext("\tscrub [-s | -p] [-w] <pool> ...\n"));
405 	case HELP_RESILVER:
406 		return (gettext("\tresilver <pool> ...\n"));
407 	case HELP_TRIM:
408 		return (gettext("\ttrim [-dw] [-r <rate>] [-c | -s] <pool> "
409 		    "[<device> ...]\n"));
410 	case HELP_STATUS:
411 		return (gettext("\tstatus [-c [script1,script2,...]] "
412 		    "[-igLpPstvxD]  [-T d|u] [pool] ... \n"
413 		    "\t    [interval [count]]\n"));
414 	case HELP_UPGRADE:
415 		return (gettext("\tupgrade\n"
416 		    "\tupgrade -v\n"
417 		    "\tupgrade [-V version] <-a | pool ...>\n"));
418 	case HELP_EVENTS:
419 		return (gettext("\tevents [-vHf [pool] | -c]\n"));
420 	case HELP_GET:
421 		return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
422 		    "<\"all\" | property[,...]> <pool> ...\n"));
423 	case HELP_SET:
424 		return (gettext("\tset <property=value> <pool>\n"
425 		    "\tset <vdev_property=value> <pool> <vdev>\n"));
426 	case HELP_SPLIT:
427 		return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n"
428 		    "\t    [-o property=value] <pool> <newpool> "
429 		    "[<device> ...]\n"));
430 	case HELP_REGUID:
431 		return (gettext("\treguid <pool>\n"));
432 	case HELP_SYNC:
433 		return (gettext("\tsync [pool] ...\n"));
434 	case HELP_VERSION:
435 		return (gettext("\tversion\n"));
436 	case HELP_WAIT:
437 		return (gettext("\twait [-Hp] [-T d|u] [-t <activity>[,...]] "
438 		    "<pool> [interval]\n"));
439 	default:
440 		__builtin_unreachable();
441 	}
442 }
443 
444 static void
445 zpool_collect_leaves(zpool_handle_t *zhp, nvlist_t *nvroot, nvlist_t *res)
446 {
447 	uint_t children = 0;
448 	nvlist_t **child;
449 	uint_t i;
450 
451 	(void) nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
452 	    &child, &children);
453 
454 	if (children == 0) {
455 		char *path = zpool_vdev_name(g_zfs, zhp, nvroot,
456 		    VDEV_NAME_PATH);
457 
458 		if (strcmp(path, VDEV_TYPE_INDIRECT) != 0 &&
459 		    strcmp(path, VDEV_TYPE_HOLE) != 0)
460 			fnvlist_add_boolean(res, path);
461 
462 		free(path);
463 		return;
464 	}
465 
466 	for (i = 0; i < children; i++) {
467 		zpool_collect_leaves(zhp, child[i], res);
468 	}
469 }
470 
471 /*
472  * Callback routine that will print out a pool property value.
473  */
474 static int
475 print_pool_prop_cb(int prop, void *cb)
476 {
477 	FILE *fp = cb;
478 
479 	(void) fprintf(fp, "\t%-19s  ", zpool_prop_to_name(prop));
480 
481 	if (zpool_prop_readonly(prop))
482 		(void) fprintf(fp, "  NO   ");
483 	else
484 		(void) fprintf(fp, " YES   ");
485 
486 	if (zpool_prop_values(prop) == NULL)
487 		(void) fprintf(fp, "-\n");
488 	else
489 		(void) fprintf(fp, "%s\n", zpool_prop_values(prop));
490 
491 	return (ZPROP_CONT);
492 }
493 
494 /*
495  * Callback routine that will print out a vdev property value.
496  */
497 static int
498 print_vdev_prop_cb(int prop, void *cb)
499 {
500 	FILE *fp = cb;
501 
502 	(void) fprintf(fp, "\t%-19s  ", vdev_prop_to_name(prop));
503 
504 	if (vdev_prop_readonly(prop))
505 		(void) fprintf(fp, "  NO   ");
506 	else
507 		(void) fprintf(fp, " YES   ");
508 
509 	if (vdev_prop_values(prop) == NULL)
510 		(void) fprintf(fp, "-\n");
511 	else
512 		(void) fprintf(fp, "%s\n", vdev_prop_values(prop));
513 
514 	return (ZPROP_CONT);
515 }
516 
517 /*
518  * Display usage message.  If we're inside a command, display only the usage for
519  * that command.  Otherwise, iterate over the entire command table and display
520  * a complete usage message.
521  */
522 static __attribute__((noreturn)) void
523 usage(boolean_t requested)
524 {
525 	FILE *fp = requested ? stdout : stderr;
526 
527 	if (current_command == NULL) {
528 		int i;
529 
530 		(void) fprintf(fp, gettext("usage: zpool command args ...\n"));
531 		(void) fprintf(fp,
532 		    gettext("where 'command' is one of the following:\n\n"));
533 
534 		for (i = 0; i < NCOMMAND; i++) {
535 			if (command_table[i].name == NULL)
536 				(void) fprintf(fp, "\n");
537 			else
538 				(void) fprintf(fp, "%s",
539 				    get_usage(command_table[i].usage));
540 		}
541 	} else {
542 		(void) fprintf(fp, gettext("usage:\n"));
543 		(void) fprintf(fp, "%s", get_usage(current_command->usage));
544 	}
545 
546 	if (current_command != NULL &&
547 	    current_prop_type != (ZFS_TYPE_POOL | ZFS_TYPE_VDEV) &&
548 	    ((strcmp(current_command->name, "set") == 0) ||
549 	    (strcmp(current_command->name, "get") == 0) ||
550 	    (strcmp(current_command->name, "list") == 0))) {
551 
552 		(void) fprintf(fp, "%s",
553 		    gettext("\nthe following properties are supported:\n"));
554 
555 		(void) fprintf(fp, "\n\t%-19s  %s   %s\n\n",
556 		    "PROPERTY", "EDIT", "VALUES");
557 
558 		/* Iterate over all properties */
559 		if (current_prop_type == ZFS_TYPE_POOL) {
560 			(void) zprop_iter(print_pool_prop_cb, fp, B_FALSE,
561 			    B_TRUE, current_prop_type);
562 
563 			(void) fprintf(fp, "\t%-19s   ", "feature@...");
564 			(void) fprintf(fp, "YES   "
565 			    "disabled | enabled | active\n");
566 
567 			(void) fprintf(fp, gettext("\nThe feature@ properties "
568 			    "must be appended with a feature name.\n"
569 			    "See zpool-features(7).\n"));
570 		} else if (current_prop_type == ZFS_TYPE_VDEV) {
571 			(void) zprop_iter(print_vdev_prop_cb, fp, B_FALSE,
572 			    B_TRUE, current_prop_type);
573 		}
574 	}
575 
576 	/*
577 	 * See comments at end of main().
578 	 */
579 	if (getenv("ZFS_ABORT") != NULL) {
580 		(void) printf("dumping core by request\n");
581 		abort();
582 	}
583 
584 	exit(requested ? 0 : 2);
585 }
586 
587 /*
588  * zpool initialize [-c | -s] [-w] <pool> [<vdev> ...]
589  * Initialize all unused blocks in the specified vdevs, or all vdevs in the pool
590  * if none specified.
591  *
592  *	-c	Cancel. Ends active initializing.
593  *	-s	Suspend. Initializing can then be restarted with no flags.
594  *	-w	Wait. Blocks until initializing has completed.
595  */
596 int
597 zpool_do_initialize(int argc, char **argv)
598 {
599 	int c;
600 	char *poolname;
601 	zpool_handle_t *zhp;
602 	nvlist_t *vdevs;
603 	int err = 0;
604 	boolean_t wait = B_FALSE;
605 
606 	struct option long_options[] = {
607 		{"cancel",	no_argument,		NULL, 'c'},
608 		{"suspend",	no_argument,		NULL, 's'},
609 		{"wait",	no_argument,		NULL, 'w'},
610 		{0, 0, 0, 0}
611 	};
612 
613 	pool_initialize_func_t cmd_type = POOL_INITIALIZE_START;
614 	while ((c = getopt_long(argc, argv, "csw", long_options, NULL)) != -1) {
615 		switch (c) {
616 		case 'c':
617 			if (cmd_type != POOL_INITIALIZE_START &&
618 			    cmd_type != POOL_INITIALIZE_CANCEL) {
619 				(void) fprintf(stderr, gettext("-c cannot be "
620 				    "combined with other options\n"));
621 				usage(B_FALSE);
622 			}
623 			cmd_type = POOL_INITIALIZE_CANCEL;
624 			break;
625 		case 's':
626 			if (cmd_type != POOL_INITIALIZE_START &&
627 			    cmd_type != POOL_INITIALIZE_SUSPEND) {
628 				(void) fprintf(stderr, gettext("-s cannot be "
629 				    "combined with other options\n"));
630 				usage(B_FALSE);
631 			}
632 			cmd_type = POOL_INITIALIZE_SUSPEND;
633 			break;
634 		case 'w':
635 			wait = B_TRUE;
636 			break;
637 		case '?':
638 			if (optopt != 0) {
639 				(void) fprintf(stderr,
640 				    gettext("invalid option '%c'\n"), optopt);
641 			} else {
642 				(void) fprintf(stderr,
643 				    gettext("invalid option '%s'\n"),
644 				    argv[optind - 1]);
645 			}
646 			usage(B_FALSE);
647 		}
648 	}
649 
650 	argc -= optind;
651 	argv += optind;
652 
653 	if (argc < 1) {
654 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
655 		usage(B_FALSE);
656 		return (-1);
657 	}
658 
659 	if (wait && (cmd_type != POOL_INITIALIZE_START)) {
660 		(void) fprintf(stderr, gettext("-w cannot be used with -c or "
661 		    "-s\n"));
662 		usage(B_FALSE);
663 	}
664 
665 	poolname = argv[0];
666 	zhp = zpool_open(g_zfs, poolname);
667 	if (zhp == NULL)
668 		return (-1);
669 
670 	vdevs = fnvlist_alloc();
671 	if (argc == 1) {
672 		/* no individual leaf vdevs specified, so add them all */
673 		nvlist_t *config = zpool_get_config(zhp, NULL);
674 		nvlist_t *nvroot = fnvlist_lookup_nvlist(config,
675 		    ZPOOL_CONFIG_VDEV_TREE);
676 		zpool_collect_leaves(zhp, nvroot, vdevs);
677 	} else {
678 		for (int i = 1; i < argc; i++) {
679 			fnvlist_add_boolean(vdevs, argv[i]);
680 		}
681 	}
682 
683 	if (wait)
684 		err = zpool_initialize_wait(zhp, cmd_type, vdevs);
685 	else
686 		err = zpool_initialize(zhp, cmd_type, vdevs);
687 
688 	fnvlist_free(vdevs);
689 	zpool_close(zhp);
690 
691 	return (err);
692 }
693 
694 /*
695  * print a pool vdev config for dry runs
696  */
697 static void
698 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
699     const char *match, int name_flags)
700 {
701 	nvlist_t **child;
702 	uint_t c, children;
703 	char *vname;
704 	boolean_t printed = B_FALSE;
705 
706 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
707 	    &child, &children) != 0) {
708 		if (name != NULL)
709 			(void) printf("\t%*s%s\n", indent, "", name);
710 		return;
711 	}
712 
713 	for (c = 0; c < children; c++) {
714 		uint64_t is_log = B_FALSE, is_hole = B_FALSE;
715 		const char *class = "";
716 
717 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
718 		    &is_hole);
719 
720 		if (is_hole == B_TRUE) {
721 			continue;
722 		}
723 
724 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
725 		    &is_log);
726 		if (is_log)
727 			class = VDEV_ALLOC_BIAS_LOG;
728 		(void) nvlist_lookup_string(child[c],
729 		    ZPOOL_CONFIG_ALLOCATION_BIAS, &class);
730 		if (strcmp(match, class) != 0)
731 			continue;
732 
733 		if (!printed && name != NULL) {
734 			(void) printf("\t%*s%s\n", indent, "", name);
735 			printed = B_TRUE;
736 		}
737 		vname = zpool_vdev_name(g_zfs, zhp, child[c], name_flags);
738 		print_vdev_tree(zhp, vname, child[c], indent + 2, "",
739 		    name_flags);
740 		free(vname);
741 	}
742 }
743 
744 /*
745  * Print the list of l2cache devices for dry runs.
746  */
747 static void
748 print_cache_list(nvlist_t *nv, int indent)
749 {
750 	nvlist_t **child;
751 	uint_t c, children;
752 
753 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
754 	    &child, &children) == 0 && children > 0) {
755 		(void) printf("\t%*s%s\n", indent, "", "cache");
756 	} else {
757 		return;
758 	}
759 	for (c = 0; c < children; c++) {
760 		char *vname;
761 
762 		vname = zpool_vdev_name(g_zfs, NULL, child[c], 0);
763 		(void) printf("\t%*s%s\n", indent + 2, "", vname);
764 		free(vname);
765 	}
766 }
767 
768 /*
769  * Print the list of spares for dry runs.
770  */
771 static void
772 print_spare_list(nvlist_t *nv, int indent)
773 {
774 	nvlist_t **child;
775 	uint_t c, children;
776 
777 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
778 	    &child, &children) == 0 && children > 0) {
779 		(void) printf("\t%*s%s\n", indent, "", "spares");
780 	} else {
781 		return;
782 	}
783 	for (c = 0; c < children; c++) {
784 		char *vname;
785 
786 		vname = zpool_vdev_name(g_zfs, NULL, child[c], 0);
787 		(void) printf("\t%*s%s\n", indent + 2, "", vname);
788 		free(vname);
789 	}
790 }
791 
792 static boolean_t
793 prop_list_contains_feature(nvlist_t *proplist)
794 {
795 	nvpair_t *nvp;
796 	for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
797 	    nvp = nvlist_next_nvpair(proplist, nvp)) {
798 		if (zpool_prop_feature(nvpair_name(nvp)))
799 			return (B_TRUE);
800 	}
801 	return (B_FALSE);
802 }
803 
804 /*
805  * Add a property pair (name, string-value) into a property nvlist.
806  */
807 static int
808 add_prop_list(const char *propname, const char *propval, nvlist_t **props,
809     boolean_t poolprop)
810 {
811 	zpool_prop_t prop = ZPOOL_PROP_INVAL;
812 	nvlist_t *proplist;
813 	const char *normnm;
814 	const char *strval;
815 
816 	if (*props == NULL &&
817 	    nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
818 		(void) fprintf(stderr,
819 		    gettext("internal error: out of memory\n"));
820 		return (1);
821 	}
822 
823 	proplist = *props;
824 
825 	if (poolprop) {
826 		const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
827 		const char *cname =
828 		    zpool_prop_to_name(ZPOOL_PROP_COMPATIBILITY);
829 
830 		if ((prop = zpool_name_to_prop(propname)) == ZPOOL_PROP_INVAL &&
831 		    (!zpool_prop_feature(propname) &&
832 		    !zpool_prop_vdev(propname))) {
833 			(void) fprintf(stderr, gettext("property '%s' is "
834 			    "not a valid pool or vdev property\n"), propname);
835 			return (2);
836 		}
837 
838 		/*
839 		 * feature@ properties and version should not be specified
840 		 * at the same time.
841 		 */
842 		if ((prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname) &&
843 		    nvlist_exists(proplist, vname)) ||
844 		    (prop == ZPOOL_PROP_VERSION &&
845 		    prop_list_contains_feature(proplist))) {
846 			(void) fprintf(stderr, gettext("'feature@' and "
847 			    "'version' properties cannot be specified "
848 			    "together\n"));
849 			return (2);
850 		}
851 
852 		/*
853 		 * if version is specified, only "legacy" compatibility
854 		 * may be requested
855 		 */
856 		if ((prop == ZPOOL_PROP_COMPATIBILITY &&
857 		    strcmp(propval, ZPOOL_COMPAT_LEGACY) != 0 &&
858 		    nvlist_exists(proplist, vname)) ||
859 		    (prop == ZPOOL_PROP_VERSION &&
860 		    nvlist_exists(proplist, cname) &&
861 		    strcmp(fnvlist_lookup_string(proplist, cname),
862 		    ZPOOL_COMPAT_LEGACY) != 0)) {
863 			(void) fprintf(stderr, gettext("when 'version' is "
864 			    "specified, the 'compatibility' feature may only "
865 			    "be set to '" ZPOOL_COMPAT_LEGACY "'\n"));
866 			return (2);
867 		}
868 
869 		if (zpool_prop_feature(propname) || zpool_prop_vdev(propname))
870 			normnm = propname;
871 		else
872 			normnm = zpool_prop_to_name(prop);
873 	} else {
874 		zfs_prop_t fsprop = zfs_name_to_prop(propname);
875 
876 		if (zfs_prop_valid_for_type(fsprop, ZFS_TYPE_FILESYSTEM,
877 		    B_FALSE)) {
878 			normnm = zfs_prop_to_name(fsprop);
879 		} else if (zfs_prop_user(propname) ||
880 		    zfs_prop_userquota(propname)) {
881 			normnm = propname;
882 		} else {
883 			(void) fprintf(stderr, gettext("property '%s' is "
884 			    "not a valid filesystem property\n"), propname);
885 			return (2);
886 		}
887 	}
888 
889 	if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
890 	    prop != ZPOOL_PROP_CACHEFILE) {
891 		(void) fprintf(stderr, gettext("property '%s' "
892 		    "specified multiple times\n"), propname);
893 		return (2);
894 	}
895 
896 	if (nvlist_add_string(proplist, normnm, propval) != 0) {
897 		(void) fprintf(stderr, gettext("internal "
898 		    "error: out of memory\n"));
899 		return (1);
900 	}
901 
902 	return (0);
903 }
904 
905 /*
906  * Set a default property pair (name, string-value) in a property nvlist
907  */
908 static int
909 add_prop_list_default(const char *propname, const char *propval,
910     nvlist_t **props)
911 {
912 	const char *pval;
913 
914 	if (nvlist_lookup_string(*props, propname, &pval) == 0)
915 		return (0);
916 
917 	return (add_prop_list(propname, propval, props, B_TRUE));
918 }
919 
920 /*
921  * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
922  *
923  *	-f	Force addition of devices, even if they appear in use
924  *	-g	Display guid for individual vdev name.
925  *	-L	Follow links when resolving vdev path name.
926  *	-n	Do not add the devices, but display the resulting layout if
927  *		they were to be added.
928  *	-o	Set property=value.
929  *	-P	Display full path for vdev name.
930  *
931  * Adds the given vdevs to 'pool'.  As with create, the bulk of this work is
932  * handled by make_root_vdev(), which constructs the nvlist needed to pass to
933  * libzfs.
934  */
935 int
936 zpool_do_add(int argc, char **argv)
937 {
938 	boolean_t force = B_FALSE;
939 	boolean_t dryrun = B_FALSE;
940 	int name_flags = 0;
941 	int c;
942 	nvlist_t *nvroot;
943 	char *poolname;
944 	int ret;
945 	zpool_handle_t *zhp;
946 	nvlist_t *config;
947 	nvlist_t *props = NULL;
948 	char *propval;
949 
950 	/* check options */
951 	while ((c = getopt(argc, argv, "fgLno:P")) != -1) {
952 		switch (c) {
953 		case 'f':
954 			force = B_TRUE;
955 			break;
956 		case 'g':
957 			name_flags |= VDEV_NAME_GUID;
958 			break;
959 		case 'L':
960 			name_flags |= VDEV_NAME_FOLLOW_LINKS;
961 			break;
962 		case 'n':
963 			dryrun = B_TRUE;
964 			break;
965 		case 'o':
966 			if ((propval = strchr(optarg, '=')) == NULL) {
967 				(void) fprintf(stderr, gettext("missing "
968 				    "'=' for -o option\n"));
969 				usage(B_FALSE);
970 			}
971 			*propval = '\0';
972 			propval++;
973 
974 			if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
975 			    (add_prop_list(optarg, propval, &props, B_TRUE)))
976 				usage(B_FALSE);
977 			break;
978 		case 'P':
979 			name_flags |= VDEV_NAME_PATH;
980 			break;
981 		case '?':
982 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
983 			    optopt);
984 			usage(B_FALSE);
985 		}
986 	}
987 
988 	argc -= optind;
989 	argv += optind;
990 
991 	/* get pool name and check number of arguments */
992 	if (argc < 1) {
993 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
994 		usage(B_FALSE);
995 	}
996 	if (argc < 2) {
997 		(void) fprintf(stderr, gettext("missing vdev specification\n"));
998 		usage(B_FALSE);
999 	}
1000 
1001 	poolname = argv[0];
1002 
1003 	argc--;
1004 	argv++;
1005 
1006 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
1007 		return (1);
1008 
1009 	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
1010 		(void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
1011 		    poolname);
1012 		zpool_close(zhp);
1013 		return (1);
1014 	}
1015 
1016 	/* unless manually specified use "ashift" pool property (if set) */
1017 	if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) {
1018 		int intval;
1019 		zprop_source_t src;
1020 		char strval[ZPOOL_MAXPROPLEN];
1021 
1022 		intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src);
1023 		if (src != ZPROP_SRC_DEFAULT) {
1024 			(void) sprintf(strval, "%" PRId32, intval);
1025 			verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval,
1026 			    &props, B_TRUE) == 0);
1027 		}
1028 	}
1029 
1030 	/* pass off to make_root_vdev for processing */
1031 	nvroot = make_root_vdev(zhp, props, force, !force, B_FALSE, dryrun,
1032 	    argc, argv);
1033 	if (nvroot == NULL) {
1034 		zpool_close(zhp);
1035 		return (1);
1036 	}
1037 
1038 	if (dryrun) {
1039 		nvlist_t *poolnvroot;
1040 		nvlist_t **l2child, **sparechild;
1041 		uint_t l2children, sparechildren, c;
1042 		char *vname;
1043 		boolean_t hadcache = B_FALSE, hadspare = B_FALSE;
1044 
1045 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1046 		    &poolnvroot) == 0);
1047 
1048 		(void) printf(gettext("would update '%s' to the following "
1049 		    "configuration:\n\n"), zpool_get_name(zhp));
1050 
1051 		/* print original main pool and new tree */
1052 		print_vdev_tree(zhp, poolname, poolnvroot, 0, "",
1053 		    name_flags | VDEV_NAME_TYPE_ID);
1054 		print_vdev_tree(zhp, NULL, nvroot, 0, "", name_flags);
1055 
1056 		/* print other classes: 'dedup', 'special', and 'log' */
1057 		if (zfs_special_devs(poolnvroot, VDEV_ALLOC_BIAS_DEDUP)) {
1058 			print_vdev_tree(zhp, "dedup", poolnvroot, 0,
1059 			    VDEV_ALLOC_BIAS_DEDUP, name_flags);
1060 			print_vdev_tree(zhp, NULL, nvroot, 0,
1061 			    VDEV_ALLOC_BIAS_DEDUP, name_flags);
1062 		} else if (zfs_special_devs(nvroot, VDEV_ALLOC_BIAS_DEDUP)) {
1063 			print_vdev_tree(zhp, "dedup", nvroot, 0,
1064 			    VDEV_ALLOC_BIAS_DEDUP, name_flags);
1065 		}
1066 
1067 		if (zfs_special_devs(poolnvroot, VDEV_ALLOC_BIAS_SPECIAL)) {
1068 			print_vdev_tree(zhp, "special", poolnvroot, 0,
1069 			    VDEV_ALLOC_BIAS_SPECIAL, name_flags);
1070 			print_vdev_tree(zhp, NULL, nvroot, 0,
1071 			    VDEV_ALLOC_BIAS_SPECIAL, name_flags);
1072 		} else if (zfs_special_devs(nvroot, VDEV_ALLOC_BIAS_SPECIAL)) {
1073 			print_vdev_tree(zhp, "special", nvroot, 0,
1074 			    VDEV_ALLOC_BIAS_SPECIAL, name_flags);
1075 		}
1076 
1077 		if (num_logs(poolnvroot) > 0) {
1078 			print_vdev_tree(zhp, "logs", poolnvroot, 0,
1079 			    VDEV_ALLOC_BIAS_LOG, name_flags);
1080 			print_vdev_tree(zhp, NULL, nvroot, 0,
1081 			    VDEV_ALLOC_BIAS_LOG, name_flags);
1082 		} else if (num_logs(nvroot) > 0) {
1083 			print_vdev_tree(zhp, "logs", nvroot, 0,
1084 			    VDEV_ALLOC_BIAS_LOG, name_flags);
1085 		}
1086 
1087 		/* Do the same for the caches */
1088 		if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_L2CACHE,
1089 		    &l2child, &l2children) == 0 && l2children) {
1090 			hadcache = B_TRUE;
1091 			(void) printf(gettext("\tcache\n"));
1092 			for (c = 0; c < l2children; c++) {
1093 				vname = zpool_vdev_name(g_zfs, NULL,
1094 				    l2child[c], name_flags);
1095 				(void) printf("\t  %s\n", vname);
1096 				free(vname);
1097 			}
1098 		}
1099 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1100 		    &l2child, &l2children) == 0 && l2children) {
1101 			if (!hadcache)
1102 				(void) printf(gettext("\tcache\n"));
1103 			for (c = 0; c < l2children; c++) {
1104 				vname = zpool_vdev_name(g_zfs, NULL,
1105 				    l2child[c], name_flags);
1106 				(void) printf("\t  %s\n", vname);
1107 				free(vname);
1108 			}
1109 		}
1110 		/* And finally the spares */
1111 		if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_SPARES,
1112 		    &sparechild, &sparechildren) == 0 && sparechildren > 0) {
1113 			hadspare = B_TRUE;
1114 			(void) printf(gettext("\tspares\n"));
1115 			for (c = 0; c < sparechildren; c++) {
1116 				vname = zpool_vdev_name(g_zfs, NULL,
1117 				    sparechild[c], name_flags);
1118 				(void) printf("\t  %s\n", vname);
1119 				free(vname);
1120 			}
1121 		}
1122 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1123 		    &sparechild, &sparechildren) == 0 && sparechildren > 0) {
1124 			if (!hadspare)
1125 				(void) printf(gettext("\tspares\n"));
1126 			for (c = 0; c < sparechildren; c++) {
1127 				vname = zpool_vdev_name(g_zfs, NULL,
1128 				    sparechild[c], name_flags);
1129 				(void) printf("\t  %s\n", vname);
1130 				free(vname);
1131 			}
1132 		}
1133 
1134 		ret = 0;
1135 	} else {
1136 		ret = (zpool_add(zhp, nvroot) != 0);
1137 	}
1138 
1139 	nvlist_free(props);
1140 	nvlist_free(nvroot);
1141 	zpool_close(zhp);
1142 
1143 	return (ret);
1144 }
1145 
1146 /*
1147  * zpool remove [-npsw] <pool> <vdev> ...
1148  *
1149  * Removes the given vdev from the pool.
1150  */
1151 int
1152 zpool_do_remove(int argc, char **argv)
1153 {
1154 	char *poolname;
1155 	int i, ret = 0;
1156 	zpool_handle_t *zhp = NULL;
1157 	boolean_t stop = B_FALSE;
1158 	int c;
1159 	boolean_t noop = B_FALSE;
1160 	boolean_t parsable = B_FALSE;
1161 	boolean_t wait = B_FALSE;
1162 
1163 	/* check options */
1164 	while ((c = getopt(argc, argv, "npsw")) != -1) {
1165 		switch (c) {
1166 		case 'n':
1167 			noop = B_TRUE;
1168 			break;
1169 		case 'p':
1170 			parsable = B_TRUE;
1171 			break;
1172 		case 's':
1173 			stop = B_TRUE;
1174 			break;
1175 		case 'w':
1176 			wait = B_TRUE;
1177 			break;
1178 		case '?':
1179 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1180 			    optopt);
1181 			usage(B_FALSE);
1182 		}
1183 	}
1184 
1185 	argc -= optind;
1186 	argv += optind;
1187 
1188 	/* get pool name and check number of arguments */
1189 	if (argc < 1) {
1190 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
1191 		usage(B_FALSE);
1192 	}
1193 
1194 	poolname = argv[0];
1195 
1196 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
1197 		return (1);
1198 
1199 	if (stop && noop) {
1200 		zpool_close(zhp);
1201 		(void) fprintf(stderr, gettext("stop request ignored\n"));
1202 		return (0);
1203 	}
1204 
1205 	if (stop) {
1206 		if (argc > 1) {
1207 			(void) fprintf(stderr, gettext("too many arguments\n"));
1208 			usage(B_FALSE);
1209 		}
1210 		if (zpool_vdev_remove_cancel(zhp) != 0)
1211 			ret = 1;
1212 		if (wait) {
1213 			(void) fprintf(stderr, gettext("invalid option "
1214 			    "combination: -w cannot be used with -s\n"));
1215 			usage(B_FALSE);
1216 		}
1217 	} else {
1218 		if (argc < 2) {
1219 			(void) fprintf(stderr, gettext("missing device\n"));
1220 			usage(B_FALSE);
1221 		}
1222 
1223 		for (i = 1; i < argc; i++) {
1224 			if (noop) {
1225 				uint64_t size;
1226 
1227 				if (zpool_vdev_indirect_size(zhp, argv[i],
1228 				    &size) != 0) {
1229 					ret = 1;
1230 					break;
1231 				}
1232 				if (parsable) {
1233 					(void) printf("%s %llu\n",
1234 					    argv[i], (unsigned long long)size);
1235 				} else {
1236 					char valstr[32];
1237 					zfs_nicenum(size, valstr,
1238 					    sizeof (valstr));
1239 					(void) printf("Memory that will be "
1240 					    "used after removing %s: %s\n",
1241 					    argv[i], valstr);
1242 				}
1243 			} else {
1244 				if (zpool_vdev_remove(zhp, argv[i]) != 0)
1245 					ret = 1;
1246 			}
1247 		}
1248 
1249 		if (ret == 0 && wait)
1250 			ret = zpool_wait(zhp, ZPOOL_WAIT_REMOVE);
1251 	}
1252 	zpool_close(zhp);
1253 
1254 	return (ret);
1255 }
1256 
1257 /*
1258  * Return 1 if a vdev is active (being used in a pool)
1259  * Return 0 if a vdev is inactive (offlined or faulted, or not in active pool)
1260  *
1261  * This is useful for checking if a disk in an active pool is offlined or
1262  * faulted.
1263  */
1264 static int
1265 vdev_is_active(char *vdev_path)
1266 {
1267 	int fd;
1268 	fd = open(vdev_path, O_EXCL);
1269 	if (fd < 0) {
1270 		return (1);   /* cant open O_EXCL - disk is active */
1271 	}
1272 
1273 	close(fd);
1274 	return (0);   /* disk is inactive in the pool */
1275 }
1276 
1277 /*
1278  * zpool labelclear [-f] <vdev>
1279  *
1280  *	-f	Force clearing the label for the vdevs which are members of
1281  *		the exported or foreign pools.
1282  *
1283  * Verifies that the vdev is not active and zeros out the label information
1284  * on the device.
1285  */
1286 int
1287 zpool_do_labelclear(int argc, char **argv)
1288 {
1289 	char vdev[MAXPATHLEN];
1290 	char *name = NULL;
1291 	int c, fd = -1, ret = 0;
1292 	nvlist_t *config;
1293 	pool_state_t state;
1294 	boolean_t inuse = B_FALSE;
1295 	boolean_t force = B_FALSE;
1296 
1297 	/* check options */
1298 	while ((c = getopt(argc, argv, "f")) != -1) {
1299 		switch (c) {
1300 		case 'f':
1301 			force = B_TRUE;
1302 			break;
1303 		default:
1304 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1305 			    optopt);
1306 			usage(B_FALSE);
1307 		}
1308 	}
1309 
1310 	argc -= optind;
1311 	argv += optind;
1312 
1313 	/* get vdev name */
1314 	if (argc < 1) {
1315 		(void) fprintf(stderr, gettext("missing vdev name\n"));
1316 		usage(B_FALSE);
1317 	}
1318 	if (argc > 1) {
1319 		(void) fprintf(stderr, gettext("too many arguments\n"));
1320 		usage(B_FALSE);
1321 	}
1322 
1323 	(void) strlcpy(vdev, argv[0], sizeof (vdev));
1324 
1325 	/*
1326 	 * If we cannot open an absolute path, we quit.
1327 	 * Otherwise if the provided vdev name doesn't point to a file,
1328 	 * try prepending expected disk paths and partition numbers.
1329 	 */
1330 	if ((fd = open(vdev, O_RDWR)) < 0) {
1331 		int error;
1332 		if (vdev[0] == '/') {
1333 			(void) fprintf(stderr, gettext("failed to open "
1334 			    "%s: %s\n"), vdev, strerror(errno));
1335 			return (1);
1336 		}
1337 
1338 		error = zfs_resolve_shortname(argv[0], vdev, MAXPATHLEN);
1339 		if (error == 0 && zfs_dev_is_whole_disk(vdev)) {
1340 			if (zfs_append_partition(vdev, MAXPATHLEN) == -1)
1341 				error = ENOENT;
1342 		}
1343 
1344 		if (error || ((fd = open(vdev, O_RDWR)) < 0)) {
1345 			if (errno == ENOENT) {
1346 				(void) fprintf(stderr, gettext(
1347 				    "failed to find device %s, try "
1348 				    "specifying absolute path instead\n"),
1349 				    argv[0]);
1350 				return (1);
1351 			}
1352 
1353 			(void) fprintf(stderr, gettext("failed to open %s:"
1354 			    " %s\n"), vdev, strerror(errno));
1355 			return (1);
1356 		}
1357 	}
1358 
1359 	/*
1360 	 * Flush all dirty pages for the block device.  This should not be
1361 	 * fatal when the device does not support BLKFLSBUF as would be the
1362 	 * case for a file vdev.
1363 	 */
1364 	if ((zfs_dev_flush(fd) != 0) && (errno != ENOTTY))
1365 		(void) fprintf(stderr, gettext("failed to invalidate "
1366 		    "cache for %s: %s\n"), vdev, strerror(errno));
1367 
1368 	if (zpool_read_label(fd, &config, NULL) != 0) {
1369 		(void) fprintf(stderr,
1370 		    gettext("failed to read label from %s\n"), vdev);
1371 		ret = 1;
1372 		goto errout;
1373 	}
1374 	nvlist_free(config);
1375 
1376 	ret = zpool_in_use(g_zfs, fd, &state, &name, &inuse);
1377 	if (ret != 0) {
1378 		(void) fprintf(stderr,
1379 		    gettext("failed to check state for %s\n"), vdev);
1380 		ret = 1;
1381 		goto errout;
1382 	}
1383 
1384 	if (!inuse)
1385 		goto wipe_label;
1386 
1387 	switch (state) {
1388 	default:
1389 	case POOL_STATE_ACTIVE:
1390 	case POOL_STATE_SPARE:
1391 	case POOL_STATE_L2CACHE:
1392 		/*
1393 		 * We allow the user to call 'zpool offline -f'
1394 		 * on an offlined disk in an active pool. We can check if
1395 		 * the disk is online by calling vdev_is_active().
1396 		 */
1397 		if (force && !vdev_is_active(vdev))
1398 			break;
1399 
1400 		(void) fprintf(stderr, gettext(
1401 		    "%s is a member (%s) of pool \"%s\""),
1402 		    vdev, zpool_pool_state_to_name(state), name);
1403 
1404 		if (force) {
1405 			(void) fprintf(stderr, gettext(
1406 			    ". Offline the disk first to clear its label."));
1407 		}
1408 		printf("\n");
1409 		ret = 1;
1410 		goto errout;
1411 
1412 	case POOL_STATE_EXPORTED:
1413 		if (force)
1414 			break;
1415 		(void) fprintf(stderr, gettext(
1416 		    "use '-f' to override the following error:\n"
1417 		    "%s is a member of exported pool \"%s\"\n"),
1418 		    vdev, name);
1419 		ret = 1;
1420 		goto errout;
1421 
1422 	case POOL_STATE_POTENTIALLY_ACTIVE:
1423 		if (force)
1424 			break;
1425 		(void) fprintf(stderr, gettext(
1426 		    "use '-f' to override the following error:\n"
1427 		    "%s is a member of potentially active pool \"%s\"\n"),
1428 		    vdev, name);
1429 		ret = 1;
1430 		goto errout;
1431 
1432 	case POOL_STATE_DESTROYED:
1433 		/* inuse should never be set for a destroyed pool */
1434 		assert(0);
1435 		break;
1436 	}
1437 
1438 wipe_label:
1439 	ret = zpool_clear_label(fd);
1440 	if (ret != 0) {
1441 		(void) fprintf(stderr,
1442 		    gettext("failed to clear label for %s\n"), vdev);
1443 	}
1444 
1445 errout:
1446 	free(name);
1447 	(void) close(fd);
1448 
1449 	return (ret);
1450 }
1451 
1452 /*
1453  * zpool create [-fnd] [-o property=value] ...
1454  *		[-O file-system-property=value] ...
1455  *		[-R root] [-m mountpoint] <pool> <dev> ...
1456  *
1457  *	-f	Force creation, even if devices appear in use
1458  *	-n	Do not create the pool, but display the resulting layout if it
1459  *		were to be created.
1460  *      -R	Create a pool under an alternate root
1461  *      -m	Set default mountpoint for the root dataset.  By default it's
1462  *		'/<pool>'
1463  *	-o	Set property=value.
1464  *	-o	Set feature@feature=enabled|disabled.
1465  *	-d	Don't automatically enable all supported pool features
1466  *		(individual features can be enabled with -o).
1467  *	-O	Set fsproperty=value in the pool's root file system
1468  *
1469  * Creates the named pool according to the given vdev specification.  The
1470  * bulk of the vdev processing is done in make_root_vdev() in zpool_vdev.c.
1471  * Once we get the nvlist back from make_root_vdev(), we either print out the
1472  * contents (if '-n' was specified), or pass it to libzfs to do the creation.
1473  */
1474 int
1475 zpool_do_create(int argc, char **argv)
1476 {
1477 	boolean_t force = B_FALSE;
1478 	boolean_t dryrun = B_FALSE;
1479 	boolean_t enable_pool_features = B_TRUE;
1480 
1481 	int c;
1482 	nvlist_t *nvroot = NULL;
1483 	char *poolname;
1484 	char *tname = NULL;
1485 	int ret = 1;
1486 	char *altroot = NULL;
1487 	char *compat = NULL;
1488 	char *mountpoint = NULL;
1489 	nvlist_t *fsprops = NULL;
1490 	nvlist_t *props = NULL;
1491 	char *propval;
1492 
1493 	/* check options */
1494 	while ((c = getopt(argc, argv, ":fndR:m:o:O:t:")) != -1) {
1495 		switch (c) {
1496 		case 'f':
1497 			force = B_TRUE;
1498 			break;
1499 		case 'n':
1500 			dryrun = B_TRUE;
1501 			break;
1502 		case 'd':
1503 			enable_pool_features = B_FALSE;
1504 			break;
1505 		case 'R':
1506 			altroot = optarg;
1507 			if (add_prop_list(zpool_prop_to_name(
1508 			    ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1509 				goto errout;
1510 			if (add_prop_list_default(zpool_prop_to_name(
1511 			    ZPOOL_PROP_CACHEFILE), "none", &props))
1512 				goto errout;
1513 			break;
1514 		case 'm':
1515 			/* Equivalent to -O mountpoint=optarg */
1516 			mountpoint = optarg;
1517 			break;
1518 		case 'o':
1519 			if ((propval = strchr(optarg, '=')) == NULL) {
1520 				(void) fprintf(stderr, gettext("missing "
1521 				    "'=' for -o option\n"));
1522 				goto errout;
1523 			}
1524 			*propval = '\0';
1525 			propval++;
1526 
1527 			if (add_prop_list(optarg, propval, &props, B_TRUE))
1528 				goto errout;
1529 
1530 			/*
1531 			 * If the user is creating a pool that doesn't support
1532 			 * feature flags, don't enable any features.
1533 			 */
1534 			if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
1535 				char *end;
1536 				u_longlong_t ver;
1537 
1538 				ver = strtoull(propval, &end, 10);
1539 				if (*end == '\0' &&
1540 				    ver < SPA_VERSION_FEATURES) {
1541 					enable_pool_features = B_FALSE;
1542 				}
1543 			}
1544 			if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT)
1545 				altroot = propval;
1546 			if (zpool_name_to_prop(optarg) ==
1547 			    ZPOOL_PROP_COMPATIBILITY)
1548 				compat = propval;
1549 			break;
1550 		case 'O':
1551 			if ((propval = strchr(optarg, '=')) == NULL) {
1552 				(void) fprintf(stderr, gettext("missing "
1553 				    "'=' for -O option\n"));
1554 				goto errout;
1555 			}
1556 			*propval = '\0';
1557 			propval++;
1558 
1559 			/*
1560 			 * Mountpoints are checked and then added later.
1561 			 * Uniquely among properties, they can be specified
1562 			 * more than once, to avoid conflict with -m.
1563 			 */
1564 			if (0 == strcmp(optarg,
1565 			    zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
1566 				mountpoint = propval;
1567 			} else if (add_prop_list(optarg, propval, &fsprops,
1568 			    B_FALSE)) {
1569 				goto errout;
1570 			}
1571 			break;
1572 		case 't':
1573 			/*
1574 			 * Sanity check temporary pool name.
1575 			 */
1576 			if (strchr(optarg, '/') != NULL) {
1577 				(void) fprintf(stderr, gettext("cannot create "
1578 				    "'%s': invalid character '/' in temporary "
1579 				    "name\n"), optarg);
1580 				(void) fprintf(stderr, gettext("use 'zfs "
1581 				    "create' to create a dataset\n"));
1582 				goto errout;
1583 			}
1584 
1585 			if (add_prop_list(zpool_prop_to_name(
1586 			    ZPOOL_PROP_TNAME), optarg, &props, B_TRUE))
1587 				goto errout;
1588 			if (add_prop_list_default(zpool_prop_to_name(
1589 			    ZPOOL_PROP_CACHEFILE), "none", &props))
1590 				goto errout;
1591 			tname = optarg;
1592 			break;
1593 		case ':':
1594 			(void) fprintf(stderr, gettext("missing argument for "
1595 			    "'%c' option\n"), optopt);
1596 			goto badusage;
1597 		case '?':
1598 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1599 			    optopt);
1600 			goto badusage;
1601 		}
1602 	}
1603 
1604 	argc -= optind;
1605 	argv += optind;
1606 
1607 	/* get pool name and check number of arguments */
1608 	if (argc < 1) {
1609 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
1610 		goto badusage;
1611 	}
1612 	if (argc < 2) {
1613 		(void) fprintf(stderr, gettext("missing vdev specification\n"));
1614 		goto badusage;
1615 	}
1616 
1617 	poolname = argv[0];
1618 
1619 	/*
1620 	 * As a special case, check for use of '/' in the name, and direct the
1621 	 * user to use 'zfs create' instead.
1622 	 */
1623 	if (strchr(poolname, '/') != NULL) {
1624 		(void) fprintf(stderr, gettext("cannot create '%s': invalid "
1625 		    "character '/' in pool name\n"), poolname);
1626 		(void) fprintf(stderr, gettext("use 'zfs create' to "
1627 		    "create a dataset\n"));
1628 		goto errout;
1629 	}
1630 
1631 	/* pass off to make_root_vdev for bulk processing */
1632 	nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun,
1633 	    argc - 1, argv + 1);
1634 	if (nvroot == NULL)
1635 		goto errout;
1636 
1637 	/* make_root_vdev() allows 0 toplevel children if there are spares */
1638 	if (!zfs_allocatable_devs(nvroot)) {
1639 		(void) fprintf(stderr, gettext("invalid vdev "
1640 		    "specification: at least one toplevel vdev must be "
1641 		    "specified\n"));
1642 		goto errout;
1643 	}
1644 
1645 	if (altroot != NULL && altroot[0] != '/') {
1646 		(void) fprintf(stderr, gettext("invalid alternate root '%s': "
1647 		    "must be an absolute path\n"), altroot);
1648 		goto errout;
1649 	}
1650 
1651 	/*
1652 	 * Check the validity of the mountpoint and direct the user to use the
1653 	 * '-m' mountpoint option if it looks like its in use.
1654 	 */
1655 	if (mountpoint == NULL ||
1656 	    (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
1657 	    strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
1658 		char buf[MAXPATHLEN];
1659 		DIR *dirp;
1660 
1661 		if (mountpoint && mountpoint[0] != '/') {
1662 			(void) fprintf(stderr, gettext("invalid mountpoint "
1663 			    "'%s': must be an absolute path, 'legacy', or "
1664 			    "'none'\n"), mountpoint);
1665 			goto errout;
1666 		}
1667 
1668 		if (mountpoint == NULL) {
1669 			if (altroot != NULL)
1670 				(void) snprintf(buf, sizeof (buf), "%s/%s",
1671 				    altroot, poolname);
1672 			else
1673 				(void) snprintf(buf, sizeof (buf), "/%s",
1674 				    poolname);
1675 		} else {
1676 			if (altroot != NULL)
1677 				(void) snprintf(buf, sizeof (buf), "%s%s",
1678 				    altroot, mountpoint);
1679 			else
1680 				(void) snprintf(buf, sizeof (buf), "%s",
1681 				    mountpoint);
1682 		}
1683 
1684 		if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
1685 			(void) fprintf(stderr, gettext("mountpoint '%s' : "
1686 			    "%s\n"), buf, strerror(errno));
1687 			(void) fprintf(stderr, gettext("use '-m' "
1688 			    "option to provide a different default\n"));
1689 			goto errout;
1690 		} else if (dirp) {
1691 			int count = 0;
1692 
1693 			while (count < 3 && readdir(dirp) != NULL)
1694 				count++;
1695 			(void) closedir(dirp);
1696 
1697 			if (count > 2) {
1698 				(void) fprintf(stderr, gettext("mountpoint "
1699 				    "'%s' exists and is not empty\n"), buf);
1700 				(void) fprintf(stderr, gettext("use '-m' "
1701 				    "option to provide a "
1702 				    "different default\n"));
1703 				goto errout;
1704 			}
1705 		}
1706 	}
1707 
1708 	/*
1709 	 * Now that the mountpoint's validity has been checked, ensure that
1710 	 * the property is set appropriately prior to creating the pool.
1711 	 */
1712 	if (mountpoint != NULL) {
1713 		ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1714 		    mountpoint, &fsprops, B_FALSE);
1715 		if (ret != 0)
1716 			goto errout;
1717 	}
1718 
1719 	ret = 1;
1720 	if (dryrun) {
1721 		/*
1722 		 * For a dry run invocation, print out a basic message and run
1723 		 * through all the vdevs in the list and print out in an
1724 		 * appropriate hierarchy.
1725 		 */
1726 		(void) printf(gettext("would create '%s' with the "
1727 		    "following layout:\n\n"), poolname);
1728 
1729 		print_vdev_tree(NULL, poolname, nvroot, 0, "", 0);
1730 		print_vdev_tree(NULL, "dedup", nvroot, 0,
1731 		    VDEV_ALLOC_BIAS_DEDUP, 0);
1732 		print_vdev_tree(NULL, "special", nvroot, 0,
1733 		    VDEV_ALLOC_BIAS_SPECIAL, 0);
1734 		print_vdev_tree(NULL, "logs", nvroot, 0,
1735 		    VDEV_ALLOC_BIAS_LOG, 0);
1736 		print_cache_list(nvroot, 0);
1737 		print_spare_list(nvroot, 0);
1738 
1739 		ret = 0;
1740 	} else {
1741 		/*
1742 		 * Load in feature set.
1743 		 * Note: if compatibility property not given, we'll have
1744 		 * NULL, which means 'all features'.
1745 		 */
1746 		boolean_t requested_features[SPA_FEATURES];
1747 		if (zpool_do_load_compat(compat, requested_features) !=
1748 		    ZPOOL_COMPATIBILITY_OK)
1749 			goto errout;
1750 
1751 		/*
1752 		 * props contains list of features to enable.
1753 		 * For each feature:
1754 		 *  - remove it if feature@name=disabled
1755 		 *  - leave it there if feature@name=enabled
1756 		 *  - add it if:
1757 		 *    - enable_pool_features (ie: no '-d' or '-o version')
1758 		 *    - it's supported by the kernel module
1759 		 *    - it's in the requested feature set
1760 		 *  - warn if it's enabled but not in compat
1761 		 */
1762 		for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
1763 			char propname[MAXPATHLEN];
1764 			const char *propval;
1765 			zfeature_info_t *feat = &spa_feature_table[i];
1766 
1767 			(void) snprintf(propname, sizeof (propname),
1768 			    "feature@%s", feat->fi_uname);
1769 
1770 			if (!nvlist_lookup_string(props, propname, &propval)) {
1771 				if (strcmp(propval,
1772 				    ZFS_FEATURE_DISABLED) == 0) {
1773 					(void) nvlist_remove_all(props,
1774 					    propname);
1775 				} else if (strcmp(propval,
1776 				    ZFS_FEATURE_ENABLED) == 0 &&
1777 				    !requested_features[i]) {
1778 					(void) fprintf(stderr, gettext(
1779 					    "Warning: feature \"%s\" enabled "
1780 					    "but is not in specified "
1781 					    "'compatibility' feature set.\n"),
1782 					    feat->fi_uname);
1783 				}
1784 			} else if (
1785 			    enable_pool_features &&
1786 			    feat->fi_zfs_mod_supported &&
1787 			    requested_features[i]) {
1788 				ret = add_prop_list(propname,
1789 				    ZFS_FEATURE_ENABLED, &props, B_TRUE);
1790 				if (ret != 0)
1791 					goto errout;
1792 			}
1793 		}
1794 
1795 		ret = 1;
1796 		if (zpool_create(g_zfs, poolname,
1797 		    nvroot, props, fsprops) == 0) {
1798 			zfs_handle_t *pool = zfs_open(g_zfs,
1799 			    tname ? tname : poolname, ZFS_TYPE_FILESYSTEM);
1800 			if (pool != NULL) {
1801 				if (zfs_mount(pool, NULL, 0) == 0) {
1802 					ret = zfs_share(pool, NULL);
1803 					zfs_commit_shares(NULL);
1804 				}
1805 				zfs_close(pool);
1806 			}
1807 		} else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1808 			(void) fprintf(stderr, gettext("pool name may have "
1809 			    "been omitted\n"));
1810 		}
1811 	}
1812 
1813 errout:
1814 	nvlist_free(nvroot);
1815 	nvlist_free(fsprops);
1816 	nvlist_free(props);
1817 	return (ret);
1818 badusage:
1819 	nvlist_free(fsprops);
1820 	nvlist_free(props);
1821 	usage(B_FALSE);
1822 	return (2);
1823 }
1824 
1825 /*
1826  * zpool destroy <pool>
1827  *
1828  * 	-f	Forcefully unmount any datasets
1829  *
1830  * Destroy the given pool.  Automatically unmounts any datasets in the pool.
1831  */
1832 int
1833 zpool_do_destroy(int argc, char **argv)
1834 {
1835 	boolean_t force = B_FALSE;
1836 	int c;
1837 	char *pool;
1838 	zpool_handle_t *zhp;
1839 	int ret;
1840 
1841 	/* check options */
1842 	while ((c = getopt(argc, argv, "f")) != -1) {
1843 		switch (c) {
1844 		case 'f':
1845 			force = B_TRUE;
1846 			break;
1847 		case '?':
1848 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1849 			    optopt);
1850 			usage(B_FALSE);
1851 		}
1852 	}
1853 
1854 	argc -= optind;
1855 	argv += optind;
1856 
1857 	/* check arguments */
1858 	if (argc < 1) {
1859 		(void) fprintf(stderr, gettext("missing pool argument\n"));
1860 		usage(B_FALSE);
1861 	}
1862 	if (argc > 1) {
1863 		(void) fprintf(stderr, gettext("too many arguments\n"));
1864 		usage(B_FALSE);
1865 	}
1866 
1867 	pool = argv[0];
1868 
1869 	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1870 		/*
1871 		 * As a special case, check for use of '/' in the name, and
1872 		 * direct the user to use 'zfs destroy' instead.
1873 		 */
1874 		if (strchr(pool, '/') != NULL)
1875 			(void) fprintf(stderr, gettext("use 'zfs destroy' to "
1876 			    "destroy a dataset\n"));
1877 		return (1);
1878 	}
1879 
1880 	if (zpool_disable_datasets(zhp, force) != 0) {
1881 		(void) fprintf(stderr, gettext("could not destroy '%s': "
1882 		    "could not unmount datasets\n"), zpool_get_name(zhp));
1883 		zpool_close(zhp);
1884 		return (1);
1885 	}
1886 
1887 	/* The history must be logged as part of the export */
1888 	log_history = B_FALSE;
1889 
1890 	ret = (zpool_destroy(zhp, history_str) != 0);
1891 
1892 	zpool_close(zhp);
1893 
1894 	return (ret);
1895 }
1896 
1897 typedef struct export_cbdata {
1898 	boolean_t force;
1899 	boolean_t hardforce;
1900 } export_cbdata_t;
1901 
1902 /*
1903  * Export one pool
1904  */
1905 static int
1906 zpool_export_one(zpool_handle_t *zhp, void *data)
1907 {
1908 	export_cbdata_t *cb = data;
1909 
1910 	if (zpool_disable_datasets(zhp, cb->force) != 0)
1911 		return (1);
1912 
1913 	/* The history must be logged as part of the export */
1914 	log_history = B_FALSE;
1915 
1916 	if (cb->hardforce) {
1917 		if (zpool_export_force(zhp, history_str) != 0)
1918 			return (1);
1919 	} else if (zpool_export(zhp, cb->force, history_str) != 0) {
1920 		return (1);
1921 	}
1922 
1923 	return (0);
1924 }
1925 
1926 /*
1927  * zpool export [-f] <pool> ...
1928  *
1929  *	-a	Export all pools
1930  *	-f	Forcefully unmount datasets
1931  *
1932  * Export the given pools.  By default, the command will attempt to cleanly
1933  * unmount any active datasets within the pool.  If the '-f' flag is specified,
1934  * then the datasets will be forcefully unmounted.
1935  */
1936 int
1937 zpool_do_export(int argc, char **argv)
1938 {
1939 	export_cbdata_t cb;
1940 	boolean_t do_all = B_FALSE;
1941 	boolean_t force = B_FALSE;
1942 	boolean_t hardforce = B_FALSE;
1943 	int c, ret;
1944 
1945 	/* check options */
1946 	while ((c = getopt(argc, argv, "afF")) != -1) {
1947 		switch (c) {
1948 		case 'a':
1949 			do_all = B_TRUE;
1950 			break;
1951 		case 'f':
1952 			force = B_TRUE;
1953 			break;
1954 		case 'F':
1955 			hardforce = B_TRUE;
1956 			break;
1957 		case '?':
1958 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1959 			    optopt);
1960 			usage(B_FALSE);
1961 		}
1962 	}
1963 
1964 	cb.force = force;
1965 	cb.hardforce = hardforce;
1966 	argc -= optind;
1967 	argv += optind;
1968 
1969 	if (do_all) {
1970 		if (argc != 0) {
1971 			(void) fprintf(stderr, gettext("too many arguments\n"));
1972 			usage(B_FALSE);
1973 		}
1974 
1975 		return (for_each_pool(argc, argv, B_TRUE, NULL,
1976 		    ZFS_TYPE_POOL, B_FALSE, zpool_export_one, &cb));
1977 	}
1978 
1979 	/* check arguments */
1980 	if (argc < 1) {
1981 		(void) fprintf(stderr, gettext("missing pool argument\n"));
1982 		usage(B_FALSE);
1983 	}
1984 
1985 	ret = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
1986 	    B_FALSE, zpool_export_one, &cb);
1987 
1988 	return (ret);
1989 }
1990 
1991 /*
1992  * Given a vdev configuration, determine the maximum width needed for the device
1993  * name column.
1994  */
1995 static int
1996 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max,
1997     int name_flags)
1998 {
1999 	static const char *const subtypes[] =
2000 	    {ZPOOL_CONFIG_SPARES, ZPOOL_CONFIG_L2CACHE, ZPOOL_CONFIG_CHILDREN};
2001 
2002 	char *name = zpool_vdev_name(g_zfs, zhp, nv, name_flags);
2003 	max = MAX(strlen(name) + depth, max);
2004 	free(name);
2005 
2006 	nvlist_t **child;
2007 	uint_t children;
2008 	for (size_t i = 0; i < ARRAY_SIZE(subtypes); ++i)
2009 		if (nvlist_lookup_nvlist_array(nv, subtypes[i],
2010 		    &child, &children) == 0)
2011 			for (uint_t c = 0; c < children; ++c)
2012 				max = MAX(max_width(zhp, child[c], depth + 2,
2013 				    max, name_flags), max);
2014 
2015 	return (max);
2016 }
2017 
2018 typedef struct spare_cbdata {
2019 	uint64_t	cb_guid;
2020 	zpool_handle_t	*cb_zhp;
2021 } spare_cbdata_t;
2022 
2023 static boolean_t
2024 find_vdev(nvlist_t *nv, uint64_t search)
2025 {
2026 	uint64_t guid;
2027 	nvlist_t **child;
2028 	uint_t c, children;
2029 
2030 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
2031 	    search == guid)
2032 		return (B_TRUE);
2033 
2034 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2035 	    &child, &children) == 0) {
2036 		for (c = 0; c < children; c++)
2037 			if (find_vdev(child[c], search))
2038 				return (B_TRUE);
2039 	}
2040 
2041 	return (B_FALSE);
2042 }
2043 
2044 static int
2045 find_spare(zpool_handle_t *zhp, void *data)
2046 {
2047 	spare_cbdata_t *cbp = data;
2048 	nvlist_t *config, *nvroot;
2049 
2050 	config = zpool_get_config(zhp, NULL);
2051 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2052 	    &nvroot) == 0);
2053 
2054 	if (find_vdev(nvroot, cbp->cb_guid)) {
2055 		cbp->cb_zhp = zhp;
2056 		return (1);
2057 	}
2058 
2059 	zpool_close(zhp);
2060 	return (0);
2061 }
2062 
2063 typedef struct status_cbdata {
2064 	int		cb_count;
2065 	int		cb_name_flags;
2066 	int		cb_namewidth;
2067 	boolean_t	cb_allpools;
2068 	boolean_t	cb_verbose;
2069 	boolean_t	cb_literal;
2070 	boolean_t	cb_explain;
2071 	boolean_t	cb_first;
2072 	boolean_t	cb_dedup_stats;
2073 	boolean_t	cb_print_status;
2074 	boolean_t	cb_print_slow_ios;
2075 	boolean_t	cb_print_vdev_init;
2076 	boolean_t	cb_print_vdev_trim;
2077 	vdev_cmd_data_list_t	*vcdl;
2078 } status_cbdata_t;
2079 
2080 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
2081 static boolean_t
2082 is_blank_str(const char *str)
2083 {
2084 	for (; str != NULL && *str != '\0'; ++str)
2085 		if (!isblank(*str))
2086 			return (B_FALSE);
2087 	return (B_TRUE);
2088 }
2089 
2090 /* Print command output lines for specific vdev in a specific pool */
2091 static void
2092 zpool_print_cmd(vdev_cmd_data_list_t *vcdl, const char *pool, const char *path)
2093 {
2094 	vdev_cmd_data_t *data;
2095 	int i, j;
2096 	const char *val;
2097 
2098 	for (i = 0; i < vcdl->count; i++) {
2099 		if ((strcmp(vcdl->data[i].path, path) != 0) ||
2100 		    (strcmp(vcdl->data[i].pool, pool) != 0)) {
2101 			/* Not the vdev we're looking for */
2102 			continue;
2103 		}
2104 
2105 		data = &vcdl->data[i];
2106 		/* Print out all the output values for this vdev */
2107 		for (j = 0; j < vcdl->uniq_cols_cnt; j++) {
2108 			val = NULL;
2109 			/* Does this vdev have values for this column? */
2110 			for (int k = 0; k < data->cols_cnt; k++) {
2111 				if (strcmp(data->cols[k],
2112 				    vcdl->uniq_cols[j]) == 0) {
2113 					/* yes it does, record the value */
2114 					val = data->lines[k];
2115 					break;
2116 				}
2117 			}
2118 			/*
2119 			 * Mark empty values with dashes to make output
2120 			 * awk-able.
2121 			 */
2122 			if (val == NULL || is_blank_str(val))
2123 				val = "-";
2124 
2125 			printf("%*s", vcdl->uniq_cols_width[j], val);
2126 			if (j < vcdl->uniq_cols_cnt - 1)
2127 				fputs("  ", stdout);
2128 		}
2129 
2130 		/* Print out any values that aren't in a column at the end */
2131 		for (j = data->cols_cnt; j < data->lines_cnt; j++) {
2132 			/* Did we have any columns?  If so print a spacer. */
2133 			if (vcdl->uniq_cols_cnt > 0)
2134 				fputs("  ", stdout);
2135 
2136 			val = data->lines[j];
2137 			fputs(val ?: "", stdout);
2138 		}
2139 		break;
2140 	}
2141 }
2142 
2143 /*
2144  * Print vdev initialization status for leaves
2145  */
2146 static void
2147 print_status_initialize(vdev_stat_t *vs, boolean_t verbose)
2148 {
2149 	if (verbose) {
2150 		if ((vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE ||
2151 		    vs->vs_initialize_state == VDEV_INITIALIZE_SUSPENDED ||
2152 		    vs->vs_initialize_state == VDEV_INITIALIZE_COMPLETE) &&
2153 		    !vs->vs_scan_removing) {
2154 			char zbuf[1024];
2155 			char tbuf[256];
2156 			struct tm zaction_ts;
2157 
2158 			time_t t = vs->vs_initialize_action_time;
2159 			int initialize_pct = 100;
2160 			if (vs->vs_initialize_state !=
2161 			    VDEV_INITIALIZE_COMPLETE) {
2162 				initialize_pct = (vs->vs_initialize_bytes_done *
2163 				    100 / (vs->vs_initialize_bytes_est + 1));
2164 			}
2165 
2166 			(void) localtime_r(&t, &zaction_ts);
2167 			(void) strftime(tbuf, sizeof (tbuf), "%c", &zaction_ts);
2168 
2169 			switch (vs->vs_initialize_state) {
2170 			case VDEV_INITIALIZE_SUSPENDED:
2171 				(void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
2172 				    gettext("suspended, started at"), tbuf);
2173 				break;
2174 			case VDEV_INITIALIZE_ACTIVE:
2175 				(void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
2176 				    gettext("started at"), tbuf);
2177 				break;
2178 			case VDEV_INITIALIZE_COMPLETE:
2179 				(void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
2180 				    gettext("completed at"), tbuf);
2181 				break;
2182 			}
2183 
2184 			(void) printf(gettext("  (%d%% initialized%s)"),
2185 			    initialize_pct, zbuf);
2186 		} else {
2187 			(void) printf(gettext("  (uninitialized)"));
2188 		}
2189 	} else if (vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE) {
2190 		(void) printf(gettext("  (initializing)"));
2191 	}
2192 }
2193 
2194 /*
2195  * Print vdev TRIM status for leaves
2196  */
2197 static void
2198 print_status_trim(vdev_stat_t *vs, boolean_t verbose)
2199 {
2200 	if (verbose) {
2201 		if ((vs->vs_trim_state == VDEV_TRIM_ACTIVE ||
2202 		    vs->vs_trim_state == VDEV_TRIM_SUSPENDED ||
2203 		    vs->vs_trim_state == VDEV_TRIM_COMPLETE) &&
2204 		    !vs->vs_scan_removing) {
2205 			char zbuf[1024];
2206 			char tbuf[256];
2207 			struct tm zaction_ts;
2208 
2209 			time_t t = vs->vs_trim_action_time;
2210 			int trim_pct = 100;
2211 			if (vs->vs_trim_state != VDEV_TRIM_COMPLETE) {
2212 				trim_pct = (vs->vs_trim_bytes_done *
2213 				    100 / (vs->vs_trim_bytes_est + 1));
2214 			}
2215 
2216 			(void) localtime_r(&t, &zaction_ts);
2217 			(void) strftime(tbuf, sizeof (tbuf), "%c", &zaction_ts);
2218 
2219 			switch (vs->vs_trim_state) {
2220 			case VDEV_TRIM_SUSPENDED:
2221 				(void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
2222 				    gettext("suspended, started at"), tbuf);
2223 				break;
2224 			case VDEV_TRIM_ACTIVE:
2225 				(void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
2226 				    gettext("started at"), tbuf);
2227 				break;
2228 			case VDEV_TRIM_COMPLETE:
2229 				(void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
2230 				    gettext("completed at"), tbuf);
2231 				break;
2232 			}
2233 
2234 			(void) printf(gettext("  (%d%% trimmed%s)"),
2235 			    trim_pct, zbuf);
2236 		} else if (vs->vs_trim_notsup) {
2237 			(void) printf(gettext("  (trim unsupported)"));
2238 		} else {
2239 			(void) printf(gettext("  (untrimmed)"));
2240 		}
2241 	} else if (vs->vs_trim_state == VDEV_TRIM_ACTIVE) {
2242 		(void) printf(gettext("  (trimming)"));
2243 	}
2244 }
2245 
2246 /*
2247  * Return the color associated with a health string.  This includes returning
2248  * NULL for no color change.
2249  */
2250 static const char *
2251 health_str_to_color(const char *health)
2252 {
2253 	if (strcmp(health, gettext("FAULTED")) == 0 ||
2254 	    strcmp(health, gettext("SUSPENDED")) == 0 ||
2255 	    strcmp(health, gettext("UNAVAIL")) == 0) {
2256 		return (ANSI_RED);
2257 	}
2258 
2259 	if (strcmp(health, gettext("OFFLINE")) == 0 ||
2260 	    strcmp(health, gettext("DEGRADED")) == 0 ||
2261 	    strcmp(health, gettext("REMOVED")) == 0) {
2262 		return (ANSI_YELLOW);
2263 	}
2264 
2265 	return (NULL);
2266 }
2267 
2268 /*
2269  * Print out configuration state as requested by status_callback.
2270  */
2271 static void
2272 print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
2273     nvlist_t *nv, int depth, boolean_t isspare, vdev_rebuild_stat_t *vrs)
2274 {
2275 	nvlist_t **child, *root;
2276 	uint_t c, i, vsc, children;
2277 	pool_scan_stat_t *ps = NULL;
2278 	vdev_stat_t *vs;
2279 	char rbuf[6], wbuf[6], cbuf[6];
2280 	char *vname;
2281 	uint64_t notpresent;
2282 	spare_cbdata_t spare_cb;
2283 	const char *state;
2284 	const char *type;
2285 	const char *path = NULL;
2286 	const char *rcolor = NULL, *wcolor = NULL, *ccolor = NULL;
2287 
2288 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2289 	    &child, &children) != 0)
2290 		children = 0;
2291 
2292 	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2293 	    (uint64_t **)&vs, &vsc) == 0);
2294 
2295 	verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
2296 
2297 	if (strcmp(type, VDEV_TYPE_INDIRECT) == 0)
2298 		return;
2299 
2300 	state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
2301 
2302 	if (isspare) {
2303 		/*
2304 		 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
2305 		 * online drives.
2306 		 */
2307 		if (vs->vs_aux == VDEV_AUX_SPARED)
2308 			state = gettext("INUSE");
2309 		else if (vs->vs_state == VDEV_STATE_HEALTHY)
2310 			state = gettext("AVAIL");
2311 	}
2312 
2313 	printf_color(health_str_to_color(state),
2314 	    "\t%*s%-*s  %-8s", depth, "", cb->cb_namewidth - depth,
2315 	    name, state);
2316 
2317 	if (!isspare) {
2318 		if (vs->vs_read_errors)
2319 			rcolor = ANSI_RED;
2320 
2321 		if (vs->vs_write_errors)
2322 			wcolor = ANSI_RED;
2323 
2324 		if (vs->vs_checksum_errors)
2325 			ccolor = ANSI_RED;
2326 
2327 		if (cb->cb_literal) {
2328 			fputc(' ', stdout);
2329 			printf_color(rcolor, "%5llu",
2330 			    (u_longlong_t)vs->vs_read_errors);
2331 			fputc(' ', stdout);
2332 			printf_color(wcolor, "%5llu",
2333 			    (u_longlong_t)vs->vs_write_errors);
2334 			fputc(' ', stdout);
2335 			printf_color(ccolor, "%5llu",
2336 			    (u_longlong_t)vs->vs_checksum_errors);
2337 		} else {
2338 			zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
2339 			zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
2340 			zfs_nicenum(vs->vs_checksum_errors, cbuf,
2341 			    sizeof (cbuf));
2342 			fputc(' ', stdout);
2343 			printf_color(rcolor, "%5s", rbuf);
2344 			fputc(' ', stdout);
2345 			printf_color(wcolor, "%5s", wbuf);
2346 			fputc(' ', stdout);
2347 			printf_color(ccolor, "%5s", cbuf);
2348 		}
2349 		if (cb->cb_print_slow_ios) {
2350 			if (children == 0)  {
2351 				/* Only leafs vdevs have slow IOs */
2352 				zfs_nicenum(vs->vs_slow_ios, rbuf,
2353 				    sizeof (rbuf));
2354 			} else {
2355 				snprintf(rbuf, sizeof (rbuf), "-");
2356 			}
2357 
2358 			if (cb->cb_literal)
2359 				printf(" %5llu", (u_longlong_t)vs->vs_slow_ios);
2360 			else
2361 				printf(" %5s", rbuf);
2362 		}
2363 	}
2364 
2365 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
2366 	    &notpresent) == 0) {
2367 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2368 		(void) printf("  %s %s", gettext("was"), path);
2369 	} else if (vs->vs_aux != 0) {
2370 		(void) printf("  ");
2371 		color_start(ANSI_RED);
2372 		switch (vs->vs_aux) {
2373 		case VDEV_AUX_OPEN_FAILED:
2374 			(void) printf(gettext("cannot open"));
2375 			break;
2376 
2377 		case VDEV_AUX_BAD_GUID_SUM:
2378 			(void) printf(gettext("missing device"));
2379 			break;
2380 
2381 		case VDEV_AUX_NO_REPLICAS:
2382 			(void) printf(gettext("insufficient replicas"));
2383 			break;
2384 
2385 		case VDEV_AUX_VERSION_NEWER:
2386 			(void) printf(gettext("newer version"));
2387 			break;
2388 
2389 		case VDEV_AUX_UNSUP_FEAT:
2390 			(void) printf(gettext("unsupported feature(s)"));
2391 			break;
2392 
2393 		case VDEV_AUX_ASHIFT_TOO_BIG:
2394 			(void) printf(gettext("unsupported minimum blocksize"));
2395 			break;
2396 
2397 		case VDEV_AUX_SPARED:
2398 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2399 			    &spare_cb.cb_guid) == 0);
2400 			if (zpool_iter(g_zfs, find_spare, &spare_cb) == 1) {
2401 				if (strcmp(zpool_get_name(spare_cb.cb_zhp),
2402 				    zpool_get_name(zhp)) == 0)
2403 					(void) printf(gettext("currently in "
2404 					    "use"));
2405 				else
2406 					(void) printf(gettext("in use by "
2407 					    "pool '%s'"),
2408 					    zpool_get_name(spare_cb.cb_zhp));
2409 				zpool_close(spare_cb.cb_zhp);
2410 			} else {
2411 				(void) printf(gettext("currently in use"));
2412 			}
2413 			break;
2414 
2415 		case VDEV_AUX_ERR_EXCEEDED:
2416 			(void) printf(gettext("too many errors"));
2417 			break;
2418 
2419 		case VDEV_AUX_IO_FAILURE:
2420 			(void) printf(gettext("experienced I/O failures"));
2421 			break;
2422 
2423 		case VDEV_AUX_BAD_LOG:
2424 			(void) printf(gettext("bad intent log"));
2425 			break;
2426 
2427 		case VDEV_AUX_EXTERNAL:
2428 			(void) printf(gettext("external device fault"));
2429 			break;
2430 
2431 		case VDEV_AUX_SPLIT_POOL:
2432 			(void) printf(gettext("split into new pool"));
2433 			break;
2434 
2435 		case VDEV_AUX_ACTIVE:
2436 			(void) printf(gettext("currently in use"));
2437 			break;
2438 
2439 		case VDEV_AUX_CHILDREN_OFFLINE:
2440 			(void) printf(gettext("all children offline"));
2441 			break;
2442 
2443 		case VDEV_AUX_BAD_LABEL:
2444 			(void) printf(gettext("invalid label"));
2445 			break;
2446 
2447 		default:
2448 			(void) printf(gettext("corrupted data"));
2449 			break;
2450 		}
2451 		color_end();
2452 	} else if (children == 0 && !isspare &&
2453 	    getenv("ZPOOL_STATUS_NON_NATIVE_ASHIFT_IGNORE") == NULL &&
2454 	    VDEV_STAT_VALID(vs_physical_ashift, vsc) &&
2455 	    vs->vs_configured_ashift < vs->vs_physical_ashift) {
2456 		(void) printf(
2457 		    gettext("  block size: %dB configured, %dB native"),
2458 		    1 << vs->vs_configured_ashift, 1 << vs->vs_physical_ashift);
2459 	}
2460 
2461 	if (vs->vs_scan_removing != 0) {
2462 		(void) printf(gettext("  (removing)"));
2463 	} else if (VDEV_STAT_VALID(vs_noalloc, vsc) && vs->vs_noalloc != 0) {
2464 		(void) printf(gettext("  (non-allocating)"));
2465 	}
2466 
2467 	/* The root vdev has the scrub/resilver stats */
2468 	root = fnvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2469 	    ZPOOL_CONFIG_VDEV_TREE);
2470 	(void) nvlist_lookup_uint64_array(root, ZPOOL_CONFIG_SCAN_STATS,
2471 	    (uint64_t **)&ps, &c);
2472 
2473 	/*
2474 	 * If you force fault a drive that's resilvering, its scan stats can
2475 	 * get frozen in time, giving the false impression that it's
2476 	 * being resilvered.  That's why we check the state to see if the vdev
2477 	 * is healthy before reporting "resilvering" or "repairing".
2478 	 */
2479 	if (ps != NULL && ps->pss_state == DSS_SCANNING && children == 0 &&
2480 	    vs->vs_state == VDEV_STATE_HEALTHY) {
2481 		if (vs->vs_scan_processed != 0) {
2482 			(void) printf(gettext("  (%s)"),
2483 			    (ps->pss_func == POOL_SCAN_RESILVER) ?
2484 			    "resilvering" : "repairing");
2485 		} else if (vs->vs_resilver_deferred) {
2486 			(void) printf(gettext("  (awaiting resilver)"));
2487 		}
2488 	}
2489 
2490 	/* The top-level vdevs have the rebuild stats */
2491 	if (vrs != NULL && vrs->vrs_state == VDEV_REBUILD_ACTIVE &&
2492 	    children == 0 && vs->vs_state == VDEV_STATE_HEALTHY) {
2493 		if (vs->vs_rebuild_processed != 0) {
2494 			(void) printf(gettext("  (resilvering)"));
2495 		}
2496 	}
2497 
2498 	if (cb->vcdl != NULL) {
2499 		if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
2500 			printf("  ");
2501 			zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
2502 		}
2503 	}
2504 
2505 	/* Display vdev initialization and trim status for leaves. */
2506 	if (children == 0) {
2507 		print_status_initialize(vs, cb->cb_print_vdev_init);
2508 		print_status_trim(vs, cb->cb_print_vdev_trim);
2509 	}
2510 
2511 	(void) printf("\n");
2512 
2513 	for (c = 0; c < children; c++) {
2514 		uint64_t islog = B_FALSE, ishole = B_FALSE;
2515 
2516 		/* Don't print logs or holes here */
2517 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2518 		    &islog);
2519 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2520 		    &ishole);
2521 		if (islog || ishole)
2522 			continue;
2523 		/* Only print normal classes here */
2524 		if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
2525 			continue;
2526 
2527 		/* Provide vdev_rebuild_stats to children if available */
2528 		if (vrs == NULL) {
2529 			(void) nvlist_lookup_uint64_array(nv,
2530 			    ZPOOL_CONFIG_REBUILD_STATS,
2531 			    (uint64_t **)&vrs, &i);
2532 		}
2533 
2534 		vname = zpool_vdev_name(g_zfs, zhp, child[c],
2535 		    cb->cb_name_flags | VDEV_NAME_TYPE_ID);
2536 		print_status_config(zhp, cb, vname, child[c], depth + 2,
2537 		    isspare, vrs);
2538 		free(vname);
2539 	}
2540 }
2541 
2542 /*
2543  * Print the configuration of an exported pool.  Iterate over all vdevs in the
2544  * pool, printing out the name and status for each one.
2545  */
2546 static void
2547 print_import_config(status_cbdata_t *cb, const char *name, nvlist_t *nv,
2548     int depth)
2549 {
2550 	nvlist_t **child;
2551 	uint_t c, children;
2552 	vdev_stat_t *vs;
2553 	const char *type;
2554 	char *vname;
2555 
2556 	verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
2557 	if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
2558 	    strcmp(type, VDEV_TYPE_HOLE) == 0)
2559 		return;
2560 
2561 	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2562 	    (uint64_t **)&vs, &c) == 0);
2563 
2564 	(void) printf("\t%*s%-*s", depth, "", cb->cb_namewidth - depth, name);
2565 	(void) printf("  %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
2566 
2567 	if (vs->vs_aux != 0) {
2568 		(void) printf("  ");
2569 
2570 		switch (vs->vs_aux) {
2571 		case VDEV_AUX_OPEN_FAILED:
2572 			(void) printf(gettext("cannot open"));
2573 			break;
2574 
2575 		case VDEV_AUX_BAD_GUID_SUM:
2576 			(void) printf(gettext("missing device"));
2577 			break;
2578 
2579 		case VDEV_AUX_NO_REPLICAS:
2580 			(void) printf(gettext("insufficient replicas"));
2581 			break;
2582 
2583 		case VDEV_AUX_VERSION_NEWER:
2584 			(void) printf(gettext("newer version"));
2585 			break;
2586 
2587 		case VDEV_AUX_UNSUP_FEAT:
2588 			(void) printf(gettext("unsupported feature(s)"));
2589 			break;
2590 
2591 		case VDEV_AUX_ERR_EXCEEDED:
2592 			(void) printf(gettext("too many errors"));
2593 			break;
2594 
2595 		case VDEV_AUX_ACTIVE:
2596 			(void) printf(gettext("currently in use"));
2597 			break;
2598 
2599 		case VDEV_AUX_CHILDREN_OFFLINE:
2600 			(void) printf(gettext("all children offline"));
2601 			break;
2602 
2603 		case VDEV_AUX_BAD_LABEL:
2604 			(void) printf(gettext("invalid label"));
2605 			break;
2606 
2607 		default:
2608 			(void) printf(gettext("corrupted data"));
2609 			break;
2610 		}
2611 	}
2612 	(void) printf("\n");
2613 
2614 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2615 	    &child, &children) != 0)
2616 		return;
2617 
2618 	for (c = 0; c < children; c++) {
2619 		uint64_t is_log = B_FALSE;
2620 
2621 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2622 		    &is_log);
2623 		if (is_log)
2624 			continue;
2625 		if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
2626 			continue;
2627 
2628 		vname = zpool_vdev_name(g_zfs, NULL, child[c],
2629 		    cb->cb_name_flags | VDEV_NAME_TYPE_ID);
2630 		print_import_config(cb, vname, child[c], depth + 2);
2631 		free(vname);
2632 	}
2633 
2634 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2635 	    &child, &children) == 0) {
2636 		(void) printf(gettext("\tcache\n"));
2637 		for (c = 0; c < children; c++) {
2638 			vname = zpool_vdev_name(g_zfs, NULL, child[c],
2639 			    cb->cb_name_flags);
2640 			(void) printf("\t  %s\n", vname);
2641 			free(vname);
2642 		}
2643 	}
2644 
2645 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2646 	    &child, &children) == 0) {
2647 		(void) printf(gettext("\tspares\n"));
2648 		for (c = 0; c < children; c++) {
2649 			vname = zpool_vdev_name(g_zfs, NULL, child[c],
2650 			    cb->cb_name_flags);
2651 			(void) printf("\t  %s\n", vname);
2652 			free(vname);
2653 		}
2654 	}
2655 }
2656 
2657 /*
2658  * Print specialized class vdevs.
2659  *
2660  * These are recorded as top level vdevs in the main pool child array
2661  * but with "is_log" set to 1 or an "alloc_bias" string. We use either
2662  * print_status_config() or print_import_config() to print the top level
2663  * class vdevs then any of their children (eg mirrored slogs) are printed
2664  * recursively - which works because only the top level vdev is marked.
2665  */
2666 static void
2667 print_class_vdevs(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv,
2668     const char *class)
2669 {
2670 	uint_t c, children;
2671 	nvlist_t **child;
2672 	boolean_t printed = B_FALSE;
2673 
2674 	assert(zhp != NULL || !cb->cb_verbose);
2675 
2676 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
2677 	    &children) != 0)
2678 		return;
2679 
2680 	for (c = 0; c < children; c++) {
2681 		uint64_t is_log = B_FALSE;
2682 		const char *bias = NULL;
2683 		const char *type = NULL;
2684 
2685 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2686 		    &is_log);
2687 
2688 		if (is_log) {
2689 			bias = (char *)VDEV_ALLOC_CLASS_LOGS;
2690 		} else {
2691 			(void) nvlist_lookup_string(child[c],
2692 			    ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
2693 			(void) nvlist_lookup_string(child[c],
2694 			    ZPOOL_CONFIG_TYPE, &type);
2695 		}
2696 
2697 		if (bias == NULL || strcmp(bias, class) != 0)
2698 			continue;
2699 		if (!is_log && strcmp(type, VDEV_TYPE_INDIRECT) == 0)
2700 			continue;
2701 
2702 		if (!printed) {
2703 			(void) printf("\t%s\t\n", gettext(class));
2704 			printed = B_TRUE;
2705 		}
2706 
2707 		char *name = zpool_vdev_name(g_zfs, zhp, child[c],
2708 		    cb->cb_name_flags | VDEV_NAME_TYPE_ID);
2709 		if (cb->cb_print_status)
2710 			print_status_config(zhp, cb, name, child[c], 2,
2711 			    B_FALSE, NULL);
2712 		else
2713 			print_import_config(cb, name, child[c], 2);
2714 		free(name);
2715 	}
2716 }
2717 
2718 /*
2719  * Display the status for the given pool.
2720  */
2721 static int
2722 show_import(nvlist_t *config, boolean_t report_error)
2723 {
2724 	uint64_t pool_state;
2725 	vdev_stat_t *vs;
2726 	const char *name;
2727 	uint64_t guid;
2728 	uint64_t hostid = 0;
2729 	const char *msgid;
2730 	const char *hostname = "unknown";
2731 	nvlist_t *nvroot, *nvinfo;
2732 	zpool_status_t reason;
2733 	zpool_errata_t errata;
2734 	const char *health;
2735 	uint_t vsc;
2736 	const char *comment;
2737 	status_cbdata_t cb = { 0 };
2738 
2739 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
2740 	    &name) == 0);
2741 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
2742 	    &guid) == 0);
2743 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2744 	    &pool_state) == 0);
2745 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2746 	    &nvroot) == 0);
2747 
2748 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
2749 	    (uint64_t **)&vs, &vsc) == 0);
2750 	health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
2751 
2752 	reason = zpool_import_status(config, &msgid, &errata);
2753 
2754 	/*
2755 	 * If we're importing using a cachefile, then we won't report any
2756 	 * errors unless we are in the scan phase of the import.
2757 	 */
2758 	if (reason != ZPOOL_STATUS_OK && !report_error)
2759 		return (reason);
2760 
2761 	(void) printf(gettext("   pool: %s\n"), name);
2762 	(void) printf(gettext("     id: %llu\n"), (u_longlong_t)guid);
2763 	(void) printf(gettext("  state: %s"), health);
2764 	if (pool_state == POOL_STATE_DESTROYED)
2765 		(void) printf(gettext(" (DESTROYED)"));
2766 	(void) printf("\n");
2767 
2768 	switch (reason) {
2769 	case ZPOOL_STATUS_MISSING_DEV_R:
2770 	case ZPOOL_STATUS_MISSING_DEV_NR:
2771 	case ZPOOL_STATUS_BAD_GUID_SUM:
2772 		printf_color(ANSI_BOLD, gettext("status: "));
2773 		printf_color(ANSI_YELLOW, gettext("One or more devices are "
2774 		    "missing from the system.\n"));
2775 		break;
2776 
2777 	case ZPOOL_STATUS_CORRUPT_LABEL_R:
2778 	case ZPOOL_STATUS_CORRUPT_LABEL_NR:
2779 		printf_color(ANSI_BOLD, gettext("status: "));
2780 		printf_color(ANSI_YELLOW, gettext("One or more devices contains"
2781 		    " corrupted data.\n"));
2782 		break;
2783 
2784 	case ZPOOL_STATUS_CORRUPT_DATA:
2785 		(void) printf(
2786 		    gettext(" status: The pool data is corrupted.\n"));
2787 		break;
2788 
2789 	case ZPOOL_STATUS_OFFLINE_DEV:
2790 		printf_color(ANSI_BOLD, gettext("status: "));
2791 		printf_color(ANSI_YELLOW, gettext("One or more devices "
2792 		    "are offlined.\n"));
2793 		break;
2794 
2795 	case ZPOOL_STATUS_CORRUPT_POOL:
2796 		printf_color(ANSI_BOLD, gettext("status: "));
2797 		printf_color(ANSI_YELLOW, gettext("The pool metadata is "
2798 		    "corrupted.\n"));
2799 		break;
2800 
2801 	case ZPOOL_STATUS_VERSION_OLDER:
2802 		printf_color(ANSI_BOLD, gettext("status: "));
2803 		printf_color(ANSI_YELLOW, gettext("The pool is formatted using "
2804 		    "a legacy on-disk version.\n"));
2805 		break;
2806 
2807 	case ZPOOL_STATUS_VERSION_NEWER:
2808 		printf_color(ANSI_BOLD, gettext("status: "));
2809 		printf_color(ANSI_YELLOW, gettext("The pool is formatted using "
2810 		    "an incompatible version.\n"));
2811 		break;
2812 
2813 	case ZPOOL_STATUS_FEAT_DISABLED:
2814 		printf_color(ANSI_BOLD, gettext("status: "));
2815 		printf_color(ANSI_YELLOW, gettext("Some supported "
2816 		    "features are not enabled on the pool.\n\t"
2817 		    "(Note that they may be intentionally disabled "
2818 		    "if the\n\t'compatibility' property is set.)\n"));
2819 		break;
2820 
2821 	case ZPOOL_STATUS_COMPATIBILITY_ERR:
2822 		printf_color(ANSI_BOLD, gettext("status: "));
2823 		printf_color(ANSI_YELLOW, gettext("Error reading or parsing "
2824 		    "the file(s) indicated by the 'compatibility'\n"
2825 		    "property.\n"));
2826 		break;
2827 
2828 	case ZPOOL_STATUS_INCOMPATIBLE_FEAT:
2829 		printf_color(ANSI_BOLD, gettext("status: "));
2830 		printf_color(ANSI_YELLOW, gettext("One or more features "
2831 		    "are enabled on the pool despite not being\n"
2832 		    "requested by the 'compatibility' property.\n"));
2833 		break;
2834 
2835 	case ZPOOL_STATUS_UNSUP_FEAT_READ:
2836 		printf_color(ANSI_BOLD, gettext("status: "));
2837 		printf_color(ANSI_YELLOW, gettext("The pool uses the following "
2838 		    "feature(s) not supported on this system:\n"));
2839 		color_start(ANSI_YELLOW);
2840 		zpool_print_unsup_feat(config);
2841 		color_end();
2842 		break;
2843 
2844 	case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
2845 		printf_color(ANSI_BOLD, gettext("status: "));
2846 		printf_color(ANSI_YELLOW, gettext("The pool can only be "
2847 		    "accessed in read-only mode on this system. It\n\tcannot be"
2848 		    " accessed in read-write mode because it uses the "
2849 		    "following\n\tfeature(s) not supported on this system:\n"));
2850 		color_start(ANSI_YELLOW);
2851 		zpool_print_unsup_feat(config);
2852 		color_end();
2853 		break;
2854 
2855 	case ZPOOL_STATUS_HOSTID_ACTIVE:
2856 		printf_color(ANSI_BOLD, gettext("status: "));
2857 		printf_color(ANSI_YELLOW, gettext("The pool is currently "
2858 		    "imported by another system.\n"));
2859 		break;
2860 
2861 	case ZPOOL_STATUS_HOSTID_REQUIRED:
2862 		printf_color(ANSI_BOLD, gettext("status: "));
2863 		printf_color(ANSI_YELLOW, gettext("The pool has the "
2864 		    "multihost property on.  It cannot\n\tbe safely imported "
2865 		    "when the system hostid is not set.\n"));
2866 		break;
2867 
2868 	case ZPOOL_STATUS_HOSTID_MISMATCH:
2869 		printf_color(ANSI_BOLD, gettext("status: "));
2870 		printf_color(ANSI_YELLOW, gettext("The pool was last accessed "
2871 		    "by another system.\n"));
2872 		break;
2873 
2874 	case ZPOOL_STATUS_FAULTED_DEV_R:
2875 	case ZPOOL_STATUS_FAULTED_DEV_NR:
2876 		printf_color(ANSI_BOLD, gettext("status: "));
2877 		printf_color(ANSI_YELLOW, gettext("One or more devices are "
2878 		    "faulted.\n"));
2879 		break;
2880 
2881 	case ZPOOL_STATUS_BAD_LOG:
2882 		printf_color(ANSI_BOLD, gettext("status: "));
2883 		printf_color(ANSI_YELLOW, gettext("An intent log record cannot "
2884 		    "be read.\n"));
2885 		break;
2886 
2887 	case ZPOOL_STATUS_RESILVERING:
2888 	case ZPOOL_STATUS_REBUILDING:
2889 		printf_color(ANSI_BOLD, gettext("status: "));
2890 		printf_color(ANSI_YELLOW, gettext("One or more devices were "
2891 		    "being resilvered.\n"));
2892 		break;
2893 
2894 	case ZPOOL_STATUS_ERRATA:
2895 		printf_color(ANSI_BOLD, gettext("status: "));
2896 		printf_color(ANSI_YELLOW, gettext("Errata #%d detected.\n"),
2897 		    errata);
2898 		break;
2899 
2900 	case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
2901 		printf_color(ANSI_BOLD, gettext("status: "));
2902 		printf_color(ANSI_YELLOW, gettext("One or more devices are "
2903 		    "configured to use a non-native block size.\n"
2904 		    "\tExpect reduced performance.\n"));
2905 		break;
2906 
2907 	default:
2908 		/*
2909 		 * No other status can be seen when importing pools.
2910 		 */
2911 		assert(reason == ZPOOL_STATUS_OK);
2912 	}
2913 
2914 	/*
2915 	 * Print out an action according to the overall state of the pool.
2916 	 */
2917 	if (vs->vs_state == VDEV_STATE_HEALTHY) {
2918 		if (reason == ZPOOL_STATUS_VERSION_OLDER ||
2919 		    reason == ZPOOL_STATUS_FEAT_DISABLED) {
2920 			(void) printf(gettext(" action: The pool can be "
2921 			    "imported using its name or numeric identifier, "
2922 			    "though\n\tsome features will not be available "
2923 			    "without an explicit 'zpool upgrade'.\n"));
2924 		} else if (reason == ZPOOL_STATUS_COMPATIBILITY_ERR) {
2925 			(void) printf(gettext(" action: The pool can be "
2926 			    "imported using its name or numeric\n\tidentifier, "
2927 			    "though the file(s) indicated by its "
2928 			    "'compatibility'\n\tproperty cannot be parsed at "
2929 			    "this time.\n"));
2930 		} else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
2931 			(void) printf(gettext(" action: The pool can be "
2932 			    "imported using its name or numeric "
2933 			    "identifier and\n\tthe '-f' flag.\n"));
2934 		} else if (reason == ZPOOL_STATUS_ERRATA) {
2935 			switch (errata) {
2936 			case ZPOOL_ERRATA_NONE:
2937 				break;
2938 
2939 			case ZPOOL_ERRATA_ZOL_2094_SCRUB:
2940 				(void) printf(gettext(" action: The pool can "
2941 				    "be imported using its name or numeric "
2942 				    "identifier,\n\thowever there is a compat"
2943 				    "ibility issue which should be corrected"
2944 				    "\n\tby running 'zpool scrub'\n"));
2945 				break;
2946 
2947 			case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY:
2948 				(void) printf(gettext(" action: The pool can"
2949 				    "not be imported with this version of ZFS "
2950 				    "due to\n\tan active asynchronous destroy. "
2951 				    "Revert to an earlier version\n\tand "
2952 				    "allow the destroy to complete before "
2953 				    "updating.\n"));
2954 				break;
2955 
2956 			case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION:
2957 				(void) printf(gettext(" action: Existing "
2958 				    "encrypted datasets contain an on-disk "
2959 				    "incompatibility, which\n\tneeds to be "
2960 				    "corrected. Backup these datasets to new "
2961 				    "encrypted datasets\n\tand destroy the "
2962 				    "old ones.\n"));
2963 				break;
2964 
2965 			case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION:
2966 				(void) printf(gettext(" action: Existing "
2967 				    "encrypted snapshots and bookmarks contain "
2968 				    "an on-disk\n\tincompatibility. This may "
2969 				    "cause on-disk corruption if they are used"
2970 				    "\n\twith 'zfs recv'. To correct the "
2971 				    "issue, enable the bookmark_v2 feature.\n\t"
2972 				    "No additional action is needed if there "
2973 				    "are no encrypted snapshots or\n\t"
2974 				    "bookmarks. If preserving the encrypted "
2975 				    "snapshots and bookmarks is\n\trequired, "
2976 				    "use a non-raw send to backup and restore "
2977 				    "them. Alternately,\n\tthey may be removed"
2978 				    " to resolve the incompatibility.\n"));
2979 				break;
2980 			default:
2981 				/*
2982 				 * All errata must contain an action message.
2983 				 */
2984 				assert(0);
2985 			}
2986 		} else {
2987 			(void) printf(gettext(" action: The pool can be "
2988 			    "imported using its name or numeric "
2989 			    "identifier.\n"));
2990 		}
2991 	} else if (vs->vs_state == VDEV_STATE_DEGRADED) {
2992 		(void) printf(gettext(" action: The pool can be imported "
2993 		    "despite missing or damaged devices.  The\n\tfault "
2994 		    "tolerance of the pool may be compromised if imported.\n"));
2995 	} else {
2996 		switch (reason) {
2997 		case ZPOOL_STATUS_VERSION_NEWER:
2998 			(void) printf(gettext(" action: The pool cannot be "
2999 			    "imported.  Access the pool on a system running "
3000 			    "newer\n\tsoftware, or recreate the pool from "
3001 			    "backup.\n"));
3002 			break;
3003 		case ZPOOL_STATUS_UNSUP_FEAT_READ:
3004 			printf_color(ANSI_BOLD, gettext("action: "));
3005 			printf_color(ANSI_YELLOW, gettext("The pool cannot be "
3006 			    "imported. Access the pool on a system that "
3007 			    "supports\n\tthe required feature(s), or recreate "
3008 			    "the pool from backup.\n"));
3009 			break;
3010 		case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
3011 			printf_color(ANSI_BOLD, gettext("action: "));
3012 			printf_color(ANSI_YELLOW, gettext("The pool cannot be "
3013 			    "imported in read-write mode. Import the pool "
3014 			    "with\n"
3015 			    "\t\"-o readonly=on\", access the pool on a system "
3016 			    "that supports the\n\trequired feature(s), or "
3017 			    "recreate the pool from backup.\n"));
3018 			break;
3019 		case ZPOOL_STATUS_MISSING_DEV_R:
3020 		case ZPOOL_STATUS_MISSING_DEV_NR:
3021 		case ZPOOL_STATUS_BAD_GUID_SUM:
3022 			(void) printf(gettext(" action: The pool cannot be "
3023 			    "imported. Attach the missing\n\tdevices and try "
3024 			    "again.\n"));
3025 			break;
3026 		case ZPOOL_STATUS_HOSTID_ACTIVE:
3027 			VERIFY0(nvlist_lookup_nvlist(config,
3028 			    ZPOOL_CONFIG_LOAD_INFO, &nvinfo));
3029 
3030 			if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
3031 				hostname = fnvlist_lookup_string(nvinfo,
3032 				    ZPOOL_CONFIG_MMP_HOSTNAME);
3033 
3034 			if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID))
3035 				hostid = fnvlist_lookup_uint64(nvinfo,
3036 				    ZPOOL_CONFIG_MMP_HOSTID);
3037 
3038 			(void) printf(gettext(" action: The pool must be "
3039 			    "exported from %s (hostid=%"PRIx64")\n\tbefore it "
3040 			    "can be safely imported.\n"), hostname, hostid);
3041 			break;
3042 		case ZPOOL_STATUS_HOSTID_REQUIRED:
3043 			(void) printf(gettext(" action: Set a unique system "
3044 			    "hostid with the zgenhostid(8) command.\n"));
3045 			break;
3046 		default:
3047 			(void) printf(gettext(" action: The pool cannot be "
3048 			    "imported due to damaged devices or data.\n"));
3049 		}
3050 	}
3051 
3052 	/* Print the comment attached to the pool. */
3053 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
3054 		(void) printf(gettext("comment: %s\n"), comment);
3055 
3056 	/*
3057 	 * If the state is "closed" or "can't open", and the aux state
3058 	 * is "corrupt data":
3059 	 */
3060 	if (((vs->vs_state == VDEV_STATE_CLOSED) ||
3061 	    (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
3062 	    (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
3063 		if (pool_state == POOL_STATE_DESTROYED)
3064 			(void) printf(gettext("\tThe pool was destroyed, "
3065 			    "but can be imported using the '-Df' flags.\n"));
3066 		else if (pool_state != POOL_STATE_EXPORTED)
3067 			(void) printf(gettext("\tThe pool may be active on "
3068 			    "another system, but can be imported using\n\t"
3069 			    "the '-f' flag.\n"));
3070 	}
3071 
3072 	if (msgid != NULL) {
3073 		(void) printf(gettext(
3074 		    "   see: https://openzfs.github.io/openzfs-docs/msg/%s\n"),
3075 		    msgid);
3076 	}
3077 
3078 	(void) printf(gettext(" config:\n\n"));
3079 
3080 	cb.cb_namewidth = max_width(NULL, nvroot, 0, strlen(name),
3081 	    VDEV_NAME_TYPE_ID);
3082 	if (cb.cb_namewidth < 10)
3083 		cb.cb_namewidth = 10;
3084 
3085 	print_import_config(&cb, name, nvroot, 0);
3086 
3087 	print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_DEDUP);
3088 	print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_SPECIAL);
3089 	print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_CLASS_LOGS);
3090 
3091 	if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
3092 		(void) printf(gettext("\n\tAdditional devices are known to "
3093 		    "be part of this pool, though their\n\texact "
3094 		    "configuration cannot be determined.\n"));
3095 	}
3096 	return (0);
3097 }
3098 
3099 static boolean_t
3100 zfs_force_import_required(nvlist_t *config)
3101 {
3102 	uint64_t state;
3103 	uint64_t hostid = 0;
3104 	nvlist_t *nvinfo;
3105 
3106 	state = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE);
3107 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, &hostid);
3108 
3109 	if (state != POOL_STATE_EXPORTED && hostid != get_system_hostid())
3110 		return (B_TRUE);
3111 
3112 	nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
3113 	if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE)) {
3114 		mmp_state_t mmp_state = fnvlist_lookup_uint64(nvinfo,
3115 		    ZPOOL_CONFIG_MMP_STATE);
3116 
3117 		if (mmp_state != MMP_STATE_INACTIVE)
3118 			return (B_TRUE);
3119 	}
3120 
3121 	return (B_FALSE);
3122 }
3123 
3124 /*
3125  * Perform the import for the given configuration.  This passes the heavy
3126  * lifting off to zpool_import_props(), and then mounts the datasets contained
3127  * within the pool.
3128  */
3129 static int
3130 do_import(nvlist_t *config, const char *newname, const char *mntopts,
3131     nvlist_t *props, int flags)
3132 {
3133 	int ret = 0;
3134 	zpool_handle_t *zhp;
3135 	const char *name;
3136 	uint64_t version;
3137 
3138 	name = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME);
3139 	version = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION);
3140 
3141 	if (!SPA_VERSION_IS_SUPPORTED(version)) {
3142 		(void) fprintf(stderr, gettext("cannot import '%s': pool "
3143 		    "is formatted using an unsupported ZFS version\n"), name);
3144 		return (1);
3145 	} else if (zfs_force_import_required(config) &&
3146 	    !(flags & ZFS_IMPORT_ANY_HOST)) {
3147 		mmp_state_t mmp_state = MMP_STATE_INACTIVE;
3148 		nvlist_t *nvinfo;
3149 
3150 		nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
3151 		if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE))
3152 			mmp_state = fnvlist_lookup_uint64(nvinfo,
3153 			    ZPOOL_CONFIG_MMP_STATE);
3154 
3155 		if (mmp_state == MMP_STATE_ACTIVE) {
3156 			const char *hostname = "<unknown>";
3157 			uint64_t hostid = 0;
3158 
3159 			if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
3160 				hostname = fnvlist_lookup_string(nvinfo,
3161 				    ZPOOL_CONFIG_MMP_HOSTNAME);
3162 
3163 			if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID))
3164 				hostid = fnvlist_lookup_uint64(nvinfo,
3165 				    ZPOOL_CONFIG_MMP_HOSTID);
3166 
3167 			(void) fprintf(stderr, gettext("cannot import '%s': "
3168 			    "pool is imported on %s (hostid: "
3169 			    "0x%"PRIx64")\nExport the pool on the other "
3170 			    "system, then run 'zpool import'.\n"),
3171 			    name, hostname, hostid);
3172 		} else if (mmp_state == MMP_STATE_NO_HOSTID) {
3173 			(void) fprintf(stderr, gettext("Cannot import '%s': "
3174 			    "pool has the multihost property on and the\n"
3175 			    "system's hostid is not set. Set a unique hostid "
3176 			    "with the zgenhostid(8) command.\n"), name);
3177 		} else {
3178 			const char *hostname = "<unknown>";
3179 			time_t timestamp = 0;
3180 			uint64_t hostid = 0;
3181 
3182 			if (nvlist_exists(config, ZPOOL_CONFIG_HOSTNAME))
3183 				hostname = fnvlist_lookup_string(config,
3184 				    ZPOOL_CONFIG_HOSTNAME);
3185 
3186 			if (nvlist_exists(config, ZPOOL_CONFIG_TIMESTAMP))
3187 				timestamp = fnvlist_lookup_uint64(config,
3188 				    ZPOOL_CONFIG_TIMESTAMP);
3189 
3190 			if (nvlist_exists(config, ZPOOL_CONFIG_HOSTID))
3191 				hostid = fnvlist_lookup_uint64(config,
3192 				    ZPOOL_CONFIG_HOSTID);
3193 
3194 			(void) fprintf(stderr, gettext("cannot import '%s': "
3195 			    "pool was previously in use from another system.\n"
3196 			    "Last accessed by %s (hostid=%"PRIx64") at %s"
3197 			    "The pool can be imported, use 'zpool import -f' "
3198 			    "to import the pool.\n"), name, hostname,
3199 			    hostid, ctime(&timestamp));
3200 		}
3201 
3202 		return (1);
3203 	}
3204 
3205 	if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
3206 		return (1);
3207 
3208 	if (newname != NULL)
3209 		name = newname;
3210 
3211 	if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
3212 		return (1);
3213 
3214 	/*
3215 	 * Loading keys is best effort. We don't want to return immediately
3216 	 * if it fails but we do want to give the error to the caller.
3217 	 */
3218 	if (flags & ZFS_IMPORT_LOAD_KEYS &&
3219 	    zfs_crypto_attempt_load_keys(g_zfs, name) != 0)
3220 			ret = 1;
3221 
3222 	if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3223 	    !(flags & ZFS_IMPORT_ONLY) &&
3224 	    zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3225 		zpool_close(zhp);
3226 		return (1);
3227 	}
3228 
3229 	zpool_close(zhp);
3230 	return (ret);
3231 }
3232 
3233 static int
3234 import_pools(nvlist_t *pools, nvlist_t *props, char *mntopts, int flags,
3235     char *orig_name, char *new_name,
3236     boolean_t do_destroyed, boolean_t pool_specified, boolean_t do_all,
3237     importargs_t *import)
3238 {
3239 	nvlist_t *config = NULL;
3240 	nvlist_t *found_config = NULL;
3241 	uint64_t pool_state;
3242 
3243 	/*
3244 	 * At this point we have a list of import candidate configs. Even if
3245 	 * we were searching by pool name or guid, we still need to
3246 	 * post-process the list to deal with pool state and possible
3247 	 * duplicate names.
3248 	 */
3249 	int err = 0;
3250 	nvpair_t *elem = NULL;
3251 	boolean_t first = B_TRUE;
3252 	while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
3253 
3254 		verify(nvpair_value_nvlist(elem, &config) == 0);
3255 
3256 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
3257 		    &pool_state) == 0);
3258 		if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
3259 			continue;
3260 		if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
3261 			continue;
3262 
3263 		verify(nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY,
3264 		    import->policy) == 0);
3265 
3266 		if (!pool_specified) {
3267 			if (first)
3268 				first = B_FALSE;
3269 			else if (!do_all)
3270 				(void) fputc('\n', stdout);
3271 
3272 			if (do_all) {
3273 				err |= do_import(config, NULL, mntopts,
3274 				    props, flags);
3275 			} else {
3276 				/*
3277 				 * If we're importing from cachefile, then
3278 				 * we don't want to report errors until we
3279 				 * are in the scan phase of the import. If
3280 				 * we get an error, then we return that error
3281 				 * to invoke the scan phase.
3282 				 */
3283 				if (import->cachefile && !import->scan)
3284 					err = show_import(config, B_FALSE);
3285 				else
3286 					(void) show_import(config, B_TRUE);
3287 			}
3288 		} else if (import->poolname != NULL) {
3289 			const char *name;
3290 
3291 			/*
3292 			 * We are searching for a pool based on name.
3293 			 */
3294 			verify(nvlist_lookup_string(config,
3295 			    ZPOOL_CONFIG_POOL_NAME, &name) == 0);
3296 
3297 			if (strcmp(name, import->poolname) == 0) {
3298 				if (found_config != NULL) {
3299 					(void) fprintf(stderr, gettext(
3300 					    "cannot import '%s': more than "
3301 					    "one matching pool\n"),
3302 					    import->poolname);
3303 					(void) fprintf(stderr, gettext(
3304 					    "import by numeric ID instead\n"));
3305 					err = B_TRUE;
3306 				}
3307 				found_config = config;
3308 			}
3309 		} else {
3310 			uint64_t guid;
3311 
3312 			/*
3313 			 * Search for a pool by guid.
3314 			 */
3315 			verify(nvlist_lookup_uint64(config,
3316 			    ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
3317 
3318 			if (guid == import->guid)
3319 				found_config = config;
3320 		}
3321 	}
3322 
3323 	/*
3324 	 * If we were searching for a specific pool, verify that we found a
3325 	 * pool, and then do the import.
3326 	 */
3327 	if (pool_specified && err == 0) {
3328 		if (found_config == NULL) {
3329 			(void) fprintf(stderr, gettext("cannot import '%s': "
3330 			    "no such pool available\n"), orig_name);
3331 			err = B_TRUE;
3332 		} else {
3333 			err |= do_import(found_config, new_name,
3334 			    mntopts, props, flags);
3335 		}
3336 	}
3337 
3338 	/*
3339 	 * If we were just looking for pools, report an error if none were
3340 	 * found.
3341 	 */
3342 	if (!pool_specified && first)
3343 		(void) fprintf(stderr,
3344 		    gettext("no pools available to import\n"));
3345 	return (err);
3346 }
3347 
3348 typedef struct target_exists_args {
3349 	const char	*poolname;
3350 	uint64_t	poolguid;
3351 } target_exists_args_t;
3352 
3353 static int
3354 name_or_guid_exists(zpool_handle_t *zhp, void *data)
3355 {
3356 	target_exists_args_t *args = data;
3357 	nvlist_t *config = zpool_get_config(zhp, NULL);
3358 	int found = 0;
3359 
3360 	if (config == NULL)
3361 		return (0);
3362 
3363 	if (args->poolname != NULL) {
3364 		const char *pool_name;
3365 
3366 		verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3367 		    &pool_name) == 0);
3368 		if (strcmp(pool_name, args->poolname) == 0)
3369 			found = 1;
3370 	} else {
3371 		uint64_t pool_guid;
3372 
3373 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3374 		    &pool_guid) == 0);
3375 		if (pool_guid == args->poolguid)
3376 			found = 1;
3377 	}
3378 	zpool_close(zhp);
3379 
3380 	return (found);
3381 }
3382 /*
3383  * zpool checkpoint <pool>
3384  *       checkpoint --discard <pool>
3385  *
3386  *       -d         Discard the checkpoint from a checkpointed
3387  *       --discard  pool.
3388  *
3389  *       -w         Wait for discarding a checkpoint to complete.
3390  *       --wait
3391  *
3392  * Checkpoints the specified pool, by taking a "snapshot" of its
3393  * current state. A pool can only have one checkpoint at a time.
3394  */
3395 int
3396 zpool_do_checkpoint(int argc, char **argv)
3397 {
3398 	boolean_t discard, wait;
3399 	char *pool;
3400 	zpool_handle_t *zhp;
3401 	int c, err;
3402 
3403 	struct option long_options[] = {
3404 		{"discard", no_argument, NULL, 'd'},
3405 		{"wait", no_argument, NULL, 'w'},
3406 		{0, 0, 0, 0}
3407 	};
3408 
3409 	discard = B_FALSE;
3410 	wait = B_FALSE;
3411 	while ((c = getopt_long(argc, argv, ":dw", long_options, NULL)) != -1) {
3412 		switch (c) {
3413 		case 'd':
3414 			discard = B_TRUE;
3415 			break;
3416 		case 'w':
3417 			wait = B_TRUE;
3418 			break;
3419 		case '?':
3420 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3421 			    optopt);
3422 			usage(B_FALSE);
3423 		}
3424 	}
3425 
3426 	if (wait && !discard) {
3427 		(void) fprintf(stderr, gettext("--wait only valid when "
3428 		    "--discard also specified\n"));
3429 		usage(B_FALSE);
3430 	}
3431 
3432 	argc -= optind;
3433 	argv += optind;
3434 
3435 	if (argc < 1) {
3436 		(void) fprintf(stderr, gettext("missing pool argument\n"));
3437 		usage(B_FALSE);
3438 	}
3439 
3440 	if (argc > 1) {
3441 		(void) fprintf(stderr, gettext("too many arguments\n"));
3442 		usage(B_FALSE);
3443 	}
3444 
3445 	pool = argv[0];
3446 
3447 	if ((zhp = zpool_open(g_zfs, pool)) == NULL) {
3448 		/* As a special case, check for use of '/' in the name */
3449 		if (strchr(pool, '/') != NULL)
3450 			(void) fprintf(stderr, gettext("'zpool checkpoint' "
3451 			    "doesn't work on datasets. To save the state "
3452 			    "of a dataset from a specific point in time "
3453 			    "please use 'zfs snapshot'\n"));
3454 		return (1);
3455 	}
3456 
3457 	if (discard) {
3458 		err = (zpool_discard_checkpoint(zhp) != 0);
3459 		if (err == 0 && wait)
3460 			err = zpool_wait(zhp, ZPOOL_WAIT_CKPT_DISCARD);
3461 	} else {
3462 		err = (zpool_checkpoint(zhp) != 0);
3463 	}
3464 
3465 	zpool_close(zhp);
3466 
3467 	return (err);
3468 }
3469 
3470 #define	CHECKPOINT_OPT	1024
3471 
3472 /*
3473  * zpool import [-d dir] [-D]
3474  *       import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
3475  *              [-d dir | -c cachefile | -s] [-f] -a
3476  *       import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
3477  *              [-d dir | -c cachefile | -s] [-f] [-n] [-F] <pool | id>
3478  *              [newpool]
3479  *
3480  *	-c	Read pool information from a cachefile instead of searching
3481  *		devices. If importing from a cachefile config fails, then
3482  *		fallback to searching for devices only in the directories that
3483  *		exist in the cachefile.
3484  *
3485  *	-d	Scan in a specific directory, other than /dev/.  More than
3486  *		one directory can be specified using multiple '-d' options.
3487  *
3488  *	-D	Scan for previously destroyed pools or import all or only
3489  *		specified destroyed pools.
3490  *
3491  *	-R	Temporarily import the pool, with all mountpoints relative to
3492  *		the given root.  The pool will remain exported when the machine
3493  *		is rebooted.
3494  *
3495  *	-V	Import even in the presence of faulted vdevs.  This is an
3496  *		intentionally undocumented option for testing purposes, and
3497  *		treats the pool configuration as complete, leaving any bad
3498  *		vdevs in the FAULTED state. In other words, it does verbatim
3499  *		import.
3500  *
3501  *	-f	Force import, even if it appears that the pool is active.
3502  *
3503  *	-F	Attempt rewind if necessary.
3504  *
3505  *	-n	See if rewind would work, but don't actually rewind.
3506  *
3507  *	-N	Import the pool but don't mount datasets.
3508  *
3509  *	-T	Specify a starting txg to use for import. This option is
3510  *		intentionally undocumented option for testing purposes.
3511  *
3512  *	-a	Import all pools found.
3513  *
3514  *	-l	Load encryption keys while importing.
3515  *
3516  *	-o	Set property=value and/or temporary mount options (without '=').
3517  *
3518  *	-s	Scan using the default search path, the libblkid cache will
3519  *		not be consulted.
3520  *
3521  *	--rewind-to-checkpoint
3522  *		Import the pool and revert back to the checkpoint.
3523  *
3524  * The import command scans for pools to import, and import pools based on pool
3525  * name and GUID.  The pool can also be renamed as part of the import process.
3526  */
3527 int
3528 zpool_do_import(int argc, char **argv)
3529 {
3530 	char **searchdirs = NULL;
3531 	char *env, *envdup = NULL;
3532 	int nsearch = 0;
3533 	int c;
3534 	int err = 0;
3535 	nvlist_t *pools = NULL;
3536 	boolean_t do_all = B_FALSE;
3537 	boolean_t do_destroyed = B_FALSE;
3538 	char *mntopts = NULL;
3539 	uint64_t searchguid = 0;
3540 	char *searchname = NULL;
3541 	char *propval;
3542 	nvlist_t *policy = NULL;
3543 	nvlist_t *props = NULL;
3544 	int flags = ZFS_IMPORT_NORMAL;
3545 	uint32_t rewind_policy = ZPOOL_NO_REWIND;
3546 	boolean_t dryrun = B_FALSE;
3547 	boolean_t do_rewind = B_FALSE;
3548 	boolean_t xtreme_rewind = B_FALSE;
3549 	boolean_t do_scan = B_FALSE;
3550 	boolean_t pool_exists = B_FALSE;
3551 	boolean_t pool_specified = B_FALSE;
3552 	uint64_t txg = -1ULL;
3553 	char *cachefile = NULL;
3554 	importargs_t idata = { 0 };
3555 	char *endptr;
3556 
3557 	struct option long_options[] = {
3558 		{"rewind-to-checkpoint", no_argument, NULL, CHECKPOINT_OPT},
3559 		{0, 0, 0, 0}
3560 	};
3561 
3562 	/* check options */
3563 	while ((c = getopt_long(argc, argv, ":aCc:d:DEfFlmnNo:R:stT:VX",
3564 	    long_options, NULL)) != -1) {
3565 		switch (c) {
3566 		case 'a':
3567 			do_all = B_TRUE;
3568 			break;
3569 		case 'c':
3570 			cachefile = optarg;
3571 			break;
3572 		case 'd':
3573 			searchdirs = safe_realloc(searchdirs,
3574 			    (nsearch + 1) * sizeof (char *));
3575 			searchdirs[nsearch++] = optarg;
3576 			break;
3577 		case 'D':
3578 			do_destroyed = B_TRUE;
3579 			break;
3580 		case 'f':
3581 			flags |= ZFS_IMPORT_ANY_HOST;
3582 			break;
3583 		case 'F':
3584 			do_rewind = B_TRUE;
3585 			break;
3586 		case 'l':
3587 			flags |= ZFS_IMPORT_LOAD_KEYS;
3588 			break;
3589 		case 'm':
3590 			flags |= ZFS_IMPORT_MISSING_LOG;
3591 			break;
3592 		case 'n':
3593 			dryrun = B_TRUE;
3594 			break;
3595 		case 'N':
3596 			flags |= ZFS_IMPORT_ONLY;
3597 			break;
3598 		case 'o':
3599 			if ((propval = strchr(optarg, '=')) != NULL) {
3600 				*propval = '\0';
3601 				propval++;
3602 				if (add_prop_list(optarg, propval,
3603 				    &props, B_TRUE))
3604 					goto error;
3605 			} else {
3606 				mntopts = optarg;
3607 			}
3608 			break;
3609 		case 'R':
3610 			if (add_prop_list(zpool_prop_to_name(
3611 			    ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
3612 				goto error;
3613 			if (add_prop_list_default(zpool_prop_to_name(
3614 			    ZPOOL_PROP_CACHEFILE), "none", &props))
3615 				goto error;
3616 			break;
3617 		case 's':
3618 			do_scan = B_TRUE;
3619 			break;
3620 		case 't':
3621 			flags |= ZFS_IMPORT_TEMP_NAME;
3622 			if (add_prop_list_default(zpool_prop_to_name(
3623 			    ZPOOL_PROP_CACHEFILE), "none", &props))
3624 				goto error;
3625 			break;
3626 
3627 		case 'T':
3628 			errno = 0;
3629 			txg = strtoull(optarg, &endptr, 0);
3630 			if (errno != 0 || *endptr != '\0') {
3631 				(void) fprintf(stderr,
3632 				    gettext("invalid txg value\n"));
3633 				usage(B_FALSE);
3634 			}
3635 			rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
3636 			break;
3637 		case 'V':
3638 			flags |= ZFS_IMPORT_VERBATIM;
3639 			break;
3640 		case 'X':
3641 			xtreme_rewind = B_TRUE;
3642 			break;
3643 		case CHECKPOINT_OPT:
3644 			flags |= ZFS_IMPORT_CHECKPOINT;
3645 			break;
3646 		case ':':
3647 			(void) fprintf(stderr, gettext("missing argument for "
3648 			    "'%c' option\n"), optopt);
3649 			usage(B_FALSE);
3650 			break;
3651 		case '?':
3652 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3653 			    optopt);
3654 			usage(B_FALSE);
3655 		}
3656 	}
3657 
3658 	argc -= optind;
3659 	argv += optind;
3660 
3661 	if (cachefile && nsearch != 0) {
3662 		(void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
3663 		usage(B_FALSE);
3664 	}
3665 
3666 	if (cachefile && do_scan) {
3667 		(void) fprintf(stderr, gettext("-c is incompatible with -s\n"));
3668 		usage(B_FALSE);
3669 	}
3670 
3671 	if ((flags & ZFS_IMPORT_LOAD_KEYS) && (flags & ZFS_IMPORT_ONLY)) {
3672 		(void) fprintf(stderr, gettext("-l is incompatible with -N\n"));
3673 		usage(B_FALSE);
3674 	}
3675 
3676 	if ((flags & ZFS_IMPORT_LOAD_KEYS) && !do_all && argc == 0) {
3677 		(void) fprintf(stderr, gettext("-l is only meaningful during "
3678 		    "an import\n"));
3679 		usage(B_FALSE);
3680 	}
3681 
3682 	if ((dryrun || xtreme_rewind) && !do_rewind) {
3683 		(void) fprintf(stderr,
3684 		    gettext("-n or -X only meaningful with -F\n"));
3685 		usage(B_FALSE);
3686 	}
3687 	if (dryrun)
3688 		rewind_policy = ZPOOL_TRY_REWIND;
3689 	else if (do_rewind)
3690 		rewind_policy = ZPOOL_DO_REWIND;
3691 	if (xtreme_rewind)
3692 		rewind_policy |= ZPOOL_EXTREME_REWIND;
3693 
3694 	/* In the future, we can capture further policy and include it here */
3695 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3696 	    nvlist_add_uint64(policy, ZPOOL_LOAD_REQUEST_TXG, txg) != 0 ||
3697 	    nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY,
3698 	    rewind_policy) != 0)
3699 		goto error;
3700 
3701 	/* check argument count */
3702 	if (do_all) {
3703 		if (argc != 0) {
3704 			(void) fprintf(stderr, gettext("too many arguments\n"));
3705 			usage(B_FALSE);
3706 		}
3707 	} else {
3708 		if (argc > 2) {
3709 			(void) fprintf(stderr, gettext("too many arguments\n"));
3710 			usage(B_FALSE);
3711 		}
3712 	}
3713 
3714 	/*
3715 	 * Check for the effective uid.  We do this explicitly here because
3716 	 * otherwise any attempt to discover pools will silently fail.
3717 	 */
3718 	if (argc == 0 && geteuid() != 0) {
3719 		(void) fprintf(stderr, gettext("cannot "
3720 		    "discover pools: permission denied\n"));
3721 
3722 		free(searchdirs);
3723 		nvlist_free(props);
3724 		nvlist_free(policy);
3725 		return (1);
3726 	}
3727 
3728 	/*
3729 	 * Depending on the arguments given, we do one of the following:
3730 	 *
3731 	 *	<none>	Iterate through all pools and display information about
3732 	 *		each one.
3733 	 *
3734 	 *	-a	Iterate through all pools and try to import each one.
3735 	 *
3736 	 *	<id>	Find the pool that corresponds to the given GUID/pool
3737 	 *		name and import that one.
3738 	 *
3739 	 *	-D	Above options applies only to destroyed pools.
3740 	 */
3741 	if (argc != 0) {
3742 		char *endptr;
3743 
3744 		errno = 0;
3745 		searchguid = strtoull(argv[0], &endptr, 10);
3746 		if (errno != 0 || *endptr != '\0') {
3747 			searchname = argv[0];
3748 			searchguid = 0;
3749 		}
3750 		pool_specified = B_TRUE;
3751 
3752 		/*
3753 		 * User specified a name or guid.  Ensure it's unique.
3754 		 */
3755 		target_exists_args_t search = {searchname, searchguid};
3756 		pool_exists = zpool_iter(g_zfs, name_or_guid_exists, &search);
3757 	}
3758 
3759 	/*
3760 	 * Check the environment for the preferred search path.
3761 	 */
3762 	if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) {
3763 		char *dir, *tmp = NULL;
3764 
3765 		envdup = strdup(env);
3766 
3767 		for (dir = strtok_r(envdup, ":", &tmp);
3768 		    dir != NULL;
3769 		    dir = strtok_r(NULL, ":", &tmp)) {
3770 			searchdirs = safe_realloc(searchdirs,
3771 			    (nsearch + 1) * sizeof (char *));
3772 			searchdirs[nsearch++] = dir;
3773 		}
3774 	}
3775 
3776 	idata.path = searchdirs;
3777 	idata.paths = nsearch;
3778 	idata.poolname = searchname;
3779 	idata.guid = searchguid;
3780 	idata.cachefile = cachefile;
3781 	idata.scan = do_scan;
3782 	idata.policy = policy;
3783 
3784 	libpc_handle_t lpch = {
3785 		.lpc_lib_handle = g_zfs,
3786 		.lpc_ops = &libzfs_config_ops,
3787 		.lpc_printerr = B_TRUE
3788 	};
3789 	pools = zpool_search_import(&lpch, &idata);
3790 
3791 	if (pools != NULL && pool_exists &&
3792 	    (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
3793 		(void) fprintf(stderr, gettext("cannot import '%s': "
3794 		    "a pool with that name already exists\n"),
3795 		    argv[0]);
3796 		(void) fprintf(stderr, gettext("use the form '%s "
3797 		    "<pool | id> <newpool>' to give it a new name\n"),
3798 		    "zpool import");
3799 		err = 1;
3800 	} else if (pools == NULL && pool_exists) {
3801 		(void) fprintf(stderr, gettext("cannot import '%s': "
3802 		    "a pool with that name is already created/imported,\n"),
3803 		    argv[0]);
3804 		(void) fprintf(stderr, gettext("and no additional pools "
3805 		    "with that name were found\n"));
3806 		err = 1;
3807 	} else if (pools == NULL) {
3808 		if (argc != 0) {
3809 			(void) fprintf(stderr, gettext("cannot import '%s': "
3810 			    "no such pool available\n"), argv[0]);
3811 		}
3812 		err = 1;
3813 	}
3814 
3815 	if (err == 1) {
3816 		free(searchdirs);
3817 		free(envdup);
3818 		nvlist_free(policy);
3819 		nvlist_free(pools);
3820 		nvlist_free(props);
3821 		return (1);
3822 	}
3823 
3824 	err = import_pools(pools, props, mntopts, flags,
3825 	    argc >= 1 ? argv[0] : NULL,
3826 	    argc >= 2 ? argv[1] : NULL,
3827 	    do_destroyed, pool_specified, do_all, &idata);
3828 
3829 	/*
3830 	 * If we're using the cachefile and we failed to import, then
3831 	 * fallback to scanning the directory for pools that match
3832 	 * those in the cachefile.
3833 	 */
3834 	if (err != 0 && cachefile != NULL) {
3835 		(void) printf(gettext("cachefile import failed, retrying\n"));
3836 
3837 		/*
3838 		 * We use the scan flag to gather the directories that exist
3839 		 * in the cachefile. If we need to fallback to searching for
3840 		 * the pool config, we will only search devices in these
3841 		 * directories.
3842 		 */
3843 		idata.scan = B_TRUE;
3844 		nvlist_free(pools);
3845 		pools = zpool_search_import(&lpch, &idata);
3846 
3847 		err = import_pools(pools, props, mntopts, flags,
3848 		    argc >= 1 ? argv[0] : NULL,
3849 		    argc >= 2 ? argv[1] : NULL,
3850 		    do_destroyed, pool_specified, do_all, &idata);
3851 	}
3852 
3853 error:
3854 	nvlist_free(props);
3855 	nvlist_free(pools);
3856 	nvlist_free(policy);
3857 	free(searchdirs);
3858 	free(envdup);
3859 
3860 	return (err ? 1 : 0);
3861 }
3862 
3863 /*
3864  * zpool sync [-f] [pool] ...
3865  *
3866  * -f (undocumented) force uberblock (and config including zpool cache file)
3867  *    update.
3868  *
3869  * Sync the specified pool(s).
3870  * Without arguments "zpool sync" will sync all pools.
3871  * This command initiates TXG sync(s) and will return after the TXG(s) commit.
3872  *
3873  */
3874 static int
3875 zpool_do_sync(int argc, char **argv)
3876 {
3877 	int ret;
3878 	boolean_t force = B_FALSE;
3879 
3880 	/* check options */
3881 	while ((ret  = getopt(argc, argv, "f")) != -1) {
3882 		switch (ret) {
3883 		case 'f':
3884 			force = B_TRUE;
3885 			break;
3886 		case '?':
3887 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3888 			    optopt);
3889 			usage(B_FALSE);
3890 		}
3891 	}
3892 
3893 	argc -= optind;
3894 	argv += optind;
3895 
3896 	/* if argc == 0 we will execute zpool_sync_one on all pools */
3897 	ret = for_each_pool(argc, argv, B_FALSE, NULL, ZFS_TYPE_POOL,
3898 	    B_FALSE, zpool_sync_one, &force);
3899 
3900 	return (ret);
3901 }
3902 
3903 typedef struct iostat_cbdata {
3904 	uint64_t cb_flags;
3905 	int cb_namewidth;
3906 	int cb_iteration;
3907 	boolean_t cb_verbose;
3908 	boolean_t cb_literal;
3909 	boolean_t cb_scripted;
3910 	zpool_list_t *cb_list;
3911 	vdev_cmd_data_list_t *vcdl;
3912 	vdev_cbdata_t cb_vdevs;
3913 } iostat_cbdata_t;
3914 
3915 /*  iostat labels */
3916 typedef struct name_and_columns {
3917 	const char *name;	/* Column name */
3918 	unsigned int columns;	/* Center name to this number of columns */
3919 } name_and_columns_t;
3920 
3921 #define	IOSTAT_MAX_LABELS	15	/* Max number of labels on one line */
3922 
3923 static const name_and_columns_t iostat_top_labels[][IOSTAT_MAX_LABELS] =
3924 {
3925 	[IOS_DEFAULT] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
3926 	    {NULL}},
3927 	[IOS_LATENCY] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
3928 	    {"asyncq_wait", 2}, {"scrub", 1}, {"trim", 1}, {"rebuild", 1},
3929 	    {NULL}},
3930 	[IOS_QUEUES] = {{"syncq_read", 2}, {"syncq_write", 2},
3931 	    {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
3932 	    {"trimq_write", 2}, {"rebuildq_write", 2}, {NULL}},
3933 	[IOS_L_HISTO] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
3934 	    {"asyncq_wait", 2}, {NULL}},
3935 	[IOS_RQ_HISTO] = {{"sync_read", 2}, {"sync_write", 2},
3936 	    {"async_read", 2}, {"async_write", 2}, {"scrub", 2},
3937 	    {"trim", 2}, {"rebuild", 2}, {NULL}},
3938 };
3939 
3940 /* Shorthand - if "columns" field not set, default to 1 column */
3941 static const name_and_columns_t iostat_bottom_labels[][IOSTAT_MAX_LABELS] =
3942 {
3943 	[IOS_DEFAULT] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
3944 	    {"write"}, {NULL}},
3945 	[IOS_LATENCY] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3946 	    {"write"}, {"read"}, {"write"}, {"wait"}, {"wait"}, {"wait"},
3947 	    {NULL}},
3948 	[IOS_QUEUES] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
3949 	    {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"},
3950 	    {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL}},
3951 	[IOS_L_HISTO] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3952 	    {"write"}, {"read"}, {"write"}, {"scrub"}, {"trim"}, {"rebuild"},
3953 	    {NULL}},
3954 	[IOS_RQ_HISTO] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
3955 	    {"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
3956 	    {"ind"}, {"agg"}, {NULL}},
3957 };
3958 
3959 static const char *histo_to_title[] = {
3960 	[IOS_L_HISTO] = "latency",
3961 	[IOS_RQ_HISTO] = "req_size",
3962 };
3963 
3964 /*
3965  * Return the number of labels in a null-terminated name_and_columns_t
3966  * array.
3967  *
3968  */
3969 static unsigned int
3970 label_array_len(const name_and_columns_t *labels)
3971 {
3972 	int i = 0;
3973 
3974 	while (labels[i].name)
3975 		i++;
3976 
3977 	return (i);
3978 }
3979 
3980 /*
3981  * Return the number of strings in a null-terminated string array.
3982  * For example:
3983  *
3984  *     const char foo[] = {"bar", "baz", NULL}
3985  *
3986  * returns 2
3987  */
3988 static uint64_t
3989 str_array_len(const char *array[])
3990 {
3991 	uint64_t i = 0;
3992 	while (array[i])
3993 		i++;
3994 
3995 	return (i);
3996 }
3997 
3998 
3999 /*
4000  * Return a default column width for default/latency/queue columns. This does
4001  * not include histograms, which have their columns autosized.
4002  */
4003 static unsigned int
4004 default_column_width(iostat_cbdata_t *cb, enum iostat_type type)
4005 {
4006 	unsigned long column_width = 5; /* Normal niceprint */
4007 	static unsigned long widths[] = {
4008 		/*
4009 		 * Choose some sane default column sizes for printing the
4010 		 * raw numbers.
4011 		 */
4012 		[IOS_DEFAULT] = 15, /* 1PB capacity */
4013 		[IOS_LATENCY] = 10, /* 1B ns = 10sec */
4014 		[IOS_QUEUES] = 6,   /* 1M queue entries */
4015 		[IOS_L_HISTO] = 10, /* 1B ns = 10sec */
4016 		[IOS_RQ_HISTO] = 6, /* 1M queue entries */
4017 	};
4018 
4019 	if (cb->cb_literal)
4020 		column_width = widths[type];
4021 
4022 	return (column_width);
4023 }
4024 
4025 /*
4026  * Print the column labels, i.e:
4027  *
4028  *   capacity     operations     bandwidth
4029  * alloc   free   read  write   read  write  ...
4030  *
4031  * If force_column_width is set, use it for the column width.  If not set, use
4032  * the default column width.
4033  */
4034 static void
4035 print_iostat_labels(iostat_cbdata_t *cb, unsigned int force_column_width,
4036     const name_and_columns_t labels[][IOSTAT_MAX_LABELS])
4037 {
4038 	int i, idx, s;
4039 	int text_start, rw_column_width, spaces_to_end;
4040 	uint64_t flags = cb->cb_flags;
4041 	uint64_t f;
4042 	unsigned int column_width = force_column_width;
4043 
4044 	/* For each bit set in flags */
4045 	for (f = flags; f; f &= ~(1ULL << idx)) {
4046 		idx = lowbit64(f) - 1;
4047 		if (!force_column_width)
4048 			column_width = default_column_width(cb, idx);
4049 		/* Print our top labels centered over "read  write" label. */
4050 		for (i = 0; i < label_array_len(labels[idx]); i++) {
4051 			const char *name = labels[idx][i].name;
4052 			/*
4053 			 * We treat labels[][].columns == 0 as shorthand
4054 			 * for one column.  It makes writing out the label
4055 			 * tables more concise.
4056 			 */
4057 			unsigned int columns = MAX(1, labels[idx][i].columns);
4058 			unsigned int slen = strlen(name);
4059 
4060 			rw_column_width = (column_width * columns) +
4061 			    (2 * (columns - 1));
4062 
4063 			text_start = (int)((rw_column_width) / columns -
4064 			    slen / columns);
4065 			if (text_start < 0)
4066 				text_start = 0;
4067 
4068 			printf("  ");	/* Two spaces between columns */
4069 
4070 			/* Space from beginning of column to label */
4071 			for (s = 0; s < text_start; s++)
4072 				printf(" ");
4073 
4074 			printf("%s", name);
4075 
4076 			/* Print space after label to end of column */
4077 			spaces_to_end = rw_column_width - text_start - slen;
4078 			if (spaces_to_end < 0)
4079 				spaces_to_end = 0;
4080 
4081 			for (s = 0; s < spaces_to_end; s++)
4082 				printf(" ");
4083 		}
4084 	}
4085 }
4086 
4087 
4088 /*
4089  * print_cmd_columns - Print custom column titles from -c
4090  *
4091  * If the user specified the "zpool status|iostat -c" then print their custom
4092  * column titles in the header.  For example, print_cmd_columns() would print
4093  * the "  col1  col2" part of this:
4094  *
4095  * $ zpool iostat -vc 'echo col1=val1; echo col2=val2'
4096  * ...
4097  *	      capacity     operations     bandwidth
4098  * pool        alloc   free   read  write   read  write  col1  col2
4099  * ----------  -----  -----  -----  -----  -----  -----  ----  ----
4100  * mypool       269K  1008M      0      0    107    946
4101  *   mirror     269K  1008M      0      0    107    946
4102  *     sdb         -      -      0      0    102    473  val1  val2
4103  *     sdc         -      -      0      0      5    473  val1  val2
4104  * ----------  -----  -----  -----  -----  -----  -----  ----  ----
4105  */
4106 static void
4107 print_cmd_columns(vdev_cmd_data_list_t *vcdl, int use_dashes)
4108 {
4109 	int i, j;
4110 	vdev_cmd_data_t *data = &vcdl->data[0];
4111 
4112 	if (vcdl->count == 0 || data == NULL)
4113 		return;
4114 
4115 	/*
4116 	 * Each vdev cmd should have the same column names unless the user did
4117 	 * something weird with their cmd.  Just take the column names from the
4118 	 * first vdev and assume it works for all of them.
4119 	 */
4120 	for (i = 0; i < vcdl->uniq_cols_cnt; i++) {
4121 		printf("  ");
4122 		if (use_dashes) {
4123 			for (j = 0; j < vcdl->uniq_cols_width[i]; j++)
4124 				printf("-");
4125 		} else {
4126 			printf_color(ANSI_BOLD, "%*s", vcdl->uniq_cols_width[i],
4127 			    vcdl->uniq_cols[i]);
4128 		}
4129 	}
4130 }
4131 
4132 
4133 /*
4134  * Utility function to print out a line of dashes like:
4135  *
4136  * 	--------------------------------  -----  -----  -----  -----  -----
4137  *
4138  * ...or a dashed named-row line like:
4139  *
4140  * 	logs                                  -      -      -      -      -
4141  *
4142  * @cb:				iostat data
4143  *
4144  * @force_column_width		If non-zero, use the value as the column width.
4145  * 				Otherwise use the default column widths.
4146  *
4147  * @name:			Print a dashed named-row line starting
4148  * 				with @name.  Otherwise, print a regular
4149  * 				dashed line.
4150  */
4151 static void
4152 print_iostat_dashes(iostat_cbdata_t *cb, unsigned int force_column_width,
4153     const char *name)
4154 {
4155 	int i;
4156 	unsigned int namewidth;
4157 	uint64_t flags = cb->cb_flags;
4158 	uint64_t f;
4159 	int idx;
4160 	const name_and_columns_t *labels;
4161 	const char *title;
4162 
4163 
4164 	if (cb->cb_flags & IOS_ANYHISTO_M) {
4165 		title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
4166 	} else if (cb->cb_vdevs.cb_names_count) {
4167 		title = "vdev";
4168 	} else  {
4169 		title = "pool";
4170 	}
4171 
4172 	namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
4173 	    name ? strlen(name) : 0);
4174 
4175 
4176 	if (name) {
4177 		printf("%-*s", namewidth, name);
4178 	} else {
4179 		for (i = 0; i < namewidth; i++)
4180 			(void) printf("-");
4181 	}
4182 
4183 	/* For each bit in flags */
4184 	for (f = flags; f; f &= ~(1ULL << idx)) {
4185 		unsigned int column_width;
4186 		idx = lowbit64(f) - 1;
4187 		if (force_column_width)
4188 			column_width = force_column_width;
4189 		else
4190 			column_width = default_column_width(cb, idx);
4191 
4192 		labels = iostat_bottom_labels[idx];
4193 		for (i = 0; i < label_array_len(labels); i++) {
4194 			if (name)
4195 				printf("  %*s-", column_width - 1, " ");
4196 			else
4197 				printf("  %.*s", column_width,
4198 				    "--------------------");
4199 		}
4200 	}
4201 }
4202 
4203 
4204 static void
4205 print_iostat_separator_impl(iostat_cbdata_t *cb,
4206     unsigned int force_column_width)
4207 {
4208 	print_iostat_dashes(cb, force_column_width, NULL);
4209 }
4210 
4211 static void
4212 print_iostat_separator(iostat_cbdata_t *cb)
4213 {
4214 	print_iostat_separator_impl(cb, 0);
4215 }
4216 
4217 static void
4218 print_iostat_header_impl(iostat_cbdata_t *cb, unsigned int force_column_width,
4219     const char *histo_vdev_name)
4220 {
4221 	unsigned int namewidth;
4222 	const char *title;
4223 
4224 	color_start(ANSI_BOLD);
4225 
4226 	if (cb->cb_flags & IOS_ANYHISTO_M) {
4227 		title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
4228 	} else if (cb->cb_vdevs.cb_names_count) {
4229 		title = "vdev";
4230 	} else  {
4231 		title = "pool";
4232 	}
4233 
4234 	namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
4235 	    histo_vdev_name ? strlen(histo_vdev_name) : 0);
4236 
4237 	if (histo_vdev_name)
4238 		printf("%-*s", namewidth, histo_vdev_name);
4239 	else
4240 		printf("%*s", namewidth, "");
4241 
4242 
4243 	print_iostat_labels(cb, force_column_width, iostat_top_labels);
4244 	printf("\n");
4245 
4246 	printf("%-*s", namewidth, title);
4247 
4248 	print_iostat_labels(cb, force_column_width, iostat_bottom_labels);
4249 	if (cb->vcdl != NULL)
4250 		print_cmd_columns(cb->vcdl, 0);
4251 
4252 	printf("\n");
4253 
4254 	print_iostat_separator_impl(cb, force_column_width);
4255 
4256 	if (cb->vcdl != NULL)
4257 		print_cmd_columns(cb->vcdl, 1);
4258 
4259 	color_end();
4260 
4261 	printf("\n");
4262 }
4263 
4264 static void
4265 print_iostat_header(iostat_cbdata_t *cb)
4266 {
4267 	print_iostat_header_impl(cb, 0, NULL);
4268 }
4269 
4270 /*
4271  * Prints a size string (i.e. 120M) with the suffix ("M") colored
4272  * by order of magnitude. Uses column_size to add padding.
4273  */
4274 static void
4275 print_stat_color(char *statbuf, unsigned int column_size)
4276 {
4277 	fputs("  ", stdout);
4278 	if (*statbuf == '0') {
4279 		color_start(ANSI_GRAY);
4280 		fputc('0', stdout);
4281 		column_size--;
4282 	} else {
4283 		for (; *statbuf; statbuf++) {
4284 			if (*statbuf == 'K') color_start(ANSI_GREEN);
4285 			else if (*statbuf == 'M') color_start(ANSI_YELLOW);
4286 			else if (*statbuf == 'G') color_start(ANSI_RED);
4287 			else if (*statbuf == 'T') color_start(ANSI_BOLD_BLUE);
4288 			else if (*statbuf == 'P') color_start(ANSI_MAGENTA);
4289 			else if (*statbuf == 'E') color_start(ANSI_CYAN);
4290 			fputc(*statbuf, stdout);
4291 			if (--column_size <= 0)
4292 				break;
4293 		}
4294 	}
4295 	color_end();
4296 	for (; column_size > 0; column_size--)
4297 		fputc(' ', stdout);
4298 }
4299 
4300 /*
4301  * Display a single statistic.
4302  */
4303 static void
4304 print_one_stat(uint64_t value, enum zfs_nicenum_format format,
4305     unsigned int column_size, boolean_t scripted)
4306 {
4307 	char buf[64];
4308 
4309 	zfs_nicenum_format(value, buf, sizeof (buf), format);
4310 
4311 	if (scripted)
4312 		printf("\t%s", buf);
4313 	else
4314 		print_stat_color(buf, column_size);
4315 }
4316 
4317 /*
4318  * Calculate the default vdev stats
4319  *
4320  * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
4321  * stats into calcvs.
4322  */
4323 static void
4324 calc_default_iostats(vdev_stat_t *oldvs, vdev_stat_t *newvs,
4325     vdev_stat_t *calcvs)
4326 {
4327 	int i;
4328 
4329 	memcpy(calcvs, newvs, sizeof (*calcvs));
4330 	for (i = 0; i < ARRAY_SIZE(calcvs->vs_ops); i++)
4331 		calcvs->vs_ops[i] = (newvs->vs_ops[i] - oldvs->vs_ops[i]);
4332 
4333 	for (i = 0; i < ARRAY_SIZE(calcvs->vs_bytes); i++)
4334 		calcvs->vs_bytes[i] = (newvs->vs_bytes[i] - oldvs->vs_bytes[i]);
4335 }
4336 
4337 /*
4338  * Internal representation of the extended iostats data.
4339  *
4340  * The extended iostat stats are exported in nvlists as either uint64_t arrays
4341  * or single uint64_t's.  We make both look like arrays to make them easier
4342  * to process.  In order to make single uint64_t's look like arrays, we set
4343  * __data to the stat data, and then set *data = &__data with count = 1.  Then,
4344  * we can just use *data and count.
4345  */
4346 struct stat_array {
4347 	uint64_t *data;
4348 	uint_t count;	/* Number of entries in data[] */
4349 	uint64_t __data; /* Only used when data is a single uint64_t */
4350 };
4351 
4352 static uint64_t
4353 stat_histo_max(struct stat_array *nva, unsigned int len)
4354 {
4355 	uint64_t max = 0;
4356 	int i;
4357 	for (i = 0; i < len; i++)
4358 		max = MAX(max, array64_max(nva[i].data, nva[i].count));
4359 
4360 	return (max);
4361 }
4362 
4363 /*
4364  * Helper function to lookup a uint64_t array or uint64_t value and store its
4365  * data as a stat_array.  If the nvpair is a single uint64_t value, then we make
4366  * it look like a one element array to make it easier to process.
4367  */
4368 static int
4369 nvpair64_to_stat_array(nvlist_t *nvl, const char *name,
4370     struct stat_array *nva)
4371 {
4372 	nvpair_t *tmp;
4373 	int ret;
4374 
4375 	verify(nvlist_lookup_nvpair(nvl, name, &tmp) == 0);
4376 	switch (nvpair_type(tmp)) {
4377 	case DATA_TYPE_UINT64_ARRAY:
4378 		ret = nvpair_value_uint64_array(tmp, &nva->data, &nva->count);
4379 		break;
4380 	case DATA_TYPE_UINT64:
4381 		ret = nvpair_value_uint64(tmp, &nva->__data);
4382 		nva->data = &nva->__data;
4383 		nva->count = 1;
4384 		break;
4385 	default:
4386 		/* Not a uint64_t */
4387 		ret = EINVAL;
4388 		break;
4389 	}
4390 
4391 	return (ret);
4392 }
4393 
4394 /*
4395  * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
4396  * subtract them, and return the results in a newly allocated stat_array.
4397  * You must free the returned array after you are done with it with
4398  * free_calc_stats().
4399  *
4400  * Additionally, you can set "oldnv" to NULL if you simply want the newnv
4401  * values.
4402  */
4403 static struct stat_array *
4404 calc_and_alloc_stats_ex(const char **names, unsigned int len, nvlist_t *oldnv,
4405     nvlist_t *newnv)
4406 {
4407 	nvlist_t *oldnvx = NULL, *newnvx;
4408 	struct stat_array *oldnva, *newnva, *calcnva;
4409 	int i, j;
4410 	unsigned int alloc_size = (sizeof (struct stat_array)) * len;
4411 
4412 	/* Extract our extended stats nvlist from the main list */
4413 	verify(nvlist_lookup_nvlist(newnv, ZPOOL_CONFIG_VDEV_STATS_EX,
4414 	    &newnvx) == 0);
4415 	if (oldnv) {
4416 		verify(nvlist_lookup_nvlist(oldnv, ZPOOL_CONFIG_VDEV_STATS_EX,
4417 		    &oldnvx) == 0);
4418 	}
4419 
4420 	newnva = safe_malloc(alloc_size);
4421 	oldnva = safe_malloc(alloc_size);
4422 	calcnva = safe_malloc(alloc_size);
4423 
4424 	for (j = 0; j < len; j++) {
4425 		verify(nvpair64_to_stat_array(newnvx, names[j],
4426 		    &newnva[j]) == 0);
4427 		calcnva[j].count = newnva[j].count;
4428 		alloc_size = calcnva[j].count * sizeof (calcnva[j].data[0]);
4429 		calcnva[j].data = safe_malloc(alloc_size);
4430 		memcpy(calcnva[j].data, newnva[j].data, alloc_size);
4431 
4432 		if (oldnvx) {
4433 			verify(nvpair64_to_stat_array(oldnvx, names[j],
4434 			    &oldnva[j]) == 0);
4435 			for (i = 0; i < oldnva[j].count; i++)
4436 				calcnva[j].data[i] -= oldnva[j].data[i];
4437 		}
4438 	}
4439 	free(newnva);
4440 	free(oldnva);
4441 	return (calcnva);
4442 }
4443 
4444 static void
4445 free_calc_stats(struct stat_array *nva, unsigned int len)
4446 {
4447 	int i;
4448 	for (i = 0; i < len; i++)
4449 		free(nva[i].data);
4450 
4451 	free(nva);
4452 }
4453 
4454 static void
4455 print_iostat_histo(struct stat_array *nva, unsigned int len,
4456     iostat_cbdata_t *cb, unsigned int column_width, unsigned int namewidth,
4457     double scale)
4458 {
4459 	int i, j;
4460 	char buf[6];
4461 	uint64_t val;
4462 	enum zfs_nicenum_format format;
4463 	unsigned int buckets;
4464 	unsigned int start_bucket;
4465 
4466 	if (cb->cb_literal)
4467 		format = ZFS_NICENUM_RAW;
4468 	else
4469 		format = ZFS_NICENUM_1024;
4470 
4471 	/* All these histos are the same size, so just use nva[0].count */
4472 	buckets = nva[0].count;
4473 
4474 	if (cb->cb_flags & IOS_RQ_HISTO_M) {
4475 		/* Start at 512 - req size should never be lower than this */
4476 		start_bucket = 9;
4477 	} else {
4478 		start_bucket = 0;
4479 	}
4480 
4481 	for (j = start_bucket; j < buckets; j++) {
4482 		/* Print histogram bucket label */
4483 		if (cb->cb_flags & IOS_L_HISTO_M) {
4484 			/* Ending range of this bucket */
4485 			val = (1UL << (j + 1)) - 1;
4486 			zfs_nicetime(val, buf, sizeof (buf));
4487 		} else {
4488 			/* Request size (starting range of bucket) */
4489 			val = (1UL << j);
4490 			zfs_nicenum(val, buf, sizeof (buf));
4491 		}
4492 
4493 		if (cb->cb_scripted)
4494 			printf("%llu", (u_longlong_t)val);
4495 		else
4496 			printf("%-*s", namewidth, buf);
4497 
4498 		/* Print the values on the line */
4499 		for (i = 0; i < len; i++) {
4500 			print_one_stat(nva[i].data[j] * scale, format,
4501 			    column_width, cb->cb_scripted);
4502 		}
4503 		printf("\n");
4504 	}
4505 }
4506 
4507 static void
4508 print_solid_separator(unsigned int length)
4509 {
4510 	while (length--)
4511 		printf("-");
4512 	printf("\n");
4513 }
4514 
4515 static void
4516 print_iostat_histos(iostat_cbdata_t *cb, nvlist_t *oldnv,
4517     nvlist_t *newnv, double scale, const char *name)
4518 {
4519 	unsigned int column_width;
4520 	unsigned int namewidth;
4521 	unsigned int entire_width;
4522 	enum iostat_type type;
4523 	struct stat_array *nva;
4524 	const char **names;
4525 	unsigned int names_len;
4526 
4527 	/* What type of histo are we? */
4528 	type = IOS_HISTO_IDX(cb->cb_flags);
4529 
4530 	/* Get NULL-terminated array of nvlist names for our histo */
4531 	names = vsx_type_to_nvlist[type];
4532 	names_len = str_array_len(names); /* num of names */
4533 
4534 	nva = calc_and_alloc_stats_ex(names, names_len, oldnv, newnv);
4535 
4536 	if (cb->cb_literal) {
4537 		column_width = MAX(5,
4538 		    (unsigned int) log10(stat_histo_max(nva, names_len)) + 1);
4539 	} else {
4540 		column_width = 5;
4541 	}
4542 
4543 	namewidth = MAX(cb->cb_namewidth,
4544 	    strlen(histo_to_title[IOS_HISTO_IDX(cb->cb_flags)]));
4545 
4546 	/*
4547 	 * Calculate the entire line width of what we're printing.  The
4548 	 * +2 is for the two spaces between columns:
4549 	 */
4550 	/*	 read  write				*/
4551 	/*	-----  -----				*/
4552 	/*	|___|  <---------- column_width		*/
4553 	/*						*/
4554 	/*	|__________|  <--- entire_width		*/
4555 	/*						*/
4556 	entire_width = namewidth + (column_width + 2) *
4557 	    label_array_len(iostat_bottom_labels[type]);
4558 
4559 	if (cb->cb_scripted)
4560 		printf("%s\n", name);
4561 	else
4562 		print_iostat_header_impl(cb, column_width, name);
4563 
4564 	print_iostat_histo(nva, names_len, cb, column_width,
4565 	    namewidth, scale);
4566 
4567 	free_calc_stats(nva, names_len);
4568 	if (!cb->cb_scripted)
4569 		print_solid_separator(entire_width);
4570 }
4571 
4572 /*
4573  * Calculate the average latency of a power-of-two latency histogram
4574  */
4575 static uint64_t
4576 single_histo_average(uint64_t *histo, unsigned int buckets)
4577 {
4578 	int i;
4579 	uint64_t count = 0, total = 0;
4580 
4581 	for (i = 0; i < buckets; i++) {
4582 		/*
4583 		 * Our buckets are power-of-two latency ranges.  Use the
4584 		 * midpoint latency of each bucket to calculate the average.
4585 		 * For example:
4586 		 *
4587 		 * Bucket          Midpoint
4588 		 * 8ns-15ns:       12ns
4589 		 * 16ns-31ns:      24ns
4590 		 * ...
4591 		 */
4592 		if (histo[i] != 0) {
4593 			total += histo[i] * (((1UL << i) + ((1UL << i)/2)));
4594 			count += histo[i];
4595 		}
4596 	}
4597 
4598 	/* Prevent divide by zero */
4599 	return (count == 0 ? 0 : total / count);
4600 }
4601 
4602 static void
4603 print_iostat_queues(iostat_cbdata_t *cb, nvlist_t *newnv)
4604 {
4605 	const char *names[] = {
4606 		ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE,
4607 		ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
4608 		ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE,
4609 		ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
4610 		ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE,
4611 		ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
4612 		ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE,
4613 		ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
4614 		ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE,
4615 		ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
4616 		ZPOOL_CONFIG_VDEV_TRIM_PEND_QUEUE,
4617 		ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE,
4618 		ZPOOL_CONFIG_VDEV_REBUILD_PEND_QUEUE,
4619 		ZPOOL_CONFIG_VDEV_REBUILD_ACTIVE_QUEUE,
4620 	};
4621 
4622 	struct stat_array *nva;
4623 
4624 	unsigned int column_width = default_column_width(cb, IOS_QUEUES);
4625 	enum zfs_nicenum_format format;
4626 
4627 	nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), NULL, newnv);
4628 
4629 	if (cb->cb_literal)
4630 		format = ZFS_NICENUM_RAW;
4631 	else
4632 		format = ZFS_NICENUM_1024;
4633 
4634 	for (int i = 0; i < ARRAY_SIZE(names); i++) {
4635 		uint64_t val = nva[i].data[0];
4636 		print_one_stat(val, format, column_width, cb->cb_scripted);
4637 	}
4638 
4639 	free_calc_stats(nva, ARRAY_SIZE(names));
4640 }
4641 
4642 static void
4643 print_iostat_latency(iostat_cbdata_t *cb, nvlist_t *oldnv,
4644     nvlist_t *newnv)
4645 {
4646 	int i;
4647 	uint64_t val;
4648 	const char *names[] = {
4649 		ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
4650 		ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
4651 		ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
4652 		ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
4653 		ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
4654 		ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
4655 		ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
4656 		ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
4657 		ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
4658 		ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO,
4659 		ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO,
4660 	};
4661 	struct stat_array *nva;
4662 
4663 	unsigned int column_width = default_column_width(cb, IOS_LATENCY);
4664 	enum zfs_nicenum_format format;
4665 
4666 	nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), oldnv, newnv);
4667 
4668 	if (cb->cb_literal)
4669 		format = ZFS_NICENUM_RAWTIME;
4670 	else
4671 		format = ZFS_NICENUM_TIME;
4672 
4673 	/* Print our avg latencies on the line */
4674 	for (i = 0; i < ARRAY_SIZE(names); i++) {
4675 		/* Compute average latency for a latency histo */
4676 		val = single_histo_average(nva[i].data, nva[i].count);
4677 		print_one_stat(val, format, column_width, cb->cb_scripted);
4678 	}
4679 	free_calc_stats(nva, ARRAY_SIZE(names));
4680 }
4681 
4682 /*
4683  * Print default statistics (capacity/operations/bandwidth)
4684  */
4685 static void
4686 print_iostat_default(vdev_stat_t *vs, iostat_cbdata_t *cb, double scale)
4687 {
4688 	unsigned int column_width = default_column_width(cb, IOS_DEFAULT);
4689 	enum zfs_nicenum_format format;
4690 	char na;	/* char to print for "not applicable" values */
4691 
4692 	if (cb->cb_literal) {
4693 		format = ZFS_NICENUM_RAW;
4694 		na = '0';
4695 	} else {
4696 		format = ZFS_NICENUM_1024;
4697 		na = '-';
4698 	}
4699 
4700 	/* only toplevel vdevs have capacity stats */
4701 	if (vs->vs_space == 0) {
4702 		if (cb->cb_scripted)
4703 			printf("\t%c\t%c", na, na);
4704 		else
4705 			printf("  %*c  %*c", column_width, na, column_width,
4706 			    na);
4707 	} else {
4708 		print_one_stat(vs->vs_alloc, format, column_width,
4709 		    cb->cb_scripted);
4710 		print_one_stat(vs->vs_space - vs->vs_alloc, format,
4711 		    column_width, cb->cb_scripted);
4712 	}
4713 
4714 	print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_READ] * scale),
4715 	    format, column_width, cb->cb_scripted);
4716 	print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_WRITE] * scale),
4717 	    format, column_width, cb->cb_scripted);
4718 	print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_READ] * scale),
4719 	    format, column_width, cb->cb_scripted);
4720 	print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_WRITE] * scale),
4721 	    format, column_width, cb->cb_scripted);
4722 }
4723 
4724 static const char *const class_name[] = {
4725 	VDEV_ALLOC_BIAS_DEDUP,
4726 	VDEV_ALLOC_BIAS_SPECIAL,
4727 	VDEV_ALLOC_CLASS_LOGS
4728 };
4729 
4730 /*
4731  * Print out all the statistics for the given vdev.  This can either be the
4732  * toplevel configuration, or called recursively.  If 'name' is NULL, then this
4733  * is a verbose output, and we don't want to display the toplevel pool stats.
4734  *
4735  * Returns the number of stat lines printed.
4736  */
4737 static unsigned int
4738 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
4739     nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
4740 {
4741 	nvlist_t **oldchild, **newchild;
4742 	uint_t c, children, oldchildren;
4743 	vdev_stat_t *oldvs, *newvs, *calcvs;
4744 	vdev_stat_t zerovs = { 0 };
4745 	char *vname;
4746 	int i;
4747 	int ret = 0;
4748 	uint64_t tdelta;
4749 	double scale;
4750 
4751 	if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
4752 		return (ret);
4753 
4754 	calcvs = safe_malloc(sizeof (*calcvs));
4755 
4756 	if (oldnv != NULL) {
4757 		verify(nvlist_lookup_uint64_array(oldnv,
4758 		    ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
4759 	} else {
4760 		oldvs = &zerovs;
4761 	}
4762 
4763 	/* Do we only want to see a specific vdev? */
4764 	for (i = 0; i < cb->cb_vdevs.cb_names_count; i++) {
4765 		/* Yes we do.  Is this the vdev? */
4766 		if (strcmp(name, cb->cb_vdevs.cb_names[i]) == 0) {
4767 			/*
4768 			 * This is our vdev.  Since it is the only vdev we
4769 			 * will be displaying, make depth = 0 so that it
4770 			 * doesn't get indented.
4771 			 */
4772 			depth = 0;
4773 			break;
4774 		}
4775 	}
4776 
4777 	if (cb->cb_vdevs.cb_names_count && (i == cb->cb_vdevs.cb_names_count)) {
4778 		/* Couldn't match the name */
4779 		goto children;
4780 	}
4781 
4782 
4783 	verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
4784 	    (uint64_t **)&newvs, &c) == 0);
4785 
4786 	/*
4787 	 * Print the vdev name unless it's is a histogram.  Histograms
4788 	 * display the vdev name in the header itself.
4789 	 */
4790 	if (!(cb->cb_flags & IOS_ANYHISTO_M)) {
4791 		if (cb->cb_scripted) {
4792 			printf("%s", name);
4793 		} else {
4794 			if (strlen(name) + depth > cb->cb_namewidth)
4795 				(void) printf("%*s%s", depth, "", name);
4796 			else
4797 				(void) printf("%*s%s%*s", depth, "", name,
4798 				    (int)(cb->cb_namewidth - strlen(name) -
4799 				    depth), "");
4800 		}
4801 	}
4802 
4803 	/* Calculate our scaling factor */
4804 	tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
4805 	if ((oldvs->vs_timestamp == 0) && (cb->cb_flags & IOS_ANYHISTO_M)) {
4806 		/*
4807 		 * If we specify printing histograms with no time interval, then
4808 		 * print the histogram numbers over the entire lifetime of the
4809 		 * vdev.
4810 		 */
4811 		scale = 1;
4812 	} else {
4813 		if (tdelta == 0)
4814 			scale = 1.0;
4815 		else
4816 			scale = (double)NANOSEC / tdelta;
4817 	}
4818 
4819 	if (cb->cb_flags & IOS_DEFAULT_M) {
4820 		calc_default_iostats(oldvs, newvs, calcvs);
4821 		print_iostat_default(calcvs, cb, scale);
4822 	}
4823 	if (cb->cb_flags & IOS_LATENCY_M)
4824 		print_iostat_latency(cb, oldnv, newnv);
4825 	if (cb->cb_flags & IOS_QUEUES_M)
4826 		print_iostat_queues(cb, newnv);
4827 	if (cb->cb_flags & IOS_ANYHISTO_M) {
4828 		printf("\n");
4829 		print_iostat_histos(cb, oldnv, newnv, scale, name);
4830 	}
4831 
4832 	if (cb->vcdl != NULL) {
4833 		const char *path;
4834 		if (nvlist_lookup_string(newnv, ZPOOL_CONFIG_PATH,
4835 		    &path) == 0) {
4836 			printf("  ");
4837 			zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
4838 		}
4839 	}
4840 
4841 	if (!(cb->cb_flags & IOS_ANYHISTO_M))
4842 		printf("\n");
4843 
4844 	ret++;
4845 
4846 children:
4847 
4848 	free(calcvs);
4849 
4850 	if (!cb->cb_verbose)
4851 		return (ret);
4852 
4853 	if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
4854 	    &newchild, &children) != 0)
4855 		return (ret);
4856 
4857 	if (oldnv) {
4858 		if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
4859 		    &oldchild, &oldchildren) != 0)
4860 			return (ret);
4861 
4862 		children = MIN(oldchildren, children);
4863 	}
4864 
4865 	/*
4866 	 * print normal top-level devices
4867 	 */
4868 	for (c = 0; c < children; c++) {
4869 		uint64_t ishole = B_FALSE, islog = B_FALSE;
4870 
4871 		(void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
4872 		    &ishole);
4873 
4874 		(void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
4875 		    &islog);
4876 
4877 		if (ishole || islog)
4878 			continue;
4879 
4880 		if (nvlist_exists(newchild[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
4881 			continue;
4882 
4883 		vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
4884 		    cb->cb_vdevs.cb_name_flags | VDEV_NAME_TYPE_ID);
4885 		ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
4886 		    newchild[c], cb, depth + 2);
4887 		free(vname);
4888 	}
4889 
4890 	/*
4891 	 * print all other top-level devices
4892 	 */
4893 	for (uint_t n = 0; n < ARRAY_SIZE(class_name); n++) {
4894 		boolean_t printed = B_FALSE;
4895 
4896 		for (c = 0; c < children; c++) {
4897 			uint64_t islog = B_FALSE;
4898 			const char *bias = NULL;
4899 			const char *type = NULL;
4900 
4901 			(void) nvlist_lookup_uint64(newchild[c],
4902 			    ZPOOL_CONFIG_IS_LOG, &islog);
4903 			if (islog) {
4904 				bias = VDEV_ALLOC_CLASS_LOGS;
4905 			} else {
4906 				(void) nvlist_lookup_string(newchild[c],
4907 				    ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
4908 				(void) nvlist_lookup_string(newchild[c],
4909 				    ZPOOL_CONFIG_TYPE, &type);
4910 			}
4911 			if (bias == NULL || strcmp(bias, class_name[n]) != 0)
4912 				continue;
4913 			if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0)
4914 				continue;
4915 
4916 			if (!printed) {
4917 				if ((!(cb->cb_flags & IOS_ANYHISTO_M)) &&
4918 				    !cb->cb_scripted &&
4919 				    !cb->cb_vdevs.cb_names) {
4920 					print_iostat_dashes(cb, 0,
4921 					    class_name[n]);
4922 				}
4923 				printf("\n");
4924 				printed = B_TRUE;
4925 			}
4926 
4927 			vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
4928 			    cb->cb_vdevs.cb_name_flags | VDEV_NAME_TYPE_ID);
4929 			ret += print_vdev_stats(zhp, vname, oldnv ?
4930 			    oldchild[c] : NULL, newchild[c], cb, depth + 2);
4931 			free(vname);
4932 		}
4933 	}
4934 
4935 	/*
4936 	 * Include level 2 ARC devices in iostat output
4937 	 */
4938 	if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
4939 	    &newchild, &children) != 0)
4940 		return (ret);
4941 
4942 	if (oldnv) {
4943 		if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
4944 		    &oldchild, &oldchildren) != 0)
4945 			return (ret);
4946 
4947 		children = MIN(oldchildren, children);
4948 	}
4949 
4950 	if (children > 0) {
4951 		if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted &&
4952 		    !cb->cb_vdevs.cb_names) {
4953 			print_iostat_dashes(cb, 0, "cache");
4954 		}
4955 		printf("\n");
4956 
4957 		for (c = 0; c < children; c++) {
4958 			vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
4959 			    cb->cb_vdevs.cb_name_flags);
4960 			ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c]
4961 			    : NULL, newchild[c], cb, depth + 2);
4962 			free(vname);
4963 		}
4964 	}
4965 
4966 	return (ret);
4967 }
4968 
4969 static int
4970 refresh_iostat(zpool_handle_t *zhp, void *data)
4971 {
4972 	iostat_cbdata_t *cb = data;
4973 	boolean_t missing;
4974 
4975 	/*
4976 	 * If the pool has disappeared, remove it from the list and continue.
4977 	 */
4978 	if (zpool_refresh_stats(zhp, &missing) != 0)
4979 		return (-1);
4980 
4981 	if (missing)
4982 		pool_list_remove(cb->cb_list, zhp);
4983 
4984 	return (0);
4985 }
4986 
4987 /*
4988  * Callback to print out the iostats for the given pool.
4989  */
4990 static int
4991 print_iostat(zpool_handle_t *zhp, void *data)
4992 {
4993 	iostat_cbdata_t *cb = data;
4994 	nvlist_t *oldconfig, *newconfig;
4995 	nvlist_t *oldnvroot, *newnvroot;
4996 	int ret;
4997 
4998 	newconfig = zpool_get_config(zhp, &oldconfig);
4999 
5000 	if (cb->cb_iteration == 1)
5001 		oldconfig = NULL;
5002 
5003 	verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
5004 	    &newnvroot) == 0);
5005 
5006 	if (oldconfig == NULL)
5007 		oldnvroot = NULL;
5008 	else
5009 		verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
5010 		    &oldnvroot) == 0);
5011 
5012 	ret = print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot,
5013 	    cb, 0);
5014 	if ((ret != 0) && !(cb->cb_flags & IOS_ANYHISTO_M) &&
5015 	    !cb->cb_scripted && cb->cb_verbose &&
5016 	    !cb->cb_vdevs.cb_names_count) {
5017 		print_iostat_separator(cb);
5018 		if (cb->vcdl != NULL) {
5019 			print_cmd_columns(cb->vcdl, 1);
5020 		}
5021 		printf("\n");
5022 	}
5023 
5024 	return (ret);
5025 }
5026 
5027 static int
5028 get_columns(void)
5029 {
5030 	struct winsize ws;
5031 	int columns = 80;
5032 	int error;
5033 
5034 	if (isatty(STDOUT_FILENO)) {
5035 		error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
5036 		if (error == 0)
5037 			columns = ws.ws_col;
5038 	} else {
5039 		columns = 999;
5040 	}
5041 
5042 	return (columns);
5043 }
5044 
5045 /*
5046  * Return the required length of the pool/vdev name column.  The minimum
5047  * allowed width and output formatting flags must be provided.
5048  */
5049 static int
5050 get_namewidth(zpool_handle_t *zhp, int min_width, int flags, boolean_t verbose)
5051 {
5052 	nvlist_t *config, *nvroot;
5053 	int width = min_width;
5054 
5055 	if ((config = zpool_get_config(zhp, NULL)) != NULL) {
5056 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
5057 		    &nvroot) == 0);
5058 		size_t poolname_len = strlen(zpool_get_name(zhp));
5059 		if (verbose == B_FALSE) {
5060 			width = MAX(poolname_len, min_width);
5061 		} else {
5062 			width = MAX(poolname_len,
5063 			    max_width(zhp, nvroot, 0, min_width, flags));
5064 		}
5065 	}
5066 
5067 	return (width);
5068 }
5069 
5070 /*
5071  * Parse the input string, get the 'interval' and 'count' value if there is one.
5072  */
5073 static void
5074 get_interval_count(int *argcp, char **argv, float *iv,
5075     unsigned long *cnt)
5076 {
5077 	float interval = 0;
5078 	unsigned long count = 0;
5079 	int argc = *argcp;
5080 
5081 	/*
5082 	 * Determine if the last argument is an integer or a pool name
5083 	 */
5084 	if (argc > 0 && zfs_isnumber(argv[argc - 1])) {
5085 		char *end;
5086 
5087 		errno = 0;
5088 		interval = strtof(argv[argc - 1], &end);
5089 
5090 		if (*end == '\0' && errno == 0) {
5091 			if (interval == 0) {
5092 				(void) fprintf(stderr, gettext(
5093 				    "interval cannot be zero\n"));
5094 				usage(B_FALSE);
5095 			}
5096 			/*
5097 			 * Ignore the last parameter
5098 			 */
5099 			argc--;
5100 		} else {
5101 			/*
5102 			 * If this is not a valid number, just plow on.  The
5103 			 * user will get a more informative error message later
5104 			 * on.
5105 			 */
5106 			interval = 0;
5107 		}
5108 	}
5109 
5110 	/*
5111 	 * If the last argument is also an integer, then we have both a count
5112 	 * and an interval.
5113 	 */
5114 	if (argc > 0 && zfs_isnumber(argv[argc - 1])) {
5115 		char *end;
5116 
5117 		errno = 0;
5118 		count = interval;
5119 		interval = strtof(argv[argc - 1], &end);
5120 
5121 		if (*end == '\0' && errno == 0) {
5122 			if (interval == 0) {
5123 				(void) fprintf(stderr, gettext(
5124 				    "interval cannot be zero\n"));
5125 				usage(B_FALSE);
5126 			}
5127 
5128 			/*
5129 			 * Ignore the last parameter
5130 			 */
5131 			argc--;
5132 		} else {
5133 			interval = 0;
5134 		}
5135 	}
5136 
5137 	*iv = interval;
5138 	*cnt = count;
5139 	*argcp = argc;
5140 }
5141 
5142 static void
5143 get_timestamp_arg(char c)
5144 {
5145 	if (c == 'u')
5146 		timestamp_fmt = UDATE;
5147 	else if (c == 'd')
5148 		timestamp_fmt = DDATE;
5149 	else
5150 		usage(B_FALSE);
5151 }
5152 
5153 /*
5154  * Return stat flags that are supported by all pools by both the module and
5155  * zpool iostat.  "*data" should be initialized to all 0xFFs before running.
5156  * It will get ANDed down until only the flags that are supported on all pools
5157  * remain.
5158  */
5159 static int
5160 get_stat_flags_cb(zpool_handle_t *zhp, void *data)
5161 {
5162 	uint64_t *mask = data;
5163 	nvlist_t *config, *nvroot, *nvx;
5164 	uint64_t flags = 0;
5165 	int i, j;
5166 
5167 	config = zpool_get_config(zhp, NULL);
5168 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
5169 	    &nvroot) == 0);
5170 
5171 	/* Default stats are always supported, but for completeness.. */
5172 	if (nvlist_exists(nvroot, ZPOOL_CONFIG_VDEV_STATS))
5173 		flags |= IOS_DEFAULT_M;
5174 
5175 	/* Get our extended stats nvlist from the main list */
5176 	if (nvlist_lookup_nvlist(nvroot, ZPOOL_CONFIG_VDEV_STATS_EX,
5177 	    &nvx) != 0) {
5178 		/*
5179 		 * No extended stats; they're probably running an older
5180 		 * module.  No big deal, we support that too.
5181 		 */
5182 		goto end;
5183 	}
5184 
5185 	/* For each extended stat, make sure all its nvpairs are supported */
5186 	for (j = 0; j < ARRAY_SIZE(vsx_type_to_nvlist); j++) {
5187 		if (!vsx_type_to_nvlist[j][0])
5188 			continue;
5189 
5190 		/* Start off by assuming the flag is supported, then check */
5191 		flags |= (1ULL << j);
5192 		for (i = 0; vsx_type_to_nvlist[j][i]; i++) {
5193 			if (!nvlist_exists(nvx, vsx_type_to_nvlist[j][i])) {
5194 				/* flag isn't supported */
5195 				flags = flags & ~(1ULL  << j);
5196 				break;
5197 			}
5198 		}
5199 	}
5200 end:
5201 	*mask = *mask & flags;
5202 	return (0);
5203 }
5204 
5205 /*
5206  * Return a bitmask of stats that are supported on all pools by both the module
5207  * and zpool iostat.
5208  */
5209 static uint64_t
5210 get_stat_flags(zpool_list_t *list)
5211 {
5212 	uint64_t mask = -1;
5213 
5214 	/*
5215 	 * get_stat_flags_cb() will lop off bits from "mask" until only the
5216 	 * flags that are supported on all pools remain.
5217 	 */
5218 	pool_list_iter(list, B_FALSE, get_stat_flags_cb, &mask);
5219 	return (mask);
5220 }
5221 
5222 /*
5223  * Return 1 if cb_data->cb_names[0] is this vdev's name, 0 otherwise.
5224  */
5225 static int
5226 is_vdev_cb(void *zhp_data, nvlist_t *nv, void *cb_data)
5227 {
5228 	uint64_t guid;
5229 	vdev_cbdata_t *cb = cb_data;
5230 	zpool_handle_t *zhp = zhp_data;
5231 
5232 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
5233 		return (0);
5234 
5235 	return (guid == zpool_vdev_path_to_guid(zhp, cb->cb_names[0]));
5236 }
5237 
5238 /*
5239  * Returns 1 if cb_data->cb_names[0] is a vdev name, 0 otherwise.
5240  */
5241 static int
5242 is_vdev(zpool_handle_t *zhp, void *cb_data)
5243 {
5244 	return (for_each_vdev(zhp, is_vdev_cb, cb_data));
5245 }
5246 
5247 /*
5248  * Check if vdevs are in a pool
5249  *
5250  * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
5251  * return 0.  If pool_name is NULL, then search all pools.
5252  */
5253 static int
5254 are_vdevs_in_pool(int argc, char **argv, char *pool_name,
5255     vdev_cbdata_t *cb)
5256 {
5257 	char **tmp_name;
5258 	int ret = 0;
5259 	int i;
5260 	int pool_count = 0;
5261 
5262 	if ((argc == 0) || !*argv)
5263 		return (0);
5264 
5265 	if (pool_name)
5266 		pool_count = 1;
5267 
5268 	/* Temporarily hijack cb_names for a second... */
5269 	tmp_name = cb->cb_names;
5270 
5271 	/* Go though our list of prospective vdev names */
5272 	for (i = 0; i < argc; i++) {
5273 		cb->cb_names = argv + i;
5274 
5275 		/* Is this name a vdev in our pools? */
5276 		ret = for_each_pool(pool_count, &pool_name, B_TRUE, NULL,
5277 		    ZFS_TYPE_POOL, B_FALSE, is_vdev, cb);
5278 		if (!ret) {
5279 			/* No match */
5280 			break;
5281 		}
5282 	}
5283 
5284 	cb->cb_names = tmp_name;
5285 
5286 	return (ret);
5287 }
5288 
5289 static int
5290 is_pool_cb(zpool_handle_t *zhp, void *data)
5291 {
5292 	char *name = data;
5293 	if (strcmp(name, zpool_get_name(zhp)) == 0)
5294 		return (1);
5295 
5296 	return (0);
5297 }
5298 
5299 /*
5300  * Do we have a pool named *name?  If so, return 1, otherwise 0.
5301  */
5302 static int
5303 is_pool(char *name)
5304 {
5305 	return (for_each_pool(0, NULL, B_TRUE, NULL, ZFS_TYPE_POOL, B_FALSE,
5306 	    is_pool_cb, name));
5307 }
5308 
5309 /* Are all our argv[] strings pool names?  If so return 1, 0 otherwise. */
5310 static int
5311 are_all_pools(int argc, char **argv)
5312 {
5313 	if ((argc == 0) || !*argv)
5314 		return (0);
5315 
5316 	while (--argc >= 0)
5317 		if (!is_pool(argv[argc]))
5318 			return (0);
5319 
5320 	return (1);
5321 }
5322 
5323 /*
5324  * Helper function to print out vdev/pool names we can't resolve.  Used for an
5325  * error message.
5326  */
5327 static void
5328 error_list_unresolved_vdevs(int argc, char **argv, char *pool_name,
5329     vdev_cbdata_t *cb)
5330 {
5331 	int i;
5332 	char *name;
5333 	char *str;
5334 	for (i = 0; i < argc; i++) {
5335 		name = argv[i];
5336 
5337 		if (is_pool(name))
5338 			str = gettext("pool");
5339 		else if (are_vdevs_in_pool(1, &name, pool_name, cb))
5340 			str = gettext("vdev in this pool");
5341 		else if (are_vdevs_in_pool(1, &name, NULL, cb))
5342 			str = gettext("vdev in another pool");
5343 		else
5344 			str = gettext("unknown");
5345 
5346 		fprintf(stderr, "\t%s (%s)\n", name, str);
5347 	}
5348 }
5349 
5350 /*
5351  * Same as get_interval_count(), but with additional checks to not misinterpret
5352  * guids as interval/count values.  Assumes VDEV_NAME_GUID is set in
5353  * cb.cb_vdevs.cb_name_flags.
5354  */
5355 static void
5356 get_interval_count_filter_guids(int *argc, char **argv, float *interval,
5357     unsigned long *count, iostat_cbdata_t *cb)
5358 {
5359 	char **tmpargv = argv;
5360 	int argc_for_interval = 0;
5361 
5362 	/* Is the last arg an interval value?  Or a guid? */
5363 	if (*argc >= 1 && !are_vdevs_in_pool(1, &argv[*argc - 1], NULL,
5364 	    &cb->cb_vdevs)) {
5365 		/*
5366 		 * The last arg is not a guid, so it's probably an
5367 		 * interval value.
5368 		 */
5369 		argc_for_interval++;
5370 
5371 		if (*argc >= 2 &&
5372 		    !are_vdevs_in_pool(1, &argv[*argc - 2], NULL,
5373 		    &cb->cb_vdevs)) {
5374 			/*
5375 			 * The 2nd to last arg is not a guid, so it's probably
5376 			 * an interval value.
5377 			 */
5378 			argc_for_interval++;
5379 		}
5380 	}
5381 
5382 	/* Point to our list of possible intervals */
5383 	tmpargv = &argv[*argc - argc_for_interval];
5384 
5385 	*argc = *argc - argc_for_interval;
5386 	get_interval_count(&argc_for_interval, tmpargv,
5387 	    interval, count);
5388 }
5389 
5390 /*
5391  * Floating point sleep().  Allows you to pass in a floating point value for
5392  * seconds.
5393  */
5394 static void
5395 fsleep(float sec)
5396 {
5397 	struct timespec req;
5398 	req.tv_sec = floor(sec);
5399 	req.tv_nsec = (sec - (float)req.tv_sec) * NANOSEC;
5400 	nanosleep(&req, NULL);
5401 }
5402 
5403 /*
5404  * Terminal height, in rows. Returns -1 if stdout is not connected to a TTY or
5405  * if we were unable to determine its size.
5406  */
5407 static int
5408 terminal_height(void)
5409 {
5410 	struct winsize win;
5411 
5412 	if (isatty(STDOUT_FILENO) == 0)
5413 		return (-1);
5414 
5415 	if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1 && win.ws_row > 0)
5416 		return (win.ws_row);
5417 
5418 	return (-1);
5419 }
5420 
5421 /*
5422  * Run one of the zpool status/iostat -c scripts with the help (-h) option and
5423  * print the result.
5424  *
5425  * name:	Short name of the script ('iostat').
5426  * path:	Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
5427  */
5428 static void
5429 print_zpool_script_help(char *name, char *path)
5430 {
5431 	char *argv[] = {path, (char *)"-h", NULL};
5432 	char **lines = NULL;
5433 	int lines_cnt = 0;
5434 	int rc;
5435 
5436 	rc = libzfs_run_process_get_stdout_nopath(path, argv, NULL, &lines,
5437 	    &lines_cnt);
5438 	if (rc != 0 || lines == NULL || lines_cnt <= 0) {
5439 		if (lines != NULL)
5440 			libzfs_free_str_array(lines, lines_cnt);
5441 		return;
5442 	}
5443 
5444 	for (int i = 0; i < lines_cnt; i++)
5445 		if (!is_blank_str(lines[i]))
5446 			printf("  %-14s  %s\n", name, lines[i]);
5447 
5448 	libzfs_free_str_array(lines, lines_cnt);
5449 }
5450 
5451 /*
5452  * Go though the zpool status/iostat -c scripts in the user's path, run their
5453  * help option (-h), and print out the results.
5454  */
5455 static void
5456 print_zpool_dir_scripts(char *dirpath)
5457 {
5458 	DIR *dir;
5459 	struct dirent *ent;
5460 	char fullpath[MAXPATHLEN];
5461 	struct stat dir_stat;
5462 
5463 	if ((dir = opendir(dirpath)) != NULL) {
5464 		/* print all the files and directories within directory */
5465 		while ((ent = readdir(dir)) != NULL) {
5466 			if (snprintf(fullpath, sizeof (fullpath), "%s/%s",
5467 			    dirpath, ent->d_name) >= sizeof (fullpath)) {
5468 				(void) fprintf(stderr,
5469 				    gettext("internal error: "
5470 				    "ZPOOL_SCRIPTS_PATH too large.\n"));
5471 				exit(1);
5472 			}
5473 
5474 			/* Print the scripts */
5475 			if (stat(fullpath, &dir_stat) == 0)
5476 				if (dir_stat.st_mode & S_IXUSR &&
5477 				    S_ISREG(dir_stat.st_mode))
5478 					print_zpool_script_help(ent->d_name,
5479 					    fullpath);
5480 		}
5481 		closedir(dir);
5482 	}
5483 }
5484 
5485 /*
5486  * Print out help text for all zpool status/iostat -c scripts.
5487  */
5488 static void
5489 print_zpool_script_list(const char *subcommand)
5490 {
5491 	char *dir, *sp, *tmp;
5492 
5493 	printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand);
5494 
5495 	sp = zpool_get_cmd_search_path();
5496 	if (sp == NULL)
5497 		return;
5498 
5499 	for (dir = strtok_r(sp, ":", &tmp);
5500 	    dir != NULL;
5501 	    dir = strtok_r(NULL, ":", &tmp))
5502 		print_zpool_dir_scripts(dir);
5503 
5504 	free(sp);
5505 }
5506 
5507 /*
5508  * Set the minimum pool/vdev name column width.  The width must be at least 10,
5509  * but may be as large as the column width - 42 so it still fits on one line.
5510  * NOTE: 42 is the width of the default capacity/operations/bandwidth output
5511  */
5512 static int
5513 get_namewidth_iostat(zpool_handle_t *zhp, void *data)
5514 {
5515 	iostat_cbdata_t *cb = data;
5516 	int width, available_width;
5517 
5518 	/*
5519 	 * get_namewidth() returns the maximum width of any name in that column
5520 	 * for any pool/vdev/device line that will be output.
5521 	 */
5522 	width = get_namewidth(zhp, cb->cb_namewidth,
5523 	    cb->cb_vdevs.cb_name_flags | VDEV_NAME_TYPE_ID, cb->cb_verbose);
5524 
5525 	/*
5526 	 * The width we are calculating is the width of the header and also the
5527 	 * padding width for names that are less than maximum width.  The stats
5528 	 * take up 42 characters, so the width available for names is:
5529 	 */
5530 	available_width = get_columns() - 42;
5531 
5532 	/*
5533 	 * If the maximum width fits on a screen, then great!  Make everything
5534 	 * line up by justifying all lines to the same width.  If that max
5535 	 * width is larger than what's available, the name plus stats won't fit
5536 	 * on one line, and justifying to that width would cause every line to
5537 	 * wrap on the screen.  We only want lines with long names to wrap.
5538 	 * Limit the padding to what won't wrap.
5539 	 */
5540 	if (width > available_width)
5541 		width = available_width;
5542 
5543 	/*
5544 	 * And regardless of whatever the screen width is (get_columns can
5545 	 * return 0 if the width is not known or less than 42 for a narrow
5546 	 * terminal) have the width be a minimum of 10.
5547 	 */
5548 	if (width < 10)
5549 		width = 10;
5550 
5551 	/* Save the calculated width */
5552 	cb->cb_namewidth = width;
5553 
5554 	return (0);
5555 }
5556 
5557 /*
5558  * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
5559  *              [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
5560  *              [interval [count]]
5561  *
5562  *	-c CMD  For each vdev, run command CMD
5563  *	-g	Display guid for individual vdev name.
5564  *	-L	Follow links when resolving vdev path name.
5565  *	-P	Display full path for vdev name.
5566  *	-v	Display statistics for individual vdevs
5567  *	-h	Display help
5568  *	-p	Display values in parsable (exact) format.
5569  *	-H	Scripted mode.  Don't display headers, and separate properties
5570  *		by a single tab.
5571  *	-l	Display average latency
5572  *	-q	Display queue depths
5573  *	-w	Display latency histograms
5574  *	-r	Display request size histogram
5575  *	-T	Display a timestamp in date(1) or Unix format
5576  *	-n	Only print headers once
5577  *
5578  * This command can be tricky because we want to be able to deal with pool
5579  * creation/destruction as well as vdev configuration changes.  The bulk of this
5580  * processing is handled by the pool_list_* routines in zpool_iter.c.  We rely
5581  * on pool_list_update() to detect the addition of new pools.  Configuration
5582  * changes are all handled within libzfs.
5583  */
5584 int
5585 zpool_do_iostat(int argc, char **argv)
5586 {
5587 	int c;
5588 	int ret;
5589 	int npools;
5590 	float interval = 0;
5591 	unsigned long count = 0;
5592 	int winheight = 24;
5593 	zpool_list_t *list;
5594 	boolean_t verbose = B_FALSE;
5595 	boolean_t latency = B_FALSE, l_histo = B_FALSE, rq_histo = B_FALSE;
5596 	boolean_t queues = B_FALSE, parsable = B_FALSE, scripted = B_FALSE;
5597 	boolean_t omit_since_boot = B_FALSE;
5598 	boolean_t guid = B_FALSE;
5599 	boolean_t follow_links = B_FALSE;
5600 	boolean_t full_name = B_FALSE;
5601 	boolean_t headers_once = B_FALSE;
5602 	iostat_cbdata_t cb = { 0 };
5603 	char *cmd = NULL;
5604 
5605 	/* Used for printing error message */
5606 	const char flag_to_arg[] = {[IOS_LATENCY] = 'l', [IOS_QUEUES] = 'q',
5607 	    [IOS_L_HISTO] = 'w', [IOS_RQ_HISTO] = 'r'};
5608 
5609 	uint64_t unsupported_flags;
5610 
5611 	/* check options */
5612 	while ((c = getopt(argc, argv, "c:gLPT:vyhplqrwnH")) != -1) {
5613 		switch (c) {
5614 		case 'c':
5615 			if (cmd != NULL) {
5616 				fprintf(stderr,
5617 				    gettext("Can't set -c flag twice\n"));
5618 				exit(1);
5619 			}
5620 
5621 			if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL &&
5622 			    !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
5623 				fprintf(stderr, gettext(
5624 				    "Can't run -c, disabled by "
5625 				    "ZPOOL_SCRIPTS_ENABLED.\n"));
5626 				exit(1);
5627 			}
5628 
5629 			if ((getuid() <= 0 || geteuid() <= 0) &&
5630 			    !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
5631 				fprintf(stderr, gettext(
5632 				    "Can't run -c with root privileges "
5633 				    "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
5634 				exit(1);
5635 			}
5636 			cmd = optarg;
5637 			verbose = B_TRUE;
5638 			break;
5639 		case 'g':
5640 			guid = B_TRUE;
5641 			break;
5642 		case 'L':
5643 			follow_links = B_TRUE;
5644 			break;
5645 		case 'P':
5646 			full_name = B_TRUE;
5647 			break;
5648 		case 'T':
5649 			get_timestamp_arg(*optarg);
5650 			break;
5651 		case 'v':
5652 			verbose = B_TRUE;
5653 			break;
5654 		case 'p':
5655 			parsable = B_TRUE;
5656 			break;
5657 		case 'l':
5658 			latency = B_TRUE;
5659 			break;
5660 		case 'q':
5661 			queues = B_TRUE;
5662 			break;
5663 		case 'H':
5664 			scripted = B_TRUE;
5665 			break;
5666 		case 'w':
5667 			l_histo = B_TRUE;
5668 			break;
5669 		case 'r':
5670 			rq_histo = B_TRUE;
5671 			break;
5672 		case 'y':
5673 			omit_since_boot = B_TRUE;
5674 			break;
5675 		case 'n':
5676 			headers_once = B_TRUE;
5677 			break;
5678 		case 'h':
5679 			usage(B_FALSE);
5680 			break;
5681 		case '?':
5682 			if (optopt == 'c') {
5683 				print_zpool_script_list("iostat");
5684 				exit(0);
5685 			} else {
5686 				fprintf(stderr,
5687 				    gettext("invalid option '%c'\n"), optopt);
5688 			}
5689 			usage(B_FALSE);
5690 		}
5691 	}
5692 
5693 	argc -= optind;
5694 	argv += optind;
5695 
5696 	cb.cb_literal = parsable;
5697 	cb.cb_scripted = scripted;
5698 
5699 	if (guid)
5700 		cb.cb_vdevs.cb_name_flags |= VDEV_NAME_GUID;
5701 	if (follow_links)
5702 		cb.cb_vdevs.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
5703 	if (full_name)
5704 		cb.cb_vdevs.cb_name_flags |= VDEV_NAME_PATH;
5705 	cb.cb_iteration = 0;
5706 	cb.cb_namewidth = 0;
5707 	cb.cb_verbose = verbose;
5708 
5709 	/* Get our interval and count values (if any) */
5710 	if (guid) {
5711 		get_interval_count_filter_guids(&argc, argv, &interval,
5712 		    &count, &cb);
5713 	} else {
5714 		get_interval_count(&argc, argv, &interval, &count);
5715 	}
5716 
5717 	if (argc == 0) {
5718 		/* No args, so just print the defaults. */
5719 	} else if (are_all_pools(argc, argv)) {
5720 		/* All the args are pool names */
5721 	} else if (are_vdevs_in_pool(argc, argv, NULL, &cb.cb_vdevs)) {
5722 		/* All the args are vdevs */
5723 		cb.cb_vdevs.cb_names = argv;
5724 		cb.cb_vdevs.cb_names_count = argc;
5725 		argc = 0; /* No pools to process */
5726 	} else if (are_all_pools(1, argv)) {
5727 		/* The first arg is a pool name */
5728 		if (are_vdevs_in_pool(argc - 1, argv + 1, argv[0],
5729 		    &cb.cb_vdevs)) {
5730 			/* ...and the rest are vdev names */
5731 			cb.cb_vdevs.cb_names = argv + 1;
5732 			cb.cb_vdevs.cb_names_count = argc - 1;
5733 			argc = 1; /* One pool to process */
5734 		} else {
5735 			fprintf(stderr, gettext("Expected either a list of "));
5736 			fprintf(stderr, gettext("pools, or list of vdevs in"));
5737 			fprintf(stderr, " \"%s\", ", argv[0]);
5738 			fprintf(stderr, gettext("but got:\n"));
5739 			error_list_unresolved_vdevs(argc - 1, argv + 1,
5740 			    argv[0], &cb.cb_vdevs);
5741 			fprintf(stderr, "\n");
5742 			usage(B_FALSE);
5743 			return (1);
5744 		}
5745 	} else {
5746 		/*
5747 		 * The args don't make sense. The first arg isn't a pool name,
5748 		 * nor are all the args vdevs.
5749 		 */
5750 		fprintf(stderr, gettext("Unable to parse pools/vdevs list.\n"));
5751 		fprintf(stderr, "\n");
5752 		return (1);
5753 	}
5754 
5755 	if (cb.cb_vdevs.cb_names_count != 0) {
5756 		/*
5757 		 * If user specified vdevs, it implies verbose.
5758 		 */
5759 		cb.cb_verbose = B_TRUE;
5760 	}
5761 
5762 	/*
5763 	 * Construct the list of all interesting pools.
5764 	 */
5765 	ret = 0;
5766 	if ((list = pool_list_get(argc, argv, NULL, ZFS_TYPE_POOL, parsable,
5767 	    &ret)) == NULL)
5768 		return (1);
5769 
5770 	if (pool_list_count(list) == 0 && argc != 0) {
5771 		pool_list_free(list);
5772 		return (1);
5773 	}
5774 
5775 	if (pool_list_count(list) == 0 && interval == 0) {
5776 		pool_list_free(list);
5777 		(void) fprintf(stderr, gettext("no pools available\n"));
5778 		return (1);
5779 	}
5780 
5781 	if ((l_histo || rq_histo) && (cmd != NULL || latency || queues)) {
5782 		pool_list_free(list);
5783 		(void) fprintf(stderr,
5784 		    gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
5785 		usage(B_FALSE);
5786 		return (1);
5787 	}
5788 
5789 	if (l_histo && rq_histo) {
5790 		pool_list_free(list);
5791 		(void) fprintf(stderr,
5792 		    gettext("Only one of [-r|-w] can be passed at a time\n"));
5793 		usage(B_FALSE);
5794 		return (1);
5795 	}
5796 
5797 	/*
5798 	 * Enter the main iostat loop.
5799 	 */
5800 	cb.cb_list = list;
5801 
5802 	if (l_histo) {
5803 		/*
5804 		 * Histograms tables look out of place when you try to display
5805 		 * them with the other stats, so make a rule that you can only
5806 		 * print histograms by themselves.
5807 		 */
5808 		cb.cb_flags = IOS_L_HISTO_M;
5809 	} else if (rq_histo) {
5810 		cb.cb_flags = IOS_RQ_HISTO_M;
5811 	} else {
5812 		cb.cb_flags = IOS_DEFAULT_M;
5813 		if (latency)
5814 			cb.cb_flags |= IOS_LATENCY_M;
5815 		if (queues)
5816 			cb.cb_flags |= IOS_QUEUES_M;
5817 	}
5818 
5819 	/*
5820 	 * See if the module supports all the stats we want to display.
5821 	 */
5822 	unsupported_flags = cb.cb_flags & ~get_stat_flags(list);
5823 	if (unsupported_flags) {
5824 		uint64_t f;
5825 		int idx;
5826 		fprintf(stderr,
5827 		    gettext("The loaded zfs module doesn't support:"));
5828 
5829 		/* for each bit set in unsupported_flags */
5830 		for (f = unsupported_flags; f; f &= ~(1ULL << idx)) {
5831 			idx = lowbit64(f) - 1;
5832 			fprintf(stderr, " -%c", flag_to_arg[idx]);
5833 		}
5834 
5835 		fprintf(stderr, ".  Try running a newer module.\n");
5836 		pool_list_free(list);
5837 
5838 		return (1);
5839 	}
5840 
5841 	for (;;) {
5842 		if ((npools = pool_list_count(list)) == 0)
5843 			(void) fprintf(stderr, gettext("no pools available\n"));
5844 		else {
5845 			/*
5846 			 * If this is the first iteration and -y was supplied
5847 			 * we skip any printing.
5848 			 */
5849 			boolean_t skip = (omit_since_boot &&
5850 			    cb.cb_iteration == 0);
5851 
5852 			/*
5853 			 * Refresh all statistics.  This is done as an
5854 			 * explicit step before calculating the maximum name
5855 			 * width, so that any * configuration changes are
5856 			 * properly accounted for.
5857 			 */
5858 			(void) pool_list_iter(list, B_FALSE, refresh_iostat,
5859 			    &cb);
5860 
5861 			/*
5862 			 * Iterate over all pools to determine the maximum width
5863 			 * for the pool / device name column across all pools.
5864 			 */
5865 			cb.cb_namewidth = 0;
5866 			(void) pool_list_iter(list, B_FALSE,
5867 			    get_namewidth_iostat, &cb);
5868 
5869 			if (timestamp_fmt != NODATE)
5870 				print_timestamp(timestamp_fmt);
5871 
5872 			if (cmd != NULL && cb.cb_verbose &&
5873 			    !(cb.cb_flags & IOS_ANYHISTO_M)) {
5874 				cb.vcdl = all_pools_for_each_vdev_run(argc,
5875 				    argv, cmd, g_zfs, cb.cb_vdevs.cb_names,
5876 				    cb.cb_vdevs.cb_names_count,
5877 				    cb.cb_vdevs.cb_name_flags);
5878 			} else {
5879 				cb.vcdl = NULL;
5880 			}
5881 
5882 
5883 			/*
5884 			 * Check terminal size so we can print headers
5885 			 * even when terminal window has its height
5886 			 * changed.
5887 			 */
5888 			winheight = terminal_height();
5889 			/*
5890 			 * Are we connected to TTY? If not, headers_once
5891 			 * should be true, to avoid breaking scripts.
5892 			 */
5893 			if (winheight < 0)
5894 				headers_once = B_TRUE;
5895 
5896 			/*
5897 			 * If it's the first time and we're not skipping it,
5898 			 * or either skip or verbose mode, print the header.
5899 			 *
5900 			 * The histogram code explicitly prints its header on
5901 			 * every vdev, so skip this for histograms.
5902 			 */
5903 			if (((++cb.cb_iteration == 1 && !skip) ||
5904 			    (skip != verbose) ||
5905 			    (!headers_once &&
5906 			    (cb.cb_iteration % winheight) == 0)) &&
5907 			    (!(cb.cb_flags & IOS_ANYHISTO_M)) &&
5908 			    !cb.cb_scripted)
5909 				print_iostat_header(&cb);
5910 
5911 			if (skip) {
5912 				(void) fsleep(interval);
5913 				continue;
5914 			}
5915 
5916 			pool_list_iter(list, B_FALSE, print_iostat, &cb);
5917 
5918 			/*
5919 			 * If there's more than one pool, and we're not in
5920 			 * verbose mode (which prints a separator for us),
5921 			 * then print a separator.
5922 			 *
5923 			 * In addition, if we're printing specific vdevs then
5924 			 * we also want an ending separator.
5925 			 */
5926 			if (((npools > 1 && !verbose &&
5927 			    !(cb.cb_flags & IOS_ANYHISTO_M)) ||
5928 			    (!(cb.cb_flags & IOS_ANYHISTO_M) &&
5929 			    cb.cb_vdevs.cb_names_count)) &&
5930 			    !cb.cb_scripted) {
5931 				print_iostat_separator(&cb);
5932 				if (cb.vcdl != NULL)
5933 					print_cmd_columns(cb.vcdl, 1);
5934 				printf("\n");
5935 			}
5936 
5937 			if (cb.vcdl != NULL)
5938 				free_vdev_cmd_data_list(cb.vcdl);
5939 
5940 		}
5941 
5942 		/*
5943 		 * Flush the output so that redirection to a file isn't buffered
5944 		 * indefinitely.
5945 		 */
5946 		(void) fflush(stdout);
5947 
5948 		if (interval == 0)
5949 			break;
5950 
5951 		if (count != 0 && --count == 0)
5952 			break;
5953 
5954 		(void) fsleep(interval);
5955 	}
5956 
5957 	pool_list_free(list);
5958 
5959 	return (ret);
5960 }
5961 
5962 typedef struct list_cbdata {
5963 	boolean_t	cb_verbose;
5964 	int		cb_name_flags;
5965 	int		cb_namewidth;
5966 	boolean_t	cb_scripted;
5967 	zprop_list_t	*cb_proplist;
5968 	boolean_t	cb_literal;
5969 } list_cbdata_t;
5970 
5971 
5972 /*
5973  * Given a list of columns to display, output appropriate headers for each one.
5974  */
5975 static void
5976 print_header(list_cbdata_t *cb)
5977 {
5978 	zprop_list_t *pl = cb->cb_proplist;
5979 	char headerbuf[ZPOOL_MAXPROPLEN];
5980 	const char *header;
5981 	boolean_t first = B_TRUE;
5982 	boolean_t right_justify;
5983 	size_t width = 0;
5984 
5985 	for (; pl != NULL; pl = pl->pl_next) {
5986 		width = pl->pl_width;
5987 		if (first && cb->cb_verbose) {
5988 			/*
5989 			 * Reset the width to accommodate the verbose listing
5990 			 * of devices.
5991 			 */
5992 			width = cb->cb_namewidth;
5993 		}
5994 
5995 		if (!first)
5996 			(void) fputs("  ", stdout);
5997 		else
5998 			first = B_FALSE;
5999 
6000 		right_justify = B_FALSE;
6001 		if (pl->pl_prop != ZPROP_USERPROP) {
6002 			header = zpool_prop_column_name(pl->pl_prop);
6003 			right_justify = zpool_prop_align_right(pl->pl_prop);
6004 		} else {
6005 			int i;
6006 
6007 			for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
6008 				headerbuf[i] = toupper(pl->pl_user_prop[i]);
6009 			headerbuf[i] = '\0';
6010 			header = headerbuf;
6011 		}
6012 
6013 		if (pl->pl_next == NULL && !right_justify)
6014 			(void) fputs(header, stdout);
6015 		else if (right_justify)
6016 			(void) printf("%*s", (int)width, header);
6017 		else
6018 			(void) printf("%-*s", (int)width, header);
6019 	}
6020 
6021 	(void) fputc('\n', stdout);
6022 }
6023 
6024 /*
6025  * Given a pool and a list of properties, print out all the properties according
6026  * to the described layout. Used by zpool_do_list().
6027  */
6028 static void
6029 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
6030 {
6031 	zprop_list_t *pl = cb->cb_proplist;
6032 	boolean_t first = B_TRUE;
6033 	char property[ZPOOL_MAXPROPLEN];
6034 	const char *propstr;
6035 	boolean_t right_justify;
6036 	size_t width;
6037 
6038 	for (; pl != NULL; pl = pl->pl_next) {
6039 
6040 		width = pl->pl_width;
6041 		if (first && cb->cb_verbose) {
6042 			/*
6043 			 * Reset the width to accommodate the verbose listing
6044 			 * of devices.
6045 			 */
6046 			width = cb->cb_namewidth;
6047 		}
6048 
6049 		if (!first) {
6050 			if (cb->cb_scripted)
6051 				(void) fputc('\t', stdout);
6052 			else
6053 				(void) fputs("  ", stdout);
6054 		} else {
6055 			first = B_FALSE;
6056 		}
6057 
6058 		right_justify = B_FALSE;
6059 		if (pl->pl_prop != ZPROP_USERPROP) {
6060 			if (zpool_get_prop(zhp, pl->pl_prop, property,
6061 			    sizeof (property), NULL, cb->cb_literal) != 0)
6062 				propstr = "-";
6063 			else
6064 				propstr = property;
6065 
6066 			right_justify = zpool_prop_align_right(pl->pl_prop);
6067 		} else if ((zpool_prop_feature(pl->pl_user_prop) ||
6068 		    zpool_prop_unsupported(pl->pl_user_prop)) &&
6069 		    zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
6070 		    sizeof (property)) == 0) {
6071 			propstr = property;
6072 		} else {
6073 			propstr = "-";
6074 		}
6075 
6076 
6077 		/*
6078 		 * If this is being called in scripted mode, or if this is the
6079 		 * last column and it is left-justified, don't include a width
6080 		 * format specifier.
6081 		 */
6082 		if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
6083 			(void) fputs(propstr, stdout);
6084 		else if (right_justify)
6085 			(void) printf("%*s", (int)width, propstr);
6086 		else
6087 			(void) printf("%-*s", (int)width, propstr);
6088 	}
6089 
6090 	(void) fputc('\n', stdout);
6091 }
6092 
6093 static void
6094 print_one_column(zpool_prop_t prop, uint64_t value, const char *str,
6095     boolean_t scripted, boolean_t valid, enum zfs_nicenum_format format)
6096 {
6097 	char propval[64];
6098 	boolean_t fixed;
6099 	size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
6100 
6101 	switch (prop) {
6102 	case ZPOOL_PROP_SIZE:
6103 	case ZPOOL_PROP_EXPANDSZ:
6104 	case ZPOOL_PROP_CHECKPOINT:
6105 	case ZPOOL_PROP_DEDUPRATIO:
6106 		if (value == 0)
6107 			(void) strlcpy(propval, "-", sizeof (propval));
6108 		else
6109 			zfs_nicenum_format(value, propval, sizeof (propval),
6110 			    format);
6111 		break;
6112 	case ZPOOL_PROP_FRAGMENTATION:
6113 		if (value == ZFS_FRAG_INVALID) {
6114 			(void) strlcpy(propval, "-", sizeof (propval));
6115 		} else if (format == ZFS_NICENUM_RAW) {
6116 			(void) snprintf(propval, sizeof (propval), "%llu",
6117 			    (unsigned long long)value);
6118 		} else {
6119 			(void) snprintf(propval, sizeof (propval), "%llu%%",
6120 			    (unsigned long long)value);
6121 		}
6122 		break;
6123 	case ZPOOL_PROP_CAPACITY:
6124 		/* capacity value is in parts-per-10,000 (aka permyriad) */
6125 		if (format == ZFS_NICENUM_RAW)
6126 			(void) snprintf(propval, sizeof (propval), "%llu",
6127 			    (unsigned long long)value / 100);
6128 		else
6129 			(void) snprintf(propval, sizeof (propval),
6130 			    value < 1000 ? "%1.2f%%" : value < 10000 ?
6131 			    "%2.1f%%" : "%3.0f%%", value / 100.0);
6132 		break;
6133 	case ZPOOL_PROP_HEALTH:
6134 		width = 8;
6135 		(void) strlcpy(propval, str, sizeof (propval));
6136 		break;
6137 	default:
6138 		zfs_nicenum_format(value, propval, sizeof (propval), format);
6139 	}
6140 
6141 	if (!valid)
6142 		(void) strlcpy(propval, "-", sizeof (propval));
6143 
6144 	if (scripted)
6145 		(void) printf("\t%s", propval);
6146 	else
6147 		(void) printf("  %*s", (int)width, propval);
6148 }
6149 
6150 /*
6151  * print static default line per vdev
6152  * not compatible with '-o' <proplist> option
6153  */
6154 static void
6155 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
6156     list_cbdata_t *cb, int depth, boolean_t isspare)
6157 {
6158 	nvlist_t **child;
6159 	vdev_stat_t *vs;
6160 	uint_t c, children;
6161 	char *vname;
6162 	boolean_t scripted = cb->cb_scripted;
6163 	uint64_t islog = B_FALSE;
6164 	const char *dashes = "%-*s      -      -      -        -         "
6165 	    "-      -      -      -         -\n";
6166 
6167 	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
6168 	    (uint64_t **)&vs, &c) == 0);
6169 
6170 	if (name != NULL) {
6171 		boolean_t toplevel = (vs->vs_space != 0);
6172 		uint64_t cap;
6173 		enum zfs_nicenum_format format;
6174 		const char *state;
6175 
6176 		if (cb->cb_literal)
6177 			format = ZFS_NICENUM_RAW;
6178 		else
6179 			format = ZFS_NICENUM_1024;
6180 
6181 		if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
6182 			return;
6183 
6184 		if (scripted)
6185 			(void) printf("\t%s", name);
6186 		else if (strlen(name) + depth > cb->cb_namewidth)
6187 			(void) printf("%*s%s", depth, "", name);
6188 		else
6189 			(void) printf("%*s%s%*s", depth, "", name,
6190 			    (int)(cb->cb_namewidth - strlen(name) - depth), "");
6191 
6192 		/*
6193 		 * Print the properties for the individual vdevs. Some
6194 		 * properties are only applicable to toplevel vdevs. The
6195 		 * 'toplevel' boolean value is passed to the print_one_column()
6196 		 * to indicate that the value is valid.
6197 		 */
6198 		if (VDEV_STAT_VALID(vs_pspace, c) && vs->vs_pspace)
6199 			print_one_column(ZPOOL_PROP_SIZE, vs->vs_pspace, NULL,
6200 			    scripted, B_TRUE, format);
6201 		else
6202 			print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, NULL,
6203 			    scripted, toplevel, format);
6204 		print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, NULL,
6205 		    scripted, toplevel, format);
6206 		print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
6207 		    NULL, scripted, toplevel, format);
6208 		print_one_column(ZPOOL_PROP_CHECKPOINT,
6209 		    vs->vs_checkpoint_space, NULL, scripted, toplevel, format);
6210 		print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, NULL,
6211 		    scripted, B_TRUE, format);
6212 		print_one_column(ZPOOL_PROP_FRAGMENTATION,
6213 		    vs->vs_fragmentation, NULL, scripted,
6214 		    (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel),
6215 		    format);
6216 		cap = (vs->vs_space == 0) ? 0 :
6217 		    (vs->vs_alloc * 10000 / vs->vs_space);
6218 		print_one_column(ZPOOL_PROP_CAPACITY, cap, NULL,
6219 		    scripted, toplevel, format);
6220 		print_one_column(ZPOOL_PROP_DEDUPRATIO, 0, NULL,
6221 		    scripted, toplevel, format);
6222 		state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
6223 		if (isspare) {
6224 			if (vs->vs_aux == VDEV_AUX_SPARED)
6225 				state = "INUSE";
6226 			else if (vs->vs_state == VDEV_STATE_HEALTHY)
6227 				state = "AVAIL";
6228 		}
6229 		print_one_column(ZPOOL_PROP_HEALTH, 0, state, scripted,
6230 		    B_TRUE, format);
6231 		(void) fputc('\n', stdout);
6232 	}
6233 
6234 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
6235 	    &child, &children) != 0)
6236 		return;
6237 
6238 	/* list the normal vdevs first */
6239 	for (c = 0; c < children; c++) {
6240 		uint64_t ishole = B_FALSE;
6241 
6242 		if (nvlist_lookup_uint64(child[c],
6243 		    ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
6244 			continue;
6245 
6246 		if (nvlist_lookup_uint64(child[c],
6247 		    ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog)
6248 			continue;
6249 
6250 		if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
6251 			continue;
6252 
6253 		vname = zpool_vdev_name(g_zfs, zhp, child[c],
6254 		    cb->cb_name_flags | VDEV_NAME_TYPE_ID);
6255 		print_list_stats(zhp, vname, child[c], cb, depth + 2, B_FALSE);
6256 		free(vname);
6257 	}
6258 
6259 	/* list the classes: 'logs', 'dedup', and 'special' */
6260 	for (uint_t n = 0; n < ARRAY_SIZE(class_name); n++) {
6261 		boolean_t printed = B_FALSE;
6262 
6263 		for (c = 0; c < children; c++) {
6264 			const char *bias = NULL;
6265 			const char *type = NULL;
6266 
6267 			if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
6268 			    &islog) == 0 && islog) {
6269 				bias = VDEV_ALLOC_CLASS_LOGS;
6270 			} else {
6271 				(void) nvlist_lookup_string(child[c],
6272 				    ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
6273 				(void) nvlist_lookup_string(child[c],
6274 				    ZPOOL_CONFIG_TYPE, &type);
6275 			}
6276 			if (bias == NULL || strcmp(bias, class_name[n]) != 0)
6277 				continue;
6278 			if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0)
6279 				continue;
6280 
6281 			if (!printed) {
6282 				/* LINTED E_SEC_PRINTF_VAR_FMT */
6283 				(void) printf(dashes, cb->cb_namewidth,
6284 				    class_name[n]);
6285 				printed = B_TRUE;
6286 			}
6287 			vname = zpool_vdev_name(g_zfs, zhp, child[c],
6288 			    cb->cb_name_flags | VDEV_NAME_TYPE_ID);
6289 			print_list_stats(zhp, vname, child[c], cb, depth + 2,
6290 			    B_FALSE);
6291 			free(vname);
6292 		}
6293 	}
6294 
6295 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
6296 	    &child, &children) == 0 && children > 0) {
6297 		/* LINTED E_SEC_PRINTF_VAR_FMT */
6298 		(void) printf(dashes, cb->cb_namewidth, "cache");
6299 		for (c = 0; c < children; c++) {
6300 			vname = zpool_vdev_name(g_zfs, zhp, child[c],
6301 			    cb->cb_name_flags);
6302 			print_list_stats(zhp, vname, child[c], cb, depth + 2,
6303 			    B_FALSE);
6304 			free(vname);
6305 		}
6306 	}
6307 
6308 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child,
6309 	    &children) == 0 && children > 0) {
6310 		/* LINTED E_SEC_PRINTF_VAR_FMT */
6311 		(void) printf(dashes, cb->cb_namewidth, "spare");
6312 		for (c = 0; c < children; c++) {
6313 			vname = zpool_vdev_name(g_zfs, zhp, child[c],
6314 			    cb->cb_name_flags);
6315 			print_list_stats(zhp, vname, child[c], cb, depth + 2,
6316 			    B_TRUE);
6317 			free(vname);
6318 		}
6319 	}
6320 }
6321 
6322 /*
6323  * Generic callback function to list a pool.
6324  */
6325 static int
6326 list_callback(zpool_handle_t *zhp, void *data)
6327 {
6328 	list_cbdata_t *cbp = data;
6329 
6330 	print_pool(zhp, cbp);
6331 
6332 	if (cbp->cb_verbose) {
6333 		nvlist_t *config, *nvroot;
6334 
6335 		config = zpool_get_config(zhp, NULL);
6336 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
6337 		    &nvroot) == 0);
6338 		print_list_stats(zhp, NULL, nvroot, cbp, 0, B_FALSE);
6339 	}
6340 
6341 	return (0);
6342 }
6343 
6344 /*
6345  * Set the minimum pool/vdev name column width.  The width must be at least 9,
6346  * but may be as large as needed.
6347  */
6348 static int
6349 get_namewidth_list(zpool_handle_t *zhp, void *data)
6350 {
6351 	list_cbdata_t *cb = data;
6352 	int width;
6353 
6354 	width = get_namewidth(zhp, cb->cb_namewidth,
6355 	    cb->cb_name_flags | VDEV_NAME_TYPE_ID, cb->cb_verbose);
6356 
6357 	if (width < 9)
6358 		width = 9;
6359 
6360 	cb->cb_namewidth = width;
6361 
6362 	return (0);
6363 }
6364 
6365 /*
6366  * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
6367  *
6368  *	-g	Display guid for individual vdev name.
6369  *	-H	Scripted mode.  Don't display headers, and separate properties
6370  *		by a single tab.
6371  *	-L	Follow links when resolving vdev path name.
6372  *	-o	List of properties to display.  Defaults to
6373  *		"name,size,allocated,free,expandsize,fragmentation,capacity,"
6374  *		"dedupratio,health,altroot"
6375  *	-p	Display values in parsable (exact) format.
6376  *	-P	Display full path for vdev name.
6377  *	-T	Display a timestamp in date(1) or Unix format
6378  *
6379  * List all pools in the system, whether or not they're healthy.  Output space
6380  * statistics for each one, as well as health status summary.
6381  */
6382 int
6383 zpool_do_list(int argc, char **argv)
6384 {
6385 	int c;
6386 	int ret = 0;
6387 	list_cbdata_t cb = { 0 };
6388 	static char default_props[] =
6389 	    "name,size,allocated,free,checkpoint,expandsize,fragmentation,"
6390 	    "capacity,dedupratio,health,altroot";
6391 	char *props = default_props;
6392 	float interval = 0;
6393 	unsigned long count = 0;
6394 	zpool_list_t *list;
6395 	boolean_t first = B_TRUE;
6396 	current_prop_type = ZFS_TYPE_POOL;
6397 
6398 	/* check options */
6399 	while ((c = getopt(argc, argv, ":gHLo:pPT:v")) != -1) {
6400 		switch (c) {
6401 		case 'g':
6402 			cb.cb_name_flags |= VDEV_NAME_GUID;
6403 			break;
6404 		case 'H':
6405 			cb.cb_scripted = B_TRUE;
6406 			break;
6407 		case 'L':
6408 			cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
6409 			break;
6410 		case 'o':
6411 			props = optarg;
6412 			break;
6413 		case 'P':
6414 			cb.cb_name_flags |= VDEV_NAME_PATH;
6415 			break;
6416 		case 'p':
6417 			cb.cb_literal = B_TRUE;
6418 			break;
6419 		case 'T':
6420 			get_timestamp_arg(*optarg);
6421 			break;
6422 		case 'v':
6423 			cb.cb_verbose = B_TRUE;
6424 			cb.cb_namewidth = 8;	/* 8 until precalc is avail */
6425 			break;
6426 		case ':':
6427 			(void) fprintf(stderr, gettext("missing argument for "
6428 			    "'%c' option\n"), optopt);
6429 			usage(B_FALSE);
6430 			break;
6431 		case '?':
6432 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
6433 			    optopt);
6434 			usage(B_FALSE);
6435 		}
6436 	}
6437 
6438 	argc -= optind;
6439 	argv += optind;
6440 
6441 	get_interval_count(&argc, argv, &interval, &count);
6442 
6443 	if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
6444 		usage(B_FALSE);
6445 
6446 	for (;;) {
6447 		if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
6448 		    ZFS_TYPE_POOL, cb.cb_literal, &ret)) == NULL)
6449 			return (1);
6450 
6451 		if (pool_list_count(list) == 0)
6452 			break;
6453 
6454 		cb.cb_namewidth = 0;
6455 		(void) pool_list_iter(list, B_FALSE, get_namewidth_list, &cb);
6456 
6457 		if (timestamp_fmt != NODATE)
6458 			print_timestamp(timestamp_fmt);
6459 
6460 		if (!cb.cb_scripted && (first || cb.cb_verbose)) {
6461 			print_header(&cb);
6462 			first = B_FALSE;
6463 		}
6464 		ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
6465 
6466 		if (interval == 0)
6467 			break;
6468 
6469 		if (count != 0 && --count == 0)
6470 			break;
6471 
6472 		pool_list_free(list);
6473 		(void) fsleep(interval);
6474 	}
6475 
6476 	if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
6477 		(void) printf(gettext("no pools available\n"));
6478 		ret = 0;
6479 	}
6480 
6481 	pool_list_free(list);
6482 	zprop_free_list(cb.cb_proplist);
6483 	return (ret);
6484 }
6485 
6486 static int
6487 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
6488 {
6489 	boolean_t force = B_FALSE;
6490 	boolean_t rebuild = B_FALSE;
6491 	boolean_t wait = B_FALSE;
6492 	int c;
6493 	nvlist_t *nvroot;
6494 	char *poolname, *old_disk, *new_disk;
6495 	zpool_handle_t *zhp;
6496 	nvlist_t *props = NULL;
6497 	char *propval;
6498 	int ret;
6499 
6500 	/* check options */
6501 	while ((c = getopt(argc, argv, "fo:sw")) != -1) {
6502 		switch (c) {
6503 		case 'f':
6504 			force = B_TRUE;
6505 			break;
6506 		case 'o':
6507 			if ((propval = strchr(optarg, '=')) == NULL) {
6508 				(void) fprintf(stderr, gettext("missing "
6509 				    "'=' for -o option\n"));
6510 				usage(B_FALSE);
6511 			}
6512 			*propval = '\0';
6513 			propval++;
6514 
6515 			if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
6516 			    (add_prop_list(optarg, propval, &props, B_TRUE)))
6517 				usage(B_FALSE);
6518 			break;
6519 		case 's':
6520 			rebuild = B_TRUE;
6521 			break;
6522 		case 'w':
6523 			wait = B_TRUE;
6524 			break;
6525 		case '?':
6526 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
6527 			    optopt);
6528 			usage(B_FALSE);
6529 		}
6530 	}
6531 
6532 	argc -= optind;
6533 	argv += optind;
6534 
6535 	/* get pool name and check number of arguments */
6536 	if (argc < 1) {
6537 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
6538 		usage(B_FALSE);
6539 	}
6540 
6541 	poolname = argv[0];
6542 
6543 	if (argc < 2) {
6544 		(void) fprintf(stderr,
6545 		    gettext("missing <device> specification\n"));
6546 		usage(B_FALSE);
6547 	}
6548 
6549 	old_disk = argv[1];
6550 
6551 	if (argc < 3) {
6552 		if (!replacing) {
6553 			(void) fprintf(stderr,
6554 			    gettext("missing <new_device> specification\n"));
6555 			usage(B_FALSE);
6556 		}
6557 		new_disk = old_disk;
6558 		argc -= 1;
6559 		argv += 1;
6560 	} else {
6561 		new_disk = argv[2];
6562 		argc -= 2;
6563 		argv += 2;
6564 	}
6565 
6566 	if (argc > 1) {
6567 		(void) fprintf(stderr, gettext("too many arguments\n"));
6568 		usage(B_FALSE);
6569 	}
6570 
6571 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
6572 		nvlist_free(props);
6573 		return (1);
6574 	}
6575 
6576 	if (zpool_get_config(zhp, NULL) == NULL) {
6577 		(void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
6578 		    poolname);
6579 		zpool_close(zhp);
6580 		nvlist_free(props);
6581 		return (1);
6582 	}
6583 
6584 	/* unless manually specified use "ashift" pool property (if set) */
6585 	if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) {
6586 		int intval;
6587 		zprop_source_t src;
6588 		char strval[ZPOOL_MAXPROPLEN];
6589 
6590 		intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src);
6591 		if (src != ZPROP_SRC_DEFAULT) {
6592 			(void) sprintf(strval, "%" PRId32, intval);
6593 			verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval,
6594 			    &props, B_TRUE) == 0);
6595 		}
6596 	}
6597 
6598 	nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE,
6599 	    argc, argv);
6600 	if (nvroot == NULL) {
6601 		zpool_close(zhp);
6602 		nvlist_free(props);
6603 		return (1);
6604 	}
6605 
6606 	ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing,
6607 	    rebuild);
6608 
6609 	if (ret == 0 && wait)
6610 		ret = zpool_wait(zhp,
6611 		    replacing ? ZPOOL_WAIT_REPLACE : ZPOOL_WAIT_RESILVER);
6612 
6613 	nvlist_free(props);
6614 	nvlist_free(nvroot);
6615 	zpool_close(zhp);
6616 
6617 	return (ret);
6618 }
6619 
6620 /*
6621  * zpool replace [-fsw] [-o property=value] <pool> <device> <new_device>
6622  *
6623  *	-f	Force attach, even if <new_device> appears to be in use.
6624  *	-s	Use sequential instead of healing reconstruction for resilver.
6625  *	-o	Set property=value.
6626  *	-w	Wait for replacing to complete before returning
6627  *
6628  * Replace <device> with <new_device>.
6629  */
6630 int
6631 zpool_do_replace(int argc, char **argv)
6632 {
6633 	return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
6634 }
6635 
6636 /*
6637  * zpool attach [-fsw] [-o property=value] <pool> <device> <new_device>
6638  *
6639  *	-f	Force attach, even if <new_device> appears to be in use.
6640  *	-s	Use sequential instead of healing reconstruction for resilver.
6641  *	-o	Set property=value.
6642  *	-w	Wait for resilvering to complete before returning
6643  *
6644  * Attach <new_device> to the mirror containing <device>.  If <device> is not
6645  * part of a mirror, then <device> will be transformed into a mirror of
6646  * <device> and <new_device>.  In either case, <new_device> will begin life
6647  * with a DTL of [0, now], and will immediately begin to resilver itself.
6648  */
6649 int
6650 zpool_do_attach(int argc, char **argv)
6651 {
6652 	return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
6653 }
6654 
6655 /*
6656  * zpool detach [-f] <pool> <device>
6657  *
6658  *	-f	Force detach of <device>, even if DTLs argue against it
6659  *		(not supported yet)
6660  *
6661  * Detach a device from a mirror.  The operation will be refused if <device>
6662  * is the last device in the mirror, or if the DTLs indicate that this device
6663  * has the only valid copy of some data.
6664  */
6665 int
6666 zpool_do_detach(int argc, char **argv)
6667 {
6668 	int c;
6669 	char *poolname, *path;
6670 	zpool_handle_t *zhp;
6671 	int ret;
6672 
6673 	/* check options */
6674 	while ((c = getopt(argc, argv, "")) != -1) {
6675 		switch (c) {
6676 		case '?':
6677 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
6678 			    optopt);
6679 			usage(B_FALSE);
6680 		}
6681 	}
6682 
6683 	argc -= optind;
6684 	argv += optind;
6685 
6686 	/* get pool name and check number of arguments */
6687 	if (argc < 1) {
6688 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
6689 		usage(B_FALSE);
6690 	}
6691 
6692 	if (argc < 2) {
6693 		(void) fprintf(stderr,
6694 		    gettext("missing <device> specification\n"));
6695 		usage(B_FALSE);
6696 	}
6697 
6698 	poolname = argv[0];
6699 	path = argv[1];
6700 
6701 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
6702 		return (1);
6703 
6704 	ret = zpool_vdev_detach(zhp, path);
6705 
6706 	zpool_close(zhp);
6707 
6708 	return (ret);
6709 }
6710 
6711 /*
6712  * zpool split [-gLnP] [-o prop=val] ...
6713  *		[-o mntopt] ...
6714  *		[-R altroot] <pool> <newpool> [<device> ...]
6715  *
6716  *	-g      Display guid for individual vdev name.
6717  *	-L	Follow links when resolving vdev path name.
6718  *	-n	Do not split the pool, but display the resulting layout if
6719  *		it were to be split.
6720  *	-o	Set property=value, or set mount options.
6721  *	-P	Display full path for vdev name.
6722  *	-R	Mount the split-off pool under an alternate root.
6723  *	-l	Load encryption keys while importing.
6724  *
6725  * Splits the named pool and gives it the new pool name.  Devices to be split
6726  * off may be listed, provided that no more than one device is specified
6727  * per top-level vdev mirror.  The newly split pool is left in an exported
6728  * state unless -R is specified.
6729  *
6730  * Restrictions: the top-level of the pool pool must only be made up of
6731  * mirrors; all devices in the pool must be healthy; no device may be
6732  * undergoing a resilvering operation.
6733  */
6734 int
6735 zpool_do_split(int argc, char **argv)
6736 {
6737 	char *srcpool, *newpool, *propval;
6738 	char *mntopts = NULL;
6739 	splitflags_t flags;
6740 	int c, ret = 0;
6741 	boolean_t loadkeys = B_FALSE;
6742 	zpool_handle_t *zhp;
6743 	nvlist_t *config, *props = NULL;
6744 
6745 	flags.dryrun = B_FALSE;
6746 	flags.import = B_FALSE;
6747 	flags.name_flags = 0;
6748 
6749 	/* check options */
6750 	while ((c = getopt(argc, argv, ":gLR:lno:P")) != -1) {
6751 		switch (c) {
6752 		case 'g':
6753 			flags.name_flags |= VDEV_NAME_GUID;
6754 			break;
6755 		case 'L':
6756 			flags.name_flags |= VDEV_NAME_FOLLOW_LINKS;
6757 			break;
6758 		case 'R':
6759 			flags.import = B_TRUE;
6760 			if (add_prop_list(
6761 			    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
6762 			    &props, B_TRUE) != 0) {
6763 				nvlist_free(props);
6764 				usage(B_FALSE);
6765 			}
6766 			break;
6767 		case 'l':
6768 			loadkeys = B_TRUE;
6769 			break;
6770 		case 'n':
6771 			flags.dryrun = B_TRUE;
6772 			break;
6773 		case 'o':
6774 			if ((propval = strchr(optarg, '=')) != NULL) {
6775 				*propval = '\0';
6776 				propval++;
6777 				if (add_prop_list(optarg, propval,
6778 				    &props, B_TRUE) != 0) {
6779 					nvlist_free(props);
6780 					usage(B_FALSE);
6781 				}
6782 			} else {
6783 				mntopts = optarg;
6784 			}
6785 			break;
6786 		case 'P':
6787 			flags.name_flags |= VDEV_NAME_PATH;
6788 			break;
6789 		case ':':
6790 			(void) fprintf(stderr, gettext("missing argument for "
6791 			    "'%c' option\n"), optopt);
6792 			usage(B_FALSE);
6793 			break;
6794 		case '?':
6795 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
6796 			    optopt);
6797 			usage(B_FALSE);
6798 			break;
6799 		}
6800 	}
6801 
6802 	if (!flags.import && mntopts != NULL) {
6803 		(void) fprintf(stderr, gettext("setting mntopts is only "
6804 		    "valid when importing the pool\n"));
6805 		usage(B_FALSE);
6806 	}
6807 
6808 	if (!flags.import && loadkeys) {
6809 		(void) fprintf(stderr, gettext("loading keys is only "
6810 		    "valid when importing the pool\n"));
6811 		usage(B_FALSE);
6812 	}
6813 
6814 	argc -= optind;
6815 	argv += optind;
6816 
6817 	if (argc < 1) {
6818 		(void) fprintf(stderr, gettext("Missing pool name\n"));
6819 		usage(B_FALSE);
6820 	}
6821 	if (argc < 2) {
6822 		(void) fprintf(stderr, gettext("Missing new pool name\n"));
6823 		usage(B_FALSE);
6824 	}
6825 
6826 	srcpool = argv[0];
6827 	newpool = argv[1];
6828 
6829 	argc -= 2;
6830 	argv += 2;
6831 
6832 	if ((zhp = zpool_open(g_zfs, srcpool)) == NULL) {
6833 		nvlist_free(props);
6834 		return (1);
6835 	}
6836 
6837 	config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
6838 	if (config == NULL) {
6839 		ret = 1;
6840 	} else {
6841 		if (flags.dryrun) {
6842 			(void) printf(gettext("would create '%s' with the "
6843 			    "following layout:\n\n"), newpool);
6844 			print_vdev_tree(NULL, newpool, config, 0, "",
6845 			    flags.name_flags);
6846 			print_vdev_tree(NULL, "dedup", config, 0,
6847 			    VDEV_ALLOC_BIAS_DEDUP, 0);
6848 			print_vdev_tree(NULL, "special", config, 0,
6849 			    VDEV_ALLOC_BIAS_SPECIAL, 0);
6850 		}
6851 	}
6852 
6853 	zpool_close(zhp);
6854 
6855 	if (ret != 0 || flags.dryrun || !flags.import) {
6856 		nvlist_free(config);
6857 		nvlist_free(props);
6858 		return (ret);
6859 	}
6860 
6861 	/*
6862 	 * The split was successful. Now we need to open the new
6863 	 * pool and import it.
6864 	 */
6865 	if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL) {
6866 		nvlist_free(config);
6867 		nvlist_free(props);
6868 		return (1);
6869 	}
6870 
6871 	if (loadkeys) {
6872 		ret = zfs_crypto_attempt_load_keys(g_zfs, newpool);
6873 		if (ret != 0)
6874 			ret = 1;
6875 	}
6876 
6877 	if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
6878 	    zpool_enable_datasets(zhp, mntopts, 0) != 0) {
6879 		ret = 1;
6880 		(void) fprintf(stderr, gettext("Split was successful, but "
6881 		    "the datasets could not all be mounted\n"));
6882 		(void) fprintf(stderr, gettext("Try doing '%s' with a "
6883 		    "different altroot\n"), "zpool import");
6884 	}
6885 	zpool_close(zhp);
6886 	nvlist_free(config);
6887 	nvlist_free(props);
6888 
6889 	return (ret);
6890 }
6891 
6892 
6893 
6894 /*
6895  * zpool online <pool> <device> ...
6896  */
6897 int
6898 zpool_do_online(int argc, char **argv)
6899 {
6900 	int c, i;
6901 	char *poolname;
6902 	zpool_handle_t *zhp;
6903 	int ret = 0;
6904 	vdev_state_t newstate;
6905 	int flags = 0;
6906 
6907 	/* check options */
6908 	while ((c = getopt(argc, argv, "e")) != -1) {
6909 		switch (c) {
6910 		case 'e':
6911 			flags |= ZFS_ONLINE_EXPAND;
6912 			break;
6913 		case '?':
6914 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
6915 			    optopt);
6916 			usage(B_FALSE);
6917 		}
6918 	}
6919 
6920 	argc -= optind;
6921 	argv += optind;
6922 
6923 	/* get pool name and check number of arguments */
6924 	if (argc < 1) {
6925 		(void) fprintf(stderr, gettext("missing pool name\n"));
6926 		usage(B_FALSE);
6927 	}
6928 	if (argc < 2) {
6929 		(void) fprintf(stderr, gettext("missing device name\n"));
6930 		usage(B_FALSE);
6931 	}
6932 
6933 	poolname = argv[0];
6934 
6935 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
6936 		return (1);
6937 
6938 	for (i = 1; i < argc; i++) {
6939 		if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
6940 			if (newstate != VDEV_STATE_HEALTHY) {
6941 				(void) printf(gettext("warning: device '%s' "
6942 				    "onlined, but remains in faulted state\n"),
6943 				    argv[i]);
6944 				if (newstate == VDEV_STATE_FAULTED)
6945 					(void) printf(gettext("use 'zpool "
6946 					    "clear' to restore a faulted "
6947 					    "device\n"));
6948 				else
6949 					(void) printf(gettext("use 'zpool "
6950 					    "replace' to replace devices "
6951 					    "that are no longer present\n"));
6952 			}
6953 		} else {
6954 			ret = 1;
6955 		}
6956 	}
6957 
6958 	zpool_close(zhp);
6959 
6960 	return (ret);
6961 }
6962 
6963 /*
6964  * zpool offline [-ft] <pool> <device> ...
6965  *
6966  *	-f	Force the device into a faulted state.
6967  *
6968  *	-t	Only take the device off-line temporarily.  The offline/faulted
6969  *		state will not be persistent across reboots.
6970  */
6971 int
6972 zpool_do_offline(int argc, char **argv)
6973 {
6974 	int c, i;
6975 	char *poolname;
6976 	zpool_handle_t *zhp;
6977 	int ret = 0;
6978 	boolean_t istmp = B_FALSE;
6979 	boolean_t fault = B_FALSE;
6980 
6981 	/* check options */
6982 	while ((c = getopt(argc, argv, "ft")) != -1) {
6983 		switch (c) {
6984 		case 'f':
6985 			fault = B_TRUE;
6986 			break;
6987 		case 't':
6988 			istmp = B_TRUE;
6989 			break;
6990 		case '?':
6991 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
6992 			    optopt);
6993 			usage(B_FALSE);
6994 		}
6995 	}
6996 
6997 	argc -= optind;
6998 	argv += optind;
6999 
7000 	/* get pool name and check number of arguments */
7001 	if (argc < 1) {
7002 		(void) fprintf(stderr, gettext("missing pool name\n"));
7003 		usage(B_FALSE);
7004 	}
7005 	if (argc < 2) {
7006 		(void) fprintf(stderr, gettext("missing device name\n"));
7007 		usage(B_FALSE);
7008 	}
7009 
7010 	poolname = argv[0];
7011 
7012 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
7013 		return (1);
7014 
7015 	for (i = 1; i < argc; i++) {
7016 		if (fault) {
7017 			uint64_t guid = zpool_vdev_path_to_guid(zhp, argv[i]);
7018 			vdev_aux_t aux;
7019 			if (istmp == B_FALSE) {
7020 				/* Force the fault to persist across imports */
7021 				aux = VDEV_AUX_EXTERNAL_PERSIST;
7022 			} else {
7023 				aux = VDEV_AUX_EXTERNAL;
7024 			}
7025 
7026 			if (guid == 0 || zpool_vdev_fault(zhp, guid, aux) != 0)
7027 				ret = 1;
7028 		} else {
7029 			if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
7030 				ret = 1;
7031 		}
7032 	}
7033 
7034 	zpool_close(zhp);
7035 
7036 	return (ret);
7037 }
7038 
7039 /*
7040  * zpool clear <pool> [device]
7041  *
7042  * Clear all errors associated with a pool or a particular device.
7043  */
7044 int
7045 zpool_do_clear(int argc, char **argv)
7046 {
7047 	int c;
7048 	int ret = 0;
7049 	boolean_t dryrun = B_FALSE;
7050 	boolean_t do_rewind = B_FALSE;
7051 	boolean_t xtreme_rewind = B_FALSE;
7052 	uint32_t rewind_policy = ZPOOL_NO_REWIND;
7053 	nvlist_t *policy = NULL;
7054 	zpool_handle_t *zhp;
7055 	char *pool, *device;
7056 
7057 	/* check options */
7058 	while ((c = getopt(argc, argv, "FnX")) != -1) {
7059 		switch (c) {
7060 		case 'F':
7061 			do_rewind = B_TRUE;
7062 			break;
7063 		case 'n':
7064 			dryrun = B_TRUE;
7065 			break;
7066 		case 'X':
7067 			xtreme_rewind = B_TRUE;
7068 			break;
7069 		case '?':
7070 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
7071 			    optopt);
7072 			usage(B_FALSE);
7073 		}
7074 	}
7075 
7076 	argc -= optind;
7077 	argv += optind;
7078 
7079 	if (argc < 1) {
7080 		(void) fprintf(stderr, gettext("missing pool name\n"));
7081 		usage(B_FALSE);
7082 	}
7083 
7084 	if (argc > 2) {
7085 		(void) fprintf(stderr, gettext("too many arguments\n"));
7086 		usage(B_FALSE);
7087 	}
7088 
7089 	if ((dryrun || xtreme_rewind) && !do_rewind) {
7090 		(void) fprintf(stderr,
7091 		    gettext("-n or -X only meaningful with -F\n"));
7092 		usage(B_FALSE);
7093 	}
7094 	if (dryrun)
7095 		rewind_policy = ZPOOL_TRY_REWIND;
7096 	else if (do_rewind)
7097 		rewind_policy = ZPOOL_DO_REWIND;
7098 	if (xtreme_rewind)
7099 		rewind_policy |= ZPOOL_EXTREME_REWIND;
7100 
7101 	/* In future, further rewind policy choices can be passed along here */
7102 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
7103 	    nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY,
7104 	    rewind_policy) != 0) {
7105 		return (1);
7106 	}
7107 
7108 	pool = argv[0];
7109 	device = argc == 2 ? argv[1] : NULL;
7110 
7111 	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
7112 		nvlist_free(policy);
7113 		return (1);
7114 	}
7115 
7116 	if (zpool_clear(zhp, device, policy) != 0)
7117 		ret = 1;
7118 
7119 	zpool_close(zhp);
7120 
7121 	nvlist_free(policy);
7122 
7123 	return (ret);
7124 }
7125 
7126 /*
7127  * zpool reguid <pool>
7128  */
7129 int
7130 zpool_do_reguid(int argc, char **argv)
7131 {
7132 	int c;
7133 	char *poolname;
7134 	zpool_handle_t *zhp;
7135 	int ret = 0;
7136 
7137 	/* check options */
7138 	while ((c = getopt(argc, argv, "")) != -1) {
7139 		switch (c) {
7140 		case '?':
7141 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
7142 			    optopt);
7143 			usage(B_FALSE);
7144 		}
7145 	}
7146 
7147 	argc -= optind;
7148 	argv += optind;
7149 
7150 	/* get pool name and check number of arguments */
7151 	if (argc < 1) {
7152 		(void) fprintf(stderr, gettext("missing pool name\n"));
7153 		usage(B_FALSE);
7154 	}
7155 
7156 	if (argc > 1) {
7157 		(void) fprintf(stderr, gettext("too many arguments\n"));
7158 		usage(B_FALSE);
7159 	}
7160 
7161 	poolname = argv[0];
7162 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
7163 		return (1);
7164 
7165 	ret = zpool_reguid(zhp);
7166 
7167 	zpool_close(zhp);
7168 	return (ret);
7169 }
7170 
7171 
7172 /*
7173  * zpool reopen <pool>
7174  *
7175  * Reopen the pool so that the kernel can update the sizes of all vdevs.
7176  */
7177 int
7178 zpool_do_reopen(int argc, char **argv)
7179 {
7180 	int c;
7181 	int ret = 0;
7182 	boolean_t scrub_restart = B_TRUE;
7183 
7184 	/* check options */
7185 	while ((c = getopt(argc, argv, "n")) != -1) {
7186 		switch (c) {
7187 		case 'n':
7188 			scrub_restart = B_FALSE;
7189 			break;
7190 		case '?':
7191 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
7192 			    optopt);
7193 			usage(B_FALSE);
7194 		}
7195 	}
7196 
7197 	argc -= optind;
7198 	argv += optind;
7199 
7200 	/* if argc == 0 we will execute zpool_reopen_one on all pools */
7201 	ret = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
7202 	    B_FALSE, zpool_reopen_one, &scrub_restart);
7203 
7204 	return (ret);
7205 }
7206 
7207 typedef struct scrub_cbdata {
7208 	int	cb_type;
7209 	pool_scrub_cmd_t cb_scrub_cmd;
7210 } scrub_cbdata_t;
7211 
7212 static boolean_t
7213 zpool_has_checkpoint(zpool_handle_t *zhp)
7214 {
7215 	nvlist_t *config, *nvroot;
7216 
7217 	config = zpool_get_config(zhp, NULL);
7218 
7219 	if (config != NULL) {
7220 		pool_checkpoint_stat_t *pcs = NULL;
7221 		uint_t c;
7222 
7223 		nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
7224 		(void) nvlist_lookup_uint64_array(nvroot,
7225 		    ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
7226 
7227 		if (pcs == NULL || pcs->pcs_state == CS_NONE)
7228 			return (B_FALSE);
7229 
7230 		assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS ||
7231 		    pcs->pcs_state == CS_CHECKPOINT_DISCARDING);
7232 		return (B_TRUE);
7233 	}
7234 
7235 	return (B_FALSE);
7236 }
7237 
7238 static int
7239 scrub_callback(zpool_handle_t *zhp, void *data)
7240 {
7241 	scrub_cbdata_t *cb = data;
7242 	int err;
7243 
7244 	/*
7245 	 * Ignore faulted pools.
7246 	 */
7247 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
7248 		(void) fprintf(stderr, gettext("cannot scan '%s': pool is "
7249 		    "currently unavailable\n"), zpool_get_name(zhp));
7250 		return (1);
7251 	}
7252 
7253 	err = zpool_scan(zhp, cb->cb_type, cb->cb_scrub_cmd);
7254 
7255 	if (err == 0 && zpool_has_checkpoint(zhp) &&
7256 	    cb->cb_type == POOL_SCAN_SCRUB) {
7257 		(void) printf(gettext("warning: will not scrub state that "
7258 		    "belongs to the checkpoint of pool '%s'\n"),
7259 		    zpool_get_name(zhp));
7260 	}
7261 
7262 	return (err != 0);
7263 }
7264 
7265 static int
7266 wait_callback(zpool_handle_t *zhp, void *data)
7267 {
7268 	zpool_wait_activity_t *act = data;
7269 	return (zpool_wait(zhp, *act));
7270 }
7271 
7272 /*
7273  * zpool scrub [-s | -p] [-w] <pool> ...
7274  *
7275  *	-s	Stop.  Stops any in-progress scrub.
7276  *	-p	Pause. Pause in-progress scrub.
7277  *	-w	Wait.  Blocks until scrub has completed.
7278  */
7279 int
7280 zpool_do_scrub(int argc, char **argv)
7281 {
7282 	int c;
7283 	scrub_cbdata_t cb;
7284 	boolean_t wait = B_FALSE;
7285 	int error;
7286 
7287 	cb.cb_type = POOL_SCAN_SCRUB;
7288 	cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
7289 
7290 	/* check options */
7291 	while ((c = getopt(argc, argv, "spw")) != -1) {
7292 		switch (c) {
7293 		case 's':
7294 			cb.cb_type = POOL_SCAN_NONE;
7295 			break;
7296 		case 'p':
7297 			cb.cb_scrub_cmd = POOL_SCRUB_PAUSE;
7298 			break;
7299 		case 'w':
7300 			wait = B_TRUE;
7301 			break;
7302 		case '?':
7303 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
7304 			    optopt);
7305 			usage(B_FALSE);
7306 		}
7307 	}
7308 
7309 	if (cb.cb_type == POOL_SCAN_NONE &&
7310 	    cb.cb_scrub_cmd == POOL_SCRUB_PAUSE) {
7311 		(void) fprintf(stderr, gettext("invalid option combination: "
7312 		    "-s and -p are mutually exclusive\n"));
7313 		usage(B_FALSE);
7314 	}
7315 
7316 	if (wait && (cb.cb_type == POOL_SCAN_NONE ||
7317 	    cb.cb_scrub_cmd == POOL_SCRUB_PAUSE)) {
7318 		(void) fprintf(stderr, gettext("invalid option combination: "
7319 		    "-w cannot be used with -p or -s\n"));
7320 		usage(B_FALSE);
7321 	}
7322 
7323 	argc -= optind;
7324 	argv += optind;
7325 
7326 	if (argc < 1) {
7327 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
7328 		usage(B_FALSE);
7329 	}
7330 
7331 	error = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
7332 	    B_FALSE, scrub_callback, &cb);
7333 
7334 	if (wait && !error) {
7335 		zpool_wait_activity_t act = ZPOOL_WAIT_SCRUB;
7336 		error = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
7337 		    B_FALSE, wait_callback, &act);
7338 	}
7339 
7340 	return (error);
7341 }
7342 
7343 /*
7344  * zpool resilver <pool> ...
7345  *
7346  *	Restarts any in-progress resilver
7347  */
7348 int
7349 zpool_do_resilver(int argc, char **argv)
7350 {
7351 	int c;
7352 	scrub_cbdata_t cb;
7353 
7354 	cb.cb_type = POOL_SCAN_RESILVER;
7355 	cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
7356 
7357 	/* check options */
7358 	while ((c = getopt(argc, argv, "")) != -1) {
7359 		switch (c) {
7360 		case '?':
7361 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
7362 			    optopt);
7363 			usage(B_FALSE);
7364 		}
7365 	}
7366 
7367 	argc -= optind;
7368 	argv += optind;
7369 
7370 	if (argc < 1) {
7371 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
7372 		usage(B_FALSE);
7373 	}
7374 
7375 	return (for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
7376 	    B_FALSE, scrub_callback, &cb));
7377 }
7378 
7379 /*
7380  * zpool trim [-d] [-r <rate>] [-c | -s] <pool> [<device> ...]
7381  *
7382  *	-c		Cancel. Ends any in-progress trim.
7383  *	-d		Secure trim.  Requires kernel and device support.
7384  *	-r <rate>	Sets the TRIM rate in bytes (per second). Supports
7385  *			adding a multiplier suffix such as 'k' or 'm'.
7386  *	-s		Suspend. TRIM can then be restarted with no flags.
7387  *	-w		Wait. Blocks until trimming has completed.
7388  */
7389 int
7390 zpool_do_trim(int argc, char **argv)
7391 {
7392 	struct option long_options[] = {
7393 		{"cancel",	no_argument,		NULL,	'c'},
7394 		{"secure",	no_argument,		NULL,	'd'},
7395 		{"rate",	required_argument,	NULL,	'r'},
7396 		{"suspend",	no_argument,		NULL,	's'},
7397 		{"wait",	no_argument,		NULL,	'w'},
7398 		{0, 0, 0, 0}
7399 	};
7400 
7401 	pool_trim_func_t cmd_type = POOL_TRIM_START;
7402 	uint64_t rate = 0;
7403 	boolean_t secure = B_FALSE;
7404 	boolean_t wait = B_FALSE;
7405 
7406 	int c;
7407 	while ((c = getopt_long(argc, argv, "cdr:sw", long_options, NULL))
7408 	    != -1) {
7409 		switch (c) {
7410 		case 'c':
7411 			if (cmd_type != POOL_TRIM_START &&
7412 			    cmd_type != POOL_TRIM_CANCEL) {
7413 				(void) fprintf(stderr, gettext("-c cannot be "
7414 				    "combined with other options\n"));
7415 				usage(B_FALSE);
7416 			}
7417 			cmd_type = POOL_TRIM_CANCEL;
7418 			break;
7419 		case 'd':
7420 			if (cmd_type != POOL_TRIM_START) {
7421 				(void) fprintf(stderr, gettext("-d cannot be "
7422 				    "combined with the -c or -s options\n"));
7423 				usage(B_FALSE);
7424 			}
7425 			secure = B_TRUE;
7426 			break;
7427 		case 'r':
7428 			if (cmd_type != POOL_TRIM_START) {
7429 				(void) fprintf(stderr, gettext("-r cannot be "
7430 				    "combined with the -c or -s options\n"));
7431 				usage(B_FALSE);
7432 			}
7433 			if (zfs_nicestrtonum(g_zfs, optarg, &rate) == -1) {
7434 				(void) fprintf(stderr, "%s: %s\n",
7435 				    gettext("invalid value for rate"),
7436 				    libzfs_error_description(g_zfs));
7437 				usage(B_FALSE);
7438 			}
7439 			break;
7440 		case 's':
7441 			if (cmd_type != POOL_TRIM_START &&
7442 			    cmd_type != POOL_TRIM_SUSPEND) {
7443 				(void) fprintf(stderr, gettext("-s cannot be "
7444 				    "combined with other options\n"));
7445 				usage(B_FALSE);
7446 			}
7447 			cmd_type = POOL_TRIM_SUSPEND;
7448 			break;
7449 		case 'w':
7450 			wait = B_TRUE;
7451 			break;
7452 		case '?':
7453 			if (optopt != 0) {
7454 				(void) fprintf(stderr,
7455 				    gettext("invalid option '%c'\n"), optopt);
7456 			} else {
7457 				(void) fprintf(stderr,
7458 				    gettext("invalid option '%s'\n"),
7459 				    argv[optind - 1]);
7460 			}
7461 			usage(B_FALSE);
7462 		}
7463 	}
7464 
7465 	argc -= optind;
7466 	argv += optind;
7467 
7468 	if (argc < 1) {
7469 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
7470 		usage(B_FALSE);
7471 		return (-1);
7472 	}
7473 
7474 	if (wait && (cmd_type != POOL_TRIM_START)) {
7475 		(void) fprintf(stderr, gettext("-w cannot be used with -c or "
7476 		    "-s\n"));
7477 		usage(B_FALSE);
7478 	}
7479 
7480 	char *poolname = argv[0];
7481 	zpool_handle_t *zhp = zpool_open(g_zfs, poolname);
7482 	if (zhp == NULL)
7483 		return (-1);
7484 
7485 	trimflags_t trim_flags = {
7486 		.secure = secure,
7487 		.rate = rate,
7488 		.wait = wait,
7489 	};
7490 
7491 	nvlist_t *vdevs = fnvlist_alloc();
7492 	if (argc == 1) {
7493 		/* no individual leaf vdevs specified, so add them all */
7494 		nvlist_t *config = zpool_get_config(zhp, NULL);
7495 		nvlist_t *nvroot = fnvlist_lookup_nvlist(config,
7496 		    ZPOOL_CONFIG_VDEV_TREE);
7497 		zpool_collect_leaves(zhp, nvroot, vdevs);
7498 		trim_flags.fullpool = B_TRUE;
7499 	} else {
7500 		trim_flags.fullpool = B_FALSE;
7501 		for (int i = 1; i < argc; i++) {
7502 			fnvlist_add_boolean(vdevs, argv[i]);
7503 		}
7504 	}
7505 
7506 	int error = zpool_trim(zhp, cmd_type, vdevs, &trim_flags);
7507 
7508 	fnvlist_free(vdevs);
7509 	zpool_close(zhp);
7510 
7511 	return (error);
7512 }
7513 
7514 /*
7515  * Converts a total number of seconds to a human readable string broken
7516  * down in to days/hours/minutes/seconds.
7517  */
7518 static void
7519 secs_to_dhms(uint64_t total, char *buf)
7520 {
7521 	uint64_t days = total / 60 / 60 / 24;
7522 	uint64_t hours = (total / 60 / 60) % 24;
7523 	uint64_t mins = (total / 60) % 60;
7524 	uint64_t secs = (total % 60);
7525 
7526 	if (days > 0) {
7527 		(void) sprintf(buf, "%llu days %02llu:%02llu:%02llu",
7528 		    (u_longlong_t)days, (u_longlong_t)hours,
7529 		    (u_longlong_t)mins, (u_longlong_t)secs);
7530 	} else {
7531 		(void) sprintf(buf, "%02llu:%02llu:%02llu",
7532 		    (u_longlong_t)hours, (u_longlong_t)mins,
7533 		    (u_longlong_t)secs);
7534 	}
7535 }
7536 
7537 /*
7538  * Print out detailed scrub status.
7539  */
7540 static void
7541 print_scan_scrub_resilver_status(pool_scan_stat_t *ps)
7542 {
7543 	time_t start, end, pause;
7544 	uint64_t pass_scanned, scanned, pass_issued, issued, total;
7545 	uint64_t elapsed, scan_rate, issue_rate;
7546 	double fraction_done;
7547 	char processed_buf[7], scanned_buf[7], issued_buf[7], total_buf[7];
7548 	char srate_buf[7], irate_buf[7], time_buf[32];
7549 
7550 	printf("  ");
7551 	printf_color(ANSI_BOLD, gettext("scan:"));
7552 	printf(" ");
7553 
7554 	/* If there's never been a scan, there's not much to say. */
7555 	if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
7556 	    ps->pss_func >= POOL_SCAN_FUNCS) {
7557 		(void) printf(gettext("none requested\n"));
7558 		return;
7559 	}
7560 
7561 	start = ps->pss_start_time;
7562 	end = ps->pss_end_time;
7563 	pause = ps->pss_pass_scrub_pause;
7564 
7565 	zfs_nicebytes(ps->pss_processed, processed_buf, sizeof (processed_buf));
7566 
7567 	int is_resilver = ps->pss_func == POOL_SCAN_RESILVER;
7568 	int is_scrub = ps->pss_func == POOL_SCAN_SCRUB;
7569 	assert(is_resilver || is_scrub);
7570 
7571 	/* Scan is finished or canceled. */
7572 	if (ps->pss_state == DSS_FINISHED) {
7573 		secs_to_dhms(end - start, time_buf);
7574 
7575 		if (is_scrub) {
7576 			(void) printf(gettext("scrub repaired %s "
7577 			    "in %s with %llu errors on %s"), processed_buf,
7578 			    time_buf, (u_longlong_t)ps->pss_errors,
7579 			    ctime(&end));
7580 		} else if (is_resilver) {
7581 			(void) printf(gettext("resilvered %s "
7582 			    "in %s with %llu errors on %s"), processed_buf,
7583 			    time_buf, (u_longlong_t)ps->pss_errors,
7584 			    ctime(&end));
7585 		}
7586 		return;
7587 	} else if (ps->pss_state == DSS_CANCELED) {
7588 		if (is_scrub) {
7589 			(void) printf(gettext("scrub canceled on %s"),
7590 			    ctime(&end));
7591 		} else if (is_resilver) {
7592 			(void) printf(gettext("resilver canceled on %s"),
7593 			    ctime(&end));
7594 		}
7595 		return;
7596 	}
7597 
7598 	assert(ps->pss_state == DSS_SCANNING);
7599 
7600 	/* Scan is in progress. Resilvers can't be paused. */
7601 	if (is_scrub) {
7602 		if (pause == 0) {
7603 			(void) printf(gettext("scrub in progress since %s"),
7604 			    ctime(&start));
7605 		} else {
7606 			(void) printf(gettext("scrub paused since %s"),
7607 			    ctime(&pause));
7608 			(void) printf(gettext("\tscrub started on %s"),
7609 			    ctime(&start));
7610 		}
7611 	} else if (is_resilver) {
7612 		(void) printf(gettext("resilver in progress since %s"),
7613 		    ctime(&start));
7614 	}
7615 
7616 	scanned = ps->pss_examined;
7617 	pass_scanned = ps->pss_pass_exam;
7618 	issued = ps->pss_issued;
7619 	pass_issued = ps->pss_pass_issued;
7620 	total = ps->pss_to_examine;
7621 
7622 	/* we are only done with a block once we have issued the IO for it */
7623 	fraction_done = (double)issued / total;
7624 
7625 	/* elapsed time for this pass, rounding up to 1 if it's 0 */
7626 	elapsed = time(NULL) - ps->pss_pass_start;
7627 	elapsed -= ps->pss_pass_scrub_spent_paused;
7628 	elapsed = (elapsed != 0) ? elapsed : 1;
7629 
7630 	scan_rate = pass_scanned / elapsed;
7631 	issue_rate = pass_issued / elapsed;
7632 	uint64_t total_secs_left = (issue_rate != 0 && total >= issued) ?
7633 	    ((total - issued) / issue_rate) : UINT64_MAX;
7634 	secs_to_dhms(total_secs_left, time_buf);
7635 
7636 	/* format all of the numbers we will be reporting */
7637 	zfs_nicebytes(scanned, scanned_buf, sizeof (scanned_buf));
7638 	zfs_nicebytes(issued, issued_buf, sizeof (issued_buf));
7639 	zfs_nicebytes(total, total_buf, sizeof (total_buf));
7640 	zfs_nicebytes(scan_rate, srate_buf, sizeof (srate_buf));
7641 	zfs_nicebytes(issue_rate, irate_buf, sizeof (irate_buf));
7642 
7643 	/* do not print estimated time if we have a paused scrub */
7644 	if (pause == 0) {
7645 		(void) printf(gettext("\t%s scanned at %s/s, "
7646 		    "%s issued at %s/s, %s total\n"),
7647 		    scanned_buf, srate_buf, issued_buf, irate_buf, total_buf);
7648 	} else {
7649 		(void) printf(gettext("\t%s scanned, %s issued, %s total\n"),
7650 		    scanned_buf, issued_buf, total_buf);
7651 	}
7652 
7653 	if (is_resilver) {
7654 		(void) printf(gettext("\t%s resilvered, %.2f%% done"),
7655 		    processed_buf, 100 * fraction_done);
7656 	} else if (is_scrub) {
7657 		(void) printf(gettext("\t%s repaired, %.2f%% done"),
7658 		    processed_buf, 100 * fraction_done);
7659 	}
7660 
7661 	if (pause == 0) {
7662 		/*
7663 		 * Only provide an estimate iff:
7664 		 * 1) the time remaining is valid, and
7665 		 * 2) the issue rate exceeds 10 MB/s, and
7666 		 * 3) it's either:
7667 		 *    a) a resilver which has started repairs, or
7668 		 *    b) a scrub which has entered the issue phase.
7669 		 */
7670 		if (total_secs_left != UINT64_MAX &&
7671 		    issue_rate >= 10 * 1024 * 1024 &&
7672 		    ((is_resilver && ps->pss_processed > 0) ||
7673 		    (is_scrub && issued > 0))) {
7674 			(void) printf(gettext(", %s to go\n"), time_buf);
7675 		} else {
7676 			(void) printf(gettext(", no estimated "
7677 			    "completion time\n"));
7678 		}
7679 	} else {
7680 		(void) printf(gettext("\n"));
7681 	}
7682 }
7683 
7684 static void
7685 print_rebuild_status_impl(vdev_rebuild_stat_t *vrs, char *vdev_name)
7686 {
7687 	if (vrs == NULL || vrs->vrs_state == VDEV_REBUILD_NONE)
7688 		return;
7689 
7690 	printf("  ");
7691 	printf_color(ANSI_BOLD, gettext("scan:"));
7692 	printf(" ");
7693 
7694 	uint64_t bytes_scanned = vrs->vrs_bytes_scanned;
7695 	uint64_t bytes_issued = vrs->vrs_bytes_issued;
7696 	uint64_t bytes_rebuilt = vrs->vrs_bytes_rebuilt;
7697 	uint64_t bytes_est = vrs->vrs_bytes_est;
7698 	uint64_t scan_rate = (vrs->vrs_pass_bytes_scanned /
7699 	    (vrs->vrs_pass_time_ms + 1)) * 1000;
7700 	uint64_t issue_rate = (vrs->vrs_pass_bytes_issued /
7701 	    (vrs->vrs_pass_time_ms + 1)) * 1000;
7702 	double scan_pct = MIN((double)bytes_scanned * 100 /
7703 	    (bytes_est + 1), 100);
7704 
7705 	/* Format all of the numbers we will be reporting */
7706 	char bytes_scanned_buf[7], bytes_issued_buf[7];
7707 	char bytes_rebuilt_buf[7], bytes_est_buf[7];
7708 	char scan_rate_buf[7], issue_rate_buf[7], time_buf[32];
7709 	zfs_nicebytes(bytes_scanned, bytes_scanned_buf,
7710 	    sizeof (bytes_scanned_buf));
7711 	zfs_nicebytes(bytes_issued, bytes_issued_buf,
7712 	    sizeof (bytes_issued_buf));
7713 	zfs_nicebytes(bytes_rebuilt, bytes_rebuilt_buf,
7714 	    sizeof (bytes_rebuilt_buf));
7715 	zfs_nicebytes(bytes_est, bytes_est_buf, sizeof (bytes_est_buf));
7716 	zfs_nicebytes(scan_rate, scan_rate_buf, sizeof (scan_rate_buf));
7717 	zfs_nicebytes(issue_rate, issue_rate_buf, sizeof (issue_rate_buf));
7718 
7719 	time_t start = vrs->vrs_start_time;
7720 	time_t end = vrs->vrs_end_time;
7721 
7722 	/* Rebuild is finished or canceled. */
7723 	if (vrs->vrs_state == VDEV_REBUILD_COMPLETE) {
7724 		secs_to_dhms(vrs->vrs_scan_time_ms / 1000, time_buf);
7725 		(void) printf(gettext("resilvered (%s) %s in %s "
7726 		    "with %llu errors on %s"), vdev_name, bytes_rebuilt_buf,
7727 		    time_buf, (u_longlong_t)vrs->vrs_errors, ctime(&end));
7728 		return;
7729 	} else if (vrs->vrs_state == VDEV_REBUILD_CANCELED) {
7730 		(void) printf(gettext("resilver (%s) canceled on %s"),
7731 		    vdev_name, ctime(&end));
7732 		return;
7733 	} else if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) {
7734 		(void) printf(gettext("resilver (%s) in progress since %s"),
7735 		    vdev_name, ctime(&start));
7736 	}
7737 
7738 	assert(vrs->vrs_state == VDEV_REBUILD_ACTIVE);
7739 
7740 	secs_to_dhms(MAX((int64_t)bytes_est - (int64_t)bytes_scanned, 0) /
7741 	    MAX(scan_rate, 1), time_buf);
7742 
7743 	(void) printf(gettext("\t%s scanned at %s/s, %s issued %s/s, "
7744 	    "%s total\n"), bytes_scanned_buf, scan_rate_buf,
7745 	    bytes_issued_buf, issue_rate_buf, bytes_est_buf);
7746 	(void) printf(gettext("\t%s resilvered, %.2f%% done"),
7747 	    bytes_rebuilt_buf, scan_pct);
7748 
7749 	if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) {
7750 		if (scan_rate >= 10 * 1024 * 1024) {
7751 			(void) printf(gettext(", %s to go\n"), time_buf);
7752 		} else {
7753 			(void) printf(gettext(", no estimated "
7754 			    "completion time\n"));
7755 		}
7756 	} else {
7757 		(void) printf(gettext("\n"));
7758 	}
7759 }
7760 
7761 /*
7762  * Print rebuild status for top-level vdevs.
7763  */
7764 static void
7765 print_rebuild_status(zpool_handle_t *zhp, nvlist_t *nvroot)
7766 {
7767 	nvlist_t **child;
7768 	uint_t children;
7769 
7770 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
7771 	    &child, &children) != 0)
7772 		children = 0;
7773 
7774 	for (uint_t c = 0; c < children; c++) {
7775 		vdev_rebuild_stat_t *vrs;
7776 		uint_t i;
7777 
7778 		if (nvlist_lookup_uint64_array(child[c],
7779 		    ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i) == 0) {
7780 			char *name = zpool_vdev_name(g_zfs, zhp,
7781 			    child[c], VDEV_NAME_TYPE_ID);
7782 			print_rebuild_status_impl(vrs, name);
7783 			free(name);
7784 		}
7785 	}
7786 }
7787 
7788 /*
7789  * As we don't scrub checkpointed blocks, we want to warn the user that we
7790  * skipped scanning some blocks if a checkpoint exists or existed at any
7791  * time during the scan.  If a sequential instead of healing reconstruction
7792  * was performed then the blocks were reconstructed.  However, their checksums
7793  * have not been verified so we still print the warning.
7794  */
7795 static void
7796 print_checkpoint_scan_warning(pool_scan_stat_t *ps, pool_checkpoint_stat_t *pcs)
7797 {
7798 	if (ps == NULL || pcs == NULL)
7799 		return;
7800 
7801 	if (pcs->pcs_state == CS_NONE ||
7802 	    pcs->pcs_state == CS_CHECKPOINT_DISCARDING)
7803 		return;
7804 
7805 	assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS);
7806 
7807 	if (ps->pss_state == DSS_NONE)
7808 		return;
7809 
7810 	if ((ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) &&
7811 	    ps->pss_end_time < pcs->pcs_start_time)
7812 		return;
7813 
7814 	if (ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) {
7815 		(void) printf(gettext("    scan warning: skipped blocks "
7816 		    "that are only referenced by the checkpoint.\n"));
7817 	} else {
7818 		assert(ps->pss_state == DSS_SCANNING);
7819 		(void) printf(gettext("    scan warning: skipping blocks "
7820 		    "that are only referenced by the checkpoint.\n"));
7821 	}
7822 }
7823 
7824 /*
7825  * Returns B_TRUE if there is an active rebuild in progress.  Otherwise,
7826  * B_FALSE is returned and 'rebuild_end_time' is set to the end time for
7827  * the last completed (or cancelled) rebuild.
7828  */
7829 static boolean_t
7830 check_rebuilding(nvlist_t *nvroot, uint64_t *rebuild_end_time)
7831 {
7832 	nvlist_t **child;
7833 	uint_t children;
7834 	boolean_t rebuilding = B_FALSE;
7835 	uint64_t end_time = 0;
7836 
7837 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
7838 	    &child, &children) != 0)
7839 		children = 0;
7840 
7841 	for (uint_t c = 0; c < children; c++) {
7842 		vdev_rebuild_stat_t *vrs;
7843 		uint_t i;
7844 
7845 		if (nvlist_lookup_uint64_array(child[c],
7846 		    ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i) == 0) {
7847 
7848 			if (vrs->vrs_end_time > end_time)
7849 				end_time = vrs->vrs_end_time;
7850 
7851 			if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) {
7852 				rebuilding = B_TRUE;
7853 				end_time = 0;
7854 				break;
7855 			}
7856 		}
7857 	}
7858 
7859 	if (rebuild_end_time != NULL)
7860 		*rebuild_end_time = end_time;
7861 
7862 	return (rebuilding);
7863 }
7864 
7865 /*
7866  * Print the scan status.
7867  */
7868 static void
7869 print_scan_status(zpool_handle_t *zhp, nvlist_t *nvroot)
7870 {
7871 	uint64_t rebuild_end_time = 0, resilver_end_time = 0;
7872 	boolean_t have_resilver = B_FALSE, have_scrub = B_FALSE;
7873 	boolean_t active_resilver = B_FALSE;
7874 	pool_checkpoint_stat_t *pcs = NULL;
7875 	pool_scan_stat_t *ps = NULL;
7876 	uint_t c;
7877 
7878 	if (nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_SCAN_STATS,
7879 	    (uint64_t **)&ps, &c) == 0) {
7880 		if (ps->pss_func == POOL_SCAN_RESILVER) {
7881 			resilver_end_time = ps->pss_end_time;
7882 			active_resilver = (ps->pss_state == DSS_SCANNING);
7883 		}
7884 
7885 		have_resilver = (ps->pss_func == POOL_SCAN_RESILVER);
7886 		have_scrub = (ps->pss_func == POOL_SCAN_SCRUB);
7887 	}
7888 
7889 	boolean_t active_rebuild = check_rebuilding(nvroot, &rebuild_end_time);
7890 	boolean_t have_rebuild = (active_rebuild || (rebuild_end_time > 0));
7891 
7892 	/* Always print the scrub status when available. */
7893 	if (have_scrub)
7894 		print_scan_scrub_resilver_status(ps);
7895 
7896 	/*
7897 	 * When there is an active resilver or rebuild print its status.
7898 	 * Otherwise print the status of the last resilver or rebuild.
7899 	 */
7900 	if (active_resilver || (!active_rebuild && have_resilver &&
7901 	    resilver_end_time && resilver_end_time > rebuild_end_time)) {
7902 		print_scan_scrub_resilver_status(ps);
7903 	} else if (active_rebuild || (!active_resilver && have_rebuild &&
7904 	    rebuild_end_time && rebuild_end_time > resilver_end_time)) {
7905 		print_rebuild_status(zhp, nvroot);
7906 	}
7907 
7908 	(void) nvlist_lookup_uint64_array(nvroot,
7909 	    ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
7910 	print_checkpoint_scan_warning(ps, pcs);
7911 }
7912 
7913 /*
7914  * Print out detailed removal status.
7915  */
7916 static void
7917 print_removal_status(zpool_handle_t *zhp, pool_removal_stat_t *prs)
7918 {
7919 	char copied_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
7920 	time_t start, end;
7921 	nvlist_t *config, *nvroot;
7922 	nvlist_t **child;
7923 	uint_t children;
7924 	char *vdev_name;
7925 
7926 	if (prs == NULL || prs->prs_state == DSS_NONE)
7927 		return;
7928 
7929 	/*
7930 	 * Determine name of vdev.
7931 	 */
7932 	config = zpool_get_config(zhp, NULL);
7933 	nvroot = fnvlist_lookup_nvlist(config,
7934 	    ZPOOL_CONFIG_VDEV_TREE);
7935 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
7936 	    &child, &children) == 0);
7937 	assert(prs->prs_removing_vdev < children);
7938 	vdev_name = zpool_vdev_name(g_zfs, zhp,
7939 	    child[prs->prs_removing_vdev], B_TRUE);
7940 
7941 	printf_color(ANSI_BOLD, gettext("remove: "));
7942 
7943 	start = prs->prs_start_time;
7944 	end = prs->prs_end_time;
7945 	zfs_nicenum(prs->prs_copied, copied_buf, sizeof (copied_buf));
7946 
7947 	/*
7948 	 * Removal is finished or canceled.
7949 	 */
7950 	if (prs->prs_state == DSS_FINISHED) {
7951 		uint64_t minutes_taken = (end - start) / 60;
7952 
7953 		(void) printf(gettext("Removal of vdev %llu copied %s "
7954 		    "in %lluh%um, completed on %s"),
7955 		    (longlong_t)prs->prs_removing_vdev,
7956 		    copied_buf,
7957 		    (u_longlong_t)(minutes_taken / 60),
7958 		    (uint_t)(minutes_taken % 60),
7959 		    ctime((time_t *)&end));
7960 	} else if (prs->prs_state == DSS_CANCELED) {
7961 		(void) printf(gettext("Removal of %s canceled on %s"),
7962 		    vdev_name, ctime(&end));
7963 	} else {
7964 		uint64_t copied, total, elapsed, mins_left, hours_left;
7965 		double fraction_done;
7966 		uint_t rate;
7967 
7968 		assert(prs->prs_state == DSS_SCANNING);
7969 
7970 		/*
7971 		 * Removal is in progress.
7972 		 */
7973 		(void) printf(gettext(
7974 		    "Evacuation of %s in progress since %s"),
7975 		    vdev_name, ctime(&start));
7976 
7977 		copied = prs->prs_copied > 0 ? prs->prs_copied : 1;
7978 		total = prs->prs_to_copy;
7979 		fraction_done = (double)copied / total;
7980 
7981 		/* elapsed time for this pass */
7982 		elapsed = time(NULL) - prs->prs_start_time;
7983 		elapsed = elapsed > 0 ? elapsed : 1;
7984 		rate = copied / elapsed;
7985 		rate = rate > 0 ? rate : 1;
7986 		mins_left = ((total - copied) / rate) / 60;
7987 		hours_left = mins_left / 60;
7988 
7989 		zfs_nicenum(copied, examined_buf, sizeof (examined_buf));
7990 		zfs_nicenum(total, total_buf, sizeof (total_buf));
7991 		zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
7992 
7993 		/*
7994 		 * do not print estimated time if hours_left is more than
7995 		 * 30 days
7996 		 */
7997 		(void) printf(gettext(
7998 		    "\t%s copied out of %s at %s/s, %.2f%% done"),
7999 		    examined_buf, total_buf, rate_buf, 100 * fraction_done);
8000 		if (hours_left < (30 * 24)) {
8001 			(void) printf(gettext(", %lluh%um to go\n"),
8002 			    (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
8003 		} else {
8004 			(void) printf(gettext(
8005 			    ", (copy is slow, no estimated time)\n"));
8006 		}
8007 	}
8008 	free(vdev_name);
8009 
8010 	if (prs->prs_mapping_memory > 0) {
8011 		char mem_buf[7];
8012 		zfs_nicenum(prs->prs_mapping_memory, mem_buf, sizeof (mem_buf));
8013 		(void) printf(gettext(
8014 		    "\t%s memory used for removed device mappings\n"),
8015 		    mem_buf);
8016 	}
8017 }
8018 
8019 static void
8020 print_checkpoint_status(pool_checkpoint_stat_t *pcs)
8021 {
8022 	time_t start;
8023 	char space_buf[7];
8024 
8025 	if (pcs == NULL || pcs->pcs_state == CS_NONE)
8026 		return;
8027 
8028 	(void) printf(gettext("checkpoint: "));
8029 
8030 	start = pcs->pcs_start_time;
8031 	zfs_nicenum(pcs->pcs_space, space_buf, sizeof (space_buf));
8032 
8033 	if (pcs->pcs_state == CS_CHECKPOINT_EXISTS) {
8034 		char *date = ctime(&start);
8035 
8036 		/*
8037 		 * ctime() adds a newline at the end of the generated
8038 		 * string, thus the weird format specifier and the
8039 		 * strlen() call used to chop it off from the output.
8040 		 */
8041 		(void) printf(gettext("created %.*s, consumes %s\n"),
8042 		    (int)(strlen(date) - 1), date, space_buf);
8043 		return;
8044 	}
8045 
8046 	assert(pcs->pcs_state == CS_CHECKPOINT_DISCARDING);
8047 
8048 	(void) printf(gettext("discarding, %s remaining.\n"),
8049 	    space_buf);
8050 }
8051 
8052 static void
8053 print_error_log(zpool_handle_t *zhp)
8054 {
8055 	nvlist_t *nverrlist = NULL;
8056 	nvpair_t *elem;
8057 	char *pathname;
8058 	size_t len = MAXPATHLEN * 2;
8059 
8060 	if (zpool_get_errlog(zhp, &nverrlist) != 0)
8061 		return;
8062 
8063 	(void) printf("errors: Permanent errors have been "
8064 	    "detected in the following files:\n\n");
8065 
8066 	pathname = safe_malloc(len);
8067 	elem = NULL;
8068 	while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
8069 		nvlist_t *nv;
8070 		uint64_t dsobj, obj;
8071 
8072 		verify(nvpair_value_nvlist(elem, &nv) == 0);
8073 		verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
8074 		    &dsobj) == 0);
8075 		verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
8076 		    &obj) == 0);
8077 		zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
8078 		(void) printf("%7s %s\n", "", pathname);
8079 	}
8080 	free(pathname);
8081 	nvlist_free(nverrlist);
8082 }
8083 
8084 static void
8085 print_spares(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **spares,
8086     uint_t nspares)
8087 {
8088 	uint_t i;
8089 	char *name;
8090 
8091 	if (nspares == 0)
8092 		return;
8093 
8094 	(void) printf(gettext("\tspares\n"));
8095 
8096 	for (i = 0; i < nspares; i++) {
8097 		name = zpool_vdev_name(g_zfs, zhp, spares[i],
8098 		    cb->cb_name_flags);
8099 		print_status_config(zhp, cb, name, spares[i], 2, B_TRUE, NULL);
8100 		free(name);
8101 	}
8102 }
8103 
8104 static void
8105 print_l2cache(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **l2cache,
8106     uint_t nl2cache)
8107 {
8108 	uint_t i;
8109 	char *name;
8110 
8111 	if (nl2cache == 0)
8112 		return;
8113 
8114 	(void) printf(gettext("\tcache\n"));
8115 
8116 	for (i = 0; i < nl2cache; i++) {
8117 		name = zpool_vdev_name(g_zfs, zhp, l2cache[i],
8118 		    cb->cb_name_flags);
8119 		print_status_config(zhp, cb, name, l2cache[i], 2,
8120 		    B_FALSE, NULL);
8121 		free(name);
8122 	}
8123 }
8124 
8125 static void
8126 print_dedup_stats(nvlist_t *config)
8127 {
8128 	ddt_histogram_t *ddh;
8129 	ddt_stat_t *dds;
8130 	ddt_object_t *ddo;
8131 	uint_t c;
8132 	char dspace[6], mspace[6];
8133 
8134 	/*
8135 	 * If the pool was faulted then we may not have been able to
8136 	 * obtain the config. Otherwise, if we have anything in the dedup
8137 	 * table continue processing the stats.
8138 	 */
8139 	if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
8140 	    (uint64_t **)&ddo, &c) != 0)
8141 		return;
8142 
8143 	(void) printf("\n");
8144 	(void) printf(gettext(" dedup: "));
8145 	if (ddo->ddo_count == 0) {
8146 		(void) printf(gettext("no DDT entries\n"));
8147 		return;
8148 	}
8149 
8150 	zfs_nicebytes(ddo->ddo_dspace, dspace, sizeof (dspace));
8151 	zfs_nicebytes(ddo->ddo_mspace, mspace, sizeof (mspace));
8152 	(void) printf("DDT entries %llu, size %s on disk, %s in core\n",
8153 	    (u_longlong_t)ddo->ddo_count,
8154 	    dspace,
8155 	    mspace);
8156 
8157 	verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
8158 	    (uint64_t **)&dds, &c) == 0);
8159 	verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
8160 	    (uint64_t **)&ddh, &c) == 0);
8161 	zpool_dump_ddt(dds, ddh);
8162 }
8163 
8164 /*
8165  * Display a summary of pool status.  Displays a summary such as:
8166  *
8167  *        pool: tank
8168  *	status: DEGRADED
8169  *	reason: One or more devices ...
8170  *         see: https://openzfs.github.io/openzfs-docs/msg/ZFS-xxxx-01
8171  *	config:
8172  *		mirror		DEGRADED
8173  *                c1t0d0	OK
8174  *                c2t0d0	UNAVAIL
8175  *
8176  * When given the '-v' option, we print out the complete config.  If the '-e'
8177  * option is specified, then we print out error rate information as well.
8178  */
8179 static int
8180 status_callback(zpool_handle_t *zhp, void *data)
8181 {
8182 	status_cbdata_t *cbp = data;
8183 	nvlist_t *config, *nvroot;
8184 	const char *msgid;
8185 	zpool_status_t reason;
8186 	zpool_errata_t errata;
8187 	const char *health;
8188 	uint_t c;
8189 	vdev_stat_t *vs;
8190 
8191 	config = zpool_get_config(zhp, NULL);
8192 	reason = zpool_get_status(zhp, &msgid, &errata);
8193 
8194 	cbp->cb_count++;
8195 
8196 	/*
8197 	 * If we were given 'zpool status -x', only report those pools with
8198 	 * problems.
8199 	 */
8200 	if (cbp->cb_explain &&
8201 	    (reason == ZPOOL_STATUS_OK ||
8202 	    reason == ZPOOL_STATUS_VERSION_OLDER ||
8203 	    reason == ZPOOL_STATUS_FEAT_DISABLED ||
8204 	    reason == ZPOOL_STATUS_COMPATIBILITY_ERR ||
8205 	    reason == ZPOOL_STATUS_INCOMPATIBLE_FEAT)) {
8206 		if (!cbp->cb_allpools) {
8207 			(void) printf(gettext("pool '%s' is healthy\n"),
8208 			    zpool_get_name(zhp));
8209 			if (cbp->cb_first)
8210 				cbp->cb_first = B_FALSE;
8211 		}
8212 		return (0);
8213 	}
8214 
8215 	if (cbp->cb_first)
8216 		cbp->cb_first = B_FALSE;
8217 	else
8218 		(void) printf("\n");
8219 
8220 	nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
8221 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
8222 	    (uint64_t **)&vs, &c) == 0);
8223 
8224 	health = zpool_get_state_str(zhp);
8225 
8226 	printf("  ");
8227 	printf_color(ANSI_BOLD, gettext("pool:"));
8228 	printf(" %s\n", zpool_get_name(zhp));
8229 	fputc(' ', stdout);
8230 	printf_color(ANSI_BOLD, gettext("state: "));
8231 
8232 	printf_color(health_str_to_color(health), "%s", health);
8233 
8234 	fputc('\n', stdout);
8235 
8236 	switch (reason) {
8237 	case ZPOOL_STATUS_MISSING_DEV_R:
8238 		printf_color(ANSI_BOLD, gettext("status: "));
8239 		printf_color(ANSI_YELLOW, gettext("One or more devices could "
8240 		    "not be opened.  Sufficient replicas exist for\n\tthe pool "
8241 		    "to continue functioning in a degraded state.\n"));
8242 		printf_color(ANSI_BOLD, gettext("action: "));
8243 		printf_color(ANSI_YELLOW, gettext("Attach the missing device "
8244 		    "and online it using 'zpool online'.\n"));
8245 		break;
8246 
8247 	case ZPOOL_STATUS_MISSING_DEV_NR:
8248 		printf_color(ANSI_BOLD, gettext("status: "));
8249 		printf_color(ANSI_YELLOW, gettext("One or more devices could "
8250 		    "not be opened.  There are insufficient\n\treplicas for the"
8251 		    " pool to continue functioning.\n"));
8252 		printf_color(ANSI_BOLD, gettext("action: "));
8253 		printf_color(ANSI_YELLOW, gettext("Attach the missing device "
8254 		    "and online it using 'zpool online'.\n"));
8255 		break;
8256 
8257 	case ZPOOL_STATUS_CORRUPT_LABEL_R:
8258 		printf_color(ANSI_BOLD, gettext("status: "));
8259 		printf_color(ANSI_YELLOW, gettext("One or more devices could "
8260 		    "not be used because the label is missing or\n\tinvalid.  "
8261 		    "Sufficient replicas exist for the pool to continue\n\t"
8262 		    "functioning in a degraded state.\n"));
8263 		printf_color(ANSI_BOLD, gettext("action: "));
8264 		printf_color(ANSI_YELLOW, gettext("Replace the device using "
8265 		    "'zpool replace'.\n"));
8266 		break;
8267 
8268 	case ZPOOL_STATUS_CORRUPT_LABEL_NR:
8269 		printf_color(ANSI_BOLD, gettext("status: "));
8270 		printf_color(ANSI_YELLOW, gettext("One or more devices could "
8271 		    "not be used because the label is missing \n\tor invalid.  "
8272 		    "There are insufficient replicas for the pool to "
8273 		    "continue\n\tfunctioning.\n"));
8274 		zpool_explain_recover(zpool_get_handle(zhp),
8275 		    zpool_get_name(zhp), reason, config);
8276 		break;
8277 
8278 	case ZPOOL_STATUS_FAILING_DEV:
8279 		printf_color(ANSI_BOLD, gettext("status: "));
8280 		printf_color(ANSI_YELLOW, gettext("One or more devices has "
8281 		    "experienced an unrecoverable error.  An\n\tattempt was "
8282 		    "made to correct the error.  Applications are "
8283 		    "unaffected.\n"));
8284 		printf_color(ANSI_BOLD, gettext("action: "));
8285 			printf_color(ANSI_YELLOW, gettext("Determine if the "
8286 		    "device needs to be replaced, and clear the errors\n\tusing"
8287 		    " 'zpool clear' or replace the device with 'zpool "
8288 		    "replace'.\n"));
8289 		break;
8290 
8291 	case ZPOOL_STATUS_OFFLINE_DEV:
8292 		printf_color(ANSI_BOLD, gettext("status: "));
8293 		printf_color(ANSI_YELLOW, gettext("One or more devices has "
8294 		    "been taken offline by the administrator.\n\tSufficient "
8295 		    "replicas exist for the pool to continue functioning in "
8296 		    "a\n\tdegraded state.\n"));
8297 		printf_color(ANSI_BOLD, gettext("action: "));
8298 		printf_color(ANSI_YELLOW, gettext("Online the device "
8299 		    "using 'zpool online' or replace the device with\n\t'zpool "
8300 		    "replace'.\n"));
8301 		break;
8302 
8303 	case ZPOOL_STATUS_REMOVED_DEV:
8304 		printf_color(ANSI_BOLD, gettext("status: "));
8305 		printf_color(ANSI_YELLOW, gettext("One or more devices has "
8306 		    "been removed by the administrator.\n\tSufficient "
8307 		    "replicas exist for the pool to continue functioning in "
8308 		    "a\n\tdegraded state.\n"));
8309 		printf_color(ANSI_BOLD, gettext("action: "));
8310 		printf_color(ANSI_YELLOW, gettext("Online the device "
8311 		    "using zpool online' or replace the device with\n\t'zpool "
8312 		    "replace'.\n"));
8313 		break;
8314 
8315 	case ZPOOL_STATUS_RESILVERING:
8316 	case ZPOOL_STATUS_REBUILDING:
8317 		printf_color(ANSI_BOLD, gettext("status: "));
8318 		printf_color(ANSI_YELLOW, gettext("One or more devices is "
8319 		    "currently being resilvered.  The pool will\n\tcontinue "
8320 		    "to function, possibly in a degraded state.\n"));
8321 		printf_color(ANSI_BOLD, gettext("action: "));
8322 		printf_color(ANSI_YELLOW, gettext("Wait for the resilver to "
8323 		    "complete.\n"));
8324 		break;
8325 
8326 	case ZPOOL_STATUS_REBUILD_SCRUB:
8327 		printf_color(ANSI_BOLD, gettext("status: "));
8328 		printf_color(ANSI_YELLOW, gettext("One or more devices have "
8329 		    "been sequentially resilvered, scrubbing\n\tthe pool "
8330 		    "is recommended.\n"));
8331 		printf_color(ANSI_BOLD, gettext("action: "));
8332 		printf_color(ANSI_YELLOW, gettext("Use 'zpool scrub' to "
8333 		    "verify all data checksums.\n"));
8334 		break;
8335 
8336 	case ZPOOL_STATUS_CORRUPT_DATA:
8337 		printf_color(ANSI_BOLD, gettext("status: "));
8338 		printf_color(ANSI_YELLOW, gettext("One or more devices has "
8339 		    "experienced an error resulting in data\n\tcorruption.  "
8340 		    "Applications may be affected.\n"));
8341 		printf_color(ANSI_BOLD, gettext("action: "));
8342 		printf_color(ANSI_YELLOW, gettext("Restore the file in question"
8343 		    " if possible.  Otherwise restore the\n\tentire pool from "
8344 		    "backup.\n"));
8345 		break;
8346 
8347 	case ZPOOL_STATUS_CORRUPT_POOL:
8348 		printf_color(ANSI_BOLD, gettext("status: "));
8349 		printf_color(ANSI_YELLOW, gettext("The pool metadata is "
8350 		    "corrupted and the pool cannot be opened.\n"));
8351 		zpool_explain_recover(zpool_get_handle(zhp),
8352 		    zpool_get_name(zhp), reason, config);
8353 		break;
8354 
8355 	case ZPOOL_STATUS_VERSION_OLDER:
8356 		printf_color(ANSI_BOLD, gettext("status: "));
8357 		printf_color(ANSI_YELLOW, gettext("The pool is formatted using "
8358 		    "a legacy on-disk format.  The pool can\n\tstill be used, "
8359 		    "but some features are unavailable.\n"));
8360 		printf_color(ANSI_BOLD, gettext("action: "));
8361 		printf_color(ANSI_YELLOW, gettext("Upgrade the pool using "
8362 		    "'zpool upgrade'.  Once this is done, the\n\tpool will no "
8363 		    "longer be accessible on software that does not support\n\t"
8364 		    "feature flags.\n"));
8365 		break;
8366 
8367 	case ZPOOL_STATUS_VERSION_NEWER:
8368 		printf_color(ANSI_BOLD, gettext("status: "));
8369 		printf_color(ANSI_YELLOW, gettext("The pool has been upgraded "
8370 		    "to a newer, incompatible on-disk version.\n\tThe pool "
8371 		    "cannot be accessed on this system.\n"));
8372 		printf_color(ANSI_BOLD, gettext("action: "));
8373 		printf_color(ANSI_YELLOW, gettext("Access the pool from a "
8374 		    "system running more recent software, or\n\trestore the "
8375 		    "pool from backup.\n"));
8376 		break;
8377 
8378 	case ZPOOL_STATUS_FEAT_DISABLED:
8379 		printf_color(ANSI_BOLD, gettext("status: "));
8380 		printf_color(ANSI_YELLOW, gettext("Some supported and "
8381 		    "requested features are not enabled on the pool.\n\t"
8382 		    "The pool can still be used, but some features are "
8383 		    "unavailable.\n"));
8384 		printf_color(ANSI_BOLD, gettext("action: "));
8385 		printf_color(ANSI_YELLOW, gettext("Enable all features using "
8386 		    "'zpool upgrade'. Once this is done,\n\tthe pool may no "
8387 		    "longer be accessible by software that does not support\n\t"
8388 		    "the features. See zpool-features(7) for details.\n"));
8389 		break;
8390 
8391 	case ZPOOL_STATUS_COMPATIBILITY_ERR:
8392 		printf_color(ANSI_BOLD, gettext("status: "));
8393 		printf_color(ANSI_YELLOW, gettext("This pool has a "
8394 		    "compatibility list specified, but it could not be\n\t"
8395 		    "read/parsed at this time. The pool can still be used, "
8396 		    "but this\n\tshould be investigated.\n"));
8397 		printf_color(ANSI_BOLD, gettext("action: "));
8398 		printf_color(ANSI_YELLOW, gettext("Check the value of the "
8399 		    "'compatibility' property against the\n\t"
8400 		    "appropriate file in " ZPOOL_SYSCONF_COMPAT_D " or "
8401 		    ZPOOL_DATA_COMPAT_D ".\n"));
8402 		break;
8403 
8404 	case ZPOOL_STATUS_INCOMPATIBLE_FEAT:
8405 		printf_color(ANSI_BOLD, gettext("status: "));
8406 		printf_color(ANSI_YELLOW, gettext("One or more features "
8407 		    "are enabled on the pool despite not being\n\t"
8408 		    "requested by the 'compatibility' property.\n"));
8409 		printf_color(ANSI_BOLD, gettext("action: "));
8410 		printf_color(ANSI_YELLOW, gettext("Consider setting "
8411 		    "'compatibility' to an appropriate value, or\n\t"
8412 		    "adding needed features to the relevant file in\n\t"
8413 		    ZPOOL_SYSCONF_COMPAT_D " or " ZPOOL_DATA_COMPAT_D ".\n"));
8414 		break;
8415 
8416 	case ZPOOL_STATUS_UNSUP_FEAT_READ:
8417 		printf_color(ANSI_BOLD, gettext("status: "));
8418 		printf_color(ANSI_YELLOW, gettext("The pool cannot be accessed "
8419 		    "on this system because it uses the\n\tfollowing feature(s)"
8420 		    " not supported on this system:\n"));
8421 		zpool_print_unsup_feat(config);
8422 		(void) printf("\n");
8423 		printf_color(ANSI_BOLD, gettext("action: "));
8424 		printf_color(ANSI_YELLOW, gettext("Access the pool from a "
8425 		    "system that supports the required feature(s),\n\tor "
8426 		    "restore the pool from backup.\n"));
8427 		break;
8428 
8429 	case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
8430 		printf_color(ANSI_BOLD, gettext("status: "));
8431 		printf_color(ANSI_YELLOW, gettext("The pool can only be "
8432 		    "accessed in read-only mode on this system. It\n\tcannot be"
8433 		    " accessed in read-write mode because it uses the "
8434 		    "following\n\tfeature(s) not supported on this system:\n"));
8435 		zpool_print_unsup_feat(config);
8436 		(void) printf("\n");
8437 		printf_color(ANSI_BOLD, gettext("action: "));
8438 		printf_color(ANSI_YELLOW, gettext("The pool cannot be accessed "
8439 		    "in read-write mode. Import the pool with\n"
8440 		    "\t\"-o readonly=on\", access the pool from a system that "
8441 		    "supports the\n\trequired feature(s), or restore the "
8442 		    "pool from backup.\n"));
8443 		break;
8444 
8445 	case ZPOOL_STATUS_FAULTED_DEV_R:
8446 		printf_color(ANSI_BOLD, gettext("status: "));
8447 		printf_color(ANSI_YELLOW, gettext("One or more devices are "
8448 		    "faulted in response to persistent errors.\n\tSufficient "
8449 		    "replicas exist for the pool to continue functioning "
8450 		    "in a\n\tdegraded state.\n"));
8451 		printf_color(ANSI_BOLD, gettext("action: "));
8452 		printf_color(ANSI_YELLOW, gettext("Replace the faulted device, "
8453 		    "or use 'zpool clear' to mark the device\n\trepaired.\n"));
8454 		break;
8455 
8456 	case ZPOOL_STATUS_FAULTED_DEV_NR:
8457 		printf_color(ANSI_BOLD, gettext("status: "));
8458 		printf_color(ANSI_YELLOW, gettext("One or more devices are "
8459 		    "faulted in response to persistent errors.  There are "
8460 		    "insufficient replicas for the pool to\n\tcontinue "
8461 		    "functioning.\n"));
8462 		printf_color(ANSI_BOLD, gettext("action: "));
8463 		printf_color(ANSI_YELLOW, gettext("Destroy and re-create the "
8464 		    "pool from a backup source.  Manually marking the device\n"
8465 		    "\trepaired using 'zpool clear' may allow some data "
8466 		    "to be recovered.\n"));
8467 		break;
8468 
8469 	case ZPOOL_STATUS_IO_FAILURE_MMP:
8470 		printf_color(ANSI_BOLD, gettext("status: "));
8471 		printf_color(ANSI_YELLOW, gettext("The pool is suspended "
8472 		    "because multihost writes failed or were delayed;\n\t"
8473 		    "another system could import the pool undetected.\n"));
8474 		printf_color(ANSI_BOLD, gettext("action: "));
8475 		printf_color(ANSI_YELLOW, gettext("Make sure the pool's devices"
8476 		    " are connected, then reboot your system and\n\timport the "
8477 		    "pool.\n"));
8478 		break;
8479 
8480 	case ZPOOL_STATUS_IO_FAILURE_WAIT:
8481 	case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
8482 		printf_color(ANSI_BOLD, gettext("status: "));
8483 		printf_color(ANSI_YELLOW, gettext("One or more devices are "
8484 		    "faulted in response to IO failures.\n"));
8485 		printf_color(ANSI_BOLD, gettext("action: "));
8486 		printf_color(ANSI_YELLOW, gettext("Make sure the affected "
8487 		    "devices are connected, then run 'zpool clear'.\n"));
8488 		break;
8489 
8490 	case ZPOOL_STATUS_BAD_LOG:
8491 		printf_color(ANSI_BOLD, gettext("status: "));
8492 		printf_color(ANSI_YELLOW, gettext("An intent log record "
8493 		    "could not be read.\n"
8494 		    "\tWaiting for administrator intervention to fix the "
8495 		    "faulted pool.\n"));
8496 		printf_color(ANSI_BOLD, gettext("action: "));
8497 		printf_color(ANSI_YELLOW, gettext("Either restore the affected "
8498 		    "device(s) and run 'zpool online',\n"
8499 		    "\tor ignore the intent log records by running "
8500 		    "'zpool clear'.\n"));
8501 		break;
8502 
8503 	case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
8504 		(void) printf(gettext("status: One or more devices are "
8505 		    "configured to use a non-native block size.\n"
8506 		    "\tExpect reduced performance.\n"));
8507 		(void) printf(gettext("action: Replace affected devices with "
8508 		    "devices that support the\n\tconfigured block size, or "
8509 		    "migrate data to a properly configured\n\tpool.\n"));
8510 		break;
8511 
8512 	case ZPOOL_STATUS_HOSTID_MISMATCH:
8513 		printf_color(ANSI_BOLD, gettext("status: "));
8514 		printf_color(ANSI_YELLOW, gettext("Mismatch between pool hostid"
8515 		    " and system hostid on imported pool.\n\tThis pool was "
8516 		    "previously imported into a system with a different "
8517 		    "hostid,\n\tand then was verbatim imported into this "
8518 		    "system.\n"));
8519 		printf_color(ANSI_BOLD, gettext("action: "));
8520 		printf_color(ANSI_YELLOW, gettext("Export this pool on all "
8521 		    "systems on which it is imported.\n"
8522 		    "\tThen import it to correct the mismatch.\n"));
8523 		break;
8524 
8525 	case ZPOOL_STATUS_ERRATA:
8526 		printf_color(ANSI_BOLD, gettext("status: "));
8527 		printf_color(ANSI_YELLOW, gettext("Errata #%d detected.\n"),
8528 		    errata);
8529 
8530 		switch (errata) {
8531 		case ZPOOL_ERRATA_NONE:
8532 			break;
8533 
8534 		case ZPOOL_ERRATA_ZOL_2094_SCRUB:
8535 			printf_color(ANSI_BOLD, gettext("action: "));
8536 			printf_color(ANSI_YELLOW, gettext("To correct the issue"
8537 			    " run 'zpool scrub'.\n"));
8538 			break;
8539 
8540 		case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION:
8541 			(void) printf(gettext("\tExisting encrypted datasets "
8542 			    "contain an on-disk incompatibility\n\twhich "
8543 			    "needs to be corrected.\n"));
8544 			printf_color(ANSI_BOLD, gettext("action: "));
8545 			printf_color(ANSI_YELLOW, gettext("To correct the issue"
8546 			    " backup existing encrypted datasets to new\n\t"
8547 			    "encrypted datasets and destroy the old ones. "
8548 			    "'zfs mount -o ro' can\n\tbe used to temporarily "
8549 			    "mount existing encrypted datasets readonly.\n"));
8550 			break;
8551 
8552 		case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION:
8553 			(void) printf(gettext("\tExisting encrypted snapshots "
8554 			    "and bookmarks contain an on-disk\n\tincompat"
8555 			    "ibility. This may cause on-disk corruption if "
8556 			    "they are used\n\twith 'zfs recv'.\n"));
8557 			printf_color(ANSI_BOLD, gettext("action: "));
8558 			printf_color(ANSI_YELLOW, gettext("To correct the"
8559 			    "issue, enable the bookmark_v2 feature. No "
8560 			    "additional\n\taction is needed if there are no "
8561 			    "encrypted snapshots or bookmarks.\n\tIf preserving"
8562 			    "the encrypted snapshots and bookmarks is required,"
8563 			    " use\n\ta non-raw send to backup and restore them."
8564 			    " Alternately, they may be\n\tremoved to resolve "
8565 			    "the incompatibility.\n"));
8566 			break;
8567 
8568 		default:
8569 			/*
8570 			 * All errata which allow the pool to be imported
8571 			 * must contain an action message.
8572 			 */
8573 			assert(0);
8574 		}
8575 		break;
8576 
8577 	default:
8578 		/*
8579 		 * The remaining errors can't actually be generated, yet.
8580 		 */
8581 		assert(reason == ZPOOL_STATUS_OK);
8582 	}
8583 
8584 	if (msgid != NULL) {
8585 		printf("   ");
8586 		printf_color(ANSI_BOLD, gettext("see:"));
8587 		printf(gettext(
8588 		    " https://openzfs.github.io/openzfs-docs/msg/%s\n"),
8589 		    msgid);
8590 	}
8591 
8592 	if (config != NULL) {
8593 		uint64_t nerr;
8594 		nvlist_t **spares, **l2cache;
8595 		uint_t nspares, nl2cache;
8596 		pool_checkpoint_stat_t *pcs = NULL;
8597 		pool_removal_stat_t *prs = NULL;
8598 
8599 		print_scan_status(zhp, nvroot);
8600 
8601 		(void) nvlist_lookup_uint64_array(nvroot,
8602 		    ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c);
8603 		print_removal_status(zhp, prs);
8604 
8605 		(void) nvlist_lookup_uint64_array(nvroot,
8606 		    ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
8607 		print_checkpoint_status(pcs);
8608 
8609 		cbp->cb_namewidth = max_width(zhp, nvroot, 0, 0,
8610 		    cbp->cb_name_flags | VDEV_NAME_TYPE_ID);
8611 		if (cbp->cb_namewidth < 10)
8612 			cbp->cb_namewidth = 10;
8613 
8614 		color_start(ANSI_BOLD);
8615 		(void) printf(gettext("config:\n\n"));
8616 		(void) printf(gettext("\t%-*s  %-8s %5s %5s %5s"),
8617 		    cbp->cb_namewidth, "NAME", "STATE", "READ", "WRITE",
8618 		    "CKSUM");
8619 		color_end();
8620 
8621 		if (cbp->cb_print_slow_ios) {
8622 			printf_color(ANSI_BOLD, " %5s", gettext("SLOW"));
8623 		}
8624 
8625 		if (cbp->vcdl != NULL)
8626 			print_cmd_columns(cbp->vcdl, 0);
8627 
8628 		printf("\n");
8629 
8630 		print_status_config(zhp, cbp, zpool_get_name(zhp), nvroot, 0,
8631 		    B_FALSE, NULL);
8632 
8633 		print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_DEDUP);
8634 		print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_SPECIAL);
8635 		print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_CLASS_LOGS);
8636 
8637 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
8638 		    &l2cache, &nl2cache) == 0)
8639 			print_l2cache(zhp, cbp, l2cache, nl2cache);
8640 
8641 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
8642 		    &spares, &nspares) == 0)
8643 			print_spares(zhp, cbp, spares, nspares);
8644 
8645 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
8646 		    &nerr) == 0) {
8647 			(void) printf("\n");
8648 			if (nerr == 0) {
8649 				(void) printf(gettext(
8650 				    "errors: No known data errors\n"));
8651 			} else if (!cbp->cb_verbose) {
8652 				(void) printf(gettext("errors: %llu data "
8653 				    "errors, use '-v' for a list\n"),
8654 				    (u_longlong_t)nerr);
8655 			} else {
8656 				print_error_log(zhp);
8657 			}
8658 		}
8659 
8660 		if (cbp->cb_dedup_stats)
8661 			print_dedup_stats(config);
8662 	} else {
8663 		(void) printf(gettext("config: The configuration cannot be "
8664 		    "determined.\n"));
8665 	}
8666 
8667 	return (0);
8668 }
8669 
8670 /*
8671  * zpool status [-c [script1,script2,...]] [-igLpPstvx] [-T d|u] [pool] ...
8672  *              [interval [count]]
8673  *
8674  *	-c CMD	For each vdev, run command CMD
8675  *	-i	Display vdev initialization status.
8676  *	-g	Display guid for individual vdev name.
8677  *	-L	Follow links when resolving vdev path name.
8678  *	-p	Display values in parsable (exact) format.
8679  *	-P	Display full path for vdev name.
8680  *	-s	Display slow IOs column.
8681  *	-v	Display complete error logs
8682  *	-x	Display only pools with potential problems
8683  *	-D	Display dedup status (undocumented)
8684  *	-t	Display vdev TRIM status.
8685  *	-T	Display a timestamp in date(1) or Unix format
8686  *
8687  * Describes the health status of all pools or some subset.
8688  */
8689 int
8690 zpool_do_status(int argc, char **argv)
8691 {
8692 	int c;
8693 	int ret;
8694 	float interval = 0;
8695 	unsigned long count = 0;
8696 	status_cbdata_t cb = { 0 };
8697 	char *cmd = NULL;
8698 
8699 	/* check options */
8700 	while ((c = getopt(argc, argv, "c:igLpPsvxDtT:")) != -1) {
8701 		switch (c) {
8702 		case 'c':
8703 			if (cmd != NULL) {
8704 				fprintf(stderr,
8705 				    gettext("Can't set -c flag twice\n"));
8706 				exit(1);
8707 			}
8708 
8709 			if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL &&
8710 			    !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
8711 				fprintf(stderr, gettext(
8712 				    "Can't run -c, disabled by "
8713 				    "ZPOOL_SCRIPTS_ENABLED.\n"));
8714 				exit(1);
8715 			}
8716 
8717 			if ((getuid() <= 0 || geteuid() <= 0) &&
8718 			    !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
8719 				fprintf(stderr, gettext(
8720 				    "Can't run -c with root privileges "
8721 				    "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
8722 				exit(1);
8723 			}
8724 			cmd = optarg;
8725 			break;
8726 		case 'i':
8727 			cb.cb_print_vdev_init = B_TRUE;
8728 			break;
8729 		case 'g':
8730 			cb.cb_name_flags |= VDEV_NAME_GUID;
8731 			break;
8732 		case 'L':
8733 			cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
8734 			break;
8735 		case 'p':
8736 			cb.cb_literal = B_TRUE;
8737 			break;
8738 		case 'P':
8739 			cb.cb_name_flags |= VDEV_NAME_PATH;
8740 			break;
8741 		case 's':
8742 			cb.cb_print_slow_ios = B_TRUE;
8743 			break;
8744 		case 'v':
8745 			cb.cb_verbose = B_TRUE;
8746 			break;
8747 		case 'x':
8748 			cb.cb_explain = B_TRUE;
8749 			break;
8750 		case 'D':
8751 			cb.cb_dedup_stats = B_TRUE;
8752 			break;
8753 		case 't':
8754 			cb.cb_print_vdev_trim = B_TRUE;
8755 			break;
8756 		case 'T':
8757 			get_timestamp_arg(*optarg);
8758 			break;
8759 		case '?':
8760 			if (optopt == 'c') {
8761 				print_zpool_script_list("status");
8762 				exit(0);
8763 			} else {
8764 				fprintf(stderr,
8765 				    gettext("invalid option '%c'\n"), optopt);
8766 			}
8767 			usage(B_FALSE);
8768 		}
8769 	}
8770 
8771 	argc -= optind;
8772 	argv += optind;
8773 
8774 	get_interval_count(&argc, argv, &interval, &count);
8775 
8776 	if (argc == 0)
8777 		cb.cb_allpools = B_TRUE;
8778 
8779 	cb.cb_first = B_TRUE;
8780 	cb.cb_print_status = B_TRUE;
8781 
8782 	for (;;) {
8783 		if (timestamp_fmt != NODATE)
8784 			print_timestamp(timestamp_fmt);
8785 
8786 		if (cmd != NULL)
8787 			cb.vcdl = all_pools_for_each_vdev_run(argc, argv, cmd,
8788 			    NULL, NULL, 0, 0);
8789 
8790 		ret = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
8791 		    cb.cb_literal, status_callback, &cb);
8792 
8793 		if (cb.vcdl != NULL)
8794 			free_vdev_cmd_data_list(cb.vcdl);
8795 
8796 		if (argc == 0 && cb.cb_count == 0)
8797 			(void) fprintf(stderr, gettext("no pools available\n"));
8798 		else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
8799 			(void) printf(gettext("all pools are healthy\n"));
8800 
8801 		if (ret != 0)
8802 			return (ret);
8803 
8804 		if (interval == 0)
8805 			break;
8806 
8807 		if (count != 0 && --count == 0)
8808 			break;
8809 
8810 		(void) fsleep(interval);
8811 	}
8812 
8813 	return (0);
8814 }
8815 
8816 typedef struct upgrade_cbdata {
8817 	int	cb_first;
8818 	int	cb_argc;
8819 	uint64_t cb_version;
8820 	char	**cb_argv;
8821 } upgrade_cbdata_t;
8822 
8823 static int
8824 check_unsupp_fs(zfs_handle_t *zhp, void *unsupp_fs)
8825 {
8826 	int zfs_version = (int)zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
8827 	int *count = (int *)unsupp_fs;
8828 
8829 	if (zfs_version > ZPL_VERSION) {
8830 		(void) printf(gettext("%s (v%d) is not supported by this "
8831 		    "implementation of ZFS.\n"),
8832 		    zfs_get_name(zhp), zfs_version);
8833 		(*count)++;
8834 	}
8835 
8836 	zfs_iter_filesystems(zhp, 0, check_unsupp_fs, unsupp_fs);
8837 
8838 	zfs_close(zhp);
8839 
8840 	return (0);
8841 }
8842 
8843 static int
8844 upgrade_version(zpool_handle_t *zhp, uint64_t version)
8845 {
8846 	int ret;
8847 	nvlist_t *config;
8848 	uint64_t oldversion;
8849 	int unsupp_fs = 0;
8850 
8851 	config = zpool_get_config(zhp, NULL);
8852 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
8853 	    &oldversion) == 0);
8854 
8855 	char compat[ZFS_MAXPROPLEN];
8856 	if (zpool_get_prop(zhp, ZPOOL_PROP_COMPATIBILITY, compat,
8857 	    ZFS_MAXPROPLEN, NULL, B_FALSE) != 0)
8858 		compat[0] = '\0';
8859 
8860 	assert(SPA_VERSION_IS_SUPPORTED(oldversion));
8861 	assert(oldversion < version);
8862 
8863 	ret = zfs_iter_root(zpool_get_handle(zhp), check_unsupp_fs, &unsupp_fs);
8864 	if (ret != 0)
8865 		return (ret);
8866 
8867 	if (unsupp_fs) {
8868 		(void) fprintf(stderr, gettext("Upgrade not performed due "
8869 		    "to %d unsupported filesystems (max v%d).\n"),
8870 		    unsupp_fs, (int)ZPL_VERSION);
8871 		return (1);
8872 	}
8873 
8874 	if (strcmp(compat, ZPOOL_COMPAT_LEGACY) == 0) {
8875 		(void) fprintf(stderr, gettext("Upgrade not performed because "
8876 		    "'compatibility' property set to '"
8877 		    ZPOOL_COMPAT_LEGACY "'.\n"));
8878 		return (1);
8879 	}
8880 
8881 	ret = zpool_upgrade(zhp, version);
8882 	if (ret != 0)
8883 		return (ret);
8884 
8885 	if (version >= SPA_VERSION_FEATURES) {
8886 		(void) printf(gettext("Successfully upgraded "
8887 		    "'%s' from version %llu to feature flags.\n"),
8888 		    zpool_get_name(zhp), (u_longlong_t)oldversion);
8889 	} else {
8890 		(void) printf(gettext("Successfully upgraded "
8891 		    "'%s' from version %llu to version %llu.\n"),
8892 		    zpool_get_name(zhp), (u_longlong_t)oldversion,
8893 		    (u_longlong_t)version);
8894 	}
8895 
8896 	return (0);
8897 }
8898 
8899 static int
8900 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
8901 {
8902 	int i, ret, count;
8903 	boolean_t firstff = B_TRUE;
8904 	nvlist_t *enabled = zpool_get_features(zhp);
8905 
8906 	char compat[ZFS_MAXPROPLEN];
8907 	if (zpool_get_prop(zhp, ZPOOL_PROP_COMPATIBILITY, compat,
8908 	    ZFS_MAXPROPLEN, NULL, B_FALSE) != 0)
8909 		compat[0] = '\0';
8910 
8911 	boolean_t requested_features[SPA_FEATURES];
8912 	if (zpool_do_load_compat(compat, requested_features) !=
8913 	    ZPOOL_COMPATIBILITY_OK)
8914 		return (-1);
8915 
8916 	count = 0;
8917 	for (i = 0; i < SPA_FEATURES; i++) {
8918 		const char *fname = spa_feature_table[i].fi_uname;
8919 		const char *fguid = spa_feature_table[i].fi_guid;
8920 
8921 		if (!spa_feature_table[i].fi_zfs_mod_supported)
8922 			continue;
8923 
8924 		if (!nvlist_exists(enabled, fguid) && requested_features[i]) {
8925 			char *propname;
8926 			verify(-1 != asprintf(&propname, "feature@%s", fname));
8927 			ret = zpool_set_prop(zhp, propname,
8928 			    ZFS_FEATURE_ENABLED);
8929 			if (ret != 0) {
8930 				free(propname);
8931 				return (ret);
8932 			}
8933 			count++;
8934 
8935 			if (firstff) {
8936 				(void) printf(gettext("Enabled the "
8937 				    "following features on '%s':\n"),
8938 				    zpool_get_name(zhp));
8939 				firstff = B_FALSE;
8940 			}
8941 			(void) printf(gettext("  %s\n"), fname);
8942 			free(propname);
8943 		}
8944 	}
8945 
8946 	if (countp != NULL)
8947 		*countp = count;
8948 	return (0);
8949 }
8950 
8951 static int
8952 upgrade_cb(zpool_handle_t *zhp, void *arg)
8953 {
8954 	upgrade_cbdata_t *cbp = arg;
8955 	nvlist_t *config;
8956 	uint64_t version;
8957 	boolean_t modified_pool = B_FALSE;
8958 	int ret;
8959 
8960 	config = zpool_get_config(zhp, NULL);
8961 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
8962 	    &version) == 0);
8963 
8964 	assert(SPA_VERSION_IS_SUPPORTED(version));
8965 
8966 	if (version < cbp->cb_version) {
8967 		cbp->cb_first = B_FALSE;
8968 		ret = upgrade_version(zhp, cbp->cb_version);
8969 		if (ret != 0)
8970 			return (ret);
8971 		modified_pool = B_TRUE;
8972 
8973 		/*
8974 		 * If they did "zpool upgrade -a", then we could
8975 		 * be doing ioctls to different pools.  We need
8976 		 * to log this history once to each pool, and bypass
8977 		 * the normal history logging that happens in main().
8978 		 */
8979 		(void) zpool_log_history(g_zfs, history_str);
8980 		log_history = B_FALSE;
8981 	}
8982 
8983 	if (cbp->cb_version >= SPA_VERSION_FEATURES) {
8984 		int count;
8985 		ret = upgrade_enable_all(zhp, &count);
8986 		if (ret != 0)
8987 			return (ret);
8988 
8989 		if (count > 0) {
8990 			cbp->cb_first = B_FALSE;
8991 			modified_pool = B_TRUE;
8992 		}
8993 	}
8994 
8995 	if (modified_pool) {
8996 		(void) printf("\n");
8997 		(void) after_zpool_upgrade(zhp);
8998 	}
8999 
9000 	return (0);
9001 }
9002 
9003 static int
9004 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
9005 {
9006 	upgrade_cbdata_t *cbp = arg;
9007 	nvlist_t *config;
9008 	uint64_t version;
9009 
9010 	config = zpool_get_config(zhp, NULL);
9011 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
9012 	    &version) == 0);
9013 
9014 	assert(SPA_VERSION_IS_SUPPORTED(version));
9015 
9016 	if (version < SPA_VERSION_FEATURES) {
9017 		if (cbp->cb_first) {
9018 			(void) printf(gettext("The following pools are "
9019 			    "formatted with legacy version numbers and can\n"
9020 			    "be upgraded to use feature flags.  After "
9021 			    "being upgraded, these pools\nwill no "
9022 			    "longer be accessible by software that does not "
9023 			    "support feature\nflags.\n\n"
9024 			    "Note that setting a pool's 'compatibility' "
9025 			    "feature to '" ZPOOL_COMPAT_LEGACY "' will\n"
9026 			    "inhibit upgrades.\n\n"));
9027 			(void) printf(gettext("VER  POOL\n"));
9028 			(void) printf(gettext("---  ------------\n"));
9029 			cbp->cb_first = B_FALSE;
9030 		}
9031 
9032 		(void) printf("%2llu   %s\n", (u_longlong_t)version,
9033 		    zpool_get_name(zhp));
9034 	}
9035 
9036 	return (0);
9037 }
9038 
9039 static int
9040 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
9041 {
9042 	upgrade_cbdata_t *cbp = arg;
9043 	nvlist_t *config;
9044 	uint64_t version;
9045 
9046 	config = zpool_get_config(zhp, NULL);
9047 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
9048 	    &version) == 0);
9049 
9050 	if (version >= SPA_VERSION_FEATURES) {
9051 		int i;
9052 		boolean_t poolfirst = B_TRUE;
9053 		nvlist_t *enabled = zpool_get_features(zhp);
9054 
9055 		for (i = 0; i < SPA_FEATURES; i++) {
9056 			const char *fguid = spa_feature_table[i].fi_guid;
9057 			const char *fname = spa_feature_table[i].fi_uname;
9058 
9059 			if (!spa_feature_table[i].fi_zfs_mod_supported)
9060 				continue;
9061 
9062 			if (!nvlist_exists(enabled, fguid)) {
9063 				if (cbp->cb_first) {
9064 					(void) printf(gettext("\nSome "
9065 					    "supported features are not "
9066 					    "enabled on the following pools. "
9067 					    "Once a\nfeature is enabled the "
9068 					    "pool may become incompatible with "
9069 					    "software\nthat does not support "
9070 					    "the feature. See "
9071 					    "zpool-features(7) for "
9072 					    "details.\n\n"
9073 					    "Note that the pool "
9074 					    "'compatibility' feature can be "
9075 					    "used to inhibit\nfeature "
9076 					    "upgrades.\n\n"));
9077 					(void) printf(gettext("POOL  "
9078 					    "FEATURE\n"));
9079 					(void) printf(gettext("------"
9080 					    "---------\n"));
9081 					cbp->cb_first = B_FALSE;
9082 				}
9083 
9084 				if (poolfirst) {
9085 					(void) printf(gettext("%s\n"),
9086 					    zpool_get_name(zhp));
9087 					poolfirst = B_FALSE;
9088 				}
9089 
9090 				(void) printf(gettext("      %s\n"), fname);
9091 			}
9092 			/*
9093 			 * If they did "zpool upgrade -a", then we could
9094 			 * be doing ioctls to different pools.  We need
9095 			 * to log this history once to each pool, and bypass
9096 			 * the normal history logging that happens in main().
9097 			 */
9098 			(void) zpool_log_history(g_zfs, history_str);
9099 			log_history = B_FALSE;
9100 		}
9101 	}
9102 
9103 	return (0);
9104 }
9105 
9106 static int
9107 upgrade_one(zpool_handle_t *zhp, void *data)
9108 {
9109 	boolean_t modified_pool = B_FALSE;
9110 	upgrade_cbdata_t *cbp = data;
9111 	uint64_t cur_version;
9112 	int ret;
9113 
9114 	if (strcmp("log", zpool_get_name(zhp)) == 0) {
9115 		(void) fprintf(stderr, gettext("'log' is now a reserved word\n"
9116 		    "Pool 'log' must be renamed using export and import"
9117 		    " to upgrade.\n"));
9118 		return (1);
9119 	}
9120 
9121 	cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
9122 	if (cur_version > cbp->cb_version) {
9123 		(void) printf(gettext("Pool '%s' is already formatted "
9124 		    "using more current version '%llu'.\n\n"),
9125 		    zpool_get_name(zhp), (u_longlong_t)cur_version);
9126 		return (0);
9127 	}
9128 
9129 	if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
9130 		(void) printf(gettext("Pool '%s' is already formatted "
9131 		    "using version %llu.\n\n"), zpool_get_name(zhp),
9132 		    (u_longlong_t)cbp->cb_version);
9133 		return (0);
9134 	}
9135 
9136 	if (cur_version != cbp->cb_version) {
9137 		modified_pool = B_TRUE;
9138 		ret = upgrade_version(zhp, cbp->cb_version);
9139 		if (ret != 0)
9140 			return (ret);
9141 	}
9142 
9143 	if (cbp->cb_version >= SPA_VERSION_FEATURES) {
9144 		int count = 0;
9145 		ret = upgrade_enable_all(zhp, &count);
9146 		if (ret != 0)
9147 			return (ret);
9148 
9149 		if (count != 0) {
9150 			modified_pool = B_TRUE;
9151 		} else if (cur_version == SPA_VERSION) {
9152 			(void) printf(gettext("Pool '%s' already has all "
9153 			    "supported and requested features enabled.\n"),
9154 			    zpool_get_name(zhp));
9155 		}
9156 	}
9157 
9158 	if (modified_pool) {
9159 		(void) printf("\n");
9160 		(void) after_zpool_upgrade(zhp);
9161 	}
9162 
9163 	return (0);
9164 }
9165 
9166 /*
9167  * zpool upgrade
9168  * zpool upgrade -v
9169  * zpool upgrade [-V version] <-a | pool ...>
9170  *
9171  * With no arguments, display downrev'd ZFS pool available for upgrade.
9172  * Individual pools can be upgraded by specifying the pool, and '-a' will
9173  * upgrade all pools.
9174  */
9175 int
9176 zpool_do_upgrade(int argc, char **argv)
9177 {
9178 	int c;
9179 	upgrade_cbdata_t cb = { 0 };
9180 	int ret = 0;
9181 	boolean_t showversions = B_FALSE;
9182 	boolean_t upgradeall = B_FALSE;
9183 	char *end;
9184 
9185 
9186 	/* check options */
9187 	while ((c = getopt(argc, argv, ":avV:")) != -1) {
9188 		switch (c) {
9189 		case 'a':
9190 			upgradeall = B_TRUE;
9191 			break;
9192 		case 'v':
9193 			showversions = B_TRUE;
9194 			break;
9195 		case 'V':
9196 			cb.cb_version = strtoll(optarg, &end, 10);
9197 			if (*end != '\0' ||
9198 			    !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
9199 				(void) fprintf(stderr,
9200 				    gettext("invalid version '%s'\n"), optarg);
9201 				usage(B_FALSE);
9202 			}
9203 			break;
9204 		case ':':
9205 			(void) fprintf(stderr, gettext("missing argument for "
9206 			    "'%c' option\n"), optopt);
9207 			usage(B_FALSE);
9208 			break;
9209 		case '?':
9210 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
9211 			    optopt);
9212 			usage(B_FALSE);
9213 		}
9214 	}
9215 
9216 	cb.cb_argc = argc;
9217 	cb.cb_argv = argv;
9218 	argc -= optind;
9219 	argv += optind;
9220 
9221 	if (cb.cb_version == 0) {
9222 		cb.cb_version = SPA_VERSION;
9223 	} else if (!upgradeall && argc == 0) {
9224 		(void) fprintf(stderr, gettext("-V option is "
9225 		    "incompatible with other arguments\n"));
9226 		usage(B_FALSE);
9227 	}
9228 
9229 	if (showversions) {
9230 		if (upgradeall || argc != 0) {
9231 			(void) fprintf(stderr, gettext("-v option is "
9232 			    "incompatible with other arguments\n"));
9233 			usage(B_FALSE);
9234 		}
9235 	} else if (upgradeall) {
9236 		if (argc != 0) {
9237 			(void) fprintf(stderr, gettext("-a option should not "
9238 			    "be used along with a pool name\n"));
9239 			usage(B_FALSE);
9240 		}
9241 	}
9242 
9243 	(void) printf("%s", gettext("This system supports ZFS pool feature "
9244 	    "flags.\n\n"));
9245 	if (showversions) {
9246 		int i;
9247 
9248 		(void) printf(gettext("The following features are "
9249 		    "supported:\n\n"));
9250 		(void) printf(gettext("FEAT DESCRIPTION\n"));
9251 		(void) printf("----------------------------------------------"
9252 		    "---------------\n");
9253 		for (i = 0; i < SPA_FEATURES; i++) {
9254 			zfeature_info_t *fi = &spa_feature_table[i];
9255 			if (!fi->fi_zfs_mod_supported)
9256 				continue;
9257 			const char *ro =
9258 			    (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
9259 			    " (read-only compatible)" : "";
9260 
9261 			(void) printf("%-37s%s\n", fi->fi_uname, ro);
9262 			(void) printf("     %s\n", fi->fi_desc);
9263 		}
9264 		(void) printf("\n");
9265 
9266 		(void) printf(gettext("The following legacy versions are also "
9267 		    "supported:\n\n"));
9268 		(void) printf(gettext("VER  DESCRIPTION\n"));
9269 		(void) printf("---  -----------------------------------------"
9270 		    "---------------\n");
9271 		(void) printf(gettext(" 1   Initial ZFS version\n"));
9272 		(void) printf(gettext(" 2   Ditto blocks "
9273 		    "(replicated metadata)\n"));
9274 		(void) printf(gettext(" 3   Hot spares and double parity "
9275 		    "RAID-Z\n"));
9276 		(void) printf(gettext(" 4   zpool history\n"));
9277 		(void) printf(gettext(" 5   Compression using the gzip "
9278 		    "algorithm\n"));
9279 		(void) printf(gettext(" 6   bootfs pool property\n"));
9280 		(void) printf(gettext(" 7   Separate intent log devices\n"));
9281 		(void) printf(gettext(" 8   Delegated administration\n"));
9282 		(void) printf(gettext(" 9   refquota and refreservation "
9283 		    "properties\n"));
9284 		(void) printf(gettext(" 10  Cache devices\n"));
9285 		(void) printf(gettext(" 11  Improved scrub performance\n"));
9286 		(void) printf(gettext(" 12  Snapshot properties\n"));
9287 		(void) printf(gettext(" 13  snapused property\n"));
9288 		(void) printf(gettext(" 14  passthrough-x aclinherit\n"));
9289 		(void) printf(gettext(" 15  user/group space accounting\n"));
9290 		(void) printf(gettext(" 16  stmf property support\n"));
9291 		(void) printf(gettext(" 17  Triple-parity RAID-Z\n"));
9292 		(void) printf(gettext(" 18  Snapshot user holds\n"));
9293 		(void) printf(gettext(" 19  Log device removal\n"));
9294 		(void) printf(gettext(" 20  Compression using zle "
9295 		    "(zero-length encoding)\n"));
9296 		(void) printf(gettext(" 21  Deduplication\n"));
9297 		(void) printf(gettext(" 22  Received properties\n"));
9298 		(void) printf(gettext(" 23  Slim ZIL\n"));
9299 		(void) printf(gettext(" 24  System attributes\n"));
9300 		(void) printf(gettext(" 25  Improved scrub stats\n"));
9301 		(void) printf(gettext(" 26  Improved snapshot deletion "
9302 		    "performance\n"));
9303 		(void) printf(gettext(" 27  Improved snapshot creation "
9304 		    "performance\n"));
9305 		(void) printf(gettext(" 28  Multiple vdev replacements\n"));
9306 		(void) printf(gettext("\nFor more information on a particular "
9307 		    "version, including supported releases,\n"));
9308 		(void) printf(gettext("see the ZFS Administration Guide.\n\n"));
9309 	} else if (argc == 0 && upgradeall) {
9310 		cb.cb_first = B_TRUE;
9311 		ret = zpool_iter(g_zfs, upgrade_cb, &cb);
9312 		if (ret == 0 && cb.cb_first) {
9313 			if (cb.cb_version == SPA_VERSION) {
9314 				(void) printf(gettext("All pools are already "
9315 				    "formatted using feature flags.\n\n"));
9316 				(void) printf(gettext("Every feature flags "
9317 				    "pool already has all supported and "
9318 				    "requested features enabled.\n"));
9319 			} else {
9320 				(void) printf(gettext("All pools are already "
9321 				    "formatted with version %llu or higher.\n"),
9322 				    (u_longlong_t)cb.cb_version);
9323 			}
9324 		}
9325 	} else if (argc == 0) {
9326 		cb.cb_first = B_TRUE;
9327 		ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
9328 		assert(ret == 0);
9329 
9330 		if (cb.cb_first) {
9331 			(void) printf(gettext("All pools are formatted "
9332 			    "using feature flags.\n\n"));
9333 		} else {
9334 			(void) printf(gettext("\nUse 'zpool upgrade -v' "
9335 			    "for a list of available legacy versions.\n"));
9336 		}
9337 
9338 		cb.cb_first = B_TRUE;
9339 		ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
9340 		assert(ret == 0);
9341 
9342 		if (cb.cb_first) {
9343 			(void) printf(gettext("Every feature flags pool has "
9344 			    "all supported and requested features enabled.\n"));
9345 		} else {
9346 			(void) printf(gettext("\n"));
9347 		}
9348 	} else {
9349 		ret = for_each_pool(argc, argv, B_FALSE, NULL, ZFS_TYPE_POOL,
9350 		    B_FALSE, upgrade_one, &cb);
9351 	}
9352 
9353 	return (ret);
9354 }
9355 
9356 typedef struct hist_cbdata {
9357 	boolean_t first;
9358 	boolean_t longfmt;
9359 	boolean_t internal;
9360 } hist_cbdata_t;
9361 
9362 static void
9363 print_history_records(nvlist_t *nvhis, hist_cbdata_t *cb)
9364 {
9365 	nvlist_t **records;
9366 	uint_t numrecords;
9367 	int i;
9368 
9369 	verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
9370 	    &records, &numrecords) == 0);
9371 	for (i = 0; i < numrecords; i++) {
9372 		nvlist_t *rec = records[i];
9373 		char tbuf[64] = "";
9374 
9375 		if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
9376 			time_t tsec;
9377 			struct tm t;
9378 
9379 			tsec = fnvlist_lookup_uint64(records[i],
9380 			    ZPOOL_HIST_TIME);
9381 			(void) localtime_r(&tsec, &t);
9382 			(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
9383 		}
9384 
9385 		if (nvlist_exists(rec, ZPOOL_HIST_ELAPSED_NS)) {
9386 			uint64_t elapsed_ns = fnvlist_lookup_int64(records[i],
9387 			    ZPOOL_HIST_ELAPSED_NS);
9388 			(void) snprintf(tbuf + strlen(tbuf),
9389 			    sizeof (tbuf) - strlen(tbuf),
9390 			    " (%lldms)", (long long)elapsed_ns / 1000 / 1000);
9391 		}
9392 
9393 		if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
9394 			(void) printf("%s %s", tbuf,
9395 			    fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
9396 		} else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
9397 			int ievent =
9398 			    fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
9399 			if (!cb->internal)
9400 				continue;
9401 			if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
9402 				(void) printf("%s unrecognized record:\n",
9403 				    tbuf);
9404 				dump_nvlist(rec, 4);
9405 				continue;
9406 			}
9407 			(void) printf("%s [internal %s txg:%lld] %s", tbuf,
9408 			    zfs_history_event_names[ievent],
9409 			    (longlong_t)fnvlist_lookup_uint64(
9410 			    rec, ZPOOL_HIST_TXG),
9411 			    fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
9412 		} else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
9413 			if (!cb->internal)
9414 				continue;
9415 			(void) printf("%s [txg:%lld] %s", tbuf,
9416 			    (longlong_t)fnvlist_lookup_uint64(
9417 			    rec, ZPOOL_HIST_TXG),
9418 			    fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
9419 			if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
9420 				(void) printf(" %s (%llu)",
9421 				    fnvlist_lookup_string(rec,
9422 				    ZPOOL_HIST_DSNAME),
9423 				    (u_longlong_t)fnvlist_lookup_uint64(rec,
9424 				    ZPOOL_HIST_DSID));
9425 			}
9426 			(void) printf(" %s", fnvlist_lookup_string(rec,
9427 			    ZPOOL_HIST_INT_STR));
9428 		} else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
9429 			if (!cb->internal)
9430 				continue;
9431 			(void) printf("%s ioctl %s\n", tbuf,
9432 			    fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
9433 			if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
9434 				(void) printf("    input:\n");
9435 				dump_nvlist(fnvlist_lookup_nvlist(rec,
9436 				    ZPOOL_HIST_INPUT_NVL), 8);
9437 			}
9438 			if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
9439 				(void) printf("    output:\n");
9440 				dump_nvlist(fnvlist_lookup_nvlist(rec,
9441 				    ZPOOL_HIST_OUTPUT_NVL), 8);
9442 			}
9443 			if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_SIZE)) {
9444 				(void) printf("    output nvlist omitted; "
9445 				    "original size: %lldKB\n",
9446 				    (longlong_t)fnvlist_lookup_int64(rec,
9447 				    ZPOOL_HIST_OUTPUT_SIZE) / 1024);
9448 			}
9449 			if (nvlist_exists(rec, ZPOOL_HIST_ERRNO)) {
9450 				(void) printf("    errno: %lld\n",
9451 				    (longlong_t)fnvlist_lookup_int64(rec,
9452 				    ZPOOL_HIST_ERRNO));
9453 			}
9454 		} else {
9455 			if (!cb->internal)
9456 				continue;
9457 			(void) printf("%s unrecognized record:\n", tbuf);
9458 			dump_nvlist(rec, 4);
9459 		}
9460 
9461 		if (!cb->longfmt) {
9462 			(void) printf("\n");
9463 			continue;
9464 		}
9465 		(void) printf(" [");
9466 		if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
9467 			uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
9468 			struct passwd *pwd = getpwuid(who);
9469 			(void) printf("user %d ", (int)who);
9470 			if (pwd != NULL)
9471 				(void) printf("(%s) ", pwd->pw_name);
9472 		}
9473 		if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
9474 			(void) printf("on %s",
9475 			    fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
9476 		}
9477 		if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
9478 			(void) printf(":%s",
9479 			    fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
9480 		}
9481 
9482 		(void) printf("]");
9483 		(void) printf("\n");
9484 	}
9485 }
9486 
9487 /*
9488  * Print out the command history for a specific pool.
9489  */
9490 static int
9491 get_history_one(zpool_handle_t *zhp, void *data)
9492 {
9493 	nvlist_t *nvhis;
9494 	int ret;
9495 	hist_cbdata_t *cb = (hist_cbdata_t *)data;
9496 	uint64_t off = 0;
9497 	boolean_t eof = B_FALSE;
9498 
9499 	cb->first = B_FALSE;
9500 
9501 	(void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
9502 
9503 	while (!eof) {
9504 		if ((ret = zpool_get_history(zhp, &nvhis, &off, &eof)) != 0)
9505 			return (ret);
9506 
9507 		print_history_records(nvhis, cb);
9508 		nvlist_free(nvhis);
9509 	}
9510 	(void) printf("\n");
9511 
9512 	return (ret);
9513 }
9514 
9515 /*
9516  * zpool history <pool>
9517  *
9518  * Displays the history of commands that modified pools.
9519  */
9520 int
9521 zpool_do_history(int argc, char **argv)
9522 {
9523 	hist_cbdata_t cbdata = { 0 };
9524 	int ret;
9525 	int c;
9526 
9527 	cbdata.first = B_TRUE;
9528 	/* check options */
9529 	while ((c = getopt(argc, argv, "li")) != -1) {
9530 		switch (c) {
9531 		case 'l':
9532 			cbdata.longfmt = B_TRUE;
9533 			break;
9534 		case 'i':
9535 			cbdata.internal = B_TRUE;
9536 			break;
9537 		case '?':
9538 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
9539 			    optopt);
9540 			usage(B_FALSE);
9541 		}
9542 	}
9543 	argc -= optind;
9544 	argv += optind;
9545 
9546 	ret = for_each_pool(argc, argv, B_FALSE, NULL, ZFS_TYPE_POOL,
9547 	    B_FALSE, get_history_one, &cbdata);
9548 
9549 	if (argc == 0 && cbdata.first == B_TRUE) {
9550 		(void) fprintf(stderr, gettext("no pools available\n"));
9551 		return (0);
9552 	}
9553 
9554 	return (ret);
9555 }
9556 
9557 typedef struct ev_opts {
9558 	int verbose;
9559 	int scripted;
9560 	int follow;
9561 	int clear;
9562 	char poolname[ZFS_MAX_DATASET_NAME_LEN];
9563 } ev_opts_t;
9564 
9565 static void
9566 zpool_do_events_short(nvlist_t *nvl, ev_opts_t *opts)
9567 {
9568 	char ctime_str[26], str[32];
9569 	const char *ptr;
9570 	int64_t *tv;
9571 	uint_t n;
9572 
9573 	verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
9574 	memset(str, ' ', 32);
9575 	(void) ctime_r((const time_t *)&tv[0], ctime_str);
9576 	(void) memcpy(str, ctime_str+4,  6);		/* 'Jun 30' */
9577 	(void) memcpy(str+7, ctime_str+20, 4);		/* '1993' */
9578 	(void) memcpy(str+12, ctime_str+11, 8);		/* '21:49:08' */
9579 	(void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]); /* '.123456789' */
9580 	if (opts->scripted)
9581 		(void) printf(gettext("%s\t"), str);
9582 	else
9583 		(void) printf(gettext("%s "), str);
9584 
9585 	verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
9586 	(void) printf(gettext("%s\n"), ptr);
9587 }
9588 
9589 static void
9590 zpool_do_events_nvprint(nvlist_t *nvl, int depth)
9591 {
9592 	nvpair_t *nvp;
9593 
9594 	for (nvp = nvlist_next_nvpair(nvl, NULL);
9595 	    nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
9596 
9597 		data_type_t type = nvpair_type(nvp);
9598 		const char *name = nvpair_name(nvp);
9599 
9600 		boolean_t b;
9601 		uint8_t i8;
9602 		uint16_t i16;
9603 		uint32_t i32;
9604 		uint64_t i64;
9605 		const char *str;
9606 		nvlist_t *cnv;
9607 
9608 		printf(gettext("%*s%s = "), depth, "", name);
9609 
9610 		switch (type) {
9611 		case DATA_TYPE_BOOLEAN:
9612 			printf(gettext("%s"), "1");
9613 			break;
9614 
9615 		case DATA_TYPE_BOOLEAN_VALUE:
9616 			(void) nvpair_value_boolean_value(nvp, &b);
9617 			printf(gettext("%s"), b ? "1" : "0");
9618 			break;
9619 
9620 		case DATA_TYPE_BYTE:
9621 			(void) nvpair_value_byte(nvp, &i8);
9622 			printf(gettext("0x%x"), i8);
9623 			break;
9624 
9625 		case DATA_TYPE_INT8:
9626 			(void) nvpair_value_int8(nvp, (void *)&i8);
9627 			printf(gettext("0x%x"), i8);
9628 			break;
9629 
9630 		case DATA_TYPE_UINT8:
9631 			(void) nvpair_value_uint8(nvp, &i8);
9632 			printf(gettext("0x%x"), i8);
9633 			break;
9634 
9635 		case DATA_TYPE_INT16:
9636 			(void) nvpair_value_int16(nvp, (void *)&i16);
9637 			printf(gettext("0x%x"), i16);
9638 			break;
9639 
9640 		case DATA_TYPE_UINT16:
9641 			(void) nvpair_value_uint16(nvp, &i16);
9642 			printf(gettext("0x%x"), i16);
9643 			break;
9644 
9645 		case DATA_TYPE_INT32:
9646 			(void) nvpair_value_int32(nvp, (void *)&i32);
9647 			printf(gettext("0x%x"), i32);
9648 			break;
9649 
9650 		case DATA_TYPE_UINT32:
9651 			(void) nvpair_value_uint32(nvp, &i32);
9652 			printf(gettext("0x%x"), i32);
9653 			break;
9654 
9655 		case DATA_TYPE_INT64:
9656 			(void) nvpair_value_int64(nvp, (void *)&i64);
9657 			printf(gettext("0x%llx"), (u_longlong_t)i64);
9658 			break;
9659 
9660 		case DATA_TYPE_UINT64:
9661 			(void) nvpair_value_uint64(nvp, &i64);
9662 			/*
9663 			 * translate vdev state values to readable
9664 			 * strings to aide zpool events consumers
9665 			 */
9666 			if (strcmp(name,
9667 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE) == 0 ||
9668 			    strcmp(name,
9669 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE) == 0) {
9670 				printf(gettext("\"%s\" (0x%llx)"),
9671 				    zpool_state_to_name(i64, VDEV_AUX_NONE),
9672 				    (u_longlong_t)i64);
9673 			} else {
9674 				printf(gettext("0x%llx"), (u_longlong_t)i64);
9675 			}
9676 			break;
9677 
9678 		case DATA_TYPE_HRTIME:
9679 			(void) nvpair_value_hrtime(nvp, (void *)&i64);
9680 			printf(gettext("0x%llx"), (u_longlong_t)i64);
9681 			break;
9682 
9683 		case DATA_TYPE_STRING:
9684 			(void) nvpair_value_string(nvp, &str);
9685 			printf(gettext("\"%s\""), str ? str : "<NULL>");
9686 			break;
9687 
9688 		case DATA_TYPE_NVLIST:
9689 			printf(gettext("(embedded nvlist)\n"));
9690 			(void) nvpair_value_nvlist(nvp, &cnv);
9691 			zpool_do_events_nvprint(cnv, depth + 8);
9692 			printf(gettext("%*s(end %s)"), depth, "", name);
9693 			break;
9694 
9695 		case DATA_TYPE_NVLIST_ARRAY: {
9696 			nvlist_t **val;
9697 			uint_t i, nelem;
9698 
9699 			(void) nvpair_value_nvlist_array(nvp, &val, &nelem);
9700 			printf(gettext("(%d embedded nvlists)\n"), nelem);
9701 			for (i = 0; i < nelem; i++) {
9702 				printf(gettext("%*s%s[%d] = %s\n"),
9703 				    depth, "", name, i, "(embedded nvlist)");
9704 				zpool_do_events_nvprint(val[i], depth + 8);
9705 				printf(gettext("%*s(end %s[%i])\n"),
9706 				    depth, "", name, i);
9707 			}
9708 			printf(gettext("%*s(end %s)\n"), depth, "", name);
9709 			}
9710 			break;
9711 
9712 		case DATA_TYPE_INT8_ARRAY: {
9713 			int8_t *val;
9714 			uint_t i, nelem;
9715 
9716 			(void) nvpair_value_int8_array(nvp, &val, &nelem);
9717 			for (i = 0; i < nelem; i++)
9718 				printf(gettext("0x%x "), val[i]);
9719 
9720 			break;
9721 			}
9722 
9723 		case DATA_TYPE_UINT8_ARRAY: {
9724 			uint8_t *val;
9725 			uint_t i, nelem;
9726 
9727 			(void) nvpair_value_uint8_array(nvp, &val, &nelem);
9728 			for (i = 0; i < nelem; i++)
9729 				printf(gettext("0x%x "), val[i]);
9730 
9731 			break;
9732 			}
9733 
9734 		case DATA_TYPE_INT16_ARRAY: {
9735 			int16_t *val;
9736 			uint_t i, nelem;
9737 
9738 			(void) nvpair_value_int16_array(nvp, &val, &nelem);
9739 			for (i = 0; i < nelem; i++)
9740 				printf(gettext("0x%x "), val[i]);
9741 
9742 			break;
9743 			}
9744 
9745 		case DATA_TYPE_UINT16_ARRAY: {
9746 			uint16_t *val;
9747 			uint_t i, nelem;
9748 
9749 			(void) nvpair_value_uint16_array(nvp, &val, &nelem);
9750 			for (i = 0; i < nelem; i++)
9751 				printf(gettext("0x%x "), val[i]);
9752 
9753 			break;
9754 			}
9755 
9756 		case DATA_TYPE_INT32_ARRAY: {
9757 			int32_t *val;
9758 			uint_t i, nelem;
9759 
9760 			(void) nvpair_value_int32_array(nvp, &val, &nelem);
9761 			for (i = 0; i < nelem; i++)
9762 				printf(gettext("0x%x "), val[i]);
9763 
9764 			break;
9765 			}
9766 
9767 		case DATA_TYPE_UINT32_ARRAY: {
9768 			uint32_t *val;
9769 			uint_t i, nelem;
9770 
9771 			(void) nvpair_value_uint32_array(nvp, &val, &nelem);
9772 			for (i = 0; i < nelem; i++)
9773 				printf(gettext("0x%x "), val[i]);
9774 
9775 			break;
9776 			}
9777 
9778 		case DATA_TYPE_INT64_ARRAY: {
9779 			int64_t *val;
9780 			uint_t i, nelem;
9781 
9782 			(void) nvpair_value_int64_array(nvp, &val, &nelem);
9783 			for (i = 0; i < nelem; i++)
9784 				printf(gettext("0x%llx "),
9785 				    (u_longlong_t)val[i]);
9786 
9787 			break;
9788 			}
9789 
9790 		case DATA_TYPE_UINT64_ARRAY: {
9791 			uint64_t *val;
9792 			uint_t i, nelem;
9793 
9794 			(void) nvpair_value_uint64_array(nvp, &val, &nelem);
9795 			for (i = 0; i < nelem; i++)
9796 				printf(gettext("0x%llx "),
9797 				    (u_longlong_t)val[i]);
9798 
9799 			break;
9800 			}
9801 
9802 		case DATA_TYPE_STRING_ARRAY: {
9803 			const char **str;
9804 			uint_t i, nelem;
9805 
9806 			(void) nvpair_value_string_array(nvp, &str, &nelem);
9807 			for (i = 0; i < nelem; i++)
9808 				printf(gettext("\"%s\" "),
9809 				    str[i] ? str[i] : "<NULL>");
9810 
9811 			break;
9812 			}
9813 
9814 		case DATA_TYPE_BOOLEAN_ARRAY:
9815 		case DATA_TYPE_BYTE_ARRAY:
9816 		case DATA_TYPE_DOUBLE:
9817 		case DATA_TYPE_DONTCARE:
9818 		case DATA_TYPE_UNKNOWN:
9819 			printf(gettext("<unknown>"));
9820 			break;
9821 		}
9822 
9823 		printf(gettext("\n"));
9824 	}
9825 }
9826 
9827 static int
9828 zpool_do_events_next(ev_opts_t *opts)
9829 {
9830 	nvlist_t *nvl;
9831 	int zevent_fd, ret, dropped;
9832 	const char *pool;
9833 
9834 	zevent_fd = open(ZFS_DEV, O_RDWR);
9835 	VERIFY(zevent_fd >= 0);
9836 
9837 	if (!opts->scripted)
9838 		(void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
9839 
9840 	while (1) {
9841 		ret = zpool_events_next(g_zfs, &nvl, &dropped,
9842 		    (opts->follow ? ZEVENT_NONE : ZEVENT_NONBLOCK), zevent_fd);
9843 		if (ret || nvl == NULL)
9844 			break;
9845 
9846 		if (dropped > 0)
9847 			(void) printf(gettext("dropped %d events\n"), dropped);
9848 
9849 		if (strlen(opts->poolname) > 0 &&
9850 		    nvlist_lookup_string(nvl, FM_FMRI_ZFS_POOL, &pool) == 0 &&
9851 		    strcmp(opts->poolname, pool) != 0)
9852 			continue;
9853 
9854 		zpool_do_events_short(nvl, opts);
9855 
9856 		if (opts->verbose) {
9857 			zpool_do_events_nvprint(nvl, 8);
9858 			printf(gettext("\n"));
9859 		}
9860 		(void) fflush(stdout);
9861 
9862 		nvlist_free(nvl);
9863 	}
9864 
9865 	VERIFY(0 == close(zevent_fd));
9866 
9867 	return (ret);
9868 }
9869 
9870 static int
9871 zpool_do_events_clear(void)
9872 {
9873 	int count, ret;
9874 
9875 	ret = zpool_events_clear(g_zfs, &count);
9876 	if (!ret)
9877 		(void) printf(gettext("cleared %d events\n"), count);
9878 
9879 	return (ret);
9880 }
9881 
9882 /*
9883  * zpool events [-vHf [pool] | -c]
9884  *
9885  * Displays events logs by ZFS.
9886  */
9887 int
9888 zpool_do_events(int argc, char **argv)
9889 {
9890 	ev_opts_t opts = { 0 };
9891 	int ret;
9892 	int c;
9893 
9894 	/* check options */
9895 	while ((c = getopt(argc, argv, "vHfc")) != -1) {
9896 		switch (c) {
9897 		case 'v':
9898 			opts.verbose = 1;
9899 			break;
9900 		case 'H':
9901 			opts.scripted = 1;
9902 			break;
9903 		case 'f':
9904 			opts.follow = 1;
9905 			break;
9906 		case 'c':
9907 			opts.clear = 1;
9908 			break;
9909 		case '?':
9910 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
9911 			    optopt);
9912 			usage(B_FALSE);
9913 		}
9914 	}
9915 	argc -= optind;
9916 	argv += optind;
9917 
9918 	if (argc > 1) {
9919 		(void) fprintf(stderr, gettext("too many arguments\n"));
9920 		usage(B_FALSE);
9921 	} else if (argc == 1) {
9922 		(void) strlcpy(opts.poolname, argv[0], sizeof (opts.poolname));
9923 		if (!zfs_name_valid(opts.poolname, ZFS_TYPE_POOL)) {
9924 			(void) fprintf(stderr,
9925 			    gettext("invalid pool name '%s'\n"), opts.poolname);
9926 			usage(B_FALSE);
9927 		}
9928 	}
9929 
9930 	if ((argc == 1 || opts.verbose || opts.scripted || opts.follow) &&
9931 	    opts.clear) {
9932 		(void) fprintf(stderr,
9933 		    gettext("invalid options combined with -c\n"));
9934 		usage(B_FALSE);
9935 	}
9936 
9937 	if (opts.clear)
9938 		ret = zpool_do_events_clear();
9939 	else
9940 		ret = zpool_do_events_next(&opts);
9941 
9942 	return (ret);
9943 }
9944 
9945 static int
9946 get_callback_vdev(zpool_handle_t *zhp, char *vdevname, void *data)
9947 {
9948 	zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
9949 	char value[ZFS_MAXPROPLEN];
9950 	zprop_source_t srctype;
9951 
9952 	for (zprop_list_t *pl = cbp->cb_proplist; pl != NULL;
9953 	    pl = pl->pl_next) {
9954 		char *prop_name;
9955 		/*
9956 		 * If the first property is pool name, it is a special
9957 		 * placeholder that we can skip. This will also skip
9958 		 * over the name property when 'all' is specified.
9959 		 */
9960 		if (pl->pl_prop == ZPOOL_PROP_NAME &&
9961 		    pl == cbp->cb_proplist)
9962 			continue;
9963 
9964 		if (pl->pl_prop == ZPROP_INVAL) {
9965 			prop_name = pl->pl_user_prop;
9966 		} else {
9967 			prop_name = (char *)vdev_prop_to_name(pl->pl_prop);
9968 		}
9969 		if (zpool_get_vdev_prop(zhp, vdevname, pl->pl_prop,
9970 		    prop_name, value, sizeof (value), &srctype,
9971 		    cbp->cb_literal) == 0) {
9972 			zprop_print_one_property(vdevname, cbp, prop_name,
9973 			    value, srctype, NULL, NULL);
9974 		}
9975 	}
9976 
9977 	return (0);
9978 }
9979 
9980 static int
9981 get_callback_vdev_width_cb(void *zhp_data, nvlist_t *nv, void *data)
9982 {
9983 	zpool_handle_t *zhp = zhp_data;
9984 	zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
9985 	char *vdevname = zpool_vdev_name(g_zfs, zhp, nv,
9986 	    cbp->cb_vdevs.cb_name_flags);
9987 	int ret;
9988 
9989 	/* Adjust the column widths for the vdev properties */
9990 	ret = vdev_expand_proplist(zhp, vdevname, &cbp->cb_proplist);
9991 
9992 	return (ret);
9993 }
9994 
9995 static int
9996 get_callback_vdev_cb(void *zhp_data, nvlist_t *nv, void *data)
9997 {
9998 	zpool_handle_t *zhp = zhp_data;
9999 	zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
10000 	char *vdevname = zpool_vdev_name(g_zfs, zhp, nv,
10001 	    cbp->cb_vdevs.cb_name_flags);
10002 	int ret;
10003 
10004 	/* Display the properties */
10005 	ret = get_callback_vdev(zhp, vdevname, data);
10006 
10007 	return (ret);
10008 }
10009 
10010 static int
10011 get_callback(zpool_handle_t *zhp, void *data)
10012 {
10013 	zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
10014 	char value[MAXNAMELEN];
10015 	zprop_source_t srctype;
10016 	zprop_list_t *pl;
10017 	int vid;
10018 
10019 	if (cbp->cb_type == ZFS_TYPE_VDEV) {
10020 		if (strcmp(cbp->cb_vdevs.cb_names[0], "all-vdevs") == 0) {
10021 			for_each_vdev(zhp, get_callback_vdev_width_cb, data);
10022 			for_each_vdev(zhp, get_callback_vdev_cb, data);
10023 		} else {
10024 			/* Adjust column widths for vdev properties */
10025 			for (vid = 0; vid < cbp->cb_vdevs.cb_names_count;
10026 			    vid++) {
10027 				vdev_expand_proplist(zhp,
10028 				    cbp->cb_vdevs.cb_names[vid],
10029 				    &cbp->cb_proplist);
10030 			}
10031 			/* Display the properties */
10032 			for (vid = 0; vid < cbp->cb_vdevs.cb_names_count;
10033 			    vid++) {
10034 				get_callback_vdev(zhp,
10035 				    cbp->cb_vdevs.cb_names[vid], data);
10036 			}
10037 		}
10038 	} else {
10039 		assert(cbp->cb_type == ZFS_TYPE_POOL);
10040 		for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
10041 			/*
10042 			 * Skip the special fake placeholder. This will also
10043 			 * skip over the name property when 'all' is specified.
10044 			 */
10045 			if (pl->pl_prop == ZPOOL_PROP_NAME &&
10046 			    pl == cbp->cb_proplist)
10047 				continue;
10048 
10049 			if (pl->pl_prop == ZPROP_INVAL &&
10050 			    (zpool_prop_feature(pl->pl_user_prop) ||
10051 			    zpool_prop_unsupported(pl->pl_user_prop))) {
10052 				srctype = ZPROP_SRC_LOCAL;
10053 
10054 				if (zpool_prop_get_feature(zhp,
10055 				    pl->pl_user_prop, value,
10056 				    sizeof (value)) == 0) {
10057 					zprop_print_one_property(
10058 					    zpool_get_name(zhp), cbp,
10059 					    pl->pl_user_prop, value, srctype,
10060 					    NULL, NULL);
10061 				}
10062 			} else {
10063 				if (zpool_get_prop(zhp, pl->pl_prop, value,
10064 				    sizeof (value), &srctype,
10065 				    cbp->cb_literal) != 0)
10066 					continue;
10067 
10068 				zprop_print_one_property(zpool_get_name(zhp),
10069 				    cbp, zpool_prop_to_name(pl->pl_prop),
10070 				    value, srctype, NULL, NULL);
10071 			}
10072 		}
10073 	}
10074 
10075 	return (0);
10076 }
10077 
10078 /*
10079  * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
10080  *
10081  *	-H	Scripted mode.  Don't display headers, and separate properties
10082  *		by a single tab.
10083  *	-o	List of columns to display.  Defaults to
10084  *		"name,property,value,source".
10085  * 	-p	Display values in parsable (exact) format.
10086  *
10087  * Get properties of pools in the system. Output space statistics
10088  * for each one as well as other attributes.
10089  */
10090 int
10091 zpool_do_get(int argc, char **argv)
10092 {
10093 	zprop_get_cbdata_t cb = { 0 };
10094 	zprop_list_t fake_name = { 0 };
10095 	int ret;
10096 	int c, i;
10097 	char *propstr = NULL;
10098 
10099 	cb.cb_first = B_TRUE;
10100 
10101 	/*
10102 	 * Set up default columns and sources.
10103 	 */
10104 	cb.cb_sources = ZPROP_SRC_ALL;
10105 	cb.cb_columns[0] = GET_COL_NAME;
10106 	cb.cb_columns[1] = GET_COL_PROPERTY;
10107 	cb.cb_columns[2] = GET_COL_VALUE;
10108 	cb.cb_columns[3] = GET_COL_SOURCE;
10109 	cb.cb_type = ZFS_TYPE_POOL;
10110 	cb.cb_vdevs.cb_name_flags |= VDEV_NAME_TYPE_ID;
10111 	current_prop_type = cb.cb_type;
10112 
10113 	/* check options */
10114 	while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
10115 		switch (c) {
10116 		case 'p':
10117 			cb.cb_literal = B_TRUE;
10118 			break;
10119 		case 'H':
10120 			cb.cb_scripted = B_TRUE;
10121 			break;
10122 		case 'o':
10123 			memset(&cb.cb_columns, 0, sizeof (cb.cb_columns));
10124 			i = 0;
10125 
10126 			for (char *tok; (tok = strsep(&optarg, ",")); ) {
10127 				static const char *const col_opts[] =
10128 				{ "name", "property", "value", "source",
10129 				    "all" };
10130 				static const zfs_get_column_t col_cols[] =
10131 				{ GET_COL_NAME, GET_COL_PROPERTY, GET_COL_VALUE,
10132 				    GET_COL_SOURCE };
10133 
10134 				if (i == ZFS_GET_NCOLS - 1) {
10135 					(void) fprintf(stderr, gettext("too "
10136 					"many fields given to -o "
10137 					"option\n"));
10138 					usage(B_FALSE);
10139 				}
10140 
10141 				for (c = 0; c < ARRAY_SIZE(col_opts); ++c)
10142 					if (strcmp(tok, col_opts[c]) == 0)
10143 						goto found;
10144 
10145 				(void) fprintf(stderr,
10146 				    gettext("invalid column name '%s'\n"), tok);
10147 				usage(B_FALSE);
10148 
10149 found:
10150 				if (c >= 4) {
10151 					if (i > 0) {
10152 						(void) fprintf(stderr,
10153 						    gettext("\"all\" conflicts "
10154 						    "with specific fields "
10155 						    "given to -o option\n"));
10156 						usage(B_FALSE);
10157 					}
10158 
10159 					memcpy(cb.cb_columns, col_cols,
10160 					    sizeof (col_cols));
10161 					i = ZFS_GET_NCOLS - 1;
10162 				} else
10163 					cb.cb_columns[i++] = col_cols[c];
10164 			}
10165 			break;
10166 		case '?':
10167 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
10168 			    optopt);
10169 			usage(B_FALSE);
10170 		}
10171 	}
10172 
10173 	argc -= optind;
10174 	argv += optind;
10175 
10176 	if (argc < 1) {
10177 		(void) fprintf(stderr, gettext("missing property "
10178 		    "argument\n"));
10179 		usage(B_FALSE);
10180 	}
10181 
10182 	/* Properties list is needed later by zprop_get_list() */
10183 	propstr = argv[0];
10184 
10185 	argc--;
10186 	argv++;
10187 
10188 	if (argc == 0) {
10189 		/* No args, so just print the defaults. */
10190 	} else if (are_all_pools(argc, argv)) {
10191 		/* All the args are pool names */
10192 	} else if (are_all_pools(1, argv)) {
10193 		/* The first arg is a pool name */
10194 		if ((argc == 2 && strcmp(argv[1], "all-vdevs") == 0) ||
10195 		    are_vdevs_in_pool(argc - 1, argv + 1, argv[0],
10196 		    &cb.cb_vdevs)) {
10197 			/* ... and the rest are vdev names */
10198 			cb.cb_vdevs.cb_names = argv + 1;
10199 			cb.cb_vdevs.cb_names_count = argc - 1;
10200 			cb.cb_type = ZFS_TYPE_VDEV;
10201 			argc = 1; /* One pool to process */
10202 		} else {
10203 			fprintf(stderr, gettext("Expected a list of vdevs in"
10204 			    " \"%s\", but got:\n"), argv[0]);
10205 			error_list_unresolved_vdevs(argc - 1, argv + 1,
10206 			    argv[0], &cb.cb_vdevs);
10207 			fprintf(stderr, "\n");
10208 			usage(B_FALSE);
10209 			return (1);
10210 		}
10211 	} else {
10212 		/*
10213 		 * The first arg isn't a pool name,
10214 		 */
10215 		fprintf(stderr, gettext("missing pool name.\n"));
10216 		fprintf(stderr, "\n");
10217 		usage(B_FALSE);
10218 		return (1);
10219 	}
10220 
10221 	if (zprop_get_list(g_zfs, propstr, &cb.cb_proplist,
10222 	    cb.cb_type) != 0) {
10223 		/* Use correct list of valid properties (pool or vdev) */
10224 		current_prop_type = cb.cb_type;
10225 		usage(B_FALSE);
10226 	}
10227 
10228 	if (cb.cb_proplist != NULL) {
10229 		fake_name.pl_prop = ZPOOL_PROP_NAME;
10230 		fake_name.pl_width = strlen(gettext("NAME"));
10231 		fake_name.pl_next = cb.cb_proplist;
10232 		cb.cb_proplist = &fake_name;
10233 	}
10234 
10235 	ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist, cb.cb_type,
10236 	    cb.cb_literal, get_callback, &cb);
10237 
10238 	if (cb.cb_proplist == &fake_name)
10239 		zprop_free_list(fake_name.pl_next);
10240 	else
10241 		zprop_free_list(cb.cb_proplist);
10242 
10243 	return (ret);
10244 }
10245 
10246 typedef struct set_cbdata {
10247 	char *cb_propname;
10248 	char *cb_value;
10249 	zfs_type_t cb_type;
10250 	vdev_cbdata_t cb_vdevs;
10251 	boolean_t cb_any_successful;
10252 } set_cbdata_t;
10253 
10254 static int
10255 set_pool_callback(zpool_handle_t *zhp, set_cbdata_t *cb)
10256 {
10257 	int error;
10258 
10259 	/* Check if we have out-of-bounds features */
10260 	if (strcmp(cb->cb_propname, ZPOOL_CONFIG_COMPATIBILITY) == 0) {
10261 		boolean_t features[SPA_FEATURES];
10262 		if (zpool_do_load_compat(cb->cb_value, features) !=
10263 		    ZPOOL_COMPATIBILITY_OK)
10264 			return (-1);
10265 
10266 		nvlist_t *enabled = zpool_get_features(zhp);
10267 		spa_feature_t i;
10268 		for (i = 0; i < SPA_FEATURES; i++) {
10269 			const char *fguid = spa_feature_table[i].fi_guid;
10270 			if (nvlist_exists(enabled, fguid) && !features[i])
10271 				break;
10272 		}
10273 		if (i < SPA_FEATURES)
10274 			(void) fprintf(stderr, gettext("Warning: one or "
10275 			    "more features already enabled on pool '%s'\n"
10276 			    "are not present in this compatibility set.\n"),
10277 			    zpool_get_name(zhp));
10278 	}
10279 
10280 	/* if we're setting a feature, check it's in compatibility set */
10281 	if (zpool_prop_feature(cb->cb_propname) &&
10282 	    strcmp(cb->cb_value, ZFS_FEATURE_ENABLED) == 0) {
10283 		char *fname = strchr(cb->cb_propname, '@') + 1;
10284 		spa_feature_t f;
10285 
10286 		if (zfeature_lookup_name(fname, &f) == 0) {
10287 			char compat[ZFS_MAXPROPLEN];
10288 			if (zpool_get_prop(zhp, ZPOOL_PROP_COMPATIBILITY,
10289 			    compat, ZFS_MAXPROPLEN, NULL, B_FALSE) != 0)
10290 				compat[0] = '\0';
10291 
10292 			boolean_t features[SPA_FEATURES];
10293 			if (zpool_do_load_compat(compat, features) !=
10294 			    ZPOOL_COMPATIBILITY_OK) {
10295 				(void) fprintf(stderr, gettext("Error: "
10296 				    "cannot enable feature '%s' on pool '%s'\n"
10297 				    "because the pool's 'compatibility' "
10298 				    "property cannot be parsed.\n"),
10299 				    fname, zpool_get_name(zhp));
10300 				return (-1);
10301 			}
10302 
10303 			if (!features[f]) {
10304 				(void) fprintf(stderr, gettext("Error: "
10305 				    "cannot enable feature '%s' on pool '%s'\n"
10306 				    "as it is not specified in this pool's "
10307 				    "current compatibility set.\n"
10308 				    "Consider setting 'compatibility' to a "
10309 				    "less restrictive set, or to 'off'.\n"),
10310 				    fname, zpool_get_name(zhp));
10311 				return (-1);
10312 			}
10313 		}
10314 	}
10315 
10316 	error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
10317 
10318 	return (error);
10319 }
10320 
10321 static int
10322 set_callback(zpool_handle_t *zhp, void *data)
10323 {
10324 	int error;
10325 	set_cbdata_t *cb = (set_cbdata_t *)data;
10326 
10327 	if (cb->cb_type == ZFS_TYPE_VDEV) {
10328 		error = zpool_set_vdev_prop(zhp, *cb->cb_vdevs.cb_names,
10329 		    cb->cb_propname, cb->cb_value);
10330 	} else {
10331 		assert(cb->cb_type == ZFS_TYPE_POOL);
10332 		error = set_pool_callback(zhp, cb);
10333 	}
10334 
10335 	cb->cb_any_successful = !error;
10336 	return (error);
10337 }
10338 
10339 int
10340 zpool_do_set(int argc, char **argv)
10341 {
10342 	set_cbdata_t cb = { 0 };
10343 	int error;
10344 
10345 	current_prop_type = ZFS_TYPE_POOL;
10346 	if (argc > 1 && argv[1][0] == '-') {
10347 		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
10348 		    argv[1][1]);
10349 		usage(B_FALSE);
10350 	}
10351 
10352 	if (argc < 2) {
10353 		(void) fprintf(stderr, gettext("missing property=value "
10354 		    "argument\n"));
10355 		usage(B_FALSE);
10356 	}
10357 
10358 	if (argc < 3) {
10359 		(void) fprintf(stderr, gettext("missing pool name\n"));
10360 		usage(B_FALSE);
10361 	}
10362 
10363 	if (argc > 4) {
10364 		(void) fprintf(stderr, gettext("too many pool names\n"));
10365 		usage(B_FALSE);
10366 	}
10367 
10368 	cb.cb_propname = argv[1];
10369 	cb.cb_type = ZFS_TYPE_POOL;
10370 	cb.cb_vdevs.cb_name_flags |= VDEV_NAME_TYPE_ID;
10371 	cb.cb_value = strchr(cb.cb_propname, '=');
10372 	if (cb.cb_value == NULL) {
10373 		(void) fprintf(stderr, gettext("missing value in "
10374 		    "property=value argument\n"));
10375 		usage(B_FALSE);
10376 	}
10377 
10378 	*(cb.cb_value) = '\0';
10379 	cb.cb_value++;
10380 	argc -= 2;
10381 	argv += 2;
10382 
10383 	/* argv[0] is pool name */
10384 	if (!is_pool(argv[0])) {
10385 		(void) fprintf(stderr,
10386 		    gettext("cannot open '%s': is not a pool\n"), argv[0]);
10387 		return (EINVAL);
10388 	}
10389 
10390 	/* argv[1], when supplied, is vdev name */
10391 	if (argc == 2) {
10392 		if (!are_vdevs_in_pool(1, argv + 1, argv[0], &cb.cb_vdevs)) {
10393 			(void) fprintf(stderr, gettext(
10394 			    "cannot find '%s' in '%s': device not in pool\n"),
10395 			    argv[1], argv[0]);
10396 			return (EINVAL);
10397 		}
10398 		cb.cb_vdevs.cb_names = argv + 1;
10399 		cb.cb_vdevs.cb_names_count = 1;
10400 		cb.cb_type = ZFS_TYPE_VDEV;
10401 	}
10402 
10403 	error = for_each_pool(1, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
10404 	    B_FALSE, set_callback, &cb);
10405 
10406 	return (error);
10407 }
10408 
10409 /* Add up the total number of bytes left to initialize/trim across all vdevs */
10410 static uint64_t
10411 vdev_activity_remaining(nvlist_t *nv, zpool_wait_activity_t activity)
10412 {
10413 	uint64_t bytes_remaining;
10414 	nvlist_t **child;
10415 	uint_t c, children;
10416 	vdev_stat_t *vs;
10417 
10418 	assert(activity == ZPOOL_WAIT_INITIALIZE ||
10419 	    activity == ZPOOL_WAIT_TRIM);
10420 
10421 	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
10422 	    (uint64_t **)&vs, &c) == 0);
10423 
10424 	if (activity == ZPOOL_WAIT_INITIALIZE &&
10425 	    vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE)
10426 		bytes_remaining = vs->vs_initialize_bytes_est -
10427 		    vs->vs_initialize_bytes_done;
10428 	else if (activity == ZPOOL_WAIT_TRIM &&
10429 	    vs->vs_trim_state == VDEV_TRIM_ACTIVE)
10430 		bytes_remaining = vs->vs_trim_bytes_est -
10431 		    vs->vs_trim_bytes_done;
10432 	else
10433 		bytes_remaining = 0;
10434 
10435 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
10436 	    &child, &children) != 0)
10437 		children = 0;
10438 
10439 	for (c = 0; c < children; c++)
10440 		bytes_remaining += vdev_activity_remaining(child[c], activity);
10441 
10442 	return (bytes_remaining);
10443 }
10444 
10445 /* Add up the total number of bytes left to rebuild across top-level vdevs */
10446 static uint64_t
10447 vdev_activity_top_remaining(nvlist_t *nv)
10448 {
10449 	uint64_t bytes_remaining = 0;
10450 	nvlist_t **child;
10451 	uint_t children;
10452 	int error;
10453 
10454 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
10455 	    &child, &children) != 0)
10456 		children = 0;
10457 
10458 	for (uint_t c = 0; c < children; c++) {
10459 		vdev_rebuild_stat_t *vrs;
10460 		uint_t i;
10461 
10462 		error = nvlist_lookup_uint64_array(child[c],
10463 		    ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i);
10464 		if (error == 0) {
10465 			if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) {
10466 				bytes_remaining += (vrs->vrs_bytes_est -
10467 				    vrs->vrs_bytes_rebuilt);
10468 			}
10469 		}
10470 	}
10471 
10472 	return (bytes_remaining);
10473 }
10474 
10475 /* Whether any vdevs are 'spare' or 'replacing' vdevs */
10476 static boolean_t
10477 vdev_any_spare_replacing(nvlist_t *nv)
10478 {
10479 	nvlist_t **child;
10480 	uint_t c, children;
10481 	const char *vdev_type;
10482 
10483 	(void) nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &vdev_type);
10484 
10485 	if (strcmp(vdev_type, VDEV_TYPE_REPLACING) == 0 ||
10486 	    strcmp(vdev_type, VDEV_TYPE_SPARE) == 0 ||
10487 	    strcmp(vdev_type, VDEV_TYPE_DRAID_SPARE) == 0) {
10488 		return (B_TRUE);
10489 	}
10490 
10491 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
10492 	    &child, &children) != 0)
10493 		children = 0;
10494 
10495 	for (c = 0; c < children; c++) {
10496 		if (vdev_any_spare_replacing(child[c]))
10497 			return (B_TRUE);
10498 	}
10499 
10500 	return (B_FALSE);
10501 }
10502 
10503 typedef struct wait_data {
10504 	char *wd_poolname;
10505 	boolean_t wd_scripted;
10506 	boolean_t wd_exact;
10507 	boolean_t wd_headers_once;
10508 	boolean_t wd_should_exit;
10509 	/* Which activities to wait for */
10510 	boolean_t wd_enabled[ZPOOL_WAIT_NUM_ACTIVITIES];
10511 	float wd_interval;
10512 	pthread_cond_t wd_cv;
10513 	pthread_mutex_t wd_mutex;
10514 } wait_data_t;
10515 
10516 /*
10517  * Print to stdout a single line, containing one column for each activity that
10518  * we are waiting for specifying how many bytes of work are left for that
10519  * activity.
10520  */
10521 static void
10522 print_wait_status_row(wait_data_t *wd, zpool_handle_t *zhp, int row)
10523 {
10524 	nvlist_t *config, *nvroot;
10525 	uint_t c;
10526 	int i;
10527 	pool_checkpoint_stat_t *pcs = NULL;
10528 	pool_scan_stat_t *pss = NULL;
10529 	pool_removal_stat_t *prs = NULL;
10530 	const char *const headers[] = {"DISCARD", "FREE", "INITIALIZE",
10531 	    "REPLACE", "REMOVE", "RESILVER", "SCRUB", "TRIM"};
10532 	int col_widths[ZPOOL_WAIT_NUM_ACTIVITIES];
10533 
10534 	/* Calculate the width of each column */
10535 	for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) {
10536 		/*
10537 		 * Make sure we have enough space in the col for pretty-printed
10538 		 * numbers and for the column header, and then leave a couple
10539 		 * spaces between cols for readability.
10540 		 */
10541 		col_widths[i] = MAX(strlen(headers[i]), 6) + 2;
10542 	}
10543 
10544 	/* Print header if appropriate */
10545 	int term_height = terminal_height();
10546 	boolean_t reprint_header = (!wd->wd_headers_once && term_height > 0 &&
10547 	    row % (term_height-1) == 0);
10548 	if (!wd->wd_scripted && (row == 0 || reprint_header)) {
10549 		for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) {
10550 			if (wd->wd_enabled[i])
10551 				(void) printf("%*s", col_widths[i], headers[i]);
10552 		}
10553 		(void) fputc('\n', stdout);
10554 	}
10555 
10556 	/* Bytes of work remaining in each activity */
10557 	int64_t bytes_rem[ZPOOL_WAIT_NUM_ACTIVITIES] = {0};
10558 
10559 	bytes_rem[ZPOOL_WAIT_FREE] =
10560 	    zpool_get_prop_int(zhp, ZPOOL_PROP_FREEING, NULL);
10561 
10562 	config = zpool_get_config(zhp, NULL);
10563 	nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
10564 
10565 	(void) nvlist_lookup_uint64_array(nvroot,
10566 	    ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
10567 	if (pcs != NULL && pcs->pcs_state == CS_CHECKPOINT_DISCARDING)
10568 		bytes_rem[ZPOOL_WAIT_CKPT_DISCARD] = pcs->pcs_space;
10569 
10570 	(void) nvlist_lookup_uint64_array(nvroot,
10571 	    ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c);
10572 	if (prs != NULL && prs->prs_state == DSS_SCANNING)
10573 		bytes_rem[ZPOOL_WAIT_REMOVE] = prs->prs_to_copy -
10574 		    prs->prs_copied;
10575 
10576 	(void) nvlist_lookup_uint64_array(nvroot,
10577 	    ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&pss, &c);
10578 	if (pss != NULL && pss->pss_state == DSS_SCANNING &&
10579 	    pss->pss_pass_scrub_pause == 0) {
10580 		int64_t rem = pss->pss_to_examine - pss->pss_issued;
10581 		if (pss->pss_func == POOL_SCAN_SCRUB)
10582 			bytes_rem[ZPOOL_WAIT_SCRUB] = rem;
10583 		else
10584 			bytes_rem[ZPOOL_WAIT_RESILVER] = rem;
10585 	} else if (check_rebuilding(nvroot, NULL)) {
10586 		bytes_rem[ZPOOL_WAIT_RESILVER] =
10587 		    vdev_activity_top_remaining(nvroot);
10588 	}
10589 
10590 	bytes_rem[ZPOOL_WAIT_INITIALIZE] =
10591 	    vdev_activity_remaining(nvroot, ZPOOL_WAIT_INITIALIZE);
10592 	bytes_rem[ZPOOL_WAIT_TRIM] =
10593 	    vdev_activity_remaining(nvroot, ZPOOL_WAIT_TRIM);
10594 
10595 	/*
10596 	 * A replace finishes after resilvering finishes, so the amount of work
10597 	 * left for a replace is the same as for resilvering.
10598 	 *
10599 	 * It isn't quite correct to say that if we have any 'spare' or
10600 	 * 'replacing' vdevs and a resilver is happening, then a replace is in
10601 	 * progress, like we do here. When a hot spare is used, the faulted vdev
10602 	 * is not removed after the hot spare is resilvered, so parent 'spare'
10603 	 * vdev is not removed either. So we could have a 'spare' vdev, but be
10604 	 * resilvering for a different reason. However, we use it as a heuristic
10605 	 * because we don't have access to the DTLs, which could tell us whether
10606 	 * or not we have really finished resilvering a hot spare.
10607 	 */
10608 	if (vdev_any_spare_replacing(nvroot))
10609 		bytes_rem[ZPOOL_WAIT_REPLACE] =  bytes_rem[ZPOOL_WAIT_RESILVER];
10610 
10611 	if (timestamp_fmt != NODATE)
10612 		print_timestamp(timestamp_fmt);
10613 
10614 	for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) {
10615 		char buf[64];
10616 		if (!wd->wd_enabled[i])
10617 			continue;
10618 
10619 		if (wd->wd_exact)
10620 			(void) snprintf(buf, sizeof (buf), "%" PRIi64,
10621 			    bytes_rem[i]);
10622 		else
10623 			zfs_nicenum(bytes_rem[i], buf, sizeof (buf));
10624 
10625 		if (wd->wd_scripted)
10626 			(void) printf(i == 0 ? "%s" : "\t%s", buf);
10627 		else
10628 			(void) printf(" %*s", col_widths[i] - 1, buf);
10629 	}
10630 	(void) printf("\n");
10631 	(void) fflush(stdout);
10632 }
10633 
10634 static void *
10635 wait_status_thread(void *arg)
10636 {
10637 	wait_data_t *wd = (wait_data_t *)arg;
10638 	zpool_handle_t *zhp;
10639 
10640 	if ((zhp = zpool_open(g_zfs, wd->wd_poolname)) == NULL)
10641 		return (void *)(1);
10642 
10643 	for (int row = 0; ; row++) {
10644 		boolean_t missing;
10645 		struct timespec timeout;
10646 		int ret = 0;
10647 		(void) clock_gettime(CLOCK_REALTIME, &timeout);
10648 
10649 		if (zpool_refresh_stats(zhp, &missing) != 0 || missing ||
10650 		    zpool_props_refresh(zhp) != 0) {
10651 			zpool_close(zhp);
10652 			return (void *)(uintptr_t)(missing ? 0 : 1);
10653 		}
10654 
10655 		print_wait_status_row(wd, zhp, row);
10656 
10657 		timeout.tv_sec += floor(wd->wd_interval);
10658 		long nanos = timeout.tv_nsec +
10659 		    (wd->wd_interval - floor(wd->wd_interval)) * NANOSEC;
10660 		if (nanos >= NANOSEC) {
10661 			timeout.tv_sec++;
10662 			timeout.tv_nsec = nanos - NANOSEC;
10663 		} else {
10664 			timeout.tv_nsec = nanos;
10665 		}
10666 		pthread_mutex_lock(&wd->wd_mutex);
10667 		if (!wd->wd_should_exit)
10668 			ret = pthread_cond_timedwait(&wd->wd_cv, &wd->wd_mutex,
10669 			    &timeout);
10670 		pthread_mutex_unlock(&wd->wd_mutex);
10671 		if (ret == 0) {
10672 			break; /* signaled by main thread */
10673 		} else if (ret != ETIMEDOUT) {
10674 			(void) fprintf(stderr, gettext("pthread_cond_timedwait "
10675 			    "failed: %s\n"), strerror(ret));
10676 			zpool_close(zhp);
10677 			return (void *)(uintptr_t)(1);
10678 		}
10679 	}
10680 
10681 	zpool_close(zhp);
10682 	return (void *)(0);
10683 }
10684 
10685 int
10686 zpool_do_wait(int argc, char **argv)
10687 {
10688 	boolean_t verbose = B_FALSE;
10689 	int c, i;
10690 	unsigned long count;
10691 	pthread_t status_thr;
10692 	int error = 0;
10693 	zpool_handle_t *zhp;
10694 
10695 	wait_data_t wd;
10696 	wd.wd_scripted = B_FALSE;
10697 	wd.wd_exact = B_FALSE;
10698 	wd.wd_headers_once = B_FALSE;
10699 	wd.wd_should_exit = B_FALSE;
10700 
10701 	pthread_mutex_init(&wd.wd_mutex, NULL);
10702 	pthread_cond_init(&wd.wd_cv, NULL);
10703 
10704 	/* By default, wait for all types of activity. */
10705 	for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++)
10706 		wd.wd_enabled[i] = B_TRUE;
10707 
10708 	while ((c = getopt(argc, argv, "HpT:t:")) != -1) {
10709 		switch (c) {
10710 		case 'H':
10711 			wd.wd_scripted = B_TRUE;
10712 			break;
10713 		case 'n':
10714 			wd.wd_headers_once = B_TRUE;
10715 			break;
10716 		case 'p':
10717 			wd.wd_exact = B_TRUE;
10718 			break;
10719 		case 'T':
10720 			get_timestamp_arg(*optarg);
10721 			break;
10722 		case 't':
10723 			/* Reset activities array */
10724 			memset(&wd.wd_enabled, 0, sizeof (wd.wd_enabled));
10725 
10726 			for (char *tok; (tok = strsep(&optarg, ",")); ) {
10727 				static const char *const col_opts[] = {
10728 				    "discard", "free", "initialize", "replace",
10729 				    "remove", "resilver", "scrub", "trim" };
10730 
10731 				for (i = 0; i < ARRAY_SIZE(col_opts); ++i)
10732 					if (strcmp(tok, col_opts[i]) == 0) {
10733 						wd.wd_enabled[i] = B_TRUE;
10734 						goto found;
10735 					}
10736 
10737 				(void) fprintf(stderr,
10738 				    gettext("invalid activity '%s'\n"), tok);
10739 				usage(B_FALSE);
10740 found:;
10741 			}
10742 			break;
10743 		case '?':
10744 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
10745 			    optopt);
10746 			usage(B_FALSE);
10747 		}
10748 	}
10749 
10750 	argc -= optind;
10751 	argv += optind;
10752 
10753 	get_interval_count(&argc, argv, &wd.wd_interval, &count);
10754 	if (count != 0) {
10755 		/* This subcmd only accepts an interval, not a count */
10756 		(void) fprintf(stderr, gettext("too many arguments\n"));
10757 		usage(B_FALSE);
10758 	}
10759 
10760 	if (wd.wd_interval != 0)
10761 		verbose = B_TRUE;
10762 
10763 	if (argc < 1) {
10764 		(void) fprintf(stderr, gettext("missing 'pool' argument\n"));
10765 		usage(B_FALSE);
10766 	}
10767 	if (argc > 1) {
10768 		(void) fprintf(stderr, gettext("too many arguments\n"));
10769 		usage(B_FALSE);
10770 	}
10771 
10772 	wd.wd_poolname = argv[0];
10773 
10774 	if ((zhp = zpool_open(g_zfs, wd.wd_poolname)) == NULL)
10775 		return (1);
10776 
10777 	if (verbose) {
10778 		/*
10779 		 * We use a separate thread for printing status updates because
10780 		 * the main thread will call lzc_wait(), which blocks as long
10781 		 * as an activity is in progress, which can be a long time.
10782 		 */
10783 		if (pthread_create(&status_thr, NULL, wait_status_thread, &wd)
10784 		    != 0) {
10785 			(void) fprintf(stderr, gettext("failed to create status"
10786 			    "thread: %s\n"), strerror(errno));
10787 			zpool_close(zhp);
10788 			return (1);
10789 		}
10790 	}
10791 
10792 	/*
10793 	 * Loop over all activities that we are supposed to wait for until none
10794 	 * of them are in progress. Note that this means we can end up waiting
10795 	 * for more activities to complete than just those that were in progress
10796 	 * when we began waiting; if an activity we are interested in begins
10797 	 * while we are waiting for another activity, we will wait for both to
10798 	 * complete before exiting.
10799 	 */
10800 	for (;;) {
10801 		boolean_t missing = B_FALSE;
10802 		boolean_t any_waited = B_FALSE;
10803 
10804 		for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) {
10805 			boolean_t waited;
10806 
10807 			if (!wd.wd_enabled[i])
10808 				continue;
10809 
10810 			error = zpool_wait_status(zhp, i, &missing, &waited);
10811 			if (error != 0 || missing)
10812 				break;
10813 
10814 			any_waited = (any_waited || waited);
10815 		}
10816 
10817 		if (error != 0 || missing || !any_waited)
10818 			break;
10819 	}
10820 
10821 	zpool_close(zhp);
10822 
10823 	if (verbose) {
10824 		uintptr_t status;
10825 		pthread_mutex_lock(&wd.wd_mutex);
10826 		wd.wd_should_exit = B_TRUE;
10827 		pthread_cond_signal(&wd.wd_cv);
10828 		pthread_mutex_unlock(&wd.wd_mutex);
10829 		(void) pthread_join(status_thr, (void *)&status);
10830 		if (status != 0)
10831 			error = status;
10832 	}
10833 
10834 	pthread_mutex_destroy(&wd.wd_mutex);
10835 	pthread_cond_destroy(&wd.wd_cv);
10836 	return (error);
10837 }
10838 
10839 static int
10840 find_command_idx(const char *command, int *idx)
10841 {
10842 	for (int i = 0; i < NCOMMAND; ++i) {
10843 		if (command_table[i].name == NULL)
10844 			continue;
10845 
10846 		if (strcmp(command, command_table[i].name) == 0) {
10847 			*idx = i;
10848 			return (0);
10849 		}
10850 	}
10851 	return (1);
10852 }
10853 
10854 /*
10855  * Display version message
10856  */
10857 static int
10858 zpool_do_version(int argc, char **argv)
10859 {
10860 	(void) argc, (void) argv;
10861 	return (zfs_version_print() != 0);
10862 }
10863 
10864 /*
10865  * Do zpool_load_compat() and print error message on failure
10866  */
10867 static zpool_compat_status_t
10868 zpool_do_load_compat(const char *compat, boolean_t *list)
10869 {
10870 	char report[1024];
10871 
10872 	zpool_compat_status_t ret;
10873 
10874 	ret = zpool_load_compat(compat, list, report, 1024);
10875 	switch (ret) {
10876 
10877 	case ZPOOL_COMPATIBILITY_OK:
10878 		break;
10879 
10880 	case ZPOOL_COMPATIBILITY_NOFILES:
10881 	case ZPOOL_COMPATIBILITY_BADFILE:
10882 	case ZPOOL_COMPATIBILITY_BADTOKEN:
10883 		(void) fprintf(stderr, "Error: %s\n", report);
10884 		break;
10885 
10886 	case ZPOOL_COMPATIBILITY_WARNTOKEN:
10887 		(void) fprintf(stderr, "Warning: %s\n", report);
10888 		ret = ZPOOL_COMPATIBILITY_OK;
10889 		break;
10890 	}
10891 	return (ret);
10892 }
10893 
10894 int
10895 main(int argc, char **argv)
10896 {
10897 	int ret = 0;
10898 	int i = 0;
10899 	char *cmdname;
10900 	char **newargv;
10901 
10902 	(void) setlocale(LC_ALL, "");
10903 	(void) setlocale(LC_NUMERIC, "C");
10904 	(void) textdomain(TEXT_DOMAIN);
10905 	srand(time(NULL));
10906 
10907 	opterr = 0;
10908 
10909 	/*
10910 	 * Make sure the user has specified some command.
10911 	 */
10912 	if (argc < 2) {
10913 		(void) fprintf(stderr, gettext("missing command\n"));
10914 		usage(B_FALSE);
10915 	}
10916 
10917 	cmdname = argv[1];
10918 
10919 	/*
10920 	 * Special case '-?'
10921 	 */
10922 	if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0)
10923 		usage(B_TRUE);
10924 
10925 	/*
10926 	 * Special case '-V|--version'
10927 	 */
10928 	if ((strcmp(cmdname, "-V") == 0) || (strcmp(cmdname, "--version") == 0))
10929 		return (zpool_do_version(argc, argv));
10930 
10931 	if ((g_zfs = libzfs_init()) == NULL) {
10932 		(void) fprintf(stderr, "%s\n", libzfs_error_init(errno));
10933 		return (1);
10934 	}
10935 
10936 	libzfs_print_on_error(g_zfs, B_TRUE);
10937 
10938 	zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
10939 
10940 	/*
10941 	 * Many commands modify input strings for string parsing reasons.
10942 	 * We create a copy to protect the original argv.
10943 	 */
10944 	newargv = safe_malloc((argc + 1) * sizeof (newargv[0]));
10945 	for (i = 0; i < argc; i++)
10946 		newargv[i] = strdup(argv[i]);
10947 	newargv[argc] = NULL;
10948 
10949 	/*
10950 	 * Run the appropriate command.
10951 	 */
10952 	if (find_command_idx(cmdname, &i) == 0) {
10953 		current_command = &command_table[i];
10954 		ret = command_table[i].func(argc - 1, newargv + 1);
10955 	} else if (strchr(cmdname, '=')) {
10956 		verify(find_command_idx("set", &i) == 0);
10957 		current_command = &command_table[i];
10958 		ret = command_table[i].func(argc, newargv);
10959 	} else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
10960 		/*
10961 		 * 'freeze' is a vile debugging abomination, so we treat
10962 		 * it as such.
10963 		 */
10964 		zfs_cmd_t zc = {"\0"};
10965 
10966 		(void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name));
10967 		ret = zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc);
10968 		if (ret != 0) {
10969 			(void) fprintf(stderr,
10970 			gettext("failed to freeze pool: %d\n"), errno);
10971 			ret = 1;
10972 		}
10973 
10974 		log_history = 0;
10975 	} else {
10976 		(void) fprintf(stderr, gettext("unrecognized "
10977 		    "command '%s'\n"), cmdname);
10978 		usage(B_FALSE);
10979 		ret = 1;
10980 	}
10981 
10982 	for (i = 0; i < argc; i++)
10983 		free(newargv[i]);
10984 	free(newargv);
10985 
10986 	if (ret == 0 && log_history)
10987 		(void) zpool_log_history(g_zfs, history_str);
10988 
10989 	libzfs_fini(g_zfs);
10990 
10991 	/*
10992 	 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
10993 	 * for the purposes of running ::findleaks.
10994 	 */
10995 	if (getenv("ZFS_ABORT") != NULL) {
10996 		(void) printf("dumping core by request\n");
10997 		abort();
10998 	}
10999 
11000 	return (ret);
11001 }
11002