xref: /titanic_53/usr/src/cmd/zpool/zpool_main.c (revision 52244c0958bdf281ca42932b449f644b4decfdc2)
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.
25e42d2059SMatthew Ahrens  * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
26e1d5e507SFrederik Wessels  * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
279edf9ebdSPrasad Joshi  * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
28fa9e4066Sahrens  */
29fa9e4066Sahrens 
30fa9e4066Sahrens #include <assert.h>
31fa9e4066Sahrens #include <ctype.h>
32fa9e4066Sahrens #include <dirent.h>
33fa9e4066Sahrens #include <errno.h>
34fa9e4066Sahrens #include <fcntl.h>
35fa9e4066Sahrens #include <libgen.h>
36fa9e4066Sahrens #include <libintl.h>
37fa9e4066Sahrens #include <libuutil.h>
38fa9e4066Sahrens #include <locale.h>
39fa9e4066Sahrens #include <stdio.h>
40fa9e4066Sahrens #include <stdlib.h>
41fa9e4066Sahrens #include <string.h>
42fa9e4066Sahrens #include <strings.h>
43fa9e4066Sahrens #include <unistd.h>
44fa9e4066Sahrens #include <priv.h>
45ecd6cf80Smarks #include <pwd.h>
46ecd6cf80Smarks #include <zone.h>
474263d13fSGeorge Wilson #include <zfs_prop.h>
48b1b8ab34Slling #include <sys/fs/zfs.h>
49fa9e4066Sahrens #include <sys/stat.h>
50fa9e4066Sahrens 
51fa9e4066Sahrens #include <libzfs.h>
52fa9e4066Sahrens 
53fa9e4066Sahrens #include "zpool_util.h"
54b7b97454Sperrin #include "zfs_comutil.h"
55ad135b5dSChristopher Siden #include "zfeature_common.h"
56fa9e4066Sahrens 
5726fd7700SKrishnendu Sadhukhan - Sun Microsystems #include "statcommon.h"
5826fd7700SKrishnendu Sadhukhan - Sun Microsystems 
59fa9e4066Sahrens static int zpool_do_create(int, char **);
60fa9e4066Sahrens static int zpool_do_destroy(int, char **);
61fa9e4066Sahrens 
62fa9e4066Sahrens static int zpool_do_add(int, char **);
6399653d4eSeschrock static int zpool_do_remove(int, char **);
64fa9e4066Sahrens 
65fa9e4066Sahrens static int zpool_do_list(int, char **);
66fa9e4066Sahrens static int zpool_do_iostat(int, char **);
67fa9e4066Sahrens static int zpool_do_status(int, char **);
68fa9e4066Sahrens 
69fa9e4066Sahrens static int zpool_do_online(int, char **);
70fa9e4066Sahrens static int zpool_do_offline(int, char **);
71ea8dc4b6Seschrock static int zpool_do_clear(int, char **);
724263d13fSGeorge Wilson static int zpool_do_reopen(int, char **);
73fa9e4066Sahrens 
74e9103aaeSGarrett D'Amore static int zpool_do_reguid(int, char **);
75e9103aaeSGarrett D'Amore 
76fa9e4066Sahrens static int zpool_do_attach(int, char **);
77fa9e4066Sahrens static int zpool_do_detach(int, char **);
78fa9e4066Sahrens static int zpool_do_replace(int, char **);
791195e687SMark J Musante static int zpool_do_split(int, char **);
80fa9e4066Sahrens 
81fa9e4066Sahrens static int zpool_do_scrub(int, char **);
82fa9e4066Sahrens 
83fa9e4066Sahrens static int zpool_do_import(int, char **);
84fa9e4066Sahrens static int zpool_do_export(int, char **);
85fa9e4066Sahrens 
86eaca9bbdSeschrock static int zpool_do_upgrade(int, char **);
87eaca9bbdSeschrock 
8806eeb2adSek110237 static int zpool_do_history(int, char **);
8906eeb2adSek110237 
90b1b8ab34Slling static int zpool_do_get(int, char **);
91b1b8ab34Slling static int zpool_do_set(int, char **);
92b1b8ab34Slling 
93fa9e4066Sahrens /*
94fa9e4066Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
95fa9e4066Sahrens  * debugging facilities.
96fa9e4066Sahrens  */
9729ab75c9Srm160521 
9829ab75c9Srm160521 #ifdef DEBUG
99fa9e4066Sahrens const char *
10099653d4eSeschrock _umem_debug_init(void)
101fa9e4066Sahrens {
102fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
103fa9e4066Sahrens }
104fa9e4066Sahrens 
105fa9e4066Sahrens const char *
106fa9e4066Sahrens _umem_logging_init(void)
107fa9e4066Sahrens {
108fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
109fa9e4066Sahrens }
11029ab75c9Srm160521 #endif
111fa9e4066Sahrens 
11265cd9f28Seschrock typedef enum {
11365cd9f28Seschrock 	HELP_ADD,
11465cd9f28Seschrock 	HELP_ATTACH,
115ea8dc4b6Seschrock 	HELP_CLEAR,
11665cd9f28Seschrock 	HELP_CREATE,
11765cd9f28Seschrock 	HELP_DESTROY,
11865cd9f28Seschrock 	HELP_DETACH,
11965cd9f28Seschrock 	HELP_EXPORT,
12006eeb2adSek110237 	HELP_HISTORY,
12165cd9f28Seschrock 	HELP_IMPORT,
12265cd9f28Seschrock 	HELP_IOSTAT,
12365cd9f28Seschrock 	HELP_LIST,
12465cd9f28Seschrock 	HELP_OFFLINE,
12565cd9f28Seschrock 	HELP_ONLINE,
12665cd9f28Seschrock 	HELP_REPLACE,
12799653d4eSeschrock 	HELP_REMOVE,
12865cd9f28Seschrock 	HELP_SCRUB,
129eaca9bbdSeschrock 	HELP_STATUS,
130b1b8ab34Slling 	HELP_UPGRADE,
131b1b8ab34Slling 	HELP_GET,
1321195e687SMark J Musante 	HELP_SET,
133e9103aaeSGarrett D'Amore 	HELP_SPLIT,
1344263d13fSGeorge Wilson 	HELP_REGUID,
1354263d13fSGeorge Wilson 	HELP_REOPEN
13665cd9f28Seschrock } zpool_help_t;
13765cd9f28Seschrock 
13865cd9f28Seschrock 
139fa9e4066Sahrens typedef struct zpool_command {
140fa9e4066Sahrens 	const char	*name;
141fa9e4066Sahrens 	int		(*func)(int, char **);
14265cd9f28Seschrock 	zpool_help_t	usage;
143fa9e4066Sahrens } zpool_command_t;
144fa9e4066Sahrens 
145fa9e4066Sahrens /*
146fa9e4066Sahrens  * Master command table.  Each ZFS command has a name, associated function, and
147ea8dc4b6Seschrock  * usage message.  The usage messages need to be internationalized, so we have
148ea8dc4b6Seschrock  * to have a function to return the usage message based on a command index.
14965cd9f28Seschrock  *
15065cd9f28Seschrock  * These commands are organized according to how they are displayed in the usage
15165cd9f28Seschrock  * message.  An empty command (one with a NULL name) indicates an empty line in
15265cd9f28Seschrock  * the generic usage message.
153fa9e4066Sahrens  */
154fa9e4066Sahrens static zpool_command_t command_table[] = {
15565cd9f28Seschrock 	{ "create",	zpool_do_create,	HELP_CREATE		},
15665cd9f28Seschrock 	{ "destroy",	zpool_do_destroy,	HELP_DESTROY		},
157fa9e4066Sahrens 	{ NULL },
15865cd9f28Seschrock 	{ "add",	zpool_do_add,		HELP_ADD		},
15999653d4eSeschrock 	{ "remove",	zpool_do_remove,	HELP_REMOVE		},
160fa9e4066Sahrens 	{ NULL },
16165cd9f28Seschrock 	{ "list",	zpool_do_list,		HELP_LIST		},
16265cd9f28Seschrock 	{ "iostat",	zpool_do_iostat,	HELP_IOSTAT		},
16365cd9f28Seschrock 	{ "status",	zpool_do_status,	HELP_STATUS		},
164fa9e4066Sahrens 	{ NULL },
16565cd9f28Seschrock 	{ "online",	zpool_do_online,	HELP_ONLINE		},
16665cd9f28Seschrock 	{ "offline",	zpool_do_offline,	HELP_OFFLINE		},
167ea8dc4b6Seschrock 	{ "clear",	zpool_do_clear,		HELP_CLEAR		},
1684263d13fSGeorge Wilson 	{ "reopen",	zpool_do_reopen,	HELP_REOPEN		},
169fa9e4066Sahrens 	{ NULL },
17065cd9f28Seschrock 	{ "attach",	zpool_do_attach,	HELP_ATTACH		},
17165cd9f28Seschrock 	{ "detach",	zpool_do_detach,	HELP_DETACH		},
17265cd9f28Seschrock 	{ "replace",	zpool_do_replace,	HELP_REPLACE		},
1731195e687SMark J Musante 	{ "split",	zpool_do_split,		HELP_SPLIT		},
174fa9e4066Sahrens 	{ NULL },
17565cd9f28Seschrock 	{ "scrub",	zpool_do_scrub,		HELP_SCRUB		},
176fa9e4066Sahrens 	{ NULL },
17765cd9f28Seschrock 	{ "import",	zpool_do_import,	HELP_IMPORT		},
17865cd9f28Seschrock 	{ "export",	zpool_do_export,	HELP_EXPORT		},
17906eeb2adSek110237 	{ "upgrade",	zpool_do_upgrade,	HELP_UPGRADE		},
180e9103aaeSGarrett D'Amore 	{ "reguid",	zpool_do_reguid,	HELP_REGUID		},
18106eeb2adSek110237 	{ NULL },
182b1b8ab34Slling 	{ "history",	zpool_do_history,	HELP_HISTORY		},
183b1b8ab34Slling 	{ "get",	zpool_do_get,		HELP_GET		},
184b1b8ab34Slling 	{ "set",	zpool_do_set,		HELP_SET		},
185fa9e4066Sahrens };
186fa9e4066Sahrens 
187fa9e4066Sahrens #define	NCOMMAND	(sizeof (command_table) / sizeof (command_table[0]))
188fa9e4066Sahrens 
1894445fffbSMatthew Ahrens static zpool_command_t *current_command;
1902a6b87f0Sek110237 static char history_str[HIS_MAX_RECORD_LEN];
1914445fffbSMatthew Ahrens static boolean_t log_history = B_TRUE;
19226fd7700SKrishnendu Sadhukhan - Sun Microsystems static uint_t timestamp_fmt = NODATE;
19326fd7700SKrishnendu Sadhukhan - Sun Microsystems 
19465cd9f28Seschrock static const char *
19565cd9f28Seschrock get_usage(zpool_help_t idx) {
19665cd9f28Seschrock 	switch (idx) {
19765cd9f28Seschrock 	case HELP_ADD:
19865cd9f28Seschrock 		return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
19965cd9f28Seschrock 	case HELP_ATTACH:
20065cd9f28Seschrock 		return (gettext("\tattach [-f] <pool> <device> "
201e45ce728Sahrens 		    "<new-device>\n"));
202ea8dc4b6Seschrock 	case HELP_CLEAR:
203468c413aSTim Haley 		return (gettext("\tclear [-nF] <pool> [device]\n"));
20465cd9f28Seschrock 	case HELP_CREATE:
205ad135b5dSChristopher Siden 		return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
2060a48a24eStimh 		    "\t    [-O file-system-property=value] ... \n"
207990b4856Slling 		    "\t    [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
20865cd9f28Seschrock 	case HELP_DESTROY:
20965cd9f28Seschrock 		return (gettext("\tdestroy [-f] <pool>\n"));
21065cd9f28Seschrock 	case HELP_DETACH:
21165cd9f28Seschrock 		return (gettext("\tdetach <pool> <device>\n"));
21265cd9f28Seschrock 	case HELP_EXPORT:
21365cd9f28Seschrock 		return (gettext("\texport [-f] <pool> ...\n"));
21406eeb2adSek110237 	case HELP_HISTORY:
215ecd6cf80Smarks 		return (gettext("\thistory [-il] [<pool>] ...\n"));
21665cd9f28Seschrock 	case HELP_IMPORT:
2174c58d714Sdarrenm 		return (gettext("\timport [-d dir] [-D]\n"
218f9af39baSGeorge Wilson 		    "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
2192f8aaab3Seschrock 		    "\timport [-o mntopts] [-o property=value] ... \n"
220f9af39baSGeorge Wilson 		    "\t    [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
221f9af39baSGeorge Wilson 		    "[-R root] [-F [-n]] -a\n"
2222f8aaab3Seschrock 		    "\timport [-o mntopts] [-o property=value] ... \n"
223f9af39baSGeorge Wilson 		    "\t    [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
224f9af39baSGeorge Wilson 		    "[-R root] [-F [-n]]\n"
225f9af39baSGeorge Wilson 		    "\t    <pool | id> [newpool]\n"));
22665cd9f28Seschrock 	case HELP_IOSTAT:
22726fd7700SKrishnendu Sadhukhan - Sun Microsystems 		return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
22865cd9f28Seschrock 		    "[count]]\n"));
22965cd9f28Seschrock 	case HELP_LIST:
230c58b3526SAdam Stevko 		return (gettext("\tlist [-Hp] [-o property[,...]] "
2313f9d6ad7SLin Ling 		    "[-T d|u] [pool] ... [interval [count]]\n"));
23265cd9f28Seschrock 	case HELP_OFFLINE:
233441d80aaSlling 		return (gettext("\toffline [-t] <pool> <device> ...\n"));
23465cd9f28Seschrock 	case HELP_ONLINE:
235441d80aaSlling 		return (gettext("\tonline <pool> <device> ...\n"));
23665cd9f28Seschrock 	case HELP_REPLACE:
23765cd9f28Seschrock 		return (gettext("\treplace [-f] <pool> <device> "
238e45ce728Sahrens 		    "[new-device]\n"));
23999653d4eSeschrock 	case HELP_REMOVE:
240fa94a07fSbrendan 		return (gettext("\tremove <pool> <device> ...\n"));
2414263d13fSGeorge Wilson 	case HELP_REOPEN:
24231d7e8faSGeorge Wilson 		return (gettext("\treopen <pool>\n"));
24365cd9f28Seschrock 	case HELP_SCRUB:
24465cd9f28Seschrock 		return (gettext("\tscrub [-s] <pool> ...\n"));
24565cd9f28Seschrock 	case HELP_STATUS:
2463f9d6ad7SLin Ling 		return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
2473f9d6ad7SLin Ling 		    "[count]]\n"));
248eaca9bbdSeschrock 	case HELP_UPGRADE:
249eaca9bbdSeschrock 		return (gettext("\tupgrade\n"
250eaca9bbdSeschrock 		    "\tupgrade -v\n"
251990b4856Slling 		    "\tupgrade [-V version] <-a | pool ...>\n"));
252b1b8ab34Slling 	case HELP_GET:
253c58b3526SAdam Stevko 		return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
254c58b3526SAdam Stevko 		    "<\"all\" | property[,...]> <pool> ...\n"));
255b1b8ab34Slling 	case HELP_SET:
256b1b8ab34Slling 		return (gettext("\tset <property=value> <pool> \n"));
2571195e687SMark J Musante 	case HELP_SPLIT:
2581195e687SMark J Musante 		return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
2591195e687SMark J Musante 		    "\t    [-o property=value] <pool> <newpool> "
2601195e687SMark J Musante 		    "[<device> ...]\n"));
261e9103aaeSGarrett D'Amore 	case HELP_REGUID:
262e9103aaeSGarrett D'Amore 		return (gettext("\treguid <pool>\n"));
26365cd9f28Seschrock 	}
26465cd9f28Seschrock 
26565cd9f28Seschrock 	abort();
26665cd9f28Seschrock 	/* NOTREACHED */
26765cd9f28Seschrock }
26865cd9f28Seschrock 
269fa9e4066Sahrens 
270fa9e4066Sahrens /*
271b1b8ab34Slling  * Callback routine that will print out a pool property value.
272b1b8ab34Slling  */
273990b4856Slling static int
274990b4856Slling print_prop_cb(int prop, void *cb)
275b1b8ab34Slling {
276b1b8ab34Slling 	FILE *fp = cb;
277b1b8ab34Slling 
278b24ab676SJeff Bonwick 	(void) fprintf(fp, "\t%-15s  ", zpool_prop_to_name(prop));
279b1b8ab34Slling 
280990b4856Slling 	if (zpool_prop_readonly(prop))
281990b4856Slling 		(void) fprintf(fp, "  NO   ");
282990b4856Slling 	else
283990b4856Slling 		(void) fprintf(fp, " YES   ");
284990b4856Slling 
285b1b8ab34Slling 	if (zpool_prop_values(prop) == NULL)
286b1b8ab34Slling 		(void) fprintf(fp, "-\n");
287b1b8ab34Slling 	else
288b1b8ab34Slling 		(void) fprintf(fp, "%s\n", zpool_prop_values(prop));
289b1b8ab34Slling 
290990b4856Slling 	return (ZPROP_CONT);
291b1b8ab34Slling }
292b1b8ab34Slling 
293b1b8ab34Slling /*
294fa9e4066Sahrens  * Display usage message.  If we're inside a command, display only the usage for
295fa9e4066Sahrens  * that command.  Otherwise, iterate over the entire command table and display
296fa9e4066Sahrens  * a complete usage message.
297fa9e4066Sahrens  */
298fa9e4066Sahrens void
29999653d4eSeschrock usage(boolean_t requested)
300fa9e4066Sahrens {
301fa9e4066Sahrens 	FILE *fp = requested ? stdout : stderr;
302fa9e4066Sahrens 
303fa9e4066Sahrens 	if (current_command == NULL) {
304fa9e4066Sahrens 		int i;
305fa9e4066Sahrens 
306fa9e4066Sahrens 		(void) fprintf(fp, gettext("usage: zpool command args ...\n"));
307fa9e4066Sahrens 		(void) fprintf(fp,
308fa9e4066Sahrens 		    gettext("where 'command' is one of the following:\n\n"));
309fa9e4066Sahrens 
310fa9e4066Sahrens 		for (i = 0; i < NCOMMAND; i++) {
311fa9e4066Sahrens 			if (command_table[i].name == NULL)
312fa9e4066Sahrens 				(void) fprintf(fp, "\n");
313fa9e4066Sahrens 			else
314fa9e4066Sahrens 				(void) fprintf(fp, "%s",
31565cd9f28Seschrock 				    get_usage(command_table[i].usage));
316fa9e4066Sahrens 		}
317fa9e4066Sahrens 	} else {
318fa9e4066Sahrens 		(void) fprintf(fp, gettext("usage:\n"));
31965cd9f28Seschrock 		(void) fprintf(fp, "%s", get_usage(current_command->usage));
320fa9e4066Sahrens 	}
321fa9e4066Sahrens 
322b1b8ab34Slling 	if (current_command != NULL &&
323b1b8ab34Slling 	    ((strcmp(current_command->name, "set") == 0) ||
324990b4856Slling 	    (strcmp(current_command->name, "get") == 0) ||
325990b4856Slling 	    (strcmp(current_command->name, "list") == 0))) {
326b1b8ab34Slling 
327b1b8ab34Slling 		(void) fprintf(fp,
328b1b8ab34Slling 		    gettext("\nthe following properties are supported:\n"));
329b1b8ab34Slling 
330b24ab676SJeff Bonwick 		(void) fprintf(fp, "\n\t%-15s  %s   %s\n\n",
331990b4856Slling 		    "PROPERTY", "EDIT", "VALUES");
332b1b8ab34Slling 
333b1b8ab34Slling 		/* Iterate over all properties */
334990b4856Slling 		(void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
335990b4856Slling 		    ZFS_TYPE_POOL);
336ad135b5dSChristopher Siden 
337ad135b5dSChristopher Siden 		(void) fprintf(fp, "\t%-15s   ", "feature@...");
338ad135b5dSChristopher Siden 		(void) fprintf(fp, "YES   disabled | enabled | active\n");
339ad135b5dSChristopher Siden 
340ad135b5dSChristopher Siden 		(void) fprintf(fp, gettext("\nThe feature@ properties must be "
341ad135b5dSChristopher Siden 		    "appended with a feature name.\nSee zpool-features(5).\n"));
342b1b8ab34Slling 	}
343b1b8ab34Slling 
344e9dbad6fSeschrock 	/*
345e9dbad6fSeschrock 	 * See comments at end of main().
346e9dbad6fSeschrock 	 */
347e9dbad6fSeschrock 	if (getenv("ZFS_ABORT") != NULL) {
348e9dbad6fSeschrock 		(void) printf("dumping core by request\n");
349e9dbad6fSeschrock 		abort();
350e9dbad6fSeschrock 	}
351e9dbad6fSeschrock 
352fa9e4066Sahrens 	exit(requested ? 0 : 2);
353fa9e4066Sahrens }
354fa9e4066Sahrens 
355fa9e4066Sahrens void
3568654d025Sperrin print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
3578654d025Sperrin     boolean_t print_logs)
358fa9e4066Sahrens {
359fa9e4066Sahrens 	nvlist_t **child;
360fa9e4066Sahrens 	uint_t c, children;
361afefbcddSeschrock 	char *vname;
362fa9e4066Sahrens 
363fa9e4066Sahrens 	if (name != NULL)
364fa9e4066Sahrens 		(void) printf("\t%*s%s\n", indent, "", name);
365fa9e4066Sahrens 
366fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
367fa9e4066Sahrens 	    &child, &children) != 0)
368fa9e4066Sahrens 		return;
369fa9e4066Sahrens 
370afefbcddSeschrock 	for (c = 0; c < children; c++) {
3718654d025Sperrin 		uint64_t is_log = B_FALSE;
3728654d025Sperrin 
3738654d025Sperrin 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3748654d025Sperrin 		    &is_log);
3758654d025Sperrin 		if ((is_log && !print_logs) || (!is_log && print_logs))
3768654d025Sperrin 			continue;
3778654d025Sperrin 
37888ecc943SGeorge Wilson 		vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3798654d025Sperrin 		print_vdev_tree(zhp, vname, child[c], indent + 2,
3808654d025Sperrin 		    B_FALSE);
381afefbcddSeschrock 		free(vname);
382afefbcddSeschrock 	}
383fa9e4066Sahrens }
384fa9e4066Sahrens 
38557221772SChristopher Siden static boolean_t
38657221772SChristopher Siden prop_list_contains_feature(nvlist_t *proplist)
38757221772SChristopher Siden {
38857221772SChristopher Siden 	nvpair_t *nvp;
38957221772SChristopher Siden 	for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
39057221772SChristopher Siden 	    nvp = nvlist_next_nvpair(proplist, nvp)) {
39157221772SChristopher Siden 		if (zpool_prop_feature(nvpair_name(nvp)))
39257221772SChristopher Siden 			return (B_TRUE);
39357221772SChristopher Siden 	}
39457221772SChristopher Siden 	return (B_FALSE);
39557221772SChristopher Siden }
39657221772SChristopher Siden 
397fa9e4066Sahrens /*
398990b4856Slling  * Add a property pair (name, string-value) into a property nvlist.
399990b4856Slling  */
400990b4856Slling static int
4010a48a24eStimh add_prop_list(const char *propname, char *propval, nvlist_t **props,
4020a48a24eStimh     boolean_t poolprop)
403990b4856Slling {
4040a48a24eStimh 	zpool_prop_t prop = ZPROP_INVAL;
4050a48a24eStimh 	zfs_prop_t fprop;
406990b4856Slling 	nvlist_t *proplist;
4070a48a24eStimh 	const char *normnm;
4080a48a24eStimh 	char *strval;
409990b4856Slling 
410990b4856Slling 	if (*props == NULL &&
411990b4856Slling 	    nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
412990b4856Slling 		(void) fprintf(stderr,
413990b4856Slling 		    gettext("internal error: out of memory\n"));
414990b4856Slling 		return (1);
415990b4856Slling 	}
416990b4856Slling 
417990b4856Slling 	proplist = *props;
418990b4856Slling 
4190a48a24eStimh 	if (poolprop) {
42057221772SChristopher Siden 		const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
42157221772SChristopher Siden 
422ad135b5dSChristopher Siden 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
423ad135b5dSChristopher Siden 		    !zpool_prop_feature(propname)) {
424990b4856Slling 			(void) fprintf(stderr, gettext("property '%s' is "
425990b4856Slling 			    "not a valid pool property\n"), propname);
426990b4856Slling 			return (2);
427990b4856Slling 		}
42857221772SChristopher Siden 
42957221772SChristopher Siden 		/*
43057221772SChristopher Siden 		 * feature@ properties and version should not be specified
43157221772SChristopher Siden 		 * at the same time.
43257221772SChristopher Siden 		 */
43357221772SChristopher Siden 		if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) &&
43457221772SChristopher Siden 		    nvlist_exists(proplist, vname)) ||
43557221772SChristopher Siden 		    (prop == ZPOOL_PROP_VERSION &&
43657221772SChristopher Siden 		    prop_list_contains_feature(proplist))) {
43757221772SChristopher Siden 			(void) fprintf(stderr, gettext("'feature@' and "
43857221772SChristopher Siden 			    "'version' properties cannot be specified "
43957221772SChristopher Siden 			    "together\n"));
44057221772SChristopher Siden 			return (2);
44157221772SChristopher Siden 		}
44257221772SChristopher Siden 
44357221772SChristopher Siden 
444ad135b5dSChristopher Siden 		if (zpool_prop_feature(propname))
445ad135b5dSChristopher Siden 			normnm = propname;
446ad135b5dSChristopher Siden 		else
4470a48a24eStimh 			normnm = zpool_prop_to_name(prop);
4480a48a24eStimh 	} else {
44914843421SMatthew Ahrens 		if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
4500a48a24eStimh 			normnm = zfs_prop_to_name(fprop);
45114843421SMatthew Ahrens 		} else {
45214843421SMatthew Ahrens 			normnm = propname;
45314843421SMatthew Ahrens 		}
4540a48a24eStimh 	}
455990b4856Slling 
4560a48a24eStimh 	if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
4570a48a24eStimh 	    prop != ZPOOL_PROP_CACHEFILE) {
458990b4856Slling 		(void) fprintf(stderr, gettext("property '%s' "
459990b4856Slling 		    "specified multiple times\n"), propname);
460990b4856Slling 		return (2);
461990b4856Slling 	}
462990b4856Slling 
4630a48a24eStimh 	if (nvlist_add_string(proplist, normnm, propval) != 0) {
464990b4856Slling 		(void) fprintf(stderr, gettext("internal "
465990b4856Slling 		    "error: out of memory\n"));
466990b4856Slling 		return (1);
467990b4856Slling 	}
468990b4856Slling 
469990b4856Slling 	return (0);
470990b4856Slling }
471990b4856Slling 
472990b4856Slling /*
473fa9e4066Sahrens  * zpool add [-fn] <pool> <vdev> ...
474fa9e4066Sahrens  *
475fa9e4066Sahrens  *	-f	Force addition of devices, even if they appear in use
476fa9e4066Sahrens  *	-n	Do not add the devices, but display the resulting layout if
477fa9e4066Sahrens  *		they were to be added.
478fa9e4066Sahrens  *
479fa9e4066Sahrens  * Adds the given vdevs to 'pool'.  As with create, the bulk of this work is
480fa9e4066Sahrens  * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
481fa9e4066Sahrens  * libzfs.
482fa9e4066Sahrens  */
483fa9e4066Sahrens int
484fa9e4066Sahrens zpool_do_add(int argc, char **argv)
485fa9e4066Sahrens {
48699653d4eSeschrock 	boolean_t force = B_FALSE;
48799653d4eSeschrock 	boolean_t dryrun = B_FALSE;
488fa9e4066Sahrens 	int c;
489fa9e4066Sahrens 	nvlist_t *nvroot;
490fa9e4066Sahrens 	char *poolname;
491fa9e4066Sahrens 	int ret;
492fa9e4066Sahrens 	zpool_handle_t *zhp;
493fa9e4066Sahrens 	nvlist_t *config;
494fa9e4066Sahrens 
495fa9e4066Sahrens 	/* check options */
496fa9e4066Sahrens 	while ((c = getopt(argc, argv, "fn")) != -1) {
497fa9e4066Sahrens 		switch (c) {
498fa9e4066Sahrens 		case 'f':
49999653d4eSeschrock 			force = B_TRUE;
500fa9e4066Sahrens 			break;
501fa9e4066Sahrens 		case 'n':
50299653d4eSeschrock 			dryrun = B_TRUE;
503fa9e4066Sahrens 			break;
504fa9e4066Sahrens 		case '?':
505fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
506fa9e4066Sahrens 			    optopt);
50799653d4eSeschrock 			usage(B_FALSE);
508fa9e4066Sahrens 		}
509fa9e4066Sahrens 	}
510fa9e4066Sahrens 
511fa9e4066Sahrens 	argc -= optind;
512fa9e4066Sahrens 	argv += optind;
513fa9e4066Sahrens 
514fa9e4066Sahrens 	/* get pool name and check number of arguments */
515fa9e4066Sahrens 	if (argc < 1) {
516fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
51799653d4eSeschrock 		usage(B_FALSE);
518fa9e4066Sahrens 	}
519fa9e4066Sahrens 	if (argc < 2) {
520fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing vdev specification\n"));
52199653d4eSeschrock 		usage(B_FALSE);
522fa9e4066Sahrens 	}
523fa9e4066Sahrens 
524fa9e4066Sahrens 	poolname = argv[0];
525fa9e4066Sahrens 
526fa9e4066Sahrens 	argc--;
527fa9e4066Sahrens 	argv++;
528fa9e4066Sahrens 
52999653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
530fa9e4066Sahrens 		return (1);
531fa9e4066Sahrens 
532088e9d47Seschrock 	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
533fa9e4066Sahrens 		(void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
534fa9e4066Sahrens 		    poolname);
535fa9e4066Sahrens 		zpool_close(zhp);
536fa9e4066Sahrens 		return (1);
537fa9e4066Sahrens 	}
538fa9e4066Sahrens 
539fa9e4066Sahrens 	/* pass off to get_vdev_spec for processing */
540705040edSEric Taylor 	nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
541705040edSEric Taylor 	    argc, argv);
542fa9e4066Sahrens 	if (nvroot == NULL) {
543fa9e4066Sahrens 		zpool_close(zhp);
544fa9e4066Sahrens 		return (1);
545fa9e4066Sahrens 	}
546fa9e4066Sahrens 
547fa9e4066Sahrens 	if (dryrun) {
548fa9e4066Sahrens 		nvlist_t *poolnvroot;
549fa9e4066Sahrens 
550fa9e4066Sahrens 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
551fa9e4066Sahrens 		    &poolnvroot) == 0);
552fa9e4066Sahrens 
553fa9e4066Sahrens 		(void) printf(gettext("would update '%s' to the following "
554fa9e4066Sahrens 		    "configuration:\n"), zpool_get_name(zhp));
555fa9e4066Sahrens 
5568654d025Sperrin 		/* print original main pool and new tree */
5578654d025Sperrin 		print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
5588654d025Sperrin 		print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
5598654d025Sperrin 
5608654d025Sperrin 		/* Do the same for the logs */
5618654d025Sperrin 		if (num_logs(poolnvroot) > 0) {
5628654d025Sperrin 			print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
5638654d025Sperrin 			print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
5648654d025Sperrin 		} else if (num_logs(nvroot) > 0) {
5658654d025Sperrin 			print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
5668654d025Sperrin 		}
567fa9e4066Sahrens 
568fa9e4066Sahrens 		ret = 0;
569fa9e4066Sahrens 	} else {
570fa9e4066Sahrens 		ret = (zpool_add(zhp, nvroot) != 0);
571fa9e4066Sahrens 	}
572fa9e4066Sahrens 
57399653d4eSeschrock 	nvlist_free(nvroot);
57499653d4eSeschrock 	zpool_close(zhp);
57599653d4eSeschrock 
57699653d4eSeschrock 	return (ret);
57799653d4eSeschrock }
57899653d4eSeschrock 
57999653d4eSeschrock /*
580fa94a07fSbrendan  * zpool remove  <pool> <vdev> ...
58199653d4eSeschrock  *
5823f9d6ad7SLin Ling  * Removes the given vdev from the pool.  Currently, this supports removing
5833f9d6ad7SLin Ling  * spares, cache, and log devices from the pool.
58499653d4eSeschrock  */
58599653d4eSeschrock int
58699653d4eSeschrock zpool_do_remove(int argc, char **argv)
58799653d4eSeschrock {
58899653d4eSeschrock 	char *poolname;
589fa94a07fSbrendan 	int i, ret = 0;
59099653d4eSeschrock 	zpool_handle_t *zhp;
59199653d4eSeschrock 
59299653d4eSeschrock 	argc--;
59399653d4eSeschrock 	argv++;
59499653d4eSeschrock 
59599653d4eSeschrock 	/* get pool name and check number of arguments */
59699653d4eSeschrock 	if (argc < 1) {
59799653d4eSeschrock 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
59899653d4eSeschrock 		usage(B_FALSE);
59999653d4eSeschrock 	}
60099653d4eSeschrock 	if (argc < 2) {
60199653d4eSeschrock 		(void) fprintf(stderr, gettext("missing device\n"));
60299653d4eSeschrock 		usage(B_FALSE);
60399653d4eSeschrock 	}
60499653d4eSeschrock 
60599653d4eSeschrock 	poolname = argv[0];
60699653d4eSeschrock 
60799653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
60899653d4eSeschrock 		return (1);
60999653d4eSeschrock 
610fa94a07fSbrendan 	for (i = 1; i < argc; i++) {
611fa94a07fSbrendan 		if (zpool_vdev_remove(zhp, argv[i]) != 0)
612fa94a07fSbrendan 			ret = 1;
613fa94a07fSbrendan 	}
61499653d4eSeschrock 
615fa9e4066Sahrens 	return (ret);
616fa9e4066Sahrens }
617fa9e4066Sahrens 
618fa9e4066Sahrens /*
619ad135b5dSChristopher Siden  * zpool create [-fnd] [-o property=value] ...
6200a48a24eStimh  *		[-O file-system-property=value] ...
6210a48a24eStimh  *		[-R root] [-m mountpoint] <pool> <dev> ...
622fa9e4066Sahrens  *
623fa9e4066Sahrens  *	-f	Force creation, even if devices appear in use
624fa9e4066Sahrens  *	-n	Do not create the pool, but display the resulting layout if it
625fa9e4066Sahrens  *		were to be created.
626fa9e4066Sahrens  *      -R	Create a pool under an alternate root
627fa9e4066Sahrens  *      -m	Set default mountpoint for the root dataset.  By default it's
628fa9e4066Sahrens  *		'/<pool>'
629990b4856Slling  *	-o	Set property=value.
630ad135b5dSChristopher Siden  *	-d	Don't automatically enable all supported pool features
631ad135b5dSChristopher Siden  *		(individual features can be enabled with -o).
6320a48a24eStimh  *	-O	Set fsproperty=value in the pool's root file system
633fa9e4066Sahrens  *
634b1b8ab34Slling  * Creates the named pool according to the given vdev specification.  The
635fa9e4066Sahrens  * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c.  Once
636fa9e4066Sahrens  * we get the nvlist back from get_vdev_spec(), we either print out the contents
637fa9e4066Sahrens  * (if '-n' was specified), or pass it to libzfs to do the creation.
638fa9e4066Sahrens  */
639fa9e4066Sahrens int
640fa9e4066Sahrens zpool_do_create(int argc, char **argv)
641fa9e4066Sahrens {
64299653d4eSeschrock 	boolean_t force = B_FALSE;
64399653d4eSeschrock 	boolean_t dryrun = B_FALSE;
644ad135b5dSChristopher Siden 	boolean_t enable_all_pool_feat = B_TRUE;
645fa9e4066Sahrens 	int c;
646990b4856Slling 	nvlist_t *nvroot = NULL;
647fa9e4066Sahrens 	char *poolname;
648990b4856Slling 	int ret = 1;
649fa9e4066Sahrens 	char *altroot = NULL;
650fa9e4066Sahrens 	char *mountpoint = NULL;
6510a48a24eStimh 	nvlist_t *fsprops = NULL;
652990b4856Slling 	nvlist_t *props = NULL;
6532f8aaab3Seschrock 	char *propval;
654fa9e4066Sahrens 
655fa9e4066Sahrens 	/* check options */
656ad135b5dSChristopher Siden 	while ((c = getopt(argc, argv, ":fndR:m:o:O:")) != -1) {
657fa9e4066Sahrens 		switch (c) {
658fa9e4066Sahrens 		case 'f':
65999653d4eSeschrock 			force = B_TRUE;
660fa9e4066Sahrens 			break;
661fa9e4066Sahrens 		case 'n':
66299653d4eSeschrock 			dryrun = B_TRUE;
663fa9e4066Sahrens 			break;
664ad135b5dSChristopher Siden 		case 'd':
665ad135b5dSChristopher Siden 			enable_all_pool_feat = B_FALSE;
666ad135b5dSChristopher Siden 			break;
667fa9e4066Sahrens 		case 'R':
668fa9e4066Sahrens 			altroot = optarg;
669990b4856Slling 			if (add_prop_list(zpool_prop_to_name(
6700a48a24eStimh 			    ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
671990b4856Slling 				goto errout;
6722f8aaab3Seschrock 			if (nvlist_lookup_string(props,
6732f8aaab3Seschrock 			    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
6742f8aaab3Seschrock 			    &propval) == 0)
6752f8aaab3Seschrock 				break;
676990b4856Slling 			if (add_prop_list(zpool_prop_to_name(
6770a48a24eStimh 			    ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
678990b4856Slling 				goto errout;
679fa9e4066Sahrens 			break;
680fa9e4066Sahrens 		case 'm':
6818b713775SWill Andrews 			/* Equivalent to -O mountpoint=optarg */
682fa9e4066Sahrens 			mountpoint = optarg;
683fa9e4066Sahrens 			break;
684990b4856Slling 		case 'o':
685990b4856Slling 			if ((propval = strchr(optarg, '=')) == NULL) {
686990b4856Slling 				(void) fprintf(stderr, gettext("missing "
687990b4856Slling 				    "'=' for -o option\n"));
688990b4856Slling 				goto errout;
689990b4856Slling 			}
690990b4856Slling 			*propval = '\0';
691990b4856Slling 			propval++;
692990b4856Slling 
6930a48a24eStimh 			if (add_prop_list(optarg, propval, &props, B_TRUE))
6940a48a24eStimh 				goto errout;
695ad135b5dSChristopher Siden 
696ad135b5dSChristopher Siden 			/*
697ad135b5dSChristopher Siden 			 * If the user is creating a pool that doesn't support
698ad135b5dSChristopher Siden 			 * feature flags, don't enable any features.
699ad135b5dSChristopher Siden 			 */
700ad135b5dSChristopher Siden 			if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
701ad135b5dSChristopher Siden 				char *end;
702ad135b5dSChristopher Siden 				u_longlong_t ver;
703ad135b5dSChristopher Siden 
704ad135b5dSChristopher Siden 				ver = strtoull(propval, &end, 10);
705ad135b5dSChristopher Siden 				if (*end == '\0' &&
706ad135b5dSChristopher Siden 				    ver < SPA_VERSION_FEATURES) {
707ad135b5dSChristopher Siden 					enable_all_pool_feat = B_FALSE;
708ad135b5dSChristopher Siden 				}
709ad135b5dSChristopher Siden 			}
7100a48a24eStimh 			break;
7110a48a24eStimh 		case 'O':
7120a48a24eStimh 			if ((propval = strchr(optarg, '=')) == NULL) {
7130a48a24eStimh 				(void) fprintf(stderr, gettext("missing "
7140a48a24eStimh 				    "'=' for -O option\n"));
7150a48a24eStimh 				goto errout;
7160a48a24eStimh 			}
7170a48a24eStimh 			*propval = '\0';
7180a48a24eStimh 			propval++;
7190a48a24eStimh 
7208b713775SWill Andrews 			/*
7218b713775SWill Andrews 			 * Mountpoints are checked and then added later.
7228b713775SWill Andrews 			 * Uniquely among properties, they can be specified
7238b713775SWill Andrews 			 * more than once, to avoid conflict with -m.
7248b713775SWill Andrews 			 */
7258b713775SWill Andrews 			if (0 == strcmp(optarg,
7268b713775SWill Andrews 			    zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
7278b713775SWill Andrews 				mountpoint = propval;
7288b713775SWill Andrews 			} else if (add_prop_list(optarg, propval, &fsprops,
7298b713775SWill Andrews 			    B_FALSE)) {
730990b4856Slling 				goto errout;
7318b713775SWill Andrews 			}
732990b4856Slling 			break;
733fa9e4066Sahrens 		case ':':
734fa9e4066Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
735fa9e4066Sahrens 			    "'%c' option\n"), optopt);
736990b4856Slling 			goto badusage;
737fa9e4066Sahrens 		case '?':
738fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
739fa9e4066Sahrens 			    optopt);
740990b4856Slling 			goto badusage;
741fa9e4066Sahrens 		}
742fa9e4066Sahrens 	}
743fa9e4066Sahrens 
744fa9e4066Sahrens 	argc -= optind;
745fa9e4066Sahrens 	argv += optind;
746fa9e4066Sahrens 
747fa9e4066Sahrens 	/* get pool name and check number of arguments */
748fa9e4066Sahrens 	if (argc < 1) {
749fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
750990b4856Slling 		goto badusage;
751fa9e4066Sahrens 	}
752fa9e4066Sahrens 	if (argc < 2) {
753fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing vdev specification\n"));
754990b4856Slling 		goto badusage;
755fa9e4066Sahrens 	}
756fa9e4066Sahrens 
757fa9e4066Sahrens 	poolname = argv[0];
758fa9e4066Sahrens 
759fa9e4066Sahrens 	/*
760fa9e4066Sahrens 	 * As a special case, check for use of '/' in the name, and direct the
761fa9e4066Sahrens 	 * user to use 'zfs create' instead.
762fa9e4066Sahrens 	 */
763fa9e4066Sahrens 	if (strchr(poolname, '/') != NULL) {
764fa9e4066Sahrens 		(void) fprintf(stderr, gettext("cannot create '%s': invalid "
765fa9e4066Sahrens 		    "character '/' in pool name\n"), poolname);
766fa9e4066Sahrens 		(void) fprintf(stderr, gettext("use 'zfs create' to "
767fa9e4066Sahrens 		    "create a dataset\n"));
768990b4856Slling 		goto errout;
769fa9e4066Sahrens 	}
770fa9e4066Sahrens 
771fa9e4066Sahrens 	/* pass off to get_vdev_spec for bulk processing */
772705040edSEric Taylor 	nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
773705040edSEric Taylor 	    argc - 1, argv + 1);
774fa9e4066Sahrens 	if (nvroot == NULL)
7750a48a24eStimh 		goto errout;
776fa9e4066Sahrens 
77799653d4eSeschrock 	/* make_root_vdev() allows 0 toplevel children if there are spares */
778b7b97454Sperrin 	if (!zfs_allocatable_devs(nvroot)) {
77999653d4eSeschrock 		(void) fprintf(stderr, gettext("invalid vdev "
78099653d4eSeschrock 		    "specification: at least one toplevel vdev must be "
78199653d4eSeschrock 		    "specified\n"));
782990b4856Slling 		goto errout;
78399653d4eSeschrock 	}
78499653d4eSeschrock 
785fa9e4066Sahrens 	if (altroot != NULL && altroot[0] != '/') {
786fa9e4066Sahrens 		(void) fprintf(stderr, gettext("invalid alternate root '%s': "
787e9dbad6fSeschrock 		    "must be an absolute path\n"), altroot);
788990b4856Slling 		goto errout;
789fa9e4066Sahrens 	}
790fa9e4066Sahrens 
791fa9e4066Sahrens 	/*
792fa9e4066Sahrens 	 * Check the validity of the mountpoint and direct the user to use the
793fa9e4066Sahrens 	 * '-m' mountpoint option if it looks like its in use.
794fa9e4066Sahrens 	 */
795fa9e4066Sahrens 	if (mountpoint == NULL ||
796fa9e4066Sahrens 	    (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
797fa9e4066Sahrens 	    strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
798fa9e4066Sahrens 		char buf[MAXPATHLEN];
79911022c7cStimh 		DIR *dirp;
800fa9e4066Sahrens 
801fa9e4066Sahrens 		if (mountpoint && mountpoint[0] != '/') {
802fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid mountpoint "
803fa9e4066Sahrens 			    "'%s': must be an absolute path, 'legacy', or "
804fa9e4066Sahrens 			    "'none'\n"), mountpoint);
805990b4856Slling 			goto errout;
806fa9e4066Sahrens 		}
807fa9e4066Sahrens 
808fa9e4066Sahrens 		if (mountpoint == NULL) {
809fa9e4066Sahrens 			if (altroot != NULL)
810fa9e4066Sahrens 				(void) snprintf(buf, sizeof (buf), "%s/%s",
811fa9e4066Sahrens 				    altroot, poolname);
812fa9e4066Sahrens 			else
813fa9e4066Sahrens 				(void) snprintf(buf, sizeof (buf), "/%s",
814fa9e4066Sahrens 				    poolname);
815fa9e4066Sahrens 		} else {
816fa9e4066Sahrens 			if (altroot != NULL)
817fa9e4066Sahrens 				(void) snprintf(buf, sizeof (buf), "%s%s",
818fa9e4066Sahrens 				    altroot, mountpoint);
819fa9e4066Sahrens 			else
820fa9e4066Sahrens 				(void) snprintf(buf, sizeof (buf), "%s",
821fa9e4066Sahrens 				    mountpoint);
822fa9e4066Sahrens 		}
823fa9e4066Sahrens 
82411022c7cStimh 		if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
82511022c7cStimh 			(void) fprintf(stderr, gettext("mountpoint '%s' : "
82611022c7cStimh 			    "%s\n"), buf, strerror(errno));
827fa9e4066Sahrens 			(void) fprintf(stderr, gettext("use '-m' "
828fa9e4066Sahrens 			    "option to provide a different default\n"));
829990b4856Slling 			goto errout;
83011022c7cStimh 		} else if (dirp) {
83111022c7cStimh 			int count = 0;
83211022c7cStimh 
83311022c7cStimh 			while (count < 3 && readdir(dirp) != NULL)
83411022c7cStimh 				count++;
83511022c7cStimh 			(void) closedir(dirp);
83611022c7cStimh 
83711022c7cStimh 			if (count > 2) {
83811022c7cStimh 				(void) fprintf(stderr, gettext("mountpoint "
83911022c7cStimh 				    "'%s' exists and is not empty\n"), buf);
84011022c7cStimh 				(void) fprintf(stderr, gettext("use '-m' "
84111022c7cStimh 				    "option to provide a "
84211022c7cStimh 				    "different default\n"));
84311022c7cStimh 				goto errout;
84411022c7cStimh 			}
845fa9e4066Sahrens 		}
846fa9e4066Sahrens 	}
847fa9e4066Sahrens 
8488b713775SWill Andrews 	/*
8498b713775SWill Andrews 	 * Now that the mountpoint's validity has been checked, ensure that
8508b713775SWill Andrews 	 * the property is set appropriately prior to creating the pool.
8518b713775SWill Andrews 	 */
8528b713775SWill Andrews 	if (mountpoint != NULL) {
8538b713775SWill Andrews 		ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
8548b713775SWill Andrews 		    mountpoint, &fsprops, B_FALSE);
8558b713775SWill Andrews 		if (ret != 0)
8568b713775SWill Andrews 			goto errout;
8578b713775SWill Andrews 	}
8588b713775SWill Andrews 
8598b713775SWill Andrews 	ret = 1;
860fa9e4066Sahrens 	if (dryrun) {
861fa9e4066Sahrens 		/*
862fa9e4066Sahrens 		 * For a dry run invocation, print out a basic message and run
863fa9e4066Sahrens 		 * through all the vdevs in the list and print out in an
864fa9e4066Sahrens 		 * appropriate hierarchy.
865fa9e4066Sahrens 		 */
866fa9e4066Sahrens 		(void) printf(gettext("would create '%s' with the "
867fa9e4066Sahrens 		    "following layout:\n\n"), poolname);
868fa9e4066Sahrens 
8698654d025Sperrin 		print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
8708654d025Sperrin 		if (num_logs(nvroot) > 0)
8718654d025Sperrin 			print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
872fa9e4066Sahrens 
873fa9e4066Sahrens 		ret = 0;
874fa9e4066Sahrens 	} else {
875fa9e4066Sahrens 		/*
876fa9e4066Sahrens 		 * Hand off to libzfs.
877fa9e4066Sahrens 		 */
878ad135b5dSChristopher Siden 		if (enable_all_pool_feat) {
8792acef22dSMatthew Ahrens 			spa_feature_t i;
880ad135b5dSChristopher Siden 			for (i = 0; i < SPA_FEATURES; i++) {
881ad135b5dSChristopher Siden 				char propname[MAXPATHLEN];
882ad135b5dSChristopher Siden 				zfeature_info_t *feat = &spa_feature_table[i];
883ad135b5dSChristopher Siden 
884ad135b5dSChristopher Siden 				(void) snprintf(propname, sizeof (propname),
885ad135b5dSChristopher Siden 				    "feature@%s", feat->fi_uname);
886ad135b5dSChristopher Siden 
887ad135b5dSChristopher Siden 				/*
888ad135b5dSChristopher Siden 				 * Skip feature if user specified it manually
889ad135b5dSChristopher Siden 				 * on the command line.
890ad135b5dSChristopher Siden 				 */
891ad135b5dSChristopher Siden 				if (nvlist_exists(props, propname))
892ad135b5dSChristopher Siden 					continue;
893ad135b5dSChristopher Siden 
8948b713775SWill Andrews 				ret = add_prop_list(propname,
8958b713775SWill Andrews 				    ZFS_FEATURE_ENABLED, &props, B_TRUE);
8968b713775SWill Andrews 				if (ret != 0)
897ad135b5dSChristopher Siden 					goto errout;
898ad135b5dSChristopher Siden 			}
899ad135b5dSChristopher Siden 		}
9008b713775SWill Andrews 
9018b713775SWill Andrews 		ret = 1;
9020a48a24eStimh 		if (zpool_create(g_zfs, poolname,
9030a48a24eStimh 		    nvroot, props, fsprops) == 0) {
90499653d4eSeschrock 			zfs_handle_t *pool = zfs_open(g_zfs, poolname,
905fa9e4066Sahrens 			    ZFS_TYPE_FILESYSTEM);
906fa9e4066Sahrens 			if (pool != NULL) {
907fa9e4066Sahrens 				if (zfs_mount(pool, NULL, 0) == 0)
908da6c28aaSamw 					ret = zfs_shareall(pool);
909fa9e4066Sahrens 				zfs_close(pool);
910fa9e4066Sahrens 			}
91199653d4eSeschrock 		} else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
91299653d4eSeschrock 			(void) fprintf(stderr, gettext("pool name may have "
91399653d4eSeschrock 			    "been omitted\n"));
914fa9e4066Sahrens 		}
915fa9e4066Sahrens 	}
916fa9e4066Sahrens 
917990b4856Slling errout:
918fa9e4066Sahrens 	nvlist_free(nvroot);
9190a48a24eStimh 	nvlist_free(fsprops);
920990b4856Slling 	nvlist_free(props);
921fa9e4066Sahrens 	return (ret);
922990b4856Slling badusage:
9230a48a24eStimh 	nvlist_free(fsprops);
924990b4856Slling 	nvlist_free(props);
925990b4856Slling 	usage(B_FALSE);
926990b4856Slling 	return (2);
927fa9e4066Sahrens }
928fa9e4066Sahrens 
929fa9e4066Sahrens /*
930fa9e4066Sahrens  * zpool destroy <pool>
931fa9e4066Sahrens  *
932fa9e4066Sahrens  * 	-f	Forcefully unmount any datasets
933fa9e4066Sahrens  *
934fa9e4066Sahrens  * Destroy the given pool.  Automatically unmounts any datasets in the pool.
935fa9e4066Sahrens  */
936fa9e4066Sahrens int
937fa9e4066Sahrens zpool_do_destroy(int argc, char **argv)
938fa9e4066Sahrens {
93999653d4eSeschrock 	boolean_t force = B_FALSE;
940fa9e4066Sahrens 	int c;
941fa9e4066Sahrens 	char *pool;
942fa9e4066Sahrens 	zpool_handle_t *zhp;
943fa9e4066Sahrens 	int ret;
944fa9e4066Sahrens 
945fa9e4066Sahrens 	/* check options */
946fa9e4066Sahrens 	while ((c = getopt(argc, argv, "f")) != -1) {
947fa9e4066Sahrens 		switch (c) {
948fa9e4066Sahrens 		case 'f':
94999653d4eSeschrock 			force = B_TRUE;
950fa9e4066Sahrens 			break;
951fa9e4066Sahrens 		case '?':
952fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
953fa9e4066Sahrens 			    optopt);
95499653d4eSeschrock 			usage(B_FALSE);
955fa9e4066Sahrens 		}
956fa9e4066Sahrens 	}
957fa9e4066Sahrens 
958fa9e4066Sahrens 	argc -= optind;
959fa9e4066Sahrens 	argv += optind;
960fa9e4066Sahrens 
961fa9e4066Sahrens 	/* check arguments */
962fa9e4066Sahrens 	if (argc < 1) {
963fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool argument\n"));
96499653d4eSeschrock 		usage(B_FALSE);
965fa9e4066Sahrens 	}
966fa9e4066Sahrens 	if (argc > 1) {
967fa9e4066Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
96899653d4eSeschrock 		usage(B_FALSE);
969fa9e4066Sahrens 	}
970fa9e4066Sahrens 
971fa9e4066Sahrens 	pool = argv[0];
972fa9e4066Sahrens 
97399653d4eSeschrock 	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
974fa9e4066Sahrens 		/*
975fa9e4066Sahrens 		 * As a special case, check for use of '/' in the name, and
976fa9e4066Sahrens 		 * direct the user to use 'zfs destroy' instead.
977fa9e4066Sahrens 		 */
978fa9e4066Sahrens 		if (strchr(pool, '/') != NULL)
979fa9e4066Sahrens 			(void) fprintf(stderr, gettext("use 'zfs destroy' to "
980fa9e4066Sahrens 			    "destroy a dataset\n"));
981fa9e4066Sahrens 		return (1);
982fa9e4066Sahrens 	}
983fa9e4066Sahrens 
984f3861e1aSahl 	if (zpool_disable_datasets(zhp, force) != 0) {
985fa9e4066Sahrens 		(void) fprintf(stderr, gettext("could not destroy '%s': "
986fa9e4066Sahrens 		    "could not unmount datasets\n"), zpool_get_name(zhp));
987fa9e4066Sahrens 		return (1);
988fa9e4066Sahrens 	}
989fa9e4066Sahrens 
9904445fffbSMatthew Ahrens 	/* The history must be logged as part of the export */
9914445fffbSMatthew Ahrens 	log_history = B_FALSE;
9924445fffbSMatthew Ahrens 
9934445fffbSMatthew Ahrens 	ret = (zpool_destroy(zhp, history_str) != 0);
994fa9e4066Sahrens 
995fa9e4066Sahrens 	zpool_close(zhp);
996fa9e4066Sahrens 
997fa9e4066Sahrens 	return (ret);
998fa9e4066Sahrens }
999fa9e4066Sahrens 
1000fa9e4066Sahrens /*
1001fa9e4066Sahrens  * zpool export [-f] <pool> ...
1002fa9e4066Sahrens  *
1003fa9e4066Sahrens  *	-f	Forcefully unmount datasets
1004fa9e4066Sahrens  *
1005b1b8ab34Slling  * Export the given pools.  By default, the command will attempt to cleanly
1006fa9e4066Sahrens  * unmount any active datasets within the pool.  If the '-f' flag is specified,
1007fa9e4066Sahrens  * then the datasets will be forcefully unmounted.
1008fa9e4066Sahrens  */
1009fa9e4066Sahrens int
1010fa9e4066Sahrens zpool_do_export(int argc, char **argv)
1011fa9e4066Sahrens {
101299653d4eSeschrock 	boolean_t force = B_FALSE;
1013394ab0cbSGeorge Wilson 	boolean_t hardforce = B_FALSE;
1014fa9e4066Sahrens 	int c;
1015fa9e4066Sahrens 	zpool_handle_t *zhp;
1016fa9e4066Sahrens 	int ret;
1017fa9e4066Sahrens 	int i;
1018fa9e4066Sahrens 
1019fa9e4066Sahrens 	/* check options */
1020394ab0cbSGeorge Wilson 	while ((c = getopt(argc, argv, "fF")) != -1) {
1021fa9e4066Sahrens 		switch (c) {
1022fa9e4066Sahrens 		case 'f':
102399653d4eSeschrock 			force = B_TRUE;
1024fa9e4066Sahrens 			break;
1025394ab0cbSGeorge Wilson 		case 'F':
1026394ab0cbSGeorge Wilson 			hardforce = B_TRUE;
1027394ab0cbSGeorge Wilson 			break;
1028fa9e4066Sahrens 		case '?':
1029fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1030fa9e4066Sahrens 			    optopt);
103199653d4eSeschrock 			usage(B_FALSE);
1032fa9e4066Sahrens 		}
1033fa9e4066Sahrens 	}
1034fa9e4066Sahrens 
1035fa9e4066Sahrens 	argc -= optind;
1036fa9e4066Sahrens 	argv += optind;
1037fa9e4066Sahrens 
1038fa9e4066Sahrens 	/* check arguments */
1039fa9e4066Sahrens 	if (argc < 1) {
1040fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool argument\n"));
104199653d4eSeschrock 		usage(B_FALSE);
1042fa9e4066Sahrens 	}
1043fa9e4066Sahrens 
1044fa9e4066Sahrens 	ret = 0;
1045fa9e4066Sahrens 	for (i = 0; i < argc; i++) {
104699653d4eSeschrock 		if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1047fa9e4066Sahrens 			ret = 1;
1048fa9e4066Sahrens 			continue;
1049fa9e4066Sahrens 		}
1050fa9e4066Sahrens 
1051f3861e1aSahl 		if (zpool_disable_datasets(zhp, force) != 0) {
1052fa9e4066Sahrens 			ret = 1;
1053fa9e4066Sahrens 			zpool_close(zhp);
1054fa9e4066Sahrens 			continue;
1055fa9e4066Sahrens 		}
1056fa9e4066Sahrens 
10574445fffbSMatthew Ahrens 		/* The history must be logged as part of the export */
10584445fffbSMatthew Ahrens 		log_history = B_FALSE;
10594445fffbSMatthew Ahrens 
1060394ab0cbSGeorge Wilson 		if (hardforce) {
10614445fffbSMatthew Ahrens 			if (zpool_export_force(zhp, history_str) != 0)
1062fa9e4066Sahrens 				ret = 1;
10634445fffbSMatthew Ahrens 		} else if (zpool_export(zhp, force, history_str) != 0) {
1064394ab0cbSGeorge Wilson 			ret = 1;
1065394ab0cbSGeorge Wilson 		}
1066fa9e4066Sahrens 
1067fa9e4066Sahrens 		zpool_close(zhp);
1068fa9e4066Sahrens 	}
1069fa9e4066Sahrens 
1070fa9e4066Sahrens 	return (ret);
1071fa9e4066Sahrens }
1072fa9e4066Sahrens 
1073fa9e4066Sahrens /*
1074fa9e4066Sahrens  * Given a vdev configuration, determine the maximum width needed for the device
1075fa9e4066Sahrens  * name column.
1076fa9e4066Sahrens  */
1077fa9e4066Sahrens static int
1078c67d9675Seschrock max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1079fa9e4066Sahrens {
108088ecc943SGeorge Wilson 	char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1081fa9e4066Sahrens 	nvlist_t **child;
1082fa9e4066Sahrens 	uint_t c, children;
1083fa9e4066Sahrens 	int ret;
1084fa9e4066Sahrens 
1085fa9e4066Sahrens 	if (strlen(name) + depth > max)
1086fa9e4066Sahrens 		max = strlen(name) + depth;
1087fa9e4066Sahrens 
1088afefbcddSeschrock 	free(name);
1089afefbcddSeschrock 
109099653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
109199653d4eSeschrock 	    &child, &children) == 0) {
1092fa9e4066Sahrens 		for (c = 0; c < children; c++)
109399653d4eSeschrock 			if ((ret = max_width(zhp, child[c], depth + 2,
109499653d4eSeschrock 			    max)) > max)
1095fa9e4066Sahrens 				max = ret;
109699653d4eSeschrock 	}
109799653d4eSeschrock 
1098fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1099fa94a07fSbrendan 	    &child, &children) == 0) {
1100fa94a07fSbrendan 		for (c = 0; c < children; c++)
1101fa94a07fSbrendan 			if ((ret = max_width(zhp, child[c], depth + 2,
1102fa94a07fSbrendan 			    max)) > max)
1103fa94a07fSbrendan 				max = ret;
1104fa94a07fSbrendan 	}
1105fa94a07fSbrendan 
110699653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
110799653d4eSeschrock 	    &child, &children) == 0) {
110899653d4eSeschrock 		for (c = 0; c < children; c++)
110999653d4eSeschrock 			if ((ret = max_width(zhp, child[c], depth + 2,
111099653d4eSeschrock 			    max)) > max)
111199653d4eSeschrock 				max = ret;
111299653d4eSeschrock 	}
111399653d4eSeschrock 
1114fa9e4066Sahrens 
1115fa9e4066Sahrens 	return (max);
1116fa9e4066Sahrens }
1117fa9e4066Sahrens 
1118e6ca193dSGeorge Wilson typedef struct spare_cbdata {
1119e6ca193dSGeorge Wilson 	uint64_t	cb_guid;
1120e6ca193dSGeorge Wilson 	zpool_handle_t	*cb_zhp;
1121e6ca193dSGeorge Wilson } spare_cbdata_t;
1122e6ca193dSGeorge Wilson 
1123e6ca193dSGeorge Wilson static boolean_t
1124e6ca193dSGeorge Wilson find_vdev(nvlist_t *nv, uint64_t search)
1125e6ca193dSGeorge Wilson {
1126e6ca193dSGeorge Wilson 	uint64_t guid;
1127e6ca193dSGeorge Wilson 	nvlist_t **child;
1128e6ca193dSGeorge Wilson 	uint_t c, children;
1129e6ca193dSGeorge Wilson 
1130e6ca193dSGeorge Wilson 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1131e6ca193dSGeorge Wilson 	    search == guid)
1132e6ca193dSGeorge Wilson 		return (B_TRUE);
1133e6ca193dSGeorge Wilson 
1134e6ca193dSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1135e6ca193dSGeorge Wilson 	    &child, &children) == 0) {
1136e6ca193dSGeorge Wilson 		for (c = 0; c < children; c++)
1137e6ca193dSGeorge Wilson 			if (find_vdev(child[c], search))
1138e6ca193dSGeorge Wilson 				return (B_TRUE);
1139e6ca193dSGeorge Wilson 	}
1140e6ca193dSGeorge Wilson 
1141e6ca193dSGeorge Wilson 	return (B_FALSE);
1142e6ca193dSGeorge Wilson }
1143e6ca193dSGeorge Wilson 
1144e6ca193dSGeorge Wilson static int
1145e6ca193dSGeorge Wilson find_spare(zpool_handle_t *zhp, void *data)
1146e6ca193dSGeorge Wilson {
1147e6ca193dSGeorge Wilson 	spare_cbdata_t *cbp = data;
1148e6ca193dSGeorge Wilson 	nvlist_t *config, *nvroot;
1149e6ca193dSGeorge Wilson 
1150e6ca193dSGeorge Wilson 	config = zpool_get_config(zhp, NULL);
1151e6ca193dSGeorge Wilson 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1152e6ca193dSGeorge Wilson 	    &nvroot) == 0);
1153e6ca193dSGeorge Wilson 
1154e6ca193dSGeorge Wilson 	if (find_vdev(nvroot, cbp->cb_guid)) {
1155e6ca193dSGeorge Wilson 		cbp->cb_zhp = zhp;
1156e6ca193dSGeorge Wilson 		return (1);
1157e6ca193dSGeorge Wilson 	}
1158e6ca193dSGeorge Wilson 
1159e6ca193dSGeorge Wilson 	zpool_close(zhp);
1160e6ca193dSGeorge Wilson 	return (0);
1161e6ca193dSGeorge Wilson }
1162e6ca193dSGeorge Wilson 
1163e6ca193dSGeorge Wilson /*
1164e6ca193dSGeorge Wilson  * Print out configuration state as requested by status_callback.
1165e6ca193dSGeorge Wilson  */
1166e6ca193dSGeorge Wilson void
1167e6ca193dSGeorge Wilson print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1168e6ca193dSGeorge Wilson     int namewidth, int depth, boolean_t isspare)
1169e6ca193dSGeorge Wilson {
1170e6ca193dSGeorge Wilson 	nvlist_t **child;
1171e6ca193dSGeorge Wilson 	uint_t c, children;
11723f9d6ad7SLin Ling 	pool_scan_stat_t *ps = NULL;
1173e6ca193dSGeorge Wilson 	vdev_stat_t *vs;
11743f9d6ad7SLin Ling 	char rbuf[6], wbuf[6], cbuf[6];
1175e6ca193dSGeorge Wilson 	char *vname;
1176e6ca193dSGeorge Wilson 	uint64_t notpresent;
1177e6ca193dSGeorge Wilson 	spare_cbdata_t cb;
1178e6ca193dSGeorge Wilson 	char *state;
1179e6ca193dSGeorge Wilson 
1180e6ca193dSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1181e6ca193dSGeorge Wilson 	    &child, &children) != 0)
1182e6ca193dSGeorge Wilson 		children = 0;
1183e6ca193dSGeorge Wilson 
11843f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
11853f9d6ad7SLin Ling 	    (uint64_t **)&vs, &c) == 0);
11863f9d6ad7SLin Ling 
1187e6ca193dSGeorge Wilson 	state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1188e6ca193dSGeorge Wilson 	if (isspare) {
1189e6ca193dSGeorge Wilson 		/*
1190e6ca193dSGeorge Wilson 		 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1191e6ca193dSGeorge Wilson 		 * online drives.
1192e6ca193dSGeorge Wilson 		 */
1193e6ca193dSGeorge Wilson 		if (vs->vs_aux == VDEV_AUX_SPARED)
1194e6ca193dSGeorge Wilson 			state = "INUSE";
1195e6ca193dSGeorge Wilson 		else if (vs->vs_state == VDEV_STATE_HEALTHY)
1196e6ca193dSGeorge Wilson 			state = "AVAIL";
1197e6ca193dSGeorge Wilson 	}
1198e6ca193dSGeorge Wilson 
1199e6ca193dSGeorge Wilson 	(void) printf("\t%*s%-*s  %-8s", depth, "", namewidth - depth,
1200e6ca193dSGeorge Wilson 	    name, state);
1201e6ca193dSGeorge Wilson 
1202e6ca193dSGeorge Wilson 	if (!isspare) {
1203e6ca193dSGeorge Wilson 		zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1204e6ca193dSGeorge Wilson 		zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1205e6ca193dSGeorge Wilson 		zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1206e6ca193dSGeorge Wilson 		(void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1207e6ca193dSGeorge Wilson 	}
1208e6ca193dSGeorge Wilson 
1209e6ca193dSGeorge Wilson 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1210e6ca193dSGeorge Wilson 	    &notpresent) == 0) {
1211e6ca193dSGeorge Wilson 		char *path;
1212e6ca193dSGeorge Wilson 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1213e6ca193dSGeorge Wilson 		(void) printf("  was %s", path);
1214e6ca193dSGeorge Wilson 	} else if (vs->vs_aux != 0) {
1215e6ca193dSGeorge Wilson 		(void) printf("  ");
1216e6ca193dSGeorge Wilson 
1217e6ca193dSGeorge Wilson 		switch (vs->vs_aux) {
1218e6ca193dSGeorge Wilson 		case VDEV_AUX_OPEN_FAILED:
1219e6ca193dSGeorge Wilson 			(void) printf(gettext("cannot open"));
1220e6ca193dSGeorge Wilson 			break;
1221e6ca193dSGeorge Wilson 
1222e6ca193dSGeorge Wilson 		case VDEV_AUX_BAD_GUID_SUM:
1223e6ca193dSGeorge Wilson 			(void) printf(gettext("missing device"));
1224e6ca193dSGeorge Wilson 			break;
1225e6ca193dSGeorge Wilson 
1226e6ca193dSGeorge Wilson 		case VDEV_AUX_NO_REPLICAS:
1227e6ca193dSGeorge Wilson 			(void) printf(gettext("insufficient replicas"));
1228e6ca193dSGeorge Wilson 			break;
1229e6ca193dSGeorge Wilson 
1230e6ca193dSGeorge Wilson 		case VDEV_AUX_VERSION_NEWER:
1231e6ca193dSGeorge Wilson 			(void) printf(gettext("newer version"));
1232e6ca193dSGeorge Wilson 			break;
1233e6ca193dSGeorge Wilson 
1234ad135b5dSChristopher Siden 		case VDEV_AUX_UNSUP_FEAT:
1235ad135b5dSChristopher Siden 			(void) printf(gettext("unsupported feature(s)"));
1236ad135b5dSChristopher Siden 			break;
1237ad135b5dSChristopher Siden 
1238e6ca193dSGeorge Wilson 		case VDEV_AUX_SPARED:
1239e6ca193dSGeorge Wilson 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1240e6ca193dSGeorge Wilson 			    &cb.cb_guid) == 0);
1241e6ca193dSGeorge Wilson 			if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1242e6ca193dSGeorge Wilson 				if (strcmp(zpool_get_name(cb.cb_zhp),
1243e6ca193dSGeorge Wilson 				    zpool_get_name(zhp)) == 0)
1244e6ca193dSGeorge Wilson 					(void) printf(gettext("currently in "
1245e6ca193dSGeorge Wilson 					    "use"));
1246e6ca193dSGeorge Wilson 				else
1247e6ca193dSGeorge Wilson 					(void) printf(gettext("in use by "
1248e6ca193dSGeorge Wilson 					    "pool '%s'"),
1249e6ca193dSGeorge Wilson 					    zpool_get_name(cb.cb_zhp));
1250e6ca193dSGeorge Wilson 				zpool_close(cb.cb_zhp);
1251e6ca193dSGeorge Wilson 			} else {
1252e6ca193dSGeorge Wilson 				(void) printf(gettext("currently in use"));
1253e6ca193dSGeorge Wilson 			}
1254e6ca193dSGeorge Wilson 			break;
1255e6ca193dSGeorge Wilson 
1256e6ca193dSGeorge Wilson 		case VDEV_AUX_ERR_EXCEEDED:
1257e6ca193dSGeorge Wilson 			(void) printf(gettext("too many errors"));
1258e6ca193dSGeorge Wilson 			break;
1259e6ca193dSGeorge Wilson 
1260e6ca193dSGeorge Wilson 		case VDEV_AUX_IO_FAILURE:
1261e6ca193dSGeorge Wilson 			(void) printf(gettext("experienced I/O failures"));
1262e6ca193dSGeorge Wilson 			break;
1263e6ca193dSGeorge Wilson 
1264e6ca193dSGeorge Wilson 		case VDEV_AUX_BAD_LOG:
1265e6ca193dSGeorge Wilson 			(void) printf(gettext("bad intent log"));
1266e6ca193dSGeorge Wilson 			break;
1267e6ca193dSGeorge Wilson 
1268069f55e2SEric Schrock 		case VDEV_AUX_EXTERNAL:
1269069f55e2SEric Schrock 			(void) printf(gettext("external device fault"));
1270069f55e2SEric Schrock 			break;
1271069f55e2SEric Schrock 
12721195e687SMark J Musante 		case VDEV_AUX_SPLIT_POOL:
12731195e687SMark J Musante 			(void) printf(gettext("split into new pool"));
12741195e687SMark J Musante 			break;
12751195e687SMark J Musante 
1276e6ca193dSGeorge Wilson 		default:
1277e6ca193dSGeorge Wilson 			(void) printf(gettext("corrupted data"));
1278e6ca193dSGeorge Wilson 			break;
1279e6ca193dSGeorge Wilson 		}
12803f9d6ad7SLin Ling 	}
12813f9d6ad7SLin Ling 
12823f9d6ad7SLin Ling 	(void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
12833f9d6ad7SLin Ling 	    (uint64_t **)&ps, &c);
12843f9d6ad7SLin Ling 
12853f9d6ad7SLin Ling 	if (ps && ps->pss_state == DSS_SCANNING &&
12863f9d6ad7SLin Ling 	    vs->vs_scan_processed != 0 && children == 0) {
12873f9d6ad7SLin Ling 		(void) printf(gettext("  (%s)"),
12883f9d6ad7SLin Ling 		    (ps->pss_func == POOL_SCAN_RESILVER) ?
12893f9d6ad7SLin Ling 		    "resilvering" : "repairing");
1290e6ca193dSGeorge Wilson 	}
1291e6ca193dSGeorge Wilson 
1292e6ca193dSGeorge Wilson 	(void) printf("\n");
1293e6ca193dSGeorge Wilson 
1294e6ca193dSGeorge Wilson 	for (c = 0; c < children; c++) {
129588ecc943SGeorge Wilson 		uint64_t islog = B_FALSE, ishole = B_FALSE;
1296e6ca193dSGeorge Wilson 
129788ecc943SGeorge Wilson 		/* Don't print logs or holes here */
1298e6ca193dSGeorge Wilson 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
129988ecc943SGeorge Wilson 		    &islog);
130088ecc943SGeorge Wilson 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
130188ecc943SGeorge Wilson 		    &ishole);
130288ecc943SGeorge Wilson 		if (islog || ishole)
1303e6ca193dSGeorge Wilson 			continue;
130488ecc943SGeorge Wilson 		vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1305e6ca193dSGeorge Wilson 		print_status_config(zhp, vname, child[c],
1306e6ca193dSGeorge Wilson 		    namewidth, depth + 2, isspare);
1307e6ca193dSGeorge Wilson 		free(vname);
1308e6ca193dSGeorge Wilson 	}
1309e6ca193dSGeorge Wilson }
1310e6ca193dSGeorge Wilson 
1311fa9e4066Sahrens 
1312fa9e4066Sahrens /*
1313fa9e4066Sahrens  * Print the configuration of an exported pool.  Iterate over all vdevs in the
1314fa9e4066Sahrens  * pool, printing out the name and status for each one.
1315fa9e4066Sahrens  */
1316fa9e4066Sahrens void
1317e6ca193dSGeorge Wilson print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1318fa9e4066Sahrens {
1319fa9e4066Sahrens 	nvlist_t **child;
1320fa9e4066Sahrens 	uint_t c, children;
1321fa9e4066Sahrens 	vdev_stat_t *vs;
1322afefbcddSeschrock 	char *type, *vname;
1323fa9e4066Sahrens 
1324fa9e4066Sahrens 	verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
132588ecc943SGeorge Wilson 	if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
132688ecc943SGeorge Wilson 	    strcmp(type, VDEV_TYPE_HOLE) == 0)
1327fa9e4066Sahrens 		return;
1328fa9e4066Sahrens 
13293f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1330fa9e4066Sahrens 	    (uint64_t **)&vs, &c) == 0);
1331fa9e4066Sahrens 
1332fa9e4066Sahrens 	(void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1333990b4856Slling 	(void) printf("  %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1334fa9e4066Sahrens 
1335fa9e4066Sahrens 	if (vs->vs_aux != 0) {
13363d7072f8Seschrock 		(void) printf("  ");
1337fa9e4066Sahrens 
1338fa9e4066Sahrens 		switch (vs->vs_aux) {
1339fa9e4066Sahrens 		case VDEV_AUX_OPEN_FAILED:
1340fa9e4066Sahrens 			(void) printf(gettext("cannot open"));
1341fa9e4066Sahrens 			break;
1342fa9e4066Sahrens 
1343fa9e4066Sahrens 		case VDEV_AUX_BAD_GUID_SUM:
1344fa9e4066Sahrens 			(void) printf(gettext("missing device"));
1345fa9e4066Sahrens 			break;
1346fa9e4066Sahrens 
1347fa9e4066Sahrens 		case VDEV_AUX_NO_REPLICAS:
1348fa9e4066Sahrens 			(void) printf(gettext("insufficient replicas"));
1349fa9e4066Sahrens 			break;
1350fa9e4066Sahrens 
1351eaca9bbdSeschrock 		case VDEV_AUX_VERSION_NEWER:
1352eaca9bbdSeschrock 			(void) printf(gettext("newer version"));
1353eaca9bbdSeschrock 			break;
1354eaca9bbdSeschrock 
1355ad135b5dSChristopher Siden 		case VDEV_AUX_UNSUP_FEAT:
1356ad135b5dSChristopher Siden 			(void) printf(gettext("unsupported feature(s)"));
1357ad135b5dSChristopher Siden 			break;
1358ad135b5dSChristopher Siden 
13593d7072f8Seschrock 		case VDEV_AUX_ERR_EXCEEDED:
13603d7072f8Seschrock 			(void) printf(gettext("too many errors"));
13613d7072f8Seschrock 			break;
13623d7072f8Seschrock 
1363fa9e4066Sahrens 		default:
1364fa9e4066Sahrens 			(void) printf(gettext("corrupted data"));
1365fa9e4066Sahrens 			break;
1366fa9e4066Sahrens 		}
1367fa9e4066Sahrens 	}
1368fa9e4066Sahrens 	(void) printf("\n");
1369fa9e4066Sahrens 
1370fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1371fa9e4066Sahrens 	    &child, &children) != 0)
1372fa9e4066Sahrens 		return;
1373fa9e4066Sahrens 
1374afefbcddSeschrock 	for (c = 0; c < children; c++) {
13758654d025Sperrin 		uint64_t is_log = B_FALSE;
13768654d025Sperrin 
13778654d025Sperrin 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
13788654d025Sperrin 		    &is_log);
1379e6ca193dSGeorge Wilson 		if (is_log)
13808654d025Sperrin 			continue;
13818654d025Sperrin 
138288ecc943SGeorge Wilson 		vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1383e6ca193dSGeorge Wilson 		print_import_config(vname, child[c], namewidth, depth + 2);
1384afefbcddSeschrock 		free(vname);
1385afefbcddSeschrock 	}
138699653d4eSeschrock 
1387fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1388fa94a07fSbrendan 	    &child, &children) == 0) {
1389fa94a07fSbrendan 		(void) printf(gettext("\tcache\n"));
1390fa94a07fSbrendan 		for (c = 0; c < children; c++) {
139188ecc943SGeorge Wilson 			vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1392fa94a07fSbrendan 			(void) printf("\t  %s\n", vname);
1393fa94a07fSbrendan 			free(vname);
1394fa94a07fSbrendan 		}
1395fa94a07fSbrendan 	}
139699653d4eSeschrock 
1397fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1398fa94a07fSbrendan 	    &child, &children) == 0) {
139999653d4eSeschrock 		(void) printf(gettext("\tspares\n"));
140099653d4eSeschrock 		for (c = 0; c < children; c++) {
140188ecc943SGeorge Wilson 			vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
140299653d4eSeschrock 			(void) printf("\t  %s\n", vname);
140399653d4eSeschrock 			free(vname);
140499653d4eSeschrock 		}
1405fa9e4066Sahrens 	}
1406fa94a07fSbrendan }
1407fa9e4066Sahrens 
1408fa9e4066Sahrens /*
1409e6ca193dSGeorge Wilson  * Print log vdevs.
1410e6ca193dSGeorge Wilson  * Logs are recorded as top level vdevs in the main pool child array
1411e6ca193dSGeorge Wilson  * but with "is_log" set to 1. We use either print_status_config() or
1412e6ca193dSGeorge Wilson  * print_import_config() to print the top level logs then any log
1413e6ca193dSGeorge Wilson  * children (eg mirrored slogs) are printed recursively - which
1414e6ca193dSGeorge Wilson  * works because only the top level vdev is marked "is_log"
1415e6ca193dSGeorge Wilson  */
1416e6ca193dSGeorge Wilson static void
1417e6ca193dSGeorge Wilson print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1418e6ca193dSGeorge Wilson {
1419e6ca193dSGeorge Wilson 	uint_t c, children;
1420e6ca193dSGeorge Wilson 	nvlist_t **child;
1421e6ca193dSGeorge Wilson 
1422e6ca193dSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1423e6ca193dSGeorge Wilson 	    &children) != 0)
1424e6ca193dSGeorge Wilson 		return;
1425e6ca193dSGeorge Wilson 
1426e6ca193dSGeorge Wilson 	(void) printf(gettext("\tlogs\n"));
1427e6ca193dSGeorge Wilson 
1428e6ca193dSGeorge Wilson 	for (c = 0; c < children; c++) {
1429e6ca193dSGeorge Wilson 		uint64_t is_log = B_FALSE;
1430e6ca193dSGeorge Wilson 		char *name;
1431e6ca193dSGeorge Wilson 
1432e6ca193dSGeorge Wilson 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1433e6ca193dSGeorge Wilson 		    &is_log);
1434e6ca193dSGeorge Wilson 		if (!is_log)
1435e6ca193dSGeorge Wilson 			continue;
143688ecc943SGeorge Wilson 		name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1437e6ca193dSGeorge Wilson 		if (verbose)
1438e6ca193dSGeorge Wilson 			print_status_config(zhp, name, child[c], namewidth,
1439e6ca193dSGeorge Wilson 			    2, B_FALSE);
1440e6ca193dSGeorge Wilson 		else
1441e6ca193dSGeorge Wilson 			print_import_config(name, child[c], namewidth, 2);
1442e6ca193dSGeorge Wilson 		free(name);
1443e6ca193dSGeorge Wilson 	}
1444e6ca193dSGeorge Wilson }
1445468c413aSTim Haley 
1446e6ca193dSGeorge Wilson /*
1447fa9e4066Sahrens  * Display the status for the given pool.
1448fa9e4066Sahrens  */
1449fa9e4066Sahrens static void
1450fa9e4066Sahrens show_import(nvlist_t *config)
1451fa9e4066Sahrens {
1452fa9e4066Sahrens 	uint64_t pool_state;
1453fa9e4066Sahrens 	vdev_stat_t *vs;
1454fa9e4066Sahrens 	char *name;
1455fa9e4066Sahrens 	uint64_t guid;
1456fa9e4066Sahrens 	char *msgid;
1457fa9e4066Sahrens 	nvlist_t *nvroot;
1458fa9e4066Sahrens 	int reason;
145946657f8dSmmusante 	const char *health;
1460fa9e4066Sahrens 	uint_t vsc;
1461fa9e4066Sahrens 	int namewidth;
14628704186eSDan McDonald 	char *comment;
1463fa9e4066Sahrens 
1464fa9e4066Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1465fa9e4066Sahrens 	    &name) == 0);
1466fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1467fa9e4066Sahrens 	    &guid) == 0);
1468fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1469fa9e4066Sahrens 	    &pool_state) == 0);
1470fa9e4066Sahrens 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1471fa9e4066Sahrens 	    &nvroot) == 0);
1472fa9e4066Sahrens 
14733f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1474fa9e4066Sahrens 	    (uint64_t **)&vs, &vsc) == 0);
1475990b4856Slling 	health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1476fa9e4066Sahrens 
1477fa9e4066Sahrens 	reason = zpool_import_status(config, &msgid);
1478fa9e4066Sahrens 
147946657f8dSmmusante 	(void) printf(gettext("   pool: %s\n"), name);
148046657f8dSmmusante 	(void) printf(gettext("     id: %llu\n"), (u_longlong_t)guid);
148146657f8dSmmusante 	(void) printf(gettext("  state: %s"), health);
14824c58d714Sdarrenm 	if (pool_state == POOL_STATE_DESTROYED)
148346657f8dSmmusante 		(void) printf(gettext(" (DESTROYED)"));
14844c58d714Sdarrenm 	(void) printf("\n");
1485fa9e4066Sahrens 
1486fa9e4066Sahrens 	switch (reason) {
1487fa9e4066Sahrens 	case ZPOOL_STATUS_MISSING_DEV_R:
1488fa9e4066Sahrens 	case ZPOOL_STATUS_MISSING_DEV_NR:
1489fa9e4066Sahrens 	case ZPOOL_STATUS_BAD_GUID_SUM:
14908704186eSDan McDonald 		(void) printf(gettext(" status: One or more devices are "
14918704186eSDan McDonald 		    "missing from the system.\n"));
1492fa9e4066Sahrens 		break;
1493fa9e4066Sahrens 
1494fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_LABEL_R:
1495fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1496fa9e4066Sahrens 		(void) printf(gettext(" status: One or more devices contains "
1497fa9e4066Sahrens 		    "corrupted data.\n"));
1498fa9e4066Sahrens 		break;
1499fa9e4066Sahrens 
1500fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_DATA:
15018704186eSDan McDonald 		(void) printf(
15028704186eSDan McDonald 		    gettext(" status: The pool data is corrupted.\n"));
1503fa9e4066Sahrens 		break;
1504fa9e4066Sahrens 
1505441d80aaSlling 	case ZPOOL_STATUS_OFFLINE_DEV:
1506441d80aaSlling 		(void) printf(gettext(" status: One or more devices "
1507441d80aaSlling 		    "are offlined.\n"));
1508441d80aaSlling 		break;
1509441d80aaSlling 
1510ea8dc4b6Seschrock 	case ZPOOL_STATUS_CORRUPT_POOL:
1511ea8dc4b6Seschrock 		(void) printf(gettext(" status: The pool metadata is "
1512ea8dc4b6Seschrock 		    "corrupted.\n"));
1513ea8dc4b6Seschrock 		break;
1514ea8dc4b6Seschrock 
1515eaca9bbdSeschrock 	case ZPOOL_STATUS_VERSION_OLDER:
151657221772SChristopher Siden 		(void) printf(gettext(" status: The pool is formatted using a "
151757221772SChristopher Siden 		    "legacy on-disk version.\n"));
1518eaca9bbdSeschrock 		break;
1519eaca9bbdSeschrock 
1520eaca9bbdSeschrock 	case ZPOOL_STATUS_VERSION_NEWER:
1521eaca9bbdSeschrock 		(void) printf(gettext(" status: The pool is formatted using an "
1522eaca9bbdSeschrock 		    "incompatible version.\n"));
1523eaca9bbdSeschrock 		break;
1524b87f3af3Sperrin 
152557221772SChristopher Siden 	case ZPOOL_STATUS_FEAT_DISABLED:
152657221772SChristopher Siden 		(void) printf(gettext(" status: Some supported features are "
152757221772SChristopher Siden 		    "not enabled on the pool.\n"));
152857221772SChristopher Siden 		break;
152957221772SChristopher Siden 
1530ad135b5dSChristopher Siden 	case ZPOOL_STATUS_UNSUP_FEAT_READ:
1531ad135b5dSChristopher Siden 		(void) printf(gettext("status: The pool uses the following "
1532ad135b5dSChristopher Siden 		    "feature(s) not supported on this sytem:\n"));
1533ad135b5dSChristopher Siden 		zpool_print_unsup_feat(config);
1534ad135b5dSChristopher Siden 		break;
1535ad135b5dSChristopher Siden 
1536ad135b5dSChristopher Siden 	case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1537ad135b5dSChristopher Siden 		(void) printf(gettext("status: The pool can only be accessed "
1538ad135b5dSChristopher Siden 		    "in read-only mode on this system. It\n\tcannot be "
1539ad135b5dSChristopher Siden 		    "accessed in read-write mode because it uses the "
1540ad135b5dSChristopher Siden 		    "following\n\tfeature(s) not supported on this system:\n"));
1541ad135b5dSChristopher Siden 		zpool_print_unsup_feat(config);
1542ad135b5dSChristopher Siden 		break;
1543ad135b5dSChristopher Siden 
154495173954Sek110237 	case ZPOOL_STATUS_HOSTID_MISMATCH:
154595173954Sek110237 		(void) printf(gettext(" status: The pool was last accessed by "
154695173954Sek110237 		    "another system.\n"));
154795173954Sek110237 		break;
1548b87f3af3Sperrin 
15493d7072f8Seschrock 	case ZPOOL_STATUS_FAULTED_DEV_R:
15503d7072f8Seschrock 	case ZPOOL_STATUS_FAULTED_DEV_NR:
15513d7072f8Seschrock 		(void) printf(gettext(" status: One or more devices are "
15523d7072f8Seschrock 		    "faulted.\n"));
15533d7072f8Seschrock 		break;
15543d7072f8Seschrock 
1555b87f3af3Sperrin 	case ZPOOL_STATUS_BAD_LOG:
1556b87f3af3Sperrin 		(void) printf(gettext(" status: An intent log record cannot be "
1557b87f3af3Sperrin 		    "read.\n"));
1558b87f3af3Sperrin 		break;
1559b87f3af3Sperrin 
15603f9d6ad7SLin Ling 	case ZPOOL_STATUS_RESILVERING:
15613f9d6ad7SLin Ling 		(void) printf(gettext(" status: One or more devices were being "
15623f9d6ad7SLin Ling 		    "resilvered.\n"));
15633f9d6ad7SLin Ling 		break;
15643f9d6ad7SLin Ling 
1565fa9e4066Sahrens 	default:
1566fa9e4066Sahrens 		/*
1567fa9e4066Sahrens 		 * No other status can be seen when importing pools.
1568fa9e4066Sahrens 		 */
1569fa9e4066Sahrens 		assert(reason == ZPOOL_STATUS_OK);
1570fa9e4066Sahrens 	}
1571fa9e4066Sahrens 
1572fa9e4066Sahrens 	/*
1573fa9e4066Sahrens 	 * Print out an action according to the overall state of the pool.
1574fa9e4066Sahrens 	 */
157546657f8dSmmusante 	if (vs->vs_state == VDEV_STATE_HEALTHY) {
157657221772SChristopher Siden 		if (reason == ZPOOL_STATUS_VERSION_OLDER ||
157757221772SChristopher Siden 		    reason == ZPOOL_STATUS_FEAT_DISABLED) {
1578eaca9bbdSeschrock 			(void) printf(gettext(" action: The pool can be "
1579eaca9bbdSeschrock 			    "imported using its name or numeric identifier, "
1580eaca9bbdSeschrock 			    "though\n\tsome features will not be available "
1581eaca9bbdSeschrock 			    "without an explicit 'zpool upgrade'.\n"));
158257221772SChristopher Siden 		} else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
158395173954Sek110237 			(void) printf(gettext(" action: The pool can be "
158495173954Sek110237 			    "imported using its name or numeric "
158595173954Sek110237 			    "identifier and\n\tthe '-f' flag.\n"));
158657221772SChristopher Siden 		} else {
1587eaca9bbdSeschrock 			(void) printf(gettext(" action: The pool can be "
1588eaca9bbdSeschrock 			    "imported using its name or numeric "
1589eaca9bbdSeschrock 			    "identifier.\n"));
159057221772SChristopher Siden 		}
159146657f8dSmmusante 	} else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1592fa9e4066Sahrens 		(void) printf(gettext(" action: The pool can be imported "
1593fa9e4066Sahrens 		    "despite missing or damaged devices.  The\n\tfault "
1594eaca9bbdSeschrock 		    "tolerance of the pool may be compromised if imported.\n"));
1595fa9e4066Sahrens 	} else {
1596eaca9bbdSeschrock 		switch (reason) {
1597eaca9bbdSeschrock 		case ZPOOL_STATUS_VERSION_NEWER:
1598eaca9bbdSeschrock 			(void) printf(gettext(" action: The pool cannot be "
1599eaca9bbdSeschrock 			    "imported.  Access the pool on a system running "
1600eaca9bbdSeschrock 			    "newer\n\tsoftware, or recreate the pool from "
1601eaca9bbdSeschrock 			    "backup.\n"));
1602eaca9bbdSeschrock 			break;
1603ad135b5dSChristopher Siden 		case ZPOOL_STATUS_UNSUP_FEAT_READ:
1604ad135b5dSChristopher Siden 			(void) printf(gettext("action: The pool cannot be "
1605ad135b5dSChristopher Siden 			    "imported. Access the pool on a system that "
1606ad135b5dSChristopher Siden 			    "supports\n\tthe required feature(s), or recreate "
1607ad135b5dSChristopher Siden 			    "the pool from backup.\n"));
1608ad135b5dSChristopher Siden 			break;
1609ad135b5dSChristopher Siden 		case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1610ad135b5dSChristopher Siden 			(void) printf(gettext("action: The pool cannot be "
1611ad135b5dSChristopher Siden 			    "imported in read-write mode. Import the pool "
1612ad135b5dSChristopher Siden 			    "with\n"
1613ad135b5dSChristopher Siden 			    "\t\"-o readonly=on\", access the pool on a system "
1614ad135b5dSChristopher Siden 			    "that supports the\n\trequired feature(s), or "
1615ad135b5dSChristopher Siden 			    "recreate the pool from backup.\n"));
1616ad135b5dSChristopher Siden 			break;
1617eaca9bbdSeschrock 		case ZPOOL_STATUS_MISSING_DEV_R:
1618eaca9bbdSeschrock 		case ZPOOL_STATUS_MISSING_DEV_NR:
1619eaca9bbdSeschrock 		case ZPOOL_STATUS_BAD_GUID_SUM:
1620fa9e4066Sahrens 			(void) printf(gettext(" action: The pool cannot be "
1621fa9e4066Sahrens 			    "imported. Attach the missing\n\tdevices and try "
1622fa9e4066Sahrens 			    "again.\n"));
1623eaca9bbdSeschrock 			break;
1624eaca9bbdSeschrock 		default:
1625fa9e4066Sahrens 			(void) printf(gettext(" action: The pool cannot be "
1626fa9e4066Sahrens 			    "imported due to damaged devices or data.\n"));
1627fa9e4066Sahrens 		}
1628eaca9bbdSeschrock 	}
1629eaca9bbdSeschrock 
16308704186eSDan McDonald 	/* Print the comment attached to the pool. */
16318704186eSDan McDonald 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
16328704186eSDan McDonald 		(void) printf(gettext("comment: %s\n"), comment);
16338704186eSDan McDonald 
163446657f8dSmmusante 	/*
163546657f8dSmmusante 	 * If the state is "closed" or "can't open", and the aux state
163646657f8dSmmusante 	 * is "corrupt data":
163746657f8dSmmusante 	 */
163846657f8dSmmusante 	if (((vs->vs_state == VDEV_STATE_CLOSED) ||
163946657f8dSmmusante 	    (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
164046657f8dSmmusante 	    (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1641eaca9bbdSeschrock 		if (pool_state == POOL_STATE_DESTROYED)
1642eaca9bbdSeschrock 			(void) printf(gettext("\tThe pool was destroyed, "
1643eaca9bbdSeschrock 			    "but can be imported using the '-Df' flags.\n"));
1644eaca9bbdSeschrock 		else if (pool_state != POOL_STATE_EXPORTED)
1645eaca9bbdSeschrock 			(void) printf(gettext("\tThe pool may be active on "
164618ce54dfSek110237 			    "another system, but can be imported using\n\t"
1647eaca9bbdSeschrock 			    "the '-f' flag.\n"));
1648eaca9bbdSeschrock 	}
1649fa9e4066Sahrens 
1650fa9e4066Sahrens 	if (msgid != NULL)
1651654b400cSJoshua M. Clulow 		(void) printf(gettext("   see: http://illumos.org/msg/%s\n"),
1652fa9e4066Sahrens 		    msgid);
1653fa9e4066Sahrens 
1654fa9e4066Sahrens 	(void) printf(gettext(" config:\n\n"));
1655fa9e4066Sahrens 
1656c67d9675Seschrock 	namewidth = max_width(NULL, nvroot, 0, 0);
1657fa9e4066Sahrens 	if (namewidth < 10)
1658fa9e4066Sahrens 		namewidth = 10;
16598654d025Sperrin 
1660e6ca193dSGeorge Wilson 	print_import_config(name, nvroot, namewidth, 0);
1661e6ca193dSGeorge Wilson 	if (num_logs(nvroot) > 0)
1662e6ca193dSGeorge Wilson 		print_logs(NULL, nvroot, namewidth, B_FALSE);
1663fa9e4066Sahrens 
1664fa9e4066Sahrens 	if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
166546657f8dSmmusante 		(void) printf(gettext("\n\tAdditional devices are known to "
1666fa9e4066Sahrens 		    "be part of this pool, though their\n\texact "
166746657f8dSmmusante 		    "configuration cannot be determined.\n"));
1668fa9e4066Sahrens 	}
1669fa9e4066Sahrens }
1670fa9e4066Sahrens 
1671fa9e4066Sahrens /*
1672fa9e4066Sahrens  * Perform the import for the given configuration.  This passes the heavy
1673990b4856Slling  * lifting off to zpool_import_props(), and then mounts the datasets contained
1674990b4856Slling  * within the pool.
1675fa9e4066Sahrens  */
1676fa9e4066Sahrens static int
1677fa9e4066Sahrens do_import(nvlist_t *config, const char *newname, const char *mntopts,
16784b964adaSGeorge Wilson     nvlist_t *props, int flags)
1679fa9e4066Sahrens {
1680fa9e4066Sahrens 	zpool_handle_t *zhp;
1681fa9e4066Sahrens 	char *name;
1682fa9e4066Sahrens 	uint64_t state;
1683eaca9bbdSeschrock 	uint64_t version;
1684fa9e4066Sahrens 
1685fa9e4066Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1686fa9e4066Sahrens 	    &name) == 0);
1687fa9e4066Sahrens 
1688fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config,
1689fa9e4066Sahrens 	    ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1690eaca9bbdSeschrock 	verify(nvlist_lookup_uint64(config,
1691eaca9bbdSeschrock 	    ZPOOL_CONFIG_VERSION, &version) == 0);
1692ad135b5dSChristopher Siden 	if (!SPA_VERSION_IS_SUPPORTED(version)) {
1693eaca9bbdSeschrock 		(void) fprintf(stderr, gettext("cannot import '%s': pool "
1694ad135b5dSChristopher Siden 		    "is formatted using an unsupported ZFS version\n"), name);
1695eaca9bbdSeschrock 		return (1);
16964b964adaSGeorge Wilson 	} else if (state != POOL_STATE_EXPORTED &&
16974b964adaSGeorge Wilson 	    !(flags & ZFS_IMPORT_ANY_HOST)) {
169895173954Sek110237 		uint64_t hostid;
169995173954Sek110237 
170095173954Sek110237 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
170195173954Sek110237 		    &hostid) == 0) {
170295173954Sek110237 			if ((unsigned long)hostid != gethostid()) {
170395173954Sek110237 				char *hostname;
170495173954Sek110237 				uint64_t timestamp;
170595173954Sek110237 				time_t t;
170695173954Sek110237 
170795173954Sek110237 				verify(nvlist_lookup_string(config,
170895173954Sek110237 				    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
170995173954Sek110237 				verify(nvlist_lookup_uint64(config,
171095173954Sek110237 				    ZPOOL_CONFIG_TIMESTAMP, &timestamp) == 0);
171195173954Sek110237 				t = timestamp;
171295173954Sek110237 				(void) fprintf(stderr, gettext("cannot import "
171395173954Sek110237 				    "'%s': pool may be in use from other "
171495173954Sek110237 				    "system, it was last accessed by %s "
171595173954Sek110237 				    "(hostid: 0x%lx) on %s"), name, hostname,
171695173954Sek110237 				    (unsigned long)hostid,
171795173954Sek110237 				    asctime(localtime(&t)));
171895173954Sek110237 				(void) fprintf(stderr, gettext("use '-f' to "
171995173954Sek110237 				    "import anyway\n"));
1720fa9e4066Sahrens 				return (1);
1721fa9e4066Sahrens 			}
172295173954Sek110237 		} else {
172395173954Sek110237 			(void) fprintf(stderr, gettext("cannot import '%s': "
172495173954Sek110237 			    "pool may be in use from other system\n"), name);
172595173954Sek110237 			(void) fprintf(stderr, gettext("use '-f' to import "
172695173954Sek110237 			    "anyway\n"));
172795173954Sek110237 			return (1);
172895173954Sek110237 		}
172995173954Sek110237 	}
1730fa9e4066Sahrens 
17314b964adaSGeorge Wilson 	if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1732fa9e4066Sahrens 		return (1);
1733fa9e4066Sahrens 
1734fa9e4066Sahrens 	if (newname != NULL)
1735fa9e4066Sahrens 		name = (char *)newname;
1736fa9e4066Sahrens 
17374f0f5e5bSVictor Latushkin 	if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
17384f0f5e5bSVictor Latushkin 		return (1);
1739fa9e4066Sahrens 
1740379c004dSEric Schrock 	if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1741f9af39baSGeorge Wilson 	    !(flags & ZFS_IMPORT_ONLY) &&
1742379c004dSEric Schrock 	    zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1743fa9e4066Sahrens 		zpool_close(zhp);
1744fa9e4066Sahrens 		return (1);
1745fa9e4066Sahrens 	}
1746fa9e4066Sahrens 
1747fa9e4066Sahrens 	zpool_close(zhp);
1748468c413aSTim Haley 	return (0);
1749fa9e4066Sahrens }
1750fa9e4066Sahrens 
1751fa9e4066Sahrens /*
17524c58d714Sdarrenm  * zpool import [-d dir] [-D]
17532f8aaab3Seschrock  *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
17542f8aaab3Seschrock  *              [-d dir | -c cachefile] [-f] -a
17552f8aaab3Seschrock  *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1756468c413aSTim Haley  *              [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
17572f8aaab3Seschrock  *
17582f8aaab3Seschrock  *	 -c	Read pool information from a cachefile instead of searching
17592f8aaab3Seschrock  *		devices.
1760fa9e4066Sahrens  *
1761fa9e4066Sahrens  *       -d	Scan in a specific directory, other than /dev/dsk.  More than
1762fa9e4066Sahrens  *		one directory can be specified using multiple '-d' options.
1763fa9e4066Sahrens  *
17644c58d714Sdarrenm  *       -D     Scan for previously destroyed pools or import all or only
17654c58d714Sdarrenm  *              specified destroyed pools.
17664c58d714Sdarrenm  *
1767fa9e4066Sahrens  *       -R	Temporarily import the pool, with all mountpoints relative to
1768fa9e4066Sahrens  *		the given root.  The pool will remain exported when the machine
1769fa9e4066Sahrens  *		is rebooted.
1770fa9e4066Sahrens  *
1771468c413aSTim Haley  *       -V	Import even in the presence of faulted vdevs.  This is an
1772c5904d13Seschrock  *       	intentionally undocumented option for testing purposes, and
1773c5904d13Seschrock  *       	treats the pool configuration as complete, leaving any bad
17744f0f5e5bSVictor Latushkin  *		vdevs in the FAULTED state. In other words, it does verbatim
17754f0f5e5bSVictor Latushkin  *		import.
1776c5904d13Seschrock  *
1777468c413aSTim Haley  *       -f	Force import, even if it appears that the pool is active.
1778468c413aSTim Haley  *
1779468c413aSTim Haley  *       -F     Attempt rewind if necessary.
1780468c413aSTim Haley  *
1781468c413aSTim Haley  *       -n     See if rewind would work, but don't actually rewind.
1782468c413aSTim Haley  *
1783f9af39baSGeorge Wilson  *       -N     Import the pool but don't mount datasets.
1784f9af39baSGeorge Wilson  *
1785f9af39baSGeorge Wilson  *       -T     Specify a starting txg to use for import. This option is
1786f9af39baSGeorge Wilson  *       	intentionally undocumented option for testing purposes.
1787f9af39baSGeorge Wilson  *
1788fa9e4066Sahrens  *       -a	Import all pools found.
1789fa9e4066Sahrens  *
1790990b4856Slling  *       -o	Set property=value and/or temporary mount options (without '=').
1791ecd6cf80Smarks  *
1792fa9e4066Sahrens  * The import command scans for pools to import, and import pools based on pool
1793fa9e4066Sahrens  * name and GUID.  The pool can also be renamed as part of the import process.
1794fa9e4066Sahrens  */
1795fa9e4066Sahrens int
1796fa9e4066Sahrens zpool_do_import(int argc, char **argv)
1797fa9e4066Sahrens {
1798fa9e4066Sahrens 	char **searchdirs = NULL;
1799fa9e4066Sahrens 	int nsearch = 0;
1800fa9e4066Sahrens 	int c;
1801d41c4376SMark J Musante 	int err = 0;
18022f8aaab3Seschrock 	nvlist_t *pools = NULL;
180399653d4eSeschrock 	boolean_t do_all = B_FALSE;
180499653d4eSeschrock 	boolean_t do_destroyed = B_FALSE;
1805fa9e4066Sahrens 	char *mntopts = NULL;
1806fa9e4066Sahrens 	nvpair_t *elem;
1807fa9e4066Sahrens 	nvlist_t *config;
180824e697d4Sck153898 	uint64_t searchguid = 0;
180924e697d4Sck153898 	char *searchname = NULL;
1810990b4856Slling 	char *propval;
1811fa9e4066Sahrens 	nvlist_t *found_config;
1812468c413aSTim Haley 	nvlist_t *policy = NULL;
1813ecd6cf80Smarks 	nvlist_t *props = NULL;
181499653d4eSeschrock 	boolean_t first;
18154b964adaSGeorge Wilson 	int flags = ZFS_IMPORT_NORMAL;
1816468c413aSTim Haley 	uint32_t rewind_policy = ZPOOL_NO_REWIND;
1817468c413aSTim Haley 	boolean_t dryrun = B_FALSE;
1818468c413aSTim Haley 	boolean_t do_rewind = B_FALSE;
1819468c413aSTim Haley 	boolean_t xtreme_rewind = B_FALSE;
1820f9af39baSGeorge Wilson 	uint64_t pool_state, txg = -1ULL;
18212f8aaab3Seschrock 	char *cachefile = NULL;
1822d41c4376SMark J Musante 	importargs_t idata = { 0 };
1823f9af39baSGeorge Wilson 	char *endptr;
1824fa9e4066Sahrens 
1825fa9e4066Sahrens 	/* check options */
1826f9af39baSGeorge Wilson 	while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1827fa9e4066Sahrens 		switch (c) {
1828fa9e4066Sahrens 		case 'a':
182999653d4eSeschrock 			do_all = B_TRUE;
1830fa9e4066Sahrens 			break;
18312f8aaab3Seschrock 		case 'c':
18322f8aaab3Seschrock 			cachefile = optarg;
18332f8aaab3Seschrock 			break;
1834fa9e4066Sahrens 		case 'd':
1835fa9e4066Sahrens 			if (searchdirs == NULL) {
1836fa9e4066Sahrens 				searchdirs = safe_malloc(sizeof (char *));
1837fa9e4066Sahrens 			} else {
1838fa9e4066Sahrens 				char **tmp = safe_malloc((nsearch + 1) *
1839fa9e4066Sahrens 				    sizeof (char *));
1840fa9e4066Sahrens 				bcopy(searchdirs, tmp, nsearch *
1841fa9e4066Sahrens 				    sizeof (char *));
1842fa9e4066Sahrens 				free(searchdirs);
1843fa9e4066Sahrens 				searchdirs = tmp;
1844fa9e4066Sahrens 			}
1845fa9e4066Sahrens 			searchdirs[nsearch++] = optarg;
1846fa9e4066Sahrens 			break;
18474c58d714Sdarrenm 		case 'D':
184899653d4eSeschrock 			do_destroyed = B_TRUE;
18494c58d714Sdarrenm 			break;
1850fa9e4066Sahrens 		case 'f':
18514b964adaSGeorge Wilson 			flags |= ZFS_IMPORT_ANY_HOST;
1852fa9e4066Sahrens 			break;
1853c5904d13Seschrock 		case 'F':
1854468c413aSTim Haley 			do_rewind = B_TRUE;
1855468c413aSTim Haley 			break;
18564b964adaSGeorge Wilson 		case 'm':
18574b964adaSGeorge Wilson 			flags |= ZFS_IMPORT_MISSING_LOG;
18584b964adaSGeorge Wilson 			break;
1859468c413aSTim Haley 		case 'n':
1860468c413aSTim Haley 			dryrun = B_TRUE;
1861c5904d13Seschrock 			break;
1862f9af39baSGeorge Wilson 		case 'N':
1863f9af39baSGeorge Wilson 			flags |= ZFS_IMPORT_ONLY;
1864f9af39baSGeorge Wilson 			break;
1865fa9e4066Sahrens 		case 'o':
1866990b4856Slling 			if ((propval = strchr(optarg, '=')) != NULL) {
1867990b4856Slling 				*propval = '\0';
1868990b4856Slling 				propval++;
18690a48a24eStimh 				if (add_prop_list(optarg, propval,
18700a48a24eStimh 				    &props, B_TRUE))
1871990b4856Slling 					goto error;
1872990b4856Slling 			} else {
1873fa9e4066Sahrens 				mntopts = optarg;
1874990b4856Slling 			}
1875fa9e4066Sahrens 			break;
1876fa9e4066Sahrens 		case 'R':
1877990b4856Slling 			if (add_prop_list(zpool_prop_to_name(
18780a48a24eStimh 			    ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1879990b4856Slling 				goto error;
18802f8aaab3Seschrock 			if (nvlist_lookup_string(props,
18812f8aaab3Seschrock 			    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
18822f8aaab3Seschrock 			    &propval) == 0)
18832f8aaab3Seschrock 				break;
1884990b4856Slling 			if (add_prop_list(zpool_prop_to_name(
18850a48a24eStimh 			    ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1886990b4856Slling 				goto error;
1887fa9e4066Sahrens 			break;
1888f9af39baSGeorge Wilson 		case 'T':
1889f9af39baSGeorge Wilson 			errno = 0;
1890e42d2059SMatthew Ahrens 			txg = strtoull(optarg, &endptr, 0);
1891f9af39baSGeorge Wilson 			if (errno != 0 || *endptr != '\0') {
1892f9af39baSGeorge Wilson 				(void) fprintf(stderr,
1893f9af39baSGeorge Wilson 				    gettext("invalid txg value\n"));
1894f9af39baSGeorge Wilson 				usage(B_FALSE);
1895f9af39baSGeorge Wilson 			}
1896f9af39baSGeorge Wilson 			rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
1897f9af39baSGeorge Wilson 			break;
1898468c413aSTim Haley 		case 'V':
18994b964adaSGeorge Wilson 			flags |= ZFS_IMPORT_VERBATIM;
1900468c413aSTim Haley 			break;
1901468c413aSTim Haley 		case 'X':
1902468c413aSTim Haley 			xtreme_rewind = B_TRUE;
1903468c413aSTim Haley 			break;
1904fa9e4066Sahrens 		case ':':
1905fa9e4066Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
1906fa9e4066Sahrens 			    "'%c' option\n"), optopt);
190799653d4eSeschrock 			usage(B_FALSE);
1908fa9e4066Sahrens 			break;
1909fa9e4066Sahrens 		case '?':
1910fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1911fa9e4066Sahrens 			    optopt);
191299653d4eSeschrock 			usage(B_FALSE);
1913fa9e4066Sahrens 		}
1914fa9e4066Sahrens 	}
1915fa9e4066Sahrens 
1916fa9e4066Sahrens 	argc -= optind;
1917fa9e4066Sahrens 	argv += optind;
1918fa9e4066Sahrens 
19192f8aaab3Seschrock 	if (cachefile && nsearch != 0) {
19202f8aaab3Seschrock 		(void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
19212f8aaab3Seschrock 		usage(B_FALSE);
19222f8aaab3Seschrock 	}
19232f8aaab3Seschrock 
1924468c413aSTim Haley 	if ((dryrun || xtreme_rewind) && !do_rewind) {
1925468c413aSTim Haley 		(void) fprintf(stderr,
1926468c413aSTim Haley 		    gettext("-n or -X only meaningful with -F\n"));
1927468c413aSTim Haley 		usage(B_FALSE);
1928468c413aSTim Haley 	}
1929468c413aSTim Haley 	if (dryrun)
1930468c413aSTim Haley 		rewind_policy = ZPOOL_TRY_REWIND;
1931468c413aSTim Haley 	else if (do_rewind)
1932468c413aSTim Haley 		rewind_policy = ZPOOL_DO_REWIND;
1933468c413aSTim Haley 	if (xtreme_rewind)
1934468c413aSTim Haley 		rewind_policy |= ZPOOL_EXTREME_REWIND;
1935468c413aSTim Haley 
1936468c413aSTim Haley 	/* In the future, we can capture further policy and include it here */
1937468c413aSTim Haley 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
1938f9af39baSGeorge Wilson 	    nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
1939468c413aSTim Haley 	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
1940468c413aSTim Haley 		goto error;
1941468c413aSTim Haley 
1942fa9e4066Sahrens 	if (searchdirs == NULL) {
1943fa9e4066Sahrens 		searchdirs = safe_malloc(sizeof (char *));
1944fa9e4066Sahrens 		searchdirs[0] = "/dev/dsk";
1945fa9e4066Sahrens 		nsearch = 1;
1946fa9e4066Sahrens 	}
1947fa9e4066Sahrens 
1948fa9e4066Sahrens 	/* check argument count */
1949fa9e4066Sahrens 	if (do_all) {
1950fa9e4066Sahrens 		if (argc != 0) {
1951fa9e4066Sahrens 			(void) fprintf(stderr, gettext("too many arguments\n"));
195299653d4eSeschrock 			usage(B_FALSE);
1953fa9e4066Sahrens 		}
1954fa9e4066Sahrens 	} else {
1955fa9e4066Sahrens 		if (argc > 2) {
1956fa9e4066Sahrens 			(void) fprintf(stderr, gettext("too many arguments\n"));
195799653d4eSeschrock 			usage(B_FALSE);
1958fa9e4066Sahrens 		}
1959fa9e4066Sahrens 
1960fa9e4066Sahrens 		/*
1961fa9e4066Sahrens 		 * Check for the SYS_CONFIG privilege.  We do this explicitly
1962fa9e4066Sahrens 		 * here because otherwise any attempt to discover pools will
1963fa9e4066Sahrens 		 * silently fail.
1964fa9e4066Sahrens 		 */
1965fa9e4066Sahrens 		if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
1966fa9e4066Sahrens 			(void) fprintf(stderr, gettext("cannot "
1967fa9e4066Sahrens 			    "discover pools: permission denied\n"));
196899653d4eSeschrock 			free(searchdirs);
1969468c413aSTim Haley 			nvlist_free(policy);
1970fa9e4066Sahrens 			return (1);
1971fa9e4066Sahrens 		}
1972fa9e4066Sahrens 	}
1973fa9e4066Sahrens 
1974fa9e4066Sahrens 	/*
1975fa9e4066Sahrens 	 * Depending on the arguments given, we do one of the following:
1976fa9e4066Sahrens 	 *
1977fa9e4066Sahrens 	 *	<none>	Iterate through all pools and display information about
1978fa9e4066Sahrens 	 *		each one.
1979fa9e4066Sahrens 	 *
1980fa9e4066Sahrens 	 *	-a	Iterate through all pools and try to import each one.
1981fa9e4066Sahrens 	 *
1982fa9e4066Sahrens 	 *	<id>	Find the pool that corresponds to the given GUID/pool
1983fa9e4066Sahrens 	 *		name and import that one.
19844c58d714Sdarrenm 	 *
19854c58d714Sdarrenm 	 *	-D	Above options applies only to destroyed pools.
1986fa9e4066Sahrens 	 */
1987fa9e4066Sahrens 	if (argc != 0) {
1988fa9e4066Sahrens 		char *endptr;
1989fa9e4066Sahrens 
1990fa9e4066Sahrens 		errno = 0;
1991fa9e4066Sahrens 		searchguid = strtoull(argv[0], &endptr, 10);
19929edf9ebdSPrasad Joshi 		if (errno != 0 || *endptr != '\0') {
1993fa9e4066Sahrens 			searchname = argv[0];
19949edf9ebdSPrasad Joshi 			searchguid = 0;
19959edf9ebdSPrasad Joshi 		}
1996fa9e4066Sahrens 		found_config = NULL;
1997fa9e4066Sahrens 
199824e697d4Sck153898 		/*
1999d41c4376SMark J Musante 		 * User specified a name or guid.  Ensure it's unique.
200024e697d4Sck153898 		 */
2001d41c4376SMark J Musante 		idata.unique = B_TRUE;
200224e697d4Sck153898 	}
200324e697d4Sck153898 
2004d41c4376SMark J Musante 
2005d41c4376SMark J Musante 	idata.path = searchdirs;
2006d41c4376SMark J Musante 	idata.paths = nsearch;
2007d41c4376SMark J Musante 	idata.poolname = searchname;
2008d41c4376SMark J Musante 	idata.guid = searchguid;
2009d41c4376SMark J Musante 	idata.cachefile = cachefile;
2010d41c4376SMark J Musante 
2011d41c4376SMark J Musante 	pools = zpool_search_import(g_zfs, &idata);
2012d41c4376SMark J Musante 
2013d41c4376SMark J Musante 	if (pools != NULL && idata.exists &&
2014d41c4376SMark J Musante 	    (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2015d41c4376SMark J Musante 		(void) fprintf(stderr, gettext("cannot import '%s': "
2016d41c4376SMark J Musante 		    "a pool with that name already exists\n"),
2017d41c4376SMark J Musante 		    argv[0]);
2018d41c4376SMark J Musante 		(void) fprintf(stderr, gettext("use the form '%s "
2019d41c4376SMark J Musante 		    "<pool | id> <newpool>' to give it a new name\n"),
2020d41c4376SMark J Musante 		    "zpool import");
2021d41c4376SMark J Musante 		err = 1;
2022d41c4376SMark J Musante 	} else if (pools == NULL && idata.exists) {
2023d41c4376SMark J Musante 		(void) fprintf(stderr, gettext("cannot import '%s': "
2024d41c4376SMark J Musante 		    "a pool with that name is already created/imported,\n"),
2025d41c4376SMark J Musante 		    argv[0]);
2026d41c4376SMark J Musante 		(void) fprintf(stderr, gettext("and no additional pools "
2027d41c4376SMark J Musante 		    "with that name were found\n"));
2028d41c4376SMark J Musante 		err = 1;
2029d41c4376SMark J Musante 	} else if (pools == NULL) {
203024e697d4Sck153898 		if (argc != 0) {
203124e697d4Sck153898 			(void) fprintf(stderr, gettext("cannot import '%s': "
203224e697d4Sck153898 			    "no such pool available\n"), argv[0]);
203324e697d4Sck153898 		}
2034d41c4376SMark J Musante 		err = 1;
2035d41c4376SMark J Musante 	}
2036d41c4376SMark J Musante 
2037d41c4376SMark J Musante 	if (err == 1) {
203824e697d4Sck153898 		free(searchdirs);
2039468c413aSTim Haley 		nvlist_free(policy);
204024e697d4Sck153898 		return (1);
204124e697d4Sck153898 	}
204224e697d4Sck153898 
204324e697d4Sck153898 	/*
204424e697d4Sck153898 	 * At this point we have a list of import candidate configs. Even if
204524e697d4Sck153898 	 * we were searching by pool name or guid, we still need to
204624e697d4Sck153898 	 * post-process the list to deal with pool state and possible
204724e697d4Sck153898 	 * duplicate names.
204824e697d4Sck153898 	 */
2049fa9e4066Sahrens 	err = 0;
2050fa9e4066Sahrens 	elem = NULL;
205199653d4eSeschrock 	first = B_TRUE;
2052fa9e4066Sahrens 	while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2053fa9e4066Sahrens 
2054fa9e4066Sahrens 		verify(nvpair_value_nvlist(elem, &config) == 0);
2055fa9e4066Sahrens 
20564c58d714Sdarrenm 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
20574c58d714Sdarrenm 		    &pool_state) == 0);
20584c58d714Sdarrenm 		if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
20594c58d714Sdarrenm 			continue;
20604c58d714Sdarrenm 		if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
20614c58d714Sdarrenm 			continue;
20624c58d714Sdarrenm 
2063468c413aSTim Haley 		verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2064468c413aSTim Haley 		    policy) == 0);
2065468c413aSTim Haley 
2066fa9e4066Sahrens 		if (argc == 0) {
2067fa9e4066Sahrens 			if (first)
206899653d4eSeschrock 				first = B_FALSE;
20693bb79becSeschrock 			else if (!do_all)
2070fa9e4066Sahrens 				(void) printf("\n");
2071fa9e4066Sahrens 
2072468c413aSTim Haley 			if (do_all) {
2073fa9e4066Sahrens 				err |= do_import(config, NULL, mntopts,
20744b964adaSGeorge Wilson 				    props, flags);
2075468c413aSTim Haley 			} else {
2076fa9e4066Sahrens 				show_import(config);
2077468c413aSTim Haley 			}
2078fa9e4066Sahrens 		} else if (searchname != NULL) {
2079fa9e4066Sahrens 			char *name;
2080fa9e4066Sahrens 
2081fa9e4066Sahrens 			/*
2082fa9e4066Sahrens 			 * We are searching for a pool based on name.
2083fa9e4066Sahrens 			 */
2084fa9e4066Sahrens 			verify(nvlist_lookup_string(config,
2085fa9e4066Sahrens 			    ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2086fa9e4066Sahrens 
2087fa9e4066Sahrens 			if (strcmp(name, searchname) == 0) {
2088fa9e4066Sahrens 				if (found_config != NULL) {
2089fa9e4066Sahrens 					(void) fprintf(stderr, gettext(
2090fa9e4066Sahrens 					    "cannot import '%s': more than "
2091fa9e4066Sahrens 					    "one matching pool\n"), searchname);
2092fa9e4066Sahrens 					(void) fprintf(stderr, gettext(
2093fa9e4066Sahrens 					    "import by numeric ID instead\n"));
209499653d4eSeschrock 					err = B_TRUE;
2095fa9e4066Sahrens 				}
2096fa9e4066Sahrens 				found_config = config;
2097fa9e4066Sahrens 			}
2098fa9e4066Sahrens 		} else {
2099fa9e4066Sahrens 			uint64_t guid;
2100fa9e4066Sahrens 
2101fa9e4066Sahrens 			/*
2102fa9e4066Sahrens 			 * Search for a pool by guid.
2103fa9e4066Sahrens 			 */
2104fa9e4066Sahrens 			verify(nvlist_lookup_uint64(config,
2105fa9e4066Sahrens 			    ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2106fa9e4066Sahrens 
2107fa9e4066Sahrens 			if (guid == searchguid)
2108fa9e4066Sahrens 				found_config = config;
2109fa9e4066Sahrens 		}
2110fa9e4066Sahrens 	}
2111fa9e4066Sahrens 
2112fa9e4066Sahrens 	/*
2113fa9e4066Sahrens 	 * If we were searching for a specific pool, verify that we found a
2114fa9e4066Sahrens 	 * pool, and then do the import.
2115fa9e4066Sahrens 	 */
2116fa9e4066Sahrens 	if (argc != 0 && err == 0) {
2117fa9e4066Sahrens 		if (found_config == NULL) {
2118fa9e4066Sahrens 			(void) fprintf(stderr, gettext("cannot import '%s': "
2119fa9e4066Sahrens 			    "no such pool available\n"), argv[0]);
212099653d4eSeschrock 			err = B_TRUE;
2121fa9e4066Sahrens 		} else {
2122fa9e4066Sahrens 			err |= do_import(found_config, argc == 1 ? NULL :
21234b964adaSGeorge Wilson 			    argv[1], mntopts, props, flags);
2124fa9e4066Sahrens 		}
2125fa9e4066Sahrens 	}
2126fa9e4066Sahrens 
2127fa9e4066Sahrens 	/*
2128fa9e4066Sahrens 	 * If we were just looking for pools, report an error if none were
2129fa9e4066Sahrens 	 * found.
2130fa9e4066Sahrens 	 */
2131fa9e4066Sahrens 	if (argc == 0 && first)
2132fa9e4066Sahrens 		(void) fprintf(stderr,
2133fa9e4066Sahrens 		    gettext("no pools available to import\n"));
2134fa9e4066Sahrens 
2135ecd6cf80Smarks error:
2136ecd6cf80Smarks 	nvlist_free(props);
2137fa9e4066Sahrens 	nvlist_free(pools);
2138468c413aSTim Haley 	nvlist_free(policy);
213999653d4eSeschrock 	free(searchdirs);
2140fa9e4066Sahrens 
2141fa9e4066Sahrens 	return (err ? 1 : 0);
2142fa9e4066Sahrens }
2143fa9e4066Sahrens 
2144fa9e4066Sahrens typedef struct iostat_cbdata {
21454263d13fSGeorge Wilson 	boolean_t cb_verbose;
2146fa9e4066Sahrens 	int cb_namewidth;
21474263d13fSGeorge Wilson 	int cb_iteration;
21484263d13fSGeorge Wilson 	zpool_list_t *cb_list;
2149fa9e4066Sahrens } iostat_cbdata_t;
2150fa9e4066Sahrens 
2151fa9e4066Sahrens static void
2152fa9e4066Sahrens print_iostat_separator(iostat_cbdata_t *cb)
2153fa9e4066Sahrens {
2154fa9e4066Sahrens 	int i = 0;
2155fa9e4066Sahrens 
2156fa9e4066Sahrens 	for (i = 0; i < cb->cb_namewidth; i++)
2157fa9e4066Sahrens 		(void) printf("-");
2158fa9e4066Sahrens 	(void) printf("  -----  -----  -----  -----  -----  -----\n");
2159fa9e4066Sahrens }
2160fa9e4066Sahrens 
2161fa9e4066Sahrens static void
2162fa9e4066Sahrens print_iostat_header(iostat_cbdata_t *cb)
2163fa9e4066Sahrens {
2164fa9e4066Sahrens 	(void) printf("%*s     capacity     operations    bandwidth\n",
2165fa9e4066Sahrens 	    cb->cb_namewidth, "");
2166485bbbf5SGeorge Wilson 	(void) printf("%-*s  alloc   free   read  write   read  write\n",
2167fa9e4066Sahrens 	    cb->cb_namewidth, "pool");
2168fa9e4066Sahrens 	print_iostat_separator(cb);
2169fa9e4066Sahrens }
2170fa9e4066Sahrens 
2171fa9e4066Sahrens /*
2172fa9e4066Sahrens  * Display a single statistic.
2173fa9e4066Sahrens  */
2174990b4856Slling static void
2175fa9e4066Sahrens print_one_stat(uint64_t value)
2176fa9e4066Sahrens {
2177fa9e4066Sahrens 	char buf[64];
2178fa9e4066Sahrens 
2179fa9e4066Sahrens 	zfs_nicenum(value, buf, sizeof (buf));
2180fa9e4066Sahrens 	(void) printf("  %5s", buf);
2181fa9e4066Sahrens }
2182fa9e4066Sahrens 
2183fa9e4066Sahrens /*
2184fa9e4066Sahrens  * Print out all the statistics for the given vdev.  This can either be the
2185fa9e4066Sahrens  * toplevel configuration, or called recursively.  If 'name' is NULL, then this
2186fa9e4066Sahrens  * is a verbose output, and we don't want to display the toplevel pool stats.
2187fa9e4066Sahrens  */
2188fa9e4066Sahrens void
2189c67d9675Seschrock print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2190c67d9675Seschrock     nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2191fa9e4066Sahrens {
2192fa9e4066Sahrens 	nvlist_t **oldchild, **newchild;
2193fa9e4066Sahrens 	uint_t c, children;
2194fa9e4066Sahrens 	vdev_stat_t *oldvs, *newvs;
2195fa9e4066Sahrens 	vdev_stat_t zerovs = { 0 };
2196fa9e4066Sahrens 	uint64_t tdelta;
2197fa9e4066Sahrens 	double scale;
2198afefbcddSeschrock 	char *vname;
2199fa9e4066Sahrens 
2200fa9e4066Sahrens 	if (oldnv != NULL) {
22013f9d6ad7SLin Ling 		verify(nvlist_lookup_uint64_array(oldnv,
22023f9d6ad7SLin Ling 		    ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2203fa9e4066Sahrens 	} else {
2204fa9e4066Sahrens 		oldvs = &zerovs;
2205fa9e4066Sahrens 	}
2206fa9e4066Sahrens 
22073f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2208fa9e4066Sahrens 	    (uint64_t **)&newvs, &c) == 0);
2209fa9e4066Sahrens 
2210fa9e4066Sahrens 	if (strlen(name) + depth > cb->cb_namewidth)
2211fa9e4066Sahrens 		(void) printf("%*s%s", depth, "", name);
2212fa9e4066Sahrens 	else
2213fa9e4066Sahrens 		(void) printf("%*s%s%*s", depth, "", name,
2214fa9e4066Sahrens 		    (int)(cb->cb_namewidth - strlen(name) - depth), "");
2215fa9e4066Sahrens 
2216fa9e4066Sahrens 	tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2217fa9e4066Sahrens 
2218fa9e4066Sahrens 	if (tdelta == 0)
2219fa9e4066Sahrens 		scale = 1.0;
2220fa9e4066Sahrens 	else
2221fa9e4066Sahrens 		scale = (double)NANOSEC / tdelta;
2222fa9e4066Sahrens 
2223fa9e4066Sahrens 	/* only toplevel vdevs have capacity stats */
2224fa9e4066Sahrens 	if (newvs->vs_space == 0) {
2225fa9e4066Sahrens 		(void) printf("      -      -");
2226fa9e4066Sahrens 	} else {
2227fa9e4066Sahrens 		print_one_stat(newvs->vs_alloc);
2228fa9e4066Sahrens 		print_one_stat(newvs->vs_space - newvs->vs_alloc);
2229fa9e4066Sahrens 	}
2230fa9e4066Sahrens 
2231fa9e4066Sahrens 	print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2232fa9e4066Sahrens 	    oldvs->vs_ops[ZIO_TYPE_READ])));
2233fa9e4066Sahrens 
2234fa9e4066Sahrens 	print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2235fa9e4066Sahrens 	    oldvs->vs_ops[ZIO_TYPE_WRITE])));
2236fa9e4066Sahrens 
2237fa9e4066Sahrens 	print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2238fa9e4066Sahrens 	    oldvs->vs_bytes[ZIO_TYPE_READ])));
2239fa9e4066Sahrens 
2240fa9e4066Sahrens 	print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2241fa9e4066Sahrens 	    oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2242fa9e4066Sahrens 
2243fa9e4066Sahrens 	(void) printf("\n");
2244fa9e4066Sahrens 
2245fa9e4066Sahrens 	if (!cb->cb_verbose)
2246fa9e4066Sahrens 		return;
2247fa9e4066Sahrens 
2248fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2249fa9e4066Sahrens 	    &newchild, &children) != 0)
2250fa9e4066Sahrens 		return;
2251fa9e4066Sahrens 
2252fa9e4066Sahrens 	if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2253fa9e4066Sahrens 	    &oldchild, &c) != 0)
2254fa9e4066Sahrens 		return;
2255fa9e4066Sahrens 
2256afefbcddSeschrock 	for (c = 0; c < children; c++) {
22579d439f90SMike Harsch 		uint64_t ishole = B_FALSE, islog = B_FALSE;
22583f9d6ad7SLin Ling 
22599d439f90SMike Harsch 		(void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
22609d439f90SMike Harsch 		    &ishole);
22619d439f90SMike Harsch 
22629d439f90SMike Harsch 		(void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
22639d439f90SMike Harsch 		    &islog);
22649d439f90SMike Harsch 
22659d439f90SMike Harsch 		if (ishole || islog)
22663f9d6ad7SLin Ling 			continue;
22673f9d6ad7SLin Ling 
226888ecc943SGeorge Wilson 		vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2269c67d9675Seschrock 		print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2270afefbcddSeschrock 		    newchild[c], cb, depth + 2);
2271afefbcddSeschrock 		free(vname);
2272afefbcddSeschrock 	}
2273fa94a07fSbrendan 
2274fa94a07fSbrendan 	/*
22759d439f90SMike Harsch 	 * Log device section
22769d439f90SMike Harsch 	 */
22779d439f90SMike Harsch 
22789d439f90SMike Harsch 	if (num_logs(newnv) > 0) {
22799d439f90SMike Harsch 		(void) printf("%-*s      -      -      -      -      -      "
22809d439f90SMike Harsch 		    "-\n", cb->cb_namewidth, "logs");
22819d439f90SMike Harsch 
22829d439f90SMike Harsch 		for (c = 0; c < children; c++) {
22839d439f90SMike Harsch 			uint64_t islog = B_FALSE;
22849d439f90SMike Harsch 			(void) nvlist_lookup_uint64(newchild[c],
22859d439f90SMike Harsch 			    ZPOOL_CONFIG_IS_LOG, &islog);
22869d439f90SMike Harsch 
22879d439f90SMike Harsch 			if (islog) {
22889d439f90SMike Harsch 				vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
22899d439f90SMike Harsch 				    B_FALSE);
22909d439f90SMike Harsch 				print_vdev_stats(zhp, vname, oldnv ?
22919d439f90SMike Harsch 				    oldchild[c] : NULL, newchild[c],
22929d439f90SMike Harsch 				    cb, depth + 2);
22939d439f90SMike Harsch 				free(vname);
22949d439f90SMike Harsch 			}
22959d439f90SMike Harsch 		}
22969d439f90SMike Harsch 
22979d439f90SMike Harsch 	}
22989d439f90SMike Harsch 
22999d439f90SMike Harsch 	/*
2300fa94a07fSbrendan 	 * Include level 2 ARC devices in iostat output
2301fa94a07fSbrendan 	 */
2302fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2303fa94a07fSbrendan 	    &newchild, &children) != 0)
2304fa94a07fSbrendan 		return;
2305fa94a07fSbrendan 
2306fa94a07fSbrendan 	if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2307fa94a07fSbrendan 	    &oldchild, &c) != 0)
2308fa94a07fSbrendan 		return;
2309fa94a07fSbrendan 
2310fa94a07fSbrendan 	if (children > 0) {
2311fa94a07fSbrendan 		(void) printf("%-*s      -      -      -      -      -      "
2312fa94a07fSbrendan 		    "-\n", cb->cb_namewidth, "cache");
2313fa94a07fSbrendan 		for (c = 0; c < children; c++) {
231488ecc943SGeorge Wilson 			vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
231588ecc943SGeorge Wilson 			    B_FALSE);
2316fa94a07fSbrendan 			print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2317fa94a07fSbrendan 			    newchild[c], cb, depth + 2);
2318fa94a07fSbrendan 			free(vname);
2319fa94a07fSbrendan 		}
2320fa94a07fSbrendan 	}
2321fa9e4066Sahrens }
2322fa9e4066Sahrens 
2323088e9d47Seschrock static int
2324088e9d47Seschrock refresh_iostat(zpool_handle_t *zhp, void *data)
2325088e9d47Seschrock {
2326088e9d47Seschrock 	iostat_cbdata_t *cb = data;
232794de1d4cSeschrock 	boolean_t missing;
2328088e9d47Seschrock 
2329088e9d47Seschrock 	/*
2330088e9d47Seschrock 	 * If the pool has disappeared, remove it from the list and continue.
2331088e9d47Seschrock 	 */
233294de1d4cSeschrock 	if (zpool_refresh_stats(zhp, &missing) != 0)
233394de1d4cSeschrock 		return (-1);
233494de1d4cSeschrock 
233594de1d4cSeschrock 	if (missing)
2336088e9d47Seschrock 		pool_list_remove(cb->cb_list, zhp);
2337088e9d47Seschrock 
2338088e9d47Seschrock 	return (0);
2339088e9d47Seschrock }
2340088e9d47Seschrock 
2341fa9e4066Sahrens /*
2342fa9e4066Sahrens  * Callback to print out the iostats for the given pool.
2343fa9e4066Sahrens  */
2344fa9e4066Sahrens int
2345fa9e4066Sahrens print_iostat(zpool_handle_t *zhp, void *data)
2346fa9e4066Sahrens {
2347fa9e4066Sahrens 	iostat_cbdata_t *cb = data;
2348fa9e4066Sahrens 	nvlist_t *oldconfig, *newconfig;
2349fa9e4066Sahrens 	nvlist_t *oldnvroot, *newnvroot;
2350fa9e4066Sahrens 
2351088e9d47Seschrock 	newconfig = zpool_get_config(zhp, &oldconfig);
2352fa9e4066Sahrens 
2353088e9d47Seschrock 	if (cb->cb_iteration == 1)
2354fa9e4066Sahrens 		oldconfig = NULL;
2355fa9e4066Sahrens 
2356fa9e4066Sahrens 	verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2357fa9e4066Sahrens 	    &newnvroot) == 0);
2358fa9e4066Sahrens 
2359088e9d47Seschrock 	if (oldconfig == NULL)
2360fa9e4066Sahrens 		oldnvroot = NULL;
2361088e9d47Seschrock 	else
2362088e9d47Seschrock 		verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2363088e9d47Seschrock 		    &oldnvroot) == 0);
2364fa9e4066Sahrens 
2365fa9e4066Sahrens 	/*
2366fa9e4066Sahrens 	 * Print out the statistics for the pool.
2367fa9e4066Sahrens 	 */
2368c67d9675Seschrock 	print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2369fa9e4066Sahrens 
2370fa9e4066Sahrens 	if (cb->cb_verbose)
2371fa9e4066Sahrens 		print_iostat_separator(cb);
2372fa9e4066Sahrens 
2373fa9e4066Sahrens 	return (0);
2374fa9e4066Sahrens }
2375fa9e4066Sahrens 
2376fa9e4066Sahrens int
2377fa9e4066Sahrens get_namewidth(zpool_handle_t *zhp, void *data)
2378fa9e4066Sahrens {
2379fa9e4066Sahrens 	iostat_cbdata_t *cb = data;
2380fa9e4066Sahrens 	nvlist_t *config, *nvroot;
2381fa9e4066Sahrens 
2382088e9d47Seschrock 	if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2383fa9e4066Sahrens 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2384fa9e4066Sahrens 		    &nvroot) == 0);
2385fa9e4066Sahrens 		if (!cb->cb_verbose)
2386fa9e4066Sahrens 			cb->cb_namewidth = strlen(zpool_get_name(zhp));
2387fa9e4066Sahrens 		else
2388e1d5e507SFrederik Wessels 			cb->cb_namewidth = max_width(zhp, nvroot, 0,
2389e1d5e507SFrederik Wessels 			    cb->cb_namewidth);
2390fa9e4066Sahrens 	}
2391fa9e4066Sahrens 
2392fa9e4066Sahrens 	/*
2393fa9e4066Sahrens 	 * The width must fall into the range [10,38].  The upper limit is the
2394fa9e4066Sahrens 	 * maximum we can have and still fit in 80 columns.
2395fa9e4066Sahrens 	 */
2396fa9e4066Sahrens 	if (cb->cb_namewidth < 10)
2397fa9e4066Sahrens 		cb->cb_namewidth = 10;
2398fa9e4066Sahrens 	if (cb->cb_namewidth > 38)
2399fa9e4066Sahrens 		cb->cb_namewidth = 38;
2400fa9e4066Sahrens 
2401fa9e4066Sahrens 	return (0);
2402fa9e4066Sahrens }
2403fa9e4066Sahrens 
2404fa9e4066Sahrens /*
24053f9d6ad7SLin Ling  * Parse the input string, get the 'interval' and 'count' value if there is one.
2406fa9e4066Sahrens  */
24073f9d6ad7SLin Ling static void
24083f9d6ad7SLin Ling get_interval_count(int *argcp, char **argv, unsigned long *iv,
24093f9d6ad7SLin Ling     unsigned long *cnt)
2410fa9e4066Sahrens {
2411fa9e4066Sahrens 	unsigned long interval = 0, count = 0;
24123f9d6ad7SLin Ling 	int argc = *argcp, errno;
2413fa9e4066Sahrens 
2414fa9e4066Sahrens 	/*
2415fa9e4066Sahrens 	 * Determine if the last argument is an integer or a pool name
2416fa9e4066Sahrens 	 */
2417fa9e4066Sahrens 	if (argc > 0 && isdigit(argv[argc - 1][0])) {
2418fa9e4066Sahrens 		char *end;
2419fa9e4066Sahrens 
2420fa9e4066Sahrens 		errno = 0;
2421fa9e4066Sahrens 		interval = strtoul(argv[argc - 1], &end, 10);
2422fa9e4066Sahrens 
2423fa9e4066Sahrens 		if (*end == '\0' && errno == 0) {
2424fa9e4066Sahrens 			if (interval == 0) {
2425fa9e4066Sahrens 				(void) fprintf(stderr, gettext("interval "
2426fa9e4066Sahrens 				    "cannot be zero\n"));
242799653d4eSeschrock 				usage(B_FALSE);
2428fa9e4066Sahrens 			}
2429fa9e4066Sahrens 			/*
2430fa9e4066Sahrens 			 * Ignore the last parameter
2431fa9e4066Sahrens 			 */
2432fa9e4066Sahrens 			argc--;
2433fa9e4066Sahrens 		} else {
2434fa9e4066Sahrens 			/*
2435fa9e4066Sahrens 			 * If this is not a valid number, just plow on.  The
2436fa9e4066Sahrens 			 * user will get a more informative error message later
2437fa9e4066Sahrens 			 * on.
2438fa9e4066Sahrens 			 */
2439fa9e4066Sahrens 			interval = 0;
2440fa9e4066Sahrens 		}
2441fa9e4066Sahrens 	}
2442fa9e4066Sahrens 
2443fa9e4066Sahrens 	/*
2444fa9e4066Sahrens 	 * If the last argument is also an integer, then we have both a count
24453f9d6ad7SLin Ling 	 * and an interval.
2446fa9e4066Sahrens 	 */
2447fa9e4066Sahrens 	if (argc > 0 && isdigit(argv[argc - 1][0])) {
2448fa9e4066Sahrens 		char *end;
2449fa9e4066Sahrens 
2450fa9e4066Sahrens 		errno = 0;
2451fa9e4066Sahrens 		count = interval;
2452fa9e4066Sahrens 		interval = strtoul(argv[argc - 1], &end, 10);
2453fa9e4066Sahrens 
2454fa9e4066Sahrens 		if (*end == '\0' && errno == 0) {
2455fa9e4066Sahrens 			if (interval == 0) {
2456fa9e4066Sahrens 				(void) fprintf(stderr, gettext("interval "
2457fa9e4066Sahrens 				    "cannot be zero\n"));
245899653d4eSeschrock 				usage(B_FALSE);
2459fa9e4066Sahrens 			}
2460fa9e4066Sahrens 
2461fa9e4066Sahrens 			/*
2462fa9e4066Sahrens 			 * Ignore the last parameter
2463fa9e4066Sahrens 			 */
2464fa9e4066Sahrens 			argc--;
2465fa9e4066Sahrens 		} else {
2466fa9e4066Sahrens 			interval = 0;
2467fa9e4066Sahrens 		}
2468fa9e4066Sahrens 	}
2469fa9e4066Sahrens 
24703f9d6ad7SLin Ling 	*iv = interval;
24713f9d6ad7SLin Ling 	*cnt = count;
24723f9d6ad7SLin Ling 	*argcp = argc;
24733f9d6ad7SLin Ling }
24743f9d6ad7SLin Ling 
24753f9d6ad7SLin Ling static void
24763f9d6ad7SLin Ling get_timestamp_arg(char c)
24773f9d6ad7SLin Ling {
24783f9d6ad7SLin Ling 	if (c == 'u')
24793f9d6ad7SLin Ling 		timestamp_fmt = UDATE;
24803f9d6ad7SLin Ling 	else if (c == 'd')
24813f9d6ad7SLin Ling 		timestamp_fmt = DDATE;
24823f9d6ad7SLin Ling 	else
24833f9d6ad7SLin Ling 		usage(B_FALSE);
24843f9d6ad7SLin Ling }
24853f9d6ad7SLin Ling 
24863f9d6ad7SLin Ling /*
24873f9d6ad7SLin Ling  * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
24883f9d6ad7SLin Ling  *
24893f9d6ad7SLin Ling  *	-v	Display statistics for individual vdevs
24903f9d6ad7SLin Ling  *	-T	Display a timestamp in date(1) or Unix format
24913f9d6ad7SLin Ling  *
24923f9d6ad7SLin Ling  * This command can be tricky because we want to be able to deal with pool
24933f9d6ad7SLin Ling  * creation/destruction as well as vdev configuration changes.  The bulk of this
24943f9d6ad7SLin Ling  * processing is handled by the pool_list_* routines in zpool_iter.c.  We rely
24953f9d6ad7SLin Ling  * on pool_list_update() to detect the addition of new pools.  Configuration
24963f9d6ad7SLin Ling  * changes are all handled within libzfs.
24973f9d6ad7SLin Ling  */
24983f9d6ad7SLin Ling int
24993f9d6ad7SLin Ling zpool_do_iostat(int argc, char **argv)
25003f9d6ad7SLin Ling {
25013f9d6ad7SLin Ling 	int c;
25023f9d6ad7SLin Ling 	int ret;
25033f9d6ad7SLin Ling 	int npools;
25043f9d6ad7SLin Ling 	unsigned long interval = 0, count = 0;
25053f9d6ad7SLin Ling 	zpool_list_t *list;
25063f9d6ad7SLin Ling 	boolean_t verbose = B_FALSE;
25073f9d6ad7SLin Ling 	iostat_cbdata_t cb;
25083f9d6ad7SLin Ling 
25093f9d6ad7SLin Ling 	/* check options */
25103f9d6ad7SLin Ling 	while ((c = getopt(argc, argv, "T:v")) != -1) {
25113f9d6ad7SLin Ling 		switch (c) {
25123f9d6ad7SLin Ling 		case 'T':
25133f9d6ad7SLin Ling 			get_timestamp_arg(*optarg);
25143f9d6ad7SLin Ling 			break;
25153f9d6ad7SLin Ling 		case 'v':
25163f9d6ad7SLin Ling 			verbose = B_TRUE;
25173f9d6ad7SLin Ling 			break;
25183f9d6ad7SLin Ling 		case '?':
25193f9d6ad7SLin Ling 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
25203f9d6ad7SLin Ling 			    optopt);
25213f9d6ad7SLin Ling 			usage(B_FALSE);
25223f9d6ad7SLin Ling 		}
25233f9d6ad7SLin Ling 	}
25243f9d6ad7SLin Ling 
25253f9d6ad7SLin Ling 	argc -= optind;
25263f9d6ad7SLin Ling 	argv += optind;
25273f9d6ad7SLin Ling 
25283f9d6ad7SLin Ling 	get_interval_count(&argc, argv, &interval, &count);
25293f9d6ad7SLin Ling 
2530fa9e4066Sahrens 	/*
2531fa9e4066Sahrens 	 * Construct the list of all interesting pools.
2532fa9e4066Sahrens 	 */
2533fa9e4066Sahrens 	ret = 0;
2534b1b8ab34Slling 	if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2535fa9e4066Sahrens 		return (1);
2536fa9e4066Sahrens 
253799653d4eSeschrock 	if (pool_list_count(list) == 0 && argc != 0) {
253899653d4eSeschrock 		pool_list_free(list);
2539fa9e4066Sahrens 		return (1);
254099653d4eSeschrock 	}
2541fa9e4066Sahrens 
2542fa9e4066Sahrens 	if (pool_list_count(list) == 0 && interval == 0) {
254399653d4eSeschrock 		pool_list_free(list);
2544fa9e4066Sahrens 		(void) fprintf(stderr, gettext("no pools available\n"));
2545fa9e4066Sahrens 		return (1);
2546fa9e4066Sahrens 	}
2547fa9e4066Sahrens 
2548fa9e4066Sahrens 	/*
2549fa9e4066Sahrens 	 * Enter the main iostat loop.
2550fa9e4066Sahrens 	 */
2551fa9e4066Sahrens 	cb.cb_list = list;
2552fa9e4066Sahrens 	cb.cb_verbose = verbose;
2553fa9e4066Sahrens 	cb.cb_iteration = 0;
2554fa9e4066Sahrens 	cb.cb_namewidth = 0;
2555fa9e4066Sahrens 
2556fa9e4066Sahrens 	for (;;) {
2557fa9e4066Sahrens 		pool_list_update(list);
2558fa9e4066Sahrens 
2559fa9e4066Sahrens 		if ((npools = pool_list_count(list)) == 0)
2560fa9e4066Sahrens 			break;
2561fa9e4066Sahrens 
2562fa9e4066Sahrens 		/*
2563088e9d47Seschrock 		 * Refresh all statistics.  This is done as an explicit step
2564088e9d47Seschrock 		 * before calculating the maximum name width, so that any
2565088e9d47Seschrock 		 * configuration changes are properly accounted for.
2566088e9d47Seschrock 		 */
256799653d4eSeschrock 		(void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2568088e9d47Seschrock 
2569088e9d47Seschrock 		/*
2570fa9e4066Sahrens 		 * Iterate over all pools to determine the maximum width
2571fa9e4066Sahrens 		 * for the pool / device name column across all pools.
2572fa9e4066Sahrens 		 */
2573fa9e4066Sahrens 		cb.cb_namewidth = 0;
257499653d4eSeschrock 		(void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2575fa9e4066Sahrens 
257626fd7700SKrishnendu Sadhukhan - Sun Microsystems 		if (timestamp_fmt != NODATE)
257726fd7700SKrishnendu Sadhukhan - Sun Microsystems 			print_timestamp(timestamp_fmt);
257826fd7700SKrishnendu Sadhukhan - Sun Microsystems 
2579fa9e4066Sahrens 		/*
2580fa9e4066Sahrens 		 * If it's the first time, or verbose mode, print the header.
2581fa9e4066Sahrens 		 */
2582fa9e4066Sahrens 		if (++cb.cb_iteration == 1 || verbose)
2583fa9e4066Sahrens 			print_iostat_header(&cb);
2584fa9e4066Sahrens 
258599653d4eSeschrock 		(void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2586fa9e4066Sahrens 
2587fa9e4066Sahrens 		/*
2588fa9e4066Sahrens 		 * If there's more than one pool, and we're not in verbose mode
2589fa9e4066Sahrens 		 * (which prints a separator for us), then print a separator.
2590fa9e4066Sahrens 		 */
2591fa9e4066Sahrens 		if (npools > 1 && !verbose)
2592fa9e4066Sahrens 			print_iostat_separator(&cb);
2593fa9e4066Sahrens 
2594fa9e4066Sahrens 		if (verbose)
2595fa9e4066Sahrens 			(void) printf("\n");
2596fa9e4066Sahrens 
259739c23413Seschrock 		/*
259839c23413Seschrock 		 * Flush the output so that redirection to a file isn't buffered
259939c23413Seschrock 		 * indefinitely.
260039c23413Seschrock 		 */
260139c23413Seschrock 		(void) fflush(stdout);
260239c23413Seschrock 
2603fa9e4066Sahrens 		if (interval == 0)
2604fa9e4066Sahrens 			break;
2605fa9e4066Sahrens 
2606fa9e4066Sahrens 		if (count != 0 && --count == 0)
2607fa9e4066Sahrens 			break;
2608fa9e4066Sahrens 
2609fa9e4066Sahrens 		(void) sleep(interval);
2610fa9e4066Sahrens 	}
2611fa9e4066Sahrens 
2612fa9e4066Sahrens 	pool_list_free(list);
2613fa9e4066Sahrens 
2614fa9e4066Sahrens 	return (ret);
2615fa9e4066Sahrens }
2616fa9e4066Sahrens 
2617fa9e4066Sahrens typedef struct list_cbdata {
26184263d13fSGeorge Wilson 	boolean_t	cb_verbose;
26194263d13fSGeorge Wilson 	int		cb_namewidth;
262099653d4eSeschrock 	boolean_t	cb_scripted;
2621990b4856Slling 	zprop_list_t	*cb_proplist;
2622c58b3526SAdam Stevko 	boolean_t	cb_literal;
2623fa9e4066Sahrens } list_cbdata_t;
2624fa9e4066Sahrens 
2625fa9e4066Sahrens /*
2626fa9e4066Sahrens  * Given a list of columns to display, output appropriate headers for each one.
2627fa9e4066Sahrens  */
2628990b4856Slling static void
26294263d13fSGeorge Wilson print_header(list_cbdata_t *cb)
2630fa9e4066Sahrens {
26314263d13fSGeorge Wilson 	zprop_list_t *pl = cb->cb_proplist;
2632ad135b5dSChristopher Siden 	char headerbuf[ZPOOL_MAXPROPLEN];
2633990b4856Slling 	const char *header;
2634990b4856Slling 	boolean_t first = B_TRUE;
2635990b4856Slling 	boolean_t right_justify;
26364263d13fSGeorge Wilson 	size_t width = 0;
2637fa9e4066Sahrens 
2638990b4856Slling 	for (; pl != NULL; pl = pl->pl_next) {
26394263d13fSGeorge Wilson 		width = pl->pl_width;
26404263d13fSGeorge Wilson 		if (first && cb->cb_verbose) {
26414263d13fSGeorge Wilson 			/*
26424263d13fSGeorge Wilson 			 * Reset the width to accommodate the verbose listing
26434263d13fSGeorge Wilson 			 * of devices.
26444263d13fSGeorge Wilson 			 */
26454263d13fSGeorge Wilson 			width = cb->cb_namewidth;
26464263d13fSGeorge Wilson 		}
26474263d13fSGeorge Wilson 
2648990b4856Slling 		if (!first)
2649fa9e4066Sahrens 			(void) printf("  ");
2650fa9e4066Sahrens 		else
2651990b4856Slling 			first = B_FALSE;
2652fa9e4066Sahrens 
2653ad135b5dSChristopher Siden 		right_justify = B_FALSE;
2654ad135b5dSChristopher Siden 		if (pl->pl_prop != ZPROP_INVAL) {
2655990b4856Slling 			header = zpool_prop_column_name(pl->pl_prop);
2656990b4856Slling 			right_justify = zpool_prop_align_right(pl->pl_prop);
2657ad135b5dSChristopher Siden 		} else {
2658ad135b5dSChristopher Siden 			int i;
2659ad135b5dSChristopher Siden 
2660ad135b5dSChristopher Siden 			for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2661ad135b5dSChristopher Siden 				headerbuf[i] = toupper(pl->pl_user_prop[i]);
2662ad135b5dSChristopher Siden 			headerbuf[i] = '\0';
2663ad135b5dSChristopher Siden 			header = headerbuf;
2664ad135b5dSChristopher Siden 		}
2665990b4856Slling 
2666990b4856Slling 		if (pl->pl_next == NULL && !right_justify)
2667990b4856Slling 			(void) printf("%s", header);
2668990b4856Slling 		else if (right_justify)
26694263d13fSGeorge Wilson 			(void) printf("%*s", width, header);
2670990b4856Slling 		else
26714263d13fSGeorge Wilson 			(void) printf("%-*s", width, header);
26724263d13fSGeorge Wilson 
2673fa9e4066Sahrens 	}
2674fa9e4066Sahrens 
2675fa9e4066Sahrens 	(void) printf("\n");
2676fa9e4066Sahrens }
2677fa9e4066Sahrens 
2678990b4856Slling /*
2679990b4856Slling  * Given a pool and a list of properties, print out all the properties according
2680990b4856Slling  * to the described layout.
2681990b4856Slling  */
2682990b4856Slling static void
26834263d13fSGeorge Wilson print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2684990b4856Slling {
26854263d13fSGeorge Wilson 	zprop_list_t *pl = cb->cb_proplist;
2686990b4856Slling 	boolean_t first = B_TRUE;
2687990b4856Slling 	char property[ZPOOL_MAXPROPLEN];
2688990b4856Slling 	char *propstr;
2689990b4856Slling 	boolean_t right_justify;
26904263d13fSGeorge Wilson 	size_t width;
2691990b4856Slling 
2692990b4856Slling 	for (; pl != NULL; pl = pl->pl_next) {
26934263d13fSGeorge Wilson 
26944263d13fSGeorge Wilson 		width = pl->pl_width;
26954263d13fSGeorge Wilson 		if (first && cb->cb_verbose) {
26964263d13fSGeorge Wilson 			/*
26974263d13fSGeorge Wilson 			 * Reset the width to accommodate the verbose listing
26984263d13fSGeorge Wilson 			 * of devices.
26994263d13fSGeorge Wilson 			 */
27004263d13fSGeorge Wilson 			width = cb->cb_namewidth;
27014263d13fSGeorge Wilson 		}
27024263d13fSGeorge Wilson 
2703990b4856Slling 		if (!first) {
27044263d13fSGeorge Wilson 			if (cb->cb_scripted)
2705990b4856Slling 				(void) printf("\t");
2706990b4856Slling 			else
2707990b4856Slling 				(void) printf("  ");
2708990b4856Slling 		} else {
2709990b4856Slling 			first = B_FALSE;
2710990b4856Slling 		}
2711990b4856Slling 
2712990b4856Slling 		right_justify = B_FALSE;
2713990b4856Slling 		if (pl->pl_prop != ZPROP_INVAL) {
27147a09f97bSGeorge Wilson 			if (zpool_get_prop(zhp, pl->pl_prop, property,
2715c58b3526SAdam Stevko 			    sizeof (property), NULL, cb->cb_literal) != 0)
2716990b4856Slling 				propstr = "-";
2717990b4856Slling 			else
2718990b4856Slling 				propstr = property;
2719990b4856Slling 
2720990b4856Slling 			right_justify = zpool_prop_align_right(pl->pl_prop);
2721ad135b5dSChristopher Siden 		} else if ((zpool_prop_feature(pl->pl_user_prop) ||
2722ad135b5dSChristopher Siden 		    zpool_prop_unsupported(pl->pl_user_prop)) &&
2723ad135b5dSChristopher Siden 		    zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2724ad135b5dSChristopher Siden 		    sizeof (property)) == 0) {
2725ad135b5dSChristopher Siden 			propstr = property;
2726990b4856Slling 		} else {
2727990b4856Slling 			propstr = "-";
2728990b4856Slling 		}
2729990b4856Slling 
2730990b4856Slling 
2731990b4856Slling 		/*
2732990b4856Slling 		 * If this is being called in scripted mode, or if this is the
2733990b4856Slling 		 * last column and it is left-justified, don't include a width
2734990b4856Slling 		 * format specifier.
2735990b4856Slling 		 */
27364263d13fSGeorge Wilson 		if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2737990b4856Slling 			(void) printf("%s", propstr);
2738990b4856Slling 		else if (right_justify)
2739990b4856Slling 			(void) printf("%*s", width, propstr);
2740990b4856Slling 		else
2741990b4856Slling 			(void) printf("%-*s", width, propstr);
2742990b4856Slling 	}
2743990b4856Slling 
2744990b4856Slling 	(void) printf("\n");
2745990b4856Slling }
2746990b4856Slling 
27474263d13fSGeorge Wilson static void
27487a09f97bSGeorge Wilson print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
27497a09f97bSGeorge Wilson     boolean_t valid)
27504263d13fSGeorge Wilson {
27514263d13fSGeorge Wilson 	char propval[64];
27524263d13fSGeorge Wilson 	boolean_t fixed;
27534263d13fSGeorge Wilson 	size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
27544263d13fSGeorge Wilson 
27557a09f97bSGeorge Wilson 	switch (prop) {
27567a09f97bSGeorge Wilson 	case ZPOOL_PROP_EXPANDSZ:
27577a09f97bSGeorge Wilson 		if (value == 0)
27584263d13fSGeorge Wilson 			(void) strlcpy(propval, "-", sizeof (propval));
27592e4c9986SGeorge Wilson 		else
27602e4c9986SGeorge Wilson 			zfs_nicenum(value, propval, sizeof (propval));
27617a09f97bSGeorge Wilson 		break;
27627a09f97bSGeorge Wilson 	case ZPOOL_PROP_FRAGMENTATION:
27637a09f97bSGeorge Wilson 		if (value == ZFS_FRAG_INVALID) {
27647a09f97bSGeorge Wilson 			(void) strlcpy(propval, "-", sizeof (propval));
27657a09f97bSGeorge Wilson 		} else {
27667a09f97bSGeorge Wilson 			(void) snprintf(propval, sizeof (propval), "%llu%%",
27677a09f97bSGeorge Wilson 			    value);
27687a09f97bSGeorge Wilson 		}
27697a09f97bSGeorge Wilson 		break;
27707a09f97bSGeorge Wilson 	case ZPOOL_PROP_CAPACITY:
27717a09f97bSGeorge Wilson 		(void) snprintf(propval, sizeof (propval), "%llu%%", value);
27727a09f97bSGeorge Wilson 		break;
27737a09f97bSGeorge Wilson 	default:
27747a09f97bSGeorge Wilson 		zfs_nicenum(value, propval, sizeof (propval));
27757a09f97bSGeorge Wilson 	}
27767a09f97bSGeorge Wilson 
27777a09f97bSGeorge Wilson 	if (!valid)
27787a09f97bSGeorge Wilson 		(void) strlcpy(propval, "-", sizeof (propval));
27794263d13fSGeorge Wilson 
27804263d13fSGeorge Wilson 	if (scripted)
27814263d13fSGeorge Wilson 		(void) printf("\t%s", propval);
27824263d13fSGeorge Wilson 	else
27834263d13fSGeorge Wilson 		(void) printf("  %*s", width, propval);
27844263d13fSGeorge Wilson }
27854263d13fSGeorge Wilson 
27864263d13fSGeorge Wilson void
27874263d13fSGeorge Wilson print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
27884263d13fSGeorge Wilson     list_cbdata_t *cb, int depth)
27894263d13fSGeorge Wilson {
27904263d13fSGeorge Wilson 	nvlist_t **child;
27914263d13fSGeorge Wilson 	vdev_stat_t *vs;
27924263d13fSGeorge Wilson 	uint_t c, children;
27934263d13fSGeorge Wilson 	char *vname;
27944263d13fSGeorge Wilson 	boolean_t scripted = cb->cb_scripted;
2795*52244c09SJohn Wren Kennedy 	uint64_t islog = B_FALSE;
2796*52244c09SJohn Wren Kennedy 	boolean_t haslog = B_FALSE;
2797*52244c09SJohn Wren Kennedy 	char *dashes = "%-*s      -      -      -         -      -      -\n";
27984263d13fSGeorge Wilson 
27994263d13fSGeorge Wilson 	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
28004263d13fSGeorge Wilson 	    (uint64_t **)&vs, &c) == 0);
28014263d13fSGeorge Wilson 
28024263d13fSGeorge Wilson 	if (name != NULL) {
28037a09f97bSGeorge Wilson 		boolean_t toplevel = (vs->vs_space != 0);
28047a09f97bSGeorge Wilson 		uint64_t cap;
28057a09f97bSGeorge Wilson 
28064263d13fSGeorge Wilson 		if (scripted)
28074263d13fSGeorge Wilson 			(void) printf("\t%s", name);
28084263d13fSGeorge Wilson 		else if (strlen(name) + depth > cb->cb_namewidth)
28094263d13fSGeorge Wilson 			(void) printf("%*s%s", depth, "", name);
28104263d13fSGeorge Wilson 		else
28114263d13fSGeorge Wilson 			(void) printf("%*s%s%*s", depth, "", name,
28124263d13fSGeorge Wilson 			    (int)(cb->cb_namewidth - strlen(name) - depth), "");
28134263d13fSGeorge Wilson 
28147a09f97bSGeorge Wilson 		/*
28157a09f97bSGeorge Wilson 		 * Print the properties for the individual vdevs. Some
28167a09f97bSGeorge Wilson 		 * properties are only applicable to toplevel vdevs. The
28177a09f97bSGeorge Wilson 		 * 'toplevel' boolean value is passed to the print_one_column()
28187a09f97bSGeorge Wilson 		 * to indicate that the value is valid.
28197a09f97bSGeorge Wilson 		 */
28207a09f97bSGeorge Wilson 		print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted,
28217a09f97bSGeorge Wilson 		    toplevel);
28227a09f97bSGeorge Wilson 		print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
28237a09f97bSGeorge Wilson 		    toplevel);
28247a09f97bSGeorge Wilson 		print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
28257a09f97bSGeorge Wilson 		    scripted, toplevel);
28267a09f97bSGeorge Wilson 		print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
28277a09f97bSGeorge Wilson 		    B_TRUE);
28282e4c9986SGeorge Wilson 		print_one_column(ZPOOL_PROP_FRAGMENTATION,
28297a09f97bSGeorge Wilson 		    vs->vs_fragmentation, scripted,
28307a09f97bSGeorge Wilson 		    (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel));
28317a09f97bSGeorge Wilson 		cap = (vs->vs_space == 0) ? 0 :
28327a09f97bSGeorge Wilson 		    (vs->vs_alloc * 100 / vs->vs_space);
28337a09f97bSGeorge Wilson 		print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel);
28344263d13fSGeorge Wilson 		(void) printf("\n");
28354263d13fSGeorge Wilson 	}
28364263d13fSGeorge Wilson 
28374263d13fSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
28384263d13fSGeorge Wilson 	    &child, &children) != 0)
28394263d13fSGeorge Wilson 		return;
28404263d13fSGeorge Wilson 
28414263d13fSGeorge Wilson 	for (c = 0; c < children; c++) {
28424263d13fSGeorge Wilson 		uint64_t ishole = B_FALSE;
28434263d13fSGeorge Wilson 
28444263d13fSGeorge Wilson 		if (nvlist_lookup_uint64(child[c],
28454263d13fSGeorge Wilson 		    ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
28464263d13fSGeorge Wilson 			continue;
28474263d13fSGeorge Wilson 
2848*52244c09SJohn Wren Kennedy 		if (nvlist_lookup_uint64(child[c],
2849*52244c09SJohn Wren Kennedy 		    ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog) {
2850*52244c09SJohn Wren Kennedy 			haslog = B_TRUE;
2851*52244c09SJohn Wren Kennedy 			continue;
2852*52244c09SJohn Wren Kennedy 		}
2853*52244c09SJohn Wren Kennedy 
28544263d13fSGeorge Wilson 		vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
28554263d13fSGeorge Wilson 		print_list_stats(zhp, vname, child[c], cb, depth + 2);
28564263d13fSGeorge Wilson 		free(vname);
28574263d13fSGeorge Wilson 	}
28584263d13fSGeorge Wilson 
2859*52244c09SJohn Wren Kennedy 	if (haslog == B_TRUE) {
2860*52244c09SJohn Wren Kennedy 		/* LINTED E_SEC_PRINTF_VAR_FMT */
2861*52244c09SJohn Wren Kennedy 		(void) printf(dashes, cb->cb_namewidth, "log");
28624263d13fSGeorge Wilson 		for (c = 0; c < children; c++) {
2863*52244c09SJohn Wren Kennedy 			if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2864*52244c09SJohn Wren Kennedy 			    &islog) != 0 || !islog)
2865*52244c09SJohn Wren Kennedy 				continue;
2866*52244c09SJohn Wren Kennedy 			vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
2867*52244c09SJohn Wren Kennedy 			print_list_stats(zhp, vname, child[c], cb, depth + 2);
2868*52244c09SJohn Wren Kennedy 			free(vname);
2869*52244c09SJohn Wren Kennedy 		}
2870*52244c09SJohn Wren Kennedy 	}
2871*52244c09SJohn Wren Kennedy 
2872*52244c09SJohn Wren Kennedy 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2873*52244c09SJohn Wren Kennedy 	    &child, &children) == 0 && children > 0) {
2874*52244c09SJohn Wren Kennedy 		/* LINTED E_SEC_PRINTF_VAR_FMT */
2875*52244c09SJohn Wren Kennedy 		(void) printf(dashes, cb->cb_namewidth, "cache");
2876*52244c09SJohn Wren Kennedy 		for (c = 0; c < children; c++) {
2877*52244c09SJohn Wren Kennedy 			vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
2878*52244c09SJohn Wren Kennedy 			print_list_stats(zhp, vname, child[c], cb, depth + 2);
2879*52244c09SJohn Wren Kennedy 			free(vname);
2880*52244c09SJohn Wren Kennedy 		}
2881*52244c09SJohn Wren Kennedy 	}
2882*52244c09SJohn Wren Kennedy 
2883*52244c09SJohn Wren Kennedy 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child,
2884*52244c09SJohn Wren Kennedy 	    &children) == 0 && children > 0) {
2885*52244c09SJohn Wren Kennedy 		/* LINTED E_SEC_PRINTF_VAR_FMT */
2886*52244c09SJohn Wren Kennedy 		(void) printf(dashes, cb->cb_namewidth, "spare");
2887*52244c09SJohn Wren Kennedy 		for (c = 0; c < children; c++) {
2888*52244c09SJohn Wren Kennedy 			vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
28894263d13fSGeorge Wilson 			print_list_stats(zhp, vname, child[c], cb, depth + 2);
28904263d13fSGeorge Wilson 			free(vname);
28914263d13fSGeorge Wilson 		}
28924263d13fSGeorge Wilson 	}
28934263d13fSGeorge Wilson }
28944263d13fSGeorge Wilson 
28954263d13fSGeorge Wilson 
2896990b4856Slling /*
2897990b4856Slling  * Generic callback function to list a pool.
2898990b4856Slling  */
2899fa9e4066Sahrens int
2900fa9e4066Sahrens list_callback(zpool_handle_t *zhp, void *data)
2901fa9e4066Sahrens {
2902fa9e4066Sahrens 	list_cbdata_t *cbp = data;
29034263d13fSGeorge Wilson 	nvlist_t *config;
29044263d13fSGeorge Wilson 	nvlist_t *nvroot;
2905fa9e4066Sahrens 
29064263d13fSGeorge Wilson 	config = zpool_get_config(zhp, NULL);
2907fa9e4066Sahrens 
29084263d13fSGeorge Wilson 	print_pool(zhp, cbp);
29094263d13fSGeorge Wilson 	if (!cbp->cb_verbose)
29104263d13fSGeorge Wilson 		return (0);
29114263d13fSGeorge Wilson 
29124263d13fSGeorge Wilson 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
29134263d13fSGeorge Wilson 	    &nvroot) == 0);
29144263d13fSGeorge Wilson 	print_list_stats(zhp, NULL, nvroot, cbp, 0);
2915fa9e4066Sahrens 
2916fa9e4066Sahrens 	return (0);
2917fa9e4066Sahrens }
2918fa9e4066Sahrens 
2919fa9e4066Sahrens /*
2920c58b3526SAdam Stevko  * zpool list [-Hp] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2921fa9e4066Sahrens  *
2922990b4856Slling  *	-H	Scripted mode.  Don't display headers, and separate properties
2923990b4856Slling  *		by a single tab.
2924990b4856Slling  *	-o	List of properties to display.  Defaults to
29257a09f97bSGeorge Wilson  *		"name,size,allocated,free,expandsize,fragmentation,capacity,"
29267a09f97bSGeorge Wilson  *		"dedupratio,health,altroot"
2927c58b3526SAdam Stevko  * 	-p	Diplay values in parsable (exact) format.
29283f9d6ad7SLin Ling  *	-T	Display a timestamp in date(1) or Unix format
2929fa9e4066Sahrens  *
2930fa9e4066Sahrens  * List all pools in the system, whether or not they're healthy.  Output space
2931fa9e4066Sahrens  * statistics for each one, as well as health status summary.
2932fa9e4066Sahrens  */
2933fa9e4066Sahrens int
2934fa9e4066Sahrens zpool_do_list(int argc, char **argv)
2935fa9e4066Sahrens {
2936fa9e4066Sahrens 	int c;
2937fa9e4066Sahrens 	int ret;
2938fa9e4066Sahrens 	list_cbdata_t cb = { 0 };
2939990b4856Slling 	static char default_props[] =
29407a09f97bSGeorge Wilson 	    "name,size,allocated,free,expandsize,fragmentation,capacity,"
29412e4c9986SGeorge Wilson 	    "dedupratio,health,altroot";
2942990b4856Slling 	char *props = default_props;
29433f9d6ad7SLin Ling 	unsigned long interval = 0, count = 0;
29444263d13fSGeorge Wilson 	zpool_list_t *list;
29454263d13fSGeorge Wilson 	boolean_t first = B_TRUE;
2946fa9e4066Sahrens 
2947fa9e4066Sahrens 	/* check options */
2948c58b3526SAdam Stevko 	while ((c = getopt(argc, argv, ":Ho:pT:v")) != -1) {
2949fa9e4066Sahrens 		switch (c) {
2950fa9e4066Sahrens 		case 'H':
295199653d4eSeschrock 			cb.cb_scripted = B_TRUE;
2952fa9e4066Sahrens 			break;
2953fa9e4066Sahrens 		case 'o':
2954990b4856Slling 			props = optarg;
2955fa9e4066Sahrens 			break;
2956c58b3526SAdam Stevko 		case 'p':
2957c58b3526SAdam Stevko 			cb.cb_literal = B_TRUE;
2958c58b3526SAdam Stevko 			break;
29593f9d6ad7SLin Ling 		case 'T':
29603f9d6ad7SLin Ling 			get_timestamp_arg(*optarg);
29613f9d6ad7SLin Ling 			break;
29624263d13fSGeorge Wilson 		case 'v':
29634263d13fSGeorge Wilson 			cb.cb_verbose = B_TRUE;
29644263d13fSGeorge Wilson 			break;
2965fa9e4066Sahrens 		case ':':
2966fa9e4066Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
2967fa9e4066Sahrens 			    "'%c' option\n"), optopt);
296899653d4eSeschrock 			usage(B_FALSE);
2969fa9e4066Sahrens 			break;
2970fa9e4066Sahrens 		case '?':
2971fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2972fa9e4066Sahrens 			    optopt);
297399653d4eSeschrock 			usage(B_FALSE);
2974fa9e4066Sahrens 		}
2975fa9e4066Sahrens 	}
2976fa9e4066Sahrens 
2977fa9e4066Sahrens 	argc -= optind;
2978fa9e4066Sahrens 	argv += optind;
2979fa9e4066Sahrens 
29803f9d6ad7SLin Ling 	get_interval_count(&argc, argv, &interval, &count);
29813f9d6ad7SLin Ling 
2982990b4856Slling 	if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
298399653d4eSeschrock 		usage(B_FALSE);
2984fa9e4066Sahrens 
29854263d13fSGeorge Wilson 	for (;;) {
2986cd67d23dSGeorge Wilson 		if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
2987cd67d23dSGeorge Wilson 		    &ret)) == NULL)
2988cd67d23dSGeorge Wilson 			return (1);
29894263d13fSGeorge Wilson 
29904263d13fSGeorge Wilson 		if (pool_list_count(list) == 0)
29914263d13fSGeorge Wilson 			break;
29924263d13fSGeorge Wilson 
29934263d13fSGeorge Wilson 		cb.cb_namewidth = 0;
29944263d13fSGeorge Wilson 		(void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
29954263d13fSGeorge Wilson 
29964263d13fSGeorge Wilson 		if (timestamp_fmt != NODATE)
29974263d13fSGeorge Wilson 			print_timestamp(timestamp_fmt);
29984263d13fSGeorge Wilson 
29994263d13fSGeorge Wilson 		if (!cb.cb_scripted && (first || cb.cb_verbose)) {
30004263d13fSGeorge Wilson 			print_header(&cb);
30014263d13fSGeorge Wilson 			first = B_FALSE;
30024263d13fSGeorge Wilson 		}
30034263d13fSGeorge Wilson 		ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
30044263d13fSGeorge Wilson 
30053f9d6ad7SLin Ling 		if (interval == 0)
30063f9d6ad7SLin Ling 			break;
30073f9d6ad7SLin Ling 
30083f9d6ad7SLin Ling 		if (count != 0 && --count == 0)
30093f9d6ad7SLin Ling 			break;
30103f9d6ad7SLin Ling 
3011cd67d23dSGeorge Wilson 		pool_list_free(list);
30123f9d6ad7SLin Ling 		(void) sleep(interval);
30133f9d6ad7SLin Ling 	}
30143f9d6ad7SLin Ling 
3015cd67d23dSGeorge Wilson 	if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3016cd67d23dSGeorge Wilson 		(void) printf(gettext("no pools available\n"));
3017cd67d23dSGeorge Wilson 		ret = 0;
3018cd67d23dSGeorge Wilson 	}
3019cd67d23dSGeorge Wilson 
3020cd67d23dSGeorge Wilson 	pool_list_free(list);
30213f9d6ad7SLin Ling 	zprop_free_list(cb.cb_proplist);
3022fa9e4066Sahrens 	return (ret);
3023fa9e4066Sahrens }
3024fa9e4066Sahrens 
3025fa9e4066Sahrens static nvlist_t *
3026fa9e4066Sahrens zpool_get_vdev_by_name(nvlist_t *nv, char *name)
3027fa9e4066Sahrens {
3028fa9e4066Sahrens 	nvlist_t **child;
3029fa9e4066Sahrens 	uint_t c, children;
3030fa9e4066Sahrens 	nvlist_t *match;
3031fa9e4066Sahrens 	char *path;
3032fa9e4066Sahrens 
3033fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3034fa9e4066Sahrens 	    &child, &children) != 0) {
3035fa9e4066Sahrens 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
3036fa9e4066Sahrens 		if (strncmp(name, "/dev/dsk/", 9) == 0)
3037fa9e4066Sahrens 			name += 9;
3038fa9e4066Sahrens 		if (strncmp(path, "/dev/dsk/", 9) == 0)
3039fa9e4066Sahrens 			path += 9;
3040fa9e4066Sahrens 		if (strcmp(name, path) == 0)
3041fa9e4066Sahrens 			return (nv);
3042fa9e4066Sahrens 		return (NULL);
3043fa9e4066Sahrens 	}
3044fa9e4066Sahrens 
3045fa9e4066Sahrens 	for (c = 0; c < children; c++)
3046fa9e4066Sahrens 		if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
3047fa9e4066Sahrens 			return (match);
3048fa9e4066Sahrens 
3049fa9e4066Sahrens 	return (NULL);
3050fa9e4066Sahrens }
3051fa9e4066Sahrens 
3052fa9e4066Sahrens static int
3053fa9e4066Sahrens zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3054fa9e4066Sahrens {
305599653d4eSeschrock 	boolean_t force = B_FALSE;
3056fa9e4066Sahrens 	int c;
3057fa9e4066Sahrens 	nvlist_t *nvroot;
3058fa9e4066Sahrens 	char *poolname, *old_disk, *new_disk;
3059fa9e4066Sahrens 	zpool_handle_t *zhp;
306099653d4eSeschrock 	int ret;
3061fa9e4066Sahrens 
3062fa9e4066Sahrens 	/* check options */
3063fa9e4066Sahrens 	while ((c = getopt(argc, argv, "f")) != -1) {
3064fa9e4066Sahrens 		switch (c) {
3065fa9e4066Sahrens 		case 'f':
306699653d4eSeschrock 			force = B_TRUE;
3067fa9e4066Sahrens 			break;
3068fa9e4066Sahrens 		case '?':
3069fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3070fa9e4066Sahrens 			    optopt);
307199653d4eSeschrock 			usage(B_FALSE);
3072fa9e4066Sahrens 		}
3073fa9e4066Sahrens 	}
3074fa9e4066Sahrens 
3075fa9e4066Sahrens 	argc -= optind;
3076fa9e4066Sahrens 	argv += optind;
3077fa9e4066Sahrens 
3078fa9e4066Sahrens 	/* get pool name and check number of arguments */
3079fa9e4066Sahrens 	if (argc < 1) {
3080fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
308199653d4eSeschrock 		usage(B_FALSE);
3082fa9e4066Sahrens 	}
3083fa9e4066Sahrens 
3084fa9e4066Sahrens 	poolname = argv[0];
3085fa9e4066Sahrens 
3086fa9e4066Sahrens 	if (argc < 2) {
3087fa9e4066Sahrens 		(void) fprintf(stderr,
3088fa9e4066Sahrens 		    gettext("missing <device> specification\n"));
308999653d4eSeschrock 		usage(B_FALSE);
3090fa9e4066Sahrens 	}
3091fa9e4066Sahrens 
3092fa9e4066Sahrens 	old_disk = argv[1];
3093fa9e4066Sahrens 
3094fa9e4066Sahrens 	if (argc < 3) {
3095fa9e4066Sahrens 		if (!replacing) {
3096fa9e4066Sahrens 			(void) fprintf(stderr,
3097fa9e4066Sahrens 			    gettext("missing <new_device> specification\n"));
309899653d4eSeschrock 			usage(B_FALSE);
3099fa9e4066Sahrens 		}
3100fa9e4066Sahrens 		new_disk = old_disk;
3101fa9e4066Sahrens 		argc -= 1;
3102fa9e4066Sahrens 		argv += 1;
3103fa9e4066Sahrens 	} else {
3104fa9e4066Sahrens 		new_disk = argv[2];
3105fa9e4066Sahrens 		argc -= 2;
3106fa9e4066Sahrens 		argv += 2;
3107fa9e4066Sahrens 	}
3108fa9e4066Sahrens 
3109fa9e4066Sahrens 	if (argc > 1) {
3110fa9e4066Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
311199653d4eSeschrock 		usage(B_FALSE);
3112fa9e4066Sahrens 	}
3113fa9e4066Sahrens 
311499653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3115fa9e4066Sahrens 		return (1);
3116fa9e4066Sahrens 
31178488aeb5Staylor 	if (zpool_get_config(zhp, NULL) == NULL) {
3118fa9e4066Sahrens 		(void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3119fa9e4066Sahrens 		    poolname);
3120fa9e4066Sahrens 		zpool_close(zhp);
3121fa9e4066Sahrens 		return (1);
3122fa9e4066Sahrens 	}
3123fa9e4066Sahrens 
3124705040edSEric Taylor 	nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
3125705040edSEric Taylor 	    argc, argv);
3126fa9e4066Sahrens 	if (nvroot == NULL) {
3127fa9e4066Sahrens 		zpool_close(zhp);
3128fa9e4066Sahrens 		return (1);
3129fa9e4066Sahrens 	}
3130fa9e4066Sahrens 
313199653d4eSeschrock 	ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
313299653d4eSeschrock 
313399653d4eSeschrock 	nvlist_free(nvroot);
313499653d4eSeschrock 	zpool_close(zhp);
313599653d4eSeschrock 
313699653d4eSeschrock 	return (ret);
3137fa9e4066Sahrens }
3138fa9e4066Sahrens 
3139fa9e4066Sahrens /*
3140fa9e4066Sahrens  * zpool replace [-f] <pool> <device> <new_device>
3141fa9e4066Sahrens  *
3142fa9e4066Sahrens  *	-f	Force attach, even if <new_device> appears to be in use.
3143fa9e4066Sahrens  *
3144fa9e4066Sahrens  * Replace <device> with <new_device>.
3145fa9e4066Sahrens  */
3146fa9e4066Sahrens /* ARGSUSED */
3147fa9e4066Sahrens int
3148fa9e4066Sahrens zpool_do_replace(int argc, char **argv)
3149fa9e4066Sahrens {
3150fa9e4066Sahrens 	return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3151fa9e4066Sahrens }
3152fa9e4066Sahrens 
3153fa9e4066Sahrens /*
3154fa9e4066Sahrens  * zpool attach [-f] <pool> <device> <new_device>
3155fa9e4066Sahrens  *
3156fa9e4066Sahrens  *	-f	Force attach, even if <new_device> appears to be in use.
3157fa9e4066Sahrens  *
3158fa9e4066Sahrens  * Attach <new_device> to the mirror containing <device>.  If <device> is not
3159fa9e4066Sahrens  * part of a mirror, then <device> will be transformed into a mirror of
3160fa9e4066Sahrens  * <device> and <new_device>.  In either case, <new_device> will begin life
3161fa9e4066Sahrens  * with a DTL of [0, now], and will immediately begin to resilver itself.
3162fa9e4066Sahrens  */
3163fa9e4066Sahrens int
3164fa9e4066Sahrens zpool_do_attach(int argc, char **argv)
3165fa9e4066Sahrens {
3166fa9e4066Sahrens 	return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3167fa9e4066Sahrens }
3168fa9e4066Sahrens 
3169fa9e4066Sahrens /*
3170fa9e4066Sahrens  * zpool detach [-f] <pool> <device>
3171fa9e4066Sahrens  *
3172fa9e4066Sahrens  *	-f	Force detach of <device>, even if DTLs argue against it
3173fa9e4066Sahrens  *		(not supported yet)
3174fa9e4066Sahrens  *
3175fa9e4066Sahrens  * Detach a device from a mirror.  The operation will be refused if <device>
3176fa9e4066Sahrens  * is the last device in the mirror, or if the DTLs indicate that this device
3177fa9e4066Sahrens  * has the only valid copy of some data.
3178fa9e4066Sahrens  */
3179fa9e4066Sahrens /* ARGSUSED */
3180fa9e4066Sahrens int
3181fa9e4066Sahrens zpool_do_detach(int argc, char **argv)
3182fa9e4066Sahrens {
3183fa9e4066Sahrens 	int c;
3184fa9e4066Sahrens 	char *poolname, *path;
3185fa9e4066Sahrens 	zpool_handle_t *zhp;
318699653d4eSeschrock 	int ret;
3187fa9e4066Sahrens 
3188fa9e4066Sahrens 	/* check options */
3189fa9e4066Sahrens 	while ((c = getopt(argc, argv, "f")) != -1) {
3190fa9e4066Sahrens 		switch (c) {
3191fa9e4066Sahrens 		case 'f':
3192fa9e4066Sahrens 		case '?':
3193fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3194fa9e4066Sahrens 			    optopt);
319599653d4eSeschrock 			usage(B_FALSE);
3196fa9e4066Sahrens 		}
3197fa9e4066Sahrens 	}
3198fa9e4066Sahrens 
3199fa9e4066Sahrens 	argc -= optind;
3200fa9e4066Sahrens 	argv += optind;
3201fa9e4066Sahrens 
3202fa9e4066Sahrens 	/* get pool name and check number of arguments */
3203fa9e4066Sahrens 	if (argc < 1) {
3204fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
320599653d4eSeschrock 		usage(B_FALSE);
3206fa9e4066Sahrens 	}
3207fa9e4066Sahrens 
3208fa9e4066Sahrens 	if (argc < 2) {
3209fa9e4066Sahrens 		(void) fprintf(stderr,
3210fa9e4066Sahrens 		    gettext("missing <device> specification\n"));
321199653d4eSeschrock 		usage(B_FALSE);
3212fa9e4066Sahrens 	}
3213fa9e4066Sahrens 
3214fa9e4066Sahrens 	poolname = argv[0];
3215fa9e4066Sahrens 	path = argv[1];
3216fa9e4066Sahrens 
321799653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3218fa9e4066Sahrens 		return (1);
3219fa9e4066Sahrens 
322099653d4eSeschrock 	ret = zpool_vdev_detach(zhp, path);
322199653d4eSeschrock 
322299653d4eSeschrock 	zpool_close(zhp);
322399653d4eSeschrock 
322499653d4eSeschrock 	return (ret);
3225fa9e4066Sahrens }
3226fa9e4066Sahrens 
3227fa9e4066Sahrens /*
32281195e687SMark J Musante  * zpool split [-n] [-o prop=val] ...
32291195e687SMark J Musante  *		[-o mntopt] ...
32301195e687SMark J Musante  *		[-R altroot] <pool> <newpool> [<device> ...]
32311195e687SMark J Musante  *
32321195e687SMark J Musante  *	-n	Do not split the pool, but display the resulting layout if
32331195e687SMark J Musante  *		it were to be split.
32341195e687SMark J Musante  *	-o	Set property=value, or set mount options.
32351195e687SMark J Musante  *	-R	Mount the split-off pool under an alternate root.
32361195e687SMark J Musante  *
32371195e687SMark J Musante  * Splits the named pool and gives it the new pool name.  Devices to be split
32381195e687SMark J Musante  * off may be listed, provided that no more than one device is specified
32391195e687SMark J Musante  * per top-level vdev mirror.  The newly split pool is left in an exported
32401195e687SMark J Musante  * state unless -R is specified.
32411195e687SMark J Musante  *
32421195e687SMark J Musante  * Restrictions: the top-level of the pool pool must only be made up of
32431195e687SMark J Musante  * mirrors; all devices in the pool must be healthy; no device may be
32441195e687SMark J Musante  * undergoing a resilvering operation.
32451195e687SMark J Musante  */
32461195e687SMark J Musante int
32471195e687SMark J Musante zpool_do_split(int argc, char **argv)
32481195e687SMark J Musante {
32491195e687SMark J Musante 	char *srcpool, *newpool, *propval;
32501195e687SMark J Musante 	char *mntopts = NULL;
32511195e687SMark J Musante 	splitflags_t flags;
32521195e687SMark J Musante 	int c, ret = 0;
32531195e687SMark J Musante 	zpool_handle_t *zhp;
32541195e687SMark J Musante 	nvlist_t *config, *props = NULL;
32551195e687SMark J Musante 
32561195e687SMark J Musante 	flags.dryrun = B_FALSE;
32571195e687SMark J Musante 	flags.import = B_FALSE;
32581195e687SMark J Musante 
32591195e687SMark J Musante 	/* check options */
32601195e687SMark J Musante 	while ((c = getopt(argc, argv, ":R:no:")) != -1) {
32611195e687SMark J Musante 		switch (c) {
32621195e687SMark J Musante 		case 'R':
32631195e687SMark J Musante 			flags.import = B_TRUE;
32641195e687SMark J Musante 			if (add_prop_list(
32651195e687SMark J Musante 			    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
32661195e687SMark J Musante 			    &props, B_TRUE) != 0) {
32671195e687SMark J Musante 				if (props)
32681195e687SMark J Musante 					nvlist_free(props);
32691195e687SMark J Musante 				usage(B_FALSE);
32701195e687SMark J Musante 			}
32711195e687SMark J Musante 			break;
32721195e687SMark J Musante 		case 'n':
32731195e687SMark J Musante 			flags.dryrun = B_TRUE;
32741195e687SMark J Musante 			break;
32751195e687SMark J Musante 		case 'o':
32761195e687SMark J Musante 			if ((propval = strchr(optarg, '=')) != NULL) {
32771195e687SMark J Musante 				*propval = '\0';
32781195e687SMark J Musante 				propval++;
32791195e687SMark J Musante 				if (add_prop_list(optarg, propval,
32801195e687SMark J Musante 				    &props, B_TRUE) != 0) {
32811195e687SMark J Musante 					if (props)
32821195e687SMark J Musante 						nvlist_free(props);
32831195e687SMark J Musante 					usage(B_FALSE);
32841195e687SMark J Musante 				}
32851195e687SMark J Musante 			} else {
32861195e687SMark J Musante 				mntopts = optarg;
32871195e687SMark J Musante 			}
32881195e687SMark J Musante 			break;
32891195e687SMark J Musante 		case ':':
32901195e687SMark J Musante 			(void) fprintf(stderr, gettext("missing argument for "
32911195e687SMark J Musante 			    "'%c' option\n"), optopt);
32921195e687SMark J Musante 			usage(B_FALSE);
32931195e687SMark J Musante 			break;
32941195e687SMark J Musante 		case '?':
32951195e687SMark J Musante 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
32961195e687SMark J Musante 			    optopt);
32971195e687SMark J Musante 			usage(B_FALSE);
32981195e687SMark J Musante 			break;
32991195e687SMark J Musante 		}
33001195e687SMark J Musante 	}
33011195e687SMark J Musante 
33021195e687SMark J Musante 	if (!flags.import && mntopts != NULL) {
33031195e687SMark J Musante 		(void) fprintf(stderr, gettext("setting mntopts is only "
33041195e687SMark J Musante 		    "valid when importing the pool\n"));
33051195e687SMark J Musante 		usage(B_FALSE);
33061195e687SMark J Musante 	}
33071195e687SMark J Musante 
33081195e687SMark J Musante 	argc -= optind;
33091195e687SMark J Musante 	argv += optind;
33101195e687SMark J Musante 
33111195e687SMark J Musante 	if (argc < 1) {
33121195e687SMark J Musante 		(void) fprintf(stderr, gettext("Missing pool name\n"));
33131195e687SMark J Musante 		usage(B_FALSE);
33141195e687SMark J Musante 	}
33151195e687SMark J Musante 	if (argc < 2) {
33161195e687SMark J Musante 		(void) fprintf(stderr, gettext("Missing new pool name\n"));
33171195e687SMark J Musante 		usage(B_FALSE);
33181195e687SMark J Musante 	}
33191195e687SMark J Musante 
33201195e687SMark J Musante 	srcpool = argv[0];
33211195e687SMark J Musante 	newpool = argv[1];
33221195e687SMark J Musante 
33231195e687SMark J Musante 	argc -= 2;
33241195e687SMark J Musante 	argv += 2;
33251195e687SMark J Musante 
33261195e687SMark J Musante 	if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
33271195e687SMark J Musante 		return (1);
33281195e687SMark J Musante 
33291195e687SMark J Musante 	config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
33301195e687SMark J Musante 	if (config == NULL) {
33311195e687SMark J Musante 		ret = 1;
33321195e687SMark J Musante 	} else {
33331195e687SMark J Musante 		if (flags.dryrun) {
33341195e687SMark J Musante 			(void) printf(gettext("would create '%s' with the "
33351195e687SMark J Musante 			    "following layout:\n\n"), newpool);
33361195e687SMark J Musante 			print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
33371195e687SMark J Musante 		}
33381195e687SMark J Musante 		nvlist_free(config);
33391195e687SMark J Musante 	}
33401195e687SMark J Musante 
33411195e687SMark J Musante 	zpool_close(zhp);
33421195e687SMark J Musante 
33431195e687SMark J Musante 	if (ret != 0 || flags.dryrun || !flags.import)
33441195e687SMark J Musante 		return (ret);
33451195e687SMark J Musante 
33461195e687SMark J Musante 	/*
33471195e687SMark J Musante 	 * The split was successful. Now we need to open the new
33481195e687SMark J Musante 	 * pool and import it.
33491195e687SMark J Musante 	 */
33501195e687SMark J Musante 	if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
33511195e687SMark J Musante 		return (1);
33521195e687SMark J Musante 	if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
33531195e687SMark J Musante 	    zpool_enable_datasets(zhp, mntopts, 0) != 0) {
33541195e687SMark J Musante 		ret = 1;
3355fe7cd8aaSCyril Plisko 		(void) fprintf(stderr, gettext("Split was successful, but "
33561195e687SMark J Musante 		    "the datasets could not all be mounted\n"));
33571195e687SMark J Musante 		(void) fprintf(stderr, gettext("Try doing '%s' with a "
33581195e687SMark J Musante 		    "different altroot\n"), "zpool import");
33591195e687SMark J Musante 	}
33601195e687SMark J Musante 	zpool_close(zhp);
33611195e687SMark J Musante 
33621195e687SMark J Musante 	return (ret);
33631195e687SMark J Musante }
33641195e687SMark J Musante 
33651195e687SMark J Musante 
33661195e687SMark J Musante 
33671195e687SMark J Musante /*
3368441d80aaSlling  * zpool online <pool> <device> ...
3369fa9e4066Sahrens  */
3370fa9e4066Sahrens int
3371fa9e4066Sahrens zpool_do_online(int argc, char **argv)
3372fa9e4066Sahrens {
3373fa9e4066Sahrens 	int c, i;
3374fa9e4066Sahrens 	char *poolname;
3375fa9e4066Sahrens 	zpool_handle_t *zhp;
3376fa9e4066Sahrens 	int ret = 0;
33773d7072f8Seschrock 	vdev_state_t newstate;
3378573ca77eSGeorge Wilson 	int flags = 0;
3379fa9e4066Sahrens 
3380fa9e4066Sahrens 	/* check options */
3381573ca77eSGeorge Wilson 	while ((c = getopt(argc, argv, "et")) != -1) {
3382fa9e4066Sahrens 		switch (c) {
3383573ca77eSGeorge Wilson 		case 'e':
3384573ca77eSGeorge Wilson 			flags |= ZFS_ONLINE_EXPAND;
3385573ca77eSGeorge Wilson 			break;
3386fa9e4066Sahrens 		case 't':
3387fa9e4066Sahrens 		case '?':
3388fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3389fa9e4066Sahrens 			    optopt);
339099653d4eSeschrock 			usage(B_FALSE);
3391fa9e4066Sahrens 		}
3392fa9e4066Sahrens 	}
3393fa9e4066Sahrens 
3394fa9e4066Sahrens 	argc -= optind;
3395fa9e4066Sahrens 	argv += optind;
3396fa9e4066Sahrens 
3397fa9e4066Sahrens 	/* get pool name and check number of arguments */
3398fa9e4066Sahrens 	if (argc < 1) {
3399fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name\n"));
340099653d4eSeschrock 		usage(B_FALSE);
3401fa9e4066Sahrens 	}
3402fa9e4066Sahrens 	if (argc < 2) {
3403fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing device name\n"));
340499653d4eSeschrock 		usage(B_FALSE);
3405fa9e4066Sahrens 	}
3406fa9e4066Sahrens 
3407fa9e4066Sahrens 	poolname = argv[0];
3408fa9e4066Sahrens 
340999653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3410fa9e4066Sahrens 		return (1);
3411fa9e4066Sahrens 
34123d7072f8Seschrock 	for (i = 1; i < argc; i++) {
3413573ca77eSGeorge Wilson 		if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
34143d7072f8Seschrock 			if (newstate != VDEV_STATE_HEALTHY) {
34153d7072f8Seschrock 				(void) printf(gettext("warning: device '%s' "
34163d7072f8Seschrock 				    "onlined, but remains in faulted state\n"),
3417fa9e4066Sahrens 				    argv[i]);
34183d7072f8Seschrock 				if (newstate == VDEV_STATE_FAULTED)
34193d7072f8Seschrock 					(void) printf(gettext("use 'zpool "
34203d7072f8Seschrock 					    "clear' to restore a faulted "
34213d7072f8Seschrock 					    "device\n"));
3422fa9e4066Sahrens 				else
34233d7072f8Seschrock 					(void) printf(gettext("use 'zpool "
34243d7072f8Seschrock 					    "replace' to replace devices "
34253d7072f8Seschrock 					    "that are no longer present\n"));
34263d7072f8Seschrock 			}
34273d7072f8Seschrock 		} else {
3428fa9e4066Sahrens 			ret = 1;
34293d7072f8Seschrock 		}
34303d7072f8Seschrock 	}
3431fa9e4066Sahrens 
343299653d4eSeschrock 	zpool_close(zhp);
343399653d4eSeschrock 
3434fa9e4066Sahrens 	return (ret);
3435fa9e4066Sahrens }
3436fa9e4066Sahrens 
3437fa9e4066Sahrens /*
3438441d80aaSlling  * zpool offline [-ft] <pool> <device> ...
3439fa9e4066Sahrens  *
3440fa9e4066Sahrens  *	-f	Force the device into the offline state, even if doing
3441fa9e4066Sahrens  *		so would appear to compromise pool availability.
3442fa9e4066Sahrens  *		(not supported yet)
3443fa9e4066Sahrens  *
3444fa9e4066Sahrens  *	-t	Only take the device off-line temporarily.  The offline
3445fa9e4066Sahrens  *		state will not be persistent across reboots.
3446fa9e4066Sahrens  */
3447fa9e4066Sahrens /* ARGSUSED */
3448fa9e4066Sahrens int
3449fa9e4066Sahrens zpool_do_offline(int argc, char **argv)
3450fa9e4066Sahrens {
3451fa9e4066Sahrens 	int c, i;
3452fa9e4066Sahrens 	char *poolname;
3453fa9e4066Sahrens 	zpool_handle_t *zhp;
345499653d4eSeschrock 	int ret = 0;
345599653d4eSeschrock 	boolean_t istmp = B_FALSE;
3456fa9e4066Sahrens 
3457fa9e4066Sahrens 	/* check options */
3458fa9e4066Sahrens 	while ((c = getopt(argc, argv, "ft")) != -1) {
3459fa9e4066Sahrens 		switch (c) {
3460fa9e4066Sahrens 		case 't':
346199653d4eSeschrock 			istmp = B_TRUE;
3462441d80aaSlling 			break;
3463441d80aaSlling 		case 'f':
3464fa9e4066Sahrens 		case '?':
3465fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3466fa9e4066Sahrens 			    optopt);
346799653d4eSeschrock 			usage(B_FALSE);
3468fa9e4066Sahrens 		}
3469fa9e4066Sahrens 	}
3470fa9e4066Sahrens 
3471fa9e4066Sahrens 	argc -= optind;
3472fa9e4066Sahrens 	argv += optind;
3473fa9e4066Sahrens 
3474fa9e4066Sahrens 	/* get pool name and check number of arguments */
3475fa9e4066Sahrens 	if (argc < 1) {
3476fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name\n"));
347799653d4eSeschrock 		usage(B_FALSE);
3478fa9e4066Sahrens 	}
3479fa9e4066Sahrens 	if (argc < 2) {
3480fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing device name\n"));
348199653d4eSeschrock 		usage(B_FALSE);
3482fa9e4066Sahrens 	}
3483fa9e4066Sahrens 
3484fa9e4066Sahrens 	poolname = argv[0];
3485fa9e4066Sahrens 
348699653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3487fa9e4066Sahrens 		return (1);
3488fa9e4066Sahrens 
34893d7072f8Seschrock 	for (i = 1; i < argc; i++) {
34903d7072f8Seschrock 		if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3491fa9e4066Sahrens 			ret = 1;
34923d7072f8Seschrock 	}
3493fa9e4066Sahrens 
349499653d4eSeschrock 	zpool_close(zhp);
349599653d4eSeschrock 
3496fa9e4066Sahrens 	return (ret);
3497fa9e4066Sahrens }
3498fa9e4066Sahrens 
3499ea8dc4b6Seschrock /*
3500ea8dc4b6Seschrock  * zpool clear <pool> [device]
3501ea8dc4b6Seschrock  *
3502ea8dc4b6Seschrock  * Clear all errors associated with a pool or a particular device.
3503ea8dc4b6Seschrock  */
3504ea8dc4b6Seschrock int
3505ea8dc4b6Seschrock zpool_do_clear(int argc, char **argv)
3506ea8dc4b6Seschrock {
3507468c413aSTim Haley 	int c;
3508ea8dc4b6Seschrock 	int ret = 0;
3509468c413aSTim Haley 	boolean_t dryrun = B_FALSE;
3510468c413aSTim Haley 	boolean_t do_rewind = B_FALSE;
3511468c413aSTim Haley 	boolean_t xtreme_rewind = B_FALSE;
3512468c413aSTim Haley 	uint32_t rewind_policy = ZPOOL_NO_REWIND;
3513468c413aSTim Haley 	nvlist_t *policy = NULL;
3514ea8dc4b6Seschrock 	zpool_handle_t *zhp;
3515ea8dc4b6Seschrock 	char *pool, *device;
3516ea8dc4b6Seschrock 
3517468c413aSTim Haley 	/* check options */
3518468c413aSTim Haley 	while ((c = getopt(argc, argv, "FnX")) != -1) {
3519468c413aSTim Haley 		switch (c) {
3520468c413aSTim Haley 		case 'F':
3521468c413aSTim Haley 			do_rewind = B_TRUE;
3522468c413aSTim Haley 			break;
3523468c413aSTim Haley 		case 'n':
3524468c413aSTim Haley 			dryrun = B_TRUE;
3525468c413aSTim Haley 			break;
3526468c413aSTim Haley 		case 'X':
3527468c413aSTim Haley 			xtreme_rewind = B_TRUE;
3528468c413aSTim Haley 			break;
3529468c413aSTim Haley 		case '?':
3530468c413aSTim Haley 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3531468c413aSTim Haley 			    optopt);
3532468c413aSTim Haley 			usage(B_FALSE);
3533468c413aSTim Haley 		}
3534468c413aSTim Haley 	}
3535468c413aSTim Haley 
3536468c413aSTim Haley 	argc -= optind;
3537468c413aSTim Haley 	argv += optind;
3538468c413aSTim Haley 
3539468c413aSTim Haley 	if (argc < 1) {
3540ea8dc4b6Seschrock 		(void) fprintf(stderr, gettext("missing pool name\n"));
354199653d4eSeschrock 		usage(B_FALSE);
3542ea8dc4b6Seschrock 	}
3543ea8dc4b6Seschrock 
3544468c413aSTim Haley 	if (argc > 2) {
3545ea8dc4b6Seschrock 		(void) fprintf(stderr, gettext("too many arguments\n"));
354699653d4eSeschrock 		usage(B_FALSE);
3547ea8dc4b6Seschrock 	}
3548ea8dc4b6Seschrock 
3549468c413aSTim Haley 	if ((dryrun || xtreme_rewind) && !do_rewind) {
3550468c413aSTim Haley 		(void) fprintf(stderr,
3551468c413aSTim Haley 		    gettext("-n or -X only meaningful with -F\n"));
3552468c413aSTim Haley 		usage(B_FALSE);
3553468c413aSTim Haley 	}
3554468c413aSTim Haley 	if (dryrun)
3555468c413aSTim Haley 		rewind_policy = ZPOOL_TRY_REWIND;
3556468c413aSTim Haley 	else if (do_rewind)
3557468c413aSTim Haley 		rewind_policy = ZPOOL_DO_REWIND;
3558468c413aSTim Haley 	if (xtreme_rewind)
3559468c413aSTim Haley 		rewind_policy |= ZPOOL_EXTREME_REWIND;
3560ea8dc4b6Seschrock 
3561468c413aSTim Haley 	/* In future, further rewind policy choices can be passed along here */
3562468c413aSTim Haley 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3563468c413aSTim Haley 	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3564ea8dc4b6Seschrock 		return (1);
3565ea8dc4b6Seschrock 
3566468c413aSTim Haley 	pool = argv[0];
3567468c413aSTim Haley 	device = argc == 2 ? argv[1] : NULL;
3568468c413aSTim Haley 
3569468c413aSTim Haley 	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3570468c413aSTim Haley 		nvlist_free(policy);
3571468c413aSTim Haley 		return (1);
3572468c413aSTim Haley 	}
3573468c413aSTim Haley 
3574468c413aSTim Haley 	if (zpool_clear(zhp, device, policy) != 0)
3575ea8dc4b6Seschrock 		ret = 1;
3576ea8dc4b6Seschrock 
3577ea8dc4b6Seschrock 	zpool_close(zhp);
3578ea8dc4b6Seschrock 
3579468c413aSTim Haley 	nvlist_free(policy);
3580468c413aSTim Haley 
3581ea8dc4b6Seschrock 	return (ret);
3582ea8dc4b6Seschrock }
3583ea8dc4b6Seschrock 
3584e9103aaeSGarrett D'Amore /*
3585e9103aaeSGarrett D'Amore  * zpool reguid <pool>
3586e9103aaeSGarrett D'Amore  */
3587e9103aaeSGarrett D'Amore int
3588e9103aaeSGarrett D'Amore zpool_do_reguid(int argc, char **argv)
3589e9103aaeSGarrett D'Amore {
3590e9103aaeSGarrett D'Amore 	int c;
3591e9103aaeSGarrett D'Amore 	char *poolname;
3592e9103aaeSGarrett D'Amore 	zpool_handle_t *zhp;
3593e9103aaeSGarrett D'Amore 	int ret = 0;
3594e9103aaeSGarrett D'Amore 
3595e9103aaeSGarrett D'Amore 	/* check options */
3596e9103aaeSGarrett D'Amore 	while ((c = getopt(argc, argv, "")) != -1) {
3597e9103aaeSGarrett D'Amore 		switch (c) {
3598e9103aaeSGarrett D'Amore 		case '?':
3599e9103aaeSGarrett D'Amore 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3600e9103aaeSGarrett D'Amore 			    optopt);
3601e9103aaeSGarrett D'Amore 			usage(B_FALSE);
3602e9103aaeSGarrett D'Amore 		}
3603e9103aaeSGarrett D'Amore 	}
3604e9103aaeSGarrett D'Amore 
3605e9103aaeSGarrett D'Amore 	argc -= optind;
3606e9103aaeSGarrett D'Amore 	argv += optind;
3607e9103aaeSGarrett D'Amore 
3608e9103aaeSGarrett D'Amore 	/* get pool name and check number of arguments */
3609e9103aaeSGarrett D'Amore 	if (argc < 1) {
3610e9103aaeSGarrett D'Amore 		(void) fprintf(stderr, gettext("missing pool name\n"));
3611e9103aaeSGarrett D'Amore 		usage(B_FALSE);
3612e9103aaeSGarrett D'Amore 	}
3613e9103aaeSGarrett D'Amore 
3614e9103aaeSGarrett D'Amore 	if (argc > 1) {
3615e9103aaeSGarrett D'Amore 		(void) fprintf(stderr, gettext("too many arguments\n"));
3616e9103aaeSGarrett D'Amore 		usage(B_FALSE);
3617e9103aaeSGarrett D'Amore 	}
3618e9103aaeSGarrett D'Amore 
3619e9103aaeSGarrett D'Amore 	poolname = argv[0];
3620e9103aaeSGarrett D'Amore 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3621e9103aaeSGarrett D'Amore 		return (1);
3622e9103aaeSGarrett D'Amore 
3623e9103aaeSGarrett D'Amore 	ret = zpool_reguid(zhp);
3624e9103aaeSGarrett D'Amore 
3625e9103aaeSGarrett D'Amore 	zpool_close(zhp);
3626e9103aaeSGarrett D'Amore 	return (ret);
3627e9103aaeSGarrett D'Amore }
3628e9103aaeSGarrett D'Amore 
3629e9103aaeSGarrett D'Amore 
36304263d13fSGeorge Wilson /*
36314263d13fSGeorge Wilson  * zpool reopen <pool>
36324263d13fSGeorge Wilson  *
36334263d13fSGeorge Wilson  * Reopen the pool so that the kernel can update the sizes of all vdevs.
36344263d13fSGeorge Wilson  */
36354263d13fSGeorge Wilson int
36364263d13fSGeorge Wilson zpool_do_reopen(int argc, char **argv)
36374263d13fSGeorge Wilson {
363831d7e8faSGeorge Wilson 	int c;
36394263d13fSGeorge Wilson 	int ret = 0;
36404263d13fSGeorge Wilson 	zpool_handle_t *zhp;
36414263d13fSGeorge Wilson 	char *pool;
36424263d13fSGeorge Wilson 
364331d7e8faSGeorge Wilson 	/* check options */
364431d7e8faSGeorge Wilson 	while ((c = getopt(argc, argv, "")) != -1) {
364531d7e8faSGeorge Wilson 		switch (c) {
364631d7e8faSGeorge Wilson 		case '?':
364731d7e8faSGeorge Wilson 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
364831d7e8faSGeorge Wilson 			    optopt);
364931d7e8faSGeorge Wilson 			usage(B_FALSE);
365031d7e8faSGeorge Wilson 		}
365131d7e8faSGeorge Wilson 	}
365231d7e8faSGeorge Wilson 
36534263d13fSGeorge Wilson 	argc--;
36544263d13fSGeorge Wilson 	argv++;
36554263d13fSGeorge Wilson 
365631d7e8faSGeorge Wilson 	if (argc < 1) {
365731d7e8faSGeorge Wilson 		(void) fprintf(stderr, gettext("missing pool name\n"));
365831d7e8faSGeorge Wilson 		usage(B_FALSE);
365931d7e8faSGeorge Wilson 	}
366031d7e8faSGeorge Wilson 
366131d7e8faSGeorge Wilson 	if (argc > 1) {
366231d7e8faSGeorge Wilson 		(void) fprintf(stderr, gettext("too many arguments\n"));
366331d7e8faSGeorge Wilson 		usage(B_FALSE);
366431d7e8faSGeorge Wilson 	}
36654263d13fSGeorge Wilson 
36664263d13fSGeorge Wilson 	pool = argv[0];
36674263d13fSGeorge Wilson 	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
36684263d13fSGeorge Wilson 		return (1);
36694263d13fSGeorge Wilson 
36704263d13fSGeorge Wilson 	ret = zpool_reopen(zhp);
36714263d13fSGeorge Wilson 	zpool_close(zhp);
36724263d13fSGeorge Wilson 	return (ret);
36734263d13fSGeorge Wilson }
36744263d13fSGeorge Wilson 
3675fa9e4066Sahrens typedef struct scrub_cbdata {
3676fa9e4066Sahrens 	int	cb_type;
367706eeb2adSek110237 	int	cb_argc;
367806eeb2adSek110237 	char	**cb_argv;
3679fa9e4066Sahrens } scrub_cbdata_t;
3680fa9e4066Sahrens 
3681fa9e4066Sahrens int
3682fa9e4066Sahrens scrub_callback(zpool_handle_t *zhp, void *data)
3683fa9e4066Sahrens {
3684fa9e4066Sahrens 	scrub_cbdata_t *cb = data;
368506eeb2adSek110237 	int err;
3686fa9e4066Sahrens 
3687ea8dc4b6Seschrock 	/*
3688ea8dc4b6Seschrock 	 * Ignore faulted pools.
3689ea8dc4b6Seschrock 	 */
3690ea8dc4b6Seschrock 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3691ea8dc4b6Seschrock 		(void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3692ea8dc4b6Seschrock 		    "currently unavailable\n"), zpool_get_name(zhp));
3693ea8dc4b6Seschrock 		return (1);
3694ea8dc4b6Seschrock 	}
3695ea8dc4b6Seschrock 
36963f9d6ad7SLin Ling 	err = zpool_scan(zhp, cb->cb_type);
369706eeb2adSek110237 
369806eeb2adSek110237 	return (err != 0);
3699fa9e4066Sahrens }
3700fa9e4066Sahrens 
3701fa9e4066Sahrens /*
3702fa9e4066Sahrens  * zpool scrub [-s] <pool> ...
3703fa9e4066Sahrens  *
3704fa9e4066Sahrens  *	-s	Stop.  Stops any in-progress scrub.
3705fa9e4066Sahrens  */
3706fa9e4066Sahrens int
3707fa9e4066Sahrens zpool_do_scrub(int argc, char **argv)
3708fa9e4066Sahrens {
3709fa9e4066Sahrens 	int c;
3710fa9e4066Sahrens 	scrub_cbdata_t cb;
3711fa9e4066Sahrens 
37123f9d6ad7SLin Ling 	cb.cb_type = POOL_SCAN_SCRUB;
3713fa9e4066Sahrens 
3714fa9e4066Sahrens 	/* check options */
3715fa9e4066Sahrens 	while ((c = getopt(argc, argv, "s")) != -1) {
3716fa9e4066Sahrens 		switch (c) {
3717fa9e4066Sahrens 		case 's':
37183f9d6ad7SLin Ling 			cb.cb_type = POOL_SCAN_NONE;
3719fa9e4066Sahrens 			break;
3720fa9e4066Sahrens 		case '?':
3721fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3722fa9e4066Sahrens 			    optopt);
372399653d4eSeschrock 			usage(B_FALSE);
3724fa9e4066Sahrens 		}
3725fa9e4066Sahrens 	}
3726fa9e4066Sahrens 
372706eeb2adSek110237 	cb.cb_argc = argc;
372806eeb2adSek110237 	cb.cb_argv = argv;
3729fa9e4066Sahrens 	argc -= optind;
3730fa9e4066Sahrens 	argv += optind;
3731fa9e4066Sahrens 
3732fa9e4066Sahrens 	if (argc < 1) {
3733fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
373499653d4eSeschrock 		usage(B_FALSE);
3735fa9e4066Sahrens 	}
3736fa9e4066Sahrens 
3737b1b8ab34Slling 	return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3738fa9e4066Sahrens }
3739fa9e4066Sahrens 
3740fa9e4066Sahrens typedef struct status_cbdata {
3741fa9e4066Sahrens 	int		cb_count;
3742e9dbad6fSeschrock 	boolean_t	cb_allpools;
374399653d4eSeschrock 	boolean_t	cb_verbose;
374499653d4eSeschrock 	boolean_t	cb_explain;
374599653d4eSeschrock 	boolean_t	cb_first;
37469eb19f4dSGeorge Wilson 	boolean_t	cb_dedup_stats;
3747fa9e4066Sahrens } status_cbdata_t;
3748fa9e4066Sahrens 
3749fa9e4066Sahrens /*
3750fa9e4066Sahrens  * Print out detailed scrub status.
3751fa9e4066Sahrens  */
3752fa9e4066Sahrens void
37533f9d6ad7SLin Ling print_scan_status(pool_scan_stat_t *ps)
3754fa9e4066Sahrens {
37553f9d6ad7SLin Ling 	time_t start, end;
375673d314ceSLin Ling 	uint64_t elapsed, mins_left, hours_left;
37573f9d6ad7SLin Ling 	uint64_t pass_exam, examined, total;
37583f9d6ad7SLin Ling 	uint_t rate;
3759fa9e4066Sahrens 	double fraction_done;
37603f9d6ad7SLin Ling 	char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3761fa9e4066Sahrens 
37623f9d6ad7SLin Ling 	(void) printf(gettext("  scan: "));
3763fa9e4066Sahrens 
37643f9d6ad7SLin Ling 	/* If there's never been a scan, there's not much to say. */
37653f9d6ad7SLin Ling 	if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
37663f9d6ad7SLin Ling 	    ps->pss_func >= POOL_SCAN_FUNCS) {
3767fa9e4066Sahrens 		(void) printf(gettext("none requested\n"));
3768fa9e4066Sahrens 		return;
3769fa9e4066Sahrens 	}
3770fa9e4066Sahrens 
37713f9d6ad7SLin Ling 	start = ps->pss_start_time;
37723f9d6ad7SLin Ling 	end = ps->pss_end_time;
37733f9d6ad7SLin Ling 	zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3774fa9e4066Sahrens 
37753f9d6ad7SLin Ling 	assert(ps->pss_func == POOL_SCAN_SCRUB ||
37763f9d6ad7SLin Ling 	    ps->pss_func == POOL_SCAN_RESILVER);
37773f9d6ad7SLin Ling 	/*
37783f9d6ad7SLin Ling 	 * Scan is finished or canceled.
37793f9d6ad7SLin Ling 	 */
37803f9d6ad7SLin Ling 	if (ps->pss_state == DSS_FINISHED) {
37813f9d6ad7SLin Ling 		uint64_t minutes_taken = (end - start) / 60;
37823f9d6ad7SLin Ling 		char *fmt;
3783fa9e4066Sahrens 
37843f9d6ad7SLin Ling 		if (ps->pss_func == POOL_SCAN_SCRUB) {
37853f9d6ad7SLin Ling 			fmt = gettext("scrub repaired %s in %lluh%um with "
37863f9d6ad7SLin Ling 			    "%llu errors on %s");
37873f9d6ad7SLin Ling 		} else if (ps->pss_func == POOL_SCAN_RESILVER) {
37883f9d6ad7SLin Ling 			fmt = gettext("resilvered %s in %lluh%um with "
37893f9d6ad7SLin Ling 			    "%llu errors on %s");
37903f9d6ad7SLin Ling 		}
37913f9d6ad7SLin Ling 		/* LINTED */
37923f9d6ad7SLin Ling 		(void) printf(fmt, processed_buf,
379318ce54dfSek110237 		    (u_longlong_t)(minutes_taken / 60),
379418ce54dfSek110237 		    (uint_t)(minutes_taken % 60),
37953f9d6ad7SLin Ling 		    (u_longlong_t)ps->pss_errors,
37963f9d6ad7SLin Ling 		    ctime((time_t *)&end));
37973f9d6ad7SLin Ling 		return;
37983f9d6ad7SLin Ling 	} else if (ps->pss_state == DSS_CANCELED) {
37993f9d6ad7SLin Ling 		if (ps->pss_func == POOL_SCAN_SCRUB) {
38003f9d6ad7SLin Ling 			(void) printf(gettext("scrub canceled on %s"),
38013f9d6ad7SLin Ling 			    ctime(&end));
38023f9d6ad7SLin Ling 		} else if (ps->pss_func == POOL_SCAN_RESILVER) {
38033f9d6ad7SLin Ling 			(void) printf(gettext("resilver canceled on %s"),
38043f9d6ad7SLin Ling 			    ctime(&end));
38053f9d6ad7SLin Ling 		}
3806fa9e4066Sahrens 		return;
3807fa9e4066Sahrens 	}
3808fa9e4066Sahrens 
38093f9d6ad7SLin Ling 	assert(ps->pss_state == DSS_SCANNING);
3810fa9e4066Sahrens 
38113f9d6ad7SLin Ling 	/*
38123f9d6ad7SLin Ling 	 * Scan is in progress.
38133f9d6ad7SLin Ling 	 */
38143f9d6ad7SLin Ling 	if (ps->pss_func == POOL_SCAN_SCRUB) {
38153f9d6ad7SLin Ling 		(void) printf(gettext("scrub in progress since %s"),
38163f9d6ad7SLin Ling 		    ctime(&start));
38173f9d6ad7SLin Ling 	} else if (ps->pss_func == POOL_SCAN_RESILVER) {
38183f9d6ad7SLin Ling 		(void) printf(gettext("resilver in progress since %s"),
38193f9d6ad7SLin Ling 		    ctime(&start));
38203f9d6ad7SLin Ling 	}
38213f9d6ad7SLin Ling 
38223f9d6ad7SLin Ling 	examined = ps->pss_examined ? ps->pss_examined : 1;
38233f9d6ad7SLin Ling 	total = ps->pss_to_examine;
3824fa9e4066Sahrens 	fraction_done = (double)examined / total;
3825fa9e4066Sahrens 
38263f9d6ad7SLin Ling 	/* elapsed time for this pass */
38273f9d6ad7SLin Ling 	elapsed = time(NULL) - ps->pss_pass_start;
38283f9d6ad7SLin Ling 	elapsed = elapsed ? elapsed : 1;
38293f9d6ad7SLin Ling 	pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
38303f9d6ad7SLin Ling 	rate = pass_exam / elapsed;
38313f9d6ad7SLin Ling 	rate = rate ? rate : 1;
38323f9d6ad7SLin Ling 	mins_left = ((total - examined) / rate) / 60;
383373d314ceSLin Ling 	hours_left = mins_left / 60;
38343f9d6ad7SLin Ling 
38353f9d6ad7SLin Ling 	zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
38363f9d6ad7SLin Ling 	zfs_nicenum(total, total_buf, sizeof (total_buf));
38373f9d6ad7SLin Ling 	zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
38383f9d6ad7SLin Ling 
383973d314ceSLin Ling 	/*
384073d314ceSLin Ling 	 * do not print estimated time if hours_left is more than 30 days
384173d314ceSLin Ling 	 */
384273d314ceSLin Ling 	(void) printf(gettext("    %s scanned out of %s at %s/s"),
384373d314ceSLin Ling 	    examined_buf, total_buf, rate_buf);
384473d314ceSLin Ling 	if (hours_left < (30 * 24)) {
384573d314ceSLin Ling 		(void) printf(gettext(", %lluh%um to go\n"),
384673d314ceSLin Ling 		    (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
384773d314ceSLin Ling 	} else {
384873d314ceSLin Ling 		(void) printf(gettext(
384973d314ceSLin Ling 		    ", (scan is slow, no estimated time)\n"));
385073d314ceSLin Ling 	}
38513f9d6ad7SLin Ling 
38523f9d6ad7SLin Ling 	if (ps->pss_func == POOL_SCAN_RESILVER) {
38533f9d6ad7SLin Ling 		(void) printf(gettext("    %s resilvered, %.2f%% done\n"),
38543f9d6ad7SLin Ling 		    processed_buf, 100 * fraction_done);
38553f9d6ad7SLin Ling 	} else if (ps->pss_func == POOL_SCAN_SCRUB) {
38563f9d6ad7SLin Ling 		(void) printf(gettext("    %s repaired, %.2f%% done\n"),
38573f9d6ad7SLin Ling 		    processed_buf, 100 * fraction_done);
38583f9d6ad7SLin Ling 	}
3859fa9e4066Sahrens }
3860fa9e4066Sahrens 
3861ea8dc4b6Seschrock static void
3862ea8dc4b6Seschrock print_error_log(zpool_handle_t *zhp)
3863ea8dc4b6Seschrock {
386475519f38Sek110237 	nvlist_t *nverrlist = NULL;
386555434c77Sek110237 	nvpair_t *elem;
386655434c77Sek110237 	char *pathname;
386755434c77Sek110237 	size_t len = MAXPATHLEN * 2;
3868ea8dc4b6Seschrock 
386955434c77Sek110237 	if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3870ea8dc4b6Seschrock 		(void) printf("errors: List of errors unavailable "
3871ea8dc4b6Seschrock 		    "(insufficient privileges)\n");
3872ea8dc4b6Seschrock 		return;
3873ea8dc4b6Seschrock 	}
3874ea8dc4b6Seschrock 
387555434c77Sek110237 	(void) printf("errors: Permanent errors have been "
387655434c77Sek110237 	    "detected in the following files:\n\n");
3877ea8dc4b6Seschrock 
387855434c77Sek110237 	pathname = safe_malloc(len);
387955434c77Sek110237 	elem = NULL;
388055434c77Sek110237 	while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
388155434c77Sek110237 		nvlist_t *nv;
388255434c77Sek110237 		uint64_t dsobj, obj;
3883ea8dc4b6Seschrock 
388455434c77Sek110237 		verify(nvpair_value_nvlist(elem, &nv) == 0);
388555434c77Sek110237 		verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
388655434c77Sek110237 		    &dsobj) == 0);
388755434c77Sek110237 		verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
388855434c77Sek110237 		    &obj) == 0);
388955434c77Sek110237 		zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
389055434c77Sek110237 		(void) printf("%7s %s\n", "", pathname);
3891ea8dc4b6Seschrock 	}
389255434c77Sek110237 	free(pathname);
389355434c77Sek110237 	nvlist_free(nverrlist);
3894ea8dc4b6Seschrock }
3895ea8dc4b6Seschrock 
389699653d4eSeschrock static void
389799653d4eSeschrock print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
389899653d4eSeschrock     int namewidth)
389999653d4eSeschrock {
390099653d4eSeschrock 	uint_t i;
390199653d4eSeschrock 	char *name;
390299653d4eSeschrock 
390399653d4eSeschrock 	if (nspares == 0)
390499653d4eSeschrock 		return;
390599653d4eSeschrock 
390699653d4eSeschrock 	(void) printf(gettext("\tspares\n"));
390799653d4eSeschrock 
390899653d4eSeschrock 	for (i = 0; i < nspares; i++) {
390988ecc943SGeorge Wilson 		name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
391099653d4eSeschrock 		print_status_config(zhp, name, spares[i],
3911aa8cf21aSNeil Perrin 		    namewidth, 2, B_TRUE);
391299653d4eSeschrock 		free(name);
391399653d4eSeschrock 	}
391499653d4eSeschrock }
391599653d4eSeschrock 
3916fa94a07fSbrendan static void
3917fa94a07fSbrendan print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3918fa94a07fSbrendan     int namewidth)
3919fa94a07fSbrendan {
3920fa94a07fSbrendan 	uint_t i;
3921fa94a07fSbrendan 	char *name;
3922fa94a07fSbrendan 
3923fa94a07fSbrendan 	if (nl2cache == 0)
3924fa94a07fSbrendan 		return;
3925fa94a07fSbrendan 
3926fa94a07fSbrendan 	(void) printf(gettext("\tcache\n"));
3927fa94a07fSbrendan 
3928fa94a07fSbrendan 	for (i = 0; i < nl2cache; i++) {
392988ecc943SGeorge Wilson 		name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3930fa94a07fSbrendan 		print_status_config(zhp, name, l2cache[i],
3931aa8cf21aSNeil Perrin 		    namewidth, 2, B_FALSE);
3932aa8cf21aSNeil Perrin 		free(name);
3933aa8cf21aSNeil Perrin 	}
3934aa8cf21aSNeil Perrin }
3935aa8cf21aSNeil Perrin 
39369eb19f4dSGeorge Wilson static void
39379eb19f4dSGeorge Wilson print_dedup_stats(nvlist_t *config)
39389eb19f4dSGeorge Wilson {
39399eb19f4dSGeorge Wilson 	ddt_histogram_t *ddh;
39409eb19f4dSGeorge Wilson 	ddt_stat_t *dds;
39419eb19f4dSGeorge Wilson 	ddt_object_t *ddo;
39429eb19f4dSGeorge Wilson 	uint_t c;
39439eb19f4dSGeorge Wilson 
39449eb19f4dSGeorge Wilson 	/*
39459eb19f4dSGeorge Wilson 	 * If the pool was faulted then we may not have been able to
39462384d9f8SGeorge Wilson 	 * obtain the config. Otherwise, if we have anything in the dedup
39479eb19f4dSGeorge Wilson 	 * table continue processing the stats.
39489eb19f4dSGeorge Wilson 	 */
39499eb19f4dSGeorge Wilson 	if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
3950ce72e614SYuri Pankov 	    (uint64_t **)&ddo, &c) != 0)
39519eb19f4dSGeorge Wilson 		return;
39529eb19f4dSGeorge Wilson 
39539eb19f4dSGeorge Wilson 	(void) printf("\n");
3954ce72e614SYuri Pankov 	(void) printf(gettext(" dedup: "));
3955ce72e614SYuri Pankov 	if (ddo->ddo_count == 0) {
3956ce72e614SYuri Pankov 		(void) printf(gettext("no DDT entries\n"));
3957ce72e614SYuri Pankov 		return;
3958ce72e614SYuri Pankov 	}
3959ce72e614SYuri Pankov 
39609eb19f4dSGeorge Wilson 	(void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
39619eb19f4dSGeorge Wilson 	    (u_longlong_t)ddo->ddo_count,
39629eb19f4dSGeorge Wilson 	    (u_longlong_t)ddo->ddo_dspace,
39639eb19f4dSGeorge Wilson 	    (u_longlong_t)ddo->ddo_mspace);
39649eb19f4dSGeorge Wilson 
39659eb19f4dSGeorge Wilson 	verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
39669eb19f4dSGeorge Wilson 	    (uint64_t **)&dds, &c) == 0);
39679eb19f4dSGeorge Wilson 	verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
39689eb19f4dSGeorge Wilson 	    (uint64_t **)&ddh, &c) == 0);
39699eb19f4dSGeorge Wilson 	zpool_dump_ddt(dds, ddh);
39709eb19f4dSGeorge Wilson }
39719eb19f4dSGeorge Wilson 
3972aa8cf21aSNeil Perrin /*
3973fa9e4066Sahrens  * Display a summary of pool status.  Displays a summary such as:
3974fa9e4066Sahrens  *
3975fa9e4066Sahrens  *        pool: tank
3976fa9e4066Sahrens  *	status: DEGRADED
3977fa9e4066Sahrens  *	reason: One or more devices ...
3978654b400cSJoshua M. Clulow  *         see: http://illumos.org/msg/ZFS-xxxx-01
3979fa9e4066Sahrens  *	config:
3980fa9e4066Sahrens  *		mirror		DEGRADED
3981fa9e4066Sahrens  *                c1t0d0	OK
3982ea8dc4b6Seschrock  *                c2t0d0	UNAVAIL
3983fa9e4066Sahrens  *
3984fa9e4066Sahrens  * When given the '-v' option, we print out the complete config.  If the '-e'
3985fa9e4066Sahrens  * option is specified, then we print out error rate information as well.
3986fa9e4066Sahrens  */
3987fa9e4066Sahrens int
3988fa9e4066Sahrens status_callback(zpool_handle_t *zhp, void *data)
3989fa9e4066Sahrens {
3990fa9e4066Sahrens 	status_cbdata_t *cbp = data;
3991fa9e4066Sahrens 	nvlist_t *config, *nvroot;
3992fa9e4066Sahrens 	char *msgid;
3993fa9e4066Sahrens 	int reason;
399446657f8dSmmusante 	const char *health;
399546657f8dSmmusante 	uint_t c;
399646657f8dSmmusante 	vdev_stat_t *vs;
3997fa9e4066Sahrens 
3998088e9d47Seschrock 	config = zpool_get_config(zhp, NULL);
3999fa9e4066Sahrens 	reason = zpool_get_status(zhp, &msgid);
4000fa9e4066Sahrens 
4001fa9e4066Sahrens 	cbp->cb_count++;
4002fa9e4066Sahrens 
4003fa9e4066Sahrens 	/*
4004fa9e4066Sahrens 	 * If we were given 'zpool status -x', only report those pools with
4005fa9e4066Sahrens 	 * problems.
4006fa9e4066Sahrens 	 */
4007b3a6f804STim Connors 	if (cbp->cb_explain &&
4008b3a6f804STim Connors 	    (reason == ZPOOL_STATUS_OK ||
4009b3a6f804STim Connors 	    reason == ZPOOL_STATUS_VERSION_OLDER ||
4010b3a6f804STim Connors 	    reason == ZPOOL_STATUS_FEAT_DISABLED)) {
4011e9dbad6fSeschrock 		if (!cbp->cb_allpools) {
4012e9dbad6fSeschrock 			(void) printf(gettext("pool '%s' is healthy\n"),
4013e9dbad6fSeschrock 			    zpool_get_name(zhp));
4014e9dbad6fSeschrock 			if (cbp->cb_first)
4015e9dbad6fSeschrock 				cbp->cb_first = B_FALSE;
4016e9dbad6fSeschrock 		}
4017fa9e4066Sahrens 		return (0);
4018e9dbad6fSeschrock 	}
4019fa9e4066Sahrens 
4020fa9e4066Sahrens 	if (cbp->cb_first)
402199653d4eSeschrock 		cbp->cb_first = B_FALSE;
4022fa9e4066Sahrens 	else
4023fa9e4066Sahrens 		(void) printf("\n");
4024fa9e4066Sahrens 
402546657f8dSmmusante 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
402646657f8dSmmusante 	    &nvroot) == 0);
40273f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
402846657f8dSmmusante 	    (uint64_t **)&vs, &c) == 0);
4029990b4856Slling 	health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4030fa9e4066Sahrens 
4031fa9e4066Sahrens 	(void) printf(gettext("  pool: %s\n"), zpool_get_name(zhp));
4032fa9e4066Sahrens 	(void) printf(gettext(" state: %s\n"), health);
4033fa9e4066Sahrens 
4034fa9e4066Sahrens 	switch (reason) {
4035fa9e4066Sahrens 	case ZPOOL_STATUS_MISSING_DEV_R:
4036fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices could not "
4037fa9e4066Sahrens 		    "be opened.  Sufficient replicas exist for\n\tthe pool to "
4038fa9e4066Sahrens 		    "continue functioning in a degraded state.\n"));
4039fa9e4066Sahrens 		(void) printf(gettext("action: Attach the missing device and "
4040fa9e4066Sahrens 		    "online it using 'zpool online'.\n"));
4041fa9e4066Sahrens 		break;
4042fa9e4066Sahrens 
4043fa9e4066Sahrens 	case ZPOOL_STATUS_MISSING_DEV_NR:
4044fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices could not "
4045fa9e4066Sahrens 		    "be opened.  There are insufficient\n\treplicas for the "
4046fa9e4066Sahrens 		    "pool to continue functioning.\n"));
4047fa9e4066Sahrens 		(void) printf(gettext("action: Attach the missing device and "
4048fa9e4066Sahrens 		    "online it using 'zpool online'.\n"));
4049fa9e4066Sahrens 		break;
4050fa9e4066Sahrens 
4051fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_LABEL_R:
4052fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices could not "
4053fa9e4066Sahrens 		    "be used because the label is missing or\n\tinvalid.  "
4054fa9e4066Sahrens 		    "Sufficient replicas exist for the pool to continue\n\t"
4055fa9e4066Sahrens 		    "functioning in a degraded state.\n"));
4056fa9e4066Sahrens 		(void) printf(gettext("action: Replace the device using "
4057fa9e4066Sahrens 		    "'zpool replace'.\n"));
4058fa9e4066Sahrens 		break;
4059fa9e4066Sahrens 
4060fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4061fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices could not "
4062b1b8ab34Slling 		    "be used because the label is missing \n\tor invalid.  "
4063fa9e4066Sahrens 		    "There are insufficient replicas for the pool to "
4064fa9e4066Sahrens 		    "continue\n\tfunctioning.\n"));
4065468c413aSTim Haley 		zpool_explain_recover(zpool_get_handle(zhp),
4066468c413aSTim Haley 		    zpool_get_name(zhp), reason, config);
4067fa9e4066Sahrens 		break;
4068fa9e4066Sahrens 
4069fa9e4066Sahrens 	case ZPOOL_STATUS_FAILING_DEV:
4070fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices has "
4071fa9e4066Sahrens 		    "experienced an unrecoverable error.  An\n\tattempt was "
4072fa9e4066Sahrens 		    "made to correct the error.  Applications are "
4073fa9e4066Sahrens 		    "unaffected.\n"));
4074fa9e4066Sahrens 		(void) printf(gettext("action: Determine if the device needs "
4075fa9e4066Sahrens 		    "to be replaced, and clear the errors\n\tusing "
4076ea8dc4b6Seschrock 		    "'zpool clear' or replace the device with 'zpool "
4077fa9e4066Sahrens 		    "replace'.\n"));
4078fa9e4066Sahrens 		break;
4079fa9e4066Sahrens 
4080fa9e4066Sahrens 	case ZPOOL_STATUS_OFFLINE_DEV:
4081fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices has "
4082d7d4af51Smmusante 		    "been taken offline by the administrator.\n\tSufficient "
4083fa9e4066Sahrens 		    "replicas exist for the pool to continue functioning in "
4084fa9e4066Sahrens 		    "a\n\tdegraded state.\n"));
4085fa9e4066Sahrens 		(void) printf(gettext("action: Online the device using "
4086fa9e4066Sahrens 		    "'zpool online' or replace the device with\n\t'zpool "
4087fa9e4066Sahrens 		    "replace'.\n"));
4088fa9e4066Sahrens 		break;
4089fa9e4066Sahrens 
4090c25309d4SGeorge Wilson 	case ZPOOL_STATUS_REMOVED_DEV:
4091c25309d4SGeorge Wilson 		(void) printf(gettext("status: One or more devices has "
4092c25309d4SGeorge Wilson 		    "been removed by the administrator.\n\tSufficient "
4093c25309d4SGeorge Wilson 		    "replicas exist for the pool to continue functioning in "
4094c25309d4SGeorge Wilson 		    "a\n\tdegraded state.\n"));
4095c25309d4SGeorge Wilson 		(void) printf(gettext("action: Online the device using "
4096c25309d4SGeorge Wilson 		    "'zpool online' or replace the device with\n\t'zpool "
4097c25309d4SGeorge Wilson 		    "replace'.\n"));
4098c25309d4SGeorge Wilson 		break;
4099c25309d4SGeorge Wilson 
4100fa9e4066Sahrens 	case ZPOOL_STATUS_RESILVERING:
4101fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices is "
4102fa9e4066Sahrens 		    "currently being resilvered.  The pool will\n\tcontinue "
4103fa9e4066Sahrens 		    "to function, possibly in a degraded state.\n"));
4104fa9e4066Sahrens 		(void) printf(gettext("action: Wait for the resilver to "
4105fa9e4066Sahrens 		    "complete.\n"));
4106fa9e4066Sahrens 		break;
4107fa9e4066Sahrens 
4108ea8dc4b6Seschrock 	case ZPOOL_STATUS_CORRUPT_DATA:
4109ea8dc4b6Seschrock 		(void) printf(gettext("status: One or more devices has "
4110ea8dc4b6Seschrock 		    "experienced an error resulting in data\n\tcorruption.  "
4111ea8dc4b6Seschrock 		    "Applications may be affected.\n"));
4112ea8dc4b6Seschrock 		(void) printf(gettext("action: Restore the file in question "
4113ea8dc4b6Seschrock 		    "if possible.  Otherwise restore the\n\tentire pool from "
4114ea8dc4b6Seschrock 		    "backup.\n"));
4115ea8dc4b6Seschrock 		break;
4116ea8dc4b6Seschrock 
4117ea8dc4b6Seschrock 	case ZPOOL_STATUS_CORRUPT_POOL:
4118ea8dc4b6Seschrock 		(void) printf(gettext("status: The pool metadata is corrupted "
4119ea8dc4b6Seschrock 		    "and the pool cannot be opened.\n"));
4120468c413aSTim Haley 		zpool_explain_recover(zpool_get_handle(zhp),
4121468c413aSTim Haley 		    zpool_get_name(zhp), reason, config);
4122ea8dc4b6Seschrock 		break;
4123ea8dc4b6Seschrock 
4124eaca9bbdSeschrock 	case ZPOOL_STATUS_VERSION_OLDER:
412557221772SChristopher Siden 		(void) printf(gettext("status: The pool is formatted using a "
412657221772SChristopher Siden 		    "legacy on-disk format.  The pool can\n\tstill be used, "
412757221772SChristopher Siden 		    "but some features are unavailable.\n"));
4128eaca9bbdSeschrock 		(void) printf(gettext("action: Upgrade the pool using 'zpool "
4129eaca9bbdSeschrock 		    "upgrade'.  Once this is done, the\n\tpool will no longer "
413057221772SChristopher Siden 		    "be accessible on software that does not support feature\n"
413157221772SChristopher Siden 		    "\tflags.\n"));
4132eaca9bbdSeschrock 		break;
4133eaca9bbdSeschrock 
4134eaca9bbdSeschrock 	case ZPOOL_STATUS_VERSION_NEWER:
4135eaca9bbdSeschrock 		(void) printf(gettext("status: The pool has been upgraded to a "
4136eaca9bbdSeschrock 		    "newer, incompatible on-disk version.\n\tThe pool cannot "
4137eaca9bbdSeschrock 		    "be accessed on this system.\n"));
4138eaca9bbdSeschrock 		(void) printf(gettext("action: Access the pool from a system "
4139eaca9bbdSeschrock 		    "running more recent software, or\n\trestore the pool from "
4140eaca9bbdSeschrock 		    "backup.\n"));
4141eaca9bbdSeschrock 		break;
4142eaca9bbdSeschrock 
414357221772SChristopher Siden 	case ZPOOL_STATUS_FEAT_DISABLED:
414457221772SChristopher Siden 		(void) printf(gettext("status: Some supported features are not "
414557221772SChristopher Siden 		    "enabled on the pool. The pool can\n\tstill be used, but "
414657221772SChristopher Siden 		    "some features are unavailable.\n"));
414757221772SChristopher Siden 		(void) printf(gettext("action: Enable all features using "
414857221772SChristopher Siden 		    "'zpool upgrade'. Once this is done,\n\tthe pool may no "
414957221772SChristopher Siden 		    "longer be accessible by software that does not support\n\t"
415057221772SChristopher Siden 		    "the features. See zpool-features(5) for details.\n"));
415157221772SChristopher Siden 		break;
415257221772SChristopher Siden 
4153ad135b5dSChristopher Siden 	case ZPOOL_STATUS_UNSUP_FEAT_READ:
4154ad135b5dSChristopher Siden 		(void) printf(gettext("status: The pool cannot be accessed on "
4155ad135b5dSChristopher Siden 		    "this system because it uses the\n\tfollowing feature(s) "
4156ad135b5dSChristopher Siden 		    "not supported on this system:\n"));
4157ad135b5dSChristopher Siden 		zpool_print_unsup_feat(config);
4158ad135b5dSChristopher Siden 		(void) printf("\n");
4159ad135b5dSChristopher Siden 		(void) printf(gettext("action: Access the pool from a system "
4160ad135b5dSChristopher Siden 		    "that supports the required feature(s),\n\tor restore the "
4161ad135b5dSChristopher Siden 		    "pool from backup.\n"));
4162ad135b5dSChristopher Siden 		break;
4163ad135b5dSChristopher Siden 
4164ad135b5dSChristopher Siden 	case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4165ad135b5dSChristopher Siden 		(void) printf(gettext("status: The pool can only be accessed "
4166ad135b5dSChristopher Siden 		    "in read-only mode on this system. It\n\tcannot be "
4167ad135b5dSChristopher Siden 		    "accessed in read-write mode because it uses the "
4168ad135b5dSChristopher Siden 		    "following\n\tfeature(s) not supported on this system:\n"));
4169ad135b5dSChristopher Siden 		zpool_print_unsup_feat(config);
4170ad135b5dSChristopher Siden 		(void) printf("\n");
4171ad135b5dSChristopher Siden 		(void) printf(gettext("action: The pool cannot be accessed in "
4172ad135b5dSChristopher Siden 		    "read-write mode. Import the pool with\n"
4173ad135b5dSChristopher Siden 		    "\t\"-o readonly=on\", access the pool from a system that "
4174ad135b5dSChristopher Siden 		    "supports the\n\trequired feature(s), or restore the "
4175ad135b5dSChristopher Siden 		    "pool from backup.\n"));
4176ad135b5dSChristopher Siden 		break;
4177ad135b5dSChristopher Siden 
41783d7072f8Seschrock 	case ZPOOL_STATUS_FAULTED_DEV_R:
41793d7072f8Seschrock 		(void) printf(gettext("status: One or more devices are "
41803d7072f8Seschrock 		    "faulted in response to persistent errors.\n\tSufficient "
41813d7072f8Seschrock 		    "replicas exist for the pool to continue functioning "
41823d7072f8Seschrock 		    "in a\n\tdegraded state.\n"));
41833d7072f8Seschrock 		(void) printf(gettext("action: Replace the faulted device, "
41843d7072f8Seschrock 		    "or use 'zpool clear' to mark the device\n\trepaired.\n"));
41853d7072f8Seschrock 		break;
41863d7072f8Seschrock 
41873d7072f8Seschrock 	case ZPOOL_STATUS_FAULTED_DEV_NR:
41883d7072f8Seschrock 		(void) printf(gettext("status: One or more devices are "
41893d7072f8Seschrock 		    "faulted in response to persistent errors.  There are "
41903d7072f8Seschrock 		    "insufficient replicas for the pool to\n\tcontinue "
41913d7072f8Seschrock 		    "functioning.\n"));
41923d7072f8Seschrock 		(void) printf(gettext("action: Destroy and re-create the pool "
41933d7072f8Seschrock 		    "from a backup source.  Manually marking the device\n"
41943d7072f8Seschrock 		    "\trepaired using 'zpool clear' may allow some data "
41953d7072f8Seschrock 		    "to be recovered.\n"));
41963d7072f8Seschrock 		break;
41973d7072f8Seschrock 
419832b87932Sek110237 	case ZPOOL_STATUS_IO_FAILURE_WAIT:
419932b87932Sek110237 	case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
420032b87932Sek110237 		(void) printf(gettext("status: One or more devices are "
42018a79c1b5Sek110237 		    "faulted in response to IO failures.\n"));
420232b87932Sek110237 		(void) printf(gettext("action: Make sure the affected devices "
420332b87932Sek110237 		    "are connected, then run 'zpool clear'.\n"));
420432b87932Sek110237 		break;
420532b87932Sek110237 
4206b87f3af3Sperrin 	case ZPOOL_STATUS_BAD_LOG:
4207b87f3af3Sperrin 		(void) printf(gettext("status: An intent log record "
4208b87f3af3Sperrin 		    "could not be read.\n"
4209b87f3af3Sperrin 		    "\tWaiting for adminstrator intervention to fix the "
4210b87f3af3Sperrin 		    "faulted pool.\n"));
4211b87f3af3Sperrin 		(void) printf(gettext("action: Either restore the affected "
4212b87f3af3Sperrin 		    "device(s) and run 'zpool online',\n"
4213b87f3af3Sperrin 		    "\tor ignore the intent log records by running "
4214b87f3af3Sperrin 		    "'zpool clear'.\n"));
4215b87f3af3Sperrin 		break;
4216b87f3af3Sperrin 
4217fa9e4066Sahrens 	default:
4218fa9e4066Sahrens 		/*
4219fa9e4066Sahrens 		 * The remaining errors can't actually be generated, yet.
4220fa9e4066Sahrens 		 */
4221fa9e4066Sahrens 		assert(reason == ZPOOL_STATUS_OK);
4222fa9e4066Sahrens 	}
4223fa9e4066Sahrens 
4224fa9e4066Sahrens 	if (msgid != NULL)
4225654b400cSJoshua M. Clulow 		(void) printf(gettext("   see: http://illumos.org/msg/%s\n"),
4226fa9e4066Sahrens 		    msgid);
4227fa9e4066Sahrens 
4228fa9e4066Sahrens 	if (config != NULL) {
4229fa9e4066Sahrens 		int namewidth;
4230ea8dc4b6Seschrock 		uint64_t nerr;
4231fa94a07fSbrendan 		nvlist_t **spares, **l2cache;
4232fa94a07fSbrendan 		uint_t nspares, nl2cache;
42333f9d6ad7SLin Ling 		pool_scan_stat_t *ps = NULL;
4234fa9e4066Sahrens 
42353f9d6ad7SLin Ling 		(void) nvlist_lookup_uint64_array(nvroot,
42363f9d6ad7SLin Ling 		    ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
42373f9d6ad7SLin Ling 		print_scan_status(ps);
4238fa9e4066Sahrens 
4239c67d9675Seschrock 		namewidth = max_width(zhp, nvroot, 0, 0);
4240fa9e4066Sahrens 		if (namewidth < 10)
4241fa9e4066Sahrens 			namewidth = 10;
4242fa9e4066Sahrens 
4243fa9e4066Sahrens 		(void) printf(gettext("config:\n\n"));
4244fa9e4066Sahrens 		(void) printf(gettext("\t%-*s  %-8s %5s %5s %5s\n"), namewidth,
4245fa9e4066Sahrens 		    "NAME", "STATE", "READ", "WRITE", "CKSUM");
4246c67d9675Seschrock 		print_status_config(zhp, zpool_get_name(zhp), nvroot,
4247aa8cf21aSNeil Perrin 		    namewidth, 0, B_FALSE);
424899653d4eSeschrock 
42494dea40f0SNeil Perrin 		if (num_logs(nvroot) > 0)
4250e6ca193dSGeorge Wilson 			print_logs(zhp, nvroot, namewidth, B_TRUE);
4251fa94a07fSbrendan 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4252fa94a07fSbrendan 		    &l2cache, &nl2cache) == 0)
4253fa94a07fSbrendan 			print_l2cache(zhp, l2cache, nl2cache, namewidth);
4254fa94a07fSbrendan 
425599653d4eSeschrock 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
425699653d4eSeschrock 		    &spares, &nspares) == 0)
425799653d4eSeschrock 			print_spares(zhp, spares, nspares, namewidth);
4258ea8dc4b6Seschrock 
4259ea8dc4b6Seschrock 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4260ea8dc4b6Seschrock 		    &nerr) == 0) {
426155434c77Sek110237 			nvlist_t *nverrlist = NULL;
426255434c77Sek110237 
4263ea8dc4b6Seschrock 			/*
4264ea8dc4b6Seschrock 			 * If the approximate error count is small, get a
4265ea8dc4b6Seschrock 			 * precise count by fetching the entire log and
4266ea8dc4b6Seschrock 			 * uniquifying the results.
4267ea8dc4b6Seschrock 			 */
426875519f38Sek110237 			if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
426955434c77Sek110237 			    zpool_get_errlog(zhp, &nverrlist) == 0) {
427055434c77Sek110237 				nvpair_t *elem;
427155434c77Sek110237 
427255434c77Sek110237 				elem = NULL;
427355434c77Sek110237 				nerr = 0;
427455434c77Sek110237 				while ((elem = nvlist_next_nvpair(nverrlist,
427555434c77Sek110237 				    elem)) != NULL) {
427655434c77Sek110237 					nerr++;
427755434c77Sek110237 				}
427855434c77Sek110237 			}
427955434c77Sek110237 			nvlist_free(nverrlist);
4280ea8dc4b6Seschrock 
4281ea8dc4b6Seschrock 			(void) printf("\n");
428299653d4eSeschrock 
4283ea8dc4b6Seschrock 			if (nerr == 0)
4284ea8dc4b6Seschrock 				(void) printf(gettext("errors: No known data "
4285ea8dc4b6Seschrock 				    "errors\n"));
4286ea8dc4b6Seschrock 			else if (!cbp->cb_verbose)
4287e9dbad6fSeschrock 				(void) printf(gettext("errors: %llu data "
42885ad82045Snd150628 				    "errors, use '-v' for a list\n"),
42895ad82045Snd150628 				    (u_longlong_t)nerr);
4290ea8dc4b6Seschrock 			else
4291ea8dc4b6Seschrock 				print_error_log(zhp);
4292ea8dc4b6Seschrock 		}
42939eb19f4dSGeorge Wilson 
42949eb19f4dSGeorge Wilson 		if (cbp->cb_dedup_stats)
42959eb19f4dSGeorge Wilson 			print_dedup_stats(config);
4296fa9e4066Sahrens 	} else {
4297fa9e4066Sahrens 		(void) printf(gettext("config: The configuration cannot be "
4298fa9e4066Sahrens 		    "determined.\n"));
4299fa9e4066Sahrens 	}
4300fa9e4066Sahrens 
4301fa9e4066Sahrens 	return (0);
4302fa9e4066Sahrens }
4303fa9e4066Sahrens 
4304fa9e4066Sahrens /*
43053f9d6ad7SLin Ling  * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4306fa9e4066Sahrens  *
4307fa9e4066Sahrens  *	-v	Display complete error logs
4308fa9e4066Sahrens  *	-x	Display only pools with potential problems
43099eb19f4dSGeorge Wilson  *	-D	Display dedup status (undocumented)
43103f9d6ad7SLin Ling  *	-T	Display a timestamp in date(1) or Unix format
4311fa9e4066Sahrens  *
4312fa9e4066Sahrens  * Describes the health status of all pools or some subset.
4313fa9e4066Sahrens  */
4314fa9e4066Sahrens int
4315fa9e4066Sahrens zpool_do_status(int argc, char **argv)
4316fa9e4066Sahrens {
4317fa9e4066Sahrens 	int c;
4318fa9e4066Sahrens 	int ret;
43193f9d6ad7SLin Ling 	unsigned long interval = 0, count = 0;
4320fa9e4066Sahrens 	status_cbdata_t cb = { 0 };
4321fa9e4066Sahrens 
4322fa9e4066Sahrens 	/* check options */
43233f9d6ad7SLin Ling 	while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4324fa9e4066Sahrens 		switch (c) {
4325fa9e4066Sahrens 		case 'v':
432699653d4eSeschrock 			cb.cb_verbose = B_TRUE;
4327fa9e4066Sahrens 			break;
4328fa9e4066Sahrens 		case 'x':
432999653d4eSeschrock 			cb.cb_explain = B_TRUE;
4330fa9e4066Sahrens 			break;
43319eb19f4dSGeorge Wilson 		case 'D':
43329eb19f4dSGeorge Wilson 			cb.cb_dedup_stats = B_TRUE;
43339eb19f4dSGeorge Wilson 			break;
43343f9d6ad7SLin Ling 		case 'T':
43353f9d6ad7SLin Ling 			get_timestamp_arg(*optarg);
43363f9d6ad7SLin Ling 			break;
4337fa9e4066Sahrens 		case '?':
4338fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4339fa9e4066Sahrens 			    optopt);
434099653d4eSeschrock 			usage(B_FALSE);
4341fa9e4066Sahrens 		}
4342fa9e4066Sahrens 	}
4343fa9e4066Sahrens 
4344fa9e4066Sahrens 	argc -= optind;
4345fa9e4066Sahrens 	argv += optind;
4346fa9e4066Sahrens 
43473f9d6ad7SLin Ling 	get_interval_count(&argc, argv, &interval, &count);
4348fa9e4066Sahrens 
4349e9dbad6fSeschrock 	if (argc == 0)
4350e9dbad6fSeschrock 		cb.cb_allpools = B_TRUE;
4351e9dbad6fSeschrock 
43523f9d6ad7SLin Ling 	cb.cb_first = B_TRUE;
43533f9d6ad7SLin Ling 
43543f9d6ad7SLin Ling 	for (;;) {
43553f9d6ad7SLin Ling 		if (timestamp_fmt != NODATE)
43563f9d6ad7SLin Ling 			print_timestamp(timestamp_fmt);
43573f9d6ad7SLin Ling 
43583f9d6ad7SLin Ling 		ret = for_each_pool(argc, argv, B_TRUE, NULL,
43593f9d6ad7SLin Ling 		    status_callback, &cb);
4360fa9e4066Sahrens 
4361fa9e4066Sahrens 		if (argc == 0 && cb.cb_count == 0)
4362fa9e4066Sahrens 			(void) printf(gettext("no pools available\n"));
4363e9dbad6fSeschrock 		else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4364fa9e4066Sahrens 			(void) printf(gettext("all pools are healthy\n"));
4365fa9e4066Sahrens 
43663f9d6ad7SLin Ling 		if (ret != 0)
4367fa9e4066Sahrens 			return (ret);
43683f9d6ad7SLin Ling 
43693f9d6ad7SLin Ling 		if (interval == 0)
43703f9d6ad7SLin Ling 			break;
43713f9d6ad7SLin Ling 
43723f9d6ad7SLin Ling 		if (count != 0 && --count == 0)
43733f9d6ad7SLin Ling 			break;
43743f9d6ad7SLin Ling 
43753f9d6ad7SLin Ling 		(void) sleep(interval);
43763f9d6ad7SLin Ling 	}
43773f9d6ad7SLin Ling 
43783f9d6ad7SLin Ling 	return (0);
4379fa9e4066Sahrens }
4380fa9e4066Sahrens 
4381eaca9bbdSeschrock typedef struct upgrade_cbdata {
4382eaca9bbdSeschrock 	int	cb_first;
438306eeb2adSek110237 	int	cb_argc;
4384990b4856Slling 	uint64_t cb_version;
438506eeb2adSek110237 	char	**cb_argv;
4386eaca9bbdSeschrock } upgrade_cbdata_t;
4387eaca9bbdSeschrock 
4388eaca9bbdSeschrock static int
438957221772SChristopher Siden upgrade_version(zpool_handle_t *zhp, uint64_t version)
439057221772SChristopher Siden {
439157221772SChristopher Siden 	int ret;
439257221772SChristopher Siden 	nvlist_t *config;
439357221772SChristopher Siden 	uint64_t oldversion;
439457221772SChristopher Siden 
439557221772SChristopher Siden 	config = zpool_get_config(zhp, NULL);
439657221772SChristopher Siden 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
439757221772SChristopher Siden 	    &oldversion) == 0);
439857221772SChristopher Siden 
439957221772SChristopher Siden 	assert(SPA_VERSION_IS_SUPPORTED(oldversion));
440057221772SChristopher Siden 	assert(oldversion < version);
440157221772SChristopher Siden 
440257221772SChristopher Siden 	ret = zpool_upgrade(zhp, version);
440357221772SChristopher Siden 	if (ret != 0)
440457221772SChristopher Siden 		return (ret);
440557221772SChristopher Siden 
440657221772SChristopher Siden 	if (version >= SPA_VERSION_FEATURES) {
440757221772SChristopher Siden 		(void) printf(gettext("Successfully upgraded "
440857221772SChristopher Siden 		    "'%s' from version %llu to feature flags.\n"),
440957221772SChristopher Siden 		    zpool_get_name(zhp), oldversion);
441057221772SChristopher Siden 	} else {
441157221772SChristopher Siden 		(void) printf(gettext("Successfully upgraded "
441257221772SChristopher Siden 		    "'%s' from version %llu to version %llu.\n"),
441357221772SChristopher Siden 		    zpool_get_name(zhp), oldversion, version);
441457221772SChristopher Siden 	}
441557221772SChristopher Siden 
441657221772SChristopher Siden 	return (0);
441757221772SChristopher Siden }
441857221772SChristopher Siden 
441957221772SChristopher Siden static int
442057221772SChristopher Siden upgrade_enable_all(zpool_handle_t *zhp, int *countp)
442157221772SChristopher Siden {
442257221772SChristopher Siden 	int i, ret, count;
442357221772SChristopher Siden 	boolean_t firstff = B_TRUE;
442457221772SChristopher Siden 	nvlist_t *enabled = zpool_get_features(zhp);
442557221772SChristopher Siden 
442657221772SChristopher Siden 	count = 0;
442757221772SChristopher Siden 	for (i = 0; i < SPA_FEATURES; i++) {
442857221772SChristopher Siden 		const char *fname = spa_feature_table[i].fi_uname;
442957221772SChristopher Siden 		const char *fguid = spa_feature_table[i].fi_guid;
443057221772SChristopher Siden 		if (!nvlist_exists(enabled, fguid)) {
443157221772SChristopher Siden 			char *propname;
443257221772SChristopher Siden 			verify(-1 != asprintf(&propname, "feature@%s", fname));
443357221772SChristopher Siden 			ret = zpool_set_prop(zhp, propname,
443457221772SChristopher Siden 			    ZFS_FEATURE_ENABLED);
443557221772SChristopher Siden 			if (ret != 0) {
443657221772SChristopher Siden 				free(propname);
443757221772SChristopher Siden 				return (ret);
443857221772SChristopher Siden 			}
443957221772SChristopher Siden 			count++;
444057221772SChristopher Siden 
444157221772SChristopher Siden 			if (firstff) {
444257221772SChristopher Siden 				(void) printf(gettext("Enabled the "
444357221772SChristopher Siden 				    "following features on '%s':\n"),
444457221772SChristopher Siden 				    zpool_get_name(zhp));
444557221772SChristopher Siden 				firstff = B_FALSE;
444657221772SChristopher Siden 			}
444757221772SChristopher Siden 			(void) printf(gettext("  %s\n"), fname);
444857221772SChristopher Siden 			free(propname);
444957221772SChristopher Siden 		}
445057221772SChristopher Siden 	}
445157221772SChristopher Siden 
445257221772SChristopher Siden 	if (countp != NULL)
445357221772SChristopher Siden 		*countp = count;
445457221772SChristopher Siden 	return (0);
445557221772SChristopher Siden }
445657221772SChristopher Siden 
445757221772SChristopher Siden static int
4458eaca9bbdSeschrock upgrade_cb(zpool_handle_t *zhp, void *arg)
4459eaca9bbdSeschrock {
4460eaca9bbdSeschrock 	upgrade_cbdata_t *cbp = arg;
4461eaca9bbdSeschrock 	nvlist_t *config;
4462eaca9bbdSeschrock 	uint64_t version;
446357221772SChristopher Siden 	boolean_t printnl = B_FALSE;
446457221772SChristopher Siden 	int ret;
4465eaca9bbdSeschrock 
4466eaca9bbdSeschrock 	config = zpool_get_config(zhp, NULL);
4467eaca9bbdSeschrock 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4468eaca9bbdSeschrock 	    &version) == 0);
4469eaca9bbdSeschrock 
447057221772SChristopher Siden 	assert(SPA_VERSION_IS_SUPPORTED(version));
4471eaca9bbdSeschrock 
447257221772SChristopher Siden 	if (version < cbp->cb_version) {
447399653d4eSeschrock 		cbp->cb_first = B_FALSE;
447457221772SChristopher Siden 		ret = upgrade_version(zhp, cbp->cb_version);
447557221772SChristopher Siden 		if (ret != 0)
447657221772SChristopher Siden 			return (ret);
447757221772SChristopher Siden 		printnl = B_TRUE;
447857221772SChristopher Siden 
44794445fffbSMatthew Ahrens 		/*
44804445fffbSMatthew Ahrens 		 * If they did "zpool upgrade -a", then we could
44814445fffbSMatthew Ahrens 		 * be doing ioctls to different pools.  We need
44824445fffbSMatthew Ahrens 		 * to log this history once to each pool, and bypass
44834445fffbSMatthew Ahrens 		 * the normal history logging that happens in main().
44844445fffbSMatthew Ahrens 		 */
44854445fffbSMatthew Ahrens 		(void) zpool_log_history(g_zfs, history_str);
44864445fffbSMatthew Ahrens 		log_history = B_FALSE;
448706eeb2adSek110237 	}
4488eaca9bbdSeschrock 
448957221772SChristopher Siden 	if (cbp->cb_version >= SPA_VERSION_FEATURES) {
449057221772SChristopher Siden 		int count;
449157221772SChristopher Siden 		ret = upgrade_enable_all(zhp, &count);
449257221772SChristopher Siden 		if (ret != 0)
449357221772SChristopher Siden 			return (ret);
449457221772SChristopher Siden 
449557221772SChristopher Siden 		if (count > 0) {
449657221772SChristopher Siden 			cbp->cb_first = B_FALSE;
449757221772SChristopher Siden 			printnl = B_TRUE;
449857221772SChristopher Siden 		}
449957221772SChristopher Siden 	}
450057221772SChristopher Siden 
450157221772SChristopher Siden 	if (printnl) {
450257221772SChristopher Siden 		(void) printf(gettext("\n"));
450357221772SChristopher Siden 	}
450457221772SChristopher Siden 
450557221772SChristopher Siden 	return (0);
450657221772SChristopher Siden }
450757221772SChristopher Siden 
450857221772SChristopher Siden static int
450957221772SChristopher Siden upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
451057221772SChristopher Siden {
451157221772SChristopher Siden 	upgrade_cbdata_t *cbp = arg;
451257221772SChristopher Siden 	nvlist_t *config;
451357221772SChristopher Siden 	uint64_t version;
451457221772SChristopher Siden 
451557221772SChristopher Siden 	config = zpool_get_config(zhp, NULL);
451657221772SChristopher Siden 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
451757221772SChristopher Siden 	    &version) == 0);
451857221772SChristopher Siden 
451957221772SChristopher Siden 	assert(SPA_VERSION_IS_SUPPORTED(version));
452057221772SChristopher Siden 
452157221772SChristopher Siden 	if (version < SPA_VERSION_FEATURES) {
4522eaca9bbdSeschrock 		if (cbp->cb_first) {
4523eaca9bbdSeschrock 			(void) printf(gettext("The following pools are "
452457221772SChristopher Siden 			    "formatted with legacy version numbers and can\n"
452557221772SChristopher Siden 			    "be upgraded to use feature flags.  After "
452657221772SChristopher Siden 			    "being upgraded, these pools\nwill no "
452757221772SChristopher Siden 			    "longer be accessible by software that does not "
452857221772SChristopher Siden 			    "support feature\nflags.\n\n"));
4529eaca9bbdSeschrock 			(void) printf(gettext("VER  POOL\n"));
4530eaca9bbdSeschrock 			(void) printf(gettext("---  ------------\n"));
453199653d4eSeschrock 			cbp->cb_first = B_FALSE;
4532eaca9bbdSeschrock 		}
4533eaca9bbdSeschrock 
45345ad82045Snd150628 		(void) printf("%2llu   %s\n", (u_longlong_t)version,
4535eaca9bbdSeschrock 		    zpool_get_name(zhp));
4536eaca9bbdSeschrock 	}
4537eaca9bbdSeschrock 
453857221772SChristopher Siden 	return (0);
453957221772SChristopher Siden }
454057221772SChristopher Siden 
454157221772SChristopher Siden static int
454257221772SChristopher Siden upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
454357221772SChristopher Siden {
454457221772SChristopher Siden 	upgrade_cbdata_t *cbp = arg;
454557221772SChristopher Siden 	nvlist_t *config;
454657221772SChristopher Siden 	uint64_t version;
454757221772SChristopher Siden 
454857221772SChristopher Siden 	config = zpool_get_config(zhp, NULL);
454957221772SChristopher Siden 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
455057221772SChristopher Siden 	    &version) == 0);
455157221772SChristopher Siden 
455257221772SChristopher Siden 	if (version >= SPA_VERSION_FEATURES) {
455357221772SChristopher Siden 		int i;
455457221772SChristopher Siden 		boolean_t poolfirst = B_TRUE;
455557221772SChristopher Siden 		nvlist_t *enabled = zpool_get_features(zhp);
455657221772SChristopher Siden 
455757221772SChristopher Siden 		for (i = 0; i < SPA_FEATURES; i++) {
455857221772SChristopher Siden 			const char *fguid = spa_feature_table[i].fi_guid;
455957221772SChristopher Siden 			const char *fname = spa_feature_table[i].fi_uname;
456057221772SChristopher Siden 			if (!nvlist_exists(enabled, fguid)) {
456157221772SChristopher Siden 				if (cbp->cb_first) {
456257221772SChristopher Siden 					(void) printf(gettext("\nSome "
456357221772SChristopher Siden 					    "supported features are not "
456457221772SChristopher Siden 					    "enabled on the following pools. "
456557221772SChristopher Siden 					    "Once a\nfeature is enabled the "
456657221772SChristopher Siden 					    "pool may become incompatible with "
456757221772SChristopher Siden 					    "software\nthat does not support "
456857221772SChristopher Siden 					    "the feature. See "
456957221772SChristopher Siden 					    "zpool-features(5) for "
457057221772SChristopher Siden 					    "details.\n\n"));
457157221772SChristopher Siden 					(void) printf(gettext("POOL  "
457257221772SChristopher Siden 					    "FEATURE\n"));
457357221772SChristopher Siden 					(void) printf(gettext("------"
457457221772SChristopher Siden 					    "---------\n"));
457557221772SChristopher Siden 					cbp->cb_first = B_FALSE;
457657221772SChristopher Siden 				}
457757221772SChristopher Siden 
457857221772SChristopher Siden 				if (poolfirst) {
457957221772SChristopher Siden 					(void) printf(gettext("%s\n"),
458057221772SChristopher Siden 					    zpool_get_name(zhp));
458157221772SChristopher Siden 					poolfirst = B_FALSE;
458257221772SChristopher Siden 				}
458357221772SChristopher Siden 
458457221772SChristopher Siden 				(void) printf(gettext("      %s\n"), fname);
458557221772SChristopher Siden 			}
458657221772SChristopher Siden 		}
458757221772SChristopher Siden 	}
458857221772SChristopher Siden 
458957221772SChristopher Siden 	return (0);
4590eaca9bbdSeschrock }
4591eaca9bbdSeschrock 
4592eaca9bbdSeschrock /* ARGSUSED */
4593eaca9bbdSeschrock static int
459406eeb2adSek110237 upgrade_one(zpool_handle_t *zhp, void *data)
4595eaca9bbdSeschrock {
459657221772SChristopher Siden 	boolean_t printnl = B_FALSE;
4597990b4856Slling 	upgrade_cbdata_t *cbp = data;
4598990b4856Slling 	uint64_t cur_version;
4599eaca9bbdSeschrock 	int ret;
4600eaca9bbdSeschrock 
46018654d025Sperrin 	if (strcmp("log", zpool_get_name(zhp)) == 0) {
46028654d025Sperrin 		(void) printf(gettext("'log' is now a reserved word\n"
46038654d025Sperrin 		    "Pool 'log' must be renamed using export and import"
46048654d025Sperrin 		    " to upgrade.\n"));
46058654d025Sperrin 		return (1);
46068654d025Sperrin 	}
4607990b4856Slling 
4608990b4856Slling 	cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4609e6c728e1Sbrendan 	if (cur_version > cbp->cb_version) {
4610eaca9bbdSeschrock 		(void) printf(gettext("Pool '%s' is already formatted "
461157221772SChristopher Siden 		    "using more current version '%llu'.\n\n"),
4612e6c728e1Sbrendan 		    zpool_get_name(zhp), cur_version);
4613e6c728e1Sbrendan 		return (0);
4614e6c728e1Sbrendan 	}
461557221772SChristopher Siden 
461657221772SChristopher Siden 	if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
4617e6c728e1Sbrendan 		(void) printf(gettext("Pool '%s' is already formatted "
461857221772SChristopher Siden 		    "using version %llu.\n\n"), zpool_get_name(zhp),
461957221772SChristopher Siden 		    cbp->cb_version);
4620eaca9bbdSeschrock 		return (0);
4621eaca9bbdSeschrock 	}
4622eaca9bbdSeschrock 
462357221772SChristopher Siden 	if (cur_version != cbp->cb_version) {
462457221772SChristopher Siden 		printnl = B_TRUE;
462557221772SChristopher Siden 		ret = upgrade_version(zhp, cbp->cb_version);
462657221772SChristopher Siden 		if (ret != 0)
462757221772SChristopher Siden 			return (ret);
462806eeb2adSek110237 	}
4629eaca9bbdSeschrock 
463057221772SChristopher Siden 	if (cbp->cb_version >= SPA_VERSION_FEATURES) {
463157221772SChristopher Siden 		int count = 0;
463257221772SChristopher Siden 		ret = upgrade_enable_all(zhp, &count);
463357221772SChristopher Siden 		if (ret != 0)
463457221772SChristopher Siden 			return (ret);
463557221772SChristopher Siden 
463657221772SChristopher Siden 		if (count != 0) {
463757221772SChristopher Siden 			printnl = B_TRUE;
463857221772SChristopher Siden 		} else if (cur_version == SPA_VERSION) {
463957221772SChristopher Siden 			(void) printf(gettext("Pool '%s' already has all "
464057221772SChristopher Siden 			    "supported features enabled.\n"),
464157221772SChristopher Siden 			    zpool_get_name(zhp));
464257221772SChristopher Siden 		}
464357221772SChristopher Siden 	}
464457221772SChristopher Siden 
464557221772SChristopher Siden 	if (printnl) {
464657221772SChristopher Siden 		(void) printf(gettext("\n"));
464757221772SChristopher Siden 	}
464857221772SChristopher Siden 
464957221772SChristopher Siden 	return (0);
4650eaca9bbdSeschrock }
4651eaca9bbdSeschrock 
4652eaca9bbdSeschrock /*
4653eaca9bbdSeschrock  * zpool upgrade
4654eaca9bbdSeschrock  * zpool upgrade -v
4655990b4856Slling  * zpool upgrade [-V version] <-a | pool ...>
4656eaca9bbdSeschrock  *
4657eaca9bbdSeschrock  * With no arguments, display downrev'd ZFS pool available for upgrade.
4658eaca9bbdSeschrock  * Individual pools can be upgraded by specifying the pool, and '-a' will
4659eaca9bbdSeschrock  * upgrade all pools.
4660eaca9bbdSeschrock  */
4661eaca9bbdSeschrock int
4662eaca9bbdSeschrock zpool_do_upgrade(int argc, char **argv)
4663eaca9bbdSeschrock {
4664eaca9bbdSeschrock 	int c;
4665eaca9bbdSeschrock 	upgrade_cbdata_t cb = { 0 };
4666eaca9bbdSeschrock 	int ret = 0;
4667eaca9bbdSeschrock 	boolean_t showversions = B_FALSE;
466857221772SChristopher Siden 	boolean_t upgradeall = B_FALSE;
4669990b4856Slling 	char *end;
4670990b4856Slling 
4671eaca9bbdSeschrock 
4672eaca9bbdSeschrock 	/* check options */
4673478ed9adSEric Taylor 	while ((c = getopt(argc, argv, ":avV:")) != -1) {
4674eaca9bbdSeschrock 		switch (c) {
4675eaca9bbdSeschrock 		case 'a':
467657221772SChristopher Siden 			upgradeall = B_TRUE;
4677eaca9bbdSeschrock 			break;
4678eaca9bbdSeschrock 		case 'v':
4679eaca9bbdSeschrock 			showversions = B_TRUE;
4680eaca9bbdSeschrock 			break;
4681990b4856Slling 		case 'V':
4682990b4856Slling 			cb.cb_version = strtoll(optarg, &end, 10);
4683ad135b5dSChristopher Siden 			if (*end != '\0' ||
4684ad135b5dSChristopher Siden 			    !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
4685990b4856Slling 				(void) fprintf(stderr,
4686990b4856Slling 				    gettext("invalid version '%s'\n"), optarg);
4687990b4856Slling 				usage(B_FALSE);
4688990b4856Slling 			}
4689990b4856Slling 			break;
4690478ed9adSEric Taylor 		case ':':
4691478ed9adSEric Taylor 			(void) fprintf(stderr, gettext("missing argument for "
4692478ed9adSEric Taylor 			    "'%c' option\n"), optopt);
4693478ed9adSEric Taylor 			usage(B_FALSE);
4694478ed9adSEric Taylor 			break;
4695eaca9bbdSeschrock 		case '?':
4696eaca9bbdSeschrock 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4697eaca9bbdSeschrock 			    optopt);
469899653d4eSeschrock 			usage(B_FALSE);
4699eaca9bbdSeschrock 		}
4700eaca9bbdSeschrock 	}
4701eaca9bbdSeschrock 
470206eeb2adSek110237 	cb.cb_argc = argc;
470306eeb2adSek110237 	cb.cb_argv = argv;
4704eaca9bbdSeschrock 	argc -= optind;
4705eaca9bbdSeschrock 	argv += optind;
4706eaca9bbdSeschrock 
4707351420b3Slling 	if (cb.cb_version == 0) {
4708351420b3Slling 		cb.cb_version = SPA_VERSION;
470957221772SChristopher Siden 	} else if (!upgradeall && argc == 0) {
4710351420b3Slling 		(void) fprintf(stderr, gettext("-V option is "
4711351420b3Slling 		    "incompatible with other arguments\n"));
4712351420b3Slling 		usage(B_FALSE);
4713351420b3Slling 	}
4714351420b3Slling 
4715eaca9bbdSeschrock 	if (showversions) {
471657221772SChristopher Siden 		if (upgradeall || argc != 0) {
4717eaca9bbdSeschrock 			(void) fprintf(stderr, gettext("-v option is "
4718eaca9bbdSeschrock 			    "incompatible with other arguments\n"));
471999653d4eSeschrock 			usage(B_FALSE);
4720eaca9bbdSeschrock 		}
472157221772SChristopher Siden 	} else if (upgradeall) {
4722eaca9bbdSeschrock 		if (argc != 0) {
4723351420b3Slling 			(void) fprintf(stderr, gettext("-a option should not "
4724351420b3Slling 			    "be used along with a pool name\n"));
472599653d4eSeschrock 			usage(B_FALSE);
4726eaca9bbdSeschrock 		}
4727eaca9bbdSeschrock 	}
4728eaca9bbdSeschrock 
4729ad135b5dSChristopher Siden 	(void) printf(gettext("This system supports ZFS pool feature "
4730ad135b5dSChristopher Siden 	    "flags.\n\n"));
4731eaca9bbdSeschrock 	if (showversions) {
473257221772SChristopher Siden 		int i;
473357221772SChristopher Siden 
473457221772SChristopher Siden 		(void) printf(gettext("The following features are "
473557221772SChristopher Siden 		    "supported:\n\n"));
473657221772SChristopher Siden 		(void) printf(gettext("FEAT DESCRIPTION\n"));
473757221772SChristopher Siden 		(void) printf("----------------------------------------------"
473857221772SChristopher Siden 		    "---------------\n");
473957221772SChristopher Siden 		for (i = 0; i < SPA_FEATURES; i++) {
474057221772SChristopher Siden 			zfeature_info_t *fi = &spa_feature_table[i];
474157221772SChristopher Siden 			const char *ro = fi->fi_can_readonly ?
474257221772SChristopher Siden 			    " (read-only compatible)" : "";
474357221772SChristopher Siden 
474457221772SChristopher Siden 			(void) printf("%-37s%s\n", fi->fi_uname, ro);
474557221772SChristopher Siden 			(void) printf("     %s\n", fi->fi_desc);
474657221772SChristopher Siden 		}
474757221772SChristopher Siden 		(void) printf("\n");
474857221772SChristopher Siden 
474957221772SChristopher Siden 		(void) printf(gettext("The following legacy versions are also "
4750d7d4af51Smmusante 		    "supported:\n\n"));
4751eaca9bbdSeschrock 		(void) printf(gettext("VER  DESCRIPTION\n"));
4752eaca9bbdSeschrock 		(void) printf("---  -----------------------------------------"
4753eaca9bbdSeschrock 		    "---------------\n");
475499653d4eSeschrock 		(void) printf(gettext(" 1   Initial ZFS version\n"));
475544cd46caSbillm 		(void) printf(gettext(" 2   Ditto blocks "
475644cd46caSbillm 		    "(replicated metadata)\n"));
475799653d4eSeschrock 		(void) printf(gettext(" 3   Hot spares and double parity "
475899653d4eSeschrock 		    "RAID-Z\n"));
4759d7306b64Sek110237 		(void) printf(gettext(" 4   zpool history\n"));
4760c9431fa1Sahl 		(void) printf(gettext(" 5   Compression using the gzip "
4761c9431fa1Sahl 		    "algorithm\n"));
4762990b4856Slling 		(void) printf(gettext(" 6   bootfs pool property\n"));
47638654d025Sperrin 		(void) printf(gettext(" 7   Separate intent log devices\n"));
4764ecd6cf80Smarks 		(void) printf(gettext(" 8   Delegated administration\n"));
4765a9799022Sck153898 		(void) printf(gettext(" 9   refquota and refreservation "
4766a9799022Sck153898 		    "properties\n"));
4767fa94a07fSbrendan 		(void) printf(gettext(" 10  Cache devices\n"));
4768088f3894Sahrens 		(void) printf(gettext(" 11  Improved scrub performance\n"));
4769bb0ade09Sahrens 		(void) printf(gettext(" 12  Snapshot properties\n"));
477074e7dc98SMatthew Ahrens 		(void) printf(gettext(" 13  snapused property\n"));
477114843421SMatthew Ahrens 		(void) printf(gettext(" 14  passthrough-x aclinherit\n"));
477214843421SMatthew Ahrens 		(void) printf(gettext(" 15  user/group space accounting\n"));
4773478ed9adSEric Taylor 		(void) printf(gettext(" 16  stmf property support\n"));
47747aeab329SAdam Leventhal 		(void) printf(gettext(" 17  Triple-parity RAID-Z\n"));
4775b24ab676SJeff Bonwick 		(void) printf(gettext(" 18  Snapshot user holds\n"));
477688ecc943SGeorge Wilson 		(void) printf(gettext(" 19  Log device removal\n"));
4777b24ab676SJeff Bonwick 		(void) printf(gettext(" 20  Compression using zle "
4778b24ab676SJeff Bonwick 		    "(zero-length encoding)\n"));
4779b24ab676SJeff Bonwick 		(void) printf(gettext(" 21  Deduplication\n"));
478092241e0bSTom Erickson 		(void) printf(gettext(" 22  Received properties\n"));
47816e1f5caaSNeil Perrin 		(void) printf(gettext(" 23  Slim ZIL\n"));
47820a586ceaSMark Shellenbaum 		(void) printf(gettext(" 24  System attributes\n"));
47833f9d6ad7SLin Ling 		(void) printf(gettext(" 25  Improved scrub stats\n"));
4784cde58dbcSMatthew Ahrens 		(void) printf(gettext(" 26  Improved snapshot deletion "
4785cde58dbcSMatthew Ahrens 		    "performance\n"));
47866e0cbcaaSMatthew Ahrens 		(void) printf(gettext(" 27  Improved snapshot creation "
47876e0cbcaaSMatthew Ahrens 		    "performance\n"));
4788cb04b873SMark J Musante 		(void) printf(gettext(" 28  Multiple vdev replacements\n"));
4789b24ab676SJeff Bonwick 		(void) printf(gettext("\nFor more information on a particular "
47909a8685acSstephanie scheffler 		    "version, including supported releases,\n"));
47919a8685acSstephanie scheffler 		(void) printf(gettext("see the ZFS Administration Guide.\n\n"));
479257221772SChristopher Siden 	} else if (argc == 0 && upgradeall) {
4793eaca9bbdSeschrock 		cb.cb_first = B_TRUE;
479499653d4eSeschrock 		ret = zpool_iter(g_zfs, upgrade_cb, &cb);
479557221772SChristopher Siden 		if (ret == 0 && cb.cb_first) {
479657221772SChristopher Siden 			if (cb.cb_version == SPA_VERSION) {
479757221772SChristopher Siden 				(void) printf(gettext("All pools are already "
479857221772SChristopher Siden 				    "formatted using feature flags.\n\n"));
479957221772SChristopher Siden 				(void) printf(gettext("Every feature flags "
480057221772SChristopher Siden 				    "pool already has all supported features "
480157221772SChristopher Siden 				    "enabled.\n"));
480257221772SChristopher Siden 			} else {
480357221772SChristopher Siden 				(void) printf(gettext("All pools are already "
480457221772SChristopher Siden 				    "formatted with version %llu or higher.\n"),
480557221772SChristopher Siden 				    cb.cb_version);
4806eaca9bbdSeschrock 			}
4807eaca9bbdSeschrock 		}
480857221772SChristopher Siden 	} else if (argc == 0) {
480957221772SChristopher Siden 		cb.cb_first = B_TRUE;
481057221772SChristopher Siden 		ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
481157221772SChristopher Siden 		assert(ret == 0);
481257221772SChristopher Siden 
481357221772SChristopher Siden 		if (cb.cb_first) {
481457221772SChristopher Siden 			(void) printf(gettext("All pools are formatted "
481557221772SChristopher Siden 			    "using feature flags.\n\n"));
481657221772SChristopher Siden 		} else {
481757221772SChristopher Siden 			(void) printf(gettext("\nUse 'zpool upgrade -v' "
481857221772SChristopher Siden 			    "for a list of available legacy versions.\n"));
481957221772SChristopher Siden 		}
4820eaca9bbdSeschrock 
482157221772SChristopher Siden 		cb.cb_first = B_TRUE;
482257221772SChristopher Siden 		ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
482357221772SChristopher Siden 		assert(ret == 0);
482457221772SChristopher Siden 
482557221772SChristopher Siden 		if (cb.cb_first) {
482657221772SChristopher Siden 			(void) printf(gettext("Every feature flags pool has "
482757221772SChristopher Siden 			    "all supported features enabled.\n"));
482857221772SChristopher Siden 		} else {
482957221772SChristopher Siden 			(void) printf(gettext("\n"));
4830eaca9bbdSeschrock 		}
4831eaca9bbdSeschrock 	} else {
4832b1b8ab34Slling 		ret = for_each_pool(argc, argv, B_FALSE, NULL,
4833b1b8ab34Slling 		    upgrade_one, &cb);
483406eeb2adSek110237 	}
483506eeb2adSek110237 
483606eeb2adSek110237 	return (ret);
483706eeb2adSek110237 }
483806eeb2adSek110237 
4839ecd6cf80Smarks typedef struct hist_cbdata {
4840ecd6cf80Smarks 	boolean_t first;
48414445fffbSMatthew Ahrens 	boolean_t longfmt;
48424445fffbSMatthew Ahrens 	boolean_t internal;
4843ecd6cf80Smarks } hist_cbdata_t;
4844ecd6cf80Smarks 
484506eeb2adSek110237 /*
484606eeb2adSek110237  * Print out the command history for a specific pool.
484706eeb2adSek110237  */
484806eeb2adSek110237 static int
484906eeb2adSek110237 get_history_one(zpool_handle_t *zhp, void *data)
485006eeb2adSek110237 {
485106eeb2adSek110237 	nvlist_t *nvhis;
485206eeb2adSek110237 	nvlist_t **records;
485306eeb2adSek110237 	uint_t numrecords;
485406eeb2adSek110237 	int ret, i;
4855ecd6cf80Smarks 	hist_cbdata_t *cb = (hist_cbdata_t *)data;
485606eeb2adSek110237 
4857ecd6cf80Smarks 	cb->first = B_FALSE;
485806eeb2adSek110237 
485906eeb2adSek110237 	(void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
486006eeb2adSek110237 
486106eeb2adSek110237 	if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
486206eeb2adSek110237 		return (ret);
486306eeb2adSek110237 
486406eeb2adSek110237 	verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
486506eeb2adSek110237 	    &records, &numrecords) == 0);
486606eeb2adSek110237 	for (i = 0; i < numrecords; i++) {
48674445fffbSMatthew Ahrens 		nvlist_t *rec = records[i];
48684445fffbSMatthew Ahrens 		char tbuf[30] = "";
4869ecd6cf80Smarks 
48704445fffbSMatthew Ahrens 		if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
48714445fffbSMatthew Ahrens 			time_t tsec;
48724445fffbSMatthew Ahrens 			struct tm t;
4873ecd6cf80Smarks 
48744445fffbSMatthew Ahrens 			tsec = fnvlist_lookup_uint64(records[i],
48754445fffbSMatthew Ahrens 			    ZPOOL_HIST_TIME);
487606eeb2adSek110237 			(void) localtime_r(&tsec, &t);
487706eeb2adSek110237 			(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
48784445fffbSMatthew Ahrens 		}
48794445fffbSMatthew Ahrens 
48804445fffbSMatthew Ahrens 		if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
48814445fffbSMatthew Ahrens 			(void) printf("%s %s", tbuf,
48824445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
48834445fffbSMatthew Ahrens 		} else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
48844445fffbSMatthew Ahrens 			int ievent =
48854445fffbSMatthew Ahrens 			    fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
48864445fffbSMatthew Ahrens 			if (!cb->internal)
48874445fffbSMatthew Ahrens 				continue;
48884445fffbSMatthew Ahrens 			if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
48894445fffbSMatthew Ahrens 				(void) printf("%s unrecognized record:\n",
48904445fffbSMatthew Ahrens 				    tbuf);
48914445fffbSMatthew Ahrens 				dump_nvlist(rec, 4);
48924445fffbSMatthew Ahrens 				continue;
48934445fffbSMatthew Ahrens 			}
48944445fffbSMatthew Ahrens 			(void) printf("%s [internal %s txg:%lld] %s", tbuf,
48954445fffbSMatthew Ahrens 			    zfs_history_event_names[ievent],
48964445fffbSMatthew Ahrens 			    fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
48974445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
48984445fffbSMatthew Ahrens 		} else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
48994445fffbSMatthew Ahrens 			if (!cb->internal)
49004445fffbSMatthew Ahrens 				continue;
49014445fffbSMatthew Ahrens 			(void) printf("%s [txg:%lld] %s", tbuf,
49024445fffbSMatthew Ahrens 			    fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
49034445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
49044445fffbSMatthew Ahrens 			if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
49054445fffbSMatthew Ahrens 				(void) printf(" %s (%llu)",
49064445fffbSMatthew Ahrens 				    fnvlist_lookup_string(rec,
49074445fffbSMatthew Ahrens 				    ZPOOL_HIST_DSNAME),
49084445fffbSMatthew Ahrens 				    fnvlist_lookup_uint64(rec,
49094445fffbSMatthew Ahrens 				    ZPOOL_HIST_DSID));
49104445fffbSMatthew Ahrens 			}
49114445fffbSMatthew Ahrens 			(void) printf(" %s", fnvlist_lookup_string(rec,
49124445fffbSMatthew Ahrens 			    ZPOOL_HIST_INT_STR));
49134445fffbSMatthew Ahrens 		} else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
49144445fffbSMatthew Ahrens 			if (!cb->internal)
49154445fffbSMatthew Ahrens 				continue;
49164445fffbSMatthew Ahrens 			(void) printf("%s ioctl %s\n", tbuf,
49174445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
49184445fffbSMatthew Ahrens 			if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
49194445fffbSMatthew Ahrens 				(void) printf("    input:\n");
49204445fffbSMatthew Ahrens 				dump_nvlist(fnvlist_lookup_nvlist(rec,
49214445fffbSMatthew Ahrens 				    ZPOOL_HIST_INPUT_NVL), 8);
49224445fffbSMatthew Ahrens 			}
49234445fffbSMatthew Ahrens 			if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
49244445fffbSMatthew Ahrens 				(void) printf("    output:\n");
49254445fffbSMatthew Ahrens 				dump_nvlist(fnvlist_lookup_nvlist(rec,
49264445fffbSMatthew Ahrens 				    ZPOOL_HIST_OUTPUT_NVL), 8);
49274445fffbSMatthew Ahrens 			}
49284445fffbSMatthew Ahrens 		} else {
49294445fffbSMatthew Ahrens 			if (!cb->internal)
49304445fffbSMatthew Ahrens 				continue;
49314445fffbSMatthew Ahrens 			(void) printf("%s unrecognized record:\n", tbuf);
49324445fffbSMatthew Ahrens 			dump_nvlist(rec, 4);
49334445fffbSMatthew Ahrens 		}
4934ecd6cf80Smarks 
4935ecd6cf80Smarks 		if (!cb->longfmt) {
4936ecd6cf80Smarks 			(void) printf("\n");
4937ecd6cf80Smarks 			continue;
493806eeb2adSek110237 		}
4939ecd6cf80Smarks 		(void) printf(" [");
49404445fffbSMatthew Ahrens 		if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
49414445fffbSMatthew Ahrens 			uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
49424445fffbSMatthew Ahrens 			struct passwd *pwd = getpwuid(who);
49434445fffbSMatthew Ahrens 			(void) printf("user %d ", (int)who);
49444445fffbSMatthew Ahrens 			if (pwd != NULL)
49454445fffbSMatthew Ahrens 				(void) printf("(%s) ", pwd->pw_name);
4946ecd6cf80Smarks 		}
49474445fffbSMatthew Ahrens 		if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
49484445fffbSMatthew Ahrens 			(void) printf("on %s",
49494445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
4950ecd6cf80Smarks 		}
49514445fffbSMatthew Ahrens 		if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
49524445fffbSMatthew Ahrens 			(void) printf(":%s",
49534445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
4954ecd6cf80Smarks 		}
4955ecd6cf80Smarks 		(void) printf("]");
4956ecd6cf80Smarks 		(void) printf("\n");
495706eeb2adSek110237 	}
495806eeb2adSek110237 	(void) printf("\n");
495906eeb2adSek110237 	nvlist_free(nvhis);
496006eeb2adSek110237 
496106eeb2adSek110237 	return (ret);
496206eeb2adSek110237 }
496306eeb2adSek110237 
496406eeb2adSek110237 /*
496506eeb2adSek110237  * zpool history <pool>
496606eeb2adSek110237  *
496706eeb2adSek110237  * Displays the history of commands that modified pools.
496806eeb2adSek110237  */
496906eeb2adSek110237 int
497006eeb2adSek110237 zpool_do_history(int argc, char **argv)
497106eeb2adSek110237 {
4972ecd6cf80Smarks 	hist_cbdata_t cbdata = { 0 };
497306eeb2adSek110237 	int ret;
4974ecd6cf80Smarks 	int c;
497506eeb2adSek110237 
4976ecd6cf80Smarks 	cbdata.first = B_TRUE;
4977ecd6cf80Smarks 	/* check options */
4978ecd6cf80Smarks 	while ((c = getopt(argc, argv, "li")) != -1) {
4979ecd6cf80Smarks 		switch (c) {
4980ecd6cf80Smarks 		case 'l':
49814445fffbSMatthew Ahrens 			cbdata.longfmt = B_TRUE;
4982ecd6cf80Smarks 			break;
4983ecd6cf80Smarks 		case 'i':
49844445fffbSMatthew Ahrens 			cbdata.internal = B_TRUE;
4985ecd6cf80Smarks 			break;
4986ecd6cf80Smarks 		case '?':
4987ecd6cf80Smarks 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4988ecd6cf80Smarks 			    optopt);
4989ecd6cf80Smarks 			usage(B_FALSE);
4990ecd6cf80Smarks 		}
4991ecd6cf80Smarks 	}
499206eeb2adSek110237 	argc -= optind;
499306eeb2adSek110237 	argv += optind;
499406eeb2adSek110237 
4995b1b8ab34Slling 	ret = for_each_pool(argc, argv, B_FALSE,  NULL, get_history_one,
4996ecd6cf80Smarks 	    &cbdata);
499706eeb2adSek110237 
4998ecd6cf80Smarks 	if (argc == 0 && cbdata.first == B_TRUE) {
499906eeb2adSek110237 		(void) printf(gettext("no pools available\n"));
500006eeb2adSek110237 		return (0);
5001eaca9bbdSeschrock 	}
5002eaca9bbdSeschrock 
5003eaca9bbdSeschrock 	return (ret);
5004eaca9bbdSeschrock }
5005eaca9bbdSeschrock 
5006b1b8ab34Slling static int
5007b1b8ab34Slling get_callback(zpool_handle_t *zhp, void *data)
5008b1b8ab34Slling {
5009990b4856Slling 	zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5010b1b8ab34Slling 	char value[MAXNAMELEN];
5011990b4856Slling 	zprop_source_t srctype;
5012990b4856Slling 	zprop_list_t *pl;
5013b1b8ab34Slling 
5014b1b8ab34Slling 	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5015b1b8ab34Slling 
5016b1b8ab34Slling 		/*
5017990b4856Slling 		 * Skip the special fake placeholder. This will also skip
5018990b4856Slling 		 * over the name property when 'all' is specified.
5019b1b8ab34Slling 		 */
5020990b4856Slling 		if (pl->pl_prop == ZPOOL_PROP_NAME &&
5021b1b8ab34Slling 		    pl == cbp->cb_proplist)
5022b1b8ab34Slling 			continue;
5023b1b8ab34Slling 
5024ad135b5dSChristopher Siden 		if (pl->pl_prop == ZPROP_INVAL &&
5025ad135b5dSChristopher Siden 		    (zpool_prop_feature(pl->pl_user_prop) ||
5026ad135b5dSChristopher Siden 		    zpool_prop_unsupported(pl->pl_user_prop))) {
5027ad135b5dSChristopher Siden 			srctype = ZPROP_SRC_LOCAL;
5028ad135b5dSChristopher Siden 
5029ad135b5dSChristopher Siden 			if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5030ad135b5dSChristopher Siden 			    value, sizeof (value)) == 0) {
5031ad135b5dSChristopher Siden 				zprop_print_one_property(zpool_get_name(zhp),
5032ad135b5dSChristopher Siden 				    cbp, pl->pl_user_prop, value, srctype,
5033ad135b5dSChristopher Siden 				    NULL, NULL);
5034ad135b5dSChristopher Siden 			}
5035ad135b5dSChristopher Siden 		} else {
5036ad135b5dSChristopher Siden 			if (zpool_get_prop(zhp, pl->pl_prop, value,
5037c58b3526SAdam Stevko 			    sizeof (value), &srctype, cbp->cb_literal) != 0)
5038b1b8ab34Slling 				continue;
5039b1b8ab34Slling 
5040990b4856Slling 			zprop_print_one_property(zpool_get_name(zhp), cbp,
5041ad135b5dSChristopher Siden 			    zpool_prop_to_name(pl->pl_prop), value, srctype,
5042ad135b5dSChristopher Siden 			    NULL, NULL);
5043ad135b5dSChristopher Siden 		}
5044b1b8ab34Slling 	}
5045b1b8ab34Slling 	return (0);
5046b1b8ab34Slling }
5047b1b8ab34Slling 
5048c58b3526SAdam Stevko /*
5049c58b3526SAdam Stevko  * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
5050c58b3526SAdam Stevko  *
5051c58b3526SAdam Stevko  *	-H	Scripted mode.  Don't display headers, and separate properties
5052c58b3526SAdam Stevko  *		by a single tab.
5053c58b3526SAdam Stevko  *	-o	List of columns to display.  Defaults to
5054c58b3526SAdam Stevko  *		"name,property,value,source".
5055c58b3526SAdam Stevko  * 	-p	Diplay values in parsable (exact) format.
5056c58b3526SAdam Stevko  *
5057c58b3526SAdam Stevko  * Get properties of pools in the system. Output space statistics
5058c58b3526SAdam Stevko  * for each one as well as other attributes.
5059c58b3526SAdam Stevko  */
5060b1b8ab34Slling int
5061b1b8ab34Slling zpool_do_get(int argc, char **argv)
5062b1b8ab34Slling {
5063990b4856Slling 	zprop_get_cbdata_t cb = { 0 };
5064990b4856Slling 	zprop_list_t fake_name = { 0 };
5065b1b8ab34Slling 	int ret;
5066c58b3526SAdam Stevko 	int c, i;
5067c58b3526SAdam Stevko 	char *value;
5068b1b8ab34Slling 
5069b1b8ab34Slling 	cb.cb_first = B_TRUE;
5070c58b3526SAdam Stevko 
5071c58b3526SAdam Stevko 	/*
5072c58b3526SAdam Stevko 	 * Set up default columns and sources.
5073c58b3526SAdam Stevko 	 */
5074990b4856Slling 	cb.cb_sources = ZPROP_SRC_ALL;
5075b1b8ab34Slling 	cb.cb_columns[0] = GET_COL_NAME;
5076b1b8ab34Slling 	cb.cb_columns[1] = GET_COL_PROPERTY;
5077b1b8ab34Slling 	cb.cb_columns[2] = GET_COL_VALUE;
5078b1b8ab34Slling 	cb.cb_columns[3] = GET_COL_SOURCE;
5079990b4856Slling 	cb.cb_type = ZFS_TYPE_POOL;
5080b1b8ab34Slling 
5081c58b3526SAdam Stevko 	/* check options */
5082c58b3526SAdam Stevko 	while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
5083c58b3526SAdam Stevko 		switch (c) {
5084c58b3526SAdam Stevko 		case 'p':
5085c58b3526SAdam Stevko 			cb.cb_literal = B_TRUE;
5086c58b3526SAdam Stevko 			break;
5087c58b3526SAdam Stevko 		case 'H':
5088c58b3526SAdam Stevko 			cb.cb_scripted = B_TRUE;
5089c58b3526SAdam Stevko 			break;
5090c58b3526SAdam Stevko 		case 'o':
5091c58b3526SAdam Stevko 			bzero(&cb.cb_columns, sizeof (cb.cb_columns));
5092c58b3526SAdam Stevko 			i = 0;
5093c58b3526SAdam Stevko 			while (*optarg != '\0') {
5094c58b3526SAdam Stevko 				static char *col_subopts[] =
5095c58b3526SAdam Stevko 				{ "name", "property", "value", "source",
5096c58b3526SAdam Stevko 				"all", NULL };
5097c58b3526SAdam Stevko 
5098c58b3526SAdam Stevko 				if (i == ZFS_GET_NCOLS) {
5099c58b3526SAdam Stevko 					(void) fprintf(stderr, gettext("too "
5100c58b3526SAdam Stevko 					"many fields given to -o "
5101c58b3526SAdam Stevko 					"option\n"));
5102c58b3526SAdam Stevko 					usage(B_FALSE);
5103c58b3526SAdam Stevko 				}
5104c58b3526SAdam Stevko 
5105c58b3526SAdam Stevko 				switch (getsubopt(&optarg, col_subopts,
5106c58b3526SAdam Stevko 				    &value)) {
5107c58b3526SAdam Stevko 				case 0:
5108c58b3526SAdam Stevko 					cb.cb_columns[i++] = GET_COL_NAME;
5109c58b3526SAdam Stevko 					break;
5110c58b3526SAdam Stevko 				case 1:
5111c58b3526SAdam Stevko 					cb.cb_columns[i++] = GET_COL_PROPERTY;
5112c58b3526SAdam Stevko 					break;
5113c58b3526SAdam Stevko 				case 2:
5114c58b3526SAdam Stevko 					cb.cb_columns[i++] = GET_COL_VALUE;
5115c58b3526SAdam Stevko 					break;
5116c58b3526SAdam Stevko 				case 3:
5117c58b3526SAdam Stevko 					cb.cb_columns[i++] = GET_COL_SOURCE;
5118c58b3526SAdam Stevko 					break;
5119c58b3526SAdam Stevko 				case 4:
5120c58b3526SAdam Stevko 					if (i > 0) {
5121c58b3526SAdam Stevko 						(void) fprintf(stderr,
5122c58b3526SAdam Stevko 						    gettext("\"all\" conflicts "
5123c58b3526SAdam Stevko 						    "with specific fields "
5124c58b3526SAdam Stevko 						    "given to -o option\n"));
5125c58b3526SAdam Stevko 						usage(B_FALSE);
5126c58b3526SAdam Stevko 					}
5127c58b3526SAdam Stevko 					cb.cb_columns[0] = GET_COL_NAME;
5128c58b3526SAdam Stevko 					cb.cb_columns[1] = GET_COL_PROPERTY;
5129c58b3526SAdam Stevko 					cb.cb_columns[2] = GET_COL_VALUE;
5130c58b3526SAdam Stevko 					cb.cb_columns[3] = GET_COL_SOURCE;
5131c58b3526SAdam Stevko 					i = ZFS_GET_NCOLS;
5132c58b3526SAdam Stevko 					break;
5133c58b3526SAdam Stevko 				default:
5134c58b3526SAdam Stevko 					(void) fprintf(stderr,
5135c58b3526SAdam Stevko 					    gettext("invalid column name "
5136c58b3526SAdam Stevko 					    "'%s'\n"), value);
5137c58b3526SAdam Stevko 					usage(B_FALSE);
5138c58b3526SAdam Stevko 				}
5139c58b3526SAdam Stevko 			}
5140c58b3526SAdam Stevko 			break;
5141c58b3526SAdam Stevko 		case '?':
5142c58b3526SAdam Stevko 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
5143c58b3526SAdam Stevko 			    optopt);
5144c58b3526SAdam Stevko 			usage(B_FALSE);
5145c58b3526SAdam Stevko 		}
5146c58b3526SAdam Stevko 	}
5147c58b3526SAdam Stevko 
5148c58b3526SAdam Stevko 	argc -= optind;
5149c58b3526SAdam Stevko 	argv += optind;
5150c58b3526SAdam Stevko 
5151c58b3526SAdam Stevko 	if (argc < 1) {
5152c58b3526SAdam Stevko 		(void) fprintf(stderr, gettext("missing property "
5153c58b3526SAdam Stevko 		    "argument\n"));
5154c58b3526SAdam Stevko 		usage(B_FALSE);
5155c58b3526SAdam Stevko 	}
5156c58b3526SAdam Stevko 
5157c58b3526SAdam Stevko 	if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
5158990b4856Slling 	    ZFS_TYPE_POOL) != 0)
5159b1b8ab34Slling 		usage(B_FALSE);
5160b1b8ab34Slling 
5161c58b3526SAdam Stevko 	argc--;
5162c58b3526SAdam Stevko 	argv++;
5163c58b3526SAdam Stevko 
5164b1b8ab34Slling 	if (cb.cb_proplist != NULL) {
5165990b4856Slling 		fake_name.pl_prop = ZPOOL_PROP_NAME;
5166b1b8ab34Slling 		fake_name.pl_width = strlen(gettext("NAME"));
5167b1b8ab34Slling 		fake_name.pl_next = cb.cb_proplist;
5168b1b8ab34Slling 		cb.cb_proplist = &fake_name;
5169b1b8ab34Slling 	}
5170b1b8ab34Slling 
5171c58b3526SAdam Stevko 	ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
5172b1b8ab34Slling 	    get_callback, &cb);
5173b1b8ab34Slling 
5174b1b8ab34Slling 	if (cb.cb_proplist == &fake_name)
5175990b4856Slling 		zprop_free_list(fake_name.pl_next);
5176b1b8ab34Slling 	else
5177990b4856Slling 		zprop_free_list(cb.cb_proplist);
5178b1b8ab34Slling 
5179b1b8ab34Slling 	return (ret);
5180b1b8ab34Slling }
5181b1b8ab34Slling 
5182b1b8ab34Slling typedef struct set_cbdata {
5183b1b8ab34Slling 	char *cb_propname;
5184b1b8ab34Slling 	char *cb_value;
5185b1b8ab34Slling 	boolean_t cb_any_successful;
5186b1b8ab34Slling } set_cbdata_t;
5187b1b8ab34Slling 
5188b1b8ab34Slling int
5189b1b8ab34Slling set_callback(zpool_handle_t *zhp, void *data)
5190b1b8ab34Slling {
5191b1b8ab34Slling 	int error;
5192b1b8ab34Slling 	set_cbdata_t *cb = (set_cbdata_t *)data;
5193b1b8ab34Slling 
5194b1b8ab34Slling 	error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5195b1b8ab34Slling 
5196b1b8ab34Slling 	if (!error)
5197b1b8ab34Slling 		cb->cb_any_successful = B_TRUE;
5198b1b8ab34Slling 
5199b1b8ab34Slling 	return (error);
5200b1b8ab34Slling }
5201b1b8ab34Slling 
5202b1b8ab34Slling int
5203b1b8ab34Slling zpool_do_set(int argc, char **argv)
5204b1b8ab34Slling {
5205b1b8ab34Slling 	set_cbdata_t cb = { 0 };
5206b1b8ab34Slling 	int error;
5207b1b8ab34Slling 
5208b1b8ab34Slling 	if (argc > 1 && argv[1][0] == '-') {
5209b1b8ab34Slling 		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
5210b1b8ab34Slling 		    argv[1][1]);
5211b1b8ab34Slling 		usage(B_FALSE);
5212b1b8ab34Slling 	}
5213b1b8ab34Slling 
5214b1b8ab34Slling 	if (argc < 2) {
5215b1b8ab34Slling 		(void) fprintf(stderr, gettext("missing property=value "
5216b1b8ab34Slling 		    "argument\n"));
5217b1b8ab34Slling 		usage(B_FALSE);
5218b1b8ab34Slling 	}
5219b1b8ab34Slling 
5220b1b8ab34Slling 	if (argc < 3) {
5221b1b8ab34Slling 		(void) fprintf(stderr, gettext("missing pool name\n"));
5222b1b8ab34Slling 		usage(B_FALSE);
5223b1b8ab34Slling 	}
5224b1b8ab34Slling 
5225b1b8ab34Slling 	if (argc > 3) {
5226b1b8ab34Slling 		(void) fprintf(stderr, gettext("too many pool names\n"));
5227b1b8ab34Slling 		usage(B_FALSE);
5228b1b8ab34Slling 	}
5229b1b8ab34Slling 
5230b1b8ab34Slling 	cb.cb_propname = argv[1];
5231b1b8ab34Slling 	cb.cb_value = strchr(cb.cb_propname, '=');
5232b1b8ab34Slling 	if (cb.cb_value == NULL) {
5233b1b8ab34Slling 		(void) fprintf(stderr, gettext("missing value in "
5234b1b8ab34Slling 		    "property=value argument\n"));
5235b1b8ab34Slling 		usage(B_FALSE);
5236b1b8ab34Slling 	}
5237b1b8ab34Slling 
5238b1b8ab34Slling 	*(cb.cb_value) = '\0';
5239b1b8ab34Slling 	cb.cb_value++;
5240b1b8ab34Slling 
5241b1b8ab34Slling 	error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5242b1b8ab34Slling 	    set_callback, &cb);
5243b1b8ab34Slling 
5244b1b8ab34Slling 	return (error);
5245b1b8ab34Slling }
5246b1b8ab34Slling 
5247b1b8ab34Slling static int
5248b1b8ab34Slling find_command_idx(char *command, int *idx)
5249b1b8ab34Slling {
5250b1b8ab34Slling 	int i;
5251b1b8ab34Slling 
5252b1b8ab34Slling 	for (i = 0; i < NCOMMAND; i++) {
5253b1b8ab34Slling 		if (command_table[i].name == NULL)
5254b1b8ab34Slling 			continue;
5255b1b8ab34Slling 
5256b1b8ab34Slling 		if (strcmp(command, command_table[i].name) == 0) {
5257b1b8ab34Slling 			*idx = i;
5258b1b8ab34Slling 			return (0);
5259b1b8ab34Slling 		}
5260b1b8ab34Slling 	}
5261b1b8ab34Slling 	return (1);
5262b1b8ab34Slling }
5263b1b8ab34Slling 
5264fa9e4066Sahrens int
5265fa9e4066Sahrens main(int argc, char **argv)
5266fa9e4066Sahrens {
5267fa9e4066Sahrens 	int ret;
5268fa9e4066Sahrens 	int i;
5269fa9e4066Sahrens 	char *cmdname;
5270fa9e4066Sahrens 
5271fa9e4066Sahrens 	(void) setlocale(LC_ALL, "");
5272fa9e4066Sahrens 	(void) textdomain(TEXT_DOMAIN);
5273fa9e4066Sahrens 
527499653d4eSeschrock 	if ((g_zfs = libzfs_init()) == NULL) {
527599653d4eSeschrock 		(void) fprintf(stderr, gettext("internal error: failed to "
5276203a47d8Snd150628 		    "initialize ZFS library\n"));
527799653d4eSeschrock 		return (1);
527899653d4eSeschrock 	}
527999653d4eSeschrock 
528099653d4eSeschrock 	libzfs_print_on_error(g_zfs, B_TRUE);
528199653d4eSeschrock 
5282fa9e4066Sahrens 	opterr = 0;
5283fa9e4066Sahrens 
5284fa9e4066Sahrens 	/*
5285fa9e4066Sahrens 	 * Make sure the user has specified some command.
5286fa9e4066Sahrens 	 */
5287fa9e4066Sahrens 	if (argc < 2) {
5288fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing command\n"));
528999653d4eSeschrock 		usage(B_FALSE);
5290fa9e4066Sahrens 	}
5291fa9e4066Sahrens 
5292fa9e4066Sahrens 	cmdname = argv[1];
5293fa9e4066Sahrens 
5294fa9e4066Sahrens 	/*
5295fa9e4066Sahrens 	 * Special case '-?'
5296fa9e4066Sahrens 	 */
5297fa9e4066Sahrens 	if (strcmp(cmdname, "-?") == 0)
529899653d4eSeschrock 		usage(B_TRUE);
5299fa9e4066Sahrens 
53004445fffbSMatthew Ahrens 	zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
53012a6b87f0Sek110237 
5302fa9e4066Sahrens 	/*
5303fa9e4066Sahrens 	 * Run the appropriate command.
5304fa9e4066Sahrens 	 */
5305b1b8ab34Slling 	if (find_command_idx(cmdname, &i) == 0) {
5306fa9e4066Sahrens 		current_command = &command_table[i];
5307fa9e4066Sahrens 		ret = command_table[i].func(argc - 1, argv + 1);
530891ebeef5Sahrens 	} else if (strchr(cmdname, '=')) {
530991ebeef5Sahrens 		verify(find_command_idx("set", &i) == 0);
531091ebeef5Sahrens 		current_command = &command_table[i];
531191ebeef5Sahrens 		ret = command_table[i].func(argc, argv);
531291ebeef5Sahrens 	} else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5313fa9e4066Sahrens 		/*
531491ebeef5Sahrens 		 * 'freeze' is a vile debugging abomination, so we treat
531591ebeef5Sahrens 		 * it as such.
5316fa9e4066Sahrens 		 */
5317ea8dc4b6Seschrock 		char buf[16384];
5318ea8dc4b6Seschrock 		int fd = open(ZFS_DEV, O_RDWR);
5319fa9e4066Sahrens 		(void) strcpy((void *)buf, argv[2]);
5320fa9e4066Sahrens 		return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
532191ebeef5Sahrens 	} else {
5322fa9e4066Sahrens 		(void) fprintf(stderr, gettext("unrecognized "
5323fa9e4066Sahrens 		    "command '%s'\n"), cmdname);
532499653d4eSeschrock 		usage(B_FALSE);
5325fa9e4066Sahrens 	}
5326fa9e4066Sahrens 
53274445fffbSMatthew Ahrens 	if (ret == 0 && log_history)
53284445fffbSMatthew Ahrens 		(void) zpool_log_history(g_zfs, history_str);
53294445fffbSMatthew Ahrens 
533099653d4eSeschrock 	libzfs_fini(g_zfs);
533199653d4eSeschrock 
5332fa9e4066Sahrens 	/*
5333fa9e4066Sahrens 	 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5334fa9e4066Sahrens 	 * for the purposes of running ::findleaks.
5335fa9e4066Sahrens 	 */
5336fa9e4066Sahrens 	if (getenv("ZFS_ABORT") != NULL) {
5337fa9e4066Sahrens 		(void) printf("dumping core by request\n");
5338fa9e4066Sahrens 		abort();
5339fa9e4066Sahrens 	}
5340fa9e4066Sahrens 
5341fa9e4066Sahrens 	return (ret);
5342fa9e4066Sahrens }
5343