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