xref: /illumos-gate/usr/src/cmd/zpool/zpool_main.c (revision d7d4af51b4d115490d97f0b89993fdbaaf441c94)
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 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <assert.h>
30 #include <ctype.h>
31 #include <dirent.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <libgen.h>
35 #include <libintl.h>
36 #include <libuutil.h>
37 #include <locale.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <strings.h>
42 #include <unistd.h>
43 #include <priv.h>
44 
45 #include <sys/stat.h>
46 
47 #include <libzfs.h>
48 
49 #include "zpool_util.h"
50 
51 static int zpool_do_create(int, char **);
52 static int zpool_do_destroy(int, char **);
53 
54 static int zpool_do_add(int, char **);
55 static int zpool_do_remove(int, char **);
56 
57 static int zpool_do_list(int, char **);
58 static int zpool_do_iostat(int, char **);
59 static int zpool_do_status(int, char **);
60 
61 static int zpool_do_online(int, char **);
62 static int zpool_do_offline(int, char **);
63 static int zpool_do_clear(int, char **);
64 
65 static int zpool_do_attach(int, char **);
66 static int zpool_do_detach(int, char **);
67 static int zpool_do_replace(int, char **);
68 
69 static int zpool_do_scrub(int, char **);
70 
71 static int zpool_do_import(int, char **);
72 static int zpool_do_export(int, char **);
73 
74 static int zpool_do_upgrade(int, char **);
75 
76 static int zpool_do_history(int, char **);
77 
78 /*
79  * These libumem hooks provide a reasonable set of defaults for the allocator's
80  * debugging facilities.
81  */
82 const char *
83 _umem_debug_init(void)
84 {
85 	return ("default,verbose"); /* $UMEM_DEBUG setting */
86 }
87 
88 const char *
89 _umem_logging_init(void)
90 {
91 	return ("fail,contents"); /* $UMEM_LOGGING setting */
92 }
93 
94 typedef enum {
95 	HELP_ADD,
96 	HELP_ATTACH,
97 	HELP_CLEAR,
98 	HELP_CREATE,
99 	HELP_DESTROY,
100 	HELP_DETACH,
101 	HELP_EXPORT,
102 	HELP_HISTORY,
103 	HELP_IMPORT,
104 	HELP_IOSTAT,
105 	HELP_LIST,
106 	HELP_OFFLINE,
107 	HELP_ONLINE,
108 	HELP_REPLACE,
109 	HELP_REMOVE,
110 	HELP_SCRUB,
111 	HELP_STATUS,
112 	HELP_UPGRADE
113 } zpool_help_t;
114 
115 
116 typedef struct zpool_command {
117 	const char	*name;
118 	int		(*func)(int, char **);
119 	zpool_help_t	usage;
120 } zpool_command_t;
121 
122 /*
123  * Master command table.  Each ZFS command has a name, associated function, and
124  * usage message.  The usage messages need to be internationalized, so we have
125  * to have a function to return the usage message based on a command index.
126  *
127  * These commands are organized according to how they are displayed in the usage
128  * message.  An empty command (one with a NULL name) indicates an empty line in
129  * the generic usage message.
130  */
131 static zpool_command_t command_table[] = {
132 	{ "create",	zpool_do_create,	HELP_CREATE		},
133 	{ "destroy",	zpool_do_destroy,	HELP_DESTROY		},
134 	{ NULL },
135 	{ "add",	zpool_do_add,		HELP_ADD		},
136 	{ "remove",	zpool_do_remove,	HELP_REMOVE		},
137 	{ NULL },
138 	{ "list",	zpool_do_list,		HELP_LIST		},
139 	{ "iostat",	zpool_do_iostat,	HELP_IOSTAT		},
140 	{ "status",	zpool_do_status,	HELP_STATUS		},
141 	{ NULL },
142 	{ "online",	zpool_do_online,	HELP_ONLINE		},
143 	{ "offline",	zpool_do_offline,	HELP_OFFLINE		},
144 	{ "clear",	zpool_do_clear,		HELP_CLEAR		},
145 	{ NULL },
146 	{ "attach",	zpool_do_attach,	HELP_ATTACH		},
147 	{ "detach",	zpool_do_detach,	HELP_DETACH		},
148 	{ "replace",	zpool_do_replace,	HELP_REPLACE		},
149 	{ NULL },
150 	{ "scrub",	zpool_do_scrub,		HELP_SCRUB		},
151 	{ NULL },
152 	{ "import",	zpool_do_import,	HELP_IMPORT		},
153 	{ "export",	zpool_do_export,	HELP_EXPORT		},
154 	{ "upgrade",	zpool_do_upgrade,	HELP_UPGRADE		},
155 	{ NULL },
156 	{ "history",	zpool_do_history,	HELP_HISTORY		}
157 };
158 
159 #define	NCOMMAND	(sizeof (command_table) / sizeof (command_table[0]))
160 
161 zpool_command_t *current_command;
162 
163 static const char *
164 get_usage(zpool_help_t idx) {
165 	switch (idx) {
166 	case HELP_ADD:
167 		return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
168 	case HELP_ATTACH:
169 		return (gettext("\tattach [-f] <pool> <device> "
170 		    "<new_device>\n"));
171 	case HELP_CLEAR:
172 		return (gettext("\tclear <pool> [device]\n"));
173 	case HELP_CREATE:
174 		return (gettext("\tcreate  [-fn] [-R root] [-m mountpoint] "
175 		    "<pool> <vdev> ...\n"));
176 	case HELP_DESTROY:
177 		return (gettext("\tdestroy [-f] <pool>\n"));
178 	case HELP_DETACH:
179 		return (gettext("\tdetach <pool> <device>\n"));
180 	case HELP_EXPORT:
181 		return (gettext("\texport [-f] <pool> ...\n"));
182 	case HELP_HISTORY:
183 		return (gettext("\thistory [<pool>]\n"));
184 	case HELP_IMPORT:
185 		return (gettext("\timport [-d dir] [-D]\n"
186 		    "\timport [-d dir] [-D] [-f] [-o opts] [-R root] -a\n"
187 		    "\timport [-d dir] [-D] [-f] [-o opts] [-R root ]"
188 		    " <pool | id> [newpool]\n"));
189 	case HELP_IOSTAT:
190 		return (gettext("\tiostat [-v] [pool] ... [interval "
191 		    "[count]]\n"));
192 	case HELP_LIST:
193 		return (gettext("\tlist [-H] [-o field[,field]*] "
194 		    "[pool] ...\n"));
195 	case HELP_OFFLINE:
196 		return (gettext("\toffline [-t] <pool> <device> ...\n"));
197 	case HELP_ONLINE:
198 		return (gettext("\tonline <pool> <device> ...\n"));
199 	case HELP_REPLACE:
200 		return (gettext("\treplace [-f] <pool> <device> "
201 		    "[new_device]\n"));
202 	case HELP_REMOVE:
203 		return (gettext("\tremove <pool> <device>\n"));
204 	case HELP_SCRUB:
205 		return (gettext("\tscrub [-s] <pool> ...\n"));
206 	case HELP_STATUS:
207 		return (gettext("\tstatus [-vx] [pool] ...\n"));
208 	case HELP_UPGRADE:
209 		return (gettext("\tupgrade\n"
210 		    "\tupgrade -v\n"
211 		    "\tupgrade <-a | pool>\n"));
212 	}
213 
214 	abort();
215 	/* NOTREACHED */
216 }
217 
218 /*
219  * Fields available for 'zpool list'.
220  */
221 typedef enum {
222 	ZPOOL_FIELD_NAME,
223 	ZPOOL_FIELD_SIZE,
224 	ZPOOL_FIELD_USED,
225 	ZPOOL_FIELD_AVAILABLE,
226 	ZPOOL_FIELD_CAPACITY,
227 	ZPOOL_FIELD_HEALTH,
228 	ZPOOL_FIELD_ROOT
229 } zpool_field_t;
230 
231 #define	MAX_FIELDS	10
232 
233 typedef struct column_def {
234 	const char	*cd_title;
235 	size_t		cd_width;
236 	enum {
237 		left_justify,
238 		right_justify
239 	}		cd_justify;
240 } column_def_t;
241 
242 static column_def_t column_table[] = {
243 	{ "NAME",	20,	left_justify	},
244 	{ "SIZE",	6,	right_justify	},
245 	{ "USED",	6,	right_justify	},
246 	{ "AVAIL",	6,	right_justify	},
247 	{ "CAP",	5,	right_justify	},
248 	{ "HEALTH",	9,	left_justify	},
249 	{ "ALTROOT",	15,	left_justify	}
250 };
251 
252 static char *column_subopts[] = {
253 	"name",
254 	"size",
255 	"used",
256 	"available",
257 	"capacity",
258 	"health",
259 	"root",
260 	NULL
261 };
262 
263 /*
264  * Display usage message.  If we're inside a command, display only the usage for
265  * that command.  Otherwise, iterate over the entire command table and display
266  * a complete usage message.
267  */
268 void
269 usage(boolean_t requested)
270 {
271 	int i;
272 	FILE *fp = requested ? stdout : stderr;
273 
274 	if (current_command == NULL) {
275 		int i;
276 
277 		(void) fprintf(fp, gettext("usage: zpool command args ...\n"));
278 		(void) fprintf(fp,
279 		    gettext("where 'command' is one of the following:\n\n"));
280 
281 		for (i = 0; i < NCOMMAND; i++) {
282 			if (command_table[i].name == NULL)
283 				(void) fprintf(fp, "\n");
284 			else
285 				(void) fprintf(fp, "%s",
286 				    get_usage(command_table[i].usage));
287 		}
288 	} else {
289 		(void) fprintf(fp, gettext("usage:\n"));
290 		(void) fprintf(fp, "%s", get_usage(current_command->usage));
291 
292 		if (strcmp(current_command->name, "list") == 0) {
293 			(void) fprintf(fp, gettext("\nwhere 'field' is one "
294 			    "of the following:\n\n"));
295 
296 			for (i = 0; column_subopts[i] != NULL; i++)
297 				(void) fprintf(fp, "\t%s\n", column_subopts[i]);
298 		}
299 	}
300 
301 	/*
302 	 * See comments at end of main().
303 	 */
304 	if (getenv("ZFS_ABORT") != NULL) {
305 		(void) printf("dumping core by request\n");
306 		abort();
307 	}
308 
309 	exit(requested ? 0 : 2);
310 }
311 
312 const char *
313 state_to_name(vdev_stat_t *vs)
314 {
315 	switch (vs->vs_state) {
316 	case VDEV_STATE_CLOSED:
317 	case VDEV_STATE_CANT_OPEN:
318 		if (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)
319 			return (gettext("FAULTED"));
320 		else
321 			return (gettext("UNAVAIL"));
322 	case VDEV_STATE_OFFLINE:
323 		return (gettext("OFFLINE"));
324 	case VDEV_STATE_DEGRADED:
325 		return (gettext("DEGRADED"));
326 	case VDEV_STATE_HEALTHY:
327 		return (gettext("ONLINE"));
328 	}
329 
330 	return (gettext("UNKNOWN"));
331 }
332 
333 void
334 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent)
335 {
336 	nvlist_t **child;
337 	uint_t c, children;
338 	char *vname;
339 
340 	if (name != NULL)
341 		(void) printf("\t%*s%s\n", indent, "", name);
342 
343 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
344 	    &child, &children) != 0)
345 		return;
346 
347 	for (c = 0; c < children; c++) {
348 		vname = zpool_vdev_name(g_zfs, zhp, child[c]);
349 		print_vdev_tree(zhp, vname, child[c], indent + 2);
350 		free(vname);
351 	}
352 }
353 
354 /*
355  * zpool add [-fn] <pool> <vdev> ...
356  *
357  *	-f	Force addition of devices, even if they appear in use
358  *	-n	Do not add the devices, but display the resulting layout if
359  *		they were to be added.
360  *
361  * Adds the given vdevs to 'pool'.  As with create, the bulk of this work is
362  * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
363  * libzfs.
364  */
365 int
366 zpool_do_add(int argc, char **argv)
367 {
368 	boolean_t force = B_FALSE;
369 	boolean_t dryrun = B_FALSE;
370 	int c;
371 	nvlist_t *nvroot;
372 	char *poolname;
373 	int ret;
374 	zpool_handle_t *zhp;
375 	nvlist_t *config;
376 
377 	/* check options */
378 	while ((c = getopt(argc, argv, "fn")) != -1) {
379 		switch (c) {
380 		case 'f':
381 			force = B_TRUE;
382 			break;
383 		case 'n':
384 			dryrun = B_TRUE;
385 			break;
386 		case '?':
387 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
388 			    optopt);
389 			usage(B_FALSE);
390 		}
391 	}
392 
393 	argc -= optind;
394 	argv += optind;
395 
396 	/* get pool name and check number of arguments */
397 	if (argc < 1) {
398 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
399 		usage(B_FALSE);
400 	}
401 	if (argc < 2) {
402 		(void) fprintf(stderr, gettext("missing vdev specification\n"));
403 		usage(B_FALSE);
404 	}
405 
406 	poolname = argv[0];
407 
408 	argc--;
409 	argv++;
410 
411 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
412 		return (1);
413 
414 	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
415 		(void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
416 		    poolname);
417 		zpool_close(zhp);
418 		return (1);
419 	}
420 
421 	/* pass off to get_vdev_spec for processing */
422 	nvroot = make_root_vdev(config, force, !force, B_FALSE, argc, argv);
423 	if (nvroot == NULL) {
424 		zpool_close(zhp);
425 		return (1);
426 	}
427 
428 	if (dryrun) {
429 		nvlist_t *poolnvroot;
430 
431 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
432 		    &poolnvroot) == 0);
433 
434 		(void) printf(gettext("would update '%s' to the following "
435 		    "configuration:\n"), zpool_get_name(zhp));
436 
437 		print_vdev_tree(zhp, poolname, poolnvroot, 0);
438 		print_vdev_tree(zhp, NULL, nvroot, 0);
439 
440 		ret = 0;
441 	} else {
442 		ret = (zpool_add(zhp, nvroot) != 0);
443 		if (!ret) {
444 			zpool_log_history(g_zfs, argc + 1 + optind,
445 			    argv - 1 - optind, poolname, B_TRUE, B_FALSE);
446 		}
447 	}
448 
449 	nvlist_free(nvroot);
450 	zpool_close(zhp);
451 
452 	return (ret);
453 }
454 
455 /*
456  * zpool remove <pool> <vdev>
457  *
458  * Removes the given vdev from the pool.  Currently, this only supports removing
459  * spares from the pool.  Eventually, we'll want to support removing leaf vdevs
460  * (as an alias for 'detach') as well as toplevel vdevs.
461  */
462 int
463 zpool_do_remove(int argc, char **argv)
464 {
465 	char *poolname;
466 	int ret;
467 	zpool_handle_t *zhp;
468 
469 	argc--;
470 	argv++;
471 
472 	/* get pool name and check number of arguments */
473 	if (argc < 1) {
474 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
475 		usage(B_FALSE);
476 	}
477 	if (argc < 2) {
478 		(void) fprintf(stderr, gettext("missing device\n"));
479 		usage(B_FALSE);
480 	}
481 
482 	poolname = argv[0];
483 
484 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
485 		return (1);
486 
487 	ret = (zpool_vdev_remove(zhp, argv[1]) != 0);
488 	if (!ret) {
489 		zpool_log_history(g_zfs, ++argc, --argv, poolname, B_TRUE,
490 		    B_FALSE);
491 	}
492 
493 	return (ret);
494 }
495 
496 /*
497  * zpool create [-fn] [-R root] [-m mountpoint] <pool> <dev> ...
498  *
499  *	-f	Force creation, even if devices appear in use
500  *	-n	Do not create the pool, but display the resulting layout if it
501  *		were to be created.
502  *      -R	Create a pool under an alternate root
503  *      -m	Set default mountpoint for the root dataset.  By default it's
504  *      	'/<pool>'
505  *
506  * Creates the the named pool according to the given vdev specification.  The
507  * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c.  Once
508  * we get the nvlist back from get_vdev_spec(), we either print out the contents
509  * (if '-n' was specified), or pass it to libzfs to do the creation.
510  */
511 int
512 zpool_do_create(int argc, char **argv)
513 {
514 	boolean_t force = B_FALSE;
515 	boolean_t dryrun = B_FALSE;
516 	int c;
517 	nvlist_t *nvroot;
518 	char *poolname;
519 	int ret;
520 	char *altroot = NULL;
521 	char *mountpoint = NULL;
522 	nvlist_t **child;
523 	uint_t children;
524 
525 	/* check options */
526 	while ((c = getopt(argc, argv, ":fnR:m:")) != -1) {
527 		switch (c) {
528 		case 'f':
529 			force = B_TRUE;
530 			break;
531 		case 'n':
532 			dryrun = B_TRUE;
533 			break;
534 		case 'R':
535 			altroot = optarg;
536 			break;
537 		case 'm':
538 			mountpoint = optarg;
539 			break;
540 		case ':':
541 			(void) fprintf(stderr, gettext("missing argument for "
542 			    "'%c' option\n"), optopt);
543 			usage(B_FALSE);
544 			break;
545 		case '?':
546 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
547 			    optopt);
548 			usage(B_FALSE);
549 		}
550 	}
551 
552 	argc -= optind;
553 	argv += optind;
554 
555 	/* get pool name and check number of arguments */
556 	if (argc < 1) {
557 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
558 		usage(B_FALSE);
559 	}
560 	if (argc < 2) {
561 		(void) fprintf(stderr, gettext("missing vdev specification\n"));
562 		usage(B_FALSE);
563 	}
564 
565 	poolname = argv[0];
566 
567 	/*
568 	 * As a special case, check for use of '/' in the name, and direct the
569 	 * user to use 'zfs create' instead.
570 	 */
571 	if (strchr(poolname, '/') != NULL) {
572 		(void) fprintf(stderr, gettext("cannot create '%s': invalid "
573 		    "character '/' in pool name\n"), poolname);
574 		(void) fprintf(stderr, gettext("use 'zfs create' to "
575 		    "create a dataset\n"));
576 		return (1);
577 	}
578 
579 	/* pass off to get_vdev_spec for bulk processing */
580 	nvroot = make_root_vdev(NULL, force, !force, B_FALSE, argc - 1,
581 	    argv + 1);
582 	if (nvroot == NULL)
583 		return (1);
584 
585 	/* make_root_vdev() allows 0 toplevel children if there are spares */
586 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
587 	    &child, &children) == 0);
588 	if (children == 0) {
589 		(void) fprintf(stderr, gettext("invalid vdev "
590 		    "specification: at least one toplevel vdev must be "
591 		    "specified\n"));
592 		return (1);
593 	}
594 
595 
596 	if (altroot != NULL && altroot[0] != '/') {
597 		(void) fprintf(stderr, gettext("invalid alternate root '%s': "
598 		    "must be an absolute path\n"), altroot);
599 		nvlist_free(nvroot);
600 		return (1);
601 	}
602 
603 	/*
604 	 * Check the validity of the mountpoint and direct the user to use the
605 	 * '-m' mountpoint option if it looks like its in use.
606 	 */
607 	if (mountpoint == NULL ||
608 	    (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
609 	    strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
610 		char buf[MAXPATHLEN];
611 		struct stat64 statbuf;
612 
613 		if (mountpoint && mountpoint[0] != '/') {
614 			(void) fprintf(stderr, gettext("invalid mountpoint "
615 			    "'%s': must be an absolute path, 'legacy', or "
616 			    "'none'\n"), mountpoint);
617 			nvlist_free(nvroot);
618 			return (1);
619 		}
620 
621 		if (mountpoint == NULL) {
622 			if (altroot != NULL)
623 				(void) snprintf(buf, sizeof (buf), "%s/%s",
624 				    altroot, poolname);
625 			else
626 				(void) snprintf(buf, sizeof (buf), "/%s",
627 				    poolname);
628 		} else {
629 			if (altroot != NULL)
630 				(void) snprintf(buf, sizeof (buf), "%s%s",
631 				    altroot, mountpoint);
632 			else
633 				(void) snprintf(buf, sizeof (buf), "%s",
634 				    mountpoint);
635 		}
636 
637 		if (stat64(buf, &statbuf) == 0 &&
638 		    statbuf.st_nlink != 2) {
639 			if (mountpoint == NULL)
640 				(void) fprintf(stderr, gettext("default "
641 				    "mountpoint '%s' exists and is not "
642 				    "empty\n"), buf);
643 			else
644 				(void) fprintf(stderr, gettext("mountpoint "
645 				    "'%s' exists and is not empty\n"), buf);
646 			(void) fprintf(stderr, gettext("use '-m' "
647 			    "option to provide a different default\n"));
648 			nvlist_free(nvroot);
649 			return (1);
650 		}
651 	}
652 
653 
654 	if (dryrun) {
655 		/*
656 		 * For a dry run invocation, print out a basic message and run
657 		 * through all the vdevs in the list and print out in an
658 		 * appropriate hierarchy.
659 		 */
660 		(void) printf(gettext("would create '%s' with the "
661 		    "following layout:\n\n"), poolname);
662 
663 		print_vdev_tree(NULL, poolname, nvroot, 0);
664 
665 		ret = 0;
666 	} else {
667 		ret = 1;
668 		/*
669 		 * Hand off to libzfs.
670 		 */
671 		if (zpool_create(g_zfs, poolname, nvroot, altroot) == 0) {
672 			zfs_handle_t *pool = zfs_open(g_zfs, poolname,
673 			    ZFS_TYPE_FILESYSTEM);
674 			if (pool != NULL) {
675 				if (mountpoint != NULL)
676 					verify(zfs_prop_set(pool,
677 					    zfs_prop_to_name(
678 					    ZFS_PROP_MOUNTPOINT),
679 					    mountpoint) == 0);
680 				if (zfs_mount(pool, NULL, 0) == 0)
681 					ret = zfs_share_nfs(pool);
682 				zfs_close(pool);
683 			}
684 			zpool_log_history(g_zfs, argc + optind, argv - optind,
685 			    poolname, B_TRUE, B_TRUE);
686 		} else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
687 			(void) fprintf(stderr, gettext("pool name may have "
688 			    "been omitted\n"));
689 		}
690 	}
691 
692 	nvlist_free(nvroot);
693 
694 	return (ret);
695 }
696 
697 /*
698  * zpool destroy <pool>
699  *
700  * 	-f	Forcefully unmount any datasets
701  *
702  * Destroy the given pool.  Automatically unmounts any datasets in the pool.
703  */
704 int
705 zpool_do_destroy(int argc, char **argv)
706 {
707 	boolean_t force = B_FALSE;
708 	int c;
709 	char *pool;
710 	zpool_handle_t *zhp;
711 	int ret;
712 
713 	/* check options */
714 	while ((c = getopt(argc, argv, "f")) != -1) {
715 		switch (c) {
716 		case 'f':
717 			force = B_TRUE;
718 			break;
719 		case '?':
720 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
721 			    optopt);
722 			usage(B_FALSE);
723 		}
724 	}
725 
726 	argc -= optind;
727 	argv += optind;
728 
729 	/* check arguments */
730 	if (argc < 1) {
731 		(void) fprintf(stderr, gettext("missing pool argument\n"));
732 		usage(B_FALSE);
733 	}
734 	if (argc > 1) {
735 		(void) fprintf(stderr, gettext("too many arguments\n"));
736 		usage(B_FALSE);
737 	}
738 
739 	pool = argv[0];
740 
741 	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
742 		/*
743 		 * As a special case, check for use of '/' in the name, and
744 		 * direct the user to use 'zfs destroy' instead.
745 		 */
746 		if (strchr(pool, '/') != NULL)
747 			(void) fprintf(stderr, gettext("use 'zfs destroy' to "
748 			    "destroy a dataset\n"));
749 		return (1);
750 	}
751 
752 	if (zpool_disable_datasets(zhp, force) != 0) {
753 		(void) fprintf(stderr, gettext("could not destroy '%s': "
754 		    "could not unmount datasets\n"), zpool_get_name(zhp));
755 		return (1);
756 	}
757 
758 	zpool_log_history(g_zfs, argc + optind, argv - optind, pool, B_TRUE,
759 	    B_FALSE);
760 
761 	ret = (zpool_destroy(zhp) != 0);
762 
763 	zpool_close(zhp);
764 
765 	return (ret);
766 }
767 
768 /*
769  * zpool export [-f] <pool> ...
770  *
771  *	-f	Forcefully unmount datasets
772  *
773  * Export the the given pools.  By default, the command will attempt to cleanly
774  * unmount any active datasets within the pool.  If the '-f' flag is specified,
775  * then the datasets will be forcefully unmounted.
776  */
777 int
778 zpool_do_export(int argc, char **argv)
779 {
780 	boolean_t force = B_FALSE;
781 	int c;
782 	zpool_handle_t *zhp;
783 	int ret;
784 	int i;
785 
786 	/* check options */
787 	while ((c = getopt(argc, argv, "f")) != -1) {
788 		switch (c) {
789 		case 'f':
790 			force = B_TRUE;
791 			break;
792 		case '?':
793 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
794 			    optopt);
795 			usage(B_FALSE);
796 		}
797 	}
798 
799 	argc -= optind;
800 	argv += optind;
801 
802 	/* check arguments */
803 	if (argc < 1) {
804 		(void) fprintf(stderr, gettext("missing pool argument\n"));
805 		usage(B_FALSE);
806 	}
807 
808 	ret = 0;
809 	for (i = 0; i < argc; i++) {
810 		if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
811 			ret = 1;
812 			continue;
813 		}
814 
815 		if (zpool_disable_datasets(zhp, force) != 0) {
816 			ret = 1;
817 			zpool_close(zhp);
818 			continue;
819 		}
820 
821 		zpool_log_history(g_zfs, argc + optind, argv - optind, argv[i],
822 		    B_TRUE, B_FALSE);
823 
824 		if (zpool_export(zhp) != 0)
825 			ret = 1;
826 
827 		zpool_close(zhp);
828 	}
829 
830 	return (ret);
831 }
832 
833 /*
834  * Given a vdev configuration, determine the maximum width needed for the device
835  * name column.
836  */
837 static int
838 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
839 {
840 	char *name = zpool_vdev_name(g_zfs, zhp, nv);
841 	nvlist_t **child;
842 	uint_t c, children;
843 	int ret;
844 
845 	if (strlen(name) + depth > max)
846 		max = strlen(name) + depth;
847 
848 	free(name);
849 
850 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
851 	    &child, &children) == 0) {
852 		for (c = 0; c < children; c++)
853 			if ((ret = max_width(zhp, child[c], depth + 2,
854 			    max)) > max)
855 				max = ret;
856 	}
857 
858 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
859 	    &child, &children) == 0) {
860 		for (c = 0; c < children; c++)
861 			if ((ret = max_width(zhp, child[c], depth + 2,
862 			    max)) > max)
863 				max = ret;
864 	}
865 
866 
867 	return (max);
868 }
869 
870 
871 /*
872  * Print the configuration of an exported pool.  Iterate over all vdevs in the
873  * pool, printing out the name and status for each one.
874  */
875 void
876 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
877 {
878 	nvlist_t **child;
879 	uint_t c, children;
880 	vdev_stat_t *vs;
881 	char *type, *vname;
882 
883 	verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
884 	if (strcmp(type, VDEV_TYPE_MISSING) == 0)
885 		return;
886 
887 	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
888 	    (uint64_t **)&vs, &c) == 0);
889 
890 	(void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
891 
892 	if (vs->vs_aux != 0) {
893 		(void) printf("  %-8s  ", state_to_name(vs));
894 
895 		switch (vs->vs_aux) {
896 		case VDEV_AUX_OPEN_FAILED:
897 			(void) printf(gettext("cannot open"));
898 			break;
899 
900 		case VDEV_AUX_BAD_GUID_SUM:
901 			(void) printf(gettext("missing device"));
902 			break;
903 
904 		case VDEV_AUX_NO_REPLICAS:
905 			(void) printf(gettext("insufficient replicas"));
906 			break;
907 
908 		case VDEV_AUX_VERSION_NEWER:
909 			(void) printf(gettext("newer version"));
910 			break;
911 
912 		default:
913 			(void) printf(gettext("corrupted data"));
914 			break;
915 		}
916 	} else {
917 		(void) printf("  %s", state_to_name(vs));
918 	}
919 	(void) printf("\n");
920 
921 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
922 	    &child, &children) != 0)
923 		return;
924 
925 	for (c = 0; c < children; c++) {
926 		vname = zpool_vdev_name(g_zfs, NULL, child[c]);
927 		print_import_config(vname, child[c],
928 		    namewidth, depth + 2);
929 		free(vname);
930 	}
931 
932 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
933 	    &child, &children) != 0)
934 		return;
935 
936 	(void) printf(gettext("\tspares\n"));
937 	for (c = 0; c < children; c++) {
938 		vname = zpool_vdev_name(g_zfs, NULL, child[c]);
939 		(void) printf("\t  %s\n", vname);
940 		free(vname);
941 	}
942 }
943 
944 /*
945  * Display the status for the given pool.
946  */
947 static void
948 show_import(nvlist_t *config)
949 {
950 	uint64_t pool_state;
951 	vdev_stat_t *vs;
952 	char *name;
953 	uint64_t guid;
954 	char *msgid;
955 	nvlist_t *nvroot;
956 	int reason;
957 	char *health;
958 	uint_t vsc;
959 	int namewidth;
960 
961 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
962 	    &name) == 0);
963 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
964 	    &guid) == 0);
965 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
966 	    &pool_state) == 0);
967 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_HEALTH,
968 	    &health) == 0);
969 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
970 	    &nvroot) == 0);
971 
972 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
973 	    (uint64_t **)&vs, &vsc) == 0);
974 
975 	reason = zpool_import_status(config, &msgid);
976 
977 	(void) printf("  pool: %s\n", name);
978 	(void) printf("    id: %llu\n", (u_longlong_t)guid);
979 	(void) printf(" state: %s", health);
980 	if (pool_state == POOL_STATE_DESTROYED)
981 	    (void) printf(" (DESTROYED)");
982 	(void) printf("\n");
983 
984 	switch (reason) {
985 	case ZPOOL_STATUS_MISSING_DEV_R:
986 	case ZPOOL_STATUS_MISSING_DEV_NR:
987 	case ZPOOL_STATUS_BAD_GUID_SUM:
988 		(void) printf(gettext("status: One or more devices are missing "
989 		    "from the system.\n"));
990 		break;
991 
992 	case ZPOOL_STATUS_CORRUPT_LABEL_R:
993 	case ZPOOL_STATUS_CORRUPT_LABEL_NR:
994 		(void) printf(gettext("status: One or more devices contains "
995 		    "corrupted data.\n"));
996 		break;
997 
998 	case ZPOOL_STATUS_CORRUPT_DATA:
999 		(void) printf(gettext("status: The pool data is corrupted.\n"));
1000 		break;
1001 
1002 	case ZPOOL_STATUS_OFFLINE_DEV:
1003 		(void) printf(gettext("status: One or more devices "
1004 		    "are offlined.\n"));
1005 		break;
1006 
1007 	case ZPOOL_STATUS_CORRUPT_POOL:
1008 		(void) printf(gettext("status: The pool metadata is "
1009 		    "corrupted.\n"));
1010 		break;
1011 
1012 	case ZPOOL_STATUS_VERSION_OLDER:
1013 		(void) printf(gettext("status: The pool is formatted using an "
1014 		    "older on-disk version.\n"));
1015 		break;
1016 
1017 	case ZPOOL_STATUS_VERSION_NEWER:
1018 		(void) printf(gettext("status: The pool is formatted using an "
1019 		    "incompatible version.\n"));
1020 		break;
1021 
1022 	default:
1023 		/*
1024 		 * No other status can be seen when importing pools.
1025 		 */
1026 		assert(reason == ZPOOL_STATUS_OK);
1027 	}
1028 
1029 	/*
1030 	 * Print out an action according to the overall state of the pool.
1031 	 */
1032 	if (strcmp(health, gettext("ONLINE")) == 0) {
1033 		if (reason == ZPOOL_STATUS_VERSION_OLDER)
1034 			(void) printf(gettext("action: The pool can be "
1035 			    "imported using its name or numeric identifier, "
1036 			    "though\n\tsome features will not be available "
1037 			    "without an explicit 'zpool upgrade'.\n"));
1038 		else
1039 			(void) printf(gettext("action: The pool can be "
1040 			    "imported using its name or numeric "
1041 			    "identifier.\n"));
1042 	} else if (strcmp(health, gettext("DEGRADED")) == 0) {
1043 		(void) printf(gettext("action: The pool can be imported "
1044 		    "despite missing or damaged devices.  The\n\tfault "
1045 		    "tolerance of the pool may be compromised if imported.\n"));
1046 	} else {
1047 		switch (reason) {
1048 		case ZPOOL_STATUS_VERSION_NEWER:
1049 			(void) printf(gettext("action: The pool cannot be "
1050 			    "imported.  Access the pool on a system running "
1051 			    "newer\n\tsoftware, or recreate the pool from "
1052 			    "backup.\n"));
1053 			break;
1054 		case ZPOOL_STATUS_MISSING_DEV_R:
1055 		case ZPOOL_STATUS_MISSING_DEV_NR:
1056 		case ZPOOL_STATUS_BAD_GUID_SUM:
1057 			(void) printf(gettext("action: The pool cannot be "
1058 			    "imported. Attach the missing\n\tdevices and try "
1059 			    "again.\n"));
1060 			break;
1061 		default:
1062 			(void) printf(gettext("action: The pool cannot be "
1063 			    "imported due to damaged devices or data.\n"));
1064 		}
1065 	}
1066 
1067 	if (strcmp(health, gettext("FAULTED")) != 0) {
1068 		if (pool_state == POOL_STATE_DESTROYED)
1069 			(void) printf(gettext("\tThe pool was destroyed, "
1070 			    "but can be imported using the '-Df' flags.\n"));
1071 		else if (pool_state != POOL_STATE_EXPORTED)
1072 			(void) printf(gettext("\tThe pool may be active on "
1073 			    "on another system, but can be imported using\n\t"
1074 			    "the '-f' flag.\n"));
1075 	}
1076 
1077 	if (msgid != NULL)
1078 		(void) printf(gettext("   see: http://www.sun.com/msg/%s\n"),
1079 		    msgid);
1080 
1081 	(void) printf(gettext("config:\n\n"));
1082 
1083 	namewidth = max_width(NULL, nvroot, 0, 0);
1084 	if (namewidth < 10)
1085 		namewidth = 10;
1086 	print_import_config(name, nvroot, namewidth, 0);
1087 
1088 	if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1089 		(void) printf("\n\tAdditional devices are known to "
1090 		    "be part of this pool, though their\n\texact "
1091 		    "configuration cannot be determined.\n");
1092 	}
1093 }
1094 
1095 /*
1096  * Perform the import for the given configuration.  This passes the heavy
1097  * lifting off to zpool_import(), and then mounts the datasets contained within
1098  * the pool.
1099  */
1100 static int
1101 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1102     const char *altroot, int force, int argc, char **argv)
1103 {
1104 	zpool_handle_t *zhp;
1105 	char *name;
1106 	uint64_t state;
1107 	uint64_t version;
1108 
1109 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1110 	    &name) == 0);
1111 
1112 	verify(nvlist_lookup_uint64(config,
1113 	    ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1114 	verify(nvlist_lookup_uint64(config,
1115 	    ZPOOL_CONFIG_VERSION, &version) == 0);
1116 	if (version > ZFS_VERSION) {
1117 		(void) fprintf(stderr, gettext("cannot import '%s': pool "
1118 		    "is formatted using a newer ZFS version\n"), name);
1119 		return (1);
1120 	} else if (state != POOL_STATE_EXPORTED && !force) {
1121 		(void) fprintf(stderr, gettext("cannot import '%s': pool "
1122 		    "may be in use from other system\n"), name);
1123 		(void) fprintf(stderr, gettext("use '-f' to import anyway\n"));
1124 		return (1);
1125 	}
1126 
1127 	if (zpool_import(g_zfs, config, newname, altroot) != 0)
1128 		return (1);
1129 
1130 	if (newname != NULL)
1131 		name = (char *)newname;
1132 
1133 	zpool_log_history(g_zfs, argc, argv, name, B_TRUE, B_FALSE);
1134 
1135 	verify((zhp = zpool_open(g_zfs, name)) != NULL);
1136 
1137 	if (zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1138 		zpool_close(zhp);
1139 		return (1);
1140 	}
1141 
1142 	zpool_close(zhp);
1143 	return (0);
1144 }
1145 
1146 /*
1147  * zpool import [-d dir] [-D]
1148  *       import [-R root] [-D] [-d dir] [-f] -a
1149  *       import [-R root] [-D] [-d dir] [-f] <pool | id> [newpool]
1150  *
1151  *       -d	Scan in a specific directory, other than /dev/dsk.  More than
1152  *		one directory can be specified using multiple '-d' options.
1153  *
1154  *       -D     Scan for previously destroyed pools or import all or only
1155  *              specified destroyed pools.
1156  *
1157  *       -R	Temporarily import the pool, with all mountpoints relative to
1158  *		the given root.  The pool will remain exported when the machine
1159  *		is rebooted.
1160  *
1161  *       -f	Force import, even if it appears that the pool is active.
1162  *
1163  *       -a	Import all pools found.
1164  *
1165  * The import command scans for pools to import, and import pools based on pool
1166  * name and GUID.  The pool can also be renamed as part of the import process.
1167  */
1168 int
1169 zpool_do_import(int argc, char **argv)
1170 {
1171 	char **searchdirs = NULL;
1172 	int nsearch = 0;
1173 	int c;
1174 	int err;
1175 	nvlist_t *pools;
1176 	boolean_t do_all = B_FALSE;
1177 	boolean_t do_destroyed = B_FALSE;
1178 	char *altroot = NULL;
1179 	char *mntopts = NULL;
1180 	boolean_t do_force = B_FALSE;
1181 	nvpair_t *elem;
1182 	nvlist_t *config;
1183 	uint64_t searchguid;
1184 	char *searchname;
1185 	nvlist_t *found_config;
1186 	boolean_t first;
1187 	uint64_t pool_state;
1188 
1189 	/* check options */
1190 	while ((c = getopt(argc, argv, ":Dfd:R:ao:")) != -1) {
1191 		switch (c) {
1192 		case 'a':
1193 			do_all = B_TRUE;
1194 			break;
1195 		case 'd':
1196 			if (searchdirs == NULL) {
1197 				searchdirs = safe_malloc(sizeof (char *));
1198 			} else {
1199 				char **tmp = safe_malloc((nsearch + 1) *
1200 				    sizeof (char *));
1201 				bcopy(searchdirs, tmp, nsearch *
1202 				    sizeof (char *));
1203 				free(searchdirs);
1204 				searchdirs = tmp;
1205 			}
1206 			searchdirs[nsearch++] = optarg;
1207 			break;
1208 		case 'D':
1209 			do_destroyed = B_TRUE;
1210 			break;
1211 		case 'f':
1212 			do_force = B_TRUE;
1213 			break;
1214 		case 'o':
1215 			mntopts = optarg;
1216 			break;
1217 		case 'R':
1218 			altroot = optarg;
1219 			break;
1220 		case ':':
1221 			(void) fprintf(stderr, gettext("missing argument for "
1222 			    "'%c' option\n"), optopt);
1223 			usage(B_FALSE);
1224 			break;
1225 		case '?':
1226 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1227 			    optopt);
1228 			usage(B_FALSE);
1229 		}
1230 	}
1231 
1232 	argc -= optind;
1233 	argv += optind;
1234 
1235 	if (searchdirs == NULL) {
1236 		searchdirs = safe_malloc(sizeof (char *));
1237 		searchdirs[0] = "/dev/dsk";
1238 		nsearch = 1;
1239 	}
1240 
1241 	/* check argument count */
1242 	if (do_all) {
1243 		if (argc != 0) {
1244 			(void) fprintf(stderr, gettext("too many arguments\n"));
1245 			usage(B_FALSE);
1246 		}
1247 	} else {
1248 		if (argc > 2) {
1249 			(void) fprintf(stderr, gettext("too many arguments\n"));
1250 			usage(B_FALSE);
1251 		}
1252 
1253 		/*
1254 		 * Check for the SYS_CONFIG privilege.  We do this explicitly
1255 		 * here because otherwise any attempt to discover pools will
1256 		 * silently fail.
1257 		 */
1258 		if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
1259 			(void) fprintf(stderr, gettext("cannot "
1260 			    "discover pools: permission denied\n"));
1261 			free(searchdirs);
1262 			return (1);
1263 		}
1264 	}
1265 
1266 	if ((pools = zpool_find_import(g_zfs, nsearch, searchdirs)) == NULL) {
1267 		free(searchdirs);
1268 		return (1);
1269 	}
1270 
1271 	/*
1272 	 * We now have a list of all available pools in the given directories.
1273 	 * Depending on the arguments given, we do one of the following:
1274 	 *
1275 	 *	<none>	Iterate through all pools and display information about
1276 	 *		each one.
1277 	 *
1278 	 *	-a	Iterate through all pools and try to import each one.
1279 	 *
1280 	 *	<id>	Find the pool that corresponds to the given GUID/pool
1281 	 *		name and import that one.
1282 	 *
1283 	 *	-D	Above options applies only to destroyed pools.
1284 	 */
1285 	if (argc != 0) {
1286 		char *endptr;
1287 
1288 		errno = 0;
1289 		searchguid = strtoull(argv[0], &endptr, 10);
1290 		if (errno != 0 || *endptr != '\0')
1291 			searchname = argv[0];
1292 		else
1293 			searchname = NULL;
1294 		found_config = NULL;
1295 	}
1296 
1297 	err = 0;
1298 	elem = NULL;
1299 	first = B_TRUE;
1300 	while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
1301 
1302 		verify(nvpair_value_nvlist(elem, &config) == 0);
1303 
1304 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1305 		    &pool_state) == 0);
1306 		if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
1307 			continue;
1308 		if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
1309 			continue;
1310 
1311 		if (argc == 0) {
1312 			if (first)
1313 				first = B_FALSE;
1314 			else if (!do_all)
1315 				(void) printf("\n");
1316 
1317 			if (do_all)
1318 				err |= do_import(config, NULL, mntopts,
1319 				    altroot, do_force, argc + optind,
1320 				    argv - optind);
1321 			else
1322 				show_import(config);
1323 		} else if (searchname != NULL) {
1324 			char *name;
1325 
1326 			/*
1327 			 * We are searching for a pool based on name.
1328 			 */
1329 			verify(nvlist_lookup_string(config,
1330 			    ZPOOL_CONFIG_POOL_NAME, &name) == 0);
1331 
1332 			if (strcmp(name, searchname) == 0) {
1333 				if (found_config != NULL) {
1334 					(void) fprintf(stderr, gettext(
1335 					    "cannot import '%s': more than "
1336 					    "one matching pool\n"), searchname);
1337 					(void) fprintf(stderr, gettext(
1338 					    "import by numeric ID instead\n"));
1339 					err = B_TRUE;
1340 				}
1341 				found_config = config;
1342 			}
1343 		} else {
1344 			uint64_t guid;
1345 
1346 			/*
1347 			 * Search for a pool by guid.
1348 			 */
1349 			verify(nvlist_lookup_uint64(config,
1350 			    ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
1351 
1352 			if (guid == searchguid)
1353 				found_config = config;
1354 		}
1355 	}
1356 
1357 	/*
1358 	 * If we were searching for a specific pool, verify that we found a
1359 	 * pool, and then do the import.
1360 	 */
1361 	if (argc != 0 && err == 0) {
1362 		if (found_config == NULL) {
1363 			(void) fprintf(stderr, gettext("cannot import '%s': "
1364 			    "no such pool available\n"), argv[0]);
1365 			err = B_TRUE;
1366 		} else {
1367 			err |= do_import(found_config, argc == 1 ? NULL :
1368 			    argv[1], mntopts, altroot, do_force, argc + optind,
1369 			    argv - optind);
1370 		}
1371 	}
1372 
1373 	/*
1374 	 * If we were just looking for pools, report an error if none were
1375 	 * found.
1376 	 */
1377 	if (argc == 0 && first)
1378 		(void) fprintf(stderr,
1379 		    gettext("no pools available to import\n"));
1380 
1381 	nvlist_free(pools);
1382 	free(searchdirs);
1383 
1384 	return (err ? 1 : 0);
1385 }
1386 
1387 typedef struct iostat_cbdata {
1388 	zpool_list_t *cb_list;
1389 	int cb_verbose;
1390 	int cb_iteration;
1391 	int cb_namewidth;
1392 } iostat_cbdata_t;
1393 
1394 static void
1395 print_iostat_separator(iostat_cbdata_t *cb)
1396 {
1397 	int i = 0;
1398 
1399 	for (i = 0; i < cb->cb_namewidth; i++)
1400 		(void) printf("-");
1401 	(void) printf("  -----  -----  -----  -----  -----  -----\n");
1402 }
1403 
1404 static void
1405 print_iostat_header(iostat_cbdata_t *cb)
1406 {
1407 	(void) printf("%*s     capacity     operations    bandwidth\n",
1408 	    cb->cb_namewidth, "");
1409 	(void) printf("%-*s   used  avail   read  write   read  write\n",
1410 	    cb->cb_namewidth, "pool");
1411 	print_iostat_separator(cb);
1412 }
1413 
1414 /*
1415  * Display a single statistic.
1416  */
1417 void
1418 print_one_stat(uint64_t value)
1419 {
1420 	char buf[64];
1421 
1422 	zfs_nicenum(value, buf, sizeof (buf));
1423 	(void) printf("  %5s", buf);
1424 }
1425 
1426 /*
1427  * Print out all the statistics for the given vdev.  This can either be the
1428  * toplevel configuration, or called recursively.  If 'name' is NULL, then this
1429  * is a verbose output, and we don't want to display the toplevel pool stats.
1430  */
1431 void
1432 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
1433     nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
1434 {
1435 	nvlist_t **oldchild, **newchild;
1436 	uint_t c, children;
1437 	vdev_stat_t *oldvs, *newvs;
1438 	vdev_stat_t zerovs = { 0 };
1439 	uint64_t tdelta;
1440 	double scale;
1441 	char *vname;
1442 
1443 	if (oldnv != NULL) {
1444 		verify(nvlist_lookup_uint64_array(oldnv, ZPOOL_CONFIG_STATS,
1445 		    (uint64_t **)&oldvs, &c) == 0);
1446 	} else {
1447 		oldvs = &zerovs;
1448 	}
1449 
1450 	verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_STATS,
1451 	    (uint64_t **)&newvs, &c) == 0);
1452 
1453 	if (strlen(name) + depth > cb->cb_namewidth)
1454 		(void) printf("%*s%s", depth, "", name);
1455 	else
1456 		(void) printf("%*s%s%*s", depth, "", name,
1457 		    (int)(cb->cb_namewidth - strlen(name) - depth), "");
1458 
1459 	tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
1460 
1461 	if (tdelta == 0)
1462 		scale = 1.0;
1463 	else
1464 		scale = (double)NANOSEC / tdelta;
1465 
1466 	/* only toplevel vdevs have capacity stats */
1467 	if (newvs->vs_space == 0) {
1468 		(void) printf("      -      -");
1469 	} else {
1470 		print_one_stat(newvs->vs_alloc);
1471 		print_one_stat(newvs->vs_space - newvs->vs_alloc);
1472 	}
1473 
1474 	print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
1475 	    oldvs->vs_ops[ZIO_TYPE_READ])));
1476 
1477 	print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
1478 	    oldvs->vs_ops[ZIO_TYPE_WRITE])));
1479 
1480 	print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
1481 	    oldvs->vs_bytes[ZIO_TYPE_READ])));
1482 
1483 	print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
1484 	    oldvs->vs_bytes[ZIO_TYPE_WRITE])));
1485 
1486 	(void) printf("\n");
1487 
1488 	if (!cb->cb_verbose)
1489 		return;
1490 
1491 	if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
1492 	    &newchild, &children) != 0)
1493 		return;
1494 
1495 	if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
1496 	    &oldchild, &c) != 0)
1497 		return;
1498 
1499 	for (c = 0; c < children; c++) {
1500 		vname = zpool_vdev_name(g_zfs, zhp, newchild[c]);
1501 		print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
1502 		    newchild[c], cb, depth + 2);
1503 		free(vname);
1504 	}
1505 }
1506 
1507 static int
1508 refresh_iostat(zpool_handle_t *zhp, void *data)
1509 {
1510 	iostat_cbdata_t *cb = data;
1511 	boolean_t missing;
1512 
1513 	/*
1514 	 * If the pool has disappeared, remove it from the list and continue.
1515 	 */
1516 	if (zpool_refresh_stats(zhp, &missing) != 0)
1517 		return (-1);
1518 
1519 	if (missing)
1520 		pool_list_remove(cb->cb_list, zhp);
1521 
1522 	return (0);
1523 }
1524 
1525 /*
1526  * Callback to print out the iostats for the given pool.
1527  */
1528 int
1529 print_iostat(zpool_handle_t *zhp, void *data)
1530 {
1531 	iostat_cbdata_t *cb = data;
1532 	nvlist_t *oldconfig, *newconfig;
1533 	nvlist_t *oldnvroot, *newnvroot;
1534 
1535 	newconfig = zpool_get_config(zhp, &oldconfig);
1536 
1537 	if (cb->cb_iteration == 1)
1538 		oldconfig = NULL;
1539 
1540 	verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
1541 	    &newnvroot) == 0);
1542 
1543 	if (oldconfig == NULL)
1544 		oldnvroot = NULL;
1545 	else
1546 		verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
1547 		    &oldnvroot) == 0);
1548 
1549 	/*
1550 	 * Print out the statistics for the pool.
1551 	 */
1552 	print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
1553 
1554 	if (cb->cb_verbose)
1555 		print_iostat_separator(cb);
1556 
1557 	return (0);
1558 }
1559 
1560 int
1561 get_namewidth(zpool_handle_t *zhp, void *data)
1562 {
1563 	iostat_cbdata_t *cb = data;
1564 	nvlist_t *config, *nvroot;
1565 
1566 	if ((config = zpool_get_config(zhp, NULL)) != NULL) {
1567 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1568 		    &nvroot) == 0);
1569 		if (!cb->cb_verbose)
1570 			cb->cb_namewidth = strlen(zpool_get_name(zhp));
1571 		else
1572 			cb->cb_namewidth = max_width(zhp, nvroot, 0, 0);
1573 	}
1574 
1575 	/*
1576 	 * The width must fall into the range [10,38].  The upper limit is the
1577 	 * maximum we can have and still fit in 80 columns.
1578 	 */
1579 	if (cb->cb_namewidth < 10)
1580 		cb->cb_namewidth = 10;
1581 	if (cb->cb_namewidth > 38)
1582 		cb->cb_namewidth = 38;
1583 
1584 	return (0);
1585 }
1586 
1587 /*
1588  * zpool iostat [-v] [pool] ... [interval [count]]
1589  *
1590  *	-v	Display statistics for individual vdevs
1591  *
1592  * This command can be tricky because we want to be able to deal with pool
1593  * creation/destruction as well as vdev configuration changes.  The bulk of this
1594  * processing is handled by the pool_list_* routines in zpool_iter.c.  We rely
1595  * on pool_list_update() to detect the addition of new pools.  Configuration
1596  * changes are all handled within libzfs.
1597  */
1598 int
1599 zpool_do_iostat(int argc, char **argv)
1600 {
1601 	int c;
1602 	int ret;
1603 	int npools;
1604 	unsigned long interval = 0, count = 0;
1605 	zpool_list_t *list;
1606 	boolean_t verbose = B_FALSE;
1607 	iostat_cbdata_t cb;
1608 
1609 	/* check options */
1610 	while ((c = getopt(argc, argv, "v")) != -1) {
1611 		switch (c) {
1612 		case 'v':
1613 			verbose = B_TRUE;
1614 			break;
1615 		case '?':
1616 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1617 			    optopt);
1618 			usage(B_FALSE);
1619 		}
1620 	}
1621 
1622 	argc -= optind;
1623 	argv += optind;
1624 
1625 	/*
1626 	 * Determine if the last argument is an integer or a pool name
1627 	 */
1628 	if (argc > 0 && isdigit(argv[argc - 1][0])) {
1629 		char *end;
1630 
1631 		errno = 0;
1632 		interval = strtoul(argv[argc - 1], &end, 10);
1633 
1634 		if (*end == '\0' && errno == 0) {
1635 			if (interval == 0) {
1636 				(void) fprintf(stderr, gettext("interval "
1637 				    "cannot be zero\n"));
1638 				usage(B_FALSE);
1639 			}
1640 
1641 			/*
1642 			 * Ignore the last parameter
1643 			 */
1644 			argc--;
1645 		} else {
1646 			/*
1647 			 * If this is not a valid number, just plow on.  The
1648 			 * user will get a more informative error message later
1649 			 * on.
1650 			 */
1651 			interval = 0;
1652 		}
1653 	}
1654 
1655 	/*
1656 	 * If the last argument is also an integer, then we have both a count
1657 	 * and an integer.
1658 	 */
1659 	if (argc > 0 && isdigit(argv[argc - 1][0])) {
1660 		char *end;
1661 
1662 		errno = 0;
1663 		count = interval;
1664 		interval = strtoul(argv[argc - 1], &end, 10);
1665 
1666 		if (*end == '\0' && errno == 0) {
1667 			if (interval == 0) {
1668 				(void) fprintf(stderr, gettext("interval "
1669 				    "cannot be zero\n"));
1670 				usage(B_FALSE);
1671 			}
1672 
1673 			/*
1674 			 * Ignore the last parameter
1675 			 */
1676 			argc--;
1677 		} else {
1678 			interval = 0;
1679 		}
1680 	}
1681 
1682 	/*
1683 	 * Construct the list of all interesting pools.
1684 	 */
1685 	ret = 0;
1686 	if ((list = pool_list_get(argc, argv, &ret)) == NULL)
1687 		return (1);
1688 
1689 	if (pool_list_count(list) == 0 && argc != 0) {
1690 		pool_list_free(list);
1691 		return (1);
1692 	}
1693 
1694 	if (pool_list_count(list) == 0 && interval == 0) {
1695 		pool_list_free(list);
1696 		(void) fprintf(stderr, gettext("no pools available\n"));
1697 		return (1);
1698 	}
1699 
1700 	/*
1701 	 * Enter the main iostat loop.
1702 	 */
1703 	cb.cb_list = list;
1704 	cb.cb_verbose = verbose;
1705 	cb.cb_iteration = 0;
1706 	cb.cb_namewidth = 0;
1707 
1708 	for (;;) {
1709 		pool_list_update(list);
1710 
1711 		if ((npools = pool_list_count(list)) == 0)
1712 			break;
1713 
1714 		/*
1715 		 * Refresh all statistics.  This is done as an explicit step
1716 		 * before calculating the maximum name width, so that any
1717 		 * configuration changes are properly accounted for.
1718 		 */
1719 		(void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
1720 
1721 		/*
1722 		 * Iterate over all pools to determine the maximum width
1723 		 * for the pool / device name column across all pools.
1724 		 */
1725 		cb.cb_namewidth = 0;
1726 		(void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
1727 
1728 		/*
1729 		 * If it's the first time, or verbose mode, print the header.
1730 		 */
1731 		if (++cb.cb_iteration == 1 || verbose)
1732 			print_iostat_header(&cb);
1733 
1734 		(void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
1735 
1736 		/*
1737 		 * If there's more than one pool, and we're not in verbose mode
1738 		 * (which prints a separator for us), then print a separator.
1739 		 */
1740 		if (npools > 1 && !verbose)
1741 			print_iostat_separator(&cb);
1742 
1743 		if (verbose)
1744 			(void) printf("\n");
1745 
1746 		/*
1747 		 * Flush the output so that redirection to a file isn't buffered
1748 		 * indefinitely.
1749 		 */
1750 		(void) fflush(stdout);
1751 
1752 		if (interval == 0)
1753 			break;
1754 
1755 		if (count != 0 && --count == 0)
1756 			break;
1757 
1758 		(void) sleep(interval);
1759 	}
1760 
1761 	pool_list_free(list);
1762 
1763 	return (ret);
1764 }
1765 
1766 typedef struct list_cbdata {
1767 	boolean_t	cb_scripted;
1768 	boolean_t	cb_first;
1769 	int		cb_fields[MAX_FIELDS];
1770 	int		cb_fieldcount;
1771 } list_cbdata_t;
1772 
1773 /*
1774  * Given a list of columns to display, output appropriate headers for each one.
1775  */
1776 void
1777 print_header(int *fields, size_t count)
1778 {
1779 	int i;
1780 	column_def_t *col;
1781 	const char *fmt;
1782 
1783 	for (i = 0; i < count; i++) {
1784 		col = &column_table[fields[i]];
1785 		if (i != 0)
1786 			(void) printf("  ");
1787 		if (col->cd_justify == left_justify)
1788 			fmt = "%-*s";
1789 		else
1790 			fmt = "%*s";
1791 
1792 		(void) printf(fmt, i == count - 1 ? strlen(col->cd_title) :
1793 		    col->cd_width, col->cd_title);
1794 	}
1795 
1796 	(void) printf("\n");
1797 }
1798 
1799 int
1800 list_callback(zpool_handle_t *zhp, void *data)
1801 {
1802 	list_cbdata_t *cbp = data;
1803 	nvlist_t *config;
1804 	int i;
1805 	char buf[ZPOOL_MAXNAMELEN];
1806 	uint64_t total;
1807 	uint64_t used;
1808 	const char *fmt;
1809 	column_def_t *col;
1810 
1811 	if (cbp->cb_first) {
1812 		if (!cbp->cb_scripted)
1813 			print_header(cbp->cb_fields, cbp->cb_fieldcount);
1814 		cbp->cb_first = B_FALSE;
1815 	}
1816 
1817 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
1818 		config = NULL;
1819 	} else {
1820 		config = zpool_get_config(zhp, NULL);
1821 		total = zpool_get_space_total(zhp);
1822 		used = zpool_get_space_used(zhp);
1823 	}
1824 
1825 	for (i = 0; i < cbp->cb_fieldcount; i++) {
1826 		if (i != 0) {
1827 			if (cbp->cb_scripted)
1828 				(void) printf("\t");
1829 			else
1830 				(void) printf("  ");
1831 		}
1832 
1833 		col = &column_table[cbp->cb_fields[i]];
1834 
1835 		switch (cbp->cb_fields[i]) {
1836 		case ZPOOL_FIELD_NAME:
1837 			(void) strlcpy(buf, zpool_get_name(zhp), sizeof (buf));
1838 			break;
1839 
1840 		case ZPOOL_FIELD_SIZE:
1841 			if (config == NULL)
1842 				(void) strlcpy(buf, "-", sizeof (buf));
1843 			else
1844 				zfs_nicenum(total, buf, sizeof (buf));
1845 			break;
1846 
1847 		case ZPOOL_FIELD_USED:
1848 			if (config == NULL)
1849 				(void) strlcpy(buf, "-", sizeof (buf));
1850 			else
1851 				zfs_nicenum(used, buf, sizeof (buf));
1852 			break;
1853 
1854 		case ZPOOL_FIELD_AVAILABLE:
1855 			if (config == NULL)
1856 				(void) strlcpy(buf, "-", sizeof (buf));
1857 			else
1858 				zfs_nicenum(total - used, buf, sizeof (buf));
1859 			break;
1860 
1861 		case ZPOOL_FIELD_CAPACITY:
1862 			if (config == NULL) {
1863 				(void) strlcpy(buf, "-", sizeof (buf));
1864 			} else {
1865 				uint64_t capacity = (total == 0 ? 0 :
1866 				    (used * 100 / total));
1867 				(void) snprintf(buf, sizeof (buf), "%llu%%",
1868 				    (u_longlong_t)capacity);
1869 			}
1870 			break;
1871 
1872 		case ZPOOL_FIELD_HEALTH:
1873 			if (config == NULL) {
1874 				(void) strlcpy(buf, "FAULTED", sizeof (buf));
1875 			} else {
1876 				nvlist_t *nvroot;
1877 				vdev_stat_t *vs;
1878 				uint_t vsc;
1879 
1880 				verify(nvlist_lookup_nvlist(config,
1881 				    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1882 				verify(nvlist_lookup_uint64_array(nvroot,
1883 				    ZPOOL_CONFIG_STATS, (uint64_t **)&vs,
1884 				    &vsc) == 0);
1885 				(void) strlcpy(buf, state_to_name(vs),
1886 				    sizeof (buf));
1887 			}
1888 			break;
1889 
1890 		case ZPOOL_FIELD_ROOT:
1891 			if (config == NULL)
1892 				(void) strlcpy(buf, "-", sizeof (buf));
1893 			else if (zpool_get_root(zhp, buf, sizeof (buf)) != 0)
1894 				(void) strlcpy(buf, "-", sizeof (buf));
1895 			break;
1896 		}
1897 
1898 		if (cbp->cb_scripted)
1899 			(void) printf("%s", buf);
1900 		else {
1901 			if (col->cd_justify == left_justify)
1902 				fmt = "%-*s";
1903 			else
1904 				fmt = "%*s";
1905 
1906 			(void) printf(fmt, i == cbp->cb_fieldcount - 1 ?
1907 			    strlen(buf) : col->cd_width, buf);
1908 		}
1909 	}
1910 
1911 	(void) printf("\n");
1912 
1913 	return (0);
1914 }
1915 
1916 /*
1917  * zpool list [-H] [-o field[,field]*] [pool] ...
1918  *
1919  *	-H	Scripted mode.  Don't display headers, and separate fields by
1920  *		a single tab.
1921  *	-o	List of fields to display.  Defaults to all fields, or
1922  *		"name,size,used,available,capacity,health,root"
1923  *
1924  * List all pools in the system, whether or not they're healthy.  Output space
1925  * statistics for each one, as well as health status summary.
1926  */
1927 int
1928 zpool_do_list(int argc, char **argv)
1929 {
1930 	int c;
1931 	int ret;
1932 	list_cbdata_t cb = { 0 };
1933 	static char default_fields[] =
1934 	    "name,size,used,available,capacity,health,root";
1935 	char *fields = default_fields;
1936 	char *value;
1937 
1938 	/* check options */
1939 	while ((c = getopt(argc, argv, ":Ho:")) != -1) {
1940 		switch (c) {
1941 		case 'H':
1942 			cb.cb_scripted = B_TRUE;
1943 			break;
1944 		case 'o':
1945 			fields = optarg;
1946 			break;
1947 		case ':':
1948 			(void) fprintf(stderr, gettext("missing argument for "
1949 			    "'%c' option\n"), optopt);
1950 			usage(B_FALSE);
1951 			break;
1952 		case '?':
1953 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1954 			    optopt);
1955 			usage(B_FALSE);
1956 		}
1957 	}
1958 
1959 	argc -= optind;
1960 	argv += optind;
1961 
1962 	while (*fields != '\0') {
1963 		if (cb.cb_fieldcount == MAX_FIELDS) {
1964 			(void) fprintf(stderr, gettext("too many "
1965 			    "properties given to -o option\n"));
1966 			usage(B_FALSE);
1967 		}
1968 
1969 		if ((cb.cb_fields[cb.cb_fieldcount] = getsubopt(&fields,
1970 		    column_subopts, &value)) == -1) {
1971 			(void) fprintf(stderr, gettext("invalid property "
1972 			    "'%s'\n"), value);
1973 			usage(B_FALSE);
1974 		}
1975 
1976 		cb.cb_fieldcount++;
1977 	}
1978 
1979 
1980 	cb.cb_first = B_TRUE;
1981 
1982 	ret = for_each_pool(argc, argv, B_TRUE, list_callback, &cb);
1983 
1984 	if (argc == 0 && cb.cb_first) {
1985 		(void) printf(gettext("no pools available\n"));
1986 		return (0);
1987 	}
1988 
1989 	return (ret);
1990 }
1991 
1992 static nvlist_t *
1993 zpool_get_vdev_by_name(nvlist_t *nv, char *name)
1994 {
1995 	nvlist_t **child;
1996 	uint_t c, children;
1997 	nvlist_t *match;
1998 	char *path;
1999 
2000 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2001 	    &child, &children) != 0) {
2002 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2003 		if (strncmp(name, "/dev/dsk/", 9) == 0)
2004 			name += 9;
2005 		if (strncmp(path, "/dev/dsk/", 9) == 0)
2006 			path += 9;
2007 		if (strcmp(name, path) == 0)
2008 			return (nv);
2009 		return (NULL);
2010 	}
2011 
2012 	for (c = 0; c < children; c++)
2013 		if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
2014 			return (match);
2015 
2016 	return (NULL);
2017 }
2018 
2019 static int
2020 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
2021 {
2022 	boolean_t force = B_FALSE;
2023 	int c;
2024 	nvlist_t *nvroot;
2025 	char *poolname, *old_disk, *new_disk;
2026 	zpool_handle_t *zhp;
2027 	nvlist_t *config;
2028 	int ret;
2029 	int log_argc;
2030 	char **log_argv;
2031 
2032 	/* check options */
2033 	while ((c = getopt(argc, argv, "f")) != -1) {
2034 		switch (c) {
2035 		case 'f':
2036 			force = B_TRUE;
2037 			break;
2038 		case '?':
2039 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2040 			    optopt);
2041 			usage(B_FALSE);
2042 		}
2043 	}
2044 
2045 	log_argc = argc;
2046 	log_argv = argv;
2047 	argc -= optind;
2048 	argv += optind;
2049 
2050 	/* get pool name and check number of arguments */
2051 	if (argc < 1) {
2052 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
2053 		usage(B_FALSE);
2054 	}
2055 
2056 	poolname = argv[0];
2057 
2058 	if (argc < 2) {
2059 		(void) fprintf(stderr,
2060 		    gettext("missing <device> specification\n"));
2061 		usage(B_FALSE);
2062 	}
2063 
2064 	old_disk = argv[1];
2065 
2066 	if (argc < 3) {
2067 		if (!replacing) {
2068 			(void) fprintf(stderr,
2069 			    gettext("missing <new_device> specification\n"));
2070 			usage(B_FALSE);
2071 		}
2072 		new_disk = old_disk;
2073 		argc -= 1;
2074 		argv += 1;
2075 	} else {
2076 		new_disk = argv[2];
2077 		argc -= 2;
2078 		argv += 2;
2079 	}
2080 
2081 	if (argc > 1) {
2082 		(void) fprintf(stderr, gettext("too many arguments\n"));
2083 		usage(B_FALSE);
2084 	}
2085 
2086 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2087 		return (1);
2088 
2089 	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2090 		(void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
2091 		    poolname);
2092 		zpool_close(zhp);
2093 		return (1);
2094 	}
2095 
2096 	nvroot = make_root_vdev(config, force, B_FALSE, replacing, argc, argv);
2097 	if (nvroot == NULL) {
2098 		zpool_close(zhp);
2099 		return (1);
2100 	}
2101 
2102 	ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
2103 
2104 	if (!ret) {
2105 		zpool_log_history(g_zfs, log_argc, log_argv, poolname, B_TRUE,
2106 		    B_FALSE);
2107 	}
2108 
2109 	nvlist_free(nvroot);
2110 	zpool_close(zhp);
2111 
2112 	return (ret);
2113 }
2114 
2115 /*
2116  * zpool replace [-f] <pool> <device> <new_device>
2117  *
2118  *	-f	Force attach, even if <new_device> appears to be in use.
2119  *
2120  * Replace <device> with <new_device>.
2121  */
2122 /* ARGSUSED */
2123 int
2124 zpool_do_replace(int argc, char **argv)
2125 {
2126 	return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
2127 }
2128 
2129 /*
2130  * zpool attach [-f] <pool> <device> <new_device>
2131  *
2132  *	-f	Force attach, even if <new_device> appears to be in use.
2133  *
2134  * Attach <new_device> to the mirror containing <device>.  If <device> is not
2135  * part of a mirror, then <device> will be transformed into a mirror of
2136  * <device> and <new_device>.  In either case, <new_device> will begin life
2137  * with a DTL of [0, now], and will immediately begin to resilver itself.
2138  */
2139 int
2140 zpool_do_attach(int argc, char **argv)
2141 {
2142 	return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
2143 }
2144 
2145 /*
2146  * zpool detach [-f] <pool> <device>
2147  *
2148  *	-f	Force detach of <device>, even if DTLs argue against it
2149  *		(not supported yet)
2150  *
2151  * Detach a device from a mirror.  The operation will be refused if <device>
2152  * is the last device in the mirror, or if the DTLs indicate that this device
2153  * has the only valid copy of some data.
2154  */
2155 /* ARGSUSED */
2156 int
2157 zpool_do_detach(int argc, char **argv)
2158 {
2159 	int c;
2160 	char *poolname, *path;
2161 	zpool_handle_t *zhp;
2162 	int ret;
2163 
2164 	/* check options */
2165 	while ((c = getopt(argc, argv, "f")) != -1) {
2166 		switch (c) {
2167 		case 'f':
2168 		case '?':
2169 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2170 			    optopt);
2171 			usage(B_FALSE);
2172 		}
2173 	}
2174 
2175 	argc -= optind;
2176 	argv += optind;
2177 
2178 	/* get pool name and check number of arguments */
2179 	if (argc < 1) {
2180 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
2181 		usage(B_FALSE);
2182 	}
2183 
2184 	if (argc < 2) {
2185 		(void) fprintf(stderr,
2186 		    gettext("missing <device> specification\n"));
2187 		usage(B_FALSE);
2188 	}
2189 
2190 	poolname = argv[0];
2191 	path = argv[1];
2192 
2193 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2194 		return (1);
2195 
2196 	ret = zpool_vdev_detach(zhp, path);
2197 
2198 	if (!ret) {
2199 		zpool_log_history(g_zfs, argc + optind, argv - optind, poolname,
2200 		    B_TRUE, B_FALSE);
2201 	}
2202 	zpool_close(zhp);
2203 
2204 	return (ret);
2205 }
2206 
2207 /*
2208  * zpool online <pool> <device> ...
2209  */
2210 /* ARGSUSED */
2211 int
2212 zpool_do_online(int argc, char **argv)
2213 {
2214 	int c, i;
2215 	char *poolname;
2216 	zpool_handle_t *zhp;
2217 	int ret = 0;
2218 
2219 	/* check options */
2220 	while ((c = getopt(argc, argv, "t")) != -1) {
2221 		switch (c) {
2222 		case 't':
2223 		case '?':
2224 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2225 			    optopt);
2226 			usage(B_FALSE);
2227 		}
2228 	}
2229 
2230 	argc -= optind;
2231 	argv += optind;
2232 
2233 	/* get pool name and check number of arguments */
2234 	if (argc < 1) {
2235 		(void) fprintf(stderr, gettext("missing pool name\n"));
2236 		usage(B_FALSE);
2237 	}
2238 	if (argc < 2) {
2239 		(void) fprintf(stderr, gettext("missing device name\n"));
2240 		usage(B_FALSE);
2241 	}
2242 
2243 	poolname = argv[0];
2244 
2245 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2246 		return (1);
2247 
2248 	for (i = 1; i < argc; i++)
2249 		if (zpool_vdev_online(zhp, argv[i]) == 0)
2250 			(void) printf(gettext("Bringing device %s online\n"),
2251 			    argv[i]);
2252 		else
2253 			ret = 1;
2254 
2255 	if (!ret) {
2256 		zpool_log_history(g_zfs, argc + optind, argv - optind, poolname,
2257 		    B_TRUE, B_FALSE);
2258 	}
2259 	zpool_close(zhp);
2260 
2261 	return (ret);
2262 }
2263 
2264 /*
2265  * zpool offline [-ft] <pool> <device> ...
2266  *
2267  *	-f	Force the device into the offline state, even if doing
2268  *		so would appear to compromise pool availability.
2269  *		(not supported yet)
2270  *
2271  *	-t	Only take the device off-line temporarily.  The offline
2272  *		state will not be persistent across reboots.
2273  */
2274 /* ARGSUSED */
2275 int
2276 zpool_do_offline(int argc, char **argv)
2277 {
2278 	int c, i;
2279 	char *poolname;
2280 	zpool_handle_t *zhp;
2281 	int ret = 0;
2282 	boolean_t istmp = B_FALSE;
2283 
2284 	/* check options */
2285 	while ((c = getopt(argc, argv, "ft")) != -1) {
2286 		switch (c) {
2287 		case 't':
2288 			istmp = B_TRUE;
2289 			break;
2290 		case 'f':
2291 		case '?':
2292 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2293 			    optopt);
2294 			usage(B_FALSE);
2295 		}
2296 	}
2297 
2298 	argc -= optind;
2299 	argv += optind;
2300 
2301 	/* get pool name and check number of arguments */
2302 	if (argc < 1) {
2303 		(void) fprintf(stderr, gettext("missing pool name\n"));
2304 		usage(B_FALSE);
2305 	}
2306 	if (argc < 2) {
2307 		(void) fprintf(stderr, gettext("missing device name\n"));
2308 		usage(B_FALSE);
2309 	}
2310 
2311 	poolname = argv[0];
2312 
2313 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2314 		return (1);
2315 
2316 	for (i = 1; i < argc; i++)
2317 		if (zpool_vdev_offline(zhp, argv[i], istmp) == 0)
2318 			(void) printf(gettext("Bringing device %s offline\n"),
2319 			    argv[i]);
2320 		else
2321 			ret = 1;
2322 
2323 	if (!ret) {
2324 		zpool_log_history(g_zfs, argc + optind, argv - optind, poolname,
2325 		    B_TRUE, B_FALSE);
2326 	}
2327 	zpool_close(zhp);
2328 
2329 	return (ret);
2330 }
2331 
2332 /*
2333  * zpool clear <pool> [device]
2334  *
2335  * Clear all errors associated with a pool or a particular device.
2336  */
2337 int
2338 zpool_do_clear(int argc, char **argv)
2339 {
2340 	int ret = 0;
2341 	zpool_handle_t *zhp;
2342 	char *pool, *device;
2343 
2344 	if (argc < 2) {
2345 		(void) fprintf(stderr, gettext("missing pool name\n"));
2346 		usage(B_FALSE);
2347 	}
2348 
2349 	if (argc > 3) {
2350 		(void) fprintf(stderr, gettext("too many arguments\n"));
2351 		usage(B_FALSE);
2352 	}
2353 
2354 	pool = argv[1];
2355 	device = argc == 3 ? argv[2] : NULL;
2356 
2357 	if ((zhp = zpool_open(g_zfs, pool)) == NULL)
2358 		return (1);
2359 
2360 	if (zpool_clear(zhp, device) != 0)
2361 		ret = 1;
2362 
2363 	if (!ret)
2364 		zpool_log_history(g_zfs, argc, argv, pool, B_TRUE, B_FALSE);
2365 	zpool_close(zhp);
2366 
2367 	return (ret);
2368 }
2369 
2370 typedef struct scrub_cbdata {
2371 	int	cb_type;
2372 	int	cb_argc;
2373 	char	**cb_argv;
2374 } scrub_cbdata_t;
2375 
2376 int
2377 scrub_callback(zpool_handle_t *zhp, void *data)
2378 {
2379 	scrub_cbdata_t *cb = data;
2380 	int err;
2381 
2382 	/*
2383 	 * Ignore faulted pools.
2384 	 */
2385 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
2386 		(void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
2387 		    "currently unavailable\n"), zpool_get_name(zhp));
2388 		return (1);
2389 	}
2390 
2391 	err = zpool_scrub(zhp, cb->cb_type);
2392 
2393 	if (!err) {
2394 		zpool_log_history(g_zfs, cb->cb_argc, cb->cb_argv,
2395 		    zpool_get_name(zhp), B_TRUE, B_FALSE);
2396 	}
2397 
2398 	return (err != 0);
2399 }
2400 
2401 /*
2402  * zpool scrub [-s] <pool> ...
2403  *
2404  *	-s	Stop.  Stops any in-progress scrub.
2405  */
2406 int
2407 zpool_do_scrub(int argc, char **argv)
2408 {
2409 	int c;
2410 	scrub_cbdata_t cb;
2411 
2412 	cb.cb_type = POOL_SCRUB_EVERYTHING;
2413 
2414 	/* check options */
2415 	while ((c = getopt(argc, argv, "s")) != -1) {
2416 		switch (c) {
2417 		case 's':
2418 			cb.cb_type = POOL_SCRUB_NONE;
2419 			break;
2420 		case '?':
2421 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2422 			    optopt);
2423 			usage(B_FALSE);
2424 		}
2425 	}
2426 
2427 	cb.cb_argc = argc;
2428 	cb.cb_argv = argv;
2429 	argc -= optind;
2430 	argv += optind;
2431 
2432 	if (argc < 1) {
2433 		(void) fprintf(stderr, gettext("missing pool name argument\n"));
2434 		usage(B_FALSE);
2435 	}
2436 
2437 	return (for_each_pool(argc, argv, B_TRUE, scrub_callback, &cb));
2438 }
2439 
2440 typedef struct status_cbdata {
2441 	int		cb_count;
2442 	boolean_t	cb_allpools;
2443 	boolean_t	cb_verbose;
2444 	boolean_t	cb_explain;
2445 	boolean_t	cb_first;
2446 } status_cbdata_t;
2447 
2448 /*
2449  * Print out detailed scrub status.
2450  */
2451 void
2452 print_scrub_status(nvlist_t *nvroot)
2453 {
2454 	vdev_stat_t *vs;
2455 	uint_t vsc;
2456 	time_t start, end, now;
2457 	double fraction_done;
2458 	uint64_t examined, total, minutes_left;
2459 	char *scrub_type;
2460 
2461 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
2462 	    (uint64_t **)&vs, &vsc) == 0);
2463 
2464 	/*
2465 	 * If there's never been a scrub, there's not much to say.
2466 	 */
2467 	if (vs->vs_scrub_end == 0 && vs->vs_scrub_type == POOL_SCRUB_NONE) {
2468 		(void) printf(gettext("none requested\n"));
2469 		return;
2470 	}
2471 
2472 	scrub_type = (vs->vs_scrub_type == POOL_SCRUB_RESILVER) ?
2473 	    "resilver" : "scrub";
2474 
2475 	start = vs->vs_scrub_start;
2476 	end = vs->vs_scrub_end;
2477 	now = time(NULL);
2478 	examined = vs->vs_scrub_examined;
2479 	total = vs->vs_alloc;
2480 
2481 	if (end != 0) {
2482 		(void) printf(gettext("%s %s with %llu errors on %s"),
2483 		    scrub_type, vs->vs_scrub_complete ? "completed" : "stopped",
2484 		    (u_longlong_t)vs->vs_scrub_errors, ctime(&end));
2485 		return;
2486 	}
2487 
2488 	if (examined == 0)
2489 		examined = 1;
2490 	if (examined > total)
2491 		total = examined;
2492 
2493 	fraction_done = (double)examined / total;
2494 	minutes_left = (uint64_t)((now - start) *
2495 	    (1 - fraction_done) / fraction_done / 60);
2496 
2497 	(void) printf(gettext("%s in progress, %.2f%% done, %lluh%um to go\n"),
2498 	    scrub_type, 100 * fraction_done,
2499 	    (u_longlong_t)(minutes_left / 60), (uint_t)(minutes_left % 60));
2500 }
2501 
2502 typedef struct spare_cbdata {
2503 	uint64_t	cb_guid;
2504 	zpool_handle_t	*cb_zhp;
2505 } spare_cbdata_t;
2506 
2507 static boolean_t
2508 find_vdev(nvlist_t *nv, uint64_t search)
2509 {
2510 	uint64_t guid;
2511 	nvlist_t **child;
2512 	uint_t c, children;
2513 
2514 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
2515 	    search == guid)
2516 		return (B_TRUE);
2517 
2518 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2519 	    &child, &children) == 0) {
2520 		for (c = 0; c < children; c++)
2521 			if (find_vdev(child[c], search))
2522 				return (B_TRUE);
2523 	}
2524 
2525 	return (B_FALSE);
2526 }
2527 
2528 static int
2529 find_spare(zpool_handle_t *zhp, void *data)
2530 {
2531 	spare_cbdata_t *cbp = data;
2532 	nvlist_t *config, *nvroot;
2533 
2534 	config = zpool_get_config(zhp, NULL);
2535 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2536 	    &nvroot) == 0);
2537 
2538 	if (find_vdev(nvroot, cbp->cb_guid)) {
2539 		cbp->cb_zhp = zhp;
2540 		return (1);
2541 	}
2542 
2543 	zpool_close(zhp);
2544 	return (0);
2545 }
2546 
2547 /*
2548  * Print out configuration state as requested by status_callback.
2549  */
2550 void
2551 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
2552     int namewidth, int depth, boolean_t isspare)
2553 {
2554 	nvlist_t **child;
2555 	uint_t c, children;
2556 	vdev_stat_t *vs;
2557 	char rbuf[6], wbuf[6], cbuf[6], repaired[7];
2558 	char *vname;
2559 	uint64_t notpresent;
2560 	spare_cbdata_t cb;
2561 	const char *state;
2562 
2563 	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
2564 	    (uint64_t **)&vs, &c) == 0);
2565 
2566 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2567 	    &child, &children) != 0)
2568 		children = 0;
2569 
2570 	state = state_to_name(vs);
2571 	if (isspare) {
2572 		/*
2573 		 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
2574 		 * online drives.
2575 		 */
2576 		if (vs->vs_aux == VDEV_AUX_SPARED)
2577 			state = "INUSE";
2578 		else if (vs->vs_state == VDEV_STATE_HEALTHY)
2579 			state = "AVAIL";
2580 	}
2581 
2582 	(void) printf("\t%*s%-*s  %-8s", depth, "", namewidth - depth,
2583 	    name, state);
2584 
2585 	if (!isspare) {
2586 		zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
2587 		zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
2588 		zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
2589 		(void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
2590 	}
2591 
2592 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
2593 	    &notpresent) == 0) {
2594 		char *path;
2595 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2596 		(void) printf("  was %s", path);
2597 	} else if (vs->vs_aux != 0) {
2598 		(void) printf("  ");
2599 
2600 		switch (vs->vs_aux) {
2601 		case VDEV_AUX_OPEN_FAILED:
2602 			(void) printf(gettext("cannot open"));
2603 			break;
2604 
2605 		case VDEV_AUX_BAD_GUID_SUM:
2606 			(void) printf(gettext("missing device"));
2607 			break;
2608 
2609 		case VDEV_AUX_NO_REPLICAS:
2610 			(void) printf(gettext("insufficient replicas"));
2611 			break;
2612 
2613 		case VDEV_AUX_VERSION_NEWER:
2614 			(void) printf(gettext("newer version"));
2615 			break;
2616 
2617 		case VDEV_AUX_SPARED:
2618 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2619 			    &cb.cb_guid) == 0);
2620 			if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
2621 				if (strcmp(zpool_get_name(cb.cb_zhp),
2622 				    zpool_get_name(zhp)) == 0)
2623 					(void) printf(gettext("currently in "
2624 					    "use"));
2625 				else
2626 					(void) printf(gettext("in use by "
2627 					    "pool '%s'"),
2628 					    zpool_get_name(cb.cb_zhp));
2629 				zpool_close(cb.cb_zhp);
2630 			} else {
2631 				(void) printf(gettext("currently in use"));
2632 			}
2633 			break;
2634 
2635 		default:
2636 			(void) printf(gettext("corrupted data"));
2637 			break;
2638 		}
2639 	} else if (vs->vs_scrub_repaired != 0 && children == 0) {
2640 		/*
2641 		 * Report bytes resilvered/repaired on leaf devices.
2642 		 */
2643 		zfs_nicenum(vs->vs_scrub_repaired, repaired, sizeof (repaired));
2644 		(void) printf(gettext("  %s %s"), repaired,
2645 		    (vs->vs_scrub_type == POOL_SCRUB_RESILVER) ?
2646 		    "resilvered" : "repaired");
2647 	}
2648 
2649 	(void) printf("\n");
2650 
2651 	for (c = 0; c < children; c++) {
2652 		vname = zpool_vdev_name(g_zfs, zhp, child[c]);
2653 		print_status_config(zhp, vname, child[c],
2654 		    namewidth, depth + 2, isspare);
2655 		free(vname);
2656 	}
2657 }
2658 
2659 static void
2660 print_error_log(zpool_handle_t *zhp)
2661 {
2662 	nvlist_t **log;
2663 	size_t nelem;
2664 	size_t maxdsname = sizeof ("DATASET") - 1;
2665 	size_t maxobjname = sizeof ("OBJECT") - 1;
2666 	int i;
2667 	nvlist_t *nv;
2668 	size_t len;
2669 	char *dsname, *objname, *range;
2670 
2671 	if (zpool_get_errlog(zhp, &log, &nelem) != 0) {
2672 		(void) printf("errors: List of errors unavailable "
2673 		    "(insufficient privileges)\n");
2674 		return;
2675 	}
2676 
2677 	for (i = 0; i < nelem; i++) {
2678 		nv = log[i];
2679 
2680 		verify(nvlist_lookup_string(nv, ZPOOL_ERR_DATASET,
2681 		    &dsname) == 0);
2682 		len = strlen(dsname);
2683 		if (len > maxdsname)
2684 			maxdsname = len;
2685 
2686 		verify(nvlist_lookup_string(nv, ZPOOL_ERR_OBJECT,
2687 		    &objname) == 0);
2688 		len = strlen(objname);
2689 		if (len > maxobjname)
2690 			maxobjname = len;
2691 	}
2692 
2693 	(void) printf("errors: The following persistent errors have been "
2694 	    "detected:\n\n");
2695 	(void) printf("%8s  %-*s  %-*s  %s\n", "", (int)maxdsname, "DATASET",
2696 	    (int)maxobjname, "OBJECT", "RANGE");
2697 
2698 	for (i = 0; i < nelem; i++) {
2699 		nv = log[i];
2700 
2701 		verify(nvlist_lookup_string(nv, ZPOOL_ERR_DATASET,
2702 		    &dsname) == 0);
2703 		verify(nvlist_lookup_string(nv, ZPOOL_ERR_OBJECT,
2704 		    &objname) == 0);
2705 		verify(nvlist_lookup_string(nv, ZPOOL_ERR_RANGE,
2706 		    &range) == 0);
2707 
2708 		(void) printf("%8s  %-*s  %-*s  %s\n", "", (int)maxdsname,
2709 		    dsname, (int)maxobjname, objname, range);
2710 	}
2711 }
2712 
2713 static void
2714 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
2715     int namewidth)
2716 {
2717 	uint_t i;
2718 	char *name;
2719 
2720 	if (nspares == 0)
2721 		return;
2722 
2723 	(void) printf(gettext("\tspares\n"));
2724 
2725 	for (i = 0; i < nspares; i++) {
2726 		name = zpool_vdev_name(g_zfs, zhp, spares[i]);
2727 		print_status_config(zhp, name, spares[i],
2728 		    namewidth, 2, B_TRUE);
2729 		free(name);
2730 	}
2731 }
2732 
2733 /*
2734  * Display a summary of pool status.  Displays a summary such as:
2735  *
2736  *        pool: tank
2737  *	status: DEGRADED
2738  *	reason: One or more devices ...
2739  *         see: http://www.sun.com/msg/ZFS-xxxx-01
2740  *	config:
2741  *		mirror		DEGRADED
2742  *                c1t0d0	OK
2743  *                c2t0d0	UNAVAIL
2744  *
2745  * When given the '-v' option, we print out the complete config.  If the '-e'
2746  * option is specified, then we print out error rate information as well.
2747  */
2748 int
2749 status_callback(zpool_handle_t *zhp, void *data)
2750 {
2751 	status_cbdata_t *cbp = data;
2752 	nvlist_t *config, *nvroot;
2753 	char *msgid;
2754 	int reason;
2755 	char *health;
2756 
2757 	config = zpool_get_config(zhp, NULL);
2758 	reason = zpool_get_status(zhp, &msgid);
2759 
2760 	cbp->cb_count++;
2761 
2762 	/*
2763 	 * If we were given 'zpool status -x', only report those pools with
2764 	 * problems.
2765 	 */
2766 	if (reason == ZPOOL_STATUS_OK && cbp->cb_explain) {
2767 		if (!cbp->cb_allpools) {
2768 			(void) printf(gettext("pool '%s' is healthy\n"),
2769 			    zpool_get_name(zhp));
2770 			if (cbp->cb_first)
2771 				cbp->cb_first = B_FALSE;
2772 		}
2773 		return (0);
2774 	}
2775 
2776 	if (cbp->cb_first)
2777 		cbp->cb_first = B_FALSE;
2778 	else
2779 		(void) printf("\n");
2780 
2781 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_HEALTH,
2782 	    &health) == 0);
2783 
2784 	(void) printf(gettext("  pool: %s\n"), zpool_get_name(zhp));
2785 	(void) printf(gettext(" state: %s\n"), health);
2786 
2787 	switch (reason) {
2788 	case ZPOOL_STATUS_MISSING_DEV_R:
2789 		(void) printf(gettext("status: One or more devices could not "
2790 		    "be opened.  Sufficient replicas exist for\n\tthe pool to "
2791 		    "continue functioning in a degraded state.\n"));
2792 		(void) printf(gettext("action: Attach the missing device and "
2793 		    "online it using 'zpool online'.\n"));
2794 		break;
2795 
2796 	case ZPOOL_STATUS_MISSING_DEV_NR:
2797 		(void) printf(gettext("status: One or more devices could not "
2798 		    "be opened.  There are insufficient\n\treplicas for the "
2799 		    "pool to continue functioning.\n"));
2800 		(void) printf(gettext("action: Attach the missing device and "
2801 		    "online it using 'zpool online'.\n"));
2802 		break;
2803 
2804 	case ZPOOL_STATUS_CORRUPT_LABEL_R:
2805 		(void) printf(gettext("status: One or more devices could not "
2806 		    "be used because the label is missing or\n\tinvalid.  "
2807 		    "Sufficient replicas exist for the pool to continue\n\t"
2808 		    "functioning in a degraded state.\n"));
2809 		(void) printf(gettext("action: Replace the device using "
2810 		    "'zpool replace'.\n"));
2811 		break;
2812 
2813 	case ZPOOL_STATUS_CORRUPT_LABEL_NR:
2814 		(void) printf(gettext("status: One or more devices could not "
2815 		    "be used because the the label is missing \n\tor invalid.  "
2816 		    "There are insufficient replicas for the pool to "
2817 		    "continue\n\tfunctioning.\n"));
2818 		(void) printf(gettext("action: Destroy and re-create the pool "
2819 		    "from a backup source.\n"));
2820 		break;
2821 
2822 	case ZPOOL_STATUS_FAILING_DEV:
2823 		(void) printf(gettext("status: One or more devices has "
2824 		    "experienced an unrecoverable error.  An\n\tattempt was "
2825 		    "made to correct the error.  Applications are "
2826 		    "unaffected.\n"));
2827 		(void) printf(gettext("action: Determine if the device needs "
2828 		    "to be replaced, and clear the errors\n\tusing "
2829 		    "'zpool clear' or replace the device with 'zpool "
2830 		    "replace'.\n"));
2831 		break;
2832 
2833 	case ZPOOL_STATUS_OFFLINE_DEV:
2834 		(void) printf(gettext("status: One or more devices has "
2835 		    "been taken offline by the administrator.\n\tSufficient "
2836 		    "replicas exist for the pool to continue functioning in "
2837 		    "a\n\tdegraded state.\n"));
2838 		(void) printf(gettext("action: Online the device using "
2839 		    "'zpool online' or replace the device with\n\t'zpool "
2840 		    "replace'.\n"));
2841 		break;
2842 
2843 	case ZPOOL_STATUS_RESILVERING:
2844 		(void) printf(gettext("status: One or more devices is "
2845 		    "currently being resilvered.  The pool will\n\tcontinue "
2846 		    "to function, possibly in a degraded state.\n"));
2847 		(void) printf(gettext("action: Wait for the resilver to "
2848 		    "complete.\n"));
2849 		break;
2850 
2851 	case ZPOOL_STATUS_CORRUPT_DATA:
2852 		(void) printf(gettext("status: One or more devices has "
2853 		    "experienced an error resulting in data\n\tcorruption.  "
2854 		    "Applications may be affected.\n"));
2855 		(void) printf(gettext("action: Restore the file in question "
2856 		    "if possible.  Otherwise restore the\n\tentire pool from "
2857 		    "backup.\n"));
2858 		break;
2859 
2860 	case ZPOOL_STATUS_CORRUPT_POOL:
2861 		(void) printf(gettext("status: The pool metadata is corrupted "
2862 		    "and the pool cannot be opened.\n"));
2863 		(void) printf(gettext("action: Destroy and re-create the pool "
2864 		    "from a backup source.\n"));
2865 		break;
2866 
2867 	case ZPOOL_STATUS_VERSION_OLDER:
2868 		(void) printf(gettext("status: The pool is formatted using an "
2869 		    "older on-disk format.  The pool can\n\tstill be used, but "
2870 		    "some features are unavailable.\n"));
2871 		(void) printf(gettext("action: Upgrade the pool using 'zpool "
2872 		    "upgrade'.  Once this is done, the\n\tpool will no longer "
2873 		    "be accessible on older software versions.\n"));
2874 		break;
2875 
2876 	case ZPOOL_STATUS_VERSION_NEWER:
2877 		(void) printf(gettext("status: The pool has been upgraded to a "
2878 		    "newer, incompatible on-disk version.\n\tThe pool cannot "
2879 		    "be accessed on this system.\n"));
2880 		(void) printf(gettext("action: Access the pool from a system "
2881 		    "running more recent software, or\n\trestore the pool from "
2882 		    "backup.\n"));
2883 		break;
2884 
2885 	default:
2886 		/*
2887 		 * The remaining errors can't actually be generated, yet.
2888 		 */
2889 		assert(reason == ZPOOL_STATUS_OK);
2890 	}
2891 
2892 	if (msgid != NULL)
2893 		(void) printf(gettext("   see: http://www.sun.com/msg/%s\n"),
2894 		    msgid);
2895 
2896 	if (config != NULL) {
2897 		int namewidth;
2898 		uint64_t nerr;
2899 		size_t realerr;
2900 		nvlist_t **spares;
2901 		uint_t nspares;
2902 
2903 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2904 		    &nvroot) == 0);
2905 
2906 		(void) printf(gettext(" scrub: "));
2907 		print_scrub_status(nvroot);
2908 
2909 		namewidth = max_width(zhp, nvroot, 0, 0);
2910 		if (namewidth < 10)
2911 			namewidth = 10;
2912 
2913 		(void) printf(gettext("config:\n\n"));
2914 		(void) printf(gettext("\t%-*s  %-8s %5s %5s %5s\n"), namewidth,
2915 		    "NAME", "STATE", "READ", "WRITE", "CKSUM");
2916 		print_status_config(zhp, zpool_get_name(zhp), nvroot,
2917 		    namewidth, 0, B_FALSE);
2918 
2919 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
2920 		    &spares, &nspares) == 0)
2921 			print_spares(zhp, spares, nspares, namewidth);
2922 
2923 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
2924 		    &nerr) == 0) {
2925 			/*
2926 			 * If the approximate error count is small, get a
2927 			 * precise count by fetching the entire log and
2928 			 * uniquifying the results.
2929 			 */
2930 			if (nerr < 100 && !cbp->cb_verbose &&
2931 			    zpool_get_errlog(zhp, NULL, &realerr) == 0)
2932 				nerr = realerr;
2933 
2934 			(void) printf("\n");
2935 
2936 			if (nerr == 0)
2937 				(void) printf(gettext("errors: No known data "
2938 				    "errors\n"));
2939 			else if (!cbp->cb_verbose)
2940 				(void) printf(gettext("errors: %llu data "
2941 				    "errors, use '-v' for a list\n"),
2942 				    (u_longlong_t)nerr);
2943 			else
2944 				print_error_log(zhp);
2945 		}
2946 	} else {
2947 		(void) printf(gettext("config: The configuration cannot be "
2948 		    "determined.\n"));
2949 	}
2950 
2951 	return (0);
2952 }
2953 
2954 /*
2955  * zpool status [-vx] [pool] ...
2956  *
2957  *	-v	Display complete error logs
2958  *	-x	Display only pools with potential problems
2959  *
2960  * Describes the health status of all pools or some subset.
2961  */
2962 int
2963 zpool_do_status(int argc, char **argv)
2964 {
2965 	int c;
2966 	int ret;
2967 	status_cbdata_t cb = { 0 };
2968 
2969 	/* check options */
2970 	while ((c = getopt(argc, argv, "vx")) != -1) {
2971 		switch (c) {
2972 		case 'v':
2973 			cb.cb_verbose = B_TRUE;
2974 			break;
2975 		case 'x':
2976 			cb.cb_explain = B_TRUE;
2977 			break;
2978 		case '?':
2979 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2980 			    optopt);
2981 			usage(B_FALSE);
2982 		}
2983 	}
2984 
2985 	argc -= optind;
2986 	argv += optind;
2987 
2988 	cb.cb_first = B_TRUE;
2989 
2990 	if (argc == 0)
2991 		cb.cb_allpools = B_TRUE;
2992 
2993 	ret = for_each_pool(argc, argv, B_TRUE, status_callback, &cb);
2994 
2995 	if (argc == 0 && cb.cb_count == 0)
2996 		(void) printf(gettext("no pools available\n"));
2997 	else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
2998 		(void) printf(gettext("all pools are healthy\n"));
2999 
3000 	return (ret);
3001 }
3002 
3003 typedef struct upgrade_cbdata {
3004 	int	cb_all;
3005 	int	cb_first;
3006 	int	cb_newer;
3007 	int	cb_argc;
3008 	char	**cb_argv;
3009 } upgrade_cbdata_t;
3010 
3011 static int
3012 upgrade_cb(zpool_handle_t *zhp, void *arg)
3013 {
3014 	upgrade_cbdata_t *cbp = arg;
3015 	nvlist_t *config;
3016 	uint64_t version;
3017 	int ret = 0;
3018 
3019 	config = zpool_get_config(zhp, NULL);
3020 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
3021 	    &version) == 0);
3022 
3023 	if (!cbp->cb_newer && version < ZFS_VERSION) {
3024 		if (!cbp->cb_all) {
3025 			if (cbp->cb_first) {
3026 				(void) printf(gettext("The following pools are "
3027 				    "out of date, and can be upgraded.  After "
3028 				    "being\nupgraded, these pools will no "
3029 				    "longer be accessible by older software "
3030 				    "versions.\n\n"));
3031 				(void) printf(gettext("VER  POOL\n"));
3032 				(void) printf(gettext("---  ------------\n"));
3033 				cbp->cb_first = B_FALSE;
3034 			}
3035 
3036 			(void) printf("%2llu   %s\n", (u_longlong_t)version,
3037 			    zpool_get_name(zhp));
3038 		} else {
3039 			cbp->cb_first = B_FALSE;
3040 			ret = zpool_upgrade(zhp);
3041 			if (!ret) {
3042 				zpool_log_history(g_zfs, cbp->cb_argc,
3043 				    cbp->cb_argv, zpool_get_name(zhp), B_TRUE,
3044 				    B_FALSE);
3045 				(void) printf(gettext("Successfully upgraded "
3046 				    "'%s'\n"), zpool_get_name(zhp));
3047 			}
3048 		}
3049 	} else if (cbp->cb_newer && version > ZFS_VERSION) {
3050 		assert(!cbp->cb_all);
3051 
3052 		if (cbp->cb_first) {
3053 			(void) printf(gettext("The following pools are "
3054 			    "formatted using a newer software version and\n"
3055 			    "cannot be accessed on the current system.\n\n"));
3056 			(void) printf(gettext("VER  POOL\n"));
3057 			(void) printf(gettext("---  ------------\n"));
3058 			cbp->cb_first = B_FALSE;
3059 		}
3060 
3061 		(void) printf("%2llu   %s\n", (u_longlong_t)version,
3062 		    zpool_get_name(zhp));
3063 	}
3064 
3065 	zpool_close(zhp);
3066 	return (ret);
3067 }
3068 
3069 /* ARGSUSED */
3070 static int
3071 upgrade_one(zpool_handle_t *zhp, void *data)
3072 {
3073 	nvlist_t *config;
3074 	uint64_t version;
3075 	int ret;
3076 	upgrade_cbdata_t *cbp = data;
3077 
3078 	config = zpool_get_config(zhp, NULL);
3079 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
3080 	    &version) == 0);
3081 
3082 	if (version == ZFS_VERSION) {
3083 		(void) printf(gettext("Pool '%s' is already formatted "
3084 		    "using the current version.\n"), zpool_get_name(zhp));
3085 		return (0);
3086 	}
3087 
3088 	ret = zpool_upgrade(zhp);
3089 
3090 	if (!ret) {
3091 		zpool_log_history(g_zfs, cbp->cb_argc, cbp->cb_argv,
3092 		    zpool_get_name(zhp), B_TRUE, B_FALSE);
3093 		(void) printf(gettext("Successfully upgraded '%s' "
3094 		    "from version %llu to version %llu\n"), zpool_get_name(zhp),
3095 		    (u_longlong_t)version, (u_longlong_t)ZFS_VERSION);
3096 	}
3097 
3098 	return (ret != 0);
3099 }
3100 
3101 /*
3102  * zpool upgrade
3103  * zpool upgrade -v
3104  * zpool upgrade <-a | pool>
3105  *
3106  * With no arguments, display downrev'd ZFS pool available for upgrade.
3107  * Individual pools can be upgraded by specifying the pool, and '-a' will
3108  * upgrade all pools.
3109  */
3110 int
3111 zpool_do_upgrade(int argc, char **argv)
3112 {
3113 	int c;
3114 	upgrade_cbdata_t cb = { 0 };
3115 	int ret = 0;
3116 	boolean_t showversions = B_FALSE;
3117 
3118 	/* check options */
3119 	while ((c = getopt(argc, argv, "av")) != -1) {
3120 		switch (c) {
3121 		case 'a':
3122 			cb.cb_all = B_TRUE;
3123 			break;
3124 		case 'v':
3125 			showversions = B_TRUE;
3126 			break;
3127 		case '?':
3128 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3129 			    optopt);
3130 			usage(B_FALSE);
3131 		}
3132 	}
3133 
3134 	cb.cb_argc = argc;
3135 	cb.cb_argv = argv;
3136 	argc -= optind;
3137 	argv += optind;
3138 
3139 	if (showversions) {
3140 		if (cb.cb_all || argc != 0) {
3141 			(void) fprintf(stderr, gettext("-v option is "
3142 			    "incompatible with other arguments\n"));
3143 			usage(B_FALSE);
3144 		}
3145 	} else if (cb.cb_all) {
3146 		if (argc != 0) {
3147 			(void) fprintf(stderr, gettext("-a option is "
3148 			    "incompatible with other arguments\n"));
3149 			usage(B_FALSE);
3150 		}
3151 	}
3152 
3153 	(void) printf(gettext("This system is currently running ZFS version "
3154 	    "%llu.\n\n"), ZFS_VERSION);
3155 	cb.cb_first = B_TRUE;
3156 	if (showversions) {
3157 		(void) printf(gettext("The following versions are "
3158 		    "supported:\n\n"));
3159 		(void) printf(gettext("VER  DESCRIPTION\n"));
3160 		(void) printf("---  -----------------------------------------"
3161 		    "---------------\n");
3162 		(void) printf(gettext(" 1   Initial ZFS version\n"));
3163 		(void) printf(gettext(" 2   Ditto blocks "
3164 		    "(replicated metadata)\n"));
3165 		(void) printf(gettext(" 3   Hot spares and double parity "
3166 		    "RAID-Z\n"));
3167 		(void) printf(gettext("\nFor more information on a particular "
3168 		    "version, including supported releases, see:\n\n"));
3169 		(void) printf("http://www.opensolaris.org/os/community/zfs/"
3170 		    "version/N\n\n");
3171 		(void) printf(gettext("Where 'N' is the version number.\n"));
3172 	} else if (argc == 0) {
3173 		int notfound;
3174 
3175 		ret = zpool_iter(g_zfs, upgrade_cb, &cb);
3176 		notfound = cb.cb_first;
3177 
3178 		if (!cb.cb_all && ret == 0) {
3179 			if (!cb.cb_first)
3180 				(void) printf("\n");
3181 			cb.cb_first = B_TRUE;
3182 			cb.cb_newer = B_TRUE;
3183 			ret = zpool_iter(g_zfs, upgrade_cb, &cb);
3184 			if (!cb.cb_first) {
3185 				notfound = B_FALSE;
3186 				(void) printf("\n");
3187 			}
3188 		}
3189 
3190 		if (ret == 0) {
3191 			if (notfound)
3192 				(void) printf(gettext("All pools are formatted "
3193 				    "using this version.\n"));
3194 			else if (!cb.cb_all)
3195 				(void) printf(gettext("Use 'zpool upgrade -v' "
3196 				    "for a list of available versions and "
3197 				    "their associated\nfeatures.\n"));
3198 		}
3199 	} else {
3200 		ret = for_each_pool(argc, argv, B_FALSE, upgrade_one, &cb);
3201 	}
3202 
3203 	return (ret);
3204 }
3205 
3206 /*
3207  * Print out the command history for a specific pool.
3208  */
3209 static int
3210 get_history_one(zpool_handle_t *zhp, void *data)
3211 {
3212 	nvlist_t *nvhis;
3213 	nvlist_t **records;
3214 	uint_t numrecords;
3215 	char *cmdstr;
3216 	uint64_t dst_time;
3217 	time_t tsec;
3218 	struct tm t;
3219 	char tbuf[30];
3220 	int ret, i;
3221 
3222 	*(boolean_t *)data = B_FALSE;
3223 
3224 	(void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
3225 
3226 	if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
3227 		return (ret);
3228 
3229 	verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
3230 	    &records, &numrecords) == 0);
3231 	for (i = 0; i < numrecords; i++) {
3232 		if (nvlist_lookup_uint64(records[i], ZPOOL_HIST_TIME,
3233 		    &dst_time) == 0) {
3234 			verify(nvlist_lookup_string(records[i], ZPOOL_HIST_CMD,
3235 			    &cmdstr) == 0);
3236 			tsec = dst_time;
3237 			(void) localtime_r(&tsec, &t);
3238 			(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
3239 			(void) printf("%s %s\n", tbuf, cmdstr);
3240 		}
3241 	}
3242 	(void) printf("\n");
3243 	nvlist_free(nvhis);
3244 
3245 	return (ret);
3246 }
3247 
3248 /*
3249  * zpool history <pool>
3250  *
3251  * Displays the history of commands that modified pools.
3252  */
3253 int
3254 zpool_do_history(int argc, char **argv)
3255 {
3256 	boolean_t first = B_TRUE;
3257 	int ret;
3258 
3259 	argc -= optind;
3260 	argv += optind;
3261 
3262 	ret = for_each_pool(argc, argv, B_FALSE, get_history_one, &first);
3263 
3264 	if (argc == 0 && first == B_TRUE) {
3265 		(void) printf(gettext("no pools available\n"));
3266 		return (0);
3267 	}
3268 
3269 	return (ret);
3270 }
3271 
3272 int
3273 main(int argc, char **argv)
3274 {
3275 	int ret;
3276 	int i;
3277 	char *cmdname;
3278 
3279 	(void) setlocale(LC_ALL, "");
3280 	(void) textdomain(TEXT_DOMAIN);
3281 
3282 	if ((g_zfs = libzfs_init()) == NULL) {
3283 		(void) fprintf(stderr, gettext("internal error: failed to "
3284 		    "initialize ZFS library\n"));
3285 		return (1);
3286 	}
3287 
3288 	libzfs_print_on_error(g_zfs, B_TRUE);
3289 
3290 	opterr = 0;
3291 
3292 	/*
3293 	 * Make sure the user has specified some command.
3294 	 */
3295 	if (argc < 2) {
3296 		(void) fprintf(stderr, gettext("missing command\n"));
3297 		usage(B_FALSE);
3298 	}
3299 
3300 	cmdname = argv[1];
3301 
3302 	/*
3303 	 * Special case '-?'
3304 	 */
3305 	if (strcmp(cmdname, "-?") == 0)
3306 		usage(B_TRUE);
3307 
3308 	/*
3309 	 * Run the appropriate command.
3310 	 */
3311 	for (i = 0; i < NCOMMAND; i++) {
3312 		if (command_table[i].name == NULL)
3313 			continue;
3314 
3315 		if (strcmp(cmdname, command_table[i].name) == 0) {
3316 			current_command = &command_table[i];
3317 			ret = command_table[i].func(argc - 1, argv + 1);
3318 			break;
3319 		}
3320 	}
3321 
3322 	/*
3323 	 * 'freeze' is a vile debugging abomination, so we treat it as such.
3324 	 */
3325 	if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
3326 		char buf[16384];
3327 		int fd = open(ZFS_DEV, O_RDWR);
3328 		(void) strcpy((void *)buf, argv[2]);
3329 		return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
3330 	}
3331 
3332 	if (i == NCOMMAND) {
3333 		(void) fprintf(stderr, gettext("unrecognized "
3334 		    "command '%s'\n"), cmdname);
3335 		usage(B_FALSE);
3336 	}
3337 
3338 	libzfs_fini(g_zfs);
3339 
3340 	/*
3341 	 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
3342 	 * for the purposes of running ::findleaks.
3343 	 */
3344 	if (getenv("ZFS_ABORT") != NULL) {
3345 		(void) printf("dumping core by request\n");
3346 		abort();
3347 	}
3348 
3349 	return (ret);
3350 }
3351