xref: /illumos-gate/usr/src/cmd/zpool/zpool_main.c (revision 9c2acf00e275b6b2125a306f33cdddcc58393220)
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>
53fa9e4066Sahrens 
54fa9e4066Sahrens #include <libzfs.h>
55fa9e4066Sahrens 
56fa9e4066Sahrens #include "zpool_util.h"
57b7b97454Sperrin #include "zfs_comutil.h"
58ad135b5dSChristopher Siden #include "zfeature_common.h"
59fa9e4066Sahrens 
6026fd7700SKrishnendu Sadhukhan - Sun Microsystems #include "statcommon.h"
6126fd7700SKrishnendu Sadhukhan - Sun Microsystems 
62fa9e4066Sahrens static int zpool_do_create(int, char **);
63fa9e4066Sahrens static int zpool_do_destroy(int, char **);
64fa9e4066Sahrens 
65fa9e4066Sahrens static int zpool_do_add(int, char **);
6699653d4eSeschrock static int zpool_do_remove(int, char **);
676401734dSWill Andrews static int zpool_do_labelclear(int, char **);
68fa9e4066Sahrens 
6986714001SSerapheim Dimitropoulos static int zpool_do_checkpoint(int, char **);
7086714001SSerapheim Dimitropoulos 
71fa9e4066Sahrens static int zpool_do_list(int, char **);
72fa9e4066Sahrens static int zpool_do_iostat(int, char **);
73fa9e4066Sahrens static int zpool_do_status(int, char **);
74fa9e4066Sahrens 
75fa9e4066Sahrens static int zpool_do_online(int, char **);
76fa9e4066Sahrens static int zpool_do_offline(int, char **);
77ea8dc4b6Seschrock static int zpool_do_clear(int, char **);
784263d13fSGeorge Wilson static int zpool_do_reopen(int, char **);
79fa9e4066Sahrens 
80e9103aaeSGarrett D'Amore static int zpool_do_reguid(int, char **);
81e9103aaeSGarrett D'Amore 
82fa9e4066Sahrens static int zpool_do_attach(int, char **);
83fa9e4066Sahrens static int zpool_do_detach(int, char **);
84fa9e4066Sahrens static int zpool_do_replace(int, char **);
851195e687SMark J Musante static int zpool_do_split(int, char **);
86fa9e4066Sahrens 
87094e47e9SGeorge Wilson static int zpool_do_initialize(int, char **);
88fa9e4066Sahrens static int zpool_do_scrub(int, char **);
89fa9e4066Sahrens 
90fa9e4066Sahrens static int zpool_do_import(int, char **);
91fa9e4066Sahrens static int zpool_do_export(int, char **);
92fa9e4066Sahrens 
93eaca9bbdSeschrock static int zpool_do_upgrade(int, char **);
94eaca9bbdSeschrock 
9506eeb2adSek static int zpool_do_history(int, char **);
9606eeb2adSek 
97b1b8ab34Slling static int zpool_do_get(int, char **);
98b1b8ab34Slling static int zpool_do_set(int, char **);
99b1b8ab34Slling 
100*9c2acf00SAlek Pinchuk static int zpool_do_sync(int, char **);
101*9c2acf00SAlek Pinchuk 
102fa9e4066Sahrens /*
103fa9e4066Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
104fa9e4066Sahrens  * debugging facilities.
105fa9e4066Sahrens  */
10629ab75c9Srm 
10729ab75c9Srm #ifdef DEBUG
108fa9e4066Sahrens const char *
10999653d4eSeschrock _umem_debug_init(void)
110fa9e4066Sahrens {
111fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
112fa9e4066Sahrens }
113fa9e4066Sahrens 
114fa9e4066Sahrens const char *
115fa9e4066Sahrens _umem_logging_init(void)
116fa9e4066Sahrens {
117fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
118fa9e4066Sahrens }
11929ab75c9Srm #endif
120fa9e4066Sahrens 
12165cd9f28Seschrock typedef enum {
12265cd9f28Seschrock 	HELP_ADD,
12365cd9f28Seschrock 	HELP_ATTACH,
124ea8dc4b6Seschrock 	HELP_CLEAR,
12565cd9f28Seschrock 	HELP_CREATE,
12686714001SSerapheim Dimitropoulos 	HELP_CHECKPOINT,
12765cd9f28Seschrock 	HELP_DESTROY,
12865cd9f28Seschrock 	HELP_DETACH,
12965cd9f28Seschrock 	HELP_EXPORT,
13006eeb2adSek 	HELP_HISTORY,
13165cd9f28Seschrock 	HELP_IMPORT,
13265cd9f28Seschrock 	HELP_IOSTAT,
1336401734dSWill Andrews 	HELP_LABELCLEAR,
13465cd9f28Seschrock 	HELP_LIST,
13565cd9f28Seschrock 	HELP_OFFLINE,
13665cd9f28Seschrock 	HELP_ONLINE,
13765cd9f28Seschrock 	HELP_REPLACE,
13899653d4eSeschrock 	HELP_REMOVE,
139094e47e9SGeorge Wilson 	HELP_INITIALIZE,
14065cd9f28Seschrock 	HELP_SCRUB,
141eaca9bbdSeschrock 	HELP_STATUS,
142b1b8ab34Slling 	HELP_UPGRADE,
143b1b8ab34Slling 	HELP_GET,
1441195e687SMark J Musante 	HELP_SET,
145e9103aaeSGarrett D'Amore 	HELP_SPLIT,
146*9c2acf00SAlek Pinchuk 	HELP_SYNC,
1474263d13fSGeorge Wilson 	HELP_REGUID,
1484263d13fSGeorge Wilson 	HELP_REOPEN
14965cd9f28Seschrock } zpool_help_t;
15065cd9f28Seschrock 
15165cd9f28Seschrock 
152fa9e4066Sahrens typedef struct zpool_command {
153fa9e4066Sahrens 	const char	*name;
154fa9e4066Sahrens 	int		(*func)(int, char **);
15565cd9f28Seschrock 	zpool_help_t	usage;
156fa9e4066Sahrens } zpool_command_t;
157fa9e4066Sahrens 
158fa9e4066Sahrens /*
159fa9e4066Sahrens  * Master command table.  Each ZFS command has a name, associated function, and
160ea8dc4b6Seschrock  * usage message.  The usage messages need to be internationalized, so we have
161ea8dc4b6Seschrock  * to have a function to return the usage message based on a command index.
16265cd9f28Seschrock  *
16365cd9f28Seschrock  * These commands are organized according to how they are displayed in the usage
16465cd9f28Seschrock  * message.  An empty command (one with a NULL name) indicates an empty line in
16565cd9f28Seschrock  * the generic usage message.
166fa9e4066Sahrens  */
167fa9e4066Sahrens static zpool_command_t command_table[] = {
16865cd9f28Seschrock 	{ "create",	zpool_do_create,	HELP_CREATE		},
16965cd9f28Seschrock 	{ "destroy",	zpool_do_destroy,	HELP_DESTROY		},
170fa9e4066Sahrens 	{ NULL },
17165cd9f28Seschrock 	{ "add",	zpool_do_add,		HELP_ADD		},
17299653d4eSeschrock 	{ "remove",	zpool_do_remove,	HELP_REMOVE		},
173fa9e4066Sahrens 	{ NULL },
1746401734dSWill Andrews 	{ "labelclear",	zpool_do_labelclear,	HELP_LABELCLEAR		},
1756401734dSWill Andrews 	{ NULL },
17686714001SSerapheim Dimitropoulos 	{ "checkpoint",	zpool_do_checkpoint,	HELP_CHECKPOINT		},
17786714001SSerapheim Dimitropoulos 	{ NULL },
17865cd9f28Seschrock 	{ "list",	zpool_do_list,		HELP_LIST		},
17965cd9f28Seschrock 	{ "iostat",	zpool_do_iostat,	HELP_IOSTAT		},
18065cd9f28Seschrock 	{ "status",	zpool_do_status,	HELP_STATUS		},
181fa9e4066Sahrens 	{ NULL },
18265cd9f28Seschrock 	{ "online",	zpool_do_online,	HELP_ONLINE		},
18365cd9f28Seschrock 	{ "offline",	zpool_do_offline,	HELP_OFFLINE		},
184ea8dc4b6Seschrock 	{ "clear",	zpool_do_clear,		HELP_CLEAR		},
1854263d13fSGeorge Wilson 	{ "reopen",	zpool_do_reopen,	HELP_REOPEN		},
186fa9e4066Sahrens 	{ NULL },
18765cd9f28Seschrock 	{ "attach",	zpool_do_attach,	HELP_ATTACH		},
18865cd9f28Seschrock 	{ "detach",	zpool_do_detach,	HELP_DETACH		},
18965cd9f28Seschrock 	{ "replace",	zpool_do_replace,	HELP_REPLACE		},
1901195e687SMark J Musante 	{ "split",	zpool_do_split,		HELP_SPLIT		},
191fa9e4066Sahrens 	{ NULL },
192094e47e9SGeorge Wilson 	{ "initialize",	zpool_do_initialize,	HELP_INITIALIZE		},
19365cd9f28Seschrock 	{ "scrub",	zpool_do_scrub,		HELP_SCRUB		},
194fa9e4066Sahrens 	{ NULL },
19565cd9f28Seschrock 	{ "import",	zpool_do_import,	HELP_IMPORT		},
19665cd9f28Seschrock 	{ "export",	zpool_do_export,	HELP_EXPORT		},
19706eeb2adSek 	{ "upgrade",	zpool_do_upgrade,	HELP_UPGRADE		},
198e9103aaeSGarrett D'Amore 	{ "reguid",	zpool_do_reguid,	HELP_REGUID		},
19906eeb2adSek 	{ NULL },
200b1b8ab34Slling 	{ "history",	zpool_do_history,	HELP_HISTORY		},
201b1b8ab34Slling 	{ "get",	zpool_do_get,		HELP_GET		},
202b1b8ab34Slling 	{ "set",	zpool_do_set,		HELP_SET		},
203*9c2acf00SAlek Pinchuk 	{ "sync",	zpool_do_sync,		HELP_SYNC		},
204fa9e4066Sahrens };
205fa9e4066Sahrens 
206fa9e4066Sahrens #define	NCOMMAND	(sizeof (command_table) / sizeof (command_table[0]))
207fa9e4066Sahrens 
2084445fffbSMatthew Ahrens static zpool_command_t *current_command;
2092a6b87f0Sek static char history_str[HIS_MAX_RECORD_LEN];
2104445fffbSMatthew Ahrens static boolean_t log_history = B_TRUE;
21126fd7700SKrishnendu Sadhukhan - Sun Microsystems static uint_t timestamp_fmt = NODATE;
21226fd7700SKrishnendu Sadhukhan - Sun Microsystems 
21365cd9f28Seschrock static const char *
2149a686fbcSPaul Dagnelie get_usage(zpool_help_t idx)
2159a686fbcSPaul Dagnelie {
21665cd9f28Seschrock 	switch (idx) {
21765cd9f28Seschrock 	case HELP_ADD:
21865cd9f28Seschrock 		return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
21965cd9f28Seschrock 	case HELP_ATTACH:
22065cd9f28Seschrock 		return (gettext("\tattach [-f] <pool> <device> "
221e45ce728Sahrens 		    "<new-device>\n"));
222ea8dc4b6Seschrock 	case HELP_CLEAR:
223468c413aSTim Haley 		return (gettext("\tclear [-nF] <pool> [device]\n"));
22465cd9f28Seschrock 	case HELP_CREATE:
2257855d95bSToomas Soome 		return (gettext("\tcreate [-fnd] [-B] "
2267855d95bSToomas Soome 		    "[-o property=value] ... \n"
22704e56356SAndriy Gapon 		    "\t    [-O file-system-property=value] ...\n"
22804e56356SAndriy Gapon 		    "\t    [-m mountpoint] [-R root] [-t tempname] "
22904e56356SAndriy Gapon 		    "<pool> <vdev> ...\n"));
23086714001SSerapheim Dimitropoulos 	case HELP_CHECKPOINT:
23186714001SSerapheim Dimitropoulos 		return (gettext("\tcheckpoint [--discard] <pool> ...\n"));
23265cd9f28Seschrock 	case HELP_DESTROY:
23365cd9f28Seschrock 		return (gettext("\tdestroy [-f] <pool>\n"));
23465cd9f28Seschrock 	case HELP_DETACH:
23565cd9f28Seschrock 		return (gettext("\tdetach <pool> <device>\n"));
23665cd9f28Seschrock 	case HELP_EXPORT:
23765cd9f28Seschrock 		return (gettext("\texport [-f] <pool> ...\n"));
23806eeb2adSek 	case HELP_HISTORY:
239ecd6cf80Smarks 		return (gettext("\thistory [-il] [<pool>] ...\n"));
24065cd9f28Seschrock 	case HELP_IMPORT:
2414c58d714Sdarrenm 		return (gettext("\timport [-d dir] [-D]\n"
2422f8aaab3Seschrock 		    "\timport [-o mntopts] [-o property=value] ... \n"
243f9af39baSGeorge Wilson 		    "\t    [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
244f9af39baSGeorge Wilson 		    "[-R root] [-F [-n]] -a\n"
2452f8aaab3Seschrock 		    "\timport [-o mntopts] [-o property=value] ... \n"
246f9af39baSGeorge Wilson 		    "\t    [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
24704e56356SAndriy Gapon 		    "[-R root] [-F [-n]] [-t]\n"
24886714001SSerapheim Dimitropoulos 		    "\t    [--rewind-to-checkpoint] <pool | id> [newpool]\n"));
24965cd9f28Seschrock 	case HELP_IOSTAT:
25026fd7700SKrishnendu Sadhukhan - Sun Microsystems 		return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
25165cd9f28Seschrock 		    "[count]]\n"));
2526401734dSWill Andrews 	case HELP_LABELCLEAR:
2536401734dSWill Andrews 		return (gettext("\tlabelclear [-f] <vdev>\n"));
25465cd9f28Seschrock 	case HELP_LIST:
255c58b3526SAdam Stevko 		return (gettext("\tlist [-Hp] [-o property[,...]] "
2563f9d6ad7SLin Ling 		    "[-T d|u] [pool] ... [interval [count]]\n"));
25765cd9f28Seschrock 	case HELP_OFFLINE:
258441d80aaSlling 		return (gettext("\toffline [-t] <pool> <device> ...\n"));
25965cd9f28Seschrock 	case HELP_ONLINE:
260441d80aaSlling 		return (gettext("\tonline <pool> <device> ...\n"));
26165cd9f28Seschrock 	case HELP_REPLACE:
26265cd9f28Seschrock 		return (gettext("\treplace [-f] <pool> <device> "
263e45ce728Sahrens 		    "[new-device]\n"));
26499653d4eSeschrock 	case HELP_REMOVE:
2655cabbc6bSPrashanth Sreenivasa 		return (gettext("\tremove [-nps] <pool> <device> ...\n"));
2664263d13fSGeorge Wilson 	case HELP_REOPEN:
26731d7e8faSGeorge Wilson 		return (gettext("\treopen <pool>\n"));
268094e47e9SGeorge Wilson 	case HELP_INITIALIZE:
269094e47e9SGeorge Wilson 		return (gettext("\tinitialize [-cs] <pool> [<device> ...]\n"));
27065cd9f28Seschrock 	case HELP_SCRUB:
2711702cce7SAlek Pinchuk 		return (gettext("\tscrub [-s | -p] <pool> ...\n"));
27265cd9f28Seschrock 	case HELP_STATUS:
2733f9d6ad7SLin Ling 		return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
2743f9d6ad7SLin Ling 		    "[count]]\n"));
275eaca9bbdSeschrock 	case HELP_UPGRADE:
276eaca9bbdSeschrock 		return (gettext("\tupgrade\n"
277eaca9bbdSeschrock 		    "\tupgrade -v\n"
278990b4856Slling 		    "\tupgrade [-V version] <-a | pool ...>\n"));
279b1b8ab34Slling 	case HELP_GET:
280c58b3526SAdam Stevko 		return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
281c58b3526SAdam Stevko 		    "<\"all\" | property[,...]> <pool> ...\n"));
282b1b8ab34Slling 	case HELP_SET:
283b1b8ab34Slling 		return (gettext("\tset <property=value> <pool> \n"));
2841195e687SMark J Musante 	case HELP_SPLIT:
2851195e687SMark J Musante 		return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
2861195e687SMark J Musante 		    "\t    [-o property=value] <pool> <newpool> "
2871195e687SMark J Musante 		    "[<device> ...]\n"));
288e9103aaeSGarrett D'Amore 	case HELP_REGUID:
289e9103aaeSGarrett D'Amore 		return (gettext("\treguid <pool>\n"));
290*9c2acf00SAlek Pinchuk 	case HELP_SYNC:
291*9c2acf00SAlek Pinchuk 		return (gettext("\tsync [pool] ...\n"));
29265cd9f28Seschrock 	}
29365cd9f28Seschrock 
29465cd9f28Seschrock 	abort();
29565cd9f28Seschrock 	/* NOTREACHED */
29665cd9f28Seschrock }
29765cd9f28Seschrock 
298fa9e4066Sahrens 
299b1b8ab34Slling /*
300b1b8ab34Slling  * Callback routine that will print out a pool property value.
301b1b8ab34Slling  */
302990b4856Slling static int
303990b4856Slling print_prop_cb(int prop, void *cb)
304b1b8ab34Slling {
305b1b8ab34Slling 	FILE *fp = cb;
306b1b8ab34Slling 
307b24ab676SJeff Bonwick 	(void) fprintf(fp, "\t%-15s  ", zpool_prop_to_name(prop));
308b1b8ab34Slling 
309990b4856Slling 	if (zpool_prop_readonly(prop))
310990b4856Slling 		(void) fprintf(fp, "  NO   ");
311990b4856Slling 	else
312b24ab676SJeff Bonwick 		(void) fprintf(fp, " YES   ");
313990b4856Slling 
314b1b8ab34Slling 	if (zpool_prop_values(prop) == NULL)
315b1b8ab34Slling 		(void) fprintf(fp, "-\n");
316b1b8ab34Slling 	else
317b1b8ab34Slling 		(void) fprintf(fp, "%s\n", zpool_prop_values(prop));
318b1b8ab34Slling 
319990b4856Slling 	return (ZPROP_CONT);
320b1b8ab34Slling }
321b1b8ab34Slling 
322fa9e4066Sahrens /*
323fa9e4066Sahrens  * Display usage message.  If we're inside a command, display only the usage for
324fa9e4066Sahrens  * that command.  Otherwise, iterate over the entire command table and display
325fa9e4066Sahrens  * a complete usage message.
326fa9e4066Sahrens  */
327fa9e4066Sahrens void
32899653d4eSeschrock usage(boolean_t requested)
329fa9e4066Sahrens {
330fa9e4066Sahrens 	FILE *fp = requested ? stdout : stderr;
331fa9e4066Sahrens 
332fa9e4066Sahrens 	if (current_command == NULL) {
333fa9e4066Sahrens 		int i;
334fa9e4066Sahrens 
335fa9e4066Sahrens 		(void) fprintf(fp, gettext("usage: zpool command args ...\n"));
336fa9e4066Sahrens 		(void) fprintf(fp,
337fa9e4066Sahrens 		    gettext("where 'command' is one of the following:\n\n"));
338fa9e4066Sahrens 
339fa9e4066Sahrens 		for (i = 0; i < NCOMMAND; i++) {
340fa9e4066Sahrens 			if (command_table[i].name == NULL)
341fa9e4066Sahrens 				(void) fprintf(fp, "\n");
342fa9e4066Sahrens 			else
343fa9e4066Sahrens 				(void) fprintf(fp, "%s",
34465cd9f28Seschrock 				    get_usage(command_table[i].usage));
345fa9e4066Sahrens 		}
346fa9e4066Sahrens 	} else {
347fa9e4066Sahrens 		(void) fprintf(fp, gettext("usage:\n"));
34865cd9f28Seschrock 		(void) fprintf(fp, "%s", get_usage(current_command->usage));
349fa9e4066Sahrens 	}
350fa9e4066Sahrens 
351b1b8ab34Slling 	if (current_command != NULL &&
352b1b8ab34Slling 	    ((strcmp(current_command->name, "set") == 0) ||
353990b4856Slling 	    (strcmp(current_command->name, "get") == 0) ||
354990b4856Slling 	    (strcmp(current_command->name, "list") == 0))) {
355b1b8ab34Slling 
356b1b8ab34Slling 		(void) fprintf(fp,
357b1b8ab34Slling 		    gettext("\nthe following properties are supported:\n"));
358b1b8ab34Slling 
359b24ab676SJeff Bonwick 		(void) fprintf(fp, "\n\t%-15s  %s   %s\n\n",
360990b4856Slling 		    "PROPERTY", "EDIT", "VALUES");
361b1b8ab34Slling 
362b1b8ab34Slling 		/* Iterate over all properties */
363990b4856Slling 		(void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
364990b4856Slling 		    ZFS_TYPE_POOL);
365ad135b5dSChristopher Siden 
366ad135b5dSChristopher Siden 		(void) fprintf(fp, "\t%-15s   ", "feature@...");
367ad135b5dSChristopher Siden 		(void) fprintf(fp, "YES   disabled | enabled | active\n");
368ad135b5dSChristopher Siden 
369ad135b5dSChristopher Siden 		(void) fprintf(fp, gettext("\nThe feature@ properties must be "
370ad135b5dSChristopher Siden 		    "appended with a feature name.\nSee zpool-features(5).\n"));
371b1b8ab34Slling 	}
372b1b8ab34Slling 
373e9dbad6fSeschrock 	/*
374e9dbad6fSeschrock 	 * See comments at end of main().
375e9dbad6fSeschrock 	 */
376e9dbad6fSeschrock 	if (getenv("ZFS_ABORT") != NULL) {
377e9dbad6fSeschrock 		(void) printf("dumping core by request\n");
378e9dbad6fSeschrock 		abort();
379e9dbad6fSeschrock 	}
380e9dbad6fSeschrock 
381fa9e4066Sahrens 	exit(requested ? 0 : 2);
382fa9e4066Sahrens }
383fa9e4066Sahrens 
384fa9e4066Sahrens void
3858654d025Sperrin print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
3868654d025Sperrin     boolean_t print_logs)
387fa9e4066Sahrens {
388fa9e4066Sahrens 	nvlist_t **child;
389fa9e4066Sahrens 	uint_t c, children;
390afefbcddSeschrock 	char *vname;
391fa9e4066Sahrens 
392fa9e4066Sahrens 	if (name != NULL)
393fa9e4066Sahrens 		(void) printf("\t%*s%s\n", indent, "", name);
394fa9e4066Sahrens 
395fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
396fa9e4066Sahrens 	    &child, &children) != 0)
397fa9e4066Sahrens 		return;
398fa9e4066Sahrens 
399afefbcddSeschrock 	for (c = 0; c < children; c++) {
4008654d025Sperrin 		uint64_t is_log = B_FALSE;
4018654d025Sperrin 
4028654d025Sperrin 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
4038654d025Sperrin 		    &is_log);
4048654d025Sperrin 		if ((is_log && !print_logs) || (!is_log && print_logs))
4058654d025Sperrin 			continue;
4068654d025Sperrin 
40788ecc943SGeorge Wilson 		vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
4088654d025Sperrin 		print_vdev_tree(zhp, vname, child[c], indent + 2,
4098654d025Sperrin 		    B_FALSE);
410afefbcddSeschrock 		free(vname);
411afefbcddSeschrock 	}
412fa9e4066Sahrens }
413fa9e4066Sahrens 
41457221772SChristopher Siden static boolean_t
41557221772SChristopher Siden prop_list_contains_feature(nvlist_t *proplist)
41657221772SChristopher Siden {
41757221772SChristopher Siden 	nvpair_t *nvp;
41857221772SChristopher Siden 	for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
41957221772SChristopher Siden 	    nvp = nvlist_next_nvpair(proplist, nvp)) {
42057221772SChristopher Siden 		if (zpool_prop_feature(nvpair_name(nvp)))
42157221772SChristopher Siden 			return (B_TRUE);
42257221772SChristopher Siden 	}
42357221772SChristopher Siden 	return (B_FALSE);
42457221772SChristopher Siden }
42557221772SChristopher Siden 
426990b4856Slling /*
427990b4856Slling  * Add a property pair (name, string-value) into a property nvlist.
428990b4856Slling  */
429990b4856Slling static int
4300a48a24eStimh add_prop_list(const char *propname, char *propval, nvlist_t **props,
4310a48a24eStimh     boolean_t poolprop)
432990b4856Slling {
433d2aa06e8SAndrew Stormont 	zpool_prop_t prop = ZPOOL_PROP_INVAL;
4340a48a24eStimh 	zfs_prop_t fprop;
435990b4856Slling 	nvlist_t *proplist;
4360a48a24eStimh 	const char *normnm;
4370a48a24eStimh 	char *strval;
438990b4856Slling 
439990b4856Slling 	if (*props == NULL &&
440990b4856Slling 	    nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
441990b4856Slling 		(void) fprintf(stderr,
442990b4856Slling 		    gettext("internal error: out of memory\n"));
443990b4856Slling 		return (1);
444990b4856Slling 	}
445990b4856Slling 
446990b4856Slling 	proplist = *props;
447990b4856Slling 
4480a48a24eStimh 	if (poolprop) {
44957221772SChristopher Siden 		const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
45057221772SChristopher Siden 
451d2aa06e8SAndrew Stormont 		if ((prop = zpool_name_to_prop(propname)) == ZPOOL_PROP_INVAL &&
452ad135b5dSChristopher Siden 		    !zpool_prop_feature(propname)) {
4530a48a24eStimh 			(void) fprintf(stderr, gettext("property '%s' is "
4540a48a24eStimh 			    "not a valid pool property\n"), propname);
4550a48a24eStimh 			return (2);
4560a48a24eStimh 		}
45757221772SChristopher Siden 
45857221772SChristopher Siden 		/*
45957221772SChristopher Siden 		 * feature@ properties and version should not be specified
46057221772SChristopher Siden 		 * at the same time.
46157221772SChristopher Siden 		 */
4624ae5f5f0SAlan Somers 		if ((prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname) &&
46357221772SChristopher Siden 		    nvlist_exists(proplist, vname)) ||
46457221772SChristopher Siden 		    (prop == ZPOOL_PROP_VERSION &&
46557221772SChristopher Siden 		    prop_list_contains_feature(proplist))) {
46657221772SChristopher Siden 			(void) fprintf(stderr, gettext("'feature@' and "
46757221772SChristopher Siden 			    "'version' properties cannot be specified "
46857221772SChristopher Siden 			    "together\n"));
46957221772SChristopher Siden 			return (2);
47057221772SChristopher Siden 		}
47157221772SChristopher Siden 
47257221772SChristopher Siden 
473ad135b5dSChristopher Siden 		if (zpool_prop_feature(propname))
474ad135b5dSChristopher Siden 			normnm = propname;
475ad135b5dSChristopher Siden 		else
476ad135b5dSChristopher Siden 			normnm = zpool_prop_to_name(prop);
4770a48a24eStimh 	} else {
47814843421SMatthew Ahrens 		if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
47914843421SMatthew Ahrens 			normnm = zfs_prop_to_name(fprop);
48014843421SMatthew Ahrens 		} else {
48114843421SMatthew Ahrens 			normnm = propname;
4820a48a24eStimh 		}
483990b4856Slling 	}
484990b4856Slling 
4850a48a24eStimh 	if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
4860a48a24eStimh 	    prop != ZPOOL_PROP_CACHEFILE) {
487990b4856Slling 		(void) fprintf(stderr, gettext("property '%s' "
488990b4856Slling 		    "specified multiple times\n"), propname);
489990b4856Slling 		return (2);
490990b4856Slling 	}
491990b4856Slling 
4920a48a24eStimh 	if (nvlist_add_string(proplist, normnm, propval) != 0) {
493990b4856Slling 		(void) fprintf(stderr, gettext("internal "
494990b4856Slling 		    "error: out of memory\n"));
495990b4856Slling 		return (1);
496990b4856Slling 	}
497990b4856Slling 
498990b4856Slling 	return (0);
499990b4856Slling }
500990b4856Slling 
50104e56356SAndriy Gapon /*
50204e56356SAndriy Gapon  * Set a default property pair (name, string-value) in a property nvlist
50304e56356SAndriy Gapon  */
50404e56356SAndriy Gapon static int
50504e56356SAndriy Gapon add_prop_list_default(const char *propname, char *propval, nvlist_t **props,
50604e56356SAndriy Gapon     boolean_t poolprop)
50704e56356SAndriy Gapon {
50804e56356SAndriy Gapon 	char *pval;
50904e56356SAndriy Gapon 
51004e56356SAndriy Gapon 	if (nvlist_lookup_string(*props, propname, &pval) == 0)
51104e56356SAndriy Gapon 		return (0);
51204e56356SAndriy Gapon 
51304e56356SAndriy Gapon 	return (add_prop_list(propname, propval, props, poolprop));
51404e56356SAndriy Gapon }
51504e56356SAndriy Gapon 
516fa9e4066Sahrens /*
517fa9e4066Sahrens  * zpool add [-fn] <pool> <vdev> ...
518fa9e4066Sahrens  *
519fa9e4066Sahrens  *	-f	Force addition of devices, even if they appear in use
520fa9e4066Sahrens  *	-n	Do not add the devices, but display the resulting layout if
521fa9e4066Sahrens  *		they were to be added.
522fa9e4066Sahrens  *
523fa9e4066Sahrens  * Adds the given vdevs to 'pool'.  As with create, the bulk of this work is
524fa9e4066Sahrens  * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
525fa9e4066Sahrens  * libzfs.
526fa9e4066Sahrens  */
527fa9e4066Sahrens int
528fa9e4066Sahrens zpool_do_add(int argc, char **argv)
529fa9e4066Sahrens {
53099653d4eSeschrock 	boolean_t force = B_FALSE;
53199653d4eSeschrock 	boolean_t dryrun = B_FALSE;
532fa9e4066Sahrens 	int c;
533fa9e4066Sahrens 	nvlist_t *nvroot;
534fa9e4066Sahrens 	char *poolname;
5357855d95bSToomas Soome 	zpool_boot_label_t boot_type;
5367855d95bSToomas Soome 	uint64_t boot_size;
537fa9e4066Sahrens 	int ret;
538fa9e4066Sahrens 	zpool_handle_t *zhp;
539fa9e4066Sahrens 	nvlist_t *config;
540fa9e4066Sahrens 
541fa9e4066Sahrens 	/* check options */
542fa9e4066Sahrens 	while ((c = getopt(argc, argv, "fn")) != -1) {
543fa9e4066Sahrens 		switch (c) {
544fa9e4066Sahrens 		case 'f':
54599653d4eSeschrock 			force = B_TRUE;
546fa9e4066Sahrens 			break;
547fa9e4066Sahrens 		case 'n':
54899653d4eSeschrock 			dryrun = B_TRUE;
549fa9e4066Sahrens 			break;
550fa9e4066Sahrens 		case '?':
551fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
552fa9e4066Sahrens 			    optopt);
55399653d4eSeschrock 			usage(B_FALSE);
554fa9e4066Sahrens 		}
555fa9e4066Sahrens 	}
556fa9e4066Sahrens 
557fa9e4066Sahrens 	argc -= optind;
558fa9e4066Sahrens 	argv += optind;
559fa9e4066Sahrens 
560fa9e4066Sahrens 	/* get pool name and check number of arguments */
561fa9e4066Sahrens 	if (argc < 1) {
562fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
56399653d4eSeschrock 		usage(B_FALSE);
564fa9e4066Sahrens 	}
565fa9e4066Sahrens 	if (argc < 2) {
566fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing vdev specification\n"));
56799653d4eSeschrock 		usage(B_FALSE);
568fa9e4066Sahrens 	}
569fa9e4066Sahrens 
570fa9e4066Sahrens 	poolname = argv[0];
571fa9e4066Sahrens 
572fa9e4066Sahrens 	argc--;
573fa9e4066Sahrens 	argv++;
574fa9e4066Sahrens 
57599653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
576fa9e4066Sahrens 		return (1);
577fa9e4066Sahrens 
578088e9d47Seschrock 	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
579fa9e4066Sahrens 		(void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
580fa9e4066Sahrens 		    poolname);
581fa9e4066Sahrens 		zpool_close(zhp);
582fa9e4066Sahrens 		return (1);
583fa9e4066Sahrens 	}
584fa9e4066Sahrens 
5857855d95bSToomas Soome 	if (zpool_is_bootable(zhp))
5867855d95bSToomas Soome 		boot_type = ZPOOL_COPY_BOOT_LABEL;
5877855d95bSToomas Soome 	else
5887855d95bSToomas Soome 		boot_type = ZPOOL_NO_BOOT_LABEL;
5897855d95bSToomas Soome 
590fa9e4066Sahrens 	/* pass off to get_vdev_spec for processing */
5917855d95bSToomas Soome 	boot_size = zpool_get_prop_int(zhp, ZPOOL_PROP_BOOTSIZE, NULL);
592705040edSEric Taylor 	nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
5937855d95bSToomas Soome 	    boot_type, boot_size, argc, argv);
594fa9e4066Sahrens 	if (nvroot == NULL) {
595fa9e4066Sahrens 		zpool_close(zhp);
596fa9e4066Sahrens 		return (1);
597fa9e4066Sahrens 	}
598fa9e4066Sahrens 
599fa9e4066Sahrens 	if (dryrun) {
600fa9e4066Sahrens 		nvlist_t *poolnvroot;
601fa9e4066Sahrens 
602fa9e4066Sahrens 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
603fa9e4066Sahrens 		    &poolnvroot) == 0);
604fa9e4066Sahrens 
605fa9e4066Sahrens 		(void) printf(gettext("would update '%s' to the following "
606fa9e4066Sahrens 		    "configuration:\n"), zpool_get_name(zhp));
607fa9e4066Sahrens 
6088654d025Sperrin 		/* print original main pool and new tree */
6098654d025Sperrin 		print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
6108654d025Sperrin 		print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
6118654d025Sperrin 
6128654d025Sperrin 		/* Do the same for the logs */
6138654d025Sperrin 		if (num_logs(poolnvroot) > 0) {
6148654d025Sperrin 			print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
6158654d025Sperrin 			print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
6168654d025Sperrin 		} else if (num_logs(nvroot) > 0) {
6178654d025Sperrin 			print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
6188654d025Sperrin 		}
619fa9e4066Sahrens 
620fa9e4066Sahrens 		ret = 0;
621fa9e4066Sahrens 	} else {
622fa9e4066Sahrens 		ret = (zpool_add(zhp, nvroot) != 0);
623fa9e4066Sahrens 	}
624fa9e4066Sahrens 
62599653d4eSeschrock 	nvlist_free(nvroot);
62699653d4eSeschrock 	zpool_close(zhp);
62799653d4eSeschrock 
62899653d4eSeschrock 	return (ret);
62999653d4eSeschrock }
63099653d4eSeschrock 
63199653d4eSeschrock /*
6323f9d6ad7SLin Ling  * zpool remove  <pool> <vdev> ...
63399653d4eSeschrock  *
6345cabbc6bSPrashanth Sreenivasa  * Removes the given vdev from the pool.
63599653d4eSeschrock  */
63699653d4eSeschrock int
63799653d4eSeschrock zpool_do_remove(int argc, char **argv)
63899653d4eSeschrock {
63999653d4eSeschrock 	char *poolname;
640fa94a07fSbrendan 	int i, ret = 0;
64199653d4eSeschrock 	zpool_handle_t *zhp;
6425cabbc6bSPrashanth Sreenivasa 	boolean_t stop = B_FALSE;
6435cabbc6bSPrashanth Sreenivasa 	boolean_t noop = B_FALSE;
6445cabbc6bSPrashanth Sreenivasa 	boolean_t parsable = B_FALSE;
6455cabbc6bSPrashanth Sreenivasa 	char c;
64699653d4eSeschrock 
6475cabbc6bSPrashanth Sreenivasa 	/* check options */
6485cabbc6bSPrashanth Sreenivasa 	while ((c = getopt(argc, argv, "nps")) != -1) {
6495cabbc6bSPrashanth Sreenivasa 		switch (c) {
6505cabbc6bSPrashanth Sreenivasa 		case 'n':
6515cabbc6bSPrashanth Sreenivasa 			noop = B_TRUE;
6525cabbc6bSPrashanth Sreenivasa 			break;
6535cabbc6bSPrashanth Sreenivasa 		case 'p':
6545cabbc6bSPrashanth Sreenivasa 			parsable = B_TRUE;
6555cabbc6bSPrashanth Sreenivasa 			break;
6565cabbc6bSPrashanth Sreenivasa 		case 's':
6575cabbc6bSPrashanth Sreenivasa 			stop = B_TRUE;
6585cabbc6bSPrashanth Sreenivasa 			break;
6595cabbc6bSPrashanth Sreenivasa 		case '?':
6605cabbc6bSPrashanth Sreenivasa 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
6615cabbc6bSPrashanth Sreenivasa 			    optopt);
6625cabbc6bSPrashanth Sreenivasa 			usage(B_FALSE);
6635cabbc6bSPrashanth Sreenivasa 		}
6645cabbc6bSPrashanth Sreenivasa 	}
6655cabbc6bSPrashanth Sreenivasa 
6665cabbc6bSPrashanth Sreenivasa 	argc -= optind;
6675cabbc6bSPrashanth Sreenivasa 	argv += optind;
66899653d4eSeschrock 
66999653d4eSeschrock 	/* get pool name and check number of arguments */
67099653d4eSeschrock 	if (argc < 1) {
67199653d4eSeschrock 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
67299653d4eSeschrock 		usage(B_FALSE);
67399653d4eSeschrock 	}
67499653d4eSeschrock 
67599653d4eSeschrock 	poolname = argv[0];
67699653d4eSeschrock 
67799653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
67899653d4eSeschrock 		return (1);
67999653d4eSeschrock 
6805cabbc6bSPrashanth Sreenivasa 	if (stop && noop) {
6815cabbc6bSPrashanth Sreenivasa 		(void) fprintf(stderr, gettext("stop request ignored\n"));
6825cabbc6bSPrashanth Sreenivasa 		return (0);
6835cabbc6bSPrashanth Sreenivasa 	}
6845cabbc6bSPrashanth Sreenivasa 
6855cabbc6bSPrashanth Sreenivasa 	if (stop) {
6865cabbc6bSPrashanth Sreenivasa 		if (argc > 1) {
6875cabbc6bSPrashanth Sreenivasa 			(void) fprintf(stderr, gettext("too many arguments\n"));
6885cabbc6bSPrashanth Sreenivasa 			usage(B_FALSE);
6895cabbc6bSPrashanth Sreenivasa 		}
6905cabbc6bSPrashanth Sreenivasa 		if (zpool_vdev_remove_cancel(zhp) != 0)
691fa94a07fSbrendan 			ret = 1;
6925cabbc6bSPrashanth Sreenivasa 	} else {
6935cabbc6bSPrashanth Sreenivasa 		if (argc < 2) {
6945cabbc6bSPrashanth Sreenivasa 			(void) fprintf(stderr, gettext("missing device\n"));
6955cabbc6bSPrashanth Sreenivasa 			usage(B_FALSE);
6965cabbc6bSPrashanth Sreenivasa 		}
6975cabbc6bSPrashanth Sreenivasa 
6985cabbc6bSPrashanth Sreenivasa 		for (i = 1; i < argc; i++) {
6995cabbc6bSPrashanth Sreenivasa 			if (noop) {
7005cabbc6bSPrashanth Sreenivasa 				uint64_t size;
7015cabbc6bSPrashanth Sreenivasa 
7025cabbc6bSPrashanth Sreenivasa 				if (zpool_vdev_indirect_size(zhp, argv[i],
7035cabbc6bSPrashanth Sreenivasa 				    &size) != 0) {
7045cabbc6bSPrashanth Sreenivasa 					ret = 1;
7055cabbc6bSPrashanth Sreenivasa 					break;
7065cabbc6bSPrashanth Sreenivasa 				}
7075cabbc6bSPrashanth Sreenivasa 				if (parsable) {
7085cabbc6bSPrashanth Sreenivasa 					(void) printf("%s %llu\n",
7095cabbc6bSPrashanth Sreenivasa 					    argv[i], size);
7105cabbc6bSPrashanth Sreenivasa 				} else {
7115cabbc6bSPrashanth Sreenivasa 					char valstr[32];
7125cabbc6bSPrashanth Sreenivasa 					zfs_nicenum(size, valstr,
7135cabbc6bSPrashanth Sreenivasa 					    sizeof (valstr));
7145cabbc6bSPrashanth Sreenivasa 					(void) printf("Memory that will be "
7155cabbc6bSPrashanth Sreenivasa 					    "used after removing %s: %s\n",
7165cabbc6bSPrashanth Sreenivasa 					    argv[i], valstr);
7175cabbc6bSPrashanth Sreenivasa 				}
7185cabbc6bSPrashanth Sreenivasa 			} else {
7195cabbc6bSPrashanth Sreenivasa 				if (zpool_vdev_remove(zhp, argv[i]) != 0)
7205cabbc6bSPrashanth Sreenivasa 					ret = 1;
7215cabbc6bSPrashanth Sreenivasa 			}
7225cabbc6bSPrashanth Sreenivasa 		}
723fa94a07fSbrendan 	}
72499653d4eSeschrock 
725fa9e4066Sahrens 	return (ret);
726fa9e4066Sahrens }
727fa9e4066Sahrens 
7286401734dSWill Andrews /*
7296401734dSWill Andrews  * zpool labelclear [-f] <vdev>
7306401734dSWill Andrews  *
7316401734dSWill Andrews  *	-f	Force clearing the label for the vdevs which are members of
7326401734dSWill Andrews  *		the exported or foreign pools.
7336401734dSWill Andrews  *
7346401734dSWill Andrews  * Verifies that the vdev is not active and zeros out the label information
7356401734dSWill Andrews  * on the device.
7366401734dSWill Andrews  */
7376401734dSWill Andrews int
7386401734dSWill Andrews zpool_do_labelclear(int argc, char **argv)
7396401734dSWill Andrews {
7406401734dSWill Andrews 	char vdev[MAXPATHLEN];
7416401734dSWill Andrews 	char *name = NULL;
7426401734dSWill Andrews 	struct stat st;
7436401734dSWill Andrews 	int c, fd, ret = 0;
7446401734dSWill Andrews 	nvlist_t *config;
7456401734dSWill Andrews 	pool_state_t state;
7466401734dSWill Andrews 	boolean_t inuse = B_FALSE;
7476401734dSWill Andrews 	boolean_t force = B_FALSE;
7486401734dSWill Andrews 
7496401734dSWill Andrews 	/* check options */
7506401734dSWill Andrews 	while ((c = getopt(argc, argv, "f")) != -1) {
7516401734dSWill Andrews 		switch (c) {
7526401734dSWill Andrews 		case 'f':
7536401734dSWill Andrews 			force = B_TRUE;
7546401734dSWill Andrews 			break;
7556401734dSWill Andrews 		default:
7566401734dSWill Andrews 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
7576401734dSWill Andrews 			    optopt);
7586401734dSWill Andrews 			usage(B_FALSE);
7596401734dSWill Andrews 		}
7606401734dSWill Andrews 	}
7616401734dSWill Andrews 
7626401734dSWill Andrews 	argc -= optind;
7636401734dSWill Andrews 	argv += optind;
7646401734dSWill Andrews 
7656401734dSWill Andrews 	/* get vdev name */
7666401734dSWill Andrews 	if (argc < 1) {
7676401734dSWill Andrews 		(void) fprintf(stderr, gettext("missing vdev name\n"));
7686401734dSWill Andrews 		usage(B_FALSE);
7696401734dSWill Andrews 	}
7706401734dSWill Andrews 	if (argc > 1) {
7716401734dSWill Andrews 		(void) fprintf(stderr, gettext("too many arguments\n"));
7726401734dSWill Andrews 		usage(B_FALSE);
7736401734dSWill Andrews 	}
7746401734dSWill Andrews 
7756401734dSWill Andrews 	/*
7766401734dSWill Andrews 	 * Check if we were given absolute path and use it as is.
7776401734dSWill Andrews 	 * Otherwise if the provided vdev name doesn't point to a file,
7786401734dSWill Andrews 	 * try prepending dsk path and appending s0.
7796401734dSWill Andrews 	 */
7806401734dSWill Andrews 	(void) strlcpy(vdev, argv[0], sizeof (vdev));
7816401734dSWill Andrews 	if (vdev[0] != '/' && stat(vdev, &st) != 0) {
7826401734dSWill Andrews 		char *s;
7836401734dSWill Andrews 
7846401734dSWill Andrews 		(void) snprintf(vdev, sizeof (vdev), "%s/%s",
7856401734dSWill Andrews 		    ZFS_DISK_ROOT, argv[0]);
7866401734dSWill Andrews 		if ((s = strrchr(argv[0], 's')) == NULL ||
7876401734dSWill Andrews 		    !isdigit(*(s + 1)))
7886401734dSWill Andrews 			(void) strlcat(vdev, "s0", sizeof (vdev));
7896401734dSWill Andrews 		if (stat(vdev, &st) != 0) {
7906401734dSWill Andrews 			(void) fprintf(stderr, gettext(
7916401734dSWill Andrews 			    "failed to find device %s, try specifying absolute "
7926401734dSWill Andrews 			    "path instead\n"), argv[0]);
7936401734dSWill Andrews 			return (1);
7946401734dSWill Andrews 		}
7956401734dSWill Andrews 	}
7966401734dSWill Andrews 
7976401734dSWill Andrews 	if ((fd = open(vdev, O_RDWR)) < 0) {
7986401734dSWill Andrews 		(void) fprintf(stderr, gettext("failed to open %s: %s\n"),
7996401734dSWill Andrews 		    vdev, strerror(errno));
8006401734dSWill Andrews 		return (1);
8016401734dSWill Andrews 	}
8026401734dSWill Andrews 
803c861bfbdSAlan Somers 	if (zpool_read_label(fd, &config) != 0) {
8046401734dSWill Andrews 		(void) fprintf(stderr,
8056401734dSWill Andrews 		    gettext("failed to read label from %s\n"), vdev);
8066401734dSWill Andrews 		return (1);
8076401734dSWill Andrews 	}
8086401734dSWill Andrews 	nvlist_free(config);
8096401734dSWill Andrews 
8106401734dSWill Andrews 	ret = zpool_in_use(g_zfs, fd, &state, &name, &inuse);
8116401734dSWill Andrews 	if (ret != 0) {
8126401734dSWill Andrews 		(void) fprintf(stderr,
8136401734dSWill Andrews 		    gettext("failed to check state for %s\n"), vdev);
8146401734dSWill Andrews 		return (1);
8156401734dSWill Andrews 	}
8166401734dSWill Andrews 
8176401734dSWill Andrews 	if (!inuse)
8186401734dSWill Andrews 		goto wipe_label;
8196401734dSWill Andrews 
8206401734dSWill Andrews 	switch (state) {
8216401734dSWill Andrews 	default:
8226401734dSWill Andrews 	case POOL_STATE_ACTIVE:
8236401734dSWill Andrews 	case POOL_STATE_SPARE:
8246401734dSWill Andrews 	case POOL_STATE_L2CACHE:
8256401734dSWill Andrews 		(void) fprintf(stderr, gettext(
8266401734dSWill Andrews 		    "%s is a member (%s) of pool \"%s\"\n"),
8276401734dSWill Andrews 		    vdev, zpool_pool_state_to_name(state), name);
8286401734dSWill Andrews 		ret = 1;
8296401734dSWill Andrews 		goto errout;
8306401734dSWill Andrews 
8316401734dSWill Andrews 	case POOL_STATE_EXPORTED:
8326401734dSWill Andrews 		if (force)
8336401734dSWill Andrews 			break;
8346401734dSWill Andrews 		(void) fprintf(stderr, gettext(
8356401734dSWill Andrews 		    "use '-f' to override the following error:\n"
8366401734dSWill Andrews 		    "%s is a member of exported pool \"%s\"\n"),
8376401734dSWill Andrews 		    vdev, name);
8386401734dSWill Andrews 		ret = 1;
8396401734dSWill Andrews 		goto errout;
8406401734dSWill Andrews 
8416401734dSWill Andrews 	case POOL_STATE_POTENTIALLY_ACTIVE:
8426401734dSWill Andrews 		if (force)
8436401734dSWill Andrews 			break;
8446401734dSWill Andrews 		(void) fprintf(stderr, gettext(
8456401734dSWill Andrews 		    "use '-f' to override the following error:\n"
8466401734dSWill Andrews 		    "%s is a member of potentially active pool \"%s\"\n"),
8476401734dSWill Andrews 		    vdev, name);
8486401734dSWill Andrews 		ret = 1;
8496401734dSWill Andrews 		goto errout;
8506401734dSWill Andrews 
8516401734dSWill Andrews 	case POOL_STATE_DESTROYED:
8526401734dSWill Andrews 		/* inuse should never be set for a destroyed pool */
8536401734dSWill Andrews 		assert(0);
8546401734dSWill Andrews 		break;
8556401734dSWill Andrews 	}
8566401734dSWill Andrews 
8576401734dSWill Andrews wipe_label:
8586401734dSWill Andrews 	ret = zpool_clear_label(fd);
8596401734dSWill Andrews 	if (ret != 0) {
8606401734dSWill Andrews 		(void) fprintf(stderr,
8616401734dSWill Andrews 		    gettext("failed to clear label for %s\n"), vdev);
8626401734dSWill Andrews 	}
8636401734dSWill Andrews 
8646401734dSWill Andrews errout:
8656401734dSWill Andrews 	free(name);
8666401734dSWill Andrews 	(void) close(fd);
8676401734dSWill Andrews 
8686401734dSWill Andrews 	return (ret);
8696401734dSWill Andrews }
8706401734dSWill Andrews 
871fa9e4066Sahrens /*
8727855d95bSToomas Soome  * zpool create [-fnd] [-B] [-o property=value] ...
8730a48a24eStimh  *		[-O file-system-property=value] ...
87404e56356SAndriy Gapon  *		[-R root] [-m mountpoint] [-t tempname] <pool> <dev> ...
875fa9e4066Sahrens  *
8767855d95bSToomas Soome  *	-B	Create boot partition.
877fa9e4066Sahrens  *	-f	Force creation, even if devices appear in use
878fa9e4066Sahrens  *	-n	Do not create the pool, but display the resulting layout if it
879fa9e4066Sahrens  *		were to be created.
88004e56356SAndriy Gapon  *	-R	Create a pool under an alternate root
88104e56356SAndriy Gapon  *	-m	Set default mountpoint for the root dataset.  By default it's
882ad135b5dSChristopher Siden  *		'/<pool>'
88304e56356SAndriy Gapon  *	-t	Use the temporary name until the pool is exported.
884990b4856Slling  *	-o	Set property=value.
885ad135b5dSChristopher Siden  *	-d	Don't automatically enable all supported pool features
886ad135b5dSChristopher Siden  *		(individual features can be enabled with -o).
8870a48a24eStimh  *	-O	Set fsproperty=value in the pool's root file system
888fa9e4066Sahrens  *
889b1b8ab34Slling  * Creates the named pool according to the given vdev specification.  The
890fa9e4066Sahrens  * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c.  Once
891fa9e4066Sahrens  * we get the nvlist back from get_vdev_spec(), we either print out the contents
892fa9e4066Sahrens  * (if '-n' was specified), or pass it to libzfs to do the creation.
893fa9e4066Sahrens  */
8947855d95bSToomas Soome 
8957855d95bSToomas Soome #define	SYSTEM256	(256 * 1024 * 1024)
896fa9e4066Sahrens int
897fa9e4066Sahrens zpool_do_create(int argc, char **argv)
898fa9e4066Sahrens {
89999653d4eSeschrock 	boolean_t force = B_FALSE;
90099653d4eSeschrock 	boolean_t dryrun = B_FALSE;
901ad135b5dSChristopher Siden 	boolean_t enable_all_pool_feat = B_TRUE;
9027855d95bSToomas Soome 	zpool_boot_label_t boot_type = ZPOOL_NO_BOOT_LABEL;
9037855d95bSToomas Soome 	uint64_t boot_size = 0;
904fa9e4066Sahrens 	int c;
905990b4856Slling 	nvlist_t *nvroot = NULL;
906fa9e4066Sahrens 	char *poolname;
90704e56356SAndriy Gapon 	char *tname = NULL;
908990b4856Slling 	int ret = 1;
909fa9e4066Sahrens 	char *altroot = NULL;
910fa9e4066Sahrens 	char *mountpoint = NULL;
9110a48a24eStimh 	nvlist_t *fsprops = NULL;
912990b4856Slling 	nvlist_t *props = NULL;
9132f8aaab3Seschrock 	char *propval;
914fa9e4066Sahrens 
915fa9e4066Sahrens 	/* check options */
91604e56356SAndriy Gapon 	while ((c = getopt(argc, argv, ":fndBR:m:o:O:t:")) != -1) {
917fa9e4066Sahrens 		switch (c) {
918fa9e4066Sahrens 		case 'f':
91999653d4eSeschrock 			force = B_TRUE;
920fa9e4066Sahrens 			break;
921fa9e4066Sahrens 		case 'n':
92299653d4eSeschrock 			dryrun = B_TRUE;
923fa9e4066Sahrens 			break;
924ad135b5dSChristopher Siden 		case 'd':
925ad135b5dSChristopher Siden 			enable_all_pool_feat = B_FALSE;
926ad135b5dSChristopher Siden 			break;
9277855d95bSToomas Soome 		case 'B':
9287855d95bSToomas Soome 			/*
9297855d95bSToomas Soome 			 * We should create the system partition.
9307855d95bSToomas Soome 			 * Also make sure the size is set.
9317855d95bSToomas Soome 			 */
9327855d95bSToomas Soome 			boot_type = ZPOOL_CREATE_BOOT_LABEL;
9337855d95bSToomas Soome 			if (boot_size == 0)
9347855d95bSToomas Soome 				boot_size = SYSTEM256;
9357855d95bSToomas Soome 			break;
936fa9e4066Sahrens 		case 'R':
937fa9e4066Sahrens 			altroot = optarg;
938990b4856Slling 			if (add_prop_list(zpool_prop_to_name(
9390a48a24eStimh 			    ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
940990b4856Slling 				goto errout;
94104e56356SAndriy Gapon 			if (add_prop_list_default(zpool_prop_to_name(
9420a48a24eStimh 			    ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
943990b4856Slling 				goto errout;
944fa9e4066Sahrens 			break;
945fa9e4066Sahrens 		case 'm':
9468b713775SWill Andrews 			/* Equivalent to -O mountpoint=optarg */
947fa9e4066Sahrens 			mountpoint = optarg;
948fa9e4066Sahrens 			break;
949990b4856Slling 		case 'o':
950990b4856Slling 			if ((propval = strchr(optarg, '=')) == NULL) {
951990b4856Slling 				(void) fprintf(stderr, gettext("missing "
952990b4856Slling 				    "'=' for -o option\n"));
953990b4856Slling 				goto errout;
954990b4856Slling 			}
955990b4856Slling 			*propval = '\0';
956990b4856Slling 			propval++;
957990b4856Slling 
9580a48a24eStimh 			if (add_prop_list(optarg, propval, &props, B_TRUE))
9590a48a24eStimh 				goto errout;
960ad135b5dSChristopher Siden 
9617855d95bSToomas Soome 			/*
9627855d95bSToomas Soome 			 * Get bootsize value for make_root_vdev().
9637855d95bSToomas Soome 			 */
9647855d95bSToomas Soome 			if (zpool_name_to_prop(optarg) == ZPOOL_PROP_BOOTSIZE) {
9657855d95bSToomas Soome 				if (zfs_nicestrtonum(g_zfs, propval,
9667855d95bSToomas Soome 				    &boot_size) < 0 || boot_size == 0) {
9677855d95bSToomas Soome 					(void) fprintf(stderr,
9687855d95bSToomas Soome 					    gettext("bad boot partition size "
9697855d95bSToomas Soome 					    "'%s': %s\n"),  propval,
9707855d95bSToomas Soome 					    libzfs_error_description(g_zfs));
9717855d95bSToomas Soome 					goto errout;
9727855d95bSToomas Soome 				}
9737855d95bSToomas Soome 			}
9747855d95bSToomas Soome 
975ad135b5dSChristopher Siden 			/*
976ad135b5dSChristopher Siden 			 * If the user is creating a pool that doesn't support
977ad135b5dSChristopher Siden 			 * feature flags, don't enable any features.
978ad135b5dSChristopher Siden 			 */
979ad135b5dSChristopher Siden 			if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
980ad135b5dSChristopher Siden 				char *end;
981ad135b5dSChristopher Siden 				u_longlong_t ver;
982ad135b5dSChristopher Siden 
983ad135b5dSChristopher Siden 				ver = strtoull(propval, &end, 10);
984ad135b5dSChristopher Siden 				if (*end == '\0' &&
985ad135b5dSChristopher Siden 				    ver < SPA_VERSION_FEATURES) {
986ad135b5dSChristopher Siden 					enable_all_pool_feat = B_FALSE;
987ad135b5dSChristopher Siden 				}
988ad135b5dSChristopher Siden 			}
989c423721fSXin Li 			if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT)
990c423721fSXin Li 				altroot = propval;
9910a48a24eStimh 			break;
9920a48a24eStimh 		case 'O':
9930a48a24eStimh 			if ((propval = strchr(optarg, '=')) == NULL) {
9940a48a24eStimh 				(void) fprintf(stderr, gettext("missing "
9950a48a24eStimh 				    "'=' for -O option\n"));
9960a48a24eStimh 				goto errout;
9970a48a24eStimh 			}
9980a48a24eStimh 			*propval = '\0';
9990a48a24eStimh 			propval++;
10000a48a24eStimh 
10018b713775SWill Andrews 			/*
10028b713775SWill Andrews 			 * Mountpoints are checked and then added later.
10038b713775SWill Andrews 			 * Uniquely among properties, they can be specified
10048b713775SWill Andrews 			 * more than once, to avoid conflict with -m.
10058b713775SWill Andrews 			 */
10068b713775SWill Andrews 			if (0 == strcmp(optarg,
10078b713775SWill Andrews 			    zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
10088b713775SWill Andrews 				mountpoint = propval;
10098b713775SWill Andrews 			} else if (add_prop_list(optarg, propval, &fsprops,
10108b713775SWill Andrews 			    B_FALSE)) {
1011990b4856Slling 				goto errout;
10128b713775SWill Andrews 			}
1013990b4856Slling 			break;
101404e56356SAndriy Gapon 		case 't':
101504e56356SAndriy Gapon 			/*
101604e56356SAndriy Gapon 			 * Sanity check temporary pool name.
101704e56356SAndriy Gapon 			 */
101804e56356SAndriy Gapon 			if (strchr(optarg, '/') != NULL) {
101904e56356SAndriy Gapon 				(void) fprintf(stderr, gettext("cannot create "
102004e56356SAndriy Gapon 				    "'%s': invalid character '/' in temporary "
102104e56356SAndriy Gapon 				    "name\n"), optarg);
102204e56356SAndriy Gapon 				(void) fprintf(stderr, gettext("use 'zfs "
102304e56356SAndriy Gapon 				    "create' to create a dataset\n"));
102404e56356SAndriy Gapon 				goto errout;
102504e56356SAndriy Gapon 			}
102604e56356SAndriy Gapon 
102704e56356SAndriy Gapon 			if (add_prop_list(zpool_prop_to_name(
102804e56356SAndriy Gapon 			    ZPOOL_PROP_TNAME), optarg, &props, B_TRUE))
102904e56356SAndriy Gapon 				goto errout;
103004e56356SAndriy Gapon 			if (add_prop_list_default(zpool_prop_to_name(
103104e56356SAndriy Gapon 			    ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
103204e56356SAndriy Gapon 				goto errout;
103304e56356SAndriy Gapon 			tname = optarg;
103404e56356SAndriy Gapon 			break;
1035fa9e4066Sahrens 		case ':':
1036fa9e4066Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
1037fa9e4066Sahrens 			    "'%c' option\n"), optopt);
1038990b4856Slling 			goto badusage;
1039fa9e4066Sahrens 		case '?':
1040fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1041fa9e4066Sahrens 			    optopt);
1042990b4856Slling 			goto badusage;
1043fa9e4066Sahrens 		}
1044fa9e4066Sahrens 	}
1045fa9e4066Sahrens 
1046fa9e4066Sahrens 	argc -= optind;
1047fa9e4066Sahrens 	argv += optind;
1048fa9e4066Sahrens 
1049fa9e4066Sahrens 	/* get pool name and check number of arguments */
1050fa9e4066Sahrens 	if (argc < 1) {
1051fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
1052990b4856Slling 		goto badusage;
1053fa9e4066Sahrens 	}
1054fa9e4066Sahrens 	if (argc < 2) {
1055fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing vdev specification\n"));
1056990b4856Slling 		goto badusage;
1057fa9e4066Sahrens 	}
1058fa9e4066Sahrens 
1059fa9e4066Sahrens 	poolname = argv[0];
1060fa9e4066Sahrens 
1061fa9e4066Sahrens 	/*
1062fa9e4066Sahrens 	 * As a special case, check for use of '/' in the name, and direct the
1063fa9e4066Sahrens 	 * user to use 'zfs create' instead.
1064fa9e4066Sahrens 	 */
1065fa9e4066Sahrens 	if (strchr(poolname, '/') != NULL) {
1066fa9e4066Sahrens 		(void) fprintf(stderr, gettext("cannot create '%s': invalid "
1067fa9e4066Sahrens 		    "character '/' in pool name\n"), poolname);
1068fa9e4066Sahrens 		(void) fprintf(stderr, gettext("use 'zfs create' to "
1069fa9e4066Sahrens 		    "create a dataset\n"));
1070990b4856Slling 		goto errout;
1071fa9e4066Sahrens 	}
1072fa9e4066Sahrens 
10737855d95bSToomas Soome 	/*
10747855d95bSToomas Soome 	 * Make sure the bootsize is set when ZPOOL_CREATE_BOOT_LABEL is used,
10757855d95bSToomas Soome 	 * and not set otherwise.
10767855d95bSToomas Soome 	 */
10777855d95bSToomas Soome 	if (boot_type == ZPOOL_CREATE_BOOT_LABEL) {
10787855d95bSToomas Soome 		const char *propname;
10797855d95bSToomas Soome 		char *strptr, *buf = NULL;
10807855d95bSToomas Soome 		int rv;
10817855d95bSToomas Soome 
10827855d95bSToomas Soome 		propname = zpool_prop_to_name(ZPOOL_PROP_BOOTSIZE);
10837855d95bSToomas Soome 		if (nvlist_lookup_string(props, propname, &strptr) != 0) {
10847855d95bSToomas Soome 			(void) asprintf(&buf, "%" PRIu64, boot_size);
10857855d95bSToomas Soome 			if (buf == NULL) {
10867855d95bSToomas Soome 				(void) fprintf(stderr,
10877855d95bSToomas Soome 				    gettext("internal error: out of memory\n"));
10887855d95bSToomas Soome 				goto errout;
10897855d95bSToomas Soome 			}
10907855d95bSToomas Soome 			rv = add_prop_list(propname, buf, &props, B_TRUE);
10917855d95bSToomas Soome 			free(buf);
10927855d95bSToomas Soome 			if (rv != 0)
10937855d95bSToomas Soome 				goto errout;
10947855d95bSToomas Soome 		}
10957855d95bSToomas Soome 	} else {
10967855d95bSToomas Soome 		const char *propname;
10977855d95bSToomas Soome 		char *strptr;
10987855d95bSToomas Soome 
10997855d95bSToomas Soome 		propname = zpool_prop_to_name(ZPOOL_PROP_BOOTSIZE);
11007855d95bSToomas Soome 		if (nvlist_lookup_string(props, propname, &strptr) == 0) {
11017855d95bSToomas Soome 			(void) fprintf(stderr, gettext("error: setting boot "
11027855d95bSToomas Soome 			    "partition size requires option '-B'\n"));
11037855d95bSToomas Soome 			goto errout;
11047855d95bSToomas Soome 		}
11057855d95bSToomas Soome 	}
11067855d95bSToomas Soome 
1107fa9e4066Sahrens 	/* pass off to get_vdev_spec for bulk processing */
1108705040edSEric Taylor 	nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
11097855d95bSToomas Soome 	    boot_type, boot_size, argc - 1, argv + 1);
1110fa9e4066Sahrens 	if (nvroot == NULL)
11110a48a24eStimh 		goto errout;
1112fa9e4066Sahrens 
111399653d4eSeschrock 	/* make_root_vdev() allows 0 toplevel children if there are spares */
1114b7b97454Sperrin 	if (!zfs_allocatable_devs(nvroot)) {
111599653d4eSeschrock 		(void) fprintf(stderr, gettext("invalid vdev "
111699653d4eSeschrock 		    "specification: at least one toplevel vdev must be "
111799653d4eSeschrock 		    "specified\n"));
1118990b4856Slling 		goto errout;
111999653d4eSeschrock 	}
112099653d4eSeschrock 
1121fa9e4066Sahrens 	if (altroot != NULL && altroot[0] != '/') {
1122fa9e4066Sahrens 		(void) fprintf(stderr, gettext("invalid alternate root '%s': "
1123e9dbad6fSeschrock 		    "must be an absolute path\n"), altroot);
1124990b4856Slling 		goto errout;
1125fa9e4066Sahrens 	}
1126fa9e4066Sahrens 
1127fa9e4066Sahrens 	/*
1128fa9e4066Sahrens 	 * Check the validity of the mountpoint and direct the user to use the
1129fa9e4066Sahrens 	 * '-m' mountpoint option if it looks like its in use.
1130fa9e4066Sahrens 	 */
1131fa9e4066Sahrens 	if (mountpoint == NULL ||
1132fa9e4066Sahrens 	    (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
1133fa9e4066Sahrens 	    strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
1134fa9e4066Sahrens 		char buf[MAXPATHLEN];
113511022c7cStimh 		DIR *dirp;
1136fa9e4066Sahrens 
1137fa9e4066Sahrens 		if (mountpoint && mountpoint[0] != '/') {
1138fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid mountpoint "
1139fa9e4066Sahrens 			    "'%s': must be an absolute path, 'legacy', or "
1140fa9e4066Sahrens 			    "'none'\n"), mountpoint);
1141990b4856Slling 			goto errout;
1142fa9e4066Sahrens 		}
1143fa9e4066Sahrens 
1144fa9e4066Sahrens 		if (mountpoint == NULL) {
1145fa9e4066Sahrens 			if (altroot != NULL)
1146fa9e4066Sahrens 				(void) snprintf(buf, sizeof (buf), "%s/%s",
1147fa9e4066Sahrens 				    altroot, poolname);
1148fa9e4066Sahrens 			else
1149fa9e4066Sahrens 				(void) snprintf(buf, sizeof (buf), "/%s",
1150fa9e4066Sahrens 				    poolname);
1151fa9e4066Sahrens 		} else {
1152fa9e4066Sahrens 			if (altroot != NULL)
1153fa9e4066Sahrens 				(void) snprintf(buf, sizeof (buf), "%s%s",
1154fa9e4066Sahrens 				    altroot, mountpoint);
1155fa9e4066Sahrens 			else
1156fa9e4066Sahrens 				(void) snprintf(buf, sizeof (buf), "%s",
1157fa9e4066Sahrens 				    mountpoint);
1158fa9e4066Sahrens 		}
1159fa9e4066Sahrens 
116011022c7cStimh 		if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
116111022c7cStimh 			(void) fprintf(stderr, gettext("mountpoint '%s' : "
116211022c7cStimh 			    "%s\n"), buf, strerror(errno));
1163fa9e4066Sahrens 			(void) fprintf(stderr, gettext("use '-m' "
1164fa9e4066Sahrens 			    "option to provide a different default\n"));
1165990b4856Slling 			goto errout;
116611022c7cStimh 		} else if (dirp) {
116711022c7cStimh 			int count = 0;
116811022c7cStimh 
116911022c7cStimh 			while (count < 3 && readdir(dirp) != NULL)
117011022c7cStimh 				count++;
117111022c7cStimh 			(void) closedir(dirp);
117211022c7cStimh 
117311022c7cStimh 			if (count > 2) {
117411022c7cStimh 				(void) fprintf(stderr, gettext("mountpoint "
117511022c7cStimh 				    "'%s' exists and is not empty\n"), buf);
117611022c7cStimh 				(void) fprintf(stderr, gettext("use '-m' "
117711022c7cStimh 				    "option to provide a "
117811022c7cStimh 				    "different default\n"));
117911022c7cStimh 				goto errout;
118011022c7cStimh 			}
1181fa9e4066Sahrens 		}
1182fa9e4066Sahrens 	}
1183fa9e4066Sahrens 
11848b713775SWill Andrews 	/*
11858b713775SWill Andrews 	 * Now that the mountpoint's validity has been checked, ensure that
11868b713775SWill Andrews 	 * the property is set appropriately prior to creating the pool.
11878b713775SWill Andrews 	 */
11888b713775SWill Andrews 	if (mountpoint != NULL) {
11898b713775SWill Andrews 		ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
11908b713775SWill Andrews 		    mountpoint, &fsprops, B_FALSE);
11918b713775SWill Andrews 		if (ret != 0)
11928b713775SWill Andrews 			goto errout;
11938b713775SWill Andrews 	}
11948b713775SWill Andrews 
11958b713775SWill Andrews 	ret = 1;
1196fa9e4066Sahrens 	if (dryrun) {
1197fa9e4066Sahrens 		/*
1198fa9e4066Sahrens 		 * For a dry run invocation, print out a basic message and run
1199fa9e4066Sahrens 		 * through all the vdevs in the list and print out in an
1200fa9e4066Sahrens 		 * appropriate hierarchy.
1201fa9e4066Sahrens 		 */
1202fa9e4066Sahrens 		(void) printf(gettext("would create '%s' with the "
1203fa9e4066Sahrens 		    "following layout:\n\n"), poolname);
1204fa9e4066Sahrens 
12058654d025Sperrin 		print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
12068654d025Sperrin 		if (num_logs(nvroot) > 0)
12078654d025Sperrin 			print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
1208fa9e4066Sahrens 
1209fa9e4066Sahrens 		ret = 0;
1210fa9e4066Sahrens 	} else {
1211fa9e4066Sahrens 		/*
1212fa9e4066Sahrens 		 * Hand off to libzfs.
1213fa9e4066Sahrens 		 */
1214ad135b5dSChristopher Siden 		if (enable_all_pool_feat) {
12152acef22dSMatthew Ahrens 			spa_feature_t i;
1216ad135b5dSChristopher Siden 			for (i = 0; i < SPA_FEATURES; i++) {
1217ad135b5dSChristopher Siden 				char propname[MAXPATHLEN];
1218ad135b5dSChristopher Siden 				zfeature_info_t *feat = &spa_feature_table[i];
1219ad135b5dSChristopher Siden 
1220ad135b5dSChristopher Siden 				(void) snprintf(propname, sizeof (propname),
1221ad135b5dSChristopher Siden 				    "feature@%s", feat->fi_uname);
1222ad135b5dSChristopher Siden 
1223ad135b5dSChristopher Siden 				/*
1224ad135b5dSChristopher Siden 				 * Skip feature if user specified it manually
1225ad135b5dSChristopher Siden 				 * on the command line.
1226ad135b5dSChristopher Siden 				 */
1227ad135b5dSChristopher Siden 				if (nvlist_exists(props, propname))
1228ad135b5dSChristopher Siden 					continue;
1229ad135b5dSChristopher Siden 
12308b713775SWill Andrews 				ret = add_prop_list(propname,
12318b713775SWill Andrews 				    ZFS_FEATURE_ENABLED, &props, B_TRUE);
12328b713775SWill Andrews 				if (ret != 0)
1233ad135b5dSChristopher Siden 					goto errout;
1234ad135b5dSChristopher Siden 			}
1235ad135b5dSChristopher Siden 		}
12368b713775SWill Andrews 
12378b713775SWill Andrews 		ret = 1;
12380a48a24eStimh 		if (zpool_create(g_zfs, poolname,
12390a48a24eStimh 		    nvroot, props, fsprops) == 0) {
124004e56356SAndriy Gapon 			zfs_handle_t *pool = zfs_open(g_zfs,
124104e56356SAndriy Gapon 			    tname ? tname : poolname, ZFS_TYPE_FILESYSTEM);
1242fa9e4066Sahrens 			if (pool != NULL) {
1243fa9e4066Sahrens 				if (zfs_mount(pool, NULL, 0) == 0)
1244da6c28aaSamw 					ret = zfs_shareall(pool);
1245fa9e4066Sahrens 				zfs_close(pool);
1246fa9e4066Sahrens 			}
124799653d4eSeschrock 		} else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
124899653d4eSeschrock 			(void) fprintf(stderr, gettext("pool name may have "
124999653d4eSeschrock 			    "been omitted\n"));
1250fa9e4066Sahrens 		}
1251fa9e4066Sahrens 	}
1252fa9e4066Sahrens 
1253990b4856Slling errout:
12542f8aaab3Seschrock 	nvlist_free(nvroot);
12550a48a24eStimh 	nvlist_free(fsprops);
12562f8aaab3Seschrock 	nvlist_free(props);
1257fa9e4066Sahrens 	return (ret);
1258990b4856Slling badusage:
12590a48a24eStimh 	nvlist_free(fsprops);
1260990b4856Slling 	nvlist_free(props);
1261990b4856Slling 	usage(B_FALSE);
1262990b4856Slling 	return (2);
1263fa9e4066Sahrens }
1264fa9e4066Sahrens 
1265fa9e4066Sahrens /*
1266fa9e4066Sahrens  * zpool destroy <pool>
1267fa9e4066Sahrens  *
126804e56356SAndriy Gapon  *	-f	Forcefully unmount any datasets
1269fa9e4066Sahrens  *
1270fa9e4066Sahrens  * Destroy the given pool.  Automatically unmounts any datasets in the pool.
1271fa9e4066Sahrens  */
1272fa9e4066Sahrens int
1273fa9e4066Sahrens zpool_do_destroy(int argc, char **argv)
1274fa9e4066Sahrens {
127599653d4eSeschrock 	boolean_t force = B_FALSE;
1276fa9e4066Sahrens 	int c;
1277fa9e4066Sahrens 	char *pool;
1278fa9e4066Sahrens 	zpool_handle_t *zhp;
1279fa9e4066Sahrens 	int ret;
1280fa9e4066Sahrens 
1281fa9e4066Sahrens 	/* check options */
1282fa9e4066Sahrens 	while ((c = getopt(argc, argv, "f")) != -1) {
1283fa9e4066Sahrens 		switch (c) {
1284fa9e4066Sahrens 		case 'f':
128599653d4eSeschrock 			force = B_TRUE;
1286fa9e4066Sahrens 			break;
1287fa9e4066Sahrens 		case '?':
1288fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1289fa9e4066Sahrens 			    optopt);
129099653d4eSeschrock 			usage(B_FALSE);
1291fa9e4066Sahrens 		}
1292fa9e4066Sahrens 	}
1293fa9e4066Sahrens 
1294fa9e4066Sahrens 	argc -= optind;
1295fa9e4066Sahrens 	argv += optind;
1296fa9e4066Sahrens 
1297fa9e4066Sahrens 	/* check arguments */
1298fa9e4066Sahrens 	if (argc < 1) {
1299fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool argument\n"));
130099653d4eSeschrock 		usage(B_FALSE);
1301fa9e4066Sahrens 	}
1302fa9e4066Sahrens 	if (argc > 1) {
1303fa9e4066Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
130499653d4eSeschrock 		usage(B_FALSE);
1305fa9e4066Sahrens 	}
1306fa9e4066Sahrens 
1307fa9e4066Sahrens 	pool = argv[0];
1308fa9e4066Sahrens 
130999653d4eSeschrock 	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1310fa9e4066Sahrens 		/*
1311fa9e4066Sahrens 		 * As a special case, check for use of '/' in the name, and
1312fa9e4066Sahrens 		 * direct the user to use 'zfs destroy' instead.
1313fa9e4066Sahrens 		 */
1314fa9e4066Sahrens 		if (strchr(pool, '/') != NULL)
1315fa9e4066Sahrens 			(void) fprintf(stderr, gettext("use 'zfs destroy' to "
1316fa9e4066Sahrens 			    "destroy a dataset\n"));
1317fa9e4066Sahrens 		return (1);
1318fa9e4066Sahrens 	}
1319fa9e4066Sahrens 
1320f3861e1aSahl 	if (zpool_disable_datasets(zhp, force) != 0) {
1321fa9e4066Sahrens 		(void) fprintf(stderr, gettext("could not destroy '%s': "
1322fa9e4066Sahrens 		    "could not unmount datasets\n"), zpool_get_name(zhp));
1323fa9e4066Sahrens 		return (1);
1324fa9e4066Sahrens 	}
1325fa9e4066Sahrens 
13264445fffbSMatthew Ahrens 	/* The history must be logged as part of the export */
13274445fffbSMatthew Ahrens 	log_history = B_FALSE;
13284445fffbSMatthew Ahrens 
13294445fffbSMatthew Ahrens 	ret = (zpool_destroy(zhp, history_str) != 0);
1330fa9e4066Sahrens 
1331fa9e4066Sahrens 	zpool_close(zhp);
1332fa9e4066Sahrens 
1333fa9e4066Sahrens 	return (ret);
1334fa9e4066Sahrens }
1335fa9e4066Sahrens 
1336fa9e4066Sahrens /*
1337fa9e4066Sahrens  * zpool export [-f] <pool> ...
1338fa9e4066Sahrens  *
1339fa9e4066Sahrens  *	-f	Forcefully unmount datasets
1340fa9e4066Sahrens  *
1341b1b8ab34Slling  * Export the given pools.  By default, the command will attempt to cleanly
1342fa9e4066Sahrens  * unmount any active datasets within the pool.  If the '-f' flag is specified,
1343fa9e4066Sahrens  * then the datasets will be forcefully unmounted.
1344fa9e4066Sahrens  */
1345fa9e4066Sahrens int
1346fa9e4066Sahrens zpool_do_export(int argc, char **argv)
1347fa9e4066Sahrens {
134899653d4eSeschrock 	boolean_t force = B_FALSE;
1349394ab0cbSGeorge Wilson 	boolean_t hardforce = B_FALSE;
1350fa9e4066Sahrens 	int c;
1351fa9e4066Sahrens 	zpool_handle_t *zhp;
1352fa9e4066Sahrens 	int ret;
1353fa9e4066Sahrens 	int i;
1354fa9e4066Sahrens 
1355fa9e4066Sahrens 	/* check options */
1356394ab0cbSGeorge Wilson 	while ((c = getopt(argc, argv, "fF")) != -1) {
1357fa9e4066Sahrens 		switch (c) {
1358fa9e4066Sahrens 		case 'f':
135999653d4eSeschrock 			force = B_TRUE;
1360fa9e4066Sahrens 			break;
1361394ab0cbSGeorge Wilson 		case 'F':
1362394ab0cbSGeorge Wilson 			hardforce = B_TRUE;
1363394ab0cbSGeorge Wilson 			break;
1364fa9e4066Sahrens 		case '?':
1365fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1366fa9e4066Sahrens 			    optopt);
136799653d4eSeschrock 			usage(B_FALSE);
1368fa9e4066Sahrens 		}
1369fa9e4066Sahrens 	}
1370fa9e4066Sahrens 
1371fa9e4066Sahrens 	argc -= optind;
1372fa9e4066Sahrens 	argv += optind;
1373fa9e4066Sahrens 
1374fa9e4066Sahrens 	/* check arguments */
1375fa9e4066Sahrens 	if (argc < 1) {
1376fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool argument\n"));
137799653d4eSeschrock 		usage(B_FALSE);
1378fa9e4066Sahrens 	}
1379fa9e4066Sahrens 
1380fa9e4066Sahrens 	ret = 0;
1381fa9e4066Sahrens 	for (i = 0; i < argc; i++) {
138299653d4eSeschrock 		if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1383fa9e4066Sahrens 			ret = 1;
1384fa9e4066Sahrens 			continue;
1385fa9e4066Sahrens 		}
1386fa9e4066Sahrens 
1387f3861e1aSahl 		if (zpool_disable_datasets(zhp, force) != 0) {
1388fa9e4066Sahrens 			ret = 1;
1389fa9e4066Sahrens 			zpool_close(zhp);
1390fa9e4066Sahrens 			continue;
1391fa9e4066Sahrens 		}
1392fa9e4066Sahrens 
13934445fffbSMatthew Ahrens 		/* The history must be logged as part of the export */
13944445fffbSMatthew Ahrens 		log_history = B_FALSE;
13954445fffbSMatthew Ahrens 
1396394ab0cbSGeorge Wilson 		if (hardforce) {
13974445fffbSMatthew Ahrens 			if (zpool_export_force(zhp, history_str) != 0)
1398394ab0cbSGeorge Wilson 				ret = 1;
13994445fffbSMatthew Ahrens 		} else if (zpool_export(zhp, force, history_str) != 0) {
1400fa9e4066Sahrens 			ret = 1;
1401394ab0cbSGeorge Wilson 		}
1402fa9e4066Sahrens 
1403fa9e4066Sahrens 		zpool_close(zhp);
1404fa9e4066Sahrens 	}
1405fa9e4066Sahrens 
1406fa9e4066Sahrens 	return (ret);
1407fa9e4066Sahrens }
1408fa9e4066Sahrens 
1409fa9e4066Sahrens /*
1410fa9e4066Sahrens  * Given a vdev configuration, determine the maximum width needed for the device
1411fa9e4066Sahrens  * name column.
1412fa9e4066Sahrens  */
1413fa9e4066Sahrens static int
1414c67d9675Seschrock max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1415fa9e4066Sahrens {
141688ecc943SGeorge Wilson 	char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1417fa9e4066Sahrens 	nvlist_t **child;
1418fa9e4066Sahrens 	uint_t c, children;
1419fa9e4066Sahrens 	int ret;
1420fa9e4066Sahrens 
1421fa9e4066Sahrens 	if (strlen(name) + depth > max)
1422fa9e4066Sahrens 		max = strlen(name) + depth;
1423fa9e4066Sahrens 
1424afefbcddSeschrock 	free(name);
1425afefbcddSeschrock 
142699653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
142799653d4eSeschrock 	    &child, &children) == 0) {
142899653d4eSeschrock 		for (c = 0; c < children; c++)
142999653d4eSeschrock 			if ((ret = max_width(zhp, child[c], depth + 2,
143099653d4eSeschrock 			    max)) > max)
143199653d4eSeschrock 				max = ret;
143299653d4eSeschrock 	}
143399653d4eSeschrock 
1434fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1435fa94a07fSbrendan 	    &child, &children) == 0) {
1436fa94a07fSbrendan 		for (c = 0; c < children; c++)
1437fa94a07fSbrendan 			if ((ret = max_width(zhp, child[c], depth + 2,
1438fa94a07fSbrendan 			    max)) > max)
1439fa94a07fSbrendan 				max = ret;
1440fa94a07fSbrendan 	}
1441fa94a07fSbrendan 
1442fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
144399653d4eSeschrock 	    &child, &children) == 0) {
144499653d4eSeschrock 		for (c = 0; c < children; c++)
144599653d4eSeschrock 			if ((ret = max_width(zhp, child[c], depth + 2,
144699653d4eSeschrock 			    max)) > max)
144799653d4eSeschrock 				max = ret;
144899653d4eSeschrock 	}
1449fa9e4066Sahrens 
1450fa9e4066Sahrens 
1451fa9e4066Sahrens 	return (max);
1452fa9e4066Sahrens }
1453fa9e4066Sahrens 
1454e6ca193dSGeorge Wilson typedef struct spare_cbdata {
1455e6ca193dSGeorge Wilson 	uint64_t	cb_guid;
1456e6ca193dSGeorge Wilson 	zpool_handle_t	*cb_zhp;
1457e6ca193dSGeorge Wilson } spare_cbdata_t;
1458e6ca193dSGeorge Wilson 
1459e6ca193dSGeorge Wilson static boolean_t
1460e6ca193dSGeorge Wilson find_vdev(nvlist_t *nv, uint64_t search)
1461e6ca193dSGeorge Wilson {
1462e6ca193dSGeorge Wilson 	uint64_t guid;
1463e6ca193dSGeorge Wilson 	nvlist_t **child;
1464e6ca193dSGeorge Wilson 	uint_t c, children;
1465e6ca193dSGeorge Wilson 
1466e6ca193dSGeorge Wilson 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1467e6ca193dSGeorge Wilson 	    search == guid)
1468e6ca193dSGeorge Wilson 		return (B_TRUE);
1469e6ca193dSGeorge Wilson 
1470e6ca193dSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1471e6ca193dSGeorge Wilson 	    &child, &children) == 0) {
1472e6ca193dSGeorge Wilson 		for (c = 0; c < children; c++)
1473e6ca193dSGeorge Wilson 			if (find_vdev(child[c], search))
1474e6ca193dSGeorge Wilson 				return (B_TRUE);
1475e6ca193dSGeorge Wilson 	}
1476e6ca193dSGeorge Wilson 
1477e6ca193dSGeorge Wilson 	return (B_FALSE);
1478e6ca193dSGeorge Wilson }
1479e6ca193dSGeorge Wilson 
1480e6ca193dSGeorge Wilson static int
1481e6ca193dSGeorge Wilson find_spare(zpool_handle_t *zhp, void *data)
1482e6ca193dSGeorge Wilson {
1483e6ca193dSGeorge Wilson 	spare_cbdata_t *cbp = data;
1484e6ca193dSGeorge Wilson 	nvlist_t *config, *nvroot;
1485e6ca193dSGeorge Wilson 
1486e6ca193dSGeorge Wilson 	config = zpool_get_config(zhp, NULL);
1487e6ca193dSGeorge Wilson 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1488e6ca193dSGeorge Wilson 	    &nvroot) == 0);
1489e6ca193dSGeorge Wilson 
1490e6ca193dSGeorge Wilson 	if (find_vdev(nvroot, cbp->cb_guid)) {
1491e6ca193dSGeorge Wilson 		cbp->cb_zhp = zhp;
1492e6ca193dSGeorge Wilson 		return (1);
1493e6ca193dSGeorge Wilson 	}
1494e6ca193dSGeorge Wilson 
1495e6ca193dSGeorge Wilson 	zpool_close(zhp);
1496e6ca193dSGeorge Wilson 	return (0);
1497e6ca193dSGeorge Wilson }
1498e6ca193dSGeorge Wilson 
1499e6ca193dSGeorge Wilson /*
1500e6ca193dSGeorge Wilson  * Print out configuration state as requested by status_callback.
1501e6ca193dSGeorge Wilson  */
1502e6ca193dSGeorge Wilson void
1503e6ca193dSGeorge Wilson print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1504e6ca193dSGeorge Wilson     int namewidth, int depth, boolean_t isspare)
1505e6ca193dSGeorge Wilson {
1506e6ca193dSGeorge Wilson 	nvlist_t **child;
1507e6ca193dSGeorge Wilson 	uint_t c, children;
15083f9d6ad7SLin Ling 	pool_scan_stat_t *ps = NULL;
1509e6ca193dSGeorge Wilson 	vdev_stat_t *vs;
15103f9d6ad7SLin Ling 	char rbuf[6], wbuf[6], cbuf[6];
1511e6ca193dSGeorge Wilson 	char *vname;
1512e6ca193dSGeorge Wilson 	uint64_t notpresent;
1513e6ca193dSGeorge Wilson 	spare_cbdata_t cb;
15146401734dSWill Andrews 	const char *state;
15155cabbc6bSPrashanth Sreenivasa 	char *type;
1516e6ca193dSGeorge Wilson 
1517e6ca193dSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1518e6ca193dSGeorge Wilson 	    &child, &children) != 0)
1519e6ca193dSGeorge Wilson 		children = 0;
1520e6ca193dSGeorge Wilson 
15213f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
15223f9d6ad7SLin Ling 	    (uint64_t **)&vs, &c) == 0);
15233f9d6ad7SLin Ling 
15245cabbc6bSPrashanth Sreenivasa 	verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
15255cabbc6bSPrashanth Sreenivasa 
15265cabbc6bSPrashanth Sreenivasa 	if (strcmp(type, VDEV_TYPE_INDIRECT) == 0)
15275cabbc6bSPrashanth Sreenivasa 		return;
15285cabbc6bSPrashanth Sreenivasa 
1529e6ca193dSGeorge Wilson 	state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1530e6ca193dSGeorge Wilson 	if (isspare) {
1531e6ca193dSGeorge Wilson 		/*
1532e6ca193dSGeorge Wilson 		 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1533e6ca193dSGeorge Wilson 		 * online drives.
1534e6ca193dSGeorge Wilson 		 */
1535e6ca193dSGeorge Wilson 		if (vs->vs_aux == VDEV_AUX_SPARED)
1536e6ca193dSGeorge Wilson 			state = "INUSE";
1537e6ca193dSGeorge Wilson 		else if (vs->vs_state == VDEV_STATE_HEALTHY)
1538e6ca193dSGeorge Wilson 			state = "AVAIL";
1539e6ca193dSGeorge Wilson 	}
1540e6ca193dSGeorge Wilson 
1541e6ca193dSGeorge Wilson 	(void) printf("\t%*s%-*s  %-8s", depth, "", namewidth - depth,
1542e6ca193dSGeorge Wilson 	    name, state);
1543e6ca193dSGeorge Wilson 
1544e6ca193dSGeorge Wilson 	if (!isspare) {
1545e6ca193dSGeorge Wilson 		zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1546e6ca193dSGeorge Wilson 		zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1547e6ca193dSGeorge Wilson 		zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1548e6ca193dSGeorge Wilson 		(void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1549e6ca193dSGeorge Wilson 	}
1550e6ca193dSGeorge Wilson 
1551e6ca193dSGeorge Wilson 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1552e6ca193dSGeorge Wilson 	    &notpresent) == 0) {
1553e6ca193dSGeorge Wilson 		char *path;
1554e6ca193dSGeorge Wilson 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1555e6ca193dSGeorge Wilson 		(void) printf("  was %s", path);
1556e6ca193dSGeorge Wilson 	} else if (vs->vs_aux != 0) {
1557e6ca193dSGeorge Wilson 		(void) printf("  ");
1558e6ca193dSGeorge Wilson 
1559e6ca193dSGeorge Wilson 		switch (vs->vs_aux) {
1560e6ca193dSGeorge Wilson 		case VDEV_AUX_OPEN_FAILED:
1561e6ca193dSGeorge Wilson 			(void) printf(gettext("cannot open"));
1562e6ca193dSGeorge Wilson 			break;
1563e6ca193dSGeorge Wilson 
1564e6ca193dSGeorge Wilson 		case VDEV_AUX_BAD_GUID_SUM:
1565e6ca193dSGeorge Wilson 			(void) printf(gettext("missing device"));
1566e6ca193dSGeorge Wilson 			break;
1567e6ca193dSGeorge Wilson 
1568e6ca193dSGeorge Wilson 		case VDEV_AUX_NO_REPLICAS:
1569e6ca193dSGeorge Wilson 			(void) printf(gettext("insufficient replicas"));
1570e6ca193dSGeorge Wilson 			break;
1571e6ca193dSGeorge Wilson 
1572e6ca193dSGeorge Wilson 		case VDEV_AUX_VERSION_NEWER:
1573e6ca193dSGeorge Wilson 			(void) printf(gettext("newer version"));
1574e6ca193dSGeorge Wilson 			break;
1575e6ca193dSGeorge Wilson 
1576ad135b5dSChristopher Siden 		case VDEV_AUX_UNSUP_FEAT:
1577ad135b5dSChristopher Siden 			(void) printf(gettext("unsupported feature(s)"));
1578ad135b5dSChristopher Siden 			break;
1579ad135b5dSChristopher Siden 
1580e6ca193dSGeorge Wilson 		case VDEV_AUX_SPARED:
1581e6ca193dSGeorge Wilson 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1582e6ca193dSGeorge Wilson 			    &cb.cb_guid) == 0);
1583e6ca193dSGeorge Wilson 			if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1584e6ca193dSGeorge Wilson 				if (strcmp(zpool_get_name(cb.cb_zhp),
1585e6ca193dSGeorge Wilson 				    zpool_get_name(zhp)) == 0)
1586e6ca193dSGeorge Wilson 					(void) printf(gettext("currently in "
1587e6ca193dSGeorge Wilson 					    "use"));
1588e6ca193dSGeorge Wilson 				else
1589e6ca193dSGeorge Wilson 					(void) printf(gettext("in use by "
1590e6ca193dSGeorge Wilson 					    "pool '%s'"),
1591e6ca193dSGeorge Wilson 					    zpool_get_name(cb.cb_zhp));
1592e6ca193dSGeorge Wilson 				zpool_close(cb.cb_zhp);
1593e6ca193dSGeorge Wilson 			} else {
1594e6ca193dSGeorge Wilson 				(void) printf(gettext("currently in use"));
1595e6ca193dSGeorge Wilson 			}
1596e6ca193dSGeorge Wilson 			break;
1597e6ca193dSGeorge Wilson 
1598e6ca193dSGeorge Wilson 		case VDEV_AUX_ERR_EXCEEDED:
1599e6ca193dSGeorge Wilson 			(void) printf(gettext("too many errors"));
1600e6ca193dSGeorge Wilson 			break;
1601e6ca193dSGeorge Wilson 
1602e6ca193dSGeorge Wilson 		case VDEV_AUX_IO_FAILURE:
1603e6ca193dSGeorge Wilson 			(void) printf(gettext("experienced I/O failures"));
1604e6ca193dSGeorge Wilson 			break;
1605e6ca193dSGeorge Wilson 
1606e6ca193dSGeorge Wilson 		case VDEV_AUX_BAD_LOG:
1607e6ca193dSGeorge Wilson 			(void) printf(gettext("bad intent log"));
1608e6ca193dSGeorge Wilson 			break;
1609e6ca193dSGeorge Wilson 
1610069f55e2SEric Schrock 		case VDEV_AUX_EXTERNAL:
1611069f55e2SEric Schrock 			(void) printf(gettext("external device fault"));
1612069f55e2SEric Schrock 			break;
1613069f55e2SEric Schrock 
16141195e687SMark J Musante 		case VDEV_AUX_SPLIT_POOL:
16151195e687SMark J Musante 			(void) printf(gettext("split into new pool"));
16161195e687SMark J Musante 			break;
16171195e687SMark J Musante 
16186f793812SPavel Zakharov 		case VDEV_AUX_CHILDREN_OFFLINE:
16196f793812SPavel Zakharov 			(void) printf(gettext("all children offline"));
16206f793812SPavel Zakharov 			break;
16216f793812SPavel Zakharov 
1622e6ca193dSGeorge Wilson 		default:
1623e6ca193dSGeorge Wilson 			(void) printf(gettext("corrupted data"));
1624e6ca193dSGeorge Wilson 			break;
1625e6ca193dSGeorge Wilson 		}
16263f9d6ad7SLin Ling 	}
16273f9d6ad7SLin Ling 
16283f9d6ad7SLin Ling 	(void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
16293f9d6ad7SLin Ling 	    (uint64_t **)&ps, &c);
16303f9d6ad7SLin Ling 
16313f9d6ad7SLin Ling 	if (ps && ps->pss_state == DSS_SCANNING &&
16323f9d6ad7SLin Ling 	    vs->vs_scan_processed != 0 && children == 0) {
16333f9d6ad7SLin Ling 		(void) printf(gettext("  (%s)"),
16343f9d6ad7SLin Ling 		    (ps->pss_func == POOL_SCAN_RESILVER) ?
16353f9d6ad7SLin Ling 		    "resilvering" : "repairing");
1636e6ca193dSGeorge Wilson 	}
1637e6ca193dSGeorge Wilson 
1638094e47e9SGeorge Wilson 	if ((vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE ||
1639094e47e9SGeorge Wilson 	    vs->vs_initialize_state == VDEV_INITIALIZE_SUSPENDED ||
1640094e47e9SGeorge Wilson 	    vs->vs_initialize_state == VDEV_INITIALIZE_COMPLETE) &&
1641094e47e9SGeorge Wilson 	    !vs->vs_scan_removing) {
1642094e47e9SGeorge Wilson 		char zbuf[1024];
1643094e47e9SGeorge Wilson 		char tbuf[256];
1644094e47e9SGeorge Wilson 		struct tm zaction_ts;
1645094e47e9SGeorge Wilson 
1646094e47e9SGeorge Wilson 		time_t t = vs->vs_initialize_action_time;
1647094e47e9SGeorge Wilson 		int initialize_pct = 100;
1648094e47e9SGeorge Wilson 		if (vs->vs_initialize_state != VDEV_INITIALIZE_COMPLETE) {
1649094e47e9SGeorge Wilson 			initialize_pct = (vs->vs_initialize_bytes_done * 100 /
1650094e47e9SGeorge Wilson 			    (vs->vs_initialize_bytes_est + 1));
1651094e47e9SGeorge Wilson 		}
1652094e47e9SGeorge Wilson 
1653094e47e9SGeorge Wilson 		(void) localtime_r(&t, &zaction_ts);
1654094e47e9SGeorge Wilson 		(void) strftime(tbuf, sizeof (tbuf), "%c", &zaction_ts);
1655094e47e9SGeorge Wilson 
1656094e47e9SGeorge Wilson 		switch (vs->vs_initialize_state) {
1657094e47e9SGeorge Wilson 		case VDEV_INITIALIZE_SUSPENDED:
1658094e47e9SGeorge Wilson 			(void) snprintf(zbuf, sizeof (zbuf),
1659094e47e9SGeorge Wilson 			    ", suspended, started at %s", tbuf);
1660094e47e9SGeorge Wilson 			break;
1661094e47e9SGeorge Wilson 		case VDEV_INITIALIZE_ACTIVE:
1662094e47e9SGeorge Wilson 			(void) snprintf(zbuf, sizeof (zbuf),
1663094e47e9SGeorge Wilson 			    ", started at %s", tbuf);
1664094e47e9SGeorge Wilson 			break;
1665094e47e9SGeorge Wilson 		case VDEV_INITIALIZE_COMPLETE:
1666094e47e9SGeorge Wilson 			(void) snprintf(zbuf, sizeof (zbuf),
1667094e47e9SGeorge Wilson 			    ", completed at %s", tbuf);
1668094e47e9SGeorge Wilson 			break;
1669094e47e9SGeorge Wilson 		}
1670094e47e9SGeorge Wilson 
1671094e47e9SGeorge Wilson 		(void) printf(gettext("  (%d%% initialized%s)"),
1672094e47e9SGeorge Wilson 		    initialize_pct, zbuf);
1673094e47e9SGeorge Wilson 	}
1674094e47e9SGeorge Wilson 
1675e6ca193dSGeorge Wilson 	(void) printf("\n");
1676e6ca193dSGeorge Wilson 
1677e6ca193dSGeorge Wilson 	for (c = 0; c < children; c++) {
167888ecc943SGeorge Wilson 		uint64_t islog = B_FALSE, ishole = B_FALSE;
1679e6ca193dSGeorge Wilson 
168088ecc943SGeorge Wilson 		/* Don't print logs or holes here */
1681e6ca193dSGeorge Wilson 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
168288ecc943SGeorge Wilson 		    &islog);
168388ecc943SGeorge Wilson 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
168488ecc943SGeorge Wilson 		    &ishole);
168588ecc943SGeorge Wilson 		if (islog || ishole)
1686e6ca193dSGeorge Wilson 			continue;
168788ecc943SGeorge Wilson 		vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1688e6ca193dSGeorge Wilson 		print_status_config(zhp, vname, child[c],
1689e6ca193dSGeorge Wilson 		    namewidth, depth + 2, isspare);
1690e6ca193dSGeorge Wilson 		free(vname);
1691e6ca193dSGeorge Wilson 	}
1692e6ca193dSGeorge Wilson }
1693e6ca193dSGeorge Wilson 
1694fa9e4066Sahrens 
1695fa9e4066Sahrens /*
1696fa9e4066Sahrens  * Print the configuration of an exported pool.  Iterate over all vdevs in the
1697fa9e4066Sahrens  * pool, printing out the name and status for each one.
1698fa9e4066Sahrens  */
1699fa9e4066Sahrens void
1700e6ca193dSGeorge Wilson print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1701fa9e4066Sahrens {
1702fa9e4066Sahrens 	nvlist_t **child;
1703fa9e4066Sahrens 	uint_t c, children;
1704fa9e4066Sahrens 	vdev_stat_t *vs;
1705afefbcddSeschrock 	char *type, *vname;
1706fa9e4066Sahrens 
1707fa9e4066Sahrens 	verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
170888ecc943SGeorge Wilson 	if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
170988ecc943SGeorge Wilson 	    strcmp(type, VDEV_TYPE_HOLE) == 0)
1710fa9e4066Sahrens 		return;
1711fa9e4066Sahrens 
17123f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1713fa9e4066Sahrens 	    (uint64_t **)&vs, &c) == 0);
1714fa9e4066Sahrens 
1715fa9e4066Sahrens 	(void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1716990b4856Slling 	(void) printf("  %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1717fa9e4066Sahrens 
1718fa9e4066Sahrens 	if (vs->vs_aux != 0) {
17193d7072f8Seschrock 		(void) printf("  ");
1720fa9e4066Sahrens 
1721fa9e4066Sahrens 		switch (vs->vs_aux) {
1722fa9e4066Sahrens 		case VDEV_AUX_OPEN_FAILED:
1723fa9e4066Sahrens 			(void) printf(gettext("cannot open"));
1724fa9e4066Sahrens 			break;
1725fa9e4066Sahrens 
1726fa9e4066Sahrens 		case VDEV_AUX_BAD_GUID_SUM:
1727fa9e4066Sahrens 			(void) printf(gettext("missing device"));
1728fa9e4066Sahrens 			break;
1729fa9e4066Sahrens 
1730fa9e4066Sahrens 		case VDEV_AUX_NO_REPLICAS:
1731fa9e4066Sahrens 			(void) printf(gettext("insufficient replicas"));
1732fa9e4066Sahrens 			break;
1733fa9e4066Sahrens 
1734eaca9bbdSeschrock 		case VDEV_AUX_VERSION_NEWER:
1735eaca9bbdSeschrock 			(void) printf(gettext("newer version"));
1736eaca9bbdSeschrock 			break;
1737eaca9bbdSeschrock 
1738ad135b5dSChristopher Siden 		case VDEV_AUX_UNSUP_FEAT:
1739ad135b5dSChristopher Siden 			(void) printf(gettext("unsupported feature(s)"));
1740ad135b5dSChristopher Siden 			break;
1741ad135b5dSChristopher Siden 
17423d7072f8Seschrock 		case VDEV_AUX_ERR_EXCEEDED:
17433d7072f8Seschrock 			(void) printf(gettext("too many errors"));
17443d7072f8Seschrock 			break;
17453d7072f8Seschrock 
17466f793812SPavel Zakharov 		case VDEV_AUX_CHILDREN_OFFLINE:
17476f793812SPavel Zakharov 			(void) printf(gettext("all children offline"));
17486f793812SPavel Zakharov 			break;
17496f793812SPavel Zakharov 
1750fa9e4066Sahrens 		default:
1751fa9e4066Sahrens 			(void) printf(gettext("corrupted data"));
1752fa9e4066Sahrens 			break;
1753fa9e4066Sahrens 		}
1754fa9e4066Sahrens 	}
1755fa9e4066Sahrens 	(void) printf("\n");
1756fa9e4066Sahrens 
1757fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1758fa9e4066Sahrens 	    &child, &children) != 0)
1759fa9e4066Sahrens 		return;
1760fa9e4066Sahrens 
1761afefbcddSeschrock 	for (c = 0; c < children; c++) {
17628654d025Sperrin 		uint64_t is_log = B_FALSE;
17638654d025Sperrin 
17648654d025Sperrin 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
17658654d025Sperrin 		    &is_log);
1766e6ca193dSGeorge Wilson 		if (is_log)
17678654d025Sperrin 			continue;
17688654d025Sperrin 
176988ecc943SGeorge Wilson 		vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1770e6ca193dSGeorge Wilson 		print_import_config(vname, child[c], namewidth, depth + 2);
1771afefbcddSeschrock 		free(vname);
1772afefbcddSeschrock 	}
177399653d4eSeschrock 
1774fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1775fa94a07fSbrendan 	    &child, &children) == 0) {
1776fa94a07fSbrendan 		(void) printf(gettext("\tcache\n"));
1777fa94a07fSbrendan 		for (c = 0; c < children; c++) {
177888ecc943SGeorge Wilson 			vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1779fa94a07fSbrendan 			(void) printf("\t  %s\n", vname);
1780fa94a07fSbrendan 			free(vname);
1781fa94a07fSbrendan 		}
1782fa94a07fSbrendan 	}
178399653d4eSeschrock 
1784fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1785fa94a07fSbrendan 	    &child, &children) == 0) {
1786fa94a07fSbrendan 		(void) printf(gettext("\tspares\n"));
1787fa94a07fSbrendan 		for (c = 0; c < children; c++) {
178888ecc943SGeorge Wilson 			vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1789fa94a07fSbrendan 			(void) printf("\t  %s\n", vname);
1790fa94a07fSbrendan 			free(vname);
1791fa94a07fSbrendan 		}
179299653d4eSeschrock 	}
1793fa9e4066Sahrens }
1794fa9e4066Sahrens 
1795e6ca193dSGeorge Wilson /*
1796e6ca193dSGeorge Wilson  * Print log vdevs.
1797e6ca193dSGeorge Wilson  * Logs are recorded as top level vdevs in the main pool child array
1798e6ca193dSGeorge Wilson  * but with "is_log" set to 1. We use either print_status_config() or
1799e6ca193dSGeorge Wilson  * print_import_config() to print the top level logs then any log
1800e6ca193dSGeorge Wilson  * children (eg mirrored slogs) are printed recursively - which
1801e6ca193dSGeorge Wilson  * works because only the top level vdev is marked "is_log"
1802e6ca193dSGeorge Wilson  */
1803e6ca193dSGeorge Wilson static void
1804e6ca193dSGeorge Wilson print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1805e6ca193dSGeorge Wilson {
1806e6ca193dSGeorge Wilson 	uint_t c, children;
1807e6ca193dSGeorge Wilson 	nvlist_t **child;
1808e6ca193dSGeorge Wilson 
1809e6ca193dSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1810e6ca193dSGeorge Wilson 	    &children) != 0)
1811e6ca193dSGeorge Wilson 		return;
1812e6ca193dSGeorge Wilson 
1813e6ca193dSGeorge Wilson 	(void) printf(gettext("\tlogs\n"));
1814e6ca193dSGeorge Wilson 
1815e6ca193dSGeorge Wilson 	for (c = 0; c < children; c++) {
1816e6ca193dSGeorge Wilson 		uint64_t is_log = B_FALSE;
1817e6ca193dSGeorge Wilson 		char *name;
1818e6ca193dSGeorge Wilson 
1819e6ca193dSGeorge Wilson 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1820e6ca193dSGeorge Wilson 		    &is_log);
1821e6ca193dSGeorge Wilson 		if (!is_log)
1822e6ca193dSGeorge Wilson 			continue;
182388ecc943SGeorge Wilson 		name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1824e6ca193dSGeorge Wilson 		if (verbose)
1825e6ca193dSGeorge Wilson 			print_status_config(zhp, name, child[c], namewidth,
1826e6ca193dSGeorge Wilson 			    2, B_FALSE);
1827e6ca193dSGeorge Wilson 		else
1828e6ca193dSGeorge Wilson 			print_import_config(name, child[c], namewidth, 2);
1829e6ca193dSGeorge Wilson 		free(name);
1830e6ca193dSGeorge Wilson 	}
1831e6ca193dSGeorge Wilson }
1832468c413aSTim Haley 
1833fa9e4066Sahrens /*
1834fa9e4066Sahrens  * Display the status for the given pool.
1835fa9e4066Sahrens  */
1836fa9e4066Sahrens static void
1837fa9e4066Sahrens show_import(nvlist_t *config)
1838fa9e4066Sahrens {
1839fa9e4066Sahrens 	uint64_t pool_state;
1840fa9e4066Sahrens 	vdev_stat_t *vs;
1841fa9e4066Sahrens 	char *name;
1842fa9e4066Sahrens 	uint64_t guid;
1843fa9e4066Sahrens 	char *msgid;
1844fa9e4066Sahrens 	nvlist_t *nvroot;
1845fa9e4066Sahrens 	int reason;
184646657f8dSmmusante 	const char *health;
1847fa9e4066Sahrens 	uint_t vsc;
1848fa9e4066Sahrens 	int namewidth;
18498704186eSDan McDonald 	char *comment;
1850fa9e4066Sahrens 
1851fa9e4066Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1852fa9e4066Sahrens 	    &name) == 0);
1853fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1854fa9e4066Sahrens 	    &guid) == 0);
1855fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1856fa9e4066Sahrens 	    &pool_state) == 0);
1857fa9e4066Sahrens 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1858fa9e4066Sahrens 	    &nvroot) == 0);
1859fa9e4066Sahrens 
18603f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1861fa9e4066Sahrens 	    (uint64_t **)&vs, &vsc) == 0);
1862990b4856Slling 	health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1863fa9e4066Sahrens 
1864fa9e4066Sahrens 	reason = zpool_import_status(config, &msgid);
1865fa9e4066Sahrens 
18668704186eSDan McDonald 	(void) printf(gettext("   pool: %s\n"), name);
18678704186eSDan McDonald 	(void) printf(gettext("     id: %llu\n"), (u_longlong_t)guid);
18688704186eSDan McDonald 	(void) printf(gettext("  state: %s"), health);
18694c58d714Sdarrenm 	if (pool_state == POOL_STATE_DESTROYED)
1870b1b8ab34Slling 		(void) printf(gettext(" (DESTROYED)"));
18714c58d714Sdarrenm 	(void) printf("\n");
1872fa9e4066Sahrens 
1873fa9e4066Sahrens 	switch (reason) {
1874fa9e4066Sahrens 	case ZPOOL_STATUS_MISSING_DEV_R:
1875fa9e4066Sahrens 	case ZPOOL_STATUS_MISSING_DEV_NR:
1876fa9e4066Sahrens 	case ZPOOL_STATUS_BAD_GUID_SUM:
18778704186eSDan McDonald 		(void) printf(gettext(" status: One or more devices are "
18788704186eSDan McDonald 		    "missing from the system.\n"));
1879fa9e4066Sahrens 		break;
1880fa9e4066Sahrens 
1881fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_LABEL_R:
1882fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_LABEL_NR:
18838704186eSDan McDonald 		(void) printf(gettext(" status: One or more devices contains "
1884fa9e4066Sahrens 		    "corrupted data.\n"));
1885fa9e4066Sahrens 		break;
1886fa9e4066Sahrens 
1887fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_DATA:
18888704186eSDan McDonald 		(void) printf(
18898704186eSDan McDonald 		    gettext(" status: The pool data is corrupted.\n"));
1890fa9e4066Sahrens 		break;
1891fa9e4066Sahrens 
1892441d80aaSlling 	case ZPOOL_STATUS_OFFLINE_DEV:
18938704186eSDan McDonald 		(void) printf(gettext(" status: One or more devices "
1894441d80aaSlling 		    "are offlined.\n"));
1895441d80aaSlling 		break;
1896441d80aaSlling 
1897ea8dc4b6Seschrock 	case ZPOOL_STATUS_CORRUPT_POOL:
18988704186eSDan McDonald 		(void) printf(gettext(" status: The pool metadata is "
1899ea8dc4b6Seschrock 		    "corrupted.\n"));
1900ea8dc4b6Seschrock 		break;
1901ea8dc4b6Seschrock 
1902eaca9bbdSeschrock 	case ZPOOL_STATUS_VERSION_OLDER:
190357221772SChristopher Siden 		(void) printf(gettext(" status: The pool is formatted using a "
190457221772SChristopher Siden 		    "legacy on-disk version.\n"));
1905eaca9bbdSeschrock 		break;
1906eaca9bbdSeschrock 
1907eaca9bbdSeschrock 	case ZPOOL_STATUS_VERSION_NEWER:
19088704186eSDan McDonald 		(void) printf(gettext(" status: The pool is formatted using an "
1909eaca9bbdSeschrock 		    "incompatible version.\n"));
1910eaca9bbdSeschrock 		break;
1911b87f3af3Sperrin 
191257221772SChristopher Siden 	case ZPOOL_STATUS_FEAT_DISABLED:
191357221772SChristopher Siden 		(void) printf(gettext(" status: Some supported features are "
191457221772SChristopher Siden 		    "not enabled on the pool.\n"));
191557221772SChristopher Siden 		break;
191657221772SChristopher Siden 
1917ad135b5dSChristopher Siden 	case ZPOOL_STATUS_UNSUP_FEAT_READ:
1918ad135b5dSChristopher Siden 		(void) printf(gettext("status: The pool uses the following "
1919edc8ef7dSToomas Soome 		    "feature(s) not supported on this system:\n"));
1920ad135b5dSChristopher Siden 		zpool_print_unsup_feat(config);
1921ad135b5dSChristopher Siden 		break;
1922ad135b5dSChristopher Siden 
1923ad135b5dSChristopher Siden 	case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1924ad135b5dSChristopher Siden 		(void) printf(gettext("status: The pool can only be accessed "
1925ad135b5dSChristopher Siden 		    "in read-only mode on this system. It\n\tcannot be "
1926ad135b5dSChristopher Siden 		    "accessed in read-write mode because it uses the "
1927ad135b5dSChristopher Siden 		    "following\n\tfeature(s) not supported on this system:\n"));
1928ad135b5dSChristopher Siden 		zpool_print_unsup_feat(config);
1929ad135b5dSChristopher Siden 		break;
1930ad135b5dSChristopher Siden 
193195173954Sek 	case ZPOOL_STATUS_HOSTID_MISMATCH:
19328704186eSDan McDonald 		(void) printf(gettext(" status: The pool was last accessed by "
193395173954Sek 		    "another system.\n"));
193495173954Sek 		break;
1935b87f3af3Sperrin 
19363d7072f8Seschrock 	case ZPOOL_STATUS_FAULTED_DEV_R:
19373d7072f8Seschrock 	case ZPOOL_STATUS_FAULTED_DEV_NR:
19388704186eSDan McDonald 		(void) printf(gettext(" status: One or more devices are "
19393d7072f8Seschrock 		    "faulted.\n"));
19403d7072f8Seschrock 		break;
19413d7072f8Seschrock 
1942b87f3af3Sperrin 	case ZPOOL_STATUS_BAD_LOG:
19438704186eSDan McDonald 		(void) printf(gettext(" status: An intent log record cannot be "
1944b87f3af3Sperrin 		    "read.\n"));
1945b87f3af3Sperrin 		break;
1946b87f3af3Sperrin 
19473f9d6ad7SLin Ling 	case ZPOOL_STATUS_RESILVERING:
19488704186eSDan McDonald 		(void) printf(gettext(" status: One or more devices were being "
19493f9d6ad7SLin Ling 		    "resilvered.\n"));
19503f9d6ad7SLin Ling 		break;
19513f9d6ad7SLin Ling 
1952fa9e4066Sahrens 	default:
1953fa9e4066Sahrens 		/*
1954fa9e4066Sahrens 		 * No other status can be seen when importing pools.
1955fa9e4066Sahrens 		 */
1956fa9e4066Sahrens 		assert(reason == ZPOOL_STATUS_OK);
1957fa9e4066Sahrens 	}
1958fa9e4066Sahrens 
1959fa9e4066Sahrens 	/*
1960fa9e4066Sahrens 	 * Print out an action according to the overall state of the pool.
1961fa9e4066Sahrens 	 */
196246657f8dSmmusante 	if (vs->vs_state == VDEV_STATE_HEALTHY) {
196357221772SChristopher Siden 		if (reason == ZPOOL_STATUS_VERSION_OLDER ||
196457221772SChristopher Siden 		    reason == ZPOOL_STATUS_FEAT_DISABLED) {
19658704186eSDan McDonald 			(void) printf(gettext(" action: The pool can be "
1966eaca9bbdSeschrock 			    "imported using its name or numeric identifier, "
1967eaca9bbdSeschrock 			    "though\n\tsome features will not be available "
1968eaca9bbdSeschrock 			    "without an explicit 'zpool upgrade'.\n"));
196957221772SChristopher Siden 		} else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
19708704186eSDan McDonald 			(void) printf(gettext(" action: The pool can be "
197195173954Sek 			    "imported using its name or numeric "
197295173954Sek 			    "identifier and\n\tthe '-f' flag.\n"));
197357221772SChristopher Siden 		} else {
19748704186eSDan McDonald 			(void) printf(gettext(" action: The pool can be "
1975eaca9bbdSeschrock 			    "imported using its name or numeric "
1976eaca9bbdSeschrock 			    "identifier.\n"));
197757221772SChristopher Siden 		}
197846657f8dSmmusante 	} else if (vs->vs_state == VDEV_STATE_DEGRADED) {
19798704186eSDan McDonald 		(void) printf(gettext(" action: The pool can be imported "
1980fa9e4066Sahrens 		    "despite missing or damaged devices.  The\n\tfault "
1981eaca9bbdSeschrock 		    "tolerance of the pool may be compromised if imported.\n"));
1982fa9e4066Sahrens 	} else {
1983eaca9bbdSeschrock 		switch (reason) {
1984eaca9bbdSeschrock 		case ZPOOL_STATUS_VERSION_NEWER:
19858704186eSDan McDonald 			(void) printf(gettext(" action: The pool cannot be "
1986eaca9bbdSeschrock 			    "imported.  Access the pool on a system running "
1987eaca9bbdSeschrock 			    "newer\n\tsoftware, or recreate the pool from "
1988eaca9bbdSeschrock 			    "backup.\n"));
1989eaca9bbdSeschrock 			break;
1990ad135b5dSChristopher Siden 		case ZPOOL_STATUS_UNSUP_FEAT_READ:
1991ad135b5dSChristopher Siden 			(void) printf(gettext("action: The pool cannot be "
1992ad135b5dSChristopher Siden 			    "imported. Access the pool on a system that "
1993ad135b5dSChristopher Siden 			    "supports\n\tthe required feature(s), or recreate "
1994ad135b5dSChristopher Siden 			    "the pool from backup.\n"));
1995ad135b5dSChristopher Siden 			break;
1996ad135b5dSChristopher Siden 		case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1997ad135b5dSChristopher Siden 			(void) printf(gettext("action: The pool cannot be "
1998ad135b5dSChristopher Siden 			    "imported in read-write mode. Import the pool "
1999ad135b5dSChristopher Siden 			    "with\n"
2000ad135b5dSChristopher Siden 			    "\t\"-o readonly=on\", access the pool on a system "
2001ad135b5dSChristopher Siden 			    "that supports the\n\trequired feature(s), or "
2002ad135b5dSChristopher Siden 			    "recreate the pool from backup.\n"));
2003ad135b5dSChristopher Siden 			break;
2004eaca9bbdSeschrock 		case ZPOOL_STATUS_MISSING_DEV_R:
2005eaca9bbdSeschrock 		case ZPOOL_STATUS_MISSING_DEV_NR:
2006eaca9bbdSeschrock 		case ZPOOL_STATUS_BAD_GUID_SUM:
20078704186eSDan McDonald 			(void) printf(gettext(" action: The pool cannot be "
2008fa9e4066Sahrens 			    "imported. Attach the missing\n\tdevices and try "
2009fa9e4066Sahrens 			    "again.\n"));
2010eaca9bbdSeschrock 			break;
2011eaca9bbdSeschrock 		default:
20128704186eSDan McDonald 			(void) printf(gettext(" action: The pool cannot be "
2013fa9e4066Sahrens 			    "imported due to damaged devices or data.\n"));
2014eaca9bbdSeschrock 		}
2015eaca9bbdSeschrock 	}
2016eaca9bbdSeschrock 
20178704186eSDan McDonald 	/* Print the comment attached to the pool. */
20188704186eSDan McDonald 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
20198704186eSDan McDonald 		(void) printf(gettext("comment: %s\n"), comment);
20208704186eSDan McDonald 
202146657f8dSmmusante 	/*
202246657f8dSmmusante 	 * If the state is "closed" or "can't open", and the aux state
202346657f8dSmmusante 	 * is "corrupt data":
202446657f8dSmmusante 	 */
202546657f8dSmmusante 	if (((vs->vs_state == VDEV_STATE_CLOSED) ||
202646657f8dSmmusante 	    (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
202746657f8dSmmusante 	    (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
2028eaca9bbdSeschrock 		if (pool_state == POOL_STATE_DESTROYED)
2029eaca9bbdSeschrock 			(void) printf(gettext("\tThe pool was destroyed, "
2030eaca9bbdSeschrock 			    "but can be imported using the '-Df' flags.\n"));
2031eaca9bbdSeschrock 		else if (pool_state != POOL_STATE_EXPORTED)
2032eaca9bbdSeschrock 			(void) printf(gettext("\tThe pool may be active on "
203318ce54dfSek 			    "another system, but can be imported using\n\t"
2034eaca9bbdSeschrock 			    "the '-f' flag.\n"));
2035fa9e4066Sahrens 	}
2036fa9e4066Sahrens 
2037fa9e4066Sahrens 	if (msgid != NULL)
2038654b400cSJoshua M. Clulow 		(void) printf(gettext("   see: http://illumos.org/msg/%s\n"),
2039fa9e4066Sahrens 		    msgid);
2040fa9e4066Sahrens 
20418704186eSDan McDonald 	(void) printf(gettext(" config:\n\n"));
2042fa9e4066Sahrens 
2043c67d9675Seschrock 	namewidth = max_width(NULL, nvroot, 0, 0);
2044fa9e4066Sahrens 	if (namewidth < 10)
2045fa9e4066Sahrens 		namewidth = 10;
20468654d025Sperrin 
2047e6ca193dSGeorge Wilson 	print_import_config(name, nvroot, namewidth, 0);
2048e6ca193dSGeorge Wilson 	if (num_logs(nvroot) > 0)
2049e6ca193dSGeorge Wilson 		print_logs(NULL, nvroot, namewidth, B_FALSE);
2050fa9e4066Sahrens 
2051fa9e4066Sahrens 	if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
205246657f8dSmmusante 		(void) printf(gettext("\n\tAdditional devices are known to "
2053fa9e4066Sahrens 		    "be part of this pool, though their\n\texact "
205446657f8dSmmusante 		    "configuration cannot be determined.\n"));
2055fa9e4066Sahrens 	}
2056fa9e4066Sahrens }
2057fa9e4066Sahrens 
2058fa9e4066Sahrens /*
2059fa9e4066Sahrens  * Perform the import for the given configuration.  This passes the heavy
2060990b4856Slling  * lifting off to zpool_import_props(), and then mounts the datasets contained
2061990b4856Slling  * within the pool.
2062fa9e4066Sahrens  */
2063fa9e4066Sahrens static int
2064fa9e4066Sahrens do_import(nvlist_t *config, const char *newname, const char *mntopts,
20654b964adaSGeorge Wilson     nvlist_t *props, int flags)
2066fa9e4066Sahrens {
2067fa9e4066Sahrens 	zpool_handle_t *zhp;
2068fa9e4066Sahrens 	char *name;
2069fa9e4066Sahrens 	uint64_t state;
2070eaca9bbdSeschrock 	uint64_t version;
2071fa9e4066Sahrens 
2072fa9e4066Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
2073fa9e4066Sahrens 	    &name) == 0);
2074fa9e4066Sahrens 
2075fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config,
2076fa9e4066Sahrens 	    ZPOOL_CONFIG_POOL_STATE, &state) == 0);
2077eaca9bbdSeschrock 	verify(nvlist_lookup_uint64(config,
2078eaca9bbdSeschrock 	    ZPOOL_CONFIG_VERSION, &version) == 0);
2079ad135b5dSChristopher Siden 	if (!SPA_VERSION_IS_SUPPORTED(version)) {
2080eaca9bbdSeschrock 		(void) fprintf(stderr, gettext("cannot import '%s': pool "
2081ad135b5dSChristopher Siden 		    "is formatted using an unsupported ZFS version\n"), name);
2082eaca9bbdSeschrock 		return (1);
20834b964adaSGeorge Wilson 	} else if (state != POOL_STATE_EXPORTED &&
20844b964adaSGeorge Wilson 	    !(flags & ZFS_IMPORT_ANY_HOST)) {
208595173954Sek 		uint64_t hostid;
208695173954Sek 
208795173954Sek 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
208895173954Sek 		    &hostid) == 0) {
208995173954Sek 			if ((unsigned long)hostid != gethostid()) {
209095173954Sek 				char *hostname;
209195173954Sek 				uint64_t timestamp;
209295173954Sek 				time_t t;
209395173954Sek 
209495173954Sek 				verify(nvlist_lookup_string(config,
209595173954Sek 				    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
209695173954Sek 				verify(nvlist_lookup_uint64(config,
209795173954Sek 				    ZPOOL_CONFIG_TIMESTAMP, &timestamp) == 0);
209895173954Sek 				t = timestamp;
209995173954Sek 				(void) fprintf(stderr, gettext("cannot import "
210095173954Sek 				    "'%s': pool may be in use from other "
210195173954Sek 				    "system, it was last accessed by %s "
210295173954Sek 				    "(hostid: 0x%lx) on %s"), name, hostname,
210395173954Sek 				    (unsigned long)hostid,
210495173954Sek 				    asctime(localtime(&t)));
210595173954Sek 				(void) fprintf(stderr, gettext("use '-f' to "
210695173954Sek 				    "import anyway\n"));
210795173954Sek 				return (1);
210895173954Sek 			}
210995173954Sek 		} else {
211095173954Sek 			(void) fprintf(stderr, gettext("cannot import '%s': "
211195173954Sek 			    "pool may be in use from other system\n"), name);
211295173954Sek 			(void) fprintf(stderr, gettext("use '-f' to import "
211395173954Sek 			    "anyway\n"));
211495173954Sek 			return (1);
211595173954Sek 		}
2116fa9e4066Sahrens 	}
2117fa9e4066Sahrens 
21184b964adaSGeorge Wilson 	if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
2119fa9e4066Sahrens 		return (1);
2120fa9e4066Sahrens 
2121fa9e4066Sahrens 	if (newname != NULL)
2122fa9e4066Sahrens 		name = (char *)newname;
2123fa9e4066Sahrens 
21244f0f5e5bSVictor Latushkin 	if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
21254f0f5e5bSVictor Latushkin 		return (1);
2126fa9e4066Sahrens 
2127379c004dSEric Schrock 	if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
2128f9af39baSGeorge Wilson 	    !(flags & ZFS_IMPORT_ONLY) &&
2129379c004dSEric Schrock 	    zpool_enable_datasets(zhp, mntopts, 0) != 0) {
2130fa9e4066Sahrens 		zpool_close(zhp);
2131fa9e4066Sahrens 		return (1);
2132fa9e4066Sahrens 	}
2133fa9e4066Sahrens 
2134fa9e4066Sahrens 	zpool_close(zhp);
2135468c413aSTim Haley 	return (0);
2136fa9e4066Sahrens }
2137fa9e4066Sahrens 
213886714001SSerapheim Dimitropoulos /*
213986714001SSerapheim Dimitropoulos  * zpool checkpoint <pool>
214086714001SSerapheim Dimitropoulos  *       checkpoint --discard <pool>
214186714001SSerapheim Dimitropoulos  *
214204e56356SAndriy Gapon  *	-d	Discard the checkpoint from a checkpointed
214304e56356SAndriy Gapon  *	--discard  pool.
214486714001SSerapheim Dimitropoulos  *
214586714001SSerapheim Dimitropoulos  * Checkpoints the specified pool, by taking a "snapshot" of its
214686714001SSerapheim Dimitropoulos  * current state. A pool can only have one checkpoint at a time.
214786714001SSerapheim Dimitropoulos  */
214886714001SSerapheim Dimitropoulos int
214986714001SSerapheim Dimitropoulos zpool_do_checkpoint(int argc, char **argv)
215086714001SSerapheim Dimitropoulos {
215186714001SSerapheim Dimitropoulos 	boolean_t discard;
215286714001SSerapheim Dimitropoulos 	char *pool;
215386714001SSerapheim Dimitropoulos 	zpool_handle_t *zhp;
215486714001SSerapheim Dimitropoulos 	int c, err;
215586714001SSerapheim Dimitropoulos 
215686714001SSerapheim Dimitropoulos 	struct option long_options[] = {
215786714001SSerapheim Dimitropoulos 		{"discard", no_argument, NULL, 'd'},
215886714001SSerapheim Dimitropoulos 		{0, 0, 0, 0}
215986714001SSerapheim Dimitropoulos 	};
216086714001SSerapheim Dimitropoulos 
216186714001SSerapheim Dimitropoulos 	discard = B_FALSE;
216286714001SSerapheim Dimitropoulos 	while ((c = getopt_long(argc, argv, ":d", long_options, NULL)) != -1) {
216386714001SSerapheim Dimitropoulos 		switch (c) {
216486714001SSerapheim Dimitropoulos 		case 'd':
216586714001SSerapheim Dimitropoulos 			discard = B_TRUE;
216686714001SSerapheim Dimitropoulos 			break;
216786714001SSerapheim Dimitropoulos 		case '?':
216886714001SSerapheim Dimitropoulos 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
216986714001SSerapheim Dimitropoulos 			    optopt);
217086714001SSerapheim Dimitropoulos 			usage(B_FALSE);
217186714001SSerapheim Dimitropoulos 		}
217286714001SSerapheim Dimitropoulos 	}
217386714001SSerapheim Dimitropoulos 
217486714001SSerapheim Dimitropoulos 	argc -= optind;
217586714001SSerapheim Dimitropoulos 	argv += optind;
217686714001SSerapheim Dimitropoulos 
217786714001SSerapheim Dimitropoulos 	if (argc < 1) {
217886714001SSerapheim Dimitropoulos 		(void) fprintf(stderr, gettext("missing pool argument\n"));
217986714001SSerapheim Dimitropoulos 		usage(B_FALSE);
218086714001SSerapheim Dimitropoulos 	}
218186714001SSerapheim Dimitropoulos 
218286714001SSerapheim Dimitropoulos 	if (argc > 1) {
218386714001SSerapheim Dimitropoulos 		(void) fprintf(stderr, gettext("too many arguments\n"));
218486714001SSerapheim Dimitropoulos 		usage(B_FALSE);
218586714001SSerapheim Dimitropoulos 	}
218686714001SSerapheim Dimitropoulos 
218786714001SSerapheim Dimitropoulos 	pool = argv[0];
218886714001SSerapheim Dimitropoulos 
218986714001SSerapheim Dimitropoulos 	if ((zhp = zpool_open(g_zfs, pool)) == NULL) {
219086714001SSerapheim Dimitropoulos 		/* As a special case, check for use of '/' in the name */
219186714001SSerapheim Dimitropoulos 		if (strchr(pool, '/') != NULL)
219286714001SSerapheim Dimitropoulos 			(void) fprintf(stderr, gettext("'zpool checkpoint' "
219386714001SSerapheim Dimitropoulos 			    "doesn't work on datasets. To save the state "
219486714001SSerapheim Dimitropoulos 			    "of a dataset from a specific point in time "
219586714001SSerapheim Dimitropoulos 			    "please use 'zfs snapshot'\n"));
219686714001SSerapheim Dimitropoulos 		return (1);
219786714001SSerapheim Dimitropoulos 	}
219886714001SSerapheim Dimitropoulos 
219986714001SSerapheim Dimitropoulos 	if (discard)
220086714001SSerapheim Dimitropoulos 		err = (zpool_discard_checkpoint(zhp) != 0);
220186714001SSerapheim Dimitropoulos 	else
220286714001SSerapheim Dimitropoulos 		err = (zpool_checkpoint(zhp) != 0);
220386714001SSerapheim Dimitropoulos 
220486714001SSerapheim Dimitropoulos 	zpool_close(zhp);
220586714001SSerapheim Dimitropoulos 
220686714001SSerapheim Dimitropoulos 	return (err);
220786714001SSerapheim Dimitropoulos }
220886714001SSerapheim Dimitropoulos 
220986714001SSerapheim Dimitropoulos #define	CHECKPOINT_OPT	1024
221086714001SSerapheim Dimitropoulos 
2211fa9e4066Sahrens /*
22124c58d714Sdarrenm  * zpool import [-d dir] [-D]
22132f8aaab3Seschrock  *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
22142f8aaab3Seschrock  *              [-d dir | -c cachefile] [-f] -a
22152f8aaab3Seschrock  *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
221604e56356SAndriy Gapon  *              [-d dir | -c cachefile] [-f] [-n] [-F] [-t]
221704e56356SAndriy Gapon  *              <pool | id> [newpool]
22182f8aaab3Seschrock  *
221904e56356SAndriy Gapon  *	-c	Read pool information from a cachefile instead of searching
22202f8aaab3Seschrock  *		devices.
2221fa9e4066Sahrens  *
222204e56356SAndriy Gapon  *	-d	Scan in a specific directory, other than /dev/dsk.  More than
2223fa9e4066Sahrens  *		one directory can be specified using multiple '-d' options.
2224fa9e4066Sahrens  *
222504e56356SAndriy Gapon  *	-D	Scan for previously destroyed pools or import all or only
222604e56356SAndriy Gapon  *		specified destroyed pools.
22274c58d714Sdarrenm  *
222804e56356SAndriy Gapon  *	-R	Temporarily import the pool, with all mountpoints relative to
2229fa9e4066Sahrens  *		the given root.  The pool will remain exported when the machine
2230fa9e4066Sahrens  *		is rebooted.
2231fa9e4066Sahrens  *
223204e56356SAndriy Gapon  *	-V	Import even in the presence of faulted vdevs.  This is an
223304e56356SAndriy Gapon  *		intentionally undocumented option for testing purposes, and
223404e56356SAndriy Gapon  *		treats the pool configuration as complete, leaving any bad
22354f0f5e5bSVictor Latushkin  *		vdevs in the FAULTED state. In other words, it does verbatim
22364f0f5e5bSVictor Latushkin  *		import.
2237c5904d13Seschrock  *
223804e56356SAndriy Gapon  *	-f	Force import, even if it appears that the pool is active.
223904e56356SAndriy Gapon  *
224004e56356SAndriy Gapon  *	-F	Attempt rewind if necessary.
2241468c413aSTim Haley  *
224204e56356SAndriy Gapon  *	-n	See if rewind would work, but don't actually rewind.
2243468c413aSTim Haley  *
224404e56356SAndriy Gapon  *	-N	Import the pool but don't mount datasets.
2245468c413aSTim Haley  *
224604e56356SAndriy Gapon  *	-t	Use newpool as a temporary pool name instead of renaming
224704e56356SAndriy Gapon  *		the pool.
2248f9af39baSGeorge Wilson  *
224904e56356SAndriy Gapon  *	-T	Specify a starting txg to use for import. This option is
225004e56356SAndriy Gapon  *		intentionally undocumented option for testing purposes.
2251f9af39baSGeorge Wilson  *
225204e56356SAndriy Gapon  *	-a	Import all pools found.
2253fa9e4066Sahrens  *
225404e56356SAndriy Gapon  *	-o	Set property=value and/or temporary mount options (without '=').
2255ecd6cf80Smarks  *
225604e56356SAndriy Gapon  *	--rewind-to-checkpoint
225704e56356SAndriy Gapon  *		Import the pool and revert back to the checkpoint.
225886714001SSerapheim Dimitropoulos  *
2259fa9e4066Sahrens  * The import command scans for pools to import, and import pools based on pool
2260fa9e4066Sahrens  * name and GUID.  The pool can also be renamed as part of the import process.
2261fa9e4066Sahrens  */
2262fa9e4066Sahrens int
2263fa9e4066Sahrens zpool_do_import(int argc, char **argv)
2264fa9e4066Sahrens {
2265fa9e4066Sahrens 	char **searchdirs = NULL;
2266fa9e4066Sahrens 	int nsearch = 0;
2267fa9e4066Sahrens 	int c;
2268d41c4376SMark J Musante 	int err = 0;
22692f8aaab3Seschrock 	nvlist_t *pools = NULL;
227099653d4eSeschrock 	boolean_t do_all = B_FALSE;
227199653d4eSeschrock 	boolean_t do_destroyed = B_FALSE;
2272fa9e4066Sahrens 	char *mntopts = NULL;
2273fa9e4066Sahrens 	nvpair_t *elem;
2274fa9e4066Sahrens 	nvlist_t *config;
227524e697d4Sck 	uint64_t searchguid = 0;
227624e697d4Sck 	char *searchname = NULL;
2277990b4856Slling 	char *propval;
2278fa9e4066Sahrens 	nvlist_t *found_config;
2279468c413aSTim Haley 	nvlist_t *policy = NULL;
2280ecd6cf80Smarks 	nvlist_t *props = NULL;
228199653d4eSeschrock 	boolean_t first;
22824b964adaSGeorge Wilson 	int flags = ZFS_IMPORT_NORMAL;
2283468c413aSTim Haley 	uint32_t rewind_policy = ZPOOL_NO_REWIND;
2284468c413aSTim Haley 	boolean_t dryrun = B_FALSE;
2285468c413aSTim Haley 	boolean_t do_rewind = B_FALSE;
2286468c413aSTim Haley 	boolean_t xtreme_rewind = B_FALSE;
2287f9af39baSGeorge Wilson 	uint64_t pool_state, txg = -1ULL;
22882f8aaab3Seschrock 	char *cachefile = NULL;
2289d41c4376SMark J Musante 	importargs_t idata = { 0 };
2290f9af39baSGeorge Wilson 	char *endptr;
2291fa9e4066Sahrens 
229286714001SSerapheim Dimitropoulos 
229386714001SSerapheim Dimitropoulos 	struct option long_options[] = {
229486714001SSerapheim Dimitropoulos 		{"rewind-to-checkpoint", no_argument, NULL, CHECKPOINT_OPT},
229586714001SSerapheim Dimitropoulos 		{0, 0, 0, 0}
229686714001SSerapheim Dimitropoulos 	};
229786714001SSerapheim Dimitropoulos 
2298fa9e4066Sahrens 	/* check options */
229904e56356SAndriy Gapon 	while ((c = getopt_long(argc, argv, ":aCc:d:DEfFmnNo:rR:tT:VX",
230086714001SSerapheim Dimitropoulos 	    long_options, NULL)) != -1) {
2301fa9e4066Sahrens 		switch (c) {
2302fa9e4066Sahrens 		case 'a':
230399653d4eSeschrock 			do_all = B_TRUE;
2304fa9e4066Sahrens 			break;
23052f8aaab3Seschrock 		case 'c':
23062f8aaab3Seschrock 			cachefile = optarg;
23072f8aaab3Seschrock 			break;
2308fa9e4066Sahrens 		case 'd':
2309fa9e4066Sahrens 			if (searchdirs == NULL) {
2310fa9e4066Sahrens 				searchdirs = safe_malloc(sizeof (char *));
2311fa9e4066Sahrens 			} else {
2312fa9e4066Sahrens 				char **tmp = safe_malloc((nsearch + 1) *
2313fa9e4066Sahrens 				    sizeof (char *));
2314fa9e4066Sahrens 				bcopy(searchdirs, tmp, nsearch *
2315fa9e4066Sahrens 				    sizeof (char *));
2316fa9e4066Sahrens 				free(searchdirs);
2317fa9e4066Sahrens 				searchdirs = tmp;
2318fa9e4066Sahrens 			}
2319fa9e4066Sahrens 			searchdirs[nsearch++] = optarg;
2320fa9e4066Sahrens 			break;
23214c58d714Sdarrenm 		case 'D':
232299653d4eSeschrock 			do_destroyed = B_TRUE;
23234c58d714Sdarrenm 			break;
2324fa9e4066Sahrens 		case 'f':
23254b964adaSGeorge Wilson 			flags |= ZFS_IMPORT_ANY_HOST;
2326fa9e4066Sahrens 			break;
2327c5904d13Seschrock 		case 'F':
2328468c413aSTim Haley 			do_rewind = B_TRUE;
2329468c413aSTim Haley 			break;
23304b964adaSGeorge Wilson 		case 'm':
23314b964adaSGeorge Wilson 			flags |= ZFS_IMPORT_MISSING_LOG;
23324b964adaSGeorge Wilson 			break;
2333468c413aSTim Haley 		case 'n':
2334468c413aSTim Haley 			dryrun = B_TRUE;
2335c5904d13Seschrock 			break;
2336f9af39baSGeorge Wilson 		case 'N':
2337f9af39baSGeorge Wilson 			flags |= ZFS_IMPORT_ONLY;
2338f9af39baSGeorge Wilson 			break;
2339fa9e4066Sahrens 		case 'o':
2340990b4856Slling 			if ((propval = strchr(optarg, '=')) != NULL) {
2341990b4856Slling 				*propval = '\0';
2342990b4856Slling 				propval++;
23430a48a24eStimh 				if (add_prop_list(optarg, propval,
23440a48a24eStimh 				    &props, B_TRUE))
2345990b4856Slling 					goto error;
2346990b4856Slling 			} else {
2347990b4856Slling 				mntopts = optarg;
2348990b4856Slling 			}
2349fa9e4066Sahrens 			break;
2350fa9e4066Sahrens 		case 'R':
2351990b4856Slling 			if (add_prop_list(zpool_prop_to_name(
23520a48a24eStimh 			    ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2353990b4856Slling 				goto error;
235404e56356SAndriy Gapon 			if (add_prop_list_default(zpool_prop_to_name(
235504e56356SAndriy Gapon 			    ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
235604e56356SAndriy Gapon 				goto error;
235704e56356SAndriy Gapon 			break;
235804e56356SAndriy Gapon 		case 't':
235904e56356SAndriy Gapon 			flags |= ZFS_IMPORT_TEMP_NAME;
236004e56356SAndriy Gapon 			if (add_prop_list_default(zpool_prop_to_name(
23610a48a24eStimh 			    ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2362990b4856Slling 				goto error;
2363fa9e4066Sahrens 			break;
2364f9af39baSGeorge Wilson 		case 'T':
2365f9af39baSGeorge Wilson 			errno = 0;
2366e42d2059SMatthew Ahrens 			txg = strtoull(optarg, &endptr, 0);
2367f9af39baSGeorge Wilson 			if (errno != 0 || *endptr != '\0') {
2368f9af39baSGeorge Wilson 				(void) fprintf(stderr,
2369f9af39baSGeorge Wilson 				    gettext("invalid txg value\n"));
2370f9af39baSGeorge Wilson 				usage(B_FALSE);
2371f9af39baSGeorge Wilson 			}
2372f9af39baSGeorge Wilson 			rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2373f9af39baSGeorge Wilson 			break;
2374468c413aSTim Haley 		case 'V':
23754b964adaSGeorge Wilson 			flags |= ZFS_IMPORT_VERBATIM;
2376468c413aSTim Haley 			break;
2377468c413aSTim Haley 		case 'X':
2378468c413aSTim Haley 			xtreme_rewind = B_TRUE;
2379468c413aSTim Haley 			break;
238086714001SSerapheim Dimitropoulos 		case CHECKPOINT_OPT:
238186714001SSerapheim Dimitropoulos 			flags |= ZFS_IMPORT_CHECKPOINT;
238286714001SSerapheim Dimitropoulos 			break;
2383fa9e4066Sahrens 		case ':':
2384fa9e4066Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
2385fa9e4066Sahrens 			    "'%c' option\n"), optopt);
238699653d4eSeschrock 			usage(B_FALSE);
2387fa9e4066Sahrens 			break;
2388fa9e4066Sahrens 		case '?':
2389fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2390fa9e4066Sahrens 			    optopt);
239199653d4eSeschrock 			usage(B_FALSE);
2392fa9e4066Sahrens 		}
2393fa9e4066Sahrens 	}
2394fa9e4066Sahrens 
2395fa9e4066Sahrens 	argc -= optind;
2396fa9e4066Sahrens 	argv += optind;
2397fa9e4066Sahrens 
23982f8aaab3Seschrock 	if (cachefile && nsearch != 0) {
23992f8aaab3Seschrock 		(void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
24002f8aaab3Seschrock 		usage(B_FALSE);
24012f8aaab3Seschrock 	}
24022f8aaab3Seschrock 
2403468c413aSTim Haley 	if ((dryrun || xtreme_rewind) && !do_rewind) {
2404468c413aSTim Haley 		(void) fprintf(stderr,
2405468c413aSTim Haley 		    gettext("-n or -X only meaningful with -F\n"));
2406468c413aSTim Haley 		usage(B_FALSE);
2407468c413aSTim Haley 	}
2408468c413aSTim Haley 	if (dryrun)
2409468c413aSTim Haley 		rewind_policy = ZPOOL_TRY_REWIND;
2410468c413aSTim Haley 	else if (do_rewind)
2411468c413aSTim Haley 		rewind_policy = ZPOOL_DO_REWIND;
2412468c413aSTim Haley 	if (xtreme_rewind)
2413468c413aSTim Haley 		rewind_policy |= ZPOOL_EXTREME_REWIND;
2414468c413aSTim Haley 
2415468c413aSTim Haley 	/* In the future, we can capture further policy and include it here */
2416468c413aSTim Haley 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
24175dafeea3SPavel Zakharov 	    nvlist_add_uint64(policy, ZPOOL_LOAD_REQUEST_TXG, txg) != 0 ||
24185dafeea3SPavel Zakharov 	    nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY,
24195dafeea3SPavel Zakharov 	    rewind_policy) != 0)
2420468c413aSTim Haley 		goto error;
2421468c413aSTim Haley 
2422fa9e4066Sahrens 	if (searchdirs == NULL) {
2423fa9e4066Sahrens 		searchdirs = safe_malloc(sizeof (char *));
24246401734dSWill Andrews 		searchdirs[0] = ZFS_DISK_ROOT;
2425fa9e4066Sahrens 		nsearch = 1;
2426fa9e4066Sahrens 	}
2427fa9e4066Sahrens 
2428fa9e4066Sahrens 	/* check argument count */
2429fa9e4066Sahrens 	if (do_all) {
2430fa9e4066Sahrens 		if (argc != 0) {
2431fa9e4066Sahrens 			(void) fprintf(stderr, gettext("too many arguments\n"));
243299653d4eSeschrock 			usage(B_FALSE);
2433fa9e4066Sahrens 		}
2434fa9e4066Sahrens 	} else {
2435fa9e4066Sahrens 		if (argc > 2) {
2436fa9e4066Sahrens 			(void) fprintf(stderr, gettext("too many arguments\n"));
243799653d4eSeschrock 			usage(B_FALSE);
2438fa9e4066Sahrens 		}
2439fa9e4066Sahrens 
2440fa9e4066Sahrens 		/*
2441fa9e4066Sahrens 		 * Check for the SYS_CONFIG privilege.  We do this explicitly
2442fa9e4066Sahrens 		 * here because otherwise any attempt to discover pools will
2443fa9e4066Sahrens 		 * silently fail.
2444fa9e4066Sahrens 		 */
2445fa9e4066Sahrens 		if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2446fa9e4066Sahrens 			(void) fprintf(stderr, gettext("cannot "
2447fa9e4066Sahrens 			    "discover pools: permission denied\n"));
244899653d4eSeschrock 			free(searchdirs);
2449468c413aSTim Haley 			nvlist_free(policy);
2450fa9e4066Sahrens 			return (1);
2451fa9e4066Sahrens 		}
2452fa9e4066Sahrens 	}
2453fa9e4066Sahrens 
2454fa9e4066Sahrens 	/*
2455fa9e4066Sahrens 	 * Depending on the arguments given, we do one of the following:
2456fa9e4066Sahrens 	 *
2457fa9e4066Sahrens 	 *	<none>	Iterate through all pools and display information about
2458fa9e4066Sahrens 	 *		each one.
2459fa9e4066Sahrens 	 *
2460fa9e4066Sahrens 	 *	-a	Iterate through all pools and try to import each one.
2461fa9e4066Sahrens 	 *
2462fa9e4066Sahrens 	 *	<id>	Find the pool that corresponds to the given GUID/pool
2463fa9e4066Sahrens 	 *		name and import that one.
24644c58d714Sdarrenm 	 *
24654c58d714Sdarrenm 	 *	-D	Above options applies only to destroyed pools.
2466fa9e4066Sahrens 	 */
2467fa9e4066Sahrens 	if (argc != 0) {
2468fa9e4066Sahrens 		char *endptr;
2469fa9e4066Sahrens 
2470fa9e4066Sahrens 		errno = 0;
2471fa9e4066Sahrens 		searchguid = strtoull(argv[0], &endptr, 10);
24729edf9ebdSPrasad Joshi 		if (errno != 0 || *endptr != '\0') {
2473fa9e4066Sahrens 			searchname = argv[0];
24749edf9ebdSPrasad Joshi 			searchguid = 0;
24759edf9ebdSPrasad Joshi 		}
2476fa9e4066Sahrens 		found_config = NULL;
2477fa9e4066Sahrens 
247824e697d4Sck 		/*
2479d41c4376SMark J Musante 		 * User specified a name or guid.  Ensure it's unique.
248024e697d4Sck 		 */
2481d41c4376SMark J Musante 		idata.unique = B_TRUE;
248224e697d4Sck 	}
248324e697d4Sck 
2484d41c4376SMark J Musante 
2485d41c4376SMark J Musante 	idata.path = searchdirs;
2486d41c4376SMark J Musante 	idata.paths = nsearch;
2487d41c4376SMark J Musante 	idata.poolname = searchname;
2488d41c4376SMark J Musante 	idata.guid = searchguid;
2489d41c4376SMark J Musante 	idata.cachefile = cachefile;
24906f793812SPavel Zakharov 	idata.policy = policy;
2491d41c4376SMark J Musante 
2492d41c4376SMark J Musante 	pools = zpool_search_import(g_zfs, &idata);
2493d41c4376SMark J Musante 
2494d41c4376SMark J Musante 	if (pools != NULL && idata.exists &&
2495d41c4376SMark J Musante 	    (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2496d41c4376SMark J Musante 		(void) fprintf(stderr, gettext("cannot import '%s': "
2497d41c4376SMark J Musante 		    "a pool with that name already exists\n"),
2498d41c4376SMark J Musante 		    argv[0]);
249904e56356SAndriy Gapon 		(void) fprintf(stderr, gettext("use the form 'zpool import "
250004e56356SAndriy Gapon 		    "[-t] <pool | id> <newpool>' to give it a new temporary "
250104e56356SAndriy Gapon 		    "or permanent name\n"));
2502d41c4376SMark J Musante 		err = 1;
2503d41c4376SMark J Musante 	} else if (pools == NULL && idata.exists) {
2504d41c4376SMark J Musante 		(void) fprintf(stderr, gettext("cannot import '%s': "
2505d41c4376SMark J Musante 		    "a pool with that name is already created/imported,\n"),
2506d41c4376SMark J Musante 		    argv[0]);
2507d41c4376SMark J Musante 		(void) fprintf(stderr, gettext("and no additional pools "
2508d41c4376SMark J Musante 		    "with that name were found\n"));
2509d41c4376SMark J Musante 		err = 1;
2510d41c4376SMark J Musante 	} else if (pools == NULL) {
251124e697d4Sck 		if (argc != 0) {
251224e697d4Sck 			(void) fprintf(stderr, gettext("cannot import '%s': "
251324e697d4Sck 			    "no such pool available\n"), argv[0]);
251424e697d4Sck 		}
2515d41c4376SMark J Musante 		err = 1;
2516d41c4376SMark J Musante 	}
2517d41c4376SMark J Musante 
2518d41c4376SMark J Musante 	if (err == 1) {
251924e697d4Sck 		free(searchdirs);
2520468c413aSTim Haley 		nvlist_free(policy);
252124e697d4Sck 		return (1);
252224e697d4Sck 	}
252324e697d4Sck 
252424e697d4Sck 	/*
252524e697d4Sck 	 * At this point we have a list of import candidate configs. Even if
252624e697d4Sck 	 * we were searching by pool name or guid, we still need to
252724e697d4Sck 	 * post-process the list to deal with pool state and possible
252824e697d4Sck 	 * duplicate names.
252924e697d4Sck 	 */
2530fa9e4066Sahrens 	err = 0;
2531fa9e4066Sahrens 	elem = NULL;
253299653d4eSeschrock 	first = B_TRUE;
2533fa9e4066Sahrens 	while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2534fa9e4066Sahrens 
2535fa9e4066Sahrens 		verify(nvpair_value_nvlist(elem, &config) == 0);
2536fa9e4066Sahrens 
25374c58d714Sdarrenm 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
25384c58d714Sdarrenm 		    &pool_state) == 0);
25394c58d714Sdarrenm 		if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
25404c58d714Sdarrenm 			continue;
25414c58d714Sdarrenm 		if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
25424c58d714Sdarrenm 			continue;
25434c58d714Sdarrenm 
25445dafeea3SPavel Zakharov 		verify(nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY,
2545468c413aSTim Haley 		    policy) == 0);
2546468c413aSTim Haley 
2547fa9e4066Sahrens 		if (argc == 0) {
2548fa9e4066Sahrens 			if (first)
254999653d4eSeschrock 				first = B_FALSE;
25503bb79becSeschrock 			else if (!do_all)
2551fa9e4066Sahrens 				(void) printf("\n");
2552fa9e4066Sahrens 
2553468c413aSTim Haley 			if (do_all) {
2554fa9e4066Sahrens 				err |= do_import(config, NULL, mntopts,
25554b964adaSGeorge Wilson 				    props, flags);
2556468c413aSTim Haley 			} else {
2557fa9e4066Sahrens 				show_import(config);
2558468c413aSTim Haley 			}
2559fa9e4066Sahrens 		} else if (searchname != NULL) {
2560fa9e4066Sahrens 			char *name;
2561fa9e4066Sahrens 
2562fa9e4066Sahrens 			/*
2563fa9e4066Sahrens 			 * We are searching for a pool based on name.
2564fa9e4066Sahrens 			 */
2565fa9e4066Sahrens 			verify(nvlist_lookup_string(config,
2566fa9e4066Sahrens 			    ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2567fa9e4066Sahrens 
2568fa9e4066Sahrens 			if (strcmp(name, searchname) == 0) {
2569fa9e4066Sahrens 				if (found_config != NULL) {
2570fa9e4066Sahrens 					(void) fprintf(stderr, gettext(
2571fa9e4066Sahrens 					    "cannot import '%s': more than "
2572fa9e4066Sahrens 					    "one matching pool\n"), searchname);
2573fa9e4066Sahrens 					(void) fprintf(stderr, gettext(
2574fa9e4066Sahrens 					    "import by numeric ID instead\n"));
257599653d4eSeschrock 					err = B_TRUE;
2576fa9e4066Sahrens 				}
2577fa9e4066Sahrens 				found_config = config;
2578fa9e4066Sahrens 			}
2579fa9e4066Sahrens 		} else {
2580fa9e4066Sahrens 			uint64_t guid;
2581fa9e4066Sahrens 
2582fa9e4066Sahrens 			/*
2583fa9e4066Sahrens 			 * Search for a pool by guid.
2584fa9e4066Sahrens 			 */
2585fa9e4066Sahrens 			verify(nvlist_lookup_uint64(config,
2586fa9e4066Sahrens 			    ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2587fa9e4066Sahrens 
2588fa9e4066Sahrens 			if (guid == searchguid)
2589fa9e4066Sahrens 				found_config = config;
2590fa9e4066Sahrens 		}
2591fa9e4066Sahrens 	}
2592fa9e4066Sahrens 
2593fa9e4066Sahrens 	/*
2594fa9e4066Sahrens 	 * If we were searching for a specific pool, verify that we found a
2595fa9e4066Sahrens 	 * pool, and then do the import.
2596fa9e4066Sahrens 	 */
2597fa9e4066Sahrens 	if (argc != 0 && err == 0) {
2598fa9e4066Sahrens 		if (found_config == NULL) {
2599fa9e4066Sahrens 			(void) fprintf(stderr, gettext("cannot import '%s': "
2600fa9e4066Sahrens 			    "no such pool available\n"), argv[0]);
260199653d4eSeschrock 			err = B_TRUE;
2602fa9e4066Sahrens 		} else {
2603fa9e4066Sahrens 			err |= do_import(found_config, argc == 1 ? NULL :
26044b964adaSGeorge Wilson 			    argv[1], mntopts, props, flags);
2605fa9e4066Sahrens 		}
2606fa9e4066Sahrens 	}
2607fa9e4066Sahrens 
2608fa9e4066Sahrens 	/*
2609fa9e4066Sahrens 	 * If we were just looking for pools, report an error if none were
2610fa9e4066Sahrens 	 * found.
2611fa9e4066Sahrens 	 */
2612fa9e4066Sahrens 	if (argc == 0 && first)
2613fa9e4066Sahrens 		(void) fprintf(stderr,
2614fa9e4066Sahrens 		    gettext("no pools available to import\n"));
2615fa9e4066Sahrens 
2616ecd6cf80Smarks error:
26172f8aaab3Seschrock 	nvlist_free(props);
2618fa9e4066Sahrens 	nvlist_free(pools);
2619468c413aSTim Haley 	nvlist_free(policy);
262099653d4eSeschrock 	free(searchdirs);
2621fa9e4066Sahrens 
2622fa9e4066Sahrens 	return (err ? 1 : 0);
2623fa9e4066Sahrens }
2624fa9e4066Sahrens 
2625*9c2acf00SAlek Pinchuk /*
2626*9c2acf00SAlek Pinchuk  * zpool sync [-f] [pool] ...
2627*9c2acf00SAlek Pinchuk  *
2628*9c2acf00SAlek Pinchuk  * -f (undocumented) force uberblock (and config including zpool cache file)
2629*9c2acf00SAlek Pinchuk  *    update.
2630*9c2acf00SAlek Pinchuk  *
2631*9c2acf00SAlek Pinchuk  * Sync the specified pool(s).
2632*9c2acf00SAlek Pinchuk  * Without arguments "zpool sync" will sync all pools.
2633*9c2acf00SAlek Pinchuk  * This command initiates TXG sync(s) and will return after the TXG(s) commit.
2634*9c2acf00SAlek Pinchuk  *
2635*9c2acf00SAlek Pinchuk  */
2636*9c2acf00SAlek Pinchuk static int
2637*9c2acf00SAlek Pinchuk zpool_do_sync(int argc, char **argv)
2638*9c2acf00SAlek Pinchuk {
2639*9c2acf00SAlek Pinchuk 	int ret;
2640*9c2acf00SAlek Pinchuk 	boolean_t force = B_FALSE;
2641*9c2acf00SAlek Pinchuk 
2642*9c2acf00SAlek Pinchuk 	/* check options */
2643*9c2acf00SAlek Pinchuk 	while ((ret  = getopt(argc, argv, "f")) != -1) {
2644*9c2acf00SAlek Pinchuk 		switch (ret) {
2645*9c2acf00SAlek Pinchuk 		case 'f':
2646*9c2acf00SAlek Pinchuk 			force = B_TRUE;
2647*9c2acf00SAlek Pinchuk 			break;
2648*9c2acf00SAlek Pinchuk 		case '?':
2649*9c2acf00SAlek Pinchuk 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2650*9c2acf00SAlek Pinchuk 			    optopt);
2651*9c2acf00SAlek Pinchuk 			usage(B_FALSE);
2652*9c2acf00SAlek Pinchuk 		}
2653*9c2acf00SAlek Pinchuk 	}
2654*9c2acf00SAlek Pinchuk 
2655*9c2acf00SAlek Pinchuk 	argc -= optind;
2656*9c2acf00SAlek Pinchuk 	argv += optind;
2657*9c2acf00SAlek Pinchuk 
2658*9c2acf00SAlek Pinchuk 	/* if argc == 0 we will execute zpool_sync_one on all pools */
2659*9c2acf00SAlek Pinchuk 	ret = for_each_pool(argc, argv, B_FALSE, NULL, zpool_sync_one, &force);
2660*9c2acf00SAlek Pinchuk 
2661*9c2acf00SAlek Pinchuk 	return (ret);
2662*9c2acf00SAlek Pinchuk }
2663*9c2acf00SAlek Pinchuk 
2664fa9e4066Sahrens typedef struct iostat_cbdata {
26654263d13fSGeorge Wilson 	boolean_t cb_verbose;
2666fa9e4066Sahrens 	int cb_namewidth;
26674263d13fSGeorge Wilson 	int cb_iteration;
26684263d13fSGeorge Wilson 	zpool_list_t *cb_list;
2669fa9e4066Sahrens } iostat_cbdata_t;
2670fa9e4066Sahrens 
2671fa9e4066Sahrens static void
2672fa9e4066Sahrens print_iostat_separator(iostat_cbdata_t *cb)
2673fa9e4066Sahrens {
2674fa9e4066Sahrens 	int i = 0;
2675fa9e4066Sahrens 
2676fa9e4066Sahrens 	for (i = 0; i < cb->cb_namewidth; i++)
2677fa9e4066Sahrens 		(void) printf("-");
2678fa9e4066Sahrens 	(void) printf("  -----  -----  -----  -----  -----  -----\n");
2679fa9e4066Sahrens }
2680fa9e4066Sahrens 
2681fa9e4066Sahrens static void
2682fa9e4066Sahrens print_iostat_header(iostat_cbdata_t *cb)
2683fa9e4066Sahrens {
2684fa9e4066Sahrens 	(void) printf("%*s     capacity     operations    bandwidth\n",
2685fa9e4066Sahrens 	    cb->cb_namewidth, "");
2686485bbbf5SGeorge Wilson 	(void) printf("%-*s  alloc   free   read  write   read  write\n",
2687fa9e4066Sahrens 	    cb->cb_namewidth, "pool");
2688fa9e4066Sahrens 	print_iostat_separator(cb);
2689fa9e4066Sahrens }
2690fa9e4066Sahrens 
2691fa9e4066Sahrens /*
2692fa9e4066Sahrens  * Display a single statistic.
2693fa9e4066Sahrens  */
2694990b4856Slling static void
2695fa9e4066Sahrens print_one_stat(uint64_t value)
2696fa9e4066Sahrens {
2697fa9e4066Sahrens 	char buf[64];
2698fa9e4066Sahrens 
2699fa9e4066Sahrens 	zfs_nicenum(value, buf, sizeof (buf));
2700fa9e4066Sahrens 	(void) printf("  %5s", buf);
2701fa9e4066Sahrens }
2702fa9e4066Sahrens 
2703fa9e4066Sahrens /*
2704fa9e4066Sahrens  * Print out all the statistics for the given vdev.  This can either be the
2705fa9e4066Sahrens  * toplevel configuration, or called recursively.  If 'name' is NULL, then this
2706fa9e4066Sahrens  * is a verbose output, and we don't want to display the toplevel pool stats.
2707fa9e4066Sahrens  */
2708fa9e4066Sahrens void
2709c67d9675Seschrock print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2710c67d9675Seschrock     nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2711fa9e4066Sahrens {
2712fa9e4066Sahrens 	nvlist_t **oldchild, **newchild;
2713fa9e4066Sahrens 	uint_t c, children;
2714fa9e4066Sahrens 	vdev_stat_t *oldvs, *newvs;
2715fa9e4066Sahrens 	vdev_stat_t zerovs = { 0 };
2716fa9e4066Sahrens 	uint64_t tdelta;
2717fa9e4066Sahrens 	double scale;
2718afefbcddSeschrock 	char *vname;
2719fa9e4066Sahrens 
27205cabbc6bSPrashanth Sreenivasa 	if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
27215cabbc6bSPrashanth Sreenivasa 		return;
27225cabbc6bSPrashanth Sreenivasa 
2723fa9e4066Sahrens 	if (oldnv != NULL) {
27243f9d6ad7SLin Ling 		verify(nvlist_lookup_uint64_array(oldnv,
27253f9d6ad7SLin Ling 		    ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2726fa9e4066Sahrens 	} else {
2727fa9e4066Sahrens 		oldvs = &zerovs;
2728fa9e4066Sahrens 	}
2729fa9e4066Sahrens 
27303f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2731fa9e4066Sahrens 	    (uint64_t **)&newvs, &c) == 0);
2732fa9e4066Sahrens 
2733fa9e4066Sahrens 	if (strlen(name) + depth > cb->cb_namewidth)
2734fa9e4066Sahrens 		(void) printf("%*s%s", depth, "", name);
2735fa9e4066Sahrens 	else
2736fa9e4066Sahrens 		(void) printf("%*s%s%*s", depth, "", name,
2737fa9e4066Sahrens 		    (int)(cb->cb_namewidth - strlen(name) - depth), "");
2738fa9e4066Sahrens 
2739fa9e4066Sahrens 	tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2740fa9e4066Sahrens 
2741fa9e4066Sahrens 	if (tdelta == 0)
2742fa9e4066Sahrens 		scale = 1.0;
2743fa9e4066Sahrens 	else
2744fa9e4066Sahrens 		scale = (double)NANOSEC / tdelta;
2745fa9e4066Sahrens 
2746fa9e4066Sahrens 	/* only toplevel vdevs have capacity stats */
2747fa9e4066Sahrens 	if (newvs->vs_space == 0) {
2748fa9e4066Sahrens 		(void) printf("      -      -");
2749fa9e4066Sahrens 	} else {
2750fa9e4066Sahrens 		print_one_stat(newvs->vs_alloc);
2751fa9e4066Sahrens 		print_one_stat(newvs->vs_space - newvs->vs_alloc);
2752fa9e4066Sahrens 	}
2753fa9e4066Sahrens 
2754fa9e4066Sahrens 	print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2755fa9e4066Sahrens 	    oldvs->vs_ops[ZIO_TYPE_READ])));
2756fa9e4066Sahrens 
2757fa9e4066Sahrens 	print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2758fa9e4066Sahrens 	    oldvs->vs_ops[ZIO_TYPE_WRITE])));
2759fa9e4066Sahrens 
2760fa9e4066Sahrens 	print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2761fa9e4066Sahrens 	    oldvs->vs_bytes[ZIO_TYPE_READ])));
2762fa9e4066Sahrens 
2763fa9e4066Sahrens 	print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2764fa9e4066Sahrens 	    oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2765fa9e4066Sahrens 
2766fa9e4066Sahrens 	(void) printf("\n");
2767fa9e4066Sahrens 
2768fa9e4066Sahrens 	if (!cb->cb_verbose)
2769fa9e4066Sahrens 		return;
2770fa9e4066Sahrens 
2771fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2772fa9e4066Sahrens 	    &newchild, &children) != 0)
2773fa9e4066Sahrens 		return;
2774fa9e4066Sahrens 
2775fa9e4066Sahrens 	if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2776fa9e4066Sahrens 	    &oldchild, &c) != 0)
2777fa9e4066Sahrens 		return;
2778fa9e4066Sahrens 
2779afefbcddSeschrock 	for (c = 0; c < children; c++) {
27809d439f90SMike Harsch 		uint64_t ishole = B_FALSE, islog = B_FALSE;
27813f9d6ad7SLin Ling 
27829d439f90SMike Harsch 		(void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
27839d439f90SMike Harsch 		    &ishole);
27849d439f90SMike Harsch 
27859d439f90SMike Harsch 		(void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
27869d439f90SMike Harsch 		    &islog);
27879d439f90SMike Harsch 
27889d439f90SMike Harsch 		if (ishole || islog)
27893f9d6ad7SLin Ling 			continue;
27903f9d6ad7SLin Ling 
279188ecc943SGeorge Wilson 		vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2792c67d9675Seschrock 		print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2793afefbcddSeschrock 		    newchild[c], cb, depth + 2);
2794afefbcddSeschrock 		free(vname);
2795afefbcddSeschrock 	}
2796fa94a07fSbrendan 
27979d439f90SMike Harsch 	/*
27989d439f90SMike Harsch 	 * Log device section
27999d439f90SMike Harsch 	 */
28009d439f90SMike Harsch 
28019d439f90SMike Harsch 	if (num_logs(newnv) > 0) {
28029d439f90SMike Harsch 		(void) printf("%-*s      -      -      -      -      -      "
28039d439f90SMike Harsch 		    "-\n", cb->cb_namewidth, "logs");
28049d439f90SMike Harsch 
28059d439f90SMike Harsch 		for (c = 0; c < children; c++) {
28069d439f90SMike Harsch 			uint64_t islog = B_FALSE;
28079d439f90SMike Harsch 			(void) nvlist_lookup_uint64(newchild[c],
28089d439f90SMike Harsch 			    ZPOOL_CONFIG_IS_LOG, &islog);
28099d439f90SMike Harsch 
28109d439f90SMike Harsch 			if (islog) {
28119d439f90SMike Harsch 				vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
28129d439f90SMike Harsch 				    B_FALSE);
28139d439f90SMike Harsch 				print_vdev_stats(zhp, vname, oldnv ?
28149d439f90SMike Harsch 				    oldchild[c] : NULL, newchild[c],
28159d439f90SMike Harsch 				    cb, depth + 2);
28169d439f90SMike Harsch 				free(vname);
28179d439f90SMike Harsch 			}
28189d439f90SMike Harsch 		}
28199d439f90SMike Harsch 
28209d439f90SMike Harsch 	}
28219d439f90SMike Harsch 
2822fa94a07fSbrendan 	/*
2823fa94a07fSbrendan 	 * Include level 2 ARC devices in iostat output
2824fa94a07fSbrendan 	 */
2825fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2826fa94a07fSbrendan 	    &newchild, &children) != 0)
2827fa94a07fSbrendan 		return;
2828fa94a07fSbrendan 
2829fa94a07fSbrendan 	if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2830fa94a07fSbrendan 	    &oldchild, &c) != 0)
2831fa94a07fSbrendan 		return;
2832fa94a07fSbrendan 
2833fa94a07fSbrendan 	if (children > 0) {
2834fa94a07fSbrendan 		(void) printf("%-*s      -      -      -      -      -      "
2835fa94a07fSbrendan 		    "-\n", cb->cb_namewidth, "cache");
2836fa94a07fSbrendan 		for (c = 0; c < children; c++) {
283788ecc943SGeorge Wilson 			vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
283888ecc943SGeorge Wilson 			    B_FALSE);
2839fa94a07fSbrendan 			print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2840fa94a07fSbrendan 			    newchild[c], cb, depth + 2);
2841fa94a07fSbrendan 			free(vname);
2842fa94a07fSbrendan 		}
2843fa94a07fSbrendan 	}
2844fa9e4066Sahrens }
2845fa9e4066Sahrens 
2846088e9d47Seschrock static int
2847088e9d47Seschrock refresh_iostat(zpool_handle_t *zhp, void *data)
2848088e9d47Seschrock {
2849088e9d47Seschrock 	iostat_cbdata_t *cb = data;
285094de1d4cSeschrock 	boolean_t missing;
2851088e9d47Seschrock 
2852088e9d47Seschrock 	/*
2853088e9d47Seschrock 	 * If the pool has disappeared, remove it from the list and continue.
2854088e9d47Seschrock 	 */
285594de1d4cSeschrock 	if (zpool_refresh_stats(zhp, &missing) != 0)
285694de1d4cSeschrock 		return (-1);
285794de1d4cSeschrock 
285894de1d4cSeschrock 	if (missing)
2859088e9d47Seschrock 		pool_list_remove(cb->cb_list, zhp);
2860088e9d47Seschrock 
2861088e9d47Seschrock 	return (0);
2862088e9d47Seschrock }
2863088e9d47Seschrock 
2864fa9e4066Sahrens /*
2865fa9e4066Sahrens  * Callback to print out the iostats for the given pool.
2866fa9e4066Sahrens  */
2867fa9e4066Sahrens int
2868fa9e4066Sahrens print_iostat(zpool_handle_t *zhp, void *data)
2869fa9e4066Sahrens {
2870fa9e4066Sahrens 	iostat_cbdata_t *cb = data;
2871fa9e4066Sahrens 	nvlist_t *oldconfig, *newconfig;
2872fa9e4066Sahrens 	nvlist_t *oldnvroot, *newnvroot;
2873fa9e4066Sahrens 
2874088e9d47Seschrock 	newconfig = zpool_get_config(zhp, &oldconfig);
2875fa9e4066Sahrens 
2876088e9d47Seschrock 	if (cb->cb_iteration == 1)
2877fa9e4066Sahrens 		oldconfig = NULL;
2878fa9e4066Sahrens 
2879fa9e4066Sahrens 	verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2880fa9e4066Sahrens 	    &newnvroot) == 0);
2881fa9e4066Sahrens 
2882088e9d47Seschrock 	if (oldconfig == NULL)
2883fa9e4066Sahrens 		oldnvroot = NULL;
2884088e9d47Seschrock 	else
2885088e9d47Seschrock 		verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2886088e9d47Seschrock 		    &oldnvroot) == 0);
2887fa9e4066Sahrens 
2888fa9e4066Sahrens 	/*
2889fa9e4066Sahrens 	 * Print out the statistics for the pool.
2890fa9e4066Sahrens 	 */
2891c67d9675Seschrock 	print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2892fa9e4066Sahrens 
2893fa9e4066Sahrens 	if (cb->cb_verbose)
2894fa9e4066Sahrens 		print_iostat_separator(cb);
2895fa9e4066Sahrens 
2896fa9e4066Sahrens 	return (0);
2897fa9e4066Sahrens }
2898fa9e4066Sahrens 
2899fa9e4066Sahrens int
2900fa9e4066Sahrens get_namewidth(zpool_handle_t *zhp, void *data)
2901fa9e4066Sahrens {
2902fa9e4066Sahrens 	iostat_cbdata_t *cb = data;
2903fa9e4066Sahrens 	nvlist_t *config, *nvroot;
2904fa9e4066Sahrens 
2905088e9d47Seschrock 	if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2906fa9e4066Sahrens 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2907fa9e4066Sahrens 		    &nvroot) == 0);
2908fa9e4066Sahrens 		if (!cb->cb_verbose)
2909fa9e4066Sahrens 			cb->cb_namewidth = strlen(zpool_get_name(zhp));
2910fa9e4066Sahrens 		else
2911e1d5e507SFrederik Wessels 			cb->cb_namewidth = max_width(zhp, nvroot, 0,
2912e1d5e507SFrederik Wessels 			    cb->cb_namewidth);
2913fa9e4066Sahrens 	}
2914fa9e4066Sahrens 
2915fa9e4066Sahrens 	/*
2916fa9e4066Sahrens 	 * The width must fall into the range [10,38].  The upper limit is the
2917fa9e4066Sahrens 	 * maximum we can have and still fit in 80 columns.
2918fa9e4066Sahrens 	 */
2919fa9e4066Sahrens 	if (cb->cb_namewidth < 10)
2920fa9e4066Sahrens 		cb->cb_namewidth = 10;
2921fa9e4066Sahrens 	if (cb->cb_namewidth > 38)
2922fa9e4066Sahrens 		cb->cb_namewidth = 38;
2923fa9e4066Sahrens 
2924fa9e4066Sahrens 	return (0);
2925fa9e4066Sahrens }
2926fa9e4066Sahrens 
2927fa9e4066Sahrens /*
29283f9d6ad7SLin Ling  * Parse the input string, get the 'interval' and 'count' value if there is one.
2929fa9e4066Sahrens  */
29303f9d6ad7SLin Ling static void
29313f9d6ad7SLin Ling get_interval_count(int *argcp, char **argv, unsigned long *iv,
29323f9d6ad7SLin Ling     unsigned long *cnt)
2933fa9e4066Sahrens {
2934fa9e4066Sahrens 	unsigned long interval = 0, count = 0;
29353f9d6ad7SLin Ling 	int argc = *argcp, errno;
2936fa9e4066Sahrens 
2937fa9e4066Sahrens 	/*
2938fa9e4066Sahrens 	 * Determine if the last argument is an integer or a pool name
2939fa9e4066Sahrens 	 */
2940fa9e4066Sahrens 	if (argc > 0 && isdigit(argv[argc - 1][0])) {
2941fa9e4066Sahrens 		char *end;
2942fa9e4066Sahrens 
2943fa9e4066Sahrens 		errno = 0;
2944fa9e4066Sahrens 		interval = strtoul(argv[argc - 1], &end, 10);
2945fa9e4066Sahrens 
2946fa9e4066Sahrens 		if (*end == '\0' && errno == 0) {
2947fa9e4066Sahrens 			if (interval == 0) {
2948fa9e4066Sahrens 				(void) fprintf(stderr, gettext("interval "
2949fa9e4066Sahrens 				    "cannot be zero\n"));
295099653d4eSeschrock 				usage(B_FALSE);
2951fa9e4066Sahrens 			}
2952fa9e4066Sahrens 			/*
2953fa9e4066Sahrens 			 * Ignore the last parameter
2954fa9e4066Sahrens 			 */
2955fa9e4066Sahrens 			argc--;
2956fa9e4066Sahrens 		} else {
2957fa9e4066Sahrens 			/*
2958fa9e4066Sahrens 			 * If this is not a valid number, just plow on.  The
2959fa9e4066Sahrens 			 * user will get a more informative error message later
2960fa9e4066Sahrens 			 * on.
2961fa9e4066Sahrens 			 */
2962fa9e4066Sahrens 			interval = 0;
2963fa9e4066Sahrens 		}
2964fa9e4066Sahrens 	}
2965fa9e4066Sahrens 
2966fa9e4066Sahrens 	/*
2967fa9e4066Sahrens 	 * If the last argument is also an integer, then we have both a count
29683f9d6ad7SLin Ling 	 * and an interval.
2969fa9e4066Sahrens 	 */
2970fa9e4066Sahrens 	if (argc > 0 && isdigit(argv[argc - 1][0])) {
2971fa9e4066Sahrens 		char *end;
2972fa9e4066Sahrens 
2973fa9e4066Sahrens 		errno = 0;
2974fa9e4066Sahrens 		count = interval;
2975fa9e4066Sahrens 		interval = strtoul(argv[argc - 1], &end, 10);
2976fa9e4066Sahrens 
2977fa9e4066Sahrens 		if (*end == '\0' && errno == 0) {
2978fa9e4066Sahrens 			if (interval == 0) {
2979fa9e4066Sahrens 				(void) fprintf(stderr, gettext("interval "
2980fa9e4066Sahrens 				    "cannot be zero\n"));
298199653d4eSeschrock 				usage(B_FALSE);
2982fa9e4066Sahrens 			}
2983fa9e4066Sahrens 
2984fa9e4066Sahrens 			/*
2985fa9e4066Sahrens 			 * Ignore the last parameter
2986fa9e4066Sahrens 			 */
2987fa9e4066Sahrens 			argc--;
2988fa9e4066Sahrens 		} else {
2989fa9e4066Sahrens 			interval = 0;
2990fa9e4066Sahrens 		}
2991fa9e4066Sahrens 	}
2992fa9e4066Sahrens 
29933f9d6ad7SLin Ling 	*iv = interval;
29943f9d6ad7SLin Ling 	*cnt = count;
29953f9d6ad7SLin Ling 	*argcp = argc;
29963f9d6ad7SLin Ling }
29973f9d6ad7SLin Ling 
29983f9d6ad7SLin Ling static void
29993f9d6ad7SLin Ling get_timestamp_arg(char c)
30003f9d6ad7SLin Ling {
30013f9d6ad7SLin Ling 	if (c == 'u')
30023f9d6ad7SLin Ling 		timestamp_fmt = UDATE;
30033f9d6ad7SLin Ling 	else if (c == 'd')
30043f9d6ad7SLin Ling 		timestamp_fmt = DDATE;
30053f9d6ad7SLin Ling 	else
30063f9d6ad7SLin Ling 		usage(B_FALSE);
30073f9d6ad7SLin Ling }
30083f9d6ad7SLin Ling 
30093f9d6ad7SLin Ling /*
30103f9d6ad7SLin Ling  * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
30113f9d6ad7SLin Ling  *
30123f9d6ad7SLin Ling  *	-v	Display statistics for individual vdevs
30133f9d6ad7SLin Ling  *	-T	Display a timestamp in date(1) or Unix format
30143f9d6ad7SLin Ling  *
30153f9d6ad7SLin Ling  * This command can be tricky because we want to be able to deal with pool
30163f9d6ad7SLin Ling  * creation/destruction as well as vdev configuration changes.  The bulk of this
30173f9d6ad7SLin Ling  * processing is handled by the pool_list_* routines in zpool_iter.c.  We rely
30183f9d6ad7SLin Ling  * on pool_list_update() to detect the addition of new pools.  Configuration
30193f9d6ad7SLin Ling  * changes are all handled within libzfs.
30203f9d6ad7SLin Ling  */
30213f9d6ad7SLin Ling int
30223f9d6ad7SLin Ling zpool_do_iostat(int argc, char **argv)
30233f9d6ad7SLin Ling {
30243f9d6ad7SLin Ling 	int c;
30253f9d6ad7SLin Ling 	int ret;
30263f9d6ad7SLin Ling 	int npools;
30273f9d6ad7SLin Ling 	unsigned long interval = 0, count = 0;
30283f9d6ad7SLin Ling 	zpool_list_t *list;
30293f9d6ad7SLin Ling 	boolean_t verbose = B_FALSE;
30303f9d6ad7SLin Ling 	iostat_cbdata_t cb;
30313f9d6ad7SLin Ling 
30323f9d6ad7SLin Ling 	/* check options */
30333f9d6ad7SLin Ling 	while ((c = getopt(argc, argv, "T:v")) != -1) {
30343f9d6ad7SLin Ling 		switch (c) {
30353f9d6ad7SLin Ling 		case 'T':
30363f9d6ad7SLin Ling 			get_timestamp_arg(*optarg);
30373f9d6ad7SLin Ling 			break;
30383f9d6ad7SLin Ling 		case 'v':
30393f9d6ad7SLin Ling 			verbose = B_TRUE;
30403f9d6ad7SLin Ling 			break;
30413f9d6ad7SLin Ling 		case '?':
30423f9d6ad7SLin Ling 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
30433f9d6ad7SLin Ling 			    optopt);
30443f9d6ad7SLin Ling 			usage(B_FALSE);
30453f9d6ad7SLin Ling 		}
30463f9d6ad7SLin Ling 	}
30473f9d6ad7SLin Ling 
30483f9d6ad7SLin Ling 	argc -= optind;
30493f9d6ad7SLin Ling 	argv += optind;
30503f9d6ad7SLin Ling 
30513f9d6ad7SLin Ling 	get_interval_count(&argc, argv, &interval, &count);
30523f9d6ad7SLin Ling 
3053fa9e4066Sahrens 	/*
3054fa9e4066Sahrens 	 * Construct the list of all interesting pools.
3055fa9e4066Sahrens 	 */
3056fa9e4066Sahrens 	ret = 0;
3057b1b8ab34Slling 	if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
3058fa9e4066Sahrens 		return (1);
3059fa9e4066Sahrens 
306099653d4eSeschrock 	if (pool_list_count(list) == 0 && argc != 0) {
306199653d4eSeschrock 		pool_list_free(list);
3062fa9e4066Sahrens 		return (1);
306399653d4eSeschrock 	}
3064fa9e4066Sahrens 
3065fa9e4066Sahrens 	if (pool_list_count(list) == 0 && interval == 0) {
306699653d4eSeschrock 		pool_list_free(list);
3067fa9e4066Sahrens 		(void) fprintf(stderr, gettext("no pools available\n"));
3068fa9e4066Sahrens 		return (1);
3069fa9e4066Sahrens 	}
3070fa9e4066Sahrens 
3071fa9e4066Sahrens 	/*
3072fa9e4066Sahrens 	 * Enter the main iostat loop.
3073fa9e4066Sahrens 	 */
3074fa9e4066Sahrens 	cb.cb_list = list;
3075fa9e4066Sahrens 	cb.cb_verbose = verbose;
3076fa9e4066Sahrens 	cb.cb_iteration = 0;
3077fa9e4066Sahrens 	cb.cb_namewidth = 0;
3078fa9e4066Sahrens 
3079fa9e4066Sahrens 	for (;;) {
3080fa9e4066Sahrens 		pool_list_update(list);
3081fa9e4066Sahrens 
3082fa9e4066Sahrens 		if ((npools = pool_list_count(list)) == 0)
3083fa9e4066Sahrens 			break;
3084fa9e4066Sahrens 
3085088e9d47Seschrock 		/*
3086088e9d47Seschrock 		 * Refresh all statistics.  This is done as an explicit step
3087088e9d47Seschrock 		 * before calculating the maximum name width, so that any
3088088e9d47Seschrock 		 * configuration changes are properly accounted for.
3089088e9d47Seschrock 		 */
309099653d4eSeschrock 		(void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
3091088e9d47Seschrock 
3092fa9e4066Sahrens 		/*
3093fa9e4066Sahrens 		 * Iterate over all pools to determine the maximum width
3094fa9e4066Sahrens 		 * for the pool / device name column across all pools.
3095fa9e4066Sahrens 		 */
3096fa9e4066Sahrens 		cb.cb_namewidth = 0;
309799653d4eSeschrock 		(void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3098fa9e4066Sahrens 
309926fd7700SKrishnendu Sadhukhan - Sun Microsystems 		if (timestamp_fmt != NODATE)
310026fd7700SKrishnendu Sadhukhan - Sun Microsystems 			print_timestamp(timestamp_fmt);
310126fd7700SKrishnendu Sadhukhan - Sun Microsystems 
3102fa9e4066Sahrens 		/*
3103fa9e4066Sahrens 		 * If it's the first time, or verbose mode, print the header.
3104fa9e4066Sahrens 		 */
3105fa9e4066Sahrens 		if (++cb.cb_iteration == 1 || verbose)
3106fa9e4066Sahrens 			print_iostat_header(&cb);
3107fa9e4066Sahrens 
310899653d4eSeschrock 		(void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
3109fa9e4066Sahrens 
3110fa9e4066Sahrens 		/*
3111fa9e4066Sahrens 		 * If there's more than one pool, and we're not in verbose mode
3112fa9e4066Sahrens 		 * (which prints a separator for us), then print a separator.
3113fa9e4066Sahrens 		 */
3114fa9e4066Sahrens 		if (npools > 1 && !verbose)
3115fa9e4066Sahrens 			print_iostat_separator(&cb);
3116fa9e4066Sahrens 
3117fa9e4066Sahrens 		if (verbose)
3118fa9e4066Sahrens 			(void) printf("\n");
3119fa9e4066Sahrens 
312039c23413Seschrock 		/*
312139c23413Seschrock 		 * Flush the output so that redirection to a file isn't buffered
312239c23413Seschrock 		 * indefinitely.
312339c23413Seschrock 		 */
312439c23413Seschrock 		(void) fflush(stdout);
312539c23413Seschrock 
3126fa9e4066Sahrens 		if (interval == 0)
3127fa9e4066Sahrens 			break;
3128fa9e4066Sahrens 
3129fa9e4066Sahrens 		if (count != 0 && --count == 0)
3130fa9e4066Sahrens 			break;
3131fa9e4066Sahrens 
3132fa9e4066Sahrens 		(void) sleep(interval);
3133fa9e4066Sahrens 	}
3134fa9e4066Sahrens 
3135fa9e4066Sahrens 	pool_list_free(list);
3136fa9e4066Sahrens 
3137fa9e4066Sahrens 	return (ret);
3138fa9e4066Sahrens }
3139fa9e4066Sahrens 
3140fa9e4066Sahrens typedef struct list_cbdata {
31414263d13fSGeorge Wilson 	boolean_t	cb_verbose;
31424263d13fSGeorge Wilson 	int		cb_namewidth;
314399653d4eSeschrock 	boolean_t	cb_scripted;
3144990b4856Slling 	zprop_list_t	*cb_proplist;
3145c58b3526SAdam Stevko 	boolean_t	cb_literal;
3146fa9e4066Sahrens } list_cbdata_t;
3147fa9e4066Sahrens 
3148fa9e4066Sahrens /*
3149fa9e4066Sahrens  * Given a list of columns to display, output appropriate headers for each one.
3150fa9e4066Sahrens  */
3151990b4856Slling static void
31524263d13fSGeorge Wilson print_header(list_cbdata_t *cb)
3153fa9e4066Sahrens {
31544263d13fSGeorge Wilson 	zprop_list_t *pl = cb->cb_proplist;
3155ad135b5dSChristopher Siden 	char headerbuf[ZPOOL_MAXPROPLEN];
3156990b4856Slling 	const char *header;
3157990b4856Slling 	boolean_t first = B_TRUE;
3158990b4856Slling 	boolean_t right_justify;
31594263d13fSGeorge Wilson 	size_t width = 0;
3160990b4856Slling 
3161990b4856Slling 	for (; pl != NULL; pl = pl->pl_next) {
31624263d13fSGeorge Wilson 		width = pl->pl_width;
31634263d13fSGeorge Wilson 		if (first && cb->cb_verbose) {
31644263d13fSGeorge Wilson 			/*
31654263d13fSGeorge Wilson 			 * Reset the width to accommodate the verbose listing
31664263d13fSGeorge Wilson 			 * of devices.
31674263d13fSGeorge Wilson 			 */
31684263d13fSGeorge Wilson 			width = cb->cb_namewidth;
31694263d13fSGeorge Wilson 		}
31704263d13fSGeorge Wilson 
3171990b4856Slling 		if (!first)
3172fa9e4066Sahrens 			(void) printf("  ");
3173fa9e4066Sahrens 		else
3174990b4856Slling 			first = B_FALSE;
3175990b4856Slling 
3176ad135b5dSChristopher Siden 		right_justify = B_FALSE;
3177ad135b5dSChristopher Siden 		if (pl->pl_prop != ZPROP_INVAL) {
3178ad135b5dSChristopher Siden 			header = zpool_prop_column_name(pl->pl_prop);
3179ad135b5dSChristopher Siden 			right_justify = zpool_prop_align_right(pl->pl_prop);
3180ad135b5dSChristopher Siden 		} else {
3181ad135b5dSChristopher Siden 			int i;
3182ad135b5dSChristopher Siden 
3183ad135b5dSChristopher Siden 			for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
3184ad135b5dSChristopher Siden 				headerbuf[i] = toupper(pl->pl_user_prop[i]);
3185ad135b5dSChristopher Siden 			headerbuf[i] = '\0';
3186ad135b5dSChristopher Siden 			header = headerbuf;
3187ad135b5dSChristopher Siden 		}
3188fa9e4066Sahrens 
3189990b4856Slling 		if (pl->pl_next == NULL && !right_justify)
3190990b4856Slling 			(void) printf("%s", header);
3191990b4856Slling 		else if (right_justify)
31924263d13fSGeorge Wilson 			(void) printf("%*s", width, header);
3193990b4856Slling 		else
31944263d13fSGeorge Wilson 			(void) printf("%-*s", width, header);
31954263d13fSGeorge Wilson 
3196fa9e4066Sahrens 	}
3197fa9e4066Sahrens 
3198fa9e4066Sahrens 	(void) printf("\n");
3199fa9e4066Sahrens }
3200fa9e4066Sahrens 
3201990b4856Slling /*
3202990b4856Slling  * Given a pool and a list of properties, print out all the properties according
3203990b4856Slling  * to the described layout.
3204990b4856Slling  */
3205990b4856Slling static void
32064263d13fSGeorge Wilson print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
3207fa9e4066Sahrens {
32084263d13fSGeorge Wilson 	zprop_list_t *pl = cb->cb_proplist;
3209990b4856Slling 	boolean_t first = B_TRUE;
3210990b4856Slling 	char property[ZPOOL_MAXPROPLEN];
3211990b4856Slling 	char *propstr;
3212990b4856Slling 	boolean_t right_justify;
32134263d13fSGeorge Wilson 	size_t width;
3214990b4856Slling 
3215990b4856Slling 	for (; pl != NULL; pl = pl->pl_next) {
32164263d13fSGeorge Wilson 
32174263d13fSGeorge Wilson 		width = pl->pl_width;
32184263d13fSGeorge Wilson 		if (first && cb->cb_verbose) {
32194263d13fSGeorge Wilson 			/*
32204263d13fSGeorge Wilson 			 * Reset the width to accommodate the verbose listing
32214263d13fSGeorge Wilson 			 * of devices.
32224263d13fSGeorge Wilson 			 */
32234263d13fSGeorge Wilson 			width = cb->cb_namewidth;
32244263d13fSGeorge Wilson 		}
32254263d13fSGeorge Wilson 
3226990b4856Slling 		if (!first) {
32274263d13fSGeorge Wilson 			if (cb->cb_scripted)
3228fa9e4066Sahrens 				(void) printf("\t");
3229fa9e4066Sahrens 			else
3230fa9e4066Sahrens 				(void) printf("  ");
3231990b4856Slling 		} else {
3232990b4856Slling 			first = B_FALSE;
3233fa9e4066Sahrens 		}
3234fa9e4066Sahrens 
3235990b4856Slling 		right_justify = B_FALSE;
3236990b4856Slling 		if (pl->pl_prop != ZPROP_INVAL) {
32377a09f97bSGeorge Wilson 			if (zpool_get_prop(zhp, pl->pl_prop, property,
3238c58b3526SAdam Stevko 			    sizeof (property), NULL, cb->cb_literal) != 0)
3239990b4856Slling 				propstr = "-";
3240fa9e4066Sahrens 			else
3241990b4856Slling 				propstr = property;
3242fa9e4066Sahrens 
3243990b4856Slling 			right_justify = zpool_prop_align_right(pl->pl_prop);
3244ad135b5dSChristopher Siden 		} else if ((zpool_prop_feature(pl->pl_user_prop) ||
3245ad135b5dSChristopher Siden 		    zpool_prop_unsupported(pl->pl_user_prop)) &&
3246ad135b5dSChristopher Siden 		    zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
3247ad135b5dSChristopher Siden 		    sizeof (property)) == 0) {
3248ad135b5dSChristopher Siden 			propstr = property;
3249990b4856Slling 		} else {
3250990b4856Slling 			propstr = "-";
3251990b4856Slling 		}
3252fa9e4066Sahrens 
3253fa9e4066Sahrens 
3254990b4856Slling 		/*
3255990b4856Slling 		 * If this is being called in scripted mode, or if this is the
3256990b4856Slling 		 * last column and it is left-justified, don't include a width
3257990b4856Slling 		 * format specifier.
3258990b4856Slling 		 */
32594263d13fSGeorge Wilson 		if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3260990b4856Slling 			(void) printf("%s", propstr);
3261990b4856Slling 		else if (right_justify)
3262990b4856Slling 			(void) printf("%*s", width, propstr);
3263990b4856Slling 		else
3264990b4856Slling 			(void) printf("%-*s", width, propstr);
3265990b4856Slling 	}
3266fa9e4066Sahrens 
3267990b4856Slling 	(void) printf("\n");
3268990b4856Slling }
3269fa9e4066Sahrens 
32704263d13fSGeorge Wilson static void
32717a09f97bSGeorge Wilson print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
32727a09f97bSGeorge Wilson     boolean_t valid)
32734263d13fSGeorge Wilson {
32744263d13fSGeorge Wilson 	char propval[64];
32754263d13fSGeorge Wilson 	boolean_t fixed;
32764263d13fSGeorge Wilson 	size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
32774263d13fSGeorge Wilson 
32787a09f97bSGeorge Wilson 	switch (prop) {
32797a09f97bSGeorge Wilson 	case ZPOOL_PROP_EXPANDSZ:
328086714001SSerapheim Dimitropoulos 	case ZPOOL_PROP_CHECKPOINT:
32817a09f97bSGeorge Wilson 		if (value == 0)
32827a09f97bSGeorge Wilson 			(void) strlcpy(propval, "-", sizeof (propval));
32837a09f97bSGeorge Wilson 		else
32847a09f97bSGeorge Wilson 			zfs_nicenum(value, propval, sizeof (propval));
32857a09f97bSGeorge Wilson 		break;
32867a09f97bSGeorge Wilson 	case ZPOOL_PROP_FRAGMENTATION:
32877a09f97bSGeorge Wilson 		if (value == ZFS_FRAG_INVALID) {
32887a09f97bSGeorge Wilson 			(void) strlcpy(propval, "-", sizeof (propval));
32897a09f97bSGeorge Wilson 		} else {
32907a09f97bSGeorge Wilson 			(void) snprintf(propval, sizeof (propval), "%llu%%",
32917a09f97bSGeorge Wilson 			    value);
32927a09f97bSGeorge Wilson 		}
32937a09f97bSGeorge Wilson 		break;
32947a09f97bSGeorge Wilson 	case ZPOOL_PROP_CAPACITY:
32952e4c9986SGeorge Wilson 		(void) snprintf(propval, sizeof (propval), "%llu%%", value);
32967a09f97bSGeorge Wilson 		break;
32977a09f97bSGeorge Wilson 	default:
32982e4c9986SGeorge Wilson 		zfs_nicenum(value, propval, sizeof (propval));
32997a09f97bSGeorge Wilson 	}
33007a09f97bSGeorge Wilson 
33017a09f97bSGeorge Wilson 	if (!valid)
33027a09f97bSGeorge Wilson 		(void) strlcpy(propval, "-", sizeof (propval));
33034263d13fSGeorge Wilson 
33044263d13fSGeorge Wilson 	if (scripted)
33054263d13fSGeorge Wilson 		(void) printf("\t%s", propval);
33064263d13fSGeorge Wilson 	else
33074263d13fSGeorge Wilson 		(void) printf("  %*s", width, propval);
33084263d13fSGeorge Wilson }
33094263d13fSGeorge Wilson 
33104263d13fSGeorge Wilson void
33114263d13fSGeorge Wilson print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
33124263d13fSGeorge Wilson     list_cbdata_t *cb, int depth)
33134263d13fSGeorge Wilson {
33144263d13fSGeorge Wilson 	nvlist_t **child;
33154263d13fSGeorge Wilson 	vdev_stat_t *vs;
33164263d13fSGeorge Wilson 	uint_t c, children;
33174263d13fSGeorge Wilson 	char *vname;
33184263d13fSGeorge Wilson 	boolean_t scripted = cb->cb_scripted;
331952244c09SJohn Wren Kennedy 	uint64_t islog = B_FALSE;
332052244c09SJohn Wren Kennedy 	boolean_t haslog = B_FALSE;
332152244c09SJohn Wren Kennedy 	char *dashes = "%-*s      -      -      -         -      -      -\n";
33224263d13fSGeorge Wilson 
33234263d13fSGeorge Wilson 	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
33244263d13fSGeorge Wilson 	    (uint64_t **)&vs, &c) == 0);
33254263d13fSGeorge Wilson 
33264263d13fSGeorge Wilson 	if (name != NULL) {
33277a09f97bSGeorge Wilson 		boolean_t toplevel = (vs->vs_space != 0);
33287a09f97bSGeorge Wilson 		uint64_t cap;
33297a09f97bSGeorge Wilson 
33305cabbc6bSPrashanth Sreenivasa 		if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
33315cabbc6bSPrashanth Sreenivasa 			return;
33325cabbc6bSPrashanth Sreenivasa 
33334263d13fSGeorge Wilson 		if (scripted)
33344263d13fSGeorge Wilson 			(void) printf("\t%s", name);
33354263d13fSGeorge Wilson 		else if (strlen(name) + depth > cb->cb_namewidth)
33364263d13fSGeorge Wilson 			(void) printf("%*s%s", depth, "", name);
33374263d13fSGeorge Wilson 		else
33384263d13fSGeorge Wilson 			(void) printf("%*s%s%*s", depth, "", name,
33394263d13fSGeorge Wilson 			    (int)(cb->cb_namewidth - strlen(name) - depth), "");
33404263d13fSGeorge Wilson 
33417a09f97bSGeorge Wilson 		/*
33427a09f97bSGeorge Wilson 		 * Print the properties for the individual vdevs. Some
33437a09f97bSGeorge Wilson 		 * properties are only applicable to toplevel vdevs. The
33447a09f97bSGeorge Wilson 		 * 'toplevel' boolean value is passed to the print_one_column()
33457a09f97bSGeorge Wilson 		 * to indicate that the value is valid.
33467a09f97bSGeorge Wilson 		 */
33477a09f97bSGeorge Wilson 		print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted,
33487a09f97bSGeorge Wilson 		    toplevel);
33497a09f97bSGeorge Wilson 		print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
33507a09f97bSGeorge Wilson 		    toplevel);
33517a09f97bSGeorge Wilson 		print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
33527a09f97bSGeorge Wilson 		    scripted, toplevel);
335386714001SSerapheim Dimitropoulos 		print_one_column(ZPOOL_PROP_CHECKPOINT,
335486714001SSerapheim Dimitropoulos 		    vs->vs_checkpoint_space, scripted, toplevel);
33557a09f97bSGeorge Wilson 		print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
33567a09f97bSGeorge Wilson 		    B_TRUE);
33577a09f97bSGeorge Wilson 		print_one_column(ZPOOL_PROP_FRAGMENTATION,
33587a09f97bSGeorge Wilson 		    vs->vs_fragmentation, scripted,
33597a09f97bSGeorge Wilson 		    (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel));
33607a09f97bSGeorge Wilson 		cap = (vs->vs_space == 0) ? 0 :
33617a09f97bSGeorge Wilson 		    (vs->vs_alloc * 100 / vs->vs_space);
33627a09f97bSGeorge Wilson 		print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel);
33634263d13fSGeorge Wilson 		(void) printf("\n");
33644263d13fSGeorge Wilson 	}
33654263d13fSGeorge Wilson 
33664263d13fSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
33674263d13fSGeorge Wilson 	    &child, &children) != 0)
33684263d13fSGeorge Wilson 		return;
33694263d13fSGeorge Wilson 
33704263d13fSGeorge Wilson 	for (c = 0; c < children; c++) {
33714263d13fSGeorge Wilson 		uint64_t ishole = B_FALSE;
33724263d13fSGeorge Wilson 
33734263d13fSGeorge Wilson 		if (nvlist_lookup_uint64(child[c],
33744263d13fSGeorge Wilson 		    ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
33754263d13fSGeorge Wilson 			continue;
33764263d13fSGeorge Wilson 
337752244c09SJohn Wren Kennedy 		if (nvlist_lookup_uint64(child[c],
337852244c09SJohn Wren Kennedy 		    ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog) {
337952244c09SJohn Wren Kennedy 			haslog = B_TRUE;
338052244c09SJohn Wren Kennedy 			continue;
338152244c09SJohn Wren Kennedy 		}
338252244c09SJohn Wren Kennedy 
33834263d13fSGeorge Wilson 		vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
33844263d13fSGeorge Wilson 		print_list_stats(zhp, vname, child[c], cb, depth + 2);
33854263d13fSGeorge Wilson 		free(vname);
33864263d13fSGeorge Wilson 	}
33874263d13fSGeorge Wilson 
338852244c09SJohn Wren Kennedy 	if (haslog == B_TRUE) {
338952244c09SJohn Wren Kennedy 		/* LINTED E_SEC_PRINTF_VAR_FMT */
339052244c09SJohn Wren Kennedy 		(void) printf(dashes, cb->cb_namewidth, "log");
339152244c09SJohn Wren Kennedy 		for (c = 0; c < children; c++) {
339252244c09SJohn Wren Kennedy 			if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
339352244c09SJohn Wren Kennedy 			    &islog) != 0 || !islog)
339452244c09SJohn Wren Kennedy 				continue;
339552244c09SJohn Wren Kennedy 			vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
339652244c09SJohn Wren Kennedy 			print_list_stats(zhp, vname, child[c], cb, depth + 2);
339752244c09SJohn Wren Kennedy 			free(vname);
339852244c09SJohn Wren Kennedy 		}
339952244c09SJohn Wren Kennedy 	}
340052244c09SJohn Wren Kennedy 
34014263d13fSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
340252244c09SJohn Wren Kennedy 	    &child, &children) == 0 && children > 0) {
340352244c09SJohn Wren Kennedy 		/* LINTED E_SEC_PRINTF_VAR_FMT */
340452244c09SJohn Wren Kennedy 		(void) printf(dashes, cb->cb_namewidth, "cache");
340552244c09SJohn Wren Kennedy 		for (c = 0; c < children; c++) {
340652244c09SJohn Wren Kennedy 			vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
340752244c09SJohn Wren Kennedy 			print_list_stats(zhp, vname, child[c], cb, depth + 2);
340852244c09SJohn Wren Kennedy 			free(vname);
340952244c09SJohn Wren Kennedy 		}
341052244c09SJohn Wren Kennedy 	}
34114263d13fSGeorge Wilson 
341252244c09SJohn Wren Kennedy 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child,
341352244c09SJohn Wren Kennedy 	    &children) == 0 && children > 0) {
341452244c09SJohn Wren Kennedy 		/* LINTED E_SEC_PRINTF_VAR_FMT */
341552244c09SJohn Wren Kennedy 		(void) printf(dashes, cb->cb_namewidth, "spare");
34164263d13fSGeorge Wilson 		for (c = 0; c < children; c++) {
341752244c09SJohn Wren Kennedy 			vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
34184263d13fSGeorge Wilson 			print_list_stats(zhp, vname, child[c], cb, depth + 2);
34194263d13fSGeorge Wilson 			free(vname);
34204263d13fSGeorge Wilson 		}
34214263d13fSGeorge Wilson 	}
34224263d13fSGeorge Wilson }
34234263d13fSGeorge Wilson 
34244263d13fSGeorge Wilson 
3425990b4856Slling /*
3426990b4856Slling  * Generic callback function to list a pool.
3427990b4856Slling  */
3428990b4856Slling int
3429990b4856Slling list_callback(zpool_handle_t *zhp, void *data)
3430990b4856Slling {
3431990b4856Slling 	list_cbdata_t *cbp = data;
34324263d13fSGeorge Wilson 	nvlist_t *config;
34334263d13fSGeorge Wilson 	nvlist_t *nvroot;
3434fa9e4066Sahrens 
34354263d13fSGeorge Wilson 	config = zpool_get_config(zhp, NULL);
3436fa9e4066Sahrens 
34374263d13fSGeorge Wilson 	print_pool(zhp, cbp);
34384263d13fSGeorge Wilson 	if (!cbp->cb_verbose)
34394263d13fSGeorge Wilson 		return (0);
34404263d13fSGeorge Wilson 
34414263d13fSGeorge Wilson 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
34424263d13fSGeorge Wilson 	    &nvroot) == 0);
34434263d13fSGeorge Wilson 	print_list_stats(zhp, NULL, nvroot, cbp, 0);
3444fa9e4066Sahrens 
3445fa9e4066Sahrens 	return (0);
3446fa9e4066Sahrens }
3447fa9e4066Sahrens 
3448fa9e4066Sahrens /*
3449c58b3526SAdam Stevko  * zpool list [-Hp] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3450fa9e4066Sahrens  *
3451990b4856Slling  *	-H	Scripted mode.  Don't display headers, and separate properties
3452990b4856Slling  *		by a single tab.
3453990b4856Slling  *	-o	List of properties to display.  Defaults to
34547a09f97bSGeorge Wilson  *		"name,size,allocated,free,expandsize,fragmentation,capacity,"
34557a09f97bSGeorge Wilson  *		"dedupratio,health,altroot"
345604e56356SAndriy Gapon  *	-p	Diplay values in parsable (exact) format.
34573f9d6ad7SLin Ling  *	-T	Display a timestamp in date(1) or Unix format
3458fa9e4066Sahrens  *
3459fa9e4066Sahrens  * List all pools in the system, whether or not they're healthy.  Output space
3460fa9e4066Sahrens  * statistics for each one, as well as health status summary.
3461fa9e4066Sahrens  */
3462fa9e4066Sahrens int
3463fa9e4066Sahrens zpool_do_list(int argc, char **argv)
3464fa9e4066Sahrens {
3465fa9e4066Sahrens 	int c;
3466fa9e4066Sahrens 	int ret;
3467fa9e4066Sahrens 	list_cbdata_t cb = { 0 };
3468990b4856Slling 	static char default_props[] =
346986714001SSerapheim Dimitropoulos 	    "name,size,allocated,free,checkpoint,expandsize,fragmentation,"
347086714001SSerapheim Dimitropoulos 	    "capacity,dedupratio,health,altroot";
3471990b4856Slling 	char *props = default_props;
34723f9d6ad7SLin Ling 	unsigned long interval = 0, count = 0;
34734263d13fSGeorge Wilson 	zpool_list_t *list;
34744263d13fSGeorge Wilson 	boolean_t first = B_TRUE;
3475fa9e4066Sahrens 
3476fa9e4066Sahrens 	/* check options */
3477c58b3526SAdam Stevko 	while ((c = getopt(argc, argv, ":Ho:pT:v")) != -1) {
3478fa9e4066Sahrens 		switch (c) {
3479fa9e4066Sahrens 		case 'H':
348099653d4eSeschrock 			cb.cb_scripted = B_TRUE;
3481fa9e4066Sahrens 			break;
3482fa9e4066Sahrens 		case 'o':
3483990b4856Slling 			props = optarg;
3484fa9e4066Sahrens 			break;
3485c58b3526SAdam Stevko 		case 'p':
3486c58b3526SAdam Stevko 			cb.cb_literal = B_TRUE;
3487c58b3526SAdam Stevko 			break;
34883f9d6ad7SLin Ling 		case 'T':
34893f9d6ad7SLin Ling 			get_timestamp_arg(*optarg);
34903f9d6ad7SLin Ling 			break;
34914263d13fSGeorge Wilson 		case 'v':
34924263d13fSGeorge Wilson 			cb.cb_verbose = B_TRUE;
34934263d13fSGeorge Wilson 			break;
3494fa9e4066Sahrens 		case ':':
3495fa9e4066Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
3496fa9e4066Sahrens 			    "'%c' option\n"), optopt);
349799653d4eSeschrock 			usage(B_FALSE);
3498fa9e4066Sahrens 			break;
3499fa9e4066Sahrens 		case '?':
3500fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3501fa9e4066Sahrens 			    optopt);
350299653d4eSeschrock 			usage(B_FALSE);
3503fa9e4066Sahrens 		}
3504fa9e4066Sahrens 	}
3505fa9e4066Sahrens 
3506fa9e4066Sahrens 	argc -= optind;
3507fa9e4066Sahrens 	argv += optind;
3508fa9e4066Sahrens 
35093f9d6ad7SLin Ling 	get_interval_count(&argc, argv, &interval, &count);
35103f9d6ad7SLin Ling 
3511990b4856Slling 	if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3512990b4856Slling 		usage(B_FALSE);
3513fa9e4066Sahrens 
35143f9d6ad7SLin Ling 	for (;;) {
3515cd67d23dSGeorge Wilson 		if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
3516cd67d23dSGeorge Wilson 		    &ret)) == NULL)
3517cd67d23dSGeorge Wilson 			return (1);
35184263d13fSGeorge Wilson 
35194263d13fSGeorge Wilson 		if (pool_list_count(list) == 0)
35204263d13fSGeorge Wilson 			break;
35214263d13fSGeorge Wilson 
35224263d13fSGeorge Wilson 		cb.cb_namewidth = 0;
35234263d13fSGeorge Wilson 		(void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3524990b4856Slling 
35253f9d6ad7SLin Ling 		if (timestamp_fmt != NODATE)
35263f9d6ad7SLin Ling 			print_timestamp(timestamp_fmt);
3527fa9e4066Sahrens 
35284263d13fSGeorge Wilson 		if (!cb.cb_scripted && (first || cb.cb_verbose)) {
35294263d13fSGeorge Wilson 			print_header(&cb);
35304263d13fSGeorge Wilson 			first = B_FALSE;
35313f9d6ad7SLin Ling 		}
35324263d13fSGeorge Wilson 		ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
35333f9d6ad7SLin Ling 
35343f9d6ad7SLin Ling 		if (interval == 0)
35353f9d6ad7SLin Ling 			break;
35363f9d6ad7SLin Ling 
35373f9d6ad7SLin Ling 		if (count != 0 && --count == 0)
35383f9d6ad7SLin Ling 			break;
35393f9d6ad7SLin Ling 
3540cd67d23dSGeorge Wilson 		pool_list_free(list);
35413f9d6ad7SLin Ling 		(void) sleep(interval);
3542fa9e4066Sahrens 	}
3543fa9e4066Sahrens 
3544cd67d23dSGeorge Wilson 	if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3545cd67d23dSGeorge Wilson 		(void) printf(gettext("no pools available\n"));
3546cd67d23dSGeorge Wilson 		ret = 0;
3547cd67d23dSGeorge Wilson 	}
3548cd67d23dSGeorge Wilson 
3549cd67d23dSGeorge Wilson 	pool_list_free(list);
35503f9d6ad7SLin Ling 	zprop_free_list(cb.cb_proplist);
3551fa9e4066Sahrens 	return (ret);
3552fa9e4066Sahrens }
3553fa9e4066Sahrens 
3554fa9e4066Sahrens static int
3555fa9e4066Sahrens zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3556fa9e4066Sahrens {
355799653d4eSeschrock 	boolean_t force = B_FALSE;
3558fa9e4066Sahrens 	int c;
3559fa9e4066Sahrens 	nvlist_t *nvroot;
3560fa9e4066Sahrens 	char *poolname, *old_disk, *new_disk;
3561fa9e4066Sahrens 	zpool_handle_t *zhp;
35627855d95bSToomas Soome 	zpool_boot_label_t boot_type;
35637855d95bSToomas Soome 	uint64_t boot_size;
356499653d4eSeschrock 	int ret;
3565fa9e4066Sahrens 
3566fa9e4066Sahrens 	/* check options */
3567fa9e4066Sahrens 	while ((c = getopt(argc, argv, "f")) != -1) {
3568fa9e4066Sahrens 		switch (c) {
3569fa9e4066Sahrens 		case 'f':
357099653d4eSeschrock 			force = B_TRUE;
3571fa9e4066Sahrens 			break;
3572fa9e4066Sahrens 		case '?':
3573fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3574fa9e4066Sahrens 			    optopt);
357599653d4eSeschrock 			usage(B_FALSE);
3576fa9e4066Sahrens 		}
3577fa9e4066Sahrens 	}
3578fa9e4066Sahrens 
3579fa9e4066Sahrens 	argc -= optind;
3580fa9e4066Sahrens 	argv += optind;
3581fa9e4066Sahrens 
3582fa9e4066Sahrens 	/* get pool name and check number of arguments */
3583fa9e4066Sahrens 	if (argc < 1) {
3584fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
358599653d4eSeschrock 		usage(B_FALSE);
3586fa9e4066Sahrens 	}
3587fa9e4066Sahrens 
3588fa9e4066Sahrens 	poolname = argv[0];
3589fa9e4066Sahrens 
3590fa9e4066Sahrens 	if (argc < 2) {
3591fa9e4066Sahrens 		(void) fprintf(stderr,
3592fa9e4066Sahrens 		    gettext("missing <device> specification\n"));
359399653d4eSeschrock 		usage(B_FALSE);
3594fa9e4066Sahrens 	}
3595fa9e4066Sahrens 
3596fa9e4066Sahrens 	old_disk = argv[1];
3597fa9e4066Sahrens 
3598fa9e4066Sahrens 	if (argc < 3) {
3599fa9e4066Sahrens 		if (!replacing) {
3600fa9e4066Sahrens 			(void) fprintf(stderr,
3601fa9e4066Sahrens 			    gettext("missing <new_device> specification\n"));
360299653d4eSeschrock 			usage(B_FALSE);
3603fa9e4066Sahrens 		}
3604fa9e4066Sahrens 		new_disk = old_disk;
3605fa9e4066Sahrens 		argc -= 1;
3606fa9e4066Sahrens 		argv += 1;
3607fa9e4066Sahrens 	} else {
3608fa9e4066Sahrens 		new_disk = argv[2];
3609fa9e4066Sahrens 		argc -= 2;
3610fa9e4066Sahrens 		argv += 2;
3611fa9e4066Sahrens 	}
3612fa9e4066Sahrens 
3613fa9e4066Sahrens 	if (argc > 1) {
3614fa9e4066Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
361599653d4eSeschrock 		usage(B_FALSE);
3616fa9e4066Sahrens 	}
3617fa9e4066Sahrens 
361899653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3619fa9e4066Sahrens 		return (1);
3620fa9e4066Sahrens 
36218488aeb5Staylor 	if (zpool_get_config(zhp, NULL) == NULL) {
3622fa9e4066Sahrens 		(void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3623fa9e4066Sahrens 		    poolname);
3624fa9e4066Sahrens 		zpool_close(zhp);
3625fa9e4066Sahrens 		return (1);
3626fa9e4066Sahrens 	}
3627fa9e4066Sahrens 
36287855d95bSToomas Soome 	if (zpool_is_bootable(zhp))
36297855d95bSToomas Soome 		boot_type = ZPOOL_COPY_BOOT_LABEL;
36307855d95bSToomas Soome 	else
36317855d95bSToomas Soome 		boot_type = ZPOOL_NO_BOOT_LABEL;
36327855d95bSToomas Soome 
36337855d95bSToomas Soome 	boot_size = zpool_get_prop_int(zhp, ZPOOL_PROP_BOOTSIZE, NULL);
3634705040edSEric Taylor 	nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
36357855d95bSToomas Soome 	    boot_type, boot_size, argc, argv);
3636fa9e4066Sahrens 	if (nvroot == NULL) {
3637fa9e4066Sahrens 		zpool_close(zhp);
3638fa9e4066Sahrens 		return (1);
3639fa9e4066Sahrens 	}
3640fa9e4066Sahrens 
364199653d4eSeschrock 	ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
364299653d4eSeschrock 
364399653d4eSeschrock 	nvlist_free(nvroot);
364499653d4eSeschrock 	zpool_close(zhp);
364599653d4eSeschrock 
364699653d4eSeschrock 	return (ret);
3647fa9e4066Sahrens }
3648fa9e4066Sahrens 
3649fa9e4066Sahrens /*
3650fa9e4066Sahrens  * zpool replace [-f] <pool> <device> <new_device>
3651fa9e4066Sahrens  *
3652fa9e4066Sahrens  *	-f	Force attach, even if <new_device> appears to be in use.
3653fa9e4066Sahrens  *
3654fa9e4066Sahrens  * Replace <device> with <new_device>.
3655fa9e4066Sahrens  */
3656fa9e4066Sahrens /* ARGSUSED */
3657fa9e4066Sahrens int
3658fa9e4066Sahrens zpool_do_replace(int argc, char **argv)
3659fa9e4066Sahrens {
3660fa9e4066Sahrens 	return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3661fa9e4066Sahrens }
3662fa9e4066Sahrens 
3663fa9e4066Sahrens /*
3664fa9e4066Sahrens  * zpool attach [-f] <pool> <device> <new_device>
3665fa9e4066Sahrens  *
3666fa9e4066Sahrens  *	-f	Force attach, even if <new_device> appears to be in use.
3667fa9e4066Sahrens  *
3668fa9e4066Sahrens  * Attach <new_device> to the mirror containing <device>.  If <device> is not
3669fa9e4066Sahrens  * part of a mirror, then <device> will be transformed into a mirror of
3670fa9e4066Sahrens  * <device> and <new_device>.  In either case, <new_device> will begin life
3671fa9e4066Sahrens  * with a DTL of [0, now], and will immediately begin to resilver itself.
3672fa9e4066Sahrens  */
3673fa9e4066Sahrens int
3674fa9e4066Sahrens zpool_do_attach(int argc, char **argv)
3675fa9e4066Sahrens {
3676fa9e4066Sahrens 	return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3677fa9e4066Sahrens }
3678fa9e4066Sahrens 
3679fa9e4066Sahrens /*
3680fa9e4066Sahrens  * zpool detach [-f] <pool> <device>
3681fa9e4066Sahrens  *
3682fa9e4066Sahrens  *	-f	Force detach of <device>, even if DTLs argue against it
3683fa9e4066Sahrens  *		(not supported yet)
3684fa9e4066Sahrens  *
3685fa9e4066Sahrens  * Detach a device from a mirror.  The operation will be refused if <device>
3686fa9e4066Sahrens  * is the last device in the mirror, or if the DTLs indicate that this device
3687fa9e4066Sahrens  * has the only valid copy of some data.
3688fa9e4066Sahrens  */
3689fa9e4066Sahrens /* ARGSUSED */
3690fa9e4066Sahrens int
3691fa9e4066Sahrens zpool_do_detach(int argc, char **argv)
3692fa9e4066Sahrens {
3693fa9e4066Sahrens 	int c;
3694fa9e4066Sahrens 	char *poolname, *path;
3695fa9e4066Sahrens 	zpool_handle_t *zhp;
369699653d4eSeschrock 	int ret;
3697fa9e4066Sahrens 
3698fa9e4066Sahrens 	/* check options */
3699fa9e4066Sahrens 	while ((c = getopt(argc, argv, "f")) != -1) {
3700fa9e4066Sahrens 		switch (c) {
3701fa9e4066Sahrens 		case 'f':
3702fa9e4066Sahrens 		case '?':
3703fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3704fa9e4066Sahrens 			    optopt);
370599653d4eSeschrock 			usage(B_FALSE);
3706fa9e4066Sahrens 		}
3707fa9e4066Sahrens 	}
3708fa9e4066Sahrens 
3709fa9e4066Sahrens 	argc -= optind;
3710fa9e4066Sahrens 	argv += optind;
3711fa9e4066Sahrens 
3712fa9e4066Sahrens 	/* get pool name and check number of arguments */
3713fa9e4066Sahrens 	if (argc < 1) {
3714fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
371599653d4eSeschrock 		usage(B_FALSE);
3716fa9e4066Sahrens 	}
3717fa9e4066Sahrens 
3718fa9e4066Sahrens 	if (argc < 2) {
3719fa9e4066Sahrens 		(void) fprintf(stderr,
3720fa9e4066Sahrens 		    gettext("missing <device> specification\n"));
372199653d4eSeschrock 		usage(B_FALSE);
3722fa9e4066Sahrens 	}
3723fa9e4066Sahrens 
3724fa9e4066Sahrens 	poolname = argv[0];
3725fa9e4066Sahrens 	path = argv[1];
3726fa9e4066Sahrens 
372799653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3728fa9e4066Sahrens 		return (1);
3729fa9e4066Sahrens 
373099653d4eSeschrock 	ret = zpool_vdev_detach(zhp, path);
373199653d4eSeschrock 
373299653d4eSeschrock 	zpool_close(zhp);
373399653d4eSeschrock 
373499653d4eSeschrock 	return (ret);
3735fa9e4066Sahrens }
3736fa9e4066Sahrens 
37371195e687SMark J Musante /*
37381195e687SMark J Musante  * zpool split [-n] [-o prop=val] ...
37391195e687SMark J Musante  *		[-o mntopt] ...
37401195e687SMark J Musante  *		[-R altroot] <pool> <newpool> [<device> ...]
37411195e687SMark J Musante  *
37421195e687SMark J Musante  *	-n	Do not split the pool, but display the resulting layout if
37431195e687SMark J Musante  *		it were to be split.
37441195e687SMark J Musante  *	-o	Set property=value, or set mount options.
37451195e687SMark J Musante  *	-R	Mount the split-off pool under an alternate root.
37461195e687SMark J Musante  *
37471195e687SMark J Musante  * Splits the named pool and gives it the new pool name.  Devices to be split
37481195e687SMark J Musante  * off may be listed, provided that no more than one device is specified
37491195e687SMark J Musante  * per top-level vdev mirror.  The newly split pool is left in an exported
37501195e687SMark J Musante  * state unless -R is specified.
37511195e687SMark J Musante  *
37521195e687SMark J Musante  * Restrictions: the top-level of the pool pool must only be made up of
37531195e687SMark J Musante  * mirrors; all devices in the pool must be healthy; no device may be
37541195e687SMark J Musante  * undergoing a resilvering operation.
37551195e687SMark J Musante  */
37561195e687SMark J Musante int
37571195e687SMark J Musante zpool_do_split(int argc, char **argv)
37581195e687SMark J Musante {
37591195e687SMark J Musante 	char *srcpool, *newpool, *propval;
37601195e687SMark J Musante 	char *mntopts = NULL;
37611195e687SMark J Musante 	splitflags_t flags;
37621195e687SMark J Musante 	int c, ret = 0;
37631195e687SMark J Musante 	zpool_handle_t *zhp;
37641195e687SMark J Musante 	nvlist_t *config, *props = NULL;
37651195e687SMark J Musante 
37661195e687SMark J Musante 	flags.dryrun = B_FALSE;
37671195e687SMark J Musante 	flags.import = B_FALSE;
37681195e687SMark J Musante 
37691195e687SMark J Musante 	/* check options */
37701195e687SMark J Musante 	while ((c = getopt(argc, argv, ":R:no:")) != -1) {
37711195e687SMark J Musante 		switch (c) {
37721195e687SMark J Musante 		case 'R':
37731195e687SMark J Musante 			flags.import = B_TRUE;
37741195e687SMark J Musante 			if (add_prop_list(
37751195e687SMark J Musante 			    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
37761195e687SMark J Musante 			    &props, B_TRUE) != 0) {
3777aab83bb8SJosef 'Jeff' Sipek 				nvlist_free(props);
37781195e687SMark J Musante 				usage(B_FALSE);
37791195e687SMark J Musante 			}
37801195e687SMark J Musante 			break;
37811195e687SMark J Musante 		case 'n':
37821195e687SMark J Musante 			flags.dryrun = B_TRUE;
37831195e687SMark J Musante 			break;
37841195e687SMark J Musante 		case 'o':
37851195e687SMark J Musante 			if ((propval = strchr(optarg, '=')) != NULL) {
37861195e687SMark J Musante 				*propval = '\0';
37871195e687SMark J Musante 				propval++;
37881195e687SMark J Musante 				if (add_prop_list(optarg, propval,
37891195e687SMark J Musante 				    &props, B_TRUE) != 0) {
3790aab83bb8SJosef 'Jeff' Sipek 					nvlist_free(props);
37911195e687SMark J Musante 					usage(B_FALSE);
37921195e687SMark J Musante 				}
37931195e687SMark J Musante 			} else {
37941195e687SMark J Musante 				mntopts = optarg;
37951195e687SMark J Musante 			}
37961195e687SMark J Musante 			break;
37971195e687SMark J Musante 		case ':':
37981195e687SMark J Musante 			(void) fprintf(stderr, gettext("missing argument for "
37991195e687SMark J Musante 			    "'%c' option\n"), optopt);
38001195e687SMark J Musante 			usage(B_FALSE);
38011195e687SMark J Musante 			break;
38021195e687SMark J Musante 		case '?':
38031195e687SMark J Musante 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
38041195e687SMark J Musante 			    optopt);
38051195e687SMark J Musante 			usage(B_FALSE);
38061195e687SMark J Musante 			break;
38071195e687SMark J Musante 		}
38081195e687SMark J Musante 	}
38091195e687SMark J Musante 
38101195e687SMark J Musante 	if (!flags.import && mntopts != NULL) {
38111195e687SMark J Musante 		(void) fprintf(stderr, gettext("setting mntopts is only "
38121195e687SMark J Musante 		    "valid when importing the pool\n"));
38131195e687SMark J Musante 		usage(B_FALSE);
38141195e687SMark J Musante 	}
38151195e687SMark J Musante 
38161195e687SMark J Musante 	argc -= optind;
38171195e687SMark J Musante 	argv += optind;
38181195e687SMark J Musante 
38191195e687SMark J Musante 	if (argc < 1) {
38201195e687SMark J Musante 		(void) fprintf(stderr, gettext("Missing pool name\n"));
38211195e687SMark J Musante 		usage(B_FALSE);
38221195e687SMark J Musante 	}
38231195e687SMark J Musante 	if (argc < 2) {
38241195e687SMark J Musante 		(void) fprintf(stderr, gettext("Missing new pool name\n"));
38251195e687SMark J Musante 		usage(B_FALSE);
38261195e687SMark J Musante 	}
38271195e687SMark J Musante 
38281195e687SMark J Musante 	srcpool = argv[0];
38291195e687SMark J Musante 	newpool = argv[1];
38301195e687SMark J Musante 
38311195e687SMark J Musante 	argc -= 2;
38321195e687SMark J Musante 	argv += 2;
38331195e687SMark J Musante 
38341195e687SMark J Musante 	if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
38351195e687SMark J Musante 		return (1);
38361195e687SMark J Musante 
38371195e687SMark J Musante 	config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
38381195e687SMark J Musante 	if (config == NULL) {
38391195e687SMark J Musante 		ret = 1;
38401195e687SMark J Musante 	} else {
38411195e687SMark J Musante 		if (flags.dryrun) {
38421195e687SMark J Musante 			(void) printf(gettext("would create '%s' with the "
38431195e687SMark J Musante 			    "following layout:\n\n"), newpool);
38441195e687SMark J Musante 			print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
38451195e687SMark J Musante 		}
38461195e687SMark J Musante 		nvlist_free(config);
38471195e687SMark J Musante 	}
38481195e687SMark J Musante 
38491195e687SMark J Musante 	zpool_close(zhp);
38501195e687SMark J Musante 
38511195e687SMark J Musante 	if (ret != 0 || flags.dryrun || !flags.import)
38521195e687SMark J Musante 		return (ret);
38531195e687SMark J Musante 
38541195e687SMark J Musante 	/*
38551195e687SMark J Musante 	 * The split was successful. Now we need to open the new
38561195e687SMark J Musante 	 * pool and import it.
38571195e687SMark J Musante 	 */
38581195e687SMark J Musante 	if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
38591195e687SMark J Musante 		return (1);
38601195e687SMark J Musante 	if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
38611195e687SMark J Musante 	    zpool_enable_datasets(zhp, mntopts, 0) != 0) {
38621195e687SMark J Musante 		ret = 1;
3863fe7cd8aaSCyril Plisko 		(void) fprintf(stderr, gettext("Split was successful, but "
38641195e687SMark J Musante 		    "the datasets could not all be mounted\n"));
38651195e687SMark J Musante 		(void) fprintf(stderr, gettext("Try doing '%s' with a "
38661195e687SMark J Musante 		    "different altroot\n"), "zpool import");
38671195e687SMark J Musante 	}
38681195e687SMark J Musante 	zpool_close(zhp);
38691195e687SMark J Musante 
38701195e687SMark J Musante 	return (ret);
38711195e687SMark J Musante }
38721195e687SMark J Musante 
38731195e687SMark J Musante 
38741195e687SMark J Musante 
3875fa9e4066Sahrens /*
3876441d80aaSlling  * zpool online <pool> <device> ...
3877fa9e4066Sahrens  */
3878fa9e4066Sahrens int
3879fa9e4066Sahrens zpool_do_online(int argc, char **argv)
3880fa9e4066Sahrens {
3881fa9e4066Sahrens 	int c, i;
3882fa9e4066Sahrens 	char *poolname;
3883fa9e4066Sahrens 	zpool_handle_t *zhp;
3884fa9e4066Sahrens 	int ret = 0;
38853d7072f8Seschrock 	vdev_state_t newstate;
3886573ca77eSGeorge Wilson 	int flags = 0;
3887fa9e4066Sahrens 
3888fa9e4066Sahrens 	/* check options */
3889573ca77eSGeorge Wilson 	while ((c = getopt(argc, argv, "et")) != -1) {
3890fa9e4066Sahrens 		switch (c) {
3891573ca77eSGeorge Wilson 		case 'e':
3892573ca77eSGeorge Wilson 			flags |= ZFS_ONLINE_EXPAND;
3893573ca77eSGeorge Wilson 			break;
3894fa9e4066Sahrens 		case 't':
3895fa9e4066Sahrens 		case '?':
3896fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3897fa9e4066Sahrens 			    optopt);
389899653d4eSeschrock 			usage(B_FALSE);
3899fa9e4066Sahrens 		}
3900fa9e4066Sahrens 	}
3901fa9e4066Sahrens 
3902fa9e4066Sahrens 	argc -= optind;
3903fa9e4066Sahrens 	argv += optind;
3904fa9e4066Sahrens 
3905fa9e4066Sahrens 	/* get pool name and check number of arguments */
3906fa9e4066Sahrens 	if (argc < 1) {
3907fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name\n"));
390899653d4eSeschrock 		usage(B_FALSE);
3909fa9e4066Sahrens 	}
3910fa9e4066Sahrens 	if (argc < 2) {
3911fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing device name\n"));
391299653d4eSeschrock 		usage(B_FALSE);
3913fa9e4066Sahrens 	}
3914fa9e4066Sahrens 
3915fa9e4066Sahrens 	poolname = argv[0];
3916fa9e4066Sahrens 
391799653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3918fa9e4066Sahrens 		return (1);
3919fa9e4066Sahrens 
39203d7072f8Seschrock 	for (i = 1; i < argc; i++) {
3921573ca77eSGeorge Wilson 		if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
39223d7072f8Seschrock 			if (newstate != VDEV_STATE_HEALTHY) {
39233d7072f8Seschrock 				(void) printf(gettext("warning: device '%s' "
39243d7072f8Seschrock 				    "onlined, but remains in faulted state\n"),
39253d7072f8Seschrock 				    argv[i]);
39263d7072f8Seschrock 				if (newstate == VDEV_STATE_FAULTED)
39273d7072f8Seschrock 					(void) printf(gettext("use 'zpool "
39283d7072f8Seschrock 					    "clear' to restore a faulted "
39293d7072f8Seschrock 					    "device\n"));
39303d7072f8Seschrock 				else
39313d7072f8Seschrock 					(void) printf(gettext("use 'zpool "
39323d7072f8Seschrock 					    "replace' to replace devices "
39333d7072f8Seschrock 					    "that are no longer present\n"));
39343d7072f8Seschrock 			}
39353d7072f8Seschrock 		} else {
3936fa9e4066Sahrens 			ret = 1;
39373d7072f8Seschrock 		}
39383d7072f8Seschrock 	}
3939fa9e4066Sahrens 
394099653d4eSeschrock 	zpool_close(zhp);
394199653d4eSeschrock 
3942fa9e4066Sahrens 	return (ret);
3943fa9e4066Sahrens }
3944fa9e4066Sahrens 
3945fa9e4066Sahrens /*
3946441d80aaSlling  * zpool offline [-ft] <pool> <device> ...
3947fa9e4066Sahrens  *
3948fa9e4066Sahrens  *	-f	Force the device into the offline state, even if doing
3949fa9e4066Sahrens  *		so would appear to compromise pool availability.
3950fa9e4066Sahrens  *		(not supported yet)
3951fa9e4066Sahrens  *
3952fa9e4066Sahrens  *	-t	Only take the device off-line temporarily.  The offline
3953fa9e4066Sahrens  *		state will not be persistent across reboots.
3954fa9e4066Sahrens  */
3955fa9e4066Sahrens /* ARGSUSED */
3956fa9e4066Sahrens int
3957fa9e4066Sahrens zpool_do_offline(int argc, char **argv)
3958fa9e4066Sahrens {
3959fa9e4066Sahrens 	int c, i;
3960fa9e4066Sahrens 	char *poolname;
3961fa9e4066Sahrens 	zpool_handle_t *zhp;
396299653d4eSeschrock 	int ret = 0;
396399653d4eSeschrock 	boolean_t istmp = B_FALSE;
3964fa9e4066Sahrens 
3965fa9e4066Sahrens 	/* check options */
3966fa9e4066Sahrens 	while ((c = getopt(argc, argv, "ft")) != -1) {
3967fa9e4066Sahrens 		switch (c) {
3968fa9e4066Sahrens 		case 't':
396999653d4eSeschrock 			istmp = B_TRUE;
3970441d80aaSlling 			break;
3971441d80aaSlling 		case 'f':
3972fa9e4066Sahrens 		case '?':
3973fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3974fa9e4066Sahrens 			    optopt);
397599653d4eSeschrock 			usage(B_FALSE);
3976fa9e4066Sahrens 		}
3977fa9e4066Sahrens 	}
3978fa9e4066Sahrens 
3979fa9e4066Sahrens 	argc -= optind;
3980fa9e4066Sahrens 	argv += optind;
3981fa9e4066Sahrens 
3982fa9e4066Sahrens 	/* get pool name and check number of arguments */
3983fa9e4066Sahrens 	if (argc < 1) {
3984fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name\n"));
398599653d4eSeschrock 		usage(B_FALSE);
3986fa9e4066Sahrens 	}
3987fa9e4066Sahrens 	if (argc < 2) {
3988fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing device name\n"));
398999653d4eSeschrock 		usage(B_FALSE);
3990fa9e4066Sahrens 	}
3991fa9e4066Sahrens 
3992fa9e4066Sahrens 	poolname = argv[0];
3993fa9e4066Sahrens 
399499653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3995fa9e4066Sahrens 		return (1);
3996fa9e4066Sahrens 
39973d7072f8Seschrock 	for (i = 1; i < argc; i++) {
39983d7072f8Seschrock 		if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3999fa9e4066Sahrens 			ret = 1;
40003d7072f8Seschrock 	}
4001fa9e4066Sahrens 
400299653d4eSeschrock 	zpool_close(zhp);
400399653d4eSeschrock 
4004fa9e4066Sahrens 	return (ret);
4005fa9e4066Sahrens }
4006fa9e4066Sahrens 
4007ea8dc4b6Seschrock /*
4008ea8dc4b6Seschrock  * zpool clear <pool> [device]
4009ea8dc4b6Seschrock  *
4010ea8dc4b6Seschrock  * Clear all errors associated with a pool or a particular device.
4011ea8dc4b6Seschrock  */
4012ea8dc4b6Seschrock int
4013ea8dc4b6Seschrock zpool_do_clear(int argc, char **argv)
4014ea8dc4b6Seschrock {
4015468c413aSTim Haley 	int c;
4016ea8dc4b6Seschrock 	int ret = 0;
4017468c413aSTim Haley 	boolean_t dryrun = B_FALSE;
4018468c413aSTim Haley 	boolean_t do_rewind = B_FALSE;
4019468c413aSTim Haley 	boolean_t xtreme_rewind = B_FALSE;
4020468c413aSTim Haley 	uint32_t rewind_policy = ZPOOL_NO_REWIND;
4021468c413aSTim Haley 	nvlist_t *policy = NULL;
4022ea8dc4b6Seschrock 	zpool_handle_t *zhp;
4023ea8dc4b6Seschrock 	char *pool, *device;
4024ea8dc4b6Seschrock 
4025468c413aSTim Haley 	/* check options */
4026468c413aSTim Haley 	while ((c = getopt(argc, argv, "FnX")) != -1) {
4027468c413aSTim Haley 		switch (c) {
4028468c413aSTim Haley 		case 'F':
4029468c413aSTim Haley 			do_rewind = B_TRUE;
4030468c413aSTim Haley 			break;
4031468c413aSTim Haley 		case 'n':
4032468c413aSTim Haley 			dryrun = B_TRUE;
4033468c413aSTim Haley 			break;
4034468c413aSTim Haley 		case 'X':
4035468c413aSTim Haley 			xtreme_rewind = B_TRUE;
4036468c413aSTim Haley 			break;
4037468c413aSTim Haley 		case '?':
4038468c413aSTim Haley 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4039468c413aSTim Haley 			    optopt);
4040468c413aSTim Haley 			usage(B_FALSE);
4041468c413aSTim Haley 		}
4042468c413aSTim Haley 	}
4043468c413aSTim Haley 
4044468c413aSTim Haley 	argc -= optind;
4045468c413aSTim Haley 	argv += optind;
4046468c413aSTim Haley 
4047468c413aSTim Haley 	if (argc < 1) {
4048ea8dc4b6Seschrock 		(void) fprintf(stderr, gettext("missing pool name\n"));
404999653d4eSeschrock 		usage(B_FALSE);
4050ea8dc4b6Seschrock 	}
4051ea8dc4b6Seschrock 
4052468c413aSTim Haley 	if (argc > 2) {
4053ea8dc4b6Seschrock 		(void) fprintf(stderr, gettext("too many arguments\n"));
405499653d4eSeschrock 		usage(B_FALSE);
4055ea8dc4b6Seschrock 	}
4056ea8dc4b6Seschrock 
4057468c413aSTim Haley 	if ((dryrun || xtreme_rewind) && !do_rewind) {
4058468c413aSTim Haley 		(void) fprintf(stderr,
4059468c413aSTim Haley 		    gettext("-n or -X only meaningful with -F\n"));
4060468c413aSTim Haley 		usage(B_FALSE);
4061468c413aSTim Haley 	}
4062468c413aSTim Haley 	if (dryrun)
4063468c413aSTim Haley 		rewind_policy = ZPOOL_TRY_REWIND;
4064468c413aSTim Haley 	else if (do_rewind)
4065468c413aSTim Haley 		rewind_policy = ZPOOL_DO_REWIND;
4066468c413aSTim Haley 	if (xtreme_rewind)
4067468c413aSTim Haley 		rewind_policy |= ZPOOL_EXTREME_REWIND;
4068468c413aSTim Haley 
4069468c413aSTim Haley 	/* In future, further rewind policy choices can be passed along here */
4070468c413aSTim Haley 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
40715dafeea3SPavel Zakharov 	    nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY,
40725dafeea3SPavel Zakharov 	    rewind_policy) != 0) {
4073468c413aSTim Haley 		return (1);
40745dafeea3SPavel Zakharov 	}
4075468c413aSTim Haley 
4076468c413aSTim Haley 	pool = argv[0];
4077468c413aSTim Haley 	device = argc == 2 ? argv[1] : NULL;
4078ea8dc4b6Seschrock 
4079468c413aSTim Haley 	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
4080468c413aSTim Haley 		nvlist_free(policy);
4081ea8dc4b6Seschrock 		return (1);
4082468c413aSTim Haley 	}
4083ea8dc4b6Seschrock 
4084468c413aSTim Haley 	if (zpool_clear(zhp, device, policy) != 0)
4085ea8dc4b6Seschrock 		ret = 1;
4086ea8dc4b6Seschrock 
4087ea8dc4b6Seschrock 	zpool_close(zhp);
4088ea8dc4b6Seschrock 
4089468c413aSTim Haley 	nvlist_free(policy);
4090468c413aSTim Haley 
4091ea8dc4b6Seschrock 	return (ret);
4092ea8dc4b6Seschrock }
4093ea8dc4b6Seschrock 
4094e9103aaeSGarrett D'Amore /*
4095e9103aaeSGarrett D'Amore  * zpool reguid <pool>
4096e9103aaeSGarrett D'Amore  */
4097e9103aaeSGarrett D'Amore int
4098e9103aaeSGarrett D'Amore zpool_do_reguid(int argc, char **argv)
4099e9103aaeSGarrett D'Amore {
4100e9103aaeSGarrett D'Amore 	int c;
4101e9103aaeSGarrett D'Amore 	char *poolname;
4102e9103aaeSGarrett D'Amore 	zpool_handle_t *zhp;
4103e9103aaeSGarrett D'Amore 	int ret = 0;
4104e9103aaeSGarrett D'Amore 
4105e9103aaeSGarrett D'Amore 	/* check options */
4106e9103aaeSGarrett D'Amore 	while ((c = getopt(argc, argv, "")) != -1) {
4107e9103aaeSGarrett D'Amore 		switch (c) {
4108e9103aaeSGarrett D'Amore 		case '?':
4109e9103aaeSGarrett D'Amore 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4110e9103aaeSGarrett D'Amore 			    optopt);
4111e9103aaeSGarrett D'Amore 			usage(B_FALSE);
4112e9103aaeSGarrett D'Amore 		}
4113e9103aaeSGarrett D'Amore 	}
4114e9103aaeSGarrett D'Amore 
4115e9103aaeSGarrett D'Amore 	argc -= optind;
4116e9103aaeSGarrett D'Amore 	argv += optind;
4117e9103aaeSGarrett D'Amore 
4118e9103aaeSGarrett D'Amore 	/* get pool name and check number of arguments */
4119e9103aaeSGarrett D'Amore 	if (argc < 1) {
4120e9103aaeSGarrett D'Amore 		(void) fprintf(stderr, gettext("missing pool name\n"));
4121e9103aaeSGarrett D'Amore 		usage(B_FALSE);
4122e9103aaeSGarrett D'Amore 	}
4123e9103aaeSGarrett D'Amore 
4124e9103aaeSGarrett D'Amore 	if (argc > 1) {
4125e9103aaeSGarrett D'Amore 		(void) fprintf(stderr, gettext("too many arguments\n"));
4126e9103aaeSGarrett D'Amore 		usage(B_FALSE);
4127e9103aaeSGarrett D'Amore 	}
4128e9103aaeSGarrett D'Amore 
4129e9103aaeSGarrett D'Amore 	poolname = argv[0];
4130e9103aaeSGarrett D'Amore 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
4131e9103aaeSGarrett D'Amore 		return (1);
4132e9103aaeSGarrett D'Amore 
4133e9103aaeSGarrett D'Amore 	ret = zpool_reguid(zhp);
4134e9103aaeSGarrett D'Amore 
4135e9103aaeSGarrett D'Amore 	zpool_close(zhp);
4136e9103aaeSGarrett D'Amore 	return (ret);
4137e9103aaeSGarrett D'Amore }
4138e9103aaeSGarrett D'Amore 
4139e9103aaeSGarrett D'Amore 
41404263d13fSGeorge Wilson /*
41414263d13fSGeorge Wilson  * zpool reopen <pool>
41424263d13fSGeorge Wilson  *
41434263d13fSGeorge Wilson  * Reopen the pool so that the kernel can update the sizes of all vdevs.
41444263d13fSGeorge Wilson  */
41454263d13fSGeorge Wilson int
41464263d13fSGeorge Wilson zpool_do_reopen(int argc, char **argv)
41474263d13fSGeorge Wilson {
414831d7e8faSGeorge Wilson 	int c;
41494263d13fSGeorge Wilson 	int ret = 0;
41504263d13fSGeorge Wilson 	zpool_handle_t *zhp;
41514263d13fSGeorge Wilson 	char *pool;
41524263d13fSGeorge Wilson 
415331d7e8faSGeorge Wilson 	/* check options */
415431d7e8faSGeorge Wilson 	while ((c = getopt(argc, argv, "")) != -1) {
415531d7e8faSGeorge Wilson 		switch (c) {
415631d7e8faSGeorge Wilson 		case '?':
415731d7e8faSGeorge Wilson 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
415831d7e8faSGeorge Wilson 			    optopt);
415931d7e8faSGeorge Wilson 			usage(B_FALSE);
416031d7e8faSGeorge Wilson 		}
416131d7e8faSGeorge Wilson 	}
416231d7e8faSGeorge Wilson 
41634263d13fSGeorge Wilson 	argc--;
41644263d13fSGeorge Wilson 	argv++;
41654263d13fSGeorge Wilson 
416631d7e8faSGeorge Wilson 	if (argc < 1) {
416731d7e8faSGeorge Wilson 		(void) fprintf(stderr, gettext("missing pool name\n"));
416831d7e8faSGeorge Wilson 		usage(B_FALSE);
416931d7e8faSGeorge Wilson 	}
417031d7e8faSGeorge Wilson 
417131d7e8faSGeorge Wilson 	if (argc > 1) {
417231d7e8faSGeorge Wilson 		(void) fprintf(stderr, gettext("too many arguments\n"));
417331d7e8faSGeorge Wilson 		usage(B_FALSE);
417431d7e8faSGeorge Wilson 	}
41754263d13fSGeorge Wilson 
41764263d13fSGeorge Wilson 	pool = argv[0];
41774263d13fSGeorge Wilson 	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
41784263d13fSGeorge Wilson 		return (1);
41794263d13fSGeorge Wilson 
41804263d13fSGeorge Wilson 	ret = zpool_reopen(zhp);
41814263d13fSGeorge Wilson 	zpool_close(zhp);
41824263d13fSGeorge Wilson 	return (ret);
41834263d13fSGeorge Wilson }
41844263d13fSGeorge Wilson 
4185fa9e4066Sahrens typedef struct scrub_cbdata {
4186fa9e4066Sahrens 	int	cb_type;
418706eeb2adSek 	int	cb_argc;
418806eeb2adSek 	char	**cb_argv;
41891702cce7SAlek Pinchuk 	pool_scrub_cmd_t cb_scrub_cmd;
4190fa9e4066Sahrens } scrub_cbdata_t;
4191fa9e4066Sahrens 
419286714001SSerapheim Dimitropoulos static boolean_t
419386714001SSerapheim Dimitropoulos zpool_has_checkpoint(zpool_handle_t *zhp)
419486714001SSerapheim Dimitropoulos {
419586714001SSerapheim Dimitropoulos 	nvlist_t *config, *nvroot;
419686714001SSerapheim Dimitropoulos 
419786714001SSerapheim Dimitropoulos 	config = zpool_get_config(zhp, NULL);
419886714001SSerapheim Dimitropoulos 
419986714001SSerapheim Dimitropoulos 	if (config != NULL) {
420086714001SSerapheim Dimitropoulos 		pool_checkpoint_stat_t *pcs = NULL;
420186714001SSerapheim Dimitropoulos 		uint_t c;
420286714001SSerapheim Dimitropoulos 
420386714001SSerapheim Dimitropoulos 		nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
420486714001SSerapheim Dimitropoulos 		(void) nvlist_lookup_uint64_array(nvroot,
420586714001SSerapheim Dimitropoulos 		    ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
420686714001SSerapheim Dimitropoulos 
420786714001SSerapheim Dimitropoulos 		if (pcs == NULL || pcs->pcs_state == CS_NONE)
420886714001SSerapheim Dimitropoulos 			return (B_FALSE);
420986714001SSerapheim Dimitropoulos 
421086714001SSerapheim Dimitropoulos 		assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS ||
421186714001SSerapheim Dimitropoulos 		    pcs->pcs_state == CS_CHECKPOINT_DISCARDING);
421286714001SSerapheim Dimitropoulos 		return (B_TRUE);
421386714001SSerapheim Dimitropoulos 	}
421486714001SSerapheim Dimitropoulos 
421586714001SSerapheim Dimitropoulos 	return (B_FALSE);
421686714001SSerapheim Dimitropoulos }
421786714001SSerapheim Dimitropoulos 
4218fa9e4066Sahrens int
4219fa9e4066Sahrens scrub_callback(zpool_handle_t *zhp, void *data)
4220fa9e4066Sahrens {
4221fa9e4066Sahrens 	scrub_cbdata_t *cb = data;
422206eeb2adSek 	int err;
4223fa9e4066Sahrens 
4224ea8dc4b6Seschrock 	/*
4225ea8dc4b6Seschrock 	 * Ignore faulted pools.
4226ea8dc4b6Seschrock 	 */
4227ea8dc4b6Seschrock 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
4228ea8dc4b6Seschrock 		(void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
4229ea8dc4b6Seschrock 		    "currently unavailable\n"), zpool_get_name(zhp));
4230ea8dc4b6Seschrock 		return (1);
4231ea8dc4b6Seschrock 	}
4232ea8dc4b6Seschrock 
42331702cce7SAlek Pinchuk 	err = zpool_scan(zhp, cb->cb_type, cb->cb_scrub_cmd);
423406eeb2adSek 
423586714001SSerapheim Dimitropoulos 	if (err == 0 && zpool_has_checkpoint(zhp) &&
423686714001SSerapheim Dimitropoulos 	    cb->cb_type == POOL_SCAN_SCRUB) {
423786714001SSerapheim Dimitropoulos 		(void) printf(gettext("warning: will not scrub state that "
423886714001SSerapheim Dimitropoulos 		    "belongs to the checkpoint of pool '%s'\n"),
423986714001SSerapheim Dimitropoulos 		    zpool_get_name(zhp));
424086714001SSerapheim Dimitropoulos 	}
424186714001SSerapheim Dimitropoulos 
424206eeb2adSek 	return (err != 0);
4243fa9e4066Sahrens }
4244fa9e4066Sahrens 
4245fa9e4066Sahrens /*
42461702cce7SAlek Pinchuk  * zpool scrub [-s | -p] <pool> ...
4247fa9e4066Sahrens  *
4248fa9e4066Sahrens  *	-s	Stop.  Stops any in-progress scrub.
42491702cce7SAlek Pinchuk  *	-p	Pause. Pause in-progress scrub.
4250fa9e4066Sahrens  */
4251fa9e4066Sahrens int
4252fa9e4066Sahrens zpool_do_scrub(int argc, char **argv)
4253fa9e4066Sahrens {
4254fa9e4066Sahrens 	int c;
4255fa9e4066Sahrens 	scrub_cbdata_t cb;
4256fa9e4066Sahrens 
42573f9d6ad7SLin Ling 	cb.cb_type = POOL_SCAN_SCRUB;
42581702cce7SAlek Pinchuk 	cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
4259fa9e4066Sahrens 
4260fa9e4066Sahrens 	/* check options */
42611702cce7SAlek Pinchuk 	while ((c = getopt(argc, argv, "sp")) != -1) {
4262fa9e4066Sahrens 		switch (c) {
4263fa9e4066Sahrens 		case 's':
42643f9d6ad7SLin Ling 			cb.cb_type = POOL_SCAN_NONE;
4265fa9e4066Sahrens 			break;
42661702cce7SAlek Pinchuk 		case 'p':
42671702cce7SAlek Pinchuk 			cb.cb_scrub_cmd = POOL_SCRUB_PAUSE;
42681702cce7SAlek Pinchuk 			break;
4269fa9e4066Sahrens 		case '?':
4270fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4271fa9e4066Sahrens 			    optopt);
427299653d4eSeschrock 			usage(B_FALSE);
4273fa9e4066Sahrens 		}
4274fa9e4066Sahrens 	}
4275fa9e4066Sahrens 
42761702cce7SAlek Pinchuk 	if (cb.cb_type == POOL_SCAN_NONE &&
42771702cce7SAlek Pinchuk 	    cb.cb_scrub_cmd == POOL_SCRUB_PAUSE) {
42781702cce7SAlek Pinchuk 		(void) fprintf(stderr, gettext("invalid option combination: "
42791702cce7SAlek Pinchuk 		    "-s and -p are mutually exclusive\n"));
42801702cce7SAlek Pinchuk 		usage(B_FALSE);
42811702cce7SAlek Pinchuk 	}
42821702cce7SAlek Pinchuk 
428306eeb2adSek 	cb.cb_argc = argc;
428406eeb2adSek 	cb.cb_argv = argv;
4285fa9e4066Sahrens 	argc -= optind;
4286fa9e4066Sahrens 	argv += optind;
4287fa9e4066Sahrens 
4288fa9e4066Sahrens 	if (argc < 1) {
4289fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
429099653d4eSeschrock 		usage(B_FALSE);
4291fa9e4066Sahrens 	}
4292fa9e4066Sahrens 
4293b1b8ab34Slling 	return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
4294fa9e4066Sahrens }
4295fa9e4066Sahrens 
4296094e47e9SGeorge Wilson static void
4297094e47e9SGeorge Wilson zpool_collect_leaves(zpool_handle_t *zhp, nvlist_t *nvroot, nvlist_t *res)
4298094e47e9SGeorge Wilson {
4299094e47e9SGeorge Wilson 	uint_t children = 0;
4300094e47e9SGeorge Wilson 	nvlist_t **child;
4301094e47e9SGeorge Wilson 	uint_t i;
4302094e47e9SGeorge Wilson 
4303094e47e9SGeorge Wilson 	(void) nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4304094e47e9SGeorge Wilson 	    &child, &children);
4305094e47e9SGeorge Wilson 
4306094e47e9SGeorge Wilson 	if (children == 0) {
4307094e47e9SGeorge Wilson 		char *path = zpool_vdev_name(g_zfs, zhp, nvroot, B_FALSE);
4308094e47e9SGeorge Wilson 		fnvlist_add_boolean(res, path);
4309094e47e9SGeorge Wilson 		free(path);
4310094e47e9SGeorge Wilson 		return;
4311094e47e9SGeorge Wilson 	}
4312094e47e9SGeorge Wilson 
4313094e47e9SGeorge Wilson 	for (i = 0; i < children; i++) {
4314094e47e9SGeorge Wilson 		zpool_collect_leaves(zhp, child[i], res);
4315094e47e9SGeorge Wilson 	}
4316094e47e9SGeorge Wilson }
4317094e47e9SGeorge Wilson 
4318094e47e9SGeorge Wilson /*
4319094e47e9SGeorge Wilson  * zpool initialize [-cs] <pool> [<vdev> ...]
4320094e47e9SGeorge Wilson  * Initialize all unused blocks in the specified vdevs, or all vdevs in the pool
4321094e47e9SGeorge Wilson  * if none specified.
4322094e47e9SGeorge Wilson  *
4323094e47e9SGeorge Wilson  *	-c	Cancel. Ends active initializing.
4324094e47e9SGeorge Wilson  *	-s	Suspend. Initializing can then be restarted with no flags.
4325094e47e9SGeorge Wilson  */
4326094e47e9SGeorge Wilson int
4327094e47e9SGeorge Wilson zpool_do_initialize(int argc, char **argv)
4328094e47e9SGeorge Wilson {
4329094e47e9SGeorge Wilson 	int c;
4330094e47e9SGeorge Wilson 	char *poolname;
4331094e47e9SGeorge Wilson 	zpool_handle_t *zhp;
4332094e47e9SGeorge Wilson 	nvlist_t *vdevs;
4333094e47e9SGeorge Wilson 	int err = 0;
4334094e47e9SGeorge Wilson 
4335094e47e9SGeorge Wilson 	struct option long_options[] = {
4336094e47e9SGeorge Wilson 		{"cancel",	no_argument,		NULL, 'c'},
4337094e47e9SGeorge Wilson 		{"suspend",	no_argument,		NULL, 's'},
4338094e47e9SGeorge Wilson 		{0, 0, 0, 0}
4339094e47e9SGeorge Wilson 	};
4340094e47e9SGeorge Wilson 
4341094e47e9SGeorge Wilson 	pool_initialize_func_t cmd_type = POOL_INITIALIZE_DO;
4342094e47e9SGeorge Wilson 	while ((c = getopt_long(argc, argv, "cs", long_options, NULL)) != -1) {
4343094e47e9SGeorge Wilson 		switch (c) {
4344094e47e9SGeorge Wilson 		case 'c':
4345094e47e9SGeorge Wilson 			if (cmd_type != POOL_INITIALIZE_DO) {
4346094e47e9SGeorge Wilson 				(void) fprintf(stderr, gettext("-c cannot be "
4347094e47e9SGeorge Wilson 				    "combined with other options\n"));
4348094e47e9SGeorge Wilson 				usage(B_FALSE);
4349094e47e9SGeorge Wilson 			}
4350094e47e9SGeorge Wilson 			cmd_type = POOL_INITIALIZE_CANCEL;
4351094e47e9SGeorge Wilson 			break;
4352094e47e9SGeorge Wilson 		case 's':
4353094e47e9SGeorge Wilson 			if (cmd_type != POOL_INITIALIZE_DO) {
4354094e47e9SGeorge Wilson 				(void) fprintf(stderr, gettext("-s cannot be "
4355094e47e9SGeorge Wilson 				    "combined with other options\n"));
4356094e47e9SGeorge Wilson 				usage(B_FALSE);
4357094e47e9SGeorge Wilson 			}
4358094e47e9SGeorge Wilson 			cmd_type = POOL_INITIALIZE_SUSPEND;
4359094e47e9SGeorge Wilson 			break;
4360094e47e9SGeorge Wilson 		case '?':
4361094e47e9SGeorge Wilson 			if (optopt != 0) {
4362094e47e9SGeorge Wilson 				(void) fprintf(stderr,
4363094e47e9SGeorge Wilson 				    gettext("invalid option '%c'\n"), optopt);
4364094e47e9SGeorge Wilson 			} else {
4365094e47e9SGeorge Wilson 				(void) fprintf(stderr,
4366094e47e9SGeorge Wilson 				    gettext("invalid option '%s'\n"),
4367094e47e9SGeorge Wilson 				    argv[optind - 1]);
4368094e47e9SGeorge Wilson 			}
4369094e47e9SGeorge Wilson 			usage(B_FALSE);
4370094e47e9SGeorge Wilson 		}
4371094e47e9SGeorge Wilson 	}
4372094e47e9SGeorge Wilson 
4373094e47e9SGeorge Wilson 	argc -= optind;
4374094e47e9SGeorge Wilson 	argv += optind;
4375094e47e9SGeorge Wilson 
4376094e47e9SGeorge Wilson 	if (argc < 1) {
4377094e47e9SGeorge Wilson 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
4378094e47e9SGeorge Wilson 		usage(B_FALSE);
4379094e47e9SGeorge Wilson 		return (-1);
4380094e47e9SGeorge Wilson 	}
4381094e47e9SGeorge Wilson 
4382094e47e9SGeorge Wilson 	poolname = argv[0];
4383094e47e9SGeorge Wilson 	zhp = zpool_open(g_zfs, poolname);
4384094e47e9SGeorge Wilson 	if (zhp == NULL)
4385094e47e9SGeorge Wilson 		return (-1);
4386094e47e9SGeorge Wilson 
4387094e47e9SGeorge Wilson 	vdevs = fnvlist_alloc();
4388094e47e9SGeorge Wilson 	if (argc == 1) {
4389094e47e9SGeorge Wilson 		/* no individual leaf vdevs specified, so add them all */
4390094e47e9SGeorge Wilson 		nvlist_t *config = zpool_get_config(zhp, NULL);
4391094e47e9SGeorge Wilson 		nvlist_t *nvroot = fnvlist_lookup_nvlist(config,
4392094e47e9SGeorge Wilson 		    ZPOOL_CONFIG_VDEV_TREE);
4393094e47e9SGeorge Wilson 		zpool_collect_leaves(zhp, nvroot, vdevs);
4394094e47e9SGeorge Wilson 	} else {
4395094e47e9SGeorge Wilson 		int i;
4396094e47e9SGeorge Wilson 		for (i = 1; i < argc; i++) {
4397094e47e9SGeorge Wilson 			fnvlist_add_boolean(vdevs, argv[i]);
4398094e47e9SGeorge Wilson 		}
4399094e47e9SGeorge Wilson 	}
4400094e47e9SGeorge Wilson 
4401094e47e9SGeorge Wilson 	err = zpool_initialize(zhp, cmd_type, vdevs);
4402094e47e9SGeorge Wilson 
4403094e47e9SGeorge Wilson 	fnvlist_free(vdevs);
4404094e47e9SGeorge Wilson 	zpool_close(zhp);
4405094e47e9SGeorge Wilson 
4406094e47e9SGeorge Wilson 	return (err);
4407094e47e9SGeorge Wilson }
4408094e47e9SGeorge Wilson 
4409fa9e4066Sahrens typedef struct status_cbdata {
441099653d4eSeschrock 	int		cb_count;
4411e9dbad6fSeschrock 	boolean_t	cb_allpools;
441299653d4eSeschrock 	boolean_t	cb_verbose;
441399653d4eSeschrock 	boolean_t	cb_explain;
441499653d4eSeschrock 	boolean_t	cb_first;
44159eb19f4dSGeorge Wilson 	boolean_t	cb_dedup_stats;
4416fa9e4066Sahrens } status_cbdata_t;
4417fa9e4066Sahrens 
4418fa9e4066Sahrens /*
4419fa9e4066Sahrens  * Print out detailed scrub status.
4420fa9e4066Sahrens  */
44215cabbc6bSPrashanth Sreenivasa static void
44223f9d6ad7SLin Ling print_scan_status(pool_scan_stat_t *ps)
4423fa9e4066Sahrens {
44241702cce7SAlek Pinchuk 	time_t start, end, pause;
442573d314ceSLin Ling 	uint64_t elapsed, mins_left, hours_left;
44263f9d6ad7SLin Ling 	uint64_t pass_exam, examined, total;
44273f9d6ad7SLin Ling 	uint_t rate;
4428fa9e4066Sahrens 	double fraction_done;
44293f9d6ad7SLin Ling 	char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
4430fa9e4066Sahrens 
4431ce72e614SYuri Pankov 	(void) printf(gettext("  scan: "));
4432fa9e4066Sahrens 
44333f9d6ad7SLin Ling 	/* If there's never been a scan, there's not much to say. */
44343f9d6ad7SLin Ling 	if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
44353f9d6ad7SLin Ling 	    ps->pss_func >= POOL_SCAN_FUNCS) {
4436fa9e4066Sahrens 		(void) printf(gettext("none requested\n"));
4437fa9e4066Sahrens 		return;
4438fa9e4066Sahrens 	}
4439fa9e4066Sahrens 
44403f9d6ad7SLin Ling 	start = ps->pss_start_time;
44413f9d6ad7SLin Ling 	end = ps->pss_end_time;
44421702cce7SAlek Pinchuk 	pause = ps->pss_pass_scrub_pause;
44433f9d6ad7SLin Ling 	zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
4444fa9e4066Sahrens 
44453f9d6ad7SLin Ling 	assert(ps->pss_func == POOL_SCAN_SCRUB ||
44463f9d6ad7SLin Ling 	    ps->pss_func == POOL_SCAN_RESILVER);
44473f9d6ad7SLin Ling 	/*
44483f9d6ad7SLin Ling 	 * Scan is finished or canceled.
44493f9d6ad7SLin Ling 	 */
44503f9d6ad7SLin Ling 	if (ps->pss_state == DSS_FINISHED) {
44513f9d6ad7SLin Ling 		uint64_t minutes_taken = (end - start) / 60;
4452b327cd3fSIgor Kozhukhov 		char *fmt = NULL;
44533f9d6ad7SLin Ling 
44543f9d6ad7SLin Ling 		if (ps->pss_func == POOL_SCAN_SCRUB) {
44553f9d6ad7SLin Ling 			fmt = gettext("scrub repaired %s in %lluh%um with "
44563f9d6ad7SLin Ling 			    "%llu errors on %s");
44573f9d6ad7SLin Ling 		} else if (ps->pss_func == POOL_SCAN_RESILVER) {
44583f9d6ad7SLin Ling 			fmt = gettext("resilvered %s in %lluh%um with "
44593f9d6ad7SLin Ling 			    "%llu errors on %s");
44603f9d6ad7SLin Ling 		}
44613f9d6ad7SLin Ling 		/* LINTED */
44623f9d6ad7SLin Ling 		(void) printf(fmt, processed_buf,
446318ce54dfSek 		    (u_longlong_t)(minutes_taken / 60),
446418ce54dfSek 		    (uint_t)(minutes_taken % 60),
44653f9d6ad7SLin Ling 		    (u_longlong_t)ps->pss_errors,
44663f9d6ad7SLin Ling 		    ctime((time_t *)&end));
44673f9d6ad7SLin Ling 		return;
44683f9d6ad7SLin Ling 	} else if (ps->pss_state == DSS_CANCELED) {
44693f9d6ad7SLin Ling 		if (ps->pss_func == POOL_SCAN_SCRUB) {
44703f9d6ad7SLin Ling 			(void) printf(gettext("scrub canceled on %s"),
44713f9d6ad7SLin Ling 			    ctime(&end));
44723f9d6ad7SLin Ling 		} else if (ps->pss_func == POOL_SCAN_RESILVER) {
44733f9d6ad7SLin Ling 			(void) printf(gettext("resilver canceled on %s"),
44743f9d6ad7SLin Ling 			    ctime(&end));
44753f9d6ad7SLin Ling 		}
4476fa9e4066Sahrens 		return;
4477fa9e4066Sahrens 	}
4478fa9e4066Sahrens 
44793f9d6ad7SLin Ling 	assert(ps->pss_state == DSS_SCANNING);
44803f9d6ad7SLin Ling 
44813f9d6ad7SLin Ling 	/*
44823f9d6ad7SLin Ling 	 * Scan is in progress.
44833f9d6ad7SLin Ling 	 */
44843f9d6ad7SLin Ling 	if (ps->pss_func == POOL_SCAN_SCRUB) {
44851702cce7SAlek Pinchuk 		if (pause == 0) {
44861702cce7SAlek Pinchuk 			(void) printf(gettext("scrub in progress since %s"),
44871702cce7SAlek Pinchuk 			    ctime(&start));
44881702cce7SAlek Pinchuk 		} else {
44891702cce7SAlek Pinchuk 			char buf[32];
44901702cce7SAlek Pinchuk 			struct tm *p = localtime(&pause);
44911702cce7SAlek Pinchuk 			(void) strftime(buf, sizeof (buf), "%a %b %e %T %Y", p);
44921702cce7SAlek Pinchuk 			(void) printf(gettext("scrub paused since %s\n"), buf);
44931702cce7SAlek Pinchuk 			(void) printf(gettext("\tscrub started on   %s"),
44941702cce7SAlek Pinchuk 			    ctime(&start));
44951702cce7SAlek Pinchuk 		}
44963f9d6ad7SLin Ling 	} else if (ps->pss_func == POOL_SCAN_RESILVER) {
44973f9d6ad7SLin Ling 		(void) printf(gettext("resilver in progress since %s"),
44983f9d6ad7SLin Ling 		    ctime(&start));
44993f9d6ad7SLin Ling 	}
4500fa9e4066Sahrens 
45013f9d6ad7SLin Ling 	examined = ps->pss_examined ? ps->pss_examined : 1;
45023f9d6ad7SLin Ling 	total = ps->pss_to_examine;
4503fa9e4066Sahrens 	fraction_done = (double)examined / total;
45043f9d6ad7SLin Ling 
45053f9d6ad7SLin Ling 	/* elapsed time for this pass */
45063f9d6ad7SLin Ling 	elapsed = time(NULL) - ps->pss_pass_start;
45071702cce7SAlek Pinchuk 	elapsed -= ps->pss_pass_scrub_spent_paused;
45083f9d6ad7SLin Ling 	elapsed = elapsed ? elapsed : 1;
45093f9d6ad7SLin Ling 	pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
45103f9d6ad7SLin Ling 	rate = pass_exam / elapsed;
45113f9d6ad7SLin Ling 	rate = rate ? rate : 1;
45123f9d6ad7SLin Ling 	mins_left = ((total - examined) / rate) / 60;
451373d314ceSLin Ling 	hours_left = mins_left / 60;
45143f9d6ad7SLin Ling 
45153f9d6ad7SLin Ling 	zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
45163f9d6ad7SLin Ling 	zfs_nicenum(total, total_buf, sizeof (total_buf));
45173f9d6ad7SLin Ling 
451873d314ceSLin Ling 	/*
451973d314ceSLin Ling 	 * do not print estimated time if hours_left is more than 30 days
45201702cce7SAlek Pinchuk 	 * or we have a paused scrub
452173d314ceSLin Ling 	 */
45221702cce7SAlek Pinchuk 	if (pause == 0) {
45231702cce7SAlek Pinchuk 		zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
45241702cce7SAlek Pinchuk 		(void) printf(gettext("\t%s scanned out of %s at %s/s"),
45251702cce7SAlek Pinchuk 		    examined_buf, total_buf, rate_buf);
45261702cce7SAlek Pinchuk 		if (hours_left < (30 * 24)) {
45271702cce7SAlek Pinchuk 			(void) printf(gettext(", %lluh%um to go\n"),
45281702cce7SAlek Pinchuk 			    (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
45291702cce7SAlek Pinchuk 		} else {
45301702cce7SAlek Pinchuk 			(void) printf(gettext(
45311702cce7SAlek Pinchuk 			    ", (scan is slow, no estimated time)\n"));
45321702cce7SAlek Pinchuk 		}
453373d314ceSLin Ling 	} else {
45341702cce7SAlek Pinchuk 		(void) printf(gettext("\t%s scanned out of %s\n"),
45351702cce7SAlek Pinchuk 		    examined_buf, total_buf);
453673d314ceSLin Ling 	}
45373f9d6ad7SLin Ling 
45383f9d6ad7SLin Ling 	if (ps->pss_func == POOL_SCAN_RESILVER) {
45393f9d6ad7SLin Ling 		(void) printf(gettext("    %s resilvered, %.2f%% done\n"),
45403f9d6ad7SLin Ling 		    processed_buf, 100 * fraction_done);
45413f9d6ad7SLin Ling 	} else if (ps->pss_func == POOL_SCAN_SCRUB) {
45423f9d6ad7SLin Ling 		(void) printf(gettext("    %s repaired, %.2f%% done\n"),
45433f9d6ad7SLin Ling 		    processed_buf, 100 * fraction_done);
45443f9d6ad7SLin Ling 	}
4545fa9e4066Sahrens }
4546fa9e4066Sahrens 
454786714001SSerapheim Dimitropoulos /*
454886714001SSerapheim Dimitropoulos  * As we don't scrub checkpointed blocks, we want to warn the
454986714001SSerapheim Dimitropoulos  * user that we skipped scanning some blocks if a checkpoint exists
455086714001SSerapheim Dimitropoulos  * or existed at any time during the scan.
455186714001SSerapheim Dimitropoulos  */
455286714001SSerapheim Dimitropoulos static void
455386714001SSerapheim Dimitropoulos print_checkpoint_scan_warning(pool_scan_stat_t *ps, pool_checkpoint_stat_t *pcs)
455486714001SSerapheim Dimitropoulos {
455586714001SSerapheim Dimitropoulos 	if (ps == NULL || pcs == NULL)
455686714001SSerapheim Dimitropoulos 		return;
455786714001SSerapheim Dimitropoulos 
455886714001SSerapheim Dimitropoulos 	if (pcs->pcs_state == CS_NONE ||
455986714001SSerapheim Dimitropoulos 	    pcs->pcs_state == CS_CHECKPOINT_DISCARDING)
456086714001SSerapheim Dimitropoulos 		return;
456186714001SSerapheim Dimitropoulos 
456286714001SSerapheim Dimitropoulos 	assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS);
456386714001SSerapheim Dimitropoulos 
456486714001SSerapheim Dimitropoulos 	if (ps->pss_state == DSS_NONE)
456586714001SSerapheim Dimitropoulos 		return;
456686714001SSerapheim Dimitropoulos 
456786714001SSerapheim Dimitropoulos 	if ((ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) &&
456886714001SSerapheim Dimitropoulos 	    ps->pss_end_time < pcs->pcs_start_time)
456986714001SSerapheim Dimitropoulos 		return;
457086714001SSerapheim Dimitropoulos 
457186714001SSerapheim Dimitropoulos 	if (ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) {
457286714001SSerapheim Dimitropoulos 		(void) printf(gettext("    scan warning: skipped blocks "
457386714001SSerapheim Dimitropoulos 		    "that are only referenced by the checkpoint.\n"));
457486714001SSerapheim Dimitropoulos 	} else {
457586714001SSerapheim Dimitropoulos 		assert(ps->pss_state == DSS_SCANNING);
457686714001SSerapheim Dimitropoulos 		(void) printf(gettext("    scan warning: skipping blocks "
457786714001SSerapheim Dimitropoulos 		    "that are only referenced by the checkpoint.\n"));
457886714001SSerapheim Dimitropoulos 	}
457986714001SSerapheim Dimitropoulos }
458086714001SSerapheim Dimitropoulos 
45815cabbc6bSPrashanth Sreenivasa /*
45825cabbc6bSPrashanth Sreenivasa  * Print out detailed removal status.
45835cabbc6bSPrashanth Sreenivasa  */
45845cabbc6bSPrashanth Sreenivasa static void
45855cabbc6bSPrashanth Sreenivasa print_removal_status(zpool_handle_t *zhp, pool_removal_stat_t *prs)
45865cabbc6bSPrashanth Sreenivasa {
45875cabbc6bSPrashanth Sreenivasa 	char copied_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
45885cabbc6bSPrashanth Sreenivasa 	time_t start, end;
45895cabbc6bSPrashanth Sreenivasa 	nvlist_t *config, *nvroot;
45905cabbc6bSPrashanth Sreenivasa 	nvlist_t **child;
45915cabbc6bSPrashanth Sreenivasa 	uint_t children;
45925cabbc6bSPrashanth Sreenivasa 	char *vdev_name;
45935cabbc6bSPrashanth Sreenivasa 
45945cabbc6bSPrashanth Sreenivasa 	if (prs == NULL || prs->prs_state == DSS_NONE)
45955cabbc6bSPrashanth Sreenivasa 		return;
45965cabbc6bSPrashanth Sreenivasa 
45975cabbc6bSPrashanth Sreenivasa 	/*
45985cabbc6bSPrashanth Sreenivasa 	 * Determine name of vdev.
45995cabbc6bSPrashanth Sreenivasa 	 */
46005cabbc6bSPrashanth Sreenivasa 	config = zpool_get_config(zhp, NULL);
46015cabbc6bSPrashanth Sreenivasa 	nvroot = fnvlist_lookup_nvlist(config,
46025cabbc6bSPrashanth Sreenivasa 	    ZPOOL_CONFIG_VDEV_TREE);
46035cabbc6bSPrashanth Sreenivasa 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
46045cabbc6bSPrashanth Sreenivasa 	    &child, &children) == 0);
46055cabbc6bSPrashanth Sreenivasa 	assert(prs->prs_removing_vdev < children);
46065cabbc6bSPrashanth Sreenivasa 	vdev_name = zpool_vdev_name(g_zfs, zhp,
46075cabbc6bSPrashanth Sreenivasa 	    child[prs->prs_removing_vdev], B_TRUE);
46085cabbc6bSPrashanth Sreenivasa 
46095cabbc6bSPrashanth Sreenivasa 	(void) printf(gettext("remove: "));
46105cabbc6bSPrashanth Sreenivasa 
46115cabbc6bSPrashanth Sreenivasa 	start = prs->prs_start_time;
46125cabbc6bSPrashanth Sreenivasa 	end = prs->prs_end_time;
46135cabbc6bSPrashanth Sreenivasa 	zfs_nicenum(prs->prs_copied, copied_buf, sizeof (copied_buf));
46145cabbc6bSPrashanth Sreenivasa 
46155cabbc6bSPrashanth Sreenivasa 	/*
46165cabbc6bSPrashanth Sreenivasa 	 * Removal is finished or canceled.
46175cabbc6bSPrashanth Sreenivasa 	 */
46185cabbc6bSPrashanth Sreenivasa 	if (prs->prs_state == DSS_FINISHED) {
46195cabbc6bSPrashanth Sreenivasa 		uint64_t minutes_taken = (end - start) / 60;
46205cabbc6bSPrashanth Sreenivasa 
46215cabbc6bSPrashanth Sreenivasa 		(void) printf(gettext("Removal of vdev %llu copied %s "
46225cabbc6bSPrashanth Sreenivasa 		    "in %lluh%um, completed on %s"),
46235cabbc6bSPrashanth Sreenivasa 		    (longlong_t)prs->prs_removing_vdev,
46245cabbc6bSPrashanth Sreenivasa 		    copied_buf,
46255cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)(minutes_taken / 60),
46265cabbc6bSPrashanth Sreenivasa 		    (uint_t)(minutes_taken % 60),
46275cabbc6bSPrashanth Sreenivasa 		    ctime((time_t *)&end));
46285cabbc6bSPrashanth Sreenivasa 	} else if (prs->prs_state == DSS_CANCELED) {
46295cabbc6bSPrashanth Sreenivasa 		(void) printf(gettext("Removal of %s canceled on %s"),
46305cabbc6bSPrashanth Sreenivasa 		    vdev_name, ctime(&end));
46315cabbc6bSPrashanth Sreenivasa 	} else {
46325cabbc6bSPrashanth Sreenivasa 		uint64_t copied, total, elapsed, mins_left, hours_left;
46335cabbc6bSPrashanth Sreenivasa 		double fraction_done;
46345cabbc6bSPrashanth Sreenivasa 		uint_t rate;
46355cabbc6bSPrashanth Sreenivasa 
46365cabbc6bSPrashanth Sreenivasa 		assert(prs->prs_state == DSS_SCANNING);
46375cabbc6bSPrashanth Sreenivasa 
46385cabbc6bSPrashanth Sreenivasa 		/*
46395cabbc6bSPrashanth Sreenivasa 		 * Removal is in progress.
46405cabbc6bSPrashanth Sreenivasa 		 */
46415cabbc6bSPrashanth Sreenivasa 		(void) printf(gettext(
46425cabbc6bSPrashanth Sreenivasa 		    "Evacuation of %s in progress since %s"),
46435cabbc6bSPrashanth Sreenivasa 		    vdev_name, ctime(&start));
46445cabbc6bSPrashanth Sreenivasa 
46455cabbc6bSPrashanth Sreenivasa 		copied = prs->prs_copied > 0 ? prs->prs_copied : 1;
46465cabbc6bSPrashanth Sreenivasa 		total = prs->prs_to_copy;
46475cabbc6bSPrashanth Sreenivasa 		fraction_done = (double)copied / total;
46485cabbc6bSPrashanth Sreenivasa 
46495cabbc6bSPrashanth Sreenivasa 		/* elapsed time for this pass */
46505cabbc6bSPrashanth Sreenivasa 		elapsed = time(NULL) - prs->prs_start_time;
46515cabbc6bSPrashanth Sreenivasa 		elapsed = elapsed > 0 ? elapsed : 1;
46525cabbc6bSPrashanth Sreenivasa 		rate = copied / elapsed;
46535cabbc6bSPrashanth Sreenivasa 		rate = rate > 0 ? rate : 1;
46545cabbc6bSPrashanth Sreenivasa 		mins_left = ((total - copied) / rate) / 60;
46555cabbc6bSPrashanth Sreenivasa 		hours_left = mins_left / 60;
46565cabbc6bSPrashanth Sreenivasa 
46575cabbc6bSPrashanth Sreenivasa 		zfs_nicenum(copied, examined_buf, sizeof (examined_buf));
46585cabbc6bSPrashanth Sreenivasa 		zfs_nicenum(total, total_buf, sizeof (total_buf));
46595cabbc6bSPrashanth Sreenivasa 		zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
46605cabbc6bSPrashanth Sreenivasa 
46615cabbc6bSPrashanth Sreenivasa 		/*
46625cabbc6bSPrashanth Sreenivasa 		 * do not print estimated time if hours_left is more than
46635cabbc6bSPrashanth Sreenivasa 		 * 30 days
46645cabbc6bSPrashanth Sreenivasa 		 */
46655cabbc6bSPrashanth Sreenivasa 		(void) printf(gettext("    %s copied out of %s at %s/s, "
46665cabbc6bSPrashanth Sreenivasa 		    "%.2f%% done"),
46675cabbc6bSPrashanth Sreenivasa 		    examined_buf, total_buf, rate_buf, 100 * fraction_done);
46685cabbc6bSPrashanth Sreenivasa 		if (hours_left < (30 * 24)) {
46695cabbc6bSPrashanth Sreenivasa 			(void) printf(gettext(", %lluh%um to go\n"),
46705cabbc6bSPrashanth Sreenivasa 			    (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
46715cabbc6bSPrashanth Sreenivasa 		} else {
46725cabbc6bSPrashanth Sreenivasa 			(void) printf(gettext(
46735cabbc6bSPrashanth Sreenivasa 			    ", (copy is slow, no estimated time)\n"));
46745cabbc6bSPrashanth Sreenivasa 		}
46755cabbc6bSPrashanth Sreenivasa 	}
46765cabbc6bSPrashanth Sreenivasa 
46775cabbc6bSPrashanth Sreenivasa 	if (prs->prs_mapping_memory > 0) {
46785cabbc6bSPrashanth Sreenivasa 		char mem_buf[7];
46795cabbc6bSPrashanth Sreenivasa 		zfs_nicenum(prs->prs_mapping_memory, mem_buf, sizeof (mem_buf));
46805cabbc6bSPrashanth Sreenivasa 		(void) printf(gettext("    %s memory used for "
46815cabbc6bSPrashanth Sreenivasa 		    "removed device mappings\n"),
46825cabbc6bSPrashanth Sreenivasa 		    mem_buf);
46835cabbc6bSPrashanth Sreenivasa 	}
46845cabbc6bSPrashanth Sreenivasa }
46855cabbc6bSPrashanth Sreenivasa 
468686714001SSerapheim Dimitropoulos static void
468786714001SSerapheim Dimitropoulos print_checkpoint_status(pool_checkpoint_stat_t *pcs)
468886714001SSerapheim Dimitropoulos {
468986714001SSerapheim Dimitropoulos 	time_t start;
469086714001SSerapheim Dimitropoulos 	char space_buf[7];
469186714001SSerapheim Dimitropoulos 
469286714001SSerapheim Dimitropoulos 	if (pcs == NULL || pcs->pcs_state == CS_NONE)
469386714001SSerapheim Dimitropoulos 		return;
469486714001SSerapheim Dimitropoulos 
469586714001SSerapheim Dimitropoulos 	(void) printf(gettext("checkpoint: "));
469686714001SSerapheim Dimitropoulos 
469786714001SSerapheim Dimitropoulos 	start = pcs->pcs_start_time;
469886714001SSerapheim Dimitropoulos 	zfs_nicenum(pcs->pcs_space, space_buf, sizeof (space_buf));
469986714001SSerapheim Dimitropoulos 
470086714001SSerapheim Dimitropoulos 	if (pcs->pcs_state == CS_CHECKPOINT_EXISTS) {
470186714001SSerapheim Dimitropoulos 		char *date = ctime(&start);
470286714001SSerapheim Dimitropoulos 
470386714001SSerapheim Dimitropoulos 		/*
470486714001SSerapheim Dimitropoulos 		 * ctime() adds a newline at the end of the generated
470586714001SSerapheim Dimitropoulos 		 * string, thus the weird format specifier and the
470686714001SSerapheim Dimitropoulos 		 * strlen() call used to chop it off from the output.
470786714001SSerapheim Dimitropoulos 		 */
470886714001SSerapheim Dimitropoulos 		(void) printf(gettext("created %.*s, consumes %s\n"),
470986714001SSerapheim Dimitropoulos 		    strlen(date) - 1, date, space_buf);
471086714001SSerapheim Dimitropoulos 		return;
471186714001SSerapheim Dimitropoulos 	}
471286714001SSerapheim Dimitropoulos 
471386714001SSerapheim Dimitropoulos 	assert(pcs->pcs_state == CS_CHECKPOINT_DISCARDING);
471486714001SSerapheim Dimitropoulos 
471586714001SSerapheim Dimitropoulos 	(void) printf(gettext("discarding, %s remaining.\n"),
471686714001SSerapheim Dimitropoulos 	    space_buf);
471786714001SSerapheim Dimitropoulos }
471886714001SSerapheim Dimitropoulos 
4719ea8dc4b6Seschrock static void
4720ea8dc4b6Seschrock print_error_log(zpool_handle_t *zhp)
4721ea8dc4b6Seschrock {
472275519f38Sek 	nvlist_t *nverrlist = NULL;
472355434c77Sek 	nvpair_t *elem;
472455434c77Sek 	char *pathname;
472555434c77Sek 	size_t len = MAXPATHLEN * 2;
4726ea8dc4b6Seschrock 
472755434c77Sek 	if (zpool_get_errlog(zhp, &nverrlist) != 0) {
4728ea8dc4b6Seschrock 		(void) printf("errors: List of errors unavailable "
4729ea8dc4b6Seschrock 		    "(insufficient privileges)\n");
4730ea8dc4b6Seschrock 		return;
4731ea8dc4b6Seschrock 	}
4732ea8dc4b6Seschrock 
473355434c77Sek 	(void) printf("errors: Permanent errors have been "
473455434c77Sek 	    "detected in the following files:\n\n");
4735ea8dc4b6Seschrock 
473655434c77Sek 	pathname = safe_malloc(len);
473755434c77Sek 	elem = NULL;
473855434c77Sek 	while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
473955434c77Sek 		nvlist_t *nv;
474055434c77Sek 		uint64_t dsobj, obj;
474155434c77Sek 
474255434c77Sek 		verify(nvpair_value_nvlist(elem, &nv) == 0);
474355434c77Sek 		verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
474455434c77Sek 		    &dsobj) == 0);
474555434c77Sek 		verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
474655434c77Sek 		    &obj) == 0);
474755434c77Sek 		zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
474855434c77Sek 		(void) printf("%7s %s\n", "", pathname);
474955434c77Sek 	}
475055434c77Sek 	free(pathname);
475155434c77Sek 	nvlist_free(nverrlist);
4752ea8dc4b6Seschrock }
4753ea8dc4b6Seschrock 
475499653d4eSeschrock static void
475599653d4eSeschrock print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
475699653d4eSeschrock     int namewidth)
475799653d4eSeschrock {
475899653d4eSeschrock 	uint_t i;
475999653d4eSeschrock 	char *name;
476099653d4eSeschrock 
476199653d4eSeschrock 	if (nspares == 0)
476299653d4eSeschrock 		return;
476399653d4eSeschrock 
476499653d4eSeschrock 	(void) printf(gettext("\tspares\n"));
476599653d4eSeschrock 
476699653d4eSeschrock 	for (i = 0; i < nspares; i++) {
476788ecc943SGeorge Wilson 		name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
476899653d4eSeschrock 		print_status_config(zhp, name, spares[i],
4769aa8cf21aSNeil Perrin 		    namewidth, 2, B_TRUE);
477099653d4eSeschrock 		free(name);
477199653d4eSeschrock 	}
477299653d4eSeschrock }
477399653d4eSeschrock 
4774fa94a07fSbrendan static void
4775fa94a07fSbrendan print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
4776fa94a07fSbrendan     int namewidth)
4777fa94a07fSbrendan {
4778fa94a07fSbrendan 	uint_t i;
4779fa94a07fSbrendan 	char *name;
4780fa94a07fSbrendan 
4781fa94a07fSbrendan 	if (nl2cache == 0)
4782fa94a07fSbrendan 		return;
4783fa94a07fSbrendan 
4784fa94a07fSbrendan 	(void) printf(gettext("\tcache\n"));
4785fa94a07fSbrendan 
4786fa94a07fSbrendan 	for (i = 0; i < nl2cache; i++) {
478788ecc943SGeorge Wilson 		name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
4788fa94a07fSbrendan 		print_status_config(zhp, name, l2cache[i],
4789aa8cf21aSNeil Perrin 		    namewidth, 2, B_FALSE);
4790aa8cf21aSNeil Perrin 		free(name);
4791aa8cf21aSNeil Perrin 	}
4792aa8cf21aSNeil Perrin }
4793aa8cf21aSNeil Perrin 
47949eb19f4dSGeorge Wilson static void
47959eb19f4dSGeorge Wilson print_dedup_stats(nvlist_t *config)
47969eb19f4dSGeorge Wilson {
47979eb19f4dSGeorge Wilson 	ddt_histogram_t *ddh;
47989eb19f4dSGeorge Wilson 	ddt_stat_t *dds;
47999eb19f4dSGeorge Wilson 	ddt_object_t *ddo;
48009eb19f4dSGeorge Wilson 	uint_t c;
48019eb19f4dSGeorge Wilson 
48029eb19f4dSGeorge Wilson 	/*
48039eb19f4dSGeorge Wilson 	 * If the pool was faulted then we may not have been able to
48042384d9f8SGeorge Wilson 	 * obtain the config. Otherwise, if we have anything in the dedup
48059eb19f4dSGeorge Wilson 	 * table continue processing the stats.
48069eb19f4dSGeorge Wilson 	 */
48079eb19f4dSGeorge Wilson 	if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
4808ce72e614SYuri Pankov 	    (uint64_t **)&ddo, &c) != 0)
48099eb19f4dSGeorge Wilson 		return;
48109eb19f4dSGeorge Wilson 
48119eb19f4dSGeorge Wilson 	(void) printf("\n");
4812ce72e614SYuri Pankov 	(void) printf(gettext(" dedup: "));
4813ce72e614SYuri Pankov 	if (ddo->ddo_count == 0) {
4814ce72e614SYuri Pankov 		(void) printf(gettext("no DDT entries\n"));
4815ce72e614SYuri Pankov 		return;
4816ce72e614SYuri Pankov 	}
4817ce72e614SYuri Pankov 
48189eb19f4dSGeorge Wilson 	(void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
48199eb19f4dSGeorge Wilson 	    (u_longlong_t)ddo->ddo_count,
48209eb19f4dSGeorge Wilson 	    (u_longlong_t)ddo->ddo_dspace,
48219eb19f4dSGeorge Wilson 	    (u_longlong_t)ddo->ddo_mspace);
48229eb19f4dSGeorge Wilson 
48239eb19f4dSGeorge Wilson 	verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
48249eb19f4dSGeorge Wilson 	    (uint64_t **)&dds, &c) == 0);
48259eb19f4dSGeorge Wilson 	verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
48269eb19f4dSGeorge Wilson 	    (uint64_t **)&ddh, &c) == 0);
48279eb19f4dSGeorge Wilson 	zpool_dump_ddt(dds, ddh);
48289eb19f4dSGeorge Wilson }
48299eb19f4dSGeorge Wilson 
4830fa9e4066Sahrens /*
4831fa9e4066Sahrens  * Display a summary of pool status.  Displays a summary such as:
4832fa9e4066Sahrens  *
4833fa9e4066Sahrens  *        pool: tank
4834fa9e4066Sahrens  *	status: DEGRADED
4835fa9e4066Sahrens  *	reason: One or more devices ...
4836654b400cSJoshua M. Clulow  *         see: http://illumos.org/msg/ZFS-xxxx-01
4837fa9e4066Sahrens  *	config:
4838fa9e4066Sahrens  *		mirror		DEGRADED
4839fa9e4066Sahrens  *                c1t0d0	OK
4840ea8dc4b6Seschrock  *                c2t0d0	UNAVAIL
4841fa9e4066Sahrens  *
4842fa9e4066Sahrens  * When given the '-v' option, we print out the complete config.  If the '-e'
4843fa9e4066Sahrens  * option is specified, then we print out error rate information as well.
4844fa9e4066Sahrens  */
4845fa9e4066Sahrens int
4846fa9e4066Sahrens status_callback(zpool_handle_t *zhp, void *data)
4847fa9e4066Sahrens {
4848fa9e4066Sahrens 	status_cbdata_t *cbp = data;
4849fa9e4066Sahrens 	nvlist_t *config, *nvroot;
4850fa9e4066Sahrens 	char *msgid;
4851fa9e4066Sahrens 	int reason;
485246657f8dSmmusante 	const char *health;
485346657f8dSmmusante 	uint_t c;
485446657f8dSmmusante 	vdev_stat_t *vs;
4855fa9e4066Sahrens 
4856088e9d47Seschrock 	config = zpool_get_config(zhp, NULL);
4857fa9e4066Sahrens 	reason = zpool_get_status(zhp, &msgid);
4858fa9e4066Sahrens 
4859fa9e4066Sahrens 	cbp->cb_count++;
4860fa9e4066Sahrens 
4861fa9e4066Sahrens 	/*
4862fa9e4066Sahrens 	 * If we were given 'zpool status -x', only report those pools with
4863fa9e4066Sahrens 	 * problems.
4864fa9e4066Sahrens 	 */
4865b3a6f804STim Connors 	if (cbp->cb_explain &&
4866b3a6f804STim Connors 	    (reason == ZPOOL_STATUS_OK ||
4867b3a6f804STim Connors 	    reason == ZPOOL_STATUS_VERSION_OLDER ||
4868b3a6f804STim Connors 	    reason == ZPOOL_STATUS_FEAT_DISABLED)) {
4869e9dbad6fSeschrock 		if (!cbp->cb_allpools) {
4870e9dbad6fSeschrock 			(void) printf(gettext("pool '%s' is healthy\n"),
4871e9dbad6fSeschrock 			    zpool_get_name(zhp));
4872e9dbad6fSeschrock 			if (cbp->cb_first)
4873e9dbad6fSeschrock 				cbp->cb_first = B_FALSE;
4874e9dbad6fSeschrock 		}
4875fa9e4066Sahrens 		return (0);
4876e9dbad6fSeschrock 	}
4877fa9e4066Sahrens 
4878fa9e4066Sahrens 	if (cbp->cb_first)
487999653d4eSeschrock 		cbp->cb_first = B_FALSE;
4880fa9e4066Sahrens 	else
4881fa9e4066Sahrens 		(void) printf("\n");
4882fa9e4066Sahrens 
48835cabbc6bSPrashanth Sreenivasa 	nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
48843f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
488546657f8dSmmusante 	    (uint64_t **)&vs, &c) == 0);
4886990b4856Slling 	health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4887fa9e4066Sahrens 
4888fa9e4066Sahrens 	(void) printf(gettext("  pool: %s\n"), zpool_get_name(zhp));
4889fa9e4066Sahrens 	(void) printf(gettext(" state: %s\n"), health);
4890fa9e4066Sahrens 
4891fa9e4066Sahrens 	switch (reason) {
4892fa9e4066Sahrens 	case ZPOOL_STATUS_MISSING_DEV_R:
4893fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices could not "
4894fa9e4066Sahrens 		    "be opened.  Sufficient replicas exist for\n\tthe pool to "
4895fa9e4066Sahrens 		    "continue functioning in a degraded state.\n"));
4896fa9e4066Sahrens 		(void) printf(gettext("action: Attach the missing device and "
4897fa9e4066Sahrens 		    "online it using 'zpool online'.\n"));
4898fa9e4066Sahrens 		break;
4899fa9e4066Sahrens 
4900fa9e4066Sahrens 	case ZPOOL_STATUS_MISSING_DEV_NR:
4901fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices could not "
4902fa9e4066Sahrens 		    "be opened.  There are insufficient\n\treplicas for the "
4903fa9e4066Sahrens 		    "pool to continue functioning.\n"));
4904fa9e4066Sahrens 		(void) printf(gettext("action: Attach the missing device and "
4905fa9e4066Sahrens 		    "online it using 'zpool online'.\n"));
4906fa9e4066Sahrens 		break;
4907fa9e4066Sahrens 
4908fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_LABEL_R:
4909fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices could not "
4910fa9e4066Sahrens 		    "be used because the label is missing or\n\tinvalid.  "
4911fa9e4066Sahrens 		    "Sufficient replicas exist for the pool to continue\n\t"
4912fa9e4066Sahrens 		    "functioning in a degraded state.\n"));
4913fa9e4066Sahrens 		(void) printf(gettext("action: Replace the device using "
4914fa9e4066Sahrens 		    "'zpool replace'.\n"));
4915fa9e4066Sahrens 		break;
4916fa9e4066Sahrens 
4917fa9e4066Sahrens 	case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4918fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices could not "
4919b1b8ab34Slling 		    "be used because the label is missing \n\tor invalid.  "
4920fa9e4066Sahrens 		    "There are insufficient replicas for the pool to "
4921fa9e4066Sahrens 		    "continue\n\tfunctioning.\n"));
4922468c413aSTim Haley 		zpool_explain_recover(zpool_get_handle(zhp),
4923468c413aSTim Haley 		    zpool_get_name(zhp), reason, config);
4924fa9e4066Sahrens 		break;
4925fa9e4066Sahrens 
4926fa9e4066Sahrens 	case ZPOOL_STATUS_FAILING_DEV:
4927fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices has "
4928fa9e4066Sahrens 		    "experienced an unrecoverable error.  An\n\tattempt was "
4929fa9e4066Sahrens 		    "made to correct the error.  Applications are "
4930fa9e4066Sahrens 		    "unaffected.\n"));
4931fa9e4066Sahrens 		(void) printf(gettext("action: Determine if the device needs "
4932fa9e4066Sahrens 		    "to be replaced, and clear the errors\n\tusing "
4933ea8dc4b6Seschrock 		    "'zpool clear' or replace the device with 'zpool "
4934fa9e4066Sahrens 		    "replace'.\n"));
4935fa9e4066Sahrens 		break;
4936fa9e4066Sahrens 
4937fa9e4066Sahrens 	case ZPOOL_STATUS_OFFLINE_DEV:
4938fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices has "
4939d7d4af51Smmusante 		    "been taken offline by the administrator.\n\tSufficient "
4940fa9e4066Sahrens 		    "replicas exist for the pool to continue functioning in "
4941fa9e4066Sahrens 		    "a\n\tdegraded state.\n"));
4942fa9e4066Sahrens 		(void) printf(gettext("action: Online the device using "
4943fa9e4066Sahrens 		    "'zpool online' or replace the device with\n\t'zpool "
4944fa9e4066Sahrens 		    "replace'.\n"));
4945fa9e4066Sahrens 		break;
4946fa9e4066Sahrens 
4947c25309d4SGeorge Wilson 	case ZPOOL_STATUS_REMOVED_DEV:
4948c25309d4SGeorge Wilson 		(void) printf(gettext("status: One or more devices has "
4949c25309d4SGeorge Wilson 		    "been removed by the administrator.\n\tSufficient "
4950c25309d4SGeorge Wilson 		    "replicas exist for the pool to continue functioning in "
4951c25309d4SGeorge Wilson 		    "a\n\tdegraded state.\n"));
4952c25309d4SGeorge Wilson 		(void) printf(gettext("action: Online the device using "
4953c25309d4SGeorge Wilson 		    "'zpool online' or replace the device with\n\t'zpool "
4954c25309d4SGeorge Wilson 		    "replace'.\n"));
4955c25309d4SGeorge Wilson 		break;
4956c25309d4SGeorge Wilson 
4957fa9e4066Sahrens 	case ZPOOL_STATUS_RESILVERING:
4958fa9e4066Sahrens 		(void) printf(gettext("status: One or more devices is "
4959fa9e4066Sahrens 		    "currently being resilvered.  The pool will\n\tcontinue "
4960fa9e4066Sahrens 		    "to function, possibly in a degraded state.\n"));
4961fa9e4066Sahrens 		(void) printf(gettext("action: Wait for the resilver to "
4962fa9e4066Sahrens 		    "complete.\n"));
4963fa9e4066Sahrens 		break;
4964fa9e4066Sahrens 
4965ea8dc4b6Seschrock 	case ZPOOL_STATUS_CORRUPT_DATA:
4966ea8dc4b6Seschrock 		(void) printf(gettext("status: One or more devices has "
4967ea8dc4b6Seschrock 		    "experienced an error resulting in data\n\tcorruption.  "
4968ea8dc4b6Seschrock 		    "Applications may be affected.\n"));
4969ea8dc4b6Seschrock 		(void) printf(gettext("action: Restore the file in question "
4970ea8dc4b6Seschrock 		    "if possible.  Otherwise restore the\n\tentire pool from "
4971ea8dc4b6Seschrock 		    "backup.\n"));
4972ea8dc4b6Seschrock 		break;
4973ea8dc4b6Seschrock 
4974ea8dc4b6Seschrock 	case ZPOOL_STATUS_CORRUPT_POOL:
4975ea8dc4b6Seschrock 		(void) printf(gettext("status: The pool metadata is corrupted "
4976ea8dc4b6Seschrock 		    "and the pool cannot be opened.\n"));
4977468c413aSTim Haley 		zpool_explain_recover(zpool_get_handle(zhp),
4978468c413aSTim Haley 		    zpool_get_name(zhp), reason, config);
4979ea8dc4b6Seschrock 		break;
4980ea8dc4b6Seschrock 
4981eaca9bbdSeschrock 	case ZPOOL_STATUS_VERSION_OLDER:
498257221772SChristopher Siden 		(void) printf(gettext("status: The pool is formatted using a "
498357221772SChristopher Siden 		    "legacy on-disk format.  The pool can\n\tstill be used, "
498457221772SChristopher Siden 		    "but some features are unavailable.\n"));
4985eaca9bbdSeschrock 		(void) printf(gettext("action: Upgrade the pool using 'zpool "
4986eaca9bbdSeschrock 		    "upgrade'.  Once this is done, the\n\tpool will no longer "
498757221772SChristopher Siden 		    "be accessible on software that does not support feature\n"
498857221772SChristopher Siden 		    "\tflags.\n"));
4989eaca9bbdSeschrock 		break;
4990eaca9bbdSeschrock 
4991eaca9bbdSeschrock 	case ZPOOL_STATUS_VERSION_NEWER:
4992eaca9bbdSeschrock 		(void) printf(gettext("status: The pool has been upgraded to a "
4993eaca9bbdSeschrock 		    "newer, incompatible on-disk version.\n\tThe pool cannot "
4994eaca9bbdSeschrock 		    "be accessed on this system.\n"));
4995eaca9bbdSeschrock 		(void) printf(gettext("action: Access the pool from a system "
4996eaca9bbdSeschrock 		    "running more recent software, or\n\trestore the pool from "
4997eaca9bbdSeschrock 		    "backup.\n"));
4998eaca9bbdSeschrock 		break;
4999eaca9bbdSeschrock 
500057221772SChristopher Siden 	case ZPOOL_STATUS_FEAT_DISABLED:
500157221772SChristopher Siden 		(void) printf(gettext("status: Some supported features are not "
500257221772SChristopher Siden 		    "enabled on the pool. The pool can\n\tstill be used, but "
500357221772SChristopher Siden 		    "some features are unavailable.\n"));
500457221772SChristopher Siden 		(void) printf(gettext("action: Enable all features using "
500557221772SChristopher Siden 		    "'zpool upgrade'. Once this is done,\n\tthe pool may no "
500657221772SChristopher Siden 		    "longer be accessible by software that does not support\n\t"
500757221772SChristopher Siden 		    "the features. See zpool-features(5) for details.\n"));
500857221772SChristopher Siden 		break;
500957221772SChristopher Siden 
5010ad135b5dSChristopher Siden 	case ZPOOL_STATUS_UNSUP_FEAT_READ:
5011ad135b5dSChristopher Siden 		(void) printf(gettext("status: The pool cannot be accessed on "
5012ad135b5dSChristopher Siden 		    "this system because it uses the\n\tfollowing feature(s) "
5013ad135b5dSChristopher Siden 		    "not supported on this system:\n"));
5014ad135b5dSChristopher Siden 		zpool_print_unsup_feat(config);
5015ad135b5dSChristopher Siden 		(void) printf("\n");
5016ad135b5dSChristopher Siden 		(void) printf(gettext("action: Access the pool from a system "
5017ad135b5dSChristopher Siden 		    "that supports the required feature(s),\n\tor restore the "
5018ad135b5dSChristopher Siden 		    "pool from backup.\n"));
5019ad135b5dSChristopher Siden 		break;
5020ad135b5dSChristopher Siden 
5021ad135b5dSChristopher Siden 	case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
5022ad135b5dSChristopher Siden 		(void) printf(gettext("status: The pool can only be accessed "
5023ad135b5dSChristopher Siden 		    "in read-only mode on this system. It\n\tcannot be "
5024ad135b5dSChristopher Siden 		    "accessed in read-write mode because it uses the "
5025ad135b5dSChristopher Siden 		    "following\n\tfeature(s) not supported on this system:\n"));
5026ad135b5dSChristopher Siden 		zpool_print_unsup_feat(config);
5027ad135b5dSChristopher Siden 		(void) printf("\n");
5028ad135b5dSChristopher Siden 		(void) printf(gettext("action: The pool cannot be accessed in "
5029ad135b5dSChristopher Siden 		    "read-write mode. Import the pool with\n"
5030ad135b5dSChristopher Siden 		    "\t\"-o readonly=on\", access the pool from a system that "
5031ad135b5dSChristopher Siden 		    "supports the\n\trequired feature(s), or restore the "
5032ad135b5dSChristopher Siden 		    "pool from backup.\n"));
5033ad135b5dSChristopher Siden 		break;
5034ad135b5dSChristopher Siden 
50353d7072f8Seschrock 	case ZPOOL_STATUS_FAULTED_DEV_R:
50363d7072f8Seschrock 		(void) printf(gettext("status: One or more devices are "
50373d7072f8Seschrock 		    "faulted in response to persistent errors.\n\tSufficient "
50383d7072f8Seschrock 		    "replicas exist for the pool to continue functioning "
50393d7072f8Seschrock 		    "in a\n\tdegraded state.\n"));
50403d7072f8Seschrock 		(void) printf(gettext("action: Replace the faulted device, "
50413d7072f8Seschrock 		    "or use 'zpool clear' to mark the device\n\trepaired.\n"));
50423d7072f8Seschrock 		break;
50433d7072f8Seschrock 
50443d7072f8Seschrock 	case ZPOOL_STATUS_FAULTED_DEV_NR:
50453d7072f8Seschrock 		(void) printf(gettext("status: One or more devices are "
50463d7072f8Seschrock 		    "faulted in response to persistent errors.  There are "
50473d7072f8Seschrock 		    "insufficient replicas for the pool to\n\tcontinue "
50483d7072f8Seschrock 		    "functioning.\n"));
50493d7072f8Seschrock 		(void) printf(gettext("action: Destroy and re-create the pool "
50503d7072f8Seschrock 		    "from a backup source.  Manually marking the device\n"
50513d7072f8Seschrock 		    "\trepaired using 'zpool clear' may allow some data "
50523d7072f8Seschrock 		    "to be recovered.\n"));
50533d7072f8Seschrock 		break;
50543d7072f8Seschrock 
505532b87932Sek 	case ZPOOL_STATUS_IO_FAILURE_WAIT:
505632b87932Sek 	case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
505732b87932Sek 		(void) printf(gettext("status: One or more devices are "
50588a79c1b5Sek 		    "faulted in response to IO failures.\n"));
505932b87932Sek 		(void) printf(gettext("action: Make sure the affected devices "
506032b87932Sek 		    "are connected, then run 'zpool clear'.\n"));
506132b87932Sek 		break;
506232b87932Sek 
5063b87f3af3Sperrin 	case ZPOOL_STATUS_BAD_LOG:
5064b87f3af3Sperrin 		(void) printf(gettext("status: An intent log record "
5065b87f3af3Sperrin 		    "could not be read.\n"
5066b87f3af3Sperrin 		    "\tWaiting for adminstrator intervention to fix the "
5067b87f3af3Sperrin 		    "faulted pool.\n"));
5068b87f3af3Sperrin 		(void) printf(gettext("action: Either restore the affected "
5069b87f3af3Sperrin 		    "device(s) and run 'zpool online',\n"
5070b87f3af3Sperrin 		    "\tor ignore the intent log records by running "
5071b87f3af3Sperrin 		    "'zpool clear'.\n"));
5072b87f3af3Sperrin 		break;
5073b87f3af3Sperrin 
5074fa9e4066Sahrens 	default:
5075fa9e4066Sahrens 		/*
5076fa9e4066Sahrens 		 * The remaining errors can't actually be generated, yet.
5077fa9e4066Sahrens 		 */
5078fa9e4066Sahrens 		assert(reason == ZPOOL_STATUS_OK);
5079fa9e4066Sahrens 	}
5080fa9e4066Sahrens 
5081fa9e4066Sahrens 	if (msgid != NULL)
5082654b400cSJoshua M. Clulow 		(void) printf(gettext("   see: http://illumos.org/msg/%s\n"),
5083fa9e4066Sahrens 		    msgid);
5084fa9e4066Sahrens 
5085fa9e4066Sahrens 	if (config != NULL) {
5086fa9e4066Sahrens 		int namewidth;
5087ea8dc4b6Seschrock 		uint64_t nerr;
5088fa94a07fSbrendan 		nvlist_t **spares, **l2cache;
5089fa94a07fSbrendan 		uint_t nspares, nl2cache;
509086714001SSerapheim Dimitropoulos 		pool_checkpoint_stat_t *pcs = NULL;
50913f9d6ad7SLin Ling 		pool_scan_stat_t *ps = NULL;
50925cabbc6bSPrashanth Sreenivasa 		pool_removal_stat_t *prs = NULL;
5093fa9e4066Sahrens 
509486714001SSerapheim Dimitropoulos 		(void) nvlist_lookup_uint64_array(nvroot,
509586714001SSerapheim Dimitropoulos 		    ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
50963f9d6ad7SLin Ling 		(void) nvlist_lookup_uint64_array(nvroot,
50973f9d6ad7SLin Ling 		    ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
50985cabbc6bSPrashanth Sreenivasa 		(void) nvlist_lookup_uint64_array(nvroot,
50995cabbc6bSPrashanth Sreenivasa 		    ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c);
510086714001SSerapheim Dimitropoulos 
510186714001SSerapheim Dimitropoulos 		print_scan_status(ps);
510286714001SSerapheim Dimitropoulos 		print_checkpoint_scan_warning(ps, pcs);
51035cabbc6bSPrashanth Sreenivasa 		print_removal_status(zhp, prs);
510486714001SSerapheim Dimitropoulos 		print_checkpoint_status(pcs);
51055cabbc6bSPrashanth Sreenivasa 
5106c67d9675Seschrock 		namewidth = max_width(zhp, nvroot, 0, 0);
5107fa9e4066Sahrens 		if (namewidth < 10)
5108fa9e4066Sahrens 			namewidth = 10;
5109fa9e4066Sahrens 
5110fa9e4066Sahrens 		(void) printf(gettext("config:\n\n"));
5111fa9e4066Sahrens 		(void) printf(gettext("\t%-*s  %-8s %5s %5s %5s\n"), namewidth,
5112fa9e4066Sahrens 		    "NAME", "STATE", "READ", "WRITE", "CKSUM");
5113c67d9675Seschrock 		print_status_config(zhp, zpool_get_name(zhp), nvroot,
5114aa8cf21aSNeil Perrin 		    namewidth, 0, B_FALSE);
511599653d4eSeschrock 
51164dea40f0SNeil Perrin 		if (num_logs(nvroot) > 0)
5117e6ca193dSGeorge Wilson 			print_logs(zhp, nvroot, namewidth, B_TRUE);
5118fa94a07fSbrendan 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
5119fa94a07fSbrendan 		    &l2cache, &nl2cache) == 0)
5120fa94a07fSbrendan 			print_l2cache(zhp, l2cache, nl2cache, namewidth);
5121fa94a07fSbrendan 
512299653d4eSeschrock 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
512399653d4eSeschrock 		    &spares, &nspares) == 0)
512499653d4eSeschrock 			print_spares(zhp, spares, nspares, namewidth);
5125ea8dc4b6Seschrock 
5126ea8dc4b6Seschrock 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
5127ea8dc4b6Seschrock 		    &nerr) == 0) {
512855434c77Sek 			nvlist_t *nverrlist = NULL;
512955434c77Sek 
5130ea8dc4b6Seschrock 			/*
5131ea8dc4b6Seschrock 			 * If the approximate error count is small, get a
5132ea8dc4b6Seschrock 			 * precise count by fetching the entire log and
5133ea8dc4b6Seschrock 			 * uniquifying the results.
5134ea8dc4b6Seschrock 			 */
513575519f38Sek 			if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
513655434c77Sek 			    zpool_get_errlog(zhp, &nverrlist) == 0) {
513755434c77Sek 				nvpair_t *elem;
513855434c77Sek 
513955434c77Sek 				elem = NULL;
514055434c77Sek 				nerr = 0;
514155434c77Sek 				while ((elem = nvlist_next_nvpair(nverrlist,
514255434c77Sek 				    elem)) != NULL) {
514355434c77Sek 					nerr++;
514455434c77Sek 				}
514555434c77Sek 			}
514655434c77Sek 			nvlist_free(nverrlist);
5147ea8dc4b6Seschrock 
5148ea8dc4b6Seschrock 			(void) printf("\n");
514999653d4eSeschrock 
5150ea8dc4b6Seschrock 			if (nerr == 0)
5151ea8dc4b6Seschrock 				(void) printf(gettext("errors: No known data "
5152ea8dc4b6Seschrock 				    "errors\n"));
5153ea8dc4b6Seschrock 			else if (!cbp->cb_verbose)
5154e9dbad6fSeschrock 				(void) printf(gettext("errors: %llu data "
51555ad82045Snd 				    "errors, use '-v' for a list\n"),
51565ad82045Snd 				    (u_longlong_t)nerr);
5157ea8dc4b6Seschrock 			else
5158ea8dc4b6Seschrock 				print_error_log(zhp);
5159ea8dc4b6Seschrock 		}
51609eb19f4dSGeorge Wilson 
51619eb19f4dSGeorge Wilson 		if (cbp->cb_dedup_stats)
51629eb19f4dSGeorge Wilson 			print_dedup_stats(config);
5163fa9e4066Sahrens 	} else {
5164fa9e4066Sahrens 		(void) printf(gettext("config: The configuration cannot be "
5165fa9e4066Sahrens 		    "determined.\n"));
5166fa9e4066Sahrens 	}
5167fa9e4066Sahrens 
5168fa9e4066Sahrens 	return (0);
5169fa9e4066Sahrens }
5170fa9e4066Sahrens 
5171fa9e4066Sahrens /*
51723f9d6ad7SLin Ling  * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
5173fa9e4066Sahrens  *
5174fa9e4066Sahrens  *	-v	Display complete error logs
5175fa9e4066Sahrens  *	-x	Display only pools with potential problems
51769eb19f4dSGeorge Wilson  *	-D	Display dedup status (undocumented)
51773f9d6ad7SLin Ling  *	-T	Display a timestamp in date(1) or Unix format
5178fa9e4066Sahrens  *
5179fa9e4066Sahrens  * Describes the health status of all pools or some subset.
5180fa9e4066Sahrens  */
5181fa9e4066Sahrens int
5182fa9e4066Sahrens zpool_do_status(int argc, char **argv)
5183fa9e4066Sahrens {
5184fa9e4066Sahrens 	int c;
5185fa9e4066Sahrens 	int ret;
51863f9d6ad7SLin Ling 	unsigned long interval = 0, count = 0;
5187fa9e4066Sahrens 	status_cbdata_t cb = { 0 };
5188fa9e4066Sahrens 
5189fa9e4066Sahrens 	/* check options */
51903f9d6ad7SLin Ling 	while ((c = getopt(argc, argv, "vxDT:")) != -1) {
5191fa9e4066Sahrens 		switch (c) {
5192fa9e4066Sahrens 		case 'v':
519399653d4eSeschrock 			cb.cb_verbose = B_TRUE;
5194fa9e4066Sahrens 			break;
5195fa9e4066Sahrens 		case 'x':
519699653d4eSeschrock 			cb.cb_explain = B_TRUE;
5197fa9e4066Sahrens 			break;
51989eb19f4dSGeorge Wilson 		case 'D':
51999eb19f4dSGeorge Wilson 			cb.cb_dedup_stats = B_TRUE;
52009eb19f4dSGeorge Wilson 			break;
52013f9d6ad7SLin Ling 		case 'T':
52023f9d6ad7SLin Ling 			get_timestamp_arg(*optarg);
52033f9d6ad7SLin Ling 			break;
5204fa9e4066Sahrens 		case '?':
5205fa9e4066Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
5206fa9e4066Sahrens 			    optopt);
520799653d4eSeschrock 			usage(B_FALSE);
5208fa9e4066Sahrens 		}
5209fa9e4066Sahrens 	}
5210fa9e4066Sahrens 
5211fa9e4066Sahrens 	argc -= optind;
5212fa9e4066Sahrens 	argv += optind;
5213fa9e4066Sahrens 
52143f9d6ad7SLin Ling 	get_interval_count(&argc, argv, &interval, &count);
5215fa9e4066Sahrens 
5216e9dbad6fSeschrock 	if (argc == 0)
5217e9dbad6fSeschrock 		cb.cb_allpools = B_TRUE;
5218e9dbad6fSeschrock 
52193f9d6ad7SLin Ling 	cb.cb_first = B_TRUE;
5220fa9e4066Sahrens 
52213f9d6ad7SLin Ling 	for (;;) {
52223f9d6ad7SLin Ling 		if (timestamp_fmt != NODATE)
52233f9d6ad7SLin Ling 			print_timestamp(timestamp_fmt);
5224fa9e4066Sahrens 
52253f9d6ad7SLin Ling 		ret = for_each_pool(argc, argv, B_TRUE, NULL,
52263f9d6ad7SLin Ling 		    status_callback, &cb);
52273f9d6ad7SLin Ling 
52283f9d6ad7SLin Ling 		if (argc == 0 && cb.cb_count == 0)
52293f9d6ad7SLin Ling 			(void) printf(gettext("no pools available\n"));
52303f9d6ad7SLin Ling 		else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
52313f9d6ad7SLin Ling 			(void) printf(gettext("all pools are healthy\n"));
52323f9d6ad7SLin Ling 
52333f9d6ad7SLin Ling 		if (ret != 0)
52343f9d6ad7SLin Ling 			return (ret);
52353f9d6ad7SLin Ling 
52363f9d6ad7SLin Ling 		if (interval == 0)
52373f9d6ad7SLin Ling 			break;
52383f9d6ad7SLin Ling 
52393f9d6ad7SLin Ling 		if (count != 0 && --count == 0)
52403f9d6ad7SLin Ling 			break;
52413f9d6ad7SLin Ling 
52423f9d6ad7SLin Ling 		(void) sleep(interval);
52433f9d6ad7SLin Ling 	}
52443f9d6ad7SLin Ling 
52453f9d6ad7SLin Ling 	return (0);
5246fa9e4066Sahrens }
5247fa9e4066Sahrens 
5248eaca9bbdSeschrock typedef struct upgrade_cbdata {
5249eaca9bbdSeschrock 	int	cb_first;
525006eeb2adSek 	int	cb_argc;
5251990b4856Slling 	uint64_t cb_version;
525206eeb2adSek 	char	**cb_argv;
5253eaca9bbdSeschrock } upgrade_cbdata_t;
5254eaca9bbdSeschrock 
525557221772SChristopher Siden static int
525657221772SChristopher Siden upgrade_version(zpool_handle_t *zhp, uint64_t version)
525757221772SChristopher Siden {
525857221772SChristopher Siden 	int ret;
525957221772SChristopher Siden 	nvlist_t *config;
526057221772SChristopher Siden 	uint64_t oldversion;
526157221772SChristopher Siden 
526257221772SChristopher Siden 	config = zpool_get_config(zhp, NULL);
526357221772SChristopher Siden 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
526457221772SChristopher Siden 	    &oldversion) == 0);
526557221772SChristopher Siden 
526657221772SChristopher Siden 	assert(SPA_VERSION_IS_SUPPORTED(oldversion));
526757221772SChristopher Siden 	assert(oldversion < version);
526857221772SChristopher Siden 
526957221772SChristopher Siden 	ret = zpool_upgrade(zhp, version);
527057221772SChristopher Siden 	if (ret != 0)
527157221772SChristopher Siden 		return (ret);
527257221772SChristopher Siden 
527357221772SChristopher Siden 	if (version >= SPA_VERSION_FEATURES) {
527457221772SChristopher Siden 		(void) printf(gettext("Successfully upgraded "
527557221772SChristopher Siden 		    "'%s' from version %llu to feature flags.\n"),
527657221772SChristopher Siden 		    zpool_get_name(zhp), oldversion);
527757221772SChristopher Siden 	} else {
527857221772SChristopher Siden 		(void) printf(gettext("Successfully upgraded "
527957221772SChristopher Siden 		    "'%s' from version %llu to version %llu.\n"),
528057221772SChristopher Siden 		    zpool_get_name(zhp), oldversion, version);
528157221772SChristopher Siden 	}
528257221772SChristopher Siden 
528357221772SChristopher Siden 	return (0);
528457221772SChristopher Siden }
528557221772SChristopher Siden 
528657221772SChristopher Siden static int
528757221772SChristopher Siden upgrade_enable_all(zpool_handle_t *zhp, int *countp)
528857221772SChristopher Siden {
528957221772SChristopher Siden 	int i, ret, count;
529057221772SChristopher Siden 	boolean_t firstff = B_TRUE;
529157221772SChristopher Siden 	nvlist_t *enabled = zpool_get_features(zhp);
529257221772SChristopher Siden 
529357221772SChristopher Siden 	count = 0;
529457221772SChristopher Siden 	for (i = 0; i < SPA_FEATURES; i++) {
529557221772SChristopher Siden 		const char *fname = spa_feature_table[i].fi_uname;
529657221772SChristopher Siden 		const char *fguid = spa_feature_table[i].fi_guid;
529757221772SChristopher Siden 		if (!nvlist_exists(enabled, fguid)) {
529857221772SChristopher Siden 			char *propname;
529957221772SChristopher Siden 			verify(-1 != asprintf(&propname, "feature@%s", fname));
530057221772SChristopher Siden 			ret = zpool_set_prop(zhp, propname,
530157221772SChristopher Siden 			    ZFS_FEATURE_ENABLED);
530257221772SChristopher Siden 			if (ret != 0) {
530357221772SChristopher Siden 				free(propname);
530457221772SChristopher Siden 				return (ret);
530557221772SChristopher Siden 			}
530657221772SChristopher Siden 			count++;
530757221772SChristopher Siden 
530857221772SChristopher Siden 			if (firstff) {
530957221772SChristopher Siden 				(void) printf(gettext("Enabled the "
531057221772SChristopher Siden 				    "following features on '%s':\n"),
531157221772SChristopher Siden 				    zpool_get_name(zhp));
531257221772SChristopher Siden 				firstff = B_FALSE;
531357221772SChristopher Siden 			}
531457221772SChristopher Siden 			(void) printf(gettext("  %s\n"), fname);
531557221772SChristopher Siden 			free(propname);
531657221772SChristopher Siden 		}
531757221772SChristopher Siden 	}
531857221772SChristopher Siden 
531957221772SChristopher Siden 	if (countp != NULL)
532057221772SChristopher Siden 		*countp = count;
532157221772SChristopher Siden 	return (0);
532257221772SChristopher Siden }
532357221772SChristopher Siden 
5324eaca9bbdSeschrock static int
5325eaca9bbdSeschrock upgrade_cb(zpool_handle_t *zhp, void *arg)
5326eaca9bbdSeschrock {
5327eaca9bbdSeschrock 	upgrade_cbdata_t *cbp = arg;
5328eaca9bbdSeschrock 	nvlist_t *config;
5329eaca9bbdSeschrock 	uint64_t version;
533057221772SChristopher Siden 	boolean_t printnl = B_FALSE;
533157221772SChristopher Siden 	int ret;
5332eaca9bbdSeschrock 
5333eaca9bbdSeschrock 	config = zpool_get_config(zhp, NULL);
5334eaca9bbdSeschrock 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
5335eaca9bbdSeschrock 	    &version) == 0);
5336eaca9bbdSeschrock 
533757221772SChristopher Siden 	assert(SPA_VERSION_IS_SUPPORTED(version));
5338eaca9bbdSeschrock 
533957221772SChristopher Siden 	if (version < cbp->cb_version) {
534057221772SChristopher Siden 		cbp->cb_first = B_FALSE;
534157221772SChristopher Siden 		ret = upgrade_version(zhp, cbp->cb_version);
534257221772SChristopher Siden 		if (ret != 0)
534357221772SChristopher Siden 			return (ret);
534457221772SChristopher Siden 		printnl = B_TRUE;
534557221772SChristopher Siden 
534657221772SChristopher Siden 		/*
534757221772SChristopher Siden 		 * If they did "zpool upgrade -a", then we could
534857221772SChristopher Siden 		 * be doing ioctls to different pools.  We need
534957221772SChristopher Siden 		 * to log this history once to each pool, and bypass
535057221772SChristopher Siden 		 * the normal history logging that happens in main().
535157221772SChristopher Siden 		 */
535257221772SChristopher Siden 		(void) zpool_log_history(g_zfs, history_str);
535357221772SChristopher Siden 		log_history = B_FALSE;
535457221772SChristopher Siden 	}
535557221772SChristopher Siden 
535657221772SChristopher Siden 	if (cbp->cb_version >= SPA_VERSION_FEATURES) {
535757221772SChristopher Siden 		int count;
535857221772SChristopher Siden 		ret = upgrade_enable_all(zhp, &count);
535957221772SChristopher Siden 		if (ret != 0)
536057221772SChristopher Siden 			return (ret);
536157221772SChristopher Siden 
536257221772SChristopher Siden 		if (count > 0) {
536399653d4eSeschrock 			cbp->cb_first = B_FALSE;
536457221772SChristopher Siden 			printnl = B_TRUE;
5365eaca9bbdSeschrock 		}
536657221772SChristopher Siden 	}
5367eaca9bbdSeschrock 
536857221772SChristopher Siden 	if (printnl) {
536957221772SChristopher Siden 		(void) printf(gettext("\n"));
537057221772SChristopher Siden 	}
537157221772SChristopher Siden 
537257221772SChristopher Siden 	return (0);
537357221772SChristopher Siden }
537457221772SChristopher Siden 
537557221772SChristopher Siden static int
537657221772SChristopher Siden upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
537757221772SChristopher Siden {
537857221772SChristopher Siden 	upgrade_cbdata_t *cbp = arg;
537957221772SChristopher Siden 	nvlist_t *config;
538057221772SChristopher Siden 	uint64_t version;
538157221772SChristopher Siden 
538257221772SChristopher Siden 	config = zpool_get_config(zhp, NULL);
538357221772SChristopher Siden 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
538457221772SChristopher Siden 	    &version) == 0);
538557221772SChristopher Siden 
538657221772SChristopher Siden 	assert(SPA_VERSION_IS_SUPPORTED(version));
538757221772SChristopher Siden 
538857221772SChristopher Siden 	if (version < SPA_VERSION_FEATURES) {
5389eaca9bbdSeschrock 		if (cbp->cb_first) {
5390eaca9bbdSeschrock 			(void) printf(gettext("The following pools are "
539157221772SChristopher Siden 			    "formatted with legacy version numbers and can\n"
539257221772SChristopher Siden 			    "be upgraded to use feature flags.  After "
539357221772SChristopher Siden 			    "being upgraded, these pools\nwill no "
539457221772SChristopher Siden 			    "longer be accessible by software that does not "
539557221772SChristopher Siden 			    "support feature\nflags.\n\n"));
5396eaca9bbdSeschrock 			(void) printf(gettext("VER  POOL\n"));
5397eaca9bbdSeschrock 			(void) printf(gettext("---  ------------\n"));
539899653d4eSeschrock 			cbp->cb_first = B_FALSE;
5399eaca9bbdSeschrock 		}
5400eaca9bbdSeschrock 
54015ad82045Snd 		(void) printf("%2llu   %s\n", (u_longlong_t)version,
5402eaca9bbdSeschrock 		    zpool_get_name(zhp));
5403eaca9bbdSeschrock 	}
5404eaca9bbdSeschrock 
540557221772SChristopher Siden 	return (0);
540657221772SChristopher Siden }
540757221772SChristopher Siden 
540857221772SChristopher Siden static int
540957221772SChristopher Siden upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
541057221772SChristopher Siden {
541157221772SChristopher Siden 	upgrade_cbdata_t *cbp = arg;
541257221772SChristopher Siden 	nvlist_t *config;
541357221772SChristopher Siden 	uint64_t version;
541457221772SChristopher Siden 
541557221772SChristopher Siden 	config = zpool_get_config(zhp, NULL);
541657221772SChristopher Siden 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
541757221772SChristopher Siden 	    &version) == 0);
541857221772SChristopher Siden 
541957221772SChristopher Siden 	if (version >= SPA_VERSION_FEATURES) {
542057221772SChristopher Siden 		int i;
542157221772SChristopher Siden 		boolean_t poolfirst = B_TRUE;
542257221772SChristopher Siden 		nvlist_t *enabled = zpool_get_features(zhp);
542357221772SChristopher Siden 
542457221772SChristopher Siden 		for (i = 0; i < SPA_FEATURES; i++) {
542557221772SChristopher Siden 			const char *fguid = spa_feature_table[i].fi_guid;
542657221772SChristopher Siden 			const char *fname = spa_feature_table[i].fi_uname;
542757221772SChristopher Siden 			if (!nvlist_exists(enabled, fguid)) {
542857221772SChristopher Siden 				if (cbp->cb_first) {
542957221772SChristopher Siden 					(void) printf(gettext("\nSome "
543057221772SChristopher Siden 					    "supported features are not "
543157221772SChristopher Siden 					    "enabled on the following pools. "
543257221772SChristopher Siden 					    "Once a\nfeature is enabled the "
543357221772SChristopher Siden 					    "pool may become incompatible with "
543457221772SChristopher Siden 					    "software\nthat does not support "
543557221772SChristopher Siden 					    "the feature. See "
543657221772SChristopher Siden 					    "zpool-features(5) for "
543757221772SChristopher Siden 					    "details.\n\n"));
543857221772SChristopher Siden 					(void) printf(gettext("POOL  "
543957221772SChristopher Siden 					    "FEATURE\n"));
544057221772SChristopher Siden 					(void) printf(gettext("------"
544157221772SChristopher Siden 					    "---------\n"));
544257221772SChristopher Siden 					cbp->cb_first = B_FALSE;
544357221772SChristopher Siden 				}
544457221772SChristopher Siden 
544557221772SChristopher Siden 				if (poolfirst) {
544657221772SChristopher Siden 					(void) printf(gettext("%s\n"),
544757221772SChristopher Siden 					    zpool_get_name(zhp));
544857221772SChristopher Siden 					poolfirst = B_FALSE;
544957221772SChristopher Siden 				}
545057221772SChristopher Siden 
545157221772SChristopher Siden 				(void) printf(gettext("      %s\n"), fname);
545257221772SChristopher Siden 			}
545357221772SChristopher Siden 		}
545457221772SChristopher Siden 	}
545557221772SChristopher Siden 
545657221772SChristopher Siden 	return (0);
5457eaca9bbdSeschrock }
5458eaca9bbdSeschrock 
5459eaca9bbdSeschrock /* ARGSUSED */
5460eaca9bbdSeschrock static int
546106eeb2adSek upgrade_one(zpool_handle_t *zhp, void *data)
5462eaca9bbdSeschrock {
546357221772SChristopher Siden 	boolean_t printnl = B_FALSE;
5464990b4856Slling 	upgrade_cbdata_t *cbp = data;
5465990b4856Slling 	uint64_t cur_version;
5466eaca9bbdSeschrock 	int ret;
5467eaca9bbdSeschrock 
54688654d025Sperrin 	if (strcmp("log", zpool_get_name(zhp)) == 0) {
54698654d025Sperrin 		(void) printf(gettext("'log' is now a reserved word\n"
54708654d025Sperrin 		    "Pool 'log' must be renamed using export and import"
54718654d025Sperrin 		    " to upgrade.\n"));
54728654d025Sperrin 		return (1);
54738654d025Sperrin 	}
5474990b4856Slling 
5475990b4856Slling 	cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
5476e6c728e1Sbrendan 	if (cur_version > cbp->cb_version) {
5477eaca9bbdSeschrock 		(void) printf(gettext("Pool '%s' is already formatted "
547857221772SChristopher Siden 		    "using more current version '%llu'.\n\n"),
5479e6c728e1Sbrendan 		    zpool_get_name(zhp), cur_version);
5480e6c728e1Sbrendan 		return (0);
5481e6c728e1Sbrendan 	}
548257221772SChristopher Siden 
548357221772SChristopher Siden 	if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
5484e6c728e1Sbrendan 		(void) printf(gettext("Pool '%s' is already formatted "
548557221772SChristopher Siden 		    "using version %llu.\n\n"), zpool_get_name(zhp),
548657221772SChristopher Siden 		    cbp->cb_version);
5487eaca9bbdSeschrock 		return (0);
5488eaca9bbdSeschrock 	}
5489eaca9bbdSeschrock 
549057221772SChristopher Siden 	if (cur_version != cbp->cb_version) {
549157221772SChristopher Siden 		printnl = B_TRUE;
549257221772SChristopher Siden 		ret = upgrade_version(zhp, cbp->cb_version);
549357221772SChristopher Siden 		if (ret != 0)
549457221772SChristopher Siden 			return (ret);
549557221772SChristopher Siden 	}
549606eeb2adSek 
549757221772SChristopher Siden 	if (cbp->cb_version >= SPA_VERSION_FEATURES) {
549857221772SChristopher Siden 		int count = 0;
549957221772SChristopher Siden 		ret = upgrade_enable_all(zhp, &count);
550057221772SChristopher Siden 		if (ret != 0)
550157221772SChristopher Siden 			return (ret);
550257221772SChristopher Siden 
550357221772SChristopher Siden 		if (count != 0) {
550457221772SChristopher Siden 			printnl = B_TRUE;
550557221772SChristopher Siden 		} else if (cur_version == SPA_VERSION) {
550657221772SChristopher Siden 			(void) printf(gettext("Pool '%s' already has all "
550757221772SChristopher Siden 			    "supported features enabled.\n"),
550857221772SChristopher Siden 			    zpool_get_name(zhp));
550957221772SChristopher Siden 		}
551057221772SChristopher Siden 	}
551157221772SChristopher Siden 
551257221772SChristopher Siden 	if (printnl) {
551357221772SChristopher Siden 		(void) printf(gettext("\n"));
551406eeb2adSek 	}
5515eaca9bbdSeschrock 
551657221772SChristopher Siden 	return (0);
5517eaca9bbdSeschrock }
5518eaca9bbdSeschrock 
5519eaca9bbdSeschrock /*
5520eaca9bbdSeschrock  * zpool upgrade
5521eaca9bbdSeschrock  * zpool upgrade -v
5522990b4856Slling  * zpool upgrade [-V version] <-a | pool ...>
5523eaca9bbdSeschrock  *
5524eaca9bbdSeschrock  * With no arguments, display downrev'd ZFS pool available for upgrade.
5525eaca9bbdSeschrock  * Individual pools can be upgraded by specifying the pool, and '-a' will
5526eaca9bbdSeschrock  * upgrade all pools.
5527eaca9bbdSeschrock  */
5528eaca9bbdSeschrock int
5529eaca9bbdSeschrock zpool_do_upgrade(int argc, char **argv)
5530eaca9bbdSeschrock {
5531eaca9bbdSeschrock 	int c;
5532eaca9bbdSeschrock 	upgrade_cbdata_t cb = { 0 };
5533eaca9bbdSeschrock 	int ret = 0;
5534eaca9bbdSeschrock 	boolean_t showversions = B_FALSE;
553557221772SChristopher Siden 	boolean_t upgradeall = B_FALSE;
5536990b4856Slling 	char *end;
5537990b4856Slling 
5538eaca9bbdSeschrock 
5539eaca9bbdSeschrock 	/* check options */
5540478ed9adSEric Taylor 	while ((c = getopt(argc, argv, ":avV:")) != -1) {
5541eaca9bbdSeschrock 		switch (c) {
5542eaca9bbdSeschrock 		case 'a':
554357221772SChristopher Siden 			upgradeall = B_TRUE;
5544eaca9bbdSeschrock 			break;
5545eaca9bbdSeschrock 		case 'v':
5546eaca9bbdSeschrock 			showversions = B_TRUE;
5547eaca9bbdSeschrock 			break;
5548990b4856Slling 		case 'V':
5549990b4856Slling 			cb.cb_version = strtoll(optarg, &end, 10);
5550ad135b5dSChristopher Siden 			if (*end != '\0' ||
5551ad135b5dSChristopher Siden 			    !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
5552990b4856Slling 				(void) fprintf(stderr,
5553990b4856Slling 				    gettext("invalid version '%s'\n"), optarg);
5554990b4856Slling 				usage(B_FALSE);
5555990b4856Slling 			}
5556990b4856Slling 			break;
5557478ed9adSEric Taylor 		case ':':
5558478ed9adSEric Taylor 			(void) fprintf(stderr, gettext("missing argument for "
5559478ed9adSEric Taylor 			    "'%c' option\n"), optopt);
5560478ed9adSEric Taylor 			usage(B_FALSE);
5561478ed9adSEric Taylor 			break;
5562eaca9bbdSeschrock 		case '?':
5563eaca9bbdSeschrock 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
5564eaca9bbdSeschrock 			    optopt);
556599653d4eSeschrock 			usage(B_FALSE);
5566eaca9bbdSeschrock 		}
5567eaca9bbdSeschrock 	}
5568eaca9bbdSeschrock 
556906eeb2adSek 	cb.cb_argc = argc;
557006eeb2adSek 	cb.cb_argv = argv;
5571eaca9bbdSeschrock 	argc -= optind;
5572eaca9bbdSeschrock 	argv += optind;
5573eaca9bbdSeschrock 
5574351420b3Slling 	if (cb.cb_version == 0) {
5575351420b3Slling 		cb.cb_version = SPA_VERSION;
557657221772SChristopher Siden 	} else if (!upgradeall && argc == 0) {
5577351420b3Slling 		(void) fprintf(stderr, gettext("-V option is "
5578351420b3Slling 		    "incompatible with other arguments\n"));
5579351420b3Slling 		usage(B_FALSE);
5580351420b3Slling 	}
5581351420b3Slling 
5582eaca9bbdSeschrock 	if (showversions) {
558357221772SChristopher Siden 		if (upgradeall || argc != 0) {
5584eaca9bbdSeschrock 			(void) fprintf(stderr, gettext("-v option is "
5585eaca9bbdSeschrock 			    "incompatible with other arguments\n"));
558699653d4eSeschrock 			usage(B_FALSE);
5587eaca9bbdSeschrock 		}
558857221772SChristopher Siden 	} else if (upgradeall) {
5589eaca9bbdSeschrock 		if (argc != 0) {
5590351420b3Slling 			(void) fprintf(stderr, gettext("-a option should not "
5591351420b3Slling 			    "be used along with a pool name\n"));
559299653d4eSeschrock 			usage(B_FALSE);
5593eaca9bbdSeschrock 		}
5594eaca9bbdSeschrock 	}
5595eaca9bbdSeschrock 
5596ad135b5dSChristopher Siden 	(void) printf(gettext("This system supports ZFS pool feature "
5597ad135b5dSChristopher Siden 	    "flags.\n\n"));
5598eaca9bbdSeschrock 	if (showversions) {
559957221772SChristopher Siden 		int i;
560057221772SChristopher Siden 
560157221772SChristopher Siden 		(void) printf(gettext("The following features are "
560257221772SChristopher Siden 		    "supported:\n\n"));
560357221772SChristopher Siden 		(void) printf(gettext("FEAT DESCRIPTION\n"));
560457221772SChristopher Siden 		(void) printf("----------------------------------------------"
560557221772SChristopher Siden 		    "---------------\n");
560657221772SChristopher Siden 		for (i = 0; i < SPA_FEATURES; i++) {
560757221772SChristopher Siden 			zfeature_info_t *fi = &spa_feature_table[i];
5608ca0cc391SMatthew Ahrens 			const char *ro =
5609ca0cc391SMatthew Ahrens 			    (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
561057221772SChristopher Siden 			    " (read-only compatible)" : "";
561157221772SChristopher Siden 
561257221772SChristopher Siden 			(void) printf("%-37s%s\n", fi->fi_uname, ro);
561357221772SChristopher Siden 			(void) printf("     %s\n", fi->fi_desc);
561457221772SChristopher Siden 		}
561557221772SChristopher Siden 		(void) printf("\n");
561657221772SChristopher Siden 
561757221772SChristopher Siden 		(void) printf(gettext("The following legacy versions are also "
5618d7d4af51Smmusante 		    "supported:\n\n"));
5619eaca9bbdSeschrock 		(void) printf(gettext("VER  DESCRIPTION\n"));
5620eaca9bbdSeschrock 		(void) printf("---  -----------------------------------------"
5621eaca9bbdSeschrock 		    "---------------\n");
562299653d4eSeschrock 		(void) printf(gettext(" 1   Initial ZFS version\n"));
562344cd46caSbillm 		(void) printf(gettext(" 2   Ditto blocks "
562444cd46caSbillm 		    "(replicated metadata)\n"));
562599653d4eSeschrock 		(void) printf(gettext(" 3   Hot spares and double parity "
562699653d4eSeschrock 		    "RAID-Z\n"));
5627d7306b64Sek 		(void) printf(gettext(" 4   zpool history\n"));
5628c9431fa1Sahl 		(void) printf(gettext(" 5   Compression using the gzip "
5629c9431fa1Sahl 		    "algorithm\n"));
5630990b4856Slling 		(void) printf(gettext(" 6   bootfs pool property\n"));
56318654d025Sperrin 		(void) printf(gettext(" 7   Separate intent log devices\n"));
5632ecd6cf80Smarks 		(void) printf(gettext(" 8   Delegated administration\n"));
56338eed72d4Sck 		(void) printf(gettext(" 9   refquota and refreservation "
5634a9799022Sck 		    "properties\n"));
5635fa94a07fSbrendan 		(void) printf(gettext(" 10  Cache devices\n"));
5636088f3894Sahrens 		(void) printf(gettext(" 11  Improved scrub performance\n"));
5637bb0ade09Sahrens 		(void) printf(gettext(" 12  Snapshot properties\n"));
563874e7dc98SMatthew Ahrens 		(void) printf(gettext(" 13  snapused property\n"));
563914843421SMatthew Ahrens 		(void) printf(gettext(" 14  passthrough-x aclinherit\n"));
564014843421SMatthew Ahrens 		(void) printf(gettext(" 15  user/group space accounting\n"));
5641478ed9adSEric Taylor 		(void) printf(gettext(" 16  stmf property support\n"));
56427aeab329SAdam Leventhal 		(void) printf(gettext(" 17  Triple-parity RAID-Z\n"));
5643b24ab676SJeff Bonwick 		(void) printf(gettext(" 18  Snapshot user holds\n"));
564488ecc943SGeorge Wilson 		(void) printf(gettext(" 19  Log device removal\n"));
5645b24ab676SJeff Bonwick 		(void) printf(gettext(" 20  Compression using zle "
5646b24ab676SJeff Bonwick 		    "(zero-length encoding)\n"));
5647b24ab676SJeff Bonwick 		(void) printf(gettext(" 21  Deduplication\n"));
564892241e0bSTom Erickson 		(void) printf(gettext(" 22  Received properties\n"));
56496e1f5caaSNeil Perrin 		(void) printf(gettext(" 23  Slim ZIL\n"));
56500a586ceaSMark Shellenbaum 		(void) printf(gettext(" 24  System attributes\n"));
56513f9d6ad7SLin Ling 		(void) printf(gettext(" 25  Improved scrub stats\n"));
5652cde58dbcSMatthew Ahrens 		(void) printf(gettext(" 26  Improved snapshot deletion "
5653cde58dbcSMatthew Ahrens 		    "performance\n"));
56546e0cbcaaSMatthew Ahrens 		(void) printf(gettext(" 27  Improved snapshot creation "
56556e0cbcaaSMatthew Ahrens 		    "performance\n"));
5656cb04b873SMark J Musante 		(void) printf(gettext(" 28  Multiple vdev replacements\n"));
5657b24ab676SJeff Bonwick 		(void) printf(gettext("\nFor more information on a particular "
56589a8685acSstephanie scheffler 		    "version, including supported releases,\n"));
56599a8685acSstephanie scheffler 		(void) printf(gettext("see the ZFS Administration Guide.\n\n"));
566057221772SChristopher Siden 	} else if (argc == 0 && upgradeall) {
566157221772SChristopher Siden 		cb.cb_first = B_TRUE;
566299653d4eSeschrock 		ret = zpool_iter(g_zfs, upgrade_cb, &cb);
566357221772SChristopher Siden 		if (ret == 0 && cb.cb_first) {
566457221772SChristopher Siden 			if (cb.cb_version == SPA_VERSION) {
566557221772SChristopher Siden 				(void) printf(gettext("All pools are already "
566657221772SChristopher Siden 				    "formatted using feature flags.\n\n"));
566757221772SChristopher Siden 				(void) printf(gettext("Every feature flags "
566857221772SChristopher Siden 				    "pool already has all supported features "
566957221772SChristopher Siden 				    "enabled.\n"));
567057221772SChristopher Siden 			} else {
567157221772SChristopher Siden 				(void) printf(gettext("All pools are already "
567257221772SChristopher Siden 				    "formatted with version %llu or higher.\n"),
567357221772SChristopher Siden 				    cb.cb_version);
5674eaca9bbdSeschrock 			}
5675eaca9bbdSeschrock 		}
567657221772SChristopher Siden 	} else if (argc == 0) {
567757221772SChristopher Siden 		cb.cb_first = B_TRUE;
567857221772SChristopher Siden 		ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
567957221772SChristopher Siden 		assert(ret == 0);
5680eaca9bbdSeschrock 
568157221772SChristopher Siden 		if (cb.cb_first) {
568257221772SChristopher Siden 			(void) printf(gettext("All pools are formatted "
568357221772SChristopher Siden 			    "using feature flags.\n\n"));
568457221772SChristopher Siden 		} else {
568557221772SChristopher Siden 			(void) printf(gettext("\nUse 'zpool upgrade -v' "
568657221772SChristopher Siden 			    "for a list of available legacy versions.\n"));
568757221772SChristopher Siden 		}
568857221772SChristopher Siden 
568957221772SChristopher Siden 		cb.cb_first = B_TRUE;
569057221772SChristopher Siden 		ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
569157221772SChristopher Siden 		assert(ret == 0);
569257221772SChristopher Siden 
569357221772SChristopher Siden 		if (cb.cb_first) {
569457221772SChristopher Siden 			(void) printf(gettext("Every feature flags pool has "
569557221772SChristopher Siden 			    "all supported features enabled.\n"));
569657221772SChristopher Siden 		} else {
569757221772SChristopher Siden 			(void) printf(gettext("\n"));
5698eaca9bbdSeschrock 		}
5699eaca9bbdSeschrock 	} else {
5700b1b8ab34Slling 		ret = for_each_pool(argc, argv, B_FALSE, NULL,
5701b1b8ab34Slling 		    upgrade_one, &cb);
570206eeb2adSek 	}
570306eeb2adSek 
570406eeb2adSek 	return (ret);
570506eeb2adSek }
570606eeb2adSek 
5707ecd6cf80Smarks typedef struct hist_cbdata {
5708ecd6cf80Smarks 	boolean_t first;
57094445fffbSMatthew Ahrens 	boolean_t longfmt;
57104445fffbSMatthew Ahrens 	boolean_t internal;
5711ecd6cf80Smarks } hist_cbdata_t;
5712ecd6cf80Smarks 
571306eeb2adSek /*
571406eeb2adSek  * Print out the command history for a specific pool.
571506eeb2adSek  */
571606eeb2adSek static int
571706eeb2adSek get_history_one(zpool_handle_t *zhp, void *data)
571806eeb2adSek {
571906eeb2adSek 	nvlist_t *nvhis;
572006eeb2adSek 	nvlist_t **records;
572106eeb2adSek 	uint_t numrecords;
572206eeb2adSek 	int ret, i;
5723ecd6cf80Smarks 	hist_cbdata_t *cb = (hist_cbdata_t *)data;
572406eeb2adSek 
5725ecd6cf80Smarks 	cb->first = B_FALSE;
572606eeb2adSek 
572706eeb2adSek 	(void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
572806eeb2adSek 
572906eeb2adSek 	if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
573006eeb2adSek 		return (ret);
573106eeb2adSek 
573206eeb2adSek 	verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
573306eeb2adSek 	    &records, &numrecords) == 0);
573406eeb2adSek 	for (i = 0; i < numrecords; i++) {
57354445fffbSMatthew Ahrens 		nvlist_t *rec = records[i];
57364445fffbSMatthew Ahrens 		char tbuf[30] = "";
5737ecd6cf80Smarks 
57384445fffbSMatthew Ahrens 		if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
57394445fffbSMatthew Ahrens 			time_t tsec;
57404445fffbSMatthew Ahrens 			struct tm t;
57414445fffbSMatthew Ahrens 
57424445fffbSMatthew Ahrens 			tsec = fnvlist_lookup_uint64(records[i],
57434445fffbSMatthew Ahrens 			    ZPOOL_HIST_TIME);
57444445fffbSMatthew Ahrens 			(void) localtime_r(&tsec, &t);
57454445fffbSMatthew Ahrens 			(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
57464445fffbSMatthew Ahrens 		}
5747ecd6cf80Smarks 
57484445fffbSMatthew Ahrens 		if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
57494445fffbSMatthew Ahrens 			(void) printf("%s %s", tbuf,
57504445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
57514445fffbSMatthew Ahrens 		} else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
57524445fffbSMatthew Ahrens 			int ievent =
57534445fffbSMatthew Ahrens 			    fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
57544445fffbSMatthew Ahrens 			if (!cb->internal)
57554445fffbSMatthew Ahrens 				continue;
57564445fffbSMatthew Ahrens 			if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
57574445fffbSMatthew Ahrens 				(void) printf("%s unrecognized record:\n",
57584445fffbSMatthew Ahrens 				    tbuf);
57594445fffbSMatthew Ahrens 				dump_nvlist(rec, 4);
57604445fffbSMatthew Ahrens 				continue;
57614445fffbSMatthew Ahrens 			}
57624445fffbSMatthew Ahrens 			(void) printf("%s [internal %s txg:%lld] %s", tbuf,
57634445fffbSMatthew Ahrens 			    zfs_history_event_names[ievent],
57644445fffbSMatthew Ahrens 			    fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
57654445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
57664445fffbSMatthew Ahrens 		} else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
57674445fffbSMatthew Ahrens 			if (!cb->internal)
5768ecd6cf80Smarks 				continue;
57694445fffbSMatthew Ahrens 			(void) printf("%s [txg:%lld] %s", tbuf,
57704445fffbSMatthew Ahrens 			    fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
57714445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
57724445fffbSMatthew Ahrens 			if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
57734445fffbSMatthew Ahrens 				(void) printf(" %s (%llu)",
57744445fffbSMatthew Ahrens 				    fnvlist_lookup_string(rec,
57754445fffbSMatthew Ahrens 				    ZPOOL_HIST_DSNAME),
57764445fffbSMatthew Ahrens 				    fnvlist_lookup_uint64(rec,
57774445fffbSMatthew Ahrens 				    ZPOOL_HIST_DSID));
57784445fffbSMatthew Ahrens 			}
57794445fffbSMatthew Ahrens 			(void) printf(" %s", fnvlist_lookup_string(rec,
57804445fffbSMatthew Ahrens 			    ZPOOL_HIST_INT_STR));
57814445fffbSMatthew Ahrens 		} else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
57824445fffbSMatthew Ahrens 			if (!cb->internal)
57834445fffbSMatthew Ahrens 				continue;
57844445fffbSMatthew Ahrens 			(void) printf("%s ioctl %s\n", tbuf,
57854445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
57864445fffbSMatthew Ahrens 			if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
57874445fffbSMatthew Ahrens 				(void) printf("    input:\n");
57884445fffbSMatthew Ahrens 				dump_nvlist(fnvlist_lookup_nvlist(rec,
57894445fffbSMatthew Ahrens 				    ZPOOL_HIST_INPUT_NVL), 8);
57904445fffbSMatthew Ahrens 			}
57914445fffbSMatthew Ahrens 			if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
57924445fffbSMatthew Ahrens 				(void) printf("    output:\n");
57934445fffbSMatthew Ahrens 				dump_nvlist(fnvlist_lookup_nvlist(rec,
57944445fffbSMatthew Ahrens 				    ZPOOL_HIST_OUTPUT_NVL), 8);
57954445fffbSMatthew Ahrens 			}
5796dfc11533SChris Williamson 			if (nvlist_exists(rec, ZPOOL_HIST_ERRNO)) {
5797dfc11533SChris Williamson 				(void) printf("    errno: %lld\n",
5798dfc11533SChris Williamson 				    fnvlist_lookup_int64(rec,
5799dfc11533SChris Williamson 				    ZPOOL_HIST_ERRNO));
5800dfc11533SChris Williamson 			}
58014445fffbSMatthew Ahrens 		} else {
58024445fffbSMatthew Ahrens 			if (!cb->internal)
5803ecd6cf80Smarks 				continue;
58044445fffbSMatthew Ahrens 			(void) printf("%s unrecognized record:\n", tbuf);
58054445fffbSMatthew Ahrens 			dump_nvlist(rec, 4);
580606eeb2adSek 		}
5807ecd6cf80Smarks 
5808ecd6cf80Smarks 		if (!cb->longfmt) {
5809ecd6cf80Smarks 			(void) printf("\n");
5810ecd6cf80Smarks 			continue;
5811ecd6cf80Smarks 		}
5812ecd6cf80Smarks 		(void) printf(" [");
58134445fffbSMatthew Ahrens 		if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
58144445fffbSMatthew Ahrens 			uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
58154445fffbSMatthew Ahrens 			struct passwd *pwd = getpwuid(who);
58164445fffbSMatthew Ahrens 			(void) printf("user %d ", (int)who);
58174445fffbSMatthew Ahrens 			if (pwd != NULL)
58184445fffbSMatthew Ahrens 				(void) printf("(%s) ", pwd->pw_name);
5819ecd6cf80Smarks 		}
58204445fffbSMatthew Ahrens 		if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
58214445fffbSMatthew Ahrens 			(void) printf("on %s",
58224445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
5823ecd6cf80Smarks 		}
58244445fffbSMatthew Ahrens 		if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
58254445fffbSMatthew Ahrens 			(void) printf(":%s",
58264445fffbSMatthew Ahrens 			    fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
5827ecd6cf80Smarks 		}
5828ecd6cf80Smarks 		(void) printf("]");
5829ecd6cf80Smarks 		(void) printf("\n");
583006eeb2adSek 	}
583106eeb2adSek 	(void) printf("\n");
583206eeb2adSek 	nvlist_free(nvhis);
583306eeb2adSek 
583406eeb2adSek 	return (ret);
583506eeb2adSek }
583606eeb2adSek 
583706eeb2adSek /*
583806eeb2adSek  * zpool history <pool>
583906eeb2adSek  *
584006eeb2adSek  * Displays the history of commands that modified pools.
584106eeb2adSek  */
584206eeb2adSek int
584306eeb2adSek zpool_do_history(int argc, char **argv)
584406eeb2adSek {
5845ecd6cf80Smarks 	hist_cbdata_t cbdata = { 0 };
584606eeb2adSek 	int ret;
5847ecd6cf80Smarks 	int c;
584806eeb2adSek 
5849ecd6cf80Smarks 	cbdata.first = B_TRUE;
5850ecd6cf80Smarks 	/* check options */
5851ecd6cf80Smarks 	while ((c = getopt(argc, argv, "li")) != -1) {
5852ecd6cf80Smarks 		switch (c) {
5853ecd6cf80Smarks 		case 'l':
58544445fffbSMatthew Ahrens 			cbdata.longfmt = B_TRUE;
5855ecd6cf80Smarks 			break;
5856ecd6cf80Smarks 		case 'i':
58574445fffbSMatthew Ahrens 			cbdata.internal = B_TRUE;
5858ecd6cf80Smarks 			break;
5859ecd6cf80Smarks 		case '?':
5860ecd6cf80Smarks 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
5861ecd6cf80Smarks 			    optopt);
5862ecd6cf80Smarks 			usage(B_FALSE);
5863ecd6cf80Smarks 		}
5864ecd6cf80Smarks 	}
586506eeb2adSek 	argc -= optind;
586606eeb2adSek 	argv += optind;
586706eeb2adSek 
5868b1b8ab34Slling 	ret = for_each_pool(argc, argv, B_FALSE,  NULL, get_history_one,
5869ecd6cf80Smarks 	    &cbdata);
587006eeb2adSek 
5871ecd6cf80Smarks 	if (argc == 0 && cbdata.first == B_TRUE) {
587206eeb2adSek 		(void) printf(gettext("no pools available\n"));
587306eeb2adSek 		return (0);
5874eaca9bbdSeschrock 	}
5875eaca9bbdSeschrock 
5876eaca9bbdSeschrock 	return (ret);
5877eaca9bbdSeschrock }
5878eaca9bbdSeschrock 
5879b1b8ab34Slling static int
5880b1b8ab34Slling get_callback(zpool_handle_t *zhp, void *data)
5881b1b8ab34Slling {
5882990b4856Slling 	zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5883b1b8ab34Slling 	char value[MAXNAMELEN];
5884990b4856Slling 	zprop_source_t srctype;
5885990b4856Slling 	zprop_list_t *pl;
5886b1b8ab34Slling 
5887b1b8ab34Slling 	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5888b1b8ab34Slling 
5889b1b8ab34Slling 		/*
5890990b4856Slling 		 * Skip the special fake placeholder. This will also skip
5891990b4856Slling 		 * over the name property when 'all' is specified.
5892b1b8ab34Slling 		 */
5893990b4856Slling 		if (pl->pl_prop == ZPOOL_PROP_NAME &&
5894b1b8ab34Slling 		    pl == cbp->cb_proplist)
5895b1b8ab34Slling 			continue;
5896b1b8ab34Slling 
5897ad135b5dSChristopher Siden 		if (pl->pl_prop == ZPROP_INVAL &&
5898ad135b5dSChristopher Siden 		    (zpool_prop_feature(pl->pl_user_prop) ||
5899ad135b5dSChristopher Siden 		    zpool_prop_unsupported(pl->pl_user_prop))) {
5900ad135b5dSChristopher Siden 			srctype = ZPROP_SRC_LOCAL;
5901b1b8ab34Slling 
5902ad135b5dSChristopher Siden 			if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5903ad135b5dSChristopher Siden 			    value, sizeof (value)) == 0) {
5904ad135b5dSChristopher Siden 				zprop_print_one_property(zpool_get_name(zhp),
5905ad135b5dSChristopher Siden 				    cbp, pl->pl_user_prop, value, srctype,
5906ad135b5dSChristopher Siden 				    NULL, NULL);
5907ad135b5dSChristopher Siden 			}
5908ad135b5dSChristopher Siden 		} else {
5909ad135b5dSChristopher Siden 			if (zpool_get_prop(zhp, pl->pl_prop, value,
5910c58b3526SAdam Stevko 			    sizeof (value), &srctype, cbp->cb_literal) != 0)
5911ad135b5dSChristopher Siden 				continue;
5912ad135b5dSChristopher Siden 
5913ad135b5dSChristopher Siden 			zprop_print_one_property(zpool_get_name(zhp), cbp,
5914ad135b5dSChristopher Siden 			    zpool_prop_to_name(pl->pl_prop), value, srctype,
5915ad135b5dSChristopher Siden 			    NULL, NULL);
5916ad135b5dSChristopher Siden 		}
5917b1b8ab34Slling 	}
5918b1b8ab34Slling 	return (0);
5919b1b8ab34Slling }
5920b1b8ab34Slling 
5921c58b3526SAdam Stevko /*
5922c58b3526SAdam Stevko  * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
5923c58b3526SAdam Stevko  *
5924c58b3526SAdam Stevko  *	-H	Scripted mode.  Don't display headers, and separate properties
5925c58b3526SAdam Stevko  *		by a single tab.
5926c58b3526SAdam Stevko  *	-o	List of columns to display.  Defaults to
5927c58b3526SAdam Stevko  *		"name,property,value,source".
592804e56356SAndriy Gapon  *	-p	Diplay values in parsable (exact) format.
5929c58b3526SAdam Stevko  *
5930c58b3526SAdam Stevko  * Get properties of pools in the system. Output space statistics
5931c58b3526SAdam Stevko  * for each one as well as other attributes.
5932c58b3526SAdam Stevko  */
5933b1b8ab34Slling int
5934b1b8ab34Slling zpool_do_get(int argc, char **argv)
5935b1b8ab34Slling {
5936990b4856Slling 	zprop_get_cbdata_t cb = { 0 };
5937990b4856Slling 	zprop_list_t fake_name = { 0 };
5938b1b8ab34Slling 	int ret;
5939c58b3526SAdam Stevko 	int c, i;
5940c58b3526SAdam Stevko 	char *value;
5941b1b8ab34Slling 
5942b1b8ab34Slling 	cb.cb_first = B_TRUE;
5943c58b3526SAdam Stevko 
5944c58b3526SAdam Stevko 	/*
5945c58b3526SAdam Stevko 	 * Set up default columns and sources.
5946c58b3526SAdam Stevko 	 */
5947990b4856Slling 	cb.cb_sources = ZPROP_SRC_ALL;
5948b1b8ab34Slling 	cb.cb_columns[0] = GET_COL_NAME;
5949b1b8ab34Slling 	cb.cb_columns[1] = GET_COL_PROPERTY;
5950b1b8ab34Slling 	cb.cb_columns[2] = GET_COL_VALUE;
5951b1b8ab34Slling 	cb.cb_columns[3] = GET_COL_SOURCE;
5952990b4856Slling 	cb.cb_type = ZFS_TYPE_POOL;
5953b1b8ab34Slling 
5954c58b3526SAdam Stevko 	/* check options */
5955c58b3526SAdam Stevko 	while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
5956c58b3526SAdam Stevko 		switch (c) {
5957c58b3526SAdam Stevko 		case 'p':
5958c58b3526SAdam Stevko 			cb.cb_literal = B_TRUE;
5959c58b3526SAdam Stevko 			break;
5960c58b3526SAdam Stevko 		case 'H':
5961c58b3526SAdam Stevko 			cb.cb_scripted = B_TRUE;
5962c58b3526SAdam Stevko 			break;
5963c58b3526SAdam Stevko 		case 'o':
5964c58b3526SAdam Stevko 			bzero(&cb.cb_columns, sizeof (cb.cb_columns));
5965c58b3526SAdam Stevko 			i = 0;
5966c58b3526SAdam Stevko 			while (*optarg != '\0') {
5967c58b3526SAdam Stevko 				static char *col_subopts[] =
5968c58b3526SAdam Stevko 				{ "name", "property", "value", "source",
5969c58b3526SAdam Stevko 				"all", NULL };
5970c58b3526SAdam Stevko 
5971c58b3526SAdam Stevko 				if (i == ZFS_GET_NCOLS) {
5972c58b3526SAdam Stevko 					(void) fprintf(stderr, gettext("too "
5973c58b3526SAdam Stevko 					"many fields given to -o "
5974c58b3526SAdam Stevko 					"option\n"));
5975c58b3526SAdam Stevko 					usage(B_FALSE);
5976c58b3526SAdam Stevko 				}
5977c58b3526SAdam Stevko 
5978c58b3526SAdam Stevko 				switch (getsubopt(&optarg, col_subopts,
5979c58b3526SAdam Stevko 				    &value)) {
5980c58b3526SAdam Stevko 				case 0:
5981c58b3526SAdam Stevko 					cb.cb_columns[i++] = GET_COL_NAME;
5982c58b3526SAdam Stevko 					break;
5983c58b3526SAdam Stevko 				case 1:
5984c58b3526SAdam Stevko 					cb.cb_columns[i++] = GET_COL_PROPERTY;
5985c58b3526SAdam Stevko 					break;
5986c58b3526SAdam Stevko 				case 2:
5987c58b3526SAdam Stevko 					cb.cb_columns[i++] = GET_COL_VALUE;
5988c58b3526SAdam Stevko 					break;
5989c58b3526SAdam Stevko 				case 3:
5990c58b3526SAdam Stevko 					cb.cb_columns[i++] = GET_COL_SOURCE;
5991c58b3526SAdam Stevko 					break;
5992c58b3526SAdam Stevko 				case 4:
5993c58b3526SAdam Stevko 					if (i > 0) {
5994c58b3526SAdam Stevko 						(void) fprintf(stderr,
5995c58b3526SAdam Stevko 						    gettext("\"all\" conflicts "
5996c58b3526SAdam Stevko 						    "with specific fields "
5997c58b3526SAdam Stevko 						    "given to -o option\n"));
5998c58b3526SAdam Stevko 						usage(B_FALSE);
5999c58b3526SAdam Stevko 					}
6000c58b3526SAdam Stevko 					cb.cb_columns[0] = GET_COL_NAME;
6001c58b3526SAdam Stevko 					cb.cb_columns[1] = GET_COL_PROPERTY;
6002c58b3526SAdam Stevko 					cb.cb_columns[2] = GET_COL_VALUE;
6003c58b3526SAdam Stevko 					cb.cb_columns[3] = GET_COL_SOURCE;
6004c58b3526SAdam Stevko 					i = ZFS_GET_NCOLS;
6005c58b3526SAdam Stevko 					break;
6006c58b3526SAdam Stevko 				default:
6007c58b3526SAdam Stevko 					(void) fprintf(stderr,
6008c58b3526SAdam Stevko 					    gettext("invalid column name "
6009c58b3526SAdam Stevko 					    "'%s'\n"), value);
6010c58b3526SAdam Stevko 					usage(B_FALSE);
6011c58b3526SAdam Stevko 				}
6012c58b3526SAdam Stevko 			}
6013c58b3526SAdam Stevko 			break;
6014c58b3526SAdam Stevko 		case '?':
6015c58b3526SAdam Stevko 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
6016c58b3526SAdam Stevko 			    optopt);
6017c58b3526SAdam Stevko 			usage(B_FALSE);
6018c58b3526SAdam Stevko 		}
6019c58b3526SAdam Stevko 	}
6020c58b3526SAdam Stevko 
6021c58b3526SAdam Stevko 	argc -= optind;
6022c58b3526SAdam Stevko 	argv += optind;
6023c58b3526SAdam Stevko 
6024c58b3526SAdam Stevko 	if (argc < 1) {
6025c58b3526SAdam Stevko 		(void) fprintf(stderr, gettext("missing property "
6026c58b3526SAdam Stevko 		    "argument\n"));
6027c58b3526SAdam Stevko 		usage(B_FALSE);
6028c58b3526SAdam Stevko 	}
6029c58b3526SAdam Stevko 
6030c58b3526SAdam Stevko 	if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
6031990b4856Slling 	    ZFS_TYPE_POOL) != 0)
6032b1b8ab34Slling 		usage(B_FALSE);
6033b1b8ab34Slling 
6034c58b3526SAdam Stevko 	argc--;
6035c58b3526SAdam Stevko 	argv++;
6036c58b3526SAdam Stevko 
6037b1b8ab34Slling 	if (cb.cb_proplist != NULL) {
6038990b4856Slling 		fake_name.pl_prop = ZPOOL_PROP_NAME;
6039b1b8ab34Slling 		fake_name.pl_width = strlen(gettext("NAME"));
6040b1b8ab34Slling 		fake_name.pl_next = cb.cb_proplist;
6041b1b8ab34Slling 		cb.cb_proplist = &fake_name;
6042b1b8ab34Slling 	}
6043b1b8ab34Slling 
6044c58b3526SAdam Stevko 	ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
6045b1b8ab34Slling 	    get_callback, &cb);
6046b1b8ab34Slling 
6047b1b8ab34Slling 	if (cb.cb_proplist == &fake_name)
6048990b4856Slling 		zprop_free_list(fake_name.pl_next);
6049b1b8ab34Slling 	else
6050990b4856Slling 		zprop_free_list(cb.cb_proplist);
6051b1b8ab34Slling 
6052b1b8ab34Slling 	return (ret);
6053b1b8ab34Slling }
6054b1b8ab34Slling 
6055b1b8ab34Slling typedef struct set_cbdata {
6056b1b8ab34Slling 	char *cb_propname;
6057b1b8ab34Slling 	char *cb_value;
6058b1b8ab34Slling 	boolean_t cb_any_successful;
6059b1b8ab34Slling } set_cbdata_t;
6060b1b8ab34Slling 
6061b1b8ab34Slling int
6062b1b8ab34Slling set_callback(zpool_handle_t *zhp, void *data)
6063b1b8ab34Slling {
6064b1b8ab34Slling 	int error;
6065b1b8ab34Slling 	set_cbdata_t *cb = (set_cbdata_t *)data;
6066b1b8ab34Slling 
6067b1b8ab34Slling 	error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
6068b1b8ab34Slling 
6069b1b8ab34Slling 	if (!error)
6070b1b8ab34Slling 		cb->cb_any_successful = B_TRUE;
6071b1b8ab34Slling 
6072b1b8ab34Slling 	return (error);
6073b1b8ab34Slling }
6074b1b8ab34Slling 
6075b1b8ab34Slling int
6076b1b8ab34Slling zpool_do_set(int argc, char **argv)
6077b1b8ab34Slling {
6078b1b8ab34Slling 	set_cbdata_t cb = { 0 };
6079b1b8ab34Slling 	int error;
6080b1b8ab34Slling 
6081b1b8ab34Slling 	if (argc > 1 && argv[1][0] == '-') {
6082b1b8ab34Slling 		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
6083b1b8ab34Slling 		    argv[1][1]);
6084b1b8ab34Slling 		usage(B_FALSE);
6085b1b8ab34Slling 	}
6086b1b8ab34Slling 
6087b1b8ab34Slling 	if (argc < 2) {
6088b1b8ab34Slling 		(void) fprintf(stderr, gettext("missing property=value "
6089b1b8ab34Slling 		    "argument\n"));
6090b1b8ab34Slling 		usage(B_FALSE);
6091b1b8ab34Slling 	}
6092b1b8ab34Slling 
6093b1b8ab34Slling 	if (argc < 3) {
6094b1b8ab34Slling 		(void) fprintf(stderr, gettext("missing pool name\n"));
6095b1b8ab34Slling 		usage(B_FALSE);
6096b1b8ab34Slling 	}
6097b1b8ab34Slling 
6098b1b8ab34Slling 	if (argc > 3) {
6099b1b8ab34Slling 		(void) fprintf(stderr, gettext("too many pool names\n"));
6100b1b8ab34Slling 		usage(B_FALSE);
6101b1b8ab34Slling 	}
6102b1b8ab34Slling 
6103b1b8ab34Slling 	cb.cb_propname = argv[1];
6104b1b8ab34Slling 	cb.cb_value = strchr(cb.cb_propname, '=');
6105b1b8ab34Slling 	if (cb.cb_value == NULL) {
6106b1b8ab34Slling 		(void) fprintf(stderr, gettext("missing value in "
6107b1b8ab34Slling 		    "property=value argument\n"));
6108b1b8ab34Slling 		usage(B_FALSE);
6109b1b8ab34Slling 	}
6110b1b8ab34Slling 
6111b1b8ab34Slling 	*(cb.cb_value) = '\0';
6112b1b8ab34Slling 	cb.cb_value++;
6113b1b8ab34Slling 
6114b1b8ab34Slling 	error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
6115b1b8ab34Slling 	    set_callback, &cb);
6116b1b8ab34Slling 
6117b1b8ab34Slling 	return (error);
6118b1b8ab34Slling }
6119b1b8ab34Slling 
6120b1b8ab34Slling static int
6121b1b8ab34Slling find_command_idx(char *command, int *idx)
6122b1b8ab34Slling {
6123b1b8ab34Slling 	int i;
6124b1b8ab34Slling 
6125b1b8ab34Slling 	for (i = 0; i < NCOMMAND; i++) {
6126b1b8ab34Slling 		if (command_table[i].name == NULL)
6127b1b8ab34Slling 			continue;
6128b1b8ab34Slling 
6129b1b8ab34Slling 		if (strcmp(command, command_table[i].name) == 0) {
6130b1b8ab34Slling 			*idx = i;
6131b1b8ab34Slling 			return (0);
6132b1b8ab34Slling 		}
6133b1b8ab34Slling 	}
6134b1b8ab34Slling 	return (1);
6135b1b8ab34Slling }
6136b1b8ab34Slling 
6137fa9e4066Sahrens int
6138fa9e4066Sahrens main(int argc, char **argv)
6139fa9e4066Sahrens {
6140b327cd3fSIgor Kozhukhov 	int ret = 0;
6141fa9e4066Sahrens 	int i;
6142fa9e4066Sahrens 	char *cmdname;
6143fa9e4066Sahrens 
6144fa9e4066Sahrens 	(void) setlocale(LC_ALL, "");
6145fa9e4066Sahrens 	(void) textdomain(TEXT_DOMAIN);
6146fa9e4066Sahrens 
614799653d4eSeschrock 	if ((g_zfs = libzfs_init()) == NULL) {
614899653d4eSeschrock 		(void) fprintf(stderr, gettext("internal error: failed to "
6149203a47d8Snd 		    "initialize ZFS library\n"));
615099653d4eSeschrock 		return (1);
615199653d4eSeschrock 	}
615299653d4eSeschrock 
615399653d4eSeschrock 	libzfs_print_on_error(g_zfs, B_TRUE);
615499653d4eSeschrock 
6155fa9e4066Sahrens 	opterr = 0;
6156fa9e4066Sahrens 
6157fa9e4066Sahrens 	/*
6158fa9e4066Sahrens 	 * Make sure the user has specified some command.
6159fa9e4066Sahrens 	 */
6160fa9e4066Sahrens 	if (argc < 2) {
6161fa9e4066Sahrens 		(void) fprintf(stderr, gettext("missing command\n"));
616299653d4eSeschrock 		usage(B_FALSE);
6163fa9e4066Sahrens 	}
6164fa9e4066Sahrens 
6165fa9e4066Sahrens 	cmdname = argv[1];
6166fa9e4066Sahrens 
6167fa9e4066Sahrens 	/*
6168fa9e4066Sahrens 	 * Special case '-?'
6169fa9e4066Sahrens 	 */
6170fa9e4066Sahrens 	if (strcmp(cmdname, "-?") == 0)
617199653d4eSeschrock 		usage(B_TRUE);
6172fa9e4066Sahrens 
61734445fffbSMatthew Ahrens 	zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
61742a6b87f0Sek 
6175fa9e4066Sahrens 	/*
6176fa9e4066Sahrens 	 * Run the appropriate command.
6177fa9e4066Sahrens 	 */
6178b1b8ab34Slling 	if (find_command_idx(cmdname, &i) == 0) {
6179b1b8ab34Slling 		current_command = &command_table[i];
6180b1b8ab34Slling 		ret = command_table[i].func(argc - 1, argv + 1);
618191ebeef5Sahrens 	} else if (strchr(cmdname, '=')) {
618291ebeef5Sahrens 		verify(find_command_idx("set", &i) == 0);
618391ebeef5Sahrens 		current_command = &command_table[i];
618491ebeef5Sahrens 		ret = command_table[i].func(argc, argv);
618591ebeef5Sahrens 	} else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
618691ebeef5Sahrens 		/*
618791ebeef5Sahrens 		 * 'freeze' is a vile debugging abomination, so we treat
618891ebeef5Sahrens 		 * it as such.
618991ebeef5Sahrens 		 */
6190ea8dc4b6Seschrock 		char buf[16384];
6191ea8dc4b6Seschrock 		int fd = open(ZFS_DEV, O_RDWR);
6192fa9e4066Sahrens 		(void) strcpy((void *)buf, argv[2]);
6193fa9e4066Sahrens 		return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
619491ebeef5Sahrens 	} else {
6195fa9e4066Sahrens 		(void) fprintf(stderr, gettext("unrecognized "
6196fa9e4066Sahrens 		    "command '%s'\n"), cmdname);
619799653d4eSeschrock 		usage(B_FALSE);
6198fa9e4066Sahrens 	}
6199fa9e4066Sahrens 
62004445fffbSMatthew Ahrens 	if (ret == 0 && log_history)
62014445fffbSMatthew Ahrens 		(void) zpool_log_history(g_zfs, history_str);
62024445fffbSMatthew Ahrens 
620399653d4eSeschrock 	libzfs_fini(g_zfs);
620499653d4eSeschrock 
6205fa9e4066Sahrens 	/*
6206fa9e4066Sahrens 	 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6207fa9e4066Sahrens 	 * for the purposes of running ::findleaks.
6208fa9e4066Sahrens 	 */
6209fa9e4066Sahrens 	if (getenv("ZFS_ABORT") != NULL) {
6210fa9e4066Sahrens 		(void) printf("dumping core by request\n");
6211fa9e4066Sahrens 		abort();
6212fa9e4066Sahrens 	}
6213fa9e4066Sahrens 
6214fa9e4066Sahrens 	return (ret);
6215fa9e4066Sahrens }
6216