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