xref: /titanic_53/usr/src/cmd/zpool/zpool_main.c (revision 8b713775314bbbf24edd503b4869342d8711ce95)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5441d80aaSlling  * Common Development and Distribution License (the "License").
6441d80aaSlling  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
2199653d4eSeschrock 
22fa9e4066Sahrens /*
233f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24ce72e614SYuri Pankov  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
254263d13fSGeorge Wilson  * Copyright (c) 2012 by Delphix. All rights reserved.
26e1d5e507SFrederik Wessels  * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27fa9e4066Sahrens  */
28fa9e4066Sahrens 
29fa9e4066Sahrens #include <assert.h>
30fa9e4066Sahrens #include <ctype.h>
31fa9e4066Sahrens #include <dirent.h>
32fa9e4066Sahrens #include <errno.h>
33fa9e4066Sahrens #include <fcntl.h>
34fa9e4066Sahrens #include <libgen.h>
35fa9e4066Sahrens #include <libintl.h>
36fa9e4066Sahrens #include <libuutil.h>
37fa9e4066Sahrens #include <locale.h>
38fa9e4066Sahrens #include <stdio.h>
39fa9e4066Sahrens #include <stdlib.h>
40fa9e4066Sahrens #include <string.h>
41fa9e4066Sahrens #include <strings.h>
42fa9e4066Sahrens #include <unistd.h>
43fa9e4066Sahrens #include <priv.h>
44ecd6cf80Smarks #include <pwd.h>
45ecd6cf80Smarks #include <zone.h>
464263d13fSGeorge Wilson #include <zfs_prop.h>
47b1b8ab34Slling #include <sys/fs/zfs.h>
48fa9e4066Sahrens #include <sys/stat.h>
49fa9e4066Sahrens 
50fa9e4066Sahrens #include <libzfs.h>
51fa9e4066Sahrens 
52fa9e4066Sahrens #include "zpool_util.h"
53b7b97454Sperrin #include "zfs_comutil.h"
54ad135b5dSChristopher Siden #include "zfeature_common.h"
55fa9e4066Sahrens 
5626fd7700SKrishnendu Sadhukhan - Sun Microsystems #include "statcommon.h"
5726fd7700SKrishnendu Sadhukhan - Sun Microsystems 
58fa9e4066Sahrens static int zpool_do_create(int, char **);
59fa9e4066Sahrens static int zpool_do_destroy(int, char **);
60fa9e4066Sahrens 
61fa9e4066Sahrens static int zpool_do_add(int, char **);
6299653d4eSeschrock static int zpool_do_remove(int, char **);
63fa9e4066Sahrens 
64fa9e4066Sahrens static int zpool_do_list(int, char **);
65fa9e4066Sahrens static int zpool_do_iostat(int, char **);
66fa9e4066Sahrens static int zpool_do_status(int, char **);
67fa9e4066Sahrens 
68fa9e4066Sahrens static int zpool_do_online(int, char **);
69fa9e4066Sahrens static int zpool_do_offline(int, char **);
70ea8dc4b6Seschrock static int zpool_do_clear(int, char **);
714263d13fSGeorge Wilson static int zpool_do_reopen(int, char **);
72fa9e4066Sahrens 
73e9103aaeSGarrett D'Amore static int zpool_do_reguid(int, char **);
74e9103aaeSGarrett D'Amore 
75fa9e4066Sahrens static int zpool_do_attach(int, char **);
76fa9e4066Sahrens static int zpool_do_detach(int, char **);
77fa9e4066Sahrens static int zpool_do_replace(int, char **);
781195e687SMark J Musante static int zpool_do_split(int, char **);
79fa9e4066Sahrens 
80fa9e4066Sahrens static int zpool_do_scrub(int, char **);
81fa9e4066Sahrens 
82fa9e4066Sahrens static int zpool_do_import(int, char **);
83fa9e4066Sahrens static int zpool_do_export(int, char **);
84fa9e4066Sahrens 
85eaca9bbdSeschrock static int zpool_do_upgrade(int, char **);
86eaca9bbdSeschrock 
8706eeb2adSek110237 static int zpool_do_history(int, char **);
8806eeb2adSek110237 
89b1b8ab34Slling static int zpool_do_get(int, char **);
90b1b8ab34Slling static int zpool_do_set(int, char **);
91b1b8ab34Slling 
92fa9e4066Sahrens /*
93fa9e4066Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
94fa9e4066Sahrens  * debugging facilities.
95fa9e4066Sahrens  */
9629ab75c9Srm160521 
9729ab75c9Srm160521 #ifdef DEBUG
98fa9e4066Sahrens const char *
9999653d4eSeschrock _umem_debug_init(void)
100fa9e4066Sahrens {
101fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
102fa9e4066Sahrens }
103fa9e4066Sahrens 
104fa9e4066Sahrens const char *
105fa9e4066Sahrens _umem_logging_init(void)
106fa9e4066Sahrens {
107fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
108fa9e4066Sahrens }
10929ab75c9Srm160521 #endif
110fa9e4066Sahrens 
11165cd9f28Seschrock typedef enum {
11265cd9f28Seschrock 	HELP_ADD,
11365cd9f28Seschrock 	HELP_ATTACH,
114ea8dc4b6Seschrock 	HELP_CLEAR,
11565cd9f28Seschrock 	HELP_CREATE,
11665cd9f28Seschrock 	HELP_DESTROY,
11765cd9f28Seschrock 	HELP_DETACH,
11865cd9f28Seschrock 	HELP_EXPORT,
11906eeb2adSek110237 	HELP_HISTORY,
12065cd9f28Seschrock 	HELP_IMPORT,
12165cd9f28Seschrock 	HELP_IOSTAT,
12265cd9f28Seschrock 	HELP_LIST,
12365cd9f28Seschrock 	HELP_OFFLINE,
12465cd9f28Seschrock 	HELP_ONLINE,
12565cd9f28Seschrock 	HELP_REPLACE,
12699653d4eSeschrock 	HELP_REMOVE,
12765cd9f28Seschrock 	HELP_SCRUB,
128eaca9bbdSeschrock 	HELP_STATUS,
129b1b8ab34Slling 	HELP_UPGRADE,
130b1b8ab34Slling 	HELP_GET,
1311195e687SMark J Musante 	HELP_SET,
132e9103aaeSGarrett D'Amore 	HELP_SPLIT,
1334263d13fSGeorge Wilson 	HELP_REGUID,
1344263d13fSGeorge Wilson 	HELP_REOPEN
13565cd9f28Seschrock } zpool_help_t;
13665cd9f28Seschrock 
13765cd9f28Seschrock 
138fa9e4066Sahrens typedef struct zpool_command {
139fa9e4066Sahrens 	const char	*name;
140fa9e4066Sahrens 	int		(*func)(int, char **);
14165cd9f28Seschrock 	zpool_help_t	usage;
142fa9e4066Sahrens } zpool_command_t;
143fa9e4066Sahrens 
144fa9e4066Sahrens /*
145fa9e4066Sahrens  * Master command table.  Each ZFS command has a name, associated function, and
146ea8dc4b6Seschrock  * usage message.  The usage messages need to be internationalized, so we have
147ea8dc4b6Seschrock  * to have a function to return the usage message based on a command index.
14865cd9f28Seschrock  *
14965cd9f28Seschrock  * These commands are organized according to how they are displayed in the usage
15065cd9f28Seschrock  * message.  An empty command (one with a NULL name) indicates an empty line in
15165cd9f28Seschrock  * the generic usage message.
152fa9e4066Sahrens  */
153fa9e4066Sahrens static zpool_command_t command_table[] = {
15465cd9f28Seschrock 	{ "create",	zpool_do_create,	HELP_CREATE		},
15565cd9f28Seschrock 	{ "destroy",	zpool_do_destroy,	HELP_DESTROY		},
156fa9e4066Sahrens 	{ NULL },
15765cd9f28Seschrock 	{ "add",	zpool_do_add,		HELP_ADD		},
15899653d4eSeschrock 	{ "remove",	zpool_do_remove,	HELP_REMOVE		},
159fa9e4066Sahrens 	{ NULL },
16065cd9f28Seschrock 	{ "list",	zpool_do_list,		HELP_LIST		},
16165cd9f28Seschrock 	{ "iostat",	zpool_do_iostat,	HELP_IOSTAT		},
16265cd9f28Seschrock 	{ "status",	zpool_do_status,	HELP_STATUS		},
163fa9e4066Sahrens 	{ NULL },
16465cd9f28Seschrock 	{ "online",	zpool_do_online,	HELP_ONLINE		},
16565cd9f28Seschrock 	{ "offline",	zpool_do_offline,	HELP_OFFLINE		},
166ea8dc4b6Seschrock 	{ "clear",	zpool_do_clear,		HELP_CLEAR		},
1674263d13fSGeorge Wilson 	{ "reopen",	zpool_do_reopen,	HELP_REOPEN		},
168fa9e4066Sahrens 	{ NULL },
16965cd9f28Seschrock 	{ "attach",	zpool_do_attach,	HELP_ATTACH		},
17065cd9f28Seschrock 	{ "detach",	zpool_do_detach,	HELP_DETACH		},
17165cd9f28Seschrock 	{ "replace",	zpool_do_replace,	HELP_REPLACE		},
1721195e687SMark J Musante 	{ "split",	zpool_do_split,		HELP_SPLIT		},
173fa9e4066Sahrens 	{ NULL },
17465cd9f28Seschrock 	{ "scrub",	zpool_do_scrub,		HELP_SCRUB		},
175fa9e4066Sahrens 	{ NULL },
17665cd9f28Seschrock 	{ "import",	zpool_do_import,	HELP_IMPORT		},
17765cd9f28Seschrock 	{ "export",	zpool_do_export,	HELP_EXPORT		},
17806eeb2adSek110237 	{ "upgrade",	zpool_do_upgrade,	HELP_UPGRADE		},
179e9103aaeSGarrett D'Amore 	{ "reguid",	zpool_do_reguid,	HELP_REGUID		},
18006eeb2adSek110237 	{ NULL },
181b1b8ab34Slling 	{ "history",	zpool_do_history,	HELP_HISTORY		},
182b1b8ab34Slling 	{ "get",	zpool_do_get,		HELP_GET		},
183b1b8ab34Slling 	{ "set",	zpool_do_set,		HELP_SET		},
184fa9e4066Sahrens };
185fa9e4066Sahrens 
186fa9e4066Sahrens #define	NCOMMAND	(sizeof (command_table) / sizeof (command_table[0]))
187fa9e4066Sahrens 
1884445fffbSMatthew Ahrens static zpool_command_t *current_command;
1892a6b87f0Sek110237 static char history_str[HIS_MAX_RECORD_LEN];
1904445fffbSMatthew Ahrens static boolean_t log_history = B_TRUE;
19126fd7700SKrishnendu Sadhukhan - Sun Microsystems static uint_t timestamp_fmt = NODATE;
19226fd7700SKrishnendu Sadhukhan - Sun Microsystems 
19365cd9f28Seschrock static const char *
19465cd9f28Seschrock get_usage(zpool_help_t idx) {
19565cd9f28Seschrock 	switch (idx) {
19665cd9f28Seschrock 	case HELP_ADD:
19765cd9f28Seschrock 		return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
19865cd9f28Seschrock 	case HELP_ATTACH:
19965cd9f28Seschrock 		return (gettext("\tattach [-f] <pool> <device> "
200e45ce728Sahrens 		    "<new-device>\n"));
201ea8dc4b6Seschrock 	case HELP_CLEAR:
202468c413aSTim Haley 		return (gettext("\tclear [-nF] <pool> [device]\n"));
20365cd9f28Seschrock 	case HELP_CREATE:
204ad135b5dSChristopher Siden 		return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
2050a48a24eStimh 		    "\t    [-O file-system-property=value] ... \n"
206990b4856Slling 		    "\t    [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
20765cd9f28Seschrock 	case HELP_DESTROY:
20865cd9f28Seschrock 		return (gettext("\tdestroy [-f] <pool>\n"));
20965cd9f28Seschrock 	case HELP_DETACH:
21065cd9f28Seschrock 		return (gettext("\tdetach <pool> <device>\n"));
21165cd9f28Seschrock 	case HELP_EXPORT:
21265cd9f28Seschrock 		return (gettext("\texport [-f] <pool> ...\n"));
21306eeb2adSek110237 	case HELP_HISTORY:
214ecd6cf80Smarks 		return (gettext("\thistory [-il] [<pool>] ...\n"));
21565cd9f28Seschrock 	case HELP_IMPORT:
2164c58d714Sdarrenm 		return (gettext("\timport [-d dir] [-D]\n"
217f9af39baSGeorge Wilson 		    "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
2182f8aaab3Seschrock 		    "\timport [-o mntopts] [-o property=value] ... \n"
219f9af39baSGeorge Wilson 		    "\t    [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
220f9af39baSGeorge Wilson 		    "[-R root] [-F [-n]] -a\n"
2212f8aaab3Seschrock 		    "\timport [-o mntopts] [-o property=value] ... \n"
222f9af39baSGeorge Wilson 		    "\t    [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
223f9af39baSGeorge Wilson 		    "[-R root] [-F [-n]]\n"
224f9af39baSGeorge Wilson 		    "\t    <pool | id> [newpool]\n"));
22565cd9f28Seschrock 	case HELP_IOSTAT:
22626fd7700SKrishnendu Sadhukhan - Sun Microsystems 		return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
22765cd9f28Seschrock 		    "[count]]\n"));
22865cd9f28Seschrock 	case HELP_LIST:
229990b4856Slling 		return (gettext("\tlist [-H] [-o property[,...]] "
2303f9d6ad7SLin Ling 		    "[-T d|u] [pool] ... [interval [count]]\n"));
23165cd9f28Seschrock 	case HELP_OFFLINE:
232441d80aaSlling 		return (gettext("\toffline [-t] <pool> <device> ...\n"));
23365cd9f28Seschrock 	case HELP_ONLINE:
234441d80aaSlling 		return (gettext("\tonline <pool> <device> ...\n"));
23565cd9f28Seschrock 	case HELP_REPLACE:
23665cd9f28Seschrock 		return (gettext("\treplace [-f] <pool> <device> "
237e45ce728Sahrens 		    "[new-device]\n"));
23899653d4eSeschrock 	case HELP_REMOVE:
239fa94a07fSbrendan 		return (gettext("\tremove <pool> <device> ...\n"));
2404263d13fSGeorge Wilson 	case HELP_REOPEN:
24131d7e8faSGeorge Wilson 		return (gettext("\treopen <pool>\n"));
24265cd9f28Seschrock 	case HELP_SCRUB:
24365cd9f28Seschrock 		return (gettext("\tscrub [-s] <pool> ...\n"));
24465cd9f28Seschrock 	case HELP_STATUS:
2453f9d6ad7SLin Ling 		return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
2463f9d6ad7SLin Ling 		    "[count]]\n"));
247eaca9bbdSeschrock 	case HELP_UPGRADE:
248eaca9bbdSeschrock 		return (gettext("\tupgrade\n"
249eaca9bbdSeschrock 		    "\tupgrade -v\n"
250990b4856Slling 		    "\tupgrade [-V version] <-a | pool ...>\n"));
251b1b8ab34Slling 	case HELP_GET:
252e45ce728Sahrens 		return (gettext("\tget <\"all\" | property[,...]> "
253b1b8ab34Slling 		    "<pool> ...\n"));
254b1b8ab34Slling 	case HELP_SET:
255b1b8ab34Slling 		return (gettext("\tset <property=value> <pool> \n"));
2561195e687SMark J Musante 	case HELP_SPLIT:
2571195e687SMark J Musante 		return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
2581195e687SMark J Musante 		    "\t    [-o property=value] <pool> <newpool> "
2591195e687SMark J Musante 		    "[<device> ...]\n"));
260e9103aaeSGarrett D'Amore 	case HELP_REGUID:
261e9103aaeSGarrett D'Amore 		return (gettext("\treguid <pool>\n"));
26265cd9f28Seschrock 	}
26365cd9f28Seschrock 
26465cd9f28Seschrock 	abort();
26565cd9f28Seschrock 	/* NOTREACHED */
26665cd9f28Seschrock }
26765cd9f28Seschrock 
268fa9e4066Sahrens 
269fa9e4066Sahrens /*
270b1b8ab34Slling  * Callback routine that will print out a pool property value.
271b1b8ab34Slling  */
272990b4856Slling static int
273990b4856Slling print_prop_cb(int prop, void *cb)
274b1b8ab34Slling {
275b1b8ab34Slling 	FILE *fp = cb;
276b1b8ab34Slling 
277b24ab676SJeff Bonwick 	(void) fprintf(fp, "\t%-15s  ", zpool_prop_to_name(prop));
278b1b8ab34Slling 
279990b4856Slling 	if (zpool_prop_readonly(prop))
280990b4856Slling 		(void) fprintf(fp, "  NO   ");
281990b4856Slling 	else
282990b4856Slling 		(void) fprintf(fp, " YES   ");
283990b4856Slling 
284b1b8ab34Slling 	if (zpool_prop_values(prop) == NULL)
285b1b8ab34Slling 		(void) fprintf(fp, "-\n");
286b1b8ab34Slling 	else
287b1b8ab34Slling 		(void) fprintf(fp, "%s\n", zpool_prop_values(prop));
288b1b8ab34Slling 
289990b4856Slling 	return (ZPROP_CONT);
290b1b8ab34Slling }
291b1b8ab34Slling 
292b1b8ab34Slling /*
293fa9e4066Sahrens  * Display usage message.  If we're inside a command, display only the usage for
294fa9e4066Sahrens  * that command.  Otherwise, iterate over the entire command table and display
295fa9e4066Sahrens  * a complete usage message.
296fa9e4066Sahrens  */
297fa9e4066Sahrens void
29899653d4eSeschrock usage(boolean_t requested)
299fa9e4066Sahrens {
300fa9e4066Sahrens 	FILE *fp = requested ? stdout : stderr;
301fa9e4066Sahrens 
302fa9e4066Sahrens 	if (current_command == NULL) {
303fa9e4066Sahrens 		int i;
304fa9e4066Sahrens 
305fa9e4066Sahrens 		(void) fprintf(fp, gettext("usage: zpool command args ...\n"));
306fa9e4066Sahrens 		(void) fprintf(fp,
307fa9e4066Sahrens 		    gettext("where 'command' is one of the following:\n\n"));
308fa9e4066Sahrens 
309fa9e4066Sahrens 		for (i = 0; i < NCOMMAND; i++) {
310fa9e4066Sahrens 			if (command_table[i].name == NULL)
311fa9e4066Sahrens 				(void) fprintf(fp, "\n");
312fa9e4066Sahrens 			else
313fa9e4066Sahrens 				(void) fprintf(fp, "%s",
31465cd9f28Seschrock 				    get_usage(command_table[i].usage));
315fa9e4066Sahrens 		}
316fa9e4066Sahrens 	} else {
317fa9e4066Sahrens 		(void) fprintf(fp, gettext("usage:\n"));
31865cd9f28Seschrock 		(void) fprintf(fp, "%s", get_usage(current_command->usage));
319fa9e4066Sahrens 	}
320fa9e4066Sahrens 
321b1b8ab34Slling 	if (current_command != NULL &&
322b1b8ab34Slling 	    ((strcmp(current_command->name, "set") == 0) ||
323990b4856Slling 	    (strcmp(current_command->name, "get") == 0) ||
324990b4856Slling 	    (strcmp(current_command->name, "list") == 0))) {
325b1b8ab34Slling 
326b1b8ab34Slling 		(void) fprintf(fp,
327b1b8ab34Slling 		    gettext("\nthe following properties are supported:\n"));
328b1b8ab34Slling 
329b24ab676SJeff Bonwick 		(void) fprintf(fp, "\n\t%-15s  %s   %s\n\n",
330990b4856Slling 		    "PROPERTY", "EDIT", "VALUES");
331b1b8ab34Slling 
332b1b8ab34Slling 		/* Iterate over all properties */
333990b4856Slling 		(void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
334990b4856Slling 		    ZFS_TYPE_POOL);
335ad135b5dSChristopher Siden 
336ad135b5dSChristopher Siden 		(void) fprintf(fp, "\t%-15s   ", "feature@...");
337ad135b5dSChristopher Siden 		(void) fprintf(fp, "YES   disabled | enabled | active\n");
338ad135b5dSChristopher Siden 
339ad135b5dSChristopher Siden 		(void) fprintf(fp, gettext("\nThe feature@ properties must be "
340ad135b5dSChristopher Siden 		    "appended with a feature name.\nSee zpool-features(5).\n"));
341b1b8ab34Slling 	}
342b1b8ab34Slling 
343e9dbad6fSeschrock 	/*
344e9dbad6fSeschrock 	 * See comments at end of main().
345e9dbad6fSeschrock 	 */
346e9dbad6fSeschrock 	if (getenv("ZFS_ABORT") != NULL) {
347e9dbad6fSeschrock 		(void) printf("dumping core by request\n");
348e9dbad6fSeschrock 		abort();
349e9dbad6fSeschrock 	}
350e9dbad6fSeschrock 
351fa9e4066Sahrens 	exit(requested ? 0 : 2);
352fa9e4066Sahrens }
353fa9e4066Sahrens 
354fa9e4066Sahrens void
3558654d025Sperrin print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
3568654d025Sperrin     boolean_t print_logs)
357fa9e4066Sahrens {
358fa9e4066Sahrens 	nvlist_t **child;
359fa9e4066Sahrens 	uint_t c, children;
360afefbcddSeschrock 	char *vname;
361fa9e4066Sahrens 
362fa9e4066Sahrens 	if (name != NULL)
363fa9e4066Sahrens 		(void) printf("\t%*s%s\n", indent, "", name);
364fa9e4066Sahrens 
365fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
366fa9e4066Sahrens 	    &child, &children) != 0)
367fa9e4066Sahrens 		return;
368fa9e4066Sahrens 
369afefbcddSeschrock 	for (c = 0; c < children; c++) {
3708654d025Sperrin 		uint64_t is_log = B_FALSE;
3718654d025Sperrin 
3728654d025Sperrin 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3738654d025Sperrin 		    &is_log);
3748654d025Sperrin 		if ((is_log && !print_logs) || (!is_log && print_logs))
3758654d025Sperrin 			continue;
3768654d025Sperrin 
37788ecc943SGeorge Wilson 		vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3788654d025Sperrin 		print_vdev_tree(zhp, vname, child[c], indent + 2,
3798654d025Sperrin 		    B_FALSE);
380afefbcddSeschrock 		free(vname);
381afefbcddSeschrock 	}
382fa9e4066Sahrens }
383fa9e4066Sahrens 
38457221772SChristopher Siden static boolean_t
38557221772SChristopher Siden prop_list_contains_feature(nvlist_t *proplist)
38657221772SChristopher Siden {
38757221772SChristopher Siden 	nvpair_t *nvp;
38857221772SChristopher Siden 	for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
38957221772SChristopher Siden 	    nvp = nvlist_next_nvpair(proplist, nvp)) {
39057221772SChristopher Siden 		if (zpool_prop_feature(nvpair_name(nvp)))
39157221772SChristopher Siden 			return (B_TRUE);
39257221772SChristopher Siden 	}
39357221772SChristopher Siden 	return (B_FALSE);
39457221772SChristopher Siden }
39557221772SChristopher Siden 
396fa9e4066Sahrens /*
397990b4856Slling  * Add a property pair (name, string-value) into a property nvlist.
398990b4856Slling  */
399990b4856Slling static int
4000a48a24eStimh add_prop_list(const char *propname, char *propval, nvlist_t **props,
4010a48a24eStimh     boolean_t poolprop)
402990b4856Slling {
4030a48a24eStimh 	zpool_prop_t prop = ZPROP_INVAL;
4040a48a24eStimh 	zfs_prop_t fprop;
405990b4856Slling 	nvlist_t *proplist;
4060a48a24eStimh 	const char *normnm;
4070a48a24eStimh 	char *strval;
408990b4856Slling 
409990b4856Slling 	if (*props == NULL &&
410990b4856Slling 	    nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
411990b4856Slling 		(void) fprintf(stderr,
412990b4856Slling 		    gettext("internal error: out of memory\n"));
413990b4856Slling 		return (1);
414990b4856Slling 	}
415990b4856Slling 
416990b4856Slling 	proplist = *props;
417990b4856Slling 
4180a48a24eStimh 	if (poolprop) {
41957221772SChristopher Siden 		const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
42057221772SChristopher Siden 
421ad135b5dSChristopher Siden 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
422ad135b5dSChristopher Siden 		    !zpool_prop_feature(propname)) {
423990b4856Slling 			(void) fprintf(stderr, gettext("property '%s' is "
424990b4856Slling 			    "not a valid pool property\n"), propname);
425990b4856Slling 			return (2);
426990b4856Slling 		}
42757221772SChristopher Siden 
42857221772SChristopher Siden 		/*
42957221772SChristopher Siden 		 * feature@ properties and version should not be specified
43057221772SChristopher Siden 		 * at the same time.
43157221772SChristopher Siden 		 */
43257221772SChristopher Siden 		if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) &&
43357221772SChristopher Siden 		    nvlist_exists(proplist, vname)) ||
43457221772SChristopher Siden 		    (prop == ZPOOL_PROP_VERSION &&
43557221772SChristopher Siden 		    prop_list_contains_feature(proplist))) {
43657221772SChristopher Siden 			(void) fprintf(stderr, gettext("'feature@' and "
43757221772SChristopher Siden 			    "'version' properties cannot be specified "
43857221772SChristopher Siden 			    "together\n"));
43957221772SChristopher Siden 			return (2);
44057221772SChristopher Siden 		}
44157221772SChristopher Siden 
44257221772SChristopher Siden 
443ad135b5dSChristopher Siden 		if (zpool_prop_feature(propname))
444ad135b5dSChristopher Siden 			normnm = propname;
445ad135b5dSChristopher Siden 		else
4460a48a24eStimh 			normnm = zpool_prop_to_name(prop);
4470a48a24eStimh 	} else {
44814843421SMatthew Ahrens 		if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
4490a48a24eStimh 			normnm = zfs_prop_to_name(fprop);
45014843421SMatthew Ahrens 		} else {
45114843421SMatthew Ahrens 			normnm = propname;
45214843421SMatthew Ahrens 		}
4530a48a24eStimh 	}
454990b4856Slling 
4550a48a24eStimh 	if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
4560a48a24eStimh 	    prop != ZPOOL_PROP_CACHEFILE) {
457990b4856Slling 		(void) fprintf(stderr, gettext("property '%s' "
458990b4856Slling 		    "specified multiple times\n"), propname);
459990b4856Slling 		return (2);
460990b4856Slling 	}
461990b4856Slling 
4620a48a24eStimh 	if (nvlist_add_string(proplist, normnm, propval) != 0) {
463990b4856Slling 		(void) fprintf(stderr, gettext("internal "
464990b4856Slling 		    "error: out of memory\n"));
465990b4856Slling 		return (1);
466990b4856Slling 	}
467990b4856Slling 
468990b4856Slling 	return (0);
469990b4856Slling }
470990b4856Slling 
471990b4856Slling /*
472fa9e4066Sahrens  * zpool add [-fn] <pool> <vdev> ...
473fa9e4066Sahrens  *
474fa9e4066Sahrens  *	-f	Force addition of devices, even if they appear in use
475fa9e4066Sahrens  *	-n	Do not add the devices, but display the resulting layout if
476fa9e4066Sahrens  *		they were to be added.
477fa9e4066Sahrens  *
478fa9e4066Sahrens  * Adds the given vdevs to 'pool'.  As with create, the bulk of this work is
479fa9e4066Sahrens  * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
480fa9e4066Sahrens  * libzfs.
481fa9e4066Sahrens  */
482fa9e4066Sahrens int
483fa9e4066Sahrens zpool_do_add(int argc, char **argv)
484fa9e4066Sahrens {
48599653d4eSeschrock 	boolean_t force = B_FALSE;
48699653d4eSeschrock 	boolean_t dryrun = B_FALSE;
487fa9e4066Sahrens 	int c;
488fa9e4066Sahrens 	nvlist_t *nvroot;
489fa9e4066Sahrens 	char *poolname;
490fa9e4066Sahrens 	int ret;
491fa9e4066Sahrens 	zpool_handle_t *zhp;
492fa9e4066Sahrens 	nvlist_t *config;
493fa9e4066Sahrens 
494fa9e4066Sahrens 	/* check options */
495fa9e4066Sahrens 	while ((c = getopt(argc, argv, "fn")) != -1) {
496fa9e4066Sahrens 		switch (c) {
497fa9e4066Sahrens 		case 'f':
49899653d4eSeschrock 			force = B_TRUE;
499fa9e4066Sahrens 			break;
500fa9e4066Sahrens 		case 'n':
50199653d4eSeschrock 			dryrun = B_TRUE;
502fa9e4066Sahrens 			break;
503fa9e4066Sahrens 		case '?':
504fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
505fa9e4066Sahrens 			    optopt);
50699653d4eSeschrock 			usage(B_FALSE);
507fa9e4066Sahrens 		}
508fa9e4066Sahrens 	}
509fa9e4066Sahrens 
510fa9e4066Sahrens 	argc -= optind;
511fa9e4066Sahrens 	argv += optind;
512fa9e4066Sahrens 
513fa9e4066Sahrens 	/* get pool name and check number of arguments */
514fa9e4066Sahrens 	if (argc < 1) {
515fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
51699653d4eSeschrock 		usage(B_FALSE);
517fa9e4066Sahrens 	}
518fa9e4066Sahrens 	if (argc < 2) {
519fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing vdev specification\n"));
52099653d4eSeschrock 		usage(B_FALSE);
521fa9e4066Sahrens 	}
522fa9e4066Sahrens 
523fa9e4066Sahrens 	poolname = argv[0];
524fa9e4066Sahrens 
525fa9e4066Sahrens 	argc--;
526fa9e4066Sahrens 	argv++;
527fa9e4066Sahrens 
52899653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
529fa9e4066Sahrens 		return (1);
530fa9e4066Sahrens 
531088e9d47Seschrock 	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
532fa9e4066Sahrens 		(void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
533fa9e4066Sahrens 		    poolname);
534fa9e4066Sahrens 		zpool_close(zhp);
535fa9e4066Sahrens 		return (1);
536fa9e4066Sahrens 	}
537fa9e4066Sahrens 
538fa9e4066Sahrens 	/* pass off to get_vdev_spec for processing */
539705040edSEric Taylor 	nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
540705040edSEric Taylor 	    argc, argv);
541fa9e4066Sahrens 	if (nvroot == NULL) {
542fa9e4066Sahrens 		zpool_close(zhp);
543fa9e4066Sahrens 		return (1);
544fa9e4066Sahrens 	}
545fa9e4066Sahrens 
546fa9e4066Sahrens 	if (dryrun) {
547fa9e4066Sahrens 		nvlist_t *poolnvroot;
548fa9e4066Sahrens 
549fa9e4066Sahrens 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
550fa9e4066Sahrens 		    &poolnvroot) == 0);
551fa9e4066Sahrens 
552fa9e4066Sahrens 		(void) printf(gettext("would update '%s' to the following "
553fa9e4066Sahrens 		    "configuration:\n"), zpool_get_name(zhp));
554fa9e4066Sahrens 
5558654d025Sperrin 		/* print original main pool and new tree */
5568654d025Sperrin 		print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
5578654d025Sperrin 		print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
5588654d025Sperrin 
5598654d025Sperrin 		/* Do the same for the logs */
5608654d025Sperrin 		if (num_logs(poolnvroot) > 0) {
5618654d025Sperrin 			print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
5628654d025Sperrin 			print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
5638654d025Sperrin 		} else if (num_logs(nvroot) > 0) {
5648654d025Sperrin 			print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
5658654d025Sperrin 		}
566fa9e4066Sahrens 
567fa9e4066Sahrens 		ret = 0;
568fa9e4066Sahrens 	} else {
569fa9e4066Sahrens 		ret = (zpool_add(zhp, nvroot) != 0);
570fa9e4066Sahrens 	}
571fa9e4066Sahrens 
57299653d4eSeschrock 	nvlist_free(nvroot);
57399653d4eSeschrock 	zpool_close(zhp);
57499653d4eSeschrock 
57599653d4eSeschrock 	return (ret);
57699653d4eSeschrock }
57799653d4eSeschrock 
57899653d4eSeschrock /*
579fa94a07fSbrendan  * zpool remove  <pool> <vdev> ...
58099653d4eSeschrock  *
5813f9d6ad7SLin Ling  * Removes the given vdev from the pool.  Currently, this supports removing
5823f9d6ad7SLin Ling  * spares, cache, and log devices from the pool.
58399653d4eSeschrock  */
58499653d4eSeschrock int
58599653d4eSeschrock zpool_do_remove(int argc, char **argv)
58699653d4eSeschrock {
58799653d4eSeschrock 	char *poolname;
588fa94a07fSbrendan 	int i, ret = 0;
58999653d4eSeschrock 	zpool_handle_t *zhp;
59099653d4eSeschrock 
59199653d4eSeschrock 	argc--;
59299653d4eSeschrock 	argv++;
59399653d4eSeschrock 
59499653d4eSeschrock 	/* get pool name and check number of arguments */
59599653d4eSeschrock 	if (argc < 1) {
59699653d4eSeschrock 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
59799653d4eSeschrock 		usage(B_FALSE);
59899653d4eSeschrock 	}
59999653d4eSeschrock 	if (argc < 2) {
60099653d4eSeschrock 		(void) fprintf(stderr, gettext("missing device\n"));
60199653d4eSeschrock 		usage(B_FALSE);
60299653d4eSeschrock 	}
60399653d4eSeschrock 
60499653d4eSeschrock 	poolname = argv[0];
60599653d4eSeschrock 
60699653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
60799653d4eSeschrock 		return (1);
60899653d4eSeschrock 
609fa94a07fSbrendan 	for (i = 1; i < argc; i++) {
610fa94a07fSbrendan 		if (zpool_vdev_remove(zhp, argv[i]) != 0)
611fa94a07fSbrendan 			ret = 1;
612fa94a07fSbrendan 	}
61399653d4eSeschrock 
614fa9e4066Sahrens 	return (ret);
615fa9e4066Sahrens }
616fa9e4066Sahrens 
617fa9e4066Sahrens /*
618ad135b5dSChristopher Siden  * zpool create [-fnd] [-o property=value] ...
6190a48a24eStimh  *		[-O file-system-property=value] ...
6200a48a24eStimh  *		[-R root] [-m mountpoint] <pool> <dev> ...
621fa9e4066Sahrens  *
622fa9e4066Sahrens  *	-f	Force creation, even if devices appear in use
623fa9e4066Sahrens  *	-n	Do not create the pool, but display the resulting layout if it
624fa9e4066Sahrens  *		were to be created.
625fa9e4066Sahrens  *      -R	Create a pool under an alternate root
626fa9e4066Sahrens  *      -m	Set default mountpoint for the root dataset.  By default it's
627fa9e4066Sahrens  *		'/<pool>'
628990b4856Slling  *	-o	Set property=value.
629ad135b5dSChristopher Siden  *	-d	Don't automatically enable all supported pool features
630ad135b5dSChristopher Siden  *		(individual features can be enabled with -o).
6310a48a24eStimh  *	-O	Set fsproperty=value in the pool's root file system
632fa9e4066Sahrens  *
633b1b8ab34Slling  * Creates the named pool according to the given vdev specification.  The
634fa9e4066Sahrens  * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c.  Once
635fa9e4066Sahrens  * we get the nvlist back from get_vdev_spec(), we either print out the contents
636fa9e4066Sahrens  * (if '-n' was specified), or pass it to libzfs to do the creation.
637fa9e4066Sahrens  */
638fa9e4066Sahrens int
639fa9e4066Sahrens zpool_do_create(int argc, char **argv)
640fa9e4066Sahrens {
64199653d4eSeschrock 	boolean_t force = B_FALSE;
64299653d4eSeschrock 	boolean_t dryrun = B_FALSE;
643ad135b5dSChristopher Siden 	boolean_t enable_all_pool_feat = B_TRUE;
644fa9e4066Sahrens 	int c;
645990b4856Slling 	nvlist_t *nvroot = NULL;
646fa9e4066Sahrens 	char *poolname;
647990b4856Slling 	int ret = 1;
648fa9e4066Sahrens 	char *altroot = NULL;
649fa9e4066Sahrens 	char *mountpoint = NULL;
6500a48a24eStimh 	nvlist_t *fsprops = NULL;
651990b4856Slling 	nvlist_t *props = NULL;
6522f8aaab3Seschrock 	char *propval;
653fa9e4066Sahrens 
654fa9e4066Sahrens 	/* check options */
655ad135b5dSChristopher Siden 	while ((c = getopt(argc, argv, ":fndR:m:o:O:")) != -1) {
656fa9e4066Sahrens 		switch (c) {
657fa9e4066Sahrens 		case 'f':
65899653d4eSeschrock 			force = B_TRUE;
659fa9e4066Sahrens 			break;
660fa9e4066Sahrens 		case 'n':
66199653d4eSeschrock 			dryrun = B_TRUE;
662fa9e4066Sahrens 			break;
663ad135b5dSChristopher Siden 		case 'd':
664ad135b5dSChristopher Siden 			enable_all_pool_feat = B_FALSE;
665ad135b5dSChristopher Siden 			break;
666fa9e4066Sahrens 		case 'R':
667fa9e4066Sahrens 			altroot = optarg;
668990b4856Slling 			if (add_prop_list(zpool_prop_to_name(
6690a48a24eStimh 			    ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
670990b4856Slling 				goto errout;
6712f8aaab3Seschrock 			if (nvlist_lookup_string(props,
6722f8aaab3Seschrock 			    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
6732f8aaab3Seschrock 			    &propval) == 0)
6742f8aaab3Seschrock 				break;
675990b4856Slling 			if (add_prop_list(zpool_prop_to_name(
6760a48a24eStimh 			    ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
677990b4856Slling 				goto errout;
678fa9e4066Sahrens 			break;
679fa9e4066Sahrens 		case 'm':
680*8b713775SWill Andrews 			/* Equivalent to -O mountpoint=optarg */
681fa9e4066Sahrens 			mountpoint = optarg;
682fa9e4066Sahrens 			break;
683990b4856Slling 		case 'o':
684990b4856Slling 			if ((propval = strchr(optarg, '=')) == NULL) {
685990b4856Slling 				(void) fprintf(stderr, gettext("missing "
686990b4856Slling 				    "'=' for -o option\n"));
687990b4856Slling 				goto errout;
688990b4856Slling 			}
689990b4856Slling 			*propval = '\0';
690990b4856Slling 			propval++;
691990b4856Slling 
6920a48a24eStimh 			if (add_prop_list(optarg, propval, &props, B_TRUE))
6930a48a24eStimh 				goto errout;
694ad135b5dSChristopher Siden 
695ad135b5dSChristopher Siden 			/*
696ad135b5dSChristopher Siden 			 * If the user is creating a pool that doesn't support
697ad135b5dSChristopher Siden 			 * feature flags, don't enable any features.
698ad135b5dSChristopher Siden 			 */
699ad135b5dSChristopher Siden 			if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
700ad135b5dSChristopher Siden 				char *end;
701ad135b5dSChristopher Siden 				u_longlong_t ver;
702ad135b5dSChristopher Siden 
703ad135b5dSChristopher Siden 				ver = strtoull(propval, &end, 10);
704ad135b5dSChristopher Siden 				if (*end == '\0' &&
705ad135b5dSChristopher Siden 				    ver < SPA_VERSION_FEATURES) {
706ad135b5dSChristopher Siden 					enable_all_pool_feat = B_FALSE;
707ad135b5dSChristopher Siden 				}
708ad135b5dSChristopher Siden 			}
7090a48a24eStimh 			break;
7100a48a24eStimh 		case 'O':
7110a48a24eStimh 			if ((propval = strchr(optarg, '=')) == NULL) {
7120a48a24eStimh 				(void) fprintf(stderr, gettext("missing "
7130a48a24eStimh 				    "'=' for -O option\n"));
7140a48a24eStimh 				goto errout;
7150a48a24eStimh 			}
7160a48a24eStimh 			*propval = '\0';
7170a48a24eStimh 			propval++;
7180a48a24eStimh 
719*8b713775SWill Andrews 			/*
720*8b713775SWill Andrews 			 * Mountpoints are checked and then added later.
721*8b713775SWill Andrews 			 * Uniquely among properties, they can be specified
722*8b713775SWill Andrews 			 * more than once, to avoid conflict with -m.
723*8b713775SWill Andrews 			 */
724*8b713775SWill Andrews 			if (0 == strcmp(optarg,
725*8b713775SWill Andrews 			    zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
726*8b713775SWill Andrews 				mountpoint = propval;
727*8b713775SWill Andrews 			} else if (add_prop_list(optarg, propval, &fsprops,
728*8b713775SWill Andrews 			    B_FALSE)) {
729990b4856Slling 				goto errout;
730*8b713775SWill Andrews 			}
731990b4856Slling 			break;
732fa9e4066Sahrens 		case ':':
733fa9e4066Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
734fa9e4066Sahrens 			    "'%c' option\n"), optopt);
735990b4856Slling 			goto badusage;
736fa9e4066Sahrens 		case '?':
737fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
738fa9e4066Sahrens 			    optopt);
739990b4856Slling 			goto badusage;
740fa9e4066Sahrens 		}
741fa9e4066Sahrens 	}
742fa9e4066Sahrens 
743fa9e4066Sahrens 	argc -= optind;
744fa9e4066Sahrens 	argv += optind;
745fa9e4066Sahrens 
746fa9e4066Sahrens 	/* get pool name and check number of arguments */
747fa9e4066Sahrens 	if (argc < 1) {
748fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
749990b4856Slling 		goto badusage;
750fa9e4066Sahrens 	}
751fa9e4066Sahrens 	if (argc < 2) {
752fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing vdev specification\n"));
753990b4856Slling 		goto badusage;
754fa9e4066Sahrens 	}
755fa9e4066Sahrens 
756fa9e4066Sahrens 	poolname = argv[0];
757fa9e4066Sahrens 
758fa9e4066Sahrens 	/*
759fa9e4066Sahrens 	 * As a special case, check for use of '/' in the name, and direct the
760fa9e4066Sahrens 	 * user to use 'zfs create' instead.
761fa9e4066Sahrens 	 */
762fa9e4066Sahrens 	if (strchr(poolname, '/') != NULL) {
763fa9e4066Sahrens 		(void) fprintf(stderr, gettext("cannot create '%s': invalid "
764fa9e4066Sahrens 		    "character '/' in pool name\n"), poolname);
765fa9e4066Sahrens 		(void) fprintf(stderr, gettext("use 'zfs create' to "
766fa9e4066Sahrens 		    "create a dataset\n"));
767990b4856Slling 		goto errout;
768fa9e4066Sahrens 	}
769fa9e4066Sahrens 
770fa9e4066Sahrens 	/* pass off to get_vdev_spec for bulk processing */
771705040edSEric Taylor 	nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
772705040edSEric Taylor 	    argc - 1, argv + 1);
773fa9e4066Sahrens 	if (nvroot == NULL)
7740a48a24eStimh 		goto errout;
775fa9e4066Sahrens 
77699653d4eSeschrock 	/* make_root_vdev() allows 0 toplevel children if there are spares */
777b7b97454Sperrin 	if (!zfs_allocatable_devs(nvroot)) {
77899653d4eSeschrock 		(void) fprintf(stderr, gettext("invalid vdev "
77999653d4eSeschrock 		    "specification: at least one toplevel vdev must be "
78099653d4eSeschrock 		    "specified\n"));
781990b4856Slling 		goto errout;
78299653d4eSeschrock 	}
78399653d4eSeschrock 
784fa9e4066Sahrens 	if (altroot != NULL && altroot[0] != '/') {
785fa9e4066Sahrens 		(void) fprintf(stderr, gettext("invalid alternate root '%s': "
786e9dbad6fSeschrock 		    "must be an absolute path\n"), altroot);
787990b4856Slling 		goto errout;
788fa9e4066Sahrens 	}
789fa9e4066Sahrens 
790fa9e4066Sahrens 	/*
791fa9e4066Sahrens 	 * Check the validity of the mountpoint and direct the user to use the
792fa9e4066Sahrens 	 * '-m' mountpoint option if it looks like its in use.
793fa9e4066Sahrens 	 */
794fa9e4066Sahrens 	if (mountpoint == NULL ||
795fa9e4066Sahrens 	    (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
796fa9e4066Sahrens 	    strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
797fa9e4066Sahrens 		char buf[MAXPATHLEN];
79811022c7cStimh 		DIR *dirp;
799fa9e4066Sahrens 
800fa9e4066Sahrens 		if (mountpoint && mountpoint[0] != '/') {
801fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid mountpoint "
802fa9e4066Sahrens 			    "'%s': must be an absolute path, 'legacy', or "
803fa9e4066Sahrens 			    "'none'\n"), mountpoint);
804990b4856Slling 			goto errout;
805fa9e4066Sahrens 		}
806fa9e4066Sahrens 
807fa9e4066Sahrens 		if (mountpoint == NULL) {
808fa9e4066Sahrens 			if (altroot != NULL)
809fa9e4066Sahrens 				(void) snprintf(buf, sizeof (buf), "%s/%s",
810fa9e4066Sahrens 				    altroot, poolname);
811fa9e4066Sahrens 			else
812fa9e4066Sahrens 				(void) snprintf(buf, sizeof (buf), "/%s",
813fa9e4066Sahrens 				    poolname);
814fa9e4066Sahrens 		} else {
815fa9e4066Sahrens 			if (altroot != NULL)
816fa9e4066Sahrens 				(void) snprintf(buf, sizeof (buf), "%s%s",
817fa9e4066Sahrens 				    altroot, mountpoint);
818fa9e4066Sahrens 			else
819fa9e4066Sahrens 				(void) snprintf(buf, sizeof (buf), "%s",
820fa9e4066Sahrens 				    mountpoint);
821fa9e4066Sahrens 		}
822fa9e4066Sahrens 
82311022c7cStimh 		if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
82411022c7cStimh 			(void) fprintf(stderr, gettext("mountpoint '%s' : "
82511022c7cStimh 			    "%s\n"), buf, strerror(errno));
826fa9e4066Sahrens 			(void) fprintf(stderr, gettext("use '-m' "
827fa9e4066Sahrens 			    "option to provide a different default\n"));
828990b4856Slling 			goto errout;
82911022c7cStimh 		} else if (dirp) {
83011022c7cStimh 			int count = 0;
83111022c7cStimh 
83211022c7cStimh 			while (count < 3 && readdir(dirp) != NULL)
83311022c7cStimh 				count++;
83411022c7cStimh 			(void) closedir(dirp);
83511022c7cStimh 
83611022c7cStimh 			if (count > 2) {
83711022c7cStimh 				(void) fprintf(stderr, gettext("mountpoint "
83811022c7cStimh 				    "'%s' exists and is not empty\n"), buf);
83911022c7cStimh 				(void) fprintf(stderr, gettext("use '-m' "
84011022c7cStimh 				    "option to provide a "
84111022c7cStimh 				    "different default\n"));
84211022c7cStimh 				goto errout;
84311022c7cStimh 			}
844fa9e4066Sahrens 		}
845fa9e4066Sahrens 	}
846fa9e4066Sahrens 
847*8b713775SWill Andrews 	/*
848*8b713775SWill Andrews 	 * Now that the mountpoint's validity has been checked, ensure that
849*8b713775SWill Andrews 	 * the property is set appropriately prior to creating the pool.
850*8b713775SWill Andrews 	 */
851*8b713775SWill Andrews 	if (mountpoint != NULL) {
852*8b713775SWill Andrews 		ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
853*8b713775SWill Andrews 		    mountpoint, &fsprops, B_FALSE);
854*8b713775SWill Andrews 		if (ret != 0)
855*8b713775SWill Andrews 			goto errout;
856*8b713775SWill Andrews 	}
857*8b713775SWill Andrews 
858*8b713775SWill Andrews 	ret = 1;
859fa9e4066Sahrens 	if (dryrun) {
860fa9e4066Sahrens 		/*
861fa9e4066Sahrens 		 * For a dry run invocation, print out a basic message and run
862fa9e4066Sahrens 		 * through all the vdevs in the list and print out in an
863fa9e4066Sahrens 		 * appropriate hierarchy.
864fa9e4066Sahrens 		 */
865fa9e4066Sahrens 		(void) printf(gettext("would create '%s' with the "
866fa9e4066Sahrens 		    "following layout:\n\n"), poolname);
867fa9e4066Sahrens 
8688654d025Sperrin 		print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
8698654d025Sperrin 		if (num_logs(nvroot) > 0)
8708654d025Sperrin 			print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
871fa9e4066Sahrens 
872fa9e4066Sahrens 		ret = 0;
873fa9e4066Sahrens 	} else {
874fa9e4066Sahrens 		/*
875fa9e4066Sahrens 		 * Hand off to libzfs.
876fa9e4066Sahrens 		 */
877ad135b5dSChristopher Siden 		if (enable_all_pool_feat) {
878ad135b5dSChristopher Siden 			int i;
879ad135b5dSChristopher Siden 			for (i = 0; i < SPA_FEATURES; i++) {
880ad135b5dSChristopher Siden 				char propname[MAXPATHLEN];
881ad135b5dSChristopher Siden 				zfeature_info_t *feat = &spa_feature_table[i];
882ad135b5dSChristopher Siden 
883ad135b5dSChristopher Siden 				(void) snprintf(propname, sizeof (propname),
884ad135b5dSChristopher Siden 				    "feature@%s", feat->fi_uname);
885ad135b5dSChristopher Siden 
886ad135b5dSChristopher Siden 				/*
887ad135b5dSChristopher Siden 				 * Skip feature if user specified it manually
888ad135b5dSChristopher Siden 				 * on the command line.
889ad135b5dSChristopher Siden 				 */
890ad135b5dSChristopher Siden 				if (nvlist_exists(props, propname))
891ad135b5dSChristopher Siden 					continue;
892ad135b5dSChristopher Siden 
893*8b713775SWill Andrews 				ret = add_prop_list(propname,
894*8b713775SWill Andrews 				    ZFS_FEATURE_ENABLED, &props, B_TRUE);
895*8b713775SWill Andrews 				if (ret != 0)
896ad135b5dSChristopher Siden 					goto errout;
897ad135b5dSChristopher Siden 			}
898ad135b5dSChristopher Siden 		}
899*8b713775SWill Andrews 
900*8b713775SWill Andrews 		ret = 1;
9010a48a24eStimh 		if (zpool_create(g_zfs, poolname,
9020a48a24eStimh 		    nvroot, props, fsprops) == 0) {
90399653d4eSeschrock 			zfs_handle_t *pool = zfs_open(g_zfs, poolname,
904fa9e4066Sahrens 			    ZFS_TYPE_FILESYSTEM);
905fa9e4066Sahrens 			if (pool != NULL) {
906fa9e4066Sahrens 				if (zfs_mount(pool, NULL, 0) == 0)
907da6c28aaSamw 					ret = zfs_shareall(pool);
908fa9e4066Sahrens 				zfs_close(pool);
909fa9e4066Sahrens 			}
91099653d4eSeschrock 		} else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
91199653d4eSeschrock 			(void) fprintf(stderr, gettext("pool name may have "
91299653d4eSeschrock 			    "been omitted\n"));
913fa9e4066Sahrens 		}
914fa9e4066Sahrens 	}
915fa9e4066Sahrens 
916990b4856Slling errout:
917fa9e4066Sahrens 	nvlist_free(nvroot);
9180a48a24eStimh 	nvlist_free(fsprops);
919990b4856Slling 	nvlist_free(props);
920fa9e4066Sahrens 	return (ret);
921990b4856Slling badusage:
9220a48a24eStimh 	nvlist_free(fsprops);
923990b4856Slling 	nvlist_free(props);
924990b4856Slling 	usage(B_FALSE);
925990b4856Slling 	return (2);
926fa9e4066Sahrens }
927fa9e4066Sahrens 
928fa9e4066Sahrens /*
929fa9e4066Sahrens  * zpool destroy <pool>
930fa9e4066Sahrens  *
931fa9e4066Sahrens  * 	-f	Forcefully unmount any datasets
932fa9e4066Sahrens  *
933fa9e4066Sahrens  * Destroy the given pool.  Automatically unmounts any datasets in the pool.
934fa9e4066Sahrens  */
935fa9e4066Sahrens int
936fa9e4066Sahrens zpool_do_destroy(int argc, char **argv)
937fa9e4066Sahrens {
93899653d4eSeschrock 	boolean_t force = B_FALSE;
939fa9e4066Sahrens 	int c;
940fa9e4066Sahrens 	char *pool;
941fa9e4066Sahrens 	zpool_handle_t *zhp;
942fa9e4066Sahrens 	int ret;
943fa9e4066Sahrens 
944fa9e4066Sahrens 	/* check options */
945fa9e4066Sahrens 	while ((c = getopt(argc, argv, "f")) != -1) {
946fa9e4066Sahrens 		switch (c) {
947fa9e4066Sahrens 		case 'f':
94899653d4eSeschrock 			force = B_TRUE;
949fa9e4066Sahrens 			break;
950fa9e4066Sahrens 		case '?':
951fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
952fa9e4066Sahrens 			    optopt);
95399653d4eSeschrock 			usage(B_FALSE);
954fa9e4066Sahrens 		}
955fa9e4066Sahrens 	}
956fa9e4066Sahrens 
957fa9e4066Sahrens 	argc -= optind;
958fa9e4066Sahrens 	argv += optind;
959fa9e4066Sahrens 
960fa9e4066Sahrens 	/* check arguments */
961fa9e4066Sahrens 	if (argc < 1) {
962fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool argument\n"));
96399653d4eSeschrock 		usage(B_FALSE);
964fa9e4066Sahrens 	}
965fa9e4066Sahrens 	if (argc > 1) {
966fa9e4066Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
96799653d4eSeschrock 		usage(B_FALSE);
968fa9e4066Sahrens 	}
969fa9e4066Sahrens 
970fa9e4066Sahrens 	pool = argv[0];
971fa9e4066Sahrens 
97299653d4eSeschrock 	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
973fa9e4066Sahrens 		/*
974fa9e4066Sahrens 		 * As a special case, check for use of '/' in the name, and
975fa9e4066Sahrens 		 * direct the user to use 'zfs destroy' instead.
976fa9e4066Sahrens 		 */
977fa9e4066Sahrens 		if (strchr(pool, '/') != NULL)
978fa9e4066Sahrens 			(void) fprintf(stderr, gettext("use 'zfs destroy' to "
979fa9e4066Sahrens 			    "destroy a dataset\n"));
980fa9e4066Sahrens 		return (1);
981fa9e4066Sahrens 	}
982fa9e4066Sahrens 
983f3861e1aSahl 	if (zpool_disable_datasets(zhp, force) != 0) {
984fa9e4066Sahrens 		(void) fprintf(stderr, gettext("could not destroy '%s': "
985fa9e4066Sahrens 		    "could not unmount datasets\n"), zpool_get_name(zhp));
986fa9e4066Sahrens 		return (1);
987fa9e4066Sahrens 	}
988fa9e4066Sahrens 
9894445fffbSMatthew Ahrens 	/* The history must be logged as part of the export */
9904445fffbSMatthew Ahrens 	log_history = B_FALSE;
9914445fffbSMatthew Ahrens 
9924445fffbSMatthew Ahrens 	ret = (zpool_destroy(zhp, history_str) != 0);
993fa9e4066Sahrens 
994fa9e4066Sahrens 	zpool_close(zhp);
995fa9e4066Sahrens 
996fa9e4066Sahrens 	return (ret);
997fa9e4066Sahrens }
998fa9e4066Sahrens 
999fa9e4066Sahrens /*
1000fa9e4066Sahrens  * zpool export [-f] <pool> ...
1001fa9e4066Sahrens  *
1002fa9e4066Sahrens  *	-f	Forcefully unmount datasets
1003fa9e4066Sahrens  *
1004b1b8ab34Slling  * Export the given pools.  By default, the command will attempt to cleanly
1005fa9e4066Sahrens  * unmount any active datasets within the pool.  If the '-f' flag is specified,
1006fa9e4066Sahrens  * then the datasets will be forcefully unmounted.
1007fa9e4066Sahrens  */
1008fa9e4066Sahrens int
1009fa9e4066Sahrens zpool_do_export(int argc, char **argv)
1010fa9e4066Sahrens {
101199653d4eSeschrock 	boolean_t force = B_FALSE;
1012394ab0cbSGeorge Wilson 	boolean_t hardforce = B_FALSE;
1013fa9e4066Sahrens 	int c;
1014fa9e4066Sahrens 	zpool_handle_t *zhp;
1015fa9e4066Sahrens 	int ret;
1016fa9e4066Sahrens 	int i;
1017fa9e4066Sahrens 
1018fa9e4066Sahrens 	/* check options */
1019394ab0cbSGeorge Wilson 	while ((c = getopt(argc, argv, "fF")) != -1) {
1020fa9e4066Sahrens 		switch (c) {
1021fa9e4066Sahrens 		case 'f':
102299653d4eSeschrock 			force = B_TRUE;
1023fa9e4066Sahrens 			break;
1024394ab0cbSGeorge Wilson 		case 'F':
1025394ab0cbSGeorge Wilson 			hardforce = B_TRUE;
1026394ab0cbSGeorge Wilson 			break;
1027fa9e4066Sahrens 		case '?':
1028fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1029fa9e4066Sahrens 			    optopt);
103099653d4eSeschrock 			usage(B_FALSE);
1031fa9e4066Sahrens 		}
1032fa9e4066Sahrens 	}
1033fa9e4066Sahrens 
1034fa9e4066Sahrens 	argc -= optind;
1035fa9e4066Sahrens 	argv += optind;
1036fa9e4066Sahrens 
1037fa9e4066Sahrens 	/* check arguments */
1038fa9e4066Sahrens 	if (argc < 1) {
1039fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool argument\n"));
104099653d4eSeschrock 		usage(B_FALSE);
1041fa9e4066Sahrens 	}
1042fa9e4066Sahrens 
1043fa9e4066Sahrens 	ret = 0;
1044fa9e4066Sahrens 	for (i = 0; i < argc; i++) {
104599653d4eSeschrock 		if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1046fa9e4066Sahrens 			ret = 1;
1047fa9e4066Sahrens 			continue;
1048fa9e4066Sahrens 		}
1049fa9e4066Sahrens 
1050f3861e1aSahl 		if (zpool_disable_datasets(zhp, force) != 0) {
1051fa9e4066Sahrens 			ret = 1;
1052fa9e4066Sahrens 			zpool_close(zhp);
1053fa9e4066Sahrens 			continue;
1054fa9e4066Sahrens 		}
1055fa9e4066Sahrens 
10564445fffbSMatthew Ahrens 		/* The history must be logged as part of the export */
10574445fffbSMatthew Ahrens 		log_history = B_FALSE;
10584445fffbSMatthew Ahrens 
1059394ab0cbSGeorge Wilson 		if (hardforce) {
10604445fffbSMatthew Ahrens 			if (zpool_export_force(zhp, history_str) != 0)
1061fa9e4066Sahrens 				ret = 1;
10624445fffbSMatthew Ahrens 		} else if (zpool_export(zhp, force, history_str) != 0) {
1063394ab0cbSGeorge Wilson 			ret = 1;
1064394ab0cbSGeorge Wilson 		}
1065fa9e4066Sahrens 
1066fa9e4066Sahrens 		zpool_close(zhp);
1067fa9e4066Sahrens 	}
1068fa9e4066Sahrens 
1069fa9e4066Sahrens 	return (ret);
1070fa9e4066Sahrens }
1071fa9e4066Sahrens 
1072fa9e4066Sahrens /*
1073fa9e4066Sahrens  * Given a vdev configuration, determine the maximum width needed for the device
1074fa9e4066Sahrens  * name column.
1075fa9e4066Sahrens  */
1076fa9e4066Sahrens static int
1077c67d9675Seschrock max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1078fa9e4066Sahrens {
107988ecc943SGeorge Wilson 	char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1080fa9e4066Sahrens 	nvlist_t **child;
1081fa9e4066Sahrens 	uint_t c, children;
1082fa9e4066Sahrens 	int ret;
1083fa9e4066Sahrens 
1084fa9e4066Sahrens 	if (strlen(name) + depth > max)
1085fa9e4066Sahrens 		max = strlen(name) + depth;
1086fa9e4066Sahrens 
1087afefbcddSeschrock 	free(name);
1088afefbcddSeschrock 
108999653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
109099653d4eSeschrock 	    &child, &children) == 0) {
1091fa9e4066Sahrens 		for (c = 0; c < children; c++)
109299653d4eSeschrock 			if ((ret = max_width(zhp, child[c], depth + 2,
109399653d4eSeschrock 			    max)) > max)
1094fa9e4066Sahrens 				max = ret;
109599653d4eSeschrock 	}
109699653d4eSeschrock 
1097fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1098fa94a07fSbrendan 	    &child, &children) == 0) {
1099fa94a07fSbrendan 		for (c = 0; c < children; c++)
1100fa94a07fSbrendan 			if ((ret = max_width(zhp, child[c], depth + 2,
1101fa94a07fSbrendan 			    max)) > max)
1102fa94a07fSbrendan 				max = ret;
1103fa94a07fSbrendan 	}
1104fa94a07fSbrendan 
110599653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
110699653d4eSeschrock 	    &child, &children) == 0) {
110799653d4eSeschrock 		for (c = 0; c < children; c++)
110899653d4eSeschrock 			if ((ret = max_width(zhp, child[c], depth + 2,
110999653d4eSeschrock 			    max)) > max)
111099653d4eSeschrock 				max = ret;
111199653d4eSeschrock 	}
111299653d4eSeschrock 
1113fa9e4066Sahrens 
1114fa9e4066Sahrens 	return (max);
1115fa9e4066Sahrens }
1116fa9e4066Sahrens 
1117e6ca193dSGeorge Wilson typedef struct spare_cbdata {
1118e6ca193dSGeorge Wilson 	uint64_t	cb_guid;
1119e6ca193dSGeorge Wilson 	zpool_handle_t	*cb_zhp;
1120e6ca193dSGeorge Wilson } spare_cbdata_t;
1121e6ca193dSGeorge Wilson 
1122e6ca193dSGeorge Wilson static boolean_t
1123e6ca193dSGeorge Wilson find_vdev(nvlist_t *nv, uint64_t search)
1124e6ca193dSGeorge Wilson {
1125e6ca193dSGeorge Wilson 	uint64_t guid;
1126e6ca193dSGeorge Wilson 	nvlist_t **child;
1127e6ca193dSGeorge Wilson 	uint_t c, children;
1128e6ca193dSGeorge Wilson 
1129e6ca193dSGeorge Wilson 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1130e6ca193dSGeorge Wilson 	    search == guid)
1131e6ca193dSGeorge Wilson 		return (B_TRUE);
1132e6ca193dSGeorge Wilson 
1133e6ca193dSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1134e6ca193dSGeorge Wilson 	    &child, &children) == 0) {
1135e6ca193dSGeorge Wilson 		for (c = 0; c < children; c++)
1136e6ca193dSGeorge Wilson 			if (find_vdev(child[c], search))
1137e6ca193dSGeorge Wilson 				return (B_TRUE);
1138e6ca193dSGeorge Wilson 	}
1139e6ca193dSGeorge Wilson 
1140e6ca193dSGeorge Wilson 	return (B_FALSE);
1141e6ca193dSGeorge Wilson }
1142e6ca193dSGeorge Wilson 
1143e6ca193dSGeorge Wilson static int
1144e6ca193dSGeorge Wilson find_spare(zpool_handle_t *zhp, void *data)
1145e6ca193dSGeorge Wilson {
1146e6ca193dSGeorge Wilson 	spare_cbdata_t *cbp = data;
1147e6ca193dSGeorge Wilson 	nvlist_t *config, *nvroot;
1148e6ca193dSGeorge Wilson 
1149e6ca193dSGeorge Wilson 	config = zpool_get_config(zhp, NULL);
1150e6ca193dSGeorge Wilson 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1151e6ca193dSGeorge Wilson 	    &nvroot) == 0);
1152e6ca193dSGeorge Wilson 
1153e6ca193dSGeorge Wilson 	if (find_vdev(nvroot, cbp->cb_guid)) {
1154e6ca193dSGeorge Wilson 		cbp->cb_zhp = zhp;
1155e6ca193dSGeorge Wilson 		return (1);
1156e6ca193dSGeorge Wilson 	}
1157e6ca193dSGeorge Wilson 
1158e6ca193dSGeorge Wilson 	zpool_close(zhp);
1159e6ca193dSGeorge Wilson 	return (0);
1160e6ca193dSGeorge Wilson }
1161e6ca193dSGeorge Wilson 
1162e6ca193dSGeorge Wilson /*
1163e6ca193dSGeorge Wilson  * Print out configuration state as requested by status_callback.
1164e6ca193dSGeorge Wilson  */
1165e6ca193dSGeorge Wilson void
1166e6ca193dSGeorge Wilson print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1167e6ca193dSGeorge Wilson     int namewidth, int depth, boolean_t isspare)
1168e6ca193dSGeorge Wilson {
1169e6ca193dSGeorge Wilson 	nvlist_t **child;
1170e6ca193dSGeorge Wilson 	uint_t c, children;
11713f9d6ad7SLin Ling 	pool_scan_stat_t *ps = NULL;
1172e6ca193dSGeorge Wilson 	vdev_stat_t *vs;
11733f9d6ad7SLin Ling 	char rbuf[6], wbuf[6], cbuf[6];
1174e6ca193dSGeorge Wilson 	char *vname;
1175e6ca193dSGeorge Wilson 	uint64_t notpresent;
1176e6ca193dSGeorge Wilson 	spare_cbdata_t cb;
1177e6ca193dSGeorge Wilson 	char *state;
1178e6ca193dSGeorge Wilson 
1179e6ca193dSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1180e6ca193dSGeorge Wilson 	    &child, &children) != 0)
1181e6ca193dSGeorge Wilson 		children = 0;
1182e6ca193dSGeorge Wilson 
11833f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
11843f9d6ad7SLin Ling 	    (uint64_t **)&vs, &c) == 0);
11853f9d6ad7SLin Ling 
1186e6ca193dSGeorge Wilson 	state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1187e6ca193dSGeorge Wilson 	if (isspare) {
1188e6ca193dSGeorge Wilson 		/*
1189e6ca193dSGeorge Wilson 		 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1190e6ca193dSGeorge Wilson 		 * online drives.
1191e6ca193dSGeorge Wilson 		 */
1192e6ca193dSGeorge Wilson 		if (vs->vs_aux == VDEV_AUX_SPARED)
1193e6ca193dSGeorge Wilson 			state = "INUSE";
1194e6ca193dSGeorge Wilson 		else if (vs->vs_state == VDEV_STATE_HEALTHY)
1195e6ca193dSGeorge Wilson 			state = "AVAIL";
1196e6ca193dSGeorge Wilson 	}
1197e6ca193dSGeorge Wilson 
1198e6ca193dSGeorge Wilson 	(void) printf("\t%*s%-*s  %-8s", depth, "", namewidth - depth,
1199e6ca193dSGeorge Wilson 	    name, state);
1200e6ca193dSGeorge Wilson 
1201e6ca193dSGeorge Wilson 	if (!isspare) {
1202e6ca193dSGeorge Wilson 		zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1203e6ca193dSGeorge Wilson 		zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1204e6ca193dSGeorge Wilson 		zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1205e6ca193dSGeorge Wilson 		(void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1206e6ca193dSGeorge Wilson 	}
1207e6ca193dSGeorge Wilson 
1208e6ca193dSGeorge Wilson 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1209e6ca193dSGeorge Wilson 	    &notpresent) == 0) {
1210e6ca193dSGeorge Wilson 		char *path;
1211e6ca193dSGeorge Wilson 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1212e6ca193dSGeorge Wilson 		(void) printf("  was %s", path);
1213e6ca193dSGeorge Wilson 	} else if (vs->vs_aux != 0) {
1214e6ca193dSGeorge Wilson 		(void) printf("  ");
1215e6ca193dSGeorge Wilson 
1216e6ca193dSGeorge Wilson 		switch (vs->vs_aux) {
1217e6ca193dSGeorge Wilson 		case VDEV_AUX_OPEN_FAILED:
1218e6ca193dSGeorge Wilson 			(void) printf(gettext("cannot open"));
1219e6ca193dSGeorge Wilson 			break;
1220e6ca193dSGeorge Wilson 
1221e6ca193dSGeorge Wilson 		case VDEV_AUX_BAD_GUID_SUM:
1222e6ca193dSGeorge Wilson 			(void) printf(gettext("missing device"));
1223e6ca193dSGeorge Wilson 			break;
1224e6ca193dSGeorge Wilson 
1225e6ca193dSGeorge Wilson 		case VDEV_AUX_NO_REPLICAS:
1226e6ca193dSGeorge Wilson 			(void) printf(gettext("insufficient replicas"));
1227e6ca193dSGeorge Wilson 			break;
1228e6ca193dSGeorge Wilson 
1229e6ca193dSGeorge Wilson 		case VDEV_AUX_VERSION_NEWER:
1230e6ca193dSGeorge Wilson 			(void) printf(gettext("newer version"));
1231e6ca193dSGeorge Wilson 			break;
1232e6ca193dSGeorge Wilson 
1233ad135b5dSChristopher Siden 		case VDEV_AUX_UNSUP_FEAT:
1234ad135b5dSChristopher Siden 			(void) printf(gettext("unsupported feature(s)"));
1235ad135b5dSChristopher Siden 			break;
1236ad135b5dSChristopher Siden 
1237e6ca193dSGeorge Wilson 		case VDEV_AUX_SPARED:
1238e6ca193dSGeorge Wilson 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1239e6ca193dSGeorge Wilson 			    &cb.cb_guid) == 0);
1240e6ca193dSGeorge Wilson 			if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1241e6ca193dSGeorge Wilson 				if (strcmp(zpool_get_name(cb.cb_zhp),
1242e6ca193dSGeorge Wilson 				    zpool_get_name(zhp)) == 0)
1243e6ca193dSGeorge Wilson 					(void) printf(gettext("currently in "
1244e6ca193dSGeorge Wilson 					    "use"));
1245e6ca193dSGeorge Wilson 				else
1246e6ca193dSGeorge Wilson 					(void) printf(gettext("in use by "
1247e6ca193dSGeorge Wilson 					    "pool '%s'"),
1248e6ca193dSGeorge Wilson 					    zpool_get_name(cb.cb_zhp));
1249e6ca193dSGeorge Wilson 				zpool_close(cb.cb_zhp);
1250e6ca193dSGeorge Wilson 			} else {
1251e6ca193dSGeorge Wilson 				(void) printf(gettext("currently in use"));
1252e6ca193dSGeorge Wilson 			}
1253e6ca193dSGeorge Wilson 			break;
1254e6ca193dSGeorge Wilson 
1255e6ca193dSGeorge Wilson 		case VDEV_AUX_ERR_EXCEEDED:
1256e6ca193dSGeorge Wilson 			(void) printf(gettext("too many errors"));
1257e6ca193dSGeorge Wilson 			break;
1258e6ca193dSGeorge Wilson 
1259e6ca193dSGeorge Wilson 		case VDEV_AUX_IO_FAILURE:
1260e6ca193dSGeorge Wilson 			(void) printf(gettext("experienced I/O failures"));
1261e6ca193dSGeorge Wilson 			break;
1262e6ca193dSGeorge Wilson 
1263e6ca193dSGeorge Wilson 		case VDEV_AUX_BAD_LOG:
1264e6ca193dSGeorge Wilson 			(void) printf(gettext("bad intent log"));
1265e6ca193dSGeorge Wilson 			break;
1266e6ca193dSGeorge Wilson 
1267069f55e2SEric Schrock 		case VDEV_AUX_EXTERNAL:
1268069f55e2SEric Schrock 			(void) printf(gettext("external device fault"));
1269069f55e2SEric Schrock 			break;
1270069f55e2SEric Schrock 
12711195e687SMark J Musante 		case VDEV_AUX_SPLIT_POOL:
12721195e687SMark J Musante 			(void) printf(gettext("split into new pool"));
12731195e687SMark J Musante 			break;
12741195e687SMark J Musante 
1275e6ca193dSGeorge Wilson 		default:
1276e6ca193dSGeorge Wilson 			(void) printf(gettext("corrupted data"));
1277e6ca193dSGeorge Wilson 			break;
1278e6ca193dSGeorge Wilson 		}
12793f9d6ad7SLin Ling 	}
12803f9d6ad7SLin Ling 
12813f9d6ad7SLin Ling 	(void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
12823f9d6ad7SLin Ling 	    (uint64_t **)&ps, &c);
12833f9d6ad7SLin Ling 
12843f9d6ad7SLin Ling 	if (ps && ps->pss_state == DSS_SCANNING &&
12853f9d6ad7SLin Ling 	    vs->vs_scan_processed != 0 && children == 0) {
12863f9d6ad7SLin Ling 		(void) printf(gettext("  (%s)"),
12873f9d6ad7SLin Ling 		    (ps->pss_func == POOL_SCAN_RESILVER) ?
12883f9d6ad7SLin Ling 		    "resilvering" : "repairing");
1289e6ca193dSGeorge Wilson 	}
1290e6ca193dSGeorge Wilson 
1291e6ca193dSGeorge Wilson 	(void) printf("\n");
1292e6ca193dSGeorge Wilson 
1293e6ca193dSGeorge Wilson 	for (c = 0; c < children; c++) {
129488ecc943SGeorge Wilson 		uint64_t islog = B_FALSE, ishole = B_FALSE;
1295e6ca193dSGeorge Wilson 
129688ecc943SGeorge Wilson 		/* Don't print logs or holes here */
1297e6ca193dSGeorge Wilson 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
129888ecc943SGeorge Wilson 		    &islog);
129988ecc943SGeorge Wilson 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
130088ecc943SGeorge Wilson 		    &ishole);
130188ecc943SGeorge Wilson 		if (islog || ishole)
1302e6ca193dSGeorge Wilson 			continue;
130388ecc943SGeorge Wilson 		vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1304e6ca193dSGeorge Wilson 		print_status_config(zhp, vname, child[c],
1305e6ca193dSGeorge Wilson 		    namewidth, depth + 2, isspare);
1306e6ca193dSGeorge Wilson 		free(vname);
1307e6ca193dSGeorge Wilson 	}
1308e6ca193dSGeorge Wilson }
1309e6ca193dSGeorge Wilson 
1310fa9e4066Sahrens 
1311fa9e4066Sahrens /*
1312fa9e4066Sahrens  * Print the configuration of an exported pool.  Iterate over all vdevs in the
1313fa9e4066Sahrens  * pool, printing out the name and status for each one.
1314fa9e4066Sahrens  */
1315fa9e4066Sahrens void
1316e6ca193dSGeorge Wilson print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1317fa9e4066Sahrens {
1318fa9e4066Sahrens 	nvlist_t **child;
1319fa9e4066Sahrens 	uint_t c, children;
1320fa9e4066Sahrens 	vdev_stat_t *vs;
1321afefbcddSeschrock 	char *type, *vname;
1322fa9e4066Sahrens 
1323fa9e4066Sahrens 	verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
132488ecc943SGeorge Wilson 	if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
132588ecc943SGeorge Wilson 	    strcmp(type, VDEV_TYPE_HOLE) == 0)
1326fa9e4066Sahrens 		return;
1327fa9e4066Sahrens 
13283f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1329fa9e4066Sahrens 	    (uint64_t **)&vs, &c) == 0);
1330fa9e4066Sahrens 
1331fa9e4066Sahrens 	(void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1332990b4856Slling 	(void) printf("  %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1333fa9e4066Sahrens 
1334fa9e4066Sahrens 	if (vs->vs_aux != 0) {
13353d7072f8Seschrock 		(void) printf("  ");
1336fa9e4066Sahrens 
1337fa9e4066Sahrens 		switch (vs->vs_aux) {
1338fa9e4066Sahrens 		case VDEV_AUX_OPEN_FAILED:
1339fa9e4066Sahrens 			(void) printf(gettext("cannot open"));
1340fa9e4066Sahrens 			break;
1341fa9e4066Sahrens 
1342fa9e4066Sahrens 		case VDEV_AUX_BAD_GUID_SUM:
1343fa9e4066Sahrens 			(void) printf(gettext("missing device"));
1344fa9e4066Sahrens 			break;
1345fa9e4066Sahrens 
1346fa9e4066Sahrens 		case VDEV_AUX_NO_REPLICAS:
1347fa9e4066Sahrens 			(void) printf(gettext("insufficient replicas"));
1348fa9e4066Sahrens 			break;
1349fa9e4066Sahrens 
1350eaca9bbdSeschrock 		case VDEV_AUX_VERSION_NEWER:
1351eaca9bbdSeschrock 			(void) printf(gettext("newer version"));
1352eaca9bbdSeschrock 			break;
1353eaca9bbdSeschrock 
1354ad135b5dSChristopher Siden 		case VDEV_AUX_UNSUP_FEAT:
1355ad135b5dSChristopher Siden 			(void) printf(gettext("unsupported feature(s)"));
1356ad135b5dSChristopher Siden 			break;
1357ad135b5dSChristopher Siden 
13583d7072f8Seschrock 		case VDEV_AUX_ERR_EXCEEDED:
13593d7072f8Seschrock 			(void) printf(gettext("too many errors"));
13603d7072f8Seschrock 			break;
13613d7072f8Seschrock 
1362fa9e4066Sahrens 		default:
1363fa9e4066Sahrens 			(void) printf(gettext("corrupted data"));
1364fa9e4066Sahrens 			break;
1365fa9e4066Sahrens 		}
1366fa9e4066Sahrens 	}
1367fa9e4066Sahrens 	(void) printf("\n");
1368fa9e4066Sahrens 
1369fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1370fa9e4066Sahrens 	    &child, &children) != 0)
1371fa9e4066Sahrens 		return;
1372fa9e4066Sahrens 
1373afefbcddSeschrock 	for (c = 0; c < children; c++) {
13748654d025Sperrin 		uint64_t is_log = B_FALSE;
13758654d025Sperrin 
13768654d025Sperrin 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
13778654d025Sperrin 		    &is_log);
1378e6ca193dSGeorge Wilson 		if (is_log)
13798654d025Sperrin 			continue;
13808654d025Sperrin 
138188ecc943SGeorge Wilson 		vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1382e6ca193dSGeorge Wilson 		print_import_config(vname, child[c], namewidth, depth + 2);
1383afefbcddSeschrock 		free(vname);
1384afefbcddSeschrock 	}
138599653d4eSeschrock 
1386fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1387fa94a07fSbrendan 	    &child, &children) == 0) {
1388fa94a07fSbrendan 		(void) printf(gettext("\tcache\n"));
1389fa94a07fSbrendan 		for (c = 0; c < children; c++) {
139088ecc943SGeorge Wilson 			vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1391fa94a07fSbrendan 			(void) printf("\t  %s\n", vname);
1392fa94a07fSbrendan 			free(vname);
1393fa94a07fSbrendan 		}
1394fa94a07fSbrendan 	}
139599653d4eSeschrock 
1396fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1397fa94a07fSbrendan 	    &child, &children) == 0) {
139899653d4eSeschrock 		(void) printf(gettext("\tspares\n"));
139999653d4eSeschrock 		for (c = 0; c < children; c++) {
140088ecc943SGeorge Wilson 			vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
140199653d4eSeschrock 			(void) printf("\t  %s\n", vname);
140299653d4eSeschrock 			free(vname);
140399653d4eSeschrock 		}
1404fa9e4066Sahrens 	}
1405fa94a07fSbrendan }
1406fa9e4066Sahrens 
1407fa9e4066Sahrens /*
1408e6ca193dSGeorge Wilson  * Print log vdevs.
1409e6ca193dSGeorge Wilson  * Logs are recorded as top level vdevs in the main pool child array
1410e6ca193dSGeorge Wilson  * but with "is_log" set to 1. We use either print_status_config() or
1411e6ca193dSGeorge Wilson  * print_import_config() to print the top level logs then any log
1412e6ca193dSGeorge Wilson  * children (eg mirrored slogs) are printed recursively - which
1413e6ca193dSGeorge Wilson  * works because only the top level vdev is marked "is_log"
1414e6ca193dSGeorge Wilson  */
1415e6ca193dSGeorge Wilson static void
1416e6ca193dSGeorge Wilson print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1417e6ca193dSGeorge Wilson {
1418e6ca193dSGeorge Wilson 	uint_t c, children;
1419e6ca193dSGeorge Wilson 	nvlist_t **child;
1420e6ca193dSGeorge Wilson 
1421e6ca193dSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1422e6ca193dSGeorge Wilson 	    &children) != 0)
1423e6ca193dSGeorge Wilson 		return;
1424e6ca193dSGeorge Wilson 
1425e6ca193dSGeorge Wilson 	(void) printf(gettext("\tlogs\n"));
1426e6ca193dSGeorge Wilson 
1427e6ca193dSGeorge Wilson 	for (c = 0; c < children; c++) {
1428e6ca193dSGeorge Wilson 		uint64_t is_log = B_FALSE;
1429e6ca193dSGeorge Wilson 		char *name;
1430e6ca193dSGeorge Wilson 
1431e6ca193dSGeorge Wilson 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1432e6ca193dSGeorge Wilson 		    &is_log);
1433e6ca193dSGeorge Wilson 		if (!is_log)
1434e6ca193dSGeorge Wilson 			continue;
143588ecc943SGeorge Wilson 		name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1436e6ca193dSGeorge Wilson 		if (verbose)
1437e6ca193dSGeorge Wilson 			print_status_config(zhp, name, child[c], namewidth,
1438e6ca193dSGeorge Wilson 			    2, B_FALSE);
1439e6ca193dSGeorge Wilson 		else
1440e6ca193dSGeorge Wilson 			print_import_config(name, child[c], namewidth, 2);
1441e6ca193dSGeorge Wilson 		free(name);
1442e6ca193dSGeorge Wilson 	}
1443e6ca193dSGeorge Wilson }
1444468c413aSTim Haley 
1445e6ca193dSGeorge Wilson /*
1446fa9e4066Sahrens  * Display the status for the given pool.
1447fa9e4066Sahrens  */
1448fa9e4066Sahrens static void
1449fa9e4066Sahrens show_import(nvlist_t *config)
1450fa9e4066Sahrens {
1451fa9e4066Sahrens 	uint64_t pool_state;
1452fa9e4066Sahrens 	vdev_stat_t *vs;
1453fa9e4066Sahrens 	char *name;
1454fa9e4066Sahrens 	uint64_t guid;
1455fa9e4066Sahrens 	char *msgid;
1456fa9e4066Sahrens 	nvlist_t *nvroot;
1457fa9e4066Sahrens 	int reason;
145846657f8dSmmusante 	const char *health;
1459fa9e4066Sahrens 	uint_t vsc;
1460fa9e4066Sahrens 	int namewidth;
14618704186eSDan McDonald 	char *comment;
1462fa9e4066Sahrens 
1463fa9e4066Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1464fa9e4066Sahrens 	    &name) == 0);
1465fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1466fa9e4066Sahrens 	    &guid) == 0);
1467fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1468fa9e4066Sahrens 	    &pool_state) == 0);
1469fa9e4066Sahrens 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1470fa9e4066Sahrens 	    &nvroot) == 0);
1471fa9e4066Sahrens 
14723f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1473fa9e4066Sahrens 	    (uint64_t **)&vs, &vsc) == 0);
1474990b4856Slling 	health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1475fa9e4066Sahrens 
1476fa9e4066Sahrens 	reason = zpool_import_status(config, &msgid);
1477fa9e4066Sahrens 
147846657f8dSmmusante 	(void) printf(gettext("   pool: %s\n"), name);
147946657f8dSmmusante 	(void) printf(gettext("     id: %llu\n"), (u_longlong_t)guid);
148046657f8dSmmusante 	(void) printf(gettext("  state: %s"), health);
14814c58d714Sdarrenm 	if (pool_state == POOL_STATE_DESTROYED)
148246657f8dSmmusante 		(void) printf(gettext(" (DESTROYED)"));
14834c58d714Sdarrenm 	(void) printf("\n");
1484fa9e4066Sahrens 
1485fa9e4066Sahrens 	switch (reason) {
1486fa9e4066Sahrens 	case ZPOOL_STATUS_MISSING_DEV_R:
1487fa9e4066Sahrens 	case ZPOOL_STATUS_MISSING_DEV_NR:
1488fa9e4066Sahrens 	case ZPOOL_STATUS_BAD_GUID_SUM:
14898704186eSDan McDonald 		(void) printf(gettext(" status: One or more devices are "
14908704186eSDan McDonald 		    "missing from the system.\n"));
1491fa9e4066Sahrens 		break;
1492fa9e4066Sahrens 
1493fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_LABEL_R:
1494fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1495fa9e4066Sahrens 		(void) printf(gettext(" status: One or more devices contains "
1496fa9e4066Sahrens 		    "corrupted data.\n"));
1497fa9e4066Sahrens 		break;
1498fa9e4066Sahrens 
1499fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_DATA:
15008704186eSDan McDonald 		(void) printf(
15018704186eSDan McDonald 		    gettext(" status: The pool data is corrupted.\n"));
1502fa9e4066Sahrens 		break;
1503fa9e4066Sahrens 
1504441d80aaSlling 	case ZPOOL_STATUS_OFFLINE_DEV:
1505441d80aaSlling 		(void) printf(gettext(" status: One or more devices "
1506441d80aaSlling 		    "are offlined.\n"));
1507441d80aaSlling 		break;
1508441d80aaSlling 
1509ea8dc4b6Seschrock 	case ZPOOL_STATUS_CORRUPT_POOL:
1510ea8dc4b6Seschrock 		(void) printf(gettext(" status: The pool metadata is "
1511ea8dc4b6Seschrock 		    "corrupted.\n"));
1512ea8dc4b6Seschrock 		break;
1513ea8dc4b6Seschrock 
1514eaca9bbdSeschrock 	case ZPOOL_STATUS_VERSION_OLDER:
151557221772SChristopher Siden 		(void) printf(gettext(" status: The pool is formatted using a "
151657221772SChristopher Siden 		    "legacy on-disk version.\n"));
1517eaca9bbdSeschrock 		break;
1518eaca9bbdSeschrock 
1519eaca9bbdSeschrock 	case ZPOOL_STATUS_VERSION_NEWER:
1520eaca9bbdSeschrock 		(void) printf(gettext(" status: The pool is formatted using an "
1521eaca9bbdSeschrock 		    "incompatible version.\n"));
1522eaca9bbdSeschrock 		break;
1523b87f3af3Sperrin 
152457221772SChristopher Siden 	case ZPOOL_STATUS_FEAT_DISABLED:
152557221772SChristopher Siden 		(void) printf(gettext(" status: Some supported features are "
152657221772SChristopher Siden 		    "not enabled on the pool.\n"));
152757221772SChristopher Siden 		break;
152857221772SChristopher Siden 
1529ad135b5dSChristopher Siden 	case ZPOOL_STATUS_UNSUP_FEAT_READ:
1530ad135b5dSChristopher Siden 		(void) printf(gettext("status: The pool uses the following "
1531ad135b5dSChristopher Siden 		    "feature(s) not supported on this sytem:\n"));
1532ad135b5dSChristopher Siden 		zpool_print_unsup_feat(config);
1533ad135b5dSChristopher Siden 		break;
1534ad135b5dSChristopher Siden 
1535ad135b5dSChristopher Siden 	case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1536ad135b5dSChristopher Siden 		(void) printf(gettext("status: The pool can only be accessed "
1537ad135b5dSChristopher Siden 		    "in read-only mode on this system. It\n\tcannot be "
1538ad135b5dSChristopher Siden 		    "accessed in read-write mode because it uses the "
1539ad135b5dSChristopher Siden 		    "following\n\tfeature(s) not supported on this system:\n"));
1540ad135b5dSChristopher Siden 		zpool_print_unsup_feat(config);
1541ad135b5dSChristopher Siden 		break;
1542ad135b5dSChristopher Siden 
154395173954Sek110237 	case ZPOOL_STATUS_HOSTID_MISMATCH:
154495173954Sek110237 		(void) printf(gettext(" status: The pool was last accessed by "
154595173954Sek110237 		    "another system.\n"));
154695173954Sek110237 		break;
1547b87f3af3Sperrin 
15483d7072f8Seschrock 	case ZPOOL_STATUS_FAULTED_DEV_R:
15493d7072f8Seschrock 	case ZPOOL_STATUS_FAULTED_DEV_NR:
15503d7072f8Seschrock 		(void) printf(gettext(" status: One or more devices are "
15513d7072f8Seschrock 		    "faulted.\n"));
15523d7072f8Seschrock 		break;
15533d7072f8Seschrock 
1554b87f3af3Sperrin 	case ZPOOL_STATUS_BAD_LOG:
1555b87f3af3Sperrin 		(void) printf(gettext(" status: An intent log record cannot be "
1556b87f3af3Sperrin 		    "read.\n"));
1557b87f3af3Sperrin 		break;
1558b87f3af3Sperrin 
15593f9d6ad7SLin Ling 	case ZPOOL_STATUS_RESILVERING:
15603f9d6ad7SLin Ling 		(void) printf(gettext(" status: One or more devices were being "
15613f9d6ad7SLin Ling 		    "resilvered.\n"));
15623f9d6ad7SLin Ling 		break;
15633f9d6ad7SLin Ling 
1564fa9e4066Sahrens 	default:
1565fa9e4066Sahrens 		/*
1566fa9e4066Sahrens 		 * No other status can be seen when importing pools.
1567fa9e4066Sahrens 		 */
1568fa9e4066Sahrens 		assert(reason == ZPOOL_STATUS_OK);
1569fa9e4066Sahrens 	}
1570fa9e4066Sahrens 
1571fa9e4066Sahrens 	/*
1572fa9e4066Sahrens 	 * Print out an action according to the overall state of the pool.
1573fa9e4066Sahrens 	 */
157446657f8dSmmusante 	if (vs->vs_state == VDEV_STATE_HEALTHY) {
157557221772SChristopher Siden 		if (reason == ZPOOL_STATUS_VERSION_OLDER ||
157657221772SChristopher Siden 		    reason == ZPOOL_STATUS_FEAT_DISABLED) {
1577eaca9bbdSeschrock 			(void) printf(gettext(" action: The pool can be "
1578eaca9bbdSeschrock 			    "imported using its name or numeric identifier, "
1579eaca9bbdSeschrock 			    "though\n\tsome features will not be available "
1580eaca9bbdSeschrock 			    "without an explicit 'zpool upgrade'.\n"));
158157221772SChristopher Siden 		} else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
158295173954Sek110237 			(void) printf(gettext(" action: The pool can be "
158395173954Sek110237 			    "imported using its name or numeric "
158495173954Sek110237 			    "identifier and\n\tthe '-f' flag.\n"));
158557221772SChristopher Siden 		} else {
1586eaca9bbdSeschrock 			(void) printf(gettext(" action: The pool can be "
1587eaca9bbdSeschrock 			    "imported using its name or numeric "
1588eaca9bbdSeschrock 			    "identifier.\n"));
158957221772SChristopher Siden 		}
159046657f8dSmmusante 	} else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1591fa9e4066Sahrens 		(void) printf(gettext(" action: The pool can be imported "
1592fa9e4066Sahrens 		    "despite missing or damaged devices.  The\n\tfault "
1593eaca9bbdSeschrock 		    "tolerance of the pool may be compromised if imported.\n"));
1594fa9e4066Sahrens 	} else {
1595eaca9bbdSeschrock 		switch (reason) {
1596eaca9bbdSeschrock 		case ZPOOL_STATUS_VERSION_NEWER:
1597eaca9bbdSeschrock 			(void) printf(gettext(" action: The pool cannot be "
1598eaca9bbdSeschrock 			    "imported.  Access the pool on a system running "
1599eaca9bbdSeschrock 			    "newer\n\tsoftware, or recreate the pool from "
1600eaca9bbdSeschrock 			    "backup.\n"));
1601eaca9bbdSeschrock 			break;
1602ad135b5dSChristopher Siden 		case ZPOOL_STATUS_UNSUP_FEAT_READ:
1603ad135b5dSChristopher Siden 			(void) printf(gettext("action: The pool cannot be "
1604ad135b5dSChristopher Siden 			    "imported. Access the pool on a system that "
1605ad135b5dSChristopher Siden 			    "supports\n\tthe required feature(s), or recreate "
1606ad135b5dSChristopher Siden 			    "the pool from backup.\n"));
1607ad135b5dSChristopher Siden 			break;
1608ad135b5dSChristopher Siden 		case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1609ad135b5dSChristopher Siden 			(void) printf(gettext("action: The pool cannot be "
1610ad135b5dSChristopher Siden 			    "imported in read-write mode. Import the pool "
1611ad135b5dSChristopher Siden 			    "with\n"
1612ad135b5dSChristopher Siden 			    "\t\"-o readonly=on\", access the pool on a system "
1613ad135b5dSChristopher Siden 			    "that supports the\n\trequired feature(s), or "
1614ad135b5dSChristopher Siden 			    "recreate the pool from backup.\n"));
1615ad135b5dSChristopher Siden 			break;
1616eaca9bbdSeschrock 		case ZPOOL_STATUS_MISSING_DEV_R:
1617eaca9bbdSeschrock 		case ZPOOL_STATUS_MISSING_DEV_NR:
1618eaca9bbdSeschrock 		case ZPOOL_STATUS_BAD_GUID_SUM:
1619fa9e4066Sahrens 			(void) printf(gettext(" action: The pool cannot be "
1620fa9e4066Sahrens 			    "imported. Attach the missing\n\tdevices and try "
1621fa9e4066Sahrens 			    "again.\n"));
1622eaca9bbdSeschrock 			break;
1623eaca9bbdSeschrock 		default:
1624fa9e4066Sahrens 			(void) printf(gettext(" action: The pool cannot be "
1625fa9e4066Sahrens 			    "imported due to damaged devices or data.\n"));
1626fa9e4066Sahrens 		}
1627eaca9bbdSeschrock 	}
1628eaca9bbdSeschrock 
16298704186eSDan McDonald 	/* Print the comment attached to the pool. */
16308704186eSDan McDonald 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
16318704186eSDan McDonald 		(void) printf(gettext("comment: %s\n"), comment);
16328704186eSDan McDonald 
163346657f8dSmmusante 	/*
163446657f8dSmmusante 	 * If the state is "closed" or "can't open", and the aux state
163546657f8dSmmusante 	 * is "corrupt data":
163646657f8dSmmusante 	 */
163746657f8dSmmusante 	if (((vs->vs_state == VDEV_STATE_CLOSED) ||
163846657f8dSmmusante 	    (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
163946657f8dSmmusante 	    (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1640eaca9bbdSeschrock 		if (pool_state == POOL_STATE_DESTROYED)
1641eaca9bbdSeschrock 			(void) printf(gettext("\tThe pool was destroyed, "
1642eaca9bbdSeschrock 			    "but can be imported using the '-Df' flags.\n"));
1643eaca9bbdSeschrock 		else if (pool_state != POOL_STATE_EXPORTED)
1644eaca9bbdSeschrock 			(void) printf(gettext("\tThe pool may be active on "
164518ce54dfSek110237 			    "another system, but can be imported using\n\t"
1646eaca9bbdSeschrock 			    "the '-f' flag.\n"));
1647eaca9bbdSeschrock 	}
1648fa9e4066Sahrens 
1649fa9e4066Sahrens 	if (msgid != NULL)
1650654b400cSJoshua M. Clulow 		(void) printf(gettext("   see: http://illumos.org/msg/%s\n"),
1651fa9e4066Sahrens 		    msgid);
1652fa9e4066Sahrens 
1653fa9e4066Sahrens 	(void) printf(gettext(" config:\n\n"));
1654fa9e4066Sahrens 
1655c67d9675Seschrock 	namewidth = max_width(NULL, nvroot, 0, 0);
1656fa9e4066Sahrens 	if (namewidth < 10)
1657fa9e4066Sahrens 		namewidth = 10;
16588654d025Sperrin 
1659e6ca193dSGeorge Wilson 	print_import_config(name, nvroot, namewidth, 0);
1660e6ca193dSGeorge Wilson 	if (num_logs(nvroot) > 0)
1661e6ca193dSGeorge Wilson 		print_logs(NULL, nvroot, namewidth, B_FALSE);
1662fa9e4066Sahrens 
1663fa9e4066Sahrens 	if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
166446657f8dSmmusante 		(void) printf(gettext("\n\tAdditional devices are known to "
1665fa9e4066Sahrens 		    "be part of this pool, though their\n\texact "
166646657f8dSmmusante 		    "configuration cannot be determined.\n"));
1667fa9e4066Sahrens 	}
1668fa9e4066Sahrens }
1669fa9e4066Sahrens 
1670fa9e4066Sahrens /*
1671fa9e4066Sahrens  * Perform the import for the given configuration.  This passes the heavy
1672990b4856Slling  * lifting off to zpool_import_props(), and then mounts the datasets contained
1673990b4856Slling  * within the pool.
1674fa9e4066Sahrens  */
1675fa9e4066Sahrens static int
1676fa9e4066Sahrens do_import(nvlist_t *config, const char *newname, const char *mntopts,
16774b964adaSGeorge Wilson     nvlist_t *props, int flags)
1678fa9e4066Sahrens {
1679fa9e4066Sahrens 	zpool_handle_t *zhp;
1680fa9e4066Sahrens 	char *name;
1681fa9e4066Sahrens 	uint64_t state;
1682eaca9bbdSeschrock 	uint64_t version;
1683fa9e4066Sahrens 
1684fa9e4066Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1685fa9e4066Sahrens 	    &name) == 0);
1686fa9e4066Sahrens 
1687fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config,
1688fa9e4066Sahrens 	    ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1689eaca9bbdSeschrock 	verify(nvlist_lookup_uint64(config,
1690eaca9bbdSeschrock 	    ZPOOL_CONFIG_VERSION, &version) == 0);
1691ad135b5dSChristopher Siden 	if (!SPA_VERSION_IS_SUPPORTED(version)) {
1692eaca9bbdSeschrock 		(void) fprintf(stderr, gettext("cannot import '%s': pool "
1693ad135b5dSChristopher Siden 		    "is formatted using an unsupported ZFS version\n"), name);
1694eaca9bbdSeschrock 		return (1);
16954b964adaSGeorge Wilson 	} else if (state != POOL_STATE_EXPORTED &&
16964b964adaSGeorge Wilson 	    !(flags & ZFS_IMPORT_ANY_HOST)) {
169795173954Sek110237 		uint64_t hostid;
169895173954Sek110237 
169995173954Sek110237 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
170095173954Sek110237 		    &hostid) == 0) {
170195173954Sek110237 			if ((unsigned long)hostid != gethostid()) {
170295173954Sek110237 				char *hostname;
170395173954Sek110237 				uint64_t timestamp;
170495173954Sek110237 				time_t t;
170595173954Sek110237 
170695173954Sek110237 				verify(nvlist_lookup_string(config,
170795173954Sek110237 				    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
170895173954Sek110237 				verify(nvlist_lookup_uint64(config,
170995173954Sek110237 				    ZPOOL_CONFIG_TIMESTAMP, &timestamp) == 0);
171095173954Sek110237 				t = timestamp;
171195173954Sek110237 				(void) fprintf(stderr, gettext("cannot import "
171295173954Sek110237 				    "'%s': pool may be in use from other "
171395173954Sek110237 				    "system, it was last accessed by %s "
171495173954Sek110237 				    "(hostid: 0x%lx) on %s"), name, hostname,
171595173954Sek110237 				    (unsigned long)hostid,
171695173954Sek110237 				    asctime(localtime(&t)));
171795173954Sek110237 				(void) fprintf(stderr, gettext("use '-f' to "
171895173954Sek110237 				    "import anyway\n"));
1719fa9e4066Sahrens 				return (1);
1720fa9e4066Sahrens 			}
172195173954Sek110237 		} else {
172295173954Sek110237 			(void) fprintf(stderr, gettext("cannot import '%s': "
172395173954Sek110237 			    "pool may be in use from other system\n"), name);
172495173954Sek110237 			(void) fprintf(stderr, gettext("use '-f' to import "
172595173954Sek110237 			    "anyway\n"));
172695173954Sek110237 			return (1);
172795173954Sek110237 		}
172895173954Sek110237 	}
1729fa9e4066Sahrens 
17304b964adaSGeorge Wilson 	if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1731fa9e4066Sahrens 		return (1);
1732fa9e4066Sahrens 
1733fa9e4066Sahrens 	if (newname != NULL)
1734fa9e4066Sahrens 		name = (char *)newname;
1735fa9e4066Sahrens 
17364f0f5e5bSVictor Latushkin 	if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
17374f0f5e5bSVictor Latushkin 		return (1);
1738fa9e4066Sahrens 
1739379c004dSEric Schrock 	if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1740f9af39baSGeorge Wilson 	    !(flags & ZFS_IMPORT_ONLY) &&
1741379c004dSEric Schrock 	    zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1742fa9e4066Sahrens 		zpool_close(zhp);
1743fa9e4066Sahrens 		return (1);
1744fa9e4066Sahrens 	}
1745fa9e4066Sahrens 
1746fa9e4066Sahrens 	zpool_close(zhp);
1747468c413aSTim Haley 	return (0);
1748fa9e4066Sahrens }
1749fa9e4066Sahrens 
1750fa9e4066Sahrens /*
17514c58d714Sdarrenm  * zpool import [-d dir] [-D]
17522f8aaab3Seschrock  *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
17532f8aaab3Seschrock  *              [-d dir | -c cachefile] [-f] -a
17542f8aaab3Seschrock  *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1755468c413aSTim Haley  *              [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
17562f8aaab3Seschrock  *
17572f8aaab3Seschrock  *	 -c	Read pool information from a cachefile instead of searching
17582f8aaab3Seschrock  *		devices.
1759fa9e4066Sahrens  *
1760fa9e4066Sahrens  *       -d	Scan in a specific directory, other than /dev/dsk.  More than
1761fa9e4066Sahrens  *		one directory can be specified using multiple '-d' options.
1762fa9e4066Sahrens  *
17634c58d714Sdarrenm  *       -D     Scan for previously destroyed pools or import all or only
17644c58d714Sdarrenm  *              specified destroyed pools.
17654c58d714Sdarrenm  *
1766fa9e4066Sahrens  *       -R	Temporarily import the pool, with all mountpoints relative to
1767fa9e4066Sahrens  *		the given root.  The pool will remain exported when the machine
1768fa9e4066Sahrens  *		is rebooted.
1769fa9e4066Sahrens  *
1770468c413aSTim Haley  *       -V	Import even in the presence of faulted vdevs.  This is an
1771c5904d13Seschrock  *       	intentionally undocumented option for testing purposes, and
1772c5904d13Seschrock  *       	treats the pool configuration as complete, leaving any bad
17734f0f5e5bSVictor Latushkin  *		vdevs in the FAULTED state. In other words, it does verbatim
17744f0f5e5bSVictor Latushkin  *		import.
1775c5904d13Seschrock  *
1776468c413aSTim Haley  *       -f	Force import, even if it appears that the pool is active.
1777468c413aSTim Haley  *
1778468c413aSTim Haley  *       -F     Attempt rewind if necessary.
1779468c413aSTim Haley  *
1780468c413aSTim Haley  *       -n     See if rewind would work, but don't actually rewind.
1781468c413aSTim Haley  *
1782f9af39baSGeorge Wilson  *       -N     Import the pool but don't mount datasets.
1783f9af39baSGeorge Wilson  *
1784f9af39baSGeorge Wilson  *       -T     Specify a starting txg to use for import. This option is
1785f9af39baSGeorge Wilson  *       	intentionally undocumented option for testing purposes.
1786f9af39baSGeorge Wilson  *
1787fa9e4066Sahrens  *       -a	Import all pools found.
1788fa9e4066Sahrens  *
1789990b4856Slling  *       -o	Set property=value and/or temporary mount options (without '=').
1790ecd6cf80Smarks  *
1791fa9e4066Sahrens  * The import command scans for pools to import, and import pools based on pool
1792fa9e4066Sahrens  * name and GUID.  The pool can also be renamed as part of the import process.
1793fa9e4066Sahrens  */
1794fa9e4066Sahrens int
1795fa9e4066Sahrens zpool_do_import(int argc, char **argv)
1796fa9e4066Sahrens {
1797fa9e4066Sahrens 	char **searchdirs = NULL;
1798fa9e4066Sahrens 	int nsearch = 0;
1799fa9e4066Sahrens 	int c;
1800d41c4376SMark J Musante 	int err = 0;
18012f8aaab3Seschrock 	nvlist_t *pools = NULL;
180299653d4eSeschrock 	boolean_t do_all = B_FALSE;
180399653d4eSeschrock 	boolean_t do_destroyed = B_FALSE;
1804fa9e4066Sahrens 	char *mntopts = NULL;
1805fa9e4066Sahrens 	nvpair_t *elem;
1806fa9e4066Sahrens 	nvlist_t *config;
180724e697d4Sck153898 	uint64_t searchguid = 0;
180824e697d4Sck153898 	char *searchname = NULL;
1809990b4856Slling 	char *propval;
1810fa9e4066Sahrens 	nvlist_t *found_config;
1811468c413aSTim Haley 	nvlist_t *policy = NULL;
1812ecd6cf80Smarks 	nvlist_t *props = NULL;
181399653d4eSeschrock 	boolean_t first;
18144b964adaSGeorge Wilson 	int flags = ZFS_IMPORT_NORMAL;
1815468c413aSTim Haley 	uint32_t rewind_policy = ZPOOL_NO_REWIND;
1816468c413aSTim Haley 	boolean_t dryrun = B_FALSE;
1817468c413aSTim Haley 	boolean_t do_rewind = B_FALSE;
1818468c413aSTim Haley 	boolean_t xtreme_rewind = B_FALSE;
1819f9af39baSGeorge Wilson 	uint64_t pool_state, txg = -1ULL;
18202f8aaab3Seschrock 	char *cachefile = NULL;
1821d41c4376SMark J Musante 	importargs_t idata = { 0 };
1822f9af39baSGeorge Wilson 	char *endptr;
1823fa9e4066Sahrens 
1824fa9e4066Sahrens 	/* check options */
1825f9af39baSGeorge Wilson 	while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1826fa9e4066Sahrens 		switch (c) {
1827fa9e4066Sahrens 		case 'a':
182899653d4eSeschrock 			do_all = B_TRUE;
1829fa9e4066Sahrens 			break;
18302f8aaab3Seschrock 		case 'c':
18312f8aaab3Seschrock 			cachefile = optarg;
18322f8aaab3Seschrock 			break;
1833fa9e4066Sahrens 		case 'd':
1834fa9e4066Sahrens 			if (searchdirs == NULL) {
1835fa9e4066Sahrens 				searchdirs = safe_malloc(sizeof (char *));
1836fa9e4066Sahrens 			} else {
1837fa9e4066Sahrens 				char **tmp = safe_malloc((nsearch + 1) *
1838fa9e4066Sahrens 				    sizeof (char *));
1839fa9e4066Sahrens 				bcopy(searchdirs, tmp, nsearch *
1840fa9e4066Sahrens 				    sizeof (char *));
1841fa9e4066Sahrens 				free(searchdirs);
1842fa9e4066Sahrens 				searchdirs = tmp;
1843fa9e4066Sahrens 			}
1844fa9e4066Sahrens 			searchdirs[nsearch++] = optarg;
1845fa9e4066Sahrens 			break;
18464c58d714Sdarrenm 		case 'D':
184799653d4eSeschrock 			do_destroyed = B_TRUE;
18484c58d714Sdarrenm 			break;
1849fa9e4066Sahrens 		case 'f':
18504b964adaSGeorge Wilson 			flags |= ZFS_IMPORT_ANY_HOST;
1851fa9e4066Sahrens 			break;
1852c5904d13Seschrock 		case 'F':
1853468c413aSTim Haley 			do_rewind = B_TRUE;
1854468c413aSTim Haley 			break;
18554b964adaSGeorge Wilson 		case 'm':
18564b964adaSGeorge Wilson 			flags |= ZFS_IMPORT_MISSING_LOG;
18574b964adaSGeorge Wilson 			break;
1858468c413aSTim Haley 		case 'n':
1859468c413aSTim Haley 			dryrun = B_TRUE;
1860c5904d13Seschrock 			break;
1861f9af39baSGeorge Wilson 		case 'N':
1862f9af39baSGeorge Wilson 			flags |= ZFS_IMPORT_ONLY;
1863f9af39baSGeorge Wilson 			break;
1864fa9e4066Sahrens 		case 'o':
1865990b4856Slling 			if ((propval = strchr(optarg, '=')) != NULL) {
1866990b4856Slling 				*propval = '\0';
1867990b4856Slling 				propval++;
18680a48a24eStimh 				if (add_prop_list(optarg, propval,
18690a48a24eStimh 				    &props, B_TRUE))
1870990b4856Slling 					goto error;
1871990b4856Slling 			} else {
1872fa9e4066Sahrens 				mntopts = optarg;
1873990b4856Slling 			}
1874fa9e4066Sahrens 			break;
1875fa9e4066Sahrens 		case 'R':
1876990b4856Slling 			if (add_prop_list(zpool_prop_to_name(
18770a48a24eStimh 			    ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1878990b4856Slling 				goto error;
18792f8aaab3Seschrock 			if (nvlist_lookup_string(props,
18802f8aaab3Seschrock 			    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
18812f8aaab3Seschrock 			    &propval) == 0)
18822f8aaab3Seschrock 				break;
1883990b4856Slling 			if (add_prop_list(zpool_prop_to_name(
18840a48a24eStimh 			    ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1885990b4856Slling 				goto error;
1886fa9e4066Sahrens 			break;
1887f9af39baSGeorge Wilson 		case 'T':
1888f9af39baSGeorge Wilson 			errno = 0;
1889f9af39baSGeorge Wilson 			txg = strtoull(optarg, &endptr, 10);
1890f9af39baSGeorge Wilson 			if (errno != 0 || *endptr != '\0') {
1891f9af39baSGeorge Wilson 				(void) fprintf(stderr,
1892f9af39baSGeorge Wilson 				    gettext("invalid txg value\n"));
1893f9af39baSGeorge Wilson 				usage(B_FALSE);
1894f9af39baSGeorge Wilson 			}
1895f9af39baSGeorge Wilson 			rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
1896f9af39baSGeorge Wilson 			break;
1897468c413aSTim Haley 		case 'V':
18984b964adaSGeorge Wilson 			flags |= ZFS_IMPORT_VERBATIM;
1899468c413aSTim Haley 			break;
1900468c413aSTim Haley 		case 'X':
1901468c413aSTim Haley 			xtreme_rewind = B_TRUE;
1902468c413aSTim Haley 			break;
1903fa9e4066Sahrens 		case ':':
1904fa9e4066Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
1905fa9e4066Sahrens 			    "'%c' option\n"), optopt);
190699653d4eSeschrock 			usage(B_FALSE);
1907fa9e4066Sahrens 			break;
1908fa9e4066Sahrens 		case '?':
1909fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1910fa9e4066Sahrens 			    optopt);
191199653d4eSeschrock 			usage(B_FALSE);
1912fa9e4066Sahrens 		}
1913fa9e4066Sahrens 	}
1914fa9e4066Sahrens 
1915fa9e4066Sahrens 	argc -= optind;
1916fa9e4066Sahrens 	argv += optind;
1917fa9e4066Sahrens 
19182f8aaab3Seschrock 	if (cachefile && nsearch != 0) {
19192f8aaab3Seschrock 		(void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
19202f8aaab3Seschrock 		usage(B_FALSE);
19212f8aaab3Seschrock 	}
19222f8aaab3Seschrock 
1923468c413aSTim Haley 	if ((dryrun || xtreme_rewind) && !do_rewind) {
1924468c413aSTim Haley 		(void) fprintf(stderr,
1925468c413aSTim Haley 		    gettext("-n or -X only meaningful with -F\n"));
1926468c413aSTim Haley 		usage(B_FALSE);
1927468c413aSTim Haley 	}
1928468c413aSTim Haley 	if (dryrun)
1929468c413aSTim Haley 		rewind_policy = ZPOOL_TRY_REWIND;
1930468c413aSTim Haley 	else if (do_rewind)
1931468c413aSTim Haley 		rewind_policy = ZPOOL_DO_REWIND;
1932468c413aSTim Haley 	if (xtreme_rewind)
1933468c413aSTim Haley 		rewind_policy |= ZPOOL_EXTREME_REWIND;
1934468c413aSTim Haley 
1935468c413aSTim Haley 	/* In the future, we can capture further policy and include it here */
1936468c413aSTim Haley 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
1937f9af39baSGeorge Wilson 	    nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
1938468c413aSTim Haley 	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
1939468c413aSTim Haley 		goto error;
1940468c413aSTim Haley 
1941fa9e4066Sahrens 	if (searchdirs == NULL) {
1942fa9e4066Sahrens 		searchdirs = safe_malloc(sizeof (char *));
1943fa9e4066Sahrens 		searchdirs[0] = "/dev/dsk";
1944fa9e4066Sahrens 		nsearch = 1;
1945fa9e4066Sahrens 	}
1946fa9e4066Sahrens 
1947fa9e4066Sahrens 	/* check argument count */
1948fa9e4066Sahrens 	if (do_all) {
1949fa9e4066Sahrens 		if (argc != 0) {
1950fa9e4066Sahrens 			(void) fprintf(stderr, gettext("too many arguments\n"));
195199653d4eSeschrock 			usage(B_FALSE);
1952fa9e4066Sahrens 		}
1953fa9e4066Sahrens 	} else {
1954fa9e4066Sahrens 		if (argc > 2) {
1955fa9e4066Sahrens 			(void) fprintf(stderr, gettext("too many arguments\n"));
195699653d4eSeschrock 			usage(B_FALSE);
1957fa9e4066Sahrens 		}
1958fa9e4066Sahrens 
1959fa9e4066Sahrens 		/*
1960fa9e4066Sahrens 		 * Check for the SYS_CONFIG privilege.  We do this explicitly
1961fa9e4066Sahrens 		 * here because otherwise any attempt to discover pools will
1962fa9e4066Sahrens 		 * silently fail.
1963fa9e4066Sahrens 		 */
1964fa9e4066Sahrens 		if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
1965fa9e4066Sahrens 			(void) fprintf(stderr, gettext("cannot "
1966fa9e4066Sahrens 			    "discover pools: permission denied\n"));
196799653d4eSeschrock 			free(searchdirs);
1968468c413aSTim Haley 			nvlist_free(policy);
1969fa9e4066Sahrens 			return (1);
1970fa9e4066Sahrens 		}
1971fa9e4066Sahrens 	}
1972fa9e4066Sahrens 
1973fa9e4066Sahrens 	/*
1974fa9e4066Sahrens 	 * Depending on the arguments given, we do one of the following:
1975fa9e4066Sahrens 	 *
1976fa9e4066Sahrens 	 *	<none>	Iterate through all pools and display information about
1977fa9e4066Sahrens 	 *		each one.
1978fa9e4066Sahrens 	 *
1979fa9e4066Sahrens 	 *	-a	Iterate through all pools and try to import each one.
1980fa9e4066Sahrens 	 *
1981fa9e4066Sahrens 	 *	<id>	Find the pool that corresponds to the given GUID/pool
1982fa9e4066Sahrens 	 *		name and import that one.
19834c58d714Sdarrenm 	 *
19844c58d714Sdarrenm 	 *	-D	Above options applies only to destroyed pools.
1985fa9e4066Sahrens 	 */
1986fa9e4066Sahrens 	if (argc != 0) {
1987fa9e4066Sahrens 		char *endptr;
1988fa9e4066Sahrens 
1989fa9e4066Sahrens 		errno = 0;
1990fa9e4066Sahrens 		searchguid = strtoull(argv[0], &endptr, 10);
1991fa9e4066Sahrens 		if (errno != 0 || *endptr != '\0')
1992fa9e4066Sahrens 			searchname = argv[0];
1993fa9e4066Sahrens 		found_config = NULL;
1994fa9e4066Sahrens 
199524e697d4Sck153898 		/*
1996d41c4376SMark J Musante 		 * User specified a name or guid.  Ensure it's unique.
199724e697d4Sck153898 		 */
1998d41c4376SMark J Musante 		idata.unique = B_TRUE;
199924e697d4Sck153898 	}
200024e697d4Sck153898 
2001d41c4376SMark J Musante 
2002d41c4376SMark J Musante 	idata.path = searchdirs;
2003d41c4376SMark J Musante 	idata.paths = nsearch;
2004d41c4376SMark J Musante 	idata.poolname = searchname;
2005d41c4376SMark J Musante 	idata.guid = searchguid;
2006d41c4376SMark J Musante 	idata.cachefile = cachefile;
2007d41c4376SMark J Musante 
2008d41c4376SMark J Musante 	pools = zpool_search_import(g_zfs, &idata);
2009d41c4376SMark J Musante 
2010d41c4376SMark J Musante 	if (pools != NULL && idata.exists &&
2011d41c4376SMark J Musante 	    (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2012d41c4376SMark J Musante 		(void) fprintf(stderr, gettext("cannot import '%s': "
2013d41c4376SMark J Musante 		    "a pool with that name already exists\n"),
2014d41c4376SMark J Musante 		    argv[0]);
2015d41c4376SMark J Musante 		(void) fprintf(stderr, gettext("use the form '%s "
2016d41c4376SMark J Musante 		    "<pool | id> <newpool>' to give it a new name\n"),
2017d41c4376SMark J Musante 		    "zpool import");
2018d41c4376SMark J Musante 		err = 1;
2019d41c4376SMark J Musante 	} else if (pools == NULL && idata.exists) {
2020d41c4376SMark J Musante 		(void) fprintf(stderr, gettext("cannot import '%s': "
2021d41c4376SMark J Musante 		    "a pool with that name is already created/imported,\n"),
2022d41c4376SMark J Musante 		    argv[0]);
2023d41c4376SMark J Musante 		(void) fprintf(stderr, gettext("and no additional pools "
2024d41c4376SMark J Musante 		    "with that name were found\n"));
2025d41c4376SMark J Musante 		err = 1;
2026d41c4376SMark J Musante 	} else if (pools == NULL) {
202724e697d4Sck153898 		if (argc != 0) {
202824e697d4Sck153898 			(void) fprintf(stderr, gettext("cannot import '%s': "
202924e697d4Sck153898 			    "no such pool available\n"), argv[0]);
203024e697d4Sck153898 		}
2031d41c4376SMark J Musante 		err = 1;
2032d41c4376SMark J Musante 	}
2033d41c4376SMark J Musante 
2034d41c4376SMark J Musante 	if (err == 1) {
203524e697d4Sck153898 		free(searchdirs);
2036468c413aSTim Haley 		nvlist_free(policy);
203724e697d4Sck153898 		return (1);
203824e697d4Sck153898 	}
203924e697d4Sck153898 
204024e697d4Sck153898 	/*
204124e697d4Sck153898 	 * At this point we have a list of import candidate configs. Even if
204224e697d4Sck153898 	 * we were searching by pool name or guid, we still need to
204324e697d4Sck153898 	 * post-process the list to deal with pool state and possible
204424e697d4Sck153898 	 * duplicate names.
204524e697d4Sck153898 	 */
2046fa9e4066Sahrens 	err = 0;
2047fa9e4066Sahrens 	elem = NULL;
204899653d4eSeschrock 	first = B_TRUE;
2049fa9e4066Sahrens 	while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2050fa9e4066Sahrens 
2051fa9e4066Sahrens 		verify(nvpair_value_nvlist(elem, &config) == 0);
2052fa9e4066Sahrens 
20534c58d714Sdarrenm 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
20544c58d714Sdarrenm 		    &pool_state) == 0);
20554c58d714Sdarrenm 		if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
20564c58d714Sdarrenm 			continue;
20574c58d714Sdarrenm 		if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
20584c58d714Sdarrenm 			continue;
20594c58d714Sdarrenm 
2060468c413aSTim Haley 		verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2061468c413aSTim Haley 		    policy) == 0);
2062468c413aSTim Haley 
2063fa9e4066Sahrens 		if (argc == 0) {
2064fa9e4066Sahrens 			if (first)
206599653d4eSeschrock 				first = B_FALSE;
20663bb79becSeschrock 			else if (!do_all)
2067fa9e4066Sahrens 				(void) printf("\n");
2068fa9e4066Sahrens 
2069468c413aSTim Haley 			if (do_all) {
2070fa9e4066Sahrens 				err |= do_import(config, NULL, mntopts,
20714b964adaSGeorge Wilson 				    props, flags);
2072468c413aSTim Haley 			} else {
2073fa9e4066Sahrens 				show_import(config);
2074468c413aSTim Haley 			}
2075fa9e4066Sahrens 		} else if (searchname != NULL) {
2076fa9e4066Sahrens 			char *name;
2077fa9e4066Sahrens 
2078fa9e4066Sahrens 			/*
2079fa9e4066Sahrens 			 * We are searching for a pool based on name.
2080fa9e4066Sahrens 			 */
2081fa9e4066Sahrens 			verify(nvlist_lookup_string(config,
2082fa9e4066Sahrens 			    ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2083fa9e4066Sahrens 
2084fa9e4066Sahrens 			if (strcmp(name, searchname) == 0) {
2085fa9e4066Sahrens 				if (found_config != NULL) {
2086fa9e4066Sahrens 					(void) fprintf(stderr, gettext(
2087fa9e4066Sahrens 					    "cannot import '%s': more than "
2088fa9e4066Sahrens 					    "one matching pool\n"), searchname);
2089fa9e4066Sahrens 					(void) fprintf(stderr, gettext(
2090fa9e4066Sahrens 					    "import by numeric ID instead\n"));
209199653d4eSeschrock 					err = B_TRUE;
2092fa9e4066Sahrens 				}
2093fa9e4066Sahrens 				found_config = config;
2094fa9e4066Sahrens 			}
2095fa9e4066Sahrens 		} else {
2096fa9e4066Sahrens 			uint64_t guid;
2097fa9e4066Sahrens 
2098fa9e4066Sahrens 			/*
2099fa9e4066Sahrens 			 * Search for a pool by guid.
2100fa9e4066Sahrens 			 */
2101fa9e4066Sahrens 			verify(nvlist_lookup_uint64(config,
2102fa9e4066Sahrens 			    ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2103fa9e4066Sahrens 
2104fa9e4066Sahrens 			if (guid == searchguid)
2105fa9e4066Sahrens 				found_config = config;
2106fa9e4066Sahrens 		}
2107fa9e4066Sahrens 	}
2108fa9e4066Sahrens 
2109fa9e4066Sahrens 	/*
2110fa9e4066Sahrens 	 * If we were searching for a specific pool, verify that we found a
2111fa9e4066Sahrens 	 * pool, and then do the import.
2112fa9e4066Sahrens 	 */
2113fa9e4066Sahrens 	if (argc != 0 && err == 0) {
2114fa9e4066Sahrens 		if (found_config == NULL) {
2115fa9e4066Sahrens 			(void) fprintf(stderr, gettext("cannot import '%s': "
2116fa9e4066Sahrens 			    "no such pool available\n"), argv[0]);
211799653d4eSeschrock 			err = B_TRUE;
2118fa9e4066Sahrens 		} else {
2119fa9e4066Sahrens 			err |= do_import(found_config, argc == 1 ? NULL :
21204b964adaSGeorge Wilson 			    argv[1], mntopts, props, flags);
2121fa9e4066Sahrens 		}
2122fa9e4066Sahrens 	}
2123fa9e4066Sahrens 
2124fa9e4066Sahrens 	/*
2125fa9e4066Sahrens 	 * If we were just looking for pools, report an error if none were
2126fa9e4066Sahrens 	 * found.
2127fa9e4066Sahrens 	 */
2128fa9e4066Sahrens 	if (argc == 0 && first)
2129fa9e4066Sahrens 		(void) fprintf(stderr,
2130fa9e4066Sahrens 		    gettext("no pools available to import\n"));
2131fa9e4066Sahrens 
2132ecd6cf80Smarks error:
2133ecd6cf80Smarks 	nvlist_free(props);
2134fa9e4066Sahrens 	nvlist_free(pools);
2135468c413aSTim Haley 	nvlist_free(policy);
213699653d4eSeschrock 	free(searchdirs);
2137fa9e4066Sahrens 
2138fa9e4066Sahrens 	return (err ? 1 : 0);
2139fa9e4066Sahrens }
2140fa9e4066Sahrens 
2141fa9e4066Sahrens typedef struct iostat_cbdata {
21424263d13fSGeorge Wilson 	boolean_t cb_verbose;
2143fa9e4066Sahrens 	int cb_namewidth;
21444263d13fSGeorge Wilson 	int cb_iteration;
21454263d13fSGeorge Wilson 	zpool_list_t *cb_list;
2146fa9e4066Sahrens } iostat_cbdata_t;
2147fa9e4066Sahrens 
2148fa9e4066Sahrens static void
2149fa9e4066Sahrens print_iostat_separator(iostat_cbdata_t *cb)
2150fa9e4066Sahrens {
2151fa9e4066Sahrens 	int i = 0;
2152fa9e4066Sahrens 
2153fa9e4066Sahrens 	for (i = 0; i < cb->cb_namewidth; i++)
2154fa9e4066Sahrens 		(void) printf("-");
2155fa9e4066Sahrens 	(void) printf("  -----  -----  -----  -----  -----  -----\n");
2156fa9e4066Sahrens }
2157fa9e4066Sahrens 
2158fa9e4066Sahrens static void
2159fa9e4066Sahrens print_iostat_header(iostat_cbdata_t *cb)
2160fa9e4066Sahrens {
2161fa9e4066Sahrens 	(void) printf("%*s     capacity     operations    bandwidth\n",
2162fa9e4066Sahrens 	    cb->cb_namewidth, "");
2163485bbbf5SGeorge Wilson 	(void) printf("%-*s  alloc   free   read  write   read  write\n",
2164fa9e4066Sahrens 	    cb->cb_namewidth, "pool");
2165fa9e4066Sahrens 	print_iostat_separator(cb);
2166fa9e4066Sahrens }
2167fa9e4066Sahrens 
2168fa9e4066Sahrens /*
2169fa9e4066Sahrens  * Display a single statistic.
2170fa9e4066Sahrens  */
2171990b4856Slling static void
2172fa9e4066Sahrens print_one_stat(uint64_t value)
2173fa9e4066Sahrens {
2174fa9e4066Sahrens 	char buf[64];
2175fa9e4066Sahrens 
2176fa9e4066Sahrens 	zfs_nicenum(value, buf, sizeof (buf));
2177fa9e4066Sahrens 	(void) printf("  %5s", buf);
2178fa9e4066Sahrens }
2179fa9e4066Sahrens 
2180fa9e4066Sahrens /*
2181fa9e4066Sahrens  * Print out all the statistics for the given vdev.  This can either be the
2182fa9e4066Sahrens  * toplevel configuration, or called recursively.  If 'name' is NULL, then this
2183fa9e4066Sahrens  * is a verbose output, and we don't want to display the toplevel pool stats.
2184fa9e4066Sahrens  */
2185fa9e4066Sahrens void
2186c67d9675Seschrock print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2187c67d9675Seschrock     nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2188fa9e4066Sahrens {
2189fa9e4066Sahrens 	nvlist_t **oldchild, **newchild;
2190fa9e4066Sahrens 	uint_t c, children;
2191fa9e4066Sahrens 	vdev_stat_t *oldvs, *newvs;
2192fa9e4066Sahrens 	vdev_stat_t zerovs = { 0 };
2193fa9e4066Sahrens 	uint64_t tdelta;
2194fa9e4066Sahrens 	double scale;
2195afefbcddSeschrock 	char *vname;
2196fa9e4066Sahrens 
2197fa9e4066Sahrens 	if (oldnv != NULL) {
21983f9d6ad7SLin Ling 		verify(nvlist_lookup_uint64_array(oldnv,
21993f9d6ad7SLin Ling 		    ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2200fa9e4066Sahrens 	} else {
2201fa9e4066Sahrens 		oldvs = &zerovs;
2202fa9e4066Sahrens 	}
2203fa9e4066Sahrens 
22043f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2205fa9e4066Sahrens 	    (uint64_t **)&newvs, &c) == 0);
2206fa9e4066Sahrens 
2207fa9e4066Sahrens 	if (strlen(name) + depth > cb->cb_namewidth)
2208fa9e4066Sahrens 		(void) printf("%*s%s", depth, "", name);
2209fa9e4066Sahrens 	else
2210fa9e4066Sahrens 		(void) printf("%*s%s%*s", depth, "", name,
2211fa9e4066Sahrens 		    (int)(cb->cb_namewidth - strlen(name) - depth), "");
2212fa9e4066Sahrens 
2213fa9e4066Sahrens 	tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2214fa9e4066Sahrens 
2215fa9e4066Sahrens 	if (tdelta == 0)
2216fa9e4066Sahrens 		scale = 1.0;
2217fa9e4066Sahrens 	else
2218fa9e4066Sahrens 		scale = (double)NANOSEC / tdelta;
2219fa9e4066Sahrens 
2220fa9e4066Sahrens 	/* only toplevel vdevs have capacity stats */
2221fa9e4066Sahrens 	if (newvs->vs_space == 0) {
2222fa9e4066Sahrens 		(void) printf("      -      -");
2223fa9e4066Sahrens 	} else {
2224fa9e4066Sahrens 		print_one_stat(newvs->vs_alloc);
2225fa9e4066Sahrens 		print_one_stat(newvs->vs_space - newvs->vs_alloc);
2226fa9e4066Sahrens 	}
2227fa9e4066Sahrens 
2228fa9e4066Sahrens 	print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2229fa9e4066Sahrens 	    oldvs->vs_ops[ZIO_TYPE_READ])));
2230fa9e4066Sahrens 
2231fa9e4066Sahrens 	print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2232fa9e4066Sahrens 	    oldvs->vs_ops[ZIO_TYPE_WRITE])));
2233fa9e4066Sahrens 
2234fa9e4066Sahrens 	print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2235fa9e4066Sahrens 	    oldvs->vs_bytes[ZIO_TYPE_READ])));
2236fa9e4066Sahrens 
2237fa9e4066Sahrens 	print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2238fa9e4066Sahrens 	    oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2239fa9e4066Sahrens 
2240fa9e4066Sahrens 	(void) printf("\n");
2241fa9e4066Sahrens 
2242fa9e4066Sahrens 	if (!cb->cb_verbose)
2243fa9e4066Sahrens 		return;
2244fa9e4066Sahrens 
2245fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2246fa9e4066Sahrens 	    &newchild, &children) != 0)
2247fa9e4066Sahrens 		return;
2248fa9e4066Sahrens 
2249fa9e4066Sahrens 	if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2250fa9e4066Sahrens 	    &oldchild, &c) != 0)
2251fa9e4066Sahrens 		return;
2252fa9e4066Sahrens 
2253afefbcddSeschrock 	for (c = 0; c < children; c++) {
22549d439f90SMike Harsch 		uint64_t ishole = B_FALSE, islog = B_FALSE;
22553f9d6ad7SLin Ling 
22569d439f90SMike Harsch 		(void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
22579d439f90SMike Harsch 		    &ishole);
22589d439f90SMike Harsch 
22599d439f90SMike Harsch 		(void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
22609d439f90SMike Harsch 		    &islog);
22619d439f90SMike Harsch 
22629d439f90SMike Harsch 		if (ishole || islog)
22633f9d6ad7SLin Ling 			continue;
22643f9d6ad7SLin Ling 
226588ecc943SGeorge Wilson 		vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2266c67d9675Seschrock 		print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2267afefbcddSeschrock 		    newchild[c], cb, depth + 2);
2268afefbcddSeschrock 		free(vname);
2269afefbcddSeschrock 	}
2270fa94a07fSbrendan 
2271fa94a07fSbrendan 	/*
22729d439f90SMike Harsch 	 * Log device section
22739d439f90SMike Harsch 	 */
22749d439f90SMike Harsch 
22759d439f90SMike Harsch 	if (num_logs(newnv) > 0) {
22769d439f90SMike Harsch 		(void) printf("%-*s      -      -      -      -      -      "
22779d439f90SMike Harsch 		    "-\n", cb->cb_namewidth, "logs");
22789d439f90SMike Harsch 
22799d439f90SMike Harsch 		for (c = 0; c < children; c++) {
22809d439f90SMike Harsch 			uint64_t islog = B_FALSE;
22819d439f90SMike Harsch 			(void) nvlist_lookup_uint64(newchild[c],
22829d439f90SMike Harsch 			    ZPOOL_CONFIG_IS_LOG, &islog);
22839d439f90SMike Harsch 
22849d439f90SMike Harsch 			if (islog) {
22859d439f90SMike Harsch 				vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
22869d439f90SMike Harsch 				    B_FALSE);
22879d439f90SMike Harsch 				print_vdev_stats(zhp, vname, oldnv ?
22889d439f90SMike Harsch 				    oldchild[c] : NULL, newchild[c],
22899d439f90SMike Harsch 				    cb, depth + 2);
22909d439f90SMike Harsch 				free(vname);
22919d439f90SMike Harsch 			}
22929d439f90SMike Harsch 		}
22939d439f90SMike Harsch 
22949d439f90SMike Harsch 	}
22959d439f90SMike Harsch 
22969d439f90SMike Harsch 	/*
2297fa94a07fSbrendan 	 * Include level 2 ARC devices in iostat output
2298fa94a07fSbrendan 	 */
2299fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2300fa94a07fSbrendan 	    &newchild, &children) != 0)
2301fa94a07fSbrendan 		return;
2302fa94a07fSbrendan 
2303fa94a07fSbrendan 	if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2304fa94a07fSbrendan 	    &oldchild, &c) != 0)
2305fa94a07fSbrendan 		return;
2306fa94a07fSbrendan 
2307fa94a07fSbrendan 	if (children > 0) {
2308fa94a07fSbrendan 		(void) printf("%-*s      -      -      -      -      -      "
2309fa94a07fSbrendan 		    "-\n", cb->cb_namewidth, "cache");
2310fa94a07fSbrendan 		for (c = 0; c < children; c++) {
231188ecc943SGeorge Wilson 			vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
231288ecc943SGeorge Wilson 			    B_FALSE);
2313fa94a07fSbrendan 			print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2314fa94a07fSbrendan 			    newchild[c], cb, depth + 2);
2315fa94a07fSbrendan 			free(vname);
2316fa94a07fSbrendan 		}
2317fa94a07fSbrendan 	}
2318fa9e4066Sahrens }
2319fa9e4066Sahrens 
2320088e9d47Seschrock static int
2321088e9d47Seschrock refresh_iostat(zpool_handle_t *zhp, void *data)
2322088e9d47Seschrock {
2323088e9d47Seschrock 	iostat_cbdata_t *cb = data;
232494de1d4cSeschrock 	boolean_t missing;
2325088e9d47Seschrock 
2326088e9d47Seschrock 	/*
2327088e9d47Seschrock 	 * If the pool has disappeared, remove it from the list and continue.
2328088e9d47Seschrock 	 */
232994de1d4cSeschrock 	if (zpool_refresh_stats(zhp, &missing) != 0)
233094de1d4cSeschrock 		return (-1);
233194de1d4cSeschrock 
233294de1d4cSeschrock 	if (missing)
2333088e9d47Seschrock 		pool_list_remove(cb->cb_list, zhp);
2334088e9d47Seschrock 
2335088e9d47Seschrock 	return (0);
2336088e9d47Seschrock }
2337088e9d47Seschrock 
2338fa9e4066Sahrens /*
2339fa9e4066Sahrens  * Callback to print out the iostats for the given pool.
2340fa9e4066Sahrens  */
2341fa9e4066Sahrens int
2342fa9e4066Sahrens print_iostat(zpool_handle_t *zhp, void *data)
2343fa9e4066Sahrens {
2344fa9e4066Sahrens 	iostat_cbdata_t *cb = data;
2345fa9e4066Sahrens 	nvlist_t *oldconfig, *newconfig;
2346fa9e4066Sahrens 	nvlist_t *oldnvroot, *newnvroot;
2347fa9e4066Sahrens 
2348088e9d47Seschrock 	newconfig = zpool_get_config(zhp, &oldconfig);
2349fa9e4066Sahrens 
2350088e9d47Seschrock 	if (cb->cb_iteration == 1)
2351fa9e4066Sahrens 		oldconfig = NULL;
2352fa9e4066Sahrens 
2353fa9e4066Sahrens 	verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2354fa9e4066Sahrens 	    &newnvroot) == 0);
2355fa9e4066Sahrens 
2356088e9d47Seschrock 	if (oldconfig == NULL)
2357fa9e4066Sahrens 		oldnvroot = NULL;
2358088e9d47Seschrock 	else
2359088e9d47Seschrock 		verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2360088e9d47Seschrock 		    &oldnvroot) == 0);
2361fa9e4066Sahrens 
2362fa9e4066Sahrens 	/*
2363fa9e4066Sahrens 	 * Print out the statistics for the pool.
2364fa9e4066Sahrens 	 */
2365c67d9675Seschrock 	print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2366fa9e4066Sahrens 
2367fa9e4066Sahrens 	if (cb->cb_verbose)
2368fa9e4066Sahrens 		print_iostat_separator(cb);
2369fa9e4066Sahrens 
2370fa9e4066Sahrens 	return (0);
2371fa9e4066Sahrens }
2372fa9e4066Sahrens 
2373fa9e4066Sahrens int
2374fa9e4066Sahrens get_namewidth(zpool_handle_t *zhp, void *data)
2375fa9e4066Sahrens {
2376fa9e4066Sahrens 	iostat_cbdata_t *cb = data;
2377fa9e4066Sahrens 	nvlist_t *config, *nvroot;
2378fa9e4066Sahrens 
2379088e9d47Seschrock 	if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2380fa9e4066Sahrens 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2381fa9e4066Sahrens 		    &nvroot) == 0);
2382fa9e4066Sahrens 		if (!cb->cb_verbose)
2383fa9e4066Sahrens 			cb->cb_namewidth = strlen(zpool_get_name(zhp));
2384fa9e4066Sahrens 		else
2385e1d5e507SFrederik Wessels 			cb->cb_namewidth = max_width(zhp, nvroot, 0,
2386e1d5e507SFrederik Wessels 			    cb->cb_namewidth);
2387fa9e4066Sahrens 	}
2388fa9e4066Sahrens 
2389fa9e4066Sahrens 	/*
2390fa9e4066Sahrens 	 * The width must fall into the range [10,38].  The upper limit is the
2391fa9e4066Sahrens 	 * maximum we can have and still fit in 80 columns.
2392fa9e4066Sahrens 	 */
2393fa9e4066Sahrens 	if (cb->cb_namewidth < 10)
2394fa9e4066Sahrens 		cb->cb_namewidth = 10;
2395fa9e4066Sahrens 	if (cb->cb_namewidth > 38)
2396fa9e4066Sahrens 		cb->cb_namewidth = 38;
2397fa9e4066Sahrens 
2398fa9e4066Sahrens 	return (0);
2399fa9e4066Sahrens }
2400fa9e4066Sahrens 
2401fa9e4066Sahrens /*
24023f9d6ad7SLin Ling  * Parse the input string, get the 'interval' and 'count' value if there is one.
2403fa9e4066Sahrens  */
24043f9d6ad7SLin Ling static void
24053f9d6ad7SLin Ling get_interval_count(int *argcp, char **argv, unsigned long *iv,
24063f9d6ad7SLin Ling     unsigned long *cnt)
2407fa9e4066Sahrens {
2408fa9e4066Sahrens 	unsigned long interval = 0, count = 0;
24093f9d6ad7SLin Ling 	int argc = *argcp, errno;
2410fa9e4066Sahrens 
2411fa9e4066Sahrens 	/*
2412fa9e4066Sahrens 	 * Determine if the last argument is an integer or a pool name
2413fa9e4066Sahrens 	 */
2414fa9e4066Sahrens 	if (argc > 0 && isdigit(argv[argc - 1][0])) {
2415fa9e4066Sahrens 		char *end;
2416fa9e4066Sahrens 
2417fa9e4066Sahrens 		errno = 0;
2418fa9e4066Sahrens 		interval = strtoul(argv[argc - 1], &end, 10);
2419fa9e4066Sahrens 
2420fa9e4066Sahrens 		if (*end == '\0' && errno == 0) {
2421fa9e4066Sahrens 			if (interval == 0) {
2422fa9e4066Sahrens 				(void) fprintf(stderr, gettext("interval "
2423fa9e4066Sahrens 				    "cannot be zero\n"));
242499653d4eSeschrock 				usage(B_FALSE);
2425fa9e4066Sahrens 			}
2426fa9e4066Sahrens 			/*
2427fa9e4066Sahrens 			 * Ignore the last parameter
2428fa9e4066Sahrens 			 */
2429fa9e4066Sahrens 			argc--;
2430fa9e4066Sahrens 		} else {
2431fa9e4066Sahrens 			/*
2432fa9e4066Sahrens 			 * If this is not a valid number, just plow on.  The
2433fa9e4066Sahrens 			 * user will get a more informative error message later
2434fa9e4066Sahrens 			 * on.
2435fa9e4066Sahrens 			 */
2436fa9e4066Sahrens 			interval = 0;
2437fa9e4066Sahrens 		}
2438fa9e4066Sahrens 	}
2439fa9e4066Sahrens 
2440fa9e4066Sahrens 	/*
2441fa9e4066Sahrens 	 * If the last argument is also an integer, then we have both a count
24423f9d6ad7SLin Ling 	 * and an interval.
2443fa9e4066Sahrens 	 */
2444fa9e4066Sahrens 	if (argc > 0 && isdigit(argv[argc - 1][0])) {
2445fa9e4066Sahrens 		char *end;
2446fa9e4066Sahrens 
2447fa9e4066Sahrens 		errno = 0;
2448fa9e4066Sahrens 		count = interval;
2449fa9e4066Sahrens 		interval = strtoul(argv[argc - 1], &end, 10);
2450fa9e4066Sahrens 
2451fa9e4066Sahrens 		if (*end == '\0' && errno == 0) {
2452fa9e4066Sahrens 			if (interval == 0) {
2453fa9e4066Sahrens 				(void) fprintf(stderr, gettext("interval "
2454fa9e4066Sahrens 				    "cannot be zero\n"));
245599653d4eSeschrock 				usage(B_FALSE);
2456fa9e4066Sahrens 			}
2457fa9e4066Sahrens 
2458fa9e4066Sahrens 			/*
2459fa9e4066Sahrens 			 * Ignore the last parameter
2460fa9e4066Sahrens 			 */
2461fa9e4066Sahrens 			argc--;
2462fa9e4066Sahrens 		} else {
2463fa9e4066Sahrens 			interval = 0;
2464fa9e4066Sahrens 		}
2465fa9e4066Sahrens 	}
2466fa9e4066Sahrens 
24673f9d6ad7SLin Ling 	*iv = interval;
24683f9d6ad7SLin Ling 	*cnt = count;
24693f9d6ad7SLin Ling 	*argcp = argc;
24703f9d6ad7SLin Ling }
24713f9d6ad7SLin Ling 
24723f9d6ad7SLin Ling static void
24733f9d6ad7SLin Ling get_timestamp_arg(char c)
24743f9d6ad7SLin Ling {
24753f9d6ad7SLin Ling 	if (c == 'u')
24763f9d6ad7SLin Ling 		timestamp_fmt = UDATE;
24773f9d6ad7SLin Ling 	else if (c == 'd')
24783f9d6ad7SLin Ling 		timestamp_fmt = DDATE;
24793f9d6ad7SLin Ling 	else
24803f9d6ad7SLin Ling 		usage(B_FALSE);
24813f9d6ad7SLin Ling }
24823f9d6ad7SLin Ling 
24833f9d6ad7SLin Ling /*
24843f9d6ad7SLin Ling  * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
24853f9d6ad7SLin Ling  *
24863f9d6ad7SLin Ling  *	-v	Display statistics for individual vdevs
24873f9d6ad7SLin Ling  *	-T	Display a timestamp in date(1) or Unix format
24883f9d6ad7SLin Ling  *
24893f9d6ad7SLin Ling  * This command can be tricky because we want to be able to deal with pool
24903f9d6ad7SLin Ling  * creation/destruction as well as vdev configuration changes.  The bulk of this
24913f9d6ad7SLin Ling  * processing is handled by the pool_list_* routines in zpool_iter.c.  We rely
24923f9d6ad7SLin Ling  * on pool_list_update() to detect the addition of new pools.  Configuration
24933f9d6ad7SLin Ling  * changes are all handled within libzfs.
24943f9d6ad7SLin Ling  */
24953f9d6ad7SLin Ling int
24963f9d6ad7SLin Ling zpool_do_iostat(int argc, char **argv)
24973f9d6ad7SLin Ling {
24983f9d6ad7SLin Ling 	int c;
24993f9d6ad7SLin Ling 	int ret;
25003f9d6ad7SLin Ling 	int npools;
25013f9d6ad7SLin Ling 	unsigned long interval = 0, count = 0;
25023f9d6ad7SLin Ling 	zpool_list_t *list;
25033f9d6ad7SLin Ling 	boolean_t verbose = B_FALSE;
25043f9d6ad7SLin Ling 	iostat_cbdata_t cb;
25053f9d6ad7SLin Ling 
25063f9d6ad7SLin Ling 	/* check options */
25073f9d6ad7SLin Ling 	while ((c = getopt(argc, argv, "T:v")) != -1) {
25083f9d6ad7SLin Ling 		switch (c) {
25093f9d6ad7SLin Ling 		case 'T':
25103f9d6ad7SLin Ling 			get_timestamp_arg(*optarg);
25113f9d6ad7SLin Ling 			break;
25123f9d6ad7SLin Ling 		case 'v':
25133f9d6ad7SLin Ling 			verbose = B_TRUE;
25143f9d6ad7SLin Ling 			break;
25153f9d6ad7SLin Ling 		case '?':
25163f9d6ad7SLin Ling 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
25173f9d6ad7SLin Ling 			    optopt);
25183f9d6ad7SLin Ling 			usage(B_FALSE);
25193f9d6ad7SLin Ling 		}
25203f9d6ad7SLin Ling 	}
25213f9d6ad7SLin Ling 
25223f9d6ad7SLin Ling 	argc -= optind;
25233f9d6ad7SLin Ling 	argv += optind;
25243f9d6ad7SLin Ling 
25253f9d6ad7SLin Ling 	get_interval_count(&argc, argv, &interval, &count);
25263f9d6ad7SLin Ling 
2527fa9e4066Sahrens 	/*
2528fa9e4066Sahrens 	 * Construct the list of all interesting pools.
2529fa9e4066Sahrens 	 */
2530fa9e4066Sahrens 	ret = 0;
2531b1b8ab34Slling 	if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2532fa9e4066Sahrens 		return (1);
2533fa9e4066Sahrens 
253499653d4eSeschrock 	if (pool_list_count(list) == 0 && argc != 0) {
253599653d4eSeschrock 		pool_list_free(list);
2536fa9e4066Sahrens 		return (1);
253799653d4eSeschrock 	}
2538fa9e4066Sahrens 
2539fa9e4066Sahrens 	if (pool_list_count(list) == 0 && interval == 0) {
254099653d4eSeschrock 		pool_list_free(list);
2541fa9e4066Sahrens 		(void) fprintf(stderr, gettext("no pools available\n"));
2542fa9e4066Sahrens 		return (1);
2543fa9e4066Sahrens 	}
2544fa9e4066Sahrens 
2545fa9e4066Sahrens 	/*
2546fa9e4066Sahrens 	 * Enter the main iostat loop.
2547fa9e4066Sahrens 	 */
2548fa9e4066Sahrens 	cb.cb_list = list;
2549fa9e4066Sahrens 	cb.cb_verbose = verbose;
2550fa9e4066Sahrens 	cb.cb_iteration = 0;
2551fa9e4066Sahrens 	cb.cb_namewidth = 0;
2552fa9e4066Sahrens 
2553fa9e4066Sahrens 	for (;;) {
2554fa9e4066Sahrens 		pool_list_update(list);
2555fa9e4066Sahrens 
2556fa9e4066Sahrens 		if ((npools = pool_list_count(list)) == 0)
2557fa9e4066Sahrens 			break;
2558fa9e4066Sahrens 
2559fa9e4066Sahrens 		/*
2560088e9d47Seschrock 		 * Refresh all statistics.  This is done as an explicit step
2561088e9d47Seschrock 		 * before calculating the maximum name width, so that any
2562088e9d47Seschrock 		 * configuration changes are properly accounted for.
2563088e9d47Seschrock 		 */
256499653d4eSeschrock 		(void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2565088e9d47Seschrock 
2566088e9d47Seschrock 		/*
2567fa9e4066Sahrens 		 * Iterate over all pools to determine the maximum width
2568fa9e4066Sahrens 		 * for the pool / device name column across all pools.
2569fa9e4066Sahrens 		 */
2570fa9e4066Sahrens 		cb.cb_namewidth = 0;
257199653d4eSeschrock 		(void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2572fa9e4066Sahrens 
257326fd7700SKrishnendu Sadhukhan - Sun Microsystems 		if (timestamp_fmt != NODATE)
257426fd7700SKrishnendu Sadhukhan - Sun Microsystems 			print_timestamp(timestamp_fmt);
257526fd7700SKrishnendu Sadhukhan - Sun Microsystems 
2576fa9e4066Sahrens 		/*
2577fa9e4066Sahrens 		 * If it's the first time, or verbose mode, print the header.
2578fa9e4066Sahrens 		 */
2579fa9e4066Sahrens 		if (++cb.cb_iteration == 1 || verbose)
2580fa9e4066Sahrens 			print_iostat_header(&cb);
2581fa9e4066Sahrens 
258299653d4eSeschrock 		(void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2583fa9e4066Sahrens 
2584fa9e4066Sahrens 		/*
2585fa9e4066Sahrens 		 * If there's more than one pool, and we're not in verbose mode
2586fa9e4066Sahrens 		 * (which prints a separator for us), then print a separator.
2587fa9e4066Sahrens 		 */
2588fa9e4066Sahrens 		if (npools > 1 && !verbose)
2589fa9e4066Sahrens 			print_iostat_separator(&cb);
2590fa9e4066Sahrens 
2591fa9e4066Sahrens 		if (verbose)
2592fa9e4066Sahrens 			(void) printf("\n");
2593fa9e4066Sahrens 
259439c23413Seschrock 		/*
259539c23413Seschrock 		 * Flush the output so that redirection to a file isn't buffered
259639c23413Seschrock 		 * indefinitely.
259739c23413Seschrock 		 */
259839c23413Seschrock 		(void) fflush(stdout);
259939c23413Seschrock 
2600fa9e4066Sahrens 		if (interval == 0)
2601fa9e4066Sahrens 			break;
2602fa9e4066Sahrens 
2603fa9e4066Sahrens 		if (count != 0 && --count == 0)
2604fa9e4066Sahrens 			break;
2605fa9e4066Sahrens 
2606fa9e4066Sahrens 		(void) sleep(interval);
2607fa9e4066Sahrens 	}
2608fa9e4066Sahrens 
2609fa9e4066Sahrens 	pool_list_free(list);
2610fa9e4066Sahrens 
2611fa9e4066Sahrens 	return (ret);
2612fa9e4066Sahrens }
2613fa9e4066Sahrens 
2614fa9e4066Sahrens typedef struct list_cbdata {
26154263d13fSGeorge Wilson 	boolean_t	cb_verbose;
26164263d13fSGeorge Wilson 	int		cb_namewidth;
261799653d4eSeschrock 	boolean_t	cb_scripted;
2618990b4856Slling 	zprop_list_t	*cb_proplist;
2619fa9e4066Sahrens } list_cbdata_t;
2620fa9e4066Sahrens 
2621fa9e4066Sahrens /*
2622fa9e4066Sahrens  * Given a list of columns to display, output appropriate headers for each one.
2623fa9e4066Sahrens  */
2624990b4856Slling static void
26254263d13fSGeorge Wilson print_header(list_cbdata_t *cb)
2626fa9e4066Sahrens {
26274263d13fSGeorge Wilson 	zprop_list_t *pl = cb->cb_proplist;
2628ad135b5dSChristopher Siden 	char headerbuf[ZPOOL_MAXPROPLEN];
2629990b4856Slling 	const char *header;
2630990b4856Slling 	boolean_t first = B_TRUE;
2631990b4856Slling 	boolean_t right_justify;
26324263d13fSGeorge Wilson 	size_t width = 0;
2633fa9e4066Sahrens 
2634990b4856Slling 	for (; pl != NULL; pl = pl->pl_next) {
26354263d13fSGeorge Wilson 		width = pl->pl_width;
26364263d13fSGeorge Wilson 		if (first && cb->cb_verbose) {
26374263d13fSGeorge Wilson 			/*
26384263d13fSGeorge Wilson 			 * Reset the width to accommodate the verbose listing
26394263d13fSGeorge Wilson 			 * of devices.
26404263d13fSGeorge Wilson 			 */
26414263d13fSGeorge Wilson 			width = cb->cb_namewidth;
26424263d13fSGeorge Wilson 		}
26434263d13fSGeorge Wilson 
2644990b4856Slling 		if (!first)
2645fa9e4066Sahrens 			(void) printf("  ");
2646fa9e4066Sahrens 		else
2647990b4856Slling 			first = B_FALSE;
2648fa9e4066Sahrens 
2649ad135b5dSChristopher Siden 		right_justify = B_FALSE;
2650ad135b5dSChristopher Siden 		if (pl->pl_prop != ZPROP_INVAL) {
2651990b4856Slling 			header = zpool_prop_column_name(pl->pl_prop);
2652990b4856Slling 			right_justify = zpool_prop_align_right(pl->pl_prop);
2653ad135b5dSChristopher Siden 		} else {
2654ad135b5dSChristopher Siden 			int i;
2655ad135b5dSChristopher Siden 
2656ad135b5dSChristopher Siden 			for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2657ad135b5dSChristopher Siden 				headerbuf[i] = toupper(pl->pl_user_prop[i]);
2658ad135b5dSChristopher Siden 			headerbuf[i] = '\0';
2659ad135b5dSChristopher Siden 			header = headerbuf;
2660ad135b5dSChristopher Siden 		}
2661990b4856Slling 
2662990b4856Slling 		if (pl->pl_next == NULL && !right_justify)
2663990b4856Slling 			(void) printf("%s", header);
2664990b4856Slling 		else if (right_justify)
26654263d13fSGeorge Wilson 			(void) printf("%*s", width, header);
2666990b4856Slling 		else
26674263d13fSGeorge Wilson 			(void) printf("%-*s", width, header);
26684263d13fSGeorge Wilson 
2669fa9e4066Sahrens 	}
2670fa9e4066Sahrens 
2671fa9e4066Sahrens 	(void) printf("\n");
2672fa9e4066Sahrens }
2673fa9e4066Sahrens 
2674990b4856Slling /*
2675990b4856Slling  * Given a pool and a list of properties, print out all the properties according
2676990b4856Slling  * to the described layout.
2677990b4856Slling  */
2678990b4856Slling static void
26794263d13fSGeorge Wilson print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2680990b4856Slling {
26814263d13fSGeorge Wilson 	zprop_list_t *pl = cb->cb_proplist;
2682990b4856Slling 	boolean_t first = B_TRUE;
2683990b4856Slling 	char property[ZPOOL_MAXPROPLEN];
2684990b4856Slling 	char *propstr;
2685990b4856Slling 	boolean_t right_justify;
26864263d13fSGeorge Wilson 	size_t width;
2687990b4856Slling 
2688990b4856Slling 	for (; pl != NULL; pl = pl->pl_next) {
26894263d13fSGeorge Wilson 
26904263d13fSGeorge Wilson 		width = pl->pl_width;
26914263d13fSGeorge Wilson 		if (first && cb->cb_verbose) {
26924263d13fSGeorge Wilson 			/*
26934263d13fSGeorge Wilson 			 * Reset the width to accommodate the verbose listing
26944263d13fSGeorge Wilson 			 * of devices.
26954263d13fSGeorge Wilson 			 */
26964263d13fSGeorge Wilson 			width = cb->cb_namewidth;
26974263d13fSGeorge Wilson 		}
26984263d13fSGeorge Wilson 
2699990b4856Slling 		if (!first) {
27004263d13fSGeorge Wilson 			if (cb->cb_scripted)
2701990b4856Slling 				(void) printf("\t");
2702990b4856Slling 			else
2703990b4856Slling 				(void) printf("  ");
2704990b4856Slling 		} else {
2705990b4856Slling 			first = B_FALSE;
2706990b4856Slling 		}
2707990b4856Slling 
2708990b4856Slling 		right_justify = B_FALSE;
2709990b4856Slling 		if (pl->pl_prop != ZPROP_INVAL) {
27104263d13fSGeorge Wilson 			if (pl->pl_prop == ZPOOL_PROP_EXPANDSZ &&
27114263d13fSGeorge Wilson 			    zpool_get_prop_int(zhp, pl->pl_prop, NULL) == 0)
27124263d13fSGeorge Wilson 				propstr = "-";
27134263d13fSGeorge Wilson 			else if (zpool_get_prop(zhp, pl->pl_prop, property,
2714990b4856Slling 			    sizeof (property), NULL) != 0)
2715990b4856Slling 				propstr = "-";
2716990b4856Slling 			else
2717990b4856Slling 				propstr = property;
2718990b4856Slling 
2719990b4856Slling 			right_justify = zpool_prop_align_right(pl->pl_prop);
2720ad135b5dSChristopher Siden 		} else if ((zpool_prop_feature(pl->pl_user_prop) ||
2721ad135b5dSChristopher Siden 		    zpool_prop_unsupported(pl->pl_user_prop)) &&
2722ad135b5dSChristopher Siden 		    zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2723ad135b5dSChristopher Siden 		    sizeof (property)) == 0) {
2724ad135b5dSChristopher Siden 			propstr = property;
2725990b4856Slling 		} else {
2726990b4856Slling 			propstr = "-";
2727990b4856Slling 		}
2728990b4856Slling 
2729990b4856Slling 
2730990b4856Slling 		/*
2731990b4856Slling 		 * If this is being called in scripted mode, or if this is the
2732990b4856Slling 		 * last column and it is left-justified, don't include a width
2733990b4856Slling 		 * format specifier.
2734990b4856Slling 		 */
27354263d13fSGeorge Wilson 		if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2736990b4856Slling 			(void) printf("%s", propstr);
2737990b4856Slling 		else if (right_justify)
2738990b4856Slling 			(void) printf("%*s", width, propstr);
2739990b4856Slling 		else
2740990b4856Slling 			(void) printf("%-*s", width, propstr);
2741990b4856Slling 	}
2742990b4856Slling 
2743990b4856Slling 	(void) printf("\n");
2744990b4856Slling }
2745990b4856Slling 
27464263d13fSGeorge Wilson static void
27474263d13fSGeorge Wilson print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted)
27484263d13fSGeorge Wilson {
27494263d13fSGeorge Wilson 	char propval[64];
27504263d13fSGeorge Wilson 	boolean_t fixed;
27514263d13fSGeorge Wilson 	size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
27524263d13fSGeorge Wilson 
27534263d13fSGeorge Wilson 	zfs_nicenum(value, propval, sizeof (propval));
27544263d13fSGeorge Wilson 
27554263d13fSGeorge Wilson 	if (prop == ZPOOL_PROP_EXPANDSZ && value == 0)
27564263d13fSGeorge Wilson 		(void) strlcpy(propval, "-", sizeof (propval));
27574263d13fSGeorge Wilson 
27584263d13fSGeorge Wilson 	if (scripted)
27594263d13fSGeorge Wilson 		(void) printf("\t%s", propval);
27604263d13fSGeorge Wilson 	else
27614263d13fSGeorge Wilson 		(void) printf("  %*s", width, propval);
27624263d13fSGeorge Wilson }
27634263d13fSGeorge Wilson 
27644263d13fSGeorge Wilson void
27654263d13fSGeorge Wilson print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
27664263d13fSGeorge Wilson     list_cbdata_t *cb, int depth)
27674263d13fSGeorge Wilson {
27684263d13fSGeorge Wilson 	nvlist_t **child;
27694263d13fSGeorge Wilson 	vdev_stat_t *vs;
27704263d13fSGeorge Wilson 	uint_t c, children;
27714263d13fSGeorge Wilson 	char *vname;
27724263d13fSGeorge Wilson 	boolean_t scripted = cb->cb_scripted;
27734263d13fSGeorge Wilson 
27744263d13fSGeorge Wilson 	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
27754263d13fSGeorge Wilson 	    (uint64_t **)&vs, &c) == 0);
27764263d13fSGeorge Wilson 
27774263d13fSGeorge Wilson 	if (name != NULL) {
27784263d13fSGeorge Wilson 		if (scripted)
27794263d13fSGeorge Wilson 			(void) printf("\t%s", name);
27804263d13fSGeorge Wilson 		else if (strlen(name) + depth > cb->cb_namewidth)
27814263d13fSGeorge Wilson 			(void) printf("%*s%s", depth, "", name);
27824263d13fSGeorge Wilson 		else
27834263d13fSGeorge Wilson 			(void) printf("%*s%s%*s", depth, "", name,
27844263d13fSGeorge Wilson 			    (int)(cb->cb_namewidth - strlen(name) - depth), "");
27854263d13fSGeorge Wilson 
27864263d13fSGeorge Wilson 		/* only toplevel vdevs have capacity stats */
27874263d13fSGeorge Wilson 		if (vs->vs_space == 0) {
27884263d13fSGeorge Wilson 			if (scripted)
27894263d13fSGeorge Wilson 				(void) printf("\t-\t-\t-");
27904263d13fSGeorge Wilson 			else
27914263d13fSGeorge Wilson 				(void) printf("      -      -      -");
27924263d13fSGeorge Wilson 		} else {
27934263d13fSGeorge Wilson 			print_one_column(ZPOOL_PROP_SIZE, vs->vs_space,
27944263d13fSGeorge Wilson 			    scripted);
27954263d13fSGeorge Wilson 			print_one_column(ZPOOL_PROP_CAPACITY, vs->vs_alloc,
27964263d13fSGeorge Wilson 			    scripted);
27974263d13fSGeorge Wilson 			print_one_column(ZPOOL_PROP_FREE,
27984263d13fSGeorge Wilson 			    vs->vs_space - vs->vs_alloc, scripted);
27994263d13fSGeorge Wilson 		}
28004263d13fSGeorge Wilson 		print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize,
28014263d13fSGeorge Wilson 		    scripted);
28024263d13fSGeorge Wilson 		(void) printf("\n");
28034263d13fSGeorge Wilson 	}
28044263d13fSGeorge Wilson 
28054263d13fSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
28064263d13fSGeorge Wilson 	    &child, &children) != 0)
28074263d13fSGeorge Wilson 		return;
28084263d13fSGeorge Wilson 
28094263d13fSGeorge Wilson 	for (c = 0; c < children; c++) {
28104263d13fSGeorge Wilson 		uint64_t ishole = B_FALSE;
28114263d13fSGeorge Wilson 
28124263d13fSGeorge Wilson 		if (nvlist_lookup_uint64(child[c],
28134263d13fSGeorge Wilson 		    ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
28144263d13fSGeorge Wilson 			continue;
28154263d13fSGeorge Wilson 
28164263d13fSGeorge Wilson 		vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
28174263d13fSGeorge Wilson 		print_list_stats(zhp, vname, child[c], cb, depth + 2);
28184263d13fSGeorge Wilson 		free(vname);
28194263d13fSGeorge Wilson 	}
28204263d13fSGeorge Wilson 
28214263d13fSGeorge Wilson 	/*
28224263d13fSGeorge Wilson 	 * Include level 2 ARC devices in iostat output
28234263d13fSGeorge Wilson 	 */
28244263d13fSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
28254263d13fSGeorge Wilson 	    &child, &children) != 0)
28264263d13fSGeorge Wilson 		return;
28274263d13fSGeorge Wilson 
28284263d13fSGeorge Wilson 	if (children > 0) {
28294263d13fSGeorge Wilson 		(void) printf("%-*s      -      -      -      -      -      "
28304263d13fSGeorge Wilson 		    "-\n", cb->cb_namewidth, "cache");
28314263d13fSGeorge Wilson 		for (c = 0; c < children; c++) {
28324263d13fSGeorge Wilson 			vname = zpool_vdev_name(g_zfs, zhp, child[c],
28334263d13fSGeorge Wilson 			    B_FALSE);
28344263d13fSGeorge Wilson 			print_list_stats(zhp, vname, child[c], cb, depth + 2);
28354263d13fSGeorge Wilson 			free(vname);
28364263d13fSGeorge Wilson 		}
28374263d13fSGeorge Wilson 	}
28384263d13fSGeorge Wilson }
28394263d13fSGeorge Wilson 
28404263d13fSGeorge Wilson 
2841990b4856Slling /*
2842990b4856Slling  * Generic callback function to list a pool.
2843990b4856Slling  */
2844fa9e4066Sahrens int
2845fa9e4066Sahrens list_callback(zpool_handle_t *zhp, void *data)
2846fa9e4066Sahrens {
2847fa9e4066Sahrens 	list_cbdata_t *cbp = data;
28484263d13fSGeorge Wilson 	nvlist_t *config;
28494263d13fSGeorge Wilson 	nvlist_t *nvroot;
2850fa9e4066Sahrens 
28514263d13fSGeorge Wilson 	config = zpool_get_config(zhp, NULL);
2852fa9e4066Sahrens 
28534263d13fSGeorge Wilson 	print_pool(zhp, cbp);
28544263d13fSGeorge Wilson 	if (!cbp->cb_verbose)
28554263d13fSGeorge Wilson 		return (0);
28564263d13fSGeorge Wilson 
28574263d13fSGeorge Wilson 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
28584263d13fSGeorge Wilson 	    &nvroot) == 0);
28594263d13fSGeorge Wilson 	print_list_stats(zhp, NULL, nvroot, cbp, 0);
2860fa9e4066Sahrens 
2861fa9e4066Sahrens 	return (0);
2862fa9e4066Sahrens }
2863fa9e4066Sahrens 
2864fa9e4066Sahrens /*
28653f9d6ad7SLin Ling  * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2866fa9e4066Sahrens  *
2867990b4856Slling  *	-H	Scripted mode.  Don't display headers, and separate properties
2868990b4856Slling  *		by a single tab.
2869990b4856Slling  *	-o	List of properties to display.  Defaults to
2870485bbbf5SGeorge Wilson  *		"name,size,allocated,free,capacity,health,altroot"
28713f9d6ad7SLin Ling  *	-T	Display a timestamp in date(1) or Unix format
2872fa9e4066Sahrens  *
2873fa9e4066Sahrens  * List all pools in the system, whether or not they're healthy.  Output space
2874fa9e4066Sahrens  * statistics for each one, as well as health status summary.
2875fa9e4066Sahrens  */
2876fa9e4066Sahrens int
2877fa9e4066Sahrens zpool_do_list(int argc, char **argv)
2878fa9e4066Sahrens {
2879fa9e4066Sahrens 	int c;
2880fa9e4066Sahrens 	int ret;
2881fa9e4066Sahrens 	list_cbdata_t cb = { 0 };
2882990b4856Slling 	static char default_props[] =
28834263d13fSGeorge Wilson 	    "name,size,allocated,free,expandsize,capacity,dedupratio,"
28844263d13fSGeorge Wilson 	    "health,altroot";
2885990b4856Slling 	char *props = default_props;
28863f9d6ad7SLin Ling 	unsigned long interval = 0, count = 0;
28874263d13fSGeorge Wilson 	zpool_list_t *list;
28884263d13fSGeorge Wilson 	boolean_t first = B_TRUE;
2889fa9e4066Sahrens 
2890fa9e4066Sahrens 	/* check options */
28914263d13fSGeorge Wilson 	while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) {
2892fa9e4066Sahrens 		switch (c) {
2893fa9e4066Sahrens 		case 'H':
289499653d4eSeschrock 			cb.cb_scripted = B_TRUE;
2895fa9e4066Sahrens 			break;
2896fa9e4066Sahrens 		case 'o':
2897990b4856Slling 			props = optarg;
2898fa9e4066Sahrens 			break;
28993f9d6ad7SLin Ling 		case 'T':
29003f9d6ad7SLin Ling 			get_timestamp_arg(*optarg);
29013f9d6ad7SLin Ling 			break;
29024263d13fSGeorge Wilson 		case 'v':
29034263d13fSGeorge Wilson 			cb.cb_verbose = B_TRUE;
29044263d13fSGeorge Wilson 			break;
2905fa9e4066Sahrens 		case ':':
2906fa9e4066Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
2907fa9e4066Sahrens 			    "'%c' option\n"), optopt);
290899653d4eSeschrock 			usage(B_FALSE);
2909fa9e4066Sahrens 			break;
2910fa9e4066Sahrens 		case '?':
2911fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2912fa9e4066Sahrens 			    optopt);
291399653d4eSeschrock 			usage(B_FALSE);
2914fa9e4066Sahrens 		}
2915fa9e4066Sahrens 	}
2916fa9e4066Sahrens 
2917fa9e4066Sahrens 	argc -= optind;
2918fa9e4066Sahrens 	argv += optind;
2919fa9e4066Sahrens 
29203f9d6ad7SLin Ling 	get_interval_count(&argc, argv, &interval, &count);
29213f9d6ad7SLin Ling 
2922990b4856Slling 	if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
292399653d4eSeschrock 		usage(B_FALSE);
2924fa9e4066Sahrens 
29254263d13fSGeorge Wilson 	if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL)
29264263d13fSGeorge Wilson 		return (1);
2927fa9e4066Sahrens 
29284263d13fSGeorge Wilson 	if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
2929fa9e4066Sahrens 		(void) printf(gettext("no pools available\n"));
29303f9d6ad7SLin Ling 		zprop_free_list(cb.cb_proplist);
2931fa9e4066Sahrens 		return (0);
2932fa9e4066Sahrens 	}
2933fa9e4066Sahrens 
29344263d13fSGeorge Wilson 	for (;;) {
29354263d13fSGeorge Wilson 		pool_list_update(list);
29364263d13fSGeorge Wilson 
29374263d13fSGeorge Wilson 		if (pool_list_count(list) == 0)
29384263d13fSGeorge Wilson 			break;
29394263d13fSGeorge Wilson 
29404263d13fSGeorge Wilson 		cb.cb_namewidth = 0;
29414263d13fSGeorge Wilson 		(void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
29424263d13fSGeorge Wilson 
29434263d13fSGeorge Wilson 		if (timestamp_fmt != NODATE)
29444263d13fSGeorge Wilson 			print_timestamp(timestamp_fmt);
29454263d13fSGeorge Wilson 
29464263d13fSGeorge Wilson 		if (!cb.cb_scripted && (first || cb.cb_verbose)) {
29474263d13fSGeorge Wilson 			print_header(&cb);
29484263d13fSGeorge Wilson 			first = B_FALSE;
29494263d13fSGeorge Wilson 		}
29504263d13fSGeorge Wilson 		ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
29514263d13fSGeorge Wilson 
29523f9d6ad7SLin Ling 		if (interval == 0)
29533f9d6ad7SLin Ling 			break;
29543f9d6ad7SLin Ling 
29553f9d6ad7SLin Ling 		if (count != 0 && --count == 0)
29563f9d6ad7SLin Ling 			break;
29573f9d6ad7SLin Ling 
29583f9d6ad7SLin Ling 		(void) sleep(interval);
29593f9d6ad7SLin Ling 	}
29603f9d6ad7SLin Ling 
29613f9d6ad7SLin Ling 	zprop_free_list(cb.cb_proplist);
2962fa9e4066Sahrens 	return (ret);
2963fa9e4066Sahrens }
2964fa9e4066Sahrens 
2965fa9e4066Sahrens static nvlist_t *
2966fa9e4066Sahrens zpool_get_vdev_by_name(nvlist_t *nv, char *name)
2967fa9e4066Sahrens {
2968fa9e4066Sahrens 	nvlist_t **child;
2969fa9e4066Sahrens 	uint_t c, children;
2970fa9e4066Sahrens 	nvlist_t *match;
2971fa9e4066Sahrens 	char *path;
2972fa9e4066Sahrens 
2973fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2974fa9e4066Sahrens 	    &child, &children) != 0) {
2975fa9e4066Sahrens 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2976fa9e4066Sahrens 		if (strncmp(name, "/dev/dsk/", 9) == 0)
2977fa9e4066Sahrens 			name += 9;
2978fa9e4066Sahrens 		if (strncmp(path, "/dev/dsk/", 9) == 0)
2979fa9e4066Sahrens 			path += 9;
2980fa9e4066Sahrens 		if (strcmp(name, path) == 0)
2981fa9e4066Sahrens 			return (nv);
2982fa9e4066Sahrens 		return (NULL);
2983fa9e4066Sahrens 	}
2984fa9e4066Sahrens 
2985fa9e4066Sahrens 	for (c = 0; c < children; c++)
2986fa9e4066Sahrens 		if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
2987fa9e4066Sahrens 			return (match);
2988fa9e4066Sahrens 
2989fa9e4066Sahrens 	return (NULL);
2990fa9e4066Sahrens }
2991fa9e4066Sahrens 
2992fa9e4066Sahrens static int
2993fa9e4066Sahrens zpool_do_attach_or_replace(int argc, char **argv, int replacing)
2994fa9e4066Sahrens {
299599653d4eSeschrock 	boolean_t force = B_FALSE;
2996fa9e4066Sahrens 	int c;
2997fa9e4066Sahrens 	nvlist_t *nvroot;
2998fa9e4066Sahrens 	char *poolname, *old_disk, *new_disk;
2999fa9e4066Sahrens 	zpool_handle_t *zhp;
300099653d4eSeschrock 	int ret;
3001fa9e4066Sahrens 
3002fa9e4066Sahrens 	/* check options */
3003fa9e4066Sahrens 	while ((c = getopt(argc, argv, "f")) != -1) {
3004fa9e4066Sahrens 		switch (c) {
3005fa9e4066Sahrens 		case 'f':
300699653d4eSeschrock 			force = B_TRUE;
3007fa9e4066Sahrens 			break;
3008fa9e4066Sahrens 		case '?':
3009fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3010fa9e4066Sahrens 			    optopt);
301199653d4eSeschrock 			usage(B_FALSE);
3012fa9e4066Sahrens 		}
3013fa9e4066Sahrens 	}
3014fa9e4066Sahrens 
3015fa9e4066Sahrens 	argc -= optind;
3016fa9e4066Sahrens 	argv += optind;
3017fa9e4066Sahrens 
3018fa9e4066Sahrens 	/* get pool name and check number of arguments */
3019fa9e4066Sahrens 	if (argc < 1) {
3020fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
302199653d4eSeschrock 		usage(B_FALSE);
3022fa9e4066Sahrens 	}
3023fa9e4066Sahrens 
3024fa9e4066Sahrens 	poolname = argv[0];
3025fa9e4066Sahrens 
3026fa9e4066Sahrens 	if (argc < 2) {
3027fa9e4066Sahrens 		(void) fprintf(stderr,
3028fa9e4066Sahrens 		    gettext("missing <device> specification\n"));
302999653d4eSeschrock 		usage(B_FALSE);
3030fa9e4066Sahrens 	}
3031fa9e4066Sahrens 
3032fa9e4066Sahrens 	old_disk = argv[1];
3033fa9e4066Sahrens 
3034fa9e4066Sahrens 	if (argc < 3) {
3035fa9e4066Sahrens 		if (!replacing) {
3036fa9e4066Sahrens 			(void) fprintf(stderr,
3037fa9e4066Sahrens 			    gettext("missing <new_device> specification\n"));
303899653d4eSeschrock 			usage(B_FALSE);
3039fa9e4066Sahrens 		}
3040fa9e4066Sahrens 		new_disk = old_disk;
3041fa9e4066Sahrens 		argc -= 1;
3042fa9e4066Sahrens 		argv += 1;
3043fa9e4066Sahrens 	} else {
3044fa9e4066Sahrens 		new_disk = argv[2];
3045fa9e4066Sahrens 		argc -= 2;
3046fa9e4066Sahrens 		argv += 2;
3047fa9e4066Sahrens 	}
3048fa9e4066Sahrens 
3049fa9e4066Sahrens 	if (argc > 1) {
3050fa9e4066Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
305199653d4eSeschrock 		usage(B_FALSE);
3052fa9e4066Sahrens 	}
3053fa9e4066Sahrens 
305499653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3055fa9e4066Sahrens 		return (1);
3056fa9e4066Sahrens 
30578488aeb5Staylor 	if (zpool_get_config(zhp, NULL) == NULL) {
3058fa9e4066Sahrens 		(void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3059fa9e4066Sahrens 		    poolname);
3060fa9e4066Sahrens 		zpool_close(zhp);
3061fa9e4066Sahrens 		return (1);
3062fa9e4066Sahrens 	}
3063fa9e4066Sahrens 
3064705040edSEric Taylor 	nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
3065705040edSEric Taylor 	    argc, argv);
3066fa9e4066Sahrens 	if (nvroot == NULL) {
3067fa9e4066Sahrens 		zpool_close(zhp);
3068fa9e4066Sahrens 		return (1);
3069fa9e4066Sahrens 	}
3070fa9e4066Sahrens 
307199653d4eSeschrock 	ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
307299653d4eSeschrock 
307399653d4eSeschrock 	nvlist_free(nvroot);
307499653d4eSeschrock 	zpool_close(zhp);
307599653d4eSeschrock 
307699653d4eSeschrock 	return (ret);
3077fa9e4066Sahrens }
3078fa9e4066Sahrens 
3079fa9e4066Sahrens /*
3080fa9e4066Sahrens  * zpool replace [-f] <pool> <device> <new_device>
3081fa9e4066Sahrens  *
3082fa9e4066Sahrens  *	-f	Force attach, even if <new_device> appears to be in use.
3083fa9e4066Sahrens  *
3084fa9e4066Sahrens  * Replace <device> with <new_device>.
3085fa9e4066Sahrens  */
3086fa9e4066Sahrens /* ARGSUSED */
3087fa9e4066Sahrens int
3088fa9e4066Sahrens zpool_do_replace(int argc, char **argv)
3089fa9e4066Sahrens {
3090fa9e4066Sahrens 	return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3091fa9e4066Sahrens }
3092fa9e4066Sahrens 
3093fa9e4066Sahrens /*
3094fa9e4066Sahrens  * zpool attach [-f] <pool> <device> <new_device>
3095fa9e4066Sahrens  *
3096fa9e4066Sahrens  *	-f	Force attach, even if <new_device> appears to be in use.
3097fa9e4066Sahrens  *
3098fa9e4066Sahrens  * Attach <new_device> to the mirror containing <device>.  If <device> is not
3099fa9e4066Sahrens  * part of a mirror, then <device> will be transformed into a mirror of
3100fa9e4066Sahrens  * <device> and <new_device>.  In either case, <new_device> will begin life
3101fa9e4066Sahrens  * with a DTL of [0, now], and will immediately begin to resilver itself.
3102fa9e4066Sahrens  */
3103fa9e4066Sahrens int
3104fa9e4066Sahrens zpool_do_attach(int argc, char **argv)
3105fa9e4066Sahrens {
3106fa9e4066Sahrens 	return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3107fa9e4066Sahrens }
3108fa9e4066Sahrens 
3109fa9e4066Sahrens /*
3110fa9e4066Sahrens  * zpool detach [-f] <pool> <device>
3111fa9e4066Sahrens  *
3112fa9e4066Sahrens  *	-f	Force detach of <device>, even if DTLs argue against it
3113fa9e4066Sahrens  *		(not supported yet)
3114fa9e4066Sahrens  *
3115fa9e4066Sahrens  * Detach a device from a mirror.  The operation will be refused if <device>
3116fa9e4066Sahrens  * is the last device in the mirror, or if the DTLs indicate that this device
3117fa9e4066Sahrens  * has the only valid copy of some data.
3118fa9e4066Sahrens  */
3119fa9e4066Sahrens /* ARGSUSED */
3120fa9e4066Sahrens int
3121fa9e4066Sahrens zpool_do_detach(int argc, char **argv)
3122fa9e4066Sahrens {
3123fa9e4066Sahrens 	int c;
3124fa9e4066Sahrens 	char *poolname, *path;
3125fa9e4066Sahrens 	zpool_handle_t *zhp;
312699653d4eSeschrock 	int ret;
3127fa9e4066Sahrens 
3128fa9e4066Sahrens 	/* check options */
3129fa9e4066Sahrens 	while ((c = getopt(argc, argv, "f")) != -1) {
3130fa9e4066Sahrens 		switch (c) {
3131fa9e4066Sahrens 		case 'f':
3132fa9e4066Sahrens 		case '?':
3133fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3134fa9e4066Sahrens 			    optopt);
313599653d4eSeschrock 			usage(B_FALSE);
3136fa9e4066Sahrens 		}
3137fa9e4066Sahrens 	}
3138fa9e4066Sahrens 
3139fa9e4066Sahrens 	argc -= optind;
3140fa9e4066Sahrens 	argv += optind;
3141fa9e4066Sahrens 
3142fa9e4066Sahrens 	/* get pool name and check number of arguments */
3143fa9e4066Sahrens 	if (argc < 1) {
3144fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
314599653d4eSeschrock 		usage(B_FALSE);
3146fa9e4066Sahrens 	}
3147fa9e4066Sahrens 
3148fa9e4066Sahrens 	if (argc < 2) {
3149fa9e4066Sahrens 		(void) fprintf(stderr,
3150fa9e4066Sahrens 		    gettext("missing <device> specification\n"));
315199653d4eSeschrock 		usage(B_FALSE);
3152fa9e4066Sahrens 	}
3153fa9e4066Sahrens 
3154fa9e4066Sahrens 	poolname = argv[0];
3155fa9e4066Sahrens 	path = argv[1];
3156fa9e4066Sahrens 
315799653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3158fa9e4066Sahrens 		return (1);
3159fa9e4066Sahrens 
316099653d4eSeschrock 	ret = zpool_vdev_detach(zhp, path);
316199653d4eSeschrock 
316299653d4eSeschrock 	zpool_close(zhp);
316399653d4eSeschrock 
316499653d4eSeschrock 	return (ret);
3165fa9e4066Sahrens }
3166fa9e4066Sahrens 
3167fa9e4066Sahrens /*
31681195e687SMark J Musante  * zpool split [-n] [-o prop=val] ...
31691195e687SMark J Musante  *		[-o mntopt] ...
31701195e687SMark J Musante  *		[-R altroot] <pool> <newpool> [<device> ...]
31711195e687SMark J Musante  *
31721195e687SMark J Musante  *	-n	Do not split the pool, but display the resulting layout if
31731195e687SMark J Musante  *		it were to be split.
31741195e687SMark J Musante  *	-o	Set property=value, or set mount options.
31751195e687SMark J Musante  *	-R	Mount the split-off pool under an alternate root.
31761195e687SMark J Musante  *
31771195e687SMark J Musante  * Splits the named pool and gives it the new pool name.  Devices to be split
31781195e687SMark J Musante  * off may be listed, provided that no more than one device is specified
31791195e687SMark J Musante  * per top-level vdev mirror.  The newly split pool is left in an exported
31801195e687SMark J Musante  * state unless -R is specified.
31811195e687SMark J Musante  *
31821195e687SMark J Musante  * Restrictions: the top-level of the pool pool must only be made up of
31831195e687SMark J Musante  * mirrors; all devices in the pool must be healthy; no device may be
31841195e687SMark J Musante  * undergoing a resilvering operation.
31851195e687SMark J Musante  */
31861195e687SMark J Musante int
31871195e687SMark J Musante zpool_do_split(int argc, char **argv)
31881195e687SMark J Musante {
31891195e687SMark J Musante 	char *srcpool, *newpool, *propval;
31901195e687SMark J Musante 	char *mntopts = NULL;
31911195e687SMark J Musante 	splitflags_t flags;
31921195e687SMark J Musante 	int c, ret = 0;
31931195e687SMark J Musante 	zpool_handle_t *zhp;
31941195e687SMark J Musante 	nvlist_t *config, *props = NULL;
31951195e687SMark J Musante 
31961195e687SMark J Musante 	flags.dryrun = B_FALSE;
31971195e687SMark J Musante 	flags.import = B_FALSE;
31981195e687SMark J Musante 
31991195e687SMark J Musante 	/* check options */
32001195e687SMark J Musante 	while ((c = getopt(argc, argv, ":R:no:")) != -1) {
32011195e687SMark J Musante 		switch (c) {
32021195e687SMark J Musante 		case 'R':
32031195e687SMark J Musante 			flags.import = B_TRUE;
32041195e687SMark J Musante 			if (add_prop_list(
32051195e687SMark J Musante 			    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
32061195e687SMark J Musante 			    &props, B_TRUE) != 0) {
32071195e687SMark J Musante 				if (props)
32081195e687SMark J Musante 					nvlist_free(props);
32091195e687SMark J Musante 				usage(B_FALSE);
32101195e687SMark J Musante 			}
32111195e687SMark J Musante 			break;
32121195e687SMark J Musante 		case 'n':
32131195e687SMark J Musante 			flags.dryrun = B_TRUE;
32141195e687SMark J Musante 			break;
32151195e687SMark J Musante 		case 'o':
32161195e687SMark J Musante 			if ((propval = strchr(optarg, '=')) != NULL) {
32171195e687SMark J Musante 				*propval = '\0';
32181195e687SMark J Musante 				propval++;
32191195e687SMark J Musante 				if (add_prop_list(optarg, propval,
32201195e687SMark J Musante 				    &props, B_TRUE) != 0) {
32211195e687SMark J Musante 					if (props)
32221195e687SMark J Musante 						nvlist_free(props);
32231195e687SMark J Musante 					usage(B_FALSE);
32241195e687SMark J Musante 				}
32251195e687SMark J Musante 			} else {
32261195e687SMark J Musante 				mntopts = optarg;
32271195e687SMark J Musante 			}
32281195e687SMark J Musante 			break;
32291195e687SMark J Musante 		case ':':
32301195e687SMark J Musante 			(void) fprintf(stderr, gettext("missing argument for "
32311195e687SMark J Musante 			    "'%c' option\n"), optopt);
32321195e687SMark J Musante 			usage(B_FALSE);
32331195e687SMark J Musante 			break;
32341195e687SMark J Musante 		case '?':
32351195e687SMark J Musante 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
32361195e687SMark J Musante 			    optopt);
32371195e687SMark J Musante 			usage(B_FALSE);
32381195e687SMark J Musante 			break;
32391195e687SMark J Musante 		}
32401195e687SMark J Musante 	}
32411195e687SMark J Musante 
32421195e687SMark J Musante 	if (!flags.import && mntopts != NULL) {
32431195e687SMark J Musante 		(void) fprintf(stderr, gettext("setting mntopts is only "
32441195e687SMark J Musante 		    "valid when importing the pool\n"));
32451195e687SMark J Musante 		usage(B_FALSE);
32461195e687SMark J Musante 	}
32471195e687SMark J Musante 
32481195e687SMark J Musante 	argc -= optind;
32491195e687SMark J Musante 	argv += optind;
32501195e687SMark J Musante 
32511195e687SMark J Musante 	if (argc < 1) {
32521195e687SMark J Musante 		(void) fprintf(stderr, gettext("Missing pool name\n"));
32531195e687SMark J Musante 		usage(B_FALSE);
32541195e687SMark J Musante 	}
32551195e687SMark J Musante 	if (argc < 2) {
32561195e687SMark J Musante 		(void) fprintf(stderr, gettext("Missing new pool name\n"));
32571195e687SMark J Musante 		usage(B_FALSE);
32581195e687SMark J Musante 	}
32591195e687SMark J Musante 
32601195e687SMark J Musante 	srcpool = argv[0];
32611195e687SMark J Musante 	newpool = argv[1];
32621195e687SMark J Musante 
32631195e687SMark J Musante 	argc -= 2;
32641195e687SMark J Musante 	argv += 2;
32651195e687SMark J Musante 
32661195e687SMark J Musante 	if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
32671195e687SMark J Musante 		return (1);
32681195e687SMark J Musante 
32691195e687SMark J Musante 	config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
32701195e687SMark J Musante 	if (config == NULL) {
32711195e687SMark J Musante 		ret = 1;
32721195e687SMark J Musante 	} else {
32731195e687SMark J Musante 		if (flags.dryrun) {
32741195e687SMark J Musante 			(void) printf(gettext("would create '%s' with the "
32751195e687SMark J Musante 			    "following layout:\n\n"), newpool);
32761195e687SMark J Musante 			print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
32771195e687SMark J Musante 		}
32781195e687SMark J Musante 		nvlist_free(config);
32791195e687SMark J Musante 	}
32801195e687SMark J Musante 
32811195e687SMark J Musante 	zpool_close(zhp);
32821195e687SMark J Musante 
32831195e687SMark J Musante 	if (ret != 0 || flags.dryrun || !flags.import)
32841195e687SMark J Musante 		return (ret);
32851195e687SMark J Musante 
32861195e687SMark J Musante 	/*
32871195e687SMark J Musante 	 * The split was successful. Now we need to open the new
32881195e687SMark J Musante 	 * pool and import it.
32891195e687SMark J Musante 	 */
32901195e687SMark J Musante 	if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
32911195e687SMark J Musante 		return (1);
32921195e687SMark J Musante 	if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
32931195e687SMark J Musante 	    zpool_enable_datasets(zhp, mntopts, 0) != 0) {
32941195e687SMark J Musante 		ret = 1;
3295fe7cd8aaSCyril Plisko 		(void) fprintf(stderr, gettext("Split was successful, but "
32961195e687SMark J Musante 		    "the datasets could not all be mounted\n"));
32971195e687SMark J Musante 		(void) fprintf(stderr, gettext("Try doing '%s' with a "
32981195e687SMark J Musante 		    "different altroot\n"), "zpool import");
32991195e687SMark J Musante 	}
33001195e687SMark J Musante 	zpool_close(zhp);
33011195e687SMark J Musante 
33021195e687SMark J Musante 	return (ret);
33031195e687SMark J Musante }
33041195e687SMark J Musante 
33051195e687SMark J Musante 
33061195e687SMark J Musante 
33071195e687SMark J Musante /*
3308441d80aaSlling  * zpool online <pool> <device> ...
3309fa9e4066Sahrens  */
3310fa9e4066Sahrens int
3311fa9e4066Sahrens zpool_do_online(int argc, char **argv)
3312fa9e4066Sahrens {
3313fa9e4066Sahrens 	int c, i;
3314fa9e4066Sahrens 	char *poolname;
3315fa9e4066Sahrens 	zpool_handle_t *zhp;
3316fa9e4066Sahrens 	int ret = 0;
33173d7072f8Seschrock 	vdev_state_t newstate;
3318573ca77eSGeorge Wilson 	int flags = 0;
3319fa9e4066Sahrens 
3320fa9e4066Sahrens 	/* check options */
3321573ca77eSGeorge Wilson 	while ((c = getopt(argc, argv, "et")) != -1) {
3322fa9e4066Sahrens 		switch (c) {
3323573ca77eSGeorge Wilson 		case 'e':
3324573ca77eSGeorge Wilson 			flags |= ZFS_ONLINE_EXPAND;
3325573ca77eSGeorge Wilson 			break;
3326fa9e4066Sahrens 		case 't':
3327fa9e4066Sahrens 		case '?':
3328fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3329fa9e4066Sahrens 			    optopt);
333099653d4eSeschrock 			usage(B_FALSE);
3331fa9e4066Sahrens 		}
3332fa9e4066Sahrens 	}
3333fa9e4066Sahrens 
3334fa9e4066Sahrens 	argc -= optind;
3335fa9e4066Sahrens 	argv += optind;
3336fa9e4066Sahrens 
3337fa9e4066Sahrens 	/* get pool name and check number of arguments */
3338fa9e4066Sahrens 	if (argc < 1) {
3339fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name\n"));
334099653d4eSeschrock 		usage(B_FALSE);
3341fa9e4066Sahrens 	}
3342fa9e4066Sahrens 	if (argc < 2) {
3343fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing device name\n"));
334499653d4eSeschrock 		usage(B_FALSE);
3345fa9e4066Sahrens 	}
3346fa9e4066Sahrens 
3347fa9e4066Sahrens 	poolname = argv[0];
3348fa9e4066Sahrens 
334999653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3350fa9e4066Sahrens 		return (1);
3351fa9e4066Sahrens 
33523d7072f8Seschrock 	for (i = 1; i < argc; i++) {
3353573ca77eSGeorge Wilson 		if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
33543d7072f8Seschrock 			if (newstate != VDEV_STATE_HEALTHY) {
33553d7072f8Seschrock 				(void) printf(gettext("warning: device '%s' "
33563d7072f8Seschrock 				    "onlined, but remains in faulted state\n"),
3357fa9e4066Sahrens 				    argv[i]);
33583d7072f8Seschrock 				if (newstate == VDEV_STATE_FAULTED)
33593d7072f8Seschrock 					(void) printf(gettext("use 'zpool "
33603d7072f8Seschrock 					    "clear' to restore a faulted "
33613d7072f8Seschrock 					    "device\n"));
3362fa9e4066Sahrens 				else
33633d7072f8Seschrock 					(void) printf(gettext("use 'zpool "
33643d7072f8Seschrock 					    "replace' to replace devices "
33653d7072f8Seschrock 					    "that are no longer present\n"));
33663d7072f8Seschrock 			}
33673d7072f8Seschrock 		} else {
3368fa9e4066Sahrens 			ret = 1;
33693d7072f8Seschrock 		}
33703d7072f8Seschrock 	}
3371fa9e4066Sahrens 
337299653d4eSeschrock 	zpool_close(zhp);
337399653d4eSeschrock 
3374fa9e4066Sahrens 	return (ret);
3375fa9e4066Sahrens }
3376fa9e4066Sahrens 
3377fa9e4066Sahrens /*
3378441d80aaSlling  * zpool offline [-ft] <pool> <device> ...
3379fa9e4066Sahrens  *
3380fa9e4066Sahrens  *	-f	Force the device into the offline state, even if doing
3381fa9e4066Sahrens  *		so would appear to compromise pool availability.
3382fa9e4066Sahrens  *		(not supported yet)
3383fa9e4066Sahrens  *
3384fa9e4066Sahrens  *	-t	Only take the device off-line temporarily.  The offline
3385fa9e4066Sahrens  *		state will not be persistent across reboots.
3386fa9e4066Sahrens  */
3387fa9e4066Sahrens /* ARGSUSED */
3388fa9e4066Sahrens int
3389fa9e4066Sahrens zpool_do_offline(int argc, char **argv)
3390fa9e4066Sahrens {
3391fa9e4066Sahrens 	int c, i;
3392fa9e4066Sahrens 	char *poolname;
3393fa9e4066Sahrens 	zpool_handle_t *zhp;
339499653d4eSeschrock 	int ret = 0;
339599653d4eSeschrock 	boolean_t istmp = B_FALSE;
3396fa9e4066Sahrens 
3397fa9e4066Sahrens 	/* check options */
3398fa9e4066Sahrens 	while ((c = getopt(argc, argv, "ft")) != -1) {
3399fa9e4066Sahrens 		switch (c) {
3400fa9e4066Sahrens 		case 't':
340199653d4eSeschrock 			istmp = B_TRUE;
3402441d80aaSlling 			break;
3403441d80aaSlling 		case 'f':
3404fa9e4066Sahrens 		case '?':
3405fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3406fa9e4066Sahrens 			    optopt);
340799653d4eSeschrock 			usage(B_FALSE);
3408fa9e4066Sahrens 		}
3409fa9e4066Sahrens 	}
3410fa9e4066Sahrens 
3411fa9e4066Sahrens 	argc -= optind;
3412fa9e4066Sahrens 	argv += optind;
3413fa9e4066Sahrens 
3414fa9e4066Sahrens 	/* get pool name and check number of arguments */
3415fa9e4066Sahrens 	if (argc < 1) {
3416fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name\n"));
341799653d4eSeschrock 		usage(B_FALSE);
3418fa9e4066Sahrens 	}
3419fa9e4066Sahrens 	if (argc < 2) {
3420fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing device name\n"));
342199653d4eSeschrock 		usage(B_FALSE);
3422fa9e4066Sahrens 	}
3423fa9e4066Sahrens 
3424fa9e4066Sahrens 	poolname = argv[0];
3425fa9e4066Sahrens 
342699653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3427fa9e4066Sahrens 		return (1);
3428fa9e4066Sahrens 
34293d7072f8Seschrock 	for (i = 1; i < argc; i++) {
34303d7072f8Seschrock 		if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3431fa9e4066Sahrens 			ret = 1;
34323d7072f8Seschrock 	}
3433fa9e4066Sahrens 
343499653d4eSeschrock 	zpool_close(zhp);
343599653d4eSeschrock 
3436fa9e4066Sahrens 	return (ret);
3437fa9e4066Sahrens }
3438fa9e4066Sahrens 
3439ea8dc4b6Seschrock /*
3440ea8dc4b6Seschrock  * zpool clear <pool> [device]
3441ea8dc4b6Seschrock  *
3442ea8dc4b6Seschrock  * Clear all errors associated with a pool or a particular device.
3443ea8dc4b6Seschrock  */
3444ea8dc4b6Seschrock int
3445ea8dc4b6Seschrock zpool_do_clear(int argc, char **argv)
3446ea8dc4b6Seschrock {
3447468c413aSTim Haley 	int c;
3448ea8dc4b6Seschrock 	int ret = 0;
3449468c413aSTim Haley 	boolean_t dryrun = B_FALSE;
3450468c413aSTim Haley 	boolean_t do_rewind = B_FALSE;
3451468c413aSTim Haley 	boolean_t xtreme_rewind = B_FALSE;
3452468c413aSTim Haley 	uint32_t rewind_policy = ZPOOL_NO_REWIND;
3453468c413aSTim Haley 	nvlist_t *policy = NULL;
3454ea8dc4b6Seschrock 	zpool_handle_t *zhp;
3455ea8dc4b6Seschrock 	char *pool, *device;
3456ea8dc4b6Seschrock 
3457468c413aSTim Haley 	/* check options */
3458468c413aSTim Haley 	while ((c = getopt(argc, argv, "FnX")) != -1) {
3459468c413aSTim Haley 		switch (c) {
3460468c413aSTim Haley 		case 'F':
3461468c413aSTim Haley 			do_rewind = B_TRUE;
3462468c413aSTim Haley 			break;
3463468c413aSTim Haley 		case 'n':
3464468c413aSTim Haley 			dryrun = B_TRUE;
3465468c413aSTim Haley 			break;
3466468c413aSTim Haley 		case 'X':
3467468c413aSTim Haley 			xtreme_rewind = B_TRUE;
3468468c413aSTim Haley 			break;
3469468c413aSTim Haley 		case '?':
3470468c413aSTim Haley 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3471468c413aSTim Haley 			    optopt);
3472468c413aSTim Haley 			usage(B_FALSE);
3473468c413aSTim Haley 		}
3474468c413aSTim Haley 	}
3475468c413aSTim Haley 
3476468c413aSTim Haley 	argc -= optind;
3477468c413aSTim Haley 	argv += optind;
3478468c413aSTim Haley 
3479468c413aSTim Haley 	if (argc < 1) {
3480ea8dc4b6Seschrock 		(void) fprintf(stderr, gettext("missing pool name\n"));
348199653d4eSeschrock 		usage(B_FALSE);
3482ea8dc4b6Seschrock 	}
3483ea8dc4b6Seschrock 
3484468c413aSTim Haley 	if (argc > 2) {
3485ea8dc4b6Seschrock 		(void) fprintf(stderr, gettext("too many arguments\n"));
348699653d4eSeschrock 		usage(B_FALSE);
3487ea8dc4b6Seschrock 	}
3488ea8dc4b6Seschrock 
3489468c413aSTim Haley 	if ((dryrun || xtreme_rewind) && !do_rewind) {
3490468c413aSTim Haley 		(void) fprintf(stderr,
3491468c413aSTim Haley 		    gettext("-n or -X only meaningful with -F\n"));
3492468c413aSTim Haley 		usage(B_FALSE);
3493468c413aSTim Haley 	}
3494468c413aSTim Haley 	if (dryrun)
3495468c413aSTim Haley 		rewind_policy = ZPOOL_TRY_REWIND;
3496468c413aSTim Haley 	else if (do_rewind)
3497468c413aSTim Haley 		rewind_policy = ZPOOL_DO_REWIND;
3498468c413aSTim Haley 	if (xtreme_rewind)
3499468c413aSTim Haley 		rewind_policy |= ZPOOL_EXTREME_REWIND;
3500ea8dc4b6Seschrock 
3501468c413aSTim Haley 	/* In future, further rewind policy choices can be passed along here */
3502468c413aSTim Haley 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3503468c413aSTim Haley 	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3504ea8dc4b6Seschrock 		return (1);
3505ea8dc4b6Seschrock 
3506468c413aSTim Haley 	pool = argv[0];
3507468c413aSTim Haley 	device = argc == 2 ? argv[1] : NULL;
3508468c413aSTim Haley 
3509468c413aSTim Haley 	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3510468c413aSTim Haley 		nvlist_free(policy);
3511468c413aSTim Haley 		return (1);
3512468c413aSTim Haley 	}
3513468c413aSTim Haley 
3514468c413aSTim Haley 	if (zpool_clear(zhp, device, policy) != 0)
3515ea8dc4b6Seschrock 		ret = 1;
3516ea8dc4b6Seschrock 
3517ea8dc4b6Seschrock 	zpool_close(zhp);
3518ea8dc4b6Seschrock 
3519468c413aSTim Haley 	nvlist_free(policy);
3520468c413aSTim Haley 
3521ea8dc4b6Seschrock 	return (ret);
3522ea8dc4b6Seschrock }
3523ea8dc4b6Seschrock 
3524e9103aaeSGarrett D'Amore /*
3525e9103aaeSGarrett D'Amore  * zpool reguid <pool>
3526e9103aaeSGarrett D'Amore  */
3527e9103aaeSGarrett D'Amore int
3528e9103aaeSGarrett D'Amore zpool_do_reguid(int argc, char **argv)
3529e9103aaeSGarrett D'Amore {
3530e9103aaeSGarrett D'Amore 	int c;
3531e9103aaeSGarrett D'Amore 	char *poolname;
3532e9103aaeSGarrett D'Amore 	zpool_handle_t *zhp;
3533e9103aaeSGarrett D'Amore 	int ret = 0;
3534e9103aaeSGarrett D'Amore 
3535e9103aaeSGarrett D'Amore 	/* check options */
3536e9103aaeSGarrett D'Amore 	while ((c = getopt(argc, argv, "")) != -1) {
3537e9103aaeSGarrett D'Amore 		switch (c) {
3538e9103aaeSGarrett D'Amore 		case '?':
3539e9103aaeSGarrett D'Amore 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3540e9103aaeSGarrett D'Amore 			    optopt);
3541e9103aaeSGarrett D'Amore 			usage(B_FALSE);
3542e9103aaeSGarrett D'Amore 		}
3543e9103aaeSGarrett D'Amore 	}
3544e9103aaeSGarrett D'Amore 
3545e9103aaeSGarrett D'Amore 	argc -= optind;
3546e9103aaeSGarrett D'Amore 	argv += optind;
3547e9103aaeSGarrett D'Amore 
3548e9103aaeSGarrett D'Amore 	/* get pool name and check number of arguments */
3549e9103aaeSGarrett D'Amore 	if (argc < 1) {
3550e9103aaeSGarrett D'Amore 		(void) fprintf(stderr, gettext("missing pool name\n"));
3551e9103aaeSGarrett D'Amore 		usage(B_FALSE);
3552e9103aaeSGarrett D'Amore 	}
3553e9103aaeSGarrett D'Amore 
3554e9103aaeSGarrett D'Amore 	if (argc > 1) {
3555e9103aaeSGarrett D'Amore 		(void) fprintf(stderr, gettext("too many arguments\n"));
3556e9103aaeSGarrett D'Amore 		usage(B_FALSE);
3557e9103aaeSGarrett D'Amore 	}
3558e9103aaeSGarrett D'Amore 
3559e9103aaeSGarrett D'Amore 	poolname = argv[0];
3560e9103aaeSGarrett D'Amore 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3561e9103aaeSGarrett D'Amore 		return (1);
3562e9103aaeSGarrett D'Amore 
3563e9103aaeSGarrett D'Amore 	ret = zpool_reguid(zhp);
3564e9103aaeSGarrett D'Amore 
3565e9103aaeSGarrett D'Amore 	zpool_close(zhp);
3566e9103aaeSGarrett D'Amore 	return (ret);
3567e9103aaeSGarrett D'Amore }
3568e9103aaeSGarrett D'Amore 
3569e9103aaeSGarrett D'Amore 
35704263d13fSGeorge Wilson /*
35714263d13fSGeorge Wilson  * zpool reopen <pool>
35724263d13fSGeorge Wilson  *
35734263d13fSGeorge Wilson  * Reopen the pool so that the kernel can update the sizes of all vdevs.
35744263d13fSGeorge Wilson  */
35754263d13fSGeorge Wilson int
35764263d13fSGeorge Wilson zpool_do_reopen(int argc, char **argv)
35774263d13fSGeorge Wilson {
357831d7e8faSGeorge Wilson 	int c;
35794263d13fSGeorge Wilson 	int ret = 0;
35804263d13fSGeorge Wilson 	zpool_handle_t *zhp;
35814263d13fSGeorge Wilson 	char *pool;
35824263d13fSGeorge Wilson 
358331d7e8faSGeorge Wilson 	/* check options */
358431d7e8faSGeorge Wilson 	while ((c = getopt(argc, argv, "")) != -1) {
358531d7e8faSGeorge Wilson 		switch (c) {
358631d7e8faSGeorge Wilson 		case '?':
358731d7e8faSGeorge Wilson 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
358831d7e8faSGeorge Wilson 			    optopt);
358931d7e8faSGeorge Wilson 			usage(B_FALSE);
359031d7e8faSGeorge Wilson 		}
359131d7e8faSGeorge Wilson 	}
359231d7e8faSGeorge Wilson 
35934263d13fSGeorge Wilson 	argc--;
35944263d13fSGeorge Wilson 	argv++;
35954263d13fSGeorge Wilson 
359631d7e8faSGeorge Wilson 	if (argc < 1) {
359731d7e8faSGeorge Wilson 		(void) fprintf(stderr, gettext("missing pool name\n"));
359831d7e8faSGeorge Wilson 		usage(B_FALSE);
359931d7e8faSGeorge Wilson 	}
360031d7e8faSGeorge Wilson 
360131d7e8faSGeorge Wilson 	if (argc > 1) {
360231d7e8faSGeorge Wilson 		(void) fprintf(stderr, gettext("too many arguments\n"));
360331d7e8faSGeorge Wilson 		usage(B_FALSE);
360431d7e8faSGeorge Wilson 	}
36054263d13fSGeorge Wilson 
36064263d13fSGeorge Wilson 	pool = argv[0];
36074263d13fSGeorge Wilson 	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
36084263d13fSGeorge Wilson 		return (1);
36094263d13fSGeorge Wilson 
36104263d13fSGeorge Wilson 	ret = zpool_reopen(zhp);
36114263d13fSGeorge Wilson 	zpool_close(zhp);
36124263d13fSGeorge Wilson 	return (ret);
36134263d13fSGeorge Wilson }
36144263d13fSGeorge Wilson 
3615fa9e4066Sahrens typedef struct scrub_cbdata {
3616fa9e4066Sahrens 	int	cb_type;
361706eeb2adSek110237 	int	cb_argc;
361806eeb2adSek110237 	char	**cb_argv;
3619fa9e4066Sahrens } scrub_cbdata_t;
3620fa9e4066Sahrens 
3621fa9e4066Sahrens int
3622fa9e4066Sahrens scrub_callback(zpool_handle_t *zhp, void *data)
3623fa9e4066Sahrens {
3624fa9e4066Sahrens 	scrub_cbdata_t *cb = data;
362506eeb2adSek110237 	int err;
3626fa9e4066Sahrens 
3627ea8dc4b6Seschrock 	/*
3628ea8dc4b6Seschrock 	 * Ignore faulted pools.
3629ea8dc4b6Seschrock 	 */
3630ea8dc4b6Seschrock 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3631ea8dc4b6Seschrock 		(void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3632ea8dc4b6Seschrock 		    "currently unavailable\n"), zpool_get_name(zhp));
3633ea8dc4b6Seschrock 		return (1);
3634ea8dc4b6Seschrock 	}
3635ea8dc4b6Seschrock 
36363f9d6ad7SLin Ling 	err = zpool_scan(zhp, cb->cb_type);
363706eeb2adSek110237 
363806eeb2adSek110237 	return (err != 0);
3639fa9e4066Sahrens }
3640fa9e4066Sahrens 
3641fa9e4066Sahrens /*
3642fa9e4066Sahrens  * zpool scrub [-s] <pool> ...
3643fa9e4066Sahrens  *
3644fa9e4066Sahrens  *	-s	Stop.  Stops any in-progress scrub.
3645fa9e4066Sahrens  */
3646fa9e4066Sahrens int
3647fa9e4066Sahrens zpool_do_scrub(int argc, char **argv)
3648fa9e4066Sahrens {
3649fa9e4066Sahrens 	int c;
3650fa9e4066Sahrens 	scrub_cbdata_t cb;
3651fa9e4066Sahrens 
36523f9d6ad7SLin Ling 	cb.cb_type = POOL_SCAN_SCRUB;
3653fa9e4066Sahrens 
3654fa9e4066Sahrens 	/* check options */
3655fa9e4066Sahrens 	while ((c = getopt(argc, argv, "s")) != -1) {
3656fa9e4066Sahrens 		switch (c) {
3657fa9e4066Sahrens 		case 's':
36583f9d6ad7SLin Ling 			cb.cb_type = POOL_SCAN_NONE;
3659fa9e4066Sahrens 			break;
3660fa9e4066Sahrens 		case '?':
3661fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3662fa9e4066Sahrens 			    optopt);
366399653d4eSeschrock 			usage(B_FALSE);
3664fa9e4066Sahrens 		}
3665fa9e4066Sahrens 	}
3666fa9e4066Sahrens 
366706eeb2adSek110237 	cb.cb_argc = argc;
366806eeb2adSek110237 	cb.cb_argv = argv;
3669fa9e4066Sahrens 	argc -= optind;
3670fa9e4066Sahrens 	argv += optind;
3671fa9e4066Sahrens 
3672fa9e4066Sahrens 	if (argc < 1) {
3673fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
367499653d4eSeschrock 		usage(B_FALSE);
3675fa9e4066Sahrens 	}
3676fa9e4066Sahrens 
3677b1b8ab34Slling 	return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3678fa9e4066Sahrens }
3679fa9e4066Sahrens 
3680fa9e4066Sahrens typedef struct status_cbdata {
3681fa9e4066Sahrens 	int		cb_count;
3682e9dbad6fSeschrock 	boolean_t	cb_allpools;
368399653d4eSeschrock 	boolean_t	cb_verbose;
368499653d4eSeschrock 	boolean_t	cb_explain;
368599653d4eSeschrock 	boolean_t	cb_first;
36869eb19f4dSGeorge Wilson 	boolean_t	cb_dedup_stats;
3687fa9e4066Sahrens } status_cbdata_t;
3688fa9e4066Sahrens 
3689fa9e4066Sahrens /*
3690fa9e4066Sahrens  * Print out detailed scrub status.
3691fa9e4066Sahrens  */
3692fa9e4066Sahrens void
36933f9d6ad7SLin Ling print_scan_status(pool_scan_stat_t *ps)
3694fa9e4066Sahrens {
36953f9d6ad7SLin Ling 	time_t start, end;
369673d314ceSLin Ling 	uint64_t elapsed, mins_left, hours_left;
36973f9d6ad7SLin Ling 	uint64_t pass_exam, examined, total;
36983f9d6ad7SLin Ling 	uint_t rate;
3699fa9e4066Sahrens 	double fraction_done;
37003f9d6ad7SLin Ling 	char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3701fa9e4066Sahrens 
37023f9d6ad7SLin Ling 	(void) printf(gettext("  scan: "));
3703fa9e4066Sahrens 
37043f9d6ad7SLin Ling 	/* If there's never been a scan, there's not much to say. */
37053f9d6ad7SLin Ling 	if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
37063f9d6ad7SLin Ling 	    ps->pss_func >= POOL_SCAN_FUNCS) {
3707fa9e4066Sahrens 		(void) printf(gettext("none requested\n"));
3708fa9e4066Sahrens 		return;
3709fa9e4066Sahrens 	}
3710fa9e4066Sahrens 
37113f9d6ad7SLin Ling 	start = ps->pss_start_time;
37123f9d6ad7SLin Ling 	end = ps->pss_end_time;
37133f9d6ad7SLin Ling 	zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3714fa9e4066Sahrens 
37153f9d6ad7SLin Ling 	assert(ps->pss_func == POOL_SCAN_SCRUB ||
37163f9d6ad7SLin Ling 	    ps->pss_func == POOL_SCAN_RESILVER);
37173f9d6ad7SLin Ling 	/*
37183f9d6ad7SLin Ling 	 * Scan is finished or canceled.
37193f9d6ad7SLin Ling 	 */
37203f9d6ad7SLin Ling 	if (ps->pss_state == DSS_FINISHED) {
37213f9d6ad7SLin Ling 		uint64_t minutes_taken = (end - start) / 60;
37223f9d6ad7SLin Ling 		char *fmt;
3723fa9e4066Sahrens 
37243f9d6ad7SLin Ling 		if (ps->pss_func == POOL_SCAN_SCRUB) {
37253f9d6ad7SLin Ling 			fmt = gettext("scrub repaired %s in %lluh%um with "
37263f9d6ad7SLin Ling 			    "%llu errors on %s");
37273f9d6ad7SLin Ling 		} else if (ps->pss_func == POOL_SCAN_RESILVER) {
37283f9d6ad7SLin Ling 			fmt = gettext("resilvered %s in %lluh%um with "
37293f9d6ad7SLin Ling 			    "%llu errors on %s");
37303f9d6ad7SLin Ling 		}
37313f9d6ad7SLin Ling 		/* LINTED */
37323f9d6ad7SLin Ling 		(void) printf(fmt, processed_buf,
373318ce54dfSek110237 		    (u_longlong_t)(minutes_taken / 60),
373418ce54dfSek110237 		    (uint_t)(minutes_taken % 60),
37353f9d6ad7SLin Ling 		    (u_longlong_t)ps->pss_errors,
37363f9d6ad7SLin Ling 		    ctime((time_t *)&end));
37373f9d6ad7SLin Ling 		return;
37383f9d6ad7SLin Ling 	} else if (ps->pss_state == DSS_CANCELED) {
37393f9d6ad7SLin Ling 		if (ps->pss_func == POOL_SCAN_SCRUB) {
37403f9d6ad7SLin Ling 			(void) printf(gettext("scrub canceled on %s"),
37413f9d6ad7SLin Ling 			    ctime(&end));
37423f9d6ad7SLin Ling 		} else if (ps->pss_func == POOL_SCAN_RESILVER) {
37433f9d6ad7SLin Ling 			(void) printf(gettext("resilver canceled on %s"),
37443f9d6ad7SLin Ling 			    ctime(&end));
37453f9d6ad7SLin Ling 		}
3746fa9e4066Sahrens 		return;
3747fa9e4066Sahrens 	}
3748fa9e4066Sahrens 
37493f9d6ad7SLin Ling 	assert(ps->pss_state == DSS_SCANNING);
3750fa9e4066Sahrens 
37513f9d6ad7SLin Ling 	/*
37523f9d6ad7SLin Ling 	 * Scan is in progress.
37533f9d6ad7SLin Ling 	 */
37543f9d6ad7SLin Ling 	if (ps->pss_func == POOL_SCAN_SCRUB) {
37553f9d6ad7SLin Ling 		(void) printf(gettext("scrub in progress since %s"),
37563f9d6ad7SLin Ling 		    ctime(&start));
37573f9d6ad7SLin Ling 	} else if (ps->pss_func == POOL_SCAN_RESILVER) {
37583f9d6ad7SLin Ling 		(void) printf(gettext("resilver in progress since %s"),
37593f9d6ad7SLin Ling 		    ctime(&start));
37603f9d6ad7SLin Ling 	}
37613f9d6ad7SLin Ling 
37623f9d6ad7SLin Ling 	examined = ps->pss_examined ? ps->pss_examined : 1;
37633f9d6ad7SLin Ling 	total = ps->pss_to_examine;
3764fa9e4066Sahrens 	fraction_done = (double)examined / total;
3765fa9e4066Sahrens 
37663f9d6ad7SLin Ling 	/* elapsed time for this pass */
37673f9d6ad7SLin Ling 	elapsed = time(NULL) - ps->pss_pass_start;
37683f9d6ad7SLin Ling 	elapsed = elapsed ? elapsed : 1;
37693f9d6ad7SLin Ling 	pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
37703f9d6ad7SLin Ling 	rate = pass_exam / elapsed;
37713f9d6ad7SLin Ling 	rate = rate ? rate : 1;
37723f9d6ad7SLin Ling 	mins_left = ((total - examined) / rate) / 60;
377373d314ceSLin Ling 	hours_left = mins_left / 60;
37743f9d6ad7SLin Ling 
37753f9d6ad7SLin Ling 	zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
37763f9d6ad7SLin Ling 	zfs_nicenum(total, total_buf, sizeof (total_buf));
37773f9d6ad7SLin Ling 	zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
37783f9d6ad7SLin Ling 
377973d314ceSLin Ling 	/*
378073d314ceSLin Ling 	 * do not print estimated time if hours_left is more than 30 days
378173d314ceSLin Ling 	 */
378273d314ceSLin Ling 	(void) printf(gettext("    %s scanned out of %s at %s/s"),
378373d314ceSLin Ling 	    examined_buf, total_buf, rate_buf);
378473d314ceSLin Ling 	if (hours_left < (30 * 24)) {
378573d314ceSLin Ling 		(void) printf(gettext(", %lluh%um to go\n"),
378673d314ceSLin Ling 		    (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
378773d314ceSLin Ling 	} else {
378873d314ceSLin Ling 		(void) printf(gettext(
378973d314ceSLin Ling 		    ", (scan is slow, no estimated time)\n"));
379073d314ceSLin Ling 	}
37913f9d6ad7SLin Ling 
37923f9d6ad7SLin Ling 	if (ps->pss_func == POOL_SCAN_RESILVER) {
37933f9d6ad7SLin Ling 		(void) printf(gettext("    %s resilvered, %.2f%% done\n"),
37943f9d6ad7SLin Ling 		    processed_buf, 100 * fraction_done);
37953f9d6ad7SLin Ling 	} else if (ps->pss_func == POOL_SCAN_SCRUB) {
37963f9d6ad7SLin Ling 		(void) printf(gettext("    %s repaired, %.2f%% done\n"),
37973f9d6ad7SLin Ling 		    processed_buf, 100 * fraction_done);
37983f9d6ad7SLin Ling 	}
3799fa9e4066Sahrens }
3800fa9e4066Sahrens 
3801ea8dc4b6Seschrock static void
3802ea8dc4b6Seschrock print_error_log(zpool_handle_t *zhp)
3803ea8dc4b6Seschrock {
380475519f38Sek110237 	nvlist_t *nverrlist = NULL;
380555434c77Sek110237 	nvpair_t *elem;
380655434c77Sek110237 	char *pathname;
380755434c77Sek110237 	size_t len = MAXPATHLEN * 2;
3808ea8dc4b6Seschrock 
380955434c77Sek110237 	if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3810ea8dc4b6Seschrock 		(void) printf("errors: List of errors unavailable "
3811ea8dc4b6Seschrock 		    "(insufficient privileges)\n");
3812ea8dc4b6Seschrock 		return;
3813ea8dc4b6Seschrock 	}
3814ea8dc4b6Seschrock 
381555434c77Sek110237 	(void) printf("errors: Permanent errors have been "
381655434c77Sek110237 	    "detected in the following files:\n\n");
3817ea8dc4b6Seschrock 
381855434c77Sek110237 	pathname = safe_malloc(len);
381955434c77Sek110237 	elem = NULL;
382055434c77Sek110237 	while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
382155434c77Sek110237 		nvlist_t *nv;
382255434c77Sek110237 		uint64_t dsobj, obj;
3823ea8dc4b6Seschrock 
382455434c77Sek110237 		verify(nvpair_value_nvlist(elem, &nv) == 0);
382555434c77Sek110237 		verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
382655434c77Sek110237 		    &dsobj) == 0);
382755434c77Sek110237 		verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
382855434c77Sek110237 		    &obj) == 0);
382955434c77Sek110237 		zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
383055434c77Sek110237 		(void) printf("%7s %s\n", "", pathname);
3831ea8dc4b6Seschrock 	}
383255434c77Sek110237 	free(pathname);
383355434c77Sek110237 	nvlist_free(nverrlist);
3834ea8dc4b6Seschrock }
3835ea8dc4b6Seschrock 
383699653d4eSeschrock static void
383799653d4eSeschrock print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
383899653d4eSeschrock     int namewidth)
383999653d4eSeschrock {
384099653d4eSeschrock 	uint_t i;
384199653d4eSeschrock 	char *name;
384299653d4eSeschrock 
384399653d4eSeschrock 	if (nspares == 0)
384499653d4eSeschrock 		return;
384599653d4eSeschrock 
384699653d4eSeschrock 	(void) printf(gettext("\tspares\n"));
384799653d4eSeschrock 
384899653d4eSeschrock 	for (i = 0; i < nspares; i++) {
384988ecc943SGeorge Wilson 		name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
385099653d4eSeschrock 		print_status_config(zhp, name, spares[i],
3851aa8cf21aSNeil Perrin 		    namewidth, 2, B_TRUE);
385299653d4eSeschrock 		free(name);
385399653d4eSeschrock 	}
385499653d4eSeschrock }
385599653d4eSeschrock 
3856fa94a07fSbrendan static void
3857fa94a07fSbrendan print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3858fa94a07fSbrendan     int namewidth)
3859fa94a07fSbrendan {
3860fa94a07fSbrendan 	uint_t i;
3861fa94a07fSbrendan 	char *name;
3862fa94a07fSbrendan 
3863fa94a07fSbrendan 	if (nl2cache == 0)
3864fa94a07fSbrendan 		return;
3865fa94a07fSbrendan 
3866fa94a07fSbrendan 	(void) printf(gettext("\tcache\n"));
3867fa94a07fSbrendan 
3868fa94a07fSbrendan 	for (i = 0; i < nl2cache; i++) {
386988ecc943SGeorge Wilson 		name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3870fa94a07fSbrendan 		print_status_config(zhp, name, l2cache[i],
3871aa8cf21aSNeil Perrin 		    namewidth, 2, B_FALSE);
3872aa8cf21aSNeil Perrin 		free(name);
3873aa8cf21aSNeil Perrin 	}
3874aa8cf21aSNeil Perrin }
3875aa8cf21aSNeil Perrin 
38769eb19f4dSGeorge Wilson static void
38779eb19f4dSGeorge Wilson print_dedup_stats(nvlist_t *config)
38789eb19f4dSGeorge Wilson {
38799eb19f4dSGeorge Wilson 	ddt_histogram_t *ddh;
38809eb19f4dSGeorge Wilson 	ddt_stat_t *dds;
38819eb19f4dSGeorge Wilson 	ddt_object_t *ddo;
38829eb19f4dSGeorge Wilson 	uint_t c;
38839eb19f4dSGeorge Wilson 
38849eb19f4dSGeorge Wilson 	/*
38859eb19f4dSGeorge Wilson 	 * If the pool was faulted then we may not have been able to
38862384d9f8SGeorge Wilson 	 * obtain the config. Otherwise, if we have anything in the dedup
38879eb19f4dSGeorge Wilson 	 * table continue processing the stats.
38889eb19f4dSGeorge Wilson 	 */
38899eb19f4dSGeorge Wilson 	if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
3890ce72e614SYuri Pankov 	    (uint64_t **)&ddo, &c) != 0)
38919eb19f4dSGeorge Wilson 		return;
38929eb19f4dSGeorge Wilson 
38939eb19f4dSGeorge Wilson 	(void) printf("\n");
3894ce72e614SYuri Pankov 	(void) printf(gettext(" dedup: "));
3895ce72e614SYuri Pankov 	if (ddo->ddo_count == 0) {
3896ce72e614SYuri Pankov 		(void) printf(gettext("no DDT entries\n"));
3897ce72e614SYuri Pankov 		return;
3898ce72e614SYuri Pankov 	}
3899ce72e614SYuri Pankov 
39009eb19f4dSGeorge Wilson 	(void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
39019eb19f4dSGeorge Wilson 	    (u_longlong_t)ddo->ddo_count,
39029eb19f4dSGeorge Wilson 	    (u_longlong_t)ddo->ddo_dspace,
39039eb19f4dSGeorge Wilson 	    (u_longlong_t)ddo->ddo_mspace);
39049eb19f4dSGeorge Wilson 
39059eb19f4dSGeorge Wilson 	verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
39069eb19f4dSGeorge Wilson 	    (uint64_t **)&dds, &c) == 0);
39079eb19f4dSGeorge Wilson 	verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
39089eb19f4dSGeorge Wilson 	    (uint64_t **)&ddh, &c) == 0);
39099eb19f4dSGeorge Wilson 	zpool_dump_ddt(dds, ddh);
39109eb19f4dSGeorge Wilson }
39119eb19f4dSGeorge Wilson 
3912aa8cf21aSNeil Perrin /*
3913fa9e4066Sahrens  * Display a summary of pool status.  Displays a summary such as:
3914fa9e4066Sahrens  *
3915fa9e4066Sahrens  *        pool: tank
3916fa9e4066Sahrens  *	status: DEGRADED
3917fa9e4066Sahrens  *	reason: One or more devices ...
3918654b400cSJoshua M. Clulow  *         see: http://illumos.org/msg/ZFS-xxxx-01
3919fa9e4066Sahrens  *	config:
3920fa9e4066Sahrens  *		mirror		DEGRADED
3921fa9e4066Sahrens  *                c1t0d0	OK
3922ea8dc4b6Seschrock  *                c2t0d0	UNAVAIL
3923fa9e4066Sahrens  *
3924fa9e4066Sahrens  * When given the '-v' option, we print out the complete config.  If the '-e'
3925fa9e4066Sahrens  * option is specified, then we print out error rate information as well.
3926fa9e4066Sahrens  */
3927fa9e4066Sahrens int
3928fa9e4066Sahrens status_callback(zpool_handle_t *zhp, void *data)
3929fa9e4066Sahrens {
3930fa9e4066Sahrens 	status_cbdata_t *cbp = data;
3931fa9e4066Sahrens 	nvlist_t *config, *nvroot;
3932fa9e4066Sahrens 	char *msgid;
3933fa9e4066Sahrens 	int reason;
393446657f8dSmmusante 	const char *health;
393546657f8dSmmusante 	uint_t c;
393646657f8dSmmusante 	vdev_stat_t *vs;
3937fa9e4066Sahrens 
3938088e9d47Seschrock 	config = zpool_get_config(zhp, NULL);
3939fa9e4066Sahrens 	reason = zpool_get_status(zhp, &msgid);
3940fa9e4066Sahrens 
3941fa9e4066Sahrens 	cbp->cb_count++;
3942fa9e4066Sahrens 
3943fa9e4066Sahrens 	/*
3944fa9e4066Sahrens 	 * If we were given 'zpool status -x', only report those pools with
3945fa9e4066Sahrens 	 * problems.
3946fa9e4066Sahrens 	 */
3947b3a6f804STim Connors 	if (cbp->cb_explain &&
3948b3a6f804STim Connors 	    (reason == ZPOOL_STATUS_OK ||
3949b3a6f804STim Connors 	    reason == ZPOOL_STATUS_VERSION_OLDER ||
3950b3a6f804STim Connors 	    reason == ZPOOL_STATUS_FEAT_DISABLED)) {
3951e9dbad6fSeschrock 		if (!cbp->cb_allpools) {
3952e9dbad6fSeschrock 			(void) printf(gettext("pool '%s' is healthy\n"),
3953e9dbad6fSeschrock 			    zpool_get_name(zhp));
3954e9dbad6fSeschrock 			if (cbp->cb_first)
3955e9dbad6fSeschrock 				cbp->cb_first = B_FALSE;
3956e9dbad6fSeschrock 		}
3957fa9e4066Sahrens 		return (0);
3958e9dbad6fSeschrock 	}
3959fa9e4066Sahrens 
3960fa9e4066Sahrens 	if (cbp->cb_first)
396199653d4eSeschrock 		cbp->cb_first = B_FALSE;
3962fa9e4066Sahrens 	else
3963fa9e4066Sahrens 		(void) printf("\n");
3964fa9e4066Sahrens 
396546657f8dSmmusante 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
396646657f8dSmmusante 	    &nvroot) == 0);
39673f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
396846657f8dSmmusante 	    (uint64_t **)&vs, &c) == 0);
3969990b4856Slling 	health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
3970fa9e4066Sahrens 
3971fa9e4066Sahrens 	(void) printf(gettext("  pool: %s\n"), zpool_get_name(zhp));
3972fa9e4066Sahrens 	(void) printf(gettext(" state: %s\n"), health);
3973fa9e4066Sahrens 
3974fa9e4066Sahrens 	switch (reason) {
3975fa9e4066Sahrens 	case ZPOOL_STATUS_MISSING_DEV_R:
3976fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices could not "
3977fa9e4066Sahrens 		    "be opened.  Sufficient replicas exist for\n\tthe pool to "
3978fa9e4066Sahrens 		    "continue functioning in a degraded state.\n"));
3979fa9e4066Sahrens 		(void) printf(gettext("action: Attach the missing device and "
3980fa9e4066Sahrens 		    "online it using 'zpool online'.\n"));
3981fa9e4066Sahrens 		break;
3982fa9e4066Sahrens 
3983fa9e4066Sahrens 	case ZPOOL_STATUS_MISSING_DEV_NR:
3984fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices could not "
3985fa9e4066Sahrens 		    "be opened.  There are insufficient\n\treplicas for the "
3986fa9e4066Sahrens 		    "pool to continue functioning.\n"));
3987fa9e4066Sahrens 		(void) printf(gettext("action: Attach the missing device and "
3988fa9e4066Sahrens 		    "online it using 'zpool online'.\n"));
3989fa9e4066Sahrens 		break;
3990fa9e4066Sahrens 
3991fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_LABEL_R:
3992fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices could not "
3993fa9e4066Sahrens 		    "be used because the label is missing or\n\tinvalid.  "
3994fa9e4066Sahrens 		    "Sufficient replicas exist for the pool to continue\n\t"
3995fa9e4066Sahrens 		    "functioning in a degraded state.\n"));
3996fa9e4066Sahrens 		(void) printf(gettext("action: Replace the device using "
3997fa9e4066Sahrens 		    "'zpool replace'.\n"));
3998fa9e4066Sahrens 		break;
3999fa9e4066Sahrens 
4000fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4001fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices could not "
4002b1b8ab34Slling 		    "be used because the label is missing \n\tor invalid.  "
4003fa9e4066Sahrens 		    "There are insufficient replicas for the pool to "
4004fa9e4066Sahrens 		    "continue\n\tfunctioning.\n"));
4005468c413aSTim Haley 		zpool_explain_recover(zpool_get_handle(zhp),
4006468c413aSTim Haley 		    zpool_get_name(zhp), reason, config);
4007fa9e4066Sahrens 		break;
4008fa9e4066Sahrens 
4009fa9e4066Sahrens 	case ZPOOL_STATUS_FAILING_DEV:
4010fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices has "
4011fa9e4066Sahrens 		    "experienced an unrecoverable error.  An\n\tattempt was "
4012fa9e4066Sahrens 		    "made to correct the error.  Applications are "
4013fa9e4066Sahrens 		    "unaffected.\n"));
4014fa9e4066Sahrens 		(void) printf(gettext("action: Determine if the device needs "
4015fa9e4066Sahrens 		    "to be replaced, and clear the errors\n\tusing "
4016ea8dc4b6Seschrock 		    "'zpool clear' or replace the device with 'zpool "
4017fa9e4066Sahrens 		    "replace'.\n"));
4018fa9e4066Sahrens 		break;
4019fa9e4066Sahrens 
4020fa9e4066Sahrens 	case ZPOOL_STATUS_OFFLINE_DEV:
4021fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices has "
4022d7d4af51Smmusante 		    "been taken offline by the administrator.\n\tSufficient "
4023fa9e4066Sahrens 		    "replicas exist for the pool to continue functioning in "
4024fa9e4066Sahrens 		    "a\n\tdegraded state.\n"));
4025fa9e4066Sahrens 		(void) printf(gettext("action: Online the device using "
4026fa9e4066Sahrens 		    "'zpool online' or replace the device with\n\t'zpool "
4027fa9e4066Sahrens 		    "replace'.\n"));
4028fa9e4066Sahrens 		break;
4029fa9e4066Sahrens 
4030c25309d4SGeorge Wilson 	case ZPOOL_STATUS_REMOVED_DEV:
4031c25309d4SGeorge Wilson 		(void) printf(gettext("status: One or more devices has "
4032c25309d4SGeorge Wilson 		    "been removed by the administrator.\n\tSufficient "
4033c25309d4SGeorge Wilson 		    "replicas exist for the pool to continue functioning in "
4034c25309d4SGeorge Wilson 		    "a\n\tdegraded state.\n"));
4035c25309d4SGeorge Wilson 		(void) printf(gettext("action: Online the device using "
4036c25309d4SGeorge Wilson 		    "'zpool online' or replace the device with\n\t'zpool "
4037c25309d4SGeorge Wilson 		    "replace'.\n"));
4038c25309d4SGeorge Wilson 		break;
4039c25309d4SGeorge Wilson 
4040fa9e4066Sahrens 	case ZPOOL_STATUS_RESILVERING:
4041fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices is "
4042fa9e4066Sahrens 		    "currently being resilvered.  The pool will\n\tcontinue "
4043fa9e4066Sahrens 		    "to function, possibly in a degraded state.\n"));
4044fa9e4066Sahrens 		(void) printf(gettext("action: Wait for the resilver to "
4045fa9e4066Sahrens 		    "complete.\n"));
4046fa9e4066Sahrens 		break;
4047fa9e4066Sahrens 
4048ea8dc4b6Seschrock 	case ZPOOL_STATUS_CORRUPT_DATA:
4049ea8dc4b6Seschrock 		(void) printf(gettext("status: One or more devices has "
4050ea8dc4b6Seschrock 		    "experienced an error resulting in data\n\tcorruption.  "
4051ea8dc4b6Seschrock 		    "Applications may be affected.\n"));
4052ea8dc4b6Seschrock 		(void) printf(gettext("action: Restore the file in question "
4053ea8dc4b6Seschrock 		    "if possible.  Otherwise restore the\n\tentire pool from "
4054ea8dc4b6Seschrock 		    "backup.\n"));
4055ea8dc4b6Seschrock 		break;
4056ea8dc4b6Seschrock 
4057ea8dc4b6Seschrock 	case ZPOOL_STATUS_CORRUPT_POOL:
4058ea8dc4b6Seschrock 		(void) printf(gettext("status: The pool metadata is corrupted "
4059ea8dc4b6Seschrock 		    "and the pool cannot be opened.\n"));
4060468c413aSTim Haley 		zpool_explain_recover(zpool_get_handle(zhp),
4061468c413aSTim Haley 		    zpool_get_name(zhp), reason, config);
4062ea8dc4b6Seschrock 		break;
4063ea8dc4b6Seschrock 
4064eaca9bbdSeschrock 	case ZPOOL_STATUS_VERSION_OLDER:
406557221772SChristopher Siden 		(void) printf(gettext("status: The pool is formatted using a "
406657221772SChristopher Siden 		    "legacy on-disk format.  The pool can\n\tstill be used, "
406757221772SChristopher Siden 		    "but some features are unavailable.\n"));
4068eaca9bbdSeschrock 		(void) printf(gettext("action: Upgrade the pool using 'zpool "
4069eaca9bbdSeschrock 		    "upgrade'.  Once this is done, the\n\tpool will no longer "
407057221772SChristopher Siden 		    "be accessible on software that does not support feature\n"
407157221772SChristopher Siden 		    "\tflags.\n"));
4072eaca9bbdSeschrock 		break;
4073eaca9bbdSeschrock 
4074eaca9bbdSeschrock 	case ZPOOL_STATUS_VERSION_NEWER:
4075eaca9bbdSeschrock 		(void) printf(gettext("status: The pool has been upgraded to a "
4076eaca9bbdSeschrock 		    "newer, incompatible on-disk version.\n\tThe pool cannot "
4077eaca9bbdSeschrock 		    "be accessed on this system.\n"));
4078eaca9bbdSeschrock 		(void) printf(gettext("action: Access the pool from a system "
4079eaca9bbdSeschrock 		    "running more recent software, or\n\trestore the pool from "
4080eaca9bbdSeschrock 		    "backup.\n"));
4081eaca9bbdSeschrock 		break;
4082eaca9bbdSeschrock 
408357221772SChristopher Siden 	case ZPOOL_STATUS_FEAT_DISABLED:
408457221772SChristopher Siden 		(void) printf(gettext("status: Some supported features are not "
408557221772SChristopher Siden 		    "enabled on the pool. The pool can\n\tstill be used, but "
408657221772SChristopher Siden 		    "some features are unavailable.\n"));
408757221772SChristopher Siden 		(void) printf(gettext("action: Enable all features using "
408857221772SChristopher Siden 		    "'zpool upgrade'. Once this is done,\n\tthe pool may no "
408957221772SChristopher Siden 		    "longer be accessible by software that does not support\n\t"
409057221772SChristopher Siden 		    "the features. See zpool-features(5) for details.\n"));
409157221772SChristopher Siden 		break;
409257221772SChristopher Siden 
4093ad135b5dSChristopher Siden 	case ZPOOL_STATUS_UNSUP_FEAT_READ:
4094ad135b5dSChristopher Siden 		(void) printf(gettext("status: The pool cannot be accessed on "
4095ad135b5dSChristopher Siden 		    "this system because it uses the\n\tfollowing feature(s) "
4096ad135b5dSChristopher Siden 		    "not supported on this system:\n"));
4097ad135b5dSChristopher Siden 		zpool_print_unsup_feat(config);
4098ad135b5dSChristopher Siden 		(void) printf("\n");
4099ad135b5dSChristopher Siden 		(void) printf(gettext("action: Access the pool from a system "
4100ad135b5dSChristopher Siden 		    "that supports the required feature(s),\n\tor restore the "
4101ad135b5dSChristopher Siden 		    "pool from backup.\n"));
4102ad135b5dSChristopher Siden 		break;
4103ad135b5dSChristopher Siden 
4104ad135b5dSChristopher Siden 	case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4105ad135b5dSChristopher Siden 		(void) printf(gettext("status: The pool can only be accessed "
4106ad135b5dSChristopher Siden 		    "in read-only mode on this system. It\n\tcannot be "
4107ad135b5dSChristopher Siden 		    "accessed in read-write mode because it uses the "
4108ad135b5dSChristopher Siden 		    "following\n\tfeature(s) not supported on this system:\n"));
4109ad135b5dSChristopher Siden 		zpool_print_unsup_feat(config);
4110ad135b5dSChristopher Siden 		(void) printf("\n");
4111ad135b5dSChristopher Siden 		(void) printf(gettext("action: The pool cannot be accessed in "
4112ad135b5dSChristopher Siden 		    "read-write mode. Import the pool with\n"
4113ad135b5dSChristopher Siden 		    "\t\"-o readonly=on\", access the pool from a system that "
4114ad135b5dSChristopher Siden 		    "supports the\n\trequired feature(s), or restore the "
4115ad135b5dSChristopher Siden 		    "pool from backup.\n"));
4116ad135b5dSChristopher Siden 		break;
4117ad135b5dSChristopher Siden 
41183d7072f8Seschrock 	case ZPOOL_STATUS_FAULTED_DEV_R:
41193d7072f8Seschrock 		(void) printf(gettext("status: One or more devices are "
41203d7072f8Seschrock 		    "faulted in response to persistent errors.\n\tSufficient "
41213d7072f8Seschrock 		    "replicas exist for the pool to continue functioning "
41223d7072f8Seschrock 		    "in a\n\tdegraded state.\n"));
41233d7072f8Seschrock 		(void) printf(gettext("action: Replace the faulted device, "
41243d7072f8Seschrock 		    "or use 'zpool clear' to mark the device\n\trepaired.\n"));
41253d7072f8Seschrock 		break;
41263d7072f8Seschrock 
41273d7072f8Seschrock 	case ZPOOL_STATUS_FAULTED_DEV_NR:
41283d7072f8Seschrock 		(void) printf(gettext("status: One or more devices are "
41293d7072f8Seschrock 		    "faulted in response to persistent errors.  There are "
41303d7072f8Seschrock 		    "insufficient replicas for the pool to\n\tcontinue "
41313d7072f8Seschrock 		    "functioning.\n"));
41323d7072f8Seschrock 		(void) printf(gettext("action: Destroy and re-create the pool "
41333d7072f8Seschrock 		    "from a backup source.  Manually marking the device\n"
41343d7072f8Seschrock 		    "\trepaired using 'zpool clear' may allow some data "
41353d7072f8Seschrock 		    "to be recovered.\n"));
41363d7072f8Seschrock 		break;
41373d7072f8Seschrock 
413832b87932Sek110237 	case ZPOOL_STATUS_IO_FAILURE_WAIT:
413932b87932Sek110237 	case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
414032b87932Sek110237 		(void) printf(gettext("status: One or more devices are "
41418a79c1b5Sek110237 		    "faulted in response to IO failures.\n"));
414232b87932Sek110237 		(void) printf(gettext("action: Make sure the affected devices "
414332b87932Sek110237 		    "are connected, then run 'zpool clear'.\n"));
414432b87932Sek110237 		break;
414532b87932Sek110237 
4146b87f3af3Sperrin 	case ZPOOL_STATUS_BAD_LOG:
4147b87f3af3Sperrin 		(void) printf(gettext("status: An intent log record "
4148b87f3af3Sperrin 		    "could not be read.\n"
4149b87f3af3Sperrin 		    "\tWaiting for adminstrator intervention to fix the "
4150b87f3af3Sperrin 		    "faulted pool.\n"));
4151b87f3af3Sperrin 		(void) printf(gettext("action: Either restore the affected "
4152b87f3af3Sperrin 		    "device(s) and run 'zpool online',\n"
4153b87f3af3Sperrin 		    "\tor ignore the intent log records by running "
4154b87f3af3Sperrin 		    "'zpool clear'.\n"));
4155b87f3af3Sperrin 		break;
4156b87f3af3Sperrin 
4157fa9e4066Sahrens 	default:
4158fa9e4066Sahrens 		/*
4159fa9e4066Sahrens 		 * The remaining errors can't actually be generated, yet.
4160fa9e4066Sahrens 		 */
4161fa9e4066Sahrens 		assert(reason == ZPOOL_STATUS_OK);
4162fa9e4066Sahrens 	}
4163fa9e4066Sahrens 
4164fa9e4066Sahrens 	if (msgid != NULL)
4165654b400cSJoshua M. Clulow 		(void) printf(gettext("   see: http://illumos.org/msg/%s\n"),
4166fa9e4066Sahrens 		    msgid);
4167fa9e4066Sahrens 
4168fa9e4066Sahrens 	if (config != NULL) {
4169fa9e4066Sahrens 		int namewidth;
4170ea8dc4b6Seschrock 		uint64_t nerr;
4171fa94a07fSbrendan 		nvlist_t **spares, **l2cache;
4172fa94a07fSbrendan 		uint_t nspares, nl2cache;
41733f9d6ad7SLin Ling 		pool_scan_stat_t *ps = NULL;
4174fa9e4066Sahrens 
41753f9d6ad7SLin Ling 		(void) nvlist_lookup_uint64_array(nvroot,
41763f9d6ad7SLin Ling 		    ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
41773f9d6ad7SLin Ling 		print_scan_status(ps);
4178fa9e4066Sahrens 
4179c67d9675Seschrock 		namewidth = max_width(zhp, nvroot, 0, 0);
4180fa9e4066Sahrens 		if (namewidth < 10)
4181fa9e4066Sahrens 			namewidth = 10;
4182fa9e4066Sahrens 
4183fa9e4066Sahrens 		(void) printf(gettext("config:\n\n"));
4184fa9e4066Sahrens 		(void) printf(gettext("\t%-*s  %-8s %5s %5s %5s\n"), namewidth,
4185fa9e4066Sahrens 		    "NAME", "STATE", "READ", "WRITE", "CKSUM");
4186c67d9675Seschrock 		print_status_config(zhp, zpool_get_name(zhp), nvroot,
4187aa8cf21aSNeil Perrin 		    namewidth, 0, B_FALSE);
418899653d4eSeschrock 
41894dea40f0SNeil Perrin 		if (num_logs(nvroot) > 0)
4190e6ca193dSGeorge Wilson 			print_logs(zhp, nvroot, namewidth, B_TRUE);
4191fa94a07fSbrendan 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4192fa94a07fSbrendan 		    &l2cache, &nl2cache) == 0)
4193fa94a07fSbrendan 			print_l2cache(zhp, l2cache, nl2cache, namewidth);
4194fa94a07fSbrendan 
419599653d4eSeschrock 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
419699653d4eSeschrock 		    &spares, &nspares) == 0)
419799653d4eSeschrock 			print_spares(zhp, spares, nspares, namewidth);
4198ea8dc4b6Seschrock 
4199ea8dc4b6Seschrock 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4200ea8dc4b6Seschrock 		    &nerr) == 0) {
420155434c77Sek110237 			nvlist_t *nverrlist = NULL;
420255434c77Sek110237 
4203ea8dc4b6Seschrock 			/*
4204ea8dc4b6Seschrock 			 * If the approximate error count is small, get a
4205ea8dc4b6Seschrock 			 * precise count by fetching the entire log and
4206ea8dc4b6Seschrock 			 * uniquifying the results.
4207ea8dc4b6Seschrock 			 */
420875519f38Sek110237 			if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
420955434c77Sek110237 			    zpool_get_errlog(zhp, &nverrlist) == 0) {
421055434c77Sek110237 				nvpair_t *elem;
421155434c77Sek110237 
421255434c77Sek110237 				elem = NULL;
421355434c77Sek110237 				nerr = 0;
421455434c77Sek110237 				while ((elem = nvlist_next_nvpair(nverrlist,
421555434c77Sek110237 				    elem)) != NULL) {
421655434c77Sek110237 					nerr++;
421755434c77Sek110237 				}
421855434c77Sek110237 			}
421955434c77Sek110237 			nvlist_free(nverrlist);
4220ea8dc4b6Seschrock 
4221ea8dc4b6Seschrock 			(void) printf("\n");
422299653d4eSeschrock 
4223ea8dc4b6Seschrock 			if (nerr == 0)
4224ea8dc4b6Seschrock 				(void) printf(gettext("errors: No known data "
4225ea8dc4b6Seschrock 				    "errors\n"));
4226ea8dc4b6Seschrock 			else if (!cbp->cb_verbose)
4227e9dbad6fSeschrock 				(void) printf(gettext("errors: %llu data "
42285ad82045Snd150628 				    "errors, use '-v' for a list\n"),
42295ad82045Snd150628 				    (u_longlong_t)nerr);
4230ea8dc4b6Seschrock 			else
4231ea8dc4b6Seschrock 				print_error_log(zhp);
4232ea8dc4b6Seschrock 		}
42339eb19f4dSGeorge Wilson 
42349eb19f4dSGeorge Wilson 		if (cbp->cb_dedup_stats)
42359eb19f4dSGeorge Wilson 			print_dedup_stats(config);
4236fa9e4066Sahrens 	} else {
4237fa9e4066Sahrens 		(void) printf(gettext("config: The configuration cannot be "
4238fa9e4066Sahrens 		    "determined.\n"));
4239fa9e4066Sahrens 	}
4240fa9e4066Sahrens 
4241fa9e4066Sahrens 	return (0);
4242fa9e4066Sahrens }
4243fa9e4066Sahrens 
4244fa9e4066Sahrens /*
42453f9d6ad7SLin Ling  * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4246fa9e4066Sahrens  *
4247fa9e4066Sahrens  *	-v	Display complete error logs
4248fa9e4066Sahrens  *	-x	Display only pools with potential problems
42499eb19f4dSGeorge Wilson  *	-D	Display dedup status (undocumented)
42503f9d6ad7SLin Ling  *	-T	Display a timestamp in date(1) or Unix format
4251fa9e4066Sahrens  *
4252fa9e4066Sahrens  * Describes the health status of all pools or some subset.
4253fa9e4066Sahrens  */
4254fa9e4066Sahrens int
4255fa9e4066Sahrens zpool_do_status(int argc, char **argv)
4256fa9e4066Sahrens {
4257fa9e4066Sahrens 	int c;
4258fa9e4066Sahrens 	int ret;
42593f9d6ad7SLin Ling 	unsigned long interval = 0, count = 0;
4260fa9e4066Sahrens 	status_cbdata_t cb = { 0 };
4261fa9e4066Sahrens 
4262fa9e4066Sahrens 	/* check options */
42633f9d6ad7SLin Ling 	while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4264fa9e4066Sahrens 		switch (c) {
4265fa9e4066Sahrens 		case 'v':
426699653d4eSeschrock 			cb.cb_verbose = B_TRUE;
4267fa9e4066Sahrens 			break;
4268fa9e4066Sahrens 		case 'x':
426999653d4eSeschrock 			cb.cb_explain = B_TRUE;
4270fa9e4066Sahrens 			break;
42719eb19f4dSGeorge Wilson 		case 'D':
42729eb19f4dSGeorge Wilson 			cb.cb_dedup_stats = B_TRUE;
42739eb19f4dSGeorge Wilson 			break;
42743f9d6ad7SLin Ling 		case 'T':
42753f9d6ad7SLin Ling 			get_timestamp_arg(*optarg);
42763f9d6ad7SLin Ling 			break;
4277fa9e4066Sahrens 		case '?':
4278fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4279fa9e4066Sahrens 			    optopt);
428099653d4eSeschrock 			usage(B_FALSE);
4281fa9e4066Sahrens 		}
4282fa9e4066Sahrens 	}
4283fa9e4066Sahrens 
4284fa9e4066Sahrens 	argc -= optind;
4285fa9e4066Sahrens 	argv += optind;
4286fa9e4066Sahrens 
42873f9d6ad7SLin Ling 	get_interval_count(&argc, argv, &interval, &count);
4288fa9e4066Sahrens 
4289e9dbad6fSeschrock 	if (argc == 0)
4290e9dbad6fSeschrock 		cb.cb_allpools = B_TRUE;
4291e9dbad6fSeschrock 
42923f9d6ad7SLin Ling 	cb.cb_first = B_TRUE;
42933f9d6ad7SLin Ling 
42943f9d6ad7SLin Ling 	for (;;) {
42953f9d6ad7SLin Ling 		if (timestamp_fmt != NODATE)
42963f9d6ad7SLin Ling 			print_timestamp(timestamp_fmt);
42973f9d6ad7SLin Ling 
42983f9d6ad7SLin Ling 		ret = for_each_pool(argc, argv, B_TRUE, NULL,
42993f9d6ad7SLin Ling 		    status_callback, &cb);
4300fa9e4066Sahrens 
4301fa9e4066Sahrens 		if (argc == 0 && cb.cb_count == 0)
4302fa9e4066Sahrens 			(void) printf(gettext("no pools available\n"));
4303e9dbad6fSeschrock 		else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4304fa9e4066Sahrens 			(void) printf(gettext("all pools are healthy\n"));
4305fa9e4066Sahrens 
43063f9d6ad7SLin Ling 		if (ret != 0)
4307fa9e4066Sahrens 			return (ret);
43083f9d6ad7SLin Ling 
43093f9d6ad7SLin Ling 		if (interval == 0)
43103f9d6ad7SLin Ling 			break;
43113f9d6ad7SLin Ling 
43123f9d6ad7SLin Ling 		if (count != 0 && --count == 0)
43133f9d6ad7SLin Ling 			break;
43143f9d6ad7SLin Ling 
43153f9d6ad7SLin Ling 		(void) sleep(interval);
43163f9d6ad7SLin Ling 	}
43173f9d6ad7SLin Ling 
43183f9d6ad7SLin Ling 	return (0);
4319fa9e4066Sahrens }
4320fa9e4066Sahrens 
4321eaca9bbdSeschrock typedef struct upgrade_cbdata {
4322eaca9bbdSeschrock 	int	cb_first;
432306eeb2adSek110237 	int	cb_argc;
4324990b4856Slling 	uint64_t cb_version;
432506eeb2adSek110237 	char	**cb_argv;
4326eaca9bbdSeschrock } upgrade_cbdata_t;
4327eaca9bbdSeschrock 
4328eaca9bbdSeschrock static int
432957221772SChristopher Siden upgrade_version(zpool_handle_t *zhp, uint64_t version)
433057221772SChristopher Siden {
433157221772SChristopher Siden 	int ret;
433257221772SChristopher Siden 	nvlist_t *config;
433357221772SChristopher Siden 	uint64_t oldversion;
433457221772SChristopher Siden 
433557221772SChristopher Siden 	config = zpool_get_config(zhp, NULL);
433657221772SChristopher Siden 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
433757221772SChristopher Siden 	    &oldversion) == 0);
433857221772SChristopher Siden 
433957221772SChristopher Siden 	assert(SPA_VERSION_IS_SUPPORTED(oldversion));
434057221772SChristopher Siden 	assert(oldversion < version);
434157221772SChristopher Siden 
434257221772SChristopher Siden 	ret = zpool_upgrade(zhp, version);
434357221772SChristopher Siden 	if (ret != 0)
434457221772SChristopher Siden 		return (ret);
434557221772SChristopher Siden 
434657221772SChristopher Siden 	if (version >= SPA_VERSION_FEATURES) {
434757221772SChristopher Siden 		(void) printf(gettext("Successfully upgraded "
434857221772SChristopher Siden 		    "'%s' from version %llu to feature flags.\n"),
434957221772SChristopher Siden 		    zpool_get_name(zhp), oldversion);
435057221772SChristopher Siden 	} else {
435157221772SChristopher Siden 		(void) printf(gettext("Successfully upgraded "
435257221772SChristopher Siden 		    "'%s' from version %llu to version %llu.\n"),
435357221772SChristopher Siden 		    zpool_get_name(zhp), oldversion, version);
435457221772SChristopher Siden 	}
435557221772SChristopher Siden 
435657221772SChristopher Siden 	return (0);
435757221772SChristopher Siden }
435857221772SChristopher Siden 
435957221772SChristopher Siden static int
436057221772SChristopher Siden upgrade_enable_all(zpool_handle_t *zhp, int *countp)
436157221772SChristopher Siden {
436257221772SChristopher Siden 	int i, ret, count;
436357221772SChristopher Siden 	boolean_t firstff = B_TRUE;
436457221772SChristopher Siden 	nvlist_t *enabled = zpool_get_features(zhp);
436557221772SChristopher Siden 
436657221772SChristopher Siden 	count = 0;
436757221772SChristopher Siden 	for (i = 0; i < SPA_FEATURES; i++) {
436857221772SChristopher Siden 		const char *fname = spa_feature_table[i].fi_uname;
436957221772SChristopher Siden 		const char *fguid = spa_feature_table[i].fi_guid;
437057221772SChristopher Siden 		if (!nvlist_exists(enabled, fguid)) {
437157221772SChristopher Siden 			char *propname;
437257221772SChristopher Siden 			verify(-1 != asprintf(&propname, "feature@%s", fname));
437357221772SChristopher Siden 			ret = zpool_set_prop(zhp, propname,
437457221772SChristopher Siden 			    ZFS_FEATURE_ENABLED);
437557221772SChristopher Siden 			if (ret != 0) {
437657221772SChristopher Siden 				free(propname);
437757221772SChristopher Siden 				return (ret);
437857221772SChristopher Siden 			}
437957221772SChristopher Siden 			count++;
438057221772SChristopher Siden 
438157221772SChristopher Siden 			if (firstff) {
438257221772SChristopher Siden 				(void) printf(gettext("Enabled the "
438357221772SChristopher Siden 				    "following features on '%s':\n"),
438457221772SChristopher Siden 				    zpool_get_name(zhp));
438557221772SChristopher Siden 				firstff = B_FALSE;
438657221772SChristopher Siden 			}
438757221772SChristopher Siden 			(void) printf(gettext("  %s\n"), fname);
438857221772SChristopher Siden 			free(propname);
438957221772SChristopher Siden 		}
439057221772SChristopher Siden 	}
439157221772SChristopher Siden 
439257221772SChristopher Siden 	if (countp != NULL)
439357221772SChristopher Siden 		*countp = count;
439457221772SChristopher Siden 	return (0);
439557221772SChristopher Siden }
439657221772SChristopher Siden 
439757221772SChristopher Siden static int
4398eaca9bbdSeschrock upgrade_cb(zpool_handle_t *zhp, void *arg)
4399eaca9bbdSeschrock {
4400eaca9bbdSeschrock 	upgrade_cbdata_t *cbp = arg;
4401eaca9bbdSeschrock 	nvlist_t *config;
4402eaca9bbdSeschrock 	uint64_t version;
440357221772SChristopher Siden 	boolean_t printnl = B_FALSE;
440457221772SChristopher Siden 	int ret;
4405eaca9bbdSeschrock 
4406eaca9bbdSeschrock 	config = zpool_get_config(zhp, NULL);
4407eaca9bbdSeschrock 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4408eaca9bbdSeschrock 	    &version) == 0);
4409eaca9bbdSeschrock 
441057221772SChristopher Siden 	assert(SPA_VERSION_IS_SUPPORTED(version));
4411eaca9bbdSeschrock 
441257221772SChristopher Siden 	if (version < cbp->cb_version) {
441399653d4eSeschrock 		cbp->cb_first = B_FALSE;
441457221772SChristopher Siden 		ret = upgrade_version(zhp, cbp->cb_version);
441557221772SChristopher Siden 		if (ret != 0)
441657221772SChristopher Siden 			return (ret);
441757221772SChristopher Siden 		printnl = B_TRUE;
441857221772SChristopher Siden 
44194445fffbSMatthew Ahrens 		/*
44204445fffbSMatthew Ahrens 		 * If they did "zpool upgrade -a", then we could
44214445fffbSMatthew Ahrens 		 * be doing ioctls to different pools.  We need
44224445fffbSMatthew Ahrens 		 * to log this history once to each pool, and bypass
44234445fffbSMatthew Ahrens 		 * the normal history logging that happens in main().
44244445fffbSMatthew Ahrens 		 */
44254445fffbSMatthew Ahrens 		(void) zpool_log_history(g_zfs, history_str);
44264445fffbSMatthew Ahrens 		log_history = B_FALSE;
442706eeb2adSek110237 	}
4428eaca9bbdSeschrock 
442957221772SChristopher Siden 	if (cbp->cb_version >= SPA_VERSION_FEATURES) {
443057221772SChristopher Siden 		int count;
443157221772SChristopher Siden 		ret = upgrade_enable_all(zhp, &count);
443257221772SChristopher Siden 		if (ret != 0)
443357221772SChristopher Siden 			return (ret);
443457221772SChristopher Siden 
443557221772SChristopher Siden 		if (count > 0) {
443657221772SChristopher Siden 			cbp->cb_first = B_FALSE;
443757221772SChristopher Siden 			printnl = B_TRUE;
443857221772SChristopher Siden 		}
443957221772SChristopher Siden 	}
444057221772SChristopher Siden 
444157221772SChristopher Siden 	if (printnl) {
444257221772SChristopher Siden 		(void) printf(gettext("\n"));
444357221772SChristopher Siden 	}
444457221772SChristopher Siden 
444557221772SChristopher Siden 	return (0);
444657221772SChristopher Siden }
444757221772SChristopher Siden 
444857221772SChristopher Siden static int
444957221772SChristopher Siden upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
445057221772SChristopher Siden {
445157221772SChristopher Siden 	upgrade_cbdata_t *cbp = arg;
445257221772SChristopher Siden 	nvlist_t *config;
445357221772SChristopher Siden 	uint64_t version;
445457221772SChristopher Siden 
445557221772SChristopher Siden 	config = zpool_get_config(zhp, NULL);
445657221772SChristopher Siden 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
445757221772SChristopher Siden 	    &version) == 0);
445857221772SChristopher Siden 
445957221772SChristopher Siden 	assert(SPA_VERSION_IS_SUPPORTED(version));
446057221772SChristopher Siden 
446157221772SChristopher Siden 	if (version < SPA_VERSION_FEATURES) {
4462eaca9bbdSeschrock 		if (cbp->cb_first) {
4463eaca9bbdSeschrock 			(void) printf(gettext("The following pools are "
446457221772SChristopher Siden 			    "formatted with legacy version numbers and can\n"
446557221772SChristopher Siden 			    "be upgraded to use feature flags.  After "
446657221772SChristopher Siden 			    "being upgraded, these pools\nwill no "
446757221772SChristopher Siden 			    "longer be accessible by software that does not "
446857221772SChristopher Siden 			    "support feature\nflags.\n\n"));
4469eaca9bbdSeschrock 			(void) printf(gettext("VER  POOL\n"));
4470eaca9bbdSeschrock 			(void) printf(gettext("---  ------------\n"));
447199653d4eSeschrock 			cbp->cb_first = B_FALSE;
4472eaca9bbdSeschrock 		}
4473eaca9bbdSeschrock 
44745ad82045Snd150628 		(void) printf("%2llu   %s\n", (u_longlong_t)version,
4475eaca9bbdSeschrock 		    zpool_get_name(zhp));
4476eaca9bbdSeschrock 	}
4477eaca9bbdSeschrock 
447857221772SChristopher Siden 	return (0);
447957221772SChristopher Siden }
448057221772SChristopher Siden 
448157221772SChristopher Siden static int
448257221772SChristopher Siden upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
448357221772SChristopher Siden {
448457221772SChristopher Siden 	upgrade_cbdata_t *cbp = arg;
448557221772SChristopher Siden 	nvlist_t *config;
448657221772SChristopher Siden 	uint64_t version;
448757221772SChristopher Siden 
448857221772SChristopher Siden 	config = zpool_get_config(zhp, NULL);
448957221772SChristopher Siden 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
449057221772SChristopher Siden 	    &version) == 0);
449157221772SChristopher Siden 
449257221772SChristopher Siden 	if (version >= SPA_VERSION_FEATURES) {
449357221772SChristopher Siden 		int i;
449457221772SChristopher Siden 		boolean_t poolfirst = B_TRUE;
449557221772SChristopher Siden 		nvlist_t *enabled = zpool_get_features(zhp);
449657221772SChristopher Siden 
449757221772SChristopher Siden 		for (i = 0; i < SPA_FEATURES; i++) {
449857221772SChristopher Siden 			const char *fguid = spa_feature_table[i].fi_guid;
449957221772SChristopher Siden 			const char *fname = spa_feature_table[i].fi_uname;
450057221772SChristopher Siden 			if (!nvlist_exists(enabled, fguid)) {
450157221772SChristopher Siden 				if (cbp->cb_first) {
450257221772SChristopher Siden 					(void) printf(gettext("\nSome "
450357221772SChristopher Siden 					    "supported features are not "
450457221772SChristopher Siden 					    "enabled on the following pools. "
450557221772SChristopher Siden 					    "Once a\nfeature is enabled the "
450657221772SChristopher Siden 					    "pool may become incompatible with "
450757221772SChristopher Siden 					    "software\nthat does not support "
450857221772SChristopher Siden 					    "the feature. See "
450957221772SChristopher Siden 					    "zpool-features(5) for "
451057221772SChristopher Siden 					    "details.\n\n"));
451157221772SChristopher Siden 					(void) printf(gettext("POOL  "
451257221772SChristopher Siden 					    "FEATURE\n"));
451357221772SChristopher Siden 					(void) printf(gettext("------"
451457221772SChristopher Siden 					    "---------\n"));
451557221772SChristopher Siden 					cbp->cb_first = B_FALSE;
451657221772SChristopher Siden 				}
451757221772SChristopher Siden 
451857221772SChristopher Siden 				if (poolfirst) {
451957221772SChristopher Siden 					(void) printf(gettext("%s\n"),
452057221772SChristopher Siden 					    zpool_get_name(zhp));
452157221772SChristopher Siden 					poolfirst = B_FALSE;
452257221772SChristopher Siden 				}
452357221772SChristopher Siden 
452457221772SChristopher Siden 				(void) printf(gettext("      %s\n"), fname);
452557221772SChristopher Siden 			}
452657221772SChristopher Siden 		}
452757221772SChristopher Siden 	}
452857221772SChristopher Siden 
452957221772SChristopher Siden 	return (0);
4530eaca9bbdSeschrock }
4531eaca9bbdSeschrock 
4532eaca9bbdSeschrock /* ARGSUSED */
4533eaca9bbdSeschrock static int
453406eeb2adSek110237 upgrade_one(zpool_handle_t *zhp, void *data)
4535eaca9bbdSeschrock {
453657221772SChristopher Siden 	boolean_t printnl = B_FALSE;
4537990b4856Slling 	upgrade_cbdata_t *cbp = data;
4538990b4856Slling 	uint64_t cur_version;
4539eaca9bbdSeschrock 	int ret;
4540eaca9bbdSeschrock 
45418654d025Sperrin 	if (strcmp("log", zpool_get_name(zhp)) == 0) {
45428654d025Sperrin 		(void) printf(gettext("'log' is now a reserved word\n"
45438654d025Sperrin 		    "Pool 'log' must be renamed using export and import"
45448654d025Sperrin 		    " to upgrade.\n"));
45458654d025Sperrin 		return (1);
45468654d025Sperrin 	}
4547990b4856Slling 
4548990b4856Slling 	cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4549e6c728e1Sbrendan 	if (cur_version > cbp->cb_version) {
4550eaca9bbdSeschrock 		(void) printf(gettext("Pool '%s' is already formatted "
455157221772SChristopher Siden 		    "using more current version '%llu'.\n\n"),
4552e6c728e1Sbrendan 		    zpool_get_name(zhp), cur_version);
4553e6c728e1Sbrendan 		return (0);
4554e6c728e1Sbrendan 	}
455557221772SChristopher Siden 
455657221772SChristopher Siden 	if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
4557e6c728e1Sbrendan 		(void) printf(gettext("Pool '%s' is already formatted "
455857221772SChristopher Siden 		    "using version %llu.\n\n"), zpool_get_name(zhp),
455957221772SChristopher Siden 		    cbp->cb_version);
4560eaca9bbdSeschrock 		return (0);
4561eaca9bbdSeschrock 	}
4562eaca9bbdSeschrock 
456357221772SChristopher Siden 	if (cur_version != cbp->cb_version) {
456457221772SChristopher Siden 		printnl = B_TRUE;
456557221772SChristopher Siden 		ret = upgrade_version(zhp, cbp->cb_version);
456657221772SChristopher Siden 		if (ret != 0)
456757221772SChristopher Siden 			return (ret);
456806eeb2adSek110237 	}
4569eaca9bbdSeschrock 
457057221772SChristopher Siden 	if (cbp->cb_version >= SPA_VERSION_FEATURES) {
457157221772SChristopher Siden 		int count = 0;
457257221772SChristopher Siden 		ret = upgrade_enable_all(zhp, &count);
457357221772SChristopher Siden 		if (ret != 0)
457457221772SChristopher Siden 			return (ret);
457557221772SChristopher Siden 
457657221772SChristopher Siden 		if (count != 0) {
457757221772SChristopher Siden 			printnl = B_TRUE;
457857221772SChristopher Siden 		} else if (cur_version == SPA_VERSION) {
457957221772SChristopher Siden 			(void) printf(gettext("Pool '%s' already has all "
458057221772SChristopher Siden 			    "supported features enabled.\n"),
458157221772SChristopher Siden 			    zpool_get_name(zhp));
458257221772SChristopher Siden 		}
458357221772SChristopher Siden 	}
458457221772SChristopher Siden 
458557221772SChristopher Siden 	if (printnl) {
458657221772SChristopher Siden 		(void) printf(gettext("\n"));
458757221772SChristopher Siden 	}
458857221772SChristopher Siden 
458957221772SChristopher Siden 	return (0);
4590eaca9bbdSeschrock }
4591eaca9bbdSeschrock 
4592eaca9bbdSeschrock /*
4593eaca9bbdSeschrock  * zpool upgrade
4594eaca9bbdSeschrock  * zpool upgrade -v
4595990b4856Slling  * zpool upgrade [-V version] <-a | pool ...>
4596eaca9bbdSeschrock  *
4597eaca9bbdSeschrock  * With no arguments, display downrev'd ZFS pool available for upgrade.
4598eaca9bbdSeschrock  * Individual pools can be upgraded by specifying the pool, and '-a' will
4599eaca9bbdSeschrock  * upgrade all pools.
4600eaca9bbdSeschrock  */
4601eaca9bbdSeschrock int
4602eaca9bbdSeschrock zpool_do_upgrade(int argc, char **argv)
4603eaca9bbdSeschrock {
4604eaca9bbdSeschrock 	int c;
4605eaca9bbdSeschrock 	upgrade_cbdata_t cb = { 0 };
4606eaca9bbdSeschrock 	int ret = 0;
4607eaca9bbdSeschrock 	boolean_t showversions = B_FALSE;
460857221772SChristopher Siden 	boolean_t upgradeall = B_FALSE;
4609990b4856Slling 	char *end;
4610990b4856Slling 
4611eaca9bbdSeschrock 
4612eaca9bbdSeschrock 	/* check options */
4613478ed9adSEric Taylor 	while ((c = getopt(argc, argv, ":avV:")) != -1) {
4614eaca9bbdSeschrock 		switch (c) {
4615eaca9bbdSeschrock 		case 'a':
461657221772SChristopher Siden 			upgradeall = B_TRUE;
4617eaca9bbdSeschrock 			break;
4618eaca9bbdSeschrock 		case 'v':
4619eaca9bbdSeschrock 			showversions = B_TRUE;
4620eaca9bbdSeschrock 			break;
4621990b4856Slling 		case 'V':
4622990b4856Slling 			cb.cb_version = strtoll(optarg, &end, 10);
4623ad135b5dSChristopher Siden 			if (*end != '\0' ||
4624ad135b5dSChristopher Siden 			    !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
4625990b4856Slling 				(void) fprintf(stderr,
4626990b4856Slling 				    gettext("invalid version '%s'\n"), optarg);
4627990b4856Slling 				usage(B_FALSE);
4628990b4856Slling 			}
4629990b4856Slling 			break;
4630478ed9adSEric Taylor 		case ':':
4631478ed9adSEric Taylor 			(void) fprintf(stderr, gettext("missing argument for "
4632478ed9adSEric Taylor 			    "'%c' option\n"), optopt);
4633478ed9adSEric Taylor 			usage(B_FALSE);
4634478ed9adSEric Taylor 			break;
4635eaca9bbdSeschrock 		case '?':
4636eaca9bbdSeschrock 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4637eaca9bbdSeschrock 			    optopt);
463899653d4eSeschrock 			usage(B_FALSE);
4639eaca9bbdSeschrock 		}
4640eaca9bbdSeschrock 	}
4641eaca9bbdSeschrock 
464206eeb2adSek110237 	cb.cb_argc = argc;
464306eeb2adSek110237 	cb.cb_argv = argv;
4644eaca9bbdSeschrock 	argc -= optind;
4645eaca9bbdSeschrock 	argv += optind;
4646eaca9bbdSeschrock 
4647351420b3Slling 	if (cb.cb_version == 0) {
4648351420b3Slling 		cb.cb_version = SPA_VERSION;
464957221772SChristopher Siden 	} else if (!upgradeall && argc == 0) {
4650351420b3Slling 		(void) fprintf(stderr, gettext("-V option is "
4651351420b3Slling 		    "incompatible with other arguments\n"));
4652351420b3Slling 		usage(B_FALSE);
4653351420b3Slling 	}
4654351420b3Slling 
4655eaca9bbdSeschrock 	if (showversions) {
465657221772SChristopher Siden 		if (upgradeall || argc != 0) {
4657eaca9bbdSeschrock 			(void) fprintf(stderr, gettext("-v option is "
4658eaca9bbdSeschrock 			    "incompatible with other arguments\n"));
465999653d4eSeschrock 			usage(B_FALSE);
4660eaca9bbdSeschrock 		}
466157221772SChristopher Siden 	} else if (upgradeall) {
4662eaca9bbdSeschrock 		if (argc != 0) {
4663351420b3Slling 			(void) fprintf(stderr, gettext("-a option should not "
4664351420b3Slling 			    "be used along with a pool name\n"));
466599653d4eSeschrock 			usage(B_FALSE);
4666eaca9bbdSeschrock 		}
4667eaca9bbdSeschrock 	}
4668eaca9bbdSeschrock 
4669ad135b5dSChristopher Siden 	(void) printf(gettext("This system supports ZFS pool feature "
4670ad135b5dSChristopher Siden 	    "flags.\n\n"));
4671eaca9bbdSeschrock 	if (showversions) {
467257221772SChristopher Siden 		int i;
467357221772SChristopher Siden 
467457221772SChristopher Siden 		(void) printf(gettext("The following features are "
467557221772SChristopher Siden 		    "supported:\n\n"));
467657221772SChristopher Siden 		(void) printf(gettext("FEAT DESCRIPTION\n"));
467757221772SChristopher Siden 		(void) printf("----------------------------------------------"
467857221772SChristopher Siden 		    "---------------\n");
467957221772SChristopher Siden 		for (i = 0; i < SPA_FEATURES; i++) {
468057221772SChristopher Siden 			zfeature_info_t *fi = &spa_feature_table[i];
468157221772SChristopher Siden 			const char *ro = fi->fi_can_readonly ?
468257221772SChristopher Siden 			    " (read-only compatible)" : "";
468357221772SChristopher Siden 
468457221772SChristopher Siden 			(void) printf("%-37s%s\n", fi->fi_uname, ro);
468557221772SChristopher Siden 			(void) printf("     %s\n", fi->fi_desc);
468657221772SChristopher Siden 		}
468757221772SChristopher Siden 		(void) printf("\n");
468857221772SChristopher Siden 
468957221772SChristopher Siden 		(void) printf(gettext("The following legacy versions are also "
4690d7d4af51Smmusante 		    "supported:\n\n"));
4691eaca9bbdSeschrock 		(void) printf(gettext("VER  DESCRIPTION\n"));
4692eaca9bbdSeschrock 		(void) printf("---  -----------------------------------------"
4693eaca9bbdSeschrock 		    "---------------\n");
469499653d4eSeschrock 		(void) printf(gettext(" 1   Initial ZFS version\n"));
469544cd46caSbillm 		(void) printf(gettext(" 2   Ditto blocks "
469644cd46caSbillm 		    "(replicated metadata)\n"));
469799653d4eSeschrock 		(void) printf(gettext(" 3   Hot spares and double parity "
469899653d4eSeschrock 		    "RAID-Z\n"));
4699d7306b64Sek110237 		(void) printf(gettext(" 4   zpool history\n"));
4700c9431fa1Sahl 		(void) printf(gettext(" 5   Compression using the gzip "
4701c9431fa1Sahl 		    "algorithm\n"));
4702990b4856Slling 		(void) printf(gettext(" 6   bootfs pool property\n"));
47038654d025Sperrin 		(void) printf(gettext(" 7   Separate intent log devices\n"));
4704ecd6cf80Smarks 		(void) printf(gettext(" 8   Delegated administration\n"));
4705a9799022Sck153898 		(void) printf(gettext(" 9   refquota and refreservation "
4706a9799022Sck153898 		    "properties\n"));
4707fa94a07fSbrendan 		(void) printf(gettext(" 10  Cache devices\n"));
4708088f3894Sahrens 		(void) printf(gettext(" 11  Improved scrub performance\n"));
4709bb0ade09Sahrens 		(void) printf(gettext(" 12  Snapshot properties\n"));
471074e7dc98SMatthew Ahrens 		(void) printf(gettext(" 13  snapused property\n"));
471114843421SMatthew Ahrens 		(void) printf(gettext(" 14  passthrough-x aclinherit\n"));
471214843421SMatthew Ahrens 		(void) printf(gettext(" 15  user/group space accounting\n"));
4713478ed9adSEric Taylor 		(void) printf(gettext(" 16  stmf property support\n"));
47147aeab329SAdam Leventhal 		(void) printf(gettext(" 17  Triple-parity RAID-Z\n"));
4715b24ab676SJeff Bonwick 		(void) printf(gettext(" 18  Snapshot user holds\n"));
471688ecc943SGeorge Wilson 		(void) printf(gettext(" 19  Log device removal\n"));
4717b24ab676SJeff Bonwick 		(void) printf(gettext(" 20  Compression using zle "
4718b24ab676SJeff Bonwick 		    "(zero-length encoding)\n"));
4719b24ab676SJeff Bonwick 		(void) printf(gettext(" 21  Deduplication\n"));
472092241e0bSTom Erickson 		(void) printf(gettext(" 22  Received properties\n"));
47216e1f5caaSNeil Perrin 		(void) printf(gettext(" 23  Slim ZIL\n"));
47220a586ceaSMark Shellenbaum 		(void) printf(gettext(" 24  System attributes\n"));
47233f9d6ad7SLin Ling 		(void) printf(gettext(" 25  Improved scrub stats\n"));
4724cde58dbcSMatthew Ahrens 		(void) printf(gettext(" 26  Improved snapshot deletion "
4725cde58dbcSMatthew Ahrens 		    "performance\n"));
47266e0cbcaaSMatthew Ahrens 		(void) printf(gettext(" 27  Improved snapshot creation "
47276e0cbcaaSMatthew Ahrens 		    "performance\n"));
4728cb04b873SMark J Musante 		(void) printf(gettext(" 28  Multiple vdev replacements\n"));
4729b24ab676SJeff Bonwick 		(void) printf(gettext("\nFor more information on a particular "
47309a8685acSstephanie scheffler 		    "version, including supported releases,\n"));
47319a8685acSstephanie scheffler 		(void) printf(gettext("see the ZFS Administration Guide.\n\n"));
473257221772SChristopher Siden 	} else if (argc == 0 && upgradeall) {
4733eaca9bbdSeschrock 		cb.cb_first = B_TRUE;
473499653d4eSeschrock 		ret = zpool_iter(g_zfs, upgrade_cb, &cb);
473557221772SChristopher Siden 		if (ret == 0 && cb.cb_first) {
473657221772SChristopher Siden 			if (cb.cb_version == SPA_VERSION) {
473757221772SChristopher Siden 				(void) printf(gettext("All pools are already "
473857221772SChristopher Siden 				    "formatted using feature flags.\n\n"));
473957221772SChristopher Siden 				(void) printf(gettext("Every feature flags "
474057221772SChristopher Siden 				    "pool already has all supported features "
474157221772SChristopher Siden 				    "enabled.\n"));
474257221772SChristopher Siden 			} else {
474357221772SChristopher Siden 				(void) printf(gettext("All pools are already "
474457221772SChristopher Siden 				    "formatted with version %llu or higher.\n"),
474557221772SChristopher Siden 				    cb.cb_version);
4746eaca9bbdSeschrock 			}
4747eaca9bbdSeschrock 		}
474857221772SChristopher Siden 	} else if (argc == 0) {
474957221772SChristopher Siden 		cb.cb_first = B_TRUE;
475057221772SChristopher Siden 		ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
475157221772SChristopher Siden 		assert(ret == 0);
475257221772SChristopher Siden 
475357221772SChristopher Siden 		if (cb.cb_first) {
475457221772SChristopher Siden 			(void) printf(gettext("All pools are formatted "
475557221772SChristopher Siden 			    "using feature flags.\n\n"));
475657221772SChristopher Siden 		} else {
475757221772SChristopher Siden 			(void) printf(gettext("\nUse 'zpool upgrade -v' "
475857221772SChristopher Siden 			    "for a list of available legacy versions.\n"));
475957221772SChristopher Siden 		}
4760eaca9bbdSeschrock 
476157221772SChristopher Siden 		cb.cb_first = B_TRUE;
476257221772SChristopher Siden 		ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
476357221772SChristopher Siden 		assert(ret == 0);
476457221772SChristopher Siden 
476557221772SChristopher Siden 		if (cb.cb_first) {
476657221772SChristopher Siden 			(void) printf(gettext("Every feature flags pool has "
476757221772SChristopher Siden 			    "all supported features enabled.\n"));
476857221772SChristopher Siden 		} else {
476957221772SChristopher Siden 			(void) printf(gettext("\n"));
4770eaca9bbdSeschrock 		}
4771eaca9bbdSeschrock 	} else {
4772b1b8ab34Slling 		ret = for_each_pool(argc, argv, B_FALSE, NULL,
4773b1b8ab34Slling 		    upgrade_one, &cb);
477406eeb2adSek110237 	}
477506eeb2adSek110237 
477606eeb2adSek110237 	return (ret);
477706eeb2adSek110237 }
477806eeb2adSek110237 
4779ecd6cf80Smarks typedef struct hist_cbdata {
4780ecd6cf80Smarks 	boolean_t first;
47814445fffbSMatthew Ahrens 	boolean_t longfmt;
47824445fffbSMatthew Ahrens 	boolean_t internal;
4783ecd6cf80Smarks } hist_cbdata_t;
4784ecd6cf80Smarks 
478506eeb2adSek110237 /*
478606eeb2adSek110237  * Print out the command history for a specific pool.
478706eeb2adSek110237  */
478806eeb2adSek110237 static int
478906eeb2adSek110237 get_history_one(zpool_handle_t *zhp, void *data)
479006eeb2adSek110237 {
479106eeb2adSek110237 	nvlist_t *nvhis;
479206eeb2adSek110237 	nvlist_t **records;
479306eeb2adSek110237 	uint_t numrecords;
479406eeb2adSek110237 	int ret, i;
4795ecd6cf80Smarks 	hist_cbdata_t *cb = (hist_cbdata_t *)data;
479606eeb2adSek110237 
4797ecd6cf80Smarks 	cb->first = B_FALSE;
479806eeb2adSek110237 
479906eeb2adSek110237 	(void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
480006eeb2adSek110237 
480106eeb2adSek110237 	if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
480206eeb2adSek110237 		return (ret);
480306eeb2adSek110237 
480406eeb2adSek110237 	verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
480506eeb2adSek110237 	    &records, &numrecords) == 0);
480606eeb2adSek110237 	for (i = 0; i < numrecords; i++) {
48074445fffbSMatthew Ahrens 		nvlist_t *rec = records[i];
48084445fffbSMatthew Ahrens 		char tbuf[30] = "";
4809ecd6cf80Smarks 
48104445fffbSMatthew Ahrens 		if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
48114445fffbSMatthew Ahrens 			time_t tsec;
48124445fffbSMatthew Ahrens 			struct tm t;
4813ecd6cf80Smarks 
48144445fffbSMatthew Ahrens 			tsec = fnvlist_lookup_uint64(records[i],
48154445fffbSMatthew Ahrens 			    ZPOOL_HIST_TIME);
481606eeb2adSek110237 			(void) localtime_r(&tsec, &t);
481706eeb2adSek110237 			(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
48184445fffbSMatthew Ahrens 		}
48194445fffbSMatthew Ahrens 
48204445fffbSMatthew Ahrens 		if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
48214445fffbSMatthew Ahrens 			(void) printf("%s %s", tbuf,
48224445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
48234445fffbSMatthew Ahrens 		} else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
48244445fffbSMatthew Ahrens 			int ievent =
48254445fffbSMatthew Ahrens 			    fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
48264445fffbSMatthew Ahrens 			if (!cb->internal)
48274445fffbSMatthew Ahrens 				continue;
48284445fffbSMatthew Ahrens 			if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
48294445fffbSMatthew Ahrens 				(void) printf("%s unrecognized record:\n",
48304445fffbSMatthew Ahrens 				    tbuf);
48314445fffbSMatthew Ahrens 				dump_nvlist(rec, 4);
48324445fffbSMatthew Ahrens 				continue;
48334445fffbSMatthew Ahrens 			}
48344445fffbSMatthew Ahrens 			(void) printf("%s [internal %s txg:%lld] %s", tbuf,
48354445fffbSMatthew Ahrens 			    zfs_history_event_names[ievent],
48364445fffbSMatthew Ahrens 			    fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
48374445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
48384445fffbSMatthew Ahrens 		} else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
48394445fffbSMatthew Ahrens 			if (!cb->internal)
48404445fffbSMatthew Ahrens 				continue;
48414445fffbSMatthew Ahrens 			(void) printf("%s [txg:%lld] %s", tbuf,
48424445fffbSMatthew Ahrens 			    fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
48434445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
48444445fffbSMatthew Ahrens 			if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
48454445fffbSMatthew Ahrens 				(void) printf(" %s (%llu)",
48464445fffbSMatthew Ahrens 				    fnvlist_lookup_string(rec,
48474445fffbSMatthew Ahrens 				    ZPOOL_HIST_DSNAME),
48484445fffbSMatthew Ahrens 				    fnvlist_lookup_uint64(rec,
48494445fffbSMatthew Ahrens 				    ZPOOL_HIST_DSID));
48504445fffbSMatthew Ahrens 			}
48514445fffbSMatthew Ahrens 			(void) printf(" %s", fnvlist_lookup_string(rec,
48524445fffbSMatthew Ahrens 			    ZPOOL_HIST_INT_STR));
48534445fffbSMatthew Ahrens 		} else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
48544445fffbSMatthew Ahrens 			if (!cb->internal)
48554445fffbSMatthew Ahrens 				continue;
48564445fffbSMatthew Ahrens 			(void) printf("%s ioctl %s\n", tbuf,
48574445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
48584445fffbSMatthew Ahrens 			if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
48594445fffbSMatthew Ahrens 				(void) printf("    input:\n");
48604445fffbSMatthew Ahrens 				dump_nvlist(fnvlist_lookup_nvlist(rec,
48614445fffbSMatthew Ahrens 				    ZPOOL_HIST_INPUT_NVL), 8);
48624445fffbSMatthew Ahrens 			}
48634445fffbSMatthew Ahrens 			if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
48644445fffbSMatthew Ahrens 				(void) printf("    output:\n");
48654445fffbSMatthew Ahrens 				dump_nvlist(fnvlist_lookup_nvlist(rec,
48664445fffbSMatthew Ahrens 				    ZPOOL_HIST_OUTPUT_NVL), 8);
48674445fffbSMatthew Ahrens 			}
48684445fffbSMatthew Ahrens 		} else {
48694445fffbSMatthew Ahrens 			if (!cb->internal)
48704445fffbSMatthew Ahrens 				continue;
48714445fffbSMatthew Ahrens 			(void) printf("%s unrecognized record:\n", tbuf);
48724445fffbSMatthew Ahrens 			dump_nvlist(rec, 4);
48734445fffbSMatthew Ahrens 		}
4874ecd6cf80Smarks 
4875ecd6cf80Smarks 		if (!cb->longfmt) {
4876ecd6cf80Smarks 			(void) printf("\n");
4877ecd6cf80Smarks 			continue;
487806eeb2adSek110237 		}
4879ecd6cf80Smarks 		(void) printf(" [");
48804445fffbSMatthew Ahrens 		if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
48814445fffbSMatthew Ahrens 			uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
48824445fffbSMatthew Ahrens 			struct passwd *pwd = getpwuid(who);
48834445fffbSMatthew Ahrens 			(void) printf("user %d ", (int)who);
48844445fffbSMatthew Ahrens 			if (pwd != NULL)
48854445fffbSMatthew Ahrens 				(void) printf("(%s) ", pwd->pw_name);
4886ecd6cf80Smarks 		}
48874445fffbSMatthew Ahrens 		if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
48884445fffbSMatthew Ahrens 			(void) printf("on %s",
48894445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
4890ecd6cf80Smarks 		}
48914445fffbSMatthew Ahrens 		if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
48924445fffbSMatthew Ahrens 			(void) printf(":%s",
48934445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
4894ecd6cf80Smarks 		}
4895ecd6cf80Smarks 		(void) printf("]");
4896ecd6cf80Smarks 		(void) printf("\n");
489706eeb2adSek110237 	}
489806eeb2adSek110237 	(void) printf("\n");
489906eeb2adSek110237 	nvlist_free(nvhis);
490006eeb2adSek110237 
490106eeb2adSek110237 	return (ret);
490206eeb2adSek110237 }
490306eeb2adSek110237 
490406eeb2adSek110237 /*
490506eeb2adSek110237  * zpool history <pool>
490606eeb2adSek110237  *
490706eeb2adSek110237  * Displays the history of commands that modified pools.
490806eeb2adSek110237  */
490906eeb2adSek110237 int
491006eeb2adSek110237 zpool_do_history(int argc, char **argv)
491106eeb2adSek110237 {
4912ecd6cf80Smarks 	hist_cbdata_t cbdata = { 0 };
491306eeb2adSek110237 	int ret;
4914ecd6cf80Smarks 	int c;
491506eeb2adSek110237 
4916ecd6cf80Smarks 	cbdata.first = B_TRUE;
4917ecd6cf80Smarks 	/* check options */
4918ecd6cf80Smarks 	while ((c = getopt(argc, argv, "li")) != -1) {
4919ecd6cf80Smarks 		switch (c) {
4920ecd6cf80Smarks 		case 'l':
49214445fffbSMatthew Ahrens 			cbdata.longfmt = B_TRUE;
4922ecd6cf80Smarks 			break;
4923ecd6cf80Smarks 		case 'i':
49244445fffbSMatthew Ahrens 			cbdata.internal = B_TRUE;
4925ecd6cf80Smarks 			break;
4926ecd6cf80Smarks 		case '?':
4927ecd6cf80Smarks 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4928ecd6cf80Smarks 			    optopt);
4929ecd6cf80Smarks 			usage(B_FALSE);
4930ecd6cf80Smarks 		}
4931ecd6cf80Smarks 	}
493206eeb2adSek110237 	argc -= optind;
493306eeb2adSek110237 	argv += optind;
493406eeb2adSek110237 
4935b1b8ab34Slling 	ret = for_each_pool(argc, argv, B_FALSE,  NULL, get_history_one,
4936ecd6cf80Smarks 	    &cbdata);
493706eeb2adSek110237 
4938ecd6cf80Smarks 	if (argc == 0 && cbdata.first == B_TRUE) {
493906eeb2adSek110237 		(void) printf(gettext("no pools available\n"));
494006eeb2adSek110237 		return (0);
4941eaca9bbdSeschrock 	}
4942eaca9bbdSeschrock 
4943eaca9bbdSeschrock 	return (ret);
4944eaca9bbdSeschrock }
4945eaca9bbdSeschrock 
4946b1b8ab34Slling static int
4947b1b8ab34Slling get_callback(zpool_handle_t *zhp, void *data)
4948b1b8ab34Slling {
4949990b4856Slling 	zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
4950b1b8ab34Slling 	char value[MAXNAMELEN];
4951990b4856Slling 	zprop_source_t srctype;
4952990b4856Slling 	zprop_list_t *pl;
4953b1b8ab34Slling 
4954b1b8ab34Slling 	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
4955b1b8ab34Slling 
4956b1b8ab34Slling 		/*
4957990b4856Slling 		 * Skip the special fake placeholder. This will also skip
4958990b4856Slling 		 * over the name property when 'all' is specified.
4959b1b8ab34Slling 		 */
4960990b4856Slling 		if (pl->pl_prop == ZPOOL_PROP_NAME &&
4961b1b8ab34Slling 		    pl == cbp->cb_proplist)
4962b1b8ab34Slling 			continue;
4963b1b8ab34Slling 
4964ad135b5dSChristopher Siden 		if (pl->pl_prop == ZPROP_INVAL &&
4965ad135b5dSChristopher Siden 		    (zpool_prop_feature(pl->pl_user_prop) ||
4966ad135b5dSChristopher Siden 		    zpool_prop_unsupported(pl->pl_user_prop))) {
4967ad135b5dSChristopher Siden 			srctype = ZPROP_SRC_LOCAL;
4968ad135b5dSChristopher Siden 
4969ad135b5dSChristopher Siden 			if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
4970ad135b5dSChristopher Siden 			    value, sizeof (value)) == 0) {
4971ad135b5dSChristopher Siden 				zprop_print_one_property(zpool_get_name(zhp),
4972ad135b5dSChristopher Siden 				    cbp, pl->pl_user_prop, value, srctype,
4973ad135b5dSChristopher Siden 				    NULL, NULL);
4974ad135b5dSChristopher Siden 			}
4975ad135b5dSChristopher Siden 		} else {
4976ad135b5dSChristopher Siden 			if (zpool_get_prop(zhp, pl->pl_prop, value,
4977ad135b5dSChristopher Siden 			    sizeof (value), &srctype) != 0)
4978b1b8ab34Slling 				continue;
4979b1b8ab34Slling 
4980990b4856Slling 			zprop_print_one_property(zpool_get_name(zhp), cbp,
4981ad135b5dSChristopher Siden 			    zpool_prop_to_name(pl->pl_prop), value, srctype,
4982ad135b5dSChristopher Siden 			    NULL, NULL);
4983ad135b5dSChristopher Siden 		}
4984b1b8ab34Slling 	}
4985b1b8ab34Slling 	return (0);
4986b1b8ab34Slling }
4987b1b8ab34Slling 
4988b1b8ab34Slling int
4989b1b8ab34Slling zpool_do_get(int argc, char **argv)
4990b1b8ab34Slling {
4991990b4856Slling 	zprop_get_cbdata_t cb = { 0 };
4992990b4856Slling 	zprop_list_t fake_name = { 0 };
4993b1b8ab34Slling 	int ret;
4994b1b8ab34Slling 
4995ad135b5dSChristopher Siden 	if (argc < 2) {
4996ad135b5dSChristopher Siden 		(void) fprintf(stderr, gettext("missing property "
4997ad135b5dSChristopher Siden 		    "argument\n"));
4998b1b8ab34Slling 		usage(B_FALSE);
4999ad135b5dSChristopher Siden 	}
5000b1b8ab34Slling 
5001b1b8ab34Slling 	cb.cb_first = B_TRUE;
5002990b4856Slling 	cb.cb_sources = ZPROP_SRC_ALL;
5003b1b8ab34Slling 	cb.cb_columns[0] = GET_COL_NAME;
5004b1b8ab34Slling 	cb.cb_columns[1] = GET_COL_PROPERTY;
5005b1b8ab34Slling 	cb.cb_columns[2] = GET_COL_VALUE;
5006b1b8ab34Slling 	cb.cb_columns[3] = GET_COL_SOURCE;
5007990b4856Slling 	cb.cb_type = ZFS_TYPE_POOL;
5008b1b8ab34Slling 
5009990b4856Slling 	if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
5010990b4856Slling 	    ZFS_TYPE_POOL) != 0)
5011b1b8ab34Slling 		usage(B_FALSE);
5012b1b8ab34Slling 
5013b1b8ab34Slling 	if (cb.cb_proplist != NULL) {
5014990b4856Slling 		fake_name.pl_prop = ZPOOL_PROP_NAME;
5015b1b8ab34Slling 		fake_name.pl_width = strlen(gettext("NAME"));
5016b1b8ab34Slling 		fake_name.pl_next = cb.cb_proplist;
5017b1b8ab34Slling 		cb.cb_proplist = &fake_name;
5018b1b8ab34Slling 	}
5019b1b8ab34Slling 
5020b1b8ab34Slling 	ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
5021b1b8ab34Slling 	    get_callback, &cb);
5022b1b8ab34Slling 
5023b1b8ab34Slling 	if (cb.cb_proplist == &fake_name)
5024990b4856Slling 		zprop_free_list(fake_name.pl_next);
5025b1b8ab34Slling 	else
5026990b4856Slling 		zprop_free_list(cb.cb_proplist);
5027b1b8ab34Slling 
5028b1b8ab34Slling 	return (ret);
5029b1b8ab34Slling }
5030b1b8ab34Slling 
5031b1b8ab34Slling typedef struct set_cbdata {
5032b1b8ab34Slling 	char *cb_propname;
5033b1b8ab34Slling 	char *cb_value;
5034b1b8ab34Slling 	boolean_t cb_any_successful;
5035b1b8ab34Slling } set_cbdata_t;
5036b1b8ab34Slling 
5037b1b8ab34Slling int
5038b1b8ab34Slling set_callback(zpool_handle_t *zhp, void *data)
5039b1b8ab34Slling {
5040b1b8ab34Slling 	int error;
5041b1b8ab34Slling 	set_cbdata_t *cb = (set_cbdata_t *)data;
5042b1b8ab34Slling 
5043b1b8ab34Slling 	error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5044b1b8ab34Slling 
5045b1b8ab34Slling 	if (!error)
5046b1b8ab34Slling 		cb->cb_any_successful = B_TRUE;
5047b1b8ab34Slling 
5048b1b8ab34Slling 	return (error);
5049b1b8ab34Slling }
5050b1b8ab34Slling 
5051b1b8ab34Slling int
5052b1b8ab34Slling zpool_do_set(int argc, char **argv)
5053b1b8ab34Slling {
5054b1b8ab34Slling 	set_cbdata_t cb = { 0 };
5055b1b8ab34Slling 	int error;
5056b1b8ab34Slling 
5057b1b8ab34Slling 	if (argc > 1 && argv[1][0] == '-') {
5058b1b8ab34Slling 		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
5059b1b8ab34Slling 		    argv[1][1]);
5060b1b8ab34Slling 		usage(B_FALSE);
5061b1b8ab34Slling 	}
5062b1b8ab34Slling 
5063b1b8ab34Slling 	if (argc < 2) {
5064b1b8ab34Slling 		(void) fprintf(stderr, gettext("missing property=value "
5065b1b8ab34Slling 		    "argument\n"));
5066b1b8ab34Slling 		usage(B_FALSE);
5067b1b8ab34Slling 	}
5068b1b8ab34Slling 
5069b1b8ab34Slling 	if (argc < 3) {
5070b1b8ab34Slling 		(void) fprintf(stderr, gettext("missing pool name\n"));
5071b1b8ab34Slling 		usage(B_FALSE);
5072b1b8ab34Slling 	}
5073b1b8ab34Slling 
5074b1b8ab34Slling 	if (argc > 3) {
5075b1b8ab34Slling 		(void) fprintf(stderr, gettext("too many pool names\n"));
5076b1b8ab34Slling 		usage(B_FALSE);
5077b1b8ab34Slling 	}
5078b1b8ab34Slling 
5079b1b8ab34Slling 	cb.cb_propname = argv[1];
5080b1b8ab34Slling 	cb.cb_value = strchr(cb.cb_propname, '=');
5081b1b8ab34Slling 	if (cb.cb_value == NULL) {
5082b1b8ab34Slling 		(void) fprintf(stderr, gettext("missing value in "
5083b1b8ab34Slling 		    "property=value argument\n"));
5084b1b8ab34Slling 		usage(B_FALSE);
5085b1b8ab34Slling 	}
5086b1b8ab34Slling 
5087b1b8ab34Slling 	*(cb.cb_value) = '\0';
5088b1b8ab34Slling 	cb.cb_value++;
5089b1b8ab34Slling 
5090b1b8ab34Slling 	error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5091b1b8ab34Slling 	    set_callback, &cb);
5092b1b8ab34Slling 
5093b1b8ab34Slling 	return (error);
5094b1b8ab34Slling }
5095b1b8ab34Slling 
5096b1b8ab34Slling static int
5097b1b8ab34Slling find_command_idx(char *command, int *idx)
5098b1b8ab34Slling {
5099b1b8ab34Slling 	int i;
5100b1b8ab34Slling 
5101b1b8ab34Slling 	for (i = 0; i < NCOMMAND; i++) {
5102b1b8ab34Slling 		if (command_table[i].name == NULL)
5103b1b8ab34Slling 			continue;
5104b1b8ab34Slling 
5105b1b8ab34Slling 		if (strcmp(command, command_table[i].name) == 0) {
5106b1b8ab34Slling 			*idx = i;
5107b1b8ab34Slling 			return (0);
5108b1b8ab34Slling 		}
5109b1b8ab34Slling 	}
5110b1b8ab34Slling 	return (1);
5111b1b8ab34Slling }
5112b1b8ab34Slling 
5113fa9e4066Sahrens int
5114fa9e4066Sahrens main(int argc, char **argv)
5115fa9e4066Sahrens {
5116fa9e4066Sahrens 	int ret;
5117fa9e4066Sahrens 	int i;
5118fa9e4066Sahrens 	char *cmdname;
5119fa9e4066Sahrens 
5120fa9e4066Sahrens 	(void) setlocale(LC_ALL, "");
5121fa9e4066Sahrens 	(void) textdomain(TEXT_DOMAIN);
5122fa9e4066Sahrens 
512399653d4eSeschrock 	if ((g_zfs = libzfs_init()) == NULL) {
512499653d4eSeschrock 		(void) fprintf(stderr, gettext("internal error: failed to "
5125203a47d8Snd150628 		    "initialize ZFS library\n"));
512699653d4eSeschrock 		return (1);
512799653d4eSeschrock 	}
512899653d4eSeschrock 
512999653d4eSeschrock 	libzfs_print_on_error(g_zfs, B_TRUE);
513099653d4eSeschrock 
5131fa9e4066Sahrens 	opterr = 0;
5132fa9e4066Sahrens 
5133fa9e4066Sahrens 	/*
5134fa9e4066Sahrens 	 * Make sure the user has specified some command.
5135fa9e4066Sahrens 	 */
5136fa9e4066Sahrens 	if (argc < 2) {
5137fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing command\n"));
513899653d4eSeschrock 		usage(B_FALSE);
5139fa9e4066Sahrens 	}
5140fa9e4066Sahrens 
5141fa9e4066Sahrens 	cmdname = argv[1];
5142fa9e4066Sahrens 
5143fa9e4066Sahrens 	/*
5144fa9e4066Sahrens 	 * Special case '-?'
5145fa9e4066Sahrens 	 */
5146fa9e4066Sahrens 	if (strcmp(cmdname, "-?") == 0)
514799653d4eSeschrock 		usage(B_TRUE);
5148fa9e4066Sahrens 
51494445fffbSMatthew Ahrens 	zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
51502a6b87f0Sek110237 
5151fa9e4066Sahrens 	/*
5152fa9e4066Sahrens 	 * Run the appropriate command.
5153fa9e4066Sahrens 	 */
5154b1b8ab34Slling 	if (find_command_idx(cmdname, &i) == 0) {
5155fa9e4066Sahrens 		current_command = &command_table[i];
5156fa9e4066Sahrens 		ret = command_table[i].func(argc - 1, argv + 1);
515791ebeef5Sahrens 	} else if (strchr(cmdname, '=')) {
515891ebeef5Sahrens 		verify(find_command_idx("set", &i) == 0);
515991ebeef5Sahrens 		current_command = &command_table[i];
516091ebeef5Sahrens 		ret = command_table[i].func(argc, argv);
516191ebeef5Sahrens 	} else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5162fa9e4066Sahrens 		/*
516391ebeef5Sahrens 		 * 'freeze' is a vile debugging abomination, so we treat
516491ebeef5Sahrens 		 * it as such.
5165fa9e4066Sahrens 		 */
5166ea8dc4b6Seschrock 		char buf[16384];
5167ea8dc4b6Seschrock 		int fd = open(ZFS_DEV, O_RDWR);
5168fa9e4066Sahrens 		(void) strcpy((void *)buf, argv[2]);
5169fa9e4066Sahrens 		return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
517091ebeef5Sahrens 	} else {
5171fa9e4066Sahrens 		(void) fprintf(stderr, gettext("unrecognized "
5172fa9e4066Sahrens 		    "command '%s'\n"), cmdname);
517399653d4eSeschrock 		usage(B_FALSE);
5174fa9e4066Sahrens 	}
5175fa9e4066Sahrens 
51764445fffbSMatthew Ahrens 	if (ret == 0 && log_history)
51774445fffbSMatthew Ahrens 		(void) zpool_log_history(g_zfs, history_str);
51784445fffbSMatthew Ahrens 
517999653d4eSeschrock 	libzfs_fini(g_zfs);
518099653d4eSeschrock 
5181fa9e4066Sahrens 	/*
5182fa9e4066Sahrens 	 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5183fa9e4066Sahrens 	 * for the purposes of running ::findleaks.
5184fa9e4066Sahrens 	 */
5185fa9e4066Sahrens 	if (getenv("ZFS_ABORT") != NULL) {
5186fa9e4066Sahrens 		(void) printf("dumping core by request\n");
5187fa9e4066Sahrens 		abort();
5188fa9e4066Sahrens 	}
5189fa9e4066Sahrens 
5190fa9e4066Sahrens 	return (ret);
5191fa9e4066Sahrens }
5192