xref: /illumos-gate/usr/src/cmd/zpool/zpool_main.c (revision e0f1c0afa46cc84d4b1e40124032a9a87310386e)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5441d80aaSlling  * Common Development and Distribution License (the "License").
6441d80aaSlling  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
2199653d4eSeschrock 
22fa9e4066Sahrens /*
233f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
245cabbc6bSPrashanth Sreenivasa  * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
25e1d5e507SFrederik Wessels  * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
269edf9ebdSPrasad Joshi  * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
27b327cd3fSIgor Kozhukhov  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
286401734dSWill Andrews  * Copyright 2016 Nexenta Systems, Inc.
291702cce7SAlek Pinchuk  * Copyright (c) 2017 Datto Inc.
30fa9e4066Sahrens  */
31fa9e4066Sahrens 
32fa9e4066Sahrens #include <assert.h>
33fa9e4066Sahrens #include <ctype.h>
34fa9e4066Sahrens #include <dirent.h>
35fa9e4066Sahrens #include <errno.h>
36fa9e4066Sahrens #include <fcntl.h>
3786714001SSerapheim Dimitropoulos #include <getopt.h>
38fa9e4066Sahrens #include <libgen.h>
39fa9e4066Sahrens #include <libintl.h>
40fa9e4066Sahrens #include <libuutil.h>
41fa9e4066Sahrens #include <locale.h>
42fa9e4066Sahrens #include <stdio.h>
43fa9e4066Sahrens #include <stdlib.h>
44fa9e4066Sahrens #include <string.h>
45fa9e4066Sahrens #include <strings.h>
46fa9e4066Sahrens #include <unistd.h>
47fa9e4066Sahrens #include <priv.h>
48ecd6cf80Smarks #include <pwd.h>
49ecd6cf80Smarks #include <zone.h>
504263d13fSGeorge Wilson #include <zfs_prop.h>
51b1b8ab34Slling #include <sys/fs/zfs.h>
52fa9e4066Sahrens #include <sys/stat.h>
53*e0f1c0afSOlaf Faaland #include <sys/debug.h>
54fa9e4066Sahrens 
55fa9e4066Sahrens #include <libzfs.h>
56fa9e4066Sahrens 
57fa9e4066Sahrens #include "zpool_util.h"
58b7b97454Sperrin #include "zfs_comutil.h"
59ad135b5dSChristopher Siden #include "zfeature_common.h"
60fa9e4066Sahrens 
6126fd7700SKrishnendu Sadhukhan - Sun Microsystems #include "statcommon.h"
6226fd7700SKrishnendu Sadhukhan - Sun Microsystems 
63fa9e4066Sahrens static int zpool_do_create(int, char **);
64fa9e4066Sahrens static int zpool_do_destroy(int, char **);
65fa9e4066Sahrens 
66fa9e4066Sahrens static int zpool_do_add(int, char **);
6799653d4eSeschrock static int zpool_do_remove(int, char **);
686401734dSWill Andrews static int zpool_do_labelclear(int, char **);
69fa9e4066Sahrens 
7086714001SSerapheim Dimitropoulos static int zpool_do_checkpoint(int, char **);
7186714001SSerapheim Dimitropoulos 
72fa9e4066Sahrens static int zpool_do_list(int, char **);
73fa9e4066Sahrens static int zpool_do_iostat(int, char **);
74fa9e4066Sahrens static int zpool_do_status(int, char **);
75fa9e4066Sahrens 
76fa9e4066Sahrens static int zpool_do_online(int, char **);
77fa9e4066Sahrens static int zpool_do_offline(int, char **);
78ea8dc4b6Seschrock static int zpool_do_clear(int, char **);
794263d13fSGeorge Wilson static int zpool_do_reopen(int, char **);
80fa9e4066Sahrens 
81e9103aaeSGarrett D'Amore static int zpool_do_reguid(int, char **);
82e9103aaeSGarrett D'Amore 
83fa9e4066Sahrens static int zpool_do_attach(int, char **);
84fa9e4066Sahrens static int zpool_do_detach(int, char **);
85fa9e4066Sahrens static int zpool_do_replace(int, char **);
861195e687SMark J Musante static int zpool_do_split(int, char **);
87fa9e4066Sahrens 
88094e47e9SGeorge Wilson static int zpool_do_initialize(int, char **);
89fa9e4066Sahrens static int zpool_do_scrub(int, char **);
90fa9e4066Sahrens 
91fa9e4066Sahrens static int zpool_do_import(int, char **);
92fa9e4066Sahrens static int zpool_do_export(int, char **);
93fa9e4066Sahrens 
94eaca9bbdSeschrock static int zpool_do_upgrade(int, char **);
95eaca9bbdSeschrock 
9606eeb2adSek static int zpool_do_history(int, char **);
9706eeb2adSek 
98b1b8ab34Slling static int zpool_do_get(int, char **);
99b1b8ab34Slling static int zpool_do_set(int, char **);
100b1b8ab34Slling 
1019c2acf00SAlek Pinchuk static int zpool_do_sync(int, char **);
1029c2acf00SAlek Pinchuk 
103fa9e4066Sahrens /*
104fa9e4066Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
105fa9e4066Sahrens  * debugging facilities.
106fa9e4066Sahrens  */
10729ab75c9Srm 
10829ab75c9Srm #ifdef DEBUG
109fa9e4066Sahrens const char *
11099653d4eSeschrock _umem_debug_init(void)
111fa9e4066Sahrens {
112fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
113fa9e4066Sahrens }
114fa9e4066Sahrens 
115fa9e4066Sahrens const char *
116fa9e4066Sahrens _umem_logging_init(void)
117fa9e4066Sahrens {
118fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
119fa9e4066Sahrens }
12029ab75c9Srm #endif
121fa9e4066Sahrens 
12265cd9f28Seschrock typedef enum {
12365cd9f28Seschrock 	HELP_ADD,
12465cd9f28Seschrock 	HELP_ATTACH,
125ea8dc4b6Seschrock 	HELP_CLEAR,
12665cd9f28Seschrock 	HELP_CREATE,
12786714001SSerapheim Dimitropoulos 	HELP_CHECKPOINT,
12865cd9f28Seschrock 	HELP_DESTROY,
12965cd9f28Seschrock 	HELP_DETACH,
13065cd9f28Seschrock 	HELP_EXPORT,
13106eeb2adSek 	HELP_HISTORY,
13265cd9f28Seschrock 	HELP_IMPORT,
13365cd9f28Seschrock 	HELP_IOSTAT,
1346401734dSWill Andrews 	HELP_LABELCLEAR,
13565cd9f28Seschrock 	HELP_LIST,
13665cd9f28Seschrock 	HELP_OFFLINE,
13765cd9f28Seschrock 	HELP_ONLINE,
13865cd9f28Seschrock 	HELP_REPLACE,
13999653d4eSeschrock 	HELP_REMOVE,
140094e47e9SGeorge Wilson 	HELP_INITIALIZE,
14165cd9f28Seschrock 	HELP_SCRUB,
142eaca9bbdSeschrock 	HELP_STATUS,
143b1b8ab34Slling 	HELP_UPGRADE,
144b1b8ab34Slling 	HELP_GET,
1451195e687SMark J Musante 	HELP_SET,
146e9103aaeSGarrett D'Amore 	HELP_SPLIT,
1479c2acf00SAlek Pinchuk 	HELP_SYNC,
1484263d13fSGeorge Wilson 	HELP_REGUID,
1494263d13fSGeorge Wilson 	HELP_REOPEN
15065cd9f28Seschrock } zpool_help_t;
15165cd9f28Seschrock 
15265cd9f28Seschrock 
153fa9e4066Sahrens typedef struct zpool_command {
154fa9e4066Sahrens 	const char	*name;
155fa9e4066Sahrens 	int		(*func)(int, char **);
15665cd9f28Seschrock 	zpool_help_t	usage;
157fa9e4066Sahrens } zpool_command_t;
158fa9e4066Sahrens 
159fa9e4066Sahrens /*
160fa9e4066Sahrens  * Master command table.  Each ZFS command has a name, associated function, and
161ea8dc4b6Seschrock  * usage message.  The usage messages need to be internationalized, so we have
162ea8dc4b6Seschrock  * to have a function to return the usage message based on a command index.
16365cd9f28Seschrock  *
16465cd9f28Seschrock  * These commands are organized according to how they are displayed in the usage
16565cd9f28Seschrock  * message.  An empty command (one with a NULL name) indicates an empty line in
16665cd9f28Seschrock  * the generic usage message.
167fa9e4066Sahrens  */
168fa9e4066Sahrens static zpool_command_t command_table[] = {
16965cd9f28Seschrock 	{ "create",	zpool_do_create,	HELP_CREATE		},
17065cd9f28Seschrock 	{ "destroy",	zpool_do_destroy,	HELP_DESTROY		},
171fa9e4066Sahrens 	{ NULL },
17265cd9f28Seschrock 	{ "add",	zpool_do_add,		HELP_ADD		},
17399653d4eSeschrock 	{ "remove",	zpool_do_remove,	HELP_REMOVE		},
174fa9e4066Sahrens 	{ NULL },
1756401734dSWill Andrews 	{ "labelclear",	zpool_do_labelclear,	HELP_LABELCLEAR		},
1766401734dSWill Andrews 	{ NULL },
17786714001SSerapheim Dimitropoulos 	{ "checkpoint",	zpool_do_checkpoint,	HELP_CHECKPOINT		},
17886714001SSerapheim Dimitropoulos 	{ NULL },
17965cd9f28Seschrock 	{ "list",	zpool_do_list,		HELP_LIST		},
18065cd9f28Seschrock 	{ "iostat",	zpool_do_iostat,	HELP_IOSTAT		},
18165cd9f28Seschrock 	{ "status",	zpool_do_status,	HELP_STATUS		},
182fa9e4066Sahrens 	{ NULL },
18365cd9f28Seschrock 	{ "online",	zpool_do_online,	HELP_ONLINE		},
18465cd9f28Seschrock 	{ "offline",	zpool_do_offline,	HELP_OFFLINE		},
185ea8dc4b6Seschrock 	{ "clear",	zpool_do_clear,		HELP_CLEAR		},
1864263d13fSGeorge Wilson 	{ "reopen",	zpool_do_reopen,	HELP_REOPEN		},
187fa9e4066Sahrens 	{ NULL },
18865cd9f28Seschrock 	{ "attach",	zpool_do_attach,	HELP_ATTACH		},
18965cd9f28Seschrock 	{ "detach",	zpool_do_detach,	HELP_DETACH		},
19065cd9f28Seschrock 	{ "replace",	zpool_do_replace,	HELP_REPLACE		},
1911195e687SMark J Musante 	{ "split",	zpool_do_split,		HELP_SPLIT		},
192fa9e4066Sahrens 	{ NULL },
193094e47e9SGeorge Wilson 	{ "initialize",	zpool_do_initialize,	HELP_INITIALIZE		},
19465cd9f28Seschrock 	{ "scrub",	zpool_do_scrub,		HELP_SCRUB		},
195fa9e4066Sahrens 	{ NULL },
19665cd9f28Seschrock 	{ "import",	zpool_do_import,	HELP_IMPORT		},
19765cd9f28Seschrock 	{ "export",	zpool_do_export,	HELP_EXPORT		},
19806eeb2adSek 	{ "upgrade",	zpool_do_upgrade,	HELP_UPGRADE		},
199e9103aaeSGarrett D'Amore 	{ "reguid",	zpool_do_reguid,	HELP_REGUID		},
20006eeb2adSek 	{ NULL },
201b1b8ab34Slling 	{ "history",	zpool_do_history,	HELP_HISTORY		},
202b1b8ab34Slling 	{ "get",	zpool_do_get,		HELP_GET		},
203b1b8ab34Slling 	{ "set",	zpool_do_set,		HELP_SET		},
2049c2acf00SAlek Pinchuk 	{ "sync",	zpool_do_sync,		HELP_SYNC		},
205fa9e4066Sahrens };
206fa9e4066Sahrens 
207fa9e4066Sahrens #define	NCOMMAND	(sizeof (command_table) / sizeof (command_table[0]))
208fa9e4066Sahrens 
2094445fffbSMatthew Ahrens static zpool_command_t *current_command;
2102a6b87f0Sek static char history_str[HIS_MAX_RECORD_LEN];
2114445fffbSMatthew Ahrens static boolean_t log_history = B_TRUE;
21226fd7700SKrishnendu Sadhukhan - Sun Microsystems static uint_t timestamp_fmt = NODATE;
21326fd7700SKrishnendu Sadhukhan - Sun Microsystems 
21465cd9f28Seschrock static const char *
2159a686fbcSPaul Dagnelie get_usage(zpool_help_t idx)
2169a686fbcSPaul Dagnelie {
21765cd9f28Seschrock 	switch (idx) {
21865cd9f28Seschrock 	case HELP_ADD:
21965cd9f28Seschrock 		return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
22065cd9f28Seschrock 	case HELP_ATTACH:
22165cd9f28Seschrock 		return (gettext("\tattach [-f] <pool> <device> "
222e45ce728Sahrens 		    "<new-device>\n"));
223ea8dc4b6Seschrock 	case HELP_CLEAR:
224468c413aSTim Haley 		return (gettext("\tclear [-nF] <pool> [device]\n"));
22565cd9f28Seschrock 	case HELP_CREATE:
2267855d95bSToomas Soome 		return (gettext("\tcreate [-fnd] [-B] "
2277855d95bSToomas Soome 		    "[-o property=value] ... \n"
22804e56356SAndriy Gapon 		    "\t    [-O file-system-property=value] ...\n"
22904e56356SAndriy Gapon 		    "\t    [-m mountpoint] [-R root] [-t tempname] "
23004e56356SAndriy Gapon 		    "<pool> <vdev> ...\n"));
23186714001SSerapheim Dimitropoulos 	case HELP_CHECKPOINT:
23286714001SSerapheim Dimitropoulos 		return (gettext("\tcheckpoint [--discard] <pool> ...\n"));
23365cd9f28Seschrock 	case HELP_DESTROY:
23465cd9f28Seschrock 		return (gettext("\tdestroy [-f] <pool>\n"));
23565cd9f28Seschrock 	case HELP_DETACH:
23665cd9f28Seschrock 		return (gettext("\tdetach <pool> <device>\n"));
23765cd9f28Seschrock 	case HELP_EXPORT:
23865cd9f28Seschrock 		return (gettext("\texport [-f] <pool> ...\n"));
23906eeb2adSek 	case HELP_HISTORY:
240ecd6cf80Smarks 		return (gettext("\thistory [-il] [<pool>] ...\n"));
24165cd9f28Seschrock 	case HELP_IMPORT:
2424c58d714Sdarrenm 		return (gettext("\timport [-d dir] [-D]\n"
2432f8aaab3Seschrock 		    "\timport [-o mntopts] [-o property=value] ... \n"
244f9af39baSGeorge Wilson 		    "\t    [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
245f9af39baSGeorge Wilson 		    "[-R root] [-F [-n]] -a\n"
2462f8aaab3Seschrock 		    "\timport [-o mntopts] [-o property=value] ... \n"
247f9af39baSGeorge Wilson 		    "\t    [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
24804e56356SAndriy Gapon 		    "[-R root] [-F [-n]] [-t]\n"
24986714001SSerapheim Dimitropoulos 		    "\t    [--rewind-to-checkpoint] <pool | id> [newpool]\n"));
25065cd9f28Seschrock 	case HELP_IOSTAT:
25126fd7700SKrishnendu Sadhukhan - Sun Microsystems 		return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
25265cd9f28Seschrock 		    "[count]]\n"));
2536401734dSWill Andrews 	case HELP_LABELCLEAR:
2546401734dSWill Andrews 		return (gettext("\tlabelclear [-f] <vdev>\n"));
25565cd9f28Seschrock 	case HELP_LIST:
256c58b3526SAdam Stevko 		return (gettext("\tlist [-Hp] [-o property[,...]] "
2573f9d6ad7SLin Ling 		    "[-T d|u] [pool] ... [interval [count]]\n"));
25865cd9f28Seschrock 	case HELP_OFFLINE:
259441d80aaSlling 		return (gettext("\toffline [-t] <pool> <device> ...\n"));
26065cd9f28Seschrock 	case HELP_ONLINE:
261441d80aaSlling 		return (gettext("\tonline <pool> <device> ...\n"));
26265cd9f28Seschrock 	case HELP_REPLACE:
26365cd9f28Seschrock 		return (gettext("\treplace [-f] <pool> <device> "
264e45ce728Sahrens 		    "[new-device]\n"));
26599653d4eSeschrock 	case HELP_REMOVE:
2665cabbc6bSPrashanth Sreenivasa 		return (gettext("\tremove [-nps] <pool> <device> ...\n"));
2674263d13fSGeorge Wilson 	case HELP_REOPEN:
26831d7e8faSGeorge Wilson 		return (gettext("\treopen <pool>\n"));
269094e47e9SGeorge Wilson 	case HELP_INITIALIZE:
270094e47e9SGeorge Wilson 		return (gettext("\tinitialize [-cs] <pool> [<device> ...]\n"));
27165cd9f28Seschrock 	case HELP_SCRUB:
2721702cce7SAlek Pinchuk 		return (gettext("\tscrub [-s | -p] <pool> ...\n"));
27365cd9f28Seschrock 	case HELP_STATUS:
2743f9d6ad7SLin Ling 		return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
2753f9d6ad7SLin Ling 		    "[count]]\n"));
276eaca9bbdSeschrock 	case HELP_UPGRADE:
277eaca9bbdSeschrock 		return (gettext("\tupgrade\n"
278eaca9bbdSeschrock 		    "\tupgrade -v\n"
279990b4856Slling 		    "\tupgrade [-V version] <-a | pool ...>\n"));
280b1b8ab34Slling 	case HELP_GET:
281c58b3526SAdam Stevko 		return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
282c58b3526SAdam Stevko 		    "<\"all\" | property[,...]> <pool> ...\n"));
283b1b8ab34Slling 	case HELP_SET:
284b1b8ab34Slling 		return (gettext("\tset <property=value> <pool> \n"));
2851195e687SMark J Musante 	case HELP_SPLIT:
2861195e687SMark J Musante 		return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
2871195e687SMark J Musante 		    "\t    [-o property=value] <pool> <newpool> "
2881195e687SMark J Musante 		    "[<device> ...]\n"));
289e9103aaeSGarrett D'Amore 	case HELP_REGUID:
290e9103aaeSGarrett D'Amore 		return (gettext("\treguid <pool>\n"));
2919c2acf00SAlek Pinchuk 	case HELP_SYNC:
2929c2acf00SAlek Pinchuk 		return (gettext("\tsync [pool] ...\n"));
29365cd9f28Seschrock 	}
29465cd9f28Seschrock 
29565cd9f28Seschrock 	abort();
29665cd9f28Seschrock 	/* NOTREACHED */
29765cd9f28Seschrock }
29865cd9f28Seschrock 
299fa9e4066Sahrens 
300b1b8ab34Slling /*
301b1b8ab34Slling  * Callback routine that will print out a pool property value.
302b1b8ab34Slling  */
303990b4856Slling static int
304990b4856Slling print_prop_cb(int prop, void *cb)
305b1b8ab34Slling {
306b1b8ab34Slling 	FILE *fp = cb;
307b1b8ab34Slling 
308b24ab676SJeff Bonwick 	(void) fprintf(fp, "\t%-15s  ", zpool_prop_to_name(prop));
309b1b8ab34Slling 
310990b4856Slling 	if (zpool_prop_readonly(prop))
311990b4856Slling 		(void) fprintf(fp, "  NO   ");
312990b4856Slling 	else
313b24ab676SJeff Bonwick 		(void) fprintf(fp, " YES   ");
314990b4856Slling 
315b1b8ab34Slling 	if (zpool_prop_values(prop) == NULL)
316b1b8ab34Slling 		(void) fprintf(fp, "-\n");
317b1b8ab34Slling 	else
318b1b8ab34Slling 		(void) fprintf(fp, "%s\n", zpool_prop_values(prop));
319b1b8ab34Slling 
320990b4856Slling 	return (ZPROP_CONT);
321b1b8ab34Slling }
322b1b8ab34Slling 
323fa9e4066Sahrens /*
324fa9e4066Sahrens  * Display usage message.  If we're inside a command, display only the usage for
325fa9e4066Sahrens  * that command.  Otherwise, iterate over the entire command table and display
326fa9e4066Sahrens  * a complete usage message.
327fa9e4066Sahrens  */
328fa9e4066Sahrens void
32999653d4eSeschrock usage(boolean_t requested)
330fa9e4066Sahrens {
331fa9e4066Sahrens 	FILE *fp = requested ? stdout : stderr;
332fa9e4066Sahrens 
333fa9e4066Sahrens 	if (current_command == NULL) {
334fa9e4066Sahrens 		int i;
335fa9e4066Sahrens 
336fa9e4066Sahrens 		(void) fprintf(fp, gettext("usage: zpool command args ...\n"));
337fa9e4066Sahrens 		(void) fprintf(fp,
338fa9e4066Sahrens 		    gettext("where 'command' is one of the following:\n\n"));
339fa9e4066Sahrens 
340fa9e4066Sahrens 		for (i = 0; i < NCOMMAND; i++) {
341fa9e4066Sahrens 			if (command_table[i].name == NULL)
342fa9e4066Sahrens 				(void) fprintf(fp, "\n");
343fa9e4066Sahrens 			else
344fa9e4066Sahrens 				(void) fprintf(fp, "%s",
34565cd9f28Seschrock 				    get_usage(command_table[i].usage));
346fa9e4066Sahrens 		}
347fa9e4066Sahrens 	} else {
348fa9e4066Sahrens 		(void) fprintf(fp, gettext("usage:\n"));
34965cd9f28Seschrock 		(void) fprintf(fp, "%s", get_usage(current_command->usage));
350fa9e4066Sahrens 	}
351fa9e4066Sahrens 
352b1b8ab34Slling 	if (current_command != NULL &&
353b1b8ab34Slling 	    ((strcmp(current_command->name, "set") == 0) ||
354990b4856Slling 	    (strcmp(current_command->name, "get") == 0) ||
355990b4856Slling 	    (strcmp(current_command->name, "list") == 0))) {
356b1b8ab34Slling 
357b1b8ab34Slling 		(void) fprintf(fp,
358b1b8ab34Slling 		    gettext("\nthe following properties are supported:\n"));
359b1b8ab34Slling 
360b24ab676SJeff Bonwick 		(void) fprintf(fp, "\n\t%-15s  %s   %s\n\n",
361990b4856Slling 		    "PROPERTY", "EDIT", "VALUES");
362b1b8ab34Slling 
363b1b8ab34Slling 		/* Iterate over all properties */
364990b4856Slling 		(void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
365990b4856Slling 		    ZFS_TYPE_POOL);
366ad135b5dSChristopher Siden 
367ad135b5dSChristopher Siden 		(void) fprintf(fp, "\t%-15s   ", "feature@...");
368ad135b5dSChristopher Siden 		(void) fprintf(fp, "YES   disabled | enabled | active\n");
369ad135b5dSChristopher Siden 
370ad135b5dSChristopher Siden 		(void) fprintf(fp, gettext("\nThe feature@ properties must be "
371ad135b5dSChristopher Siden 		    "appended with a feature name.\nSee zpool-features(5).\n"));
372b1b8ab34Slling 	}
373b1b8ab34Slling 
374e9dbad6fSeschrock 	/*
375e9dbad6fSeschrock 	 * See comments at end of main().
376e9dbad6fSeschrock 	 */
377e9dbad6fSeschrock 	if (getenv("ZFS_ABORT") != NULL) {
378e9dbad6fSeschrock 		(void) printf("dumping core by request\n");
379e9dbad6fSeschrock 		abort();
380e9dbad6fSeschrock 	}
381e9dbad6fSeschrock 
382fa9e4066Sahrens 	exit(requested ? 0 : 2);
383fa9e4066Sahrens }
384fa9e4066Sahrens 
385fa9e4066Sahrens void
3868654d025Sperrin print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
3878654d025Sperrin     boolean_t print_logs)
388fa9e4066Sahrens {
389fa9e4066Sahrens 	nvlist_t **child;
390fa9e4066Sahrens 	uint_t c, children;
391afefbcddSeschrock 	char *vname;
392fa9e4066Sahrens 
393fa9e4066Sahrens 	if (name != NULL)
394fa9e4066Sahrens 		(void) printf("\t%*s%s\n", indent, "", name);
395fa9e4066Sahrens 
396fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
397fa9e4066Sahrens 	    &child, &children) != 0)
398fa9e4066Sahrens 		return;
399fa9e4066Sahrens 
400afefbcddSeschrock 	for (c = 0; c < children; c++) {
4018654d025Sperrin 		uint64_t is_log = B_FALSE;
4028654d025Sperrin 
4038654d025Sperrin 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
4048654d025Sperrin 		    &is_log);
4058654d025Sperrin 		if ((is_log && !print_logs) || (!is_log && print_logs))
4068654d025Sperrin 			continue;
4078654d025Sperrin 
40888ecc943SGeorge Wilson 		vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
4098654d025Sperrin 		print_vdev_tree(zhp, vname, child[c], indent + 2,
4108654d025Sperrin 		    B_FALSE);
411afefbcddSeschrock 		free(vname);
412afefbcddSeschrock 	}
413fa9e4066Sahrens }
414fa9e4066Sahrens 
41557221772SChristopher Siden static boolean_t
41657221772SChristopher Siden prop_list_contains_feature(nvlist_t *proplist)
41757221772SChristopher Siden {
41857221772SChristopher Siden 	nvpair_t *nvp;
41957221772SChristopher Siden 	for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
42057221772SChristopher Siden 	    nvp = nvlist_next_nvpair(proplist, nvp)) {
42157221772SChristopher Siden 		if (zpool_prop_feature(nvpair_name(nvp)))
42257221772SChristopher Siden 			return (B_TRUE);
42357221772SChristopher Siden 	}
42457221772SChristopher Siden 	return (B_FALSE);
42557221772SChristopher Siden }
42657221772SChristopher Siden 
427990b4856Slling /*
428990b4856Slling  * Add a property pair (name, string-value) into a property nvlist.
429990b4856Slling  */
430990b4856Slling static int
4310a48a24eStimh add_prop_list(const char *propname, char *propval, nvlist_t **props,
4320a48a24eStimh     boolean_t poolprop)
433990b4856Slling {
434d2aa06e8SAndrew Stormont 	zpool_prop_t prop = ZPOOL_PROP_INVAL;
4350a48a24eStimh 	zfs_prop_t fprop;
436990b4856Slling 	nvlist_t *proplist;
4370a48a24eStimh 	const char *normnm;
4380a48a24eStimh 	char *strval;
439990b4856Slling 
440990b4856Slling 	if (*props == NULL &&
441990b4856Slling 	    nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
442990b4856Slling 		(void) fprintf(stderr,
443990b4856Slling 		    gettext("internal error: out of memory\n"));
444990b4856Slling 		return (1);
445990b4856Slling 	}
446990b4856Slling 
447990b4856Slling 	proplist = *props;
448990b4856Slling 
4490a48a24eStimh 	if (poolprop) {
45057221772SChristopher Siden 		const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
45157221772SChristopher Siden 
452d2aa06e8SAndrew Stormont 		if ((prop = zpool_name_to_prop(propname)) == ZPOOL_PROP_INVAL &&
453ad135b5dSChristopher Siden 		    !zpool_prop_feature(propname)) {
4540a48a24eStimh 			(void) fprintf(stderr, gettext("property '%s' is "
4550a48a24eStimh 			    "not a valid pool property\n"), propname);
4560a48a24eStimh 			return (2);
4570a48a24eStimh 		}
45857221772SChristopher Siden 
45957221772SChristopher Siden 		/*
46057221772SChristopher Siden 		 * feature@ properties and version should not be specified
46157221772SChristopher Siden 		 * at the same time.
46257221772SChristopher Siden 		 */
4634ae5f5f0SAlan Somers 		if ((prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname) &&
46457221772SChristopher Siden 		    nvlist_exists(proplist, vname)) ||
46557221772SChristopher Siden 		    (prop == ZPOOL_PROP_VERSION &&
46657221772SChristopher Siden 		    prop_list_contains_feature(proplist))) {
46757221772SChristopher Siden 			(void) fprintf(stderr, gettext("'feature@' and "
46857221772SChristopher Siden 			    "'version' properties cannot be specified "
46957221772SChristopher Siden 			    "together\n"));
47057221772SChristopher Siden 			return (2);
47157221772SChristopher Siden 		}
47257221772SChristopher Siden 
47357221772SChristopher Siden 
474ad135b5dSChristopher Siden 		if (zpool_prop_feature(propname))
475ad135b5dSChristopher Siden 			normnm = propname;
476ad135b5dSChristopher Siden 		else
477ad135b5dSChristopher Siden 			normnm = zpool_prop_to_name(prop);
4780a48a24eStimh 	} else {
47914843421SMatthew Ahrens 		if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
48014843421SMatthew Ahrens 			normnm = zfs_prop_to_name(fprop);
48114843421SMatthew Ahrens 		} else {
48214843421SMatthew Ahrens 			normnm = propname;
4830a48a24eStimh 		}
484990b4856Slling 	}
485990b4856Slling 
4860a48a24eStimh 	if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
4870a48a24eStimh 	    prop != ZPOOL_PROP_CACHEFILE) {
488990b4856Slling 		(void) fprintf(stderr, gettext("property '%s' "
489990b4856Slling 		    "specified multiple times\n"), propname);
490990b4856Slling 		return (2);
491990b4856Slling 	}
492990b4856Slling 
4930a48a24eStimh 	if (nvlist_add_string(proplist, normnm, propval) != 0) {
494990b4856Slling 		(void) fprintf(stderr, gettext("internal "
495990b4856Slling 		    "error: out of memory\n"));
496990b4856Slling 		return (1);
497990b4856Slling 	}
498990b4856Slling 
499990b4856Slling 	return (0);
500990b4856Slling }
501990b4856Slling 
50204e56356SAndriy Gapon /*
50304e56356SAndriy Gapon  * Set a default property pair (name, string-value) in a property nvlist
50404e56356SAndriy Gapon  */
50504e56356SAndriy Gapon static int
50604e56356SAndriy Gapon add_prop_list_default(const char *propname, char *propval, nvlist_t **props,
50704e56356SAndriy Gapon     boolean_t poolprop)
50804e56356SAndriy Gapon {
50904e56356SAndriy Gapon 	char *pval;
51004e56356SAndriy Gapon 
51104e56356SAndriy Gapon 	if (nvlist_lookup_string(*props, propname, &pval) == 0)
51204e56356SAndriy Gapon 		return (0);
51304e56356SAndriy Gapon 
51404e56356SAndriy Gapon 	return (add_prop_list(propname, propval, props, poolprop));
51504e56356SAndriy Gapon }
51604e56356SAndriy Gapon 
517fa9e4066Sahrens /*
518fa9e4066Sahrens  * zpool add [-fn] <pool> <vdev> ...
519fa9e4066Sahrens  *
520fa9e4066Sahrens  *	-f	Force addition of devices, even if they appear in use
521fa9e4066Sahrens  *	-n	Do not add the devices, but display the resulting layout if
522fa9e4066Sahrens  *		they were to be added.
523fa9e4066Sahrens  *
524fa9e4066Sahrens  * Adds the given vdevs to 'pool'.  As with create, the bulk of this work is
525fa9e4066Sahrens  * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
526fa9e4066Sahrens  * libzfs.
527fa9e4066Sahrens  */
528fa9e4066Sahrens int
529fa9e4066Sahrens zpool_do_add(int argc, char **argv)
530fa9e4066Sahrens {
53199653d4eSeschrock 	boolean_t force = B_FALSE;
53299653d4eSeschrock 	boolean_t dryrun = B_FALSE;
533fa9e4066Sahrens 	int c;
534fa9e4066Sahrens 	nvlist_t *nvroot;
535fa9e4066Sahrens 	char *poolname;
5367855d95bSToomas Soome 	zpool_boot_label_t boot_type;
5377855d95bSToomas Soome 	uint64_t boot_size;
538fa9e4066Sahrens 	int ret;
539fa9e4066Sahrens 	zpool_handle_t *zhp;
540fa9e4066Sahrens 	nvlist_t *config;
541fa9e4066Sahrens 
542fa9e4066Sahrens 	/* check options */
543fa9e4066Sahrens 	while ((c = getopt(argc, argv, "fn")) != -1) {
544fa9e4066Sahrens 		switch (c) {
545fa9e4066Sahrens 		case 'f':
54699653d4eSeschrock 			force = B_TRUE;
547fa9e4066Sahrens 			break;
548fa9e4066Sahrens 		case 'n':
54999653d4eSeschrock 			dryrun = B_TRUE;
550fa9e4066Sahrens 			break;
551fa9e4066Sahrens 		case '?':
552fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
553fa9e4066Sahrens 			    optopt);
55499653d4eSeschrock 			usage(B_FALSE);
555fa9e4066Sahrens 		}
556fa9e4066Sahrens 	}
557fa9e4066Sahrens 
558fa9e4066Sahrens 	argc -= optind;
559fa9e4066Sahrens 	argv += optind;
560fa9e4066Sahrens 
561fa9e4066Sahrens 	/* get pool name and check number of arguments */
562fa9e4066Sahrens 	if (argc < 1) {
563fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
56499653d4eSeschrock 		usage(B_FALSE);
565fa9e4066Sahrens 	}
566fa9e4066Sahrens 	if (argc < 2) {
567fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing vdev specification\n"));
56899653d4eSeschrock 		usage(B_FALSE);
569fa9e4066Sahrens 	}
570fa9e4066Sahrens 
571fa9e4066Sahrens 	poolname = argv[0];
572fa9e4066Sahrens 
573fa9e4066Sahrens 	argc--;
574fa9e4066Sahrens 	argv++;
575fa9e4066Sahrens 
57699653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
577fa9e4066Sahrens 		return (1);
578fa9e4066Sahrens 
579088e9d47Seschrock 	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
580fa9e4066Sahrens 		(void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
581fa9e4066Sahrens 		    poolname);
582fa9e4066Sahrens 		zpool_close(zhp);
583fa9e4066Sahrens 		return (1);
584fa9e4066Sahrens 	}
585fa9e4066Sahrens 
5867855d95bSToomas Soome 	if (zpool_is_bootable(zhp))
5877855d95bSToomas Soome 		boot_type = ZPOOL_COPY_BOOT_LABEL;
5887855d95bSToomas Soome 	else
5897855d95bSToomas Soome 		boot_type = ZPOOL_NO_BOOT_LABEL;
5907855d95bSToomas Soome 
591fa9e4066Sahrens 	/* pass off to get_vdev_spec for processing */
5927855d95bSToomas Soome 	boot_size = zpool_get_prop_int(zhp, ZPOOL_PROP_BOOTSIZE, NULL);
593705040edSEric Taylor 	nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
5947855d95bSToomas Soome 	    boot_type, boot_size, argc, argv);
595fa9e4066Sahrens 	if (nvroot == NULL) {
596fa9e4066Sahrens 		zpool_close(zhp);
597fa9e4066Sahrens 		return (1);
598fa9e4066Sahrens 	}
599fa9e4066Sahrens 
600fa9e4066Sahrens 	if (dryrun) {
601fa9e4066Sahrens 		nvlist_t *poolnvroot;
602fa9e4066Sahrens 
603fa9e4066Sahrens 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
604fa9e4066Sahrens 		    &poolnvroot) == 0);
605fa9e4066Sahrens 
606fa9e4066Sahrens 		(void) printf(gettext("would update '%s' to the following "
607fa9e4066Sahrens 		    "configuration:\n"), zpool_get_name(zhp));
608fa9e4066Sahrens 
6098654d025Sperrin 		/* print original main pool and new tree */
6108654d025Sperrin 		print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
6118654d025Sperrin 		print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
6128654d025Sperrin 
6138654d025Sperrin 		/* Do the same for the logs */
6148654d025Sperrin 		if (num_logs(poolnvroot) > 0) {
6158654d025Sperrin 			print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
6168654d025Sperrin 			print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
6178654d025Sperrin 		} else if (num_logs(nvroot) > 0) {
6188654d025Sperrin 			print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
6198654d025Sperrin 		}
620fa9e4066Sahrens 
621fa9e4066Sahrens 		ret = 0;
622fa9e4066Sahrens 	} else {
623fa9e4066Sahrens 		ret = (zpool_add(zhp, nvroot) != 0);
624fa9e4066Sahrens 	}
625fa9e4066Sahrens 
62699653d4eSeschrock 	nvlist_free(nvroot);
62799653d4eSeschrock 	zpool_close(zhp);
62899653d4eSeschrock 
62999653d4eSeschrock 	return (ret);
63099653d4eSeschrock }
63199653d4eSeschrock 
63299653d4eSeschrock /*
6333f9d6ad7SLin Ling  * zpool remove  <pool> <vdev> ...
63499653d4eSeschrock  *
6355cabbc6bSPrashanth Sreenivasa  * Removes the given vdev from the pool.
63699653d4eSeschrock  */
63799653d4eSeschrock int
63899653d4eSeschrock zpool_do_remove(int argc, char **argv)
63999653d4eSeschrock {
64099653d4eSeschrock 	char *poolname;
641fa94a07fSbrendan 	int i, ret = 0;
64299653d4eSeschrock 	zpool_handle_t *zhp;
6435cabbc6bSPrashanth Sreenivasa 	boolean_t stop = B_FALSE;
6445cabbc6bSPrashanth Sreenivasa 	boolean_t noop = B_FALSE;
6455cabbc6bSPrashanth Sreenivasa 	boolean_t parsable = B_FALSE;
6465cabbc6bSPrashanth Sreenivasa 	char c;
64799653d4eSeschrock 
6485cabbc6bSPrashanth Sreenivasa 	/* check options */
6495cabbc6bSPrashanth Sreenivasa 	while ((c = getopt(argc, argv, "nps")) != -1) {
6505cabbc6bSPrashanth Sreenivasa 		switch (c) {
6515cabbc6bSPrashanth Sreenivasa 		case 'n':
6525cabbc6bSPrashanth Sreenivasa 			noop = B_TRUE;
6535cabbc6bSPrashanth Sreenivasa 			break;
6545cabbc6bSPrashanth Sreenivasa 		case 'p':
6555cabbc6bSPrashanth Sreenivasa 			parsable = B_TRUE;
6565cabbc6bSPrashanth Sreenivasa 			break;
6575cabbc6bSPrashanth Sreenivasa 		case 's':
6585cabbc6bSPrashanth Sreenivasa 			stop = B_TRUE;
6595cabbc6bSPrashanth Sreenivasa 			break;
6605cabbc6bSPrashanth Sreenivasa 		case '?':
6615cabbc6bSPrashanth Sreenivasa 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
6625cabbc6bSPrashanth Sreenivasa 			    optopt);
6635cabbc6bSPrashanth Sreenivasa 			usage(B_FALSE);
6645cabbc6bSPrashanth Sreenivasa 		}
6655cabbc6bSPrashanth Sreenivasa 	}
6665cabbc6bSPrashanth Sreenivasa 
6675cabbc6bSPrashanth Sreenivasa 	argc -= optind;
6685cabbc6bSPrashanth Sreenivasa 	argv += optind;
66999653d4eSeschrock 
67099653d4eSeschrock 	/* get pool name and check number of arguments */
67199653d4eSeschrock 	if (argc < 1) {
67299653d4eSeschrock 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
67399653d4eSeschrock 		usage(B_FALSE);
67499653d4eSeschrock 	}
67599653d4eSeschrock 
67699653d4eSeschrock 	poolname = argv[0];
67799653d4eSeschrock 
67899653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
67999653d4eSeschrock 		return (1);
68099653d4eSeschrock 
6815cabbc6bSPrashanth Sreenivasa 	if (stop && noop) {
6825cabbc6bSPrashanth Sreenivasa 		(void) fprintf(stderr, gettext("stop request ignored\n"));
6835cabbc6bSPrashanth Sreenivasa 		return (0);
6845cabbc6bSPrashanth Sreenivasa 	}
6855cabbc6bSPrashanth Sreenivasa 
6865cabbc6bSPrashanth Sreenivasa 	if (stop) {
6875cabbc6bSPrashanth Sreenivasa 		if (argc > 1) {
6885cabbc6bSPrashanth Sreenivasa 			(void) fprintf(stderr, gettext("too many arguments\n"));
6895cabbc6bSPrashanth Sreenivasa 			usage(B_FALSE);
6905cabbc6bSPrashanth Sreenivasa 		}
6915cabbc6bSPrashanth Sreenivasa 		if (zpool_vdev_remove_cancel(zhp) != 0)
692fa94a07fSbrendan 			ret = 1;
6935cabbc6bSPrashanth Sreenivasa 	} else {
6945cabbc6bSPrashanth Sreenivasa 		if (argc < 2) {
6955cabbc6bSPrashanth Sreenivasa 			(void) fprintf(stderr, gettext("missing device\n"));
6965cabbc6bSPrashanth Sreenivasa 			usage(B_FALSE);
6975cabbc6bSPrashanth Sreenivasa 		}
6985cabbc6bSPrashanth Sreenivasa 
6995cabbc6bSPrashanth Sreenivasa 		for (i = 1; i < argc; i++) {
7005cabbc6bSPrashanth Sreenivasa 			if (noop) {
7015cabbc6bSPrashanth Sreenivasa 				uint64_t size;
7025cabbc6bSPrashanth Sreenivasa 
7035cabbc6bSPrashanth Sreenivasa 				if (zpool_vdev_indirect_size(zhp, argv[i],
7045cabbc6bSPrashanth Sreenivasa 				    &size) != 0) {
7055cabbc6bSPrashanth Sreenivasa 					ret = 1;
7065cabbc6bSPrashanth Sreenivasa 					break;
7075cabbc6bSPrashanth Sreenivasa 				}
7085cabbc6bSPrashanth Sreenivasa 				if (parsable) {
7095cabbc6bSPrashanth Sreenivasa 					(void) printf("%s %llu\n",
7105cabbc6bSPrashanth Sreenivasa 					    argv[i], size);
7115cabbc6bSPrashanth Sreenivasa 				} else {
7125cabbc6bSPrashanth Sreenivasa 					char valstr[32];
7135cabbc6bSPrashanth Sreenivasa 					zfs_nicenum(size, valstr,
7145cabbc6bSPrashanth Sreenivasa 					    sizeof (valstr));
7155cabbc6bSPrashanth Sreenivasa 					(void) printf("Memory that will be "
7165cabbc6bSPrashanth Sreenivasa 					    "used after removing %s: %s\n",
7175cabbc6bSPrashanth Sreenivasa 					    argv[i], valstr);
7185cabbc6bSPrashanth Sreenivasa 				}
7195cabbc6bSPrashanth Sreenivasa 			} else {
7205cabbc6bSPrashanth Sreenivasa 				if (zpool_vdev_remove(zhp, argv[i]) != 0)
7215cabbc6bSPrashanth Sreenivasa 					ret = 1;
7225cabbc6bSPrashanth Sreenivasa 			}
7235cabbc6bSPrashanth Sreenivasa 		}
724fa94a07fSbrendan 	}
72599653d4eSeschrock 
726fa9e4066Sahrens 	return (ret);
727fa9e4066Sahrens }
728fa9e4066Sahrens 
7296401734dSWill Andrews /*
7306401734dSWill Andrews  * zpool labelclear [-f] <vdev>
7316401734dSWill Andrews  *
7326401734dSWill Andrews  *	-f	Force clearing the label for the vdevs which are members of
7336401734dSWill Andrews  *		the exported or foreign pools.
7346401734dSWill Andrews  *
7356401734dSWill Andrews  * Verifies that the vdev is not active and zeros out the label information
7366401734dSWill Andrews  * on the device.
7376401734dSWill Andrews  */
7386401734dSWill Andrews int
7396401734dSWill Andrews zpool_do_labelclear(int argc, char **argv)
7406401734dSWill Andrews {
7416401734dSWill Andrews 	char vdev[MAXPATHLEN];
7426401734dSWill Andrews 	char *name = NULL;
7436401734dSWill Andrews 	struct stat st;
7446401734dSWill Andrews 	int c, fd, ret = 0;
7456401734dSWill Andrews 	nvlist_t *config;
7466401734dSWill Andrews 	pool_state_t state;
7476401734dSWill Andrews 	boolean_t inuse = B_FALSE;
7486401734dSWill Andrews 	boolean_t force = B_FALSE;
7496401734dSWill Andrews 
7506401734dSWill Andrews 	/* check options */
7516401734dSWill Andrews 	while ((c = getopt(argc, argv, "f")) != -1) {
7526401734dSWill Andrews 		switch (c) {
7536401734dSWill Andrews 		case 'f':
7546401734dSWill Andrews 			force = B_TRUE;
7556401734dSWill Andrews 			break;
7566401734dSWill Andrews 		default:
7576401734dSWill Andrews 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
7586401734dSWill Andrews 			    optopt);
7596401734dSWill Andrews 			usage(B_FALSE);
7606401734dSWill Andrews 		}
7616401734dSWill Andrews 	}
7626401734dSWill Andrews 
7636401734dSWill Andrews 	argc -= optind;
7646401734dSWill Andrews 	argv += optind;
7656401734dSWill Andrews 
7666401734dSWill Andrews 	/* get vdev name */
7676401734dSWill Andrews 	if (argc < 1) {
7686401734dSWill Andrews 		(void) fprintf(stderr, gettext("missing vdev name\n"));
7696401734dSWill Andrews 		usage(B_FALSE);
7706401734dSWill Andrews 	}
7716401734dSWill Andrews 	if (argc > 1) {
7726401734dSWill Andrews 		(void) fprintf(stderr, gettext("too many arguments\n"));
7736401734dSWill Andrews 		usage(B_FALSE);
7746401734dSWill Andrews 	}
7756401734dSWill Andrews 
7766401734dSWill Andrews 	/*
7776401734dSWill Andrews 	 * Check if we were given absolute path and use it as is.
7786401734dSWill Andrews 	 * Otherwise if the provided vdev name doesn't point to a file,
7796401734dSWill Andrews 	 * try prepending dsk path and appending s0.
7806401734dSWill Andrews 	 */
7816401734dSWill Andrews 	(void) strlcpy(vdev, argv[0], sizeof (vdev));
7826401734dSWill Andrews 	if (vdev[0] != '/' && stat(vdev, &st) != 0) {
7836401734dSWill Andrews 		char *s;
7846401734dSWill Andrews 
7856401734dSWill Andrews 		(void) snprintf(vdev, sizeof (vdev), "%s/%s",
7866401734dSWill Andrews 		    ZFS_DISK_ROOT, argv[0]);
7876401734dSWill Andrews 		if ((s = strrchr(argv[0], 's')) == NULL ||
7886401734dSWill Andrews 		    !isdigit(*(s + 1)))
7896401734dSWill Andrews 			(void) strlcat(vdev, "s0", sizeof (vdev));
7906401734dSWill Andrews 		if (stat(vdev, &st) != 0) {
7916401734dSWill Andrews 			(void) fprintf(stderr, gettext(
7926401734dSWill Andrews 			    "failed to find device %s, try specifying absolute "
7936401734dSWill Andrews 			    "path instead\n"), argv[0]);
7946401734dSWill Andrews 			return (1);
7956401734dSWill Andrews 		}
7966401734dSWill Andrews 	}
7976401734dSWill Andrews 
7986401734dSWill Andrews 	if ((fd = open(vdev, O_RDWR)) < 0) {
7996401734dSWill Andrews 		(void) fprintf(stderr, gettext("failed to open %s: %s\n"),
8006401734dSWill Andrews 		    vdev, strerror(errno));
8016401734dSWill Andrews 		return (1);
8026401734dSWill Andrews 	}
8036401734dSWill Andrews 
804c861bfbdSAlan Somers 	if (zpool_read_label(fd, &config) != 0) {
8056401734dSWill Andrews 		(void) fprintf(stderr,
8066401734dSWill Andrews 		    gettext("failed to read label from %s\n"), vdev);
8076401734dSWill Andrews 		return (1);
8086401734dSWill Andrews 	}
8096401734dSWill Andrews 	nvlist_free(config);
8106401734dSWill Andrews 
8116401734dSWill Andrews 	ret = zpool_in_use(g_zfs, fd, &state, &name, &inuse);
8126401734dSWill Andrews 	if (ret != 0) {
8136401734dSWill Andrews 		(void) fprintf(stderr,
8146401734dSWill Andrews 		    gettext("failed to check state for %s\n"), vdev);
8156401734dSWill Andrews 		return (1);
8166401734dSWill Andrews 	}
8176401734dSWill Andrews 
8186401734dSWill Andrews 	if (!inuse)
8196401734dSWill Andrews 		goto wipe_label;
8206401734dSWill Andrews 
8216401734dSWill Andrews 	switch (state) {
8226401734dSWill Andrews 	default:
8236401734dSWill Andrews 	case POOL_STATE_ACTIVE:
8246401734dSWill Andrews 	case POOL_STATE_SPARE:
8256401734dSWill Andrews 	case POOL_STATE_L2CACHE:
8266401734dSWill Andrews 		(void) fprintf(stderr, gettext(
8276401734dSWill Andrews 		    "%s is a member (%s) of pool \"%s\"\n"),
8286401734dSWill Andrews 		    vdev, zpool_pool_state_to_name(state), name);
8296401734dSWill Andrews 		ret = 1;
8306401734dSWill Andrews 		goto errout;
8316401734dSWill Andrews 
8326401734dSWill Andrews 	case POOL_STATE_EXPORTED:
8336401734dSWill Andrews 		if (force)
8346401734dSWill Andrews 			break;
8356401734dSWill Andrews 		(void) fprintf(stderr, gettext(
8366401734dSWill Andrews 		    "use '-f' to override the following error:\n"
8376401734dSWill Andrews 		    "%s is a member of exported pool \"%s\"\n"),
8386401734dSWill Andrews 		    vdev, name);
8396401734dSWill Andrews 		ret = 1;
8406401734dSWill Andrews 		goto errout;
8416401734dSWill Andrews 
8426401734dSWill Andrews 	case POOL_STATE_POTENTIALLY_ACTIVE:
8436401734dSWill Andrews 		if (force)
8446401734dSWill Andrews 			break;
8456401734dSWill Andrews 		(void) fprintf(stderr, gettext(
8466401734dSWill Andrews 		    "use '-f' to override the following error:\n"
8476401734dSWill Andrews 		    "%s is a member of potentially active pool \"%s\"\n"),
8486401734dSWill Andrews 		    vdev, name);
8496401734dSWill Andrews 		ret = 1;
8506401734dSWill Andrews 		goto errout;
8516401734dSWill Andrews 
8526401734dSWill Andrews 	case POOL_STATE_DESTROYED:
8536401734dSWill Andrews 		/* inuse should never be set for a destroyed pool */
8546401734dSWill Andrews 		assert(0);
8556401734dSWill Andrews 		break;
8566401734dSWill Andrews 	}
8576401734dSWill Andrews 
8586401734dSWill Andrews wipe_label:
8596401734dSWill Andrews 	ret = zpool_clear_label(fd);
8606401734dSWill Andrews 	if (ret != 0) {
8616401734dSWill Andrews 		(void) fprintf(stderr,
8626401734dSWill Andrews 		    gettext("failed to clear label for %s\n"), vdev);
8636401734dSWill Andrews 	}
8646401734dSWill Andrews 
8656401734dSWill Andrews errout:
8666401734dSWill Andrews 	free(name);
8676401734dSWill Andrews 	(void) close(fd);
8686401734dSWill Andrews 
8696401734dSWill Andrews 	return (ret);
8706401734dSWill Andrews }
8716401734dSWill Andrews 
872fa9e4066Sahrens /*
8737855d95bSToomas Soome  * zpool create [-fnd] [-B] [-o property=value] ...
8740a48a24eStimh  *		[-O file-system-property=value] ...
87504e56356SAndriy Gapon  *		[-R root] [-m mountpoint] [-t tempname] <pool> <dev> ...
876fa9e4066Sahrens  *
8777855d95bSToomas Soome  *	-B	Create boot partition.
878fa9e4066Sahrens  *	-f	Force creation, even if devices appear in use
879fa9e4066Sahrens  *	-n	Do not create the pool, but display the resulting layout if it
880fa9e4066Sahrens  *		were to be created.
88104e56356SAndriy Gapon  *	-R	Create a pool under an alternate root
88204e56356SAndriy Gapon  *	-m	Set default mountpoint for the root dataset.  By default it's
883ad135b5dSChristopher Siden  *		'/<pool>'
88404e56356SAndriy Gapon  *	-t	Use the temporary name until the pool is exported.
885990b4856Slling  *	-o	Set property=value.
886ad135b5dSChristopher Siden  *	-d	Don't automatically enable all supported pool features
887ad135b5dSChristopher Siden  *		(individual features can be enabled with -o).
8880a48a24eStimh  *	-O	Set fsproperty=value in the pool's root file system
889fa9e4066Sahrens  *
890b1b8ab34Slling  * Creates the named pool according to the given vdev specification.  The
891fa9e4066Sahrens  * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c.  Once
892fa9e4066Sahrens  * we get the nvlist back from get_vdev_spec(), we either print out the contents
893fa9e4066Sahrens  * (if '-n' was specified), or pass it to libzfs to do the creation.
894fa9e4066Sahrens  */
8957855d95bSToomas Soome 
8967855d95bSToomas Soome #define	SYSTEM256	(256 * 1024 * 1024)
897fa9e4066Sahrens int
898fa9e4066Sahrens zpool_do_create(int argc, char **argv)
899fa9e4066Sahrens {
90099653d4eSeschrock 	boolean_t force = B_FALSE;
90199653d4eSeschrock 	boolean_t dryrun = B_FALSE;
902ad135b5dSChristopher Siden 	boolean_t enable_all_pool_feat = B_TRUE;
9037855d95bSToomas Soome 	zpool_boot_label_t boot_type = ZPOOL_NO_BOOT_LABEL;
9047855d95bSToomas Soome 	uint64_t boot_size = 0;
905fa9e4066Sahrens 	int c;
906990b4856Slling 	nvlist_t *nvroot = NULL;
907fa9e4066Sahrens 	char *poolname;
90804e56356SAndriy Gapon 	char *tname = NULL;
909990b4856Slling 	int ret = 1;
910fa9e4066Sahrens 	char *altroot = NULL;
911fa9e4066Sahrens 	char *mountpoint = NULL;
9120a48a24eStimh 	nvlist_t *fsprops = NULL;
913990b4856Slling 	nvlist_t *props = NULL;
9142f8aaab3Seschrock 	char *propval;
915fa9e4066Sahrens 
916fa9e4066Sahrens 	/* check options */
91704e56356SAndriy Gapon 	while ((c = getopt(argc, argv, ":fndBR:m:o:O:t:")) != -1) {
918fa9e4066Sahrens 		switch (c) {
919fa9e4066Sahrens 		case 'f':
92099653d4eSeschrock 			force = B_TRUE;
921fa9e4066Sahrens 			break;
922fa9e4066Sahrens 		case 'n':
92399653d4eSeschrock 			dryrun = B_TRUE;
924fa9e4066Sahrens 			break;
925ad135b5dSChristopher Siden 		case 'd':
926ad135b5dSChristopher Siden 			enable_all_pool_feat = B_FALSE;
927ad135b5dSChristopher Siden 			break;
9287855d95bSToomas Soome 		case 'B':
9297855d95bSToomas Soome 			/*
9307855d95bSToomas Soome 			 * We should create the system partition.
9317855d95bSToomas Soome 			 * Also make sure the size is set.
9327855d95bSToomas Soome 			 */
9337855d95bSToomas Soome 			boot_type = ZPOOL_CREATE_BOOT_LABEL;
9347855d95bSToomas Soome 			if (boot_size == 0)
9357855d95bSToomas Soome 				boot_size = SYSTEM256;
9367855d95bSToomas Soome 			break;
937fa9e4066Sahrens 		case 'R':
938fa9e4066Sahrens 			altroot = optarg;
939990b4856Slling 			if (add_prop_list(zpool_prop_to_name(
9400a48a24eStimh 			    ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
941990b4856Slling 				goto errout;
94204e56356SAndriy Gapon 			if (add_prop_list_default(zpool_prop_to_name(
9430a48a24eStimh 			    ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
944990b4856Slling 				goto errout;
945fa9e4066Sahrens 			break;
946fa9e4066Sahrens 		case 'm':
9478b713775SWill Andrews 			/* Equivalent to -O mountpoint=optarg */
948fa9e4066Sahrens 			mountpoint = optarg;
949fa9e4066Sahrens 			break;
950990b4856Slling 		case 'o':
951990b4856Slling 			if ((propval = strchr(optarg, '=')) == NULL) {
952990b4856Slling 				(void) fprintf(stderr, gettext("missing "
953990b4856Slling 				    "'=' for -o option\n"));
954990b4856Slling 				goto errout;
955990b4856Slling 			}
956990b4856Slling 			*propval = '\0';
957990b4856Slling 			propval++;
958990b4856Slling 
9590a48a24eStimh 			if (add_prop_list(optarg, propval, &props, B_TRUE))
9600a48a24eStimh 				goto errout;
961ad135b5dSChristopher Siden 
9627855d95bSToomas Soome 			/*
9637855d95bSToomas Soome 			 * Get bootsize value for make_root_vdev().
9647855d95bSToomas Soome 			 */
9657855d95bSToomas Soome 			if (zpool_name_to_prop(optarg) == ZPOOL_PROP_BOOTSIZE) {
9667855d95bSToomas Soome 				if (zfs_nicestrtonum(g_zfs, propval,
9677855d95bSToomas Soome 				    &boot_size) < 0 || boot_size == 0) {
9687855d95bSToomas Soome 					(void) fprintf(stderr,
9697855d95bSToomas Soome 					    gettext("bad boot partition size "
9707855d95bSToomas Soome 					    "'%s': %s\n"),  propval,
9717855d95bSToomas Soome 					    libzfs_error_description(g_zfs));
9727855d95bSToomas Soome 					goto errout;
9737855d95bSToomas Soome 				}
9747855d95bSToomas Soome 			}
9757855d95bSToomas Soome 
976ad135b5dSChristopher Siden 			/*
977ad135b5dSChristopher Siden 			 * If the user is creating a pool that doesn't support
978ad135b5dSChristopher Siden 			 * feature flags, don't enable any features.
979ad135b5dSChristopher Siden 			 */
980ad135b5dSChristopher Siden 			if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
981ad135b5dSChristopher Siden 				char *end;
982ad135b5dSChristopher Siden 				u_longlong_t ver;
983ad135b5dSChristopher Siden 
984ad135b5dSChristopher Siden 				ver = strtoull(propval, &end, 10);
985ad135b5dSChristopher Siden 				if (*end == '\0' &&
986ad135b5dSChristopher Siden 				    ver < SPA_VERSION_FEATURES) {
987ad135b5dSChristopher Siden 					enable_all_pool_feat = B_FALSE;
988ad135b5dSChristopher Siden 				}
989ad135b5dSChristopher Siden 			}
990c423721fSXin Li 			if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT)
991c423721fSXin Li 				altroot = propval;
9920a48a24eStimh 			break;
9930a48a24eStimh 		case 'O':
9940a48a24eStimh 			if ((propval = strchr(optarg, '=')) == NULL) {
9950a48a24eStimh 				(void) fprintf(stderr, gettext("missing "
9960a48a24eStimh 				    "'=' for -O option\n"));
9970a48a24eStimh 				goto errout;
9980a48a24eStimh 			}
9990a48a24eStimh 			*propval = '\0';
10000a48a24eStimh 			propval++;
10010a48a24eStimh 
10028b713775SWill Andrews 			/*
10038b713775SWill Andrews 			 * Mountpoints are checked and then added later.
10048b713775SWill Andrews 			 * Uniquely among properties, they can be specified
10058b713775SWill Andrews 			 * more than once, to avoid conflict with -m.
10068b713775SWill Andrews 			 */
10078b713775SWill Andrews 			if (0 == strcmp(optarg,
10088b713775SWill Andrews 			    zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
10098b713775SWill Andrews 				mountpoint = propval;
10108b713775SWill Andrews 			} else if (add_prop_list(optarg, propval, &fsprops,
10118b713775SWill Andrews 			    B_FALSE)) {
1012990b4856Slling 				goto errout;
10138b713775SWill Andrews 			}
1014990b4856Slling 			break;
101504e56356SAndriy Gapon 		case 't':
101604e56356SAndriy Gapon 			/*
101704e56356SAndriy Gapon 			 * Sanity check temporary pool name.
101804e56356SAndriy Gapon 			 */
101904e56356SAndriy Gapon 			if (strchr(optarg, '/') != NULL) {
102004e56356SAndriy Gapon 				(void) fprintf(stderr, gettext("cannot create "
102104e56356SAndriy Gapon 				    "'%s': invalid character '/' in temporary "
102204e56356SAndriy Gapon 				    "name\n"), optarg);
102304e56356SAndriy Gapon 				(void) fprintf(stderr, gettext("use 'zfs "
102404e56356SAndriy Gapon 				    "create' to create a dataset\n"));
102504e56356SAndriy Gapon 				goto errout;
102604e56356SAndriy Gapon 			}
102704e56356SAndriy Gapon 
102804e56356SAndriy Gapon 			if (add_prop_list(zpool_prop_to_name(
102904e56356SAndriy Gapon 			    ZPOOL_PROP_TNAME), optarg, &props, B_TRUE))
103004e56356SAndriy Gapon 				goto errout;
103104e56356SAndriy Gapon 			if (add_prop_list_default(zpool_prop_to_name(
103204e56356SAndriy Gapon 			    ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
103304e56356SAndriy Gapon 				goto errout;
103404e56356SAndriy Gapon 			tname = optarg;
103504e56356SAndriy Gapon 			break;
1036fa9e4066Sahrens 		case ':':
1037fa9e4066Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
1038fa9e4066Sahrens 			    "'%c' option\n"), optopt);
1039990b4856Slling 			goto badusage;
1040fa9e4066Sahrens 		case '?':
1041fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1042fa9e4066Sahrens 			    optopt);
1043990b4856Slling 			goto badusage;
1044fa9e4066Sahrens 		}
1045fa9e4066Sahrens 	}
1046fa9e4066Sahrens 
1047fa9e4066Sahrens 	argc -= optind;
1048fa9e4066Sahrens 	argv += optind;
1049fa9e4066Sahrens 
1050fa9e4066Sahrens 	/* get pool name and check number of arguments */
1051fa9e4066Sahrens 	if (argc < 1) {
1052fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
1053990b4856Slling 		goto badusage;
1054fa9e4066Sahrens 	}
1055fa9e4066Sahrens 	if (argc < 2) {
1056fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing vdev specification\n"));
1057990b4856Slling 		goto badusage;
1058fa9e4066Sahrens 	}
1059fa9e4066Sahrens 
1060fa9e4066Sahrens 	poolname = argv[0];
1061fa9e4066Sahrens 
1062fa9e4066Sahrens 	/*
1063fa9e4066Sahrens 	 * As a special case, check for use of '/' in the name, and direct the
1064fa9e4066Sahrens 	 * user to use 'zfs create' instead.
1065fa9e4066Sahrens 	 */
1066fa9e4066Sahrens 	if (strchr(poolname, '/') != NULL) {
1067fa9e4066Sahrens 		(void) fprintf(stderr, gettext("cannot create '%s': invalid "
1068fa9e4066Sahrens 		    "character '/' in pool name\n"), poolname);
1069fa9e4066Sahrens 		(void) fprintf(stderr, gettext("use 'zfs create' to "
1070fa9e4066Sahrens 		    "create a dataset\n"));
1071990b4856Slling 		goto errout;
1072fa9e4066Sahrens 	}
1073fa9e4066Sahrens 
10747855d95bSToomas Soome 	/*
10757855d95bSToomas Soome 	 * Make sure the bootsize is set when ZPOOL_CREATE_BOOT_LABEL is used,
10767855d95bSToomas Soome 	 * and not set otherwise.
10777855d95bSToomas Soome 	 */
10787855d95bSToomas Soome 	if (boot_type == ZPOOL_CREATE_BOOT_LABEL) {
10797855d95bSToomas Soome 		const char *propname;
10807855d95bSToomas Soome 		char *strptr, *buf = NULL;
10817855d95bSToomas Soome 		int rv;
10827855d95bSToomas Soome 
10837855d95bSToomas Soome 		propname = zpool_prop_to_name(ZPOOL_PROP_BOOTSIZE);
10847855d95bSToomas Soome 		if (nvlist_lookup_string(props, propname, &strptr) != 0) {
10857855d95bSToomas Soome 			(void) asprintf(&buf, "%" PRIu64, boot_size);
10867855d95bSToomas Soome 			if (buf == NULL) {
10877855d95bSToomas Soome 				(void) fprintf(stderr,
10887855d95bSToomas Soome 				    gettext("internal error: out of memory\n"));
10897855d95bSToomas Soome 				goto errout;
10907855d95bSToomas Soome 			}
10917855d95bSToomas Soome 			rv = add_prop_list(propname, buf, &props, B_TRUE);
10927855d95bSToomas Soome 			free(buf);
10937855d95bSToomas Soome 			if (rv != 0)
10947855d95bSToomas Soome 				goto errout;
10957855d95bSToomas Soome 		}
10967855d95bSToomas Soome 	} else {
10977855d95bSToomas Soome 		const char *propname;
10987855d95bSToomas Soome 		char *strptr;
10997855d95bSToomas Soome 
11007855d95bSToomas Soome 		propname = zpool_prop_to_name(ZPOOL_PROP_BOOTSIZE);
11017855d95bSToomas Soome 		if (nvlist_lookup_string(props, propname, &strptr) == 0) {
11027855d95bSToomas Soome 			(void) fprintf(stderr, gettext("error: setting boot "
11037855d95bSToomas Soome 			    "partition size requires option '-B'\n"));
11047855d95bSToomas Soome 			goto errout;
11057855d95bSToomas Soome 		}
11067855d95bSToomas Soome 	}
11077855d95bSToomas Soome 
1108fa9e4066Sahrens 	/* pass off to get_vdev_spec for bulk processing */
1109705040edSEric Taylor 	nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
11107855d95bSToomas Soome 	    boot_type, boot_size, argc - 1, argv + 1);
1111fa9e4066Sahrens 	if (nvroot == NULL)
11120a48a24eStimh 		goto errout;
1113fa9e4066Sahrens 
111499653d4eSeschrock 	/* make_root_vdev() allows 0 toplevel children if there are spares */
1115b7b97454Sperrin 	if (!zfs_allocatable_devs(nvroot)) {
111699653d4eSeschrock 		(void) fprintf(stderr, gettext("invalid vdev "
111799653d4eSeschrock 		    "specification: at least one toplevel vdev must be "
111899653d4eSeschrock 		    "specified\n"));
1119990b4856Slling 		goto errout;
112099653d4eSeschrock 	}
112199653d4eSeschrock 
1122fa9e4066Sahrens 	if (altroot != NULL && altroot[0] != '/') {
1123fa9e4066Sahrens 		(void) fprintf(stderr, gettext("invalid alternate root '%s': "
1124e9dbad6fSeschrock 		    "must be an absolute path\n"), altroot);
1125990b4856Slling 		goto errout;
1126fa9e4066Sahrens 	}
1127fa9e4066Sahrens 
1128fa9e4066Sahrens 	/*
1129fa9e4066Sahrens 	 * Check the validity of the mountpoint and direct the user to use the
1130fa9e4066Sahrens 	 * '-m' mountpoint option if it looks like its in use.
1131fa9e4066Sahrens 	 */
1132fa9e4066Sahrens 	if (mountpoint == NULL ||
1133fa9e4066Sahrens 	    (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
1134fa9e4066Sahrens 	    strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
1135fa9e4066Sahrens 		char buf[MAXPATHLEN];
113611022c7cStimh 		DIR *dirp;
1137fa9e4066Sahrens 
1138fa9e4066Sahrens 		if (mountpoint && mountpoint[0] != '/') {
1139fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid mountpoint "
1140fa9e4066Sahrens 			    "'%s': must be an absolute path, 'legacy', or "
1141fa9e4066Sahrens 			    "'none'\n"), mountpoint);
1142990b4856Slling 			goto errout;
1143fa9e4066Sahrens 		}
1144fa9e4066Sahrens 
1145fa9e4066Sahrens 		if (mountpoint == NULL) {
1146fa9e4066Sahrens 			if (altroot != NULL)
1147fa9e4066Sahrens 				(void) snprintf(buf, sizeof (buf), "%s/%s",
1148fa9e4066Sahrens 				    altroot, poolname);
1149fa9e4066Sahrens 			else
1150fa9e4066Sahrens 				(void) snprintf(buf, sizeof (buf), "/%s",
1151fa9e4066Sahrens 				    poolname);
1152fa9e4066Sahrens 		} else {
1153fa9e4066Sahrens 			if (altroot != NULL)
1154fa9e4066Sahrens 				(void) snprintf(buf, sizeof (buf), "%s%s",
1155fa9e4066Sahrens 				    altroot, mountpoint);
1156fa9e4066Sahrens 			else
1157fa9e4066Sahrens 				(void) snprintf(buf, sizeof (buf), "%s",
1158fa9e4066Sahrens 				    mountpoint);
1159fa9e4066Sahrens 		}
1160fa9e4066Sahrens 
116111022c7cStimh 		if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
116211022c7cStimh 			(void) fprintf(stderr, gettext("mountpoint '%s' : "
116311022c7cStimh 			    "%s\n"), buf, strerror(errno));
1164fa9e4066Sahrens 			(void) fprintf(stderr, gettext("use '-m' "
1165fa9e4066Sahrens 			    "option to provide a different default\n"));
1166990b4856Slling 			goto errout;
116711022c7cStimh 		} else if (dirp) {
116811022c7cStimh 			int count = 0;
116911022c7cStimh 
117011022c7cStimh 			while (count < 3 && readdir(dirp) != NULL)
117111022c7cStimh 				count++;
117211022c7cStimh 			(void) closedir(dirp);
117311022c7cStimh 
117411022c7cStimh 			if (count > 2) {
117511022c7cStimh 				(void) fprintf(stderr, gettext("mountpoint "
117611022c7cStimh 				    "'%s' exists and is not empty\n"), buf);
117711022c7cStimh 				(void) fprintf(stderr, gettext("use '-m' "
117811022c7cStimh 				    "option to provide a "
117911022c7cStimh 				    "different default\n"));
118011022c7cStimh 				goto errout;
118111022c7cStimh 			}
1182fa9e4066Sahrens 		}
1183fa9e4066Sahrens 	}
1184fa9e4066Sahrens 
11858b713775SWill Andrews 	/*
11868b713775SWill Andrews 	 * Now that the mountpoint's validity has been checked, ensure that
11878b713775SWill Andrews 	 * the property is set appropriately prior to creating the pool.
11888b713775SWill Andrews 	 */
11898b713775SWill Andrews 	if (mountpoint != NULL) {
11908b713775SWill Andrews 		ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
11918b713775SWill Andrews 		    mountpoint, &fsprops, B_FALSE);
11928b713775SWill Andrews 		if (ret != 0)
11938b713775SWill Andrews 			goto errout;
11948b713775SWill Andrews 	}
11958b713775SWill Andrews 
11968b713775SWill Andrews 	ret = 1;
1197fa9e4066Sahrens 	if (dryrun) {
1198fa9e4066Sahrens 		/*
1199fa9e4066Sahrens 		 * For a dry run invocation, print out a basic message and run
1200fa9e4066Sahrens 		 * through all the vdevs in the list and print out in an
1201fa9e4066Sahrens 		 * appropriate hierarchy.
1202fa9e4066Sahrens 		 */
1203fa9e4066Sahrens 		(void) printf(gettext("would create '%s' with the "
1204fa9e4066Sahrens 		    "following layout:\n\n"), poolname);
1205fa9e4066Sahrens 
12068654d025Sperrin 		print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
12078654d025Sperrin 		if (num_logs(nvroot) > 0)
12088654d025Sperrin 			print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
1209fa9e4066Sahrens 
1210fa9e4066Sahrens 		ret = 0;
1211fa9e4066Sahrens 	} else {
1212fa9e4066Sahrens 		/*
1213fa9e4066Sahrens 		 * Hand off to libzfs.
1214fa9e4066Sahrens 		 */
1215ad135b5dSChristopher Siden 		if (enable_all_pool_feat) {
12162acef22dSMatthew Ahrens 			spa_feature_t i;
1217ad135b5dSChristopher Siden 			for (i = 0; i < SPA_FEATURES; i++) {
1218ad135b5dSChristopher Siden 				char propname[MAXPATHLEN];
1219ad135b5dSChristopher Siden 				zfeature_info_t *feat = &spa_feature_table[i];
1220ad135b5dSChristopher Siden 
1221ad135b5dSChristopher Siden 				(void) snprintf(propname, sizeof (propname),
1222ad135b5dSChristopher Siden 				    "feature@%s", feat->fi_uname);
1223ad135b5dSChristopher Siden 
1224ad135b5dSChristopher Siden 				/*
1225ad135b5dSChristopher Siden 				 * Skip feature if user specified it manually
1226ad135b5dSChristopher Siden 				 * on the command line.
1227ad135b5dSChristopher Siden 				 */
1228ad135b5dSChristopher Siden 				if (nvlist_exists(props, propname))
1229ad135b5dSChristopher Siden 					continue;
1230ad135b5dSChristopher Siden 
12318b713775SWill Andrews 				ret = add_prop_list(propname,
12328b713775SWill Andrews 				    ZFS_FEATURE_ENABLED, &props, B_TRUE);
12338b713775SWill Andrews 				if (ret != 0)
1234ad135b5dSChristopher Siden 					goto errout;
1235ad135b5dSChristopher Siden 			}
1236ad135b5dSChristopher Siden 		}
12378b713775SWill Andrews 
12388b713775SWill Andrews 		ret = 1;
12390a48a24eStimh 		if (zpool_create(g_zfs, poolname,
12400a48a24eStimh 		    nvroot, props, fsprops) == 0) {
124104e56356SAndriy Gapon 			zfs_handle_t *pool = zfs_open(g_zfs,
124204e56356SAndriy Gapon 			    tname ? tname : poolname, ZFS_TYPE_FILESYSTEM);
1243fa9e4066Sahrens 			if (pool != NULL) {
1244fa9e4066Sahrens 				if (zfs_mount(pool, NULL, 0) == 0)
1245da6c28aaSamw 					ret = zfs_shareall(pool);
1246fa9e4066Sahrens 				zfs_close(pool);
1247fa9e4066Sahrens 			}
124899653d4eSeschrock 		} else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
124999653d4eSeschrock 			(void) fprintf(stderr, gettext("pool name may have "
125099653d4eSeschrock 			    "been omitted\n"));
1251fa9e4066Sahrens 		}
1252fa9e4066Sahrens 	}
1253fa9e4066Sahrens 
1254990b4856Slling errout:
12552f8aaab3Seschrock 	nvlist_free(nvroot);
12560a48a24eStimh 	nvlist_free(fsprops);
12572f8aaab3Seschrock 	nvlist_free(props);
1258fa9e4066Sahrens 	return (ret);
1259990b4856Slling badusage:
12600a48a24eStimh 	nvlist_free(fsprops);
1261990b4856Slling 	nvlist_free(props);
1262990b4856Slling 	usage(B_FALSE);
1263990b4856Slling 	return (2);
1264fa9e4066Sahrens }
1265fa9e4066Sahrens 
1266fa9e4066Sahrens /*
1267fa9e4066Sahrens  * zpool destroy <pool>
1268fa9e4066Sahrens  *
126904e56356SAndriy Gapon  *	-f	Forcefully unmount any datasets
1270fa9e4066Sahrens  *
1271fa9e4066Sahrens  * Destroy the given pool.  Automatically unmounts any datasets in the pool.
1272fa9e4066Sahrens  */
1273fa9e4066Sahrens int
1274fa9e4066Sahrens zpool_do_destroy(int argc, char **argv)
1275fa9e4066Sahrens {
127699653d4eSeschrock 	boolean_t force = B_FALSE;
1277fa9e4066Sahrens 	int c;
1278fa9e4066Sahrens 	char *pool;
1279fa9e4066Sahrens 	zpool_handle_t *zhp;
1280fa9e4066Sahrens 	int ret;
1281fa9e4066Sahrens 
1282fa9e4066Sahrens 	/* check options */
1283fa9e4066Sahrens 	while ((c = getopt(argc, argv, "f")) != -1) {
1284fa9e4066Sahrens 		switch (c) {
1285fa9e4066Sahrens 		case 'f':
128699653d4eSeschrock 			force = B_TRUE;
1287fa9e4066Sahrens 			break;
1288fa9e4066Sahrens 		case '?':
1289fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1290fa9e4066Sahrens 			    optopt);
129199653d4eSeschrock 			usage(B_FALSE);
1292fa9e4066Sahrens 		}
1293fa9e4066Sahrens 	}
1294fa9e4066Sahrens 
1295fa9e4066Sahrens 	argc -= optind;
1296fa9e4066Sahrens 	argv += optind;
1297fa9e4066Sahrens 
1298fa9e4066Sahrens 	/* check arguments */
1299fa9e4066Sahrens 	if (argc < 1) {
1300fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool argument\n"));
130199653d4eSeschrock 		usage(B_FALSE);
1302fa9e4066Sahrens 	}
1303fa9e4066Sahrens 	if (argc > 1) {
1304fa9e4066Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
130599653d4eSeschrock 		usage(B_FALSE);
1306fa9e4066Sahrens 	}
1307fa9e4066Sahrens 
1308fa9e4066Sahrens 	pool = argv[0];
1309fa9e4066Sahrens 
131099653d4eSeschrock 	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1311fa9e4066Sahrens 		/*
1312fa9e4066Sahrens 		 * As a special case, check for use of '/' in the name, and
1313fa9e4066Sahrens 		 * direct the user to use 'zfs destroy' instead.
1314fa9e4066Sahrens 		 */
1315fa9e4066Sahrens 		if (strchr(pool, '/') != NULL)
1316fa9e4066Sahrens 			(void) fprintf(stderr, gettext("use 'zfs destroy' to "
1317fa9e4066Sahrens 			    "destroy a dataset\n"));
1318fa9e4066Sahrens 		return (1);
1319fa9e4066Sahrens 	}
1320fa9e4066Sahrens 
1321f3861e1aSahl 	if (zpool_disable_datasets(zhp, force) != 0) {
1322fa9e4066Sahrens 		(void) fprintf(stderr, gettext("could not destroy '%s': "
1323fa9e4066Sahrens 		    "could not unmount datasets\n"), zpool_get_name(zhp));
1324fa9e4066Sahrens 		return (1);
1325fa9e4066Sahrens 	}
1326fa9e4066Sahrens 
13274445fffbSMatthew Ahrens 	/* The history must be logged as part of the export */
13284445fffbSMatthew Ahrens 	log_history = B_FALSE;
13294445fffbSMatthew Ahrens 
13304445fffbSMatthew Ahrens 	ret = (zpool_destroy(zhp, history_str) != 0);
1331fa9e4066Sahrens 
1332fa9e4066Sahrens 	zpool_close(zhp);
1333fa9e4066Sahrens 
1334fa9e4066Sahrens 	return (ret);
1335fa9e4066Sahrens }
1336fa9e4066Sahrens 
1337fa9e4066Sahrens /*
1338fa9e4066Sahrens  * zpool export [-f] <pool> ...
1339fa9e4066Sahrens  *
1340fa9e4066Sahrens  *	-f	Forcefully unmount datasets
1341fa9e4066Sahrens  *
1342b1b8ab34Slling  * Export the given pools.  By default, the command will attempt to cleanly
1343fa9e4066Sahrens  * unmount any active datasets within the pool.  If the '-f' flag is specified,
1344fa9e4066Sahrens  * then the datasets will be forcefully unmounted.
1345fa9e4066Sahrens  */
1346fa9e4066Sahrens int
1347fa9e4066Sahrens zpool_do_export(int argc, char **argv)
1348fa9e4066Sahrens {
134999653d4eSeschrock 	boolean_t force = B_FALSE;
1350394ab0cbSGeorge Wilson 	boolean_t hardforce = B_FALSE;
1351fa9e4066Sahrens 	int c;
1352fa9e4066Sahrens 	zpool_handle_t *zhp;
1353fa9e4066Sahrens 	int ret;
1354fa9e4066Sahrens 	int i;
1355fa9e4066Sahrens 
1356fa9e4066Sahrens 	/* check options */
1357394ab0cbSGeorge Wilson 	while ((c = getopt(argc, argv, "fF")) != -1) {
1358fa9e4066Sahrens 		switch (c) {
1359fa9e4066Sahrens 		case 'f':
136099653d4eSeschrock 			force = B_TRUE;
1361fa9e4066Sahrens 			break;
1362394ab0cbSGeorge Wilson 		case 'F':
1363394ab0cbSGeorge Wilson 			hardforce = B_TRUE;
1364394ab0cbSGeorge Wilson 			break;
1365fa9e4066Sahrens 		case '?':
1366fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1367fa9e4066Sahrens 			    optopt);
136899653d4eSeschrock 			usage(B_FALSE);
1369fa9e4066Sahrens 		}
1370fa9e4066Sahrens 	}
1371fa9e4066Sahrens 
1372fa9e4066Sahrens 	argc -= optind;
1373fa9e4066Sahrens 	argv += optind;
1374fa9e4066Sahrens 
1375fa9e4066Sahrens 	/* check arguments */
1376fa9e4066Sahrens 	if (argc < 1) {
1377fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool argument\n"));
137899653d4eSeschrock 		usage(B_FALSE);
1379fa9e4066Sahrens 	}
1380fa9e4066Sahrens 
1381fa9e4066Sahrens 	ret = 0;
1382fa9e4066Sahrens 	for (i = 0; i < argc; i++) {
138399653d4eSeschrock 		if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1384fa9e4066Sahrens 			ret = 1;
1385fa9e4066Sahrens 			continue;
1386fa9e4066Sahrens 		}
1387fa9e4066Sahrens 
1388f3861e1aSahl 		if (zpool_disable_datasets(zhp, force) != 0) {
1389fa9e4066Sahrens 			ret = 1;
1390fa9e4066Sahrens 			zpool_close(zhp);
1391fa9e4066Sahrens 			continue;
1392fa9e4066Sahrens 		}
1393fa9e4066Sahrens 
13944445fffbSMatthew Ahrens 		/* The history must be logged as part of the export */
13954445fffbSMatthew Ahrens 		log_history = B_FALSE;
13964445fffbSMatthew Ahrens 
1397394ab0cbSGeorge Wilson 		if (hardforce) {
13984445fffbSMatthew Ahrens 			if (zpool_export_force(zhp, history_str) != 0)
1399394ab0cbSGeorge Wilson 				ret = 1;
14004445fffbSMatthew Ahrens 		} else if (zpool_export(zhp, force, history_str) != 0) {
1401fa9e4066Sahrens 			ret = 1;
1402394ab0cbSGeorge Wilson 		}
1403fa9e4066Sahrens 
1404fa9e4066Sahrens 		zpool_close(zhp);
1405fa9e4066Sahrens 	}
1406fa9e4066Sahrens 
1407fa9e4066Sahrens 	return (ret);
1408fa9e4066Sahrens }
1409fa9e4066Sahrens 
1410fa9e4066Sahrens /*
1411fa9e4066Sahrens  * Given a vdev configuration, determine the maximum width needed for the device
1412fa9e4066Sahrens  * name column.
1413fa9e4066Sahrens  */
1414fa9e4066Sahrens static int
1415c67d9675Seschrock max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1416fa9e4066Sahrens {
141788ecc943SGeorge Wilson 	char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1418fa9e4066Sahrens 	nvlist_t **child;
1419fa9e4066Sahrens 	uint_t c, children;
1420fa9e4066Sahrens 	int ret;
1421fa9e4066Sahrens 
1422fa9e4066Sahrens 	if (strlen(name) + depth > max)
1423fa9e4066Sahrens 		max = strlen(name) + depth;
1424fa9e4066Sahrens 
1425afefbcddSeschrock 	free(name);
1426afefbcddSeschrock 
142799653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
142899653d4eSeschrock 	    &child, &children) == 0) {
142999653d4eSeschrock 		for (c = 0; c < children; c++)
143099653d4eSeschrock 			if ((ret = max_width(zhp, child[c], depth + 2,
143199653d4eSeschrock 			    max)) > max)
143299653d4eSeschrock 				max = ret;
143399653d4eSeschrock 	}
143499653d4eSeschrock 
1435fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1436fa94a07fSbrendan 	    &child, &children) == 0) {
1437fa94a07fSbrendan 		for (c = 0; c < children; c++)
1438fa94a07fSbrendan 			if ((ret = max_width(zhp, child[c], depth + 2,
1439fa94a07fSbrendan 			    max)) > max)
1440fa94a07fSbrendan 				max = ret;
1441fa94a07fSbrendan 	}
1442fa94a07fSbrendan 
1443fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
144499653d4eSeschrock 	    &child, &children) == 0) {
144599653d4eSeschrock 		for (c = 0; c < children; c++)
144699653d4eSeschrock 			if ((ret = max_width(zhp, child[c], depth + 2,
144799653d4eSeschrock 			    max)) > max)
144899653d4eSeschrock 				max = ret;
144999653d4eSeschrock 	}
1450fa9e4066Sahrens 
1451fa9e4066Sahrens 
1452fa9e4066Sahrens 	return (max);
1453fa9e4066Sahrens }
1454fa9e4066Sahrens 
1455e6ca193dSGeorge Wilson typedef struct spare_cbdata {
1456e6ca193dSGeorge Wilson 	uint64_t	cb_guid;
1457e6ca193dSGeorge Wilson 	zpool_handle_t	*cb_zhp;
1458e6ca193dSGeorge Wilson } spare_cbdata_t;
1459e6ca193dSGeorge Wilson 
1460e6ca193dSGeorge Wilson static boolean_t
1461e6ca193dSGeorge Wilson find_vdev(nvlist_t *nv, uint64_t search)
1462e6ca193dSGeorge Wilson {
1463e6ca193dSGeorge Wilson 	uint64_t guid;
1464e6ca193dSGeorge Wilson 	nvlist_t **child;
1465e6ca193dSGeorge Wilson 	uint_t c, children;
1466e6ca193dSGeorge Wilson 
1467e6ca193dSGeorge Wilson 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1468e6ca193dSGeorge Wilson 	    search == guid)
1469e6ca193dSGeorge Wilson 		return (B_TRUE);
1470e6ca193dSGeorge Wilson 
1471e6ca193dSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1472e6ca193dSGeorge Wilson 	    &child, &children) == 0) {
1473e6ca193dSGeorge Wilson 		for (c = 0; c < children; c++)
1474e6ca193dSGeorge Wilson 			if (find_vdev(child[c], search))
1475e6ca193dSGeorge Wilson 				return (B_TRUE);
1476e6ca193dSGeorge Wilson 	}
1477e6ca193dSGeorge Wilson 
1478e6ca193dSGeorge Wilson 	return (B_FALSE);
1479e6ca193dSGeorge Wilson }
1480e6ca193dSGeorge Wilson 
1481e6ca193dSGeorge Wilson static int
1482e6ca193dSGeorge Wilson find_spare(zpool_handle_t *zhp, void *data)
1483e6ca193dSGeorge Wilson {
1484e6ca193dSGeorge Wilson 	spare_cbdata_t *cbp = data;
1485e6ca193dSGeorge Wilson 	nvlist_t *config, *nvroot;
1486e6ca193dSGeorge Wilson 
1487e6ca193dSGeorge Wilson 	config = zpool_get_config(zhp, NULL);
1488e6ca193dSGeorge Wilson 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1489e6ca193dSGeorge Wilson 	    &nvroot) == 0);
1490e6ca193dSGeorge Wilson 
1491e6ca193dSGeorge Wilson 	if (find_vdev(nvroot, cbp->cb_guid)) {
1492e6ca193dSGeorge Wilson 		cbp->cb_zhp = zhp;
1493e6ca193dSGeorge Wilson 		return (1);
1494e6ca193dSGeorge Wilson 	}
1495e6ca193dSGeorge Wilson 
1496e6ca193dSGeorge Wilson 	zpool_close(zhp);
1497e6ca193dSGeorge Wilson 	return (0);
1498e6ca193dSGeorge Wilson }
1499e6ca193dSGeorge Wilson 
1500e6ca193dSGeorge Wilson /*
1501e6ca193dSGeorge Wilson  * Print out configuration state as requested by status_callback.
1502e6ca193dSGeorge Wilson  */
1503e6ca193dSGeorge Wilson void
1504e6ca193dSGeorge Wilson print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1505e6ca193dSGeorge Wilson     int namewidth, int depth, boolean_t isspare)
1506e6ca193dSGeorge Wilson {
1507e6ca193dSGeorge Wilson 	nvlist_t **child;
1508e6ca193dSGeorge Wilson 	uint_t c, children;
15093f9d6ad7SLin Ling 	pool_scan_stat_t *ps = NULL;
1510e6ca193dSGeorge Wilson 	vdev_stat_t *vs;
15113f9d6ad7SLin Ling 	char rbuf[6], wbuf[6], cbuf[6];
1512e6ca193dSGeorge Wilson 	char *vname;
1513e6ca193dSGeorge Wilson 	uint64_t notpresent;
1514e6ca193dSGeorge Wilson 	spare_cbdata_t cb;
15156401734dSWill Andrews 	const char *state;
15165cabbc6bSPrashanth Sreenivasa 	char *type;
1517e6ca193dSGeorge Wilson 
1518e6ca193dSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1519e6ca193dSGeorge Wilson 	    &child, &children) != 0)
1520e6ca193dSGeorge Wilson 		children = 0;
1521e6ca193dSGeorge Wilson 
15223f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
15233f9d6ad7SLin Ling 	    (uint64_t **)&vs, &c) == 0);
15243f9d6ad7SLin Ling 
15255cabbc6bSPrashanth Sreenivasa 	verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
15265cabbc6bSPrashanth Sreenivasa 
15275cabbc6bSPrashanth Sreenivasa 	if (strcmp(type, VDEV_TYPE_INDIRECT) == 0)
15285cabbc6bSPrashanth Sreenivasa 		return;
15295cabbc6bSPrashanth Sreenivasa 
1530e6ca193dSGeorge Wilson 	state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1531e6ca193dSGeorge Wilson 	if (isspare) {
1532e6ca193dSGeorge Wilson 		/*
1533e6ca193dSGeorge Wilson 		 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1534e6ca193dSGeorge Wilson 		 * online drives.
1535e6ca193dSGeorge Wilson 		 */
1536e6ca193dSGeorge Wilson 		if (vs->vs_aux == VDEV_AUX_SPARED)
1537e6ca193dSGeorge Wilson 			state = "INUSE";
1538e6ca193dSGeorge Wilson 		else if (vs->vs_state == VDEV_STATE_HEALTHY)
1539e6ca193dSGeorge Wilson 			state = "AVAIL";
1540e6ca193dSGeorge Wilson 	}
1541e6ca193dSGeorge Wilson 
1542e6ca193dSGeorge Wilson 	(void) printf("\t%*s%-*s  %-8s", depth, "", namewidth - depth,
1543e6ca193dSGeorge Wilson 	    name, state);
1544e6ca193dSGeorge Wilson 
1545e6ca193dSGeorge Wilson 	if (!isspare) {
1546e6ca193dSGeorge Wilson 		zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1547e6ca193dSGeorge Wilson 		zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1548e6ca193dSGeorge Wilson 		zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1549e6ca193dSGeorge Wilson 		(void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1550e6ca193dSGeorge Wilson 	}
1551e6ca193dSGeorge Wilson 
1552e6ca193dSGeorge Wilson 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1553e6ca193dSGeorge Wilson 	    &notpresent) == 0) {
1554e6ca193dSGeorge Wilson 		char *path;
1555e6ca193dSGeorge Wilson 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1556e6ca193dSGeorge Wilson 		(void) printf("  was %s", path);
1557e6ca193dSGeorge Wilson 	} else if (vs->vs_aux != 0) {
1558e6ca193dSGeorge Wilson 		(void) printf("  ");
1559e6ca193dSGeorge Wilson 
1560e6ca193dSGeorge Wilson 		switch (vs->vs_aux) {
1561e6ca193dSGeorge Wilson 		case VDEV_AUX_OPEN_FAILED:
1562e6ca193dSGeorge Wilson 			(void) printf(gettext("cannot open"));
1563e6ca193dSGeorge Wilson 			break;
1564e6ca193dSGeorge Wilson 
1565e6ca193dSGeorge Wilson 		case VDEV_AUX_BAD_GUID_SUM:
1566e6ca193dSGeorge Wilson 			(void) printf(gettext("missing device"));
1567e6ca193dSGeorge Wilson 			break;
1568e6ca193dSGeorge Wilson 
1569e6ca193dSGeorge Wilson 		case VDEV_AUX_NO_REPLICAS:
1570e6ca193dSGeorge Wilson 			(void) printf(gettext("insufficient replicas"));
1571e6ca193dSGeorge Wilson 			break;
1572e6ca193dSGeorge Wilson 
1573e6ca193dSGeorge Wilson 		case VDEV_AUX_VERSION_NEWER:
1574e6ca193dSGeorge Wilson 			(void) printf(gettext("newer version"));
1575e6ca193dSGeorge Wilson 			break;
1576e6ca193dSGeorge Wilson 
1577ad135b5dSChristopher Siden 		case VDEV_AUX_UNSUP_FEAT:
1578ad135b5dSChristopher Siden 			(void) printf(gettext("unsupported feature(s)"));
1579ad135b5dSChristopher Siden 			break;
1580ad135b5dSChristopher Siden 
1581e6ca193dSGeorge Wilson 		case VDEV_AUX_SPARED:
1582e6ca193dSGeorge Wilson 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1583e6ca193dSGeorge Wilson 			    &cb.cb_guid) == 0);
1584e6ca193dSGeorge Wilson 			if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1585e6ca193dSGeorge Wilson 				if (strcmp(zpool_get_name(cb.cb_zhp),
1586e6ca193dSGeorge Wilson 				    zpool_get_name(zhp)) == 0)
1587e6ca193dSGeorge Wilson 					(void) printf(gettext("currently in "
1588e6ca193dSGeorge Wilson 					    "use"));
1589e6ca193dSGeorge Wilson 				else
1590e6ca193dSGeorge Wilson 					(void) printf(gettext("in use by "
1591e6ca193dSGeorge Wilson 					    "pool '%s'"),
1592e6ca193dSGeorge Wilson 					    zpool_get_name(cb.cb_zhp));
1593e6ca193dSGeorge Wilson 				zpool_close(cb.cb_zhp);
1594e6ca193dSGeorge Wilson 			} else {
1595e6ca193dSGeorge Wilson 				(void) printf(gettext("currently in use"));
1596e6ca193dSGeorge Wilson 			}
1597e6ca193dSGeorge Wilson 			break;
1598e6ca193dSGeorge Wilson 
1599e6ca193dSGeorge Wilson 		case VDEV_AUX_ERR_EXCEEDED:
1600e6ca193dSGeorge Wilson 			(void) printf(gettext("too many errors"));
1601e6ca193dSGeorge Wilson 			break;
1602e6ca193dSGeorge Wilson 
1603e6ca193dSGeorge Wilson 		case VDEV_AUX_IO_FAILURE:
1604e6ca193dSGeorge Wilson 			(void) printf(gettext("experienced I/O failures"));
1605e6ca193dSGeorge Wilson 			break;
1606e6ca193dSGeorge Wilson 
1607e6ca193dSGeorge Wilson 		case VDEV_AUX_BAD_LOG:
1608e6ca193dSGeorge Wilson 			(void) printf(gettext("bad intent log"));
1609e6ca193dSGeorge Wilson 			break;
1610e6ca193dSGeorge Wilson 
1611069f55e2SEric Schrock 		case VDEV_AUX_EXTERNAL:
1612069f55e2SEric Schrock 			(void) printf(gettext("external device fault"));
1613069f55e2SEric Schrock 			break;
1614069f55e2SEric Schrock 
16151195e687SMark J Musante 		case VDEV_AUX_SPLIT_POOL:
16161195e687SMark J Musante 			(void) printf(gettext("split into new pool"));
16171195e687SMark J Musante 			break;
16181195e687SMark J Musante 
1619*e0f1c0afSOlaf Faaland 		case VDEV_AUX_ACTIVE:
1620*e0f1c0afSOlaf Faaland 			(void) printf(gettext("currently in use"));
1621*e0f1c0afSOlaf Faaland 			break;
1622*e0f1c0afSOlaf Faaland 
16236f793812SPavel Zakharov 		case VDEV_AUX_CHILDREN_OFFLINE:
16246f793812SPavel Zakharov 			(void) printf(gettext("all children offline"));
16256f793812SPavel Zakharov 			break;
16266f793812SPavel Zakharov 
1627e6ca193dSGeorge Wilson 		default:
1628e6ca193dSGeorge Wilson 			(void) printf(gettext("corrupted data"));
1629e6ca193dSGeorge Wilson 			break;
1630e6ca193dSGeorge Wilson 		}
16313f9d6ad7SLin Ling 	}
16323f9d6ad7SLin Ling 
16333f9d6ad7SLin Ling 	(void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
16343f9d6ad7SLin Ling 	    (uint64_t **)&ps, &c);
16353f9d6ad7SLin Ling 
16363f9d6ad7SLin Ling 	if (ps && ps->pss_state == DSS_SCANNING &&
16373f9d6ad7SLin Ling 	    vs->vs_scan_processed != 0 && children == 0) {
16383f9d6ad7SLin Ling 		(void) printf(gettext("  (%s)"),
16393f9d6ad7SLin Ling 		    (ps->pss_func == POOL_SCAN_RESILVER) ?
16403f9d6ad7SLin Ling 		    "resilvering" : "repairing");
1641e6ca193dSGeorge Wilson 	}
1642e6ca193dSGeorge Wilson 
1643094e47e9SGeorge Wilson 	if ((vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE ||
1644094e47e9SGeorge Wilson 	    vs->vs_initialize_state == VDEV_INITIALIZE_SUSPENDED ||
1645094e47e9SGeorge Wilson 	    vs->vs_initialize_state == VDEV_INITIALIZE_COMPLETE) &&
1646094e47e9SGeorge Wilson 	    !vs->vs_scan_removing) {
1647094e47e9SGeorge Wilson 		char zbuf[1024];
1648094e47e9SGeorge Wilson 		char tbuf[256];
1649094e47e9SGeorge Wilson 		struct tm zaction_ts;
1650094e47e9SGeorge Wilson 
1651094e47e9SGeorge Wilson 		time_t t = vs->vs_initialize_action_time;
1652094e47e9SGeorge Wilson 		int initialize_pct = 100;
1653094e47e9SGeorge Wilson 		if (vs->vs_initialize_state != VDEV_INITIALIZE_COMPLETE) {
1654094e47e9SGeorge Wilson 			initialize_pct = (vs->vs_initialize_bytes_done * 100 /
1655094e47e9SGeorge Wilson 			    (vs->vs_initialize_bytes_est + 1));
1656094e47e9SGeorge Wilson 		}
1657094e47e9SGeorge Wilson 
1658094e47e9SGeorge Wilson 		(void) localtime_r(&t, &zaction_ts);
1659094e47e9SGeorge Wilson 		(void) strftime(tbuf, sizeof (tbuf), "%c", &zaction_ts);
1660094e47e9SGeorge Wilson 
1661094e47e9SGeorge Wilson 		switch (vs->vs_initialize_state) {
1662094e47e9SGeorge Wilson 		case VDEV_INITIALIZE_SUSPENDED:
1663094e47e9SGeorge Wilson 			(void) snprintf(zbuf, sizeof (zbuf),
1664094e47e9SGeorge Wilson 			    ", suspended, started at %s", tbuf);
1665094e47e9SGeorge Wilson 			break;
1666094e47e9SGeorge Wilson 		case VDEV_INITIALIZE_ACTIVE:
1667094e47e9SGeorge Wilson 			(void) snprintf(zbuf, sizeof (zbuf),
1668094e47e9SGeorge Wilson 			    ", started at %s", tbuf);
1669094e47e9SGeorge Wilson 			break;
1670094e47e9SGeorge Wilson 		case VDEV_INITIALIZE_COMPLETE:
1671094e47e9SGeorge Wilson 			(void) snprintf(zbuf, sizeof (zbuf),
1672094e47e9SGeorge Wilson 			    ", completed at %s", tbuf);
1673094e47e9SGeorge Wilson 			break;
1674094e47e9SGeorge Wilson 		}
1675094e47e9SGeorge Wilson 
1676094e47e9SGeorge Wilson 		(void) printf(gettext("  (%d%% initialized%s)"),
1677094e47e9SGeorge Wilson 		    initialize_pct, zbuf);
1678094e47e9SGeorge Wilson 	}
1679094e47e9SGeorge Wilson 
1680e6ca193dSGeorge Wilson 	(void) printf("\n");
1681e6ca193dSGeorge Wilson 
1682e6ca193dSGeorge Wilson 	for (c = 0; c < children; c++) {
168388ecc943SGeorge Wilson 		uint64_t islog = B_FALSE, ishole = B_FALSE;
1684e6ca193dSGeorge Wilson 
168588ecc943SGeorge Wilson 		/* Don't print logs or holes here */
1686e6ca193dSGeorge Wilson 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
168788ecc943SGeorge Wilson 		    &islog);
168888ecc943SGeorge Wilson 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
168988ecc943SGeorge Wilson 		    &ishole);
169088ecc943SGeorge Wilson 		if (islog || ishole)
1691e6ca193dSGeorge Wilson 			continue;
169288ecc943SGeorge Wilson 		vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1693e6ca193dSGeorge Wilson 		print_status_config(zhp, vname, child[c],
1694e6ca193dSGeorge Wilson 		    namewidth, depth + 2, isspare);
1695e6ca193dSGeorge Wilson 		free(vname);
1696e6ca193dSGeorge Wilson 	}
1697e6ca193dSGeorge Wilson }
1698e6ca193dSGeorge Wilson 
1699fa9e4066Sahrens 
1700fa9e4066Sahrens /*
1701fa9e4066Sahrens  * Print the configuration of an exported pool.  Iterate over all vdevs in the
1702fa9e4066Sahrens  * pool, printing out the name and status for each one.
1703fa9e4066Sahrens  */
1704fa9e4066Sahrens void
1705e6ca193dSGeorge Wilson print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1706fa9e4066Sahrens {
1707fa9e4066Sahrens 	nvlist_t **child;
1708fa9e4066Sahrens 	uint_t c, children;
1709fa9e4066Sahrens 	vdev_stat_t *vs;
1710afefbcddSeschrock 	char *type, *vname;
1711fa9e4066Sahrens 
1712fa9e4066Sahrens 	verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
171388ecc943SGeorge Wilson 	if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
171488ecc943SGeorge Wilson 	    strcmp(type, VDEV_TYPE_HOLE) == 0)
1715fa9e4066Sahrens 		return;
1716fa9e4066Sahrens 
17173f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1718fa9e4066Sahrens 	    (uint64_t **)&vs, &c) == 0);
1719fa9e4066Sahrens 
1720fa9e4066Sahrens 	(void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1721990b4856Slling 	(void) printf("  %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1722fa9e4066Sahrens 
1723fa9e4066Sahrens 	if (vs->vs_aux != 0) {
17243d7072f8Seschrock 		(void) printf("  ");
1725fa9e4066Sahrens 
1726fa9e4066Sahrens 		switch (vs->vs_aux) {
1727fa9e4066Sahrens 		case VDEV_AUX_OPEN_FAILED:
1728fa9e4066Sahrens 			(void) printf(gettext("cannot open"));
1729fa9e4066Sahrens 			break;
1730fa9e4066Sahrens 
1731fa9e4066Sahrens 		case VDEV_AUX_BAD_GUID_SUM:
1732fa9e4066Sahrens 			(void) printf(gettext("missing device"));
1733fa9e4066Sahrens 			break;
1734fa9e4066Sahrens 
1735fa9e4066Sahrens 		case VDEV_AUX_NO_REPLICAS:
1736fa9e4066Sahrens 			(void) printf(gettext("insufficient replicas"));
1737fa9e4066Sahrens 			break;
1738fa9e4066Sahrens 
1739eaca9bbdSeschrock 		case VDEV_AUX_VERSION_NEWER:
1740eaca9bbdSeschrock 			(void) printf(gettext("newer version"));
1741eaca9bbdSeschrock 			break;
1742eaca9bbdSeschrock 
1743ad135b5dSChristopher Siden 		case VDEV_AUX_UNSUP_FEAT:
1744ad135b5dSChristopher Siden 			(void) printf(gettext("unsupported feature(s)"));
1745ad135b5dSChristopher Siden 			break;
1746ad135b5dSChristopher Siden 
17473d7072f8Seschrock 		case VDEV_AUX_ERR_EXCEEDED:
17483d7072f8Seschrock 			(void) printf(gettext("too many errors"));
17493d7072f8Seschrock 			break;
17503d7072f8Seschrock 
1751*e0f1c0afSOlaf Faaland 		case VDEV_AUX_ACTIVE:
1752*e0f1c0afSOlaf Faaland 			(void) printf(gettext("currently in use"));
1753*e0f1c0afSOlaf Faaland 			break;
1754*e0f1c0afSOlaf Faaland 
17556f793812SPavel Zakharov 		case VDEV_AUX_CHILDREN_OFFLINE:
17566f793812SPavel Zakharov 			(void) printf(gettext("all children offline"));
17576f793812SPavel Zakharov 			break;
17586f793812SPavel Zakharov 
1759fa9e4066Sahrens 		default:
1760fa9e4066Sahrens 			(void) printf(gettext("corrupted data"));
1761fa9e4066Sahrens 			break;
1762fa9e4066Sahrens 		}
1763fa9e4066Sahrens 	}
1764fa9e4066Sahrens 	(void) printf("\n");
1765fa9e4066Sahrens 
1766fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1767fa9e4066Sahrens 	    &child, &children) != 0)
1768fa9e4066Sahrens 		return;
1769fa9e4066Sahrens 
1770afefbcddSeschrock 	for (c = 0; c < children; c++) {
17718654d025Sperrin 		uint64_t is_log = B_FALSE;
17728654d025Sperrin 
17738654d025Sperrin 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
17748654d025Sperrin 		    &is_log);
1775e6ca193dSGeorge Wilson 		if (is_log)
17768654d025Sperrin 			continue;
17778654d025Sperrin 
177888ecc943SGeorge Wilson 		vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1779e6ca193dSGeorge Wilson 		print_import_config(vname, child[c], namewidth, depth + 2);
1780afefbcddSeschrock 		free(vname);
1781afefbcddSeschrock 	}
178299653d4eSeschrock 
1783fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1784fa94a07fSbrendan 	    &child, &children) == 0) {
1785fa94a07fSbrendan 		(void) printf(gettext("\tcache\n"));
1786fa94a07fSbrendan 		for (c = 0; c < children; c++) {
178788ecc943SGeorge Wilson 			vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1788fa94a07fSbrendan 			(void) printf("\t  %s\n", vname);
1789fa94a07fSbrendan 			free(vname);
1790fa94a07fSbrendan 		}
1791fa94a07fSbrendan 	}
179299653d4eSeschrock 
1793fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1794fa94a07fSbrendan 	    &child, &children) == 0) {
1795fa94a07fSbrendan 		(void) printf(gettext("\tspares\n"));
1796fa94a07fSbrendan 		for (c = 0; c < children; c++) {
179788ecc943SGeorge Wilson 			vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1798fa94a07fSbrendan 			(void) printf("\t  %s\n", vname);
1799fa94a07fSbrendan 			free(vname);
1800fa94a07fSbrendan 		}
180199653d4eSeschrock 	}
1802fa9e4066Sahrens }
1803fa9e4066Sahrens 
1804e6ca193dSGeorge Wilson /*
1805e6ca193dSGeorge Wilson  * Print log vdevs.
1806e6ca193dSGeorge Wilson  * Logs are recorded as top level vdevs in the main pool child array
1807e6ca193dSGeorge Wilson  * but with "is_log" set to 1. We use either print_status_config() or
1808e6ca193dSGeorge Wilson  * print_import_config() to print the top level logs then any log
1809e6ca193dSGeorge Wilson  * children (eg mirrored slogs) are printed recursively - which
1810e6ca193dSGeorge Wilson  * works because only the top level vdev is marked "is_log"
1811e6ca193dSGeorge Wilson  */
1812e6ca193dSGeorge Wilson static void
1813e6ca193dSGeorge Wilson print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1814e6ca193dSGeorge Wilson {
1815e6ca193dSGeorge Wilson 	uint_t c, children;
1816e6ca193dSGeorge Wilson 	nvlist_t **child;
1817e6ca193dSGeorge Wilson 
1818e6ca193dSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1819e6ca193dSGeorge Wilson 	    &children) != 0)
1820e6ca193dSGeorge Wilson 		return;
1821e6ca193dSGeorge Wilson 
1822e6ca193dSGeorge Wilson 	(void) printf(gettext("\tlogs\n"));
1823e6ca193dSGeorge Wilson 
1824e6ca193dSGeorge Wilson 	for (c = 0; c < children; c++) {
1825e6ca193dSGeorge Wilson 		uint64_t is_log = B_FALSE;
1826e6ca193dSGeorge Wilson 		char *name;
1827e6ca193dSGeorge Wilson 
1828e6ca193dSGeorge Wilson 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1829e6ca193dSGeorge Wilson 		    &is_log);
1830e6ca193dSGeorge Wilson 		if (!is_log)
1831e6ca193dSGeorge Wilson 			continue;
183288ecc943SGeorge Wilson 		name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1833e6ca193dSGeorge Wilson 		if (verbose)
1834e6ca193dSGeorge Wilson 			print_status_config(zhp, name, child[c], namewidth,
1835e6ca193dSGeorge Wilson 			    2, B_FALSE);
1836e6ca193dSGeorge Wilson 		else
1837e6ca193dSGeorge Wilson 			print_import_config(name, child[c], namewidth, 2);
1838e6ca193dSGeorge Wilson 		free(name);
1839e6ca193dSGeorge Wilson 	}
1840e6ca193dSGeorge Wilson }
1841468c413aSTim Haley 
1842fa9e4066Sahrens /*
1843fa9e4066Sahrens  * Display the status for the given pool.
1844fa9e4066Sahrens  */
1845fa9e4066Sahrens static void
1846fa9e4066Sahrens show_import(nvlist_t *config)
1847fa9e4066Sahrens {
1848fa9e4066Sahrens 	uint64_t pool_state;
1849fa9e4066Sahrens 	vdev_stat_t *vs;
1850fa9e4066Sahrens 	char *name;
1851fa9e4066Sahrens 	uint64_t guid;
1852*e0f1c0afSOlaf Faaland 	uint64_t hostid = 0;
1853fa9e4066Sahrens 	char *msgid;
1854*e0f1c0afSOlaf Faaland 	char *hostname = "unknown";
1855*e0f1c0afSOlaf Faaland 	nvlist_t *nvroot, *nvinfo;
1856fa9e4066Sahrens 	int reason;
185746657f8dSmmusante 	const char *health;
1858fa9e4066Sahrens 	uint_t vsc;
1859fa9e4066Sahrens 	int namewidth;
18608704186eSDan McDonald 	char *comment;
1861fa9e4066Sahrens 
1862fa9e4066Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1863fa9e4066Sahrens 	    &name) == 0);
1864fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1865fa9e4066Sahrens 	    &guid) == 0);
1866fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1867fa9e4066Sahrens 	    &pool_state) == 0);
1868fa9e4066Sahrens 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1869fa9e4066Sahrens 	    &nvroot) == 0);
1870fa9e4066Sahrens 
18713f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1872fa9e4066Sahrens 	    (uint64_t **)&vs, &vsc) == 0);
1873990b4856Slling 	health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1874fa9e4066Sahrens 
1875fa9e4066Sahrens 	reason = zpool_import_status(config, &msgid);
1876fa9e4066Sahrens 
18778704186eSDan McDonald 	(void) printf(gettext("   pool: %s\n"), name);
18788704186eSDan McDonald 	(void) printf(gettext("     id: %llu\n"), (u_longlong_t)guid);
18798704186eSDan McDonald 	(void) printf(gettext("  state: %s"), health);
18804c58d714Sdarrenm 	if (pool_state == POOL_STATE_DESTROYED)
1881b1b8ab34Slling 		(void) printf(gettext(" (DESTROYED)"));
18824c58d714Sdarrenm 	(void) printf("\n");
1883fa9e4066Sahrens 
1884fa9e4066Sahrens 	switch (reason) {
1885fa9e4066Sahrens 	case ZPOOL_STATUS_MISSING_DEV_R:
1886fa9e4066Sahrens 	case ZPOOL_STATUS_MISSING_DEV_NR:
1887fa9e4066Sahrens 	case ZPOOL_STATUS_BAD_GUID_SUM:
18888704186eSDan McDonald 		(void) printf(gettext(" status: One or more devices are "
18898704186eSDan McDonald 		    "missing from the system.\n"));
1890fa9e4066Sahrens 		break;
1891fa9e4066Sahrens 
1892fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_LABEL_R:
1893fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_LABEL_NR:
18948704186eSDan McDonald 		(void) printf(gettext(" status: One or more devices contains "
1895fa9e4066Sahrens 		    "corrupted data.\n"));
1896fa9e4066Sahrens 		break;
1897fa9e4066Sahrens 
1898fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_DATA:
18998704186eSDan McDonald 		(void) printf(
19008704186eSDan McDonald 		    gettext(" status: The pool data is corrupted.\n"));
1901fa9e4066Sahrens 		break;
1902fa9e4066Sahrens 
1903441d80aaSlling 	case ZPOOL_STATUS_OFFLINE_DEV:
19048704186eSDan McDonald 		(void) printf(gettext(" status: One or more devices "
1905441d80aaSlling 		    "are offlined.\n"));
1906441d80aaSlling 		break;
1907441d80aaSlling 
1908ea8dc4b6Seschrock 	case ZPOOL_STATUS_CORRUPT_POOL:
19098704186eSDan McDonald 		(void) printf(gettext(" status: The pool metadata is "
1910ea8dc4b6Seschrock 		    "corrupted.\n"));
1911ea8dc4b6Seschrock 		break;
1912ea8dc4b6Seschrock 
1913eaca9bbdSeschrock 	case ZPOOL_STATUS_VERSION_OLDER:
191457221772SChristopher Siden 		(void) printf(gettext(" status: The pool is formatted using a "
191557221772SChristopher Siden 		    "legacy on-disk version.\n"));
1916eaca9bbdSeschrock 		break;
1917eaca9bbdSeschrock 
1918eaca9bbdSeschrock 	case ZPOOL_STATUS_VERSION_NEWER:
19198704186eSDan McDonald 		(void) printf(gettext(" status: The pool is formatted using an "
1920eaca9bbdSeschrock 		    "incompatible version.\n"));
1921eaca9bbdSeschrock 		break;
1922b87f3af3Sperrin 
192357221772SChristopher Siden 	case ZPOOL_STATUS_FEAT_DISABLED:
192457221772SChristopher Siden 		(void) printf(gettext(" status: Some supported features are "
192557221772SChristopher Siden 		    "not enabled on the pool.\n"));
192657221772SChristopher Siden 		break;
192757221772SChristopher Siden 
1928ad135b5dSChristopher Siden 	case ZPOOL_STATUS_UNSUP_FEAT_READ:
1929ad135b5dSChristopher Siden 		(void) printf(gettext("status: The pool uses the following "
1930edc8ef7dSToomas Soome 		    "feature(s) not supported on this system:\n"));
1931ad135b5dSChristopher Siden 		zpool_print_unsup_feat(config);
1932ad135b5dSChristopher Siden 		break;
1933ad135b5dSChristopher Siden 
1934ad135b5dSChristopher Siden 	case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1935ad135b5dSChristopher Siden 		(void) printf(gettext("status: The pool can only be accessed "
1936ad135b5dSChristopher Siden 		    "in read-only mode on this system. It\n\tcannot be "
1937ad135b5dSChristopher Siden 		    "accessed in read-write mode because it uses the "
1938ad135b5dSChristopher Siden 		    "following\n\tfeature(s) not supported on this system:\n"));
1939ad135b5dSChristopher Siden 		zpool_print_unsup_feat(config);
1940ad135b5dSChristopher Siden 		break;
1941ad135b5dSChristopher Siden 
1942*e0f1c0afSOlaf Faaland 	case ZPOOL_STATUS_HOSTID_ACTIVE:
1943*e0f1c0afSOlaf Faaland 		(void) printf(gettext(" status: The pool is currently "
1944*e0f1c0afSOlaf Faaland 		    "imported by another system.\n"));
1945*e0f1c0afSOlaf Faaland 		break;
1946*e0f1c0afSOlaf Faaland 
1947*e0f1c0afSOlaf Faaland 	case ZPOOL_STATUS_HOSTID_REQUIRED:
1948*e0f1c0afSOlaf Faaland 		(void) printf(gettext(" status: The pool has the "
1949*e0f1c0afSOlaf Faaland 		    "multihost property on.  It cannot\n\tbe safely imported "
1950*e0f1c0afSOlaf Faaland 		    "when the system hostid is not set.\n"));
1951*e0f1c0afSOlaf Faaland 		break;
1952*e0f1c0afSOlaf Faaland 
195395173954Sek 	case ZPOOL_STATUS_HOSTID_MISMATCH:
19548704186eSDan McDonald 		(void) printf(gettext(" status: The pool was last accessed by "
195595173954Sek 		    "another system.\n"));
195695173954Sek 		break;
1957b87f3af3Sperrin 
19583d7072f8Seschrock 	case ZPOOL_STATUS_FAULTED_DEV_R:
19593d7072f8Seschrock 	case ZPOOL_STATUS_FAULTED_DEV_NR:
19608704186eSDan McDonald 		(void) printf(gettext(" status: One or more devices are "
19613d7072f8Seschrock 		    "faulted.\n"));
19623d7072f8Seschrock 		break;
19633d7072f8Seschrock 
1964b87f3af3Sperrin 	case ZPOOL_STATUS_BAD_LOG:
19658704186eSDan McDonald 		(void) printf(gettext(" status: An intent log record cannot be "
1966b87f3af3Sperrin 		    "read.\n"));
1967b87f3af3Sperrin 		break;
1968b87f3af3Sperrin 
19693f9d6ad7SLin Ling 	case ZPOOL_STATUS_RESILVERING:
19708704186eSDan McDonald 		(void) printf(gettext(" status: One or more devices were being "
19713f9d6ad7SLin Ling 		    "resilvered.\n"));
19723f9d6ad7SLin Ling 		break;
19733f9d6ad7SLin Ling 
1974fa9e4066Sahrens 	default:
1975fa9e4066Sahrens 		/*
1976fa9e4066Sahrens 		 * No other status can be seen when importing pools.
1977fa9e4066Sahrens 		 */
1978fa9e4066Sahrens 		assert(reason == ZPOOL_STATUS_OK);
1979fa9e4066Sahrens 	}
1980fa9e4066Sahrens 
1981fa9e4066Sahrens 	/*
1982fa9e4066Sahrens 	 * Print out an action according to the overall state of the pool.
1983fa9e4066Sahrens 	 */
198446657f8dSmmusante 	if (vs->vs_state == VDEV_STATE_HEALTHY) {
198557221772SChristopher Siden 		if (reason == ZPOOL_STATUS_VERSION_OLDER ||
198657221772SChristopher Siden 		    reason == ZPOOL_STATUS_FEAT_DISABLED) {
19878704186eSDan McDonald 			(void) printf(gettext(" action: The pool can be "
1988eaca9bbdSeschrock 			    "imported using its name or numeric identifier, "
1989eaca9bbdSeschrock 			    "though\n\tsome features will not be available "
1990eaca9bbdSeschrock 			    "without an explicit 'zpool upgrade'.\n"));
199157221772SChristopher Siden 		} else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
19928704186eSDan McDonald 			(void) printf(gettext(" action: The pool can be "
199395173954Sek 			    "imported using its name or numeric "
199495173954Sek 			    "identifier and\n\tthe '-f' flag.\n"));
199557221772SChristopher Siden 		} else {
19968704186eSDan McDonald 			(void) printf(gettext(" action: The pool can be "
1997eaca9bbdSeschrock 			    "imported using its name or numeric "
1998eaca9bbdSeschrock 			    "identifier.\n"));
199957221772SChristopher Siden 		}
200046657f8dSmmusante 	} else if (vs->vs_state == VDEV_STATE_DEGRADED) {
20018704186eSDan McDonald 		(void) printf(gettext(" action: The pool can be imported "
2002fa9e4066Sahrens 		    "despite missing or damaged devices.  The\n\tfault "
2003eaca9bbdSeschrock 		    "tolerance of the pool may be compromised if imported.\n"));
2004fa9e4066Sahrens 	} else {
2005eaca9bbdSeschrock 		switch (reason) {
2006eaca9bbdSeschrock 		case ZPOOL_STATUS_VERSION_NEWER:
20078704186eSDan McDonald 			(void) printf(gettext(" action: The pool cannot be "
2008eaca9bbdSeschrock 			    "imported.  Access the pool on a system running "
2009eaca9bbdSeschrock 			    "newer\n\tsoftware, or recreate the pool from "
2010eaca9bbdSeschrock 			    "backup.\n"));
2011eaca9bbdSeschrock 			break;
2012ad135b5dSChristopher Siden 		case ZPOOL_STATUS_UNSUP_FEAT_READ:
2013ad135b5dSChristopher Siden 			(void) printf(gettext("action: The pool cannot be "
2014ad135b5dSChristopher Siden 			    "imported. Access the pool on a system that "
2015ad135b5dSChristopher Siden 			    "supports\n\tthe required feature(s), or recreate "
2016ad135b5dSChristopher Siden 			    "the pool from backup.\n"));
2017ad135b5dSChristopher Siden 			break;
2018ad135b5dSChristopher Siden 		case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
2019ad135b5dSChristopher Siden 			(void) printf(gettext("action: The pool cannot be "
2020ad135b5dSChristopher Siden 			    "imported in read-write mode. Import the pool "
2021ad135b5dSChristopher Siden 			    "with\n"
2022ad135b5dSChristopher Siden 			    "\t\"-o readonly=on\", access the pool on a system "
2023ad135b5dSChristopher Siden 			    "that supports the\n\trequired feature(s), or "
2024ad135b5dSChristopher Siden 			    "recreate the pool from backup.\n"));
2025ad135b5dSChristopher Siden 			break;
2026eaca9bbdSeschrock 		case ZPOOL_STATUS_MISSING_DEV_R:
2027eaca9bbdSeschrock 		case ZPOOL_STATUS_MISSING_DEV_NR:
2028eaca9bbdSeschrock 		case ZPOOL_STATUS_BAD_GUID_SUM:
20298704186eSDan McDonald 			(void) printf(gettext(" action: The pool cannot be "
2030fa9e4066Sahrens 			    "imported. Attach the missing\n\tdevices and try "
2031fa9e4066Sahrens 			    "again.\n"));
2032eaca9bbdSeschrock 			break;
2033*e0f1c0afSOlaf Faaland 		case ZPOOL_STATUS_HOSTID_ACTIVE:
2034*e0f1c0afSOlaf Faaland 			VERIFY0(nvlist_lookup_nvlist(config,
2035*e0f1c0afSOlaf Faaland 			    ZPOOL_CONFIG_LOAD_INFO, &nvinfo));
2036*e0f1c0afSOlaf Faaland 
2037*e0f1c0afSOlaf Faaland 			if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
2038*e0f1c0afSOlaf Faaland 				hostname = fnvlist_lookup_string(nvinfo,
2039*e0f1c0afSOlaf Faaland 				    ZPOOL_CONFIG_MMP_HOSTNAME);
2040*e0f1c0afSOlaf Faaland 
2041*e0f1c0afSOlaf Faaland 			if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID))
2042*e0f1c0afSOlaf Faaland 				hostid = fnvlist_lookup_uint64(nvinfo,
2043*e0f1c0afSOlaf Faaland 				    ZPOOL_CONFIG_MMP_HOSTID);
2044*e0f1c0afSOlaf Faaland 
2045*e0f1c0afSOlaf Faaland 			(void) printf(gettext(" action: The pool must be "
2046*e0f1c0afSOlaf Faaland 			    "exported from %s (hostid=%lx)\n\tbefore it "
2047*e0f1c0afSOlaf Faaland 			    "can be safely imported.\n"), hostname,
2048*e0f1c0afSOlaf Faaland 			    (unsigned long) hostid);
2049*e0f1c0afSOlaf Faaland 			break;
2050*e0f1c0afSOlaf Faaland 		case ZPOOL_STATUS_HOSTID_REQUIRED:
2051*e0f1c0afSOlaf Faaland 			(void) printf(gettext(" action: Check the SMF "
2052*e0f1c0afSOlaf Faaland 			    "svc:/system/hostid service.\n"));
2053*e0f1c0afSOlaf Faaland 			break;
2054eaca9bbdSeschrock 		default:
20558704186eSDan McDonald 			(void) printf(gettext(" action: The pool cannot be "
2056fa9e4066Sahrens 			    "imported due to damaged devices or data.\n"));
2057eaca9bbdSeschrock 		}
2058eaca9bbdSeschrock 	}
2059eaca9bbdSeschrock 
20608704186eSDan McDonald 	/* Print the comment attached to the pool. */
20618704186eSDan McDonald 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
20628704186eSDan McDonald 		(void) printf(gettext("comment: %s\n"), comment);
20638704186eSDan McDonald 
206446657f8dSmmusante 	/*
206546657f8dSmmusante 	 * If the state is "closed" or "can't open", and the aux state
206646657f8dSmmusante 	 * is "corrupt data":
206746657f8dSmmusante 	 */
206846657f8dSmmusante 	if (((vs->vs_state == VDEV_STATE_CLOSED) ||
206946657f8dSmmusante 	    (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
207046657f8dSmmusante 	    (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
2071eaca9bbdSeschrock 		if (pool_state == POOL_STATE_DESTROYED)
2072eaca9bbdSeschrock 			(void) printf(gettext("\tThe pool was destroyed, "
2073eaca9bbdSeschrock 			    "but can be imported using the '-Df' flags.\n"));
2074eaca9bbdSeschrock 		else if (pool_state != POOL_STATE_EXPORTED)
2075eaca9bbdSeschrock 			(void) printf(gettext("\tThe pool may be active on "
207618ce54dfSek 			    "another system, but can be imported using\n\t"
2077eaca9bbdSeschrock 			    "the '-f' flag.\n"));
2078fa9e4066Sahrens 	}
2079fa9e4066Sahrens 
2080fa9e4066Sahrens 	if (msgid != NULL)
2081654b400cSJoshua M. Clulow 		(void) printf(gettext("   see: http://illumos.org/msg/%s\n"),
2082fa9e4066Sahrens 		    msgid);
2083fa9e4066Sahrens 
20848704186eSDan McDonald 	(void) printf(gettext(" config:\n\n"));
2085fa9e4066Sahrens 
2086c67d9675Seschrock 	namewidth = max_width(NULL, nvroot, 0, 0);
2087fa9e4066Sahrens 	if (namewidth < 10)
2088fa9e4066Sahrens 		namewidth = 10;
20898654d025Sperrin 
2090e6ca193dSGeorge Wilson 	print_import_config(name, nvroot, namewidth, 0);
2091e6ca193dSGeorge Wilson 	if (num_logs(nvroot) > 0)
2092e6ca193dSGeorge Wilson 		print_logs(NULL, nvroot, namewidth, B_FALSE);
2093fa9e4066Sahrens 
2094fa9e4066Sahrens 	if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
209546657f8dSmmusante 		(void) printf(gettext("\n\tAdditional devices are known to "
2096fa9e4066Sahrens 		    "be part of this pool, though their\n\texact "
209746657f8dSmmusante 		    "configuration cannot be determined.\n"));
2098fa9e4066Sahrens 	}
2099fa9e4066Sahrens }
2100fa9e4066Sahrens 
2101*e0f1c0afSOlaf Faaland static boolean_t
2102*e0f1c0afSOlaf Faaland zfs_force_import_required(nvlist_t *config)
2103*e0f1c0afSOlaf Faaland {
2104*e0f1c0afSOlaf Faaland 	uint64_t state;
2105*e0f1c0afSOlaf Faaland 	uint64_t hostid = 0;
2106*e0f1c0afSOlaf Faaland 	nvlist_t *nvinfo;
2107*e0f1c0afSOlaf Faaland 
2108*e0f1c0afSOlaf Faaland 	state = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE);
2109*e0f1c0afSOlaf Faaland 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, &hostid);
2110*e0f1c0afSOlaf Faaland 
2111*e0f1c0afSOlaf Faaland 	if (state != POOL_STATE_EXPORTED && hostid != get_system_hostid())
2112*e0f1c0afSOlaf Faaland 		return (B_TRUE);
2113*e0f1c0afSOlaf Faaland 
2114*e0f1c0afSOlaf Faaland 	nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
2115*e0f1c0afSOlaf Faaland 	if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE)) {
2116*e0f1c0afSOlaf Faaland 		mmp_state_t mmp_state = fnvlist_lookup_uint64(nvinfo,
2117*e0f1c0afSOlaf Faaland 		    ZPOOL_CONFIG_MMP_STATE);
2118*e0f1c0afSOlaf Faaland 
2119*e0f1c0afSOlaf Faaland 		if (mmp_state != MMP_STATE_INACTIVE)
2120*e0f1c0afSOlaf Faaland 			return (B_TRUE);
2121*e0f1c0afSOlaf Faaland 	}
2122*e0f1c0afSOlaf Faaland 
2123*e0f1c0afSOlaf Faaland 	return (B_FALSE);
2124*e0f1c0afSOlaf Faaland }
2125*e0f1c0afSOlaf Faaland 
2126fa9e4066Sahrens /*
2127fa9e4066Sahrens  * Perform the import for the given configuration.  This passes the heavy
2128990b4856Slling  * lifting off to zpool_import_props(), and then mounts the datasets contained
2129990b4856Slling  * within the pool.
2130fa9e4066Sahrens  */
2131fa9e4066Sahrens static int
2132fa9e4066Sahrens do_import(nvlist_t *config, const char *newname, const char *mntopts,
21334b964adaSGeorge Wilson     nvlist_t *props, int flags)
2134fa9e4066Sahrens {
2135fa9e4066Sahrens 	zpool_handle_t *zhp;
2136fa9e4066Sahrens 	char *name;
2137eaca9bbdSeschrock 	uint64_t version;
2138fa9e4066Sahrens 
2139*e0f1c0afSOlaf Faaland 	name = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME);
2140*e0f1c0afSOlaf Faaland 	version = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION);
2141fa9e4066Sahrens 
2142ad135b5dSChristopher Siden 	if (!SPA_VERSION_IS_SUPPORTED(version)) {
2143eaca9bbdSeschrock 		(void) fprintf(stderr, gettext("cannot import '%s': pool "
2144ad135b5dSChristopher Siden 		    "is formatted using an unsupported ZFS version\n"), name);
2145eaca9bbdSeschrock 		return (1);
2146*e0f1c0afSOlaf Faaland 	} else if (zfs_force_import_required(config) &&
21474b964adaSGeorge Wilson 	    !(flags & ZFS_IMPORT_ANY_HOST)) {
2148*e0f1c0afSOlaf Faaland 		mmp_state_t mmp_state = MMP_STATE_INACTIVE;
2149*e0f1c0afSOlaf Faaland 		nvlist_t *nvinfo;
2150*e0f1c0afSOlaf Faaland 
2151*e0f1c0afSOlaf Faaland 		nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
2152*e0f1c0afSOlaf Faaland 		if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE))
2153*e0f1c0afSOlaf Faaland 			mmp_state = fnvlist_lookup_uint64(nvinfo,
2154*e0f1c0afSOlaf Faaland 			    ZPOOL_CONFIG_MMP_STATE);
2155*e0f1c0afSOlaf Faaland 
2156*e0f1c0afSOlaf Faaland 		if (mmp_state == MMP_STATE_ACTIVE) {
2157*e0f1c0afSOlaf Faaland 			char *hostname = "<unknown>";
2158*e0f1c0afSOlaf Faaland 			uint64_t hostid = 0;
2159*e0f1c0afSOlaf Faaland 
2160*e0f1c0afSOlaf Faaland 			if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
2161*e0f1c0afSOlaf Faaland 				hostname = fnvlist_lookup_string(nvinfo,
2162*e0f1c0afSOlaf Faaland 				    ZPOOL_CONFIG_MMP_HOSTNAME);
2163*e0f1c0afSOlaf Faaland 
2164*e0f1c0afSOlaf Faaland 			if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID))
2165*e0f1c0afSOlaf Faaland 				hostid = fnvlist_lookup_uint64(nvinfo,
2166*e0f1c0afSOlaf Faaland 				    ZPOOL_CONFIG_MMP_HOSTID);
2167*e0f1c0afSOlaf Faaland 
2168*e0f1c0afSOlaf Faaland 			(void) fprintf(stderr, gettext("cannot import '%s': "
2169*e0f1c0afSOlaf Faaland 			    "pool is imported on %s (hostid: "
2170*e0f1c0afSOlaf Faaland 			    "0x%lx)\nExport the pool on the other system, "
2171*e0f1c0afSOlaf Faaland 			    "then run 'zpool import'.\n"),
2172*e0f1c0afSOlaf Faaland 			    name, hostname, (unsigned long) hostid);
2173*e0f1c0afSOlaf Faaland 		} else if (mmp_state == MMP_STATE_NO_HOSTID) {
2174*e0f1c0afSOlaf Faaland 			(void) fprintf(stderr, gettext("Cannot import '%s': "
2175*e0f1c0afSOlaf Faaland 			    "pool has the multihost property on and the\n"
2176*e0f1c0afSOlaf Faaland 			    "system's hostid is not set.\n"), name);
217795173954Sek 		} else {
2178*e0f1c0afSOlaf Faaland 			char *hostname = "<unknown>";
2179*e0f1c0afSOlaf Faaland 			uint64_t timestamp = 0;
2180*e0f1c0afSOlaf Faaland 			uint64_t hostid = 0;
2181*e0f1c0afSOlaf Faaland 
2182*e0f1c0afSOlaf Faaland 			if (nvlist_exists(config, ZPOOL_CONFIG_HOSTNAME))
2183*e0f1c0afSOlaf Faaland 				hostname = fnvlist_lookup_string(config,
2184*e0f1c0afSOlaf Faaland 				    ZPOOL_CONFIG_HOSTNAME);
2185*e0f1c0afSOlaf Faaland 
2186*e0f1c0afSOlaf Faaland 			if (nvlist_exists(config, ZPOOL_CONFIG_TIMESTAMP))
2187*e0f1c0afSOlaf Faaland 				timestamp = fnvlist_lookup_uint64(config,
2188*e0f1c0afSOlaf Faaland 				    ZPOOL_CONFIG_TIMESTAMP);
2189*e0f1c0afSOlaf Faaland 
2190*e0f1c0afSOlaf Faaland 			if (nvlist_exists(config, ZPOOL_CONFIG_HOSTID))
2191*e0f1c0afSOlaf Faaland 				hostid = fnvlist_lookup_uint64(config,
2192*e0f1c0afSOlaf Faaland 				    ZPOOL_CONFIG_HOSTID);
2193*e0f1c0afSOlaf Faaland 
219495173954Sek 			(void) fprintf(stderr, gettext("cannot import '%s': "
2195*e0f1c0afSOlaf Faaland 			    "pool was previously in use from another system.\n"
2196*e0f1c0afSOlaf Faaland 			    "Last accessed by %s (hostid=%lx) at %s"
2197*e0f1c0afSOlaf Faaland 			    "The pool can be imported, use 'zpool import -f' "
2198*e0f1c0afSOlaf Faaland 			    "to import the pool.\n"), name, hostname,
2199*e0f1c0afSOlaf Faaland 			    (unsigned long)hostid, ctime((time_t *)&timestamp));
2200*e0f1c0afSOlaf Faaland 
220195173954Sek 		}
2202*e0f1c0afSOlaf Faaland 
2203*e0f1c0afSOlaf Faaland 		return (1);
2204fa9e4066Sahrens 	}
2205fa9e4066Sahrens 
22064b964adaSGeorge Wilson 	if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
2207fa9e4066Sahrens 		return (1);
2208fa9e4066Sahrens 
2209fa9e4066Sahrens 	if (newname != NULL)
2210fa9e4066Sahrens 		name = (char *)newname;
2211fa9e4066Sahrens 
22124f0f5e5bSVictor Latushkin 	if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
22134f0f5e5bSVictor Latushkin 		return (1);
2214fa9e4066Sahrens 
2215379c004dSEric Schrock 	if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
2216f9af39baSGeorge Wilson 	    !(flags & ZFS_IMPORT_ONLY) &&
2217379c004dSEric Schrock 	    zpool_enable_datasets(zhp, mntopts, 0) != 0) {
2218fa9e4066Sahrens 		zpool_close(zhp);
2219fa9e4066Sahrens 		return (1);
2220fa9e4066Sahrens 	}
2221fa9e4066Sahrens 
2222fa9e4066Sahrens 	zpool_close(zhp);
2223468c413aSTim Haley 	return (0);
2224fa9e4066Sahrens }
2225fa9e4066Sahrens 
222686714001SSerapheim Dimitropoulos /*
222786714001SSerapheim Dimitropoulos  * zpool checkpoint <pool>
222886714001SSerapheim Dimitropoulos  *       checkpoint --discard <pool>
222986714001SSerapheim Dimitropoulos  *
223004e56356SAndriy Gapon  *	-d	Discard the checkpoint from a checkpointed
223104e56356SAndriy Gapon  *	--discard  pool.
223286714001SSerapheim Dimitropoulos  *
223386714001SSerapheim Dimitropoulos  * Checkpoints the specified pool, by taking a "snapshot" of its
223486714001SSerapheim Dimitropoulos  * current state. A pool can only have one checkpoint at a time.
223586714001SSerapheim Dimitropoulos  */
223686714001SSerapheim Dimitropoulos int
223786714001SSerapheim Dimitropoulos zpool_do_checkpoint(int argc, char **argv)
223886714001SSerapheim Dimitropoulos {
223986714001SSerapheim Dimitropoulos 	boolean_t discard;
224086714001SSerapheim Dimitropoulos 	char *pool;
224186714001SSerapheim Dimitropoulos 	zpool_handle_t *zhp;
224286714001SSerapheim Dimitropoulos 	int c, err;
224386714001SSerapheim Dimitropoulos 
224486714001SSerapheim Dimitropoulos 	struct option long_options[] = {
224586714001SSerapheim Dimitropoulos 		{"discard", no_argument, NULL, 'd'},
224686714001SSerapheim Dimitropoulos 		{0, 0, 0, 0}
224786714001SSerapheim Dimitropoulos 	};
224886714001SSerapheim Dimitropoulos 
224986714001SSerapheim Dimitropoulos 	discard = B_FALSE;
225086714001SSerapheim Dimitropoulos 	while ((c = getopt_long(argc, argv, ":d", long_options, NULL)) != -1) {
225186714001SSerapheim Dimitropoulos 		switch (c) {
225286714001SSerapheim Dimitropoulos 		case 'd':
225386714001SSerapheim Dimitropoulos 			discard = B_TRUE;
225486714001SSerapheim Dimitropoulos 			break;
225586714001SSerapheim Dimitropoulos 		case '?':
225686714001SSerapheim Dimitropoulos 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
225786714001SSerapheim Dimitropoulos 			    optopt);
225886714001SSerapheim Dimitropoulos 			usage(B_FALSE);
225986714001SSerapheim Dimitropoulos 		}
226086714001SSerapheim Dimitropoulos 	}
226186714001SSerapheim Dimitropoulos 
226286714001SSerapheim Dimitropoulos 	argc -= optind;
226386714001SSerapheim Dimitropoulos 	argv += optind;
226486714001SSerapheim Dimitropoulos 
226586714001SSerapheim Dimitropoulos 	if (argc < 1) {
226686714001SSerapheim Dimitropoulos 		(void) fprintf(stderr, gettext("missing pool argument\n"));
226786714001SSerapheim Dimitropoulos 		usage(B_FALSE);
226886714001SSerapheim Dimitropoulos 	}
226986714001SSerapheim Dimitropoulos 
227086714001SSerapheim Dimitropoulos 	if (argc > 1) {
227186714001SSerapheim Dimitropoulos 		(void) fprintf(stderr, gettext("too many arguments\n"));
227286714001SSerapheim Dimitropoulos 		usage(B_FALSE);
227386714001SSerapheim Dimitropoulos 	}
227486714001SSerapheim Dimitropoulos 
227586714001SSerapheim Dimitropoulos 	pool = argv[0];
227686714001SSerapheim Dimitropoulos 
227786714001SSerapheim Dimitropoulos 	if ((zhp = zpool_open(g_zfs, pool)) == NULL) {
227886714001SSerapheim Dimitropoulos 		/* As a special case, check for use of '/' in the name */
227986714001SSerapheim Dimitropoulos 		if (strchr(pool, '/') != NULL)
228086714001SSerapheim Dimitropoulos 			(void) fprintf(stderr, gettext("'zpool checkpoint' "
228186714001SSerapheim Dimitropoulos 			    "doesn't work on datasets. To save the state "
228286714001SSerapheim Dimitropoulos 			    "of a dataset from a specific point in time "
228386714001SSerapheim Dimitropoulos 			    "please use 'zfs snapshot'\n"));
228486714001SSerapheim Dimitropoulos 		return (1);
228586714001SSerapheim Dimitropoulos 	}
228686714001SSerapheim Dimitropoulos 
228786714001SSerapheim Dimitropoulos 	if (discard)
228886714001SSerapheim Dimitropoulos 		err = (zpool_discard_checkpoint(zhp) != 0);
228986714001SSerapheim Dimitropoulos 	else
229086714001SSerapheim Dimitropoulos 		err = (zpool_checkpoint(zhp) != 0);
229186714001SSerapheim Dimitropoulos 
229286714001SSerapheim Dimitropoulos 	zpool_close(zhp);
229386714001SSerapheim Dimitropoulos 
229486714001SSerapheim Dimitropoulos 	return (err);
229586714001SSerapheim Dimitropoulos }
229686714001SSerapheim Dimitropoulos 
229786714001SSerapheim Dimitropoulos #define	CHECKPOINT_OPT	1024
229886714001SSerapheim Dimitropoulos 
2299fa9e4066Sahrens /*
23004c58d714Sdarrenm  * zpool import [-d dir] [-D]
23012f8aaab3Seschrock  *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
23022f8aaab3Seschrock  *              [-d dir | -c cachefile] [-f] -a
23032f8aaab3Seschrock  *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
230404e56356SAndriy Gapon  *              [-d dir | -c cachefile] [-f] [-n] [-F] [-t]
230504e56356SAndriy Gapon  *              <pool | id> [newpool]
23062f8aaab3Seschrock  *
230704e56356SAndriy Gapon  *	-c	Read pool information from a cachefile instead of searching
23082f8aaab3Seschrock  *		devices.
2309fa9e4066Sahrens  *
231004e56356SAndriy Gapon  *	-d	Scan in a specific directory, other than /dev/dsk.  More than
2311fa9e4066Sahrens  *		one directory can be specified using multiple '-d' options.
2312fa9e4066Sahrens  *
231304e56356SAndriy Gapon  *	-D	Scan for previously destroyed pools or import all or only
231404e56356SAndriy Gapon  *		specified destroyed pools.
23154c58d714Sdarrenm  *
231604e56356SAndriy Gapon  *	-R	Temporarily import the pool, with all mountpoints relative to
2317fa9e4066Sahrens  *		the given root.  The pool will remain exported when the machine
2318fa9e4066Sahrens  *		is rebooted.
2319fa9e4066Sahrens  *
232004e56356SAndriy Gapon  *	-V	Import even in the presence of faulted vdevs.  This is an
232104e56356SAndriy Gapon  *		intentionally undocumented option for testing purposes, and
232204e56356SAndriy Gapon  *		treats the pool configuration as complete, leaving any bad
23234f0f5e5bSVictor Latushkin  *		vdevs in the FAULTED state. In other words, it does verbatim
23244f0f5e5bSVictor Latushkin  *		import.
2325c5904d13Seschrock  *
232604e56356SAndriy Gapon  *	-f	Force import, even if it appears that the pool is active.
232704e56356SAndriy Gapon  *
232804e56356SAndriy Gapon  *	-F	Attempt rewind if necessary.
2329468c413aSTim Haley  *
233004e56356SAndriy Gapon  *	-n	See if rewind would work, but don't actually rewind.
2331468c413aSTim Haley  *
233204e56356SAndriy Gapon  *	-N	Import the pool but don't mount datasets.
2333468c413aSTim Haley  *
233404e56356SAndriy Gapon  *	-t	Use newpool as a temporary pool name instead of renaming
233504e56356SAndriy Gapon  *		the pool.
2336f9af39baSGeorge Wilson  *
233704e56356SAndriy Gapon  *	-T	Specify a starting txg to use for import. This option is
233804e56356SAndriy Gapon  *		intentionally undocumented option for testing purposes.
2339f9af39baSGeorge Wilson  *
234004e56356SAndriy Gapon  *	-a	Import all pools found.
2341fa9e4066Sahrens  *
234204e56356SAndriy Gapon  *	-o	Set property=value and/or temporary mount options (without '=').
2343ecd6cf80Smarks  *
234404e56356SAndriy Gapon  *	--rewind-to-checkpoint
234504e56356SAndriy Gapon  *		Import the pool and revert back to the checkpoint.
234686714001SSerapheim Dimitropoulos  *
2347fa9e4066Sahrens  * The import command scans for pools to import, and import pools based on pool
2348fa9e4066Sahrens  * name and GUID.  The pool can also be renamed as part of the import process.
2349fa9e4066Sahrens  */
2350fa9e4066Sahrens int
2351fa9e4066Sahrens zpool_do_import(int argc, char **argv)
2352fa9e4066Sahrens {
2353fa9e4066Sahrens 	char **searchdirs = NULL;
2354fa9e4066Sahrens 	int nsearch = 0;
2355fa9e4066Sahrens 	int c;
2356d41c4376SMark J Musante 	int err = 0;
23572f8aaab3Seschrock 	nvlist_t *pools = NULL;
235899653d4eSeschrock 	boolean_t do_all = B_FALSE;
235999653d4eSeschrock 	boolean_t do_destroyed = B_FALSE;
2360fa9e4066Sahrens 	char *mntopts = NULL;
2361fa9e4066Sahrens 	nvpair_t *elem;
2362fa9e4066Sahrens 	nvlist_t *config;
236324e697d4Sck 	uint64_t searchguid = 0;
236424e697d4Sck 	char *searchname = NULL;
2365990b4856Slling 	char *propval;
2366fa9e4066Sahrens 	nvlist_t *found_config;
2367468c413aSTim Haley 	nvlist_t *policy = NULL;
2368ecd6cf80Smarks 	nvlist_t *props = NULL;
236999653d4eSeschrock 	boolean_t first;
23704b964adaSGeorge Wilson 	int flags = ZFS_IMPORT_NORMAL;
2371468c413aSTim Haley 	uint32_t rewind_policy = ZPOOL_NO_REWIND;
2372468c413aSTim Haley 	boolean_t dryrun = B_FALSE;
2373468c413aSTim Haley 	boolean_t do_rewind = B_FALSE;
2374468c413aSTim Haley 	boolean_t xtreme_rewind = B_FALSE;
2375f9af39baSGeorge Wilson 	uint64_t pool_state, txg = -1ULL;
23762f8aaab3Seschrock 	char *cachefile = NULL;
2377d41c4376SMark J Musante 	importargs_t idata = { 0 };
2378f9af39baSGeorge Wilson 	char *endptr;
2379fa9e4066Sahrens 
238086714001SSerapheim Dimitropoulos 
238186714001SSerapheim Dimitropoulos 	struct option long_options[] = {
238286714001SSerapheim Dimitropoulos 		{"rewind-to-checkpoint", no_argument, NULL, CHECKPOINT_OPT},
238386714001SSerapheim Dimitropoulos 		{0, 0, 0, 0}
238486714001SSerapheim Dimitropoulos 	};
238586714001SSerapheim Dimitropoulos 
2386fa9e4066Sahrens 	/* check options */
238704e56356SAndriy Gapon 	while ((c = getopt_long(argc, argv, ":aCc:d:DEfFmnNo:rR:tT:VX",
238886714001SSerapheim Dimitropoulos 	    long_options, NULL)) != -1) {
2389fa9e4066Sahrens 		switch (c) {
2390fa9e4066Sahrens 		case 'a':
239199653d4eSeschrock 			do_all = B_TRUE;
2392fa9e4066Sahrens 			break;
23932f8aaab3Seschrock 		case 'c':
23942f8aaab3Seschrock 			cachefile = optarg;
23952f8aaab3Seschrock 			break;
2396fa9e4066Sahrens 		case 'd':
2397fa9e4066Sahrens 			if (searchdirs == NULL) {
2398fa9e4066Sahrens 				searchdirs = safe_malloc(sizeof (char *));
2399fa9e4066Sahrens 			} else {
2400fa9e4066Sahrens 				char **tmp = safe_malloc((nsearch + 1) *
2401fa9e4066Sahrens 				    sizeof (char *));
2402fa9e4066Sahrens 				bcopy(searchdirs, tmp, nsearch *
2403fa9e4066Sahrens 				    sizeof (char *));
2404fa9e4066Sahrens 				free(searchdirs);
2405fa9e4066Sahrens 				searchdirs = tmp;
2406fa9e4066Sahrens 			}
2407fa9e4066Sahrens 			searchdirs[nsearch++] = optarg;
2408fa9e4066Sahrens 			break;
24094c58d714Sdarrenm 		case 'D':
241099653d4eSeschrock 			do_destroyed = B_TRUE;
24114c58d714Sdarrenm 			break;
2412fa9e4066Sahrens 		case 'f':
24134b964adaSGeorge Wilson 			flags |= ZFS_IMPORT_ANY_HOST;
2414fa9e4066Sahrens 			break;
2415c5904d13Seschrock 		case 'F':
2416468c413aSTim Haley 			do_rewind = B_TRUE;
2417468c413aSTim Haley 			break;
24184b964adaSGeorge Wilson 		case 'm':
24194b964adaSGeorge Wilson 			flags |= ZFS_IMPORT_MISSING_LOG;
24204b964adaSGeorge Wilson 			break;
2421468c413aSTim Haley 		case 'n':
2422468c413aSTim Haley 			dryrun = B_TRUE;
2423c5904d13Seschrock 			break;
2424f9af39baSGeorge Wilson 		case 'N':
2425f9af39baSGeorge Wilson 			flags |= ZFS_IMPORT_ONLY;
2426f9af39baSGeorge Wilson 			break;
2427fa9e4066Sahrens 		case 'o':
2428990b4856Slling 			if ((propval = strchr(optarg, '=')) != NULL) {
2429990b4856Slling 				*propval = '\0';
2430990b4856Slling 				propval++;
24310a48a24eStimh 				if (add_prop_list(optarg, propval,
24320a48a24eStimh 				    &props, B_TRUE))
2433990b4856Slling 					goto error;
2434990b4856Slling 			} else {
2435990b4856Slling 				mntopts = optarg;
2436990b4856Slling 			}
2437fa9e4066Sahrens 			break;
2438fa9e4066Sahrens 		case 'R':
2439990b4856Slling 			if (add_prop_list(zpool_prop_to_name(
24400a48a24eStimh 			    ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2441990b4856Slling 				goto error;
244204e56356SAndriy Gapon 			if (add_prop_list_default(zpool_prop_to_name(
244304e56356SAndriy Gapon 			    ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
244404e56356SAndriy Gapon 				goto error;
244504e56356SAndriy Gapon 			break;
244604e56356SAndriy Gapon 		case 't':
244704e56356SAndriy Gapon 			flags |= ZFS_IMPORT_TEMP_NAME;
244804e56356SAndriy Gapon 			if (add_prop_list_default(zpool_prop_to_name(
24490a48a24eStimh 			    ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2450990b4856Slling 				goto error;
2451fa9e4066Sahrens 			break;
2452f9af39baSGeorge Wilson 		case 'T':
2453f9af39baSGeorge Wilson 			errno = 0;
2454e42d2059SMatthew Ahrens 			txg = strtoull(optarg, &endptr, 0);
2455f9af39baSGeorge Wilson 			if (errno != 0 || *endptr != '\0') {
2456f9af39baSGeorge Wilson 				(void) fprintf(stderr,
2457f9af39baSGeorge Wilson 				    gettext("invalid txg value\n"));
2458f9af39baSGeorge Wilson 				usage(B_FALSE);
2459f9af39baSGeorge Wilson 			}
2460f9af39baSGeorge Wilson 			rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2461f9af39baSGeorge Wilson 			break;
2462468c413aSTim Haley 		case 'V':
24634b964adaSGeorge Wilson 			flags |= ZFS_IMPORT_VERBATIM;
2464468c413aSTim Haley 			break;
2465468c413aSTim Haley 		case 'X':
2466468c413aSTim Haley 			xtreme_rewind = B_TRUE;
2467468c413aSTim Haley 			break;
246886714001SSerapheim Dimitropoulos 		case CHECKPOINT_OPT:
246986714001SSerapheim Dimitropoulos 			flags |= ZFS_IMPORT_CHECKPOINT;
247086714001SSerapheim Dimitropoulos 			break;
2471fa9e4066Sahrens 		case ':':
2472fa9e4066Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
2473fa9e4066Sahrens 			    "'%c' option\n"), optopt);
247499653d4eSeschrock 			usage(B_FALSE);
2475fa9e4066Sahrens 			break;
2476fa9e4066Sahrens 		case '?':
2477fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2478fa9e4066Sahrens 			    optopt);
247999653d4eSeschrock 			usage(B_FALSE);
2480fa9e4066Sahrens 		}
2481fa9e4066Sahrens 	}
2482fa9e4066Sahrens 
2483fa9e4066Sahrens 	argc -= optind;
2484fa9e4066Sahrens 	argv += optind;
2485fa9e4066Sahrens 
24862f8aaab3Seschrock 	if (cachefile && nsearch != 0) {
24872f8aaab3Seschrock 		(void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
24882f8aaab3Seschrock 		usage(B_FALSE);
24892f8aaab3Seschrock 	}
24902f8aaab3Seschrock 
2491468c413aSTim Haley 	if ((dryrun || xtreme_rewind) && !do_rewind) {
2492468c413aSTim Haley 		(void) fprintf(stderr,
2493468c413aSTim Haley 		    gettext("-n or -X only meaningful with -F\n"));
2494468c413aSTim Haley 		usage(B_FALSE);
2495468c413aSTim Haley 	}
2496468c413aSTim Haley 	if (dryrun)
2497468c413aSTim Haley 		rewind_policy = ZPOOL_TRY_REWIND;
2498468c413aSTim Haley 	else if (do_rewind)
2499468c413aSTim Haley 		rewind_policy = ZPOOL_DO_REWIND;
2500468c413aSTim Haley 	if (xtreme_rewind)
2501468c413aSTim Haley 		rewind_policy |= ZPOOL_EXTREME_REWIND;
2502468c413aSTim Haley 
2503468c413aSTim Haley 	/* In the future, we can capture further policy and include it here */
2504468c413aSTim Haley 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
25055dafeea3SPavel Zakharov 	    nvlist_add_uint64(policy, ZPOOL_LOAD_REQUEST_TXG, txg) != 0 ||
25065dafeea3SPavel Zakharov 	    nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY,
25075dafeea3SPavel Zakharov 	    rewind_policy) != 0)
2508468c413aSTim Haley 		goto error;
2509468c413aSTim Haley 
2510fa9e4066Sahrens 	if (searchdirs == NULL) {
2511fa9e4066Sahrens 		searchdirs = safe_malloc(sizeof (char *));
25126401734dSWill Andrews 		searchdirs[0] = ZFS_DISK_ROOT;
2513fa9e4066Sahrens 		nsearch = 1;
2514fa9e4066Sahrens 	}
2515fa9e4066Sahrens 
2516fa9e4066Sahrens 	/* check argument count */
2517fa9e4066Sahrens 	if (do_all) {
2518fa9e4066Sahrens 		if (argc != 0) {
2519fa9e4066Sahrens 			(void) fprintf(stderr, gettext("too many arguments\n"));
252099653d4eSeschrock 			usage(B_FALSE);
2521fa9e4066Sahrens 		}
2522fa9e4066Sahrens 	} else {
2523fa9e4066Sahrens 		if (argc > 2) {
2524fa9e4066Sahrens 			(void) fprintf(stderr, gettext("too many arguments\n"));
252599653d4eSeschrock 			usage(B_FALSE);
2526fa9e4066Sahrens 		}
2527fa9e4066Sahrens 
2528fa9e4066Sahrens 		/*
2529fa9e4066Sahrens 		 * Check for the SYS_CONFIG privilege.  We do this explicitly
2530fa9e4066Sahrens 		 * here because otherwise any attempt to discover pools will
2531fa9e4066Sahrens 		 * silently fail.
2532fa9e4066Sahrens 		 */
2533fa9e4066Sahrens 		if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2534fa9e4066Sahrens 			(void) fprintf(stderr, gettext("cannot "
2535fa9e4066Sahrens 			    "discover pools: permission denied\n"));
253699653d4eSeschrock 			free(searchdirs);
2537468c413aSTim Haley 			nvlist_free(policy);
2538fa9e4066Sahrens 			return (1);
2539fa9e4066Sahrens 		}
2540fa9e4066Sahrens 	}
2541fa9e4066Sahrens 
2542fa9e4066Sahrens 	/*
2543fa9e4066Sahrens 	 * Depending on the arguments given, we do one of the following:
2544fa9e4066Sahrens 	 *
2545fa9e4066Sahrens 	 *	<none>	Iterate through all pools and display information about
2546fa9e4066Sahrens 	 *		each one.
2547fa9e4066Sahrens 	 *
2548fa9e4066Sahrens 	 *	-a	Iterate through all pools and try to import each one.
2549fa9e4066Sahrens 	 *
2550fa9e4066Sahrens 	 *	<id>	Find the pool that corresponds to the given GUID/pool
2551fa9e4066Sahrens 	 *		name and import that one.
25524c58d714Sdarrenm 	 *
25534c58d714Sdarrenm 	 *	-D	Above options applies only to destroyed pools.
2554fa9e4066Sahrens 	 */
2555fa9e4066Sahrens 	if (argc != 0) {
2556fa9e4066Sahrens 		char *endptr;
2557fa9e4066Sahrens 
2558fa9e4066Sahrens 		errno = 0;
2559fa9e4066Sahrens 		searchguid = strtoull(argv[0], &endptr, 10);
25609edf9ebdSPrasad Joshi 		if (errno != 0 || *endptr != '\0') {
2561fa9e4066Sahrens 			searchname = argv[0];
25629edf9ebdSPrasad Joshi 			searchguid = 0;
25639edf9ebdSPrasad Joshi 		}
2564fa9e4066Sahrens 		found_config = NULL;
2565fa9e4066Sahrens 
256624e697d4Sck 		/*
2567d41c4376SMark J Musante 		 * User specified a name or guid.  Ensure it's unique.
256824e697d4Sck 		 */
2569d41c4376SMark J Musante 		idata.unique = B_TRUE;
257024e697d4Sck 	}
257124e697d4Sck 
2572d41c4376SMark J Musante 
2573d41c4376SMark J Musante 	idata.path = searchdirs;
2574d41c4376SMark J Musante 	idata.paths = nsearch;
2575d41c4376SMark J Musante 	idata.poolname = searchname;
2576d41c4376SMark J Musante 	idata.guid = searchguid;
2577d41c4376SMark J Musante 	idata.cachefile = cachefile;
25786f793812SPavel Zakharov 	idata.policy = policy;
2579d41c4376SMark J Musante 
2580d41c4376SMark J Musante 	pools = zpool_search_import(g_zfs, &idata);
2581d41c4376SMark J Musante 
2582d41c4376SMark J Musante 	if (pools != NULL && idata.exists &&
2583d41c4376SMark J Musante 	    (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2584d41c4376SMark J Musante 		(void) fprintf(stderr, gettext("cannot import '%s': "
2585d41c4376SMark J Musante 		    "a pool with that name already exists\n"),
2586d41c4376SMark J Musante 		    argv[0]);
258704e56356SAndriy Gapon 		(void) fprintf(stderr, gettext("use the form 'zpool import "
258804e56356SAndriy Gapon 		    "[-t] <pool | id> <newpool>' to give it a new temporary "
258904e56356SAndriy Gapon 		    "or permanent name\n"));
2590d41c4376SMark J Musante 		err = 1;
2591d41c4376SMark J Musante 	} else if (pools == NULL && idata.exists) {
2592d41c4376SMark J Musante 		(void) fprintf(stderr, gettext("cannot import '%s': "
2593d41c4376SMark J Musante 		    "a pool with that name is already created/imported,\n"),
2594d41c4376SMark J Musante 		    argv[0]);
2595d41c4376SMark J Musante 		(void) fprintf(stderr, gettext("and no additional pools "
2596d41c4376SMark J Musante 		    "with that name were found\n"));
2597d41c4376SMark J Musante 		err = 1;
2598d41c4376SMark J Musante 	} else if (pools == NULL) {
259924e697d4Sck 		if (argc != 0) {
260024e697d4Sck 			(void) fprintf(stderr, gettext("cannot import '%s': "
260124e697d4Sck 			    "no such pool available\n"), argv[0]);
260224e697d4Sck 		}
2603d41c4376SMark J Musante 		err = 1;
2604d41c4376SMark J Musante 	}
2605d41c4376SMark J Musante 
2606d41c4376SMark J Musante 	if (err == 1) {
260724e697d4Sck 		free(searchdirs);
2608468c413aSTim Haley 		nvlist_free(policy);
260924e697d4Sck 		return (1);
261024e697d4Sck 	}
261124e697d4Sck 
261224e697d4Sck 	/*
261324e697d4Sck 	 * At this point we have a list of import candidate configs. Even if
261424e697d4Sck 	 * we were searching by pool name or guid, we still need to
261524e697d4Sck 	 * post-process the list to deal with pool state and possible
261624e697d4Sck 	 * duplicate names.
261724e697d4Sck 	 */
2618fa9e4066Sahrens 	err = 0;
2619fa9e4066Sahrens 	elem = NULL;
262099653d4eSeschrock 	first = B_TRUE;
2621fa9e4066Sahrens 	while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2622fa9e4066Sahrens 
2623fa9e4066Sahrens 		verify(nvpair_value_nvlist(elem, &config) == 0);
2624fa9e4066Sahrens 
26254c58d714Sdarrenm 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
26264c58d714Sdarrenm 		    &pool_state) == 0);
26274c58d714Sdarrenm 		if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
26284c58d714Sdarrenm 			continue;
26294c58d714Sdarrenm 		if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
26304c58d714Sdarrenm 			continue;
26314c58d714Sdarrenm 
26325dafeea3SPavel Zakharov 		verify(nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY,
2633468c413aSTim Haley 		    policy) == 0);
2634468c413aSTim Haley 
2635fa9e4066Sahrens 		if (argc == 0) {
2636fa9e4066Sahrens 			if (first)
263799653d4eSeschrock 				first = B_FALSE;
26383bb79becSeschrock 			else if (!do_all)
2639fa9e4066Sahrens 				(void) printf("\n");
2640fa9e4066Sahrens 
2641468c413aSTim Haley 			if (do_all) {
2642fa9e4066Sahrens 				err |= do_import(config, NULL, mntopts,
26434b964adaSGeorge Wilson 				    props, flags);
2644468c413aSTim Haley 			} else {
2645fa9e4066Sahrens 				show_import(config);
2646468c413aSTim Haley 			}
2647fa9e4066Sahrens 		} else if (searchname != NULL) {
2648fa9e4066Sahrens 			char *name;
2649fa9e4066Sahrens 
2650fa9e4066Sahrens 			/*
2651fa9e4066Sahrens 			 * We are searching for a pool based on name.
2652fa9e4066Sahrens 			 */
2653fa9e4066Sahrens 			verify(nvlist_lookup_string(config,
2654fa9e4066Sahrens 			    ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2655fa9e4066Sahrens 
2656fa9e4066Sahrens 			if (strcmp(name, searchname) == 0) {
2657fa9e4066Sahrens 				if (found_config != NULL) {
2658fa9e4066Sahrens 					(void) fprintf(stderr, gettext(
2659fa9e4066Sahrens 					    "cannot import '%s': more than "
2660fa9e4066Sahrens 					    "one matching pool\n"), searchname);
2661fa9e4066Sahrens 					(void) fprintf(stderr, gettext(
2662fa9e4066Sahrens 					    "import by numeric ID instead\n"));
266399653d4eSeschrock 					err = B_TRUE;
2664fa9e4066Sahrens 				}
2665fa9e4066Sahrens 				found_config = config;
2666fa9e4066Sahrens 			}
2667fa9e4066Sahrens 		} else {
2668fa9e4066Sahrens 			uint64_t guid;
2669fa9e4066Sahrens 
2670fa9e4066Sahrens 			/*
2671fa9e4066Sahrens 			 * Search for a pool by guid.
2672fa9e4066Sahrens 			 */
2673fa9e4066Sahrens 			verify(nvlist_lookup_uint64(config,
2674fa9e4066Sahrens 			    ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2675fa9e4066Sahrens 
2676fa9e4066Sahrens 			if (guid == searchguid)
2677fa9e4066Sahrens 				found_config = config;
2678fa9e4066Sahrens 		}
2679fa9e4066Sahrens 	}
2680fa9e4066Sahrens 
2681fa9e4066Sahrens 	/*
2682fa9e4066Sahrens 	 * If we were searching for a specific pool, verify that we found a
2683fa9e4066Sahrens 	 * pool, and then do the import.
2684fa9e4066Sahrens 	 */
2685fa9e4066Sahrens 	if (argc != 0 && err == 0) {
2686fa9e4066Sahrens 		if (found_config == NULL) {
2687fa9e4066Sahrens 			(void) fprintf(stderr, gettext("cannot import '%s': "
2688fa9e4066Sahrens 			    "no such pool available\n"), argv[0]);
268999653d4eSeschrock 			err = B_TRUE;
2690fa9e4066Sahrens 		} else {
2691fa9e4066Sahrens 			err |= do_import(found_config, argc == 1 ? NULL :
26924b964adaSGeorge Wilson 			    argv[1], mntopts, props, flags);
2693fa9e4066Sahrens 		}
2694fa9e4066Sahrens 	}
2695fa9e4066Sahrens 
2696fa9e4066Sahrens 	/*
2697fa9e4066Sahrens 	 * If we were just looking for pools, report an error if none were
2698fa9e4066Sahrens 	 * found.
2699fa9e4066Sahrens 	 */
2700fa9e4066Sahrens 	if (argc == 0 && first)
2701fa9e4066Sahrens 		(void) fprintf(stderr,
2702fa9e4066Sahrens 		    gettext("no pools available to import\n"));
2703fa9e4066Sahrens 
2704ecd6cf80Smarks error:
27052f8aaab3Seschrock 	nvlist_free(props);
2706fa9e4066Sahrens 	nvlist_free(pools);
2707468c413aSTim Haley 	nvlist_free(policy);
270899653d4eSeschrock 	free(searchdirs);
2709fa9e4066Sahrens 
2710fa9e4066Sahrens 	return (err ? 1 : 0);
2711fa9e4066Sahrens }
2712fa9e4066Sahrens 
27139c2acf00SAlek Pinchuk /*
27149c2acf00SAlek Pinchuk  * zpool sync [-f] [pool] ...
27159c2acf00SAlek Pinchuk  *
27169c2acf00SAlek Pinchuk  * -f (undocumented) force uberblock (and config including zpool cache file)
27179c2acf00SAlek Pinchuk  *    update.
27189c2acf00SAlek Pinchuk  *
27199c2acf00SAlek Pinchuk  * Sync the specified pool(s).
27209c2acf00SAlek Pinchuk  * Without arguments "zpool sync" will sync all pools.
27219c2acf00SAlek Pinchuk  * This command initiates TXG sync(s) and will return after the TXG(s) commit.
27229c2acf00SAlek Pinchuk  *
27239c2acf00SAlek Pinchuk  */
27249c2acf00SAlek Pinchuk static int
27259c2acf00SAlek Pinchuk zpool_do_sync(int argc, char **argv)
27269c2acf00SAlek Pinchuk {
27279c2acf00SAlek Pinchuk 	int ret;
27289c2acf00SAlek Pinchuk 	boolean_t force = B_FALSE;
27299c2acf00SAlek Pinchuk 
27309c2acf00SAlek Pinchuk 	/* check options */
27319c2acf00SAlek Pinchuk 	while ((ret  = getopt(argc, argv, "f")) != -1) {
27329c2acf00SAlek Pinchuk 		switch (ret) {
27339c2acf00SAlek Pinchuk 		case 'f':
27349c2acf00SAlek Pinchuk 			force = B_TRUE;
27359c2acf00SAlek Pinchuk 			break;
27369c2acf00SAlek Pinchuk 		case '?':
27379c2acf00SAlek Pinchuk 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
27389c2acf00SAlek Pinchuk 			    optopt);
27399c2acf00SAlek Pinchuk 			usage(B_FALSE);
27409c2acf00SAlek Pinchuk 		}
27419c2acf00SAlek Pinchuk 	}
27429c2acf00SAlek Pinchuk 
27439c2acf00SAlek Pinchuk 	argc -= optind;
27449c2acf00SAlek Pinchuk 	argv += optind;
27459c2acf00SAlek Pinchuk 
27469c2acf00SAlek Pinchuk 	/* if argc == 0 we will execute zpool_sync_one on all pools */
27479c2acf00SAlek Pinchuk 	ret = for_each_pool(argc, argv, B_FALSE, NULL, zpool_sync_one, &force);
27489c2acf00SAlek Pinchuk 
27499c2acf00SAlek Pinchuk 	return (ret);
27509c2acf00SAlek Pinchuk }
27519c2acf00SAlek Pinchuk 
2752fa9e4066Sahrens typedef struct iostat_cbdata {
27534263d13fSGeorge Wilson 	boolean_t cb_verbose;
2754fa9e4066Sahrens 	int cb_namewidth;
27554263d13fSGeorge Wilson 	int cb_iteration;
27564263d13fSGeorge Wilson 	zpool_list_t *cb_list;
2757fa9e4066Sahrens } iostat_cbdata_t;
2758fa9e4066Sahrens 
2759fa9e4066Sahrens static void
2760fa9e4066Sahrens print_iostat_separator(iostat_cbdata_t *cb)
2761fa9e4066Sahrens {
2762fa9e4066Sahrens 	int i = 0;
2763fa9e4066Sahrens 
2764fa9e4066Sahrens 	for (i = 0; i < cb->cb_namewidth; i++)
2765fa9e4066Sahrens 		(void) printf("-");
2766fa9e4066Sahrens 	(void) printf("  -----  -----  -----  -----  -----  -----\n");
2767fa9e4066Sahrens }
2768fa9e4066Sahrens 
2769fa9e4066Sahrens static void
2770fa9e4066Sahrens print_iostat_header(iostat_cbdata_t *cb)
2771fa9e4066Sahrens {
2772fa9e4066Sahrens 	(void) printf("%*s     capacity     operations    bandwidth\n",
2773fa9e4066Sahrens 	    cb->cb_namewidth, "");
2774485bbbf5SGeorge Wilson 	(void) printf("%-*s  alloc   free   read  write   read  write\n",
2775fa9e4066Sahrens 	    cb->cb_namewidth, "pool");
2776fa9e4066Sahrens 	print_iostat_separator(cb);
2777fa9e4066Sahrens }
2778fa9e4066Sahrens 
2779fa9e4066Sahrens /*
2780fa9e4066Sahrens  * Display a single statistic.
2781fa9e4066Sahrens  */
2782990b4856Slling static void
2783fa9e4066Sahrens print_one_stat(uint64_t value)
2784fa9e4066Sahrens {
2785fa9e4066Sahrens 	char buf[64];
2786fa9e4066Sahrens 
2787fa9e4066Sahrens 	zfs_nicenum(value, buf, sizeof (buf));
2788fa9e4066Sahrens 	(void) printf("  %5s", buf);
2789fa9e4066Sahrens }
2790fa9e4066Sahrens 
2791fa9e4066Sahrens /*
2792fa9e4066Sahrens  * Print out all the statistics for the given vdev.  This can either be the
2793fa9e4066Sahrens  * toplevel configuration, or called recursively.  If 'name' is NULL, then this
2794fa9e4066Sahrens  * is a verbose output, and we don't want to display the toplevel pool stats.
2795fa9e4066Sahrens  */
2796fa9e4066Sahrens void
2797c67d9675Seschrock print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2798c67d9675Seschrock     nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2799fa9e4066Sahrens {
2800fa9e4066Sahrens 	nvlist_t **oldchild, **newchild;
2801fa9e4066Sahrens 	uint_t c, children;
2802fa9e4066Sahrens 	vdev_stat_t *oldvs, *newvs;
2803fa9e4066Sahrens 	vdev_stat_t zerovs = { 0 };
2804fa9e4066Sahrens 	uint64_t tdelta;
2805fa9e4066Sahrens 	double scale;
2806afefbcddSeschrock 	char *vname;
2807fa9e4066Sahrens 
28085cabbc6bSPrashanth Sreenivasa 	if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
28095cabbc6bSPrashanth Sreenivasa 		return;
28105cabbc6bSPrashanth Sreenivasa 
2811fa9e4066Sahrens 	if (oldnv != NULL) {
28123f9d6ad7SLin Ling 		verify(nvlist_lookup_uint64_array(oldnv,
28133f9d6ad7SLin Ling 		    ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2814fa9e4066Sahrens 	} else {
2815fa9e4066Sahrens 		oldvs = &zerovs;
2816fa9e4066Sahrens 	}
2817fa9e4066Sahrens 
28183f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2819fa9e4066Sahrens 	    (uint64_t **)&newvs, &c) == 0);
2820fa9e4066Sahrens 
2821fa9e4066Sahrens 	if (strlen(name) + depth > cb->cb_namewidth)
2822fa9e4066Sahrens 		(void) printf("%*s%s", depth, "", name);
2823fa9e4066Sahrens 	else
2824fa9e4066Sahrens 		(void) printf("%*s%s%*s", depth, "", name,
2825fa9e4066Sahrens 		    (int)(cb->cb_namewidth - strlen(name) - depth), "");
2826fa9e4066Sahrens 
2827fa9e4066Sahrens 	tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2828fa9e4066Sahrens 
2829fa9e4066Sahrens 	if (tdelta == 0)
2830fa9e4066Sahrens 		scale = 1.0;
2831fa9e4066Sahrens 	else
2832fa9e4066Sahrens 		scale = (double)NANOSEC / tdelta;
2833fa9e4066Sahrens 
2834fa9e4066Sahrens 	/* only toplevel vdevs have capacity stats */
2835fa9e4066Sahrens 	if (newvs->vs_space == 0) {
2836fa9e4066Sahrens 		(void) printf("      -      -");
2837fa9e4066Sahrens 	} else {
2838fa9e4066Sahrens 		print_one_stat(newvs->vs_alloc);
2839fa9e4066Sahrens 		print_one_stat(newvs->vs_space - newvs->vs_alloc);
2840fa9e4066Sahrens 	}
2841fa9e4066Sahrens 
2842fa9e4066Sahrens 	print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2843fa9e4066Sahrens 	    oldvs->vs_ops[ZIO_TYPE_READ])));
2844fa9e4066Sahrens 
2845fa9e4066Sahrens 	print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2846fa9e4066Sahrens 	    oldvs->vs_ops[ZIO_TYPE_WRITE])));
2847fa9e4066Sahrens 
2848fa9e4066Sahrens 	print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2849fa9e4066Sahrens 	    oldvs->vs_bytes[ZIO_TYPE_READ])));
2850fa9e4066Sahrens 
2851fa9e4066Sahrens 	print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2852fa9e4066Sahrens 	    oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2853fa9e4066Sahrens 
2854fa9e4066Sahrens 	(void) printf("\n");
2855fa9e4066Sahrens 
2856fa9e4066Sahrens 	if (!cb->cb_verbose)
2857fa9e4066Sahrens 		return;
2858fa9e4066Sahrens 
2859fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2860fa9e4066Sahrens 	    &newchild, &children) != 0)
2861fa9e4066Sahrens 		return;
2862fa9e4066Sahrens 
2863fa9e4066Sahrens 	if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2864fa9e4066Sahrens 	    &oldchild, &c) != 0)
2865fa9e4066Sahrens 		return;
2866fa9e4066Sahrens 
2867afefbcddSeschrock 	for (c = 0; c < children; c++) {
28689d439f90SMike Harsch 		uint64_t ishole = B_FALSE, islog = B_FALSE;
28693f9d6ad7SLin Ling 
28709d439f90SMike Harsch 		(void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
28719d439f90SMike Harsch 		    &ishole);
28729d439f90SMike Harsch 
28739d439f90SMike Harsch 		(void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
28749d439f90SMike Harsch 		    &islog);
28759d439f90SMike Harsch 
28769d439f90SMike Harsch 		if (ishole || islog)
28773f9d6ad7SLin Ling 			continue;
28783f9d6ad7SLin Ling 
287988ecc943SGeorge Wilson 		vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2880c67d9675Seschrock 		print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2881afefbcddSeschrock 		    newchild[c], cb, depth + 2);
2882afefbcddSeschrock 		free(vname);
2883afefbcddSeschrock 	}
2884fa94a07fSbrendan 
28859d439f90SMike Harsch 	/*
28869d439f90SMike Harsch 	 * Log device section
28879d439f90SMike Harsch 	 */
28889d439f90SMike Harsch 
28899d439f90SMike Harsch 	if (num_logs(newnv) > 0) {
28909d439f90SMike Harsch 		(void) printf("%-*s      -      -      -      -      -      "
28919d439f90SMike Harsch 		    "-\n", cb->cb_namewidth, "logs");
28929d439f90SMike Harsch 
28939d439f90SMike Harsch 		for (c = 0; c < children; c++) {
28949d439f90SMike Harsch 			uint64_t islog = B_FALSE;
28959d439f90SMike Harsch 			(void) nvlist_lookup_uint64(newchild[c],
28969d439f90SMike Harsch 			    ZPOOL_CONFIG_IS_LOG, &islog);
28979d439f90SMike Harsch 
28989d439f90SMike Harsch 			if (islog) {
28999d439f90SMike Harsch 				vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
29009d439f90SMike Harsch 				    B_FALSE);
29019d439f90SMike Harsch 				print_vdev_stats(zhp, vname, oldnv ?
29029d439f90SMike Harsch 				    oldchild[c] : NULL, newchild[c],
29039d439f90SMike Harsch 				    cb, depth + 2);
29049d439f90SMike Harsch 				free(vname);
29059d439f90SMike Harsch 			}
29069d439f90SMike Harsch 		}
29079d439f90SMike Harsch 
29089d439f90SMike Harsch 	}
29099d439f90SMike Harsch 
2910fa94a07fSbrendan 	/*
2911fa94a07fSbrendan 	 * Include level 2 ARC devices in iostat output
2912fa94a07fSbrendan 	 */
2913fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2914fa94a07fSbrendan 	    &newchild, &children) != 0)
2915fa94a07fSbrendan 		return;
2916fa94a07fSbrendan 
2917fa94a07fSbrendan 	if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2918fa94a07fSbrendan 	    &oldchild, &c) != 0)
2919fa94a07fSbrendan 		return;
2920fa94a07fSbrendan 
2921fa94a07fSbrendan 	if (children > 0) {
2922fa94a07fSbrendan 		(void) printf("%-*s      -      -      -      -      -      "
2923fa94a07fSbrendan 		    "-\n", cb->cb_namewidth, "cache");
2924fa94a07fSbrendan 		for (c = 0; c < children; c++) {
292588ecc943SGeorge Wilson 			vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
292688ecc943SGeorge Wilson 			    B_FALSE);
2927fa94a07fSbrendan 			print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2928fa94a07fSbrendan 			    newchild[c], cb, depth + 2);
2929fa94a07fSbrendan 			free(vname);
2930fa94a07fSbrendan 		}
2931fa94a07fSbrendan 	}
2932fa9e4066Sahrens }
2933fa9e4066Sahrens 
2934088e9d47Seschrock static int
2935088e9d47Seschrock refresh_iostat(zpool_handle_t *zhp, void *data)
2936088e9d47Seschrock {
2937088e9d47Seschrock 	iostat_cbdata_t *cb = data;
293894de1d4cSeschrock 	boolean_t missing;
2939088e9d47Seschrock 
2940088e9d47Seschrock 	/*
2941088e9d47Seschrock 	 * If the pool has disappeared, remove it from the list and continue.
2942088e9d47Seschrock 	 */
294394de1d4cSeschrock 	if (zpool_refresh_stats(zhp, &missing) != 0)
294494de1d4cSeschrock 		return (-1);
294594de1d4cSeschrock 
294694de1d4cSeschrock 	if (missing)
2947088e9d47Seschrock 		pool_list_remove(cb->cb_list, zhp);
2948088e9d47Seschrock 
2949088e9d47Seschrock 	return (0);
2950088e9d47Seschrock }
2951088e9d47Seschrock 
2952fa9e4066Sahrens /*
2953fa9e4066Sahrens  * Callback to print out the iostats for the given pool.
2954fa9e4066Sahrens  */
2955fa9e4066Sahrens int
2956fa9e4066Sahrens print_iostat(zpool_handle_t *zhp, void *data)
2957fa9e4066Sahrens {
2958fa9e4066Sahrens 	iostat_cbdata_t *cb = data;
2959fa9e4066Sahrens 	nvlist_t *oldconfig, *newconfig;
2960fa9e4066Sahrens 	nvlist_t *oldnvroot, *newnvroot;
2961fa9e4066Sahrens 
2962088e9d47Seschrock 	newconfig = zpool_get_config(zhp, &oldconfig);
2963fa9e4066Sahrens 
2964088e9d47Seschrock 	if (cb->cb_iteration == 1)
2965fa9e4066Sahrens 		oldconfig = NULL;
2966fa9e4066Sahrens 
2967fa9e4066Sahrens 	verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2968fa9e4066Sahrens 	    &newnvroot) == 0);
2969fa9e4066Sahrens 
2970088e9d47Seschrock 	if (oldconfig == NULL)
2971fa9e4066Sahrens 		oldnvroot = NULL;
2972088e9d47Seschrock 	else
2973088e9d47Seschrock 		verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2974088e9d47Seschrock 		    &oldnvroot) == 0);
2975fa9e4066Sahrens 
2976fa9e4066Sahrens 	/*
2977fa9e4066Sahrens 	 * Print out the statistics for the pool.
2978fa9e4066Sahrens 	 */
2979c67d9675Seschrock 	print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2980fa9e4066Sahrens 
2981fa9e4066Sahrens 	if (cb->cb_verbose)
2982fa9e4066Sahrens 		print_iostat_separator(cb);
2983fa9e4066Sahrens 
2984fa9e4066Sahrens 	return (0);
2985fa9e4066Sahrens }
2986fa9e4066Sahrens 
2987fa9e4066Sahrens int
2988fa9e4066Sahrens get_namewidth(zpool_handle_t *zhp, void *data)
2989fa9e4066Sahrens {
2990fa9e4066Sahrens 	iostat_cbdata_t *cb = data;
2991fa9e4066Sahrens 	nvlist_t *config, *nvroot;
2992fa9e4066Sahrens 
2993088e9d47Seschrock 	if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2994fa9e4066Sahrens 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2995fa9e4066Sahrens 		    &nvroot) == 0);
2996fa9e4066Sahrens 		if (!cb->cb_verbose)
2997fa9e4066Sahrens 			cb->cb_namewidth = strlen(zpool_get_name(zhp));
2998fa9e4066Sahrens 		else
2999e1d5e507SFrederik Wessels 			cb->cb_namewidth = max_width(zhp, nvroot, 0,
3000e1d5e507SFrederik Wessels 			    cb->cb_namewidth);
3001fa9e4066Sahrens 	}
3002fa9e4066Sahrens 
3003fa9e4066Sahrens 	/*
3004fa9e4066Sahrens 	 * The width must fall into the range [10,38].  The upper limit is the
3005fa9e4066Sahrens 	 * maximum we can have and still fit in 80 columns.
3006fa9e4066Sahrens 	 */
3007fa9e4066Sahrens 	if (cb->cb_namewidth < 10)
3008fa9e4066Sahrens 		cb->cb_namewidth = 10;
3009fa9e4066Sahrens 	if (cb->cb_namewidth > 38)
3010fa9e4066Sahrens 		cb->cb_namewidth = 38;
3011fa9e4066Sahrens 
3012fa9e4066Sahrens 	return (0);
3013fa9e4066Sahrens }
3014fa9e4066Sahrens 
3015fa9e4066Sahrens /*
30163f9d6ad7SLin Ling  * Parse the input string, get the 'interval' and 'count' value if there is one.
3017fa9e4066Sahrens  */
30183f9d6ad7SLin Ling static void
30193f9d6ad7SLin Ling get_interval_count(int *argcp, char **argv, unsigned long *iv,
30203f9d6ad7SLin Ling     unsigned long *cnt)
3021fa9e4066Sahrens {
3022fa9e4066Sahrens 	unsigned long interval = 0, count = 0;
30233f9d6ad7SLin Ling 	int argc = *argcp, errno;
3024fa9e4066Sahrens 
3025fa9e4066Sahrens 	/*
3026fa9e4066Sahrens 	 * Determine if the last argument is an integer or a pool name
3027fa9e4066Sahrens 	 */
3028fa9e4066Sahrens 	if (argc > 0 && isdigit(argv[argc - 1][0])) {
3029fa9e4066Sahrens 		char *end;
3030fa9e4066Sahrens 
3031fa9e4066Sahrens 		errno = 0;
3032fa9e4066Sahrens 		interval = strtoul(argv[argc - 1], &end, 10);
3033fa9e4066Sahrens 
3034fa9e4066Sahrens 		if (*end == '\0' && errno == 0) {
3035fa9e4066Sahrens 			if (interval == 0) {
3036fa9e4066Sahrens 				(void) fprintf(stderr, gettext("interval "
3037fa9e4066Sahrens 				    "cannot be zero\n"));
303899653d4eSeschrock 				usage(B_FALSE);
3039fa9e4066Sahrens 			}
3040fa9e4066Sahrens 			/*
3041fa9e4066Sahrens 			 * Ignore the last parameter
3042fa9e4066Sahrens 			 */
3043fa9e4066Sahrens 			argc--;
3044fa9e4066Sahrens 		} else {
3045fa9e4066Sahrens 			/*
3046fa9e4066Sahrens 			 * If this is not a valid number, just plow on.  The
3047fa9e4066Sahrens 			 * user will get a more informative error message later
3048fa9e4066Sahrens 			 * on.
3049fa9e4066Sahrens 			 */
3050fa9e4066Sahrens 			interval = 0;
3051fa9e4066Sahrens 		}
3052fa9e4066Sahrens 	}
3053fa9e4066Sahrens 
3054fa9e4066Sahrens 	/*
3055fa9e4066Sahrens 	 * If the last argument is also an integer, then we have both a count
30563f9d6ad7SLin Ling 	 * and an interval.
3057fa9e4066Sahrens 	 */
3058fa9e4066Sahrens 	if (argc > 0 && isdigit(argv[argc - 1][0])) {
3059fa9e4066Sahrens 		char *end;
3060fa9e4066Sahrens 
3061fa9e4066Sahrens 		errno = 0;
3062fa9e4066Sahrens 		count = interval;
3063fa9e4066Sahrens 		interval = strtoul(argv[argc - 1], &end, 10);
3064fa9e4066Sahrens 
3065fa9e4066Sahrens 		if (*end == '\0' && errno == 0) {
3066fa9e4066Sahrens 			if (interval == 0) {
3067fa9e4066Sahrens 				(void) fprintf(stderr, gettext("interval "
3068fa9e4066Sahrens 				    "cannot be zero\n"));
306999653d4eSeschrock 				usage(B_FALSE);
3070fa9e4066Sahrens 			}
3071fa9e4066Sahrens 
3072fa9e4066Sahrens 			/*
3073fa9e4066Sahrens 			 * Ignore the last parameter
3074fa9e4066Sahrens 			 */
3075fa9e4066Sahrens 			argc--;
3076fa9e4066Sahrens 		} else {
3077fa9e4066Sahrens 			interval = 0;
3078fa9e4066Sahrens 		}
3079fa9e4066Sahrens 	}
3080fa9e4066Sahrens 
30813f9d6ad7SLin Ling 	*iv = interval;
30823f9d6ad7SLin Ling 	*cnt = count;
30833f9d6ad7SLin Ling 	*argcp = argc;
30843f9d6ad7SLin Ling }
30853f9d6ad7SLin Ling 
30863f9d6ad7SLin Ling static void
30873f9d6ad7SLin Ling get_timestamp_arg(char c)
30883f9d6ad7SLin Ling {
30893f9d6ad7SLin Ling 	if (c == 'u')
30903f9d6ad7SLin Ling 		timestamp_fmt = UDATE;
30913f9d6ad7SLin Ling 	else if (c == 'd')
30923f9d6ad7SLin Ling 		timestamp_fmt = DDATE;
30933f9d6ad7SLin Ling 	else
30943f9d6ad7SLin Ling 		usage(B_FALSE);
30953f9d6ad7SLin Ling }
30963f9d6ad7SLin Ling 
30973f9d6ad7SLin Ling /*
30983f9d6ad7SLin Ling  * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
30993f9d6ad7SLin Ling  *
31003f9d6ad7SLin Ling  *	-v	Display statistics for individual vdevs
31013f9d6ad7SLin Ling  *	-T	Display a timestamp in date(1) or Unix format
31023f9d6ad7SLin Ling  *
31033f9d6ad7SLin Ling  * This command can be tricky because we want to be able to deal with pool
31043f9d6ad7SLin Ling  * creation/destruction as well as vdev configuration changes.  The bulk of this
31053f9d6ad7SLin Ling  * processing is handled by the pool_list_* routines in zpool_iter.c.  We rely
31063f9d6ad7SLin Ling  * on pool_list_update() to detect the addition of new pools.  Configuration
31073f9d6ad7SLin Ling  * changes are all handled within libzfs.
31083f9d6ad7SLin Ling  */
31093f9d6ad7SLin Ling int
31103f9d6ad7SLin Ling zpool_do_iostat(int argc, char **argv)
31113f9d6ad7SLin Ling {
31123f9d6ad7SLin Ling 	int c;
31133f9d6ad7SLin Ling 	int ret;
31143f9d6ad7SLin Ling 	int npools;
31153f9d6ad7SLin Ling 	unsigned long interval = 0, count = 0;
31163f9d6ad7SLin Ling 	zpool_list_t *list;
31173f9d6ad7SLin Ling 	boolean_t verbose = B_FALSE;
31183f9d6ad7SLin Ling 	iostat_cbdata_t cb;
31193f9d6ad7SLin Ling 
31203f9d6ad7SLin Ling 	/* check options */
31213f9d6ad7SLin Ling 	while ((c = getopt(argc, argv, "T:v")) != -1) {
31223f9d6ad7SLin Ling 		switch (c) {
31233f9d6ad7SLin Ling 		case 'T':
31243f9d6ad7SLin Ling 			get_timestamp_arg(*optarg);
31253f9d6ad7SLin Ling 			break;
31263f9d6ad7SLin Ling 		case 'v':
31273f9d6ad7SLin Ling 			verbose = B_TRUE;
31283f9d6ad7SLin Ling 			break;
31293f9d6ad7SLin Ling 		case '?':
31303f9d6ad7SLin Ling 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
31313f9d6ad7SLin Ling 			    optopt);
31323f9d6ad7SLin Ling 			usage(B_FALSE);
31333f9d6ad7SLin Ling 		}
31343f9d6ad7SLin Ling 	}
31353f9d6ad7SLin Ling 
31363f9d6ad7SLin Ling 	argc -= optind;
31373f9d6ad7SLin Ling 	argv += optind;
31383f9d6ad7SLin Ling 
31393f9d6ad7SLin Ling 	get_interval_count(&argc, argv, &interval, &count);
31403f9d6ad7SLin Ling 
3141fa9e4066Sahrens 	/*
3142fa9e4066Sahrens 	 * Construct the list of all interesting pools.
3143fa9e4066Sahrens 	 */
3144fa9e4066Sahrens 	ret = 0;
3145b1b8ab34Slling 	if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
3146fa9e4066Sahrens 		return (1);
3147fa9e4066Sahrens 
314899653d4eSeschrock 	if (pool_list_count(list) == 0 && argc != 0) {
314999653d4eSeschrock 		pool_list_free(list);
3150fa9e4066Sahrens 		return (1);
315199653d4eSeschrock 	}
3152fa9e4066Sahrens 
3153fa9e4066Sahrens 	if (pool_list_count(list) == 0 && interval == 0) {
315499653d4eSeschrock 		pool_list_free(list);
3155fa9e4066Sahrens 		(void) fprintf(stderr, gettext("no pools available\n"));
3156fa9e4066Sahrens 		return (1);
3157fa9e4066Sahrens 	}
3158fa9e4066Sahrens 
3159fa9e4066Sahrens 	/*
3160fa9e4066Sahrens 	 * Enter the main iostat loop.
3161fa9e4066Sahrens 	 */
3162fa9e4066Sahrens 	cb.cb_list = list;
3163fa9e4066Sahrens 	cb.cb_verbose = verbose;
3164fa9e4066Sahrens 	cb.cb_iteration = 0;
3165fa9e4066Sahrens 	cb.cb_namewidth = 0;
3166fa9e4066Sahrens 
3167fa9e4066Sahrens 	for (;;) {
3168fa9e4066Sahrens 		pool_list_update(list);
3169fa9e4066Sahrens 
3170fa9e4066Sahrens 		if ((npools = pool_list_count(list)) == 0)
3171fa9e4066Sahrens 			break;
3172fa9e4066Sahrens 
3173088e9d47Seschrock 		/*
3174088e9d47Seschrock 		 * Refresh all statistics.  This is done as an explicit step
3175088e9d47Seschrock 		 * before calculating the maximum name width, so that any
3176088e9d47Seschrock 		 * configuration changes are properly accounted for.
3177088e9d47Seschrock 		 */
317899653d4eSeschrock 		(void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
3179088e9d47Seschrock 
3180fa9e4066Sahrens 		/*
3181fa9e4066Sahrens 		 * Iterate over all pools to determine the maximum width
3182fa9e4066Sahrens 		 * for the pool / device name column across all pools.
3183fa9e4066Sahrens 		 */
3184fa9e4066Sahrens 		cb.cb_namewidth = 0;
318599653d4eSeschrock 		(void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3186fa9e4066Sahrens 
318726fd7700SKrishnendu Sadhukhan - Sun Microsystems 		if (timestamp_fmt != NODATE)
318826fd7700SKrishnendu Sadhukhan - Sun Microsystems 			print_timestamp(timestamp_fmt);
318926fd7700SKrishnendu Sadhukhan - Sun Microsystems 
3190fa9e4066Sahrens 		/*
3191fa9e4066Sahrens 		 * If it's the first time, or verbose mode, print the header.
3192fa9e4066Sahrens 		 */
3193fa9e4066Sahrens 		if (++cb.cb_iteration == 1 || verbose)
3194fa9e4066Sahrens 			print_iostat_header(&cb);
3195fa9e4066Sahrens 
319699653d4eSeschrock 		(void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
3197fa9e4066Sahrens 
3198fa9e4066Sahrens 		/*
3199fa9e4066Sahrens 		 * If there's more than one pool, and we're not in verbose mode
3200fa9e4066Sahrens 		 * (which prints a separator for us), then print a separator.
3201fa9e4066Sahrens 		 */
3202fa9e4066Sahrens 		if (npools > 1 && !verbose)
3203fa9e4066Sahrens 			print_iostat_separator(&cb);
3204fa9e4066Sahrens 
3205fa9e4066Sahrens 		if (verbose)
3206fa9e4066Sahrens 			(void) printf("\n");
3207fa9e4066Sahrens 
320839c23413Seschrock 		/*
320939c23413Seschrock 		 * Flush the output so that redirection to a file isn't buffered
321039c23413Seschrock 		 * indefinitely.
321139c23413Seschrock 		 */
321239c23413Seschrock 		(void) fflush(stdout);
321339c23413Seschrock 
3214fa9e4066Sahrens 		if (interval == 0)
3215fa9e4066Sahrens 			break;
3216fa9e4066Sahrens 
3217fa9e4066Sahrens 		if (count != 0 && --count == 0)
3218fa9e4066Sahrens 			break;
3219fa9e4066Sahrens 
3220fa9e4066Sahrens 		(void) sleep(interval);
3221fa9e4066Sahrens 	}
3222fa9e4066Sahrens 
3223fa9e4066Sahrens 	pool_list_free(list);
3224fa9e4066Sahrens 
3225fa9e4066Sahrens 	return (ret);
3226fa9e4066Sahrens }
3227fa9e4066Sahrens 
3228fa9e4066Sahrens typedef struct list_cbdata {
32294263d13fSGeorge Wilson 	boolean_t	cb_verbose;
32304263d13fSGeorge Wilson 	int		cb_namewidth;
323199653d4eSeschrock 	boolean_t	cb_scripted;
3232990b4856Slling 	zprop_list_t	*cb_proplist;
3233c58b3526SAdam Stevko 	boolean_t	cb_literal;
3234fa9e4066Sahrens } list_cbdata_t;
3235fa9e4066Sahrens 
3236fa9e4066Sahrens /*
3237fa9e4066Sahrens  * Given a list of columns to display, output appropriate headers for each one.
3238fa9e4066Sahrens  */
3239990b4856Slling static void
32404263d13fSGeorge Wilson print_header(list_cbdata_t *cb)
3241fa9e4066Sahrens {
32424263d13fSGeorge Wilson 	zprop_list_t *pl = cb->cb_proplist;
3243ad135b5dSChristopher Siden 	char headerbuf[ZPOOL_MAXPROPLEN];
3244990b4856Slling 	const char *header;
3245990b4856Slling 	boolean_t first = B_TRUE;
3246990b4856Slling 	boolean_t right_justify;
32474263d13fSGeorge Wilson 	size_t width = 0;
3248990b4856Slling 
3249990b4856Slling 	for (; pl != NULL; pl = pl->pl_next) {
32504263d13fSGeorge Wilson 		width = pl->pl_width;
32514263d13fSGeorge Wilson 		if (first && cb->cb_verbose) {
32524263d13fSGeorge Wilson 			/*
32534263d13fSGeorge Wilson 			 * Reset the width to accommodate the verbose listing
32544263d13fSGeorge Wilson 			 * of devices.
32554263d13fSGeorge Wilson 			 */
32564263d13fSGeorge Wilson 			width = cb->cb_namewidth;
32574263d13fSGeorge Wilson 		}
32584263d13fSGeorge Wilson 
3259990b4856Slling 		if (!first)
3260fa9e4066Sahrens 			(void) printf("  ");
3261fa9e4066Sahrens 		else
3262990b4856Slling 			first = B_FALSE;
3263990b4856Slling 
3264ad135b5dSChristopher Siden 		right_justify = B_FALSE;
3265ad135b5dSChristopher Siden 		if (pl->pl_prop != ZPROP_INVAL) {
3266ad135b5dSChristopher Siden 			header = zpool_prop_column_name(pl->pl_prop);
3267ad135b5dSChristopher Siden 			right_justify = zpool_prop_align_right(pl->pl_prop);
3268ad135b5dSChristopher Siden 		} else {
3269ad135b5dSChristopher Siden 			int i;
3270ad135b5dSChristopher Siden 
3271ad135b5dSChristopher Siden 			for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
3272ad135b5dSChristopher Siden 				headerbuf[i] = toupper(pl->pl_user_prop[i]);
3273ad135b5dSChristopher Siden 			headerbuf[i] = '\0';
3274ad135b5dSChristopher Siden 			header = headerbuf;
3275ad135b5dSChristopher Siden 		}
3276fa9e4066Sahrens 
3277990b4856Slling 		if (pl->pl_next == NULL && !right_justify)
3278990b4856Slling 			(void) printf("%s", header);
3279990b4856Slling 		else if (right_justify)
32804263d13fSGeorge Wilson 			(void) printf("%*s", width, header);
3281990b4856Slling 		else
32824263d13fSGeorge Wilson 			(void) printf("%-*s", width, header);
32834263d13fSGeorge Wilson 
3284fa9e4066Sahrens 	}
3285fa9e4066Sahrens 
3286fa9e4066Sahrens 	(void) printf("\n");
3287fa9e4066Sahrens }
3288fa9e4066Sahrens 
3289990b4856Slling /*
3290990b4856Slling  * Given a pool and a list of properties, print out all the properties according
3291990b4856Slling  * to the described layout.
3292990b4856Slling  */
3293990b4856Slling static void
32944263d13fSGeorge Wilson print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
3295fa9e4066Sahrens {
32964263d13fSGeorge Wilson 	zprop_list_t *pl = cb->cb_proplist;
3297990b4856Slling 	boolean_t first = B_TRUE;
3298990b4856Slling 	char property[ZPOOL_MAXPROPLEN];
3299990b4856Slling 	char *propstr;
3300990b4856Slling 	boolean_t right_justify;
33014263d13fSGeorge Wilson 	size_t width;
3302990b4856Slling 
3303990b4856Slling 	for (; pl != NULL; pl = pl->pl_next) {
33044263d13fSGeorge Wilson 
33054263d13fSGeorge Wilson 		width = pl->pl_width;
33064263d13fSGeorge Wilson 		if (first && cb->cb_verbose) {
33074263d13fSGeorge Wilson 			/*
33084263d13fSGeorge Wilson 			 * Reset the width to accommodate the verbose listing
33094263d13fSGeorge Wilson 			 * of devices.
33104263d13fSGeorge Wilson 			 */
33114263d13fSGeorge Wilson 			width = cb->cb_namewidth;
33124263d13fSGeorge Wilson 		}
33134263d13fSGeorge Wilson 
3314990b4856Slling 		if (!first) {
33154263d13fSGeorge Wilson 			if (cb->cb_scripted)
3316fa9e4066Sahrens 				(void) printf("\t");
3317fa9e4066Sahrens 			else
3318fa9e4066Sahrens 				(void) printf("  ");
3319990b4856Slling 		} else {
3320990b4856Slling 			first = B_FALSE;
3321fa9e4066Sahrens 		}
3322fa9e4066Sahrens 
3323990b4856Slling 		right_justify = B_FALSE;
3324990b4856Slling 		if (pl->pl_prop != ZPROP_INVAL) {
33257a09f97bSGeorge Wilson 			if (zpool_get_prop(zhp, pl->pl_prop, property,
3326c58b3526SAdam Stevko 			    sizeof (property), NULL, cb->cb_literal) != 0)
3327990b4856Slling 				propstr = "-";
3328fa9e4066Sahrens 			else
3329990b4856Slling 				propstr = property;
3330fa9e4066Sahrens 
3331990b4856Slling 			right_justify = zpool_prop_align_right(pl->pl_prop);
3332ad135b5dSChristopher Siden 		} else if ((zpool_prop_feature(pl->pl_user_prop) ||
3333ad135b5dSChristopher Siden 		    zpool_prop_unsupported(pl->pl_user_prop)) &&
3334ad135b5dSChristopher Siden 		    zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
3335ad135b5dSChristopher Siden 		    sizeof (property)) == 0) {
3336ad135b5dSChristopher Siden 			propstr = property;
3337990b4856Slling 		} else {
3338990b4856Slling 			propstr = "-";
3339990b4856Slling 		}
3340fa9e4066Sahrens 
3341fa9e4066Sahrens 
3342990b4856Slling 		/*
3343990b4856Slling 		 * If this is being called in scripted mode, or if this is the
3344990b4856Slling 		 * last column and it is left-justified, don't include a width
3345990b4856Slling 		 * format specifier.
3346990b4856Slling 		 */
33474263d13fSGeorge Wilson 		if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3348990b4856Slling 			(void) printf("%s", propstr);
3349990b4856Slling 		else if (right_justify)
3350990b4856Slling 			(void) printf("%*s", width, propstr);
3351990b4856Slling 		else
3352990b4856Slling 			(void) printf("%-*s", width, propstr);
3353990b4856Slling 	}
3354fa9e4066Sahrens 
3355990b4856Slling 	(void) printf("\n");
3356990b4856Slling }
3357fa9e4066Sahrens 
33584263d13fSGeorge Wilson static void
33597a09f97bSGeorge Wilson print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
33607a09f97bSGeorge Wilson     boolean_t valid)
33614263d13fSGeorge Wilson {
33624263d13fSGeorge Wilson 	char propval[64];
33634263d13fSGeorge Wilson 	boolean_t fixed;
33644263d13fSGeorge Wilson 	size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
33654263d13fSGeorge Wilson 
33667a09f97bSGeorge Wilson 	switch (prop) {
33677a09f97bSGeorge Wilson 	case ZPOOL_PROP_EXPANDSZ:
336886714001SSerapheim Dimitropoulos 	case ZPOOL_PROP_CHECKPOINT:
33697a09f97bSGeorge Wilson 		if (value == 0)
33707a09f97bSGeorge Wilson 			(void) strlcpy(propval, "-", sizeof (propval));
33717a09f97bSGeorge Wilson 		else
33727a09f97bSGeorge Wilson 			zfs_nicenum(value, propval, sizeof (propval));
33737a09f97bSGeorge Wilson 		break;
33747a09f97bSGeorge Wilson 	case ZPOOL_PROP_FRAGMENTATION:
33757a09f97bSGeorge Wilson 		if (value == ZFS_FRAG_INVALID) {
33767a09f97bSGeorge Wilson 			(void) strlcpy(propval, "-", sizeof (propval));
33777a09f97bSGeorge Wilson 		} else {
33787a09f97bSGeorge Wilson 			(void) snprintf(propval, sizeof (propval), "%llu%%",
33797a09f97bSGeorge Wilson 			    value);
33807a09f97bSGeorge Wilson 		}
33817a09f97bSGeorge Wilson 		break;
33827a09f97bSGeorge Wilson 	case ZPOOL_PROP_CAPACITY:
33832e4c9986SGeorge Wilson 		(void) snprintf(propval, sizeof (propval), "%llu%%", value);
33847a09f97bSGeorge Wilson 		break;
33857a09f97bSGeorge Wilson 	default:
33862e4c9986SGeorge Wilson 		zfs_nicenum(value, propval, sizeof (propval));
33877a09f97bSGeorge Wilson 	}
33887a09f97bSGeorge Wilson 
33897a09f97bSGeorge Wilson 	if (!valid)
33907a09f97bSGeorge Wilson 		(void) strlcpy(propval, "-", sizeof (propval));
33914263d13fSGeorge Wilson 
33924263d13fSGeorge Wilson 	if (scripted)
33934263d13fSGeorge Wilson 		(void) printf("\t%s", propval);
33944263d13fSGeorge Wilson 	else
33954263d13fSGeorge Wilson 		(void) printf("  %*s", width, propval);
33964263d13fSGeorge Wilson }
33974263d13fSGeorge Wilson 
33984263d13fSGeorge Wilson void
33994263d13fSGeorge Wilson print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
34004263d13fSGeorge Wilson     list_cbdata_t *cb, int depth)
34014263d13fSGeorge Wilson {
34024263d13fSGeorge Wilson 	nvlist_t **child;
34034263d13fSGeorge Wilson 	vdev_stat_t *vs;
34044263d13fSGeorge Wilson 	uint_t c, children;
34054263d13fSGeorge Wilson 	char *vname;
34064263d13fSGeorge Wilson 	boolean_t scripted = cb->cb_scripted;
340752244c09SJohn Wren Kennedy 	uint64_t islog = B_FALSE;
340852244c09SJohn Wren Kennedy 	boolean_t haslog = B_FALSE;
340952244c09SJohn Wren Kennedy 	char *dashes = "%-*s      -      -      -         -      -      -\n";
34104263d13fSGeorge Wilson 
34114263d13fSGeorge Wilson 	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
34124263d13fSGeorge Wilson 	    (uint64_t **)&vs, &c) == 0);
34134263d13fSGeorge Wilson 
34144263d13fSGeorge Wilson 	if (name != NULL) {
34157a09f97bSGeorge Wilson 		boolean_t toplevel = (vs->vs_space != 0);
34167a09f97bSGeorge Wilson 		uint64_t cap;
34177a09f97bSGeorge Wilson 
34185cabbc6bSPrashanth Sreenivasa 		if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
34195cabbc6bSPrashanth Sreenivasa 			return;
34205cabbc6bSPrashanth Sreenivasa 
34214263d13fSGeorge Wilson 		if (scripted)
34224263d13fSGeorge Wilson 			(void) printf("\t%s", name);
34234263d13fSGeorge Wilson 		else if (strlen(name) + depth > cb->cb_namewidth)
34244263d13fSGeorge Wilson 			(void) printf("%*s%s", depth, "", name);
34254263d13fSGeorge Wilson 		else
34264263d13fSGeorge Wilson 			(void) printf("%*s%s%*s", depth, "", name,
34274263d13fSGeorge Wilson 			    (int)(cb->cb_namewidth - strlen(name) - depth), "");
34284263d13fSGeorge Wilson 
34297a09f97bSGeorge Wilson 		/*
34307a09f97bSGeorge Wilson 		 * Print the properties for the individual vdevs. Some
34317a09f97bSGeorge Wilson 		 * properties are only applicable to toplevel vdevs. The
34327a09f97bSGeorge Wilson 		 * 'toplevel' boolean value is passed to the print_one_column()
34337a09f97bSGeorge Wilson 		 * to indicate that the value is valid.
34347a09f97bSGeorge Wilson 		 */
34357a09f97bSGeorge Wilson 		print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted,
34367a09f97bSGeorge Wilson 		    toplevel);
34377a09f97bSGeorge Wilson 		print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
34387a09f97bSGeorge Wilson 		    toplevel);
34397a09f97bSGeorge Wilson 		print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
34407a09f97bSGeorge Wilson 		    scripted, toplevel);
344186714001SSerapheim Dimitropoulos 		print_one_column(ZPOOL_PROP_CHECKPOINT,
344286714001SSerapheim Dimitropoulos 		    vs->vs_checkpoint_space, scripted, toplevel);
34437a09f97bSGeorge Wilson 		print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
34447a09f97bSGeorge Wilson 		    B_TRUE);
34457a09f97bSGeorge Wilson 		print_one_column(ZPOOL_PROP_FRAGMENTATION,
34467a09f97bSGeorge Wilson 		    vs->vs_fragmentation, scripted,
34477a09f97bSGeorge Wilson 		    (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel));
34487a09f97bSGeorge Wilson 		cap = (vs->vs_space == 0) ? 0 :
34497a09f97bSGeorge Wilson 		    (vs->vs_alloc * 100 / vs->vs_space);
34507a09f97bSGeorge Wilson 		print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel);
34514263d13fSGeorge Wilson 		(void) printf("\n");
34524263d13fSGeorge Wilson 	}
34534263d13fSGeorge Wilson 
34544263d13fSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
34554263d13fSGeorge Wilson 	    &child, &children) != 0)
34564263d13fSGeorge Wilson 		return;
34574263d13fSGeorge Wilson 
34584263d13fSGeorge Wilson 	for (c = 0; c < children; c++) {
34594263d13fSGeorge Wilson 		uint64_t ishole = B_FALSE;
34604263d13fSGeorge Wilson 
34614263d13fSGeorge Wilson 		if (nvlist_lookup_uint64(child[c],
34624263d13fSGeorge Wilson 		    ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
34634263d13fSGeorge Wilson 			continue;
34644263d13fSGeorge Wilson 
346552244c09SJohn Wren Kennedy 		if (nvlist_lookup_uint64(child[c],
346652244c09SJohn Wren Kennedy 		    ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog) {
346752244c09SJohn Wren Kennedy 			haslog = B_TRUE;
346852244c09SJohn Wren Kennedy 			continue;
346952244c09SJohn Wren Kennedy 		}
347052244c09SJohn Wren Kennedy 
34714263d13fSGeorge Wilson 		vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
34724263d13fSGeorge Wilson 		print_list_stats(zhp, vname, child[c], cb, depth + 2);
34734263d13fSGeorge Wilson 		free(vname);
34744263d13fSGeorge Wilson 	}
34754263d13fSGeorge Wilson 
347652244c09SJohn Wren Kennedy 	if (haslog == B_TRUE) {
347752244c09SJohn Wren Kennedy 		/* LINTED E_SEC_PRINTF_VAR_FMT */
347852244c09SJohn Wren Kennedy 		(void) printf(dashes, cb->cb_namewidth, "log");
347952244c09SJohn Wren Kennedy 		for (c = 0; c < children; c++) {
348052244c09SJohn Wren Kennedy 			if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
348152244c09SJohn Wren Kennedy 			    &islog) != 0 || !islog)
348252244c09SJohn Wren Kennedy 				continue;
348352244c09SJohn Wren Kennedy 			vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
348452244c09SJohn Wren Kennedy 			print_list_stats(zhp, vname, child[c], cb, depth + 2);
348552244c09SJohn Wren Kennedy 			free(vname);
348652244c09SJohn Wren Kennedy 		}
348752244c09SJohn Wren Kennedy 	}
348852244c09SJohn Wren Kennedy 
34894263d13fSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
349052244c09SJohn Wren Kennedy 	    &child, &children) == 0 && children > 0) {
349152244c09SJohn Wren Kennedy 		/* LINTED E_SEC_PRINTF_VAR_FMT */
349252244c09SJohn Wren Kennedy 		(void) printf(dashes, cb->cb_namewidth, "cache");
349352244c09SJohn Wren Kennedy 		for (c = 0; c < children; c++) {
349452244c09SJohn Wren Kennedy 			vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
349552244c09SJohn Wren Kennedy 			print_list_stats(zhp, vname, child[c], cb, depth + 2);
349652244c09SJohn Wren Kennedy 			free(vname);
349752244c09SJohn Wren Kennedy 		}
349852244c09SJohn Wren Kennedy 	}
34994263d13fSGeorge Wilson 
350052244c09SJohn Wren Kennedy 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child,
350152244c09SJohn Wren Kennedy 	    &children) == 0 && children > 0) {
350252244c09SJohn Wren Kennedy 		/* LINTED E_SEC_PRINTF_VAR_FMT */
350352244c09SJohn Wren Kennedy 		(void) printf(dashes, cb->cb_namewidth, "spare");
35044263d13fSGeorge Wilson 		for (c = 0; c < children; c++) {
350552244c09SJohn Wren Kennedy 			vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
35064263d13fSGeorge Wilson 			print_list_stats(zhp, vname, child[c], cb, depth + 2);
35074263d13fSGeorge Wilson 			free(vname);
35084263d13fSGeorge Wilson 		}
35094263d13fSGeorge Wilson 	}
35104263d13fSGeorge Wilson }
35114263d13fSGeorge Wilson 
35124263d13fSGeorge Wilson 
3513990b4856Slling /*
3514990b4856Slling  * Generic callback function to list a pool.
3515990b4856Slling  */
3516990b4856Slling int
3517990b4856Slling list_callback(zpool_handle_t *zhp, void *data)
3518990b4856Slling {
3519990b4856Slling 	list_cbdata_t *cbp = data;
35204263d13fSGeorge Wilson 	nvlist_t *config;
35214263d13fSGeorge Wilson 	nvlist_t *nvroot;
3522fa9e4066Sahrens 
35234263d13fSGeorge Wilson 	config = zpool_get_config(zhp, NULL);
3524fa9e4066Sahrens 
35254263d13fSGeorge Wilson 	print_pool(zhp, cbp);
35264263d13fSGeorge Wilson 	if (!cbp->cb_verbose)
35274263d13fSGeorge Wilson 		return (0);
35284263d13fSGeorge Wilson 
35294263d13fSGeorge Wilson 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
35304263d13fSGeorge Wilson 	    &nvroot) == 0);
35314263d13fSGeorge Wilson 	print_list_stats(zhp, NULL, nvroot, cbp, 0);
3532fa9e4066Sahrens 
3533fa9e4066Sahrens 	return (0);
3534fa9e4066Sahrens }
3535fa9e4066Sahrens 
3536fa9e4066Sahrens /*
3537c58b3526SAdam Stevko  * zpool list [-Hp] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3538fa9e4066Sahrens  *
3539990b4856Slling  *	-H	Scripted mode.  Don't display headers, and separate properties
3540990b4856Slling  *		by a single tab.
3541990b4856Slling  *	-o	List of properties to display.  Defaults to
35427a09f97bSGeorge Wilson  *		"name,size,allocated,free,expandsize,fragmentation,capacity,"
35437a09f97bSGeorge Wilson  *		"dedupratio,health,altroot"
354404e56356SAndriy Gapon  *	-p	Diplay values in parsable (exact) format.
35453f9d6ad7SLin Ling  *	-T	Display a timestamp in date(1) or Unix format
3546fa9e4066Sahrens  *
3547fa9e4066Sahrens  * List all pools in the system, whether or not they're healthy.  Output space
3548fa9e4066Sahrens  * statistics for each one, as well as health status summary.
3549fa9e4066Sahrens  */
3550fa9e4066Sahrens int
3551fa9e4066Sahrens zpool_do_list(int argc, char **argv)
3552fa9e4066Sahrens {
3553fa9e4066Sahrens 	int c;
3554fa9e4066Sahrens 	int ret;
3555fa9e4066Sahrens 	list_cbdata_t cb = { 0 };
3556990b4856Slling 	static char default_props[] =
355786714001SSerapheim Dimitropoulos 	    "name,size,allocated,free,checkpoint,expandsize,fragmentation,"
355886714001SSerapheim Dimitropoulos 	    "capacity,dedupratio,health,altroot";
3559990b4856Slling 	char *props = default_props;
35603f9d6ad7SLin Ling 	unsigned long interval = 0, count = 0;
35614263d13fSGeorge Wilson 	zpool_list_t *list;
35624263d13fSGeorge Wilson 	boolean_t first = B_TRUE;
3563fa9e4066Sahrens 
3564fa9e4066Sahrens 	/* check options */
3565c58b3526SAdam Stevko 	while ((c = getopt(argc, argv, ":Ho:pT:v")) != -1) {
3566fa9e4066Sahrens 		switch (c) {
3567fa9e4066Sahrens 		case 'H':
356899653d4eSeschrock 			cb.cb_scripted = B_TRUE;
3569fa9e4066Sahrens 			break;
3570fa9e4066Sahrens 		case 'o':
3571990b4856Slling 			props = optarg;
3572fa9e4066Sahrens 			break;
3573c58b3526SAdam Stevko 		case 'p':
3574c58b3526SAdam Stevko 			cb.cb_literal = B_TRUE;
3575c58b3526SAdam Stevko 			break;
35763f9d6ad7SLin Ling 		case 'T':
35773f9d6ad7SLin Ling 			get_timestamp_arg(*optarg);
35783f9d6ad7SLin Ling 			break;
35794263d13fSGeorge Wilson 		case 'v':
35804263d13fSGeorge Wilson 			cb.cb_verbose = B_TRUE;
35814263d13fSGeorge Wilson 			break;
3582fa9e4066Sahrens 		case ':':
3583fa9e4066Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
3584fa9e4066Sahrens 			    "'%c' option\n"), optopt);
358599653d4eSeschrock 			usage(B_FALSE);
3586fa9e4066Sahrens 			break;
3587fa9e4066Sahrens 		case '?':
3588fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3589fa9e4066Sahrens 			    optopt);
359099653d4eSeschrock 			usage(B_FALSE);
3591fa9e4066Sahrens 		}
3592fa9e4066Sahrens 	}
3593fa9e4066Sahrens 
3594fa9e4066Sahrens 	argc -= optind;
3595fa9e4066Sahrens 	argv += optind;
3596fa9e4066Sahrens 
35973f9d6ad7SLin Ling 	get_interval_count(&argc, argv, &interval, &count);
35983f9d6ad7SLin Ling 
3599990b4856Slling 	if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3600990b4856Slling 		usage(B_FALSE);
3601fa9e4066Sahrens 
36023f9d6ad7SLin Ling 	for (;;) {
3603cd67d23dSGeorge Wilson 		if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
3604cd67d23dSGeorge Wilson 		    &ret)) == NULL)
3605cd67d23dSGeorge Wilson 			return (1);
36064263d13fSGeorge Wilson 
36074263d13fSGeorge Wilson 		if (pool_list_count(list) == 0)
36084263d13fSGeorge Wilson 			break;
36094263d13fSGeorge Wilson 
36104263d13fSGeorge Wilson 		cb.cb_namewidth = 0;
36114263d13fSGeorge Wilson 		(void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3612990b4856Slling 
36133f9d6ad7SLin Ling 		if (timestamp_fmt != NODATE)
36143f9d6ad7SLin Ling 			print_timestamp(timestamp_fmt);
3615fa9e4066Sahrens 
36164263d13fSGeorge Wilson 		if (!cb.cb_scripted && (first || cb.cb_verbose)) {
36174263d13fSGeorge Wilson 			print_header(&cb);
36184263d13fSGeorge Wilson 			first = B_FALSE;
36193f9d6ad7SLin Ling 		}
36204263d13fSGeorge Wilson 		ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
36213f9d6ad7SLin Ling 
36223f9d6ad7SLin Ling 		if (interval == 0)
36233f9d6ad7SLin Ling 			break;
36243f9d6ad7SLin Ling 
36253f9d6ad7SLin Ling 		if (count != 0 && --count == 0)
36263f9d6ad7SLin Ling 			break;
36273f9d6ad7SLin Ling 
3628cd67d23dSGeorge Wilson 		pool_list_free(list);
36293f9d6ad7SLin Ling 		(void) sleep(interval);
3630fa9e4066Sahrens 	}
3631fa9e4066Sahrens 
3632cd67d23dSGeorge Wilson 	if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3633cd67d23dSGeorge Wilson 		(void) printf(gettext("no pools available\n"));
3634cd67d23dSGeorge Wilson 		ret = 0;
3635cd67d23dSGeorge Wilson 	}
3636cd67d23dSGeorge Wilson 
3637cd67d23dSGeorge Wilson 	pool_list_free(list);
36383f9d6ad7SLin Ling 	zprop_free_list(cb.cb_proplist);
3639fa9e4066Sahrens 	return (ret);
3640fa9e4066Sahrens }
3641fa9e4066Sahrens 
3642fa9e4066Sahrens static int
3643fa9e4066Sahrens zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3644fa9e4066Sahrens {
364599653d4eSeschrock 	boolean_t force = B_FALSE;
3646fa9e4066Sahrens 	int c;
3647fa9e4066Sahrens 	nvlist_t *nvroot;
3648fa9e4066Sahrens 	char *poolname, *old_disk, *new_disk;
3649fa9e4066Sahrens 	zpool_handle_t *zhp;
36507855d95bSToomas Soome 	zpool_boot_label_t boot_type;
36517855d95bSToomas Soome 	uint64_t boot_size;
365299653d4eSeschrock 	int ret;
3653fa9e4066Sahrens 
3654fa9e4066Sahrens 	/* check options */
3655fa9e4066Sahrens 	while ((c = getopt(argc, argv, "f")) != -1) {
3656fa9e4066Sahrens 		switch (c) {
3657fa9e4066Sahrens 		case 'f':
365899653d4eSeschrock 			force = B_TRUE;
3659fa9e4066Sahrens 			break;
3660fa9e4066Sahrens 		case '?':
3661fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3662fa9e4066Sahrens 			    optopt);
366399653d4eSeschrock 			usage(B_FALSE);
3664fa9e4066Sahrens 		}
3665fa9e4066Sahrens 	}
3666fa9e4066Sahrens 
3667fa9e4066Sahrens 	argc -= optind;
3668fa9e4066Sahrens 	argv += optind;
3669fa9e4066Sahrens 
3670fa9e4066Sahrens 	/* get pool name and check number of arguments */
3671fa9e4066Sahrens 	if (argc < 1) {
3672fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
367399653d4eSeschrock 		usage(B_FALSE);
3674fa9e4066Sahrens 	}
3675fa9e4066Sahrens 
3676fa9e4066Sahrens 	poolname = argv[0];
3677fa9e4066Sahrens 
3678fa9e4066Sahrens 	if (argc < 2) {
3679fa9e4066Sahrens 		(void) fprintf(stderr,
3680fa9e4066Sahrens 		    gettext("missing <device> specification\n"));
368199653d4eSeschrock 		usage(B_FALSE);
3682fa9e4066Sahrens 	}
3683fa9e4066Sahrens 
3684fa9e4066Sahrens 	old_disk = argv[1];
3685fa9e4066Sahrens 
3686fa9e4066Sahrens 	if (argc < 3) {
3687fa9e4066Sahrens 		if (!replacing) {
3688fa9e4066Sahrens 			(void) fprintf(stderr,
3689fa9e4066Sahrens 			    gettext("missing <new_device> specification\n"));
369099653d4eSeschrock 			usage(B_FALSE);
3691fa9e4066Sahrens 		}
3692fa9e4066Sahrens 		new_disk = old_disk;
3693fa9e4066Sahrens 		argc -= 1;
3694fa9e4066Sahrens 		argv += 1;
3695fa9e4066Sahrens 	} else {
3696fa9e4066Sahrens 		new_disk = argv[2];
3697fa9e4066Sahrens 		argc -= 2;
3698fa9e4066Sahrens 		argv += 2;
3699fa9e4066Sahrens 	}
3700fa9e4066Sahrens 
3701fa9e4066Sahrens 	if (argc > 1) {
3702fa9e4066Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
370399653d4eSeschrock 		usage(B_FALSE);
3704fa9e4066Sahrens 	}
3705fa9e4066Sahrens 
370699653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3707fa9e4066Sahrens 		return (1);
3708fa9e4066Sahrens 
37098488aeb5Staylor 	if (zpool_get_config(zhp, NULL) == NULL) {
3710fa9e4066Sahrens 		(void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3711fa9e4066Sahrens 		    poolname);
3712fa9e4066Sahrens 		zpool_close(zhp);
3713fa9e4066Sahrens 		return (1);
3714fa9e4066Sahrens 	}
3715fa9e4066Sahrens 
37167855d95bSToomas Soome 	if (zpool_is_bootable(zhp))
37177855d95bSToomas Soome 		boot_type = ZPOOL_COPY_BOOT_LABEL;
37187855d95bSToomas Soome 	else
37197855d95bSToomas Soome 		boot_type = ZPOOL_NO_BOOT_LABEL;
37207855d95bSToomas Soome 
37217855d95bSToomas Soome 	boot_size = zpool_get_prop_int(zhp, ZPOOL_PROP_BOOTSIZE, NULL);
3722705040edSEric Taylor 	nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
37237855d95bSToomas Soome 	    boot_type, boot_size, argc, argv);
3724fa9e4066Sahrens 	if (nvroot == NULL) {
3725fa9e4066Sahrens 		zpool_close(zhp);
3726fa9e4066Sahrens 		return (1);
3727fa9e4066Sahrens 	}
3728fa9e4066Sahrens 
372999653d4eSeschrock 	ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
373099653d4eSeschrock 
373199653d4eSeschrock 	nvlist_free(nvroot);
373299653d4eSeschrock 	zpool_close(zhp);
373399653d4eSeschrock 
373499653d4eSeschrock 	return (ret);
3735fa9e4066Sahrens }
3736fa9e4066Sahrens 
3737fa9e4066Sahrens /*
3738fa9e4066Sahrens  * zpool replace [-f] <pool> <device> <new_device>
3739fa9e4066Sahrens  *
3740fa9e4066Sahrens  *	-f	Force attach, even if <new_device> appears to be in use.
3741fa9e4066Sahrens  *
3742fa9e4066Sahrens  * Replace <device> with <new_device>.
3743fa9e4066Sahrens  */
3744fa9e4066Sahrens /* ARGSUSED */
3745fa9e4066Sahrens int
3746fa9e4066Sahrens zpool_do_replace(int argc, char **argv)
3747fa9e4066Sahrens {
3748fa9e4066Sahrens 	return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3749fa9e4066Sahrens }
3750fa9e4066Sahrens 
3751fa9e4066Sahrens /*
3752fa9e4066Sahrens  * zpool attach [-f] <pool> <device> <new_device>
3753fa9e4066Sahrens  *
3754fa9e4066Sahrens  *	-f	Force attach, even if <new_device> appears to be in use.
3755fa9e4066Sahrens  *
3756fa9e4066Sahrens  * Attach <new_device> to the mirror containing <device>.  If <device> is not
3757fa9e4066Sahrens  * part of a mirror, then <device> will be transformed into a mirror of
3758fa9e4066Sahrens  * <device> and <new_device>.  In either case, <new_device> will begin life
3759fa9e4066Sahrens  * with a DTL of [0, now], and will immediately begin to resilver itself.
3760fa9e4066Sahrens  */
3761fa9e4066Sahrens int
3762fa9e4066Sahrens zpool_do_attach(int argc, char **argv)
3763fa9e4066Sahrens {
3764fa9e4066Sahrens 	return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3765fa9e4066Sahrens }
3766fa9e4066Sahrens 
3767fa9e4066Sahrens /*
3768fa9e4066Sahrens  * zpool detach [-f] <pool> <device>
3769fa9e4066Sahrens  *
3770fa9e4066Sahrens  *	-f	Force detach of <device>, even if DTLs argue against it
3771fa9e4066Sahrens  *		(not supported yet)
3772fa9e4066Sahrens  *
3773fa9e4066Sahrens  * Detach a device from a mirror.  The operation will be refused if <device>
3774fa9e4066Sahrens  * is the last device in the mirror, or if the DTLs indicate that this device
3775fa9e4066Sahrens  * has the only valid copy of some data.
3776fa9e4066Sahrens  */
3777fa9e4066Sahrens /* ARGSUSED */
3778fa9e4066Sahrens int
3779fa9e4066Sahrens zpool_do_detach(int argc, char **argv)
3780fa9e4066Sahrens {
3781fa9e4066Sahrens 	int c;
3782fa9e4066Sahrens 	char *poolname, *path;
3783fa9e4066Sahrens 	zpool_handle_t *zhp;
378499653d4eSeschrock 	int ret;
3785fa9e4066Sahrens 
3786fa9e4066Sahrens 	/* check options */
3787fa9e4066Sahrens 	while ((c = getopt(argc, argv, "f")) != -1) {
3788fa9e4066Sahrens 		switch (c) {
3789fa9e4066Sahrens 		case 'f':
3790fa9e4066Sahrens 		case '?':
3791fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3792fa9e4066Sahrens 			    optopt);
379399653d4eSeschrock 			usage(B_FALSE);
3794fa9e4066Sahrens 		}
3795fa9e4066Sahrens 	}
3796fa9e4066Sahrens 
3797fa9e4066Sahrens 	argc -= optind;
3798fa9e4066Sahrens 	argv += optind;
3799fa9e4066Sahrens 
3800fa9e4066Sahrens 	/* get pool name and check number of arguments */
3801fa9e4066Sahrens 	if (argc < 1) {
3802fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
380399653d4eSeschrock 		usage(B_FALSE);
3804fa9e4066Sahrens 	}
3805fa9e4066Sahrens 
3806fa9e4066Sahrens 	if (argc < 2) {
3807fa9e4066Sahrens 		(void) fprintf(stderr,
3808fa9e4066Sahrens 		    gettext("missing <device> specification\n"));
380999653d4eSeschrock 		usage(B_FALSE);
3810fa9e4066Sahrens 	}
3811fa9e4066Sahrens 
3812fa9e4066Sahrens 	poolname = argv[0];
3813fa9e4066Sahrens 	path = argv[1];
3814fa9e4066Sahrens 
381599653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3816fa9e4066Sahrens 		return (1);
3817fa9e4066Sahrens 
381899653d4eSeschrock 	ret = zpool_vdev_detach(zhp, path);
381999653d4eSeschrock 
382099653d4eSeschrock 	zpool_close(zhp);
382199653d4eSeschrock 
382299653d4eSeschrock 	return (ret);
3823fa9e4066Sahrens }
3824fa9e4066Sahrens 
38251195e687SMark J Musante /*
38261195e687SMark J Musante  * zpool split [-n] [-o prop=val] ...
38271195e687SMark J Musante  *		[-o mntopt] ...
38281195e687SMark J Musante  *		[-R altroot] <pool> <newpool> [<device> ...]
38291195e687SMark J Musante  *
38301195e687SMark J Musante  *	-n	Do not split the pool, but display the resulting layout if
38311195e687SMark J Musante  *		it were to be split.
38321195e687SMark J Musante  *	-o	Set property=value, or set mount options.
38331195e687SMark J Musante  *	-R	Mount the split-off pool under an alternate root.
38341195e687SMark J Musante  *
38351195e687SMark J Musante  * Splits the named pool and gives it the new pool name.  Devices to be split
38361195e687SMark J Musante  * off may be listed, provided that no more than one device is specified
38371195e687SMark J Musante  * per top-level vdev mirror.  The newly split pool is left in an exported
38381195e687SMark J Musante  * state unless -R is specified.
38391195e687SMark J Musante  *
38401195e687SMark J Musante  * Restrictions: the top-level of the pool pool must only be made up of
38411195e687SMark J Musante  * mirrors; all devices in the pool must be healthy; no device may be
38421195e687SMark J Musante  * undergoing a resilvering operation.
38431195e687SMark J Musante  */
38441195e687SMark J Musante int
38451195e687SMark J Musante zpool_do_split(int argc, char **argv)
38461195e687SMark J Musante {
38471195e687SMark J Musante 	char *srcpool, *newpool, *propval;
38481195e687SMark J Musante 	char *mntopts = NULL;
38491195e687SMark J Musante 	splitflags_t flags;
38501195e687SMark J Musante 	int c, ret = 0;
38511195e687SMark J Musante 	zpool_handle_t *zhp;
38521195e687SMark J Musante 	nvlist_t *config, *props = NULL;
38531195e687SMark J Musante 
38541195e687SMark J Musante 	flags.dryrun = B_FALSE;
38551195e687SMark J Musante 	flags.import = B_FALSE;
38561195e687SMark J Musante 
38571195e687SMark J Musante 	/* check options */
38581195e687SMark J Musante 	while ((c = getopt(argc, argv, ":R:no:")) != -1) {
38591195e687SMark J Musante 		switch (c) {
38601195e687SMark J Musante 		case 'R':
38611195e687SMark J Musante 			flags.import = B_TRUE;
38621195e687SMark J Musante 			if (add_prop_list(
38631195e687SMark J Musante 			    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
38641195e687SMark J Musante 			    &props, B_TRUE) != 0) {
3865aab83bb8SJosef 'Jeff' Sipek 				nvlist_free(props);
38661195e687SMark J Musante 				usage(B_FALSE);
38671195e687SMark J Musante 			}
38681195e687SMark J Musante 			break;
38691195e687SMark J Musante 		case 'n':
38701195e687SMark J Musante 			flags.dryrun = B_TRUE;
38711195e687SMark J Musante 			break;
38721195e687SMark J Musante 		case 'o':
38731195e687SMark J Musante 			if ((propval = strchr(optarg, '=')) != NULL) {
38741195e687SMark J Musante 				*propval = '\0';
38751195e687SMark J Musante 				propval++;
38761195e687SMark J Musante 				if (add_prop_list(optarg, propval,
38771195e687SMark J Musante 				    &props, B_TRUE) != 0) {
3878aab83bb8SJosef 'Jeff' Sipek 					nvlist_free(props);
38791195e687SMark J Musante 					usage(B_FALSE);
38801195e687SMark J Musante 				}
38811195e687SMark J Musante 			} else {
38821195e687SMark J Musante 				mntopts = optarg;
38831195e687SMark J Musante 			}
38841195e687SMark J Musante 			break;
38851195e687SMark J Musante 		case ':':
38861195e687SMark J Musante 			(void) fprintf(stderr, gettext("missing argument for "
38871195e687SMark J Musante 			    "'%c' option\n"), optopt);
38881195e687SMark J Musante 			usage(B_FALSE);
38891195e687SMark J Musante 			break;
38901195e687SMark J Musante 		case '?':
38911195e687SMark J Musante 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
38921195e687SMark J Musante 			    optopt);
38931195e687SMark J Musante 			usage(B_FALSE);
38941195e687SMark J Musante 			break;
38951195e687SMark J Musante 		}
38961195e687SMark J Musante 	}
38971195e687SMark J Musante 
38981195e687SMark J Musante 	if (!flags.import && mntopts != NULL) {
38991195e687SMark J Musante 		(void) fprintf(stderr, gettext("setting mntopts is only "
39001195e687SMark J Musante 		    "valid when importing the pool\n"));
39011195e687SMark J Musante 		usage(B_FALSE);
39021195e687SMark J Musante 	}
39031195e687SMark J Musante 
39041195e687SMark J Musante 	argc -= optind;
39051195e687SMark J Musante 	argv += optind;
39061195e687SMark J Musante 
39071195e687SMark J Musante 	if (argc < 1) {
39081195e687SMark J Musante 		(void) fprintf(stderr, gettext("Missing pool name\n"));
39091195e687SMark J Musante 		usage(B_FALSE);
39101195e687SMark J Musante 	}
39111195e687SMark J Musante 	if (argc < 2) {
39121195e687SMark J Musante 		(void) fprintf(stderr, gettext("Missing new pool name\n"));
39131195e687SMark J Musante 		usage(B_FALSE);
39141195e687SMark J Musante 	}
39151195e687SMark J Musante 
39161195e687SMark J Musante 	srcpool = argv[0];
39171195e687SMark J Musante 	newpool = argv[1];
39181195e687SMark J Musante 
39191195e687SMark J Musante 	argc -= 2;
39201195e687SMark J Musante 	argv += 2;
39211195e687SMark J Musante 
39221195e687SMark J Musante 	if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
39231195e687SMark J Musante 		return (1);
39241195e687SMark J Musante 
39251195e687SMark J Musante 	config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
39261195e687SMark J Musante 	if (config == NULL) {
39271195e687SMark J Musante 		ret = 1;
39281195e687SMark J Musante 	} else {
39291195e687SMark J Musante 		if (flags.dryrun) {
39301195e687SMark J Musante 			(void) printf(gettext("would create '%s' with the "
39311195e687SMark J Musante 			    "following layout:\n\n"), newpool);
39321195e687SMark J Musante 			print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
39331195e687SMark J Musante 		}
39341195e687SMark J Musante 		nvlist_free(config);
39351195e687SMark J Musante 	}
39361195e687SMark J Musante 
39371195e687SMark J Musante 	zpool_close(zhp);
39381195e687SMark J Musante 
39391195e687SMark J Musante 	if (ret != 0 || flags.dryrun || !flags.import)
39401195e687SMark J Musante 		return (ret);
39411195e687SMark J Musante 
39421195e687SMark J Musante 	/*
39431195e687SMark J Musante 	 * The split was successful. Now we need to open the new
39441195e687SMark J Musante 	 * pool and import it.
39451195e687SMark J Musante 	 */
39461195e687SMark J Musante 	if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
39471195e687SMark J Musante 		return (1);
39481195e687SMark J Musante 	if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
39491195e687SMark J Musante 	    zpool_enable_datasets(zhp, mntopts, 0) != 0) {
39501195e687SMark J Musante 		ret = 1;
3951fe7cd8aaSCyril Plisko 		(void) fprintf(stderr, gettext("Split was successful, but "
39521195e687SMark J Musante 		    "the datasets could not all be mounted\n"));
39531195e687SMark J Musante 		(void) fprintf(stderr, gettext("Try doing '%s' with a "
39541195e687SMark J Musante 		    "different altroot\n"), "zpool import");
39551195e687SMark J Musante 	}
39561195e687SMark J Musante 	zpool_close(zhp);
39571195e687SMark J Musante 
39581195e687SMark J Musante 	return (ret);
39591195e687SMark J Musante }
39601195e687SMark J Musante 
39611195e687SMark J Musante 
39621195e687SMark J Musante 
3963fa9e4066Sahrens /*
3964441d80aaSlling  * zpool online <pool> <device> ...
3965fa9e4066Sahrens  */
3966fa9e4066Sahrens int
3967fa9e4066Sahrens zpool_do_online(int argc, char **argv)
3968fa9e4066Sahrens {
3969fa9e4066Sahrens 	int c, i;
3970fa9e4066Sahrens 	char *poolname;
3971fa9e4066Sahrens 	zpool_handle_t *zhp;
3972fa9e4066Sahrens 	int ret = 0;
39733d7072f8Seschrock 	vdev_state_t newstate;
3974573ca77eSGeorge Wilson 	int flags = 0;
3975fa9e4066Sahrens 
3976fa9e4066Sahrens 	/* check options */
3977573ca77eSGeorge Wilson 	while ((c = getopt(argc, argv, "et")) != -1) {
3978fa9e4066Sahrens 		switch (c) {
3979573ca77eSGeorge Wilson 		case 'e':
3980573ca77eSGeorge Wilson 			flags |= ZFS_ONLINE_EXPAND;
3981573ca77eSGeorge Wilson 			break;
3982fa9e4066Sahrens 		case 't':
3983fa9e4066Sahrens 		case '?':
3984fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3985fa9e4066Sahrens 			    optopt);
398699653d4eSeschrock 			usage(B_FALSE);
3987fa9e4066Sahrens 		}
3988fa9e4066Sahrens 	}
3989fa9e4066Sahrens 
3990fa9e4066Sahrens 	argc -= optind;
3991fa9e4066Sahrens 	argv += optind;
3992fa9e4066Sahrens 
3993fa9e4066Sahrens 	/* get pool name and check number of arguments */
3994fa9e4066Sahrens 	if (argc < 1) {
3995fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name\n"));
399699653d4eSeschrock 		usage(B_FALSE);
3997fa9e4066Sahrens 	}
3998fa9e4066Sahrens 	if (argc < 2) {
3999fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing device name\n"));
400099653d4eSeschrock 		usage(B_FALSE);
4001fa9e4066Sahrens 	}
4002fa9e4066Sahrens 
4003fa9e4066Sahrens 	poolname = argv[0];
4004fa9e4066Sahrens 
400599653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
4006fa9e4066Sahrens 		return (1);
4007fa9e4066Sahrens 
40083d7072f8Seschrock 	for (i = 1; i < argc; i++) {
4009573ca77eSGeorge Wilson 		if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
40103d7072f8Seschrock 			if (newstate != VDEV_STATE_HEALTHY) {
40113d7072f8Seschrock 				(void) printf(gettext("warning: device '%s' "
40123d7072f8Seschrock 				    "onlined, but remains in faulted state\n"),
40133d7072f8Seschrock 				    argv[i]);
40143d7072f8Seschrock 				if (newstate == VDEV_STATE_FAULTED)
40153d7072f8Seschrock 					(void) printf(gettext("use 'zpool "
40163d7072f8Seschrock 					    "clear' to restore a faulted "
40173d7072f8Seschrock 					    "device\n"));
40183d7072f8Seschrock 				else
40193d7072f8Seschrock 					(void) printf(gettext("use 'zpool "
40203d7072f8Seschrock 					    "replace' to replace devices "
40213d7072f8Seschrock 					    "that are no longer present\n"));
40223d7072f8Seschrock 			}
40233d7072f8Seschrock 		} else {
4024fa9e4066Sahrens 			ret = 1;
40253d7072f8Seschrock 		}
40263d7072f8Seschrock 	}
4027fa9e4066Sahrens 
402899653d4eSeschrock 	zpool_close(zhp);
402999653d4eSeschrock 
4030fa9e4066Sahrens 	return (ret);
4031fa9e4066Sahrens }
4032fa9e4066Sahrens 
4033fa9e4066Sahrens /*
4034441d80aaSlling  * zpool offline [-ft] <pool> <device> ...
4035fa9e4066Sahrens  *
4036fa9e4066Sahrens  *	-f	Force the device into the offline state, even if doing
4037fa9e4066Sahrens  *		so would appear to compromise pool availability.
4038fa9e4066Sahrens  *		(not supported yet)
4039fa9e4066Sahrens  *
4040fa9e4066Sahrens  *	-t	Only take the device off-line temporarily.  The offline
4041fa9e4066Sahrens  *		state will not be persistent across reboots.
4042fa9e4066Sahrens  */
4043fa9e4066Sahrens /* ARGSUSED */
4044fa9e4066Sahrens int
4045fa9e4066Sahrens zpool_do_offline(int argc, char **argv)
4046fa9e4066Sahrens {
4047fa9e4066Sahrens 	int c, i;
4048fa9e4066Sahrens 	char *poolname;
4049fa9e4066Sahrens 	zpool_handle_t *zhp;
405099653d4eSeschrock 	int ret = 0;
405199653d4eSeschrock 	boolean_t istmp = B_FALSE;
4052fa9e4066Sahrens 
4053fa9e4066Sahrens 	/* check options */
4054fa9e4066Sahrens 	while ((c = getopt(argc, argv, "ft")) != -1) {
4055fa9e4066Sahrens 		switch (c) {
4056fa9e4066Sahrens 		case 't':
405799653d4eSeschrock 			istmp = B_TRUE;
4058441d80aaSlling 			break;
4059441d80aaSlling 		case 'f':
4060fa9e4066Sahrens 		case '?':
4061fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4062fa9e4066Sahrens 			    optopt);
406399653d4eSeschrock 			usage(B_FALSE);
4064fa9e4066Sahrens 		}
4065fa9e4066Sahrens 	}
4066fa9e4066Sahrens 
4067fa9e4066Sahrens 	argc -= optind;
4068fa9e4066Sahrens 	argv += optind;
4069fa9e4066Sahrens 
4070fa9e4066Sahrens 	/* get pool name and check number of arguments */
4071fa9e4066Sahrens 	if (argc < 1) {
4072fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name\n"));
407399653d4eSeschrock 		usage(B_FALSE);
4074fa9e4066Sahrens 	}
4075fa9e4066Sahrens 	if (argc < 2) {
4076fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing device name\n"));
407799653d4eSeschrock 		usage(B_FALSE);
4078fa9e4066Sahrens 	}
4079fa9e4066Sahrens 
4080fa9e4066Sahrens 	poolname = argv[0];
4081fa9e4066Sahrens 
408299653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
4083fa9e4066Sahrens 		return (1);
4084fa9e4066Sahrens 
40853d7072f8Seschrock 	for (i = 1; i < argc; i++) {
40863d7072f8Seschrock 		if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
4087fa9e4066Sahrens 			ret = 1;
40883d7072f8Seschrock 	}
4089fa9e4066Sahrens 
409099653d4eSeschrock 	zpool_close(zhp);
409199653d4eSeschrock 
4092fa9e4066Sahrens 	return (ret);
4093fa9e4066Sahrens }
4094fa9e4066Sahrens 
4095ea8dc4b6Seschrock /*
4096ea8dc4b6Seschrock  * zpool clear <pool> [device]
4097ea8dc4b6Seschrock  *
4098ea8dc4b6Seschrock  * Clear all errors associated with a pool or a particular device.
4099ea8dc4b6Seschrock  */
4100ea8dc4b6Seschrock int
4101ea8dc4b6Seschrock zpool_do_clear(int argc, char **argv)
4102ea8dc4b6Seschrock {
4103468c413aSTim Haley 	int c;
4104ea8dc4b6Seschrock 	int ret = 0;
4105468c413aSTim Haley 	boolean_t dryrun = B_FALSE;
4106468c413aSTim Haley 	boolean_t do_rewind = B_FALSE;
4107468c413aSTim Haley 	boolean_t xtreme_rewind = B_FALSE;
4108468c413aSTim Haley 	uint32_t rewind_policy = ZPOOL_NO_REWIND;
4109468c413aSTim Haley 	nvlist_t *policy = NULL;
4110ea8dc4b6Seschrock 	zpool_handle_t *zhp;
4111ea8dc4b6Seschrock 	char *pool, *device;
4112ea8dc4b6Seschrock 
4113468c413aSTim Haley 	/* check options */
4114468c413aSTim Haley 	while ((c = getopt(argc, argv, "FnX")) != -1) {
4115468c413aSTim Haley 		switch (c) {
4116468c413aSTim Haley 		case 'F':
4117468c413aSTim Haley 			do_rewind = B_TRUE;
4118468c413aSTim Haley 			break;
4119468c413aSTim Haley 		case 'n':
4120468c413aSTim Haley 			dryrun = B_TRUE;
4121468c413aSTim Haley 			break;
4122468c413aSTim Haley 		case 'X':
4123468c413aSTim Haley 			xtreme_rewind = B_TRUE;
4124468c413aSTim Haley 			break;
4125468c413aSTim Haley 		case '?':
4126468c413aSTim Haley 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4127468c413aSTim Haley 			    optopt);
4128468c413aSTim Haley 			usage(B_FALSE);
4129468c413aSTim Haley 		}
4130468c413aSTim Haley 	}
4131468c413aSTim Haley 
4132468c413aSTim Haley 	argc -= optind;
4133468c413aSTim Haley 	argv += optind;
4134468c413aSTim Haley 
4135468c413aSTim Haley 	if (argc < 1) {
4136ea8dc4b6Seschrock 		(void) fprintf(stderr, gettext("missing pool name\n"));
413799653d4eSeschrock 		usage(B_FALSE);
4138ea8dc4b6Seschrock 	}
4139ea8dc4b6Seschrock 
4140468c413aSTim Haley 	if (argc > 2) {
4141ea8dc4b6Seschrock 		(void) fprintf(stderr, gettext("too many arguments\n"));
414299653d4eSeschrock 		usage(B_FALSE);
4143ea8dc4b6Seschrock 	}
4144ea8dc4b6Seschrock 
4145468c413aSTim Haley 	if ((dryrun || xtreme_rewind) && !do_rewind) {
4146468c413aSTim Haley 		(void) fprintf(stderr,
4147468c413aSTim Haley 		    gettext("-n or -X only meaningful with -F\n"));
4148468c413aSTim Haley 		usage(B_FALSE);
4149468c413aSTim Haley 	}
4150468c413aSTim Haley 	if (dryrun)
4151468c413aSTim Haley 		rewind_policy = ZPOOL_TRY_REWIND;
4152468c413aSTim Haley 	else if (do_rewind)
4153468c413aSTim Haley 		rewind_policy = ZPOOL_DO_REWIND;
4154468c413aSTim Haley 	if (xtreme_rewind)
4155468c413aSTim Haley 		rewind_policy |= ZPOOL_EXTREME_REWIND;
4156468c413aSTim Haley 
4157468c413aSTim Haley 	/* In future, further rewind policy choices can be passed along here */
4158468c413aSTim Haley 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
41595dafeea3SPavel Zakharov 	    nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY,
41605dafeea3SPavel Zakharov 	    rewind_policy) != 0) {
4161468c413aSTim Haley 		return (1);
41625dafeea3SPavel Zakharov 	}
4163468c413aSTim Haley 
4164468c413aSTim Haley 	pool = argv[0];
4165468c413aSTim Haley 	device = argc == 2 ? argv[1] : NULL;
4166ea8dc4b6Seschrock 
4167468c413aSTim Haley 	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
4168468c413aSTim Haley 		nvlist_free(policy);
4169ea8dc4b6Seschrock 		return (1);
4170468c413aSTim Haley 	}
4171ea8dc4b6Seschrock 
4172468c413aSTim Haley 	if (zpool_clear(zhp, device, policy) != 0)
4173ea8dc4b6Seschrock 		ret = 1;
4174ea8dc4b6Seschrock 
4175ea8dc4b6Seschrock 	zpool_close(zhp);
4176ea8dc4b6Seschrock 
4177468c413aSTim Haley 	nvlist_free(policy);
4178468c413aSTim Haley 
4179ea8dc4b6Seschrock 	return (ret);
4180ea8dc4b6Seschrock }
4181ea8dc4b6Seschrock 
4182e9103aaeSGarrett D'Amore /*
4183e9103aaeSGarrett D'Amore  * zpool reguid <pool>
4184e9103aaeSGarrett D'Amore  */
4185e9103aaeSGarrett D'Amore int
4186e9103aaeSGarrett D'Amore zpool_do_reguid(int argc, char **argv)
4187e9103aaeSGarrett D'Amore {
4188e9103aaeSGarrett D'Amore 	int c;
4189e9103aaeSGarrett D'Amore 	char *poolname;
4190e9103aaeSGarrett D'Amore 	zpool_handle_t *zhp;
4191e9103aaeSGarrett D'Amore 	int ret = 0;
4192e9103aaeSGarrett D'Amore 
4193e9103aaeSGarrett D'Amore 	/* check options */
4194e9103aaeSGarrett D'Amore 	while ((c = getopt(argc, argv, "")) != -1) {
4195e9103aaeSGarrett D'Amore 		switch (c) {
4196e9103aaeSGarrett D'Amore 		case '?':
4197e9103aaeSGarrett D'Amore 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4198e9103aaeSGarrett D'Amore 			    optopt);
4199e9103aaeSGarrett D'Amore 			usage(B_FALSE);
4200e9103aaeSGarrett D'Amore 		}
4201e9103aaeSGarrett D'Amore 	}
4202e9103aaeSGarrett D'Amore 
4203e9103aaeSGarrett D'Amore 	argc -= optind;
4204e9103aaeSGarrett D'Amore 	argv += optind;
4205e9103aaeSGarrett D'Amore 
4206e9103aaeSGarrett D'Amore 	/* get pool name and check number of arguments */
4207e9103aaeSGarrett D'Amore 	if (argc < 1) {
4208e9103aaeSGarrett D'Amore 		(void) fprintf(stderr, gettext("missing pool name\n"));
4209e9103aaeSGarrett D'Amore 		usage(B_FALSE);
4210e9103aaeSGarrett D'Amore 	}
4211e9103aaeSGarrett D'Amore 
4212e9103aaeSGarrett D'Amore 	if (argc > 1) {
4213e9103aaeSGarrett D'Amore 		(void) fprintf(stderr, gettext("too many arguments\n"));
4214e9103aaeSGarrett D'Amore 		usage(B_FALSE);
4215e9103aaeSGarrett D'Amore 	}
4216e9103aaeSGarrett D'Amore 
4217e9103aaeSGarrett D'Amore 	poolname = argv[0];
4218e9103aaeSGarrett D'Amore 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
4219e9103aaeSGarrett D'Amore 		return (1);
4220e9103aaeSGarrett D'Amore 
4221e9103aaeSGarrett D'Amore 	ret = zpool_reguid(zhp);
4222e9103aaeSGarrett D'Amore 
4223e9103aaeSGarrett D'Amore 	zpool_close(zhp);
4224e9103aaeSGarrett D'Amore 	return (ret);
4225e9103aaeSGarrett D'Amore }
4226e9103aaeSGarrett D'Amore 
4227e9103aaeSGarrett D'Amore 
42284263d13fSGeorge Wilson /*
42294263d13fSGeorge Wilson  * zpool reopen <pool>
42304263d13fSGeorge Wilson  *
42314263d13fSGeorge Wilson  * Reopen the pool so that the kernel can update the sizes of all vdevs.
42324263d13fSGeorge Wilson  */
42334263d13fSGeorge Wilson int
42344263d13fSGeorge Wilson zpool_do_reopen(int argc, char **argv)
42354263d13fSGeorge Wilson {
423631d7e8faSGeorge Wilson 	int c;
42374263d13fSGeorge Wilson 	int ret = 0;
42384263d13fSGeorge Wilson 	zpool_handle_t *zhp;
42394263d13fSGeorge Wilson 	char *pool;
42404263d13fSGeorge Wilson 
424131d7e8faSGeorge Wilson 	/* check options */
424231d7e8faSGeorge Wilson 	while ((c = getopt(argc, argv, "")) != -1) {
424331d7e8faSGeorge Wilson 		switch (c) {
424431d7e8faSGeorge Wilson 		case '?':
424531d7e8faSGeorge Wilson 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
424631d7e8faSGeorge Wilson 			    optopt);
424731d7e8faSGeorge Wilson 			usage(B_FALSE);
424831d7e8faSGeorge Wilson 		}
424931d7e8faSGeorge Wilson 	}
425031d7e8faSGeorge Wilson 
42514263d13fSGeorge Wilson 	argc--;
42524263d13fSGeorge Wilson 	argv++;
42534263d13fSGeorge Wilson 
425431d7e8faSGeorge Wilson 	if (argc < 1) {
425531d7e8faSGeorge Wilson 		(void) fprintf(stderr, gettext("missing pool name\n"));
425631d7e8faSGeorge Wilson 		usage(B_FALSE);
425731d7e8faSGeorge Wilson 	}
425831d7e8faSGeorge Wilson 
425931d7e8faSGeorge Wilson 	if (argc > 1) {
426031d7e8faSGeorge Wilson 		(void) fprintf(stderr, gettext("too many arguments\n"));
426131d7e8faSGeorge Wilson 		usage(B_FALSE);
426231d7e8faSGeorge Wilson 	}
42634263d13fSGeorge Wilson 
42644263d13fSGeorge Wilson 	pool = argv[0];
42654263d13fSGeorge Wilson 	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
42664263d13fSGeorge Wilson 		return (1);
42674263d13fSGeorge Wilson 
42684263d13fSGeorge Wilson 	ret = zpool_reopen(zhp);
42694263d13fSGeorge Wilson 	zpool_close(zhp);
42704263d13fSGeorge Wilson 	return (ret);
42714263d13fSGeorge Wilson }
42724263d13fSGeorge Wilson 
4273fa9e4066Sahrens typedef struct scrub_cbdata {
4274fa9e4066Sahrens 	int	cb_type;
427506eeb2adSek 	int	cb_argc;
427606eeb2adSek 	char	**cb_argv;
42771702cce7SAlek Pinchuk 	pool_scrub_cmd_t cb_scrub_cmd;
4278fa9e4066Sahrens } scrub_cbdata_t;
4279fa9e4066Sahrens 
428086714001SSerapheim Dimitropoulos static boolean_t
428186714001SSerapheim Dimitropoulos zpool_has_checkpoint(zpool_handle_t *zhp)
428286714001SSerapheim Dimitropoulos {
428386714001SSerapheim Dimitropoulos 	nvlist_t *config, *nvroot;
428486714001SSerapheim Dimitropoulos 
428586714001SSerapheim Dimitropoulos 	config = zpool_get_config(zhp, NULL);
428686714001SSerapheim Dimitropoulos 
428786714001SSerapheim Dimitropoulos 	if (config != NULL) {
428886714001SSerapheim Dimitropoulos 		pool_checkpoint_stat_t *pcs = NULL;
428986714001SSerapheim Dimitropoulos 		uint_t c;
429086714001SSerapheim Dimitropoulos 
429186714001SSerapheim Dimitropoulos 		nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
429286714001SSerapheim Dimitropoulos 		(void) nvlist_lookup_uint64_array(nvroot,
429386714001SSerapheim Dimitropoulos 		    ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
429486714001SSerapheim Dimitropoulos 
429586714001SSerapheim Dimitropoulos 		if (pcs == NULL || pcs->pcs_state == CS_NONE)
429686714001SSerapheim Dimitropoulos 			return (B_FALSE);
429786714001SSerapheim Dimitropoulos 
429886714001SSerapheim Dimitropoulos 		assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS ||
429986714001SSerapheim Dimitropoulos 		    pcs->pcs_state == CS_CHECKPOINT_DISCARDING);
430086714001SSerapheim Dimitropoulos 		return (B_TRUE);
430186714001SSerapheim Dimitropoulos 	}
430286714001SSerapheim Dimitropoulos 
430386714001SSerapheim Dimitropoulos 	return (B_FALSE);
430486714001SSerapheim Dimitropoulos }
430586714001SSerapheim Dimitropoulos 
4306fa9e4066Sahrens int
4307fa9e4066Sahrens scrub_callback(zpool_handle_t *zhp, void *data)
4308fa9e4066Sahrens {
4309fa9e4066Sahrens 	scrub_cbdata_t *cb = data;
431006eeb2adSek 	int err;
4311fa9e4066Sahrens 
4312ea8dc4b6Seschrock 	/*
4313ea8dc4b6Seschrock 	 * Ignore faulted pools.
4314ea8dc4b6Seschrock 	 */
4315ea8dc4b6Seschrock 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
4316ea8dc4b6Seschrock 		(void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
4317ea8dc4b6Seschrock 		    "currently unavailable\n"), zpool_get_name(zhp));
4318ea8dc4b6Seschrock 		return (1);
4319ea8dc4b6Seschrock 	}
4320ea8dc4b6Seschrock 
43211702cce7SAlek Pinchuk 	err = zpool_scan(zhp, cb->cb_type, cb->cb_scrub_cmd);
432206eeb2adSek 
432386714001SSerapheim Dimitropoulos 	if (err == 0 && zpool_has_checkpoint(zhp) &&
432486714001SSerapheim Dimitropoulos 	    cb->cb_type == POOL_SCAN_SCRUB) {
432586714001SSerapheim Dimitropoulos 		(void) printf(gettext("warning: will not scrub state that "
432686714001SSerapheim Dimitropoulos 		    "belongs to the checkpoint of pool '%s'\n"),
432786714001SSerapheim Dimitropoulos 		    zpool_get_name(zhp));
432886714001SSerapheim Dimitropoulos 	}
432986714001SSerapheim Dimitropoulos 
433006eeb2adSek 	return (err != 0);
4331fa9e4066Sahrens }
4332fa9e4066Sahrens 
4333fa9e4066Sahrens /*
43341702cce7SAlek Pinchuk  * zpool scrub [-s | -p] <pool> ...
4335fa9e4066Sahrens  *
4336fa9e4066Sahrens  *	-s	Stop.  Stops any in-progress scrub.
43371702cce7SAlek Pinchuk  *	-p	Pause. Pause in-progress scrub.
4338fa9e4066Sahrens  */
4339fa9e4066Sahrens int
4340fa9e4066Sahrens zpool_do_scrub(int argc, char **argv)
4341fa9e4066Sahrens {
4342fa9e4066Sahrens 	int c;
4343fa9e4066Sahrens 	scrub_cbdata_t cb;
4344fa9e4066Sahrens 
43453f9d6ad7SLin Ling 	cb.cb_type = POOL_SCAN_SCRUB;
43461702cce7SAlek Pinchuk 	cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
4347fa9e4066Sahrens 
4348fa9e4066Sahrens 	/* check options */
43491702cce7SAlek Pinchuk 	while ((c = getopt(argc, argv, "sp")) != -1) {
4350fa9e4066Sahrens 		switch (c) {
4351fa9e4066Sahrens 		case 's':
43523f9d6ad7SLin Ling 			cb.cb_type = POOL_SCAN_NONE;
4353fa9e4066Sahrens 			break;
43541702cce7SAlek Pinchuk 		case 'p':
43551702cce7SAlek Pinchuk 			cb.cb_scrub_cmd = POOL_SCRUB_PAUSE;
43561702cce7SAlek Pinchuk 			break;
4357fa9e4066Sahrens 		case '?':
4358fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4359fa9e4066Sahrens 			    optopt);
436099653d4eSeschrock 			usage(B_FALSE);
4361fa9e4066Sahrens 		}
4362fa9e4066Sahrens 	}
4363fa9e4066Sahrens 
43641702cce7SAlek Pinchuk 	if (cb.cb_type == POOL_SCAN_NONE &&
43651702cce7SAlek Pinchuk 	    cb.cb_scrub_cmd == POOL_SCRUB_PAUSE) {
43661702cce7SAlek Pinchuk 		(void) fprintf(stderr, gettext("invalid option combination: "
43671702cce7SAlek Pinchuk 		    "-s and -p are mutually exclusive\n"));
43681702cce7SAlek Pinchuk 		usage(B_FALSE);
43691702cce7SAlek Pinchuk 	}
43701702cce7SAlek Pinchuk 
437106eeb2adSek 	cb.cb_argc = argc;
437206eeb2adSek 	cb.cb_argv = argv;
4373fa9e4066Sahrens 	argc -= optind;
4374fa9e4066Sahrens 	argv += optind;
4375fa9e4066Sahrens 
4376fa9e4066Sahrens 	if (argc < 1) {
4377fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
437899653d4eSeschrock 		usage(B_FALSE);
4379fa9e4066Sahrens 	}
4380fa9e4066Sahrens 
4381b1b8ab34Slling 	return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
4382fa9e4066Sahrens }
4383fa9e4066Sahrens 
4384094e47e9SGeorge Wilson static void
4385094e47e9SGeorge Wilson zpool_collect_leaves(zpool_handle_t *zhp, nvlist_t *nvroot, nvlist_t *res)
4386094e47e9SGeorge Wilson {
4387094e47e9SGeorge Wilson 	uint_t children = 0;
4388094e47e9SGeorge Wilson 	nvlist_t **child;
4389094e47e9SGeorge Wilson 	uint_t i;
4390094e47e9SGeorge Wilson 
4391094e47e9SGeorge Wilson 	(void) nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4392094e47e9SGeorge Wilson 	    &child, &children);
4393094e47e9SGeorge Wilson 
4394094e47e9SGeorge Wilson 	if (children == 0) {
4395094e47e9SGeorge Wilson 		char *path = zpool_vdev_name(g_zfs, zhp, nvroot, B_FALSE);
4396094e47e9SGeorge Wilson 		fnvlist_add_boolean(res, path);
4397094e47e9SGeorge Wilson 		free(path);
4398094e47e9SGeorge Wilson 		return;
4399094e47e9SGeorge Wilson 	}
4400094e47e9SGeorge Wilson 
4401094e47e9SGeorge Wilson 	for (i = 0; i < children; i++) {
4402094e47e9SGeorge Wilson 		zpool_collect_leaves(zhp, child[i], res);
4403094e47e9SGeorge Wilson 	}
4404094e47e9SGeorge Wilson }
4405094e47e9SGeorge Wilson 
4406094e47e9SGeorge Wilson /*
4407094e47e9SGeorge Wilson  * zpool initialize [-cs] <pool> [<vdev> ...]
4408094e47e9SGeorge Wilson  * Initialize all unused blocks in the specified vdevs, or all vdevs in the pool
4409094e47e9SGeorge Wilson  * if none specified.
4410094e47e9SGeorge Wilson  *
4411094e47e9SGeorge Wilson  *	-c	Cancel. Ends active initializing.
4412094e47e9SGeorge Wilson  *	-s	Suspend. Initializing can then be restarted with no flags.
4413094e47e9SGeorge Wilson  */
4414094e47e9SGeorge Wilson int
4415094e47e9SGeorge Wilson zpool_do_initialize(int argc, char **argv)
4416094e47e9SGeorge Wilson {
4417094e47e9SGeorge Wilson 	int c;
4418094e47e9SGeorge Wilson 	char *poolname;
4419094e47e9SGeorge Wilson 	zpool_handle_t *zhp;
4420094e47e9SGeorge Wilson 	nvlist_t *vdevs;
4421094e47e9SGeorge Wilson 	int err = 0;
4422094e47e9SGeorge Wilson 
4423094e47e9SGeorge Wilson 	struct option long_options[] = {
4424094e47e9SGeorge Wilson 		{"cancel",	no_argument,		NULL, 'c'},
4425094e47e9SGeorge Wilson 		{"suspend",	no_argument,		NULL, 's'},
4426094e47e9SGeorge Wilson 		{0, 0, 0, 0}
4427094e47e9SGeorge Wilson 	};
4428094e47e9SGeorge Wilson 
4429094e47e9SGeorge Wilson 	pool_initialize_func_t cmd_type = POOL_INITIALIZE_DO;
4430094e47e9SGeorge Wilson 	while ((c = getopt_long(argc, argv, "cs", long_options, NULL)) != -1) {
4431094e47e9SGeorge Wilson 		switch (c) {
4432094e47e9SGeorge Wilson 		case 'c':
4433094e47e9SGeorge Wilson 			if (cmd_type != POOL_INITIALIZE_DO) {
4434094e47e9SGeorge Wilson 				(void) fprintf(stderr, gettext("-c cannot be "
4435094e47e9SGeorge Wilson 				    "combined with other options\n"));
4436094e47e9SGeorge Wilson 				usage(B_FALSE);
4437094e47e9SGeorge Wilson 			}
4438094e47e9SGeorge Wilson 			cmd_type = POOL_INITIALIZE_CANCEL;
4439094e47e9SGeorge Wilson 			break;
4440094e47e9SGeorge Wilson 		case 's':
4441094e47e9SGeorge Wilson 			if (cmd_type != POOL_INITIALIZE_DO) {
4442094e47e9SGeorge Wilson 				(void) fprintf(stderr, gettext("-s cannot be "
4443094e47e9SGeorge Wilson 				    "combined with other options\n"));
4444094e47e9SGeorge Wilson 				usage(B_FALSE);
4445094e47e9SGeorge Wilson 			}
4446094e47e9SGeorge Wilson 			cmd_type = POOL_INITIALIZE_SUSPEND;
4447094e47e9SGeorge Wilson 			break;
4448094e47e9SGeorge Wilson 		case '?':
4449094e47e9SGeorge Wilson 			if (optopt != 0) {
4450094e47e9SGeorge Wilson 				(void) fprintf(stderr,
4451094e47e9SGeorge Wilson 				    gettext("invalid option '%c'\n"), optopt);
4452094e47e9SGeorge Wilson 			} else {
4453094e47e9SGeorge Wilson 				(void) fprintf(stderr,
4454094e47e9SGeorge Wilson 				    gettext("invalid option '%s'\n"),
4455094e47e9SGeorge Wilson 				    argv[optind - 1]);
4456094e47e9SGeorge Wilson 			}
4457094e47e9SGeorge Wilson 			usage(B_FALSE);
4458094e47e9SGeorge Wilson 		}
4459094e47e9SGeorge Wilson 	}
4460094e47e9SGeorge Wilson 
4461094e47e9SGeorge Wilson 	argc -= optind;
4462094e47e9SGeorge Wilson 	argv += optind;
4463094e47e9SGeorge Wilson 
4464094e47e9SGeorge Wilson 	if (argc < 1) {
4465094e47e9SGeorge Wilson 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
4466094e47e9SGeorge Wilson 		usage(B_FALSE);
4467094e47e9SGeorge Wilson 		return (-1);
4468094e47e9SGeorge Wilson 	}
4469094e47e9SGeorge Wilson 
4470094e47e9SGeorge Wilson 	poolname = argv[0];
4471094e47e9SGeorge Wilson 	zhp = zpool_open(g_zfs, poolname);
4472094e47e9SGeorge Wilson 	if (zhp == NULL)
4473094e47e9SGeorge Wilson 		return (-1);
4474094e47e9SGeorge Wilson 
4475094e47e9SGeorge Wilson 	vdevs = fnvlist_alloc();
4476094e47e9SGeorge Wilson 	if (argc == 1) {
4477094e47e9SGeorge Wilson 		/* no individual leaf vdevs specified, so add them all */
4478094e47e9SGeorge Wilson 		nvlist_t *config = zpool_get_config(zhp, NULL);
4479094e47e9SGeorge Wilson 		nvlist_t *nvroot = fnvlist_lookup_nvlist(config,
4480094e47e9SGeorge Wilson 		    ZPOOL_CONFIG_VDEV_TREE);
4481094e47e9SGeorge Wilson 		zpool_collect_leaves(zhp, nvroot, vdevs);
4482094e47e9SGeorge Wilson 	} else {
4483094e47e9SGeorge Wilson 		int i;
4484094e47e9SGeorge Wilson 		for (i = 1; i < argc; i++) {
4485094e47e9SGeorge Wilson 			fnvlist_add_boolean(vdevs, argv[i]);
4486094e47e9SGeorge Wilson 		}
4487094e47e9SGeorge Wilson 	}
4488094e47e9SGeorge Wilson 
4489094e47e9SGeorge Wilson 	err = zpool_initialize(zhp, cmd_type, vdevs);
4490094e47e9SGeorge Wilson 
4491094e47e9SGeorge Wilson 	fnvlist_free(vdevs);
4492094e47e9SGeorge Wilson 	zpool_close(zhp);
4493094e47e9SGeorge Wilson 
4494094e47e9SGeorge Wilson 	return (err);
4495094e47e9SGeorge Wilson }
4496094e47e9SGeorge Wilson 
4497fa9e4066Sahrens typedef struct status_cbdata {
449899653d4eSeschrock 	int		cb_count;
4499e9dbad6fSeschrock 	boolean_t	cb_allpools;
450099653d4eSeschrock 	boolean_t	cb_verbose;
450199653d4eSeschrock 	boolean_t	cb_explain;
450299653d4eSeschrock 	boolean_t	cb_first;
45039eb19f4dSGeorge Wilson 	boolean_t	cb_dedup_stats;
4504fa9e4066Sahrens } status_cbdata_t;
4505fa9e4066Sahrens 
4506fa9e4066Sahrens /*
4507fa9e4066Sahrens  * Print out detailed scrub status.
4508fa9e4066Sahrens  */
45095cabbc6bSPrashanth Sreenivasa static void
45103f9d6ad7SLin Ling print_scan_status(pool_scan_stat_t *ps)
4511fa9e4066Sahrens {
45121702cce7SAlek Pinchuk 	time_t start, end, pause;
451373d314ceSLin Ling 	uint64_t elapsed, mins_left, hours_left;
45143f9d6ad7SLin Ling 	uint64_t pass_exam, examined, total;
45153f9d6ad7SLin Ling 	uint_t rate;
4516fa9e4066Sahrens 	double fraction_done;
45173f9d6ad7SLin Ling 	char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
4518fa9e4066Sahrens 
4519ce72e614SYuri Pankov 	(void) printf(gettext("  scan: "));
4520fa9e4066Sahrens 
45213f9d6ad7SLin Ling 	/* If there's never been a scan, there's not much to say. */
45223f9d6ad7SLin Ling 	if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
45233f9d6ad7SLin Ling 	    ps->pss_func >= POOL_SCAN_FUNCS) {
4524fa9e4066Sahrens 		(void) printf(gettext("none requested\n"));
4525fa9e4066Sahrens 		return;
4526fa9e4066Sahrens 	}
4527fa9e4066Sahrens 
45283f9d6ad7SLin Ling 	start = ps->pss_start_time;
45293f9d6ad7SLin Ling 	end = ps->pss_end_time;
45301702cce7SAlek Pinchuk 	pause = ps->pss_pass_scrub_pause;
45313f9d6ad7SLin Ling 	zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
4532fa9e4066Sahrens 
45333f9d6ad7SLin Ling 	assert(ps->pss_func == POOL_SCAN_SCRUB ||
45343f9d6ad7SLin Ling 	    ps->pss_func == POOL_SCAN_RESILVER);
45353f9d6ad7SLin Ling 	/*
45363f9d6ad7SLin Ling 	 * Scan is finished or canceled.
45373f9d6ad7SLin Ling 	 */
45383f9d6ad7SLin Ling 	if (ps->pss_state == DSS_FINISHED) {
45393f9d6ad7SLin Ling 		uint64_t minutes_taken = (end - start) / 60;
4540b327cd3fSIgor Kozhukhov 		char *fmt = NULL;
45413f9d6ad7SLin Ling 
45423f9d6ad7SLin Ling 		if (ps->pss_func == POOL_SCAN_SCRUB) {
45433f9d6ad7SLin Ling 			fmt = gettext("scrub repaired %s in %lluh%um with "
45443f9d6ad7SLin Ling 			    "%llu errors on %s");
45453f9d6ad7SLin Ling 		} else if (ps->pss_func == POOL_SCAN_RESILVER) {
45463f9d6ad7SLin Ling 			fmt = gettext("resilvered %s in %lluh%um with "
45473f9d6ad7SLin Ling 			    "%llu errors on %s");
45483f9d6ad7SLin Ling 		}
45493f9d6ad7SLin Ling 		/* LINTED */
45503f9d6ad7SLin Ling 		(void) printf(fmt, processed_buf,
455118ce54dfSek 		    (u_longlong_t)(minutes_taken / 60),
455218ce54dfSek 		    (uint_t)(minutes_taken % 60),
45533f9d6ad7SLin Ling 		    (u_longlong_t)ps->pss_errors,
45543f9d6ad7SLin Ling 		    ctime((time_t *)&end));
45553f9d6ad7SLin Ling 		return;
45563f9d6ad7SLin Ling 	} else if (ps->pss_state == DSS_CANCELED) {
45573f9d6ad7SLin Ling 		if (ps->pss_func == POOL_SCAN_SCRUB) {
45583f9d6ad7SLin Ling 			(void) printf(gettext("scrub canceled on %s"),
45593f9d6ad7SLin Ling 			    ctime(&end));
45603f9d6ad7SLin Ling 		} else if (ps->pss_func == POOL_SCAN_RESILVER) {
45613f9d6ad7SLin Ling 			(void) printf(gettext("resilver canceled on %s"),
45623f9d6ad7SLin Ling 			    ctime(&end));
45633f9d6ad7SLin Ling 		}
4564fa9e4066Sahrens 		return;
4565fa9e4066Sahrens 	}
4566fa9e4066Sahrens 
45673f9d6ad7SLin Ling 	assert(ps->pss_state == DSS_SCANNING);
45683f9d6ad7SLin Ling 
45693f9d6ad7SLin Ling 	/*
45703f9d6ad7SLin Ling 	 * Scan is in progress.
45713f9d6ad7SLin Ling 	 */
45723f9d6ad7SLin Ling 	if (ps->pss_func == POOL_SCAN_SCRUB) {
45731702cce7SAlek Pinchuk 		if (pause == 0) {
45741702cce7SAlek Pinchuk 			(void) printf(gettext("scrub in progress since %s"),
45751702cce7SAlek Pinchuk 			    ctime(&start));
45761702cce7SAlek Pinchuk 		} else {
45771702cce7SAlek Pinchuk 			char buf[32];
45781702cce7SAlek Pinchuk 			struct tm *p = localtime(&pause);
45791702cce7SAlek Pinchuk 			(void) strftime(buf, sizeof (buf), "%a %b %e %T %Y", p);
45801702cce7SAlek Pinchuk 			(void) printf(gettext("scrub paused since %s\n"), buf);
45811702cce7SAlek Pinchuk 			(void) printf(gettext("\tscrub started on   %s"),
45821702cce7SAlek Pinchuk 			    ctime(&start));
45831702cce7SAlek Pinchuk 		}
45843f9d6ad7SLin Ling 	} else if (ps->pss_func == POOL_SCAN_RESILVER) {
45853f9d6ad7SLin Ling 		(void) printf(gettext("resilver in progress since %s"),
45863f9d6ad7SLin Ling 		    ctime(&start));
45873f9d6ad7SLin Ling 	}
4588fa9e4066Sahrens 
45893f9d6ad7SLin Ling 	examined = ps->pss_examined ? ps->pss_examined : 1;
45903f9d6ad7SLin Ling 	total = ps->pss_to_examine;
4591fa9e4066Sahrens 	fraction_done = (double)examined / total;
45923f9d6ad7SLin Ling 
45933f9d6ad7SLin Ling 	/* elapsed time for this pass */
45943f9d6ad7SLin Ling 	elapsed = time(NULL) - ps->pss_pass_start;
45951702cce7SAlek Pinchuk 	elapsed -= ps->pss_pass_scrub_spent_paused;
45963f9d6ad7SLin Ling 	elapsed = elapsed ? elapsed : 1;
45973f9d6ad7SLin Ling 	pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
45983f9d6ad7SLin Ling 	rate = pass_exam / elapsed;
45993f9d6ad7SLin Ling 	rate = rate ? rate : 1;
46003f9d6ad7SLin Ling 	mins_left = ((total - examined) / rate) / 60;
460173d314ceSLin Ling 	hours_left = mins_left / 60;
46023f9d6ad7SLin Ling 
46033f9d6ad7SLin Ling 	zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
46043f9d6ad7SLin Ling 	zfs_nicenum(total, total_buf, sizeof (total_buf));
46053f9d6ad7SLin Ling 
460673d314ceSLin Ling 	/*
460773d314ceSLin Ling 	 * do not print estimated time if hours_left is more than 30 days
46081702cce7SAlek Pinchuk 	 * or we have a paused scrub
460973d314ceSLin Ling 	 */
46101702cce7SAlek Pinchuk 	if (pause == 0) {
46111702cce7SAlek Pinchuk 		zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
46121702cce7SAlek Pinchuk 		(void) printf(gettext("\t%s scanned out of %s at %s/s"),
46131702cce7SAlek Pinchuk 		    examined_buf, total_buf, rate_buf);
46141702cce7SAlek Pinchuk 		if (hours_left < (30 * 24)) {
46151702cce7SAlek Pinchuk 			(void) printf(gettext(", %lluh%um to go\n"),
46161702cce7SAlek Pinchuk 			    (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
46171702cce7SAlek Pinchuk 		} else {
46181702cce7SAlek Pinchuk 			(void) printf(gettext(
46191702cce7SAlek Pinchuk 			    ", (scan is slow, no estimated time)\n"));
46201702cce7SAlek Pinchuk 		}
462173d314ceSLin Ling 	} else {
46221702cce7SAlek Pinchuk 		(void) printf(gettext("\t%s scanned out of %s\n"),
46231702cce7SAlek Pinchuk 		    examined_buf, total_buf);
462473d314ceSLin Ling 	}
46253f9d6ad7SLin Ling 
46263f9d6ad7SLin Ling 	if (ps->pss_func == POOL_SCAN_RESILVER) {
46273f9d6ad7SLin Ling 		(void) printf(gettext("    %s resilvered, %.2f%% done\n"),
46283f9d6ad7SLin Ling 		    processed_buf, 100 * fraction_done);
46293f9d6ad7SLin Ling 	} else if (ps->pss_func == POOL_SCAN_SCRUB) {
46303f9d6ad7SLin Ling 		(void) printf(gettext("    %s repaired, %.2f%% done\n"),
46313f9d6ad7SLin Ling 		    processed_buf, 100 * fraction_done);
46323f9d6ad7SLin Ling 	}
4633fa9e4066Sahrens }
4634fa9e4066Sahrens 
463586714001SSerapheim Dimitropoulos /*
463686714001SSerapheim Dimitropoulos  * As we don't scrub checkpointed blocks, we want to warn the
463786714001SSerapheim Dimitropoulos  * user that we skipped scanning some blocks if a checkpoint exists
463886714001SSerapheim Dimitropoulos  * or existed at any time during the scan.
463986714001SSerapheim Dimitropoulos  */
464086714001SSerapheim Dimitropoulos static void
464186714001SSerapheim Dimitropoulos print_checkpoint_scan_warning(pool_scan_stat_t *ps, pool_checkpoint_stat_t *pcs)
464286714001SSerapheim Dimitropoulos {
464386714001SSerapheim Dimitropoulos 	if (ps == NULL || pcs == NULL)
464486714001SSerapheim Dimitropoulos 		return;
464586714001SSerapheim Dimitropoulos 
464686714001SSerapheim Dimitropoulos 	if (pcs->pcs_state == CS_NONE ||
464786714001SSerapheim Dimitropoulos 	    pcs->pcs_state == CS_CHECKPOINT_DISCARDING)
464886714001SSerapheim Dimitropoulos 		return;
464986714001SSerapheim Dimitropoulos 
465086714001SSerapheim Dimitropoulos 	assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS);
465186714001SSerapheim Dimitropoulos 
465286714001SSerapheim Dimitropoulos 	if (ps->pss_state == DSS_NONE)
465386714001SSerapheim Dimitropoulos 		return;
465486714001SSerapheim Dimitropoulos 
465586714001SSerapheim Dimitropoulos 	if ((ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) &&
465686714001SSerapheim Dimitropoulos 	    ps->pss_end_time < pcs->pcs_start_time)
465786714001SSerapheim Dimitropoulos 		return;
465886714001SSerapheim Dimitropoulos 
465986714001SSerapheim Dimitropoulos 	if (ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) {
466086714001SSerapheim Dimitropoulos 		(void) printf(gettext("    scan warning: skipped blocks "
466186714001SSerapheim Dimitropoulos 		    "that are only referenced by the checkpoint.\n"));
466286714001SSerapheim Dimitropoulos 	} else {
466386714001SSerapheim Dimitropoulos 		assert(ps->pss_state == DSS_SCANNING);
466486714001SSerapheim Dimitropoulos 		(void) printf(gettext("    scan warning: skipping blocks "
466586714001SSerapheim Dimitropoulos 		    "that are only referenced by the checkpoint.\n"));
466686714001SSerapheim Dimitropoulos 	}
466786714001SSerapheim Dimitropoulos }
466886714001SSerapheim Dimitropoulos 
46695cabbc6bSPrashanth Sreenivasa /*
46705cabbc6bSPrashanth Sreenivasa  * Print out detailed removal status.
46715cabbc6bSPrashanth Sreenivasa  */
46725cabbc6bSPrashanth Sreenivasa static void
46735cabbc6bSPrashanth Sreenivasa print_removal_status(zpool_handle_t *zhp, pool_removal_stat_t *prs)
46745cabbc6bSPrashanth Sreenivasa {
46755cabbc6bSPrashanth Sreenivasa 	char copied_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
46765cabbc6bSPrashanth Sreenivasa 	time_t start, end;
46775cabbc6bSPrashanth Sreenivasa 	nvlist_t *config, *nvroot;
46785cabbc6bSPrashanth Sreenivasa 	nvlist_t **child;
46795cabbc6bSPrashanth Sreenivasa 	uint_t children;
46805cabbc6bSPrashanth Sreenivasa 	char *vdev_name;
46815cabbc6bSPrashanth Sreenivasa 
46825cabbc6bSPrashanth Sreenivasa 	if (prs == NULL || prs->prs_state == DSS_NONE)
46835cabbc6bSPrashanth Sreenivasa 		return;
46845cabbc6bSPrashanth Sreenivasa 
46855cabbc6bSPrashanth Sreenivasa 	/*
46865cabbc6bSPrashanth Sreenivasa 	 * Determine name of vdev.
46875cabbc6bSPrashanth Sreenivasa 	 */
46885cabbc6bSPrashanth Sreenivasa 	config = zpool_get_config(zhp, NULL);
46895cabbc6bSPrashanth Sreenivasa 	nvroot = fnvlist_lookup_nvlist(config,
46905cabbc6bSPrashanth Sreenivasa 	    ZPOOL_CONFIG_VDEV_TREE);
46915cabbc6bSPrashanth Sreenivasa 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
46925cabbc6bSPrashanth Sreenivasa 	    &child, &children) == 0);
46935cabbc6bSPrashanth Sreenivasa 	assert(prs->prs_removing_vdev < children);
46945cabbc6bSPrashanth Sreenivasa 	vdev_name = zpool_vdev_name(g_zfs, zhp,
46955cabbc6bSPrashanth Sreenivasa 	    child[prs->prs_removing_vdev], B_TRUE);
46965cabbc6bSPrashanth Sreenivasa 
46975cabbc6bSPrashanth Sreenivasa 	(void) printf(gettext("remove: "));
46985cabbc6bSPrashanth Sreenivasa 
46995cabbc6bSPrashanth Sreenivasa 	start = prs->prs_start_time;
47005cabbc6bSPrashanth Sreenivasa 	end = prs->prs_end_time;
47015cabbc6bSPrashanth Sreenivasa 	zfs_nicenum(prs->prs_copied, copied_buf, sizeof (copied_buf));
47025cabbc6bSPrashanth Sreenivasa 
47035cabbc6bSPrashanth Sreenivasa 	/*
47045cabbc6bSPrashanth Sreenivasa 	 * Removal is finished or canceled.
47055cabbc6bSPrashanth Sreenivasa 	 */
47065cabbc6bSPrashanth Sreenivasa 	if (prs->prs_state == DSS_FINISHED) {
47075cabbc6bSPrashanth Sreenivasa 		uint64_t minutes_taken = (end - start) / 60;
47085cabbc6bSPrashanth Sreenivasa 
47095cabbc6bSPrashanth Sreenivasa 		(void) printf(gettext("Removal of vdev %llu copied %s "
47105cabbc6bSPrashanth Sreenivasa 		    "in %lluh%um, completed on %s"),
47115cabbc6bSPrashanth Sreenivasa 		    (longlong_t)prs->prs_removing_vdev,
47125cabbc6bSPrashanth Sreenivasa 		    copied_buf,
47135cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)(minutes_taken / 60),
47145cabbc6bSPrashanth Sreenivasa 		    (uint_t)(minutes_taken % 60),
47155cabbc6bSPrashanth Sreenivasa 		    ctime((time_t *)&end));
47165cabbc6bSPrashanth Sreenivasa 	} else if (prs->prs_state == DSS_CANCELED) {
47175cabbc6bSPrashanth Sreenivasa 		(void) printf(gettext("Removal of %s canceled on %s"),
47185cabbc6bSPrashanth Sreenivasa 		    vdev_name, ctime(&end));
47195cabbc6bSPrashanth Sreenivasa 	} else {
47205cabbc6bSPrashanth Sreenivasa 		uint64_t copied, total, elapsed, mins_left, hours_left;
47215cabbc6bSPrashanth Sreenivasa 		double fraction_done;
47225cabbc6bSPrashanth Sreenivasa 		uint_t rate;
47235cabbc6bSPrashanth Sreenivasa 
47245cabbc6bSPrashanth Sreenivasa 		assert(prs->prs_state == DSS_SCANNING);
47255cabbc6bSPrashanth Sreenivasa 
47265cabbc6bSPrashanth Sreenivasa 		/*
47275cabbc6bSPrashanth Sreenivasa 		 * Removal is in progress.
47285cabbc6bSPrashanth Sreenivasa 		 */
47295cabbc6bSPrashanth Sreenivasa 		(void) printf(gettext(
47305cabbc6bSPrashanth Sreenivasa 		    "Evacuation of %s in progress since %s"),
47315cabbc6bSPrashanth Sreenivasa 		    vdev_name, ctime(&start));
47325cabbc6bSPrashanth Sreenivasa 
47335cabbc6bSPrashanth Sreenivasa 		copied = prs->prs_copied > 0 ? prs->prs_copied : 1;
47345cabbc6bSPrashanth Sreenivasa 		total = prs->prs_to_copy;
47355cabbc6bSPrashanth Sreenivasa 		fraction_done = (double)copied / total;
47365cabbc6bSPrashanth Sreenivasa 
47375cabbc6bSPrashanth Sreenivasa 		/* elapsed time for this pass */
47385cabbc6bSPrashanth Sreenivasa 		elapsed = time(NULL) - prs->prs_start_time;
47395cabbc6bSPrashanth Sreenivasa 		elapsed = elapsed > 0 ? elapsed : 1;
47405cabbc6bSPrashanth Sreenivasa 		rate = copied / elapsed;
47415cabbc6bSPrashanth Sreenivasa 		rate = rate > 0 ? rate : 1;
47425cabbc6bSPrashanth Sreenivasa 		mins_left = ((total - copied) / rate) / 60;
47435cabbc6bSPrashanth Sreenivasa 		hours_left = mins_left / 60;
47445cabbc6bSPrashanth Sreenivasa 
47455cabbc6bSPrashanth Sreenivasa 		zfs_nicenum(copied, examined_buf, sizeof (examined_buf));
47465cabbc6bSPrashanth Sreenivasa 		zfs_nicenum(total, total_buf, sizeof (total_buf));
47475cabbc6bSPrashanth Sreenivasa 		zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
47485cabbc6bSPrashanth Sreenivasa 
47495cabbc6bSPrashanth Sreenivasa 		/*
47505cabbc6bSPrashanth Sreenivasa 		 * do not print estimated time if hours_left is more than
47515cabbc6bSPrashanth Sreenivasa 		 * 30 days
47525cabbc6bSPrashanth Sreenivasa 		 */
47535cabbc6bSPrashanth Sreenivasa 		(void) printf(gettext("    %s copied out of %s at %s/s, "
47545cabbc6bSPrashanth Sreenivasa 		    "%.2f%% done"),
47555cabbc6bSPrashanth Sreenivasa 		    examined_buf, total_buf, rate_buf, 100 * fraction_done);
47565cabbc6bSPrashanth Sreenivasa 		if (hours_left < (30 * 24)) {
47575cabbc6bSPrashanth Sreenivasa 			(void) printf(gettext(", %lluh%um to go\n"),
47585cabbc6bSPrashanth Sreenivasa 			    (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
47595cabbc6bSPrashanth Sreenivasa 		} else {
47605cabbc6bSPrashanth Sreenivasa 			(void) printf(gettext(
47615cabbc6bSPrashanth Sreenivasa 			    ", (copy is slow, no estimated time)\n"));
47625cabbc6bSPrashanth Sreenivasa 		}
47635cabbc6bSPrashanth Sreenivasa 	}
47645cabbc6bSPrashanth Sreenivasa 
47655cabbc6bSPrashanth Sreenivasa 	if (prs->prs_mapping_memory > 0) {
47665cabbc6bSPrashanth Sreenivasa 		char mem_buf[7];
47675cabbc6bSPrashanth Sreenivasa 		zfs_nicenum(prs->prs_mapping_memory, mem_buf, sizeof (mem_buf));
47685cabbc6bSPrashanth Sreenivasa 		(void) printf(gettext("    %s memory used for "
47695cabbc6bSPrashanth Sreenivasa 		    "removed device mappings\n"),
47705cabbc6bSPrashanth Sreenivasa 		    mem_buf);
47715cabbc6bSPrashanth Sreenivasa 	}
47725cabbc6bSPrashanth Sreenivasa }
47735cabbc6bSPrashanth Sreenivasa 
477486714001SSerapheim Dimitropoulos static void
477586714001SSerapheim Dimitropoulos print_checkpoint_status(pool_checkpoint_stat_t *pcs)
477686714001SSerapheim Dimitropoulos {
477786714001SSerapheim Dimitropoulos 	time_t start;
477886714001SSerapheim Dimitropoulos 	char space_buf[7];
477986714001SSerapheim Dimitropoulos 
478086714001SSerapheim Dimitropoulos 	if (pcs == NULL || pcs->pcs_state == CS_NONE)
478186714001SSerapheim Dimitropoulos 		return;
478286714001SSerapheim Dimitropoulos 
478386714001SSerapheim Dimitropoulos 	(void) printf(gettext("checkpoint: "));
478486714001SSerapheim Dimitropoulos 
478586714001SSerapheim Dimitropoulos 	start = pcs->pcs_start_time;
478686714001SSerapheim Dimitropoulos 	zfs_nicenum(pcs->pcs_space, space_buf, sizeof (space_buf));
478786714001SSerapheim Dimitropoulos 
478886714001SSerapheim Dimitropoulos 	if (pcs->pcs_state == CS_CHECKPOINT_EXISTS) {
478986714001SSerapheim Dimitropoulos 		char *date = ctime(&start);
479086714001SSerapheim Dimitropoulos 
479186714001SSerapheim Dimitropoulos 		/*
479286714001SSerapheim Dimitropoulos 		 * ctime() adds a newline at the end of the generated
479386714001SSerapheim Dimitropoulos 		 * string, thus the weird format specifier and the
479486714001SSerapheim Dimitropoulos 		 * strlen() call used to chop it off from the output.
479586714001SSerapheim Dimitropoulos 		 */
479686714001SSerapheim Dimitropoulos 		(void) printf(gettext("created %.*s, consumes %s\n"),
479786714001SSerapheim Dimitropoulos 		    strlen(date) - 1, date, space_buf);
479886714001SSerapheim Dimitropoulos 		return;
479986714001SSerapheim Dimitropoulos 	}
480086714001SSerapheim Dimitropoulos 
480186714001SSerapheim Dimitropoulos 	assert(pcs->pcs_state == CS_CHECKPOINT_DISCARDING);
480286714001SSerapheim Dimitropoulos 
480386714001SSerapheim Dimitropoulos 	(void) printf(gettext("discarding, %s remaining.\n"),
480486714001SSerapheim Dimitropoulos 	    space_buf);
480586714001SSerapheim Dimitropoulos }
480686714001SSerapheim Dimitropoulos 
4807ea8dc4b6Seschrock static void
4808ea8dc4b6Seschrock print_error_log(zpool_handle_t *zhp)
4809ea8dc4b6Seschrock {
481075519f38Sek 	nvlist_t *nverrlist = NULL;
481155434c77Sek 	nvpair_t *elem;
481255434c77Sek 	char *pathname;
481355434c77Sek 	size_t len = MAXPATHLEN * 2;
4814ea8dc4b6Seschrock 
481555434c77Sek 	if (zpool_get_errlog(zhp, &nverrlist) != 0) {
4816ea8dc4b6Seschrock 		(void) printf("errors: List of errors unavailable "
4817ea8dc4b6Seschrock 		    "(insufficient privileges)\n");
4818ea8dc4b6Seschrock 		return;
4819ea8dc4b6Seschrock 	}
4820ea8dc4b6Seschrock 
482155434c77Sek 	(void) printf("errors: Permanent errors have been "
482255434c77Sek 	    "detected in the following files:\n\n");
4823ea8dc4b6Seschrock 
482455434c77Sek 	pathname = safe_malloc(len);
482555434c77Sek 	elem = NULL;
482655434c77Sek 	while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
482755434c77Sek 		nvlist_t *nv;
482855434c77Sek 		uint64_t dsobj, obj;
482955434c77Sek 
483055434c77Sek 		verify(nvpair_value_nvlist(elem, &nv) == 0);
483155434c77Sek 		verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
483255434c77Sek 		    &dsobj) == 0);
483355434c77Sek 		verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
483455434c77Sek 		    &obj) == 0);
483555434c77Sek 		zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
483655434c77Sek 		(void) printf("%7s %s\n", "", pathname);
483755434c77Sek 	}
483855434c77Sek 	free(pathname);
483955434c77Sek 	nvlist_free(nverrlist);
4840ea8dc4b6Seschrock }
4841ea8dc4b6Seschrock 
484299653d4eSeschrock static void
484399653d4eSeschrock print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
484499653d4eSeschrock     int namewidth)
484599653d4eSeschrock {
484699653d4eSeschrock 	uint_t i;
484799653d4eSeschrock 	char *name;
484899653d4eSeschrock 
484999653d4eSeschrock 	if (nspares == 0)
485099653d4eSeschrock 		return;
485199653d4eSeschrock 
485299653d4eSeschrock 	(void) printf(gettext("\tspares\n"));
485399653d4eSeschrock 
485499653d4eSeschrock 	for (i = 0; i < nspares; i++) {
485588ecc943SGeorge Wilson 		name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
485699653d4eSeschrock 		print_status_config(zhp, name, spares[i],
4857aa8cf21aSNeil Perrin 		    namewidth, 2, B_TRUE);
485899653d4eSeschrock 		free(name);
485999653d4eSeschrock 	}
486099653d4eSeschrock }
486199653d4eSeschrock 
4862fa94a07fSbrendan static void
4863fa94a07fSbrendan print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
4864fa94a07fSbrendan     int namewidth)
4865fa94a07fSbrendan {
4866fa94a07fSbrendan 	uint_t i;
4867fa94a07fSbrendan 	char *name;
4868fa94a07fSbrendan 
4869fa94a07fSbrendan 	if (nl2cache == 0)
4870fa94a07fSbrendan 		return;
4871fa94a07fSbrendan 
4872fa94a07fSbrendan 	(void) printf(gettext("\tcache\n"));
4873fa94a07fSbrendan 
4874fa94a07fSbrendan 	for (i = 0; i < nl2cache; i++) {
487588ecc943SGeorge Wilson 		name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
4876fa94a07fSbrendan 		print_status_config(zhp, name, l2cache[i],
4877aa8cf21aSNeil Perrin 		    namewidth, 2, B_FALSE);
4878aa8cf21aSNeil Perrin 		free(name);
4879aa8cf21aSNeil Perrin 	}
4880aa8cf21aSNeil Perrin }
4881aa8cf21aSNeil Perrin 
48829eb19f4dSGeorge Wilson static void
48839eb19f4dSGeorge Wilson print_dedup_stats(nvlist_t *config)
48849eb19f4dSGeorge Wilson {
48859eb19f4dSGeorge Wilson 	ddt_histogram_t *ddh;
48869eb19f4dSGeorge Wilson 	ddt_stat_t *dds;
48879eb19f4dSGeorge Wilson 	ddt_object_t *ddo;
48889eb19f4dSGeorge Wilson 	uint_t c;
48899eb19f4dSGeorge Wilson 
48909eb19f4dSGeorge Wilson 	/*
48919eb19f4dSGeorge Wilson 	 * If the pool was faulted then we may not have been able to
48922384d9f8SGeorge Wilson 	 * obtain the config. Otherwise, if we have anything in the dedup
48939eb19f4dSGeorge Wilson 	 * table continue processing the stats.
48949eb19f4dSGeorge Wilson 	 */
48959eb19f4dSGeorge Wilson 	if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
4896ce72e614SYuri Pankov 	    (uint64_t **)&ddo, &c) != 0)
48979eb19f4dSGeorge Wilson 		return;
48989eb19f4dSGeorge Wilson 
48999eb19f4dSGeorge Wilson 	(void) printf("\n");
4900ce72e614SYuri Pankov 	(void) printf(gettext(" dedup: "));
4901ce72e614SYuri Pankov 	if (ddo->ddo_count == 0) {
4902ce72e614SYuri Pankov 		(void) printf(gettext("no DDT entries\n"));
4903ce72e614SYuri Pankov 		return;
4904ce72e614SYuri Pankov 	}
4905ce72e614SYuri Pankov 
49069eb19f4dSGeorge Wilson 	(void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
49079eb19f4dSGeorge Wilson 	    (u_longlong_t)ddo->ddo_count,
49089eb19f4dSGeorge Wilson 	    (u_longlong_t)ddo->ddo_dspace,
49099eb19f4dSGeorge Wilson 	    (u_longlong_t)ddo->ddo_mspace);
49109eb19f4dSGeorge Wilson 
49119eb19f4dSGeorge Wilson 	verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
49129eb19f4dSGeorge Wilson 	    (uint64_t **)&dds, &c) == 0);
49139eb19f4dSGeorge Wilson 	verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
49149eb19f4dSGeorge Wilson 	    (uint64_t **)&ddh, &c) == 0);
49159eb19f4dSGeorge Wilson 	zpool_dump_ddt(dds, ddh);
49169eb19f4dSGeorge Wilson }
49179eb19f4dSGeorge Wilson 
4918fa9e4066Sahrens /*
4919fa9e4066Sahrens  * Display a summary of pool status.  Displays a summary such as:
4920fa9e4066Sahrens  *
4921fa9e4066Sahrens  *        pool: tank
4922fa9e4066Sahrens  *	status: DEGRADED
4923fa9e4066Sahrens  *	reason: One or more devices ...
4924654b400cSJoshua M. Clulow  *         see: http://illumos.org/msg/ZFS-xxxx-01
4925fa9e4066Sahrens  *	config:
4926fa9e4066Sahrens  *		mirror		DEGRADED
4927fa9e4066Sahrens  *                c1t0d0	OK
4928ea8dc4b6Seschrock  *                c2t0d0	UNAVAIL
4929fa9e4066Sahrens  *
4930fa9e4066Sahrens  * When given the '-v' option, we print out the complete config.  If the '-e'
4931fa9e4066Sahrens  * option is specified, then we print out error rate information as well.
4932fa9e4066Sahrens  */
4933fa9e4066Sahrens int
4934fa9e4066Sahrens status_callback(zpool_handle_t *zhp, void *data)
4935fa9e4066Sahrens {
4936fa9e4066Sahrens 	status_cbdata_t *cbp = data;
4937fa9e4066Sahrens 	nvlist_t *config, *nvroot;
4938fa9e4066Sahrens 	char *msgid;
4939fa9e4066Sahrens 	int reason;
494046657f8dSmmusante 	const char *health;
494146657f8dSmmusante 	uint_t c;
494246657f8dSmmusante 	vdev_stat_t *vs;
4943fa9e4066Sahrens 
4944088e9d47Seschrock 	config = zpool_get_config(zhp, NULL);
4945fa9e4066Sahrens 	reason = zpool_get_status(zhp, &msgid);
4946fa9e4066Sahrens 
4947fa9e4066Sahrens 	cbp->cb_count++;
4948fa9e4066Sahrens 
4949fa9e4066Sahrens 	/*
4950fa9e4066Sahrens 	 * If we were given 'zpool status -x', only report those pools with
4951fa9e4066Sahrens 	 * problems.
4952fa9e4066Sahrens 	 */
4953b3a6f804STim Connors 	if (cbp->cb_explain &&
4954b3a6f804STim Connors 	    (reason == ZPOOL_STATUS_OK ||
4955b3a6f804STim Connors 	    reason == ZPOOL_STATUS_VERSION_OLDER ||
4956b3a6f804STim Connors 	    reason == ZPOOL_STATUS_FEAT_DISABLED)) {
4957e9dbad6fSeschrock 		if (!cbp->cb_allpools) {
4958e9dbad6fSeschrock 			(void) printf(gettext("pool '%s' is healthy\n"),
4959e9dbad6fSeschrock 			    zpool_get_name(zhp));
4960e9dbad6fSeschrock 			if (cbp->cb_first)
4961e9dbad6fSeschrock 				cbp->cb_first = B_FALSE;
4962e9dbad6fSeschrock 		}
4963fa9e4066Sahrens 		return (0);
4964e9dbad6fSeschrock 	}
4965fa9e4066Sahrens 
4966fa9e4066Sahrens 	if (cbp->cb_first)
496799653d4eSeschrock 		cbp->cb_first = B_FALSE;
4968fa9e4066Sahrens 	else
4969fa9e4066Sahrens 		(void) printf("\n");
4970fa9e4066Sahrens 
49715cabbc6bSPrashanth Sreenivasa 	nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
49723f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
497346657f8dSmmusante 	    (uint64_t **)&vs, &c) == 0);
4974990b4856Slling 	health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4975fa9e4066Sahrens 
4976fa9e4066Sahrens 	(void) printf(gettext("  pool: %s\n"), zpool_get_name(zhp));
4977fa9e4066Sahrens 	(void) printf(gettext(" state: %s\n"), health);
4978fa9e4066Sahrens 
4979fa9e4066Sahrens 	switch (reason) {
4980fa9e4066Sahrens 	case ZPOOL_STATUS_MISSING_DEV_R:
4981fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices could not "
4982fa9e4066Sahrens 		    "be opened.  Sufficient replicas exist for\n\tthe pool to "
4983fa9e4066Sahrens 		    "continue functioning in a degraded state.\n"));
4984fa9e4066Sahrens 		(void) printf(gettext("action: Attach the missing device and "
4985fa9e4066Sahrens 		    "online it using 'zpool online'.\n"));
4986fa9e4066Sahrens 		break;
4987fa9e4066Sahrens 
4988fa9e4066Sahrens 	case ZPOOL_STATUS_MISSING_DEV_NR:
4989fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices could not "
4990fa9e4066Sahrens 		    "be opened.  There are insufficient\n\treplicas for the "
4991fa9e4066Sahrens 		    "pool to continue functioning.\n"));
4992fa9e4066Sahrens 		(void) printf(gettext("action: Attach the missing device and "
4993fa9e4066Sahrens 		    "online it using 'zpool online'.\n"));
4994fa9e4066Sahrens 		break;
4995fa9e4066Sahrens 
4996fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_LABEL_R:
4997fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices could not "
4998fa9e4066Sahrens 		    "be used because the label is missing or\n\tinvalid.  "
4999fa9e4066Sahrens 		    "Sufficient replicas exist for the pool to continue\n\t"
5000fa9e4066Sahrens 		    "functioning in a degraded state.\n"));
5001fa9e4066Sahrens 		(void) printf(gettext("action: Replace the device using "
5002fa9e4066Sahrens 		    "'zpool replace'.\n"));
5003fa9e4066Sahrens 		break;
5004fa9e4066Sahrens 
5005fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_LABEL_NR:
5006fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices could not "
5007b1b8ab34Slling 		    "be used because the label is missing \n\tor invalid.  "
5008fa9e4066Sahrens 		    "There are insufficient replicas for the pool to "
5009fa9e4066Sahrens 		    "continue\n\tfunctioning.\n"));
5010468c413aSTim Haley 		zpool_explain_recover(zpool_get_handle(zhp),
5011468c413aSTim Haley 		    zpool_get_name(zhp), reason, config);
5012fa9e4066Sahrens 		break;
5013fa9e4066Sahrens 
5014fa9e4066Sahrens 	case ZPOOL_STATUS_FAILING_DEV:
5015fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices has "
5016fa9e4066Sahrens 		    "experienced an unrecoverable error.  An\n\tattempt was "
5017fa9e4066Sahrens 		    "made to correct the error.  Applications are "
5018fa9e4066Sahrens 		    "unaffected.\n"));
5019fa9e4066Sahrens 		(void) printf(gettext("action: Determine if the device needs "
5020fa9e4066Sahrens 		    "to be replaced, and clear the errors\n\tusing "
5021ea8dc4b6Seschrock 		    "'zpool clear' or replace the device with 'zpool "
5022fa9e4066Sahrens 		    "replace'.\n"));
5023fa9e4066Sahrens 		break;
5024fa9e4066Sahrens 
5025fa9e4066Sahrens 	case ZPOOL_STATUS_OFFLINE_DEV:
5026fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices has "
5027d7d4af51Smmusante 		    "been taken offline by the administrator.\n\tSufficient "
5028fa9e4066Sahrens 		    "replicas exist for the pool to continue functioning in "
5029fa9e4066Sahrens 		    "a\n\tdegraded state.\n"));
5030fa9e4066Sahrens 		(void) printf(gettext("action: Online the device using "
5031fa9e4066Sahrens 		    "'zpool online' or replace the device with\n\t'zpool "
5032fa9e4066Sahrens 		    "replace'.\n"));
5033fa9e4066Sahrens 		break;
5034fa9e4066Sahrens 
5035c25309d4SGeorge Wilson 	case ZPOOL_STATUS_REMOVED_DEV:
5036c25309d4SGeorge Wilson 		(void) printf(gettext("status: One or more devices has "
5037c25309d4SGeorge Wilson 		    "been removed by the administrator.\n\tSufficient "
5038c25309d4SGeorge Wilson 		    "replicas exist for the pool to continue functioning in "
5039c25309d4SGeorge Wilson 		    "a\n\tdegraded state.\n"));
5040c25309d4SGeorge Wilson 		(void) printf(gettext("action: Online the device using "
5041c25309d4SGeorge Wilson 		    "'zpool online' or replace the device with\n\t'zpool "
5042c25309d4SGeorge Wilson 		    "replace'.\n"));
5043c25309d4SGeorge Wilson 		break;
5044c25309d4SGeorge Wilson 
5045fa9e4066Sahrens 	case ZPOOL_STATUS_RESILVERING:
5046fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices is "
5047fa9e4066Sahrens 		    "currently being resilvered.  The pool will\n\tcontinue "
5048fa9e4066Sahrens 		    "to function, possibly in a degraded state.\n"));
5049fa9e4066Sahrens 		(void) printf(gettext("action: Wait for the resilver to "
5050fa9e4066Sahrens 		    "complete.\n"));
5051fa9e4066Sahrens 		break;
5052fa9e4066Sahrens 
5053ea8dc4b6Seschrock 	case ZPOOL_STATUS_CORRUPT_DATA:
5054ea8dc4b6Seschrock 		(void) printf(gettext("status: One or more devices has "
5055ea8dc4b6Seschrock 		    "experienced an error resulting in data\n\tcorruption.  "
5056ea8dc4b6Seschrock 		    "Applications may be affected.\n"));
5057ea8dc4b6Seschrock 		(void) printf(gettext("action: Restore the file in question "
5058ea8dc4b6Seschrock 		    "if possible.  Otherwise restore the\n\tentire pool from "
5059ea8dc4b6Seschrock 		    "backup.\n"));
5060ea8dc4b6Seschrock 		break;
5061ea8dc4b6Seschrock 
5062ea8dc4b6Seschrock 	case ZPOOL_STATUS_CORRUPT_POOL:
5063ea8dc4b6Seschrock 		(void) printf(gettext("status: The pool metadata is corrupted "
5064ea8dc4b6Seschrock 		    "and the pool cannot be opened.\n"));
5065468c413aSTim Haley 		zpool_explain_recover(zpool_get_handle(zhp),
5066468c413aSTim Haley 		    zpool_get_name(zhp), reason, config);
5067ea8dc4b6Seschrock 		break;
5068ea8dc4b6Seschrock 
5069eaca9bbdSeschrock 	case ZPOOL_STATUS_VERSION_OLDER:
507057221772SChristopher Siden 		(void) printf(gettext("status: The pool is formatted using a "
507157221772SChristopher Siden 		    "legacy on-disk format.  The pool can\n\tstill be used, "
507257221772SChristopher Siden 		    "but some features are unavailable.\n"));
5073eaca9bbdSeschrock 		(void) printf(gettext("action: Upgrade the pool using 'zpool "
5074eaca9bbdSeschrock 		    "upgrade'.  Once this is done, the\n\tpool will no longer "
507557221772SChristopher Siden 		    "be accessible on software that does not support feature\n"
507657221772SChristopher Siden 		    "\tflags.\n"));
5077eaca9bbdSeschrock 		break;
5078eaca9bbdSeschrock 
5079eaca9bbdSeschrock 	case ZPOOL_STATUS_VERSION_NEWER:
5080eaca9bbdSeschrock 		(void) printf(gettext("status: The pool has been upgraded to a "
5081eaca9bbdSeschrock 		    "newer, incompatible on-disk version.\n\tThe pool cannot "
5082eaca9bbdSeschrock 		    "be accessed on this system.\n"));
5083eaca9bbdSeschrock 		(void) printf(gettext("action: Access the pool from a system "
5084eaca9bbdSeschrock 		    "running more recent software, or\n\trestore the pool from "
5085eaca9bbdSeschrock 		    "backup.\n"));
5086eaca9bbdSeschrock 		break;
5087eaca9bbdSeschrock 
508857221772SChristopher Siden 	case ZPOOL_STATUS_FEAT_DISABLED:
508957221772SChristopher Siden 		(void) printf(gettext("status: Some supported features are not "
509057221772SChristopher Siden 		    "enabled on the pool. The pool can\n\tstill be used, but "
509157221772SChristopher Siden 		    "some features are unavailable.\n"));
509257221772SChristopher Siden 		(void) printf(gettext("action: Enable all features using "
509357221772SChristopher Siden 		    "'zpool upgrade'. Once this is done,\n\tthe pool may no "
509457221772SChristopher Siden 		    "longer be accessible by software that does not support\n\t"
509557221772SChristopher Siden 		    "the features. See zpool-features(5) for details.\n"));
509657221772SChristopher Siden 		break;
509757221772SChristopher Siden 
5098ad135b5dSChristopher Siden 	case ZPOOL_STATUS_UNSUP_FEAT_READ:
5099ad135b5dSChristopher Siden 		(void) printf(gettext("status: The pool cannot be accessed on "
5100ad135b5dSChristopher Siden 		    "this system because it uses the\n\tfollowing feature(s) "
5101ad135b5dSChristopher Siden 		    "not supported on this system:\n"));
5102ad135b5dSChristopher Siden 		zpool_print_unsup_feat(config);
5103ad135b5dSChristopher Siden 		(void) printf("\n");
5104ad135b5dSChristopher Siden 		(void) printf(gettext("action: Access the pool from a system "
5105ad135b5dSChristopher Siden 		    "that supports the required feature(s),\n\tor restore the "
5106ad135b5dSChristopher Siden 		    "pool from backup.\n"));
5107ad135b5dSChristopher Siden 		break;
5108ad135b5dSChristopher Siden 
5109ad135b5dSChristopher Siden 	case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
5110ad135b5dSChristopher Siden 		(void) printf(gettext("status: The pool can only be accessed "
5111ad135b5dSChristopher Siden 		    "in read-only mode on this system. It\n\tcannot be "
5112ad135b5dSChristopher Siden 		    "accessed in read-write mode because it uses the "
5113ad135b5dSChristopher Siden 		    "following\n\tfeature(s) not supported on this system:\n"));
5114ad135b5dSChristopher Siden 		zpool_print_unsup_feat(config);
5115ad135b5dSChristopher Siden 		(void) printf("\n");
5116ad135b5dSChristopher Siden 		(void) printf(gettext("action: The pool cannot be accessed in "
5117ad135b5dSChristopher Siden 		    "read-write mode. Import the pool with\n"
5118ad135b5dSChristopher Siden 		    "\t\"-o readonly=on\", access the pool from a system that "
5119ad135b5dSChristopher Siden 		    "supports the\n\trequired feature(s), or restore the "
5120ad135b5dSChristopher Siden 		    "pool from backup.\n"));
5121ad135b5dSChristopher Siden 		break;
5122ad135b5dSChristopher Siden 
51233d7072f8Seschrock 	case ZPOOL_STATUS_FAULTED_DEV_R:
51243d7072f8Seschrock 		(void) printf(gettext("status: One or more devices are "
51253d7072f8Seschrock 		    "faulted in response to persistent errors.\n\tSufficient "
51263d7072f8Seschrock 		    "replicas exist for the pool to continue functioning "
51273d7072f8Seschrock 		    "in a\n\tdegraded state.\n"));
51283d7072f8Seschrock 		(void) printf(gettext("action: Replace the faulted device, "
51293d7072f8Seschrock 		    "or use 'zpool clear' to mark the device\n\trepaired.\n"));
51303d7072f8Seschrock 		break;
51313d7072f8Seschrock 
51323d7072f8Seschrock 	case ZPOOL_STATUS_FAULTED_DEV_NR:
51333d7072f8Seschrock 		(void) printf(gettext("status: One or more devices are "
51343d7072f8Seschrock 		    "faulted in response to persistent errors.  There are "
51353d7072f8Seschrock 		    "insufficient replicas for the pool to\n\tcontinue "
51363d7072f8Seschrock 		    "functioning.\n"));
51373d7072f8Seschrock 		(void) printf(gettext("action: Destroy and re-create the pool "
51383d7072f8Seschrock 		    "from a backup source.  Manually marking the device\n"
51393d7072f8Seschrock 		    "\trepaired using 'zpool clear' may allow some data "
51403d7072f8Seschrock 		    "to be recovered.\n"));
51413d7072f8Seschrock 		break;
51423d7072f8Seschrock 
5143*e0f1c0afSOlaf Faaland 	case ZPOOL_STATUS_IO_FAILURE_MMP:
5144*e0f1c0afSOlaf Faaland 		(void) printf(gettext("status: The pool is suspended because "
5145*e0f1c0afSOlaf Faaland 		    "multihost writes failed or were delayed;\n\tanother "
5146*e0f1c0afSOlaf Faaland 		    "system could import the pool undetected.\n"));
5147*e0f1c0afSOlaf Faaland 		(void) printf(gettext("action: Make sure the pool's devices "
5148*e0f1c0afSOlaf Faaland 		    "are connected, then reboot your system and\n\timport the "
5149*e0f1c0afSOlaf Faaland 		    "pool.\n"));
5150*e0f1c0afSOlaf Faaland 		break;
5151*e0f1c0afSOlaf Faaland 
515232b87932Sek 	case ZPOOL_STATUS_IO_FAILURE_WAIT:
515332b87932Sek 	case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
515432b87932Sek 		(void) printf(gettext("status: One or more devices are "
51558a79c1b5Sek 		    "faulted in response to IO failures.\n"));
515632b87932Sek 		(void) printf(gettext("action: Make sure the affected devices "
515732b87932Sek 		    "are connected, then run 'zpool clear'.\n"));
515832b87932Sek 		break;
515932b87932Sek 
5160b87f3af3Sperrin 	case ZPOOL_STATUS_BAD_LOG:
5161b87f3af3Sperrin 		(void) printf(gettext("status: An intent log record "
5162b87f3af3Sperrin 		    "could not be read.\n"
5163b87f3af3Sperrin 		    "\tWaiting for adminstrator intervention to fix the "
5164b87f3af3Sperrin 		    "faulted pool.\n"));
5165b87f3af3Sperrin 		(void) printf(gettext("action: Either restore the affected "
5166b87f3af3Sperrin 		    "device(s) and run 'zpool online',\n"
5167b87f3af3Sperrin 		    "\tor ignore the intent log records by running "
5168b87f3af3Sperrin 		    "'zpool clear'.\n"));
5169b87f3af3Sperrin 		break;
5170b87f3af3Sperrin 
5171fa9e4066Sahrens 	default:
5172fa9e4066Sahrens 		/*
5173fa9e4066Sahrens 		 * The remaining errors can't actually be generated, yet.
5174fa9e4066Sahrens 		 */
5175fa9e4066Sahrens 		assert(reason == ZPOOL_STATUS_OK);
5176fa9e4066Sahrens 	}
5177fa9e4066Sahrens 
5178fa9e4066Sahrens 	if (msgid != NULL)
5179654b400cSJoshua M. Clulow 		(void) printf(gettext("   see: http://illumos.org/msg/%s\n"),
5180fa9e4066Sahrens 		    msgid);
5181fa9e4066Sahrens 
5182fa9e4066Sahrens 	if (config != NULL) {
5183fa9e4066Sahrens 		int namewidth;
5184ea8dc4b6Seschrock 		uint64_t nerr;
5185fa94a07fSbrendan 		nvlist_t **spares, **l2cache;
5186fa94a07fSbrendan 		uint_t nspares, nl2cache;
518786714001SSerapheim Dimitropoulos 		pool_checkpoint_stat_t *pcs = NULL;
51883f9d6ad7SLin Ling 		pool_scan_stat_t *ps = NULL;
51895cabbc6bSPrashanth Sreenivasa 		pool_removal_stat_t *prs = NULL;
5190fa9e4066Sahrens 
519186714001SSerapheim Dimitropoulos 		(void) nvlist_lookup_uint64_array(nvroot,
519286714001SSerapheim Dimitropoulos 		    ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
51933f9d6ad7SLin Ling 		(void) nvlist_lookup_uint64_array(nvroot,
51943f9d6ad7SLin Ling 		    ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
51955cabbc6bSPrashanth Sreenivasa 		(void) nvlist_lookup_uint64_array(nvroot,
51965cabbc6bSPrashanth Sreenivasa 		    ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c);
519786714001SSerapheim Dimitropoulos 
519886714001SSerapheim Dimitropoulos 		print_scan_status(ps);
519986714001SSerapheim Dimitropoulos 		print_checkpoint_scan_warning(ps, pcs);
52005cabbc6bSPrashanth Sreenivasa 		print_removal_status(zhp, prs);
520186714001SSerapheim Dimitropoulos 		print_checkpoint_status(pcs);
52025cabbc6bSPrashanth Sreenivasa 
5203c67d9675Seschrock 		namewidth = max_width(zhp, nvroot, 0, 0);
5204fa9e4066Sahrens 		if (namewidth < 10)
5205fa9e4066Sahrens 			namewidth = 10;
5206fa9e4066Sahrens 
5207fa9e4066Sahrens 		(void) printf(gettext("config:\n\n"));
5208fa9e4066Sahrens 		(void) printf(gettext("\t%-*s  %-8s %5s %5s %5s\n"), namewidth,
5209fa9e4066Sahrens 		    "NAME", "STATE", "READ", "WRITE", "CKSUM");
5210c67d9675Seschrock 		print_status_config(zhp, zpool_get_name(zhp), nvroot,
5211aa8cf21aSNeil Perrin 		    namewidth, 0, B_FALSE);
521299653d4eSeschrock 
52134dea40f0SNeil Perrin 		if (num_logs(nvroot) > 0)
5214e6ca193dSGeorge Wilson 			print_logs(zhp, nvroot, namewidth, B_TRUE);
5215fa94a07fSbrendan 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
5216fa94a07fSbrendan 		    &l2cache, &nl2cache) == 0)
5217fa94a07fSbrendan 			print_l2cache(zhp, l2cache, nl2cache, namewidth);
5218fa94a07fSbrendan 
521999653d4eSeschrock 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
522099653d4eSeschrock 		    &spares, &nspares) == 0)
522199653d4eSeschrock 			print_spares(zhp, spares, nspares, namewidth);
5222ea8dc4b6Seschrock 
5223ea8dc4b6Seschrock 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
5224ea8dc4b6Seschrock 		    &nerr) == 0) {
522555434c77Sek 			nvlist_t *nverrlist = NULL;
522655434c77Sek 
5227ea8dc4b6Seschrock 			/*
5228ea8dc4b6Seschrock 			 * If the approximate error count is small, get a
5229ea8dc4b6Seschrock 			 * precise count by fetching the entire log and
5230ea8dc4b6Seschrock 			 * uniquifying the results.
5231ea8dc4b6Seschrock 			 */
523275519f38Sek 			if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
523355434c77Sek 			    zpool_get_errlog(zhp, &nverrlist) == 0) {
523455434c77Sek 				nvpair_t *elem;
523555434c77Sek 
523655434c77Sek 				elem = NULL;
523755434c77Sek 				nerr = 0;
523855434c77Sek 				while ((elem = nvlist_next_nvpair(nverrlist,
523955434c77Sek 				    elem)) != NULL) {
524055434c77Sek 					nerr++;
524155434c77Sek 				}
524255434c77Sek 			}
524355434c77Sek 			nvlist_free(nverrlist);
5244ea8dc4b6Seschrock 
5245ea8dc4b6Seschrock 			(void) printf("\n");
524699653d4eSeschrock 
5247ea8dc4b6Seschrock 			if (nerr == 0)
5248ea8dc4b6Seschrock 				(void) printf(gettext("errors: No known data "
5249ea8dc4b6Seschrock 				    "errors\n"));
5250ea8dc4b6Seschrock 			else if (!cbp->cb_verbose)
5251e9dbad6fSeschrock 				(void) printf(gettext("errors: %llu data "
52525ad82045Snd 				    "errors, use '-v' for a list\n"),
52535ad82045Snd 				    (u_longlong_t)nerr);
5254ea8dc4b6Seschrock 			else
5255ea8dc4b6Seschrock 				print_error_log(zhp);
5256ea8dc4b6Seschrock 		}
52579eb19f4dSGeorge Wilson 
52589eb19f4dSGeorge Wilson 		if (cbp->cb_dedup_stats)
52599eb19f4dSGeorge Wilson 			print_dedup_stats(config);
5260fa9e4066Sahrens 	} else {
5261fa9e4066Sahrens 		(void) printf(gettext("config: The configuration cannot be "
5262fa9e4066Sahrens 		    "determined.\n"));
5263fa9e4066Sahrens 	}
5264fa9e4066Sahrens 
5265fa9e4066Sahrens 	return (0);
5266fa9e4066Sahrens }
5267fa9e4066Sahrens 
5268fa9e4066Sahrens /*
52693f9d6ad7SLin Ling  * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
5270fa9e4066Sahrens  *
5271fa9e4066Sahrens  *	-v	Display complete error logs
5272fa9e4066Sahrens  *	-x	Display only pools with potential problems
52739eb19f4dSGeorge Wilson  *	-D	Display dedup status (undocumented)
52743f9d6ad7SLin Ling  *	-T	Display a timestamp in date(1) or Unix format
5275fa9e4066Sahrens  *
5276fa9e4066Sahrens  * Describes the health status of all pools or some subset.
5277fa9e4066Sahrens  */
5278fa9e4066Sahrens int
5279fa9e4066Sahrens zpool_do_status(int argc, char **argv)
5280fa9e4066Sahrens {
5281fa9e4066Sahrens 	int c;
5282fa9e4066Sahrens 	int ret;
52833f9d6ad7SLin Ling 	unsigned long interval = 0, count = 0;
5284fa9e4066Sahrens 	status_cbdata_t cb = { 0 };
5285fa9e4066Sahrens 
5286fa9e4066Sahrens 	/* check options */
52873f9d6ad7SLin Ling 	while ((c = getopt(argc, argv, "vxDT:")) != -1) {
5288fa9e4066Sahrens 		switch (c) {
5289fa9e4066Sahrens 		case 'v':
529099653d4eSeschrock 			cb.cb_verbose = B_TRUE;
5291fa9e4066Sahrens 			break;
5292fa9e4066Sahrens 		case 'x':
529399653d4eSeschrock 			cb.cb_explain = B_TRUE;
5294fa9e4066Sahrens 			break;
52959eb19f4dSGeorge Wilson 		case 'D':
52969eb19f4dSGeorge Wilson 			cb.cb_dedup_stats = B_TRUE;
52979eb19f4dSGeorge Wilson 			break;
52983f9d6ad7SLin Ling 		case 'T':
52993f9d6ad7SLin Ling 			get_timestamp_arg(*optarg);
53003f9d6ad7SLin Ling 			break;
5301fa9e4066Sahrens 		case '?':
5302fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
5303fa9e4066Sahrens 			    optopt);
530499653d4eSeschrock 			usage(B_FALSE);
5305fa9e4066Sahrens 		}
5306fa9e4066Sahrens 	}
5307fa9e4066Sahrens 
5308fa9e4066Sahrens 	argc -= optind;
5309fa9e4066Sahrens 	argv += optind;
5310fa9e4066Sahrens 
53113f9d6ad7SLin Ling 	get_interval_count(&argc, argv, &interval, &count);
5312fa9e4066Sahrens 
5313e9dbad6fSeschrock 	if (argc == 0)
5314e9dbad6fSeschrock 		cb.cb_allpools = B_TRUE;
5315e9dbad6fSeschrock 
53163f9d6ad7SLin Ling 	cb.cb_first = B_TRUE;
5317fa9e4066Sahrens 
53183f9d6ad7SLin Ling 	for (;;) {
53193f9d6ad7SLin Ling 		if (timestamp_fmt != NODATE)
53203f9d6ad7SLin Ling 			print_timestamp(timestamp_fmt);
5321fa9e4066Sahrens 
53223f9d6ad7SLin Ling 		ret = for_each_pool(argc, argv, B_TRUE, NULL,
53233f9d6ad7SLin Ling 		    status_callback, &cb);
53243f9d6ad7SLin Ling 
53253f9d6ad7SLin Ling 		if (argc == 0 && cb.cb_count == 0)
53263f9d6ad7SLin Ling 			(void) printf(gettext("no pools available\n"));
53273f9d6ad7SLin Ling 		else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
53283f9d6ad7SLin Ling 			(void) printf(gettext("all pools are healthy\n"));
53293f9d6ad7SLin Ling 
53303f9d6ad7SLin Ling 		if (ret != 0)
53313f9d6ad7SLin Ling 			return (ret);
53323f9d6ad7SLin Ling 
53333f9d6ad7SLin Ling 		if (interval == 0)
53343f9d6ad7SLin Ling 			break;
53353f9d6ad7SLin Ling 
53363f9d6ad7SLin Ling 		if (count != 0 && --count == 0)
53373f9d6ad7SLin Ling 			break;
53383f9d6ad7SLin Ling 
53393f9d6ad7SLin Ling 		(void) sleep(interval);
53403f9d6ad7SLin Ling 	}
53413f9d6ad7SLin Ling 
53423f9d6ad7SLin Ling 	return (0);
5343fa9e4066Sahrens }
5344fa9e4066Sahrens 
5345eaca9bbdSeschrock typedef struct upgrade_cbdata {
5346eaca9bbdSeschrock 	int	cb_first;
534706eeb2adSek 	int	cb_argc;
5348990b4856Slling 	uint64_t cb_version;
534906eeb2adSek 	char	**cb_argv;
5350eaca9bbdSeschrock } upgrade_cbdata_t;
5351eaca9bbdSeschrock 
535257221772SChristopher Siden static int
535357221772SChristopher Siden upgrade_version(zpool_handle_t *zhp, uint64_t version)
535457221772SChristopher Siden {
535557221772SChristopher Siden 	int ret;
535657221772SChristopher Siden 	nvlist_t *config;
535757221772SChristopher Siden 	uint64_t oldversion;
535857221772SChristopher Siden 
535957221772SChristopher Siden 	config = zpool_get_config(zhp, NULL);
536057221772SChristopher Siden 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
536157221772SChristopher Siden 	    &oldversion) == 0);
536257221772SChristopher Siden 
536357221772SChristopher Siden 	assert(SPA_VERSION_IS_SUPPORTED(oldversion));
536457221772SChristopher Siden 	assert(oldversion < version);
536557221772SChristopher Siden 
536657221772SChristopher Siden 	ret = zpool_upgrade(zhp, version);
536757221772SChristopher Siden 	if (ret != 0)
536857221772SChristopher Siden 		return (ret);
536957221772SChristopher Siden 
537057221772SChristopher Siden 	if (version >= SPA_VERSION_FEATURES) {
537157221772SChristopher Siden 		(void) printf(gettext("Successfully upgraded "
537257221772SChristopher Siden 		    "'%s' from version %llu to feature flags.\n"),
537357221772SChristopher Siden 		    zpool_get_name(zhp), oldversion);
537457221772SChristopher Siden 	} else {
537557221772SChristopher Siden 		(void) printf(gettext("Successfully upgraded "
537657221772SChristopher Siden 		    "'%s' from version %llu to version %llu.\n"),
537757221772SChristopher Siden 		    zpool_get_name(zhp), oldversion, version);
537857221772SChristopher Siden 	}
537957221772SChristopher Siden 
538057221772SChristopher Siden 	return (0);
538157221772SChristopher Siden }
538257221772SChristopher Siden 
538357221772SChristopher Siden static int
538457221772SChristopher Siden upgrade_enable_all(zpool_handle_t *zhp, int *countp)
538557221772SChristopher Siden {
538657221772SChristopher Siden 	int i, ret, count;
538757221772SChristopher Siden 	boolean_t firstff = B_TRUE;
538857221772SChristopher Siden 	nvlist_t *enabled = zpool_get_features(zhp);
538957221772SChristopher Siden 
539057221772SChristopher Siden 	count = 0;
539157221772SChristopher Siden 	for (i = 0; i < SPA_FEATURES; i++) {
539257221772SChristopher Siden 		const char *fname = spa_feature_table[i].fi_uname;
539357221772SChristopher Siden 		const char *fguid = spa_feature_table[i].fi_guid;
539457221772SChristopher Siden 		if (!nvlist_exists(enabled, fguid)) {
539557221772SChristopher Siden 			char *propname;
539657221772SChristopher Siden 			verify(-1 != asprintf(&propname, "feature@%s", fname));
539757221772SChristopher Siden 			ret = zpool_set_prop(zhp, propname,
539857221772SChristopher Siden 			    ZFS_FEATURE_ENABLED);
539957221772SChristopher Siden 			if (ret != 0) {
540057221772SChristopher Siden 				free(propname);
540157221772SChristopher Siden 				return (ret);
540257221772SChristopher Siden 			}
540357221772SChristopher Siden 			count++;
540457221772SChristopher Siden 
540557221772SChristopher Siden 			if (firstff) {
540657221772SChristopher Siden 				(void) printf(gettext("Enabled the "
540757221772SChristopher Siden 				    "following features on '%s':\n"),
540857221772SChristopher Siden 				    zpool_get_name(zhp));
540957221772SChristopher Siden 				firstff = B_FALSE;
541057221772SChristopher Siden 			}
541157221772SChristopher Siden 			(void) printf(gettext("  %s\n"), fname);
541257221772SChristopher Siden 			free(propname);
541357221772SChristopher Siden 		}
541457221772SChristopher Siden 	}
541557221772SChristopher Siden 
541657221772SChristopher Siden 	if (countp != NULL)
541757221772SChristopher Siden 		*countp = count;
541857221772SChristopher Siden 	return (0);
541957221772SChristopher Siden }
542057221772SChristopher Siden 
5421eaca9bbdSeschrock static int
5422eaca9bbdSeschrock upgrade_cb(zpool_handle_t *zhp, void *arg)
5423eaca9bbdSeschrock {
5424eaca9bbdSeschrock 	upgrade_cbdata_t *cbp = arg;
5425eaca9bbdSeschrock 	nvlist_t *config;
5426eaca9bbdSeschrock 	uint64_t version;
542757221772SChristopher Siden 	boolean_t printnl = B_FALSE;
542857221772SChristopher Siden 	int ret;
5429eaca9bbdSeschrock 
5430eaca9bbdSeschrock 	config = zpool_get_config(zhp, NULL);
5431eaca9bbdSeschrock 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
5432eaca9bbdSeschrock 	    &version) == 0);
5433eaca9bbdSeschrock 
543457221772SChristopher Siden 	assert(SPA_VERSION_IS_SUPPORTED(version));
5435eaca9bbdSeschrock 
543657221772SChristopher Siden 	if (version < cbp->cb_version) {
543757221772SChristopher Siden 		cbp->cb_first = B_FALSE;
543857221772SChristopher Siden 		ret = upgrade_version(zhp, cbp->cb_version);
543957221772SChristopher Siden 		if (ret != 0)
544057221772SChristopher Siden 			return (ret);
544157221772SChristopher Siden 		printnl = B_TRUE;
544257221772SChristopher Siden 
544357221772SChristopher Siden 		/*
544457221772SChristopher Siden 		 * If they did "zpool upgrade -a", then we could
544557221772SChristopher Siden 		 * be doing ioctls to different pools.  We need
544657221772SChristopher Siden 		 * to log this history once to each pool, and bypass
544757221772SChristopher Siden 		 * the normal history logging that happens in main().
544857221772SChristopher Siden 		 */
544957221772SChristopher Siden 		(void) zpool_log_history(g_zfs, history_str);
545057221772SChristopher Siden 		log_history = B_FALSE;
545157221772SChristopher Siden 	}
545257221772SChristopher Siden 
545357221772SChristopher Siden 	if (cbp->cb_version >= SPA_VERSION_FEATURES) {
545457221772SChristopher Siden 		int count;
545557221772SChristopher Siden 		ret = upgrade_enable_all(zhp, &count);
545657221772SChristopher Siden 		if (ret != 0)
545757221772SChristopher Siden 			return (ret);
545857221772SChristopher Siden 
545957221772SChristopher Siden 		if (count > 0) {
546099653d4eSeschrock 			cbp->cb_first = B_FALSE;
546157221772SChristopher Siden 			printnl = B_TRUE;
5462eaca9bbdSeschrock 		}
546357221772SChristopher Siden 	}
5464eaca9bbdSeschrock 
546557221772SChristopher Siden 	if (printnl) {
546657221772SChristopher Siden 		(void) printf(gettext("\n"));
546757221772SChristopher Siden 	}
546857221772SChristopher Siden 
546957221772SChristopher Siden 	return (0);
547057221772SChristopher Siden }
547157221772SChristopher Siden 
547257221772SChristopher Siden static int
547357221772SChristopher Siden upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
547457221772SChristopher Siden {
547557221772SChristopher Siden 	upgrade_cbdata_t *cbp = arg;
547657221772SChristopher Siden 	nvlist_t *config;
547757221772SChristopher Siden 	uint64_t version;
547857221772SChristopher Siden 
547957221772SChristopher Siden 	config = zpool_get_config(zhp, NULL);
548057221772SChristopher Siden 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
548157221772SChristopher Siden 	    &version) == 0);
548257221772SChristopher Siden 
548357221772SChristopher Siden 	assert(SPA_VERSION_IS_SUPPORTED(version));
548457221772SChristopher Siden 
548557221772SChristopher Siden 	if (version < SPA_VERSION_FEATURES) {
5486eaca9bbdSeschrock 		if (cbp->cb_first) {
5487eaca9bbdSeschrock 			(void) printf(gettext("The following pools are "
548857221772SChristopher Siden 			    "formatted with legacy version numbers and can\n"
548957221772SChristopher Siden 			    "be upgraded to use feature flags.  After "
549057221772SChristopher Siden 			    "being upgraded, these pools\nwill no "
549157221772SChristopher Siden 			    "longer be accessible by software that does not "
549257221772SChristopher Siden 			    "support feature\nflags.\n\n"));
5493eaca9bbdSeschrock 			(void) printf(gettext("VER  POOL\n"));
5494eaca9bbdSeschrock 			(void) printf(gettext("---  ------------\n"));
549599653d4eSeschrock 			cbp->cb_first = B_FALSE;
5496eaca9bbdSeschrock 		}
5497eaca9bbdSeschrock 
54985ad82045Snd 		(void) printf("%2llu   %s\n", (u_longlong_t)version,
5499eaca9bbdSeschrock 		    zpool_get_name(zhp));
5500eaca9bbdSeschrock 	}
5501eaca9bbdSeschrock 
550257221772SChristopher Siden 	return (0);
550357221772SChristopher Siden }
550457221772SChristopher Siden 
550557221772SChristopher Siden static int
550657221772SChristopher Siden upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
550757221772SChristopher Siden {
550857221772SChristopher Siden 	upgrade_cbdata_t *cbp = arg;
550957221772SChristopher Siden 	nvlist_t *config;
551057221772SChristopher Siden 	uint64_t version;
551157221772SChristopher Siden 
551257221772SChristopher Siden 	config = zpool_get_config(zhp, NULL);
551357221772SChristopher Siden 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
551457221772SChristopher Siden 	    &version) == 0);
551557221772SChristopher Siden 
551657221772SChristopher Siden 	if (version >= SPA_VERSION_FEATURES) {
551757221772SChristopher Siden 		int i;
551857221772SChristopher Siden 		boolean_t poolfirst = B_TRUE;
551957221772SChristopher Siden 		nvlist_t *enabled = zpool_get_features(zhp);
552057221772SChristopher Siden 
552157221772SChristopher Siden 		for (i = 0; i < SPA_FEATURES; i++) {
552257221772SChristopher Siden 			const char *fguid = spa_feature_table[i].fi_guid;
552357221772SChristopher Siden 			const char *fname = spa_feature_table[i].fi_uname;
552457221772SChristopher Siden 			if (!nvlist_exists(enabled, fguid)) {
552557221772SChristopher Siden 				if (cbp->cb_first) {
552657221772SChristopher Siden 					(void) printf(gettext("\nSome "
552757221772SChristopher Siden 					    "supported features are not "
552857221772SChristopher Siden 					    "enabled on the following pools. "
552957221772SChristopher Siden 					    "Once a\nfeature is enabled the "
553057221772SChristopher Siden 					    "pool may become incompatible with "
553157221772SChristopher Siden 					    "software\nthat does not support "
553257221772SChristopher Siden 					    "the feature. See "
553357221772SChristopher Siden 					    "zpool-features(5) for "
553457221772SChristopher Siden 					    "details.\n\n"));
553557221772SChristopher Siden 					(void) printf(gettext("POOL  "
553657221772SChristopher Siden 					    "FEATURE\n"));
553757221772SChristopher Siden 					(void) printf(gettext("------"
553857221772SChristopher Siden 					    "---------\n"));
553957221772SChristopher Siden 					cbp->cb_first = B_FALSE;
554057221772SChristopher Siden 				}
554157221772SChristopher Siden 
554257221772SChristopher Siden 				if (poolfirst) {
554357221772SChristopher Siden 					(void) printf(gettext("%s\n"),
554457221772SChristopher Siden 					    zpool_get_name(zhp));
554557221772SChristopher Siden 					poolfirst = B_FALSE;
554657221772SChristopher Siden 				}
554757221772SChristopher Siden 
554857221772SChristopher Siden 				(void) printf(gettext("      %s\n"), fname);
554957221772SChristopher Siden 			}
555057221772SChristopher Siden 		}
555157221772SChristopher Siden 	}
555257221772SChristopher Siden 
555357221772SChristopher Siden 	return (0);
5554eaca9bbdSeschrock }
5555eaca9bbdSeschrock 
5556eaca9bbdSeschrock /* ARGSUSED */
5557eaca9bbdSeschrock static int
555806eeb2adSek upgrade_one(zpool_handle_t *zhp, void *data)
5559eaca9bbdSeschrock {
556057221772SChristopher Siden 	boolean_t printnl = B_FALSE;
5561990b4856Slling 	upgrade_cbdata_t *cbp = data;
5562990b4856Slling 	uint64_t cur_version;
5563eaca9bbdSeschrock 	int ret;
5564eaca9bbdSeschrock 
55658654d025Sperrin 	if (strcmp("log", zpool_get_name(zhp)) == 0) {
55668654d025Sperrin 		(void) printf(gettext("'log' is now a reserved word\n"
55678654d025Sperrin 		    "Pool 'log' must be renamed using export and import"
55688654d025Sperrin 		    " to upgrade.\n"));
55698654d025Sperrin 		return (1);
55708654d025Sperrin 	}
5571990b4856Slling 
5572990b4856Slling 	cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
5573e6c728e1Sbrendan 	if (cur_version > cbp->cb_version) {
5574eaca9bbdSeschrock 		(void) printf(gettext("Pool '%s' is already formatted "
557557221772SChristopher Siden 		    "using more current version '%llu'.\n\n"),
5576e6c728e1Sbrendan 		    zpool_get_name(zhp), cur_version);
5577e6c728e1Sbrendan 		return (0);
5578e6c728e1Sbrendan 	}
557957221772SChristopher Siden 
558057221772SChristopher Siden 	if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
5581e6c728e1Sbrendan 		(void) printf(gettext("Pool '%s' is already formatted "
558257221772SChristopher Siden 		    "using version %llu.\n\n"), zpool_get_name(zhp),
558357221772SChristopher Siden 		    cbp->cb_version);
5584eaca9bbdSeschrock 		return (0);
5585eaca9bbdSeschrock 	}
5586eaca9bbdSeschrock 
558757221772SChristopher Siden 	if (cur_version != cbp->cb_version) {
558857221772SChristopher Siden 		printnl = B_TRUE;
558957221772SChristopher Siden 		ret = upgrade_version(zhp, cbp->cb_version);
559057221772SChristopher Siden 		if (ret != 0)
559157221772SChristopher Siden 			return (ret);
559257221772SChristopher Siden 	}
559306eeb2adSek 
559457221772SChristopher Siden 	if (cbp->cb_version >= SPA_VERSION_FEATURES) {
559557221772SChristopher Siden 		int count = 0;
559657221772SChristopher Siden 		ret = upgrade_enable_all(zhp, &count);
559757221772SChristopher Siden 		if (ret != 0)
559857221772SChristopher Siden 			return (ret);
559957221772SChristopher Siden 
560057221772SChristopher Siden 		if (count != 0) {
560157221772SChristopher Siden 			printnl = B_TRUE;
560257221772SChristopher Siden 		} else if (cur_version == SPA_VERSION) {
560357221772SChristopher Siden 			(void) printf(gettext("Pool '%s' already has all "
560457221772SChristopher Siden 			    "supported features enabled.\n"),
560557221772SChristopher Siden 			    zpool_get_name(zhp));
560657221772SChristopher Siden 		}
560757221772SChristopher Siden 	}
560857221772SChristopher Siden 
560957221772SChristopher Siden 	if (printnl) {
561057221772SChristopher Siden 		(void) printf(gettext("\n"));
561106eeb2adSek 	}
5612eaca9bbdSeschrock 
561357221772SChristopher Siden 	return (0);
5614eaca9bbdSeschrock }
5615eaca9bbdSeschrock 
5616eaca9bbdSeschrock /*
5617eaca9bbdSeschrock  * zpool upgrade
5618eaca9bbdSeschrock  * zpool upgrade -v
5619990b4856Slling  * zpool upgrade [-V version] <-a | pool ...>
5620eaca9bbdSeschrock  *
5621eaca9bbdSeschrock  * With no arguments, display downrev'd ZFS pool available for upgrade.
5622eaca9bbdSeschrock  * Individual pools can be upgraded by specifying the pool, and '-a' will
5623eaca9bbdSeschrock  * upgrade all pools.
5624eaca9bbdSeschrock  */
5625eaca9bbdSeschrock int
5626eaca9bbdSeschrock zpool_do_upgrade(int argc, char **argv)
5627eaca9bbdSeschrock {
5628eaca9bbdSeschrock 	int c;
5629eaca9bbdSeschrock 	upgrade_cbdata_t cb = { 0 };
5630eaca9bbdSeschrock 	int ret = 0;
5631eaca9bbdSeschrock 	boolean_t showversions = B_FALSE;
563257221772SChristopher Siden 	boolean_t upgradeall = B_FALSE;
5633990b4856Slling 	char *end;
5634990b4856Slling 
5635eaca9bbdSeschrock 
5636eaca9bbdSeschrock 	/* check options */
5637478ed9adSEric Taylor 	while ((c = getopt(argc, argv, ":avV:")) != -1) {
5638eaca9bbdSeschrock 		switch (c) {
5639eaca9bbdSeschrock 		case 'a':
564057221772SChristopher Siden 			upgradeall = B_TRUE;
5641eaca9bbdSeschrock 			break;
5642eaca9bbdSeschrock 		case 'v':
5643eaca9bbdSeschrock 			showversions = B_TRUE;
5644eaca9bbdSeschrock 			break;
5645990b4856Slling 		case 'V':
5646990b4856Slling 			cb.cb_version = strtoll(optarg, &end, 10);
5647ad135b5dSChristopher Siden 			if (*end != '\0' ||
5648ad135b5dSChristopher Siden 			    !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
5649990b4856Slling 				(void) fprintf(stderr,
5650990b4856Slling 				    gettext("invalid version '%s'\n"), optarg);
5651990b4856Slling 				usage(B_FALSE);
5652990b4856Slling 			}
5653990b4856Slling 			break;
5654478ed9adSEric Taylor 		case ':':
5655478ed9adSEric Taylor 			(void) fprintf(stderr, gettext("missing argument for "
5656478ed9adSEric Taylor 			    "'%c' option\n"), optopt);
5657478ed9adSEric Taylor 			usage(B_FALSE);
5658478ed9adSEric Taylor 			break;
5659eaca9bbdSeschrock 		case '?':
5660eaca9bbdSeschrock 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
5661eaca9bbdSeschrock 			    optopt);
566299653d4eSeschrock 			usage(B_FALSE);
5663eaca9bbdSeschrock 		}
5664eaca9bbdSeschrock 	}
5665eaca9bbdSeschrock 
566606eeb2adSek 	cb.cb_argc = argc;
566706eeb2adSek 	cb.cb_argv = argv;
5668eaca9bbdSeschrock 	argc -= optind;
5669eaca9bbdSeschrock 	argv += optind;
5670eaca9bbdSeschrock 
5671351420b3Slling 	if (cb.cb_version == 0) {
5672351420b3Slling 		cb.cb_version = SPA_VERSION;
567357221772SChristopher Siden 	} else if (!upgradeall && argc == 0) {
5674351420b3Slling 		(void) fprintf(stderr, gettext("-V option is "
5675351420b3Slling 		    "incompatible with other arguments\n"));
5676351420b3Slling 		usage(B_FALSE);
5677351420b3Slling 	}
5678351420b3Slling 
5679eaca9bbdSeschrock 	if (showversions) {
568057221772SChristopher Siden 		if (upgradeall || argc != 0) {
5681eaca9bbdSeschrock 			(void) fprintf(stderr, gettext("-v option is "
5682eaca9bbdSeschrock 			    "incompatible with other arguments\n"));
568399653d4eSeschrock 			usage(B_FALSE);
5684eaca9bbdSeschrock 		}
568557221772SChristopher Siden 	} else if (upgradeall) {
5686eaca9bbdSeschrock 		if (argc != 0) {
5687351420b3Slling 			(void) fprintf(stderr, gettext("-a option should not "
5688351420b3Slling 			    "be used along with a pool name\n"));
568999653d4eSeschrock 			usage(B_FALSE);
5690eaca9bbdSeschrock 		}
5691eaca9bbdSeschrock 	}
5692eaca9bbdSeschrock 
5693ad135b5dSChristopher Siden 	(void) printf(gettext("This system supports ZFS pool feature "
5694ad135b5dSChristopher Siden 	    "flags.\n\n"));
5695eaca9bbdSeschrock 	if (showversions) {
569657221772SChristopher Siden 		int i;
569757221772SChristopher Siden 
569857221772SChristopher Siden 		(void) printf(gettext("The following features are "
569957221772SChristopher Siden 		    "supported:\n\n"));
570057221772SChristopher Siden 		(void) printf(gettext("FEAT DESCRIPTION\n"));
570157221772SChristopher Siden 		(void) printf("----------------------------------------------"
570257221772SChristopher Siden 		    "---------------\n");
570357221772SChristopher Siden 		for (i = 0; i < SPA_FEATURES; i++) {
570457221772SChristopher Siden 			zfeature_info_t *fi = &spa_feature_table[i];
5705ca0cc391SMatthew Ahrens 			const char *ro =
5706ca0cc391SMatthew Ahrens 			    (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
570757221772SChristopher Siden 			    " (read-only compatible)" : "";
570857221772SChristopher Siden 
570957221772SChristopher Siden 			(void) printf("%-37s%s\n", fi->fi_uname, ro);
571057221772SChristopher Siden 			(void) printf("     %s\n", fi->fi_desc);
571157221772SChristopher Siden 		}
571257221772SChristopher Siden 		(void) printf("\n");
571357221772SChristopher Siden 
571457221772SChristopher Siden 		(void) printf(gettext("The following legacy versions are also "
5715d7d4af51Smmusante 		    "supported:\n\n"));
5716eaca9bbdSeschrock 		(void) printf(gettext("VER  DESCRIPTION\n"));
5717eaca9bbdSeschrock 		(void) printf("---  -----------------------------------------"
5718eaca9bbdSeschrock 		    "---------------\n");
571999653d4eSeschrock 		(void) printf(gettext(" 1   Initial ZFS version\n"));
572044cd46caSbillm 		(void) printf(gettext(" 2   Ditto blocks "
572144cd46caSbillm 		    "(replicated metadata)\n"));
572299653d4eSeschrock 		(void) printf(gettext(" 3   Hot spares and double parity "
572399653d4eSeschrock 		    "RAID-Z\n"));
5724d7306b64Sek 		(void) printf(gettext(" 4   zpool history\n"));
5725c9431fa1Sahl 		(void) printf(gettext(" 5   Compression using the gzip "
5726c9431fa1Sahl 		    "algorithm\n"));
5727990b4856Slling 		(void) printf(gettext(" 6   bootfs pool property\n"));
57288654d025Sperrin 		(void) printf(gettext(" 7   Separate intent log devices\n"));
5729ecd6cf80Smarks 		(void) printf(gettext(" 8   Delegated administration\n"));
57308eed72d4Sck 		(void) printf(gettext(" 9   refquota and refreservation "
5731a9799022Sck 		    "properties\n"));
5732fa94a07fSbrendan 		(void) printf(gettext(" 10  Cache devices\n"));
5733088f3894Sahrens 		(void) printf(gettext(" 11  Improved scrub performance\n"));
5734bb0ade09Sahrens 		(void) printf(gettext(" 12  Snapshot properties\n"));
573574e7dc98SMatthew Ahrens 		(void) printf(gettext(" 13  snapused property\n"));
573614843421SMatthew Ahrens 		(void) printf(gettext(" 14  passthrough-x aclinherit\n"));
573714843421SMatthew Ahrens 		(void) printf(gettext(" 15  user/group space accounting\n"));
5738478ed9adSEric Taylor 		(void) printf(gettext(" 16  stmf property support\n"));
57397aeab329SAdam Leventhal 		(void) printf(gettext(" 17  Triple-parity RAID-Z\n"));
5740b24ab676SJeff Bonwick 		(void) printf(gettext(" 18  Snapshot user holds\n"));
574188ecc943SGeorge Wilson 		(void) printf(gettext(" 19  Log device removal\n"));
5742b24ab676SJeff Bonwick 		(void) printf(gettext(" 20  Compression using zle "
5743b24ab676SJeff Bonwick 		    "(zero-length encoding)\n"));
5744b24ab676SJeff Bonwick 		(void) printf(gettext(" 21  Deduplication\n"));
574592241e0bSTom Erickson 		(void) printf(gettext(" 22  Received properties\n"));
57466e1f5caaSNeil Perrin 		(void) printf(gettext(" 23  Slim ZIL\n"));
57470a586ceaSMark Shellenbaum 		(void) printf(gettext(" 24  System attributes\n"));
57483f9d6ad7SLin Ling 		(void) printf(gettext(" 25  Improved scrub stats\n"));
5749cde58dbcSMatthew Ahrens 		(void) printf(gettext(" 26  Improved snapshot deletion "
5750cde58dbcSMatthew Ahrens 		    "performance\n"));
57516e0cbcaaSMatthew Ahrens 		(void) printf(gettext(" 27  Improved snapshot creation "
57526e0cbcaaSMatthew Ahrens 		    "performance\n"));
5753cb04b873SMark J Musante 		(void) printf(gettext(" 28  Multiple vdev replacements\n"));
5754b24ab676SJeff Bonwick 		(void) printf(gettext("\nFor more information on a particular "
57559a8685acSstephanie scheffler 		    "version, including supported releases,\n"));
57569a8685acSstephanie scheffler 		(void) printf(gettext("see the ZFS Administration Guide.\n\n"));
575757221772SChristopher Siden 	} else if (argc == 0 && upgradeall) {
575857221772SChristopher Siden 		cb.cb_first = B_TRUE;
575999653d4eSeschrock 		ret = zpool_iter(g_zfs, upgrade_cb, &cb);
576057221772SChristopher Siden 		if (ret == 0 && cb.cb_first) {
576157221772SChristopher Siden 			if (cb.cb_version == SPA_VERSION) {
576257221772SChristopher Siden 				(void) printf(gettext("All pools are already "
576357221772SChristopher Siden 				    "formatted using feature flags.\n\n"));
576457221772SChristopher Siden 				(void) printf(gettext("Every feature flags "
576557221772SChristopher Siden 				    "pool already has all supported features "
576657221772SChristopher Siden 				    "enabled.\n"));
576757221772SChristopher Siden 			} else {
576857221772SChristopher Siden 				(void) printf(gettext("All pools are already "
576957221772SChristopher Siden 				    "formatted with version %llu or higher.\n"),
577057221772SChristopher Siden 				    cb.cb_version);
5771eaca9bbdSeschrock 			}
5772eaca9bbdSeschrock 		}
577357221772SChristopher Siden 	} else if (argc == 0) {
577457221772SChristopher Siden 		cb.cb_first = B_TRUE;
577557221772SChristopher Siden 		ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
577657221772SChristopher Siden 		assert(ret == 0);
5777eaca9bbdSeschrock 
577857221772SChristopher Siden 		if (cb.cb_first) {
577957221772SChristopher Siden 			(void) printf(gettext("All pools are formatted "
578057221772SChristopher Siden 			    "using feature flags.\n\n"));
578157221772SChristopher Siden 		} else {
578257221772SChristopher Siden 			(void) printf(gettext("\nUse 'zpool upgrade -v' "
578357221772SChristopher Siden 			    "for a list of available legacy versions.\n"));
578457221772SChristopher Siden 		}
578557221772SChristopher Siden 
578657221772SChristopher Siden 		cb.cb_first = B_TRUE;
578757221772SChristopher Siden 		ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
578857221772SChristopher Siden 		assert(ret == 0);
578957221772SChristopher Siden 
579057221772SChristopher Siden 		if (cb.cb_first) {
579157221772SChristopher Siden 			(void) printf(gettext("Every feature flags pool has "
579257221772SChristopher Siden 			    "all supported features enabled.\n"));
579357221772SChristopher Siden 		} else {
579457221772SChristopher Siden 			(void) printf(gettext("\n"));
5795eaca9bbdSeschrock 		}
5796eaca9bbdSeschrock 	} else {
5797b1b8ab34Slling 		ret = for_each_pool(argc, argv, B_FALSE, NULL,
5798b1b8ab34Slling 		    upgrade_one, &cb);
579906eeb2adSek 	}
580006eeb2adSek 
580106eeb2adSek 	return (ret);
580206eeb2adSek }
580306eeb2adSek 
5804ecd6cf80Smarks typedef struct hist_cbdata {
5805ecd6cf80Smarks 	boolean_t first;
58064445fffbSMatthew Ahrens 	boolean_t longfmt;
58074445fffbSMatthew Ahrens 	boolean_t internal;
5808ecd6cf80Smarks } hist_cbdata_t;
5809ecd6cf80Smarks 
581006eeb2adSek /*
581106eeb2adSek  * Print out the command history for a specific pool.
581206eeb2adSek  */
581306eeb2adSek static int
581406eeb2adSek get_history_one(zpool_handle_t *zhp, void *data)
581506eeb2adSek {
581606eeb2adSek 	nvlist_t *nvhis;
581706eeb2adSek 	nvlist_t **records;
581806eeb2adSek 	uint_t numrecords;
581906eeb2adSek 	int ret, i;
5820ecd6cf80Smarks 	hist_cbdata_t *cb = (hist_cbdata_t *)data;
582106eeb2adSek 
5822ecd6cf80Smarks 	cb->first = B_FALSE;
582306eeb2adSek 
582406eeb2adSek 	(void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
582506eeb2adSek 
582606eeb2adSek 	if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
582706eeb2adSek 		return (ret);
582806eeb2adSek 
582906eeb2adSek 	verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
583006eeb2adSek 	    &records, &numrecords) == 0);
583106eeb2adSek 	for (i = 0; i < numrecords; i++) {
58324445fffbSMatthew Ahrens 		nvlist_t *rec = records[i];
58334445fffbSMatthew Ahrens 		char tbuf[30] = "";
5834ecd6cf80Smarks 
58354445fffbSMatthew Ahrens 		if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
58364445fffbSMatthew Ahrens 			time_t tsec;
58374445fffbSMatthew Ahrens 			struct tm t;
58384445fffbSMatthew Ahrens 
58394445fffbSMatthew Ahrens 			tsec = fnvlist_lookup_uint64(records[i],
58404445fffbSMatthew Ahrens 			    ZPOOL_HIST_TIME);
58414445fffbSMatthew Ahrens 			(void) localtime_r(&tsec, &t);
58424445fffbSMatthew Ahrens 			(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
58434445fffbSMatthew Ahrens 		}
5844ecd6cf80Smarks 
58454445fffbSMatthew Ahrens 		if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
58464445fffbSMatthew Ahrens 			(void) printf("%s %s", tbuf,
58474445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
58484445fffbSMatthew Ahrens 		} else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
58494445fffbSMatthew Ahrens 			int ievent =
58504445fffbSMatthew Ahrens 			    fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
58514445fffbSMatthew Ahrens 			if (!cb->internal)
58524445fffbSMatthew Ahrens 				continue;
58534445fffbSMatthew Ahrens 			if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
58544445fffbSMatthew Ahrens 				(void) printf("%s unrecognized record:\n",
58554445fffbSMatthew Ahrens 				    tbuf);
58564445fffbSMatthew Ahrens 				dump_nvlist(rec, 4);
58574445fffbSMatthew Ahrens 				continue;
58584445fffbSMatthew Ahrens 			}
58594445fffbSMatthew Ahrens 			(void) printf("%s [internal %s txg:%lld] %s", tbuf,
58604445fffbSMatthew Ahrens 			    zfs_history_event_names[ievent],
58614445fffbSMatthew Ahrens 			    fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
58624445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
58634445fffbSMatthew Ahrens 		} else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
58644445fffbSMatthew Ahrens 			if (!cb->internal)
5865ecd6cf80Smarks 				continue;
58664445fffbSMatthew Ahrens 			(void) printf("%s [txg:%lld] %s", tbuf,
58674445fffbSMatthew Ahrens 			    fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
58684445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
58694445fffbSMatthew Ahrens 			if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
58704445fffbSMatthew Ahrens 				(void) printf(" %s (%llu)",
58714445fffbSMatthew Ahrens 				    fnvlist_lookup_string(rec,
58724445fffbSMatthew Ahrens 				    ZPOOL_HIST_DSNAME),
58734445fffbSMatthew Ahrens 				    fnvlist_lookup_uint64(rec,
58744445fffbSMatthew Ahrens 				    ZPOOL_HIST_DSID));
58754445fffbSMatthew Ahrens 			}
58764445fffbSMatthew Ahrens 			(void) printf(" %s", fnvlist_lookup_string(rec,
58774445fffbSMatthew Ahrens 			    ZPOOL_HIST_INT_STR));
58784445fffbSMatthew Ahrens 		} else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
58794445fffbSMatthew Ahrens 			if (!cb->internal)
58804445fffbSMatthew Ahrens 				continue;
58814445fffbSMatthew Ahrens 			(void) printf("%s ioctl %s\n", tbuf,
58824445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
58834445fffbSMatthew Ahrens 			if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
58844445fffbSMatthew Ahrens 				(void) printf("    input:\n");
58854445fffbSMatthew Ahrens 				dump_nvlist(fnvlist_lookup_nvlist(rec,
58864445fffbSMatthew Ahrens 				    ZPOOL_HIST_INPUT_NVL), 8);
58874445fffbSMatthew Ahrens 			}
58884445fffbSMatthew Ahrens 			if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
58894445fffbSMatthew Ahrens 				(void) printf("    output:\n");
58904445fffbSMatthew Ahrens 				dump_nvlist(fnvlist_lookup_nvlist(rec,
58914445fffbSMatthew Ahrens 				    ZPOOL_HIST_OUTPUT_NVL), 8);
58924445fffbSMatthew Ahrens 			}
5893dfc11533SChris Williamson 			if (nvlist_exists(rec, ZPOOL_HIST_ERRNO)) {
5894dfc11533SChris Williamson 				(void) printf("    errno: %lld\n",
5895dfc11533SChris Williamson 				    fnvlist_lookup_int64(rec,
5896dfc11533SChris Williamson 				    ZPOOL_HIST_ERRNO));
5897dfc11533SChris Williamson 			}
58984445fffbSMatthew Ahrens 		} else {
58994445fffbSMatthew Ahrens 			if (!cb->internal)
5900ecd6cf80Smarks 				continue;
59014445fffbSMatthew Ahrens 			(void) printf("%s unrecognized record:\n", tbuf);
59024445fffbSMatthew Ahrens 			dump_nvlist(rec, 4);
590306eeb2adSek 		}
5904ecd6cf80Smarks 
5905ecd6cf80Smarks 		if (!cb->longfmt) {
5906ecd6cf80Smarks 			(void) printf("\n");
5907ecd6cf80Smarks 			continue;
5908ecd6cf80Smarks 		}
5909ecd6cf80Smarks 		(void) printf(" [");
59104445fffbSMatthew Ahrens 		if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
59114445fffbSMatthew Ahrens 			uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
59124445fffbSMatthew Ahrens 			struct passwd *pwd = getpwuid(who);
59134445fffbSMatthew Ahrens 			(void) printf("user %d ", (int)who);
59144445fffbSMatthew Ahrens 			if (pwd != NULL)
59154445fffbSMatthew Ahrens 				(void) printf("(%s) ", pwd->pw_name);
5916ecd6cf80Smarks 		}
59174445fffbSMatthew Ahrens 		if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
59184445fffbSMatthew Ahrens 			(void) printf("on %s",
59194445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
5920ecd6cf80Smarks 		}
59214445fffbSMatthew Ahrens 		if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
59224445fffbSMatthew Ahrens 			(void) printf(":%s",
59234445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
5924ecd6cf80Smarks 		}
5925ecd6cf80Smarks 		(void) printf("]");
5926ecd6cf80Smarks 		(void) printf("\n");
592706eeb2adSek 	}
592806eeb2adSek 	(void) printf("\n");
592906eeb2adSek 	nvlist_free(nvhis);
593006eeb2adSek 
593106eeb2adSek 	return (ret);
593206eeb2adSek }
593306eeb2adSek 
593406eeb2adSek /*
593506eeb2adSek  * zpool history <pool>
593606eeb2adSek  *
593706eeb2adSek  * Displays the history of commands that modified pools.
593806eeb2adSek  */
593906eeb2adSek int
594006eeb2adSek zpool_do_history(int argc, char **argv)
594106eeb2adSek {
5942ecd6cf80Smarks 	hist_cbdata_t cbdata = { 0 };
594306eeb2adSek 	int ret;
5944ecd6cf80Smarks 	int c;
594506eeb2adSek 
5946ecd6cf80Smarks 	cbdata.first = B_TRUE;
5947ecd6cf80Smarks 	/* check options */
5948ecd6cf80Smarks 	while ((c = getopt(argc, argv, "li")) != -1) {
5949ecd6cf80Smarks 		switch (c) {
5950ecd6cf80Smarks 		case 'l':
59514445fffbSMatthew Ahrens 			cbdata.longfmt = B_TRUE;
5952ecd6cf80Smarks 			break;
5953ecd6cf80Smarks 		case 'i':
59544445fffbSMatthew Ahrens 			cbdata.internal = B_TRUE;
5955ecd6cf80Smarks 			break;
5956ecd6cf80Smarks 		case '?':
5957ecd6cf80Smarks 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
5958ecd6cf80Smarks 			    optopt);
5959ecd6cf80Smarks 			usage(B_FALSE);
5960ecd6cf80Smarks 		}
5961ecd6cf80Smarks 	}
596206eeb2adSek 	argc -= optind;
596306eeb2adSek 	argv += optind;
596406eeb2adSek 
5965b1b8ab34Slling 	ret = for_each_pool(argc, argv, B_FALSE,  NULL, get_history_one,
5966ecd6cf80Smarks 	    &cbdata);
596706eeb2adSek 
5968ecd6cf80Smarks 	if (argc == 0 && cbdata.first == B_TRUE) {
596906eeb2adSek 		(void) printf(gettext("no pools available\n"));
597006eeb2adSek 		return (0);
5971eaca9bbdSeschrock 	}
5972eaca9bbdSeschrock 
5973eaca9bbdSeschrock 	return (ret);
5974eaca9bbdSeschrock }
5975eaca9bbdSeschrock 
5976b1b8ab34Slling static int
5977b1b8ab34Slling get_callback(zpool_handle_t *zhp, void *data)
5978b1b8ab34Slling {
5979990b4856Slling 	zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5980b1b8ab34Slling 	char value[MAXNAMELEN];
5981990b4856Slling 	zprop_source_t srctype;
5982990b4856Slling 	zprop_list_t *pl;
5983b1b8ab34Slling 
5984b1b8ab34Slling 	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5985b1b8ab34Slling 
5986b1b8ab34Slling 		/*
5987990b4856Slling 		 * Skip the special fake placeholder. This will also skip
5988990b4856Slling 		 * over the name property when 'all' is specified.
5989b1b8ab34Slling 		 */
5990990b4856Slling 		if (pl->pl_prop == ZPOOL_PROP_NAME &&
5991b1b8ab34Slling 		    pl == cbp->cb_proplist)
5992b1b8ab34Slling 			continue;
5993b1b8ab34Slling 
5994ad135b5dSChristopher Siden 		if (pl->pl_prop == ZPROP_INVAL &&
5995ad135b5dSChristopher Siden 		    (zpool_prop_feature(pl->pl_user_prop) ||
5996ad135b5dSChristopher Siden 		    zpool_prop_unsupported(pl->pl_user_prop))) {
5997ad135b5dSChristopher Siden 			srctype = ZPROP_SRC_LOCAL;
5998b1b8ab34Slling 
5999ad135b5dSChristopher Siden 			if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
6000ad135b5dSChristopher Siden 			    value, sizeof (value)) == 0) {
6001ad135b5dSChristopher Siden 				zprop_print_one_property(zpool_get_name(zhp),
6002ad135b5dSChristopher Siden 				    cbp, pl->pl_user_prop, value, srctype,
6003ad135b5dSChristopher Siden 				    NULL, NULL);
6004ad135b5dSChristopher Siden 			}
6005ad135b5dSChristopher Siden 		} else {
6006ad135b5dSChristopher Siden 			if (zpool_get_prop(zhp, pl->pl_prop, value,
6007c58b3526SAdam Stevko 			    sizeof (value), &srctype, cbp->cb_literal) != 0)
6008ad135b5dSChristopher Siden 				continue;
6009ad135b5dSChristopher Siden 
6010ad135b5dSChristopher Siden 			zprop_print_one_property(zpool_get_name(zhp), cbp,
6011ad135b5dSChristopher Siden 			    zpool_prop_to_name(pl->pl_prop), value, srctype,
6012ad135b5dSChristopher Siden 			    NULL, NULL);
6013ad135b5dSChristopher Siden 		}
6014b1b8ab34Slling 	}
6015b1b8ab34Slling 	return (0);
6016b1b8ab34Slling }
6017b1b8ab34Slling 
6018c58b3526SAdam Stevko /*
6019c58b3526SAdam Stevko  * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
6020c58b3526SAdam Stevko  *
6021c58b3526SAdam Stevko  *	-H	Scripted mode.  Don't display headers, and separate properties
6022c58b3526SAdam Stevko  *		by a single tab.
6023c58b3526SAdam Stevko  *	-o	List of columns to display.  Defaults to
6024c58b3526SAdam Stevko  *		"name,property,value,source".
602504e56356SAndriy Gapon  *	-p	Diplay values in parsable (exact) format.
6026c58b3526SAdam Stevko  *
6027c58b3526SAdam Stevko  * Get properties of pools in the system. Output space statistics
6028c58b3526SAdam Stevko  * for each one as well as other attributes.
6029c58b3526SAdam Stevko  */
6030b1b8ab34Slling int
6031b1b8ab34Slling zpool_do_get(int argc, char **argv)
6032b1b8ab34Slling {
6033990b4856Slling 	zprop_get_cbdata_t cb = { 0 };
6034990b4856Slling 	zprop_list_t fake_name = { 0 };
6035b1b8ab34Slling 	int ret;
6036c58b3526SAdam Stevko 	int c, i;
6037c58b3526SAdam Stevko 	char *value;
6038b1b8ab34Slling 
6039b1b8ab34Slling 	cb.cb_first = B_TRUE;
6040c58b3526SAdam Stevko 
6041c58b3526SAdam Stevko 	/*
6042c58b3526SAdam Stevko 	 * Set up default columns and sources.
6043c58b3526SAdam Stevko 	 */
6044990b4856Slling 	cb.cb_sources = ZPROP_SRC_ALL;
6045b1b8ab34Slling 	cb.cb_columns[0] = GET_COL_NAME;
6046b1b8ab34Slling 	cb.cb_columns[1] = GET_COL_PROPERTY;
6047b1b8ab34Slling 	cb.cb_columns[2] = GET_COL_VALUE;
6048b1b8ab34Slling 	cb.cb_columns[3] = GET_COL_SOURCE;
6049990b4856Slling 	cb.cb_type = ZFS_TYPE_POOL;
6050b1b8ab34Slling 
6051c58b3526SAdam Stevko 	/* check options */
6052c58b3526SAdam Stevko 	while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
6053c58b3526SAdam Stevko 		switch (c) {
6054c58b3526SAdam Stevko 		case 'p':
6055c58b3526SAdam Stevko 			cb.cb_literal = B_TRUE;
6056c58b3526SAdam Stevko 			break;
6057c58b3526SAdam Stevko 		case 'H':
6058c58b3526SAdam Stevko 			cb.cb_scripted = B_TRUE;
6059c58b3526SAdam Stevko 			break;
6060c58b3526SAdam Stevko 		case 'o':
6061c58b3526SAdam Stevko 			bzero(&cb.cb_columns, sizeof (cb.cb_columns));
6062c58b3526SAdam Stevko 			i = 0;
6063c58b3526SAdam Stevko 			while (*optarg != '\0') {
6064c58b3526SAdam Stevko 				static char *col_subopts[] =
6065c58b3526SAdam Stevko 				{ "name", "property", "value", "source",
6066c58b3526SAdam Stevko 				"all", NULL };
6067c58b3526SAdam Stevko 
6068c58b3526SAdam Stevko 				if (i == ZFS_GET_NCOLS) {
6069c58b3526SAdam Stevko 					(void) fprintf(stderr, gettext("too "
6070c58b3526SAdam Stevko 					"many fields given to -o "
6071c58b3526SAdam Stevko 					"option\n"));
6072c58b3526SAdam Stevko 					usage(B_FALSE);
6073c58b3526SAdam Stevko 				}
6074c58b3526SAdam Stevko 
6075c58b3526SAdam Stevko 				switch (getsubopt(&optarg, col_subopts,
6076c58b3526SAdam Stevko 				    &value)) {
6077c58b3526SAdam Stevko 				case 0:
6078c58b3526SAdam Stevko 					cb.cb_columns[i++] = GET_COL_NAME;
6079c58b3526SAdam Stevko 					break;
6080c58b3526SAdam Stevko 				case 1:
6081c58b3526SAdam Stevko 					cb.cb_columns[i++] = GET_COL_PROPERTY;
6082c58b3526SAdam Stevko 					break;
6083c58b3526SAdam Stevko 				case 2:
6084c58b3526SAdam Stevko 					cb.cb_columns[i++] = GET_COL_VALUE;
6085c58b3526SAdam Stevko 					break;
6086c58b3526SAdam Stevko 				case 3:
6087c58b3526SAdam Stevko 					cb.cb_columns[i++] = GET_COL_SOURCE;
6088c58b3526SAdam Stevko 					break;
6089c58b3526SAdam Stevko 				case 4:
6090c58b3526SAdam Stevko 					if (i > 0) {
6091c58b3526SAdam Stevko 						(void) fprintf(stderr,
6092c58b3526SAdam Stevko 						    gettext("\"all\" conflicts "
6093c58b3526SAdam Stevko 						    "with specific fields "
6094c58b3526SAdam Stevko 						    "given to -o option\n"));
6095c58b3526SAdam Stevko 						usage(B_FALSE);
6096c58b3526SAdam Stevko 					}
6097c58b3526SAdam Stevko 					cb.cb_columns[0] = GET_COL_NAME;
6098c58b3526SAdam Stevko 					cb.cb_columns[1] = GET_COL_PROPERTY;
6099c58b3526SAdam Stevko 					cb.cb_columns[2] = GET_COL_VALUE;
6100c58b3526SAdam Stevko 					cb.cb_columns[3] = GET_COL_SOURCE;
6101c58b3526SAdam Stevko 					i = ZFS_GET_NCOLS;
6102c58b3526SAdam Stevko 					break;
6103c58b3526SAdam Stevko 				default:
6104c58b3526SAdam Stevko 					(void) fprintf(stderr,
6105c58b3526SAdam Stevko 					    gettext("invalid column name "
6106c58b3526SAdam Stevko 					    "'%s'\n"), value);
6107c58b3526SAdam Stevko 					usage(B_FALSE);
6108c58b3526SAdam Stevko 				}
6109c58b3526SAdam Stevko 			}
6110c58b3526SAdam Stevko 			break;
6111c58b3526SAdam Stevko 		case '?':
6112c58b3526SAdam Stevko 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
6113c58b3526SAdam Stevko 			    optopt);
6114c58b3526SAdam Stevko 			usage(B_FALSE);
6115c58b3526SAdam Stevko 		}
6116c58b3526SAdam Stevko 	}
6117c58b3526SAdam Stevko 
6118c58b3526SAdam Stevko 	argc -= optind;
6119c58b3526SAdam Stevko 	argv += optind;
6120c58b3526SAdam Stevko 
6121c58b3526SAdam Stevko 	if (argc < 1) {
6122c58b3526SAdam Stevko 		(void) fprintf(stderr, gettext("missing property "
6123c58b3526SAdam Stevko 		    "argument\n"));
6124c58b3526SAdam Stevko 		usage(B_FALSE);
6125c58b3526SAdam Stevko 	}
6126c58b3526SAdam Stevko 
6127c58b3526SAdam Stevko 	if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
6128990b4856Slling 	    ZFS_TYPE_POOL) != 0)
6129b1b8ab34Slling 		usage(B_FALSE);
6130b1b8ab34Slling 
6131c58b3526SAdam Stevko 	argc--;
6132c58b3526SAdam Stevko 	argv++;
6133c58b3526SAdam Stevko 
6134b1b8ab34Slling 	if (cb.cb_proplist != NULL) {
6135990b4856Slling 		fake_name.pl_prop = ZPOOL_PROP_NAME;
6136b1b8ab34Slling 		fake_name.pl_width = strlen(gettext("NAME"));
6137b1b8ab34Slling 		fake_name.pl_next = cb.cb_proplist;
6138b1b8ab34Slling 		cb.cb_proplist = &fake_name;
6139b1b8ab34Slling 	}
6140b1b8ab34Slling 
6141c58b3526SAdam Stevko 	ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
6142b1b8ab34Slling 	    get_callback, &cb);
6143b1b8ab34Slling 
6144b1b8ab34Slling 	if (cb.cb_proplist == &fake_name)
6145990b4856Slling 		zprop_free_list(fake_name.pl_next);
6146b1b8ab34Slling 	else
6147990b4856Slling 		zprop_free_list(cb.cb_proplist);
6148b1b8ab34Slling 
6149b1b8ab34Slling 	return (ret);
6150b1b8ab34Slling }
6151b1b8ab34Slling 
6152b1b8ab34Slling typedef struct set_cbdata {
6153b1b8ab34Slling 	char *cb_propname;
6154b1b8ab34Slling 	char *cb_value;
6155b1b8ab34Slling 	boolean_t cb_any_successful;
6156b1b8ab34Slling } set_cbdata_t;
6157b1b8ab34Slling 
6158b1b8ab34Slling int
6159b1b8ab34Slling set_callback(zpool_handle_t *zhp, void *data)
6160b1b8ab34Slling {
6161b1b8ab34Slling 	int error;
6162b1b8ab34Slling 	set_cbdata_t *cb = (set_cbdata_t *)data;
6163b1b8ab34Slling 
6164b1b8ab34Slling 	error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
6165b1b8ab34Slling 
6166b1b8ab34Slling 	if (!error)
6167b1b8ab34Slling 		cb->cb_any_successful = B_TRUE;
6168b1b8ab34Slling 
6169b1b8ab34Slling 	return (error);
6170b1b8ab34Slling }
6171b1b8ab34Slling 
6172b1b8ab34Slling int
6173b1b8ab34Slling zpool_do_set(int argc, char **argv)
6174b1b8ab34Slling {
6175b1b8ab34Slling 	set_cbdata_t cb = { 0 };
6176b1b8ab34Slling 	int error;
6177b1b8ab34Slling 
6178b1b8ab34Slling 	if (argc > 1 && argv[1][0] == '-') {
6179b1b8ab34Slling 		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
6180b1b8ab34Slling 		    argv[1][1]);
6181b1b8ab34Slling 		usage(B_FALSE);
6182b1b8ab34Slling 	}
6183b1b8ab34Slling 
6184b1b8ab34Slling 	if (argc < 2) {
6185b1b8ab34Slling 		(void) fprintf(stderr, gettext("missing property=value "
6186b1b8ab34Slling 		    "argument\n"));
6187b1b8ab34Slling 		usage(B_FALSE);
6188b1b8ab34Slling 	}
6189b1b8ab34Slling 
6190b1b8ab34Slling 	if (argc < 3) {
6191b1b8ab34Slling 		(void) fprintf(stderr, gettext("missing pool name\n"));
6192b1b8ab34Slling 		usage(B_FALSE);
6193b1b8ab34Slling 	}
6194b1b8ab34Slling 
6195b1b8ab34Slling 	if (argc > 3) {
6196b1b8ab34Slling 		(void) fprintf(stderr, gettext("too many pool names\n"));
6197b1b8ab34Slling 		usage(B_FALSE);
6198b1b8ab34Slling 	}
6199b1b8ab34Slling 
6200b1b8ab34Slling 	cb.cb_propname = argv[1];
6201b1b8ab34Slling 	cb.cb_value = strchr(cb.cb_propname, '=');
6202b1b8ab34Slling 	if (cb.cb_value == NULL) {
6203b1b8ab34Slling 		(void) fprintf(stderr, gettext("missing value in "
6204b1b8ab34Slling 		    "property=value argument\n"));
6205b1b8ab34Slling 		usage(B_FALSE);
6206b1b8ab34Slling 	}
6207b1b8ab34Slling 
6208b1b8ab34Slling 	*(cb.cb_value) = '\0';
6209b1b8ab34Slling 	cb.cb_value++;
6210b1b8ab34Slling 
6211b1b8ab34Slling 	error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
6212b1b8ab34Slling 	    set_callback, &cb);
6213b1b8ab34Slling 
6214b1b8ab34Slling 	return (error);
6215b1b8ab34Slling }
6216b1b8ab34Slling 
6217b1b8ab34Slling static int
6218b1b8ab34Slling find_command_idx(char *command, int *idx)
6219b1b8ab34Slling {
6220b1b8ab34Slling 	int i;
6221b1b8ab34Slling 
6222b1b8ab34Slling 	for (i = 0; i < NCOMMAND; i++) {
6223b1b8ab34Slling 		if (command_table[i].name == NULL)
6224b1b8ab34Slling 			continue;
6225b1b8ab34Slling 
6226b1b8ab34Slling 		if (strcmp(command, command_table[i].name) == 0) {
6227b1b8ab34Slling 			*idx = i;
6228b1b8ab34Slling 			return (0);
6229b1b8ab34Slling 		}
6230b1b8ab34Slling 	}
6231b1b8ab34Slling 	return (1);
6232b1b8ab34Slling }
6233b1b8ab34Slling 
6234fa9e4066Sahrens int
6235fa9e4066Sahrens main(int argc, char **argv)
6236fa9e4066Sahrens {
6237b327cd3fSIgor Kozhukhov 	int ret = 0;
6238fa9e4066Sahrens 	int i;
6239fa9e4066Sahrens 	char *cmdname;
6240fa9e4066Sahrens 
6241fa9e4066Sahrens 	(void) setlocale(LC_ALL, "");
6242fa9e4066Sahrens 	(void) textdomain(TEXT_DOMAIN);
6243fa9e4066Sahrens 
624499653d4eSeschrock 	if ((g_zfs = libzfs_init()) == NULL) {
624599653d4eSeschrock 		(void) fprintf(stderr, gettext("internal error: failed to "
6246203a47d8Snd 		    "initialize ZFS library\n"));
624799653d4eSeschrock 		return (1);
624899653d4eSeschrock 	}
624999653d4eSeschrock 
625099653d4eSeschrock 	libzfs_print_on_error(g_zfs, B_TRUE);
625199653d4eSeschrock 
6252fa9e4066Sahrens 	opterr = 0;
6253fa9e4066Sahrens 
6254fa9e4066Sahrens 	/*
6255fa9e4066Sahrens 	 * Make sure the user has specified some command.
6256fa9e4066Sahrens 	 */
6257fa9e4066Sahrens 	if (argc < 2) {
6258fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing command\n"));
625999653d4eSeschrock 		usage(B_FALSE);
6260fa9e4066Sahrens 	}
6261fa9e4066Sahrens 
6262fa9e4066Sahrens 	cmdname = argv[1];
6263fa9e4066Sahrens 
6264fa9e4066Sahrens 	/*
6265fa9e4066Sahrens 	 * Special case '-?'
6266fa9e4066Sahrens 	 */
6267fa9e4066Sahrens 	if (strcmp(cmdname, "-?") == 0)
626899653d4eSeschrock 		usage(B_TRUE);
6269fa9e4066Sahrens 
62704445fffbSMatthew Ahrens 	zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
62712a6b87f0Sek 
6272fa9e4066Sahrens 	/*
6273fa9e4066Sahrens 	 * Run the appropriate command.
6274fa9e4066Sahrens 	 */
6275b1b8ab34Slling 	if (find_command_idx(cmdname, &i) == 0) {
6276b1b8ab34Slling 		current_command = &command_table[i];
6277b1b8ab34Slling 		ret = command_table[i].func(argc - 1, argv + 1);
627891ebeef5Sahrens 	} else if (strchr(cmdname, '=')) {
627991ebeef5Sahrens 		verify(find_command_idx("set", &i) == 0);
628091ebeef5Sahrens 		current_command = &command_table[i];
628191ebeef5Sahrens 		ret = command_table[i].func(argc, argv);
628291ebeef5Sahrens 	} else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
628391ebeef5Sahrens 		/*
628491ebeef5Sahrens 		 * 'freeze' is a vile debugging abomination, so we treat
628591ebeef5Sahrens 		 * it as such.
628691ebeef5Sahrens 		 */
6287ea8dc4b6Seschrock 		char buf[16384];
6288ea8dc4b6Seschrock 		int fd = open(ZFS_DEV, O_RDWR);
6289fa9e4066Sahrens 		(void) strcpy((void *)buf, argv[2]);
6290fa9e4066Sahrens 		return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
629191ebeef5Sahrens 	} else {
6292fa9e4066Sahrens 		(void) fprintf(stderr, gettext("unrecognized "
6293fa9e4066Sahrens 		    "command '%s'\n"), cmdname);
629499653d4eSeschrock 		usage(B_FALSE);
6295fa9e4066Sahrens 	}
6296fa9e4066Sahrens 
62974445fffbSMatthew Ahrens 	if (ret == 0 && log_history)
62984445fffbSMatthew Ahrens 		(void) zpool_log_history(g_zfs, history_str);
62994445fffbSMatthew Ahrens 
630099653d4eSeschrock 	libzfs_fini(g_zfs);
630199653d4eSeschrock 
6302fa9e4066Sahrens 	/*
6303fa9e4066Sahrens 	 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6304fa9e4066Sahrens 	 * for the purposes of running ::findleaks.
6305fa9e4066Sahrens 	 */
6306fa9e4066Sahrens 	if (getenv("ZFS_ABORT") != NULL) {
6307fa9e4066Sahrens 		(void) printf("dumping core by request\n");
6308fa9e4066Sahrens 		abort();
6309fa9e4066Sahrens 	}
6310fa9e4066Sahrens 
6311fa9e4066Sahrens 	return (ret);
6312fa9e4066Sahrens }
6313