zpool_main.c revision 254758
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.
25236155Smm * Copyright (c) 2012 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:
239236960Smm		return (gettext("\tlist [-Hv] [-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:
251236155Smm		return (""); /* Undocumented command */
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:
261185029Spjd		return (gettext("\tget <\"all\" | property[,...]> "
262168404Spjd		    "<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) {
1007236884Smm			int 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
1705168404Spjd	default:
1706168404Spjd		/*
1707168404Spjd		 * No other status can be seen when importing pools.
1708168404Spjd		 */
1709168404Spjd		assert(reason == ZPOOL_STATUS_OK);
1710168404Spjd	}
1711168404Spjd
1712168404Spjd	/*
1713168404Spjd	 * Print out an action according to the overall state of the pool.
1714168404Spjd	 */
1715168404Spjd	if (vs->vs_state == VDEV_STATE_HEALTHY) {
1716238926Smm		if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1717238926Smm		    reason == ZPOOL_STATUS_FEAT_DISABLED) {
1718228103Smm			(void) printf(gettext(" action: The pool can be "
1719168404Spjd			    "imported using its name or numeric identifier, "
1720168404Spjd			    "though\n\tsome features will not be available "
1721168404Spjd			    "without an explicit 'zpool upgrade'.\n"));
1722238926Smm		} else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1723228103Smm			(void) printf(gettext(" action: The pool can be "
1724168498Spjd			    "imported using its name or numeric "
1725168498Spjd			    "identifier and\n\tthe '-f' flag.\n"));
1726238926Smm		} else {
1727228103Smm			(void) printf(gettext(" action: The pool can be "
1728168404Spjd			    "imported using its name or numeric "
1729168404Spjd			    "identifier.\n"));
1730238926Smm		}
1731168404Spjd	} else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1732228103Smm		(void) printf(gettext(" action: The pool can be imported "
1733168404Spjd		    "despite missing or damaged devices.  The\n\tfault "
1734168404Spjd		    "tolerance of the pool may be compromised if imported.\n"));
1735168404Spjd	} else {
1736168404Spjd		switch (reason) {
1737168404Spjd		case ZPOOL_STATUS_VERSION_NEWER:
1738228103Smm			(void) printf(gettext(" action: The pool cannot be "
1739168404Spjd			    "imported.  Access the pool on a system running "
1740168404Spjd			    "newer\n\tsoftware, or recreate the pool from "
1741168404Spjd			    "backup.\n"));
1742168404Spjd			break;
1743236884Smm		case ZPOOL_STATUS_UNSUP_FEAT_READ:
1744236884Smm			(void) printf(gettext("action: The pool cannot be "
1745236884Smm			    "imported. Access the pool on a system that "
1746236884Smm			    "supports\n\tthe required feature(s), or recreate "
1747236884Smm			    "the pool from backup.\n"));
1748236884Smm			break;
1749236884Smm		case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1750236884Smm			(void) printf(gettext("action: The pool cannot be "
1751236884Smm			    "imported in read-write mode. Import the pool "
1752236884Smm			    "with\n"
1753236884Smm			    "\t\"-o readonly=on\", access the pool on a system "
1754236884Smm			    "that supports the\n\trequired feature(s), or "
1755236884Smm			    "recreate the pool from backup.\n"));
1756236884Smm			break;
1757168404Spjd		case ZPOOL_STATUS_MISSING_DEV_R:
1758168404Spjd		case ZPOOL_STATUS_MISSING_DEV_NR:
1759168404Spjd		case ZPOOL_STATUS_BAD_GUID_SUM:
1760228103Smm			(void) printf(gettext(" action: The pool cannot be "
1761168404Spjd			    "imported. Attach the missing\n\tdevices and try "
1762168404Spjd			    "again.\n"));
1763168404Spjd			break;
1764168404Spjd		default:
1765228103Smm			(void) printf(gettext(" action: The pool cannot be "
1766168404Spjd			    "imported due to damaged devices or data.\n"));
1767168404Spjd		}
1768168404Spjd	}
1769168404Spjd
1770228103Smm	/* Print the comment attached to the pool. */
1771228103Smm	if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1772228103Smm		(void) printf(gettext("comment: %s\n"), comment);
1773228103Smm
1774168404Spjd	/*
1775168404Spjd	 * If the state is "closed" or "can't open", and the aux state
1776168404Spjd	 * is "corrupt data":
1777168404Spjd	 */
1778168404Spjd	if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1779168404Spjd	    (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1780168404Spjd	    (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1781168404Spjd		if (pool_state == POOL_STATE_DESTROYED)
1782168404Spjd			(void) printf(gettext("\tThe pool was destroyed, "
1783168404Spjd			    "but can be imported using the '-Df' flags.\n"));
1784168404Spjd		else if (pool_state != POOL_STATE_EXPORTED)
1785168404Spjd			(void) printf(gettext("\tThe pool may be active on "
1786185029Spjd			    "another system, but can be imported using\n\t"
1787168404Spjd			    "the '-f' flag.\n"));
1788168404Spjd	}
1789168404Spjd
1790168404Spjd	if (msgid != NULL)
1791236146Smm		(void) printf(gettext("   see: http://illumos.org/msg/%s\n"),
1792168404Spjd		    msgid);
1793168404Spjd
1794228103Smm	(void) printf(gettext(" config:\n\n"));
1795168404Spjd
1796168404Spjd	namewidth = max_width(NULL, nvroot, 0, 0);
1797168404Spjd	if (namewidth < 10)
1798168404Spjd		namewidth = 10;
1799168404Spjd
1800213197Smm	print_import_config(name, nvroot, namewidth, 0);
1801213197Smm	if (num_logs(nvroot) > 0)
1802213197Smm		print_logs(NULL, nvroot, namewidth, B_FALSE);
1803185029Spjd
1804168404Spjd	if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1805168404Spjd		(void) printf(gettext("\n\tAdditional devices are known to "
1806168404Spjd		    "be part of this pool, though their\n\texact "
1807168404Spjd		    "configuration cannot be determined.\n"));
1808168404Spjd	}
1809168404Spjd}
1810168404Spjd
1811168404Spjd/*
1812168404Spjd * Perform the import for the given configuration.  This passes the heavy
1813185029Spjd * lifting off to zpool_import_props(), and then mounts the datasets contained
1814185029Spjd * within the pool.
1815168404Spjd */
1816168404Spjdstatic int
1817168404Spjddo_import(nvlist_t *config, const char *newname, const char *mntopts,
1818219089Spjd    nvlist_t *props, int flags)
1819168404Spjd{
1820168404Spjd	zpool_handle_t *zhp;
1821168404Spjd	char *name;
1822168404Spjd	uint64_t state;
1823168404Spjd	uint64_t version;
1824168404Spjd
1825168404Spjd	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1826168404Spjd	    &name) == 0);
1827168404Spjd
1828168404Spjd	verify(nvlist_lookup_uint64(config,
1829168404Spjd	    ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1830168404Spjd	verify(nvlist_lookup_uint64(config,
1831168404Spjd	    ZPOOL_CONFIG_VERSION, &version) == 0);
1832236884Smm	if (!SPA_VERSION_IS_SUPPORTED(version)) {
1833168404Spjd		(void) fprintf(stderr, gettext("cannot import '%s': pool "
1834236884Smm		    "is formatted using an unsupported ZFS version\n"), name);
1835168404Spjd		return (1);
1836219089Spjd	} else if (state != POOL_STATE_EXPORTED &&
1837219089Spjd	    !(flags & ZFS_IMPORT_ANY_HOST)) {
1838168498Spjd		uint64_t hostid;
1839168498Spjd
1840168498Spjd		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1841168498Spjd		    &hostid) == 0) {
1842168498Spjd			if ((unsigned long)hostid != gethostid()) {
1843168498Spjd				char *hostname;
1844168498Spjd				uint64_t timestamp;
1845168498Spjd				time_t t;
1846168498Spjd
1847168498Spjd				verify(nvlist_lookup_string(config,
1848168498Spjd				    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1849168498Spjd				verify(nvlist_lookup_uint64(config,
1850168498Spjd				    ZPOOL_CONFIG_TIMESTAMP, &timestamp) == 0);
1851168498Spjd				t = timestamp;
1852168498Spjd				(void) fprintf(stderr, gettext("cannot import "
1853168498Spjd				    "'%s': pool may be in use from other "
1854168498Spjd				    "system, it was last accessed by %s "
1855168498Spjd				    "(hostid: 0x%lx) on %s"), name, hostname,
1856168498Spjd				    (unsigned long)hostid,
1857168498Spjd				    asctime(localtime(&t)));
1858168498Spjd				(void) fprintf(stderr, gettext("use '-f' to "
1859168498Spjd				    "import anyway\n"));
1860168498Spjd				return (1);
1861168498Spjd			}
1862168498Spjd		} else {
1863168498Spjd			(void) fprintf(stderr, gettext("cannot import '%s': "
1864168498Spjd			    "pool may be in use from other system\n"), name);
1865168498Spjd			(void) fprintf(stderr, gettext("use '-f' to import "
1866168498Spjd			    "anyway\n"));
1867168498Spjd			return (1);
1868168498Spjd		}
1869168404Spjd	}
1870168404Spjd
1871219089Spjd	if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1872168404Spjd		return (1);
1873168404Spjd
1874168404Spjd	if (newname != NULL)
1875168404Spjd		name = (char *)newname;
1876168404Spjd
1877209962Smm	if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1878209962Smm		return (1);
1879168404Spjd
1880209962Smm	if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1881219089Spjd	    !(flags & ZFS_IMPORT_ONLY) &&
1882209962Smm	    zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1883168404Spjd		zpool_close(zhp);
1884168404Spjd		return (1);
1885168404Spjd	}
1886168404Spjd
1887168404Spjd	zpool_close(zhp);
1888219089Spjd	return (0);
1889168404Spjd}
1890168404Spjd
1891168404Spjd/*
1892168404Spjd * zpool import [-d dir] [-D]
1893185029Spjd *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1894185029Spjd *              [-d dir | -c cachefile] [-f] -a
1895185029Spjd *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1896219089Spjd *              [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1897168404Spjd *
1898185029Spjd *	 -c	Read pool information from a cachefile instead of searching
1899185029Spjd *		devices.
1900185029Spjd *
1901168404Spjd *       -d	Scan in a specific directory, other than /dev/dsk.  More than
1902168404Spjd *		one directory can be specified using multiple '-d' options.
1903168404Spjd *
1904168404Spjd *       -D     Scan for previously destroyed pools or import all or only
1905168404Spjd *              specified destroyed pools.
1906168404Spjd *
1907168404Spjd *       -R	Temporarily import the pool, with all mountpoints relative to
1908168404Spjd *		the given root.  The pool will remain exported when the machine
1909168404Spjd *		is rebooted.
1910168404Spjd *
1911219089Spjd *       -V	Import even in the presence of faulted vdevs.  This is an
1912185029Spjd *       	intentionally undocumented option for testing purposes, and
1913185029Spjd *       	treats the pool configuration as complete, leaving any bad
1914209962Smm *		vdevs in the FAULTED state. In other words, it does verbatim
1915209962Smm *		import.
1916185029Spjd *
1917219089Spjd *       -f	Force import, even if it appears that the pool is active.
1918219089Spjd *
1919219089Spjd *       -F     Attempt rewind if necessary.
1920219089Spjd *
1921219089Spjd *       -n     See if rewind would work, but don't actually rewind.
1922219089Spjd *
1923219089Spjd *       -N     Import the pool but don't mount datasets.
1924219089Spjd *
1925219089Spjd *       -T     Specify a starting txg to use for import. This option is
1926219089Spjd *       	intentionally undocumented option for testing purposes.
1927219089Spjd *
1928168404Spjd *       -a	Import all pools found.
1929168404Spjd *
1930185029Spjd *       -o	Set property=value and/or temporary mount options (without '=').
1931185029Spjd *
1932168404Spjd * The import command scans for pools to import, and import pools based on pool
1933168404Spjd * name and GUID.  The pool can also be renamed as part of the import process.
1934168404Spjd */
1935168404Spjdint
1936168404Spjdzpool_do_import(int argc, char **argv)
1937168404Spjd{
1938168404Spjd	char **searchdirs = NULL;
1939168404Spjd	int nsearch = 0;
1940168404Spjd	int c;
1941219089Spjd	int err = 0;
1942185029Spjd	nvlist_t *pools = NULL;
1943168404Spjd	boolean_t do_all = B_FALSE;
1944168404Spjd	boolean_t do_destroyed = B_FALSE;
1945168404Spjd	char *mntopts = NULL;
1946168404Spjd	nvpair_t *elem;
1947168404Spjd	nvlist_t *config;
1948185029Spjd	uint64_t searchguid = 0;
1949185029Spjd	char *searchname = NULL;
1950185029Spjd	char *propval;
1951168404Spjd	nvlist_t *found_config;
1952219089Spjd	nvlist_t *policy = NULL;
1953185029Spjd	nvlist_t *props = NULL;
1954168404Spjd	boolean_t first;
1955219089Spjd	int flags = ZFS_IMPORT_NORMAL;
1956219089Spjd	uint32_t rewind_policy = ZPOOL_NO_REWIND;
1957219089Spjd	boolean_t dryrun = B_FALSE;
1958219089Spjd	boolean_t do_rewind = B_FALSE;
1959219089Spjd	boolean_t xtreme_rewind = B_FALSE;
1960219089Spjd	uint64_t pool_state, txg = -1ULL;
1961185029Spjd	char *cachefile = NULL;
1962219089Spjd	importargs_t idata = { 0 };
1963219089Spjd	char *endptr;
1964168404Spjd
1965168404Spjd	/* check options */
1966219089Spjd	while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1967168404Spjd		switch (c) {
1968168404Spjd		case 'a':
1969168404Spjd			do_all = B_TRUE;
1970168404Spjd			break;
1971185029Spjd		case 'c':
1972185029Spjd			cachefile = optarg;
1973185029Spjd			break;
1974168404Spjd		case 'd':
1975168404Spjd			if (searchdirs == NULL) {
1976168404Spjd				searchdirs = safe_malloc(sizeof (char *));
1977168404Spjd			} else {
1978168404Spjd				char **tmp = safe_malloc((nsearch + 1) *
1979168404Spjd				    sizeof (char *));
1980168404Spjd				bcopy(searchdirs, tmp, nsearch *
1981168404Spjd				    sizeof (char *));
1982168404Spjd				free(searchdirs);
1983168404Spjd				searchdirs = tmp;
1984168404Spjd			}
1985168404Spjd			searchdirs[nsearch++] = optarg;
1986168404Spjd			break;
1987168404Spjd		case 'D':
1988168404Spjd			do_destroyed = B_TRUE;
1989168404Spjd			break;
1990168404Spjd		case 'f':
1991219089Spjd			flags |= ZFS_IMPORT_ANY_HOST;
1992168404Spjd			break;
1993185029Spjd		case 'F':
1994219089Spjd			do_rewind = B_TRUE;
1995185029Spjd			break;
1996219089Spjd		case 'm':
1997219089Spjd			flags |= ZFS_IMPORT_MISSING_LOG;
1998219089Spjd			break;
1999219089Spjd		case 'n':
2000219089Spjd			dryrun = B_TRUE;
2001219089Spjd			break;
2002219089Spjd		case 'N':
2003219089Spjd			flags |= ZFS_IMPORT_ONLY;
2004219089Spjd			break;
2005168404Spjd		case 'o':
2006185029Spjd			if ((propval = strchr(optarg, '=')) != NULL) {
2007185029Spjd				*propval = '\0';
2008185029Spjd				propval++;
2009185029Spjd				if (add_prop_list(optarg, propval,
2010185029Spjd				    &props, B_TRUE))
2011185029Spjd					goto error;
2012185029Spjd			} else {
2013185029Spjd				mntopts = optarg;
2014185029Spjd			}
2015168404Spjd			break;
2016168404Spjd		case 'R':
2017185029Spjd			if (add_prop_list(zpool_prop_to_name(
2018185029Spjd			    ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2019185029Spjd				goto error;
2020185029Spjd			if (nvlist_lookup_string(props,
2021185029Spjd			    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
2022185029Spjd			    &propval) == 0)
2023185029Spjd				break;
2024185029Spjd			if (add_prop_list(zpool_prop_to_name(
2025185029Spjd			    ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2026185029Spjd				goto error;
2027168404Spjd			break;
2028219089Spjd		case 'T':
2029219089Spjd			errno = 0;
2030219089Spjd			txg = strtoull(optarg, &endptr, 10);
2031219089Spjd			if (errno != 0 || *endptr != '\0') {
2032219089Spjd				(void) fprintf(stderr,
2033219089Spjd				    gettext("invalid txg value\n"));
2034219089Spjd				usage(B_FALSE);
2035219089Spjd			}
2036219089Spjd			rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2037219089Spjd			break;
2038219089Spjd		case 'V':
2039219089Spjd			flags |= ZFS_IMPORT_VERBATIM;
2040219089Spjd			break;
2041219089Spjd		case 'X':
2042219089Spjd			xtreme_rewind = B_TRUE;
2043219089Spjd			break;
2044168404Spjd		case ':':
2045168404Spjd			(void) fprintf(stderr, gettext("missing argument for "
2046168404Spjd			    "'%c' option\n"), optopt);
2047168404Spjd			usage(B_FALSE);
2048168404Spjd			break;
2049168404Spjd		case '?':
2050168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2051168404Spjd			    optopt);
2052168404Spjd			usage(B_FALSE);
2053168404Spjd		}
2054168404Spjd	}
2055168404Spjd
2056168404Spjd	argc -= optind;
2057168404Spjd	argv += optind;
2058168404Spjd
2059185029Spjd	if (cachefile && nsearch != 0) {
2060185029Spjd		(void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2061185029Spjd		usage(B_FALSE);
2062185029Spjd	}
2063185029Spjd
2064219089Spjd	if ((dryrun || xtreme_rewind) && !do_rewind) {
2065219089Spjd		(void) fprintf(stderr,
2066219089Spjd		    gettext("-n or -X only meaningful with -F\n"));
2067219089Spjd		usage(B_FALSE);
2068219089Spjd	}
2069219089Spjd	if (dryrun)
2070219089Spjd		rewind_policy = ZPOOL_TRY_REWIND;
2071219089Spjd	else if (do_rewind)
2072219089Spjd		rewind_policy = ZPOOL_DO_REWIND;
2073219089Spjd	if (xtreme_rewind)
2074219089Spjd		rewind_policy |= ZPOOL_EXTREME_REWIND;
2075219089Spjd
2076219089Spjd	/* In the future, we can capture further policy and include it here */
2077219089Spjd	if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2078219089Spjd	    nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2079219089Spjd	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2080219089Spjd		goto error;
2081219089Spjd
2082168404Spjd	if (searchdirs == NULL) {
2083168404Spjd		searchdirs = safe_malloc(sizeof (char *));
2084235478Savg		searchdirs[0] = "/dev";
2085168404Spjd		nsearch = 1;
2086168404Spjd	}
2087168404Spjd
2088168404Spjd	/* check argument count */
2089168404Spjd	if (do_all) {
2090168404Spjd		if (argc != 0) {
2091168404Spjd			(void) fprintf(stderr, gettext("too many arguments\n"));
2092168404Spjd			usage(B_FALSE);
2093168404Spjd		}
2094168404Spjd	} else {
2095168404Spjd		if (argc > 2) {
2096168404Spjd			(void) fprintf(stderr, gettext("too many arguments\n"));
2097168404Spjd			usage(B_FALSE);
2098168404Spjd		}
2099168404Spjd
2100168404Spjd		/*
2101168404Spjd		 * Check for the SYS_CONFIG privilege.  We do this explicitly
2102168404Spjd		 * here because otherwise any attempt to discover pools will
2103168404Spjd		 * silently fail.
2104168404Spjd		 */
2105168404Spjd		if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2106168404Spjd			(void) fprintf(stderr, gettext("cannot "
2107168404Spjd			    "discover pools: permission denied\n"));
2108168404Spjd			free(searchdirs);
2109219089Spjd			nvlist_free(policy);
2110168404Spjd			return (1);
2111168404Spjd		}
2112168404Spjd	}
2113168404Spjd
2114168404Spjd	/*
2115168404Spjd	 * Depending on the arguments given, we do one of the following:
2116168404Spjd	 *
2117168404Spjd	 *	<none>	Iterate through all pools and display information about
2118168404Spjd	 *		each one.
2119168404Spjd	 *
2120168404Spjd	 *	-a	Iterate through all pools and try to import each one.
2121168404Spjd	 *
2122168404Spjd	 *	<id>	Find the pool that corresponds to the given GUID/pool
2123168404Spjd	 *		name and import that one.
2124168404Spjd	 *
2125168404Spjd	 *	-D	Above options applies only to destroyed pools.
2126168404Spjd	 */
2127168404Spjd	if (argc != 0) {
2128168404Spjd		char *endptr;
2129168404Spjd
2130168404Spjd		errno = 0;
2131168404Spjd		searchguid = strtoull(argv[0], &endptr, 10);
2132254758Sdelphij		if (errno != 0 || *endptr != '\0') {
2133168404Spjd			searchname = argv[0];
2134254758Sdelphij			searchguid = 0;
2135254758Sdelphij		}
2136168404Spjd		found_config = NULL;
2137168404Spjd
2138185029Spjd		/*
2139219089Spjd		 * User specified a name or guid.  Ensure it's unique.
2140185029Spjd		 */
2141219089Spjd		idata.unique = B_TRUE;
2142185029Spjd	}
2143185029Spjd
2144219089Spjd
2145219089Spjd	idata.path = searchdirs;
2146219089Spjd	idata.paths = nsearch;
2147219089Spjd	idata.poolname = searchname;
2148219089Spjd	idata.guid = searchguid;
2149219089Spjd	idata.cachefile = cachefile;
2150219089Spjd
2151219089Spjd	pools = zpool_search_import(g_zfs, &idata);
2152219089Spjd
2153219089Spjd	if (pools != NULL && idata.exists &&
2154219089Spjd	    (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2155219089Spjd		(void) fprintf(stderr, gettext("cannot import '%s': "
2156219089Spjd		    "a pool with that name already exists\n"),
2157219089Spjd		    argv[0]);
2158219089Spjd		(void) fprintf(stderr, gettext("use the form '%s "
2159219089Spjd		    "<pool | id> <newpool>' to give it a new name\n"),
2160219089Spjd		    "zpool import");
2161219089Spjd		err = 1;
2162219089Spjd	} else if (pools == NULL && idata.exists) {
2163219089Spjd		(void) fprintf(stderr, gettext("cannot import '%s': "
2164219089Spjd		    "a pool with that name is already created/imported,\n"),
2165219089Spjd		    argv[0]);
2166219089Spjd		(void) fprintf(stderr, gettext("and no additional pools "
2167219089Spjd		    "with that name were found\n"));
2168219089Spjd		err = 1;
2169219089Spjd	} else if (pools == NULL) {
2170185029Spjd		if (argc != 0) {
2171185029Spjd			(void) fprintf(stderr, gettext("cannot import '%s': "
2172185029Spjd			    "no such pool available\n"), argv[0]);
2173185029Spjd		}
2174219089Spjd		err = 1;
2175219089Spjd	}
2176219089Spjd
2177219089Spjd	if (err == 1) {
2178185029Spjd		free(searchdirs);
2179219089Spjd		nvlist_free(policy);
2180185029Spjd		return (1);
2181185029Spjd	}
2182185029Spjd
2183185029Spjd	/*
2184185029Spjd	 * At this point we have a list of import candidate configs. Even if
2185185029Spjd	 * we were searching by pool name or guid, we still need to
2186185029Spjd	 * post-process the list to deal with pool state and possible
2187185029Spjd	 * duplicate names.
2188185029Spjd	 */
2189168404Spjd	err = 0;
2190168404Spjd	elem = NULL;
2191168404Spjd	first = B_TRUE;
2192168404Spjd	while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2193168404Spjd
2194168404Spjd		verify(nvpair_value_nvlist(elem, &config) == 0);
2195168404Spjd
2196168404Spjd		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2197168404Spjd		    &pool_state) == 0);
2198168404Spjd		if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2199168404Spjd			continue;
2200168404Spjd		if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2201168404Spjd			continue;
2202168404Spjd
2203219089Spjd		verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2204219089Spjd		    policy) == 0);
2205219089Spjd
2206168404Spjd		if (argc == 0) {
2207168404Spjd			if (first)
2208168404Spjd				first = B_FALSE;
2209168404Spjd			else if (!do_all)
2210168404Spjd				(void) printf("\n");
2211168404Spjd
2212219089Spjd			if (do_all) {
2213168404Spjd				err |= do_import(config, NULL, mntopts,
2214219089Spjd				    props, flags);
2215219089Spjd			} else {
2216168404Spjd				show_import(config);
2217219089Spjd			}
2218168404Spjd		} else if (searchname != NULL) {
2219168404Spjd			char *name;
2220168404Spjd
2221168404Spjd			/*
2222168404Spjd			 * We are searching for a pool based on name.
2223168404Spjd			 */
2224168404Spjd			verify(nvlist_lookup_string(config,
2225168404Spjd			    ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2226168404Spjd
2227168404Spjd			if (strcmp(name, searchname) == 0) {
2228168404Spjd				if (found_config != NULL) {
2229168404Spjd					(void) fprintf(stderr, gettext(
2230168404Spjd					    "cannot import '%s': more than "
2231168404Spjd					    "one matching pool\n"), searchname);
2232168404Spjd					(void) fprintf(stderr, gettext(
2233168404Spjd					    "import by numeric ID instead\n"));
2234168404Spjd					err = B_TRUE;
2235168404Spjd				}
2236168404Spjd				found_config = config;
2237168404Spjd			}
2238168404Spjd		} else {
2239168404Spjd			uint64_t guid;
2240168404Spjd
2241168404Spjd			/*
2242168404Spjd			 * Search for a pool by guid.
2243168404Spjd			 */
2244168404Spjd			verify(nvlist_lookup_uint64(config,
2245168404Spjd			    ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2246168404Spjd
2247168404Spjd			if (guid == searchguid)
2248168404Spjd				found_config = config;
2249168404Spjd		}
2250168404Spjd	}
2251168404Spjd
2252168404Spjd	/*
2253168404Spjd	 * If we were searching for a specific pool, verify that we found a
2254168404Spjd	 * pool, and then do the import.
2255168404Spjd	 */
2256168404Spjd	if (argc != 0 && err == 0) {
2257168404Spjd		if (found_config == NULL) {
2258168404Spjd			(void) fprintf(stderr, gettext("cannot import '%s': "
2259168404Spjd			    "no such pool available\n"), argv[0]);
2260168404Spjd			err = B_TRUE;
2261168404Spjd		} else {
2262168404Spjd			err |= do_import(found_config, argc == 1 ? NULL :
2263219089Spjd			    argv[1], mntopts, props, flags);
2264168404Spjd		}
2265168404Spjd	}
2266168404Spjd
2267168404Spjd	/*
2268168404Spjd	 * If we were just looking for pools, report an error if none were
2269168404Spjd	 * found.
2270168404Spjd	 */
2271168404Spjd	if (argc == 0 && first)
2272168404Spjd		(void) fprintf(stderr,
2273168404Spjd		    gettext("no pools available to import\n"));
2274168404Spjd
2275185029Spjderror:
2276185029Spjd	nvlist_free(props);
2277168404Spjd	nvlist_free(pools);
2278219089Spjd	nvlist_free(policy);
2279168404Spjd	free(searchdirs);
2280168404Spjd
2281168404Spjd	return (err ? 1 : 0);
2282168404Spjd}
2283168404Spjd
2284168404Spjdtypedef struct iostat_cbdata {
2285236155Smm	boolean_t cb_verbose;
2286236155Smm	int cb_namewidth;
2287236155Smm	int cb_iteration;
2288168404Spjd	zpool_list_t *cb_list;
2289168404Spjd} iostat_cbdata_t;
2290168404Spjd
2291168404Spjdstatic void
2292168404Spjdprint_iostat_separator(iostat_cbdata_t *cb)
2293168404Spjd{
2294168404Spjd	int i = 0;
2295168404Spjd
2296168404Spjd	for (i = 0; i < cb->cb_namewidth; i++)
2297168404Spjd		(void) printf("-");
2298168404Spjd	(void) printf("  -----  -----  -----  -----  -----  -----\n");
2299168404Spjd}
2300168404Spjd
2301168404Spjdstatic void
2302168404Spjdprint_iostat_header(iostat_cbdata_t *cb)
2303168404Spjd{
2304168404Spjd	(void) printf("%*s     capacity     operations    bandwidth\n",
2305168404Spjd	    cb->cb_namewidth, "");
2306219089Spjd	(void) printf("%-*s  alloc   free   read  write   read  write\n",
2307168404Spjd	    cb->cb_namewidth, "pool");
2308168404Spjd	print_iostat_separator(cb);
2309168404Spjd}
2310168404Spjd
2311168404Spjd/*
2312168404Spjd * Display a single statistic.
2313168404Spjd */
2314185029Spjdstatic void
2315168404Spjdprint_one_stat(uint64_t value)
2316168404Spjd{
2317168404Spjd	char buf[64];
2318168404Spjd
2319168404Spjd	zfs_nicenum(value, buf, sizeof (buf));
2320168404Spjd	(void) printf("  %5s", buf);
2321168404Spjd}
2322168404Spjd
2323168404Spjd/*
2324168404Spjd * Print out all the statistics for the given vdev.  This can either be the
2325168404Spjd * toplevel configuration, or called recursively.  If 'name' is NULL, then this
2326168404Spjd * is a verbose output, and we don't want to display the toplevel pool stats.
2327168404Spjd */
2328168404Spjdvoid
2329168404Spjdprint_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2330168404Spjd    nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2331168404Spjd{
2332168404Spjd	nvlist_t **oldchild, **newchild;
2333168404Spjd	uint_t c, children;
2334168404Spjd	vdev_stat_t *oldvs, *newvs;
2335168404Spjd	vdev_stat_t zerovs = { 0 };
2336168404Spjd	uint64_t tdelta;
2337168404Spjd	double scale;
2338168404Spjd	char *vname;
2339168404Spjd
2340168404Spjd	if (oldnv != NULL) {
2341219089Spjd		verify(nvlist_lookup_uint64_array(oldnv,
2342219089Spjd		    ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2343168404Spjd	} else {
2344168404Spjd		oldvs = &zerovs;
2345168404Spjd	}
2346168404Spjd
2347219089Spjd	verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2348168404Spjd	    (uint64_t **)&newvs, &c) == 0);
2349168404Spjd
2350168404Spjd	if (strlen(name) + depth > cb->cb_namewidth)
2351168404Spjd		(void) printf("%*s%s", depth, "", name);
2352168404Spjd	else
2353168404Spjd		(void) printf("%*s%s%*s", depth, "", name,
2354168404Spjd		    (int)(cb->cb_namewidth - strlen(name) - depth), "");
2355168404Spjd
2356168404Spjd	tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2357168404Spjd
2358168404Spjd	if (tdelta == 0)
2359168404Spjd		scale = 1.0;
2360168404Spjd	else
2361168404Spjd		scale = (double)NANOSEC / tdelta;
2362168404Spjd
2363168404Spjd	/* only toplevel vdevs have capacity stats */
2364168404Spjd	if (newvs->vs_space == 0) {
2365168404Spjd		(void) printf("      -      -");
2366168404Spjd	} else {
2367168404Spjd		print_one_stat(newvs->vs_alloc);
2368168404Spjd		print_one_stat(newvs->vs_space - newvs->vs_alloc);
2369168404Spjd	}
2370168404Spjd
2371168404Spjd	print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2372168404Spjd	    oldvs->vs_ops[ZIO_TYPE_READ])));
2373168404Spjd
2374168404Spjd	print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2375168404Spjd	    oldvs->vs_ops[ZIO_TYPE_WRITE])));
2376168404Spjd
2377168404Spjd	print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2378168404Spjd	    oldvs->vs_bytes[ZIO_TYPE_READ])));
2379168404Spjd
2380168404Spjd	print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2381168404Spjd	    oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2382168404Spjd
2383168404Spjd	(void) printf("\n");
2384168404Spjd
2385168404Spjd	if (!cb->cb_verbose)
2386168404Spjd		return;
2387168404Spjd
2388168404Spjd	if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2389168404Spjd	    &newchild, &children) != 0)
2390168404Spjd		return;
2391168404Spjd
2392168404Spjd	if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2393168404Spjd	    &oldchild, &c) != 0)
2394168404Spjd		return;
2395168404Spjd
2396168404Spjd	for (c = 0; c < children; c++) {
2397227497Smm		uint64_t ishole = B_FALSE, islog = B_FALSE;
2398219089Spjd
2399227497Smm		(void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2400227497Smm		    &ishole);
2401227497Smm
2402227497Smm		(void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2403227497Smm		    &islog);
2404227497Smm
2405227497Smm		if (ishole || islog)
2406219089Spjd			continue;
2407219089Spjd
2408219089Spjd		vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2409168404Spjd		print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2410168404Spjd		    newchild[c], cb, depth + 2);
2411168404Spjd		free(vname);
2412168404Spjd	}
2413185029Spjd
2414185029Spjd	/*
2415227497Smm	 * Log device section
2416227497Smm	 */
2417227497Smm
2418227497Smm	if (num_logs(newnv) > 0) {
2419227497Smm		(void) printf("%-*s      -      -      -      -      -      "
2420227497Smm		    "-\n", cb->cb_namewidth, "logs");
2421227497Smm
2422227497Smm		for (c = 0; c < children; c++) {
2423227497Smm			uint64_t islog = B_FALSE;
2424227497Smm			(void) nvlist_lookup_uint64(newchild[c],
2425227497Smm			    ZPOOL_CONFIG_IS_LOG, &islog);
2426227497Smm
2427227497Smm			if (islog) {
2428227497Smm				vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2429227497Smm				    B_FALSE);
2430227497Smm				print_vdev_stats(zhp, vname, oldnv ?
2431227497Smm				    oldchild[c] : NULL, newchild[c],
2432227497Smm				    cb, depth + 2);
2433227497Smm				free(vname);
2434227497Smm			}
2435227497Smm		}
2436227497Smm
2437227497Smm	}
2438227497Smm
2439227497Smm	/*
2440185029Spjd	 * Include level 2 ARC devices in iostat output
2441185029Spjd	 */
2442185029Spjd	if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2443185029Spjd	    &newchild, &children) != 0)
2444185029Spjd		return;
2445185029Spjd
2446185029Spjd	if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2447185029Spjd	    &oldchild, &c) != 0)
2448185029Spjd		return;
2449185029Spjd
2450185029Spjd	if (children > 0) {
2451185029Spjd		(void) printf("%-*s      -      -      -      -      -      "
2452185029Spjd		    "-\n", cb->cb_namewidth, "cache");
2453185029Spjd		for (c = 0; c < children; c++) {
2454219089Spjd			vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2455219089Spjd			    B_FALSE);
2456185029Spjd			print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2457185029Spjd			    newchild[c], cb, depth + 2);
2458185029Spjd			free(vname);
2459185029Spjd		}
2460185029Spjd	}
2461168404Spjd}
2462168404Spjd
2463168404Spjdstatic int
2464168404Spjdrefresh_iostat(zpool_handle_t *zhp, void *data)
2465168404Spjd{
2466168404Spjd	iostat_cbdata_t *cb = data;
2467168404Spjd	boolean_t missing;
2468168404Spjd
2469168404Spjd	/*
2470168404Spjd	 * If the pool has disappeared, remove it from the list and continue.
2471168404Spjd	 */
2472168404Spjd	if (zpool_refresh_stats(zhp, &missing) != 0)
2473168404Spjd		return (-1);
2474168404Spjd
2475168404Spjd	if (missing)
2476168404Spjd		pool_list_remove(cb->cb_list, zhp);
2477168404Spjd
2478168404Spjd	return (0);
2479168404Spjd}
2480168404Spjd
2481168404Spjd/*
2482168404Spjd * Callback to print out the iostats for the given pool.
2483168404Spjd */
2484168404Spjdint
2485168404Spjdprint_iostat(zpool_handle_t *zhp, void *data)
2486168404Spjd{
2487168404Spjd	iostat_cbdata_t *cb = data;
2488168404Spjd	nvlist_t *oldconfig, *newconfig;
2489168404Spjd	nvlist_t *oldnvroot, *newnvroot;
2490168404Spjd
2491168404Spjd	newconfig = zpool_get_config(zhp, &oldconfig);
2492168404Spjd
2493168404Spjd	if (cb->cb_iteration == 1)
2494168404Spjd		oldconfig = NULL;
2495168404Spjd
2496168404Spjd	verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2497168404Spjd	    &newnvroot) == 0);
2498168404Spjd
2499168404Spjd	if (oldconfig == NULL)
2500168404Spjd		oldnvroot = NULL;
2501168404Spjd	else
2502168404Spjd		verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2503168404Spjd		    &oldnvroot) == 0);
2504168404Spjd
2505168404Spjd	/*
2506168404Spjd	 * Print out the statistics for the pool.
2507168404Spjd	 */
2508168404Spjd	print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2509168404Spjd
2510168404Spjd	if (cb->cb_verbose)
2511168404Spjd		print_iostat_separator(cb);
2512168404Spjd
2513168404Spjd	return (0);
2514168404Spjd}
2515168404Spjd
2516168404Spjdint
2517168404Spjdget_namewidth(zpool_handle_t *zhp, void *data)
2518168404Spjd{
2519168404Spjd	iostat_cbdata_t *cb = data;
2520168404Spjd	nvlist_t *config, *nvroot;
2521168404Spjd
2522168404Spjd	if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2523168404Spjd		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2524168404Spjd		    &nvroot) == 0);
2525168404Spjd		if (!cb->cb_verbose)
2526168404Spjd			cb->cb_namewidth = strlen(zpool_get_name(zhp));
2527168404Spjd		else
2528236145Smm			cb->cb_namewidth = max_width(zhp, nvroot, 0,
2529236145Smm			    cb->cb_namewidth);
2530168404Spjd	}
2531168404Spjd
2532168404Spjd	/*
2533168404Spjd	 * The width must fall into the range [10,38].  The upper limit is the
2534168404Spjd	 * maximum we can have and still fit in 80 columns.
2535168404Spjd	 */
2536168404Spjd	if (cb->cb_namewidth < 10)
2537168404Spjd		cb->cb_namewidth = 10;
2538168404Spjd	if (cb->cb_namewidth > 38)
2539168404Spjd		cb->cb_namewidth = 38;
2540168404Spjd
2541168404Spjd	return (0);
2542168404Spjd}
2543168404Spjd
2544168404Spjd/*
2545219089Spjd * Parse the input string, get the 'interval' and 'count' value if there is one.
2546168404Spjd */
2547219089Spjdstatic void
2548219089Spjdget_interval_count(int *argcp, char **argv, unsigned long *iv,
2549219089Spjd    unsigned long *cnt)
2550168404Spjd{
2551168404Spjd	unsigned long interval = 0, count = 0;
2552219089Spjd	int argc = *argcp, errno;
2553168404Spjd
2554168404Spjd	/*
2555168404Spjd	 * Determine if the last argument is an integer or a pool name
2556168404Spjd	 */
2557168404Spjd	if (argc > 0 && isdigit(argv[argc - 1][0])) {
2558168404Spjd		char *end;
2559168404Spjd
2560168404Spjd		errno = 0;
2561168404Spjd		interval = strtoul(argv[argc - 1], &end, 10);
2562168404Spjd
2563168404Spjd		if (*end == '\0' && errno == 0) {
2564168404Spjd			if (interval == 0) {
2565168404Spjd				(void) fprintf(stderr, gettext("interval "
2566168404Spjd				    "cannot be zero\n"));
2567168404Spjd				usage(B_FALSE);
2568168404Spjd			}
2569168404Spjd			/*
2570168404Spjd			 * Ignore the last parameter
2571168404Spjd			 */
2572168404Spjd			argc--;
2573168404Spjd		} else {
2574168404Spjd			/*
2575168404Spjd			 * If this is not a valid number, just plow on.  The
2576168404Spjd			 * user will get a more informative error message later
2577168404Spjd			 * on.
2578168404Spjd			 */
2579168404Spjd			interval = 0;
2580168404Spjd		}
2581168404Spjd	}
2582168404Spjd
2583168404Spjd	/*
2584168404Spjd	 * If the last argument is also an integer, then we have both a count
2585219089Spjd	 * and an interval.
2586168404Spjd	 */
2587168404Spjd	if (argc > 0 && isdigit(argv[argc - 1][0])) {
2588168404Spjd		char *end;
2589168404Spjd
2590168404Spjd		errno = 0;
2591168404Spjd		count = interval;
2592168404Spjd		interval = strtoul(argv[argc - 1], &end, 10);
2593168404Spjd
2594168404Spjd		if (*end == '\0' && errno == 0) {
2595168404Spjd			if (interval == 0) {
2596168404Spjd				(void) fprintf(stderr, gettext("interval "
2597168404Spjd				    "cannot be zero\n"));
2598168404Spjd				usage(B_FALSE);
2599168404Spjd			}
2600168404Spjd
2601168404Spjd			/*
2602168404Spjd			 * Ignore the last parameter
2603168404Spjd			 */
2604168404Spjd			argc--;
2605168404Spjd		} else {
2606168404Spjd			interval = 0;
2607168404Spjd		}
2608168404Spjd	}
2609168404Spjd
2610219089Spjd	*iv = interval;
2611219089Spjd	*cnt = count;
2612219089Spjd	*argcp = argc;
2613219089Spjd}
2614219089Spjd
2615219089Spjdstatic void
2616219089Spjdget_timestamp_arg(char c)
2617219089Spjd{
2618219089Spjd	if (c == 'u')
2619219089Spjd		timestamp_fmt = UDATE;
2620219089Spjd	else if (c == 'd')
2621219089Spjd		timestamp_fmt = DDATE;
2622219089Spjd	else
2623219089Spjd		usage(B_FALSE);
2624219089Spjd}
2625219089Spjd
2626219089Spjd/*
2627219089Spjd * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2628219089Spjd *
2629219089Spjd *	-v	Display statistics for individual vdevs
2630219089Spjd *	-T	Display a timestamp in date(1) or Unix format
2631219089Spjd *
2632219089Spjd * This command can be tricky because we want to be able to deal with pool
2633219089Spjd * creation/destruction as well as vdev configuration changes.  The bulk of this
2634219089Spjd * processing is handled by the pool_list_* routines in zpool_iter.c.  We rely
2635219089Spjd * on pool_list_update() to detect the addition of new pools.  Configuration
2636219089Spjd * changes are all handled within libzfs.
2637219089Spjd */
2638219089Spjdint
2639219089Spjdzpool_do_iostat(int argc, char **argv)
2640219089Spjd{
2641219089Spjd	int c;
2642219089Spjd	int ret;
2643219089Spjd	int npools;
2644219089Spjd	unsigned long interval = 0, count = 0;
2645219089Spjd	zpool_list_t *list;
2646219089Spjd	boolean_t verbose = B_FALSE;
2647219089Spjd	iostat_cbdata_t cb;
2648219089Spjd
2649219089Spjd	/* check options */
2650219089Spjd	while ((c = getopt(argc, argv, "T:v")) != -1) {
2651219089Spjd		switch (c) {
2652219089Spjd		case 'T':
2653219089Spjd			get_timestamp_arg(*optarg);
2654219089Spjd			break;
2655219089Spjd		case 'v':
2656219089Spjd			verbose = B_TRUE;
2657219089Spjd			break;
2658219089Spjd		case '?':
2659219089Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2660219089Spjd			    optopt);
2661219089Spjd			usage(B_FALSE);
2662219089Spjd		}
2663219089Spjd	}
2664219089Spjd
2665219089Spjd	argc -= optind;
2666219089Spjd	argv += optind;
2667219089Spjd
2668219089Spjd	get_interval_count(&argc, argv, &interval, &count);
2669219089Spjd
2670168404Spjd	/*
2671168404Spjd	 * Construct the list of all interesting pools.
2672168404Spjd	 */
2673168404Spjd	ret = 0;
2674168404Spjd	if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2675168404Spjd		return (1);
2676168404Spjd
2677168404Spjd	if (pool_list_count(list) == 0 && argc != 0) {
2678168404Spjd		pool_list_free(list);
2679168404Spjd		return (1);
2680168404Spjd	}
2681168404Spjd
2682168404Spjd	if (pool_list_count(list) == 0 && interval == 0) {
2683168404Spjd		pool_list_free(list);
2684168404Spjd		(void) fprintf(stderr, gettext("no pools available\n"));
2685168404Spjd		return (1);
2686168404Spjd	}
2687168404Spjd
2688168404Spjd	/*
2689168404Spjd	 * Enter the main iostat loop.
2690168404Spjd	 */
2691168404Spjd	cb.cb_list = list;
2692168404Spjd	cb.cb_verbose = verbose;
2693168404Spjd	cb.cb_iteration = 0;
2694168404Spjd	cb.cb_namewidth = 0;
2695168404Spjd
2696168404Spjd	for (;;) {
2697168404Spjd		pool_list_update(list);
2698168404Spjd
2699168404Spjd		if ((npools = pool_list_count(list)) == 0)
2700168404Spjd			break;
2701168404Spjd
2702168404Spjd		/*
2703168404Spjd		 * Refresh all statistics.  This is done as an explicit step
2704168404Spjd		 * before calculating the maximum name width, so that any
2705168404Spjd		 * configuration changes are properly accounted for.
2706168404Spjd		 */
2707168404Spjd		(void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2708168404Spjd
2709168404Spjd		/*
2710168404Spjd		 * Iterate over all pools to determine the maximum width
2711168404Spjd		 * for the pool / device name column across all pools.
2712168404Spjd		 */
2713168404Spjd		cb.cb_namewidth = 0;
2714168404Spjd		(void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2715168404Spjd
2716219089Spjd		if (timestamp_fmt != NODATE)
2717219089Spjd			print_timestamp(timestamp_fmt);
2718219089Spjd
2719168404Spjd		/*
2720168404Spjd		 * If it's the first time, or verbose mode, print the header.
2721168404Spjd		 */
2722168404Spjd		if (++cb.cb_iteration == 1 || verbose)
2723168404Spjd			print_iostat_header(&cb);
2724168404Spjd
2725168404Spjd		(void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2726168404Spjd
2727168404Spjd		/*
2728168404Spjd		 * If there's more than one pool, and we're not in verbose mode
2729168404Spjd		 * (which prints a separator for us), then print a separator.
2730168404Spjd		 */
2731168404Spjd		if (npools > 1 && !verbose)
2732168404Spjd			print_iostat_separator(&cb);
2733168404Spjd
2734168404Spjd		if (verbose)
2735168404Spjd			(void) printf("\n");
2736168404Spjd
2737168404Spjd		/*
2738168404Spjd		 * Flush the output so that redirection to a file isn't buffered
2739168404Spjd		 * indefinitely.
2740168404Spjd		 */
2741168404Spjd		(void) fflush(stdout);
2742168404Spjd
2743168404Spjd		if (interval == 0)
2744168404Spjd			break;
2745168404Spjd
2746168404Spjd		if (count != 0 && --count == 0)
2747168404Spjd			break;
2748168404Spjd
2749168404Spjd		(void) sleep(interval);
2750168404Spjd	}
2751168404Spjd
2752168404Spjd	pool_list_free(list);
2753168404Spjd
2754168404Spjd	return (ret);
2755168404Spjd}
2756168404Spjd
2757168404Spjdtypedef struct list_cbdata {
2758236155Smm	boolean_t	cb_verbose;
2759236155Smm	int		cb_namewidth;
2760168404Spjd	boolean_t	cb_scripted;
2761185029Spjd	zprop_list_t	*cb_proplist;
2762168404Spjd} list_cbdata_t;
2763168404Spjd
2764168404Spjd/*
2765168404Spjd * Given a list of columns to display, output appropriate headers for each one.
2766168404Spjd */
2767185029Spjdstatic void
2768236155Smmprint_header(list_cbdata_t *cb)
2769168404Spjd{
2770236155Smm	zprop_list_t *pl = cb->cb_proplist;
2771236884Smm	char headerbuf[ZPOOL_MAXPROPLEN];
2772185029Spjd	const char *header;
2773185029Spjd	boolean_t first = B_TRUE;
2774185029Spjd	boolean_t right_justify;
2775236155Smm	size_t width = 0;
2776168404Spjd
2777185029Spjd	for (; pl != NULL; pl = pl->pl_next) {
2778236155Smm		width = pl->pl_width;
2779236155Smm		if (first && cb->cb_verbose) {
2780236155Smm			/*
2781236155Smm			 * Reset the width to accommodate the verbose listing
2782236155Smm			 * of devices.
2783236155Smm			 */
2784236155Smm			width = cb->cb_namewidth;
2785236155Smm		}
2786236155Smm
2787185029Spjd		if (!first)
2788168404Spjd			(void) printf("  ");
2789168404Spjd		else
2790185029Spjd			first = B_FALSE;
2791168404Spjd
2792236884Smm		right_justify = B_FALSE;
2793236884Smm		if (pl->pl_prop != ZPROP_INVAL) {
2794236884Smm			header = zpool_prop_column_name(pl->pl_prop);
2795236884Smm			right_justify = zpool_prop_align_right(pl->pl_prop);
2796236884Smm		} else {
2797236884Smm			int i;
2798185029Spjd
2799236884Smm			for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2800236884Smm				headerbuf[i] = toupper(pl->pl_user_prop[i]);
2801236884Smm			headerbuf[i] = '\0';
2802236884Smm			header = headerbuf;
2803236884Smm		}
2804236884Smm
2805185029Spjd		if (pl->pl_next == NULL && !right_justify)
2806185029Spjd			(void) printf("%s", header);
2807185029Spjd		else if (right_justify)
2808236155Smm			(void) printf("%*s", width, header);
2809185029Spjd		else
2810236155Smm			(void) printf("%-*s", width, header);
2811236155Smm
2812168404Spjd	}
2813168404Spjd
2814168404Spjd	(void) printf("\n");
2815168404Spjd}
2816168404Spjd
2817185029Spjd/*
2818185029Spjd * Given a pool and a list of properties, print out all the properties according
2819185029Spjd * to the described layout.
2820185029Spjd */
2821185029Spjdstatic void
2822236155Smmprint_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2823168404Spjd{
2824236155Smm	zprop_list_t *pl = cb->cb_proplist;
2825185029Spjd	boolean_t first = B_TRUE;
2826185029Spjd	char property[ZPOOL_MAXPROPLEN];
2827185029Spjd	char *propstr;
2828185029Spjd	boolean_t right_justify;
2829236155Smm	size_t width;
2830168404Spjd
2831185029Spjd	for (; pl != NULL; pl = pl->pl_next) {
2832236155Smm
2833236155Smm		width = pl->pl_width;
2834236155Smm		if (first && cb->cb_verbose) {
2835236155Smm			/*
2836236155Smm			 * Reset the width to accommodate the verbose listing
2837236155Smm			 * of devices.
2838236155Smm			 */
2839236155Smm			width = cb->cb_namewidth;
2840236155Smm		}
2841236155Smm
2842185029Spjd		if (!first) {
2843236155Smm			if (cb->cb_scripted)
2844168404Spjd				(void) printf("\t");
2845168404Spjd			else
2846168404Spjd				(void) printf("  ");
2847185029Spjd		} else {
2848185029Spjd			first = B_FALSE;
2849168404Spjd		}
2850168404Spjd
2851185029Spjd		right_justify = B_FALSE;
2852185029Spjd		if (pl->pl_prop != ZPROP_INVAL) {
2853236155Smm			if (pl->pl_prop == ZPOOL_PROP_EXPANDSZ &&
2854236155Smm			    zpool_get_prop_int(zhp, pl->pl_prop, NULL) == 0)
2855236155Smm				propstr = "-";
2856236155Smm			else if (zpool_get_prop(zhp, pl->pl_prop, property,
2857185029Spjd			    sizeof (property), NULL) != 0)
2858185029Spjd				propstr = "-";
2859168404Spjd			else
2860185029Spjd				propstr = property;
2861168404Spjd
2862185029Spjd			right_justify = zpool_prop_align_right(pl->pl_prop);
2863236884Smm		} else if ((zpool_prop_feature(pl->pl_user_prop) ||
2864236884Smm		    zpool_prop_unsupported(pl->pl_user_prop)) &&
2865236884Smm		    zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2866236884Smm		    sizeof (property)) == 0) {
2867236884Smm			propstr = property;
2868185029Spjd		} else {
2869185029Spjd			propstr = "-";
2870185029Spjd		}
2871168404Spjd
2872168404Spjd
2873185029Spjd		/*
2874185029Spjd		 * If this is being called in scripted mode, or if this is the
2875185029Spjd		 * last column and it is left-justified, don't include a width
2876185029Spjd		 * format specifier.
2877185029Spjd		 */
2878236155Smm		if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2879185029Spjd			(void) printf("%s", propstr);
2880185029Spjd		else if (right_justify)
2881185029Spjd			(void) printf("%*s", width, propstr);
2882185029Spjd		else
2883185029Spjd			(void) printf("%-*s", width, propstr);
2884185029Spjd	}
2885168404Spjd
2886185029Spjd	(void) printf("\n");
2887185029Spjd}
2888168404Spjd
2889236155Smmstatic void
2890236155Smmprint_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted)
2891236155Smm{
2892236155Smm	char propval[64];
2893236155Smm	boolean_t fixed;
2894236155Smm	size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
2895236155Smm
2896236155Smm	zfs_nicenum(value, propval, sizeof (propval));
2897236155Smm
2898236155Smm	if (prop == ZPOOL_PROP_EXPANDSZ && value == 0)
2899236155Smm		(void) strlcpy(propval, "-", sizeof (propval));
2900236155Smm
2901236155Smm	if (scripted)
2902236155Smm		(void) printf("\t%s", propval);
2903236155Smm	else
2904236155Smm		(void) printf("  %*s", width, propval);
2905236155Smm}
2906236155Smm
2907236155Smmvoid
2908236155Smmprint_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
2909236155Smm    list_cbdata_t *cb, int depth)
2910236155Smm{
2911236155Smm	nvlist_t **child;
2912236155Smm	vdev_stat_t *vs;
2913236155Smm	uint_t c, children;
2914236155Smm	char *vname;
2915236155Smm	boolean_t scripted = cb->cb_scripted;
2916236155Smm
2917236155Smm	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2918236155Smm	    (uint64_t **)&vs, &c) == 0);
2919236155Smm
2920236155Smm	if (name != NULL) {
2921236155Smm		if (scripted)
2922236155Smm			(void) printf("\t%s", name);
2923236155Smm		else if (strlen(name) + depth > cb->cb_namewidth)
2924236155Smm			(void) printf("%*s%s", depth, "", name);
2925236155Smm		else
2926236155Smm			(void) printf("%*s%s%*s", depth, "", name,
2927236155Smm			    (int)(cb->cb_namewidth - strlen(name) - depth), "");
2928236155Smm
2929236155Smm		/* only toplevel vdevs have capacity stats */
2930236155Smm		if (vs->vs_space == 0) {
2931236155Smm			if (scripted)
2932236155Smm				(void) printf("\t-\t-\t-");
2933236155Smm			else
2934236155Smm				(void) printf("      -      -      -");
2935236155Smm		} else {
2936236155Smm			print_one_column(ZPOOL_PROP_SIZE, vs->vs_space,
2937236155Smm			    scripted);
2938236155Smm			print_one_column(ZPOOL_PROP_CAPACITY, vs->vs_alloc,
2939236155Smm			    scripted);
2940236155Smm			print_one_column(ZPOOL_PROP_FREE,
2941236155Smm			    vs->vs_space - vs->vs_alloc, scripted);
2942236155Smm		}
2943236155Smm		print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize,
2944236155Smm		    scripted);
2945236155Smm		(void) printf("\n");
2946236155Smm	}
2947236155Smm
2948236155Smm	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2949236155Smm	    &child, &children) != 0)
2950236155Smm		return;
2951236155Smm
2952236155Smm	for (c = 0; c < children; c++) {
2953236155Smm		uint64_t ishole = B_FALSE;
2954236155Smm
2955236155Smm		if (nvlist_lookup_uint64(child[c],
2956236155Smm		    ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
2957236155Smm			continue;
2958236155Smm
2959236155Smm		vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
2960236155Smm		print_list_stats(zhp, vname, child[c], cb, depth + 2);
2961236155Smm		free(vname);
2962236155Smm	}
2963236155Smm
2964236155Smm	/*
2965236155Smm	 * Include level 2 ARC devices in iostat output
2966236155Smm	 */
2967236155Smm	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2968236155Smm	    &child, &children) != 0)
2969236155Smm		return;
2970236155Smm
2971236155Smm	if (children > 0) {
2972236155Smm		(void) printf("%-*s      -      -      -      -      -      "
2973236155Smm		    "-\n", cb->cb_namewidth, "cache");
2974236155Smm		for (c = 0; c < children; c++) {
2975236155Smm			vname = zpool_vdev_name(g_zfs, zhp, child[c],
2976236155Smm			    B_FALSE);
2977236155Smm			print_list_stats(zhp, vname, child[c], cb, depth + 2);
2978236155Smm			free(vname);
2979236155Smm		}
2980236155Smm	}
2981236155Smm}
2982236155Smm
2983236155Smm
2984185029Spjd/*
2985185029Spjd * Generic callback function to list a pool.
2986185029Spjd */
2987185029Spjdint
2988185029Spjdlist_callback(zpool_handle_t *zhp, void *data)
2989185029Spjd{
2990185029Spjd	list_cbdata_t *cbp = data;
2991236155Smm	nvlist_t *config;
2992236155Smm	nvlist_t *nvroot;
2993168404Spjd
2994236155Smm	config = zpool_get_config(zhp, NULL);
2995168404Spjd
2996236155Smm	print_pool(zhp, cbp);
2997236155Smm	if (!cbp->cb_verbose)
2998236155Smm		return (0);
2999168404Spjd
3000236155Smm	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3001236155Smm	    &nvroot) == 0);
3002236155Smm	print_list_stats(zhp, NULL, nvroot, cbp, 0);
3003236155Smm
3004168404Spjd	return (0);
3005168404Spjd}
3006168404Spjd
3007168404Spjd/*
3008219089Spjd * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3009168404Spjd *
3010185029Spjd *	-H	Scripted mode.  Don't display headers, and separate properties
3011185029Spjd *		by a single tab.
3012185029Spjd *	-o	List of properties to display.  Defaults to
3013219089Spjd *		"name,size,allocated,free,capacity,health,altroot"
3014219089Spjd *	-T	Display a timestamp in date(1) or Unix format
3015168404Spjd *
3016168404Spjd * List all pools in the system, whether or not they're healthy.  Output space
3017168404Spjd * statistics for each one, as well as health status summary.
3018168404Spjd */
3019168404Spjdint
3020168404Spjdzpool_do_list(int argc, char **argv)
3021168404Spjd{
3022168404Spjd	int c;
3023168404Spjd	int ret;
3024168404Spjd	list_cbdata_t cb = { 0 };
3025185029Spjd	static char default_props[] =
3026236155Smm	    "name,size,allocated,free,capacity,dedupratio,"
3027236155Smm	    "health,altroot";
3028185029Spjd	char *props = default_props;
3029219089Spjd	unsigned long interval = 0, count = 0;
3030236155Smm	zpool_list_t *list;
3031236155Smm	boolean_t first = B_TRUE;
3032168404Spjd
3033168404Spjd	/* check options */
3034236155Smm	while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) {
3035168404Spjd		switch (c) {
3036168404Spjd		case 'H':
3037168404Spjd			cb.cb_scripted = B_TRUE;
3038168404Spjd			break;
3039168404Spjd		case 'o':
3040185029Spjd			props = optarg;
3041168404Spjd			break;
3042219089Spjd		case 'T':
3043219089Spjd			get_timestamp_arg(*optarg);
3044219089Spjd			break;
3045236155Smm		case 'v':
3046236155Smm			cb.cb_verbose = B_TRUE;
3047236155Smm			break;
3048168404Spjd		case ':':
3049168404Spjd			(void) fprintf(stderr, gettext("missing argument for "
3050168404Spjd			    "'%c' option\n"), optopt);
3051168404Spjd			usage(B_FALSE);
3052168404Spjd			break;
3053168404Spjd		case '?':
3054168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3055168404Spjd			    optopt);
3056168404Spjd			usage(B_FALSE);
3057168404Spjd		}
3058168404Spjd	}
3059168404Spjd
3060168404Spjd	argc -= optind;
3061168404Spjd	argv += optind;
3062168404Spjd
3063219089Spjd	get_interval_count(&argc, argv, &interval, &count);
3064219089Spjd
3065185029Spjd	if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3066185029Spjd		usage(B_FALSE);
3067168404Spjd
3068236155Smm	if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL)
3069236155Smm		return (1);
3070168404Spjd
3071236155Smm	if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3072236155Smm		(void) printf(gettext("no pools available\n"));
3073236155Smm		zprop_free_list(cb.cb_proplist);
3074236155Smm		return (0);
3075236155Smm	}
3076236155Smm
3077219089Spjd	for (;;) {
3078236155Smm		pool_list_update(list);
3079168404Spjd
3080236155Smm		if (pool_list_count(list) == 0)
3081236155Smm			break;
3082236155Smm
3083236155Smm		cb.cb_namewidth = 0;
3084236155Smm		(void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3085236155Smm
3086219089Spjd		if (timestamp_fmt != NODATE)
3087219089Spjd			print_timestamp(timestamp_fmt);
3088168404Spjd
3089236155Smm		if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3090236155Smm			print_header(&cb);
3091236155Smm			first = B_FALSE;
3092219089Spjd		}
3093236155Smm		ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
3094219089Spjd
3095219089Spjd		if (interval == 0)
3096219089Spjd			break;
3097219089Spjd
3098219089Spjd		if (count != 0 && --count == 0)
3099219089Spjd			break;
3100219089Spjd
3101219089Spjd		(void) sleep(interval);
3102168404Spjd	}
3103168404Spjd
3104219089Spjd	zprop_free_list(cb.cb_proplist);
3105168404Spjd	return (ret);
3106168404Spjd}
3107168404Spjd
3108168404Spjdstatic nvlist_t *
3109168404Spjdzpool_get_vdev_by_name(nvlist_t *nv, char *name)
3110168404Spjd{
3111168404Spjd	nvlist_t **child;
3112168404Spjd	uint_t c, children;
3113168404Spjd	nvlist_t *match;
3114168404Spjd	char *path;
3115168404Spjd
3116168404Spjd	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3117168404Spjd	    &child, &children) != 0) {
3118168404Spjd		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
3119219089Spjd		if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3120219089Spjd			name += sizeof(_PATH_DEV) - 1;
3121219089Spjd		if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3122219089Spjd			path += sizeof(_PATH_DEV) - 1;
3123168404Spjd		if (strcmp(name, path) == 0)
3124168404Spjd			return (nv);
3125168404Spjd		return (NULL);
3126168404Spjd	}
3127168404Spjd
3128168404Spjd	for (c = 0; c < children; c++)
3129168404Spjd		if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
3130168404Spjd			return (match);
3131168404Spjd
3132168404Spjd	return (NULL);
3133168404Spjd}
3134168404Spjd
3135168404Spjdstatic int
3136168404Spjdzpool_do_attach_or_replace(int argc, char **argv, int replacing)
3137168404Spjd{
3138168404Spjd	boolean_t force = B_FALSE;
3139168404Spjd	int c;
3140168404Spjd	nvlist_t *nvroot;
3141168404Spjd	char *poolname, *old_disk, *new_disk;
3142168404Spjd	zpool_handle_t *zhp;
3143168404Spjd	int ret;
3144168404Spjd
3145168404Spjd	/* check options */
3146168404Spjd	while ((c = getopt(argc, argv, "f")) != -1) {
3147168404Spjd		switch (c) {
3148168404Spjd		case 'f':
3149168404Spjd			force = B_TRUE;
3150168404Spjd			break;
3151168404Spjd		case '?':
3152168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3153168404Spjd			    optopt);
3154168404Spjd			usage(B_FALSE);
3155168404Spjd		}
3156168404Spjd	}
3157168404Spjd
3158168404Spjd	argc -= optind;
3159168404Spjd	argv += optind;
3160168404Spjd
3161168404Spjd	/* get pool name and check number of arguments */
3162168404Spjd	if (argc < 1) {
3163168404Spjd		(void) fprintf(stderr, gettext("missing pool name argument\n"));
3164168404Spjd		usage(B_FALSE);
3165168404Spjd	}
3166168404Spjd
3167168404Spjd	poolname = argv[0];
3168168404Spjd
3169168404Spjd	if (argc < 2) {
3170168404Spjd		(void) fprintf(stderr,
3171168404Spjd		    gettext("missing <device> specification\n"));
3172168404Spjd		usage(B_FALSE);
3173168404Spjd	}
3174168404Spjd
3175168404Spjd	old_disk = argv[1];
3176168404Spjd
3177168404Spjd	if (argc < 3) {
3178168404Spjd		if (!replacing) {
3179168404Spjd			(void) fprintf(stderr,
3180168404Spjd			    gettext("missing <new_device> specification\n"));
3181168404Spjd			usage(B_FALSE);
3182168404Spjd		}
3183168404Spjd		new_disk = old_disk;
3184168404Spjd		argc -= 1;
3185168404Spjd		argv += 1;
3186168404Spjd	} else {
3187168404Spjd		new_disk = argv[2];
3188168404Spjd		argc -= 2;
3189168404Spjd		argv += 2;
3190168404Spjd	}
3191168404Spjd
3192168404Spjd	if (argc > 1) {
3193168404Spjd		(void) fprintf(stderr, gettext("too many arguments\n"));
3194168404Spjd		usage(B_FALSE);
3195168404Spjd	}
3196168404Spjd
3197168404Spjd	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3198168404Spjd		return (1);
3199168404Spjd
3200185029Spjd	if (zpool_get_config(zhp, NULL) == NULL) {
3201168404Spjd		(void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3202168404Spjd		    poolname);
3203168404Spjd		zpool_close(zhp);
3204168404Spjd		return (1);
3205168404Spjd	}
3206168404Spjd
3207185029Spjd	nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
3208185029Spjd	    argc, argv);
3209168404Spjd	if (nvroot == NULL) {
3210168404Spjd		zpool_close(zhp);
3211168404Spjd		return (1);
3212168404Spjd	}
3213168404Spjd
3214168404Spjd	ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3215168404Spjd
3216168404Spjd	nvlist_free(nvroot);
3217168404Spjd	zpool_close(zhp);
3218168404Spjd
3219168404Spjd	return (ret);
3220168404Spjd}
3221168404Spjd
3222168404Spjd/*
3223168404Spjd * zpool replace [-f] <pool> <device> <new_device>
3224168404Spjd *
3225168404Spjd *	-f	Force attach, even if <new_device> appears to be in use.
3226168404Spjd *
3227168404Spjd * Replace <device> with <new_device>.
3228168404Spjd */
3229168404Spjd/* ARGSUSED */
3230168404Spjdint
3231168404Spjdzpool_do_replace(int argc, char **argv)
3232168404Spjd{
3233168404Spjd	return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3234168404Spjd}
3235168404Spjd
3236168404Spjd/*
3237168404Spjd * zpool attach [-f] <pool> <device> <new_device>
3238168404Spjd *
3239168404Spjd *	-f	Force attach, even if <new_device> appears to be in use.
3240168404Spjd *
3241168404Spjd * Attach <new_device> to the mirror containing <device>.  If <device> is not
3242168404Spjd * part of a mirror, then <device> will be transformed into a mirror of
3243168404Spjd * <device> and <new_device>.  In either case, <new_device> will begin life
3244168404Spjd * with a DTL of [0, now], and will immediately begin to resilver itself.
3245168404Spjd */
3246168404Spjdint
3247168404Spjdzpool_do_attach(int argc, char **argv)
3248168404Spjd{
3249168404Spjd	return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3250168404Spjd}
3251168404Spjd
3252168404Spjd/*
3253168404Spjd * zpool detach [-f] <pool> <device>
3254168404Spjd *
3255168404Spjd *	-f	Force detach of <device>, even if DTLs argue against it
3256168404Spjd *		(not supported yet)
3257168404Spjd *
3258168404Spjd * Detach a device from a mirror.  The operation will be refused if <device>
3259168404Spjd * is the last device in the mirror, or if the DTLs indicate that this device
3260168404Spjd * has the only valid copy of some data.
3261168404Spjd */
3262168404Spjd/* ARGSUSED */
3263168404Spjdint
3264168404Spjdzpool_do_detach(int argc, char **argv)
3265168404Spjd{
3266168404Spjd	int c;
3267168404Spjd	char *poolname, *path;
3268168404Spjd	zpool_handle_t *zhp;
3269168404Spjd	int ret;
3270168404Spjd
3271168404Spjd	/* check options */
3272168404Spjd	while ((c = getopt(argc, argv, "f")) != -1) {
3273168404Spjd		switch (c) {
3274168404Spjd		case 'f':
3275168404Spjd		case '?':
3276168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3277168404Spjd			    optopt);
3278168404Spjd			usage(B_FALSE);
3279168404Spjd		}
3280168404Spjd	}
3281168404Spjd
3282168404Spjd	argc -= optind;
3283168404Spjd	argv += optind;
3284168404Spjd
3285168404Spjd	/* get pool name and check number of arguments */
3286168404Spjd	if (argc < 1) {
3287168404Spjd		(void) fprintf(stderr, gettext("missing pool name argument\n"));
3288168404Spjd		usage(B_FALSE);
3289168404Spjd	}
3290168404Spjd
3291168404Spjd	if (argc < 2) {
3292168404Spjd		(void) fprintf(stderr,
3293168404Spjd		    gettext("missing <device> specification\n"));
3294168404Spjd		usage(B_FALSE);
3295168404Spjd	}
3296168404Spjd
3297168404Spjd	poolname = argv[0];
3298168404Spjd	path = argv[1];
3299168404Spjd
3300168404Spjd	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3301168404Spjd		return (1);
3302168404Spjd
3303168404Spjd	ret = zpool_vdev_detach(zhp, path);
3304168404Spjd
3305168404Spjd	zpool_close(zhp);
3306168404Spjd
3307168404Spjd	return (ret);
3308168404Spjd}
3309168404Spjd
3310168404Spjd/*
3311219089Spjd * zpool split [-n] [-o prop=val] ...
3312219089Spjd *		[-o mntopt] ...
3313219089Spjd *		[-R altroot] <pool> <newpool> [<device> ...]
3314219089Spjd *
3315219089Spjd *	-n	Do not split the pool, but display the resulting layout if
3316219089Spjd *		it were to be split.
3317219089Spjd *	-o	Set property=value, or set mount options.
3318219089Spjd *	-R	Mount the split-off pool under an alternate root.
3319219089Spjd *
3320219089Spjd * Splits the named pool and gives it the new pool name.  Devices to be split
3321219089Spjd * off may be listed, provided that no more than one device is specified
3322219089Spjd * per top-level vdev mirror.  The newly split pool is left in an exported
3323219089Spjd * state unless -R is specified.
3324219089Spjd *
3325219089Spjd * Restrictions: the top-level of the pool pool must only be made up of
3326219089Spjd * mirrors; all devices in the pool must be healthy; no device may be
3327219089Spjd * undergoing a resilvering operation.
3328219089Spjd */
3329219089Spjdint
3330219089Spjdzpool_do_split(int argc, char **argv)
3331219089Spjd{
3332219089Spjd	char *srcpool, *newpool, *propval;
3333219089Spjd	char *mntopts = NULL;
3334219089Spjd	splitflags_t flags;
3335219089Spjd	int c, ret = 0;
3336219089Spjd	zpool_handle_t *zhp;
3337219089Spjd	nvlist_t *config, *props = NULL;
3338219089Spjd
3339219089Spjd	flags.dryrun = B_FALSE;
3340219089Spjd	flags.import = B_FALSE;
3341219089Spjd
3342219089Spjd	/* check options */
3343219089Spjd	while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3344219089Spjd		switch (c) {
3345219089Spjd		case 'R':
3346219089Spjd			flags.import = B_TRUE;
3347219089Spjd			if (add_prop_list(
3348219089Spjd			    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3349219089Spjd			    &props, B_TRUE) != 0) {
3350219089Spjd				if (props)
3351219089Spjd					nvlist_free(props);
3352219089Spjd				usage(B_FALSE);
3353219089Spjd			}
3354219089Spjd			break;
3355219089Spjd		case 'n':
3356219089Spjd			flags.dryrun = B_TRUE;
3357219089Spjd			break;
3358219089Spjd		case 'o':
3359219089Spjd			if ((propval = strchr(optarg, '=')) != NULL) {
3360219089Spjd				*propval = '\0';
3361219089Spjd				propval++;
3362219089Spjd				if (add_prop_list(optarg, propval,
3363219089Spjd				    &props, B_TRUE) != 0) {
3364219089Spjd					if (props)
3365219089Spjd						nvlist_free(props);
3366219089Spjd					usage(B_FALSE);
3367219089Spjd				}
3368219089Spjd			} else {
3369219089Spjd				mntopts = optarg;
3370219089Spjd			}
3371219089Spjd			break;
3372219089Spjd		case ':':
3373219089Spjd			(void) fprintf(stderr, gettext("missing argument for "
3374219089Spjd			    "'%c' option\n"), optopt);
3375219089Spjd			usage(B_FALSE);
3376219089Spjd			break;
3377219089Spjd		case '?':
3378219089Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3379219089Spjd			    optopt);
3380219089Spjd			usage(B_FALSE);
3381219089Spjd			break;
3382219089Spjd		}
3383219089Spjd	}
3384219089Spjd
3385219089Spjd	if (!flags.import && mntopts != NULL) {
3386219089Spjd		(void) fprintf(stderr, gettext("setting mntopts is only "
3387219089Spjd		    "valid when importing the pool\n"));
3388219089Spjd		usage(B_FALSE);
3389219089Spjd	}
3390219089Spjd
3391219089Spjd	argc -= optind;
3392219089Spjd	argv += optind;
3393219089Spjd
3394219089Spjd	if (argc < 1) {
3395219089Spjd		(void) fprintf(stderr, gettext("Missing pool name\n"));
3396219089Spjd		usage(B_FALSE);
3397219089Spjd	}
3398219089Spjd	if (argc < 2) {
3399219089Spjd		(void) fprintf(stderr, gettext("Missing new pool name\n"));
3400219089Spjd		usage(B_FALSE);
3401219089Spjd	}
3402219089Spjd
3403219089Spjd	srcpool = argv[0];
3404219089Spjd	newpool = argv[1];
3405219089Spjd
3406219089Spjd	argc -= 2;
3407219089Spjd	argv += 2;
3408219089Spjd
3409219089Spjd	if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3410219089Spjd		return (1);
3411219089Spjd
3412219089Spjd	config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3413219089Spjd	if (config == NULL) {
3414219089Spjd		ret = 1;
3415219089Spjd	} else {
3416219089Spjd		if (flags.dryrun) {
3417219089Spjd			(void) printf(gettext("would create '%s' with the "
3418219089Spjd			    "following layout:\n\n"), newpool);
3419219089Spjd			print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3420219089Spjd		}
3421219089Spjd		nvlist_free(config);
3422219089Spjd	}
3423219089Spjd
3424219089Spjd	zpool_close(zhp);
3425219089Spjd
3426219089Spjd	if (ret != 0 || flags.dryrun || !flags.import)
3427219089Spjd		return (ret);
3428219089Spjd
3429219089Spjd	/*
3430219089Spjd	 * The split was successful. Now we need to open the new
3431219089Spjd	 * pool and import it.
3432219089Spjd	 */
3433219089Spjd	if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3434219089Spjd		return (1);
3435219089Spjd	if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3436219089Spjd	    zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3437219089Spjd		ret = 1;
3438240415Smm		(void) fprintf(stderr, gettext("Split was successful, but "
3439219089Spjd		    "the datasets could not all be mounted\n"));
3440219089Spjd		(void) fprintf(stderr, gettext("Try doing '%s' with a "
3441219089Spjd		    "different altroot\n"), "zpool import");
3442219089Spjd	}
3443219089Spjd	zpool_close(zhp);
3444219089Spjd
3445219089Spjd	return (ret);
3446219089Spjd}
3447219089Spjd
3448219089Spjd
3449219089Spjd
3450219089Spjd/*
3451168404Spjd * zpool online <pool> <device> ...
3452168404Spjd */
3453168404Spjdint
3454168404Spjdzpool_do_online(int argc, char **argv)
3455168404Spjd{
3456168404Spjd	int c, i;
3457168404Spjd	char *poolname;
3458168404Spjd	zpool_handle_t *zhp;
3459168404Spjd	int ret = 0;
3460185029Spjd	vdev_state_t newstate;
3461219089Spjd	int flags = 0;
3462168404Spjd
3463168404Spjd	/* check options */
3464219089Spjd	while ((c = getopt(argc, argv, "et")) != -1) {
3465168404Spjd		switch (c) {
3466219089Spjd		case 'e':
3467219089Spjd			flags |= ZFS_ONLINE_EXPAND;
3468219089Spjd			break;
3469168404Spjd		case 't':
3470168404Spjd		case '?':
3471168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3472168404Spjd			    optopt);
3473168404Spjd			usage(B_FALSE);
3474168404Spjd		}
3475168404Spjd	}
3476168404Spjd
3477168404Spjd	argc -= optind;
3478168404Spjd	argv += optind;
3479168404Spjd
3480168404Spjd	/* get pool name and check number of arguments */
3481168404Spjd	if (argc < 1) {
3482168404Spjd		(void) fprintf(stderr, gettext("missing pool name\n"));
3483168404Spjd		usage(B_FALSE);
3484168404Spjd	}
3485168404Spjd	if (argc < 2) {
3486168404Spjd		(void) fprintf(stderr, gettext("missing device name\n"));
3487168404Spjd		usage(B_FALSE);
3488168404Spjd	}
3489168404Spjd
3490168404Spjd	poolname = argv[0];
3491168404Spjd
3492168404Spjd	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3493168404Spjd		return (1);
3494168404Spjd
3495185029Spjd	for (i = 1; i < argc; i++) {
3496219089Spjd		if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3497185029Spjd			if (newstate != VDEV_STATE_HEALTHY) {
3498185029Spjd				(void) printf(gettext("warning: device '%s' "
3499185029Spjd				    "onlined, but remains in faulted state\n"),
3500185029Spjd				    argv[i]);
3501185029Spjd				if (newstate == VDEV_STATE_FAULTED)
3502185029Spjd					(void) printf(gettext("use 'zpool "
3503185029Spjd					    "clear' to restore a faulted "
3504185029Spjd					    "device\n"));
3505185029Spjd				else
3506185029Spjd					(void) printf(gettext("use 'zpool "
3507185029Spjd					    "replace' to replace devices "
3508185029Spjd					    "that are no longer present\n"));
3509185029Spjd			}
3510185029Spjd		} else {
3511168404Spjd			ret = 1;
3512185029Spjd		}
3513185029Spjd	}
3514168404Spjd
3515168404Spjd	zpool_close(zhp);
3516168404Spjd
3517168404Spjd	return (ret);
3518168404Spjd}
3519168404Spjd
3520168404Spjd/*
3521168404Spjd * zpool offline [-ft] <pool> <device> ...
3522168404Spjd *
3523168404Spjd *	-f	Force the device into the offline state, even if doing
3524168404Spjd *		so would appear to compromise pool availability.
3525168404Spjd *		(not supported yet)
3526168404Spjd *
3527168404Spjd *	-t	Only take the device off-line temporarily.  The offline
3528168404Spjd *		state will not be persistent across reboots.
3529168404Spjd */
3530168404Spjd/* ARGSUSED */
3531168404Spjdint
3532168404Spjdzpool_do_offline(int argc, char **argv)
3533168404Spjd{
3534168404Spjd	int c, i;
3535168404Spjd	char *poolname;
3536168404Spjd	zpool_handle_t *zhp;
3537168404Spjd	int ret = 0;
3538168404Spjd	boolean_t istmp = B_FALSE;
3539168404Spjd
3540168404Spjd	/* check options */
3541168404Spjd	while ((c = getopt(argc, argv, "ft")) != -1) {
3542168404Spjd		switch (c) {
3543168404Spjd		case 't':
3544168404Spjd			istmp = B_TRUE;
3545168404Spjd			break;
3546168404Spjd		case 'f':
3547168404Spjd		case '?':
3548168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3549168404Spjd			    optopt);
3550168404Spjd			usage(B_FALSE);
3551168404Spjd		}
3552168404Spjd	}
3553168404Spjd
3554168404Spjd	argc -= optind;
3555168404Spjd	argv += optind;
3556168404Spjd
3557168404Spjd	/* get pool name and check number of arguments */
3558168404Spjd	if (argc < 1) {
3559168404Spjd		(void) fprintf(stderr, gettext("missing pool name\n"));
3560168404Spjd		usage(B_FALSE);
3561168404Spjd	}
3562168404Spjd	if (argc < 2) {
3563168404Spjd		(void) fprintf(stderr, gettext("missing device name\n"));
3564168404Spjd		usage(B_FALSE);
3565168404Spjd	}
3566168404Spjd
3567168404Spjd	poolname = argv[0];
3568168404Spjd
3569168404Spjd	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3570168404Spjd		return (1);
3571168404Spjd
3572185029Spjd	for (i = 1; i < argc; i++) {
3573185029Spjd		if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3574168404Spjd			ret = 1;
3575185029Spjd	}
3576168404Spjd
3577168404Spjd	zpool_close(zhp);
3578168404Spjd
3579168404Spjd	return (ret);
3580168404Spjd}
3581168404Spjd
3582168404Spjd/*
3583168404Spjd * zpool clear <pool> [device]
3584168404Spjd *
3585168404Spjd * Clear all errors associated with a pool or a particular device.
3586168404Spjd */
3587168404Spjdint
3588168404Spjdzpool_do_clear(int argc, char **argv)
3589168404Spjd{
3590219089Spjd	int c;
3591168404Spjd	int ret = 0;
3592219089Spjd	boolean_t dryrun = B_FALSE;
3593219089Spjd	boolean_t do_rewind = B_FALSE;
3594219089Spjd	boolean_t xtreme_rewind = B_FALSE;
3595219089Spjd	uint32_t rewind_policy = ZPOOL_NO_REWIND;
3596219089Spjd	nvlist_t *policy = NULL;
3597168404Spjd	zpool_handle_t *zhp;
3598168404Spjd	char *pool, *device;
3599168404Spjd
3600219089Spjd	/* check options */
3601219089Spjd	while ((c = getopt(argc, argv, "FnX")) != -1) {
3602219089Spjd		switch (c) {
3603219089Spjd		case 'F':
3604219089Spjd			do_rewind = B_TRUE;
3605219089Spjd			break;
3606219089Spjd		case 'n':
3607219089Spjd			dryrun = B_TRUE;
3608219089Spjd			break;
3609219089Spjd		case 'X':
3610219089Spjd			xtreme_rewind = B_TRUE;
3611219089Spjd			break;
3612219089Spjd		case '?':
3613219089Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3614219089Spjd			    optopt);
3615219089Spjd			usage(B_FALSE);
3616219089Spjd		}
3617219089Spjd	}
3618219089Spjd
3619219089Spjd	argc -= optind;
3620219089Spjd	argv += optind;
3621219089Spjd
3622219089Spjd	if (argc < 1) {
3623168404Spjd		(void) fprintf(stderr, gettext("missing pool name\n"));
3624168404Spjd		usage(B_FALSE);
3625168404Spjd	}
3626168404Spjd
3627219089Spjd	if (argc > 2) {
3628168404Spjd		(void) fprintf(stderr, gettext("too many arguments\n"));
3629168404Spjd		usage(B_FALSE);
3630168404Spjd	}
3631168404Spjd
3632219089Spjd	if ((dryrun || xtreme_rewind) && !do_rewind) {
3633219089Spjd		(void) fprintf(stderr,
3634219089Spjd		    gettext("-n or -X only meaningful with -F\n"));
3635219089Spjd		usage(B_FALSE);
3636219089Spjd	}
3637219089Spjd	if (dryrun)
3638219089Spjd		rewind_policy = ZPOOL_TRY_REWIND;
3639219089Spjd	else if (do_rewind)
3640219089Spjd		rewind_policy = ZPOOL_DO_REWIND;
3641219089Spjd	if (xtreme_rewind)
3642219089Spjd		rewind_policy |= ZPOOL_EXTREME_REWIND;
3643168404Spjd
3644219089Spjd	/* In future, further rewind policy choices can be passed along here */
3645219089Spjd	if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3646219089Spjd	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3647168404Spjd		return (1);
3648168404Spjd
3649219089Spjd	pool = argv[0];
3650219089Spjd	device = argc == 2 ? argv[1] : NULL;
3651219089Spjd
3652219089Spjd	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3653219089Spjd		nvlist_free(policy);
3654219089Spjd		return (1);
3655219089Spjd	}
3656219089Spjd
3657219089Spjd	if (zpool_clear(zhp, device, policy) != 0)
3658168404Spjd		ret = 1;
3659168404Spjd
3660168404Spjd	zpool_close(zhp);
3661168404Spjd
3662219089Spjd	nvlist_free(policy);
3663219089Spjd
3664168404Spjd	return (ret);
3665168404Spjd}
3666168404Spjd
3667228103Smm/*
3668228103Smm * zpool reguid <pool>
3669228103Smm */
3670228103Smmint
3671228103Smmzpool_do_reguid(int argc, char **argv)
3672228103Smm{
3673228103Smm	int c;
3674228103Smm	char *poolname;
3675228103Smm	zpool_handle_t *zhp;
3676228103Smm	int ret = 0;
3677228103Smm
3678228103Smm	/* check options */
3679228103Smm	while ((c = getopt(argc, argv, "")) != -1) {
3680228103Smm		switch (c) {
3681228103Smm		case '?':
3682228103Smm			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3683228103Smm			    optopt);
3684228103Smm			usage(B_FALSE);
3685228103Smm		}
3686228103Smm	}
3687228103Smm
3688228103Smm	argc -= optind;
3689228103Smm	argv += optind;
3690228103Smm
3691228103Smm	/* get pool name and check number of arguments */
3692228103Smm	if (argc < 1) {
3693228103Smm		(void) fprintf(stderr, gettext("missing pool name\n"));
3694228103Smm		usage(B_FALSE);
3695228103Smm	}
3696228103Smm
3697228103Smm	if (argc > 1) {
3698228103Smm		(void) fprintf(stderr, gettext("too many arguments\n"));
3699228103Smm		usage(B_FALSE);
3700228103Smm	}
3701228103Smm
3702228103Smm	poolname = argv[0];
3703228103Smm	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3704228103Smm		return (1);
3705228103Smm
3706228103Smm	ret = zpool_reguid(zhp);
3707228103Smm
3708228103Smm	zpool_close(zhp);
3709228103Smm	return (ret);
3710228103Smm}
3711228103Smm
3712228103Smm
3713236155Smm/*
3714236155Smm * zpool reopen <pool>
3715236155Smm *
3716236155Smm * Reopen the pool so that the kernel can update the sizes of all vdevs.
3717236155Smm *
3718236155Smm * NOTE: This command is currently undocumented.  If the command is ever
3719236155Smm * exposed then the appropriate usage() messages will need to be made.
3720236155Smm */
3721236155Smmint
3722236155Smmzpool_do_reopen(int argc, char **argv)
3723236155Smm{
3724236155Smm	int ret = 0;
3725236155Smm	zpool_handle_t *zhp;
3726236155Smm	char *pool;
3727236155Smm
3728236155Smm	argc--;
3729236155Smm	argv++;
3730236155Smm
3731236155Smm	if (argc != 1)
3732236155Smm		return (2);
3733236155Smm
3734236155Smm	pool = argv[0];
3735236155Smm	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3736236155Smm		return (1);
3737236155Smm
3738236155Smm	ret = zpool_reopen(zhp);
3739236155Smm	zpool_close(zhp);
3740236155Smm	return (ret);
3741236155Smm}
3742236155Smm
3743168404Spjdtypedef struct scrub_cbdata {
3744168404Spjd	int	cb_type;
3745168404Spjd	int	cb_argc;
3746168404Spjd	char	**cb_argv;
3747168404Spjd} scrub_cbdata_t;
3748168404Spjd
3749168404Spjdint
3750168404Spjdscrub_callback(zpool_handle_t *zhp, void *data)
3751168404Spjd{
3752168404Spjd	scrub_cbdata_t *cb = data;
3753168404Spjd	int err;
3754168404Spjd
3755168404Spjd	/*
3756168404Spjd	 * Ignore faulted pools.
3757168404Spjd	 */
3758168404Spjd	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3759168404Spjd		(void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3760168404Spjd		    "currently unavailable\n"), zpool_get_name(zhp));
3761168404Spjd		return (1);
3762168404Spjd	}
3763168404Spjd
3764219089Spjd	err = zpool_scan(zhp, cb->cb_type);
3765168404Spjd
3766168404Spjd	return (err != 0);
3767168404Spjd}
3768168404Spjd
3769168404Spjd/*
3770168404Spjd * zpool scrub [-s] <pool> ...
3771168404Spjd *
3772168404Spjd *	-s	Stop.  Stops any in-progress scrub.
3773168404Spjd */
3774168404Spjdint
3775168404Spjdzpool_do_scrub(int argc, char **argv)
3776168404Spjd{
3777168404Spjd	int c;
3778168404Spjd	scrub_cbdata_t cb;
3779168404Spjd
3780219089Spjd	cb.cb_type = POOL_SCAN_SCRUB;
3781168404Spjd
3782168404Spjd	/* check options */
3783168404Spjd	while ((c = getopt(argc, argv, "s")) != -1) {
3784168404Spjd		switch (c) {
3785168404Spjd		case 's':
3786219089Spjd			cb.cb_type = POOL_SCAN_NONE;
3787168404Spjd			break;
3788168404Spjd		case '?':
3789168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3790168404Spjd			    optopt);
3791168404Spjd			usage(B_FALSE);
3792168404Spjd		}
3793168404Spjd	}
3794168404Spjd
3795168404Spjd	cb.cb_argc = argc;
3796168404Spjd	cb.cb_argv = argv;
3797168404Spjd	argc -= optind;
3798168404Spjd	argv += optind;
3799168404Spjd
3800168404Spjd	if (argc < 1) {
3801168404Spjd		(void) fprintf(stderr, gettext("missing pool name argument\n"));
3802168404Spjd		usage(B_FALSE);
3803168404Spjd	}
3804168404Spjd
3805168404Spjd	return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3806168404Spjd}
3807168404Spjd
3808168404Spjdtypedef struct status_cbdata {
3809168404Spjd	int		cb_count;
3810168404Spjd	boolean_t	cb_allpools;
3811168404Spjd	boolean_t	cb_verbose;
3812168404Spjd	boolean_t	cb_explain;
3813168404Spjd	boolean_t	cb_first;
3814219089Spjd	boolean_t	cb_dedup_stats;
3815168404Spjd} status_cbdata_t;
3816168404Spjd
3817168404Spjd/*
3818168404Spjd * Print out detailed scrub status.
3819168404Spjd */
3820168404Spjdvoid
3821219089Spjdprint_scan_status(pool_scan_stat_t *ps)
3822168404Spjd{
3823219089Spjd	time_t start, end;
3824219089Spjd	uint64_t elapsed, mins_left, hours_left;
3825219089Spjd	uint64_t pass_exam, examined, total;
3826219089Spjd	uint_t rate;
3827168404Spjd	double fraction_done;
3828219089Spjd	char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3829168404Spjd
3830226583Spjd	(void) printf(gettext("  scan: "));
3831168404Spjd
3832219089Spjd	/* If there's never been a scan, there's not much to say. */
3833219089Spjd	if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3834219089Spjd	    ps->pss_func >= POOL_SCAN_FUNCS) {
3835168404Spjd		(void) printf(gettext("none requested\n"));
3836168404Spjd		return;
3837168404Spjd	}
3838168404Spjd
3839219089Spjd	start = ps->pss_start_time;
3840219089Spjd	end = ps->pss_end_time;
3841219089Spjd	zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3842168404Spjd
3843219089Spjd	assert(ps->pss_func == POOL_SCAN_SCRUB ||
3844219089Spjd	    ps->pss_func == POOL_SCAN_RESILVER);
3845219089Spjd	/*
3846219089Spjd	 * Scan is finished or canceled.
3847219089Spjd	 */
3848219089Spjd	if (ps->pss_state == DSS_FINISHED) {
3849219089Spjd		uint64_t minutes_taken = (end - start) / 60;
3850219089Spjd		char *fmt;
3851168404Spjd
3852219089Spjd		if (ps->pss_func == POOL_SCAN_SCRUB) {
3853219089Spjd			fmt = gettext("scrub repaired %s in %lluh%um with "
3854219089Spjd			    "%llu errors on %s");
3855219089Spjd		} else if (ps->pss_func == POOL_SCAN_RESILVER) {
3856219089Spjd			fmt = gettext("resilvered %s in %lluh%um with "
3857219089Spjd			    "%llu errors on %s");
3858219089Spjd		}
3859219089Spjd		/* LINTED */
3860219089Spjd		(void) printf(fmt, processed_buf,
3861185029Spjd		    (u_longlong_t)(minutes_taken / 60),
3862185029Spjd		    (uint_t)(minutes_taken % 60),
3863219089Spjd		    (u_longlong_t)ps->pss_errors,
3864219089Spjd		    ctime((time_t *)&end));
3865168404Spjd		return;
3866219089Spjd	} else if (ps->pss_state == DSS_CANCELED) {
3867219089Spjd		if (ps->pss_func == POOL_SCAN_SCRUB) {
3868219089Spjd			(void) printf(gettext("scrub canceled on %s"),
3869219089Spjd			    ctime(&end));
3870219089Spjd		} else if (ps->pss_func == POOL_SCAN_RESILVER) {
3871219089Spjd			(void) printf(gettext("resilver canceled on %s"),
3872219089Spjd			    ctime(&end));
3873219089Spjd		}
3874219089Spjd		return;
3875168404Spjd	}
3876168404Spjd
3877219089Spjd	assert(ps->pss_state == DSS_SCANNING);
3878168404Spjd
3879219089Spjd	/*
3880219089Spjd	 * Scan is in progress.
3881219089Spjd	 */
3882219089Spjd	if (ps->pss_func == POOL_SCAN_SCRUB) {
3883219089Spjd		(void) printf(gettext("scrub in progress since %s"),
3884219089Spjd		    ctime(&start));
3885219089Spjd	} else if (ps->pss_func == POOL_SCAN_RESILVER) {
3886219089Spjd		(void) printf(gettext("resilver in progress since %s"),
3887219089Spjd		    ctime(&start));
3888219089Spjd	}
3889219089Spjd
3890219089Spjd	examined = ps->pss_examined ? ps->pss_examined : 1;
3891219089Spjd	total = ps->pss_to_examine;
3892168404Spjd	fraction_done = (double)examined / total;
3893168404Spjd
3894219089Spjd	/* elapsed time for this pass */
3895219089Spjd	elapsed = time(NULL) - ps->pss_pass_start;
3896219089Spjd	elapsed = elapsed ? elapsed : 1;
3897219089Spjd	pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3898219089Spjd	rate = pass_exam / elapsed;
3899219089Spjd	rate = rate ? rate : 1;
3900219089Spjd	mins_left = ((total - examined) / rate) / 60;
3901219089Spjd	hours_left = mins_left / 60;
3902219089Spjd
3903219089Spjd	zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3904219089Spjd	zfs_nicenum(total, total_buf, sizeof (total_buf));
3905219089Spjd	zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3906219089Spjd
3907219089Spjd	/*
3908219089Spjd	 * do not print estimated time if hours_left is more than 30 days
3909219089Spjd	 */
3910226583Spjd	(void) printf(gettext("        %s scanned out of %s at %s/s"),
3911219089Spjd	    examined_buf, total_buf, rate_buf);
3912219089Spjd	if (hours_left < (30 * 24)) {
3913219089Spjd		(void) printf(gettext(", %lluh%um to go\n"),
3914219089Spjd		    (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3915219089Spjd	} else {
3916219089Spjd		(void) printf(gettext(
3917219089Spjd		    ", (scan is slow, no estimated time)\n"));
3918219089Spjd	}
3919219089Spjd
3920219089Spjd	if (ps->pss_func == POOL_SCAN_RESILVER) {
3921226583Spjd		(void) printf(gettext("        %s resilvered, %.2f%% done\n"),
3922219089Spjd		    processed_buf, 100 * fraction_done);
3923219089Spjd	} else if (ps->pss_func == POOL_SCAN_SCRUB) {
3924226583Spjd		(void) printf(gettext("        %s repaired, %.2f%% done\n"),
3925219089Spjd		    processed_buf, 100 * fraction_done);
3926219089Spjd	}
3927168404Spjd}
3928168404Spjd
3929168404Spjdstatic void
3930168404Spjdprint_error_log(zpool_handle_t *zhp)
3931168404Spjd{
3932185029Spjd	nvlist_t *nverrlist = NULL;
3933168404Spjd	nvpair_t *elem;
3934168404Spjd	char *pathname;
3935168404Spjd	size_t len = MAXPATHLEN * 2;
3936168404Spjd
3937168404Spjd	if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3938168404Spjd		(void) printf("errors: List of errors unavailable "
3939168404Spjd		    "(insufficient privileges)\n");
3940168404Spjd		return;
3941168404Spjd	}
3942168404Spjd
3943168404Spjd	(void) printf("errors: Permanent errors have been "
3944168404Spjd	    "detected in the following files:\n\n");
3945168404Spjd
3946168404Spjd	pathname = safe_malloc(len);
3947168404Spjd	elem = NULL;
3948168404Spjd	while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
3949168404Spjd		nvlist_t *nv;
3950168404Spjd		uint64_t dsobj, obj;
3951168404Spjd
3952168404Spjd		verify(nvpair_value_nvlist(elem, &nv) == 0);
3953168404Spjd		verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
3954168404Spjd		    &dsobj) == 0);
3955168404Spjd		verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
3956168404Spjd		    &obj) == 0);
3957168404Spjd		zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
3958168404Spjd		(void) printf("%7s %s\n", "", pathname);
3959168404Spjd	}
3960168404Spjd	free(pathname);
3961168404Spjd	nvlist_free(nverrlist);
3962168404Spjd}
3963168404Spjd
3964168404Spjdstatic void
3965168404Spjdprint_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
3966168404Spjd    int namewidth)
3967168404Spjd{
3968168404Spjd	uint_t i;
3969168404Spjd	char *name;
3970168404Spjd
3971168404Spjd	if (nspares == 0)
3972168404Spjd		return;
3973168404Spjd
3974168404Spjd	(void) printf(gettext("\tspares\n"));
3975168404Spjd
3976168404Spjd	for (i = 0; i < nspares; i++) {
3977219089Spjd		name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
3978168404Spjd		print_status_config(zhp, name, spares[i],
3979209962Smm		    namewidth, 2, B_TRUE);
3980168404Spjd		free(name);
3981168404Spjd	}
3982168404Spjd}
3983168404Spjd
3984185029Spjdstatic void
3985185029Spjdprint_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3986185029Spjd    int namewidth)
3987185029Spjd{
3988185029Spjd	uint_t i;
3989185029Spjd	char *name;
3990185029Spjd
3991185029Spjd	if (nl2cache == 0)
3992185029Spjd		return;
3993185029Spjd
3994185029Spjd	(void) printf(gettext("\tcache\n"));
3995185029Spjd
3996185029Spjd	for (i = 0; i < nl2cache; i++) {
3997219089Spjd		name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3998185029Spjd		print_status_config(zhp, name, l2cache[i],
3999209962Smm		    namewidth, 2, B_FALSE);
4000185029Spjd		free(name);
4001185029Spjd	}
4002185029Spjd}
4003185029Spjd
4004219089Spjdstatic void
4005219089Spjdprint_dedup_stats(nvlist_t *config)
4006219089Spjd{
4007219089Spjd	ddt_histogram_t *ddh;
4008219089Spjd	ddt_stat_t *dds;
4009219089Spjd	ddt_object_t *ddo;
4010219089Spjd	uint_t c;
4011219089Spjd
4012219089Spjd	/*
4013219089Spjd	 * If the pool was faulted then we may not have been able to
4014253441Sdelphij	 * obtain the config. Otherwise, if we have anything in the dedup
4015219089Spjd	 * table continue processing the stats.
4016219089Spjd	 */
4017219089Spjd	if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
4018227497Smm	    (uint64_t **)&ddo, &c) != 0)
4019219089Spjd		return;
4020219089Spjd
4021219089Spjd	(void) printf("\n");
4022227497Smm	(void) printf(gettext(" dedup: "));
4023227497Smm	if (ddo->ddo_count == 0) {
4024227497Smm		(void) printf(gettext("no DDT entries\n"));
4025227497Smm		return;
4026227497Smm	}
4027227497Smm
4028219089Spjd	(void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4029219089Spjd	    (u_longlong_t)ddo->ddo_count,
4030219089Spjd	    (u_longlong_t)ddo->ddo_dspace,
4031219089Spjd	    (u_longlong_t)ddo->ddo_mspace);
4032219089Spjd
4033219089Spjd	verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
4034219089Spjd	    (uint64_t **)&dds, &c) == 0);
4035219089Spjd	verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
4036219089Spjd	    (uint64_t **)&ddh, &c) == 0);
4037219089Spjd	zpool_dump_ddt(dds, ddh);
4038219089Spjd}
4039219089Spjd
4040168404Spjd/*
4041168404Spjd * Display a summary of pool status.  Displays a summary such as:
4042168404Spjd *
4043168404Spjd *        pool: tank
4044168404Spjd *	status: DEGRADED
4045168404Spjd *	reason: One or more devices ...
4046236146Smm *         see: http://illumos.org/msg/ZFS-xxxx-01
4047168404Spjd *	config:
4048168404Spjd *		mirror		DEGRADED
4049168404Spjd *                c1t0d0	OK
4050168404Spjd *                c2t0d0	UNAVAIL
4051168404Spjd *
4052168404Spjd * When given the '-v' option, we print out the complete config.  If the '-e'
4053168404Spjd * option is specified, then we print out error rate information as well.
4054168404Spjd */
4055168404Spjdint
4056168404Spjdstatus_callback(zpool_handle_t *zhp, void *data)
4057168404Spjd{
4058168404Spjd	status_cbdata_t *cbp = data;
4059168404Spjd	nvlist_t *config, *nvroot;
4060168404Spjd	char *msgid;
4061168404Spjd	int reason;
4062168404Spjd	const char *health;
4063168404Spjd	uint_t c;
4064168404Spjd	vdev_stat_t *vs;
4065168404Spjd
4066168404Spjd	config = zpool_get_config(zhp, NULL);
4067168404Spjd	reason = zpool_get_status(zhp, &msgid);
4068168404Spjd
4069168404Spjd	cbp->cb_count++;
4070168404Spjd
4071168404Spjd	/*
4072168404Spjd	 * If we were given 'zpool status -x', only report those pools with
4073168404Spjd	 * problems.
4074168404Spjd	 */
4075248267Smm	if (cbp->cb_explain &&
4076248267Smm	    (reason == ZPOOL_STATUS_OK ||
4077248267Smm	    reason == ZPOOL_STATUS_VERSION_OLDER ||
4078248267Smm	    reason == ZPOOL_STATUS_FEAT_DISABLED)) {
4079168404Spjd		if (!cbp->cb_allpools) {
4080168404Spjd			(void) printf(gettext("pool '%s' is healthy\n"),
4081168404Spjd			    zpool_get_name(zhp));
4082168404Spjd			if (cbp->cb_first)
4083168404Spjd				cbp->cb_first = B_FALSE;
4084168404Spjd		}
4085168404Spjd		return (0);
4086168404Spjd	}
4087168404Spjd
4088168404Spjd	if (cbp->cb_first)
4089168404Spjd		cbp->cb_first = B_FALSE;
4090168404Spjd	else
4091168404Spjd		(void) printf("\n");
4092168404Spjd
4093168404Spjd	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4094168404Spjd	    &nvroot) == 0);
4095219089Spjd	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
4096168404Spjd	    (uint64_t **)&vs, &c) == 0);
4097185029Spjd	health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4098168404Spjd
4099168404Spjd	(void) printf(gettext("  pool: %s\n"), zpool_get_name(zhp));
4100168404Spjd	(void) printf(gettext(" state: %s\n"), health);
4101168404Spjd
4102168404Spjd	switch (reason) {
4103168404Spjd	case ZPOOL_STATUS_MISSING_DEV_R:
4104168404Spjd		(void) printf(gettext("status: One or more devices could not "
4105168404Spjd		    "be opened.  Sufficient replicas exist for\n\tthe pool to "
4106168404Spjd		    "continue functioning in a degraded state.\n"));
4107168404Spjd		(void) printf(gettext("action: Attach the missing device and "
4108168404Spjd		    "online it using 'zpool online'.\n"));
4109168404Spjd		break;
4110168404Spjd
4111168404Spjd	case ZPOOL_STATUS_MISSING_DEV_NR:
4112168404Spjd		(void) printf(gettext("status: One or more devices could not "
4113168404Spjd		    "be opened.  There are insufficient\n\treplicas for the "
4114168404Spjd		    "pool to continue functioning.\n"));
4115168404Spjd		(void) printf(gettext("action: Attach the missing device and "
4116168404Spjd		    "online it using 'zpool online'.\n"));
4117168404Spjd		break;
4118168404Spjd
4119168404Spjd	case ZPOOL_STATUS_CORRUPT_LABEL_R:
4120168404Spjd		(void) printf(gettext("status: One or more devices could not "
4121168404Spjd		    "be used because the label is missing or\n\tinvalid.  "
4122168404Spjd		    "Sufficient replicas exist for the pool to continue\n\t"
4123168404Spjd		    "functioning in a degraded state.\n"));
4124168404Spjd		(void) printf(gettext("action: Replace the device using "
4125168404Spjd		    "'zpool replace'.\n"));
4126168404Spjd		break;
4127168404Spjd
4128168404Spjd	case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4129168404Spjd		(void) printf(gettext("status: One or more devices could not "
4130168404Spjd		    "be used because the label is missing \n\tor invalid.  "
4131168404Spjd		    "There are insufficient replicas for the pool to "
4132168404Spjd		    "continue\n\tfunctioning.\n"));
4133219089Spjd		zpool_explain_recover(zpool_get_handle(zhp),
4134219089Spjd		    zpool_get_name(zhp), reason, config);
4135168404Spjd		break;
4136168404Spjd
4137168404Spjd	case ZPOOL_STATUS_FAILING_DEV:
4138168404Spjd		(void) printf(gettext("status: One or more devices has "
4139168404Spjd		    "experienced an unrecoverable error.  An\n\tattempt was "
4140168404Spjd		    "made to correct the error.  Applications are "
4141168404Spjd		    "unaffected.\n"));
4142168404Spjd		(void) printf(gettext("action: Determine if the device needs "
4143168404Spjd		    "to be replaced, and clear the errors\n\tusing "
4144168404Spjd		    "'zpool clear' or replace the device with 'zpool "
4145168404Spjd		    "replace'.\n"));
4146168404Spjd		break;
4147168404Spjd
4148168404Spjd	case ZPOOL_STATUS_OFFLINE_DEV:
4149168404Spjd		(void) printf(gettext("status: One or more devices has "
4150168404Spjd		    "been taken offline by the administrator.\n\tSufficient "
4151168404Spjd		    "replicas exist for the pool to continue functioning in "
4152168404Spjd		    "a\n\tdegraded state.\n"));
4153168404Spjd		(void) printf(gettext("action: Online the device using "
4154168404Spjd		    "'zpool online' or replace the device with\n\t'zpool "
4155168404Spjd		    "replace'.\n"));
4156168404Spjd		break;
4157168404Spjd
4158219089Spjd	case ZPOOL_STATUS_REMOVED_DEV:
4159219089Spjd		(void) printf(gettext("status: One or more devices has "
4160219089Spjd		    "been removed by the administrator.\n\tSufficient "
4161219089Spjd		    "replicas exist for the pool to continue functioning in "
4162219089Spjd		    "a\n\tdegraded state.\n"));
4163219089Spjd		(void) printf(gettext("action: Online the device using "
4164219089Spjd		    "'zpool online' or replace the device with\n\t'zpool "
4165219089Spjd		    "replace'.\n"));
4166219089Spjd		break;
4167219089Spjd
4168168404Spjd	case ZPOOL_STATUS_RESILVERING:
4169168404Spjd		(void) printf(gettext("status: One or more devices is "
4170168404Spjd		    "currently being resilvered.  The pool will\n\tcontinue "
4171168404Spjd		    "to function, possibly in a degraded state.\n"));
4172168404Spjd		(void) printf(gettext("action: Wait for the resilver to "
4173168404Spjd		    "complete.\n"));
4174168404Spjd		break;
4175168404Spjd
4176168404Spjd	case ZPOOL_STATUS_CORRUPT_DATA:
4177168404Spjd		(void) printf(gettext("status: One or more devices has "
4178168404Spjd		    "experienced an error resulting in data\n\tcorruption.  "
4179168404Spjd		    "Applications may be affected.\n"));
4180168404Spjd		(void) printf(gettext("action: Restore the file in question "
4181168404Spjd		    "if possible.  Otherwise restore the\n\tentire pool from "
4182168404Spjd		    "backup.\n"));
4183168404Spjd		break;
4184168404Spjd
4185168404Spjd	case ZPOOL_STATUS_CORRUPT_POOL:
4186168404Spjd		(void) printf(gettext("status: The pool metadata is corrupted "
4187168404Spjd		    "and the pool cannot be opened.\n"));
4188219089Spjd		zpool_explain_recover(zpool_get_handle(zhp),
4189219089Spjd		    zpool_get_name(zhp), reason, config);
4190168404Spjd		break;
4191168404Spjd
4192168404Spjd	case ZPOOL_STATUS_VERSION_OLDER:
4193238926Smm		(void) printf(gettext("status: The pool is formatted using a "
4194238926Smm		    "legacy on-disk format.  The pool can\n\tstill be used, "
4195238926Smm		    "but some features are unavailable.\n"));
4196168404Spjd		(void) printf(gettext("action: Upgrade the pool using 'zpool "
4197168404Spjd		    "upgrade'.  Once this is done, the\n\tpool will no longer "
4198238926Smm		    "be accessible on software that does not support feature\n"
4199238926Smm		    "\tflags.\n"));
4200168404Spjd		break;
4201168404Spjd
4202168404Spjd	case ZPOOL_STATUS_VERSION_NEWER:
4203168404Spjd		(void) printf(gettext("status: The pool has been upgraded to a "
4204168404Spjd		    "newer, incompatible on-disk version.\n\tThe pool cannot "
4205168404Spjd		    "be accessed on this system.\n"));
4206168404Spjd		(void) printf(gettext("action: Access the pool from a system "
4207168404Spjd		    "running more recent software, or\n\trestore the pool from "
4208168404Spjd		    "backup.\n"));
4209168404Spjd		break;
4210168404Spjd
4211238926Smm	case ZPOOL_STATUS_FEAT_DISABLED:
4212238926Smm		(void) printf(gettext("status: Some supported features are not "
4213238926Smm		    "enabled on the pool. The pool can\n\tstill be used, but "
4214238926Smm		    "some features are unavailable.\n"));
4215238926Smm		(void) printf(gettext("action: Enable all features using "
4216238926Smm		    "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4217238926Smm		    "longer be accessible by software that does not support\n\t"
4218243014Smm		    "the features. See zpool-features(7) for details.\n"));
4219238926Smm		break;
4220238926Smm
4221236884Smm	case ZPOOL_STATUS_UNSUP_FEAT_READ:
4222236884Smm		(void) printf(gettext("status: The pool cannot be accessed on "
4223236884Smm		    "this system because it uses the\n\tfollowing feature(s) "
4224236884Smm		    "not supported on this system:\n"));
4225236884Smm		zpool_print_unsup_feat(config);
4226236884Smm		(void) printf("\n");
4227236884Smm		(void) printf(gettext("action: Access the pool from a system "
4228236884Smm		    "that supports the required feature(s),\n\tor restore the "
4229236884Smm		    "pool from backup.\n"));
4230236884Smm		break;
4231236884Smm
4232236884Smm	case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4233236884Smm		(void) printf(gettext("status: The pool can only be accessed "
4234236884Smm		    "in read-only mode on this system. It\n\tcannot be "
4235236884Smm		    "accessed in read-write mode because it uses the "
4236236884Smm		    "following\n\tfeature(s) not supported on this system:\n"));
4237236884Smm		zpool_print_unsup_feat(config);
4238236884Smm		(void) printf("\n");
4239236884Smm		(void) printf(gettext("action: The pool cannot be accessed in "
4240236884Smm		    "read-write mode. Import the pool with\n"
4241236884Smm		    "\t\"-o readonly=on\", access the pool from a system that "
4242236884Smm		    "supports the\n\trequired feature(s), or restore the "
4243236884Smm		    "pool from backup.\n"));
4244236884Smm		break;
4245236884Smm
4246185029Spjd	case ZPOOL_STATUS_FAULTED_DEV_R:
4247185029Spjd		(void) printf(gettext("status: One or more devices are "
4248185029Spjd		    "faulted in response to persistent errors.\n\tSufficient "
4249185029Spjd		    "replicas exist for the pool to continue functioning "
4250185029Spjd		    "in a\n\tdegraded state.\n"));
4251185029Spjd		(void) printf(gettext("action: Replace the faulted device, "
4252185029Spjd		    "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4253185029Spjd		break;
4254185029Spjd
4255185029Spjd	case ZPOOL_STATUS_FAULTED_DEV_NR:
4256185029Spjd		(void) printf(gettext("status: One or more devices are "
4257185029Spjd		    "faulted in response to persistent errors.  There are "
4258185029Spjd		    "insufficient replicas for the pool to\n\tcontinue "
4259185029Spjd		    "functioning.\n"));
4260185029Spjd		(void) printf(gettext("action: Destroy and re-create the pool "
4261185029Spjd		    "from a backup source.  Manually marking the device\n"
4262185029Spjd		    "\trepaired using 'zpool clear' may allow some data "
4263185029Spjd		    "to be recovered.\n"));
4264185029Spjd		break;
4265185029Spjd
4266185029Spjd	case ZPOOL_STATUS_IO_FAILURE_WAIT:
4267185029Spjd	case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4268185029Spjd		(void) printf(gettext("status: One or more devices are "
4269185029Spjd		    "faulted in response to IO failures.\n"));
4270185029Spjd		(void) printf(gettext("action: Make sure the affected devices "
4271185029Spjd		    "are connected, then run 'zpool clear'.\n"));
4272185029Spjd		break;
4273185029Spjd
4274185029Spjd	case ZPOOL_STATUS_BAD_LOG:
4275185029Spjd		(void) printf(gettext("status: An intent log record "
4276185029Spjd		    "could not be read.\n"
4277185029Spjd		    "\tWaiting for adminstrator intervention to fix the "
4278185029Spjd		    "faulted pool.\n"));
4279185029Spjd		(void) printf(gettext("action: Either restore the affected "
4280185029Spjd		    "device(s) and run 'zpool online',\n"
4281185029Spjd		    "\tor ignore the intent log records by running "
4282185029Spjd		    "'zpool clear'.\n"));
4283185029Spjd		break;
4284185029Spjd
4285254591Sgibbs	case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
4286254591Sgibbs		(void) printf(gettext("status: One or more devices are "
4287254591Sgibbs		    "configured to use a non-native block size.\n"
4288254591Sgibbs		    "\tExpect reduced performance.\n"));
4289254591Sgibbs		(void) printf(gettext("action: Replace affected devices with "
4290254591Sgibbs		    "devices that support the\n\tconfigured block size, or "
4291254591Sgibbs		    "migrate data to a properly configured\n\tpool.\n"));
4292254591Sgibbs		break;
4293254591Sgibbs
4294168404Spjd	default:
4295168404Spjd		/*
4296168404Spjd		 * The remaining errors can't actually be generated, yet.
4297168404Spjd		 */
4298168404Spjd		assert(reason == ZPOOL_STATUS_OK);
4299168404Spjd	}
4300168404Spjd
4301168404Spjd	if (msgid != NULL)
4302236146Smm		(void) printf(gettext("   see: http://illumos.org/msg/%s\n"),
4303168404Spjd		    msgid);
4304168404Spjd
4305168404Spjd	if (config != NULL) {
4306168404Spjd		int namewidth;
4307168404Spjd		uint64_t nerr;
4308185029Spjd		nvlist_t **spares, **l2cache;
4309185029Spjd		uint_t nspares, nl2cache;
4310219089Spjd		pool_scan_stat_t *ps = NULL;
4311168404Spjd
4312219089Spjd		(void) nvlist_lookup_uint64_array(nvroot,
4313219089Spjd		    ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4314219089Spjd		print_scan_status(ps);
4315168404Spjd
4316168404Spjd		namewidth = max_width(zhp, nvroot, 0, 0);
4317168404Spjd		if (namewidth < 10)
4318168404Spjd			namewidth = 10;
4319168404Spjd
4320168404Spjd		(void) printf(gettext("config:\n\n"));
4321168404Spjd		(void) printf(gettext("\t%-*s  %-8s %5s %5s %5s\n"), namewidth,
4322168404Spjd		    "NAME", "STATE", "READ", "WRITE", "CKSUM");
4323168404Spjd		print_status_config(zhp, zpool_get_name(zhp), nvroot,
4324209962Smm		    namewidth, 0, B_FALSE);
4325209962Smm
4326185029Spjd		if (num_logs(nvroot) > 0)
4327213197Smm			print_logs(zhp, nvroot, namewidth, B_TRUE);
4328185029Spjd		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4329185029Spjd		    &l2cache, &nl2cache) == 0)
4330185029Spjd			print_l2cache(zhp, l2cache, nl2cache, namewidth);
4331185029Spjd
4332168404Spjd		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4333168404Spjd		    &spares, &nspares) == 0)
4334168404Spjd			print_spares(zhp, spares, nspares, namewidth);
4335168404Spjd
4336168404Spjd		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4337168404Spjd		    &nerr) == 0) {
4338168404Spjd			nvlist_t *nverrlist = NULL;
4339168404Spjd
4340168404Spjd			/*
4341168404Spjd			 * If the approximate error count is small, get a
4342168404Spjd			 * precise count by fetching the entire log and
4343168404Spjd			 * uniquifying the results.
4344168404Spjd			 */
4345185029Spjd			if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4346168404Spjd			    zpool_get_errlog(zhp, &nverrlist) == 0) {
4347168404Spjd				nvpair_t *elem;
4348168404Spjd
4349168404Spjd				elem = NULL;
4350168404Spjd				nerr = 0;
4351168404Spjd				while ((elem = nvlist_next_nvpair(nverrlist,
4352168404Spjd				    elem)) != NULL) {
4353168404Spjd					nerr++;
4354168404Spjd				}
4355168404Spjd			}
4356168404Spjd			nvlist_free(nverrlist);
4357168404Spjd
4358168404Spjd			(void) printf("\n");
4359168404Spjd
4360168404Spjd			if (nerr == 0)
4361168404Spjd				(void) printf(gettext("errors: No known data "
4362168404Spjd				    "errors\n"));
4363168404Spjd			else if (!cbp->cb_verbose)
4364168404Spjd				(void) printf(gettext("errors: %llu data "
4365168404Spjd				    "errors, use '-v' for a list\n"),
4366168404Spjd				    (u_longlong_t)nerr);
4367168404Spjd			else
4368168404Spjd				print_error_log(zhp);
4369168404Spjd		}
4370219089Spjd
4371219089Spjd		if (cbp->cb_dedup_stats)
4372219089Spjd			print_dedup_stats(config);
4373168404Spjd	} else {
4374168404Spjd		(void) printf(gettext("config: The configuration cannot be "
4375168404Spjd		    "determined.\n"));
4376168404Spjd	}
4377168404Spjd
4378168404Spjd	return (0);
4379168404Spjd}
4380168404Spjd
4381168404Spjd/*
4382219089Spjd * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4383168404Spjd *
4384168404Spjd *	-v	Display complete error logs
4385168404Spjd *	-x	Display only pools with potential problems
4386219089Spjd *	-D	Display dedup status (undocumented)
4387219089Spjd *	-T	Display a timestamp in date(1) or Unix format
4388168404Spjd *
4389168404Spjd * Describes the health status of all pools or some subset.
4390168404Spjd */
4391168404Spjdint
4392168404Spjdzpool_do_status(int argc, char **argv)
4393168404Spjd{
4394168404Spjd	int c;
4395168404Spjd	int ret;
4396219089Spjd	unsigned long interval = 0, count = 0;
4397168404Spjd	status_cbdata_t cb = { 0 };
4398168404Spjd
4399168404Spjd	/* check options */
4400219089Spjd	while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4401168404Spjd		switch (c) {
4402168404Spjd		case 'v':
4403168404Spjd			cb.cb_verbose = B_TRUE;
4404168404Spjd			break;
4405168404Spjd		case 'x':
4406168404Spjd			cb.cb_explain = B_TRUE;
4407168404Spjd			break;
4408219089Spjd		case 'D':
4409219089Spjd			cb.cb_dedup_stats = B_TRUE;
4410219089Spjd			break;
4411219089Spjd		case 'T':
4412219089Spjd			get_timestamp_arg(*optarg);
4413219089Spjd			break;
4414168404Spjd		case '?':
4415168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4416168404Spjd			    optopt);
4417168404Spjd			usage(B_FALSE);
4418168404Spjd		}
4419168404Spjd	}
4420168404Spjd
4421168404Spjd	argc -= optind;
4422168404Spjd	argv += optind;
4423168404Spjd
4424219089Spjd	get_interval_count(&argc, argv, &interval, &count);
4425168404Spjd
4426168404Spjd	if (argc == 0)
4427168404Spjd		cb.cb_allpools = B_TRUE;
4428168404Spjd
4429219089Spjd	cb.cb_first = B_TRUE;
4430168404Spjd
4431219089Spjd	for (;;) {
4432219089Spjd		if (timestamp_fmt != NODATE)
4433219089Spjd			print_timestamp(timestamp_fmt);
4434168404Spjd
4435219089Spjd		ret = for_each_pool(argc, argv, B_TRUE, NULL,
4436219089Spjd		    status_callback, &cb);
4437219089Spjd
4438219089Spjd		if (argc == 0 && cb.cb_count == 0)
4439219089Spjd			(void) printf(gettext("no pools available\n"));
4440219089Spjd		else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4441219089Spjd			(void) printf(gettext("all pools are healthy\n"));
4442219089Spjd
4443219089Spjd		if (ret != 0)
4444219089Spjd			return (ret);
4445219089Spjd
4446219089Spjd		if (interval == 0)
4447219089Spjd			break;
4448219089Spjd
4449219089Spjd		if (count != 0 && --count == 0)
4450219089Spjd			break;
4451219089Spjd
4452219089Spjd		(void) sleep(interval);
4453219089Spjd	}
4454219089Spjd
4455219089Spjd	return (0);
4456168404Spjd}
4457168404Spjd
4458168404Spjdtypedef struct upgrade_cbdata {
4459168404Spjd	int	cb_first;
4460212050Spjd	char	cb_poolname[ZPOOL_MAXNAMELEN];
4461168404Spjd	int	cb_argc;
4462185029Spjd	uint64_t cb_version;
4463168404Spjd	char	**cb_argv;
4464168404Spjd} upgrade_cbdata_t;
4465168404Spjd
4466238950Smm#ifdef __FreeBSD__
4467168404Spjdstatic int
4468212050Spjdis_root_pool(zpool_handle_t *zhp)
4469212050Spjd{
4470212050Spjd	static struct statfs sfs;
4471212050Spjd	static char *poolname = NULL;
4472212050Spjd	static boolean_t stated = B_FALSE;
4473212050Spjd	char *slash;
4474212050Spjd
4475212067Spjd	if (!stated) {
4476212050Spjd		stated = B_TRUE;
4477212050Spjd		if (statfs("/", &sfs) == -1) {
4478212050Spjd			(void) fprintf(stderr,
4479212050Spjd			    "Unable to stat root file system: %s.\n",
4480212050Spjd			    strerror(errno));
4481212067Spjd			return (0);
4482212050Spjd		}
4483212050Spjd		if (strcmp(sfs.f_fstypename, "zfs") != 0)
4484212067Spjd			return (0);
4485212050Spjd		poolname = sfs.f_mntfromname;
4486212050Spjd		if ((slash = strchr(poolname, '/')) != NULL)
4487212050Spjd			*slash = '\0';
4488212050Spjd	}
4489212050Spjd	return (poolname != NULL && strcmp(poolname, zpool_get_name(zhp)) == 0);
4490212050Spjd}
4491212050Spjd
4492238950Smmstatic void
4493238950Smmroot_pool_upgrade_check(zpool_handle_t *zhp, char *poolname, int size) {
4494238950Smm
4495238950Smm	if (poolname[0] == '\0' && is_root_pool(zhp))
4496238950Smm		(void) strlcpy(poolname, zpool_get_name(zhp), size);
4497238950Smm}
4498238950Smm#endif	/* FreeBSD */
4499238950Smm
4500212050Spjdstatic int
4501238926Smmupgrade_version(zpool_handle_t *zhp, uint64_t version)
4502238926Smm{
4503238926Smm	int ret;
4504238926Smm	nvlist_t *config;
4505238926Smm	uint64_t oldversion;
4506238926Smm
4507238926Smm	config = zpool_get_config(zhp, NULL);
4508238926Smm	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4509238926Smm	    &oldversion) == 0);
4510238926Smm
4511238926Smm	assert(SPA_VERSION_IS_SUPPORTED(oldversion));
4512238926Smm	assert(oldversion < version);
4513238926Smm
4514238926Smm	ret = zpool_upgrade(zhp, version);
4515238926Smm	if (ret != 0)
4516238926Smm		return (ret);
4517238926Smm
4518238926Smm	if (version >= SPA_VERSION_FEATURES) {
4519238926Smm		(void) printf(gettext("Successfully upgraded "
4520238926Smm		    "'%s' from version %llu to feature flags.\n"),
4521238926Smm		    zpool_get_name(zhp), oldversion);
4522238926Smm	} else {
4523238926Smm		(void) printf(gettext("Successfully upgraded "
4524238926Smm		    "'%s' from version %llu to version %llu.\n"),
4525238926Smm		    zpool_get_name(zhp), oldversion, version);
4526238926Smm	}
4527238926Smm
4528238926Smm	return (0);
4529238926Smm}
4530238926Smm
4531238926Smmstatic int
4532238926Smmupgrade_enable_all(zpool_handle_t *zhp, int *countp)
4533238926Smm{
4534238926Smm	int i, ret, count;
4535238926Smm	boolean_t firstff = B_TRUE;
4536238926Smm	nvlist_t *enabled = zpool_get_features(zhp);
4537238926Smm
4538238926Smm	count = 0;
4539238926Smm	for (i = 0; i < SPA_FEATURES; i++) {
4540238926Smm		const char *fname = spa_feature_table[i].fi_uname;
4541238926Smm		const char *fguid = spa_feature_table[i].fi_guid;
4542238926Smm		if (!nvlist_exists(enabled, fguid)) {
4543238926Smm			char *propname;
4544238926Smm			verify(-1 != asprintf(&propname, "feature@%s", fname));
4545238926Smm			ret = zpool_set_prop(zhp, propname,
4546238926Smm			    ZFS_FEATURE_ENABLED);
4547238926Smm			if (ret != 0) {
4548238926Smm				free(propname);
4549238926Smm				return (ret);
4550238926Smm			}
4551238926Smm			count++;
4552238926Smm
4553238926Smm			if (firstff) {
4554238926Smm				(void) printf(gettext("Enabled the "
4555238926Smm				    "following features on '%s':\n"),
4556238926Smm				    zpool_get_name(zhp));
4557238926Smm				firstff = B_FALSE;
4558238926Smm			}
4559238926Smm			(void) printf(gettext("  %s\n"), fname);
4560238926Smm			free(propname);
4561238926Smm		}
4562238926Smm	}
4563238926Smm
4564238926Smm	if (countp != NULL)
4565238926Smm		*countp = count;
4566238926Smm	return (0);
4567238926Smm}
4568238926Smm
4569238926Smmstatic int
4570168404Spjdupgrade_cb(zpool_handle_t *zhp, void *arg)
4571168404Spjd{
4572168404Spjd	upgrade_cbdata_t *cbp = arg;
4573168404Spjd	nvlist_t *config;
4574168404Spjd	uint64_t version;
4575238926Smm	boolean_t printnl = B_FALSE;
4576238926Smm	int ret;
4577168404Spjd
4578168404Spjd	config = zpool_get_config(zhp, NULL);
4579168404Spjd	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4580168404Spjd	    &version) == 0);
4581168404Spjd
4582238926Smm	assert(SPA_VERSION_IS_SUPPORTED(version));
4583168404Spjd
4584238926Smm	if (version < cbp->cb_version) {
4585238926Smm		cbp->cb_first = B_FALSE;
4586238926Smm		ret = upgrade_version(zhp, cbp->cb_version);
4587238926Smm		if (ret != 0)
4588238926Smm			return (ret);
4589238926Smm#ifdef __FreeBSD__
4590238950Smm		root_pool_upgrade_check(zhp, cbp->cb_poolname,
4591238950Smm		    sizeof(cbp->cb_poolname));
4592238926Smm#endif	/* ___FreeBSD__ */
4593238926Smm		printnl = B_TRUE;
4594238926Smm
4595238926Smm#ifdef illumos
4596238926Smm		/*
4597238926Smm		 * If they did "zpool upgrade -a", then we could
4598238926Smm		 * be doing ioctls to different pools.  We need
4599238926Smm		 * to log this history once to each pool, and bypass
4600238926Smm		 * the normal history logging that happens in main().
4601238926Smm		 */
4602238926Smm		(void) zpool_log_history(g_zfs, history_str);
4603238926Smm		log_history = B_FALSE;
4604238926Smm#endif
4605238926Smm	}
4606238926Smm
4607238926Smm	if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4608238926Smm		int count;
4609238926Smm		ret = upgrade_enable_all(zhp, &count);
4610238926Smm		if (ret != 0)
4611238926Smm			return (ret);
4612238926Smm
4613238926Smm		if (count > 0) {
4614168404Spjd			cbp->cb_first = B_FALSE;
4615238926Smm			printnl = B_TRUE;
4616248571Smm			/*
4617248571Smm			 * If they did "zpool upgrade -a", then we could
4618248571Smm			 * be doing ioctls to different pools.  We need
4619248571Smm			 * to log this history once to each pool, and bypass
4620248571Smm			 * the normal history logging that happens in main().
4621248571Smm			 */
4622248571Smm			(void) zpool_log_history(g_zfs, history_str);
4623248571Smm			log_history = B_FALSE;
4624168404Spjd		}
4625238926Smm	}
4626168404Spjd
4627238926Smm	if (printnl) {
4628238926Smm		(void) printf(gettext("\n"));
4629238926Smm	}
4630238926Smm
4631238926Smm	return (0);
4632238926Smm}
4633238926Smm
4634238926Smmstatic int
4635238926Smmupgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
4636238926Smm{
4637238926Smm	upgrade_cbdata_t *cbp = arg;
4638238926Smm	nvlist_t *config;
4639238926Smm	uint64_t version;
4640238926Smm
4641238926Smm	config = zpool_get_config(zhp, NULL);
4642238926Smm	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4643238926Smm	    &version) == 0);
4644238926Smm
4645238926Smm	assert(SPA_VERSION_IS_SUPPORTED(version));
4646238926Smm
4647238926Smm	if (version < SPA_VERSION_FEATURES) {
4648168404Spjd		if (cbp->cb_first) {
4649168404Spjd			(void) printf(gettext("The following pools are "
4650238926Smm			    "formatted with legacy version numbers and can\n"
4651238926Smm			    "be upgraded to use feature flags.  After "
4652238926Smm			    "being upgraded, these pools\nwill no "
4653238926Smm			    "longer be accessible by software that does not "
4654238926Smm			    "support feature\nflags.\n\n"));
4655168404Spjd			(void) printf(gettext("VER  POOL\n"));
4656168404Spjd			(void) printf(gettext("---  ------------\n"));
4657168404Spjd			cbp->cb_first = B_FALSE;
4658168404Spjd		}
4659168404Spjd
4660168404Spjd		(void) printf("%2llu   %s\n", (u_longlong_t)version,
4661168404Spjd		    zpool_get_name(zhp));
4662168404Spjd	}
4663168404Spjd
4664238926Smm	return (0);
4665168404Spjd}
4666168404Spjd
4667238926Smmstatic int
4668238926Smmupgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
4669238926Smm{
4670238926Smm	upgrade_cbdata_t *cbp = arg;
4671238926Smm	nvlist_t *config;
4672238926Smm	uint64_t version;
4673238926Smm
4674238926Smm	config = zpool_get_config(zhp, NULL);
4675238926Smm	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4676238926Smm	    &version) == 0);
4677238926Smm
4678238926Smm	if (version >= SPA_VERSION_FEATURES) {
4679238926Smm		int i;
4680238926Smm		boolean_t poolfirst = B_TRUE;
4681238926Smm		nvlist_t *enabled = zpool_get_features(zhp);
4682238926Smm
4683238926Smm		for (i = 0; i < SPA_FEATURES; i++) {
4684238926Smm			const char *fguid = spa_feature_table[i].fi_guid;
4685238926Smm			const char *fname = spa_feature_table[i].fi_uname;
4686238926Smm			if (!nvlist_exists(enabled, fguid)) {
4687238926Smm				if (cbp->cb_first) {
4688238926Smm					(void) printf(gettext("\nSome "
4689238926Smm					    "supported features are not "
4690238926Smm					    "enabled on the following pools. "
4691238926Smm					    "Once a\nfeature is enabled the "
4692238926Smm					    "pool may become incompatible with "
4693238926Smm					    "software\nthat does not support "
4694238926Smm					    "the feature. See "
4695243014Smm					    "zpool-features(7) for "
4696238926Smm					    "details.\n\n"));
4697238926Smm					(void) printf(gettext("POOL  "
4698238926Smm					    "FEATURE\n"));
4699238926Smm					(void) printf(gettext("------"
4700238926Smm					    "---------\n"));
4701238926Smm					cbp->cb_first = B_FALSE;
4702238926Smm				}
4703238926Smm
4704238926Smm				if (poolfirst) {
4705238926Smm					(void) printf(gettext("%s\n"),
4706238926Smm					    zpool_get_name(zhp));
4707238926Smm					poolfirst = B_FALSE;
4708238926Smm				}
4709238926Smm
4710238926Smm				(void) printf(gettext("      %s\n"), fname);
4711238926Smm			}
4712238926Smm		}
4713238926Smm	}
4714238926Smm
4715238926Smm	return (0);
4716238926Smm}
4717238926Smm
4718168404Spjd/* ARGSUSED */
4719168404Spjdstatic int
4720168404Spjdupgrade_one(zpool_handle_t *zhp, void *data)
4721168404Spjd{
4722238926Smm	boolean_t printnl = B_FALSE;
4723185029Spjd	upgrade_cbdata_t *cbp = data;
4724185029Spjd	uint64_t cur_version;
4725168404Spjd	int ret;
4726168404Spjd
4727185029Spjd	if (strcmp("log", zpool_get_name(zhp)) == 0) {
4728185029Spjd		(void) printf(gettext("'log' is now a reserved word\n"
4729185029Spjd		    "Pool 'log' must be renamed using export and import"
4730185029Spjd		    " to upgrade.\n"));
4731185029Spjd		return (1);
4732185029Spjd	}
4733168404Spjd
4734185029Spjd	cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4735185029Spjd	if (cur_version > cbp->cb_version) {
4736168404Spjd		(void) printf(gettext("Pool '%s' is already formatted "
4737238926Smm		    "using more current version '%llu'.\n\n"),
4738185029Spjd		    zpool_get_name(zhp), cur_version);
4739185029Spjd		return (0);
4740185029Spjd	}
4741238926Smm
4742238926Smm	if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
4743185029Spjd		(void) printf(gettext("Pool '%s' is already formatted "
4744238926Smm		    "using version %llu.\n\n"), zpool_get_name(zhp),
4745238926Smm		    cbp->cb_version);
4746168404Spjd		return (0);
4747168404Spjd	}
4748168404Spjd
4749238926Smm	if (cur_version != cbp->cb_version) {
4750238926Smm		printnl = B_TRUE;
4751238926Smm		ret = upgrade_version(zhp, cbp->cb_version);
4752238950Smm		if (ret != 0)
4753238950Smm			return (ret);
4754238926Smm#ifdef __FreeBSD__
4755238950Smm		root_pool_upgrade_check(zhp, cbp->cb_poolname,
4756238950Smm		    sizeof(cbp->cb_poolname));
4757238926Smm#endif	/* ___FreeBSD__ */
4758238926Smm	}
4759168404Spjd
4760238926Smm	if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4761238926Smm		int count = 0;
4762238926Smm		ret = upgrade_enable_all(zhp, &count);
4763238926Smm		if (ret != 0)
4764238926Smm			return (ret);
4765238926Smm
4766238926Smm		if (count != 0) {
4767238926Smm			printnl = B_TRUE;
4768238950Smm#ifdef __FreeBSD__
4769238951Smm			root_pool_upgrade_check(zhp, cbp->cb_poolname,
4770238951Smm			    sizeof(cbp->cb_poolname));
4771238950Smm#endif	/* __FreeBSD __*/
4772238926Smm		} else if (cur_version == SPA_VERSION) {
4773238926Smm			(void) printf(gettext("Pool '%s' already has all "
4774238926Smm			    "supported features enabled.\n"),
4775238926Smm			    zpool_get_name(zhp));
4776238926Smm		}
4777168404Spjd	}
4778168404Spjd
4779238926Smm	if (printnl) {
4780238926Smm		(void) printf(gettext("\n"));
4781238926Smm	}
4782238926Smm
4783238926Smm	return (0);
4784168404Spjd}
4785168404Spjd
4786168404Spjd/*
4787168404Spjd * zpool upgrade
4788168404Spjd * zpool upgrade -v
4789185029Spjd * zpool upgrade [-V version] <-a | pool ...>
4790168404Spjd *
4791168404Spjd * With no arguments, display downrev'd ZFS pool available for upgrade.
4792168404Spjd * Individual pools can be upgraded by specifying the pool, and '-a' will
4793168404Spjd * upgrade all pools.
4794168404Spjd */
4795168404Spjdint
4796168404Spjdzpool_do_upgrade(int argc, char **argv)
4797168404Spjd{
4798168404Spjd	int c;
4799168404Spjd	upgrade_cbdata_t cb = { 0 };
4800168404Spjd	int ret = 0;
4801168404Spjd	boolean_t showversions = B_FALSE;
4802238926Smm	boolean_t upgradeall = B_FALSE;
4803185029Spjd	char *end;
4804168404Spjd
4805185029Spjd
4806168404Spjd	/* check options */
4807219089Spjd	while ((c = getopt(argc, argv, ":avV:")) != -1) {
4808168404Spjd		switch (c) {
4809168404Spjd		case 'a':
4810238926Smm			upgradeall = B_TRUE;
4811168404Spjd			break;
4812168404Spjd		case 'v':
4813168404Spjd			showversions = B_TRUE;
4814168404Spjd			break;
4815185029Spjd		case 'V':
4816185029Spjd			cb.cb_version = strtoll(optarg, &end, 10);
4817236884Smm			if (*end != '\0' ||
4818236884Smm			    !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
4819185029Spjd				(void) fprintf(stderr,
4820185029Spjd				    gettext("invalid version '%s'\n"), optarg);
4821185029Spjd				usage(B_FALSE);
4822185029Spjd			}
4823185029Spjd			break;
4824219089Spjd		case ':':
4825219089Spjd			(void) fprintf(stderr, gettext("missing argument for "
4826219089Spjd			    "'%c' option\n"), optopt);
4827219089Spjd			usage(B_FALSE);
4828219089Spjd			break;
4829168404Spjd		case '?':
4830168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4831168404Spjd			    optopt);
4832168404Spjd			usage(B_FALSE);
4833168404Spjd		}
4834168404Spjd	}
4835168404Spjd
4836168404Spjd	cb.cb_argc = argc;
4837168404Spjd	cb.cb_argv = argv;
4838168404Spjd	argc -= optind;
4839168404Spjd	argv += optind;
4840168404Spjd
4841185029Spjd	if (cb.cb_version == 0) {
4842185029Spjd		cb.cb_version = SPA_VERSION;
4843238926Smm	} else if (!upgradeall && argc == 0) {
4844185029Spjd		(void) fprintf(stderr, gettext("-V option is "
4845185029Spjd		    "incompatible with other arguments\n"));
4846185029Spjd		usage(B_FALSE);
4847185029Spjd	}
4848185029Spjd
4849168404Spjd	if (showversions) {
4850238926Smm		if (upgradeall || argc != 0) {
4851168404Spjd			(void) fprintf(stderr, gettext("-v option is "
4852168404Spjd			    "incompatible with other arguments\n"));
4853168404Spjd			usage(B_FALSE);
4854168404Spjd		}
4855238926Smm	} else if (upgradeall) {
4856168404Spjd		if (argc != 0) {
4857185029Spjd			(void) fprintf(stderr, gettext("-a option should not "
4858185029Spjd			    "be used along with a pool name\n"));
4859168404Spjd			usage(B_FALSE);
4860168404Spjd		}
4861168404Spjd	}
4862168404Spjd
4863236884Smm	(void) printf(gettext("This system supports ZFS pool feature "
4864236884Smm	    "flags.\n\n"));
4865168404Spjd	if (showversions) {
4866238926Smm		int i;
4867238926Smm
4868238926Smm		(void) printf(gettext("The following features are "
4869168404Spjd		    "supported:\n\n"));
4870238926Smm		(void) printf(gettext("FEAT DESCRIPTION\n"));
4871238926Smm		(void) printf("----------------------------------------------"
4872238926Smm		    "---------------\n");
4873238926Smm		for (i = 0; i < SPA_FEATURES; i++) {
4874238926Smm			zfeature_info_t *fi = &spa_feature_table[i];
4875238926Smm			const char *ro = fi->fi_can_readonly ?
4876238926Smm			    " (read-only compatible)" : "";
4877238926Smm
4878238926Smm			(void) printf("%-37s%s\n", fi->fi_uname, ro);
4879238926Smm			(void) printf("     %s\n", fi->fi_desc);
4880238926Smm		}
4881238926Smm		(void) printf("\n");
4882238926Smm
4883238926Smm		(void) printf(gettext("The following legacy versions are also "
4884238926Smm		    "supported:\n\n"));
4885168404Spjd		(void) printf(gettext("VER  DESCRIPTION\n"));
4886168404Spjd		(void) printf("---  -----------------------------------------"
4887168404Spjd		    "---------------\n");
4888168404Spjd		(void) printf(gettext(" 1   Initial ZFS version\n"));
4889168404Spjd		(void) printf(gettext(" 2   Ditto blocks "
4890168404Spjd		    "(replicated metadata)\n"));
4891168404Spjd		(void) printf(gettext(" 3   Hot spares and double parity "
4892168404Spjd		    "RAID-Z\n"));
4893168404Spjd		(void) printf(gettext(" 4   zpool history\n"));
4894168404Spjd		(void) printf(gettext(" 5   Compression using the gzip "
4895168404Spjd		    "algorithm\n"));
4896185029Spjd		(void) printf(gettext(" 6   bootfs pool property\n"));
4897185029Spjd		(void) printf(gettext(" 7   Separate intent log devices\n"));
4898185029Spjd		(void) printf(gettext(" 8   Delegated administration\n"));
4899185029Spjd		(void) printf(gettext(" 9   refquota and refreservation "
4900185029Spjd		    "properties\n"));
4901185029Spjd		(void) printf(gettext(" 10  Cache devices\n"));
4902185029Spjd		(void) printf(gettext(" 11  Improved scrub performance\n"));
4903185029Spjd		(void) printf(gettext(" 12  Snapshot properties\n"));
4904185029Spjd		(void) printf(gettext(" 13  snapused property\n"));
4905209962Smm		(void) printf(gettext(" 14  passthrough-x aclinherit\n"));
4906209962Smm		(void) printf(gettext(" 15  user/group space accounting\n"));
4907219089Spjd		(void) printf(gettext(" 16  stmf property support\n"));
4908219089Spjd		(void) printf(gettext(" 17  Triple-parity RAID-Z\n"));
4909219089Spjd		(void) printf(gettext(" 18  Snapshot user holds\n"));
4910219089Spjd		(void) printf(gettext(" 19  Log device removal\n"));
4911219089Spjd		(void) printf(gettext(" 20  Compression using zle "
4912219089Spjd		    "(zero-length encoding)\n"));
4913219089Spjd		(void) printf(gettext(" 21  Deduplication\n"));
4914219089Spjd		(void) printf(gettext(" 22  Received properties\n"));
4915219089Spjd		(void) printf(gettext(" 23  Slim ZIL\n"));
4916219089Spjd		(void) printf(gettext(" 24  System attributes\n"));
4917219089Spjd		(void) printf(gettext(" 25  Improved scrub stats\n"));
4918219089Spjd		(void) printf(gettext(" 26  Improved snapshot deletion "
4919219089Spjd		    "performance\n"));
4920219089Spjd		(void) printf(gettext(" 27  Improved snapshot creation "
4921219089Spjd		    "performance\n"));
4922219089Spjd		(void) printf(gettext(" 28  Multiple vdev replacements\n"));
4923219089Spjd		(void) printf(gettext("\nFor more information on a particular "
4924219089Spjd		    "version, including supported releases,\n"));
4925219089Spjd		(void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4926238926Smm	} else if (argc == 0 && upgradeall) {
4927238926Smm		cb.cb_first = B_TRUE;
4928168404Spjd		ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4929238926Smm		if (ret == 0 && cb.cb_first) {
4930238926Smm			if (cb.cb_version == SPA_VERSION) {
4931238926Smm				(void) printf(gettext("All pools are already "
4932238926Smm				    "formatted using feature flags.\n\n"));
4933238926Smm				(void) printf(gettext("Every feature flags "
4934238926Smm				    "pool already has all supported features "
4935238926Smm				    "enabled.\n"));
4936238926Smm			} else {
4937238926Smm				(void) printf(gettext("All pools are already "
4938238926Smm				    "formatted with version %llu or higher.\n"),
4939238926Smm				    cb.cb_version);
4940168404Spjd			}
4941168404Spjd		}
4942238926Smm	} else if (argc == 0) {
4943238926Smm		cb.cb_first = B_TRUE;
4944238926Smm		ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
4945238926Smm		assert(ret == 0);
4946168404Spjd
4947238926Smm		if (cb.cb_first) {
4948238926Smm			(void) printf(gettext("All pools are formatted "
4949238926Smm			    "using feature flags.\n\n"));
4950238926Smm		} else {
4951238926Smm			(void) printf(gettext("\nUse 'zpool upgrade -v' "
4952238926Smm			    "for a list of available legacy versions.\n"));
4953168404Spjd		}
4954238926Smm
4955238926Smm		cb.cb_first = B_TRUE;
4956238926Smm		ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
4957238926Smm		assert(ret == 0);
4958238926Smm
4959238926Smm		if (cb.cb_first) {
4960238926Smm			(void) printf(gettext("Every feature flags pool has "
4961238926Smm			    "all supported features enabled.\n"));
4962238926Smm		} else {
4963238926Smm			(void) printf(gettext("\n"));
4964238926Smm		}
4965168404Spjd	} else {
4966168404Spjd		ret = for_each_pool(argc, argv, B_FALSE, NULL,
4967168404Spjd		    upgrade_one, &cb);
4968168404Spjd	}
4969168404Spjd
4970212050Spjd	if (cb.cb_poolname[0] != '\0') {
4971212050Spjd		(void) printf(
4972212050Spjd		    "If you boot from pool '%s', don't forget to update boot code.\n"
4973212050Spjd		    "Assuming you use GPT partitioning and da0 is your boot disk\n"
4974212050Spjd		    "the following command will do it:\n"
4975212050Spjd		    "\n"
4976212050Spjd		    "\tgpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 da0\n\n",
4977212050Spjd		    cb.cb_poolname);
4978212050Spjd	}
4979212050Spjd
4980168404Spjd	return (ret);
4981168404Spjd}
4982168404Spjd
4983185029Spjdtypedef struct hist_cbdata {
4984185029Spjd	boolean_t first;
4985248571Smm	boolean_t longfmt;
4986248571Smm	boolean_t internal;
4987185029Spjd} hist_cbdata_t;
4988185029Spjd
4989168404Spjd/*
4990168404Spjd * Print out the command history for a specific pool.
4991168404Spjd */
4992168404Spjdstatic int
4993168404Spjdget_history_one(zpool_handle_t *zhp, void *data)
4994168404Spjd{
4995168404Spjd	nvlist_t *nvhis;
4996168404Spjd	nvlist_t **records;
4997168404Spjd	uint_t numrecords;
4998168404Spjd	int ret, i;
4999185029Spjd	hist_cbdata_t *cb = (hist_cbdata_t *)data;
5000168404Spjd
5001185029Spjd	cb->first = B_FALSE;
5002168404Spjd
5003168404Spjd	(void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
5004168404Spjd
5005168404Spjd	if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
5006168404Spjd		return (ret);
5007168404Spjd
5008168404Spjd	verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
5009168404Spjd	    &records, &numrecords) == 0);
5010168404Spjd	for (i = 0; i < numrecords; i++) {
5011248571Smm		nvlist_t *rec = records[i];
5012248571Smm		char tbuf[30] = "";
5013185029Spjd
5014248571Smm		if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
5015248571Smm			time_t tsec;
5016248571Smm			struct tm t;
5017185029Spjd
5018248571Smm			tsec = fnvlist_lookup_uint64(records[i],
5019248571Smm			    ZPOOL_HIST_TIME);
5020248571Smm			(void) localtime_r(&tsec, &t);
5021248571Smm			(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
5022248571Smm		}
5023248571Smm
5024248571Smm		if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
5025248571Smm			(void) printf("%s %s", tbuf,
5026248571Smm			    fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
5027248571Smm		} else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
5028248571Smm			int ievent =
5029248571Smm			    fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
5030248571Smm			if (!cb->internal)
5031185029Spjd				continue;
5032248571Smm			if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
5033248571Smm				(void) printf("%s unrecognized record:\n",
5034248571Smm				    tbuf);
5035248571Smm				dump_nvlist(rec, 4);
5036185029Spjd				continue;
5037248571Smm			}
5038248571Smm			(void) printf("%s [internal %s txg:%lld] %s", tbuf,
5039248571Smm			    zfs_history_event_names[ievent],
5040248571Smm			    fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5041248571Smm			    fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
5042248571Smm		} else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
5043248571Smm			if (!cb->internal)
5044248571Smm				continue;
5045248571Smm			(void) printf("%s [txg:%lld] %s", tbuf,
5046248571Smm			    fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5047248571Smm			    fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
5048248571Smm			if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
5049248571Smm				(void) printf(" %s (%llu)",
5050248571Smm				    fnvlist_lookup_string(rec,
5051248571Smm				    ZPOOL_HIST_DSNAME),
5052248571Smm				    fnvlist_lookup_uint64(rec,
5053248571Smm				    ZPOOL_HIST_DSID));
5054248571Smm			}
5055248571Smm			(void) printf(" %s", fnvlist_lookup_string(rec,
5056248571Smm			    ZPOOL_HIST_INT_STR));
5057248571Smm		} else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
5058248571Smm			if (!cb->internal)
5059248571Smm				continue;
5060248571Smm			(void) printf("%s ioctl %s\n", tbuf,
5061248571Smm			    fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
5062248571Smm			if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
5063248571Smm				(void) printf("    input:\n");
5064248571Smm				dump_nvlist(fnvlist_lookup_nvlist(rec,
5065248571Smm				    ZPOOL_HIST_INPUT_NVL), 8);
5066248571Smm			}
5067248571Smm			if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
5068248571Smm				(void) printf("    output:\n");
5069248571Smm				dump_nvlist(fnvlist_lookup_nvlist(rec,
5070248571Smm				    ZPOOL_HIST_OUTPUT_NVL), 8);
5071248571Smm			}
5072248571Smm		} else {
5073248571Smm			if (!cb->internal)
5074248571Smm				continue;
5075248571Smm			(void) printf("%s unrecognized record:\n", tbuf);
5076248571Smm			dump_nvlist(rec, 4);
5077168404Spjd		}
5078185029Spjd
5079185029Spjd		if (!cb->longfmt) {
5080185029Spjd			(void) printf("\n");
5081185029Spjd			continue;
5082185029Spjd		}
5083185029Spjd		(void) printf(" [");
5084248571Smm		if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
5085248571Smm			uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
5086248571Smm			struct passwd *pwd = getpwuid(who);
5087248571Smm			(void) printf("user %d ", (int)who);
5088248571Smm			if (pwd != NULL)
5089248571Smm				(void) printf("(%s) ", pwd->pw_name);
5090185029Spjd		}
5091248571Smm		if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
5092248571Smm			(void) printf("on %s",
5093248571Smm			    fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
5094185029Spjd		}
5095248571Smm		if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
5096248571Smm			(void) printf(":%s",
5097248571Smm			    fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
5098185029Spjd		}
5099185029Spjd		(void) printf("]");
5100185029Spjd		(void) printf("\n");
5101168404Spjd	}
5102168404Spjd	(void) printf("\n");
5103168404Spjd	nvlist_free(nvhis);
5104168404Spjd
5105168404Spjd	return (ret);
5106168404Spjd}
5107168404Spjd
5108168404Spjd/*
5109168404Spjd * zpool history <pool>
5110168404Spjd *
5111168404Spjd * Displays the history of commands that modified pools.
5112168404Spjd */
5113168404Spjdint
5114168404Spjdzpool_do_history(int argc, char **argv)
5115168404Spjd{
5116185029Spjd	hist_cbdata_t cbdata = { 0 };
5117168404Spjd	int ret;
5118185029Spjd	int c;
5119168404Spjd
5120185029Spjd	cbdata.first = B_TRUE;
5121185029Spjd	/* check options */
5122185029Spjd	while ((c = getopt(argc, argv, "li")) != -1) {
5123185029Spjd		switch (c) {
5124185029Spjd		case 'l':
5125248571Smm			cbdata.longfmt = B_TRUE;
5126185029Spjd			break;
5127185029Spjd		case 'i':
5128248571Smm			cbdata.internal = B_TRUE;
5129185029Spjd			break;
5130185029Spjd		case '?':
5131185029Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
5132185029Spjd			    optopt);
5133185029Spjd			usage(B_FALSE);
5134185029Spjd		}
5135185029Spjd	}
5136168404Spjd	argc -= optind;
5137168404Spjd	argv += optind;
5138168404Spjd
5139168404Spjd	ret = for_each_pool(argc, argv, B_FALSE,  NULL, get_history_one,
5140185029Spjd	    &cbdata);
5141168404Spjd
5142185029Spjd	if (argc == 0 && cbdata.first == B_TRUE) {
5143168404Spjd		(void) printf(gettext("no pools available\n"));
5144168404Spjd		return (0);
5145168404Spjd	}
5146168404Spjd
5147168404Spjd	return (ret);
5148168404Spjd}
5149168404Spjd
5150168404Spjdstatic int
5151168404Spjdget_callback(zpool_handle_t *zhp, void *data)
5152168404Spjd{
5153185029Spjd	zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5154168404Spjd	char value[MAXNAMELEN];
5155185029Spjd	zprop_source_t srctype;
5156185029Spjd	zprop_list_t *pl;
5157168404Spjd
5158168404Spjd	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5159168404Spjd
5160168404Spjd		/*
5161185029Spjd		 * Skip the special fake placeholder. This will also skip
5162185029Spjd		 * over the name property when 'all' is specified.
5163168404Spjd		 */
5164185029Spjd		if (pl->pl_prop == ZPOOL_PROP_NAME &&
5165168404Spjd		    pl == cbp->cb_proplist)
5166168404Spjd			continue;
5167168404Spjd
5168236884Smm		if (pl->pl_prop == ZPROP_INVAL &&
5169236884Smm		    (zpool_prop_feature(pl->pl_user_prop) ||
5170236884Smm		    zpool_prop_unsupported(pl->pl_user_prop))) {
5171236884Smm			srctype = ZPROP_SRC_LOCAL;
5172168404Spjd
5173236884Smm			if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5174236884Smm			    value, sizeof (value)) == 0) {
5175236884Smm				zprop_print_one_property(zpool_get_name(zhp),
5176236884Smm				    cbp, pl->pl_user_prop, value, srctype,
5177236884Smm				    NULL, NULL);
5178236884Smm			}
5179236884Smm		} else {
5180236884Smm			if (zpool_get_prop(zhp, pl->pl_prop, value,
5181236884Smm			    sizeof (value), &srctype) != 0)
5182236884Smm				continue;
5183236884Smm
5184236884Smm			zprop_print_one_property(zpool_get_name(zhp), cbp,
5185236884Smm			    zpool_prop_to_name(pl->pl_prop), value, srctype,
5186236884Smm			    NULL, NULL);
5187236884Smm		}
5188168404Spjd	}
5189168404Spjd	return (0);
5190168404Spjd}
5191168404Spjd
5192168404Spjdint
5193168404Spjdzpool_do_get(int argc, char **argv)
5194168404Spjd{
5195185029Spjd	zprop_get_cbdata_t cb = { 0 };
5196185029Spjd	zprop_list_t fake_name = { 0 };
5197168404Spjd	int ret;
5198168404Spjd
5199236884Smm	if (argc < 2) {
5200236884Smm		(void) fprintf(stderr, gettext("missing property "
5201236884Smm		    "argument\n"));
5202168404Spjd		usage(B_FALSE);
5203236884Smm	}
5204168404Spjd
5205168404Spjd	cb.cb_first = B_TRUE;
5206185029Spjd	cb.cb_sources = ZPROP_SRC_ALL;
5207168404Spjd	cb.cb_columns[0] = GET_COL_NAME;
5208168404Spjd	cb.cb_columns[1] = GET_COL_PROPERTY;
5209168404Spjd	cb.cb_columns[2] = GET_COL_VALUE;
5210168404Spjd	cb.cb_columns[3] = GET_COL_SOURCE;
5211185029Spjd	cb.cb_type = ZFS_TYPE_POOL;
5212168404Spjd
5213236884Smm	if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
5214185029Spjd	    ZFS_TYPE_POOL) != 0)
5215168404Spjd		usage(B_FALSE);
5216168404Spjd
5217168404Spjd	if (cb.cb_proplist != NULL) {
5218185029Spjd		fake_name.pl_prop = ZPOOL_PROP_NAME;
5219168404Spjd		fake_name.pl_width = strlen(gettext("NAME"));
5220168404Spjd		fake_name.pl_next = cb.cb_proplist;
5221168404Spjd		cb.cb_proplist = &fake_name;
5222168404Spjd	}
5223168404Spjd
5224168404Spjd	ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
5225168404Spjd	    get_callback, &cb);
5226168404Spjd
5227168404Spjd	if (cb.cb_proplist == &fake_name)
5228185029Spjd		zprop_free_list(fake_name.pl_next);
5229168404Spjd	else
5230185029Spjd		zprop_free_list(cb.cb_proplist);
5231168404Spjd
5232168404Spjd	return (ret);
5233168404Spjd}
5234168404Spjd
5235168404Spjdtypedef struct set_cbdata {
5236168404Spjd	char *cb_propname;
5237168404Spjd	char *cb_value;
5238168404Spjd	boolean_t cb_any_successful;
5239168404Spjd} set_cbdata_t;
5240168404Spjd
5241168404Spjdint
5242168404Spjdset_callback(zpool_handle_t *zhp, void *data)
5243168404Spjd{
5244168404Spjd	int error;
5245168404Spjd	set_cbdata_t *cb = (set_cbdata_t *)data;
5246168404Spjd
5247168404Spjd	error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5248168404Spjd
5249168404Spjd	if (!error)
5250168404Spjd		cb->cb_any_successful = B_TRUE;
5251168404Spjd
5252168404Spjd	return (error);
5253168404Spjd}
5254168404Spjd
5255168404Spjdint
5256168404Spjdzpool_do_set(int argc, char **argv)
5257168404Spjd{
5258168404Spjd	set_cbdata_t cb = { 0 };
5259168404Spjd	int error;
5260168404Spjd
5261168404Spjd	if (argc > 1 && argv[1][0] == '-') {
5262168404Spjd		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
5263168404Spjd		    argv[1][1]);
5264168404Spjd		usage(B_FALSE);
5265168404Spjd	}
5266168404Spjd
5267168404Spjd	if (argc < 2) {
5268168404Spjd		(void) fprintf(stderr, gettext("missing property=value "
5269168404Spjd		    "argument\n"));
5270168404Spjd		usage(B_FALSE);
5271168404Spjd	}
5272168404Spjd
5273168404Spjd	if (argc < 3) {
5274168404Spjd		(void) fprintf(stderr, gettext("missing pool name\n"));
5275168404Spjd		usage(B_FALSE);
5276168404Spjd	}
5277168404Spjd
5278168404Spjd	if (argc > 3) {
5279168404Spjd		(void) fprintf(stderr, gettext("too many pool names\n"));
5280168404Spjd		usage(B_FALSE);
5281168404Spjd	}
5282168404Spjd
5283168404Spjd	cb.cb_propname = argv[1];
5284168404Spjd	cb.cb_value = strchr(cb.cb_propname, '=');
5285168404Spjd	if (cb.cb_value == NULL) {
5286168404Spjd		(void) fprintf(stderr, gettext("missing value in "
5287168404Spjd		    "property=value argument\n"));
5288168404Spjd		usage(B_FALSE);
5289168404Spjd	}
5290168404Spjd
5291168404Spjd	*(cb.cb_value) = '\0';
5292168404Spjd	cb.cb_value++;
5293168404Spjd
5294168404Spjd	error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5295168404Spjd	    set_callback, &cb);
5296168404Spjd
5297168404Spjd	return (error);
5298168404Spjd}
5299168404Spjd
5300168404Spjdstatic int
5301168404Spjdfind_command_idx(char *command, int *idx)
5302168404Spjd{
5303168404Spjd	int i;
5304168404Spjd
5305168404Spjd	for (i = 0; i < NCOMMAND; i++) {
5306168404Spjd		if (command_table[i].name == NULL)
5307168404Spjd			continue;
5308168404Spjd
5309168404Spjd		if (strcmp(command, command_table[i].name) == 0) {
5310168404Spjd			*idx = i;
5311168404Spjd			return (0);
5312168404Spjd		}
5313168404Spjd	}
5314168404Spjd	return (1);
5315168404Spjd}
5316168404Spjd
5317168404Spjdint
5318168404Spjdmain(int argc, char **argv)
5319168404Spjd{
5320168404Spjd	int ret;
5321168404Spjd	int i;
5322168404Spjd	char *cmdname;
5323168404Spjd
5324168404Spjd	(void) setlocale(LC_ALL, "");
5325168404Spjd	(void) textdomain(TEXT_DOMAIN);
5326168404Spjd
5327168404Spjd	if ((g_zfs = libzfs_init()) == NULL) {
5328168404Spjd		(void) fprintf(stderr, gettext("internal error: failed to "
5329168404Spjd		    "initialize ZFS library\n"));
5330168404Spjd		return (1);
5331168404Spjd	}
5332168404Spjd
5333168404Spjd	libzfs_print_on_error(g_zfs, B_TRUE);
5334168404Spjd
5335168404Spjd	opterr = 0;
5336168404Spjd
5337168404Spjd	/*
5338168404Spjd	 * Make sure the user has specified some command.
5339168404Spjd	 */
5340168404Spjd	if (argc < 2) {
5341168404Spjd		(void) fprintf(stderr, gettext("missing command\n"));
5342168404Spjd		usage(B_FALSE);
5343168404Spjd	}
5344168404Spjd
5345168404Spjd	cmdname = argv[1];
5346168404Spjd
5347168404Spjd	/*
5348168404Spjd	 * Special case '-?'
5349168404Spjd	 */
5350168404Spjd	if (strcmp(cmdname, "-?") == 0)
5351168404Spjd		usage(B_TRUE);
5352168404Spjd
5353248571Smm	zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
5354185029Spjd
5355168404Spjd	/*
5356168404Spjd	 * Run the appropriate command.
5357168404Spjd	 */
5358168404Spjd	if (find_command_idx(cmdname, &i) == 0) {
5359168404Spjd		current_command = &command_table[i];
5360168404Spjd		ret = command_table[i].func(argc - 1, argv + 1);
5361185029Spjd	} else if (strchr(cmdname, '=')) {
5362185029Spjd		verify(find_command_idx("set", &i) == 0);
5363185029Spjd		current_command = &command_table[i];
5364185029Spjd		ret = command_table[i].func(argc, argv);
5365185029Spjd	} else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5366185029Spjd		/*
5367185029Spjd		 * 'freeze' is a vile debugging abomination, so we treat
5368185029Spjd		 * it as such.
5369185029Spjd		 */
5370252059Ssmh		zfs_cmd_t zc = { 0 };
5371252059Ssmh		(void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name));
5372252059Ssmh		return (!!zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc));
5373185029Spjd	} else {
5374168404Spjd		(void) fprintf(stderr, gettext("unrecognized "
5375168404Spjd		    "command '%s'\n"), cmdname);
5376168404Spjd		usage(B_FALSE);
5377168404Spjd	}
5378168404Spjd
5379248571Smm	if (ret == 0 && log_history)
5380248571Smm		(void) zpool_log_history(g_zfs, history_str);
5381248571Smm
5382168404Spjd	libzfs_fini(g_zfs);
5383168404Spjd
5384168404Spjd	/*
5385168404Spjd	 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5386168404Spjd	 * for the purposes of running ::findleaks.
5387168404Spjd	 */
5388168404Spjd	if (getenv("ZFS_ABORT") != NULL) {
5389168404Spjd		(void) printf("dumping core by request\n");
5390168404Spjd		abort();
5391168404Spjd	}
5392168404Spjd
5393168404Spjd	return (ret);
5394168404Spjd}
5395