xref: /illumos-gate/usr/src/cmd/zpool/zpool_main.c (revision c9ab4856df62d6551317f963d6be1cfc56a72ed1)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25  * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
26  * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27  * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
28  */
29 
30 #include <assert.h>
31 #include <ctype.h>
32 #include <dirent.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <libgen.h>
36 #include <libintl.h>
37 #include <libuutil.h>
38 #include <locale.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <strings.h>
43 #include <unistd.h>
44 #include <priv.h>
45 #include <pwd.h>
46 #include <zone.h>
47 #include <zfs_prop.h>
48 #include <sys/fs/zfs.h>
49 #include <sys/stat.h>
50 
51 #include <libzfs.h>
52 
53 #include "zpool_util.h"
54 #include "zfs_comutil.h"
55 #include "zfeature_common.h"
56 
57 #include "statcommon.h"
58 
59 static int zpool_do_create(int, char **);
60 static int zpool_do_destroy(int, char **);
61 
62 static int zpool_do_add(int, char **);
63 static int zpool_do_remove(int, char **);
64 
65 static int zpool_do_list(int, char **);
66 static int zpool_do_iostat(int, char **);
67 static int zpool_do_status(int, char **);
68 
69 static int zpool_do_online(int, char **);
70 static int zpool_do_offline(int, char **);
71 static int zpool_do_clear(int, char **);
72 static int zpool_do_reopen(int, char **);
73 
74 static int zpool_do_reguid(int, char **);
75 
76 static int zpool_do_attach(int, char **);
77 static int zpool_do_detach(int, char **);
78 static int zpool_do_replace(int, char **);
79 static int zpool_do_split(int, char **);
80 
81 static int zpool_do_scrub(int, char **);
82 
83 static int zpool_do_import(int, char **);
84 static int zpool_do_export(int, char **);
85 
86 static int zpool_do_upgrade(int, char **);
87 
88 static int zpool_do_history(int, char **);
89 
90 static int zpool_do_get(int, char **);
91 static int zpool_do_set(int, char **);
92 
93 /*
94  * These libumem hooks provide a reasonable set of defaults for the allocator's
95  * debugging facilities.
96  */
97 
98 #ifdef DEBUG
99 const char *
100 _umem_debug_init(void)
101 {
102 	return ("default,verbose"); /* $UMEM_DEBUG setting */
103 }
104 
105 const char *
106 _umem_logging_init(void)
107 {
108 	return ("fail,contents"); /* $UMEM_LOGGING setting */
109 }
110 #endif
111 
112 typedef enum {
113 	HELP_ADD,
114 	HELP_ATTACH,
115 	HELP_CLEAR,
116 	HELP_CREATE,
117 	HELP_DESTROY,
118 	HELP_DETACH,
119 	HELP_EXPORT,
120 	HELP_HISTORY,
121 	HELP_IMPORT,
122 	HELP_IOSTAT,
123 	HELP_LIST,
124 	HELP_OFFLINE,
125 	HELP_ONLINE,
126 	HELP_REPLACE,
127 	HELP_REMOVE,
128 	HELP_SCRUB,
129 	HELP_STATUS,
130 	HELP_UPGRADE,
131 	HELP_GET,
132 	HELP_SET,
133 	HELP_SPLIT,
134 	HELP_REGUID,
135 	HELP_REOPEN
136 } zpool_help_t;
137 
138 
139 typedef struct zpool_command {
140 	const char	*name;
141 	int		(*func)(int, char **);
142 	zpool_help_t	usage;
143 } zpool_command_t;
144 
145 /*
146  * Master command table.  Each ZFS command has a name, associated function, and
147  * usage message.  The usage messages need to be internationalized, so we have
148  * to have a function to return the usage message based on a command index.
149  *
150  * These commands are organized according to how they are displayed in the usage
151  * message.  An empty command (one with a NULL name) indicates an empty line in
152  * the generic usage message.
153  */
154 static zpool_command_t command_table[] = {
155 	{ "create",	zpool_do_create,	HELP_CREATE		},
156 	{ "destroy",	zpool_do_destroy,	HELP_DESTROY		},
157 	{ NULL },
158 	{ "add",	zpool_do_add,		HELP_ADD		},
159 	{ "remove",	zpool_do_remove,	HELP_REMOVE		},
160 	{ NULL },
161 	{ "list",	zpool_do_list,		HELP_LIST		},
162 	{ "iostat",	zpool_do_iostat,	HELP_IOSTAT		},
163 	{ "status",	zpool_do_status,	HELP_STATUS		},
164 	{ NULL },
165 	{ "online",	zpool_do_online,	HELP_ONLINE		},
166 	{ "offline",	zpool_do_offline,	HELP_OFFLINE		},
167 	{ "clear",	zpool_do_clear,		HELP_CLEAR		},
168 	{ "reopen",	zpool_do_reopen,	HELP_REOPEN		},
169 	{ NULL },
170 	{ "attach",	zpool_do_attach,	HELP_ATTACH		},
171 	{ "detach",	zpool_do_detach,	HELP_DETACH		},
172 	{ "replace",	zpool_do_replace,	HELP_REPLACE		},
173 	{ "split",	zpool_do_split,		HELP_SPLIT		},
174 	{ NULL },
175 	{ "scrub",	zpool_do_scrub,		HELP_SCRUB		},
176 	{ NULL },
177 	{ "import",	zpool_do_import,	HELP_IMPORT		},
178 	{ "export",	zpool_do_export,	HELP_EXPORT		},
179 	{ "upgrade",	zpool_do_upgrade,	HELP_UPGRADE		},
180 	{ "reguid",	zpool_do_reguid,	HELP_REGUID		},
181 	{ NULL },
182 	{ "history",	zpool_do_history,	HELP_HISTORY		},
183 	{ "get",	zpool_do_get,		HELP_GET		},
184 	{ "set",	zpool_do_set,		HELP_SET		},
185 };
186 
187 #define	NCOMMAND	(sizeof (command_table) / sizeof (command_table[0]))
188 
189 static zpool_command_t *current_command;
190 static char history_str[HIS_MAX_RECORD_LEN];
191 static boolean_t log_history = B_TRUE;
192 static uint_t timestamp_fmt = NODATE;
193 
194 static const char *
195 get_usage(zpool_help_t idx) {
196 	switch (idx) {
197 	case HELP_ADD:
198 		return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
199 	case HELP_ATTACH:
200 		return (gettext("\tattach [-f] <pool> <device> "
201 		    "<new-device>\n"));
202 	case HELP_CLEAR:
203 		return (gettext("\tclear [-nF] <pool> [device]\n"));
204 	case HELP_CREATE:
205 		return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
206 		    "\t    [-O file-system-property=value] ... \n"
207 		    "\t    [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
208 	case HELP_DESTROY:
209 		return (gettext("\tdestroy [-f] <pool>\n"));
210 	case HELP_DETACH:
211 		return (gettext("\tdetach <pool> <device>\n"));
212 	case HELP_EXPORT:
213 		return (gettext("\texport [-f] <pool> ...\n"));
214 	case HELP_HISTORY:
215 		return (gettext("\thistory [-il] [<pool>] ...\n"));
216 	case HELP_IMPORT:
217 		return (gettext("\timport [-d dir] [-D]\n"
218 		    "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
219 		    "\timport [-o mntopts] [-o property=value] ... \n"
220 		    "\t    [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
221 		    "[-R root] [-F [-n]] -a\n"
222 		    "\timport [-o mntopts] [-o property=value] ... \n"
223 		    "\t    [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
224 		    "[-R root] [-F [-n]]\n"
225 		    "\t    <pool | id> [newpool]\n"));
226 	case HELP_IOSTAT:
227 		return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
228 		    "[count]]\n"));
229 	case HELP_LIST:
230 		return (gettext("\tlist [-Hp] [-o property[,...]] "
231 		    "[-T d|u] [pool] ... [interval [count]]\n"));
232 	case HELP_OFFLINE:
233 		return (gettext("\toffline [-t] <pool> <device> ...\n"));
234 	case HELP_ONLINE:
235 		return (gettext("\tonline <pool> <device> ...\n"));
236 	case HELP_REPLACE:
237 		return (gettext("\treplace [-f] <pool> <device> "
238 		    "[new-device]\n"));
239 	case HELP_REMOVE:
240 		return (gettext("\tremove <pool> <device> ...\n"));
241 	case HELP_REOPEN:
242 		return (gettext("\treopen <pool>\n"));
243 	case HELP_SCRUB:
244 		return (gettext("\tscrub [-s] <pool> ...\n"));
245 	case HELP_STATUS:
246 		return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
247 		    "[count]]\n"));
248 	case HELP_UPGRADE:
249 		return (gettext("\tupgrade\n"
250 		    "\tupgrade -v\n"
251 		    "\tupgrade [-V version] <-a | pool ...>\n"));
252 	case HELP_GET:
253 		return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
254 		    "<\"all\" | property[,...]> <pool> ...\n"));
255 	case HELP_SET:
256 		return (gettext("\tset <property=value> <pool> \n"));
257 	case HELP_SPLIT:
258 		return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
259 		    "\t    [-o property=value] <pool> <newpool> "
260 		    "[<device> ...]\n"));
261 	case HELP_REGUID:
262 		return (gettext("\treguid <pool>\n"));
263 	}
264 
265 	abort();
266 	/* NOTREACHED */
267 }
268 
269 
270 /*
271  * Callback routine that will print out a pool property value.
272  */
273 static int
274 print_prop_cb(int prop, void *cb)
275 {
276 	FILE *fp = cb;
277 
278 	(void) fprintf(fp, "\t%-15s  ", zpool_prop_to_name(prop));
279 
280 	if (zpool_prop_readonly(prop))
281 		(void) fprintf(fp, "  NO   ");
282 	else
283 		(void) fprintf(fp, " YES   ");
284 
285 	if (zpool_prop_values(prop) == NULL)
286 		(void) fprintf(fp, "-\n");
287 	else
288 		(void) fprintf(fp, "%s\n", zpool_prop_values(prop));
289 
290 	return (ZPROP_CONT);
291 }
292 
293 /*
294  * Display usage message.  If we're inside a command, display only the usage for
295  * that command.  Otherwise, iterate over the entire command table and display
296  * a complete usage message.
297  */
298 void
299 usage(boolean_t requested)
300 {
301 	FILE *fp = requested ? stdout : stderr;
302 
303 	if (current_command == NULL) {
304 		int i;
305 
306 		(void) fprintf(fp, gettext("usage: zpool command args ...\n"));
307 		(void) fprintf(fp,
308 		    gettext("where 'command' is one of the following:\n\n"));
309 
310 		for (i = 0; i < NCOMMAND; i++) {
311 			if (command_table[i].name == NULL)
312 				(void) fprintf(fp, "\n");
313 			else
314 				(void) fprintf(fp, "%s",
315 				    get_usage(command_table[i].usage));
316 		}
317 	} else {
318 		(void) fprintf(fp, gettext("usage:\n"));
319 		(void) fprintf(fp, "%s", get_usage(current_command->usage));
320 	}
321 
322 	if (current_command != NULL &&
323 	    ((strcmp(current_command->name, "set") == 0) ||
324 	    (strcmp(current_command->name, "get") == 0) ||
325 	    (strcmp(current_command->name, "list") == 0))) {
326 
327 		(void) fprintf(fp,
328 		    gettext("\nthe following properties are supported:\n"));
329 
330 		(void) fprintf(fp, "\n\t%-15s  %s   %s\n\n",
331 		    "PROPERTY", "EDIT", "VALUES");
332 
333 		/* Iterate over all properties */
334 		(void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
335 		    ZFS_TYPE_POOL);
336 
337 		(void) fprintf(fp, "\t%-15s   ", "feature@...");
338 		(void) fprintf(fp, "YES   disabled | enabled | active\n");
339 
340 		(void) fprintf(fp, gettext("\nThe feature@ properties must be "
341 		    "appended with a feature name.\nSee zpool-features(5).\n"));
342 	}
343 
344 	/*
345 	 * See comments at end of main().
346 	 */
347 	if (getenv("ZFS_ABORT") != NULL) {
348 		(void) printf("dumping core by request\n");
349 		abort();
350 	}
351 
352 	exit(requested ? 0 : 2);
353 }
354 
355 void
356 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
357     boolean_t print_logs)
358 {
359 	nvlist_t **child;
360 	uint_t c, children;
361 	char *vname;
362 
363 	if (name != NULL)
364 		(void) printf("\t%*s%s\n", indent, "", name);
365 
366 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
367 	    &child, &children) != 0)
368 		return;
369 
370 	for (c = 0; c < children; c++) {
371 		uint64_t is_log = B_FALSE;
372 
373 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
374 		    &is_log);
375 		if ((is_log && !print_logs) || (!is_log && print_logs))
376 			continue;
377 
378 		vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
379 		print_vdev_tree(zhp, vname, child[c], indent + 2,
380 		    B_FALSE);
381 		free(vname);
382 	}
383 }
384 
385 static boolean_t
386 prop_list_contains_feature(nvlist_t *proplist)
387 {
388 	nvpair_t *nvp;
389 	for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
390 	    nvp = nvlist_next_nvpair(proplist, nvp)) {
391 		if (zpool_prop_feature(nvpair_name(nvp)))
392 			return (B_TRUE);
393 	}
394 	return (B_FALSE);
395 }
396 
397 /*
398  * Add a property pair (name, string-value) into a property nvlist.
399  */
400 static int
401 add_prop_list(const char *propname, char *propval, nvlist_t **props,
402     boolean_t poolprop)
403 {
404 	zpool_prop_t prop = ZPROP_INVAL;
405 	zfs_prop_t fprop;
406 	nvlist_t *proplist;
407 	const char *normnm;
408 	char *strval;
409 
410 	if (*props == NULL &&
411 	    nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
412 		(void) fprintf(stderr,
413 		    gettext("internal error: out of memory\n"));
414 		return (1);
415 	}
416 
417 	proplist = *props;
418 
419 	if (poolprop) {
420 		const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
421 
422 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
423 		    !zpool_prop_feature(propname)) {
424 			(void) fprintf(stderr, gettext("property '%s' is "
425 			    "not a valid pool property\n"), propname);
426 			return (2);
427 		}
428 
429 		/*
430 		 * feature@ properties and version should not be specified
431 		 * at the same time.
432 		 */
433 		if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) &&
434 		    nvlist_exists(proplist, vname)) ||
435 		    (prop == ZPOOL_PROP_VERSION &&
436 		    prop_list_contains_feature(proplist))) {
437 			(void) fprintf(stderr, gettext("'feature@' and "
438 			    "'version' properties cannot be specified "
439 			    "together\n"));
440 			return (2);
441 		}
442 
443 
444 		if (zpool_prop_feature(propname))
445 			normnm = propname;
446 		else
447 			normnm = zpool_prop_to_name(prop);
448 	} else {
449 		if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
450 			normnm = zfs_prop_to_name(fprop);
451 		} else {
452 			normnm = propname;
453 		}
454 	}
455 
456 	if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
457 	    prop != ZPOOL_PROP_CACHEFILE) {
458 		(void) fprintf(stderr, gettext("property '%s' "
459 		    "specified multiple times\n"), propname);
460 		return (2);
461 	}
462 
463 	if (nvlist_add_string(proplist, normnm, propval) != 0) {
464 		(void) fprintf(stderr, gettext("internal "
465 		    "error: out of memory\n"));
466 		return (1);
467 	}
468 
469 	return (0);
470 }
471 
472 /*
473  * zpool add [-fn] <pool> <vdev> ...
474  *
475  *	-f	Force addition of devices, even if they appear in use
476  *	-n	Do not add the devices, but display the resulting layout if
477  *		they were to be added.
478  *
479  * Adds the given vdevs to 'pool'.  As with create, the bulk of this work is
480  * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
481  * libzfs.
482  */
483 int
484 zpool_do_add(int argc, char **argv)
485 {
486 	boolean_t force = B_FALSE;
487 	boolean_t dryrun = B_FALSE;
488 	int c;
489 	nvlist_t *nvroot;
490 	char *poolname;
491 	int ret;
492 	zpool_handle_t *zhp;
493 	nvlist_t *config;
494 
495 	/* check options */
496 	while ((c = getopt(argc, argv, "fn")) != -1) {
497 		switch (c) {
498 		case 'f':
499 			force = B_TRUE;
500 			break;
501 		case 'n':
502 			dryrun = B_TRUE;
503 			break;
504 		case '?':
505 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
506 			    optopt);
507 			usage(B_FALSE);
508 		}
509 	}
510 
511 	argc -= optind;
512 	argv += optind;
513 
514 	/* get pool name and check number of arguments */
515 	if (argc < 1) {
516 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
517 		usage(B_FALSE);
518 	}
519 	if (argc < 2) {
520 		(void) fprintf(stderr, gettext("missing vdev specification\n"));
521 		usage(B_FALSE);
522 	}
523 
524 	poolname = argv[0];
525 
526 	argc--;
527 	argv++;
528 
529 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
530 		return (1);
531 
532 	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
533 		(void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
534 		    poolname);
535 		zpool_close(zhp);
536 		return (1);
537 	}
538 
539 	/* pass off to get_vdev_spec for processing */
540 	nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
541 	    argc, argv);
542 	if (nvroot == NULL) {
543 		zpool_close(zhp);
544 		return (1);
545 	}
546 
547 	if (dryrun) {
548 		nvlist_t *poolnvroot;
549 
550 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
551 		    &poolnvroot) == 0);
552 
553 		(void) printf(gettext("would update '%s' to the following "
554 		    "configuration:\n"), zpool_get_name(zhp));
555 
556 		/* print original main pool and new tree */
557 		print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
558 		print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
559 
560 		/* Do the same for the logs */
561 		if (num_logs(poolnvroot) > 0) {
562 			print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
563 			print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
564 		} else if (num_logs(nvroot) > 0) {
565 			print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
566 		}
567 
568 		ret = 0;
569 	} else {
570 		ret = (zpool_add(zhp, nvroot) != 0);
571 	}
572 
573 	nvlist_free(nvroot);
574 	zpool_close(zhp);
575 
576 	return (ret);
577 }
578 
579 /*
580  * zpool remove  <pool> <vdev> ...
581  *
582  * Removes the given vdev from the pool.  Currently, this supports removing
583  * spares, cache, and log devices from the pool.
584  */
585 int
586 zpool_do_remove(int argc, char **argv)
587 {
588 	char *poolname;
589 	int i, ret = 0;
590 	zpool_handle_t *zhp;
591 
592 	argc--;
593 	argv++;
594 
595 	/* get pool name and check number of arguments */
596 	if (argc < 1) {
597 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
598 		usage(B_FALSE);
599 	}
600 	if (argc < 2) {
601 		(void) fprintf(stderr, gettext("missing device\n"));
602 		usage(B_FALSE);
603 	}
604 
605 	poolname = argv[0];
606 
607 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
608 		return (1);
609 
610 	for (i = 1; i < argc; i++) {
611 		if (zpool_vdev_remove(zhp, argv[i]) != 0)
612 			ret = 1;
613 	}
614 
615 	return (ret);
616 }
617 
618 /*
619  * zpool create [-fnd] [-o property=value] ...
620  *		[-O file-system-property=value] ...
621  *		[-R root] [-m mountpoint] <pool> <dev> ...
622  *
623  *	-f	Force creation, even if devices appear in use
624  *	-n	Do not create the pool, but display the resulting layout if it
625  *		were to be created.
626  *      -R	Create a pool under an alternate root
627  *      -m	Set default mountpoint for the root dataset.  By default it's
628  *		'/<pool>'
629  *	-o	Set property=value.
630  *	-d	Don't automatically enable all supported pool features
631  *		(individual features can be enabled with -o).
632  *	-O	Set fsproperty=value in the pool's root file system
633  *
634  * Creates the named pool according to the given vdev specification.  The
635  * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c.  Once
636  * we get the nvlist back from get_vdev_spec(), we either print out the contents
637  * (if '-n' was specified), or pass it to libzfs to do the creation.
638  */
639 int
640 zpool_do_create(int argc, char **argv)
641 {
642 	boolean_t force = B_FALSE;
643 	boolean_t dryrun = B_FALSE;
644 	boolean_t enable_all_pool_feat = B_TRUE;
645 	int c;
646 	nvlist_t *nvroot = NULL;
647 	char *poolname;
648 	int ret = 1;
649 	char *altroot = NULL;
650 	char *mountpoint = NULL;
651 	nvlist_t *fsprops = NULL;
652 	nvlist_t *props = NULL;
653 	char *propval;
654 
655 	/* check options */
656 	while ((c = getopt(argc, argv, ":fndR:m:o:O:")) != -1) {
657 		switch (c) {
658 		case 'f':
659 			force = B_TRUE;
660 			break;
661 		case 'n':
662 			dryrun = B_TRUE;
663 			break;
664 		case 'd':
665 			enable_all_pool_feat = B_FALSE;
666 			break;
667 		case 'R':
668 			altroot = optarg;
669 			if (add_prop_list(zpool_prop_to_name(
670 			    ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
671 				goto errout;
672 			if (nvlist_lookup_string(props,
673 			    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
674 			    &propval) == 0)
675 				break;
676 			if (add_prop_list(zpool_prop_to_name(
677 			    ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
678 				goto errout;
679 			break;
680 		case 'm':
681 			/* Equivalent to -O mountpoint=optarg */
682 			mountpoint = optarg;
683 			break;
684 		case 'o':
685 			if ((propval = strchr(optarg, '=')) == NULL) {
686 				(void) fprintf(stderr, gettext("missing "
687 				    "'=' for -o option\n"));
688 				goto errout;
689 			}
690 			*propval = '\0';
691 			propval++;
692 
693 			if (add_prop_list(optarg, propval, &props, B_TRUE))
694 				goto errout;
695 
696 			/*
697 			 * If the user is creating a pool that doesn't support
698 			 * feature flags, don't enable any features.
699 			 */
700 			if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
701 				char *end;
702 				u_longlong_t ver;
703 
704 				ver = strtoull(propval, &end, 10);
705 				if (*end == '\0' &&
706 				    ver < SPA_VERSION_FEATURES) {
707 					enable_all_pool_feat = B_FALSE;
708 				}
709 			}
710 			break;
711 		case 'O':
712 			if ((propval = strchr(optarg, '=')) == NULL) {
713 				(void) fprintf(stderr, gettext("missing "
714 				    "'=' for -O option\n"));
715 				goto errout;
716 			}
717 			*propval = '\0';
718 			propval++;
719 
720 			/*
721 			 * Mountpoints are checked and then added later.
722 			 * Uniquely among properties, they can be specified
723 			 * more than once, to avoid conflict with -m.
724 			 */
725 			if (0 == strcmp(optarg,
726 			    zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
727 				mountpoint = propval;
728 			} else if (add_prop_list(optarg, propval, &fsprops,
729 			    B_FALSE)) {
730 				goto errout;
731 			}
732 			break;
733 		case ':':
734 			(void) fprintf(stderr, gettext("missing argument for "
735 			    "'%c' option\n"), optopt);
736 			goto badusage;
737 		case '?':
738 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
739 			    optopt);
740 			goto badusage;
741 		}
742 	}
743 
744 	argc -= optind;
745 	argv += optind;
746 
747 	/* get pool name and check number of arguments */
748 	if (argc < 1) {
749 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
750 		goto badusage;
751 	}
752 	if (argc < 2) {
753 		(void) fprintf(stderr, gettext("missing vdev specification\n"));
754 		goto badusage;
755 	}
756 
757 	poolname = argv[0];
758 
759 	/*
760 	 * As a special case, check for use of '/' in the name, and direct the
761 	 * user to use 'zfs create' instead.
762 	 */
763 	if (strchr(poolname, '/') != NULL) {
764 		(void) fprintf(stderr, gettext("cannot create '%s': invalid "
765 		    "character '/' in pool name\n"), poolname);
766 		(void) fprintf(stderr, gettext("use 'zfs create' to "
767 		    "create a dataset\n"));
768 		goto errout;
769 	}
770 
771 	/* pass off to get_vdev_spec for bulk processing */
772 	nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
773 	    argc - 1, argv + 1);
774 	if (nvroot == NULL)
775 		goto errout;
776 
777 	/* make_root_vdev() allows 0 toplevel children if there are spares */
778 	if (!zfs_allocatable_devs(nvroot)) {
779 		(void) fprintf(stderr, gettext("invalid vdev "
780 		    "specification: at least one toplevel vdev must be "
781 		    "specified\n"));
782 		goto errout;
783 	}
784 
785 	if (altroot != NULL && altroot[0] != '/') {
786 		(void) fprintf(stderr, gettext("invalid alternate root '%s': "
787 		    "must be an absolute path\n"), altroot);
788 		goto errout;
789 	}
790 
791 	/*
792 	 * Check the validity of the mountpoint and direct the user to use the
793 	 * '-m' mountpoint option if it looks like its in use.
794 	 */
795 	if (mountpoint == NULL ||
796 	    (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
797 	    strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
798 		char buf[MAXPATHLEN];
799 		DIR *dirp;
800 
801 		if (mountpoint && mountpoint[0] != '/') {
802 			(void) fprintf(stderr, gettext("invalid mountpoint "
803 			    "'%s': must be an absolute path, 'legacy', or "
804 			    "'none'\n"), mountpoint);
805 			goto errout;
806 		}
807 
808 		if (mountpoint == NULL) {
809 			if (altroot != NULL)
810 				(void) snprintf(buf, sizeof (buf), "%s/%s",
811 				    altroot, poolname);
812 			else
813 				(void) snprintf(buf, sizeof (buf), "/%s",
814 				    poolname);
815 		} else {
816 			if (altroot != NULL)
817 				(void) snprintf(buf, sizeof (buf), "%s%s",
818 				    altroot, mountpoint);
819 			else
820 				(void) snprintf(buf, sizeof (buf), "%s",
821 				    mountpoint);
822 		}
823 
824 		if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
825 			(void) fprintf(stderr, gettext("mountpoint '%s' : "
826 			    "%s\n"), buf, strerror(errno));
827 			(void) fprintf(stderr, gettext("use '-m' "
828 			    "option to provide a different default\n"));
829 			goto errout;
830 		} else if (dirp) {
831 			int count = 0;
832 
833 			while (count < 3 && readdir(dirp) != NULL)
834 				count++;
835 			(void) closedir(dirp);
836 
837 			if (count > 2) {
838 				(void) fprintf(stderr, gettext("mountpoint "
839 				    "'%s' exists and is not empty\n"), buf);
840 				(void) fprintf(stderr, gettext("use '-m' "
841 				    "option to provide a "
842 				    "different default\n"));
843 				goto errout;
844 			}
845 		}
846 	}
847 
848 	/*
849 	 * Now that the mountpoint's validity has been checked, ensure that
850 	 * the property is set appropriately prior to creating the pool.
851 	 */
852 	if (mountpoint != NULL) {
853 		ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
854 		    mountpoint, &fsprops, B_FALSE);
855 		if (ret != 0)
856 			goto errout;
857 	}
858 
859 	ret = 1;
860 	if (dryrun) {
861 		/*
862 		 * For a dry run invocation, print out a basic message and run
863 		 * through all the vdevs in the list and print out in an
864 		 * appropriate hierarchy.
865 		 */
866 		(void) printf(gettext("would create '%s' with the "
867 		    "following layout:\n\n"), poolname);
868 
869 		print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
870 		if (num_logs(nvroot) > 0)
871 			print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
872 
873 		ret = 0;
874 	} else {
875 		/*
876 		 * Hand off to libzfs.
877 		 */
878 		if (enable_all_pool_feat) {
879 			spa_feature_t i;
880 			for (i = 0; i < SPA_FEATURES; i++) {
881 				char propname[MAXPATHLEN];
882 				zfeature_info_t *feat = &spa_feature_table[i];
883 
884 				(void) snprintf(propname, sizeof (propname),
885 				    "feature@%s", feat->fi_uname);
886 
887 				/*
888 				 * Skip feature if user specified it manually
889 				 * on the command line.
890 				 */
891 				if (nvlist_exists(props, propname))
892 					continue;
893 
894 				ret = add_prop_list(propname,
895 				    ZFS_FEATURE_ENABLED, &props, B_TRUE);
896 				if (ret != 0)
897 					goto errout;
898 			}
899 		}
900 
901 		ret = 1;
902 		if (zpool_create(g_zfs, poolname,
903 		    nvroot, props, fsprops) == 0) {
904 			zfs_handle_t *pool = zfs_open(g_zfs, poolname,
905 			    ZFS_TYPE_FILESYSTEM);
906 			if (pool != NULL) {
907 				if (zfs_mount(pool, NULL, 0) == 0)
908 					ret = zfs_shareall(pool);
909 				zfs_close(pool);
910 			}
911 		} else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
912 			(void) fprintf(stderr, gettext("pool name may have "
913 			    "been omitted\n"));
914 		}
915 	}
916 
917 errout:
918 	nvlist_free(nvroot);
919 	nvlist_free(fsprops);
920 	nvlist_free(props);
921 	return (ret);
922 badusage:
923 	nvlist_free(fsprops);
924 	nvlist_free(props);
925 	usage(B_FALSE);
926 	return (2);
927 }
928 
929 /*
930  * zpool destroy <pool>
931  *
932  * 	-f	Forcefully unmount any datasets
933  *
934  * Destroy the given pool.  Automatically unmounts any datasets in the pool.
935  */
936 int
937 zpool_do_destroy(int argc, char **argv)
938 {
939 	boolean_t force = B_FALSE;
940 	int c;
941 	char *pool;
942 	zpool_handle_t *zhp;
943 	int ret;
944 
945 	/* check options */
946 	while ((c = getopt(argc, argv, "f")) != -1) {
947 		switch (c) {
948 		case 'f':
949 			force = B_TRUE;
950 			break;
951 		case '?':
952 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
953 			    optopt);
954 			usage(B_FALSE);
955 		}
956 	}
957 
958 	argc -= optind;
959 	argv += optind;
960 
961 	/* check arguments */
962 	if (argc < 1) {
963 		(void) fprintf(stderr, gettext("missing pool argument\n"));
964 		usage(B_FALSE);
965 	}
966 	if (argc > 1) {
967 		(void) fprintf(stderr, gettext("too many arguments\n"));
968 		usage(B_FALSE);
969 	}
970 
971 	pool = argv[0];
972 
973 	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
974 		/*
975 		 * As a special case, check for use of '/' in the name, and
976 		 * direct the user to use 'zfs destroy' instead.
977 		 */
978 		if (strchr(pool, '/') != NULL)
979 			(void) fprintf(stderr, gettext("use 'zfs destroy' to "
980 			    "destroy a dataset\n"));
981 		return (1);
982 	}
983 
984 	if (zpool_disable_datasets(zhp, force) != 0) {
985 		(void) fprintf(stderr, gettext("could not destroy '%s': "
986 		    "could not unmount datasets\n"), zpool_get_name(zhp));
987 		return (1);
988 	}
989 
990 	/* The history must be logged as part of the export */
991 	log_history = B_FALSE;
992 
993 	ret = (zpool_destroy(zhp, history_str) != 0);
994 
995 	zpool_close(zhp);
996 
997 	return (ret);
998 }
999 
1000 /*
1001  * zpool export [-f] <pool> ...
1002  *
1003  *	-f	Forcefully unmount datasets
1004  *
1005  * Export the given pools.  By default, the command will attempt to cleanly
1006  * unmount any active datasets within the pool.  If the '-f' flag is specified,
1007  * then the datasets will be forcefully unmounted.
1008  */
1009 int
1010 zpool_do_export(int argc, char **argv)
1011 {
1012 	boolean_t force = B_FALSE;
1013 	boolean_t hardforce = B_FALSE;
1014 	int c;
1015 	zpool_handle_t *zhp;
1016 	int ret;
1017 	int i;
1018 
1019 	/* check options */
1020 	while ((c = getopt(argc, argv, "fF")) != -1) {
1021 		switch (c) {
1022 		case 'f':
1023 			force = B_TRUE;
1024 			break;
1025 		case 'F':
1026 			hardforce = B_TRUE;
1027 			break;
1028 		case '?':
1029 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1030 			    optopt);
1031 			usage(B_FALSE);
1032 		}
1033 	}
1034 
1035 	argc -= optind;
1036 	argv += optind;
1037 
1038 	/* check arguments */
1039 	if (argc < 1) {
1040 		(void) fprintf(stderr, gettext("missing pool argument\n"));
1041 		usage(B_FALSE);
1042 	}
1043 
1044 	ret = 0;
1045 	for (i = 0; i < argc; i++) {
1046 		if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1047 			ret = 1;
1048 			continue;
1049 		}
1050 
1051 		if (zpool_disable_datasets(zhp, force) != 0) {
1052 			ret = 1;
1053 			zpool_close(zhp);
1054 			continue;
1055 		}
1056 
1057 		/* The history must be logged as part of the export */
1058 		log_history = B_FALSE;
1059 
1060 		if (hardforce) {
1061 			if (zpool_export_force(zhp, history_str) != 0)
1062 				ret = 1;
1063 		} else if (zpool_export(zhp, force, history_str) != 0) {
1064 			ret = 1;
1065 		}
1066 
1067 		zpool_close(zhp);
1068 	}
1069 
1070 	return (ret);
1071 }
1072 
1073 /*
1074  * Given a vdev configuration, determine the maximum width needed for the device
1075  * name column.
1076  */
1077 static int
1078 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1079 {
1080 	char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1081 	nvlist_t **child;
1082 	uint_t c, children;
1083 	int ret;
1084 
1085 	if (strlen(name) + depth > max)
1086 		max = strlen(name) + depth;
1087 
1088 	free(name);
1089 
1090 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1091 	    &child, &children) == 0) {
1092 		for (c = 0; c < children; c++)
1093 			if ((ret = max_width(zhp, child[c], depth + 2,
1094 			    max)) > max)
1095 				max = ret;
1096 	}
1097 
1098 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1099 	    &child, &children) == 0) {
1100 		for (c = 0; c < children; c++)
1101 			if ((ret = max_width(zhp, child[c], depth + 2,
1102 			    max)) > max)
1103 				max = ret;
1104 	}
1105 
1106 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1107 	    &child, &children) == 0) {
1108 		for (c = 0; c < children; c++)
1109 			if ((ret = max_width(zhp, child[c], depth + 2,
1110 			    max)) > max)
1111 				max = ret;
1112 	}
1113 
1114 
1115 	return (max);
1116 }
1117 
1118 typedef struct spare_cbdata {
1119 	uint64_t	cb_guid;
1120 	zpool_handle_t	*cb_zhp;
1121 } spare_cbdata_t;
1122 
1123 static boolean_t
1124 find_vdev(nvlist_t *nv, uint64_t search)
1125 {
1126 	uint64_t guid;
1127 	nvlist_t **child;
1128 	uint_t c, children;
1129 
1130 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1131 	    search == guid)
1132 		return (B_TRUE);
1133 
1134 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1135 	    &child, &children) == 0) {
1136 		for (c = 0; c < children; c++)
1137 			if (find_vdev(child[c], search))
1138 				return (B_TRUE);
1139 	}
1140 
1141 	return (B_FALSE);
1142 }
1143 
1144 static int
1145 find_spare(zpool_handle_t *zhp, void *data)
1146 {
1147 	spare_cbdata_t *cbp = data;
1148 	nvlist_t *config, *nvroot;
1149 
1150 	config = zpool_get_config(zhp, NULL);
1151 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1152 	    &nvroot) == 0);
1153 
1154 	if (find_vdev(nvroot, cbp->cb_guid)) {
1155 		cbp->cb_zhp = zhp;
1156 		return (1);
1157 	}
1158 
1159 	zpool_close(zhp);
1160 	return (0);
1161 }
1162 
1163 /*
1164  * Print out configuration state as requested by status_callback.
1165  */
1166 void
1167 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1168     int namewidth, int depth, boolean_t isspare)
1169 {
1170 	nvlist_t **child;
1171 	uint_t c, children;
1172 	pool_scan_stat_t *ps = NULL;
1173 	vdev_stat_t *vs;
1174 	char rbuf[6], wbuf[6], cbuf[6];
1175 	char *vname;
1176 	uint64_t notpresent;
1177 	spare_cbdata_t cb;
1178 	char *state;
1179 
1180 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1181 	    &child, &children) != 0)
1182 		children = 0;
1183 
1184 	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1185 	    (uint64_t **)&vs, &c) == 0);
1186 
1187 	state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1188 	if (isspare) {
1189 		/*
1190 		 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1191 		 * online drives.
1192 		 */
1193 		if (vs->vs_aux == VDEV_AUX_SPARED)
1194 			state = "INUSE";
1195 		else if (vs->vs_state == VDEV_STATE_HEALTHY)
1196 			state = "AVAIL";
1197 	}
1198 
1199 	(void) printf("\t%*s%-*s  %-8s", depth, "", namewidth - depth,
1200 	    name, state);
1201 
1202 	if (!isspare) {
1203 		zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1204 		zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1205 		zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1206 		(void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1207 	}
1208 
1209 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1210 	    &notpresent) == 0) {
1211 		char *path;
1212 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1213 		(void) printf("  was %s", path);
1214 	} else if (vs->vs_aux != 0) {
1215 		(void) printf("  ");
1216 
1217 		switch (vs->vs_aux) {
1218 		case VDEV_AUX_OPEN_FAILED:
1219 			(void) printf(gettext("cannot open"));
1220 			break;
1221 
1222 		case VDEV_AUX_BAD_GUID_SUM:
1223 			(void) printf(gettext("missing device"));
1224 			break;
1225 
1226 		case VDEV_AUX_NO_REPLICAS:
1227 			(void) printf(gettext("insufficient replicas"));
1228 			break;
1229 
1230 		case VDEV_AUX_VERSION_NEWER:
1231 			(void) printf(gettext("newer version"));
1232 			break;
1233 
1234 		case VDEV_AUX_UNSUP_FEAT:
1235 			(void) printf(gettext("unsupported feature(s)"));
1236 			break;
1237 
1238 		case VDEV_AUX_SPARED:
1239 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1240 			    &cb.cb_guid) == 0);
1241 			if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1242 				if (strcmp(zpool_get_name(cb.cb_zhp),
1243 				    zpool_get_name(zhp)) == 0)
1244 					(void) printf(gettext("currently in "
1245 					    "use"));
1246 				else
1247 					(void) printf(gettext("in use by "
1248 					    "pool '%s'"),
1249 					    zpool_get_name(cb.cb_zhp));
1250 				zpool_close(cb.cb_zhp);
1251 			} else {
1252 				(void) printf(gettext("currently in use"));
1253 			}
1254 			break;
1255 
1256 		case VDEV_AUX_ERR_EXCEEDED:
1257 			(void) printf(gettext("too many errors"));
1258 			break;
1259 
1260 		case VDEV_AUX_IO_FAILURE:
1261 			(void) printf(gettext("experienced I/O failures"));
1262 			break;
1263 
1264 		case VDEV_AUX_BAD_LOG:
1265 			(void) printf(gettext("bad intent log"));
1266 			break;
1267 
1268 		case VDEV_AUX_EXTERNAL:
1269 			(void) printf(gettext("external device fault"));
1270 			break;
1271 
1272 		case VDEV_AUX_SPLIT_POOL:
1273 			(void) printf(gettext("split into new pool"));
1274 			break;
1275 
1276 		default:
1277 			(void) printf(gettext("corrupted data"));
1278 			break;
1279 		}
1280 	}
1281 
1282 	(void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1283 	    (uint64_t **)&ps, &c);
1284 
1285 	if (ps && ps->pss_state == DSS_SCANNING &&
1286 	    vs->vs_scan_processed != 0 && children == 0) {
1287 		(void) printf(gettext("  (%s)"),
1288 		    (ps->pss_func == POOL_SCAN_RESILVER) ?
1289 		    "resilvering" : "repairing");
1290 	}
1291 
1292 	(void) printf("\n");
1293 
1294 	for (c = 0; c < children; c++) {
1295 		uint64_t islog = B_FALSE, ishole = B_FALSE;
1296 
1297 		/* Don't print logs or holes here */
1298 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1299 		    &islog);
1300 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1301 		    &ishole);
1302 		if (islog || ishole)
1303 			continue;
1304 		vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1305 		print_status_config(zhp, vname, child[c],
1306 		    namewidth, depth + 2, isspare);
1307 		free(vname);
1308 	}
1309 }
1310 
1311 
1312 /*
1313  * Print the configuration of an exported pool.  Iterate over all vdevs in the
1314  * pool, printing out the name and status for each one.
1315  */
1316 void
1317 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1318 {
1319 	nvlist_t **child;
1320 	uint_t c, children;
1321 	vdev_stat_t *vs;
1322 	char *type, *vname;
1323 
1324 	verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1325 	if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1326 	    strcmp(type, VDEV_TYPE_HOLE) == 0)
1327 		return;
1328 
1329 	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1330 	    (uint64_t **)&vs, &c) == 0);
1331 
1332 	(void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1333 	(void) printf("  %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1334 
1335 	if (vs->vs_aux != 0) {
1336 		(void) printf("  ");
1337 
1338 		switch (vs->vs_aux) {
1339 		case VDEV_AUX_OPEN_FAILED:
1340 			(void) printf(gettext("cannot open"));
1341 			break;
1342 
1343 		case VDEV_AUX_BAD_GUID_SUM:
1344 			(void) printf(gettext("missing device"));
1345 			break;
1346 
1347 		case VDEV_AUX_NO_REPLICAS:
1348 			(void) printf(gettext("insufficient replicas"));
1349 			break;
1350 
1351 		case VDEV_AUX_VERSION_NEWER:
1352 			(void) printf(gettext("newer version"));
1353 			break;
1354 
1355 		case VDEV_AUX_UNSUP_FEAT:
1356 			(void) printf(gettext("unsupported feature(s)"));
1357 			break;
1358 
1359 		case VDEV_AUX_ERR_EXCEEDED:
1360 			(void) printf(gettext("too many errors"));
1361 			break;
1362 
1363 		default:
1364 			(void) printf(gettext("corrupted data"));
1365 			break;
1366 		}
1367 	}
1368 	(void) printf("\n");
1369 
1370 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1371 	    &child, &children) != 0)
1372 		return;
1373 
1374 	for (c = 0; c < children; c++) {
1375 		uint64_t is_log = B_FALSE;
1376 
1377 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1378 		    &is_log);
1379 		if (is_log)
1380 			continue;
1381 
1382 		vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1383 		print_import_config(vname, child[c], namewidth, depth + 2);
1384 		free(vname);
1385 	}
1386 
1387 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1388 	    &child, &children) == 0) {
1389 		(void) printf(gettext("\tcache\n"));
1390 		for (c = 0; c < children; c++) {
1391 			vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1392 			(void) printf("\t  %s\n", vname);
1393 			free(vname);
1394 		}
1395 	}
1396 
1397 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1398 	    &child, &children) == 0) {
1399 		(void) printf(gettext("\tspares\n"));
1400 		for (c = 0; c < children; c++) {
1401 			vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1402 			(void) printf("\t  %s\n", vname);
1403 			free(vname);
1404 		}
1405 	}
1406 }
1407 
1408 /*
1409  * Print log vdevs.
1410  * Logs are recorded as top level vdevs in the main pool child array
1411  * but with "is_log" set to 1. We use either print_status_config() or
1412  * print_import_config() to print the top level logs then any log
1413  * children (eg mirrored slogs) are printed recursively - which
1414  * works because only the top level vdev is marked "is_log"
1415  */
1416 static void
1417 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1418 {
1419 	uint_t c, children;
1420 	nvlist_t **child;
1421 
1422 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1423 	    &children) != 0)
1424 		return;
1425 
1426 	(void) printf(gettext("\tlogs\n"));
1427 
1428 	for (c = 0; c < children; c++) {
1429 		uint64_t is_log = B_FALSE;
1430 		char *name;
1431 
1432 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1433 		    &is_log);
1434 		if (!is_log)
1435 			continue;
1436 		name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1437 		if (verbose)
1438 			print_status_config(zhp, name, child[c], namewidth,
1439 			    2, B_FALSE);
1440 		else
1441 			print_import_config(name, child[c], namewidth, 2);
1442 		free(name);
1443 	}
1444 }
1445 
1446 /*
1447  * Display the status for the given pool.
1448  */
1449 static void
1450 show_import(nvlist_t *config)
1451 {
1452 	uint64_t pool_state;
1453 	vdev_stat_t *vs;
1454 	char *name;
1455 	uint64_t guid;
1456 	char *msgid;
1457 	nvlist_t *nvroot;
1458 	int reason;
1459 	const char *health;
1460 	uint_t vsc;
1461 	int namewidth;
1462 	char *comment;
1463 
1464 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1465 	    &name) == 0);
1466 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1467 	    &guid) == 0);
1468 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1469 	    &pool_state) == 0);
1470 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1471 	    &nvroot) == 0);
1472 
1473 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1474 	    (uint64_t **)&vs, &vsc) == 0);
1475 	health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1476 
1477 	reason = zpool_import_status(config, &msgid);
1478 
1479 	(void) printf(gettext("   pool: %s\n"), name);
1480 	(void) printf(gettext("     id: %llu\n"), (u_longlong_t)guid);
1481 	(void) printf(gettext("  state: %s"), health);
1482 	if (pool_state == POOL_STATE_DESTROYED)
1483 		(void) printf(gettext(" (DESTROYED)"));
1484 	(void) printf("\n");
1485 
1486 	switch (reason) {
1487 	case ZPOOL_STATUS_MISSING_DEV_R:
1488 	case ZPOOL_STATUS_MISSING_DEV_NR:
1489 	case ZPOOL_STATUS_BAD_GUID_SUM:
1490 		(void) printf(gettext(" status: One or more devices are "
1491 		    "missing from the system.\n"));
1492 		break;
1493 
1494 	case ZPOOL_STATUS_CORRUPT_LABEL_R:
1495 	case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1496 		(void) printf(gettext(" status: One or more devices contains "
1497 		    "corrupted data.\n"));
1498 		break;
1499 
1500 	case ZPOOL_STATUS_CORRUPT_DATA:
1501 		(void) printf(
1502 		    gettext(" status: The pool data is corrupted.\n"));
1503 		break;
1504 
1505 	case ZPOOL_STATUS_OFFLINE_DEV:
1506 		(void) printf(gettext(" status: One or more devices "
1507 		    "are offlined.\n"));
1508 		break;
1509 
1510 	case ZPOOL_STATUS_CORRUPT_POOL:
1511 		(void) printf(gettext(" status: The pool metadata is "
1512 		    "corrupted.\n"));
1513 		break;
1514 
1515 	case ZPOOL_STATUS_VERSION_OLDER:
1516 		(void) printf(gettext(" status: The pool is formatted using a "
1517 		    "legacy on-disk version.\n"));
1518 		break;
1519 
1520 	case ZPOOL_STATUS_VERSION_NEWER:
1521 		(void) printf(gettext(" status: The pool is formatted using an "
1522 		    "incompatible version.\n"));
1523 		break;
1524 
1525 	case ZPOOL_STATUS_FEAT_DISABLED:
1526 		(void) printf(gettext(" status: Some supported features are "
1527 		    "not enabled on the pool.\n"));
1528 		break;
1529 
1530 	case ZPOOL_STATUS_UNSUP_FEAT_READ:
1531 		(void) printf(gettext("status: The pool uses the following "
1532 		    "feature(s) not supported on this sytem:\n"));
1533 		zpool_print_unsup_feat(config);
1534 		break;
1535 
1536 	case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1537 		(void) printf(gettext("status: The pool can only be accessed "
1538 		    "in read-only mode on this system. It\n\tcannot be "
1539 		    "accessed in read-write mode because it uses the "
1540 		    "following\n\tfeature(s) not supported on this system:\n"));
1541 		zpool_print_unsup_feat(config);
1542 		break;
1543 
1544 	case ZPOOL_STATUS_HOSTID_MISMATCH:
1545 		(void) printf(gettext(" status: The pool was last accessed by "
1546 		    "another system.\n"));
1547 		break;
1548 
1549 	case ZPOOL_STATUS_FAULTED_DEV_R:
1550 	case ZPOOL_STATUS_FAULTED_DEV_NR:
1551 		(void) printf(gettext(" status: One or more devices are "
1552 		    "faulted.\n"));
1553 		break;
1554 
1555 	case ZPOOL_STATUS_BAD_LOG:
1556 		(void) printf(gettext(" status: An intent log record cannot be "
1557 		    "read.\n"));
1558 		break;
1559 
1560 	case ZPOOL_STATUS_RESILVERING:
1561 		(void) printf(gettext(" status: One or more devices were being "
1562 		    "resilvered.\n"));
1563 		break;
1564 
1565 	default:
1566 		/*
1567 		 * No other status can be seen when importing pools.
1568 		 */
1569 		assert(reason == ZPOOL_STATUS_OK);
1570 	}
1571 
1572 	/*
1573 	 * Print out an action according to the overall state of the pool.
1574 	 */
1575 	if (vs->vs_state == VDEV_STATE_HEALTHY) {
1576 		if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1577 		    reason == ZPOOL_STATUS_FEAT_DISABLED) {
1578 			(void) printf(gettext(" action: The pool can be "
1579 			    "imported using its name or numeric identifier, "
1580 			    "though\n\tsome features will not be available "
1581 			    "without an explicit 'zpool upgrade'.\n"));
1582 		} else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1583 			(void) printf(gettext(" action: The pool can be "
1584 			    "imported using its name or numeric "
1585 			    "identifier and\n\tthe '-f' flag.\n"));
1586 		} else {
1587 			(void) printf(gettext(" action: The pool can be "
1588 			    "imported using its name or numeric "
1589 			    "identifier.\n"));
1590 		}
1591 	} else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1592 		(void) printf(gettext(" action: The pool can be imported "
1593 		    "despite missing or damaged devices.  The\n\tfault "
1594 		    "tolerance of the pool may be compromised if imported.\n"));
1595 	} else {
1596 		switch (reason) {
1597 		case ZPOOL_STATUS_VERSION_NEWER:
1598 			(void) printf(gettext(" action: The pool cannot be "
1599 			    "imported.  Access the pool on a system running "
1600 			    "newer\n\tsoftware, or recreate the pool from "
1601 			    "backup.\n"));
1602 			break;
1603 		case ZPOOL_STATUS_UNSUP_FEAT_READ:
1604 			(void) printf(gettext("action: The pool cannot be "
1605 			    "imported. Access the pool on a system that "
1606 			    "supports\n\tthe required feature(s), or recreate "
1607 			    "the pool from backup.\n"));
1608 			break;
1609 		case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1610 			(void) printf(gettext("action: The pool cannot be "
1611 			    "imported in read-write mode. Import the pool "
1612 			    "with\n"
1613 			    "\t\"-o readonly=on\", access the pool on a system "
1614 			    "that supports the\n\trequired feature(s), or "
1615 			    "recreate the pool from backup.\n"));
1616 			break;
1617 		case ZPOOL_STATUS_MISSING_DEV_R:
1618 		case ZPOOL_STATUS_MISSING_DEV_NR:
1619 		case ZPOOL_STATUS_BAD_GUID_SUM:
1620 			(void) printf(gettext(" action: The pool cannot be "
1621 			    "imported. Attach the missing\n\tdevices and try "
1622 			    "again.\n"));
1623 			break;
1624 		default:
1625 			(void) printf(gettext(" action: The pool cannot be "
1626 			    "imported due to damaged devices or data.\n"));
1627 		}
1628 	}
1629 
1630 	/* Print the comment attached to the pool. */
1631 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1632 		(void) printf(gettext("comment: %s\n"), comment);
1633 
1634 	/*
1635 	 * If the state is "closed" or "can't open", and the aux state
1636 	 * is "corrupt data":
1637 	 */
1638 	if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1639 	    (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1640 	    (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1641 		if (pool_state == POOL_STATE_DESTROYED)
1642 			(void) printf(gettext("\tThe pool was destroyed, "
1643 			    "but can be imported using the '-Df' flags.\n"));
1644 		else if (pool_state != POOL_STATE_EXPORTED)
1645 			(void) printf(gettext("\tThe pool may be active on "
1646 			    "another system, but can be imported using\n\t"
1647 			    "the '-f' flag.\n"));
1648 	}
1649 
1650 	if (msgid != NULL)
1651 		(void) printf(gettext("   see: http://illumos.org/msg/%s\n"),
1652 		    msgid);
1653 
1654 	(void) printf(gettext(" config:\n\n"));
1655 
1656 	namewidth = max_width(NULL, nvroot, 0, 0);
1657 	if (namewidth < 10)
1658 		namewidth = 10;
1659 
1660 	print_import_config(name, nvroot, namewidth, 0);
1661 	if (num_logs(nvroot) > 0)
1662 		print_logs(NULL, nvroot, namewidth, B_FALSE);
1663 
1664 	if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1665 		(void) printf(gettext("\n\tAdditional devices are known to "
1666 		    "be part of this pool, though their\n\texact "
1667 		    "configuration cannot be determined.\n"));
1668 	}
1669 }
1670 
1671 /*
1672  * Perform the import for the given configuration.  This passes the heavy
1673  * lifting off to zpool_import_props(), and then mounts the datasets contained
1674  * within the pool.
1675  */
1676 static int
1677 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1678     nvlist_t *props, int flags)
1679 {
1680 	zpool_handle_t *zhp;
1681 	char *name;
1682 	uint64_t state;
1683 	uint64_t version;
1684 
1685 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1686 	    &name) == 0);
1687 
1688 	verify(nvlist_lookup_uint64(config,
1689 	    ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1690 	verify(nvlist_lookup_uint64(config,
1691 	    ZPOOL_CONFIG_VERSION, &version) == 0);
1692 	if (!SPA_VERSION_IS_SUPPORTED(version)) {
1693 		(void) fprintf(stderr, gettext("cannot import '%s': pool "
1694 		    "is formatted using an unsupported ZFS version\n"), name);
1695 		return (1);
1696 	} else if (state != POOL_STATE_EXPORTED &&
1697 	    !(flags & ZFS_IMPORT_ANY_HOST)) {
1698 		uint64_t hostid;
1699 
1700 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1701 		    &hostid) == 0) {
1702 			if ((unsigned long)hostid != gethostid()) {
1703 				char *hostname;
1704 				uint64_t timestamp;
1705 				time_t t;
1706 
1707 				verify(nvlist_lookup_string(config,
1708 				    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1709 				verify(nvlist_lookup_uint64(config,
1710 				    ZPOOL_CONFIG_TIMESTAMP, &timestamp) == 0);
1711 				t = timestamp;
1712 				(void) fprintf(stderr, gettext("cannot import "
1713 				    "'%s': pool may be in use from other "
1714 				    "system, it was last accessed by %s "
1715 				    "(hostid: 0x%lx) on %s"), name, hostname,
1716 				    (unsigned long)hostid,
1717 				    asctime(localtime(&t)));
1718 				(void) fprintf(stderr, gettext("use '-f' to "
1719 				    "import anyway\n"));
1720 				return (1);
1721 			}
1722 		} else {
1723 			(void) fprintf(stderr, gettext("cannot import '%s': "
1724 			    "pool may be in use from other system\n"), name);
1725 			(void) fprintf(stderr, gettext("use '-f' to import "
1726 			    "anyway\n"));
1727 			return (1);
1728 		}
1729 	}
1730 
1731 	if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1732 		return (1);
1733 
1734 	if (newname != NULL)
1735 		name = (char *)newname;
1736 
1737 	if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1738 		return (1);
1739 
1740 	if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1741 	    !(flags & ZFS_IMPORT_ONLY) &&
1742 	    zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1743 		zpool_close(zhp);
1744 		return (1);
1745 	}
1746 
1747 	zpool_close(zhp);
1748 	return (0);
1749 }
1750 
1751 /*
1752  * zpool import [-d dir] [-D]
1753  *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1754  *              [-d dir | -c cachefile] [-f] -a
1755  *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1756  *              [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1757  *
1758  *	 -c	Read pool information from a cachefile instead of searching
1759  *		devices.
1760  *
1761  *       -d	Scan in a specific directory, other than /dev/dsk.  More than
1762  *		one directory can be specified using multiple '-d' options.
1763  *
1764  *       -D     Scan for previously destroyed pools or import all or only
1765  *              specified destroyed pools.
1766  *
1767  *       -R	Temporarily import the pool, with all mountpoints relative to
1768  *		the given root.  The pool will remain exported when the machine
1769  *		is rebooted.
1770  *
1771  *       -V	Import even in the presence of faulted vdevs.  This is an
1772  *       	intentionally undocumented option for testing purposes, and
1773  *       	treats the pool configuration as complete, leaving any bad
1774  *		vdevs in the FAULTED state. In other words, it does verbatim
1775  *		import.
1776  *
1777  *       -f	Force import, even if it appears that the pool is active.
1778  *
1779  *       -F     Attempt rewind if necessary.
1780  *
1781  *       -n     See if rewind would work, but don't actually rewind.
1782  *
1783  *       -N     Import the pool but don't mount datasets.
1784  *
1785  *       -T     Specify a starting txg to use for import. This option is
1786  *       	intentionally undocumented option for testing purposes.
1787  *
1788  *       -a	Import all pools found.
1789  *
1790  *       -o	Set property=value and/or temporary mount options (without '=').
1791  *
1792  * The import command scans for pools to import, and import pools based on pool
1793  * name and GUID.  The pool can also be renamed as part of the import process.
1794  */
1795 int
1796 zpool_do_import(int argc, char **argv)
1797 {
1798 	char **searchdirs = NULL;
1799 	int nsearch = 0;
1800 	int c;
1801 	int err = 0;
1802 	nvlist_t *pools = NULL;
1803 	boolean_t do_all = B_FALSE;
1804 	boolean_t do_destroyed = B_FALSE;
1805 	char *mntopts = NULL;
1806 	nvpair_t *elem;
1807 	nvlist_t *config;
1808 	uint64_t searchguid = 0;
1809 	char *searchname = NULL;
1810 	char *propval;
1811 	nvlist_t *found_config;
1812 	nvlist_t *policy = NULL;
1813 	nvlist_t *props = NULL;
1814 	boolean_t first;
1815 	int flags = ZFS_IMPORT_NORMAL;
1816 	uint32_t rewind_policy = ZPOOL_NO_REWIND;
1817 	boolean_t dryrun = B_FALSE;
1818 	boolean_t do_rewind = B_FALSE;
1819 	boolean_t xtreme_rewind = B_FALSE;
1820 	uint64_t pool_state, txg = -1ULL;
1821 	char *cachefile = NULL;
1822 	importargs_t idata = { 0 };
1823 	char *endptr;
1824 
1825 	/* check options */
1826 	while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1827 		switch (c) {
1828 		case 'a':
1829 			do_all = B_TRUE;
1830 			break;
1831 		case 'c':
1832 			cachefile = optarg;
1833 			break;
1834 		case 'd':
1835 			if (searchdirs == NULL) {
1836 				searchdirs = safe_malloc(sizeof (char *));
1837 			} else {
1838 				char **tmp = safe_malloc((nsearch + 1) *
1839 				    sizeof (char *));
1840 				bcopy(searchdirs, tmp, nsearch *
1841 				    sizeof (char *));
1842 				free(searchdirs);
1843 				searchdirs = tmp;
1844 			}
1845 			searchdirs[nsearch++] = optarg;
1846 			break;
1847 		case 'D':
1848 			do_destroyed = B_TRUE;
1849 			break;
1850 		case 'f':
1851 			flags |= ZFS_IMPORT_ANY_HOST;
1852 			break;
1853 		case 'F':
1854 			do_rewind = B_TRUE;
1855 			break;
1856 		case 'm':
1857 			flags |= ZFS_IMPORT_MISSING_LOG;
1858 			break;
1859 		case 'n':
1860 			dryrun = B_TRUE;
1861 			break;
1862 		case 'N':
1863 			flags |= ZFS_IMPORT_ONLY;
1864 			break;
1865 		case 'o':
1866 			if ((propval = strchr(optarg, '=')) != NULL) {
1867 				*propval = '\0';
1868 				propval++;
1869 				if (add_prop_list(optarg, propval,
1870 				    &props, B_TRUE))
1871 					goto error;
1872 			} else {
1873 				mntopts = optarg;
1874 			}
1875 			break;
1876 		case 'R':
1877 			if (add_prop_list(zpool_prop_to_name(
1878 			    ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1879 				goto error;
1880 			if (nvlist_lookup_string(props,
1881 			    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
1882 			    &propval) == 0)
1883 				break;
1884 			if (add_prop_list(zpool_prop_to_name(
1885 			    ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1886 				goto error;
1887 			break;
1888 		case 'T':
1889 			errno = 0;
1890 			txg = strtoull(optarg, &endptr, 0);
1891 			if (errno != 0 || *endptr != '\0') {
1892 				(void) fprintf(stderr,
1893 				    gettext("invalid txg value\n"));
1894 				usage(B_FALSE);
1895 			}
1896 			rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
1897 			break;
1898 		case 'V':
1899 			flags |= ZFS_IMPORT_VERBATIM;
1900 			break;
1901 		case 'X':
1902 			xtreme_rewind = B_TRUE;
1903 			break;
1904 		case ':':
1905 			(void) fprintf(stderr, gettext("missing argument for "
1906 			    "'%c' option\n"), optopt);
1907 			usage(B_FALSE);
1908 			break;
1909 		case '?':
1910 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1911 			    optopt);
1912 			usage(B_FALSE);
1913 		}
1914 	}
1915 
1916 	argc -= optind;
1917 	argv += optind;
1918 
1919 	if (cachefile && nsearch != 0) {
1920 		(void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
1921 		usage(B_FALSE);
1922 	}
1923 
1924 	if ((dryrun || xtreme_rewind) && !do_rewind) {
1925 		(void) fprintf(stderr,
1926 		    gettext("-n or -X only meaningful with -F\n"));
1927 		usage(B_FALSE);
1928 	}
1929 	if (dryrun)
1930 		rewind_policy = ZPOOL_TRY_REWIND;
1931 	else if (do_rewind)
1932 		rewind_policy = ZPOOL_DO_REWIND;
1933 	if (xtreme_rewind)
1934 		rewind_policy |= ZPOOL_EXTREME_REWIND;
1935 
1936 	/* In the future, we can capture further policy and include it here */
1937 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
1938 	    nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
1939 	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
1940 		goto error;
1941 
1942 	if (searchdirs == NULL) {
1943 		searchdirs = safe_malloc(sizeof (char *));
1944 		searchdirs[0] = "/dev/dsk";
1945 		nsearch = 1;
1946 	}
1947 
1948 	/* check argument count */
1949 	if (do_all) {
1950 		if (argc != 0) {
1951 			(void) fprintf(stderr, gettext("too many arguments\n"));
1952 			usage(B_FALSE);
1953 		}
1954 	} else {
1955 		if (argc > 2) {
1956 			(void) fprintf(stderr, gettext("too many arguments\n"));
1957 			usage(B_FALSE);
1958 		}
1959 
1960 		/*
1961 		 * Check for the SYS_CONFIG privilege.  We do this explicitly
1962 		 * here because otherwise any attempt to discover pools will
1963 		 * silently fail.
1964 		 */
1965 		if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
1966 			(void) fprintf(stderr, gettext("cannot "
1967 			    "discover pools: permission denied\n"));
1968 			free(searchdirs);
1969 			nvlist_free(policy);
1970 			return (1);
1971 		}
1972 	}
1973 
1974 	/*
1975 	 * Depending on the arguments given, we do one of the following:
1976 	 *
1977 	 *	<none>	Iterate through all pools and display information about
1978 	 *		each one.
1979 	 *
1980 	 *	-a	Iterate through all pools and try to import each one.
1981 	 *
1982 	 *	<id>	Find the pool that corresponds to the given GUID/pool
1983 	 *		name and import that one.
1984 	 *
1985 	 *	-D	Above options applies only to destroyed pools.
1986 	 */
1987 	if (argc != 0) {
1988 		char *endptr;
1989 
1990 		errno = 0;
1991 		searchguid = strtoull(argv[0], &endptr, 10);
1992 		if (errno != 0 || *endptr != '\0') {
1993 			searchname = argv[0];
1994 			searchguid = 0;
1995 		}
1996 		found_config = NULL;
1997 
1998 		/*
1999 		 * User specified a name or guid.  Ensure it's unique.
2000 		 */
2001 		idata.unique = B_TRUE;
2002 	}
2003 
2004 
2005 	idata.path = searchdirs;
2006 	idata.paths = nsearch;
2007 	idata.poolname = searchname;
2008 	idata.guid = searchguid;
2009 	idata.cachefile = cachefile;
2010 
2011 	pools = zpool_search_import(g_zfs, &idata);
2012 
2013 	if (pools != NULL && idata.exists &&
2014 	    (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2015 		(void) fprintf(stderr, gettext("cannot import '%s': "
2016 		    "a pool with that name already exists\n"),
2017 		    argv[0]);
2018 		(void) fprintf(stderr, gettext("use the form '%s "
2019 		    "<pool | id> <newpool>' to give it a new name\n"),
2020 		    "zpool import");
2021 		err = 1;
2022 	} else if (pools == NULL && idata.exists) {
2023 		(void) fprintf(stderr, gettext("cannot import '%s': "
2024 		    "a pool with that name is already created/imported,\n"),
2025 		    argv[0]);
2026 		(void) fprintf(stderr, gettext("and no additional pools "
2027 		    "with that name were found\n"));
2028 		err = 1;
2029 	} else if (pools == NULL) {
2030 		if (argc != 0) {
2031 			(void) fprintf(stderr, gettext("cannot import '%s': "
2032 			    "no such pool available\n"), argv[0]);
2033 		}
2034 		err = 1;
2035 	}
2036 
2037 	if (err == 1) {
2038 		free(searchdirs);
2039 		nvlist_free(policy);
2040 		return (1);
2041 	}
2042 
2043 	/*
2044 	 * At this point we have a list of import candidate configs. Even if
2045 	 * we were searching by pool name or guid, we still need to
2046 	 * post-process the list to deal with pool state and possible
2047 	 * duplicate names.
2048 	 */
2049 	err = 0;
2050 	elem = NULL;
2051 	first = B_TRUE;
2052 	while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2053 
2054 		verify(nvpair_value_nvlist(elem, &config) == 0);
2055 
2056 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2057 		    &pool_state) == 0);
2058 		if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2059 			continue;
2060 		if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2061 			continue;
2062 
2063 		verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2064 		    policy) == 0);
2065 
2066 		if (argc == 0) {
2067 			if (first)
2068 				first = B_FALSE;
2069 			else if (!do_all)
2070 				(void) printf("\n");
2071 
2072 			if (do_all) {
2073 				err |= do_import(config, NULL, mntopts,
2074 				    props, flags);
2075 			} else {
2076 				show_import(config);
2077 			}
2078 		} else if (searchname != NULL) {
2079 			char *name;
2080 
2081 			/*
2082 			 * We are searching for a pool based on name.
2083 			 */
2084 			verify(nvlist_lookup_string(config,
2085 			    ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2086 
2087 			if (strcmp(name, searchname) == 0) {
2088 				if (found_config != NULL) {
2089 					(void) fprintf(stderr, gettext(
2090 					    "cannot import '%s': more than "
2091 					    "one matching pool\n"), searchname);
2092 					(void) fprintf(stderr, gettext(
2093 					    "import by numeric ID instead\n"));
2094 					err = B_TRUE;
2095 				}
2096 				found_config = config;
2097 			}
2098 		} else {
2099 			uint64_t guid;
2100 
2101 			/*
2102 			 * Search for a pool by guid.
2103 			 */
2104 			verify(nvlist_lookup_uint64(config,
2105 			    ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2106 
2107 			if (guid == searchguid)
2108 				found_config = config;
2109 		}
2110 	}
2111 
2112 	/*
2113 	 * If we were searching for a specific pool, verify that we found a
2114 	 * pool, and then do the import.
2115 	 */
2116 	if (argc != 0 && err == 0) {
2117 		if (found_config == NULL) {
2118 			(void) fprintf(stderr, gettext("cannot import '%s': "
2119 			    "no such pool available\n"), argv[0]);
2120 			err = B_TRUE;
2121 		} else {
2122 			err |= do_import(found_config, argc == 1 ? NULL :
2123 			    argv[1], mntopts, props, flags);
2124 		}
2125 	}
2126 
2127 	/*
2128 	 * If we were just looking for pools, report an error if none were
2129 	 * found.
2130 	 */
2131 	if (argc == 0 && first)
2132 		(void) fprintf(stderr,
2133 		    gettext("no pools available to import\n"));
2134 
2135 error:
2136 	nvlist_free(props);
2137 	nvlist_free(pools);
2138 	nvlist_free(policy);
2139 	free(searchdirs);
2140 
2141 	return (err ? 1 : 0);
2142 }
2143 
2144 typedef struct iostat_cbdata {
2145 	boolean_t cb_verbose;
2146 	int cb_namewidth;
2147 	int cb_iteration;
2148 	zpool_list_t *cb_list;
2149 } iostat_cbdata_t;
2150 
2151 static void
2152 print_iostat_separator(iostat_cbdata_t *cb)
2153 {
2154 	int i = 0;
2155 
2156 	for (i = 0; i < cb->cb_namewidth; i++)
2157 		(void) printf("-");
2158 	(void) printf("  -----  -----  -----  -----  -----  -----\n");
2159 }
2160 
2161 static void
2162 print_iostat_header(iostat_cbdata_t *cb)
2163 {
2164 	(void) printf("%*s     capacity     operations    bandwidth\n",
2165 	    cb->cb_namewidth, "");
2166 	(void) printf("%-*s  alloc   free   read  write   read  write\n",
2167 	    cb->cb_namewidth, "pool");
2168 	print_iostat_separator(cb);
2169 }
2170 
2171 /*
2172  * Display a single statistic.
2173  */
2174 static void
2175 print_one_stat(uint64_t value)
2176 {
2177 	char buf[64];
2178 
2179 	zfs_nicenum(value, buf, sizeof (buf));
2180 	(void) printf("  %5s", buf);
2181 }
2182 
2183 /*
2184  * Print out all the statistics for the given vdev.  This can either be the
2185  * toplevel configuration, or called recursively.  If 'name' is NULL, then this
2186  * is a verbose output, and we don't want to display the toplevel pool stats.
2187  */
2188 void
2189 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2190     nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2191 {
2192 	nvlist_t **oldchild, **newchild;
2193 	uint_t c, children;
2194 	vdev_stat_t *oldvs, *newvs;
2195 	vdev_stat_t zerovs = { 0 };
2196 	uint64_t tdelta;
2197 	double scale;
2198 	char *vname;
2199 
2200 	if (oldnv != NULL) {
2201 		verify(nvlist_lookup_uint64_array(oldnv,
2202 		    ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2203 	} else {
2204 		oldvs = &zerovs;
2205 	}
2206 
2207 	verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2208 	    (uint64_t **)&newvs, &c) == 0);
2209 
2210 	if (strlen(name) + depth > cb->cb_namewidth)
2211 		(void) printf("%*s%s", depth, "", name);
2212 	else
2213 		(void) printf("%*s%s%*s", depth, "", name,
2214 		    (int)(cb->cb_namewidth - strlen(name) - depth), "");
2215 
2216 	tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2217 
2218 	if (tdelta == 0)
2219 		scale = 1.0;
2220 	else
2221 		scale = (double)NANOSEC / tdelta;
2222 
2223 	/* only toplevel vdevs have capacity stats */
2224 	if (newvs->vs_space == 0) {
2225 		(void) printf("      -      -");
2226 	} else {
2227 		print_one_stat(newvs->vs_alloc);
2228 		print_one_stat(newvs->vs_space - newvs->vs_alloc);
2229 	}
2230 
2231 	print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2232 	    oldvs->vs_ops[ZIO_TYPE_READ])));
2233 
2234 	print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2235 	    oldvs->vs_ops[ZIO_TYPE_WRITE])));
2236 
2237 	print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2238 	    oldvs->vs_bytes[ZIO_TYPE_READ])));
2239 
2240 	print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2241 	    oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2242 
2243 	(void) printf("\n");
2244 
2245 	if (!cb->cb_verbose)
2246 		return;
2247 
2248 	if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2249 	    &newchild, &children) != 0)
2250 		return;
2251 
2252 	if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2253 	    &oldchild, &c) != 0)
2254 		return;
2255 
2256 	for (c = 0; c < children; c++) {
2257 		uint64_t ishole = B_FALSE, islog = B_FALSE;
2258 
2259 		(void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2260 		    &ishole);
2261 
2262 		(void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2263 		    &islog);
2264 
2265 		if (ishole || islog)
2266 			continue;
2267 
2268 		vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2269 		print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2270 		    newchild[c], cb, depth + 2);
2271 		free(vname);
2272 	}
2273 
2274 	/*
2275 	 * Log device section
2276 	 */
2277 
2278 	if (num_logs(newnv) > 0) {
2279 		(void) printf("%-*s      -      -      -      -      -      "
2280 		    "-\n", cb->cb_namewidth, "logs");
2281 
2282 		for (c = 0; c < children; c++) {
2283 			uint64_t islog = B_FALSE;
2284 			(void) nvlist_lookup_uint64(newchild[c],
2285 			    ZPOOL_CONFIG_IS_LOG, &islog);
2286 
2287 			if (islog) {
2288 				vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2289 				    B_FALSE);
2290 				print_vdev_stats(zhp, vname, oldnv ?
2291 				    oldchild[c] : NULL, newchild[c],
2292 				    cb, depth + 2);
2293 				free(vname);
2294 			}
2295 		}
2296 
2297 	}
2298 
2299 	/*
2300 	 * Include level 2 ARC devices in iostat output
2301 	 */
2302 	if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2303 	    &newchild, &children) != 0)
2304 		return;
2305 
2306 	if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2307 	    &oldchild, &c) != 0)
2308 		return;
2309 
2310 	if (children > 0) {
2311 		(void) printf("%-*s      -      -      -      -      -      "
2312 		    "-\n", cb->cb_namewidth, "cache");
2313 		for (c = 0; c < children; c++) {
2314 			vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2315 			    B_FALSE);
2316 			print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2317 			    newchild[c], cb, depth + 2);
2318 			free(vname);
2319 		}
2320 	}
2321 }
2322 
2323 static int
2324 refresh_iostat(zpool_handle_t *zhp, void *data)
2325 {
2326 	iostat_cbdata_t *cb = data;
2327 	boolean_t missing;
2328 
2329 	/*
2330 	 * If the pool has disappeared, remove it from the list and continue.
2331 	 */
2332 	if (zpool_refresh_stats(zhp, &missing) != 0)
2333 		return (-1);
2334 
2335 	if (missing)
2336 		pool_list_remove(cb->cb_list, zhp);
2337 
2338 	return (0);
2339 }
2340 
2341 /*
2342  * Callback to print out the iostats for the given pool.
2343  */
2344 int
2345 print_iostat(zpool_handle_t *zhp, void *data)
2346 {
2347 	iostat_cbdata_t *cb = data;
2348 	nvlist_t *oldconfig, *newconfig;
2349 	nvlist_t *oldnvroot, *newnvroot;
2350 
2351 	newconfig = zpool_get_config(zhp, &oldconfig);
2352 
2353 	if (cb->cb_iteration == 1)
2354 		oldconfig = NULL;
2355 
2356 	verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2357 	    &newnvroot) == 0);
2358 
2359 	if (oldconfig == NULL)
2360 		oldnvroot = NULL;
2361 	else
2362 		verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2363 		    &oldnvroot) == 0);
2364 
2365 	/*
2366 	 * Print out the statistics for the pool.
2367 	 */
2368 	print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2369 
2370 	if (cb->cb_verbose)
2371 		print_iostat_separator(cb);
2372 
2373 	return (0);
2374 }
2375 
2376 int
2377 get_namewidth(zpool_handle_t *zhp, void *data)
2378 {
2379 	iostat_cbdata_t *cb = data;
2380 	nvlist_t *config, *nvroot;
2381 
2382 	if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2383 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2384 		    &nvroot) == 0);
2385 		if (!cb->cb_verbose)
2386 			cb->cb_namewidth = strlen(zpool_get_name(zhp));
2387 		else
2388 			cb->cb_namewidth = max_width(zhp, nvroot, 0,
2389 			    cb->cb_namewidth);
2390 	}
2391 
2392 	/*
2393 	 * The width must fall into the range [10,38].  The upper limit is the
2394 	 * maximum we can have and still fit in 80 columns.
2395 	 */
2396 	if (cb->cb_namewidth < 10)
2397 		cb->cb_namewidth = 10;
2398 	if (cb->cb_namewidth > 38)
2399 		cb->cb_namewidth = 38;
2400 
2401 	return (0);
2402 }
2403 
2404 /*
2405  * Parse the input string, get the 'interval' and 'count' value if there is one.
2406  */
2407 static void
2408 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2409     unsigned long *cnt)
2410 {
2411 	unsigned long interval = 0, count = 0;
2412 	int argc = *argcp, errno;
2413 
2414 	/*
2415 	 * Determine if the last argument is an integer or a pool name
2416 	 */
2417 	if (argc > 0 && isdigit(argv[argc - 1][0])) {
2418 		char *end;
2419 
2420 		errno = 0;
2421 		interval = strtoul(argv[argc - 1], &end, 10);
2422 
2423 		if (*end == '\0' && errno == 0) {
2424 			if (interval == 0) {
2425 				(void) fprintf(stderr, gettext("interval "
2426 				    "cannot be zero\n"));
2427 				usage(B_FALSE);
2428 			}
2429 			/*
2430 			 * Ignore the last parameter
2431 			 */
2432 			argc--;
2433 		} else {
2434 			/*
2435 			 * If this is not a valid number, just plow on.  The
2436 			 * user will get a more informative error message later
2437 			 * on.
2438 			 */
2439 			interval = 0;
2440 		}
2441 	}
2442 
2443 	/*
2444 	 * If the last argument is also an integer, then we have both a count
2445 	 * and an interval.
2446 	 */
2447 	if (argc > 0 && isdigit(argv[argc - 1][0])) {
2448 		char *end;
2449 
2450 		errno = 0;
2451 		count = interval;
2452 		interval = strtoul(argv[argc - 1], &end, 10);
2453 
2454 		if (*end == '\0' && errno == 0) {
2455 			if (interval == 0) {
2456 				(void) fprintf(stderr, gettext("interval "
2457 				    "cannot be zero\n"));
2458 				usage(B_FALSE);
2459 			}
2460 
2461 			/*
2462 			 * Ignore the last parameter
2463 			 */
2464 			argc--;
2465 		} else {
2466 			interval = 0;
2467 		}
2468 	}
2469 
2470 	*iv = interval;
2471 	*cnt = count;
2472 	*argcp = argc;
2473 }
2474 
2475 static void
2476 get_timestamp_arg(char c)
2477 {
2478 	if (c == 'u')
2479 		timestamp_fmt = UDATE;
2480 	else if (c == 'd')
2481 		timestamp_fmt = DDATE;
2482 	else
2483 		usage(B_FALSE);
2484 }
2485 
2486 /*
2487  * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2488  *
2489  *	-v	Display statistics for individual vdevs
2490  *	-T	Display a timestamp in date(1) or Unix format
2491  *
2492  * This command can be tricky because we want to be able to deal with pool
2493  * creation/destruction as well as vdev configuration changes.  The bulk of this
2494  * processing is handled by the pool_list_* routines in zpool_iter.c.  We rely
2495  * on pool_list_update() to detect the addition of new pools.  Configuration
2496  * changes are all handled within libzfs.
2497  */
2498 int
2499 zpool_do_iostat(int argc, char **argv)
2500 {
2501 	int c;
2502 	int ret;
2503 	int npools;
2504 	unsigned long interval = 0, count = 0;
2505 	zpool_list_t *list;
2506 	boolean_t verbose = B_FALSE;
2507 	iostat_cbdata_t cb;
2508 
2509 	/* check options */
2510 	while ((c = getopt(argc, argv, "T:v")) != -1) {
2511 		switch (c) {
2512 		case 'T':
2513 			get_timestamp_arg(*optarg);
2514 			break;
2515 		case 'v':
2516 			verbose = B_TRUE;
2517 			break;
2518 		case '?':
2519 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2520 			    optopt);
2521 			usage(B_FALSE);
2522 		}
2523 	}
2524 
2525 	argc -= optind;
2526 	argv += optind;
2527 
2528 	get_interval_count(&argc, argv, &interval, &count);
2529 
2530 	/*
2531 	 * Construct the list of all interesting pools.
2532 	 */
2533 	ret = 0;
2534 	if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2535 		return (1);
2536 
2537 	if (pool_list_count(list) == 0 && argc != 0) {
2538 		pool_list_free(list);
2539 		return (1);
2540 	}
2541 
2542 	if (pool_list_count(list) == 0 && interval == 0) {
2543 		pool_list_free(list);
2544 		(void) fprintf(stderr, gettext("no pools available\n"));
2545 		return (1);
2546 	}
2547 
2548 	/*
2549 	 * Enter the main iostat loop.
2550 	 */
2551 	cb.cb_list = list;
2552 	cb.cb_verbose = verbose;
2553 	cb.cb_iteration = 0;
2554 	cb.cb_namewidth = 0;
2555 
2556 	for (;;) {
2557 		pool_list_update(list);
2558 
2559 		if ((npools = pool_list_count(list)) == 0)
2560 			break;
2561 
2562 		/*
2563 		 * Refresh all statistics.  This is done as an explicit step
2564 		 * before calculating the maximum name width, so that any
2565 		 * configuration changes are properly accounted for.
2566 		 */
2567 		(void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2568 
2569 		/*
2570 		 * Iterate over all pools to determine the maximum width
2571 		 * for the pool / device name column across all pools.
2572 		 */
2573 		cb.cb_namewidth = 0;
2574 		(void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2575 
2576 		if (timestamp_fmt != NODATE)
2577 			print_timestamp(timestamp_fmt);
2578 
2579 		/*
2580 		 * If it's the first time, or verbose mode, print the header.
2581 		 */
2582 		if (++cb.cb_iteration == 1 || verbose)
2583 			print_iostat_header(&cb);
2584 
2585 		(void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2586 
2587 		/*
2588 		 * If there's more than one pool, and we're not in verbose mode
2589 		 * (which prints a separator for us), then print a separator.
2590 		 */
2591 		if (npools > 1 && !verbose)
2592 			print_iostat_separator(&cb);
2593 
2594 		if (verbose)
2595 			(void) printf("\n");
2596 
2597 		/*
2598 		 * Flush the output so that redirection to a file isn't buffered
2599 		 * indefinitely.
2600 		 */
2601 		(void) fflush(stdout);
2602 
2603 		if (interval == 0)
2604 			break;
2605 
2606 		if (count != 0 && --count == 0)
2607 			break;
2608 
2609 		(void) sleep(interval);
2610 	}
2611 
2612 	pool_list_free(list);
2613 
2614 	return (ret);
2615 }
2616 
2617 typedef struct list_cbdata {
2618 	boolean_t	cb_verbose;
2619 	int		cb_namewidth;
2620 	boolean_t	cb_scripted;
2621 	zprop_list_t	*cb_proplist;
2622 	boolean_t	cb_literal;
2623 } list_cbdata_t;
2624 
2625 /*
2626  * Given a list of columns to display, output appropriate headers for each one.
2627  */
2628 static void
2629 print_header(list_cbdata_t *cb)
2630 {
2631 	zprop_list_t *pl = cb->cb_proplist;
2632 	char headerbuf[ZPOOL_MAXPROPLEN];
2633 	const char *header;
2634 	boolean_t first = B_TRUE;
2635 	boolean_t right_justify;
2636 	size_t width = 0;
2637 
2638 	for (; pl != NULL; pl = pl->pl_next) {
2639 		width = pl->pl_width;
2640 		if (first && cb->cb_verbose) {
2641 			/*
2642 			 * Reset the width to accommodate the verbose listing
2643 			 * of devices.
2644 			 */
2645 			width = cb->cb_namewidth;
2646 		}
2647 
2648 		if (!first)
2649 			(void) printf("  ");
2650 		else
2651 			first = B_FALSE;
2652 
2653 		right_justify = B_FALSE;
2654 		if (pl->pl_prop != ZPROP_INVAL) {
2655 			header = zpool_prop_column_name(pl->pl_prop);
2656 			right_justify = zpool_prop_align_right(pl->pl_prop);
2657 		} else {
2658 			int i;
2659 
2660 			for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2661 				headerbuf[i] = toupper(pl->pl_user_prop[i]);
2662 			headerbuf[i] = '\0';
2663 			header = headerbuf;
2664 		}
2665 
2666 		if (pl->pl_next == NULL && !right_justify)
2667 			(void) printf("%s", header);
2668 		else if (right_justify)
2669 			(void) printf("%*s", width, header);
2670 		else
2671 			(void) printf("%-*s", width, header);
2672 
2673 	}
2674 
2675 	(void) printf("\n");
2676 }
2677 
2678 /*
2679  * Given a pool and a list of properties, print out all the properties according
2680  * to the described layout.
2681  */
2682 static void
2683 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2684 {
2685 	zprop_list_t *pl = cb->cb_proplist;
2686 	boolean_t first = B_TRUE;
2687 	char property[ZPOOL_MAXPROPLEN];
2688 	char *propstr;
2689 	boolean_t right_justify;
2690 	size_t width;
2691 
2692 	for (; pl != NULL; pl = pl->pl_next) {
2693 
2694 		width = pl->pl_width;
2695 		if (first && cb->cb_verbose) {
2696 			/*
2697 			 * Reset the width to accommodate the verbose listing
2698 			 * of devices.
2699 			 */
2700 			width = cb->cb_namewidth;
2701 		}
2702 
2703 		if (!first) {
2704 			if (cb->cb_scripted)
2705 				(void) printf("\t");
2706 			else
2707 				(void) printf("  ");
2708 		} else {
2709 			first = B_FALSE;
2710 		}
2711 
2712 		right_justify = B_FALSE;
2713 		if (pl->pl_prop != ZPROP_INVAL) {
2714 			if (zpool_get_prop(zhp, pl->pl_prop, property,
2715 			    sizeof (property), NULL, cb->cb_literal) != 0)
2716 				propstr = "-";
2717 			else
2718 				propstr = property;
2719 
2720 			right_justify = zpool_prop_align_right(pl->pl_prop);
2721 		} else if ((zpool_prop_feature(pl->pl_user_prop) ||
2722 		    zpool_prop_unsupported(pl->pl_user_prop)) &&
2723 		    zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2724 		    sizeof (property)) == 0) {
2725 			propstr = property;
2726 		} else {
2727 			propstr = "-";
2728 		}
2729 
2730 
2731 		/*
2732 		 * If this is being called in scripted mode, or if this is the
2733 		 * last column and it is left-justified, don't include a width
2734 		 * format specifier.
2735 		 */
2736 		if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2737 			(void) printf("%s", propstr);
2738 		else if (right_justify)
2739 			(void) printf("%*s", width, propstr);
2740 		else
2741 			(void) printf("%-*s", width, propstr);
2742 	}
2743 
2744 	(void) printf("\n");
2745 }
2746 
2747 static void
2748 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
2749     boolean_t valid)
2750 {
2751 	char propval[64];
2752 	boolean_t fixed;
2753 	size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
2754 
2755 	switch (prop) {
2756 	case ZPOOL_PROP_EXPANDSZ:
2757 		if (value == 0)
2758 			(void) strlcpy(propval, "-", sizeof (propval));
2759 		else
2760 			zfs_nicenum(value, propval, sizeof (propval));
2761 		break;
2762 	case ZPOOL_PROP_FRAGMENTATION:
2763 		if (value == ZFS_FRAG_INVALID) {
2764 			(void) strlcpy(propval, "-", sizeof (propval));
2765 		} else {
2766 			(void) snprintf(propval, sizeof (propval), "%llu%%",
2767 			    value);
2768 		}
2769 		break;
2770 	case ZPOOL_PROP_CAPACITY:
2771 		(void) snprintf(propval, sizeof (propval), "%llu%%", value);
2772 		break;
2773 	default:
2774 		zfs_nicenum(value, propval, sizeof (propval));
2775 	}
2776 
2777 	if (!valid)
2778 		(void) strlcpy(propval, "-", sizeof (propval));
2779 
2780 	if (scripted)
2781 		(void) printf("\t%s", propval);
2782 	else
2783 		(void) printf("  %*s", width, propval);
2784 }
2785 
2786 void
2787 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
2788     list_cbdata_t *cb, int depth)
2789 {
2790 	nvlist_t **child;
2791 	vdev_stat_t *vs;
2792 	uint_t c, children;
2793 	char *vname;
2794 	boolean_t scripted = cb->cb_scripted;
2795 
2796 	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2797 	    (uint64_t **)&vs, &c) == 0);
2798 
2799 	if (name != NULL) {
2800 		boolean_t toplevel = (vs->vs_space != 0);
2801 		uint64_t cap;
2802 
2803 		if (scripted)
2804 			(void) printf("\t%s", name);
2805 		else if (strlen(name) + depth > cb->cb_namewidth)
2806 			(void) printf("%*s%s", depth, "", name);
2807 		else
2808 			(void) printf("%*s%s%*s", depth, "", name,
2809 			    (int)(cb->cb_namewidth - strlen(name) - depth), "");
2810 
2811 		/*
2812 		 * Print the properties for the individual vdevs. Some
2813 		 * properties are only applicable to toplevel vdevs. The
2814 		 * 'toplevel' boolean value is passed to the print_one_column()
2815 		 * to indicate that the value is valid.
2816 		 */
2817 		print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted,
2818 		    toplevel);
2819 		print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
2820 		    toplevel);
2821 		print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
2822 		    scripted, toplevel);
2823 		print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
2824 		    B_TRUE);
2825 		print_one_column(ZPOOL_PROP_FRAGMENTATION,
2826 		    vs->vs_fragmentation, scripted,
2827 		    (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel));
2828 		cap = (vs->vs_space == 0) ? 0 :
2829 		    (vs->vs_alloc * 100 / vs->vs_space);
2830 		print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel);
2831 		(void) printf("\n");
2832 	}
2833 
2834 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2835 	    &child, &children) != 0)
2836 		return;
2837 
2838 	for (c = 0; c < children; c++) {
2839 		uint64_t ishole = B_FALSE;
2840 
2841 		if (nvlist_lookup_uint64(child[c],
2842 		    ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
2843 			continue;
2844 
2845 		vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
2846 		print_list_stats(zhp, vname, child[c], cb, depth + 2);
2847 		free(vname);
2848 	}
2849 
2850 	/*
2851 	 * Include level 2 ARC devices in iostat output
2852 	 */
2853 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2854 	    &child, &children) != 0)
2855 		return;
2856 
2857 	if (children > 0) {
2858 		(void) printf("%-*s      -      -      -      -      -      "
2859 		    "-\n", cb->cb_namewidth, "cache");
2860 		for (c = 0; c < children; c++) {
2861 			vname = zpool_vdev_name(g_zfs, zhp, child[c],
2862 			    B_FALSE);
2863 			print_list_stats(zhp, vname, child[c], cb, depth + 2);
2864 			free(vname);
2865 		}
2866 	}
2867 }
2868 
2869 
2870 /*
2871  * Generic callback function to list a pool.
2872  */
2873 int
2874 list_callback(zpool_handle_t *zhp, void *data)
2875 {
2876 	list_cbdata_t *cbp = data;
2877 	nvlist_t *config;
2878 	nvlist_t *nvroot;
2879 
2880 	config = zpool_get_config(zhp, NULL);
2881 
2882 	print_pool(zhp, cbp);
2883 	if (!cbp->cb_verbose)
2884 		return (0);
2885 
2886 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2887 	    &nvroot) == 0);
2888 	print_list_stats(zhp, NULL, nvroot, cbp, 0);
2889 
2890 	return (0);
2891 }
2892 
2893 /*
2894  * zpool list [-Hp] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2895  *
2896  *	-H	Scripted mode.  Don't display headers, and separate properties
2897  *		by a single tab.
2898  *	-o	List of properties to display.  Defaults to
2899  *		"name,size,allocated,free,expandsize,fragmentation,capacity,"
2900  *		"dedupratio,health,altroot"
2901  * 	-p	Diplay values in parsable (exact) format.
2902  *	-T	Display a timestamp in date(1) or Unix format
2903  *
2904  * List all pools in the system, whether or not they're healthy.  Output space
2905  * statistics for each one, as well as health status summary.
2906  */
2907 int
2908 zpool_do_list(int argc, char **argv)
2909 {
2910 	int c;
2911 	int ret;
2912 	list_cbdata_t cb = { 0 };
2913 	static char default_props[] =
2914 	    "name,size,allocated,free,expandsize,fragmentation,capacity,"
2915 	    "dedupratio,health,altroot";
2916 	char *props = default_props;
2917 	unsigned long interval = 0, count = 0;
2918 	zpool_list_t *list;
2919 	boolean_t first = B_TRUE;
2920 
2921 	/* check options */
2922 	while ((c = getopt(argc, argv, ":Ho:pT:v")) != -1) {
2923 		switch (c) {
2924 		case 'H':
2925 			cb.cb_scripted = B_TRUE;
2926 			break;
2927 		case 'o':
2928 			props = optarg;
2929 			break;
2930 		case 'p':
2931 			cb.cb_literal = B_TRUE;
2932 			break;
2933 		case 'T':
2934 			get_timestamp_arg(*optarg);
2935 			break;
2936 		case 'v':
2937 			cb.cb_verbose = B_TRUE;
2938 			break;
2939 		case ':':
2940 			(void) fprintf(stderr, gettext("missing argument for "
2941 			    "'%c' option\n"), optopt);
2942 			usage(B_FALSE);
2943 			break;
2944 		case '?':
2945 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2946 			    optopt);
2947 			usage(B_FALSE);
2948 		}
2949 	}
2950 
2951 	argc -= optind;
2952 	argv += optind;
2953 
2954 	get_interval_count(&argc, argv, &interval, &count);
2955 
2956 	if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
2957 		usage(B_FALSE);
2958 
2959 	for (;;) {
2960 		if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
2961 		    &ret)) == NULL)
2962 			return (1);
2963 
2964 		if (pool_list_count(list) == 0)
2965 			break;
2966 
2967 		cb.cb_namewidth = 0;
2968 		(void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2969 
2970 		if (timestamp_fmt != NODATE)
2971 			print_timestamp(timestamp_fmt);
2972 
2973 		if (!cb.cb_scripted && (first || cb.cb_verbose)) {
2974 			print_header(&cb);
2975 			first = B_FALSE;
2976 		}
2977 		ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
2978 
2979 		if (interval == 0)
2980 			break;
2981 
2982 		if (count != 0 && --count == 0)
2983 			break;
2984 
2985 		pool_list_free(list);
2986 		(void) sleep(interval);
2987 	}
2988 
2989 	if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
2990 		(void) printf(gettext("no pools available\n"));
2991 		ret = 0;
2992 	}
2993 
2994 	pool_list_free(list);
2995 	zprop_free_list(cb.cb_proplist);
2996 	return (ret);
2997 }
2998 
2999 static nvlist_t *
3000 zpool_get_vdev_by_name(nvlist_t *nv, char *name)
3001 {
3002 	nvlist_t **child;
3003 	uint_t c, children;
3004 	nvlist_t *match;
3005 	char *path;
3006 
3007 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3008 	    &child, &children) != 0) {
3009 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
3010 		if (strncmp(name, "/dev/dsk/", 9) == 0)
3011 			name += 9;
3012 		if (strncmp(path, "/dev/dsk/", 9) == 0)
3013 			path += 9;
3014 		if (strcmp(name, path) == 0)
3015 			return (nv);
3016 		return (NULL);
3017 	}
3018 
3019 	for (c = 0; c < children; c++)
3020 		if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
3021 			return (match);
3022 
3023 	return (NULL);
3024 }
3025 
3026 static int
3027 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3028 {
3029 	boolean_t force = B_FALSE;
3030 	int c;
3031 	nvlist_t *nvroot;
3032 	char *poolname, *old_disk, *new_disk;
3033 	zpool_handle_t *zhp;
3034 	int ret;
3035 
3036 	/* check options */
3037 	while ((c = getopt(argc, argv, "f")) != -1) {
3038 		switch (c) {
3039 		case 'f':
3040 			force = B_TRUE;
3041 			break;
3042 		case '?':
3043 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3044 			    optopt);
3045 			usage(B_FALSE);
3046 		}
3047 	}
3048 
3049 	argc -= optind;
3050 	argv += optind;
3051 
3052 	/* get pool name and check number of arguments */
3053 	if (argc < 1) {
3054 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
3055 		usage(B_FALSE);
3056 	}
3057 
3058 	poolname = argv[0];
3059 
3060 	if (argc < 2) {
3061 		(void) fprintf(stderr,
3062 		    gettext("missing <device> specification\n"));
3063 		usage(B_FALSE);
3064 	}
3065 
3066 	old_disk = argv[1];
3067 
3068 	if (argc < 3) {
3069 		if (!replacing) {
3070 			(void) fprintf(stderr,
3071 			    gettext("missing <new_device> specification\n"));
3072 			usage(B_FALSE);
3073 		}
3074 		new_disk = old_disk;
3075 		argc -= 1;
3076 		argv += 1;
3077 	} else {
3078 		new_disk = argv[2];
3079 		argc -= 2;
3080 		argv += 2;
3081 	}
3082 
3083 	if (argc > 1) {
3084 		(void) fprintf(stderr, gettext("too many arguments\n"));
3085 		usage(B_FALSE);
3086 	}
3087 
3088 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3089 		return (1);
3090 
3091 	if (zpool_get_config(zhp, NULL) == NULL) {
3092 		(void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3093 		    poolname);
3094 		zpool_close(zhp);
3095 		return (1);
3096 	}
3097 
3098 	nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
3099 	    argc, argv);
3100 	if (nvroot == NULL) {
3101 		zpool_close(zhp);
3102 		return (1);
3103 	}
3104 
3105 	ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3106 
3107 	nvlist_free(nvroot);
3108 	zpool_close(zhp);
3109 
3110 	return (ret);
3111 }
3112 
3113 /*
3114  * zpool replace [-f] <pool> <device> <new_device>
3115  *
3116  *	-f	Force attach, even if <new_device> appears to be in use.
3117  *
3118  * Replace <device> with <new_device>.
3119  */
3120 /* ARGSUSED */
3121 int
3122 zpool_do_replace(int argc, char **argv)
3123 {
3124 	return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3125 }
3126 
3127 /*
3128  * zpool attach [-f] <pool> <device> <new_device>
3129  *
3130  *	-f	Force attach, even if <new_device> appears to be in use.
3131  *
3132  * Attach <new_device> to the mirror containing <device>.  If <device> is not
3133  * part of a mirror, then <device> will be transformed into a mirror of
3134  * <device> and <new_device>.  In either case, <new_device> will begin life
3135  * with a DTL of [0, now], and will immediately begin to resilver itself.
3136  */
3137 int
3138 zpool_do_attach(int argc, char **argv)
3139 {
3140 	return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3141 }
3142 
3143 /*
3144  * zpool detach [-f] <pool> <device>
3145  *
3146  *	-f	Force detach of <device>, even if DTLs argue against it
3147  *		(not supported yet)
3148  *
3149  * Detach a device from a mirror.  The operation will be refused if <device>
3150  * is the last device in the mirror, or if the DTLs indicate that this device
3151  * has the only valid copy of some data.
3152  */
3153 /* ARGSUSED */
3154 int
3155 zpool_do_detach(int argc, char **argv)
3156 {
3157 	int c;
3158 	char *poolname, *path;
3159 	zpool_handle_t *zhp;
3160 	int ret;
3161 
3162 	/* check options */
3163 	while ((c = getopt(argc, argv, "f")) != -1) {
3164 		switch (c) {
3165 		case 'f':
3166 		case '?':
3167 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3168 			    optopt);
3169 			usage(B_FALSE);
3170 		}
3171 	}
3172 
3173 	argc -= optind;
3174 	argv += optind;
3175 
3176 	/* get pool name and check number of arguments */
3177 	if (argc < 1) {
3178 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
3179 		usage(B_FALSE);
3180 	}
3181 
3182 	if (argc < 2) {
3183 		(void) fprintf(stderr,
3184 		    gettext("missing <device> specification\n"));
3185 		usage(B_FALSE);
3186 	}
3187 
3188 	poolname = argv[0];
3189 	path = argv[1];
3190 
3191 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3192 		return (1);
3193 
3194 	ret = zpool_vdev_detach(zhp, path);
3195 
3196 	zpool_close(zhp);
3197 
3198 	return (ret);
3199 }
3200 
3201 /*
3202  * zpool split [-n] [-o prop=val] ...
3203  *		[-o mntopt] ...
3204  *		[-R altroot] <pool> <newpool> [<device> ...]
3205  *
3206  *	-n	Do not split the pool, but display the resulting layout if
3207  *		it were to be split.
3208  *	-o	Set property=value, or set mount options.
3209  *	-R	Mount the split-off pool under an alternate root.
3210  *
3211  * Splits the named pool and gives it the new pool name.  Devices to be split
3212  * off may be listed, provided that no more than one device is specified
3213  * per top-level vdev mirror.  The newly split pool is left in an exported
3214  * state unless -R is specified.
3215  *
3216  * Restrictions: the top-level of the pool pool must only be made up of
3217  * mirrors; all devices in the pool must be healthy; no device may be
3218  * undergoing a resilvering operation.
3219  */
3220 int
3221 zpool_do_split(int argc, char **argv)
3222 {
3223 	char *srcpool, *newpool, *propval;
3224 	char *mntopts = NULL;
3225 	splitflags_t flags;
3226 	int c, ret = 0;
3227 	zpool_handle_t *zhp;
3228 	nvlist_t *config, *props = NULL;
3229 
3230 	flags.dryrun = B_FALSE;
3231 	flags.import = B_FALSE;
3232 
3233 	/* check options */
3234 	while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3235 		switch (c) {
3236 		case 'R':
3237 			flags.import = B_TRUE;
3238 			if (add_prop_list(
3239 			    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3240 			    &props, B_TRUE) != 0) {
3241 				if (props)
3242 					nvlist_free(props);
3243 				usage(B_FALSE);
3244 			}
3245 			break;
3246 		case 'n':
3247 			flags.dryrun = B_TRUE;
3248 			break;
3249 		case 'o':
3250 			if ((propval = strchr(optarg, '=')) != NULL) {
3251 				*propval = '\0';
3252 				propval++;
3253 				if (add_prop_list(optarg, propval,
3254 				    &props, B_TRUE) != 0) {
3255 					if (props)
3256 						nvlist_free(props);
3257 					usage(B_FALSE);
3258 				}
3259 			} else {
3260 				mntopts = optarg;
3261 			}
3262 			break;
3263 		case ':':
3264 			(void) fprintf(stderr, gettext("missing argument for "
3265 			    "'%c' option\n"), optopt);
3266 			usage(B_FALSE);
3267 			break;
3268 		case '?':
3269 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3270 			    optopt);
3271 			usage(B_FALSE);
3272 			break;
3273 		}
3274 	}
3275 
3276 	if (!flags.import && mntopts != NULL) {
3277 		(void) fprintf(stderr, gettext("setting mntopts is only "
3278 		    "valid when importing the pool\n"));
3279 		usage(B_FALSE);
3280 	}
3281 
3282 	argc -= optind;
3283 	argv += optind;
3284 
3285 	if (argc < 1) {
3286 		(void) fprintf(stderr, gettext("Missing pool name\n"));
3287 		usage(B_FALSE);
3288 	}
3289 	if (argc < 2) {
3290 		(void) fprintf(stderr, gettext("Missing new pool name\n"));
3291 		usage(B_FALSE);
3292 	}
3293 
3294 	srcpool = argv[0];
3295 	newpool = argv[1];
3296 
3297 	argc -= 2;
3298 	argv += 2;
3299 
3300 	if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3301 		return (1);
3302 
3303 	config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3304 	if (config == NULL) {
3305 		ret = 1;
3306 	} else {
3307 		if (flags.dryrun) {
3308 			(void) printf(gettext("would create '%s' with the "
3309 			    "following layout:\n\n"), newpool);
3310 			print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3311 		}
3312 		nvlist_free(config);
3313 	}
3314 
3315 	zpool_close(zhp);
3316 
3317 	if (ret != 0 || flags.dryrun || !flags.import)
3318 		return (ret);
3319 
3320 	/*
3321 	 * The split was successful. Now we need to open the new
3322 	 * pool and import it.
3323 	 */
3324 	if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3325 		return (1);
3326 	if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3327 	    zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3328 		ret = 1;
3329 		(void) fprintf(stderr, gettext("Split was successful, but "
3330 		    "the datasets could not all be mounted\n"));
3331 		(void) fprintf(stderr, gettext("Try doing '%s' with a "
3332 		    "different altroot\n"), "zpool import");
3333 	}
3334 	zpool_close(zhp);
3335 
3336 	return (ret);
3337 }
3338 
3339 
3340 
3341 /*
3342  * zpool online <pool> <device> ...
3343  */
3344 int
3345 zpool_do_online(int argc, char **argv)
3346 {
3347 	int c, i;
3348 	char *poolname;
3349 	zpool_handle_t *zhp;
3350 	int ret = 0;
3351 	vdev_state_t newstate;
3352 	int flags = 0;
3353 
3354 	/* check options */
3355 	while ((c = getopt(argc, argv, "et")) != -1) {
3356 		switch (c) {
3357 		case 'e':
3358 			flags |= ZFS_ONLINE_EXPAND;
3359 			break;
3360 		case 't':
3361 		case '?':
3362 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3363 			    optopt);
3364 			usage(B_FALSE);
3365 		}
3366 	}
3367 
3368 	argc -= optind;
3369 	argv += optind;
3370 
3371 	/* get pool name and check number of arguments */
3372 	if (argc < 1) {
3373 		(void) fprintf(stderr, gettext("missing pool name\n"));
3374 		usage(B_FALSE);
3375 	}
3376 	if (argc < 2) {
3377 		(void) fprintf(stderr, gettext("missing device name\n"));
3378 		usage(B_FALSE);
3379 	}
3380 
3381 	poolname = argv[0];
3382 
3383 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3384 		return (1);
3385 
3386 	for (i = 1; i < argc; i++) {
3387 		if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3388 			if (newstate != VDEV_STATE_HEALTHY) {
3389 				(void) printf(gettext("warning: device '%s' "
3390 				    "onlined, but remains in faulted state\n"),
3391 				    argv[i]);
3392 				if (newstate == VDEV_STATE_FAULTED)
3393 					(void) printf(gettext("use 'zpool "
3394 					    "clear' to restore a faulted "
3395 					    "device\n"));
3396 				else
3397 					(void) printf(gettext("use 'zpool "
3398 					    "replace' to replace devices "
3399 					    "that are no longer present\n"));
3400 			}
3401 		} else {
3402 			ret = 1;
3403 		}
3404 	}
3405 
3406 	zpool_close(zhp);
3407 
3408 	return (ret);
3409 }
3410 
3411 /*
3412  * zpool offline [-ft] <pool> <device> ...
3413  *
3414  *	-f	Force the device into the offline state, even if doing
3415  *		so would appear to compromise pool availability.
3416  *		(not supported yet)
3417  *
3418  *	-t	Only take the device off-line temporarily.  The offline
3419  *		state will not be persistent across reboots.
3420  */
3421 /* ARGSUSED */
3422 int
3423 zpool_do_offline(int argc, char **argv)
3424 {
3425 	int c, i;
3426 	char *poolname;
3427 	zpool_handle_t *zhp;
3428 	int ret = 0;
3429 	boolean_t istmp = B_FALSE;
3430 
3431 	/* check options */
3432 	while ((c = getopt(argc, argv, "ft")) != -1) {
3433 		switch (c) {
3434 		case 't':
3435 			istmp = B_TRUE;
3436 			break;
3437 		case 'f':
3438 		case '?':
3439 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3440 			    optopt);
3441 			usage(B_FALSE);
3442 		}
3443 	}
3444 
3445 	argc -= optind;
3446 	argv += optind;
3447 
3448 	/* get pool name and check number of arguments */
3449 	if (argc < 1) {
3450 		(void) fprintf(stderr, gettext("missing pool name\n"));
3451 		usage(B_FALSE);
3452 	}
3453 	if (argc < 2) {
3454 		(void) fprintf(stderr, gettext("missing device name\n"));
3455 		usage(B_FALSE);
3456 	}
3457 
3458 	poolname = argv[0];
3459 
3460 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3461 		return (1);
3462 
3463 	for (i = 1; i < argc; i++) {
3464 		if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3465 			ret = 1;
3466 	}
3467 
3468 	zpool_close(zhp);
3469 
3470 	return (ret);
3471 }
3472 
3473 /*
3474  * zpool clear <pool> [device]
3475  *
3476  * Clear all errors associated with a pool or a particular device.
3477  */
3478 int
3479 zpool_do_clear(int argc, char **argv)
3480 {
3481 	int c;
3482 	int ret = 0;
3483 	boolean_t dryrun = B_FALSE;
3484 	boolean_t do_rewind = B_FALSE;
3485 	boolean_t xtreme_rewind = B_FALSE;
3486 	uint32_t rewind_policy = ZPOOL_NO_REWIND;
3487 	nvlist_t *policy = NULL;
3488 	zpool_handle_t *zhp;
3489 	char *pool, *device;
3490 
3491 	/* check options */
3492 	while ((c = getopt(argc, argv, "FnX")) != -1) {
3493 		switch (c) {
3494 		case 'F':
3495 			do_rewind = B_TRUE;
3496 			break;
3497 		case 'n':
3498 			dryrun = B_TRUE;
3499 			break;
3500 		case 'X':
3501 			xtreme_rewind = B_TRUE;
3502 			break;
3503 		case '?':
3504 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3505 			    optopt);
3506 			usage(B_FALSE);
3507 		}
3508 	}
3509 
3510 	argc -= optind;
3511 	argv += optind;
3512 
3513 	if (argc < 1) {
3514 		(void) fprintf(stderr, gettext("missing pool name\n"));
3515 		usage(B_FALSE);
3516 	}
3517 
3518 	if (argc > 2) {
3519 		(void) fprintf(stderr, gettext("too many arguments\n"));
3520 		usage(B_FALSE);
3521 	}
3522 
3523 	if ((dryrun || xtreme_rewind) && !do_rewind) {
3524 		(void) fprintf(stderr,
3525 		    gettext("-n or -X only meaningful with -F\n"));
3526 		usage(B_FALSE);
3527 	}
3528 	if (dryrun)
3529 		rewind_policy = ZPOOL_TRY_REWIND;
3530 	else if (do_rewind)
3531 		rewind_policy = ZPOOL_DO_REWIND;
3532 	if (xtreme_rewind)
3533 		rewind_policy |= ZPOOL_EXTREME_REWIND;
3534 
3535 	/* In future, further rewind policy choices can be passed along here */
3536 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3537 	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3538 		return (1);
3539 
3540 	pool = argv[0];
3541 	device = argc == 2 ? argv[1] : NULL;
3542 
3543 	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3544 		nvlist_free(policy);
3545 		return (1);
3546 	}
3547 
3548 	if (zpool_clear(zhp, device, policy) != 0)
3549 		ret = 1;
3550 
3551 	zpool_close(zhp);
3552 
3553 	nvlist_free(policy);
3554 
3555 	return (ret);
3556 }
3557 
3558 /*
3559  * zpool reguid <pool>
3560  */
3561 int
3562 zpool_do_reguid(int argc, char **argv)
3563 {
3564 	int c;
3565 	char *poolname;
3566 	zpool_handle_t *zhp;
3567 	int ret = 0;
3568 
3569 	/* check options */
3570 	while ((c = getopt(argc, argv, "")) != -1) {
3571 		switch (c) {
3572 		case '?':
3573 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3574 			    optopt);
3575 			usage(B_FALSE);
3576 		}
3577 	}
3578 
3579 	argc -= optind;
3580 	argv += optind;
3581 
3582 	/* get pool name and check number of arguments */
3583 	if (argc < 1) {
3584 		(void) fprintf(stderr, gettext("missing pool name\n"));
3585 		usage(B_FALSE);
3586 	}
3587 
3588 	if (argc > 1) {
3589 		(void) fprintf(stderr, gettext("too many arguments\n"));
3590 		usage(B_FALSE);
3591 	}
3592 
3593 	poolname = argv[0];
3594 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3595 		return (1);
3596 
3597 	ret = zpool_reguid(zhp);
3598 
3599 	zpool_close(zhp);
3600 	return (ret);
3601 }
3602 
3603 
3604 /*
3605  * zpool reopen <pool>
3606  *
3607  * Reopen the pool so that the kernel can update the sizes of all vdevs.
3608  */
3609 int
3610 zpool_do_reopen(int argc, char **argv)
3611 {
3612 	int c;
3613 	int ret = 0;
3614 	zpool_handle_t *zhp;
3615 	char *pool;
3616 
3617 	/* check options */
3618 	while ((c = getopt(argc, argv, "")) != -1) {
3619 		switch (c) {
3620 		case '?':
3621 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3622 			    optopt);
3623 			usage(B_FALSE);
3624 		}
3625 	}
3626 
3627 	argc--;
3628 	argv++;
3629 
3630 	if (argc < 1) {
3631 		(void) fprintf(stderr, gettext("missing pool name\n"));
3632 		usage(B_FALSE);
3633 	}
3634 
3635 	if (argc > 1) {
3636 		(void) fprintf(stderr, gettext("too many arguments\n"));
3637 		usage(B_FALSE);
3638 	}
3639 
3640 	pool = argv[0];
3641 	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3642 		return (1);
3643 
3644 	ret = zpool_reopen(zhp);
3645 	zpool_close(zhp);
3646 	return (ret);
3647 }
3648 
3649 typedef struct scrub_cbdata {
3650 	int	cb_type;
3651 	int	cb_argc;
3652 	char	**cb_argv;
3653 } scrub_cbdata_t;
3654 
3655 int
3656 scrub_callback(zpool_handle_t *zhp, void *data)
3657 {
3658 	scrub_cbdata_t *cb = data;
3659 	int err;
3660 
3661 	/*
3662 	 * Ignore faulted pools.
3663 	 */
3664 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3665 		(void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3666 		    "currently unavailable\n"), zpool_get_name(zhp));
3667 		return (1);
3668 	}
3669 
3670 	err = zpool_scan(zhp, cb->cb_type);
3671 
3672 	return (err != 0);
3673 }
3674 
3675 /*
3676  * zpool scrub [-s] <pool> ...
3677  *
3678  *	-s	Stop.  Stops any in-progress scrub.
3679  */
3680 int
3681 zpool_do_scrub(int argc, char **argv)
3682 {
3683 	int c;
3684 	scrub_cbdata_t cb;
3685 
3686 	cb.cb_type = POOL_SCAN_SCRUB;
3687 
3688 	/* check options */
3689 	while ((c = getopt(argc, argv, "s")) != -1) {
3690 		switch (c) {
3691 		case 's':
3692 			cb.cb_type = POOL_SCAN_NONE;
3693 			break;
3694 		case '?':
3695 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3696 			    optopt);
3697 			usage(B_FALSE);
3698 		}
3699 	}
3700 
3701 	cb.cb_argc = argc;
3702 	cb.cb_argv = argv;
3703 	argc -= optind;
3704 	argv += optind;
3705 
3706 	if (argc < 1) {
3707 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
3708 		usage(B_FALSE);
3709 	}
3710 
3711 	return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3712 }
3713 
3714 typedef struct status_cbdata {
3715 	int		cb_count;
3716 	boolean_t	cb_allpools;
3717 	boolean_t	cb_verbose;
3718 	boolean_t	cb_explain;
3719 	boolean_t	cb_first;
3720 	boolean_t	cb_dedup_stats;
3721 } status_cbdata_t;
3722 
3723 /*
3724  * Print out detailed scrub status.
3725  */
3726 void
3727 print_scan_status(pool_scan_stat_t *ps)
3728 {
3729 	time_t start, end;
3730 	uint64_t elapsed, mins_left, hours_left;
3731 	uint64_t pass_exam, examined, total;
3732 	uint_t rate;
3733 	double fraction_done;
3734 	char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3735 
3736 	(void) printf(gettext("  scan: "));
3737 
3738 	/* If there's never been a scan, there's not much to say. */
3739 	if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3740 	    ps->pss_func >= POOL_SCAN_FUNCS) {
3741 		(void) printf(gettext("none requested\n"));
3742 		return;
3743 	}
3744 
3745 	start = ps->pss_start_time;
3746 	end = ps->pss_end_time;
3747 	zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3748 
3749 	assert(ps->pss_func == POOL_SCAN_SCRUB ||
3750 	    ps->pss_func == POOL_SCAN_RESILVER);
3751 	/*
3752 	 * Scan is finished or canceled.
3753 	 */
3754 	if (ps->pss_state == DSS_FINISHED) {
3755 		uint64_t minutes_taken = (end - start) / 60;
3756 		char *fmt;
3757 
3758 		if (ps->pss_func == POOL_SCAN_SCRUB) {
3759 			fmt = gettext("scrub repaired %s in %lluh%um with "
3760 			    "%llu errors on %s");
3761 		} else if (ps->pss_func == POOL_SCAN_RESILVER) {
3762 			fmt = gettext("resilvered %s in %lluh%um with "
3763 			    "%llu errors on %s");
3764 		}
3765 		/* LINTED */
3766 		(void) printf(fmt, processed_buf,
3767 		    (u_longlong_t)(minutes_taken / 60),
3768 		    (uint_t)(minutes_taken % 60),
3769 		    (u_longlong_t)ps->pss_errors,
3770 		    ctime((time_t *)&end));
3771 		return;
3772 	} else if (ps->pss_state == DSS_CANCELED) {
3773 		if (ps->pss_func == POOL_SCAN_SCRUB) {
3774 			(void) printf(gettext("scrub canceled on %s"),
3775 			    ctime(&end));
3776 		} else if (ps->pss_func == POOL_SCAN_RESILVER) {
3777 			(void) printf(gettext("resilver canceled on %s"),
3778 			    ctime(&end));
3779 		}
3780 		return;
3781 	}
3782 
3783 	assert(ps->pss_state == DSS_SCANNING);
3784 
3785 	/*
3786 	 * Scan is in progress.
3787 	 */
3788 	if (ps->pss_func == POOL_SCAN_SCRUB) {
3789 		(void) printf(gettext("scrub in progress since %s"),
3790 		    ctime(&start));
3791 	} else if (ps->pss_func == POOL_SCAN_RESILVER) {
3792 		(void) printf(gettext("resilver in progress since %s"),
3793 		    ctime(&start));
3794 	}
3795 
3796 	examined = ps->pss_examined ? ps->pss_examined : 1;
3797 	total = ps->pss_to_examine;
3798 	fraction_done = (double)examined / total;
3799 
3800 	/* elapsed time for this pass */
3801 	elapsed = time(NULL) - ps->pss_pass_start;
3802 	elapsed = elapsed ? elapsed : 1;
3803 	pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3804 	rate = pass_exam / elapsed;
3805 	rate = rate ? rate : 1;
3806 	mins_left = ((total - examined) / rate) / 60;
3807 	hours_left = mins_left / 60;
3808 
3809 	zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3810 	zfs_nicenum(total, total_buf, sizeof (total_buf));
3811 	zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3812 
3813 	/*
3814 	 * do not print estimated time if hours_left is more than 30 days
3815 	 */
3816 	(void) printf(gettext("    %s scanned out of %s at %s/s"),
3817 	    examined_buf, total_buf, rate_buf);
3818 	if (hours_left < (30 * 24)) {
3819 		(void) printf(gettext(", %lluh%um to go\n"),
3820 		    (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3821 	} else {
3822 		(void) printf(gettext(
3823 		    ", (scan is slow, no estimated time)\n"));
3824 	}
3825 
3826 	if (ps->pss_func == POOL_SCAN_RESILVER) {
3827 		(void) printf(gettext("    %s resilvered, %.2f%% done\n"),
3828 		    processed_buf, 100 * fraction_done);
3829 	} else if (ps->pss_func == POOL_SCAN_SCRUB) {
3830 		(void) printf(gettext("    %s repaired, %.2f%% done\n"),
3831 		    processed_buf, 100 * fraction_done);
3832 	}
3833 }
3834 
3835 static void
3836 print_error_log(zpool_handle_t *zhp)
3837 {
3838 	nvlist_t *nverrlist = NULL;
3839 	nvpair_t *elem;
3840 	char *pathname;
3841 	size_t len = MAXPATHLEN * 2;
3842 
3843 	if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3844 		(void) printf("errors: List of errors unavailable "
3845 		    "(insufficient privileges)\n");
3846 		return;
3847 	}
3848 
3849 	(void) printf("errors: Permanent errors have been "
3850 	    "detected in the following files:\n\n");
3851 
3852 	pathname = safe_malloc(len);
3853 	elem = NULL;
3854 	while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
3855 		nvlist_t *nv;
3856 		uint64_t dsobj, obj;
3857 
3858 		verify(nvpair_value_nvlist(elem, &nv) == 0);
3859 		verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
3860 		    &dsobj) == 0);
3861 		verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
3862 		    &obj) == 0);
3863 		zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
3864 		(void) printf("%7s %s\n", "", pathname);
3865 	}
3866 	free(pathname);
3867 	nvlist_free(nverrlist);
3868 }
3869 
3870 static void
3871 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
3872     int namewidth)
3873 {
3874 	uint_t i;
3875 	char *name;
3876 
3877 	if (nspares == 0)
3878 		return;
3879 
3880 	(void) printf(gettext("\tspares\n"));
3881 
3882 	for (i = 0; i < nspares; i++) {
3883 		name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
3884 		print_status_config(zhp, name, spares[i],
3885 		    namewidth, 2, B_TRUE);
3886 		free(name);
3887 	}
3888 }
3889 
3890 static void
3891 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3892     int namewidth)
3893 {
3894 	uint_t i;
3895 	char *name;
3896 
3897 	if (nl2cache == 0)
3898 		return;
3899 
3900 	(void) printf(gettext("\tcache\n"));
3901 
3902 	for (i = 0; i < nl2cache; i++) {
3903 		name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3904 		print_status_config(zhp, name, l2cache[i],
3905 		    namewidth, 2, B_FALSE);
3906 		free(name);
3907 	}
3908 }
3909 
3910 static void
3911 print_dedup_stats(nvlist_t *config)
3912 {
3913 	ddt_histogram_t *ddh;
3914 	ddt_stat_t *dds;
3915 	ddt_object_t *ddo;
3916 	uint_t c;
3917 
3918 	/*
3919 	 * If the pool was faulted then we may not have been able to
3920 	 * obtain the config. Otherwise, if we have anything in the dedup
3921 	 * table continue processing the stats.
3922 	 */
3923 	if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
3924 	    (uint64_t **)&ddo, &c) != 0)
3925 		return;
3926 
3927 	(void) printf("\n");
3928 	(void) printf(gettext(" dedup: "));
3929 	if (ddo->ddo_count == 0) {
3930 		(void) printf(gettext("no DDT entries\n"));
3931 		return;
3932 	}
3933 
3934 	(void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
3935 	    (u_longlong_t)ddo->ddo_count,
3936 	    (u_longlong_t)ddo->ddo_dspace,
3937 	    (u_longlong_t)ddo->ddo_mspace);
3938 
3939 	verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
3940 	    (uint64_t **)&dds, &c) == 0);
3941 	verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
3942 	    (uint64_t **)&ddh, &c) == 0);
3943 	zpool_dump_ddt(dds, ddh);
3944 }
3945 
3946 /*
3947  * Display a summary of pool status.  Displays a summary such as:
3948  *
3949  *        pool: tank
3950  *	status: DEGRADED
3951  *	reason: One or more devices ...
3952  *         see: http://illumos.org/msg/ZFS-xxxx-01
3953  *	config:
3954  *		mirror		DEGRADED
3955  *                c1t0d0	OK
3956  *                c2t0d0	UNAVAIL
3957  *
3958  * When given the '-v' option, we print out the complete config.  If the '-e'
3959  * option is specified, then we print out error rate information as well.
3960  */
3961 int
3962 status_callback(zpool_handle_t *zhp, void *data)
3963 {
3964 	status_cbdata_t *cbp = data;
3965 	nvlist_t *config, *nvroot;
3966 	char *msgid;
3967 	int reason;
3968 	const char *health;
3969 	uint_t c;
3970 	vdev_stat_t *vs;
3971 
3972 	config = zpool_get_config(zhp, NULL);
3973 	reason = zpool_get_status(zhp, &msgid);
3974 
3975 	cbp->cb_count++;
3976 
3977 	/*
3978 	 * If we were given 'zpool status -x', only report those pools with
3979 	 * problems.
3980 	 */
3981 	if (cbp->cb_explain &&
3982 	    (reason == ZPOOL_STATUS_OK ||
3983 	    reason == ZPOOL_STATUS_VERSION_OLDER ||
3984 	    reason == ZPOOL_STATUS_FEAT_DISABLED)) {
3985 		if (!cbp->cb_allpools) {
3986 			(void) printf(gettext("pool '%s' is healthy\n"),
3987 			    zpool_get_name(zhp));
3988 			if (cbp->cb_first)
3989 				cbp->cb_first = B_FALSE;
3990 		}
3991 		return (0);
3992 	}
3993 
3994 	if (cbp->cb_first)
3995 		cbp->cb_first = B_FALSE;
3996 	else
3997 		(void) printf("\n");
3998 
3999 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4000 	    &nvroot) == 0);
4001 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
4002 	    (uint64_t **)&vs, &c) == 0);
4003 	health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4004 
4005 	(void) printf(gettext("  pool: %s\n"), zpool_get_name(zhp));
4006 	(void) printf(gettext(" state: %s\n"), health);
4007 
4008 	switch (reason) {
4009 	case ZPOOL_STATUS_MISSING_DEV_R:
4010 		(void) printf(gettext("status: One or more devices could not "
4011 		    "be opened.  Sufficient replicas exist for\n\tthe pool to "
4012 		    "continue functioning in a degraded state.\n"));
4013 		(void) printf(gettext("action: Attach the missing device and "
4014 		    "online it using 'zpool online'.\n"));
4015 		break;
4016 
4017 	case ZPOOL_STATUS_MISSING_DEV_NR:
4018 		(void) printf(gettext("status: One or more devices could not "
4019 		    "be opened.  There are insufficient\n\treplicas for the "
4020 		    "pool to continue functioning.\n"));
4021 		(void) printf(gettext("action: Attach the missing device and "
4022 		    "online it using 'zpool online'.\n"));
4023 		break;
4024 
4025 	case ZPOOL_STATUS_CORRUPT_LABEL_R:
4026 		(void) printf(gettext("status: One or more devices could not "
4027 		    "be used because the label is missing or\n\tinvalid.  "
4028 		    "Sufficient replicas exist for the pool to continue\n\t"
4029 		    "functioning in a degraded state.\n"));
4030 		(void) printf(gettext("action: Replace the device using "
4031 		    "'zpool replace'.\n"));
4032 		break;
4033 
4034 	case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4035 		(void) printf(gettext("status: One or more devices could not "
4036 		    "be used because the label is missing \n\tor invalid.  "
4037 		    "There are insufficient replicas for the pool to "
4038 		    "continue\n\tfunctioning.\n"));
4039 		zpool_explain_recover(zpool_get_handle(zhp),
4040 		    zpool_get_name(zhp), reason, config);
4041 		break;
4042 
4043 	case ZPOOL_STATUS_FAILING_DEV:
4044 		(void) printf(gettext("status: One or more devices has "
4045 		    "experienced an unrecoverable error.  An\n\tattempt was "
4046 		    "made to correct the error.  Applications are "
4047 		    "unaffected.\n"));
4048 		(void) printf(gettext("action: Determine if the device needs "
4049 		    "to be replaced, and clear the errors\n\tusing "
4050 		    "'zpool clear' or replace the device with 'zpool "
4051 		    "replace'.\n"));
4052 		break;
4053 
4054 	case ZPOOL_STATUS_OFFLINE_DEV:
4055 		(void) printf(gettext("status: One or more devices has "
4056 		    "been taken offline by the administrator.\n\tSufficient "
4057 		    "replicas exist for the pool to continue functioning in "
4058 		    "a\n\tdegraded state.\n"));
4059 		(void) printf(gettext("action: Online the device using "
4060 		    "'zpool online' or replace the device with\n\t'zpool "
4061 		    "replace'.\n"));
4062 		break;
4063 
4064 	case ZPOOL_STATUS_REMOVED_DEV:
4065 		(void) printf(gettext("status: One or more devices has "
4066 		    "been removed by the administrator.\n\tSufficient "
4067 		    "replicas exist for the pool to continue functioning in "
4068 		    "a\n\tdegraded state.\n"));
4069 		(void) printf(gettext("action: Online the device using "
4070 		    "'zpool online' or replace the device with\n\t'zpool "
4071 		    "replace'.\n"));
4072 		break;
4073 
4074 	case ZPOOL_STATUS_RESILVERING:
4075 		(void) printf(gettext("status: One or more devices is "
4076 		    "currently being resilvered.  The pool will\n\tcontinue "
4077 		    "to function, possibly in a degraded state.\n"));
4078 		(void) printf(gettext("action: Wait for the resilver to "
4079 		    "complete.\n"));
4080 		break;
4081 
4082 	case ZPOOL_STATUS_CORRUPT_DATA:
4083 		(void) printf(gettext("status: One or more devices has "
4084 		    "experienced an error resulting in data\n\tcorruption.  "
4085 		    "Applications may be affected.\n"));
4086 		(void) printf(gettext("action: Restore the file in question "
4087 		    "if possible.  Otherwise restore the\n\tentire pool from "
4088 		    "backup.\n"));
4089 		break;
4090 
4091 	case ZPOOL_STATUS_CORRUPT_POOL:
4092 		(void) printf(gettext("status: The pool metadata is corrupted "
4093 		    "and the pool cannot be opened.\n"));
4094 		zpool_explain_recover(zpool_get_handle(zhp),
4095 		    zpool_get_name(zhp), reason, config);
4096 		break;
4097 
4098 	case ZPOOL_STATUS_VERSION_OLDER:
4099 		(void) printf(gettext("status: The pool is formatted using a "
4100 		    "legacy on-disk format.  The pool can\n\tstill be used, "
4101 		    "but some features are unavailable.\n"));
4102 		(void) printf(gettext("action: Upgrade the pool using 'zpool "
4103 		    "upgrade'.  Once this is done, the\n\tpool will no longer "
4104 		    "be accessible on software that does not support feature\n"
4105 		    "\tflags.\n"));
4106 		break;
4107 
4108 	case ZPOOL_STATUS_VERSION_NEWER:
4109 		(void) printf(gettext("status: The pool has been upgraded to a "
4110 		    "newer, incompatible on-disk version.\n\tThe pool cannot "
4111 		    "be accessed on this system.\n"));
4112 		(void) printf(gettext("action: Access the pool from a system "
4113 		    "running more recent software, or\n\trestore the pool from "
4114 		    "backup.\n"));
4115 		break;
4116 
4117 	case ZPOOL_STATUS_FEAT_DISABLED:
4118 		(void) printf(gettext("status: Some supported features are not "
4119 		    "enabled on the pool. The pool can\n\tstill be used, but "
4120 		    "some features are unavailable.\n"));
4121 		(void) printf(gettext("action: Enable all features using "
4122 		    "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4123 		    "longer be accessible by software that does not support\n\t"
4124 		    "the features. See zpool-features(5) for details.\n"));
4125 		break;
4126 
4127 	case ZPOOL_STATUS_UNSUP_FEAT_READ:
4128 		(void) printf(gettext("status: The pool cannot be accessed on "
4129 		    "this system because it uses the\n\tfollowing feature(s) "
4130 		    "not supported on this system:\n"));
4131 		zpool_print_unsup_feat(config);
4132 		(void) printf("\n");
4133 		(void) printf(gettext("action: Access the pool from a system "
4134 		    "that supports the required feature(s),\n\tor restore the "
4135 		    "pool from backup.\n"));
4136 		break;
4137 
4138 	case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4139 		(void) printf(gettext("status: The pool can only be accessed "
4140 		    "in read-only mode on this system. It\n\tcannot be "
4141 		    "accessed in read-write mode because it uses the "
4142 		    "following\n\tfeature(s) not supported on this system:\n"));
4143 		zpool_print_unsup_feat(config);
4144 		(void) printf("\n");
4145 		(void) printf(gettext("action: The pool cannot be accessed in "
4146 		    "read-write mode. Import the pool with\n"
4147 		    "\t\"-o readonly=on\", access the pool from a system that "
4148 		    "supports the\n\trequired feature(s), or restore the "
4149 		    "pool from backup.\n"));
4150 		break;
4151 
4152 	case ZPOOL_STATUS_FAULTED_DEV_R:
4153 		(void) printf(gettext("status: One or more devices are "
4154 		    "faulted in response to persistent errors.\n\tSufficient "
4155 		    "replicas exist for the pool to continue functioning "
4156 		    "in a\n\tdegraded state.\n"));
4157 		(void) printf(gettext("action: Replace the faulted device, "
4158 		    "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4159 		break;
4160 
4161 	case ZPOOL_STATUS_FAULTED_DEV_NR:
4162 		(void) printf(gettext("status: One or more devices are "
4163 		    "faulted in response to persistent errors.  There are "
4164 		    "insufficient replicas for the pool to\n\tcontinue "
4165 		    "functioning.\n"));
4166 		(void) printf(gettext("action: Destroy and re-create the pool "
4167 		    "from a backup source.  Manually marking the device\n"
4168 		    "\trepaired using 'zpool clear' may allow some data "
4169 		    "to be recovered.\n"));
4170 		break;
4171 
4172 	case ZPOOL_STATUS_IO_FAILURE_WAIT:
4173 	case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4174 		(void) printf(gettext("status: One or more devices are "
4175 		    "faulted in response to IO failures.\n"));
4176 		(void) printf(gettext("action: Make sure the affected devices "
4177 		    "are connected, then run 'zpool clear'.\n"));
4178 		break;
4179 
4180 	case ZPOOL_STATUS_BAD_LOG:
4181 		(void) printf(gettext("status: An intent log record "
4182 		    "could not be read.\n"
4183 		    "\tWaiting for adminstrator intervention to fix the "
4184 		    "faulted pool.\n"));
4185 		(void) printf(gettext("action: Either restore the affected "
4186 		    "device(s) and run 'zpool online',\n"
4187 		    "\tor ignore the intent log records by running "
4188 		    "'zpool clear'.\n"));
4189 		break;
4190 
4191 	default:
4192 		/*
4193 		 * The remaining errors can't actually be generated, yet.
4194 		 */
4195 		assert(reason == ZPOOL_STATUS_OK);
4196 	}
4197 
4198 	if (msgid != NULL)
4199 		(void) printf(gettext("   see: http://illumos.org/msg/%s\n"),
4200 		    msgid);
4201 
4202 	if (config != NULL) {
4203 		int namewidth;
4204 		uint64_t nerr;
4205 		nvlist_t **spares, **l2cache;
4206 		uint_t nspares, nl2cache;
4207 		pool_scan_stat_t *ps = NULL;
4208 
4209 		(void) nvlist_lookup_uint64_array(nvroot,
4210 		    ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4211 		print_scan_status(ps);
4212 
4213 		namewidth = max_width(zhp, nvroot, 0, 0);
4214 		if (namewidth < 10)
4215 			namewidth = 10;
4216 
4217 		(void) printf(gettext("config:\n\n"));
4218 		(void) printf(gettext("\t%-*s  %-8s %5s %5s %5s\n"), namewidth,
4219 		    "NAME", "STATE", "READ", "WRITE", "CKSUM");
4220 		print_status_config(zhp, zpool_get_name(zhp), nvroot,
4221 		    namewidth, 0, B_FALSE);
4222 
4223 		if (num_logs(nvroot) > 0)
4224 			print_logs(zhp, nvroot, namewidth, B_TRUE);
4225 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4226 		    &l2cache, &nl2cache) == 0)
4227 			print_l2cache(zhp, l2cache, nl2cache, namewidth);
4228 
4229 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4230 		    &spares, &nspares) == 0)
4231 			print_spares(zhp, spares, nspares, namewidth);
4232 
4233 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4234 		    &nerr) == 0) {
4235 			nvlist_t *nverrlist = NULL;
4236 
4237 			/*
4238 			 * If the approximate error count is small, get a
4239 			 * precise count by fetching the entire log and
4240 			 * uniquifying the results.
4241 			 */
4242 			if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4243 			    zpool_get_errlog(zhp, &nverrlist) == 0) {
4244 				nvpair_t *elem;
4245 
4246 				elem = NULL;
4247 				nerr = 0;
4248 				while ((elem = nvlist_next_nvpair(nverrlist,
4249 				    elem)) != NULL) {
4250 					nerr++;
4251 				}
4252 			}
4253 			nvlist_free(nverrlist);
4254 
4255 			(void) printf("\n");
4256 
4257 			if (nerr == 0)
4258 				(void) printf(gettext("errors: No known data "
4259 				    "errors\n"));
4260 			else if (!cbp->cb_verbose)
4261 				(void) printf(gettext("errors: %llu data "
4262 				    "errors, use '-v' for a list\n"),
4263 				    (u_longlong_t)nerr);
4264 			else
4265 				print_error_log(zhp);
4266 		}
4267 
4268 		if (cbp->cb_dedup_stats)
4269 			print_dedup_stats(config);
4270 	} else {
4271 		(void) printf(gettext("config: The configuration cannot be "
4272 		    "determined.\n"));
4273 	}
4274 
4275 	return (0);
4276 }
4277 
4278 /*
4279  * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4280  *
4281  *	-v	Display complete error logs
4282  *	-x	Display only pools with potential problems
4283  *	-D	Display dedup status (undocumented)
4284  *	-T	Display a timestamp in date(1) or Unix format
4285  *
4286  * Describes the health status of all pools or some subset.
4287  */
4288 int
4289 zpool_do_status(int argc, char **argv)
4290 {
4291 	int c;
4292 	int ret;
4293 	unsigned long interval = 0, count = 0;
4294 	status_cbdata_t cb = { 0 };
4295 
4296 	/* check options */
4297 	while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4298 		switch (c) {
4299 		case 'v':
4300 			cb.cb_verbose = B_TRUE;
4301 			break;
4302 		case 'x':
4303 			cb.cb_explain = B_TRUE;
4304 			break;
4305 		case 'D':
4306 			cb.cb_dedup_stats = B_TRUE;
4307 			break;
4308 		case 'T':
4309 			get_timestamp_arg(*optarg);
4310 			break;
4311 		case '?':
4312 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4313 			    optopt);
4314 			usage(B_FALSE);
4315 		}
4316 	}
4317 
4318 	argc -= optind;
4319 	argv += optind;
4320 
4321 	get_interval_count(&argc, argv, &interval, &count);
4322 
4323 	if (argc == 0)
4324 		cb.cb_allpools = B_TRUE;
4325 
4326 	cb.cb_first = B_TRUE;
4327 
4328 	for (;;) {
4329 		if (timestamp_fmt != NODATE)
4330 			print_timestamp(timestamp_fmt);
4331 
4332 		ret = for_each_pool(argc, argv, B_TRUE, NULL,
4333 		    status_callback, &cb);
4334 
4335 		if (argc == 0 && cb.cb_count == 0)
4336 			(void) printf(gettext("no pools available\n"));
4337 		else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4338 			(void) printf(gettext("all pools are healthy\n"));
4339 
4340 		if (ret != 0)
4341 			return (ret);
4342 
4343 		if (interval == 0)
4344 			break;
4345 
4346 		if (count != 0 && --count == 0)
4347 			break;
4348 
4349 		(void) sleep(interval);
4350 	}
4351 
4352 	return (0);
4353 }
4354 
4355 typedef struct upgrade_cbdata {
4356 	int	cb_first;
4357 	int	cb_argc;
4358 	uint64_t cb_version;
4359 	char	**cb_argv;
4360 } upgrade_cbdata_t;
4361 
4362 static int
4363 upgrade_version(zpool_handle_t *zhp, uint64_t version)
4364 {
4365 	int ret;
4366 	nvlist_t *config;
4367 	uint64_t oldversion;
4368 
4369 	config = zpool_get_config(zhp, NULL);
4370 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4371 	    &oldversion) == 0);
4372 
4373 	assert(SPA_VERSION_IS_SUPPORTED(oldversion));
4374 	assert(oldversion < version);
4375 
4376 	ret = zpool_upgrade(zhp, version);
4377 	if (ret != 0)
4378 		return (ret);
4379 
4380 	if (version >= SPA_VERSION_FEATURES) {
4381 		(void) printf(gettext("Successfully upgraded "
4382 		    "'%s' from version %llu to feature flags.\n"),
4383 		    zpool_get_name(zhp), oldversion);
4384 	} else {
4385 		(void) printf(gettext("Successfully upgraded "
4386 		    "'%s' from version %llu to version %llu.\n"),
4387 		    zpool_get_name(zhp), oldversion, version);
4388 	}
4389 
4390 	return (0);
4391 }
4392 
4393 static int
4394 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
4395 {
4396 	int i, ret, count;
4397 	boolean_t firstff = B_TRUE;
4398 	nvlist_t *enabled = zpool_get_features(zhp);
4399 
4400 	count = 0;
4401 	for (i = 0; i < SPA_FEATURES; i++) {
4402 		const char *fname = spa_feature_table[i].fi_uname;
4403 		const char *fguid = spa_feature_table[i].fi_guid;
4404 		if (!nvlist_exists(enabled, fguid)) {
4405 			char *propname;
4406 			verify(-1 != asprintf(&propname, "feature@%s", fname));
4407 			ret = zpool_set_prop(zhp, propname,
4408 			    ZFS_FEATURE_ENABLED);
4409 			if (ret != 0) {
4410 				free(propname);
4411 				return (ret);
4412 			}
4413 			count++;
4414 
4415 			if (firstff) {
4416 				(void) printf(gettext("Enabled the "
4417 				    "following features on '%s':\n"),
4418 				    zpool_get_name(zhp));
4419 				firstff = B_FALSE;
4420 			}
4421 			(void) printf(gettext("  %s\n"), fname);
4422 			free(propname);
4423 		}
4424 	}
4425 
4426 	if (countp != NULL)
4427 		*countp = count;
4428 	return (0);
4429 }
4430 
4431 static int
4432 upgrade_cb(zpool_handle_t *zhp, void *arg)
4433 {
4434 	upgrade_cbdata_t *cbp = arg;
4435 	nvlist_t *config;
4436 	uint64_t version;
4437 	boolean_t printnl = B_FALSE;
4438 	int ret;
4439 
4440 	config = zpool_get_config(zhp, NULL);
4441 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4442 	    &version) == 0);
4443 
4444 	assert(SPA_VERSION_IS_SUPPORTED(version));
4445 
4446 	if (version < cbp->cb_version) {
4447 		cbp->cb_first = B_FALSE;
4448 		ret = upgrade_version(zhp, cbp->cb_version);
4449 		if (ret != 0)
4450 			return (ret);
4451 		printnl = B_TRUE;
4452 
4453 		/*
4454 		 * If they did "zpool upgrade -a", then we could
4455 		 * be doing ioctls to different pools.  We need
4456 		 * to log this history once to each pool, and bypass
4457 		 * the normal history logging that happens in main().
4458 		 */
4459 		(void) zpool_log_history(g_zfs, history_str);
4460 		log_history = B_FALSE;
4461 	}
4462 
4463 	if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4464 		int count;
4465 		ret = upgrade_enable_all(zhp, &count);
4466 		if (ret != 0)
4467 			return (ret);
4468 
4469 		if (count > 0) {
4470 			cbp->cb_first = B_FALSE;
4471 			printnl = B_TRUE;
4472 		}
4473 	}
4474 
4475 	if (printnl) {
4476 		(void) printf(gettext("\n"));
4477 	}
4478 
4479 	return (0);
4480 }
4481 
4482 static int
4483 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
4484 {
4485 	upgrade_cbdata_t *cbp = arg;
4486 	nvlist_t *config;
4487 	uint64_t version;
4488 
4489 	config = zpool_get_config(zhp, NULL);
4490 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4491 	    &version) == 0);
4492 
4493 	assert(SPA_VERSION_IS_SUPPORTED(version));
4494 
4495 	if (version < SPA_VERSION_FEATURES) {
4496 		if (cbp->cb_first) {
4497 			(void) printf(gettext("The following pools are "
4498 			    "formatted with legacy version numbers and can\n"
4499 			    "be upgraded to use feature flags.  After "
4500 			    "being upgraded, these pools\nwill no "
4501 			    "longer be accessible by software that does not "
4502 			    "support feature\nflags.\n\n"));
4503 			(void) printf(gettext("VER  POOL\n"));
4504 			(void) printf(gettext("---  ------------\n"));
4505 			cbp->cb_first = B_FALSE;
4506 		}
4507 
4508 		(void) printf("%2llu   %s\n", (u_longlong_t)version,
4509 		    zpool_get_name(zhp));
4510 	}
4511 
4512 	return (0);
4513 }
4514 
4515 static int
4516 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
4517 {
4518 	upgrade_cbdata_t *cbp = arg;
4519 	nvlist_t *config;
4520 	uint64_t version;
4521 
4522 	config = zpool_get_config(zhp, NULL);
4523 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4524 	    &version) == 0);
4525 
4526 	if (version >= SPA_VERSION_FEATURES) {
4527 		int i;
4528 		boolean_t poolfirst = B_TRUE;
4529 		nvlist_t *enabled = zpool_get_features(zhp);
4530 
4531 		for (i = 0; i < SPA_FEATURES; i++) {
4532 			const char *fguid = spa_feature_table[i].fi_guid;
4533 			const char *fname = spa_feature_table[i].fi_uname;
4534 			if (!nvlist_exists(enabled, fguid)) {
4535 				if (cbp->cb_first) {
4536 					(void) printf(gettext("\nSome "
4537 					    "supported features are not "
4538 					    "enabled on the following pools. "
4539 					    "Once a\nfeature is enabled the "
4540 					    "pool may become incompatible with "
4541 					    "software\nthat does not support "
4542 					    "the feature. See "
4543 					    "zpool-features(5) for "
4544 					    "details.\n\n"));
4545 					(void) printf(gettext("POOL  "
4546 					    "FEATURE\n"));
4547 					(void) printf(gettext("------"
4548 					    "---------\n"));
4549 					cbp->cb_first = B_FALSE;
4550 				}
4551 
4552 				if (poolfirst) {
4553 					(void) printf(gettext("%s\n"),
4554 					    zpool_get_name(zhp));
4555 					poolfirst = B_FALSE;
4556 				}
4557 
4558 				(void) printf(gettext("      %s\n"), fname);
4559 			}
4560 		}
4561 	}
4562 
4563 	return (0);
4564 }
4565 
4566 /* ARGSUSED */
4567 static int
4568 upgrade_one(zpool_handle_t *zhp, void *data)
4569 {
4570 	boolean_t printnl = B_FALSE;
4571 	upgrade_cbdata_t *cbp = data;
4572 	uint64_t cur_version;
4573 	int ret;
4574 
4575 	if (strcmp("log", zpool_get_name(zhp)) == 0) {
4576 		(void) printf(gettext("'log' is now a reserved word\n"
4577 		    "Pool 'log' must be renamed using export and import"
4578 		    " to upgrade.\n"));
4579 		return (1);
4580 	}
4581 
4582 	cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4583 	if (cur_version > cbp->cb_version) {
4584 		(void) printf(gettext("Pool '%s' is already formatted "
4585 		    "using more current version '%llu'.\n\n"),
4586 		    zpool_get_name(zhp), cur_version);
4587 		return (0);
4588 	}
4589 
4590 	if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
4591 		(void) printf(gettext("Pool '%s' is already formatted "
4592 		    "using version %llu.\n\n"), zpool_get_name(zhp),
4593 		    cbp->cb_version);
4594 		return (0);
4595 	}
4596 
4597 	if (cur_version != cbp->cb_version) {
4598 		printnl = B_TRUE;
4599 		ret = upgrade_version(zhp, cbp->cb_version);
4600 		if (ret != 0)
4601 			return (ret);
4602 	}
4603 
4604 	if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4605 		int count = 0;
4606 		ret = upgrade_enable_all(zhp, &count);
4607 		if (ret != 0)
4608 			return (ret);
4609 
4610 		if (count != 0) {
4611 			printnl = B_TRUE;
4612 		} else if (cur_version == SPA_VERSION) {
4613 			(void) printf(gettext("Pool '%s' already has all "
4614 			    "supported features enabled.\n"),
4615 			    zpool_get_name(zhp));
4616 		}
4617 	}
4618 
4619 	if (printnl) {
4620 		(void) printf(gettext("\n"));
4621 	}
4622 
4623 	return (0);
4624 }
4625 
4626 /*
4627  * zpool upgrade
4628  * zpool upgrade -v
4629  * zpool upgrade [-V version] <-a | pool ...>
4630  *
4631  * With no arguments, display downrev'd ZFS pool available for upgrade.
4632  * Individual pools can be upgraded by specifying the pool, and '-a' will
4633  * upgrade all pools.
4634  */
4635 int
4636 zpool_do_upgrade(int argc, char **argv)
4637 {
4638 	int c;
4639 	upgrade_cbdata_t cb = { 0 };
4640 	int ret = 0;
4641 	boolean_t showversions = B_FALSE;
4642 	boolean_t upgradeall = B_FALSE;
4643 	char *end;
4644 
4645 
4646 	/* check options */
4647 	while ((c = getopt(argc, argv, ":avV:")) != -1) {
4648 		switch (c) {
4649 		case 'a':
4650 			upgradeall = B_TRUE;
4651 			break;
4652 		case 'v':
4653 			showversions = B_TRUE;
4654 			break;
4655 		case 'V':
4656 			cb.cb_version = strtoll(optarg, &end, 10);
4657 			if (*end != '\0' ||
4658 			    !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
4659 				(void) fprintf(stderr,
4660 				    gettext("invalid version '%s'\n"), optarg);
4661 				usage(B_FALSE);
4662 			}
4663 			break;
4664 		case ':':
4665 			(void) fprintf(stderr, gettext("missing argument for "
4666 			    "'%c' option\n"), optopt);
4667 			usage(B_FALSE);
4668 			break;
4669 		case '?':
4670 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4671 			    optopt);
4672 			usage(B_FALSE);
4673 		}
4674 	}
4675 
4676 	cb.cb_argc = argc;
4677 	cb.cb_argv = argv;
4678 	argc -= optind;
4679 	argv += optind;
4680 
4681 	if (cb.cb_version == 0) {
4682 		cb.cb_version = SPA_VERSION;
4683 	} else if (!upgradeall && argc == 0) {
4684 		(void) fprintf(stderr, gettext("-V option is "
4685 		    "incompatible with other arguments\n"));
4686 		usage(B_FALSE);
4687 	}
4688 
4689 	if (showversions) {
4690 		if (upgradeall || argc != 0) {
4691 			(void) fprintf(stderr, gettext("-v option is "
4692 			    "incompatible with other arguments\n"));
4693 			usage(B_FALSE);
4694 		}
4695 	} else if (upgradeall) {
4696 		if (argc != 0) {
4697 			(void) fprintf(stderr, gettext("-a option should not "
4698 			    "be used along with a pool name\n"));
4699 			usage(B_FALSE);
4700 		}
4701 	}
4702 
4703 	(void) printf(gettext("This system supports ZFS pool feature "
4704 	    "flags.\n\n"));
4705 	if (showversions) {
4706 		int i;
4707 
4708 		(void) printf(gettext("The following features are "
4709 		    "supported:\n\n"));
4710 		(void) printf(gettext("FEAT DESCRIPTION\n"));
4711 		(void) printf("----------------------------------------------"
4712 		    "---------------\n");
4713 		for (i = 0; i < SPA_FEATURES; i++) {
4714 			zfeature_info_t *fi = &spa_feature_table[i];
4715 			const char *ro = fi->fi_can_readonly ?
4716 			    " (read-only compatible)" : "";
4717 
4718 			(void) printf("%-37s%s\n", fi->fi_uname, ro);
4719 			(void) printf("     %s\n", fi->fi_desc);
4720 		}
4721 		(void) printf("\n");
4722 
4723 		(void) printf(gettext("The following legacy versions are also "
4724 		    "supported:\n\n"));
4725 		(void) printf(gettext("VER  DESCRIPTION\n"));
4726 		(void) printf("---  -----------------------------------------"
4727 		    "---------------\n");
4728 		(void) printf(gettext(" 1   Initial ZFS version\n"));
4729 		(void) printf(gettext(" 2   Ditto blocks "
4730 		    "(replicated metadata)\n"));
4731 		(void) printf(gettext(" 3   Hot spares and double parity "
4732 		    "RAID-Z\n"));
4733 		(void) printf(gettext(" 4   zpool history\n"));
4734 		(void) printf(gettext(" 5   Compression using the gzip "
4735 		    "algorithm\n"));
4736 		(void) printf(gettext(" 6   bootfs pool property\n"));
4737 		(void) printf(gettext(" 7   Separate intent log devices\n"));
4738 		(void) printf(gettext(" 8   Delegated administration\n"));
4739 		(void) printf(gettext(" 9   refquota and refreservation "
4740 		    "properties\n"));
4741 		(void) printf(gettext(" 10  Cache devices\n"));
4742 		(void) printf(gettext(" 11  Improved scrub performance\n"));
4743 		(void) printf(gettext(" 12  Snapshot properties\n"));
4744 		(void) printf(gettext(" 13  snapused property\n"));
4745 		(void) printf(gettext(" 14  passthrough-x aclinherit\n"));
4746 		(void) printf(gettext(" 15  user/group space accounting\n"));
4747 		(void) printf(gettext(" 16  stmf property support\n"));
4748 		(void) printf(gettext(" 17  Triple-parity RAID-Z\n"));
4749 		(void) printf(gettext(" 18  Snapshot user holds\n"));
4750 		(void) printf(gettext(" 19  Log device removal\n"));
4751 		(void) printf(gettext(" 20  Compression using zle "
4752 		    "(zero-length encoding)\n"));
4753 		(void) printf(gettext(" 21  Deduplication\n"));
4754 		(void) printf(gettext(" 22  Received properties\n"));
4755 		(void) printf(gettext(" 23  Slim ZIL\n"));
4756 		(void) printf(gettext(" 24  System attributes\n"));
4757 		(void) printf(gettext(" 25  Improved scrub stats\n"));
4758 		(void) printf(gettext(" 26  Improved snapshot deletion "
4759 		    "performance\n"));
4760 		(void) printf(gettext(" 27  Improved snapshot creation "
4761 		    "performance\n"));
4762 		(void) printf(gettext(" 28  Multiple vdev replacements\n"));
4763 		(void) printf(gettext("\nFor more information on a particular "
4764 		    "version, including supported releases,\n"));
4765 		(void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4766 	} else if (argc == 0 && upgradeall) {
4767 		cb.cb_first = B_TRUE;
4768 		ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4769 		if (ret == 0 && cb.cb_first) {
4770 			if (cb.cb_version == SPA_VERSION) {
4771 				(void) printf(gettext("All pools are already "
4772 				    "formatted using feature flags.\n\n"));
4773 				(void) printf(gettext("Every feature flags "
4774 				    "pool already has all supported features "
4775 				    "enabled.\n"));
4776 			} else {
4777 				(void) printf(gettext("All pools are already "
4778 				    "formatted with version %llu or higher.\n"),
4779 				    cb.cb_version);
4780 			}
4781 		}
4782 	} else if (argc == 0) {
4783 		cb.cb_first = B_TRUE;
4784 		ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
4785 		assert(ret == 0);
4786 
4787 		if (cb.cb_first) {
4788 			(void) printf(gettext("All pools are formatted "
4789 			    "using feature flags.\n\n"));
4790 		} else {
4791 			(void) printf(gettext("\nUse 'zpool upgrade -v' "
4792 			    "for a list of available legacy versions.\n"));
4793 		}
4794 
4795 		cb.cb_first = B_TRUE;
4796 		ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
4797 		assert(ret == 0);
4798 
4799 		if (cb.cb_first) {
4800 			(void) printf(gettext("Every feature flags pool has "
4801 			    "all supported features enabled.\n"));
4802 		} else {
4803 			(void) printf(gettext("\n"));
4804 		}
4805 	} else {
4806 		ret = for_each_pool(argc, argv, B_FALSE, NULL,
4807 		    upgrade_one, &cb);
4808 	}
4809 
4810 	return (ret);
4811 }
4812 
4813 typedef struct hist_cbdata {
4814 	boolean_t first;
4815 	boolean_t longfmt;
4816 	boolean_t internal;
4817 } hist_cbdata_t;
4818 
4819 /*
4820  * Print out the command history for a specific pool.
4821  */
4822 static int
4823 get_history_one(zpool_handle_t *zhp, void *data)
4824 {
4825 	nvlist_t *nvhis;
4826 	nvlist_t **records;
4827 	uint_t numrecords;
4828 	int ret, i;
4829 	hist_cbdata_t *cb = (hist_cbdata_t *)data;
4830 
4831 	cb->first = B_FALSE;
4832 
4833 	(void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
4834 
4835 	if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
4836 		return (ret);
4837 
4838 	verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
4839 	    &records, &numrecords) == 0);
4840 	for (i = 0; i < numrecords; i++) {
4841 		nvlist_t *rec = records[i];
4842 		char tbuf[30] = "";
4843 
4844 		if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
4845 			time_t tsec;
4846 			struct tm t;
4847 
4848 			tsec = fnvlist_lookup_uint64(records[i],
4849 			    ZPOOL_HIST_TIME);
4850 			(void) localtime_r(&tsec, &t);
4851 			(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
4852 		}
4853 
4854 		if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
4855 			(void) printf("%s %s", tbuf,
4856 			    fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
4857 		} else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
4858 			int ievent =
4859 			    fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
4860 			if (!cb->internal)
4861 				continue;
4862 			if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
4863 				(void) printf("%s unrecognized record:\n",
4864 				    tbuf);
4865 				dump_nvlist(rec, 4);
4866 				continue;
4867 			}
4868 			(void) printf("%s [internal %s txg:%lld] %s", tbuf,
4869 			    zfs_history_event_names[ievent],
4870 			    fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
4871 			    fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
4872 		} else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
4873 			if (!cb->internal)
4874 				continue;
4875 			(void) printf("%s [txg:%lld] %s", tbuf,
4876 			    fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
4877 			    fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
4878 			if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
4879 				(void) printf(" %s (%llu)",
4880 				    fnvlist_lookup_string(rec,
4881 				    ZPOOL_HIST_DSNAME),
4882 				    fnvlist_lookup_uint64(rec,
4883 				    ZPOOL_HIST_DSID));
4884 			}
4885 			(void) printf(" %s", fnvlist_lookup_string(rec,
4886 			    ZPOOL_HIST_INT_STR));
4887 		} else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
4888 			if (!cb->internal)
4889 				continue;
4890 			(void) printf("%s ioctl %s\n", tbuf,
4891 			    fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
4892 			if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
4893 				(void) printf("    input:\n");
4894 				dump_nvlist(fnvlist_lookup_nvlist(rec,
4895 				    ZPOOL_HIST_INPUT_NVL), 8);
4896 			}
4897 			if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
4898 				(void) printf("    output:\n");
4899 				dump_nvlist(fnvlist_lookup_nvlist(rec,
4900 				    ZPOOL_HIST_OUTPUT_NVL), 8);
4901 			}
4902 		} else {
4903 			if (!cb->internal)
4904 				continue;
4905 			(void) printf("%s unrecognized record:\n", tbuf);
4906 			dump_nvlist(rec, 4);
4907 		}
4908 
4909 		if (!cb->longfmt) {
4910 			(void) printf("\n");
4911 			continue;
4912 		}
4913 		(void) printf(" [");
4914 		if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
4915 			uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
4916 			struct passwd *pwd = getpwuid(who);
4917 			(void) printf("user %d ", (int)who);
4918 			if (pwd != NULL)
4919 				(void) printf("(%s) ", pwd->pw_name);
4920 		}
4921 		if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
4922 			(void) printf("on %s",
4923 			    fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
4924 		}
4925 		if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
4926 			(void) printf(":%s",
4927 			    fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
4928 		}
4929 		(void) printf("]");
4930 		(void) printf("\n");
4931 	}
4932 	(void) printf("\n");
4933 	nvlist_free(nvhis);
4934 
4935 	return (ret);
4936 }
4937 
4938 /*
4939  * zpool history <pool>
4940  *
4941  * Displays the history of commands that modified pools.
4942  */
4943 int
4944 zpool_do_history(int argc, char **argv)
4945 {
4946 	hist_cbdata_t cbdata = { 0 };
4947 	int ret;
4948 	int c;
4949 
4950 	cbdata.first = B_TRUE;
4951 	/* check options */
4952 	while ((c = getopt(argc, argv, "li")) != -1) {
4953 		switch (c) {
4954 		case 'l':
4955 			cbdata.longfmt = B_TRUE;
4956 			break;
4957 		case 'i':
4958 			cbdata.internal = B_TRUE;
4959 			break;
4960 		case '?':
4961 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4962 			    optopt);
4963 			usage(B_FALSE);
4964 		}
4965 	}
4966 	argc -= optind;
4967 	argv += optind;
4968 
4969 	ret = for_each_pool(argc, argv, B_FALSE,  NULL, get_history_one,
4970 	    &cbdata);
4971 
4972 	if (argc == 0 && cbdata.first == B_TRUE) {
4973 		(void) printf(gettext("no pools available\n"));
4974 		return (0);
4975 	}
4976 
4977 	return (ret);
4978 }
4979 
4980 static int
4981 get_callback(zpool_handle_t *zhp, void *data)
4982 {
4983 	zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
4984 	char value[MAXNAMELEN];
4985 	zprop_source_t srctype;
4986 	zprop_list_t *pl;
4987 
4988 	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
4989 
4990 		/*
4991 		 * Skip the special fake placeholder. This will also skip
4992 		 * over the name property when 'all' is specified.
4993 		 */
4994 		if (pl->pl_prop == ZPOOL_PROP_NAME &&
4995 		    pl == cbp->cb_proplist)
4996 			continue;
4997 
4998 		if (pl->pl_prop == ZPROP_INVAL &&
4999 		    (zpool_prop_feature(pl->pl_user_prop) ||
5000 		    zpool_prop_unsupported(pl->pl_user_prop))) {
5001 			srctype = ZPROP_SRC_LOCAL;
5002 
5003 			if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5004 			    value, sizeof (value)) == 0) {
5005 				zprop_print_one_property(zpool_get_name(zhp),
5006 				    cbp, pl->pl_user_prop, value, srctype,
5007 				    NULL, NULL);
5008 			}
5009 		} else {
5010 			if (zpool_get_prop(zhp, pl->pl_prop, value,
5011 			    sizeof (value), &srctype, cbp->cb_literal) != 0)
5012 				continue;
5013 
5014 			zprop_print_one_property(zpool_get_name(zhp), cbp,
5015 			    zpool_prop_to_name(pl->pl_prop), value, srctype,
5016 			    NULL, NULL);
5017 		}
5018 	}
5019 	return (0);
5020 }
5021 
5022 /*
5023  * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
5024  *
5025  *	-H	Scripted mode.  Don't display headers, and separate properties
5026  *		by a single tab.
5027  *	-o	List of columns to display.  Defaults to
5028  *		"name,property,value,source".
5029  * 	-p	Diplay values in parsable (exact) format.
5030  *
5031  * Get properties of pools in the system. Output space statistics
5032  * for each one as well as other attributes.
5033  */
5034 int
5035 zpool_do_get(int argc, char **argv)
5036 {
5037 	zprop_get_cbdata_t cb = { 0 };
5038 	zprop_list_t fake_name = { 0 };
5039 	int ret;
5040 	int c, i;
5041 	char *value;
5042 
5043 	cb.cb_first = B_TRUE;
5044 
5045 	/*
5046 	 * Set up default columns and sources.
5047 	 */
5048 	cb.cb_sources = ZPROP_SRC_ALL;
5049 	cb.cb_columns[0] = GET_COL_NAME;
5050 	cb.cb_columns[1] = GET_COL_PROPERTY;
5051 	cb.cb_columns[2] = GET_COL_VALUE;
5052 	cb.cb_columns[3] = GET_COL_SOURCE;
5053 	cb.cb_type = ZFS_TYPE_POOL;
5054 
5055 	/* check options */
5056 	while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
5057 		switch (c) {
5058 		case 'p':
5059 			cb.cb_literal = B_TRUE;
5060 			break;
5061 		case 'H':
5062 			cb.cb_scripted = B_TRUE;
5063 			break;
5064 		case 'o':
5065 			bzero(&cb.cb_columns, sizeof (cb.cb_columns));
5066 			i = 0;
5067 			while (*optarg != '\0') {
5068 				static char *col_subopts[] =
5069 				{ "name", "property", "value", "source",
5070 				"all", NULL };
5071 
5072 				if (i == ZFS_GET_NCOLS) {
5073 					(void) fprintf(stderr, gettext("too "
5074 					"many fields given to -o "
5075 					"option\n"));
5076 					usage(B_FALSE);
5077 				}
5078 
5079 				switch (getsubopt(&optarg, col_subopts,
5080 				    &value)) {
5081 				case 0:
5082 					cb.cb_columns[i++] = GET_COL_NAME;
5083 					break;
5084 				case 1:
5085 					cb.cb_columns[i++] = GET_COL_PROPERTY;
5086 					break;
5087 				case 2:
5088 					cb.cb_columns[i++] = GET_COL_VALUE;
5089 					break;
5090 				case 3:
5091 					cb.cb_columns[i++] = GET_COL_SOURCE;
5092 					break;
5093 				case 4:
5094 					if (i > 0) {
5095 						(void) fprintf(stderr,
5096 						    gettext("\"all\" conflicts "
5097 						    "with specific fields "
5098 						    "given to -o option\n"));
5099 						usage(B_FALSE);
5100 					}
5101 					cb.cb_columns[0] = GET_COL_NAME;
5102 					cb.cb_columns[1] = GET_COL_PROPERTY;
5103 					cb.cb_columns[2] = GET_COL_VALUE;
5104 					cb.cb_columns[3] = GET_COL_SOURCE;
5105 					i = ZFS_GET_NCOLS;
5106 					break;
5107 				default:
5108 					(void) fprintf(stderr,
5109 					    gettext("invalid column name "
5110 					    "'%s'\n"), value);
5111 					usage(B_FALSE);
5112 				}
5113 			}
5114 			break;
5115 		case '?':
5116 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
5117 			    optopt);
5118 			usage(B_FALSE);
5119 		}
5120 	}
5121 
5122 	argc -= optind;
5123 	argv += optind;
5124 
5125 	if (argc < 1) {
5126 		(void) fprintf(stderr, gettext("missing property "
5127 		    "argument\n"));
5128 		usage(B_FALSE);
5129 	}
5130 
5131 	if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
5132 	    ZFS_TYPE_POOL) != 0)
5133 		usage(B_FALSE);
5134 
5135 	argc--;
5136 	argv++;
5137 
5138 	if (cb.cb_proplist != NULL) {
5139 		fake_name.pl_prop = ZPOOL_PROP_NAME;
5140 		fake_name.pl_width = strlen(gettext("NAME"));
5141 		fake_name.pl_next = cb.cb_proplist;
5142 		cb.cb_proplist = &fake_name;
5143 	}
5144 
5145 	ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
5146 	    get_callback, &cb);
5147 
5148 	if (cb.cb_proplist == &fake_name)
5149 		zprop_free_list(fake_name.pl_next);
5150 	else
5151 		zprop_free_list(cb.cb_proplist);
5152 
5153 	return (ret);
5154 }
5155 
5156 typedef struct set_cbdata {
5157 	char *cb_propname;
5158 	char *cb_value;
5159 	boolean_t cb_any_successful;
5160 } set_cbdata_t;
5161 
5162 int
5163 set_callback(zpool_handle_t *zhp, void *data)
5164 {
5165 	int error;
5166 	set_cbdata_t *cb = (set_cbdata_t *)data;
5167 
5168 	error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5169 
5170 	if (!error)
5171 		cb->cb_any_successful = B_TRUE;
5172 
5173 	return (error);
5174 }
5175 
5176 int
5177 zpool_do_set(int argc, char **argv)
5178 {
5179 	set_cbdata_t cb = { 0 };
5180 	int error;
5181 
5182 	if (argc > 1 && argv[1][0] == '-') {
5183 		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
5184 		    argv[1][1]);
5185 		usage(B_FALSE);
5186 	}
5187 
5188 	if (argc < 2) {
5189 		(void) fprintf(stderr, gettext("missing property=value "
5190 		    "argument\n"));
5191 		usage(B_FALSE);
5192 	}
5193 
5194 	if (argc < 3) {
5195 		(void) fprintf(stderr, gettext("missing pool name\n"));
5196 		usage(B_FALSE);
5197 	}
5198 
5199 	if (argc > 3) {
5200 		(void) fprintf(stderr, gettext("too many pool names\n"));
5201 		usage(B_FALSE);
5202 	}
5203 
5204 	cb.cb_propname = argv[1];
5205 	cb.cb_value = strchr(cb.cb_propname, '=');
5206 	if (cb.cb_value == NULL) {
5207 		(void) fprintf(stderr, gettext("missing value in "
5208 		    "property=value argument\n"));
5209 		usage(B_FALSE);
5210 	}
5211 
5212 	*(cb.cb_value) = '\0';
5213 	cb.cb_value++;
5214 
5215 	error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5216 	    set_callback, &cb);
5217 
5218 	return (error);
5219 }
5220 
5221 static int
5222 find_command_idx(char *command, int *idx)
5223 {
5224 	int i;
5225 
5226 	for (i = 0; i < NCOMMAND; i++) {
5227 		if (command_table[i].name == NULL)
5228 			continue;
5229 
5230 		if (strcmp(command, command_table[i].name) == 0) {
5231 			*idx = i;
5232 			return (0);
5233 		}
5234 	}
5235 	return (1);
5236 }
5237 
5238 int
5239 main(int argc, char **argv)
5240 {
5241 	int ret;
5242 	int i;
5243 	char *cmdname;
5244 
5245 	(void) setlocale(LC_ALL, "");
5246 	(void) textdomain(TEXT_DOMAIN);
5247 
5248 	if ((g_zfs = libzfs_init()) == NULL) {
5249 		(void) fprintf(stderr, gettext("internal error: failed to "
5250 		    "initialize ZFS library\n"));
5251 		return (1);
5252 	}
5253 
5254 	libzfs_print_on_error(g_zfs, B_TRUE);
5255 
5256 	opterr = 0;
5257 
5258 	/*
5259 	 * Make sure the user has specified some command.
5260 	 */
5261 	if (argc < 2) {
5262 		(void) fprintf(stderr, gettext("missing command\n"));
5263 		usage(B_FALSE);
5264 	}
5265 
5266 	cmdname = argv[1];
5267 
5268 	/*
5269 	 * Special case '-?'
5270 	 */
5271 	if (strcmp(cmdname, "-?") == 0)
5272 		usage(B_TRUE);
5273 
5274 	zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
5275 
5276 	/*
5277 	 * Run the appropriate command.
5278 	 */
5279 	if (find_command_idx(cmdname, &i) == 0) {
5280 		current_command = &command_table[i];
5281 		ret = command_table[i].func(argc - 1, argv + 1);
5282 	} else if (strchr(cmdname, '=')) {
5283 		verify(find_command_idx("set", &i) == 0);
5284 		current_command = &command_table[i];
5285 		ret = command_table[i].func(argc, argv);
5286 	} else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5287 		/*
5288 		 * 'freeze' is a vile debugging abomination, so we treat
5289 		 * it as such.
5290 		 */
5291 		char buf[16384];
5292 		int fd = open(ZFS_DEV, O_RDWR);
5293 		(void) strcpy((void *)buf, argv[2]);
5294 		return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
5295 	} else {
5296 		(void) fprintf(stderr, gettext("unrecognized "
5297 		    "command '%s'\n"), cmdname);
5298 		usage(B_FALSE);
5299 	}
5300 
5301 	if (ret == 0 && log_history)
5302 		(void) zpool_log_history(g_zfs, history_str);
5303 
5304 	libzfs_fini(g_zfs);
5305 
5306 	/*
5307 	 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5308 	 * for the purposes of running ::findleaks.
5309 	 */
5310 	if (getenv("ZFS_ABORT") != NULL) {
5311 		(void) printf("dumping core by request\n");
5312 		abort();
5313 	}
5314 
5315 	return (ret);
5316 }
5317