xref: /illumos-gate/usr/src/cmd/zpool/zpool_main.c (revision d2aa06e8e95fc3abd82008cdd0959cbbdb2d50fb)
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.
245cabbc6bSPrashanth Sreenivasa  * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
25e1d5e507SFrederik Wessels  * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
269edf9ebdSPrasad Joshi  * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
27b327cd3fSIgor Kozhukhov  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
286401734dSWill Andrews  * Copyright 2016 Nexenta Systems, Inc.
291702cce7SAlek Pinchuk  * Copyright (c) 2017 Datto Inc.
30fa9e4066Sahrens  */
31fa9e4066Sahrens 
32fa9e4066Sahrens #include <assert.h>
33fa9e4066Sahrens #include <ctype.h>
34fa9e4066Sahrens #include <dirent.h>
35fa9e4066Sahrens #include <errno.h>
36fa9e4066Sahrens #include <fcntl.h>
37fa9e4066Sahrens #include <libgen.h>
38fa9e4066Sahrens #include <libintl.h>
39fa9e4066Sahrens #include <libuutil.h>
40fa9e4066Sahrens #include <locale.h>
41fa9e4066Sahrens #include <stdio.h>
42fa9e4066Sahrens #include <stdlib.h>
43fa9e4066Sahrens #include <string.h>
44fa9e4066Sahrens #include <strings.h>
45fa9e4066Sahrens #include <unistd.h>
46fa9e4066Sahrens #include <priv.h>
47ecd6cf80Smarks #include <pwd.h>
48ecd6cf80Smarks #include <zone.h>
494263d13fSGeorge Wilson #include <zfs_prop.h>
50b1b8ab34Slling #include <sys/fs/zfs.h>
51fa9e4066Sahrens #include <sys/stat.h>
52fa9e4066Sahrens 
53fa9e4066Sahrens #include <libzfs.h>
54fa9e4066Sahrens 
55fa9e4066Sahrens #include "zpool_util.h"
56b7b97454Sperrin #include "zfs_comutil.h"
57ad135b5dSChristopher Siden #include "zfeature_common.h"
58fa9e4066Sahrens 
5926fd7700SKrishnendu Sadhukhan - Sun Microsystems #include "statcommon.h"
6026fd7700SKrishnendu Sadhukhan - Sun Microsystems 
61fa9e4066Sahrens static int zpool_do_create(int, char **);
62fa9e4066Sahrens static int zpool_do_destroy(int, char **);
63fa9e4066Sahrens 
64fa9e4066Sahrens static int zpool_do_add(int, char **);
6599653d4eSeschrock static int zpool_do_remove(int, char **);
666401734dSWill Andrews static int zpool_do_labelclear(int, char **);
67fa9e4066Sahrens 
68fa9e4066Sahrens static int zpool_do_list(int, char **);
69fa9e4066Sahrens static int zpool_do_iostat(int, char **);
70fa9e4066Sahrens static int zpool_do_status(int, char **);
71fa9e4066Sahrens 
72fa9e4066Sahrens static int zpool_do_online(int, char **);
73fa9e4066Sahrens static int zpool_do_offline(int, char **);
74ea8dc4b6Seschrock static int zpool_do_clear(int, char **);
754263d13fSGeorge Wilson static int zpool_do_reopen(int, char **);
76fa9e4066Sahrens 
77e9103aaeSGarrett D'Amore static int zpool_do_reguid(int, char **);
78e9103aaeSGarrett D'Amore 
79fa9e4066Sahrens static int zpool_do_attach(int, char **);
80fa9e4066Sahrens static int zpool_do_detach(int, char **);
81fa9e4066Sahrens static int zpool_do_replace(int, char **);
821195e687SMark J Musante static int zpool_do_split(int, char **);
83fa9e4066Sahrens 
84fa9e4066Sahrens static int zpool_do_scrub(int, char **);
85fa9e4066Sahrens 
86fa9e4066Sahrens static int zpool_do_import(int, char **);
87fa9e4066Sahrens static int zpool_do_export(int, char **);
88fa9e4066Sahrens 
89eaca9bbdSeschrock static int zpool_do_upgrade(int, char **);
90eaca9bbdSeschrock 
9106eeb2adSek static int zpool_do_history(int, char **);
9206eeb2adSek 
93b1b8ab34Slling static int zpool_do_get(int, char **);
94b1b8ab34Slling static int zpool_do_set(int, char **);
95b1b8ab34Slling 
96fa9e4066Sahrens /*
97fa9e4066Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
98fa9e4066Sahrens  * debugging facilities.
99fa9e4066Sahrens  */
10029ab75c9Srm 
10129ab75c9Srm #ifdef DEBUG
102fa9e4066Sahrens const char *
10399653d4eSeschrock _umem_debug_init(void)
104fa9e4066Sahrens {
105fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
106fa9e4066Sahrens }
107fa9e4066Sahrens 
108fa9e4066Sahrens const char *
109fa9e4066Sahrens _umem_logging_init(void)
110fa9e4066Sahrens {
111fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
112fa9e4066Sahrens }
11329ab75c9Srm #endif
114fa9e4066Sahrens 
11565cd9f28Seschrock typedef enum {
11665cd9f28Seschrock 	HELP_ADD,
11765cd9f28Seschrock 	HELP_ATTACH,
118ea8dc4b6Seschrock 	HELP_CLEAR,
11965cd9f28Seschrock 	HELP_CREATE,
12065cd9f28Seschrock 	HELP_DESTROY,
12165cd9f28Seschrock 	HELP_DETACH,
12265cd9f28Seschrock 	HELP_EXPORT,
12306eeb2adSek 	HELP_HISTORY,
12465cd9f28Seschrock 	HELP_IMPORT,
12565cd9f28Seschrock 	HELP_IOSTAT,
1266401734dSWill Andrews 	HELP_LABELCLEAR,
12765cd9f28Seschrock 	HELP_LIST,
12865cd9f28Seschrock 	HELP_OFFLINE,
12965cd9f28Seschrock 	HELP_ONLINE,
13065cd9f28Seschrock 	HELP_REPLACE,
13199653d4eSeschrock 	HELP_REMOVE,
13265cd9f28Seschrock 	HELP_SCRUB,
133eaca9bbdSeschrock 	HELP_STATUS,
134b1b8ab34Slling 	HELP_UPGRADE,
135b1b8ab34Slling 	HELP_GET,
1361195e687SMark J Musante 	HELP_SET,
137e9103aaeSGarrett D'Amore 	HELP_SPLIT,
1384263d13fSGeorge Wilson 	HELP_REGUID,
1394263d13fSGeorge Wilson 	HELP_REOPEN
14065cd9f28Seschrock } zpool_help_t;
14165cd9f28Seschrock 
14265cd9f28Seschrock 
143fa9e4066Sahrens typedef struct zpool_command {
144fa9e4066Sahrens 	const char	*name;
145fa9e4066Sahrens 	int		(*func)(int, char **);
14665cd9f28Seschrock 	zpool_help_t	usage;
147fa9e4066Sahrens } zpool_command_t;
148fa9e4066Sahrens 
149fa9e4066Sahrens /*
150fa9e4066Sahrens  * Master command table.  Each ZFS command has a name, associated function, and
151ea8dc4b6Seschrock  * usage message.  The usage messages need to be internationalized, so we have
152ea8dc4b6Seschrock  * to have a function to return the usage message based on a command index.
15365cd9f28Seschrock  *
15465cd9f28Seschrock  * These commands are organized according to how they are displayed in the usage
15565cd9f28Seschrock  * message.  An empty command (one with a NULL name) indicates an empty line in
15665cd9f28Seschrock  * the generic usage message.
157fa9e4066Sahrens  */
158fa9e4066Sahrens static zpool_command_t command_table[] = {
15965cd9f28Seschrock 	{ "create",	zpool_do_create,	HELP_CREATE		},
16065cd9f28Seschrock 	{ "destroy",	zpool_do_destroy,	HELP_DESTROY		},
161fa9e4066Sahrens 	{ NULL },
16265cd9f28Seschrock 	{ "add",	zpool_do_add,		HELP_ADD		},
16399653d4eSeschrock 	{ "remove",	zpool_do_remove,	HELP_REMOVE		},
164fa9e4066Sahrens 	{ NULL },
1656401734dSWill Andrews 	{ "labelclear",	zpool_do_labelclear,	HELP_LABELCLEAR		},
1666401734dSWill Andrews 	{ NULL },
16765cd9f28Seschrock 	{ "list",	zpool_do_list,		HELP_LIST		},
16865cd9f28Seschrock 	{ "iostat",	zpool_do_iostat,	HELP_IOSTAT		},
16965cd9f28Seschrock 	{ "status",	zpool_do_status,	HELP_STATUS		},
170fa9e4066Sahrens 	{ NULL },
17165cd9f28Seschrock 	{ "online",	zpool_do_online,	HELP_ONLINE		},
17265cd9f28Seschrock 	{ "offline",	zpool_do_offline,	HELP_OFFLINE		},
173ea8dc4b6Seschrock 	{ "clear",	zpool_do_clear,		HELP_CLEAR		},
1744263d13fSGeorge Wilson 	{ "reopen",	zpool_do_reopen,	HELP_REOPEN		},
175fa9e4066Sahrens 	{ NULL },
17665cd9f28Seschrock 	{ "attach",	zpool_do_attach,	HELP_ATTACH		},
17765cd9f28Seschrock 	{ "detach",	zpool_do_detach,	HELP_DETACH		},
17865cd9f28Seschrock 	{ "replace",	zpool_do_replace,	HELP_REPLACE		},
1791195e687SMark J Musante 	{ "split",	zpool_do_split,		HELP_SPLIT		},
180fa9e4066Sahrens 	{ NULL },
18165cd9f28Seschrock 	{ "scrub",	zpool_do_scrub,		HELP_SCRUB		},
182fa9e4066Sahrens 	{ NULL },
18365cd9f28Seschrock 	{ "import",	zpool_do_import,	HELP_IMPORT		},
18465cd9f28Seschrock 	{ "export",	zpool_do_export,	HELP_EXPORT		},
18506eeb2adSek 	{ "upgrade",	zpool_do_upgrade,	HELP_UPGRADE		},
186e9103aaeSGarrett D'Amore 	{ "reguid",	zpool_do_reguid,	HELP_REGUID		},
18706eeb2adSek 	{ NULL },
188b1b8ab34Slling 	{ "history",	zpool_do_history,	HELP_HISTORY		},
189b1b8ab34Slling 	{ "get",	zpool_do_get,		HELP_GET		},
190b1b8ab34Slling 	{ "set",	zpool_do_set,		HELP_SET		},
191fa9e4066Sahrens };
192fa9e4066Sahrens 
193fa9e4066Sahrens #define	NCOMMAND	(sizeof (command_table) / sizeof (command_table[0]))
194fa9e4066Sahrens 
1954445fffbSMatthew Ahrens static zpool_command_t *current_command;
1962a6b87f0Sek static char history_str[HIS_MAX_RECORD_LEN];
1974445fffbSMatthew Ahrens static boolean_t log_history = B_TRUE;
19826fd7700SKrishnendu Sadhukhan - Sun Microsystems static uint_t timestamp_fmt = NODATE;
19926fd7700SKrishnendu Sadhukhan - Sun Microsystems 
20065cd9f28Seschrock static const char *
2019a686fbcSPaul Dagnelie get_usage(zpool_help_t idx)
2029a686fbcSPaul Dagnelie {
20365cd9f28Seschrock 	switch (idx) {
20465cd9f28Seschrock 	case HELP_ADD:
20565cd9f28Seschrock 		return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
20665cd9f28Seschrock 	case HELP_ATTACH:
20765cd9f28Seschrock 		return (gettext("\tattach [-f] <pool> <device> "
208e45ce728Sahrens 		    "<new-device>\n"));
209ea8dc4b6Seschrock 	case HELP_CLEAR:
210468c413aSTim Haley 		return (gettext("\tclear [-nF] <pool> [device]\n"));
21165cd9f28Seschrock 	case HELP_CREATE:
2127855d95bSToomas Soome 		return (gettext("\tcreate [-fnd] [-B] "
2137855d95bSToomas Soome 		    "[-o property=value] ... \n"
2140a48a24eStimh 		    "\t    [-O file-system-property=value] ... \n"
215990b4856Slling 		    "\t    [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
21665cd9f28Seschrock 	case HELP_DESTROY:
21765cd9f28Seschrock 		return (gettext("\tdestroy [-f] <pool>\n"));
21865cd9f28Seschrock 	case HELP_DETACH:
21965cd9f28Seschrock 		return (gettext("\tdetach <pool> <device>\n"));
22065cd9f28Seschrock 	case HELP_EXPORT:
22165cd9f28Seschrock 		return (gettext("\texport [-f] <pool> ...\n"));
22206eeb2adSek 	case HELP_HISTORY:
223ecd6cf80Smarks 		return (gettext("\thistory [-il] [<pool>] ...\n"));
22465cd9f28Seschrock 	case HELP_IMPORT:
2254c58d714Sdarrenm 		return (gettext("\timport [-d dir] [-D]\n"
226f9af39baSGeorge Wilson 		    "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
2272f8aaab3Seschrock 		    "\timport [-o mntopts] [-o property=value] ... \n"
228f9af39baSGeorge Wilson 		    "\t    [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
229f9af39baSGeorge Wilson 		    "[-R root] [-F [-n]] -a\n"
2302f8aaab3Seschrock 		    "\timport [-o mntopts] [-o property=value] ... \n"
231f9af39baSGeorge Wilson 		    "\t    [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
232f9af39baSGeorge Wilson 		    "[-R root] [-F [-n]]\n"
233f9af39baSGeorge Wilson 		    "\t    <pool | id> [newpool]\n"));
23465cd9f28Seschrock 	case HELP_IOSTAT:
23526fd7700SKrishnendu Sadhukhan - Sun Microsystems 		return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
23665cd9f28Seschrock 		    "[count]]\n"));
2376401734dSWill Andrews 	case HELP_LABELCLEAR:
2386401734dSWill Andrews 		return (gettext("\tlabelclear [-f] <vdev>\n"));
23965cd9f28Seschrock 	case HELP_LIST:
240c58b3526SAdam Stevko 		return (gettext("\tlist [-Hp] [-o property[,...]] "
2413f9d6ad7SLin Ling 		    "[-T d|u] [pool] ... [interval [count]]\n"));
24265cd9f28Seschrock 	case HELP_OFFLINE:
243441d80aaSlling 		return (gettext("\toffline [-t] <pool> <device> ...\n"));
24465cd9f28Seschrock 	case HELP_ONLINE:
245441d80aaSlling 		return (gettext("\tonline <pool> <device> ...\n"));
24665cd9f28Seschrock 	case HELP_REPLACE:
24765cd9f28Seschrock 		return (gettext("\treplace [-f] <pool> <device> "
248e45ce728Sahrens 		    "[new-device]\n"));
24999653d4eSeschrock 	case HELP_REMOVE:
2505cabbc6bSPrashanth Sreenivasa 		return (gettext("\tremove [-nps] <pool> <device> ...\n"));
2514263d13fSGeorge Wilson 	case HELP_REOPEN:
25231d7e8faSGeorge Wilson 		return (gettext("\treopen <pool>\n"));
25365cd9f28Seschrock 	case HELP_SCRUB:
2541702cce7SAlek Pinchuk 		return (gettext("\tscrub [-s | -p] <pool> ...\n"));
25565cd9f28Seschrock 	case HELP_STATUS:
2563f9d6ad7SLin Ling 		return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
2573f9d6ad7SLin Ling 		    "[count]]\n"));
258eaca9bbdSeschrock 	case HELP_UPGRADE:
259eaca9bbdSeschrock 		return (gettext("\tupgrade\n"
260eaca9bbdSeschrock 		    "\tupgrade -v\n"
261990b4856Slling 		    "\tupgrade [-V version] <-a | pool ...>\n"));
262b1b8ab34Slling 	case HELP_GET:
263c58b3526SAdam Stevko 		return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
264c58b3526SAdam Stevko 		    "<\"all\" | property[,...]> <pool> ...\n"));
265b1b8ab34Slling 	case HELP_SET:
266b1b8ab34Slling 		return (gettext("\tset <property=value> <pool> \n"));
2671195e687SMark J Musante 	case HELP_SPLIT:
2681195e687SMark J Musante 		return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
2691195e687SMark J Musante 		    "\t    [-o property=value] <pool> <newpool> "
2701195e687SMark J Musante 		    "[<device> ...]\n"));
271e9103aaeSGarrett D'Amore 	case HELP_REGUID:
272e9103aaeSGarrett D'Amore 		return (gettext("\treguid <pool>\n"));
27365cd9f28Seschrock 	}
27465cd9f28Seschrock 
27565cd9f28Seschrock 	abort();
27665cd9f28Seschrock 	/* NOTREACHED */
27765cd9f28Seschrock }
27865cd9f28Seschrock 
279fa9e4066Sahrens 
280b1b8ab34Slling /*
281b1b8ab34Slling  * Callback routine that will print out a pool property value.
282b1b8ab34Slling  */
283990b4856Slling static int
284990b4856Slling print_prop_cb(int prop, void *cb)
285b1b8ab34Slling {
286b1b8ab34Slling 	FILE *fp = cb;
287b1b8ab34Slling 
288b24ab676SJeff Bonwick 	(void) fprintf(fp, "\t%-15s  ", zpool_prop_to_name(prop));
289b1b8ab34Slling 
290990b4856Slling 	if (zpool_prop_readonly(prop))
291990b4856Slling 		(void) fprintf(fp, "  NO   ");
292990b4856Slling 	else
293b24ab676SJeff Bonwick 		(void) fprintf(fp, " YES   ");
294990b4856Slling 
295b1b8ab34Slling 	if (zpool_prop_values(prop) == NULL)
296b1b8ab34Slling 		(void) fprintf(fp, "-\n");
297b1b8ab34Slling 	else
298b1b8ab34Slling 		(void) fprintf(fp, "%s\n", zpool_prop_values(prop));
299b1b8ab34Slling 
300990b4856Slling 	return (ZPROP_CONT);
301b1b8ab34Slling }
302b1b8ab34Slling 
303fa9e4066Sahrens /*
304fa9e4066Sahrens  * Display usage message.  If we're inside a command, display only the usage for
305fa9e4066Sahrens  * that command.  Otherwise, iterate over the entire command table and display
306fa9e4066Sahrens  * a complete usage message.
307fa9e4066Sahrens  */
308fa9e4066Sahrens void
30999653d4eSeschrock usage(boolean_t requested)
310fa9e4066Sahrens {
311fa9e4066Sahrens 	FILE *fp = requested ? stdout : stderr;
312fa9e4066Sahrens 
313fa9e4066Sahrens 	if (current_command == NULL) {
314fa9e4066Sahrens 		int i;
315fa9e4066Sahrens 
316fa9e4066Sahrens 		(void) fprintf(fp, gettext("usage: zpool command args ...\n"));
317fa9e4066Sahrens 		(void) fprintf(fp,
318fa9e4066Sahrens 		    gettext("where 'command' is one of the following:\n\n"));
319fa9e4066Sahrens 
320fa9e4066Sahrens 		for (i = 0; i < NCOMMAND; i++) {
321fa9e4066Sahrens 			if (command_table[i].name == NULL)
322fa9e4066Sahrens 				(void) fprintf(fp, "\n");
323fa9e4066Sahrens 			else
324fa9e4066Sahrens 				(void) fprintf(fp, "%s",
32565cd9f28Seschrock 				    get_usage(command_table[i].usage));
326fa9e4066Sahrens 		}
327fa9e4066Sahrens 	} else {
328fa9e4066Sahrens 		(void) fprintf(fp, gettext("usage:\n"));
32965cd9f28Seschrock 		(void) fprintf(fp, "%s", get_usage(current_command->usage));
330fa9e4066Sahrens 	}
331fa9e4066Sahrens 
332b1b8ab34Slling 	if (current_command != NULL &&
333b1b8ab34Slling 	    ((strcmp(current_command->name, "set") == 0) ||
334990b4856Slling 	    (strcmp(current_command->name, "get") == 0) ||
335990b4856Slling 	    (strcmp(current_command->name, "list") == 0))) {
336b1b8ab34Slling 
337b1b8ab34Slling 		(void) fprintf(fp,
338b1b8ab34Slling 		    gettext("\nthe following properties are supported:\n"));
339b1b8ab34Slling 
340b24ab676SJeff Bonwick 		(void) fprintf(fp, "\n\t%-15s  %s   %s\n\n",
341990b4856Slling 		    "PROPERTY", "EDIT", "VALUES");
342b1b8ab34Slling 
343b1b8ab34Slling 		/* Iterate over all properties */
344990b4856Slling 		(void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
345990b4856Slling 		    ZFS_TYPE_POOL);
346ad135b5dSChristopher Siden 
347ad135b5dSChristopher Siden 		(void) fprintf(fp, "\t%-15s   ", "feature@...");
348ad135b5dSChristopher Siden 		(void) fprintf(fp, "YES   disabled | enabled | active\n");
349ad135b5dSChristopher Siden 
350ad135b5dSChristopher Siden 		(void) fprintf(fp, gettext("\nThe feature@ properties must be "
351ad135b5dSChristopher Siden 		    "appended with a feature name.\nSee zpool-features(5).\n"));
352b1b8ab34Slling 	}
353b1b8ab34Slling 
354e9dbad6fSeschrock 	/*
355e9dbad6fSeschrock 	 * See comments at end of main().
356e9dbad6fSeschrock 	 */
357e9dbad6fSeschrock 	if (getenv("ZFS_ABORT") != NULL) {
358e9dbad6fSeschrock 		(void) printf("dumping core by request\n");
359e9dbad6fSeschrock 		abort();
360e9dbad6fSeschrock 	}
361e9dbad6fSeschrock 
362fa9e4066Sahrens 	exit(requested ? 0 : 2);
363fa9e4066Sahrens }
364fa9e4066Sahrens 
365fa9e4066Sahrens void
3668654d025Sperrin print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
3678654d025Sperrin     boolean_t print_logs)
368fa9e4066Sahrens {
369fa9e4066Sahrens 	nvlist_t **child;
370fa9e4066Sahrens 	uint_t c, children;
371afefbcddSeschrock 	char *vname;
372fa9e4066Sahrens 
373fa9e4066Sahrens 	if (name != NULL)
374fa9e4066Sahrens 		(void) printf("\t%*s%s\n", indent, "", name);
375fa9e4066Sahrens 
376fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
377fa9e4066Sahrens 	    &child, &children) != 0)
378fa9e4066Sahrens 		return;
379fa9e4066Sahrens 
380afefbcddSeschrock 	for (c = 0; c < children; c++) {
3818654d025Sperrin 		uint64_t is_log = B_FALSE;
3828654d025Sperrin 
3838654d025Sperrin 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3848654d025Sperrin 		    &is_log);
3858654d025Sperrin 		if ((is_log && !print_logs) || (!is_log && print_logs))
3868654d025Sperrin 			continue;
3878654d025Sperrin 
38888ecc943SGeorge Wilson 		vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3898654d025Sperrin 		print_vdev_tree(zhp, vname, child[c], indent + 2,
3908654d025Sperrin 		    B_FALSE);
391afefbcddSeschrock 		free(vname);
392afefbcddSeschrock 	}
393fa9e4066Sahrens }
394fa9e4066Sahrens 
39557221772SChristopher Siden static boolean_t
39657221772SChristopher Siden prop_list_contains_feature(nvlist_t *proplist)
39757221772SChristopher Siden {
39857221772SChristopher Siden 	nvpair_t *nvp;
39957221772SChristopher Siden 	for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
40057221772SChristopher Siden 	    nvp = nvlist_next_nvpair(proplist, nvp)) {
40157221772SChristopher Siden 		if (zpool_prop_feature(nvpair_name(nvp)))
40257221772SChristopher Siden 			return (B_TRUE);
40357221772SChristopher Siden 	}
40457221772SChristopher Siden 	return (B_FALSE);
40557221772SChristopher Siden }
40657221772SChristopher Siden 
407990b4856Slling /*
408990b4856Slling  * Add a property pair (name, string-value) into a property nvlist.
409990b4856Slling  */
410990b4856Slling static int
4110a48a24eStimh add_prop_list(const char *propname, char *propval, nvlist_t **props,
4120a48a24eStimh     boolean_t poolprop)
413990b4856Slling {
414*d2aa06e8SAndrew Stormont 	zpool_prop_t prop = ZPOOL_PROP_INVAL;
4150a48a24eStimh 	zfs_prop_t fprop;
416990b4856Slling 	nvlist_t *proplist;
4170a48a24eStimh 	const char *normnm;
4180a48a24eStimh 	char *strval;
419990b4856Slling 
420990b4856Slling 	if (*props == NULL &&
421990b4856Slling 	    nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
422990b4856Slling 		(void) fprintf(stderr,
423990b4856Slling 		    gettext("internal error: out of memory\n"));
424990b4856Slling 		return (1);
425990b4856Slling 	}
426990b4856Slling 
427990b4856Slling 	proplist = *props;
428990b4856Slling 
4290a48a24eStimh 	if (poolprop) {
43057221772SChristopher Siden 		const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
43157221772SChristopher Siden 
432*d2aa06e8SAndrew Stormont 		if ((prop = zpool_name_to_prop(propname)) == ZPOOL_PROP_INVAL &&
433ad135b5dSChristopher Siden 		    !zpool_prop_feature(propname)) {
4340a48a24eStimh 			(void) fprintf(stderr, gettext("property '%s' is "
4350a48a24eStimh 			    "not a valid pool property\n"), propname);
4360a48a24eStimh 			return (2);
4370a48a24eStimh 		}
43857221772SChristopher Siden 
43957221772SChristopher Siden 		/*
44057221772SChristopher Siden 		 * feature@ properties and version should not be specified
44157221772SChristopher Siden 		 * at the same time.
44257221772SChristopher Siden 		 */
4434ae5f5f0SAlan Somers 		if ((prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname) &&
44457221772SChristopher Siden 		    nvlist_exists(proplist, vname)) ||
44557221772SChristopher Siden 		    (prop == ZPOOL_PROP_VERSION &&
44657221772SChristopher Siden 		    prop_list_contains_feature(proplist))) {
44757221772SChristopher Siden 			(void) fprintf(stderr, gettext("'feature@' and "
44857221772SChristopher Siden 			    "'version' properties cannot be specified "
44957221772SChristopher Siden 			    "together\n"));
45057221772SChristopher Siden 			return (2);
45157221772SChristopher Siden 		}
45257221772SChristopher Siden 
45357221772SChristopher Siden 
454ad135b5dSChristopher Siden 		if (zpool_prop_feature(propname))
455ad135b5dSChristopher Siden 			normnm = propname;
456ad135b5dSChristopher Siden 		else
457ad135b5dSChristopher Siden 			normnm = zpool_prop_to_name(prop);
4580a48a24eStimh 	} else {
45914843421SMatthew Ahrens 		if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
46014843421SMatthew Ahrens 			normnm = zfs_prop_to_name(fprop);
46114843421SMatthew Ahrens 		} else {
46214843421SMatthew Ahrens 			normnm = propname;
4630a48a24eStimh 		}
464990b4856Slling 	}
465990b4856Slling 
4660a48a24eStimh 	if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
4670a48a24eStimh 	    prop != ZPOOL_PROP_CACHEFILE) {
468990b4856Slling 		(void) fprintf(stderr, gettext("property '%s' "
469990b4856Slling 		    "specified multiple times\n"), propname);
470990b4856Slling 		return (2);
471990b4856Slling 	}
472990b4856Slling 
4730a48a24eStimh 	if (nvlist_add_string(proplist, normnm, propval) != 0) {
474990b4856Slling 		(void) fprintf(stderr, gettext("internal "
475990b4856Slling 		    "error: out of memory\n"));
476990b4856Slling 		return (1);
477990b4856Slling 	}
478990b4856Slling 
479990b4856Slling 	return (0);
480990b4856Slling }
481990b4856Slling 
482fa9e4066Sahrens /*
483fa9e4066Sahrens  * zpool add [-fn] <pool> <vdev> ...
484fa9e4066Sahrens  *
485fa9e4066Sahrens  *	-f	Force addition of devices, even if they appear in use
486fa9e4066Sahrens  *	-n	Do not add the devices, but display the resulting layout if
487fa9e4066Sahrens  *		they were to be added.
488fa9e4066Sahrens  *
489fa9e4066Sahrens  * Adds the given vdevs to 'pool'.  As with create, the bulk of this work is
490fa9e4066Sahrens  * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
491fa9e4066Sahrens  * libzfs.
492fa9e4066Sahrens  */
493fa9e4066Sahrens int
494fa9e4066Sahrens zpool_do_add(int argc, char **argv)
495fa9e4066Sahrens {
49699653d4eSeschrock 	boolean_t force = B_FALSE;
49799653d4eSeschrock 	boolean_t dryrun = B_FALSE;
498fa9e4066Sahrens 	int c;
499fa9e4066Sahrens 	nvlist_t *nvroot;
500fa9e4066Sahrens 	char *poolname;
5017855d95bSToomas Soome 	zpool_boot_label_t boot_type;
5027855d95bSToomas Soome 	uint64_t boot_size;
503fa9e4066Sahrens 	int ret;
504fa9e4066Sahrens 	zpool_handle_t *zhp;
505fa9e4066Sahrens 	nvlist_t *config;
506fa9e4066Sahrens 
507fa9e4066Sahrens 	/* check options */
508fa9e4066Sahrens 	while ((c = getopt(argc, argv, "fn")) != -1) {
509fa9e4066Sahrens 		switch (c) {
510fa9e4066Sahrens 		case 'f':
51199653d4eSeschrock 			force = B_TRUE;
512fa9e4066Sahrens 			break;
513fa9e4066Sahrens 		case 'n':
51499653d4eSeschrock 			dryrun = B_TRUE;
515fa9e4066Sahrens 			break;
516fa9e4066Sahrens 		case '?':
517fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
518fa9e4066Sahrens 			    optopt);
51999653d4eSeschrock 			usage(B_FALSE);
520fa9e4066Sahrens 		}
521fa9e4066Sahrens 	}
522fa9e4066Sahrens 
523fa9e4066Sahrens 	argc -= optind;
524fa9e4066Sahrens 	argv += optind;
525fa9e4066Sahrens 
526fa9e4066Sahrens 	/* get pool name and check number of arguments */
527fa9e4066Sahrens 	if (argc < 1) {
528fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
52999653d4eSeschrock 		usage(B_FALSE);
530fa9e4066Sahrens 	}
531fa9e4066Sahrens 	if (argc < 2) {
532fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing vdev specification\n"));
53399653d4eSeschrock 		usage(B_FALSE);
534fa9e4066Sahrens 	}
535fa9e4066Sahrens 
536fa9e4066Sahrens 	poolname = argv[0];
537fa9e4066Sahrens 
538fa9e4066Sahrens 	argc--;
539fa9e4066Sahrens 	argv++;
540fa9e4066Sahrens 
54199653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
542fa9e4066Sahrens 		return (1);
543fa9e4066Sahrens 
544088e9d47Seschrock 	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
545fa9e4066Sahrens 		(void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
546fa9e4066Sahrens 		    poolname);
547fa9e4066Sahrens 		zpool_close(zhp);
548fa9e4066Sahrens 		return (1);
549fa9e4066Sahrens 	}
550fa9e4066Sahrens 
5517855d95bSToomas Soome 	if (zpool_is_bootable(zhp))
5527855d95bSToomas Soome 		boot_type = ZPOOL_COPY_BOOT_LABEL;
5537855d95bSToomas Soome 	else
5547855d95bSToomas Soome 		boot_type = ZPOOL_NO_BOOT_LABEL;
5557855d95bSToomas Soome 
556fa9e4066Sahrens 	/* pass off to get_vdev_spec for processing */
5577855d95bSToomas Soome 	boot_size = zpool_get_prop_int(zhp, ZPOOL_PROP_BOOTSIZE, NULL);
558705040edSEric Taylor 	nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
5597855d95bSToomas Soome 	    boot_type, boot_size, argc, argv);
560fa9e4066Sahrens 	if (nvroot == NULL) {
561fa9e4066Sahrens 		zpool_close(zhp);
562fa9e4066Sahrens 		return (1);
563fa9e4066Sahrens 	}
564fa9e4066Sahrens 
565fa9e4066Sahrens 	if (dryrun) {
566fa9e4066Sahrens 		nvlist_t *poolnvroot;
567fa9e4066Sahrens 
568fa9e4066Sahrens 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
569fa9e4066Sahrens 		    &poolnvroot) == 0);
570fa9e4066Sahrens 
571fa9e4066Sahrens 		(void) printf(gettext("would update '%s' to the following "
572fa9e4066Sahrens 		    "configuration:\n"), zpool_get_name(zhp));
573fa9e4066Sahrens 
5748654d025Sperrin 		/* print original main pool and new tree */
5758654d025Sperrin 		print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
5768654d025Sperrin 		print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
5778654d025Sperrin 
5788654d025Sperrin 		/* Do the same for the logs */
5798654d025Sperrin 		if (num_logs(poolnvroot) > 0) {
5808654d025Sperrin 			print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
5818654d025Sperrin 			print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
5828654d025Sperrin 		} else if (num_logs(nvroot) > 0) {
5838654d025Sperrin 			print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
5848654d025Sperrin 		}
585fa9e4066Sahrens 
586fa9e4066Sahrens 		ret = 0;
587fa9e4066Sahrens 	} else {
588fa9e4066Sahrens 		ret = (zpool_add(zhp, nvroot) != 0);
589fa9e4066Sahrens 	}
590fa9e4066Sahrens 
59199653d4eSeschrock 	nvlist_free(nvroot);
59299653d4eSeschrock 	zpool_close(zhp);
59399653d4eSeschrock 
59499653d4eSeschrock 	return (ret);
59599653d4eSeschrock }
59699653d4eSeschrock 
59799653d4eSeschrock /*
5983f9d6ad7SLin Ling  * zpool remove  <pool> <vdev> ...
59999653d4eSeschrock  *
6005cabbc6bSPrashanth Sreenivasa  * Removes the given vdev from the pool.
60199653d4eSeschrock  */
60299653d4eSeschrock int
60399653d4eSeschrock zpool_do_remove(int argc, char **argv)
60499653d4eSeschrock {
60599653d4eSeschrock 	char *poolname;
606fa94a07fSbrendan 	int i, ret = 0;
60799653d4eSeschrock 	zpool_handle_t *zhp;
6085cabbc6bSPrashanth Sreenivasa 	boolean_t stop = B_FALSE;
6095cabbc6bSPrashanth Sreenivasa 	boolean_t noop = B_FALSE;
6105cabbc6bSPrashanth Sreenivasa 	boolean_t parsable = B_FALSE;
6115cabbc6bSPrashanth Sreenivasa 	char c;
61299653d4eSeschrock 
6135cabbc6bSPrashanth Sreenivasa 	/* check options */
6145cabbc6bSPrashanth Sreenivasa 	while ((c = getopt(argc, argv, "nps")) != -1) {
6155cabbc6bSPrashanth Sreenivasa 		switch (c) {
6165cabbc6bSPrashanth Sreenivasa 		case 'n':
6175cabbc6bSPrashanth Sreenivasa 			noop = B_TRUE;
6185cabbc6bSPrashanth Sreenivasa 			break;
6195cabbc6bSPrashanth Sreenivasa 		case 'p':
6205cabbc6bSPrashanth Sreenivasa 			parsable = B_TRUE;
6215cabbc6bSPrashanth Sreenivasa 			break;
6225cabbc6bSPrashanth Sreenivasa 		case 's':
6235cabbc6bSPrashanth Sreenivasa 			stop = B_TRUE;
6245cabbc6bSPrashanth Sreenivasa 			break;
6255cabbc6bSPrashanth Sreenivasa 		case '?':
6265cabbc6bSPrashanth Sreenivasa 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
6275cabbc6bSPrashanth Sreenivasa 			    optopt);
6285cabbc6bSPrashanth Sreenivasa 			usage(B_FALSE);
6295cabbc6bSPrashanth Sreenivasa 		}
6305cabbc6bSPrashanth Sreenivasa 	}
6315cabbc6bSPrashanth Sreenivasa 
6325cabbc6bSPrashanth Sreenivasa 	argc -= optind;
6335cabbc6bSPrashanth Sreenivasa 	argv += optind;
63499653d4eSeschrock 
63599653d4eSeschrock 	/* get pool name and check number of arguments */
63699653d4eSeschrock 	if (argc < 1) {
63799653d4eSeschrock 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
63899653d4eSeschrock 		usage(B_FALSE);
63999653d4eSeschrock 	}
64099653d4eSeschrock 
64199653d4eSeschrock 	poolname = argv[0];
64299653d4eSeschrock 
64399653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
64499653d4eSeschrock 		return (1);
64599653d4eSeschrock 
6465cabbc6bSPrashanth Sreenivasa 	if (stop && noop) {
6475cabbc6bSPrashanth Sreenivasa 		(void) fprintf(stderr, gettext("stop request ignored\n"));
6485cabbc6bSPrashanth Sreenivasa 		return (0);
6495cabbc6bSPrashanth Sreenivasa 	}
6505cabbc6bSPrashanth Sreenivasa 
6515cabbc6bSPrashanth Sreenivasa 	if (stop) {
6525cabbc6bSPrashanth Sreenivasa 		if (argc > 1) {
6535cabbc6bSPrashanth Sreenivasa 			(void) fprintf(stderr, gettext("too many arguments\n"));
6545cabbc6bSPrashanth Sreenivasa 			usage(B_FALSE);
6555cabbc6bSPrashanth Sreenivasa 		}
6565cabbc6bSPrashanth Sreenivasa 		if (zpool_vdev_remove_cancel(zhp) != 0)
657fa94a07fSbrendan 			ret = 1;
6585cabbc6bSPrashanth Sreenivasa 	} else {
6595cabbc6bSPrashanth Sreenivasa 		if (argc < 2) {
6605cabbc6bSPrashanth Sreenivasa 			(void) fprintf(stderr, gettext("missing device\n"));
6615cabbc6bSPrashanth Sreenivasa 			usage(B_FALSE);
6625cabbc6bSPrashanth Sreenivasa 		}
6635cabbc6bSPrashanth Sreenivasa 
6645cabbc6bSPrashanth Sreenivasa 		for (i = 1; i < argc; i++) {
6655cabbc6bSPrashanth Sreenivasa 			if (noop) {
6665cabbc6bSPrashanth Sreenivasa 				uint64_t size;
6675cabbc6bSPrashanth Sreenivasa 
6685cabbc6bSPrashanth Sreenivasa 				if (zpool_vdev_indirect_size(zhp, argv[i],
6695cabbc6bSPrashanth Sreenivasa 				    &size) != 0) {
6705cabbc6bSPrashanth Sreenivasa 					ret = 1;
6715cabbc6bSPrashanth Sreenivasa 					break;
6725cabbc6bSPrashanth Sreenivasa 				}
6735cabbc6bSPrashanth Sreenivasa 				if (parsable) {
6745cabbc6bSPrashanth Sreenivasa 					(void) printf("%s %llu\n",
6755cabbc6bSPrashanth Sreenivasa 					    argv[i], size);
6765cabbc6bSPrashanth Sreenivasa 				} else {
6775cabbc6bSPrashanth Sreenivasa 					char valstr[32];
6785cabbc6bSPrashanth Sreenivasa 					zfs_nicenum(size, valstr,
6795cabbc6bSPrashanth Sreenivasa 					    sizeof (valstr));
6805cabbc6bSPrashanth Sreenivasa 					(void) printf("Memory that will be "
6815cabbc6bSPrashanth Sreenivasa 					    "used after removing %s: %s\n",
6825cabbc6bSPrashanth Sreenivasa 					    argv[i], valstr);
6835cabbc6bSPrashanth Sreenivasa 				}
6845cabbc6bSPrashanth Sreenivasa 			} else {
6855cabbc6bSPrashanth Sreenivasa 				if (zpool_vdev_remove(zhp, argv[i]) != 0)
6865cabbc6bSPrashanth Sreenivasa 					ret = 1;
6875cabbc6bSPrashanth Sreenivasa 			}
6885cabbc6bSPrashanth Sreenivasa 		}
689fa94a07fSbrendan 	}
69099653d4eSeschrock 
691fa9e4066Sahrens 	return (ret);
692fa9e4066Sahrens }
693fa9e4066Sahrens 
6946401734dSWill Andrews /*
6956401734dSWill Andrews  * zpool labelclear [-f] <vdev>
6966401734dSWill Andrews  *
6976401734dSWill Andrews  *	-f	Force clearing the label for the vdevs which are members of
6986401734dSWill Andrews  *		the exported or foreign pools.
6996401734dSWill Andrews  *
7006401734dSWill Andrews  * Verifies that the vdev is not active and zeros out the label information
7016401734dSWill Andrews  * on the device.
7026401734dSWill Andrews  */
7036401734dSWill Andrews int
7046401734dSWill Andrews zpool_do_labelclear(int argc, char **argv)
7056401734dSWill Andrews {
7066401734dSWill Andrews 	char vdev[MAXPATHLEN];
7076401734dSWill Andrews 	char *name = NULL;
7086401734dSWill Andrews 	struct stat st;
7096401734dSWill Andrews 	int c, fd, ret = 0;
7106401734dSWill Andrews 	nvlist_t *config;
7116401734dSWill Andrews 	pool_state_t state;
7126401734dSWill Andrews 	boolean_t inuse = B_FALSE;
7136401734dSWill Andrews 	boolean_t force = B_FALSE;
7146401734dSWill Andrews 
7156401734dSWill Andrews 	/* check options */
7166401734dSWill Andrews 	while ((c = getopt(argc, argv, "f")) != -1) {
7176401734dSWill Andrews 		switch (c) {
7186401734dSWill Andrews 		case 'f':
7196401734dSWill Andrews 			force = B_TRUE;
7206401734dSWill Andrews 			break;
7216401734dSWill Andrews 		default:
7226401734dSWill Andrews 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
7236401734dSWill Andrews 			    optopt);
7246401734dSWill Andrews 			usage(B_FALSE);
7256401734dSWill Andrews 		}
7266401734dSWill Andrews 	}
7276401734dSWill Andrews 
7286401734dSWill Andrews 	argc -= optind;
7296401734dSWill Andrews 	argv += optind;
7306401734dSWill Andrews 
7316401734dSWill Andrews 	/* get vdev name */
7326401734dSWill Andrews 	if (argc < 1) {
7336401734dSWill Andrews 		(void) fprintf(stderr, gettext("missing vdev name\n"));
7346401734dSWill Andrews 		usage(B_FALSE);
7356401734dSWill Andrews 	}
7366401734dSWill Andrews 	if (argc > 1) {
7376401734dSWill Andrews 		(void) fprintf(stderr, gettext("too many arguments\n"));
7386401734dSWill Andrews 		usage(B_FALSE);
7396401734dSWill Andrews 	}
7406401734dSWill Andrews 
7416401734dSWill Andrews 	/*
7426401734dSWill Andrews 	 * Check if we were given absolute path and use it as is.
7436401734dSWill Andrews 	 * Otherwise if the provided vdev name doesn't point to a file,
7446401734dSWill Andrews 	 * try prepending dsk path and appending s0.
7456401734dSWill Andrews 	 */
7466401734dSWill Andrews 	(void) strlcpy(vdev, argv[0], sizeof (vdev));
7476401734dSWill Andrews 	if (vdev[0] != '/' && stat(vdev, &st) != 0) {
7486401734dSWill Andrews 		char *s;
7496401734dSWill Andrews 
7506401734dSWill Andrews 		(void) snprintf(vdev, sizeof (vdev), "%s/%s",
7516401734dSWill Andrews 		    ZFS_DISK_ROOT, argv[0]);
7526401734dSWill Andrews 		if ((s = strrchr(argv[0], 's')) == NULL ||
7536401734dSWill Andrews 		    !isdigit(*(s + 1)))
7546401734dSWill Andrews 			(void) strlcat(vdev, "s0", sizeof (vdev));
7556401734dSWill Andrews 		if (stat(vdev, &st) != 0) {
7566401734dSWill Andrews 			(void) fprintf(stderr, gettext(
7576401734dSWill Andrews 			    "failed to find device %s, try specifying absolute "
7586401734dSWill Andrews 			    "path instead\n"), argv[0]);
7596401734dSWill Andrews 			return (1);
7606401734dSWill Andrews 		}
7616401734dSWill Andrews 	}
7626401734dSWill Andrews 
7636401734dSWill Andrews 	if ((fd = open(vdev, O_RDWR)) < 0) {
7646401734dSWill Andrews 		(void) fprintf(stderr, gettext("failed to open %s: %s\n"),
7656401734dSWill Andrews 		    vdev, strerror(errno));
7666401734dSWill Andrews 		return (1);
7676401734dSWill Andrews 	}
7686401734dSWill Andrews 
769c861bfbdSAlan Somers 	if (zpool_read_label(fd, &config) != 0) {
7706401734dSWill Andrews 		(void) fprintf(stderr,
7716401734dSWill Andrews 		    gettext("failed to read label from %s\n"), vdev);
7726401734dSWill Andrews 		return (1);
7736401734dSWill Andrews 	}
7746401734dSWill Andrews 	nvlist_free(config);
7756401734dSWill Andrews 
7766401734dSWill Andrews 	ret = zpool_in_use(g_zfs, fd, &state, &name, &inuse);
7776401734dSWill Andrews 	if (ret != 0) {
7786401734dSWill Andrews 		(void) fprintf(stderr,
7796401734dSWill Andrews 		    gettext("failed to check state for %s\n"), vdev);
7806401734dSWill Andrews 		return (1);
7816401734dSWill Andrews 	}
7826401734dSWill Andrews 
7836401734dSWill Andrews 	if (!inuse)
7846401734dSWill Andrews 		goto wipe_label;
7856401734dSWill Andrews 
7866401734dSWill Andrews 	switch (state) {
7876401734dSWill Andrews 	default:
7886401734dSWill Andrews 	case POOL_STATE_ACTIVE:
7896401734dSWill Andrews 	case POOL_STATE_SPARE:
7906401734dSWill Andrews 	case POOL_STATE_L2CACHE:
7916401734dSWill Andrews 		(void) fprintf(stderr, gettext(
7926401734dSWill Andrews 		    "%s is a member (%s) of pool \"%s\"\n"),
7936401734dSWill Andrews 		    vdev, zpool_pool_state_to_name(state), name);
7946401734dSWill Andrews 		ret = 1;
7956401734dSWill Andrews 		goto errout;
7966401734dSWill Andrews 
7976401734dSWill Andrews 	case POOL_STATE_EXPORTED:
7986401734dSWill Andrews 		if (force)
7996401734dSWill Andrews 			break;
8006401734dSWill Andrews 		(void) fprintf(stderr, gettext(
8016401734dSWill Andrews 		    "use '-f' to override the following error:\n"
8026401734dSWill Andrews 		    "%s is a member of exported pool \"%s\"\n"),
8036401734dSWill Andrews 		    vdev, name);
8046401734dSWill Andrews 		ret = 1;
8056401734dSWill Andrews 		goto errout;
8066401734dSWill Andrews 
8076401734dSWill Andrews 	case POOL_STATE_POTENTIALLY_ACTIVE:
8086401734dSWill Andrews 		if (force)
8096401734dSWill Andrews 			break;
8106401734dSWill Andrews 		(void) fprintf(stderr, gettext(
8116401734dSWill Andrews 		    "use '-f' to override the following error:\n"
8126401734dSWill Andrews 		    "%s is a member of potentially active pool \"%s\"\n"),
8136401734dSWill Andrews 		    vdev, name);
8146401734dSWill Andrews 		ret = 1;
8156401734dSWill Andrews 		goto errout;
8166401734dSWill Andrews 
8176401734dSWill Andrews 	case POOL_STATE_DESTROYED:
8186401734dSWill Andrews 		/* inuse should never be set for a destroyed pool */
8196401734dSWill Andrews 		assert(0);
8206401734dSWill Andrews 		break;
8216401734dSWill Andrews 	}
8226401734dSWill Andrews 
8236401734dSWill Andrews wipe_label:
8246401734dSWill Andrews 	ret = zpool_clear_label(fd);
8256401734dSWill Andrews 	if (ret != 0) {
8266401734dSWill Andrews 		(void) fprintf(stderr,
8276401734dSWill Andrews 		    gettext("failed to clear label for %s\n"), vdev);
8286401734dSWill Andrews 	}
8296401734dSWill Andrews 
8306401734dSWill Andrews errout:
8316401734dSWill Andrews 	free(name);
8326401734dSWill Andrews 	(void) close(fd);
8336401734dSWill Andrews 
8346401734dSWill Andrews 	return (ret);
8356401734dSWill Andrews }
8366401734dSWill Andrews 
837fa9e4066Sahrens /*
8387855d95bSToomas Soome  * zpool create [-fnd] [-B] [-o property=value] ...
8390a48a24eStimh  *		[-O file-system-property=value] ...
8400a48a24eStimh  *		[-R root] [-m mountpoint] <pool> <dev> ...
841fa9e4066Sahrens  *
8427855d95bSToomas Soome  *	-B	Create boot partition.
843fa9e4066Sahrens  *	-f	Force creation, even if devices appear in use
844fa9e4066Sahrens  *	-n	Do not create the pool, but display the resulting layout if it
845fa9e4066Sahrens  *		were to be created.
846fa9e4066Sahrens  *      -R	Create a pool under an alternate root
847fa9e4066Sahrens  *      -m	Set default mountpoint for the root dataset.  By default it's
848ad135b5dSChristopher Siden  *		'/<pool>'
849990b4856Slling  *	-o	Set property=value.
850ad135b5dSChristopher Siden  *	-d	Don't automatically enable all supported pool features
851ad135b5dSChristopher Siden  *		(individual features can be enabled with -o).
8520a48a24eStimh  *	-O	Set fsproperty=value in the pool's root file system
853fa9e4066Sahrens  *
854b1b8ab34Slling  * Creates the named pool according to the given vdev specification.  The
855fa9e4066Sahrens  * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c.  Once
856fa9e4066Sahrens  * we get the nvlist back from get_vdev_spec(), we either print out the contents
857fa9e4066Sahrens  * (if '-n' was specified), or pass it to libzfs to do the creation.
858fa9e4066Sahrens  */
8597855d95bSToomas Soome 
8607855d95bSToomas Soome #define	SYSTEM256	(256 * 1024 * 1024)
861fa9e4066Sahrens int
862fa9e4066Sahrens zpool_do_create(int argc, char **argv)
863fa9e4066Sahrens {
86499653d4eSeschrock 	boolean_t force = B_FALSE;
86599653d4eSeschrock 	boolean_t dryrun = B_FALSE;
866ad135b5dSChristopher Siden 	boolean_t enable_all_pool_feat = B_TRUE;
8677855d95bSToomas Soome 	zpool_boot_label_t boot_type = ZPOOL_NO_BOOT_LABEL;
8687855d95bSToomas Soome 	uint64_t boot_size = 0;
869fa9e4066Sahrens 	int c;
870990b4856Slling 	nvlist_t *nvroot = NULL;
871fa9e4066Sahrens 	char *poolname;
872990b4856Slling 	int ret = 1;
873fa9e4066Sahrens 	char *altroot = NULL;
874fa9e4066Sahrens 	char *mountpoint = NULL;
8750a48a24eStimh 	nvlist_t *fsprops = NULL;
876990b4856Slling 	nvlist_t *props = NULL;
8772f8aaab3Seschrock 	char *propval;
878fa9e4066Sahrens 
879fa9e4066Sahrens 	/* check options */
8807855d95bSToomas Soome 	while ((c = getopt(argc, argv, ":fndBR:m:o:O:")) != -1) {
881fa9e4066Sahrens 		switch (c) {
882fa9e4066Sahrens 		case 'f':
88399653d4eSeschrock 			force = B_TRUE;
884fa9e4066Sahrens 			break;
885fa9e4066Sahrens 		case 'n':
88699653d4eSeschrock 			dryrun = B_TRUE;
887fa9e4066Sahrens 			break;
888ad135b5dSChristopher Siden 		case 'd':
889ad135b5dSChristopher Siden 			enable_all_pool_feat = B_FALSE;
890ad135b5dSChristopher Siden 			break;
8917855d95bSToomas Soome 		case 'B':
8927855d95bSToomas Soome 			/*
8937855d95bSToomas Soome 			 * We should create the system partition.
8947855d95bSToomas Soome 			 * Also make sure the size is set.
8957855d95bSToomas Soome 			 */
8967855d95bSToomas Soome 			boot_type = ZPOOL_CREATE_BOOT_LABEL;
8977855d95bSToomas Soome 			if (boot_size == 0)
8987855d95bSToomas Soome 				boot_size = SYSTEM256;
8997855d95bSToomas Soome 			break;
900fa9e4066Sahrens 		case 'R':
901fa9e4066Sahrens 			altroot = optarg;
902990b4856Slling 			if (add_prop_list(zpool_prop_to_name(
9030a48a24eStimh 			    ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
904990b4856Slling 				goto errout;
9052f8aaab3Seschrock 			if (nvlist_lookup_string(props,
9062f8aaab3Seschrock 			    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
9072f8aaab3Seschrock 			    &propval) == 0)
9082f8aaab3Seschrock 				break;
909990b4856Slling 			if (add_prop_list(zpool_prop_to_name(
9100a48a24eStimh 			    ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
911990b4856Slling 				goto errout;
912fa9e4066Sahrens 			break;
913fa9e4066Sahrens 		case 'm':
9148b713775SWill Andrews 			/* Equivalent to -O mountpoint=optarg */
915fa9e4066Sahrens 			mountpoint = optarg;
916fa9e4066Sahrens 			break;
917990b4856Slling 		case 'o':
918990b4856Slling 			if ((propval = strchr(optarg, '=')) == NULL) {
919990b4856Slling 				(void) fprintf(stderr, gettext("missing "
920990b4856Slling 				    "'=' for -o option\n"));
921990b4856Slling 				goto errout;
922990b4856Slling 			}
923990b4856Slling 			*propval = '\0';
924990b4856Slling 			propval++;
925990b4856Slling 
9260a48a24eStimh 			if (add_prop_list(optarg, propval, &props, B_TRUE))
9270a48a24eStimh 				goto errout;
928ad135b5dSChristopher Siden 
9297855d95bSToomas Soome 			/*
9307855d95bSToomas Soome 			 * Get bootsize value for make_root_vdev().
9317855d95bSToomas Soome 			 */
9327855d95bSToomas Soome 			if (zpool_name_to_prop(optarg) == ZPOOL_PROP_BOOTSIZE) {
9337855d95bSToomas Soome 				if (zfs_nicestrtonum(g_zfs, propval,
9347855d95bSToomas Soome 				    &boot_size) < 0 || boot_size == 0) {
9357855d95bSToomas Soome 					(void) fprintf(stderr,
9367855d95bSToomas Soome 					    gettext("bad boot partition size "
9377855d95bSToomas Soome 					    "'%s': %s\n"),  propval,
9387855d95bSToomas Soome 					    libzfs_error_description(g_zfs));
9397855d95bSToomas Soome 					goto errout;
9407855d95bSToomas Soome 				}
9417855d95bSToomas Soome 			}
9427855d95bSToomas Soome 
943ad135b5dSChristopher Siden 			/*
944ad135b5dSChristopher Siden 			 * If the user is creating a pool that doesn't support
945ad135b5dSChristopher Siden 			 * feature flags, don't enable any features.
946ad135b5dSChristopher Siden 			 */
947ad135b5dSChristopher Siden 			if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
948ad135b5dSChristopher Siden 				char *end;
949ad135b5dSChristopher Siden 				u_longlong_t ver;
950ad135b5dSChristopher Siden 
951ad135b5dSChristopher Siden 				ver = strtoull(propval, &end, 10);
952ad135b5dSChristopher Siden 				if (*end == '\0' &&
953ad135b5dSChristopher Siden 				    ver < SPA_VERSION_FEATURES) {
954ad135b5dSChristopher Siden 					enable_all_pool_feat = B_FALSE;
955ad135b5dSChristopher Siden 				}
956ad135b5dSChristopher Siden 			}
957c423721fSXin Li 			if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT)
958c423721fSXin Li 				altroot = propval;
9590a48a24eStimh 			break;
9600a48a24eStimh 		case 'O':
9610a48a24eStimh 			if ((propval = strchr(optarg, '=')) == NULL) {
9620a48a24eStimh 				(void) fprintf(stderr, gettext("missing "
9630a48a24eStimh 				    "'=' for -O option\n"));
9640a48a24eStimh 				goto errout;
9650a48a24eStimh 			}
9660a48a24eStimh 			*propval = '\0';
9670a48a24eStimh 			propval++;
9680a48a24eStimh 
9698b713775SWill Andrews 			/*
9708b713775SWill Andrews 			 * Mountpoints are checked and then added later.
9718b713775SWill Andrews 			 * Uniquely among properties, they can be specified
9728b713775SWill Andrews 			 * more than once, to avoid conflict with -m.
9738b713775SWill Andrews 			 */
9748b713775SWill Andrews 			if (0 == strcmp(optarg,
9758b713775SWill Andrews 			    zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
9768b713775SWill Andrews 				mountpoint = propval;
9778b713775SWill Andrews 			} else if (add_prop_list(optarg, propval, &fsprops,
9788b713775SWill Andrews 			    B_FALSE)) {
979990b4856Slling 				goto errout;
9808b713775SWill Andrews 			}
981990b4856Slling 			break;
982fa9e4066Sahrens 		case ':':
983fa9e4066Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
984fa9e4066Sahrens 			    "'%c' option\n"), optopt);
985990b4856Slling 			goto badusage;
986fa9e4066Sahrens 		case '?':
987fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
988fa9e4066Sahrens 			    optopt);
989990b4856Slling 			goto badusage;
990fa9e4066Sahrens 		}
991fa9e4066Sahrens 	}
992fa9e4066Sahrens 
993fa9e4066Sahrens 	argc -= optind;
994fa9e4066Sahrens 	argv += optind;
995fa9e4066Sahrens 
996fa9e4066Sahrens 	/* get pool name and check number of arguments */
997fa9e4066Sahrens 	if (argc < 1) {
998fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
999990b4856Slling 		goto badusage;
1000fa9e4066Sahrens 	}
1001fa9e4066Sahrens 	if (argc < 2) {
1002fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing vdev specification\n"));
1003990b4856Slling 		goto badusage;
1004fa9e4066Sahrens 	}
1005fa9e4066Sahrens 
1006fa9e4066Sahrens 	poolname = argv[0];
1007fa9e4066Sahrens 
1008fa9e4066Sahrens 	/*
1009fa9e4066Sahrens 	 * As a special case, check for use of '/' in the name, and direct the
1010fa9e4066Sahrens 	 * user to use 'zfs create' instead.
1011fa9e4066Sahrens 	 */
1012fa9e4066Sahrens 	if (strchr(poolname, '/') != NULL) {
1013fa9e4066Sahrens 		(void) fprintf(stderr, gettext("cannot create '%s': invalid "
1014fa9e4066Sahrens 		    "character '/' in pool name\n"), poolname);
1015fa9e4066Sahrens 		(void) fprintf(stderr, gettext("use 'zfs create' to "
1016fa9e4066Sahrens 		    "create a dataset\n"));
1017990b4856Slling 		goto errout;
1018fa9e4066Sahrens 	}
1019fa9e4066Sahrens 
10207855d95bSToomas Soome 	/*
10217855d95bSToomas Soome 	 * Make sure the bootsize is set when ZPOOL_CREATE_BOOT_LABEL is used,
10227855d95bSToomas Soome 	 * and not set otherwise.
10237855d95bSToomas Soome 	 */
10247855d95bSToomas Soome 	if (boot_type == ZPOOL_CREATE_BOOT_LABEL) {
10257855d95bSToomas Soome 		const char *propname;
10267855d95bSToomas Soome 		char *strptr, *buf = NULL;
10277855d95bSToomas Soome 		int rv;
10287855d95bSToomas Soome 
10297855d95bSToomas Soome 		propname = zpool_prop_to_name(ZPOOL_PROP_BOOTSIZE);
10307855d95bSToomas Soome 		if (nvlist_lookup_string(props, propname, &strptr) != 0) {
10317855d95bSToomas Soome 			(void) asprintf(&buf, "%" PRIu64, boot_size);
10327855d95bSToomas Soome 			if (buf == NULL) {
10337855d95bSToomas Soome 				(void) fprintf(stderr,
10347855d95bSToomas Soome 				    gettext("internal error: out of memory\n"));
10357855d95bSToomas Soome 				goto errout;
10367855d95bSToomas Soome 			}
10377855d95bSToomas Soome 			rv = add_prop_list(propname, buf, &props, B_TRUE);
10387855d95bSToomas Soome 			free(buf);
10397855d95bSToomas Soome 			if (rv != 0)
10407855d95bSToomas Soome 				goto errout;
10417855d95bSToomas Soome 		}
10427855d95bSToomas Soome 	} else {
10437855d95bSToomas Soome 		const char *propname;
10447855d95bSToomas Soome 		char *strptr;
10457855d95bSToomas Soome 
10467855d95bSToomas Soome 		propname = zpool_prop_to_name(ZPOOL_PROP_BOOTSIZE);
10477855d95bSToomas Soome 		if (nvlist_lookup_string(props, propname, &strptr) == 0) {
10487855d95bSToomas Soome 			(void) fprintf(stderr, gettext("error: setting boot "
10497855d95bSToomas Soome 			    "partition size requires option '-B'\n"));
10507855d95bSToomas Soome 			goto errout;
10517855d95bSToomas Soome 		}
10527855d95bSToomas Soome 	}
10537855d95bSToomas Soome 
1054fa9e4066Sahrens 	/* pass off to get_vdev_spec for bulk processing */
1055705040edSEric Taylor 	nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
10567855d95bSToomas Soome 	    boot_type, boot_size, argc - 1, argv + 1);
1057fa9e4066Sahrens 	if (nvroot == NULL)
10580a48a24eStimh 		goto errout;
1059fa9e4066Sahrens 
106099653d4eSeschrock 	/* make_root_vdev() allows 0 toplevel children if there are spares */
1061b7b97454Sperrin 	if (!zfs_allocatable_devs(nvroot)) {
106299653d4eSeschrock 		(void) fprintf(stderr, gettext("invalid vdev "
106399653d4eSeschrock 		    "specification: at least one toplevel vdev must be "
106499653d4eSeschrock 		    "specified\n"));
1065990b4856Slling 		goto errout;
106699653d4eSeschrock 	}
106799653d4eSeschrock 
1068fa9e4066Sahrens 	if (altroot != NULL && altroot[0] != '/') {
1069fa9e4066Sahrens 		(void) fprintf(stderr, gettext("invalid alternate root '%s': "
1070e9dbad6fSeschrock 		    "must be an absolute path\n"), altroot);
1071990b4856Slling 		goto errout;
1072fa9e4066Sahrens 	}
1073fa9e4066Sahrens 
1074fa9e4066Sahrens 	/*
1075fa9e4066Sahrens 	 * Check the validity of the mountpoint and direct the user to use the
1076fa9e4066Sahrens 	 * '-m' mountpoint option if it looks like its in use.
1077fa9e4066Sahrens 	 */
1078fa9e4066Sahrens 	if (mountpoint == NULL ||
1079fa9e4066Sahrens 	    (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
1080fa9e4066Sahrens 	    strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
1081fa9e4066Sahrens 		char buf[MAXPATHLEN];
108211022c7cStimh 		DIR *dirp;
1083fa9e4066Sahrens 
1084fa9e4066Sahrens 		if (mountpoint && mountpoint[0] != '/') {
1085fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid mountpoint "
1086fa9e4066Sahrens 			    "'%s': must be an absolute path, 'legacy', or "
1087fa9e4066Sahrens 			    "'none'\n"), mountpoint);
1088990b4856Slling 			goto errout;
1089fa9e4066Sahrens 		}
1090fa9e4066Sahrens 
1091fa9e4066Sahrens 		if (mountpoint == NULL) {
1092fa9e4066Sahrens 			if (altroot != NULL)
1093fa9e4066Sahrens 				(void) snprintf(buf, sizeof (buf), "%s/%s",
1094fa9e4066Sahrens 				    altroot, poolname);
1095fa9e4066Sahrens 			else
1096fa9e4066Sahrens 				(void) snprintf(buf, sizeof (buf), "/%s",
1097fa9e4066Sahrens 				    poolname);
1098fa9e4066Sahrens 		} else {
1099fa9e4066Sahrens 			if (altroot != NULL)
1100fa9e4066Sahrens 				(void) snprintf(buf, sizeof (buf), "%s%s",
1101fa9e4066Sahrens 				    altroot, mountpoint);
1102fa9e4066Sahrens 			else
1103fa9e4066Sahrens 				(void) snprintf(buf, sizeof (buf), "%s",
1104fa9e4066Sahrens 				    mountpoint);
1105fa9e4066Sahrens 		}
1106fa9e4066Sahrens 
110711022c7cStimh 		if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
110811022c7cStimh 			(void) fprintf(stderr, gettext("mountpoint '%s' : "
110911022c7cStimh 			    "%s\n"), buf, strerror(errno));
1110fa9e4066Sahrens 			(void) fprintf(stderr, gettext("use '-m' "
1111fa9e4066Sahrens 			    "option to provide a different default\n"));
1112990b4856Slling 			goto errout;
111311022c7cStimh 		} else if (dirp) {
111411022c7cStimh 			int count = 0;
111511022c7cStimh 
111611022c7cStimh 			while (count < 3 && readdir(dirp) != NULL)
111711022c7cStimh 				count++;
111811022c7cStimh 			(void) closedir(dirp);
111911022c7cStimh 
112011022c7cStimh 			if (count > 2) {
112111022c7cStimh 				(void) fprintf(stderr, gettext("mountpoint "
112211022c7cStimh 				    "'%s' exists and is not empty\n"), buf);
112311022c7cStimh 				(void) fprintf(stderr, gettext("use '-m' "
112411022c7cStimh 				    "option to provide a "
112511022c7cStimh 				    "different default\n"));
112611022c7cStimh 				goto errout;
112711022c7cStimh 			}
1128fa9e4066Sahrens 		}
1129fa9e4066Sahrens 	}
1130fa9e4066Sahrens 
11318b713775SWill Andrews 	/*
11328b713775SWill Andrews 	 * Now that the mountpoint's validity has been checked, ensure that
11338b713775SWill Andrews 	 * the property is set appropriately prior to creating the pool.
11348b713775SWill Andrews 	 */
11358b713775SWill Andrews 	if (mountpoint != NULL) {
11368b713775SWill Andrews 		ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
11378b713775SWill Andrews 		    mountpoint, &fsprops, B_FALSE);
11388b713775SWill Andrews 		if (ret != 0)
11398b713775SWill Andrews 			goto errout;
11408b713775SWill Andrews 	}
11418b713775SWill Andrews 
11428b713775SWill Andrews 	ret = 1;
1143fa9e4066Sahrens 	if (dryrun) {
1144fa9e4066Sahrens 		/*
1145fa9e4066Sahrens 		 * For a dry run invocation, print out a basic message and run
1146fa9e4066Sahrens 		 * through all the vdevs in the list and print out in an
1147fa9e4066Sahrens 		 * appropriate hierarchy.
1148fa9e4066Sahrens 		 */
1149fa9e4066Sahrens 		(void) printf(gettext("would create '%s' with the "
1150fa9e4066Sahrens 		    "following layout:\n\n"), poolname);
1151fa9e4066Sahrens 
11528654d025Sperrin 		print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
11538654d025Sperrin 		if (num_logs(nvroot) > 0)
11548654d025Sperrin 			print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
1155fa9e4066Sahrens 
1156fa9e4066Sahrens 		ret = 0;
1157fa9e4066Sahrens 	} else {
1158fa9e4066Sahrens 		/*
1159fa9e4066Sahrens 		 * Hand off to libzfs.
1160fa9e4066Sahrens 		 */
1161ad135b5dSChristopher Siden 		if (enable_all_pool_feat) {
11622acef22dSMatthew Ahrens 			spa_feature_t i;
1163ad135b5dSChristopher Siden 			for (i = 0; i < SPA_FEATURES; i++) {
1164ad135b5dSChristopher Siden 				char propname[MAXPATHLEN];
1165ad135b5dSChristopher Siden 				zfeature_info_t *feat = &spa_feature_table[i];
1166ad135b5dSChristopher Siden 
1167ad135b5dSChristopher Siden 				(void) snprintf(propname, sizeof (propname),
1168ad135b5dSChristopher Siden 				    "feature@%s", feat->fi_uname);
1169ad135b5dSChristopher Siden 
1170ad135b5dSChristopher Siden 				/*
1171ad135b5dSChristopher Siden 				 * Skip feature if user specified it manually
1172ad135b5dSChristopher Siden 				 * on the command line.
1173ad135b5dSChristopher Siden 				 */
1174ad135b5dSChristopher Siden 				if (nvlist_exists(props, propname))
1175ad135b5dSChristopher Siden 					continue;
1176ad135b5dSChristopher Siden 
11778b713775SWill Andrews 				ret = add_prop_list(propname,
11788b713775SWill Andrews 				    ZFS_FEATURE_ENABLED, &props, B_TRUE);
11798b713775SWill Andrews 				if (ret != 0)
1180ad135b5dSChristopher Siden 					goto errout;
1181ad135b5dSChristopher Siden 			}
1182ad135b5dSChristopher Siden 		}
11838b713775SWill Andrews 
11848b713775SWill Andrews 		ret = 1;
11850a48a24eStimh 		if (zpool_create(g_zfs, poolname,
11860a48a24eStimh 		    nvroot, props, fsprops) == 0) {
118799653d4eSeschrock 			zfs_handle_t *pool = zfs_open(g_zfs, poolname,
1188fa9e4066Sahrens 			    ZFS_TYPE_FILESYSTEM);
1189fa9e4066Sahrens 			if (pool != NULL) {
1190fa9e4066Sahrens 				if (zfs_mount(pool, NULL, 0) == 0)
1191da6c28aaSamw 					ret = zfs_shareall(pool);
1192fa9e4066Sahrens 				zfs_close(pool);
1193fa9e4066Sahrens 			}
119499653d4eSeschrock 		} else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
119599653d4eSeschrock 			(void) fprintf(stderr, gettext("pool name may have "
119699653d4eSeschrock 			    "been omitted\n"));
1197fa9e4066Sahrens 		}
1198fa9e4066Sahrens 	}
1199fa9e4066Sahrens 
1200990b4856Slling errout:
12012f8aaab3Seschrock 	nvlist_free(nvroot);
12020a48a24eStimh 	nvlist_free(fsprops);
12032f8aaab3Seschrock 	nvlist_free(props);
1204fa9e4066Sahrens 	return (ret);
1205990b4856Slling badusage:
12060a48a24eStimh 	nvlist_free(fsprops);
1207990b4856Slling 	nvlist_free(props);
1208990b4856Slling 	usage(B_FALSE);
1209990b4856Slling 	return (2);
1210fa9e4066Sahrens }
1211fa9e4066Sahrens 
1212fa9e4066Sahrens /*
1213fa9e4066Sahrens  * zpool destroy <pool>
1214fa9e4066Sahrens  *
1215fa9e4066Sahrens  * 	-f	Forcefully unmount any datasets
1216fa9e4066Sahrens  *
1217fa9e4066Sahrens  * Destroy the given pool.  Automatically unmounts any datasets in the pool.
1218fa9e4066Sahrens  */
1219fa9e4066Sahrens int
1220fa9e4066Sahrens zpool_do_destroy(int argc, char **argv)
1221fa9e4066Sahrens {
122299653d4eSeschrock 	boolean_t force = B_FALSE;
1223fa9e4066Sahrens 	int c;
1224fa9e4066Sahrens 	char *pool;
1225fa9e4066Sahrens 	zpool_handle_t *zhp;
1226fa9e4066Sahrens 	int ret;
1227fa9e4066Sahrens 
1228fa9e4066Sahrens 	/* check options */
1229fa9e4066Sahrens 	while ((c = getopt(argc, argv, "f")) != -1) {
1230fa9e4066Sahrens 		switch (c) {
1231fa9e4066Sahrens 		case 'f':
123299653d4eSeschrock 			force = B_TRUE;
1233fa9e4066Sahrens 			break;
1234fa9e4066Sahrens 		case '?':
1235fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1236fa9e4066Sahrens 			    optopt);
123799653d4eSeschrock 			usage(B_FALSE);
1238fa9e4066Sahrens 		}
1239fa9e4066Sahrens 	}
1240fa9e4066Sahrens 
1241fa9e4066Sahrens 	argc -= optind;
1242fa9e4066Sahrens 	argv += optind;
1243fa9e4066Sahrens 
1244fa9e4066Sahrens 	/* check arguments */
1245fa9e4066Sahrens 	if (argc < 1) {
1246fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool argument\n"));
124799653d4eSeschrock 		usage(B_FALSE);
1248fa9e4066Sahrens 	}
1249fa9e4066Sahrens 	if (argc > 1) {
1250fa9e4066Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
125199653d4eSeschrock 		usage(B_FALSE);
1252fa9e4066Sahrens 	}
1253fa9e4066Sahrens 
1254fa9e4066Sahrens 	pool = argv[0];
1255fa9e4066Sahrens 
125699653d4eSeschrock 	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1257fa9e4066Sahrens 		/*
1258fa9e4066Sahrens 		 * As a special case, check for use of '/' in the name, and
1259fa9e4066Sahrens 		 * direct the user to use 'zfs destroy' instead.
1260fa9e4066Sahrens 		 */
1261fa9e4066Sahrens 		if (strchr(pool, '/') != NULL)
1262fa9e4066Sahrens 			(void) fprintf(stderr, gettext("use 'zfs destroy' to "
1263fa9e4066Sahrens 			    "destroy a dataset\n"));
1264fa9e4066Sahrens 		return (1);
1265fa9e4066Sahrens 	}
1266fa9e4066Sahrens 
1267f3861e1aSahl 	if (zpool_disable_datasets(zhp, force) != 0) {
1268fa9e4066Sahrens 		(void) fprintf(stderr, gettext("could not destroy '%s': "
1269fa9e4066Sahrens 		    "could not unmount datasets\n"), zpool_get_name(zhp));
1270fa9e4066Sahrens 		return (1);
1271fa9e4066Sahrens 	}
1272fa9e4066Sahrens 
12734445fffbSMatthew Ahrens 	/* The history must be logged as part of the export */
12744445fffbSMatthew Ahrens 	log_history = B_FALSE;
12754445fffbSMatthew Ahrens 
12764445fffbSMatthew Ahrens 	ret = (zpool_destroy(zhp, history_str) != 0);
1277fa9e4066Sahrens 
1278fa9e4066Sahrens 	zpool_close(zhp);
1279fa9e4066Sahrens 
1280fa9e4066Sahrens 	return (ret);
1281fa9e4066Sahrens }
1282fa9e4066Sahrens 
1283fa9e4066Sahrens /*
1284fa9e4066Sahrens  * zpool export [-f] <pool> ...
1285fa9e4066Sahrens  *
1286fa9e4066Sahrens  *	-f	Forcefully unmount datasets
1287fa9e4066Sahrens  *
1288b1b8ab34Slling  * Export the given pools.  By default, the command will attempt to cleanly
1289fa9e4066Sahrens  * unmount any active datasets within the pool.  If the '-f' flag is specified,
1290fa9e4066Sahrens  * then the datasets will be forcefully unmounted.
1291fa9e4066Sahrens  */
1292fa9e4066Sahrens int
1293fa9e4066Sahrens zpool_do_export(int argc, char **argv)
1294fa9e4066Sahrens {
129599653d4eSeschrock 	boolean_t force = B_FALSE;
1296394ab0cbSGeorge Wilson 	boolean_t hardforce = B_FALSE;
1297fa9e4066Sahrens 	int c;
1298fa9e4066Sahrens 	zpool_handle_t *zhp;
1299fa9e4066Sahrens 	int ret;
1300fa9e4066Sahrens 	int i;
1301fa9e4066Sahrens 
1302fa9e4066Sahrens 	/* check options */
1303394ab0cbSGeorge Wilson 	while ((c = getopt(argc, argv, "fF")) != -1) {
1304fa9e4066Sahrens 		switch (c) {
1305fa9e4066Sahrens 		case 'f':
130699653d4eSeschrock 			force = B_TRUE;
1307fa9e4066Sahrens 			break;
1308394ab0cbSGeorge Wilson 		case 'F':
1309394ab0cbSGeorge Wilson 			hardforce = B_TRUE;
1310394ab0cbSGeorge Wilson 			break;
1311fa9e4066Sahrens 		case '?':
1312fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1313fa9e4066Sahrens 			    optopt);
131499653d4eSeschrock 			usage(B_FALSE);
1315fa9e4066Sahrens 		}
1316fa9e4066Sahrens 	}
1317fa9e4066Sahrens 
1318fa9e4066Sahrens 	argc -= optind;
1319fa9e4066Sahrens 	argv += optind;
1320fa9e4066Sahrens 
1321fa9e4066Sahrens 	/* check arguments */
1322fa9e4066Sahrens 	if (argc < 1) {
1323fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool argument\n"));
132499653d4eSeschrock 		usage(B_FALSE);
1325fa9e4066Sahrens 	}
1326fa9e4066Sahrens 
1327fa9e4066Sahrens 	ret = 0;
1328fa9e4066Sahrens 	for (i = 0; i < argc; i++) {
132999653d4eSeschrock 		if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1330fa9e4066Sahrens 			ret = 1;
1331fa9e4066Sahrens 			continue;
1332fa9e4066Sahrens 		}
1333fa9e4066Sahrens 
1334f3861e1aSahl 		if (zpool_disable_datasets(zhp, force) != 0) {
1335fa9e4066Sahrens 			ret = 1;
1336fa9e4066Sahrens 			zpool_close(zhp);
1337fa9e4066Sahrens 			continue;
1338fa9e4066Sahrens 		}
1339fa9e4066Sahrens 
13404445fffbSMatthew Ahrens 		/* The history must be logged as part of the export */
13414445fffbSMatthew Ahrens 		log_history = B_FALSE;
13424445fffbSMatthew Ahrens 
1343394ab0cbSGeorge Wilson 		if (hardforce) {
13444445fffbSMatthew Ahrens 			if (zpool_export_force(zhp, history_str) != 0)
1345394ab0cbSGeorge Wilson 				ret = 1;
13464445fffbSMatthew Ahrens 		} else if (zpool_export(zhp, force, history_str) != 0) {
1347fa9e4066Sahrens 			ret = 1;
1348394ab0cbSGeorge Wilson 		}
1349fa9e4066Sahrens 
1350fa9e4066Sahrens 		zpool_close(zhp);
1351fa9e4066Sahrens 	}
1352fa9e4066Sahrens 
1353fa9e4066Sahrens 	return (ret);
1354fa9e4066Sahrens }
1355fa9e4066Sahrens 
1356fa9e4066Sahrens /*
1357fa9e4066Sahrens  * Given a vdev configuration, determine the maximum width needed for the device
1358fa9e4066Sahrens  * name column.
1359fa9e4066Sahrens  */
1360fa9e4066Sahrens static int
1361c67d9675Seschrock max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1362fa9e4066Sahrens {
136388ecc943SGeorge Wilson 	char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1364fa9e4066Sahrens 	nvlist_t **child;
1365fa9e4066Sahrens 	uint_t c, children;
1366fa9e4066Sahrens 	int ret;
1367fa9e4066Sahrens 
1368fa9e4066Sahrens 	if (strlen(name) + depth > max)
1369fa9e4066Sahrens 		max = strlen(name) + depth;
1370fa9e4066Sahrens 
1371afefbcddSeschrock 	free(name);
1372afefbcddSeschrock 
137399653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
137499653d4eSeschrock 	    &child, &children) == 0) {
137599653d4eSeschrock 		for (c = 0; c < children; c++)
137699653d4eSeschrock 			if ((ret = max_width(zhp, child[c], depth + 2,
137799653d4eSeschrock 			    max)) > max)
137899653d4eSeschrock 				max = ret;
137999653d4eSeschrock 	}
138099653d4eSeschrock 
1381fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1382fa94a07fSbrendan 	    &child, &children) == 0) {
1383fa94a07fSbrendan 		for (c = 0; c < children; c++)
1384fa94a07fSbrendan 			if ((ret = max_width(zhp, child[c], depth + 2,
1385fa94a07fSbrendan 			    max)) > max)
1386fa94a07fSbrendan 				max = ret;
1387fa94a07fSbrendan 	}
1388fa94a07fSbrendan 
1389fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
139099653d4eSeschrock 	    &child, &children) == 0) {
139199653d4eSeschrock 		for (c = 0; c < children; c++)
139299653d4eSeschrock 			if ((ret = max_width(zhp, child[c], depth + 2,
139399653d4eSeschrock 			    max)) > max)
139499653d4eSeschrock 				max = ret;
139599653d4eSeschrock 	}
1396fa9e4066Sahrens 
1397fa9e4066Sahrens 
1398fa9e4066Sahrens 	return (max);
1399fa9e4066Sahrens }
1400fa9e4066Sahrens 
1401e6ca193dSGeorge Wilson typedef struct spare_cbdata {
1402e6ca193dSGeorge Wilson 	uint64_t	cb_guid;
1403e6ca193dSGeorge Wilson 	zpool_handle_t	*cb_zhp;
1404e6ca193dSGeorge Wilson } spare_cbdata_t;
1405e6ca193dSGeorge Wilson 
1406e6ca193dSGeorge Wilson static boolean_t
1407e6ca193dSGeorge Wilson find_vdev(nvlist_t *nv, uint64_t search)
1408e6ca193dSGeorge Wilson {
1409e6ca193dSGeorge Wilson 	uint64_t guid;
1410e6ca193dSGeorge Wilson 	nvlist_t **child;
1411e6ca193dSGeorge Wilson 	uint_t c, children;
1412e6ca193dSGeorge Wilson 
1413e6ca193dSGeorge Wilson 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1414e6ca193dSGeorge Wilson 	    search == guid)
1415e6ca193dSGeorge Wilson 		return (B_TRUE);
1416e6ca193dSGeorge Wilson 
1417e6ca193dSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1418e6ca193dSGeorge Wilson 	    &child, &children) == 0) {
1419e6ca193dSGeorge Wilson 		for (c = 0; c < children; c++)
1420e6ca193dSGeorge Wilson 			if (find_vdev(child[c], search))
1421e6ca193dSGeorge Wilson 				return (B_TRUE);
1422e6ca193dSGeorge Wilson 	}
1423e6ca193dSGeorge Wilson 
1424e6ca193dSGeorge Wilson 	return (B_FALSE);
1425e6ca193dSGeorge Wilson }
1426e6ca193dSGeorge Wilson 
1427e6ca193dSGeorge Wilson static int
1428e6ca193dSGeorge Wilson find_spare(zpool_handle_t *zhp, void *data)
1429e6ca193dSGeorge Wilson {
1430e6ca193dSGeorge Wilson 	spare_cbdata_t *cbp = data;
1431e6ca193dSGeorge Wilson 	nvlist_t *config, *nvroot;
1432e6ca193dSGeorge Wilson 
1433e6ca193dSGeorge Wilson 	config = zpool_get_config(zhp, NULL);
1434e6ca193dSGeorge Wilson 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1435e6ca193dSGeorge Wilson 	    &nvroot) == 0);
1436e6ca193dSGeorge Wilson 
1437e6ca193dSGeorge Wilson 	if (find_vdev(nvroot, cbp->cb_guid)) {
1438e6ca193dSGeorge Wilson 		cbp->cb_zhp = zhp;
1439e6ca193dSGeorge Wilson 		return (1);
1440e6ca193dSGeorge Wilson 	}
1441e6ca193dSGeorge Wilson 
1442e6ca193dSGeorge Wilson 	zpool_close(zhp);
1443e6ca193dSGeorge Wilson 	return (0);
1444e6ca193dSGeorge Wilson }
1445e6ca193dSGeorge Wilson 
1446e6ca193dSGeorge Wilson /*
1447e6ca193dSGeorge Wilson  * Print out configuration state as requested by status_callback.
1448e6ca193dSGeorge Wilson  */
1449e6ca193dSGeorge Wilson void
1450e6ca193dSGeorge Wilson print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1451e6ca193dSGeorge Wilson     int namewidth, int depth, boolean_t isspare)
1452e6ca193dSGeorge Wilson {
1453e6ca193dSGeorge Wilson 	nvlist_t **child;
1454e6ca193dSGeorge Wilson 	uint_t c, children;
14553f9d6ad7SLin Ling 	pool_scan_stat_t *ps = NULL;
1456e6ca193dSGeorge Wilson 	vdev_stat_t *vs;
14573f9d6ad7SLin Ling 	char rbuf[6], wbuf[6], cbuf[6];
1458e6ca193dSGeorge Wilson 	char *vname;
1459e6ca193dSGeorge Wilson 	uint64_t notpresent;
1460e6ca193dSGeorge Wilson 	spare_cbdata_t cb;
14616401734dSWill Andrews 	const char *state;
14625cabbc6bSPrashanth Sreenivasa 	char *type;
1463e6ca193dSGeorge Wilson 
1464e6ca193dSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1465e6ca193dSGeorge Wilson 	    &child, &children) != 0)
1466e6ca193dSGeorge Wilson 		children = 0;
1467e6ca193dSGeorge Wilson 
14683f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
14693f9d6ad7SLin Ling 	    (uint64_t **)&vs, &c) == 0);
14703f9d6ad7SLin Ling 
14715cabbc6bSPrashanth Sreenivasa 	verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
14725cabbc6bSPrashanth Sreenivasa 
14735cabbc6bSPrashanth Sreenivasa 	if (strcmp(type, VDEV_TYPE_INDIRECT) == 0)
14745cabbc6bSPrashanth Sreenivasa 		return;
14755cabbc6bSPrashanth Sreenivasa 
1476e6ca193dSGeorge Wilson 	state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1477e6ca193dSGeorge Wilson 	if (isspare) {
1478e6ca193dSGeorge Wilson 		/*
1479e6ca193dSGeorge Wilson 		 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1480e6ca193dSGeorge Wilson 		 * online drives.
1481e6ca193dSGeorge Wilson 		 */
1482e6ca193dSGeorge Wilson 		if (vs->vs_aux == VDEV_AUX_SPARED)
1483e6ca193dSGeorge Wilson 			state = "INUSE";
1484e6ca193dSGeorge Wilson 		else if (vs->vs_state == VDEV_STATE_HEALTHY)
1485e6ca193dSGeorge Wilson 			state = "AVAIL";
1486e6ca193dSGeorge Wilson 	}
1487e6ca193dSGeorge Wilson 
1488e6ca193dSGeorge Wilson 	(void) printf("\t%*s%-*s  %-8s", depth, "", namewidth - depth,
1489e6ca193dSGeorge Wilson 	    name, state);
1490e6ca193dSGeorge Wilson 
1491e6ca193dSGeorge Wilson 	if (!isspare) {
1492e6ca193dSGeorge Wilson 		zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1493e6ca193dSGeorge Wilson 		zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1494e6ca193dSGeorge Wilson 		zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1495e6ca193dSGeorge Wilson 		(void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1496e6ca193dSGeorge Wilson 	}
1497e6ca193dSGeorge Wilson 
1498e6ca193dSGeorge Wilson 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1499e6ca193dSGeorge Wilson 	    &notpresent) == 0) {
1500e6ca193dSGeorge Wilson 		char *path;
1501e6ca193dSGeorge Wilson 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1502e6ca193dSGeorge Wilson 		(void) printf("  was %s", path);
1503e6ca193dSGeorge Wilson 	} else if (vs->vs_aux != 0) {
1504e6ca193dSGeorge Wilson 		(void) printf("  ");
1505e6ca193dSGeorge Wilson 
1506e6ca193dSGeorge Wilson 		switch (vs->vs_aux) {
1507e6ca193dSGeorge Wilson 		case VDEV_AUX_OPEN_FAILED:
1508e6ca193dSGeorge Wilson 			(void) printf(gettext("cannot open"));
1509e6ca193dSGeorge Wilson 			break;
1510e6ca193dSGeorge Wilson 
1511e6ca193dSGeorge Wilson 		case VDEV_AUX_BAD_GUID_SUM:
1512e6ca193dSGeorge Wilson 			(void) printf(gettext("missing device"));
1513e6ca193dSGeorge Wilson 			break;
1514e6ca193dSGeorge Wilson 
1515e6ca193dSGeorge Wilson 		case VDEV_AUX_NO_REPLICAS:
1516e6ca193dSGeorge Wilson 			(void) printf(gettext("insufficient replicas"));
1517e6ca193dSGeorge Wilson 			break;
1518e6ca193dSGeorge Wilson 
1519e6ca193dSGeorge Wilson 		case VDEV_AUX_VERSION_NEWER:
1520e6ca193dSGeorge Wilson 			(void) printf(gettext("newer version"));
1521e6ca193dSGeorge Wilson 			break;
1522e6ca193dSGeorge Wilson 
1523ad135b5dSChristopher Siden 		case VDEV_AUX_UNSUP_FEAT:
1524ad135b5dSChristopher Siden 			(void) printf(gettext("unsupported feature(s)"));
1525ad135b5dSChristopher Siden 			break;
1526ad135b5dSChristopher Siden 
1527e6ca193dSGeorge Wilson 		case VDEV_AUX_SPARED:
1528e6ca193dSGeorge Wilson 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1529e6ca193dSGeorge Wilson 			    &cb.cb_guid) == 0);
1530e6ca193dSGeorge Wilson 			if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1531e6ca193dSGeorge Wilson 				if (strcmp(zpool_get_name(cb.cb_zhp),
1532e6ca193dSGeorge Wilson 				    zpool_get_name(zhp)) == 0)
1533e6ca193dSGeorge Wilson 					(void) printf(gettext("currently in "
1534e6ca193dSGeorge Wilson 					    "use"));
1535e6ca193dSGeorge Wilson 				else
1536e6ca193dSGeorge Wilson 					(void) printf(gettext("in use by "
1537e6ca193dSGeorge Wilson 					    "pool '%s'"),
1538e6ca193dSGeorge Wilson 					    zpool_get_name(cb.cb_zhp));
1539e6ca193dSGeorge Wilson 				zpool_close(cb.cb_zhp);
1540e6ca193dSGeorge Wilson 			} else {
1541e6ca193dSGeorge Wilson 				(void) printf(gettext("currently in use"));
1542e6ca193dSGeorge Wilson 			}
1543e6ca193dSGeorge Wilson 			break;
1544e6ca193dSGeorge Wilson 
1545e6ca193dSGeorge Wilson 		case VDEV_AUX_ERR_EXCEEDED:
1546e6ca193dSGeorge Wilson 			(void) printf(gettext("too many errors"));
1547e6ca193dSGeorge Wilson 			break;
1548e6ca193dSGeorge Wilson 
1549e6ca193dSGeorge Wilson 		case VDEV_AUX_IO_FAILURE:
1550e6ca193dSGeorge Wilson 			(void) printf(gettext("experienced I/O failures"));
1551e6ca193dSGeorge Wilson 			break;
1552e6ca193dSGeorge Wilson 
1553e6ca193dSGeorge Wilson 		case VDEV_AUX_BAD_LOG:
1554e6ca193dSGeorge Wilson 			(void) printf(gettext("bad intent log"));
1555e6ca193dSGeorge Wilson 			break;
1556e6ca193dSGeorge Wilson 
1557069f55e2SEric Schrock 		case VDEV_AUX_EXTERNAL:
1558069f55e2SEric Schrock 			(void) printf(gettext("external device fault"));
1559069f55e2SEric Schrock 			break;
1560069f55e2SEric Schrock 
15611195e687SMark J Musante 		case VDEV_AUX_SPLIT_POOL:
15621195e687SMark J Musante 			(void) printf(gettext("split into new pool"));
15631195e687SMark J Musante 			break;
15641195e687SMark J Musante 
1565e6ca193dSGeorge Wilson 		default:
1566e6ca193dSGeorge Wilson 			(void) printf(gettext("corrupted data"));
1567e6ca193dSGeorge Wilson 			break;
1568e6ca193dSGeorge Wilson 		}
15693f9d6ad7SLin Ling 	}
15703f9d6ad7SLin Ling 
15713f9d6ad7SLin Ling 	(void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
15723f9d6ad7SLin Ling 	    (uint64_t **)&ps, &c);
15733f9d6ad7SLin Ling 
15743f9d6ad7SLin Ling 	if (ps && ps->pss_state == DSS_SCANNING &&
15753f9d6ad7SLin Ling 	    vs->vs_scan_processed != 0 && children == 0) {
15763f9d6ad7SLin Ling 		(void) printf(gettext("  (%s)"),
15773f9d6ad7SLin Ling 		    (ps->pss_func == POOL_SCAN_RESILVER) ?
15783f9d6ad7SLin Ling 		    "resilvering" : "repairing");
1579e6ca193dSGeorge Wilson 	}
1580e6ca193dSGeorge Wilson 
1581e6ca193dSGeorge Wilson 	(void) printf("\n");
1582e6ca193dSGeorge Wilson 
1583e6ca193dSGeorge Wilson 	for (c = 0; c < children; c++) {
158488ecc943SGeorge Wilson 		uint64_t islog = B_FALSE, ishole = B_FALSE;
1585e6ca193dSGeorge Wilson 
158688ecc943SGeorge Wilson 		/* Don't print logs or holes here */
1587e6ca193dSGeorge Wilson 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
158888ecc943SGeorge Wilson 		    &islog);
158988ecc943SGeorge Wilson 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
159088ecc943SGeorge Wilson 		    &ishole);
159188ecc943SGeorge Wilson 		if (islog || ishole)
1592e6ca193dSGeorge Wilson 			continue;
159388ecc943SGeorge Wilson 		vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1594e6ca193dSGeorge Wilson 		print_status_config(zhp, vname, child[c],
1595e6ca193dSGeorge Wilson 		    namewidth, depth + 2, isspare);
1596e6ca193dSGeorge Wilson 		free(vname);
1597e6ca193dSGeorge Wilson 	}
1598e6ca193dSGeorge Wilson }
1599e6ca193dSGeorge Wilson 
1600fa9e4066Sahrens 
1601fa9e4066Sahrens /*
1602fa9e4066Sahrens  * Print the configuration of an exported pool.  Iterate over all vdevs in the
1603fa9e4066Sahrens  * pool, printing out the name and status for each one.
1604fa9e4066Sahrens  */
1605fa9e4066Sahrens void
1606e6ca193dSGeorge Wilson print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1607fa9e4066Sahrens {
1608fa9e4066Sahrens 	nvlist_t **child;
1609fa9e4066Sahrens 	uint_t c, children;
1610fa9e4066Sahrens 	vdev_stat_t *vs;
1611afefbcddSeschrock 	char *type, *vname;
1612fa9e4066Sahrens 
1613fa9e4066Sahrens 	verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
161488ecc943SGeorge Wilson 	if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
161588ecc943SGeorge Wilson 	    strcmp(type, VDEV_TYPE_HOLE) == 0)
1616fa9e4066Sahrens 		return;
1617fa9e4066Sahrens 
16183f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1619fa9e4066Sahrens 	    (uint64_t **)&vs, &c) == 0);
1620fa9e4066Sahrens 
1621fa9e4066Sahrens 	(void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1622990b4856Slling 	(void) printf("  %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1623fa9e4066Sahrens 
1624fa9e4066Sahrens 	if (vs->vs_aux != 0) {
16253d7072f8Seschrock 		(void) printf("  ");
1626fa9e4066Sahrens 
1627fa9e4066Sahrens 		switch (vs->vs_aux) {
1628fa9e4066Sahrens 		case VDEV_AUX_OPEN_FAILED:
1629fa9e4066Sahrens 			(void) printf(gettext("cannot open"));
1630fa9e4066Sahrens 			break;
1631fa9e4066Sahrens 
1632fa9e4066Sahrens 		case VDEV_AUX_BAD_GUID_SUM:
1633fa9e4066Sahrens 			(void) printf(gettext("missing device"));
1634fa9e4066Sahrens 			break;
1635fa9e4066Sahrens 
1636fa9e4066Sahrens 		case VDEV_AUX_NO_REPLICAS:
1637fa9e4066Sahrens 			(void) printf(gettext("insufficient replicas"));
1638fa9e4066Sahrens 			break;
1639fa9e4066Sahrens 
1640eaca9bbdSeschrock 		case VDEV_AUX_VERSION_NEWER:
1641eaca9bbdSeschrock 			(void) printf(gettext("newer version"));
1642eaca9bbdSeschrock 			break;
1643eaca9bbdSeschrock 
1644ad135b5dSChristopher Siden 		case VDEV_AUX_UNSUP_FEAT:
1645ad135b5dSChristopher Siden 			(void) printf(gettext("unsupported feature(s)"));
1646ad135b5dSChristopher Siden 			break;
1647ad135b5dSChristopher Siden 
16483d7072f8Seschrock 		case VDEV_AUX_ERR_EXCEEDED:
16493d7072f8Seschrock 			(void) printf(gettext("too many errors"));
16503d7072f8Seschrock 			break;
16513d7072f8Seschrock 
1652fa9e4066Sahrens 		default:
1653fa9e4066Sahrens 			(void) printf(gettext("corrupted data"));
1654fa9e4066Sahrens 			break;
1655fa9e4066Sahrens 		}
1656fa9e4066Sahrens 	}
1657fa9e4066Sahrens 	(void) printf("\n");
1658fa9e4066Sahrens 
1659fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1660fa9e4066Sahrens 	    &child, &children) != 0)
1661fa9e4066Sahrens 		return;
1662fa9e4066Sahrens 
1663afefbcddSeschrock 	for (c = 0; c < children; c++) {
16648654d025Sperrin 		uint64_t is_log = B_FALSE;
16658654d025Sperrin 
16668654d025Sperrin 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
16678654d025Sperrin 		    &is_log);
1668e6ca193dSGeorge Wilson 		if (is_log)
16698654d025Sperrin 			continue;
16708654d025Sperrin 
167188ecc943SGeorge Wilson 		vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1672e6ca193dSGeorge Wilson 		print_import_config(vname, child[c], namewidth, depth + 2);
1673afefbcddSeschrock 		free(vname);
1674afefbcddSeschrock 	}
167599653d4eSeschrock 
1676fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1677fa94a07fSbrendan 	    &child, &children) == 0) {
1678fa94a07fSbrendan 		(void) printf(gettext("\tcache\n"));
1679fa94a07fSbrendan 		for (c = 0; c < children; c++) {
168088ecc943SGeorge Wilson 			vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1681fa94a07fSbrendan 			(void) printf("\t  %s\n", vname);
1682fa94a07fSbrendan 			free(vname);
1683fa94a07fSbrendan 		}
1684fa94a07fSbrendan 	}
168599653d4eSeschrock 
1686fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1687fa94a07fSbrendan 	    &child, &children) == 0) {
1688fa94a07fSbrendan 		(void) printf(gettext("\tspares\n"));
1689fa94a07fSbrendan 		for (c = 0; c < children; c++) {
169088ecc943SGeorge Wilson 			vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1691fa94a07fSbrendan 			(void) printf("\t  %s\n", vname);
1692fa94a07fSbrendan 			free(vname);
1693fa94a07fSbrendan 		}
169499653d4eSeschrock 	}
1695fa9e4066Sahrens }
1696fa9e4066Sahrens 
1697e6ca193dSGeorge Wilson /*
1698e6ca193dSGeorge Wilson  * Print log vdevs.
1699e6ca193dSGeorge Wilson  * Logs are recorded as top level vdevs in the main pool child array
1700e6ca193dSGeorge Wilson  * but with "is_log" set to 1. We use either print_status_config() or
1701e6ca193dSGeorge Wilson  * print_import_config() to print the top level logs then any log
1702e6ca193dSGeorge Wilson  * children (eg mirrored slogs) are printed recursively - which
1703e6ca193dSGeorge Wilson  * works because only the top level vdev is marked "is_log"
1704e6ca193dSGeorge Wilson  */
1705e6ca193dSGeorge Wilson static void
1706e6ca193dSGeorge Wilson print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1707e6ca193dSGeorge Wilson {
1708e6ca193dSGeorge Wilson 	uint_t c, children;
1709e6ca193dSGeorge Wilson 	nvlist_t **child;
1710e6ca193dSGeorge Wilson 
1711e6ca193dSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1712e6ca193dSGeorge Wilson 	    &children) != 0)
1713e6ca193dSGeorge Wilson 		return;
1714e6ca193dSGeorge Wilson 
1715e6ca193dSGeorge Wilson 	(void) printf(gettext("\tlogs\n"));
1716e6ca193dSGeorge Wilson 
1717e6ca193dSGeorge Wilson 	for (c = 0; c < children; c++) {
1718e6ca193dSGeorge Wilson 		uint64_t is_log = B_FALSE;
1719e6ca193dSGeorge Wilson 		char *name;
1720e6ca193dSGeorge Wilson 
1721e6ca193dSGeorge Wilson 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1722e6ca193dSGeorge Wilson 		    &is_log);
1723e6ca193dSGeorge Wilson 		if (!is_log)
1724e6ca193dSGeorge Wilson 			continue;
172588ecc943SGeorge Wilson 		name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1726e6ca193dSGeorge Wilson 		if (verbose)
1727e6ca193dSGeorge Wilson 			print_status_config(zhp, name, child[c], namewidth,
1728e6ca193dSGeorge Wilson 			    2, B_FALSE);
1729e6ca193dSGeorge Wilson 		else
1730e6ca193dSGeorge Wilson 			print_import_config(name, child[c], namewidth, 2);
1731e6ca193dSGeorge Wilson 		free(name);
1732e6ca193dSGeorge Wilson 	}
1733e6ca193dSGeorge Wilson }
1734468c413aSTim Haley 
1735fa9e4066Sahrens /*
1736fa9e4066Sahrens  * Display the status for the given pool.
1737fa9e4066Sahrens  */
1738fa9e4066Sahrens static void
1739fa9e4066Sahrens show_import(nvlist_t *config)
1740fa9e4066Sahrens {
1741fa9e4066Sahrens 	uint64_t pool_state;
1742fa9e4066Sahrens 	vdev_stat_t *vs;
1743fa9e4066Sahrens 	char *name;
1744fa9e4066Sahrens 	uint64_t guid;
1745fa9e4066Sahrens 	char *msgid;
1746fa9e4066Sahrens 	nvlist_t *nvroot;
1747fa9e4066Sahrens 	int reason;
174846657f8dSmmusante 	const char *health;
1749fa9e4066Sahrens 	uint_t vsc;
1750fa9e4066Sahrens 	int namewidth;
17518704186eSDan McDonald 	char *comment;
1752fa9e4066Sahrens 
1753fa9e4066Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1754fa9e4066Sahrens 	    &name) == 0);
1755fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1756fa9e4066Sahrens 	    &guid) == 0);
1757fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1758fa9e4066Sahrens 	    &pool_state) == 0);
1759fa9e4066Sahrens 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1760fa9e4066Sahrens 	    &nvroot) == 0);
1761fa9e4066Sahrens 
17623f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1763fa9e4066Sahrens 	    (uint64_t **)&vs, &vsc) == 0);
1764990b4856Slling 	health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1765fa9e4066Sahrens 
1766fa9e4066Sahrens 	reason = zpool_import_status(config, &msgid);
1767fa9e4066Sahrens 
17688704186eSDan McDonald 	(void) printf(gettext("   pool: %s\n"), name);
17698704186eSDan McDonald 	(void) printf(gettext("     id: %llu\n"), (u_longlong_t)guid);
17708704186eSDan McDonald 	(void) printf(gettext("  state: %s"), health);
17714c58d714Sdarrenm 	if (pool_state == POOL_STATE_DESTROYED)
1772b1b8ab34Slling 		(void) printf(gettext(" (DESTROYED)"));
17734c58d714Sdarrenm 	(void) printf("\n");
1774fa9e4066Sahrens 
1775fa9e4066Sahrens 	switch (reason) {
1776fa9e4066Sahrens 	case ZPOOL_STATUS_MISSING_DEV_R:
1777fa9e4066Sahrens 	case ZPOOL_STATUS_MISSING_DEV_NR:
1778fa9e4066Sahrens 	case ZPOOL_STATUS_BAD_GUID_SUM:
17798704186eSDan McDonald 		(void) printf(gettext(" status: One or more devices are "
17808704186eSDan McDonald 		    "missing from the system.\n"));
1781fa9e4066Sahrens 		break;
1782fa9e4066Sahrens 
1783fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_LABEL_R:
1784fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_LABEL_NR:
17858704186eSDan McDonald 		(void) printf(gettext(" status: One or more devices contains "
1786fa9e4066Sahrens 		    "corrupted data.\n"));
1787fa9e4066Sahrens 		break;
1788fa9e4066Sahrens 
1789fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_DATA:
17908704186eSDan McDonald 		(void) printf(
17918704186eSDan McDonald 		    gettext(" status: The pool data is corrupted.\n"));
1792fa9e4066Sahrens 		break;
1793fa9e4066Sahrens 
1794441d80aaSlling 	case ZPOOL_STATUS_OFFLINE_DEV:
17958704186eSDan McDonald 		(void) printf(gettext(" status: One or more devices "
1796441d80aaSlling 		    "are offlined.\n"));
1797441d80aaSlling 		break;
1798441d80aaSlling 
1799ea8dc4b6Seschrock 	case ZPOOL_STATUS_CORRUPT_POOL:
18008704186eSDan McDonald 		(void) printf(gettext(" status: The pool metadata is "
1801ea8dc4b6Seschrock 		    "corrupted.\n"));
1802ea8dc4b6Seschrock 		break;
1803ea8dc4b6Seschrock 
1804eaca9bbdSeschrock 	case ZPOOL_STATUS_VERSION_OLDER:
180557221772SChristopher Siden 		(void) printf(gettext(" status: The pool is formatted using a "
180657221772SChristopher Siden 		    "legacy on-disk version.\n"));
1807eaca9bbdSeschrock 		break;
1808eaca9bbdSeschrock 
1809eaca9bbdSeschrock 	case ZPOOL_STATUS_VERSION_NEWER:
18108704186eSDan McDonald 		(void) printf(gettext(" status: The pool is formatted using an "
1811eaca9bbdSeschrock 		    "incompatible version.\n"));
1812eaca9bbdSeschrock 		break;
1813b87f3af3Sperrin 
181457221772SChristopher Siden 	case ZPOOL_STATUS_FEAT_DISABLED:
181557221772SChristopher Siden 		(void) printf(gettext(" status: Some supported features are "
181657221772SChristopher Siden 		    "not enabled on the pool.\n"));
181757221772SChristopher Siden 		break;
181857221772SChristopher Siden 
1819ad135b5dSChristopher Siden 	case ZPOOL_STATUS_UNSUP_FEAT_READ:
1820ad135b5dSChristopher Siden 		(void) printf(gettext("status: The pool uses the following "
1821ad135b5dSChristopher Siden 		    "feature(s) not supported on this sytem:\n"));
1822ad135b5dSChristopher Siden 		zpool_print_unsup_feat(config);
1823ad135b5dSChristopher Siden 		break;
1824ad135b5dSChristopher Siden 
1825ad135b5dSChristopher Siden 	case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1826ad135b5dSChristopher Siden 		(void) printf(gettext("status: The pool can only be accessed "
1827ad135b5dSChristopher Siden 		    "in read-only mode on this system. It\n\tcannot be "
1828ad135b5dSChristopher Siden 		    "accessed in read-write mode because it uses the "
1829ad135b5dSChristopher Siden 		    "following\n\tfeature(s) not supported on this system:\n"));
1830ad135b5dSChristopher Siden 		zpool_print_unsup_feat(config);
1831ad135b5dSChristopher Siden 		break;
1832ad135b5dSChristopher Siden 
183395173954Sek 	case ZPOOL_STATUS_HOSTID_MISMATCH:
18348704186eSDan McDonald 		(void) printf(gettext(" status: The pool was last accessed by "
183595173954Sek 		    "another system.\n"));
183695173954Sek 		break;
1837b87f3af3Sperrin 
18383d7072f8Seschrock 	case ZPOOL_STATUS_FAULTED_DEV_R:
18393d7072f8Seschrock 	case ZPOOL_STATUS_FAULTED_DEV_NR:
18408704186eSDan McDonald 		(void) printf(gettext(" status: One or more devices are "
18413d7072f8Seschrock 		    "faulted.\n"));
18423d7072f8Seschrock 		break;
18433d7072f8Seschrock 
1844b87f3af3Sperrin 	case ZPOOL_STATUS_BAD_LOG:
18458704186eSDan McDonald 		(void) printf(gettext(" status: An intent log record cannot be "
1846b87f3af3Sperrin 		    "read.\n"));
1847b87f3af3Sperrin 		break;
1848b87f3af3Sperrin 
18493f9d6ad7SLin Ling 	case ZPOOL_STATUS_RESILVERING:
18508704186eSDan McDonald 		(void) printf(gettext(" status: One or more devices were being "
18513f9d6ad7SLin Ling 		    "resilvered.\n"));
18523f9d6ad7SLin Ling 		break;
18533f9d6ad7SLin Ling 
1854fa9e4066Sahrens 	default:
1855fa9e4066Sahrens 		/*
1856fa9e4066Sahrens 		 * No other status can be seen when importing pools.
1857fa9e4066Sahrens 		 */
1858fa9e4066Sahrens 		assert(reason == ZPOOL_STATUS_OK);
1859fa9e4066Sahrens 	}
1860fa9e4066Sahrens 
1861fa9e4066Sahrens 	/*
1862fa9e4066Sahrens 	 * Print out an action according to the overall state of the pool.
1863fa9e4066Sahrens 	 */
186446657f8dSmmusante 	if (vs->vs_state == VDEV_STATE_HEALTHY) {
186557221772SChristopher Siden 		if (reason == ZPOOL_STATUS_VERSION_OLDER ||
186657221772SChristopher Siden 		    reason == ZPOOL_STATUS_FEAT_DISABLED) {
18678704186eSDan McDonald 			(void) printf(gettext(" action: The pool can be "
1868eaca9bbdSeschrock 			    "imported using its name or numeric identifier, "
1869eaca9bbdSeschrock 			    "though\n\tsome features will not be available "
1870eaca9bbdSeschrock 			    "without an explicit 'zpool upgrade'.\n"));
187157221772SChristopher Siden 		} else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
18728704186eSDan McDonald 			(void) printf(gettext(" action: The pool can be "
187395173954Sek 			    "imported using its name or numeric "
187495173954Sek 			    "identifier and\n\tthe '-f' flag.\n"));
187557221772SChristopher Siden 		} else {
18768704186eSDan McDonald 			(void) printf(gettext(" action: The pool can be "
1877eaca9bbdSeschrock 			    "imported using its name or numeric "
1878eaca9bbdSeschrock 			    "identifier.\n"));
187957221772SChristopher Siden 		}
188046657f8dSmmusante 	} else if (vs->vs_state == VDEV_STATE_DEGRADED) {
18818704186eSDan McDonald 		(void) printf(gettext(" action: The pool can be imported "
1882fa9e4066Sahrens 		    "despite missing or damaged devices.  The\n\tfault "
1883eaca9bbdSeschrock 		    "tolerance of the pool may be compromised if imported.\n"));
1884fa9e4066Sahrens 	} else {
1885eaca9bbdSeschrock 		switch (reason) {
1886eaca9bbdSeschrock 		case ZPOOL_STATUS_VERSION_NEWER:
18878704186eSDan McDonald 			(void) printf(gettext(" action: The pool cannot be "
1888eaca9bbdSeschrock 			    "imported.  Access the pool on a system running "
1889eaca9bbdSeschrock 			    "newer\n\tsoftware, or recreate the pool from "
1890eaca9bbdSeschrock 			    "backup.\n"));
1891eaca9bbdSeschrock 			break;
1892ad135b5dSChristopher Siden 		case ZPOOL_STATUS_UNSUP_FEAT_READ:
1893ad135b5dSChristopher Siden 			(void) printf(gettext("action: The pool cannot be "
1894ad135b5dSChristopher Siden 			    "imported. Access the pool on a system that "
1895ad135b5dSChristopher Siden 			    "supports\n\tthe required feature(s), or recreate "
1896ad135b5dSChristopher Siden 			    "the pool from backup.\n"));
1897ad135b5dSChristopher Siden 			break;
1898ad135b5dSChristopher Siden 		case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1899ad135b5dSChristopher Siden 			(void) printf(gettext("action: The pool cannot be "
1900ad135b5dSChristopher Siden 			    "imported in read-write mode. Import the pool "
1901ad135b5dSChristopher Siden 			    "with\n"
1902ad135b5dSChristopher Siden 			    "\t\"-o readonly=on\", access the pool on a system "
1903ad135b5dSChristopher Siden 			    "that supports the\n\trequired feature(s), or "
1904ad135b5dSChristopher Siden 			    "recreate the pool from backup.\n"));
1905ad135b5dSChristopher Siden 			break;
1906eaca9bbdSeschrock 		case ZPOOL_STATUS_MISSING_DEV_R:
1907eaca9bbdSeschrock 		case ZPOOL_STATUS_MISSING_DEV_NR:
1908eaca9bbdSeschrock 		case ZPOOL_STATUS_BAD_GUID_SUM:
19098704186eSDan McDonald 			(void) printf(gettext(" action: The pool cannot be "
1910fa9e4066Sahrens 			    "imported. Attach the missing\n\tdevices and try "
1911fa9e4066Sahrens 			    "again.\n"));
1912eaca9bbdSeschrock 			break;
1913eaca9bbdSeschrock 		default:
19148704186eSDan McDonald 			(void) printf(gettext(" action: The pool cannot be "
1915fa9e4066Sahrens 			    "imported due to damaged devices or data.\n"));
1916eaca9bbdSeschrock 		}
1917eaca9bbdSeschrock 	}
1918eaca9bbdSeschrock 
19198704186eSDan McDonald 	/* Print the comment attached to the pool. */
19208704186eSDan McDonald 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
19218704186eSDan McDonald 		(void) printf(gettext("comment: %s\n"), comment);
19228704186eSDan McDonald 
192346657f8dSmmusante 	/*
192446657f8dSmmusante 	 * If the state is "closed" or "can't open", and the aux state
192546657f8dSmmusante 	 * is "corrupt data":
192646657f8dSmmusante 	 */
192746657f8dSmmusante 	if (((vs->vs_state == VDEV_STATE_CLOSED) ||
192846657f8dSmmusante 	    (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
192946657f8dSmmusante 	    (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1930eaca9bbdSeschrock 		if (pool_state == POOL_STATE_DESTROYED)
1931eaca9bbdSeschrock 			(void) printf(gettext("\tThe pool was destroyed, "
1932eaca9bbdSeschrock 			    "but can be imported using the '-Df' flags.\n"));
1933eaca9bbdSeschrock 		else if (pool_state != POOL_STATE_EXPORTED)
1934eaca9bbdSeschrock 			(void) printf(gettext("\tThe pool may be active on "
193518ce54dfSek 			    "another system, but can be imported using\n\t"
1936eaca9bbdSeschrock 			    "the '-f' flag.\n"));
1937fa9e4066Sahrens 	}
1938fa9e4066Sahrens 
1939fa9e4066Sahrens 	if (msgid != NULL)
1940654b400cSJoshua M. Clulow 		(void) printf(gettext("   see: http://illumos.org/msg/%s\n"),
1941fa9e4066Sahrens 		    msgid);
1942fa9e4066Sahrens 
19438704186eSDan McDonald 	(void) printf(gettext(" config:\n\n"));
1944fa9e4066Sahrens 
1945c67d9675Seschrock 	namewidth = max_width(NULL, nvroot, 0, 0);
1946fa9e4066Sahrens 	if (namewidth < 10)
1947fa9e4066Sahrens 		namewidth = 10;
19488654d025Sperrin 
1949e6ca193dSGeorge Wilson 	print_import_config(name, nvroot, namewidth, 0);
1950e6ca193dSGeorge Wilson 	if (num_logs(nvroot) > 0)
1951e6ca193dSGeorge Wilson 		print_logs(NULL, nvroot, namewidth, B_FALSE);
1952fa9e4066Sahrens 
1953fa9e4066Sahrens 	if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
195446657f8dSmmusante 		(void) printf(gettext("\n\tAdditional devices are known to "
1955fa9e4066Sahrens 		    "be part of this pool, though their\n\texact "
195646657f8dSmmusante 		    "configuration cannot be determined.\n"));
1957fa9e4066Sahrens 	}
1958fa9e4066Sahrens }
1959fa9e4066Sahrens 
1960fa9e4066Sahrens /*
1961fa9e4066Sahrens  * Perform the import for the given configuration.  This passes the heavy
1962990b4856Slling  * lifting off to zpool_import_props(), and then mounts the datasets contained
1963990b4856Slling  * within the pool.
1964fa9e4066Sahrens  */
1965fa9e4066Sahrens static int
1966fa9e4066Sahrens do_import(nvlist_t *config, const char *newname, const char *mntopts,
19674b964adaSGeorge Wilson     nvlist_t *props, int flags)
1968fa9e4066Sahrens {
1969fa9e4066Sahrens 	zpool_handle_t *zhp;
1970fa9e4066Sahrens 	char *name;
1971fa9e4066Sahrens 	uint64_t state;
1972eaca9bbdSeschrock 	uint64_t version;
1973fa9e4066Sahrens 
1974fa9e4066Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1975fa9e4066Sahrens 	    &name) == 0);
1976fa9e4066Sahrens 
1977fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config,
1978fa9e4066Sahrens 	    ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1979eaca9bbdSeschrock 	verify(nvlist_lookup_uint64(config,
1980eaca9bbdSeschrock 	    ZPOOL_CONFIG_VERSION, &version) == 0);
1981ad135b5dSChristopher Siden 	if (!SPA_VERSION_IS_SUPPORTED(version)) {
1982eaca9bbdSeschrock 		(void) fprintf(stderr, gettext("cannot import '%s': pool "
1983ad135b5dSChristopher Siden 		    "is formatted using an unsupported ZFS version\n"), name);
1984eaca9bbdSeschrock 		return (1);
19854b964adaSGeorge Wilson 	} else if (state != POOL_STATE_EXPORTED &&
19864b964adaSGeorge Wilson 	    !(flags & ZFS_IMPORT_ANY_HOST)) {
198795173954Sek 		uint64_t hostid;
198895173954Sek 
198995173954Sek 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
199095173954Sek 		    &hostid) == 0) {
199195173954Sek 			if ((unsigned long)hostid != gethostid()) {
199295173954Sek 				char *hostname;
199395173954Sek 				uint64_t timestamp;
199495173954Sek 				time_t t;
199595173954Sek 
199695173954Sek 				verify(nvlist_lookup_string(config,
199795173954Sek 				    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
199895173954Sek 				verify(nvlist_lookup_uint64(config,
199995173954Sek 				    ZPOOL_CONFIG_TIMESTAMP, &timestamp) == 0);
200095173954Sek 				t = timestamp;
200195173954Sek 				(void) fprintf(stderr, gettext("cannot import "
200295173954Sek 				    "'%s': pool may be in use from other "
200395173954Sek 				    "system, it was last accessed by %s "
200495173954Sek 				    "(hostid: 0x%lx) on %s"), name, hostname,
200595173954Sek 				    (unsigned long)hostid,
200695173954Sek 				    asctime(localtime(&t)));
200795173954Sek 				(void) fprintf(stderr, gettext("use '-f' to "
200895173954Sek 				    "import anyway\n"));
200995173954Sek 				return (1);
201095173954Sek 			}
201195173954Sek 		} else {
201295173954Sek 			(void) fprintf(stderr, gettext("cannot import '%s': "
201395173954Sek 			    "pool may be in use from other system\n"), name);
201495173954Sek 			(void) fprintf(stderr, gettext("use '-f' to import "
201595173954Sek 			    "anyway\n"));
201695173954Sek 			return (1);
201795173954Sek 		}
2018fa9e4066Sahrens 	}
2019fa9e4066Sahrens 
20204b964adaSGeorge Wilson 	if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
2021fa9e4066Sahrens 		return (1);
2022fa9e4066Sahrens 
2023fa9e4066Sahrens 	if (newname != NULL)
2024fa9e4066Sahrens 		name = (char *)newname;
2025fa9e4066Sahrens 
20264f0f5e5bSVictor Latushkin 	if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
20274f0f5e5bSVictor Latushkin 		return (1);
2028fa9e4066Sahrens 
2029379c004dSEric Schrock 	if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
2030f9af39baSGeorge Wilson 	    !(flags & ZFS_IMPORT_ONLY) &&
2031379c004dSEric Schrock 	    zpool_enable_datasets(zhp, mntopts, 0) != 0) {
2032fa9e4066Sahrens 		zpool_close(zhp);
2033fa9e4066Sahrens 		return (1);
2034fa9e4066Sahrens 	}
2035fa9e4066Sahrens 
2036fa9e4066Sahrens 	zpool_close(zhp);
2037468c413aSTim Haley 	return (0);
2038fa9e4066Sahrens }
2039fa9e4066Sahrens 
2040fa9e4066Sahrens /*
20414c58d714Sdarrenm  * zpool import [-d dir] [-D]
20422f8aaab3Seschrock  *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
20432f8aaab3Seschrock  *              [-d dir | -c cachefile] [-f] -a
20442f8aaab3Seschrock  *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2045468c413aSTim Haley  *              [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
20462f8aaab3Seschrock  *
20472f8aaab3Seschrock  *	 -c	Read pool information from a cachefile instead of searching
20482f8aaab3Seschrock  *		devices.
2049fa9e4066Sahrens  *
2050fa9e4066Sahrens  *       -d	Scan in a specific directory, other than /dev/dsk.  More than
2051fa9e4066Sahrens  *		one directory can be specified using multiple '-d' options.
2052fa9e4066Sahrens  *
20534c58d714Sdarrenm  *       -D     Scan for previously destroyed pools or import all or only
20544c58d714Sdarrenm  *              specified destroyed pools.
20554c58d714Sdarrenm  *
2056fa9e4066Sahrens  *       -R	Temporarily import the pool, with all mountpoints relative to
2057fa9e4066Sahrens  *		the given root.  The pool will remain exported when the machine
2058fa9e4066Sahrens  *		is rebooted.
2059fa9e4066Sahrens  *
2060468c413aSTim Haley  *       -V	Import even in the presence of faulted vdevs.  This is an
2061c5904d13Seschrock  *       	intentionally undocumented option for testing purposes, and
2062c5904d13Seschrock  *       	treats the pool configuration as complete, leaving any bad
20634f0f5e5bSVictor Latushkin  *		vdevs in the FAULTED state. In other words, it does verbatim
20644f0f5e5bSVictor Latushkin  *		import.
2065c5904d13Seschrock  *
2066468c413aSTim Haley  *       -f	Force import, even if it appears that the pool is active.
2067468c413aSTim Haley  *
2068468c413aSTim Haley  *       -F     Attempt rewind if necessary.
2069468c413aSTim Haley  *
2070468c413aSTim Haley  *       -n     See if rewind would work, but don't actually rewind.
2071468c413aSTim Haley  *
2072f9af39baSGeorge Wilson  *       -N     Import the pool but don't mount datasets.
2073f9af39baSGeorge Wilson  *
2074f9af39baSGeorge Wilson  *       -T     Specify a starting txg to use for import. This option is
2075f9af39baSGeorge Wilson  *       	intentionally undocumented option for testing purposes.
2076f9af39baSGeorge Wilson  *
2077fa9e4066Sahrens  *       -a	Import all pools found.
2078fa9e4066Sahrens  *
2079990b4856Slling  *       -o	Set property=value and/or temporary mount options (without '=').
2080ecd6cf80Smarks  *
2081fa9e4066Sahrens  * The import command scans for pools to import, and import pools based on pool
2082fa9e4066Sahrens  * name and GUID.  The pool can also be renamed as part of the import process.
2083fa9e4066Sahrens  */
2084fa9e4066Sahrens int
2085fa9e4066Sahrens zpool_do_import(int argc, char **argv)
2086fa9e4066Sahrens {
2087fa9e4066Sahrens 	char **searchdirs = NULL;
2088fa9e4066Sahrens 	int nsearch = 0;
2089fa9e4066Sahrens 	int c;
2090d41c4376SMark J Musante 	int err = 0;
20912f8aaab3Seschrock 	nvlist_t *pools = NULL;
209299653d4eSeschrock 	boolean_t do_all = B_FALSE;
209399653d4eSeschrock 	boolean_t do_destroyed = B_FALSE;
2094fa9e4066Sahrens 	char *mntopts = NULL;
2095fa9e4066Sahrens 	nvpair_t *elem;
2096fa9e4066Sahrens 	nvlist_t *config;
209724e697d4Sck 	uint64_t searchguid = 0;
209824e697d4Sck 	char *searchname = NULL;
2099990b4856Slling 	char *propval;
2100fa9e4066Sahrens 	nvlist_t *found_config;
2101468c413aSTim Haley 	nvlist_t *policy = NULL;
2102ecd6cf80Smarks 	nvlist_t *props = NULL;
210399653d4eSeschrock 	boolean_t first;
21044b964adaSGeorge Wilson 	int flags = ZFS_IMPORT_NORMAL;
2105468c413aSTim Haley 	uint32_t rewind_policy = ZPOOL_NO_REWIND;
2106468c413aSTim Haley 	boolean_t dryrun = B_FALSE;
2107468c413aSTim Haley 	boolean_t do_rewind = B_FALSE;
2108468c413aSTim Haley 	boolean_t xtreme_rewind = B_FALSE;
2109f9af39baSGeorge Wilson 	uint64_t pool_state, txg = -1ULL;
21102f8aaab3Seschrock 	char *cachefile = NULL;
2111d41c4376SMark J Musante 	importargs_t idata = { 0 };
2112f9af39baSGeorge Wilson 	char *endptr;
2113fa9e4066Sahrens 
2114fa9e4066Sahrens 	/* check options */
2115f9af39baSGeorge Wilson 	while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
2116fa9e4066Sahrens 		switch (c) {
2117fa9e4066Sahrens 		case 'a':
211899653d4eSeschrock 			do_all = B_TRUE;
2119fa9e4066Sahrens 			break;
21202f8aaab3Seschrock 		case 'c':
21212f8aaab3Seschrock 			cachefile = optarg;
21222f8aaab3Seschrock 			break;
2123fa9e4066Sahrens 		case 'd':
2124fa9e4066Sahrens 			if (searchdirs == NULL) {
2125fa9e4066Sahrens 				searchdirs = safe_malloc(sizeof (char *));
2126fa9e4066Sahrens 			} else {
2127fa9e4066Sahrens 				char **tmp = safe_malloc((nsearch + 1) *
2128fa9e4066Sahrens 				    sizeof (char *));
2129fa9e4066Sahrens 				bcopy(searchdirs, tmp, nsearch *
2130fa9e4066Sahrens 				    sizeof (char *));
2131fa9e4066Sahrens 				free(searchdirs);
2132fa9e4066Sahrens 				searchdirs = tmp;
2133fa9e4066Sahrens 			}
2134fa9e4066Sahrens 			searchdirs[nsearch++] = optarg;
2135fa9e4066Sahrens 			break;
21364c58d714Sdarrenm 		case 'D':
213799653d4eSeschrock 			do_destroyed = B_TRUE;
21384c58d714Sdarrenm 			break;
2139fa9e4066Sahrens 		case 'f':
21404b964adaSGeorge Wilson 			flags |= ZFS_IMPORT_ANY_HOST;
2141fa9e4066Sahrens 			break;
2142c5904d13Seschrock 		case 'F':
2143468c413aSTim Haley 			do_rewind = B_TRUE;
2144468c413aSTim Haley 			break;
21454b964adaSGeorge Wilson 		case 'm':
21464b964adaSGeorge Wilson 			flags |= ZFS_IMPORT_MISSING_LOG;
21474b964adaSGeorge Wilson 			break;
2148468c413aSTim Haley 		case 'n':
2149468c413aSTim Haley 			dryrun = B_TRUE;
2150c5904d13Seschrock 			break;
2151f9af39baSGeorge Wilson 		case 'N':
2152f9af39baSGeorge Wilson 			flags |= ZFS_IMPORT_ONLY;
2153f9af39baSGeorge Wilson 			break;
2154fa9e4066Sahrens 		case 'o':
2155990b4856Slling 			if ((propval = strchr(optarg, '=')) != NULL) {
2156990b4856Slling 				*propval = '\0';
2157990b4856Slling 				propval++;
21580a48a24eStimh 				if (add_prop_list(optarg, propval,
21590a48a24eStimh 				    &props, B_TRUE))
2160990b4856Slling 					goto error;
2161990b4856Slling 			} else {
2162990b4856Slling 				mntopts = optarg;
2163990b4856Slling 			}
2164fa9e4066Sahrens 			break;
2165fa9e4066Sahrens 		case 'R':
2166990b4856Slling 			if (add_prop_list(zpool_prop_to_name(
21670a48a24eStimh 			    ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2168990b4856Slling 				goto error;
21692f8aaab3Seschrock 			if (nvlist_lookup_string(props,
21702f8aaab3Seschrock 			    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
21712f8aaab3Seschrock 			    &propval) == 0)
21722f8aaab3Seschrock 				break;
2173990b4856Slling 			if (add_prop_list(zpool_prop_to_name(
21740a48a24eStimh 			    ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2175990b4856Slling 				goto error;
2176fa9e4066Sahrens 			break;
2177f9af39baSGeorge Wilson 		case 'T':
2178f9af39baSGeorge Wilson 			errno = 0;
2179e42d2059SMatthew Ahrens 			txg = strtoull(optarg, &endptr, 0);
2180f9af39baSGeorge Wilson 			if (errno != 0 || *endptr != '\0') {
2181f9af39baSGeorge Wilson 				(void) fprintf(stderr,
2182f9af39baSGeorge Wilson 				    gettext("invalid txg value\n"));
2183f9af39baSGeorge Wilson 				usage(B_FALSE);
2184f9af39baSGeorge Wilson 			}
2185f9af39baSGeorge Wilson 			rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2186f9af39baSGeorge Wilson 			break;
2187468c413aSTim Haley 		case 'V':
21884b964adaSGeorge Wilson 			flags |= ZFS_IMPORT_VERBATIM;
2189468c413aSTim Haley 			break;
2190468c413aSTim Haley 		case 'X':
2191468c413aSTim Haley 			xtreme_rewind = B_TRUE;
2192468c413aSTim Haley 			break;
2193fa9e4066Sahrens 		case ':':
2194fa9e4066Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
2195fa9e4066Sahrens 			    "'%c' option\n"), optopt);
219699653d4eSeschrock 			usage(B_FALSE);
2197fa9e4066Sahrens 			break;
2198fa9e4066Sahrens 		case '?':
2199fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2200fa9e4066Sahrens 			    optopt);
220199653d4eSeschrock 			usage(B_FALSE);
2202fa9e4066Sahrens 		}
2203fa9e4066Sahrens 	}
2204fa9e4066Sahrens 
2205fa9e4066Sahrens 	argc -= optind;
2206fa9e4066Sahrens 	argv += optind;
2207fa9e4066Sahrens 
22082f8aaab3Seschrock 	if (cachefile && nsearch != 0) {
22092f8aaab3Seschrock 		(void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
22102f8aaab3Seschrock 		usage(B_FALSE);
22112f8aaab3Seschrock 	}
22122f8aaab3Seschrock 
2213468c413aSTim Haley 	if ((dryrun || xtreme_rewind) && !do_rewind) {
2214468c413aSTim Haley 		(void) fprintf(stderr,
2215468c413aSTim Haley 		    gettext("-n or -X only meaningful with -F\n"));
2216468c413aSTim Haley 		usage(B_FALSE);
2217468c413aSTim Haley 	}
2218468c413aSTim Haley 	if (dryrun)
2219468c413aSTim Haley 		rewind_policy = ZPOOL_TRY_REWIND;
2220468c413aSTim Haley 	else if (do_rewind)
2221468c413aSTim Haley 		rewind_policy = ZPOOL_DO_REWIND;
2222468c413aSTim Haley 	if (xtreme_rewind)
2223468c413aSTim Haley 		rewind_policy |= ZPOOL_EXTREME_REWIND;
2224468c413aSTim Haley 
2225468c413aSTim Haley 	/* In the future, we can capture further policy and include it here */
2226468c413aSTim Haley 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2227f9af39baSGeorge Wilson 	    nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2228468c413aSTim Haley 	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2229468c413aSTim Haley 		goto error;
2230468c413aSTim Haley 
2231fa9e4066Sahrens 	if (searchdirs == NULL) {
2232fa9e4066Sahrens 		searchdirs = safe_malloc(sizeof (char *));
22336401734dSWill Andrews 		searchdirs[0] = ZFS_DISK_ROOT;
2234fa9e4066Sahrens 		nsearch = 1;
2235fa9e4066Sahrens 	}
2236fa9e4066Sahrens 
2237fa9e4066Sahrens 	/* check argument count */
2238fa9e4066Sahrens 	if (do_all) {
2239fa9e4066Sahrens 		if (argc != 0) {
2240fa9e4066Sahrens 			(void) fprintf(stderr, gettext("too many arguments\n"));
224199653d4eSeschrock 			usage(B_FALSE);
2242fa9e4066Sahrens 		}
2243fa9e4066Sahrens 	} else {
2244fa9e4066Sahrens 		if (argc > 2) {
2245fa9e4066Sahrens 			(void) fprintf(stderr, gettext("too many arguments\n"));
224699653d4eSeschrock 			usage(B_FALSE);
2247fa9e4066Sahrens 		}
2248fa9e4066Sahrens 
2249fa9e4066Sahrens 		/*
2250fa9e4066Sahrens 		 * Check for the SYS_CONFIG privilege.  We do this explicitly
2251fa9e4066Sahrens 		 * here because otherwise any attempt to discover pools will
2252fa9e4066Sahrens 		 * silently fail.
2253fa9e4066Sahrens 		 */
2254fa9e4066Sahrens 		if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2255fa9e4066Sahrens 			(void) fprintf(stderr, gettext("cannot "
2256fa9e4066Sahrens 			    "discover pools: permission denied\n"));
225799653d4eSeschrock 			free(searchdirs);
2258468c413aSTim Haley 			nvlist_free(policy);
2259fa9e4066Sahrens 			return (1);
2260fa9e4066Sahrens 		}
2261fa9e4066Sahrens 	}
2262fa9e4066Sahrens 
2263fa9e4066Sahrens 	/*
2264fa9e4066Sahrens 	 * Depending on the arguments given, we do one of the following:
2265fa9e4066Sahrens 	 *
2266fa9e4066Sahrens 	 *	<none>	Iterate through all pools and display information about
2267fa9e4066Sahrens 	 *		each one.
2268fa9e4066Sahrens 	 *
2269fa9e4066Sahrens 	 *	-a	Iterate through all pools and try to import each one.
2270fa9e4066Sahrens 	 *
2271fa9e4066Sahrens 	 *	<id>	Find the pool that corresponds to the given GUID/pool
2272fa9e4066Sahrens 	 *		name and import that one.
22734c58d714Sdarrenm 	 *
22744c58d714Sdarrenm 	 *	-D	Above options applies only to destroyed pools.
2275fa9e4066Sahrens 	 */
2276fa9e4066Sahrens 	if (argc != 0) {
2277fa9e4066Sahrens 		char *endptr;
2278fa9e4066Sahrens 
2279fa9e4066Sahrens 		errno = 0;
2280fa9e4066Sahrens 		searchguid = strtoull(argv[0], &endptr, 10);
22819edf9ebdSPrasad Joshi 		if (errno != 0 || *endptr != '\0') {
2282fa9e4066Sahrens 			searchname = argv[0];
22839edf9ebdSPrasad Joshi 			searchguid = 0;
22849edf9ebdSPrasad Joshi 		}
2285fa9e4066Sahrens 		found_config = NULL;
2286fa9e4066Sahrens 
228724e697d4Sck 		/*
2288d41c4376SMark J Musante 		 * User specified a name or guid.  Ensure it's unique.
228924e697d4Sck 		 */
2290d41c4376SMark J Musante 		idata.unique = B_TRUE;
229124e697d4Sck 	}
229224e697d4Sck 
2293d41c4376SMark J Musante 
2294d41c4376SMark J Musante 	idata.path = searchdirs;
2295d41c4376SMark J Musante 	idata.paths = nsearch;
2296d41c4376SMark J Musante 	idata.poolname = searchname;
2297d41c4376SMark J Musante 	idata.guid = searchguid;
2298d41c4376SMark J Musante 	idata.cachefile = cachefile;
2299d41c4376SMark J Musante 
2300d41c4376SMark J Musante 	pools = zpool_search_import(g_zfs, &idata);
2301d41c4376SMark J Musante 
2302d41c4376SMark J Musante 	if (pools != NULL && idata.exists &&
2303d41c4376SMark J Musante 	    (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2304d41c4376SMark J Musante 		(void) fprintf(stderr, gettext("cannot import '%s': "
2305d41c4376SMark J Musante 		    "a pool with that name already exists\n"),
2306d41c4376SMark J Musante 		    argv[0]);
2307d41c4376SMark J Musante 		(void) fprintf(stderr, gettext("use the form '%s "
2308d41c4376SMark J Musante 		    "<pool | id> <newpool>' to give it a new name\n"),
2309d41c4376SMark J Musante 		    "zpool import");
2310d41c4376SMark J Musante 		err = 1;
2311d41c4376SMark J Musante 	} else if (pools == NULL && idata.exists) {
2312d41c4376SMark J Musante 		(void) fprintf(stderr, gettext("cannot import '%s': "
2313d41c4376SMark J Musante 		    "a pool with that name is already created/imported,\n"),
2314d41c4376SMark J Musante 		    argv[0]);
2315d41c4376SMark J Musante 		(void) fprintf(stderr, gettext("and no additional pools "
2316d41c4376SMark J Musante 		    "with that name were found\n"));
2317d41c4376SMark J Musante 		err = 1;
2318d41c4376SMark J Musante 	} else if (pools == NULL) {
231924e697d4Sck 		if (argc != 0) {
232024e697d4Sck 			(void) fprintf(stderr, gettext("cannot import '%s': "
232124e697d4Sck 			    "no such pool available\n"), argv[0]);
232224e697d4Sck 		}
2323d41c4376SMark J Musante 		err = 1;
2324d41c4376SMark J Musante 	}
2325d41c4376SMark J Musante 
2326d41c4376SMark J Musante 	if (err == 1) {
232724e697d4Sck 		free(searchdirs);
2328468c413aSTim Haley 		nvlist_free(policy);
232924e697d4Sck 		return (1);
233024e697d4Sck 	}
233124e697d4Sck 
233224e697d4Sck 	/*
233324e697d4Sck 	 * At this point we have a list of import candidate configs. Even if
233424e697d4Sck 	 * we were searching by pool name or guid, we still need to
233524e697d4Sck 	 * post-process the list to deal with pool state and possible
233624e697d4Sck 	 * duplicate names.
233724e697d4Sck 	 */
2338fa9e4066Sahrens 	err = 0;
2339fa9e4066Sahrens 	elem = NULL;
234099653d4eSeschrock 	first = B_TRUE;
2341fa9e4066Sahrens 	while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2342fa9e4066Sahrens 
2343fa9e4066Sahrens 		verify(nvpair_value_nvlist(elem, &config) == 0);
2344fa9e4066Sahrens 
23454c58d714Sdarrenm 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
23464c58d714Sdarrenm 		    &pool_state) == 0);
23474c58d714Sdarrenm 		if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
23484c58d714Sdarrenm 			continue;
23494c58d714Sdarrenm 		if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
23504c58d714Sdarrenm 			continue;
23514c58d714Sdarrenm 
2352468c413aSTim Haley 		verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2353468c413aSTim Haley 		    policy) == 0);
2354468c413aSTim Haley 
2355fa9e4066Sahrens 		if (argc == 0) {
2356fa9e4066Sahrens 			if (first)
235799653d4eSeschrock 				first = B_FALSE;
23583bb79becSeschrock 			else if (!do_all)
2359fa9e4066Sahrens 				(void) printf("\n");
2360fa9e4066Sahrens 
2361468c413aSTim Haley 			if (do_all) {
2362fa9e4066Sahrens 				err |= do_import(config, NULL, mntopts,
23634b964adaSGeorge Wilson 				    props, flags);
2364468c413aSTim Haley 			} else {
2365fa9e4066Sahrens 				show_import(config);
2366468c413aSTim Haley 			}
2367fa9e4066Sahrens 		} else if (searchname != NULL) {
2368fa9e4066Sahrens 			char *name;
2369fa9e4066Sahrens 
2370fa9e4066Sahrens 			/*
2371fa9e4066Sahrens 			 * We are searching for a pool based on name.
2372fa9e4066Sahrens 			 */
2373fa9e4066Sahrens 			verify(nvlist_lookup_string(config,
2374fa9e4066Sahrens 			    ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2375fa9e4066Sahrens 
2376fa9e4066Sahrens 			if (strcmp(name, searchname) == 0) {
2377fa9e4066Sahrens 				if (found_config != NULL) {
2378fa9e4066Sahrens 					(void) fprintf(stderr, gettext(
2379fa9e4066Sahrens 					    "cannot import '%s': more than "
2380fa9e4066Sahrens 					    "one matching pool\n"), searchname);
2381fa9e4066Sahrens 					(void) fprintf(stderr, gettext(
2382fa9e4066Sahrens 					    "import by numeric ID instead\n"));
238399653d4eSeschrock 					err = B_TRUE;
2384fa9e4066Sahrens 				}
2385fa9e4066Sahrens 				found_config = config;
2386fa9e4066Sahrens 			}
2387fa9e4066Sahrens 		} else {
2388fa9e4066Sahrens 			uint64_t guid;
2389fa9e4066Sahrens 
2390fa9e4066Sahrens 			/*
2391fa9e4066Sahrens 			 * Search for a pool by guid.
2392fa9e4066Sahrens 			 */
2393fa9e4066Sahrens 			verify(nvlist_lookup_uint64(config,
2394fa9e4066Sahrens 			    ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2395fa9e4066Sahrens 
2396fa9e4066Sahrens 			if (guid == searchguid)
2397fa9e4066Sahrens 				found_config = config;
2398fa9e4066Sahrens 		}
2399fa9e4066Sahrens 	}
2400fa9e4066Sahrens 
2401fa9e4066Sahrens 	/*
2402fa9e4066Sahrens 	 * If we were searching for a specific pool, verify that we found a
2403fa9e4066Sahrens 	 * pool, and then do the import.
2404fa9e4066Sahrens 	 */
2405fa9e4066Sahrens 	if (argc != 0 && err == 0) {
2406fa9e4066Sahrens 		if (found_config == NULL) {
2407fa9e4066Sahrens 			(void) fprintf(stderr, gettext("cannot import '%s': "
2408fa9e4066Sahrens 			    "no such pool available\n"), argv[0]);
240999653d4eSeschrock 			err = B_TRUE;
2410fa9e4066Sahrens 		} else {
2411fa9e4066Sahrens 			err |= do_import(found_config, argc == 1 ? NULL :
24124b964adaSGeorge Wilson 			    argv[1], mntopts, props, flags);
2413fa9e4066Sahrens 		}
2414fa9e4066Sahrens 	}
2415fa9e4066Sahrens 
2416fa9e4066Sahrens 	/*
2417fa9e4066Sahrens 	 * If we were just looking for pools, report an error if none were
2418fa9e4066Sahrens 	 * found.
2419fa9e4066Sahrens 	 */
2420fa9e4066Sahrens 	if (argc == 0 && first)
2421fa9e4066Sahrens 		(void) fprintf(stderr,
2422fa9e4066Sahrens 		    gettext("no pools available to import\n"));
2423fa9e4066Sahrens 
2424ecd6cf80Smarks error:
24252f8aaab3Seschrock 	nvlist_free(props);
2426fa9e4066Sahrens 	nvlist_free(pools);
2427468c413aSTim Haley 	nvlist_free(policy);
242899653d4eSeschrock 	free(searchdirs);
2429fa9e4066Sahrens 
2430fa9e4066Sahrens 	return (err ? 1 : 0);
2431fa9e4066Sahrens }
2432fa9e4066Sahrens 
2433fa9e4066Sahrens typedef struct iostat_cbdata {
24344263d13fSGeorge Wilson 	boolean_t cb_verbose;
2435fa9e4066Sahrens 	int cb_namewidth;
24364263d13fSGeorge Wilson 	int cb_iteration;
24374263d13fSGeorge Wilson 	zpool_list_t *cb_list;
2438fa9e4066Sahrens } iostat_cbdata_t;
2439fa9e4066Sahrens 
2440fa9e4066Sahrens static void
2441fa9e4066Sahrens print_iostat_separator(iostat_cbdata_t *cb)
2442fa9e4066Sahrens {
2443fa9e4066Sahrens 	int i = 0;
2444fa9e4066Sahrens 
2445fa9e4066Sahrens 	for (i = 0; i < cb->cb_namewidth; i++)
2446fa9e4066Sahrens 		(void) printf("-");
2447fa9e4066Sahrens 	(void) printf("  -----  -----  -----  -----  -----  -----\n");
2448fa9e4066Sahrens }
2449fa9e4066Sahrens 
2450fa9e4066Sahrens static void
2451fa9e4066Sahrens print_iostat_header(iostat_cbdata_t *cb)
2452fa9e4066Sahrens {
2453fa9e4066Sahrens 	(void) printf("%*s     capacity     operations    bandwidth\n",
2454fa9e4066Sahrens 	    cb->cb_namewidth, "");
2455485bbbf5SGeorge Wilson 	(void) printf("%-*s  alloc   free   read  write   read  write\n",
2456fa9e4066Sahrens 	    cb->cb_namewidth, "pool");
2457fa9e4066Sahrens 	print_iostat_separator(cb);
2458fa9e4066Sahrens }
2459fa9e4066Sahrens 
2460fa9e4066Sahrens /*
2461fa9e4066Sahrens  * Display a single statistic.
2462fa9e4066Sahrens  */
2463990b4856Slling static void
2464fa9e4066Sahrens print_one_stat(uint64_t value)
2465fa9e4066Sahrens {
2466fa9e4066Sahrens 	char buf[64];
2467fa9e4066Sahrens 
2468fa9e4066Sahrens 	zfs_nicenum(value, buf, sizeof (buf));
2469fa9e4066Sahrens 	(void) printf("  %5s", buf);
2470fa9e4066Sahrens }
2471fa9e4066Sahrens 
2472fa9e4066Sahrens /*
2473fa9e4066Sahrens  * Print out all the statistics for the given vdev.  This can either be the
2474fa9e4066Sahrens  * toplevel configuration, or called recursively.  If 'name' is NULL, then this
2475fa9e4066Sahrens  * is a verbose output, and we don't want to display the toplevel pool stats.
2476fa9e4066Sahrens  */
2477fa9e4066Sahrens void
2478c67d9675Seschrock print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2479c67d9675Seschrock     nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2480fa9e4066Sahrens {
2481fa9e4066Sahrens 	nvlist_t **oldchild, **newchild;
2482fa9e4066Sahrens 	uint_t c, children;
2483fa9e4066Sahrens 	vdev_stat_t *oldvs, *newvs;
2484fa9e4066Sahrens 	vdev_stat_t zerovs = { 0 };
2485fa9e4066Sahrens 	uint64_t tdelta;
2486fa9e4066Sahrens 	double scale;
2487afefbcddSeschrock 	char *vname;
2488fa9e4066Sahrens 
24895cabbc6bSPrashanth Sreenivasa 	if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
24905cabbc6bSPrashanth Sreenivasa 		return;
24915cabbc6bSPrashanth Sreenivasa 
2492fa9e4066Sahrens 	if (oldnv != NULL) {
24933f9d6ad7SLin Ling 		verify(nvlist_lookup_uint64_array(oldnv,
24943f9d6ad7SLin Ling 		    ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2495fa9e4066Sahrens 	} else {
2496fa9e4066Sahrens 		oldvs = &zerovs;
2497fa9e4066Sahrens 	}
2498fa9e4066Sahrens 
24993f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2500fa9e4066Sahrens 	    (uint64_t **)&newvs, &c) == 0);
2501fa9e4066Sahrens 
2502fa9e4066Sahrens 	if (strlen(name) + depth > cb->cb_namewidth)
2503fa9e4066Sahrens 		(void) printf("%*s%s", depth, "", name);
2504fa9e4066Sahrens 	else
2505fa9e4066Sahrens 		(void) printf("%*s%s%*s", depth, "", name,
2506fa9e4066Sahrens 		    (int)(cb->cb_namewidth - strlen(name) - depth), "");
2507fa9e4066Sahrens 
2508fa9e4066Sahrens 	tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2509fa9e4066Sahrens 
2510fa9e4066Sahrens 	if (tdelta == 0)
2511fa9e4066Sahrens 		scale = 1.0;
2512fa9e4066Sahrens 	else
2513fa9e4066Sahrens 		scale = (double)NANOSEC / tdelta;
2514fa9e4066Sahrens 
2515fa9e4066Sahrens 	/* only toplevel vdevs have capacity stats */
2516fa9e4066Sahrens 	if (newvs->vs_space == 0) {
2517fa9e4066Sahrens 		(void) printf("      -      -");
2518fa9e4066Sahrens 	} else {
2519fa9e4066Sahrens 		print_one_stat(newvs->vs_alloc);
2520fa9e4066Sahrens 		print_one_stat(newvs->vs_space - newvs->vs_alloc);
2521fa9e4066Sahrens 	}
2522fa9e4066Sahrens 
2523fa9e4066Sahrens 	print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2524fa9e4066Sahrens 	    oldvs->vs_ops[ZIO_TYPE_READ])));
2525fa9e4066Sahrens 
2526fa9e4066Sahrens 	print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2527fa9e4066Sahrens 	    oldvs->vs_ops[ZIO_TYPE_WRITE])));
2528fa9e4066Sahrens 
2529fa9e4066Sahrens 	print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2530fa9e4066Sahrens 	    oldvs->vs_bytes[ZIO_TYPE_READ])));
2531fa9e4066Sahrens 
2532fa9e4066Sahrens 	print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2533fa9e4066Sahrens 	    oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2534fa9e4066Sahrens 
2535fa9e4066Sahrens 	(void) printf("\n");
2536fa9e4066Sahrens 
2537fa9e4066Sahrens 	if (!cb->cb_verbose)
2538fa9e4066Sahrens 		return;
2539fa9e4066Sahrens 
2540fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2541fa9e4066Sahrens 	    &newchild, &children) != 0)
2542fa9e4066Sahrens 		return;
2543fa9e4066Sahrens 
2544fa9e4066Sahrens 	if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2545fa9e4066Sahrens 	    &oldchild, &c) != 0)
2546fa9e4066Sahrens 		return;
2547fa9e4066Sahrens 
2548afefbcddSeschrock 	for (c = 0; c < children; c++) {
25499d439f90SMike Harsch 		uint64_t ishole = B_FALSE, islog = B_FALSE;
25503f9d6ad7SLin Ling 
25519d439f90SMike Harsch 		(void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
25529d439f90SMike Harsch 		    &ishole);
25539d439f90SMike Harsch 
25549d439f90SMike Harsch 		(void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
25559d439f90SMike Harsch 		    &islog);
25569d439f90SMike Harsch 
25579d439f90SMike Harsch 		if (ishole || islog)
25583f9d6ad7SLin Ling 			continue;
25593f9d6ad7SLin Ling 
256088ecc943SGeorge Wilson 		vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2561c67d9675Seschrock 		print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2562afefbcddSeschrock 		    newchild[c], cb, depth + 2);
2563afefbcddSeschrock 		free(vname);
2564afefbcddSeschrock 	}
2565fa94a07fSbrendan 
25669d439f90SMike Harsch 	/*
25679d439f90SMike Harsch 	 * Log device section
25689d439f90SMike Harsch 	 */
25699d439f90SMike Harsch 
25709d439f90SMike Harsch 	if (num_logs(newnv) > 0) {
25719d439f90SMike Harsch 		(void) printf("%-*s      -      -      -      -      -      "
25729d439f90SMike Harsch 		    "-\n", cb->cb_namewidth, "logs");
25739d439f90SMike Harsch 
25749d439f90SMike Harsch 		for (c = 0; c < children; c++) {
25759d439f90SMike Harsch 			uint64_t islog = B_FALSE;
25769d439f90SMike Harsch 			(void) nvlist_lookup_uint64(newchild[c],
25779d439f90SMike Harsch 			    ZPOOL_CONFIG_IS_LOG, &islog);
25789d439f90SMike Harsch 
25799d439f90SMike Harsch 			if (islog) {
25809d439f90SMike Harsch 				vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
25819d439f90SMike Harsch 				    B_FALSE);
25829d439f90SMike Harsch 				print_vdev_stats(zhp, vname, oldnv ?
25839d439f90SMike Harsch 				    oldchild[c] : NULL, newchild[c],
25849d439f90SMike Harsch 				    cb, depth + 2);
25859d439f90SMike Harsch 				free(vname);
25869d439f90SMike Harsch 			}
25879d439f90SMike Harsch 		}
25889d439f90SMike Harsch 
25899d439f90SMike Harsch 	}
25909d439f90SMike Harsch 
2591fa94a07fSbrendan 	/*
2592fa94a07fSbrendan 	 * Include level 2 ARC devices in iostat output
2593fa94a07fSbrendan 	 */
2594fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2595fa94a07fSbrendan 	    &newchild, &children) != 0)
2596fa94a07fSbrendan 		return;
2597fa94a07fSbrendan 
2598fa94a07fSbrendan 	if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2599fa94a07fSbrendan 	    &oldchild, &c) != 0)
2600fa94a07fSbrendan 		return;
2601fa94a07fSbrendan 
2602fa94a07fSbrendan 	if (children > 0) {
2603fa94a07fSbrendan 		(void) printf("%-*s      -      -      -      -      -      "
2604fa94a07fSbrendan 		    "-\n", cb->cb_namewidth, "cache");
2605fa94a07fSbrendan 		for (c = 0; c < children; c++) {
260688ecc943SGeorge Wilson 			vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
260788ecc943SGeorge Wilson 			    B_FALSE);
2608fa94a07fSbrendan 			print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2609fa94a07fSbrendan 			    newchild[c], cb, depth + 2);
2610fa94a07fSbrendan 			free(vname);
2611fa94a07fSbrendan 		}
2612fa94a07fSbrendan 	}
2613fa9e4066Sahrens }
2614fa9e4066Sahrens 
2615088e9d47Seschrock static int
2616088e9d47Seschrock refresh_iostat(zpool_handle_t *zhp, void *data)
2617088e9d47Seschrock {
2618088e9d47Seschrock 	iostat_cbdata_t *cb = data;
261994de1d4cSeschrock 	boolean_t missing;
2620088e9d47Seschrock 
2621088e9d47Seschrock 	/*
2622088e9d47Seschrock 	 * If the pool has disappeared, remove it from the list and continue.
2623088e9d47Seschrock 	 */
262494de1d4cSeschrock 	if (zpool_refresh_stats(zhp, &missing) != 0)
262594de1d4cSeschrock 		return (-1);
262694de1d4cSeschrock 
262794de1d4cSeschrock 	if (missing)
2628088e9d47Seschrock 		pool_list_remove(cb->cb_list, zhp);
2629088e9d47Seschrock 
2630088e9d47Seschrock 	return (0);
2631088e9d47Seschrock }
2632088e9d47Seschrock 
2633fa9e4066Sahrens /*
2634fa9e4066Sahrens  * Callback to print out the iostats for the given pool.
2635fa9e4066Sahrens  */
2636fa9e4066Sahrens int
2637fa9e4066Sahrens print_iostat(zpool_handle_t *zhp, void *data)
2638fa9e4066Sahrens {
2639fa9e4066Sahrens 	iostat_cbdata_t *cb = data;
2640fa9e4066Sahrens 	nvlist_t *oldconfig, *newconfig;
2641fa9e4066Sahrens 	nvlist_t *oldnvroot, *newnvroot;
2642fa9e4066Sahrens 
2643088e9d47Seschrock 	newconfig = zpool_get_config(zhp, &oldconfig);
2644fa9e4066Sahrens 
2645088e9d47Seschrock 	if (cb->cb_iteration == 1)
2646fa9e4066Sahrens 		oldconfig = NULL;
2647fa9e4066Sahrens 
2648fa9e4066Sahrens 	verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2649fa9e4066Sahrens 	    &newnvroot) == 0);
2650fa9e4066Sahrens 
2651088e9d47Seschrock 	if (oldconfig == NULL)
2652fa9e4066Sahrens 		oldnvroot = NULL;
2653088e9d47Seschrock 	else
2654088e9d47Seschrock 		verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2655088e9d47Seschrock 		    &oldnvroot) == 0);
2656fa9e4066Sahrens 
2657fa9e4066Sahrens 	/*
2658fa9e4066Sahrens 	 * Print out the statistics for the pool.
2659fa9e4066Sahrens 	 */
2660c67d9675Seschrock 	print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2661fa9e4066Sahrens 
2662fa9e4066Sahrens 	if (cb->cb_verbose)
2663fa9e4066Sahrens 		print_iostat_separator(cb);
2664fa9e4066Sahrens 
2665fa9e4066Sahrens 	return (0);
2666fa9e4066Sahrens }
2667fa9e4066Sahrens 
2668fa9e4066Sahrens int
2669fa9e4066Sahrens get_namewidth(zpool_handle_t *zhp, void *data)
2670fa9e4066Sahrens {
2671fa9e4066Sahrens 	iostat_cbdata_t *cb = data;
2672fa9e4066Sahrens 	nvlist_t *config, *nvroot;
2673fa9e4066Sahrens 
2674088e9d47Seschrock 	if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2675fa9e4066Sahrens 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2676fa9e4066Sahrens 		    &nvroot) == 0);
2677fa9e4066Sahrens 		if (!cb->cb_verbose)
2678fa9e4066Sahrens 			cb->cb_namewidth = strlen(zpool_get_name(zhp));
2679fa9e4066Sahrens 		else
2680e1d5e507SFrederik Wessels 			cb->cb_namewidth = max_width(zhp, nvroot, 0,
2681e1d5e507SFrederik Wessels 			    cb->cb_namewidth);
2682fa9e4066Sahrens 	}
2683fa9e4066Sahrens 
2684fa9e4066Sahrens 	/*
2685fa9e4066Sahrens 	 * The width must fall into the range [10,38].  The upper limit is the
2686fa9e4066Sahrens 	 * maximum we can have and still fit in 80 columns.
2687fa9e4066Sahrens 	 */
2688fa9e4066Sahrens 	if (cb->cb_namewidth < 10)
2689fa9e4066Sahrens 		cb->cb_namewidth = 10;
2690fa9e4066Sahrens 	if (cb->cb_namewidth > 38)
2691fa9e4066Sahrens 		cb->cb_namewidth = 38;
2692fa9e4066Sahrens 
2693fa9e4066Sahrens 	return (0);
2694fa9e4066Sahrens }
2695fa9e4066Sahrens 
2696fa9e4066Sahrens /*
26973f9d6ad7SLin Ling  * Parse the input string, get the 'interval' and 'count' value if there is one.
2698fa9e4066Sahrens  */
26993f9d6ad7SLin Ling static void
27003f9d6ad7SLin Ling get_interval_count(int *argcp, char **argv, unsigned long *iv,
27013f9d6ad7SLin Ling     unsigned long *cnt)
2702fa9e4066Sahrens {
2703fa9e4066Sahrens 	unsigned long interval = 0, count = 0;
27043f9d6ad7SLin Ling 	int argc = *argcp, errno;
2705fa9e4066Sahrens 
2706fa9e4066Sahrens 	/*
2707fa9e4066Sahrens 	 * Determine if the last argument is an integer or a pool name
2708fa9e4066Sahrens 	 */
2709fa9e4066Sahrens 	if (argc > 0 && isdigit(argv[argc - 1][0])) {
2710fa9e4066Sahrens 		char *end;
2711fa9e4066Sahrens 
2712fa9e4066Sahrens 		errno = 0;
2713fa9e4066Sahrens 		interval = strtoul(argv[argc - 1], &end, 10);
2714fa9e4066Sahrens 
2715fa9e4066Sahrens 		if (*end == '\0' && errno == 0) {
2716fa9e4066Sahrens 			if (interval == 0) {
2717fa9e4066Sahrens 				(void) fprintf(stderr, gettext("interval "
2718fa9e4066Sahrens 				    "cannot be zero\n"));
271999653d4eSeschrock 				usage(B_FALSE);
2720fa9e4066Sahrens 			}
2721fa9e4066Sahrens 			/*
2722fa9e4066Sahrens 			 * Ignore the last parameter
2723fa9e4066Sahrens 			 */
2724fa9e4066Sahrens 			argc--;
2725fa9e4066Sahrens 		} else {
2726fa9e4066Sahrens 			/*
2727fa9e4066Sahrens 			 * If this is not a valid number, just plow on.  The
2728fa9e4066Sahrens 			 * user will get a more informative error message later
2729fa9e4066Sahrens 			 * on.
2730fa9e4066Sahrens 			 */
2731fa9e4066Sahrens 			interval = 0;
2732fa9e4066Sahrens 		}
2733fa9e4066Sahrens 	}
2734fa9e4066Sahrens 
2735fa9e4066Sahrens 	/*
2736fa9e4066Sahrens 	 * If the last argument is also an integer, then we have both a count
27373f9d6ad7SLin Ling 	 * and an interval.
2738fa9e4066Sahrens 	 */
2739fa9e4066Sahrens 	if (argc > 0 && isdigit(argv[argc - 1][0])) {
2740fa9e4066Sahrens 		char *end;
2741fa9e4066Sahrens 
2742fa9e4066Sahrens 		errno = 0;
2743fa9e4066Sahrens 		count = interval;
2744fa9e4066Sahrens 		interval = strtoul(argv[argc - 1], &end, 10);
2745fa9e4066Sahrens 
2746fa9e4066Sahrens 		if (*end == '\0' && errno == 0) {
2747fa9e4066Sahrens 			if (interval == 0) {
2748fa9e4066Sahrens 				(void) fprintf(stderr, gettext("interval "
2749fa9e4066Sahrens 				    "cannot be zero\n"));
275099653d4eSeschrock 				usage(B_FALSE);
2751fa9e4066Sahrens 			}
2752fa9e4066Sahrens 
2753fa9e4066Sahrens 			/*
2754fa9e4066Sahrens 			 * Ignore the last parameter
2755fa9e4066Sahrens 			 */
2756fa9e4066Sahrens 			argc--;
2757fa9e4066Sahrens 		} else {
2758fa9e4066Sahrens 			interval = 0;
2759fa9e4066Sahrens 		}
2760fa9e4066Sahrens 	}
2761fa9e4066Sahrens 
27623f9d6ad7SLin Ling 	*iv = interval;
27633f9d6ad7SLin Ling 	*cnt = count;
27643f9d6ad7SLin Ling 	*argcp = argc;
27653f9d6ad7SLin Ling }
27663f9d6ad7SLin Ling 
27673f9d6ad7SLin Ling static void
27683f9d6ad7SLin Ling get_timestamp_arg(char c)
27693f9d6ad7SLin Ling {
27703f9d6ad7SLin Ling 	if (c == 'u')
27713f9d6ad7SLin Ling 		timestamp_fmt = UDATE;
27723f9d6ad7SLin Ling 	else if (c == 'd')
27733f9d6ad7SLin Ling 		timestamp_fmt = DDATE;
27743f9d6ad7SLin Ling 	else
27753f9d6ad7SLin Ling 		usage(B_FALSE);
27763f9d6ad7SLin Ling }
27773f9d6ad7SLin Ling 
27783f9d6ad7SLin Ling /*
27793f9d6ad7SLin Ling  * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
27803f9d6ad7SLin Ling  *
27813f9d6ad7SLin Ling  *	-v	Display statistics for individual vdevs
27823f9d6ad7SLin Ling  *	-T	Display a timestamp in date(1) or Unix format
27833f9d6ad7SLin Ling  *
27843f9d6ad7SLin Ling  * This command can be tricky because we want to be able to deal with pool
27853f9d6ad7SLin Ling  * creation/destruction as well as vdev configuration changes.  The bulk of this
27863f9d6ad7SLin Ling  * processing is handled by the pool_list_* routines in zpool_iter.c.  We rely
27873f9d6ad7SLin Ling  * on pool_list_update() to detect the addition of new pools.  Configuration
27883f9d6ad7SLin Ling  * changes are all handled within libzfs.
27893f9d6ad7SLin Ling  */
27903f9d6ad7SLin Ling int
27913f9d6ad7SLin Ling zpool_do_iostat(int argc, char **argv)
27923f9d6ad7SLin Ling {
27933f9d6ad7SLin Ling 	int c;
27943f9d6ad7SLin Ling 	int ret;
27953f9d6ad7SLin Ling 	int npools;
27963f9d6ad7SLin Ling 	unsigned long interval = 0, count = 0;
27973f9d6ad7SLin Ling 	zpool_list_t *list;
27983f9d6ad7SLin Ling 	boolean_t verbose = B_FALSE;
27993f9d6ad7SLin Ling 	iostat_cbdata_t cb;
28003f9d6ad7SLin Ling 
28013f9d6ad7SLin Ling 	/* check options */
28023f9d6ad7SLin Ling 	while ((c = getopt(argc, argv, "T:v")) != -1) {
28033f9d6ad7SLin Ling 		switch (c) {
28043f9d6ad7SLin Ling 		case 'T':
28053f9d6ad7SLin Ling 			get_timestamp_arg(*optarg);
28063f9d6ad7SLin Ling 			break;
28073f9d6ad7SLin Ling 		case 'v':
28083f9d6ad7SLin Ling 			verbose = B_TRUE;
28093f9d6ad7SLin Ling 			break;
28103f9d6ad7SLin Ling 		case '?':
28113f9d6ad7SLin Ling 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
28123f9d6ad7SLin Ling 			    optopt);
28133f9d6ad7SLin Ling 			usage(B_FALSE);
28143f9d6ad7SLin Ling 		}
28153f9d6ad7SLin Ling 	}
28163f9d6ad7SLin Ling 
28173f9d6ad7SLin Ling 	argc -= optind;
28183f9d6ad7SLin Ling 	argv += optind;
28193f9d6ad7SLin Ling 
28203f9d6ad7SLin Ling 	get_interval_count(&argc, argv, &interval, &count);
28213f9d6ad7SLin Ling 
2822fa9e4066Sahrens 	/*
2823fa9e4066Sahrens 	 * Construct the list of all interesting pools.
2824fa9e4066Sahrens 	 */
2825fa9e4066Sahrens 	ret = 0;
2826b1b8ab34Slling 	if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2827fa9e4066Sahrens 		return (1);
2828fa9e4066Sahrens 
282999653d4eSeschrock 	if (pool_list_count(list) == 0 && argc != 0) {
283099653d4eSeschrock 		pool_list_free(list);
2831fa9e4066Sahrens 		return (1);
283299653d4eSeschrock 	}
2833fa9e4066Sahrens 
2834fa9e4066Sahrens 	if (pool_list_count(list) == 0 && interval == 0) {
283599653d4eSeschrock 		pool_list_free(list);
2836fa9e4066Sahrens 		(void) fprintf(stderr, gettext("no pools available\n"));
2837fa9e4066Sahrens 		return (1);
2838fa9e4066Sahrens 	}
2839fa9e4066Sahrens 
2840fa9e4066Sahrens 	/*
2841fa9e4066Sahrens 	 * Enter the main iostat loop.
2842fa9e4066Sahrens 	 */
2843fa9e4066Sahrens 	cb.cb_list = list;
2844fa9e4066Sahrens 	cb.cb_verbose = verbose;
2845fa9e4066Sahrens 	cb.cb_iteration = 0;
2846fa9e4066Sahrens 	cb.cb_namewidth = 0;
2847fa9e4066Sahrens 
2848fa9e4066Sahrens 	for (;;) {
2849fa9e4066Sahrens 		pool_list_update(list);
2850fa9e4066Sahrens 
2851fa9e4066Sahrens 		if ((npools = pool_list_count(list)) == 0)
2852fa9e4066Sahrens 			break;
2853fa9e4066Sahrens 
2854088e9d47Seschrock 		/*
2855088e9d47Seschrock 		 * Refresh all statistics.  This is done as an explicit step
2856088e9d47Seschrock 		 * before calculating the maximum name width, so that any
2857088e9d47Seschrock 		 * configuration changes are properly accounted for.
2858088e9d47Seschrock 		 */
285999653d4eSeschrock 		(void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2860088e9d47Seschrock 
2861fa9e4066Sahrens 		/*
2862fa9e4066Sahrens 		 * Iterate over all pools to determine the maximum width
2863fa9e4066Sahrens 		 * for the pool / device name column across all pools.
2864fa9e4066Sahrens 		 */
2865fa9e4066Sahrens 		cb.cb_namewidth = 0;
286699653d4eSeschrock 		(void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2867fa9e4066Sahrens 
286826fd7700SKrishnendu Sadhukhan - Sun Microsystems 		if (timestamp_fmt != NODATE)
286926fd7700SKrishnendu Sadhukhan - Sun Microsystems 			print_timestamp(timestamp_fmt);
287026fd7700SKrishnendu Sadhukhan - Sun Microsystems 
2871fa9e4066Sahrens 		/*
2872fa9e4066Sahrens 		 * If it's the first time, or verbose mode, print the header.
2873fa9e4066Sahrens 		 */
2874fa9e4066Sahrens 		if (++cb.cb_iteration == 1 || verbose)
2875fa9e4066Sahrens 			print_iostat_header(&cb);
2876fa9e4066Sahrens 
287799653d4eSeschrock 		(void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2878fa9e4066Sahrens 
2879fa9e4066Sahrens 		/*
2880fa9e4066Sahrens 		 * If there's more than one pool, and we're not in verbose mode
2881fa9e4066Sahrens 		 * (which prints a separator for us), then print a separator.
2882fa9e4066Sahrens 		 */
2883fa9e4066Sahrens 		if (npools > 1 && !verbose)
2884fa9e4066Sahrens 			print_iostat_separator(&cb);
2885fa9e4066Sahrens 
2886fa9e4066Sahrens 		if (verbose)
2887fa9e4066Sahrens 			(void) printf("\n");
2888fa9e4066Sahrens 
288939c23413Seschrock 		/*
289039c23413Seschrock 		 * Flush the output so that redirection to a file isn't buffered
289139c23413Seschrock 		 * indefinitely.
289239c23413Seschrock 		 */
289339c23413Seschrock 		(void) fflush(stdout);
289439c23413Seschrock 
2895fa9e4066Sahrens 		if (interval == 0)
2896fa9e4066Sahrens 			break;
2897fa9e4066Sahrens 
2898fa9e4066Sahrens 		if (count != 0 && --count == 0)
2899fa9e4066Sahrens 			break;
2900fa9e4066Sahrens 
2901fa9e4066Sahrens 		(void) sleep(interval);
2902fa9e4066Sahrens 	}
2903fa9e4066Sahrens 
2904fa9e4066Sahrens 	pool_list_free(list);
2905fa9e4066Sahrens 
2906fa9e4066Sahrens 	return (ret);
2907fa9e4066Sahrens }
2908fa9e4066Sahrens 
2909fa9e4066Sahrens typedef struct list_cbdata {
29104263d13fSGeorge Wilson 	boolean_t	cb_verbose;
29114263d13fSGeorge Wilson 	int		cb_namewidth;
291299653d4eSeschrock 	boolean_t	cb_scripted;
2913990b4856Slling 	zprop_list_t	*cb_proplist;
2914c58b3526SAdam Stevko 	boolean_t	cb_literal;
2915fa9e4066Sahrens } list_cbdata_t;
2916fa9e4066Sahrens 
2917fa9e4066Sahrens /*
2918fa9e4066Sahrens  * Given a list of columns to display, output appropriate headers for each one.
2919fa9e4066Sahrens  */
2920990b4856Slling static void
29214263d13fSGeorge Wilson print_header(list_cbdata_t *cb)
2922fa9e4066Sahrens {
29234263d13fSGeorge Wilson 	zprop_list_t *pl = cb->cb_proplist;
2924ad135b5dSChristopher Siden 	char headerbuf[ZPOOL_MAXPROPLEN];
2925990b4856Slling 	const char *header;
2926990b4856Slling 	boolean_t first = B_TRUE;
2927990b4856Slling 	boolean_t right_justify;
29284263d13fSGeorge Wilson 	size_t width = 0;
2929990b4856Slling 
2930990b4856Slling 	for (; pl != NULL; pl = pl->pl_next) {
29314263d13fSGeorge Wilson 		width = pl->pl_width;
29324263d13fSGeorge Wilson 		if (first && cb->cb_verbose) {
29334263d13fSGeorge Wilson 			/*
29344263d13fSGeorge Wilson 			 * Reset the width to accommodate the verbose listing
29354263d13fSGeorge Wilson 			 * of devices.
29364263d13fSGeorge Wilson 			 */
29374263d13fSGeorge Wilson 			width = cb->cb_namewidth;
29384263d13fSGeorge Wilson 		}
29394263d13fSGeorge Wilson 
2940990b4856Slling 		if (!first)
2941fa9e4066Sahrens 			(void) printf("  ");
2942fa9e4066Sahrens 		else
2943990b4856Slling 			first = B_FALSE;
2944990b4856Slling 
2945ad135b5dSChristopher Siden 		right_justify = B_FALSE;
2946ad135b5dSChristopher Siden 		if (pl->pl_prop != ZPROP_INVAL) {
2947ad135b5dSChristopher Siden 			header = zpool_prop_column_name(pl->pl_prop);
2948ad135b5dSChristopher Siden 			right_justify = zpool_prop_align_right(pl->pl_prop);
2949ad135b5dSChristopher Siden 		} else {
2950ad135b5dSChristopher Siden 			int i;
2951ad135b5dSChristopher Siden 
2952ad135b5dSChristopher Siden 			for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2953ad135b5dSChristopher Siden 				headerbuf[i] = toupper(pl->pl_user_prop[i]);
2954ad135b5dSChristopher Siden 			headerbuf[i] = '\0';
2955ad135b5dSChristopher Siden 			header = headerbuf;
2956ad135b5dSChristopher Siden 		}
2957fa9e4066Sahrens 
2958990b4856Slling 		if (pl->pl_next == NULL && !right_justify)
2959990b4856Slling 			(void) printf("%s", header);
2960990b4856Slling 		else if (right_justify)
29614263d13fSGeorge Wilson 			(void) printf("%*s", width, header);
2962990b4856Slling 		else
29634263d13fSGeorge Wilson 			(void) printf("%-*s", width, header);
29644263d13fSGeorge Wilson 
2965fa9e4066Sahrens 	}
2966fa9e4066Sahrens 
2967fa9e4066Sahrens 	(void) printf("\n");
2968fa9e4066Sahrens }
2969fa9e4066Sahrens 
2970990b4856Slling /*
2971990b4856Slling  * Given a pool and a list of properties, print out all the properties according
2972990b4856Slling  * to the described layout.
2973990b4856Slling  */
2974990b4856Slling static void
29754263d13fSGeorge Wilson print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2976fa9e4066Sahrens {
29774263d13fSGeorge Wilson 	zprop_list_t *pl = cb->cb_proplist;
2978990b4856Slling 	boolean_t first = B_TRUE;
2979990b4856Slling 	char property[ZPOOL_MAXPROPLEN];
2980990b4856Slling 	char *propstr;
2981990b4856Slling 	boolean_t right_justify;
29824263d13fSGeorge Wilson 	size_t width;
2983990b4856Slling 
2984990b4856Slling 	for (; pl != NULL; pl = pl->pl_next) {
29854263d13fSGeorge Wilson 
29864263d13fSGeorge Wilson 		width = pl->pl_width;
29874263d13fSGeorge Wilson 		if (first && cb->cb_verbose) {
29884263d13fSGeorge Wilson 			/*
29894263d13fSGeorge Wilson 			 * Reset the width to accommodate the verbose listing
29904263d13fSGeorge Wilson 			 * of devices.
29914263d13fSGeorge Wilson 			 */
29924263d13fSGeorge Wilson 			width = cb->cb_namewidth;
29934263d13fSGeorge Wilson 		}
29944263d13fSGeorge Wilson 
2995990b4856Slling 		if (!first) {
29964263d13fSGeorge Wilson 			if (cb->cb_scripted)
2997fa9e4066Sahrens 				(void) printf("\t");
2998fa9e4066Sahrens 			else
2999fa9e4066Sahrens 				(void) printf("  ");
3000990b4856Slling 		} else {
3001990b4856Slling 			first = B_FALSE;
3002fa9e4066Sahrens 		}
3003fa9e4066Sahrens 
3004990b4856Slling 		right_justify = B_FALSE;
3005990b4856Slling 		if (pl->pl_prop != ZPROP_INVAL) {
30067a09f97bSGeorge Wilson 			if (zpool_get_prop(zhp, pl->pl_prop, property,
3007c58b3526SAdam Stevko 			    sizeof (property), NULL, cb->cb_literal) != 0)
3008990b4856Slling 				propstr = "-";
3009fa9e4066Sahrens 			else
3010990b4856Slling 				propstr = property;
3011fa9e4066Sahrens 
3012990b4856Slling 			right_justify = zpool_prop_align_right(pl->pl_prop);
3013ad135b5dSChristopher Siden 		} else if ((zpool_prop_feature(pl->pl_user_prop) ||
3014ad135b5dSChristopher Siden 		    zpool_prop_unsupported(pl->pl_user_prop)) &&
3015ad135b5dSChristopher Siden 		    zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
3016ad135b5dSChristopher Siden 		    sizeof (property)) == 0) {
3017ad135b5dSChristopher Siden 			propstr = property;
3018990b4856Slling 		} else {
3019990b4856Slling 			propstr = "-";
3020990b4856Slling 		}
3021fa9e4066Sahrens 
3022fa9e4066Sahrens 
3023990b4856Slling 		/*
3024990b4856Slling 		 * If this is being called in scripted mode, or if this is the
3025990b4856Slling 		 * last column and it is left-justified, don't include a width
3026990b4856Slling 		 * format specifier.
3027990b4856Slling 		 */
30284263d13fSGeorge Wilson 		if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3029990b4856Slling 			(void) printf("%s", propstr);
3030990b4856Slling 		else if (right_justify)
3031990b4856Slling 			(void) printf("%*s", width, propstr);
3032990b4856Slling 		else
3033990b4856Slling 			(void) printf("%-*s", width, propstr);
3034990b4856Slling 	}
3035fa9e4066Sahrens 
3036990b4856Slling 	(void) printf("\n");
3037990b4856Slling }
3038fa9e4066Sahrens 
30394263d13fSGeorge Wilson static void
30407a09f97bSGeorge Wilson print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
30417a09f97bSGeorge Wilson     boolean_t valid)
30424263d13fSGeorge Wilson {
30434263d13fSGeorge Wilson 	char propval[64];
30444263d13fSGeorge Wilson 	boolean_t fixed;
30454263d13fSGeorge Wilson 	size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
30464263d13fSGeorge Wilson 
30477a09f97bSGeorge Wilson 	switch (prop) {
30487a09f97bSGeorge Wilson 	case ZPOOL_PROP_EXPANDSZ:
30497a09f97bSGeorge Wilson 		if (value == 0)
30507a09f97bSGeorge Wilson 			(void) strlcpy(propval, "-", sizeof (propval));
30517a09f97bSGeorge Wilson 		else
30527a09f97bSGeorge Wilson 			zfs_nicenum(value, propval, sizeof (propval));
30537a09f97bSGeorge Wilson 		break;
30547a09f97bSGeorge Wilson 	case ZPOOL_PROP_FRAGMENTATION:
30557a09f97bSGeorge Wilson 		if (value == ZFS_FRAG_INVALID) {
30567a09f97bSGeorge Wilson 			(void) strlcpy(propval, "-", sizeof (propval));
30577a09f97bSGeorge Wilson 		} else {
30587a09f97bSGeorge Wilson 			(void) snprintf(propval, sizeof (propval), "%llu%%",
30597a09f97bSGeorge Wilson 			    value);
30607a09f97bSGeorge Wilson 		}
30617a09f97bSGeorge Wilson 		break;
30627a09f97bSGeorge Wilson 	case ZPOOL_PROP_CAPACITY:
30632e4c9986SGeorge Wilson 		(void) snprintf(propval, sizeof (propval), "%llu%%", value);
30647a09f97bSGeorge Wilson 		break;
30657a09f97bSGeorge Wilson 	default:
30662e4c9986SGeorge Wilson 		zfs_nicenum(value, propval, sizeof (propval));
30677a09f97bSGeorge Wilson 	}
30687a09f97bSGeorge Wilson 
30697a09f97bSGeorge Wilson 	if (!valid)
30707a09f97bSGeorge Wilson 		(void) strlcpy(propval, "-", sizeof (propval));
30714263d13fSGeorge Wilson 
30724263d13fSGeorge Wilson 	if (scripted)
30734263d13fSGeorge Wilson 		(void) printf("\t%s", propval);
30744263d13fSGeorge Wilson 	else
30754263d13fSGeorge Wilson 		(void) printf("  %*s", width, propval);
30764263d13fSGeorge Wilson }
30774263d13fSGeorge Wilson 
30784263d13fSGeorge Wilson void
30794263d13fSGeorge Wilson print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
30804263d13fSGeorge Wilson     list_cbdata_t *cb, int depth)
30814263d13fSGeorge Wilson {
30824263d13fSGeorge Wilson 	nvlist_t **child;
30834263d13fSGeorge Wilson 	vdev_stat_t *vs;
30844263d13fSGeorge Wilson 	uint_t c, children;
30854263d13fSGeorge Wilson 	char *vname;
30864263d13fSGeorge Wilson 	boolean_t scripted = cb->cb_scripted;
308752244c09SJohn Wren Kennedy 	uint64_t islog = B_FALSE;
308852244c09SJohn Wren Kennedy 	boolean_t haslog = B_FALSE;
308952244c09SJohn Wren Kennedy 	char *dashes = "%-*s      -      -      -         -      -      -\n";
30904263d13fSGeorge Wilson 
30914263d13fSGeorge Wilson 	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
30924263d13fSGeorge Wilson 	    (uint64_t **)&vs, &c) == 0);
30934263d13fSGeorge Wilson 
30944263d13fSGeorge Wilson 	if (name != NULL) {
30957a09f97bSGeorge Wilson 		boolean_t toplevel = (vs->vs_space != 0);
30967a09f97bSGeorge Wilson 		uint64_t cap;
30977a09f97bSGeorge Wilson 
30985cabbc6bSPrashanth Sreenivasa 		if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
30995cabbc6bSPrashanth Sreenivasa 			return;
31005cabbc6bSPrashanth Sreenivasa 
31014263d13fSGeorge Wilson 		if (scripted)
31024263d13fSGeorge Wilson 			(void) printf("\t%s", name);
31034263d13fSGeorge Wilson 		else if (strlen(name) + depth > cb->cb_namewidth)
31044263d13fSGeorge Wilson 			(void) printf("%*s%s", depth, "", name);
31054263d13fSGeorge Wilson 		else
31064263d13fSGeorge Wilson 			(void) printf("%*s%s%*s", depth, "", name,
31074263d13fSGeorge Wilson 			    (int)(cb->cb_namewidth - strlen(name) - depth), "");
31084263d13fSGeorge Wilson 
31097a09f97bSGeorge Wilson 		/*
31107a09f97bSGeorge Wilson 		 * Print the properties for the individual vdevs. Some
31117a09f97bSGeorge Wilson 		 * properties are only applicable to toplevel vdevs. The
31127a09f97bSGeorge Wilson 		 * 'toplevel' boolean value is passed to the print_one_column()
31137a09f97bSGeorge Wilson 		 * to indicate that the value is valid.
31147a09f97bSGeorge Wilson 		 */
31157a09f97bSGeorge Wilson 		print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted,
31167a09f97bSGeorge Wilson 		    toplevel);
31177a09f97bSGeorge Wilson 		print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
31187a09f97bSGeorge Wilson 		    toplevel);
31197a09f97bSGeorge Wilson 		print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
31207a09f97bSGeorge Wilson 		    scripted, toplevel);
31217a09f97bSGeorge Wilson 		print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
31227a09f97bSGeorge Wilson 		    B_TRUE);
31237a09f97bSGeorge Wilson 		print_one_column(ZPOOL_PROP_FRAGMENTATION,
31247a09f97bSGeorge Wilson 		    vs->vs_fragmentation, scripted,
31257a09f97bSGeorge Wilson 		    (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel));
31267a09f97bSGeorge Wilson 		cap = (vs->vs_space == 0) ? 0 :
31277a09f97bSGeorge Wilson 		    (vs->vs_alloc * 100 / vs->vs_space);
31287a09f97bSGeorge Wilson 		print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel);
31294263d13fSGeorge Wilson 		(void) printf("\n");
31304263d13fSGeorge Wilson 	}
31314263d13fSGeorge Wilson 
31324263d13fSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
31334263d13fSGeorge Wilson 	    &child, &children) != 0)
31344263d13fSGeorge Wilson 		return;
31354263d13fSGeorge Wilson 
31364263d13fSGeorge Wilson 	for (c = 0; c < children; c++) {
31374263d13fSGeorge Wilson 		uint64_t ishole = B_FALSE;
31384263d13fSGeorge Wilson 
31394263d13fSGeorge Wilson 		if (nvlist_lookup_uint64(child[c],
31404263d13fSGeorge Wilson 		    ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
31414263d13fSGeorge Wilson 			continue;
31424263d13fSGeorge Wilson 
314352244c09SJohn Wren Kennedy 		if (nvlist_lookup_uint64(child[c],
314452244c09SJohn Wren Kennedy 		    ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog) {
314552244c09SJohn Wren Kennedy 			haslog = B_TRUE;
314652244c09SJohn Wren Kennedy 			continue;
314752244c09SJohn Wren Kennedy 		}
314852244c09SJohn Wren Kennedy 
31494263d13fSGeorge Wilson 		vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
31504263d13fSGeorge Wilson 		print_list_stats(zhp, vname, child[c], cb, depth + 2);
31514263d13fSGeorge Wilson 		free(vname);
31524263d13fSGeorge Wilson 	}
31534263d13fSGeorge Wilson 
315452244c09SJohn Wren Kennedy 	if (haslog == B_TRUE) {
315552244c09SJohn Wren Kennedy 		/* LINTED E_SEC_PRINTF_VAR_FMT */
315652244c09SJohn Wren Kennedy 		(void) printf(dashes, cb->cb_namewidth, "log");
315752244c09SJohn Wren Kennedy 		for (c = 0; c < children; c++) {
315852244c09SJohn Wren Kennedy 			if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
315952244c09SJohn Wren Kennedy 			    &islog) != 0 || !islog)
316052244c09SJohn Wren Kennedy 				continue;
316152244c09SJohn Wren Kennedy 			vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
316252244c09SJohn Wren Kennedy 			print_list_stats(zhp, vname, child[c], cb, depth + 2);
316352244c09SJohn Wren Kennedy 			free(vname);
316452244c09SJohn Wren Kennedy 		}
316552244c09SJohn Wren Kennedy 	}
316652244c09SJohn Wren Kennedy 
31674263d13fSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
316852244c09SJohn Wren Kennedy 	    &child, &children) == 0 && children > 0) {
316952244c09SJohn Wren Kennedy 		/* LINTED E_SEC_PRINTF_VAR_FMT */
317052244c09SJohn Wren Kennedy 		(void) printf(dashes, cb->cb_namewidth, "cache");
317152244c09SJohn Wren Kennedy 		for (c = 0; c < children; c++) {
317252244c09SJohn Wren Kennedy 			vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
317352244c09SJohn Wren Kennedy 			print_list_stats(zhp, vname, child[c], cb, depth + 2);
317452244c09SJohn Wren Kennedy 			free(vname);
317552244c09SJohn Wren Kennedy 		}
317652244c09SJohn Wren Kennedy 	}
31774263d13fSGeorge Wilson 
317852244c09SJohn Wren Kennedy 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child,
317952244c09SJohn Wren Kennedy 	    &children) == 0 && children > 0) {
318052244c09SJohn Wren Kennedy 		/* LINTED E_SEC_PRINTF_VAR_FMT */
318152244c09SJohn Wren Kennedy 		(void) printf(dashes, cb->cb_namewidth, "spare");
31824263d13fSGeorge Wilson 		for (c = 0; c < children; c++) {
318352244c09SJohn Wren Kennedy 			vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
31844263d13fSGeorge Wilson 			print_list_stats(zhp, vname, child[c], cb, depth + 2);
31854263d13fSGeorge Wilson 			free(vname);
31864263d13fSGeorge Wilson 		}
31874263d13fSGeorge Wilson 	}
31884263d13fSGeorge Wilson }
31894263d13fSGeorge Wilson 
31904263d13fSGeorge Wilson 
3191990b4856Slling /*
3192990b4856Slling  * Generic callback function to list a pool.
3193990b4856Slling  */
3194990b4856Slling int
3195990b4856Slling list_callback(zpool_handle_t *zhp, void *data)
3196990b4856Slling {
3197990b4856Slling 	list_cbdata_t *cbp = data;
31984263d13fSGeorge Wilson 	nvlist_t *config;
31994263d13fSGeorge Wilson 	nvlist_t *nvroot;
3200fa9e4066Sahrens 
32014263d13fSGeorge Wilson 	config = zpool_get_config(zhp, NULL);
3202fa9e4066Sahrens 
32034263d13fSGeorge Wilson 	print_pool(zhp, cbp);
32044263d13fSGeorge Wilson 	if (!cbp->cb_verbose)
32054263d13fSGeorge Wilson 		return (0);
32064263d13fSGeorge Wilson 
32074263d13fSGeorge Wilson 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
32084263d13fSGeorge Wilson 	    &nvroot) == 0);
32094263d13fSGeorge Wilson 	print_list_stats(zhp, NULL, nvroot, cbp, 0);
3210fa9e4066Sahrens 
3211fa9e4066Sahrens 	return (0);
3212fa9e4066Sahrens }
3213fa9e4066Sahrens 
3214fa9e4066Sahrens /*
3215c58b3526SAdam Stevko  * zpool list [-Hp] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3216fa9e4066Sahrens  *
3217990b4856Slling  *	-H	Scripted mode.  Don't display headers, and separate properties
3218990b4856Slling  *		by a single tab.
3219990b4856Slling  *	-o	List of properties to display.  Defaults to
32207a09f97bSGeorge Wilson  *		"name,size,allocated,free,expandsize,fragmentation,capacity,"
32217a09f97bSGeorge Wilson  *		"dedupratio,health,altroot"
3222c58b3526SAdam Stevko  * 	-p	Diplay values in parsable (exact) format.
32233f9d6ad7SLin Ling  *	-T	Display a timestamp in date(1) or Unix format
3224fa9e4066Sahrens  *
3225fa9e4066Sahrens  * List all pools in the system, whether or not they're healthy.  Output space
3226fa9e4066Sahrens  * statistics for each one, as well as health status summary.
3227fa9e4066Sahrens  */
3228fa9e4066Sahrens int
3229fa9e4066Sahrens zpool_do_list(int argc, char **argv)
3230fa9e4066Sahrens {
3231fa9e4066Sahrens 	int c;
3232fa9e4066Sahrens 	int ret;
3233fa9e4066Sahrens 	list_cbdata_t cb = { 0 };
3234990b4856Slling 	static char default_props[] =
32357a09f97bSGeorge Wilson 	    "name,size,allocated,free,expandsize,fragmentation,capacity,"
32362e4c9986SGeorge Wilson 	    "dedupratio,health,altroot";
3237990b4856Slling 	char *props = default_props;
32383f9d6ad7SLin Ling 	unsigned long interval = 0, count = 0;
32394263d13fSGeorge Wilson 	zpool_list_t *list;
32404263d13fSGeorge Wilson 	boolean_t first = B_TRUE;
3241fa9e4066Sahrens 
3242fa9e4066Sahrens 	/* check options */
3243c58b3526SAdam Stevko 	while ((c = getopt(argc, argv, ":Ho:pT:v")) != -1) {
3244fa9e4066Sahrens 		switch (c) {
3245fa9e4066Sahrens 		case 'H':
324699653d4eSeschrock 			cb.cb_scripted = B_TRUE;
3247fa9e4066Sahrens 			break;
3248fa9e4066Sahrens 		case 'o':
3249990b4856Slling 			props = optarg;
3250fa9e4066Sahrens 			break;
3251c58b3526SAdam Stevko 		case 'p':
3252c58b3526SAdam Stevko 			cb.cb_literal = B_TRUE;
3253c58b3526SAdam Stevko 			break;
32543f9d6ad7SLin Ling 		case 'T':
32553f9d6ad7SLin Ling 			get_timestamp_arg(*optarg);
32563f9d6ad7SLin Ling 			break;
32574263d13fSGeorge Wilson 		case 'v':
32584263d13fSGeorge Wilson 			cb.cb_verbose = B_TRUE;
32594263d13fSGeorge Wilson 			break;
3260fa9e4066Sahrens 		case ':':
3261fa9e4066Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
3262fa9e4066Sahrens 			    "'%c' option\n"), optopt);
326399653d4eSeschrock 			usage(B_FALSE);
3264fa9e4066Sahrens 			break;
3265fa9e4066Sahrens 		case '?':
3266fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3267fa9e4066Sahrens 			    optopt);
326899653d4eSeschrock 			usage(B_FALSE);
3269fa9e4066Sahrens 		}
3270fa9e4066Sahrens 	}
3271fa9e4066Sahrens 
3272fa9e4066Sahrens 	argc -= optind;
3273fa9e4066Sahrens 	argv += optind;
3274fa9e4066Sahrens 
32753f9d6ad7SLin Ling 	get_interval_count(&argc, argv, &interval, &count);
32763f9d6ad7SLin Ling 
3277990b4856Slling 	if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3278990b4856Slling 		usage(B_FALSE);
3279fa9e4066Sahrens 
32803f9d6ad7SLin Ling 	for (;;) {
3281cd67d23dSGeorge Wilson 		if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
3282cd67d23dSGeorge Wilson 		    &ret)) == NULL)
3283cd67d23dSGeorge Wilson 			return (1);
32844263d13fSGeorge Wilson 
32854263d13fSGeorge Wilson 		if (pool_list_count(list) == 0)
32864263d13fSGeorge Wilson 			break;
32874263d13fSGeorge Wilson 
32884263d13fSGeorge Wilson 		cb.cb_namewidth = 0;
32894263d13fSGeorge Wilson 		(void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3290990b4856Slling 
32913f9d6ad7SLin Ling 		if (timestamp_fmt != NODATE)
32923f9d6ad7SLin Ling 			print_timestamp(timestamp_fmt);
3293fa9e4066Sahrens 
32944263d13fSGeorge Wilson 		if (!cb.cb_scripted && (first || cb.cb_verbose)) {
32954263d13fSGeorge Wilson 			print_header(&cb);
32964263d13fSGeorge Wilson 			first = B_FALSE;
32973f9d6ad7SLin Ling 		}
32984263d13fSGeorge Wilson 		ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
32993f9d6ad7SLin Ling 
33003f9d6ad7SLin Ling 		if (interval == 0)
33013f9d6ad7SLin Ling 			break;
33023f9d6ad7SLin Ling 
33033f9d6ad7SLin Ling 		if (count != 0 && --count == 0)
33043f9d6ad7SLin Ling 			break;
33053f9d6ad7SLin Ling 
3306cd67d23dSGeorge Wilson 		pool_list_free(list);
33073f9d6ad7SLin Ling 		(void) sleep(interval);
3308fa9e4066Sahrens 	}
3309fa9e4066Sahrens 
3310cd67d23dSGeorge Wilson 	if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3311cd67d23dSGeorge Wilson 		(void) printf(gettext("no pools available\n"));
3312cd67d23dSGeorge Wilson 		ret = 0;
3313cd67d23dSGeorge Wilson 	}
3314cd67d23dSGeorge Wilson 
3315cd67d23dSGeorge Wilson 	pool_list_free(list);
33163f9d6ad7SLin Ling 	zprop_free_list(cb.cb_proplist);
3317fa9e4066Sahrens 	return (ret);
3318fa9e4066Sahrens }
3319fa9e4066Sahrens 
3320fa9e4066Sahrens static int
3321fa9e4066Sahrens zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3322fa9e4066Sahrens {
332399653d4eSeschrock 	boolean_t force = B_FALSE;
3324fa9e4066Sahrens 	int c;
3325fa9e4066Sahrens 	nvlist_t *nvroot;
3326fa9e4066Sahrens 	char *poolname, *old_disk, *new_disk;
3327fa9e4066Sahrens 	zpool_handle_t *zhp;
33287855d95bSToomas Soome 	zpool_boot_label_t boot_type;
33297855d95bSToomas Soome 	uint64_t boot_size;
333099653d4eSeschrock 	int ret;
3331fa9e4066Sahrens 
3332fa9e4066Sahrens 	/* check options */
3333fa9e4066Sahrens 	while ((c = getopt(argc, argv, "f")) != -1) {
3334fa9e4066Sahrens 		switch (c) {
3335fa9e4066Sahrens 		case 'f':
333699653d4eSeschrock 			force = B_TRUE;
3337fa9e4066Sahrens 			break;
3338fa9e4066Sahrens 		case '?':
3339fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3340fa9e4066Sahrens 			    optopt);
334199653d4eSeschrock 			usage(B_FALSE);
3342fa9e4066Sahrens 		}
3343fa9e4066Sahrens 	}
3344fa9e4066Sahrens 
3345fa9e4066Sahrens 	argc -= optind;
3346fa9e4066Sahrens 	argv += optind;
3347fa9e4066Sahrens 
3348fa9e4066Sahrens 	/* get pool name and check number of arguments */
3349fa9e4066Sahrens 	if (argc < 1) {
3350fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
335199653d4eSeschrock 		usage(B_FALSE);
3352fa9e4066Sahrens 	}
3353fa9e4066Sahrens 
3354fa9e4066Sahrens 	poolname = argv[0];
3355fa9e4066Sahrens 
3356fa9e4066Sahrens 	if (argc < 2) {
3357fa9e4066Sahrens 		(void) fprintf(stderr,
3358fa9e4066Sahrens 		    gettext("missing <device> specification\n"));
335999653d4eSeschrock 		usage(B_FALSE);
3360fa9e4066Sahrens 	}
3361fa9e4066Sahrens 
3362fa9e4066Sahrens 	old_disk = argv[1];
3363fa9e4066Sahrens 
3364fa9e4066Sahrens 	if (argc < 3) {
3365fa9e4066Sahrens 		if (!replacing) {
3366fa9e4066Sahrens 			(void) fprintf(stderr,
3367fa9e4066Sahrens 			    gettext("missing <new_device> specification\n"));
336899653d4eSeschrock 			usage(B_FALSE);
3369fa9e4066Sahrens 		}
3370fa9e4066Sahrens 		new_disk = old_disk;
3371fa9e4066Sahrens 		argc -= 1;
3372fa9e4066Sahrens 		argv += 1;
3373fa9e4066Sahrens 	} else {
3374fa9e4066Sahrens 		new_disk = argv[2];
3375fa9e4066Sahrens 		argc -= 2;
3376fa9e4066Sahrens 		argv += 2;
3377fa9e4066Sahrens 	}
3378fa9e4066Sahrens 
3379fa9e4066Sahrens 	if (argc > 1) {
3380fa9e4066Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
338199653d4eSeschrock 		usage(B_FALSE);
3382fa9e4066Sahrens 	}
3383fa9e4066Sahrens 
338499653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3385fa9e4066Sahrens 		return (1);
3386fa9e4066Sahrens 
33878488aeb5Staylor 	if (zpool_get_config(zhp, NULL) == NULL) {
3388fa9e4066Sahrens 		(void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3389fa9e4066Sahrens 		    poolname);
3390fa9e4066Sahrens 		zpool_close(zhp);
3391fa9e4066Sahrens 		return (1);
3392fa9e4066Sahrens 	}
3393fa9e4066Sahrens 
33947855d95bSToomas Soome 	if (zpool_is_bootable(zhp))
33957855d95bSToomas Soome 		boot_type = ZPOOL_COPY_BOOT_LABEL;
33967855d95bSToomas Soome 	else
33977855d95bSToomas Soome 		boot_type = ZPOOL_NO_BOOT_LABEL;
33987855d95bSToomas Soome 
33997855d95bSToomas Soome 	boot_size = zpool_get_prop_int(zhp, ZPOOL_PROP_BOOTSIZE, NULL);
3400705040edSEric Taylor 	nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
34017855d95bSToomas Soome 	    boot_type, boot_size, argc, argv);
3402fa9e4066Sahrens 	if (nvroot == NULL) {
3403fa9e4066Sahrens 		zpool_close(zhp);
3404fa9e4066Sahrens 		return (1);
3405fa9e4066Sahrens 	}
3406fa9e4066Sahrens 
340799653d4eSeschrock 	ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
340899653d4eSeschrock 
340999653d4eSeschrock 	nvlist_free(nvroot);
341099653d4eSeschrock 	zpool_close(zhp);
341199653d4eSeschrock 
341299653d4eSeschrock 	return (ret);
3413fa9e4066Sahrens }
3414fa9e4066Sahrens 
3415fa9e4066Sahrens /*
3416fa9e4066Sahrens  * zpool replace [-f] <pool> <device> <new_device>
3417fa9e4066Sahrens  *
3418fa9e4066Sahrens  *	-f	Force attach, even if <new_device> appears to be in use.
3419fa9e4066Sahrens  *
3420fa9e4066Sahrens  * Replace <device> with <new_device>.
3421fa9e4066Sahrens  */
3422fa9e4066Sahrens /* ARGSUSED */
3423fa9e4066Sahrens int
3424fa9e4066Sahrens zpool_do_replace(int argc, char **argv)
3425fa9e4066Sahrens {
3426fa9e4066Sahrens 	return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3427fa9e4066Sahrens }
3428fa9e4066Sahrens 
3429fa9e4066Sahrens /*
3430fa9e4066Sahrens  * zpool attach [-f] <pool> <device> <new_device>
3431fa9e4066Sahrens  *
3432fa9e4066Sahrens  *	-f	Force attach, even if <new_device> appears to be in use.
3433fa9e4066Sahrens  *
3434fa9e4066Sahrens  * Attach <new_device> to the mirror containing <device>.  If <device> is not
3435fa9e4066Sahrens  * part of a mirror, then <device> will be transformed into a mirror of
3436fa9e4066Sahrens  * <device> and <new_device>.  In either case, <new_device> will begin life
3437fa9e4066Sahrens  * with a DTL of [0, now], and will immediately begin to resilver itself.
3438fa9e4066Sahrens  */
3439fa9e4066Sahrens int
3440fa9e4066Sahrens zpool_do_attach(int argc, char **argv)
3441fa9e4066Sahrens {
3442fa9e4066Sahrens 	return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3443fa9e4066Sahrens }
3444fa9e4066Sahrens 
3445fa9e4066Sahrens /*
3446fa9e4066Sahrens  * zpool detach [-f] <pool> <device>
3447fa9e4066Sahrens  *
3448fa9e4066Sahrens  *	-f	Force detach of <device>, even if DTLs argue against it
3449fa9e4066Sahrens  *		(not supported yet)
3450fa9e4066Sahrens  *
3451fa9e4066Sahrens  * Detach a device from a mirror.  The operation will be refused if <device>
3452fa9e4066Sahrens  * is the last device in the mirror, or if the DTLs indicate that this device
3453fa9e4066Sahrens  * has the only valid copy of some data.
3454fa9e4066Sahrens  */
3455fa9e4066Sahrens /* ARGSUSED */
3456fa9e4066Sahrens int
3457fa9e4066Sahrens zpool_do_detach(int argc, char **argv)
3458fa9e4066Sahrens {
3459fa9e4066Sahrens 	int c;
3460fa9e4066Sahrens 	char *poolname, *path;
3461fa9e4066Sahrens 	zpool_handle_t *zhp;
346299653d4eSeschrock 	int ret;
3463fa9e4066Sahrens 
3464fa9e4066Sahrens 	/* check options */
3465fa9e4066Sahrens 	while ((c = getopt(argc, argv, "f")) != -1) {
3466fa9e4066Sahrens 		switch (c) {
3467fa9e4066Sahrens 		case 'f':
3468fa9e4066Sahrens 		case '?':
3469fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3470fa9e4066Sahrens 			    optopt);
347199653d4eSeschrock 			usage(B_FALSE);
3472fa9e4066Sahrens 		}
3473fa9e4066Sahrens 	}
3474fa9e4066Sahrens 
3475fa9e4066Sahrens 	argc -= optind;
3476fa9e4066Sahrens 	argv += optind;
3477fa9e4066Sahrens 
3478fa9e4066Sahrens 	/* get pool name and check number of arguments */
3479fa9e4066Sahrens 	if (argc < 1) {
3480fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
348199653d4eSeschrock 		usage(B_FALSE);
3482fa9e4066Sahrens 	}
3483fa9e4066Sahrens 
3484fa9e4066Sahrens 	if (argc < 2) {
3485fa9e4066Sahrens 		(void) fprintf(stderr,
3486fa9e4066Sahrens 		    gettext("missing <device> specification\n"));
348799653d4eSeschrock 		usage(B_FALSE);
3488fa9e4066Sahrens 	}
3489fa9e4066Sahrens 
3490fa9e4066Sahrens 	poolname = argv[0];
3491fa9e4066Sahrens 	path = argv[1];
3492fa9e4066Sahrens 
349399653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3494fa9e4066Sahrens 		return (1);
3495fa9e4066Sahrens 
349699653d4eSeschrock 	ret = zpool_vdev_detach(zhp, path);
349799653d4eSeschrock 
349899653d4eSeschrock 	zpool_close(zhp);
349999653d4eSeschrock 
350099653d4eSeschrock 	return (ret);
3501fa9e4066Sahrens }
3502fa9e4066Sahrens 
35031195e687SMark J Musante /*
35041195e687SMark J Musante  * zpool split [-n] [-o prop=val] ...
35051195e687SMark J Musante  *		[-o mntopt] ...
35061195e687SMark J Musante  *		[-R altroot] <pool> <newpool> [<device> ...]
35071195e687SMark J Musante  *
35081195e687SMark J Musante  *	-n	Do not split the pool, but display the resulting layout if
35091195e687SMark J Musante  *		it were to be split.
35101195e687SMark J Musante  *	-o	Set property=value, or set mount options.
35111195e687SMark J Musante  *	-R	Mount the split-off pool under an alternate root.
35121195e687SMark J Musante  *
35131195e687SMark J Musante  * Splits the named pool and gives it the new pool name.  Devices to be split
35141195e687SMark J Musante  * off may be listed, provided that no more than one device is specified
35151195e687SMark J Musante  * per top-level vdev mirror.  The newly split pool is left in an exported
35161195e687SMark J Musante  * state unless -R is specified.
35171195e687SMark J Musante  *
35181195e687SMark J Musante  * Restrictions: the top-level of the pool pool must only be made up of
35191195e687SMark J Musante  * mirrors; all devices in the pool must be healthy; no device may be
35201195e687SMark J Musante  * undergoing a resilvering operation.
35211195e687SMark J Musante  */
35221195e687SMark J Musante int
35231195e687SMark J Musante zpool_do_split(int argc, char **argv)
35241195e687SMark J Musante {
35251195e687SMark J Musante 	char *srcpool, *newpool, *propval;
35261195e687SMark J Musante 	char *mntopts = NULL;
35271195e687SMark J Musante 	splitflags_t flags;
35281195e687SMark J Musante 	int c, ret = 0;
35291195e687SMark J Musante 	zpool_handle_t *zhp;
35301195e687SMark J Musante 	nvlist_t *config, *props = NULL;
35311195e687SMark J Musante 
35321195e687SMark J Musante 	flags.dryrun = B_FALSE;
35331195e687SMark J Musante 	flags.import = B_FALSE;
35341195e687SMark J Musante 
35351195e687SMark J Musante 	/* check options */
35361195e687SMark J Musante 	while ((c = getopt(argc, argv, ":R:no:")) != -1) {
35371195e687SMark J Musante 		switch (c) {
35381195e687SMark J Musante 		case 'R':
35391195e687SMark J Musante 			flags.import = B_TRUE;
35401195e687SMark J Musante 			if (add_prop_list(
35411195e687SMark J Musante 			    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
35421195e687SMark J Musante 			    &props, B_TRUE) != 0) {
3543aab83bb8SJosef 'Jeff' Sipek 				nvlist_free(props);
35441195e687SMark J Musante 				usage(B_FALSE);
35451195e687SMark J Musante 			}
35461195e687SMark J Musante 			break;
35471195e687SMark J Musante 		case 'n':
35481195e687SMark J Musante 			flags.dryrun = B_TRUE;
35491195e687SMark J Musante 			break;
35501195e687SMark J Musante 		case 'o':
35511195e687SMark J Musante 			if ((propval = strchr(optarg, '=')) != NULL) {
35521195e687SMark J Musante 				*propval = '\0';
35531195e687SMark J Musante 				propval++;
35541195e687SMark J Musante 				if (add_prop_list(optarg, propval,
35551195e687SMark J Musante 				    &props, B_TRUE) != 0) {
3556aab83bb8SJosef 'Jeff' Sipek 					nvlist_free(props);
35571195e687SMark J Musante 					usage(B_FALSE);
35581195e687SMark J Musante 				}
35591195e687SMark J Musante 			} else {
35601195e687SMark J Musante 				mntopts = optarg;
35611195e687SMark J Musante 			}
35621195e687SMark J Musante 			break;
35631195e687SMark J Musante 		case ':':
35641195e687SMark J Musante 			(void) fprintf(stderr, gettext("missing argument for "
35651195e687SMark J Musante 			    "'%c' option\n"), optopt);
35661195e687SMark J Musante 			usage(B_FALSE);
35671195e687SMark J Musante 			break;
35681195e687SMark J Musante 		case '?':
35691195e687SMark J Musante 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
35701195e687SMark J Musante 			    optopt);
35711195e687SMark J Musante 			usage(B_FALSE);
35721195e687SMark J Musante 			break;
35731195e687SMark J Musante 		}
35741195e687SMark J Musante 	}
35751195e687SMark J Musante 
35761195e687SMark J Musante 	if (!flags.import && mntopts != NULL) {
35771195e687SMark J Musante 		(void) fprintf(stderr, gettext("setting mntopts is only "
35781195e687SMark J Musante 		    "valid when importing the pool\n"));
35791195e687SMark J Musante 		usage(B_FALSE);
35801195e687SMark J Musante 	}
35811195e687SMark J Musante 
35821195e687SMark J Musante 	argc -= optind;
35831195e687SMark J Musante 	argv += optind;
35841195e687SMark J Musante 
35851195e687SMark J Musante 	if (argc < 1) {
35861195e687SMark J Musante 		(void) fprintf(stderr, gettext("Missing pool name\n"));
35871195e687SMark J Musante 		usage(B_FALSE);
35881195e687SMark J Musante 	}
35891195e687SMark J Musante 	if (argc < 2) {
35901195e687SMark J Musante 		(void) fprintf(stderr, gettext("Missing new pool name\n"));
35911195e687SMark J Musante 		usage(B_FALSE);
35921195e687SMark J Musante 	}
35931195e687SMark J Musante 
35941195e687SMark J Musante 	srcpool = argv[0];
35951195e687SMark J Musante 	newpool = argv[1];
35961195e687SMark J Musante 
35971195e687SMark J Musante 	argc -= 2;
35981195e687SMark J Musante 	argv += 2;
35991195e687SMark J Musante 
36001195e687SMark J Musante 	if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
36011195e687SMark J Musante 		return (1);
36021195e687SMark J Musante 
36031195e687SMark J Musante 	config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
36041195e687SMark J Musante 	if (config == NULL) {
36051195e687SMark J Musante 		ret = 1;
36061195e687SMark J Musante 	} else {
36071195e687SMark J Musante 		if (flags.dryrun) {
36081195e687SMark J Musante 			(void) printf(gettext("would create '%s' with the "
36091195e687SMark J Musante 			    "following layout:\n\n"), newpool);
36101195e687SMark J Musante 			print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
36111195e687SMark J Musante 		}
36121195e687SMark J Musante 		nvlist_free(config);
36131195e687SMark J Musante 	}
36141195e687SMark J Musante 
36151195e687SMark J Musante 	zpool_close(zhp);
36161195e687SMark J Musante 
36171195e687SMark J Musante 	if (ret != 0 || flags.dryrun || !flags.import)
36181195e687SMark J Musante 		return (ret);
36191195e687SMark J Musante 
36201195e687SMark J Musante 	/*
36211195e687SMark J Musante 	 * The split was successful. Now we need to open the new
36221195e687SMark J Musante 	 * pool and import it.
36231195e687SMark J Musante 	 */
36241195e687SMark J Musante 	if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
36251195e687SMark J Musante 		return (1);
36261195e687SMark J Musante 	if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
36271195e687SMark J Musante 	    zpool_enable_datasets(zhp, mntopts, 0) != 0) {
36281195e687SMark J Musante 		ret = 1;
3629fe7cd8aaSCyril Plisko 		(void) fprintf(stderr, gettext("Split was successful, but "
36301195e687SMark J Musante 		    "the datasets could not all be mounted\n"));
36311195e687SMark J Musante 		(void) fprintf(stderr, gettext("Try doing '%s' with a "
36321195e687SMark J Musante 		    "different altroot\n"), "zpool import");
36331195e687SMark J Musante 	}
36341195e687SMark J Musante 	zpool_close(zhp);
36351195e687SMark J Musante 
36361195e687SMark J Musante 	return (ret);
36371195e687SMark J Musante }
36381195e687SMark J Musante 
36391195e687SMark J Musante 
36401195e687SMark J Musante 
3641fa9e4066Sahrens /*
3642441d80aaSlling  * zpool online <pool> <device> ...
3643fa9e4066Sahrens  */
3644fa9e4066Sahrens int
3645fa9e4066Sahrens zpool_do_online(int argc, char **argv)
3646fa9e4066Sahrens {
3647fa9e4066Sahrens 	int c, i;
3648fa9e4066Sahrens 	char *poolname;
3649fa9e4066Sahrens 	zpool_handle_t *zhp;
3650fa9e4066Sahrens 	int ret = 0;
36513d7072f8Seschrock 	vdev_state_t newstate;
3652573ca77eSGeorge Wilson 	int flags = 0;
3653fa9e4066Sahrens 
3654fa9e4066Sahrens 	/* check options */
3655573ca77eSGeorge Wilson 	while ((c = getopt(argc, argv, "et")) != -1) {
3656fa9e4066Sahrens 		switch (c) {
3657573ca77eSGeorge Wilson 		case 'e':
3658573ca77eSGeorge Wilson 			flags |= ZFS_ONLINE_EXPAND;
3659573ca77eSGeorge Wilson 			break;
3660fa9e4066Sahrens 		case 't':
3661fa9e4066Sahrens 		case '?':
3662fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3663fa9e4066Sahrens 			    optopt);
366499653d4eSeschrock 			usage(B_FALSE);
3665fa9e4066Sahrens 		}
3666fa9e4066Sahrens 	}
3667fa9e4066Sahrens 
3668fa9e4066Sahrens 	argc -= optind;
3669fa9e4066Sahrens 	argv += optind;
3670fa9e4066Sahrens 
3671fa9e4066Sahrens 	/* get pool name and check number of arguments */
3672fa9e4066Sahrens 	if (argc < 1) {
3673fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name\n"));
367499653d4eSeschrock 		usage(B_FALSE);
3675fa9e4066Sahrens 	}
3676fa9e4066Sahrens 	if (argc < 2) {
3677fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing device name\n"));
367899653d4eSeschrock 		usage(B_FALSE);
3679fa9e4066Sahrens 	}
3680fa9e4066Sahrens 
3681fa9e4066Sahrens 	poolname = argv[0];
3682fa9e4066Sahrens 
368399653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3684fa9e4066Sahrens 		return (1);
3685fa9e4066Sahrens 
36863d7072f8Seschrock 	for (i = 1; i < argc; i++) {
3687573ca77eSGeorge Wilson 		if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
36883d7072f8Seschrock 			if (newstate != VDEV_STATE_HEALTHY) {
36893d7072f8Seschrock 				(void) printf(gettext("warning: device '%s' "
36903d7072f8Seschrock 				    "onlined, but remains in faulted state\n"),
36913d7072f8Seschrock 				    argv[i]);
36923d7072f8Seschrock 				if (newstate == VDEV_STATE_FAULTED)
36933d7072f8Seschrock 					(void) printf(gettext("use 'zpool "
36943d7072f8Seschrock 					    "clear' to restore a faulted "
36953d7072f8Seschrock 					    "device\n"));
36963d7072f8Seschrock 				else
36973d7072f8Seschrock 					(void) printf(gettext("use 'zpool "
36983d7072f8Seschrock 					    "replace' to replace devices "
36993d7072f8Seschrock 					    "that are no longer present\n"));
37003d7072f8Seschrock 			}
37013d7072f8Seschrock 		} else {
3702fa9e4066Sahrens 			ret = 1;
37033d7072f8Seschrock 		}
37043d7072f8Seschrock 	}
3705fa9e4066Sahrens 
370699653d4eSeschrock 	zpool_close(zhp);
370799653d4eSeschrock 
3708fa9e4066Sahrens 	return (ret);
3709fa9e4066Sahrens }
3710fa9e4066Sahrens 
3711fa9e4066Sahrens /*
3712441d80aaSlling  * zpool offline [-ft] <pool> <device> ...
3713fa9e4066Sahrens  *
3714fa9e4066Sahrens  *	-f	Force the device into the offline state, even if doing
3715fa9e4066Sahrens  *		so would appear to compromise pool availability.
3716fa9e4066Sahrens  *		(not supported yet)
3717fa9e4066Sahrens  *
3718fa9e4066Sahrens  *	-t	Only take the device off-line temporarily.  The offline
3719fa9e4066Sahrens  *		state will not be persistent across reboots.
3720fa9e4066Sahrens  */
3721fa9e4066Sahrens /* ARGSUSED */
3722fa9e4066Sahrens int
3723fa9e4066Sahrens zpool_do_offline(int argc, char **argv)
3724fa9e4066Sahrens {
3725fa9e4066Sahrens 	int c, i;
3726fa9e4066Sahrens 	char *poolname;
3727fa9e4066Sahrens 	zpool_handle_t *zhp;
372899653d4eSeschrock 	int ret = 0;
372999653d4eSeschrock 	boolean_t istmp = B_FALSE;
3730fa9e4066Sahrens 
3731fa9e4066Sahrens 	/* check options */
3732fa9e4066Sahrens 	while ((c = getopt(argc, argv, "ft")) != -1) {
3733fa9e4066Sahrens 		switch (c) {
3734fa9e4066Sahrens 		case 't':
373599653d4eSeschrock 			istmp = B_TRUE;
3736441d80aaSlling 			break;
3737441d80aaSlling 		case 'f':
3738fa9e4066Sahrens 		case '?':
3739fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3740fa9e4066Sahrens 			    optopt);
374199653d4eSeschrock 			usage(B_FALSE);
3742fa9e4066Sahrens 		}
3743fa9e4066Sahrens 	}
3744fa9e4066Sahrens 
3745fa9e4066Sahrens 	argc -= optind;
3746fa9e4066Sahrens 	argv += optind;
3747fa9e4066Sahrens 
3748fa9e4066Sahrens 	/* get pool name and check number of arguments */
3749fa9e4066Sahrens 	if (argc < 1) {
3750fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name\n"));
375199653d4eSeschrock 		usage(B_FALSE);
3752fa9e4066Sahrens 	}
3753fa9e4066Sahrens 	if (argc < 2) {
3754fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing device name\n"));
375599653d4eSeschrock 		usage(B_FALSE);
3756fa9e4066Sahrens 	}
3757fa9e4066Sahrens 
3758fa9e4066Sahrens 	poolname = argv[0];
3759fa9e4066Sahrens 
376099653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3761fa9e4066Sahrens 		return (1);
3762fa9e4066Sahrens 
37633d7072f8Seschrock 	for (i = 1; i < argc; i++) {
37643d7072f8Seschrock 		if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3765fa9e4066Sahrens 			ret = 1;
37663d7072f8Seschrock 	}
3767fa9e4066Sahrens 
376899653d4eSeschrock 	zpool_close(zhp);
376999653d4eSeschrock 
3770fa9e4066Sahrens 	return (ret);
3771fa9e4066Sahrens }
3772fa9e4066Sahrens 
3773ea8dc4b6Seschrock /*
3774ea8dc4b6Seschrock  * zpool clear <pool> [device]
3775ea8dc4b6Seschrock  *
3776ea8dc4b6Seschrock  * Clear all errors associated with a pool or a particular device.
3777ea8dc4b6Seschrock  */
3778ea8dc4b6Seschrock int
3779ea8dc4b6Seschrock zpool_do_clear(int argc, char **argv)
3780ea8dc4b6Seschrock {
3781468c413aSTim Haley 	int c;
3782ea8dc4b6Seschrock 	int ret = 0;
3783468c413aSTim Haley 	boolean_t dryrun = B_FALSE;
3784468c413aSTim Haley 	boolean_t do_rewind = B_FALSE;
3785468c413aSTim Haley 	boolean_t xtreme_rewind = B_FALSE;
3786468c413aSTim Haley 	uint32_t rewind_policy = ZPOOL_NO_REWIND;
3787468c413aSTim Haley 	nvlist_t *policy = NULL;
3788ea8dc4b6Seschrock 	zpool_handle_t *zhp;
3789ea8dc4b6Seschrock 	char *pool, *device;
3790ea8dc4b6Seschrock 
3791468c413aSTim Haley 	/* check options */
3792468c413aSTim Haley 	while ((c = getopt(argc, argv, "FnX")) != -1) {
3793468c413aSTim Haley 		switch (c) {
3794468c413aSTim Haley 		case 'F':
3795468c413aSTim Haley 			do_rewind = B_TRUE;
3796468c413aSTim Haley 			break;
3797468c413aSTim Haley 		case 'n':
3798468c413aSTim Haley 			dryrun = B_TRUE;
3799468c413aSTim Haley 			break;
3800468c413aSTim Haley 		case 'X':
3801468c413aSTim Haley 			xtreme_rewind = B_TRUE;
3802468c413aSTim Haley 			break;
3803468c413aSTim Haley 		case '?':
3804468c413aSTim Haley 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3805468c413aSTim Haley 			    optopt);
3806468c413aSTim Haley 			usage(B_FALSE);
3807468c413aSTim Haley 		}
3808468c413aSTim Haley 	}
3809468c413aSTim Haley 
3810468c413aSTim Haley 	argc -= optind;
3811468c413aSTim Haley 	argv += optind;
3812468c413aSTim Haley 
3813468c413aSTim Haley 	if (argc < 1) {
3814ea8dc4b6Seschrock 		(void) fprintf(stderr, gettext("missing pool name\n"));
381599653d4eSeschrock 		usage(B_FALSE);
3816ea8dc4b6Seschrock 	}
3817ea8dc4b6Seschrock 
3818468c413aSTim Haley 	if (argc > 2) {
3819ea8dc4b6Seschrock 		(void) fprintf(stderr, gettext("too many arguments\n"));
382099653d4eSeschrock 		usage(B_FALSE);
3821ea8dc4b6Seschrock 	}
3822ea8dc4b6Seschrock 
3823468c413aSTim Haley 	if ((dryrun || xtreme_rewind) && !do_rewind) {
3824468c413aSTim Haley 		(void) fprintf(stderr,
3825468c413aSTim Haley 		    gettext("-n or -X only meaningful with -F\n"));
3826468c413aSTim Haley 		usage(B_FALSE);
3827468c413aSTim Haley 	}
3828468c413aSTim Haley 	if (dryrun)
3829468c413aSTim Haley 		rewind_policy = ZPOOL_TRY_REWIND;
3830468c413aSTim Haley 	else if (do_rewind)
3831468c413aSTim Haley 		rewind_policy = ZPOOL_DO_REWIND;
3832468c413aSTim Haley 	if (xtreme_rewind)
3833468c413aSTim Haley 		rewind_policy |= ZPOOL_EXTREME_REWIND;
3834468c413aSTim Haley 
3835468c413aSTim Haley 	/* In future, further rewind policy choices can be passed along here */
3836468c413aSTim Haley 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3837468c413aSTim Haley 	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3838468c413aSTim Haley 		return (1);
3839468c413aSTim Haley 
3840468c413aSTim Haley 	pool = argv[0];
3841468c413aSTim Haley 	device = argc == 2 ? argv[1] : NULL;
3842ea8dc4b6Seschrock 
3843468c413aSTim Haley 	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3844468c413aSTim Haley 		nvlist_free(policy);
3845ea8dc4b6Seschrock 		return (1);
3846468c413aSTim Haley 	}
3847ea8dc4b6Seschrock 
3848468c413aSTim Haley 	if (zpool_clear(zhp, device, policy) != 0)
3849ea8dc4b6Seschrock 		ret = 1;
3850ea8dc4b6Seschrock 
3851ea8dc4b6Seschrock 	zpool_close(zhp);
3852ea8dc4b6Seschrock 
3853468c413aSTim Haley 	nvlist_free(policy);
3854468c413aSTim Haley 
3855ea8dc4b6Seschrock 	return (ret);
3856ea8dc4b6Seschrock }
3857ea8dc4b6Seschrock 
3858e9103aaeSGarrett D'Amore /*
3859e9103aaeSGarrett D'Amore  * zpool reguid <pool>
3860e9103aaeSGarrett D'Amore  */
3861e9103aaeSGarrett D'Amore int
3862e9103aaeSGarrett D'Amore zpool_do_reguid(int argc, char **argv)
3863e9103aaeSGarrett D'Amore {
3864e9103aaeSGarrett D'Amore 	int c;
3865e9103aaeSGarrett D'Amore 	char *poolname;
3866e9103aaeSGarrett D'Amore 	zpool_handle_t *zhp;
3867e9103aaeSGarrett D'Amore 	int ret = 0;
3868e9103aaeSGarrett D'Amore 
3869e9103aaeSGarrett D'Amore 	/* check options */
3870e9103aaeSGarrett D'Amore 	while ((c = getopt(argc, argv, "")) != -1) {
3871e9103aaeSGarrett D'Amore 		switch (c) {
3872e9103aaeSGarrett D'Amore 		case '?':
3873e9103aaeSGarrett D'Amore 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3874e9103aaeSGarrett D'Amore 			    optopt);
3875e9103aaeSGarrett D'Amore 			usage(B_FALSE);
3876e9103aaeSGarrett D'Amore 		}
3877e9103aaeSGarrett D'Amore 	}
3878e9103aaeSGarrett D'Amore 
3879e9103aaeSGarrett D'Amore 	argc -= optind;
3880e9103aaeSGarrett D'Amore 	argv += optind;
3881e9103aaeSGarrett D'Amore 
3882e9103aaeSGarrett D'Amore 	/* get pool name and check number of arguments */
3883e9103aaeSGarrett D'Amore 	if (argc < 1) {
3884e9103aaeSGarrett D'Amore 		(void) fprintf(stderr, gettext("missing pool name\n"));
3885e9103aaeSGarrett D'Amore 		usage(B_FALSE);
3886e9103aaeSGarrett D'Amore 	}
3887e9103aaeSGarrett D'Amore 
3888e9103aaeSGarrett D'Amore 	if (argc > 1) {
3889e9103aaeSGarrett D'Amore 		(void) fprintf(stderr, gettext("too many arguments\n"));
3890e9103aaeSGarrett D'Amore 		usage(B_FALSE);
3891e9103aaeSGarrett D'Amore 	}
3892e9103aaeSGarrett D'Amore 
3893e9103aaeSGarrett D'Amore 	poolname = argv[0];
3894e9103aaeSGarrett D'Amore 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3895e9103aaeSGarrett D'Amore 		return (1);
3896e9103aaeSGarrett D'Amore 
3897e9103aaeSGarrett D'Amore 	ret = zpool_reguid(zhp);
3898e9103aaeSGarrett D'Amore 
3899e9103aaeSGarrett D'Amore 	zpool_close(zhp);
3900e9103aaeSGarrett D'Amore 	return (ret);
3901e9103aaeSGarrett D'Amore }
3902e9103aaeSGarrett D'Amore 
3903e9103aaeSGarrett D'Amore 
39044263d13fSGeorge Wilson /*
39054263d13fSGeorge Wilson  * zpool reopen <pool>
39064263d13fSGeorge Wilson  *
39074263d13fSGeorge Wilson  * Reopen the pool so that the kernel can update the sizes of all vdevs.
39084263d13fSGeorge Wilson  */
39094263d13fSGeorge Wilson int
39104263d13fSGeorge Wilson zpool_do_reopen(int argc, char **argv)
39114263d13fSGeorge Wilson {
391231d7e8faSGeorge Wilson 	int c;
39134263d13fSGeorge Wilson 	int ret = 0;
39144263d13fSGeorge Wilson 	zpool_handle_t *zhp;
39154263d13fSGeorge Wilson 	char *pool;
39164263d13fSGeorge Wilson 
391731d7e8faSGeorge Wilson 	/* check options */
391831d7e8faSGeorge Wilson 	while ((c = getopt(argc, argv, "")) != -1) {
391931d7e8faSGeorge Wilson 		switch (c) {
392031d7e8faSGeorge Wilson 		case '?':
392131d7e8faSGeorge Wilson 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
392231d7e8faSGeorge Wilson 			    optopt);
392331d7e8faSGeorge Wilson 			usage(B_FALSE);
392431d7e8faSGeorge Wilson 		}
392531d7e8faSGeorge Wilson 	}
392631d7e8faSGeorge Wilson 
39274263d13fSGeorge Wilson 	argc--;
39284263d13fSGeorge Wilson 	argv++;
39294263d13fSGeorge Wilson 
393031d7e8faSGeorge Wilson 	if (argc < 1) {
393131d7e8faSGeorge Wilson 		(void) fprintf(stderr, gettext("missing pool name\n"));
393231d7e8faSGeorge Wilson 		usage(B_FALSE);
393331d7e8faSGeorge Wilson 	}
393431d7e8faSGeorge Wilson 
393531d7e8faSGeorge Wilson 	if (argc > 1) {
393631d7e8faSGeorge Wilson 		(void) fprintf(stderr, gettext("too many arguments\n"));
393731d7e8faSGeorge Wilson 		usage(B_FALSE);
393831d7e8faSGeorge Wilson 	}
39394263d13fSGeorge Wilson 
39404263d13fSGeorge Wilson 	pool = argv[0];
39414263d13fSGeorge Wilson 	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
39424263d13fSGeorge Wilson 		return (1);
39434263d13fSGeorge Wilson 
39444263d13fSGeorge Wilson 	ret = zpool_reopen(zhp);
39454263d13fSGeorge Wilson 	zpool_close(zhp);
39464263d13fSGeorge Wilson 	return (ret);
39474263d13fSGeorge Wilson }
39484263d13fSGeorge Wilson 
3949fa9e4066Sahrens typedef struct scrub_cbdata {
3950fa9e4066Sahrens 	int	cb_type;
395106eeb2adSek 	int	cb_argc;
395206eeb2adSek 	char	**cb_argv;
39531702cce7SAlek Pinchuk 	pool_scrub_cmd_t cb_scrub_cmd;
3954fa9e4066Sahrens } scrub_cbdata_t;
3955fa9e4066Sahrens 
3956fa9e4066Sahrens int
3957fa9e4066Sahrens scrub_callback(zpool_handle_t *zhp, void *data)
3958fa9e4066Sahrens {
3959fa9e4066Sahrens 	scrub_cbdata_t *cb = data;
396006eeb2adSek 	int err;
3961fa9e4066Sahrens 
3962ea8dc4b6Seschrock 	/*
3963ea8dc4b6Seschrock 	 * Ignore faulted pools.
3964ea8dc4b6Seschrock 	 */
3965ea8dc4b6Seschrock 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3966ea8dc4b6Seschrock 		(void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3967ea8dc4b6Seschrock 		    "currently unavailable\n"), zpool_get_name(zhp));
3968ea8dc4b6Seschrock 		return (1);
3969ea8dc4b6Seschrock 	}
3970ea8dc4b6Seschrock 
39711702cce7SAlek Pinchuk 	err = zpool_scan(zhp, cb->cb_type, cb->cb_scrub_cmd);
397206eeb2adSek 
397306eeb2adSek 	return (err != 0);
3974fa9e4066Sahrens }
3975fa9e4066Sahrens 
3976fa9e4066Sahrens /*
39771702cce7SAlek Pinchuk  * zpool scrub [-s | -p] <pool> ...
3978fa9e4066Sahrens  *
3979fa9e4066Sahrens  *	-s	Stop.  Stops any in-progress scrub.
39801702cce7SAlek Pinchuk  *	-p	Pause. Pause in-progress scrub.
3981fa9e4066Sahrens  */
3982fa9e4066Sahrens int
3983fa9e4066Sahrens zpool_do_scrub(int argc, char **argv)
3984fa9e4066Sahrens {
3985fa9e4066Sahrens 	int c;
3986fa9e4066Sahrens 	scrub_cbdata_t cb;
3987fa9e4066Sahrens 
39883f9d6ad7SLin Ling 	cb.cb_type = POOL_SCAN_SCRUB;
39891702cce7SAlek Pinchuk 	cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
3990fa9e4066Sahrens 
3991fa9e4066Sahrens 	/* check options */
39921702cce7SAlek Pinchuk 	while ((c = getopt(argc, argv, "sp")) != -1) {
3993fa9e4066Sahrens 		switch (c) {
3994fa9e4066Sahrens 		case 's':
39953f9d6ad7SLin Ling 			cb.cb_type = POOL_SCAN_NONE;
3996fa9e4066Sahrens 			break;
39971702cce7SAlek Pinchuk 		case 'p':
39981702cce7SAlek Pinchuk 			cb.cb_scrub_cmd = POOL_SCRUB_PAUSE;
39991702cce7SAlek Pinchuk 			break;
4000fa9e4066Sahrens 		case '?':
4001fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4002fa9e4066Sahrens 			    optopt);
400399653d4eSeschrock 			usage(B_FALSE);
4004fa9e4066Sahrens 		}
4005fa9e4066Sahrens 	}
4006fa9e4066Sahrens 
40071702cce7SAlek Pinchuk 	if (cb.cb_type == POOL_SCAN_NONE &&
40081702cce7SAlek Pinchuk 	    cb.cb_scrub_cmd == POOL_SCRUB_PAUSE) {
40091702cce7SAlek Pinchuk 		(void) fprintf(stderr, gettext("invalid option combination: "
40101702cce7SAlek Pinchuk 		    "-s and -p are mutually exclusive\n"));
40111702cce7SAlek Pinchuk 		usage(B_FALSE);
40121702cce7SAlek Pinchuk 	}
40131702cce7SAlek Pinchuk 
401406eeb2adSek 	cb.cb_argc = argc;
401506eeb2adSek 	cb.cb_argv = argv;
4016fa9e4066Sahrens 	argc -= optind;
4017fa9e4066Sahrens 	argv += optind;
4018fa9e4066Sahrens 
4019fa9e4066Sahrens 	if (argc < 1) {
4020fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
402199653d4eSeschrock 		usage(B_FALSE);
4022fa9e4066Sahrens 	}
4023fa9e4066Sahrens 
4024b1b8ab34Slling 	return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
4025fa9e4066Sahrens }
4026fa9e4066Sahrens 
4027fa9e4066Sahrens typedef struct status_cbdata {
402899653d4eSeschrock 	int		cb_count;
4029e9dbad6fSeschrock 	boolean_t	cb_allpools;
403099653d4eSeschrock 	boolean_t	cb_verbose;
403199653d4eSeschrock 	boolean_t	cb_explain;
403299653d4eSeschrock 	boolean_t	cb_first;
40339eb19f4dSGeorge Wilson 	boolean_t	cb_dedup_stats;
4034fa9e4066Sahrens } status_cbdata_t;
4035fa9e4066Sahrens 
4036fa9e4066Sahrens /*
4037fa9e4066Sahrens  * Print out detailed scrub status.
4038fa9e4066Sahrens  */
40395cabbc6bSPrashanth Sreenivasa static void
40403f9d6ad7SLin Ling print_scan_status(pool_scan_stat_t *ps)
4041fa9e4066Sahrens {
40421702cce7SAlek Pinchuk 	time_t start, end, pause;
404373d314ceSLin Ling 	uint64_t elapsed, mins_left, hours_left;
40443f9d6ad7SLin Ling 	uint64_t pass_exam, examined, total;
40453f9d6ad7SLin Ling 	uint_t rate;
4046fa9e4066Sahrens 	double fraction_done;
40473f9d6ad7SLin Ling 	char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
4048fa9e4066Sahrens 
4049ce72e614SYuri Pankov 	(void) printf(gettext("  scan: "));
4050fa9e4066Sahrens 
40513f9d6ad7SLin Ling 	/* If there's never been a scan, there's not much to say. */
40523f9d6ad7SLin Ling 	if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
40533f9d6ad7SLin Ling 	    ps->pss_func >= POOL_SCAN_FUNCS) {
4054fa9e4066Sahrens 		(void) printf(gettext("none requested\n"));
4055fa9e4066Sahrens 		return;
4056fa9e4066Sahrens 	}
4057fa9e4066Sahrens 
40583f9d6ad7SLin Ling 	start = ps->pss_start_time;
40593f9d6ad7SLin Ling 	end = ps->pss_end_time;
40601702cce7SAlek Pinchuk 	pause = ps->pss_pass_scrub_pause;
40613f9d6ad7SLin Ling 	zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
4062fa9e4066Sahrens 
40633f9d6ad7SLin Ling 	assert(ps->pss_func == POOL_SCAN_SCRUB ||
40643f9d6ad7SLin Ling 	    ps->pss_func == POOL_SCAN_RESILVER);
40653f9d6ad7SLin Ling 	/*
40663f9d6ad7SLin Ling 	 * Scan is finished or canceled.
40673f9d6ad7SLin Ling 	 */
40683f9d6ad7SLin Ling 	if (ps->pss_state == DSS_FINISHED) {
40693f9d6ad7SLin Ling 		uint64_t minutes_taken = (end - start) / 60;
4070b327cd3fSIgor Kozhukhov 		char *fmt = NULL;
40713f9d6ad7SLin Ling 
40723f9d6ad7SLin Ling 		if (ps->pss_func == POOL_SCAN_SCRUB) {
40733f9d6ad7SLin Ling 			fmt = gettext("scrub repaired %s in %lluh%um with "
40743f9d6ad7SLin Ling 			    "%llu errors on %s");
40753f9d6ad7SLin Ling 		} else if (ps->pss_func == POOL_SCAN_RESILVER) {
40763f9d6ad7SLin Ling 			fmt = gettext("resilvered %s in %lluh%um with "
40773f9d6ad7SLin Ling 			    "%llu errors on %s");
40783f9d6ad7SLin Ling 		}
40793f9d6ad7SLin Ling 		/* LINTED */
40803f9d6ad7SLin Ling 		(void) printf(fmt, processed_buf,
408118ce54dfSek 		    (u_longlong_t)(minutes_taken / 60),
408218ce54dfSek 		    (uint_t)(minutes_taken % 60),
40833f9d6ad7SLin Ling 		    (u_longlong_t)ps->pss_errors,
40843f9d6ad7SLin Ling 		    ctime((time_t *)&end));
40853f9d6ad7SLin Ling 		return;
40863f9d6ad7SLin Ling 	} else if (ps->pss_state == DSS_CANCELED) {
40873f9d6ad7SLin Ling 		if (ps->pss_func == POOL_SCAN_SCRUB) {
40883f9d6ad7SLin Ling 			(void) printf(gettext("scrub canceled on %s"),
40893f9d6ad7SLin Ling 			    ctime(&end));
40903f9d6ad7SLin Ling 		} else if (ps->pss_func == POOL_SCAN_RESILVER) {
40913f9d6ad7SLin Ling 			(void) printf(gettext("resilver canceled on %s"),
40923f9d6ad7SLin Ling 			    ctime(&end));
40933f9d6ad7SLin Ling 		}
4094fa9e4066Sahrens 		return;
4095fa9e4066Sahrens 	}
4096fa9e4066Sahrens 
40973f9d6ad7SLin Ling 	assert(ps->pss_state == DSS_SCANNING);
40983f9d6ad7SLin Ling 
40993f9d6ad7SLin Ling 	/*
41003f9d6ad7SLin Ling 	 * Scan is in progress.
41013f9d6ad7SLin Ling 	 */
41023f9d6ad7SLin Ling 	if (ps->pss_func == POOL_SCAN_SCRUB) {
41031702cce7SAlek Pinchuk 		if (pause == 0) {
41041702cce7SAlek Pinchuk 			(void) printf(gettext("scrub in progress since %s"),
41051702cce7SAlek Pinchuk 			    ctime(&start));
41061702cce7SAlek Pinchuk 		} else {
41071702cce7SAlek Pinchuk 			char buf[32];
41081702cce7SAlek Pinchuk 			struct tm *p = localtime(&pause);
41091702cce7SAlek Pinchuk 			(void) strftime(buf, sizeof (buf), "%a %b %e %T %Y", p);
41101702cce7SAlek Pinchuk 			(void) printf(gettext("scrub paused since %s\n"), buf);
41111702cce7SAlek Pinchuk 			(void) printf(gettext("\tscrub started on   %s"),
41121702cce7SAlek Pinchuk 			    ctime(&start));
41131702cce7SAlek Pinchuk 		}
41143f9d6ad7SLin Ling 	} else if (ps->pss_func == POOL_SCAN_RESILVER) {
41153f9d6ad7SLin Ling 		(void) printf(gettext("resilver in progress since %s"),
41163f9d6ad7SLin Ling 		    ctime(&start));
41173f9d6ad7SLin Ling 	}
4118fa9e4066Sahrens 
41193f9d6ad7SLin Ling 	examined = ps->pss_examined ? ps->pss_examined : 1;
41203f9d6ad7SLin Ling 	total = ps->pss_to_examine;
4121fa9e4066Sahrens 	fraction_done = (double)examined / total;
41223f9d6ad7SLin Ling 
41233f9d6ad7SLin Ling 	/* elapsed time for this pass */
41243f9d6ad7SLin Ling 	elapsed = time(NULL) - ps->pss_pass_start;
41251702cce7SAlek Pinchuk 	elapsed -= ps->pss_pass_scrub_spent_paused;
41263f9d6ad7SLin Ling 	elapsed = elapsed ? elapsed : 1;
41273f9d6ad7SLin Ling 	pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
41283f9d6ad7SLin Ling 	rate = pass_exam / elapsed;
41293f9d6ad7SLin Ling 	rate = rate ? rate : 1;
41303f9d6ad7SLin Ling 	mins_left = ((total - examined) / rate) / 60;
413173d314ceSLin Ling 	hours_left = mins_left / 60;
41323f9d6ad7SLin Ling 
41333f9d6ad7SLin Ling 	zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
41343f9d6ad7SLin Ling 	zfs_nicenum(total, total_buf, sizeof (total_buf));
41353f9d6ad7SLin Ling 
413673d314ceSLin Ling 	/*
413773d314ceSLin Ling 	 * do not print estimated time if hours_left is more than 30 days
41381702cce7SAlek Pinchuk 	 * or we have a paused scrub
413973d314ceSLin Ling 	 */
41401702cce7SAlek Pinchuk 	if (pause == 0) {
41411702cce7SAlek Pinchuk 		zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
41421702cce7SAlek Pinchuk 		(void) printf(gettext("\t%s scanned out of %s at %s/s"),
41431702cce7SAlek Pinchuk 		    examined_buf, total_buf, rate_buf);
41441702cce7SAlek Pinchuk 		if (hours_left < (30 * 24)) {
41451702cce7SAlek Pinchuk 			(void) printf(gettext(", %lluh%um to go\n"),
41461702cce7SAlek Pinchuk 			    (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
41471702cce7SAlek Pinchuk 		} else {
41481702cce7SAlek Pinchuk 			(void) printf(gettext(
41491702cce7SAlek Pinchuk 			    ", (scan is slow, no estimated time)\n"));
41501702cce7SAlek Pinchuk 		}
415173d314ceSLin Ling 	} else {
41521702cce7SAlek Pinchuk 		(void) printf(gettext("\t%s scanned out of %s\n"),
41531702cce7SAlek Pinchuk 		    examined_buf, total_buf);
415473d314ceSLin Ling 	}
41553f9d6ad7SLin Ling 
41563f9d6ad7SLin Ling 	if (ps->pss_func == POOL_SCAN_RESILVER) {
41573f9d6ad7SLin Ling 		(void) printf(gettext("    %s resilvered, %.2f%% done\n"),
41583f9d6ad7SLin Ling 		    processed_buf, 100 * fraction_done);
41593f9d6ad7SLin Ling 	} else if (ps->pss_func == POOL_SCAN_SCRUB) {
41603f9d6ad7SLin Ling 		(void) printf(gettext("    %s repaired, %.2f%% done\n"),
41613f9d6ad7SLin Ling 		    processed_buf, 100 * fraction_done);
41623f9d6ad7SLin Ling 	}
4163fa9e4066Sahrens }
4164fa9e4066Sahrens 
41655cabbc6bSPrashanth Sreenivasa /*
41665cabbc6bSPrashanth Sreenivasa  * Print out detailed removal status.
41675cabbc6bSPrashanth Sreenivasa  */
41685cabbc6bSPrashanth Sreenivasa static void
41695cabbc6bSPrashanth Sreenivasa print_removal_status(zpool_handle_t *zhp, pool_removal_stat_t *prs)
41705cabbc6bSPrashanth Sreenivasa {
41715cabbc6bSPrashanth Sreenivasa 	char copied_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
41725cabbc6bSPrashanth Sreenivasa 	time_t start, end;
41735cabbc6bSPrashanth Sreenivasa 	nvlist_t *config, *nvroot;
41745cabbc6bSPrashanth Sreenivasa 	nvlist_t **child;
41755cabbc6bSPrashanth Sreenivasa 	uint_t children;
41765cabbc6bSPrashanth Sreenivasa 	char *vdev_name;
41775cabbc6bSPrashanth Sreenivasa 
41785cabbc6bSPrashanth Sreenivasa 	if (prs == NULL || prs->prs_state == DSS_NONE)
41795cabbc6bSPrashanth Sreenivasa 		return;
41805cabbc6bSPrashanth Sreenivasa 
41815cabbc6bSPrashanth Sreenivasa 	/*
41825cabbc6bSPrashanth Sreenivasa 	 * Determine name of vdev.
41835cabbc6bSPrashanth Sreenivasa 	 */
41845cabbc6bSPrashanth Sreenivasa 	config = zpool_get_config(zhp, NULL);
41855cabbc6bSPrashanth Sreenivasa 	nvroot = fnvlist_lookup_nvlist(config,
41865cabbc6bSPrashanth Sreenivasa 	    ZPOOL_CONFIG_VDEV_TREE);
41875cabbc6bSPrashanth Sreenivasa 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
41885cabbc6bSPrashanth Sreenivasa 	    &child, &children) == 0);
41895cabbc6bSPrashanth Sreenivasa 	assert(prs->prs_removing_vdev < children);
41905cabbc6bSPrashanth Sreenivasa 	vdev_name = zpool_vdev_name(g_zfs, zhp,
41915cabbc6bSPrashanth Sreenivasa 	    child[prs->prs_removing_vdev], B_TRUE);
41925cabbc6bSPrashanth Sreenivasa 
41935cabbc6bSPrashanth Sreenivasa 	(void) printf(gettext("remove: "));
41945cabbc6bSPrashanth Sreenivasa 
41955cabbc6bSPrashanth Sreenivasa 	start = prs->prs_start_time;
41965cabbc6bSPrashanth Sreenivasa 	end = prs->prs_end_time;
41975cabbc6bSPrashanth Sreenivasa 	zfs_nicenum(prs->prs_copied, copied_buf, sizeof (copied_buf));
41985cabbc6bSPrashanth Sreenivasa 
41995cabbc6bSPrashanth Sreenivasa 	/*
42005cabbc6bSPrashanth Sreenivasa 	 * Removal is finished or canceled.
42015cabbc6bSPrashanth Sreenivasa 	 */
42025cabbc6bSPrashanth Sreenivasa 	if (prs->prs_state == DSS_FINISHED) {
42035cabbc6bSPrashanth Sreenivasa 		uint64_t minutes_taken = (end - start) / 60;
42045cabbc6bSPrashanth Sreenivasa 
42055cabbc6bSPrashanth Sreenivasa 		(void) printf(gettext("Removal of vdev %llu copied %s "
42065cabbc6bSPrashanth Sreenivasa 		    "in %lluh%um, completed on %s"),
42075cabbc6bSPrashanth Sreenivasa 		    (longlong_t)prs->prs_removing_vdev,
42085cabbc6bSPrashanth Sreenivasa 		    copied_buf,
42095cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)(minutes_taken / 60),
42105cabbc6bSPrashanth Sreenivasa 		    (uint_t)(minutes_taken % 60),
42115cabbc6bSPrashanth Sreenivasa 		    ctime((time_t *)&end));
42125cabbc6bSPrashanth Sreenivasa 	} else if (prs->prs_state == DSS_CANCELED) {
42135cabbc6bSPrashanth Sreenivasa 		(void) printf(gettext("Removal of %s canceled on %s"),
42145cabbc6bSPrashanth Sreenivasa 		    vdev_name, ctime(&end));
42155cabbc6bSPrashanth Sreenivasa 	} else {
42165cabbc6bSPrashanth Sreenivasa 		uint64_t copied, total, elapsed, mins_left, hours_left;
42175cabbc6bSPrashanth Sreenivasa 		double fraction_done;
42185cabbc6bSPrashanth Sreenivasa 		uint_t rate;
42195cabbc6bSPrashanth Sreenivasa 
42205cabbc6bSPrashanth Sreenivasa 		assert(prs->prs_state == DSS_SCANNING);
42215cabbc6bSPrashanth Sreenivasa 
42225cabbc6bSPrashanth Sreenivasa 		/*
42235cabbc6bSPrashanth Sreenivasa 		 * Removal is in progress.
42245cabbc6bSPrashanth Sreenivasa 		 */
42255cabbc6bSPrashanth Sreenivasa 		(void) printf(gettext(
42265cabbc6bSPrashanth Sreenivasa 		    "Evacuation of %s in progress since %s"),
42275cabbc6bSPrashanth Sreenivasa 		    vdev_name, ctime(&start));
42285cabbc6bSPrashanth Sreenivasa 
42295cabbc6bSPrashanth Sreenivasa 		copied = prs->prs_copied > 0 ? prs->prs_copied : 1;
42305cabbc6bSPrashanth Sreenivasa 		total = prs->prs_to_copy;
42315cabbc6bSPrashanth Sreenivasa 		fraction_done = (double)copied / total;
42325cabbc6bSPrashanth Sreenivasa 
42335cabbc6bSPrashanth Sreenivasa 		/* elapsed time for this pass */
42345cabbc6bSPrashanth Sreenivasa 		elapsed = time(NULL) - prs->prs_start_time;
42355cabbc6bSPrashanth Sreenivasa 		elapsed = elapsed > 0 ? elapsed : 1;
42365cabbc6bSPrashanth Sreenivasa 		rate = copied / elapsed;
42375cabbc6bSPrashanth Sreenivasa 		rate = rate > 0 ? rate : 1;
42385cabbc6bSPrashanth Sreenivasa 		mins_left = ((total - copied) / rate) / 60;
42395cabbc6bSPrashanth Sreenivasa 		hours_left = mins_left / 60;
42405cabbc6bSPrashanth Sreenivasa 
42415cabbc6bSPrashanth Sreenivasa 		zfs_nicenum(copied, examined_buf, sizeof (examined_buf));
42425cabbc6bSPrashanth Sreenivasa 		zfs_nicenum(total, total_buf, sizeof (total_buf));
42435cabbc6bSPrashanth Sreenivasa 		zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
42445cabbc6bSPrashanth Sreenivasa 
42455cabbc6bSPrashanth Sreenivasa 		/*
42465cabbc6bSPrashanth Sreenivasa 		 * do not print estimated time if hours_left is more than
42475cabbc6bSPrashanth Sreenivasa 		 * 30 days
42485cabbc6bSPrashanth Sreenivasa 		 */
42495cabbc6bSPrashanth Sreenivasa 		(void) printf(gettext("    %s copied out of %s at %s/s, "
42505cabbc6bSPrashanth Sreenivasa 		    "%.2f%% done"),
42515cabbc6bSPrashanth Sreenivasa 		    examined_buf, total_buf, rate_buf, 100 * fraction_done);
42525cabbc6bSPrashanth Sreenivasa 		if (hours_left < (30 * 24)) {
42535cabbc6bSPrashanth Sreenivasa 			(void) printf(gettext(", %lluh%um to go\n"),
42545cabbc6bSPrashanth Sreenivasa 			    (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
42555cabbc6bSPrashanth Sreenivasa 		} else {
42565cabbc6bSPrashanth Sreenivasa 			(void) printf(gettext(
42575cabbc6bSPrashanth Sreenivasa 			    ", (copy is slow, no estimated time)\n"));
42585cabbc6bSPrashanth Sreenivasa 		}
42595cabbc6bSPrashanth Sreenivasa 	}
42605cabbc6bSPrashanth Sreenivasa 
42615cabbc6bSPrashanth Sreenivasa 	if (prs->prs_mapping_memory > 0) {
42625cabbc6bSPrashanth Sreenivasa 		char mem_buf[7];
42635cabbc6bSPrashanth Sreenivasa 		zfs_nicenum(prs->prs_mapping_memory, mem_buf, sizeof (mem_buf));
42645cabbc6bSPrashanth Sreenivasa 		(void) printf(gettext("    %s memory used for "
42655cabbc6bSPrashanth Sreenivasa 		    "removed device mappings\n"),
42665cabbc6bSPrashanth Sreenivasa 		    mem_buf);
42675cabbc6bSPrashanth Sreenivasa 	}
42685cabbc6bSPrashanth Sreenivasa }
42695cabbc6bSPrashanth Sreenivasa 
4270ea8dc4b6Seschrock static void
4271ea8dc4b6Seschrock print_error_log(zpool_handle_t *zhp)
4272ea8dc4b6Seschrock {
427375519f38Sek 	nvlist_t *nverrlist = NULL;
427455434c77Sek 	nvpair_t *elem;
427555434c77Sek 	char *pathname;
427655434c77Sek 	size_t len = MAXPATHLEN * 2;
4277ea8dc4b6Seschrock 
427855434c77Sek 	if (zpool_get_errlog(zhp, &nverrlist) != 0) {
4279ea8dc4b6Seschrock 		(void) printf("errors: List of errors unavailable "
4280ea8dc4b6Seschrock 		    "(insufficient privileges)\n");
4281ea8dc4b6Seschrock 		return;
4282ea8dc4b6Seschrock 	}
4283ea8dc4b6Seschrock 
428455434c77Sek 	(void) printf("errors: Permanent errors have been "
428555434c77Sek 	    "detected in the following files:\n\n");
4286ea8dc4b6Seschrock 
428755434c77Sek 	pathname = safe_malloc(len);
428855434c77Sek 	elem = NULL;
428955434c77Sek 	while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
429055434c77Sek 		nvlist_t *nv;
429155434c77Sek 		uint64_t dsobj, obj;
429255434c77Sek 
429355434c77Sek 		verify(nvpair_value_nvlist(elem, &nv) == 0);
429455434c77Sek 		verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
429555434c77Sek 		    &dsobj) == 0);
429655434c77Sek 		verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
429755434c77Sek 		    &obj) == 0);
429855434c77Sek 		zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
429955434c77Sek 		(void) printf("%7s %s\n", "", pathname);
430055434c77Sek 	}
430155434c77Sek 	free(pathname);
430255434c77Sek 	nvlist_free(nverrlist);
4303ea8dc4b6Seschrock }
4304ea8dc4b6Seschrock 
430599653d4eSeschrock static void
430699653d4eSeschrock print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
430799653d4eSeschrock     int namewidth)
430899653d4eSeschrock {
430999653d4eSeschrock 	uint_t i;
431099653d4eSeschrock 	char *name;
431199653d4eSeschrock 
431299653d4eSeschrock 	if (nspares == 0)
431399653d4eSeschrock 		return;
431499653d4eSeschrock 
431599653d4eSeschrock 	(void) printf(gettext("\tspares\n"));
431699653d4eSeschrock 
431799653d4eSeschrock 	for (i = 0; i < nspares; i++) {
431888ecc943SGeorge Wilson 		name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
431999653d4eSeschrock 		print_status_config(zhp, name, spares[i],
4320aa8cf21aSNeil Perrin 		    namewidth, 2, B_TRUE);
432199653d4eSeschrock 		free(name);
432299653d4eSeschrock 	}
432399653d4eSeschrock }
432499653d4eSeschrock 
4325fa94a07fSbrendan static void
4326fa94a07fSbrendan print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
4327fa94a07fSbrendan     int namewidth)
4328fa94a07fSbrendan {
4329fa94a07fSbrendan 	uint_t i;
4330fa94a07fSbrendan 	char *name;
4331fa94a07fSbrendan 
4332fa94a07fSbrendan 	if (nl2cache == 0)
4333fa94a07fSbrendan 		return;
4334fa94a07fSbrendan 
4335fa94a07fSbrendan 	(void) printf(gettext("\tcache\n"));
4336fa94a07fSbrendan 
4337fa94a07fSbrendan 	for (i = 0; i < nl2cache; i++) {
433888ecc943SGeorge Wilson 		name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
4339fa94a07fSbrendan 		print_status_config(zhp, name, l2cache[i],
4340aa8cf21aSNeil Perrin 		    namewidth, 2, B_FALSE);
4341aa8cf21aSNeil Perrin 		free(name);
4342aa8cf21aSNeil Perrin 	}
4343aa8cf21aSNeil Perrin }
4344aa8cf21aSNeil Perrin 
43459eb19f4dSGeorge Wilson static void
43469eb19f4dSGeorge Wilson print_dedup_stats(nvlist_t *config)
43479eb19f4dSGeorge Wilson {
43489eb19f4dSGeorge Wilson 	ddt_histogram_t *ddh;
43499eb19f4dSGeorge Wilson 	ddt_stat_t *dds;
43509eb19f4dSGeorge Wilson 	ddt_object_t *ddo;
43519eb19f4dSGeorge Wilson 	uint_t c;
43529eb19f4dSGeorge Wilson 
43539eb19f4dSGeorge Wilson 	/*
43549eb19f4dSGeorge Wilson 	 * If the pool was faulted then we may not have been able to
43552384d9f8SGeorge Wilson 	 * obtain the config. Otherwise, if we have anything in the dedup
43569eb19f4dSGeorge Wilson 	 * table continue processing the stats.
43579eb19f4dSGeorge Wilson 	 */
43589eb19f4dSGeorge Wilson 	if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
4359ce72e614SYuri Pankov 	    (uint64_t **)&ddo, &c) != 0)
43609eb19f4dSGeorge Wilson 		return;
43619eb19f4dSGeorge Wilson 
43629eb19f4dSGeorge Wilson 	(void) printf("\n");
4363ce72e614SYuri Pankov 	(void) printf(gettext(" dedup: "));
4364ce72e614SYuri Pankov 	if (ddo->ddo_count == 0) {
4365ce72e614SYuri Pankov 		(void) printf(gettext("no DDT entries\n"));
4366ce72e614SYuri Pankov 		return;
4367ce72e614SYuri Pankov 	}
4368ce72e614SYuri Pankov 
43699eb19f4dSGeorge Wilson 	(void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
43709eb19f4dSGeorge Wilson 	    (u_longlong_t)ddo->ddo_count,
43719eb19f4dSGeorge Wilson 	    (u_longlong_t)ddo->ddo_dspace,
43729eb19f4dSGeorge Wilson 	    (u_longlong_t)ddo->ddo_mspace);
43739eb19f4dSGeorge Wilson 
43749eb19f4dSGeorge Wilson 	verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
43759eb19f4dSGeorge Wilson 	    (uint64_t **)&dds, &c) == 0);
43769eb19f4dSGeorge Wilson 	verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
43779eb19f4dSGeorge Wilson 	    (uint64_t **)&ddh, &c) == 0);
43789eb19f4dSGeorge Wilson 	zpool_dump_ddt(dds, ddh);
43799eb19f4dSGeorge Wilson }
43809eb19f4dSGeorge Wilson 
4381fa9e4066Sahrens /*
4382fa9e4066Sahrens  * Display a summary of pool status.  Displays a summary such as:
4383fa9e4066Sahrens  *
4384fa9e4066Sahrens  *        pool: tank
4385fa9e4066Sahrens  *	status: DEGRADED
4386fa9e4066Sahrens  *	reason: One or more devices ...
4387654b400cSJoshua M. Clulow  *         see: http://illumos.org/msg/ZFS-xxxx-01
4388fa9e4066Sahrens  *	config:
4389fa9e4066Sahrens  *		mirror		DEGRADED
4390fa9e4066Sahrens  *                c1t0d0	OK
4391ea8dc4b6Seschrock  *                c2t0d0	UNAVAIL
4392fa9e4066Sahrens  *
4393fa9e4066Sahrens  * When given the '-v' option, we print out the complete config.  If the '-e'
4394fa9e4066Sahrens  * option is specified, then we print out error rate information as well.
4395fa9e4066Sahrens  */
4396fa9e4066Sahrens int
4397fa9e4066Sahrens status_callback(zpool_handle_t *zhp, void *data)
4398fa9e4066Sahrens {
4399fa9e4066Sahrens 	status_cbdata_t *cbp = data;
4400fa9e4066Sahrens 	nvlist_t *config, *nvroot;
4401fa9e4066Sahrens 	char *msgid;
4402fa9e4066Sahrens 	int reason;
440346657f8dSmmusante 	const char *health;
440446657f8dSmmusante 	uint_t c;
440546657f8dSmmusante 	vdev_stat_t *vs;
4406fa9e4066Sahrens 
4407088e9d47Seschrock 	config = zpool_get_config(zhp, NULL);
4408fa9e4066Sahrens 	reason = zpool_get_status(zhp, &msgid);
4409fa9e4066Sahrens 
4410fa9e4066Sahrens 	cbp->cb_count++;
4411fa9e4066Sahrens 
4412fa9e4066Sahrens 	/*
4413fa9e4066Sahrens 	 * If we were given 'zpool status -x', only report those pools with
4414fa9e4066Sahrens 	 * problems.
4415fa9e4066Sahrens 	 */
4416b3a6f804STim Connors 	if (cbp->cb_explain &&
4417b3a6f804STim Connors 	    (reason == ZPOOL_STATUS_OK ||
4418b3a6f804STim Connors 	    reason == ZPOOL_STATUS_VERSION_OLDER ||
4419b3a6f804STim Connors 	    reason == ZPOOL_STATUS_FEAT_DISABLED)) {
4420e9dbad6fSeschrock 		if (!cbp->cb_allpools) {
4421e9dbad6fSeschrock 			(void) printf(gettext("pool '%s' is healthy\n"),
4422e9dbad6fSeschrock 			    zpool_get_name(zhp));
4423e9dbad6fSeschrock 			if (cbp->cb_first)
4424e9dbad6fSeschrock 				cbp->cb_first = B_FALSE;
4425e9dbad6fSeschrock 		}
4426fa9e4066Sahrens 		return (0);
4427e9dbad6fSeschrock 	}
4428fa9e4066Sahrens 
4429fa9e4066Sahrens 	if (cbp->cb_first)
443099653d4eSeschrock 		cbp->cb_first = B_FALSE;
4431fa9e4066Sahrens 	else
4432fa9e4066Sahrens 		(void) printf("\n");
4433fa9e4066Sahrens 
44345cabbc6bSPrashanth Sreenivasa 	nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
44353f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
443646657f8dSmmusante 	    (uint64_t **)&vs, &c) == 0);
4437990b4856Slling 	health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4438fa9e4066Sahrens 
4439fa9e4066Sahrens 	(void) printf(gettext("  pool: %s\n"), zpool_get_name(zhp));
4440fa9e4066Sahrens 	(void) printf(gettext(" state: %s\n"), health);
4441fa9e4066Sahrens 
4442fa9e4066Sahrens 	switch (reason) {
4443fa9e4066Sahrens 	case ZPOOL_STATUS_MISSING_DEV_R:
4444fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices could not "
4445fa9e4066Sahrens 		    "be opened.  Sufficient replicas exist for\n\tthe pool to "
4446fa9e4066Sahrens 		    "continue functioning in a degraded state.\n"));
4447fa9e4066Sahrens 		(void) printf(gettext("action: Attach the missing device and "
4448fa9e4066Sahrens 		    "online it using 'zpool online'.\n"));
4449fa9e4066Sahrens 		break;
4450fa9e4066Sahrens 
4451fa9e4066Sahrens 	case ZPOOL_STATUS_MISSING_DEV_NR:
4452fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices could not "
4453fa9e4066Sahrens 		    "be opened.  There are insufficient\n\treplicas for the "
4454fa9e4066Sahrens 		    "pool to continue functioning.\n"));
4455fa9e4066Sahrens 		(void) printf(gettext("action: Attach the missing device and "
4456fa9e4066Sahrens 		    "online it using 'zpool online'.\n"));
4457fa9e4066Sahrens 		break;
4458fa9e4066Sahrens 
4459fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_LABEL_R:
4460fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices could not "
4461fa9e4066Sahrens 		    "be used because the label is missing or\n\tinvalid.  "
4462fa9e4066Sahrens 		    "Sufficient replicas exist for the pool to continue\n\t"
4463fa9e4066Sahrens 		    "functioning in a degraded state.\n"));
4464fa9e4066Sahrens 		(void) printf(gettext("action: Replace the device using "
4465fa9e4066Sahrens 		    "'zpool replace'.\n"));
4466fa9e4066Sahrens 		break;
4467fa9e4066Sahrens 
4468fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4469fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices could not "
4470b1b8ab34Slling 		    "be used because the label is missing \n\tor invalid.  "
4471fa9e4066Sahrens 		    "There are insufficient replicas for the pool to "
4472fa9e4066Sahrens 		    "continue\n\tfunctioning.\n"));
4473468c413aSTim Haley 		zpool_explain_recover(zpool_get_handle(zhp),
4474468c413aSTim Haley 		    zpool_get_name(zhp), reason, config);
4475fa9e4066Sahrens 		break;
4476fa9e4066Sahrens 
4477fa9e4066Sahrens 	case ZPOOL_STATUS_FAILING_DEV:
4478fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices has "
4479fa9e4066Sahrens 		    "experienced an unrecoverable error.  An\n\tattempt was "
4480fa9e4066Sahrens 		    "made to correct the error.  Applications are "
4481fa9e4066Sahrens 		    "unaffected.\n"));
4482fa9e4066Sahrens 		(void) printf(gettext("action: Determine if the device needs "
4483fa9e4066Sahrens 		    "to be replaced, and clear the errors\n\tusing "
4484ea8dc4b6Seschrock 		    "'zpool clear' or replace the device with 'zpool "
4485fa9e4066Sahrens 		    "replace'.\n"));
4486fa9e4066Sahrens 		break;
4487fa9e4066Sahrens 
4488fa9e4066Sahrens 	case ZPOOL_STATUS_OFFLINE_DEV:
4489fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices has "
4490d7d4af51Smmusante 		    "been taken offline by the administrator.\n\tSufficient "
4491fa9e4066Sahrens 		    "replicas exist for the pool to continue functioning in "
4492fa9e4066Sahrens 		    "a\n\tdegraded state.\n"));
4493fa9e4066Sahrens 		(void) printf(gettext("action: Online the device using "
4494fa9e4066Sahrens 		    "'zpool online' or replace the device with\n\t'zpool "
4495fa9e4066Sahrens 		    "replace'.\n"));
4496fa9e4066Sahrens 		break;
4497fa9e4066Sahrens 
4498c25309d4SGeorge Wilson 	case ZPOOL_STATUS_REMOVED_DEV:
4499c25309d4SGeorge Wilson 		(void) printf(gettext("status: One or more devices has "
4500c25309d4SGeorge Wilson 		    "been removed by the administrator.\n\tSufficient "
4501c25309d4SGeorge Wilson 		    "replicas exist for the pool to continue functioning in "
4502c25309d4SGeorge Wilson 		    "a\n\tdegraded state.\n"));
4503c25309d4SGeorge Wilson 		(void) printf(gettext("action: Online the device using "
4504c25309d4SGeorge Wilson 		    "'zpool online' or replace the device with\n\t'zpool "
4505c25309d4SGeorge Wilson 		    "replace'.\n"));
4506c25309d4SGeorge Wilson 		break;
4507c25309d4SGeorge Wilson 
4508fa9e4066Sahrens 	case ZPOOL_STATUS_RESILVERING:
4509fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices is "
4510fa9e4066Sahrens 		    "currently being resilvered.  The pool will\n\tcontinue "
4511fa9e4066Sahrens 		    "to function, possibly in a degraded state.\n"));
4512fa9e4066Sahrens 		(void) printf(gettext("action: Wait for the resilver to "
4513fa9e4066Sahrens 		    "complete.\n"));
4514fa9e4066Sahrens 		break;
4515fa9e4066Sahrens 
4516ea8dc4b6Seschrock 	case ZPOOL_STATUS_CORRUPT_DATA:
4517ea8dc4b6Seschrock 		(void) printf(gettext("status: One or more devices has "
4518ea8dc4b6Seschrock 		    "experienced an error resulting in data\n\tcorruption.  "
4519ea8dc4b6Seschrock 		    "Applications may be affected.\n"));
4520ea8dc4b6Seschrock 		(void) printf(gettext("action: Restore the file in question "
4521ea8dc4b6Seschrock 		    "if possible.  Otherwise restore the\n\tentire pool from "
4522ea8dc4b6Seschrock 		    "backup.\n"));
4523ea8dc4b6Seschrock 		break;
4524ea8dc4b6Seschrock 
4525ea8dc4b6Seschrock 	case ZPOOL_STATUS_CORRUPT_POOL:
4526ea8dc4b6Seschrock 		(void) printf(gettext("status: The pool metadata is corrupted "
4527ea8dc4b6Seschrock 		    "and the pool cannot be opened.\n"));
4528468c413aSTim Haley 		zpool_explain_recover(zpool_get_handle(zhp),
4529468c413aSTim Haley 		    zpool_get_name(zhp), reason, config);
4530ea8dc4b6Seschrock 		break;
4531ea8dc4b6Seschrock 
4532eaca9bbdSeschrock 	case ZPOOL_STATUS_VERSION_OLDER:
453357221772SChristopher Siden 		(void) printf(gettext("status: The pool is formatted using a "
453457221772SChristopher Siden 		    "legacy on-disk format.  The pool can\n\tstill be used, "
453557221772SChristopher Siden 		    "but some features are unavailable.\n"));
4536eaca9bbdSeschrock 		(void) printf(gettext("action: Upgrade the pool using 'zpool "
4537eaca9bbdSeschrock 		    "upgrade'.  Once this is done, the\n\tpool will no longer "
453857221772SChristopher Siden 		    "be accessible on software that does not support feature\n"
453957221772SChristopher Siden 		    "\tflags.\n"));
4540eaca9bbdSeschrock 		break;
4541eaca9bbdSeschrock 
4542eaca9bbdSeschrock 	case ZPOOL_STATUS_VERSION_NEWER:
4543eaca9bbdSeschrock 		(void) printf(gettext("status: The pool has been upgraded to a "
4544eaca9bbdSeschrock 		    "newer, incompatible on-disk version.\n\tThe pool cannot "
4545eaca9bbdSeschrock 		    "be accessed on this system.\n"));
4546eaca9bbdSeschrock 		(void) printf(gettext("action: Access the pool from a system "
4547eaca9bbdSeschrock 		    "running more recent software, or\n\trestore the pool from "
4548eaca9bbdSeschrock 		    "backup.\n"));
4549eaca9bbdSeschrock 		break;
4550eaca9bbdSeschrock 
455157221772SChristopher Siden 	case ZPOOL_STATUS_FEAT_DISABLED:
455257221772SChristopher Siden 		(void) printf(gettext("status: Some supported features are not "
455357221772SChristopher Siden 		    "enabled on the pool. The pool can\n\tstill be used, but "
455457221772SChristopher Siden 		    "some features are unavailable.\n"));
455557221772SChristopher Siden 		(void) printf(gettext("action: Enable all features using "
455657221772SChristopher Siden 		    "'zpool upgrade'. Once this is done,\n\tthe pool may no "
455757221772SChristopher Siden 		    "longer be accessible by software that does not support\n\t"
455857221772SChristopher Siden 		    "the features. See zpool-features(5) for details.\n"));
455957221772SChristopher Siden 		break;
456057221772SChristopher Siden 
4561ad135b5dSChristopher Siden 	case ZPOOL_STATUS_UNSUP_FEAT_READ:
4562ad135b5dSChristopher Siden 		(void) printf(gettext("status: The pool cannot be accessed on "
4563ad135b5dSChristopher Siden 		    "this system because it uses the\n\tfollowing feature(s) "
4564ad135b5dSChristopher Siden 		    "not supported on this system:\n"));
4565ad135b5dSChristopher Siden 		zpool_print_unsup_feat(config);
4566ad135b5dSChristopher Siden 		(void) printf("\n");
4567ad135b5dSChristopher Siden 		(void) printf(gettext("action: Access the pool from a system "
4568ad135b5dSChristopher Siden 		    "that supports the required feature(s),\n\tor restore the "
4569ad135b5dSChristopher Siden 		    "pool from backup.\n"));
4570ad135b5dSChristopher Siden 		break;
4571ad135b5dSChristopher Siden 
4572ad135b5dSChristopher Siden 	case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4573ad135b5dSChristopher Siden 		(void) printf(gettext("status: The pool can only be accessed "
4574ad135b5dSChristopher Siden 		    "in read-only mode on this system. It\n\tcannot be "
4575ad135b5dSChristopher Siden 		    "accessed in read-write mode because it uses the "
4576ad135b5dSChristopher Siden 		    "following\n\tfeature(s) not supported on this system:\n"));
4577ad135b5dSChristopher Siden 		zpool_print_unsup_feat(config);
4578ad135b5dSChristopher Siden 		(void) printf("\n");
4579ad135b5dSChristopher Siden 		(void) printf(gettext("action: The pool cannot be accessed in "
4580ad135b5dSChristopher Siden 		    "read-write mode. Import the pool with\n"
4581ad135b5dSChristopher Siden 		    "\t\"-o readonly=on\", access the pool from a system that "
4582ad135b5dSChristopher Siden 		    "supports the\n\trequired feature(s), or restore the "
4583ad135b5dSChristopher Siden 		    "pool from backup.\n"));
4584ad135b5dSChristopher Siden 		break;
4585ad135b5dSChristopher Siden 
45863d7072f8Seschrock 	case ZPOOL_STATUS_FAULTED_DEV_R:
45873d7072f8Seschrock 		(void) printf(gettext("status: One or more devices are "
45883d7072f8Seschrock 		    "faulted in response to persistent errors.\n\tSufficient "
45893d7072f8Seschrock 		    "replicas exist for the pool to continue functioning "
45903d7072f8Seschrock 		    "in a\n\tdegraded state.\n"));
45913d7072f8Seschrock 		(void) printf(gettext("action: Replace the faulted device, "
45923d7072f8Seschrock 		    "or use 'zpool clear' to mark the device\n\trepaired.\n"));
45933d7072f8Seschrock 		break;
45943d7072f8Seschrock 
45953d7072f8Seschrock 	case ZPOOL_STATUS_FAULTED_DEV_NR:
45963d7072f8Seschrock 		(void) printf(gettext("status: One or more devices are "
45973d7072f8Seschrock 		    "faulted in response to persistent errors.  There are "
45983d7072f8Seschrock 		    "insufficient replicas for the pool to\n\tcontinue "
45993d7072f8Seschrock 		    "functioning.\n"));
46003d7072f8Seschrock 		(void) printf(gettext("action: Destroy and re-create the pool "
46013d7072f8Seschrock 		    "from a backup source.  Manually marking the device\n"
46023d7072f8Seschrock 		    "\trepaired using 'zpool clear' may allow some data "
46033d7072f8Seschrock 		    "to be recovered.\n"));
46043d7072f8Seschrock 		break;
46053d7072f8Seschrock 
460632b87932Sek 	case ZPOOL_STATUS_IO_FAILURE_WAIT:
460732b87932Sek 	case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
460832b87932Sek 		(void) printf(gettext("status: One or more devices are "
46098a79c1b5Sek 		    "faulted in response to IO failures.\n"));
461032b87932Sek 		(void) printf(gettext("action: Make sure the affected devices "
461132b87932Sek 		    "are connected, then run 'zpool clear'.\n"));
461232b87932Sek 		break;
461332b87932Sek 
4614b87f3af3Sperrin 	case ZPOOL_STATUS_BAD_LOG:
4615b87f3af3Sperrin 		(void) printf(gettext("status: An intent log record "
4616b87f3af3Sperrin 		    "could not be read.\n"
4617b87f3af3Sperrin 		    "\tWaiting for adminstrator intervention to fix the "
4618b87f3af3Sperrin 		    "faulted pool.\n"));
4619b87f3af3Sperrin 		(void) printf(gettext("action: Either restore the affected "
4620b87f3af3Sperrin 		    "device(s) and run 'zpool online',\n"
4621b87f3af3Sperrin 		    "\tor ignore the intent log records by running "
4622b87f3af3Sperrin 		    "'zpool clear'.\n"));
4623b87f3af3Sperrin 		break;
4624b87f3af3Sperrin 
4625fa9e4066Sahrens 	default:
4626fa9e4066Sahrens 		/*
4627fa9e4066Sahrens 		 * The remaining errors can't actually be generated, yet.
4628fa9e4066Sahrens 		 */
4629fa9e4066Sahrens 		assert(reason == ZPOOL_STATUS_OK);
4630fa9e4066Sahrens 	}
4631fa9e4066Sahrens 
4632fa9e4066Sahrens 	if (msgid != NULL)
4633654b400cSJoshua M. Clulow 		(void) printf(gettext("   see: http://illumos.org/msg/%s\n"),
4634fa9e4066Sahrens 		    msgid);
4635fa9e4066Sahrens 
4636fa9e4066Sahrens 	if (config != NULL) {
4637fa9e4066Sahrens 		int namewidth;
4638ea8dc4b6Seschrock 		uint64_t nerr;
4639fa94a07fSbrendan 		nvlist_t **spares, **l2cache;
4640fa94a07fSbrendan 		uint_t nspares, nl2cache;
46413f9d6ad7SLin Ling 		pool_scan_stat_t *ps = NULL;
46425cabbc6bSPrashanth Sreenivasa 		pool_removal_stat_t *prs = NULL;
4643fa9e4066Sahrens 
46443f9d6ad7SLin Ling 		(void) nvlist_lookup_uint64_array(nvroot,
46453f9d6ad7SLin Ling 		    ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
46463f9d6ad7SLin Ling 		print_scan_status(ps);
4647fa9e4066Sahrens 
46485cabbc6bSPrashanth Sreenivasa 		(void) nvlist_lookup_uint64_array(nvroot,
46495cabbc6bSPrashanth Sreenivasa 		    ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c);
46505cabbc6bSPrashanth Sreenivasa 		print_removal_status(zhp, prs);
46515cabbc6bSPrashanth Sreenivasa 
4652c67d9675Seschrock 		namewidth = max_width(zhp, nvroot, 0, 0);
4653fa9e4066Sahrens 		if (namewidth < 10)
4654fa9e4066Sahrens 			namewidth = 10;
4655fa9e4066Sahrens 
4656fa9e4066Sahrens 		(void) printf(gettext("config:\n\n"));
4657fa9e4066Sahrens 		(void) printf(gettext("\t%-*s  %-8s %5s %5s %5s\n"), namewidth,
4658fa9e4066Sahrens 		    "NAME", "STATE", "READ", "WRITE", "CKSUM");
4659c67d9675Seschrock 		print_status_config(zhp, zpool_get_name(zhp), nvroot,
4660aa8cf21aSNeil Perrin 		    namewidth, 0, B_FALSE);
466199653d4eSeschrock 
46624dea40f0SNeil Perrin 		if (num_logs(nvroot) > 0)
4663e6ca193dSGeorge Wilson 			print_logs(zhp, nvroot, namewidth, B_TRUE);
4664fa94a07fSbrendan 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4665fa94a07fSbrendan 		    &l2cache, &nl2cache) == 0)
4666fa94a07fSbrendan 			print_l2cache(zhp, l2cache, nl2cache, namewidth);
4667fa94a07fSbrendan 
466899653d4eSeschrock 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
466999653d4eSeschrock 		    &spares, &nspares) == 0)
467099653d4eSeschrock 			print_spares(zhp, spares, nspares, namewidth);
4671ea8dc4b6Seschrock 
4672ea8dc4b6Seschrock 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4673ea8dc4b6Seschrock 		    &nerr) == 0) {
467455434c77Sek 			nvlist_t *nverrlist = NULL;
467555434c77Sek 
4676ea8dc4b6Seschrock 			/*
4677ea8dc4b6Seschrock 			 * If the approximate error count is small, get a
4678ea8dc4b6Seschrock 			 * precise count by fetching the entire log and
4679ea8dc4b6Seschrock 			 * uniquifying the results.
4680ea8dc4b6Seschrock 			 */
468175519f38Sek 			if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
468255434c77Sek 			    zpool_get_errlog(zhp, &nverrlist) == 0) {
468355434c77Sek 				nvpair_t *elem;
468455434c77Sek 
468555434c77Sek 				elem = NULL;
468655434c77Sek 				nerr = 0;
468755434c77Sek 				while ((elem = nvlist_next_nvpair(nverrlist,
468855434c77Sek 				    elem)) != NULL) {
468955434c77Sek 					nerr++;
469055434c77Sek 				}
469155434c77Sek 			}
469255434c77Sek 			nvlist_free(nverrlist);
4693ea8dc4b6Seschrock 
4694ea8dc4b6Seschrock 			(void) printf("\n");
469599653d4eSeschrock 
4696ea8dc4b6Seschrock 			if (nerr == 0)
4697ea8dc4b6Seschrock 				(void) printf(gettext("errors: No known data "
4698ea8dc4b6Seschrock 				    "errors\n"));
4699ea8dc4b6Seschrock 			else if (!cbp->cb_verbose)
4700e9dbad6fSeschrock 				(void) printf(gettext("errors: %llu data "
47015ad82045Snd 				    "errors, use '-v' for a list\n"),
47025ad82045Snd 				    (u_longlong_t)nerr);
4703ea8dc4b6Seschrock 			else
4704ea8dc4b6Seschrock 				print_error_log(zhp);
4705ea8dc4b6Seschrock 		}
47069eb19f4dSGeorge Wilson 
47079eb19f4dSGeorge Wilson 		if (cbp->cb_dedup_stats)
47089eb19f4dSGeorge Wilson 			print_dedup_stats(config);
4709fa9e4066Sahrens 	} else {
4710fa9e4066Sahrens 		(void) printf(gettext("config: The configuration cannot be "
4711fa9e4066Sahrens 		    "determined.\n"));
4712fa9e4066Sahrens 	}
4713fa9e4066Sahrens 
4714fa9e4066Sahrens 	return (0);
4715fa9e4066Sahrens }
4716fa9e4066Sahrens 
4717fa9e4066Sahrens /*
47183f9d6ad7SLin Ling  * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4719fa9e4066Sahrens  *
4720fa9e4066Sahrens  *	-v	Display complete error logs
4721fa9e4066Sahrens  *	-x	Display only pools with potential problems
47229eb19f4dSGeorge Wilson  *	-D	Display dedup status (undocumented)
47233f9d6ad7SLin Ling  *	-T	Display a timestamp in date(1) or Unix format
4724fa9e4066Sahrens  *
4725fa9e4066Sahrens  * Describes the health status of all pools or some subset.
4726fa9e4066Sahrens  */
4727fa9e4066Sahrens int
4728fa9e4066Sahrens zpool_do_status(int argc, char **argv)
4729fa9e4066Sahrens {
4730fa9e4066Sahrens 	int c;
4731fa9e4066Sahrens 	int ret;
47323f9d6ad7SLin Ling 	unsigned long interval = 0, count = 0;
4733fa9e4066Sahrens 	status_cbdata_t cb = { 0 };
4734fa9e4066Sahrens 
4735fa9e4066Sahrens 	/* check options */
47363f9d6ad7SLin Ling 	while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4737fa9e4066Sahrens 		switch (c) {
4738fa9e4066Sahrens 		case 'v':
473999653d4eSeschrock 			cb.cb_verbose = B_TRUE;
4740fa9e4066Sahrens 			break;
4741fa9e4066Sahrens 		case 'x':
474299653d4eSeschrock 			cb.cb_explain = B_TRUE;
4743fa9e4066Sahrens 			break;
47449eb19f4dSGeorge Wilson 		case 'D':
47459eb19f4dSGeorge Wilson 			cb.cb_dedup_stats = B_TRUE;
47469eb19f4dSGeorge Wilson 			break;
47473f9d6ad7SLin Ling 		case 'T':
47483f9d6ad7SLin Ling 			get_timestamp_arg(*optarg);
47493f9d6ad7SLin Ling 			break;
4750fa9e4066Sahrens 		case '?':
4751fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4752fa9e4066Sahrens 			    optopt);
475399653d4eSeschrock 			usage(B_FALSE);
4754fa9e4066Sahrens 		}
4755fa9e4066Sahrens 	}
4756fa9e4066Sahrens 
4757fa9e4066Sahrens 	argc -= optind;
4758fa9e4066Sahrens 	argv += optind;
4759fa9e4066Sahrens 
47603f9d6ad7SLin Ling 	get_interval_count(&argc, argv, &interval, &count);
4761fa9e4066Sahrens 
4762e9dbad6fSeschrock 	if (argc == 0)
4763e9dbad6fSeschrock 		cb.cb_allpools = B_TRUE;
4764e9dbad6fSeschrock 
47653f9d6ad7SLin Ling 	cb.cb_first = B_TRUE;
4766fa9e4066Sahrens 
47673f9d6ad7SLin Ling 	for (;;) {
47683f9d6ad7SLin Ling 		if (timestamp_fmt != NODATE)
47693f9d6ad7SLin Ling 			print_timestamp(timestamp_fmt);
4770fa9e4066Sahrens 
47713f9d6ad7SLin Ling 		ret = for_each_pool(argc, argv, B_TRUE, NULL,
47723f9d6ad7SLin Ling 		    status_callback, &cb);
47733f9d6ad7SLin Ling 
47743f9d6ad7SLin Ling 		if (argc == 0 && cb.cb_count == 0)
47753f9d6ad7SLin Ling 			(void) printf(gettext("no pools available\n"));
47763f9d6ad7SLin Ling 		else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
47773f9d6ad7SLin Ling 			(void) printf(gettext("all pools are healthy\n"));
47783f9d6ad7SLin Ling 
47793f9d6ad7SLin Ling 		if (ret != 0)
47803f9d6ad7SLin Ling 			return (ret);
47813f9d6ad7SLin Ling 
47823f9d6ad7SLin Ling 		if (interval == 0)
47833f9d6ad7SLin Ling 			break;
47843f9d6ad7SLin Ling 
47853f9d6ad7SLin Ling 		if (count != 0 && --count == 0)
47863f9d6ad7SLin Ling 			break;
47873f9d6ad7SLin Ling 
47883f9d6ad7SLin Ling 		(void) sleep(interval);
47893f9d6ad7SLin Ling 	}
47903f9d6ad7SLin Ling 
47913f9d6ad7SLin Ling 	return (0);
4792fa9e4066Sahrens }
4793fa9e4066Sahrens 
4794eaca9bbdSeschrock typedef struct upgrade_cbdata {
4795eaca9bbdSeschrock 	int	cb_first;
479606eeb2adSek 	int	cb_argc;
4797990b4856Slling 	uint64_t cb_version;
479806eeb2adSek 	char	**cb_argv;
4799eaca9bbdSeschrock } upgrade_cbdata_t;
4800eaca9bbdSeschrock 
480157221772SChristopher Siden static int
480257221772SChristopher Siden upgrade_version(zpool_handle_t *zhp, uint64_t version)
480357221772SChristopher Siden {
480457221772SChristopher Siden 	int ret;
480557221772SChristopher Siden 	nvlist_t *config;
480657221772SChristopher Siden 	uint64_t oldversion;
480757221772SChristopher Siden 
480857221772SChristopher Siden 	config = zpool_get_config(zhp, NULL);
480957221772SChristopher Siden 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
481057221772SChristopher Siden 	    &oldversion) == 0);
481157221772SChristopher Siden 
481257221772SChristopher Siden 	assert(SPA_VERSION_IS_SUPPORTED(oldversion));
481357221772SChristopher Siden 	assert(oldversion < version);
481457221772SChristopher Siden 
481557221772SChristopher Siden 	ret = zpool_upgrade(zhp, version);
481657221772SChristopher Siden 	if (ret != 0)
481757221772SChristopher Siden 		return (ret);
481857221772SChristopher Siden 
481957221772SChristopher Siden 	if (version >= SPA_VERSION_FEATURES) {
482057221772SChristopher Siden 		(void) printf(gettext("Successfully upgraded "
482157221772SChristopher Siden 		    "'%s' from version %llu to feature flags.\n"),
482257221772SChristopher Siden 		    zpool_get_name(zhp), oldversion);
482357221772SChristopher Siden 	} else {
482457221772SChristopher Siden 		(void) printf(gettext("Successfully upgraded "
482557221772SChristopher Siden 		    "'%s' from version %llu to version %llu.\n"),
482657221772SChristopher Siden 		    zpool_get_name(zhp), oldversion, version);
482757221772SChristopher Siden 	}
482857221772SChristopher Siden 
482957221772SChristopher Siden 	return (0);
483057221772SChristopher Siden }
483157221772SChristopher Siden 
483257221772SChristopher Siden static int
483357221772SChristopher Siden upgrade_enable_all(zpool_handle_t *zhp, int *countp)
483457221772SChristopher Siden {
483557221772SChristopher Siden 	int i, ret, count;
483657221772SChristopher Siden 	boolean_t firstff = B_TRUE;
483757221772SChristopher Siden 	nvlist_t *enabled = zpool_get_features(zhp);
483857221772SChristopher Siden 
483957221772SChristopher Siden 	count = 0;
484057221772SChristopher Siden 	for (i = 0; i < SPA_FEATURES; i++) {
484157221772SChristopher Siden 		const char *fname = spa_feature_table[i].fi_uname;
484257221772SChristopher Siden 		const char *fguid = spa_feature_table[i].fi_guid;
484357221772SChristopher Siden 		if (!nvlist_exists(enabled, fguid)) {
484457221772SChristopher Siden 			char *propname;
484557221772SChristopher Siden 			verify(-1 != asprintf(&propname, "feature@%s", fname));
484657221772SChristopher Siden 			ret = zpool_set_prop(zhp, propname,
484757221772SChristopher Siden 			    ZFS_FEATURE_ENABLED);
484857221772SChristopher Siden 			if (ret != 0) {
484957221772SChristopher Siden 				free(propname);
485057221772SChristopher Siden 				return (ret);
485157221772SChristopher Siden 			}
485257221772SChristopher Siden 			count++;
485357221772SChristopher Siden 
485457221772SChristopher Siden 			if (firstff) {
485557221772SChristopher Siden 				(void) printf(gettext("Enabled the "
485657221772SChristopher Siden 				    "following features on '%s':\n"),
485757221772SChristopher Siden 				    zpool_get_name(zhp));
485857221772SChristopher Siden 				firstff = B_FALSE;
485957221772SChristopher Siden 			}
486057221772SChristopher Siden 			(void) printf(gettext("  %s\n"), fname);
486157221772SChristopher Siden 			free(propname);
486257221772SChristopher Siden 		}
486357221772SChristopher Siden 	}
486457221772SChristopher Siden 
486557221772SChristopher Siden 	if (countp != NULL)
486657221772SChristopher Siden 		*countp = count;
486757221772SChristopher Siden 	return (0);
486857221772SChristopher Siden }
486957221772SChristopher Siden 
4870eaca9bbdSeschrock static int
4871eaca9bbdSeschrock upgrade_cb(zpool_handle_t *zhp, void *arg)
4872eaca9bbdSeschrock {
4873eaca9bbdSeschrock 	upgrade_cbdata_t *cbp = arg;
4874eaca9bbdSeschrock 	nvlist_t *config;
4875eaca9bbdSeschrock 	uint64_t version;
487657221772SChristopher Siden 	boolean_t printnl = B_FALSE;
487757221772SChristopher Siden 	int ret;
4878eaca9bbdSeschrock 
4879eaca9bbdSeschrock 	config = zpool_get_config(zhp, NULL);
4880eaca9bbdSeschrock 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4881eaca9bbdSeschrock 	    &version) == 0);
4882eaca9bbdSeschrock 
488357221772SChristopher Siden 	assert(SPA_VERSION_IS_SUPPORTED(version));
4884eaca9bbdSeschrock 
488557221772SChristopher Siden 	if (version < cbp->cb_version) {
488657221772SChristopher Siden 		cbp->cb_first = B_FALSE;
488757221772SChristopher Siden 		ret = upgrade_version(zhp, cbp->cb_version);
488857221772SChristopher Siden 		if (ret != 0)
488957221772SChristopher Siden 			return (ret);
489057221772SChristopher Siden 		printnl = B_TRUE;
489157221772SChristopher Siden 
489257221772SChristopher Siden 		/*
489357221772SChristopher Siden 		 * If they did "zpool upgrade -a", then we could
489457221772SChristopher Siden 		 * be doing ioctls to different pools.  We need
489557221772SChristopher Siden 		 * to log this history once to each pool, and bypass
489657221772SChristopher Siden 		 * the normal history logging that happens in main().
489757221772SChristopher Siden 		 */
489857221772SChristopher Siden 		(void) zpool_log_history(g_zfs, history_str);
489957221772SChristopher Siden 		log_history = B_FALSE;
490057221772SChristopher Siden 	}
490157221772SChristopher Siden 
490257221772SChristopher Siden 	if (cbp->cb_version >= SPA_VERSION_FEATURES) {
490357221772SChristopher Siden 		int count;
490457221772SChristopher Siden 		ret = upgrade_enable_all(zhp, &count);
490557221772SChristopher Siden 		if (ret != 0)
490657221772SChristopher Siden 			return (ret);
490757221772SChristopher Siden 
490857221772SChristopher Siden 		if (count > 0) {
490999653d4eSeschrock 			cbp->cb_first = B_FALSE;
491057221772SChristopher Siden 			printnl = B_TRUE;
4911eaca9bbdSeschrock 		}
491257221772SChristopher Siden 	}
4913eaca9bbdSeschrock 
491457221772SChristopher Siden 	if (printnl) {
491557221772SChristopher Siden 		(void) printf(gettext("\n"));
491657221772SChristopher Siden 	}
491757221772SChristopher Siden 
491857221772SChristopher Siden 	return (0);
491957221772SChristopher Siden }
492057221772SChristopher Siden 
492157221772SChristopher Siden static int
492257221772SChristopher Siden upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
492357221772SChristopher Siden {
492457221772SChristopher Siden 	upgrade_cbdata_t *cbp = arg;
492557221772SChristopher Siden 	nvlist_t *config;
492657221772SChristopher Siden 	uint64_t version;
492757221772SChristopher Siden 
492857221772SChristopher Siden 	config = zpool_get_config(zhp, NULL);
492957221772SChristopher Siden 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
493057221772SChristopher Siden 	    &version) == 0);
493157221772SChristopher Siden 
493257221772SChristopher Siden 	assert(SPA_VERSION_IS_SUPPORTED(version));
493357221772SChristopher Siden 
493457221772SChristopher Siden 	if (version < SPA_VERSION_FEATURES) {
4935eaca9bbdSeschrock 		if (cbp->cb_first) {
4936eaca9bbdSeschrock 			(void) printf(gettext("The following pools are "
493757221772SChristopher Siden 			    "formatted with legacy version numbers and can\n"
493857221772SChristopher Siden 			    "be upgraded to use feature flags.  After "
493957221772SChristopher Siden 			    "being upgraded, these pools\nwill no "
494057221772SChristopher Siden 			    "longer be accessible by software that does not "
494157221772SChristopher Siden 			    "support feature\nflags.\n\n"));
4942eaca9bbdSeschrock 			(void) printf(gettext("VER  POOL\n"));
4943eaca9bbdSeschrock 			(void) printf(gettext("---  ------------\n"));
494499653d4eSeschrock 			cbp->cb_first = B_FALSE;
4945eaca9bbdSeschrock 		}
4946eaca9bbdSeschrock 
49475ad82045Snd 		(void) printf("%2llu   %s\n", (u_longlong_t)version,
4948eaca9bbdSeschrock 		    zpool_get_name(zhp));
4949eaca9bbdSeschrock 	}
4950eaca9bbdSeschrock 
495157221772SChristopher Siden 	return (0);
495257221772SChristopher Siden }
495357221772SChristopher Siden 
495457221772SChristopher Siden static int
495557221772SChristopher Siden upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
495657221772SChristopher Siden {
495757221772SChristopher Siden 	upgrade_cbdata_t *cbp = arg;
495857221772SChristopher Siden 	nvlist_t *config;
495957221772SChristopher Siden 	uint64_t version;
496057221772SChristopher Siden 
496157221772SChristopher Siden 	config = zpool_get_config(zhp, NULL);
496257221772SChristopher Siden 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
496357221772SChristopher Siden 	    &version) == 0);
496457221772SChristopher Siden 
496557221772SChristopher Siden 	if (version >= SPA_VERSION_FEATURES) {
496657221772SChristopher Siden 		int i;
496757221772SChristopher Siden 		boolean_t poolfirst = B_TRUE;
496857221772SChristopher Siden 		nvlist_t *enabled = zpool_get_features(zhp);
496957221772SChristopher Siden 
497057221772SChristopher Siden 		for (i = 0; i < SPA_FEATURES; i++) {
497157221772SChristopher Siden 			const char *fguid = spa_feature_table[i].fi_guid;
497257221772SChristopher Siden 			const char *fname = spa_feature_table[i].fi_uname;
497357221772SChristopher Siden 			if (!nvlist_exists(enabled, fguid)) {
497457221772SChristopher Siden 				if (cbp->cb_first) {
497557221772SChristopher Siden 					(void) printf(gettext("\nSome "
497657221772SChristopher Siden 					    "supported features are not "
497757221772SChristopher Siden 					    "enabled on the following pools. "
497857221772SChristopher Siden 					    "Once a\nfeature is enabled the "
497957221772SChristopher Siden 					    "pool may become incompatible with "
498057221772SChristopher Siden 					    "software\nthat does not support "
498157221772SChristopher Siden 					    "the feature. See "
498257221772SChristopher Siden 					    "zpool-features(5) for "
498357221772SChristopher Siden 					    "details.\n\n"));
498457221772SChristopher Siden 					(void) printf(gettext("POOL  "
498557221772SChristopher Siden 					    "FEATURE\n"));
498657221772SChristopher Siden 					(void) printf(gettext("------"
498757221772SChristopher Siden 					    "---------\n"));
498857221772SChristopher Siden 					cbp->cb_first = B_FALSE;
498957221772SChristopher Siden 				}
499057221772SChristopher Siden 
499157221772SChristopher Siden 				if (poolfirst) {
499257221772SChristopher Siden 					(void) printf(gettext("%s\n"),
499357221772SChristopher Siden 					    zpool_get_name(zhp));
499457221772SChristopher Siden 					poolfirst = B_FALSE;
499557221772SChristopher Siden 				}
499657221772SChristopher Siden 
499757221772SChristopher Siden 				(void) printf(gettext("      %s\n"), fname);
499857221772SChristopher Siden 			}
499957221772SChristopher Siden 		}
500057221772SChristopher Siden 	}
500157221772SChristopher Siden 
500257221772SChristopher Siden 	return (0);
5003eaca9bbdSeschrock }
5004eaca9bbdSeschrock 
5005eaca9bbdSeschrock /* ARGSUSED */
5006eaca9bbdSeschrock static int
500706eeb2adSek upgrade_one(zpool_handle_t *zhp, void *data)
5008eaca9bbdSeschrock {
500957221772SChristopher Siden 	boolean_t printnl = B_FALSE;
5010990b4856Slling 	upgrade_cbdata_t *cbp = data;
5011990b4856Slling 	uint64_t cur_version;
5012eaca9bbdSeschrock 	int ret;
5013eaca9bbdSeschrock 
50148654d025Sperrin 	if (strcmp("log", zpool_get_name(zhp)) == 0) {
50158654d025Sperrin 		(void) printf(gettext("'log' is now a reserved word\n"
50168654d025Sperrin 		    "Pool 'log' must be renamed using export and import"
50178654d025Sperrin 		    " to upgrade.\n"));
50188654d025Sperrin 		return (1);
50198654d025Sperrin 	}
5020990b4856Slling 
5021990b4856Slling 	cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
5022e6c728e1Sbrendan 	if (cur_version > cbp->cb_version) {
5023eaca9bbdSeschrock 		(void) printf(gettext("Pool '%s' is already formatted "
502457221772SChristopher Siden 		    "using more current version '%llu'.\n\n"),
5025e6c728e1Sbrendan 		    zpool_get_name(zhp), cur_version);
5026e6c728e1Sbrendan 		return (0);
5027e6c728e1Sbrendan 	}
502857221772SChristopher Siden 
502957221772SChristopher Siden 	if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
5030e6c728e1Sbrendan 		(void) printf(gettext("Pool '%s' is already formatted "
503157221772SChristopher Siden 		    "using version %llu.\n\n"), zpool_get_name(zhp),
503257221772SChristopher Siden 		    cbp->cb_version);
5033eaca9bbdSeschrock 		return (0);
5034eaca9bbdSeschrock 	}
5035eaca9bbdSeschrock 
503657221772SChristopher Siden 	if (cur_version != cbp->cb_version) {
503757221772SChristopher Siden 		printnl = B_TRUE;
503857221772SChristopher Siden 		ret = upgrade_version(zhp, cbp->cb_version);
503957221772SChristopher Siden 		if (ret != 0)
504057221772SChristopher Siden 			return (ret);
504157221772SChristopher Siden 	}
504206eeb2adSek 
504357221772SChristopher Siden 	if (cbp->cb_version >= SPA_VERSION_FEATURES) {
504457221772SChristopher Siden 		int count = 0;
504557221772SChristopher Siden 		ret = upgrade_enable_all(zhp, &count);
504657221772SChristopher Siden 		if (ret != 0)
504757221772SChristopher Siden 			return (ret);
504857221772SChristopher Siden 
504957221772SChristopher Siden 		if (count != 0) {
505057221772SChristopher Siden 			printnl = B_TRUE;
505157221772SChristopher Siden 		} else if (cur_version == SPA_VERSION) {
505257221772SChristopher Siden 			(void) printf(gettext("Pool '%s' already has all "
505357221772SChristopher Siden 			    "supported features enabled.\n"),
505457221772SChristopher Siden 			    zpool_get_name(zhp));
505557221772SChristopher Siden 		}
505657221772SChristopher Siden 	}
505757221772SChristopher Siden 
505857221772SChristopher Siden 	if (printnl) {
505957221772SChristopher Siden 		(void) printf(gettext("\n"));
506006eeb2adSek 	}
5061eaca9bbdSeschrock 
506257221772SChristopher Siden 	return (0);
5063eaca9bbdSeschrock }
5064eaca9bbdSeschrock 
5065eaca9bbdSeschrock /*
5066eaca9bbdSeschrock  * zpool upgrade
5067eaca9bbdSeschrock  * zpool upgrade -v
5068990b4856Slling  * zpool upgrade [-V version] <-a | pool ...>
5069eaca9bbdSeschrock  *
5070eaca9bbdSeschrock  * With no arguments, display downrev'd ZFS pool available for upgrade.
5071eaca9bbdSeschrock  * Individual pools can be upgraded by specifying the pool, and '-a' will
5072eaca9bbdSeschrock  * upgrade all pools.
5073eaca9bbdSeschrock  */
5074eaca9bbdSeschrock int
5075eaca9bbdSeschrock zpool_do_upgrade(int argc, char **argv)
5076eaca9bbdSeschrock {
5077eaca9bbdSeschrock 	int c;
5078eaca9bbdSeschrock 	upgrade_cbdata_t cb = { 0 };
5079eaca9bbdSeschrock 	int ret = 0;
5080eaca9bbdSeschrock 	boolean_t showversions = B_FALSE;
508157221772SChristopher Siden 	boolean_t upgradeall = B_FALSE;
5082990b4856Slling 	char *end;
5083990b4856Slling 
5084eaca9bbdSeschrock 
5085eaca9bbdSeschrock 	/* check options */
5086478ed9adSEric Taylor 	while ((c = getopt(argc, argv, ":avV:")) != -1) {
5087eaca9bbdSeschrock 		switch (c) {
5088eaca9bbdSeschrock 		case 'a':
508957221772SChristopher Siden 			upgradeall = B_TRUE;
5090eaca9bbdSeschrock 			break;
5091eaca9bbdSeschrock 		case 'v':
5092eaca9bbdSeschrock 			showversions = B_TRUE;
5093eaca9bbdSeschrock 			break;
5094990b4856Slling 		case 'V':
5095990b4856Slling 			cb.cb_version = strtoll(optarg, &end, 10);
5096ad135b5dSChristopher Siden 			if (*end != '\0' ||
5097ad135b5dSChristopher Siden 			    !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
5098990b4856Slling 				(void) fprintf(stderr,
5099990b4856Slling 				    gettext("invalid version '%s'\n"), optarg);
5100990b4856Slling 				usage(B_FALSE);
5101990b4856Slling 			}
5102990b4856Slling 			break;
5103478ed9adSEric Taylor 		case ':':
5104478ed9adSEric Taylor 			(void) fprintf(stderr, gettext("missing argument for "
5105478ed9adSEric Taylor 			    "'%c' option\n"), optopt);
5106478ed9adSEric Taylor 			usage(B_FALSE);
5107478ed9adSEric Taylor 			break;
5108eaca9bbdSeschrock 		case '?':
5109eaca9bbdSeschrock 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
5110eaca9bbdSeschrock 			    optopt);
511199653d4eSeschrock 			usage(B_FALSE);
5112eaca9bbdSeschrock 		}
5113eaca9bbdSeschrock 	}
5114eaca9bbdSeschrock 
511506eeb2adSek 	cb.cb_argc = argc;
511606eeb2adSek 	cb.cb_argv = argv;
5117eaca9bbdSeschrock 	argc -= optind;
5118eaca9bbdSeschrock 	argv += optind;
5119eaca9bbdSeschrock 
5120351420b3Slling 	if (cb.cb_version == 0) {
5121351420b3Slling 		cb.cb_version = SPA_VERSION;
512257221772SChristopher Siden 	} else if (!upgradeall && argc == 0) {
5123351420b3Slling 		(void) fprintf(stderr, gettext("-V option is "
5124351420b3Slling 		    "incompatible with other arguments\n"));
5125351420b3Slling 		usage(B_FALSE);
5126351420b3Slling 	}
5127351420b3Slling 
5128eaca9bbdSeschrock 	if (showversions) {
512957221772SChristopher Siden 		if (upgradeall || argc != 0) {
5130eaca9bbdSeschrock 			(void) fprintf(stderr, gettext("-v option is "
5131eaca9bbdSeschrock 			    "incompatible with other arguments\n"));
513299653d4eSeschrock 			usage(B_FALSE);
5133eaca9bbdSeschrock 		}
513457221772SChristopher Siden 	} else if (upgradeall) {
5135eaca9bbdSeschrock 		if (argc != 0) {
5136351420b3Slling 			(void) fprintf(stderr, gettext("-a option should not "
5137351420b3Slling 			    "be used along with a pool name\n"));
513899653d4eSeschrock 			usage(B_FALSE);
5139eaca9bbdSeschrock 		}
5140eaca9bbdSeschrock 	}
5141eaca9bbdSeschrock 
5142ad135b5dSChristopher Siden 	(void) printf(gettext("This system supports ZFS pool feature "
5143ad135b5dSChristopher Siden 	    "flags.\n\n"));
5144eaca9bbdSeschrock 	if (showversions) {
514557221772SChristopher Siden 		int i;
514657221772SChristopher Siden 
514757221772SChristopher Siden 		(void) printf(gettext("The following features are "
514857221772SChristopher Siden 		    "supported:\n\n"));
514957221772SChristopher Siden 		(void) printf(gettext("FEAT DESCRIPTION\n"));
515057221772SChristopher Siden 		(void) printf("----------------------------------------------"
515157221772SChristopher Siden 		    "---------------\n");
515257221772SChristopher Siden 		for (i = 0; i < SPA_FEATURES; i++) {
515357221772SChristopher Siden 			zfeature_info_t *fi = &spa_feature_table[i];
5154ca0cc391SMatthew Ahrens 			const char *ro =
5155ca0cc391SMatthew Ahrens 			    (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
515657221772SChristopher Siden 			    " (read-only compatible)" : "";
515757221772SChristopher Siden 
515857221772SChristopher Siden 			(void) printf("%-37s%s\n", fi->fi_uname, ro);
515957221772SChristopher Siden 			(void) printf("     %s\n", fi->fi_desc);
516057221772SChristopher Siden 		}
516157221772SChristopher Siden 		(void) printf("\n");
516257221772SChristopher Siden 
516357221772SChristopher Siden 		(void) printf(gettext("The following legacy versions are also "
5164d7d4af51Smmusante 		    "supported:\n\n"));
5165eaca9bbdSeschrock 		(void) printf(gettext("VER  DESCRIPTION\n"));
5166eaca9bbdSeschrock 		(void) printf("---  -----------------------------------------"
5167eaca9bbdSeschrock 		    "---------------\n");
516899653d4eSeschrock 		(void) printf(gettext(" 1   Initial ZFS version\n"));
516944cd46caSbillm 		(void) printf(gettext(" 2   Ditto blocks "
517044cd46caSbillm 		    "(replicated metadata)\n"));
517199653d4eSeschrock 		(void) printf(gettext(" 3   Hot spares and double parity "
517299653d4eSeschrock 		    "RAID-Z\n"));
5173d7306b64Sek 		(void) printf(gettext(" 4   zpool history\n"));
5174c9431fa1Sahl 		(void) printf(gettext(" 5   Compression using the gzip "
5175c9431fa1Sahl 		    "algorithm\n"));
5176990b4856Slling 		(void) printf(gettext(" 6   bootfs pool property\n"));
51778654d025Sperrin 		(void) printf(gettext(" 7   Separate intent log devices\n"));
5178ecd6cf80Smarks 		(void) printf(gettext(" 8   Delegated administration\n"));
51798eed72d4Sck 		(void) printf(gettext(" 9   refquota and refreservation "
5180a9799022Sck 		    "properties\n"));
5181fa94a07fSbrendan 		(void) printf(gettext(" 10  Cache devices\n"));
5182088f3894Sahrens 		(void) printf(gettext(" 11  Improved scrub performance\n"));
5183bb0ade09Sahrens 		(void) printf(gettext(" 12  Snapshot properties\n"));
518474e7dc98SMatthew Ahrens 		(void) printf(gettext(" 13  snapused property\n"));
518514843421SMatthew Ahrens 		(void) printf(gettext(" 14  passthrough-x aclinherit\n"));
518614843421SMatthew Ahrens 		(void) printf(gettext(" 15  user/group space accounting\n"));
5187478ed9adSEric Taylor 		(void) printf(gettext(" 16  stmf property support\n"));
51887aeab329SAdam Leventhal 		(void) printf(gettext(" 17  Triple-parity RAID-Z\n"));
5189b24ab676SJeff Bonwick 		(void) printf(gettext(" 18  Snapshot user holds\n"));
519088ecc943SGeorge Wilson 		(void) printf(gettext(" 19  Log device removal\n"));
5191b24ab676SJeff Bonwick 		(void) printf(gettext(" 20  Compression using zle "
5192b24ab676SJeff Bonwick 		    "(zero-length encoding)\n"));
5193b24ab676SJeff Bonwick 		(void) printf(gettext(" 21  Deduplication\n"));
519492241e0bSTom Erickson 		(void) printf(gettext(" 22  Received properties\n"));
51956e1f5caaSNeil Perrin 		(void) printf(gettext(" 23  Slim ZIL\n"));
51960a586ceaSMark Shellenbaum 		(void) printf(gettext(" 24  System attributes\n"));
51973f9d6ad7SLin Ling 		(void) printf(gettext(" 25  Improved scrub stats\n"));
5198cde58dbcSMatthew Ahrens 		(void) printf(gettext(" 26  Improved snapshot deletion "
5199cde58dbcSMatthew Ahrens 		    "performance\n"));
52006e0cbcaaSMatthew Ahrens 		(void) printf(gettext(" 27  Improved snapshot creation "
52016e0cbcaaSMatthew Ahrens 		    "performance\n"));
5202cb04b873SMark J Musante 		(void) printf(gettext(" 28  Multiple vdev replacements\n"));
5203b24ab676SJeff Bonwick 		(void) printf(gettext("\nFor more information on a particular "
52049a8685acSstephanie scheffler 		    "version, including supported releases,\n"));
52059a8685acSstephanie scheffler 		(void) printf(gettext("see the ZFS Administration Guide.\n\n"));
520657221772SChristopher Siden 	} else if (argc == 0 && upgradeall) {
520757221772SChristopher Siden 		cb.cb_first = B_TRUE;
520899653d4eSeschrock 		ret = zpool_iter(g_zfs, upgrade_cb, &cb);
520957221772SChristopher Siden 		if (ret == 0 && cb.cb_first) {
521057221772SChristopher Siden 			if (cb.cb_version == SPA_VERSION) {
521157221772SChristopher Siden 				(void) printf(gettext("All pools are already "
521257221772SChristopher Siden 				    "formatted using feature flags.\n\n"));
521357221772SChristopher Siden 				(void) printf(gettext("Every feature flags "
521457221772SChristopher Siden 				    "pool already has all supported features "
521557221772SChristopher Siden 				    "enabled.\n"));
521657221772SChristopher Siden 			} else {
521757221772SChristopher Siden 				(void) printf(gettext("All pools are already "
521857221772SChristopher Siden 				    "formatted with version %llu or higher.\n"),
521957221772SChristopher Siden 				    cb.cb_version);
5220eaca9bbdSeschrock 			}
5221eaca9bbdSeschrock 		}
522257221772SChristopher Siden 	} else if (argc == 0) {
522357221772SChristopher Siden 		cb.cb_first = B_TRUE;
522457221772SChristopher Siden 		ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
522557221772SChristopher Siden 		assert(ret == 0);
5226eaca9bbdSeschrock 
522757221772SChristopher Siden 		if (cb.cb_first) {
522857221772SChristopher Siden 			(void) printf(gettext("All pools are formatted "
522957221772SChristopher Siden 			    "using feature flags.\n\n"));
523057221772SChristopher Siden 		} else {
523157221772SChristopher Siden 			(void) printf(gettext("\nUse 'zpool upgrade -v' "
523257221772SChristopher Siden 			    "for a list of available legacy versions.\n"));
523357221772SChristopher Siden 		}
523457221772SChristopher Siden 
523557221772SChristopher Siden 		cb.cb_first = B_TRUE;
523657221772SChristopher Siden 		ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
523757221772SChristopher Siden 		assert(ret == 0);
523857221772SChristopher Siden 
523957221772SChristopher Siden 		if (cb.cb_first) {
524057221772SChristopher Siden 			(void) printf(gettext("Every feature flags pool has "
524157221772SChristopher Siden 			    "all supported features enabled.\n"));
524257221772SChristopher Siden 		} else {
524357221772SChristopher Siden 			(void) printf(gettext("\n"));
5244eaca9bbdSeschrock 		}
5245eaca9bbdSeschrock 	} else {
5246b1b8ab34Slling 		ret = for_each_pool(argc, argv, B_FALSE, NULL,
5247b1b8ab34Slling 		    upgrade_one, &cb);
524806eeb2adSek 	}
524906eeb2adSek 
525006eeb2adSek 	return (ret);
525106eeb2adSek }
525206eeb2adSek 
5253ecd6cf80Smarks typedef struct hist_cbdata {
5254ecd6cf80Smarks 	boolean_t first;
52554445fffbSMatthew Ahrens 	boolean_t longfmt;
52564445fffbSMatthew Ahrens 	boolean_t internal;
5257ecd6cf80Smarks } hist_cbdata_t;
5258ecd6cf80Smarks 
525906eeb2adSek /*
526006eeb2adSek  * Print out the command history for a specific pool.
526106eeb2adSek  */
526206eeb2adSek static int
526306eeb2adSek get_history_one(zpool_handle_t *zhp, void *data)
526406eeb2adSek {
526506eeb2adSek 	nvlist_t *nvhis;
526606eeb2adSek 	nvlist_t **records;
526706eeb2adSek 	uint_t numrecords;
526806eeb2adSek 	int ret, i;
5269ecd6cf80Smarks 	hist_cbdata_t *cb = (hist_cbdata_t *)data;
527006eeb2adSek 
5271ecd6cf80Smarks 	cb->first = B_FALSE;
527206eeb2adSek 
527306eeb2adSek 	(void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
527406eeb2adSek 
527506eeb2adSek 	if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
527606eeb2adSek 		return (ret);
527706eeb2adSek 
527806eeb2adSek 	verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
527906eeb2adSek 	    &records, &numrecords) == 0);
528006eeb2adSek 	for (i = 0; i < numrecords; i++) {
52814445fffbSMatthew Ahrens 		nvlist_t *rec = records[i];
52824445fffbSMatthew Ahrens 		char tbuf[30] = "";
5283ecd6cf80Smarks 
52844445fffbSMatthew Ahrens 		if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
52854445fffbSMatthew Ahrens 			time_t tsec;
52864445fffbSMatthew Ahrens 			struct tm t;
52874445fffbSMatthew Ahrens 
52884445fffbSMatthew Ahrens 			tsec = fnvlist_lookup_uint64(records[i],
52894445fffbSMatthew Ahrens 			    ZPOOL_HIST_TIME);
52904445fffbSMatthew Ahrens 			(void) localtime_r(&tsec, &t);
52914445fffbSMatthew Ahrens 			(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
52924445fffbSMatthew Ahrens 		}
5293ecd6cf80Smarks 
52944445fffbSMatthew Ahrens 		if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
52954445fffbSMatthew Ahrens 			(void) printf("%s %s", tbuf,
52964445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
52974445fffbSMatthew Ahrens 		} else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
52984445fffbSMatthew Ahrens 			int ievent =
52994445fffbSMatthew Ahrens 			    fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
53004445fffbSMatthew Ahrens 			if (!cb->internal)
53014445fffbSMatthew Ahrens 				continue;
53024445fffbSMatthew Ahrens 			if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
53034445fffbSMatthew Ahrens 				(void) printf("%s unrecognized record:\n",
53044445fffbSMatthew Ahrens 				    tbuf);
53054445fffbSMatthew Ahrens 				dump_nvlist(rec, 4);
53064445fffbSMatthew Ahrens 				continue;
53074445fffbSMatthew Ahrens 			}
53084445fffbSMatthew Ahrens 			(void) printf("%s [internal %s txg:%lld] %s", tbuf,
53094445fffbSMatthew Ahrens 			    zfs_history_event_names[ievent],
53104445fffbSMatthew Ahrens 			    fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
53114445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
53124445fffbSMatthew Ahrens 		} else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
53134445fffbSMatthew Ahrens 			if (!cb->internal)
5314ecd6cf80Smarks 				continue;
53154445fffbSMatthew Ahrens 			(void) printf("%s [txg:%lld] %s", tbuf,
53164445fffbSMatthew Ahrens 			    fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
53174445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
53184445fffbSMatthew Ahrens 			if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
53194445fffbSMatthew Ahrens 				(void) printf(" %s (%llu)",
53204445fffbSMatthew Ahrens 				    fnvlist_lookup_string(rec,
53214445fffbSMatthew Ahrens 				    ZPOOL_HIST_DSNAME),
53224445fffbSMatthew Ahrens 				    fnvlist_lookup_uint64(rec,
53234445fffbSMatthew Ahrens 				    ZPOOL_HIST_DSID));
53244445fffbSMatthew Ahrens 			}
53254445fffbSMatthew Ahrens 			(void) printf(" %s", fnvlist_lookup_string(rec,
53264445fffbSMatthew Ahrens 			    ZPOOL_HIST_INT_STR));
53274445fffbSMatthew Ahrens 		} else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
53284445fffbSMatthew Ahrens 			if (!cb->internal)
53294445fffbSMatthew Ahrens 				continue;
53304445fffbSMatthew Ahrens 			(void) printf("%s ioctl %s\n", tbuf,
53314445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
53324445fffbSMatthew Ahrens 			if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
53334445fffbSMatthew Ahrens 				(void) printf("    input:\n");
53344445fffbSMatthew Ahrens 				dump_nvlist(fnvlist_lookup_nvlist(rec,
53354445fffbSMatthew Ahrens 				    ZPOOL_HIST_INPUT_NVL), 8);
53364445fffbSMatthew Ahrens 			}
53374445fffbSMatthew Ahrens 			if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
53384445fffbSMatthew Ahrens 				(void) printf("    output:\n");
53394445fffbSMatthew Ahrens 				dump_nvlist(fnvlist_lookup_nvlist(rec,
53404445fffbSMatthew Ahrens 				    ZPOOL_HIST_OUTPUT_NVL), 8);
53414445fffbSMatthew Ahrens 			}
5342dfc11533SChris Williamson 			if (nvlist_exists(rec, ZPOOL_HIST_ERRNO)) {
5343dfc11533SChris Williamson 				(void) printf("    errno: %lld\n",
5344dfc11533SChris Williamson 				    fnvlist_lookup_int64(rec,
5345dfc11533SChris Williamson 				    ZPOOL_HIST_ERRNO));
5346dfc11533SChris Williamson 			}
53474445fffbSMatthew Ahrens 		} else {
53484445fffbSMatthew Ahrens 			if (!cb->internal)
5349ecd6cf80Smarks 				continue;
53504445fffbSMatthew Ahrens 			(void) printf("%s unrecognized record:\n", tbuf);
53514445fffbSMatthew Ahrens 			dump_nvlist(rec, 4);
535206eeb2adSek 		}
5353ecd6cf80Smarks 
5354ecd6cf80Smarks 		if (!cb->longfmt) {
5355ecd6cf80Smarks 			(void) printf("\n");
5356ecd6cf80Smarks 			continue;
5357ecd6cf80Smarks 		}
5358ecd6cf80Smarks 		(void) printf(" [");
53594445fffbSMatthew Ahrens 		if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
53604445fffbSMatthew Ahrens 			uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
53614445fffbSMatthew Ahrens 			struct passwd *pwd = getpwuid(who);
53624445fffbSMatthew Ahrens 			(void) printf("user %d ", (int)who);
53634445fffbSMatthew Ahrens 			if (pwd != NULL)
53644445fffbSMatthew Ahrens 				(void) printf("(%s) ", pwd->pw_name);
5365ecd6cf80Smarks 		}
53664445fffbSMatthew Ahrens 		if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
53674445fffbSMatthew Ahrens 			(void) printf("on %s",
53684445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
5369ecd6cf80Smarks 		}
53704445fffbSMatthew Ahrens 		if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
53714445fffbSMatthew Ahrens 			(void) printf(":%s",
53724445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
5373ecd6cf80Smarks 		}
5374ecd6cf80Smarks 		(void) printf("]");
5375ecd6cf80Smarks 		(void) printf("\n");
537606eeb2adSek 	}
537706eeb2adSek 	(void) printf("\n");
537806eeb2adSek 	nvlist_free(nvhis);
537906eeb2adSek 
538006eeb2adSek 	return (ret);
538106eeb2adSek }
538206eeb2adSek 
538306eeb2adSek /*
538406eeb2adSek  * zpool history <pool>
538506eeb2adSek  *
538606eeb2adSek  * Displays the history of commands that modified pools.
538706eeb2adSek  */
538806eeb2adSek int
538906eeb2adSek zpool_do_history(int argc, char **argv)
539006eeb2adSek {
5391ecd6cf80Smarks 	hist_cbdata_t cbdata = { 0 };
539206eeb2adSek 	int ret;
5393ecd6cf80Smarks 	int c;
539406eeb2adSek 
5395ecd6cf80Smarks 	cbdata.first = B_TRUE;
5396ecd6cf80Smarks 	/* check options */
5397ecd6cf80Smarks 	while ((c = getopt(argc, argv, "li")) != -1) {
5398ecd6cf80Smarks 		switch (c) {
5399ecd6cf80Smarks 		case 'l':
54004445fffbSMatthew Ahrens 			cbdata.longfmt = B_TRUE;
5401ecd6cf80Smarks 			break;
5402ecd6cf80Smarks 		case 'i':
54034445fffbSMatthew Ahrens 			cbdata.internal = B_TRUE;
5404ecd6cf80Smarks 			break;
5405ecd6cf80Smarks 		case '?':
5406ecd6cf80Smarks 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
5407ecd6cf80Smarks 			    optopt);
5408ecd6cf80Smarks 			usage(B_FALSE);
5409ecd6cf80Smarks 		}
5410ecd6cf80Smarks 	}
541106eeb2adSek 	argc -= optind;
541206eeb2adSek 	argv += optind;
541306eeb2adSek 
5414b1b8ab34Slling 	ret = for_each_pool(argc, argv, B_FALSE,  NULL, get_history_one,
5415ecd6cf80Smarks 	    &cbdata);
541606eeb2adSek 
5417ecd6cf80Smarks 	if (argc == 0 && cbdata.first == B_TRUE) {
541806eeb2adSek 		(void) printf(gettext("no pools available\n"));
541906eeb2adSek 		return (0);
5420eaca9bbdSeschrock 	}
5421eaca9bbdSeschrock 
5422eaca9bbdSeschrock 	return (ret);
5423eaca9bbdSeschrock }
5424eaca9bbdSeschrock 
5425b1b8ab34Slling static int
5426b1b8ab34Slling get_callback(zpool_handle_t *zhp, void *data)
5427b1b8ab34Slling {
5428990b4856Slling 	zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5429b1b8ab34Slling 	char value[MAXNAMELEN];
5430990b4856Slling 	zprop_source_t srctype;
5431990b4856Slling 	zprop_list_t *pl;
5432b1b8ab34Slling 
5433b1b8ab34Slling 	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5434b1b8ab34Slling 
5435b1b8ab34Slling 		/*
5436990b4856Slling 		 * Skip the special fake placeholder. This will also skip
5437990b4856Slling 		 * over the name property when 'all' is specified.
5438b1b8ab34Slling 		 */
5439990b4856Slling 		if (pl->pl_prop == ZPOOL_PROP_NAME &&
5440b1b8ab34Slling 		    pl == cbp->cb_proplist)
5441b1b8ab34Slling 			continue;
5442b1b8ab34Slling 
5443ad135b5dSChristopher Siden 		if (pl->pl_prop == ZPROP_INVAL &&
5444ad135b5dSChristopher Siden 		    (zpool_prop_feature(pl->pl_user_prop) ||
5445ad135b5dSChristopher Siden 		    zpool_prop_unsupported(pl->pl_user_prop))) {
5446ad135b5dSChristopher Siden 			srctype = ZPROP_SRC_LOCAL;
5447b1b8ab34Slling 
5448ad135b5dSChristopher Siden 			if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5449ad135b5dSChristopher Siden 			    value, sizeof (value)) == 0) {
5450ad135b5dSChristopher Siden 				zprop_print_one_property(zpool_get_name(zhp),
5451ad135b5dSChristopher Siden 				    cbp, pl->pl_user_prop, value, srctype,
5452ad135b5dSChristopher Siden 				    NULL, NULL);
5453ad135b5dSChristopher Siden 			}
5454ad135b5dSChristopher Siden 		} else {
5455ad135b5dSChristopher Siden 			if (zpool_get_prop(zhp, pl->pl_prop, value,
5456c58b3526SAdam Stevko 			    sizeof (value), &srctype, cbp->cb_literal) != 0)
5457ad135b5dSChristopher Siden 				continue;
5458ad135b5dSChristopher Siden 
5459ad135b5dSChristopher Siden 			zprop_print_one_property(zpool_get_name(zhp), cbp,
5460ad135b5dSChristopher Siden 			    zpool_prop_to_name(pl->pl_prop), value, srctype,
5461ad135b5dSChristopher Siden 			    NULL, NULL);
5462ad135b5dSChristopher Siden 		}
5463b1b8ab34Slling 	}
5464b1b8ab34Slling 	return (0);
5465b1b8ab34Slling }
5466b1b8ab34Slling 
5467c58b3526SAdam Stevko /*
5468c58b3526SAdam Stevko  * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
5469c58b3526SAdam Stevko  *
5470c58b3526SAdam Stevko  *	-H	Scripted mode.  Don't display headers, and separate properties
5471c58b3526SAdam Stevko  *		by a single tab.
5472c58b3526SAdam Stevko  *	-o	List of columns to display.  Defaults to
5473c58b3526SAdam Stevko  *		"name,property,value,source".
5474c58b3526SAdam Stevko  * 	-p	Diplay values in parsable (exact) format.
5475c58b3526SAdam Stevko  *
5476c58b3526SAdam Stevko  * Get properties of pools in the system. Output space statistics
5477c58b3526SAdam Stevko  * for each one as well as other attributes.
5478c58b3526SAdam Stevko  */
5479b1b8ab34Slling int
5480b1b8ab34Slling zpool_do_get(int argc, char **argv)
5481b1b8ab34Slling {
5482990b4856Slling 	zprop_get_cbdata_t cb = { 0 };
5483990b4856Slling 	zprop_list_t fake_name = { 0 };
5484b1b8ab34Slling 	int ret;
5485c58b3526SAdam Stevko 	int c, i;
5486c58b3526SAdam Stevko 	char *value;
5487b1b8ab34Slling 
5488b1b8ab34Slling 	cb.cb_first = B_TRUE;
5489c58b3526SAdam Stevko 
5490c58b3526SAdam Stevko 	/*
5491c58b3526SAdam Stevko 	 * Set up default columns and sources.
5492c58b3526SAdam Stevko 	 */
5493990b4856Slling 	cb.cb_sources = ZPROP_SRC_ALL;
5494b1b8ab34Slling 	cb.cb_columns[0] = GET_COL_NAME;
5495b1b8ab34Slling 	cb.cb_columns[1] = GET_COL_PROPERTY;
5496b1b8ab34Slling 	cb.cb_columns[2] = GET_COL_VALUE;
5497b1b8ab34Slling 	cb.cb_columns[3] = GET_COL_SOURCE;
5498990b4856Slling 	cb.cb_type = ZFS_TYPE_POOL;
5499b1b8ab34Slling 
5500c58b3526SAdam Stevko 	/* check options */
5501c58b3526SAdam Stevko 	while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
5502c58b3526SAdam Stevko 		switch (c) {
5503c58b3526SAdam Stevko 		case 'p':
5504c58b3526SAdam Stevko 			cb.cb_literal = B_TRUE;
5505c58b3526SAdam Stevko 			break;
5506c58b3526SAdam Stevko 		case 'H':
5507c58b3526SAdam Stevko 			cb.cb_scripted = B_TRUE;
5508c58b3526SAdam Stevko 			break;
5509c58b3526SAdam Stevko 		case 'o':
5510c58b3526SAdam Stevko 			bzero(&cb.cb_columns, sizeof (cb.cb_columns));
5511c58b3526SAdam Stevko 			i = 0;
5512c58b3526SAdam Stevko 			while (*optarg != '\0') {
5513c58b3526SAdam Stevko 				static char *col_subopts[] =
5514c58b3526SAdam Stevko 				{ "name", "property", "value", "source",
5515c58b3526SAdam Stevko 				"all", NULL };
5516c58b3526SAdam Stevko 
5517c58b3526SAdam Stevko 				if (i == ZFS_GET_NCOLS) {
5518c58b3526SAdam Stevko 					(void) fprintf(stderr, gettext("too "
5519c58b3526SAdam Stevko 					"many fields given to -o "
5520c58b3526SAdam Stevko 					"option\n"));
5521c58b3526SAdam Stevko 					usage(B_FALSE);
5522c58b3526SAdam Stevko 				}
5523c58b3526SAdam Stevko 
5524c58b3526SAdam Stevko 				switch (getsubopt(&optarg, col_subopts,
5525c58b3526SAdam Stevko 				    &value)) {
5526c58b3526SAdam Stevko 				case 0:
5527c58b3526SAdam Stevko 					cb.cb_columns[i++] = GET_COL_NAME;
5528c58b3526SAdam Stevko 					break;
5529c58b3526SAdam Stevko 				case 1:
5530c58b3526SAdam Stevko 					cb.cb_columns[i++] = GET_COL_PROPERTY;
5531c58b3526SAdam Stevko 					break;
5532c58b3526SAdam Stevko 				case 2:
5533c58b3526SAdam Stevko 					cb.cb_columns[i++] = GET_COL_VALUE;
5534c58b3526SAdam Stevko 					break;
5535c58b3526SAdam Stevko 				case 3:
5536c58b3526SAdam Stevko 					cb.cb_columns[i++] = GET_COL_SOURCE;
5537c58b3526SAdam Stevko 					break;
5538c58b3526SAdam Stevko 				case 4:
5539c58b3526SAdam Stevko 					if (i > 0) {
5540c58b3526SAdam Stevko 						(void) fprintf(stderr,
5541c58b3526SAdam Stevko 						    gettext("\"all\" conflicts "
5542c58b3526SAdam Stevko 						    "with specific fields "
5543c58b3526SAdam Stevko 						    "given to -o option\n"));
5544c58b3526SAdam Stevko 						usage(B_FALSE);
5545c58b3526SAdam Stevko 					}
5546c58b3526SAdam Stevko 					cb.cb_columns[0] = GET_COL_NAME;
5547c58b3526SAdam Stevko 					cb.cb_columns[1] = GET_COL_PROPERTY;
5548c58b3526SAdam Stevko 					cb.cb_columns[2] = GET_COL_VALUE;
5549c58b3526SAdam Stevko 					cb.cb_columns[3] = GET_COL_SOURCE;
5550c58b3526SAdam Stevko 					i = ZFS_GET_NCOLS;
5551c58b3526SAdam Stevko 					break;
5552c58b3526SAdam Stevko 				default:
5553c58b3526SAdam Stevko 					(void) fprintf(stderr,
5554c58b3526SAdam Stevko 					    gettext("invalid column name "
5555c58b3526SAdam Stevko 					    "'%s'\n"), value);
5556c58b3526SAdam Stevko 					usage(B_FALSE);
5557c58b3526SAdam Stevko 				}
5558c58b3526SAdam Stevko 			}
5559c58b3526SAdam Stevko 			break;
5560c58b3526SAdam Stevko 		case '?':
5561c58b3526SAdam Stevko 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
5562c58b3526SAdam Stevko 			    optopt);
5563c58b3526SAdam Stevko 			usage(B_FALSE);
5564c58b3526SAdam Stevko 		}
5565c58b3526SAdam Stevko 	}
5566c58b3526SAdam Stevko 
5567c58b3526SAdam Stevko 	argc -= optind;
5568c58b3526SAdam Stevko 	argv += optind;
5569c58b3526SAdam Stevko 
5570c58b3526SAdam Stevko 	if (argc < 1) {
5571c58b3526SAdam Stevko 		(void) fprintf(stderr, gettext("missing property "
5572c58b3526SAdam Stevko 		    "argument\n"));
5573c58b3526SAdam Stevko 		usage(B_FALSE);
5574c58b3526SAdam Stevko 	}
5575c58b3526SAdam Stevko 
5576c58b3526SAdam Stevko 	if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
5577990b4856Slling 	    ZFS_TYPE_POOL) != 0)
5578b1b8ab34Slling 		usage(B_FALSE);
5579b1b8ab34Slling 
5580c58b3526SAdam Stevko 	argc--;
5581c58b3526SAdam Stevko 	argv++;
5582c58b3526SAdam Stevko 
5583b1b8ab34Slling 	if (cb.cb_proplist != NULL) {
5584990b4856Slling 		fake_name.pl_prop = ZPOOL_PROP_NAME;
5585b1b8ab34Slling 		fake_name.pl_width = strlen(gettext("NAME"));
5586b1b8ab34Slling 		fake_name.pl_next = cb.cb_proplist;
5587b1b8ab34Slling 		cb.cb_proplist = &fake_name;
5588b1b8ab34Slling 	}
5589b1b8ab34Slling 
5590c58b3526SAdam Stevko 	ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
5591b1b8ab34Slling 	    get_callback, &cb);
5592b1b8ab34Slling 
5593b1b8ab34Slling 	if (cb.cb_proplist == &fake_name)
5594990b4856Slling 		zprop_free_list(fake_name.pl_next);
5595b1b8ab34Slling 	else
5596990b4856Slling 		zprop_free_list(cb.cb_proplist);
5597b1b8ab34Slling 
5598b1b8ab34Slling 	return (ret);
5599b1b8ab34Slling }
5600b1b8ab34Slling 
5601b1b8ab34Slling typedef struct set_cbdata {
5602b1b8ab34Slling 	char *cb_propname;
5603b1b8ab34Slling 	char *cb_value;
5604b1b8ab34Slling 	boolean_t cb_any_successful;
5605b1b8ab34Slling } set_cbdata_t;
5606b1b8ab34Slling 
5607b1b8ab34Slling int
5608b1b8ab34Slling set_callback(zpool_handle_t *zhp, void *data)
5609b1b8ab34Slling {
5610b1b8ab34Slling 	int error;
5611b1b8ab34Slling 	set_cbdata_t *cb = (set_cbdata_t *)data;
5612b1b8ab34Slling 
5613b1b8ab34Slling 	error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5614b1b8ab34Slling 
5615b1b8ab34Slling 	if (!error)
5616b1b8ab34Slling 		cb->cb_any_successful = B_TRUE;
5617b1b8ab34Slling 
5618b1b8ab34Slling 	return (error);
5619b1b8ab34Slling }
5620b1b8ab34Slling 
5621b1b8ab34Slling int
5622b1b8ab34Slling zpool_do_set(int argc, char **argv)
5623b1b8ab34Slling {
5624b1b8ab34Slling 	set_cbdata_t cb = { 0 };
5625b1b8ab34Slling 	int error;
5626b1b8ab34Slling 
5627b1b8ab34Slling 	if (argc > 1 && argv[1][0] == '-') {
5628b1b8ab34Slling 		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
5629b1b8ab34Slling 		    argv[1][1]);
5630b1b8ab34Slling 		usage(B_FALSE);
5631b1b8ab34Slling 	}
5632b1b8ab34Slling 
5633b1b8ab34Slling 	if (argc < 2) {
5634b1b8ab34Slling 		(void) fprintf(stderr, gettext("missing property=value "
5635b1b8ab34Slling 		    "argument\n"));
5636b1b8ab34Slling 		usage(B_FALSE);
5637b1b8ab34Slling 	}
5638b1b8ab34Slling 
5639b1b8ab34Slling 	if (argc < 3) {
5640b1b8ab34Slling 		(void) fprintf(stderr, gettext("missing pool name\n"));
5641b1b8ab34Slling 		usage(B_FALSE);
5642b1b8ab34Slling 	}
5643b1b8ab34Slling 
5644b1b8ab34Slling 	if (argc > 3) {
5645b1b8ab34Slling 		(void) fprintf(stderr, gettext("too many pool names\n"));
5646b1b8ab34Slling 		usage(B_FALSE);
5647b1b8ab34Slling 	}
5648b1b8ab34Slling 
5649b1b8ab34Slling 	cb.cb_propname = argv[1];
5650b1b8ab34Slling 	cb.cb_value = strchr(cb.cb_propname, '=');
5651b1b8ab34Slling 	if (cb.cb_value == NULL) {
5652b1b8ab34Slling 		(void) fprintf(stderr, gettext("missing value in "
5653b1b8ab34Slling 		    "property=value argument\n"));
5654b1b8ab34Slling 		usage(B_FALSE);
5655b1b8ab34Slling 	}
5656b1b8ab34Slling 
5657b1b8ab34Slling 	*(cb.cb_value) = '\0';
5658b1b8ab34Slling 	cb.cb_value++;
5659b1b8ab34Slling 
5660b1b8ab34Slling 	error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5661b1b8ab34Slling 	    set_callback, &cb);
5662b1b8ab34Slling 
5663b1b8ab34Slling 	return (error);
5664b1b8ab34Slling }
5665b1b8ab34Slling 
5666b1b8ab34Slling static int
5667b1b8ab34Slling find_command_idx(char *command, int *idx)
5668b1b8ab34Slling {
5669b1b8ab34Slling 	int i;
5670b1b8ab34Slling 
5671b1b8ab34Slling 	for (i = 0; i < NCOMMAND; i++) {
5672b1b8ab34Slling 		if (command_table[i].name == NULL)
5673b1b8ab34Slling 			continue;
5674b1b8ab34Slling 
5675b1b8ab34Slling 		if (strcmp(command, command_table[i].name) == 0) {
5676b1b8ab34Slling 			*idx = i;
5677b1b8ab34Slling 			return (0);
5678b1b8ab34Slling 		}
5679b1b8ab34Slling 	}
5680b1b8ab34Slling 	return (1);
5681b1b8ab34Slling }
5682b1b8ab34Slling 
5683fa9e4066Sahrens int
5684fa9e4066Sahrens main(int argc, char **argv)
5685fa9e4066Sahrens {
5686b327cd3fSIgor Kozhukhov 	int ret = 0;
5687fa9e4066Sahrens 	int i;
5688fa9e4066Sahrens 	char *cmdname;
5689fa9e4066Sahrens 
5690fa9e4066Sahrens 	(void) setlocale(LC_ALL, "");
5691fa9e4066Sahrens 	(void) textdomain(TEXT_DOMAIN);
5692fa9e4066Sahrens 
569399653d4eSeschrock 	if ((g_zfs = libzfs_init()) == NULL) {
569499653d4eSeschrock 		(void) fprintf(stderr, gettext("internal error: failed to "
5695203a47d8Snd 		    "initialize ZFS library\n"));
569699653d4eSeschrock 		return (1);
569799653d4eSeschrock 	}
569899653d4eSeschrock 
569999653d4eSeschrock 	libzfs_print_on_error(g_zfs, B_TRUE);
570099653d4eSeschrock 
5701fa9e4066Sahrens 	opterr = 0;
5702fa9e4066Sahrens 
5703fa9e4066Sahrens 	/*
5704fa9e4066Sahrens 	 * Make sure the user has specified some command.
5705fa9e4066Sahrens 	 */
5706fa9e4066Sahrens 	if (argc < 2) {
5707fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing command\n"));
570899653d4eSeschrock 		usage(B_FALSE);
5709fa9e4066Sahrens 	}
5710fa9e4066Sahrens 
5711fa9e4066Sahrens 	cmdname = argv[1];
5712fa9e4066Sahrens 
5713fa9e4066Sahrens 	/*
5714fa9e4066Sahrens 	 * Special case '-?'
5715fa9e4066Sahrens 	 */
5716fa9e4066Sahrens 	if (strcmp(cmdname, "-?") == 0)
571799653d4eSeschrock 		usage(B_TRUE);
5718fa9e4066Sahrens 
57194445fffbSMatthew Ahrens 	zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
57202a6b87f0Sek 
5721fa9e4066Sahrens 	/*
5722fa9e4066Sahrens 	 * Run the appropriate command.
5723fa9e4066Sahrens 	 */
5724b1b8ab34Slling 	if (find_command_idx(cmdname, &i) == 0) {
5725b1b8ab34Slling 		current_command = &command_table[i];
5726b1b8ab34Slling 		ret = command_table[i].func(argc - 1, argv + 1);
572791ebeef5Sahrens 	} else if (strchr(cmdname, '=')) {
572891ebeef5Sahrens 		verify(find_command_idx("set", &i) == 0);
572991ebeef5Sahrens 		current_command = &command_table[i];
573091ebeef5Sahrens 		ret = command_table[i].func(argc, argv);
573191ebeef5Sahrens 	} else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
573291ebeef5Sahrens 		/*
573391ebeef5Sahrens 		 * 'freeze' is a vile debugging abomination, so we treat
573491ebeef5Sahrens 		 * it as such.
573591ebeef5Sahrens 		 */
5736ea8dc4b6Seschrock 		char buf[16384];
5737ea8dc4b6Seschrock 		int fd = open(ZFS_DEV, O_RDWR);
5738fa9e4066Sahrens 		(void) strcpy((void *)buf, argv[2]);
5739fa9e4066Sahrens 		return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
574091ebeef5Sahrens 	} else {
5741fa9e4066Sahrens 		(void) fprintf(stderr, gettext("unrecognized "
5742fa9e4066Sahrens 		    "command '%s'\n"), cmdname);
574399653d4eSeschrock 		usage(B_FALSE);
5744fa9e4066Sahrens 	}
5745fa9e4066Sahrens 
57464445fffbSMatthew Ahrens 	if (ret == 0 && log_history)
57474445fffbSMatthew Ahrens 		(void) zpool_log_history(g_zfs, history_str);
57484445fffbSMatthew Ahrens 
574999653d4eSeschrock 	libzfs_fini(g_zfs);
575099653d4eSeschrock 
5751fa9e4066Sahrens 	/*
5752fa9e4066Sahrens 	 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5753fa9e4066Sahrens 	 * for the purposes of running ::findleaks.
5754fa9e4066Sahrens 	 */
5755fa9e4066Sahrens 	if (getenv("ZFS_ABORT") != NULL) {
5756fa9e4066Sahrens 		(void) printf("dumping core by request\n");
5757fa9e4066Sahrens 		abort();
5758fa9e4066Sahrens 	}
5759fa9e4066Sahrens 
5760fa9e4066Sahrens 	return (ret);
5761fa9e4066Sahrens }
5762