zpool_main.c revision 269118
1168404Spjd/*
2168404Spjd * CDDL HEADER START
3168404Spjd *
4168404Spjd * The contents of this file are subject to the terms of the
5168404Spjd * Common Development and Distribution License (the "License").
6168404Spjd * You may not use this file except in compliance with the License.
7168404Spjd *
8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9168404Spjd * or http://www.opensolaris.org/os/licensing.
10168404Spjd * See the License for the specific language governing permissions
11168404Spjd * and limitations under the License.
12168404Spjd *
13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each
14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15168404Spjd * If applicable, add the following below this CDDL HEADER, with the
16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying
17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner]
18168404Spjd *
19168404Spjd * CDDL HEADER END
20168404Spjd */
21168404Spjd
22168404Spjd/*
23219089Spjd * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24227497Smm * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25268720Sdelphij * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
26236145Smm * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27236155Smm * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
28254758Sdelphij * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
29168404Spjd */
30168404Spjd
31168404Spjd#include <solaris.h>
32168404Spjd#include <assert.h>
33168404Spjd#include <ctype.h>
34168404Spjd#include <dirent.h>
35168404Spjd#include <errno.h>
36168404Spjd#include <fcntl.h>
37168404Spjd#include <libgen.h>
38168404Spjd#include <libintl.h>
39168404Spjd#include <libuutil.h>
40168404Spjd#include <locale.h>
41168404Spjd#include <stdio.h>
42168404Spjd#include <stdlib.h>
43168404Spjd#include <string.h>
44168404Spjd#include <strings.h>
45168404Spjd#include <unistd.h>
46168404Spjd#include <priv.h>
47185029Spjd#include <pwd.h>
48185029Spjd#include <zone.h>
49168404Spjd#include <sys/time.h>
50236155Smm#include <zfs_prop.h>
51168404Spjd#include <sys/fs/zfs.h>
52168404Spjd#include <sys/stat.h>
53168404Spjd
54168404Spjd#include <libzfs.h>
55168404Spjd
56168404Spjd#include "zpool_util.h"
57185029Spjd#include "zfs_comutil.h"
58236884Smm#include "zfeature_common.h"
59168404Spjd
60219089Spjd#include "statcommon.h"
61219089Spjd
62168404Spjdstatic int zpool_do_create(int, char **);
63168404Spjdstatic int zpool_do_destroy(int, char **);
64168404Spjd
65168404Spjdstatic int zpool_do_add(int, char **);
66168404Spjdstatic int zpool_do_remove(int, char **);
67224171Sgibbsstatic int zpool_do_labelclear(int, char **);
68168404Spjd
69168404Spjdstatic int zpool_do_list(int, char **);
70168404Spjdstatic int zpool_do_iostat(int, char **);
71168404Spjdstatic int zpool_do_status(int, char **);
72168404Spjd
73168404Spjdstatic int zpool_do_online(int, char **);
74168404Spjdstatic int zpool_do_offline(int, char **);
75168404Spjdstatic int zpool_do_clear(int, char **);
76236155Smmstatic int zpool_do_reopen(int, char **);
77168404Spjd
78228103Smmstatic int zpool_do_reguid(int, char **);
79228103Smm
80168404Spjdstatic int zpool_do_attach(int, char **);
81168404Spjdstatic int zpool_do_detach(int, char **);
82168404Spjdstatic int zpool_do_replace(int, char **);
83219089Spjdstatic int zpool_do_split(int, char **);
84168404Spjd
85168404Spjdstatic int zpool_do_scrub(int, char **);
86168404Spjd
87168404Spjdstatic int zpool_do_import(int, char **);
88168404Spjdstatic int zpool_do_export(int, char **);
89168404Spjd
90168404Spjdstatic int zpool_do_upgrade(int, char **);
91168404Spjd
92168404Spjdstatic int zpool_do_history(int, char **);
93168404Spjd
94168404Spjdstatic int zpool_do_get(int, char **);
95168404Spjdstatic int zpool_do_set(int, char **);
96168404Spjd
97168404Spjd/*
98168404Spjd * These libumem hooks provide a reasonable set of defaults for the allocator's
99168404Spjd * debugging facilities.
100168404Spjd */
101185029Spjd
102185029Spjd#ifdef DEBUG
103168404Spjdconst char *
104168404Spjd_umem_debug_init(void)
105168404Spjd{
106168404Spjd	return ("default,verbose"); /* $UMEM_DEBUG setting */
107168404Spjd}
108168404Spjd
109168404Spjdconst char *
110168404Spjd_umem_logging_init(void)
111168404Spjd{
112168404Spjd	return ("fail,contents"); /* $UMEM_LOGGING setting */
113168404Spjd}
114185029Spjd#endif
115168404Spjd
116168404Spjdtypedef enum {
117168404Spjd	HELP_ADD,
118168404Spjd	HELP_ATTACH,
119168404Spjd	HELP_CLEAR,
120168404Spjd	HELP_CREATE,
121168404Spjd	HELP_DESTROY,
122168404Spjd	HELP_DETACH,
123168404Spjd	HELP_EXPORT,
124168404Spjd	HELP_HISTORY,
125168404Spjd	HELP_IMPORT,
126168404Spjd	HELP_IOSTAT,
127224171Sgibbs	HELP_LABELCLEAR,
128168404Spjd	HELP_LIST,
129168404Spjd	HELP_OFFLINE,
130168404Spjd	HELP_ONLINE,
131168404Spjd	HELP_REPLACE,
132168404Spjd	HELP_REMOVE,
133168404Spjd	HELP_SCRUB,
134168404Spjd	HELP_STATUS,
135168404Spjd	HELP_UPGRADE,
136168404Spjd	HELP_GET,
137219089Spjd	HELP_SET,
138228103Smm	HELP_SPLIT,
139236155Smm	HELP_REGUID,
140236155Smm	HELP_REOPEN
141168404Spjd} zpool_help_t;
142168404Spjd
143168404Spjd
144168404Spjdtypedef struct zpool_command {
145168404Spjd	const char	*name;
146168404Spjd	int		(*func)(int, char **);
147168404Spjd	zpool_help_t	usage;
148168404Spjd} zpool_command_t;
149168404Spjd
150168404Spjd/*
151168404Spjd * Master command table.  Each ZFS command has a name, associated function, and
152168404Spjd * usage message.  The usage messages need to be internationalized, so we have
153168404Spjd * to have a function to return the usage message based on a command index.
154168404Spjd *
155168404Spjd * These commands are organized according to how they are displayed in the usage
156168404Spjd * message.  An empty command (one with a NULL name) indicates an empty line in
157168404Spjd * the generic usage message.
158168404Spjd */
159168404Spjdstatic zpool_command_t command_table[] = {
160168404Spjd	{ "create",	zpool_do_create,	HELP_CREATE		},
161168404Spjd	{ "destroy",	zpool_do_destroy,	HELP_DESTROY		},
162168404Spjd	{ NULL },
163168404Spjd	{ "add",	zpool_do_add,		HELP_ADD		},
164168404Spjd	{ "remove",	zpool_do_remove,	HELP_REMOVE		},
165168404Spjd	{ NULL },
166224171Sgibbs	{ "labelclear",	zpool_do_labelclear,	HELP_LABELCLEAR		},
167224171Sgibbs	{ NULL },
168168404Spjd	{ "list",	zpool_do_list,		HELP_LIST		},
169168404Spjd	{ "iostat",	zpool_do_iostat,	HELP_IOSTAT		},
170168404Spjd	{ "status",	zpool_do_status,	HELP_STATUS		},
171168404Spjd	{ NULL },
172168404Spjd	{ "online",	zpool_do_online,	HELP_ONLINE		},
173168404Spjd	{ "offline",	zpool_do_offline,	HELP_OFFLINE		},
174168404Spjd	{ "clear",	zpool_do_clear,		HELP_CLEAR		},
175236155Smm	{ "reopen",	zpool_do_reopen,	HELP_REOPEN		},
176168404Spjd	{ NULL },
177168404Spjd	{ "attach",	zpool_do_attach,	HELP_ATTACH		},
178168404Spjd	{ "detach",	zpool_do_detach,	HELP_DETACH		},
179168404Spjd	{ "replace",	zpool_do_replace,	HELP_REPLACE		},
180219089Spjd	{ "split",	zpool_do_split,		HELP_SPLIT		},
181168404Spjd	{ NULL },
182168404Spjd	{ "scrub",	zpool_do_scrub,		HELP_SCRUB		},
183168404Spjd	{ NULL },
184168404Spjd	{ "import",	zpool_do_import,	HELP_IMPORT		},
185168404Spjd	{ "export",	zpool_do_export,	HELP_EXPORT		},
186168404Spjd	{ "upgrade",	zpool_do_upgrade,	HELP_UPGRADE		},
187228103Smm	{ "reguid",	zpool_do_reguid,	HELP_REGUID		},
188168404Spjd	{ NULL },
189168404Spjd	{ "history",	zpool_do_history,	HELP_HISTORY		},
190168404Spjd	{ "get",	zpool_do_get,		HELP_GET		},
191168404Spjd	{ "set",	zpool_do_set,		HELP_SET		},
192168404Spjd};
193168404Spjd
194168404Spjd#define	NCOMMAND	(sizeof (command_table) / sizeof (command_table[0]))
195168404Spjd
196248571Smmstatic zpool_command_t *current_command;
197185029Spjdstatic char history_str[HIS_MAX_RECORD_LEN];
198248571Smmstatic boolean_t log_history = B_TRUE;
199219089Spjdstatic uint_t timestamp_fmt = NODATE;
200219089Spjd
201168404Spjdstatic const char *
202168404Spjdget_usage(zpool_help_t idx) {
203168404Spjd	switch (idx) {
204168404Spjd	case HELP_ADD:
205168404Spjd		return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
206168404Spjd	case HELP_ATTACH:
207168404Spjd		return (gettext("\tattach [-f] <pool> <device> "
208185029Spjd		    "<new-device>\n"));
209168404Spjd	case HELP_CLEAR:
210219089Spjd		return (gettext("\tclear [-nF] <pool> [device]\n"));
211168404Spjd	case HELP_CREATE:
212236884Smm		return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
213185029Spjd		    "\t    [-O file-system-property=value] ... \n"
214185029Spjd		    "\t    [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
215168404Spjd	case HELP_DESTROY:
216168404Spjd		return (gettext("\tdestroy [-f] <pool>\n"));
217168404Spjd	case HELP_DETACH:
218168404Spjd		return (gettext("\tdetach <pool> <device>\n"));
219168404Spjd	case HELP_EXPORT:
220168404Spjd		return (gettext("\texport [-f] <pool> ...\n"));
221168404Spjd	case HELP_HISTORY:
222185029Spjd		return (gettext("\thistory [-il] [<pool>] ...\n"));
223168404Spjd	case HELP_IMPORT:
224168404Spjd		return (gettext("\timport [-d dir] [-D]\n"
225219089Spjd		    "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
226185029Spjd		    "\timport [-o mntopts] [-o property=value] ... \n"
227219089Spjd		    "\t    [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
228219089Spjd		    "[-R root] [-F [-n]] -a\n"
229185029Spjd		    "\timport [-o mntopts] [-o property=value] ... \n"
230219089Spjd		    "\t    [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
231219089Spjd		    "[-R root] [-F [-n]]\n"
232219089Spjd		    "\t    <pool | id> [newpool]\n"));
233168404Spjd	case HELP_IOSTAT:
234219089Spjd		return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
235168404Spjd		    "[count]]\n"));
236224171Sgibbs	case HELP_LABELCLEAR:
237224171Sgibbs		return (gettext("\tlabelclear [-f] <vdev>\n"));
238168404Spjd	case HELP_LIST:
239263889Sdelphij		return (gettext("\tlist [-Hpv] [-o property[,...]] "
240219089Spjd		    "[-T d|u] [pool] ... [interval [count]]\n"));
241168404Spjd	case HELP_OFFLINE:
242168404Spjd		return (gettext("\toffline [-t] <pool> <device> ...\n"));
243168404Spjd	case HELP_ONLINE:
244228020Smm		return (gettext("\tonline [-e] <pool> <device> ...\n"));
245168404Spjd	case HELP_REPLACE:
246168404Spjd		return (gettext("\treplace [-f] <pool> <device> "
247185029Spjd		    "[new-device]\n"));
248168404Spjd	case HELP_REMOVE:
249185029Spjd		return (gettext("\tremove <pool> <device> ...\n"));
250236155Smm	case HELP_REOPEN:
251260138Sdelphij		return (gettext("\treopen <pool>\n"));
252168404Spjd	case HELP_SCRUB:
253168404Spjd		return (gettext("\tscrub [-s] <pool> ...\n"));
254168404Spjd	case HELP_STATUS:
255219089Spjd		return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
256219089Spjd		    "[count]]\n"));
257168404Spjd	case HELP_UPGRADE:
258228020Smm		return (gettext("\tupgrade [-v]\n"
259185029Spjd		    "\tupgrade [-V version] <-a | pool ...>\n"));
260168404Spjd	case HELP_GET:
261263889Sdelphij		return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
262263889Sdelphij		    "<\"all\" | property[,...]> <pool> ...\n"));
263168404Spjd	case HELP_SET:
264168404Spjd		return (gettext("\tset <property=value> <pool> \n"));
265219089Spjd	case HELP_SPLIT:
266219089Spjd		return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
267219089Spjd		    "\t    [-o property=value] <pool> <newpool> "
268219089Spjd		    "[<device> ...]\n"));
269228103Smm	case HELP_REGUID:
270228103Smm		return (gettext("\treguid <pool>\n"));
271168404Spjd	}
272168404Spjd
273168404Spjd	abort();
274168404Spjd	/* NOTREACHED */
275168404Spjd}
276168404Spjd
277168404Spjd
278168404Spjd/*
279168404Spjd * Callback routine that will print out a pool property value.
280168404Spjd */
281185029Spjdstatic int
282185029Spjdprint_prop_cb(int prop, void *cb)
283168404Spjd{
284168404Spjd	FILE *fp = cb;
285168404Spjd
286219089Spjd	(void) fprintf(fp, "\t%-15s  ", zpool_prop_to_name(prop));
287168404Spjd
288185029Spjd	if (zpool_prop_readonly(prop))
289185029Spjd		(void) fprintf(fp, "  NO   ");
290185029Spjd	else
291219089Spjd		(void) fprintf(fp, " YES   ");
292185029Spjd
293168404Spjd	if (zpool_prop_values(prop) == NULL)
294168404Spjd		(void) fprintf(fp, "-\n");
295168404Spjd	else
296168404Spjd		(void) fprintf(fp, "%s\n", zpool_prop_values(prop));
297168404Spjd
298185029Spjd	return (ZPROP_CONT);
299168404Spjd}
300168404Spjd
301168404Spjd/*
302168404Spjd * Display usage message.  If we're inside a command, display only the usage for
303168404Spjd * that command.  Otherwise, iterate over the entire command table and display
304168404Spjd * a complete usage message.
305168404Spjd */
306168404Spjdvoid
307168404Spjdusage(boolean_t requested)
308168404Spjd{
309168404Spjd	FILE *fp = requested ? stdout : stderr;
310168404Spjd
311168404Spjd	if (current_command == NULL) {
312168404Spjd		int i;
313168404Spjd
314168404Spjd		(void) fprintf(fp, gettext("usage: zpool command args ...\n"));
315168404Spjd		(void) fprintf(fp,
316168404Spjd		    gettext("where 'command' is one of the following:\n\n"));
317168404Spjd
318168404Spjd		for (i = 0; i < NCOMMAND; i++) {
319168404Spjd			if (command_table[i].name == NULL)
320168404Spjd				(void) fprintf(fp, "\n");
321168404Spjd			else
322168404Spjd				(void) fprintf(fp, "%s",
323168404Spjd				    get_usage(command_table[i].usage));
324168404Spjd		}
325168404Spjd	} else {
326168404Spjd		(void) fprintf(fp, gettext("usage:\n"));
327168404Spjd		(void) fprintf(fp, "%s", get_usage(current_command->usage));
328168404Spjd	}
329168404Spjd
330168404Spjd	if (current_command != NULL &&
331168404Spjd	    ((strcmp(current_command->name, "set") == 0) ||
332185029Spjd	    (strcmp(current_command->name, "get") == 0) ||
333185029Spjd	    (strcmp(current_command->name, "list") == 0))) {
334168404Spjd
335168404Spjd		(void) fprintf(fp,
336168404Spjd		    gettext("\nthe following properties are supported:\n"));
337168404Spjd
338219089Spjd		(void) fprintf(fp, "\n\t%-15s  %s   %s\n\n",
339185029Spjd		    "PROPERTY", "EDIT", "VALUES");
340168404Spjd
341168404Spjd		/* Iterate over all properties */
342185029Spjd		(void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
343185029Spjd		    ZFS_TYPE_POOL);
344236884Smm
345236884Smm		(void) fprintf(fp, "\t%-15s   ", "feature@...");
346236884Smm		(void) fprintf(fp, "YES   disabled | enabled | active\n");
347236884Smm
348236884Smm		(void) fprintf(fp, gettext("\nThe feature@ properties must be "
349243014Smm		    "appended with a feature name.\nSee zpool-features(7).\n"));
350168404Spjd	}
351168404Spjd
352168404Spjd	/*
353168404Spjd	 * See comments at end of main().
354168404Spjd	 */
355168404Spjd	if (getenv("ZFS_ABORT") != NULL) {
356168404Spjd		(void) printf("dumping core by request\n");
357168404Spjd		abort();
358168404Spjd	}
359168404Spjd
360168404Spjd	exit(requested ? 0 : 2);
361168404Spjd}
362168404Spjd
363168404Spjdvoid
364185029Spjdprint_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
365185029Spjd    boolean_t print_logs)
366168404Spjd{
367168404Spjd	nvlist_t **child;
368168404Spjd	uint_t c, children;
369168404Spjd	char *vname;
370168404Spjd
371168404Spjd	if (name != NULL)
372168404Spjd		(void) printf("\t%*s%s\n", indent, "", name);
373168404Spjd
374168404Spjd	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
375168404Spjd	    &child, &children) != 0)
376168404Spjd		return;
377168404Spjd
378168404Spjd	for (c = 0; c < children; c++) {
379185029Spjd		uint64_t is_log = B_FALSE;
380185029Spjd
381185029Spjd		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
382185029Spjd		    &is_log);
383185029Spjd		if ((is_log && !print_logs) || (!is_log && print_logs))
384185029Spjd			continue;
385185029Spjd
386219089Spjd		vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
387185029Spjd		print_vdev_tree(zhp, vname, child[c], indent + 2,
388185029Spjd		    B_FALSE);
389168404Spjd		free(vname);
390168404Spjd	}
391168404Spjd}
392168404Spjd
393238926Smmstatic boolean_t
394238926Smmprop_list_contains_feature(nvlist_t *proplist)
395238926Smm{
396238926Smm	nvpair_t *nvp;
397238926Smm	for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
398238926Smm	    nvp = nvlist_next_nvpair(proplist, nvp)) {
399238926Smm		if (zpool_prop_feature(nvpair_name(nvp)))
400238926Smm			return (B_TRUE);
401238926Smm	}
402238926Smm	return (B_FALSE);
403238926Smm}
404238926Smm
405168404Spjd/*
406185029Spjd * Add a property pair (name, string-value) into a property nvlist.
407185029Spjd */
408185029Spjdstatic int
409185029Spjdadd_prop_list(const char *propname, char *propval, nvlist_t **props,
410185029Spjd    boolean_t poolprop)
411185029Spjd{
412185029Spjd	zpool_prop_t prop = ZPROP_INVAL;
413185029Spjd	zfs_prop_t fprop;
414185029Spjd	nvlist_t *proplist;
415185029Spjd	const char *normnm;
416185029Spjd	char *strval;
417185029Spjd
418185029Spjd	if (*props == NULL &&
419185029Spjd	    nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
420185029Spjd		(void) fprintf(stderr,
421185029Spjd		    gettext("internal error: out of memory\n"));
422185029Spjd		return (1);
423185029Spjd	}
424185029Spjd
425185029Spjd	proplist = *props;
426185029Spjd
427185029Spjd	if (poolprop) {
428238926Smm		const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
429238926Smm
430236884Smm		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
431236884Smm		    !zpool_prop_feature(propname)) {
432185029Spjd			(void) fprintf(stderr, gettext("property '%s' is "
433185029Spjd			    "not a valid pool property\n"), propname);
434185029Spjd			return (2);
435185029Spjd		}
436238926Smm
437238926Smm		/*
438238926Smm		 * feature@ properties and version should not be specified
439238926Smm		 * at the same time.
440238926Smm		 */
441238926Smm		if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) &&
442238926Smm		    nvlist_exists(proplist, vname)) ||
443238926Smm		    (prop == ZPOOL_PROP_VERSION &&
444238926Smm		    prop_list_contains_feature(proplist))) {
445238926Smm			(void) fprintf(stderr, gettext("'feature@' and "
446238926Smm			    "'version' properties cannot be specified "
447238926Smm			    "together\n"));
448238926Smm			return (2);
449238926Smm		}
450238926Smm
451238926Smm
452236884Smm		if (zpool_prop_feature(propname))
453236884Smm			normnm = propname;
454236884Smm		else
455236884Smm			normnm = zpool_prop_to_name(prop);
456185029Spjd	} else {
457209962Smm		if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
458209962Smm			normnm = zfs_prop_to_name(fprop);
459209962Smm		} else {
460209962Smm			normnm = propname;
461185029Spjd		}
462185029Spjd	}
463185029Spjd
464185029Spjd	if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
465185029Spjd	    prop != ZPOOL_PROP_CACHEFILE) {
466185029Spjd		(void) fprintf(stderr, gettext("property '%s' "
467185029Spjd		    "specified multiple times\n"), propname);
468185029Spjd		return (2);
469185029Spjd	}
470185029Spjd
471185029Spjd	if (nvlist_add_string(proplist, normnm, propval) != 0) {
472185029Spjd		(void) fprintf(stderr, gettext("internal "
473185029Spjd		    "error: out of memory\n"));
474185029Spjd		return (1);
475185029Spjd	}
476185029Spjd
477185029Spjd	return (0);
478185029Spjd}
479185029Spjd
480185029Spjd/*
481168404Spjd * zpool add [-fn] <pool> <vdev> ...
482168404Spjd *
483168404Spjd *	-f	Force addition of devices, even if they appear in use
484168404Spjd *	-n	Do not add the devices, but display the resulting layout if
485168404Spjd *		they were to be added.
486168404Spjd *
487168404Spjd * Adds the given vdevs to 'pool'.  As with create, the bulk of this work is
488168404Spjd * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
489168404Spjd * libzfs.
490168404Spjd */
491168404Spjdint
492168404Spjdzpool_do_add(int argc, char **argv)
493168404Spjd{
494168404Spjd	boolean_t force = B_FALSE;
495168404Spjd	boolean_t dryrun = B_FALSE;
496168404Spjd	int c;
497168404Spjd	nvlist_t *nvroot;
498168404Spjd	char *poolname;
499168404Spjd	int ret;
500168404Spjd	zpool_handle_t *zhp;
501168404Spjd	nvlist_t *config;
502168404Spjd
503168404Spjd	/* check options */
504168404Spjd	while ((c = getopt(argc, argv, "fn")) != -1) {
505168404Spjd		switch (c) {
506168404Spjd		case 'f':
507168404Spjd			force = B_TRUE;
508168404Spjd			break;
509168404Spjd		case 'n':
510168404Spjd			dryrun = B_TRUE;
511168404Spjd			break;
512168404Spjd		case '?':
513168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
514168404Spjd			    optopt);
515168404Spjd			usage(B_FALSE);
516168404Spjd		}
517168404Spjd	}
518168404Spjd
519168404Spjd	argc -= optind;
520168404Spjd	argv += optind;
521168404Spjd
522168404Spjd	/* get pool name and check number of arguments */
523168404Spjd	if (argc < 1) {
524168404Spjd		(void) fprintf(stderr, gettext("missing pool name argument\n"));
525168404Spjd		usage(B_FALSE);
526168404Spjd	}
527168404Spjd	if (argc < 2) {
528168404Spjd		(void) fprintf(stderr, gettext("missing vdev specification\n"));
529168404Spjd		usage(B_FALSE);
530168404Spjd	}
531168404Spjd
532168404Spjd	poolname = argv[0];
533168404Spjd
534168404Spjd	argc--;
535168404Spjd	argv++;
536168404Spjd
537168404Spjd	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
538168404Spjd		return (1);
539168404Spjd
540168404Spjd	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
541168404Spjd		(void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
542168404Spjd		    poolname);
543168404Spjd		zpool_close(zhp);
544168404Spjd		return (1);
545168404Spjd	}
546168404Spjd
547168404Spjd	/* pass off to get_vdev_spec for processing */
548185029Spjd	nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
549185029Spjd	    argc, argv);
550168404Spjd	if (nvroot == NULL) {
551168404Spjd		zpool_close(zhp);
552168404Spjd		return (1);
553168404Spjd	}
554168404Spjd
555168404Spjd	if (dryrun) {
556168404Spjd		nvlist_t *poolnvroot;
557168404Spjd
558168404Spjd		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
559168404Spjd		    &poolnvroot) == 0);
560168404Spjd
561168404Spjd		(void) printf(gettext("would update '%s' to the following "
562168404Spjd		    "configuration:\n"), zpool_get_name(zhp));
563168404Spjd
564185029Spjd		/* print original main pool and new tree */
565185029Spjd		print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
566185029Spjd		print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
567168404Spjd
568185029Spjd		/* Do the same for the logs */
569185029Spjd		if (num_logs(poolnvroot) > 0) {
570185029Spjd			print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
571185029Spjd			print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
572185029Spjd		} else if (num_logs(nvroot) > 0) {
573185029Spjd			print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
574185029Spjd		}
575185029Spjd
576168404Spjd		ret = 0;
577168404Spjd	} else {
578168404Spjd		ret = (zpool_add(zhp, nvroot) != 0);
579168404Spjd	}
580168404Spjd
581168404Spjd	nvlist_free(nvroot);
582168404Spjd	zpool_close(zhp);
583168404Spjd
584168404Spjd	return (ret);
585168404Spjd}
586168404Spjd
587168404Spjd/*
588219089Spjd * zpool remove  <pool> <vdev> ...
589168404Spjd *
590219089Spjd * Removes the given vdev from the pool.  Currently, this supports removing
591219089Spjd * spares, cache, and log devices from the pool.
592168404Spjd */
593168404Spjdint
594168404Spjdzpool_do_remove(int argc, char **argv)
595168404Spjd{
596168404Spjd	char *poolname;
597185029Spjd	int i, ret = 0;
598168404Spjd	zpool_handle_t *zhp;
599168404Spjd
600168404Spjd	argc--;
601168404Spjd	argv++;
602168404Spjd
603168404Spjd	/* get pool name and check number of arguments */
604168404Spjd	if (argc < 1) {
605168404Spjd		(void) fprintf(stderr, gettext("missing pool name argument\n"));
606168404Spjd		usage(B_FALSE);
607168404Spjd	}
608168404Spjd	if (argc < 2) {
609168404Spjd		(void) fprintf(stderr, gettext("missing device\n"));
610168404Spjd		usage(B_FALSE);
611168404Spjd	}
612168404Spjd
613168404Spjd	poolname = argv[0];
614168404Spjd
615168404Spjd	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
616168404Spjd		return (1);
617168404Spjd
618185029Spjd	for (i = 1; i < argc; i++) {
619185029Spjd		if (zpool_vdev_remove(zhp, argv[i]) != 0)
620185029Spjd			ret = 1;
621168404Spjd	}
622168404Spjd
623168404Spjd	return (ret);
624168404Spjd}
625168404Spjd
626168404Spjd/*
627224171Sgibbs * zpool labelclear <vdev>
628224171Sgibbs *
629224171Sgibbs * Verifies that the vdev is not active and zeros out the label information
630224171Sgibbs * on the device.
631224171Sgibbs */
632224171Sgibbsint
633224171Sgibbszpool_do_labelclear(int argc, char **argv)
634224171Sgibbs{
635224171Sgibbs	char *vdev, *name;
636224171Sgibbs	int c, fd = -1, ret = 0;
637224171Sgibbs	pool_state_t state;
638224171Sgibbs	boolean_t inuse = B_FALSE;
639224171Sgibbs	boolean_t force = B_FALSE;
640224171Sgibbs
641224171Sgibbs	/* check options */
642224171Sgibbs	while ((c = getopt(argc, argv, "f")) != -1) {
643224171Sgibbs		switch (c) {
644224171Sgibbs		case 'f':
645224171Sgibbs			force = B_TRUE;
646224171Sgibbs			break;
647224171Sgibbs		default:
648224171Sgibbs			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
649224171Sgibbs			    optopt);
650224171Sgibbs			usage(B_FALSE);
651224171Sgibbs		}
652224171Sgibbs	}
653224171Sgibbs
654224171Sgibbs	argc -= optind;
655224171Sgibbs	argv += optind;
656224171Sgibbs
657224171Sgibbs	/* get vdev name */
658224171Sgibbs	if (argc < 1) {
659224171Sgibbs		(void) fprintf(stderr, gettext("missing vdev device name\n"));
660224171Sgibbs		usage(B_FALSE);
661224171Sgibbs	}
662224171Sgibbs
663224171Sgibbs	vdev = argv[0];
664224171Sgibbs	if ((fd = open(vdev, O_RDWR)) < 0) {
665224171Sgibbs		(void) fprintf(stderr, gettext("Unable to open %s\n"), vdev);
666224171Sgibbs		return (B_FALSE);
667224171Sgibbs	}
668224171Sgibbs
669224171Sgibbs	name = NULL;
670224171Sgibbs	if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0) {
671224171Sgibbs		if (force)
672224171Sgibbs			goto wipe_label;
673224171Sgibbs
674224171Sgibbs		(void) fprintf(stderr,
675224171Sgibbs		    gettext("Unable to determine pool state for %s\n"
676224171Sgibbs		    "Use -f to force the clearing any label data\n"), vdev);
677224171Sgibbs
678224171Sgibbs		return (1);
679224171Sgibbs	}
680224171Sgibbs
681224171Sgibbs	if (inuse) {
682224171Sgibbs		switch (state) {
683224171Sgibbs		default:
684224171Sgibbs		case POOL_STATE_ACTIVE:
685224171Sgibbs		case POOL_STATE_SPARE:
686224171Sgibbs		case POOL_STATE_L2CACHE:
687224171Sgibbs			(void) fprintf(stderr,
688224171Sgibbsgettext("labelclear operation failed.\n"
689224171Sgibbs	"\tVdev %s is a member (%s), of pool \"%s\".\n"
690224171Sgibbs	"\tTo remove label information from this device, export or destroy\n"
691224171Sgibbs	"\tthe pool, or remove %s from the configuration of this pool\n"
692224171Sgibbs	"\tand retry the labelclear operation\n"),
693224171Sgibbs			    vdev, zpool_pool_state_to_name(state), name, vdev);
694224171Sgibbs			ret = 1;
695224171Sgibbs			goto errout;
696224171Sgibbs
697224171Sgibbs		case POOL_STATE_EXPORTED:
698224171Sgibbs			if (force)
699224171Sgibbs				break;
700224171Sgibbs
701224171Sgibbs			(void) fprintf(stderr,
702224171Sgibbsgettext("labelclear operation failed.\n"
703224171Sgibbs	"\tVdev %s is a member of the exported pool \"%s\".\n"
704224171Sgibbs	"\tUse \"zpool labelclear -f %s\" to force the removal of label\n"
705224171Sgibbs	"\tinformation.\n"),
706224171Sgibbs			    vdev, name, vdev);
707224171Sgibbs			ret = 1;
708224171Sgibbs			goto errout;
709224171Sgibbs
710224171Sgibbs		case POOL_STATE_POTENTIALLY_ACTIVE:
711224171Sgibbs			if (force)
712224171Sgibbs				break;
713224171Sgibbs
714224171Sgibbs			(void) fprintf(stderr,
715224171Sgibbsgettext("labelclear operation failed.\n"
716224171Sgibbs	"\tVdev %s is a member of the pool \"%s\".\n"
717224171Sgibbs	"\tThis pool is unknown to this system, but may be active on\n"
718224171Sgibbs	"\tanother system. Use \'zpool labelclear -f %s\' to force the\n"
719224171Sgibbs	"\tremoval of label information.\n"),
720224171Sgibbs			    vdev, name, vdev);
721224171Sgibbs			ret = 1;
722224171Sgibbs			goto errout;
723224171Sgibbs
724224171Sgibbs		case POOL_STATE_DESTROYED:
725224171Sgibbs			/* inuse should never be set for a destoryed pool... */
726224171Sgibbs			break;
727224171Sgibbs		}
728224171Sgibbs	}
729224171Sgibbs
730224171Sgibbswipe_label:
731224171Sgibbs	if (zpool_clear_label(fd) != 0) {
732224171Sgibbs		(void) fprintf(stderr,
733224171Sgibbs		    gettext("Label clear failed on vdev %s\n"), vdev);
734224171Sgibbs		ret = 1;
735224171Sgibbs	}
736224171Sgibbs
737224171Sgibbserrout:
738224171Sgibbs	close(fd);
739224171Sgibbs	if (name != NULL)
740224171Sgibbs		free(name);
741224171Sgibbs
742224171Sgibbs	return (ret);
743224171Sgibbs}
744224171Sgibbs
745224171Sgibbs/*
746236884Smm * zpool create [-fnd] [-o property=value] ...
747185029Spjd *		[-O file-system-property=value] ...
748185029Spjd *		[-R root] [-m mountpoint] <pool> <dev> ...
749168404Spjd *
750168404Spjd *	-f	Force creation, even if devices appear in use
751168404Spjd *	-n	Do not create the pool, but display the resulting layout if it
752168404Spjd *		were to be created.
753168404Spjd *      -R	Create a pool under an alternate root
754168404Spjd *      -m	Set default mountpoint for the root dataset.  By default it's
755236884Smm *		'/<pool>'
756185029Spjd *	-o	Set property=value.
757236884Smm *	-d	Don't automatically enable all supported pool features
758236884Smm *		(individual features can be enabled with -o).
759185029Spjd *	-O	Set fsproperty=value in the pool's root file system
760168404Spjd *
761168404Spjd * Creates the named pool according to the given vdev specification.  The
762168404Spjd * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c.  Once
763168404Spjd * we get the nvlist back from get_vdev_spec(), we either print out the contents
764168404Spjd * (if '-n' was specified), or pass it to libzfs to do the creation.
765168404Spjd */
766168404Spjdint
767168404Spjdzpool_do_create(int argc, char **argv)
768168404Spjd{
769168404Spjd	boolean_t force = B_FALSE;
770168404Spjd	boolean_t dryrun = B_FALSE;
771236884Smm	boolean_t enable_all_pool_feat = B_TRUE;
772168404Spjd	int c;
773185029Spjd	nvlist_t *nvroot = NULL;
774168404Spjd	char *poolname;
775185029Spjd	int ret = 1;
776168404Spjd	char *altroot = NULL;
777168404Spjd	char *mountpoint = NULL;
778185029Spjd	nvlist_t *fsprops = NULL;
779185029Spjd	nvlist_t *props = NULL;
780185029Spjd	char *propval;
781168404Spjd
782168404Spjd	/* check options */
783236884Smm	while ((c = getopt(argc, argv, ":fndR:m:o:O:")) != -1) {
784168404Spjd		switch (c) {
785168404Spjd		case 'f':
786168404Spjd			force = B_TRUE;
787168404Spjd			break;
788168404Spjd		case 'n':
789168404Spjd			dryrun = B_TRUE;
790168404Spjd			break;
791236884Smm		case 'd':
792236884Smm			enable_all_pool_feat = B_FALSE;
793236884Smm			break;
794168404Spjd		case 'R':
795168404Spjd			altroot = optarg;
796185029Spjd			if (add_prop_list(zpool_prop_to_name(
797185029Spjd			    ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
798185029Spjd				goto errout;
799185029Spjd			if (nvlist_lookup_string(props,
800185029Spjd			    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
801185029Spjd			    &propval) == 0)
802185029Spjd				break;
803185029Spjd			if (add_prop_list(zpool_prop_to_name(
804185029Spjd			    ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
805185029Spjd				goto errout;
806168404Spjd			break;
807168404Spjd		case 'm':
808251634Sdelphij			/* Equivalent to -O mountpoint=optarg */
809168404Spjd			mountpoint = optarg;
810168404Spjd			break;
811185029Spjd		case 'o':
812185029Spjd			if ((propval = strchr(optarg, '=')) == NULL) {
813185029Spjd				(void) fprintf(stderr, gettext("missing "
814185029Spjd				    "'=' for -o option\n"));
815185029Spjd				goto errout;
816185029Spjd			}
817185029Spjd			*propval = '\0';
818185029Spjd			propval++;
819185029Spjd
820185029Spjd			if (add_prop_list(optarg, propval, &props, B_TRUE))
821185029Spjd				goto errout;
822236884Smm
823236884Smm			/*
824236884Smm			 * If the user is creating a pool that doesn't support
825236884Smm			 * feature flags, don't enable any features.
826236884Smm			 */
827236884Smm			if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
828236884Smm				char *end;
829236884Smm				u_longlong_t ver;
830236884Smm
831236884Smm				ver = strtoull(propval, &end, 10);
832236884Smm				if (*end == '\0' &&
833236884Smm				    ver < SPA_VERSION_FEATURES) {
834236884Smm					enable_all_pool_feat = B_FALSE;
835236884Smm				}
836236884Smm			}
837185029Spjd			break;
838185029Spjd		case 'O':
839185029Spjd			if ((propval = strchr(optarg, '=')) == NULL) {
840185029Spjd				(void) fprintf(stderr, gettext("missing "
841185029Spjd				    "'=' for -O option\n"));
842185029Spjd				goto errout;
843185029Spjd			}
844185029Spjd			*propval = '\0';
845185029Spjd			propval++;
846185029Spjd
847251634Sdelphij			/*
848251634Sdelphij			 * Mountpoints are checked and then added later.
849251634Sdelphij			 * Uniquely among properties, they can be specified
850251634Sdelphij			 * more than once, to avoid conflict with -m.
851251634Sdelphij			 */
852251634Sdelphij			if (0 == strcmp(optarg,
853251634Sdelphij			    zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
854251634Sdelphij				mountpoint = propval;
855251634Sdelphij			} else if (add_prop_list(optarg, propval, &fsprops,
856251634Sdelphij			    B_FALSE)) {
857185029Spjd				goto errout;
858251634Sdelphij			}
859185029Spjd			break;
860168404Spjd		case ':':
861168404Spjd			(void) fprintf(stderr, gettext("missing argument for "
862168404Spjd			    "'%c' option\n"), optopt);
863185029Spjd			goto badusage;
864168404Spjd		case '?':
865168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
866168404Spjd			    optopt);
867185029Spjd			goto badusage;
868168404Spjd		}
869168404Spjd	}
870168404Spjd
871168404Spjd	argc -= optind;
872168404Spjd	argv += optind;
873168404Spjd
874168404Spjd	/* get pool name and check number of arguments */
875168404Spjd	if (argc < 1) {
876168404Spjd		(void) fprintf(stderr, gettext("missing pool name argument\n"));
877185029Spjd		goto badusage;
878168404Spjd	}
879168404Spjd	if (argc < 2) {
880168404Spjd		(void) fprintf(stderr, gettext("missing vdev specification\n"));
881185029Spjd		goto badusage;
882168404Spjd	}
883168404Spjd
884168404Spjd	poolname = argv[0];
885168404Spjd
886168404Spjd	/*
887168404Spjd	 * As a special case, check for use of '/' in the name, and direct the
888168404Spjd	 * user to use 'zfs create' instead.
889168404Spjd	 */
890168404Spjd	if (strchr(poolname, '/') != NULL) {
891168404Spjd		(void) fprintf(stderr, gettext("cannot create '%s': invalid "
892168404Spjd		    "character '/' in pool name\n"), poolname);
893168404Spjd		(void) fprintf(stderr, gettext("use 'zfs create' to "
894168404Spjd		    "create a dataset\n"));
895185029Spjd		goto errout;
896168404Spjd	}
897168404Spjd
898168404Spjd	/* pass off to get_vdev_spec for bulk processing */
899185029Spjd	nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
900185029Spjd	    argc - 1, argv + 1);
901168404Spjd	if (nvroot == NULL)
902185029Spjd		goto errout;
903168404Spjd
904168404Spjd	/* make_root_vdev() allows 0 toplevel children if there are spares */
905185029Spjd	if (!zfs_allocatable_devs(nvroot)) {
906168404Spjd		(void) fprintf(stderr, gettext("invalid vdev "
907168404Spjd		    "specification: at least one toplevel vdev must be "
908168404Spjd		    "specified\n"));
909185029Spjd		goto errout;
910168404Spjd	}
911168404Spjd
912168404Spjd	if (altroot != NULL && altroot[0] != '/') {
913168404Spjd		(void) fprintf(stderr, gettext("invalid alternate root '%s': "
914168404Spjd		    "must be an absolute path\n"), altroot);
915185029Spjd		goto errout;
916168404Spjd	}
917168404Spjd
918168404Spjd	/*
919168404Spjd	 * Check the validity of the mountpoint and direct the user to use the
920168404Spjd	 * '-m' mountpoint option if it looks like its in use.
921244857Spjd	 * Ignore the checks if the '-f' option is given.
922168404Spjd	 */
923244857Spjd	if (!force && (mountpoint == NULL ||
924168404Spjd	    (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
925244857Spjd	    strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0))) {
926168404Spjd		char buf[MAXPATHLEN];
927185029Spjd		DIR *dirp;
928168404Spjd
929168404Spjd		if (mountpoint && mountpoint[0] != '/') {
930168404Spjd			(void) fprintf(stderr, gettext("invalid mountpoint "
931168404Spjd			    "'%s': must be an absolute path, 'legacy', or "
932168404Spjd			    "'none'\n"), mountpoint);
933185029Spjd			goto errout;
934168404Spjd		}
935168404Spjd
936168404Spjd		if (mountpoint == NULL) {
937168404Spjd			if (altroot != NULL)
938168404Spjd				(void) snprintf(buf, sizeof (buf), "%s/%s",
939168404Spjd				    altroot, poolname);
940168404Spjd			else
941168404Spjd				(void) snprintf(buf, sizeof (buf), "/%s",
942168404Spjd				    poolname);
943168404Spjd		} else {
944168404Spjd			if (altroot != NULL)
945168404Spjd				(void) snprintf(buf, sizeof (buf), "%s%s",
946168404Spjd				    altroot, mountpoint);
947168404Spjd			else
948168404Spjd				(void) snprintf(buf, sizeof (buf), "%s",
949168404Spjd				    mountpoint);
950168404Spjd		}
951168404Spjd
952185029Spjd		if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
953185029Spjd			(void) fprintf(stderr, gettext("mountpoint '%s' : "
954185029Spjd			    "%s\n"), buf, strerror(errno));
955185029Spjd			(void) fprintf(stderr, gettext("use '-m' "
956185029Spjd			    "option to provide a different default\n"));
957185029Spjd			goto errout;
958185029Spjd		} else if (dirp) {
959185029Spjd			int count = 0;
960185029Spjd
961185029Spjd			while (count < 3 && readdir(dirp) != NULL)
962185029Spjd				count++;
963185029Spjd			(void) closedir(dirp);
964185029Spjd
965185029Spjd			if (count > 2) {
966168404Spjd				(void) fprintf(stderr, gettext("mountpoint "
967168404Spjd				    "'%s' exists and is not empty\n"), buf);
968185029Spjd				(void) fprintf(stderr, gettext("use '-m' "
969185029Spjd				    "option to provide a "
970185029Spjd				    "different default\n"));
971185029Spjd				goto errout;
972185029Spjd			}
973168404Spjd		}
974168404Spjd	}
975168404Spjd
976251634Sdelphij	/*
977251634Sdelphij	 * Now that the mountpoint's validity has been checked, ensure that
978251634Sdelphij	 * the property is set appropriately prior to creating the pool.
979251634Sdelphij	 */
980251634Sdelphij	if (mountpoint != NULL) {
981251634Sdelphij		ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
982251634Sdelphij		    mountpoint, &fsprops, B_FALSE);
983251634Sdelphij		if (ret != 0)
984251634Sdelphij			goto errout;
985251634Sdelphij	}
986251634Sdelphij
987251634Sdelphij	ret = 1;
988168404Spjd	if (dryrun) {
989168404Spjd		/*
990168404Spjd		 * For a dry run invocation, print out a basic message and run
991168404Spjd		 * through all the vdevs in the list and print out in an
992168404Spjd		 * appropriate hierarchy.
993168404Spjd		 */
994168404Spjd		(void) printf(gettext("would create '%s' with the "
995168404Spjd		    "following layout:\n\n"), poolname);
996168404Spjd
997185029Spjd		print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
998185029Spjd		if (num_logs(nvroot) > 0)
999185029Spjd			print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
1000168404Spjd
1001168404Spjd		ret = 0;
1002168404Spjd	} else {
1003168404Spjd		/*
1004168404Spjd		 * Hand off to libzfs.
1005168404Spjd		 */
1006236884Smm		if (enable_all_pool_feat) {
1007259813Sdelphij			spa_feature_t i;
1008236884Smm			for (i = 0; i < SPA_FEATURES; i++) {
1009236884Smm				char propname[MAXPATHLEN];
1010236884Smm				zfeature_info_t *feat = &spa_feature_table[i];
1011236884Smm
1012236884Smm				(void) snprintf(propname, sizeof (propname),
1013236884Smm				    "feature@%s", feat->fi_uname);
1014236884Smm
1015236884Smm				/*
1016236884Smm				 * Skip feature if user specified it manually
1017236884Smm				 * on the command line.
1018236884Smm				 */
1019236884Smm				if (nvlist_exists(props, propname))
1020236884Smm					continue;
1021236884Smm
1022251634Sdelphij				ret = add_prop_list(propname,
1023251634Sdelphij				    ZFS_FEATURE_ENABLED, &props, B_TRUE);
1024251634Sdelphij				if (ret != 0)
1025236884Smm					goto errout;
1026236884Smm			}
1027236884Smm		}
1028251634Sdelphij
1029251634Sdelphij		ret = 1;
1030185029Spjd		if (zpool_create(g_zfs, poolname,
1031185029Spjd		    nvroot, props, fsprops) == 0) {
1032168404Spjd			zfs_handle_t *pool = zfs_open(g_zfs, poolname,
1033168404Spjd			    ZFS_TYPE_FILESYSTEM);
1034168404Spjd			if (pool != NULL) {
1035168404Spjd				if (zfs_mount(pool, NULL, 0) == 0)
1036185029Spjd					ret = zfs_shareall(pool);
1037168404Spjd				zfs_close(pool);
1038168404Spjd			}
1039168404Spjd		} else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1040168404Spjd			(void) fprintf(stderr, gettext("pool name may have "
1041168404Spjd			    "been omitted\n"));
1042168404Spjd		}
1043168404Spjd	}
1044168404Spjd
1045185029Spjderrout:
1046168404Spjd	nvlist_free(nvroot);
1047185029Spjd	nvlist_free(fsprops);
1048185029Spjd	nvlist_free(props);
1049168404Spjd	return (ret);
1050185029Spjdbadusage:
1051185029Spjd	nvlist_free(fsprops);
1052185029Spjd	nvlist_free(props);
1053185029Spjd	usage(B_FALSE);
1054185029Spjd	return (2);
1055168404Spjd}
1056168404Spjd
1057168404Spjd/*
1058168404Spjd * zpool destroy <pool>
1059168404Spjd *
1060168404Spjd * 	-f	Forcefully unmount any datasets
1061168404Spjd *
1062168404Spjd * Destroy the given pool.  Automatically unmounts any datasets in the pool.
1063168404Spjd */
1064168404Spjdint
1065168404Spjdzpool_do_destroy(int argc, char **argv)
1066168404Spjd{
1067168404Spjd	boolean_t force = B_FALSE;
1068168404Spjd	int c;
1069168404Spjd	char *pool;
1070168404Spjd	zpool_handle_t *zhp;
1071168404Spjd	int ret;
1072168404Spjd
1073168404Spjd	/* check options */
1074168404Spjd	while ((c = getopt(argc, argv, "f")) != -1) {
1075168404Spjd		switch (c) {
1076168404Spjd		case 'f':
1077168404Spjd			force = B_TRUE;
1078168404Spjd			break;
1079168404Spjd		case '?':
1080168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1081168404Spjd			    optopt);
1082168404Spjd			usage(B_FALSE);
1083168404Spjd		}
1084168404Spjd	}
1085168404Spjd
1086168404Spjd	argc -= optind;
1087168404Spjd	argv += optind;
1088168404Spjd
1089168404Spjd	/* check arguments */
1090168404Spjd	if (argc < 1) {
1091168404Spjd		(void) fprintf(stderr, gettext("missing pool argument\n"));
1092168404Spjd		usage(B_FALSE);
1093168404Spjd	}
1094168404Spjd	if (argc > 1) {
1095168404Spjd		(void) fprintf(stderr, gettext("too many arguments\n"));
1096168404Spjd		usage(B_FALSE);
1097168404Spjd	}
1098168404Spjd
1099168404Spjd	pool = argv[0];
1100168404Spjd
1101168404Spjd	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1102168404Spjd		/*
1103168404Spjd		 * As a special case, check for use of '/' in the name, and
1104168404Spjd		 * direct the user to use 'zfs destroy' instead.
1105168404Spjd		 */
1106168404Spjd		if (strchr(pool, '/') != NULL)
1107168404Spjd			(void) fprintf(stderr, gettext("use 'zfs destroy' to "
1108168404Spjd			    "destroy a dataset\n"));
1109168404Spjd		return (1);
1110168404Spjd	}
1111168404Spjd
1112168404Spjd	if (zpool_disable_datasets(zhp, force) != 0) {
1113168404Spjd		(void) fprintf(stderr, gettext("could not destroy '%s': "
1114168404Spjd		    "could not unmount datasets\n"), zpool_get_name(zhp));
1115168404Spjd		return (1);
1116168404Spjd	}
1117168404Spjd
1118248571Smm	/* The history must be logged as part of the export */
1119248571Smm	log_history = B_FALSE;
1120168404Spjd
1121248571Smm	ret = (zpool_destroy(zhp, history_str) != 0);
1122248571Smm
1123168404Spjd	zpool_close(zhp);
1124168404Spjd
1125168404Spjd	return (ret);
1126168404Spjd}
1127168404Spjd
1128168404Spjd/*
1129168404Spjd * zpool export [-f] <pool> ...
1130168404Spjd *
1131168404Spjd *	-f	Forcefully unmount datasets
1132168404Spjd *
1133168404Spjd * Export the given pools.  By default, the command will attempt to cleanly
1134168404Spjd * unmount any active datasets within the pool.  If the '-f' flag is specified,
1135168404Spjd * then the datasets will be forcefully unmounted.
1136168404Spjd */
1137168404Spjdint
1138168404Spjdzpool_do_export(int argc, char **argv)
1139168404Spjd{
1140168404Spjd	boolean_t force = B_FALSE;
1141207670Smm	boolean_t hardforce = B_FALSE;
1142168404Spjd	int c;
1143168404Spjd	zpool_handle_t *zhp;
1144168404Spjd	int ret;
1145168404Spjd	int i;
1146168404Spjd
1147168404Spjd	/* check options */
1148207670Smm	while ((c = getopt(argc, argv, "fF")) != -1) {
1149168404Spjd		switch (c) {
1150168404Spjd		case 'f':
1151168404Spjd			force = B_TRUE;
1152168404Spjd			break;
1153207670Smm		case 'F':
1154207670Smm			hardforce = B_TRUE;
1155207670Smm			break;
1156168404Spjd		case '?':
1157168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1158168404Spjd			    optopt);
1159168404Spjd			usage(B_FALSE);
1160168404Spjd		}
1161168404Spjd	}
1162168404Spjd
1163168404Spjd	argc -= optind;
1164168404Spjd	argv += optind;
1165168404Spjd
1166168404Spjd	/* check arguments */
1167168404Spjd	if (argc < 1) {
1168168404Spjd		(void) fprintf(stderr, gettext("missing pool argument\n"));
1169168404Spjd		usage(B_FALSE);
1170168404Spjd	}
1171168404Spjd
1172168404Spjd	ret = 0;
1173168404Spjd	for (i = 0; i < argc; i++) {
1174168404Spjd		if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1175168404Spjd			ret = 1;
1176168404Spjd			continue;
1177168404Spjd		}
1178168404Spjd
1179168404Spjd		if (zpool_disable_datasets(zhp, force) != 0) {
1180168404Spjd			ret = 1;
1181168404Spjd			zpool_close(zhp);
1182168404Spjd			continue;
1183168404Spjd		}
1184168404Spjd
1185248571Smm		/* The history must be logged as part of the export */
1186248571Smm		log_history = B_FALSE;
1187248571Smm
1188207670Smm		if (hardforce) {
1189248571Smm			if (zpool_export_force(zhp, history_str) != 0)
1190207670Smm				ret = 1;
1191248571Smm		} else if (zpool_export(zhp, force, history_str) != 0) {
1192168404Spjd			ret = 1;
1193207670Smm		}
1194168404Spjd
1195168404Spjd		zpool_close(zhp);
1196168404Spjd	}
1197168404Spjd
1198168404Spjd	return (ret);
1199168404Spjd}
1200168404Spjd
1201168404Spjd/*
1202168404Spjd * Given a vdev configuration, determine the maximum width needed for the device
1203168404Spjd * name column.
1204168404Spjd */
1205168404Spjdstatic int
1206168404Spjdmax_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1207168404Spjd{
1208219089Spjd	char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1209168404Spjd	nvlist_t **child;
1210168404Spjd	uint_t c, children;
1211168404Spjd	int ret;
1212168404Spjd
1213168404Spjd	if (strlen(name) + depth > max)
1214168404Spjd		max = strlen(name) + depth;
1215168404Spjd
1216168404Spjd	free(name);
1217168404Spjd
1218168404Spjd	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1219168404Spjd	    &child, &children) == 0) {
1220168404Spjd		for (c = 0; c < children; c++)
1221168404Spjd			if ((ret = max_width(zhp, child[c], depth + 2,
1222168404Spjd			    max)) > max)
1223168404Spjd				max = ret;
1224168404Spjd	}
1225168404Spjd
1226185029Spjd	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1227185029Spjd	    &child, &children) == 0) {
1228185029Spjd		for (c = 0; c < children; c++)
1229185029Spjd			if ((ret = max_width(zhp, child[c], depth + 2,
1230185029Spjd			    max)) > max)
1231185029Spjd				max = ret;
1232185029Spjd	}
1233185029Spjd
1234168404Spjd	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1235168404Spjd	    &child, &children) == 0) {
1236168404Spjd		for (c = 0; c < children; c++)
1237168404Spjd			if ((ret = max_width(zhp, child[c], depth + 2,
1238168404Spjd			    max)) > max)
1239168404Spjd				max = ret;
1240168404Spjd	}
1241168404Spjd
1242168404Spjd
1243168404Spjd	return (max);
1244168404Spjd}
1245168404Spjd
1246213197Smmtypedef struct spare_cbdata {
1247213197Smm	uint64_t	cb_guid;
1248213197Smm	zpool_handle_t	*cb_zhp;
1249213197Smm} spare_cbdata_t;
1250168404Spjd
1251213197Smmstatic boolean_t
1252213197Smmfind_vdev(nvlist_t *nv, uint64_t search)
1253213197Smm{
1254213197Smm	uint64_t guid;
1255213197Smm	nvlist_t **child;
1256213197Smm	uint_t c, children;
1257213197Smm
1258213197Smm	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1259213197Smm	    search == guid)
1260213197Smm		return (B_TRUE);
1261213197Smm
1262213197Smm	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1263213197Smm	    &child, &children) == 0) {
1264213197Smm		for (c = 0; c < children; c++)
1265213197Smm			if (find_vdev(child[c], search))
1266213197Smm				return (B_TRUE);
1267213197Smm	}
1268213197Smm
1269213197Smm	return (B_FALSE);
1270213197Smm}
1271213197Smm
1272213197Smmstatic int
1273213197Smmfind_spare(zpool_handle_t *zhp, void *data)
1274213197Smm{
1275213197Smm	spare_cbdata_t *cbp = data;
1276213197Smm	nvlist_t *config, *nvroot;
1277213197Smm
1278213197Smm	config = zpool_get_config(zhp, NULL);
1279213197Smm	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1280213197Smm	    &nvroot) == 0);
1281213197Smm
1282213197Smm	if (find_vdev(nvroot, cbp->cb_guid)) {
1283213197Smm		cbp->cb_zhp = zhp;
1284213197Smm		return (1);
1285213197Smm	}
1286213197Smm
1287213197Smm	zpool_close(zhp);
1288213197Smm	return (0);
1289213197Smm}
1290213197Smm
1291168404Spjd/*
1292213197Smm * Print out configuration state as requested by status_callback.
1293213197Smm */
1294213197Smmvoid
1295213197Smmprint_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1296213197Smm    int namewidth, int depth, boolean_t isspare)
1297213197Smm{
1298213197Smm	nvlist_t **child;
1299254591Sgibbs	uint_t c, vsc, children;
1300219089Spjd	pool_scan_stat_t *ps = NULL;
1301213197Smm	vdev_stat_t *vs;
1302219089Spjd	char rbuf[6], wbuf[6], cbuf[6];
1303213197Smm	char *vname;
1304213197Smm	uint64_t notpresent;
1305254591Sgibbs	uint64_t ashift;
1306213197Smm	spare_cbdata_t cb;
1307224169Sgibbs	const char *state;
1308213197Smm
1309213197Smm	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1310213197Smm	    &child, &children) != 0)
1311213197Smm		children = 0;
1312213197Smm
1313219089Spjd	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1314254591Sgibbs	    (uint64_t **)&vs, &vsc) == 0);
1315219089Spjd
1316213197Smm	state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1317213197Smm	if (isspare) {
1318213197Smm		/*
1319213197Smm		 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1320213197Smm		 * online drives.
1321213197Smm		 */
1322213197Smm		if (vs->vs_aux == VDEV_AUX_SPARED)
1323213197Smm			state = "INUSE";
1324213197Smm		else if (vs->vs_state == VDEV_STATE_HEALTHY)
1325213197Smm			state = "AVAIL";
1326213197Smm	}
1327213197Smm
1328213197Smm	(void) printf("\t%*s%-*s  %-8s", depth, "", namewidth - depth,
1329213197Smm	    name, state);
1330213197Smm
1331213197Smm	if (!isspare) {
1332213197Smm		zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1333213197Smm		zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1334213197Smm		zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1335213197Smm		(void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1336213197Smm	}
1337213197Smm
1338213197Smm	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1339224170Sgibbs	    &notpresent) == 0 ||
1340224170Sgibbs	    vs->vs_state <= VDEV_STATE_CANT_OPEN) {
1341213197Smm		char *path;
1342224170Sgibbs		if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0)
1343224170Sgibbs			(void) printf("  was %s", path);
1344213197Smm	} else if (vs->vs_aux != 0) {
1345213197Smm		(void) printf("  ");
1346213197Smm
1347213197Smm		switch (vs->vs_aux) {
1348213197Smm		case VDEV_AUX_OPEN_FAILED:
1349213197Smm			(void) printf(gettext("cannot open"));
1350213197Smm			break;
1351213197Smm
1352213197Smm		case VDEV_AUX_BAD_GUID_SUM:
1353213197Smm			(void) printf(gettext("missing device"));
1354213197Smm			break;
1355213197Smm
1356213197Smm		case VDEV_AUX_NO_REPLICAS:
1357213197Smm			(void) printf(gettext("insufficient replicas"));
1358213197Smm			break;
1359213197Smm
1360213197Smm		case VDEV_AUX_VERSION_NEWER:
1361213197Smm			(void) printf(gettext("newer version"));
1362213197Smm			break;
1363213197Smm
1364236884Smm		case VDEV_AUX_UNSUP_FEAT:
1365236884Smm			(void) printf(gettext("unsupported feature(s)"));
1366236884Smm			break;
1367236884Smm
1368254591Sgibbs		case VDEV_AUX_ASHIFT_TOO_BIG:
1369254591Sgibbs			(void) printf(gettext("unsupported minimum blocksize"));
1370254591Sgibbs			break;
1371254591Sgibbs
1372213197Smm		case VDEV_AUX_SPARED:
1373213197Smm			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1374213197Smm			    &cb.cb_guid) == 0);
1375213197Smm			if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1376213197Smm				if (strcmp(zpool_get_name(cb.cb_zhp),
1377213197Smm				    zpool_get_name(zhp)) == 0)
1378213197Smm					(void) printf(gettext("currently in "
1379213197Smm					    "use"));
1380213197Smm				else
1381213197Smm					(void) printf(gettext("in use by "
1382213197Smm					    "pool '%s'"),
1383213197Smm					    zpool_get_name(cb.cb_zhp));
1384213197Smm				zpool_close(cb.cb_zhp);
1385213197Smm			} else {
1386213197Smm				(void) printf(gettext("currently in use"));
1387213197Smm			}
1388213197Smm			break;
1389213197Smm
1390213197Smm		case VDEV_AUX_ERR_EXCEEDED:
1391213197Smm			(void) printf(gettext("too many errors"));
1392213197Smm			break;
1393213197Smm
1394213197Smm		case VDEV_AUX_IO_FAILURE:
1395213197Smm			(void) printf(gettext("experienced I/O failures"));
1396213197Smm			break;
1397213197Smm
1398213197Smm		case VDEV_AUX_BAD_LOG:
1399213197Smm			(void) printf(gettext("bad intent log"));
1400213197Smm			break;
1401213197Smm
1402219089Spjd		case VDEV_AUX_EXTERNAL:
1403219089Spjd			(void) printf(gettext("external device fault"));
1404219089Spjd			break;
1405219089Spjd
1406219089Spjd		case VDEV_AUX_SPLIT_POOL:
1407219089Spjd			(void) printf(gettext("split into new pool"));
1408219089Spjd			break;
1409219089Spjd
1410213197Smm		default:
1411213197Smm			(void) printf(gettext("corrupted data"));
1412213197Smm			break;
1413213197Smm		}
1414254591Sgibbs	} else if (children == 0 && !isspare &&
1415254591Sgibbs	    VDEV_STAT_VALID(vs_physical_ashift, vsc) &&
1416254591Sgibbs	    vs->vs_configured_ashift < vs->vs_physical_ashift) {
1417254591Sgibbs		(void) printf(
1418254591Sgibbs		    gettext("  block size: %dB configured, %dB native"),
1419254591Sgibbs		    1 << vs->vs_configured_ashift, 1 << vs->vs_physical_ashift);
1420213197Smm	}
1421213197Smm
1422219089Spjd	(void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1423219089Spjd	    (uint64_t **)&ps, &c);
1424219089Spjd
1425219089Spjd	if (ps && ps->pss_state == DSS_SCANNING &&
1426219089Spjd	    vs->vs_scan_processed != 0 && children == 0) {
1427219089Spjd		(void) printf(gettext("  (%s)"),
1428219089Spjd		    (ps->pss_func == POOL_SCAN_RESILVER) ?
1429219089Spjd		    "resilvering" : "repairing");
1430219089Spjd	}
1431219089Spjd
1432213197Smm	(void) printf("\n");
1433213197Smm
1434213197Smm	for (c = 0; c < children; c++) {
1435219089Spjd		uint64_t islog = B_FALSE, ishole = B_FALSE;
1436213197Smm
1437219089Spjd		/* Don't print logs or holes here */
1438213197Smm		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1439219089Spjd		    &islog);
1440219089Spjd		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1441219089Spjd		    &ishole);
1442219089Spjd		if (islog || ishole)
1443213197Smm			continue;
1444219089Spjd		vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1445213197Smm		print_status_config(zhp, vname, child[c],
1446213197Smm		    namewidth, depth + 2, isspare);
1447213197Smm		free(vname);
1448213197Smm	}
1449213197Smm}
1450213197Smm
1451213197Smm
1452213197Smm/*
1453168404Spjd * Print the configuration of an exported pool.  Iterate over all vdevs in the
1454168404Spjd * pool, printing out the name and status for each one.
1455168404Spjd */
1456168404Spjdvoid
1457213197Smmprint_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1458168404Spjd{
1459168404Spjd	nvlist_t **child;
1460168404Spjd	uint_t c, children;
1461168404Spjd	vdev_stat_t *vs;
1462168404Spjd	char *type, *vname;
1463168404Spjd
1464168404Spjd	verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1465219089Spjd	if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1466219089Spjd	    strcmp(type, VDEV_TYPE_HOLE) == 0)
1467168404Spjd		return;
1468168404Spjd
1469219089Spjd	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1470168404Spjd	    (uint64_t **)&vs, &c) == 0);
1471168404Spjd
1472168404Spjd	(void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1473185029Spjd	(void) printf("  %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1474168404Spjd
1475168404Spjd	if (vs->vs_aux != 0) {
1476185029Spjd		(void) printf("  ");
1477168404Spjd
1478168404Spjd		switch (vs->vs_aux) {
1479168404Spjd		case VDEV_AUX_OPEN_FAILED:
1480168404Spjd			(void) printf(gettext("cannot open"));
1481168404Spjd			break;
1482168404Spjd
1483168404Spjd		case VDEV_AUX_BAD_GUID_SUM:
1484168404Spjd			(void) printf(gettext("missing device"));
1485168404Spjd			break;
1486168404Spjd
1487168404Spjd		case VDEV_AUX_NO_REPLICAS:
1488168404Spjd			(void) printf(gettext("insufficient replicas"));
1489168404Spjd			break;
1490168404Spjd
1491168404Spjd		case VDEV_AUX_VERSION_NEWER:
1492168404Spjd			(void) printf(gettext("newer version"));
1493168404Spjd			break;
1494168404Spjd
1495236884Smm		case VDEV_AUX_UNSUP_FEAT:
1496236884Smm			(void) printf(gettext("unsupported feature(s)"));
1497236884Smm			break;
1498236884Smm
1499185029Spjd		case VDEV_AUX_ERR_EXCEEDED:
1500185029Spjd			(void) printf(gettext("too many errors"));
1501185029Spjd			break;
1502185029Spjd
1503168404Spjd		default:
1504168404Spjd			(void) printf(gettext("corrupted data"));
1505168404Spjd			break;
1506168404Spjd		}
1507168404Spjd	}
1508168404Spjd	(void) printf("\n");
1509168404Spjd
1510168404Spjd	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1511168404Spjd	    &child, &children) != 0)
1512168404Spjd		return;
1513168404Spjd
1514168404Spjd	for (c = 0; c < children; c++) {
1515185029Spjd		uint64_t is_log = B_FALSE;
1516185029Spjd
1517185029Spjd		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1518185029Spjd		    &is_log);
1519213197Smm		if (is_log)
1520185029Spjd			continue;
1521185029Spjd
1522219089Spjd		vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1523213197Smm		print_import_config(vname, child[c], namewidth, depth + 2);
1524168404Spjd		free(vname);
1525168404Spjd	}
1526168404Spjd
1527185029Spjd	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1528185029Spjd	    &child, &children) == 0) {
1529185029Spjd		(void) printf(gettext("\tcache\n"));
1530185029Spjd		for (c = 0; c < children; c++) {
1531219089Spjd			vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1532185029Spjd			(void) printf("\t  %s\n", vname);
1533185029Spjd			free(vname);
1534185029Spjd		}
1535185029Spjd	}
1536185029Spjd
1537168404Spjd	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1538185029Spjd	    &child, &children) == 0) {
1539185029Spjd		(void) printf(gettext("\tspares\n"));
1540185029Spjd		for (c = 0; c < children; c++) {
1541219089Spjd			vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1542185029Spjd			(void) printf("\t  %s\n", vname);
1543185029Spjd			free(vname);
1544185029Spjd		}
1545168404Spjd	}
1546168404Spjd}
1547168404Spjd
1548168404Spjd/*
1549213197Smm * Print log vdevs.
1550213197Smm * Logs are recorded as top level vdevs in the main pool child array
1551213197Smm * but with "is_log" set to 1. We use either print_status_config() or
1552213197Smm * print_import_config() to print the top level logs then any log
1553213197Smm * children (eg mirrored slogs) are printed recursively - which
1554213197Smm * works because only the top level vdev is marked "is_log"
1555213197Smm */
1556213197Smmstatic void
1557213197Smmprint_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1558213197Smm{
1559213197Smm	uint_t c, children;
1560213197Smm	nvlist_t **child;
1561213197Smm
1562213197Smm	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1563213197Smm	    &children) != 0)
1564213197Smm		return;
1565213197Smm
1566213197Smm	(void) printf(gettext("\tlogs\n"));
1567213197Smm
1568213197Smm	for (c = 0; c < children; c++) {
1569213197Smm		uint64_t is_log = B_FALSE;
1570213197Smm		char *name;
1571213197Smm
1572213197Smm		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1573213197Smm		    &is_log);
1574213197Smm		if (!is_log)
1575213197Smm			continue;
1576219089Spjd		name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1577213197Smm		if (verbose)
1578213197Smm			print_status_config(zhp, name, child[c], namewidth,
1579213197Smm			    2, B_FALSE);
1580213197Smm		else
1581213197Smm			print_import_config(name, child[c], namewidth, 2);
1582213197Smm		free(name);
1583213197Smm	}
1584213197Smm}
1585219089Spjd
1586213197Smm/*
1587168404Spjd * Display the status for the given pool.
1588168404Spjd */
1589168404Spjdstatic void
1590168404Spjdshow_import(nvlist_t *config)
1591168404Spjd{
1592168404Spjd	uint64_t pool_state;
1593168404Spjd	vdev_stat_t *vs;
1594168404Spjd	char *name;
1595168404Spjd	uint64_t guid;
1596168404Spjd	char *msgid;
1597168404Spjd	nvlist_t *nvroot;
1598168404Spjd	int reason;
1599168404Spjd	const char *health;
1600168404Spjd	uint_t vsc;
1601168404Spjd	int namewidth;
1602228103Smm	char *comment;
1603168404Spjd
1604168404Spjd	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1605168404Spjd	    &name) == 0);
1606168404Spjd	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1607168404Spjd	    &guid) == 0);
1608168404Spjd	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1609168404Spjd	    &pool_state) == 0);
1610168404Spjd	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1611168404Spjd	    &nvroot) == 0);
1612168404Spjd
1613219089Spjd	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1614168404Spjd	    (uint64_t **)&vs, &vsc) == 0);
1615185029Spjd	health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1616168404Spjd
1617168404Spjd	reason = zpool_import_status(config, &msgid);
1618168404Spjd
1619228103Smm	(void) printf(gettext("   pool: %s\n"), name);
1620228103Smm	(void) printf(gettext("     id: %llu\n"), (u_longlong_t)guid);
1621228103Smm	(void) printf(gettext("  state: %s"), health);
1622168404Spjd	if (pool_state == POOL_STATE_DESTROYED)
1623168404Spjd		(void) printf(gettext(" (DESTROYED)"));
1624168404Spjd	(void) printf("\n");
1625168404Spjd
1626168404Spjd	switch (reason) {
1627168404Spjd	case ZPOOL_STATUS_MISSING_DEV_R:
1628168404Spjd	case ZPOOL_STATUS_MISSING_DEV_NR:
1629168404Spjd	case ZPOOL_STATUS_BAD_GUID_SUM:
1630228103Smm		(void) printf(gettext(" status: One or more devices are "
1631228103Smm		    "missing from the system.\n"));
1632168404Spjd		break;
1633168404Spjd
1634168404Spjd	case ZPOOL_STATUS_CORRUPT_LABEL_R:
1635168404Spjd	case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1636228103Smm		(void) printf(gettext(" status: One or more devices contains "
1637168404Spjd		    "corrupted data.\n"));
1638168404Spjd		break;
1639168404Spjd
1640168404Spjd	case ZPOOL_STATUS_CORRUPT_DATA:
1641228103Smm		(void) printf(
1642228103Smm		    gettext(" status: The pool data is corrupted.\n"));
1643168404Spjd		break;
1644168404Spjd
1645168404Spjd	case ZPOOL_STATUS_OFFLINE_DEV:
1646228103Smm		(void) printf(gettext(" status: One or more devices "
1647168404Spjd		    "are offlined.\n"));
1648168404Spjd		break;
1649168404Spjd
1650168404Spjd	case ZPOOL_STATUS_CORRUPT_POOL:
1651228103Smm		(void) printf(gettext(" status: The pool metadata is "
1652168404Spjd		    "corrupted.\n"));
1653168404Spjd		break;
1654168404Spjd
1655168404Spjd	case ZPOOL_STATUS_VERSION_OLDER:
1656238926Smm		(void) printf(gettext(" status: The pool is formatted using a "
1657238926Smm		    "legacy on-disk version.\n"));
1658168404Spjd		break;
1659168404Spjd
1660168404Spjd	case ZPOOL_STATUS_VERSION_NEWER:
1661228103Smm		(void) printf(gettext(" status: The pool is formatted using an "
1662168404Spjd		    "incompatible version.\n"));
1663168404Spjd		break;
1664168404Spjd
1665238926Smm	case ZPOOL_STATUS_FEAT_DISABLED:
1666238926Smm		(void) printf(gettext(" status: Some supported features are "
1667238926Smm		    "not enabled on the pool.\n"));
1668238926Smm		break;
1669238926Smm
1670236884Smm	case ZPOOL_STATUS_UNSUP_FEAT_READ:
1671236884Smm		(void) printf(gettext("status: The pool uses the following "
1672236884Smm		    "feature(s) not supported on this sytem:\n"));
1673236884Smm		zpool_print_unsup_feat(config);
1674236884Smm		break;
1675236884Smm
1676236884Smm	case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1677236884Smm		(void) printf(gettext("status: The pool can only be accessed "
1678236884Smm		    "in read-only mode on this system. It\n\tcannot be "
1679236884Smm		    "accessed in read-write mode because it uses the "
1680236884Smm		    "following\n\tfeature(s) not supported on this system:\n"));
1681236884Smm		zpool_print_unsup_feat(config);
1682236884Smm		break;
1683236884Smm
1684168498Spjd	case ZPOOL_STATUS_HOSTID_MISMATCH:
1685228103Smm		(void) printf(gettext(" status: The pool was last accessed by "
1686168498Spjd		    "another system.\n"));
1687168498Spjd		break;
1688185029Spjd
1689185029Spjd	case ZPOOL_STATUS_FAULTED_DEV_R:
1690185029Spjd	case ZPOOL_STATUS_FAULTED_DEV_NR:
1691228103Smm		(void) printf(gettext(" status: One or more devices are "
1692185029Spjd		    "faulted.\n"));
1693185029Spjd		break;
1694185029Spjd
1695185029Spjd	case ZPOOL_STATUS_BAD_LOG:
1696228103Smm		(void) printf(gettext(" status: An intent log record cannot be "
1697185029Spjd		    "read.\n"));
1698185029Spjd		break;
1699185029Spjd
1700219089Spjd	case ZPOOL_STATUS_RESILVERING:
1701228103Smm		(void) printf(gettext(" status: One or more devices were being "
1702219089Spjd		    "resilvered.\n"));
1703219089Spjd		break;
1704219089Spjd
1705259131Sdelphij	case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
1706259131Sdelphij		(void) printf(gettext("status: One or more devices were "
1707259131Sdelphij		    "configured to use a non-native block size.\n"
1708259131Sdelphij		    "\tExpect reduced performance.\n"));
1709259131Sdelphij		break;
1710259131Sdelphij
1711168404Spjd	default:
1712168404Spjd		/*
1713168404Spjd		 * No other status can be seen when importing pools.
1714168404Spjd		 */
1715168404Spjd		assert(reason == ZPOOL_STATUS_OK);
1716168404Spjd	}
1717168404Spjd
1718168404Spjd	/*
1719168404Spjd	 * Print out an action according to the overall state of the pool.
1720168404Spjd	 */
1721168404Spjd	if (vs->vs_state == VDEV_STATE_HEALTHY) {
1722238926Smm		if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1723238926Smm		    reason == ZPOOL_STATUS_FEAT_DISABLED) {
1724228103Smm			(void) printf(gettext(" action: The pool can be "
1725168404Spjd			    "imported using its name or numeric identifier, "
1726168404Spjd			    "though\n\tsome features will not be available "
1727168404Spjd			    "without an explicit 'zpool upgrade'.\n"));
1728238926Smm		} else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1729228103Smm			(void) printf(gettext(" action: The pool can be "
1730168498Spjd			    "imported using its name or numeric "
1731168498Spjd			    "identifier and\n\tthe '-f' flag.\n"));
1732238926Smm		} else {
1733228103Smm			(void) printf(gettext(" action: The pool can be "
1734168404Spjd			    "imported using its name or numeric "
1735168404Spjd			    "identifier.\n"));
1736238926Smm		}
1737168404Spjd	} else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1738228103Smm		(void) printf(gettext(" action: The pool can be imported "
1739168404Spjd		    "despite missing or damaged devices.  The\n\tfault "
1740168404Spjd		    "tolerance of the pool may be compromised if imported.\n"));
1741168404Spjd	} else {
1742168404Spjd		switch (reason) {
1743168404Spjd		case ZPOOL_STATUS_VERSION_NEWER:
1744228103Smm			(void) printf(gettext(" action: The pool cannot be "
1745168404Spjd			    "imported.  Access the pool on a system running "
1746168404Spjd			    "newer\n\tsoftware, or recreate the pool from "
1747168404Spjd			    "backup.\n"));
1748168404Spjd			break;
1749236884Smm		case ZPOOL_STATUS_UNSUP_FEAT_READ:
1750236884Smm			(void) printf(gettext("action: The pool cannot be "
1751236884Smm			    "imported. Access the pool on a system that "
1752236884Smm			    "supports\n\tthe required feature(s), or recreate "
1753236884Smm			    "the pool from backup.\n"));
1754236884Smm			break;
1755236884Smm		case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1756236884Smm			(void) printf(gettext("action: The pool cannot be "
1757236884Smm			    "imported in read-write mode. Import the pool "
1758236884Smm			    "with\n"
1759236884Smm			    "\t\"-o readonly=on\", access the pool on a system "
1760236884Smm			    "that supports the\n\trequired feature(s), or "
1761236884Smm			    "recreate the pool from backup.\n"));
1762236884Smm			break;
1763168404Spjd		case ZPOOL_STATUS_MISSING_DEV_R:
1764168404Spjd		case ZPOOL_STATUS_MISSING_DEV_NR:
1765168404Spjd		case ZPOOL_STATUS_BAD_GUID_SUM:
1766228103Smm			(void) printf(gettext(" action: The pool cannot be "
1767168404Spjd			    "imported. Attach the missing\n\tdevices and try "
1768168404Spjd			    "again.\n"));
1769168404Spjd			break;
1770168404Spjd		default:
1771228103Smm			(void) printf(gettext(" action: The pool cannot be "
1772168404Spjd			    "imported due to damaged devices or data.\n"));
1773168404Spjd		}
1774168404Spjd	}
1775168404Spjd
1776228103Smm	/* Print the comment attached to the pool. */
1777228103Smm	if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1778228103Smm		(void) printf(gettext("comment: %s\n"), comment);
1779228103Smm
1780168404Spjd	/*
1781168404Spjd	 * If the state is "closed" or "can't open", and the aux state
1782168404Spjd	 * is "corrupt data":
1783168404Spjd	 */
1784168404Spjd	if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1785168404Spjd	    (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1786168404Spjd	    (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1787168404Spjd		if (pool_state == POOL_STATE_DESTROYED)
1788168404Spjd			(void) printf(gettext("\tThe pool was destroyed, "
1789168404Spjd			    "but can be imported using the '-Df' flags.\n"));
1790168404Spjd		else if (pool_state != POOL_STATE_EXPORTED)
1791168404Spjd			(void) printf(gettext("\tThe pool may be active on "
1792185029Spjd			    "another system, but can be imported using\n\t"
1793168404Spjd			    "the '-f' flag.\n"));
1794168404Spjd	}
1795168404Spjd
1796168404Spjd	if (msgid != NULL)
1797236146Smm		(void) printf(gettext("   see: http://illumos.org/msg/%s\n"),
1798168404Spjd		    msgid);
1799168404Spjd
1800228103Smm	(void) printf(gettext(" config:\n\n"));
1801168404Spjd
1802168404Spjd	namewidth = max_width(NULL, nvroot, 0, 0);
1803168404Spjd	if (namewidth < 10)
1804168404Spjd		namewidth = 10;
1805168404Spjd
1806213197Smm	print_import_config(name, nvroot, namewidth, 0);
1807213197Smm	if (num_logs(nvroot) > 0)
1808213197Smm		print_logs(NULL, nvroot, namewidth, B_FALSE);
1809185029Spjd
1810168404Spjd	if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1811168404Spjd		(void) printf(gettext("\n\tAdditional devices are known to "
1812168404Spjd		    "be part of this pool, though their\n\texact "
1813168404Spjd		    "configuration cannot be determined.\n"));
1814168404Spjd	}
1815168404Spjd}
1816168404Spjd
1817168404Spjd/*
1818168404Spjd * Perform the import for the given configuration.  This passes the heavy
1819185029Spjd * lifting off to zpool_import_props(), and then mounts the datasets contained
1820185029Spjd * within the pool.
1821168404Spjd */
1822168404Spjdstatic int
1823168404Spjddo_import(nvlist_t *config, const char *newname, const char *mntopts,
1824219089Spjd    nvlist_t *props, int flags)
1825168404Spjd{
1826168404Spjd	zpool_handle_t *zhp;
1827168404Spjd	char *name;
1828168404Spjd	uint64_t state;
1829168404Spjd	uint64_t version;
1830168404Spjd
1831168404Spjd	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1832168404Spjd	    &name) == 0);
1833168404Spjd
1834168404Spjd	verify(nvlist_lookup_uint64(config,
1835168404Spjd	    ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1836168404Spjd	verify(nvlist_lookup_uint64(config,
1837168404Spjd	    ZPOOL_CONFIG_VERSION, &version) == 0);
1838236884Smm	if (!SPA_VERSION_IS_SUPPORTED(version)) {
1839168404Spjd		(void) fprintf(stderr, gettext("cannot import '%s': pool "
1840236884Smm		    "is formatted using an unsupported ZFS version\n"), name);
1841168404Spjd		return (1);
1842219089Spjd	} else if (state != POOL_STATE_EXPORTED &&
1843219089Spjd	    !(flags & ZFS_IMPORT_ANY_HOST)) {
1844168498Spjd		uint64_t hostid;
1845168498Spjd
1846168498Spjd		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1847168498Spjd		    &hostid) == 0) {
1848168498Spjd			if ((unsigned long)hostid != gethostid()) {
1849168498Spjd				char *hostname;
1850168498Spjd				uint64_t timestamp;
1851168498Spjd				time_t t;
1852168498Spjd
1853168498Spjd				verify(nvlist_lookup_string(config,
1854168498Spjd				    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1855168498Spjd				verify(nvlist_lookup_uint64(config,
1856168498Spjd				    ZPOOL_CONFIG_TIMESTAMP, &timestamp) == 0);
1857168498Spjd				t = timestamp;
1858168498Spjd				(void) fprintf(stderr, gettext("cannot import "
1859168498Spjd				    "'%s': pool may be in use from other "
1860168498Spjd				    "system, it was last accessed by %s "
1861168498Spjd				    "(hostid: 0x%lx) on %s"), name, hostname,
1862168498Spjd				    (unsigned long)hostid,
1863168498Spjd				    asctime(localtime(&t)));
1864168498Spjd				(void) fprintf(stderr, gettext("use '-f' to "
1865168498Spjd				    "import anyway\n"));
1866168498Spjd				return (1);
1867168498Spjd			}
1868168498Spjd		} else {
1869168498Spjd			(void) fprintf(stderr, gettext("cannot import '%s': "
1870168498Spjd			    "pool may be in use from other system\n"), name);
1871168498Spjd			(void) fprintf(stderr, gettext("use '-f' to import "
1872168498Spjd			    "anyway\n"));
1873168498Spjd			return (1);
1874168498Spjd		}
1875168404Spjd	}
1876168404Spjd
1877219089Spjd	if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1878168404Spjd		return (1);
1879168404Spjd
1880168404Spjd	if (newname != NULL)
1881168404Spjd		name = (char *)newname;
1882168404Spjd
1883209962Smm	if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1884209962Smm		return (1);
1885168404Spjd
1886209962Smm	if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1887219089Spjd	    !(flags & ZFS_IMPORT_ONLY) &&
1888209962Smm	    zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1889168404Spjd		zpool_close(zhp);
1890168404Spjd		return (1);
1891168404Spjd	}
1892168404Spjd
1893168404Spjd	zpool_close(zhp);
1894219089Spjd	return (0);
1895168404Spjd}
1896168404Spjd
1897168404Spjd/*
1898168404Spjd * zpool import [-d dir] [-D]
1899185029Spjd *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1900185029Spjd *              [-d dir | -c cachefile] [-f] -a
1901185029Spjd *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1902219089Spjd *              [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1903168404Spjd *
1904185029Spjd *	 -c	Read pool information from a cachefile instead of searching
1905185029Spjd *		devices.
1906185029Spjd *
1907168404Spjd *       -d	Scan in a specific directory, other than /dev/dsk.  More than
1908168404Spjd *		one directory can be specified using multiple '-d' options.
1909168404Spjd *
1910168404Spjd *       -D     Scan for previously destroyed pools or import all or only
1911168404Spjd *              specified destroyed pools.
1912168404Spjd *
1913168404Spjd *       -R	Temporarily import the pool, with all mountpoints relative to
1914168404Spjd *		the given root.  The pool will remain exported when the machine
1915168404Spjd *		is rebooted.
1916168404Spjd *
1917219089Spjd *       -V	Import even in the presence of faulted vdevs.  This is an
1918185029Spjd *       	intentionally undocumented option for testing purposes, and
1919185029Spjd *       	treats the pool configuration as complete, leaving any bad
1920209962Smm *		vdevs in the FAULTED state. In other words, it does verbatim
1921209962Smm *		import.
1922185029Spjd *
1923219089Spjd *       -f	Force import, even if it appears that the pool is active.
1924219089Spjd *
1925219089Spjd *       -F     Attempt rewind if necessary.
1926219089Spjd *
1927219089Spjd *       -n     See if rewind would work, but don't actually rewind.
1928219089Spjd *
1929219089Spjd *       -N     Import the pool but don't mount datasets.
1930219089Spjd *
1931219089Spjd *       -T     Specify a starting txg to use for import. This option is
1932219089Spjd *       	intentionally undocumented option for testing purposes.
1933219089Spjd *
1934168404Spjd *       -a	Import all pools found.
1935168404Spjd *
1936185029Spjd *       -o	Set property=value and/or temporary mount options (without '=').
1937185029Spjd *
1938168404Spjd * The import command scans for pools to import, and import pools based on pool
1939168404Spjd * name and GUID.  The pool can also be renamed as part of the import process.
1940168404Spjd */
1941168404Spjdint
1942168404Spjdzpool_do_import(int argc, char **argv)
1943168404Spjd{
1944168404Spjd	char **searchdirs = NULL;
1945168404Spjd	int nsearch = 0;
1946168404Spjd	int c;
1947219089Spjd	int err = 0;
1948185029Spjd	nvlist_t *pools = NULL;
1949168404Spjd	boolean_t do_all = B_FALSE;
1950168404Spjd	boolean_t do_destroyed = B_FALSE;
1951168404Spjd	char *mntopts = NULL;
1952168404Spjd	nvpair_t *elem;
1953168404Spjd	nvlist_t *config;
1954185029Spjd	uint64_t searchguid = 0;
1955185029Spjd	char *searchname = NULL;
1956185029Spjd	char *propval;
1957168404Spjd	nvlist_t *found_config;
1958219089Spjd	nvlist_t *policy = NULL;
1959185029Spjd	nvlist_t *props = NULL;
1960168404Spjd	boolean_t first;
1961219089Spjd	int flags = ZFS_IMPORT_NORMAL;
1962219089Spjd	uint32_t rewind_policy = ZPOOL_NO_REWIND;
1963219089Spjd	boolean_t dryrun = B_FALSE;
1964219089Spjd	boolean_t do_rewind = B_FALSE;
1965219089Spjd	boolean_t xtreme_rewind = B_FALSE;
1966219089Spjd	uint64_t pool_state, txg = -1ULL;
1967185029Spjd	char *cachefile = NULL;
1968219089Spjd	importargs_t idata = { 0 };
1969219089Spjd	char *endptr;
1970168404Spjd
1971168404Spjd	/* check options */
1972263385Sdelphij	while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:R:T:VX")) != -1) {
1973168404Spjd		switch (c) {
1974168404Spjd		case 'a':
1975168404Spjd			do_all = B_TRUE;
1976168404Spjd			break;
1977185029Spjd		case 'c':
1978185029Spjd			cachefile = optarg;
1979185029Spjd			break;
1980168404Spjd		case 'd':
1981168404Spjd			if (searchdirs == NULL) {
1982168404Spjd				searchdirs = safe_malloc(sizeof (char *));
1983168404Spjd			} else {
1984168404Spjd				char **tmp = safe_malloc((nsearch + 1) *
1985168404Spjd				    sizeof (char *));
1986168404Spjd				bcopy(searchdirs, tmp, nsearch *
1987168404Spjd				    sizeof (char *));
1988168404Spjd				free(searchdirs);
1989168404Spjd				searchdirs = tmp;
1990168404Spjd			}
1991168404Spjd			searchdirs[nsearch++] = optarg;
1992168404Spjd			break;
1993168404Spjd		case 'D':
1994168404Spjd			do_destroyed = B_TRUE;
1995168404Spjd			break;
1996168404Spjd		case 'f':
1997219089Spjd			flags |= ZFS_IMPORT_ANY_HOST;
1998168404Spjd			break;
1999185029Spjd		case 'F':
2000219089Spjd			do_rewind = B_TRUE;
2001185029Spjd			break;
2002219089Spjd		case 'm':
2003219089Spjd			flags |= ZFS_IMPORT_MISSING_LOG;
2004219089Spjd			break;
2005219089Spjd		case 'n':
2006219089Spjd			dryrun = B_TRUE;
2007219089Spjd			break;
2008219089Spjd		case 'N':
2009219089Spjd			flags |= ZFS_IMPORT_ONLY;
2010219089Spjd			break;
2011168404Spjd		case 'o':
2012185029Spjd			if ((propval = strchr(optarg, '=')) != NULL) {
2013185029Spjd				*propval = '\0';
2014185029Spjd				propval++;
2015185029Spjd				if (add_prop_list(optarg, propval,
2016185029Spjd				    &props, B_TRUE))
2017185029Spjd					goto error;
2018185029Spjd			} else {
2019185029Spjd				mntopts = optarg;
2020185029Spjd			}
2021168404Spjd			break;
2022168404Spjd		case 'R':
2023185029Spjd			if (add_prop_list(zpool_prop_to_name(
2024185029Spjd			    ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2025185029Spjd				goto error;
2026185029Spjd			if (nvlist_lookup_string(props,
2027185029Spjd			    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
2028185029Spjd			    &propval) == 0)
2029185029Spjd				break;
2030185029Spjd			if (add_prop_list(zpool_prop_to_name(
2031185029Spjd			    ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2032185029Spjd				goto error;
2033168404Spjd			break;
2034219089Spjd		case 'T':
2035219089Spjd			errno = 0;
2036268720Sdelphij			txg = strtoull(optarg, &endptr, 0);
2037219089Spjd			if (errno != 0 || *endptr != '\0') {
2038219089Spjd				(void) fprintf(stderr,
2039219089Spjd				    gettext("invalid txg value\n"));
2040219089Spjd				usage(B_FALSE);
2041219089Spjd			}
2042219089Spjd			rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2043219089Spjd			break;
2044219089Spjd		case 'V':
2045219089Spjd			flags |= ZFS_IMPORT_VERBATIM;
2046219089Spjd			break;
2047219089Spjd		case 'X':
2048219089Spjd			xtreme_rewind = B_TRUE;
2049219089Spjd			break;
2050168404Spjd		case ':':
2051168404Spjd			(void) fprintf(stderr, gettext("missing argument for "
2052168404Spjd			    "'%c' option\n"), optopt);
2053168404Spjd			usage(B_FALSE);
2054168404Spjd			break;
2055168404Spjd		case '?':
2056168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2057168404Spjd			    optopt);
2058168404Spjd			usage(B_FALSE);
2059168404Spjd		}
2060168404Spjd	}
2061168404Spjd
2062168404Spjd	argc -= optind;
2063168404Spjd	argv += optind;
2064168404Spjd
2065185029Spjd	if (cachefile && nsearch != 0) {
2066185029Spjd		(void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2067185029Spjd		usage(B_FALSE);
2068185029Spjd	}
2069185029Spjd
2070219089Spjd	if ((dryrun || xtreme_rewind) && !do_rewind) {
2071219089Spjd		(void) fprintf(stderr,
2072219089Spjd		    gettext("-n or -X only meaningful with -F\n"));
2073219089Spjd		usage(B_FALSE);
2074219089Spjd	}
2075219089Spjd	if (dryrun)
2076219089Spjd		rewind_policy = ZPOOL_TRY_REWIND;
2077219089Spjd	else if (do_rewind)
2078219089Spjd		rewind_policy = ZPOOL_DO_REWIND;
2079219089Spjd	if (xtreme_rewind)
2080219089Spjd		rewind_policy |= ZPOOL_EXTREME_REWIND;
2081219089Spjd
2082219089Spjd	/* In the future, we can capture further policy and include it here */
2083219089Spjd	if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2084219089Spjd	    nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2085219089Spjd	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2086219089Spjd		goto error;
2087219089Spjd
2088168404Spjd	if (searchdirs == NULL) {
2089168404Spjd		searchdirs = safe_malloc(sizeof (char *));
2090235478Savg		searchdirs[0] = "/dev";
2091168404Spjd		nsearch = 1;
2092168404Spjd	}
2093168404Spjd
2094168404Spjd	/* check argument count */
2095168404Spjd	if (do_all) {
2096168404Spjd		if (argc != 0) {
2097168404Spjd			(void) fprintf(stderr, gettext("too many arguments\n"));
2098168404Spjd			usage(B_FALSE);
2099168404Spjd		}
2100168404Spjd	} else {
2101168404Spjd		if (argc > 2) {
2102168404Spjd			(void) fprintf(stderr, gettext("too many arguments\n"));
2103168404Spjd			usage(B_FALSE);
2104168404Spjd		}
2105168404Spjd
2106168404Spjd		/*
2107168404Spjd		 * Check for the SYS_CONFIG privilege.  We do this explicitly
2108168404Spjd		 * here because otherwise any attempt to discover pools will
2109168404Spjd		 * silently fail.
2110168404Spjd		 */
2111168404Spjd		if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2112168404Spjd			(void) fprintf(stderr, gettext("cannot "
2113168404Spjd			    "discover pools: permission denied\n"));
2114168404Spjd			free(searchdirs);
2115219089Spjd			nvlist_free(policy);
2116168404Spjd			return (1);
2117168404Spjd		}
2118168404Spjd	}
2119168404Spjd
2120168404Spjd	/*
2121168404Spjd	 * Depending on the arguments given, we do one of the following:
2122168404Spjd	 *
2123168404Spjd	 *	<none>	Iterate through all pools and display information about
2124168404Spjd	 *		each one.
2125168404Spjd	 *
2126168404Spjd	 *	-a	Iterate through all pools and try to import each one.
2127168404Spjd	 *
2128168404Spjd	 *	<id>	Find the pool that corresponds to the given GUID/pool
2129168404Spjd	 *		name and import that one.
2130168404Spjd	 *
2131168404Spjd	 *	-D	Above options applies only to destroyed pools.
2132168404Spjd	 */
2133168404Spjd	if (argc != 0) {
2134168404Spjd		char *endptr;
2135168404Spjd
2136168404Spjd		errno = 0;
2137168404Spjd		searchguid = strtoull(argv[0], &endptr, 10);
2138254758Sdelphij		if (errno != 0 || *endptr != '\0') {
2139168404Spjd			searchname = argv[0];
2140254758Sdelphij			searchguid = 0;
2141254758Sdelphij		}
2142168404Spjd		found_config = NULL;
2143168404Spjd
2144185029Spjd		/*
2145219089Spjd		 * User specified a name or guid.  Ensure it's unique.
2146185029Spjd		 */
2147219089Spjd		idata.unique = B_TRUE;
2148185029Spjd	}
2149185029Spjd
2150219089Spjd
2151219089Spjd	idata.path = searchdirs;
2152219089Spjd	idata.paths = nsearch;
2153219089Spjd	idata.poolname = searchname;
2154219089Spjd	idata.guid = searchguid;
2155219089Spjd	idata.cachefile = cachefile;
2156219089Spjd
2157219089Spjd	pools = zpool_search_import(g_zfs, &idata);
2158219089Spjd
2159219089Spjd	if (pools != NULL && idata.exists &&
2160219089Spjd	    (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2161219089Spjd		(void) fprintf(stderr, gettext("cannot import '%s': "
2162219089Spjd		    "a pool with that name already exists\n"),
2163219089Spjd		    argv[0]);
2164219089Spjd		(void) fprintf(stderr, gettext("use the form '%s "
2165219089Spjd		    "<pool | id> <newpool>' to give it a new name\n"),
2166219089Spjd		    "zpool import");
2167219089Spjd		err = 1;
2168219089Spjd	} else if (pools == NULL && idata.exists) {
2169219089Spjd		(void) fprintf(stderr, gettext("cannot import '%s': "
2170219089Spjd		    "a pool with that name is already created/imported,\n"),
2171219089Spjd		    argv[0]);
2172219089Spjd		(void) fprintf(stderr, gettext("and no additional pools "
2173219089Spjd		    "with that name were found\n"));
2174219089Spjd		err = 1;
2175219089Spjd	} else if (pools == NULL) {
2176185029Spjd		if (argc != 0) {
2177185029Spjd			(void) fprintf(stderr, gettext("cannot import '%s': "
2178185029Spjd			    "no such pool available\n"), argv[0]);
2179185029Spjd		}
2180219089Spjd		err = 1;
2181219089Spjd	}
2182219089Spjd
2183219089Spjd	if (err == 1) {
2184185029Spjd		free(searchdirs);
2185219089Spjd		nvlist_free(policy);
2186185029Spjd		return (1);
2187185029Spjd	}
2188185029Spjd
2189185029Spjd	/*
2190185029Spjd	 * At this point we have a list of import candidate configs. Even if
2191185029Spjd	 * we were searching by pool name or guid, we still need to
2192185029Spjd	 * post-process the list to deal with pool state and possible
2193185029Spjd	 * duplicate names.
2194185029Spjd	 */
2195168404Spjd	err = 0;
2196168404Spjd	elem = NULL;
2197168404Spjd	first = B_TRUE;
2198168404Spjd	while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2199168404Spjd
2200168404Spjd		verify(nvpair_value_nvlist(elem, &config) == 0);
2201168404Spjd
2202168404Spjd		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2203168404Spjd		    &pool_state) == 0);
2204168404Spjd		if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2205168404Spjd			continue;
2206168404Spjd		if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2207168404Spjd			continue;
2208168404Spjd
2209219089Spjd		verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2210219089Spjd		    policy) == 0);
2211219089Spjd
2212168404Spjd		if (argc == 0) {
2213168404Spjd			if (first)
2214168404Spjd				first = B_FALSE;
2215168404Spjd			else if (!do_all)
2216168404Spjd				(void) printf("\n");
2217168404Spjd
2218219089Spjd			if (do_all) {
2219168404Spjd				err |= do_import(config, NULL, mntopts,
2220219089Spjd				    props, flags);
2221219089Spjd			} else {
2222168404Spjd				show_import(config);
2223219089Spjd			}
2224168404Spjd		} else if (searchname != NULL) {
2225168404Spjd			char *name;
2226168404Spjd
2227168404Spjd			/*
2228168404Spjd			 * We are searching for a pool based on name.
2229168404Spjd			 */
2230168404Spjd			verify(nvlist_lookup_string(config,
2231168404Spjd			    ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2232168404Spjd
2233168404Spjd			if (strcmp(name, searchname) == 0) {
2234168404Spjd				if (found_config != NULL) {
2235168404Spjd					(void) fprintf(stderr, gettext(
2236168404Spjd					    "cannot import '%s': more than "
2237168404Spjd					    "one matching pool\n"), searchname);
2238168404Spjd					(void) fprintf(stderr, gettext(
2239168404Spjd					    "import by numeric ID instead\n"));
2240168404Spjd					err = B_TRUE;
2241168404Spjd				}
2242168404Spjd				found_config = config;
2243168404Spjd			}
2244168404Spjd		} else {
2245168404Spjd			uint64_t guid;
2246168404Spjd
2247168404Spjd			/*
2248168404Spjd			 * Search for a pool by guid.
2249168404Spjd			 */
2250168404Spjd			verify(nvlist_lookup_uint64(config,
2251168404Spjd			    ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2252168404Spjd
2253168404Spjd			if (guid == searchguid)
2254168404Spjd				found_config = config;
2255168404Spjd		}
2256168404Spjd	}
2257168404Spjd
2258168404Spjd	/*
2259168404Spjd	 * If we were searching for a specific pool, verify that we found a
2260168404Spjd	 * pool, and then do the import.
2261168404Spjd	 */
2262168404Spjd	if (argc != 0 && err == 0) {
2263168404Spjd		if (found_config == NULL) {
2264168404Spjd			(void) fprintf(stderr, gettext("cannot import '%s': "
2265168404Spjd			    "no such pool available\n"), argv[0]);
2266168404Spjd			err = B_TRUE;
2267168404Spjd		} else {
2268168404Spjd			err |= do_import(found_config, argc == 1 ? NULL :
2269219089Spjd			    argv[1], mntopts, props, flags);
2270168404Spjd		}
2271168404Spjd	}
2272168404Spjd
2273168404Spjd	/*
2274168404Spjd	 * If we were just looking for pools, report an error if none were
2275168404Spjd	 * found.
2276168404Spjd	 */
2277168404Spjd	if (argc == 0 && first)
2278168404Spjd		(void) fprintf(stderr,
2279168404Spjd		    gettext("no pools available to import\n"));
2280168404Spjd
2281185029Spjderror:
2282185029Spjd	nvlist_free(props);
2283168404Spjd	nvlist_free(pools);
2284219089Spjd	nvlist_free(policy);
2285168404Spjd	free(searchdirs);
2286168404Spjd
2287168404Spjd	return (err ? 1 : 0);
2288168404Spjd}
2289168404Spjd
2290168404Spjdtypedef struct iostat_cbdata {
2291236155Smm	boolean_t cb_verbose;
2292236155Smm	int cb_namewidth;
2293236155Smm	int cb_iteration;
2294168404Spjd	zpool_list_t *cb_list;
2295168404Spjd} iostat_cbdata_t;
2296168404Spjd
2297168404Spjdstatic void
2298168404Spjdprint_iostat_separator(iostat_cbdata_t *cb)
2299168404Spjd{
2300168404Spjd	int i = 0;
2301168404Spjd
2302168404Spjd	for (i = 0; i < cb->cb_namewidth; i++)
2303168404Spjd		(void) printf("-");
2304168404Spjd	(void) printf("  -----  -----  -----  -----  -----  -----\n");
2305168404Spjd}
2306168404Spjd
2307168404Spjdstatic void
2308168404Spjdprint_iostat_header(iostat_cbdata_t *cb)
2309168404Spjd{
2310168404Spjd	(void) printf("%*s     capacity     operations    bandwidth\n",
2311168404Spjd	    cb->cb_namewidth, "");
2312219089Spjd	(void) printf("%-*s  alloc   free   read  write   read  write\n",
2313168404Spjd	    cb->cb_namewidth, "pool");
2314168404Spjd	print_iostat_separator(cb);
2315168404Spjd}
2316168404Spjd
2317168404Spjd/*
2318168404Spjd * Display a single statistic.
2319168404Spjd */
2320185029Spjdstatic void
2321168404Spjdprint_one_stat(uint64_t value)
2322168404Spjd{
2323168404Spjd	char buf[64];
2324168404Spjd
2325168404Spjd	zfs_nicenum(value, buf, sizeof (buf));
2326168404Spjd	(void) printf("  %5s", buf);
2327168404Spjd}
2328168404Spjd
2329168404Spjd/*
2330168404Spjd * Print out all the statistics for the given vdev.  This can either be the
2331168404Spjd * toplevel configuration, or called recursively.  If 'name' is NULL, then this
2332168404Spjd * is a verbose output, and we don't want to display the toplevel pool stats.
2333168404Spjd */
2334168404Spjdvoid
2335168404Spjdprint_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2336168404Spjd    nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2337168404Spjd{
2338168404Spjd	nvlist_t **oldchild, **newchild;
2339168404Spjd	uint_t c, children;
2340168404Spjd	vdev_stat_t *oldvs, *newvs;
2341168404Spjd	vdev_stat_t zerovs = { 0 };
2342168404Spjd	uint64_t tdelta;
2343168404Spjd	double scale;
2344168404Spjd	char *vname;
2345168404Spjd
2346168404Spjd	if (oldnv != NULL) {
2347219089Spjd		verify(nvlist_lookup_uint64_array(oldnv,
2348219089Spjd		    ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2349168404Spjd	} else {
2350168404Spjd		oldvs = &zerovs;
2351168404Spjd	}
2352168404Spjd
2353219089Spjd	verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2354168404Spjd	    (uint64_t **)&newvs, &c) == 0);
2355168404Spjd
2356168404Spjd	if (strlen(name) + depth > cb->cb_namewidth)
2357168404Spjd		(void) printf("%*s%s", depth, "", name);
2358168404Spjd	else
2359168404Spjd		(void) printf("%*s%s%*s", depth, "", name,
2360168404Spjd		    (int)(cb->cb_namewidth - strlen(name) - depth), "");
2361168404Spjd
2362168404Spjd	tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2363168404Spjd
2364168404Spjd	if (tdelta == 0)
2365168404Spjd		scale = 1.0;
2366168404Spjd	else
2367168404Spjd		scale = (double)NANOSEC / tdelta;
2368168404Spjd
2369168404Spjd	/* only toplevel vdevs have capacity stats */
2370168404Spjd	if (newvs->vs_space == 0) {
2371168404Spjd		(void) printf("      -      -");
2372168404Spjd	} else {
2373168404Spjd		print_one_stat(newvs->vs_alloc);
2374168404Spjd		print_one_stat(newvs->vs_space - newvs->vs_alloc);
2375168404Spjd	}
2376168404Spjd
2377168404Spjd	print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2378168404Spjd	    oldvs->vs_ops[ZIO_TYPE_READ])));
2379168404Spjd
2380168404Spjd	print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2381168404Spjd	    oldvs->vs_ops[ZIO_TYPE_WRITE])));
2382168404Spjd
2383168404Spjd	print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2384168404Spjd	    oldvs->vs_bytes[ZIO_TYPE_READ])));
2385168404Spjd
2386168404Spjd	print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2387168404Spjd	    oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2388168404Spjd
2389168404Spjd	(void) printf("\n");
2390168404Spjd
2391168404Spjd	if (!cb->cb_verbose)
2392168404Spjd		return;
2393168404Spjd
2394168404Spjd	if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2395168404Spjd	    &newchild, &children) != 0)
2396168404Spjd		return;
2397168404Spjd
2398168404Spjd	if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2399168404Spjd	    &oldchild, &c) != 0)
2400168404Spjd		return;
2401168404Spjd
2402168404Spjd	for (c = 0; c < children; c++) {
2403227497Smm		uint64_t ishole = B_FALSE, islog = B_FALSE;
2404219089Spjd
2405227497Smm		(void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2406227497Smm		    &ishole);
2407227497Smm
2408227497Smm		(void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2409227497Smm		    &islog);
2410227497Smm
2411227497Smm		if (ishole || islog)
2412219089Spjd			continue;
2413219089Spjd
2414219089Spjd		vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2415168404Spjd		print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2416168404Spjd		    newchild[c], cb, depth + 2);
2417168404Spjd		free(vname);
2418168404Spjd	}
2419185029Spjd
2420185029Spjd	/*
2421227497Smm	 * Log device section
2422227497Smm	 */
2423227497Smm
2424227497Smm	if (num_logs(newnv) > 0) {
2425227497Smm		(void) printf("%-*s      -      -      -      -      -      "
2426227497Smm		    "-\n", cb->cb_namewidth, "logs");
2427227497Smm
2428227497Smm		for (c = 0; c < children; c++) {
2429227497Smm			uint64_t islog = B_FALSE;
2430227497Smm			(void) nvlist_lookup_uint64(newchild[c],
2431227497Smm			    ZPOOL_CONFIG_IS_LOG, &islog);
2432227497Smm
2433227497Smm			if (islog) {
2434227497Smm				vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2435227497Smm				    B_FALSE);
2436227497Smm				print_vdev_stats(zhp, vname, oldnv ?
2437227497Smm				    oldchild[c] : NULL, newchild[c],
2438227497Smm				    cb, depth + 2);
2439227497Smm				free(vname);
2440227497Smm			}
2441227497Smm		}
2442227497Smm
2443227497Smm	}
2444227497Smm
2445227497Smm	/*
2446185029Spjd	 * Include level 2 ARC devices in iostat output
2447185029Spjd	 */
2448185029Spjd	if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2449185029Spjd	    &newchild, &children) != 0)
2450185029Spjd		return;
2451185029Spjd
2452185029Spjd	if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2453185029Spjd	    &oldchild, &c) != 0)
2454185029Spjd		return;
2455185029Spjd
2456185029Spjd	if (children > 0) {
2457185029Spjd		(void) printf("%-*s      -      -      -      -      -      "
2458185029Spjd		    "-\n", cb->cb_namewidth, "cache");
2459185029Spjd		for (c = 0; c < children; c++) {
2460219089Spjd			vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2461219089Spjd			    B_FALSE);
2462185029Spjd			print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2463185029Spjd			    newchild[c], cb, depth + 2);
2464185029Spjd			free(vname);
2465185029Spjd		}
2466185029Spjd	}
2467168404Spjd}
2468168404Spjd
2469168404Spjdstatic int
2470168404Spjdrefresh_iostat(zpool_handle_t *zhp, void *data)
2471168404Spjd{
2472168404Spjd	iostat_cbdata_t *cb = data;
2473168404Spjd	boolean_t missing;
2474168404Spjd
2475168404Spjd	/*
2476168404Spjd	 * If the pool has disappeared, remove it from the list and continue.
2477168404Spjd	 */
2478168404Spjd	if (zpool_refresh_stats(zhp, &missing) != 0)
2479168404Spjd		return (-1);
2480168404Spjd
2481168404Spjd	if (missing)
2482168404Spjd		pool_list_remove(cb->cb_list, zhp);
2483168404Spjd
2484168404Spjd	return (0);
2485168404Spjd}
2486168404Spjd
2487168404Spjd/*
2488168404Spjd * Callback to print out the iostats for the given pool.
2489168404Spjd */
2490168404Spjdint
2491168404Spjdprint_iostat(zpool_handle_t *zhp, void *data)
2492168404Spjd{
2493168404Spjd	iostat_cbdata_t *cb = data;
2494168404Spjd	nvlist_t *oldconfig, *newconfig;
2495168404Spjd	nvlist_t *oldnvroot, *newnvroot;
2496168404Spjd
2497168404Spjd	newconfig = zpool_get_config(zhp, &oldconfig);
2498168404Spjd
2499168404Spjd	if (cb->cb_iteration == 1)
2500168404Spjd		oldconfig = NULL;
2501168404Spjd
2502168404Spjd	verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2503168404Spjd	    &newnvroot) == 0);
2504168404Spjd
2505168404Spjd	if (oldconfig == NULL)
2506168404Spjd		oldnvroot = NULL;
2507168404Spjd	else
2508168404Spjd		verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2509168404Spjd		    &oldnvroot) == 0);
2510168404Spjd
2511168404Spjd	/*
2512168404Spjd	 * Print out the statistics for the pool.
2513168404Spjd	 */
2514168404Spjd	print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2515168404Spjd
2516168404Spjd	if (cb->cb_verbose)
2517168404Spjd		print_iostat_separator(cb);
2518168404Spjd
2519168404Spjd	return (0);
2520168404Spjd}
2521168404Spjd
2522168404Spjdint
2523168404Spjdget_namewidth(zpool_handle_t *zhp, void *data)
2524168404Spjd{
2525168404Spjd	iostat_cbdata_t *cb = data;
2526168404Spjd	nvlist_t *config, *nvroot;
2527168404Spjd
2528168404Spjd	if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2529168404Spjd		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2530168404Spjd		    &nvroot) == 0);
2531168404Spjd		if (!cb->cb_verbose)
2532168404Spjd			cb->cb_namewidth = strlen(zpool_get_name(zhp));
2533168404Spjd		else
2534236145Smm			cb->cb_namewidth = max_width(zhp, nvroot, 0,
2535236145Smm			    cb->cb_namewidth);
2536168404Spjd	}
2537168404Spjd
2538168404Spjd	/*
2539168404Spjd	 * The width must fall into the range [10,38].  The upper limit is the
2540168404Spjd	 * maximum we can have and still fit in 80 columns.
2541168404Spjd	 */
2542168404Spjd	if (cb->cb_namewidth < 10)
2543168404Spjd		cb->cb_namewidth = 10;
2544168404Spjd	if (cb->cb_namewidth > 38)
2545168404Spjd		cb->cb_namewidth = 38;
2546168404Spjd
2547168404Spjd	return (0);
2548168404Spjd}
2549168404Spjd
2550168404Spjd/*
2551219089Spjd * Parse the input string, get the 'interval' and 'count' value if there is one.
2552168404Spjd */
2553219089Spjdstatic void
2554219089Spjdget_interval_count(int *argcp, char **argv, unsigned long *iv,
2555219089Spjd    unsigned long *cnt)
2556168404Spjd{
2557168404Spjd	unsigned long interval = 0, count = 0;
2558219089Spjd	int argc = *argcp, errno;
2559168404Spjd
2560168404Spjd	/*
2561168404Spjd	 * Determine if the last argument is an integer or a pool name
2562168404Spjd	 */
2563168404Spjd	if (argc > 0 && isdigit(argv[argc - 1][0])) {
2564168404Spjd		char *end;
2565168404Spjd
2566168404Spjd		errno = 0;
2567168404Spjd		interval = strtoul(argv[argc - 1], &end, 10);
2568168404Spjd
2569168404Spjd		if (*end == '\0' && errno == 0) {
2570168404Spjd			if (interval == 0) {
2571168404Spjd				(void) fprintf(stderr, gettext("interval "
2572168404Spjd				    "cannot be zero\n"));
2573168404Spjd				usage(B_FALSE);
2574168404Spjd			}
2575168404Spjd			/*
2576168404Spjd			 * Ignore the last parameter
2577168404Spjd			 */
2578168404Spjd			argc--;
2579168404Spjd		} else {
2580168404Spjd			/*
2581168404Spjd			 * If this is not a valid number, just plow on.  The
2582168404Spjd			 * user will get a more informative error message later
2583168404Spjd			 * on.
2584168404Spjd			 */
2585168404Spjd			interval = 0;
2586168404Spjd		}
2587168404Spjd	}
2588168404Spjd
2589168404Spjd	/*
2590168404Spjd	 * If the last argument is also an integer, then we have both a count
2591219089Spjd	 * and an interval.
2592168404Spjd	 */
2593168404Spjd	if (argc > 0 && isdigit(argv[argc - 1][0])) {
2594168404Spjd		char *end;
2595168404Spjd
2596168404Spjd		errno = 0;
2597168404Spjd		count = interval;
2598168404Spjd		interval = strtoul(argv[argc - 1], &end, 10);
2599168404Spjd
2600168404Spjd		if (*end == '\0' && errno == 0) {
2601168404Spjd			if (interval == 0) {
2602168404Spjd				(void) fprintf(stderr, gettext("interval "
2603168404Spjd				    "cannot be zero\n"));
2604168404Spjd				usage(B_FALSE);
2605168404Spjd			}
2606168404Spjd
2607168404Spjd			/*
2608168404Spjd			 * Ignore the last parameter
2609168404Spjd			 */
2610168404Spjd			argc--;
2611168404Spjd		} else {
2612168404Spjd			interval = 0;
2613168404Spjd		}
2614168404Spjd	}
2615168404Spjd
2616219089Spjd	*iv = interval;
2617219089Spjd	*cnt = count;
2618219089Spjd	*argcp = argc;
2619219089Spjd}
2620219089Spjd
2621219089Spjdstatic void
2622219089Spjdget_timestamp_arg(char c)
2623219089Spjd{
2624219089Spjd	if (c == 'u')
2625219089Spjd		timestamp_fmt = UDATE;
2626219089Spjd	else if (c == 'd')
2627219089Spjd		timestamp_fmt = DDATE;
2628219089Spjd	else
2629219089Spjd		usage(B_FALSE);
2630219089Spjd}
2631219089Spjd
2632219089Spjd/*
2633219089Spjd * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2634219089Spjd *
2635219089Spjd *	-v	Display statistics for individual vdevs
2636219089Spjd *	-T	Display a timestamp in date(1) or Unix format
2637219089Spjd *
2638219089Spjd * This command can be tricky because we want to be able to deal with pool
2639219089Spjd * creation/destruction as well as vdev configuration changes.  The bulk of this
2640219089Spjd * processing is handled by the pool_list_* routines in zpool_iter.c.  We rely
2641219089Spjd * on pool_list_update() to detect the addition of new pools.  Configuration
2642219089Spjd * changes are all handled within libzfs.
2643219089Spjd */
2644219089Spjdint
2645219089Spjdzpool_do_iostat(int argc, char **argv)
2646219089Spjd{
2647219089Spjd	int c;
2648219089Spjd	int ret;
2649219089Spjd	int npools;
2650219089Spjd	unsigned long interval = 0, count = 0;
2651219089Spjd	zpool_list_t *list;
2652219089Spjd	boolean_t verbose = B_FALSE;
2653219089Spjd	iostat_cbdata_t cb;
2654219089Spjd
2655219089Spjd	/* check options */
2656219089Spjd	while ((c = getopt(argc, argv, "T:v")) != -1) {
2657219089Spjd		switch (c) {
2658219089Spjd		case 'T':
2659219089Spjd			get_timestamp_arg(*optarg);
2660219089Spjd			break;
2661219089Spjd		case 'v':
2662219089Spjd			verbose = B_TRUE;
2663219089Spjd			break;
2664219089Spjd		case '?':
2665219089Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2666219089Spjd			    optopt);
2667219089Spjd			usage(B_FALSE);
2668219089Spjd		}
2669219089Spjd	}
2670219089Spjd
2671219089Spjd	argc -= optind;
2672219089Spjd	argv += optind;
2673219089Spjd
2674219089Spjd	get_interval_count(&argc, argv, &interval, &count);
2675219089Spjd
2676168404Spjd	/*
2677168404Spjd	 * Construct the list of all interesting pools.
2678168404Spjd	 */
2679168404Spjd	ret = 0;
2680168404Spjd	if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2681168404Spjd		return (1);
2682168404Spjd
2683168404Spjd	if (pool_list_count(list) == 0 && argc != 0) {
2684168404Spjd		pool_list_free(list);
2685168404Spjd		return (1);
2686168404Spjd	}
2687168404Spjd
2688168404Spjd	if (pool_list_count(list) == 0 && interval == 0) {
2689168404Spjd		pool_list_free(list);
2690168404Spjd		(void) fprintf(stderr, gettext("no pools available\n"));
2691168404Spjd		return (1);
2692168404Spjd	}
2693168404Spjd
2694168404Spjd	/*
2695168404Spjd	 * Enter the main iostat loop.
2696168404Spjd	 */
2697168404Spjd	cb.cb_list = list;
2698168404Spjd	cb.cb_verbose = verbose;
2699168404Spjd	cb.cb_iteration = 0;
2700168404Spjd	cb.cb_namewidth = 0;
2701168404Spjd
2702168404Spjd	for (;;) {
2703168404Spjd		pool_list_update(list);
2704168404Spjd
2705168404Spjd		if ((npools = pool_list_count(list)) == 0)
2706168404Spjd			break;
2707168404Spjd
2708168404Spjd		/*
2709168404Spjd		 * Refresh all statistics.  This is done as an explicit step
2710168404Spjd		 * before calculating the maximum name width, so that any
2711168404Spjd		 * configuration changes are properly accounted for.
2712168404Spjd		 */
2713168404Spjd		(void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2714168404Spjd
2715168404Spjd		/*
2716168404Spjd		 * Iterate over all pools to determine the maximum width
2717168404Spjd		 * for the pool / device name column across all pools.
2718168404Spjd		 */
2719168404Spjd		cb.cb_namewidth = 0;
2720168404Spjd		(void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2721168404Spjd
2722219089Spjd		if (timestamp_fmt != NODATE)
2723219089Spjd			print_timestamp(timestamp_fmt);
2724219089Spjd
2725168404Spjd		/*
2726168404Spjd		 * If it's the first time, or verbose mode, print the header.
2727168404Spjd		 */
2728168404Spjd		if (++cb.cb_iteration == 1 || verbose)
2729168404Spjd			print_iostat_header(&cb);
2730168404Spjd
2731168404Spjd		(void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2732168404Spjd
2733168404Spjd		/*
2734168404Spjd		 * If there's more than one pool, and we're not in verbose mode
2735168404Spjd		 * (which prints a separator for us), then print a separator.
2736168404Spjd		 */
2737168404Spjd		if (npools > 1 && !verbose)
2738168404Spjd			print_iostat_separator(&cb);
2739168404Spjd
2740168404Spjd		if (verbose)
2741168404Spjd			(void) printf("\n");
2742168404Spjd
2743168404Spjd		/*
2744168404Spjd		 * Flush the output so that redirection to a file isn't buffered
2745168404Spjd		 * indefinitely.
2746168404Spjd		 */
2747168404Spjd		(void) fflush(stdout);
2748168404Spjd
2749168404Spjd		if (interval == 0)
2750168404Spjd			break;
2751168404Spjd
2752168404Spjd		if (count != 0 && --count == 0)
2753168404Spjd			break;
2754168404Spjd
2755168404Spjd		(void) sleep(interval);
2756168404Spjd	}
2757168404Spjd
2758168404Spjd	pool_list_free(list);
2759168404Spjd
2760168404Spjd	return (ret);
2761168404Spjd}
2762168404Spjd
2763168404Spjdtypedef struct list_cbdata {
2764236155Smm	boolean_t	cb_verbose;
2765236155Smm	int		cb_namewidth;
2766168404Spjd	boolean_t	cb_scripted;
2767185029Spjd	zprop_list_t	*cb_proplist;
2768263889Sdelphij	boolean_t	cb_literal;
2769168404Spjd} list_cbdata_t;
2770168404Spjd
2771168404Spjd/*
2772168404Spjd * Given a list of columns to display, output appropriate headers for each one.
2773168404Spjd */
2774185029Spjdstatic void
2775236155Smmprint_header(list_cbdata_t *cb)
2776168404Spjd{
2777236155Smm	zprop_list_t *pl = cb->cb_proplist;
2778236884Smm	char headerbuf[ZPOOL_MAXPROPLEN];
2779185029Spjd	const char *header;
2780185029Spjd	boolean_t first = B_TRUE;
2781185029Spjd	boolean_t right_justify;
2782236155Smm	size_t width = 0;
2783168404Spjd
2784185029Spjd	for (; pl != NULL; pl = pl->pl_next) {
2785236155Smm		width = pl->pl_width;
2786236155Smm		if (first && cb->cb_verbose) {
2787236155Smm			/*
2788236155Smm			 * Reset the width to accommodate the verbose listing
2789236155Smm			 * of devices.
2790236155Smm			 */
2791236155Smm			width = cb->cb_namewidth;
2792236155Smm		}
2793236155Smm
2794185029Spjd		if (!first)
2795168404Spjd			(void) printf("  ");
2796168404Spjd		else
2797185029Spjd			first = B_FALSE;
2798168404Spjd
2799236884Smm		right_justify = B_FALSE;
2800236884Smm		if (pl->pl_prop != ZPROP_INVAL) {
2801236884Smm			header = zpool_prop_column_name(pl->pl_prop);
2802236884Smm			right_justify = zpool_prop_align_right(pl->pl_prop);
2803236884Smm		} else {
2804236884Smm			int i;
2805185029Spjd
2806236884Smm			for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2807236884Smm				headerbuf[i] = toupper(pl->pl_user_prop[i]);
2808236884Smm			headerbuf[i] = '\0';
2809236884Smm			header = headerbuf;
2810236884Smm		}
2811236884Smm
2812185029Spjd		if (pl->pl_next == NULL && !right_justify)
2813185029Spjd			(void) printf("%s", header);
2814185029Spjd		else if (right_justify)
2815236155Smm			(void) printf("%*s", width, header);
2816185029Spjd		else
2817236155Smm			(void) printf("%-*s", width, header);
2818236155Smm
2819168404Spjd	}
2820168404Spjd
2821168404Spjd	(void) printf("\n");
2822168404Spjd}
2823168404Spjd
2824185029Spjd/*
2825185029Spjd * Given a pool and a list of properties, print out all the properties according
2826185029Spjd * to the described layout.
2827185029Spjd */
2828185029Spjdstatic void
2829236155Smmprint_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2830168404Spjd{
2831236155Smm	zprop_list_t *pl = cb->cb_proplist;
2832185029Spjd	boolean_t first = B_TRUE;
2833185029Spjd	char property[ZPOOL_MAXPROPLEN];
2834185029Spjd	char *propstr;
2835185029Spjd	boolean_t right_justify;
2836236155Smm	size_t width;
2837168404Spjd
2838185029Spjd	for (; pl != NULL; pl = pl->pl_next) {
2839236155Smm
2840236155Smm		width = pl->pl_width;
2841236155Smm		if (first && cb->cb_verbose) {
2842236155Smm			/*
2843236155Smm			 * Reset the width to accommodate the verbose listing
2844236155Smm			 * of devices.
2845236155Smm			 */
2846236155Smm			width = cb->cb_namewidth;
2847236155Smm		}
2848236155Smm
2849185029Spjd		if (!first) {
2850236155Smm			if (cb->cb_scripted)
2851168404Spjd				(void) printf("\t");
2852168404Spjd			else
2853168404Spjd				(void) printf("  ");
2854185029Spjd		} else {
2855185029Spjd			first = B_FALSE;
2856168404Spjd		}
2857168404Spjd
2858185029Spjd		right_justify = B_FALSE;
2859185029Spjd		if (pl->pl_prop != ZPROP_INVAL) {
2860236155Smm			if (pl->pl_prop == ZPOOL_PROP_EXPANDSZ &&
2861236155Smm			    zpool_get_prop_int(zhp, pl->pl_prop, NULL) == 0)
2862236155Smm				propstr = "-";
2863236155Smm			else if (zpool_get_prop(zhp, pl->pl_prop, property,
2864263889Sdelphij			    sizeof (property), NULL, cb->cb_literal) != 0)
2865185029Spjd				propstr = "-";
2866168404Spjd			else
2867185029Spjd				propstr = property;
2868168404Spjd
2869185029Spjd			right_justify = zpool_prop_align_right(pl->pl_prop);
2870236884Smm		} else if ((zpool_prop_feature(pl->pl_user_prop) ||
2871236884Smm		    zpool_prop_unsupported(pl->pl_user_prop)) &&
2872236884Smm		    zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2873236884Smm		    sizeof (property)) == 0) {
2874236884Smm			propstr = property;
2875185029Spjd		} else {
2876185029Spjd			propstr = "-";
2877185029Spjd		}
2878168404Spjd
2879168404Spjd
2880185029Spjd		/*
2881185029Spjd		 * If this is being called in scripted mode, or if this is the
2882185029Spjd		 * last column and it is left-justified, don't include a width
2883185029Spjd		 * format specifier.
2884185029Spjd		 */
2885236155Smm		if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2886185029Spjd			(void) printf("%s", propstr);
2887185029Spjd		else if (right_justify)
2888185029Spjd			(void) printf("%*s", width, propstr);
2889185029Spjd		else
2890185029Spjd			(void) printf("%-*s", width, propstr);
2891185029Spjd	}
2892168404Spjd
2893185029Spjd	(void) printf("\n");
2894185029Spjd}
2895168404Spjd
2896236155Smmstatic void
2897236155Smmprint_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted)
2898236155Smm{
2899236155Smm	char propval[64];
2900236155Smm	boolean_t fixed;
2901236155Smm	size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
2902236155Smm
2903236155Smm
2904236155Smm	if (prop == ZPOOL_PROP_EXPANDSZ && value == 0)
2905236155Smm		(void) strlcpy(propval, "-", sizeof (propval));
2906269118Sdelphij	else if (prop == ZPOOL_PROP_FRAGMENTATION && value == ZFS_FRAG_INVALID)
2907269118Sdelphij		(void) strlcpy(propval, "-", sizeof (propval));
2908269118Sdelphij	else if (prop == ZPOOL_PROP_FRAGMENTATION)
2909269118Sdelphij		(void) snprintf(propval, sizeof (propval), "%llu%%", value);
2910269118Sdelphij	else
2911269118Sdelphij		zfs_nicenum(value, propval, sizeof (propval));
2912236155Smm
2913236155Smm	if (scripted)
2914236155Smm		(void) printf("\t%s", propval);
2915236155Smm	else
2916236155Smm		(void) printf("  %*s", width, propval);
2917236155Smm}
2918236155Smm
2919236155Smmvoid
2920236155Smmprint_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
2921236155Smm    list_cbdata_t *cb, int depth)
2922236155Smm{
2923236155Smm	nvlist_t **child;
2924236155Smm	vdev_stat_t *vs;
2925236155Smm	uint_t c, children;
2926236155Smm	char *vname;
2927236155Smm	boolean_t scripted = cb->cb_scripted;
2928236155Smm
2929236155Smm	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2930236155Smm	    (uint64_t **)&vs, &c) == 0);
2931236155Smm
2932236155Smm	if (name != NULL) {
2933236155Smm		if (scripted)
2934236155Smm			(void) printf("\t%s", name);
2935236155Smm		else if (strlen(name) + depth > cb->cb_namewidth)
2936236155Smm			(void) printf("%*s%s", depth, "", name);
2937236155Smm		else
2938236155Smm			(void) printf("%*s%s%*s", depth, "", name,
2939236155Smm			    (int)(cb->cb_namewidth - strlen(name) - depth), "");
2940236155Smm
2941236155Smm		/* only toplevel vdevs have capacity stats */
2942236155Smm		if (vs->vs_space == 0) {
2943236155Smm			if (scripted)
2944269118Sdelphij				(void) printf("\t-\t-\t-\t-");
2945236155Smm			else
2946269118Sdelphij				(void) printf("      -      -      -      -");
2947236155Smm		} else {
2948236155Smm			print_one_column(ZPOOL_PROP_SIZE, vs->vs_space,
2949236155Smm			    scripted);
2950236155Smm			print_one_column(ZPOOL_PROP_CAPACITY, vs->vs_alloc,
2951236155Smm			    scripted);
2952236155Smm			print_one_column(ZPOOL_PROP_FREE,
2953236155Smm			    vs->vs_space - vs->vs_alloc, scripted);
2954269118Sdelphij			print_one_column(ZPOOL_PROP_FRAGMENTATION,
2955269118Sdelphij			    vs->vs_fragmentation, scripted);
2956236155Smm		}
2957236155Smm		print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize,
2958236155Smm		    scripted);
2959236155Smm		(void) printf("\n");
2960236155Smm	}
2961236155Smm
2962236155Smm	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2963236155Smm	    &child, &children) != 0)
2964236155Smm		return;
2965236155Smm
2966236155Smm	for (c = 0; c < children; c++) {
2967236155Smm		uint64_t ishole = B_FALSE;
2968236155Smm
2969236155Smm		if (nvlist_lookup_uint64(child[c],
2970236155Smm		    ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
2971236155Smm			continue;
2972236155Smm
2973236155Smm		vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
2974236155Smm		print_list_stats(zhp, vname, child[c], cb, depth + 2);
2975236155Smm		free(vname);
2976236155Smm	}
2977236155Smm
2978236155Smm	/*
2979236155Smm	 * Include level 2 ARC devices in iostat output
2980236155Smm	 */
2981236155Smm	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2982236155Smm	    &child, &children) != 0)
2983236155Smm		return;
2984236155Smm
2985236155Smm	if (children > 0) {
2986236155Smm		(void) printf("%-*s      -      -      -      -      -      "
2987236155Smm		    "-\n", cb->cb_namewidth, "cache");
2988236155Smm		for (c = 0; c < children; c++) {
2989236155Smm			vname = zpool_vdev_name(g_zfs, zhp, child[c],
2990236155Smm			    B_FALSE);
2991236155Smm			print_list_stats(zhp, vname, child[c], cb, depth + 2);
2992236155Smm			free(vname);
2993236155Smm		}
2994236155Smm	}
2995236155Smm}
2996236155Smm
2997236155Smm
2998185029Spjd/*
2999185029Spjd * Generic callback function to list a pool.
3000185029Spjd */
3001185029Spjdint
3002185029Spjdlist_callback(zpool_handle_t *zhp, void *data)
3003185029Spjd{
3004185029Spjd	list_cbdata_t *cbp = data;
3005236155Smm	nvlist_t *config;
3006236155Smm	nvlist_t *nvroot;
3007168404Spjd
3008236155Smm	config = zpool_get_config(zhp, NULL);
3009168404Spjd
3010236155Smm	print_pool(zhp, cbp);
3011236155Smm	if (!cbp->cb_verbose)
3012236155Smm		return (0);
3013168404Spjd
3014236155Smm	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3015236155Smm	    &nvroot) == 0);
3016236155Smm	print_list_stats(zhp, NULL, nvroot, cbp, 0);
3017236155Smm
3018168404Spjd	return (0);
3019168404Spjd}
3020168404Spjd
3021168404Spjd/*
3022263889Sdelphij * zpool list [-Hp] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3023168404Spjd *
3024185029Spjd *	-H	Scripted mode.  Don't display headers, and separate properties
3025185029Spjd *		by a single tab.
3026185029Spjd *	-o	List of properties to display.  Defaults to
3027219089Spjd *		"name,size,allocated,free,capacity,health,altroot"
3028263889Sdelphij * 	-p	Diplay values in parsable (exact) format.
3029219089Spjd *	-T	Display a timestamp in date(1) or Unix format
3030168404Spjd *
3031168404Spjd * List all pools in the system, whether or not they're healthy.  Output space
3032168404Spjd * statistics for each one, as well as health status summary.
3033168404Spjd */
3034168404Spjdint
3035168404Spjdzpool_do_list(int argc, char **argv)
3036168404Spjd{
3037168404Spjd	int c;
3038168404Spjd	int ret;
3039168404Spjd	list_cbdata_t cb = { 0 };
3040185029Spjd	static char default_props[] =
3041269118Sdelphij	    "name,size,allocated,free,fragmentation,expandsize,capacity,"
3042269118Sdelphij	    "dedupratio,health,altroot";
3043185029Spjd	char *props = default_props;
3044219089Spjd	unsigned long interval = 0, count = 0;
3045236155Smm	zpool_list_t *list;
3046236155Smm	boolean_t first = B_TRUE;
3047168404Spjd
3048168404Spjd	/* check options */
3049263889Sdelphij	while ((c = getopt(argc, argv, ":Ho:pT:v")) != -1) {
3050168404Spjd		switch (c) {
3051168404Spjd		case 'H':
3052168404Spjd			cb.cb_scripted = B_TRUE;
3053168404Spjd			break;
3054168404Spjd		case 'o':
3055185029Spjd			props = optarg;
3056168404Spjd			break;
3057263889Sdelphij		case 'p':
3058263889Sdelphij			cb.cb_literal = B_TRUE;
3059263889Sdelphij			break;
3060219089Spjd		case 'T':
3061219089Spjd			get_timestamp_arg(*optarg);
3062219089Spjd			break;
3063236155Smm		case 'v':
3064236155Smm			cb.cb_verbose = B_TRUE;
3065236155Smm			break;
3066168404Spjd		case ':':
3067168404Spjd			(void) fprintf(stderr, gettext("missing argument for "
3068168404Spjd			    "'%c' option\n"), optopt);
3069168404Spjd			usage(B_FALSE);
3070168404Spjd			break;
3071168404Spjd		case '?':
3072168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3073168404Spjd			    optopt);
3074168404Spjd			usage(B_FALSE);
3075168404Spjd		}
3076168404Spjd	}
3077168404Spjd
3078168404Spjd	argc -= optind;
3079168404Spjd	argv += optind;
3080168404Spjd
3081219089Spjd	get_interval_count(&argc, argv, &interval, &count);
3082219089Spjd
3083185029Spjd	if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3084185029Spjd		usage(B_FALSE);
3085168404Spjd
3086219089Spjd	for (;;) {
3087268470Sdelphij		if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
3088268470Sdelphij		    &ret)) == NULL)
3089268470Sdelphij			return (1);
3090168404Spjd
3091236155Smm		if (pool_list_count(list) == 0)
3092236155Smm			break;
3093236155Smm
3094236155Smm		cb.cb_namewidth = 0;
3095236155Smm		(void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3096236155Smm
3097219089Spjd		if (timestamp_fmt != NODATE)
3098219089Spjd			print_timestamp(timestamp_fmt);
3099168404Spjd
3100236155Smm		if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3101236155Smm			print_header(&cb);
3102236155Smm			first = B_FALSE;
3103219089Spjd		}
3104236155Smm		ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
3105219089Spjd
3106219089Spjd		if (interval == 0)
3107219089Spjd			break;
3108219089Spjd
3109219089Spjd		if (count != 0 && --count == 0)
3110219089Spjd			break;
3111219089Spjd
3112268470Sdelphij		pool_list_free(list);
3113219089Spjd		(void) sleep(interval);
3114168404Spjd	}
3115168404Spjd
3116268470Sdelphij	if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3117268470Sdelphij		(void) printf(gettext("no pools available\n"));
3118268470Sdelphij		ret = 0;
3119268470Sdelphij	}
3120268470Sdelphij
3121268470Sdelphij	pool_list_free(list);
3122219089Spjd	zprop_free_list(cb.cb_proplist);
3123168404Spjd	return (ret);
3124168404Spjd}
3125168404Spjd
3126168404Spjdstatic nvlist_t *
3127168404Spjdzpool_get_vdev_by_name(nvlist_t *nv, char *name)
3128168404Spjd{
3129168404Spjd	nvlist_t **child;
3130168404Spjd	uint_t c, children;
3131168404Spjd	nvlist_t *match;
3132168404Spjd	char *path;
3133168404Spjd
3134168404Spjd	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3135168404Spjd	    &child, &children) != 0) {
3136168404Spjd		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
3137219089Spjd		if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3138219089Spjd			name += sizeof(_PATH_DEV) - 1;
3139219089Spjd		if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3140219089Spjd			path += sizeof(_PATH_DEV) - 1;
3141168404Spjd		if (strcmp(name, path) == 0)
3142168404Spjd			return (nv);
3143168404Spjd		return (NULL);
3144168404Spjd	}
3145168404Spjd
3146168404Spjd	for (c = 0; c < children; c++)
3147168404Spjd		if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
3148168404Spjd			return (match);
3149168404Spjd
3150168404Spjd	return (NULL);
3151168404Spjd}
3152168404Spjd
3153168404Spjdstatic int
3154168404Spjdzpool_do_attach_or_replace(int argc, char **argv, int replacing)
3155168404Spjd{
3156168404Spjd	boolean_t force = B_FALSE;
3157168404Spjd	int c;
3158168404Spjd	nvlist_t *nvroot;
3159168404Spjd	char *poolname, *old_disk, *new_disk;
3160168404Spjd	zpool_handle_t *zhp;
3161168404Spjd	int ret;
3162168404Spjd
3163168404Spjd	/* check options */
3164168404Spjd	while ((c = getopt(argc, argv, "f")) != -1) {
3165168404Spjd		switch (c) {
3166168404Spjd		case 'f':
3167168404Spjd			force = B_TRUE;
3168168404Spjd			break;
3169168404Spjd		case '?':
3170168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3171168404Spjd			    optopt);
3172168404Spjd			usage(B_FALSE);
3173168404Spjd		}
3174168404Spjd	}
3175168404Spjd
3176168404Spjd	argc -= optind;
3177168404Spjd	argv += optind;
3178168404Spjd
3179168404Spjd	/* get pool name and check number of arguments */
3180168404Spjd	if (argc < 1) {
3181168404Spjd		(void) fprintf(stderr, gettext("missing pool name argument\n"));
3182168404Spjd		usage(B_FALSE);
3183168404Spjd	}
3184168404Spjd
3185168404Spjd	poolname = argv[0];
3186168404Spjd
3187168404Spjd	if (argc < 2) {
3188168404Spjd		(void) fprintf(stderr,
3189168404Spjd		    gettext("missing <device> specification\n"));
3190168404Spjd		usage(B_FALSE);
3191168404Spjd	}
3192168404Spjd
3193168404Spjd	old_disk = argv[1];
3194168404Spjd
3195168404Spjd	if (argc < 3) {
3196168404Spjd		if (!replacing) {
3197168404Spjd			(void) fprintf(stderr,
3198168404Spjd			    gettext("missing <new_device> specification\n"));
3199168404Spjd			usage(B_FALSE);
3200168404Spjd		}
3201168404Spjd		new_disk = old_disk;
3202168404Spjd		argc -= 1;
3203168404Spjd		argv += 1;
3204168404Spjd	} else {
3205168404Spjd		new_disk = argv[2];
3206168404Spjd		argc -= 2;
3207168404Spjd		argv += 2;
3208168404Spjd	}
3209168404Spjd
3210168404Spjd	if (argc > 1) {
3211168404Spjd		(void) fprintf(stderr, gettext("too many arguments\n"));
3212168404Spjd		usage(B_FALSE);
3213168404Spjd	}
3214168404Spjd
3215168404Spjd	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3216168404Spjd		return (1);
3217168404Spjd
3218185029Spjd	if (zpool_get_config(zhp, NULL) == NULL) {
3219168404Spjd		(void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3220168404Spjd		    poolname);
3221168404Spjd		zpool_close(zhp);
3222168404Spjd		return (1);
3223168404Spjd	}
3224168404Spjd
3225185029Spjd	nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
3226185029Spjd	    argc, argv);
3227168404Spjd	if (nvroot == NULL) {
3228168404Spjd		zpool_close(zhp);
3229168404Spjd		return (1);
3230168404Spjd	}
3231168404Spjd
3232168404Spjd	ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3233168404Spjd
3234168404Spjd	nvlist_free(nvroot);
3235168404Spjd	zpool_close(zhp);
3236168404Spjd
3237168404Spjd	return (ret);
3238168404Spjd}
3239168404Spjd
3240168404Spjd/*
3241168404Spjd * zpool replace [-f] <pool> <device> <new_device>
3242168404Spjd *
3243168404Spjd *	-f	Force attach, even if <new_device> appears to be in use.
3244168404Spjd *
3245168404Spjd * Replace <device> with <new_device>.
3246168404Spjd */
3247168404Spjd/* ARGSUSED */
3248168404Spjdint
3249168404Spjdzpool_do_replace(int argc, char **argv)
3250168404Spjd{
3251168404Spjd	return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3252168404Spjd}
3253168404Spjd
3254168404Spjd/*
3255168404Spjd * zpool attach [-f] <pool> <device> <new_device>
3256168404Spjd *
3257168404Spjd *	-f	Force attach, even if <new_device> appears to be in use.
3258168404Spjd *
3259168404Spjd * Attach <new_device> to the mirror containing <device>.  If <device> is not
3260168404Spjd * part of a mirror, then <device> will be transformed into a mirror of
3261168404Spjd * <device> and <new_device>.  In either case, <new_device> will begin life
3262168404Spjd * with a DTL of [0, now], and will immediately begin to resilver itself.
3263168404Spjd */
3264168404Spjdint
3265168404Spjdzpool_do_attach(int argc, char **argv)
3266168404Spjd{
3267168404Spjd	return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3268168404Spjd}
3269168404Spjd
3270168404Spjd/*
3271168404Spjd * zpool detach [-f] <pool> <device>
3272168404Spjd *
3273168404Spjd *	-f	Force detach of <device>, even if DTLs argue against it
3274168404Spjd *		(not supported yet)
3275168404Spjd *
3276168404Spjd * Detach a device from a mirror.  The operation will be refused if <device>
3277168404Spjd * is the last device in the mirror, or if the DTLs indicate that this device
3278168404Spjd * has the only valid copy of some data.
3279168404Spjd */
3280168404Spjd/* ARGSUSED */
3281168404Spjdint
3282168404Spjdzpool_do_detach(int argc, char **argv)
3283168404Spjd{
3284168404Spjd	int c;
3285168404Spjd	char *poolname, *path;
3286168404Spjd	zpool_handle_t *zhp;
3287168404Spjd	int ret;
3288168404Spjd
3289168404Spjd	/* check options */
3290168404Spjd	while ((c = getopt(argc, argv, "f")) != -1) {
3291168404Spjd		switch (c) {
3292168404Spjd		case 'f':
3293168404Spjd		case '?':
3294168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3295168404Spjd			    optopt);
3296168404Spjd			usage(B_FALSE);
3297168404Spjd		}
3298168404Spjd	}
3299168404Spjd
3300168404Spjd	argc -= optind;
3301168404Spjd	argv += optind;
3302168404Spjd
3303168404Spjd	/* get pool name and check number of arguments */
3304168404Spjd	if (argc < 1) {
3305168404Spjd		(void) fprintf(stderr, gettext("missing pool name argument\n"));
3306168404Spjd		usage(B_FALSE);
3307168404Spjd	}
3308168404Spjd
3309168404Spjd	if (argc < 2) {
3310168404Spjd		(void) fprintf(stderr,
3311168404Spjd		    gettext("missing <device> specification\n"));
3312168404Spjd		usage(B_FALSE);
3313168404Spjd	}
3314168404Spjd
3315168404Spjd	poolname = argv[0];
3316168404Spjd	path = argv[1];
3317168404Spjd
3318168404Spjd	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3319168404Spjd		return (1);
3320168404Spjd
3321168404Spjd	ret = zpool_vdev_detach(zhp, path);
3322168404Spjd
3323168404Spjd	zpool_close(zhp);
3324168404Spjd
3325168404Spjd	return (ret);
3326168404Spjd}
3327168404Spjd
3328168404Spjd/*
3329219089Spjd * zpool split [-n] [-o prop=val] ...
3330219089Spjd *		[-o mntopt] ...
3331219089Spjd *		[-R altroot] <pool> <newpool> [<device> ...]
3332219089Spjd *
3333219089Spjd *	-n	Do not split the pool, but display the resulting layout if
3334219089Spjd *		it were to be split.
3335219089Spjd *	-o	Set property=value, or set mount options.
3336219089Spjd *	-R	Mount the split-off pool under an alternate root.
3337219089Spjd *
3338219089Spjd * Splits the named pool and gives it the new pool name.  Devices to be split
3339219089Spjd * off may be listed, provided that no more than one device is specified
3340219089Spjd * per top-level vdev mirror.  The newly split pool is left in an exported
3341219089Spjd * state unless -R is specified.
3342219089Spjd *
3343219089Spjd * Restrictions: the top-level of the pool pool must only be made up of
3344219089Spjd * mirrors; all devices in the pool must be healthy; no device may be
3345219089Spjd * undergoing a resilvering operation.
3346219089Spjd */
3347219089Spjdint
3348219089Spjdzpool_do_split(int argc, char **argv)
3349219089Spjd{
3350219089Spjd	char *srcpool, *newpool, *propval;
3351219089Spjd	char *mntopts = NULL;
3352219089Spjd	splitflags_t flags;
3353219089Spjd	int c, ret = 0;
3354219089Spjd	zpool_handle_t *zhp;
3355219089Spjd	nvlist_t *config, *props = NULL;
3356219089Spjd
3357219089Spjd	flags.dryrun = B_FALSE;
3358219089Spjd	flags.import = B_FALSE;
3359219089Spjd
3360219089Spjd	/* check options */
3361219089Spjd	while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3362219089Spjd		switch (c) {
3363219089Spjd		case 'R':
3364219089Spjd			flags.import = B_TRUE;
3365219089Spjd			if (add_prop_list(
3366219089Spjd			    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3367219089Spjd			    &props, B_TRUE) != 0) {
3368219089Spjd				if (props)
3369219089Spjd					nvlist_free(props);
3370219089Spjd				usage(B_FALSE);
3371219089Spjd			}
3372219089Spjd			break;
3373219089Spjd		case 'n':
3374219089Spjd			flags.dryrun = B_TRUE;
3375219089Spjd			break;
3376219089Spjd		case 'o':
3377219089Spjd			if ((propval = strchr(optarg, '=')) != NULL) {
3378219089Spjd				*propval = '\0';
3379219089Spjd				propval++;
3380219089Spjd				if (add_prop_list(optarg, propval,
3381219089Spjd				    &props, B_TRUE) != 0) {
3382219089Spjd					if (props)
3383219089Spjd						nvlist_free(props);
3384219089Spjd					usage(B_FALSE);
3385219089Spjd				}
3386219089Spjd			} else {
3387219089Spjd				mntopts = optarg;
3388219089Spjd			}
3389219089Spjd			break;
3390219089Spjd		case ':':
3391219089Spjd			(void) fprintf(stderr, gettext("missing argument for "
3392219089Spjd			    "'%c' option\n"), optopt);
3393219089Spjd			usage(B_FALSE);
3394219089Spjd			break;
3395219089Spjd		case '?':
3396219089Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3397219089Spjd			    optopt);
3398219089Spjd			usage(B_FALSE);
3399219089Spjd			break;
3400219089Spjd		}
3401219089Spjd	}
3402219089Spjd
3403219089Spjd	if (!flags.import && mntopts != NULL) {
3404219089Spjd		(void) fprintf(stderr, gettext("setting mntopts is only "
3405219089Spjd		    "valid when importing the pool\n"));
3406219089Spjd		usage(B_FALSE);
3407219089Spjd	}
3408219089Spjd
3409219089Spjd	argc -= optind;
3410219089Spjd	argv += optind;
3411219089Spjd
3412219089Spjd	if (argc < 1) {
3413219089Spjd		(void) fprintf(stderr, gettext("Missing pool name\n"));
3414219089Spjd		usage(B_FALSE);
3415219089Spjd	}
3416219089Spjd	if (argc < 2) {
3417219089Spjd		(void) fprintf(stderr, gettext("Missing new pool name\n"));
3418219089Spjd		usage(B_FALSE);
3419219089Spjd	}
3420219089Spjd
3421219089Spjd	srcpool = argv[0];
3422219089Spjd	newpool = argv[1];
3423219089Spjd
3424219089Spjd	argc -= 2;
3425219089Spjd	argv += 2;
3426219089Spjd
3427219089Spjd	if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3428219089Spjd		return (1);
3429219089Spjd
3430219089Spjd	config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3431219089Spjd	if (config == NULL) {
3432219089Spjd		ret = 1;
3433219089Spjd	} else {
3434219089Spjd		if (flags.dryrun) {
3435219089Spjd			(void) printf(gettext("would create '%s' with the "
3436219089Spjd			    "following layout:\n\n"), newpool);
3437219089Spjd			print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3438219089Spjd		}
3439219089Spjd		nvlist_free(config);
3440219089Spjd	}
3441219089Spjd
3442219089Spjd	zpool_close(zhp);
3443219089Spjd
3444219089Spjd	if (ret != 0 || flags.dryrun || !flags.import)
3445219089Spjd		return (ret);
3446219089Spjd
3447219089Spjd	/*
3448219089Spjd	 * The split was successful. Now we need to open the new
3449219089Spjd	 * pool and import it.
3450219089Spjd	 */
3451219089Spjd	if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3452219089Spjd		return (1);
3453219089Spjd	if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3454219089Spjd	    zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3455219089Spjd		ret = 1;
3456240415Smm		(void) fprintf(stderr, gettext("Split was successful, but "
3457219089Spjd		    "the datasets could not all be mounted\n"));
3458219089Spjd		(void) fprintf(stderr, gettext("Try doing '%s' with a "
3459219089Spjd		    "different altroot\n"), "zpool import");
3460219089Spjd	}
3461219089Spjd	zpool_close(zhp);
3462219089Spjd
3463219089Spjd	return (ret);
3464219089Spjd}
3465219089Spjd
3466219089Spjd
3467219089Spjd
3468219089Spjd/*
3469168404Spjd * zpool online <pool> <device> ...
3470168404Spjd */
3471168404Spjdint
3472168404Spjdzpool_do_online(int argc, char **argv)
3473168404Spjd{
3474168404Spjd	int c, i;
3475168404Spjd	char *poolname;
3476168404Spjd	zpool_handle_t *zhp;
3477168404Spjd	int ret = 0;
3478185029Spjd	vdev_state_t newstate;
3479219089Spjd	int flags = 0;
3480168404Spjd
3481168404Spjd	/* check options */
3482219089Spjd	while ((c = getopt(argc, argv, "et")) != -1) {
3483168404Spjd		switch (c) {
3484219089Spjd		case 'e':
3485219089Spjd			flags |= ZFS_ONLINE_EXPAND;
3486219089Spjd			break;
3487168404Spjd		case 't':
3488168404Spjd		case '?':
3489168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3490168404Spjd			    optopt);
3491168404Spjd			usage(B_FALSE);
3492168404Spjd		}
3493168404Spjd	}
3494168404Spjd
3495168404Spjd	argc -= optind;
3496168404Spjd	argv += optind;
3497168404Spjd
3498168404Spjd	/* get pool name and check number of arguments */
3499168404Spjd	if (argc < 1) {
3500168404Spjd		(void) fprintf(stderr, gettext("missing pool name\n"));
3501168404Spjd		usage(B_FALSE);
3502168404Spjd	}
3503168404Spjd	if (argc < 2) {
3504168404Spjd		(void) fprintf(stderr, gettext("missing device name\n"));
3505168404Spjd		usage(B_FALSE);
3506168404Spjd	}
3507168404Spjd
3508168404Spjd	poolname = argv[0];
3509168404Spjd
3510168404Spjd	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3511168404Spjd		return (1);
3512168404Spjd
3513185029Spjd	for (i = 1; i < argc; i++) {
3514219089Spjd		if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3515185029Spjd			if (newstate != VDEV_STATE_HEALTHY) {
3516185029Spjd				(void) printf(gettext("warning: device '%s' "
3517185029Spjd				    "onlined, but remains in faulted state\n"),
3518185029Spjd				    argv[i]);
3519185029Spjd				if (newstate == VDEV_STATE_FAULTED)
3520185029Spjd					(void) printf(gettext("use 'zpool "
3521185029Spjd					    "clear' to restore a faulted "
3522185029Spjd					    "device\n"));
3523185029Spjd				else
3524185029Spjd					(void) printf(gettext("use 'zpool "
3525185029Spjd					    "replace' to replace devices "
3526185029Spjd					    "that are no longer present\n"));
3527185029Spjd			}
3528185029Spjd		} else {
3529168404Spjd			ret = 1;
3530185029Spjd		}
3531185029Spjd	}
3532168404Spjd
3533168404Spjd	zpool_close(zhp);
3534168404Spjd
3535168404Spjd	return (ret);
3536168404Spjd}
3537168404Spjd
3538168404Spjd/*
3539168404Spjd * zpool offline [-ft] <pool> <device> ...
3540168404Spjd *
3541168404Spjd *	-f	Force the device into the offline state, even if doing
3542168404Spjd *		so would appear to compromise pool availability.
3543168404Spjd *		(not supported yet)
3544168404Spjd *
3545168404Spjd *	-t	Only take the device off-line temporarily.  The offline
3546168404Spjd *		state will not be persistent across reboots.
3547168404Spjd */
3548168404Spjd/* ARGSUSED */
3549168404Spjdint
3550168404Spjdzpool_do_offline(int argc, char **argv)
3551168404Spjd{
3552168404Spjd	int c, i;
3553168404Spjd	char *poolname;
3554168404Spjd	zpool_handle_t *zhp;
3555168404Spjd	int ret = 0;
3556168404Spjd	boolean_t istmp = B_FALSE;
3557168404Spjd
3558168404Spjd	/* check options */
3559168404Spjd	while ((c = getopt(argc, argv, "ft")) != -1) {
3560168404Spjd		switch (c) {
3561168404Spjd		case 't':
3562168404Spjd			istmp = B_TRUE;
3563168404Spjd			break;
3564168404Spjd		case 'f':
3565168404Spjd		case '?':
3566168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3567168404Spjd			    optopt);
3568168404Spjd			usage(B_FALSE);
3569168404Spjd		}
3570168404Spjd	}
3571168404Spjd
3572168404Spjd	argc -= optind;
3573168404Spjd	argv += optind;
3574168404Spjd
3575168404Spjd	/* get pool name and check number of arguments */
3576168404Spjd	if (argc < 1) {
3577168404Spjd		(void) fprintf(stderr, gettext("missing pool name\n"));
3578168404Spjd		usage(B_FALSE);
3579168404Spjd	}
3580168404Spjd	if (argc < 2) {
3581168404Spjd		(void) fprintf(stderr, gettext("missing device name\n"));
3582168404Spjd		usage(B_FALSE);
3583168404Spjd	}
3584168404Spjd
3585168404Spjd	poolname = argv[0];
3586168404Spjd
3587168404Spjd	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3588168404Spjd		return (1);
3589168404Spjd
3590185029Spjd	for (i = 1; i < argc; i++) {
3591185029Spjd		if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3592168404Spjd			ret = 1;
3593185029Spjd	}
3594168404Spjd
3595168404Spjd	zpool_close(zhp);
3596168404Spjd
3597168404Spjd	return (ret);
3598168404Spjd}
3599168404Spjd
3600168404Spjd/*
3601168404Spjd * zpool clear <pool> [device]
3602168404Spjd *
3603168404Spjd * Clear all errors associated with a pool or a particular device.
3604168404Spjd */
3605168404Spjdint
3606168404Spjdzpool_do_clear(int argc, char **argv)
3607168404Spjd{
3608219089Spjd	int c;
3609168404Spjd	int ret = 0;
3610219089Spjd	boolean_t dryrun = B_FALSE;
3611219089Spjd	boolean_t do_rewind = B_FALSE;
3612219089Spjd	boolean_t xtreme_rewind = B_FALSE;
3613219089Spjd	uint32_t rewind_policy = ZPOOL_NO_REWIND;
3614219089Spjd	nvlist_t *policy = NULL;
3615168404Spjd	zpool_handle_t *zhp;
3616168404Spjd	char *pool, *device;
3617168404Spjd
3618219089Spjd	/* check options */
3619219089Spjd	while ((c = getopt(argc, argv, "FnX")) != -1) {
3620219089Spjd		switch (c) {
3621219089Spjd		case 'F':
3622219089Spjd			do_rewind = B_TRUE;
3623219089Spjd			break;
3624219089Spjd		case 'n':
3625219089Spjd			dryrun = B_TRUE;
3626219089Spjd			break;
3627219089Spjd		case 'X':
3628219089Spjd			xtreme_rewind = B_TRUE;
3629219089Spjd			break;
3630219089Spjd		case '?':
3631219089Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3632219089Spjd			    optopt);
3633219089Spjd			usage(B_FALSE);
3634219089Spjd		}
3635219089Spjd	}
3636219089Spjd
3637219089Spjd	argc -= optind;
3638219089Spjd	argv += optind;
3639219089Spjd
3640219089Spjd	if (argc < 1) {
3641168404Spjd		(void) fprintf(stderr, gettext("missing pool name\n"));
3642168404Spjd		usage(B_FALSE);
3643168404Spjd	}
3644168404Spjd
3645219089Spjd	if (argc > 2) {
3646168404Spjd		(void) fprintf(stderr, gettext("too many arguments\n"));
3647168404Spjd		usage(B_FALSE);
3648168404Spjd	}
3649168404Spjd
3650219089Spjd	if ((dryrun || xtreme_rewind) && !do_rewind) {
3651219089Spjd		(void) fprintf(stderr,
3652219089Spjd		    gettext("-n or -X only meaningful with -F\n"));
3653219089Spjd		usage(B_FALSE);
3654219089Spjd	}
3655219089Spjd	if (dryrun)
3656219089Spjd		rewind_policy = ZPOOL_TRY_REWIND;
3657219089Spjd	else if (do_rewind)
3658219089Spjd		rewind_policy = ZPOOL_DO_REWIND;
3659219089Spjd	if (xtreme_rewind)
3660219089Spjd		rewind_policy |= ZPOOL_EXTREME_REWIND;
3661168404Spjd
3662219089Spjd	/* In future, further rewind policy choices can be passed along here */
3663219089Spjd	if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3664219089Spjd	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3665168404Spjd		return (1);
3666168404Spjd
3667219089Spjd	pool = argv[0];
3668219089Spjd	device = argc == 2 ? argv[1] : NULL;
3669219089Spjd
3670219089Spjd	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3671219089Spjd		nvlist_free(policy);
3672219089Spjd		return (1);
3673219089Spjd	}
3674219089Spjd
3675219089Spjd	if (zpool_clear(zhp, device, policy) != 0)
3676168404Spjd		ret = 1;
3677168404Spjd
3678168404Spjd	zpool_close(zhp);
3679168404Spjd
3680219089Spjd	nvlist_free(policy);
3681219089Spjd
3682168404Spjd	return (ret);
3683168404Spjd}
3684168404Spjd
3685228103Smm/*
3686228103Smm * zpool reguid <pool>
3687228103Smm */
3688228103Smmint
3689228103Smmzpool_do_reguid(int argc, char **argv)
3690228103Smm{
3691228103Smm	int c;
3692228103Smm	char *poolname;
3693228103Smm	zpool_handle_t *zhp;
3694228103Smm	int ret = 0;
3695228103Smm
3696228103Smm	/* check options */
3697228103Smm	while ((c = getopt(argc, argv, "")) != -1) {
3698228103Smm		switch (c) {
3699228103Smm		case '?':
3700228103Smm			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3701228103Smm			    optopt);
3702228103Smm			usage(B_FALSE);
3703228103Smm		}
3704228103Smm	}
3705228103Smm
3706228103Smm	argc -= optind;
3707228103Smm	argv += optind;
3708228103Smm
3709228103Smm	/* get pool name and check number of arguments */
3710228103Smm	if (argc < 1) {
3711228103Smm		(void) fprintf(stderr, gettext("missing pool name\n"));
3712228103Smm		usage(B_FALSE);
3713228103Smm	}
3714228103Smm
3715228103Smm	if (argc > 1) {
3716228103Smm		(void) fprintf(stderr, gettext("too many arguments\n"));
3717228103Smm		usage(B_FALSE);
3718228103Smm	}
3719228103Smm
3720228103Smm	poolname = argv[0];
3721228103Smm	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3722228103Smm		return (1);
3723228103Smm
3724228103Smm	ret = zpool_reguid(zhp);
3725228103Smm
3726228103Smm	zpool_close(zhp);
3727228103Smm	return (ret);
3728228103Smm}
3729228103Smm
3730228103Smm
3731236155Smm/*
3732236155Smm * zpool reopen <pool>
3733236155Smm *
3734236155Smm * Reopen the pool so that the kernel can update the sizes of all vdevs.
3735236155Smm */
3736236155Smmint
3737236155Smmzpool_do_reopen(int argc, char **argv)
3738236155Smm{
3739260138Sdelphij	int c;
3740236155Smm	int ret = 0;
3741236155Smm	zpool_handle_t *zhp;
3742236155Smm	char *pool;
3743236155Smm
3744260138Sdelphij	/* check options */
3745260138Sdelphij	while ((c = getopt(argc, argv, "")) != -1) {
3746260138Sdelphij		switch (c) {
3747260138Sdelphij		case '?':
3748260138Sdelphij			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3749260138Sdelphij			    optopt);
3750260138Sdelphij			usage(B_FALSE);
3751260138Sdelphij		}
3752260138Sdelphij	}
3753260138Sdelphij
3754236155Smm	argc--;
3755236155Smm	argv++;
3756236155Smm
3757260138Sdelphij	if (argc < 1) {
3758260138Sdelphij		(void) fprintf(stderr, gettext("missing pool name\n"));
3759260138Sdelphij		usage(B_FALSE);
3760260138Sdelphij	}
3761236155Smm
3762260138Sdelphij	if (argc > 1) {
3763260138Sdelphij		(void) fprintf(stderr, gettext("too many arguments\n"));
3764260138Sdelphij		usage(B_FALSE);
3765260138Sdelphij	}
3766260138Sdelphij
3767236155Smm	pool = argv[0];
3768236155Smm	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3769236155Smm		return (1);
3770236155Smm
3771236155Smm	ret = zpool_reopen(zhp);
3772236155Smm	zpool_close(zhp);
3773236155Smm	return (ret);
3774236155Smm}
3775236155Smm
3776168404Spjdtypedef struct scrub_cbdata {
3777168404Spjd	int	cb_type;
3778168404Spjd	int	cb_argc;
3779168404Spjd	char	**cb_argv;
3780168404Spjd} scrub_cbdata_t;
3781168404Spjd
3782168404Spjdint
3783168404Spjdscrub_callback(zpool_handle_t *zhp, void *data)
3784168404Spjd{
3785168404Spjd	scrub_cbdata_t *cb = data;
3786168404Spjd	int err;
3787168404Spjd
3788168404Spjd	/*
3789168404Spjd	 * Ignore faulted pools.
3790168404Spjd	 */
3791168404Spjd	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3792168404Spjd		(void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3793168404Spjd		    "currently unavailable\n"), zpool_get_name(zhp));
3794168404Spjd		return (1);
3795168404Spjd	}
3796168404Spjd
3797219089Spjd	err = zpool_scan(zhp, cb->cb_type);
3798168404Spjd
3799168404Spjd	return (err != 0);
3800168404Spjd}
3801168404Spjd
3802168404Spjd/*
3803168404Spjd * zpool scrub [-s] <pool> ...
3804168404Spjd *
3805168404Spjd *	-s	Stop.  Stops any in-progress scrub.
3806168404Spjd */
3807168404Spjdint
3808168404Spjdzpool_do_scrub(int argc, char **argv)
3809168404Spjd{
3810168404Spjd	int c;
3811168404Spjd	scrub_cbdata_t cb;
3812168404Spjd
3813219089Spjd	cb.cb_type = POOL_SCAN_SCRUB;
3814168404Spjd
3815168404Spjd	/* check options */
3816168404Spjd	while ((c = getopt(argc, argv, "s")) != -1) {
3817168404Spjd		switch (c) {
3818168404Spjd		case 's':
3819219089Spjd			cb.cb_type = POOL_SCAN_NONE;
3820168404Spjd			break;
3821168404Spjd		case '?':
3822168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3823168404Spjd			    optopt);
3824168404Spjd			usage(B_FALSE);
3825168404Spjd		}
3826168404Spjd	}
3827168404Spjd
3828168404Spjd	cb.cb_argc = argc;
3829168404Spjd	cb.cb_argv = argv;
3830168404Spjd	argc -= optind;
3831168404Spjd	argv += optind;
3832168404Spjd
3833168404Spjd	if (argc < 1) {
3834168404Spjd		(void) fprintf(stderr, gettext("missing pool name argument\n"));
3835168404Spjd		usage(B_FALSE);
3836168404Spjd	}
3837168404Spjd
3838168404Spjd	return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3839168404Spjd}
3840168404Spjd
3841168404Spjdtypedef struct status_cbdata {
3842168404Spjd	int		cb_count;
3843168404Spjd	boolean_t	cb_allpools;
3844168404Spjd	boolean_t	cb_verbose;
3845168404Spjd	boolean_t	cb_explain;
3846168404Spjd	boolean_t	cb_first;
3847219089Spjd	boolean_t	cb_dedup_stats;
3848168404Spjd} status_cbdata_t;
3849168404Spjd
3850168404Spjd/*
3851168404Spjd * Print out detailed scrub status.
3852168404Spjd */
3853168404Spjdvoid
3854219089Spjdprint_scan_status(pool_scan_stat_t *ps)
3855168404Spjd{
3856219089Spjd	time_t start, end;
3857219089Spjd	uint64_t elapsed, mins_left, hours_left;
3858219089Spjd	uint64_t pass_exam, examined, total;
3859219089Spjd	uint_t rate;
3860168404Spjd	double fraction_done;
3861219089Spjd	char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3862168404Spjd
3863226583Spjd	(void) printf(gettext("  scan: "));
3864168404Spjd
3865219089Spjd	/* If there's never been a scan, there's not much to say. */
3866219089Spjd	if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3867219089Spjd	    ps->pss_func >= POOL_SCAN_FUNCS) {
3868168404Spjd		(void) printf(gettext("none requested\n"));
3869168404Spjd		return;
3870168404Spjd	}
3871168404Spjd
3872219089Spjd	start = ps->pss_start_time;
3873219089Spjd	end = ps->pss_end_time;
3874219089Spjd	zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3875168404Spjd
3876219089Spjd	assert(ps->pss_func == POOL_SCAN_SCRUB ||
3877219089Spjd	    ps->pss_func == POOL_SCAN_RESILVER);
3878219089Spjd	/*
3879219089Spjd	 * Scan is finished or canceled.
3880219089Spjd	 */
3881219089Spjd	if (ps->pss_state == DSS_FINISHED) {
3882219089Spjd		uint64_t minutes_taken = (end - start) / 60;
3883219089Spjd		char *fmt;
3884168404Spjd
3885219089Spjd		if (ps->pss_func == POOL_SCAN_SCRUB) {
3886219089Spjd			fmt = gettext("scrub repaired %s in %lluh%um with "
3887219089Spjd			    "%llu errors on %s");
3888219089Spjd		} else if (ps->pss_func == POOL_SCAN_RESILVER) {
3889219089Spjd			fmt = gettext("resilvered %s in %lluh%um with "
3890219089Spjd			    "%llu errors on %s");
3891219089Spjd		}
3892219089Spjd		/* LINTED */
3893219089Spjd		(void) printf(fmt, processed_buf,
3894185029Spjd		    (u_longlong_t)(minutes_taken / 60),
3895185029Spjd		    (uint_t)(minutes_taken % 60),
3896219089Spjd		    (u_longlong_t)ps->pss_errors,
3897219089Spjd		    ctime((time_t *)&end));
3898168404Spjd		return;
3899219089Spjd	} else if (ps->pss_state == DSS_CANCELED) {
3900219089Spjd		if (ps->pss_func == POOL_SCAN_SCRUB) {
3901219089Spjd			(void) printf(gettext("scrub canceled on %s"),
3902219089Spjd			    ctime(&end));
3903219089Spjd		} else if (ps->pss_func == POOL_SCAN_RESILVER) {
3904219089Spjd			(void) printf(gettext("resilver canceled on %s"),
3905219089Spjd			    ctime(&end));
3906219089Spjd		}
3907219089Spjd		return;
3908168404Spjd	}
3909168404Spjd
3910219089Spjd	assert(ps->pss_state == DSS_SCANNING);
3911168404Spjd
3912219089Spjd	/*
3913219089Spjd	 * Scan is in progress.
3914219089Spjd	 */
3915219089Spjd	if (ps->pss_func == POOL_SCAN_SCRUB) {
3916219089Spjd		(void) printf(gettext("scrub in progress since %s"),
3917219089Spjd		    ctime(&start));
3918219089Spjd	} else if (ps->pss_func == POOL_SCAN_RESILVER) {
3919219089Spjd		(void) printf(gettext("resilver in progress since %s"),
3920219089Spjd		    ctime(&start));
3921219089Spjd	}
3922219089Spjd
3923219089Spjd	examined = ps->pss_examined ? ps->pss_examined : 1;
3924219089Spjd	total = ps->pss_to_examine;
3925168404Spjd	fraction_done = (double)examined / total;
3926168404Spjd
3927219089Spjd	/* elapsed time for this pass */
3928219089Spjd	elapsed = time(NULL) - ps->pss_pass_start;
3929219089Spjd	elapsed = elapsed ? elapsed : 1;
3930219089Spjd	pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3931219089Spjd	rate = pass_exam / elapsed;
3932219089Spjd	rate = rate ? rate : 1;
3933219089Spjd	mins_left = ((total - examined) / rate) / 60;
3934219089Spjd	hours_left = mins_left / 60;
3935219089Spjd
3936219089Spjd	zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3937219089Spjd	zfs_nicenum(total, total_buf, sizeof (total_buf));
3938219089Spjd	zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3939219089Spjd
3940219089Spjd	/*
3941219089Spjd	 * do not print estimated time if hours_left is more than 30 days
3942219089Spjd	 */
3943226583Spjd	(void) printf(gettext("        %s scanned out of %s at %s/s"),
3944219089Spjd	    examined_buf, total_buf, rate_buf);
3945219089Spjd	if (hours_left < (30 * 24)) {
3946219089Spjd		(void) printf(gettext(", %lluh%um to go\n"),
3947219089Spjd		    (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3948219089Spjd	} else {
3949219089Spjd		(void) printf(gettext(
3950219089Spjd		    ", (scan is slow, no estimated time)\n"));
3951219089Spjd	}
3952219089Spjd
3953219089Spjd	if (ps->pss_func == POOL_SCAN_RESILVER) {
3954226583Spjd		(void) printf(gettext("        %s resilvered, %.2f%% done\n"),
3955219089Spjd		    processed_buf, 100 * fraction_done);
3956219089Spjd	} else if (ps->pss_func == POOL_SCAN_SCRUB) {
3957226583Spjd		(void) printf(gettext("        %s repaired, %.2f%% done\n"),
3958219089Spjd		    processed_buf, 100 * fraction_done);
3959219089Spjd	}
3960168404Spjd}
3961168404Spjd
3962168404Spjdstatic void
3963168404Spjdprint_error_log(zpool_handle_t *zhp)
3964168404Spjd{
3965185029Spjd	nvlist_t *nverrlist = NULL;
3966168404Spjd	nvpair_t *elem;
3967168404Spjd	char *pathname;
3968168404Spjd	size_t len = MAXPATHLEN * 2;
3969168404Spjd
3970168404Spjd	if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3971168404Spjd		(void) printf("errors: List of errors unavailable "
3972168404Spjd		    "(insufficient privileges)\n");
3973168404Spjd		return;
3974168404Spjd	}
3975168404Spjd
3976168404Spjd	(void) printf("errors: Permanent errors have been "
3977168404Spjd	    "detected in the following files:\n\n");
3978168404Spjd
3979168404Spjd	pathname = safe_malloc(len);
3980168404Spjd	elem = NULL;
3981168404Spjd	while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
3982168404Spjd		nvlist_t *nv;
3983168404Spjd		uint64_t dsobj, obj;
3984168404Spjd
3985168404Spjd		verify(nvpair_value_nvlist(elem, &nv) == 0);
3986168404Spjd		verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
3987168404Spjd		    &dsobj) == 0);
3988168404Spjd		verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
3989168404Spjd		    &obj) == 0);
3990168404Spjd		zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
3991168404Spjd		(void) printf("%7s %s\n", "", pathname);
3992168404Spjd	}
3993168404Spjd	free(pathname);
3994168404Spjd	nvlist_free(nverrlist);
3995168404Spjd}
3996168404Spjd
3997168404Spjdstatic void
3998168404Spjdprint_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
3999168404Spjd    int namewidth)
4000168404Spjd{
4001168404Spjd	uint_t i;
4002168404Spjd	char *name;
4003168404Spjd
4004168404Spjd	if (nspares == 0)
4005168404Spjd		return;
4006168404Spjd
4007168404Spjd	(void) printf(gettext("\tspares\n"));
4008168404Spjd
4009168404Spjd	for (i = 0; i < nspares; i++) {
4010219089Spjd		name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
4011168404Spjd		print_status_config(zhp, name, spares[i],
4012209962Smm		    namewidth, 2, B_TRUE);
4013168404Spjd		free(name);
4014168404Spjd	}
4015168404Spjd}
4016168404Spjd
4017185029Spjdstatic void
4018185029Spjdprint_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
4019185029Spjd    int namewidth)
4020185029Spjd{
4021185029Spjd	uint_t i;
4022185029Spjd	char *name;
4023185029Spjd
4024185029Spjd	if (nl2cache == 0)
4025185029Spjd		return;
4026185029Spjd
4027185029Spjd	(void) printf(gettext("\tcache\n"));
4028185029Spjd
4029185029Spjd	for (i = 0; i < nl2cache; i++) {
4030219089Spjd		name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
4031185029Spjd		print_status_config(zhp, name, l2cache[i],
4032209962Smm		    namewidth, 2, B_FALSE);
4033185029Spjd		free(name);
4034185029Spjd	}
4035185029Spjd}
4036185029Spjd
4037219089Spjdstatic void
4038219089Spjdprint_dedup_stats(nvlist_t *config)
4039219089Spjd{
4040219089Spjd	ddt_histogram_t *ddh;
4041219089Spjd	ddt_stat_t *dds;
4042219089Spjd	ddt_object_t *ddo;
4043219089Spjd	uint_t c;
4044219089Spjd
4045219089Spjd	/*
4046219089Spjd	 * If the pool was faulted then we may not have been able to
4047253441Sdelphij	 * obtain the config. Otherwise, if we have anything in the dedup
4048219089Spjd	 * table continue processing the stats.
4049219089Spjd	 */
4050219089Spjd	if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
4051227497Smm	    (uint64_t **)&ddo, &c) != 0)
4052219089Spjd		return;
4053219089Spjd
4054219089Spjd	(void) printf("\n");
4055227497Smm	(void) printf(gettext(" dedup: "));
4056227497Smm	if (ddo->ddo_count == 0) {
4057227497Smm		(void) printf(gettext("no DDT entries\n"));
4058227497Smm		return;
4059227497Smm	}
4060227497Smm
4061219089Spjd	(void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4062219089Spjd	    (u_longlong_t)ddo->ddo_count,
4063219089Spjd	    (u_longlong_t)ddo->ddo_dspace,
4064219089Spjd	    (u_longlong_t)ddo->ddo_mspace);
4065219089Spjd
4066219089Spjd	verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
4067219089Spjd	    (uint64_t **)&dds, &c) == 0);
4068219089Spjd	verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
4069219089Spjd	    (uint64_t **)&ddh, &c) == 0);
4070219089Spjd	zpool_dump_ddt(dds, ddh);
4071219089Spjd}
4072219089Spjd
4073168404Spjd/*
4074168404Spjd * Display a summary of pool status.  Displays a summary such as:
4075168404Spjd *
4076168404Spjd *        pool: tank
4077168404Spjd *	status: DEGRADED
4078168404Spjd *	reason: One or more devices ...
4079236146Smm *         see: http://illumos.org/msg/ZFS-xxxx-01
4080168404Spjd *	config:
4081168404Spjd *		mirror		DEGRADED
4082168404Spjd *                c1t0d0	OK
4083168404Spjd *                c2t0d0	UNAVAIL
4084168404Spjd *
4085168404Spjd * When given the '-v' option, we print out the complete config.  If the '-e'
4086168404Spjd * option is specified, then we print out error rate information as well.
4087168404Spjd */
4088168404Spjdint
4089168404Spjdstatus_callback(zpool_handle_t *zhp, void *data)
4090168404Spjd{
4091168404Spjd	status_cbdata_t *cbp = data;
4092168404Spjd	nvlist_t *config, *nvroot;
4093168404Spjd	char *msgid;
4094168404Spjd	int reason;
4095168404Spjd	const char *health;
4096168404Spjd	uint_t c;
4097168404Spjd	vdev_stat_t *vs;
4098168404Spjd
4099168404Spjd	config = zpool_get_config(zhp, NULL);
4100168404Spjd	reason = zpool_get_status(zhp, &msgid);
4101168404Spjd
4102168404Spjd	cbp->cb_count++;
4103168404Spjd
4104168404Spjd	/*
4105168404Spjd	 * If we were given 'zpool status -x', only report those pools with
4106168404Spjd	 * problems.
4107168404Spjd	 */
4108248267Smm	if (cbp->cb_explain &&
4109248267Smm	    (reason == ZPOOL_STATUS_OK ||
4110248267Smm	    reason == ZPOOL_STATUS_VERSION_OLDER ||
4111268621Ssmh	    reason == ZPOOL_STATUS_NON_NATIVE_ASHIFT ||
4112248267Smm	    reason == ZPOOL_STATUS_FEAT_DISABLED)) {
4113168404Spjd		if (!cbp->cb_allpools) {
4114168404Spjd			(void) printf(gettext("pool '%s' is healthy\n"),
4115168404Spjd			    zpool_get_name(zhp));
4116168404Spjd			if (cbp->cb_first)
4117168404Spjd				cbp->cb_first = B_FALSE;
4118168404Spjd		}
4119168404Spjd		return (0);
4120168404Spjd	}
4121168404Spjd
4122168404Spjd	if (cbp->cb_first)
4123168404Spjd		cbp->cb_first = B_FALSE;
4124168404Spjd	else
4125168404Spjd		(void) printf("\n");
4126168404Spjd
4127168404Spjd	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4128168404Spjd	    &nvroot) == 0);
4129219089Spjd	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
4130168404Spjd	    (uint64_t **)&vs, &c) == 0);
4131185029Spjd	health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4132168404Spjd
4133168404Spjd	(void) printf(gettext("  pool: %s\n"), zpool_get_name(zhp));
4134168404Spjd	(void) printf(gettext(" state: %s\n"), health);
4135168404Spjd
4136168404Spjd	switch (reason) {
4137168404Spjd	case ZPOOL_STATUS_MISSING_DEV_R:
4138168404Spjd		(void) printf(gettext("status: One or more devices could not "
4139168404Spjd		    "be opened.  Sufficient replicas exist for\n\tthe pool to "
4140168404Spjd		    "continue functioning in a degraded state.\n"));
4141168404Spjd		(void) printf(gettext("action: Attach the missing device and "
4142168404Spjd		    "online it using 'zpool online'.\n"));
4143168404Spjd		break;
4144168404Spjd
4145168404Spjd	case ZPOOL_STATUS_MISSING_DEV_NR:
4146168404Spjd		(void) printf(gettext("status: One or more devices could not "
4147168404Spjd		    "be opened.  There are insufficient\n\treplicas for the "
4148168404Spjd		    "pool to continue functioning.\n"));
4149168404Spjd		(void) printf(gettext("action: Attach the missing device and "
4150168404Spjd		    "online it using 'zpool online'.\n"));
4151168404Spjd		break;
4152168404Spjd
4153168404Spjd	case ZPOOL_STATUS_CORRUPT_LABEL_R:
4154168404Spjd		(void) printf(gettext("status: One or more devices could not "
4155168404Spjd		    "be used because the label is missing or\n\tinvalid.  "
4156168404Spjd		    "Sufficient replicas exist for the pool to continue\n\t"
4157168404Spjd		    "functioning in a degraded state.\n"));
4158168404Spjd		(void) printf(gettext("action: Replace the device using "
4159168404Spjd		    "'zpool replace'.\n"));
4160168404Spjd		break;
4161168404Spjd
4162168404Spjd	case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4163168404Spjd		(void) printf(gettext("status: One or more devices could not "
4164168404Spjd		    "be used because the label is missing \n\tor invalid.  "
4165168404Spjd		    "There are insufficient replicas for the pool to "
4166168404Spjd		    "continue\n\tfunctioning.\n"));
4167219089Spjd		zpool_explain_recover(zpool_get_handle(zhp),
4168219089Spjd		    zpool_get_name(zhp), reason, config);
4169168404Spjd		break;
4170168404Spjd
4171168404Spjd	case ZPOOL_STATUS_FAILING_DEV:
4172168404Spjd		(void) printf(gettext("status: One or more devices has "
4173168404Spjd		    "experienced an unrecoverable error.  An\n\tattempt was "
4174168404Spjd		    "made to correct the error.  Applications are "
4175168404Spjd		    "unaffected.\n"));
4176168404Spjd		(void) printf(gettext("action: Determine if the device needs "
4177168404Spjd		    "to be replaced, and clear the errors\n\tusing "
4178168404Spjd		    "'zpool clear' or replace the device with 'zpool "
4179168404Spjd		    "replace'.\n"));
4180168404Spjd		break;
4181168404Spjd
4182168404Spjd	case ZPOOL_STATUS_OFFLINE_DEV:
4183168404Spjd		(void) printf(gettext("status: One or more devices has "
4184168404Spjd		    "been taken offline by the administrator.\n\tSufficient "
4185168404Spjd		    "replicas exist for the pool to continue functioning in "
4186168404Spjd		    "a\n\tdegraded state.\n"));
4187168404Spjd		(void) printf(gettext("action: Online the device using "
4188168404Spjd		    "'zpool online' or replace the device with\n\t'zpool "
4189168404Spjd		    "replace'.\n"));
4190168404Spjd		break;
4191168404Spjd
4192219089Spjd	case ZPOOL_STATUS_REMOVED_DEV:
4193219089Spjd		(void) printf(gettext("status: One or more devices has "
4194219089Spjd		    "been removed by the administrator.\n\tSufficient "
4195219089Spjd		    "replicas exist for the pool to continue functioning in "
4196219089Spjd		    "a\n\tdegraded state.\n"));
4197219089Spjd		(void) printf(gettext("action: Online the device using "
4198219089Spjd		    "'zpool online' or replace the device with\n\t'zpool "
4199219089Spjd		    "replace'.\n"));
4200219089Spjd		break;
4201219089Spjd
4202168404Spjd	case ZPOOL_STATUS_RESILVERING:
4203168404Spjd		(void) printf(gettext("status: One or more devices is "
4204168404Spjd		    "currently being resilvered.  The pool will\n\tcontinue "
4205168404Spjd		    "to function, possibly in a degraded state.\n"));
4206168404Spjd		(void) printf(gettext("action: Wait for the resilver to "
4207168404Spjd		    "complete.\n"));
4208168404Spjd		break;
4209168404Spjd
4210168404Spjd	case ZPOOL_STATUS_CORRUPT_DATA:
4211168404Spjd		(void) printf(gettext("status: One or more devices has "
4212168404Spjd		    "experienced an error resulting in data\n\tcorruption.  "
4213168404Spjd		    "Applications may be affected.\n"));
4214168404Spjd		(void) printf(gettext("action: Restore the file in question "
4215168404Spjd		    "if possible.  Otherwise restore the\n\tentire pool from "
4216168404Spjd		    "backup.\n"));
4217168404Spjd		break;
4218168404Spjd
4219168404Spjd	case ZPOOL_STATUS_CORRUPT_POOL:
4220168404Spjd		(void) printf(gettext("status: The pool metadata is corrupted "
4221168404Spjd		    "and the pool cannot be opened.\n"));
4222219089Spjd		zpool_explain_recover(zpool_get_handle(zhp),
4223219089Spjd		    zpool_get_name(zhp), reason, config);
4224168404Spjd		break;
4225168404Spjd
4226168404Spjd	case ZPOOL_STATUS_VERSION_OLDER:
4227238926Smm		(void) printf(gettext("status: The pool is formatted using a "
4228238926Smm		    "legacy on-disk format.  The pool can\n\tstill be used, "
4229238926Smm		    "but some features are unavailable.\n"));
4230168404Spjd		(void) printf(gettext("action: Upgrade the pool using 'zpool "
4231168404Spjd		    "upgrade'.  Once this is done, the\n\tpool will no longer "
4232238926Smm		    "be accessible on software that does not support feature\n"
4233238926Smm		    "\tflags.\n"));
4234168404Spjd		break;
4235168404Spjd
4236168404Spjd	case ZPOOL_STATUS_VERSION_NEWER:
4237168404Spjd		(void) printf(gettext("status: The pool has been upgraded to a "
4238168404Spjd		    "newer, incompatible on-disk version.\n\tThe pool cannot "
4239168404Spjd		    "be accessed on this system.\n"));
4240168404Spjd		(void) printf(gettext("action: Access the pool from a system "
4241168404Spjd		    "running more recent software, or\n\trestore the pool from "
4242168404Spjd		    "backup.\n"));
4243168404Spjd		break;
4244168404Spjd
4245238926Smm	case ZPOOL_STATUS_FEAT_DISABLED:
4246238926Smm		(void) printf(gettext("status: Some supported features are not "
4247238926Smm		    "enabled on the pool. The pool can\n\tstill be used, but "
4248238926Smm		    "some features are unavailable.\n"));
4249238926Smm		(void) printf(gettext("action: Enable all features using "
4250238926Smm		    "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4251238926Smm		    "longer be accessible by software that does not support\n\t"
4252243014Smm		    "the features. See zpool-features(7) for details.\n"));
4253238926Smm		break;
4254238926Smm
4255236884Smm	case ZPOOL_STATUS_UNSUP_FEAT_READ:
4256236884Smm		(void) printf(gettext("status: The pool cannot be accessed on "
4257236884Smm		    "this system because it uses the\n\tfollowing feature(s) "
4258236884Smm		    "not supported on this system:\n"));
4259236884Smm		zpool_print_unsup_feat(config);
4260236884Smm		(void) printf("\n");
4261236884Smm		(void) printf(gettext("action: Access the pool from a system "
4262236884Smm		    "that supports the required feature(s),\n\tor restore the "
4263236884Smm		    "pool from backup.\n"));
4264236884Smm		break;
4265236884Smm
4266236884Smm	case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4267236884Smm		(void) printf(gettext("status: The pool can only be accessed "
4268236884Smm		    "in read-only mode on this system. It\n\tcannot be "
4269236884Smm		    "accessed in read-write mode because it uses the "
4270236884Smm		    "following\n\tfeature(s) not supported on this system:\n"));
4271236884Smm		zpool_print_unsup_feat(config);
4272236884Smm		(void) printf("\n");
4273236884Smm		(void) printf(gettext("action: The pool cannot be accessed in "
4274236884Smm		    "read-write mode. Import the pool with\n"
4275236884Smm		    "\t\"-o readonly=on\", access the pool from a system that "
4276236884Smm		    "supports the\n\trequired feature(s), or restore the "
4277236884Smm		    "pool from backup.\n"));
4278236884Smm		break;
4279236884Smm
4280185029Spjd	case ZPOOL_STATUS_FAULTED_DEV_R:
4281185029Spjd		(void) printf(gettext("status: One or more devices are "
4282185029Spjd		    "faulted in response to persistent errors.\n\tSufficient "
4283185029Spjd		    "replicas exist for the pool to continue functioning "
4284185029Spjd		    "in a\n\tdegraded state.\n"));
4285185029Spjd		(void) printf(gettext("action: Replace the faulted device, "
4286185029Spjd		    "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4287185029Spjd		break;
4288185029Spjd
4289185029Spjd	case ZPOOL_STATUS_FAULTED_DEV_NR:
4290185029Spjd		(void) printf(gettext("status: One or more devices are "
4291185029Spjd		    "faulted in response to persistent errors.  There are "
4292185029Spjd		    "insufficient replicas for the pool to\n\tcontinue "
4293185029Spjd		    "functioning.\n"));
4294185029Spjd		(void) printf(gettext("action: Destroy and re-create the pool "
4295185029Spjd		    "from a backup source.  Manually marking the device\n"
4296185029Spjd		    "\trepaired using 'zpool clear' may allow some data "
4297185029Spjd		    "to be recovered.\n"));
4298185029Spjd		break;
4299185029Spjd
4300185029Spjd	case ZPOOL_STATUS_IO_FAILURE_WAIT:
4301185029Spjd	case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4302185029Spjd		(void) printf(gettext("status: One or more devices are "
4303185029Spjd		    "faulted in response to IO failures.\n"));
4304185029Spjd		(void) printf(gettext("action: Make sure the affected devices "
4305185029Spjd		    "are connected, then run 'zpool clear'.\n"));
4306185029Spjd		break;
4307185029Spjd
4308185029Spjd	case ZPOOL_STATUS_BAD_LOG:
4309185029Spjd		(void) printf(gettext("status: An intent log record "
4310185029Spjd		    "could not be read.\n"
4311185029Spjd		    "\tWaiting for adminstrator intervention to fix the "
4312185029Spjd		    "faulted pool.\n"));
4313185029Spjd		(void) printf(gettext("action: Either restore the affected "
4314185029Spjd		    "device(s) and run 'zpool online',\n"
4315185029Spjd		    "\tor ignore the intent log records by running "
4316185029Spjd		    "'zpool clear'.\n"));
4317185029Spjd		break;
4318185029Spjd
4319254591Sgibbs	case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
4320254591Sgibbs		(void) printf(gettext("status: One or more devices are "
4321254591Sgibbs		    "configured to use a non-native block size.\n"
4322254591Sgibbs		    "\tExpect reduced performance.\n"));
4323254591Sgibbs		(void) printf(gettext("action: Replace affected devices with "
4324254591Sgibbs		    "devices that support the\n\tconfigured block size, or "
4325254591Sgibbs		    "migrate data to a properly configured\n\tpool.\n"));
4326254591Sgibbs		break;
4327254591Sgibbs
4328168404Spjd	default:
4329168404Spjd		/*
4330168404Spjd		 * The remaining errors can't actually be generated, yet.
4331168404Spjd		 */
4332168404Spjd		assert(reason == ZPOOL_STATUS_OK);
4333168404Spjd	}
4334168404Spjd
4335168404Spjd	if (msgid != NULL)
4336236146Smm		(void) printf(gettext("   see: http://illumos.org/msg/%s\n"),
4337168404Spjd		    msgid);
4338168404Spjd
4339168404Spjd	if (config != NULL) {
4340168404Spjd		int namewidth;
4341168404Spjd		uint64_t nerr;
4342185029Spjd		nvlist_t **spares, **l2cache;
4343185029Spjd		uint_t nspares, nl2cache;
4344219089Spjd		pool_scan_stat_t *ps = NULL;
4345168404Spjd
4346219089Spjd		(void) nvlist_lookup_uint64_array(nvroot,
4347219089Spjd		    ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4348219089Spjd		print_scan_status(ps);
4349168404Spjd
4350168404Spjd		namewidth = max_width(zhp, nvroot, 0, 0);
4351168404Spjd		if (namewidth < 10)
4352168404Spjd			namewidth = 10;
4353168404Spjd
4354168404Spjd		(void) printf(gettext("config:\n\n"));
4355168404Spjd		(void) printf(gettext("\t%-*s  %-8s %5s %5s %5s\n"), namewidth,
4356168404Spjd		    "NAME", "STATE", "READ", "WRITE", "CKSUM");
4357168404Spjd		print_status_config(zhp, zpool_get_name(zhp), nvroot,
4358209962Smm		    namewidth, 0, B_FALSE);
4359209962Smm
4360185029Spjd		if (num_logs(nvroot) > 0)
4361213197Smm			print_logs(zhp, nvroot, namewidth, B_TRUE);
4362185029Spjd		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4363185029Spjd		    &l2cache, &nl2cache) == 0)
4364185029Spjd			print_l2cache(zhp, l2cache, nl2cache, namewidth);
4365185029Spjd
4366168404Spjd		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4367168404Spjd		    &spares, &nspares) == 0)
4368168404Spjd			print_spares(zhp, spares, nspares, namewidth);
4369168404Spjd
4370168404Spjd		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4371168404Spjd		    &nerr) == 0) {
4372168404Spjd			nvlist_t *nverrlist = NULL;
4373168404Spjd
4374168404Spjd			/*
4375168404Spjd			 * If the approximate error count is small, get a
4376168404Spjd			 * precise count by fetching the entire log and
4377168404Spjd			 * uniquifying the results.
4378168404Spjd			 */
4379185029Spjd			if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4380168404Spjd			    zpool_get_errlog(zhp, &nverrlist) == 0) {
4381168404Spjd				nvpair_t *elem;
4382168404Spjd
4383168404Spjd				elem = NULL;
4384168404Spjd				nerr = 0;
4385168404Spjd				while ((elem = nvlist_next_nvpair(nverrlist,
4386168404Spjd				    elem)) != NULL) {
4387168404Spjd					nerr++;
4388168404Spjd				}
4389168404Spjd			}
4390168404Spjd			nvlist_free(nverrlist);
4391168404Spjd
4392168404Spjd			(void) printf("\n");
4393168404Spjd
4394168404Spjd			if (nerr == 0)
4395168404Spjd				(void) printf(gettext("errors: No known data "
4396168404Spjd				    "errors\n"));
4397168404Spjd			else if (!cbp->cb_verbose)
4398168404Spjd				(void) printf(gettext("errors: %llu data "
4399168404Spjd				    "errors, use '-v' for a list\n"),
4400168404Spjd				    (u_longlong_t)nerr);
4401168404Spjd			else
4402168404Spjd				print_error_log(zhp);
4403168404Spjd		}
4404219089Spjd
4405219089Spjd		if (cbp->cb_dedup_stats)
4406219089Spjd			print_dedup_stats(config);
4407168404Spjd	} else {
4408168404Spjd		(void) printf(gettext("config: The configuration cannot be "
4409168404Spjd		    "determined.\n"));
4410168404Spjd	}
4411168404Spjd
4412168404Spjd	return (0);
4413168404Spjd}
4414168404Spjd
4415168404Spjd/*
4416219089Spjd * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4417168404Spjd *
4418168404Spjd *	-v	Display complete error logs
4419168404Spjd *	-x	Display only pools with potential problems
4420219089Spjd *	-D	Display dedup status (undocumented)
4421219089Spjd *	-T	Display a timestamp in date(1) or Unix format
4422168404Spjd *
4423168404Spjd * Describes the health status of all pools or some subset.
4424168404Spjd */
4425168404Spjdint
4426168404Spjdzpool_do_status(int argc, char **argv)
4427168404Spjd{
4428168404Spjd	int c;
4429168404Spjd	int ret;
4430219089Spjd	unsigned long interval = 0, count = 0;
4431168404Spjd	status_cbdata_t cb = { 0 };
4432168404Spjd
4433168404Spjd	/* check options */
4434219089Spjd	while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4435168404Spjd		switch (c) {
4436168404Spjd		case 'v':
4437168404Spjd			cb.cb_verbose = B_TRUE;
4438168404Spjd			break;
4439168404Spjd		case 'x':
4440168404Spjd			cb.cb_explain = B_TRUE;
4441168404Spjd			break;
4442219089Spjd		case 'D':
4443219089Spjd			cb.cb_dedup_stats = B_TRUE;
4444219089Spjd			break;
4445219089Spjd		case 'T':
4446219089Spjd			get_timestamp_arg(*optarg);
4447219089Spjd			break;
4448168404Spjd		case '?':
4449168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4450168404Spjd			    optopt);
4451168404Spjd			usage(B_FALSE);
4452168404Spjd		}
4453168404Spjd	}
4454168404Spjd
4455168404Spjd	argc -= optind;
4456168404Spjd	argv += optind;
4457168404Spjd
4458219089Spjd	get_interval_count(&argc, argv, &interval, &count);
4459168404Spjd
4460168404Spjd	if (argc == 0)
4461168404Spjd		cb.cb_allpools = B_TRUE;
4462168404Spjd
4463219089Spjd	cb.cb_first = B_TRUE;
4464168404Spjd
4465219089Spjd	for (;;) {
4466219089Spjd		if (timestamp_fmt != NODATE)
4467219089Spjd			print_timestamp(timestamp_fmt);
4468168404Spjd
4469219089Spjd		ret = for_each_pool(argc, argv, B_TRUE, NULL,
4470219089Spjd		    status_callback, &cb);
4471219089Spjd
4472219089Spjd		if (argc == 0 && cb.cb_count == 0)
4473219089Spjd			(void) printf(gettext("no pools available\n"));
4474219089Spjd		else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4475219089Spjd			(void) printf(gettext("all pools are healthy\n"));
4476219089Spjd
4477219089Spjd		if (ret != 0)
4478219089Spjd			return (ret);
4479219089Spjd
4480219089Spjd		if (interval == 0)
4481219089Spjd			break;
4482219089Spjd
4483219089Spjd		if (count != 0 && --count == 0)
4484219089Spjd			break;
4485219089Spjd
4486219089Spjd		(void) sleep(interval);
4487219089Spjd	}
4488219089Spjd
4489219089Spjd	return (0);
4490168404Spjd}
4491168404Spjd
4492168404Spjdtypedef struct upgrade_cbdata {
4493168404Spjd	int	cb_first;
4494212050Spjd	char	cb_poolname[ZPOOL_MAXNAMELEN];
4495168404Spjd	int	cb_argc;
4496185029Spjd	uint64_t cb_version;
4497168404Spjd	char	**cb_argv;
4498168404Spjd} upgrade_cbdata_t;
4499168404Spjd
4500238950Smm#ifdef __FreeBSD__
4501168404Spjdstatic int
4502212050Spjdis_root_pool(zpool_handle_t *zhp)
4503212050Spjd{
4504212050Spjd	static struct statfs sfs;
4505212050Spjd	static char *poolname = NULL;
4506212050Spjd	static boolean_t stated = B_FALSE;
4507212050Spjd	char *slash;
4508212050Spjd
4509212067Spjd	if (!stated) {
4510212050Spjd		stated = B_TRUE;
4511212050Spjd		if (statfs("/", &sfs) == -1) {
4512212050Spjd			(void) fprintf(stderr,
4513212050Spjd			    "Unable to stat root file system: %s.\n",
4514212050Spjd			    strerror(errno));
4515212067Spjd			return (0);
4516212050Spjd		}
4517212050Spjd		if (strcmp(sfs.f_fstypename, "zfs") != 0)
4518212067Spjd			return (0);
4519212050Spjd		poolname = sfs.f_mntfromname;
4520212050Spjd		if ((slash = strchr(poolname, '/')) != NULL)
4521212050Spjd			*slash = '\0';
4522212050Spjd	}
4523212050Spjd	return (poolname != NULL && strcmp(poolname, zpool_get_name(zhp)) == 0);
4524212050Spjd}
4525212050Spjd
4526238950Smmstatic void
4527238950Smmroot_pool_upgrade_check(zpool_handle_t *zhp, char *poolname, int size) {
4528238950Smm
4529238950Smm	if (poolname[0] == '\0' && is_root_pool(zhp))
4530238950Smm		(void) strlcpy(poolname, zpool_get_name(zhp), size);
4531238950Smm}
4532238950Smm#endif	/* FreeBSD */
4533238950Smm
4534212050Spjdstatic int
4535238926Smmupgrade_version(zpool_handle_t *zhp, uint64_t version)
4536238926Smm{
4537238926Smm	int ret;
4538238926Smm	nvlist_t *config;
4539238926Smm	uint64_t oldversion;
4540238926Smm
4541238926Smm	config = zpool_get_config(zhp, NULL);
4542238926Smm	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4543238926Smm	    &oldversion) == 0);
4544238926Smm
4545238926Smm	assert(SPA_VERSION_IS_SUPPORTED(oldversion));
4546238926Smm	assert(oldversion < version);
4547238926Smm
4548238926Smm	ret = zpool_upgrade(zhp, version);
4549238926Smm	if (ret != 0)
4550238926Smm		return (ret);
4551238926Smm
4552238926Smm	if (version >= SPA_VERSION_FEATURES) {
4553238926Smm		(void) printf(gettext("Successfully upgraded "
4554238926Smm		    "'%s' from version %llu to feature flags.\n"),
4555238926Smm		    zpool_get_name(zhp), oldversion);
4556238926Smm	} else {
4557238926Smm		(void) printf(gettext("Successfully upgraded "
4558238926Smm		    "'%s' from version %llu to version %llu.\n"),
4559238926Smm		    zpool_get_name(zhp), oldversion, version);
4560238926Smm	}
4561238926Smm
4562238926Smm	return (0);
4563238926Smm}
4564238926Smm
4565238926Smmstatic int
4566238926Smmupgrade_enable_all(zpool_handle_t *zhp, int *countp)
4567238926Smm{
4568238926Smm	int i, ret, count;
4569238926Smm	boolean_t firstff = B_TRUE;
4570238926Smm	nvlist_t *enabled = zpool_get_features(zhp);
4571238926Smm
4572238926Smm	count = 0;
4573238926Smm	for (i = 0; i < SPA_FEATURES; i++) {
4574238926Smm		const char *fname = spa_feature_table[i].fi_uname;
4575238926Smm		const char *fguid = spa_feature_table[i].fi_guid;
4576238926Smm		if (!nvlist_exists(enabled, fguid)) {
4577238926Smm			char *propname;
4578238926Smm			verify(-1 != asprintf(&propname, "feature@%s", fname));
4579238926Smm			ret = zpool_set_prop(zhp, propname,
4580238926Smm			    ZFS_FEATURE_ENABLED);
4581238926Smm			if (ret != 0) {
4582238926Smm				free(propname);
4583238926Smm				return (ret);
4584238926Smm			}
4585238926Smm			count++;
4586238926Smm
4587238926Smm			if (firstff) {
4588238926Smm				(void) printf(gettext("Enabled the "
4589238926Smm				    "following features on '%s':\n"),
4590238926Smm				    zpool_get_name(zhp));
4591238926Smm				firstff = B_FALSE;
4592238926Smm			}
4593238926Smm			(void) printf(gettext("  %s\n"), fname);
4594238926Smm			free(propname);
4595238926Smm		}
4596238926Smm	}
4597238926Smm
4598238926Smm	if (countp != NULL)
4599238926Smm		*countp = count;
4600238926Smm	return (0);
4601238926Smm}
4602238926Smm
4603238926Smmstatic int
4604168404Spjdupgrade_cb(zpool_handle_t *zhp, void *arg)
4605168404Spjd{
4606168404Spjd	upgrade_cbdata_t *cbp = arg;
4607168404Spjd	nvlist_t *config;
4608168404Spjd	uint64_t version;
4609238926Smm	boolean_t printnl = B_FALSE;
4610238926Smm	int ret;
4611168404Spjd
4612168404Spjd	config = zpool_get_config(zhp, NULL);
4613168404Spjd	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4614168404Spjd	    &version) == 0);
4615168404Spjd
4616238926Smm	assert(SPA_VERSION_IS_SUPPORTED(version));
4617168404Spjd
4618238926Smm	if (version < cbp->cb_version) {
4619238926Smm		cbp->cb_first = B_FALSE;
4620238926Smm		ret = upgrade_version(zhp, cbp->cb_version);
4621238926Smm		if (ret != 0)
4622238926Smm			return (ret);
4623238926Smm#ifdef __FreeBSD__
4624238950Smm		root_pool_upgrade_check(zhp, cbp->cb_poolname,
4625238950Smm		    sizeof(cbp->cb_poolname));
4626238926Smm#endif	/* ___FreeBSD__ */
4627238926Smm		printnl = B_TRUE;
4628238926Smm
4629238926Smm#ifdef illumos
4630238926Smm		/*
4631238926Smm		 * If they did "zpool upgrade -a", then we could
4632238926Smm		 * be doing ioctls to different pools.  We need
4633238926Smm		 * to log this history once to each pool, and bypass
4634238926Smm		 * the normal history logging that happens in main().
4635238926Smm		 */
4636238926Smm		(void) zpool_log_history(g_zfs, history_str);
4637238926Smm		log_history = B_FALSE;
4638238926Smm#endif
4639238926Smm	}
4640238926Smm
4641238926Smm	if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4642238926Smm		int count;
4643238926Smm		ret = upgrade_enable_all(zhp, &count);
4644238926Smm		if (ret != 0)
4645238926Smm			return (ret);
4646238926Smm
4647238926Smm		if (count > 0) {
4648168404Spjd			cbp->cb_first = B_FALSE;
4649238926Smm			printnl = B_TRUE;
4650248571Smm			/*
4651248571Smm			 * If they did "zpool upgrade -a", then we could
4652248571Smm			 * be doing ioctls to different pools.  We need
4653248571Smm			 * to log this history once to each pool, and bypass
4654248571Smm			 * the normal history logging that happens in main().
4655248571Smm			 */
4656248571Smm			(void) zpool_log_history(g_zfs, history_str);
4657248571Smm			log_history = B_FALSE;
4658168404Spjd		}
4659238926Smm	}
4660168404Spjd
4661238926Smm	if (printnl) {
4662238926Smm		(void) printf(gettext("\n"));
4663238926Smm	}
4664238926Smm
4665238926Smm	return (0);
4666238926Smm}
4667238926Smm
4668238926Smmstatic int
4669238926Smmupgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
4670238926Smm{
4671238926Smm	upgrade_cbdata_t *cbp = arg;
4672238926Smm	nvlist_t *config;
4673238926Smm	uint64_t version;
4674238926Smm
4675238926Smm	config = zpool_get_config(zhp, NULL);
4676238926Smm	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4677238926Smm	    &version) == 0);
4678238926Smm
4679238926Smm	assert(SPA_VERSION_IS_SUPPORTED(version));
4680238926Smm
4681238926Smm	if (version < SPA_VERSION_FEATURES) {
4682168404Spjd		if (cbp->cb_first) {
4683168404Spjd			(void) printf(gettext("The following pools are "
4684238926Smm			    "formatted with legacy version numbers and can\n"
4685238926Smm			    "be upgraded to use feature flags.  After "
4686238926Smm			    "being upgraded, these pools\nwill no "
4687238926Smm			    "longer be accessible by software that does not "
4688238926Smm			    "support feature\nflags.\n\n"));
4689168404Spjd			(void) printf(gettext("VER  POOL\n"));
4690168404Spjd			(void) printf(gettext("---  ------------\n"));
4691168404Spjd			cbp->cb_first = B_FALSE;
4692168404Spjd		}
4693168404Spjd
4694168404Spjd		(void) printf("%2llu   %s\n", (u_longlong_t)version,
4695168404Spjd		    zpool_get_name(zhp));
4696168404Spjd	}
4697168404Spjd
4698238926Smm	return (0);
4699168404Spjd}
4700168404Spjd
4701238926Smmstatic int
4702238926Smmupgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
4703238926Smm{
4704238926Smm	upgrade_cbdata_t *cbp = arg;
4705238926Smm	nvlist_t *config;
4706238926Smm	uint64_t version;
4707238926Smm
4708238926Smm	config = zpool_get_config(zhp, NULL);
4709238926Smm	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4710238926Smm	    &version) == 0);
4711238926Smm
4712238926Smm	if (version >= SPA_VERSION_FEATURES) {
4713238926Smm		int i;
4714238926Smm		boolean_t poolfirst = B_TRUE;
4715238926Smm		nvlist_t *enabled = zpool_get_features(zhp);
4716238926Smm
4717238926Smm		for (i = 0; i < SPA_FEATURES; i++) {
4718238926Smm			const char *fguid = spa_feature_table[i].fi_guid;
4719238926Smm			const char *fname = spa_feature_table[i].fi_uname;
4720238926Smm			if (!nvlist_exists(enabled, fguid)) {
4721238926Smm				if (cbp->cb_first) {
4722238926Smm					(void) printf(gettext("\nSome "
4723238926Smm					    "supported features are not "
4724238926Smm					    "enabled on the following pools. "
4725238926Smm					    "Once a\nfeature is enabled the "
4726238926Smm					    "pool may become incompatible with "
4727238926Smm					    "software\nthat does not support "
4728238926Smm					    "the feature. See "
4729243014Smm					    "zpool-features(7) for "
4730238926Smm					    "details.\n\n"));
4731238926Smm					(void) printf(gettext("POOL  "
4732238926Smm					    "FEATURE\n"));
4733238926Smm					(void) printf(gettext("------"
4734238926Smm					    "---------\n"));
4735238926Smm					cbp->cb_first = B_FALSE;
4736238926Smm				}
4737238926Smm
4738238926Smm				if (poolfirst) {
4739238926Smm					(void) printf(gettext("%s\n"),
4740238926Smm					    zpool_get_name(zhp));
4741238926Smm					poolfirst = B_FALSE;
4742238926Smm				}
4743238926Smm
4744238926Smm				(void) printf(gettext("      %s\n"), fname);
4745238926Smm			}
4746238926Smm		}
4747238926Smm	}
4748238926Smm
4749238926Smm	return (0);
4750238926Smm}
4751238926Smm
4752168404Spjd/* ARGSUSED */
4753168404Spjdstatic int
4754168404Spjdupgrade_one(zpool_handle_t *zhp, void *data)
4755168404Spjd{
4756238926Smm	boolean_t printnl = B_FALSE;
4757185029Spjd	upgrade_cbdata_t *cbp = data;
4758185029Spjd	uint64_t cur_version;
4759168404Spjd	int ret;
4760168404Spjd
4761185029Spjd	if (strcmp("log", zpool_get_name(zhp)) == 0) {
4762185029Spjd		(void) printf(gettext("'log' is now a reserved word\n"
4763185029Spjd		    "Pool 'log' must be renamed using export and import"
4764185029Spjd		    " to upgrade.\n"));
4765185029Spjd		return (1);
4766185029Spjd	}
4767168404Spjd
4768185029Spjd	cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4769185029Spjd	if (cur_version > cbp->cb_version) {
4770168404Spjd		(void) printf(gettext("Pool '%s' is already formatted "
4771238926Smm		    "using more current version '%llu'.\n\n"),
4772185029Spjd		    zpool_get_name(zhp), cur_version);
4773185029Spjd		return (0);
4774185029Spjd	}
4775238926Smm
4776238926Smm	if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
4777185029Spjd		(void) printf(gettext("Pool '%s' is already formatted "
4778238926Smm		    "using version %llu.\n\n"), zpool_get_name(zhp),
4779238926Smm		    cbp->cb_version);
4780168404Spjd		return (0);
4781168404Spjd	}
4782168404Spjd
4783238926Smm	if (cur_version != cbp->cb_version) {
4784238926Smm		printnl = B_TRUE;
4785238926Smm		ret = upgrade_version(zhp, cbp->cb_version);
4786238950Smm		if (ret != 0)
4787238950Smm			return (ret);
4788238926Smm#ifdef __FreeBSD__
4789238950Smm		root_pool_upgrade_check(zhp, cbp->cb_poolname,
4790238950Smm		    sizeof(cbp->cb_poolname));
4791238926Smm#endif	/* ___FreeBSD__ */
4792238926Smm	}
4793168404Spjd
4794238926Smm	if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4795238926Smm		int count = 0;
4796238926Smm		ret = upgrade_enable_all(zhp, &count);
4797238926Smm		if (ret != 0)
4798238926Smm			return (ret);
4799238926Smm
4800238926Smm		if (count != 0) {
4801238926Smm			printnl = B_TRUE;
4802238950Smm#ifdef __FreeBSD__
4803238951Smm			root_pool_upgrade_check(zhp, cbp->cb_poolname,
4804238951Smm			    sizeof(cbp->cb_poolname));
4805238950Smm#endif	/* __FreeBSD __*/
4806238926Smm		} else if (cur_version == SPA_VERSION) {
4807238926Smm			(void) printf(gettext("Pool '%s' already has all "
4808238926Smm			    "supported features enabled.\n"),
4809238926Smm			    zpool_get_name(zhp));
4810238926Smm		}
4811168404Spjd	}
4812168404Spjd
4813238926Smm	if (printnl) {
4814238926Smm		(void) printf(gettext("\n"));
4815238926Smm	}
4816238926Smm
4817238926Smm	return (0);
4818168404Spjd}
4819168404Spjd
4820168404Spjd/*
4821168404Spjd * zpool upgrade
4822168404Spjd * zpool upgrade -v
4823185029Spjd * zpool upgrade [-V version] <-a | pool ...>
4824168404Spjd *
4825168404Spjd * With no arguments, display downrev'd ZFS pool available for upgrade.
4826168404Spjd * Individual pools can be upgraded by specifying the pool, and '-a' will
4827168404Spjd * upgrade all pools.
4828168404Spjd */
4829168404Spjdint
4830168404Spjdzpool_do_upgrade(int argc, char **argv)
4831168404Spjd{
4832168404Spjd	int c;
4833168404Spjd	upgrade_cbdata_t cb = { 0 };
4834168404Spjd	int ret = 0;
4835168404Spjd	boolean_t showversions = B_FALSE;
4836238926Smm	boolean_t upgradeall = B_FALSE;
4837185029Spjd	char *end;
4838168404Spjd
4839185029Spjd
4840168404Spjd	/* check options */
4841219089Spjd	while ((c = getopt(argc, argv, ":avV:")) != -1) {
4842168404Spjd		switch (c) {
4843168404Spjd		case 'a':
4844238926Smm			upgradeall = B_TRUE;
4845168404Spjd			break;
4846168404Spjd		case 'v':
4847168404Spjd			showversions = B_TRUE;
4848168404Spjd			break;
4849185029Spjd		case 'V':
4850185029Spjd			cb.cb_version = strtoll(optarg, &end, 10);
4851236884Smm			if (*end != '\0' ||
4852236884Smm			    !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
4853185029Spjd				(void) fprintf(stderr,
4854185029Spjd				    gettext("invalid version '%s'\n"), optarg);
4855185029Spjd				usage(B_FALSE);
4856185029Spjd			}
4857185029Spjd			break;
4858219089Spjd		case ':':
4859219089Spjd			(void) fprintf(stderr, gettext("missing argument for "
4860219089Spjd			    "'%c' option\n"), optopt);
4861219089Spjd			usage(B_FALSE);
4862219089Spjd			break;
4863168404Spjd		case '?':
4864168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4865168404Spjd			    optopt);
4866168404Spjd			usage(B_FALSE);
4867168404Spjd		}
4868168404Spjd	}
4869168404Spjd
4870168404Spjd	cb.cb_argc = argc;
4871168404Spjd	cb.cb_argv = argv;
4872168404Spjd	argc -= optind;
4873168404Spjd	argv += optind;
4874168404Spjd
4875185029Spjd	if (cb.cb_version == 0) {
4876185029Spjd		cb.cb_version = SPA_VERSION;
4877238926Smm	} else if (!upgradeall && argc == 0) {
4878185029Spjd		(void) fprintf(stderr, gettext("-V option is "
4879185029Spjd		    "incompatible with other arguments\n"));
4880185029Spjd		usage(B_FALSE);
4881185029Spjd	}
4882185029Spjd
4883168404Spjd	if (showversions) {
4884238926Smm		if (upgradeall || argc != 0) {
4885168404Spjd			(void) fprintf(stderr, gettext("-v option is "
4886168404Spjd			    "incompatible with other arguments\n"));
4887168404Spjd			usage(B_FALSE);
4888168404Spjd		}
4889238926Smm	} else if (upgradeall) {
4890168404Spjd		if (argc != 0) {
4891185029Spjd			(void) fprintf(stderr, gettext("-a option should not "
4892185029Spjd			    "be used along with a pool name\n"));
4893168404Spjd			usage(B_FALSE);
4894168404Spjd		}
4895168404Spjd	}
4896168404Spjd
4897236884Smm	(void) printf(gettext("This system supports ZFS pool feature "
4898236884Smm	    "flags.\n\n"));
4899168404Spjd	if (showversions) {
4900238926Smm		int i;
4901238926Smm
4902238926Smm		(void) printf(gettext("The following features are "
4903168404Spjd		    "supported:\n\n"));
4904238926Smm		(void) printf(gettext("FEAT DESCRIPTION\n"));
4905238926Smm		(void) printf("----------------------------------------------"
4906238926Smm		    "---------------\n");
4907238926Smm		for (i = 0; i < SPA_FEATURES; i++) {
4908238926Smm			zfeature_info_t *fi = &spa_feature_table[i];
4909238926Smm			const char *ro = fi->fi_can_readonly ?
4910238926Smm			    " (read-only compatible)" : "";
4911238926Smm
4912238926Smm			(void) printf("%-37s%s\n", fi->fi_uname, ro);
4913238926Smm			(void) printf("     %s\n", fi->fi_desc);
4914238926Smm		}
4915238926Smm		(void) printf("\n");
4916238926Smm
4917238926Smm		(void) printf(gettext("The following legacy versions are also "
4918238926Smm		    "supported:\n\n"));
4919168404Spjd		(void) printf(gettext("VER  DESCRIPTION\n"));
4920168404Spjd		(void) printf("---  -----------------------------------------"
4921168404Spjd		    "---------------\n");
4922168404Spjd		(void) printf(gettext(" 1   Initial ZFS version\n"));
4923168404Spjd		(void) printf(gettext(" 2   Ditto blocks "
4924168404Spjd		    "(replicated metadata)\n"));
4925168404Spjd		(void) printf(gettext(" 3   Hot spares and double parity "
4926168404Spjd		    "RAID-Z\n"));
4927168404Spjd		(void) printf(gettext(" 4   zpool history\n"));
4928168404Spjd		(void) printf(gettext(" 5   Compression using the gzip "
4929168404Spjd		    "algorithm\n"));
4930185029Spjd		(void) printf(gettext(" 6   bootfs pool property\n"));
4931185029Spjd		(void) printf(gettext(" 7   Separate intent log devices\n"));
4932185029Spjd		(void) printf(gettext(" 8   Delegated administration\n"));
4933185029Spjd		(void) printf(gettext(" 9   refquota and refreservation "
4934185029Spjd		    "properties\n"));
4935185029Spjd		(void) printf(gettext(" 10  Cache devices\n"));
4936185029Spjd		(void) printf(gettext(" 11  Improved scrub performance\n"));
4937185029Spjd		(void) printf(gettext(" 12  Snapshot properties\n"));
4938185029Spjd		(void) printf(gettext(" 13  snapused property\n"));
4939209962Smm		(void) printf(gettext(" 14  passthrough-x aclinherit\n"));
4940209962Smm		(void) printf(gettext(" 15  user/group space accounting\n"));
4941219089Spjd		(void) printf(gettext(" 16  stmf property support\n"));
4942219089Spjd		(void) printf(gettext(" 17  Triple-parity RAID-Z\n"));
4943219089Spjd		(void) printf(gettext(" 18  Snapshot user holds\n"));
4944219089Spjd		(void) printf(gettext(" 19  Log device removal\n"));
4945219089Spjd		(void) printf(gettext(" 20  Compression using zle "
4946219089Spjd		    "(zero-length encoding)\n"));
4947219089Spjd		(void) printf(gettext(" 21  Deduplication\n"));
4948219089Spjd		(void) printf(gettext(" 22  Received properties\n"));
4949219089Spjd		(void) printf(gettext(" 23  Slim ZIL\n"));
4950219089Spjd		(void) printf(gettext(" 24  System attributes\n"));
4951219089Spjd		(void) printf(gettext(" 25  Improved scrub stats\n"));
4952219089Spjd		(void) printf(gettext(" 26  Improved snapshot deletion "
4953219089Spjd		    "performance\n"));
4954219089Spjd		(void) printf(gettext(" 27  Improved snapshot creation "
4955219089Spjd		    "performance\n"));
4956219089Spjd		(void) printf(gettext(" 28  Multiple vdev replacements\n"));
4957219089Spjd		(void) printf(gettext("\nFor more information on a particular "
4958219089Spjd		    "version, including supported releases,\n"));
4959219089Spjd		(void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4960238926Smm	} else if (argc == 0 && upgradeall) {
4961238926Smm		cb.cb_first = B_TRUE;
4962168404Spjd		ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4963238926Smm		if (ret == 0 && cb.cb_first) {
4964238926Smm			if (cb.cb_version == SPA_VERSION) {
4965238926Smm				(void) printf(gettext("All pools are already "
4966238926Smm				    "formatted using feature flags.\n\n"));
4967238926Smm				(void) printf(gettext("Every feature flags "
4968238926Smm				    "pool already has all supported features "
4969238926Smm				    "enabled.\n"));
4970238926Smm			} else {
4971238926Smm				(void) printf(gettext("All pools are already "
4972238926Smm				    "formatted with version %llu or higher.\n"),
4973238926Smm				    cb.cb_version);
4974168404Spjd			}
4975168404Spjd		}
4976238926Smm	} else if (argc == 0) {
4977238926Smm		cb.cb_first = B_TRUE;
4978238926Smm		ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
4979238926Smm		assert(ret == 0);
4980168404Spjd
4981238926Smm		if (cb.cb_first) {
4982238926Smm			(void) printf(gettext("All pools are formatted "
4983238926Smm			    "using feature flags.\n\n"));
4984238926Smm		} else {
4985238926Smm			(void) printf(gettext("\nUse 'zpool upgrade -v' "
4986238926Smm			    "for a list of available legacy versions.\n"));
4987168404Spjd		}
4988238926Smm
4989238926Smm		cb.cb_first = B_TRUE;
4990238926Smm		ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
4991238926Smm		assert(ret == 0);
4992238926Smm
4993238926Smm		if (cb.cb_first) {
4994238926Smm			(void) printf(gettext("Every feature flags pool has "
4995238926Smm			    "all supported features enabled.\n"));
4996238926Smm		} else {
4997238926Smm			(void) printf(gettext("\n"));
4998238926Smm		}
4999168404Spjd	} else {
5000168404Spjd		ret = for_each_pool(argc, argv, B_FALSE, NULL,
5001168404Spjd		    upgrade_one, &cb);
5002168404Spjd	}
5003168404Spjd
5004212050Spjd	if (cb.cb_poolname[0] != '\0') {
5005212050Spjd		(void) printf(
5006212050Spjd		    "If you boot from pool '%s', don't forget to update boot code.\n"
5007212050Spjd		    "Assuming you use GPT partitioning and da0 is your boot disk\n"
5008212050Spjd		    "the following command will do it:\n"
5009212050Spjd		    "\n"
5010212050Spjd		    "\tgpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 da0\n\n",
5011212050Spjd		    cb.cb_poolname);
5012212050Spjd	}
5013212050Spjd
5014168404Spjd	return (ret);
5015168404Spjd}
5016168404Spjd
5017185029Spjdtypedef struct hist_cbdata {
5018185029Spjd	boolean_t first;
5019248571Smm	boolean_t longfmt;
5020248571Smm	boolean_t internal;
5021185029Spjd} hist_cbdata_t;
5022185029Spjd
5023168404Spjd/*
5024168404Spjd * Print out the command history for a specific pool.
5025168404Spjd */
5026168404Spjdstatic int
5027168404Spjdget_history_one(zpool_handle_t *zhp, void *data)
5028168404Spjd{
5029168404Spjd	nvlist_t *nvhis;
5030168404Spjd	nvlist_t **records;
5031168404Spjd	uint_t numrecords;
5032168404Spjd	int ret, i;
5033185029Spjd	hist_cbdata_t *cb = (hist_cbdata_t *)data;
5034168404Spjd
5035185029Spjd	cb->first = B_FALSE;
5036168404Spjd
5037168404Spjd	(void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
5038168404Spjd
5039168404Spjd	if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
5040168404Spjd		return (ret);
5041168404Spjd
5042168404Spjd	verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
5043168404Spjd	    &records, &numrecords) == 0);
5044168404Spjd	for (i = 0; i < numrecords; i++) {
5045248571Smm		nvlist_t *rec = records[i];
5046248571Smm		char tbuf[30] = "";
5047185029Spjd
5048248571Smm		if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
5049248571Smm			time_t tsec;
5050248571Smm			struct tm t;
5051185029Spjd
5052248571Smm			tsec = fnvlist_lookup_uint64(records[i],
5053248571Smm			    ZPOOL_HIST_TIME);
5054248571Smm			(void) localtime_r(&tsec, &t);
5055248571Smm			(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
5056248571Smm		}
5057248571Smm
5058248571Smm		if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
5059248571Smm			(void) printf("%s %s", tbuf,
5060248571Smm			    fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
5061248571Smm		} else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
5062248571Smm			int ievent =
5063248571Smm			    fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
5064248571Smm			if (!cb->internal)
5065185029Spjd				continue;
5066248571Smm			if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
5067248571Smm				(void) printf("%s unrecognized record:\n",
5068248571Smm				    tbuf);
5069248571Smm				dump_nvlist(rec, 4);
5070185029Spjd				continue;
5071248571Smm			}
5072248571Smm			(void) printf("%s [internal %s txg:%lld] %s", tbuf,
5073248571Smm			    zfs_history_event_names[ievent],
5074248571Smm			    fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5075248571Smm			    fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
5076248571Smm		} else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
5077248571Smm			if (!cb->internal)
5078248571Smm				continue;
5079248571Smm			(void) printf("%s [txg:%lld] %s", tbuf,
5080248571Smm			    fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5081248571Smm			    fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
5082248571Smm			if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
5083248571Smm				(void) printf(" %s (%llu)",
5084248571Smm				    fnvlist_lookup_string(rec,
5085248571Smm				    ZPOOL_HIST_DSNAME),
5086248571Smm				    fnvlist_lookup_uint64(rec,
5087248571Smm				    ZPOOL_HIST_DSID));
5088248571Smm			}
5089248571Smm			(void) printf(" %s", fnvlist_lookup_string(rec,
5090248571Smm			    ZPOOL_HIST_INT_STR));
5091248571Smm		} else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
5092248571Smm			if (!cb->internal)
5093248571Smm				continue;
5094248571Smm			(void) printf("%s ioctl %s\n", tbuf,
5095248571Smm			    fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
5096248571Smm			if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
5097248571Smm				(void) printf("    input:\n");
5098248571Smm				dump_nvlist(fnvlist_lookup_nvlist(rec,
5099248571Smm				    ZPOOL_HIST_INPUT_NVL), 8);
5100248571Smm			}
5101248571Smm			if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
5102248571Smm				(void) printf("    output:\n");
5103248571Smm				dump_nvlist(fnvlist_lookup_nvlist(rec,
5104248571Smm				    ZPOOL_HIST_OUTPUT_NVL), 8);
5105248571Smm			}
5106248571Smm		} else {
5107248571Smm			if (!cb->internal)
5108248571Smm				continue;
5109248571Smm			(void) printf("%s unrecognized record:\n", tbuf);
5110248571Smm			dump_nvlist(rec, 4);
5111168404Spjd		}
5112185029Spjd
5113185029Spjd		if (!cb->longfmt) {
5114185029Spjd			(void) printf("\n");
5115185029Spjd			continue;
5116185029Spjd		}
5117185029Spjd		(void) printf(" [");
5118248571Smm		if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
5119248571Smm			uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
5120248571Smm			struct passwd *pwd = getpwuid(who);
5121248571Smm			(void) printf("user %d ", (int)who);
5122248571Smm			if (pwd != NULL)
5123248571Smm				(void) printf("(%s) ", pwd->pw_name);
5124185029Spjd		}
5125248571Smm		if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
5126248571Smm			(void) printf("on %s",
5127248571Smm			    fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
5128185029Spjd		}
5129248571Smm		if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
5130248571Smm			(void) printf(":%s",
5131248571Smm			    fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
5132185029Spjd		}
5133185029Spjd		(void) printf("]");
5134185029Spjd		(void) printf("\n");
5135168404Spjd	}
5136168404Spjd	(void) printf("\n");
5137168404Spjd	nvlist_free(nvhis);
5138168404Spjd
5139168404Spjd	return (ret);
5140168404Spjd}
5141168404Spjd
5142168404Spjd/*
5143168404Spjd * zpool history <pool>
5144168404Spjd *
5145168404Spjd * Displays the history of commands that modified pools.
5146168404Spjd */
5147168404Spjdint
5148168404Spjdzpool_do_history(int argc, char **argv)
5149168404Spjd{
5150185029Spjd	hist_cbdata_t cbdata = { 0 };
5151168404Spjd	int ret;
5152185029Spjd	int c;
5153168404Spjd
5154185029Spjd	cbdata.first = B_TRUE;
5155185029Spjd	/* check options */
5156185029Spjd	while ((c = getopt(argc, argv, "li")) != -1) {
5157185029Spjd		switch (c) {
5158185029Spjd		case 'l':
5159248571Smm			cbdata.longfmt = B_TRUE;
5160185029Spjd			break;
5161185029Spjd		case 'i':
5162248571Smm			cbdata.internal = B_TRUE;
5163185029Spjd			break;
5164185029Spjd		case '?':
5165185029Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
5166185029Spjd			    optopt);
5167185029Spjd			usage(B_FALSE);
5168185029Spjd		}
5169185029Spjd	}
5170168404Spjd	argc -= optind;
5171168404Spjd	argv += optind;
5172168404Spjd
5173168404Spjd	ret = for_each_pool(argc, argv, B_FALSE,  NULL, get_history_one,
5174185029Spjd	    &cbdata);
5175168404Spjd
5176185029Spjd	if (argc == 0 && cbdata.first == B_TRUE) {
5177168404Spjd		(void) printf(gettext("no pools available\n"));
5178168404Spjd		return (0);
5179168404Spjd	}
5180168404Spjd
5181168404Spjd	return (ret);
5182168404Spjd}
5183168404Spjd
5184168404Spjdstatic int
5185168404Spjdget_callback(zpool_handle_t *zhp, void *data)
5186168404Spjd{
5187185029Spjd	zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5188168404Spjd	char value[MAXNAMELEN];
5189185029Spjd	zprop_source_t srctype;
5190185029Spjd	zprop_list_t *pl;
5191168404Spjd
5192168404Spjd	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5193168404Spjd
5194168404Spjd		/*
5195185029Spjd		 * Skip the special fake placeholder. This will also skip
5196185029Spjd		 * over the name property when 'all' is specified.
5197168404Spjd		 */
5198185029Spjd		if (pl->pl_prop == ZPOOL_PROP_NAME &&
5199168404Spjd		    pl == cbp->cb_proplist)
5200168404Spjd			continue;
5201168404Spjd
5202236884Smm		if (pl->pl_prop == ZPROP_INVAL &&
5203236884Smm		    (zpool_prop_feature(pl->pl_user_prop) ||
5204236884Smm		    zpool_prop_unsupported(pl->pl_user_prop))) {
5205236884Smm			srctype = ZPROP_SRC_LOCAL;
5206168404Spjd
5207236884Smm			if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5208236884Smm			    value, sizeof (value)) == 0) {
5209236884Smm				zprop_print_one_property(zpool_get_name(zhp),
5210236884Smm				    cbp, pl->pl_user_prop, value, srctype,
5211236884Smm				    NULL, NULL);
5212236884Smm			}
5213236884Smm		} else {
5214236884Smm			if (zpool_get_prop(zhp, pl->pl_prop, value,
5215263889Sdelphij			    sizeof (value), &srctype, cbp->cb_literal) != 0)
5216236884Smm				continue;
5217236884Smm
5218236884Smm			zprop_print_one_property(zpool_get_name(zhp), cbp,
5219236884Smm			    zpool_prop_to_name(pl->pl_prop), value, srctype,
5220236884Smm			    NULL, NULL);
5221236884Smm		}
5222168404Spjd	}
5223168404Spjd	return (0);
5224168404Spjd}
5225168404Spjd
5226263889Sdelphij/*
5227263889Sdelphij * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
5228263889Sdelphij *
5229263889Sdelphij *	-H	Scripted mode.  Don't display headers, and separate properties
5230263889Sdelphij *		by a single tab.
5231263889Sdelphij *	-o	List of columns to display.  Defaults to
5232263889Sdelphij *		"name,property,value,source".
5233263889Sdelphij * 	-p	Diplay values in parsable (exact) format.
5234263889Sdelphij *
5235263889Sdelphij * Get properties of pools in the system. Output space statistics
5236263889Sdelphij * for each one as well as other attributes.
5237263889Sdelphij */
5238168404Spjdint
5239168404Spjdzpool_do_get(int argc, char **argv)
5240168404Spjd{
5241185029Spjd	zprop_get_cbdata_t cb = { 0 };
5242185029Spjd	zprop_list_t fake_name = { 0 };
5243168404Spjd	int ret;
5244263889Sdelphij	int c, i;
5245263889Sdelphij	char *value;
5246168404Spjd
5247263889Sdelphij	cb.cb_first = B_TRUE;
5248168404Spjd
5249263889Sdelphij	/*
5250263889Sdelphij	 * Set up default columns and sources.
5251263889Sdelphij	 */
5252185029Spjd	cb.cb_sources = ZPROP_SRC_ALL;
5253168404Spjd	cb.cb_columns[0] = GET_COL_NAME;
5254168404Spjd	cb.cb_columns[1] = GET_COL_PROPERTY;
5255168404Spjd	cb.cb_columns[2] = GET_COL_VALUE;
5256168404Spjd	cb.cb_columns[3] = GET_COL_SOURCE;
5257185029Spjd	cb.cb_type = ZFS_TYPE_POOL;
5258168404Spjd
5259263889Sdelphij	/* check options */
5260263889Sdelphij	while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
5261263889Sdelphij		switch (c) {
5262263889Sdelphij		case 'p':
5263263889Sdelphij			cb.cb_literal = B_TRUE;
5264263889Sdelphij			break;
5265263889Sdelphij		case 'H':
5266263889Sdelphij			cb.cb_scripted = B_TRUE;
5267263889Sdelphij			break;
5268263889Sdelphij		case 'o':
5269263889Sdelphij			bzero(&cb.cb_columns, sizeof (cb.cb_columns));
5270263889Sdelphij			i = 0;
5271263889Sdelphij			while (*optarg != '\0') {
5272263889Sdelphij				static char *col_subopts[] =
5273263889Sdelphij				{ "name", "property", "value", "source",
5274263889Sdelphij				"all", NULL };
5275263889Sdelphij
5276263889Sdelphij				if (i == ZFS_GET_NCOLS) {
5277263889Sdelphij					(void) fprintf(stderr, gettext("too "
5278263889Sdelphij					"many fields given to -o "
5279263889Sdelphij					"option\n"));
5280263889Sdelphij					usage(B_FALSE);
5281263889Sdelphij				}
5282263889Sdelphij
5283263889Sdelphij				switch (getsubopt(&optarg, col_subopts,
5284263889Sdelphij				    &value)) {
5285263889Sdelphij				case 0:
5286263889Sdelphij					cb.cb_columns[i++] = GET_COL_NAME;
5287263889Sdelphij					break;
5288263889Sdelphij				case 1:
5289263889Sdelphij					cb.cb_columns[i++] = GET_COL_PROPERTY;
5290263889Sdelphij					break;
5291263889Sdelphij				case 2:
5292263889Sdelphij					cb.cb_columns[i++] = GET_COL_VALUE;
5293263889Sdelphij					break;
5294263889Sdelphij				case 3:
5295263889Sdelphij					cb.cb_columns[i++] = GET_COL_SOURCE;
5296263889Sdelphij					break;
5297263889Sdelphij				case 4:
5298263889Sdelphij					if (i > 0) {
5299263889Sdelphij						(void) fprintf(stderr,
5300263889Sdelphij						    gettext("\"all\" conflicts "
5301263889Sdelphij						    "with specific fields "
5302263889Sdelphij						    "given to -o option\n"));
5303263889Sdelphij						usage(B_FALSE);
5304263889Sdelphij					}
5305263889Sdelphij					cb.cb_columns[0] = GET_COL_NAME;
5306263889Sdelphij					cb.cb_columns[1] = GET_COL_PROPERTY;
5307263889Sdelphij					cb.cb_columns[2] = GET_COL_VALUE;
5308263889Sdelphij					cb.cb_columns[3] = GET_COL_SOURCE;
5309263889Sdelphij					i = ZFS_GET_NCOLS;
5310263889Sdelphij					break;
5311263889Sdelphij				default:
5312263889Sdelphij					(void) fprintf(stderr,
5313263889Sdelphij					    gettext("invalid column name "
5314263889Sdelphij					    "'%s'\n"), value);
5315263889Sdelphij					usage(B_FALSE);
5316263889Sdelphij				}
5317263889Sdelphij			}
5318263889Sdelphij			break;
5319263889Sdelphij		case '?':
5320263889Sdelphij			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
5321263889Sdelphij			    optopt);
5322263889Sdelphij			usage(B_FALSE);
5323263889Sdelphij		}
5324263889Sdelphij	}
5325263889Sdelphij
5326263889Sdelphij	argc -= optind;
5327263889Sdelphij	argv += optind;
5328263889Sdelphij
5329263889Sdelphij	if (argc < 1) {
5330263889Sdelphij		(void) fprintf(stderr, gettext("missing property "
5331263889Sdelphij		    "argument\n"));
5332263889Sdelphij		usage(B_FALSE);
5333263889Sdelphij	}
5334263889Sdelphij
5335263889Sdelphij	if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
5336185029Spjd	    ZFS_TYPE_POOL) != 0)
5337168404Spjd		usage(B_FALSE);
5338168404Spjd
5339263889Sdelphij	argc--;
5340263889Sdelphij	argv++;
5341263889Sdelphij
5342168404Spjd	if (cb.cb_proplist != NULL) {
5343185029Spjd		fake_name.pl_prop = ZPOOL_PROP_NAME;
5344168404Spjd		fake_name.pl_width = strlen(gettext("NAME"));
5345168404Spjd		fake_name.pl_next = cb.cb_proplist;
5346168404Spjd		cb.cb_proplist = &fake_name;
5347168404Spjd	}
5348168404Spjd
5349263889Sdelphij	ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
5350168404Spjd	    get_callback, &cb);
5351168404Spjd
5352168404Spjd	if (cb.cb_proplist == &fake_name)
5353185029Spjd		zprop_free_list(fake_name.pl_next);
5354168404Spjd	else
5355185029Spjd		zprop_free_list(cb.cb_proplist);
5356168404Spjd
5357168404Spjd	return (ret);
5358168404Spjd}
5359168404Spjd
5360168404Spjdtypedef struct set_cbdata {
5361168404Spjd	char *cb_propname;
5362168404Spjd	char *cb_value;
5363168404Spjd	boolean_t cb_any_successful;
5364168404Spjd} set_cbdata_t;
5365168404Spjd
5366168404Spjdint
5367168404Spjdset_callback(zpool_handle_t *zhp, void *data)
5368168404Spjd{
5369168404Spjd	int error;
5370168404Spjd	set_cbdata_t *cb = (set_cbdata_t *)data;
5371168404Spjd
5372168404Spjd	error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5373168404Spjd
5374168404Spjd	if (!error)
5375168404Spjd		cb->cb_any_successful = B_TRUE;
5376168404Spjd
5377168404Spjd	return (error);
5378168404Spjd}
5379168404Spjd
5380168404Spjdint
5381168404Spjdzpool_do_set(int argc, char **argv)
5382168404Spjd{
5383168404Spjd	set_cbdata_t cb = { 0 };
5384168404Spjd	int error;
5385168404Spjd
5386168404Spjd	if (argc > 1 && argv[1][0] == '-') {
5387168404Spjd		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
5388168404Spjd		    argv[1][1]);
5389168404Spjd		usage(B_FALSE);
5390168404Spjd	}
5391168404Spjd
5392168404Spjd	if (argc < 2) {
5393168404Spjd		(void) fprintf(stderr, gettext("missing property=value "
5394168404Spjd		    "argument\n"));
5395168404Spjd		usage(B_FALSE);
5396168404Spjd	}
5397168404Spjd
5398168404Spjd	if (argc < 3) {
5399168404Spjd		(void) fprintf(stderr, gettext("missing pool name\n"));
5400168404Spjd		usage(B_FALSE);
5401168404Spjd	}
5402168404Spjd
5403168404Spjd	if (argc > 3) {
5404168404Spjd		(void) fprintf(stderr, gettext("too many pool names\n"));
5405168404Spjd		usage(B_FALSE);
5406168404Spjd	}
5407168404Spjd
5408168404Spjd	cb.cb_propname = argv[1];
5409168404Spjd	cb.cb_value = strchr(cb.cb_propname, '=');
5410168404Spjd	if (cb.cb_value == NULL) {
5411168404Spjd		(void) fprintf(stderr, gettext("missing value in "
5412168404Spjd		    "property=value argument\n"));
5413168404Spjd		usage(B_FALSE);
5414168404Spjd	}
5415168404Spjd
5416168404Spjd	*(cb.cb_value) = '\0';
5417168404Spjd	cb.cb_value++;
5418168404Spjd
5419168404Spjd	error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5420168404Spjd	    set_callback, &cb);
5421168404Spjd
5422168404Spjd	return (error);
5423168404Spjd}
5424168404Spjd
5425168404Spjdstatic int
5426168404Spjdfind_command_idx(char *command, int *idx)
5427168404Spjd{
5428168404Spjd	int i;
5429168404Spjd
5430168404Spjd	for (i = 0; i < NCOMMAND; i++) {
5431168404Spjd		if (command_table[i].name == NULL)
5432168404Spjd			continue;
5433168404Spjd
5434168404Spjd		if (strcmp(command, command_table[i].name) == 0) {
5435168404Spjd			*idx = i;
5436168404Spjd			return (0);
5437168404Spjd		}
5438168404Spjd	}
5439168404Spjd	return (1);
5440168404Spjd}
5441168404Spjd
5442168404Spjdint
5443168404Spjdmain(int argc, char **argv)
5444168404Spjd{
5445168404Spjd	int ret;
5446168404Spjd	int i;
5447168404Spjd	char *cmdname;
5448168404Spjd
5449168404Spjd	(void) setlocale(LC_ALL, "");
5450168404Spjd	(void) textdomain(TEXT_DOMAIN);
5451168404Spjd
5452168404Spjd	if ((g_zfs = libzfs_init()) == NULL) {
5453168404Spjd		(void) fprintf(stderr, gettext("internal error: failed to "
5454168404Spjd		    "initialize ZFS library\n"));
5455168404Spjd		return (1);
5456168404Spjd	}
5457168404Spjd
5458168404Spjd	libzfs_print_on_error(g_zfs, B_TRUE);
5459168404Spjd
5460168404Spjd	opterr = 0;
5461168404Spjd
5462168404Spjd	/*
5463168404Spjd	 * Make sure the user has specified some command.
5464168404Spjd	 */
5465168404Spjd	if (argc < 2) {
5466168404Spjd		(void) fprintf(stderr, gettext("missing command\n"));
5467168404Spjd		usage(B_FALSE);
5468168404Spjd	}
5469168404Spjd
5470168404Spjd	cmdname = argv[1];
5471168404Spjd
5472168404Spjd	/*
5473168404Spjd	 * Special case '-?'
5474168404Spjd	 */
5475168404Spjd	if (strcmp(cmdname, "-?") == 0)
5476168404Spjd		usage(B_TRUE);
5477168404Spjd
5478248571Smm	zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
5479185029Spjd
5480168404Spjd	/*
5481168404Spjd	 * Run the appropriate command.
5482168404Spjd	 */
5483168404Spjd	if (find_command_idx(cmdname, &i) == 0) {
5484168404Spjd		current_command = &command_table[i];
5485168404Spjd		ret = command_table[i].func(argc - 1, argv + 1);
5486185029Spjd	} else if (strchr(cmdname, '=')) {
5487185029Spjd		verify(find_command_idx("set", &i) == 0);
5488185029Spjd		current_command = &command_table[i];
5489185029Spjd		ret = command_table[i].func(argc, argv);
5490185029Spjd	} else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5491185029Spjd		/*
5492185029Spjd		 * 'freeze' is a vile debugging abomination, so we treat
5493185029Spjd		 * it as such.
5494185029Spjd		 */
5495252059Ssmh		zfs_cmd_t zc = { 0 };
5496252059Ssmh		(void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name));
5497252059Ssmh		return (!!zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc));
5498185029Spjd	} else {
5499168404Spjd		(void) fprintf(stderr, gettext("unrecognized "
5500168404Spjd		    "command '%s'\n"), cmdname);
5501168404Spjd		usage(B_FALSE);
5502168404Spjd	}
5503168404Spjd
5504248571Smm	if (ret == 0 && log_history)
5505248571Smm		(void) zpool_log_history(g_zfs, history_str);
5506248571Smm
5507168404Spjd	libzfs_fini(g_zfs);
5508168404Spjd
5509168404Spjd	/*
5510168404Spjd	 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5511168404Spjd	 * for the purposes of running ::findleaks.
5512168404Spjd	 */
5513168404Spjd	if (getenv("ZFS_ABORT") != NULL) {
5514168404Spjd		(void) printf("dumping core by request\n");
5515168404Spjd		abort();
5516168404Spjd	}
5517168404Spjd
5518168404Spjd	return (ret);
5519168404Spjd}
5520