xref: /titanic_53/usr/src/cmd/zpool/zpool_main.c (revision 654b400c387942fc00d4f6869539adbd7b25fbce)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5441d80aaSlling  * Common Development and Distribution License (the "License").
6441d80aaSlling  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
2199653d4eSeschrock 
22fa9e4066Sahrens /*
233f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24ce72e614SYuri Pankov  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
254263d13fSGeorge Wilson  * Copyright (c) 2012 by Delphix. All rights reserved.
26e1d5e507SFrederik Wessels  * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27fa9e4066Sahrens  */
28fa9e4066Sahrens 
29fa9e4066Sahrens #include <assert.h>
30fa9e4066Sahrens #include <ctype.h>
31fa9e4066Sahrens #include <dirent.h>
32fa9e4066Sahrens #include <errno.h>
33fa9e4066Sahrens #include <fcntl.h>
34fa9e4066Sahrens #include <libgen.h>
35fa9e4066Sahrens #include <libintl.h>
36fa9e4066Sahrens #include <libuutil.h>
37fa9e4066Sahrens #include <locale.h>
38fa9e4066Sahrens #include <stdio.h>
39fa9e4066Sahrens #include <stdlib.h>
40fa9e4066Sahrens #include <string.h>
41fa9e4066Sahrens #include <strings.h>
42fa9e4066Sahrens #include <unistd.h>
43fa9e4066Sahrens #include <priv.h>
44ecd6cf80Smarks #include <pwd.h>
45ecd6cf80Smarks #include <zone.h>
464263d13fSGeorge Wilson #include <zfs_prop.h>
47b1b8ab34Slling #include <sys/fs/zfs.h>
48fa9e4066Sahrens #include <sys/stat.h>
49fa9e4066Sahrens 
50fa9e4066Sahrens #include <libzfs.h>
51fa9e4066Sahrens 
52fa9e4066Sahrens #include "zpool_util.h"
53b7b97454Sperrin #include "zfs_comutil.h"
54fa9e4066Sahrens 
5526fd7700SKrishnendu Sadhukhan - Sun Microsystems #include "statcommon.h"
5626fd7700SKrishnendu Sadhukhan - Sun Microsystems 
57fa9e4066Sahrens static int zpool_do_create(int, char **);
58fa9e4066Sahrens static int zpool_do_destroy(int, char **);
59fa9e4066Sahrens 
60fa9e4066Sahrens static int zpool_do_add(int, char **);
6199653d4eSeschrock static int zpool_do_remove(int, char **);
62fa9e4066Sahrens 
63fa9e4066Sahrens static int zpool_do_list(int, char **);
64fa9e4066Sahrens static int zpool_do_iostat(int, char **);
65fa9e4066Sahrens static int zpool_do_status(int, char **);
66fa9e4066Sahrens 
67fa9e4066Sahrens static int zpool_do_online(int, char **);
68fa9e4066Sahrens static int zpool_do_offline(int, char **);
69ea8dc4b6Seschrock static int zpool_do_clear(int, char **);
704263d13fSGeorge Wilson static int zpool_do_reopen(int, char **);
71fa9e4066Sahrens 
72e9103aaeSGarrett D'Amore static int zpool_do_reguid(int, char **);
73e9103aaeSGarrett D'Amore 
74fa9e4066Sahrens static int zpool_do_attach(int, char **);
75fa9e4066Sahrens static int zpool_do_detach(int, char **);
76fa9e4066Sahrens static int zpool_do_replace(int, char **);
771195e687SMark J Musante static int zpool_do_split(int, char **);
78fa9e4066Sahrens 
79fa9e4066Sahrens static int zpool_do_scrub(int, char **);
80fa9e4066Sahrens 
81fa9e4066Sahrens static int zpool_do_import(int, char **);
82fa9e4066Sahrens static int zpool_do_export(int, char **);
83fa9e4066Sahrens 
84eaca9bbdSeschrock static int zpool_do_upgrade(int, char **);
85eaca9bbdSeschrock 
8606eeb2adSek110237 static int zpool_do_history(int, char **);
8706eeb2adSek110237 
88b1b8ab34Slling static int zpool_do_get(int, char **);
89b1b8ab34Slling static int zpool_do_set(int, char **);
90b1b8ab34Slling 
91fa9e4066Sahrens /*
92fa9e4066Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
93fa9e4066Sahrens  * debugging facilities.
94fa9e4066Sahrens  */
9529ab75c9Srm160521 
9629ab75c9Srm160521 #ifdef DEBUG
97fa9e4066Sahrens const char *
9899653d4eSeschrock _umem_debug_init(void)
99fa9e4066Sahrens {
100fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
101fa9e4066Sahrens }
102fa9e4066Sahrens 
103fa9e4066Sahrens const char *
104fa9e4066Sahrens _umem_logging_init(void)
105fa9e4066Sahrens {
106fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
107fa9e4066Sahrens }
10829ab75c9Srm160521 #endif
109fa9e4066Sahrens 
11065cd9f28Seschrock typedef enum {
11165cd9f28Seschrock 	HELP_ADD,
11265cd9f28Seschrock 	HELP_ATTACH,
113ea8dc4b6Seschrock 	HELP_CLEAR,
11465cd9f28Seschrock 	HELP_CREATE,
11565cd9f28Seschrock 	HELP_DESTROY,
11665cd9f28Seschrock 	HELP_DETACH,
11765cd9f28Seschrock 	HELP_EXPORT,
11806eeb2adSek110237 	HELP_HISTORY,
11965cd9f28Seschrock 	HELP_IMPORT,
12065cd9f28Seschrock 	HELP_IOSTAT,
12165cd9f28Seschrock 	HELP_LIST,
12265cd9f28Seschrock 	HELP_OFFLINE,
12365cd9f28Seschrock 	HELP_ONLINE,
12465cd9f28Seschrock 	HELP_REPLACE,
12599653d4eSeschrock 	HELP_REMOVE,
12665cd9f28Seschrock 	HELP_SCRUB,
127eaca9bbdSeschrock 	HELP_STATUS,
128b1b8ab34Slling 	HELP_UPGRADE,
129b1b8ab34Slling 	HELP_GET,
1301195e687SMark J Musante 	HELP_SET,
131e9103aaeSGarrett D'Amore 	HELP_SPLIT,
1324263d13fSGeorge Wilson 	HELP_REGUID,
1334263d13fSGeorge Wilson 	HELP_REOPEN
13465cd9f28Seschrock } zpool_help_t;
13565cd9f28Seschrock 
13665cd9f28Seschrock 
137fa9e4066Sahrens typedef struct zpool_command {
138fa9e4066Sahrens 	const char	*name;
139fa9e4066Sahrens 	int		(*func)(int, char **);
14065cd9f28Seschrock 	zpool_help_t	usage;
141fa9e4066Sahrens } zpool_command_t;
142fa9e4066Sahrens 
143fa9e4066Sahrens /*
144fa9e4066Sahrens  * Master command table.  Each ZFS command has a name, associated function, and
145ea8dc4b6Seschrock  * usage message.  The usage messages need to be internationalized, so we have
146ea8dc4b6Seschrock  * to have a function to return the usage message based on a command index.
14765cd9f28Seschrock  *
14865cd9f28Seschrock  * These commands are organized according to how they are displayed in the usage
14965cd9f28Seschrock  * message.  An empty command (one with a NULL name) indicates an empty line in
15065cd9f28Seschrock  * the generic usage message.
151fa9e4066Sahrens  */
152fa9e4066Sahrens static zpool_command_t command_table[] = {
15365cd9f28Seschrock 	{ "create",	zpool_do_create,	HELP_CREATE		},
15465cd9f28Seschrock 	{ "destroy",	zpool_do_destroy,	HELP_DESTROY		},
155fa9e4066Sahrens 	{ NULL },
15665cd9f28Seschrock 	{ "add",	zpool_do_add,		HELP_ADD		},
15799653d4eSeschrock 	{ "remove",	zpool_do_remove,	HELP_REMOVE		},
158fa9e4066Sahrens 	{ NULL },
15965cd9f28Seschrock 	{ "list",	zpool_do_list,		HELP_LIST		},
16065cd9f28Seschrock 	{ "iostat",	zpool_do_iostat,	HELP_IOSTAT		},
16165cd9f28Seschrock 	{ "status",	zpool_do_status,	HELP_STATUS		},
162fa9e4066Sahrens 	{ NULL },
16365cd9f28Seschrock 	{ "online",	zpool_do_online,	HELP_ONLINE		},
16465cd9f28Seschrock 	{ "offline",	zpool_do_offline,	HELP_OFFLINE		},
165ea8dc4b6Seschrock 	{ "clear",	zpool_do_clear,		HELP_CLEAR		},
1664263d13fSGeorge Wilson 	{ "reopen",	zpool_do_reopen,	HELP_REOPEN		},
167fa9e4066Sahrens 	{ NULL },
16865cd9f28Seschrock 	{ "attach",	zpool_do_attach,	HELP_ATTACH		},
16965cd9f28Seschrock 	{ "detach",	zpool_do_detach,	HELP_DETACH		},
17065cd9f28Seschrock 	{ "replace",	zpool_do_replace,	HELP_REPLACE		},
1711195e687SMark J Musante 	{ "split",	zpool_do_split,		HELP_SPLIT		},
172fa9e4066Sahrens 	{ NULL },
17365cd9f28Seschrock 	{ "scrub",	zpool_do_scrub,		HELP_SCRUB		},
174fa9e4066Sahrens 	{ NULL },
17565cd9f28Seschrock 	{ "import",	zpool_do_import,	HELP_IMPORT		},
17665cd9f28Seschrock 	{ "export",	zpool_do_export,	HELP_EXPORT		},
17706eeb2adSek110237 	{ "upgrade",	zpool_do_upgrade,	HELP_UPGRADE		},
178e9103aaeSGarrett D'Amore 	{ "reguid",	zpool_do_reguid,	HELP_REGUID		},
17906eeb2adSek110237 	{ NULL },
180b1b8ab34Slling 	{ "history",	zpool_do_history,	HELP_HISTORY		},
181b1b8ab34Slling 	{ "get",	zpool_do_get,		HELP_GET		},
182b1b8ab34Slling 	{ "set",	zpool_do_set,		HELP_SET		},
183fa9e4066Sahrens };
184fa9e4066Sahrens 
185fa9e4066Sahrens #define	NCOMMAND	(sizeof (command_table) / sizeof (command_table[0]))
186fa9e4066Sahrens 
187fa9e4066Sahrens zpool_command_t *current_command;
1882a6b87f0Sek110237 static char history_str[HIS_MAX_RECORD_LEN];
189fa9e4066Sahrens 
19026fd7700SKrishnendu Sadhukhan - Sun Microsystems static uint_t timestamp_fmt = NODATE;
19126fd7700SKrishnendu Sadhukhan - Sun Microsystems 
19265cd9f28Seschrock static const char *
19365cd9f28Seschrock get_usage(zpool_help_t idx) {
19465cd9f28Seschrock 	switch (idx) {
19565cd9f28Seschrock 	case HELP_ADD:
19665cd9f28Seschrock 		return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
19765cd9f28Seschrock 	case HELP_ATTACH:
19865cd9f28Seschrock 		return (gettext("\tattach [-f] <pool> <device> "
199e45ce728Sahrens 		    "<new-device>\n"));
200ea8dc4b6Seschrock 	case HELP_CLEAR:
201468c413aSTim Haley 		return (gettext("\tclear [-nF] <pool> [device]\n"));
20265cd9f28Seschrock 	case HELP_CREATE:
203990b4856Slling 		return (gettext("\tcreate [-fn] [-o property=value] ... \n"
2040a48a24eStimh 		    "\t    [-O file-system-property=value] ... \n"
205990b4856Slling 		    "\t    [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
20665cd9f28Seschrock 	case HELP_DESTROY:
20765cd9f28Seschrock 		return (gettext("\tdestroy [-f] <pool>\n"));
20865cd9f28Seschrock 	case HELP_DETACH:
20965cd9f28Seschrock 		return (gettext("\tdetach <pool> <device>\n"));
21065cd9f28Seschrock 	case HELP_EXPORT:
21165cd9f28Seschrock 		return (gettext("\texport [-f] <pool> ...\n"));
21206eeb2adSek110237 	case HELP_HISTORY:
213ecd6cf80Smarks 		return (gettext("\thistory [-il] [<pool>] ...\n"));
21465cd9f28Seschrock 	case HELP_IMPORT:
2154c58d714Sdarrenm 		return (gettext("\timport [-d dir] [-D]\n"
216f9af39baSGeorge Wilson 		    "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
2172f8aaab3Seschrock 		    "\timport [-o mntopts] [-o property=value] ... \n"
218f9af39baSGeorge Wilson 		    "\t    [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
219f9af39baSGeorge Wilson 		    "[-R root] [-F [-n]] -a\n"
2202f8aaab3Seschrock 		    "\timport [-o mntopts] [-o property=value] ... \n"
221f9af39baSGeorge Wilson 		    "\t    [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
222f9af39baSGeorge Wilson 		    "[-R root] [-F [-n]]\n"
223f9af39baSGeorge Wilson 		    "\t    <pool | id> [newpool]\n"));
22465cd9f28Seschrock 	case HELP_IOSTAT:
22526fd7700SKrishnendu Sadhukhan - Sun Microsystems 		return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
22665cd9f28Seschrock 		    "[count]]\n"));
22765cd9f28Seschrock 	case HELP_LIST:
228990b4856Slling 		return (gettext("\tlist [-H] [-o property[,...]] "
2293f9d6ad7SLin Ling 		    "[-T d|u] [pool] ... [interval [count]]\n"));
23065cd9f28Seschrock 	case HELP_OFFLINE:
231441d80aaSlling 		return (gettext("\toffline [-t] <pool> <device> ...\n"));
23265cd9f28Seschrock 	case HELP_ONLINE:
233441d80aaSlling 		return (gettext("\tonline <pool> <device> ...\n"));
23465cd9f28Seschrock 	case HELP_REPLACE:
23565cd9f28Seschrock 		return (gettext("\treplace [-f] <pool> <device> "
236e45ce728Sahrens 		    "[new-device]\n"));
23799653d4eSeschrock 	case HELP_REMOVE:
238fa94a07fSbrendan 		return (gettext("\tremove <pool> <device> ...\n"));
2394263d13fSGeorge Wilson 	case HELP_REOPEN:
2404263d13fSGeorge Wilson 		return (""); /* Undocumented command */
24165cd9f28Seschrock 	case HELP_SCRUB:
24265cd9f28Seschrock 		return (gettext("\tscrub [-s] <pool> ...\n"));
24365cd9f28Seschrock 	case HELP_STATUS:
2443f9d6ad7SLin Ling 		return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
2453f9d6ad7SLin Ling 		    "[count]]\n"));
246eaca9bbdSeschrock 	case HELP_UPGRADE:
247eaca9bbdSeschrock 		return (gettext("\tupgrade\n"
248eaca9bbdSeschrock 		    "\tupgrade -v\n"
249990b4856Slling 		    "\tupgrade [-V version] <-a | pool ...>\n"));
250b1b8ab34Slling 	case HELP_GET:
251e45ce728Sahrens 		return (gettext("\tget <\"all\" | property[,...]> "
252b1b8ab34Slling 		    "<pool> ...\n"));
253b1b8ab34Slling 	case HELP_SET:
254b1b8ab34Slling 		return (gettext("\tset <property=value> <pool> \n"));
2551195e687SMark J Musante 	case HELP_SPLIT:
2561195e687SMark J Musante 		return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
2571195e687SMark J Musante 		    "\t    [-o property=value] <pool> <newpool> "
2581195e687SMark J Musante 		    "[<device> ...]\n"));
259e9103aaeSGarrett D'Amore 	case HELP_REGUID:
260e9103aaeSGarrett D'Amore 		return (gettext("\treguid <pool>\n"));
26165cd9f28Seschrock 	}
26265cd9f28Seschrock 
26365cd9f28Seschrock 	abort();
26465cd9f28Seschrock 	/* NOTREACHED */
26565cd9f28Seschrock }
26665cd9f28Seschrock 
267fa9e4066Sahrens 
268fa9e4066Sahrens /*
269b1b8ab34Slling  * Callback routine that will print out a pool property value.
270b1b8ab34Slling  */
271990b4856Slling static int
272990b4856Slling print_prop_cb(int prop, void *cb)
273b1b8ab34Slling {
274b1b8ab34Slling 	FILE *fp = cb;
275b1b8ab34Slling 
276b24ab676SJeff Bonwick 	(void) fprintf(fp, "\t%-15s  ", zpool_prop_to_name(prop));
277b1b8ab34Slling 
278990b4856Slling 	if (zpool_prop_readonly(prop))
279990b4856Slling 		(void) fprintf(fp, "  NO   ");
280990b4856Slling 	else
281990b4856Slling 		(void) fprintf(fp, " YES   ");
282990b4856Slling 
283b1b8ab34Slling 	if (zpool_prop_values(prop) == NULL)
284b1b8ab34Slling 		(void) fprintf(fp, "-\n");
285b1b8ab34Slling 	else
286b1b8ab34Slling 		(void) fprintf(fp, "%s\n", zpool_prop_values(prop));
287b1b8ab34Slling 
288990b4856Slling 	return (ZPROP_CONT);
289b1b8ab34Slling }
290b1b8ab34Slling 
291b1b8ab34Slling /*
292fa9e4066Sahrens  * Display usage message.  If we're inside a command, display only the usage for
293fa9e4066Sahrens  * that command.  Otherwise, iterate over the entire command table and display
294fa9e4066Sahrens  * a complete usage message.
295fa9e4066Sahrens  */
296fa9e4066Sahrens void
29799653d4eSeschrock usage(boolean_t requested)
298fa9e4066Sahrens {
299fa9e4066Sahrens 	FILE *fp = requested ? stdout : stderr;
300fa9e4066Sahrens 
301fa9e4066Sahrens 	if (current_command == NULL) {
302fa9e4066Sahrens 		int i;
303fa9e4066Sahrens 
304fa9e4066Sahrens 		(void) fprintf(fp, gettext("usage: zpool command args ...\n"));
305fa9e4066Sahrens 		(void) fprintf(fp,
306fa9e4066Sahrens 		    gettext("where 'command' is one of the following:\n\n"));
307fa9e4066Sahrens 
308fa9e4066Sahrens 		for (i = 0; i < NCOMMAND; i++) {
309fa9e4066Sahrens 			if (command_table[i].name == NULL)
310fa9e4066Sahrens 				(void) fprintf(fp, "\n");
311fa9e4066Sahrens 			else
312fa9e4066Sahrens 				(void) fprintf(fp, "%s",
31365cd9f28Seschrock 				    get_usage(command_table[i].usage));
314fa9e4066Sahrens 		}
315fa9e4066Sahrens 	} else {
316fa9e4066Sahrens 		(void) fprintf(fp, gettext("usage:\n"));
31765cd9f28Seschrock 		(void) fprintf(fp, "%s", get_usage(current_command->usage));
318fa9e4066Sahrens 	}
319fa9e4066Sahrens 
320b1b8ab34Slling 	if (current_command != NULL &&
321b1b8ab34Slling 	    ((strcmp(current_command->name, "set") == 0) ||
322990b4856Slling 	    (strcmp(current_command->name, "get") == 0) ||
323990b4856Slling 	    (strcmp(current_command->name, "list") == 0))) {
324b1b8ab34Slling 
325b1b8ab34Slling 		(void) fprintf(fp,
326b1b8ab34Slling 		    gettext("\nthe following properties are supported:\n"));
327b1b8ab34Slling 
328b24ab676SJeff Bonwick 		(void) fprintf(fp, "\n\t%-15s  %s   %s\n\n",
329990b4856Slling 		    "PROPERTY", "EDIT", "VALUES");
330b1b8ab34Slling 
331b1b8ab34Slling 		/* Iterate over all properties */
332990b4856Slling 		(void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
333990b4856Slling 		    ZFS_TYPE_POOL);
334b1b8ab34Slling 	}
335b1b8ab34Slling 
336e9dbad6fSeschrock 	/*
337e9dbad6fSeschrock 	 * See comments at end of main().
338e9dbad6fSeschrock 	 */
339e9dbad6fSeschrock 	if (getenv("ZFS_ABORT") != NULL) {
340e9dbad6fSeschrock 		(void) printf("dumping core by request\n");
341e9dbad6fSeschrock 		abort();
342e9dbad6fSeschrock 	}
343e9dbad6fSeschrock 
344fa9e4066Sahrens 	exit(requested ? 0 : 2);
345fa9e4066Sahrens }
346fa9e4066Sahrens 
347fa9e4066Sahrens void
3488654d025Sperrin print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
3498654d025Sperrin     boolean_t print_logs)
350fa9e4066Sahrens {
351fa9e4066Sahrens 	nvlist_t **child;
352fa9e4066Sahrens 	uint_t c, children;
353afefbcddSeschrock 	char *vname;
354fa9e4066Sahrens 
355fa9e4066Sahrens 	if (name != NULL)
356fa9e4066Sahrens 		(void) printf("\t%*s%s\n", indent, "", name);
357fa9e4066Sahrens 
358fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
359fa9e4066Sahrens 	    &child, &children) != 0)
360fa9e4066Sahrens 		return;
361fa9e4066Sahrens 
362afefbcddSeschrock 	for (c = 0; c < children; c++) {
3638654d025Sperrin 		uint64_t is_log = B_FALSE;
3648654d025Sperrin 
3658654d025Sperrin 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3668654d025Sperrin 		    &is_log);
3678654d025Sperrin 		if ((is_log && !print_logs) || (!is_log && print_logs))
3688654d025Sperrin 			continue;
3698654d025Sperrin 
37088ecc943SGeorge Wilson 		vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3718654d025Sperrin 		print_vdev_tree(zhp, vname, child[c], indent + 2,
3728654d025Sperrin 		    B_FALSE);
373afefbcddSeschrock 		free(vname);
374afefbcddSeschrock 	}
375fa9e4066Sahrens }
376fa9e4066Sahrens 
377fa9e4066Sahrens /*
378990b4856Slling  * Add a property pair (name, string-value) into a property nvlist.
379990b4856Slling  */
380990b4856Slling static int
3810a48a24eStimh add_prop_list(const char *propname, char *propval, nvlist_t **props,
3820a48a24eStimh     boolean_t poolprop)
383990b4856Slling {
3840a48a24eStimh 	zpool_prop_t prop = ZPROP_INVAL;
3850a48a24eStimh 	zfs_prop_t fprop;
386990b4856Slling 	nvlist_t *proplist;
3870a48a24eStimh 	const char *normnm;
3880a48a24eStimh 	char *strval;
389990b4856Slling 
390990b4856Slling 	if (*props == NULL &&
391990b4856Slling 	    nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
392990b4856Slling 		(void) fprintf(stderr,
393990b4856Slling 		    gettext("internal error: out of memory\n"));
394990b4856Slling 		return (1);
395990b4856Slling 	}
396990b4856Slling 
397990b4856Slling 	proplist = *props;
398990b4856Slling 
3990a48a24eStimh 	if (poolprop) {
400990b4856Slling 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
401990b4856Slling 			(void) fprintf(stderr, gettext("property '%s' is "
402990b4856Slling 			    "not a valid pool property\n"), propname);
403990b4856Slling 			return (2);
404990b4856Slling 		}
4050a48a24eStimh 		normnm = zpool_prop_to_name(prop);
4060a48a24eStimh 	} else {
40714843421SMatthew Ahrens 		if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
4080a48a24eStimh 			normnm = zfs_prop_to_name(fprop);
40914843421SMatthew Ahrens 		} else {
41014843421SMatthew Ahrens 			normnm = propname;
41114843421SMatthew Ahrens 		}
4120a48a24eStimh 	}
413990b4856Slling 
4140a48a24eStimh 	if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
4150a48a24eStimh 	    prop != ZPOOL_PROP_CACHEFILE) {
416990b4856Slling 		(void) fprintf(stderr, gettext("property '%s' "
417990b4856Slling 		    "specified multiple times\n"), propname);
418990b4856Slling 		return (2);
419990b4856Slling 	}
420990b4856Slling 
4210a48a24eStimh 	if (nvlist_add_string(proplist, normnm, propval) != 0) {
422990b4856Slling 		(void) fprintf(stderr, gettext("internal "
423990b4856Slling 		    "error: out of memory\n"));
424990b4856Slling 		return (1);
425990b4856Slling 	}
426990b4856Slling 
427990b4856Slling 	return (0);
428990b4856Slling }
429990b4856Slling 
430990b4856Slling /*
431fa9e4066Sahrens  * zpool add [-fn] <pool> <vdev> ...
432fa9e4066Sahrens  *
433fa9e4066Sahrens  *	-f	Force addition of devices, even if they appear in use
434fa9e4066Sahrens  *	-n	Do not add the devices, but display the resulting layout if
435fa9e4066Sahrens  *		they were to be added.
436fa9e4066Sahrens  *
437fa9e4066Sahrens  * Adds the given vdevs to 'pool'.  As with create, the bulk of this work is
438fa9e4066Sahrens  * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
439fa9e4066Sahrens  * libzfs.
440fa9e4066Sahrens  */
441fa9e4066Sahrens int
442fa9e4066Sahrens zpool_do_add(int argc, char **argv)
443fa9e4066Sahrens {
44499653d4eSeschrock 	boolean_t force = B_FALSE;
44599653d4eSeschrock 	boolean_t dryrun = B_FALSE;
446fa9e4066Sahrens 	int c;
447fa9e4066Sahrens 	nvlist_t *nvroot;
448fa9e4066Sahrens 	char *poolname;
449fa9e4066Sahrens 	int ret;
450fa9e4066Sahrens 	zpool_handle_t *zhp;
451fa9e4066Sahrens 	nvlist_t *config;
452fa9e4066Sahrens 
453fa9e4066Sahrens 	/* check options */
454fa9e4066Sahrens 	while ((c = getopt(argc, argv, "fn")) != -1) {
455fa9e4066Sahrens 		switch (c) {
456fa9e4066Sahrens 		case 'f':
45799653d4eSeschrock 			force = B_TRUE;
458fa9e4066Sahrens 			break;
459fa9e4066Sahrens 		case 'n':
46099653d4eSeschrock 			dryrun = B_TRUE;
461fa9e4066Sahrens 			break;
462fa9e4066Sahrens 		case '?':
463fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
464fa9e4066Sahrens 			    optopt);
46599653d4eSeschrock 			usage(B_FALSE);
466fa9e4066Sahrens 		}
467fa9e4066Sahrens 	}
468fa9e4066Sahrens 
469fa9e4066Sahrens 	argc -= optind;
470fa9e4066Sahrens 	argv += optind;
471fa9e4066Sahrens 
472fa9e4066Sahrens 	/* get pool name and check number of arguments */
473fa9e4066Sahrens 	if (argc < 1) {
474fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
47599653d4eSeschrock 		usage(B_FALSE);
476fa9e4066Sahrens 	}
477fa9e4066Sahrens 	if (argc < 2) {
478fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing vdev specification\n"));
47999653d4eSeschrock 		usage(B_FALSE);
480fa9e4066Sahrens 	}
481fa9e4066Sahrens 
482fa9e4066Sahrens 	poolname = argv[0];
483fa9e4066Sahrens 
484fa9e4066Sahrens 	argc--;
485fa9e4066Sahrens 	argv++;
486fa9e4066Sahrens 
48799653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
488fa9e4066Sahrens 		return (1);
489fa9e4066Sahrens 
490088e9d47Seschrock 	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
491fa9e4066Sahrens 		(void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
492fa9e4066Sahrens 		    poolname);
493fa9e4066Sahrens 		zpool_close(zhp);
494fa9e4066Sahrens 		return (1);
495fa9e4066Sahrens 	}
496fa9e4066Sahrens 
497fa9e4066Sahrens 	/* pass off to get_vdev_spec for processing */
498705040edSEric Taylor 	nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
499705040edSEric Taylor 	    argc, argv);
500fa9e4066Sahrens 	if (nvroot == NULL) {
501fa9e4066Sahrens 		zpool_close(zhp);
502fa9e4066Sahrens 		return (1);
503fa9e4066Sahrens 	}
504fa9e4066Sahrens 
505fa9e4066Sahrens 	if (dryrun) {
506fa9e4066Sahrens 		nvlist_t *poolnvroot;
507fa9e4066Sahrens 
508fa9e4066Sahrens 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
509fa9e4066Sahrens 		    &poolnvroot) == 0);
510fa9e4066Sahrens 
511fa9e4066Sahrens 		(void) printf(gettext("would update '%s' to the following "
512fa9e4066Sahrens 		    "configuration:\n"), zpool_get_name(zhp));
513fa9e4066Sahrens 
5148654d025Sperrin 		/* print original main pool and new tree */
5158654d025Sperrin 		print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
5168654d025Sperrin 		print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
5178654d025Sperrin 
5188654d025Sperrin 		/* Do the same for the logs */
5198654d025Sperrin 		if (num_logs(poolnvroot) > 0) {
5208654d025Sperrin 			print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
5218654d025Sperrin 			print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
5228654d025Sperrin 		} else if (num_logs(nvroot) > 0) {
5238654d025Sperrin 			print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
5248654d025Sperrin 		}
525fa9e4066Sahrens 
526fa9e4066Sahrens 		ret = 0;
527fa9e4066Sahrens 	} else {
528fa9e4066Sahrens 		ret = (zpool_add(zhp, nvroot) != 0);
529fa9e4066Sahrens 	}
530fa9e4066Sahrens 
53199653d4eSeschrock 	nvlist_free(nvroot);
53299653d4eSeschrock 	zpool_close(zhp);
53399653d4eSeschrock 
53499653d4eSeschrock 	return (ret);
53599653d4eSeschrock }
53699653d4eSeschrock 
53799653d4eSeschrock /*
538fa94a07fSbrendan  * zpool remove  <pool> <vdev> ...
53999653d4eSeschrock  *
5403f9d6ad7SLin Ling  * Removes the given vdev from the pool.  Currently, this supports removing
5413f9d6ad7SLin Ling  * spares, cache, and log devices from the pool.
54299653d4eSeschrock  */
54399653d4eSeschrock int
54499653d4eSeschrock zpool_do_remove(int argc, char **argv)
54599653d4eSeschrock {
54699653d4eSeschrock 	char *poolname;
547fa94a07fSbrendan 	int i, ret = 0;
54899653d4eSeschrock 	zpool_handle_t *zhp;
54999653d4eSeschrock 
55099653d4eSeschrock 	argc--;
55199653d4eSeschrock 	argv++;
55299653d4eSeschrock 
55399653d4eSeschrock 	/* get pool name and check number of arguments */
55499653d4eSeschrock 	if (argc < 1) {
55599653d4eSeschrock 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
55699653d4eSeschrock 		usage(B_FALSE);
55799653d4eSeschrock 	}
55899653d4eSeschrock 	if (argc < 2) {
55999653d4eSeschrock 		(void) fprintf(stderr, gettext("missing device\n"));
56099653d4eSeschrock 		usage(B_FALSE);
56199653d4eSeschrock 	}
56299653d4eSeschrock 
56399653d4eSeschrock 	poolname = argv[0];
56499653d4eSeschrock 
56599653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
56699653d4eSeschrock 		return (1);
56799653d4eSeschrock 
568fa94a07fSbrendan 	for (i = 1; i < argc; i++) {
569fa94a07fSbrendan 		if (zpool_vdev_remove(zhp, argv[i]) != 0)
570fa94a07fSbrendan 			ret = 1;
571fa94a07fSbrendan 	}
57299653d4eSeschrock 
573fa9e4066Sahrens 	return (ret);
574fa9e4066Sahrens }
575fa9e4066Sahrens 
576fa9e4066Sahrens /*
5770a48a24eStimh  * zpool create [-fn] [-o property=value] ...
5780a48a24eStimh  *		[-O file-system-property=value] ...
5790a48a24eStimh  *		[-R root] [-m mountpoint] <pool> <dev> ...
580fa9e4066Sahrens  *
581fa9e4066Sahrens  *	-f	Force creation, even if devices appear in use
582fa9e4066Sahrens  *	-n	Do not create the pool, but display the resulting layout if it
583fa9e4066Sahrens  *		were to be created.
584fa9e4066Sahrens  *      -R	Create a pool under an alternate root
585fa9e4066Sahrens  *      -m	Set default mountpoint for the root dataset.  By default it's
586fa9e4066Sahrens  *      	'/<pool>'
587990b4856Slling  *	-o	Set property=value.
5880a48a24eStimh  *	-O	Set fsproperty=value in the pool's root file system
589fa9e4066Sahrens  *
590b1b8ab34Slling  * Creates the named pool according to the given vdev specification.  The
591fa9e4066Sahrens  * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c.  Once
592fa9e4066Sahrens  * we get the nvlist back from get_vdev_spec(), we either print out the contents
593fa9e4066Sahrens  * (if '-n' was specified), or pass it to libzfs to do the creation.
594fa9e4066Sahrens  */
595fa9e4066Sahrens int
596fa9e4066Sahrens zpool_do_create(int argc, char **argv)
597fa9e4066Sahrens {
59899653d4eSeschrock 	boolean_t force = B_FALSE;
59999653d4eSeschrock 	boolean_t dryrun = B_FALSE;
600fa9e4066Sahrens 	int c;
601990b4856Slling 	nvlist_t *nvroot = NULL;
602fa9e4066Sahrens 	char *poolname;
603990b4856Slling 	int ret = 1;
604fa9e4066Sahrens 	char *altroot = NULL;
605fa9e4066Sahrens 	char *mountpoint = NULL;
6060a48a24eStimh 	nvlist_t *fsprops = NULL;
607990b4856Slling 	nvlist_t *props = NULL;
6082f8aaab3Seschrock 	char *propval;
609fa9e4066Sahrens 
610fa9e4066Sahrens 	/* check options */
6110a48a24eStimh 	while ((c = getopt(argc, argv, ":fnR:m:o:O:")) != -1) {
612fa9e4066Sahrens 		switch (c) {
613fa9e4066Sahrens 		case 'f':
61499653d4eSeschrock 			force = B_TRUE;
615fa9e4066Sahrens 			break;
616fa9e4066Sahrens 		case 'n':
61799653d4eSeschrock 			dryrun = B_TRUE;
618fa9e4066Sahrens 			break;
619fa9e4066Sahrens 		case 'R':
620fa9e4066Sahrens 			altroot = optarg;
621990b4856Slling 			if (add_prop_list(zpool_prop_to_name(
6220a48a24eStimh 			    ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
623990b4856Slling 				goto errout;
6242f8aaab3Seschrock 			if (nvlist_lookup_string(props,
6252f8aaab3Seschrock 			    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
6262f8aaab3Seschrock 			    &propval) == 0)
6272f8aaab3Seschrock 				break;
628990b4856Slling 			if (add_prop_list(zpool_prop_to_name(
6290a48a24eStimh 			    ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
630990b4856Slling 				goto errout;
631fa9e4066Sahrens 			break;
632fa9e4066Sahrens 		case 'm':
633fa9e4066Sahrens 			mountpoint = optarg;
634fa9e4066Sahrens 			break;
635990b4856Slling 		case 'o':
636990b4856Slling 			if ((propval = strchr(optarg, '=')) == NULL) {
637990b4856Slling 				(void) fprintf(stderr, gettext("missing "
638990b4856Slling 				    "'=' for -o option\n"));
639990b4856Slling 				goto errout;
640990b4856Slling 			}
641990b4856Slling 			*propval = '\0';
642990b4856Slling 			propval++;
643990b4856Slling 
6440a48a24eStimh 			if (add_prop_list(optarg, propval, &props, B_TRUE))
6450a48a24eStimh 				goto errout;
6460a48a24eStimh 			break;
6470a48a24eStimh 		case 'O':
6480a48a24eStimh 			if ((propval = strchr(optarg, '=')) == NULL) {
6490a48a24eStimh 				(void) fprintf(stderr, gettext("missing "
6500a48a24eStimh 				    "'=' for -O option\n"));
6510a48a24eStimh 				goto errout;
6520a48a24eStimh 			}
6530a48a24eStimh 			*propval = '\0';
6540a48a24eStimh 			propval++;
6550a48a24eStimh 
6560a48a24eStimh 			if (add_prop_list(optarg, propval, &fsprops, B_FALSE))
657990b4856Slling 				goto errout;
658990b4856Slling 			break;
659fa9e4066Sahrens 		case ':':
660fa9e4066Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
661fa9e4066Sahrens 			    "'%c' option\n"), optopt);
662990b4856Slling 			goto badusage;
663fa9e4066Sahrens 		case '?':
664fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
665fa9e4066Sahrens 			    optopt);
666990b4856Slling 			goto badusage;
667fa9e4066Sahrens 		}
668fa9e4066Sahrens 	}
669fa9e4066Sahrens 
670fa9e4066Sahrens 	argc -= optind;
671fa9e4066Sahrens 	argv += optind;
672fa9e4066Sahrens 
673fa9e4066Sahrens 	/* get pool name and check number of arguments */
674fa9e4066Sahrens 	if (argc < 1) {
675fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
676990b4856Slling 		goto badusage;
677fa9e4066Sahrens 	}
678fa9e4066Sahrens 	if (argc < 2) {
679fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing vdev specification\n"));
680990b4856Slling 		goto badusage;
681fa9e4066Sahrens 	}
682fa9e4066Sahrens 
683fa9e4066Sahrens 	poolname = argv[0];
684fa9e4066Sahrens 
685fa9e4066Sahrens 	/*
686fa9e4066Sahrens 	 * As a special case, check for use of '/' in the name, and direct the
687fa9e4066Sahrens 	 * user to use 'zfs create' instead.
688fa9e4066Sahrens 	 */
689fa9e4066Sahrens 	if (strchr(poolname, '/') != NULL) {
690fa9e4066Sahrens 		(void) fprintf(stderr, gettext("cannot create '%s': invalid "
691fa9e4066Sahrens 		    "character '/' in pool name\n"), poolname);
692fa9e4066Sahrens 		(void) fprintf(stderr, gettext("use 'zfs create' to "
693fa9e4066Sahrens 		    "create a dataset\n"));
694990b4856Slling 		goto errout;
695fa9e4066Sahrens 	}
696fa9e4066Sahrens 
697fa9e4066Sahrens 	/* pass off to get_vdev_spec for bulk processing */
698705040edSEric Taylor 	nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
699705040edSEric Taylor 	    argc - 1, argv + 1);
700fa9e4066Sahrens 	if (nvroot == NULL)
7010a48a24eStimh 		goto errout;
702fa9e4066Sahrens 
70399653d4eSeschrock 	/* make_root_vdev() allows 0 toplevel children if there are spares */
704b7b97454Sperrin 	if (!zfs_allocatable_devs(nvroot)) {
70599653d4eSeschrock 		(void) fprintf(stderr, gettext("invalid vdev "
70699653d4eSeschrock 		    "specification: at least one toplevel vdev must be "
70799653d4eSeschrock 		    "specified\n"));
708990b4856Slling 		goto errout;
70999653d4eSeschrock 	}
71099653d4eSeschrock 
71199653d4eSeschrock 
712fa9e4066Sahrens 	if (altroot != NULL && altroot[0] != '/') {
713fa9e4066Sahrens 		(void) fprintf(stderr, gettext("invalid alternate root '%s': "
714e9dbad6fSeschrock 		    "must be an absolute path\n"), altroot);
715990b4856Slling 		goto errout;
716fa9e4066Sahrens 	}
717fa9e4066Sahrens 
718fa9e4066Sahrens 	/*
719fa9e4066Sahrens 	 * Check the validity of the mountpoint and direct the user to use the
720fa9e4066Sahrens 	 * '-m' mountpoint option if it looks like its in use.
721fa9e4066Sahrens 	 */
722fa9e4066Sahrens 	if (mountpoint == NULL ||
723fa9e4066Sahrens 	    (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
724fa9e4066Sahrens 	    strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
725fa9e4066Sahrens 		char buf[MAXPATHLEN];
72611022c7cStimh 		DIR *dirp;
727fa9e4066Sahrens 
728fa9e4066Sahrens 		if (mountpoint && mountpoint[0] != '/') {
729fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid mountpoint "
730fa9e4066Sahrens 			    "'%s': must be an absolute path, 'legacy', or "
731fa9e4066Sahrens 			    "'none'\n"), mountpoint);
732990b4856Slling 			goto errout;
733fa9e4066Sahrens 		}
734fa9e4066Sahrens 
735fa9e4066Sahrens 		if (mountpoint == NULL) {
736fa9e4066Sahrens 			if (altroot != NULL)
737fa9e4066Sahrens 				(void) snprintf(buf, sizeof (buf), "%s/%s",
738fa9e4066Sahrens 				    altroot, poolname);
739fa9e4066Sahrens 			else
740fa9e4066Sahrens 				(void) snprintf(buf, sizeof (buf), "/%s",
741fa9e4066Sahrens 				    poolname);
742fa9e4066Sahrens 		} else {
743fa9e4066Sahrens 			if (altroot != NULL)
744fa9e4066Sahrens 				(void) snprintf(buf, sizeof (buf), "%s%s",
745fa9e4066Sahrens 				    altroot, mountpoint);
746fa9e4066Sahrens 			else
747fa9e4066Sahrens 				(void) snprintf(buf, sizeof (buf), "%s",
748fa9e4066Sahrens 				    mountpoint);
749fa9e4066Sahrens 		}
750fa9e4066Sahrens 
75111022c7cStimh 		if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
75211022c7cStimh 			(void) fprintf(stderr, gettext("mountpoint '%s' : "
75311022c7cStimh 			    "%s\n"), buf, strerror(errno));
754fa9e4066Sahrens 			(void) fprintf(stderr, gettext("use '-m' "
755fa9e4066Sahrens 			    "option to provide a different default\n"));
756990b4856Slling 			goto errout;
75711022c7cStimh 		} else if (dirp) {
75811022c7cStimh 			int count = 0;
75911022c7cStimh 
76011022c7cStimh 			while (count < 3 && readdir(dirp) != NULL)
76111022c7cStimh 				count++;
76211022c7cStimh 			(void) closedir(dirp);
76311022c7cStimh 
76411022c7cStimh 			if (count > 2) {
76511022c7cStimh 				(void) fprintf(stderr, gettext("mountpoint "
76611022c7cStimh 				    "'%s' exists and is not empty\n"), buf);
76711022c7cStimh 				(void) fprintf(stderr, gettext("use '-m' "
76811022c7cStimh 				    "option to provide a "
76911022c7cStimh 				    "different default\n"));
77011022c7cStimh 				goto errout;
77111022c7cStimh 			}
772fa9e4066Sahrens 		}
773fa9e4066Sahrens 	}
774fa9e4066Sahrens 
775fa9e4066Sahrens 	if (dryrun) {
776fa9e4066Sahrens 		/*
777fa9e4066Sahrens 		 * For a dry run invocation, print out a basic message and run
778fa9e4066Sahrens 		 * through all the vdevs in the list and print out in an
779fa9e4066Sahrens 		 * appropriate hierarchy.
780fa9e4066Sahrens 		 */
781fa9e4066Sahrens 		(void) printf(gettext("would create '%s' with the "
782fa9e4066Sahrens 		    "following layout:\n\n"), poolname);
783fa9e4066Sahrens 
7848654d025Sperrin 		print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
7858654d025Sperrin 		if (num_logs(nvroot) > 0)
7868654d025Sperrin 			print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
787fa9e4066Sahrens 
788fa9e4066Sahrens 		ret = 0;
789fa9e4066Sahrens 	} else {
790fa9e4066Sahrens 		/*
791fa9e4066Sahrens 		 * Hand off to libzfs.
792fa9e4066Sahrens 		 */
7930a48a24eStimh 		if (zpool_create(g_zfs, poolname,
7940a48a24eStimh 		    nvroot, props, fsprops) == 0) {
79599653d4eSeschrock 			zfs_handle_t *pool = zfs_open(g_zfs, poolname,
796fa9e4066Sahrens 			    ZFS_TYPE_FILESYSTEM);
797fa9e4066Sahrens 			if (pool != NULL) {
798fa9e4066Sahrens 				if (mountpoint != NULL)
799fa9e4066Sahrens 					verify(zfs_prop_set(pool,
800e9dbad6fSeschrock 					    zfs_prop_to_name(
801e9dbad6fSeschrock 					    ZFS_PROP_MOUNTPOINT),
802fa9e4066Sahrens 					    mountpoint) == 0);
803fa9e4066Sahrens 				if (zfs_mount(pool, NULL, 0) == 0)
804da6c28aaSamw 					ret = zfs_shareall(pool);
805fa9e4066Sahrens 				zfs_close(pool);
806fa9e4066Sahrens 			}
80799653d4eSeschrock 		} else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
80899653d4eSeschrock 			(void) fprintf(stderr, gettext("pool name may have "
80999653d4eSeschrock 			    "been omitted\n"));
810fa9e4066Sahrens 		}
811fa9e4066Sahrens 	}
812fa9e4066Sahrens 
813990b4856Slling errout:
814fa9e4066Sahrens 	nvlist_free(nvroot);
8150a48a24eStimh 	nvlist_free(fsprops);
816990b4856Slling 	nvlist_free(props);
817fa9e4066Sahrens 	return (ret);
818990b4856Slling badusage:
8190a48a24eStimh 	nvlist_free(fsprops);
820990b4856Slling 	nvlist_free(props);
821990b4856Slling 	usage(B_FALSE);
822990b4856Slling 	return (2);
823fa9e4066Sahrens }
824fa9e4066Sahrens 
825fa9e4066Sahrens /*
826fa9e4066Sahrens  * zpool destroy <pool>
827fa9e4066Sahrens  *
828fa9e4066Sahrens  * 	-f	Forcefully unmount any datasets
829fa9e4066Sahrens  *
830fa9e4066Sahrens  * Destroy the given pool.  Automatically unmounts any datasets in the pool.
831fa9e4066Sahrens  */
832fa9e4066Sahrens int
833fa9e4066Sahrens zpool_do_destroy(int argc, char **argv)
834fa9e4066Sahrens {
83599653d4eSeschrock 	boolean_t force = B_FALSE;
836fa9e4066Sahrens 	int c;
837fa9e4066Sahrens 	char *pool;
838fa9e4066Sahrens 	zpool_handle_t *zhp;
839fa9e4066Sahrens 	int ret;
840fa9e4066Sahrens 
841fa9e4066Sahrens 	/* check options */
842fa9e4066Sahrens 	while ((c = getopt(argc, argv, "f")) != -1) {
843fa9e4066Sahrens 		switch (c) {
844fa9e4066Sahrens 		case 'f':
84599653d4eSeschrock 			force = B_TRUE;
846fa9e4066Sahrens 			break;
847fa9e4066Sahrens 		case '?':
848fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
849fa9e4066Sahrens 			    optopt);
85099653d4eSeschrock 			usage(B_FALSE);
851fa9e4066Sahrens 		}
852fa9e4066Sahrens 	}
853fa9e4066Sahrens 
854fa9e4066Sahrens 	argc -= optind;
855fa9e4066Sahrens 	argv += optind;
856fa9e4066Sahrens 
857fa9e4066Sahrens 	/* check arguments */
858fa9e4066Sahrens 	if (argc < 1) {
859fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool argument\n"));
86099653d4eSeschrock 		usage(B_FALSE);
861fa9e4066Sahrens 	}
862fa9e4066Sahrens 	if (argc > 1) {
863fa9e4066Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
86499653d4eSeschrock 		usage(B_FALSE);
865fa9e4066Sahrens 	}
866fa9e4066Sahrens 
867fa9e4066Sahrens 	pool = argv[0];
868fa9e4066Sahrens 
86999653d4eSeschrock 	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
870fa9e4066Sahrens 		/*
871fa9e4066Sahrens 		 * As a special case, check for use of '/' in the name, and
872fa9e4066Sahrens 		 * direct the user to use 'zfs destroy' instead.
873fa9e4066Sahrens 		 */
874fa9e4066Sahrens 		if (strchr(pool, '/') != NULL)
875fa9e4066Sahrens 			(void) fprintf(stderr, gettext("use 'zfs destroy' to "
876fa9e4066Sahrens 			    "destroy a dataset\n"));
877fa9e4066Sahrens 		return (1);
878fa9e4066Sahrens 	}
879fa9e4066Sahrens 
880f3861e1aSahl 	if (zpool_disable_datasets(zhp, force) != 0) {
881fa9e4066Sahrens 		(void) fprintf(stderr, gettext("could not destroy '%s': "
882fa9e4066Sahrens 		    "could not unmount datasets\n"), zpool_get_name(zhp));
883fa9e4066Sahrens 		return (1);
884fa9e4066Sahrens 	}
885fa9e4066Sahrens 
886fa9e4066Sahrens 	ret = (zpool_destroy(zhp) != 0);
887fa9e4066Sahrens 
888fa9e4066Sahrens 	zpool_close(zhp);
889fa9e4066Sahrens 
890fa9e4066Sahrens 	return (ret);
891fa9e4066Sahrens }
892fa9e4066Sahrens 
893fa9e4066Sahrens /*
894fa9e4066Sahrens  * zpool export [-f] <pool> ...
895fa9e4066Sahrens  *
896fa9e4066Sahrens  *	-f	Forcefully unmount datasets
897fa9e4066Sahrens  *
898b1b8ab34Slling  * Export the given pools.  By default, the command will attempt to cleanly
899fa9e4066Sahrens  * unmount any active datasets within the pool.  If the '-f' flag is specified,
900fa9e4066Sahrens  * then the datasets will be forcefully unmounted.
901fa9e4066Sahrens  */
902fa9e4066Sahrens int
903fa9e4066Sahrens zpool_do_export(int argc, char **argv)
904fa9e4066Sahrens {
90599653d4eSeschrock 	boolean_t force = B_FALSE;
906394ab0cbSGeorge Wilson 	boolean_t hardforce = B_FALSE;
907fa9e4066Sahrens 	int c;
908fa9e4066Sahrens 	zpool_handle_t *zhp;
909fa9e4066Sahrens 	int ret;
910fa9e4066Sahrens 	int i;
911fa9e4066Sahrens 
912fa9e4066Sahrens 	/* check options */
913394ab0cbSGeorge Wilson 	while ((c = getopt(argc, argv, "fF")) != -1) {
914fa9e4066Sahrens 		switch (c) {
915fa9e4066Sahrens 		case 'f':
91699653d4eSeschrock 			force = B_TRUE;
917fa9e4066Sahrens 			break;
918394ab0cbSGeorge Wilson 		case 'F':
919394ab0cbSGeorge Wilson 			hardforce = B_TRUE;
920394ab0cbSGeorge Wilson 			break;
921fa9e4066Sahrens 		case '?':
922fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
923fa9e4066Sahrens 			    optopt);
92499653d4eSeschrock 			usage(B_FALSE);
925fa9e4066Sahrens 		}
926fa9e4066Sahrens 	}
927fa9e4066Sahrens 
928fa9e4066Sahrens 	argc -= optind;
929fa9e4066Sahrens 	argv += optind;
930fa9e4066Sahrens 
931fa9e4066Sahrens 	/* check arguments */
932fa9e4066Sahrens 	if (argc < 1) {
933fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool argument\n"));
93499653d4eSeschrock 		usage(B_FALSE);
935fa9e4066Sahrens 	}
936fa9e4066Sahrens 
937fa9e4066Sahrens 	ret = 0;
938fa9e4066Sahrens 	for (i = 0; i < argc; i++) {
93999653d4eSeschrock 		if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
940fa9e4066Sahrens 			ret = 1;
941fa9e4066Sahrens 			continue;
942fa9e4066Sahrens 		}
943fa9e4066Sahrens 
944f3861e1aSahl 		if (zpool_disable_datasets(zhp, force) != 0) {
945fa9e4066Sahrens 			ret = 1;
946fa9e4066Sahrens 			zpool_close(zhp);
947fa9e4066Sahrens 			continue;
948fa9e4066Sahrens 		}
949fa9e4066Sahrens 
950394ab0cbSGeorge Wilson 		if (hardforce) {
951394ab0cbSGeorge Wilson 			if (zpool_export_force(zhp) != 0)
952fa9e4066Sahrens 				ret = 1;
953394ab0cbSGeorge Wilson 		} else if (zpool_export(zhp, force) != 0) {
954394ab0cbSGeorge Wilson 			ret = 1;
955394ab0cbSGeorge Wilson 		}
956fa9e4066Sahrens 
957fa9e4066Sahrens 		zpool_close(zhp);
958fa9e4066Sahrens 	}
959fa9e4066Sahrens 
960fa9e4066Sahrens 	return (ret);
961fa9e4066Sahrens }
962fa9e4066Sahrens 
963fa9e4066Sahrens /*
964fa9e4066Sahrens  * Given a vdev configuration, determine the maximum width needed for the device
965fa9e4066Sahrens  * name column.
966fa9e4066Sahrens  */
967fa9e4066Sahrens static int
968c67d9675Seschrock max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
969fa9e4066Sahrens {
97088ecc943SGeorge Wilson 	char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
971fa9e4066Sahrens 	nvlist_t **child;
972fa9e4066Sahrens 	uint_t c, children;
973fa9e4066Sahrens 	int ret;
974fa9e4066Sahrens 
975fa9e4066Sahrens 	if (strlen(name) + depth > max)
976fa9e4066Sahrens 		max = strlen(name) + depth;
977fa9e4066Sahrens 
978afefbcddSeschrock 	free(name);
979afefbcddSeschrock 
98099653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
98199653d4eSeschrock 	    &child, &children) == 0) {
982fa9e4066Sahrens 		for (c = 0; c < children; c++)
98399653d4eSeschrock 			if ((ret = max_width(zhp, child[c], depth + 2,
98499653d4eSeschrock 			    max)) > max)
985fa9e4066Sahrens 				max = ret;
98699653d4eSeschrock 	}
98799653d4eSeschrock 
988fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
989fa94a07fSbrendan 	    &child, &children) == 0) {
990fa94a07fSbrendan 		for (c = 0; c < children; c++)
991fa94a07fSbrendan 			if ((ret = max_width(zhp, child[c], depth + 2,
992fa94a07fSbrendan 			    max)) > max)
993fa94a07fSbrendan 				max = ret;
994fa94a07fSbrendan 	}
995fa94a07fSbrendan 
99699653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
99799653d4eSeschrock 	    &child, &children) == 0) {
99899653d4eSeschrock 		for (c = 0; c < children; c++)
99999653d4eSeschrock 			if ((ret = max_width(zhp, child[c], depth + 2,
100099653d4eSeschrock 			    max)) > max)
100199653d4eSeschrock 				max = ret;
100299653d4eSeschrock 	}
100399653d4eSeschrock 
1004fa9e4066Sahrens 
1005fa9e4066Sahrens 	return (max);
1006fa9e4066Sahrens }
1007fa9e4066Sahrens 
1008e6ca193dSGeorge Wilson typedef struct spare_cbdata {
1009e6ca193dSGeorge Wilson 	uint64_t	cb_guid;
1010e6ca193dSGeorge Wilson 	zpool_handle_t	*cb_zhp;
1011e6ca193dSGeorge Wilson } spare_cbdata_t;
1012e6ca193dSGeorge Wilson 
1013e6ca193dSGeorge Wilson static boolean_t
1014e6ca193dSGeorge Wilson find_vdev(nvlist_t *nv, uint64_t search)
1015e6ca193dSGeorge Wilson {
1016e6ca193dSGeorge Wilson 	uint64_t guid;
1017e6ca193dSGeorge Wilson 	nvlist_t **child;
1018e6ca193dSGeorge Wilson 	uint_t c, children;
1019e6ca193dSGeorge Wilson 
1020e6ca193dSGeorge Wilson 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1021e6ca193dSGeorge Wilson 	    search == guid)
1022e6ca193dSGeorge Wilson 		return (B_TRUE);
1023e6ca193dSGeorge Wilson 
1024e6ca193dSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1025e6ca193dSGeorge Wilson 	    &child, &children) == 0) {
1026e6ca193dSGeorge Wilson 		for (c = 0; c < children; c++)
1027e6ca193dSGeorge Wilson 			if (find_vdev(child[c], search))
1028e6ca193dSGeorge Wilson 				return (B_TRUE);
1029e6ca193dSGeorge Wilson 	}
1030e6ca193dSGeorge Wilson 
1031e6ca193dSGeorge Wilson 	return (B_FALSE);
1032e6ca193dSGeorge Wilson }
1033e6ca193dSGeorge Wilson 
1034e6ca193dSGeorge Wilson static int
1035e6ca193dSGeorge Wilson find_spare(zpool_handle_t *zhp, void *data)
1036e6ca193dSGeorge Wilson {
1037e6ca193dSGeorge Wilson 	spare_cbdata_t *cbp = data;
1038e6ca193dSGeorge Wilson 	nvlist_t *config, *nvroot;
1039e6ca193dSGeorge Wilson 
1040e6ca193dSGeorge Wilson 	config = zpool_get_config(zhp, NULL);
1041e6ca193dSGeorge Wilson 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1042e6ca193dSGeorge Wilson 	    &nvroot) == 0);
1043e6ca193dSGeorge Wilson 
1044e6ca193dSGeorge Wilson 	if (find_vdev(nvroot, cbp->cb_guid)) {
1045e6ca193dSGeorge Wilson 		cbp->cb_zhp = zhp;
1046e6ca193dSGeorge Wilson 		return (1);
1047e6ca193dSGeorge Wilson 	}
1048e6ca193dSGeorge Wilson 
1049e6ca193dSGeorge Wilson 	zpool_close(zhp);
1050e6ca193dSGeorge Wilson 	return (0);
1051e6ca193dSGeorge Wilson }
1052e6ca193dSGeorge Wilson 
1053e6ca193dSGeorge Wilson /*
1054e6ca193dSGeorge Wilson  * Print out configuration state as requested by status_callback.
1055e6ca193dSGeorge Wilson  */
1056e6ca193dSGeorge Wilson void
1057e6ca193dSGeorge Wilson print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1058e6ca193dSGeorge Wilson     int namewidth, int depth, boolean_t isspare)
1059e6ca193dSGeorge Wilson {
1060e6ca193dSGeorge Wilson 	nvlist_t **child;
1061e6ca193dSGeorge Wilson 	uint_t c, children;
10623f9d6ad7SLin Ling 	pool_scan_stat_t *ps = NULL;
1063e6ca193dSGeorge Wilson 	vdev_stat_t *vs;
10643f9d6ad7SLin Ling 	char rbuf[6], wbuf[6], cbuf[6];
1065e6ca193dSGeorge Wilson 	char *vname;
1066e6ca193dSGeorge Wilson 	uint64_t notpresent;
1067e6ca193dSGeorge Wilson 	spare_cbdata_t cb;
1068e6ca193dSGeorge Wilson 	char *state;
1069e6ca193dSGeorge Wilson 
1070e6ca193dSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1071e6ca193dSGeorge Wilson 	    &child, &children) != 0)
1072e6ca193dSGeorge Wilson 		children = 0;
1073e6ca193dSGeorge Wilson 
10743f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
10753f9d6ad7SLin Ling 	    (uint64_t **)&vs, &c) == 0);
10763f9d6ad7SLin Ling 
1077e6ca193dSGeorge Wilson 	state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1078e6ca193dSGeorge Wilson 	if (isspare) {
1079e6ca193dSGeorge Wilson 		/*
1080e6ca193dSGeorge Wilson 		 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1081e6ca193dSGeorge Wilson 		 * online drives.
1082e6ca193dSGeorge Wilson 		 */
1083e6ca193dSGeorge Wilson 		if (vs->vs_aux == VDEV_AUX_SPARED)
1084e6ca193dSGeorge Wilson 			state = "INUSE";
1085e6ca193dSGeorge Wilson 		else if (vs->vs_state == VDEV_STATE_HEALTHY)
1086e6ca193dSGeorge Wilson 			state = "AVAIL";
1087e6ca193dSGeorge Wilson 	}
1088e6ca193dSGeorge Wilson 
1089e6ca193dSGeorge Wilson 	(void) printf("\t%*s%-*s  %-8s", depth, "", namewidth - depth,
1090e6ca193dSGeorge Wilson 	    name, state);
1091e6ca193dSGeorge Wilson 
1092e6ca193dSGeorge Wilson 	if (!isspare) {
1093e6ca193dSGeorge Wilson 		zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1094e6ca193dSGeorge Wilson 		zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1095e6ca193dSGeorge Wilson 		zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1096e6ca193dSGeorge Wilson 		(void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1097e6ca193dSGeorge Wilson 	}
1098e6ca193dSGeorge Wilson 
1099e6ca193dSGeorge Wilson 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1100e6ca193dSGeorge Wilson 	    &notpresent) == 0) {
1101e6ca193dSGeorge Wilson 		char *path;
1102e6ca193dSGeorge Wilson 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1103e6ca193dSGeorge Wilson 		(void) printf("  was %s", path);
1104e6ca193dSGeorge Wilson 	} else if (vs->vs_aux != 0) {
1105e6ca193dSGeorge Wilson 		(void) printf("  ");
1106e6ca193dSGeorge Wilson 
1107e6ca193dSGeorge Wilson 		switch (vs->vs_aux) {
1108e6ca193dSGeorge Wilson 		case VDEV_AUX_OPEN_FAILED:
1109e6ca193dSGeorge Wilson 			(void) printf(gettext("cannot open"));
1110e6ca193dSGeorge Wilson 			break;
1111e6ca193dSGeorge Wilson 
1112e6ca193dSGeorge Wilson 		case VDEV_AUX_BAD_GUID_SUM:
1113e6ca193dSGeorge Wilson 			(void) printf(gettext("missing device"));
1114e6ca193dSGeorge Wilson 			break;
1115e6ca193dSGeorge Wilson 
1116e6ca193dSGeorge Wilson 		case VDEV_AUX_NO_REPLICAS:
1117e6ca193dSGeorge Wilson 			(void) printf(gettext("insufficient replicas"));
1118e6ca193dSGeorge Wilson 			break;
1119e6ca193dSGeorge Wilson 
1120e6ca193dSGeorge Wilson 		case VDEV_AUX_VERSION_NEWER:
1121e6ca193dSGeorge Wilson 			(void) printf(gettext("newer version"));
1122e6ca193dSGeorge Wilson 			break;
1123e6ca193dSGeorge Wilson 
1124e6ca193dSGeorge Wilson 		case VDEV_AUX_SPARED:
1125e6ca193dSGeorge Wilson 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1126e6ca193dSGeorge Wilson 			    &cb.cb_guid) == 0);
1127e6ca193dSGeorge Wilson 			if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1128e6ca193dSGeorge Wilson 				if (strcmp(zpool_get_name(cb.cb_zhp),
1129e6ca193dSGeorge Wilson 				    zpool_get_name(zhp)) == 0)
1130e6ca193dSGeorge Wilson 					(void) printf(gettext("currently in "
1131e6ca193dSGeorge Wilson 					    "use"));
1132e6ca193dSGeorge Wilson 				else
1133e6ca193dSGeorge Wilson 					(void) printf(gettext("in use by "
1134e6ca193dSGeorge Wilson 					    "pool '%s'"),
1135e6ca193dSGeorge Wilson 					    zpool_get_name(cb.cb_zhp));
1136e6ca193dSGeorge Wilson 				zpool_close(cb.cb_zhp);
1137e6ca193dSGeorge Wilson 			} else {
1138e6ca193dSGeorge Wilson 				(void) printf(gettext("currently in use"));
1139e6ca193dSGeorge Wilson 			}
1140e6ca193dSGeorge Wilson 			break;
1141e6ca193dSGeorge Wilson 
1142e6ca193dSGeorge Wilson 		case VDEV_AUX_ERR_EXCEEDED:
1143e6ca193dSGeorge Wilson 			(void) printf(gettext("too many errors"));
1144e6ca193dSGeorge Wilson 			break;
1145e6ca193dSGeorge Wilson 
1146e6ca193dSGeorge Wilson 		case VDEV_AUX_IO_FAILURE:
1147e6ca193dSGeorge Wilson 			(void) printf(gettext("experienced I/O failures"));
1148e6ca193dSGeorge Wilson 			break;
1149e6ca193dSGeorge Wilson 
1150e6ca193dSGeorge Wilson 		case VDEV_AUX_BAD_LOG:
1151e6ca193dSGeorge Wilson 			(void) printf(gettext("bad intent log"));
1152e6ca193dSGeorge Wilson 			break;
1153e6ca193dSGeorge Wilson 
1154069f55e2SEric Schrock 		case VDEV_AUX_EXTERNAL:
1155069f55e2SEric Schrock 			(void) printf(gettext("external device fault"));
1156069f55e2SEric Schrock 			break;
1157069f55e2SEric Schrock 
11581195e687SMark J Musante 		case VDEV_AUX_SPLIT_POOL:
11591195e687SMark J Musante 			(void) printf(gettext("split into new pool"));
11601195e687SMark J Musante 			break;
11611195e687SMark J Musante 
1162e6ca193dSGeorge Wilson 		default:
1163e6ca193dSGeorge Wilson 			(void) printf(gettext("corrupted data"));
1164e6ca193dSGeorge Wilson 			break;
1165e6ca193dSGeorge Wilson 		}
11663f9d6ad7SLin Ling 	}
11673f9d6ad7SLin Ling 
11683f9d6ad7SLin Ling 	(void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
11693f9d6ad7SLin Ling 	    (uint64_t **)&ps, &c);
11703f9d6ad7SLin Ling 
11713f9d6ad7SLin Ling 	if (ps && ps->pss_state == DSS_SCANNING &&
11723f9d6ad7SLin Ling 	    vs->vs_scan_processed != 0 && children == 0) {
11733f9d6ad7SLin Ling 		(void) printf(gettext("  (%s)"),
11743f9d6ad7SLin Ling 		    (ps->pss_func == POOL_SCAN_RESILVER) ?
11753f9d6ad7SLin Ling 		    "resilvering" : "repairing");
1176e6ca193dSGeorge Wilson 	}
1177e6ca193dSGeorge Wilson 
1178e6ca193dSGeorge Wilson 	(void) printf("\n");
1179e6ca193dSGeorge Wilson 
1180e6ca193dSGeorge Wilson 	for (c = 0; c < children; c++) {
118188ecc943SGeorge Wilson 		uint64_t islog = B_FALSE, ishole = B_FALSE;
1182e6ca193dSGeorge Wilson 
118388ecc943SGeorge Wilson 		/* Don't print logs or holes here */
1184e6ca193dSGeorge Wilson 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
118588ecc943SGeorge Wilson 		    &islog);
118688ecc943SGeorge Wilson 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
118788ecc943SGeorge Wilson 		    &ishole);
118888ecc943SGeorge Wilson 		if (islog || ishole)
1189e6ca193dSGeorge Wilson 			continue;
119088ecc943SGeorge Wilson 		vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1191e6ca193dSGeorge Wilson 		print_status_config(zhp, vname, child[c],
1192e6ca193dSGeorge Wilson 		    namewidth, depth + 2, isspare);
1193e6ca193dSGeorge Wilson 		free(vname);
1194e6ca193dSGeorge Wilson 	}
1195e6ca193dSGeorge Wilson }
1196e6ca193dSGeorge Wilson 
1197fa9e4066Sahrens 
1198fa9e4066Sahrens /*
1199fa9e4066Sahrens  * Print the configuration of an exported pool.  Iterate over all vdevs in the
1200fa9e4066Sahrens  * pool, printing out the name and status for each one.
1201fa9e4066Sahrens  */
1202fa9e4066Sahrens void
1203e6ca193dSGeorge Wilson print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1204fa9e4066Sahrens {
1205fa9e4066Sahrens 	nvlist_t **child;
1206fa9e4066Sahrens 	uint_t c, children;
1207fa9e4066Sahrens 	vdev_stat_t *vs;
1208afefbcddSeschrock 	char *type, *vname;
1209fa9e4066Sahrens 
1210fa9e4066Sahrens 	verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
121188ecc943SGeorge Wilson 	if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
121288ecc943SGeorge Wilson 	    strcmp(type, VDEV_TYPE_HOLE) == 0)
1213fa9e4066Sahrens 		return;
1214fa9e4066Sahrens 
12153f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1216fa9e4066Sahrens 	    (uint64_t **)&vs, &c) == 0);
1217fa9e4066Sahrens 
1218fa9e4066Sahrens 	(void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1219990b4856Slling 	(void) printf("  %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1220fa9e4066Sahrens 
1221fa9e4066Sahrens 	if (vs->vs_aux != 0) {
12223d7072f8Seschrock 		(void) printf("  ");
1223fa9e4066Sahrens 
1224fa9e4066Sahrens 		switch (vs->vs_aux) {
1225fa9e4066Sahrens 		case VDEV_AUX_OPEN_FAILED:
1226fa9e4066Sahrens 			(void) printf(gettext("cannot open"));
1227fa9e4066Sahrens 			break;
1228fa9e4066Sahrens 
1229fa9e4066Sahrens 		case VDEV_AUX_BAD_GUID_SUM:
1230fa9e4066Sahrens 			(void) printf(gettext("missing device"));
1231fa9e4066Sahrens 			break;
1232fa9e4066Sahrens 
1233fa9e4066Sahrens 		case VDEV_AUX_NO_REPLICAS:
1234fa9e4066Sahrens 			(void) printf(gettext("insufficient replicas"));
1235fa9e4066Sahrens 			break;
1236fa9e4066Sahrens 
1237eaca9bbdSeschrock 		case VDEV_AUX_VERSION_NEWER:
1238eaca9bbdSeschrock 			(void) printf(gettext("newer version"));
1239eaca9bbdSeschrock 			break;
1240eaca9bbdSeschrock 
12413d7072f8Seschrock 		case VDEV_AUX_ERR_EXCEEDED:
12423d7072f8Seschrock 			(void) printf(gettext("too many errors"));
12433d7072f8Seschrock 			break;
12443d7072f8Seschrock 
1245fa9e4066Sahrens 		default:
1246fa9e4066Sahrens 			(void) printf(gettext("corrupted data"));
1247fa9e4066Sahrens 			break;
1248fa9e4066Sahrens 		}
1249fa9e4066Sahrens 	}
1250fa9e4066Sahrens 	(void) printf("\n");
1251fa9e4066Sahrens 
1252fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1253fa9e4066Sahrens 	    &child, &children) != 0)
1254fa9e4066Sahrens 		return;
1255fa9e4066Sahrens 
1256afefbcddSeschrock 	for (c = 0; c < children; c++) {
12578654d025Sperrin 		uint64_t is_log = B_FALSE;
12588654d025Sperrin 
12598654d025Sperrin 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
12608654d025Sperrin 		    &is_log);
1261e6ca193dSGeorge Wilson 		if (is_log)
12628654d025Sperrin 			continue;
12638654d025Sperrin 
126488ecc943SGeorge Wilson 		vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1265e6ca193dSGeorge Wilson 		print_import_config(vname, child[c], namewidth, depth + 2);
1266afefbcddSeschrock 		free(vname);
1267afefbcddSeschrock 	}
126899653d4eSeschrock 
1269fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1270fa94a07fSbrendan 	    &child, &children) == 0) {
1271fa94a07fSbrendan 		(void) printf(gettext("\tcache\n"));
1272fa94a07fSbrendan 		for (c = 0; c < children; c++) {
127388ecc943SGeorge Wilson 			vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1274fa94a07fSbrendan 			(void) printf("\t  %s\n", vname);
1275fa94a07fSbrendan 			free(vname);
1276fa94a07fSbrendan 		}
1277fa94a07fSbrendan 	}
127899653d4eSeschrock 
1279fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1280fa94a07fSbrendan 	    &child, &children) == 0) {
128199653d4eSeschrock 		(void) printf(gettext("\tspares\n"));
128299653d4eSeschrock 		for (c = 0; c < children; c++) {
128388ecc943SGeorge Wilson 			vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
128499653d4eSeschrock 			(void) printf("\t  %s\n", vname);
128599653d4eSeschrock 			free(vname);
128699653d4eSeschrock 		}
1287fa9e4066Sahrens 	}
1288fa94a07fSbrendan }
1289fa9e4066Sahrens 
1290fa9e4066Sahrens /*
1291e6ca193dSGeorge Wilson  * Print log vdevs.
1292e6ca193dSGeorge Wilson  * Logs are recorded as top level vdevs in the main pool child array
1293e6ca193dSGeorge Wilson  * but with "is_log" set to 1. We use either print_status_config() or
1294e6ca193dSGeorge Wilson  * print_import_config() to print the top level logs then any log
1295e6ca193dSGeorge Wilson  * children (eg mirrored slogs) are printed recursively - which
1296e6ca193dSGeorge Wilson  * works because only the top level vdev is marked "is_log"
1297e6ca193dSGeorge Wilson  */
1298e6ca193dSGeorge Wilson static void
1299e6ca193dSGeorge Wilson print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1300e6ca193dSGeorge Wilson {
1301e6ca193dSGeorge Wilson 	uint_t c, children;
1302e6ca193dSGeorge Wilson 	nvlist_t **child;
1303e6ca193dSGeorge Wilson 
1304e6ca193dSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1305e6ca193dSGeorge Wilson 	    &children) != 0)
1306e6ca193dSGeorge Wilson 		return;
1307e6ca193dSGeorge Wilson 
1308e6ca193dSGeorge Wilson 	(void) printf(gettext("\tlogs\n"));
1309e6ca193dSGeorge Wilson 
1310e6ca193dSGeorge Wilson 	for (c = 0; c < children; c++) {
1311e6ca193dSGeorge Wilson 		uint64_t is_log = B_FALSE;
1312e6ca193dSGeorge Wilson 		char *name;
1313e6ca193dSGeorge Wilson 
1314e6ca193dSGeorge Wilson 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1315e6ca193dSGeorge Wilson 		    &is_log);
1316e6ca193dSGeorge Wilson 		if (!is_log)
1317e6ca193dSGeorge Wilson 			continue;
131888ecc943SGeorge Wilson 		name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1319e6ca193dSGeorge Wilson 		if (verbose)
1320e6ca193dSGeorge Wilson 			print_status_config(zhp, name, child[c], namewidth,
1321e6ca193dSGeorge Wilson 			    2, B_FALSE);
1322e6ca193dSGeorge Wilson 		else
1323e6ca193dSGeorge Wilson 			print_import_config(name, child[c], namewidth, 2);
1324e6ca193dSGeorge Wilson 		free(name);
1325e6ca193dSGeorge Wilson 	}
1326e6ca193dSGeorge Wilson }
1327468c413aSTim Haley 
1328e6ca193dSGeorge Wilson /*
1329fa9e4066Sahrens  * Display the status for the given pool.
1330fa9e4066Sahrens  */
1331fa9e4066Sahrens static void
1332fa9e4066Sahrens show_import(nvlist_t *config)
1333fa9e4066Sahrens {
1334fa9e4066Sahrens 	uint64_t pool_state;
1335fa9e4066Sahrens 	vdev_stat_t *vs;
1336fa9e4066Sahrens 	char *name;
1337fa9e4066Sahrens 	uint64_t guid;
1338fa9e4066Sahrens 	char *msgid;
1339fa9e4066Sahrens 	nvlist_t *nvroot;
1340fa9e4066Sahrens 	int reason;
134146657f8dSmmusante 	const char *health;
1342fa9e4066Sahrens 	uint_t vsc;
1343fa9e4066Sahrens 	int namewidth;
13448704186eSDan McDonald 	char *comment;
1345fa9e4066Sahrens 
1346fa9e4066Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1347fa9e4066Sahrens 	    &name) == 0);
1348fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1349fa9e4066Sahrens 	    &guid) == 0);
1350fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1351fa9e4066Sahrens 	    &pool_state) == 0);
1352fa9e4066Sahrens 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1353fa9e4066Sahrens 	    &nvroot) == 0);
1354fa9e4066Sahrens 
13553f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1356fa9e4066Sahrens 	    (uint64_t **)&vs, &vsc) == 0);
1357990b4856Slling 	health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1358fa9e4066Sahrens 
1359fa9e4066Sahrens 	reason = zpool_import_status(config, &msgid);
1360fa9e4066Sahrens 
136146657f8dSmmusante 	(void) printf(gettext("   pool: %s\n"), name);
136246657f8dSmmusante 	(void) printf(gettext("     id: %llu\n"), (u_longlong_t)guid);
136346657f8dSmmusante 	(void) printf(gettext("  state: %s"), health);
13644c58d714Sdarrenm 	if (pool_state == POOL_STATE_DESTROYED)
136546657f8dSmmusante 		(void) printf(gettext(" (DESTROYED)"));
13664c58d714Sdarrenm 	(void) printf("\n");
1367fa9e4066Sahrens 
1368fa9e4066Sahrens 	switch (reason) {
1369fa9e4066Sahrens 	case ZPOOL_STATUS_MISSING_DEV_R:
1370fa9e4066Sahrens 	case ZPOOL_STATUS_MISSING_DEV_NR:
1371fa9e4066Sahrens 	case ZPOOL_STATUS_BAD_GUID_SUM:
13728704186eSDan McDonald 		(void) printf(gettext(" status: One or more devices are "
13738704186eSDan McDonald 		    "missing from the system.\n"));
1374fa9e4066Sahrens 		break;
1375fa9e4066Sahrens 
1376fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_LABEL_R:
1377fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1378fa9e4066Sahrens 		(void) printf(gettext(" status: One or more devices contains "
1379fa9e4066Sahrens 		    "corrupted data.\n"));
1380fa9e4066Sahrens 		break;
1381fa9e4066Sahrens 
1382fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_DATA:
13838704186eSDan McDonald 		(void) printf(
13848704186eSDan McDonald 		    gettext(" status: The pool data is corrupted.\n"));
1385fa9e4066Sahrens 		break;
1386fa9e4066Sahrens 
1387441d80aaSlling 	case ZPOOL_STATUS_OFFLINE_DEV:
1388441d80aaSlling 		(void) printf(gettext(" status: One or more devices "
1389441d80aaSlling 		    "are offlined.\n"));
1390441d80aaSlling 		break;
1391441d80aaSlling 
1392ea8dc4b6Seschrock 	case ZPOOL_STATUS_CORRUPT_POOL:
1393ea8dc4b6Seschrock 		(void) printf(gettext(" status: The pool metadata is "
1394ea8dc4b6Seschrock 		    "corrupted.\n"));
1395ea8dc4b6Seschrock 		break;
1396ea8dc4b6Seschrock 
1397eaca9bbdSeschrock 	case ZPOOL_STATUS_VERSION_OLDER:
1398eaca9bbdSeschrock 		(void) printf(gettext(" status: The pool is formatted using an "
1399eaca9bbdSeschrock 		    "older on-disk version.\n"));
1400eaca9bbdSeschrock 		break;
1401eaca9bbdSeschrock 
1402eaca9bbdSeschrock 	case ZPOOL_STATUS_VERSION_NEWER:
1403eaca9bbdSeschrock 		(void) printf(gettext(" status: The pool is formatted using an "
1404eaca9bbdSeschrock 		    "incompatible version.\n"));
1405eaca9bbdSeschrock 		break;
1406b87f3af3Sperrin 
140795173954Sek110237 	case ZPOOL_STATUS_HOSTID_MISMATCH:
140895173954Sek110237 		(void) printf(gettext(" status: The pool was last accessed by "
140995173954Sek110237 		    "another system.\n"));
141095173954Sek110237 		break;
1411b87f3af3Sperrin 
14123d7072f8Seschrock 	case ZPOOL_STATUS_FAULTED_DEV_R:
14133d7072f8Seschrock 	case ZPOOL_STATUS_FAULTED_DEV_NR:
14143d7072f8Seschrock 		(void) printf(gettext(" status: One or more devices are "
14153d7072f8Seschrock 		    "faulted.\n"));
14163d7072f8Seschrock 		break;
14173d7072f8Seschrock 
1418b87f3af3Sperrin 	case ZPOOL_STATUS_BAD_LOG:
1419b87f3af3Sperrin 		(void) printf(gettext(" status: An intent log record cannot be "
1420b87f3af3Sperrin 		    "read.\n"));
1421b87f3af3Sperrin 		break;
1422b87f3af3Sperrin 
14233f9d6ad7SLin Ling 	case ZPOOL_STATUS_RESILVERING:
14243f9d6ad7SLin Ling 		(void) printf(gettext(" status: One or more devices were being "
14253f9d6ad7SLin Ling 		    "resilvered.\n"));
14263f9d6ad7SLin Ling 		break;
14273f9d6ad7SLin Ling 
1428fa9e4066Sahrens 	default:
1429fa9e4066Sahrens 		/*
1430fa9e4066Sahrens 		 * No other status can be seen when importing pools.
1431fa9e4066Sahrens 		 */
1432fa9e4066Sahrens 		assert(reason == ZPOOL_STATUS_OK);
1433fa9e4066Sahrens 	}
1434fa9e4066Sahrens 
1435fa9e4066Sahrens 	/*
1436fa9e4066Sahrens 	 * Print out an action according to the overall state of the pool.
1437fa9e4066Sahrens 	 */
143846657f8dSmmusante 	if (vs->vs_state == VDEV_STATE_HEALTHY) {
1439eaca9bbdSeschrock 		if (reason == ZPOOL_STATUS_VERSION_OLDER)
1440eaca9bbdSeschrock 			(void) printf(gettext(" action: The pool can be "
1441eaca9bbdSeschrock 			    "imported using its name or numeric identifier, "
1442eaca9bbdSeschrock 			    "though\n\tsome features will not be available "
1443eaca9bbdSeschrock 			    "without an explicit 'zpool upgrade'.\n"));
144495173954Sek110237 		else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH)
144595173954Sek110237 			(void) printf(gettext(" action: The pool can be "
144695173954Sek110237 			    "imported using its name or numeric "
144795173954Sek110237 			    "identifier and\n\tthe '-f' flag.\n"));
1448fa9e4066Sahrens 		else
1449eaca9bbdSeschrock 			(void) printf(gettext(" action: The pool can be "
1450eaca9bbdSeschrock 			    "imported using its name or numeric "
1451eaca9bbdSeschrock 			    "identifier.\n"));
145246657f8dSmmusante 	} else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1453fa9e4066Sahrens 		(void) printf(gettext(" action: The pool can be imported "
1454fa9e4066Sahrens 		    "despite missing or damaged devices.  The\n\tfault "
1455eaca9bbdSeschrock 		    "tolerance of the pool may be compromised if imported.\n"));
1456fa9e4066Sahrens 	} else {
1457eaca9bbdSeschrock 		switch (reason) {
1458eaca9bbdSeschrock 		case ZPOOL_STATUS_VERSION_NEWER:
1459eaca9bbdSeschrock 			(void) printf(gettext(" action: The pool cannot be "
1460eaca9bbdSeschrock 			    "imported.  Access the pool on a system running "
1461eaca9bbdSeschrock 			    "newer\n\tsoftware, or recreate the pool from "
1462eaca9bbdSeschrock 			    "backup.\n"));
1463eaca9bbdSeschrock 			break;
1464eaca9bbdSeschrock 		case ZPOOL_STATUS_MISSING_DEV_R:
1465eaca9bbdSeschrock 		case ZPOOL_STATUS_MISSING_DEV_NR:
1466eaca9bbdSeschrock 		case ZPOOL_STATUS_BAD_GUID_SUM:
1467fa9e4066Sahrens 			(void) printf(gettext(" action: The pool cannot be "
1468fa9e4066Sahrens 			    "imported. Attach the missing\n\tdevices and try "
1469fa9e4066Sahrens 			    "again.\n"));
1470eaca9bbdSeschrock 			break;
1471eaca9bbdSeschrock 		default:
1472fa9e4066Sahrens 			(void) printf(gettext(" action: The pool cannot be "
1473fa9e4066Sahrens 			    "imported due to damaged devices or data.\n"));
1474fa9e4066Sahrens 		}
1475eaca9bbdSeschrock 	}
1476eaca9bbdSeschrock 
14778704186eSDan McDonald 	/* Print the comment attached to the pool. */
14788704186eSDan McDonald 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
14798704186eSDan McDonald 		(void) printf(gettext("comment: %s\n"), comment);
14808704186eSDan McDonald 
148146657f8dSmmusante 	/*
148246657f8dSmmusante 	 * If the state is "closed" or "can't open", and the aux state
148346657f8dSmmusante 	 * is "corrupt data":
148446657f8dSmmusante 	 */
148546657f8dSmmusante 	if (((vs->vs_state == VDEV_STATE_CLOSED) ||
148646657f8dSmmusante 	    (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
148746657f8dSmmusante 	    (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1488eaca9bbdSeschrock 		if (pool_state == POOL_STATE_DESTROYED)
1489eaca9bbdSeschrock 			(void) printf(gettext("\tThe pool was destroyed, "
1490eaca9bbdSeschrock 			    "but can be imported using the '-Df' flags.\n"));
1491eaca9bbdSeschrock 		else if (pool_state != POOL_STATE_EXPORTED)
1492eaca9bbdSeschrock 			(void) printf(gettext("\tThe pool may be active on "
149318ce54dfSek110237 			    "another system, but can be imported using\n\t"
1494eaca9bbdSeschrock 			    "the '-f' flag.\n"));
1495eaca9bbdSeschrock 	}
1496fa9e4066Sahrens 
1497fa9e4066Sahrens 	if (msgid != NULL)
1498*654b400cSJoshua M. Clulow 		(void) printf(gettext("   see: http://illumos.org/msg/%s\n"),
1499fa9e4066Sahrens 		    msgid);
1500fa9e4066Sahrens 
1501fa9e4066Sahrens 	(void) printf(gettext(" config:\n\n"));
1502fa9e4066Sahrens 
1503c67d9675Seschrock 	namewidth = max_width(NULL, nvroot, 0, 0);
1504fa9e4066Sahrens 	if (namewidth < 10)
1505fa9e4066Sahrens 		namewidth = 10;
15068654d025Sperrin 
1507e6ca193dSGeorge Wilson 	print_import_config(name, nvroot, namewidth, 0);
1508e6ca193dSGeorge Wilson 	if (num_logs(nvroot) > 0)
1509e6ca193dSGeorge Wilson 		print_logs(NULL, nvroot, namewidth, B_FALSE);
1510fa9e4066Sahrens 
1511fa9e4066Sahrens 	if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
151246657f8dSmmusante 		(void) printf(gettext("\n\tAdditional devices are known to "
1513fa9e4066Sahrens 		    "be part of this pool, though their\n\texact "
151446657f8dSmmusante 		    "configuration cannot be determined.\n"));
1515fa9e4066Sahrens 	}
1516fa9e4066Sahrens }
1517fa9e4066Sahrens 
1518fa9e4066Sahrens /*
1519fa9e4066Sahrens  * Perform the import for the given configuration.  This passes the heavy
1520990b4856Slling  * lifting off to zpool_import_props(), and then mounts the datasets contained
1521990b4856Slling  * within the pool.
1522fa9e4066Sahrens  */
1523fa9e4066Sahrens static int
1524fa9e4066Sahrens do_import(nvlist_t *config, const char *newname, const char *mntopts,
15254b964adaSGeorge Wilson     nvlist_t *props, int flags)
1526fa9e4066Sahrens {
1527fa9e4066Sahrens 	zpool_handle_t *zhp;
1528fa9e4066Sahrens 	char *name;
1529fa9e4066Sahrens 	uint64_t state;
1530eaca9bbdSeschrock 	uint64_t version;
1531fa9e4066Sahrens 
1532fa9e4066Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1533fa9e4066Sahrens 	    &name) == 0);
1534fa9e4066Sahrens 
1535fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config,
1536fa9e4066Sahrens 	    ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1537eaca9bbdSeschrock 	verify(nvlist_lookup_uint64(config,
1538eaca9bbdSeschrock 	    ZPOOL_CONFIG_VERSION, &version) == 0);
1539e7437265Sahrens 	if (version > SPA_VERSION) {
1540eaca9bbdSeschrock 		(void) fprintf(stderr, gettext("cannot import '%s': pool "
1541eaca9bbdSeschrock 		    "is formatted using a newer ZFS version\n"), name);
1542eaca9bbdSeschrock 		return (1);
15434b964adaSGeorge Wilson 	} else if (state != POOL_STATE_EXPORTED &&
15444b964adaSGeorge Wilson 	    !(flags & ZFS_IMPORT_ANY_HOST)) {
154595173954Sek110237 		uint64_t hostid;
154695173954Sek110237 
154795173954Sek110237 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
154895173954Sek110237 		    &hostid) == 0) {
154995173954Sek110237 			if ((unsigned long)hostid != gethostid()) {
155095173954Sek110237 				char *hostname;
155195173954Sek110237 				uint64_t timestamp;
155295173954Sek110237 				time_t t;
155395173954Sek110237 
155495173954Sek110237 				verify(nvlist_lookup_string(config,
155595173954Sek110237 				    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
155695173954Sek110237 				verify(nvlist_lookup_uint64(config,
155795173954Sek110237 				    ZPOOL_CONFIG_TIMESTAMP, &timestamp) == 0);
155895173954Sek110237 				t = timestamp;
155995173954Sek110237 				(void) fprintf(stderr, gettext("cannot import "
156095173954Sek110237 				    "'%s': pool may be in use from other "
156195173954Sek110237 				    "system, it was last accessed by %s "
156295173954Sek110237 				    "(hostid: 0x%lx) on %s"), name, hostname,
156395173954Sek110237 				    (unsigned long)hostid,
156495173954Sek110237 				    asctime(localtime(&t)));
156595173954Sek110237 				(void) fprintf(stderr, gettext("use '-f' to "
156695173954Sek110237 				    "import anyway\n"));
1567fa9e4066Sahrens 				return (1);
1568fa9e4066Sahrens 			}
156995173954Sek110237 		} else {
157095173954Sek110237 			(void) fprintf(stderr, gettext("cannot import '%s': "
157195173954Sek110237 			    "pool may be in use from other system\n"), name);
157295173954Sek110237 			(void) fprintf(stderr, gettext("use '-f' to import "
157395173954Sek110237 			    "anyway\n"));
157495173954Sek110237 			return (1);
157595173954Sek110237 		}
157695173954Sek110237 	}
1577fa9e4066Sahrens 
15784b964adaSGeorge Wilson 	if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1579fa9e4066Sahrens 		return (1);
1580fa9e4066Sahrens 
1581fa9e4066Sahrens 	if (newname != NULL)
1582fa9e4066Sahrens 		name = (char *)newname;
1583fa9e4066Sahrens 
15844f0f5e5bSVictor Latushkin 	if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
15854f0f5e5bSVictor Latushkin 		return (1);
1586fa9e4066Sahrens 
1587379c004dSEric Schrock 	if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1588f9af39baSGeorge Wilson 	    !(flags & ZFS_IMPORT_ONLY) &&
1589379c004dSEric Schrock 	    zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1590fa9e4066Sahrens 		zpool_close(zhp);
1591fa9e4066Sahrens 		return (1);
1592fa9e4066Sahrens 	}
1593fa9e4066Sahrens 
1594fa9e4066Sahrens 	zpool_close(zhp);
1595468c413aSTim Haley 	return (0);
1596fa9e4066Sahrens }
1597fa9e4066Sahrens 
1598fa9e4066Sahrens /*
15994c58d714Sdarrenm  * zpool import [-d dir] [-D]
16002f8aaab3Seschrock  *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
16012f8aaab3Seschrock  *              [-d dir | -c cachefile] [-f] -a
16022f8aaab3Seschrock  *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1603468c413aSTim Haley  *              [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
16042f8aaab3Seschrock  *
16052f8aaab3Seschrock  *	 -c	Read pool information from a cachefile instead of searching
16062f8aaab3Seschrock  *		devices.
1607fa9e4066Sahrens  *
1608fa9e4066Sahrens  *       -d	Scan in a specific directory, other than /dev/dsk.  More than
1609fa9e4066Sahrens  *		one directory can be specified using multiple '-d' options.
1610fa9e4066Sahrens  *
16114c58d714Sdarrenm  *       -D     Scan for previously destroyed pools or import all or only
16124c58d714Sdarrenm  *              specified destroyed pools.
16134c58d714Sdarrenm  *
1614fa9e4066Sahrens  *       -R	Temporarily import the pool, with all mountpoints relative to
1615fa9e4066Sahrens  *		the given root.  The pool will remain exported when the machine
1616fa9e4066Sahrens  *		is rebooted.
1617fa9e4066Sahrens  *
1618468c413aSTim Haley  *       -V	Import even in the presence of faulted vdevs.  This is an
1619c5904d13Seschrock  *       	intentionally undocumented option for testing purposes, and
1620c5904d13Seschrock  *       	treats the pool configuration as complete, leaving any bad
16214f0f5e5bSVictor Latushkin  *		vdevs in the FAULTED state. In other words, it does verbatim
16224f0f5e5bSVictor Latushkin  *		import.
1623c5904d13Seschrock  *
1624468c413aSTim Haley  *       -f	Force import, even if it appears that the pool is active.
1625468c413aSTim Haley  *
1626468c413aSTim Haley  *       -F     Attempt rewind if necessary.
1627468c413aSTim Haley  *
1628468c413aSTim Haley  *       -n     See if rewind would work, but don't actually rewind.
1629468c413aSTim Haley  *
1630f9af39baSGeorge Wilson  *       -N     Import the pool but don't mount datasets.
1631f9af39baSGeorge Wilson  *
1632f9af39baSGeorge Wilson  *       -T     Specify a starting txg to use for import. This option is
1633f9af39baSGeorge Wilson  *       	intentionally undocumented option for testing purposes.
1634f9af39baSGeorge Wilson  *
1635fa9e4066Sahrens  *       -a	Import all pools found.
1636fa9e4066Sahrens  *
1637990b4856Slling  *       -o	Set property=value and/or temporary mount options (without '=').
1638ecd6cf80Smarks  *
1639fa9e4066Sahrens  * The import command scans for pools to import, and import pools based on pool
1640fa9e4066Sahrens  * name and GUID.  The pool can also be renamed as part of the import process.
1641fa9e4066Sahrens  */
1642fa9e4066Sahrens int
1643fa9e4066Sahrens zpool_do_import(int argc, char **argv)
1644fa9e4066Sahrens {
1645fa9e4066Sahrens 	char **searchdirs = NULL;
1646fa9e4066Sahrens 	int nsearch = 0;
1647fa9e4066Sahrens 	int c;
1648d41c4376SMark J Musante 	int err = 0;
16492f8aaab3Seschrock 	nvlist_t *pools = NULL;
165099653d4eSeschrock 	boolean_t do_all = B_FALSE;
165199653d4eSeschrock 	boolean_t do_destroyed = B_FALSE;
1652fa9e4066Sahrens 	char *mntopts = NULL;
1653fa9e4066Sahrens 	nvpair_t *elem;
1654fa9e4066Sahrens 	nvlist_t *config;
165524e697d4Sck153898 	uint64_t searchguid = 0;
165624e697d4Sck153898 	char *searchname = NULL;
1657990b4856Slling 	char *propval;
1658fa9e4066Sahrens 	nvlist_t *found_config;
1659468c413aSTim Haley 	nvlist_t *policy = NULL;
1660ecd6cf80Smarks 	nvlist_t *props = NULL;
166199653d4eSeschrock 	boolean_t first;
16624b964adaSGeorge Wilson 	int flags = ZFS_IMPORT_NORMAL;
1663468c413aSTim Haley 	uint32_t rewind_policy = ZPOOL_NO_REWIND;
1664468c413aSTim Haley 	boolean_t dryrun = B_FALSE;
1665468c413aSTim Haley 	boolean_t do_rewind = B_FALSE;
1666468c413aSTim Haley 	boolean_t xtreme_rewind = B_FALSE;
1667f9af39baSGeorge Wilson 	uint64_t pool_state, txg = -1ULL;
16682f8aaab3Seschrock 	char *cachefile = NULL;
1669d41c4376SMark J Musante 	importargs_t idata = { 0 };
1670f9af39baSGeorge Wilson 	char *endptr;
1671fa9e4066Sahrens 
1672fa9e4066Sahrens 	/* check options */
1673f9af39baSGeorge Wilson 	while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1674fa9e4066Sahrens 		switch (c) {
1675fa9e4066Sahrens 		case 'a':
167699653d4eSeschrock 			do_all = B_TRUE;
1677fa9e4066Sahrens 			break;
16782f8aaab3Seschrock 		case 'c':
16792f8aaab3Seschrock 			cachefile = optarg;
16802f8aaab3Seschrock 			break;
1681fa9e4066Sahrens 		case 'd':
1682fa9e4066Sahrens 			if (searchdirs == NULL) {
1683fa9e4066Sahrens 				searchdirs = safe_malloc(sizeof (char *));
1684fa9e4066Sahrens 			} else {
1685fa9e4066Sahrens 				char **tmp = safe_malloc((nsearch + 1) *
1686fa9e4066Sahrens 				    sizeof (char *));
1687fa9e4066Sahrens 				bcopy(searchdirs, tmp, nsearch *
1688fa9e4066Sahrens 				    sizeof (char *));
1689fa9e4066Sahrens 				free(searchdirs);
1690fa9e4066Sahrens 				searchdirs = tmp;
1691fa9e4066Sahrens 			}
1692fa9e4066Sahrens 			searchdirs[nsearch++] = optarg;
1693fa9e4066Sahrens 			break;
16944c58d714Sdarrenm 		case 'D':
169599653d4eSeschrock 			do_destroyed = B_TRUE;
16964c58d714Sdarrenm 			break;
1697fa9e4066Sahrens 		case 'f':
16984b964adaSGeorge Wilson 			flags |= ZFS_IMPORT_ANY_HOST;
1699fa9e4066Sahrens 			break;
1700c5904d13Seschrock 		case 'F':
1701468c413aSTim Haley 			do_rewind = B_TRUE;
1702468c413aSTim Haley 			break;
17034b964adaSGeorge Wilson 		case 'm':
17044b964adaSGeorge Wilson 			flags |= ZFS_IMPORT_MISSING_LOG;
17054b964adaSGeorge Wilson 			break;
1706468c413aSTim Haley 		case 'n':
1707468c413aSTim Haley 			dryrun = B_TRUE;
1708c5904d13Seschrock 			break;
1709f9af39baSGeorge Wilson 		case 'N':
1710f9af39baSGeorge Wilson 			flags |= ZFS_IMPORT_ONLY;
1711f9af39baSGeorge Wilson 			break;
1712fa9e4066Sahrens 		case 'o':
1713990b4856Slling 			if ((propval = strchr(optarg, '=')) != NULL) {
1714990b4856Slling 				*propval = '\0';
1715990b4856Slling 				propval++;
17160a48a24eStimh 				if (add_prop_list(optarg, propval,
17170a48a24eStimh 				    &props, B_TRUE))
1718990b4856Slling 					goto error;
1719990b4856Slling 			} else {
1720fa9e4066Sahrens 				mntopts = optarg;
1721990b4856Slling 			}
1722fa9e4066Sahrens 			break;
1723fa9e4066Sahrens 		case 'R':
1724990b4856Slling 			if (add_prop_list(zpool_prop_to_name(
17250a48a24eStimh 			    ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1726990b4856Slling 				goto error;
17272f8aaab3Seschrock 			if (nvlist_lookup_string(props,
17282f8aaab3Seschrock 			    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
17292f8aaab3Seschrock 			    &propval) == 0)
17302f8aaab3Seschrock 				break;
1731990b4856Slling 			if (add_prop_list(zpool_prop_to_name(
17320a48a24eStimh 			    ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1733990b4856Slling 				goto error;
1734fa9e4066Sahrens 			break;
1735f9af39baSGeorge Wilson 		case 'T':
1736f9af39baSGeorge Wilson 			errno = 0;
1737f9af39baSGeorge Wilson 			txg = strtoull(optarg, &endptr, 10);
1738f9af39baSGeorge Wilson 			if (errno != 0 || *endptr != '\0') {
1739f9af39baSGeorge Wilson 				(void) fprintf(stderr,
1740f9af39baSGeorge Wilson 				    gettext("invalid txg value\n"));
1741f9af39baSGeorge Wilson 				usage(B_FALSE);
1742f9af39baSGeorge Wilson 			}
1743f9af39baSGeorge Wilson 			rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
1744f9af39baSGeorge Wilson 			break;
1745468c413aSTim Haley 		case 'V':
17464b964adaSGeorge Wilson 			flags |= ZFS_IMPORT_VERBATIM;
1747468c413aSTim Haley 			break;
1748468c413aSTim Haley 		case 'X':
1749468c413aSTim Haley 			xtreme_rewind = B_TRUE;
1750468c413aSTim Haley 			break;
1751fa9e4066Sahrens 		case ':':
1752fa9e4066Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
1753fa9e4066Sahrens 			    "'%c' option\n"), optopt);
175499653d4eSeschrock 			usage(B_FALSE);
1755fa9e4066Sahrens 			break;
1756fa9e4066Sahrens 		case '?':
1757fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1758fa9e4066Sahrens 			    optopt);
175999653d4eSeschrock 			usage(B_FALSE);
1760fa9e4066Sahrens 		}
1761fa9e4066Sahrens 	}
1762fa9e4066Sahrens 
1763fa9e4066Sahrens 	argc -= optind;
1764fa9e4066Sahrens 	argv += optind;
1765fa9e4066Sahrens 
17662f8aaab3Seschrock 	if (cachefile && nsearch != 0) {
17672f8aaab3Seschrock 		(void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
17682f8aaab3Seschrock 		usage(B_FALSE);
17692f8aaab3Seschrock 	}
17702f8aaab3Seschrock 
1771468c413aSTim Haley 	if ((dryrun || xtreme_rewind) && !do_rewind) {
1772468c413aSTim Haley 		(void) fprintf(stderr,
1773468c413aSTim Haley 		    gettext("-n or -X only meaningful with -F\n"));
1774468c413aSTim Haley 		usage(B_FALSE);
1775468c413aSTim Haley 	}
1776468c413aSTim Haley 	if (dryrun)
1777468c413aSTim Haley 		rewind_policy = ZPOOL_TRY_REWIND;
1778468c413aSTim Haley 	else if (do_rewind)
1779468c413aSTim Haley 		rewind_policy = ZPOOL_DO_REWIND;
1780468c413aSTim Haley 	if (xtreme_rewind)
1781468c413aSTim Haley 		rewind_policy |= ZPOOL_EXTREME_REWIND;
1782468c413aSTim Haley 
1783468c413aSTim Haley 	/* In the future, we can capture further policy and include it here */
1784468c413aSTim Haley 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
1785f9af39baSGeorge Wilson 	    nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
1786468c413aSTim Haley 	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
1787468c413aSTim Haley 		goto error;
1788468c413aSTim Haley 
1789fa9e4066Sahrens 	if (searchdirs == NULL) {
1790fa9e4066Sahrens 		searchdirs = safe_malloc(sizeof (char *));
1791fa9e4066Sahrens 		searchdirs[0] = "/dev/dsk";
1792fa9e4066Sahrens 		nsearch = 1;
1793fa9e4066Sahrens 	}
1794fa9e4066Sahrens 
1795fa9e4066Sahrens 	/* check argument count */
1796fa9e4066Sahrens 	if (do_all) {
1797fa9e4066Sahrens 		if (argc != 0) {
1798fa9e4066Sahrens 			(void) fprintf(stderr, gettext("too many arguments\n"));
179999653d4eSeschrock 			usage(B_FALSE);
1800fa9e4066Sahrens 		}
1801fa9e4066Sahrens 	} else {
1802fa9e4066Sahrens 		if (argc > 2) {
1803fa9e4066Sahrens 			(void) fprintf(stderr, gettext("too many arguments\n"));
180499653d4eSeschrock 			usage(B_FALSE);
1805fa9e4066Sahrens 		}
1806fa9e4066Sahrens 
1807fa9e4066Sahrens 		/*
1808fa9e4066Sahrens 		 * Check for the SYS_CONFIG privilege.  We do this explicitly
1809fa9e4066Sahrens 		 * here because otherwise any attempt to discover pools will
1810fa9e4066Sahrens 		 * silently fail.
1811fa9e4066Sahrens 		 */
1812fa9e4066Sahrens 		if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
1813fa9e4066Sahrens 			(void) fprintf(stderr, gettext("cannot "
1814fa9e4066Sahrens 			    "discover pools: permission denied\n"));
181599653d4eSeschrock 			free(searchdirs);
1816468c413aSTim Haley 			nvlist_free(policy);
1817fa9e4066Sahrens 			return (1);
1818fa9e4066Sahrens 		}
1819fa9e4066Sahrens 	}
1820fa9e4066Sahrens 
1821fa9e4066Sahrens 	/*
1822fa9e4066Sahrens 	 * Depending on the arguments given, we do one of the following:
1823fa9e4066Sahrens 	 *
1824fa9e4066Sahrens 	 *	<none>	Iterate through all pools and display information about
1825fa9e4066Sahrens 	 *		each one.
1826fa9e4066Sahrens 	 *
1827fa9e4066Sahrens 	 *	-a	Iterate through all pools and try to import each one.
1828fa9e4066Sahrens 	 *
1829fa9e4066Sahrens 	 *	<id>	Find the pool that corresponds to the given GUID/pool
1830fa9e4066Sahrens 	 *		name and import that one.
18314c58d714Sdarrenm 	 *
18324c58d714Sdarrenm 	 *	-D	Above options applies only to destroyed pools.
1833fa9e4066Sahrens 	 */
1834fa9e4066Sahrens 	if (argc != 0) {
1835fa9e4066Sahrens 		char *endptr;
1836fa9e4066Sahrens 
1837fa9e4066Sahrens 		errno = 0;
1838fa9e4066Sahrens 		searchguid = strtoull(argv[0], &endptr, 10);
1839fa9e4066Sahrens 		if (errno != 0 || *endptr != '\0')
1840fa9e4066Sahrens 			searchname = argv[0];
1841fa9e4066Sahrens 		found_config = NULL;
1842fa9e4066Sahrens 
184324e697d4Sck153898 		/*
1844d41c4376SMark J Musante 		 * User specified a name or guid.  Ensure it's unique.
184524e697d4Sck153898 		 */
1846d41c4376SMark J Musante 		idata.unique = B_TRUE;
184724e697d4Sck153898 	}
184824e697d4Sck153898 
1849d41c4376SMark J Musante 
1850d41c4376SMark J Musante 	idata.path = searchdirs;
1851d41c4376SMark J Musante 	idata.paths = nsearch;
1852d41c4376SMark J Musante 	idata.poolname = searchname;
1853d41c4376SMark J Musante 	idata.guid = searchguid;
1854d41c4376SMark J Musante 	idata.cachefile = cachefile;
1855d41c4376SMark J Musante 
1856d41c4376SMark J Musante 	pools = zpool_search_import(g_zfs, &idata);
1857d41c4376SMark J Musante 
1858d41c4376SMark J Musante 	if (pools != NULL && idata.exists &&
1859d41c4376SMark J Musante 	    (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
1860d41c4376SMark J Musante 		(void) fprintf(stderr, gettext("cannot import '%s': "
1861d41c4376SMark J Musante 		    "a pool with that name already exists\n"),
1862d41c4376SMark J Musante 		    argv[0]);
1863d41c4376SMark J Musante 		(void) fprintf(stderr, gettext("use the form '%s "
1864d41c4376SMark J Musante 		    "<pool | id> <newpool>' to give it a new name\n"),
1865d41c4376SMark J Musante 		    "zpool import");
1866d41c4376SMark J Musante 		err = 1;
1867d41c4376SMark J Musante 	} else if (pools == NULL && idata.exists) {
1868d41c4376SMark J Musante 		(void) fprintf(stderr, gettext("cannot import '%s': "
1869d41c4376SMark J Musante 		    "a pool with that name is already created/imported,\n"),
1870d41c4376SMark J Musante 		    argv[0]);
1871d41c4376SMark J Musante 		(void) fprintf(stderr, gettext("and no additional pools "
1872d41c4376SMark J Musante 		    "with that name were found\n"));
1873d41c4376SMark J Musante 		err = 1;
1874d41c4376SMark J Musante 	} else if (pools == NULL) {
187524e697d4Sck153898 		if (argc != 0) {
187624e697d4Sck153898 			(void) fprintf(stderr, gettext("cannot import '%s': "
187724e697d4Sck153898 			    "no such pool available\n"), argv[0]);
187824e697d4Sck153898 		}
1879d41c4376SMark J Musante 		err = 1;
1880d41c4376SMark J Musante 	}
1881d41c4376SMark J Musante 
1882d41c4376SMark J Musante 	if (err == 1) {
188324e697d4Sck153898 		free(searchdirs);
1884468c413aSTim Haley 		nvlist_free(policy);
188524e697d4Sck153898 		return (1);
188624e697d4Sck153898 	}
188724e697d4Sck153898 
188824e697d4Sck153898 	/*
188924e697d4Sck153898 	 * At this point we have a list of import candidate configs. Even if
189024e697d4Sck153898 	 * we were searching by pool name or guid, we still need to
189124e697d4Sck153898 	 * post-process the list to deal with pool state and possible
189224e697d4Sck153898 	 * duplicate names.
189324e697d4Sck153898 	 */
1894fa9e4066Sahrens 	err = 0;
1895fa9e4066Sahrens 	elem = NULL;
189699653d4eSeschrock 	first = B_TRUE;
1897fa9e4066Sahrens 	while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
1898fa9e4066Sahrens 
1899fa9e4066Sahrens 		verify(nvpair_value_nvlist(elem, &config) == 0);
1900fa9e4066Sahrens 
19014c58d714Sdarrenm 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
19024c58d714Sdarrenm 		    &pool_state) == 0);
19034c58d714Sdarrenm 		if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
19044c58d714Sdarrenm 			continue;
19054c58d714Sdarrenm 		if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
19064c58d714Sdarrenm 			continue;
19074c58d714Sdarrenm 
1908468c413aSTim Haley 		verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
1909468c413aSTim Haley 		    policy) == 0);
1910468c413aSTim Haley 
1911fa9e4066Sahrens 		if (argc == 0) {
1912fa9e4066Sahrens 			if (first)
191399653d4eSeschrock 				first = B_FALSE;
19143bb79becSeschrock 			else if (!do_all)
1915fa9e4066Sahrens 				(void) printf("\n");
1916fa9e4066Sahrens 
1917468c413aSTim Haley 			if (do_all) {
1918fa9e4066Sahrens 				err |= do_import(config, NULL, mntopts,
19194b964adaSGeorge Wilson 				    props, flags);
1920468c413aSTim Haley 			} else {
1921fa9e4066Sahrens 				show_import(config);
1922468c413aSTim Haley 			}
1923fa9e4066Sahrens 		} else if (searchname != NULL) {
1924fa9e4066Sahrens 			char *name;
1925fa9e4066Sahrens 
1926fa9e4066Sahrens 			/*
1927fa9e4066Sahrens 			 * We are searching for a pool based on name.
1928fa9e4066Sahrens 			 */
1929fa9e4066Sahrens 			verify(nvlist_lookup_string(config,
1930fa9e4066Sahrens 			    ZPOOL_CONFIG_POOL_NAME, &name) == 0);
1931fa9e4066Sahrens 
1932fa9e4066Sahrens 			if (strcmp(name, searchname) == 0) {
1933fa9e4066Sahrens 				if (found_config != NULL) {
1934fa9e4066Sahrens 					(void) fprintf(stderr, gettext(
1935fa9e4066Sahrens 					    "cannot import '%s': more than "
1936fa9e4066Sahrens 					    "one matching pool\n"), searchname);
1937fa9e4066Sahrens 					(void) fprintf(stderr, gettext(
1938fa9e4066Sahrens 					    "import by numeric ID instead\n"));
193999653d4eSeschrock 					err = B_TRUE;
1940fa9e4066Sahrens 				}
1941fa9e4066Sahrens 				found_config = config;
1942fa9e4066Sahrens 			}
1943fa9e4066Sahrens 		} else {
1944fa9e4066Sahrens 			uint64_t guid;
1945fa9e4066Sahrens 
1946fa9e4066Sahrens 			/*
1947fa9e4066Sahrens 			 * Search for a pool by guid.
1948fa9e4066Sahrens 			 */
1949fa9e4066Sahrens 			verify(nvlist_lookup_uint64(config,
1950fa9e4066Sahrens 			    ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
1951fa9e4066Sahrens 
1952fa9e4066Sahrens 			if (guid == searchguid)
1953fa9e4066Sahrens 				found_config = config;
1954fa9e4066Sahrens 		}
1955fa9e4066Sahrens 	}
1956fa9e4066Sahrens 
1957fa9e4066Sahrens 	/*
1958fa9e4066Sahrens 	 * If we were searching for a specific pool, verify that we found a
1959fa9e4066Sahrens 	 * pool, and then do the import.
1960fa9e4066Sahrens 	 */
1961fa9e4066Sahrens 	if (argc != 0 && err == 0) {
1962fa9e4066Sahrens 		if (found_config == NULL) {
1963fa9e4066Sahrens 			(void) fprintf(stderr, gettext("cannot import '%s': "
1964fa9e4066Sahrens 			    "no such pool available\n"), argv[0]);
196599653d4eSeschrock 			err = B_TRUE;
1966fa9e4066Sahrens 		} else {
1967fa9e4066Sahrens 			err |= do_import(found_config, argc == 1 ? NULL :
19684b964adaSGeorge Wilson 			    argv[1], mntopts, props, flags);
1969fa9e4066Sahrens 		}
1970fa9e4066Sahrens 	}
1971fa9e4066Sahrens 
1972fa9e4066Sahrens 	/*
1973fa9e4066Sahrens 	 * If we were just looking for pools, report an error if none were
1974fa9e4066Sahrens 	 * found.
1975fa9e4066Sahrens 	 */
1976fa9e4066Sahrens 	if (argc == 0 && first)
1977fa9e4066Sahrens 		(void) fprintf(stderr,
1978fa9e4066Sahrens 		    gettext("no pools available to import\n"));
1979fa9e4066Sahrens 
1980ecd6cf80Smarks error:
1981ecd6cf80Smarks 	nvlist_free(props);
1982fa9e4066Sahrens 	nvlist_free(pools);
1983468c413aSTim Haley 	nvlist_free(policy);
198499653d4eSeschrock 	free(searchdirs);
1985fa9e4066Sahrens 
1986fa9e4066Sahrens 	return (err ? 1 : 0);
1987fa9e4066Sahrens }
1988fa9e4066Sahrens 
1989fa9e4066Sahrens typedef struct iostat_cbdata {
19904263d13fSGeorge Wilson 	boolean_t cb_verbose;
1991fa9e4066Sahrens 	int cb_namewidth;
19924263d13fSGeorge Wilson 	int cb_iteration;
19934263d13fSGeorge Wilson 	zpool_list_t *cb_list;
1994fa9e4066Sahrens } iostat_cbdata_t;
1995fa9e4066Sahrens 
1996fa9e4066Sahrens static void
1997fa9e4066Sahrens print_iostat_separator(iostat_cbdata_t *cb)
1998fa9e4066Sahrens {
1999fa9e4066Sahrens 	int i = 0;
2000fa9e4066Sahrens 
2001fa9e4066Sahrens 	for (i = 0; i < cb->cb_namewidth; i++)
2002fa9e4066Sahrens 		(void) printf("-");
2003fa9e4066Sahrens 	(void) printf("  -----  -----  -----  -----  -----  -----\n");
2004fa9e4066Sahrens }
2005fa9e4066Sahrens 
2006fa9e4066Sahrens static void
2007fa9e4066Sahrens print_iostat_header(iostat_cbdata_t *cb)
2008fa9e4066Sahrens {
2009fa9e4066Sahrens 	(void) printf("%*s     capacity     operations    bandwidth\n",
2010fa9e4066Sahrens 	    cb->cb_namewidth, "");
2011485bbbf5SGeorge Wilson 	(void) printf("%-*s  alloc   free   read  write   read  write\n",
2012fa9e4066Sahrens 	    cb->cb_namewidth, "pool");
2013fa9e4066Sahrens 	print_iostat_separator(cb);
2014fa9e4066Sahrens }
2015fa9e4066Sahrens 
2016fa9e4066Sahrens /*
2017fa9e4066Sahrens  * Display a single statistic.
2018fa9e4066Sahrens  */
2019990b4856Slling static void
2020fa9e4066Sahrens print_one_stat(uint64_t value)
2021fa9e4066Sahrens {
2022fa9e4066Sahrens 	char buf[64];
2023fa9e4066Sahrens 
2024fa9e4066Sahrens 	zfs_nicenum(value, buf, sizeof (buf));
2025fa9e4066Sahrens 	(void) printf("  %5s", buf);
2026fa9e4066Sahrens }
2027fa9e4066Sahrens 
2028fa9e4066Sahrens /*
2029fa9e4066Sahrens  * Print out all the statistics for the given vdev.  This can either be the
2030fa9e4066Sahrens  * toplevel configuration, or called recursively.  If 'name' is NULL, then this
2031fa9e4066Sahrens  * is a verbose output, and we don't want to display the toplevel pool stats.
2032fa9e4066Sahrens  */
2033fa9e4066Sahrens void
2034c67d9675Seschrock print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2035c67d9675Seschrock     nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2036fa9e4066Sahrens {
2037fa9e4066Sahrens 	nvlist_t **oldchild, **newchild;
2038fa9e4066Sahrens 	uint_t c, children;
2039fa9e4066Sahrens 	vdev_stat_t *oldvs, *newvs;
2040fa9e4066Sahrens 	vdev_stat_t zerovs = { 0 };
2041fa9e4066Sahrens 	uint64_t tdelta;
2042fa9e4066Sahrens 	double scale;
2043afefbcddSeschrock 	char *vname;
2044fa9e4066Sahrens 
2045fa9e4066Sahrens 	if (oldnv != NULL) {
20463f9d6ad7SLin Ling 		verify(nvlist_lookup_uint64_array(oldnv,
20473f9d6ad7SLin Ling 		    ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2048fa9e4066Sahrens 	} else {
2049fa9e4066Sahrens 		oldvs = &zerovs;
2050fa9e4066Sahrens 	}
2051fa9e4066Sahrens 
20523f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2053fa9e4066Sahrens 	    (uint64_t **)&newvs, &c) == 0);
2054fa9e4066Sahrens 
2055fa9e4066Sahrens 	if (strlen(name) + depth > cb->cb_namewidth)
2056fa9e4066Sahrens 		(void) printf("%*s%s", depth, "", name);
2057fa9e4066Sahrens 	else
2058fa9e4066Sahrens 		(void) printf("%*s%s%*s", depth, "", name,
2059fa9e4066Sahrens 		    (int)(cb->cb_namewidth - strlen(name) - depth), "");
2060fa9e4066Sahrens 
2061fa9e4066Sahrens 	tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2062fa9e4066Sahrens 
2063fa9e4066Sahrens 	if (tdelta == 0)
2064fa9e4066Sahrens 		scale = 1.0;
2065fa9e4066Sahrens 	else
2066fa9e4066Sahrens 		scale = (double)NANOSEC / tdelta;
2067fa9e4066Sahrens 
2068fa9e4066Sahrens 	/* only toplevel vdevs have capacity stats */
2069fa9e4066Sahrens 	if (newvs->vs_space == 0) {
2070fa9e4066Sahrens 		(void) printf("      -      -");
2071fa9e4066Sahrens 	} else {
2072fa9e4066Sahrens 		print_one_stat(newvs->vs_alloc);
2073fa9e4066Sahrens 		print_one_stat(newvs->vs_space - newvs->vs_alloc);
2074fa9e4066Sahrens 	}
2075fa9e4066Sahrens 
2076fa9e4066Sahrens 	print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2077fa9e4066Sahrens 	    oldvs->vs_ops[ZIO_TYPE_READ])));
2078fa9e4066Sahrens 
2079fa9e4066Sahrens 	print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2080fa9e4066Sahrens 	    oldvs->vs_ops[ZIO_TYPE_WRITE])));
2081fa9e4066Sahrens 
2082fa9e4066Sahrens 	print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2083fa9e4066Sahrens 	    oldvs->vs_bytes[ZIO_TYPE_READ])));
2084fa9e4066Sahrens 
2085fa9e4066Sahrens 	print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2086fa9e4066Sahrens 	    oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2087fa9e4066Sahrens 
2088fa9e4066Sahrens 	(void) printf("\n");
2089fa9e4066Sahrens 
2090fa9e4066Sahrens 	if (!cb->cb_verbose)
2091fa9e4066Sahrens 		return;
2092fa9e4066Sahrens 
2093fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2094fa9e4066Sahrens 	    &newchild, &children) != 0)
2095fa9e4066Sahrens 		return;
2096fa9e4066Sahrens 
2097fa9e4066Sahrens 	if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2098fa9e4066Sahrens 	    &oldchild, &c) != 0)
2099fa9e4066Sahrens 		return;
2100fa9e4066Sahrens 
2101afefbcddSeschrock 	for (c = 0; c < children; c++) {
21029d439f90SMike Harsch 		uint64_t ishole = B_FALSE, islog = B_FALSE;
21033f9d6ad7SLin Ling 
21049d439f90SMike Harsch 		(void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
21059d439f90SMike Harsch 		    &ishole);
21069d439f90SMike Harsch 
21079d439f90SMike Harsch 		(void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
21089d439f90SMike Harsch 		    &islog);
21099d439f90SMike Harsch 
21109d439f90SMike Harsch 		if (ishole || islog)
21113f9d6ad7SLin Ling 			continue;
21123f9d6ad7SLin Ling 
211388ecc943SGeorge Wilson 		vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2114c67d9675Seschrock 		print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2115afefbcddSeschrock 		    newchild[c], cb, depth + 2);
2116afefbcddSeschrock 		free(vname);
2117afefbcddSeschrock 	}
2118fa94a07fSbrendan 
2119fa94a07fSbrendan 	/*
21209d439f90SMike Harsch 	 * Log device section
21219d439f90SMike Harsch 	 */
21229d439f90SMike Harsch 
21239d439f90SMike Harsch 	if (num_logs(newnv) > 0) {
21249d439f90SMike Harsch 		(void) printf("%-*s      -      -      -      -      -      "
21259d439f90SMike Harsch 		    "-\n", cb->cb_namewidth, "logs");
21269d439f90SMike Harsch 
21279d439f90SMike Harsch 		for (c = 0; c < children; c++) {
21289d439f90SMike Harsch 			uint64_t islog = B_FALSE;
21299d439f90SMike Harsch 			(void) nvlist_lookup_uint64(newchild[c],
21309d439f90SMike Harsch 			    ZPOOL_CONFIG_IS_LOG, &islog);
21319d439f90SMike Harsch 
21329d439f90SMike Harsch 			if (islog) {
21339d439f90SMike Harsch 				vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
21349d439f90SMike Harsch 				    B_FALSE);
21359d439f90SMike Harsch 				print_vdev_stats(zhp, vname, oldnv ?
21369d439f90SMike Harsch 				    oldchild[c] : NULL, newchild[c],
21379d439f90SMike Harsch 				    cb, depth + 2);
21389d439f90SMike Harsch 				free(vname);
21399d439f90SMike Harsch 			}
21409d439f90SMike Harsch 		}
21419d439f90SMike Harsch 
21429d439f90SMike Harsch 	}
21439d439f90SMike Harsch 
21449d439f90SMike Harsch 	/*
2145fa94a07fSbrendan 	 * Include level 2 ARC devices in iostat output
2146fa94a07fSbrendan 	 */
2147fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2148fa94a07fSbrendan 	    &newchild, &children) != 0)
2149fa94a07fSbrendan 		return;
2150fa94a07fSbrendan 
2151fa94a07fSbrendan 	if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2152fa94a07fSbrendan 	    &oldchild, &c) != 0)
2153fa94a07fSbrendan 		return;
2154fa94a07fSbrendan 
2155fa94a07fSbrendan 	if (children > 0) {
2156fa94a07fSbrendan 		(void) printf("%-*s      -      -      -      -      -      "
2157fa94a07fSbrendan 		    "-\n", cb->cb_namewidth, "cache");
2158fa94a07fSbrendan 		for (c = 0; c < children; c++) {
215988ecc943SGeorge Wilson 			vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
216088ecc943SGeorge Wilson 			    B_FALSE);
2161fa94a07fSbrendan 			print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2162fa94a07fSbrendan 			    newchild[c], cb, depth + 2);
2163fa94a07fSbrendan 			free(vname);
2164fa94a07fSbrendan 		}
2165fa94a07fSbrendan 	}
2166fa9e4066Sahrens }
2167fa9e4066Sahrens 
2168088e9d47Seschrock static int
2169088e9d47Seschrock refresh_iostat(zpool_handle_t *zhp, void *data)
2170088e9d47Seschrock {
2171088e9d47Seschrock 	iostat_cbdata_t *cb = data;
217294de1d4cSeschrock 	boolean_t missing;
2173088e9d47Seschrock 
2174088e9d47Seschrock 	/*
2175088e9d47Seschrock 	 * If the pool has disappeared, remove it from the list and continue.
2176088e9d47Seschrock 	 */
217794de1d4cSeschrock 	if (zpool_refresh_stats(zhp, &missing) != 0)
217894de1d4cSeschrock 		return (-1);
217994de1d4cSeschrock 
218094de1d4cSeschrock 	if (missing)
2181088e9d47Seschrock 		pool_list_remove(cb->cb_list, zhp);
2182088e9d47Seschrock 
2183088e9d47Seschrock 	return (0);
2184088e9d47Seschrock }
2185088e9d47Seschrock 
2186fa9e4066Sahrens /*
2187fa9e4066Sahrens  * Callback to print out the iostats for the given pool.
2188fa9e4066Sahrens  */
2189fa9e4066Sahrens int
2190fa9e4066Sahrens print_iostat(zpool_handle_t *zhp, void *data)
2191fa9e4066Sahrens {
2192fa9e4066Sahrens 	iostat_cbdata_t *cb = data;
2193fa9e4066Sahrens 	nvlist_t *oldconfig, *newconfig;
2194fa9e4066Sahrens 	nvlist_t *oldnvroot, *newnvroot;
2195fa9e4066Sahrens 
2196088e9d47Seschrock 	newconfig = zpool_get_config(zhp, &oldconfig);
2197fa9e4066Sahrens 
2198088e9d47Seschrock 	if (cb->cb_iteration == 1)
2199fa9e4066Sahrens 		oldconfig = NULL;
2200fa9e4066Sahrens 
2201fa9e4066Sahrens 	verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2202fa9e4066Sahrens 	    &newnvroot) == 0);
2203fa9e4066Sahrens 
2204088e9d47Seschrock 	if (oldconfig == NULL)
2205fa9e4066Sahrens 		oldnvroot = NULL;
2206088e9d47Seschrock 	else
2207088e9d47Seschrock 		verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2208088e9d47Seschrock 		    &oldnvroot) == 0);
2209fa9e4066Sahrens 
2210fa9e4066Sahrens 	/*
2211fa9e4066Sahrens 	 * Print out the statistics for the pool.
2212fa9e4066Sahrens 	 */
2213c67d9675Seschrock 	print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2214fa9e4066Sahrens 
2215fa9e4066Sahrens 	if (cb->cb_verbose)
2216fa9e4066Sahrens 		print_iostat_separator(cb);
2217fa9e4066Sahrens 
2218fa9e4066Sahrens 	return (0);
2219fa9e4066Sahrens }
2220fa9e4066Sahrens 
2221fa9e4066Sahrens int
2222fa9e4066Sahrens get_namewidth(zpool_handle_t *zhp, void *data)
2223fa9e4066Sahrens {
2224fa9e4066Sahrens 	iostat_cbdata_t *cb = data;
2225fa9e4066Sahrens 	nvlist_t *config, *nvroot;
2226fa9e4066Sahrens 
2227088e9d47Seschrock 	if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2228fa9e4066Sahrens 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2229fa9e4066Sahrens 		    &nvroot) == 0);
2230fa9e4066Sahrens 		if (!cb->cb_verbose)
2231fa9e4066Sahrens 			cb->cb_namewidth = strlen(zpool_get_name(zhp));
2232fa9e4066Sahrens 		else
2233e1d5e507SFrederik Wessels 			cb->cb_namewidth = max_width(zhp, nvroot, 0,
2234e1d5e507SFrederik Wessels 			    cb->cb_namewidth);
2235fa9e4066Sahrens 	}
2236fa9e4066Sahrens 
2237fa9e4066Sahrens 	/*
2238fa9e4066Sahrens 	 * The width must fall into the range [10,38].  The upper limit is the
2239fa9e4066Sahrens 	 * maximum we can have and still fit in 80 columns.
2240fa9e4066Sahrens 	 */
2241fa9e4066Sahrens 	if (cb->cb_namewidth < 10)
2242fa9e4066Sahrens 		cb->cb_namewidth = 10;
2243fa9e4066Sahrens 	if (cb->cb_namewidth > 38)
2244fa9e4066Sahrens 		cb->cb_namewidth = 38;
2245fa9e4066Sahrens 
2246fa9e4066Sahrens 	return (0);
2247fa9e4066Sahrens }
2248fa9e4066Sahrens 
2249fa9e4066Sahrens /*
22503f9d6ad7SLin Ling  * Parse the input string, get the 'interval' and 'count' value if there is one.
2251fa9e4066Sahrens  */
22523f9d6ad7SLin Ling static void
22533f9d6ad7SLin Ling get_interval_count(int *argcp, char **argv, unsigned long *iv,
22543f9d6ad7SLin Ling     unsigned long *cnt)
2255fa9e4066Sahrens {
2256fa9e4066Sahrens 	unsigned long interval = 0, count = 0;
22573f9d6ad7SLin Ling 	int argc = *argcp, errno;
2258fa9e4066Sahrens 
2259fa9e4066Sahrens 	/*
2260fa9e4066Sahrens 	 * Determine if the last argument is an integer or a pool name
2261fa9e4066Sahrens 	 */
2262fa9e4066Sahrens 	if (argc > 0 && isdigit(argv[argc - 1][0])) {
2263fa9e4066Sahrens 		char *end;
2264fa9e4066Sahrens 
2265fa9e4066Sahrens 		errno = 0;
2266fa9e4066Sahrens 		interval = strtoul(argv[argc - 1], &end, 10);
2267fa9e4066Sahrens 
2268fa9e4066Sahrens 		if (*end == '\0' && errno == 0) {
2269fa9e4066Sahrens 			if (interval == 0) {
2270fa9e4066Sahrens 				(void) fprintf(stderr, gettext("interval "
2271fa9e4066Sahrens 				    "cannot be zero\n"));
227299653d4eSeschrock 				usage(B_FALSE);
2273fa9e4066Sahrens 			}
2274fa9e4066Sahrens 			/*
2275fa9e4066Sahrens 			 * Ignore the last parameter
2276fa9e4066Sahrens 			 */
2277fa9e4066Sahrens 			argc--;
2278fa9e4066Sahrens 		} else {
2279fa9e4066Sahrens 			/*
2280fa9e4066Sahrens 			 * If this is not a valid number, just plow on.  The
2281fa9e4066Sahrens 			 * user will get a more informative error message later
2282fa9e4066Sahrens 			 * on.
2283fa9e4066Sahrens 			 */
2284fa9e4066Sahrens 			interval = 0;
2285fa9e4066Sahrens 		}
2286fa9e4066Sahrens 	}
2287fa9e4066Sahrens 
2288fa9e4066Sahrens 	/*
2289fa9e4066Sahrens 	 * If the last argument is also an integer, then we have both a count
22903f9d6ad7SLin Ling 	 * and an interval.
2291fa9e4066Sahrens 	 */
2292fa9e4066Sahrens 	if (argc > 0 && isdigit(argv[argc - 1][0])) {
2293fa9e4066Sahrens 		char *end;
2294fa9e4066Sahrens 
2295fa9e4066Sahrens 		errno = 0;
2296fa9e4066Sahrens 		count = interval;
2297fa9e4066Sahrens 		interval = strtoul(argv[argc - 1], &end, 10);
2298fa9e4066Sahrens 
2299fa9e4066Sahrens 		if (*end == '\0' && errno == 0) {
2300fa9e4066Sahrens 			if (interval == 0) {
2301fa9e4066Sahrens 				(void) fprintf(stderr, gettext("interval "
2302fa9e4066Sahrens 				    "cannot be zero\n"));
230399653d4eSeschrock 				usage(B_FALSE);
2304fa9e4066Sahrens 			}
2305fa9e4066Sahrens 
2306fa9e4066Sahrens 			/*
2307fa9e4066Sahrens 			 * Ignore the last parameter
2308fa9e4066Sahrens 			 */
2309fa9e4066Sahrens 			argc--;
2310fa9e4066Sahrens 		} else {
2311fa9e4066Sahrens 			interval = 0;
2312fa9e4066Sahrens 		}
2313fa9e4066Sahrens 	}
2314fa9e4066Sahrens 
23153f9d6ad7SLin Ling 	*iv = interval;
23163f9d6ad7SLin Ling 	*cnt = count;
23173f9d6ad7SLin Ling 	*argcp = argc;
23183f9d6ad7SLin Ling }
23193f9d6ad7SLin Ling 
23203f9d6ad7SLin Ling static void
23213f9d6ad7SLin Ling get_timestamp_arg(char c)
23223f9d6ad7SLin Ling {
23233f9d6ad7SLin Ling 	if (c == 'u')
23243f9d6ad7SLin Ling 		timestamp_fmt = UDATE;
23253f9d6ad7SLin Ling 	else if (c == 'd')
23263f9d6ad7SLin Ling 		timestamp_fmt = DDATE;
23273f9d6ad7SLin Ling 	else
23283f9d6ad7SLin Ling 		usage(B_FALSE);
23293f9d6ad7SLin Ling }
23303f9d6ad7SLin Ling 
23313f9d6ad7SLin Ling /*
23323f9d6ad7SLin Ling  * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
23333f9d6ad7SLin Ling  *
23343f9d6ad7SLin Ling  *	-v	Display statistics for individual vdevs
23353f9d6ad7SLin Ling  *	-T	Display a timestamp in date(1) or Unix format
23363f9d6ad7SLin Ling  *
23373f9d6ad7SLin Ling  * This command can be tricky because we want to be able to deal with pool
23383f9d6ad7SLin Ling  * creation/destruction as well as vdev configuration changes.  The bulk of this
23393f9d6ad7SLin Ling  * processing is handled by the pool_list_* routines in zpool_iter.c.  We rely
23403f9d6ad7SLin Ling  * on pool_list_update() to detect the addition of new pools.  Configuration
23413f9d6ad7SLin Ling  * changes are all handled within libzfs.
23423f9d6ad7SLin Ling  */
23433f9d6ad7SLin Ling int
23443f9d6ad7SLin Ling zpool_do_iostat(int argc, char **argv)
23453f9d6ad7SLin Ling {
23463f9d6ad7SLin Ling 	int c;
23473f9d6ad7SLin Ling 	int ret;
23483f9d6ad7SLin Ling 	int npools;
23493f9d6ad7SLin Ling 	unsigned long interval = 0, count = 0;
23503f9d6ad7SLin Ling 	zpool_list_t *list;
23513f9d6ad7SLin Ling 	boolean_t verbose = B_FALSE;
23523f9d6ad7SLin Ling 	iostat_cbdata_t cb;
23533f9d6ad7SLin Ling 
23543f9d6ad7SLin Ling 	/* check options */
23553f9d6ad7SLin Ling 	while ((c = getopt(argc, argv, "T:v")) != -1) {
23563f9d6ad7SLin Ling 		switch (c) {
23573f9d6ad7SLin Ling 		case 'T':
23583f9d6ad7SLin Ling 			get_timestamp_arg(*optarg);
23593f9d6ad7SLin Ling 			break;
23603f9d6ad7SLin Ling 		case 'v':
23613f9d6ad7SLin Ling 			verbose = B_TRUE;
23623f9d6ad7SLin Ling 			break;
23633f9d6ad7SLin Ling 		case '?':
23643f9d6ad7SLin Ling 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
23653f9d6ad7SLin Ling 			    optopt);
23663f9d6ad7SLin Ling 			usage(B_FALSE);
23673f9d6ad7SLin Ling 		}
23683f9d6ad7SLin Ling 	}
23693f9d6ad7SLin Ling 
23703f9d6ad7SLin Ling 	argc -= optind;
23713f9d6ad7SLin Ling 	argv += optind;
23723f9d6ad7SLin Ling 
23733f9d6ad7SLin Ling 	get_interval_count(&argc, argv, &interval, &count);
23743f9d6ad7SLin Ling 
2375fa9e4066Sahrens 	/*
2376fa9e4066Sahrens 	 * Construct the list of all interesting pools.
2377fa9e4066Sahrens 	 */
2378fa9e4066Sahrens 	ret = 0;
2379b1b8ab34Slling 	if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2380fa9e4066Sahrens 		return (1);
2381fa9e4066Sahrens 
238299653d4eSeschrock 	if (pool_list_count(list) == 0 && argc != 0) {
238399653d4eSeschrock 		pool_list_free(list);
2384fa9e4066Sahrens 		return (1);
238599653d4eSeschrock 	}
2386fa9e4066Sahrens 
2387fa9e4066Sahrens 	if (pool_list_count(list) == 0 && interval == 0) {
238899653d4eSeschrock 		pool_list_free(list);
2389fa9e4066Sahrens 		(void) fprintf(stderr, gettext("no pools available\n"));
2390fa9e4066Sahrens 		return (1);
2391fa9e4066Sahrens 	}
2392fa9e4066Sahrens 
2393fa9e4066Sahrens 	/*
2394fa9e4066Sahrens 	 * Enter the main iostat loop.
2395fa9e4066Sahrens 	 */
2396fa9e4066Sahrens 	cb.cb_list = list;
2397fa9e4066Sahrens 	cb.cb_verbose = verbose;
2398fa9e4066Sahrens 	cb.cb_iteration = 0;
2399fa9e4066Sahrens 	cb.cb_namewidth = 0;
2400fa9e4066Sahrens 
2401fa9e4066Sahrens 	for (;;) {
2402fa9e4066Sahrens 		pool_list_update(list);
2403fa9e4066Sahrens 
2404fa9e4066Sahrens 		if ((npools = pool_list_count(list)) == 0)
2405fa9e4066Sahrens 			break;
2406fa9e4066Sahrens 
2407fa9e4066Sahrens 		/*
2408088e9d47Seschrock 		 * Refresh all statistics.  This is done as an explicit step
2409088e9d47Seschrock 		 * before calculating the maximum name width, so that any
2410088e9d47Seschrock 		 * configuration changes are properly accounted for.
2411088e9d47Seschrock 		 */
241299653d4eSeschrock 		(void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2413088e9d47Seschrock 
2414088e9d47Seschrock 		/*
2415fa9e4066Sahrens 		 * Iterate over all pools to determine the maximum width
2416fa9e4066Sahrens 		 * for the pool / device name column across all pools.
2417fa9e4066Sahrens 		 */
2418fa9e4066Sahrens 		cb.cb_namewidth = 0;
241999653d4eSeschrock 		(void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2420fa9e4066Sahrens 
242126fd7700SKrishnendu Sadhukhan - Sun Microsystems 		if (timestamp_fmt != NODATE)
242226fd7700SKrishnendu Sadhukhan - Sun Microsystems 			print_timestamp(timestamp_fmt);
242326fd7700SKrishnendu Sadhukhan - Sun Microsystems 
2424fa9e4066Sahrens 		/*
2425fa9e4066Sahrens 		 * If it's the first time, or verbose mode, print the header.
2426fa9e4066Sahrens 		 */
2427fa9e4066Sahrens 		if (++cb.cb_iteration == 1 || verbose)
2428fa9e4066Sahrens 			print_iostat_header(&cb);
2429fa9e4066Sahrens 
243099653d4eSeschrock 		(void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2431fa9e4066Sahrens 
2432fa9e4066Sahrens 		/*
2433fa9e4066Sahrens 		 * If there's more than one pool, and we're not in verbose mode
2434fa9e4066Sahrens 		 * (which prints a separator for us), then print a separator.
2435fa9e4066Sahrens 		 */
2436fa9e4066Sahrens 		if (npools > 1 && !verbose)
2437fa9e4066Sahrens 			print_iostat_separator(&cb);
2438fa9e4066Sahrens 
2439fa9e4066Sahrens 		if (verbose)
2440fa9e4066Sahrens 			(void) printf("\n");
2441fa9e4066Sahrens 
244239c23413Seschrock 		/*
244339c23413Seschrock 		 * Flush the output so that redirection to a file isn't buffered
244439c23413Seschrock 		 * indefinitely.
244539c23413Seschrock 		 */
244639c23413Seschrock 		(void) fflush(stdout);
244739c23413Seschrock 
2448fa9e4066Sahrens 		if (interval == 0)
2449fa9e4066Sahrens 			break;
2450fa9e4066Sahrens 
2451fa9e4066Sahrens 		if (count != 0 && --count == 0)
2452fa9e4066Sahrens 			break;
2453fa9e4066Sahrens 
2454fa9e4066Sahrens 		(void) sleep(interval);
2455fa9e4066Sahrens 	}
2456fa9e4066Sahrens 
2457fa9e4066Sahrens 	pool_list_free(list);
2458fa9e4066Sahrens 
2459fa9e4066Sahrens 	return (ret);
2460fa9e4066Sahrens }
2461fa9e4066Sahrens 
2462fa9e4066Sahrens typedef struct list_cbdata {
24634263d13fSGeorge Wilson 	boolean_t	cb_verbose;
24644263d13fSGeorge Wilson 	int		cb_namewidth;
246599653d4eSeschrock 	boolean_t	cb_scripted;
2466990b4856Slling 	zprop_list_t	*cb_proplist;
2467fa9e4066Sahrens } list_cbdata_t;
2468fa9e4066Sahrens 
2469fa9e4066Sahrens /*
2470fa9e4066Sahrens  * Given a list of columns to display, output appropriate headers for each one.
2471fa9e4066Sahrens  */
2472990b4856Slling static void
24734263d13fSGeorge Wilson print_header(list_cbdata_t *cb)
2474fa9e4066Sahrens {
24754263d13fSGeorge Wilson 	zprop_list_t *pl = cb->cb_proplist;
2476990b4856Slling 	const char *header;
2477990b4856Slling 	boolean_t first = B_TRUE;
2478990b4856Slling 	boolean_t right_justify;
24794263d13fSGeorge Wilson 	size_t width = 0;
2480fa9e4066Sahrens 
2481990b4856Slling 	for (; pl != NULL; pl = pl->pl_next) {
2482990b4856Slling 		if (pl->pl_prop == ZPROP_INVAL)
2483990b4856Slling 			continue;
2484990b4856Slling 
24854263d13fSGeorge Wilson 		width = pl->pl_width;
24864263d13fSGeorge Wilson 		if (first && cb->cb_verbose) {
24874263d13fSGeorge Wilson 			/*
24884263d13fSGeorge Wilson 			 * Reset the width to accommodate the verbose listing
24894263d13fSGeorge Wilson 			 * of devices.
24904263d13fSGeorge Wilson 			 */
24914263d13fSGeorge Wilson 			width = cb->cb_namewidth;
24924263d13fSGeorge Wilson 		}
24934263d13fSGeorge Wilson 
2494990b4856Slling 		if (!first)
2495fa9e4066Sahrens 			(void) printf("  ");
2496fa9e4066Sahrens 		else
2497990b4856Slling 			first = B_FALSE;
2498fa9e4066Sahrens 
2499990b4856Slling 		header = zpool_prop_column_name(pl->pl_prop);
2500990b4856Slling 		right_justify = zpool_prop_align_right(pl->pl_prop);
2501990b4856Slling 
2502990b4856Slling 		if (pl->pl_next == NULL && !right_justify)
2503990b4856Slling 			(void) printf("%s", header);
2504990b4856Slling 		else if (right_justify)
25054263d13fSGeorge Wilson 			(void) printf("%*s", width, header);
2506990b4856Slling 		else
25074263d13fSGeorge Wilson 			(void) printf("%-*s", width, header);
25084263d13fSGeorge Wilson 
2509fa9e4066Sahrens 	}
2510fa9e4066Sahrens 
2511fa9e4066Sahrens 	(void) printf("\n");
2512fa9e4066Sahrens }
2513fa9e4066Sahrens 
2514990b4856Slling /*
2515990b4856Slling  * Given a pool and a list of properties, print out all the properties according
2516990b4856Slling  * to the described layout.
2517990b4856Slling  */
2518990b4856Slling static void
25194263d13fSGeorge Wilson print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2520990b4856Slling {
25214263d13fSGeorge Wilson 	zprop_list_t *pl = cb->cb_proplist;
2522990b4856Slling 	boolean_t first = B_TRUE;
2523990b4856Slling 	char property[ZPOOL_MAXPROPLEN];
2524990b4856Slling 	char *propstr;
2525990b4856Slling 	boolean_t right_justify;
25264263d13fSGeorge Wilson 	size_t width;
2527990b4856Slling 
2528990b4856Slling 	for (; pl != NULL; pl = pl->pl_next) {
25294263d13fSGeorge Wilson 
25304263d13fSGeorge Wilson 		width = pl->pl_width;
25314263d13fSGeorge Wilson 		if (first && cb->cb_verbose) {
25324263d13fSGeorge Wilson 			/*
25334263d13fSGeorge Wilson 			 * Reset the width to accommodate the verbose listing
25344263d13fSGeorge Wilson 			 * of devices.
25354263d13fSGeorge Wilson 			 */
25364263d13fSGeorge Wilson 			width = cb->cb_namewidth;
25374263d13fSGeorge Wilson 		}
25384263d13fSGeorge Wilson 
2539990b4856Slling 		if (!first) {
25404263d13fSGeorge Wilson 			if (cb->cb_scripted)
2541990b4856Slling 				(void) printf("\t");
2542990b4856Slling 			else
2543990b4856Slling 				(void) printf("  ");
2544990b4856Slling 		} else {
2545990b4856Slling 			first = B_FALSE;
2546990b4856Slling 		}
2547990b4856Slling 
2548990b4856Slling 		right_justify = B_FALSE;
2549990b4856Slling 		if (pl->pl_prop != ZPROP_INVAL) {
25504263d13fSGeorge Wilson 			if (pl->pl_prop == ZPOOL_PROP_EXPANDSZ &&
25514263d13fSGeorge Wilson 			    zpool_get_prop_int(zhp, pl->pl_prop, NULL) == 0)
25524263d13fSGeorge Wilson 				propstr = "-";
25534263d13fSGeorge Wilson 			else if (zpool_get_prop(zhp, pl->pl_prop, property,
2554990b4856Slling 			    sizeof (property), NULL) != 0)
2555990b4856Slling 				propstr = "-";
2556990b4856Slling 			else
2557990b4856Slling 				propstr = property;
2558990b4856Slling 
2559990b4856Slling 			right_justify = zpool_prop_align_right(pl->pl_prop);
2560990b4856Slling 		} else {
2561990b4856Slling 			propstr = "-";
2562990b4856Slling 		}
2563990b4856Slling 
2564990b4856Slling 
2565990b4856Slling 		/*
2566990b4856Slling 		 * If this is being called in scripted mode, or if this is the
2567990b4856Slling 		 * last column and it is left-justified, don't include a width
2568990b4856Slling 		 * format specifier.
2569990b4856Slling 		 */
25704263d13fSGeorge Wilson 		if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2571990b4856Slling 			(void) printf("%s", propstr);
2572990b4856Slling 		else if (right_justify)
2573990b4856Slling 			(void) printf("%*s", width, propstr);
2574990b4856Slling 		else
2575990b4856Slling 			(void) printf("%-*s", width, propstr);
2576990b4856Slling 	}
2577990b4856Slling 
2578990b4856Slling 	(void) printf("\n");
2579990b4856Slling }
2580990b4856Slling 
25814263d13fSGeorge Wilson static void
25824263d13fSGeorge Wilson print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted)
25834263d13fSGeorge Wilson {
25844263d13fSGeorge Wilson 	char propval[64];
25854263d13fSGeorge Wilson 	boolean_t fixed;
25864263d13fSGeorge Wilson 	size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
25874263d13fSGeorge Wilson 
25884263d13fSGeorge Wilson 	zfs_nicenum(value, propval, sizeof (propval));
25894263d13fSGeorge Wilson 
25904263d13fSGeorge Wilson 	if (prop == ZPOOL_PROP_EXPANDSZ && value == 0)
25914263d13fSGeorge Wilson 		(void) strlcpy(propval, "-", sizeof (propval));
25924263d13fSGeorge Wilson 
25934263d13fSGeorge Wilson 	if (scripted)
25944263d13fSGeorge Wilson 		(void) printf("\t%s", propval);
25954263d13fSGeorge Wilson 	else
25964263d13fSGeorge Wilson 		(void) printf("  %*s", width, propval);
25974263d13fSGeorge Wilson }
25984263d13fSGeorge Wilson 
25994263d13fSGeorge Wilson void
26004263d13fSGeorge Wilson print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
26014263d13fSGeorge Wilson     list_cbdata_t *cb, int depth)
26024263d13fSGeorge Wilson {
26034263d13fSGeorge Wilson 	nvlist_t **child;
26044263d13fSGeorge Wilson 	vdev_stat_t *vs;
26054263d13fSGeorge Wilson 	uint_t c, children;
26064263d13fSGeorge Wilson 	char *vname;
26074263d13fSGeorge Wilson 	boolean_t scripted = cb->cb_scripted;
26084263d13fSGeorge Wilson 
26094263d13fSGeorge Wilson 	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
26104263d13fSGeorge Wilson 	    (uint64_t **)&vs, &c) == 0);
26114263d13fSGeorge Wilson 
26124263d13fSGeorge Wilson 	if (name != NULL) {
26134263d13fSGeorge Wilson 		if (scripted)
26144263d13fSGeorge Wilson 			(void) printf("\t%s", name);
26154263d13fSGeorge Wilson 		else if (strlen(name) + depth > cb->cb_namewidth)
26164263d13fSGeorge Wilson 			(void) printf("%*s%s", depth, "", name);
26174263d13fSGeorge Wilson 		else
26184263d13fSGeorge Wilson 			(void) printf("%*s%s%*s", depth, "", name,
26194263d13fSGeorge Wilson 			    (int)(cb->cb_namewidth - strlen(name) - depth), "");
26204263d13fSGeorge Wilson 
26214263d13fSGeorge Wilson 		/* only toplevel vdevs have capacity stats */
26224263d13fSGeorge Wilson 		if (vs->vs_space == 0) {
26234263d13fSGeorge Wilson 			if (scripted)
26244263d13fSGeorge Wilson 				(void) printf("\t-\t-\t-");
26254263d13fSGeorge Wilson 			else
26264263d13fSGeorge Wilson 				(void) printf("      -      -      -");
26274263d13fSGeorge Wilson 		} else {
26284263d13fSGeorge Wilson 			print_one_column(ZPOOL_PROP_SIZE, vs->vs_space,
26294263d13fSGeorge Wilson 			    scripted);
26304263d13fSGeorge Wilson 			print_one_column(ZPOOL_PROP_CAPACITY, vs->vs_alloc,
26314263d13fSGeorge Wilson 			    scripted);
26324263d13fSGeorge Wilson 			print_one_column(ZPOOL_PROP_FREE,
26334263d13fSGeorge Wilson 			    vs->vs_space - vs->vs_alloc, scripted);
26344263d13fSGeorge Wilson 		}
26354263d13fSGeorge Wilson 		print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize,
26364263d13fSGeorge Wilson 		    scripted);
26374263d13fSGeorge Wilson 		(void) printf("\n");
26384263d13fSGeorge Wilson 	}
26394263d13fSGeorge Wilson 
26404263d13fSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
26414263d13fSGeorge Wilson 	    &child, &children) != 0)
26424263d13fSGeorge Wilson 		return;
26434263d13fSGeorge Wilson 
26444263d13fSGeorge Wilson 	for (c = 0; c < children; c++) {
26454263d13fSGeorge Wilson 		uint64_t ishole = B_FALSE;
26464263d13fSGeorge Wilson 
26474263d13fSGeorge Wilson 		if (nvlist_lookup_uint64(child[c],
26484263d13fSGeorge Wilson 		    ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
26494263d13fSGeorge Wilson 			continue;
26504263d13fSGeorge Wilson 
26514263d13fSGeorge Wilson 		vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
26524263d13fSGeorge Wilson 		print_list_stats(zhp, vname, child[c], cb, depth + 2);
26534263d13fSGeorge Wilson 		free(vname);
26544263d13fSGeorge Wilson 	}
26554263d13fSGeorge Wilson 
26564263d13fSGeorge Wilson 	/*
26574263d13fSGeorge Wilson 	 * Include level 2 ARC devices in iostat output
26584263d13fSGeorge Wilson 	 */
26594263d13fSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
26604263d13fSGeorge Wilson 	    &child, &children) != 0)
26614263d13fSGeorge Wilson 		return;
26624263d13fSGeorge Wilson 
26634263d13fSGeorge Wilson 	if (children > 0) {
26644263d13fSGeorge Wilson 		(void) printf("%-*s      -      -      -      -      -      "
26654263d13fSGeorge Wilson 		    "-\n", cb->cb_namewidth, "cache");
26664263d13fSGeorge Wilson 		for (c = 0; c < children; c++) {
26674263d13fSGeorge Wilson 			vname = zpool_vdev_name(g_zfs, zhp, child[c],
26684263d13fSGeorge Wilson 			    B_FALSE);
26694263d13fSGeorge Wilson 			print_list_stats(zhp, vname, child[c], cb, depth + 2);
26704263d13fSGeorge Wilson 			free(vname);
26714263d13fSGeorge Wilson 		}
26724263d13fSGeorge Wilson 	}
26734263d13fSGeorge Wilson }
26744263d13fSGeorge Wilson 
26754263d13fSGeorge Wilson 
2676990b4856Slling /*
2677990b4856Slling  * Generic callback function to list a pool.
2678990b4856Slling  */
2679fa9e4066Sahrens int
2680fa9e4066Sahrens list_callback(zpool_handle_t *zhp, void *data)
2681fa9e4066Sahrens {
2682fa9e4066Sahrens 	list_cbdata_t *cbp = data;
26834263d13fSGeorge Wilson 	nvlist_t *config;
26844263d13fSGeorge Wilson 	nvlist_t *nvroot;
2685fa9e4066Sahrens 
26864263d13fSGeorge Wilson 	config = zpool_get_config(zhp, NULL);
2687fa9e4066Sahrens 
26884263d13fSGeorge Wilson 	print_pool(zhp, cbp);
26894263d13fSGeorge Wilson 	if (!cbp->cb_verbose)
26904263d13fSGeorge Wilson 		return (0);
26914263d13fSGeorge Wilson 
26924263d13fSGeorge Wilson 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
26934263d13fSGeorge Wilson 	    &nvroot) == 0);
26944263d13fSGeorge Wilson 	print_list_stats(zhp, NULL, nvroot, cbp, 0);
2695fa9e4066Sahrens 
2696fa9e4066Sahrens 	return (0);
2697fa9e4066Sahrens }
2698fa9e4066Sahrens 
2699fa9e4066Sahrens /*
27003f9d6ad7SLin Ling  * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2701fa9e4066Sahrens  *
2702990b4856Slling  *	-H	Scripted mode.  Don't display headers, and separate properties
2703990b4856Slling  *		by a single tab.
2704990b4856Slling  *	-o	List of properties to display.  Defaults to
2705485bbbf5SGeorge Wilson  *		"name,size,allocated,free,capacity,health,altroot"
27063f9d6ad7SLin Ling  *	-T	Display a timestamp in date(1) or Unix format
2707fa9e4066Sahrens  *
2708fa9e4066Sahrens  * List all pools in the system, whether or not they're healthy.  Output space
2709fa9e4066Sahrens  * statistics for each one, as well as health status summary.
2710fa9e4066Sahrens  */
2711fa9e4066Sahrens int
2712fa9e4066Sahrens zpool_do_list(int argc, char **argv)
2713fa9e4066Sahrens {
2714fa9e4066Sahrens 	int c;
2715fa9e4066Sahrens 	int ret;
2716fa9e4066Sahrens 	list_cbdata_t cb = { 0 };
2717990b4856Slling 	static char default_props[] =
27184263d13fSGeorge Wilson 	    "name,size,allocated,free,expandsize,capacity,dedupratio,"
27194263d13fSGeorge Wilson 	    "health,altroot";
2720990b4856Slling 	char *props = default_props;
27213f9d6ad7SLin Ling 	unsigned long interval = 0, count = 0;
27224263d13fSGeorge Wilson 	zpool_list_t *list;
27234263d13fSGeorge Wilson 	boolean_t first = B_TRUE;
2724fa9e4066Sahrens 
2725fa9e4066Sahrens 	/* check options */
27264263d13fSGeorge Wilson 	while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) {
2727fa9e4066Sahrens 		switch (c) {
2728fa9e4066Sahrens 		case 'H':
272999653d4eSeschrock 			cb.cb_scripted = B_TRUE;
2730fa9e4066Sahrens 			break;
2731fa9e4066Sahrens 		case 'o':
2732990b4856Slling 			props = optarg;
2733fa9e4066Sahrens 			break;
27343f9d6ad7SLin Ling 		case 'T':
27353f9d6ad7SLin Ling 			get_timestamp_arg(*optarg);
27363f9d6ad7SLin Ling 			break;
27374263d13fSGeorge Wilson 		case 'v':
27384263d13fSGeorge Wilson 			cb.cb_verbose = B_TRUE;
27394263d13fSGeorge Wilson 			break;
2740fa9e4066Sahrens 		case ':':
2741fa9e4066Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
2742fa9e4066Sahrens 			    "'%c' option\n"), optopt);
274399653d4eSeschrock 			usage(B_FALSE);
2744fa9e4066Sahrens 			break;
2745fa9e4066Sahrens 		case '?':
2746fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2747fa9e4066Sahrens 			    optopt);
274899653d4eSeschrock 			usage(B_FALSE);
2749fa9e4066Sahrens 		}
2750fa9e4066Sahrens 	}
2751fa9e4066Sahrens 
2752fa9e4066Sahrens 	argc -= optind;
2753fa9e4066Sahrens 	argv += optind;
2754fa9e4066Sahrens 
27553f9d6ad7SLin Ling 	get_interval_count(&argc, argv, &interval, &count);
27563f9d6ad7SLin Ling 
2757990b4856Slling 	if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
275899653d4eSeschrock 		usage(B_FALSE);
2759fa9e4066Sahrens 
27604263d13fSGeorge Wilson 	if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL)
27614263d13fSGeorge Wilson 		return (1);
2762fa9e4066Sahrens 
27634263d13fSGeorge Wilson 	if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
2764fa9e4066Sahrens 		(void) printf(gettext("no pools available\n"));
27653f9d6ad7SLin Ling 		zprop_free_list(cb.cb_proplist);
2766fa9e4066Sahrens 		return (0);
2767fa9e4066Sahrens 	}
2768fa9e4066Sahrens 
27694263d13fSGeorge Wilson 	for (;;) {
27704263d13fSGeorge Wilson 		pool_list_update(list);
27714263d13fSGeorge Wilson 
27724263d13fSGeorge Wilson 		if (pool_list_count(list) == 0)
27734263d13fSGeorge Wilson 			break;
27744263d13fSGeorge Wilson 
27754263d13fSGeorge Wilson 		cb.cb_namewidth = 0;
27764263d13fSGeorge Wilson 		(void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
27774263d13fSGeorge Wilson 
27784263d13fSGeorge Wilson 		if (timestamp_fmt != NODATE)
27794263d13fSGeorge Wilson 			print_timestamp(timestamp_fmt);
27804263d13fSGeorge Wilson 
27814263d13fSGeorge Wilson 		if (!cb.cb_scripted && (first || cb.cb_verbose)) {
27824263d13fSGeorge Wilson 			print_header(&cb);
27834263d13fSGeorge Wilson 			first = B_FALSE;
27844263d13fSGeorge Wilson 		}
27854263d13fSGeorge Wilson 		ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
27864263d13fSGeorge Wilson 
27873f9d6ad7SLin Ling 		if (interval == 0)
27883f9d6ad7SLin Ling 			break;
27893f9d6ad7SLin Ling 
27903f9d6ad7SLin Ling 		if (count != 0 && --count == 0)
27913f9d6ad7SLin Ling 			break;
27923f9d6ad7SLin Ling 
27933f9d6ad7SLin Ling 		(void) sleep(interval);
27943f9d6ad7SLin Ling 	}
27953f9d6ad7SLin Ling 
27963f9d6ad7SLin Ling 	zprop_free_list(cb.cb_proplist);
2797fa9e4066Sahrens 	return (ret);
2798fa9e4066Sahrens }
2799fa9e4066Sahrens 
2800fa9e4066Sahrens static nvlist_t *
2801fa9e4066Sahrens zpool_get_vdev_by_name(nvlist_t *nv, char *name)
2802fa9e4066Sahrens {
2803fa9e4066Sahrens 	nvlist_t **child;
2804fa9e4066Sahrens 	uint_t c, children;
2805fa9e4066Sahrens 	nvlist_t *match;
2806fa9e4066Sahrens 	char *path;
2807fa9e4066Sahrens 
2808fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2809fa9e4066Sahrens 	    &child, &children) != 0) {
2810fa9e4066Sahrens 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2811fa9e4066Sahrens 		if (strncmp(name, "/dev/dsk/", 9) == 0)
2812fa9e4066Sahrens 			name += 9;
2813fa9e4066Sahrens 		if (strncmp(path, "/dev/dsk/", 9) == 0)
2814fa9e4066Sahrens 			path += 9;
2815fa9e4066Sahrens 		if (strcmp(name, path) == 0)
2816fa9e4066Sahrens 			return (nv);
2817fa9e4066Sahrens 		return (NULL);
2818fa9e4066Sahrens 	}
2819fa9e4066Sahrens 
2820fa9e4066Sahrens 	for (c = 0; c < children; c++)
2821fa9e4066Sahrens 		if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
2822fa9e4066Sahrens 			return (match);
2823fa9e4066Sahrens 
2824fa9e4066Sahrens 	return (NULL);
2825fa9e4066Sahrens }
2826fa9e4066Sahrens 
2827fa9e4066Sahrens static int
2828fa9e4066Sahrens zpool_do_attach_or_replace(int argc, char **argv, int replacing)
2829fa9e4066Sahrens {
283099653d4eSeschrock 	boolean_t force = B_FALSE;
2831fa9e4066Sahrens 	int c;
2832fa9e4066Sahrens 	nvlist_t *nvroot;
2833fa9e4066Sahrens 	char *poolname, *old_disk, *new_disk;
2834fa9e4066Sahrens 	zpool_handle_t *zhp;
283599653d4eSeschrock 	int ret;
2836fa9e4066Sahrens 
2837fa9e4066Sahrens 	/* check options */
2838fa9e4066Sahrens 	while ((c = getopt(argc, argv, "f")) != -1) {
2839fa9e4066Sahrens 		switch (c) {
2840fa9e4066Sahrens 		case 'f':
284199653d4eSeschrock 			force = B_TRUE;
2842fa9e4066Sahrens 			break;
2843fa9e4066Sahrens 		case '?':
2844fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2845fa9e4066Sahrens 			    optopt);
284699653d4eSeschrock 			usage(B_FALSE);
2847fa9e4066Sahrens 		}
2848fa9e4066Sahrens 	}
2849fa9e4066Sahrens 
2850fa9e4066Sahrens 	argc -= optind;
2851fa9e4066Sahrens 	argv += optind;
2852fa9e4066Sahrens 
2853fa9e4066Sahrens 	/* get pool name and check number of arguments */
2854fa9e4066Sahrens 	if (argc < 1) {
2855fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
285699653d4eSeschrock 		usage(B_FALSE);
2857fa9e4066Sahrens 	}
2858fa9e4066Sahrens 
2859fa9e4066Sahrens 	poolname = argv[0];
2860fa9e4066Sahrens 
2861fa9e4066Sahrens 	if (argc < 2) {
2862fa9e4066Sahrens 		(void) fprintf(stderr,
2863fa9e4066Sahrens 		    gettext("missing <device> specification\n"));
286499653d4eSeschrock 		usage(B_FALSE);
2865fa9e4066Sahrens 	}
2866fa9e4066Sahrens 
2867fa9e4066Sahrens 	old_disk = argv[1];
2868fa9e4066Sahrens 
2869fa9e4066Sahrens 	if (argc < 3) {
2870fa9e4066Sahrens 		if (!replacing) {
2871fa9e4066Sahrens 			(void) fprintf(stderr,
2872fa9e4066Sahrens 			    gettext("missing <new_device> specification\n"));
287399653d4eSeschrock 			usage(B_FALSE);
2874fa9e4066Sahrens 		}
2875fa9e4066Sahrens 		new_disk = old_disk;
2876fa9e4066Sahrens 		argc -= 1;
2877fa9e4066Sahrens 		argv += 1;
2878fa9e4066Sahrens 	} else {
2879fa9e4066Sahrens 		new_disk = argv[2];
2880fa9e4066Sahrens 		argc -= 2;
2881fa9e4066Sahrens 		argv += 2;
2882fa9e4066Sahrens 	}
2883fa9e4066Sahrens 
2884fa9e4066Sahrens 	if (argc > 1) {
2885fa9e4066Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
288699653d4eSeschrock 		usage(B_FALSE);
2887fa9e4066Sahrens 	}
2888fa9e4066Sahrens 
288999653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2890fa9e4066Sahrens 		return (1);
2891fa9e4066Sahrens 
28928488aeb5Staylor 	if (zpool_get_config(zhp, NULL) == NULL) {
2893fa9e4066Sahrens 		(void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
2894fa9e4066Sahrens 		    poolname);
2895fa9e4066Sahrens 		zpool_close(zhp);
2896fa9e4066Sahrens 		return (1);
2897fa9e4066Sahrens 	}
2898fa9e4066Sahrens 
2899705040edSEric Taylor 	nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
2900705040edSEric Taylor 	    argc, argv);
2901fa9e4066Sahrens 	if (nvroot == NULL) {
2902fa9e4066Sahrens 		zpool_close(zhp);
2903fa9e4066Sahrens 		return (1);
2904fa9e4066Sahrens 	}
2905fa9e4066Sahrens 
290699653d4eSeschrock 	ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
290799653d4eSeschrock 
290899653d4eSeschrock 	nvlist_free(nvroot);
290999653d4eSeschrock 	zpool_close(zhp);
291099653d4eSeschrock 
291199653d4eSeschrock 	return (ret);
2912fa9e4066Sahrens }
2913fa9e4066Sahrens 
2914fa9e4066Sahrens /*
2915fa9e4066Sahrens  * zpool replace [-f] <pool> <device> <new_device>
2916fa9e4066Sahrens  *
2917fa9e4066Sahrens  *	-f	Force attach, even if <new_device> appears to be in use.
2918fa9e4066Sahrens  *
2919fa9e4066Sahrens  * Replace <device> with <new_device>.
2920fa9e4066Sahrens  */
2921fa9e4066Sahrens /* ARGSUSED */
2922fa9e4066Sahrens int
2923fa9e4066Sahrens zpool_do_replace(int argc, char **argv)
2924fa9e4066Sahrens {
2925fa9e4066Sahrens 	return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
2926fa9e4066Sahrens }
2927fa9e4066Sahrens 
2928fa9e4066Sahrens /*
2929fa9e4066Sahrens  * zpool attach [-f] <pool> <device> <new_device>
2930fa9e4066Sahrens  *
2931fa9e4066Sahrens  *	-f	Force attach, even if <new_device> appears to be in use.
2932fa9e4066Sahrens  *
2933fa9e4066Sahrens  * Attach <new_device> to the mirror containing <device>.  If <device> is not
2934fa9e4066Sahrens  * part of a mirror, then <device> will be transformed into a mirror of
2935fa9e4066Sahrens  * <device> and <new_device>.  In either case, <new_device> will begin life
2936fa9e4066Sahrens  * with a DTL of [0, now], and will immediately begin to resilver itself.
2937fa9e4066Sahrens  */
2938fa9e4066Sahrens int
2939fa9e4066Sahrens zpool_do_attach(int argc, char **argv)
2940fa9e4066Sahrens {
2941fa9e4066Sahrens 	return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
2942fa9e4066Sahrens }
2943fa9e4066Sahrens 
2944fa9e4066Sahrens /*
2945fa9e4066Sahrens  * zpool detach [-f] <pool> <device>
2946fa9e4066Sahrens  *
2947fa9e4066Sahrens  *	-f	Force detach of <device>, even if DTLs argue against it
2948fa9e4066Sahrens  *		(not supported yet)
2949fa9e4066Sahrens  *
2950fa9e4066Sahrens  * Detach a device from a mirror.  The operation will be refused if <device>
2951fa9e4066Sahrens  * is the last device in the mirror, or if the DTLs indicate that this device
2952fa9e4066Sahrens  * has the only valid copy of some data.
2953fa9e4066Sahrens  */
2954fa9e4066Sahrens /* ARGSUSED */
2955fa9e4066Sahrens int
2956fa9e4066Sahrens zpool_do_detach(int argc, char **argv)
2957fa9e4066Sahrens {
2958fa9e4066Sahrens 	int c;
2959fa9e4066Sahrens 	char *poolname, *path;
2960fa9e4066Sahrens 	zpool_handle_t *zhp;
296199653d4eSeschrock 	int ret;
2962fa9e4066Sahrens 
2963fa9e4066Sahrens 	/* check options */
2964fa9e4066Sahrens 	while ((c = getopt(argc, argv, "f")) != -1) {
2965fa9e4066Sahrens 		switch (c) {
2966fa9e4066Sahrens 		case 'f':
2967fa9e4066Sahrens 		case '?':
2968fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2969fa9e4066Sahrens 			    optopt);
297099653d4eSeschrock 			usage(B_FALSE);
2971fa9e4066Sahrens 		}
2972fa9e4066Sahrens 	}
2973fa9e4066Sahrens 
2974fa9e4066Sahrens 	argc -= optind;
2975fa9e4066Sahrens 	argv += optind;
2976fa9e4066Sahrens 
2977fa9e4066Sahrens 	/* get pool name and check number of arguments */
2978fa9e4066Sahrens 	if (argc < 1) {
2979fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
298099653d4eSeschrock 		usage(B_FALSE);
2981fa9e4066Sahrens 	}
2982fa9e4066Sahrens 
2983fa9e4066Sahrens 	if (argc < 2) {
2984fa9e4066Sahrens 		(void) fprintf(stderr,
2985fa9e4066Sahrens 		    gettext("missing <device> specification\n"));
298699653d4eSeschrock 		usage(B_FALSE);
2987fa9e4066Sahrens 	}
2988fa9e4066Sahrens 
2989fa9e4066Sahrens 	poolname = argv[0];
2990fa9e4066Sahrens 	path = argv[1];
2991fa9e4066Sahrens 
299299653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2993fa9e4066Sahrens 		return (1);
2994fa9e4066Sahrens 
299599653d4eSeschrock 	ret = zpool_vdev_detach(zhp, path);
299699653d4eSeschrock 
299799653d4eSeschrock 	zpool_close(zhp);
299899653d4eSeschrock 
299999653d4eSeschrock 	return (ret);
3000fa9e4066Sahrens }
3001fa9e4066Sahrens 
3002fa9e4066Sahrens /*
30031195e687SMark J Musante  * zpool split [-n] [-o prop=val] ...
30041195e687SMark J Musante  *		[-o mntopt] ...
30051195e687SMark J Musante  *		[-R altroot] <pool> <newpool> [<device> ...]
30061195e687SMark J Musante  *
30071195e687SMark J Musante  *	-n	Do not split the pool, but display the resulting layout if
30081195e687SMark J Musante  *		it were to be split.
30091195e687SMark J Musante  *	-o	Set property=value, or set mount options.
30101195e687SMark J Musante  *	-R	Mount the split-off pool under an alternate root.
30111195e687SMark J Musante  *
30121195e687SMark J Musante  * Splits the named pool and gives it the new pool name.  Devices to be split
30131195e687SMark J Musante  * off may be listed, provided that no more than one device is specified
30141195e687SMark J Musante  * per top-level vdev mirror.  The newly split pool is left in an exported
30151195e687SMark J Musante  * state unless -R is specified.
30161195e687SMark J Musante  *
30171195e687SMark J Musante  * Restrictions: the top-level of the pool pool must only be made up of
30181195e687SMark J Musante  * mirrors; all devices in the pool must be healthy; no device may be
30191195e687SMark J Musante  * undergoing a resilvering operation.
30201195e687SMark J Musante  */
30211195e687SMark J Musante int
30221195e687SMark J Musante zpool_do_split(int argc, char **argv)
30231195e687SMark J Musante {
30241195e687SMark J Musante 	char *srcpool, *newpool, *propval;
30251195e687SMark J Musante 	char *mntopts = NULL;
30261195e687SMark J Musante 	splitflags_t flags;
30271195e687SMark J Musante 	int c, ret = 0;
30281195e687SMark J Musante 	zpool_handle_t *zhp;
30291195e687SMark J Musante 	nvlist_t *config, *props = NULL;
30301195e687SMark J Musante 
30311195e687SMark J Musante 	flags.dryrun = B_FALSE;
30321195e687SMark J Musante 	flags.import = B_FALSE;
30331195e687SMark J Musante 
30341195e687SMark J Musante 	/* check options */
30351195e687SMark J Musante 	while ((c = getopt(argc, argv, ":R:no:")) != -1) {
30361195e687SMark J Musante 		switch (c) {
30371195e687SMark J Musante 		case 'R':
30381195e687SMark J Musante 			flags.import = B_TRUE;
30391195e687SMark J Musante 			if (add_prop_list(
30401195e687SMark J Musante 			    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
30411195e687SMark J Musante 			    &props, B_TRUE) != 0) {
30421195e687SMark J Musante 				if (props)
30431195e687SMark J Musante 					nvlist_free(props);
30441195e687SMark J Musante 				usage(B_FALSE);
30451195e687SMark J Musante 			}
30461195e687SMark J Musante 			break;
30471195e687SMark J Musante 		case 'n':
30481195e687SMark J Musante 			flags.dryrun = B_TRUE;
30491195e687SMark J Musante 			break;
30501195e687SMark J Musante 		case 'o':
30511195e687SMark J Musante 			if ((propval = strchr(optarg, '=')) != NULL) {
30521195e687SMark J Musante 				*propval = '\0';
30531195e687SMark J Musante 				propval++;
30541195e687SMark J Musante 				if (add_prop_list(optarg, propval,
30551195e687SMark J Musante 				    &props, B_TRUE) != 0) {
30561195e687SMark J Musante 					if (props)
30571195e687SMark J Musante 						nvlist_free(props);
30581195e687SMark J Musante 					usage(B_FALSE);
30591195e687SMark J Musante 				}
30601195e687SMark J Musante 			} else {
30611195e687SMark J Musante 				mntopts = optarg;
30621195e687SMark J Musante 			}
30631195e687SMark J Musante 			break;
30641195e687SMark J Musante 		case ':':
30651195e687SMark J Musante 			(void) fprintf(stderr, gettext("missing argument for "
30661195e687SMark J Musante 			    "'%c' option\n"), optopt);
30671195e687SMark J Musante 			usage(B_FALSE);
30681195e687SMark J Musante 			break;
30691195e687SMark J Musante 		case '?':
30701195e687SMark J Musante 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
30711195e687SMark J Musante 			    optopt);
30721195e687SMark J Musante 			usage(B_FALSE);
30731195e687SMark J Musante 			break;
30741195e687SMark J Musante 		}
30751195e687SMark J Musante 	}
30761195e687SMark J Musante 
30771195e687SMark J Musante 	if (!flags.import && mntopts != NULL) {
30781195e687SMark J Musante 		(void) fprintf(stderr, gettext("setting mntopts is only "
30791195e687SMark J Musante 		    "valid when importing the pool\n"));
30801195e687SMark J Musante 		usage(B_FALSE);
30811195e687SMark J Musante 	}
30821195e687SMark J Musante 
30831195e687SMark J Musante 	argc -= optind;
30841195e687SMark J Musante 	argv += optind;
30851195e687SMark J Musante 
30861195e687SMark J Musante 	if (argc < 1) {
30871195e687SMark J Musante 		(void) fprintf(stderr, gettext("Missing pool name\n"));
30881195e687SMark J Musante 		usage(B_FALSE);
30891195e687SMark J Musante 	}
30901195e687SMark J Musante 	if (argc < 2) {
30911195e687SMark J Musante 		(void) fprintf(stderr, gettext("Missing new pool name\n"));
30921195e687SMark J Musante 		usage(B_FALSE);
30931195e687SMark J Musante 	}
30941195e687SMark J Musante 
30951195e687SMark J Musante 	srcpool = argv[0];
30961195e687SMark J Musante 	newpool = argv[1];
30971195e687SMark J Musante 
30981195e687SMark J Musante 	argc -= 2;
30991195e687SMark J Musante 	argv += 2;
31001195e687SMark J Musante 
31011195e687SMark J Musante 	if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
31021195e687SMark J Musante 		return (1);
31031195e687SMark J Musante 
31041195e687SMark J Musante 	config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
31051195e687SMark J Musante 	if (config == NULL) {
31061195e687SMark J Musante 		ret = 1;
31071195e687SMark J Musante 	} else {
31081195e687SMark J Musante 		if (flags.dryrun) {
31091195e687SMark J Musante 			(void) printf(gettext("would create '%s' with the "
31101195e687SMark J Musante 			    "following layout:\n\n"), newpool);
31111195e687SMark J Musante 			print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
31121195e687SMark J Musante 		}
31131195e687SMark J Musante 		nvlist_free(config);
31141195e687SMark J Musante 	}
31151195e687SMark J Musante 
31161195e687SMark J Musante 	zpool_close(zhp);
31171195e687SMark J Musante 
31181195e687SMark J Musante 	if (ret != 0 || flags.dryrun || !flags.import)
31191195e687SMark J Musante 		return (ret);
31201195e687SMark J Musante 
31211195e687SMark J Musante 	/*
31221195e687SMark J Musante 	 * The split was successful. Now we need to open the new
31231195e687SMark J Musante 	 * pool and import it.
31241195e687SMark J Musante 	 */
31251195e687SMark J Musante 	if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
31261195e687SMark J Musante 		return (1);
31271195e687SMark J Musante 	if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
31281195e687SMark J Musante 	    zpool_enable_datasets(zhp, mntopts, 0) != 0) {
31291195e687SMark J Musante 		ret = 1;
31301195e687SMark J Musante 		(void) fprintf(stderr, gettext("Split was succssful, but "
31311195e687SMark J Musante 		    "the datasets could not all be mounted\n"));
31321195e687SMark J Musante 		(void) fprintf(stderr, gettext("Try doing '%s' with a "
31331195e687SMark J Musante 		    "different altroot\n"), "zpool import");
31341195e687SMark J Musante 	}
31351195e687SMark J Musante 	zpool_close(zhp);
31361195e687SMark J Musante 
31371195e687SMark J Musante 	return (ret);
31381195e687SMark J Musante }
31391195e687SMark J Musante 
31401195e687SMark J Musante 
31411195e687SMark J Musante 
31421195e687SMark J Musante /*
3143441d80aaSlling  * zpool online <pool> <device> ...
3144fa9e4066Sahrens  */
3145fa9e4066Sahrens int
3146fa9e4066Sahrens zpool_do_online(int argc, char **argv)
3147fa9e4066Sahrens {
3148fa9e4066Sahrens 	int c, i;
3149fa9e4066Sahrens 	char *poolname;
3150fa9e4066Sahrens 	zpool_handle_t *zhp;
3151fa9e4066Sahrens 	int ret = 0;
31523d7072f8Seschrock 	vdev_state_t newstate;
3153573ca77eSGeorge Wilson 	int flags = 0;
3154fa9e4066Sahrens 
3155fa9e4066Sahrens 	/* check options */
3156573ca77eSGeorge Wilson 	while ((c = getopt(argc, argv, "et")) != -1) {
3157fa9e4066Sahrens 		switch (c) {
3158573ca77eSGeorge Wilson 		case 'e':
3159573ca77eSGeorge Wilson 			flags |= ZFS_ONLINE_EXPAND;
3160573ca77eSGeorge Wilson 			break;
3161fa9e4066Sahrens 		case 't':
3162fa9e4066Sahrens 		case '?':
3163fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3164fa9e4066Sahrens 			    optopt);
316599653d4eSeschrock 			usage(B_FALSE);
3166fa9e4066Sahrens 		}
3167fa9e4066Sahrens 	}
3168fa9e4066Sahrens 
3169fa9e4066Sahrens 	argc -= optind;
3170fa9e4066Sahrens 	argv += optind;
3171fa9e4066Sahrens 
3172fa9e4066Sahrens 	/* get pool name and check number of arguments */
3173fa9e4066Sahrens 	if (argc < 1) {
3174fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name\n"));
317599653d4eSeschrock 		usage(B_FALSE);
3176fa9e4066Sahrens 	}
3177fa9e4066Sahrens 	if (argc < 2) {
3178fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing device name\n"));
317999653d4eSeschrock 		usage(B_FALSE);
3180fa9e4066Sahrens 	}
3181fa9e4066Sahrens 
3182fa9e4066Sahrens 	poolname = argv[0];
3183fa9e4066Sahrens 
318499653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3185fa9e4066Sahrens 		return (1);
3186fa9e4066Sahrens 
31873d7072f8Seschrock 	for (i = 1; i < argc; i++) {
3188573ca77eSGeorge Wilson 		if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
31893d7072f8Seschrock 			if (newstate != VDEV_STATE_HEALTHY) {
31903d7072f8Seschrock 				(void) printf(gettext("warning: device '%s' "
31913d7072f8Seschrock 				    "onlined, but remains in faulted state\n"),
3192fa9e4066Sahrens 				    argv[i]);
31933d7072f8Seschrock 				if (newstate == VDEV_STATE_FAULTED)
31943d7072f8Seschrock 					(void) printf(gettext("use 'zpool "
31953d7072f8Seschrock 					    "clear' to restore a faulted "
31963d7072f8Seschrock 					    "device\n"));
3197fa9e4066Sahrens 				else
31983d7072f8Seschrock 					(void) printf(gettext("use 'zpool "
31993d7072f8Seschrock 					    "replace' to replace devices "
32003d7072f8Seschrock 					    "that are no longer present\n"));
32013d7072f8Seschrock 			}
32023d7072f8Seschrock 		} else {
3203fa9e4066Sahrens 			ret = 1;
32043d7072f8Seschrock 		}
32053d7072f8Seschrock 	}
3206fa9e4066Sahrens 
320799653d4eSeschrock 	zpool_close(zhp);
320899653d4eSeschrock 
3209fa9e4066Sahrens 	return (ret);
3210fa9e4066Sahrens }
3211fa9e4066Sahrens 
3212fa9e4066Sahrens /*
3213441d80aaSlling  * zpool offline [-ft] <pool> <device> ...
3214fa9e4066Sahrens  *
3215fa9e4066Sahrens  *	-f	Force the device into the offline state, even if doing
3216fa9e4066Sahrens  *		so would appear to compromise pool availability.
3217fa9e4066Sahrens  *		(not supported yet)
3218fa9e4066Sahrens  *
3219fa9e4066Sahrens  *	-t	Only take the device off-line temporarily.  The offline
3220fa9e4066Sahrens  *		state will not be persistent across reboots.
3221fa9e4066Sahrens  */
3222fa9e4066Sahrens /* ARGSUSED */
3223fa9e4066Sahrens int
3224fa9e4066Sahrens zpool_do_offline(int argc, char **argv)
3225fa9e4066Sahrens {
3226fa9e4066Sahrens 	int c, i;
3227fa9e4066Sahrens 	char *poolname;
3228fa9e4066Sahrens 	zpool_handle_t *zhp;
322999653d4eSeschrock 	int ret = 0;
323099653d4eSeschrock 	boolean_t istmp = B_FALSE;
3231fa9e4066Sahrens 
3232fa9e4066Sahrens 	/* check options */
3233fa9e4066Sahrens 	while ((c = getopt(argc, argv, "ft")) != -1) {
3234fa9e4066Sahrens 		switch (c) {
3235fa9e4066Sahrens 		case 't':
323699653d4eSeschrock 			istmp = B_TRUE;
3237441d80aaSlling 			break;
3238441d80aaSlling 		case 'f':
3239fa9e4066Sahrens 		case '?':
3240fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3241fa9e4066Sahrens 			    optopt);
324299653d4eSeschrock 			usage(B_FALSE);
3243fa9e4066Sahrens 		}
3244fa9e4066Sahrens 	}
3245fa9e4066Sahrens 
3246fa9e4066Sahrens 	argc -= optind;
3247fa9e4066Sahrens 	argv += optind;
3248fa9e4066Sahrens 
3249fa9e4066Sahrens 	/* get pool name and check number of arguments */
3250fa9e4066Sahrens 	if (argc < 1) {
3251fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name\n"));
325299653d4eSeschrock 		usage(B_FALSE);
3253fa9e4066Sahrens 	}
3254fa9e4066Sahrens 	if (argc < 2) {
3255fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing device name\n"));
325699653d4eSeschrock 		usage(B_FALSE);
3257fa9e4066Sahrens 	}
3258fa9e4066Sahrens 
3259fa9e4066Sahrens 	poolname = argv[0];
3260fa9e4066Sahrens 
326199653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3262fa9e4066Sahrens 		return (1);
3263fa9e4066Sahrens 
32643d7072f8Seschrock 	for (i = 1; i < argc; i++) {
32653d7072f8Seschrock 		if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3266fa9e4066Sahrens 			ret = 1;
32673d7072f8Seschrock 	}
3268fa9e4066Sahrens 
326999653d4eSeschrock 	zpool_close(zhp);
327099653d4eSeschrock 
3271fa9e4066Sahrens 	return (ret);
3272fa9e4066Sahrens }
3273fa9e4066Sahrens 
3274ea8dc4b6Seschrock /*
3275ea8dc4b6Seschrock  * zpool clear <pool> [device]
3276ea8dc4b6Seschrock  *
3277ea8dc4b6Seschrock  * Clear all errors associated with a pool or a particular device.
3278ea8dc4b6Seschrock  */
3279ea8dc4b6Seschrock int
3280ea8dc4b6Seschrock zpool_do_clear(int argc, char **argv)
3281ea8dc4b6Seschrock {
3282468c413aSTim Haley 	int c;
3283ea8dc4b6Seschrock 	int ret = 0;
3284468c413aSTim Haley 	boolean_t dryrun = B_FALSE;
3285468c413aSTim Haley 	boolean_t do_rewind = B_FALSE;
3286468c413aSTim Haley 	boolean_t xtreme_rewind = B_FALSE;
3287468c413aSTim Haley 	uint32_t rewind_policy = ZPOOL_NO_REWIND;
3288468c413aSTim Haley 	nvlist_t *policy = NULL;
3289ea8dc4b6Seschrock 	zpool_handle_t *zhp;
3290ea8dc4b6Seschrock 	char *pool, *device;
3291ea8dc4b6Seschrock 
3292468c413aSTim Haley 	/* check options */
3293468c413aSTim Haley 	while ((c = getopt(argc, argv, "FnX")) != -1) {
3294468c413aSTim Haley 		switch (c) {
3295468c413aSTim Haley 		case 'F':
3296468c413aSTim Haley 			do_rewind = B_TRUE;
3297468c413aSTim Haley 			break;
3298468c413aSTim Haley 		case 'n':
3299468c413aSTim Haley 			dryrun = B_TRUE;
3300468c413aSTim Haley 			break;
3301468c413aSTim Haley 		case 'X':
3302468c413aSTim Haley 			xtreme_rewind = B_TRUE;
3303468c413aSTim Haley 			break;
3304468c413aSTim Haley 		case '?':
3305468c413aSTim Haley 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3306468c413aSTim Haley 			    optopt);
3307468c413aSTim Haley 			usage(B_FALSE);
3308468c413aSTim Haley 		}
3309468c413aSTim Haley 	}
3310468c413aSTim Haley 
3311468c413aSTim Haley 	argc -= optind;
3312468c413aSTim Haley 	argv += optind;
3313468c413aSTim Haley 
3314468c413aSTim Haley 	if (argc < 1) {
3315ea8dc4b6Seschrock 		(void) fprintf(stderr, gettext("missing pool name\n"));
331699653d4eSeschrock 		usage(B_FALSE);
3317ea8dc4b6Seschrock 	}
3318ea8dc4b6Seschrock 
3319468c413aSTim Haley 	if (argc > 2) {
3320ea8dc4b6Seschrock 		(void) fprintf(stderr, gettext("too many arguments\n"));
332199653d4eSeschrock 		usage(B_FALSE);
3322ea8dc4b6Seschrock 	}
3323ea8dc4b6Seschrock 
3324468c413aSTim Haley 	if ((dryrun || xtreme_rewind) && !do_rewind) {
3325468c413aSTim Haley 		(void) fprintf(stderr,
3326468c413aSTim Haley 		    gettext("-n or -X only meaningful with -F\n"));
3327468c413aSTim Haley 		usage(B_FALSE);
3328468c413aSTim Haley 	}
3329468c413aSTim Haley 	if (dryrun)
3330468c413aSTim Haley 		rewind_policy = ZPOOL_TRY_REWIND;
3331468c413aSTim Haley 	else if (do_rewind)
3332468c413aSTim Haley 		rewind_policy = ZPOOL_DO_REWIND;
3333468c413aSTim Haley 	if (xtreme_rewind)
3334468c413aSTim Haley 		rewind_policy |= ZPOOL_EXTREME_REWIND;
3335ea8dc4b6Seschrock 
3336468c413aSTim Haley 	/* In future, further rewind policy choices can be passed along here */
3337468c413aSTim Haley 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3338468c413aSTim Haley 	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3339ea8dc4b6Seschrock 		return (1);
3340ea8dc4b6Seschrock 
3341468c413aSTim Haley 	pool = argv[0];
3342468c413aSTim Haley 	device = argc == 2 ? argv[1] : NULL;
3343468c413aSTim Haley 
3344468c413aSTim Haley 	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3345468c413aSTim Haley 		nvlist_free(policy);
3346468c413aSTim Haley 		return (1);
3347468c413aSTim Haley 	}
3348468c413aSTim Haley 
3349468c413aSTim Haley 	if (zpool_clear(zhp, device, policy) != 0)
3350ea8dc4b6Seschrock 		ret = 1;
3351ea8dc4b6Seschrock 
3352ea8dc4b6Seschrock 	zpool_close(zhp);
3353ea8dc4b6Seschrock 
3354468c413aSTim Haley 	nvlist_free(policy);
3355468c413aSTim Haley 
3356ea8dc4b6Seschrock 	return (ret);
3357ea8dc4b6Seschrock }
3358ea8dc4b6Seschrock 
3359e9103aaeSGarrett D'Amore /*
3360e9103aaeSGarrett D'Amore  * zpool reguid <pool>
3361e9103aaeSGarrett D'Amore  */
3362e9103aaeSGarrett D'Amore int
3363e9103aaeSGarrett D'Amore zpool_do_reguid(int argc, char **argv)
3364e9103aaeSGarrett D'Amore {
3365e9103aaeSGarrett D'Amore 	int c;
3366e9103aaeSGarrett D'Amore 	char *poolname;
3367e9103aaeSGarrett D'Amore 	zpool_handle_t *zhp;
3368e9103aaeSGarrett D'Amore 	int ret = 0;
3369e9103aaeSGarrett D'Amore 
3370e9103aaeSGarrett D'Amore 	/* check options */
3371e9103aaeSGarrett D'Amore 	while ((c = getopt(argc, argv, "")) != -1) {
3372e9103aaeSGarrett D'Amore 		switch (c) {
3373e9103aaeSGarrett D'Amore 		case '?':
3374e9103aaeSGarrett D'Amore 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3375e9103aaeSGarrett D'Amore 			    optopt);
3376e9103aaeSGarrett D'Amore 			usage(B_FALSE);
3377e9103aaeSGarrett D'Amore 		}
3378e9103aaeSGarrett D'Amore 	}
3379e9103aaeSGarrett D'Amore 
3380e9103aaeSGarrett D'Amore 	argc -= optind;
3381e9103aaeSGarrett D'Amore 	argv += optind;
3382e9103aaeSGarrett D'Amore 
3383e9103aaeSGarrett D'Amore 	/* get pool name and check number of arguments */
3384e9103aaeSGarrett D'Amore 	if (argc < 1) {
3385e9103aaeSGarrett D'Amore 		(void) fprintf(stderr, gettext("missing pool name\n"));
3386e9103aaeSGarrett D'Amore 		usage(B_FALSE);
3387e9103aaeSGarrett D'Amore 	}
3388e9103aaeSGarrett D'Amore 
3389e9103aaeSGarrett D'Amore 	if (argc > 1) {
3390e9103aaeSGarrett D'Amore 		(void) fprintf(stderr, gettext("too many arguments\n"));
3391e9103aaeSGarrett D'Amore 		usage(B_FALSE);
3392e9103aaeSGarrett D'Amore 	}
3393e9103aaeSGarrett D'Amore 
3394e9103aaeSGarrett D'Amore 	poolname = argv[0];
3395e9103aaeSGarrett D'Amore 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3396e9103aaeSGarrett D'Amore 		return (1);
3397e9103aaeSGarrett D'Amore 
3398e9103aaeSGarrett D'Amore 	ret = zpool_reguid(zhp);
3399e9103aaeSGarrett D'Amore 
3400e9103aaeSGarrett D'Amore 	zpool_close(zhp);
3401e9103aaeSGarrett D'Amore 	return (ret);
3402e9103aaeSGarrett D'Amore }
3403e9103aaeSGarrett D'Amore 
3404e9103aaeSGarrett D'Amore 
34054263d13fSGeorge Wilson /*
34064263d13fSGeorge Wilson  * zpool reopen <pool>
34074263d13fSGeorge Wilson  *
34084263d13fSGeorge Wilson  * Reopen the pool so that the kernel can update the sizes of all vdevs.
34094263d13fSGeorge Wilson  *
34104263d13fSGeorge Wilson  * NOTE: This command is currently undocumented.  If the command is ever
34114263d13fSGeorge Wilson  * exposed then the appropriate usage() messages will need to be made.
34124263d13fSGeorge Wilson  */
34134263d13fSGeorge Wilson int
34144263d13fSGeorge Wilson zpool_do_reopen(int argc, char **argv)
34154263d13fSGeorge Wilson {
34164263d13fSGeorge Wilson 	int ret = 0;
34174263d13fSGeorge Wilson 	zpool_handle_t *zhp;
34184263d13fSGeorge Wilson 	char *pool;
34194263d13fSGeorge Wilson 
34204263d13fSGeorge Wilson 	argc--;
34214263d13fSGeorge Wilson 	argv++;
34224263d13fSGeorge Wilson 
34234263d13fSGeorge Wilson 	if (argc != 1)
34244263d13fSGeorge Wilson 		return (2);
34254263d13fSGeorge Wilson 
34264263d13fSGeorge Wilson 	pool = argv[0];
34274263d13fSGeorge Wilson 	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
34284263d13fSGeorge Wilson 		return (1);
34294263d13fSGeorge Wilson 
34304263d13fSGeorge Wilson 	ret = zpool_reopen(zhp);
34314263d13fSGeorge Wilson 	zpool_close(zhp);
34324263d13fSGeorge Wilson 	return (ret);
34334263d13fSGeorge Wilson }
34344263d13fSGeorge Wilson 
3435fa9e4066Sahrens typedef struct scrub_cbdata {
3436fa9e4066Sahrens 	int	cb_type;
343706eeb2adSek110237 	int	cb_argc;
343806eeb2adSek110237 	char	**cb_argv;
3439fa9e4066Sahrens } scrub_cbdata_t;
3440fa9e4066Sahrens 
3441fa9e4066Sahrens int
3442fa9e4066Sahrens scrub_callback(zpool_handle_t *zhp, void *data)
3443fa9e4066Sahrens {
3444fa9e4066Sahrens 	scrub_cbdata_t *cb = data;
344506eeb2adSek110237 	int err;
3446fa9e4066Sahrens 
3447ea8dc4b6Seschrock 	/*
3448ea8dc4b6Seschrock 	 * Ignore faulted pools.
3449ea8dc4b6Seschrock 	 */
3450ea8dc4b6Seschrock 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3451ea8dc4b6Seschrock 		(void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3452ea8dc4b6Seschrock 		    "currently unavailable\n"), zpool_get_name(zhp));
3453ea8dc4b6Seschrock 		return (1);
3454ea8dc4b6Seschrock 	}
3455ea8dc4b6Seschrock 
34563f9d6ad7SLin Ling 	err = zpool_scan(zhp, cb->cb_type);
345706eeb2adSek110237 
345806eeb2adSek110237 	return (err != 0);
3459fa9e4066Sahrens }
3460fa9e4066Sahrens 
3461fa9e4066Sahrens /*
3462fa9e4066Sahrens  * zpool scrub [-s] <pool> ...
3463fa9e4066Sahrens  *
3464fa9e4066Sahrens  *	-s	Stop.  Stops any in-progress scrub.
3465fa9e4066Sahrens  */
3466fa9e4066Sahrens int
3467fa9e4066Sahrens zpool_do_scrub(int argc, char **argv)
3468fa9e4066Sahrens {
3469fa9e4066Sahrens 	int c;
3470fa9e4066Sahrens 	scrub_cbdata_t cb;
3471fa9e4066Sahrens 
34723f9d6ad7SLin Ling 	cb.cb_type = POOL_SCAN_SCRUB;
3473fa9e4066Sahrens 
3474fa9e4066Sahrens 	/* check options */
3475fa9e4066Sahrens 	while ((c = getopt(argc, argv, "s")) != -1) {
3476fa9e4066Sahrens 		switch (c) {
3477fa9e4066Sahrens 		case 's':
34783f9d6ad7SLin Ling 			cb.cb_type = POOL_SCAN_NONE;
3479fa9e4066Sahrens 			break;
3480fa9e4066Sahrens 		case '?':
3481fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3482fa9e4066Sahrens 			    optopt);
348399653d4eSeschrock 			usage(B_FALSE);
3484fa9e4066Sahrens 		}
3485fa9e4066Sahrens 	}
3486fa9e4066Sahrens 
348706eeb2adSek110237 	cb.cb_argc = argc;
348806eeb2adSek110237 	cb.cb_argv = argv;
3489fa9e4066Sahrens 	argc -= optind;
3490fa9e4066Sahrens 	argv += optind;
3491fa9e4066Sahrens 
3492fa9e4066Sahrens 	if (argc < 1) {
3493fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
349499653d4eSeschrock 		usage(B_FALSE);
3495fa9e4066Sahrens 	}
3496fa9e4066Sahrens 
3497b1b8ab34Slling 	return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3498fa9e4066Sahrens }
3499fa9e4066Sahrens 
3500fa9e4066Sahrens typedef struct status_cbdata {
3501fa9e4066Sahrens 	int		cb_count;
3502e9dbad6fSeschrock 	boolean_t	cb_allpools;
350399653d4eSeschrock 	boolean_t	cb_verbose;
350499653d4eSeschrock 	boolean_t	cb_explain;
350599653d4eSeschrock 	boolean_t	cb_first;
35069eb19f4dSGeorge Wilson 	boolean_t	cb_dedup_stats;
3507fa9e4066Sahrens } status_cbdata_t;
3508fa9e4066Sahrens 
3509fa9e4066Sahrens /*
3510fa9e4066Sahrens  * Print out detailed scrub status.
3511fa9e4066Sahrens  */
3512fa9e4066Sahrens void
35133f9d6ad7SLin Ling print_scan_status(pool_scan_stat_t *ps)
3514fa9e4066Sahrens {
35153f9d6ad7SLin Ling 	time_t start, end;
351673d314ceSLin Ling 	uint64_t elapsed, mins_left, hours_left;
35173f9d6ad7SLin Ling 	uint64_t pass_exam, examined, total;
35183f9d6ad7SLin Ling 	uint_t rate;
3519fa9e4066Sahrens 	double fraction_done;
35203f9d6ad7SLin Ling 	char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3521fa9e4066Sahrens 
35223f9d6ad7SLin Ling 	(void) printf(gettext("  scan: "));
3523fa9e4066Sahrens 
35243f9d6ad7SLin Ling 	/* If there's never been a scan, there's not much to say. */
35253f9d6ad7SLin Ling 	if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
35263f9d6ad7SLin Ling 	    ps->pss_func >= POOL_SCAN_FUNCS) {
3527fa9e4066Sahrens 		(void) printf(gettext("none requested\n"));
3528fa9e4066Sahrens 		return;
3529fa9e4066Sahrens 	}
3530fa9e4066Sahrens 
35313f9d6ad7SLin Ling 	start = ps->pss_start_time;
35323f9d6ad7SLin Ling 	end = ps->pss_end_time;
35333f9d6ad7SLin Ling 	zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3534fa9e4066Sahrens 
35353f9d6ad7SLin Ling 	assert(ps->pss_func == POOL_SCAN_SCRUB ||
35363f9d6ad7SLin Ling 	    ps->pss_func == POOL_SCAN_RESILVER);
35373f9d6ad7SLin Ling 	/*
35383f9d6ad7SLin Ling 	 * Scan is finished or canceled.
35393f9d6ad7SLin Ling 	 */
35403f9d6ad7SLin Ling 	if (ps->pss_state == DSS_FINISHED) {
35413f9d6ad7SLin Ling 		uint64_t minutes_taken = (end - start) / 60;
35423f9d6ad7SLin Ling 		char *fmt;
3543fa9e4066Sahrens 
35443f9d6ad7SLin Ling 		if (ps->pss_func == POOL_SCAN_SCRUB) {
35453f9d6ad7SLin Ling 			fmt = gettext("scrub repaired %s in %lluh%um with "
35463f9d6ad7SLin Ling 			    "%llu errors on %s");
35473f9d6ad7SLin Ling 		} else if (ps->pss_func == POOL_SCAN_RESILVER) {
35483f9d6ad7SLin Ling 			fmt = gettext("resilvered %s in %lluh%um with "
35493f9d6ad7SLin Ling 			    "%llu errors on %s");
35503f9d6ad7SLin Ling 		}
35513f9d6ad7SLin Ling 		/* LINTED */
35523f9d6ad7SLin Ling 		(void) printf(fmt, processed_buf,
355318ce54dfSek110237 		    (u_longlong_t)(minutes_taken / 60),
355418ce54dfSek110237 		    (uint_t)(minutes_taken % 60),
35553f9d6ad7SLin Ling 		    (u_longlong_t)ps->pss_errors,
35563f9d6ad7SLin Ling 		    ctime((time_t *)&end));
35573f9d6ad7SLin Ling 		return;
35583f9d6ad7SLin Ling 	} else if (ps->pss_state == DSS_CANCELED) {
35593f9d6ad7SLin Ling 		if (ps->pss_func == POOL_SCAN_SCRUB) {
35603f9d6ad7SLin Ling 			(void) printf(gettext("scrub canceled on %s"),
35613f9d6ad7SLin Ling 			    ctime(&end));
35623f9d6ad7SLin Ling 		} else if (ps->pss_func == POOL_SCAN_RESILVER) {
35633f9d6ad7SLin Ling 			(void) printf(gettext("resilver canceled on %s"),
35643f9d6ad7SLin Ling 			    ctime(&end));
35653f9d6ad7SLin Ling 		}
3566fa9e4066Sahrens 		return;
3567fa9e4066Sahrens 	}
3568fa9e4066Sahrens 
35693f9d6ad7SLin Ling 	assert(ps->pss_state == DSS_SCANNING);
3570fa9e4066Sahrens 
35713f9d6ad7SLin Ling 	/*
35723f9d6ad7SLin Ling 	 * Scan is in progress.
35733f9d6ad7SLin Ling 	 */
35743f9d6ad7SLin Ling 	if (ps->pss_func == POOL_SCAN_SCRUB) {
35753f9d6ad7SLin Ling 		(void) printf(gettext("scrub in progress since %s"),
35763f9d6ad7SLin Ling 		    ctime(&start));
35773f9d6ad7SLin Ling 	} else if (ps->pss_func == POOL_SCAN_RESILVER) {
35783f9d6ad7SLin Ling 		(void) printf(gettext("resilver in progress since %s"),
35793f9d6ad7SLin Ling 		    ctime(&start));
35803f9d6ad7SLin Ling 	}
35813f9d6ad7SLin Ling 
35823f9d6ad7SLin Ling 	examined = ps->pss_examined ? ps->pss_examined : 1;
35833f9d6ad7SLin Ling 	total = ps->pss_to_examine;
3584fa9e4066Sahrens 	fraction_done = (double)examined / total;
3585fa9e4066Sahrens 
35863f9d6ad7SLin Ling 	/* elapsed time for this pass */
35873f9d6ad7SLin Ling 	elapsed = time(NULL) - ps->pss_pass_start;
35883f9d6ad7SLin Ling 	elapsed = elapsed ? elapsed : 1;
35893f9d6ad7SLin Ling 	pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
35903f9d6ad7SLin Ling 	rate = pass_exam / elapsed;
35913f9d6ad7SLin Ling 	rate = rate ? rate : 1;
35923f9d6ad7SLin Ling 	mins_left = ((total - examined) / rate) / 60;
359373d314ceSLin Ling 	hours_left = mins_left / 60;
35943f9d6ad7SLin Ling 
35953f9d6ad7SLin Ling 	zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
35963f9d6ad7SLin Ling 	zfs_nicenum(total, total_buf, sizeof (total_buf));
35973f9d6ad7SLin Ling 	zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
35983f9d6ad7SLin Ling 
359973d314ceSLin Ling 	/*
360073d314ceSLin Ling 	 * do not print estimated time if hours_left is more than 30 days
360173d314ceSLin Ling 	 */
360273d314ceSLin Ling 	(void) printf(gettext("    %s scanned out of %s at %s/s"),
360373d314ceSLin Ling 	    examined_buf, total_buf, rate_buf);
360473d314ceSLin Ling 	if (hours_left < (30 * 24)) {
360573d314ceSLin Ling 		(void) printf(gettext(", %lluh%um to go\n"),
360673d314ceSLin Ling 		    (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
360773d314ceSLin Ling 	} else {
360873d314ceSLin Ling 		(void) printf(gettext(
360973d314ceSLin Ling 		    ", (scan is slow, no estimated time)\n"));
361073d314ceSLin Ling 	}
36113f9d6ad7SLin Ling 
36123f9d6ad7SLin Ling 	if (ps->pss_func == POOL_SCAN_RESILVER) {
36133f9d6ad7SLin Ling 		(void) printf(gettext("    %s resilvered, %.2f%% done\n"),
36143f9d6ad7SLin Ling 		    processed_buf, 100 * fraction_done);
36153f9d6ad7SLin Ling 	} else if (ps->pss_func == POOL_SCAN_SCRUB) {
36163f9d6ad7SLin Ling 		(void) printf(gettext("    %s repaired, %.2f%% done\n"),
36173f9d6ad7SLin Ling 		    processed_buf, 100 * fraction_done);
36183f9d6ad7SLin Ling 	}
3619fa9e4066Sahrens }
3620fa9e4066Sahrens 
3621ea8dc4b6Seschrock static void
3622ea8dc4b6Seschrock print_error_log(zpool_handle_t *zhp)
3623ea8dc4b6Seschrock {
362475519f38Sek110237 	nvlist_t *nverrlist = NULL;
362555434c77Sek110237 	nvpair_t *elem;
362655434c77Sek110237 	char *pathname;
362755434c77Sek110237 	size_t len = MAXPATHLEN * 2;
3628ea8dc4b6Seschrock 
362955434c77Sek110237 	if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3630ea8dc4b6Seschrock 		(void) printf("errors: List of errors unavailable "
3631ea8dc4b6Seschrock 		    "(insufficient privileges)\n");
3632ea8dc4b6Seschrock 		return;
3633ea8dc4b6Seschrock 	}
3634ea8dc4b6Seschrock 
363555434c77Sek110237 	(void) printf("errors: Permanent errors have been "
363655434c77Sek110237 	    "detected in the following files:\n\n");
3637ea8dc4b6Seschrock 
363855434c77Sek110237 	pathname = safe_malloc(len);
363955434c77Sek110237 	elem = NULL;
364055434c77Sek110237 	while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
364155434c77Sek110237 		nvlist_t *nv;
364255434c77Sek110237 		uint64_t dsobj, obj;
3643ea8dc4b6Seschrock 
364455434c77Sek110237 		verify(nvpair_value_nvlist(elem, &nv) == 0);
364555434c77Sek110237 		verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
364655434c77Sek110237 		    &dsobj) == 0);
364755434c77Sek110237 		verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
364855434c77Sek110237 		    &obj) == 0);
364955434c77Sek110237 		zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
365055434c77Sek110237 		(void) printf("%7s %s\n", "", pathname);
3651ea8dc4b6Seschrock 	}
365255434c77Sek110237 	free(pathname);
365355434c77Sek110237 	nvlist_free(nverrlist);
3654ea8dc4b6Seschrock }
3655ea8dc4b6Seschrock 
365699653d4eSeschrock static void
365799653d4eSeschrock print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
365899653d4eSeschrock     int namewidth)
365999653d4eSeschrock {
366099653d4eSeschrock 	uint_t i;
366199653d4eSeschrock 	char *name;
366299653d4eSeschrock 
366399653d4eSeschrock 	if (nspares == 0)
366499653d4eSeschrock 		return;
366599653d4eSeschrock 
366699653d4eSeschrock 	(void) printf(gettext("\tspares\n"));
366799653d4eSeschrock 
366899653d4eSeschrock 	for (i = 0; i < nspares; i++) {
366988ecc943SGeorge Wilson 		name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
367099653d4eSeschrock 		print_status_config(zhp, name, spares[i],
3671aa8cf21aSNeil Perrin 		    namewidth, 2, B_TRUE);
367299653d4eSeschrock 		free(name);
367399653d4eSeschrock 	}
367499653d4eSeschrock }
367599653d4eSeschrock 
3676fa94a07fSbrendan static void
3677fa94a07fSbrendan print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3678fa94a07fSbrendan     int namewidth)
3679fa94a07fSbrendan {
3680fa94a07fSbrendan 	uint_t i;
3681fa94a07fSbrendan 	char *name;
3682fa94a07fSbrendan 
3683fa94a07fSbrendan 	if (nl2cache == 0)
3684fa94a07fSbrendan 		return;
3685fa94a07fSbrendan 
3686fa94a07fSbrendan 	(void) printf(gettext("\tcache\n"));
3687fa94a07fSbrendan 
3688fa94a07fSbrendan 	for (i = 0; i < nl2cache; i++) {
368988ecc943SGeorge Wilson 		name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3690fa94a07fSbrendan 		print_status_config(zhp, name, l2cache[i],
3691aa8cf21aSNeil Perrin 		    namewidth, 2, B_FALSE);
3692aa8cf21aSNeil Perrin 		free(name);
3693aa8cf21aSNeil Perrin 	}
3694aa8cf21aSNeil Perrin }
3695aa8cf21aSNeil Perrin 
36969eb19f4dSGeorge Wilson static void
36979eb19f4dSGeorge Wilson print_dedup_stats(nvlist_t *config)
36989eb19f4dSGeorge Wilson {
36999eb19f4dSGeorge Wilson 	ddt_histogram_t *ddh;
37009eb19f4dSGeorge Wilson 	ddt_stat_t *dds;
37019eb19f4dSGeorge Wilson 	ddt_object_t *ddo;
37029eb19f4dSGeorge Wilson 	uint_t c;
37039eb19f4dSGeorge Wilson 
37049eb19f4dSGeorge Wilson 	/*
37059eb19f4dSGeorge Wilson 	 * If the pool was faulted then we may not have been able to
37069eb19f4dSGeorge Wilson 	 * obtain the config. Otherwise, if have anything in the dedup
37079eb19f4dSGeorge Wilson 	 * table continue processing the stats.
37089eb19f4dSGeorge Wilson 	 */
37099eb19f4dSGeorge Wilson 	if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
3710ce72e614SYuri Pankov 	    (uint64_t **)&ddo, &c) != 0)
37119eb19f4dSGeorge Wilson 		return;
37129eb19f4dSGeorge Wilson 
37139eb19f4dSGeorge Wilson 	(void) printf("\n");
3714ce72e614SYuri Pankov 	(void) printf(gettext(" dedup: "));
3715ce72e614SYuri Pankov 	if (ddo->ddo_count == 0) {
3716ce72e614SYuri Pankov 		(void) printf(gettext("no DDT entries\n"));
3717ce72e614SYuri Pankov 		return;
3718ce72e614SYuri Pankov 	}
3719ce72e614SYuri Pankov 
37209eb19f4dSGeorge Wilson 	(void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
37219eb19f4dSGeorge Wilson 	    (u_longlong_t)ddo->ddo_count,
37229eb19f4dSGeorge Wilson 	    (u_longlong_t)ddo->ddo_dspace,
37239eb19f4dSGeorge Wilson 	    (u_longlong_t)ddo->ddo_mspace);
37249eb19f4dSGeorge Wilson 
37259eb19f4dSGeorge Wilson 	verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
37269eb19f4dSGeorge Wilson 	    (uint64_t **)&dds, &c) == 0);
37279eb19f4dSGeorge Wilson 	verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
37289eb19f4dSGeorge Wilson 	    (uint64_t **)&ddh, &c) == 0);
37299eb19f4dSGeorge Wilson 	zpool_dump_ddt(dds, ddh);
37309eb19f4dSGeorge Wilson }
37319eb19f4dSGeorge Wilson 
3732aa8cf21aSNeil Perrin /*
3733fa9e4066Sahrens  * Display a summary of pool status.  Displays a summary such as:
3734fa9e4066Sahrens  *
3735fa9e4066Sahrens  *        pool: tank
3736fa9e4066Sahrens  *	status: DEGRADED
3737fa9e4066Sahrens  *	reason: One or more devices ...
3738*654b400cSJoshua M. Clulow  *         see: http://illumos.org/msg/ZFS-xxxx-01
3739fa9e4066Sahrens  *	config:
3740fa9e4066Sahrens  *		mirror		DEGRADED
3741fa9e4066Sahrens  *                c1t0d0	OK
3742ea8dc4b6Seschrock  *                c2t0d0	UNAVAIL
3743fa9e4066Sahrens  *
3744fa9e4066Sahrens  * When given the '-v' option, we print out the complete config.  If the '-e'
3745fa9e4066Sahrens  * option is specified, then we print out error rate information as well.
3746fa9e4066Sahrens  */
3747fa9e4066Sahrens int
3748fa9e4066Sahrens status_callback(zpool_handle_t *zhp, void *data)
3749fa9e4066Sahrens {
3750fa9e4066Sahrens 	status_cbdata_t *cbp = data;
3751fa9e4066Sahrens 	nvlist_t *config, *nvroot;
3752fa9e4066Sahrens 	char *msgid;
3753fa9e4066Sahrens 	int reason;
375446657f8dSmmusante 	const char *health;
375546657f8dSmmusante 	uint_t c;
375646657f8dSmmusante 	vdev_stat_t *vs;
3757fa9e4066Sahrens 
3758088e9d47Seschrock 	config = zpool_get_config(zhp, NULL);
3759fa9e4066Sahrens 	reason = zpool_get_status(zhp, &msgid);
3760fa9e4066Sahrens 
3761fa9e4066Sahrens 	cbp->cb_count++;
3762fa9e4066Sahrens 
3763fa9e4066Sahrens 	/*
3764fa9e4066Sahrens 	 * If we were given 'zpool status -x', only report those pools with
3765fa9e4066Sahrens 	 * problems.
3766fa9e4066Sahrens 	 */
3767e9dbad6fSeschrock 	if (reason == ZPOOL_STATUS_OK && cbp->cb_explain) {
3768e9dbad6fSeschrock 		if (!cbp->cb_allpools) {
3769e9dbad6fSeschrock 			(void) printf(gettext("pool '%s' is healthy\n"),
3770e9dbad6fSeschrock 			    zpool_get_name(zhp));
3771e9dbad6fSeschrock 			if (cbp->cb_first)
3772e9dbad6fSeschrock 				cbp->cb_first = B_FALSE;
3773e9dbad6fSeschrock 		}
3774fa9e4066Sahrens 		return (0);
3775e9dbad6fSeschrock 	}
3776fa9e4066Sahrens 
3777fa9e4066Sahrens 	if (cbp->cb_first)
377899653d4eSeschrock 		cbp->cb_first = B_FALSE;
3779fa9e4066Sahrens 	else
3780fa9e4066Sahrens 		(void) printf("\n");
3781fa9e4066Sahrens 
378246657f8dSmmusante 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
378346657f8dSmmusante 	    &nvroot) == 0);
37843f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
378546657f8dSmmusante 	    (uint64_t **)&vs, &c) == 0);
3786990b4856Slling 	health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
3787fa9e4066Sahrens 
3788fa9e4066Sahrens 	(void) printf(gettext("  pool: %s\n"), zpool_get_name(zhp));
3789fa9e4066Sahrens 	(void) printf(gettext(" state: %s\n"), health);
3790fa9e4066Sahrens 
3791fa9e4066Sahrens 	switch (reason) {
3792fa9e4066Sahrens 	case ZPOOL_STATUS_MISSING_DEV_R:
3793fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices could not "
3794fa9e4066Sahrens 		    "be opened.  Sufficient replicas exist for\n\tthe pool to "
3795fa9e4066Sahrens 		    "continue functioning in a degraded state.\n"));
3796fa9e4066Sahrens 		(void) printf(gettext("action: Attach the missing device and "
3797fa9e4066Sahrens 		    "online it using 'zpool online'.\n"));
3798fa9e4066Sahrens 		break;
3799fa9e4066Sahrens 
3800fa9e4066Sahrens 	case ZPOOL_STATUS_MISSING_DEV_NR:
3801fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices could not "
3802fa9e4066Sahrens 		    "be opened.  There are insufficient\n\treplicas for the "
3803fa9e4066Sahrens 		    "pool to continue functioning.\n"));
3804fa9e4066Sahrens 		(void) printf(gettext("action: Attach the missing device and "
3805fa9e4066Sahrens 		    "online it using 'zpool online'.\n"));
3806fa9e4066Sahrens 		break;
3807fa9e4066Sahrens 
3808fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_LABEL_R:
3809fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices could not "
3810fa9e4066Sahrens 		    "be used because the label is missing or\n\tinvalid.  "
3811fa9e4066Sahrens 		    "Sufficient replicas exist for the pool to continue\n\t"
3812fa9e4066Sahrens 		    "functioning in a degraded state.\n"));
3813fa9e4066Sahrens 		(void) printf(gettext("action: Replace the device using "
3814fa9e4066Sahrens 		    "'zpool replace'.\n"));
3815fa9e4066Sahrens 		break;
3816fa9e4066Sahrens 
3817fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_LABEL_NR:
3818fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices could not "
3819b1b8ab34Slling 		    "be used because the label is missing \n\tor invalid.  "
3820fa9e4066Sahrens 		    "There are insufficient replicas for the pool to "
3821fa9e4066Sahrens 		    "continue\n\tfunctioning.\n"));
3822468c413aSTim Haley 		zpool_explain_recover(zpool_get_handle(zhp),
3823468c413aSTim Haley 		    zpool_get_name(zhp), reason, config);
3824fa9e4066Sahrens 		break;
3825fa9e4066Sahrens 
3826fa9e4066Sahrens 	case ZPOOL_STATUS_FAILING_DEV:
3827fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices has "
3828fa9e4066Sahrens 		    "experienced an unrecoverable error.  An\n\tattempt was "
3829fa9e4066Sahrens 		    "made to correct the error.  Applications are "
3830fa9e4066Sahrens 		    "unaffected.\n"));
3831fa9e4066Sahrens 		(void) printf(gettext("action: Determine if the device needs "
3832fa9e4066Sahrens 		    "to be replaced, and clear the errors\n\tusing "
3833ea8dc4b6Seschrock 		    "'zpool clear' or replace the device with 'zpool "
3834fa9e4066Sahrens 		    "replace'.\n"));
3835fa9e4066Sahrens 		break;
3836fa9e4066Sahrens 
3837fa9e4066Sahrens 	case ZPOOL_STATUS_OFFLINE_DEV:
3838fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices has "
3839d7d4af51Smmusante 		    "been taken offline by the administrator.\n\tSufficient "
3840fa9e4066Sahrens 		    "replicas exist for the pool to continue functioning in "
3841fa9e4066Sahrens 		    "a\n\tdegraded state.\n"));
3842fa9e4066Sahrens 		(void) printf(gettext("action: Online the device using "
3843fa9e4066Sahrens 		    "'zpool online' or replace the device with\n\t'zpool "
3844fa9e4066Sahrens 		    "replace'.\n"));
3845fa9e4066Sahrens 		break;
3846fa9e4066Sahrens 
3847c25309d4SGeorge Wilson 	case ZPOOL_STATUS_REMOVED_DEV:
3848c25309d4SGeorge Wilson 		(void) printf(gettext("status: One or more devices has "
3849c25309d4SGeorge Wilson 		    "been removed by the administrator.\n\tSufficient "
3850c25309d4SGeorge Wilson 		    "replicas exist for the pool to continue functioning in "
3851c25309d4SGeorge Wilson 		    "a\n\tdegraded state.\n"));
3852c25309d4SGeorge Wilson 		(void) printf(gettext("action: Online the device using "
3853c25309d4SGeorge Wilson 		    "'zpool online' or replace the device with\n\t'zpool "
3854c25309d4SGeorge Wilson 		    "replace'.\n"));
3855c25309d4SGeorge Wilson 		break;
3856c25309d4SGeorge Wilson 
3857fa9e4066Sahrens 	case ZPOOL_STATUS_RESILVERING:
3858fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices is "
3859fa9e4066Sahrens 		    "currently being resilvered.  The pool will\n\tcontinue "
3860fa9e4066Sahrens 		    "to function, possibly in a degraded state.\n"));
3861fa9e4066Sahrens 		(void) printf(gettext("action: Wait for the resilver to "
3862fa9e4066Sahrens 		    "complete.\n"));
3863fa9e4066Sahrens 		break;
3864fa9e4066Sahrens 
3865ea8dc4b6Seschrock 	case ZPOOL_STATUS_CORRUPT_DATA:
3866ea8dc4b6Seschrock 		(void) printf(gettext("status: One or more devices has "
3867ea8dc4b6Seschrock 		    "experienced an error resulting in data\n\tcorruption.  "
3868ea8dc4b6Seschrock 		    "Applications may be affected.\n"));
3869ea8dc4b6Seschrock 		(void) printf(gettext("action: Restore the file in question "
3870ea8dc4b6Seschrock 		    "if possible.  Otherwise restore the\n\tentire pool from "
3871ea8dc4b6Seschrock 		    "backup.\n"));
3872ea8dc4b6Seschrock 		break;
3873ea8dc4b6Seschrock 
3874ea8dc4b6Seschrock 	case ZPOOL_STATUS_CORRUPT_POOL:
3875ea8dc4b6Seschrock 		(void) printf(gettext("status: The pool metadata is corrupted "
3876ea8dc4b6Seschrock 		    "and the pool cannot be opened.\n"));
3877468c413aSTim Haley 		zpool_explain_recover(zpool_get_handle(zhp),
3878468c413aSTim Haley 		    zpool_get_name(zhp), reason, config);
3879ea8dc4b6Seschrock 		break;
3880ea8dc4b6Seschrock 
3881eaca9bbdSeschrock 	case ZPOOL_STATUS_VERSION_OLDER:
3882eaca9bbdSeschrock 		(void) printf(gettext("status: The pool is formatted using an "
3883eaca9bbdSeschrock 		    "older on-disk format.  The pool can\n\tstill be used, but "
3884eaca9bbdSeschrock 		    "some features are unavailable.\n"));
3885eaca9bbdSeschrock 		(void) printf(gettext("action: Upgrade the pool using 'zpool "
3886eaca9bbdSeschrock 		    "upgrade'.  Once this is done, the\n\tpool will no longer "
3887eaca9bbdSeschrock 		    "be accessible on older software versions.\n"));
3888eaca9bbdSeschrock 		break;
3889eaca9bbdSeschrock 
3890eaca9bbdSeschrock 	case ZPOOL_STATUS_VERSION_NEWER:
3891eaca9bbdSeschrock 		(void) printf(gettext("status: The pool has been upgraded to a "
3892eaca9bbdSeschrock 		    "newer, incompatible on-disk version.\n\tThe pool cannot "
3893eaca9bbdSeschrock 		    "be accessed on this system.\n"));
3894eaca9bbdSeschrock 		(void) printf(gettext("action: Access the pool from a system "
3895eaca9bbdSeschrock 		    "running more recent software, or\n\trestore the pool from "
3896eaca9bbdSeschrock 		    "backup.\n"));
3897eaca9bbdSeschrock 		break;
3898eaca9bbdSeschrock 
38993d7072f8Seschrock 	case ZPOOL_STATUS_FAULTED_DEV_R:
39003d7072f8Seschrock 		(void) printf(gettext("status: One or more devices are "
39013d7072f8Seschrock 		    "faulted in response to persistent errors.\n\tSufficient "
39023d7072f8Seschrock 		    "replicas exist for the pool to continue functioning "
39033d7072f8Seschrock 		    "in a\n\tdegraded state.\n"));
39043d7072f8Seschrock 		(void) printf(gettext("action: Replace the faulted device, "
39053d7072f8Seschrock 		    "or use 'zpool clear' to mark the device\n\trepaired.\n"));
39063d7072f8Seschrock 		break;
39073d7072f8Seschrock 
39083d7072f8Seschrock 	case ZPOOL_STATUS_FAULTED_DEV_NR:
39093d7072f8Seschrock 		(void) printf(gettext("status: One or more devices are "
39103d7072f8Seschrock 		    "faulted in response to persistent errors.  There are "
39113d7072f8Seschrock 		    "insufficient replicas for the pool to\n\tcontinue "
39123d7072f8Seschrock 		    "functioning.\n"));
39133d7072f8Seschrock 		(void) printf(gettext("action: Destroy and re-create the pool "
39143d7072f8Seschrock 		    "from a backup source.  Manually marking the device\n"
39153d7072f8Seschrock 		    "\trepaired using 'zpool clear' may allow some data "
39163d7072f8Seschrock 		    "to be recovered.\n"));
39173d7072f8Seschrock 		break;
39183d7072f8Seschrock 
391932b87932Sek110237 	case ZPOOL_STATUS_IO_FAILURE_WAIT:
392032b87932Sek110237 	case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
392132b87932Sek110237 		(void) printf(gettext("status: One or more devices are "
39228a79c1b5Sek110237 		    "faulted in response to IO failures.\n"));
392332b87932Sek110237 		(void) printf(gettext("action: Make sure the affected devices "
392432b87932Sek110237 		    "are connected, then run 'zpool clear'.\n"));
392532b87932Sek110237 		break;
392632b87932Sek110237 
3927b87f3af3Sperrin 	case ZPOOL_STATUS_BAD_LOG:
3928b87f3af3Sperrin 		(void) printf(gettext("status: An intent log record "
3929b87f3af3Sperrin 		    "could not be read.\n"
3930b87f3af3Sperrin 		    "\tWaiting for adminstrator intervention to fix the "
3931b87f3af3Sperrin 		    "faulted pool.\n"));
3932b87f3af3Sperrin 		(void) printf(gettext("action: Either restore the affected "
3933b87f3af3Sperrin 		    "device(s) and run 'zpool online',\n"
3934b87f3af3Sperrin 		    "\tor ignore the intent log records by running "
3935b87f3af3Sperrin 		    "'zpool clear'.\n"));
3936b87f3af3Sperrin 		break;
3937b87f3af3Sperrin 
3938fa9e4066Sahrens 	default:
3939fa9e4066Sahrens 		/*
3940fa9e4066Sahrens 		 * The remaining errors can't actually be generated, yet.
3941fa9e4066Sahrens 		 */
3942fa9e4066Sahrens 		assert(reason == ZPOOL_STATUS_OK);
3943fa9e4066Sahrens 	}
3944fa9e4066Sahrens 
3945fa9e4066Sahrens 	if (msgid != NULL)
3946*654b400cSJoshua M. Clulow 		(void) printf(gettext("   see: http://illumos.org/msg/%s\n"),
3947fa9e4066Sahrens 		    msgid);
3948fa9e4066Sahrens 
3949fa9e4066Sahrens 	if (config != NULL) {
3950fa9e4066Sahrens 		int namewidth;
3951ea8dc4b6Seschrock 		uint64_t nerr;
3952fa94a07fSbrendan 		nvlist_t **spares, **l2cache;
3953fa94a07fSbrendan 		uint_t nspares, nl2cache;
39543f9d6ad7SLin Ling 		pool_scan_stat_t *ps = NULL;
3955fa9e4066Sahrens 
39563f9d6ad7SLin Ling 		(void) nvlist_lookup_uint64_array(nvroot,
39573f9d6ad7SLin Ling 		    ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
39583f9d6ad7SLin Ling 		print_scan_status(ps);
3959fa9e4066Sahrens 
3960c67d9675Seschrock 		namewidth = max_width(zhp, nvroot, 0, 0);
3961fa9e4066Sahrens 		if (namewidth < 10)
3962fa9e4066Sahrens 			namewidth = 10;
3963fa9e4066Sahrens 
3964fa9e4066Sahrens 		(void) printf(gettext("config:\n\n"));
3965fa9e4066Sahrens 		(void) printf(gettext("\t%-*s  %-8s %5s %5s %5s\n"), namewidth,
3966fa9e4066Sahrens 		    "NAME", "STATE", "READ", "WRITE", "CKSUM");
3967c67d9675Seschrock 		print_status_config(zhp, zpool_get_name(zhp), nvroot,
3968aa8cf21aSNeil Perrin 		    namewidth, 0, B_FALSE);
396999653d4eSeschrock 
39704dea40f0SNeil Perrin 		if (num_logs(nvroot) > 0)
3971e6ca193dSGeorge Wilson 			print_logs(zhp, nvroot, namewidth, B_TRUE);
3972fa94a07fSbrendan 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3973fa94a07fSbrendan 		    &l2cache, &nl2cache) == 0)
3974fa94a07fSbrendan 			print_l2cache(zhp, l2cache, nl2cache, namewidth);
3975fa94a07fSbrendan 
397699653d4eSeschrock 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
397799653d4eSeschrock 		    &spares, &nspares) == 0)
397899653d4eSeschrock 			print_spares(zhp, spares, nspares, namewidth);
3979ea8dc4b6Seschrock 
3980ea8dc4b6Seschrock 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
3981ea8dc4b6Seschrock 		    &nerr) == 0) {
398255434c77Sek110237 			nvlist_t *nverrlist = NULL;
398355434c77Sek110237 
3984ea8dc4b6Seschrock 			/*
3985ea8dc4b6Seschrock 			 * If the approximate error count is small, get a
3986ea8dc4b6Seschrock 			 * precise count by fetching the entire log and
3987ea8dc4b6Seschrock 			 * uniquifying the results.
3988ea8dc4b6Seschrock 			 */
398975519f38Sek110237 			if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
399055434c77Sek110237 			    zpool_get_errlog(zhp, &nverrlist) == 0) {
399155434c77Sek110237 				nvpair_t *elem;
399255434c77Sek110237 
399355434c77Sek110237 				elem = NULL;
399455434c77Sek110237 				nerr = 0;
399555434c77Sek110237 				while ((elem = nvlist_next_nvpair(nverrlist,
399655434c77Sek110237 				    elem)) != NULL) {
399755434c77Sek110237 					nerr++;
399855434c77Sek110237 				}
399955434c77Sek110237 			}
400055434c77Sek110237 			nvlist_free(nverrlist);
4001ea8dc4b6Seschrock 
4002ea8dc4b6Seschrock 			(void) printf("\n");
400399653d4eSeschrock 
4004ea8dc4b6Seschrock 			if (nerr == 0)
4005ea8dc4b6Seschrock 				(void) printf(gettext("errors: No known data "
4006ea8dc4b6Seschrock 				    "errors\n"));
4007ea8dc4b6Seschrock 			else if (!cbp->cb_verbose)
4008e9dbad6fSeschrock 				(void) printf(gettext("errors: %llu data "
40095ad82045Snd150628 				    "errors, use '-v' for a list\n"),
40105ad82045Snd150628 				    (u_longlong_t)nerr);
4011ea8dc4b6Seschrock 			else
4012ea8dc4b6Seschrock 				print_error_log(zhp);
4013ea8dc4b6Seschrock 		}
40149eb19f4dSGeorge Wilson 
40159eb19f4dSGeorge Wilson 		if (cbp->cb_dedup_stats)
40169eb19f4dSGeorge Wilson 			print_dedup_stats(config);
4017fa9e4066Sahrens 	} else {
4018fa9e4066Sahrens 		(void) printf(gettext("config: The configuration cannot be "
4019fa9e4066Sahrens 		    "determined.\n"));
4020fa9e4066Sahrens 	}
4021fa9e4066Sahrens 
4022fa9e4066Sahrens 	return (0);
4023fa9e4066Sahrens }
4024fa9e4066Sahrens 
4025fa9e4066Sahrens /*
40263f9d6ad7SLin Ling  * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4027fa9e4066Sahrens  *
4028fa9e4066Sahrens  *	-v	Display complete error logs
4029fa9e4066Sahrens  *	-x	Display only pools with potential problems
40309eb19f4dSGeorge Wilson  *	-D	Display dedup status (undocumented)
40313f9d6ad7SLin Ling  *	-T	Display a timestamp in date(1) or Unix format
4032fa9e4066Sahrens  *
4033fa9e4066Sahrens  * Describes the health status of all pools or some subset.
4034fa9e4066Sahrens  */
4035fa9e4066Sahrens int
4036fa9e4066Sahrens zpool_do_status(int argc, char **argv)
4037fa9e4066Sahrens {
4038fa9e4066Sahrens 	int c;
4039fa9e4066Sahrens 	int ret;
40403f9d6ad7SLin Ling 	unsigned long interval = 0, count = 0;
4041fa9e4066Sahrens 	status_cbdata_t cb = { 0 };
4042fa9e4066Sahrens 
4043fa9e4066Sahrens 	/* check options */
40443f9d6ad7SLin Ling 	while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4045fa9e4066Sahrens 		switch (c) {
4046fa9e4066Sahrens 		case 'v':
404799653d4eSeschrock 			cb.cb_verbose = B_TRUE;
4048fa9e4066Sahrens 			break;
4049fa9e4066Sahrens 		case 'x':
405099653d4eSeschrock 			cb.cb_explain = B_TRUE;
4051fa9e4066Sahrens 			break;
40529eb19f4dSGeorge Wilson 		case 'D':
40539eb19f4dSGeorge Wilson 			cb.cb_dedup_stats = B_TRUE;
40549eb19f4dSGeorge Wilson 			break;
40553f9d6ad7SLin Ling 		case 'T':
40563f9d6ad7SLin Ling 			get_timestamp_arg(*optarg);
40573f9d6ad7SLin Ling 			break;
4058fa9e4066Sahrens 		case '?':
4059fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4060fa9e4066Sahrens 			    optopt);
406199653d4eSeschrock 			usage(B_FALSE);
4062fa9e4066Sahrens 		}
4063fa9e4066Sahrens 	}
4064fa9e4066Sahrens 
4065fa9e4066Sahrens 	argc -= optind;
4066fa9e4066Sahrens 	argv += optind;
4067fa9e4066Sahrens 
40683f9d6ad7SLin Ling 	get_interval_count(&argc, argv, &interval, &count);
4069fa9e4066Sahrens 
4070e9dbad6fSeschrock 	if (argc == 0)
4071e9dbad6fSeschrock 		cb.cb_allpools = B_TRUE;
4072e9dbad6fSeschrock 
40733f9d6ad7SLin Ling 	cb.cb_first = B_TRUE;
40743f9d6ad7SLin Ling 
40753f9d6ad7SLin Ling 	for (;;) {
40763f9d6ad7SLin Ling 		if (timestamp_fmt != NODATE)
40773f9d6ad7SLin Ling 			print_timestamp(timestamp_fmt);
40783f9d6ad7SLin Ling 
40793f9d6ad7SLin Ling 		ret = for_each_pool(argc, argv, B_TRUE, NULL,
40803f9d6ad7SLin Ling 		    status_callback, &cb);
4081fa9e4066Sahrens 
4082fa9e4066Sahrens 		if (argc == 0 && cb.cb_count == 0)
4083fa9e4066Sahrens 			(void) printf(gettext("no pools available\n"));
4084e9dbad6fSeschrock 		else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4085fa9e4066Sahrens 			(void) printf(gettext("all pools are healthy\n"));
4086fa9e4066Sahrens 
40873f9d6ad7SLin Ling 		if (ret != 0)
4088fa9e4066Sahrens 			return (ret);
40893f9d6ad7SLin Ling 
40903f9d6ad7SLin Ling 		if (interval == 0)
40913f9d6ad7SLin Ling 			break;
40923f9d6ad7SLin Ling 
40933f9d6ad7SLin Ling 		if (count != 0 && --count == 0)
40943f9d6ad7SLin Ling 			break;
40953f9d6ad7SLin Ling 
40963f9d6ad7SLin Ling 		(void) sleep(interval);
40973f9d6ad7SLin Ling 	}
40983f9d6ad7SLin Ling 
40993f9d6ad7SLin Ling 	return (0);
4100fa9e4066Sahrens }
4101fa9e4066Sahrens 
4102eaca9bbdSeschrock typedef struct upgrade_cbdata {
4103eaca9bbdSeschrock 	int	cb_all;
4104eaca9bbdSeschrock 	int	cb_first;
4105eaca9bbdSeschrock 	int	cb_newer;
410606eeb2adSek110237 	int	cb_argc;
4107990b4856Slling 	uint64_t cb_version;
410806eeb2adSek110237 	char	**cb_argv;
4109eaca9bbdSeschrock } upgrade_cbdata_t;
4110eaca9bbdSeschrock 
4111eaca9bbdSeschrock static int
4112eaca9bbdSeschrock upgrade_cb(zpool_handle_t *zhp, void *arg)
4113eaca9bbdSeschrock {
4114eaca9bbdSeschrock 	upgrade_cbdata_t *cbp = arg;
4115eaca9bbdSeschrock 	nvlist_t *config;
4116eaca9bbdSeschrock 	uint64_t version;
4117eaca9bbdSeschrock 	int ret = 0;
4118eaca9bbdSeschrock 
4119eaca9bbdSeschrock 	config = zpool_get_config(zhp, NULL);
4120eaca9bbdSeschrock 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4121eaca9bbdSeschrock 	    &version) == 0);
4122eaca9bbdSeschrock 
4123e7437265Sahrens 	if (!cbp->cb_newer && version < SPA_VERSION) {
4124eaca9bbdSeschrock 		if (!cbp->cb_all) {
4125eaca9bbdSeschrock 			if (cbp->cb_first) {
4126eaca9bbdSeschrock 				(void) printf(gettext("The following pools are "
4127eaca9bbdSeschrock 				    "out of date, and can be upgraded.  After "
4128eaca9bbdSeschrock 				    "being\nupgraded, these pools will no "
4129eaca9bbdSeschrock 				    "longer be accessible by older software "
4130eaca9bbdSeschrock 				    "versions.\n\n"));
4131eaca9bbdSeschrock 				(void) printf(gettext("VER  POOL\n"));
4132eaca9bbdSeschrock 				(void) printf(gettext("---  ------------\n"));
413399653d4eSeschrock 				cbp->cb_first = B_FALSE;
4134eaca9bbdSeschrock 			}
4135eaca9bbdSeschrock 
41365ad82045Snd150628 			(void) printf("%2llu   %s\n", (u_longlong_t)version,
4137eaca9bbdSeschrock 			    zpool_get_name(zhp));
4138eaca9bbdSeschrock 		} else {
413999653d4eSeschrock 			cbp->cb_first = B_FALSE;
4140990b4856Slling 			ret = zpool_upgrade(zhp, cbp->cb_version);
414106eeb2adSek110237 			if (!ret) {
4142eaca9bbdSeschrock 				(void) printf(gettext("Successfully upgraded "
4143990b4856Slling 				    "'%s'\n\n"), zpool_get_name(zhp));
4144eaca9bbdSeschrock 			}
414506eeb2adSek110237 		}
4146e7437265Sahrens 	} else if (cbp->cb_newer && version > SPA_VERSION) {
4147eaca9bbdSeschrock 		assert(!cbp->cb_all);
4148eaca9bbdSeschrock 
4149eaca9bbdSeschrock 		if (cbp->cb_first) {
4150eaca9bbdSeschrock 			(void) printf(gettext("The following pools are "
4151eaca9bbdSeschrock 			    "formatted using a newer software version and\n"
4152eaca9bbdSeschrock 			    "cannot be accessed on the current system.\n\n"));
4153eaca9bbdSeschrock 			(void) printf(gettext("VER  POOL\n"));
4154eaca9bbdSeschrock 			(void) printf(gettext("---  ------------\n"));
415599653d4eSeschrock 			cbp->cb_first = B_FALSE;
4156eaca9bbdSeschrock 		}
4157eaca9bbdSeschrock 
41585ad82045Snd150628 		(void) printf("%2llu   %s\n", (u_longlong_t)version,
4159eaca9bbdSeschrock 		    zpool_get_name(zhp));
4160eaca9bbdSeschrock 	}
4161eaca9bbdSeschrock 
4162eaca9bbdSeschrock 	zpool_close(zhp);
4163eaca9bbdSeschrock 	return (ret);
4164eaca9bbdSeschrock }
4165eaca9bbdSeschrock 
4166eaca9bbdSeschrock /* ARGSUSED */
4167eaca9bbdSeschrock static int
416806eeb2adSek110237 upgrade_one(zpool_handle_t *zhp, void *data)
4169eaca9bbdSeschrock {
4170990b4856Slling 	upgrade_cbdata_t *cbp = data;
4171990b4856Slling 	uint64_t cur_version;
4172eaca9bbdSeschrock 	int ret;
4173eaca9bbdSeschrock 
41748654d025Sperrin 	if (strcmp("log", zpool_get_name(zhp)) == 0) {
41758654d025Sperrin 		(void) printf(gettext("'log' is now a reserved word\n"
41768654d025Sperrin 		    "Pool 'log' must be renamed using export and import"
41778654d025Sperrin 		    " to upgrade.\n"));
41788654d025Sperrin 		return (1);
41798654d025Sperrin 	}
4180990b4856Slling 
4181990b4856Slling 	cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4182e6c728e1Sbrendan 	if (cur_version > cbp->cb_version) {
4183eaca9bbdSeschrock 		(void) printf(gettext("Pool '%s' is already formatted "
4184e6c728e1Sbrendan 		    "using more current version '%llu'.\n"),
4185e6c728e1Sbrendan 		    zpool_get_name(zhp), cur_version);
4186e6c728e1Sbrendan 		return (0);
4187e6c728e1Sbrendan 	}
4188e6c728e1Sbrendan 	if (cur_version == cbp->cb_version) {
4189e6c728e1Sbrendan 		(void) printf(gettext("Pool '%s' is already formatted "
4190e6c728e1Sbrendan 		    "using the current version.\n"), zpool_get_name(zhp));
4191eaca9bbdSeschrock 		return (0);
4192eaca9bbdSeschrock 	}
4193eaca9bbdSeschrock 
4194990b4856Slling 	ret = zpool_upgrade(zhp, cbp->cb_version);
419506eeb2adSek110237 
419606eeb2adSek110237 	if (!ret) {
419744cd46caSbillm 		(void) printf(gettext("Successfully upgraded '%s' "
4198990b4856Slling 		    "from version %llu to version %llu\n\n"),
4199990b4856Slling 		    zpool_get_name(zhp), (u_longlong_t)cur_version,
4200990b4856Slling 		    (u_longlong_t)cbp->cb_version);
420106eeb2adSek110237 	}
4202eaca9bbdSeschrock 
4203eaca9bbdSeschrock 	return (ret != 0);
4204eaca9bbdSeschrock }
4205eaca9bbdSeschrock 
4206eaca9bbdSeschrock /*
4207eaca9bbdSeschrock  * zpool upgrade
4208eaca9bbdSeschrock  * zpool upgrade -v
4209990b4856Slling  * zpool upgrade [-V version] <-a | pool ...>
4210eaca9bbdSeschrock  *
4211eaca9bbdSeschrock  * With no arguments, display downrev'd ZFS pool available for upgrade.
4212eaca9bbdSeschrock  * Individual pools can be upgraded by specifying the pool, and '-a' will
4213eaca9bbdSeschrock  * upgrade all pools.
4214eaca9bbdSeschrock  */
4215eaca9bbdSeschrock int
4216eaca9bbdSeschrock zpool_do_upgrade(int argc, char **argv)
4217eaca9bbdSeschrock {
4218eaca9bbdSeschrock 	int c;
4219eaca9bbdSeschrock 	upgrade_cbdata_t cb = { 0 };
4220eaca9bbdSeschrock 	int ret = 0;
4221eaca9bbdSeschrock 	boolean_t showversions = B_FALSE;
4222990b4856Slling 	char *end;
4223990b4856Slling 
4224eaca9bbdSeschrock 
4225eaca9bbdSeschrock 	/* check options */
4226478ed9adSEric Taylor 	while ((c = getopt(argc, argv, ":avV:")) != -1) {
4227eaca9bbdSeschrock 		switch (c) {
4228eaca9bbdSeschrock 		case 'a':
422999653d4eSeschrock 			cb.cb_all = B_TRUE;
4230eaca9bbdSeschrock 			break;
4231eaca9bbdSeschrock 		case 'v':
4232eaca9bbdSeschrock 			showversions = B_TRUE;
4233eaca9bbdSeschrock 			break;
4234990b4856Slling 		case 'V':
4235990b4856Slling 			cb.cb_version = strtoll(optarg, &end, 10);
4236351420b3Slling 			if (*end != '\0' || cb.cb_version > SPA_VERSION ||
4237351420b3Slling 			    cb.cb_version < SPA_VERSION_1) {
4238990b4856Slling 				(void) fprintf(stderr,
4239990b4856Slling 				    gettext("invalid version '%s'\n"), optarg);
4240990b4856Slling 				usage(B_FALSE);
4241990b4856Slling 			}
4242990b4856Slling 			break;
4243478ed9adSEric Taylor 		case ':':
4244478ed9adSEric Taylor 			(void) fprintf(stderr, gettext("missing argument for "
4245478ed9adSEric Taylor 			    "'%c' option\n"), optopt);
4246478ed9adSEric Taylor 			usage(B_FALSE);
4247478ed9adSEric Taylor 			break;
4248eaca9bbdSeschrock 		case '?':
4249eaca9bbdSeschrock 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4250eaca9bbdSeschrock 			    optopt);
425199653d4eSeschrock 			usage(B_FALSE);
4252eaca9bbdSeschrock 		}
4253eaca9bbdSeschrock 	}
4254eaca9bbdSeschrock 
425506eeb2adSek110237 	cb.cb_argc = argc;
425606eeb2adSek110237 	cb.cb_argv = argv;
4257eaca9bbdSeschrock 	argc -= optind;
4258eaca9bbdSeschrock 	argv += optind;
4259eaca9bbdSeschrock 
4260351420b3Slling 	if (cb.cb_version == 0) {
4261351420b3Slling 		cb.cb_version = SPA_VERSION;
4262351420b3Slling 	} else if (!cb.cb_all && argc == 0) {
4263351420b3Slling 		(void) fprintf(stderr, gettext("-V option is "
4264351420b3Slling 		    "incompatible with other arguments\n"));
4265351420b3Slling 		usage(B_FALSE);
4266351420b3Slling 	}
4267351420b3Slling 
4268eaca9bbdSeschrock 	if (showversions) {
4269eaca9bbdSeschrock 		if (cb.cb_all || argc != 0) {
4270eaca9bbdSeschrock 			(void) fprintf(stderr, gettext("-v option is "
4271eaca9bbdSeschrock 			    "incompatible with other arguments\n"));
427299653d4eSeschrock 			usage(B_FALSE);
4273eaca9bbdSeschrock 		}
4274eaca9bbdSeschrock 	} else if (cb.cb_all) {
4275eaca9bbdSeschrock 		if (argc != 0) {
4276351420b3Slling 			(void) fprintf(stderr, gettext("-a option should not "
4277351420b3Slling 			    "be used along with a pool name\n"));
427899653d4eSeschrock 			usage(B_FALSE);
4279eaca9bbdSeschrock 		}
4280eaca9bbdSeschrock 	}
4281eaca9bbdSeschrock 
4282e7437265Sahrens 	(void) printf(gettext("This system is currently running "
4283e7437265Sahrens 	    "ZFS pool version %llu.\n\n"), SPA_VERSION);
428499653d4eSeschrock 	cb.cb_first = B_TRUE;
4285eaca9bbdSeschrock 	if (showversions) {
4286eaca9bbdSeschrock 		(void) printf(gettext("The following versions are "
4287d7d4af51Smmusante 		    "supported:\n\n"));
4288eaca9bbdSeschrock 		(void) printf(gettext("VER  DESCRIPTION\n"));
4289eaca9bbdSeschrock 		(void) printf("---  -----------------------------------------"
4290eaca9bbdSeschrock 		    "---------------\n");
429199653d4eSeschrock 		(void) printf(gettext(" 1   Initial ZFS version\n"));
429244cd46caSbillm 		(void) printf(gettext(" 2   Ditto blocks "
429344cd46caSbillm 		    "(replicated metadata)\n"));
429499653d4eSeschrock 		(void) printf(gettext(" 3   Hot spares and double parity "
429599653d4eSeschrock 		    "RAID-Z\n"));
4296d7306b64Sek110237 		(void) printf(gettext(" 4   zpool history\n"));
4297c9431fa1Sahl 		(void) printf(gettext(" 5   Compression using the gzip "
4298c9431fa1Sahl 		    "algorithm\n"));
4299990b4856Slling 		(void) printf(gettext(" 6   bootfs pool property\n"));
43008654d025Sperrin 		(void) printf(gettext(" 7   Separate intent log devices\n"));
4301ecd6cf80Smarks 		(void) printf(gettext(" 8   Delegated administration\n"));
4302a9799022Sck153898 		(void) printf(gettext(" 9   refquota and refreservation "
4303a9799022Sck153898 		    "properties\n"));
4304fa94a07fSbrendan 		(void) printf(gettext(" 10  Cache devices\n"));
4305088f3894Sahrens 		(void) printf(gettext(" 11  Improved scrub performance\n"));
4306bb0ade09Sahrens 		(void) printf(gettext(" 12  Snapshot properties\n"));
430774e7dc98SMatthew Ahrens 		(void) printf(gettext(" 13  snapused property\n"));
430814843421SMatthew Ahrens 		(void) printf(gettext(" 14  passthrough-x aclinherit\n"));
430914843421SMatthew Ahrens 		(void) printf(gettext(" 15  user/group space accounting\n"));
4310478ed9adSEric Taylor 		(void) printf(gettext(" 16  stmf property support\n"));
43117aeab329SAdam Leventhal 		(void) printf(gettext(" 17  Triple-parity RAID-Z\n"));
4312b24ab676SJeff Bonwick 		(void) printf(gettext(" 18  Snapshot user holds\n"));
431388ecc943SGeorge Wilson 		(void) printf(gettext(" 19  Log device removal\n"));
4314b24ab676SJeff Bonwick 		(void) printf(gettext(" 20  Compression using zle "
4315b24ab676SJeff Bonwick 		    "(zero-length encoding)\n"));
4316b24ab676SJeff Bonwick 		(void) printf(gettext(" 21  Deduplication\n"));
431792241e0bSTom Erickson 		(void) printf(gettext(" 22  Received properties\n"));
43186e1f5caaSNeil Perrin 		(void) printf(gettext(" 23  Slim ZIL\n"));
43190a586ceaSMark Shellenbaum 		(void) printf(gettext(" 24  System attributes\n"));
43203f9d6ad7SLin Ling 		(void) printf(gettext(" 25  Improved scrub stats\n"));
4321cde58dbcSMatthew Ahrens 		(void) printf(gettext(" 26  Improved snapshot deletion "
4322cde58dbcSMatthew Ahrens 		    "performance\n"));
43236e0cbcaaSMatthew Ahrens 		(void) printf(gettext(" 27  Improved snapshot creation "
43246e0cbcaaSMatthew Ahrens 		    "performance\n"));
4325cb04b873SMark J Musante 		(void) printf(gettext(" 28  Multiple vdev replacements\n"));
4326b24ab676SJeff Bonwick 		(void) printf(gettext("\nFor more information on a particular "
43279a8685acSstephanie scheffler 		    "version, including supported releases,\n"));
43289a8685acSstephanie scheffler 		(void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4329eaca9bbdSeschrock 	} else if (argc == 0) {
4330eaca9bbdSeschrock 		int notfound;
4331eaca9bbdSeschrock 
433299653d4eSeschrock 		ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4333eaca9bbdSeschrock 		notfound = cb.cb_first;
4334eaca9bbdSeschrock 
4335eaca9bbdSeschrock 		if (!cb.cb_all && ret == 0) {
4336eaca9bbdSeschrock 			if (!cb.cb_first)
4337eaca9bbdSeschrock 				(void) printf("\n");
4338eaca9bbdSeschrock 			cb.cb_first = B_TRUE;
4339eaca9bbdSeschrock 			cb.cb_newer = B_TRUE;
434099653d4eSeschrock 			ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4341eaca9bbdSeschrock 			if (!cb.cb_first) {
4342eaca9bbdSeschrock 				notfound = B_FALSE;
4343eaca9bbdSeschrock 				(void) printf("\n");
4344eaca9bbdSeschrock 			}
4345eaca9bbdSeschrock 		}
4346eaca9bbdSeschrock 
4347eaca9bbdSeschrock 		if (ret == 0) {
4348eaca9bbdSeschrock 			if (notfound)
4349eaca9bbdSeschrock 				(void) printf(gettext("All pools are formatted "
4350eaca9bbdSeschrock 				    "using this version.\n"));
4351eaca9bbdSeschrock 			else if (!cb.cb_all)
4352eaca9bbdSeschrock 				(void) printf(gettext("Use 'zpool upgrade -v' "
4353eaca9bbdSeschrock 				    "for a list of available versions and "
4354eaca9bbdSeschrock 				    "their associated\nfeatures.\n"));
4355eaca9bbdSeschrock 		}
4356eaca9bbdSeschrock 	} else {
4357b1b8ab34Slling 		ret = for_each_pool(argc, argv, B_FALSE, NULL,
4358b1b8ab34Slling 		    upgrade_one, &cb);
435906eeb2adSek110237 	}
436006eeb2adSek110237 
436106eeb2adSek110237 	return (ret);
436206eeb2adSek110237 }
436306eeb2adSek110237 
4364ecd6cf80Smarks typedef struct hist_cbdata {
4365ecd6cf80Smarks 	boolean_t first;
4366ecd6cf80Smarks 	int longfmt;
4367ecd6cf80Smarks 	int internal;
4368ecd6cf80Smarks } hist_cbdata_t;
4369ecd6cf80Smarks 
437006eeb2adSek110237 /*
437106eeb2adSek110237  * Print out the command history for a specific pool.
437206eeb2adSek110237  */
437306eeb2adSek110237 static int
437406eeb2adSek110237 get_history_one(zpool_handle_t *zhp, void *data)
437506eeb2adSek110237 {
437606eeb2adSek110237 	nvlist_t *nvhis;
437706eeb2adSek110237 	nvlist_t **records;
437806eeb2adSek110237 	uint_t numrecords;
437906eeb2adSek110237 	char *cmdstr;
4380ecd6cf80Smarks 	char *pathstr;
438106eeb2adSek110237 	uint64_t dst_time;
438206eeb2adSek110237 	time_t tsec;
438306eeb2adSek110237 	struct tm t;
438406eeb2adSek110237 	char tbuf[30];
438506eeb2adSek110237 	int ret, i;
4386ecd6cf80Smarks 	uint64_t who;
4387ecd6cf80Smarks 	struct passwd *pwd;
4388ecd6cf80Smarks 	char *hostname;
4389ecd6cf80Smarks 	char *zonename;
4390ecd6cf80Smarks 	char internalstr[MAXPATHLEN];
4391ecd6cf80Smarks 	hist_cbdata_t *cb = (hist_cbdata_t *)data;
4392ecd6cf80Smarks 	uint64_t txg;
4393ecd6cf80Smarks 	uint64_t ievent;
439406eeb2adSek110237 
4395ecd6cf80Smarks 	cb->first = B_FALSE;
439606eeb2adSek110237 
439706eeb2adSek110237 	(void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
439806eeb2adSek110237 
439906eeb2adSek110237 	if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
440006eeb2adSek110237 		return (ret);
440106eeb2adSek110237 
440206eeb2adSek110237 	verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
440306eeb2adSek110237 	    &records, &numrecords) == 0);
440406eeb2adSek110237 	for (i = 0; i < numrecords; i++) {
440506eeb2adSek110237 		if (nvlist_lookup_uint64(records[i], ZPOOL_HIST_TIME,
4406ecd6cf80Smarks 		    &dst_time) != 0)
4407ecd6cf80Smarks 			continue;
4408ecd6cf80Smarks 
4409ecd6cf80Smarks 		/* is it an internal event or a standard event? */
4410ecd6cf80Smarks 		if (nvlist_lookup_string(records[i], ZPOOL_HIST_CMD,
4411ecd6cf80Smarks 		    &cmdstr) != 0) {
4412ecd6cf80Smarks 			if (cb->internal == 0)
4413ecd6cf80Smarks 				continue;
4414ecd6cf80Smarks 
4415ecd6cf80Smarks 			if (nvlist_lookup_uint64(records[i],
4416ecd6cf80Smarks 			    ZPOOL_HIST_INT_EVENT, &ievent) != 0)
4417ecd6cf80Smarks 				continue;
4418ecd6cf80Smarks 			verify(nvlist_lookup_uint64(records[i],
4419ecd6cf80Smarks 			    ZPOOL_HIST_TXG, &txg) == 0);
4420ecd6cf80Smarks 			verify(nvlist_lookup_string(records[i],
4421ecd6cf80Smarks 			    ZPOOL_HIST_INT_STR, &pathstr) == 0);
4422088f3894Sahrens 			if (ievent >= LOG_END)
4423ecd6cf80Smarks 				continue;
4424ecd6cf80Smarks 			(void) snprintf(internalstr,
4425ecd6cf80Smarks 			    sizeof (internalstr),
4426ecd6cf80Smarks 			    "[internal %s txg:%lld] %s",
44273f9d6ad7SLin Ling 			    zfs_history_event_names[ievent], txg,
4428ecd6cf80Smarks 			    pathstr);
4429ecd6cf80Smarks 			cmdstr = internalstr;
4430ecd6cf80Smarks 		}
443106eeb2adSek110237 		tsec = dst_time;
443206eeb2adSek110237 		(void) localtime_r(&tsec, &t);
443306eeb2adSek110237 		(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
4434ecd6cf80Smarks 		(void) printf("%s %s", tbuf, cmdstr);
4435ecd6cf80Smarks 
4436ecd6cf80Smarks 		if (!cb->longfmt) {
4437ecd6cf80Smarks 			(void) printf("\n");
4438ecd6cf80Smarks 			continue;
443906eeb2adSek110237 		}
4440ecd6cf80Smarks 		(void) printf(" [");
4441ecd6cf80Smarks 		if (nvlist_lookup_uint64(records[i],
4442ecd6cf80Smarks 		    ZPOOL_HIST_WHO, &who) == 0) {
4443ecd6cf80Smarks 			pwd = getpwuid((uid_t)who);
4444ecd6cf80Smarks 			if (pwd)
4445ecd6cf80Smarks 				(void) printf("user %s on",
4446ecd6cf80Smarks 				    pwd->pw_name);
4447ecd6cf80Smarks 			else
4448ecd6cf80Smarks 				(void) printf("user %d on",
4449ecd6cf80Smarks 				    (int)who);
4450ecd6cf80Smarks 		} else {
4451ecd6cf80Smarks 			(void) printf(gettext("no info]\n"));
4452ecd6cf80Smarks 			continue;
4453ecd6cf80Smarks 		}
4454ecd6cf80Smarks 		if (nvlist_lookup_string(records[i],
4455ecd6cf80Smarks 		    ZPOOL_HIST_HOST, &hostname) == 0) {
4456ecd6cf80Smarks 			(void) printf(" %s", hostname);
4457ecd6cf80Smarks 		}
4458ecd6cf80Smarks 		if (nvlist_lookup_string(records[i],
4459ecd6cf80Smarks 		    ZPOOL_HIST_ZONE, &zonename) == 0) {
4460ecd6cf80Smarks 			(void) printf(":%s", zonename);
4461ecd6cf80Smarks 		}
4462ecd6cf80Smarks 
4463ecd6cf80Smarks 		(void) printf("]");
4464ecd6cf80Smarks 		(void) printf("\n");
446506eeb2adSek110237 	}
446606eeb2adSek110237 	(void) printf("\n");
446706eeb2adSek110237 	nvlist_free(nvhis);
446806eeb2adSek110237 
446906eeb2adSek110237 	return (ret);
447006eeb2adSek110237 }
447106eeb2adSek110237 
447206eeb2adSek110237 /*
447306eeb2adSek110237  * zpool history <pool>
447406eeb2adSek110237  *
447506eeb2adSek110237  * Displays the history of commands that modified pools.
447606eeb2adSek110237  */
4477ecd6cf80Smarks 
4478ecd6cf80Smarks 
447906eeb2adSek110237 int
448006eeb2adSek110237 zpool_do_history(int argc, char **argv)
448106eeb2adSek110237 {
4482ecd6cf80Smarks 	hist_cbdata_t cbdata = { 0 };
448306eeb2adSek110237 	int ret;
4484ecd6cf80Smarks 	int c;
448506eeb2adSek110237 
4486ecd6cf80Smarks 	cbdata.first = B_TRUE;
4487ecd6cf80Smarks 	/* check options */
4488ecd6cf80Smarks 	while ((c = getopt(argc, argv, "li")) != -1) {
4489ecd6cf80Smarks 		switch (c) {
4490ecd6cf80Smarks 		case 'l':
4491ecd6cf80Smarks 			cbdata.longfmt = 1;
4492ecd6cf80Smarks 			break;
4493ecd6cf80Smarks 		case 'i':
4494ecd6cf80Smarks 			cbdata.internal = 1;
4495ecd6cf80Smarks 			break;
4496ecd6cf80Smarks 		case '?':
4497ecd6cf80Smarks 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4498ecd6cf80Smarks 			    optopt);
4499ecd6cf80Smarks 			usage(B_FALSE);
4500ecd6cf80Smarks 		}
4501ecd6cf80Smarks 	}
450206eeb2adSek110237 	argc -= optind;
450306eeb2adSek110237 	argv += optind;
450406eeb2adSek110237 
4505b1b8ab34Slling 	ret = for_each_pool(argc, argv, B_FALSE,  NULL, get_history_one,
4506ecd6cf80Smarks 	    &cbdata);
450706eeb2adSek110237 
4508ecd6cf80Smarks 	if (argc == 0 && cbdata.first == B_TRUE) {
450906eeb2adSek110237 		(void) printf(gettext("no pools available\n"));
451006eeb2adSek110237 		return (0);
4511eaca9bbdSeschrock 	}
4512eaca9bbdSeschrock 
4513eaca9bbdSeschrock 	return (ret);
4514eaca9bbdSeschrock }
4515eaca9bbdSeschrock 
4516b1b8ab34Slling static int
4517b1b8ab34Slling get_callback(zpool_handle_t *zhp, void *data)
4518b1b8ab34Slling {
4519990b4856Slling 	zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
4520b1b8ab34Slling 	char value[MAXNAMELEN];
4521990b4856Slling 	zprop_source_t srctype;
4522990b4856Slling 	zprop_list_t *pl;
4523b1b8ab34Slling 
4524b1b8ab34Slling 	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
4525b1b8ab34Slling 
4526b1b8ab34Slling 		/*
4527990b4856Slling 		 * Skip the special fake placeholder. This will also skip
4528990b4856Slling 		 * over the name property when 'all' is specified.
4529b1b8ab34Slling 		 */
4530990b4856Slling 		if (pl->pl_prop == ZPOOL_PROP_NAME &&
4531b1b8ab34Slling 		    pl == cbp->cb_proplist)
4532b1b8ab34Slling 			continue;
4533b1b8ab34Slling 
4534b1b8ab34Slling 		if (zpool_get_prop(zhp, pl->pl_prop,
4535b1b8ab34Slling 		    value, sizeof (value), &srctype) != 0)
4536b1b8ab34Slling 			continue;
4537b1b8ab34Slling 
4538990b4856Slling 		zprop_print_one_property(zpool_get_name(zhp), cbp,
453992241e0bSTom Erickson 		    zpool_prop_to_name(pl->pl_prop), value, srctype, NULL,
454092241e0bSTom Erickson 		    NULL);
4541b1b8ab34Slling 	}
4542b1b8ab34Slling 	return (0);
4543b1b8ab34Slling }
4544b1b8ab34Slling 
4545b1b8ab34Slling int
4546b1b8ab34Slling zpool_do_get(int argc, char **argv)
4547b1b8ab34Slling {
4548990b4856Slling 	zprop_get_cbdata_t cb = { 0 };
4549990b4856Slling 	zprop_list_t fake_name = { 0 };
4550b1b8ab34Slling 	int ret;
4551b1b8ab34Slling 
4552b1b8ab34Slling 	if (argc < 3)
4553b1b8ab34Slling 		usage(B_FALSE);
4554b1b8ab34Slling 
4555b1b8ab34Slling 	cb.cb_first = B_TRUE;
4556990b4856Slling 	cb.cb_sources = ZPROP_SRC_ALL;
4557b1b8ab34Slling 	cb.cb_columns[0] = GET_COL_NAME;
4558b1b8ab34Slling 	cb.cb_columns[1] = GET_COL_PROPERTY;
4559b1b8ab34Slling 	cb.cb_columns[2] = GET_COL_VALUE;
4560b1b8ab34Slling 	cb.cb_columns[3] = GET_COL_SOURCE;
4561990b4856Slling 	cb.cb_type = ZFS_TYPE_POOL;
4562b1b8ab34Slling 
4563990b4856Slling 	if (zprop_get_list(g_zfs, argv[1],  &cb.cb_proplist,
4564990b4856Slling 	    ZFS_TYPE_POOL) != 0)
4565b1b8ab34Slling 		usage(B_FALSE);
4566b1b8ab34Slling 
4567b1b8ab34Slling 	if (cb.cb_proplist != NULL) {
4568990b4856Slling 		fake_name.pl_prop = ZPOOL_PROP_NAME;
4569b1b8ab34Slling 		fake_name.pl_width = strlen(gettext("NAME"));
4570b1b8ab34Slling 		fake_name.pl_next = cb.cb_proplist;
4571b1b8ab34Slling 		cb.cb_proplist = &fake_name;
4572b1b8ab34Slling 	}
4573b1b8ab34Slling 
4574b1b8ab34Slling 	ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
4575b1b8ab34Slling 	    get_callback, &cb);
4576b1b8ab34Slling 
4577b1b8ab34Slling 	if (cb.cb_proplist == &fake_name)
4578990b4856Slling 		zprop_free_list(fake_name.pl_next);
4579b1b8ab34Slling 	else
4580990b4856Slling 		zprop_free_list(cb.cb_proplist);
4581b1b8ab34Slling 
4582b1b8ab34Slling 	return (ret);
4583b1b8ab34Slling }
4584b1b8ab34Slling 
4585b1b8ab34Slling typedef struct set_cbdata {
4586b1b8ab34Slling 	char *cb_propname;
4587b1b8ab34Slling 	char *cb_value;
4588b1b8ab34Slling 	boolean_t cb_any_successful;
4589b1b8ab34Slling } set_cbdata_t;
4590b1b8ab34Slling 
4591b1b8ab34Slling int
4592b1b8ab34Slling set_callback(zpool_handle_t *zhp, void *data)
4593b1b8ab34Slling {
4594b1b8ab34Slling 	int error;
4595b1b8ab34Slling 	set_cbdata_t *cb = (set_cbdata_t *)data;
4596b1b8ab34Slling 
4597b1b8ab34Slling 	error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
4598b1b8ab34Slling 
4599b1b8ab34Slling 	if (!error)
4600b1b8ab34Slling 		cb->cb_any_successful = B_TRUE;
4601b1b8ab34Slling 
4602b1b8ab34Slling 	return (error);
4603b1b8ab34Slling }
4604b1b8ab34Slling 
4605b1b8ab34Slling int
4606b1b8ab34Slling zpool_do_set(int argc, char **argv)
4607b1b8ab34Slling {
4608b1b8ab34Slling 	set_cbdata_t cb = { 0 };
4609b1b8ab34Slling 	int error;
4610b1b8ab34Slling 
4611b1b8ab34Slling 	if (argc > 1 && argv[1][0] == '-') {
4612b1b8ab34Slling 		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4613b1b8ab34Slling 		    argv[1][1]);
4614b1b8ab34Slling 		usage(B_FALSE);
4615b1b8ab34Slling 	}
4616b1b8ab34Slling 
4617b1b8ab34Slling 	if (argc < 2) {
4618b1b8ab34Slling 		(void) fprintf(stderr, gettext("missing property=value "
4619b1b8ab34Slling 		    "argument\n"));
4620b1b8ab34Slling 		usage(B_FALSE);
4621b1b8ab34Slling 	}
4622b1b8ab34Slling 
4623b1b8ab34Slling 	if (argc < 3) {
4624b1b8ab34Slling 		(void) fprintf(stderr, gettext("missing pool name\n"));
4625b1b8ab34Slling 		usage(B_FALSE);
4626b1b8ab34Slling 	}
4627b1b8ab34Slling 
4628b1b8ab34Slling 	if (argc > 3) {
4629b1b8ab34Slling 		(void) fprintf(stderr, gettext("too many pool names\n"));
4630b1b8ab34Slling 		usage(B_FALSE);
4631b1b8ab34Slling 	}
4632b1b8ab34Slling 
4633b1b8ab34Slling 	cb.cb_propname = argv[1];
4634b1b8ab34Slling 	cb.cb_value = strchr(cb.cb_propname, '=');
4635b1b8ab34Slling 	if (cb.cb_value == NULL) {
4636b1b8ab34Slling 		(void) fprintf(stderr, gettext("missing value in "
4637b1b8ab34Slling 		    "property=value argument\n"));
4638b1b8ab34Slling 		usage(B_FALSE);
4639b1b8ab34Slling 	}
4640b1b8ab34Slling 
4641b1b8ab34Slling 	*(cb.cb_value) = '\0';
4642b1b8ab34Slling 	cb.cb_value++;
4643b1b8ab34Slling 
4644b1b8ab34Slling 	error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
4645b1b8ab34Slling 	    set_callback, &cb);
4646b1b8ab34Slling 
4647b1b8ab34Slling 	return (error);
4648b1b8ab34Slling }
4649b1b8ab34Slling 
4650b1b8ab34Slling static int
4651b1b8ab34Slling find_command_idx(char *command, int *idx)
4652b1b8ab34Slling {
4653b1b8ab34Slling 	int i;
4654b1b8ab34Slling 
4655b1b8ab34Slling 	for (i = 0; i < NCOMMAND; i++) {
4656b1b8ab34Slling 		if (command_table[i].name == NULL)
4657b1b8ab34Slling 			continue;
4658b1b8ab34Slling 
4659b1b8ab34Slling 		if (strcmp(command, command_table[i].name) == 0) {
4660b1b8ab34Slling 			*idx = i;
4661b1b8ab34Slling 			return (0);
4662b1b8ab34Slling 		}
4663b1b8ab34Slling 	}
4664b1b8ab34Slling 	return (1);
4665b1b8ab34Slling }
4666b1b8ab34Slling 
4667fa9e4066Sahrens int
4668fa9e4066Sahrens main(int argc, char **argv)
4669fa9e4066Sahrens {
4670fa9e4066Sahrens 	int ret;
4671fa9e4066Sahrens 	int i;
4672fa9e4066Sahrens 	char *cmdname;
4673fa9e4066Sahrens 
4674fa9e4066Sahrens 	(void) setlocale(LC_ALL, "");
4675fa9e4066Sahrens 	(void) textdomain(TEXT_DOMAIN);
4676fa9e4066Sahrens 
467799653d4eSeschrock 	if ((g_zfs = libzfs_init()) == NULL) {
467899653d4eSeschrock 		(void) fprintf(stderr, gettext("internal error: failed to "
4679203a47d8Snd150628 		    "initialize ZFS library\n"));
468099653d4eSeschrock 		return (1);
468199653d4eSeschrock 	}
468299653d4eSeschrock 
468399653d4eSeschrock 	libzfs_print_on_error(g_zfs, B_TRUE);
468499653d4eSeschrock 
4685fa9e4066Sahrens 	opterr = 0;
4686fa9e4066Sahrens 
4687fa9e4066Sahrens 	/*
4688fa9e4066Sahrens 	 * Make sure the user has specified some command.
4689fa9e4066Sahrens 	 */
4690fa9e4066Sahrens 	if (argc < 2) {
4691fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing command\n"));
469299653d4eSeschrock 		usage(B_FALSE);
4693fa9e4066Sahrens 	}
4694fa9e4066Sahrens 
4695fa9e4066Sahrens 	cmdname = argv[1];
4696fa9e4066Sahrens 
4697fa9e4066Sahrens 	/*
4698fa9e4066Sahrens 	 * Special case '-?'
4699fa9e4066Sahrens 	 */
4700fa9e4066Sahrens 	if (strcmp(cmdname, "-?") == 0)
470199653d4eSeschrock 		usage(B_TRUE);
4702fa9e4066Sahrens 
47032a6b87f0Sek110237 	zpool_set_history_str("zpool", argc, argv, history_str);
47042a6b87f0Sek110237 	verify(zpool_stage_history(g_zfs, history_str) == 0);
47052a6b87f0Sek110237 
4706fa9e4066Sahrens 	/*
4707fa9e4066Sahrens 	 * Run the appropriate command.
4708fa9e4066Sahrens 	 */
4709b1b8ab34Slling 	if (find_command_idx(cmdname, &i) == 0) {
4710fa9e4066Sahrens 		current_command = &command_table[i];
4711fa9e4066Sahrens 		ret = command_table[i].func(argc - 1, argv + 1);
471291ebeef5Sahrens 	} else if (strchr(cmdname, '=')) {
471391ebeef5Sahrens 		verify(find_command_idx("set", &i) == 0);
471491ebeef5Sahrens 		current_command = &command_table[i];
471591ebeef5Sahrens 		ret = command_table[i].func(argc, argv);
471691ebeef5Sahrens 	} else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
4717fa9e4066Sahrens 		/*
471891ebeef5Sahrens 		 * 'freeze' is a vile debugging abomination, so we treat
471991ebeef5Sahrens 		 * it as such.
4720fa9e4066Sahrens 		 */
4721ea8dc4b6Seschrock 		char buf[16384];
4722ea8dc4b6Seschrock 		int fd = open(ZFS_DEV, O_RDWR);
4723fa9e4066Sahrens 		(void) strcpy((void *)buf, argv[2]);
4724fa9e4066Sahrens 		return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
472591ebeef5Sahrens 	} else {
4726fa9e4066Sahrens 		(void) fprintf(stderr, gettext("unrecognized "
4727fa9e4066Sahrens 		    "command '%s'\n"), cmdname);
472899653d4eSeschrock 		usage(B_FALSE);
4729fa9e4066Sahrens 	}
4730fa9e4066Sahrens 
473199653d4eSeschrock 	libzfs_fini(g_zfs);
473299653d4eSeschrock 
4733fa9e4066Sahrens 	/*
4734fa9e4066Sahrens 	 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4735fa9e4066Sahrens 	 * for the purposes of running ::findleaks.
4736fa9e4066Sahrens 	 */
4737fa9e4066Sahrens 	if (getenv("ZFS_ABORT") != NULL) {
4738fa9e4066Sahrens 		(void) printf("dumping core by request\n");
4739fa9e4066Sahrens 		abort();
4740fa9e4066Sahrens 	}
4741fa9e4066Sahrens 
4742fa9e4066Sahrens 	return (ret);
4743fa9e4066Sahrens }
4744