zpool_main.c revision 259131
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
1705259131Sdelphij	case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
1706259131Sdelphij		(void) printf(gettext("status: One or more devices were "
1707259131Sdelphij		    "configured to use a non-native block size.\n"
1708259131Sdelphij		    "\tExpect reduced performance.\n"));
1709259131Sdelphij		break;
1710259131Sdelphij
1711168404Spjd	default:
1712168404Spjd		/*
1713168404Spjd		 * No other status can be seen when importing pools.
1714168404Spjd		 */
1715168404Spjd		assert(reason == ZPOOL_STATUS_OK);
1716168404Spjd	}
1717168404Spjd
1718168404Spjd	/*
1719168404Spjd	 * Print out an action according to the overall state of the pool.
1720168404Spjd	 */
1721168404Spjd	if (vs->vs_state == VDEV_STATE_HEALTHY) {
1722238926Smm		if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1723238926Smm		    reason == ZPOOL_STATUS_FEAT_DISABLED) {
1724228103Smm			(void) printf(gettext(" action: The pool can be "
1725168404Spjd			    "imported using its name or numeric identifier, "
1726168404Spjd			    "though\n\tsome features will not be available "
1727168404Spjd			    "without an explicit 'zpool upgrade'.\n"));
1728238926Smm		} else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1729228103Smm			(void) printf(gettext(" action: The pool can be "
1730168498Spjd			    "imported using its name or numeric "
1731168498Spjd			    "identifier and\n\tthe '-f' flag.\n"));
1732238926Smm		} else {
1733228103Smm			(void) printf(gettext(" action: The pool can be "
1734168404Spjd			    "imported using its name or numeric "
1735168404Spjd			    "identifier.\n"));
1736238926Smm		}
1737168404Spjd	} else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1738228103Smm		(void) printf(gettext(" action: The pool can be imported "
1739168404Spjd		    "despite missing or damaged devices.  The\n\tfault "
1740168404Spjd		    "tolerance of the pool may be compromised if imported.\n"));
1741168404Spjd	} else {
1742168404Spjd		switch (reason) {
1743168404Spjd		case ZPOOL_STATUS_VERSION_NEWER:
1744228103Smm			(void) printf(gettext(" action: The pool cannot be "
1745168404Spjd			    "imported.  Access the pool on a system running "
1746168404Spjd			    "newer\n\tsoftware, or recreate the pool from "
1747168404Spjd			    "backup.\n"));
1748168404Spjd			break;
1749236884Smm		case ZPOOL_STATUS_UNSUP_FEAT_READ:
1750236884Smm			(void) printf(gettext("action: The pool cannot be "
1751236884Smm			    "imported. Access the pool on a system that "
1752236884Smm			    "supports\n\tthe required feature(s), or recreate "
1753236884Smm			    "the pool from backup.\n"));
1754236884Smm			break;
1755236884Smm		case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1756236884Smm			(void) printf(gettext("action: The pool cannot be "
1757236884Smm			    "imported in read-write mode. Import the pool "
1758236884Smm			    "with\n"
1759236884Smm			    "\t\"-o readonly=on\", access the pool on a system "
1760236884Smm			    "that supports the\n\trequired feature(s), or "
1761236884Smm			    "recreate the pool from backup.\n"));
1762236884Smm			break;
1763168404Spjd		case ZPOOL_STATUS_MISSING_DEV_R:
1764168404Spjd		case ZPOOL_STATUS_MISSING_DEV_NR:
1765168404Spjd		case ZPOOL_STATUS_BAD_GUID_SUM:
1766228103Smm			(void) printf(gettext(" action: The pool cannot be "
1767168404Spjd			    "imported. Attach the missing\n\tdevices and try "
1768168404Spjd			    "again.\n"));
1769168404Spjd			break;
1770168404Spjd		default:
1771228103Smm			(void) printf(gettext(" action: The pool cannot be "
1772168404Spjd			    "imported due to damaged devices or data.\n"));
1773168404Spjd		}
1774168404Spjd	}
1775168404Spjd
1776228103Smm	/* Print the comment attached to the pool. */
1777228103Smm	if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1778228103Smm		(void) printf(gettext("comment: %s\n"), comment);
1779228103Smm
1780168404Spjd	/*
1781168404Spjd	 * If the state is "closed" or "can't open", and the aux state
1782168404Spjd	 * is "corrupt data":
1783168404Spjd	 */
1784168404Spjd	if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1785168404Spjd	    (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1786168404Spjd	    (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1787168404Spjd		if (pool_state == POOL_STATE_DESTROYED)
1788168404Spjd			(void) printf(gettext("\tThe pool was destroyed, "
1789168404Spjd			    "but can be imported using the '-Df' flags.\n"));
1790168404Spjd		else if (pool_state != POOL_STATE_EXPORTED)
1791168404Spjd			(void) printf(gettext("\tThe pool may be active on "
1792185029Spjd			    "another system, but can be imported using\n\t"
1793168404Spjd			    "the '-f' flag.\n"));
1794168404Spjd	}
1795168404Spjd
1796168404Spjd	if (msgid != NULL)
1797236146Smm		(void) printf(gettext("   see: http://illumos.org/msg/%s\n"),
1798168404Spjd		    msgid);
1799168404Spjd
1800228103Smm	(void) printf(gettext(" config:\n\n"));
1801168404Spjd
1802168404Spjd	namewidth = max_width(NULL, nvroot, 0, 0);
1803168404Spjd	if (namewidth < 10)
1804168404Spjd		namewidth = 10;
1805168404Spjd
1806213197Smm	print_import_config(name, nvroot, namewidth, 0);
1807213197Smm	if (num_logs(nvroot) > 0)
1808213197Smm		print_logs(NULL, nvroot, namewidth, B_FALSE);
1809185029Spjd
1810168404Spjd	if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1811168404Spjd		(void) printf(gettext("\n\tAdditional devices are known to "
1812168404Spjd		    "be part of this pool, though their\n\texact "
1813168404Spjd		    "configuration cannot be determined.\n"));
1814168404Spjd	}
1815168404Spjd}
1816168404Spjd
1817168404Spjd/*
1818168404Spjd * Perform the import for the given configuration.  This passes the heavy
1819185029Spjd * lifting off to zpool_import_props(), and then mounts the datasets contained
1820185029Spjd * within the pool.
1821168404Spjd */
1822168404Spjdstatic int
1823168404Spjddo_import(nvlist_t *config, const char *newname, const char *mntopts,
1824219089Spjd    nvlist_t *props, int flags)
1825168404Spjd{
1826168404Spjd	zpool_handle_t *zhp;
1827168404Spjd	char *name;
1828168404Spjd	uint64_t state;
1829168404Spjd	uint64_t version;
1830168404Spjd
1831168404Spjd	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1832168404Spjd	    &name) == 0);
1833168404Spjd
1834168404Spjd	verify(nvlist_lookup_uint64(config,
1835168404Spjd	    ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1836168404Spjd	verify(nvlist_lookup_uint64(config,
1837168404Spjd	    ZPOOL_CONFIG_VERSION, &version) == 0);
1838236884Smm	if (!SPA_VERSION_IS_SUPPORTED(version)) {
1839168404Spjd		(void) fprintf(stderr, gettext("cannot import '%s': pool "
1840236884Smm		    "is formatted using an unsupported ZFS version\n"), name);
1841168404Spjd		return (1);
1842219089Spjd	} else if (state != POOL_STATE_EXPORTED &&
1843219089Spjd	    !(flags & ZFS_IMPORT_ANY_HOST)) {
1844168498Spjd		uint64_t hostid;
1845168498Spjd
1846168498Spjd		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1847168498Spjd		    &hostid) == 0) {
1848168498Spjd			if ((unsigned long)hostid != gethostid()) {
1849168498Spjd				char *hostname;
1850168498Spjd				uint64_t timestamp;
1851168498Spjd				time_t t;
1852168498Spjd
1853168498Spjd				verify(nvlist_lookup_string(config,
1854168498Spjd				    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1855168498Spjd				verify(nvlist_lookup_uint64(config,
1856168498Spjd				    ZPOOL_CONFIG_TIMESTAMP, &timestamp) == 0);
1857168498Spjd				t = timestamp;
1858168498Spjd				(void) fprintf(stderr, gettext("cannot import "
1859168498Spjd				    "'%s': pool may be in use from other "
1860168498Spjd				    "system, it was last accessed by %s "
1861168498Spjd				    "(hostid: 0x%lx) on %s"), name, hostname,
1862168498Spjd				    (unsigned long)hostid,
1863168498Spjd				    asctime(localtime(&t)));
1864168498Spjd				(void) fprintf(stderr, gettext("use '-f' to "
1865168498Spjd				    "import anyway\n"));
1866168498Spjd				return (1);
1867168498Spjd			}
1868168498Spjd		} else {
1869168498Spjd			(void) fprintf(stderr, gettext("cannot import '%s': "
1870168498Spjd			    "pool may be in use from other system\n"), name);
1871168498Spjd			(void) fprintf(stderr, gettext("use '-f' to import "
1872168498Spjd			    "anyway\n"));
1873168498Spjd			return (1);
1874168498Spjd		}
1875168404Spjd	}
1876168404Spjd
1877219089Spjd	if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1878168404Spjd		return (1);
1879168404Spjd
1880168404Spjd	if (newname != NULL)
1881168404Spjd		name = (char *)newname;
1882168404Spjd
1883209962Smm	if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1884209962Smm		return (1);
1885168404Spjd
1886209962Smm	if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1887219089Spjd	    !(flags & ZFS_IMPORT_ONLY) &&
1888209962Smm	    zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1889168404Spjd		zpool_close(zhp);
1890168404Spjd		return (1);
1891168404Spjd	}
1892168404Spjd
1893168404Spjd	zpool_close(zhp);
1894219089Spjd	return (0);
1895168404Spjd}
1896168404Spjd
1897168404Spjd/*
1898168404Spjd * zpool import [-d dir] [-D]
1899185029Spjd *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1900185029Spjd *              [-d dir | -c cachefile] [-f] -a
1901185029Spjd *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1902219089Spjd *              [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1903168404Spjd *
1904185029Spjd *	 -c	Read pool information from a cachefile instead of searching
1905185029Spjd *		devices.
1906185029Spjd *
1907168404Spjd *       -d	Scan in a specific directory, other than /dev/dsk.  More than
1908168404Spjd *		one directory can be specified using multiple '-d' options.
1909168404Spjd *
1910168404Spjd *       -D     Scan for previously destroyed pools or import all or only
1911168404Spjd *              specified destroyed pools.
1912168404Spjd *
1913168404Spjd *       -R	Temporarily import the pool, with all mountpoints relative to
1914168404Spjd *		the given root.  The pool will remain exported when the machine
1915168404Spjd *		is rebooted.
1916168404Spjd *
1917219089Spjd *       -V	Import even in the presence of faulted vdevs.  This is an
1918185029Spjd *       	intentionally undocumented option for testing purposes, and
1919185029Spjd *       	treats the pool configuration as complete, leaving any bad
1920209962Smm *		vdevs in the FAULTED state. In other words, it does verbatim
1921209962Smm *		import.
1922185029Spjd *
1923219089Spjd *       -f	Force import, even if it appears that the pool is active.
1924219089Spjd *
1925219089Spjd *       -F     Attempt rewind if necessary.
1926219089Spjd *
1927219089Spjd *       -n     See if rewind would work, but don't actually rewind.
1928219089Spjd *
1929219089Spjd *       -N     Import the pool but don't mount datasets.
1930219089Spjd *
1931219089Spjd *       -T     Specify a starting txg to use for import. This option is
1932219089Spjd *       	intentionally undocumented option for testing purposes.
1933219089Spjd *
1934168404Spjd *       -a	Import all pools found.
1935168404Spjd *
1936185029Spjd *       -o	Set property=value and/or temporary mount options (without '=').
1937185029Spjd *
1938168404Spjd * The import command scans for pools to import, and import pools based on pool
1939168404Spjd * name and GUID.  The pool can also be renamed as part of the import process.
1940168404Spjd */
1941168404Spjdint
1942168404Spjdzpool_do_import(int argc, char **argv)
1943168404Spjd{
1944168404Spjd	char **searchdirs = NULL;
1945168404Spjd	int nsearch = 0;
1946168404Spjd	int c;
1947219089Spjd	int err = 0;
1948185029Spjd	nvlist_t *pools = NULL;
1949168404Spjd	boolean_t do_all = B_FALSE;
1950168404Spjd	boolean_t do_destroyed = B_FALSE;
1951168404Spjd	char *mntopts = NULL;
1952168404Spjd	nvpair_t *elem;
1953168404Spjd	nvlist_t *config;
1954185029Spjd	uint64_t searchguid = 0;
1955185029Spjd	char *searchname = NULL;
1956185029Spjd	char *propval;
1957168404Spjd	nvlist_t *found_config;
1958219089Spjd	nvlist_t *policy = NULL;
1959185029Spjd	nvlist_t *props = NULL;
1960168404Spjd	boolean_t first;
1961219089Spjd	int flags = ZFS_IMPORT_NORMAL;
1962219089Spjd	uint32_t rewind_policy = ZPOOL_NO_REWIND;
1963219089Spjd	boolean_t dryrun = B_FALSE;
1964219089Spjd	boolean_t do_rewind = B_FALSE;
1965219089Spjd	boolean_t xtreme_rewind = B_FALSE;
1966219089Spjd	uint64_t pool_state, txg = -1ULL;
1967185029Spjd	char *cachefile = NULL;
1968219089Spjd	importargs_t idata = { 0 };
1969219089Spjd	char *endptr;
1970168404Spjd
1971168404Spjd	/* check options */
1972219089Spjd	while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1973168404Spjd		switch (c) {
1974168404Spjd		case 'a':
1975168404Spjd			do_all = B_TRUE;
1976168404Spjd			break;
1977185029Spjd		case 'c':
1978185029Spjd			cachefile = optarg;
1979185029Spjd			break;
1980168404Spjd		case 'd':
1981168404Spjd			if (searchdirs == NULL) {
1982168404Spjd				searchdirs = safe_malloc(sizeof (char *));
1983168404Spjd			} else {
1984168404Spjd				char **tmp = safe_malloc((nsearch + 1) *
1985168404Spjd				    sizeof (char *));
1986168404Spjd				bcopy(searchdirs, tmp, nsearch *
1987168404Spjd				    sizeof (char *));
1988168404Spjd				free(searchdirs);
1989168404Spjd				searchdirs = tmp;
1990168404Spjd			}
1991168404Spjd			searchdirs[nsearch++] = optarg;
1992168404Spjd			break;
1993168404Spjd		case 'D':
1994168404Spjd			do_destroyed = B_TRUE;
1995168404Spjd			break;
1996168404Spjd		case 'f':
1997219089Spjd			flags |= ZFS_IMPORT_ANY_HOST;
1998168404Spjd			break;
1999185029Spjd		case 'F':
2000219089Spjd			do_rewind = B_TRUE;
2001185029Spjd			break;
2002219089Spjd		case 'm':
2003219089Spjd			flags |= ZFS_IMPORT_MISSING_LOG;
2004219089Spjd			break;
2005219089Spjd		case 'n':
2006219089Spjd			dryrun = B_TRUE;
2007219089Spjd			break;
2008219089Spjd		case 'N':
2009219089Spjd			flags |= ZFS_IMPORT_ONLY;
2010219089Spjd			break;
2011168404Spjd		case 'o':
2012185029Spjd			if ((propval = strchr(optarg, '=')) != NULL) {
2013185029Spjd				*propval = '\0';
2014185029Spjd				propval++;
2015185029Spjd				if (add_prop_list(optarg, propval,
2016185029Spjd				    &props, B_TRUE))
2017185029Spjd					goto error;
2018185029Spjd			} else {
2019185029Spjd				mntopts = optarg;
2020185029Spjd			}
2021168404Spjd			break;
2022168404Spjd		case 'R':
2023185029Spjd			if (add_prop_list(zpool_prop_to_name(
2024185029Spjd			    ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2025185029Spjd				goto error;
2026185029Spjd			if (nvlist_lookup_string(props,
2027185029Spjd			    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
2028185029Spjd			    &propval) == 0)
2029185029Spjd				break;
2030185029Spjd			if (add_prop_list(zpool_prop_to_name(
2031185029Spjd			    ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2032185029Spjd				goto error;
2033168404Spjd			break;
2034219089Spjd		case 'T':
2035219089Spjd			errno = 0;
2036219089Spjd			txg = strtoull(optarg, &endptr, 10);
2037219089Spjd			if (errno != 0 || *endptr != '\0') {
2038219089Spjd				(void) fprintf(stderr,
2039219089Spjd				    gettext("invalid txg value\n"));
2040219089Spjd				usage(B_FALSE);
2041219089Spjd			}
2042219089Spjd			rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2043219089Spjd			break;
2044219089Spjd		case 'V':
2045219089Spjd			flags |= ZFS_IMPORT_VERBATIM;
2046219089Spjd			break;
2047219089Spjd		case 'X':
2048219089Spjd			xtreme_rewind = B_TRUE;
2049219089Spjd			break;
2050168404Spjd		case ':':
2051168404Spjd			(void) fprintf(stderr, gettext("missing argument for "
2052168404Spjd			    "'%c' option\n"), optopt);
2053168404Spjd			usage(B_FALSE);
2054168404Spjd			break;
2055168404Spjd		case '?':
2056168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2057168404Spjd			    optopt);
2058168404Spjd			usage(B_FALSE);
2059168404Spjd		}
2060168404Spjd	}
2061168404Spjd
2062168404Spjd	argc -= optind;
2063168404Spjd	argv += optind;
2064168404Spjd
2065185029Spjd	if (cachefile && nsearch != 0) {
2066185029Spjd		(void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2067185029Spjd		usage(B_FALSE);
2068185029Spjd	}
2069185029Spjd
2070219089Spjd	if ((dryrun || xtreme_rewind) && !do_rewind) {
2071219089Spjd		(void) fprintf(stderr,
2072219089Spjd		    gettext("-n or -X only meaningful with -F\n"));
2073219089Spjd		usage(B_FALSE);
2074219089Spjd	}
2075219089Spjd	if (dryrun)
2076219089Spjd		rewind_policy = ZPOOL_TRY_REWIND;
2077219089Spjd	else if (do_rewind)
2078219089Spjd		rewind_policy = ZPOOL_DO_REWIND;
2079219089Spjd	if (xtreme_rewind)
2080219089Spjd		rewind_policy |= ZPOOL_EXTREME_REWIND;
2081219089Spjd
2082219089Spjd	/* In the future, we can capture further policy and include it here */
2083219089Spjd	if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2084219089Spjd	    nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2085219089Spjd	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2086219089Spjd		goto error;
2087219089Spjd
2088168404Spjd	if (searchdirs == NULL) {
2089168404Spjd		searchdirs = safe_malloc(sizeof (char *));
2090235478Savg		searchdirs[0] = "/dev";
2091168404Spjd		nsearch = 1;
2092168404Spjd	}
2093168404Spjd
2094168404Spjd	/* check argument count */
2095168404Spjd	if (do_all) {
2096168404Spjd		if (argc != 0) {
2097168404Spjd			(void) fprintf(stderr, gettext("too many arguments\n"));
2098168404Spjd			usage(B_FALSE);
2099168404Spjd		}
2100168404Spjd	} else {
2101168404Spjd		if (argc > 2) {
2102168404Spjd			(void) fprintf(stderr, gettext("too many arguments\n"));
2103168404Spjd			usage(B_FALSE);
2104168404Spjd		}
2105168404Spjd
2106168404Spjd		/*
2107168404Spjd		 * Check for the SYS_CONFIG privilege.  We do this explicitly
2108168404Spjd		 * here because otherwise any attempt to discover pools will
2109168404Spjd		 * silently fail.
2110168404Spjd		 */
2111168404Spjd		if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2112168404Spjd			(void) fprintf(stderr, gettext("cannot "
2113168404Spjd			    "discover pools: permission denied\n"));
2114168404Spjd			free(searchdirs);
2115219089Spjd			nvlist_free(policy);
2116168404Spjd			return (1);
2117168404Spjd		}
2118168404Spjd	}
2119168404Spjd
2120168404Spjd	/*
2121168404Spjd	 * Depending on the arguments given, we do one of the following:
2122168404Spjd	 *
2123168404Spjd	 *	<none>	Iterate through all pools and display information about
2124168404Spjd	 *		each one.
2125168404Spjd	 *
2126168404Spjd	 *	-a	Iterate through all pools and try to import each one.
2127168404Spjd	 *
2128168404Spjd	 *	<id>	Find the pool that corresponds to the given GUID/pool
2129168404Spjd	 *		name and import that one.
2130168404Spjd	 *
2131168404Spjd	 *	-D	Above options applies only to destroyed pools.
2132168404Spjd	 */
2133168404Spjd	if (argc != 0) {
2134168404Spjd		char *endptr;
2135168404Spjd
2136168404Spjd		errno = 0;
2137168404Spjd		searchguid = strtoull(argv[0], &endptr, 10);
2138254758Sdelphij		if (errno != 0 || *endptr != '\0') {
2139168404Spjd			searchname = argv[0];
2140254758Sdelphij			searchguid = 0;
2141254758Sdelphij		}
2142168404Spjd		found_config = NULL;
2143168404Spjd
2144185029Spjd		/*
2145219089Spjd		 * User specified a name or guid.  Ensure it's unique.
2146185029Spjd		 */
2147219089Spjd		idata.unique = B_TRUE;
2148185029Spjd	}
2149185029Spjd
2150219089Spjd
2151219089Spjd	idata.path = searchdirs;
2152219089Spjd	idata.paths = nsearch;
2153219089Spjd	idata.poolname = searchname;
2154219089Spjd	idata.guid = searchguid;
2155219089Spjd	idata.cachefile = cachefile;
2156219089Spjd
2157219089Spjd	pools = zpool_search_import(g_zfs, &idata);
2158219089Spjd
2159219089Spjd	if (pools != NULL && idata.exists &&
2160219089Spjd	    (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2161219089Spjd		(void) fprintf(stderr, gettext("cannot import '%s': "
2162219089Spjd		    "a pool with that name already exists\n"),
2163219089Spjd		    argv[0]);
2164219089Spjd		(void) fprintf(stderr, gettext("use the form '%s "
2165219089Spjd		    "<pool | id> <newpool>' to give it a new name\n"),
2166219089Spjd		    "zpool import");
2167219089Spjd		err = 1;
2168219089Spjd	} else if (pools == NULL && idata.exists) {
2169219089Spjd		(void) fprintf(stderr, gettext("cannot import '%s': "
2170219089Spjd		    "a pool with that name is already created/imported,\n"),
2171219089Spjd		    argv[0]);
2172219089Spjd		(void) fprintf(stderr, gettext("and no additional pools "
2173219089Spjd		    "with that name were found\n"));
2174219089Spjd		err = 1;
2175219089Spjd	} else if (pools == NULL) {
2176185029Spjd		if (argc != 0) {
2177185029Spjd			(void) fprintf(stderr, gettext("cannot import '%s': "
2178185029Spjd			    "no such pool available\n"), argv[0]);
2179185029Spjd		}
2180219089Spjd		err = 1;
2181219089Spjd	}
2182219089Spjd
2183219089Spjd	if (err == 1) {
2184185029Spjd		free(searchdirs);
2185219089Spjd		nvlist_free(policy);
2186185029Spjd		return (1);
2187185029Spjd	}
2188185029Spjd
2189185029Spjd	/*
2190185029Spjd	 * At this point we have a list of import candidate configs. Even if
2191185029Spjd	 * we were searching by pool name or guid, we still need to
2192185029Spjd	 * post-process the list to deal with pool state and possible
2193185029Spjd	 * duplicate names.
2194185029Spjd	 */
2195168404Spjd	err = 0;
2196168404Spjd	elem = NULL;
2197168404Spjd	first = B_TRUE;
2198168404Spjd	while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2199168404Spjd
2200168404Spjd		verify(nvpair_value_nvlist(elem, &config) == 0);
2201168404Spjd
2202168404Spjd		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2203168404Spjd		    &pool_state) == 0);
2204168404Spjd		if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2205168404Spjd			continue;
2206168404Spjd		if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2207168404Spjd			continue;
2208168404Spjd
2209219089Spjd		verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2210219089Spjd		    policy) == 0);
2211219089Spjd
2212168404Spjd		if (argc == 0) {
2213168404Spjd			if (first)
2214168404Spjd				first = B_FALSE;
2215168404Spjd			else if (!do_all)
2216168404Spjd				(void) printf("\n");
2217168404Spjd
2218219089Spjd			if (do_all) {
2219168404Spjd				err |= do_import(config, NULL, mntopts,
2220219089Spjd				    props, flags);
2221219089Spjd			} else {
2222168404Spjd				show_import(config);
2223219089Spjd			}
2224168404Spjd		} else if (searchname != NULL) {
2225168404Spjd			char *name;
2226168404Spjd
2227168404Spjd			/*
2228168404Spjd			 * We are searching for a pool based on name.
2229168404Spjd			 */
2230168404Spjd			verify(nvlist_lookup_string(config,
2231168404Spjd			    ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2232168404Spjd
2233168404Spjd			if (strcmp(name, searchname) == 0) {
2234168404Spjd				if (found_config != NULL) {
2235168404Spjd					(void) fprintf(stderr, gettext(
2236168404Spjd					    "cannot import '%s': more than "
2237168404Spjd					    "one matching pool\n"), searchname);
2238168404Spjd					(void) fprintf(stderr, gettext(
2239168404Spjd					    "import by numeric ID instead\n"));
2240168404Spjd					err = B_TRUE;
2241168404Spjd				}
2242168404Spjd				found_config = config;
2243168404Spjd			}
2244168404Spjd		} else {
2245168404Spjd			uint64_t guid;
2246168404Spjd
2247168404Spjd			/*
2248168404Spjd			 * Search for a pool by guid.
2249168404Spjd			 */
2250168404Spjd			verify(nvlist_lookup_uint64(config,
2251168404Spjd			    ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2252168404Spjd
2253168404Spjd			if (guid == searchguid)
2254168404Spjd				found_config = config;
2255168404Spjd		}
2256168404Spjd	}
2257168404Spjd
2258168404Spjd	/*
2259168404Spjd	 * If we were searching for a specific pool, verify that we found a
2260168404Spjd	 * pool, and then do the import.
2261168404Spjd	 */
2262168404Spjd	if (argc != 0 && err == 0) {
2263168404Spjd		if (found_config == NULL) {
2264168404Spjd			(void) fprintf(stderr, gettext("cannot import '%s': "
2265168404Spjd			    "no such pool available\n"), argv[0]);
2266168404Spjd			err = B_TRUE;
2267168404Spjd		} else {
2268168404Spjd			err |= do_import(found_config, argc == 1 ? NULL :
2269219089Spjd			    argv[1], mntopts, props, flags);
2270168404Spjd		}
2271168404Spjd	}
2272168404Spjd
2273168404Spjd	/*
2274168404Spjd	 * If we were just looking for pools, report an error if none were
2275168404Spjd	 * found.
2276168404Spjd	 */
2277168404Spjd	if (argc == 0 && first)
2278168404Spjd		(void) fprintf(stderr,
2279168404Spjd		    gettext("no pools available to import\n"));
2280168404Spjd
2281185029Spjderror:
2282185029Spjd	nvlist_free(props);
2283168404Spjd	nvlist_free(pools);
2284219089Spjd	nvlist_free(policy);
2285168404Spjd	free(searchdirs);
2286168404Spjd
2287168404Spjd	return (err ? 1 : 0);
2288168404Spjd}
2289168404Spjd
2290168404Spjdtypedef struct iostat_cbdata {
2291236155Smm	boolean_t cb_verbose;
2292236155Smm	int cb_namewidth;
2293236155Smm	int cb_iteration;
2294168404Spjd	zpool_list_t *cb_list;
2295168404Spjd} iostat_cbdata_t;
2296168404Spjd
2297168404Spjdstatic void
2298168404Spjdprint_iostat_separator(iostat_cbdata_t *cb)
2299168404Spjd{
2300168404Spjd	int i = 0;
2301168404Spjd
2302168404Spjd	for (i = 0; i < cb->cb_namewidth; i++)
2303168404Spjd		(void) printf("-");
2304168404Spjd	(void) printf("  -----  -----  -----  -----  -----  -----\n");
2305168404Spjd}
2306168404Spjd
2307168404Spjdstatic void
2308168404Spjdprint_iostat_header(iostat_cbdata_t *cb)
2309168404Spjd{
2310168404Spjd	(void) printf("%*s     capacity     operations    bandwidth\n",
2311168404Spjd	    cb->cb_namewidth, "");
2312219089Spjd	(void) printf("%-*s  alloc   free   read  write   read  write\n",
2313168404Spjd	    cb->cb_namewidth, "pool");
2314168404Spjd	print_iostat_separator(cb);
2315168404Spjd}
2316168404Spjd
2317168404Spjd/*
2318168404Spjd * Display a single statistic.
2319168404Spjd */
2320185029Spjdstatic void
2321168404Spjdprint_one_stat(uint64_t value)
2322168404Spjd{
2323168404Spjd	char buf[64];
2324168404Spjd
2325168404Spjd	zfs_nicenum(value, buf, sizeof (buf));
2326168404Spjd	(void) printf("  %5s", buf);
2327168404Spjd}
2328168404Spjd
2329168404Spjd/*
2330168404Spjd * Print out all the statistics for the given vdev.  This can either be the
2331168404Spjd * toplevel configuration, or called recursively.  If 'name' is NULL, then this
2332168404Spjd * is a verbose output, and we don't want to display the toplevel pool stats.
2333168404Spjd */
2334168404Spjdvoid
2335168404Spjdprint_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2336168404Spjd    nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2337168404Spjd{
2338168404Spjd	nvlist_t **oldchild, **newchild;
2339168404Spjd	uint_t c, children;
2340168404Spjd	vdev_stat_t *oldvs, *newvs;
2341168404Spjd	vdev_stat_t zerovs = { 0 };
2342168404Spjd	uint64_t tdelta;
2343168404Spjd	double scale;
2344168404Spjd	char *vname;
2345168404Spjd
2346168404Spjd	if (oldnv != NULL) {
2347219089Spjd		verify(nvlist_lookup_uint64_array(oldnv,
2348219089Spjd		    ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2349168404Spjd	} else {
2350168404Spjd		oldvs = &zerovs;
2351168404Spjd	}
2352168404Spjd
2353219089Spjd	verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2354168404Spjd	    (uint64_t **)&newvs, &c) == 0);
2355168404Spjd
2356168404Spjd	if (strlen(name) + depth > cb->cb_namewidth)
2357168404Spjd		(void) printf("%*s%s", depth, "", name);
2358168404Spjd	else
2359168404Spjd		(void) printf("%*s%s%*s", depth, "", name,
2360168404Spjd		    (int)(cb->cb_namewidth - strlen(name) - depth), "");
2361168404Spjd
2362168404Spjd	tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2363168404Spjd
2364168404Spjd	if (tdelta == 0)
2365168404Spjd		scale = 1.0;
2366168404Spjd	else
2367168404Spjd		scale = (double)NANOSEC / tdelta;
2368168404Spjd
2369168404Spjd	/* only toplevel vdevs have capacity stats */
2370168404Spjd	if (newvs->vs_space == 0) {
2371168404Spjd		(void) printf("      -      -");
2372168404Spjd	} else {
2373168404Spjd		print_one_stat(newvs->vs_alloc);
2374168404Spjd		print_one_stat(newvs->vs_space - newvs->vs_alloc);
2375168404Spjd	}
2376168404Spjd
2377168404Spjd	print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2378168404Spjd	    oldvs->vs_ops[ZIO_TYPE_READ])));
2379168404Spjd
2380168404Spjd	print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2381168404Spjd	    oldvs->vs_ops[ZIO_TYPE_WRITE])));
2382168404Spjd
2383168404Spjd	print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2384168404Spjd	    oldvs->vs_bytes[ZIO_TYPE_READ])));
2385168404Spjd
2386168404Spjd	print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2387168404Spjd	    oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2388168404Spjd
2389168404Spjd	(void) printf("\n");
2390168404Spjd
2391168404Spjd	if (!cb->cb_verbose)
2392168404Spjd		return;
2393168404Spjd
2394168404Spjd	if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2395168404Spjd	    &newchild, &children) != 0)
2396168404Spjd		return;
2397168404Spjd
2398168404Spjd	if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2399168404Spjd	    &oldchild, &c) != 0)
2400168404Spjd		return;
2401168404Spjd
2402168404Spjd	for (c = 0; c < children; c++) {
2403227497Smm		uint64_t ishole = B_FALSE, islog = B_FALSE;
2404219089Spjd
2405227497Smm		(void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2406227497Smm		    &ishole);
2407227497Smm
2408227497Smm		(void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2409227497Smm		    &islog);
2410227497Smm
2411227497Smm		if (ishole || islog)
2412219089Spjd			continue;
2413219089Spjd
2414219089Spjd		vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2415168404Spjd		print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2416168404Spjd		    newchild[c], cb, depth + 2);
2417168404Spjd		free(vname);
2418168404Spjd	}
2419185029Spjd
2420185029Spjd	/*
2421227497Smm	 * Log device section
2422227497Smm	 */
2423227497Smm
2424227497Smm	if (num_logs(newnv) > 0) {
2425227497Smm		(void) printf("%-*s      -      -      -      -      -      "
2426227497Smm		    "-\n", cb->cb_namewidth, "logs");
2427227497Smm
2428227497Smm		for (c = 0; c < children; c++) {
2429227497Smm			uint64_t islog = B_FALSE;
2430227497Smm			(void) nvlist_lookup_uint64(newchild[c],
2431227497Smm			    ZPOOL_CONFIG_IS_LOG, &islog);
2432227497Smm
2433227497Smm			if (islog) {
2434227497Smm				vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2435227497Smm				    B_FALSE);
2436227497Smm				print_vdev_stats(zhp, vname, oldnv ?
2437227497Smm				    oldchild[c] : NULL, newchild[c],
2438227497Smm				    cb, depth + 2);
2439227497Smm				free(vname);
2440227497Smm			}
2441227497Smm		}
2442227497Smm
2443227497Smm	}
2444227497Smm
2445227497Smm	/*
2446185029Spjd	 * Include level 2 ARC devices in iostat output
2447185029Spjd	 */
2448185029Spjd	if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2449185029Spjd	    &newchild, &children) != 0)
2450185029Spjd		return;
2451185029Spjd
2452185029Spjd	if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2453185029Spjd	    &oldchild, &c) != 0)
2454185029Spjd		return;
2455185029Spjd
2456185029Spjd	if (children > 0) {
2457185029Spjd		(void) printf("%-*s      -      -      -      -      -      "
2458185029Spjd		    "-\n", cb->cb_namewidth, "cache");
2459185029Spjd		for (c = 0; c < children; c++) {
2460219089Spjd			vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2461219089Spjd			    B_FALSE);
2462185029Spjd			print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2463185029Spjd			    newchild[c], cb, depth + 2);
2464185029Spjd			free(vname);
2465185029Spjd		}
2466185029Spjd	}
2467168404Spjd}
2468168404Spjd
2469168404Spjdstatic int
2470168404Spjdrefresh_iostat(zpool_handle_t *zhp, void *data)
2471168404Spjd{
2472168404Spjd	iostat_cbdata_t *cb = data;
2473168404Spjd	boolean_t missing;
2474168404Spjd
2475168404Spjd	/*
2476168404Spjd	 * If the pool has disappeared, remove it from the list and continue.
2477168404Spjd	 */
2478168404Spjd	if (zpool_refresh_stats(zhp, &missing) != 0)
2479168404Spjd		return (-1);
2480168404Spjd
2481168404Spjd	if (missing)
2482168404Spjd		pool_list_remove(cb->cb_list, zhp);
2483168404Spjd
2484168404Spjd	return (0);
2485168404Spjd}
2486168404Spjd
2487168404Spjd/*
2488168404Spjd * Callback to print out the iostats for the given pool.
2489168404Spjd */
2490168404Spjdint
2491168404Spjdprint_iostat(zpool_handle_t *zhp, void *data)
2492168404Spjd{
2493168404Spjd	iostat_cbdata_t *cb = data;
2494168404Spjd	nvlist_t *oldconfig, *newconfig;
2495168404Spjd	nvlist_t *oldnvroot, *newnvroot;
2496168404Spjd
2497168404Spjd	newconfig = zpool_get_config(zhp, &oldconfig);
2498168404Spjd
2499168404Spjd	if (cb->cb_iteration == 1)
2500168404Spjd		oldconfig = NULL;
2501168404Spjd
2502168404Spjd	verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2503168404Spjd	    &newnvroot) == 0);
2504168404Spjd
2505168404Spjd	if (oldconfig == NULL)
2506168404Spjd		oldnvroot = NULL;
2507168404Spjd	else
2508168404Spjd		verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2509168404Spjd		    &oldnvroot) == 0);
2510168404Spjd
2511168404Spjd	/*
2512168404Spjd	 * Print out the statistics for the pool.
2513168404Spjd	 */
2514168404Spjd	print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2515168404Spjd
2516168404Spjd	if (cb->cb_verbose)
2517168404Spjd		print_iostat_separator(cb);
2518168404Spjd
2519168404Spjd	return (0);
2520168404Spjd}
2521168404Spjd
2522168404Spjdint
2523168404Spjdget_namewidth(zpool_handle_t *zhp, void *data)
2524168404Spjd{
2525168404Spjd	iostat_cbdata_t *cb = data;
2526168404Spjd	nvlist_t *config, *nvroot;
2527168404Spjd
2528168404Spjd	if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2529168404Spjd		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2530168404Spjd		    &nvroot) == 0);
2531168404Spjd		if (!cb->cb_verbose)
2532168404Spjd			cb->cb_namewidth = strlen(zpool_get_name(zhp));
2533168404Spjd		else
2534236145Smm			cb->cb_namewidth = max_width(zhp, nvroot, 0,
2535236145Smm			    cb->cb_namewidth);
2536168404Spjd	}
2537168404Spjd
2538168404Spjd	/*
2539168404Spjd	 * The width must fall into the range [10,38].  The upper limit is the
2540168404Spjd	 * maximum we can have and still fit in 80 columns.
2541168404Spjd	 */
2542168404Spjd	if (cb->cb_namewidth < 10)
2543168404Spjd		cb->cb_namewidth = 10;
2544168404Spjd	if (cb->cb_namewidth > 38)
2545168404Spjd		cb->cb_namewidth = 38;
2546168404Spjd
2547168404Spjd	return (0);
2548168404Spjd}
2549168404Spjd
2550168404Spjd/*
2551219089Spjd * Parse the input string, get the 'interval' and 'count' value if there is one.
2552168404Spjd */
2553219089Spjdstatic void
2554219089Spjdget_interval_count(int *argcp, char **argv, unsigned long *iv,
2555219089Spjd    unsigned long *cnt)
2556168404Spjd{
2557168404Spjd	unsigned long interval = 0, count = 0;
2558219089Spjd	int argc = *argcp, errno;
2559168404Spjd
2560168404Spjd	/*
2561168404Spjd	 * Determine if the last argument is an integer or a pool name
2562168404Spjd	 */
2563168404Spjd	if (argc > 0 && isdigit(argv[argc - 1][0])) {
2564168404Spjd		char *end;
2565168404Spjd
2566168404Spjd		errno = 0;
2567168404Spjd		interval = strtoul(argv[argc - 1], &end, 10);
2568168404Spjd
2569168404Spjd		if (*end == '\0' && errno == 0) {
2570168404Spjd			if (interval == 0) {
2571168404Spjd				(void) fprintf(stderr, gettext("interval "
2572168404Spjd				    "cannot be zero\n"));
2573168404Spjd				usage(B_FALSE);
2574168404Spjd			}
2575168404Spjd			/*
2576168404Spjd			 * Ignore the last parameter
2577168404Spjd			 */
2578168404Spjd			argc--;
2579168404Spjd		} else {
2580168404Spjd			/*
2581168404Spjd			 * If this is not a valid number, just plow on.  The
2582168404Spjd			 * user will get a more informative error message later
2583168404Spjd			 * on.
2584168404Spjd			 */
2585168404Spjd			interval = 0;
2586168404Spjd		}
2587168404Spjd	}
2588168404Spjd
2589168404Spjd	/*
2590168404Spjd	 * If the last argument is also an integer, then we have both a count
2591219089Spjd	 * and an interval.
2592168404Spjd	 */
2593168404Spjd	if (argc > 0 && isdigit(argv[argc - 1][0])) {
2594168404Spjd		char *end;
2595168404Spjd
2596168404Spjd		errno = 0;
2597168404Spjd		count = interval;
2598168404Spjd		interval = strtoul(argv[argc - 1], &end, 10);
2599168404Spjd
2600168404Spjd		if (*end == '\0' && errno == 0) {
2601168404Spjd			if (interval == 0) {
2602168404Spjd				(void) fprintf(stderr, gettext("interval "
2603168404Spjd				    "cannot be zero\n"));
2604168404Spjd				usage(B_FALSE);
2605168404Spjd			}
2606168404Spjd
2607168404Spjd			/*
2608168404Spjd			 * Ignore the last parameter
2609168404Spjd			 */
2610168404Spjd			argc--;
2611168404Spjd		} else {
2612168404Spjd			interval = 0;
2613168404Spjd		}
2614168404Spjd	}
2615168404Spjd
2616219089Spjd	*iv = interval;
2617219089Spjd	*cnt = count;
2618219089Spjd	*argcp = argc;
2619219089Spjd}
2620219089Spjd
2621219089Spjdstatic void
2622219089Spjdget_timestamp_arg(char c)
2623219089Spjd{
2624219089Spjd	if (c == 'u')
2625219089Spjd		timestamp_fmt = UDATE;
2626219089Spjd	else if (c == 'd')
2627219089Spjd		timestamp_fmt = DDATE;
2628219089Spjd	else
2629219089Spjd		usage(B_FALSE);
2630219089Spjd}
2631219089Spjd
2632219089Spjd/*
2633219089Spjd * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2634219089Spjd *
2635219089Spjd *	-v	Display statistics for individual vdevs
2636219089Spjd *	-T	Display a timestamp in date(1) or Unix format
2637219089Spjd *
2638219089Spjd * This command can be tricky because we want to be able to deal with pool
2639219089Spjd * creation/destruction as well as vdev configuration changes.  The bulk of this
2640219089Spjd * processing is handled by the pool_list_* routines in zpool_iter.c.  We rely
2641219089Spjd * on pool_list_update() to detect the addition of new pools.  Configuration
2642219089Spjd * changes are all handled within libzfs.
2643219089Spjd */
2644219089Spjdint
2645219089Spjdzpool_do_iostat(int argc, char **argv)
2646219089Spjd{
2647219089Spjd	int c;
2648219089Spjd	int ret;
2649219089Spjd	int npools;
2650219089Spjd	unsigned long interval = 0, count = 0;
2651219089Spjd	zpool_list_t *list;
2652219089Spjd	boolean_t verbose = B_FALSE;
2653219089Spjd	iostat_cbdata_t cb;
2654219089Spjd
2655219089Spjd	/* check options */
2656219089Spjd	while ((c = getopt(argc, argv, "T:v")) != -1) {
2657219089Spjd		switch (c) {
2658219089Spjd		case 'T':
2659219089Spjd			get_timestamp_arg(*optarg);
2660219089Spjd			break;
2661219089Spjd		case 'v':
2662219089Spjd			verbose = B_TRUE;
2663219089Spjd			break;
2664219089Spjd		case '?':
2665219089Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2666219089Spjd			    optopt);
2667219089Spjd			usage(B_FALSE);
2668219089Spjd		}
2669219089Spjd	}
2670219089Spjd
2671219089Spjd	argc -= optind;
2672219089Spjd	argv += optind;
2673219089Spjd
2674219089Spjd	get_interval_count(&argc, argv, &interval, &count);
2675219089Spjd
2676168404Spjd	/*
2677168404Spjd	 * Construct the list of all interesting pools.
2678168404Spjd	 */
2679168404Spjd	ret = 0;
2680168404Spjd	if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2681168404Spjd		return (1);
2682168404Spjd
2683168404Spjd	if (pool_list_count(list) == 0 && argc != 0) {
2684168404Spjd		pool_list_free(list);
2685168404Spjd		return (1);
2686168404Spjd	}
2687168404Spjd
2688168404Spjd	if (pool_list_count(list) == 0 && interval == 0) {
2689168404Spjd		pool_list_free(list);
2690168404Spjd		(void) fprintf(stderr, gettext("no pools available\n"));
2691168404Spjd		return (1);
2692168404Spjd	}
2693168404Spjd
2694168404Spjd	/*
2695168404Spjd	 * Enter the main iostat loop.
2696168404Spjd	 */
2697168404Spjd	cb.cb_list = list;
2698168404Spjd	cb.cb_verbose = verbose;
2699168404Spjd	cb.cb_iteration = 0;
2700168404Spjd	cb.cb_namewidth = 0;
2701168404Spjd
2702168404Spjd	for (;;) {
2703168404Spjd		pool_list_update(list);
2704168404Spjd
2705168404Spjd		if ((npools = pool_list_count(list)) == 0)
2706168404Spjd			break;
2707168404Spjd
2708168404Spjd		/*
2709168404Spjd		 * Refresh all statistics.  This is done as an explicit step
2710168404Spjd		 * before calculating the maximum name width, so that any
2711168404Spjd		 * configuration changes are properly accounted for.
2712168404Spjd		 */
2713168404Spjd		(void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2714168404Spjd
2715168404Spjd		/*
2716168404Spjd		 * Iterate over all pools to determine the maximum width
2717168404Spjd		 * for the pool / device name column across all pools.
2718168404Spjd		 */
2719168404Spjd		cb.cb_namewidth = 0;
2720168404Spjd		(void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2721168404Spjd
2722219089Spjd		if (timestamp_fmt != NODATE)
2723219089Spjd			print_timestamp(timestamp_fmt);
2724219089Spjd
2725168404Spjd		/*
2726168404Spjd		 * If it's the first time, or verbose mode, print the header.
2727168404Spjd		 */
2728168404Spjd		if (++cb.cb_iteration == 1 || verbose)
2729168404Spjd			print_iostat_header(&cb);
2730168404Spjd
2731168404Spjd		(void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2732168404Spjd
2733168404Spjd		/*
2734168404Spjd		 * If there's more than one pool, and we're not in verbose mode
2735168404Spjd		 * (which prints a separator for us), then print a separator.
2736168404Spjd		 */
2737168404Spjd		if (npools > 1 && !verbose)
2738168404Spjd			print_iostat_separator(&cb);
2739168404Spjd
2740168404Spjd		if (verbose)
2741168404Spjd			(void) printf("\n");
2742168404Spjd
2743168404Spjd		/*
2744168404Spjd		 * Flush the output so that redirection to a file isn't buffered
2745168404Spjd		 * indefinitely.
2746168404Spjd		 */
2747168404Spjd		(void) fflush(stdout);
2748168404Spjd
2749168404Spjd		if (interval == 0)
2750168404Spjd			break;
2751168404Spjd
2752168404Spjd		if (count != 0 && --count == 0)
2753168404Spjd			break;
2754168404Spjd
2755168404Spjd		(void) sleep(interval);
2756168404Spjd	}
2757168404Spjd
2758168404Spjd	pool_list_free(list);
2759168404Spjd
2760168404Spjd	return (ret);
2761168404Spjd}
2762168404Spjd
2763168404Spjdtypedef struct list_cbdata {
2764236155Smm	boolean_t	cb_verbose;
2765236155Smm	int		cb_namewidth;
2766168404Spjd	boolean_t	cb_scripted;
2767185029Spjd	zprop_list_t	*cb_proplist;
2768168404Spjd} list_cbdata_t;
2769168404Spjd
2770168404Spjd/*
2771168404Spjd * Given a list of columns to display, output appropriate headers for each one.
2772168404Spjd */
2773185029Spjdstatic void
2774236155Smmprint_header(list_cbdata_t *cb)
2775168404Spjd{
2776236155Smm	zprop_list_t *pl = cb->cb_proplist;
2777236884Smm	char headerbuf[ZPOOL_MAXPROPLEN];
2778185029Spjd	const char *header;
2779185029Spjd	boolean_t first = B_TRUE;
2780185029Spjd	boolean_t right_justify;
2781236155Smm	size_t width = 0;
2782168404Spjd
2783185029Spjd	for (; pl != NULL; pl = pl->pl_next) {
2784236155Smm		width = pl->pl_width;
2785236155Smm		if (first && cb->cb_verbose) {
2786236155Smm			/*
2787236155Smm			 * Reset the width to accommodate the verbose listing
2788236155Smm			 * of devices.
2789236155Smm			 */
2790236155Smm			width = cb->cb_namewidth;
2791236155Smm		}
2792236155Smm
2793185029Spjd		if (!first)
2794168404Spjd			(void) printf("  ");
2795168404Spjd		else
2796185029Spjd			first = B_FALSE;
2797168404Spjd
2798236884Smm		right_justify = B_FALSE;
2799236884Smm		if (pl->pl_prop != ZPROP_INVAL) {
2800236884Smm			header = zpool_prop_column_name(pl->pl_prop);
2801236884Smm			right_justify = zpool_prop_align_right(pl->pl_prop);
2802236884Smm		} else {
2803236884Smm			int i;
2804185029Spjd
2805236884Smm			for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2806236884Smm				headerbuf[i] = toupper(pl->pl_user_prop[i]);
2807236884Smm			headerbuf[i] = '\0';
2808236884Smm			header = headerbuf;
2809236884Smm		}
2810236884Smm
2811185029Spjd		if (pl->pl_next == NULL && !right_justify)
2812185029Spjd			(void) printf("%s", header);
2813185029Spjd		else if (right_justify)
2814236155Smm			(void) printf("%*s", width, header);
2815185029Spjd		else
2816236155Smm			(void) printf("%-*s", width, header);
2817236155Smm
2818168404Spjd	}
2819168404Spjd
2820168404Spjd	(void) printf("\n");
2821168404Spjd}
2822168404Spjd
2823185029Spjd/*
2824185029Spjd * Given a pool and a list of properties, print out all the properties according
2825185029Spjd * to the described layout.
2826185029Spjd */
2827185029Spjdstatic void
2828236155Smmprint_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2829168404Spjd{
2830236155Smm	zprop_list_t *pl = cb->cb_proplist;
2831185029Spjd	boolean_t first = B_TRUE;
2832185029Spjd	char property[ZPOOL_MAXPROPLEN];
2833185029Spjd	char *propstr;
2834185029Spjd	boolean_t right_justify;
2835236155Smm	size_t width;
2836168404Spjd
2837185029Spjd	for (; pl != NULL; pl = pl->pl_next) {
2838236155Smm
2839236155Smm		width = pl->pl_width;
2840236155Smm		if (first && cb->cb_verbose) {
2841236155Smm			/*
2842236155Smm			 * Reset the width to accommodate the verbose listing
2843236155Smm			 * of devices.
2844236155Smm			 */
2845236155Smm			width = cb->cb_namewidth;
2846236155Smm		}
2847236155Smm
2848185029Spjd		if (!first) {
2849236155Smm			if (cb->cb_scripted)
2850168404Spjd				(void) printf("\t");
2851168404Spjd			else
2852168404Spjd				(void) printf("  ");
2853185029Spjd		} else {
2854185029Spjd			first = B_FALSE;
2855168404Spjd		}
2856168404Spjd
2857185029Spjd		right_justify = B_FALSE;
2858185029Spjd		if (pl->pl_prop != ZPROP_INVAL) {
2859236155Smm			if (pl->pl_prop == ZPOOL_PROP_EXPANDSZ &&
2860236155Smm			    zpool_get_prop_int(zhp, pl->pl_prop, NULL) == 0)
2861236155Smm				propstr = "-";
2862236155Smm			else if (zpool_get_prop(zhp, pl->pl_prop, property,
2863185029Spjd			    sizeof (property), NULL) != 0)
2864185029Spjd				propstr = "-";
2865168404Spjd			else
2866185029Spjd				propstr = property;
2867168404Spjd
2868185029Spjd			right_justify = zpool_prop_align_right(pl->pl_prop);
2869236884Smm		} else if ((zpool_prop_feature(pl->pl_user_prop) ||
2870236884Smm		    zpool_prop_unsupported(pl->pl_user_prop)) &&
2871236884Smm		    zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2872236884Smm		    sizeof (property)) == 0) {
2873236884Smm			propstr = property;
2874185029Spjd		} else {
2875185029Spjd			propstr = "-";
2876185029Spjd		}
2877168404Spjd
2878168404Spjd
2879185029Spjd		/*
2880185029Spjd		 * If this is being called in scripted mode, or if this is the
2881185029Spjd		 * last column and it is left-justified, don't include a width
2882185029Spjd		 * format specifier.
2883185029Spjd		 */
2884236155Smm		if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2885185029Spjd			(void) printf("%s", propstr);
2886185029Spjd		else if (right_justify)
2887185029Spjd			(void) printf("%*s", width, propstr);
2888185029Spjd		else
2889185029Spjd			(void) printf("%-*s", width, propstr);
2890185029Spjd	}
2891168404Spjd
2892185029Spjd	(void) printf("\n");
2893185029Spjd}
2894168404Spjd
2895236155Smmstatic void
2896236155Smmprint_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted)
2897236155Smm{
2898236155Smm	char propval[64];
2899236155Smm	boolean_t fixed;
2900236155Smm	size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
2901236155Smm
2902236155Smm	zfs_nicenum(value, propval, sizeof (propval));
2903236155Smm
2904236155Smm	if (prop == ZPOOL_PROP_EXPANDSZ && value == 0)
2905236155Smm		(void) strlcpy(propval, "-", sizeof (propval));
2906236155Smm
2907236155Smm	if (scripted)
2908236155Smm		(void) printf("\t%s", propval);
2909236155Smm	else
2910236155Smm		(void) printf("  %*s", width, propval);
2911236155Smm}
2912236155Smm
2913236155Smmvoid
2914236155Smmprint_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
2915236155Smm    list_cbdata_t *cb, int depth)
2916236155Smm{
2917236155Smm	nvlist_t **child;
2918236155Smm	vdev_stat_t *vs;
2919236155Smm	uint_t c, children;
2920236155Smm	char *vname;
2921236155Smm	boolean_t scripted = cb->cb_scripted;
2922236155Smm
2923236155Smm	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2924236155Smm	    (uint64_t **)&vs, &c) == 0);
2925236155Smm
2926236155Smm	if (name != NULL) {
2927236155Smm		if (scripted)
2928236155Smm			(void) printf("\t%s", name);
2929236155Smm		else if (strlen(name) + depth > cb->cb_namewidth)
2930236155Smm			(void) printf("%*s%s", depth, "", name);
2931236155Smm		else
2932236155Smm			(void) printf("%*s%s%*s", depth, "", name,
2933236155Smm			    (int)(cb->cb_namewidth - strlen(name) - depth), "");
2934236155Smm
2935236155Smm		/* only toplevel vdevs have capacity stats */
2936236155Smm		if (vs->vs_space == 0) {
2937236155Smm			if (scripted)
2938236155Smm				(void) printf("\t-\t-\t-");
2939236155Smm			else
2940236155Smm				(void) printf("      -      -      -");
2941236155Smm		} else {
2942236155Smm			print_one_column(ZPOOL_PROP_SIZE, vs->vs_space,
2943236155Smm			    scripted);
2944236155Smm			print_one_column(ZPOOL_PROP_CAPACITY, vs->vs_alloc,
2945236155Smm			    scripted);
2946236155Smm			print_one_column(ZPOOL_PROP_FREE,
2947236155Smm			    vs->vs_space - vs->vs_alloc, scripted);
2948236155Smm		}
2949236155Smm		print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize,
2950236155Smm		    scripted);
2951236155Smm		(void) printf("\n");
2952236155Smm	}
2953236155Smm
2954236155Smm	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2955236155Smm	    &child, &children) != 0)
2956236155Smm		return;
2957236155Smm
2958236155Smm	for (c = 0; c < children; c++) {
2959236155Smm		uint64_t ishole = B_FALSE;
2960236155Smm
2961236155Smm		if (nvlist_lookup_uint64(child[c],
2962236155Smm		    ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
2963236155Smm			continue;
2964236155Smm
2965236155Smm		vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
2966236155Smm		print_list_stats(zhp, vname, child[c], cb, depth + 2);
2967236155Smm		free(vname);
2968236155Smm	}
2969236155Smm
2970236155Smm	/*
2971236155Smm	 * Include level 2 ARC devices in iostat output
2972236155Smm	 */
2973236155Smm	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2974236155Smm	    &child, &children) != 0)
2975236155Smm		return;
2976236155Smm
2977236155Smm	if (children > 0) {
2978236155Smm		(void) printf("%-*s      -      -      -      -      -      "
2979236155Smm		    "-\n", cb->cb_namewidth, "cache");
2980236155Smm		for (c = 0; c < children; c++) {
2981236155Smm			vname = zpool_vdev_name(g_zfs, zhp, child[c],
2982236155Smm			    B_FALSE);
2983236155Smm			print_list_stats(zhp, vname, child[c], cb, depth + 2);
2984236155Smm			free(vname);
2985236155Smm		}
2986236155Smm	}
2987236155Smm}
2988236155Smm
2989236155Smm
2990185029Spjd/*
2991185029Spjd * Generic callback function to list a pool.
2992185029Spjd */
2993185029Spjdint
2994185029Spjdlist_callback(zpool_handle_t *zhp, void *data)
2995185029Spjd{
2996185029Spjd	list_cbdata_t *cbp = data;
2997236155Smm	nvlist_t *config;
2998236155Smm	nvlist_t *nvroot;
2999168404Spjd
3000236155Smm	config = zpool_get_config(zhp, NULL);
3001168404Spjd
3002236155Smm	print_pool(zhp, cbp);
3003236155Smm	if (!cbp->cb_verbose)
3004236155Smm		return (0);
3005168404Spjd
3006236155Smm	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3007236155Smm	    &nvroot) == 0);
3008236155Smm	print_list_stats(zhp, NULL, nvroot, cbp, 0);
3009236155Smm
3010168404Spjd	return (0);
3011168404Spjd}
3012168404Spjd
3013168404Spjd/*
3014219089Spjd * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3015168404Spjd *
3016185029Spjd *	-H	Scripted mode.  Don't display headers, and separate properties
3017185029Spjd *		by a single tab.
3018185029Spjd *	-o	List of properties to display.  Defaults to
3019219089Spjd *		"name,size,allocated,free,capacity,health,altroot"
3020219089Spjd *	-T	Display a timestamp in date(1) or Unix format
3021168404Spjd *
3022168404Spjd * List all pools in the system, whether or not they're healthy.  Output space
3023168404Spjd * statistics for each one, as well as health status summary.
3024168404Spjd */
3025168404Spjdint
3026168404Spjdzpool_do_list(int argc, char **argv)
3027168404Spjd{
3028168404Spjd	int c;
3029168404Spjd	int ret;
3030168404Spjd	list_cbdata_t cb = { 0 };
3031185029Spjd	static char default_props[] =
3032236155Smm	    "name,size,allocated,free,capacity,dedupratio,"
3033236155Smm	    "health,altroot";
3034185029Spjd	char *props = default_props;
3035219089Spjd	unsigned long interval = 0, count = 0;
3036236155Smm	zpool_list_t *list;
3037236155Smm	boolean_t first = B_TRUE;
3038168404Spjd
3039168404Spjd	/* check options */
3040236155Smm	while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) {
3041168404Spjd		switch (c) {
3042168404Spjd		case 'H':
3043168404Spjd			cb.cb_scripted = B_TRUE;
3044168404Spjd			break;
3045168404Spjd		case 'o':
3046185029Spjd			props = optarg;
3047168404Spjd			break;
3048219089Spjd		case 'T':
3049219089Spjd			get_timestamp_arg(*optarg);
3050219089Spjd			break;
3051236155Smm		case 'v':
3052236155Smm			cb.cb_verbose = B_TRUE;
3053236155Smm			break;
3054168404Spjd		case ':':
3055168404Spjd			(void) fprintf(stderr, gettext("missing argument for "
3056168404Spjd			    "'%c' option\n"), optopt);
3057168404Spjd			usage(B_FALSE);
3058168404Spjd			break;
3059168404Spjd		case '?':
3060168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3061168404Spjd			    optopt);
3062168404Spjd			usage(B_FALSE);
3063168404Spjd		}
3064168404Spjd	}
3065168404Spjd
3066168404Spjd	argc -= optind;
3067168404Spjd	argv += optind;
3068168404Spjd
3069219089Spjd	get_interval_count(&argc, argv, &interval, &count);
3070219089Spjd
3071185029Spjd	if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3072185029Spjd		usage(B_FALSE);
3073168404Spjd
3074236155Smm	if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL)
3075236155Smm		return (1);
3076168404Spjd
3077236155Smm	if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3078236155Smm		(void) printf(gettext("no pools available\n"));
3079236155Smm		zprop_free_list(cb.cb_proplist);
3080236155Smm		return (0);
3081236155Smm	}
3082236155Smm
3083219089Spjd	for (;;) {
3084236155Smm		pool_list_update(list);
3085168404Spjd
3086236155Smm		if (pool_list_count(list) == 0)
3087236155Smm			break;
3088236155Smm
3089236155Smm		cb.cb_namewidth = 0;
3090236155Smm		(void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3091236155Smm
3092219089Spjd		if (timestamp_fmt != NODATE)
3093219089Spjd			print_timestamp(timestamp_fmt);
3094168404Spjd
3095236155Smm		if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3096236155Smm			print_header(&cb);
3097236155Smm			first = B_FALSE;
3098219089Spjd		}
3099236155Smm		ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
3100219089Spjd
3101219089Spjd		if (interval == 0)
3102219089Spjd			break;
3103219089Spjd
3104219089Spjd		if (count != 0 && --count == 0)
3105219089Spjd			break;
3106219089Spjd
3107219089Spjd		(void) sleep(interval);
3108168404Spjd	}
3109168404Spjd
3110219089Spjd	zprop_free_list(cb.cb_proplist);
3111168404Spjd	return (ret);
3112168404Spjd}
3113168404Spjd
3114168404Spjdstatic nvlist_t *
3115168404Spjdzpool_get_vdev_by_name(nvlist_t *nv, char *name)
3116168404Spjd{
3117168404Spjd	nvlist_t **child;
3118168404Spjd	uint_t c, children;
3119168404Spjd	nvlist_t *match;
3120168404Spjd	char *path;
3121168404Spjd
3122168404Spjd	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3123168404Spjd	    &child, &children) != 0) {
3124168404Spjd		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
3125219089Spjd		if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3126219089Spjd			name += sizeof(_PATH_DEV) - 1;
3127219089Spjd		if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3128219089Spjd			path += sizeof(_PATH_DEV) - 1;
3129168404Spjd		if (strcmp(name, path) == 0)
3130168404Spjd			return (nv);
3131168404Spjd		return (NULL);
3132168404Spjd	}
3133168404Spjd
3134168404Spjd	for (c = 0; c < children; c++)
3135168404Spjd		if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
3136168404Spjd			return (match);
3137168404Spjd
3138168404Spjd	return (NULL);
3139168404Spjd}
3140168404Spjd
3141168404Spjdstatic int
3142168404Spjdzpool_do_attach_or_replace(int argc, char **argv, int replacing)
3143168404Spjd{
3144168404Spjd	boolean_t force = B_FALSE;
3145168404Spjd	int c;
3146168404Spjd	nvlist_t *nvroot;
3147168404Spjd	char *poolname, *old_disk, *new_disk;
3148168404Spjd	zpool_handle_t *zhp;
3149168404Spjd	int ret;
3150168404Spjd
3151168404Spjd	/* check options */
3152168404Spjd	while ((c = getopt(argc, argv, "f")) != -1) {
3153168404Spjd		switch (c) {
3154168404Spjd		case 'f':
3155168404Spjd			force = B_TRUE;
3156168404Spjd			break;
3157168404Spjd		case '?':
3158168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3159168404Spjd			    optopt);
3160168404Spjd			usage(B_FALSE);
3161168404Spjd		}
3162168404Spjd	}
3163168404Spjd
3164168404Spjd	argc -= optind;
3165168404Spjd	argv += optind;
3166168404Spjd
3167168404Spjd	/* get pool name and check number of arguments */
3168168404Spjd	if (argc < 1) {
3169168404Spjd		(void) fprintf(stderr, gettext("missing pool name argument\n"));
3170168404Spjd		usage(B_FALSE);
3171168404Spjd	}
3172168404Spjd
3173168404Spjd	poolname = argv[0];
3174168404Spjd
3175168404Spjd	if (argc < 2) {
3176168404Spjd		(void) fprintf(stderr,
3177168404Spjd		    gettext("missing <device> specification\n"));
3178168404Spjd		usage(B_FALSE);
3179168404Spjd	}
3180168404Spjd
3181168404Spjd	old_disk = argv[1];
3182168404Spjd
3183168404Spjd	if (argc < 3) {
3184168404Spjd		if (!replacing) {
3185168404Spjd			(void) fprintf(stderr,
3186168404Spjd			    gettext("missing <new_device> specification\n"));
3187168404Spjd			usage(B_FALSE);
3188168404Spjd		}
3189168404Spjd		new_disk = old_disk;
3190168404Spjd		argc -= 1;
3191168404Spjd		argv += 1;
3192168404Spjd	} else {
3193168404Spjd		new_disk = argv[2];
3194168404Spjd		argc -= 2;
3195168404Spjd		argv += 2;
3196168404Spjd	}
3197168404Spjd
3198168404Spjd	if (argc > 1) {
3199168404Spjd		(void) fprintf(stderr, gettext("too many arguments\n"));
3200168404Spjd		usage(B_FALSE);
3201168404Spjd	}
3202168404Spjd
3203168404Spjd	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3204168404Spjd		return (1);
3205168404Spjd
3206185029Spjd	if (zpool_get_config(zhp, NULL) == NULL) {
3207168404Spjd		(void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3208168404Spjd		    poolname);
3209168404Spjd		zpool_close(zhp);
3210168404Spjd		return (1);
3211168404Spjd	}
3212168404Spjd
3213185029Spjd	nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
3214185029Spjd	    argc, argv);
3215168404Spjd	if (nvroot == NULL) {
3216168404Spjd		zpool_close(zhp);
3217168404Spjd		return (1);
3218168404Spjd	}
3219168404Spjd
3220168404Spjd	ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3221168404Spjd
3222168404Spjd	nvlist_free(nvroot);
3223168404Spjd	zpool_close(zhp);
3224168404Spjd
3225168404Spjd	return (ret);
3226168404Spjd}
3227168404Spjd
3228168404Spjd/*
3229168404Spjd * zpool replace [-f] <pool> <device> <new_device>
3230168404Spjd *
3231168404Spjd *	-f	Force attach, even if <new_device> appears to be in use.
3232168404Spjd *
3233168404Spjd * Replace <device> with <new_device>.
3234168404Spjd */
3235168404Spjd/* ARGSUSED */
3236168404Spjdint
3237168404Spjdzpool_do_replace(int argc, char **argv)
3238168404Spjd{
3239168404Spjd	return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3240168404Spjd}
3241168404Spjd
3242168404Spjd/*
3243168404Spjd * zpool attach [-f] <pool> <device> <new_device>
3244168404Spjd *
3245168404Spjd *	-f	Force attach, even if <new_device> appears to be in use.
3246168404Spjd *
3247168404Spjd * Attach <new_device> to the mirror containing <device>.  If <device> is not
3248168404Spjd * part of a mirror, then <device> will be transformed into a mirror of
3249168404Spjd * <device> and <new_device>.  In either case, <new_device> will begin life
3250168404Spjd * with a DTL of [0, now], and will immediately begin to resilver itself.
3251168404Spjd */
3252168404Spjdint
3253168404Spjdzpool_do_attach(int argc, char **argv)
3254168404Spjd{
3255168404Spjd	return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3256168404Spjd}
3257168404Spjd
3258168404Spjd/*
3259168404Spjd * zpool detach [-f] <pool> <device>
3260168404Spjd *
3261168404Spjd *	-f	Force detach of <device>, even if DTLs argue against it
3262168404Spjd *		(not supported yet)
3263168404Spjd *
3264168404Spjd * Detach a device from a mirror.  The operation will be refused if <device>
3265168404Spjd * is the last device in the mirror, or if the DTLs indicate that this device
3266168404Spjd * has the only valid copy of some data.
3267168404Spjd */
3268168404Spjd/* ARGSUSED */
3269168404Spjdint
3270168404Spjdzpool_do_detach(int argc, char **argv)
3271168404Spjd{
3272168404Spjd	int c;
3273168404Spjd	char *poolname, *path;
3274168404Spjd	zpool_handle_t *zhp;
3275168404Spjd	int ret;
3276168404Spjd
3277168404Spjd	/* check options */
3278168404Spjd	while ((c = getopt(argc, argv, "f")) != -1) {
3279168404Spjd		switch (c) {
3280168404Spjd		case 'f':
3281168404Spjd		case '?':
3282168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3283168404Spjd			    optopt);
3284168404Spjd			usage(B_FALSE);
3285168404Spjd		}
3286168404Spjd	}
3287168404Spjd
3288168404Spjd	argc -= optind;
3289168404Spjd	argv += optind;
3290168404Spjd
3291168404Spjd	/* get pool name and check number of arguments */
3292168404Spjd	if (argc < 1) {
3293168404Spjd		(void) fprintf(stderr, gettext("missing pool name argument\n"));
3294168404Spjd		usage(B_FALSE);
3295168404Spjd	}
3296168404Spjd
3297168404Spjd	if (argc < 2) {
3298168404Spjd		(void) fprintf(stderr,
3299168404Spjd		    gettext("missing <device> specification\n"));
3300168404Spjd		usage(B_FALSE);
3301168404Spjd	}
3302168404Spjd
3303168404Spjd	poolname = argv[0];
3304168404Spjd	path = argv[1];
3305168404Spjd
3306168404Spjd	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3307168404Spjd		return (1);
3308168404Spjd
3309168404Spjd	ret = zpool_vdev_detach(zhp, path);
3310168404Spjd
3311168404Spjd	zpool_close(zhp);
3312168404Spjd
3313168404Spjd	return (ret);
3314168404Spjd}
3315168404Spjd
3316168404Spjd/*
3317219089Spjd * zpool split [-n] [-o prop=val] ...
3318219089Spjd *		[-o mntopt] ...
3319219089Spjd *		[-R altroot] <pool> <newpool> [<device> ...]
3320219089Spjd *
3321219089Spjd *	-n	Do not split the pool, but display the resulting layout if
3322219089Spjd *		it were to be split.
3323219089Spjd *	-o	Set property=value, or set mount options.
3324219089Spjd *	-R	Mount the split-off pool under an alternate root.
3325219089Spjd *
3326219089Spjd * Splits the named pool and gives it the new pool name.  Devices to be split
3327219089Spjd * off may be listed, provided that no more than one device is specified
3328219089Spjd * per top-level vdev mirror.  The newly split pool is left in an exported
3329219089Spjd * state unless -R is specified.
3330219089Spjd *
3331219089Spjd * Restrictions: the top-level of the pool pool must only be made up of
3332219089Spjd * mirrors; all devices in the pool must be healthy; no device may be
3333219089Spjd * undergoing a resilvering operation.
3334219089Spjd */
3335219089Spjdint
3336219089Spjdzpool_do_split(int argc, char **argv)
3337219089Spjd{
3338219089Spjd	char *srcpool, *newpool, *propval;
3339219089Spjd	char *mntopts = NULL;
3340219089Spjd	splitflags_t flags;
3341219089Spjd	int c, ret = 0;
3342219089Spjd	zpool_handle_t *zhp;
3343219089Spjd	nvlist_t *config, *props = NULL;
3344219089Spjd
3345219089Spjd	flags.dryrun = B_FALSE;
3346219089Spjd	flags.import = B_FALSE;
3347219089Spjd
3348219089Spjd	/* check options */
3349219089Spjd	while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3350219089Spjd		switch (c) {
3351219089Spjd		case 'R':
3352219089Spjd			flags.import = B_TRUE;
3353219089Spjd			if (add_prop_list(
3354219089Spjd			    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3355219089Spjd			    &props, B_TRUE) != 0) {
3356219089Spjd				if (props)
3357219089Spjd					nvlist_free(props);
3358219089Spjd				usage(B_FALSE);
3359219089Spjd			}
3360219089Spjd			break;
3361219089Spjd		case 'n':
3362219089Spjd			flags.dryrun = B_TRUE;
3363219089Spjd			break;
3364219089Spjd		case 'o':
3365219089Spjd			if ((propval = strchr(optarg, '=')) != NULL) {
3366219089Spjd				*propval = '\0';
3367219089Spjd				propval++;
3368219089Spjd				if (add_prop_list(optarg, propval,
3369219089Spjd				    &props, B_TRUE) != 0) {
3370219089Spjd					if (props)
3371219089Spjd						nvlist_free(props);
3372219089Spjd					usage(B_FALSE);
3373219089Spjd				}
3374219089Spjd			} else {
3375219089Spjd				mntopts = optarg;
3376219089Spjd			}
3377219089Spjd			break;
3378219089Spjd		case ':':
3379219089Spjd			(void) fprintf(stderr, gettext("missing argument for "
3380219089Spjd			    "'%c' option\n"), optopt);
3381219089Spjd			usage(B_FALSE);
3382219089Spjd			break;
3383219089Spjd		case '?':
3384219089Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3385219089Spjd			    optopt);
3386219089Spjd			usage(B_FALSE);
3387219089Spjd			break;
3388219089Spjd		}
3389219089Spjd	}
3390219089Spjd
3391219089Spjd	if (!flags.import && mntopts != NULL) {
3392219089Spjd		(void) fprintf(stderr, gettext("setting mntopts is only "
3393219089Spjd		    "valid when importing the pool\n"));
3394219089Spjd		usage(B_FALSE);
3395219089Spjd	}
3396219089Spjd
3397219089Spjd	argc -= optind;
3398219089Spjd	argv += optind;
3399219089Spjd
3400219089Spjd	if (argc < 1) {
3401219089Spjd		(void) fprintf(stderr, gettext("Missing pool name\n"));
3402219089Spjd		usage(B_FALSE);
3403219089Spjd	}
3404219089Spjd	if (argc < 2) {
3405219089Spjd		(void) fprintf(stderr, gettext("Missing new pool name\n"));
3406219089Spjd		usage(B_FALSE);
3407219089Spjd	}
3408219089Spjd
3409219089Spjd	srcpool = argv[0];
3410219089Spjd	newpool = argv[1];
3411219089Spjd
3412219089Spjd	argc -= 2;
3413219089Spjd	argv += 2;
3414219089Spjd
3415219089Spjd	if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3416219089Spjd		return (1);
3417219089Spjd
3418219089Spjd	config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3419219089Spjd	if (config == NULL) {
3420219089Spjd		ret = 1;
3421219089Spjd	} else {
3422219089Spjd		if (flags.dryrun) {
3423219089Spjd			(void) printf(gettext("would create '%s' with the "
3424219089Spjd			    "following layout:\n\n"), newpool);
3425219089Spjd			print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3426219089Spjd		}
3427219089Spjd		nvlist_free(config);
3428219089Spjd	}
3429219089Spjd
3430219089Spjd	zpool_close(zhp);
3431219089Spjd
3432219089Spjd	if (ret != 0 || flags.dryrun || !flags.import)
3433219089Spjd		return (ret);
3434219089Spjd
3435219089Spjd	/*
3436219089Spjd	 * The split was successful. Now we need to open the new
3437219089Spjd	 * pool and import it.
3438219089Spjd	 */
3439219089Spjd	if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3440219089Spjd		return (1);
3441219089Spjd	if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3442219089Spjd	    zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3443219089Spjd		ret = 1;
3444240415Smm		(void) fprintf(stderr, gettext("Split was successful, but "
3445219089Spjd		    "the datasets could not all be mounted\n"));
3446219089Spjd		(void) fprintf(stderr, gettext("Try doing '%s' with a "
3447219089Spjd		    "different altroot\n"), "zpool import");
3448219089Spjd	}
3449219089Spjd	zpool_close(zhp);
3450219089Spjd
3451219089Spjd	return (ret);
3452219089Spjd}
3453219089Spjd
3454219089Spjd
3455219089Spjd
3456219089Spjd/*
3457168404Spjd * zpool online <pool> <device> ...
3458168404Spjd */
3459168404Spjdint
3460168404Spjdzpool_do_online(int argc, char **argv)
3461168404Spjd{
3462168404Spjd	int c, i;
3463168404Spjd	char *poolname;
3464168404Spjd	zpool_handle_t *zhp;
3465168404Spjd	int ret = 0;
3466185029Spjd	vdev_state_t newstate;
3467219089Spjd	int flags = 0;
3468168404Spjd
3469168404Spjd	/* check options */
3470219089Spjd	while ((c = getopt(argc, argv, "et")) != -1) {
3471168404Spjd		switch (c) {
3472219089Spjd		case 'e':
3473219089Spjd			flags |= ZFS_ONLINE_EXPAND;
3474219089Spjd			break;
3475168404Spjd		case 't':
3476168404Spjd		case '?':
3477168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3478168404Spjd			    optopt);
3479168404Spjd			usage(B_FALSE);
3480168404Spjd		}
3481168404Spjd	}
3482168404Spjd
3483168404Spjd	argc -= optind;
3484168404Spjd	argv += optind;
3485168404Spjd
3486168404Spjd	/* get pool name and check number of arguments */
3487168404Spjd	if (argc < 1) {
3488168404Spjd		(void) fprintf(stderr, gettext("missing pool name\n"));
3489168404Spjd		usage(B_FALSE);
3490168404Spjd	}
3491168404Spjd	if (argc < 2) {
3492168404Spjd		(void) fprintf(stderr, gettext("missing device name\n"));
3493168404Spjd		usage(B_FALSE);
3494168404Spjd	}
3495168404Spjd
3496168404Spjd	poolname = argv[0];
3497168404Spjd
3498168404Spjd	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3499168404Spjd		return (1);
3500168404Spjd
3501185029Spjd	for (i = 1; i < argc; i++) {
3502219089Spjd		if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3503185029Spjd			if (newstate != VDEV_STATE_HEALTHY) {
3504185029Spjd				(void) printf(gettext("warning: device '%s' "
3505185029Spjd				    "onlined, but remains in faulted state\n"),
3506185029Spjd				    argv[i]);
3507185029Spjd				if (newstate == VDEV_STATE_FAULTED)
3508185029Spjd					(void) printf(gettext("use 'zpool "
3509185029Spjd					    "clear' to restore a faulted "
3510185029Spjd					    "device\n"));
3511185029Spjd				else
3512185029Spjd					(void) printf(gettext("use 'zpool "
3513185029Spjd					    "replace' to replace devices "
3514185029Spjd					    "that are no longer present\n"));
3515185029Spjd			}
3516185029Spjd		} else {
3517168404Spjd			ret = 1;
3518185029Spjd		}
3519185029Spjd	}
3520168404Spjd
3521168404Spjd	zpool_close(zhp);
3522168404Spjd
3523168404Spjd	return (ret);
3524168404Spjd}
3525168404Spjd
3526168404Spjd/*
3527168404Spjd * zpool offline [-ft] <pool> <device> ...
3528168404Spjd *
3529168404Spjd *	-f	Force the device into the offline state, even if doing
3530168404Spjd *		so would appear to compromise pool availability.
3531168404Spjd *		(not supported yet)
3532168404Spjd *
3533168404Spjd *	-t	Only take the device off-line temporarily.  The offline
3534168404Spjd *		state will not be persistent across reboots.
3535168404Spjd */
3536168404Spjd/* ARGSUSED */
3537168404Spjdint
3538168404Spjdzpool_do_offline(int argc, char **argv)
3539168404Spjd{
3540168404Spjd	int c, i;
3541168404Spjd	char *poolname;
3542168404Spjd	zpool_handle_t *zhp;
3543168404Spjd	int ret = 0;
3544168404Spjd	boolean_t istmp = B_FALSE;
3545168404Spjd
3546168404Spjd	/* check options */
3547168404Spjd	while ((c = getopt(argc, argv, "ft")) != -1) {
3548168404Spjd		switch (c) {
3549168404Spjd		case 't':
3550168404Spjd			istmp = B_TRUE;
3551168404Spjd			break;
3552168404Spjd		case 'f':
3553168404Spjd		case '?':
3554168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3555168404Spjd			    optopt);
3556168404Spjd			usage(B_FALSE);
3557168404Spjd		}
3558168404Spjd	}
3559168404Spjd
3560168404Spjd	argc -= optind;
3561168404Spjd	argv += optind;
3562168404Spjd
3563168404Spjd	/* get pool name and check number of arguments */
3564168404Spjd	if (argc < 1) {
3565168404Spjd		(void) fprintf(stderr, gettext("missing pool name\n"));
3566168404Spjd		usage(B_FALSE);
3567168404Spjd	}
3568168404Spjd	if (argc < 2) {
3569168404Spjd		(void) fprintf(stderr, gettext("missing device name\n"));
3570168404Spjd		usage(B_FALSE);
3571168404Spjd	}
3572168404Spjd
3573168404Spjd	poolname = argv[0];
3574168404Spjd
3575168404Spjd	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3576168404Spjd		return (1);
3577168404Spjd
3578185029Spjd	for (i = 1; i < argc; i++) {
3579185029Spjd		if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3580168404Spjd			ret = 1;
3581185029Spjd	}
3582168404Spjd
3583168404Spjd	zpool_close(zhp);
3584168404Spjd
3585168404Spjd	return (ret);
3586168404Spjd}
3587168404Spjd
3588168404Spjd/*
3589168404Spjd * zpool clear <pool> [device]
3590168404Spjd *
3591168404Spjd * Clear all errors associated with a pool or a particular device.
3592168404Spjd */
3593168404Spjdint
3594168404Spjdzpool_do_clear(int argc, char **argv)
3595168404Spjd{
3596219089Spjd	int c;
3597168404Spjd	int ret = 0;
3598219089Spjd	boolean_t dryrun = B_FALSE;
3599219089Spjd	boolean_t do_rewind = B_FALSE;
3600219089Spjd	boolean_t xtreme_rewind = B_FALSE;
3601219089Spjd	uint32_t rewind_policy = ZPOOL_NO_REWIND;
3602219089Spjd	nvlist_t *policy = NULL;
3603168404Spjd	zpool_handle_t *zhp;
3604168404Spjd	char *pool, *device;
3605168404Spjd
3606219089Spjd	/* check options */
3607219089Spjd	while ((c = getopt(argc, argv, "FnX")) != -1) {
3608219089Spjd		switch (c) {
3609219089Spjd		case 'F':
3610219089Spjd			do_rewind = B_TRUE;
3611219089Spjd			break;
3612219089Spjd		case 'n':
3613219089Spjd			dryrun = B_TRUE;
3614219089Spjd			break;
3615219089Spjd		case 'X':
3616219089Spjd			xtreme_rewind = B_TRUE;
3617219089Spjd			break;
3618219089Spjd		case '?':
3619219089Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3620219089Spjd			    optopt);
3621219089Spjd			usage(B_FALSE);
3622219089Spjd		}
3623219089Spjd	}
3624219089Spjd
3625219089Spjd	argc -= optind;
3626219089Spjd	argv += optind;
3627219089Spjd
3628219089Spjd	if (argc < 1) {
3629168404Spjd		(void) fprintf(stderr, gettext("missing pool name\n"));
3630168404Spjd		usage(B_FALSE);
3631168404Spjd	}
3632168404Spjd
3633219089Spjd	if (argc > 2) {
3634168404Spjd		(void) fprintf(stderr, gettext("too many arguments\n"));
3635168404Spjd		usage(B_FALSE);
3636168404Spjd	}
3637168404Spjd
3638219089Spjd	if ((dryrun || xtreme_rewind) && !do_rewind) {
3639219089Spjd		(void) fprintf(stderr,
3640219089Spjd		    gettext("-n or -X only meaningful with -F\n"));
3641219089Spjd		usage(B_FALSE);
3642219089Spjd	}
3643219089Spjd	if (dryrun)
3644219089Spjd		rewind_policy = ZPOOL_TRY_REWIND;
3645219089Spjd	else if (do_rewind)
3646219089Spjd		rewind_policy = ZPOOL_DO_REWIND;
3647219089Spjd	if (xtreme_rewind)
3648219089Spjd		rewind_policy |= ZPOOL_EXTREME_REWIND;
3649168404Spjd
3650219089Spjd	/* In future, further rewind policy choices can be passed along here */
3651219089Spjd	if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3652219089Spjd	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3653168404Spjd		return (1);
3654168404Spjd
3655219089Spjd	pool = argv[0];
3656219089Spjd	device = argc == 2 ? argv[1] : NULL;
3657219089Spjd
3658219089Spjd	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3659219089Spjd		nvlist_free(policy);
3660219089Spjd		return (1);
3661219089Spjd	}
3662219089Spjd
3663219089Spjd	if (zpool_clear(zhp, device, policy) != 0)
3664168404Spjd		ret = 1;
3665168404Spjd
3666168404Spjd	zpool_close(zhp);
3667168404Spjd
3668219089Spjd	nvlist_free(policy);
3669219089Spjd
3670168404Spjd	return (ret);
3671168404Spjd}
3672168404Spjd
3673228103Smm/*
3674228103Smm * zpool reguid <pool>
3675228103Smm */
3676228103Smmint
3677228103Smmzpool_do_reguid(int argc, char **argv)
3678228103Smm{
3679228103Smm	int c;
3680228103Smm	char *poolname;
3681228103Smm	zpool_handle_t *zhp;
3682228103Smm	int ret = 0;
3683228103Smm
3684228103Smm	/* check options */
3685228103Smm	while ((c = getopt(argc, argv, "")) != -1) {
3686228103Smm		switch (c) {
3687228103Smm		case '?':
3688228103Smm			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3689228103Smm			    optopt);
3690228103Smm			usage(B_FALSE);
3691228103Smm		}
3692228103Smm	}
3693228103Smm
3694228103Smm	argc -= optind;
3695228103Smm	argv += optind;
3696228103Smm
3697228103Smm	/* get pool name and check number of arguments */
3698228103Smm	if (argc < 1) {
3699228103Smm		(void) fprintf(stderr, gettext("missing pool name\n"));
3700228103Smm		usage(B_FALSE);
3701228103Smm	}
3702228103Smm
3703228103Smm	if (argc > 1) {
3704228103Smm		(void) fprintf(stderr, gettext("too many arguments\n"));
3705228103Smm		usage(B_FALSE);
3706228103Smm	}
3707228103Smm
3708228103Smm	poolname = argv[0];
3709228103Smm	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3710228103Smm		return (1);
3711228103Smm
3712228103Smm	ret = zpool_reguid(zhp);
3713228103Smm
3714228103Smm	zpool_close(zhp);
3715228103Smm	return (ret);
3716228103Smm}
3717228103Smm
3718228103Smm
3719236155Smm/*
3720236155Smm * zpool reopen <pool>
3721236155Smm *
3722236155Smm * Reopen the pool so that the kernel can update the sizes of all vdevs.
3723236155Smm *
3724236155Smm * NOTE: This command is currently undocumented.  If the command is ever
3725236155Smm * exposed then the appropriate usage() messages will need to be made.
3726236155Smm */
3727236155Smmint
3728236155Smmzpool_do_reopen(int argc, char **argv)
3729236155Smm{
3730236155Smm	int ret = 0;
3731236155Smm	zpool_handle_t *zhp;
3732236155Smm	char *pool;
3733236155Smm
3734236155Smm	argc--;
3735236155Smm	argv++;
3736236155Smm
3737236155Smm	if (argc != 1)
3738236155Smm		return (2);
3739236155Smm
3740236155Smm	pool = argv[0];
3741236155Smm	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3742236155Smm		return (1);
3743236155Smm
3744236155Smm	ret = zpool_reopen(zhp);
3745236155Smm	zpool_close(zhp);
3746236155Smm	return (ret);
3747236155Smm}
3748236155Smm
3749168404Spjdtypedef struct scrub_cbdata {
3750168404Spjd	int	cb_type;
3751168404Spjd	int	cb_argc;
3752168404Spjd	char	**cb_argv;
3753168404Spjd} scrub_cbdata_t;
3754168404Spjd
3755168404Spjdint
3756168404Spjdscrub_callback(zpool_handle_t *zhp, void *data)
3757168404Spjd{
3758168404Spjd	scrub_cbdata_t *cb = data;
3759168404Spjd	int err;
3760168404Spjd
3761168404Spjd	/*
3762168404Spjd	 * Ignore faulted pools.
3763168404Spjd	 */
3764168404Spjd	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3765168404Spjd		(void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3766168404Spjd		    "currently unavailable\n"), zpool_get_name(zhp));
3767168404Spjd		return (1);
3768168404Spjd	}
3769168404Spjd
3770219089Spjd	err = zpool_scan(zhp, cb->cb_type);
3771168404Spjd
3772168404Spjd	return (err != 0);
3773168404Spjd}
3774168404Spjd
3775168404Spjd/*
3776168404Spjd * zpool scrub [-s] <pool> ...
3777168404Spjd *
3778168404Spjd *	-s	Stop.  Stops any in-progress scrub.
3779168404Spjd */
3780168404Spjdint
3781168404Spjdzpool_do_scrub(int argc, char **argv)
3782168404Spjd{
3783168404Spjd	int c;
3784168404Spjd	scrub_cbdata_t cb;
3785168404Spjd
3786219089Spjd	cb.cb_type = POOL_SCAN_SCRUB;
3787168404Spjd
3788168404Spjd	/* check options */
3789168404Spjd	while ((c = getopt(argc, argv, "s")) != -1) {
3790168404Spjd		switch (c) {
3791168404Spjd		case 's':
3792219089Spjd			cb.cb_type = POOL_SCAN_NONE;
3793168404Spjd			break;
3794168404Spjd		case '?':
3795168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3796168404Spjd			    optopt);
3797168404Spjd			usage(B_FALSE);
3798168404Spjd		}
3799168404Spjd	}
3800168404Spjd
3801168404Spjd	cb.cb_argc = argc;
3802168404Spjd	cb.cb_argv = argv;
3803168404Spjd	argc -= optind;
3804168404Spjd	argv += optind;
3805168404Spjd
3806168404Spjd	if (argc < 1) {
3807168404Spjd		(void) fprintf(stderr, gettext("missing pool name argument\n"));
3808168404Spjd		usage(B_FALSE);
3809168404Spjd	}
3810168404Spjd
3811168404Spjd	return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3812168404Spjd}
3813168404Spjd
3814168404Spjdtypedef struct status_cbdata {
3815168404Spjd	int		cb_count;
3816168404Spjd	boolean_t	cb_allpools;
3817168404Spjd	boolean_t	cb_verbose;
3818168404Spjd	boolean_t	cb_explain;
3819168404Spjd	boolean_t	cb_first;
3820219089Spjd	boolean_t	cb_dedup_stats;
3821168404Spjd} status_cbdata_t;
3822168404Spjd
3823168404Spjd/*
3824168404Spjd * Print out detailed scrub status.
3825168404Spjd */
3826168404Spjdvoid
3827219089Spjdprint_scan_status(pool_scan_stat_t *ps)
3828168404Spjd{
3829219089Spjd	time_t start, end;
3830219089Spjd	uint64_t elapsed, mins_left, hours_left;
3831219089Spjd	uint64_t pass_exam, examined, total;
3832219089Spjd	uint_t rate;
3833168404Spjd	double fraction_done;
3834219089Spjd	char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3835168404Spjd
3836226583Spjd	(void) printf(gettext("  scan: "));
3837168404Spjd
3838219089Spjd	/* If there's never been a scan, there's not much to say. */
3839219089Spjd	if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3840219089Spjd	    ps->pss_func >= POOL_SCAN_FUNCS) {
3841168404Spjd		(void) printf(gettext("none requested\n"));
3842168404Spjd		return;
3843168404Spjd	}
3844168404Spjd
3845219089Spjd	start = ps->pss_start_time;
3846219089Spjd	end = ps->pss_end_time;
3847219089Spjd	zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3848168404Spjd
3849219089Spjd	assert(ps->pss_func == POOL_SCAN_SCRUB ||
3850219089Spjd	    ps->pss_func == POOL_SCAN_RESILVER);
3851219089Spjd	/*
3852219089Spjd	 * Scan is finished or canceled.
3853219089Spjd	 */
3854219089Spjd	if (ps->pss_state == DSS_FINISHED) {
3855219089Spjd		uint64_t minutes_taken = (end - start) / 60;
3856219089Spjd		char *fmt;
3857168404Spjd
3858219089Spjd		if (ps->pss_func == POOL_SCAN_SCRUB) {
3859219089Spjd			fmt = gettext("scrub repaired %s in %lluh%um with "
3860219089Spjd			    "%llu errors on %s");
3861219089Spjd		} else if (ps->pss_func == POOL_SCAN_RESILVER) {
3862219089Spjd			fmt = gettext("resilvered %s in %lluh%um with "
3863219089Spjd			    "%llu errors on %s");
3864219089Spjd		}
3865219089Spjd		/* LINTED */
3866219089Spjd		(void) printf(fmt, processed_buf,
3867185029Spjd		    (u_longlong_t)(minutes_taken / 60),
3868185029Spjd		    (uint_t)(minutes_taken % 60),
3869219089Spjd		    (u_longlong_t)ps->pss_errors,
3870219089Spjd		    ctime((time_t *)&end));
3871168404Spjd		return;
3872219089Spjd	} else if (ps->pss_state == DSS_CANCELED) {
3873219089Spjd		if (ps->pss_func == POOL_SCAN_SCRUB) {
3874219089Spjd			(void) printf(gettext("scrub canceled on %s"),
3875219089Spjd			    ctime(&end));
3876219089Spjd		} else if (ps->pss_func == POOL_SCAN_RESILVER) {
3877219089Spjd			(void) printf(gettext("resilver canceled on %s"),
3878219089Spjd			    ctime(&end));
3879219089Spjd		}
3880219089Spjd		return;
3881168404Spjd	}
3882168404Spjd
3883219089Spjd	assert(ps->pss_state == DSS_SCANNING);
3884168404Spjd
3885219089Spjd	/*
3886219089Spjd	 * Scan is in progress.
3887219089Spjd	 */
3888219089Spjd	if (ps->pss_func == POOL_SCAN_SCRUB) {
3889219089Spjd		(void) printf(gettext("scrub in progress since %s"),
3890219089Spjd		    ctime(&start));
3891219089Spjd	} else if (ps->pss_func == POOL_SCAN_RESILVER) {
3892219089Spjd		(void) printf(gettext("resilver in progress since %s"),
3893219089Spjd		    ctime(&start));
3894219089Spjd	}
3895219089Spjd
3896219089Spjd	examined = ps->pss_examined ? ps->pss_examined : 1;
3897219089Spjd	total = ps->pss_to_examine;
3898168404Spjd	fraction_done = (double)examined / total;
3899168404Spjd
3900219089Spjd	/* elapsed time for this pass */
3901219089Spjd	elapsed = time(NULL) - ps->pss_pass_start;
3902219089Spjd	elapsed = elapsed ? elapsed : 1;
3903219089Spjd	pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3904219089Spjd	rate = pass_exam / elapsed;
3905219089Spjd	rate = rate ? rate : 1;
3906219089Spjd	mins_left = ((total - examined) / rate) / 60;
3907219089Spjd	hours_left = mins_left / 60;
3908219089Spjd
3909219089Spjd	zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3910219089Spjd	zfs_nicenum(total, total_buf, sizeof (total_buf));
3911219089Spjd	zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3912219089Spjd
3913219089Spjd	/*
3914219089Spjd	 * do not print estimated time if hours_left is more than 30 days
3915219089Spjd	 */
3916226583Spjd	(void) printf(gettext("        %s scanned out of %s at %s/s"),
3917219089Spjd	    examined_buf, total_buf, rate_buf);
3918219089Spjd	if (hours_left < (30 * 24)) {
3919219089Spjd		(void) printf(gettext(", %lluh%um to go\n"),
3920219089Spjd		    (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3921219089Spjd	} else {
3922219089Spjd		(void) printf(gettext(
3923219089Spjd		    ", (scan is slow, no estimated time)\n"));
3924219089Spjd	}
3925219089Spjd
3926219089Spjd	if (ps->pss_func == POOL_SCAN_RESILVER) {
3927226583Spjd		(void) printf(gettext("        %s resilvered, %.2f%% done\n"),
3928219089Spjd		    processed_buf, 100 * fraction_done);
3929219089Spjd	} else if (ps->pss_func == POOL_SCAN_SCRUB) {
3930226583Spjd		(void) printf(gettext("        %s repaired, %.2f%% done\n"),
3931219089Spjd		    processed_buf, 100 * fraction_done);
3932219089Spjd	}
3933168404Spjd}
3934168404Spjd
3935168404Spjdstatic void
3936168404Spjdprint_error_log(zpool_handle_t *zhp)
3937168404Spjd{
3938185029Spjd	nvlist_t *nverrlist = NULL;
3939168404Spjd	nvpair_t *elem;
3940168404Spjd	char *pathname;
3941168404Spjd	size_t len = MAXPATHLEN * 2;
3942168404Spjd
3943168404Spjd	if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3944168404Spjd		(void) printf("errors: List of errors unavailable "
3945168404Spjd		    "(insufficient privileges)\n");
3946168404Spjd		return;
3947168404Spjd	}
3948168404Spjd
3949168404Spjd	(void) printf("errors: Permanent errors have been "
3950168404Spjd	    "detected in the following files:\n\n");
3951168404Spjd
3952168404Spjd	pathname = safe_malloc(len);
3953168404Spjd	elem = NULL;
3954168404Spjd	while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
3955168404Spjd		nvlist_t *nv;
3956168404Spjd		uint64_t dsobj, obj;
3957168404Spjd
3958168404Spjd		verify(nvpair_value_nvlist(elem, &nv) == 0);
3959168404Spjd		verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
3960168404Spjd		    &dsobj) == 0);
3961168404Spjd		verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
3962168404Spjd		    &obj) == 0);
3963168404Spjd		zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
3964168404Spjd		(void) printf("%7s %s\n", "", pathname);
3965168404Spjd	}
3966168404Spjd	free(pathname);
3967168404Spjd	nvlist_free(nverrlist);
3968168404Spjd}
3969168404Spjd
3970168404Spjdstatic void
3971168404Spjdprint_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
3972168404Spjd    int namewidth)
3973168404Spjd{
3974168404Spjd	uint_t i;
3975168404Spjd	char *name;
3976168404Spjd
3977168404Spjd	if (nspares == 0)
3978168404Spjd		return;
3979168404Spjd
3980168404Spjd	(void) printf(gettext("\tspares\n"));
3981168404Spjd
3982168404Spjd	for (i = 0; i < nspares; i++) {
3983219089Spjd		name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
3984168404Spjd		print_status_config(zhp, name, spares[i],
3985209962Smm		    namewidth, 2, B_TRUE);
3986168404Spjd		free(name);
3987168404Spjd	}
3988168404Spjd}
3989168404Spjd
3990185029Spjdstatic void
3991185029Spjdprint_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3992185029Spjd    int namewidth)
3993185029Spjd{
3994185029Spjd	uint_t i;
3995185029Spjd	char *name;
3996185029Spjd
3997185029Spjd	if (nl2cache == 0)
3998185029Spjd		return;
3999185029Spjd
4000185029Spjd	(void) printf(gettext("\tcache\n"));
4001185029Spjd
4002185029Spjd	for (i = 0; i < nl2cache; i++) {
4003219089Spjd		name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
4004185029Spjd		print_status_config(zhp, name, l2cache[i],
4005209962Smm		    namewidth, 2, B_FALSE);
4006185029Spjd		free(name);
4007185029Spjd	}
4008185029Spjd}
4009185029Spjd
4010219089Spjdstatic void
4011219089Spjdprint_dedup_stats(nvlist_t *config)
4012219089Spjd{
4013219089Spjd	ddt_histogram_t *ddh;
4014219089Spjd	ddt_stat_t *dds;
4015219089Spjd	ddt_object_t *ddo;
4016219089Spjd	uint_t c;
4017219089Spjd
4018219089Spjd	/*
4019219089Spjd	 * If the pool was faulted then we may not have been able to
4020253441Sdelphij	 * obtain the config. Otherwise, if we have anything in the dedup
4021219089Spjd	 * table continue processing the stats.
4022219089Spjd	 */
4023219089Spjd	if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
4024227497Smm	    (uint64_t **)&ddo, &c) != 0)
4025219089Spjd		return;
4026219089Spjd
4027219089Spjd	(void) printf("\n");
4028227497Smm	(void) printf(gettext(" dedup: "));
4029227497Smm	if (ddo->ddo_count == 0) {
4030227497Smm		(void) printf(gettext("no DDT entries\n"));
4031227497Smm		return;
4032227497Smm	}
4033227497Smm
4034219089Spjd	(void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4035219089Spjd	    (u_longlong_t)ddo->ddo_count,
4036219089Spjd	    (u_longlong_t)ddo->ddo_dspace,
4037219089Spjd	    (u_longlong_t)ddo->ddo_mspace);
4038219089Spjd
4039219089Spjd	verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
4040219089Spjd	    (uint64_t **)&dds, &c) == 0);
4041219089Spjd	verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
4042219089Spjd	    (uint64_t **)&ddh, &c) == 0);
4043219089Spjd	zpool_dump_ddt(dds, ddh);
4044219089Spjd}
4045219089Spjd
4046168404Spjd/*
4047168404Spjd * Display a summary of pool status.  Displays a summary such as:
4048168404Spjd *
4049168404Spjd *        pool: tank
4050168404Spjd *	status: DEGRADED
4051168404Spjd *	reason: One or more devices ...
4052236146Smm *         see: http://illumos.org/msg/ZFS-xxxx-01
4053168404Spjd *	config:
4054168404Spjd *		mirror		DEGRADED
4055168404Spjd *                c1t0d0	OK
4056168404Spjd *                c2t0d0	UNAVAIL
4057168404Spjd *
4058168404Spjd * When given the '-v' option, we print out the complete config.  If the '-e'
4059168404Spjd * option is specified, then we print out error rate information as well.
4060168404Spjd */
4061168404Spjdint
4062168404Spjdstatus_callback(zpool_handle_t *zhp, void *data)
4063168404Spjd{
4064168404Spjd	status_cbdata_t *cbp = data;
4065168404Spjd	nvlist_t *config, *nvroot;
4066168404Spjd	char *msgid;
4067168404Spjd	int reason;
4068168404Spjd	const char *health;
4069168404Spjd	uint_t c;
4070168404Spjd	vdev_stat_t *vs;
4071168404Spjd
4072168404Spjd	config = zpool_get_config(zhp, NULL);
4073168404Spjd	reason = zpool_get_status(zhp, &msgid);
4074168404Spjd
4075168404Spjd	cbp->cb_count++;
4076168404Spjd
4077168404Spjd	/*
4078168404Spjd	 * If we were given 'zpool status -x', only report those pools with
4079168404Spjd	 * problems.
4080168404Spjd	 */
4081248267Smm	if (cbp->cb_explain &&
4082248267Smm	    (reason == ZPOOL_STATUS_OK ||
4083248267Smm	    reason == ZPOOL_STATUS_VERSION_OLDER ||
4084248267Smm	    reason == ZPOOL_STATUS_FEAT_DISABLED)) {
4085168404Spjd		if (!cbp->cb_allpools) {
4086168404Spjd			(void) printf(gettext("pool '%s' is healthy\n"),
4087168404Spjd			    zpool_get_name(zhp));
4088168404Spjd			if (cbp->cb_first)
4089168404Spjd				cbp->cb_first = B_FALSE;
4090168404Spjd		}
4091168404Spjd		return (0);
4092168404Spjd	}
4093168404Spjd
4094168404Spjd	if (cbp->cb_first)
4095168404Spjd		cbp->cb_first = B_FALSE;
4096168404Spjd	else
4097168404Spjd		(void) printf("\n");
4098168404Spjd
4099168404Spjd	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4100168404Spjd	    &nvroot) == 0);
4101219089Spjd	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
4102168404Spjd	    (uint64_t **)&vs, &c) == 0);
4103185029Spjd	health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4104168404Spjd
4105168404Spjd	(void) printf(gettext("  pool: %s\n"), zpool_get_name(zhp));
4106168404Spjd	(void) printf(gettext(" state: %s\n"), health);
4107168404Spjd
4108168404Spjd	switch (reason) {
4109168404Spjd	case ZPOOL_STATUS_MISSING_DEV_R:
4110168404Spjd		(void) printf(gettext("status: One or more devices could not "
4111168404Spjd		    "be opened.  Sufficient replicas exist for\n\tthe pool to "
4112168404Spjd		    "continue functioning in a degraded state.\n"));
4113168404Spjd		(void) printf(gettext("action: Attach the missing device and "
4114168404Spjd		    "online it using 'zpool online'.\n"));
4115168404Spjd		break;
4116168404Spjd
4117168404Spjd	case ZPOOL_STATUS_MISSING_DEV_NR:
4118168404Spjd		(void) printf(gettext("status: One or more devices could not "
4119168404Spjd		    "be opened.  There are insufficient\n\treplicas for the "
4120168404Spjd		    "pool to continue functioning.\n"));
4121168404Spjd		(void) printf(gettext("action: Attach the missing device and "
4122168404Spjd		    "online it using 'zpool online'.\n"));
4123168404Spjd		break;
4124168404Spjd
4125168404Spjd	case ZPOOL_STATUS_CORRUPT_LABEL_R:
4126168404Spjd		(void) printf(gettext("status: One or more devices could not "
4127168404Spjd		    "be used because the label is missing or\n\tinvalid.  "
4128168404Spjd		    "Sufficient replicas exist for the pool to continue\n\t"
4129168404Spjd		    "functioning in a degraded state.\n"));
4130168404Spjd		(void) printf(gettext("action: Replace the device using "
4131168404Spjd		    "'zpool replace'.\n"));
4132168404Spjd		break;
4133168404Spjd
4134168404Spjd	case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4135168404Spjd		(void) printf(gettext("status: One or more devices could not "
4136168404Spjd		    "be used because the label is missing \n\tor invalid.  "
4137168404Spjd		    "There are insufficient replicas for the pool to "
4138168404Spjd		    "continue\n\tfunctioning.\n"));
4139219089Spjd		zpool_explain_recover(zpool_get_handle(zhp),
4140219089Spjd		    zpool_get_name(zhp), reason, config);
4141168404Spjd		break;
4142168404Spjd
4143168404Spjd	case ZPOOL_STATUS_FAILING_DEV:
4144168404Spjd		(void) printf(gettext("status: One or more devices has "
4145168404Spjd		    "experienced an unrecoverable error.  An\n\tattempt was "
4146168404Spjd		    "made to correct the error.  Applications are "
4147168404Spjd		    "unaffected.\n"));
4148168404Spjd		(void) printf(gettext("action: Determine if the device needs "
4149168404Spjd		    "to be replaced, and clear the errors\n\tusing "
4150168404Spjd		    "'zpool clear' or replace the device with 'zpool "
4151168404Spjd		    "replace'.\n"));
4152168404Spjd		break;
4153168404Spjd
4154168404Spjd	case ZPOOL_STATUS_OFFLINE_DEV:
4155168404Spjd		(void) printf(gettext("status: One or more devices has "
4156168404Spjd		    "been taken offline by the administrator.\n\tSufficient "
4157168404Spjd		    "replicas exist for the pool to continue functioning in "
4158168404Spjd		    "a\n\tdegraded state.\n"));
4159168404Spjd		(void) printf(gettext("action: Online the device using "
4160168404Spjd		    "'zpool online' or replace the device with\n\t'zpool "
4161168404Spjd		    "replace'.\n"));
4162168404Spjd		break;
4163168404Spjd
4164219089Spjd	case ZPOOL_STATUS_REMOVED_DEV:
4165219089Spjd		(void) printf(gettext("status: One or more devices has "
4166219089Spjd		    "been removed by the administrator.\n\tSufficient "
4167219089Spjd		    "replicas exist for the pool to continue functioning in "
4168219089Spjd		    "a\n\tdegraded state.\n"));
4169219089Spjd		(void) printf(gettext("action: Online the device using "
4170219089Spjd		    "'zpool online' or replace the device with\n\t'zpool "
4171219089Spjd		    "replace'.\n"));
4172219089Spjd		break;
4173219089Spjd
4174168404Spjd	case ZPOOL_STATUS_RESILVERING:
4175168404Spjd		(void) printf(gettext("status: One or more devices is "
4176168404Spjd		    "currently being resilvered.  The pool will\n\tcontinue "
4177168404Spjd		    "to function, possibly in a degraded state.\n"));
4178168404Spjd		(void) printf(gettext("action: Wait for the resilver to "
4179168404Spjd		    "complete.\n"));
4180168404Spjd		break;
4181168404Spjd
4182168404Spjd	case ZPOOL_STATUS_CORRUPT_DATA:
4183168404Spjd		(void) printf(gettext("status: One or more devices has "
4184168404Spjd		    "experienced an error resulting in data\n\tcorruption.  "
4185168404Spjd		    "Applications may be affected.\n"));
4186168404Spjd		(void) printf(gettext("action: Restore the file in question "
4187168404Spjd		    "if possible.  Otherwise restore the\n\tentire pool from "
4188168404Spjd		    "backup.\n"));
4189168404Spjd		break;
4190168404Spjd
4191168404Spjd	case ZPOOL_STATUS_CORRUPT_POOL:
4192168404Spjd		(void) printf(gettext("status: The pool metadata is corrupted "
4193168404Spjd		    "and the pool cannot be opened.\n"));
4194219089Spjd		zpool_explain_recover(zpool_get_handle(zhp),
4195219089Spjd		    zpool_get_name(zhp), reason, config);
4196168404Spjd		break;
4197168404Spjd
4198168404Spjd	case ZPOOL_STATUS_VERSION_OLDER:
4199238926Smm		(void) printf(gettext("status: The pool is formatted using a "
4200238926Smm		    "legacy on-disk format.  The pool can\n\tstill be used, "
4201238926Smm		    "but some features are unavailable.\n"));
4202168404Spjd		(void) printf(gettext("action: Upgrade the pool using 'zpool "
4203168404Spjd		    "upgrade'.  Once this is done, the\n\tpool will no longer "
4204238926Smm		    "be accessible on software that does not support feature\n"
4205238926Smm		    "\tflags.\n"));
4206168404Spjd		break;
4207168404Spjd
4208168404Spjd	case ZPOOL_STATUS_VERSION_NEWER:
4209168404Spjd		(void) printf(gettext("status: The pool has been upgraded to a "
4210168404Spjd		    "newer, incompatible on-disk version.\n\tThe pool cannot "
4211168404Spjd		    "be accessed on this system.\n"));
4212168404Spjd		(void) printf(gettext("action: Access the pool from a system "
4213168404Spjd		    "running more recent software, or\n\trestore the pool from "
4214168404Spjd		    "backup.\n"));
4215168404Spjd		break;
4216168404Spjd
4217238926Smm	case ZPOOL_STATUS_FEAT_DISABLED:
4218238926Smm		(void) printf(gettext("status: Some supported features are not "
4219238926Smm		    "enabled on the pool. The pool can\n\tstill be used, but "
4220238926Smm		    "some features are unavailable.\n"));
4221238926Smm		(void) printf(gettext("action: Enable all features using "
4222238926Smm		    "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4223238926Smm		    "longer be accessible by software that does not support\n\t"
4224243014Smm		    "the features. See zpool-features(7) for details.\n"));
4225238926Smm		break;
4226238926Smm
4227236884Smm	case ZPOOL_STATUS_UNSUP_FEAT_READ:
4228236884Smm		(void) printf(gettext("status: The pool cannot be accessed on "
4229236884Smm		    "this system because it uses the\n\tfollowing feature(s) "
4230236884Smm		    "not supported on this system:\n"));
4231236884Smm		zpool_print_unsup_feat(config);
4232236884Smm		(void) printf("\n");
4233236884Smm		(void) printf(gettext("action: Access the pool from a system "
4234236884Smm		    "that supports the required feature(s),\n\tor restore the "
4235236884Smm		    "pool from backup.\n"));
4236236884Smm		break;
4237236884Smm
4238236884Smm	case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4239236884Smm		(void) printf(gettext("status: The pool can only be accessed "
4240236884Smm		    "in read-only mode on this system. It\n\tcannot be "
4241236884Smm		    "accessed in read-write mode because it uses the "
4242236884Smm		    "following\n\tfeature(s) not supported on this system:\n"));
4243236884Smm		zpool_print_unsup_feat(config);
4244236884Smm		(void) printf("\n");
4245236884Smm		(void) printf(gettext("action: The pool cannot be accessed in "
4246236884Smm		    "read-write mode. Import the pool with\n"
4247236884Smm		    "\t\"-o readonly=on\", access the pool from a system that "
4248236884Smm		    "supports the\n\trequired feature(s), or restore the "
4249236884Smm		    "pool from backup.\n"));
4250236884Smm		break;
4251236884Smm
4252185029Spjd	case ZPOOL_STATUS_FAULTED_DEV_R:
4253185029Spjd		(void) printf(gettext("status: One or more devices are "
4254185029Spjd		    "faulted in response to persistent errors.\n\tSufficient "
4255185029Spjd		    "replicas exist for the pool to continue functioning "
4256185029Spjd		    "in a\n\tdegraded state.\n"));
4257185029Spjd		(void) printf(gettext("action: Replace the faulted device, "
4258185029Spjd		    "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4259185029Spjd		break;
4260185029Spjd
4261185029Spjd	case ZPOOL_STATUS_FAULTED_DEV_NR:
4262185029Spjd		(void) printf(gettext("status: One or more devices are "
4263185029Spjd		    "faulted in response to persistent errors.  There are "
4264185029Spjd		    "insufficient replicas for the pool to\n\tcontinue "
4265185029Spjd		    "functioning.\n"));
4266185029Spjd		(void) printf(gettext("action: Destroy and re-create the pool "
4267185029Spjd		    "from a backup source.  Manually marking the device\n"
4268185029Spjd		    "\trepaired using 'zpool clear' may allow some data "
4269185029Spjd		    "to be recovered.\n"));
4270185029Spjd		break;
4271185029Spjd
4272185029Spjd	case ZPOOL_STATUS_IO_FAILURE_WAIT:
4273185029Spjd	case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4274185029Spjd		(void) printf(gettext("status: One or more devices are "
4275185029Spjd		    "faulted in response to IO failures.\n"));
4276185029Spjd		(void) printf(gettext("action: Make sure the affected devices "
4277185029Spjd		    "are connected, then run 'zpool clear'.\n"));
4278185029Spjd		break;
4279185029Spjd
4280185029Spjd	case ZPOOL_STATUS_BAD_LOG:
4281185029Spjd		(void) printf(gettext("status: An intent log record "
4282185029Spjd		    "could not be read.\n"
4283185029Spjd		    "\tWaiting for adminstrator intervention to fix the "
4284185029Spjd		    "faulted pool.\n"));
4285185029Spjd		(void) printf(gettext("action: Either restore the affected "
4286185029Spjd		    "device(s) and run 'zpool online',\n"
4287185029Spjd		    "\tor ignore the intent log records by running "
4288185029Spjd		    "'zpool clear'.\n"));
4289185029Spjd		break;
4290185029Spjd
4291254591Sgibbs	case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
4292254591Sgibbs		(void) printf(gettext("status: One or more devices are "
4293254591Sgibbs		    "configured to use a non-native block size.\n"
4294254591Sgibbs		    "\tExpect reduced performance.\n"));
4295254591Sgibbs		(void) printf(gettext("action: Replace affected devices with "
4296254591Sgibbs		    "devices that support the\n\tconfigured block size, or "
4297254591Sgibbs		    "migrate data to a properly configured\n\tpool.\n"));
4298254591Sgibbs		break;
4299254591Sgibbs
4300168404Spjd	default:
4301168404Spjd		/*
4302168404Spjd		 * The remaining errors can't actually be generated, yet.
4303168404Spjd		 */
4304168404Spjd		assert(reason == ZPOOL_STATUS_OK);
4305168404Spjd	}
4306168404Spjd
4307168404Spjd	if (msgid != NULL)
4308236146Smm		(void) printf(gettext("   see: http://illumos.org/msg/%s\n"),
4309168404Spjd		    msgid);
4310168404Spjd
4311168404Spjd	if (config != NULL) {
4312168404Spjd		int namewidth;
4313168404Spjd		uint64_t nerr;
4314185029Spjd		nvlist_t **spares, **l2cache;
4315185029Spjd		uint_t nspares, nl2cache;
4316219089Spjd		pool_scan_stat_t *ps = NULL;
4317168404Spjd
4318219089Spjd		(void) nvlist_lookup_uint64_array(nvroot,
4319219089Spjd		    ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4320219089Spjd		print_scan_status(ps);
4321168404Spjd
4322168404Spjd		namewidth = max_width(zhp, nvroot, 0, 0);
4323168404Spjd		if (namewidth < 10)
4324168404Spjd			namewidth = 10;
4325168404Spjd
4326168404Spjd		(void) printf(gettext("config:\n\n"));
4327168404Spjd		(void) printf(gettext("\t%-*s  %-8s %5s %5s %5s\n"), namewidth,
4328168404Spjd		    "NAME", "STATE", "READ", "WRITE", "CKSUM");
4329168404Spjd		print_status_config(zhp, zpool_get_name(zhp), nvroot,
4330209962Smm		    namewidth, 0, B_FALSE);
4331209962Smm
4332185029Spjd		if (num_logs(nvroot) > 0)
4333213197Smm			print_logs(zhp, nvroot, namewidth, B_TRUE);
4334185029Spjd		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4335185029Spjd		    &l2cache, &nl2cache) == 0)
4336185029Spjd			print_l2cache(zhp, l2cache, nl2cache, namewidth);
4337185029Spjd
4338168404Spjd		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4339168404Spjd		    &spares, &nspares) == 0)
4340168404Spjd			print_spares(zhp, spares, nspares, namewidth);
4341168404Spjd
4342168404Spjd		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4343168404Spjd		    &nerr) == 0) {
4344168404Spjd			nvlist_t *nverrlist = NULL;
4345168404Spjd
4346168404Spjd			/*
4347168404Spjd			 * If the approximate error count is small, get a
4348168404Spjd			 * precise count by fetching the entire log and
4349168404Spjd			 * uniquifying the results.
4350168404Spjd			 */
4351185029Spjd			if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4352168404Spjd			    zpool_get_errlog(zhp, &nverrlist) == 0) {
4353168404Spjd				nvpair_t *elem;
4354168404Spjd
4355168404Spjd				elem = NULL;
4356168404Spjd				nerr = 0;
4357168404Spjd				while ((elem = nvlist_next_nvpair(nverrlist,
4358168404Spjd				    elem)) != NULL) {
4359168404Spjd					nerr++;
4360168404Spjd				}
4361168404Spjd			}
4362168404Spjd			nvlist_free(nverrlist);
4363168404Spjd
4364168404Spjd			(void) printf("\n");
4365168404Spjd
4366168404Spjd			if (nerr == 0)
4367168404Spjd				(void) printf(gettext("errors: No known data "
4368168404Spjd				    "errors\n"));
4369168404Spjd			else if (!cbp->cb_verbose)
4370168404Spjd				(void) printf(gettext("errors: %llu data "
4371168404Spjd				    "errors, use '-v' for a list\n"),
4372168404Spjd				    (u_longlong_t)nerr);
4373168404Spjd			else
4374168404Spjd				print_error_log(zhp);
4375168404Spjd		}
4376219089Spjd
4377219089Spjd		if (cbp->cb_dedup_stats)
4378219089Spjd			print_dedup_stats(config);
4379168404Spjd	} else {
4380168404Spjd		(void) printf(gettext("config: The configuration cannot be "
4381168404Spjd		    "determined.\n"));
4382168404Spjd	}
4383168404Spjd
4384168404Spjd	return (0);
4385168404Spjd}
4386168404Spjd
4387168404Spjd/*
4388219089Spjd * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4389168404Spjd *
4390168404Spjd *	-v	Display complete error logs
4391168404Spjd *	-x	Display only pools with potential problems
4392219089Spjd *	-D	Display dedup status (undocumented)
4393219089Spjd *	-T	Display a timestamp in date(1) or Unix format
4394168404Spjd *
4395168404Spjd * Describes the health status of all pools or some subset.
4396168404Spjd */
4397168404Spjdint
4398168404Spjdzpool_do_status(int argc, char **argv)
4399168404Spjd{
4400168404Spjd	int c;
4401168404Spjd	int ret;
4402219089Spjd	unsigned long interval = 0, count = 0;
4403168404Spjd	status_cbdata_t cb = { 0 };
4404168404Spjd
4405168404Spjd	/* check options */
4406219089Spjd	while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4407168404Spjd		switch (c) {
4408168404Spjd		case 'v':
4409168404Spjd			cb.cb_verbose = B_TRUE;
4410168404Spjd			break;
4411168404Spjd		case 'x':
4412168404Spjd			cb.cb_explain = B_TRUE;
4413168404Spjd			break;
4414219089Spjd		case 'D':
4415219089Spjd			cb.cb_dedup_stats = B_TRUE;
4416219089Spjd			break;
4417219089Spjd		case 'T':
4418219089Spjd			get_timestamp_arg(*optarg);
4419219089Spjd			break;
4420168404Spjd		case '?':
4421168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4422168404Spjd			    optopt);
4423168404Spjd			usage(B_FALSE);
4424168404Spjd		}
4425168404Spjd	}
4426168404Spjd
4427168404Spjd	argc -= optind;
4428168404Spjd	argv += optind;
4429168404Spjd
4430219089Spjd	get_interval_count(&argc, argv, &interval, &count);
4431168404Spjd
4432168404Spjd	if (argc == 0)
4433168404Spjd		cb.cb_allpools = B_TRUE;
4434168404Spjd
4435219089Spjd	cb.cb_first = B_TRUE;
4436168404Spjd
4437219089Spjd	for (;;) {
4438219089Spjd		if (timestamp_fmt != NODATE)
4439219089Spjd			print_timestamp(timestamp_fmt);
4440168404Spjd
4441219089Spjd		ret = for_each_pool(argc, argv, B_TRUE, NULL,
4442219089Spjd		    status_callback, &cb);
4443219089Spjd
4444219089Spjd		if (argc == 0 && cb.cb_count == 0)
4445219089Spjd			(void) printf(gettext("no pools available\n"));
4446219089Spjd		else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4447219089Spjd			(void) printf(gettext("all pools are healthy\n"));
4448219089Spjd
4449219089Spjd		if (ret != 0)
4450219089Spjd			return (ret);
4451219089Spjd
4452219089Spjd		if (interval == 0)
4453219089Spjd			break;
4454219089Spjd
4455219089Spjd		if (count != 0 && --count == 0)
4456219089Spjd			break;
4457219089Spjd
4458219089Spjd		(void) sleep(interval);
4459219089Spjd	}
4460219089Spjd
4461219089Spjd	return (0);
4462168404Spjd}
4463168404Spjd
4464168404Spjdtypedef struct upgrade_cbdata {
4465168404Spjd	int	cb_first;
4466212050Spjd	char	cb_poolname[ZPOOL_MAXNAMELEN];
4467168404Spjd	int	cb_argc;
4468185029Spjd	uint64_t cb_version;
4469168404Spjd	char	**cb_argv;
4470168404Spjd} upgrade_cbdata_t;
4471168404Spjd
4472238950Smm#ifdef __FreeBSD__
4473168404Spjdstatic int
4474212050Spjdis_root_pool(zpool_handle_t *zhp)
4475212050Spjd{
4476212050Spjd	static struct statfs sfs;
4477212050Spjd	static char *poolname = NULL;
4478212050Spjd	static boolean_t stated = B_FALSE;
4479212050Spjd	char *slash;
4480212050Spjd
4481212067Spjd	if (!stated) {
4482212050Spjd		stated = B_TRUE;
4483212050Spjd		if (statfs("/", &sfs) == -1) {
4484212050Spjd			(void) fprintf(stderr,
4485212050Spjd			    "Unable to stat root file system: %s.\n",
4486212050Spjd			    strerror(errno));
4487212067Spjd			return (0);
4488212050Spjd		}
4489212050Spjd		if (strcmp(sfs.f_fstypename, "zfs") != 0)
4490212067Spjd			return (0);
4491212050Spjd		poolname = sfs.f_mntfromname;
4492212050Spjd		if ((slash = strchr(poolname, '/')) != NULL)
4493212050Spjd			*slash = '\0';
4494212050Spjd	}
4495212050Spjd	return (poolname != NULL && strcmp(poolname, zpool_get_name(zhp)) == 0);
4496212050Spjd}
4497212050Spjd
4498238950Smmstatic void
4499238950Smmroot_pool_upgrade_check(zpool_handle_t *zhp, char *poolname, int size) {
4500238950Smm
4501238950Smm	if (poolname[0] == '\0' && is_root_pool(zhp))
4502238950Smm		(void) strlcpy(poolname, zpool_get_name(zhp), size);
4503238950Smm}
4504238950Smm#endif	/* FreeBSD */
4505238950Smm
4506212050Spjdstatic int
4507238926Smmupgrade_version(zpool_handle_t *zhp, uint64_t version)
4508238926Smm{
4509238926Smm	int ret;
4510238926Smm	nvlist_t *config;
4511238926Smm	uint64_t oldversion;
4512238926Smm
4513238926Smm	config = zpool_get_config(zhp, NULL);
4514238926Smm	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4515238926Smm	    &oldversion) == 0);
4516238926Smm
4517238926Smm	assert(SPA_VERSION_IS_SUPPORTED(oldversion));
4518238926Smm	assert(oldversion < version);
4519238926Smm
4520238926Smm	ret = zpool_upgrade(zhp, version);
4521238926Smm	if (ret != 0)
4522238926Smm		return (ret);
4523238926Smm
4524238926Smm	if (version >= SPA_VERSION_FEATURES) {
4525238926Smm		(void) printf(gettext("Successfully upgraded "
4526238926Smm		    "'%s' from version %llu to feature flags.\n"),
4527238926Smm		    zpool_get_name(zhp), oldversion);
4528238926Smm	} else {
4529238926Smm		(void) printf(gettext("Successfully upgraded "
4530238926Smm		    "'%s' from version %llu to version %llu.\n"),
4531238926Smm		    zpool_get_name(zhp), oldversion, version);
4532238926Smm	}
4533238926Smm
4534238926Smm	return (0);
4535238926Smm}
4536238926Smm
4537238926Smmstatic int
4538238926Smmupgrade_enable_all(zpool_handle_t *zhp, int *countp)
4539238926Smm{
4540238926Smm	int i, ret, count;
4541238926Smm	boolean_t firstff = B_TRUE;
4542238926Smm	nvlist_t *enabled = zpool_get_features(zhp);
4543238926Smm
4544238926Smm	count = 0;
4545238926Smm	for (i = 0; i < SPA_FEATURES; i++) {
4546238926Smm		const char *fname = spa_feature_table[i].fi_uname;
4547238926Smm		const char *fguid = spa_feature_table[i].fi_guid;
4548238926Smm		if (!nvlist_exists(enabled, fguid)) {
4549238926Smm			char *propname;
4550238926Smm			verify(-1 != asprintf(&propname, "feature@%s", fname));
4551238926Smm			ret = zpool_set_prop(zhp, propname,
4552238926Smm			    ZFS_FEATURE_ENABLED);
4553238926Smm			if (ret != 0) {
4554238926Smm				free(propname);
4555238926Smm				return (ret);
4556238926Smm			}
4557238926Smm			count++;
4558238926Smm
4559238926Smm			if (firstff) {
4560238926Smm				(void) printf(gettext("Enabled the "
4561238926Smm				    "following features on '%s':\n"),
4562238926Smm				    zpool_get_name(zhp));
4563238926Smm				firstff = B_FALSE;
4564238926Smm			}
4565238926Smm			(void) printf(gettext("  %s\n"), fname);
4566238926Smm			free(propname);
4567238926Smm		}
4568238926Smm	}
4569238926Smm
4570238926Smm	if (countp != NULL)
4571238926Smm		*countp = count;
4572238926Smm	return (0);
4573238926Smm}
4574238926Smm
4575238926Smmstatic int
4576168404Spjdupgrade_cb(zpool_handle_t *zhp, void *arg)
4577168404Spjd{
4578168404Spjd	upgrade_cbdata_t *cbp = arg;
4579168404Spjd	nvlist_t *config;
4580168404Spjd	uint64_t version;
4581238926Smm	boolean_t printnl = B_FALSE;
4582238926Smm	int ret;
4583168404Spjd
4584168404Spjd	config = zpool_get_config(zhp, NULL);
4585168404Spjd	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4586168404Spjd	    &version) == 0);
4587168404Spjd
4588238926Smm	assert(SPA_VERSION_IS_SUPPORTED(version));
4589168404Spjd
4590238926Smm	if (version < cbp->cb_version) {
4591238926Smm		cbp->cb_first = B_FALSE;
4592238926Smm		ret = upgrade_version(zhp, cbp->cb_version);
4593238926Smm		if (ret != 0)
4594238926Smm			return (ret);
4595238926Smm#ifdef __FreeBSD__
4596238950Smm		root_pool_upgrade_check(zhp, cbp->cb_poolname,
4597238950Smm		    sizeof(cbp->cb_poolname));
4598238926Smm#endif	/* ___FreeBSD__ */
4599238926Smm		printnl = B_TRUE;
4600238926Smm
4601238926Smm#ifdef illumos
4602238926Smm		/*
4603238926Smm		 * If they did "zpool upgrade -a", then we could
4604238926Smm		 * be doing ioctls to different pools.  We need
4605238926Smm		 * to log this history once to each pool, and bypass
4606238926Smm		 * the normal history logging that happens in main().
4607238926Smm		 */
4608238926Smm		(void) zpool_log_history(g_zfs, history_str);
4609238926Smm		log_history = B_FALSE;
4610238926Smm#endif
4611238926Smm	}
4612238926Smm
4613238926Smm	if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4614238926Smm		int count;
4615238926Smm		ret = upgrade_enable_all(zhp, &count);
4616238926Smm		if (ret != 0)
4617238926Smm			return (ret);
4618238926Smm
4619238926Smm		if (count > 0) {
4620168404Spjd			cbp->cb_first = B_FALSE;
4621238926Smm			printnl = B_TRUE;
4622248571Smm			/*
4623248571Smm			 * If they did "zpool upgrade -a", then we could
4624248571Smm			 * be doing ioctls to different pools.  We need
4625248571Smm			 * to log this history once to each pool, and bypass
4626248571Smm			 * the normal history logging that happens in main().
4627248571Smm			 */
4628248571Smm			(void) zpool_log_history(g_zfs, history_str);
4629248571Smm			log_history = B_FALSE;
4630168404Spjd		}
4631238926Smm	}
4632168404Spjd
4633238926Smm	if (printnl) {
4634238926Smm		(void) printf(gettext("\n"));
4635238926Smm	}
4636238926Smm
4637238926Smm	return (0);
4638238926Smm}
4639238926Smm
4640238926Smmstatic int
4641238926Smmupgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
4642238926Smm{
4643238926Smm	upgrade_cbdata_t *cbp = arg;
4644238926Smm	nvlist_t *config;
4645238926Smm	uint64_t version;
4646238926Smm
4647238926Smm	config = zpool_get_config(zhp, NULL);
4648238926Smm	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4649238926Smm	    &version) == 0);
4650238926Smm
4651238926Smm	assert(SPA_VERSION_IS_SUPPORTED(version));
4652238926Smm
4653238926Smm	if (version < SPA_VERSION_FEATURES) {
4654168404Spjd		if (cbp->cb_first) {
4655168404Spjd			(void) printf(gettext("The following pools are "
4656238926Smm			    "formatted with legacy version numbers and can\n"
4657238926Smm			    "be upgraded to use feature flags.  After "
4658238926Smm			    "being upgraded, these pools\nwill no "
4659238926Smm			    "longer be accessible by software that does not "
4660238926Smm			    "support feature\nflags.\n\n"));
4661168404Spjd			(void) printf(gettext("VER  POOL\n"));
4662168404Spjd			(void) printf(gettext("---  ------------\n"));
4663168404Spjd			cbp->cb_first = B_FALSE;
4664168404Spjd		}
4665168404Spjd
4666168404Spjd		(void) printf("%2llu   %s\n", (u_longlong_t)version,
4667168404Spjd		    zpool_get_name(zhp));
4668168404Spjd	}
4669168404Spjd
4670238926Smm	return (0);
4671168404Spjd}
4672168404Spjd
4673238926Smmstatic int
4674238926Smmupgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
4675238926Smm{
4676238926Smm	upgrade_cbdata_t *cbp = arg;
4677238926Smm	nvlist_t *config;
4678238926Smm	uint64_t version;
4679238926Smm
4680238926Smm	config = zpool_get_config(zhp, NULL);
4681238926Smm	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4682238926Smm	    &version) == 0);
4683238926Smm
4684238926Smm	if (version >= SPA_VERSION_FEATURES) {
4685238926Smm		int i;
4686238926Smm		boolean_t poolfirst = B_TRUE;
4687238926Smm		nvlist_t *enabled = zpool_get_features(zhp);
4688238926Smm
4689238926Smm		for (i = 0; i < SPA_FEATURES; i++) {
4690238926Smm			const char *fguid = spa_feature_table[i].fi_guid;
4691238926Smm			const char *fname = spa_feature_table[i].fi_uname;
4692238926Smm			if (!nvlist_exists(enabled, fguid)) {
4693238926Smm				if (cbp->cb_first) {
4694238926Smm					(void) printf(gettext("\nSome "
4695238926Smm					    "supported features are not "
4696238926Smm					    "enabled on the following pools. "
4697238926Smm					    "Once a\nfeature is enabled the "
4698238926Smm					    "pool may become incompatible with "
4699238926Smm					    "software\nthat does not support "
4700238926Smm					    "the feature. See "
4701243014Smm					    "zpool-features(7) for "
4702238926Smm					    "details.\n\n"));
4703238926Smm					(void) printf(gettext("POOL  "
4704238926Smm					    "FEATURE\n"));
4705238926Smm					(void) printf(gettext("------"
4706238926Smm					    "---------\n"));
4707238926Smm					cbp->cb_first = B_FALSE;
4708238926Smm				}
4709238926Smm
4710238926Smm				if (poolfirst) {
4711238926Smm					(void) printf(gettext("%s\n"),
4712238926Smm					    zpool_get_name(zhp));
4713238926Smm					poolfirst = B_FALSE;
4714238926Smm				}
4715238926Smm
4716238926Smm				(void) printf(gettext("      %s\n"), fname);
4717238926Smm			}
4718238926Smm		}
4719238926Smm	}
4720238926Smm
4721238926Smm	return (0);
4722238926Smm}
4723238926Smm
4724168404Spjd/* ARGSUSED */
4725168404Spjdstatic int
4726168404Spjdupgrade_one(zpool_handle_t *zhp, void *data)
4727168404Spjd{
4728238926Smm	boolean_t printnl = B_FALSE;
4729185029Spjd	upgrade_cbdata_t *cbp = data;
4730185029Spjd	uint64_t cur_version;
4731168404Spjd	int ret;
4732168404Spjd
4733185029Spjd	if (strcmp("log", zpool_get_name(zhp)) == 0) {
4734185029Spjd		(void) printf(gettext("'log' is now a reserved word\n"
4735185029Spjd		    "Pool 'log' must be renamed using export and import"
4736185029Spjd		    " to upgrade.\n"));
4737185029Spjd		return (1);
4738185029Spjd	}
4739168404Spjd
4740185029Spjd	cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4741185029Spjd	if (cur_version > cbp->cb_version) {
4742168404Spjd		(void) printf(gettext("Pool '%s' is already formatted "
4743238926Smm		    "using more current version '%llu'.\n\n"),
4744185029Spjd		    zpool_get_name(zhp), cur_version);
4745185029Spjd		return (0);
4746185029Spjd	}
4747238926Smm
4748238926Smm	if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
4749185029Spjd		(void) printf(gettext("Pool '%s' is already formatted "
4750238926Smm		    "using version %llu.\n\n"), zpool_get_name(zhp),
4751238926Smm		    cbp->cb_version);
4752168404Spjd		return (0);
4753168404Spjd	}
4754168404Spjd
4755238926Smm	if (cur_version != cbp->cb_version) {
4756238926Smm		printnl = B_TRUE;
4757238926Smm		ret = upgrade_version(zhp, cbp->cb_version);
4758238950Smm		if (ret != 0)
4759238950Smm			return (ret);
4760238926Smm#ifdef __FreeBSD__
4761238950Smm		root_pool_upgrade_check(zhp, cbp->cb_poolname,
4762238950Smm		    sizeof(cbp->cb_poolname));
4763238926Smm#endif	/* ___FreeBSD__ */
4764238926Smm	}
4765168404Spjd
4766238926Smm	if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4767238926Smm		int count = 0;
4768238926Smm		ret = upgrade_enable_all(zhp, &count);
4769238926Smm		if (ret != 0)
4770238926Smm			return (ret);
4771238926Smm
4772238926Smm		if (count != 0) {
4773238926Smm			printnl = B_TRUE;
4774238950Smm#ifdef __FreeBSD__
4775238951Smm			root_pool_upgrade_check(zhp, cbp->cb_poolname,
4776238951Smm			    sizeof(cbp->cb_poolname));
4777238950Smm#endif	/* __FreeBSD __*/
4778238926Smm		} else if (cur_version == SPA_VERSION) {
4779238926Smm			(void) printf(gettext("Pool '%s' already has all "
4780238926Smm			    "supported features enabled.\n"),
4781238926Smm			    zpool_get_name(zhp));
4782238926Smm		}
4783168404Spjd	}
4784168404Spjd
4785238926Smm	if (printnl) {
4786238926Smm		(void) printf(gettext("\n"));
4787238926Smm	}
4788238926Smm
4789238926Smm	return (0);
4790168404Spjd}
4791168404Spjd
4792168404Spjd/*
4793168404Spjd * zpool upgrade
4794168404Spjd * zpool upgrade -v
4795185029Spjd * zpool upgrade [-V version] <-a | pool ...>
4796168404Spjd *
4797168404Spjd * With no arguments, display downrev'd ZFS pool available for upgrade.
4798168404Spjd * Individual pools can be upgraded by specifying the pool, and '-a' will
4799168404Spjd * upgrade all pools.
4800168404Spjd */
4801168404Spjdint
4802168404Spjdzpool_do_upgrade(int argc, char **argv)
4803168404Spjd{
4804168404Spjd	int c;
4805168404Spjd	upgrade_cbdata_t cb = { 0 };
4806168404Spjd	int ret = 0;
4807168404Spjd	boolean_t showversions = B_FALSE;
4808238926Smm	boolean_t upgradeall = B_FALSE;
4809185029Spjd	char *end;
4810168404Spjd
4811185029Spjd
4812168404Spjd	/* check options */
4813219089Spjd	while ((c = getopt(argc, argv, ":avV:")) != -1) {
4814168404Spjd		switch (c) {
4815168404Spjd		case 'a':
4816238926Smm			upgradeall = B_TRUE;
4817168404Spjd			break;
4818168404Spjd		case 'v':
4819168404Spjd			showversions = B_TRUE;
4820168404Spjd			break;
4821185029Spjd		case 'V':
4822185029Spjd			cb.cb_version = strtoll(optarg, &end, 10);
4823236884Smm			if (*end != '\0' ||
4824236884Smm			    !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
4825185029Spjd				(void) fprintf(stderr,
4826185029Spjd				    gettext("invalid version '%s'\n"), optarg);
4827185029Spjd				usage(B_FALSE);
4828185029Spjd			}
4829185029Spjd			break;
4830219089Spjd		case ':':
4831219089Spjd			(void) fprintf(stderr, gettext("missing argument for "
4832219089Spjd			    "'%c' option\n"), optopt);
4833219089Spjd			usage(B_FALSE);
4834219089Spjd			break;
4835168404Spjd		case '?':
4836168404Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4837168404Spjd			    optopt);
4838168404Spjd			usage(B_FALSE);
4839168404Spjd		}
4840168404Spjd	}
4841168404Spjd
4842168404Spjd	cb.cb_argc = argc;
4843168404Spjd	cb.cb_argv = argv;
4844168404Spjd	argc -= optind;
4845168404Spjd	argv += optind;
4846168404Spjd
4847185029Spjd	if (cb.cb_version == 0) {
4848185029Spjd		cb.cb_version = SPA_VERSION;
4849238926Smm	} else if (!upgradeall && argc == 0) {
4850185029Spjd		(void) fprintf(stderr, gettext("-V option is "
4851185029Spjd		    "incompatible with other arguments\n"));
4852185029Spjd		usage(B_FALSE);
4853185029Spjd	}
4854185029Spjd
4855168404Spjd	if (showversions) {
4856238926Smm		if (upgradeall || argc != 0) {
4857168404Spjd			(void) fprintf(stderr, gettext("-v option is "
4858168404Spjd			    "incompatible with other arguments\n"));
4859168404Spjd			usage(B_FALSE);
4860168404Spjd		}
4861238926Smm	} else if (upgradeall) {
4862168404Spjd		if (argc != 0) {
4863185029Spjd			(void) fprintf(stderr, gettext("-a option should not "
4864185029Spjd			    "be used along with a pool name\n"));
4865168404Spjd			usage(B_FALSE);
4866168404Spjd		}
4867168404Spjd	}
4868168404Spjd
4869236884Smm	(void) printf(gettext("This system supports ZFS pool feature "
4870236884Smm	    "flags.\n\n"));
4871168404Spjd	if (showversions) {
4872238926Smm		int i;
4873238926Smm
4874238926Smm		(void) printf(gettext("The following features are "
4875168404Spjd		    "supported:\n\n"));
4876238926Smm		(void) printf(gettext("FEAT DESCRIPTION\n"));
4877238926Smm		(void) printf("----------------------------------------------"
4878238926Smm		    "---------------\n");
4879238926Smm		for (i = 0; i < SPA_FEATURES; i++) {
4880238926Smm			zfeature_info_t *fi = &spa_feature_table[i];
4881238926Smm			const char *ro = fi->fi_can_readonly ?
4882238926Smm			    " (read-only compatible)" : "";
4883238926Smm
4884238926Smm			(void) printf("%-37s%s\n", fi->fi_uname, ro);
4885238926Smm			(void) printf("     %s\n", fi->fi_desc);
4886238926Smm		}
4887238926Smm		(void) printf("\n");
4888238926Smm
4889238926Smm		(void) printf(gettext("The following legacy versions are also "
4890238926Smm		    "supported:\n\n"));
4891168404Spjd		(void) printf(gettext("VER  DESCRIPTION\n"));
4892168404Spjd		(void) printf("---  -----------------------------------------"
4893168404Spjd		    "---------------\n");
4894168404Spjd		(void) printf(gettext(" 1   Initial ZFS version\n"));
4895168404Spjd		(void) printf(gettext(" 2   Ditto blocks "
4896168404Spjd		    "(replicated metadata)\n"));
4897168404Spjd		(void) printf(gettext(" 3   Hot spares and double parity "
4898168404Spjd		    "RAID-Z\n"));
4899168404Spjd		(void) printf(gettext(" 4   zpool history\n"));
4900168404Spjd		(void) printf(gettext(" 5   Compression using the gzip "
4901168404Spjd		    "algorithm\n"));
4902185029Spjd		(void) printf(gettext(" 6   bootfs pool property\n"));
4903185029Spjd		(void) printf(gettext(" 7   Separate intent log devices\n"));
4904185029Spjd		(void) printf(gettext(" 8   Delegated administration\n"));
4905185029Spjd		(void) printf(gettext(" 9   refquota and refreservation "
4906185029Spjd		    "properties\n"));
4907185029Spjd		(void) printf(gettext(" 10  Cache devices\n"));
4908185029Spjd		(void) printf(gettext(" 11  Improved scrub performance\n"));
4909185029Spjd		(void) printf(gettext(" 12  Snapshot properties\n"));
4910185029Spjd		(void) printf(gettext(" 13  snapused property\n"));
4911209962Smm		(void) printf(gettext(" 14  passthrough-x aclinherit\n"));
4912209962Smm		(void) printf(gettext(" 15  user/group space accounting\n"));
4913219089Spjd		(void) printf(gettext(" 16  stmf property support\n"));
4914219089Spjd		(void) printf(gettext(" 17  Triple-parity RAID-Z\n"));
4915219089Spjd		(void) printf(gettext(" 18  Snapshot user holds\n"));
4916219089Spjd		(void) printf(gettext(" 19  Log device removal\n"));
4917219089Spjd		(void) printf(gettext(" 20  Compression using zle "
4918219089Spjd		    "(zero-length encoding)\n"));
4919219089Spjd		(void) printf(gettext(" 21  Deduplication\n"));
4920219089Spjd		(void) printf(gettext(" 22  Received properties\n"));
4921219089Spjd		(void) printf(gettext(" 23  Slim ZIL\n"));
4922219089Spjd		(void) printf(gettext(" 24  System attributes\n"));
4923219089Spjd		(void) printf(gettext(" 25  Improved scrub stats\n"));
4924219089Spjd		(void) printf(gettext(" 26  Improved snapshot deletion "
4925219089Spjd		    "performance\n"));
4926219089Spjd		(void) printf(gettext(" 27  Improved snapshot creation "
4927219089Spjd		    "performance\n"));
4928219089Spjd		(void) printf(gettext(" 28  Multiple vdev replacements\n"));
4929219089Spjd		(void) printf(gettext("\nFor more information on a particular "
4930219089Spjd		    "version, including supported releases,\n"));
4931219089Spjd		(void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4932238926Smm	} else if (argc == 0 && upgradeall) {
4933238926Smm		cb.cb_first = B_TRUE;
4934168404Spjd		ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4935238926Smm		if (ret == 0 && cb.cb_first) {
4936238926Smm			if (cb.cb_version == SPA_VERSION) {
4937238926Smm				(void) printf(gettext("All pools are already "
4938238926Smm				    "formatted using feature flags.\n\n"));
4939238926Smm				(void) printf(gettext("Every feature flags "
4940238926Smm				    "pool already has all supported features "
4941238926Smm				    "enabled.\n"));
4942238926Smm			} else {
4943238926Smm				(void) printf(gettext("All pools are already "
4944238926Smm				    "formatted with version %llu or higher.\n"),
4945238926Smm				    cb.cb_version);
4946168404Spjd			}
4947168404Spjd		}
4948238926Smm	} else if (argc == 0) {
4949238926Smm		cb.cb_first = B_TRUE;
4950238926Smm		ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
4951238926Smm		assert(ret == 0);
4952168404Spjd
4953238926Smm		if (cb.cb_first) {
4954238926Smm			(void) printf(gettext("All pools are formatted "
4955238926Smm			    "using feature flags.\n\n"));
4956238926Smm		} else {
4957238926Smm			(void) printf(gettext("\nUse 'zpool upgrade -v' "
4958238926Smm			    "for a list of available legacy versions.\n"));
4959168404Spjd		}
4960238926Smm
4961238926Smm		cb.cb_first = B_TRUE;
4962238926Smm		ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
4963238926Smm		assert(ret == 0);
4964238926Smm
4965238926Smm		if (cb.cb_first) {
4966238926Smm			(void) printf(gettext("Every feature flags pool has "
4967238926Smm			    "all supported features enabled.\n"));
4968238926Smm		} else {
4969238926Smm			(void) printf(gettext("\n"));
4970238926Smm		}
4971168404Spjd	} else {
4972168404Spjd		ret = for_each_pool(argc, argv, B_FALSE, NULL,
4973168404Spjd		    upgrade_one, &cb);
4974168404Spjd	}
4975168404Spjd
4976212050Spjd	if (cb.cb_poolname[0] != '\0') {
4977212050Spjd		(void) printf(
4978212050Spjd		    "If you boot from pool '%s', don't forget to update boot code.\n"
4979212050Spjd		    "Assuming you use GPT partitioning and da0 is your boot disk\n"
4980212050Spjd		    "the following command will do it:\n"
4981212050Spjd		    "\n"
4982212050Spjd		    "\tgpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 da0\n\n",
4983212050Spjd		    cb.cb_poolname);
4984212050Spjd	}
4985212050Spjd
4986168404Spjd	return (ret);
4987168404Spjd}
4988168404Spjd
4989185029Spjdtypedef struct hist_cbdata {
4990185029Spjd	boolean_t first;
4991248571Smm	boolean_t longfmt;
4992248571Smm	boolean_t internal;
4993185029Spjd} hist_cbdata_t;
4994185029Spjd
4995168404Spjd/*
4996168404Spjd * Print out the command history for a specific pool.
4997168404Spjd */
4998168404Spjdstatic int
4999168404Spjdget_history_one(zpool_handle_t *zhp, void *data)
5000168404Spjd{
5001168404Spjd	nvlist_t *nvhis;
5002168404Spjd	nvlist_t **records;
5003168404Spjd	uint_t numrecords;
5004168404Spjd	int ret, i;
5005185029Spjd	hist_cbdata_t *cb = (hist_cbdata_t *)data;
5006168404Spjd
5007185029Spjd	cb->first = B_FALSE;
5008168404Spjd
5009168404Spjd	(void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
5010168404Spjd
5011168404Spjd	if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
5012168404Spjd		return (ret);
5013168404Spjd
5014168404Spjd	verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
5015168404Spjd	    &records, &numrecords) == 0);
5016168404Spjd	for (i = 0; i < numrecords; i++) {
5017248571Smm		nvlist_t *rec = records[i];
5018248571Smm		char tbuf[30] = "";
5019185029Spjd
5020248571Smm		if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
5021248571Smm			time_t tsec;
5022248571Smm			struct tm t;
5023185029Spjd
5024248571Smm			tsec = fnvlist_lookup_uint64(records[i],
5025248571Smm			    ZPOOL_HIST_TIME);
5026248571Smm			(void) localtime_r(&tsec, &t);
5027248571Smm			(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
5028248571Smm		}
5029248571Smm
5030248571Smm		if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
5031248571Smm			(void) printf("%s %s", tbuf,
5032248571Smm			    fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
5033248571Smm		} else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
5034248571Smm			int ievent =
5035248571Smm			    fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
5036248571Smm			if (!cb->internal)
5037185029Spjd				continue;
5038248571Smm			if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
5039248571Smm				(void) printf("%s unrecognized record:\n",
5040248571Smm				    tbuf);
5041248571Smm				dump_nvlist(rec, 4);
5042185029Spjd				continue;
5043248571Smm			}
5044248571Smm			(void) printf("%s [internal %s txg:%lld] %s", tbuf,
5045248571Smm			    zfs_history_event_names[ievent],
5046248571Smm			    fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5047248571Smm			    fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
5048248571Smm		} else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
5049248571Smm			if (!cb->internal)
5050248571Smm				continue;
5051248571Smm			(void) printf("%s [txg:%lld] %s", tbuf,
5052248571Smm			    fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5053248571Smm			    fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
5054248571Smm			if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
5055248571Smm				(void) printf(" %s (%llu)",
5056248571Smm				    fnvlist_lookup_string(rec,
5057248571Smm				    ZPOOL_HIST_DSNAME),
5058248571Smm				    fnvlist_lookup_uint64(rec,
5059248571Smm				    ZPOOL_HIST_DSID));
5060248571Smm			}
5061248571Smm			(void) printf(" %s", fnvlist_lookup_string(rec,
5062248571Smm			    ZPOOL_HIST_INT_STR));
5063248571Smm		} else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
5064248571Smm			if (!cb->internal)
5065248571Smm				continue;
5066248571Smm			(void) printf("%s ioctl %s\n", tbuf,
5067248571Smm			    fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
5068248571Smm			if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
5069248571Smm				(void) printf("    input:\n");
5070248571Smm				dump_nvlist(fnvlist_lookup_nvlist(rec,
5071248571Smm				    ZPOOL_HIST_INPUT_NVL), 8);
5072248571Smm			}
5073248571Smm			if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
5074248571Smm				(void) printf("    output:\n");
5075248571Smm				dump_nvlist(fnvlist_lookup_nvlist(rec,
5076248571Smm				    ZPOOL_HIST_OUTPUT_NVL), 8);
5077248571Smm			}
5078248571Smm		} else {
5079248571Smm			if (!cb->internal)
5080248571Smm				continue;
5081248571Smm			(void) printf("%s unrecognized record:\n", tbuf);
5082248571Smm			dump_nvlist(rec, 4);
5083168404Spjd		}
5084185029Spjd
5085185029Spjd		if (!cb->longfmt) {
5086185029Spjd			(void) printf("\n");
5087185029Spjd			continue;
5088185029Spjd		}
5089185029Spjd		(void) printf(" [");
5090248571Smm		if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
5091248571Smm			uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
5092248571Smm			struct passwd *pwd = getpwuid(who);
5093248571Smm			(void) printf("user %d ", (int)who);
5094248571Smm			if (pwd != NULL)
5095248571Smm				(void) printf("(%s) ", pwd->pw_name);
5096185029Spjd		}
5097248571Smm		if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
5098248571Smm			(void) printf("on %s",
5099248571Smm			    fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
5100185029Spjd		}
5101248571Smm		if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
5102248571Smm			(void) printf(":%s",
5103248571Smm			    fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
5104185029Spjd		}
5105185029Spjd		(void) printf("]");
5106185029Spjd		(void) printf("\n");
5107168404Spjd	}
5108168404Spjd	(void) printf("\n");
5109168404Spjd	nvlist_free(nvhis);
5110168404Spjd
5111168404Spjd	return (ret);
5112168404Spjd}
5113168404Spjd
5114168404Spjd/*
5115168404Spjd * zpool history <pool>
5116168404Spjd *
5117168404Spjd * Displays the history of commands that modified pools.
5118168404Spjd */
5119168404Spjdint
5120168404Spjdzpool_do_history(int argc, char **argv)
5121168404Spjd{
5122185029Spjd	hist_cbdata_t cbdata = { 0 };
5123168404Spjd	int ret;
5124185029Spjd	int c;
5125168404Spjd
5126185029Spjd	cbdata.first = B_TRUE;
5127185029Spjd	/* check options */
5128185029Spjd	while ((c = getopt(argc, argv, "li")) != -1) {
5129185029Spjd		switch (c) {
5130185029Spjd		case 'l':
5131248571Smm			cbdata.longfmt = B_TRUE;
5132185029Spjd			break;
5133185029Spjd		case 'i':
5134248571Smm			cbdata.internal = B_TRUE;
5135185029Spjd			break;
5136185029Spjd		case '?':
5137185029Spjd			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
5138185029Spjd			    optopt);
5139185029Spjd			usage(B_FALSE);
5140185029Spjd		}
5141185029Spjd	}
5142168404Spjd	argc -= optind;
5143168404Spjd	argv += optind;
5144168404Spjd
5145168404Spjd	ret = for_each_pool(argc, argv, B_FALSE,  NULL, get_history_one,
5146185029Spjd	    &cbdata);
5147168404Spjd
5148185029Spjd	if (argc == 0 && cbdata.first == B_TRUE) {
5149168404Spjd		(void) printf(gettext("no pools available\n"));
5150168404Spjd		return (0);
5151168404Spjd	}
5152168404Spjd
5153168404Spjd	return (ret);
5154168404Spjd}
5155168404Spjd
5156168404Spjdstatic int
5157168404Spjdget_callback(zpool_handle_t *zhp, void *data)
5158168404Spjd{
5159185029Spjd	zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5160168404Spjd	char value[MAXNAMELEN];
5161185029Spjd	zprop_source_t srctype;
5162185029Spjd	zprop_list_t *pl;
5163168404Spjd
5164168404Spjd	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5165168404Spjd
5166168404Spjd		/*
5167185029Spjd		 * Skip the special fake placeholder. This will also skip
5168185029Spjd		 * over the name property when 'all' is specified.
5169168404Spjd		 */
5170185029Spjd		if (pl->pl_prop == ZPOOL_PROP_NAME &&
5171168404Spjd		    pl == cbp->cb_proplist)
5172168404Spjd			continue;
5173168404Spjd
5174236884Smm		if (pl->pl_prop == ZPROP_INVAL &&
5175236884Smm		    (zpool_prop_feature(pl->pl_user_prop) ||
5176236884Smm		    zpool_prop_unsupported(pl->pl_user_prop))) {
5177236884Smm			srctype = ZPROP_SRC_LOCAL;
5178168404Spjd
5179236884Smm			if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5180236884Smm			    value, sizeof (value)) == 0) {
5181236884Smm				zprop_print_one_property(zpool_get_name(zhp),
5182236884Smm				    cbp, pl->pl_user_prop, value, srctype,
5183236884Smm				    NULL, NULL);
5184236884Smm			}
5185236884Smm		} else {
5186236884Smm			if (zpool_get_prop(zhp, pl->pl_prop, value,
5187236884Smm			    sizeof (value), &srctype) != 0)
5188236884Smm				continue;
5189236884Smm
5190236884Smm			zprop_print_one_property(zpool_get_name(zhp), cbp,
5191236884Smm			    zpool_prop_to_name(pl->pl_prop), value, srctype,
5192236884Smm			    NULL, NULL);
5193236884Smm		}
5194168404Spjd	}
5195168404Spjd	return (0);
5196168404Spjd}
5197168404Spjd
5198168404Spjdint
5199168404Spjdzpool_do_get(int argc, char **argv)
5200168404Spjd{
5201185029Spjd	zprop_get_cbdata_t cb = { 0 };
5202185029Spjd	zprop_list_t fake_name = { 0 };
5203168404Spjd	int ret;
5204168404Spjd
5205236884Smm	if (argc < 2) {
5206236884Smm		(void) fprintf(stderr, gettext("missing property "
5207236884Smm		    "argument\n"));
5208168404Spjd		usage(B_FALSE);
5209236884Smm	}
5210168404Spjd
5211168404Spjd	cb.cb_first = B_TRUE;
5212185029Spjd	cb.cb_sources = ZPROP_SRC_ALL;
5213168404Spjd	cb.cb_columns[0] = GET_COL_NAME;
5214168404Spjd	cb.cb_columns[1] = GET_COL_PROPERTY;
5215168404Spjd	cb.cb_columns[2] = GET_COL_VALUE;
5216168404Spjd	cb.cb_columns[3] = GET_COL_SOURCE;
5217185029Spjd	cb.cb_type = ZFS_TYPE_POOL;
5218168404Spjd
5219236884Smm	if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
5220185029Spjd	    ZFS_TYPE_POOL) != 0)
5221168404Spjd		usage(B_FALSE);
5222168404Spjd
5223168404Spjd	if (cb.cb_proplist != NULL) {
5224185029Spjd		fake_name.pl_prop = ZPOOL_PROP_NAME;
5225168404Spjd		fake_name.pl_width = strlen(gettext("NAME"));
5226168404Spjd		fake_name.pl_next = cb.cb_proplist;
5227168404Spjd		cb.cb_proplist = &fake_name;
5228168404Spjd	}
5229168404Spjd
5230168404Spjd	ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
5231168404Spjd	    get_callback, &cb);
5232168404Spjd
5233168404Spjd	if (cb.cb_proplist == &fake_name)
5234185029Spjd		zprop_free_list(fake_name.pl_next);
5235168404Spjd	else
5236185029Spjd		zprop_free_list(cb.cb_proplist);
5237168404Spjd
5238168404Spjd	return (ret);
5239168404Spjd}
5240168404Spjd
5241168404Spjdtypedef struct set_cbdata {
5242168404Spjd	char *cb_propname;
5243168404Spjd	char *cb_value;
5244168404Spjd	boolean_t cb_any_successful;
5245168404Spjd} set_cbdata_t;
5246168404Spjd
5247168404Spjdint
5248168404Spjdset_callback(zpool_handle_t *zhp, void *data)
5249168404Spjd{
5250168404Spjd	int error;
5251168404Spjd	set_cbdata_t *cb = (set_cbdata_t *)data;
5252168404Spjd
5253168404Spjd	error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5254168404Spjd
5255168404Spjd	if (!error)
5256168404Spjd		cb->cb_any_successful = B_TRUE;
5257168404Spjd
5258168404Spjd	return (error);
5259168404Spjd}
5260168404Spjd
5261168404Spjdint
5262168404Spjdzpool_do_set(int argc, char **argv)
5263168404Spjd{
5264168404Spjd	set_cbdata_t cb = { 0 };
5265168404Spjd	int error;
5266168404Spjd
5267168404Spjd	if (argc > 1 && argv[1][0] == '-') {
5268168404Spjd		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
5269168404Spjd		    argv[1][1]);
5270168404Spjd		usage(B_FALSE);
5271168404Spjd	}
5272168404Spjd
5273168404Spjd	if (argc < 2) {
5274168404Spjd		(void) fprintf(stderr, gettext("missing property=value "
5275168404Spjd		    "argument\n"));
5276168404Spjd		usage(B_FALSE);
5277168404Spjd	}
5278168404Spjd
5279168404Spjd	if (argc < 3) {
5280168404Spjd		(void) fprintf(stderr, gettext("missing pool name\n"));
5281168404Spjd		usage(B_FALSE);
5282168404Spjd	}
5283168404Spjd
5284168404Spjd	if (argc > 3) {
5285168404Spjd		(void) fprintf(stderr, gettext("too many pool names\n"));
5286168404Spjd		usage(B_FALSE);
5287168404Spjd	}
5288168404Spjd
5289168404Spjd	cb.cb_propname = argv[1];
5290168404Spjd	cb.cb_value = strchr(cb.cb_propname, '=');
5291168404Spjd	if (cb.cb_value == NULL) {
5292168404Spjd		(void) fprintf(stderr, gettext("missing value in "
5293168404Spjd		    "property=value argument\n"));
5294168404Spjd		usage(B_FALSE);
5295168404Spjd	}
5296168404Spjd
5297168404Spjd	*(cb.cb_value) = '\0';
5298168404Spjd	cb.cb_value++;
5299168404Spjd
5300168404Spjd	error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5301168404Spjd	    set_callback, &cb);
5302168404Spjd
5303168404Spjd	return (error);
5304168404Spjd}
5305168404Spjd
5306168404Spjdstatic int
5307168404Spjdfind_command_idx(char *command, int *idx)
5308168404Spjd{
5309168404Spjd	int i;
5310168404Spjd
5311168404Spjd	for (i = 0; i < NCOMMAND; i++) {
5312168404Spjd		if (command_table[i].name == NULL)
5313168404Spjd			continue;
5314168404Spjd
5315168404Spjd		if (strcmp(command, command_table[i].name) == 0) {
5316168404Spjd			*idx = i;
5317168404Spjd			return (0);
5318168404Spjd		}
5319168404Spjd	}
5320168404Spjd	return (1);
5321168404Spjd}
5322168404Spjd
5323168404Spjdint
5324168404Spjdmain(int argc, char **argv)
5325168404Spjd{
5326168404Spjd	int ret;
5327168404Spjd	int i;
5328168404Spjd	char *cmdname;
5329168404Spjd
5330168404Spjd	(void) setlocale(LC_ALL, "");
5331168404Spjd	(void) textdomain(TEXT_DOMAIN);
5332168404Spjd
5333168404Spjd	if ((g_zfs = libzfs_init()) == NULL) {
5334168404Spjd		(void) fprintf(stderr, gettext("internal error: failed to "
5335168404Spjd		    "initialize ZFS library\n"));
5336168404Spjd		return (1);
5337168404Spjd	}
5338168404Spjd
5339168404Spjd	libzfs_print_on_error(g_zfs, B_TRUE);
5340168404Spjd
5341168404Spjd	opterr = 0;
5342168404Spjd
5343168404Spjd	/*
5344168404Spjd	 * Make sure the user has specified some command.
5345168404Spjd	 */
5346168404Spjd	if (argc < 2) {
5347168404Spjd		(void) fprintf(stderr, gettext("missing command\n"));
5348168404Spjd		usage(B_FALSE);
5349168404Spjd	}
5350168404Spjd
5351168404Spjd	cmdname = argv[1];
5352168404Spjd
5353168404Spjd	/*
5354168404Spjd	 * Special case '-?'
5355168404Spjd	 */
5356168404Spjd	if (strcmp(cmdname, "-?") == 0)
5357168404Spjd		usage(B_TRUE);
5358168404Spjd
5359248571Smm	zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
5360185029Spjd
5361168404Spjd	/*
5362168404Spjd	 * Run the appropriate command.
5363168404Spjd	 */
5364168404Spjd	if (find_command_idx(cmdname, &i) == 0) {
5365168404Spjd		current_command = &command_table[i];
5366168404Spjd		ret = command_table[i].func(argc - 1, argv + 1);
5367185029Spjd	} else if (strchr(cmdname, '=')) {
5368185029Spjd		verify(find_command_idx("set", &i) == 0);
5369185029Spjd		current_command = &command_table[i];
5370185029Spjd		ret = command_table[i].func(argc, argv);
5371185029Spjd	} else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5372185029Spjd		/*
5373185029Spjd		 * 'freeze' is a vile debugging abomination, so we treat
5374185029Spjd		 * it as such.
5375185029Spjd		 */
5376252059Ssmh		zfs_cmd_t zc = { 0 };
5377252059Ssmh		(void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name));
5378252059Ssmh		return (!!zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc));
5379185029Spjd	} else {
5380168404Spjd		(void) fprintf(stderr, gettext("unrecognized "
5381168404Spjd		    "command '%s'\n"), cmdname);
5382168404Spjd		usage(B_FALSE);
5383168404Spjd	}
5384168404Spjd
5385248571Smm	if (ret == 0 && log_history)
5386248571Smm		(void) zpool_log_history(g_zfs, history_str);
5387248571Smm
5388168404Spjd	libzfs_fini(g_zfs);
5389168404Spjd
5390168404Spjd	/*
5391168404Spjd	 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5392168404Spjd	 * for the purposes of running ::findleaks.
5393168404Spjd	 */
5394168404Spjd	if (getenv("ZFS_ABORT") != NULL) {
5395168404Spjd		(void) printf("dumping core by request\n");
5396168404Spjd		abort();
5397168404Spjd	}
5398168404Spjd
5399168404Spjd	return (ret);
5400168404Spjd}
5401