xref: /illumos-gate/usr/src/cmd/zpool/zpool_main.c (revision 468c413a79615e77179e8d98f22a7e513a8135bd)
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 /*
23379c004dSEric Schrock  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24fa9e4066Sahrens  * Use is subject to license terms.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #include <assert.h>
28fa9e4066Sahrens #include <ctype.h>
29fa9e4066Sahrens #include <dirent.h>
30fa9e4066Sahrens #include <errno.h>
31fa9e4066Sahrens #include <fcntl.h>
32fa9e4066Sahrens #include <libgen.h>
33fa9e4066Sahrens #include <libintl.h>
34fa9e4066Sahrens #include <libuutil.h>
35fa9e4066Sahrens #include <locale.h>
36fa9e4066Sahrens #include <stdio.h>
37fa9e4066Sahrens #include <stdlib.h>
38fa9e4066Sahrens #include <string.h>
39fa9e4066Sahrens #include <strings.h>
40fa9e4066Sahrens #include <unistd.h>
41fa9e4066Sahrens #include <priv.h>
42ecd6cf80Smarks #include <pwd.h>
43ecd6cf80Smarks #include <zone.h>
44b1b8ab34Slling #include <sys/fs/zfs.h>
45fa9e4066Sahrens 
46fa9e4066Sahrens #include <sys/stat.h>
47fa9e4066Sahrens 
48fa9e4066Sahrens #include <libzfs.h>
49fa9e4066Sahrens 
50fa9e4066Sahrens #include "zpool_util.h"
51b7b97454Sperrin #include "zfs_comutil.h"
52fa9e4066Sahrens 
5326fd7700SKrishnendu Sadhukhan - Sun Microsystems #include "statcommon.h"
5426fd7700SKrishnendu Sadhukhan - Sun Microsystems 
55fa9e4066Sahrens static int zpool_do_create(int, char **);
56fa9e4066Sahrens static int zpool_do_destroy(int, char **);
57fa9e4066Sahrens 
58fa9e4066Sahrens static int zpool_do_add(int, char **);
5999653d4eSeschrock static int zpool_do_remove(int, char **);
60fa9e4066Sahrens 
61fa9e4066Sahrens static int zpool_do_list(int, char **);
62fa9e4066Sahrens static int zpool_do_iostat(int, char **);
63fa9e4066Sahrens static int zpool_do_status(int, char **);
64fa9e4066Sahrens 
65fa9e4066Sahrens static int zpool_do_online(int, char **);
66fa9e4066Sahrens static int zpool_do_offline(int, char **);
67ea8dc4b6Seschrock static int zpool_do_clear(int, char **);
68fa9e4066Sahrens 
69fa9e4066Sahrens static int zpool_do_attach(int, char **);
70fa9e4066Sahrens static int zpool_do_detach(int, char **);
71fa9e4066Sahrens static int zpool_do_replace(int, char **);
72fa9e4066Sahrens 
73fa9e4066Sahrens static int zpool_do_scrub(int, char **);
74fa9e4066Sahrens 
75fa9e4066Sahrens static int zpool_do_import(int, char **);
76fa9e4066Sahrens static int zpool_do_export(int, char **);
77fa9e4066Sahrens 
78eaca9bbdSeschrock static int zpool_do_upgrade(int, char **);
79eaca9bbdSeschrock 
8006eeb2adSek static int zpool_do_history(int, char **);
8106eeb2adSek 
82b1b8ab34Slling static int zpool_do_get(int, char **);
83b1b8ab34Slling static int zpool_do_set(int, char **);
84b1b8ab34Slling 
85fa9e4066Sahrens /*
86fa9e4066Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
87fa9e4066Sahrens  * debugging facilities.
88fa9e4066Sahrens  */
8929ab75c9Srm 
9029ab75c9Srm #ifdef DEBUG
91fa9e4066Sahrens const char *
9299653d4eSeschrock _umem_debug_init(void)
93fa9e4066Sahrens {
94fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
95fa9e4066Sahrens }
96fa9e4066Sahrens 
97fa9e4066Sahrens const char *
98fa9e4066Sahrens _umem_logging_init(void)
99fa9e4066Sahrens {
100fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
101fa9e4066Sahrens }
10229ab75c9Srm #endif
103fa9e4066Sahrens 
10465cd9f28Seschrock typedef enum {
10565cd9f28Seschrock 	HELP_ADD,
10665cd9f28Seschrock 	HELP_ATTACH,
107ea8dc4b6Seschrock 	HELP_CLEAR,
10865cd9f28Seschrock 	HELP_CREATE,
10965cd9f28Seschrock 	HELP_DESTROY,
11065cd9f28Seschrock 	HELP_DETACH,
11165cd9f28Seschrock 	HELP_EXPORT,
11206eeb2adSek 	HELP_HISTORY,
11365cd9f28Seschrock 	HELP_IMPORT,
11465cd9f28Seschrock 	HELP_IOSTAT,
11565cd9f28Seschrock 	HELP_LIST,
11665cd9f28Seschrock 	HELP_OFFLINE,
11765cd9f28Seschrock 	HELP_ONLINE,
11865cd9f28Seschrock 	HELP_REPLACE,
11999653d4eSeschrock 	HELP_REMOVE,
12065cd9f28Seschrock 	HELP_SCRUB,
121eaca9bbdSeschrock 	HELP_STATUS,
122b1b8ab34Slling 	HELP_UPGRADE,
123b1b8ab34Slling 	HELP_GET,
124b1b8ab34Slling 	HELP_SET
12565cd9f28Seschrock } zpool_help_t;
12665cd9f28Seschrock 
12765cd9f28Seschrock 
128fa9e4066Sahrens typedef struct zpool_command {
129fa9e4066Sahrens 	const char	*name;
130fa9e4066Sahrens 	int		(*func)(int, char **);
13165cd9f28Seschrock 	zpool_help_t	usage;
132fa9e4066Sahrens } zpool_command_t;
133fa9e4066Sahrens 
134fa9e4066Sahrens /*
135fa9e4066Sahrens  * Master command table.  Each ZFS command has a name, associated function, and
136ea8dc4b6Seschrock  * usage message.  The usage messages need to be internationalized, so we have
137ea8dc4b6Seschrock  * to have a function to return the usage message based on a command index.
13865cd9f28Seschrock  *
13965cd9f28Seschrock  * These commands are organized according to how they are displayed in the usage
14065cd9f28Seschrock  * message.  An empty command (one with a NULL name) indicates an empty line in
14165cd9f28Seschrock  * the generic usage message.
142fa9e4066Sahrens  */
143fa9e4066Sahrens static zpool_command_t command_table[] = {
14465cd9f28Seschrock 	{ "create",	zpool_do_create,	HELP_CREATE		},
14565cd9f28Seschrock 	{ "destroy",	zpool_do_destroy,	HELP_DESTROY		},
146fa9e4066Sahrens 	{ NULL },
14765cd9f28Seschrock 	{ "add",	zpool_do_add,		HELP_ADD		},
14899653d4eSeschrock 	{ "remove",	zpool_do_remove,	HELP_REMOVE		},
149fa9e4066Sahrens 	{ NULL },
15065cd9f28Seschrock 	{ "list",	zpool_do_list,		HELP_LIST		},
15165cd9f28Seschrock 	{ "iostat",	zpool_do_iostat,	HELP_IOSTAT		},
15265cd9f28Seschrock 	{ "status",	zpool_do_status,	HELP_STATUS		},
153fa9e4066Sahrens 	{ NULL },
15465cd9f28Seschrock 	{ "online",	zpool_do_online,	HELP_ONLINE		},
15565cd9f28Seschrock 	{ "offline",	zpool_do_offline,	HELP_OFFLINE		},
156ea8dc4b6Seschrock 	{ "clear",	zpool_do_clear,		HELP_CLEAR		},
157fa9e4066Sahrens 	{ NULL },
15865cd9f28Seschrock 	{ "attach",	zpool_do_attach,	HELP_ATTACH		},
15965cd9f28Seschrock 	{ "detach",	zpool_do_detach,	HELP_DETACH		},
16065cd9f28Seschrock 	{ "replace",	zpool_do_replace,	HELP_REPLACE		},
161fa9e4066Sahrens 	{ NULL },
16265cd9f28Seschrock 	{ "scrub",	zpool_do_scrub,		HELP_SCRUB		},
163fa9e4066Sahrens 	{ NULL },
16465cd9f28Seschrock 	{ "import",	zpool_do_import,	HELP_IMPORT		},
16565cd9f28Seschrock 	{ "export",	zpool_do_export,	HELP_EXPORT		},
16606eeb2adSek 	{ "upgrade",	zpool_do_upgrade,	HELP_UPGRADE		},
16706eeb2adSek 	{ NULL },
168b1b8ab34Slling 	{ "history",	zpool_do_history,	HELP_HISTORY		},
169b1b8ab34Slling 	{ "get",	zpool_do_get,		HELP_GET		},
170b1b8ab34Slling 	{ "set",	zpool_do_set,		HELP_SET		},
171fa9e4066Sahrens };
172fa9e4066Sahrens 
173fa9e4066Sahrens #define	NCOMMAND	(sizeof (command_table) / sizeof (command_table[0]))
174fa9e4066Sahrens 
175fa9e4066Sahrens zpool_command_t *current_command;
1762a6b87f0Sek static char history_str[HIS_MAX_RECORD_LEN];
177fa9e4066Sahrens 
17826fd7700SKrishnendu Sadhukhan - Sun Microsystems static uint_t timestamp_fmt = NODATE;
17926fd7700SKrishnendu Sadhukhan - Sun Microsystems 
18065cd9f28Seschrock static const char *
18165cd9f28Seschrock get_usage(zpool_help_t idx) {
18265cd9f28Seschrock 	switch (idx) {
18365cd9f28Seschrock 	case HELP_ADD:
18465cd9f28Seschrock 		return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
18565cd9f28Seschrock 	case HELP_ATTACH:
18665cd9f28Seschrock 		return (gettext("\tattach [-f] <pool> <device> "
187e45ce728Sahrens 		    "<new-device>\n"));
188ea8dc4b6Seschrock 	case HELP_CLEAR:
189*468c413aSTim Haley 		return (gettext("\tclear [-nF] <pool> [device]\n"));
19065cd9f28Seschrock 	case HELP_CREATE:
191990b4856Slling 		return (gettext("\tcreate [-fn] [-o property=value] ... \n"
1920a48a24eStimh 		    "\t    [-O file-system-property=value] ... \n"
193990b4856Slling 		    "\t    [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
19465cd9f28Seschrock 	case HELP_DESTROY:
19565cd9f28Seschrock 		return (gettext("\tdestroy [-f] <pool>\n"));
19665cd9f28Seschrock 	case HELP_DETACH:
19765cd9f28Seschrock 		return (gettext("\tdetach <pool> <device>\n"));
19865cd9f28Seschrock 	case HELP_EXPORT:
19965cd9f28Seschrock 		return (gettext("\texport [-f] <pool> ...\n"));
20006eeb2adSek 	case HELP_HISTORY:
201ecd6cf80Smarks 		return (gettext("\thistory [-il] [<pool>] ...\n"));
20265cd9f28Seschrock 	case HELP_IMPORT:
2034c58d714Sdarrenm 		return (gettext("\timport [-d dir] [-D]\n"
204*468c413aSTim Haley 		    "\timport [-d dir | -c cachefile] [-n] -F <pool | id>\n"
2052f8aaab3Seschrock 		    "\timport [-o mntopts] [-o property=value] ... \n"
2062f8aaab3Seschrock 		    "\t    [-d dir | -c cachefile] [-D] [-f] [-R root] -a\n"
2072f8aaab3Seschrock 		    "\timport [-o mntopts] [-o property=value] ... \n"
2082f8aaab3Seschrock 		    "\t    [-d dir | -c cachefile] [-D] [-f] [-R root] "
2092f8aaab3Seschrock 		    "<pool | id> [newpool]\n"));
21065cd9f28Seschrock 	case HELP_IOSTAT:
21126fd7700SKrishnendu Sadhukhan - Sun Microsystems 		return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
21265cd9f28Seschrock 		    "[count]]\n"));
21365cd9f28Seschrock 	case HELP_LIST:
214990b4856Slling 		return (gettext("\tlist [-H] [-o property[,...]] "
215990b4856Slling 		    "[pool] ...\n"));
21665cd9f28Seschrock 	case HELP_OFFLINE:
217441d80aaSlling 		return (gettext("\toffline [-t] <pool> <device> ...\n"));
21865cd9f28Seschrock 	case HELP_ONLINE:
219441d80aaSlling 		return (gettext("\tonline <pool> <device> ...\n"));
22065cd9f28Seschrock 	case HELP_REPLACE:
22165cd9f28Seschrock 		return (gettext("\treplace [-f] <pool> <device> "
222e45ce728Sahrens 		    "[new-device]\n"));
22399653d4eSeschrock 	case HELP_REMOVE:
224fa94a07fSbrendan 		return (gettext("\tremove <pool> <device> ...\n"));
22565cd9f28Seschrock 	case HELP_SCRUB:
22665cd9f28Seschrock 		return (gettext("\tscrub [-s] <pool> ...\n"));
22765cd9f28Seschrock 	case HELP_STATUS:
22865cd9f28Seschrock 		return (gettext("\tstatus [-vx] [pool] ...\n"));
229eaca9bbdSeschrock 	case HELP_UPGRADE:
230eaca9bbdSeschrock 		return (gettext("\tupgrade\n"
231eaca9bbdSeschrock 		    "\tupgrade -v\n"
232990b4856Slling 		    "\tupgrade [-V version] <-a | pool ...>\n"));
233b1b8ab34Slling 	case HELP_GET:
234e45ce728Sahrens 		return (gettext("\tget <\"all\" | property[,...]> "
235b1b8ab34Slling 		    "<pool> ...\n"));
236b1b8ab34Slling 	case HELP_SET:
237b1b8ab34Slling 		return (gettext("\tset <property=value> <pool> \n"));
23865cd9f28Seschrock 	}
23965cd9f28Seschrock 
24065cd9f28Seschrock 	abort();
24165cd9f28Seschrock 	/* NOTREACHED */
24265cd9f28Seschrock }
24365cd9f28Seschrock 
244fa9e4066Sahrens 
245b1b8ab34Slling /*
246b1b8ab34Slling  * Callback routine that will print out a pool property value.
247b1b8ab34Slling  */
248990b4856Slling static int
249990b4856Slling print_prop_cb(int prop, void *cb)
250b1b8ab34Slling {
251b1b8ab34Slling 	FILE *fp = cb;
252b1b8ab34Slling 
253b1b8ab34Slling 	(void) fprintf(fp, "\t%-13s  ", zpool_prop_to_name(prop));
254b1b8ab34Slling 
255990b4856Slling 	if (zpool_prop_readonly(prop))
256990b4856Slling 		(void) fprintf(fp, "  NO   ");
257990b4856Slling 	else
258990b4856Slling 		(void) fprintf(fp, " YES    ");
259990b4856Slling 
260b1b8ab34Slling 	if (zpool_prop_values(prop) == NULL)
261b1b8ab34Slling 		(void) fprintf(fp, "-\n");
262b1b8ab34Slling 	else
263b1b8ab34Slling 		(void) fprintf(fp, "%s\n", zpool_prop_values(prop));
264b1b8ab34Slling 
265990b4856Slling 	return (ZPROP_CONT);
266b1b8ab34Slling }
267b1b8ab34Slling 
268fa9e4066Sahrens /*
269fa9e4066Sahrens  * Display usage message.  If we're inside a command, display only the usage for
270fa9e4066Sahrens  * that command.  Otherwise, iterate over the entire command table and display
271fa9e4066Sahrens  * a complete usage message.
272fa9e4066Sahrens  */
273fa9e4066Sahrens void
27499653d4eSeschrock usage(boolean_t requested)
275fa9e4066Sahrens {
276fa9e4066Sahrens 	FILE *fp = requested ? stdout : stderr;
277fa9e4066Sahrens 
278fa9e4066Sahrens 	if (current_command == NULL) {
279fa9e4066Sahrens 		int i;
280fa9e4066Sahrens 
281fa9e4066Sahrens 		(void) fprintf(fp, gettext("usage: zpool command args ...\n"));
282fa9e4066Sahrens 		(void) fprintf(fp,
283fa9e4066Sahrens 		    gettext("where 'command' is one of the following:\n\n"));
284fa9e4066Sahrens 
285fa9e4066Sahrens 		for (i = 0; i < NCOMMAND; i++) {
286fa9e4066Sahrens 			if (command_table[i].name == NULL)
287fa9e4066Sahrens 				(void) fprintf(fp, "\n");
288fa9e4066Sahrens 			else
289fa9e4066Sahrens 				(void) fprintf(fp, "%s",
29065cd9f28Seschrock 				    get_usage(command_table[i].usage));
291fa9e4066Sahrens 		}
292fa9e4066Sahrens 	} else {
293fa9e4066Sahrens 		(void) fprintf(fp, gettext("usage:\n"));
29465cd9f28Seschrock 		(void) fprintf(fp, "%s", get_usage(current_command->usage));
295fa9e4066Sahrens 	}
296fa9e4066Sahrens 
297b1b8ab34Slling 	if (current_command != NULL &&
298b1b8ab34Slling 	    ((strcmp(current_command->name, "set") == 0) ||
299990b4856Slling 	    (strcmp(current_command->name, "get") == 0) ||
300990b4856Slling 	    (strcmp(current_command->name, "list") == 0))) {
301b1b8ab34Slling 
302b1b8ab34Slling 		(void) fprintf(fp,
303b1b8ab34Slling 		    gettext("\nthe following properties are supported:\n"));
304b1b8ab34Slling 
305990b4856Slling 		(void) fprintf(fp, "\n\t%-13s  %s  %s\n\n",
306990b4856Slling 		    "PROPERTY", "EDIT", "VALUES");
307b1b8ab34Slling 
308b1b8ab34Slling 		/* Iterate over all properties */
309990b4856Slling 		(void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
310990b4856Slling 		    ZFS_TYPE_POOL);
311b1b8ab34Slling 	}
312b1b8ab34Slling 
313e9dbad6fSeschrock 	/*
314e9dbad6fSeschrock 	 * See comments at end of main().
315e9dbad6fSeschrock 	 */
316e9dbad6fSeschrock 	if (getenv("ZFS_ABORT") != NULL) {
317e9dbad6fSeschrock 		(void) printf("dumping core by request\n");
318e9dbad6fSeschrock 		abort();
319e9dbad6fSeschrock 	}
320e9dbad6fSeschrock 
321fa9e4066Sahrens 	exit(requested ? 0 : 2);
322fa9e4066Sahrens }
323fa9e4066Sahrens 
324fa9e4066Sahrens void
3258654d025Sperrin print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
3268654d025Sperrin     boolean_t print_logs)
327fa9e4066Sahrens {
328fa9e4066Sahrens 	nvlist_t **child;
329fa9e4066Sahrens 	uint_t c, children;
330afefbcddSeschrock 	char *vname;
331fa9e4066Sahrens 
332fa9e4066Sahrens 	if (name != NULL)
333fa9e4066Sahrens 		(void) printf("\t%*s%s\n", indent, "", name);
334fa9e4066Sahrens 
335fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
336fa9e4066Sahrens 	    &child, &children) != 0)
337fa9e4066Sahrens 		return;
338fa9e4066Sahrens 
339afefbcddSeschrock 	for (c = 0; c < children; c++) {
3408654d025Sperrin 		uint64_t is_log = B_FALSE;
3418654d025Sperrin 
3428654d025Sperrin 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3438654d025Sperrin 		    &is_log);
3448654d025Sperrin 		if ((is_log && !print_logs) || (!is_log && print_logs))
3458654d025Sperrin 			continue;
3468654d025Sperrin 
34788ecc943SGeorge Wilson 		vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3488654d025Sperrin 		print_vdev_tree(zhp, vname, child[c], indent + 2,
3498654d025Sperrin 		    B_FALSE);
350afefbcddSeschrock 		free(vname);
351afefbcddSeschrock 	}
352fa9e4066Sahrens }
353fa9e4066Sahrens 
354990b4856Slling /*
355990b4856Slling  * Add a property pair (name, string-value) into a property nvlist.
356990b4856Slling  */
357990b4856Slling static int
3580a48a24eStimh add_prop_list(const char *propname, char *propval, nvlist_t **props,
3590a48a24eStimh     boolean_t poolprop)
360990b4856Slling {
3610a48a24eStimh 	zpool_prop_t prop = ZPROP_INVAL;
3620a48a24eStimh 	zfs_prop_t fprop;
363990b4856Slling 	nvlist_t *proplist;
3640a48a24eStimh 	const char *normnm;
3650a48a24eStimh 	char *strval;
366990b4856Slling 
367990b4856Slling 	if (*props == NULL &&
368990b4856Slling 	    nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
369990b4856Slling 		(void) fprintf(stderr,
370990b4856Slling 		    gettext("internal error: out of memory\n"));
371990b4856Slling 		return (1);
372990b4856Slling 	}
373990b4856Slling 
374990b4856Slling 	proplist = *props;
375990b4856Slling 
3760a48a24eStimh 	if (poolprop) {
3770a48a24eStimh 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
3780a48a24eStimh 			(void) fprintf(stderr, gettext("property '%s' is "
3790a48a24eStimh 			    "not a valid pool property\n"), propname);
3800a48a24eStimh 			return (2);
3810a48a24eStimh 		}
3820a48a24eStimh 		normnm = zpool_prop_to_name(prop);
3830a48a24eStimh 	} else {
38414843421SMatthew Ahrens 		if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
38514843421SMatthew Ahrens 			normnm = zfs_prop_to_name(fprop);
38614843421SMatthew Ahrens 		} else {
38714843421SMatthew Ahrens 			normnm = propname;
3880a48a24eStimh 		}
389990b4856Slling 	}
390990b4856Slling 
3910a48a24eStimh 	if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
3920a48a24eStimh 	    prop != ZPOOL_PROP_CACHEFILE) {
393990b4856Slling 		(void) fprintf(stderr, gettext("property '%s' "
394990b4856Slling 		    "specified multiple times\n"), propname);
395990b4856Slling 		return (2);
396990b4856Slling 	}
397990b4856Slling 
3980a48a24eStimh 	if (nvlist_add_string(proplist, normnm, propval) != 0) {
399990b4856Slling 		(void) fprintf(stderr, gettext("internal "
400990b4856Slling 		    "error: out of memory\n"));
401990b4856Slling 		return (1);
402990b4856Slling 	}
403990b4856Slling 
404990b4856Slling 	return (0);
405990b4856Slling }
406990b4856Slling 
407fa9e4066Sahrens /*
408fa9e4066Sahrens  * zpool add [-fn] <pool> <vdev> ...
409fa9e4066Sahrens  *
410fa9e4066Sahrens  *	-f	Force addition of devices, even if they appear in use
411fa9e4066Sahrens  *	-n	Do not add the devices, but display the resulting layout if
412fa9e4066Sahrens  *		they were to be added.
413fa9e4066Sahrens  *
414fa9e4066Sahrens  * Adds the given vdevs to 'pool'.  As with create, the bulk of this work is
415fa9e4066Sahrens  * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
416fa9e4066Sahrens  * libzfs.
417fa9e4066Sahrens  */
418fa9e4066Sahrens int
419fa9e4066Sahrens zpool_do_add(int argc, char **argv)
420fa9e4066Sahrens {
42199653d4eSeschrock 	boolean_t force = B_FALSE;
42299653d4eSeschrock 	boolean_t dryrun = B_FALSE;
423fa9e4066Sahrens 	int c;
424fa9e4066Sahrens 	nvlist_t *nvroot;
425fa9e4066Sahrens 	char *poolname;
426fa9e4066Sahrens 	int ret;
427fa9e4066Sahrens 	zpool_handle_t *zhp;
428fa9e4066Sahrens 	nvlist_t *config;
429fa9e4066Sahrens 
430fa9e4066Sahrens 	/* check options */
431fa9e4066Sahrens 	while ((c = getopt(argc, argv, "fn")) != -1) {
432fa9e4066Sahrens 		switch (c) {
433fa9e4066Sahrens 		case 'f':
43499653d4eSeschrock 			force = B_TRUE;
435fa9e4066Sahrens 			break;
436fa9e4066Sahrens 		case 'n':
43799653d4eSeschrock 			dryrun = B_TRUE;
438fa9e4066Sahrens 			break;
439fa9e4066Sahrens 		case '?':
440fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
441fa9e4066Sahrens 			    optopt);
44299653d4eSeschrock 			usage(B_FALSE);
443fa9e4066Sahrens 		}
444fa9e4066Sahrens 	}
445fa9e4066Sahrens 
446fa9e4066Sahrens 	argc -= optind;
447fa9e4066Sahrens 	argv += optind;
448fa9e4066Sahrens 
449fa9e4066Sahrens 	/* get pool name and check number of arguments */
450fa9e4066Sahrens 	if (argc < 1) {
451fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
45299653d4eSeschrock 		usage(B_FALSE);
453fa9e4066Sahrens 	}
454fa9e4066Sahrens 	if (argc < 2) {
455fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing vdev specification\n"));
45699653d4eSeschrock 		usage(B_FALSE);
457fa9e4066Sahrens 	}
458fa9e4066Sahrens 
459fa9e4066Sahrens 	poolname = argv[0];
460fa9e4066Sahrens 
461fa9e4066Sahrens 	argc--;
462fa9e4066Sahrens 	argv++;
463fa9e4066Sahrens 
46499653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
465fa9e4066Sahrens 		return (1);
466fa9e4066Sahrens 
467088e9d47Seschrock 	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
468fa9e4066Sahrens 		(void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
469fa9e4066Sahrens 		    poolname);
470fa9e4066Sahrens 		zpool_close(zhp);
471fa9e4066Sahrens 		return (1);
472fa9e4066Sahrens 	}
473fa9e4066Sahrens 
474fa9e4066Sahrens 	/* pass off to get_vdev_spec for processing */
475705040edSEric Taylor 	nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
476705040edSEric Taylor 	    argc, argv);
477fa9e4066Sahrens 	if (nvroot == NULL) {
478fa9e4066Sahrens 		zpool_close(zhp);
479fa9e4066Sahrens 		return (1);
480fa9e4066Sahrens 	}
481fa9e4066Sahrens 
482fa9e4066Sahrens 	if (dryrun) {
483fa9e4066Sahrens 		nvlist_t *poolnvroot;
484fa9e4066Sahrens 
485fa9e4066Sahrens 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
486fa9e4066Sahrens 		    &poolnvroot) == 0);
487fa9e4066Sahrens 
488fa9e4066Sahrens 		(void) printf(gettext("would update '%s' to the following "
489fa9e4066Sahrens 		    "configuration:\n"), zpool_get_name(zhp));
490fa9e4066Sahrens 
4918654d025Sperrin 		/* print original main pool and new tree */
4928654d025Sperrin 		print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
4938654d025Sperrin 		print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
4948654d025Sperrin 
4958654d025Sperrin 		/* Do the same for the logs */
4968654d025Sperrin 		if (num_logs(poolnvroot) > 0) {
4978654d025Sperrin 			print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
4988654d025Sperrin 			print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
4998654d025Sperrin 		} else if (num_logs(nvroot) > 0) {
5008654d025Sperrin 			print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
5018654d025Sperrin 		}
502fa9e4066Sahrens 
503fa9e4066Sahrens 		ret = 0;
504fa9e4066Sahrens 	} else {
505fa9e4066Sahrens 		ret = (zpool_add(zhp, nvroot) != 0);
506fa9e4066Sahrens 	}
507fa9e4066Sahrens 
50899653d4eSeschrock 	nvlist_free(nvroot);
50999653d4eSeschrock 	zpool_close(zhp);
51099653d4eSeschrock 
51199653d4eSeschrock 	return (ret);
51299653d4eSeschrock }
51399653d4eSeschrock 
51499653d4eSeschrock /*
515fa94a07fSbrendan  * zpool remove <pool> <vdev> ...
51699653d4eSeschrock  *
51799653d4eSeschrock  * Removes the given vdev from the pool.  Currently, this only supports removing
518fa94a07fSbrendan  * spares and cache devices from the pool.  Eventually, we'll want to support
519fa94a07fSbrendan  * removing leaf vdevs (as an alias for 'detach') as well as toplevel vdevs.
52099653d4eSeschrock  */
52199653d4eSeschrock int
52299653d4eSeschrock zpool_do_remove(int argc, char **argv)
52399653d4eSeschrock {
52499653d4eSeschrock 	char *poolname;
525fa94a07fSbrendan 	int i, ret = 0;
52699653d4eSeschrock 	zpool_handle_t *zhp;
52799653d4eSeschrock 
52899653d4eSeschrock 	argc--;
52999653d4eSeschrock 	argv++;
53099653d4eSeschrock 
53199653d4eSeschrock 	/* get pool name and check number of arguments */
53299653d4eSeschrock 	if (argc < 1) {
53399653d4eSeschrock 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
53499653d4eSeschrock 		usage(B_FALSE);
53599653d4eSeschrock 	}
53699653d4eSeschrock 	if (argc < 2) {
53799653d4eSeschrock 		(void) fprintf(stderr, gettext("missing device\n"));
53899653d4eSeschrock 		usage(B_FALSE);
53999653d4eSeschrock 	}
54099653d4eSeschrock 
54199653d4eSeschrock 	poolname = argv[0];
54299653d4eSeschrock 
54399653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
54499653d4eSeschrock 		return (1);
54599653d4eSeschrock 
546fa94a07fSbrendan 	for (i = 1; i < argc; i++) {
547fa94a07fSbrendan 		if (zpool_vdev_remove(zhp, argv[i]) != 0)
548fa94a07fSbrendan 			ret = 1;
549fa94a07fSbrendan 	}
55099653d4eSeschrock 
551fa9e4066Sahrens 	return (ret);
552fa9e4066Sahrens }
553fa9e4066Sahrens 
554fa9e4066Sahrens /*
5550a48a24eStimh  * zpool create [-fn] [-o property=value] ...
5560a48a24eStimh  *		[-O file-system-property=value] ...
5570a48a24eStimh  *		[-R root] [-m mountpoint] <pool> <dev> ...
558fa9e4066Sahrens  *
559fa9e4066Sahrens  *	-f	Force creation, even if devices appear in use
560fa9e4066Sahrens  *	-n	Do not create the pool, but display the resulting layout if it
561fa9e4066Sahrens  *		were to be created.
562fa9e4066Sahrens  *      -R	Create a pool under an alternate root
563fa9e4066Sahrens  *      -m	Set default mountpoint for the root dataset.  By default it's
564fa9e4066Sahrens  *      	'/<pool>'
565990b4856Slling  *	-o	Set property=value.
5660a48a24eStimh  *	-O	Set fsproperty=value in the pool's root file system
567fa9e4066Sahrens  *
568b1b8ab34Slling  * Creates the named pool according to the given vdev specification.  The
569fa9e4066Sahrens  * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c.  Once
570fa9e4066Sahrens  * we get the nvlist back from get_vdev_spec(), we either print out the contents
571fa9e4066Sahrens  * (if '-n' was specified), or pass it to libzfs to do the creation.
572fa9e4066Sahrens  */
573fa9e4066Sahrens int
574fa9e4066Sahrens zpool_do_create(int argc, char **argv)
575fa9e4066Sahrens {
57699653d4eSeschrock 	boolean_t force = B_FALSE;
57799653d4eSeschrock 	boolean_t dryrun = B_FALSE;
578fa9e4066Sahrens 	int c;
579990b4856Slling 	nvlist_t *nvroot = NULL;
580fa9e4066Sahrens 	char *poolname;
581990b4856Slling 	int ret = 1;
582fa9e4066Sahrens 	char *altroot = NULL;
583fa9e4066Sahrens 	char *mountpoint = NULL;
5840a48a24eStimh 	nvlist_t *fsprops = NULL;
585990b4856Slling 	nvlist_t *props = NULL;
5862f8aaab3Seschrock 	char *propval;
587fa9e4066Sahrens 
588fa9e4066Sahrens 	/* check options */
5890a48a24eStimh 	while ((c = getopt(argc, argv, ":fnR:m:o:O:")) != -1) {
590fa9e4066Sahrens 		switch (c) {
591fa9e4066Sahrens 		case 'f':
59299653d4eSeschrock 			force = B_TRUE;
593fa9e4066Sahrens 			break;
594fa9e4066Sahrens 		case 'n':
59599653d4eSeschrock 			dryrun = B_TRUE;
596fa9e4066Sahrens 			break;
597fa9e4066Sahrens 		case 'R':
598fa9e4066Sahrens 			altroot = optarg;
599990b4856Slling 			if (add_prop_list(zpool_prop_to_name(
6000a48a24eStimh 			    ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
601990b4856Slling 				goto errout;
6022f8aaab3Seschrock 			if (nvlist_lookup_string(props,
6032f8aaab3Seschrock 			    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
6042f8aaab3Seschrock 			    &propval) == 0)
6052f8aaab3Seschrock 				break;
606990b4856Slling 			if (add_prop_list(zpool_prop_to_name(
6070a48a24eStimh 			    ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
608990b4856Slling 				goto errout;
609fa9e4066Sahrens 			break;
610fa9e4066Sahrens 		case 'm':
611fa9e4066Sahrens 			mountpoint = optarg;
612fa9e4066Sahrens 			break;
613990b4856Slling 		case 'o':
614990b4856Slling 			if ((propval = strchr(optarg, '=')) == NULL) {
615990b4856Slling 				(void) fprintf(stderr, gettext("missing "
616990b4856Slling 				    "'=' for -o option\n"));
617990b4856Slling 				goto errout;
618990b4856Slling 			}
619990b4856Slling 			*propval = '\0';
620990b4856Slling 			propval++;
621990b4856Slling 
6220a48a24eStimh 			if (add_prop_list(optarg, propval, &props, B_TRUE))
6230a48a24eStimh 				goto errout;
6240a48a24eStimh 			break;
6250a48a24eStimh 		case 'O':
6260a48a24eStimh 			if ((propval = strchr(optarg, '=')) == NULL) {
6270a48a24eStimh 				(void) fprintf(stderr, gettext("missing "
6280a48a24eStimh 				    "'=' for -O option\n"));
6290a48a24eStimh 				goto errout;
6300a48a24eStimh 			}
6310a48a24eStimh 			*propval = '\0';
6320a48a24eStimh 			propval++;
6330a48a24eStimh 
6340a48a24eStimh 			if (add_prop_list(optarg, propval, &fsprops, B_FALSE))
635990b4856Slling 				goto errout;
636990b4856Slling 			break;
637fa9e4066Sahrens 		case ':':
638fa9e4066Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
639fa9e4066Sahrens 			    "'%c' option\n"), optopt);
640990b4856Slling 			goto badusage;
641fa9e4066Sahrens 		case '?':
642fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
643fa9e4066Sahrens 			    optopt);
644990b4856Slling 			goto badusage;
645fa9e4066Sahrens 		}
646fa9e4066Sahrens 	}
647fa9e4066Sahrens 
648fa9e4066Sahrens 	argc -= optind;
649fa9e4066Sahrens 	argv += optind;
650fa9e4066Sahrens 
651fa9e4066Sahrens 	/* get pool name and check number of arguments */
652fa9e4066Sahrens 	if (argc < 1) {
653fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
654990b4856Slling 		goto badusage;
655fa9e4066Sahrens 	}
656fa9e4066Sahrens 	if (argc < 2) {
657fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing vdev specification\n"));
658990b4856Slling 		goto badusage;
659fa9e4066Sahrens 	}
660fa9e4066Sahrens 
661fa9e4066Sahrens 	poolname = argv[0];
662fa9e4066Sahrens 
663fa9e4066Sahrens 	/*
664fa9e4066Sahrens 	 * As a special case, check for use of '/' in the name, and direct the
665fa9e4066Sahrens 	 * user to use 'zfs create' instead.
666fa9e4066Sahrens 	 */
667fa9e4066Sahrens 	if (strchr(poolname, '/') != NULL) {
668fa9e4066Sahrens 		(void) fprintf(stderr, gettext("cannot create '%s': invalid "
669fa9e4066Sahrens 		    "character '/' in pool name\n"), poolname);
670fa9e4066Sahrens 		(void) fprintf(stderr, gettext("use 'zfs create' to "
671fa9e4066Sahrens 		    "create a dataset\n"));
672990b4856Slling 		goto errout;
673fa9e4066Sahrens 	}
674fa9e4066Sahrens 
675fa9e4066Sahrens 	/* pass off to get_vdev_spec for bulk processing */
676705040edSEric Taylor 	nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
677705040edSEric Taylor 	    argc - 1, argv + 1);
678fa9e4066Sahrens 	if (nvroot == NULL)
6790a48a24eStimh 		goto errout;
680fa9e4066Sahrens 
68199653d4eSeschrock 	/* make_root_vdev() allows 0 toplevel children if there are spares */
682b7b97454Sperrin 	if (!zfs_allocatable_devs(nvroot)) {
68399653d4eSeschrock 		(void) fprintf(stderr, gettext("invalid vdev "
68499653d4eSeschrock 		    "specification: at least one toplevel vdev must be "
68599653d4eSeschrock 		    "specified\n"));
686990b4856Slling 		goto errout;
68799653d4eSeschrock 	}
68899653d4eSeschrock 
68999653d4eSeschrock 
690fa9e4066Sahrens 	if (altroot != NULL && altroot[0] != '/') {
691fa9e4066Sahrens 		(void) fprintf(stderr, gettext("invalid alternate root '%s': "
692e9dbad6fSeschrock 		    "must be an absolute path\n"), altroot);
693990b4856Slling 		goto errout;
694fa9e4066Sahrens 	}
695fa9e4066Sahrens 
696fa9e4066Sahrens 	/*
697fa9e4066Sahrens 	 * Check the validity of the mountpoint and direct the user to use the
698fa9e4066Sahrens 	 * '-m' mountpoint option if it looks like its in use.
699fa9e4066Sahrens 	 */
700fa9e4066Sahrens 	if (mountpoint == NULL ||
701fa9e4066Sahrens 	    (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
702fa9e4066Sahrens 	    strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
703fa9e4066Sahrens 		char buf[MAXPATHLEN];
70411022c7cStimh 		DIR *dirp;
705fa9e4066Sahrens 
706fa9e4066Sahrens 		if (mountpoint && mountpoint[0] != '/') {
707fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid mountpoint "
708fa9e4066Sahrens 			    "'%s': must be an absolute path, 'legacy', or "
709fa9e4066Sahrens 			    "'none'\n"), mountpoint);
710990b4856Slling 			goto errout;
711fa9e4066Sahrens 		}
712fa9e4066Sahrens 
713fa9e4066Sahrens 		if (mountpoint == NULL) {
714fa9e4066Sahrens 			if (altroot != NULL)
715fa9e4066Sahrens 				(void) snprintf(buf, sizeof (buf), "%s/%s",
716fa9e4066Sahrens 				    altroot, poolname);
717fa9e4066Sahrens 			else
718fa9e4066Sahrens 				(void) snprintf(buf, sizeof (buf), "/%s",
719fa9e4066Sahrens 				    poolname);
720fa9e4066Sahrens 		} else {
721fa9e4066Sahrens 			if (altroot != NULL)
722fa9e4066Sahrens 				(void) snprintf(buf, sizeof (buf), "%s%s",
723fa9e4066Sahrens 				    altroot, mountpoint);
724fa9e4066Sahrens 			else
725fa9e4066Sahrens 				(void) snprintf(buf, sizeof (buf), "%s",
726fa9e4066Sahrens 				    mountpoint);
727fa9e4066Sahrens 		}
728fa9e4066Sahrens 
72911022c7cStimh 		if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
73011022c7cStimh 			(void) fprintf(stderr, gettext("mountpoint '%s' : "
73111022c7cStimh 			    "%s\n"), buf, strerror(errno));
732fa9e4066Sahrens 			(void) fprintf(stderr, gettext("use '-m' "
733fa9e4066Sahrens 			    "option to provide a different default\n"));
734990b4856Slling 			goto errout;
73511022c7cStimh 		} else if (dirp) {
73611022c7cStimh 			int count = 0;
73711022c7cStimh 
73811022c7cStimh 			while (count < 3 && readdir(dirp) != NULL)
73911022c7cStimh 				count++;
74011022c7cStimh 			(void) closedir(dirp);
74111022c7cStimh 
74211022c7cStimh 			if (count > 2) {
74311022c7cStimh 				(void) fprintf(stderr, gettext("mountpoint "
74411022c7cStimh 				    "'%s' exists and is not empty\n"), buf);
74511022c7cStimh 				(void) fprintf(stderr, gettext("use '-m' "
74611022c7cStimh 				    "option to provide a "
74711022c7cStimh 				    "different default\n"));
74811022c7cStimh 				goto errout;
74911022c7cStimh 			}
750fa9e4066Sahrens 		}
751fa9e4066Sahrens 	}
752fa9e4066Sahrens 
753fa9e4066Sahrens 	if (dryrun) {
754fa9e4066Sahrens 		/*
755fa9e4066Sahrens 		 * For a dry run invocation, print out a basic message and run
756fa9e4066Sahrens 		 * through all the vdevs in the list and print out in an
757fa9e4066Sahrens 		 * appropriate hierarchy.
758fa9e4066Sahrens 		 */
759fa9e4066Sahrens 		(void) printf(gettext("would create '%s' with the "
760fa9e4066Sahrens 		    "following layout:\n\n"), poolname);
761fa9e4066Sahrens 
7628654d025Sperrin 		print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
7638654d025Sperrin 		if (num_logs(nvroot) > 0)
7648654d025Sperrin 			print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
765fa9e4066Sahrens 
766fa9e4066Sahrens 		ret = 0;
767fa9e4066Sahrens 	} else {
768fa9e4066Sahrens 		/*
769fa9e4066Sahrens 		 * Hand off to libzfs.
770fa9e4066Sahrens 		 */
7710a48a24eStimh 		if (zpool_create(g_zfs, poolname,
7720a48a24eStimh 		    nvroot, props, fsprops) == 0) {
77399653d4eSeschrock 			zfs_handle_t *pool = zfs_open(g_zfs, poolname,
774fa9e4066Sahrens 			    ZFS_TYPE_FILESYSTEM);
775fa9e4066Sahrens 			if (pool != NULL) {
776fa9e4066Sahrens 				if (mountpoint != NULL)
777fa9e4066Sahrens 					verify(zfs_prop_set(pool,
778e9dbad6fSeschrock 					    zfs_prop_to_name(
779e9dbad6fSeschrock 					    ZFS_PROP_MOUNTPOINT),
780fa9e4066Sahrens 					    mountpoint) == 0);
781fa9e4066Sahrens 				if (zfs_mount(pool, NULL, 0) == 0)
782da6c28aaSamw 					ret = zfs_shareall(pool);
783fa9e4066Sahrens 				zfs_close(pool);
784fa9e4066Sahrens 			}
78599653d4eSeschrock 		} else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
78699653d4eSeschrock 			(void) fprintf(stderr, gettext("pool name may have "
78799653d4eSeschrock 			    "been omitted\n"));
788fa9e4066Sahrens 		}
789fa9e4066Sahrens 	}
790fa9e4066Sahrens 
791990b4856Slling errout:
7922f8aaab3Seschrock 	nvlist_free(nvroot);
7930a48a24eStimh 	nvlist_free(fsprops);
7942f8aaab3Seschrock 	nvlist_free(props);
795fa9e4066Sahrens 	return (ret);
796990b4856Slling badusage:
7970a48a24eStimh 	nvlist_free(fsprops);
798990b4856Slling 	nvlist_free(props);
799990b4856Slling 	usage(B_FALSE);
800990b4856Slling 	return (2);
801fa9e4066Sahrens }
802fa9e4066Sahrens 
803fa9e4066Sahrens /*
804fa9e4066Sahrens  * zpool destroy <pool>
805fa9e4066Sahrens  *
806fa9e4066Sahrens  * 	-f	Forcefully unmount any datasets
807fa9e4066Sahrens  *
808fa9e4066Sahrens  * Destroy the given pool.  Automatically unmounts any datasets in the pool.
809fa9e4066Sahrens  */
810fa9e4066Sahrens int
811fa9e4066Sahrens zpool_do_destroy(int argc, char **argv)
812fa9e4066Sahrens {
81399653d4eSeschrock 	boolean_t force = B_FALSE;
814fa9e4066Sahrens 	int c;
815fa9e4066Sahrens 	char *pool;
816fa9e4066Sahrens 	zpool_handle_t *zhp;
817fa9e4066Sahrens 	int ret;
818fa9e4066Sahrens 
819fa9e4066Sahrens 	/* check options */
820fa9e4066Sahrens 	while ((c = getopt(argc, argv, "f")) != -1) {
821fa9e4066Sahrens 		switch (c) {
822fa9e4066Sahrens 		case 'f':
82399653d4eSeschrock 			force = B_TRUE;
824fa9e4066Sahrens 			break;
825fa9e4066Sahrens 		case '?':
826fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
827fa9e4066Sahrens 			    optopt);
82899653d4eSeschrock 			usage(B_FALSE);
829fa9e4066Sahrens 		}
830fa9e4066Sahrens 	}
831fa9e4066Sahrens 
832fa9e4066Sahrens 	argc -= optind;
833fa9e4066Sahrens 	argv += optind;
834fa9e4066Sahrens 
835fa9e4066Sahrens 	/* check arguments */
836fa9e4066Sahrens 	if (argc < 1) {
837fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool argument\n"));
83899653d4eSeschrock 		usage(B_FALSE);
839fa9e4066Sahrens 	}
840fa9e4066Sahrens 	if (argc > 1) {
841fa9e4066Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
84299653d4eSeschrock 		usage(B_FALSE);
843fa9e4066Sahrens 	}
844fa9e4066Sahrens 
845fa9e4066Sahrens 	pool = argv[0];
846fa9e4066Sahrens 
84799653d4eSeschrock 	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
848fa9e4066Sahrens 		/*
849fa9e4066Sahrens 		 * As a special case, check for use of '/' in the name, and
850fa9e4066Sahrens 		 * direct the user to use 'zfs destroy' instead.
851fa9e4066Sahrens 		 */
852fa9e4066Sahrens 		if (strchr(pool, '/') != NULL)
853fa9e4066Sahrens 			(void) fprintf(stderr, gettext("use 'zfs destroy' to "
854fa9e4066Sahrens 			    "destroy a dataset\n"));
855fa9e4066Sahrens 		return (1);
856fa9e4066Sahrens 	}
857fa9e4066Sahrens 
858f3861e1aSahl 	if (zpool_disable_datasets(zhp, force) != 0) {
859fa9e4066Sahrens 		(void) fprintf(stderr, gettext("could not destroy '%s': "
860fa9e4066Sahrens 		    "could not unmount datasets\n"), zpool_get_name(zhp));
861fa9e4066Sahrens 		return (1);
862fa9e4066Sahrens 	}
863fa9e4066Sahrens 
864fa9e4066Sahrens 	ret = (zpool_destroy(zhp) != 0);
865fa9e4066Sahrens 
866fa9e4066Sahrens 	zpool_close(zhp);
867fa9e4066Sahrens 
868fa9e4066Sahrens 	return (ret);
869fa9e4066Sahrens }
870fa9e4066Sahrens 
871fa9e4066Sahrens /*
872fa9e4066Sahrens  * zpool export [-f] <pool> ...
873fa9e4066Sahrens  *
874fa9e4066Sahrens  *	-f	Forcefully unmount datasets
875fa9e4066Sahrens  *
876b1b8ab34Slling  * Export the given pools.  By default, the command will attempt to cleanly
877fa9e4066Sahrens  * unmount any active datasets within the pool.  If the '-f' flag is specified,
878fa9e4066Sahrens  * then the datasets will be forcefully unmounted.
879fa9e4066Sahrens  */
880fa9e4066Sahrens int
881fa9e4066Sahrens zpool_do_export(int argc, char **argv)
882fa9e4066Sahrens {
88399653d4eSeschrock 	boolean_t force = B_FALSE;
884394ab0cbSGeorge Wilson 	boolean_t hardforce = B_FALSE;
885fa9e4066Sahrens 	int c;
886fa9e4066Sahrens 	zpool_handle_t *zhp;
887fa9e4066Sahrens 	int ret;
888fa9e4066Sahrens 	int i;
889fa9e4066Sahrens 
890fa9e4066Sahrens 	/* check options */
891394ab0cbSGeorge Wilson 	while ((c = getopt(argc, argv, "fF")) != -1) {
892fa9e4066Sahrens 		switch (c) {
893fa9e4066Sahrens 		case 'f':
89499653d4eSeschrock 			force = B_TRUE;
895fa9e4066Sahrens 			break;
896394ab0cbSGeorge Wilson 		case 'F':
897394ab0cbSGeorge Wilson 			hardforce = B_TRUE;
898394ab0cbSGeorge Wilson 			break;
899fa9e4066Sahrens 		case '?':
900fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
901fa9e4066Sahrens 			    optopt);
90299653d4eSeschrock 			usage(B_FALSE);
903fa9e4066Sahrens 		}
904fa9e4066Sahrens 	}
905fa9e4066Sahrens 
906fa9e4066Sahrens 	argc -= optind;
907fa9e4066Sahrens 	argv += optind;
908fa9e4066Sahrens 
909fa9e4066Sahrens 	/* check arguments */
910fa9e4066Sahrens 	if (argc < 1) {
911fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool argument\n"));
91299653d4eSeschrock 		usage(B_FALSE);
913fa9e4066Sahrens 	}
914fa9e4066Sahrens 
915fa9e4066Sahrens 	ret = 0;
916fa9e4066Sahrens 	for (i = 0; i < argc; i++) {
91799653d4eSeschrock 		if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
918fa9e4066Sahrens 			ret = 1;
919fa9e4066Sahrens 			continue;
920fa9e4066Sahrens 		}
921fa9e4066Sahrens 
922f3861e1aSahl 		if (zpool_disable_datasets(zhp, force) != 0) {
923fa9e4066Sahrens 			ret = 1;
924fa9e4066Sahrens 			zpool_close(zhp);
925fa9e4066Sahrens 			continue;
926fa9e4066Sahrens 		}
927fa9e4066Sahrens 
928394ab0cbSGeorge Wilson 		if (hardforce) {
929394ab0cbSGeorge Wilson 			if (zpool_export_force(zhp) != 0)
930394ab0cbSGeorge Wilson 				ret = 1;
931394ab0cbSGeorge Wilson 		} else if (zpool_export(zhp, force) != 0) {
932fa9e4066Sahrens 			ret = 1;
933394ab0cbSGeorge Wilson 		}
934fa9e4066Sahrens 
935fa9e4066Sahrens 		zpool_close(zhp);
936fa9e4066Sahrens 	}
937fa9e4066Sahrens 
938fa9e4066Sahrens 	return (ret);
939fa9e4066Sahrens }
940fa9e4066Sahrens 
941fa9e4066Sahrens /*
942fa9e4066Sahrens  * Given a vdev configuration, determine the maximum width needed for the device
943fa9e4066Sahrens  * name column.
944fa9e4066Sahrens  */
945fa9e4066Sahrens static int
946c67d9675Seschrock max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
947fa9e4066Sahrens {
94888ecc943SGeorge Wilson 	char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
949fa9e4066Sahrens 	nvlist_t **child;
950fa9e4066Sahrens 	uint_t c, children;
951fa9e4066Sahrens 	int ret;
952fa9e4066Sahrens 
953fa9e4066Sahrens 	if (strlen(name) + depth > max)
954fa9e4066Sahrens 		max = strlen(name) + depth;
955fa9e4066Sahrens 
956afefbcddSeschrock 	free(name);
957afefbcddSeschrock 
95899653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
95999653d4eSeschrock 	    &child, &children) == 0) {
96099653d4eSeschrock 		for (c = 0; c < children; c++)
96199653d4eSeschrock 			if ((ret = max_width(zhp, child[c], depth + 2,
96299653d4eSeschrock 			    max)) > max)
96399653d4eSeschrock 				max = ret;
96499653d4eSeschrock 	}
96599653d4eSeschrock 
966fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
967fa94a07fSbrendan 	    &child, &children) == 0) {
968fa94a07fSbrendan 		for (c = 0; c < children; c++)
969fa94a07fSbrendan 			if ((ret = max_width(zhp, child[c], depth + 2,
970fa94a07fSbrendan 			    max)) > max)
971fa94a07fSbrendan 				max = ret;
972fa94a07fSbrendan 	}
973fa94a07fSbrendan 
974fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
97599653d4eSeschrock 	    &child, &children) == 0) {
97699653d4eSeschrock 		for (c = 0; c < children; c++)
97799653d4eSeschrock 			if ((ret = max_width(zhp, child[c], depth + 2,
97899653d4eSeschrock 			    max)) > max)
97999653d4eSeschrock 				max = ret;
98099653d4eSeschrock 	}
981fa9e4066Sahrens 
982fa9e4066Sahrens 
983fa9e4066Sahrens 	return (max);
984fa9e4066Sahrens }
985fa9e4066Sahrens 
986e6ca193dSGeorge Wilson typedef struct spare_cbdata {
987e6ca193dSGeorge Wilson 	uint64_t	cb_guid;
988e6ca193dSGeorge Wilson 	zpool_handle_t	*cb_zhp;
989e6ca193dSGeorge Wilson } spare_cbdata_t;
990e6ca193dSGeorge Wilson 
991e6ca193dSGeorge Wilson static boolean_t
992e6ca193dSGeorge Wilson find_vdev(nvlist_t *nv, uint64_t search)
993e6ca193dSGeorge Wilson {
994e6ca193dSGeorge Wilson 	uint64_t guid;
995e6ca193dSGeorge Wilson 	nvlist_t **child;
996e6ca193dSGeorge Wilson 	uint_t c, children;
997e6ca193dSGeorge Wilson 
998e6ca193dSGeorge Wilson 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
999e6ca193dSGeorge Wilson 	    search == guid)
1000e6ca193dSGeorge Wilson 		return (B_TRUE);
1001e6ca193dSGeorge Wilson 
1002e6ca193dSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1003e6ca193dSGeorge Wilson 	    &child, &children) == 0) {
1004e6ca193dSGeorge Wilson 		for (c = 0; c < children; c++)
1005e6ca193dSGeorge Wilson 			if (find_vdev(child[c], search))
1006e6ca193dSGeorge Wilson 				return (B_TRUE);
1007e6ca193dSGeorge Wilson 	}
1008e6ca193dSGeorge Wilson 
1009e6ca193dSGeorge Wilson 	return (B_FALSE);
1010e6ca193dSGeorge Wilson }
1011e6ca193dSGeorge Wilson 
1012e6ca193dSGeorge Wilson static int
1013e6ca193dSGeorge Wilson find_spare(zpool_handle_t *zhp, void *data)
1014e6ca193dSGeorge Wilson {
1015e6ca193dSGeorge Wilson 	spare_cbdata_t *cbp = data;
1016e6ca193dSGeorge Wilson 	nvlist_t *config, *nvroot;
1017e6ca193dSGeorge Wilson 
1018e6ca193dSGeorge Wilson 	config = zpool_get_config(zhp, NULL);
1019e6ca193dSGeorge Wilson 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1020e6ca193dSGeorge Wilson 	    &nvroot) == 0);
1021e6ca193dSGeorge Wilson 
1022e6ca193dSGeorge Wilson 	if (find_vdev(nvroot, cbp->cb_guid)) {
1023e6ca193dSGeorge Wilson 		cbp->cb_zhp = zhp;
1024e6ca193dSGeorge Wilson 		return (1);
1025e6ca193dSGeorge Wilson 	}
1026e6ca193dSGeorge Wilson 
1027e6ca193dSGeorge Wilson 	zpool_close(zhp);
1028e6ca193dSGeorge Wilson 	return (0);
1029e6ca193dSGeorge Wilson }
1030e6ca193dSGeorge Wilson 
1031e6ca193dSGeorge Wilson /*
1032e6ca193dSGeorge Wilson  * Print out configuration state as requested by status_callback.
1033e6ca193dSGeorge Wilson  */
1034e6ca193dSGeorge Wilson void
1035e6ca193dSGeorge Wilson print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1036e6ca193dSGeorge Wilson     int namewidth, int depth, boolean_t isspare)
1037e6ca193dSGeorge Wilson {
1038e6ca193dSGeorge Wilson 	nvlist_t **child;
1039e6ca193dSGeorge Wilson 	uint_t c, children;
1040e6ca193dSGeorge Wilson 	vdev_stat_t *vs;
1041e6ca193dSGeorge Wilson 	char rbuf[6], wbuf[6], cbuf[6], repaired[7];
1042e6ca193dSGeorge Wilson 	char *vname;
1043e6ca193dSGeorge Wilson 	uint64_t notpresent;
1044e6ca193dSGeorge Wilson 	spare_cbdata_t cb;
1045e6ca193dSGeorge Wilson 	char *state;
1046e6ca193dSGeorge Wilson 
1047e6ca193dSGeorge Wilson 	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
1048e6ca193dSGeorge Wilson 	    (uint64_t **)&vs, &c) == 0);
1049e6ca193dSGeorge Wilson 
1050e6ca193dSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1051e6ca193dSGeorge Wilson 	    &child, &children) != 0)
1052e6ca193dSGeorge Wilson 		children = 0;
1053e6ca193dSGeorge Wilson 
1054e6ca193dSGeorge Wilson 	state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1055e6ca193dSGeorge Wilson 	if (isspare) {
1056e6ca193dSGeorge Wilson 		/*
1057e6ca193dSGeorge Wilson 		 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1058e6ca193dSGeorge Wilson 		 * online drives.
1059e6ca193dSGeorge Wilson 		 */
1060e6ca193dSGeorge Wilson 		if (vs->vs_aux == VDEV_AUX_SPARED)
1061e6ca193dSGeorge Wilson 			state = "INUSE";
1062e6ca193dSGeorge Wilson 		else if (vs->vs_state == VDEV_STATE_HEALTHY)
1063e6ca193dSGeorge Wilson 			state = "AVAIL";
1064e6ca193dSGeorge Wilson 	}
1065e6ca193dSGeorge Wilson 
1066e6ca193dSGeorge Wilson 	(void) printf("\t%*s%-*s  %-8s", depth, "", namewidth - depth,
1067e6ca193dSGeorge Wilson 	    name, state);
1068e6ca193dSGeorge Wilson 
1069e6ca193dSGeorge Wilson 	if (!isspare) {
1070e6ca193dSGeorge Wilson 		zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1071e6ca193dSGeorge Wilson 		zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1072e6ca193dSGeorge Wilson 		zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1073e6ca193dSGeorge Wilson 		(void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1074e6ca193dSGeorge Wilson 	}
1075e6ca193dSGeorge Wilson 
1076e6ca193dSGeorge Wilson 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1077e6ca193dSGeorge Wilson 	    &notpresent) == 0) {
1078e6ca193dSGeorge Wilson 		char *path;
1079e6ca193dSGeorge Wilson 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1080e6ca193dSGeorge Wilson 		(void) printf("  was %s", path);
1081e6ca193dSGeorge Wilson 	} else if (vs->vs_aux != 0) {
1082e6ca193dSGeorge Wilson 		(void) printf("  ");
1083e6ca193dSGeorge Wilson 
1084e6ca193dSGeorge Wilson 		switch (vs->vs_aux) {
1085e6ca193dSGeorge Wilson 		case VDEV_AUX_OPEN_FAILED:
1086e6ca193dSGeorge Wilson 			(void) printf(gettext("cannot open"));
1087e6ca193dSGeorge Wilson 			break;
1088e6ca193dSGeorge Wilson 
1089e6ca193dSGeorge Wilson 		case VDEV_AUX_BAD_GUID_SUM:
1090e6ca193dSGeorge Wilson 			(void) printf(gettext("missing device"));
1091e6ca193dSGeorge Wilson 			break;
1092e6ca193dSGeorge Wilson 
1093e6ca193dSGeorge Wilson 		case VDEV_AUX_NO_REPLICAS:
1094e6ca193dSGeorge Wilson 			(void) printf(gettext("insufficient replicas"));
1095e6ca193dSGeorge Wilson 			break;
1096e6ca193dSGeorge Wilson 
1097e6ca193dSGeorge Wilson 		case VDEV_AUX_VERSION_NEWER:
1098e6ca193dSGeorge Wilson 			(void) printf(gettext("newer version"));
1099e6ca193dSGeorge Wilson 			break;
1100e6ca193dSGeorge Wilson 
1101e6ca193dSGeorge Wilson 		case VDEV_AUX_SPARED:
1102e6ca193dSGeorge Wilson 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1103e6ca193dSGeorge Wilson 			    &cb.cb_guid) == 0);
1104e6ca193dSGeorge Wilson 			if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1105e6ca193dSGeorge Wilson 				if (strcmp(zpool_get_name(cb.cb_zhp),
1106e6ca193dSGeorge Wilson 				    zpool_get_name(zhp)) == 0)
1107e6ca193dSGeorge Wilson 					(void) printf(gettext("currently in "
1108e6ca193dSGeorge Wilson 					    "use"));
1109e6ca193dSGeorge Wilson 				else
1110e6ca193dSGeorge Wilson 					(void) printf(gettext("in use by "
1111e6ca193dSGeorge Wilson 					    "pool '%s'"),
1112e6ca193dSGeorge Wilson 					    zpool_get_name(cb.cb_zhp));
1113e6ca193dSGeorge Wilson 				zpool_close(cb.cb_zhp);
1114e6ca193dSGeorge Wilson 			} else {
1115e6ca193dSGeorge Wilson 				(void) printf(gettext("currently in use"));
1116e6ca193dSGeorge Wilson 			}
1117e6ca193dSGeorge Wilson 			break;
1118e6ca193dSGeorge Wilson 
1119e6ca193dSGeorge Wilson 		case VDEV_AUX_ERR_EXCEEDED:
1120e6ca193dSGeorge Wilson 			(void) printf(gettext("too many errors"));
1121e6ca193dSGeorge Wilson 			break;
1122e6ca193dSGeorge Wilson 
1123e6ca193dSGeorge Wilson 		case VDEV_AUX_IO_FAILURE:
1124e6ca193dSGeorge Wilson 			(void) printf(gettext("experienced I/O failures"));
1125e6ca193dSGeorge Wilson 			break;
1126e6ca193dSGeorge Wilson 
1127e6ca193dSGeorge Wilson 		case VDEV_AUX_BAD_LOG:
1128e6ca193dSGeorge Wilson 			(void) printf(gettext("bad intent log"));
1129e6ca193dSGeorge Wilson 			break;
1130e6ca193dSGeorge Wilson 
1131069f55e2SEric Schrock 		case VDEV_AUX_EXTERNAL:
1132069f55e2SEric Schrock 			(void) printf(gettext("external device fault"));
1133069f55e2SEric Schrock 			break;
1134069f55e2SEric Schrock 
1135e6ca193dSGeorge Wilson 		default:
1136e6ca193dSGeorge Wilson 			(void) printf(gettext("corrupted data"));
1137e6ca193dSGeorge Wilson 			break;
1138e6ca193dSGeorge Wilson 		}
1139e6ca193dSGeorge Wilson 	} else if (vs->vs_scrub_repaired != 0 && children == 0) {
1140e6ca193dSGeorge Wilson 		/*
1141e6ca193dSGeorge Wilson 		 * Report bytes resilvered/repaired on leaf devices.
1142e6ca193dSGeorge Wilson 		 */
1143e6ca193dSGeorge Wilson 		zfs_nicenum(vs->vs_scrub_repaired, repaired, sizeof (repaired));
1144e6ca193dSGeorge Wilson 		(void) printf(gettext("  %s %s"), repaired,
1145e6ca193dSGeorge Wilson 		    (vs->vs_scrub_type == POOL_SCRUB_RESILVER) ?
1146e6ca193dSGeorge Wilson 		    "resilvered" : "repaired");
1147e6ca193dSGeorge Wilson 	}
1148e6ca193dSGeorge Wilson 
1149e6ca193dSGeorge Wilson 	(void) printf("\n");
1150e6ca193dSGeorge Wilson 
1151e6ca193dSGeorge Wilson 	for (c = 0; c < children; c++) {
115288ecc943SGeorge Wilson 		uint64_t islog = B_FALSE, ishole = B_FALSE;
1153e6ca193dSGeorge Wilson 
115488ecc943SGeorge Wilson 		/* Don't print logs or holes here */
1155e6ca193dSGeorge Wilson 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
115688ecc943SGeorge Wilson 		    &islog);
115788ecc943SGeorge Wilson 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
115888ecc943SGeorge Wilson 		    &ishole);
115988ecc943SGeorge Wilson 		if (islog || ishole)
1160e6ca193dSGeorge Wilson 			continue;
116188ecc943SGeorge Wilson 		vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1162e6ca193dSGeorge Wilson 		print_status_config(zhp, vname, child[c],
1163e6ca193dSGeorge Wilson 		    namewidth, depth + 2, isspare);
1164e6ca193dSGeorge Wilson 		free(vname);
1165e6ca193dSGeorge Wilson 	}
1166e6ca193dSGeorge Wilson }
1167e6ca193dSGeorge Wilson 
1168fa9e4066Sahrens 
1169fa9e4066Sahrens /*
1170fa9e4066Sahrens  * Print the configuration of an exported pool.  Iterate over all vdevs in the
1171fa9e4066Sahrens  * pool, printing out the name and status for each one.
1172fa9e4066Sahrens  */
1173fa9e4066Sahrens void
1174e6ca193dSGeorge Wilson print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1175fa9e4066Sahrens {
1176fa9e4066Sahrens 	nvlist_t **child;
1177fa9e4066Sahrens 	uint_t c, children;
1178fa9e4066Sahrens 	vdev_stat_t *vs;
1179afefbcddSeschrock 	char *type, *vname;
1180fa9e4066Sahrens 
1181fa9e4066Sahrens 	verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
118288ecc943SGeorge Wilson 	if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
118388ecc943SGeorge Wilson 	    strcmp(type, VDEV_TYPE_HOLE) == 0)
1184fa9e4066Sahrens 		return;
1185fa9e4066Sahrens 
1186fa9e4066Sahrens 	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
1187fa9e4066Sahrens 	    (uint64_t **)&vs, &c) == 0);
1188fa9e4066Sahrens 
1189fa9e4066Sahrens 	(void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1190990b4856Slling 	(void) printf("  %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1191fa9e4066Sahrens 
1192fa9e4066Sahrens 	if (vs->vs_aux != 0) {
11933d7072f8Seschrock 		(void) printf("  ");
1194fa9e4066Sahrens 
1195fa9e4066Sahrens 		switch (vs->vs_aux) {
1196fa9e4066Sahrens 		case VDEV_AUX_OPEN_FAILED:
1197fa9e4066Sahrens 			(void) printf(gettext("cannot open"));
1198fa9e4066Sahrens 			break;
1199fa9e4066Sahrens 
1200fa9e4066Sahrens 		case VDEV_AUX_BAD_GUID_SUM:
1201fa9e4066Sahrens 			(void) printf(gettext("missing device"));
1202fa9e4066Sahrens 			break;
1203fa9e4066Sahrens 
1204fa9e4066Sahrens 		case VDEV_AUX_NO_REPLICAS:
1205fa9e4066Sahrens 			(void) printf(gettext("insufficient replicas"));
1206fa9e4066Sahrens 			break;
1207fa9e4066Sahrens 
1208eaca9bbdSeschrock 		case VDEV_AUX_VERSION_NEWER:
1209eaca9bbdSeschrock 			(void) printf(gettext("newer version"));
1210eaca9bbdSeschrock 			break;
1211eaca9bbdSeschrock 
12123d7072f8Seschrock 		case VDEV_AUX_ERR_EXCEEDED:
12133d7072f8Seschrock 			(void) printf(gettext("too many errors"));
12143d7072f8Seschrock 			break;
12153d7072f8Seschrock 
1216fa9e4066Sahrens 		default:
1217fa9e4066Sahrens 			(void) printf(gettext("corrupted data"));
1218fa9e4066Sahrens 			break;
1219fa9e4066Sahrens 		}
1220fa9e4066Sahrens 	}
1221fa9e4066Sahrens 	(void) printf("\n");
1222fa9e4066Sahrens 
1223fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1224fa9e4066Sahrens 	    &child, &children) != 0)
1225fa9e4066Sahrens 		return;
1226fa9e4066Sahrens 
1227afefbcddSeschrock 	for (c = 0; c < children; c++) {
12288654d025Sperrin 		uint64_t is_log = B_FALSE;
12298654d025Sperrin 
12308654d025Sperrin 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
12318654d025Sperrin 		    &is_log);
1232e6ca193dSGeorge Wilson 		if (is_log)
12338654d025Sperrin 			continue;
12348654d025Sperrin 
123588ecc943SGeorge Wilson 		vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1236e6ca193dSGeorge Wilson 		print_import_config(vname, child[c], namewidth, depth + 2);
1237afefbcddSeschrock 		free(vname);
1238afefbcddSeschrock 	}
123999653d4eSeschrock 
1240fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1241fa94a07fSbrendan 	    &child, &children) == 0) {
1242fa94a07fSbrendan 		(void) printf(gettext("\tcache\n"));
1243fa94a07fSbrendan 		for (c = 0; c < children; c++) {
124488ecc943SGeorge Wilson 			vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1245fa94a07fSbrendan 			(void) printf("\t  %s\n", vname);
1246fa94a07fSbrendan 			free(vname);
1247fa94a07fSbrendan 		}
1248fa94a07fSbrendan 	}
124999653d4eSeschrock 
1250fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1251fa94a07fSbrendan 	    &child, &children) == 0) {
1252fa94a07fSbrendan 		(void) printf(gettext("\tspares\n"));
1253fa94a07fSbrendan 		for (c = 0; c < children; c++) {
125488ecc943SGeorge Wilson 			vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1255fa94a07fSbrendan 			(void) printf("\t  %s\n", vname);
1256fa94a07fSbrendan 			free(vname);
1257fa94a07fSbrendan 		}
125899653d4eSeschrock 	}
1259fa9e4066Sahrens }
1260fa9e4066Sahrens 
1261e6ca193dSGeorge Wilson /*
1262e6ca193dSGeorge Wilson  * Print log vdevs.
1263e6ca193dSGeorge Wilson  * Logs are recorded as top level vdevs in the main pool child array
1264e6ca193dSGeorge Wilson  * but with "is_log" set to 1. We use either print_status_config() or
1265e6ca193dSGeorge Wilson  * print_import_config() to print the top level logs then any log
1266e6ca193dSGeorge Wilson  * children (eg mirrored slogs) are printed recursively - which
1267e6ca193dSGeorge Wilson  * works because only the top level vdev is marked "is_log"
1268e6ca193dSGeorge Wilson  */
1269e6ca193dSGeorge Wilson static void
1270e6ca193dSGeorge Wilson print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1271e6ca193dSGeorge Wilson {
1272e6ca193dSGeorge Wilson 	uint_t c, children;
1273e6ca193dSGeorge Wilson 	nvlist_t **child;
1274e6ca193dSGeorge Wilson 
1275e6ca193dSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1276e6ca193dSGeorge Wilson 	    &children) != 0)
1277e6ca193dSGeorge Wilson 		return;
1278e6ca193dSGeorge Wilson 
1279e6ca193dSGeorge Wilson 	(void) printf(gettext("\tlogs\n"));
1280e6ca193dSGeorge Wilson 
1281e6ca193dSGeorge Wilson 	for (c = 0; c < children; c++) {
1282e6ca193dSGeorge Wilson 		uint64_t is_log = B_FALSE;
1283e6ca193dSGeorge Wilson 		char *name;
1284e6ca193dSGeorge Wilson 
1285e6ca193dSGeorge Wilson 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1286e6ca193dSGeorge Wilson 		    &is_log);
1287e6ca193dSGeorge Wilson 		if (!is_log)
1288e6ca193dSGeorge Wilson 			continue;
128988ecc943SGeorge Wilson 		name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1290e6ca193dSGeorge Wilson 		if (verbose)
1291e6ca193dSGeorge Wilson 			print_status_config(zhp, name, child[c], namewidth,
1292e6ca193dSGeorge Wilson 			    2, B_FALSE);
1293e6ca193dSGeorge Wilson 		else
1294e6ca193dSGeorge Wilson 			print_import_config(name, child[c], namewidth, 2);
1295e6ca193dSGeorge Wilson 		free(name);
1296e6ca193dSGeorge Wilson 	}
1297e6ca193dSGeorge Wilson }
1298*468c413aSTim Haley 
1299fa9e4066Sahrens /*
1300fa9e4066Sahrens  * Display the status for the given pool.
1301fa9e4066Sahrens  */
1302fa9e4066Sahrens static void
1303fa9e4066Sahrens show_import(nvlist_t *config)
1304fa9e4066Sahrens {
1305fa9e4066Sahrens 	uint64_t pool_state;
1306fa9e4066Sahrens 	vdev_stat_t *vs;
1307fa9e4066Sahrens 	char *name;
1308fa9e4066Sahrens 	uint64_t guid;
1309fa9e4066Sahrens 	char *msgid;
1310fa9e4066Sahrens 	nvlist_t *nvroot;
1311fa9e4066Sahrens 	int reason;
131246657f8dSmmusante 	const char *health;
1313fa9e4066Sahrens 	uint_t vsc;
1314fa9e4066Sahrens 	int namewidth;
1315fa9e4066Sahrens 
1316fa9e4066Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1317fa9e4066Sahrens 	    &name) == 0);
1318fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1319fa9e4066Sahrens 	    &guid) == 0);
1320fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1321fa9e4066Sahrens 	    &pool_state) == 0);
1322fa9e4066Sahrens 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1323fa9e4066Sahrens 	    &nvroot) == 0);
1324fa9e4066Sahrens 
1325fa9e4066Sahrens 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
1326fa9e4066Sahrens 	    (uint64_t **)&vs, &vsc) == 0);
1327990b4856Slling 	health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1328fa9e4066Sahrens 
1329fa9e4066Sahrens 	reason = zpool_import_status(config, &msgid);
1330fa9e4066Sahrens 
133146657f8dSmmusante 	(void) printf(gettext("  pool: %s\n"), name);
133246657f8dSmmusante 	(void) printf(gettext("    id: %llu\n"), (u_longlong_t)guid);
133346657f8dSmmusante 	(void) printf(gettext(" state: %s"), health);
13344c58d714Sdarrenm 	if (pool_state == POOL_STATE_DESTROYED)
1335b1b8ab34Slling 		(void) printf(gettext(" (DESTROYED)"));
13364c58d714Sdarrenm 	(void) printf("\n");
1337fa9e4066Sahrens 
1338fa9e4066Sahrens 	switch (reason) {
1339fa9e4066Sahrens 	case ZPOOL_STATUS_MISSING_DEV_R:
1340fa9e4066Sahrens 	case ZPOOL_STATUS_MISSING_DEV_NR:
1341fa9e4066Sahrens 	case ZPOOL_STATUS_BAD_GUID_SUM:
1342fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices are missing "
1343fa9e4066Sahrens 		    "from the system.\n"));
1344fa9e4066Sahrens 		break;
1345fa9e4066Sahrens 
1346fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_LABEL_R:
1347fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1348fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices contains "
1349fa9e4066Sahrens 		    "corrupted data.\n"));
1350fa9e4066Sahrens 		break;
1351fa9e4066Sahrens 
1352fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_DATA:
1353fa9e4066Sahrens 		(void) printf(gettext("status: The pool data is corrupted.\n"));
1354fa9e4066Sahrens 		break;
1355fa9e4066Sahrens 
1356441d80aaSlling 	case ZPOOL_STATUS_OFFLINE_DEV:
1357441d80aaSlling 		(void) printf(gettext("status: One or more devices "
1358441d80aaSlling 		    "are offlined.\n"));
1359441d80aaSlling 		break;
1360441d80aaSlling 
1361ea8dc4b6Seschrock 	case ZPOOL_STATUS_CORRUPT_POOL:
1362ea8dc4b6Seschrock 		(void) printf(gettext("status: The pool metadata is "
1363ea8dc4b6Seschrock 		    "corrupted.\n"));
1364ea8dc4b6Seschrock 		break;
1365ea8dc4b6Seschrock 
1366eaca9bbdSeschrock 	case ZPOOL_STATUS_VERSION_OLDER:
1367eaca9bbdSeschrock 		(void) printf(gettext("status: The pool is formatted using an "
1368eaca9bbdSeschrock 		    "older on-disk version.\n"));
1369eaca9bbdSeschrock 		break;
1370eaca9bbdSeschrock 
1371eaca9bbdSeschrock 	case ZPOOL_STATUS_VERSION_NEWER:
1372eaca9bbdSeschrock 		(void) printf(gettext("status: The pool is formatted using an "
1373eaca9bbdSeschrock 		    "incompatible version.\n"));
1374eaca9bbdSeschrock 		break;
1375b87f3af3Sperrin 
137695173954Sek 	case ZPOOL_STATUS_HOSTID_MISMATCH:
137795173954Sek 		(void) printf(gettext("status: The pool was last accessed by "
137895173954Sek 		    "another system.\n"));
137995173954Sek 		break;
1380b87f3af3Sperrin 
13813d7072f8Seschrock 	case ZPOOL_STATUS_FAULTED_DEV_R:
13823d7072f8Seschrock 	case ZPOOL_STATUS_FAULTED_DEV_NR:
13833d7072f8Seschrock 		(void) printf(gettext("status: One or more devices are "
13843d7072f8Seschrock 		    "faulted.\n"));
13853d7072f8Seschrock 		break;
13863d7072f8Seschrock 
1387b87f3af3Sperrin 	case ZPOOL_STATUS_BAD_LOG:
1388b87f3af3Sperrin 		(void) printf(gettext("status: An intent log record cannot be "
1389b87f3af3Sperrin 		    "read.\n"));
1390b87f3af3Sperrin 		break;
1391b87f3af3Sperrin 
1392fa9e4066Sahrens 	default:
1393fa9e4066Sahrens 		/*
1394fa9e4066Sahrens 		 * No other status can be seen when importing pools.
1395fa9e4066Sahrens 		 */
1396fa9e4066Sahrens 		assert(reason == ZPOOL_STATUS_OK);
1397fa9e4066Sahrens 	}
1398fa9e4066Sahrens 
1399fa9e4066Sahrens 	/*
1400fa9e4066Sahrens 	 * Print out an action according to the overall state of the pool.
1401fa9e4066Sahrens 	 */
140246657f8dSmmusante 	if (vs->vs_state == VDEV_STATE_HEALTHY) {
1403eaca9bbdSeschrock 		if (reason == ZPOOL_STATUS_VERSION_OLDER)
1404eaca9bbdSeschrock 			(void) printf(gettext("action: The pool can be "
1405eaca9bbdSeschrock 			    "imported using its name or numeric identifier, "
1406eaca9bbdSeschrock 			    "though\n\tsome features will not be available "
1407eaca9bbdSeschrock 			    "without an explicit 'zpool upgrade'.\n"));
140895173954Sek 		else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH)
140995173954Sek 			(void) printf(gettext("action: The pool can be "
141095173954Sek 			    "imported using its name or numeric "
141195173954Sek 			    "identifier and\n\tthe '-f' flag.\n"));
1412fa9e4066Sahrens 		else
1413eaca9bbdSeschrock 			(void) printf(gettext("action: The pool can be "
1414eaca9bbdSeschrock 			    "imported using its name or numeric "
1415eaca9bbdSeschrock 			    "identifier.\n"));
141646657f8dSmmusante 	} else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1417fa9e4066Sahrens 		(void) printf(gettext("action: The pool can be imported "
1418fa9e4066Sahrens 		    "despite missing or damaged devices.  The\n\tfault "
1419eaca9bbdSeschrock 		    "tolerance of the pool may be compromised if imported.\n"));
1420fa9e4066Sahrens 	} else {
1421eaca9bbdSeschrock 		switch (reason) {
1422eaca9bbdSeschrock 		case ZPOOL_STATUS_VERSION_NEWER:
1423eaca9bbdSeschrock 			(void) printf(gettext("action: The pool cannot be "
1424eaca9bbdSeschrock 			    "imported.  Access the pool on a system running "
1425eaca9bbdSeschrock 			    "newer\n\tsoftware, or recreate the pool from "
1426eaca9bbdSeschrock 			    "backup.\n"));
1427eaca9bbdSeschrock 			break;
1428eaca9bbdSeschrock 		case ZPOOL_STATUS_MISSING_DEV_R:
1429eaca9bbdSeschrock 		case ZPOOL_STATUS_MISSING_DEV_NR:
1430eaca9bbdSeschrock 		case ZPOOL_STATUS_BAD_GUID_SUM:
1431fa9e4066Sahrens 			(void) printf(gettext("action: The pool cannot be "
1432fa9e4066Sahrens 			    "imported. Attach the missing\n\tdevices and try "
1433fa9e4066Sahrens 			    "again.\n"));
1434eaca9bbdSeschrock 			break;
1435eaca9bbdSeschrock 		default:
1436fa9e4066Sahrens 			(void) printf(gettext("action: The pool cannot be "
1437fa9e4066Sahrens 			    "imported due to damaged devices or data.\n"));
1438eaca9bbdSeschrock 		}
1439eaca9bbdSeschrock 	}
1440eaca9bbdSeschrock 
144146657f8dSmmusante 	/*
144246657f8dSmmusante 	 * If the state is "closed" or "can't open", and the aux state
144346657f8dSmmusante 	 * is "corrupt data":
144446657f8dSmmusante 	 */
144546657f8dSmmusante 	if (((vs->vs_state == VDEV_STATE_CLOSED) ||
144646657f8dSmmusante 	    (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
144746657f8dSmmusante 	    (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1448eaca9bbdSeschrock 		if (pool_state == POOL_STATE_DESTROYED)
1449eaca9bbdSeschrock 			(void) printf(gettext("\tThe pool was destroyed, "
1450eaca9bbdSeschrock 			    "but can be imported using the '-Df' flags.\n"));
1451eaca9bbdSeschrock 		else if (pool_state != POOL_STATE_EXPORTED)
1452eaca9bbdSeschrock 			(void) printf(gettext("\tThe pool may be active on "
145318ce54dfSek 			    "another system, but can be imported using\n\t"
1454eaca9bbdSeschrock 			    "the '-f' flag.\n"));
1455fa9e4066Sahrens 	}
1456fa9e4066Sahrens 
1457fa9e4066Sahrens 	if (msgid != NULL)
1458fa9e4066Sahrens 		(void) printf(gettext("   see: http://www.sun.com/msg/%s\n"),
1459fa9e4066Sahrens 		    msgid);
1460fa9e4066Sahrens 
1461fa9e4066Sahrens 	(void) printf(gettext("config:\n\n"));
1462fa9e4066Sahrens 
1463c67d9675Seschrock 	namewidth = max_width(NULL, nvroot, 0, 0);
1464fa9e4066Sahrens 	if (namewidth < 10)
1465fa9e4066Sahrens 		namewidth = 10;
14668654d025Sperrin 
1467e6ca193dSGeorge Wilson 	print_import_config(name, nvroot, namewidth, 0);
1468e6ca193dSGeorge Wilson 	if (num_logs(nvroot) > 0)
1469e6ca193dSGeorge Wilson 		print_logs(NULL, nvroot, namewidth, B_FALSE);
1470fa9e4066Sahrens 
1471fa9e4066Sahrens 	if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
147246657f8dSmmusante 		(void) printf(gettext("\n\tAdditional devices are known to "
1473fa9e4066Sahrens 		    "be part of this pool, though their\n\texact "
147446657f8dSmmusante 		    "configuration cannot be determined.\n"));
1475fa9e4066Sahrens 	}
1476fa9e4066Sahrens }
1477fa9e4066Sahrens 
1478fa9e4066Sahrens /*
1479fa9e4066Sahrens  * Perform the import for the given configuration.  This passes the heavy
1480990b4856Slling  * lifting off to zpool_import_props(), and then mounts the datasets contained
1481990b4856Slling  * within the pool.
1482fa9e4066Sahrens  */
1483fa9e4066Sahrens static int
1484fa9e4066Sahrens do_import(nvlist_t *config, const char *newname, const char *mntopts,
14854f0f5e5bSVictor Latushkin     int force, nvlist_t *props, boolean_t do_verbatim)
1486fa9e4066Sahrens {
1487fa9e4066Sahrens 	zpool_handle_t *zhp;
1488fa9e4066Sahrens 	char *name;
1489fa9e4066Sahrens 	uint64_t state;
1490eaca9bbdSeschrock 	uint64_t version;
1491fa9e4066Sahrens 
1492fa9e4066Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1493fa9e4066Sahrens 	    &name) == 0);
1494fa9e4066Sahrens 
1495fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config,
1496fa9e4066Sahrens 	    ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1497eaca9bbdSeschrock 	verify(nvlist_lookup_uint64(config,
1498eaca9bbdSeschrock 	    ZPOOL_CONFIG_VERSION, &version) == 0);
1499e7437265Sahrens 	if (version > SPA_VERSION) {
1500eaca9bbdSeschrock 		(void) fprintf(stderr, gettext("cannot import '%s': pool "
1501eaca9bbdSeschrock 		    "is formatted using a newer ZFS version\n"), name);
1502eaca9bbdSeschrock 		return (1);
1503eaca9bbdSeschrock 	} else if (state != POOL_STATE_EXPORTED && !force) {
150495173954Sek 		uint64_t hostid;
150595173954Sek 
150695173954Sek 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
150795173954Sek 		    &hostid) == 0) {
150895173954Sek 			if ((unsigned long)hostid != gethostid()) {
150995173954Sek 				char *hostname;
151095173954Sek 				uint64_t timestamp;
151195173954Sek 				time_t t;
151295173954Sek 
151395173954Sek 				verify(nvlist_lookup_string(config,
151495173954Sek 				    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
151595173954Sek 				verify(nvlist_lookup_uint64(config,
151695173954Sek 				    ZPOOL_CONFIG_TIMESTAMP, &timestamp) == 0);
151795173954Sek 				t = timestamp;
151895173954Sek 				(void) fprintf(stderr, gettext("cannot import "
151995173954Sek 				    "'%s': pool may be in use from other "
152095173954Sek 				    "system, it was last accessed by %s "
152195173954Sek 				    "(hostid: 0x%lx) on %s"), name, hostname,
152295173954Sek 				    (unsigned long)hostid,
152395173954Sek 				    asctime(localtime(&t)));
152495173954Sek 				(void) fprintf(stderr, gettext("use '-f' to "
152595173954Sek 				    "import anyway\n"));
152695173954Sek 				return (1);
152795173954Sek 			}
152895173954Sek 		} else {
152995173954Sek 			(void) fprintf(stderr, gettext("cannot import '%s': "
153095173954Sek 			    "pool may be in use from other system\n"), name);
153195173954Sek 			(void) fprintf(stderr, gettext("use '-f' to import "
153295173954Sek 			    "anyway\n"));
153395173954Sek 			return (1);
153495173954Sek 		}
1535fa9e4066Sahrens 	}
1536fa9e4066Sahrens 
15374f0f5e5bSVictor Latushkin 	if (zpool_import_props(g_zfs, config, newname, props, do_verbatim) != 0)
1538fa9e4066Sahrens 		return (1);
1539fa9e4066Sahrens 
1540fa9e4066Sahrens 	if (newname != NULL)
1541fa9e4066Sahrens 		name = (char *)newname;
1542fa9e4066Sahrens 
15434f0f5e5bSVictor Latushkin 	if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
15444f0f5e5bSVictor Latushkin 		return (1);
1545fa9e4066Sahrens 
1546379c004dSEric Schrock 	if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1547379c004dSEric Schrock 	    zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1548fa9e4066Sahrens 		zpool_close(zhp);
1549fa9e4066Sahrens 		return (1);
1550fa9e4066Sahrens 	}
1551fa9e4066Sahrens 
1552fa9e4066Sahrens 	zpool_close(zhp);
1553*468c413aSTim Haley 	return (0);
1554fa9e4066Sahrens }
1555fa9e4066Sahrens 
1556fa9e4066Sahrens /*
15574c58d714Sdarrenm  * zpool import [-d dir] [-D]
15582f8aaab3Seschrock  *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
15592f8aaab3Seschrock  *              [-d dir | -c cachefile] [-f] -a
15602f8aaab3Seschrock  *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1561*468c413aSTim Haley  *              [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
15622f8aaab3Seschrock  *
15632f8aaab3Seschrock  *	 -c	Read pool information from a cachefile instead of searching
15642f8aaab3Seschrock  *		devices.
1565fa9e4066Sahrens  *
1566fa9e4066Sahrens  *       -d	Scan in a specific directory, other than /dev/dsk.  More than
1567fa9e4066Sahrens  *		one directory can be specified using multiple '-d' options.
1568fa9e4066Sahrens  *
15694c58d714Sdarrenm  *       -D     Scan for previously destroyed pools or import all or only
15704c58d714Sdarrenm  *              specified destroyed pools.
15714c58d714Sdarrenm  *
1572fa9e4066Sahrens  *       -R	Temporarily import the pool, with all mountpoints relative to
1573fa9e4066Sahrens  *		the given root.  The pool will remain exported when the machine
1574fa9e4066Sahrens  *		is rebooted.
1575fa9e4066Sahrens  *
1576*468c413aSTim Haley  *       -V	Import even in the presence of faulted vdevs.  This is an
1577c5904d13Seschrock  *       	intentionally undocumented option for testing purposes, and
1578c5904d13Seschrock  *       	treats the pool configuration as complete, leaving any bad
15794f0f5e5bSVictor Latushkin  *		vdevs in the FAULTED state. In other words, it does verbatim
15804f0f5e5bSVictor Latushkin  *		import.
1581c5904d13Seschrock  *
1582*468c413aSTim Haley  *       -f	Force import, even if it appears that the pool is active.
1583*468c413aSTim Haley  *
1584*468c413aSTim Haley  *       -F     Attempt rewind if necessary.
1585*468c413aSTim Haley  *
1586*468c413aSTim Haley  *       -n     See if rewind would work, but don't actually rewind.
1587*468c413aSTim Haley  *
1588fa9e4066Sahrens  *       -a	Import all pools found.
1589fa9e4066Sahrens  *
1590990b4856Slling  *       -o	Set property=value and/or temporary mount options (without '=').
1591ecd6cf80Smarks  *
1592fa9e4066Sahrens  * The import command scans for pools to import, and import pools based on pool
1593fa9e4066Sahrens  * name and GUID.  The pool can also be renamed as part of the import process.
1594fa9e4066Sahrens  */
1595fa9e4066Sahrens int
1596fa9e4066Sahrens zpool_do_import(int argc, char **argv)
1597fa9e4066Sahrens {
1598fa9e4066Sahrens 	char **searchdirs = NULL;
1599fa9e4066Sahrens 	int nsearch = 0;
1600fa9e4066Sahrens 	int c;
1601fa9e4066Sahrens 	int err;
16022f8aaab3Seschrock 	nvlist_t *pools = NULL;
160399653d4eSeschrock 	boolean_t do_all = B_FALSE;
160499653d4eSeschrock 	boolean_t do_destroyed = B_FALSE;
1605fa9e4066Sahrens 	char *mntopts = NULL;
160699653d4eSeschrock 	boolean_t do_force = B_FALSE;
1607fa9e4066Sahrens 	nvpair_t *elem;
1608fa9e4066Sahrens 	nvlist_t *config;
160924e697d4Sck 	uint64_t searchguid = 0;
161024e697d4Sck 	char *searchname = NULL;
1611990b4856Slling 	char *propval;
1612fa9e4066Sahrens 	nvlist_t *found_config;
1613*468c413aSTim Haley 	nvlist_t *policy = NULL;
1614ecd6cf80Smarks 	nvlist_t *props = NULL;
161599653d4eSeschrock 	boolean_t first;
16164f0f5e5bSVictor Latushkin 	boolean_t do_verbatim = B_FALSE;
1617*468c413aSTim Haley 	uint32_t rewind_policy = ZPOOL_NO_REWIND;
1618*468c413aSTim Haley 	boolean_t dryrun = B_FALSE;
1619*468c413aSTim Haley 	boolean_t do_rewind = B_FALSE;
1620*468c413aSTim Haley 	boolean_t xtreme_rewind = B_FALSE;
16214c58d714Sdarrenm 	uint64_t pool_state;
16222f8aaab3Seschrock 	char *cachefile = NULL;
1623fa9e4066Sahrens 
1624fa9e4066Sahrens 	/* check options */
1625*468c413aSTim Haley 	while ((c = getopt(argc, argv, ":aCc:d:DEfFno:p:rR:VX")) != -1) {
1626fa9e4066Sahrens 		switch (c) {
1627fa9e4066Sahrens 		case 'a':
162899653d4eSeschrock 			do_all = B_TRUE;
1629fa9e4066Sahrens 			break;
16302f8aaab3Seschrock 		case 'c':
16312f8aaab3Seschrock 			cachefile = optarg;
16322f8aaab3Seschrock 			break;
1633fa9e4066Sahrens 		case 'd':
1634fa9e4066Sahrens 			if (searchdirs == NULL) {
1635fa9e4066Sahrens 				searchdirs = safe_malloc(sizeof (char *));
1636fa9e4066Sahrens 			} else {
1637fa9e4066Sahrens 				char **tmp = safe_malloc((nsearch + 1) *
1638fa9e4066Sahrens 				    sizeof (char *));
1639fa9e4066Sahrens 				bcopy(searchdirs, tmp, nsearch *
1640fa9e4066Sahrens 				    sizeof (char *));
1641fa9e4066Sahrens 				free(searchdirs);
1642fa9e4066Sahrens 				searchdirs = tmp;
1643fa9e4066Sahrens 			}
1644fa9e4066Sahrens 			searchdirs[nsearch++] = optarg;
1645fa9e4066Sahrens 			break;
16464c58d714Sdarrenm 		case 'D':
164799653d4eSeschrock 			do_destroyed = B_TRUE;
16484c58d714Sdarrenm 			break;
1649fa9e4066Sahrens 		case 'f':
165099653d4eSeschrock 			do_force = B_TRUE;
1651fa9e4066Sahrens 			break;
1652c5904d13Seschrock 		case 'F':
1653*468c413aSTim Haley 			do_rewind = B_TRUE;
1654*468c413aSTim Haley 			break;
1655*468c413aSTim Haley 		case 'n':
1656*468c413aSTim Haley 			dryrun = B_TRUE;
1657c5904d13Seschrock 			break;
1658fa9e4066Sahrens 		case 'o':
1659990b4856Slling 			if ((propval = strchr(optarg, '=')) != NULL) {
1660990b4856Slling 				*propval = '\0';
1661990b4856Slling 				propval++;
16620a48a24eStimh 				if (add_prop_list(optarg, propval,
16630a48a24eStimh 				    &props, B_TRUE))
1664990b4856Slling 					goto error;
1665990b4856Slling 			} else {
1666990b4856Slling 				mntopts = optarg;
1667990b4856Slling 			}
1668fa9e4066Sahrens 			break;
1669fa9e4066Sahrens 		case 'R':
1670990b4856Slling 			if (add_prop_list(zpool_prop_to_name(
16710a48a24eStimh 			    ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1672990b4856Slling 				goto error;
16732f8aaab3Seschrock 			if (nvlist_lookup_string(props,
16742f8aaab3Seschrock 			    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
16752f8aaab3Seschrock 			    &propval) == 0)
16762f8aaab3Seschrock 				break;
1677990b4856Slling 			if (add_prop_list(zpool_prop_to_name(
16780a48a24eStimh 			    ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1679990b4856Slling 				goto error;
1680fa9e4066Sahrens 			break;
1681*468c413aSTim Haley 		case 'V':
1682*468c413aSTim Haley 			do_verbatim = B_TRUE;
1683*468c413aSTim Haley 			break;
1684*468c413aSTim Haley 		case 'X':
1685*468c413aSTim Haley 			xtreme_rewind = B_TRUE;
1686*468c413aSTim Haley 			break;
1687fa9e4066Sahrens 		case ':':
1688fa9e4066Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
1689fa9e4066Sahrens 			    "'%c' option\n"), optopt);
169099653d4eSeschrock 			usage(B_FALSE);
1691fa9e4066Sahrens 			break;
1692fa9e4066Sahrens 		case '?':
1693fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1694fa9e4066Sahrens 			    optopt);
169599653d4eSeschrock 			usage(B_FALSE);
1696fa9e4066Sahrens 		}
1697fa9e4066Sahrens 	}
1698fa9e4066Sahrens 
1699fa9e4066Sahrens 	argc -= optind;
1700fa9e4066Sahrens 	argv += optind;
1701fa9e4066Sahrens 
17022f8aaab3Seschrock 	if (cachefile && nsearch != 0) {
17032f8aaab3Seschrock 		(void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
17042f8aaab3Seschrock 		usage(B_FALSE);
17052f8aaab3Seschrock 	}
17062f8aaab3Seschrock 
1707*468c413aSTim Haley 	if ((dryrun || xtreme_rewind) && !do_rewind) {
1708*468c413aSTim Haley 		(void) fprintf(stderr,
1709*468c413aSTim Haley 		    gettext("-n or -X only meaningful with -F\n"));
1710*468c413aSTim Haley 		usage(B_FALSE);
1711*468c413aSTim Haley 	}
1712*468c413aSTim Haley 	if (dryrun)
1713*468c413aSTim Haley 		rewind_policy = ZPOOL_TRY_REWIND;
1714*468c413aSTim Haley 	else if (do_rewind)
1715*468c413aSTim Haley 		rewind_policy = ZPOOL_DO_REWIND;
1716*468c413aSTim Haley 	if (xtreme_rewind)
1717*468c413aSTim Haley 		rewind_policy |= ZPOOL_EXTREME_REWIND;
1718*468c413aSTim Haley 
1719*468c413aSTim Haley 	/* In the future, we can capture further policy and include it here */
1720*468c413aSTim Haley 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
1721*468c413aSTim Haley 	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
1722*468c413aSTim Haley 		goto error;
1723*468c413aSTim Haley 
1724fa9e4066Sahrens 	if (searchdirs == NULL) {
1725fa9e4066Sahrens 		searchdirs = safe_malloc(sizeof (char *));
1726fa9e4066Sahrens 		searchdirs[0] = "/dev/dsk";
1727fa9e4066Sahrens 		nsearch = 1;
1728fa9e4066Sahrens 	}
1729fa9e4066Sahrens 
1730fa9e4066Sahrens 	/* check argument count */
1731fa9e4066Sahrens 	if (do_all) {
1732fa9e4066Sahrens 		if (argc != 0) {
1733fa9e4066Sahrens 			(void) fprintf(stderr, gettext("too many arguments\n"));
173499653d4eSeschrock 			usage(B_FALSE);
1735fa9e4066Sahrens 		}
1736fa9e4066Sahrens 	} else {
1737fa9e4066Sahrens 		if (argc > 2) {
1738fa9e4066Sahrens 			(void) fprintf(stderr, gettext("too many arguments\n"));
173999653d4eSeschrock 			usage(B_FALSE);
1740fa9e4066Sahrens 		}
1741fa9e4066Sahrens 
1742fa9e4066Sahrens 		/*
1743fa9e4066Sahrens 		 * Check for the SYS_CONFIG privilege.  We do this explicitly
1744fa9e4066Sahrens 		 * here because otherwise any attempt to discover pools will
1745fa9e4066Sahrens 		 * silently fail.
1746fa9e4066Sahrens 		 */
1747fa9e4066Sahrens 		if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
1748fa9e4066Sahrens 			(void) fprintf(stderr, gettext("cannot "
1749fa9e4066Sahrens 			    "discover pools: permission denied\n"));
175099653d4eSeschrock 			free(searchdirs);
1751*468c413aSTim Haley 			nvlist_free(policy);
1752fa9e4066Sahrens 			return (1);
1753fa9e4066Sahrens 		}
1754fa9e4066Sahrens 	}
1755fa9e4066Sahrens 
1756fa9e4066Sahrens 	/*
1757fa9e4066Sahrens 	 * Depending on the arguments given, we do one of the following:
1758fa9e4066Sahrens 	 *
1759fa9e4066Sahrens 	 *	<none>	Iterate through all pools and display information about
1760fa9e4066Sahrens 	 *		each one.
1761fa9e4066Sahrens 	 *
1762fa9e4066Sahrens 	 *	-a	Iterate through all pools and try to import each one.
1763fa9e4066Sahrens 	 *
1764fa9e4066Sahrens 	 *	<id>	Find the pool that corresponds to the given GUID/pool
1765fa9e4066Sahrens 	 *		name and import that one.
17664c58d714Sdarrenm 	 *
17674c58d714Sdarrenm 	 *	-D	Above options applies only to destroyed pools.
1768fa9e4066Sahrens 	 */
1769fa9e4066Sahrens 	if (argc != 0) {
1770fa9e4066Sahrens 		char *endptr;
1771fa9e4066Sahrens 
1772fa9e4066Sahrens 		errno = 0;
1773fa9e4066Sahrens 		searchguid = strtoull(argv[0], &endptr, 10);
1774fa9e4066Sahrens 		if (errno != 0 || *endptr != '\0')
1775fa9e4066Sahrens 			searchname = argv[0];
1776fa9e4066Sahrens 		found_config = NULL;
1777fa9e4066Sahrens 	}
1778fa9e4066Sahrens 
177924e697d4Sck 	if (cachefile) {
1780e829d913Sck 		pools = zpool_find_import_cached(g_zfs, cachefile, searchname,
1781e829d913Sck 		    searchguid);
178224e697d4Sck 	} else if (searchname != NULL) {
178324e697d4Sck 		pools = zpool_find_import_byname(g_zfs, nsearch, searchdirs,
178424e697d4Sck 		    searchname);
178524e697d4Sck 	} else {
178624e697d4Sck 		/*
178724e697d4Sck 		 * It's OK to search by guid even if searchguid is 0.
178824e697d4Sck 		 */
178924e697d4Sck 		pools = zpool_find_import_byguid(g_zfs, nsearch, searchdirs,
179024e697d4Sck 		    searchguid);
179124e697d4Sck 	}
179224e697d4Sck 
179324e697d4Sck 	if (pools == NULL) {
179424e697d4Sck 		if (argc != 0) {
179524e697d4Sck 			(void) fprintf(stderr, gettext("cannot import '%s': "
179624e697d4Sck 			    "no such pool available\n"), argv[0]);
179724e697d4Sck 		}
179824e697d4Sck 		free(searchdirs);
1799*468c413aSTim Haley 		nvlist_free(policy);
180024e697d4Sck 		return (1);
180124e697d4Sck 	}
180224e697d4Sck 
180324e697d4Sck 	/*
180424e697d4Sck 	 * At this point we have a list of import candidate configs. Even if
180524e697d4Sck 	 * we were searching by pool name or guid, we still need to
180624e697d4Sck 	 * post-process the list to deal with pool state and possible
180724e697d4Sck 	 * duplicate names.
180824e697d4Sck 	 */
1809fa9e4066Sahrens 	err = 0;
1810fa9e4066Sahrens 	elem = NULL;
181199653d4eSeschrock 	first = B_TRUE;
1812fa9e4066Sahrens 	while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
1813fa9e4066Sahrens 
1814fa9e4066Sahrens 		verify(nvpair_value_nvlist(elem, &config) == 0);
1815fa9e4066Sahrens 
18164c58d714Sdarrenm 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
18174c58d714Sdarrenm 		    &pool_state) == 0);
18184c58d714Sdarrenm 		if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
18194c58d714Sdarrenm 			continue;
18204c58d714Sdarrenm 		if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
18214c58d714Sdarrenm 			continue;
18224c58d714Sdarrenm 
1823*468c413aSTim Haley 		verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
1824*468c413aSTim Haley 		    policy) == 0);
1825*468c413aSTim Haley 
1826fa9e4066Sahrens 		if (argc == 0) {
1827fa9e4066Sahrens 			if (first)
182899653d4eSeschrock 				first = B_FALSE;
18293bb79becSeschrock 			else if (!do_all)
1830fa9e4066Sahrens 				(void) printf("\n");
1831fa9e4066Sahrens 
1832*468c413aSTim Haley 			if (do_all) {
1833fa9e4066Sahrens 				err |= do_import(config, NULL, mntopts,
18344f0f5e5bSVictor Latushkin 				    do_force, props, do_verbatim);
1835*468c413aSTim Haley 			} else {
1836fa9e4066Sahrens 				show_import(config);
1837*468c413aSTim Haley 			}
1838fa9e4066Sahrens 		} else if (searchname != NULL) {
1839fa9e4066Sahrens 			char *name;
1840fa9e4066Sahrens 
1841fa9e4066Sahrens 			/*
1842fa9e4066Sahrens 			 * We are searching for a pool based on name.
1843fa9e4066Sahrens 			 */
1844fa9e4066Sahrens 			verify(nvlist_lookup_string(config,
1845fa9e4066Sahrens 			    ZPOOL_CONFIG_POOL_NAME, &name) == 0);
1846fa9e4066Sahrens 
1847fa9e4066Sahrens 			if (strcmp(name, searchname) == 0) {
1848fa9e4066Sahrens 				if (found_config != NULL) {
1849fa9e4066Sahrens 					(void) fprintf(stderr, gettext(
1850fa9e4066Sahrens 					    "cannot import '%s': more than "
1851fa9e4066Sahrens 					    "one matching pool\n"), searchname);
1852fa9e4066Sahrens 					(void) fprintf(stderr, gettext(
1853fa9e4066Sahrens 					    "import by numeric ID instead\n"));
185499653d4eSeschrock 					err = B_TRUE;
1855fa9e4066Sahrens 				}
1856fa9e4066Sahrens 				found_config = config;
1857fa9e4066Sahrens 			}
1858fa9e4066Sahrens 		} else {
1859fa9e4066Sahrens 			uint64_t guid;
1860fa9e4066Sahrens 
1861fa9e4066Sahrens 			/*
1862fa9e4066Sahrens 			 * Search for a pool by guid.
1863fa9e4066Sahrens 			 */
1864fa9e4066Sahrens 			verify(nvlist_lookup_uint64(config,
1865fa9e4066Sahrens 			    ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
1866fa9e4066Sahrens 
1867fa9e4066Sahrens 			if (guid == searchguid)
1868fa9e4066Sahrens 				found_config = config;
1869fa9e4066Sahrens 		}
1870fa9e4066Sahrens 	}
1871fa9e4066Sahrens 
1872fa9e4066Sahrens 	/*
1873fa9e4066Sahrens 	 * If we were searching for a specific pool, verify that we found a
1874fa9e4066Sahrens 	 * pool, and then do the import.
1875fa9e4066Sahrens 	 */
1876fa9e4066Sahrens 	if (argc != 0 && err == 0) {
1877fa9e4066Sahrens 		if (found_config == NULL) {
1878fa9e4066Sahrens 			(void) fprintf(stderr, gettext("cannot import '%s': "
1879fa9e4066Sahrens 			    "no such pool available\n"), argv[0]);
188099653d4eSeschrock 			err = B_TRUE;
1881fa9e4066Sahrens 		} else {
1882fa9e4066Sahrens 			err |= do_import(found_config, argc == 1 ? NULL :
18834f0f5e5bSVictor Latushkin 			    argv[1], mntopts, do_force, props, do_verbatim);
1884fa9e4066Sahrens 		}
1885fa9e4066Sahrens 	}
1886fa9e4066Sahrens 
1887fa9e4066Sahrens 	/*
1888fa9e4066Sahrens 	 * If we were just looking for pools, report an error if none were
1889fa9e4066Sahrens 	 * found.
1890fa9e4066Sahrens 	 */
1891fa9e4066Sahrens 	if (argc == 0 && first)
1892fa9e4066Sahrens 		(void) fprintf(stderr,
1893fa9e4066Sahrens 		    gettext("no pools available to import\n"));
1894fa9e4066Sahrens 
1895ecd6cf80Smarks error:
18962f8aaab3Seschrock 	nvlist_free(props);
1897fa9e4066Sahrens 	nvlist_free(pools);
1898*468c413aSTim Haley 	nvlist_free(policy);
189999653d4eSeschrock 	free(searchdirs);
1900fa9e4066Sahrens 
1901fa9e4066Sahrens 	return (err ? 1 : 0);
1902fa9e4066Sahrens }
1903fa9e4066Sahrens 
1904fa9e4066Sahrens typedef struct iostat_cbdata {
1905fa9e4066Sahrens 	zpool_list_t *cb_list;
1906fa9e4066Sahrens 	int cb_verbose;
1907fa9e4066Sahrens 	int cb_iteration;
1908fa9e4066Sahrens 	int cb_namewidth;
1909fa9e4066Sahrens } iostat_cbdata_t;
1910fa9e4066Sahrens 
1911fa9e4066Sahrens static void
1912fa9e4066Sahrens print_iostat_separator(iostat_cbdata_t *cb)
1913fa9e4066Sahrens {
1914fa9e4066Sahrens 	int i = 0;
1915fa9e4066Sahrens 
1916fa9e4066Sahrens 	for (i = 0; i < cb->cb_namewidth; i++)
1917fa9e4066Sahrens 		(void) printf("-");
1918fa9e4066Sahrens 	(void) printf("  -----  -----  -----  -----  -----  -----\n");
1919fa9e4066Sahrens }
1920fa9e4066Sahrens 
1921fa9e4066Sahrens static void
1922fa9e4066Sahrens print_iostat_header(iostat_cbdata_t *cb)
1923fa9e4066Sahrens {
1924fa9e4066Sahrens 	(void) printf("%*s     capacity     operations    bandwidth\n",
1925fa9e4066Sahrens 	    cb->cb_namewidth, "");
1926fa9e4066Sahrens 	(void) printf("%-*s   used  avail   read  write   read  write\n",
1927fa9e4066Sahrens 	    cb->cb_namewidth, "pool");
1928fa9e4066Sahrens 	print_iostat_separator(cb);
1929fa9e4066Sahrens }
1930fa9e4066Sahrens 
1931fa9e4066Sahrens /*
1932fa9e4066Sahrens  * Display a single statistic.
1933fa9e4066Sahrens  */
1934990b4856Slling static void
1935fa9e4066Sahrens print_one_stat(uint64_t value)
1936fa9e4066Sahrens {
1937fa9e4066Sahrens 	char buf[64];
1938fa9e4066Sahrens 
1939fa9e4066Sahrens 	zfs_nicenum(value, buf, sizeof (buf));
1940fa9e4066Sahrens 	(void) printf("  %5s", buf);
1941fa9e4066Sahrens }
1942fa9e4066Sahrens 
1943fa9e4066Sahrens /*
1944fa9e4066Sahrens  * Print out all the statistics for the given vdev.  This can either be the
1945fa9e4066Sahrens  * toplevel configuration, or called recursively.  If 'name' is NULL, then this
1946fa9e4066Sahrens  * is a verbose output, and we don't want to display the toplevel pool stats.
1947fa9e4066Sahrens  */
1948fa9e4066Sahrens void
1949c67d9675Seschrock print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
1950c67d9675Seschrock     nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
1951fa9e4066Sahrens {
1952fa9e4066Sahrens 	nvlist_t **oldchild, **newchild;
1953fa9e4066Sahrens 	uint_t c, children;
1954fa9e4066Sahrens 	vdev_stat_t *oldvs, *newvs;
1955fa9e4066Sahrens 	vdev_stat_t zerovs = { 0 };
1956fa9e4066Sahrens 	uint64_t tdelta;
1957fa9e4066Sahrens 	double scale;
1958afefbcddSeschrock 	char *vname;
1959fa9e4066Sahrens 
1960fa9e4066Sahrens 	if (oldnv != NULL) {
1961fa9e4066Sahrens 		verify(nvlist_lookup_uint64_array(oldnv, ZPOOL_CONFIG_STATS,
1962fa9e4066Sahrens 		    (uint64_t **)&oldvs, &c) == 0);
1963fa9e4066Sahrens 	} else {
1964fa9e4066Sahrens 		oldvs = &zerovs;
1965fa9e4066Sahrens 	}
1966fa9e4066Sahrens 
1967fa9e4066Sahrens 	verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_STATS,
1968fa9e4066Sahrens 	    (uint64_t **)&newvs, &c) == 0);
1969fa9e4066Sahrens 
1970fa9e4066Sahrens 	if (strlen(name) + depth > cb->cb_namewidth)
1971fa9e4066Sahrens 		(void) printf("%*s%s", depth, "", name);
1972fa9e4066Sahrens 	else
1973fa9e4066Sahrens 		(void) printf("%*s%s%*s", depth, "", name,
1974fa9e4066Sahrens 		    (int)(cb->cb_namewidth - strlen(name) - depth), "");
1975fa9e4066Sahrens 
1976fa9e4066Sahrens 	tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
1977fa9e4066Sahrens 
1978fa9e4066Sahrens 	if (tdelta == 0)
1979fa9e4066Sahrens 		scale = 1.0;
1980fa9e4066Sahrens 	else
1981fa9e4066Sahrens 		scale = (double)NANOSEC / tdelta;
1982fa9e4066Sahrens 
1983fa9e4066Sahrens 	/* only toplevel vdevs have capacity stats */
1984fa9e4066Sahrens 	if (newvs->vs_space == 0) {
1985fa9e4066Sahrens 		(void) printf("      -      -");
1986fa9e4066Sahrens 	} else {
1987fa9e4066Sahrens 		print_one_stat(newvs->vs_alloc);
1988fa9e4066Sahrens 		print_one_stat(newvs->vs_space - newvs->vs_alloc);
1989fa9e4066Sahrens 	}
1990fa9e4066Sahrens 
1991fa9e4066Sahrens 	print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
1992fa9e4066Sahrens 	    oldvs->vs_ops[ZIO_TYPE_READ])));
1993fa9e4066Sahrens 
1994fa9e4066Sahrens 	print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
1995fa9e4066Sahrens 	    oldvs->vs_ops[ZIO_TYPE_WRITE])));
1996fa9e4066Sahrens 
1997fa9e4066Sahrens 	print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
1998fa9e4066Sahrens 	    oldvs->vs_bytes[ZIO_TYPE_READ])));
1999fa9e4066Sahrens 
2000fa9e4066Sahrens 	print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2001fa9e4066Sahrens 	    oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2002fa9e4066Sahrens 
2003fa9e4066Sahrens 	(void) printf("\n");
2004fa9e4066Sahrens 
2005fa9e4066Sahrens 	if (!cb->cb_verbose)
2006fa9e4066Sahrens 		return;
2007fa9e4066Sahrens 
2008fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2009fa9e4066Sahrens 	    &newchild, &children) != 0)
2010fa9e4066Sahrens 		return;
2011fa9e4066Sahrens 
2012fa9e4066Sahrens 	if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2013fa9e4066Sahrens 	    &oldchild, &c) != 0)
2014fa9e4066Sahrens 		return;
2015fa9e4066Sahrens 
2016afefbcddSeschrock 	for (c = 0; c < children; c++) {
201788ecc943SGeorge Wilson 		vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2018c67d9675Seschrock 		print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2019afefbcddSeschrock 		    newchild[c], cb, depth + 2);
2020afefbcddSeschrock 		free(vname);
2021afefbcddSeschrock 	}
2022fa94a07fSbrendan 
2023fa94a07fSbrendan 	/*
2024fa94a07fSbrendan 	 * Include level 2 ARC devices in iostat output
2025fa94a07fSbrendan 	 */
2026fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2027fa94a07fSbrendan 	    &newchild, &children) != 0)
2028fa94a07fSbrendan 		return;
2029fa94a07fSbrendan 
2030fa94a07fSbrendan 	if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2031fa94a07fSbrendan 	    &oldchild, &c) != 0)
2032fa94a07fSbrendan 		return;
2033fa94a07fSbrendan 
2034fa94a07fSbrendan 	if (children > 0) {
2035fa94a07fSbrendan 		(void) printf("%-*s      -      -      -      -      -      "
2036fa94a07fSbrendan 		    "-\n", cb->cb_namewidth, "cache");
2037fa94a07fSbrendan 		for (c = 0; c < children; c++) {
203888ecc943SGeorge Wilson 			vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
203988ecc943SGeorge Wilson 			    B_FALSE);
2040fa94a07fSbrendan 			print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2041fa94a07fSbrendan 			    newchild[c], cb, depth + 2);
2042fa94a07fSbrendan 			free(vname);
2043fa94a07fSbrendan 		}
2044fa94a07fSbrendan 	}
2045fa9e4066Sahrens }
2046fa9e4066Sahrens 
2047088e9d47Seschrock static int
2048088e9d47Seschrock refresh_iostat(zpool_handle_t *zhp, void *data)
2049088e9d47Seschrock {
2050088e9d47Seschrock 	iostat_cbdata_t *cb = data;
205194de1d4cSeschrock 	boolean_t missing;
2052088e9d47Seschrock 
2053088e9d47Seschrock 	/*
2054088e9d47Seschrock 	 * If the pool has disappeared, remove it from the list and continue.
2055088e9d47Seschrock 	 */
205694de1d4cSeschrock 	if (zpool_refresh_stats(zhp, &missing) != 0)
205794de1d4cSeschrock 		return (-1);
205894de1d4cSeschrock 
205994de1d4cSeschrock 	if (missing)
2060088e9d47Seschrock 		pool_list_remove(cb->cb_list, zhp);
2061088e9d47Seschrock 
2062088e9d47Seschrock 	return (0);
2063088e9d47Seschrock }
2064088e9d47Seschrock 
2065fa9e4066Sahrens /*
2066fa9e4066Sahrens  * Callback to print out the iostats for the given pool.
2067fa9e4066Sahrens  */
2068fa9e4066Sahrens int
2069fa9e4066Sahrens print_iostat(zpool_handle_t *zhp, void *data)
2070fa9e4066Sahrens {
2071fa9e4066Sahrens 	iostat_cbdata_t *cb = data;
2072fa9e4066Sahrens 	nvlist_t *oldconfig, *newconfig;
2073fa9e4066Sahrens 	nvlist_t *oldnvroot, *newnvroot;
2074fa9e4066Sahrens 
2075088e9d47Seschrock 	newconfig = zpool_get_config(zhp, &oldconfig);
2076fa9e4066Sahrens 
2077088e9d47Seschrock 	if (cb->cb_iteration == 1)
2078fa9e4066Sahrens 		oldconfig = NULL;
2079fa9e4066Sahrens 
2080fa9e4066Sahrens 	verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2081fa9e4066Sahrens 	    &newnvroot) == 0);
2082fa9e4066Sahrens 
2083088e9d47Seschrock 	if (oldconfig == NULL)
2084fa9e4066Sahrens 		oldnvroot = NULL;
2085088e9d47Seschrock 	else
2086088e9d47Seschrock 		verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2087088e9d47Seschrock 		    &oldnvroot) == 0);
2088fa9e4066Sahrens 
2089fa9e4066Sahrens 	/*
2090fa9e4066Sahrens 	 * Print out the statistics for the pool.
2091fa9e4066Sahrens 	 */
2092c67d9675Seschrock 	print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2093fa9e4066Sahrens 
2094fa9e4066Sahrens 	if (cb->cb_verbose)
2095fa9e4066Sahrens 		print_iostat_separator(cb);
2096fa9e4066Sahrens 
2097fa9e4066Sahrens 	return (0);
2098fa9e4066Sahrens }
2099fa9e4066Sahrens 
2100fa9e4066Sahrens int
2101fa9e4066Sahrens get_namewidth(zpool_handle_t *zhp, void *data)
2102fa9e4066Sahrens {
2103fa9e4066Sahrens 	iostat_cbdata_t *cb = data;
2104fa9e4066Sahrens 	nvlist_t *config, *nvroot;
2105fa9e4066Sahrens 
2106088e9d47Seschrock 	if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2107fa9e4066Sahrens 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2108fa9e4066Sahrens 		    &nvroot) == 0);
2109fa9e4066Sahrens 		if (!cb->cb_verbose)
2110fa9e4066Sahrens 			cb->cb_namewidth = strlen(zpool_get_name(zhp));
2111fa9e4066Sahrens 		else
2112c67d9675Seschrock 			cb->cb_namewidth = max_width(zhp, nvroot, 0, 0);
2113fa9e4066Sahrens 	}
2114fa9e4066Sahrens 
2115fa9e4066Sahrens 	/*
2116fa9e4066Sahrens 	 * The width must fall into the range [10,38].  The upper limit is the
2117fa9e4066Sahrens 	 * maximum we can have and still fit in 80 columns.
2118fa9e4066Sahrens 	 */
2119fa9e4066Sahrens 	if (cb->cb_namewidth < 10)
2120fa9e4066Sahrens 		cb->cb_namewidth = 10;
2121fa9e4066Sahrens 	if (cb->cb_namewidth > 38)
2122fa9e4066Sahrens 		cb->cb_namewidth = 38;
2123fa9e4066Sahrens 
2124fa9e4066Sahrens 	return (0);
2125fa9e4066Sahrens }
2126fa9e4066Sahrens 
2127fa9e4066Sahrens /*
212826fd7700SKrishnendu Sadhukhan - Sun Microsystems  * zpool iostat [-T d|u] [-v] [pool] ... [interval [count]]
2129fa9e4066Sahrens  *
213026fd7700SKrishnendu Sadhukhan - Sun Microsystems  *	-T	Display a timestamp in date(1) or Unix format
2131fa9e4066Sahrens  *	-v	Display statistics for individual vdevs
2132fa9e4066Sahrens  *
2133fa9e4066Sahrens  * This command can be tricky because we want to be able to deal with pool
2134fa9e4066Sahrens  * creation/destruction as well as vdev configuration changes.  The bulk of this
2135fa9e4066Sahrens  * processing is handled by the pool_list_* routines in zpool_iter.c.  We rely
2136fa9e4066Sahrens  * on pool_list_update() to detect the addition of new pools.  Configuration
2137fa9e4066Sahrens  * changes are all handled within libzfs.
2138fa9e4066Sahrens  */
2139fa9e4066Sahrens int
2140fa9e4066Sahrens zpool_do_iostat(int argc, char **argv)
2141fa9e4066Sahrens {
2142fa9e4066Sahrens 	int c;
2143fa9e4066Sahrens 	int ret;
2144fa9e4066Sahrens 	int npools;
2145fa9e4066Sahrens 	unsigned long interval = 0, count = 0;
2146fa9e4066Sahrens 	zpool_list_t *list;
214799653d4eSeschrock 	boolean_t verbose = B_FALSE;
2148fa9e4066Sahrens 	iostat_cbdata_t cb;
2149fa9e4066Sahrens 
2150fa9e4066Sahrens 	/* check options */
215126fd7700SKrishnendu Sadhukhan - Sun Microsystems 	while ((c = getopt(argc, argv, "T:v")) != -1) {
2152fa9e4066Sahrens 		switch (c) {
215326fd7700SKrishnendu Sadhukhan - Sun Microsystems 		case 'T':
215426fd7700SKrishnendu Sadhukhan - Sun Microsystems 			if (optarg) {
215526fd7700SKrishnendu Sadhukhan - Sun Microsystems 				if (*optarg == 'u')
215626fd7700SKrishnendu Sadhukhan - Sun Microsystems 					timestamp_fmt = UDATE;
215726fd7700SKrishnendu Sadhukhan - Sun Microsystems 				else if (*optarg == 'd')
215826fd7700SKrishnendu Sadhukhan - Sun Microsystems 					timestamp_fmt = DDATE;
215926fd7700SKrishnendu Sadhukhan - Sun Microsystems 				else
216026fd7700SKrishnendu Sadhukhan - Sun Microsystems 					usage(B_FALSE);
216126fd7700SKrishnendu Sadhukhan - Sun Microsystems 			} else {
216226fd7700SKrishnendu Sadhukhan - Sun Microsystems 				usage(B_FALSE);
216326fd7700SKrishnendu Sadhukhan - Sun Microsystems 			}
216426fd7700SKrishnendu Sadhukhan - Sun Microsystems 			break;
2165fa9e4066Sahrens 		case 'v':
216699653d4eSeschrock 			verbose = B_TRUE;
2167fa9e4066Sahrens 			break;
2168fa9e4066Sahrens 		case '?':
2169fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2170fa9e4066Sahrens 			    optopt);
217199653d4eSeschrock 			usage(B_FALSE);
2172fa9e4066Sahrens 		}
2173fa9e4066Sahrens 	}
2174fa9e4066Sahrens 
2175fa9e4066Sahrens 	argc -= optind;
2176fa9e4066Sahrens 	argv += optind;
2177fa9e4066Sahrens 
2178fa9e4066Sahrens 	/*
2179fa9e4066Sahrens 	 * Determine if the last argument is an integer or a pool name
2180fa9e4066Sahrens 	 */
2181fa9e4066Sahrens 	if (argc > 0 && isdigit(argv[argc - 1][0])) {
2182fa9e4066Sahrens 		char *end;
2183fa9e4066Sahrens 
2184fa9e4066Sahrens 		errno = 0;
2185fa9e4066Sahrens 		interval = strtoul(argv[argc - 1], &end, 10);
2186fa9e4066Sahrens 
2187fa9e4066Sahrens 		if (*end == '\0' && errno == 0) {
2188fa9e4066Sahrens 			if (interval == 0) {
2189fa9e4066Sahrens 				(void) fprintf(stderr, gettext("interval "
2190fa9e4066Sahrens 				    "cannot be zero\n"));
219199653d4eSeschrock 				usage(B_FALSE);
2192fa9e4066Sahrens 			}
2193fa9e4066Sahrens 
2194fa9e4066Sahrens 			/*
2195fa9e4066Sahrens 			 * Ignore the last parameter
2196fa9e4066Sahrens 			 */
2197fa9e4066Sahrens 			argc--;
2198fa9e4066Sahrens 		} else {
2199fa9e4066Sahrens 			/*
2200fa9e4066Sahrens 			 * If this is not a valid number, just plow on.  The
2201fa9e4066Sahrens 			 * user will get a more informative error message later
2202fa9e4066Sahrens 			 * on.
2203fa9e4066Sahrens 			 */
2204fa9e4066Sahrens 			interval = 0;
2205fa9e4066Sahrens 		}
2206fa9e4066Sahrens 	}
2207fa9e4066Sahrens 
2208fa9e4066Sahrens 	/*
2209fa9e4066Sahrens 	 * If the last argument is also an integer, then we have both a count
2210fa9e4066Sahrens 	 * and an integer.
2211fa9e4066Sahrens 	 */
2212fa9e4066Sahrens 	if (argc > 0 && isdigit(argv[argc - 1][0])) {
2213fa9e4066Sahrens 		char *end;
2214fa9e4066Sahrens 
2215fa9e4066Sahrens 		errno = 0;
2216fa9e4066Sahrens 		count = interval;
2217fa9e4066Sahrens 		interval = strtoul(argv[argc - 1], &end, 10);
2218fa9e4066Sahrens 
2219fa9e4066Sahrens 		if (*end == '\0' && errno == 0) {
2220fa9e4066Sahrens 			if (interval == 0) {
2221fa9e4066Sahrens 				(void) fprintf(stderr, gettext("interval "
2222fa9e4066Sahrens 				    "cannot be zero\n"));
222399653d4eSeschrock 				usage(B_FALSE);
2224fa9e4066Sahrens 			}
2225fa9e4066Sahrens 
2226fa9e4066Sahrens 			/*
2227fa9e4066Sahrens 			 * Ignore the last parameter
2228fa9e4066Sahrens 			 */
2229fa9e4066Sahrens 			argc--;
2230fa9e4066Sahrens 		} else {
2231fa9e4066Sahrens 			interval = 0;
2232fa9e4066Sahrens 		}
2233fa9e4066Sahrens 	}
2234fa9e4066Sahrens 
2235fa9e4066Sahrens 	/*
2236fa9e4066Sahrens 	 * Construct the list of all interesting pools.
2237fa9e4066Sahrens 	 */
2238fa9e4066Sahrens 	ret = 0;
2239b1b8ab34Slling 	if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2240fa9e4066Sahrens 		return (1);
2241fa9e4066Sahrens 
224299653d4eSeschrock 	if (pool_list_count(list) == 0 && argc != 0) {
224399653d4eSeschrock 		pool_list_free(list);
2244fa9e4066Sahrens 		return (1);
224599653d4eSeschrock 	}
2246fa9e4066Sahrens 
2247fa9e4066Sahrens 	if (pool_list_count(list) == 0 && interval == 0) {
224899653d4eSeschrock 		pool_list_free(list);
2249fa9e4066Sahrens 		(void) fprintf(stderr, gettext("no pools available\n"));
2250fa9e4066Sahrens 		return (1);
2251fa9e4066Sahrens 	}
2252fa9e4066Sahrens 
2253fa9e4066Sahrens 	/*
2254fa9e4066Sahrens 	 * Enter the main iostat loop.
2255fa9e4066Sahrens 	 */
2256fa9e4066Sahrens 	cb.cb_list = list;
2257fa9e4066Sahrens 	cb.cb_verbose = verbose;
2258fa9e4066Sahrens 	cb.cb_iteration = 0;
2259fa9e4066Sahrens 	cb.cb_namewidth = 0;
2260fa9e4066Sahrens 
2261fa9e4066Sahrens 	for (;;) {
2262fa9e4066Sahrens 		pool_list_update(list);
2263fa9e4066Sahrens 
2264fa9e4066Sahrens 		if ((npools = pool_list_count(list)) == 0)
2265fa9e4066Sahrens 			break;
2266fa9e4066Sahrens 
2267088e9d47Seschrock 		/*
2268088e9d47Seschrock 		 * Refresh all statistics.  This is done as an explicit step
2269088e9d47Seschrock 		 * before calculating the maximum name width, so that any
2270088e9d47Seschrock 		 * configuration changes are properly accounted for.
2271088e9d47Seschrock 		 */
227299653d4eSeschrock 		(void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2273088e9d47Seschrock 
2274fa9e4066Sahrens 		/*
2275fa9e4066Sahrens 		 * Iterate over all pools to determine the maximum width
2276fa9e4066Sahrens 		 * for the pool / device name column across all pools.
2277fa9e4066Sahrens 		 */
2278fa9e4066Sahrens 		cb.cb_namewidth = 0;
227999653d4eSeschrock 		(void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2280fa9e4066Sahrens 
228126fd7700SKrishnendu Sadhukhan - Sun Microsystems 		if (timestamp_fmt != NODATE)
228226fd7700SKrishnendu Sadhukhan - Sun Microsystems 			print_timestamp(timestamp_fmt);
228326fd7700SKrishnendu Sadhukhan - Sun Microsystems 
2284fa9e4066Sahrens 		/*
2285fa9e4066Sahrens 		 * If it's the first time, or verbose mode, print the header.
2286fa9e4066Sahrens 		 */
2287fa9e4066Sahrens 		if (++cb.cb_iteration == 1 || verbose)
2288fa9e4066Sahrens 			print_iostat_header(&cb);
2289fa9e4066Sahrens 
229099653d4eSeschrock 		(void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2291fa9e4066Sahrens 
2292fa9e4066Sahrens 		/*
2293fa9e4066Sahrens 		 * If there's more than one pool, and we're not in verbose mode
2294fa9e4066Sahrens 		 * (which prints a separator for us), then print a separator.
2295fa9e4066Sahrens 		 */
2296fa9e4066Sahrens 		if (npools > 1 && !verbose)
2297fa9e4066Sahrens 			print_iostat_separator(&cb);
2298fa9e4066Sahrens 
2299fa9e4066Sahrens 		if (verbose)
2300fa9e4066Sahrens 			(void) printf("\n");
2301fa9e4066Sahrens 
230239c23413Seschrock 		/*
230339c23413Seschrock 		 * Flush the output so that redirection to a file isn't buffered
230439c23413Seschrock 		 * indefinitely.
230539c23413Seschrock 		 */
230639c23413Seschrock 		(void) fflush(stdout);
230739c23413Seschrock 
2308fa9e4066Sahrens 		if (interval == 0)
2309fa9e4066Sahrens 			break;
2310fa9e4066Sahrens 
2311fa9e4066Sahrens 		if (count != 0 && --count == 0)
2312fa9e4066Sahrens 			break;
2313fa9e4066Sahrens 
2314fa9e4066Sahrens 		(void) sleep(interval);
2315fa9e4066Sahrens 	}
2316fa9e4066Sahrens 
2317fa9e4066Sahrens 	pool_list_free(list);
2318fa9e4066Sahrens 
2319fa9e4066Sahrens 	return (ret);
2320fa9e4066Sahrens }
2321fa9e4066Sahrens 
2322fa9e4066Sahrens typedef struct list_cbdata {
232399653d4eSeschrock 	boolean_t	cb_scripted;
232499653d4eSeschrock 	boolean_t	cb_first;
2325990b4856Slling 	zprop_list_t	*cb_proplist;
2326fa9e4066Sahrens } list_cbdata_t;
2327fa9e4066Sahrens 
2328fa9e4066Sahrens /*
2329fa9e4066Sahrens  * Given a list of columns to display, output appropriate headers for each one.
2330fa9e4066Sahrens  */
2331990b4856Slling static void
2332990b4856Slling print_header(zprop_list_t *pl)
2333fa9e4066Sahrens {
2334990b4856Slling 	const char *header;
2335990b4856Slling 	boolean_t first = B_TRUE;
2336990b4856Slling 	boolean_t right_justify;
2337990b4856Slling 
2338990b4856Slling 	for (; pl != NULL; pl = pl->pl_next) {
2339990b4856Slling 		if (pl->pl_prop == ZPROP_INVAL)
2340990b4856Slling 			continue;
2341fa9e4066Sahrens 
2342990b4856Slling 		if (!first)
2343fa9e4066Sahrens 			(void) printf("  ");
2344fa9e4066Sahrens 		else
2345990b4856Slling 			first = B_FALSE;
2346990b4856Slling 
2347990b4856Slling 		header = zpool_prop_column_name(pl->pl_prop);
2348990b4856Slling 		right_justify = zpool_prop_align_right(pl->pl_prop);
2349fa9e4066Sahrens 
2350990b4856Slling 		if (pl->pl_next == NULL && !right_justify)
2351990b4856Slling 			(void) printf("%s", header);
2352990b4856Slling 		else if (right_justify)
2353990b4856Slling 			(void) printf("%*s", pl->pl_width, header);
2354990b4856Slling 		else
2355990b4856Slling 			(void) printf("%-*s", pl->pl_width, header);
2356fa9e4066Sahrens 	}
2357fa9e4066Sahrens 
2358fa9e4066Sahrens 	(void) printf("\n");
2359fa9e4066Sahrens }
2360fa9e4066Sahrens 
2361990b4856Slling /*
2362990b4856Slling  * Given a pool and a list of properties, print out all the properties according
2363990b4856Slling  * to the described layout.
2364990b4856Slling  */
2365990b4856Slling static void
2366990b4856Slling print_pool(zpool_handle_t *zhp, zprop_list_t *pl, int scripted)
2367fa9e4066Sahrens {
2368990b4856Slling 	boolean_t first = B_TRUE;
2369990b4856Slling 	char property[ZPOOL_MAXPROPLEN];
2370990b4856Slling 	char *propstr;
2371990b4856Slling 	boolean_t right_justify;
2372990b4856Slling 	int width;
2373990b4856Slling 
2374990b4856Slling 	for (; pl != NULL; pl = pl->pl_next) {
2375990b4856Slling 		if (!first) {
2376990b4856Slling 			if (scripted)
2377fa9e4066Sahrens 				(void) printf("\t");
2378fa9e4066Sahrens 			else
2379fa9e4066Sahrens 				(void) printf("  ");
2380990b4856Slling 		} else {
2381990b4856Slling 			first = B_FALSE;
2382fa9e4066Sahrens 		}
2383fa9e4066Sahrens 
2384990b4856Slling 		right_justify = B_FALSE;
2385990b4856Slling 		if (pl->pl_prop != ZPROP_INVAL) {
2386990b4856Slling 			if (zpool_get_prop(zhp, pl->pl_prop, property,
2387990b4856Slling 			    sizeof (property), NULL) != 0)
2388990b4856Slling 				propstr = "-";
2389fa9e4066Sahrens 			else
2390990b4856Slling 				propstr = property;
2391fa9e4066Sahrens 
2392990b4856Slling 			right_justify = zpool_prop_align_right(pl->pl_prop);
2393990b4856Slling 		} else {
2394990b4856Slling 			propstr = "-";
2395990b4856Slling 		}
2396fa9e4066Sahrens 
2397990b4856Slling 		width = pl->pl_width;
2398fa9e4066Sahrens 
2399990b4856Slling 		/*
2400990b4856Slling 		 * If this is being called in scripted mode, or if this is the
2401990b4856Slling 		 * last column and it is left-justified, don't include a width
2402990b4856Slling 		 * format specifier.
2403990b4856Slling 		 */
2404990b4856Slling 		if (scripted || (pl->pl_next == NULL && !right_justify))
2405990b4856Slling 			(void) printf("%s", propstr);
2406990b4856Slling 		else if (right_justify)
2407990b4856Slling 			(void) printf("%*s", width, propstr);
2408990b4856Slling 		else
2409990b4856Slling 			(void) printf("%-*s", width, propstr);
2410990b4856Slling 	}
2411fa9e4066Sahrens 
2412990b4856Slling 	(void) printf("\n");
2413990b4856Slling }
2414fa9e4066Sahrens 
2415990b4856Slling /*
2416990b4856Slling  * Generic callback function to list a pool.
2417990b4856Slling  */
2418990b4856Slling int
2419990b4856Slling list_callback(zpool_handle_t *zhp, void *data)
2420990b4856Slling {
2421990b4856Slling 	list_cbdata_t *cbp = data;
2422fa9e4066Sahrens 
2423990b4856Slling 	if (cbp->cb_first) {
2424990b4856Slling 		if (!cbp->cb_scripted)
2425990b4856Slling 			print_header(cbp->cb_proplist);
2426990b4856Slling 		cbp->cb_first = B_FALSE;
2427fa9e4066Sahrens 	}
2428fa9e4066Sahrens 
2429990b4856Slling 	print_pool(zhp, cbp->cb_proplist, cbp->cb_scripted);
2430fa9e4066Sahrens 
2431fa9e4066Sahrens 	return (0);
2432fa9e4066Sahrens }
2433fa9e4066Sahrens 
2434fa9e4066Sahrens /*
2435990b4856Slling  * zpool list [-H] [-o prop[,prop]*] [pool] ...
2436fa9e4066Sahrens  *
2437990b4856Slling  *	-H	Scripted mode.  Don't display headers, and separate properties
2438990b4856Slling  *		by a single tab.
2439990b4856Slling  *	-o	List of properties to display.  Defaults to
2440990b4856Slling  *		"name,size,used,available,capacity,health,altroot"
2441fa9e4066Sahrens  *
2442fa9e4066Sahrens  * List all pools in the system, whether or not they're healthy.  Output space
2443fa9e4066Sahrens  * statistics for each one, as well as health status summary.
2444fa9e4066Sahrens  */
2445fa9e4066Sahrens int
2446fa9e4066Sahrens zpool_do_list(int argc, char **argv)
2447fa9e4066Sahrens {
2448fa9e4066Sahrens 	int c;
2449fa9e4066Sahrens 	int ret;
2450fa9e4066Sahrens 	list_cbdata_t cb = { 0 };
2451990b4856Slling 	static char default_props[] =
2452990b4856Slling 	    "name,size,used,available,capacity,health,altroot";
2453990b4856Slling 	char *props = default_props;
2454fa9e4066Sahrens 
2455fa9e4066Sahrens 	/* check options */
2456fa9e4066Sahrens 	while ((c = getopt(argc, argv, ":Ho:")) != -1) {
2457fa9e4066Sahrens 		switch (c) {
2458fa9e4066Sahrens 		case 'H':
245999653d4eSeschrock 			cb.cb_scripted = B_TRUE;
2460fa9e4066Sahrens 			break;
2461fa9e4066Sahrens 		case 'o':
2462990b4856Slling 			props = optarg;
2463fa9e4066Sahrens 			break;
2464fa9e4066Sahrens 		case ':':
2465fa9e4066Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
2466fa9e4066Sahrens 			    "'%c' option\n"), optopt);
246799653d4eSeschrock 			usage(B_FALSE);
2468fa9e4066Sahrens 			break;
2469fa9e4066Sahrens 		case '?':
2470fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2471fa9e4066Sahrens 			    optopt);
247299653d4eSeschrock 			usage(B_FALSE);
2473fa9e4066Sahrens 		}
2474fa9e4066Sahrens 	}
2475fa9e4066Sahrens 
2476fa9e4066Sahrens 	argc -= optind;
2477fa9e4066Sahrens 	argv += optind;
2478fa9e4066Sahrens 
2479990b4856Slling 	if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
2480990b4856Slling 		usage(B_FALSE);
2481fa9e4066Sahrens 
248299653d4eSeschrock 	cb.cb_first = B_TRUE;
2483fa9e4066Sahrens 
2484990b4856Slling 	ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
2485990b4856Slling 	    list_callback, &cb);
2486990b4856Slling 
2487990b4856Slling 	zprop_free_list(cb.cb_proplist);
2488fa9e4066Sahrens 
2489fce7d82bSmmusante 	if (argc == 0 && cb.cb_first && !cb.cb_scripted) {
2490fa9e4066Sahrens 		(void) printf(gettext("no pools available\n"));
2491fa9e4066Sahrens 		return (0);
2492fa9e4066Sahrens 	}
2493fa9e4066Sahrens 
2494fa9e4066Sahrens 	return (ret);
2495fa9e4066Sahrens }
2496fa9e4066Sahrens 
2497fa9e4066Sahrens static nvlist_t *
2498fa9e4066Sahrens zpool_get_vdev_by_name(nvlist_t *nv, char *name)
2499fa9e4066Sahrens {
2500fa9e4066Sahrens 	nvlist_t **child;
2501fa9e4066Sahrens 	uint_t c, children;
2502fa9e4066Sahrens 	nvlist_t *match;
2503fa9e4066Sahrens 	char *path;
2504fa9e4066Sahrens 
2505fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2506fa9e4066Sahrens 	    &child, &children) != 0) {
2507fa9e4066Sahrens 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2508fa9e4066Sahrens 		if (strncmp(name, "/dev/dsk/", 9) == 0)
2509fa9e4066Sahrens 			name += 9;
2510fa9e4066Sahrens 		if (strncmp(path, "/dev/dsk/", 9) == 0)
2511fa9e4066Sahrens 			path += 9;
2512fa9e4066Sahrens 		if (strcmp(name, path) == 0)
2513fa9e4066Sahrens 			return (nv);
2514fa9e4066Sahrens 		return (NULL);
2515fa9e4066Sahrens 	}
2516fa9e4066Sahrens 
2517fa9e4066Sahrens 	for (c = 0; c < children; c++)
2518fa9e4066Sahrens 		if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
2519fa9e4066Sahrens 			return (match);
2520fa9e4066Sahrens 
2521fa9e4066Sahrens 	return (NULL);
2522fa9e4066Sahrens }
2523fa9e4066Sahrens 
2524fa9e4066Sahrens static int
2525fa9e4066Sahrens zpool_do_attach_or_replace(int argc, char **argv, int replacing)
2526fa9e4066Sahrens {
252799653d4eSeschrock 	boolean_t force = B_FALSE;
2528fa9e4066Sahrens 	int c;
2529fa9e4066Sahrens 	nvlist_t *nvroot;
2530fa9e4066Sahrens 	char *poolname, *old_disk, *new_disk;
2531fa9e4066Sahrens 	zpool_handle_t *zhp;
253299653d4eSeschrock 	int ret;
2533fa9e4066Sahrens 
2534fa9e4066Sahrens 	/* check options */
2535fa9e4066Sahrens 	while ((c = getopt(argc, argv, "f")) != -1) {
2536fa9e4066Sahrens 		switch (c) {
2537fa9e4066Sahrens 		case 'f':
253899653d4eSeschrock 			force = B_TRUE;
2539fa9e4066Sahrens 			break;
2540fa9e4066Sahrens 		case '?':
2541fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2542fa9e4066Sahrens 			    optopt);
254399653d4eSeschrock 			usage(B_FALSE);
2544fa9e4066Sahrens 		}
2545fa9e4066Sahrens 	}
2546fa9e4066Sahrens 
2547fa9e4066Sahrens 	argc -= optind;
2548fa9e4066Sahrens 	argv += optind;
2549fa9e4066Sahrens 
2550fa9e4066Sahrens 	/* get pool name and check number of arguments */
2551fa9e4066Sahrens 	if (argc < 1) {
2552fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
255399653d4eSeschrock 		usage(B_FALSE);
2554fa9e4066Sahrens 	}
2555fa9e4066Sahrens 
2556fa9e4066Sahrens 	poolname = argv[0];
2557fa9e4066Sahrens 
2558fa9e4066Sahrens 	if (argc < 2) {
2559fa9e4066Sahrens 		(void) fprintf(stderr,
2560fa9e4066Sahrens 		    gettext("missing <device> specification\n"));
256199653d4eSeschrock 		usage(B_FALSE);
2562fa9e4066Sahrens 	}
2563fa9e4066Sahrens 
2564fa9e4066Sahrens 	old_disk = argv[1];
2565fa9e4066Sahrens 
2566fa9e4066Sahrens 	if (argc < 3) {
2567fa9e4066Sahrens 		if (!replacing) {
2568fa9e4066Sahrens 			(void) fprintf(stderr,
2569fa9e4066Sahrens 			    gettext("missing <new_device> specification\n"));
257099653d4eSeschrock 			usage(B_FALSE);
2571fa9e4066Sahrens 		}
2572fa9e4066Sahrens 		new_disk = old_disk;
2573fa9e4066Sahrens 		argc -= 1;
2574fa9e4066Sahrens 		argv += 1;
2575fa9e4066Sahrens 	} else {
2576fa9e4066Sahrens 		new_disk = argv[2];
2577fa9e4066Sahrens 		argc -= 2;
2578fa9e4066Sahrens 		argv += 2;
2579fa9e4066Sahrens 	}
2580fa9e4066Sahrens 
2581fa9e4066Sahrens 	if (argc > 1) {
2582fa9e4066Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
258399653d4eSeschrock 		usage(B_FALSE);
2584fa9e4066Sahrens 	}
2585fa9e4066Sahrens 
258699653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2587fa9e4066Sahrens 		return (1);
2588fa9e4066Sahrens 
25898488aeb5Staylor 	if (zpool_get_config(zhp, NULL) == NULL) {
2590fa9e4066Sahrens 		(void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
2591fa9e4066Sahrens 		    poolname);
2592fa9e4066Sahrens 		zpool_close(zhp);
2593fa9e4066Sahrens 		return (1);
2594fa9e4066Sahrens 	}
2595fa9e4066Sahrens 
2596705040edSEric Taylor 	nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
2597705040edSEric Taylor 	    argc, argv);
2598fa9e4066Sahrens 	if (nvroot == NULL) {
2599fa9e4066Sahrens 		zpool_close(zhp);
2600fa9e4066Sahrens 		return (1);
2601fa9e4066Sahrens 	}
2602fa9e4066Sahrens 
260399653d4eSeschrock 	ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
260499653d4eSeschrock 
260599653d4eSeschrock 	nvlist_free(nvroot);
260699653d4eSeschrock 	zpool_close(zhp);
260799653d4eSeschrock 
260899653d4eSeschrock 	return (ret);
2609fa9e4066Sahrens }
2610fa9e4066Sahrens 
2611fa9e4066Sahrens /*
2612fa9e4066Sahrens  * zpool replace [-f] <pool> <device> <new_device>
2613fa9e4066Sahrens  *
2614fa9e4066Sahrens  *	-f	Force attach, even if <new_device> appears to be in use.
2615fa9e4066Sahrens  *
2616fa9e4066Sahrens  * Replace <device> with <new_device>.
2617fa9e4066Sahrens  */
2618fa9e4066Sahrens /* ARGSUSED */
2619fa9e4066Sahrens int
2620fa9e4066Sahrens zpool_do_replace(int argc, char **argv)
2621fa9e4066Sahrens {
2622fa9e4066Sahrens 	return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
2623fa9e4066Sahrens }
2624fa9e4066Sahrens 
2625fa9e4066Sahrens /*
2626fa9e4066Sahrens  * zpool attach [-f] <pool> <device> <new_device>
2627fa9e4066Sahrens  *
2628fa9e4066Sahrens  *	-f	Force attach, even if <new_device> appears to be in use.
2629fa9e4066Sahrens  *
2630fa9e4066Sahrens  * Attach <new_device> to the mirror containing <device>.  If <device> is not
2631fa9e4066Sahrens  * part of a mirror, then <device> will be transformed into a mirror of
2632fa9e4066Sahrens  * <device> and <new_device>.  In either case, <new_device> will begin life
2633fa9e4066Sahrens  * with a DTL of [0, now], and will immediately begin to resilver itself.
2634fa9e4066Sahrens  */
2635fa9e4066Sahrens int
2636fa9e4066Sahrens zpool_do_attach(int argc, char **argv)
2637fa9e4066Sahrens {
2638fa9e4066Sahrens 	return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
2639fa9e4066Sahrens }
2640fa9e4066Sahrens 
2641fa9e4066Sahrens /*
2642fa9e4066Sahrens  * zpool detach [-f] <pool> <device>
2643fa9e4066Sahrens  *
2644fa9e4066Sahrens  *	-f	Force detach of <device>, even if DTLs argue against it
2645fa9e4066Sahrens  *		(not supported yet)
2646fa9e4066Sahrens  *
2647fa9e4066Sahrens  * Detach a device from a mirror.  The operation will be refused if <device>
2648fa9e4066Sahrens  * is the last device in the mirror, or if the DTLs indicate that this device
2649fa9e4066Sahrens  * has the only valid copy of some data.
2650fa9e4066Sahrens  */
2651fa9e4066Sahrens /* ARGSUSED */
2652fa9e4066Sahrens int
2653fa9e4066Sahrens zpool_do_detach(int argc, char **argv)
2654fa9e4066Sahrens {
2655fa9e4066Sahrens 	int c;
2656fa9e4066Sahrens 	char *poolname, *path;
2657fa9e4066Sahrens 	zpool_handle_t *zhp;
265899653d4eSeschrock 	int ret;
2659fa9e4066Sahrens 
2660fa9e4066Sahrens 	/* check options */
2661fa9e4066Sahrens 	while ((c = getopt(argc, argv, "f")) != -1) {
2662fa9e4066Sahrens 		switch (c) {
2663fa9e4066Sahrens 		case 'f':
2664fa9e4066Sahrens 		case '?':
2665fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2666fa9e4066Sahrens 			    optopt);
266799653d4eSeschrock 			usage(B_FALSE);
2668fa9e4066Sahrens 		}
2669fa9e4066Sahrens 	}
2670fa9e4066Sahrens 
2671fa9e4066Sahrens 	argc -= optind;
2672fa9e4066Sahrens 	argv += optind;
2673fa9e4066Sahrens 
2674fa9e4066Sahrens 	/* get pool name and check number of arguments */
2675fa9e4066Sahrens 	if (argc < 1) {
2676fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
267799653d4eSeschrock 		usage(B_FALSE);
2678fa9e4066Sahrens 	}
2679fa9e4066Sahrens 
2680fa9e4066Sahrens 	if (argc < 2) {
2681fa9e4066Sahrens 		(void) fprintf(stderr,
2682fa9e4066Sahrens 		    gettext("missing <device> specification\n"));
268399653d4eSeschrock 		usage(B_FALSE);
2684fa9e4066Sahrens 	}
2685fa9e4066Sahrens 
2686fa9e4066Sahrens 	poolname = argv[0];
2687fa9e4066Sahrens 	path = argv[1];
2688fa9e4066Sahrens 
268999653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2690fa9e4066Sahrens 		return (1);
2691fa9e4066Sahrens 
269299653d4eSeschrock 	ret = zpool_vdev_detach(zhp, path);
269399653d4eSeschrock 
269499653d4eSeschrock 	zpool_close(zhp);
269599653d4eSeschrock 
269699653d4eSeschrock 	return (ret);
2697fa9e4066Sahrens }
2698fa9e4066Sahrens 
2699fa9e4066Sahrens /*
2700441d80aaSlling  * zpool online <pool> <device> ...
2701fa9e4066Sahrens  */
2702fa9e4066Sahrens int
2703fa9e4066Sahrens zpool_do_online(int argc, char **argv)
2704fa9e4066Sahrens {
2705fa9e4066Sahrens 	int c, i;
2706fa9e4066Sahrens 	char *poolname;
2707fa9e4066Sahrens 	zpool_handle_t *zhp;
2708fa9e4066Sahrens 	int ret = 0;
27093d7072f8Seschrock 	vdev_state_t newstate;
2710573ca77eSGeorge Wilson 	int flags = 0;
2711fa9e4066Sahrens 
2712fa9e4066Sahrens 	/* check options */
2713573ca77eSGeorge Wilson 	while ((c = getopt(argc, argv, "et")) != -1) {
2714fa9e4066Sahrens 		switch (c) {
2715573ca77eSGeorge Wilson 		case 'e':
2716573ca77eSGeorge Wilson 			flags |= ZFS_ONLINE_EXPAND;
2717573ca77eSGeorge Wilson 			break;
2718fa9e4066Sahrens 		case 't':
2719fa9e4066Sahrens 		case '?':
2720fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2721fa9e4066Sahrens 			    optopt);
272299653d4eSeschrock 			usage(B_FALSE);
2723fa9e4066Sahrens 		}
2724fa9e4066Sahrens 	}
2725fa9e4066Sahrens 
2726fa9e4066Sahrens 	argc -= optind;
2727fa9e4066Sahrens 	argv += optind;
2728fa9e4066Sahrens 
2729fa9e4066Sahrens 	/* get pool name and check number of arguments */
2730fa9e4066Sahrens 	if (argc < 1) {
2731fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name\n"));
273299653d4eSeschrock 		usage(B_FALSE);
2733fa9e4066Sahrens 	}
2734fa9e4066Sahrens 	if (argc < 2) {
2735fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing device name\n"));
273699653d4eSeschrock 		usage(B_FALSE);
2737fa9e4066Sahrens 	}
2738fa9e4066Sahrens 
2739fa9e4066Sahrens 	poolname = argv[0];
2740fa9e4066Sahrens 
274199653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2742fa9e4066Sahrens 		return (1);
2743fa9e4066Sahrens 
27443d7072f8Seschrock 	for (i = 1; i < argc; i++) {
2745573ca77eSGeorge Wilson 		if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
27463d7072f8Seschrock 			if (newstate != VDEV_STATE_HEALTHY) {
27473d7072f8Seschrock 				(void) printf(gettext("warning: device '%s' "
27483d7072f8Seschrock 				    "onlined, but remains in faulted state\n"),
27493d7072f8Seschrock 				    argv[i]);
27503d7072f8Seschrock 				if (newstate == VDEV_STATE_FAULTED)
27513d7072f8Seschrock 					(void) printf(gettext("use 'zpool "
27523d7072f8Seschrock 					    "clear' to restore a faulted "
27533d7072f8Seschrock 					    "device\n"));
27543d7072f8Seschrock 				else
27553d7072f8Seschrock 					(void) printf(gettext("use 'zpool "
27563d7072f8Seschrock 					    "replace' to replace devices "
27573d7072f8Seschrock 					    "that are no longer present\n"));
27583d7072f8Seschrock 			}
27593d7072f8Seschrock 		} else {
2760fa9e4066Sahrens 			ret = 1;
27613d7072f8Seschrock 		}
27623d7072f8Seschrock 	}
2763fa9e4066Sahrens 
276499653d4eSeschrock 	zpool_close(zhp);
276599653d4eSeschrock 
2766fa9e4066Sahrens 	return (ret);
2767fa9e4066Sahrens }
2768fa9e4066Sahrens 
2769fa9e4066Sahrens /*
2770441d80aaSlling  * zpool offline [-ft] <pool> <device> ...
2771fa9e4066Sahrens  *
2772fa9e4066Sahrens  *	-f	Force the device into the offline state, even if doing
2773fa9e4066Sahrens  *		so would appear to compromise pool availability.
2774fa9e4066Sahrens  *		(not supported yet)
2775fa9e4066Sahrens  *
2776fa9e4066Sahrens  *	-t	Only take the device off-line temporarily.  The offline
2777fa9e4066Sahrens  *		state will not be persistent across reboots.
2778fa9e4066Sahrens  */
2779fa9e4066Sahrens /* ARGSUSED */
2780fa9e4066Sahrens int
2781fa9e4066Sahrens zpool_do_offline(int argc, char **argv)
2782fa9e4066Sahrens {
2783fa9e4066Sahrens 	int c, i;
2784fa9e4066Sahrens 	char *poolname;
2785fa9e4066Sahrens 	zpool_handle_t *zhp;
278699653d4eSeschrock 	int ret = 0;
278799653d4eSeschrock 	boolean_t istmp = B_FALSE;
2788fa9e4066Sahrens 
2789fa9e4066Sahrens 	/* check options */
2790fa9e4066Sahrens 	while ((c = getopt(argc, argv, "ft")) != -1) {
2791fa9e4066Sahrens 		switch (c) {
2792fa9e4066Sahrens 		case 't':
279399653d4eSeschrock 			istmp = B_TRUE;
2794441d80aaSlling 			break;
2795441d80aaSlling 		case 'f':
2796fa9e4066Sahrens 		case '?':
2797fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2798fa9e4066Sahrens 			    optopt);
279999653d4eSeschrock 			usage(B_FALSE);
2800fa9e4066Sahrens 		}
2801fa9e4066Sahrens 	}
2802fa9e4066Sahrens 
2803fa9e4066Sahrens 	argc -= optind;
2804fa9e4066Sahrens 	argv += optind;
2805fa9e4066Sahrens 
2806fa9e4066Sahrens 	/* get pool name and check number of arguments */
2807fa9e4066Sahrens 	if (argc < 1) {
2808fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name\n"));
280999653d4eSeschrock 		usage(B_FALSE);
2810fa9e4066Sahrens 	}
2811fa9e4066Sahrens 	if (argc < 2) {
2812fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing device name\n"));
281399653d4eSeschrock 		usage(B_FALSE);
2814fa9e4066Sahrens 	}
2815fa9e4066Sahrens 
2816fa9e4066Sahrens 	poolname = argv[0];
2817fa9e4066Sahrens 
281899653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2819fa9e4066Sahrens 		return (1);
2820fa9e4066Sahrens 
28213d7072f8Seschrock 	for (i = 1; i < argc; i++) {
28223d7072f8Seschrock 		if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
2823fa9e4066Sahrens 			ret = 1;
28243d7072f8Seschrock 	}
2825fa9e4066Sahrens 
282699653d4eSeschrock 	zpool_close(zhp);
282799653d4eSeschrock 
2828fa9e4066Sahrens 	return (ret);
2829fa9e4066Sahrens }
2830fa9e4066Sahrens 
2831ea8dc4b6Seschrock /*
2832ea8dc4b6Seschrock  * zpool clear <pool> [device]
2833ea8dc4b6Seschrock  *
2834ea8dc4b6Seschrock  * Clear all errors associated with a pool or a particular device.
2835ea8dc4b6Seschrock  */
2836ea8dc4b6Seschrock int
2837ea8dc4b6Seschrock zpool_do_clear(int argc, char **argv)
2838ea8dc4b6Seschrock {
2839*468c413aSTim Haley 	int c;
2840ea8dc4b6Seschrock 	int ret = 0;
2841*468c413aSTim Haley 	boolean_t dryrun = B_FALSE;
2842*468c413aSTim Haley 	boolean_t do_rewind = B_FALSE;
2843*468c413aSTim Haley 	boolean_t xtreme_rewind = B_FALSE;
2844*468c413aSTim Haley 	uint32_t rewind_policy = ZPOOL_NO_REWIND;
2845*468c413aSTim Haley 	nvlist_t *policy = NULL;
2846ea8dc4b6Seschrock 	zpool_handle_t *zhp;
2847ea8dc4b6Seschrock 	char *pool, *device;
2848ea8dc4b6Seschrock 
2849*468c413aSTim Haley 	/* check options */
2850*468c413aSTim Haley 	while ((c = getopt(argc, argv, "FnX")) != -1) {
2851*468c413aSTim Haley 		switch (c) {
2852*468c413aSTim Haley 		case 'F':
2853*468c413aSTim Haley 			do_rewind = B_TRUE;
2854*468c413aSTim Haley 			break;
2855*468c413aSTim Haley 		case 'n':
2856*468c413aSTim Haley 			dryrun = B_TRUE;
2857*468c413aSTim Haley 			break;
2858*468c413aSTim Haley 		case 'X':
2859*468c413aSTim Haley 			xtreme_rewind = B_TRUE;
2860*468c413aSTim Haley 			break;
2861*468c413aSTim Haley 		case '?':
2862*468c413aSTim Haley 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2863*468c413aSTim Haley 			    optopt);
2864*468c413aSTim Haley 			usage(B_FALSE);
2865*468c413aSTim Haley 		}
2866*468c413aSTim Haley 	}
2867*468c413aSTim Haley 
2868*468c413aSTim Haley 	argc -= optind;
2869*468c413aSTim Haley 	argv += optind;
2870*468c413aSTim Haley 
2871*468c413aSTim Haley 	if (argc < 1) {
2872ea8dc4b6Seschrock 		(void) fprintf(stderr, gettext("missing pool name\n"));
287399653d4eSeschrock 		usage(B_FALSE);
2874ea8dc4b6Seschrock 	}
2875ea8dc4b6Seschrock 
2876*468c413aSTim Haley 	if (argc > 2) {
2877ea8dc4b6Seschrock 		(void) fprintf(stderr, gettext("too many arguments\n"));
287899653d4eSeschrock 		usage(B_FALSE);
2879ea8dc4b6Seschrock 	}
2880ea8dc4b6Seschrock 
2881*468c413aSTim Haley 	if ((dryrun || xtreme_rewind) && !do_rewind) {
2882*468c413aSTim Haley 		(void) fprintf(stderr,
2883*468c413aSTim Haley 		    gettext("-n or -X only meaningful with -F\n"));
2884*468c413aSTim Haley 		usage(B_FALSE);
2885*468c413aSTim Haley 	}
2886*468c413aSTim Haley 	if (dryrun)
2887*468c413aSTim Haley 		rewind_policy = ZPOOL_TRY_REWIND;
2888*468c413aSTim Haley 	else if (do_rewind)
2889*468c413aSTim Haley 		rewind_policy = ZPOOL_DO_REWIND;
2890*468c413aSTim Haley 	if (xtreme_rewind)
2891*468c413aSTim Haley 		rewind_policy |= ZPOOL_EXTREME_REWIND;
2892*468c413aSTim Haley 
2893*468c413aSTim Haley 	/* In future, further rewind policy choices can be passed along here */
2894*468c413aSTim Haley 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2895*468c413aSTim Haley 	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2896*468c413aSTim Haley 		return (1);
2897*468c413aSTim Haley 
2898*468c413aSTim Haley 	pool = argv[0];
2899*468c413aSTim Haley 	device = argc == 2 ? argv[1] : NULL;
2900ea8dc4b6Seschrock 
2901*468c413aSTim Haley 	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
2902*468c413aSTim Haley 		nvlist_free(policy);
2903ea8dc4b6Seschrock 		return (1);
2904*468c413aSTim Haley 	}
2905ea8dc4b6Seschrock 
2906*468c413aSTim Haley 	if (zpool_clear(zhp, device, policy) != 0)
2907ea8dc4b6Seschrock 		ret = 1;
2908ea8dc4b6Seschrock 
2909ea8dc4b6Seschrock 	zpool_close(zhp);
2910ea8dc4b6Seschrock 
2911*468c413aSTim Haley 	nvlist_free(policy);
2912*468c413aSTim Haley 
2913ea8dc4b6Seschrock 	return (ret);
2914ea8dc4b6Seschrock }
2915ea8dc4b6Seschrock 
2916fa9e4066Sahrens typedef struct scrub_cbdata {
2917fa9e4066Sahrens 	int	cb_type;
291806eeb2adSek 	int	cb_argc;
291906eeb2adSek 	char	**cb_argv;
2920fa9e4066Sahrens } scrub_cbdata_t;
2921fa9e4066Sahrens 
2922fa9e4066Sahrens int
2923fa9e4066Sahrens scrub_callback(zpool_handle_t *zhp, void *data)
2924fa9e4066Sahrens {
2925fa9e4066Sahrens 	scrub_cbdata_t *cb = data;
292606eeb2adSek 	int err;
2927fa9e4066Sahrens 
2928ea8dc4b6Seschrock 	/*
2929ea8dc4b6Seschrock 	 * Ignore faulted pools.
2930ea8dc4b6Seschrock 	 */
2931ea8dc4b6Seschrock 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
2932ea8dc4b6Seschrock 		(void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
2933ea8dc4b6Seschrock 		    "currently unavailable\n"), zpool_get_name(zhp));
2934ea8dc4b6Seschrock 		return (1);
2935ea8dc4b6Seschrock 	}
2936ea8dc4b6Seschrock 
293706eeb2adSek 	err = zpool_scrub(zhp, cb->cb_type);
293806eeb2adSek 
293906eeb2adSek 	return (err != 0);
2940fa9e4066Sahrens }
2941fa9e4066Sahrens 
2942fa9e4066Sahrens /*
2943fa9e4066Sahrens  * zpool scrub [-s] <pool> ...
2944fa9e4066Sahrens  *
2945fa9e4066Sahrens  *	-s	Stop.  Stops any in-progress scrub.
2946fa9e4066Sahrens  */
2947fa9e4066Sahrens int
2948fa9e4066Sahrens zpool_do_scrub(int argc, char **argv)
2949fa9e4066Sahrens {
2950fa9e4066Sahrens 	int c;
2951fa9e4066Sahrens 	scrub_cbdata_t cb;
2952fa9e4066Sahrens 
2953fa9e4066Sahrens 	cb.cb_type = POOL_SCRUB_EVERYTHING;
2954fa9e4066Sahrens 
2955fa9e4066Sahrens 	/* check options */
2956fa9e4066Sahrens 	while ((c = getopt(argc, argv, "s")) != -1) {
2957fa9e4066Sahrens 		switch (c) {
2958fa9e4066Sahrens 		case 's':
2959fa9e4066Sahrens 			cb.cb_type = POOL_SCRUB_NONE;
2960fa9e4066Sahrens 			break;
2961fa9e4066Sahrens 		case '?':
2962fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2963fa9e4066Sahrens 			    optopt);
296499653d4eSeschrock 			usage(B_FALSE);
2965fa9e4066Sahrens 		}
2966fa9e4066Sahrens 	}
2967fa9e4066Sahrens 
296806eeb2adSek 	cb.cb_argc = argc;
296906eeb2adSek 	cb.cb_argv = argv;
2970fa9e4066Sahrens 	argc -= optind;
2971fa9e4066Sahrens 	argv += optind;
2972fa9e4066Sahrens 
2973fa9e4066Sahrens 	if (argc < 1) {
2974fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
297599653d4eSeschrock 		usage(B_FALSE);
2976fa9e4066Sahrens 	}
2977fa9e4066Sahrens 
2978b1b8ab34Slling 	return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
2979fa9e4066Sahrens }
2980fa9e4066Sahrens 
2981fa9e4066Sahrens typedef struct status_cbdata {
298299653d4eSeschrock 	int		cb_count;
2983e9dbad6fSeschrock 	boolean_t	cb_allpools;
298499653d4eSeschrock 	boolean_t	cb_verbose;
298599653d4eSeschrock 	boolean_t	cb_explain;
298699653d4eSeschrock 	boolean_t	cb_first;
2987fa9e4066Sahrens } status_cbdata_t;
2988fa9e4066Sahrens 
2989fa9e4066Sahrens /*
2990fa9e4066Sahrens  * Print out detailed scrub status.
2991fa9e4066Sahrens  */
2992fa9e4066Sahrens void
2993fa9e4066Sahrens print_scrub_status(nvlist_t *nvroot)
2994fa9e4066Sahrens {
2995fa9e4066Sahrens 	vdev_stat_t *vs;
2996fa9e4066Sahrens 	uint_t vsc;
2997fa9e4066Sahrens 	time_t start, end, now;
2998fa9e4066Sahrens 	double fraction_done;
299918ce54dfSek 	uint64_t examined, total, minutes_left, minutes_taken;
3000fa9e4066Sahrens 	char *scrub_type;
3001fa9e4066Sahrens 
3002fa9e4066Sahrens 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
3003fa9e4066Sahrens 	    (uint64_t **)&vs, &vsc) == 0);
3004fa9e4066Sahrens 
3005fa9e4066Sahrens 	/*
3006fa9e4066Sahrens 	 * If there's never been a scrub, there's not much to say.
3007fa9e4066Sahrens 	 */
3008fa9e4066Sahrens 	if (vs->vs_scrub_end == 0 && vs->vs_scrub_type == POOL_SCRUB_NONE) {
3009fa9e4066Sahrens 		(void) printf(gettext("none requested\n"));
3010fa9e4066Sahrens 		return;
3011fa9e4066Sahrens 	}
3012fa9e4066Sahrens 
3013fa9e4066Sahrens 	scrub_type = (vs->vs_scrub_type == POOL_SCRUB_RESILVER) ?
3014fa9e4066Sahrens 	    "resilver" : "scrub";
3015fa9e4066Sahrens 
3016fa9e4066Sahrens 	start = vs->vs_scrub_start;
3017fa9e4066Sahrens 	end = vs->vs_scrub_end;
3018fa9e4066Sahrens 	now = time(NULL);
3019fa9e4066Sahrens 	examined = vs->vs_scrub_examined;
3020fa9e4066Sahrens 	total = vs->vs_alloc;
3021fa9e4066Sahrens 
3022fa9e4066Sahrens 	if (end != 0) {
302318ce54dfSek 		minutes_taken = (uint64_t)((end - start) / 60);
302418ce54dfSek 
302518ce54dfSek 		(void) printf(gettext("%s %s after %lluh%um with %llu errors "
302618ce54dfSek 		    "on %s"),
3027fa9e4066Sahrens 		    scrub_type, vs->vs_scrub_complete ? "completed" : "stopped",
302818ce54dfSek 		    (u_longlong_t)(minutes_taken / 60),
302918ce54dfSek 		    (uint_t)(minutes_taken % 60),
3030fa9e4066Sahrens 		    (u_longlong_t)vs->vs_scrub_errors, ctime(&end));
3031fa9e4066Sahrens 		return;
3032fa9e4066Sahrens 	}
3033fa9e4066Sahrens 
3034fa9e4066Sahrens 	if (examined == 0)
3035fa9e4066Sahrens 		examined = 1;
3036fa9e4066Sahrens 	if (examined > total)
3037fa9e4066Sahrens 		total = examined;
3038fa9e4066Sahrens 
3039fa9e4066Sahrens 	fraction_done = (double)examined / total;
3040fa9e4066Sahrens 	minutes_left = (uint64_t)((now - start) *
3041fa9e4066Sahrens 	    (1 - fraction_done) / fraction_done / 60);
304218ce54dfSek 	minutes_taken = (uint64_t)((now - start) / 60);
3043fa9e4066Sahrens 
304418ce54dfSek 	(void) printf(gettext("%s in progress for %lluh%um, %.2f%% done, "
304518ce54dfSek 	    "%lluh%um to go\n"),
304618ce54dfSek 	    scrub_type, (u_longlong_t)(minutes_taken / 60),
304718ce54dfSek 	    (uint_t)(minutes_taken % 60), 100 * fraction_done,
3048fa9e4066Sahrens 	    (u_longlong_t)(minutes_left / 60), (uint_t)(minutes_left % 60));
3049fa9e4066Sahrens }
3050fa9e4066Sahrens 
3051ea8dc4b6Seschrock static void
3052ea8dc4b6Seschrock print_error_log(zpool_handle_t *zhp)
3053ea8dc4b6Seschrock {
305475519f38Sek 	nvlist_t *nverrlist = NULL;
305555434c77Sek 	nvpair_t *elem;
305655434c77Sek 	char *pathname;
305755434c77Sek 	size_t len = MAXPATHLEN * 2;
3058ea8dc4b6Seschrock 
305955434c77Sek 	if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3060ea8dc4b6Seschrock 		(void) printf("errors: List of errors unavailable "
3061ea8dc4b6Seschrock 		    "(insufficient privileges)\n");
3062ea8dc4b6Seschrock 		return;
3063ea8dc4b6Seschrock 	}
3064ea8dc4b6Seschrock 
306555434c77Sek 	(void) printf("errors: Permanent errors have been "
306655434c77Sek 	    "detected in the following files:\n\n");
3067ea8dc4b6Seschrock 
306855434c77Sek 	pathname = safe_malloc(len);
306955434c77Sek 	elem = NULL;
307055434c77Sek 	while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
307155434c77Sek 		nvlist_t *nv;
307255434c77Sek 		uint64_t dsobj, obj;
307355434c77Sek 
307455434c77Sek 		verify(nvpair_value_nvlist(elem, &nv) == 0);
307555434c77Sek 		verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
307655434c77Sek 		    &dsobj) == 0);
307755434c77Sek 		verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
307855434c77Sek 		    &obj) == 0);
307955434c77Sek 		zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
308055434c77Sek 		(void) printf("%7s %s\n", "", pathname);
308155434c77Sek 	}
308255434c77Sek 	free(pathname);
308355434c77Sek 	nvlist_free(nverrlist);
3084ea8dc4b6Seschrock }
3085ea8dc4b6Seschrock 
308699653d4eSeschrock static void
308799653d4eSeschrock print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
308899653d4eSeschrock     int namewidth)
308999653d4eSeschrock {
309099653d4eSeschrock 	uint_t i;
309199653d4eSeschrock 	char *name;
309299653d4eSeschrock 
309399653d4eSeschrock 	if (nspares == 0)
309499653d4eSeschrock 		return;
309599653d4eSeschrock 
309699653d4eSeschrock 	(void) printf(gettext("\tspares\n"));
309799653d4eSeschrock 
309899653d4eSeschrock 	for (i = 0; i < nspares; i++) {
309988ecc943SGeorge Wilson 		name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
310099653d4eSeschrock 		print_status_config(zhp, name, spares[i],
3101aa8cf21aSNeil Perrin 		    namewidth, 2, B_TRUE);
310299653d4eSeschrock 		free(name);
310399653d4eSeschrock 	}
310499653d4eSeschrock }
310599653d4eSeschrock 
3106fa94a07fSbrendan static void
3107fa94a07fSbrendan print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3108fa94a07fSbrendan     int namewidth)
3109fa94a07fSbrendan {
3110fa94a07fSbrendan 	uint_t i;
3111fa94a07fSbrendan 	char *name;
3112fa94a07fSbrendan 
3113fa94a07fSbrendan 	if (nl2cache == 0)
3114fa94a07fSbrendan 		return;
3115fa94a07fSbrendan 
3116fa94a07fSbrendan 	(void) printf(gettext("\tcache\n"));
3117fa94a07fSbrendan 
3118fa94a07fSbrendan 	for (i = 0; i < nl2cache; i++) {
311988ecc943SGeorge Wilson 		name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3120fa94a07fSbrendan 		print_status_config(zhp, name, l2cache[i],
3121aa8cf21aSNeil Perrin 		    namewidth, 2, B_FALSE);
3122aa8cf21aSNeil Perrin 		free(name);
3123aa8cf21aSNeil Perrin 	}
3124aa8cf21aSNeil Perrin }
3125aa8cf21aSNeil Perrin 
3126fa9e4066Sahrens /*
3127fa9e4066Sahrens  * Display a summary of pool status.  Displays a summary such as:
3128fa9e4066Sahrens  *
3129fa9e4066Sahrens  *        pool: tank
3130fa9e4066Sahrens  *	status: DEGRADED
3131fa9e4066Sahrens  *	reason: One or more devices ...
3132fa9e4066Sahrens  *         see: http://www.sun.com/msg/ZFS-xxxx-01
3133fa9e4066Sahrens  *	config:
3134fa9e4066Sahrens  *		mirror		DEGRADED
3135fa9e4066Sahrens  *                c1t0d0	OK
3136ea8dc4b6Seschrock  *                c2t0d0	UNAVAIL
3137fa9e4066Sahrens  *
3138fa9e4066Sahrens  * When given the '-v' option, we print out the complete config.  If the '-e'
3139fa9e4066Sahrens  * option is specified, then we print out error rate information as well.
3140fa9e4066Sahrens  */
3141fa9e4066Sahrens int
3142fa9e4066Sahrens status_callback(zpool_handle_t *zhp, void *data)
3143fa9e4066Sahrens {
3144fa9e4066Sahrens 	status_cbdata_t *cbp = data;
3145fa9e4066Sahrens 	nvlist_t *config, *nvroot;
3146fa9e4066Sahrens 	char *msgid;
3147fa9e4066Sahrens 	int reason;
314846657f8dSmmusante 	const char *health;
314946657f8dSmmusante 	uint_t c;
315046657f8dSmmusante 	vdev_stat_t *vs;
3151fa9e4066Sahrens 
3152088e9d47Seschrock 	config = zpool_get_config(zhp, NULL);
3153fa9e4066Sahrens 	reason = zpool_get_status(zhp, &msgid);
3154fa9e4066Sahrens 
3155fa9e4066Sahrens 	cbp->cb_count++;
3156fa9e4066Sahrens 
3157fa9e4066Sahrens 	/*
3158fa9e4066Sahrens 	 * If we were given 'zpool status -x', only report those pools with
3159fa9e4066Sahrens 	 * problems.
3160fa9e4066Sahrens 	 */
3161e9dbad6fSeschrock 	if (reason == ZPOOL_STATUS_OK && cbp->cb_explain) {
3162e9dbad6fSeschrock 		if (!cbp->cb_allpools) {
3163e9dbad6fSeschrock 			(void) printf(gettext("pool '%s' is healthy\n"),
3164e9dbad6fSeschrock 			    zpool_get_name(zhp));
3165e9dbad6fSeschrock 			if (cbp->cb_first)
3166e9dbad6fSeschrock 				cbp->cb_first = B_FALSE;
3167e9dbad6fSeschrock 		}
3168fa9e4066Sahrens 		return (0);
3169e9dbad6fSeschrock 	}
3170fa9e4066Sahrens 
3171fa9e4066Sahrens 	if (cbp->cb_first)
317299653d4eSeschrock 		cbp->cb_first = B_FALSE;
3173fa9e4066Sahrens 	else
3174fa9e4066Sahrens 		(void) printf("\n");
3175fa9e4066Sahrens 
317646657f8dSmmusante 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
317746657f8dSmmusante 	    &nvroot) == 0);
317846657f8dSmmusante 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
317946657f8dSmmusante 	    (uint64_t **)&vs, &c) == 0);
3180990b4856Slling 	health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
3181fa9e4066Sahrens 
3182fa9e4066Sahrens 	(void) printf(gettext("  pool: %s\n"), zpool_get_name(zhp));
3183fa9e4066Sahrens 	(void) printf(gettext(" state: %s\n"), health);
3184fa9e4066Sahrens 
3185fa9e4066Sahrens 	switch (reason) {
3186fa9e4066Sahrens 	case ZPOOL_STATUS_MISSING_DEV_R:
3187fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices could not "
3188fa9e4066Sahrens 		    "be opened.  Sufficient replicas exist for\n\tthe pool to "
3189fa9e4066Sahrens 		    "continue functioning in a degraded state.\n"));
3190fa9e4066Sahrens 		(void) printf(gettext("action: Attach the missing device and "
3191fa9e4066Sahrens 		    "online it using 'zpool online'.\n"));
3192fa9e4066Sahrens 		break;
3193fa9e4066Sahrens 
3194fa9e4066Sahrens 	case ZPOOL_STATUS_MISSING_DEV_NR:
3195fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices could not "
3196fa9e4066Sahrens 		    "be opened.  There are insufficient\n\treplicas for the "
3197fa9e4066Sahrens 		    "pool to continue functioning.\n"));
3198fa9e4066Sahrens 		(void) printf(gettext("action: Attach the missing device and "
3199fa9e4066Sahrens 		    "online it using 'zpool online'.\n"));
3200fa9e4066Sahrens 		break;
3201fa9e4066Sahrens 
3202fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_LABEL_R:
3203fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices could not "
3204fa9e4066Sahrens 		    "be used because the label is missing or\n\tinvalid.  "
3205fa9e4066Sahrens 		    "Sufficient replicas exist for the pool to continue\n\t"
3206fa9e4066Sahrens 		    "functioning in a degraded state.\n"));
3207fa9e4066Sahrens 		(void) printf(gettext("action: Replace the device using "
3208fa9e4066Sahrens 		    "'zpool replace'.\n"));
3209fa9e4066Sahrens 		break;
3210fa9e4066Sahrens 
3211fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_LABEL_NR:
3212fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices could not "
3213b1b8ab34Slling 		    "be used because the label is missing \n\tor invalid.  "
3214fa9e4066Sahrens 		    "There are insufficient replicas for the pool to "
3215fa9e4066Sahrens 		    "continue\n\tfunctioning.\n"));
3216*468c413aSTim Haley 		zpool_explain_recover(zpool_get_handle(zhp),
3217*468c413aSTim Haley 		    zpool_get_name(zhp), reason, config);
3218fa9e4066Sahrens 		break;
3219fa9e4066Sahrens 
3220fa9e4066Sahrens 	case ZPOOL_STATUS_FAILING_DEV:
3221fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices has "
3222fa9e4066Sahrens 		    "experienced an unrecoverable error.  An\n\tattempt was "
3223fa9e4066Sahrens 		    "made to correct the error.  Applications are "
3224fa9e4066Sahrens 		    "unaffected.\n"));
3225fa9e4066Sahrens 		(void) printf(gettext("action: Determine if the device needs "
3226fa9e4066Sahrens 		    "to be replaced, and clear the errors\n\tusing "
3227ea8dc4b6Seschrock 		    "'zpool clear' or replace the device with 'zpool "
3228fa9e4066Sahrens 		    "replace'.\n"));
3229fa9e4066Sahrens 		break;
3230fa9e4066Sahrens 
3231fa9e4066Sahrens 	case ZPOOL_STATUS_OFFLINE_DEV:
3232fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices has "
3233d7d4af51Smmusante 		    "been taken offline by the administrator.\n\tSufficient "
3234fa9e4066Sahrens 		    "replicas exist for the pool to continue functioning in "
3235fa9e4066Sahrens 		    "a\n\tdegraded state.\n"));
3236fa9e4066Sahrens 		(void) printf(gettext("action: Online the device using "
3237fa9e4066Sahrens 		    "'zpool online' or replace the device with\n\t'zpool "
3238fa9e4066Sahrens 		    "replace'.\n"));
3239fa9e4066Sahrens 		break;
3240fa9e4066Sahrens 
3241c25309d4SGeorge Wilson 	case ZPOOL_STATUS_REMOVED_DEV:
3242c25309d4SGeorge Wilson 		(void) printf(gettext("status: One or more devices has "
3243c25309d4SGeorge Wilson 		    "been removed by the administrator.\n\tSufficient "
3244c25309d4SGeorge Wilson 		    "replicas exist for the pool to continue functioning in "
3245c25309d4SGeorge Wilson 		    "a\n\tdegraded state.\n"));
3246c25309d4SGeorge Wilson 		(void) printf(gettext("action: Online the device using "
3247c25309d4SGeorge Wilson 		    "'zpool online' or replace the device with\n\t'zpool "
3248c25309d4SGeorge Wilson 		    "replace'.\n"));
3249c25309d4SGeorge Wilson 		break;
3250c25309d4SGeorge Wilson 
3251c25309d4SGeorge Wilson 
3252fa9e4066Sahrens 	case ZPOOL_STATUS_RESILVERING:
3253fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices is "
3254fa9e4066Sahrens 		    "currently being resilvered.  The pool will\n\tcontinue "
3255fa9e4066Sahrens 		    "to function, possibly in a degraded state.\n"));
3256fa9e4066Sahrens 		(void) printf(gettext("action: Wait for the resilver to "
3257fa9e4066Sahrens 		    "complete.\n"));
3258fa9e4066Sahrens 		break;
3259fa9e4066Sahrens 
3260ea8dc4b6Seschrock 	case ZPOOL_STATUS_CORRUPT_DATA:
3261ea8dc4b6Seschrock 		(void) printf(gettext("status: One or more devices has "
3262ea8dc4b6Seschrock 		    "experienced an error resulting in data\n\tcorruption.  "
3263ea8dc4b6Seschrock 		    "Applications may be affected.\n"));
3264ea8dc4b6Seschrock 		(void) printf(gettext("action: Restore the file in question "
3265ea8dc4b6Seschrock 		    "if possible.  Otherwise restore the\n\tentire pool from "
3266ea8dc4b6Seschrock 		    "backup.\n"));
3267ea8dc4b6Seschrock 		break;
3268ea8dc4b6Seschrock 
3269ea8dc4b6Seschrock 	case ZPOOL_STATUS_CORRUPT_POOL:
3270ea8dc4b6Seschrock 		(void) printf(gettext("status: The pool metadata is corrupted "
3271ea8dc4b6Seschrock 		    "and the pool cannot be opened.\n"));
3272*468c413aSTim Haley 		zpool_explain_recover(zpool_get_handle(zhp),
3273*468c413aSTim Haley 		    zpool_get_name(zhp), reason, config);
3274ea8dc4b6Seschrock 		break;
3275ea8dc4b6Seschrock 
3276eaca9bbdSeschrock 	case ZPOOL_STATUS_VERSION_OLDER:
3277eaca9bbdSeschrock 		(void) printf(gettext("status: The pool is formatted using an "
3278eaca9bbdSeschrock 		    "older on-disk format.  The pool can\n\tstill be used, but "
3279eaca9bbdSeschrock 		    "some features are unavailable.\n"));
3280eaca9bbdSeschrock 		(void) printf(gettext("action: Upgrade the pool using 'zpool "
3281eaca9bbdSeschrock 		    "upgrade'.  Once this is done, the\n\tpool will no longer "
3282eaca9bbdSeschrock 		    "be accessible on older software versions.\n"));
3283eaca9bbdSeschrock 		break;
3284eaca9bbdSeschrock 
3285eaca9bbdSeschrock 	case ZPOOL_STATUS_VERSION_NEWER:
3286eaca9bbdSeschrock 		(void) printf(gettext("status: The pool has been upgraded to a "
3287eaca9bbdSeschrock 		    "newer, incompatible on-disk version.\n\tThe pool cannot "
3288eaca9bbdSeschrock 		    "be accessed on this system.\n"));
3289eaca9bbdSeschrock 		(void) printf(gettext("action: Access the pool from a system "
3290eaca9bbdSeschrock 		    "running more recent software, or\n\trestore the pool from "
3291eaca9bbdSeschrock 		    "backup.\n"));
3292eaca9bbdSeschrock 		break;
3293eaca9bbdSeschrock 
32943d7072f8Seschrock 	case ZPOOL_STATUS_FAULTED_DEV_R:
32953d7072f8Seschrock 		(void) printf(gettext("status: One or more devices are "
32963d7072f8Seschrock 		    "faulted in response to persistent errors.\n\tSufficient "
32973d7072f8Seschrock 		    "replicas exist for the pool to continue functioning "
32983d7072f8Seschrock 		    "in a\n\tdegraded state.\n"));
32993d7072f8Seschrock 		(void) printf(gettext("action: Replace the faulted device, "
33003d7072f8Seschrock 		    "or use 'zpool clear' to mark the device\n\trepaired.\n"));
33013d7072f8Seschrock 		break;
33023d7072f8Seschrock 
33033d7072f8Seschrock 	case ZPOOL_STATUS_FAULTED_DEV_NR:
33043d7072f8Seschrock 		(void) printf(gettext("status: One or more devices are "
33053d7072f8Seschrock 		    "faulted in response to persistent errors.  There are "
33063d7072f8Seschrock 		    "insufficient replicas for the pool to\n\tcontinue "
33073d7072f8Seschrock 		    "functioning.\n"));
33083d7072f8Seschrock 		(void) printf(gettext("action: Destroy and re-create the pool "
33093d7072f8Seschrock 		    "from a backup source.  Manually marking the device\n"
33103d7072f8Seschrock 		    "\trepaired using 'zpool clear' may allow some data "
33113d7072f8Seschrock 		    "to be recovered.\n"));
33123d7072f8Seschrock 		break;
33133d7072f8Seschrock 
331432b87932Sek 	case ZPOOL_STATUS_IO_FAILURE_WAIT:
331532b87932Sek 	case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
331632b87932Sek 		(void) printf(gettext("status: One or more devices are "
33178a79c1b5Sek 		    "faulted in response to IO failures.\n"));
331832b87932Sek 		(void) printf(gettext("action: Make sure the affected devices "
331932b87932Sek 		    "are connected, then run 'zpool clear'.\n"));
332032b87932Sek 		break;
332132b87932Sek 
3322b87f3af3Sperrin 	case ZPOOL_STATUS_BAD_LOG:
3323b87f3af3Sperrin 		(void) printf(gettext("status: An intent log record "
3324b87f3af3Sperrin 		    "could not be read.\n"
3325b87f3af3Sperrin 		    "\tWaiting for adminstrator intervention to fix the "
3326b87f3af3Sperrin 		    "faulted pool.\n"));
3327b87f3af3Sperrin 		(void) printf(gettext("action: Either restore the affected "
3328b87f3af3Sperrin 		    "device(s) and run 'zpool online',\n"
3329b87f3af3Sperrin 		    "\tor ignore the intent log records by running "
3330b87f3af3Sperrin 		    "'zpool clear'.\n"));
3331b87f3af3Sperrin 		break;
3332b87f3af3Sperrin 
3333fa9e4066Sahrens 	default:
3334fa9e4066Sahrens 		/*
3335fa9e4066Sahrens 		 * The remaining errors can't actually be generated, yet.
3336fa9e4066Sahrens 		 */
3337fa9e4066Sahrens 		assert(reason == ZPOOL_STATUS_OK);
3338fa9e4066Sahrens 	}
3339fa9e4066Sahrens 
3340fa9e4066Sahrens 	if (msgid != NULL)
3341fa9e4066Sahrens 		(void) printf(gettext("   see: http://www.sun.com/msg/%s\n"),
3342fa9e4066Sahrens 		    msgid);
3343fa9e4066Sahrens 
3344fa9e4066Sahrens 	if (config != NULL) {
3345fa9e4066Sahrens 		int namewidth;
3346ea8dc4b6Seschrock 		uint64_t nerr;
3347fa94a07fSbrendan 		nvlist_t **spares, **l2cache;
3348fa94a07fSbrendan 		uint_t nspares, nl2cache;
3349fa9e4066Sahrens 
3350fa9e4066Sahrens 
3351fa9e4066Sahrens 		(void) printf(gettext(" scrub: "));
3352fa9e4066Sahrens 		print_scrub_status(nvroot);
3353fa9e4066Sahrens 
3354c67d9675Seschrock 		namewidth = max_width(zhp, nvroot, 0, 0);
3355fa9e4066Sahrens 		if (namewidth < 10)
3356fa9e4066Sahrens 			namewidth = 10;
3357fa9e4066Sahrens 
3358fa9e4066Sahrens 		(void) printf(gettext("config:\n\n"));
3359fa9e4066Sahrens 		(void) printf(gettext("\t%-*s  %-8s %5s %5s %5s\n"), namewidth,
3360fa9e4066Sahrens 		    "NAME", "STATE", "READ", "WRITE", "CKSUM");
3361c67d9675Seschrock 		print_status_config(zhp, zpool_get_name(zhp), nvroot,
3362aa8cf21aSNeil Perrin 		    namewidth, 0, B_FALSE);
336399653d4eSeschrock 
33644dea40f0SNeil Perrin 		if (num_logs(nvroot) > 0)
3365e6ca193dSGeorge Wilson 			print_logs(zhp, nvroot, namewidth, B_TRUE);
3366fa94a07fSbrendan 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3367fa94a07fSbrendan 		    &l2cache, &nl2cache) == 0)
3368fa94a07fSbrendan 			print_l2cache(zhp, l2cache, nl2cache, namewidth);
3369fa94a07fSbrendan 
337099653d4eSeschrock 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
337199653d4eSeschrock 		    &spares, &nspares) == 0)
337299653d4eSeschrock 			print_spares(zhp, spares, nspares, namewidth);
3373ea8dc4b6Seschrock 
3374ea8dc4b6Seschrock 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
3375ea8dc4b6Seschrock 		    &nerr) == 0) {
337655434c77Sek 			nvlist_t *nverrlist = NULL;
337755434c77Sek 
3378ea8dc4b6Seschrock 			/*
3379ea8dc4b6Seschrock 			 * If the approximate error count is small, get a
3380ea8dc4b6Seschrock 			 * precise count by fetching the entire log and
3381ea8dc4b6Seschrock 			 * uniquifying the results.
3382ea8dc4b6Seschrock 			 */
338375519f38Sek 			if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
338455434c77Sek 			    zpool_get_errlog(zhp, &nverrlist) == 0) {
338555434c77Sek 				nvpair_t *elem;
338655434c77Sek 
338755434c77Sek 				elem = NULL;
338855434c77Sek 				nerr = 0;
338955434c77Sek 				while ((elem = nvlist_next_nvpair(nverrlist,
339055434c77Sek 				    elem)) != NULL) {
339155434c77Sek 					nerr++;
339255434c77Sek 				}
339355434c77Sek 			}
339455434c77Sek 			nvlist_free(nverrlist);
3395ea8dc4b6Seschrock 
3396ea8dc4b6Seschrock 			(void) printf("\n");
339799653d4eSeschrock 
3398ea8dc4b6Seschrock 			if (nerr == 0)
3399ea8dc4b6Seschrock 				(void) printf(gettext("errors: No known data "
3400ea8dc4b6Seschrock 				    "errors\n"));
3401ea8dc4b6Seschrock 			else if (!cbp->cb_verbose)
3402e9dbad6fSeschrock 				(void) printf(gettext("errors: %llu data "
34035ad82045Snd 				    "errors, use '-v' for a list\n"),
34045ad82045Snd 				    (u_longlong_t)nerr);
3405ea8dc4b6Seschrock 			else
3406ea8dc4b6Seschrock 				print_error_log(zhp);
3407ea8dc4b6Seschrock 		}
3408fa9e4066Sahrens 	} else {
3409fa9e4066Sahrens 		(void) printf(gettext("config: The configuration cannot be "
3410fa9e4066Sahrens 		    "determined.\n"));
3411fa9e4066Sahrens 	}
3412fa9e4066Sahrens 
3413fa9e4066Sahrens 	return (0);
3414fa9e4066Sahrens }
3415fa9e4066Sahrens 
3416fa9e4066Sahrens /*
3417fa9e4066Sahrens  * zpool status [-vx] [pool] ...
3418fa9e4066Sahrens  *
3419fa9e4066Sahrens  *	-v	Display complete error logs
3420fa9e4066Sahrens  *	-x	Display only pools with potential problems
3421fa9e4066Sahrens  *
3422fa9e4066Sahrens  * Describes the health status of all pools or some subset.
3423fa9e4066Sahrens  */
3424fa9e4066Sahrens int
3425fa9e4066Sahrens zpool_do_status(int argc, char **argv)
3426fa9e4066Sahrens {
3427fa9e4066Sahrens 	int c;
3428fa9e4066Sahrens 	int ret;
3429fa9e4066Sahrens 	status_cbdata_t cb = { 0 };
3430fa9e4066Sahrens 
3431fa9e4066Sahrens 	/* check options */
3432fa9e4066Sahrens 	while ((c = getopt(argc, argv, "vx")) != -1) {
3433fa9e4066Sahrens 		switch (c) {
3434fa9e4066Sahrens 		case 'v':
343599653d4eSeschrock 			cb.cb_verbose = B_TRUE;
3436fa9e4066Sahrens 			break;
3437fa9e4066Sahrens 		case 'x':
343899653d4eSeschrock 			cb.cb_explain = B_TRUE;
3439fa9e4066Sahrens 			break;
3440fa9e4066Sahrens 		case '?':
3441fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3442fa9e4066Sahrens 			    optopt);
344399653d4eSeschrock 			usage(B_FALSE);
3444fa9e4066Sahrens 		}
3445fa9e4066Sahrens 	}
3446fa9e4066Sahrens 
3447fa9e4066Sahrens 	argc -= optind;
3448fa9e4066Sahrens 	argv += optind;
3449fa9e4066Sahrens 
345099653d4eSeschrock 	cb.cb_first = B_TRUE;
3451fa9e4066Sahrens 
3452e9dbad6fSeschrock 	if (argc == 0)
3453e9dbad6fSeschrock 		cb.cb_allpools = B_TRUE;
3454e9dbad6fSeschrock 
3455b1b8ab34Slling 	ret = for_each_pool(argc, argv, B_TRUE, NULL, status_callback, &cb);
3456fa9e4066Sahrens 
3457fa9e4066Sahrens 	if (argc == 0 && cb.cb_count == 0)
3458fa9e4066Sahrens 		(void) printf(gettext("no pools available\n"));
3459e9dbad6fSeschrock 	else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
3460e9dbad6fSeschrock 		(void) printf(gettext("all pools are healthy\n"));
3461fa9e4066Sahrens 
3462fa9e4066Sahrens 	return (ret);
3463fa9e4066Sahrens }
3464fa9e4066Sahrens 
3465eaca9bbdSeschrock typedef struct upgrade_cbdata {
3466eaca9bbdSeschrock 	int	cb_all;
3467eaca9bbdSeschrock 	int	cb_first;
3468eaca9bbdSeschrock 	int	cb_newer;
346906eeb2adSek 	int	cb_argc;
3470990b4856Slling 	uint64_t cb_version;
347106eeb2adSek 	char	**cb_argv;
3472eaca9bbdSeschrock } upgrade_cbdata_t;
3473eaca9bbdSeschrock 
3474eaca9bbdSeschrock static int
3475eaca9bbdSeschrock upgrade_cb(zpool_handle_t *zhp, void *arg)
3476eaca9bbdSeschrock {
3477eaca9bbdSeschrock 	upgrade_cbdata_t *cbp = arg;
3478eaca9bbdSeschrock 	nvlist_t *config;
3479eaca9bbdSeschrock 	uint64_t version;
3480eaca9bbdSeschrock 	int ret = 0;
3481eaca9bbdSeschrock 
3482eaca9bbdSeschrock 	config = zpool_get_config(zhp, NULL);
3483eaca9bbdSeschrock 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
3484eaca9bbdSeschrock 	    &version) == 0);
3485eaca9bbdSeschrock 
3486e7437265Sahrens 	if (!cbp->cb_newer && version < SPA_VERSION) {
3487eaca9bbdSeschrock 		if (!cbp->cb_all) {
3488eaca9bbdSeschrock 			if (cbp->cb_first) {
3489eaca9bbdSeschrock 				(void) printf(gettext("The following pools are "
3490eaca9bbdSeschrock 				    "out of date, and can be upgraded.  After "
3491eaca9bbdSeschrock 				    "being\nupgraded, these pools will no "
3492eaca9bbdSeschrock 				    "longer be accessible by older software "
3493eaca9bbdSeschrock 				    "versions.\n\n"));
3494eaca9bbdSeschrock 				(void) printf(gettext("VER  POOL\n"));
3495eaca9bbdSeschrock 				(void) printf(gettext("---  ------------\n"));
349699653d4eSeschrock 				cbp->cb_first = B_FALSE;
3497eaca9bbdSeschrock 			}
3498eaca9bbdSeschrock 
34995ad82045Snd 			(void) printf("%2llu   %s\n", (u_longlong_t)version,
3500eaca9bbdSeschrock 			    zpool_get_name(zhp));
3501eaca9bbdSeschrock 		} else {
350299653d4eSeschrock 			cbp->cb_first = B_FALSE;
3503990b4856Slling 			ret = zpool_upgrade(zhp, cbp->cb_version);
350406eeb2adSek 			if (!ret) {
3505eaca9bbdSeschrock 				(void) printf(gettext("Successfully upgraded "
3506990b4856Slling 				    "'%s'\n\n"), zpool_get_name(zhp));
350706eeb2adSek 			}
3508eaca9bbdSeschrock 		}
3509e7437265Sahrens 	} else if (cbp->cb_newer && version > SPA_VERSION) {
3510eaca9bbdSeschrock 		assert(!cbp->cb_all);
3511eaca9bbdSeschrock 
3512eaca9bbdSeschrock 		if (cbp->cb_first) {
3513eaca9bbdSeschrock 			(void) printf(gettext("The following pools are "
3514eaca9bbdSeschrock 			    "formatted using a newer software version and\n"
3515eaca9bbdSeschrock 			    "cannot be accessed on the current system.\n\n"));
3516eaca9bbdSeschrock 			(void) printf(gettext("VER  POOL\n"));
3517eaca9bbdSeschrock 			(void) printf(gettext("---  ------------\n"));
351899653d4eSeschrock 			cbp->cb_first = B_FALSE;
3519eaca9bbdSeschrock 		}
3520eaca9bbdSeschrock 
35215ad82045Snd 		(void) printf("%2llu   %s\n", (u_longlong_t)version,
3522eaca9bbdSeschrock 		    zpool_get_name(zhp));
3523eaca9bbdSeschrock 	}
3524eaca9bbdSeschrock 
3525eaca9bbdSeschrock 	zpool_close(zhp);
3526eaca9bbdSeschrock 	return (ret);
3527eaca9bbdSeschrock }
3528eaca9bbdSeschrock 
3529eaca9bbdSeschrock /* ARGSUSED */
3530eaca9bbdSeschrock static int
353106eeb2adSek upgrade_one(zpool_handle_t *zhp, void *data)
3532eaca9bbdSeschrock {
3533990b4856Slling 	upgrade_cbdata_t *cbp = data;
3534990b4856Slling 	uint64_t cur_version;
3535eaca9bbdSeschrock 	int ret;
3536eaca9bbdSeschrock 
35378654d025Sperrin 	if (strcmp("log", zpool_get_name(zhp)) == 0) {
35388654d025Sperrin 		(void) printf(gettext("'log' is now a reserved word\n"
35398654d025Sperrin 		    "Pool 'log' must be renamed using export and import"
35408654d025Sperrin 		    " to upgrade.\n"));
35418654d025Sperrin 		return (1);
35428654d025Sperrin 	}
3543990b4856Slling 
3544990b4856Slling 	cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3545e6c728e1Sbrendan 	if (cur_version > cbp->cb_version) {
3546eaca9bbdSeschrock 		(void) printf(gettext("Pool '%s' is already formatted "
3547e6c728e1Sbrendan 		    "using more current version '%llu'.\n"),
3548e6c728e1Sbrendan 		    zpool_get_name(zhp), cur_version);
3549e6c728e1Sbrendan 		return (0);
3550e6c728e1Sbrendan 	}
3551e6c728e1Sbrendan 	if (cur_version == cbp->cb_version) {
3552e6c728e1Sbrendan 		(void) printf(gettext("Pool '%s' is already formatted "
3553e6c728e1Sbrendan 		    "using the current version.\n"), zpool_get_name(zhp));
3554eaca9bbdSeschrock 		return (0);
3555eaca9bbdSeschrock 	}
3556eaca9bbdSeschrock 
3557990b4856Slling 	ret = zpool_upgrade(zhp, cbp->cb_version);
355806eeb2adSek 
355906eeb2adSek 	if (!ret) {
356044cd46caSbillm 		(void) printf(gettext("Successfully upgraded '%s' "
3561990b4856Slling 		    "from version %llu to version %llu\n\n"),
3562990b4856Slling 		    zpool_get_name(zhp), (u_longlong_t)cur_version,
3563990b4856Slling 		    (u_longlong_t)cbp->cb_version);
356406eeb2adSek 	}
3565eaca9bbdSeschrock 
3566eaca9bbdSeschrock 	return (ret != 0);
3567eaca9bbdSeschrock }
3568eaca9bbdSeschrock 
3569eaca9bbdSeschrock /*
3570eaca9bbdSeschrock  * zpool upgrade
3571eaca9bbdSeschrock  * zpool upgrade -v
3572990b4856Slling  * zpool upgrade [-V version] <-a | pool ...>
3573eaca9bbdSeschrock  *
3574eaca9bbdSeschrock  * With no arguments, display downrev'd ZFS pool available for upgrade.
3575eaca9bbdSeschrock  * Individual pools can be upgraded by specifying the pool, and '-a' will
3576eaca9bbdSeschrock  * upgrade all pools.
3577eaca9bbdSeschrock  */
3578eaca9bbdSeschrock int
3579eaca9bbdSeschrock zpool_do_upgrade(int argc, char **argv)
3580eaca9bbdSeschrock {
3581eaca9bbdSeschrock 	int c;
3582eaca9bbdSeschrock 	upgrade_cbdata_t cb = { 0 };
3583eaca9bbdSeschrock 	int ret = 0;
3584eaca9bbdSeschrock 	boolean_t showversions = B_FALSE;
3585990b4856Slling 	char *end;
3586990b4856Slling 
3587eaca9bbdSeschrock 
3588eaca9bbdSeschrock 	/* check options */
3589478ed9adSEric Taylor 	while ((c = getopt(argc, argv, ":avV:")) != -1) {
3590eaca9bbdSeschrock 		switch (c) {
3591eaca9bbdSeschrock 		case 'a':
359299653d4eSeschrock 			cb.cb_all = B_TRUE;
3593eaca9bbdSeschrock 			break;
3594eaca9bbdSeschrock 		case 'v':
3595eaca9bbdSeschrock 			showversions = B_TRUE;
3596eaca9bbdSeschrock 			break;
3597990b4856Slling 		case 'V':
3598990b4856Slling 			cb.cb_version = strtoll(optarg, &end, 10);
3599351420b3Slling 			if (*end != '\0' || cb.cb_version > SPA_VERSION ||
3600351420b3Slling 			    cb.cb_version < SPA_VERSION_1) {
3601990b4856Slling 				(void) fprintf(stderr,
3602990b4856Slling 				    gettext("invalid version '%s'\n"), optarg);
3603990b4856Slling 				usage(B_FALSE);
3604990b4856Slling 			}
3605990b4856Slling 			break;
3606478ed9adSEric Taylor 		case ':':
3607478ed9adSEric Taylor 			(void) fprintf(stderr, gettext("missing argument for "
3608478ed9adSEric Taylor 			    "'%c' option\n"), optopt);
3609478ed9adSEric Taylor 			usage(B_FALSE);
3610478ed9adSEric Taylor 			break;
3611eaca9bbdSeschrock 		case '?':
3612eaca9bbdSeschrock 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3613eaca9bbdSeschrock 			    optopt);
361499653d4eSeschrock 			usage(B_FALSE);
3615eaca9bbdSeschrock 		}
3616eaca9bbdSeschrock 	}
3617eaca9bbdSeschrock 
361806eeb2adSek 	cb.cb_argc = argc;
361906eeb2adSek 	cb.cb_argv = argv;
3620eaca9bbdSeschrock 	argc -= optind;
3621eaca9bbdSeschrock 	argv += optind;
3622eaca9bbdSeschrock 
3623351420b3Slling 	if (cb.cb_version == 0) {
3624351420b3Slling 		cb.cb_version = SPA_VERSION;
3625351420b3Slling 	} else if (!cb.cb_all && argc == 0) {
3626351420b3Slling 		(void) fprintf(stderr, gettext("-V option is "
3627351420b3Slling 		    "incompatible with other arguments\n"));
3628351420b3Slling 		usage(B_FALSE);
3629351420b3Slling 	}
3630351420b3Slling 
3631eaca9bbdSeschrock 	if (showversions) {
3632eaca9bbdSeschrock 		if (cb.cb_all || argc != 0) {
3633eaca9bbdSeschrock 			(void) fprintf(stderr, gettext("-v option is "
3634eaca9bbdSeschrock 			    "incompatible with other arguments\n"));
363599653d4eSeschrock 			usage(B_FALSE);
3636eaca9bbdSeschrock 		}
3637eaca9bbdSeschrock 	} else if (cb.cb_all) {
3638eaca9bbdSeschrock 		if (argc != 0) {
3639351420b3Slling 			(void) fprintf(stderr, gettext("-a option should not "
3640351420b3Slling 			    "be used along with a pool name\n"));
364199653d4eSeschrock 			usage(B_FALSE);
3642eaca9bbdSeschrock 		}
3643eaca9bbdSeschrock 	}
3644eaca9bbdSeschrock 
3645e7437265Sahrens 	(void) printf(gettext("This system is currently running "
3646e7437265Sahrens 	    "ZFS pool version %llu.\n\n"), SPA_VERSION);
364799653d4eSeschrock 	cb.cb_first = B_TRUE;
3648eaca9bbdSeschrock 	if (showversions) {
3649eaca9bbdSeschrock 		(void) printf(gettext("The following versions are "
3650d7d4af51Smmusante 		    "supported:\n\n"));
3651eaca9bbdSeschrock 		(void) printf(gettext("VER  DESCRIPTION\n"));
3652eaca9bbdSeschrock 		(void) printf("---  -----------------------------------------"
3653eaca9bbdSeschrock 		    "---------------\n");
365499653d4eSeschrock 		(void) printf(gettext(" 1   Initial ZFS version\n"));
365544cd46caSbillm 		(void) printf(gettext(" 2   Ditto blocks "
365644cd46caSbillm 		    "(replicated metadata)\n"));
365799653d4eSeschrock 		(void) printf(gettext(" 3   Hot spares and double parity "
365899653d4eSeschrock 		    "RAID-Z\n"));
3659d7306b64Sek 		(void) printf(gettext(" 4   zpool history\n"));
3660c9431fa1Sahl 		(void) printf(gettext(" 5   Compression using the gzip "
3661c9431fa1Sahl 		    "algorithm\n"));
3662990b4856Slling 		(void) printf(gettext(" 6   bootfs pool property\n"));
36638654d025Sperrin 		(void) printf(gettext(" 7   Separate intent log devices\n"));
3664ecd6cf80Smarks 		(void) printf(gettext(" 8   Delegated administration\n"));
36658eed72d4Sck 		(void) printf(gettext(" 9   refquota and refreservation "
3666a9799022Sck 		    "properties\n"));
3667fa94a07fSbrendan 		(void) printf(gettext(" 10  Cache devices\n"));
3668088f3894Sahrens 		(void) printf(gettext(" 11  Improved scrub performance\n"));
3669bb0ade09Sahrens 		(void) printf(gettext(" 12  Snapshot properties\n"));
367074e7dc98SMatthew Ahrens 		(void) printf(gettext(" 13  snapused property\n"));
367114843421SMatthew Ahrens 		(void) printf(gettext(" 14  passthrough-x aclinherit\n"));
367214843421SMatthew Ahrens 		(void) printf(gettext(" 15  user/group space accounting\n"));
3673478ed9adSEric Taylor 		(void) printf(gettext(" 16  stmf property support\n"));
36747aeab329SAdam Leventhal 		(void) printf(gettext(" 17  Triple-parity RAID-Z\n"));
3675842727c2SChris Kirby 		(void) printf(gettext(" 18  snapshot user holds\n"));
367688ecc943SGeorge Wilson 		(void) printf(gettext(" 19  Log device removal\n"));
36778654d025Sperrin 		(void) printf(gettext("For more information on a particular "
3678eaca9bbdSeschrock 		    "version, including supported releases, see:\n\n"));
3679eaca9bbdSeschrock 		(void) printf("http://www.opensolaris.org/os/community/zfs/"
3680eaca9bbdSeschrock 		    "version/N\n\n");
3681eaca9bbdSeschrock 		(void) printf(gettext("Where 'N' is the version number.\n"));
3682eaca9bbdSeschrock 	} else if (argc == 0) {
3683eaca9bbdSeschrock 		int notfound;
3684eaca9bbdSeschrock 
368599653d4eSeschrock 		ret = zpool_iter(g_zfs, upgrade_cb, &cb);
3686eaca9bbdSeschrock 		notfound = cb.cb_first;
3687eaca9bbdSeschrock 
3688eaca9bbdSeschrock 		if (!cb.cb_all && ret == 0) {
3689eaca9bbdSeschrock 			if (!cb.cb_first)
3690eaca9bbdSeschrock 				(void) printf("\n");
3691eaca9bbdSeschrock 			cb.cb_first = B_TRUE;
3692eaca9bbdSeschrock 			cb.cb_newer = B_TRUE;
369399653d4eSeschrock 			ret = zpool_iter(g_zfs, upgrade_cb, &cb);
3694eaca9bbdSeschrock 			if (!cb.cb_first) {
3695eaca9bbdSeschrock 				notfound = B_FALSE;
3696eaca9bbdSeschrock 				(void) printf("\n");
3697eaca9bbdSeschrock 			}
3698eaca9bbdSeschrock 		}
3699eaca9bbdSeschrock 
3700eaca9bbdSeschrock 		if (ret == 0) {
3701eaca9bbdSeschrock 			if (notfound)
3702eaca9bbdSeschrock 				(void) printf(gettext("All pools are formatted "
3703eaca9bbdSeschrock 				    "using this version.\n"));
3704eaca9bbdSeschrock 			else if (!cb.cb_all)
3705eaca9bbdSeschrock 				(void) printf(gettext("Use 'zpool upgrade -v' "
3706eaca9bbdSeschrock 				    "for a list of available versions and "
3707eaca9bbdSeschrock 				    "their associated\nfeatures.\n"));
3708eaca9bbdSeschrock 		}
3709eaca9bbdSeschrock 	} else {
3710b1b8ab34Slling 		ret = for_each_pool(argc, argv, B_FALSE, NULL,
3711b1b8ab34Slling 		    upgrade_one, &cb);
371206eeb2adSek 	}
371306eeb2adSek 
371406eeb2adSek 	return (ret);
371506eeb2adSek }
371606eeb2adSek 
3717ecd6cf80Smarks typedef struct hist_cbdata {
3718ecd6cf80Smarks 	boolean_t first;
3719ecd6cf80Smarks 	int longfmt;
3720ecd6cf80Smarks 	int internal;
3721ecd6cf80Smarks } hist_cbdata_t;
3722ecd6cf80Smarks 
372306eeb2adSek /*
372406eeb2adSek  * Print out the command history for a specific pool.
372506eeb2adSek  */
372606eeb2adSek static int
372706eeb2adSek get_history_one(zpool_handle_t *zhp, void *data)
372806eeb2adSek {
372906eeb2adSek 	nvlist_t *nvhis;
373006eeb2adSek 	nvlist_t **records;
373106eeb2adSek 	uint_t numrecords;
373206eeb2adSek 	char *cmdstr;
3733ecd6cf80Smarks 	char *pathstr;
373406eeb2adSek 	uint64_t dst_time;
373506eeb2adSek 	time_t tsec;
373606eeb2adSek 	struct tm t;
373706eeb2adSek 	char tbuf[30];
373806eeb2adSek 	int ret, i;
3739ecd6cf80Smarks 	uint64_t who;
3740ecd6cf80Smarks 	struct passwd *pwd;
3741ecd6cf80Smarks 	char *hostname;
3742ecd6cf80Smarks 	char *zonename;
3743ecd6cf80Smarks 	char internalstr[MAXPATHLEN];
3744ecd6cf80Smarks 	hist_cbdata_t *cb = (hist_cbdata_t *)data;
3745ecd6cf80Smarks 	uint64_t txg;
3746ecd6cf80Smarks 	uint64_t ievent;
374706eeb2adSek 
3748ecd6cf80Smarks 	cb->first = B_FALSE;
374906eeb2adSek 
375006eeb2adSek 	(void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
375106eeb2adSek 
375206eeb2adSek 	if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
375306eeb2adSek 		return (ret);
375406eeb2adSek 
375506eeb2adSek 	verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
375606eeb2adSek 	    &records, &numrecords) == 0);
375706eeb2adSek 	for (i = 0; i < numrecords; i++) {
375806eeb2adSek 		if (nvlist_lookup_uint64(records[i], ZPOOL_HIST_TIME,
3759ecd6cf80Smarks 		    &dst_time) != 0)
3760ecd6cf80Smarks 			continue;
3761ecd6cf80Smarks 
3762ecd6cf80Smarks 		/* is it an internal event or a standard event? */
3763ecd6cf80Smarks 		if (nvlist_lookup_string(records[i], ZPOOL_HIST_CMD,
3764ecd6cf80Smarks 		    &cmdstr) != 0) {
3765ecd6cf80Smarks 			if (cb->internal == 0)
3766ecd6cf80Smarks 				continue;
3767ecd6cf80Smarks 
3768ecd6cf80Smarks 			if (nvlist_lookup_uint64(records[i],
3769ecd6cf80Smarks 			    ZPOOL_HIST_INT_EVENT, &ievent) != 0)
3770ecd6cf80Smarks 				continue;
3771ecd6cf80Smarks 			verify(nvlist_lookup_uint64(records[i],
3772ecd6cf80Smarks 			    ZPOOL_HIST_TXG, &txg) == 0);
3773ecd6cf80Smarks 			verify(nvlist_lookup_string(records[i],
3774ecd6cf80Smarks 			    ZPOOL_HIST_INT_STR, &pathstr) == 0);
3775088f3894Sahrens 			if (ievent >= LOG_END)
3776ecd6cf80Smarks 				continue;
3777ecd6cf80Smarks 			(void) snprintf(internalstr,
3778ecd6cf80Smarks 			    sizeof (internalstr),
3779ecd6cf80Smarks 			    "[internal %s txg:%lld] %s",
3780ecd6cf80Smarks 			    hist_event_table[ievent], txg,
3781ecd6cf80Smarks 			    pathstr);
3782ecd6cf80Smarks 			cmdstr = internalstr;
378306eeb2adSek 		}
3784ecd6cf80Smarks 		tsec = dst_time;
3785ecd6cf80Smarks 		(void) localtime_r(&tsec, &t);
3786ecd6cf80Smarks 		(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
3787ecd6cf80Smarks 		(void) printf("%s %s", tbuf, cmdstr);
3788ecd6cf80Smarks 
3789ecd6cf80Smarks 		if (!cb->longfmt) {
3790ecd6cf80Smarks 			(void) printf("\n");
3791ecd6cf80Smarks 			continue;
3792ecd6cf80Smarks 		}
3793ecd6cf80Smarks 		(void) printf(" [");
3794ecd6cf80Smarks 		if (nvlist_lookup_uint64(records[i],
3795ecd6cf80Smarks 		    ZPOOL_HIST_WHO, &who) == 0) {
3796ecd6cf80Smarks 			pwd = getpwuid((uid_t)who);
3797ecd6cf80Smarks 			if (pwd)
3798ecd6cf80Smarks 				(void) printf("user %s on",
3799ecd6cf80Smarks 				    pwd->pw_name);
3800ecd6cf80Smarks 			else
3801ecd6cf80Smarks 				(void) printf("user %d on",
3802ecd6cf80Smarks 				    (int)who);
3803ecd6cf80Smarks 		} else {
3804ecd6cf80Smarks 			(void) printf(gettext("no info]\n"));
3805ecd6cf80Smarks 			continue;
3806ecd6cf80Smarks 		}
3807ecd6cf80Smarks 		if (nvlist_lookup_string(records[i],
3808ecd6cf80Smarks 		    ZPOOL_HIST_HOST, &hostname) == 0) {
3809ecd6cf80Smarks 			(void) printf(" %s", hostname);
3810ecd6cf80Smarks 		}
3811ecd6cf80Smarks 		if (nvlist_lookup_string(records[i],
3812ecd6cf80Smarks 		    ZPOOL_HIST_ZONE, &zonename) == 0) {
3813ecd6cf80Smarks 			(void) printf(":%s", zonename);
3814ecd6cf80Smarks 		}
3815ecd6cf80Smarks 
3816ecd6cf80Smarks 		(void) printf("]");
3817ecd6cf80Smarks 		(void) printf("\n");
381806eeb2adSek 	}
381906eeb2adSek 	(void) printf("\n");
382006eeb2adSek 	nvlist_free(nvhis);
382106eeb2adSek 
382206eeb2adSek 	return (ret);
382306eeb2adSek }
382406eeb2adSek 
382506eeb2adSek /*
382606eeb2adSek  * zpool history <pool>
382706eeb2adSek  *
382806eeb2adSek  * Displays the history of commands that modified pools.
382906eeb2adSek  */
3830ecd6cf80Smarks 
3831ecd6cf80Smarks 
383206eeb2adSek int
383306eeb2adSek zpool_do_history(int argc, char **argv)
383406eeb2adSek {
3835ecd6cf80Smarks 	hist_cbdata_t cbdata = { 0 };
383606eeb2adSek 	int ret;
3837ecd6cf80Smarks 	int c;
383806eeb2adSek 
3839ecd6cf80Smarks 	cbdata.first = B_TRUE;
3840ecd6cf80Smarks 	/* check options */
3841ecd6cf80Smarks 	while ((c = getopt(argc, argv, "li")) != -1) {
3842ecd6cf80Smarks 		switch (c) {
3843ecd6cf80Smarks 		case 'l':
3844ecd6cf80Smarks 			cbdata.longfmt = 1;
3845ecd6cf80Smarks 			break;
3846ecd6cf80Smarks 		case 'i':
3847ecd6cf80Smarks 			cbdata.internal = 1;
3848ecd6cf80Smarks 			break;
3849ecd6cf80Smarks 		case '?':
3850ecd6cf80Smarks 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3851ecd6cf80Smarks 			    optopt);
3852ecd6cf80Smarks 			usage(B_FALSE);
3853ecd6cf80Smarks 		}
3854ecd6cf80Smarks 	}
385506eeb2adSek 	argc -= optind;
385606eeb2adSek 	argv += optind;
385706eeb2adSek 
3858b1b8ab34Slling 	ret = for_each_pool(argc, argv, B_FALSE,  NULL, get_history_one,
3859ecd6cf80Smarks 	    &cbdata);
386006eeb2adSek 
3861ecd6cf80Smarks 	if (argc == 0 && cbdata.first == B_TRUE) {
386206eeb2adSek 		(void) printf(gettext("no pools available\n"));
386306eeb2adSek 		return (0);
3864eaca9bbdSeschrock 	}
3865eaca9bbdSeschrock 
3866eaca9bbdSeschrock 	return (ret);
3867eaca9bbdSeschrock }
3868eaca9bbdSeschrock 
3869b1b8ab34Slling static int
3870b1b8ab34Slling get_callback(zpool_handle_t *zhp, void *data)
3871b1b8ab34Slling {
3872990b4856Slling 	zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
3873b1b8ab34Slling 	char value[MAXNAMELEN];
3874990b4856Slling 	zprop_source_t srctype;
3875990b4856Slling 	zprop_list_t *pl;
3876b1b8ab34Slling 
3877b1b8ab34Slling 	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
3878b1b8ab34Slling 
3879b1b8ab34Slling 		/*
3880990b4856Slling 		 * Skip the special fake placeholder. This will also skip
3881990b4856Slling 		 * over the name property when 'all' is specified.
3882b1b8ab34Slling 		 */
3883990b4856Slling 		if (pl->pl_prop == ZPOOL_PROP_NAME &&
3884b1b8ab34Slling 		    pl == cbp->cb_proplist)
3885b1b8ab34Slling 			continue;
3886b1b8ab34Slling 
3887b1b8ab34Slling 		if (zpool_get_prop(zhp, pl->pl_prop,
3888b1b8ab34Slling 		    value, sizeof (value), &srctype) != 0)
3889b1b8ab34Slling 			continue;
3890b1b8ab34Slling 
3891990b4856Slling 		zprop_print_one_property(zpool_get_name(zhp), cbp,
3892b1b8ab34Slling 		    zpool_prop_to_name(pl->pl_prop), value, srctype, NULL);
3893b1b8ab34Slling 	}
3894b1b8ab34Slling 	return (0);
3895b1b8ab34Slling }
3896b1b8ab34Slling 
3897b1b8ab34Slling int
3898b1b8ab34Slling zpool_do_get(int argc, char **argv)
3899b1b8ab34Slling {
3900990b4856Slling 	zprop_get_cbdata_t cb = { 0 };
3901990b4856Slling 	zprop_list_t fake_name = { 0 };
3902b1b8ab34Slling 	int ret;
3903b1b8ab34Slling 
3904b1b8ab34Slling 	if (argc < 3)
3905b1b8ab34Slling 		usage(B_FALSE);
3906b1b8ab34Slling 
3907b1b8ab34Slling 	cb.cb_first = B_TRUE;
3908990b4856Slling 	cb.cb_sources = ZPROP_SRC_ALL;
3909b1b8ab34Slling 	cb.cb_columns[0] = GET_COL_NAME;
3910b1b8ab34Slling 	cb.cb_columns[1] = GET_COL_PROPERTY;
3911b1b8ab34Slling 	cb.cb_columns[2] = GET_COL_VALUE;
3912b1b8ab34Slling 	cb.cb_columns[3] = GET_COL_SOURCE;
3913990b4856Slling 	cb.cb_type = ZFS_TYPE_POOL;
3914b1b8ab34Slling 
3915990b4856Slling 	if (zprop_get_list(g_zfs, argv[1],  &cb.cb_proplist,
3916990b4856Slling 	    ZFS_TYPE_POOL) != 0)
3917b1b8ab34Slling 		usage(B_FALSE);
3918b1b8ab34Slling 
3919b1b8ab34Slling 	if (cb.cb_proplist != NULL) {
3920990b4856Slling 		fake_name.pl_prop = ZPOOL_PROP_NAME;
3921b1b8ab34Slling 		fake_name.pl_width = strlen(gettext("NAME"));
3922b1b8ab34Slling 		fake_name.pl_next = cb.cb_proplist;
3923b1b8ab34Slling 		cb.cb_proplist = &fake_name;
3924b1b8ab34Slling 	}
3925b1b8ab34Slling 
3926b1b8ab34Slling 	ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
3927b1b8ab34Slling 	    get_callback, &cb);
3928b1b8ab34Slling 
3929b1b8ab34Slling 	if (cb.cb_proplist == &fake_name)
3930990b4856Slling 		zprop_free_list(fake_name.pl_next);
3931b1b8ab34Slling 	else
3932990b4856Slling 		zprop_free_list(cb.cb_proplist);
3933b1b8ab34Slling 
3934b1b8ab34Slling 	return (ret);
3935b1b8ab34Slling }
3936b1b8ab34Slling 
3937b1b8ab34Slling typedef struct set_cbdata {
3938b1b8ab34Slling 	char *cb_propname;
3939b1b8ab34Slling 	char *cb_value;
3940b1b8ab34Slling 	boolean_t cb_any_successful;
3941b1b8ab34Slling } set_cbdata_t;
3942b1b8ab34Slling 
3943b1b8ab34Slling int
3944b1b8ab34Slling set_callback(zpool_handle_t *zhp, void *data)
3945b1b8ab34Slling {
3946b1b8ab34Slling 	int error;
3947b1b8ab34Slling 	set_cbdata_t *cb = (set_cbdata_t *)data;
3948b1b8ab34Slling 
3949b1b8ab34Slling 	error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
3950b1b8ab34Slling 
3951b1b8ab34Slling 	if (!error)
3952b1b8ab34Slling 		cb->cb_any_successful = B_TRUE;
3953b1b8ab34Slling 
3954b1b8ab34Slling 	return (error);
3955b1b8ab34Slling }
3956b1b8ab34Slling 
3957b1b8ab34Slling int
3958b1b8ab34Slling zpool_do_set(int argc, char **argv)
3959b1b8ab34Slling {
3960b1b8ab34Slling 	set_cbdata_t cb = { 0 };
3961b1b8ab34Slling 	int error;
3962b1b8ab34Slling 
3963b1b8ab34Slling 	if (argc > 1 && argv[1][0] == '-') {
3964b1b8ab34Slling 		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3965b1b8ab34Slling 		    argv[1][1]);
3966b1b8ab34Slling 		usage(B_FALSE);
3967b1b8ab34Slling 	}
3968b1b8ab34Slling 
3969b1b8ab34Slling 	if (argc < 2) {
3970b1b8ab34Slling 		(void) fprintf(stderr, gettext("missing property=value "
3971b1b8ab34Slling 		    "argument\n"));
3972b1b8ab34Slling 		usage(B_FALSE);
3973b1b8ab34Slling 	}
3974b1b8ab34Slling 
3975b1b8ab34Slling 	if (argc < 3) {
3976b1b8ab34Slling 		(void) fprintf(stderr, gettext("missing pool name\n"));
3977b1b8ab34Slling 		usage(B_FALSE);
3978b1b8ab34Slling 	}
3979b1b8ab34Slling 
3980b1b8ab34Slling 	if (argc > 3) {
3981b1b8ab34Slling 		(void) fprintf(stderr, gettext("too many pool names\n"));
3982b1b8ab34Slling 		usage(B_FALSE);
3983b1b8ab34Slling 	}
3984b1b8ab34Slling 
3985b1b8ab34Slling 	cb.cb_propname = argv[1];
3986b1b8ab34Slling 	cb.cb_value = strchr(cb.cb_propname, '=');
3987b1b8ab34Slling 	if (cb.cb_value == NULL) {
3988b1b8ab34Slling 		(void) fprintf(stderr, gettext("missing value in "
3989b1b8ab34Slling 		    "property=value argument\n"));
3990b1b8ab34Slling 		usage(B_FALSE);
3991b1b8ab34Slling 	}
3992b1b8ab34Slling 
3993b1b8ab34Slling 	*(cb.cb_value) = '\0';
3994b1b8ab34Slling 	cb.cb_value++;
3995b1b8ab34Slling 
3996b1b8ab34Slling 	error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
3997b1b8ab34Slling 	    set_callback, &cb);
3998b1b8ab34Slling 
3999b1b8ab34Slling 	return (error);
4000b1b8ab34Slling }
4001b1b8ab34Slling 
4002b1b8ab34Slling static int
4003b1b8ab34Slling find_command_idx(char *command, int *idx)
4004b1b8ab34Slling {
4005b1b8ab34Slling 	int i;
4006b1b8ab34Slling 
4007b1b8ab34Slling 	for (i = 0; i < NCOMMAND; i++) {
4008b1b8ab34Slling 		if (command_table[i].name == NULL)
4009b1b8ab34Slling 			continue;
4010b1b8ab34Slling 
4011b1b8ab34Slling 		if (strcmp(command, command_table[i].name) == 0) {
4012b1b8ab34Slling 			*idx = i;
4013b1b8ab34Slling 			return (0);
4014b1b8ab34Slling 		}
4015b1b8ab34Slling 	}
4016b1b8ab34Slling 	return (1);
4017b1b8ab34Slling }
4018b1b8ab34Slling 
4019fa9e4066Sahrens int
4020fa9e4066Sahrens main(int argc, char **argv)
4021fa9e4066Sahrens {
4022fa9e4066Sahrens 	int ret;
4023fa9e4066Sahrens 	int i;
4024fa9e4066Sahrens 	char *cmdname;
4025fa9e4066Sahrens 
4026fa9e4066Sahrens 	(void) setlocale(LC_ALL, "");
4027fa9e4066Sahrens 	(void) textdomain(TEXT_DOMAIN);
4028fa9e4066Sahrens 
402999653d4eSeschrock 	if ((g_zfs = libzfs_init()) == NULL) {
403099653d4eSeschrock 		(void) fprintf(stderr, gettext("internal error: failed to "
4031203a47d8Snd 		    "initialize ZFS library\n"));
403299653d4eSeschrock 		return (1);
403399653d4eSeschrock 	}
403499653d4eSeschrock 
403599653d4eSeschrock 	libzfs_print_on_error(g_zfs, B_TRUE);
403699653d4eSeschrock 
4037fa9e4066Sahrens 	opterr = 0;
4038fa9e4066Sahrens 
4039fa9e4066Sahrens 	/*
4040fa9e4066Sahrens 	 * Make sure the user has specified some command.
4041fa9e4066Sahrens 	 */
4042fa9e4066Sahrens 	if (argc < 2) {
4043fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing command\n"));
404499653d4eSeschrock 		usage(B_FALSE);
4045fa9e4066Sahrens 	}
4046fa9e4066Sahrens 
4047fa9e4066Sahrens 	cmdname = argv[1];
4048fa9e4066Sahrens 
4049fa9e4066Sahrens 	/*
4050fa9e4066Sahrens 	 * Special case '-?'
4051fa9e4066Sahrens 	 */
4052fa9e4066Sahrens 	if (strcmp(cmdname, "-?") == 0)
405399653d4eSeschrock 		usage(B_TRUE);
4054fa9e4066Sahrens 
40552a6b87f0Sek 	zpool_set_history_str("zpool", argc, argv, history_str);
40562a6b87f0Sek 	verify(zpool_stage_history(g_zfs, history_str) == 0);
40572a6b87f0Sek 
4058fa9e4066Sahrens 	/*
4059fa9e4066Sahrens 	 * Run the appropriate command.
4060fa9e4066Sahrens 	 */
4061b1b8ab34Slling 	if (find_command_idx(cmdname, &i) == 0) {
4062b1b8ab34Slling 		current_command = &command_table[i];
4063b1b8ab34Slling 		ret = command_table[i].func(argc - 1, argv + 1);
406491ebeef5Sahrens 	} else if (strchr(cmdname, '=')) {
406591ebeef5Sahrens 		verify(find_command_idx("set", &i) == 0);
406691ebeef5Sahrens 		current_command = &command_table[i];
406791ebeef5Sahrens 		ret = command_table[i].func(argc, argv);
406891ebeef5Sahrens 	} else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
406991ebeef5Sahrens 		/*
407091ebeef5Sahrens 		 * 'freeze' is a vile debugging abomination, so we treat
407191ebeef5Sahrens 		 * it as such.
407291ebeef5Sahrens 		 */
4073ea8dc4b6Seschrock 		char buf[16384];
4074ea8dc4b6Seschrock 		int fd = open(ZFS_DEV, O_RDWR);
4075fa9e4066Sahrens 		(void) strcpy((void *)buf, argv[2]);
4076fa9e4066Sahrens 		return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
407791ebeef5Sahrens 	} else {
4078fa9e4066Sahrens 		(void) fprintf(stderr, gettext("unrecognized "
4079fa9e4066Sahrens 		    "command '%s'\n"), cmdname);
408099653d4eSeschrock 		usage(B_FALSE);
4081fa9e4066Sahrens 	}
4082fa9e4066Sahrens 
408399653d4eSeschrock 	libzfs_fini(g_zfs);
408499653d4eSeschrock 
4085fa9e4066Sahrens 	/*
4086fa9e4066Sahrens 	 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4087fa9e4066Sahrens 	 * for the purposes of running ::findleaks.
4088fa9e4066Sahrens 	 */
4089fa9e4066Sahrens 	if (getenv("ZFS_ABORT") != NULL) {
4090fa9e4066Sahrens 		(void) printf("dumping core by request\n");
4091fa9e4066Sahrens 		abort();
4092fa9e4066Sahrens 	}
4093fa9e4066Sahrens 
4094fa9e4066Sahrens 	return (ret);
4095fa9e4066Sahrens }
4096