zpool_main.c revision 224171
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 */
25
26#include <solaris.h>
27#include <assert.h>
28#include <ctype.h>
29#include <dirent.h>
30#include <errno.h>
31#include <fcntl.h>
32#include <libgen.h>
33#include <libintl.h>
34#include <libuutil.h>
35#include <locale.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <strings.h>
40#include <unistd.h>
41#include <priv.h>
42#include <pwd.h>
43#include <zone.h>
44#include <sys/time.h>
45#include <sys/fs/zfs.h>
46#include <sys/stat.h>
47
48#include <libzfs.h>
49
50#include "zpool_util.h"
51#include "zfs_comutil.h"
52
53#include "statcommon.h"
54
55static int zpool_do_create(int, char **);
56static int zpool_do_destroy(int, char **);
57
58static int zpool_do_add(int, char **);
59static int zpool_do_remove(int, char **);
60static int zpool_do_labelclear(int, char **);
61
62static int zpool_do_list(int, char **);
63static int zpool_do_iostat(int, char **);
64static int zpool_do_status(int, char **);
65
66static int zpool_do_online(int, char **);
67static int zpool_do_offline(int, char **);
68static int zpool_do_clear(int, char **);
69
70static int zpool_do_attach(int, char **);
71static int zpool_do_detach(int, char **);
72static int zpool_do_replace(int, char **);
73static int zpool_do_split(int, char **);
74
75static int zpool_do_scrub(int, char **);
76
77static int zpool_do_import(int, char **);
78static int zpool_do_export(int, char **);
79
80static int zpool_do_upgrade(int, char **);
81
82static int zpool_do_history(int, char **);
83
84static int zpool_do_get(int, char **);
85static int zpool_do_set(int, char **);
86
87/*
88 * These libumem hooks provide a reasonable set of defaults for the allocator's
89 * debugging facilities.
90 */
91
92#ifdef DEBUG
93const char *
94_umem_debug_init(void)
95{
96	return ("default,verbose"); /* $UMEM_DEBUG setting */
97}
98
99const char *
100_umem_logging_init(void)
101{
102	return ("fail,contents"); /* $UMEM_LOGGING setting */
103}
104#endif
105
106typedef enum {
107	HELP_ADD,
108	HELP_ATTACH,
109	HELP_CLEAR,
110	HELP_CREATE,
111	HELP_DESTROY,
112	HELP_DETACH,
113	HELP_EXPORT,
114	HELP_HISTORY,
115	HELP_IMPORT,
116	HELP_IOSTAT,
117	HELP_LABELCLEAR,
118	HELP_LIST,
119	HELP_OFFLINE,
120	HELP_ONLINE,
121	HELP_REPLACE,
122	HELP_REMOVE,
123	HELP_SCRUB,
124	HELP_STATUS,
125	HELP_UPGRADE,
126	HELP_GET,
127	HELP_SET,
128	HELP_SPLIT
129} zpool_help_t;
130
131
132typedef struct zpool_command {
133	const char	*name;
134	int		(*func)(int, char **);
135	zpool_help_t	usage;
136} zpool_command_t;
137
138/*
139 * Master command table.  Each ZFS command has a name, associated function, and
140 * usage message.  The usage messages need to be internationalized, so we have
141 * to have a function to return the usage message based on a command index.
142 *
143 * These commands are organized according to how they are displayed in the usage
144 * message.  An empty command (one with a NULL name) indicates an empty line in
145 * the generic usage message.
146 */
147static zpool_command_t command_table[] = {
148	{ "create",	zpool_do_create,	HELP_CREATE		},
149	{ "destroy",	zpool_do_destroy,	HELP_DESTROY		},
150	{ NULL },
151	{ "add",	zpool_do_add,		HELP_ADD		},
152	{ "remove",	zpool_do_remove,	HELP_REMOVE		},
153	{ NULL },
154	{ "labelclear",	zpool_do_labelclear,	HELP_LABELCLEAR		},
155	{ NULL },
156	{ "list",	zpool_do_list,		HELP_LIST		},
157	{ "iostat",	zpool_do_iostat,	HELP_IOSTAT		},
158	{ "status",	zpool_do_status,	HELP_STATUS		},
159	{ NULL },
160	{ "online",	zpool_do_online,	HELP_ONLINE		},
161	{ "offline",	zpool_do_offline,	HELP_OFFLINE		},
162	{ "clear",	zpool_do_clear,		HELP_CLEAR		},
163	{ NULL },
164	{ "attach",	zpool_do_attach,	HELP_ATTACH		},
165	{ "detach",	zpool_do_detach,	HELP_DETACH		},
166	{ "replace",	zpool_do_replace,	HELP_REPLACE		},
167	{ "split",	zpool_do_split,		HELP_SPLIT		},
168	{ NULL },
169	{ "scrub",	zpool_do_scrub,		HELP_SCRUB		},
170	{ NULL },
171	{ "import",	zpool_do_import,	HELP_IMPORT		},
172	{ "export",	zpool_do_export,	HELP_EXPORT		},
173	{ "upgrade",	zpool_do_upgrade,	HELP_UPGRADE		},
174	{ NULL },
175	{ "history",	zpool_do_history,	HELP_HISTORY		},
176	{ "get",	zpool_do_get,		HELP_GET		},
177	{ "set",	zpool_do_set,		HELP_SET		},
178};
179
180#define	NCOMMAND	(sizeof (command_table) / sizeof (command_table[0]))
181
182zpool_command_t *current_command;
183static char history_str[HIS_MAX_RECORD_LEN];
184
185static uint_t timestamp_fmt = NODATE;
186
187static const char *
188get_usage(zpool_help_t idx) {
189	switch (idx) {
190	case HELP_ADD:
191		return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
192	case HELP_ATTACH:
193		return (gettext("\tattach [-f] <pool> <device> "
194		    "<new-device>\n"));
195	case HELP_CLEAR:
196		return (gettext("\tclear [-nF] <pool> [device]\n"));
197	case HELP_CREATE:
198		return (gettext("\tcreate [-fn] [-o property=value] ... \n"
199		    "\t    [-O file-system-property=value] ... \n"
200		    "\t    [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
201	case HELP_DESTROY:
202		return (gettext("\tdestroy [-f] <pool>\n"));
203	case HELP_DETACH:
204		return (gettext("\tdetach <pool> <device>\n"));
205	case HELP_EXPORT:
206		return (gettext("\texport [-f] <pool> ...\n"));
207	case HELP_HISTORY:
208		return (gettext("\thistory [-il] [<pool>] ...\n"));
209	case HELP_IMPORT:
210		return (gettext("\timport [-d dir] [-D]\n"
211		    "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
212		    "\timport [-o mntopts] [-o property=value] ... \n"
213		    "\t    [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
214		    "[-R root] [-F [-n]] -a\n"
215		    "\timport [-o mntopts] [-o property=value] ... \n"
216		    "\t    [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
217		    "[-R root] [-F [-n]]\n"
218		    "\t    <pool | id> [newpool]\n"));
219	case HELP_IOSTAT:
220		return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
221		    "[count]]\n"));
222	case HELP_LABELCLEAR:
223		return (gettext("\tlabelclear [-f] <vdev>\n"));
224	case HELP_LIST:
225		return (gettext("\tlist [-H] [-o property[,...]] "
226		    "[-T d|u] [pool] ... [interval [count]]\n"));
227	case HELP_OFFLINE:
228		return (gettext("\toffline [-t] <pool> <device> ...\n"));
229	case HELP_ONLINE:
230		return (gettext("\tonline <pool> <device> ...\n"));
231	case HELP_REPLACE:
232		return (gettext("\treplace [-f] <pool> <device> "
233		    "[new-device]\n"));
234	case HELP_REMOVE:
235		return (gettext("\tremove <pool> <device> ...\n"));
236	case HELP_SCRUB:
237		return (gettext("\tscrub [-s] <pool> ...\n"));
238	case HELP_STATUS:
239		return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
240		    "[count]]\n"));
241	case HELP_UPGRADE:
242		return (gettext("\tupgrade\n"
243		    "\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;
2210
2211		if (nvlist_lookup_uint64(newchild[c],
2212		    ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
2213			continue;
2214
2215		vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2216		print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2217		    newchild[c], cb, depth + 2);
2218		free(vname);
2219	}
2220
2221	/*
2222	 * Include level 2 ARC devices in iostat output
2223	 */
2224	if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2225	    &newchild, &children) != 0)
2226		return;
2227
2228	if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2229	    &oldchild, &c) != 0)
2230		return;
2231
2232	if (children > 0) {
2233		(void) printf("%-*s      -      -      -      -      -      "
2234		    "-\n", cb->cb_namewidth, "cache");
2235		for (c = 0; c < children; c++) {
2236			vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2237			    B_FALSE);
2238			print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2239			    newchild[c], cb, depth + 2);
2240			free(vname);
2241		}
2242	}
2243}
2244
2245static int
2246refresh_iostat(zpool_handle_t *zhp, void *data)
2247{
2248	iostat_cbdata_t *cb = data;
2249	boolean_t missing;
2250
2251	/*
2252	 * If the pool has disappeared, remove it from the list and continue.
2253	 */
2254	if (zpool_refresh_stats(zhp, &missing) != 0)
2255		return (-1);
2256
2257	if (missing)
2258		pool_list_remove(cb->cb_list, zhp);
2259
2260	return (0);
2261}
2262
2263/*
2264 * Callback to print out the iostats for the given pool.
2265 */
2266int
2267print_iostat(zpool_handle_t *zhp, void *data)
2268{
2269	iostat_cbdata_t *cb = data;
2270	nvlist_t *oldconfig, *newconfig;
2271	nvlist_t *oldnvroot, *newnvroot;
2272
2273	newconfig = zpool_get_config(zhp, &oldconfig);
2274
2275	if (cb->cb_iteration == 1)
2276		oldconfig = NULL;
2277
2278	verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2279	    &newnvroot) == 0);
2280
2281	if (oldconfig == NULL)
2282		oldnvroot = NULL;
2283	else
2284		verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2285		    &oldnvroot) == 0);
2286
2287	/*
2288	 * Print out the statistics for the pool.
2289	 */
2290	print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2291
2292	if (cb->cb_verbose)
2293		print_iostat_separator(cb);
2294
2295	return (0);
2296}
2297
2298int
2299get_namewidth(zpool_handle_t *zhp, void *data)
2300{
2301	iostat_cbdata_t *cb = data;
2302	nvlist_t *config, *nvroot;
2303
2304	if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2305		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2306		    &nvroot) == 0);
2307		if (!cb->cb_verbose)
2308			cb->cb_namewidth = strlen(zpool_get_name(zhp));
2309		else
2310			cb->cb_namewidth = max_width(zhp, nvroot, 0, 0);
2311	}
2312
2313	/*
2314	 * The width must fall into the range [10,38].  The upper limit is the
2315	 * maximum we can have and still fit in 80 columns.
2316	 */
2317	if (cb->cb_namewidth < 10)
2318		cb->cb_namewidth = 10;
2319	if (cb->cb_namewidth > 38)
2320		cb->cb_namewidth = 38;
2321
2322	return (0);
2323}
2324
2325/*
2326 * Parse the input string, get the 'interval' and 'count' value if there is one.
2327 */
2328static void
2329get_interval_count(int *argcp, char **argv, unsigned long *iv,
2330    unsigned long *cnt)
2331{
2332	unsigned long interval = 0, count = 0;
2333	int argc = *argcp, errno;
2334
2335	/*
2336	 * Determine if the last argument is an integer or a pool name
2337	 */
2338	if (argc > 0 && isdigit(argv[argc - 1][0])) {
2339		char *end;
2340
2341		errno = 0;
2342		interval = strtoul(argv[argc - 1], &end, 10);
2343
2344		if (*end == '\0' && errno == 0) {
2345			if (interval == 0) {
2346				(void) fprintf(stderr, gettext("interval "
2347				    "cannot be zero\n"));
2348				usage(B_FALSE);
2349			}
2350			/*
2351			 * Ignore the last parameter
2352			 */
2353			argc--;
2354		} else {
2355			/*
2356			 * If this is not a valid number, just plow on.  The
2357			 * user will get a more informative error message later
2358			 * on.
2359			 */
2360			interval = 0;
2361		}
2362	}
2363
2364	/*
2365	 * If the last argument is also an integer, then we have both a count
2366	 * and an interval.
2367	 */
2368	if (argc > 0 && isdigit(argv[argc - 1][0])) {
2369		char *end;
2370
2371		errno = 0;
2372		count = interval;
2373		interval = strtoul(argv[argc - 1], &end, 10);
2374
2375		if (*end == '\0' && errno == 0) {
2376			if (interval == 0) {
2377				(void) fprintf(stderr, gettext("interval "
2378				    "cannot be zero\n"));
2379				usage(B_FALSE);
2380			}
2381
2382			/*
2383			 * Ignore the last parameter
2384			 */
2385			argc--;
2386		} else {
2387			interval = 0;
2388		}
2389	}
2390
2391	*iv = interval;
2392	*cnt = count;
2393	*argcp = argc;
2394}
2395
2396static void
2397get_timestamp_arg(char c)
2398{
2399	if (c == 'u')
2400		timestamp_fmt = UDATE;
2401	else if (c == 'd')
2402		timestamp_fmt = DDATE;
2403	else
2404		usage(B_FALSE);
2405}
2406
2407/*
2408 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2409 *
2410 *	-v	Display statistics for individual vdevs
2411 *	-T	Display a timestamp in date(1) or Unix format
2412 *
2413 * This command can be tricky because we want to be able to deal with pool
2414 * creation/destruction as well as vdev configuration changes.  The bulk of this
2415 * processing is handled by the pool_list_* routines in zpool_iter.c.  We rely
2416 * on pool_list_update() to detect the addition of new pools.  Configuration
2417 * changes are all handled within libzfs.
2418 */
2419int
2420zpool_do_iostat(int argc, char **argv)
2421{
2422	int c;
2423	int ret;
2424	int npools;
2425	unsigned long interval = 0, count = 0;
2426	zpool_list_t *list;
2427	boolean_t verbose = B_FALSE;
2428	iostat_cbdata_t cb;
2429
2430	/* check options */
2431	while ((c = getopt(argc, argv, "T:v")) != -1) {
2432		switch (c) {
2433		case 'T':
2434			get_timestamp_arg(*optarg);
2435			break;
2436		case 'v':
2437			verbose = B_TRUE;
2438			break;
2439		case '?':
2440			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2441			    optopt);
2442			usage(B_FALSE);
2443		}
2444	}
2445
2446	argc -= optind;
2447	argv += optind;
2448
2449	get_interval_count(&argc, argv, &interval, &count);
2450
2451	/*
2452	 * Construct the list of all interesting pools.
2453	 */
2454	ret = 0;
2455	if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2456		return (1);
2457
2458	if (pool_list_count(list) == 0 && argc != 0) {
2459		pool_list_free(list);
2460		return (1);
2461	}
2462
2463	if (pool_list_count(list) == 0 && interval == 0) {
2464		pool_list_free(list);
2465		(void) fprintf(stderr, gettext("no pools available\n"));
2466		return (1);
2467	}
2468
2469	/*
2470	 * Enter the main iostat loop.
2471	 */
2472	cb.cb_list = list;
2473	cb.cb_verbose = verbose;
2474	cb.cb_iteration = 0;
2475	cb.cb_namewidth = 0;
2476
2477	for (;;) {
2478		pool_list_update(list);
2479
2480		if ((npools = pool_list_count(list)) == 0)
2481			break;
2482
2483		/*
2484		 * Refresh all statistics.  This is done as an explicit step
2485		 * before calculating the maximum name width, so that any
2486		 * configuration changes are properly accounted for.
2487		 */
2488		(void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2489
2490		/*
2491		 * Iterate over all pools to determine the maximum width
2492		 * for the pool / device name column across all pools.
2493		 */
2494		cb.cb_namewidth = 0;
2495		(void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2496
2497		if (timestamp_fmt != NODATE)
2498			print_timestamp(timestamp_fmt);
2499
2500		/*
2501		 * If it's the first time, or verbose mode, print the header.
2502		 */
2503		if (++cb.cb_iteration == 1 || verbose)
2504			print_iostat_header(&cb);
2505
2506		(void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2507
2508		/*
2509		 * If there's more than one pool, and we're not in verbose mode
2510		 * (which prints a separator for us), then print a separator.
2511		 */
2512		if (npools > 1 && !verbose)
2513			print_iostat_separator(&cb);
2514
2515		if (verbose)
2516			(void) printf("\n");
2517
2518		/*
2519		 * Flush the output so that redirection to a file isn't buffered
2520		 * indefinitely.
2521		 */
2522		(void) fflush(stdout);
2523
2524		if (interval == 0)
2525			break;
2526
2527		if (count != 0 && --count == 0)
2528			break;
2529
2530		(void) sleep(interval);
2531	}
2532
2533	pool_list_free(list);
2534
2535	return (ret);
2536}
2537
2538typedef struct list_cbdata {
2539	boolean_t	cb_scripted;
2540	boolean_t	cb_first;
2541	zprop_list_t	*cb_proplist;
2542} list_cbdata_t;
2543
2544/*
2545 * Given a list of columns to display, output appropriate headers for each one.
2546 */
2547static void
2548print_header(zprop_list_t *pl)
2549{
2550	const char *header;
2551	boolean_t first = B_TRUE;
2552	boolean_t right_justify;
2553
2554	for (; pl != NULL; pl = pl->pl_next) {
2555		if (pl->pl_prop == ZPROP_INVAL)
2556			continue;
2557
2558		if (!first)
2559			(void) printf("  ");
2560		else
2561			first = B_FALSE;
2562
2563		header = zpool_prop_column_name(pl->pl_prop);
2564		right_justify = zpool_prop_align_right(pl->pl_prop);
2565
2566		if (pl->pl_next == NULL && !right_justify)
2567			(void) printf("%s", header);
2568		else if (right_justify)
2569			(void) printf("%*s", pl->pl_width, header);
2570		else
2571			(void) printf("%-*s", pl->pl_width, header);
2572	}
2573
2574	(void) printf("\n");
2575}
2576
2577/*
2578 * Given a pool and a list of properties, print out all the properties according
2579 * to the described layout.
2580 */
2581static void
2582print_pool(zpool_handle_t *zhp, zprop_list_t *pl, int scripted)
2583{
2584	boolean_t first = B_TRUE;
2585	char property[ZPOOL_MAXPROPLEN];
2586	char *propstr;
2587	boolean_t right_justify;
2588	int width;
2589
2590	for (; pl != NULL; pl = pl->pl_next) {
2591		if (!first) {
2592			if (scripted)
2593				(void) printf("\t");
2594			else
2595				(void) printf("  ");
2596		} else {
2597			first = B_FALSE;
2598		}
2599
2600		right_justify = B_FALSE;
2601		if (pl->pl_prop != ZPROP_INVAL) {
2602			if (zpool_get_prop(zhp, pl->pl_prop, property,
2603			    sizeof (property), NULL) != 0)
2604				propstr = "-";
2605			else
2606				propstr = property;
2607
2608			right_justify = zpool_prop_align_right(pl->pl_prop);
2609		} else {
2610			propstr = "-";
2611		}
2612
2613		width = pl->pl_width;
2614
2615		/*
2616		 * If this is being called in scripted mode, or if this is the
2617		 * last column and it is left-justified, don't include a width
2618		 * format specifier.
2619		 */
2620		if (scripted || (pl->pl_next == NULL && !right_justify))
2621			(void) printf("%s", propstr);
2622		else if (right_justify)
2623			(void) printf("%*s", width, propstr);
2624		else
2625			(void) printf("%-*s", width, propstr);
2626	}
2627
2628	(void) printf("\n");
2629}
2630
2631/*
2632 * Generic callback function to list a pool.
2633 */
2634int
2635list_callback(zpool_handle_t *zhp, void *data)
2636{
2637	list_cbdata_t *cbp = data;
2638
2639	if (cbp->cb_first) {
2640		if (!cbp->cb_scripted)
2641			print_header(cbp->cb_proplist);
2642		cbp->cb_first = B_FALSE;
2643	}
2644
2645	print_pool(zhp, cbp->cb_proplist, cbp->cb_scripted);
2646
2647	return (0);
2648}
2649
2650/*
2651 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2652 *
2653 *	-H	Scripted mode.  Don't display headers, and separate properties
2654 *		by a single tab.
2655 *	-o	List of properties to display.  Defaults to
2656 *		"name,size,allocated,free,capacity,health,altroot"
2657 *	-T	Display a timestamp in date(1) or Unix format
2658 *
2659 * List all pools in the system, whether or not they're healthy.  Output space
2660 * statistics for each one, as well as health status summary.
2661 */
2662int
2663zpool_do_list(int argc, char **argv)
2664{
2665	int c;
2666	int ret;
2667	list_cbdata_t cb = { 0 };
2668	static char default_props[] =
2669	    "name,size,allocated,free,capacity,dedupratio,health,altroot";
2670	char *props = default_props;
2671	unsigned long interval = 0, count = 0;
2672
2673	/* check options */
2674	while ((c = getopt(argc, argv, ":Ho:T:")) != -1) {
2675		switch (c) {
2676		case 'H':
2677			cb.cb_scripted = B_TRUE;
2678			break;
2679		case 'o':
2680			props = optarg;
2681			break;
2682		case 'T':
2683			get_timestamp_arg(*optarg);
2684			break;
2685		case ':':
2686			(void) fprintf(stderr, gettext("missing argument for "
2687			    "'%c' option\n"), optopt);
2688			usage(B_FALSE);
2689			break;
2690		case '?':
2691			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2692			    optopt);
2693			usage(B_FALSE);
2694		}
2695	}
2696
2697	argc -= optind;
2698	argv += optind;
2699
2700	get_interval_count(&argc, argv, &interval, &count);
2701
2702	if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
2703		usage(B_FALSE);
2704
2705	cb.cb_first = B_TRUE;
2706
2707	for (;;) {
2708
2709		if (timestamp_fmt != NODATE)
2710			print_timestamp(timestamp_fmt);
2711
2712		ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
2713		    list_callback, &cb);
2714
2715		if (argc == 0 && cb.cb_first && !cb.cb_scripted) {
2716			(void) printf(gettext("no pools available\n"));
2717			zprop_free_list(cb.cb_proplist);
2718			return (0);
2719		}
2720
2721		if (interval == 0)
2722			break;
2723
2724		if (count != 0 && --count == 0)
2725			break;
2726
2727		(void) sleep(interval);
2728	}
2729
2730	zprop_free_list(cb.cb_proplist);
2731	return (ret);
2732}
2733
2734static nvlist_t *
2735zpool_get_vdev_by_name(nvlist_t *nv, char *name)
2736{
2737	nvlist_t **child;
2738	uint_t c, children;
2739	nvlist_t *match;
2740	char *path;
2741
2742	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2743	    &child, &children) != 0) {
2744		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2745		if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
2746			name += sizeof(_PATH_DEV) - 1;
2747		if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
2748			path += sizeof(_PATH_DEV) - 1;
2749		if (strcmp(name, path) == 0)
2750			return (nv);
2751		return (NULL);
2752	}
2753
2754	for (c = 0; c < children; c++)
2755		if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
2756			return (match);
2757
2758	return (NULL);
2759}
2760
2761static int
2762zpool_do_attach_or_replace(int argc, char **argv, int replacing)
2763{
2764	boolean_t force = B_FALSE;
2765	int c;
2766	nvlist_t *nvroot;
2767	char *poolname, *old_disk, *new_disk;
2768	zpool_handle_t *zhp;
2769	int ret;
2770
2771	/* check options */
2772	while ((c = getopt(argc, argv, "f")) != -1) {
2773		switch (c) {
2774		case 'f':
2775			force = B_TRUE;
2776			break;
2777		case '?':
2778			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2779			    optopt);
2780			usage(B_FALSE);
2781		}
2782	}
2783
2784	argc -= optind;
2785	argv += optind;
2786
2787	/* get pool name and check number of arguments */
2788	if (argc < 1) {
2789		(void) fprintf(stderr, gettext("missing pool name argument\n"));
2790		usage(B_FALSE);
2791	}
2792
2793	poolname = argv[0];
2794
2795	if (argc < 2) {
2796		(void) fprintf(stderr,
2797		    gettext("missing <device> specification\n"));
2798		usage(B_FALSE);
2799	}
2800
2801	old_disk = argv[1];
2802
2803	if (argc < 3) {
2804		if (!replacing) {
2805			(void) fprintf(stderr,
2806			    gettext("missing <new_device> specification\n"));
2807			usage(B_FALSE);
2808		}
2809		new_disk = old_disk;
2810		argc -= 1;
2811		argv += 1;
2812	} else {
2813		new_disk = argv[2];
2814		argc -= 2;
2815		argv += 2;
2816	}
2817
2818	if (argc > 1) {
2819		(void) fprintf(stderr, gettext("too many arguments\n"));
2820		usage(B_FALSE);
2821	}
2822
2823	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2824		return (1);
2825
2826	if (zpool_get_config(zhp, NULL) == NULL) {
2827		(void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
2828		    poolname);
2829		zpool_close(zhp);
2830		return (1);
2831	}
2832
2833	nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
2834	    argc, argv);
2835	if (nvroot == NULL) {
2836		zpool_close(zhp);
2837		return (1);
2838	}
2839
2840	ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
2841
2842	nvlist_free(nvroot);
2843	zpool_close(zhp);
2844
2845	return (ret);
2846}
2847
2848/*
2849 * zpool replace [-f] <pool> <device> <new_device>
2850 *
2851 *	-f	Force attach, even if <new_device> appears to be in use.
2852 *
2853 * Replace <device> with <new_device>.
2854 */
2855/* ARGSUSED */
2856int
2857zpool_do_replace(int argc, char **argv)
2858{
2859	return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
2860}
2861
2862/*
2863 * zpool attach [-f] <pool> <device> <new_device>
2864 *
2865 *	-f	Force attach, even if <new_device> appears to be in use.
2866 *
2867 * Attach <new_device> to the mirror containing <device>.  If <device> is not
2868 * part of a mirror, then <device> will be transformed into a mirror of
2869 * <device> and <new_device>.  In either case, <new_device> will begin life
2870 * with a DTL of [0, now], and will immediately begin to resilver itself.
2871 */
2872int
2873zpool_do_attach(int argc, char **argv)
2874{
2875	return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
2876}
2877
2878/*
2879 * zpool detach [-f] <pool> <device>
2880 *
2881 *	-f	Force detach of <device>, even if DTLs argue against it
2882 *		(not supported yet)
2883 *
2884 * Detach a device from a mirror.  The operation will be refused if <device>
2885 * is the last device in the mirror, or if the DTLs indicate that this device
2886 * has the only valid copy of some data.
2887 */
2888/* ARGSUSED */
2889int
2890zpool_do_detach(int argc, char **argv)
2891{
2892	int c;
2893	char *poolname, *path;
2894	zpool_handle_t *zhp;
2895	int ret;
2896
2897	/* check options */
2898	while ((c = getopt(argc, argv, "f")) != -1) {
2899		switch (c) {
2900		case 'f':
2901		case '?':
2902			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2903			    optopt);
2904			usage(B_FALSE);
2905		}
2906	}
2907
2908	argc -= optind;
2909	argv += optind;
2910
2911	/* get pool name and check number of arguments */
2912	if (argc < 1) {
2913		(void) fprintf(stderr, gettext("missing pool name argument\n"));
2914		usage(B_FALSE);
2915	}
2916
2917	if (argc < 2) {
2918		(void) fprintf(stderr,
2919		    gettext("missing <device> specification\n"));
2920		usage(B_FALSE);
2921	}
2922
2923	poolname = argv[0];
2924	path = argv[1];
2925
2926	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2927		return (1);
2928
2929	ret = zpool_vdev_detach(zhp, path);
2930
2931	zpool_close(zhp);
2932
2933	return (ret);
2934}
2935
2936/*
2937 * zpool split [-n] [-o prop=val] ...
2938 *		[-o mntopt] ...
2939 *		[-R altroot] <pool> <newpool> [<device> ...]
2940 *
2941 *	-n	Do not split the pool, but display the resulting layout if
2942 *		it were to be split.
2943 *	-o	Set property=value, or set mount options.
2944 *	-R	Mount the split-off pool under an alternate root.
2945 *
2946 * Splits the named pool and gives it the new pool name.  Devices to be split
2947 * off may be listed, provided that no more than one device is specified
2948 * per top-level vdev mirror.  The newly split pool is left in an exported
2949 * state unless -R is specified.
2950 *
2951 * Restrictions: the top-level of the pool pool must only be made up of
2952 * mirrors; all devices in the pool must be healthy; no device may be
2953 * undergoing a resilvering operation.
2954 */
2955int
2956zpool_do_split(int argc, char **argv)
2957{
2958	char *srcpool, *newpool, *propval;
2959	char *mntopts = NULL;
2960	splitflags_t flags;
2961	int c, ret = 0;
2962	zpool_handle_t *zhp;
2963	nvlist_t *config, *props = NULL;
2964
2965	flags.dryrun = B_FALSE;
2966	flags.import = B_FALSE;
2967
2968	/* check options */
2969	while ((c = getopt(argc, argv, ":R:no:")) != -1) {
2970		switch (c) {
2971		case 'R':
2972			flags.import = B_TRUE;
2973			if (add_prop_list(
2974			    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
2975			    &props, B_TRUE) != 0) {
2976				if (props)
2977					nvlist_free(props);
2978				usage(B_FALSE);
2979			}
2980			break;
2981		case 'n':
2982			flags.dryrun = B_TRUE;
2983			break;
2984		case 'o':
2985			if ((propval = strchr(optarg, '=')) != NULL) {
2986				*propval = '\0';
2987				propval++;
2988				if (add_prop_list(optarg, propval,
2989				    &props, B_TRUE) != 0) {
2990					if (props)
2991						nvlist_free(props);
2992					usage(B_FALSE);
2993				}
2994			} else {
2995				mntopts = optarg;
2996			}
2997			break;
2998		case ':':
2999			(void) fprintf(stderr, gettext("missing argument for "
3000			    "'%c' option\n"), optopt);
3001			usage(B_FALSE);
3002			break;
3003		case '?':
3004			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3005			    optopt);
3006			usage(B_FALSE);
3007			break;
3008		}
3009	}
3010
3011	if (!flags.import && mntopts != NULL) {
3012		(void) fprintf(stderr, gettext("setting mntopts is only "
3013		    "valid when importing the pool\n"));
3014		usage(B_FALSE);
3015	}
3016
3017	argc -= optind;
3018	argv += optind;
3019
3020	if (argc < 1) {
3021		(void) fprintf(stderr, gettext("Missing pool name\n"));
3022		usage(B_FALSE);
3023	}
3024	if (argc < 2) {
3025		(void) fprintf(stderr, gettext("Missing new pool name\n"));
3026		usage(B_FALSE);
3027	}
3028
3029	srcpool = argv[0];
3030	newpool = argv[1];
3031
3032	argc -= 2;
3033	argv += 2;
3034
3035	if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3036		return (1);
3037
3038	config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3039	if (config == NULL) {
3040		ret = 1;
3041	} else {
3042		if (flags.dryrun) {
3043			(void) printf(gettext("would create '%s' with the "
3044			    "following layout:\n\n"), newpool);
3045			print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3046		}
3047		nvlist_free(config);
3048	}
3049
3050	zpool_close(zhp);
3051
3052	if (ret != 0 || flags.dryrun || !flags.import)
3053		return (ret);
3054
3055	/*
3056	 * The split was successful. Now we need to open the new
3057	 * pool and import it.
3058	 */
3059	if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3060		return (1);
3061	if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3062	    zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3063		ret = 1;
3064		(void) fprintf(stderr, gettext("Split was succssful, but "
3065		    "the datasets could not all be mounted\n"));
3066		(void) fprintf(stderr, gettext("Try doing '%s' with a "
3067		    "different altroot\n"), "zpool import");
3068	}
3069	zpool_close(zhp);
3070
3071	return (ret);
3072}
3073
3074
3075
3076/*
3077 * zpool online <pool> <device> ...
3078 */
3079int
3080zpool_do_online(int argc, char **argv)
3081{
3082	int c, i;
3083	char *poolname;
3084	zpool_handle_t *zhp;
3085	int ret = 0;
3086	vdev_state_t newstate;
3087	int flags = 0;
3088
3089	/* check options */
3090	while ((c = getopt(argc, argv, "et")) != -1) {
3091		switch (c) {
3092		case 'e':
3093			flags |= ZFS_ONLINE_EXPAND;
3094			break;
3095		case 't':
3096		case '?':
3097			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3098			    optopt);
3099			usage(B_FALSE);
3100		}
3101	}
3102
3103	argc -= optind;
3104	argv += optind;
3105
3106	/* get pool name and check number of arguments */
3107	if (argc < 1) {
3108		(void) fprintf(stderr, gettext("missing pool name\n"));
3109		usage(B_FALSE);
3110	}
3111	if (argc < 2) {
3112		(void) fprintf(stderr, gettext("missing device name\n"));
3113		usage(B_FALSE);
3114	}
3115
3116	poolname = argv[0];
3117
3118	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3119		return (1);
3120
3121	for (i = 1; i < argc; i++) {
3122		if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3123			if (newstate != VDEV_STATE_HEALTHY) {
3124				(void) printf(gettext("warning: device '%s' "
3125				    "onlined, but remains in faulted state\n"),
3126				    argv[i]);
3127				if (newstate == VDEV_STATE_FAULTED)
3128					(void) printf(gettext("use 'zpool "
3129					    "clear' to restore a faulted "
3130					    "device\n"));
3131				else
3132					(void) printf(gettext("use 'zpool "
3133					    "replace' to replace devices "
3134					    "that are no longer present\n"));
3135			}
3136		} else {
3137			ret = 1;
3138		}
3139	}
3140
3141	zpool_close(zhp);
3142
3143	return (ret);
3144}
3145
3146/*
3147 * zpool offline [-ft] <pool> <device> ...
3148 *
3149 *	-f	Force the device into the offline state, even if doing
3150 *		so would appear to compromise pool availability.
3151 *		(not supported yet)
3152 *
3153 *	-t	Only take the device off-line temporarily.  The offline
3154 *		state will not be persistent across reboots.
3155 */
3156/* ARGSUSED */
3157int
3158zpool_do_offline(int argc, char **argv)
3159{
3160	int c, i;
3161	char *poolname;
3162	zpool_handle_t *zhp;
3163	int ret = 0;
3164	boolean_t istmp = B_FALSE;
3165
3166	/* check options */
3167	while ((c = getopt(argc, argv, "ft")) != -1) {
3168		switch (c) {
3169		case 't':
3170			istmp = B_TRUE;
3171			break;
3172		case 'f':
3173		case '?':
3174			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3175			    optopt);
3176			usage(B_FALSE);
3177		}
3178	}
3179
3180	argc -= optind;
3181	argv += optind;
3182
3183	/* get pool name and check number of arguments */
3184	if (argc < 1) {
3185		(void) fprintf(stderr, gettext("missing pool name\n"));
3186		usage(B_FALSE);
3187	}
3188	if (argc < 2) {
3189		(void) fprintf(stderr, gettext("missing device name\n"));
3190		usage(B_FALSE);
3191	}
3192
3193	poolname = argv[0];
3194
3195	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3196		return (1);
3197
3198	for (i = 1; i < argc; i++) {
3199		if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3200			ret = 1;
3201	}
3202
3203	zpool_close(zhp);
3204
3205	return (ret);
3206}
3207
3208/*
3209 * zpool clear <pool> [device]
3210 *
3211 * Clear all errors associated with a pool or a particular device.
3212 */
3213int
3214zpool_do_clear(int argc, char **argv)
3215{
3216	int c;
3217	int ret = 0;
3218	boolean_t dryrun = B_FALSE;
3219	boolean_t do_rewind = B_FALSE;
3220	boolean_t xtreme_rewind = B_FALSE;
3221	uint32_t rewind_policy = ZPOOL_NO_REWIND;
3222	nvlist_t *policy = NULL;
3223	zpool_handle_t *zhp;
3224	char *pool, *device;
3225
3226	/* check options */
3227	while ((c = getopt(argc, argv, "FnX")) != -1) {
3228		switch (c) {
3229		case 'F':
3230			do_rewind = B_TRUE;
3231			break;
3232		case 'n':
3233			dryrun = B_TRUE;
3234			break;
3235		case 'X':
3236			xtreme_rewind = B_TRUE;
3237			break;
3238		case '?':
3239			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3240			    optopt);
3241			usage(B_FALSE);
3242		}
3243	}
3244
3245	argc -= optind;
3246	argv += optind;
3247
3248	if (argc < 1) {
3249		(void) fprintf(stderr, gettext("missing pool name\n"));
3250		usage(B_FALSE);
3251	}
3252
3253	if (argc > 2) {
3254		(void) fprintf(stderr, gettext("too many arguments\n"));
3255		usage(B_FALSE);
3256	}
3257
3258	if ((dryrun || xtreme_rewind) && !do_rewind) {
3259		(void) fprintf(stderr,
3260		    gettext("-n or -X only meaningful with -F\n"));
3261		usage(B_FALSE);
3262	}
3263	if (dryrun)
3264		rewind_policy = ZPOOL_TRY_REWIND;
3265	else if (do_rewind)
3266		rewind_policy = ZPOOL_DO_REWIND;
3267	if (xtreme_rewind)
3268		rewind_policy |= ZPOOL_EXTREME_REWIND;
3269
3270	/* In future, further rewind policy choices can be passed along here */
3271	if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3272	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3273		return (1);
3274
3275	pool = argv[0];
3276	device = argc == 2 ? argv[1] : NULL;
3277
3278	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3279		nvlist_free(policy);
3280		return (1);
3281	}
3282
3283	if (zpool_clear(zhp, device, policy) != 0)
3284		ret = 1;
3285
3286	zpool_close(zhp);
3287
3288	nvlist_free(policy);
3289
3290	return (ret);
3291}
3292
3293typedef struct scrub_cbdata {
3294	int	cb_type;
3295	int	cb_argc;
3296	char	**cb_argv;
3297} scrub_cbdata_t;
3298
3299int
3300scrub_callback(zpool_handle_t *zhp, void *data)
3301{
3302	scrub_cbdata_t *cb = data;
3303	int err;
3304
3305	/*
3306	 * Ignore faulted pools.
3307	 */
3308	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3309		(void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3310		    "currently unavailable\n"), zpool_get_name(zhp));
3311		return (1);
3312	}
3313
3314	err = zpool_scan(zhp, cb->cb_type);
3315
3316	return (err != 0);
3317}
3318
3319/*
3320 * zpool scrub [-s] <pool> ...
3321 *
3322 *	-s	Stop.  Stops any in-progress scrub.
3323 */
3324int
3325zpool_do_scrub(int argc, char **argv)
3326{
3327	int c;
3328	scrub_cbdata_t cb;
3329
3330	cb.cb_type = POOL_SCAN_SCRUB;
3331
3332	/* check options */
3333	while ((c = getopt(argc, argv, "s")) != -1) {
3334		switch (c) {
3335		case 's':
3336			cb.cb_type = POOL_SCAN_NONE;
3337			break;
3338		case '?':
3339			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3340			    optopt);
3341			usage(B_FALSE);
3342		}
3343	}
3344
3345	cb.cb_argc = argc;
3346	cb.cb_argv = argv;
3347	argc -= optind;
3348	argv += optind;
3349
3350	if (argc < 1) {
3351		(void) fprintf(stderr, gettext("missing pool name argument\n"));
3352		usage(B_FALSE);
3353	}
3354
3355	return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3356}
3357
3358typedef struct status_cbdata {
3359	int		cb_count;
3360	boolean_t	cb_allpools;
3361	boolean_t	cb_verbose;
3362	boolean_t	cb_explain;
3363	boolean_t	cb_first;
3364	boolean_t	cb_dedup_stats;
3365} status_cbdata_t;
3366
3367/*
3368 * Print out detailed scrub status.
3369 */
3370void
3371print_scan_status(pool_scan_stat_t *ps)
3372{
3373	time_t start, end;
3374	uint64_t elapsed, mins_left, hours_left;
3375	uint64_t pass_exam, examined, total;
3376	uint_t rate;
3377	double fraction_done;
3378	char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3379
3380	(void) printf(gettext(" scan: "));
3381
3382	/* If there's never been a scan, there's not much to say. */
3383	if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3384	    ps->pss_func >= POOL_SCAN_FUNCS) {
3385		(void) printf(gettext("none requested\n"));
3386		return;
3387	}
3388
3389	start = ps->pss_start_time;
3390	end = ps->pss_end_time;
3391	zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3392
3393	assert(ps->pss_func == POOL_SCAN_SCRUB ||
3394	    ps->pss_func == POOL_SCAN_RESILVER);
3395	/*
3396	 * Scan is finished or canceled.
3397	 */
3398	if (ps->pss_state == DSS_FINISHED) {
3399		uint64_t minutes_taken = (end - start) / 60;
3400		char *fmt;
3401
3402		if (ps->pss_func == POOL_SCAN_SCRUB) {
3403			fmt = gettext("scrub repaired %s in %lluh%um with "
3404			    "%llu errors on %s");
3405		} else if (ps->pss_func == POOL_SCAN_RESILVER) {
3406			fmt = gettext("resilvered %s in %lluh%um with "
3407			    "%llu errors on %s");
3408		}
3409		/* LINTED */
3410		(void) printf(fmt, processed_buf,
3411		    (u_longlong_t)(minutes_taken / 60),
3412		    (uint_t)(minutes_taken % 60),
3413		    (u_longlong_t)ps->pss_errors,
3414		    ctime((time_t *)&end));
3415		return;
3416	} else if (ps->pss_state == DSS_CANCELED) {
3417		if (ps->pss_func == POOL_SCAN_SCRUB) {
3418			(void) printf(gettext("scrub canceled on %s"),
3419			    ctime(&end));
3420		} else if (ps->pss_func == POOL_SCAN_RESILVER) {
3421			(void) printf(gettext("resilver canceled on %s"),
3422			    ctime(&end));
3423		}
3424		return;
3425	}
3426
3427	assert(ps->pss_state == DSS_SCANNING);
3428
3429	/*
3430	 * Scan is in progress.
3431	 */
3432	if (ps->pss_func == POOL_SCAN_SCRUB) {
3433		(void) printf(gettext("scrub in progress since %s"),
3434		    ctime(&start));
3435	} else if (ps->pss_func == POOL_SCAN_RESILVER) {
3436		(void) printf(gettext("resilver in progress since %s"),
3437		    ctime(&start));
3438	}
3439
3440	examined = ps->pss_examined ? ps->pss_examined : 1;
3441	total = ps->pss_to_examine;
3442	fraction_done = (double)examined / total;
3443
3444	/* elapsed time for this pass */
3445	elapsed = time(NULL) - ps->pss_pass_start;
3446	elapsed = elapsed ? elapsed : 1;
3447	pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3448	rate = pass_exam / elapsed;
3449	rate = rate ? rate : 1;
3450	mins_left = ((total - examined) / rate) / 60;
3451	hours_left = mins_left / 60;
3452
3453	zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3454	zfs_nicenum(total, total_buf, sizeof (total_buf));
3455	zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3456
3457	/*
3458	 * do not print estimated time if hours_left is more than 30 days
3459	 */
3460	(void) printf(gettext("    %s scanned out of %s at %s/s"),
3461	    examined_buf, total_buf, rate_buf);
3462	if (hours_left < (30 * 24)) {
3463		(void) printf(gettext(", %lluh%um to go\n"),
3464		    (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3465	} else {
3466		(void) printf(gettext(
3467		    ", (scan is slow, no estimated time)\n"));
3468	}
3469
3470	if (ps->pss_func == POOL_SCAN_RESILVER) {
3471		(void) printf(gettext("    %s resilvered, %.2f%% done\n"),
3472		    processed_buf, 100 * fraction_done);
3473	} else if (ps->pss_func == POOL_SCAN_SCRUB) {
3474		(void) printf(gettext("    %s repaired, %.2f%% done\n"),
3475		    processed_buf, 100 * fraction_done);
3476	}
3477}
3478
3479static void
3480print_error_log(zpool_handle_t *zhp)
3481{
3482	nvlist_t *nverrlist = NULL;
3483	nvpair_t *elem;
3484	char *pathname;
3485	size_t len = MAXPATHLEN * 2;
3486
3487	if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3488		(void) printf("errors: List of errors unavailable "
3489		    "(insufficient privileges)\n");
3490		return;
3491	}
3492
3493	(void) printf("errors: Permanent errors have been "
3494	    "detected in the following files:\n\n");
3495
3496	pathname = safe_malloc(len);
3497	elem = NULL;
3498	while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
3499		nvlist_t *nv;
3500		uint64_t dsobj, obj;
3501
3502		verify(nvpair_value_nvlist(elem, &nv) == 0);
3503		verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
3504		    &dsobj) == 0);
3505		verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
3506		    &obj) == 0);
3507		zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
3508		(void) printf("%7s %s\n", "", pathname);
3509	}
3510	free(pathname);
3511	nvlist_free(nverrlist);
3512}
3513
3514static void
3515print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
3516    int namewidth)
3517{
3518	uint_t i;
3519	char *name;
3520
3521	if (nspares == 0)
3522		return;
3523
3524	(void) printf(gettext("\tspares\n"));
3525
3526	for (i = 0; i < nspares; i++) {
3527		name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
3528		print_status_config(zhp, name, spares[i],
3529		    namewidth, 2, B_TRUE);
3530		free(name);
3531	}
3532}
3533
3534static void
3535print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3536    int namewidth)
3537{
3538	uint_t i;
3539	char *name;
3540
3541	if (nl2cache == 0)
3542		return;
3543
3544	(void) printf(gettext("\tcache\n"));
3545
3546	for (i = 0; i < nl2cache; i++) {
3547		name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3548		print_status_config(zhp, name, l2cache[i],
3549		    namewidth, 2, B_FALSE);
3550		free(name);
3551	}
3552}
3553
3554static void
3555print_dedup_stats(nvlist_t *config)
3556{
3557	ddt_histogram_t *ddh;
3558	ddt_stat_t *dds;
3559	ddt_object_t *ddo;
3560	uint_t c;
3561
3562	/*
3563	 * If the pool was faulted then we may not have been able to
3564	 * obtain the config. Otherwise, if have anything in the dedup
3565	 * table continue processing the stats.
3566	 */
3567	if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
3568	    (uint64_t **)&ddo, &c) != 0 || ddo->ddo_count == 0)
3569		return;
3570
3571	(void) printf("\n");
3572	(void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
3573	    (u_longlong_t)ddo->ddo_count,
3574	    (u_longlong_t)ddo->ddo_dspace,
3575	    (u_longlong_t)ddo->ddo_mspace);
3576
3577	verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
3578	    (uint64_t **)&dds, &c) == 0);
3579	verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
3580	    (uint64_t **)&ddh, &c) == 0);
3581	zpool_dump_ddt(dds, ddh);
3582}
3583
3584/*
3585 * Display a summary of pool status.  Displays a summary such as:
3586 *
3587 *        pool: tank
3588 *	status: DEGRADED
3589 *	reason: One or more devices ...
3590 *         see: http://www.sun.com/msg/ZFS-xxxx-01
3591 *	config:
3592 *		mirror		DEGRADED
3593 *                c1t0d0	OK
3594 *                c2t0d0	UNAVAIL
3595 *
3596 * When given the '-v' option, we print out the complete config.  If the '-e'
3597 * option is specified, then we print out error rate information as well.
3598 */
3599int
3600status_callback(zpool_handle_t *zhp, void *data)
3601{
3602	status_cbdata_t *cbp = data;
3603	nvlist_t *config, *nvroot;
3604	char *msgid;
3605	int reason;
3606	const char *health;
3607	uint_t c;
3608	vdev_stat_t *vs;
3609
3610	config = zpool_get_config(zhp, NULL);
3611	reason = zpool_get_status(zhp, &msgid);
3612
3613	cbp->cb_count++;
3614
3615	/*
3616	 * If we were given 'zpool status -x', only report those pools with
3617	 * problems.
3618	 */
3619	if (reason == ZPOOL_STATUS_OK && cbp->cb_explain) {
3620		if (!cbp->cb_allpools) {
3621			(void) printf(gettext("pool '%s' is healthy\n"),
3622			    zpool_get_name(zhp));
3623			if (cbp->cb_first)
3624				cbp->cb_first = B_FALSE;
3625		}
3626		return (0);
3627	}
3628
3629	if (cbp->cb_first)
3630		cbp->cb_first = B_FALSE;
3631	else
3632		(void) printf("\n");
3633
3634	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3635	    &nvroot) == 0);
3636	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
3637	    (uint64_t **)&vs, &c) == 0);
3638	health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
3639
3640	(void) printf(gettext("  pool: %s\n"), zpool_get_name(zhp));
3641	(void) printf(gettext(" state: %s\n"), health);
3642
3643	switch (reason) {
3644	case ZPOOL_STATUS_MISSING_DEV_R:
3645		(void) printf(gettext("status: One or more devices could not "
3646		    "be opened.  Sufficient replicas exist for\n\tthe pool to "
3647		    "continue functioning in a degraded state.\n"));
3648		(void) printf(gettext("action: Attach the missing device and "
3649		    "online it using 'zpool online'.\n"));
3650		break;
3651
3652	case ZPOOL_STATUS_MISSING_DEV_NR:
3653		(void) printf(gettext("status: One or more devices could not "
3654		    "be opened.  There are insufficient\n\treplicas for the "
3655		    "pool to continue functioning.\n"));
3656		(void) printf(gettext("action: Attach the missing device and "
3657		    "online it using 'zpool online'.\n"));
3658		break;
3659
3660	case ZPOOL_STATUS_CORRUPT_LABEL_R:
3661		(void) printf(gettext("status: One or more devices could not "
3662		    "be used because the label is missing or\n\tinvalid.  "
3663		    "Sufficient replicas exist for the pool to continue\n\t"
3664		    "functioning in a degraded state.\n"));
3665		(void) printf(gettext("action: Replace the device using "
3666		    "'zpool replace'.\n"));
3667		break;
3668
3669	case ZPOOL_STATUS_CORRUPT_LABEL_NR:
3670		(void) printf(gettext("status: One or more devices could not "
3671		    "be used because the label is missing \n\tor invalid.  "
3672		    "There are insufficient replicas for the pool to "
3673		    "continue\n\tfunctioning.\n"));
3674		zpool_explain_recover(zpool_get_handle(zhp),
3675		    zpool_get_name(zhp), reason, config);
3676		break;
3677
3678	case ZPOOL_STATUS_FAILING_DEV:
3679		(void) printf(gettext("status: One or more devices has "
3680		    "experienced an unrecoverable error.  An\n\tattempt was "
3681		    "made to correct the error.  Applications are "
3682		    "unaffected.\n"));
3683		(void) printf(gettext("action: Determine if the device needs "
3684		    "to be replaced, and clear the errors\n\tusing "
3685		    "'zpool clear' or replace the device with 'zpool "
3686		    "replace'.\n"));
3687		break;
3688
3689	case ZPOOL_STATUS_OFFLINE_DEV:
3690		(void) printf(gettext("status: One or more devices has "
3691		    "been taken offline by the administrator.\n\tSufficient "
3692		    "replicas exist for the pool to continue functioning in "
3693		    "a\n\tdegraded state.\n"));
3694		(void) printf(gettext("action: Online the device using "
3695		    "'zpool online' or replace the device with\n\t'zpool "
3696		    "replace'.\n"));
3697		break;
3698
3699	case ZPOOL_STATUS_REMOVED_DEV:
3700		(void) printf(gettext("status: One or more devices has "
3701		    "been removed by the administrator.\n\tSufficient "
3702		    "replicas exist for the pool to continue functioning in "
3703		    "a\n\tdegraded state.\n"));
3704		(void) printf(gettext("action: Online the device using "
3705		    "'zpool online' or replace the device with\n\t'zpool "
3706		    "replace'.\n"));
3707		break;
3708
3709	case ZPOOL_STATUS_RESILVERING:
3710		(void) printf(gettext("status: One or more devices is "
3711		    "currently being resilvered.  The pool will\n\tcontinue "
3712		    "to function, possibly in a degraded state.\n"));
3713		(void) printf(gettext("action: Wait for the resilver to "
3714		    "complete.\n"));
3715		break;
3716
3717	case ZPOOL_STATUS_CORRUPT_DATA:
3718		(void) printf(gettext("status: One or more devices has "
3719		    "experienced an error resulting in data\n\tcorruption.  "
3720		    "Applications may be affected.\n"));
3721		(void) printf(gettext("action: Restore the file in question "
3722		    "if possible.  Otherwise restore the\n\tentire pool from "
3723		    "backup.\n"));
3724		break;
3725
3726	case ZPOOL_STATUS_CORRUPT_POOL:
3727		(void) printf(gettext("status: The pool metadata is corrupted "
3728		    "and the pool cannot be opened.\n"));
3729		zpool_explain_recover(zpool_get_handle(zhp),
3730		    zpool_get_name(zhp), reason, config);
3731		break;
3732
3733	case ZPOOL_STATUS_VERSION_OLDER:
3734		(void) printf(gettext("status: The pool is formatted using an "
3735		    "older on-disk format.  The pool can\n\tstill be used, but "
3736		    "some features are unavailable.\n"));
3737		(void) printf(gettext("action: Upgrade the pool using 'zpool "
3738		    "upgrade'.  Once this is done, the\n\tpool will no longer "
3739		    "be accessible on older software versions.\n"));
3740		break;
3741
3742	case ZPOOL_STATUS_VERSION_NEWER:
3743		(void) printf(gettext("status: The pool has been upgraded to a "
3744		    "newer, incompatible on-disk version.\n\tThe pool cannot "
3745		    "be accessed on this system.\n"));
3746		(void) printf(gettext("action: Access the pool from a system "
3747		    "running more recent software, or\n\trestore the pool from "
3748		    "backup.\n"));
3749		break;
3750
3751	case ZPOOL_STATUS_FAULTED_DEV_R:
3752		(void) printf(gettext("status: One or more devices are "
3753		    "faulted in response to persistent errors.\n\tSufficient "
3754		    "replicas exist for the pool to continue functioning "
3755		    "in a\n\tdegraded state.\n"));
3756		(void) printf(gettext("action: Replace the faulted device, "
3757		    "or use 'zpool clear' to mark the device\n\trepaired.\n"));
3758		break;
3759
3760	case ZPOOL_STATUS_FAULTED_DEV_NR:
3761		(void) printf(gettext("status: One or more devices are "
3762		    "faulted in response to persistent errors.  There are "
3763		    "insufficient replicas for the pool to\n\tcontinue "
3764		    "functioning.\n"));
3765		(void) printf(gettext("action: Destroy and re-create the pool "
3766		    "from a backup source.  Manually marking the device\n"
3767		    "\trepaired using 'zpool clear' may allow some data "
3768		    "to be recovered.\n"));
3769		break;
3770
3771	case ZPOOL_STATUS_IO_FAILURE_WAIT:
3772	case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
3773		(void) printf(gettext("status: One or more devices are "
3774		    "faulted in response to IO failures.\n"));
3775		(void) printf(gettext("action: Make sure the affected devices "
3776		    "are connected, then run 'zpool clear'.\n"));
3777		break;
3778
3779	case ZPOOL_STATUS_BAD_LOG:
3780		(void) printf(gettext("status: An intent log record "
3781		    "could not be read.\n"
3782		    "\tWaiting for adminstrator intervention to fix the "
3783		    "faulted pool.\n"));
3784		(void) printf(gettext("action: Either restore the affected "
3785		    "device(s) and run 'zpool online',\n"
3786		    "\tor ignore the intent log records by running "
3787		    "'zpool clear'.\n"));
3788		break;
3789
3790	default:
3791		/*
3792		 * The remaining errors can't actually be generated, yet.
3793		 */
3794		assert(reason == ZPOOL_STATUS_OK);
3795	}
3796
3797	if (msgid != NULL)
3798		(void) printf(gettext("   see: http://www.sun.com/msg/%s\n"),
3799		    msgid);
3800
3801	if (config != NULL) {
3802		int namewidth;
3803		uint64_t nerr;
3804		nvlist_t **spares, **l2cache;
3805		uint_t nspares, nl2cache;
3806		pool_scan_stat_t *ps = NULL;
3807
3808		(void) nvlist_lookup_uint64_array(nvroot,
3809		    ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
3810		print_scan_status(ps);
3811
3812		namewidth = max_width(zhp, nvroot, 0, 0);
3813		if (namewidth < 10)
3814			namewidth = 10;
3815
3816		(void) printf(gettext("config:\n\n"));
3817		(void) printf(gettext("\t%-*s  %-8s %5s %5s %5s\n"), namewidth,
3818		    "NAME", "STATE", "READ", "WRITE", "CKSUM");
3819		print_status_config(zhp, zpool_get_name(zhp), nvroot,
3820		    namewidth, 0, B_FALSE);
3821
3822		if (num_logs(nvroot) > 0)
3823			print_logs(zhp, nvroot, namewidth, B_TRUE);
3824		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3825		    &l2cache, &nl2cache) == 0)
3826			print_l2cache(zhp, l2cache, nl2cache, namewidth);
3827
3828		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3829		    &spares, &nspares) == 0)
3830			print_spares(zhp, spares, nspares, namewidth);
3831
3832		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
3833		    &nerr) == 0) {
3834			nvlist_t *nverrlist = NULL;
3835
3836			/*
3837			 * If the approximate error count is small, get a
3838			 * precise count by fetching the entire log and
3839			 * uniquifying the results.
3840			 */
3841			if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
3842			    zpool_get_errlog(zhp, &nverrlist) == 0) {
3843				nvpair_t *elem;
3844
3845				elem = NULL;
3846				nerr = 0;
3847				while ((elem = nvlist_next_nvpair(nverrlist,
3848				    elem)) != NULL) {
3849					nerr++;
3850				}
3851			}
3852			nvlist_free(nverrlist);
3853
3854			(void) printf("\n");
3855
3856			if (nerr == 0)
3857				(void) printf(gettext("errors: No known data "
3858				    "errors\n"));
3859			else if (!cbp->cb_verbose)
3860				(void) printf(gettext("errors: %llu data "
3861				    "errors, use '-v' for a list\n"),
3862				    (u_longlong_t)nerr);
3863			else
3864				print_error_log(zhp);
3865		}
3866
3867		if (cbp->cb_dedup_stats)
3868			print_dedup_stats(config);
3869	} else {
3870		(void) printf(gettext("config: The configuration cannot be "
3871		    "determined.\n"));
3872	}
3873
3874	return (0);
3875}
3876
3877/*
3878 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
3879 *
3880 *	-v	Display complete error logs
3881 *	-x	Display only pools with potential problems
3882 *	-D	Display dedup status (undocumented)
3883 *	-T	Display a timestamp in date(1) or Unix format
3884 *
3885 * Describes the health status of all pools or some subset.
3886 */
3887int
3888zpool_do_status(int argc, char **argv)
3889{
3890	int c;
3891	int ret;
3892	unsigned long interval = 0, count = 0;
3893	status_cbdata_t cb = { 0 };
3894
3895	/* check options */
3896	while ((c = getopt(argc, argv, "vxDT:")) != -1) {
3897		switch (c) {
3898		case 'v':
3899			cb.cb_verbose = B_TRUE;
3900			break;
3901		case 'x':
3902			cb.cb_explain = B_TRUE;
3903			break;
3904		case 'D':
3905			cb.cb_dedup_stats = B_TRUE;
3906			break;
3907		case 'T':
3908			get_timestamp_arg(*optarg);
3909			break;
3910		case '?':
3911			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3912			    optopt);
3913			usage(B_FALSE);
3914		}
3915	}
3916
3917	argc -= optind;
3918	argv += optind;
3919
3920	get_interval_count(&argc, argv, &interval, &count);
3921
3922	if (argc == 0)
3923		cb.cb_allpools = B_TRUE;
3924
3925	cb.cb_first = B_TRUE;
3926
3927	for (;;) {
3928		if (timestamp_fmt != NODATE)
3929			print_timestamp(timestamp_fmt);
3930
3931		ret = for_each_pool(argc, argv, B_TRUE, NULL,
3932		    status_callback, &cb);
3933
3934		if (argc == 0 && cb.cb_count == 0)
3935			(void) printf(gettext("no pools available\n"));
3936		else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
3937			(void) printf(gettext("all pools are healthy\n"));
3938
3939		if (ret != 0)
3940			return (ret);
3941
3942		if (interval == 0)
3943			break;
3944
3945		if (count != 0 && --count == 0)
3946			break;
3947
3948		(void) sleep(interval);
3949	}
3950
3951	return (0);
3952}
3953
3954typedef struct upgrade_cbdata {
3955	int	cb_all;
3956	int	cb_first;
3957	int	cb_newer;
3958	char	cb_poolname[ZPOOL_MAXNAMELEN];
3959	int	cb_argc;
3960	uint64_t cb_version;
3961	char	**cb_argv;
3962} upgrade_cbdata_t;
3963
3964static int
3965is_root_pool(zpool_handle_t *zhp)
3966{
3967	static struct statfs sfs;
3968	static char *poolname = NULL;
3969	static boolean_t stated = B_FALSE;
3970	char *slash;
3971
3972	if (!stated) {
3973		stated = B_TRUE;
3974		if (statfs("/", &sfs) == -1) {
3975			(void) fprintf(stderr,
3976			    "Unable to stat root file system: %s.\n",
3977			    strerror(errno));
3978			return (0);
3979		}
3980		if (strcmp(sfs.f_fstypename, "zfs") != 0)
3981			return (0);
3982		poolname = sfs.f_mntfromname;
3983		if ((slash = strchr(poolname, '/')) != NULL)
3984			*slash = '\0';
3985	}
3986	return (poolname != NULL && strcmp(poolname, zpool_get_name(zhp)) == 0);
3987}
3988
3989static int
3990upgrade_cb(zpool_handle_t *zhp, void *arg)
3991{
3992	upgrade_cbdata_t *cbp = arg;
3993	nvlist_t *config;
3994	uint64_t version;
3995	int ret = 0;
3996
3997	config = zpool_get_config(zhp, NULL);
3998	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
3999	    &version) == 0);
4000
4001	if (!cbp->cb_newer && version < SPA_VERSION) {
4002		if (!cbp->cb_all) {
4003			if (cbp->cb_first) {
4004				(void) printf(gettext("The following pools are "
4005				    "out of date, and can be upgraded.  After "
4006				    "being\nupgraded, these pools will no "
4007				    "longer be accessible by older software "
4008				    "versions.\n\n"));
4009				(void) printf(gettext("VER  POOL\n"));
4010				(void) printf(gettext("---  ------------\n"));
4011				cbp->cb_first = B_FALSE;
4012			}
4013
4014			(void) printf("%2llu   %s\n", (u_longlong_t)version,
4015			    zpool_get_name(zhp));
4016		} else {
4017			cbp->cb_first = B_FALSE;
4018			ret = zpool_upgrade(zhp, cbp->cb_version);
4019			if (!ret) {
4020				(void) printf(gettext("Successfully upgraded "
4021				    "'%s'\n\n"), zpool_get_name(zhp));
4022				if (cbp->cb_poolname[0] == '\0' &&
4023				    is_root_pool(zhp)) {
4024					(void) strlcpy(cbp->cb_poolname,
4025					    zpool_get_name(zhp),
4026					    sizeof(cbp->cb_poolname));
4027				}
4028			}
4029		}
4030	} else if (cbp->cb_newer && version > SPA_VERSION) {
4031		assert(!cbp->cb_all);
4032
4033		if (cbp->cb_first) {
4034			(void) printf(gettext("The following pools are "
4035			    "formatted using a newer software version and\n"
4036			    "cannot be accessed on the current system.\n\n"));
4037			(void) printf(gettext("VER  POOL\n"));
4038			(void) printf(gettext("---  ------------\n"));
4039			cbp->cb_first = B_FALSE;
4040		}
4041
4042		(void) printf("%2llu   %s\n", (u_longlong_t)version,
4043		    zpool_get_name(zhp));
4044	}
4045
4046	zpool_close(zhp);
4047	return (ret);
4048}
4049
4050/* ARGSUSED */
4051static int
4052upgrade_one(zpool_handle_t *zhp, void *data)
4053{
4054	upgrade_cbdata_t *cbp = data;
4055	uint64_t cur_version;
4056	int ret;
4057
4058	if (strcmp("log", zpool_get_name(zhp)) == 0) {
4059		(void) printf(gettext("'log' is now a reserved word\n"
4060		    "Pool 'log' must be renamed using export and import"
4061		    " to upgrade.\n"));
4062		return (1);
4063	}
4064
4065	cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4066	if (cur_version > cbp->cb_version) {
4067		(void) printf(gettext("Pool '%s' is already formatted "
4068		    "using more current version '%llu'.\n"),
4069		    zpool_get_name(zhp), cur_version);
4070		return (0);
4071	}
4072	if (cur_version == cbp->cb_version) {
4073		(void) printf(gettext("Pool '%s' is already formatted "
4074		    "using the current version.\n"), zpool_get_name(zhp));
4075		return (0);
4076	}
4077
4078	ret = zpool_upgrade(zhp, cbp->cb_version);
4079
4080	if (!ret) {
4081		(void) printf(gettext("Successfully upgraded '%s' "
4082		    "from version %llu to version %llu\n\n"),
4083		    zpool_get_name(zhp), (u_longlong_t)cur_version,
4084		    (u_longlong_t)cbp->cb_version);
4085		if (cbp->cb_poolname[0] == '\0' && is_root_pool(zhp)) {
4086			(void) strlcpy(cbp->cb_poolname, zpool_get_name(zhp),
4087			    sizeof(cbp->cb_poolname));
4088		}
4089	}
4090
4091	return (ret != 0);
4092}
4093
4094/*
4095 * zpool upgrade
4096 * zpool upgrade -v
4097 * zpool upgrade [-V version] <-a | pool ...>
4098 *
4099 * With no arguments, display downrev'd ZFS pool available for upgrade.
4100 * Individual pools can be upgraded by specifying the pool, and '-a' will
4101 * upgrade all pools.
4102 */
4103int
4104zpool_do_upgrade(int argc, char **argv)
4105{
4106	int c;
4107	upgrade_cbdata_t cb = { 0 };
4108	int ret = 0;
4109	boolean_t showversions = B_FALSE;
4110	char *end;
4111
4112
4113	/* check options */
4114	while ((c = getopt(argc, argv, ":avV:")) != -1) {
4115		switch (c) {
4116		case 'a':
4117			cb.cb_all = B_TRUE;
4118			break;
4119		case 'v':
4120			showversions = B_TRUE;
4121			break;
4122		case 'V':
4123			cb.cb_version = strtoll(optarg, &end, 10);
4124			if (*end != '\0' || cb.cb_version > SPA_VERSION ||
4125			    cb.cb_version < SPA_VERSION_1) {
4126				(void) fprintf(stderr,
4127				    gettext("invalid version '%s'\n"), optarg);
4128				usage(B_FALSE);
4129			}
4130			break;
4131		case ':':
4132			(void) fprintf(stderr, gettext("missing argument for "
4133			    "'%c' option\n"), optopt);
4134			usage(B_FALSE);
4135			break;
4136		case '?':
4137			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4138			    optopt);
4139			usage(B_FALSE);
4140		}
4141	}
4142
4143	cb.cb_argc = argc;
4144	cb.cb_argv = argv;
4145	argc -= optind;
4146	argv += optind;
4147
4148	if (cb.cb_version == 0) {
4149		cb.cb_version = SPA_VERSION;
4150	} else if (!cb.cb_all && argc == 0) {
4151		(void) fprintf(stderr, gettext("-V option is "
4152		    "incompatible with other arguments\n"));
4153		usage(B_FALSE);
4154	}
4155
4156	if (showversions) {
4157		if (cb.cb_all || argc != 0) {
4158			(void) fprintf(stderr, gettext("-v option is "
4159			    "incompatible with other arguments\n"));
4160			usage(B_FALSE);
4161		}
4162	} else if (cb.cb_all) {
4163		if (argc != 0) {
4164			(void) fprintf(stderr, gettext("-a option should not "
4165			    "be used along with a pool name\n"));
4166			usage(B_FALSE);
4167		}
4168	}
4169
4170	(void) printf(gettext("This system is currently running "
4171	    "ZFS pool version %llu.\n\n"), SPA_VERSION);
4172	cb.cb_first = B_TRUE;
4173	if (showversions) {
4174		(void) printf(gettext("The following versions are "
4175		    "supported:\n\n"));
4176		(void) printf(gettext("VER  DESCRIPTION\n"));
4177		(void) printf("---  -----------------------------------------"
4178		    "---------------\n");
4179		(void) printf(gettext(" 1   Initial ZFS version\n"));
4180		(void) printf(gettext(" 2   Ditto blocks "
4181		    "(replicated metadata)\n"));
4182		(void) printf(gettext(" 3   Hot spares and double parity "
4183		    "RAID-Z\n"));
4184		(void) printf(gettext(" 4   zpool history\n"));
4185		(void) printf(gettext(" 5   Compression using the gzip "
4186		    "algorithm\n"));
4187		(void) printf(gettext(" 6   bootfs pool property\n"));
4188		(void) printf(gettext(" 7   Separate intent log devices\n"));
4189		(void) printf(gettext(" 8   Delegated administration\n"));
4190		(void) printf(gettext(" 9   refquota and refreservation "
4191		    "properties\n"));
4192		(void) printf(gettext(" 10  Cache devices\n"));
4193		(void) printf(gettext(" 11  Improved scrub performance\n"));
4194		(void) printf(gettext(" 12  Snapshot properties\n"));
4195		(void) printf(gettext(" 13  snapused property\n"));
4196		(void) printf(gettext(" 14  passthrough-x aclinherit\n"));
4197		(void) printf(gettext(" 15  user/group space accounting\n"));
4198		(void) printf(gettext(" 16  stmf property support\n"));
4199		(void) printf(gettext(" 17  Triple-parity RAID-Z\n"));
4200		(void) printf(gettext(" 18  Snapshot user holds\n"));
4201		(void) printf(gettext(" 19  Log device removal\n"));
4202		(void) printf(gettext(" 20  Compression using zle "
4203		    "(zero-length encoding)\n"));
4204		(void) printf(gettext(" 21  Deduplication\n"));
4205		(void) printf(gettext(" 22  Received properties\n"));
4206		(void) printf(gettext(" 23  Slim ZIL\n"));
4207		(void) printf(gettext(" 24  System attributes\n"));
4208		(void) printf(gettext(" 25  Improved scrub stats\n"));
4209		(void) printf(gettext(" 26  Improved snapshot deletion "
4210		    "performance\n"));
4211		(void) printf(gettext(" 27  Improved snapshot creation "
4212		    "performance\n"));
4213		(void) printf(gettext(" 28  Multiple vdev replacements\n"));
4214		(void) printf(gettext("\nFor more information on a particular "
4215		    "version, including supported releases,\n"));
4216		(void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4217	} else if (argc == 0) {
4218		int notfound;
4219
4220		ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4221		notfound = cb.cb_first;
4222
4223		if (!cb.cb_all && ret == 0) {
4224			if (!cb.cb_first)
4225				(void) printf("\n");
4226			cb.cb_first = B_TRUE;
4227			cb.cb_newer = B_TRUE;
4228			ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4229			if (!cb.cb_first) {
4230				notfound = B_FALSE;
4231				(void) printf("\n");
4232			}
4233		}
4234
4235		if (ret == 0) {
4236			if (notfound)
4237				(void) printf(gettext("All pools are formatted "
4238				    "using this version.\n"));
4239			else if (!cb.cb_all)
4240				(void) printf(gettext("Use 'zpool upgrade -v' "
4241				    "for a list of available versions and "
4242				    "their associated\nfeatures.\n"));
4243		}
4244	} else {
4245		ret = for_each_pool(argc, argv, B_FALSE, NULL,
4246		    upgrade_one, &cb);
4247	}
4248
4249	if (cb.cb_poolname[0] != '\0') {
4250		(void) printf(
4251		    "If you boot from pool '%s', don't forget to update boot code.\n"
4252		    "Assuming you use GPT partitioning and da0 is your boot disk\n"
4253		    "the following command will do it:\n"
4254		    "\n"
4255		    "\tgpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 da0\n\n",
4256		    cb.cb_poolname);
4257	}
4258
4259	return (ret);
4260}
4261
4262typedef struct hist_cbdata {
4263	boolean_t first;
4264	int longfmt;
4265	int internal;
4266} hist_cbdata_t;
4267
4268/*
4269 * Print out the command history for a specific pool.
4270 */
4271static int
4272get_history_one(zpool_handle_t *zhp, void *data)
4273{
4274	nvlist_t *nvhis;
4275	nvlist_t **records;
4276	uint_t numrecords;
4277	char *cmdstr;
4278	char *pathstr;
4279	uint64_t dst_time;
4280	time_t tsec;
4281	struct tm t;
4282	char tbuf[30];
4283	int ret, i;
4284	uint64_t who;
4285	struct passwd *pwd;
4286	char *hostname;
4287	char *zonename;
4288	char internalstr[MAXPATHLEN];
4289	hist_cbdata_t *cb = (hist_cbdata_t *)data;
4290	uint64_t txg;
4291	uint64_t ievent;
4292
4293	cb->first = B_FALSE;
4294
4295	(void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
4296
4297	if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
4298		return (ret);
4299
4300	verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
4301	    &records, &numrecords) == 0);
4302	for (i = 0; i < numrecords; i++) {
4303		if (nvlist_lookup_uint64(records[i], ZPOOL_HIST_TIME,
4304		    &dst_time) != 0)
4305			continue;
4306
4307		/* is it an internal event or a standard event? */
4308		if (nvlist_lookup_string(records[i], ZPOOL_HIST_CMD,
4309		    &cmdstr) != 0) {
4310			if (cb->internal == 0)
4311				continue;
4312
4313			if (nvlist_lookup_uint64(records[i],
4314			    ZPOOL_HIST_INT_EVENT, &ievent) != 0)
4315				continue;
4316			verify(nvlist_lookup_uint64(records[i],
4317			    ZPOOL_HIST_TXG, &txg) == 0);
4318			verify(nvlist_lookup_string(records[i],
4319			    ZPOOL_HIST_INT_STR, &pathstr) == 0);
4320			if (ievent >= LOG_END)
4321				continue;
4322			(void) snprintf(internalstr,
4323			    sizeof (internalstr),
4324			    "[internal %s txg:%lld] %s",
4325			    zfs_history_event_names[ievent], txg,
4326			    pathstr);
4327			cmdstr = internalstr;
4328		}
4329		tsec = dst_time;
4330		(void) localtime_r(&tsec, &t);
4331		(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
4332		(void) printf("%s %s", tbuf, cmdstr);
4333
4334		if (!cb->longfmt) {
4335			(void) printf("\n");
4336			continue;
4337		}
4338		(void) printf(" [");
4339		if (nvlist_lookup_uint64(records[i],
4340		    ZPOOL_HIST_WHO, &who) == 0) {
4341			pwd = getpwuid((uid_t)who);
4342			if (pwd)
4343				(void) printf("user %s on",
4344				    pwd->pw_name);
4345			else
4346				(void) printf("user %d on",
4347				    (int)who);
4348		} else {
4349			(void) printf(gettext("no info]\n"));
4350			continue;
4351		}
4352		if (nvlist_lookup_string(records[i],
4353		    ZPOOL_HIST_HOST, &hostname) == 0) {
4354			(void) printf(" %s", hostname);
4355		}
4356		if (nvlist_lookup_string(records[i],
4357		    ZPOOL_HIST_ZONE, &zonename) == 0) {
4358			(void) printf(":%s", zonename);
4359		}
4360
4361		(void) printf("]");
4362		(void) printf("\n");
4363	}
4364	(void) printf("\n");
4365	nvlist_free(nvhis);
4366
4367	return (ret);
4368}
4369
4370/*
4371 * zpool history <pool>
4372 *
4373 * Displays the history of commands that modified pools.
4374 */
4375
4376
4377int
4378zpool_do_history(int argc, char **argv)
4379{
4380	hist_cbdata_t cbdata = { 0 };
4381	int ret;
4382	int c;
4383
4384	cbdata.first = B_TRUE;
4385	/* check options */
4386	while ((c = getopt(argc, argv, "li")) != -1) {
4387		switch (c) {
4388		case 'l':
4389			cbdata.longfmt = 1;
4390			break;
4391		case 'i':
4392			cbdata.internal = 1;
4393			break;
4394		case '?':
4395			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4396			    optopt);
4397			usage(B_FALSE);
4398		}
4399	}
4400	argc -= optind;
4401	argv += optind;
4402
4403	ret = for_each_pool(argc, argv, B_FALSE,  NULL, get_history_one,
4404	    &cbdata);
4405
4406	if (argc == 0 && cbdata.first == B_TRUE) {
4407		(void) printf(gettext("no pools available\n"));
4408		return (0);
4409	}
4410
4411	return (ret);
4412}
4413
4414static int
4415get_callback(zpool_handle_t *zhp, void *data)
4416{
4417	zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
4418	char value[MAXNAMELEN];
4419	zprop_source_t srctype;
4420	zprop_list_t *pl;
4421
4422	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
4423
4424		/*
4425		 * Skip the special fake placeholder. This will also skip
4426		 * over the name property when 'all' is specified.
4427		 */
4428		if (pl->pl_prop == ZPOOL_PROP_NAME &&
4429		    pl == cbp->cb_proplist)
4430			continue;
4431
4432		if (zpool_get_prop(zhp, pl->pl_prop,
4433		    value, sizeof (value), &srctype) != 0)
4434			continue;
4435
4436		zprop_print_one_property(zpool_get_name(zhp), cbp,
4437		    zpool_prop_to_name(pl->pl_prop), value, srctype, NULL,
4438		    NULL);
4439	}
4440	return (0);
4441}
4442
4443int
4444zpool_do_get(int argc, char **argv)
4445{
4446	zprop_get_cbdata_t cb = { 0 };
4447	zprop_list_t fake_name = { 0 };
4448	int ret;
4449
4450	if (argc < 3)
4451		usage(B_FALSE);
4452
4453	cb.cb_first = B_TRUE;
4454	cb.cb_sources = ZPROP_SRC_ALL;
4455	cb.cb_columns[0] = GET_COL_NAME;
4456	cb.cb_columns[1] = GET_COL_PROPERTY;
4457	cb.cb_columns[2] = GET_COL_VALUE;
4458	cb.cb_columns[3] = GET_COL_SOURCE;
4459	cb.cb_type = ZFS_TYPE_POOL;
4460
4461	if (zprop_get_list(g_zfs, argv[1],  &cb.cb_proplist,
4462	    ZFS_TYPE_POOL) != 0)
4463		usage(B_FALSE);
4464
4465	if (cb.cb_proplist != NULL) {
4466		fake_name.pl_prop = ZPOOL_PROP_NAME;
4467		fake_name.pl_width = strlen(gettext("NAME"));
4468		fake_name.pl_next = cb.cb_proplist;
4469		cb.cb_proplist = &fake_name;
4470	}
4471
4472	ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
4473	    get_callback, &cb);
4474
4475	if (cb.cb_proplist == &fake_name)
4476		zprop_free_list(fake_name.pl_next);
4477	else
4478		zprop_free_list(cb.cb_proplist);
4479
4480	return (ret);
4481}
4482
4483typedef struct set_cbdata {
4484	char *cb_propname;
4485	char *cb_value;
4486	boolean_t cb_any_successful;
4487} set_cbdata_t;
4488
4489int
4490set_callback(zpool_handle_t *zhp, void *data)
4491{
4492	int error;
4493	set_cbdata_t *cb = (set_cbdata_t *)data;
4494
4495	error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
4496
4497	if (!error)
4498		cb->cb_any_successful = B_TRUE;
4499
4500	return (error);
4501}
4502
4503int
4504zpool_do_set(int argc, char **argv)
4505{
4506	set_cbdata_t cb = { 0 };
4507	int error;
4508
4509	if (argc > 1 && argv[1][0] == '-') {
4510		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4511		    argv[1][1]);
4512		usage(B_FALSE);
4513	}
4514
4515	if (argc < 2) {
4516		(void) fprintf(stderr, gettext("missing property=value "
4517		    "argument\n"));
4518		usage(B_FALSE);
4519	}
4520
4521	if (argc < 3) {
4522		(void) fprintf(stderr, gettext("missing pool name\n"));
4523		usage(B_FALSE);
4524	}
4525
4526	if (argc > 3) {
4527		(void) fprintf(stderr, gettext("too many pool names\n"));
4528		usage(B_FALSE);
4529	}
4530
4531	cb.cb_propname = argv[1];
4532	cb.cb_value = strchr(cb.cb_propname, '=');
4533	if (cb.cb_value == NULL) {
4534		(void) fprintf(stderr, gettext("missing value in "
4535		    "property=value argument\n"));
4536		usage(B_FALSE);
4537	}
4538
4539	*(cb.cb_value) = '\0';
4540	cb.cb_value++;
4541
4542	error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
4543	    set_callback, &cb);
4544
4545	return (error);
4546}
4547
4548static int
4549find_command_idx(char *command, int *idx)
4550{
4551	int i;
4552
4553	for (i = 0; i < NCOMMAND; i++) {
4554		if (command_table[i].name == NULL)
4555			continue;
4556
4557		if (strcmp(command, command_table[i].name) == 0) {
4558			*idx = i;
4559			return (0);
4560		}
4561	}
4562	return (1);
4563}
4564
4565int
4566main(int argc, char **argv)
4567{
4568	int ret;
4569	int i;
4570	char *cmdname;
4571
4572	(void) setlocale(LC_ALL, "");
4573	(void) textdomain(TEXT_DOMAIN);
4574
4575	if ((g_zfs = libzfs_init()) == NULL) {
4576		(void) fprintf(stderr, gettext("internal error: failed to "
4577		    "initialize ZFS library\n"));
4578		return (1);
4579	}
4580
4581	libzfs_print_on_error(g_zfs, B_TRUE);
4582
4583	opterr = 0;
4584
4585	/*
4586	 * Make sure the user has specified some command.
4587	 */
4588	if (argc < 2) {
4589		(void) fprintf(stderr, gettext("missing command\n"));
4590		usage(B_FALSE);
4591	}
4592
4593	cmdname = argv[1];
4594
4595	/*
4596	 * Special case '-?'
4597	 */
4598	if (strcmp(cmdname, "-?") == 0)
4599		usage(B_TRUE);
4600
4601	zpool_set_history_str("zpool", argc, argv, history_str);
4602	verify(zpool_stage_history(g_zfs, history_str) == 0);
4603
4604	/*
4605	 * Run the appropriate command.
4606	 */
4607	if (find_command_idx(cmdname, &i) == 0) {
4608		current_command = &command_table[i];
4609		ret = command_table[i].func(argc - 1, argv + 1);
4610	} else if (strchr(cmdname, '=')) {
4611		verify(find_command_idx("set", &i) == 0);
4612		current_command = &command_table[i];
4613		ret = command_table[i].func(argc, argv);
4614	} else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
4615		/*
4616		 * 'freeze' is a vile debugging abomination, so we treat
4617		 * it as such.
4618		 */
4619		char buf[16384];
4620		int fd = open(ZFS_DEV, O_RDWR);
4621		(void) strcpy((void *)buf, argv[2]);
4622		return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
4623	} else {
4624		(void) fprintf(stderr, gettext("unrecognized "
4625		    "command '%s'\n"), cmdname);
4626		usage(B_FALSE);
4627	}
4628
4629	libzfs_fini(g_zfs);
4630
4631	/*
4632	 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4633	 * for the purposes of running ::findleaks.
4634	 */
4635	if (getenv("ZFS_ABORT") != NULL) {
4636		(void) printf("dumping core by request\n");
4637		abort();
4638	}
4639
4640	return (ret);
4641}
4642