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