xref: /titanic_41/usr/src/lib/libzfs/common/libzfs_pool.c (revision 35ea52c6e821fbaae843db51babd7cd248e86bf9)
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 <ctype.h>
27 #include <errno.h>
28 #include <devid.h>
29 #include <fcntl.h>
30 #include <libintl.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <strings.h>
34 #include <unistd.h>
35 #include <sys/efi_partition.h>
36 #include <sys/vtoc.h>
37 #include <sys/zfs_ioctl.h>
38 #include <dlfcn.h>
39 
40 #include "zfs_namecheck.h"
41 #include "zfs_prop.h"
42 #include "libzfs_impl.h"
43 #include "zfs_comutil.h"
44 
45 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
46 
47 #if defined(__i386) || defined(__amd64)
48 #define	BOOTCMD	"installgrub(1M)"
49 #else
50 #define	BOOTCMD	"installboot(1M)"
51 #endif
52 
53 #define	DISK_ROOT	"/dev/dsk"
54 #define	RDISK_ROOT	"/dev/rdsk"
55 #define	BACKUP_SLICE	"s2"
56 
57 /*
58  * ====================================================================
59  *   zpool property functions
60  * ====================================================================
61  */
62 
63 static int
64 zpool_get_all_props(zpool_handle_t *zhp)
65 {
66 	zfs_cmd_t zc = { 0 };
67 	libzfs_handle_t *hdl = zhp->zpool_hdl;
68 
69 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
70 
71 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
72 		return (-1);
73 
74 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
75 		if (errno == ENOMEM) {
76 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
77 				zcmd_free_nvlists(&zc);
78 				return (-1);
79 			}
80 		} else {
81 			zcmd_free_nvlists(&zc);
82 			return (-1);
83 		}
84 	}
85 
86 	if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
87 		zcmd_free_nvlists(&zc);
88 		return (-1);
89 	}
90 
91 	zcmd_free_nvlists(&zc);
92 
93 	return (0);
94 }
95 
96 static int
97 zpool_props_refresh(zpool_handle_t *zhp)
98 {
99 	nvlist_t *old_props;
100 
101 	old_props = zhp->zpool_props;
102 
103 	if (zpool_get_all_props(zhp) != 0)
104 		return (-1);
105 
106 	nvlist_free(old_props);
107 	return (0);
108 }
109 
110 static char *
111 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
112     zprop_source_t *src)
113 {
114 	nvlist_t *nv, *nvl;
115 	uint64_t ival;
116 	char *value;
117 	zprop_source_t source;
118 
119 	nvl = zhp->zpool_props;
120 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
121 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
122 		source = ival;
123 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
124 	} else {
125 		source = ZPROP_SRC_DEFAULT;
126 		if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
127 			value = "-";
128 	}
129 
130 	if (src)
131 		*src = source;
132 
133 	return (value);
134 }
135 
136 uint64_t
137 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
138 {
139 	nvlist_t *nv, *nvl;
140 	uint64_t value;
141 	zprop_source_t source;
142 
143 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
144 		/*
145 		 * zpool_get_all_props() has most likely failed because
146 		 * the pool is faulted, but if all we need is the top level
147 		 * vdev's guid then get it from the zhp config nvlist.
148 		 */
149 		if ((prop == ZPOOL_PROP_GUID) &&
150 		    (nvlist_lookup_nvlist(zhp->zpool_config,
151 		    ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
152 		    (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
153 		    == 0)) {
154 			return (value);
155 		}
156 		return (zpool_prop_default_numeric(prop));
157 	}
158 
159 	nvl = zhp->zpool_props;
160 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
161 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
162 		source = value;
163 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
164 	} else {
165 		source = ZPROP_SRC_DEFAULT;
166 		value = zpool_prop_default_numeric(prop);
167 	}
168 
169 	if (src)
170 		*src = source;
171 
172 	return (value);
173 }
174 
175 /*
176  * Map VDEV STATE to printed strings.
177  */
178 char *
179 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
180 {
181 	switch (state) {
182 	case VDEV_STATE_CLOSED:
183 	case VDEV_STATE_OFFLINE:
184 		return (gettext("OFFLINE"));
185 	case VDEV_STATE_REMOVED:
186 		return (gettext("REMOVED"));
187 	case VDEV_STATE_CANT_OPEN:
188 		if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
189 			return (gettext("FAULTED"));
190 		else if (aux == VDEV_AUX_SPLIT_POOL)
191 			return (gettext("SPLIT"));
192 		else
193 			return (gettext("UNAVAIL"));
194 	case VDEV_STATE_FAULTED:
195 		return (gettext("FAULTED"));
196 	case VDEV_STATE_DEGRADED:
197 		return (gettext("DEGRADED"));
198 	case VDEV_STATE_HEALTHY:
199 		return (gettext("ONLINE"));
200 	}
201 
202 	return (gettext("UNKNOWN"));
203 }
204 
205 /*
206  * Get a zpool property value for 'prop' and return the value in
207  * a pre-allocated buffer.
208  */
209 int
210 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
211     zprop_source_t *srctype)
212 {
213 	uint64_t intval;
214 	const char *strval;
215 	zprop_source_t src = ZPROP_SRC_NONE;
216 	nvlist_t *nvroot;
217 	vdev_stat_t *vs;
218 	uint_t vsc;
219 
220 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
221 		switch (prop) {
222 		case ZPOOL_PROP_NAME:
223 			(void) strlcpy(buf, zpool_get_name(zhp), len);
224 			break;
225 
226 		case ZPOOL_PROP_HEALTH:
227 			(void) strlcpy(buf, "FAULTED", len);
228 			break;
229 
230 		case ZPOOL_PROP_GUID:
231 			intval = zpool_get_prop_int(zhp, prop, &src);
232 			(void) snprintf(buf, len, "%llu", intval);
233 			break;
234 
235 		case ZPOOL_PROP_ALTROOT:
236 		case ZPOOL_PROP_CACHEFILE:
237 			if (zhp->zpool_props != NULL ||
238 			    zpool_get_all_props(zhp) == 0) {
239 				(void) strlcpy(buf,
240 				    zpool_get_prop_string(zhp, prop, &src),
241 				    len);
242 				if (srctype != NULL)
243 					*srctype = src;
244 				return (0);
245 			}
246 			/* FALLTHROUGH */
247 		default:
248 			(void) strlcpy(buf, "-", len);
249 			break;
250 		}
251 
252 		if (srctype != NULL)
253 			*srctype = src;
254 		return (0);
255 	}
256 
257 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
258 	    prop != ZPOOL_PROP_NAME)
259 		return (-1);
260 
261 	switch (zpool_prop_get_type(prop)) {
262 	case PROP_TYPE_STRING:
263 		(void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
264 		    len);
265 		break;
266 
267 	case PROP_TYPE_NUMBER:
268 		intval = zpool_get_prop_int(zhp, prop, &src);
269 
270 		switch (prop) {
271 		case ZPOOL_PROP_SIZE:
272 		case ZPOOL_PROP_ALLOCATED:
273 		case ZPOOL_PROP_FREE:
274 			(void) zfs_nicenum(intval, buf, len);
275 			break;
276 
277 		case ZPOOL_PROP_CAPACITY:
278 			(void) snprintf(buf, len, "%llu%%",
279 			    (u_longlong_t)intval);
280 			break;
281 
282 		case ZPOOL_PROP_DEDUPRATIO:
283 			(void) snprintf(buf, len, "%llu.%02llux",
284 			    (u_longlong_t)(intval / 100),
285 			    (u_longlong_t)(intval % 100));
286 			break;
287 
288 		case ZPOOL_PROP_HEALTH:
289 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
290 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
291 			verify(nvlist_lookup_uint64_array(nvroot,
292 			    ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
293 			    == 0);
294 
295 			(void) strlcpy(buf, zpool_state_to_name(intval,
296 			    vs->vs_aux), len);
297 			break;
298 		default:
299 			(void) snprintf(buf, len, "%llu", intval);
300 		}
301 		break;
302 
303 	case PROP_TYPE_INDEX:
304 		intval = zpool_get_prop_int(zhp, prop, &src);
305 		if (zpool_prop_index_to_string(prop, intval, &strval)
306 		    != 0)
307 			return (-1);
308 		(void) strlcpy(buf, strval, len);
309 		break;
310 
311 	default:
312 		abort();
313 	}
314 
315 	if (srctype)
316 		*srctype = src;
317 
318 	return (0);
319 }
320 
321 /*
322  * Check if the bootfs name has the same pool name as it is set to.
323  * Assuming bootfs is a valid dataset name.
324  */
325 static boolean_t
326 bootfs_name_valid(const char *pool, char *bootfs)
327 {
328 	int len = strlen(pool);
329 
330 	if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
331 		return (B_FALSE);
332 
333 	if (strncmp(pool, bootfs, len) == 0 &&
334 	    (bootfs[len] == '/' || bootfs[len] == '\0'))
335 		return (B_TRUE);
336 
337 	return (B_FALSE);
338 }
339 
340 /*
341  * Inspect the configuration to determine if any of the devices contain
342  * an EFI label.
343  */
344 static boolean_t
345 pool_uses_efi(nvlist_t *config)
346 {
347 	nvlist_t **child;
348 	uint_t c, children;
349 
350 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
351 	    &child, &children) != 0)
352 		return (read_efi_label(config, NULL) >= 0);
353 
354 	for (c = 0; c < children; c++) {
355 		if (pool_uses_efi(child[c]))
356 			return (B_TRUE);
357 	}
358 	return (B_FALSE);
359 }
360 
361 static boolean_t
362 pool_is_bootable(zpool_handle_t *zhp)
363 {
364 	char bootfs[ZPOOL_MAXNAMELEN];
365 
366 	return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
367 	    sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
368 	    sizeof (bootfs)) != 0);
369 }
370 
371 
372 /*
373  * Given an nvlist of zpool properties to be set, validate that they are
374  * correct, and parse any numeric properties (index, boolean, etc) if they are
375  * specified as strings.
376  */
377 static nvlist_t *
378 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
379     nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf)
380 {
381 	nvpair_t *elem;
382 	nvlist_t *retprops;
383 	zpool_prop_t prop;
384 	char *strval;
385 	uint64_t intval;
386 	char *slash;
387 	struct stat64 statbuf;
388 	zpool_handle_t *zhp;
389 	nvlist_t *nvroot;
390 
391 	if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
392 		(void) no_memory(hdl);
393 		return (NULL);
394 	}
395 
396 	elem = NULL;
397 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
398 		const char *propname = nvpair_name(elem);
399 
400 		/*
401 		 * Make sure this property is valid and applies to this type.
402 		 */
403 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
404 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
405 			    "invalid property '%s'"), propname);
406 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
407 			goto error;
408 		}
409 
410 		if (zpool_prop_readonly(prop)) {
411 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
412 			    "is readonly"), propname);
413 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
414 			goto error;
415 		}
416 
417 		if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
418 		    &strval, &intval, errbuf) != 0)
419 			goto error;
420 
421 		/*
422 		 * Perform additional checking for specific properties.
423 		 */
424 		switch (prop) {
425 		case ZPOOL_PROP_VERSION:
426 			if (intval < version || intval > SPA_VERSION) {
427 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
428 				    "property '%s' number %d is invalid."),
429 				    propname, intval);
430 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
431 				goto error;
432 			}
433 			break;
434 
435 		case ZPOOL_PROP_BOOTFS:
436 			if (create_or_import) {
437 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
438 				    "property '%s' cannot be set at creation "
439 				    "or import time"), propname);
440 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
441 				goto error;
442 			}
443 
444 			if (version < SPA_VERSION_BOOTFS) {
445 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
446 				    "pool must be upgraded to support "
447 				    "'%s' property"), propname);
448 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
449 				goto error;
450 			}
451 
452 			/*
453 			 * bootfs property value has to be a dataset name and
454 			 * the dataset has to be in the same pool as it sets to.
455 			 */
456 			if (strval[0] != '\0' && !bootfs_name_valid(poolname,
457 			    strval)) {
458 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
459 				    "is an invalid name"), strval);
460 				(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
461 				goto error;
462 			}
463 
464 			if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
465 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
466 				    "could not open pool '%s'"), poolname);
467 				(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
468 				goto error;
469 			}
470 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
471 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
472 
473 			/*
474 			 * bootfs property cannot be set on a disk which has
475 			 * been EFI labeled.
476 			 */
477 			if (pool_uses_efi(nvroot)) {
478 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
479 				    "property '%s' not supported on "
480 				    "EFI labeled devices"), propname);
481 				(void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
482 				zpool_close(zhp);
483 				goto error;
484 			}
485 			zpool_close(zhp);
486 			break;
487 
488 		case ZPOOL_PROP_ALTROOT:
489 			if (!create_or_import) {
490 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
491 				    "property '%s' can only be set during pool "
492 				    "creation or import"), propname);
493 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
494 				goto error;
495 			}
496 
497 			if (strval[0] != '/') {
498 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
499 				    "bad alternate root '%s'"), strval);
500 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
501 				goto error;
502 			}
503 			break;
504 
505 		case ZPOOL_PROP_CACHEFILE:
506 			if (strval[0] == '\0')
507 				break;
508 
509 			if (strcmp(strval, "none") == 0)
510 				break;
511 
512 			if (strval[0] != '/') {
513 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
514 				    "property '%s' must be empty, an "
515 				    "absolute path, or 'none'"), propname);
516 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
517 				goto error;
518 			}
519 
520 			slash = strrchr(strval, '/');
521 
522 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
523 			    strcmp(slash, "/..") == 0) {
524 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
525 				    "'%s' is not a valid file"), strval);
526 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
527 				goto error;
528 			}
529 
530 			*slash = '\0';
531 
532 			if (strval[0] != '\0' &&
533 			    (stat64(strval, &statbuf) != 0 ||
534 			    !S_ISDIR(statbuf.st_mode))) {
535 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
536 				    "'%s' is not a valid directory"),
537 				    strval);
538 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
539 				goto error;
540 			}
541 
542 			*slash = '/';
543 			break;
544 		}
545 	}
546 
547 	return (retprops);
548 error:
549 	nvlist_free(retprops);
550 	return (NULL);
551 }
552 
553 /*
554  * Set zpool property : propname=propval.
555  */
556 int
557 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
558 {
559 	zfs_cmd_t zc = { 0 };
560 	int ret = -1;
561 	char errbuf[1024];
562 	nvlist_t *nvl = NULL;
563 	nvlist_t *realprops;
564 	uint64_t version;
565 
566 	(void) snprintf(errbuf, sizeof (errbuf),
567 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
568 	    zhp->zpool_name);
569 
570 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
571 		return (no_memory(zhp->zpool_hdl));
572 
573 	if (nvlist_add_string(nvl, propname, propval) != 0) {
574 		nvlist_free(nvl);
575 		return (no_memory(zhp->zpool_hdl));
576 	}
577 
578 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
579 	if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
580 	    zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) {
581 		nvlist_free(nvl);
582 		return (-1);
583 	}
584 
585 	nvlist_free(nvl);
586 	nvl = realprops;
587 
588 	/*
589 	 * Execute the corresponding ioctl() to set this property.
590 	 */
591 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
592 
593 	if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
594 		nvlist_free(nvl);
595 		return (-1);
596 	}
597 
598 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
599 
600 	zcmd_free_nvlists(&zc);
601 	nvlist_free(nvl);
602 
603 	if (ret)
604 		(void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
605 	else
606 		(void) zpool_props_refresh(zhp);
607 
608 	return (ret);
609 }
610 
611 int
612 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
613 {
614 	libzfs_handle_t *hdl = zhp->zpool_hdl;
615 	zprop_list_t *entry;
616 	char buf[ZFS_MAXPROPLEN];
617 
618 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
619 		return (-1);
620 
621 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
622 
623 		if (entry->pl_fixed)
624 			continue;
625 
626 		if (entry->pl_prop != ZPROP_INVAL &&
627 		    zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
628 		    NULL) == 0) {
629 			if (strlen(buf) > entry->pl_width)
630 				entry->pl_width = strlen(buf);
631 		}
632 	}
633 
634 	return (0);
635 }
636 
637 
638 /*
639  * Don't start the slice at the default block of 34; many storage
640  * devices will use a stripe width of 128k, so start there instead.
641  */
642 #define	NEW_START_BLOCK	256
643 
644 /*
645  * Validate the given pool name, optionally putting an extended error message in
646  * 'buf'.
647  */
648 boolean_t
649 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
650 {
651 	namecheck_err_t why;
652 	char what;
653 	int ret;
654 
655 	ret = pool_namecheck(pool, &why, &what);
656 
657 	/*
658 	 * The rules for reserved pool names were extended at a later point.
659 	 * But we need to support users with existing pools that may now be
660 	 * invalid.  So we only check for this expanded set of names during a
661 	 * create (or import), and only in userland.
662 	 */
663 	if (ret == 0 && !isopen &&
664 	    (strncmp(pool, "mirror", 6) == 0 ||
665 	    strncmp(pool, "raidz", 5) == 0 ||
666 	    strncmp(pool, "spare", 5) == 0 ||
667 	    strcmp(pool, "log") == 0)) {
668 		if (hdl != NULL)
669 			zfs_error_aux(hdl,
670 			    dgettext(TEXT_DOMAIN, "name is reserved"));
671 		return (B_FALSE);
672 	}
673 
674 
675 	if (ret != 0) {
676 		if (hdl != NULL) {
677 			switch (why) {
678 			case NAME_ERR_TOOLONG:
679 				zfs_error_aux(hdl,
680 				    dgettext(TEXT_DOMAIN, "name is too long"));
681 				break;
682 
683 			case NAME_ERR_INVALCHAR:
684 				zfs_error_aux(hdl,
685 				    dgettext(TEXT_DOMAIN, "invalid character "
686 				    "'%c' in pool name"), what);
687 				break;
688 
689 			case NAME_ERR_NOLETTER:
690 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
691 				    "name must begin with a letter"));
692 				break;
693 
694 			case NAME_ERR_RESERVED:
695 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
696 				    "name is reserved"));
697 				break;
698 
699 			case NAME_ERR_DISKLIKE:
700 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
701 				    "pool name is reserved"));
702 				break;
703 
704 			case NAME_ERR_LEADING_SLASH:
705 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
706 				    "leading slash in name"));
707 				break;
708 
709 			case NAME_ERR_EMPTY_COMPONENT:
710 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
711 				    "empty component in name"));
712 				break;
713 
714 			case NAME_ERR_TRAILING_SLASH:
715 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
716 				    "trailing slash in name"));
717 				break;
718 
719 			case NAME_ERR_MULTIPLE_AT:
720 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
721 				    "multiple '@' delimiters in name"));
722 				break;
723 
724 			}
725 		}
726 		return (B_FALSE);
727 	}
728 
729 	return (B_TRUE);
730 }
731 
732 /*
733  * Open a handle to the given pool, even if the pool is currently in the FAULTED
734  * state.
735  */
736 zpool_handle_t *
737 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
738 {
739 	zpool_handle_t *zhp;
740 	boolean_t missing;
741 
742 	/*
743 	 * Make sure the pool name is valid.
744 	 */
745 	if (!zpool_name_valid(hdl, B_TRUE, pool)) {
746 		(void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
747 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
748 		    pool);
749 		return (NULL);
750 	}
751 
752 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
753 		return (NULL);
754 
755 	zhp->zpool_hdl = hdl;
756 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
757 
758 	if (zpool_refresh_stats(zhp, &missing) != 0) {
759 		zpool_close(zhp);
760 		return (NULL);
761 	}
762 
763 	if (missing) {
764 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
765 		(void) zfs_error_fmt(hdl, EZFS_NOENT,
766 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
767 		zpool_close(zhp);
768 		return (NULL);
769 	}
770 
771 	return (zhp);
772 }
773 
774 /*
775  * Like the above, but silent on error.  Used when iterating over pools (because
776  * the configuration cache may be out of date).
777  */
778 int
779 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
780 {
781 	zpool_handle_t *zhp;
782 	boolean_t missing;
783 
784 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
785 		return (-1);
786 
787 	zhp->zpool_hdl = hdl;
788 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
789 
790 	if (zpool_refresh_stats(zhp, &missing) != 0) {
791 		zpool_close(zhp);
792 		return (-1);
793 	}
794 
795 	if (missing) {
796 		zpool_close(zhp);
797 		*ret = NULL;
798 		return (0);
799 	}
800 
801 	*ret = zhp;
802 	return (0);
803 }
804 
805 /*
806  * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
807  * state.
808  */
809 zpool_handle_t *
810 zpool_open(libzfs_handle_t *hdl, const char *pool)
811 {
812 	zpool_handle_t *zhp;
813 
814 	if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
815 		return (NULL);
816 
817 	if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
818 		(void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
819 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
820 		zpool_close(zhp);
821 		return (NULL);
822 	}
823 
824 	return (zhp);
825 }
826 
827 /*
828  * Close the handle.  Simply frees the memory associated with the handle.
829  */
830 void
831 zpool_close(zpool_handle_t *zhp)
832 {
833 	if (zhp->zpool_config)
834 		nvlist_free(zhp->zpool_config);
835 	if (zhp->zpool_old_config)
836 		nvlist_free(zhp->zpool_old_config);
837 	if (zhp->zpool_props)
838 		nvlist_free(zhp->zpool_props);
839 	free(zhp);
840 }
841 
842 /*
843  * Return the name of the pool.
844  */
845 const char *
846 zpool_get_name(zpool_handle_t *zhp)
847 {
848 	return (zhp->zpool_name);
849 }
850 
851 
852 /*
853  * Return the state of the pool (ACTIVE or UNAVAILABLE)
854  */
855 int
856 zpool_get_state(zpool_handle_t *zhp)
857 {
858 	return (zhp->zpool_state);
859 }
860 
861 /*
862  * Create the named pool, using the provided vdev list.  It is assumed
863  * that the consumer has already validated the contents of the nvlist, so we
864  * don't have to worry about error semantics.
865  */
866 int
867 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
868     nvlist_t *props, nvlist_t *fsprops)
869 {
870 	zfs_cmd_t zc = { 0 };
871 	nvlist_t *zc_fsprops = NULL;
872 	nvlist_t *zc_props = NULL;
873 	char msg[1024];
874 	char *altroot;
875 	int ret = -1;
876 
877 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
878 	    "cannot create '%s'"), pool);
879 
880 	if (!zpool_name_valid(hdl, B_FALSE, pool))
881 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
882 
883 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
884 		return (-1);
885 
886 	if (props) {
887 		if ((zc_props = zpool_valid_proplist(hdl, pool, props,
888 		    SPA_VERSION_1, B_TRUE, msg)) == NULL) {
889 			goto create_failed;
890 		}
891 	}
892 
893 	if (fsprops) {
894 		uint64_t zoned;
895 		char *zonestr;
896 
897 		zoned = ((nvlist_lookup_string(fsprops,
898 		    zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
899 		    strcmp(zonestr, "on") == 0);
900 
901 		if ((zc_fsprops = zfs_valid_proplist(hdl,
902 		    ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
903 			goto create_failed;
904 		}
905 		if (!zc_props &&
906 		    (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
907 			goto create_failed;
908 		}
909 		if (nvlist_add_nvlist(zc_props,
910 		    ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
911 			goto create_failed;
912 		}
913 	}
914 
915 	if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
916 		goto create_failed;
917 
918 	(void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
919 
920 	if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
921 
922 		zcmd_free_nvlists(&zc);
923 		nvlist_free(zc_props);
924 		nvlist_free(zc_fsprops);
925 
926 		switch (errno) {
927 		case EBUSY:
928 			/*
929 			 * This can happen if the user has specified the same
930 			 * device multiple times.  We can't reliably detect this
931 			 * until we try to add it and see we already have a
932 			 * label.
933 			 */
934 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
935 			    "one or more vdevs refer to the same device"));
936 			return (zfs_error(hdl, EZFS_BADDEV, msg));
937 
938 		case EOVERFLOW:
939 			/*
940 			 * This occurs when one of the devices is below
941 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
942 			 * device was the problem device since there's no
943 			 * reliable way to determine device size from userland.
944 			 */
945 			{
946 				char buf[64];
947 
948 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
949 
950 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
951 				    "one or more devices is less than the "
952 				    "minimum size (%s)"), buf);
953 			}
954 			return (zfs_error(hdl, EZFS_BADDEV, msg));
955 
956 		case ENOSPC:
957 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
958 			    "one or more devices is out of space"));
959 			return (zfs_error(hdl, EZFS_BADDEV, msg));
960 
961 		case ENOTBLK:
962 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
963 			    "cache device must be a disk or disk slice"));
964 			return (zfs_error(hdl, EZFS_BADDEV, msg));
965 
966 		default:
967 			return (zpool_standard_error(hdl, errno, msg));
968 		}
969 	}
970 
971 	/*
972 	 * If this is an alternate root pool, then we automatically set the
973 	 * mountpoint of the root dataset to be '/'.
974 	 */
975 	if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
976 	    &altroot) == 0) {
977 		zfs_handle_t *zhp;
978 
979 		verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
980 		verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
981 		    "/") == 0);
982 
983 		zfs_close(zhp);
984 	}
985 
986 create_failed:
987 	zcmd_free_nvlists(&zc);
988 	nvlist_free(zc_props);
989 	nvlist_free(zc_fsprops);
990 	return (ret);
991 }
992 
993 /*
994  * Destroy the given pool.  It is up to the caller to ensure that there are no
995  * datasets left in the pool.
996  */
997 int
998 zpool_destroy(zpool_handle_t *zhp)
999 {
1000 	zfs_cmd_t zc = { 0 };
1001 	zfs_handle_t *zfp = NULL;
1002 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1003 	char msg[1024];
1004 
1005 	if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1006 	    (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name,
1007 	    ZFS_TYPE_FILESYSTEM)) == NULL)
1008 		return (-1);
1009 
1010 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1011 
1012 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1013 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1014 		    "cannot destroy '%s'"), zhp->zpool_name);
1015 
1016 		if (errno == EROFS) {
1017 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1018 			    "one or more devices is read only"));
1019 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1020 		} else {
1021 			(void) zpool_standard_error(hdl, errno, msg);
1022 		}
1023 
1024 		if (zfp)
1025 			zfs_close(zfp);
1026 		return (-1);
1027 	}
1028 
1029 	if (zfp) {
1030 		remove_mountpoint(zfp);
1031 		zfs_close(zfp);
1032 	}
1033 
1034 	return (0);
1035 }
1036 
1037 /*
1038  * Add the given vdevs to the pool.  The caller must have already performed the
1039  * necessary verification to ensure that the vdev specification is well-formed.
1040  */
1041 int
1042 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1043 {
1044 	zfs_cmd_t zc = { 0 };
1045 	int ret;
1046 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1047 	char msg[1024];
1048 	nvlist_t **spares, **l2cache;
1049 	uint_t nspares, nl2cache;
1050 
1051 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1052 	    "cannot add to '%s'"), zhp->zpool_name);
1053 
1054 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1055 	    SPA_VERSION_SPARES &&
1056 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1057 	    &spares, &nspares) == 0) {
1058 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1059 		    "upgraded to add hot spares"));
1060 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
1061 	}
1062 
1063 	if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
1064 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
1065 		uint64_t s;
1066 
1067 		for (s = 0; s < nspares; s++) {
1068 			char *path;
1069 
1070 			if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
1071 			    &path) == 0 && pool_uses_efi(spares[s])) {
1072 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1073 				    "device '%s' contains an EFI label and "
1074 				    "cannot be used on root pools."),
1075 				    zpool_vdev_name(hdl, NULL, spares[s],
1076 				    B_FALSE));
1077 				return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1078 			}
1079 		}
1080 	}
1081 
1082 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1083 	    SPA_VERSION_L2CACHE &&
1084 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1085 	    &l2cache, &nl2cache) == 0) {
1086 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1087 		    "upgraded to add cache devices"));
1088 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
1089 	}
1090 
1091 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1092 		return (-1);
1093 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1094 
1095 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1096 		switch (errno) {
1097 		case EBUSY:
1098 			/*
1099 			 * This can happen if the user has specified the same
1100 			 * device multiple times.  We can't reliably detect this
1101 			 * until we try to add it and see we already have a
1102 			 * label.
1103 			 */
1104 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1105 			    "one or more vdevs refer to the same device"));
1106 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1107 			break;
1108 
1109 		case EOVERFLOW:
1110 			/*
1111 			 * This occurrs when one of the devices is below
1112 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1113 			 * device was the problem device since there's no
1114 			 * reliable way to determine device size from userland.
1115 			 */
1116 			{
1117 				char buf[64];
1118 
1119 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1120 
1121 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1122 				    "device is less than the minimum "
1123 				    "size (%s)"), buf);
1124 			}
1125 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1126 			break;
1127 
1128 		case ENOTSUP:
1129 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1130 			    "pool must be upgraded to add these vdevs"));
1131 			(void) zfs_error(hdl, EZFS_BADVERSION, msg);
1132 			break;
1133 
1134 		case EDOM:
1135 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1136 			    "root pool can not have multiple vdevs"
1137 			    " or separate logs"));
1138 			(void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1139 			break;
1140 
1141 		case ENOTBLK:
1142 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1143 			    "cache device must be a disk or disk slice"));
1144 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1145 			break;
1146 
1147 		default:
1148 			(void) zpool_standard_error(hdl, errno, msg);
1149 		}
1150 
1151 		ret = -1;
1152 	} else {
1153 		ret = 0;
1154 	}
1155 
1156 	zcmd_free_nvlists(&zc);
1157 
1158 	return (ret);
1159 }
1160 
1161 /*
1162  * Exports the pool from the system.  The caller must ensure that there are no
1163  * mounted datasets in the pool.
1164  */
1165 int
1166 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce)
1167 {
1168 	zfs_cmd_t zc = { 0 };
1169 	char msg[1024];
1170 
1171 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1172 	    "cannot export '%s'"), zhp->zpool_name);
1173 
1174 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1175 	zc.zc_cookie = force;
1176 	zc.zc_guid = hardforce;
1177 
1178 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1179 		switch (errno) {
1180 		case EXDEV:
1181 			zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1182 			    "use '-f' to override the following errors:\n"
1183 			    "'%s' has an active shared spare which could be"
1184 			    " used by other pools once '%s' is exported."),
1185 			    zhp->zpool_name, zhp->zpool_name);
1186 			return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1187 			    msg));
1188 		default:
1189 			return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1190 			    msg));
1191 		}
1192 	}
1193 
1194 	return (0);
1195 }
1196 
1197 int
1198 zpool_export(zpool_handle_t *zhp, boolean_t force)
1199 {
1200 	return (zpool_export_common(zhp, force, B_FALSE));
1201 }
1202 
1203 int
1204 zpool_export_force(zpool_handle_t *zhp)
1205 {
1206 	return (zpool_export_common(zhp, B_TRUE, B_TRUE));
1207 }
1208 
1209 static void
1210 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1211     nvlist_t *rbi)
1212 {
1213 	uint64_t rewindto;
1214 	int64_t loss = -1;
1215 	struct tm t;
1216 	char timestr[128];
1217 
1218 	if (!hdl->libzfs_printerr || rbi == NULL)
1219 		return;
1220 
1221 	if (nvlist_lookup_uint64(rbi, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1222 		return;
1223 	(void) nvlist_lookup_int64(rbi, ZPOOL_CONFIG_REWIND_TIME, &loss);
1224 
1225 	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1226 	    strftime(timestr, 128, 0, &t) != 0) {
1227 		if (dryrun) {
1228 			(void) printf(dgettext(TEXT_DOMAIN,
1229 			    "Would be able to return %s "
1230 			    "to its state as of %s.\n"),
1231 			    name, timestr);
1232 		} else {
1233 			(void) printf(dgettext(TEXT_DOMAIN,
1234 			    "Pool %s returned to its state as of %s.\n"),
1235 			    name, timestr);
1236 		}
1237 		if (loss > 120) {
1238 			(void) printf(dgettext(TEXT_DOMAIN,
1239 			    "%s approximately %lld "),
1240 			    dryrun ? "Would discard" : "Discarded",
1241 			    (loss + 30) / 60);
1242 			(void) printf(dgettext(TEXT_DOMAIN,
1243 			    "minutes of transactions.\n"));
1244 		} else if (loss > 0) {
1245 			(void) printf(dgettext(TEXT_DOMAIN,
1246 			    "%s approximately %lld "),
1247 			    dryrun ? "Would discard" : "Discarded", loss);
1248 			(void) printf(dgettext(TEXT_DOMAIN,
1249 			    "seconds of transactions.\n"));
1250 		}
1251 	}
1252 }
1253 
1254 void
1255 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1256     nvlist_t *config)
1257 {
1258 	int64_t loss = -1;
1259 	uint64_t edata = UINT64_MAX;
1260 	uint64_t rewindto;
1261 	struct tm t;
1262 	char timestr[128];
1263 
1264 	if (!hdl->libzfs_printerr)
1265 		return;
1266 
1267 	if (reason >= 0)
1268 		(void) printf(dgettext(TEXT_DOMAIN, "action: "));
1269 	else
1270 		(void) printf(dgettext(TEXT_DOMAIN, "\t"));
1271 
1272 	/* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1273 	if (nvlist_lookup_uint64(config,
1274 	    ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1275 		goto no_info;
1276 
1277 	(void) nvlist_lookup_int64(config, ZPOOL_CONFIG_REWIND_TIME, &loss);
1278 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1279 	    &edata);
1280 
1281 	(void) printf(dgettext(TEXT_DOMAIN,
1282 	    "Recovery is possible, but will result in some data loss.\n"));
1283 
1284 	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1285 	    strftime(timestr, 128, 0, &t) != 0) {
1286 		(void) printf(dgettext(TEXT_DOMAIN,
1287 		    "\tReturning the pool to its state as of %s\n"
1288 		    "\tshould correct the problem.  "),
1289 		    timestr);
1290 	} else {
1291 		(void) printf(dgettext(TEXT_DOMAIN,
1292 		    "\tReverting the pool to an earlier state "
1293 		    "should correct the problem.\n\t"));
1294 	}
1295 
1296 	if (loss > 120) {
1297 		(void) printf(dgettext(TEXT_DOMAIN,
1298 		    "Approximately %lld minutes of data\n"
1299 		    "\tmust be discarded, irreversibly.  "), (loss + 30) / 60);
1300 	} else if (loss > 0) {
1301 		(void) printf(dgettext(TEXT_DOMAIN,
1302 		    "Approximately %lld seconds of data\n"
1303 		    "\tmust be discarded, irreversibly.  "), loss);
1304 	}
1305 	if (edata != 0 && edata != UINT64_MAX) {
1306 		if (edata == 1) {
1307 			(void) printf(dgettext(TEXT_DOMAIN,
1308 			    "After rewind, at least\n"
1309 			    "\tone persistent user-data error will remain.  "));
1310 		} else {
1311 			(void) printf(dgettext(TEXT_DOMAIN,
1312 			    "After rewind, several\n"
1313 			    "\tpersistent user-data errors will remain.  "));
1314 		}
1315 	}
1316 	(void) printf(dgettext(TEXT_DOMAIN,
1317 	    "Recovery can be attempted\n\tby executing 'zpool %s -F %s'.  "),
1318 	    reason >= 0 ? "clear" : "import", name);
1319 
1320 	(void) printf(dgettext(TEXT_DOMAIN,
1321 	    "A scrub of the pool\n"
1322 	    "\tis strongly recommended after recovery.\n"));
1323 	return;
1324 
1325 no_info:
1326 	(void) printf(dgettext(TEXT_DOMAIN,
1327 	    "Destroy and re-create the pool from\n\ta backup source.\n"));
1328 }
1329 
1330 /*
1331  * zpool_import() is a contracted interface. Should be kept the same
1332  * if possible.
1333  *
1334  * Applications should use zpool_import_props() to import a pool with
1335  * new properties value to be set.
1336  */
1337 int
1338 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1339     char *altroot)
1340 {
1341 	nvlist_t *props = NULL;
1342 	int ret;
1343 
1344 	if (altroot != NULL) {
1345 		if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1346 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
1347 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1348 			    newname));
1349 		}
1350 
1351 		if (nvlist_add_string(props,
1352 		    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1353 		    nvlist_add_string(props,
1354 		    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1355 			nvlist_free(props);
1356 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
1357 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1358 			    newname));
1359 		}
1360 	}
1361 
1362 	ret = zpool_import_props(hdl, config, newname, props, B_FALSE);
1363 	if (props)
1364 		nvlist_free(props);
1365 	return (ret);
1366 }
1367 
1368 /*
1369  * Import the given pool using the known configuration and a list of
1370  * properties to be set. The configuration should have come from
1371  * zpool_find_import(). The 'newname' parameters control whether the pool
1372  * is imported with a different name.
1373  */
1374 int
1375 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1376     nvlist_t *props, boolean_t importfaulted)
1377 {
1378 	zfs_cmd_t zc = { 0 };
1379 	zpool_rewind_policy_t policy;
1380 	nvlist_t *nvi = NULL;
1381 	char *thename;
1382 	char *origname;
1383 	uint64_t returned_size;
1384 	int ret;
1385 	char errbuf[1024];
1386 
1387 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1388 	    &origname) == 0);
1389 
1390 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1391 	    "cannot import pool '%s'"), origname);
1392 
1393 	if (newname != NULL) {
1394 		if (!zpool_name_valid(hdl, B_FALSE, newname))
1395 			return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1396 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1397 			    newname));
1398 		thename = (char *)newname;
1399 	} else {
1400 		thename = origname;
1401 	}
1402 
1403 	if (props) {
1404 		uint64_t version;
1405 
1406 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1407 		    &version) == 0);
1408 
1409 		if ((props = zpool_valid_proplist(hdl, origname,
1410 		    props, version, B_TRUE, errbuf)) == NULL) {
1411 			return (-1);
1412 		} else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1413 			nvlist_free(props);
1414 			return (-1);
1415 		}
1416 	}
1417 
1418 	(void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1419 
1420 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1421 	    &zc.zc_guid) == 0);
1422 
1423 	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1424 		nvlist_free(props);
1425 		return (-1);
1426 	}
1427 	returned_size =  zc.zc_nvlist_conf_size + 512;
1428 	if (zcmd_alloc_dst_nvlist(hdl, &zc, returned_size) != 0) {
1429 		nvlist_free(props);
1430 		return (-1);
1431 	}
1432 
1433 	zc.zc_cookie = (uint64_t)importfaulted;
1434 	ret = 0;
1435 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) {
1436 		char desc[1024];
1437 
1438 		(void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
1439 		zpool_get_rewind_policy(config, &policy);
1440 		/*
1441 		 * Dry-run failed, but we print out what success
1442 		 * looks like if we found a best txg
1443 		 */
1444 		if ((policy.zrp_request & ZPOOL_TRY_REWIND) && nvi) {
1445 			zpool_rewind_exclaim(hdl, newname ? origname : thename,
1446 			    B_TRUE, nvi);
1447 			nvlist_free(nvi);
1448 			return (-1);
1449 		}
1450 
1451 		if (newname == NULL)
1452 			(void) snprintf(desc, sizeof (desc),
1453 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1454 			    thename);
1455 		else
1456 			(void) snprintf(desc, sizeof (desc),
1457 			    dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1458 			    origname, thename);
1459 
1460 		switch (errno) {
1461 		case ENOTSUP:
1462 			/*
1463 			 * Unsupported version.
1464 			 */
1465 			(void) zfs_error(hdl, EZFS_BADVERSION, desc);
1466 			break;
1467 
1468 		case EINVAL:
1469 			(void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1470 			break;
1471 
1472 		case EROFS:
1473 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1474 			    "one or more devices is read only"));
1475 			(void) zfs_error(hdl, EZFS_BADDEV, desc);
1476 			break;
1477 
1478 		default:
1479 			(void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
1480 			(void) zpool_standard_error(hdl, errno, desc);
1481 			zpool_explain_recover(hdl,
1482 			    newname ? origname : thename, -errno, nvi);
1483 			nvlist_free(nvi);
1484 			break;
1485 		}
1486 
1487 		ret = -1;
1488 	} else {
1489 		zpool_handle_t *zhp;
1490 
1491 		/*
1492 		 * This should never fail, but play it safe anyway.
1493 		 */
1494 		if (zpool_open_silent(hdl, thename, &zhp) != 0)
1495 			ret = -1;
1496 		else if (zhp != NULL)
1497 			zpool_close(zhp);
1498 		(void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
1499 		zpool_get_rewind_policy(config, &policy);
1500 		if (policy.zrp_request &
1501 		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1502 			zpool_rewind_exclaim(hdl, newname ? origname : thename,
1503 			    ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
1504 			    nvi);
1505 		}
1506 		nvlist_free(nvi);
1507 		return (0);
1508 	}
1509 
1510 	zcmd_free_nvlists(&zc);
1511 	nvlist_free(props);
1512 
1513 	return (ret);
1514 }
1515 
1516 /*
1517  * Scan the pool.
1518  */
1519 int
1520 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func)
1521 {
1522 	zfs_cmd_t zc = { 0 };
1523 	char msg[1024];
1524 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1525 
1526 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1527 	zc.zc_cookie = func;
1528 
1529 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 ||
1530 	    (errno == ENOENT && func != POOL_SCAN_NONE))
1531 		return (0);
1532 
1533 	if (func == POOL_SCAN_SCRUB) {
1534 		(void) snprintf(msg, sizeof (msg),
1535 		    dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1536 	} else if (func == POOL_SCAN_NONE) {
1537 		(void) snprintf(msg, sizeof (msg),
1538 		    dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
1539 		    zc.zc_name);
1540 	} else {
1541 		assert(!"unexpected result");
1542 	}
1543 
1544 	if (errno == EBUSY) {
1545 		nvlist_t *nvroot;
1546 		pool_scan_stat_t *ps = NULL;
1547 		uint_t psc;
1548 
1549 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
1550 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1551 		(void) nvlist_lookup_uint64_array(nvroot,
1552 		    ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
1553 		if (ps && ps->pss_func == POOL_SCAN_SCRUB)
1554 			return (zfs_error(hdl, EZFS_SCRUBBING, msg));
1555 		else
1556 			return (zfs_error(hdl, EZFS_RESILVERING, msg));
1557 	} else if (errno == ENOENT) {
1558 		return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
1559 	} else {
1560 		return (zpool_standard_error(hdl, errno, msg));
1561 	}
1562 }
1563 
1564 /*
1565  * Find a vdev that matches the search criteria specified. We use the
1566  * the nvpair name to determine how we should look for the device.
1567  * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1568  * spare; but FALSE if its an INUSE spare.
1569  */
1570 static nvlist_t *
1571 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1572     boolean_t *l2cache, boolean_t *log)
1573 {
1574 	uint_t c, children;
1575 	nvlist_t **child;
1576 	nvlist_t *ret;
1577 	uint64_t is_log;
1578 	char *srchkey;
1579 	nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1580 
1581 	/* Nothing to look for */
1582 	if (search == NULL || pair == NULL)
1583 		return (NULL);
1584 
1585 	/* Obtain the key we will use to search */
1586 	srchkey = nvpair_name(pair);
1587 
1588 	switch (nvpair_type(pair)) {
1589 	case DATA_TYPE_UINT64: {
1590 		uint64_t srchval, theguid, present;
1591 
1592 		verify(nvpair_value_uint64(pair, &srchval) == 0);
1593 		if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1594 			if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1595 			    &present) == 0) {
1596 				/*
1597 				 * If the device has never been present since
1598 				 * import, the only reliable way to match the
1599 				 * vdev is by GUID.
1600 				 */
1601 				verify(nvlist_lookup_uint64(nv,
1602 				    ZPOOL_CONFIG_GUID, &theguid) == 0);
1603 				if (theguid == srchval)
1604 					return (nv);
1605 			}
1606 		}
1607 		break;
1608 	}
1609 
1610 	case DATA_TYPE_STRING: {
1611 		char *srchval, *val;
1612 
1613 		verify(nvpair_value_string(pair, &srchval) == 0);
1614 		if (nvlist_lookup_string(nv, srchkey, &val) != 0)
1615 			break;
1616 
1617 		/*
1618 		 * Search for the requested value. We special case the search
1619 		 * for ZPOOL_CONFIG_PATH when it's a wholedisk and when
1620 		 * Looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
1621 		 * Otherwise, all other searches are simple string compares.
1622 		 */
1623 		if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 && val) {
1624 			uint64_t wholedisk = 0;
1625 
1626 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1627 			    &wholedisk);
1628 			if (wholedisk) {
1629 				/*
1630 				 * For whole disks, the internal path has 's0',
1631 				 * but the path passed in by the user doesn't.
1632 				 */
1633 				if (strlen(srchval) == strlen(val) - 2 &&
1634 				    strncmp(srchval, val, strlen(srchval)) == 0)
1635 					return (nv);
1636 				break;
1637 			}
1638 		} else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
1639 			char *type, *idx, *end, *p;
1640 			uint64_t id, vdev_id;
1641 
1642 			/*
1643 			 * Determine our vdev type, keeping in mind
1644 			 * that the srchval is composed of a type and
1645 			 * vdev id pair (i.e. mirror-4).
1646 			 */
1647 			if ((type = strdup(srchval)) == NULL)
1648 				return (NULL);
1649 
1650 			if ((p = strrchr(type, '-')) == NULL) {
1651 				free(type);
1652 				break;
1653 			}
1654 			idx = p + 1;
1655 			*p = '\0';
1656 
1657 			/*
1658 			 * If the types don't match then keep looking.
1659 			 */
1660 			if (strncmp(val, type, strlen(val)) != 0) {
1661 				free(type);
1662 				break;
1663 			}
1664 
1665 			verify(strncmp(type, VDEV_TYPE_RAIDZ,
1666 			    strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1667 			    strncmp(type, VDEV_TYPE_MIRROR,
1668 			    strlen(VDEV_TYPE_MIRROR)) == 0);
1669 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
1670 			    &id) == 0);
1671 
1672 			errno = 0;
1673 			vdev_id = strtoull(idx, &end, 10);
1674 
1675 			free(type);
1676 			if (errno != 0)
1677 				return (NULL);
1678 
1679 			/*
1680 			 * Now verify that we have the correct vdev id.
1681 			 */
1682 			if (vdev_id == id)
1683 				return (nv);
1684 		}
1685 
1686 		/*
1687 		 * Common case
1688 		 */
1689 		if (strcmp(srchval, val) == 0)
1690 			return (nv);
1691 		break;
1692 	}
1693 
1694 	default:
1695 		break;
1696 	}
1697 
1698 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1699 	    &child, &children) != 0)
1700 		return (NULL);
1701 
1702 	for (c = 0; c < children; c++) {
1703 		if ((ret = vdev_to_nvlist_iter(child[c], search,
1704 		    avail_spare, l2cache, NULL)) != NULL) {
1705 			/*
1706 			 * The 'is_log' value is only set for the toplevel
1707 			 * vdev, not the leaf vdevs.  So we always lookup the
1708 			 * log device from the root of the vdev tree (where
1709 			 * 'log' is non-NULL).
1710 			 */
1711 			if (log != NULL &&
1712 			    nvlist_lookup_uint64(child[c],
1713 			    ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
1714 			    is_log) {
1715 				*log = B_TRUE;
1716 			}
1717 			return (ret);
1718 		}
1719 	}
1720 
1721 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1722 	    &child, &children) == 0) {
1723 		for (c = 0; c < children; c++) {
1724 			if ((ret = vdev_to_nvlist_iter(child[c], search,
1725 			    avail_spare, l2cache, NULL)) != NULL) {
1726 				*avail_spare = B_TRUE;
1727 				return (ret);
1728 			}
1729 		}
1730 	}
1731 
1732 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1733 	    &child, &children) == 0) {
1734 		for (c = 0; c < children; c++) {
1735 			if ((ret = vdev_to_nvlist_iter(child[c], search,
1736 			    avail_spare, l2cache, NULL)) != NULL) {
1737 				*l2cache = B_TRUE;
1738 				return (ret);
1739 			}
1740 		}
1741 	}
1742 
1743 	return (NULL);
1744 }
1745 
1746 /*
1747  * Given a physical path (minus the "/devices" prefix), find the
1748  * associated vdev.
1749  */
1750 nvlist_t *
1751 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
1752     boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
1753 {
1754 	nvlist_t *search, *nvroot, *ret;
1755 
1756 	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1757 	verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
1758 
1759 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1760 	    &nvroot) == 0);
1761 
1762 	*avail_spare = B_FALSE;
1763 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1764 	nvlist_free(search);
1765 
1766 	return (ret);
1767 }
1768 
1769 /*
1770  * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
1771  */
1772 boolean_t
1773 zpool_vdev_is_interior(const char *name)
1774 {
1775 	if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1776 	    strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
1777 		return (B_TRUE);
1778 	return (B_FALSE);
1779 }
1780 
1781 nvlist_t *
1782 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
1783     boolean_t *l2cache, boolean_t *log)
1784 {
1785 	char buf[MAXPATHLEN];
1786 	char *end;
1787 	nvlist_t *nvroot, *search, *ret;
1788 	uint64_t guid;
1789 
1790 	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1791 
1792 	guid = strtoull(path, &end, 10);
1793 	if (guid != 0 && *end == '\0') {
1794 		verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
1795 	} else if (zpool_vdev_is_interior(path)) {
1796 		verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
1797 	} else if (path[0] != '/') {
1798 		(void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path);
1799 		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
1800 	} else {
1801 		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
1802 	}
1803 
1804 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1805 	    &nvroot) == 0);
1806 
1807 	*avail_spare = B_FALSE;
1808 	*l2cache = B_FALSE;
1809 	if (log != NULL)
1810 		*log = B_FALSE;
1811 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1812 	nvlist_free(search);
1813 
1814 	return (ret);
1815 }
1816 
1817 static int
1818 vdev_online(nvlist_t *nv)
1819 {
1820 	uint64_t ival;
1821 
1822 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
1823 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
1824 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
1825 		return (0);
1826 
1827 	return (1);
1828 }
1829 
1830 /*
1831  * Helper function for zpool_get_physpaths().
1832  */
1833 static int
1834 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
1835     size_t *bytes_written)
1836 {
1837 	size_t bytes_left, pos, rsz;
1838 	char *tmppath;
1839 	const char *format;
1840 
1841 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
1842 	    &tmppath) != 0)
1843 		return (EZFS_NODEVICE);
1844 
1845 	pos = *bytes_written;
1846 	bytes_left = physpath_size - pos;
1847 	format = (pos == 0) ? "%s" : " %s";
1848 
1849 	rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
1850 	*bytes_written += rsz;
1851 
1852 	if (rsz >= bytes_left) {
1853 		/* if physpath was not copied properly, clear it */
1854 		if (bytes_left != 0) {
1855 			physpath[pos] = 0;
1856 		}
1857 		return (EZFS_NOSPC);
1858 	}
1859 	return (0);
1860 }
1861 
1862 static int
1863 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
1864     size_t *rsz, boolean_t is_spare)
1865 {
1866 	char *type;
1867 	int ret;
1868 
1869 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
1870 		return (EZFS_INVALCONFIG);
1871 
1872 	if (strcmp(type, VDEV_TYPE_DISK) == 0) {
1873 		/*
1874 		 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
1875 		 * For a spare vdev, we only want to boot from the active
1876 		 * spare device.
1877 		 */
1878 		if (is_spare) {
1879 			uint64_t spare = 0;
1880 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
1881 			    &spare);
1882 			if (!spare)
1883 				return (EZFS_INVALCONFIG);
1884 		}
1885 
1886 		if (vdev_online(nv)) {
1887 			if ((ret = vdev_get_one_physpath(nv, physpath,
1888 			    phypath_size, rsz)) != 0)
1889 				return (ret);
1890 		}
1891 	} else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
1892 	    strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
1893 	    (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
1894 		nvlist_t **child;
1895 		uint_t count;
1896 		int i, ret;
1897 
1898 		if (nvlist_lookup_nvlist_array(nv,
1899 		    ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
1900 			return (EZFS_INVALCONFIG);
1901 
1902 		for (i = 0; i < count; i++) {
1903 			ret = vdev_get_physpaths(child[i], physpath,
1904 			    phypath_size, rsz, is_spare);
1905 			if (ret == EZFS_NOSPC)
1906 				return (ret);
1907 		}
1908 	}
1909 
1910 	return (EZFS_POOL_INVALARG);
1911 }
1912 
1913 /*
1914  * Get phys_path for a root pool config.
1915  * Return 0 on success; non-zero on failure.
1916  */
1917 static int
1918 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
1919 {
1920 	size_t rsz;
1921 	nvlist_t *vdev_root;
1922 	nvlist_t **child;
1923 	uint_t count;
1924 	char *type;
1925 
1926 	rsz = 0;
1927 
1928 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1929 	    &vdev_root) != 0)
1930 		return (EZFS_INVALCONFIG);
1931 
1932 	if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
1933 	    nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
1934 	    &child, &count) != 0)
1935 		return (EZFS_INVALCONFIG);
1936 
1937 	/*
1938 	 * root pool can not have EFI labeled disks and can only have
1939 	 * a single top-level vdev.
1940 	 */
1941 	if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
1942 	    pool_uses_efi(vdev_root))
1943 		return (EZFS_POOL_INVALARG);
1944 
1945 	(void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
1946 	    B_FALSE);
1947 
1948 	/* No online devices */
1949 	if (rsz == 0)
1950 		return (EZFS_NODEVICE);
1951 
1952 	return (0);
1953 }
1954 
1955 /*
1956  * Get phys_path for a root pool
1957  * Return 0 on success; non-zero on failure.
1958  */
1959 int
1960 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
1961 {
1962 	return (zpool_get_config_physpath(zhp->zpool_config, physpath,
1963 	    phypath_size));
1964 }
1965 
1966 /*
1967  * If the device has being dynamically expanded then we need to relabel
1968  * the disk to use the new unallocated space.
1969  */
1970 static int
1971 zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
1972 {
1973 	char path[MAXPATHLEN];
1974 	char errbuf[1024];
1975 	int fd, error;
1976 	int (*_efi_use_whole_disk)(int);
1977 
1978 	if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
1979 	    "efi_use_whole_disk")) == NULL)
1980 		return (-1);
1981 
1982 	(void) snprintf(path, sizeof (path), "%s/%s", RDISK_ROOT, name);
1983 
1984 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
1985 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
1986 		    "relabel '%s': unable to open device"), name);
1987 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
1988 	}
1989 
1990 	/*
1991 	 * It's possible that we might encounter an error if the device
1992 	 * does not have any unallocated space left. If so, we simply
1993 	 * ignore that error and continue on.
1994 	 */
1995 	error = _efi_use_whole_disk(fd);
1996 	(void) close(fd);
1997 	if (error && error != VT_ENOSPC) {
1998 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
1999 		    "relabel '%s': unable to read disk capacity"), name);
2000 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2001 	}
2002 	return (0);
2003 }
2004 
2005 /*
2006  * Bring the specified vdev online.   The 'flags' parameter is a set of the
2007  * ZFS_ONLINE_* flags.
2008  */
2009 int
2010 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2011     vdev_state_t *newstate)
2012 {
2013 	zfs_cmd_t zc = { 0 };
2014 	char msg[1024];
2015 	nvlist_t *tgt;
2016 	boolean_t avail_spare, l2cache, islog;
2017 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2018 
2019 	if (flags & ZFS_ONLINE_EXPAND) {
2020 		(void) snprintf(msg, sizeof (msg),
2021 		    dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2022 	} else {
2023 		(void) snprintf(msg, sizeof (msg),
2024 		    dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2025 	}
2026 
2027 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2028 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2029 	    &islog)) == NULL)
2030 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2031 
2032 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2033 
2034 	if (avail_spare)
2035 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
2036 
2037 	if (flags & ZFS_ONLINE_EXPAND ||
2038 	    zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2039 		char *pathname = NULL;
2040 		uint64_t wholedisk = 0;
2041 
2042 		(void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2043 		    &wholedisk);
2044 		verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
2045 		    &pathname) == 0);
2046 
2047 		/*
2048 		 * XXX - L2ARC 1.0 devices can't support expansion.
2049 		 */
2050 		if (l2cache) {
2051 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2052 			    "cannot expand cache devices"));
2053 			return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2054 		}
2055 
2056 		if (wholedisk) {
2057 			pathname += strlen(DISK_ROOT) + 1;
2058 			(void) zpool_relabel_disk(zhp->zpool_hdl, pathname);
2059 		}
2060 	}
2061 
2062 	zc.zc_cookie = VDEV_STATE_ONLINE;
2063 	zc.zc_obj = flags;
2064 
2065 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2066 		if (errno == EINVAL) {
2067 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2068 			    "from this pool into a new one.  Use '%s' "
2069 			    "instead"), "zpool detach");
2070 			return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2071 		}
2072 		return (zpool_standard_error(hdl, errno, msg));
2073 	}
2074 
2075 	*newstate = zc.zc_cookie;
2076 	return (0);
2077 }
2078 
2079 /*
2080  * Take the specified vdev offline
2081  */
2082 int
2083 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2084 {
2085 	zfs_cmd_t zc = { 0 };
2086 	char msg[1024];
2087 	nvlist_t *tgt;
2088 	boolean_t avail_spare, l2cache;
2089 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2090 
2091 	(void) snprintf(msg, sizeof (msg),
2092 	    dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2093 
2094 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2095 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2096 	    NULL)) == NULL)
2097 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2098 
2099 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2100 
2101 	if (avail_spare)
2102 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
2103 
2104 	zc.zc_cookie = VDEV_STATE_OFFLINE;
2105 	zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2106 
2107 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2108 		return (0);
2109 
2110 	switch (errno) {
2111 	case EBUSY:
2112 
2113 		/*
2114 		 * There are no other replicas of this device.
2115 		 */
2116 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2117 
2118 	case EEXIST:
2119 		/*
2120 		 * The log device has unplayed logs
2121 		 */
2122 		return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2123 
2124 	default:
2125 		return (zpool_standard_error(hdl, errno, msg));
2126 	}
2127 }
2128 
2129 /*
2130  * Mark the given vdev faulted.
2131  */
2132 int
2133 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2134 {
2135 	zfs_cmd_t zc = { 0 };
2136 	char msg[1024];
2137 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2138 
2139 	(void) snprintf(msg, sizeof (msg),
2140 	    dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
2141 
2142 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2143 	zc.zc_guid = guid;
2144 	zc.zc_cookie = VDEV_STATE_FAULTED;
2145 	zc.zc_obj = aux;
2146 
2147 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2148 		return (0);
2149 
2150 	switch (errno) {
2151 	case EBUSY:
2152 
2153 		/*
2154 		 * There are no other replicas of this device.
2155 		 */
2156 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2157 
2158 	default:
2159 		return (zpool_standard_error(hdl, errno, msg));
2160 	}
2161 
2162 }
2163 
2164 /*
2165  * Mark the given vdev degraded.
2166  */
2167 int
2168 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2169 {
2170 	zfs_cmd_t zc = { 0 };
2171 	char msg[1024];
2172 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2173 
2174 	(void) snprintf(msg, sizeof (msg),
2175 	    dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
2176 
2177 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2178 	zc.zc_guid = guid;
2179 	zc.zc_cookie = VDEV_STATE_DEGRADED;
2180 	zc.zc_obj = aux;
2181 
2182 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2183 		return (0);
2184 
2185 	return (zpool_standard_error(hdl, errno, msg));
2186 }
2187 
2188 /*
2189  * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2190  * a hot spare.
2191  */
2192 static boolean_t
2193 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2194 {
2195 	nvlist_t **child;
2196 	uint_t c, children;
2197 	char *type;
2198 
2199 	if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2200 	    &children) == 0) {
2201 		verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2202 		    &type) == 0);
2203 
2204 		if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2205 		    children == 2 && child[which] == tgt)
2206 			return (B_TRUE);
2207 
2208 		for (c = 0; c < children; c++)
2209 			if (is_replacing_spare(child[c], tgt, which))
2210 				return (B_TRUE);
2211 	}
2212 
2213 	return (B_FALSE);
2214 }
2215 
2216 /*
2217  * Attach new_disk (fully described by nvroot) to old_disk.
2218  * If 'replacing' is specified, the new disk will replace the old one.
2219  */
2220 int
2221 zpool_vdev_attach(zpool_handle_t *zhp,
2222     const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2223 {
2224 	zfs_cmd_t zc = { 0 };
2225 	char msg[1024];
2226 	int ret;
2227 	nvlist_t *tgt;
2228 	boolean_t avail_spare, l2cache, islog;
2229 	uint64_t val;
2230 	char *path, *newname;
2231 	nvlist_t **child;
2232 	uint_t children;
2233 	nvlist_t *config_root;
2234 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2235 	boolean_t rootpool = pool_is_bootable(zhp);
2236 
2237 	if (replacing)
2238 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2239 		    "cannot replace %s with %s"), old_disk, new_disk);
2240 	else
2241 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2242 		    "cannot attach %s to %s"), new_disk, old_disk);
2243 
2244 	/*
2245 	 * If this is a root pool, make sure that we're not attaching an
2246 	 * EFI labeled device.
2247 	 */
2248 	if (rootpool && pool_uses_efi(nvroot)) {
2249 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2250 		    "EFI labeled devices are not supported on root pools."));
2251 		return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
2252 	}
2253 
2254 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2255 	if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2256 	    &islog)) == 0)
2257 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2258 
2259 	if (avail_spare)
2260 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
2261 
2262 	if (l2cache)
2263 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2264 
2265 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2266 	zc.zc_cookie = replacing;
2267 
2268 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2269 	    &child, &children) != 0 || children != 1) {
2270 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2271 		    "new device must be a single disk"));
2272 		return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2273 	}
2274 
2275 	verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2276 	    ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2277 
2278 	if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
2279 		return (-1);
2280 
2281 	/*
2282 	 * If the target is a hot spare that has been swapped in, we can only
2283 	 * replace it with another hot spare.
2284 	 */
2285 	if (replacing &&
2286 	    nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2287 	    (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2288 	    NULL) == NULL || !avail_spare) &&
2289 	    is_replacing_spare(config_root, tgt, 1)) {
2290 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2291 		    "can only be replaced by another hot spare"));
2292 		free(newname);
2293 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
2294 	}
2295 
2296 	/*
2297 	 * If we are attempting to replace a spare, it canot be applied to an
2298 	 * already spared device.
2299 	 */
2300 	if (replacing &&
2301 	    nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 &&
2302 	    zpool_find_vdev(zhp, newname, &avail_spare,
2303 	    &l2cache, NULL) != NULL && avail_spare &&
2304 	    is_replacing_spare(config_root, tgt, 0)) {
2305 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2306 		    "device has already been replaced with a spare"));
2307 		free(newname);
2308 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
2309 	}
2310 
2311 	free(newname);
2312 
2313 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2314 		return (-1);
2315 
2316 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2317 
2318 	zcmd_free_nvlists(&zc);
2319 
2320 	if (ret == 0) {
2321 		if (rootpool) {
2322 			/*
2323 			 * XXX - This should be removed once we can
2324 			 * automatically install the bootblocks on the
2325 			 * newly attached disk.
2326 			 */
2327 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Please "
2328 			    "be sure to invoke %s to make '%s' bootable.\n"),
2329 			    BOOTCMD, new_disk);
2330 
2331 			/*
2332 			 * XXX need a better way to prevent user from
2333 			 * booting up a half-baked vdev.
2334 			 */
2335 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2336 			    "sure to wait until resilver is done "
2337 			    "before rebooting.\n"));
2338 		}
2339 		return (0);
2340 	}
2341 
2342 	switch (errno) {
2343 	case ENOTSUP:
2344 		/*
2345 		 * Can't attach to or replace this type of vdev.
2346 		 */
2347 		if (replacing) {
2348 			if (islog)
2349 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2350 				    "cannot replace a log with a spare"));
2351 			else
2352 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2353 				    "cannot replace a replacing device"));
2354 		} else {
2355 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2356 			    "can only attach to mirrors and top-level "
2357 			    "disks"));
2358 		}
2359 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
2360 		break;
2361 
2362 	case EINVAL:
2363 		/*
2364 		 * The new device must be a single disk.
2365 		 */
2366 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2367 		    "new device must be a single disk"));
2368 		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2369 		break;
2370 
2371 	case EBUSY:
2372 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
2373 		    new_disk);
2374 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2375 		break;
2376 
2377 	case EOVERFLOW:
2378 		/*
2379 		 * The new device is too small.
2380 		 */
2381 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2382 		    "device is too small"));
2383 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2384 		break;
2385 
2386 	case EDOM:
2387 		/*
2388 		 * The new device has a different alignment requirement.
2389 		 */
2390 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2391 		    "devices have different sector alignment"));
2392 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2393 		break;
2394 
2395 	case ENAMETOOLONG:
2396 		/*
2397 		 * The resulting top-level vdev spec won't fit in the label.
2398 		 */
2399 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2400 		break;
2401 
2402 	default:
2403 		(void) zpool_standard_error(hdl, errno, msg);
2404 	}
2405 
2406 	return (-1);
2407 }
2408 
2409 /*
2410  * Detach the specified device.
2411  */
2412 int
2413 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2414 {
2415 	zfs_cmd_t zc = { 0 };
2416 	char msg[1024];
2417 	nvlist_t *tgt;
2418 	boolean_t avail_spare, l2cache;
2419 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2420 
2421 	(void) snprintf(msg, sizeof (msg),
2422 	    dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2423 
2424 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2425 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2426 	    NULL)) == 0)
2427 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2428 
2429 	if (avail_spare)
2430 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
2431 
2432 	if (l2cache)
2433 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2434 
2435 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2436 
2437 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2438 		return (0);
2439 
2440 	switch (errno) {
2441 
2442 	case ENOTSUP:
2443 		/*
2444 		 * Can't detach from this type of vdev.
2445 		 */
2446 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2447 		    "applicable to mirror and replacing vdevs"));
2448 		(void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg);
2449 		break;
2450 
2451 	case EBUSY:
2452 		/*
2453 		 * There are no other replicas of this device.
2454 		 */
2455 		(void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2456 		break;
2457 
2458 	default:
2459 		(void) zpool_standard_error(hdl, errno, msg);
2460 	}
2461 
2462 	return (-1);
2463 }
2464 
2465 /*
2466  * Find a mirror vdev in the source nvlist.
2467  *
2468  * The mchild array contains a list of disks in one of the top-level mirrors
2469  * of the source pool.  The schild array contains a list of disks that the
2470  * user specified on the command line.  We loop over the mchild array to
2471  * see if any entry in the schild array matches.
2472  *
2473  * If a disk in the mchild array is found in the schild array, we return
2474  * the index of that entry.  Otherwise we return -1.
2475  */
2476 static int
2477 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2478     nvlist_t **schild, uint_t schildren)
2479 {
2480 	uint_t mc;
2481 
2482 	for (mc = 0; mc < mchildren; mc++) {
2483 		uint_t sc;
2484 		char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2485 		    mchild[mc], B_FALSE);
2486 
2487 		for (sc = 0; sc < schildren; sc++) {
2488 			char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2489 			    schild[sc], B_FALSE);
2490 			boolean_t result = (strcmp(mpath, spath) == 0);
2491 
2492 			free(spath);
2493 			if (result) {
2494 				free(mpath);
2495 				return (mc);
2496 			}
2497 		}
2498 
2499 		free(mpath);
2500 	}
2501 
2502 	return (-1);
2503 }
2504 
2505 /*
2506  * Split a mirror pool.  If newroot points to null, then a new nvlist
2507  * is generated and it is the responsibility of the caller to free it.
2508  */
2509 int
2510 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
2511     nvlist_t *props, splitflags_t flags)
2512 {
2513 	zfs_cmd_t zc = { 0 };
2514 	char msg[1024];
2515 	nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
2516 	nvlist_t **varray = NULL, *zc_props = NULL;
2517 	uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
2518 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2519 	uint64_t vers;
2520 	boolean_t freelist = B_FALSE, memory_err = B_TRUE;
2521 	int retval = 0;
2522 
2523 	(void) snprintf(msg, sizeof (msg),
2524 	    dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
2525 
2526 	if (!zpool_name_valid(hdl, B_FALSE, newname))
2527 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
2528 
2529 	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2530 		(void) fprintf(stderr, gettext("Internal error: unable to "
2531 		    "retrieve pool configuration\n"));
2532 		return (-1);
2533 	}
2534 
2535 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
2536 	    == 0);
2537 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
2538 
2539 	if (props) {
2540 		if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2541 		    props, vers, B_TRUE, msg)) == NULL)
2542 			return (-1);
2543 	}
2544 
2545 	if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2546 	    &children) != 0) {
2547 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2548 		    "Source pool is missing vdev tree"));
2549 		if (zc_props)
2550 			nvlist_free(zc_props);
2551 		return (-1);
2552 	}
2553 
2554 	varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
2555 	vcount = 0;
2556 
2557 	if (*newroot == NULL ||
2558 	    nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
2559 	    &newchild, &newchildren) != 0)
2560 		newchildren = 0;
2561 
2562 	for (c = 0; c < children; c++) {
2563 		uint64_t is_log = B_FALSE, is_hole = B_FALSE;
2564 		char *type;
2565 		nvlist_t **mchild, *vdev;
2566 		uint_t mchildren;
2567 		int entry;
2568 
2569 		/*
2570 		 * Unlike cache & spares, slogs are stored in the
2571 		 * ZPOOL_CONFIG_CHILDREN array.  We filter them out here.
2572 		 */
2573 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2574 		    &is_log);
2575 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2576 		    &is_hole);
2577 		if (is_log || is_hole) {
2578 			/*
2579 			 * Create a hole vdev and put it in the config.
2580 			 */
2581 			if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
2582 				goto out;
2583 			if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
2584 			    VDEV_TYPE_HOLE) != 0)
2585 				goto out;
2586 			if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
2587 			    1) != 0)
2588 				goto out;
2589 			if (lastlog == 0)
2590 				lastlog = vcount;
2591 			varray[vcount++] = vdev;
2592 			continue;
2593 		}
2594 		lastlog = 0;
2595 		verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
2596 		    == 0);
2597 		if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
2598 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2599 			    "Source pool must be composed only of mirrors\n"));
2600 			retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2601 			goto out;
2602 		}
2603 
2604 		verify(nvlist_lookup_nvlist_array(child[c],
2605 		    ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2606 
2607 		/* find or add an entry for this top-level vdev */
2608 		if (newchildren > 0 &&
2609 		    (entry = find_vdev_entry(zhp, mchild, mchildren,
2610 		    newchild, newchildren)) >= 0) {
2611 			/* We found a disk that the user specified. */
2612 			vdev = mchild[entry];
2613 			++found;
2614 		} else {
2615 			/* User didn't specify a disk for this vdev. */
2616 			vdev = mchild[mchildren - 1];
2617 		}
2618 
2619 		if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
2620 			goto out;
2621 	}
2622 
2623 	/* did we find every disk the user specified? */
2624 	if (found != newchildren) {
2625 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
2626 		    "include at most one disk from each mirror"));
2627 		retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2628 		goto out;
2629 	}
2630 
2631 	/* Prepare the nvlist for populating. */
2632 	if (*newroot == NULL) {
2633 		if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
2634 			goto out;
2635 		freelist = B_TRUE;
2636 		if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
2637 		    VDEV_TYPE_ROOT) != 0)
2638 			goto out;
2639 	} else {
2640 		verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
2641 	}
2642 
2643 	/* Add all the children we found */
2644 	if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
2645 	    lastlog == 0 ? vcount : lastlog) != 0)
2646 		goto out;
2647 
2648 	/*
2649 	 * If we're just doing a dry run, exit now with success.
2650 	 */
2651 	if (flags.dryrun) {
2652 		memory_err = B_FALSE;
2653 		freelist = B_FALSE;
2654 		goto out;
2655 	}
2656 
2657 	/* now build up the config list & call the ioctl */
2658 	if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
2659 		goto out;
2660 
2661 	if (nvlist_add_nvlist(newconfig,
2662 	    ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
2663 	    nvlist_add_string(newconfig,
2664 	    ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
2665 	    nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
2666 		goto out;
2667 
2668 	/*
2669 	 * The new pool is automatically part of the namespace unless we
2670 	 * explicitly export it.
2671 	 */
2672 	if (!flags.import)
2673 		zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
2674 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2675 	(void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
2676 	if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
2677 		goto out;
2678 	if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
2679 		goto out;
2680 
2681 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
2682 		retval = zpool_standard_error(hdl, errno, msg);
2683 		goto out;
2684 	}
2685 
2686 	freelist = B_FALSE;
2687 	memory_err = B_FALSE;
2688 
2689 out:
2690 	if (varray != NULL) {
2691 		int v;
2692 
2693 		for (v = 0; v < vcount; v++)
2694 			nvlist_free(varray[v]);
2695 		free(varray);
2696 	}
2697 	zcmd_free_nvlists(&zc);
2698 	if (zc_props)
2699 		nvlist_free(zc_props);
2700 	if (newconfig)
2701 		nvlist_free(newconfig);
2702 	if (freelist) {
2703 		nvlist_free(*newroot);
2704 		*newroot = NULL;
2705 	}
2706 
2707 	if (retval != 0)
2708 		return (retval);
2709 
2710 	if (memory_err)
2711 		return (no_memory(hdl));
2712 
2713 	return (0);
2714 }
2715 
2716 /*
2717  * Remove the given device.  Currently, this is supported only for hot spares
2718  * and level 2 cache devices.
2719  */
2720 int
2721 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
2722 {
2723 	zfs_cmd_t zc = { 0 };
2724 	char msg[1024];
2725 	nvlist_t *tgt;
2726 	boolean_t avail_spare, l2cache, islog;
2727 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2728 	uint64_t version;
2729 
2730 	(void) snprintf(msg, sizeof (msg),
2731 	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
2732 
2733 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2734 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2735 	    &islog)) == 0)
2736 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2737 	/*
2738 	 * XXX - this should just go away.
2739 	 */
2740 	if (!avail_spare && !l2cache && !islog) {
2741 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2742 		    "only inactive hot spares, cache, top-level, "
2743 		    "or log devices can be removed"));
2744 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2745 	}
2746 
2747 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
2748 	if (islog && version < SPA_VERSION_HOLES) {
2749 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2750 		    "pool must be upgrade to support log removal"));
2751 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
2752 	}
2753 
2754 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2755 
2756 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
2757 		return (0);
2758 
2759 	return (zpool_standard_error(hdl, errno, msg));
2760 }
2761 
2762 /*
2763  * Clear the errors for the pool, or the particular device if specified.
2764  */
2765 int
2766 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
2767 {
2768 	zfs_cmd_t zc = { 0 };
2769 	char msg[1024];
2770 	nvlist_t *tgt;
2771 	zpool_rewind_policy_t policy;
2772 	boolean_t avail_spare, l2cache;
2773 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2774 	nvlist_t *nvi = NULL;
2775 
2776 	if (path)
2777 		(void) snprintf(msg, sizeof (msg),
2778 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2779 		    path);
2780 	else
2781 		(void) snprintf(msg, sizeof (msg),
2782 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2783 		    zhp->zpool_name);
2784 
2785 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2786 	if (path) {
2787 		if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
2788 		    &l2cache, NULL)) == 0)
2789 			return (zfs_error(hdl, EZFS_NODEVICE, msg));
2790 
2791 		/*
2792 		 * Don't allow error clearing for hot spares.  Do allow
2793 		 * error clearing for l2cache devices.
2794 		 */
2795 		if (avail_spare)
2796 			return (zfs_error(hdl, EZFS_ISSPARE, msg));
2797 
2798 		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
2799 		    &zc.zc_guid) == 0);
2800 	}
2801 
2802 	zpool_get_rewind_policy(rewindnvl, &policy);
2803 	zc.zc_cookie = policy.zrp_request;
2804 
2805 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 8192) != 0)
2806 		return (-1);
2807 
2808 	if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, rewindnvl) != 0)
2809 		return (-1);
2810 
2811 	if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0 ||
2812 	    ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
2813 	    errno != EPERM && errno != EACCES)) {
2814 		if (policy.zrp_request &
2815 		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
2816 			(void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
2817 			zpool_rewind_exclaim(hdl, zc.zc_name,
2818 			    ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
2819 			    nvi);
2820 			nvlist_free(nvi);
2821 		}
2822 		zcmd_free_nvlists(&zc);
2823 		return (0);
2824 	}
2825 
2826 	zcmd_free_nvlists(&zc);
2827 	return (zpool_standard_error(hdl, errno, msg));
2828 }
2829 
2830 /*
2831  * Similar to zpool_clear(), but takes a GUID (used by fmd).
2832  */
2833 int
2834 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
2835 {
2836 	zfs_cmd_t zc = { 0 };
2837 	char msg[1024];
2838 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2839 
2840 	(void) snprintf(msg, sizeof (msg),
2841 	    dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
2842 	    guid);
2843 
2844 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2845 	zc.zc_guid = guid;
2846 
2847 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
2848 		return (0);
2849 
2850 	return (zpool_standard_error(hdl, errno, msg));
2851 }
2852 
2853 /*
2854  * Convert from a devid string to a path.
2855  */
2856 static char *
2857 devid_to_path(char *devid_str)
2858 {
2859 	ddi_devid_t devid;
2860 	char *minor;
2861 	char *path;
2862 	devid_nmlist_t *list = NULL;
2863 	int ret;
2864 
2865 	if (devid_str_decode(devid_str, &devid, &minor) != 0)
2866 		return (NULL);
2867 
2868 	ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
2869 
2870 	devid_str_free(minor);
2871 	devid_free(devid);
2872 
2873 	if (ret != 0)
2874 		return (NULL);
2875 
2876 	if ((path = strdup(list[0].devname)) == NULL)
2877 		return (NULL);
2878 
2879 	devid_free_nmlist(list);
2880 
2881 	return (path);
2882 }
2883 
2884 /*
2885  * Convert from a path to a devid string.
2886  */
2887 static char *
2888 path_to_devid(const char *path)
2889 {
2890 	int fd;
2891 	ddi_devid_t devid;
2892 	char *minor, *ret;
2893 
2894 	if ((fd = open(path, O_RDONLY)) < 0)
2895 		return (NULL);
2896 
2897 	minor = NULL;
2898 	ret = NULL;
2899 	if (devid_get(fd, &devid) == 0) {
2900 		if (devid_get_minor_name(fd, &minor) == 0)
2901 			ret = devid_str_encode(devid, minor);
2902 		if (minor != NULL)
2903 			devid_str_free(minor);
2904 		devid_free(devid);
2905 	}
2906 	(void) close(fd);
2907 
2908 	return (ret);
2909 }
2910 
2911 /*
2912  * Issue the necessary ioctl() to update the stored path value for the vdev.  We
2913  * ignore any failure here, since a common case is for an unprivileged user to
2914  * type 'zpool status', and we'll display the correct information anyway.
2915  */
2916 static void
2917 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
2918 {
2919 	zfs_cmd_t zc = { 0 };
2920 
2921 	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2922 	(void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
2923 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2924 	    &zc.zc_guid) == 0);
2925 
2926 	(void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
2927 }
2928 
2929 /*
2930  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
2931  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
2932  * We also check if this is a whole disk, in which case we strip off the
2933  * trailing 's0' slice name.
2934  *
2935  * This routine is also responsible for identifying when disks have been
2936  * reconfigured in a new location.  The kernel will have opened the device by
2937  * devid, but the path will still refer to the old location.  To catch this, we
2938  * first do a path -> devid translation (which is fast for the common case).  If
2939  * the devid matches, we're done.  If not, we do a reverse devid -> path
2940  * translation and issue the appropriate ioctl() to update the path of the vdev.
2941  * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
2942  * of these checks.
2943  */
2944 char *
2945 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
2946     boolean_t verbose)
2947 {
2948 	char *path, *devid;
2949 	uint64_t value;
2950 	char buf[64];
2951 	vdev_stat_t *vs;
2952 	uint_t vsc;
2953 
2954 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
2955 	    &value) == 0) {
2956 		verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2957 		    &value) == 0);
2958 		(void) snprintf(buf, sizeof (buf), "%llu",
2959 		    (u_longlong_t)value);
2960 		path = buf;
2961 	} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
2962 
2963 		/*
2964 		 * If the device is dead (faulted, offline, etc) then don't
2965 		 * bother opening it.  Otherwise we may be forcing the user to
2966 		 * open a misbehaving device, which can have undesirable
2967 		 * effects.
2968 		 */
2969 		if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2970 		    (uint64_t **)&vs, &vsc) != 0 ||
2971 		    vs->vs_state >= VDEV_STATE_DEGRADED) &&
2972 		    zhp != NULL &&
2973 		    nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
2974 			/*
2975 			 * Determine if the current path is correct.
2976 			 */
2977 			char *newdevid = path_to_devid(path);
2978 
2979 			if (newdevid == NULL ||
2980 			    strcmp(devid, newdevid) != 0) {
2981 				char *newpath;
2982 
2983 				if ((newpath = devid_to_path(devid)) != NULL) {
2984 					/*
2985 					 * Update the path appropriately.
2986 					 */
2987 					set_path(zhp, nv, newpath);
2988 					if (nvlist_add_string(nv,
2989 					    ZPOOL_CONFIG_PATH, newpath) == 0)
2990 						verify(nvlist_lookup_string(nv,
2991 						    ZPOOL_CONFIG_PATH,
2992 						    &path) == 0);
2993 					free(newpath);
2994 				}
2995 			}
2996 
2997 			if (newdevid)
2998 				devid_str_free(newdevid);
2999 		}
3000 
3001 		if (strncmp(path, "/dev/dsk/", 9) == 0)
3002 			path += 9;
3003 
3004 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3005 		    &value) == 0 && value) {
3006 			char *tmp = zfs_strdup(hdl, path);
3007 			if (tmp == NULL)
3008 				return (NULL);
3009 			tmp[strlen(path) - 2] = '\0';
3010 			return (tmp);
3011 		}
3012 	} else {
3013 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3014 
3015 		/*
3016 		 * If it's a raidz device, we need to stick in the parity level.
3017 		 */
3018 		if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3019 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3020 			    &value) == 0);
3021 			(void) snprintf(buf, sizeof (buf), "%s%llu", path,
3022 			    (u_longlong_t)value);
3023 			path = buf;
3024 		}
3025 
3026 		/*
3027 		 * We identify each top-level vdev by using a <type-id>
3028 		 * naming convention.
3029 		 */
3030 		if (verbose) {
3031 			uint64_t id;
3032 
3033 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3034 			    &id) == 0);
3035 			(void) snprintf(buf, sizeof (buf), "%s-%llu", path,
3036 			    (u_longlong_t)id);
3037 			path = buf;
3038 		}
3039 	}
3040 
3041 	return (zfs_strdup(hdl, path));
3042 }
3043 
3044 static int
3045 zbookmark_compare(const void *a, const void *b)
3046 {
3047 	return (memcmp(a, b, sizeof (zbookmark_t)));
3048 }
3049 
3050 /*
3051  * Retrieve the persistent error log, uniquify the members, and return to the
3052  * caller.
3053  */
3054 int
3055 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3056 {
3057 	zfs_cmd_t zc = { 0 };
3058 	uint64_t count;
3059 	zbookmark_t *zb = NULL;
3060 	int i;
3061 
3062 	/*
3063 	 * Retrieve the raw error list from the kernel.  If the number of errors
3064 	 * has increased, allocate more space and continue until we get the
3065 	 * entire list.
3066 	 */
3067 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3068 	    &count) == 0);
3069 	if (count == 0)
3070 		return (0);
3071 	if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3072 	    count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
3073 		return (-1);
3074 	zc.zc_nvlist_dst_size = count;
3075 	(void) strcpy(zc.zc_name, zhp->zpool_name);
3076 	for (;;) {
3077 		if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3078 		    &zc) != 0) {
3079 			free((void *)(uintptr_t)zc.zc_nvlist_dst);
3080 			if (errno == ENOMEM) {
3081 				count = zc.zc_nvlist_dst_size;
3082 				if ((zc.zc_nvlist_dst = (uintptr_t)
3083 				    zfs_alloc(zhp->zpool_hdl, count *
3084 				    sizeof (zbookmark_t))) == (uintptr_t)NULL)
3085 					return (-1);
3086 			} else {
3087 				return (-1);
3088 			}
3089 		} else {
3090 			break;
3091 		}
3092 	}
3093 
3094 	/*
3095 	 * Sort the resulting bookmarks.  This is a little confusing due to the
3096 	 * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
3097 	 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3098 	 * _not_ copied as part of the process.  So we point the start of our
3099 	 * array appropriate and decrement the total number of elements.
3100 	 */
3101 	zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
3102 	    zc.zc_nvlist_dst_size;
3103 	count -= zc.zc_nvlist_dst_size;
3104 
3105 	qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
3106 
3107 	verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3108 
3109 	/*
3110 	 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3111 	 */
3112 	for (i = 0; i < count; i++) {
3113 		nvlist_t *nv;
3114 
3115 		/* ignoring zb_blkid and zb_level for now */
3116 		if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3117 		    zb[i-1].zb_object == zb[i].zb_object)
3118 			continue;
3119 
3120 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3121 			goto nomem;
3122 		if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3123 		    zb[i].zb_objset) != 0) {
3124 			nvlist_free(nv);
3125 			goto nomem;
3126 		}
3127 		if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3128 		    zb[i].zb_object) != 0) {
3129 			nvlist_free(nv);
3130 			goto nomem;
3131 		}
3132 		if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3133 			nvlist_free(nv);
3134 			goto nomem;
3135 		}
3136 		nvlist_free(nv);
3137 	}
3138 
3139 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
3140 	return (0);
3141 
3142 nomem:
3143 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
3144 	return (no_memory(zhp->zpool_hdl));
3145 }
3146 
3147 /*
3148  * Upgrade a ZFS pool to the latest on-disk version.
3149  */
3150 int
3151 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3152 {
3153 	zfs_cmd_t zc = { 0 };
3154 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3155 
3156 	(void) strcpy(zc.zc_name, zhp->zpool_name);
3157 	zc.zc_cookie = new_version;
3158 
3159 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3160 		return (zpool_standard_error_fmt(hdl, errno,
3161 		    dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3162 		    zhp->zpool_name));
3163 	return (0);
3164 }
3165 
3166 void
3167 zpool_set_history_str(const char *subcommand, int argc, char **argv,
3168     char *history_str)
3169 {
3170 	int i;
3171 
3172 	(void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
3173 	for (i = 1; i < argc; i++) {
3174 		if (strlen(history_str) + 1 + strlen(argv[i]) >
3175 		    HIS_MAX_RECORD_LEN)
3176 			break;
3177 		(void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
3178 		(void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
3179 	}
3180 }
3181 
3182 /*
3183  * Stage command history for logging.
3184  */
3185 int
3186 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
3187 {
3188 	if (history_str == NULL)
3189 		return (EINVAL);
3190 
3191 	if (strlen(history_str) > HIS_MAX_RECORD_LEN)
3192 		return (EINVAL);
3193 
3194 	if (hdl->libzfs_log_str != NULL)
3195 		free(hdl->libzfs_log_str);
3196 
3197 	if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
3198 		return (no_memory(hdl));
3199 
3200 	return (0);
3201 }
3202 
3203 /*
3204  * Perform ioctl to get some command history of a pool.
3205  *
3206  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
3207  * logical offset of the history buffer to start reading from.
3208  *
3209  * Upon return, 'off' is the next logical offset to read from and
3210  * 'len' is the actual amount of bytes read into 'buf'.
3211  */
3212 static int
3213 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3214 {
3215 	zfs_cmd_t zc = { 0 };
3216 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3217 
3218 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3219 
3220 	zc.zc_history = (uint64_t)(uintptr_t)buf;
3221 	zc.zc_history_len = *len;
3222 	zc.zc_history_offset = *off;
3223 
3224 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3225 		switch (errno) {
3226 		case EPERM:
3227 			return (zfs_error_fmt(hdl, EZFS_PERM,
3228 			    dgettext(TEXT_DOMAIN,
3229 			    "cannot show history for pool '%s'"),
3230 			    zhp->zpool_name));
3231 		case ENOENT:
3232 			return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3233 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
3234 			    "'%s'"), zhp->zpool_name));
3235 		case ENOTSUP:
3236 			return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3237 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
3238 			    "'%s', pool must be upgraded"), zhp->zpool_name));
3239 		default:
3240 			return (zpool_standard_error_fmt(hdl, errno,
3241 			    dgettext(TEXT_DOMAIN,
3242 			    "cannot get history for '%s'"), zhp->zpool_name));
3243 		}
3244 	}
3245 
3246 	*len = zc.zc_history_len;
3247 	*off = zc.zc_history_offset;
3248 
3249 	return (0);
3250 }
3251 
3252 /*
3253  * Process the buffer of nvlists, unpacking and storing each nvlist record
3254  * into 'records'.  'leftover' is set to the number of bytes that weren't
3255  * processed as there wasn't a complete record.
3256  */
3257 int
3258 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3259     nvlist_t ***records, uint_t *numrecords)
3260 {
3261 	uint64_t reclen;
3262 	nvlist_t *nv;
3263 	int i;
3264 
3265 	while (bytes_read > sizeof (reclen)) {
3266 
3267 		/* get length of packed record (stored as little endian) */
3268 		for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3269 			reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3270 
3271 		if (bytes_read < sizeof (reclen) + reclen)
3272 			break;
3273 
3274 		/* unpack record */
3275 		if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3276 			return (ENOMEM);
3277 		bytes_read -= sizeof (reclen) + reclen;
3278 		buf += sizeof (reclen) + reclen;
3279 
3280 		/* add record to nvlist array */
3281 		(*numrecords)++;
3282 		if (ISP2(*numrecords + 1)) {
3283 			*records = realloc(*records,
3284 			    *numrecords * 2 * sizeof (nvlist_t *));
3285 		}
3286 		(*records)[*numrecords - 1] = nv;
3287 	}
3288 
3289 	*leftover = bytes_read;
3290 	return (0);
3291 }
3292 
3293 #define	HIS_BUF_LEN	(128*1024)
3294 
3295 /*
3296  * Retrieve the command history of a pool.
3297  */
3298 int
3299 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3300 {
3301 	char buf[HIS_BUF_LEN];
3302 	uint64_t off = 0;
3303 	nvlist_t **records = NULL;
3304 	uint_t numrecords = 0;
3305 	int err, i;
3306 
3307 	do {
3308 		uint64_t bytes_read = sizeof (buf);
3309 		uint64_t leftover;
3310 
3311 		if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
3312 			break;
3313 
3314 		/* if nothing else was read in, we're at EOF, just return */
3315 		if (!bytes_read)
3316 			break;
3317 
3318 		if ((err = zpool_history_unpack(buf, bytes_read,
3319 		    &leftover, &records, &numrecords)) != 0)
3320 			break;
3321 		off -= leftover;
3322 
3323 		/* CONSTCOND */
3324 	} while (1);
3325 
3326 	if (!err) {
3327 		verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
3328 		verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
3329 		    records, numrecords) == 0);
3330 	}
3331 	for (i = 0; i < numrecords; i++)
3332 		nvlist_free(records[i]);
3333 	free(records);
3334 
3335 	return (err);
3336 }
3337 
3338 void
3339 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
3340     char *pathname, size_t len)
3341 {
3342 	zfs_cmd_t zc = { 0 };
3343 	boolean_t mounted = B_FALSE;
3344 	char *mntpnt = NULL;
3345 	char dsname[MAXNAMELEN];
3346 
3347 	if (dsobj == 0) {
3348 		/* special case for the MOS */
3349 		(void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
3350 		return;
3351 	}
3352 
3353 	/* get the dataset's name */
3354 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3355 	zc.zc_obj = dsobj;
3356 	if (ioctl(zhp->zpool_hdl->libzfs_fd,
3357 	    ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
3358 		/* just write out a path of two object numbers */
3359 		(void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
3360 		    dsobj, obj);
3361 		return;
3362 	}
3363 	(void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
3364 
3365 	/* find out if the dataset is mounted */
3366 	mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
3367 
3368 	/* get the corrupted object's path */
3369 	(void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
3370 	zc.zc_obj = obj;
3371 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
3372 	    &zc) == 0) {
3373 		if (mounted) {
3374 			(void) snprintf(pathname, len, "%s%s", mntpnt,
3375 			    zc.zc_value);
3376 		} else {
3377 			(void) snprintf(pathname, len, "%s:%s",
3378 			    dsname, zc.zc_value);
3379 		}
3380 	} else {
3381 		(void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
3382 	}
3383 	free(mntpnt);
3384 }
3385 
3386 /*
3387  * Read the EFI label from the config, if a label does not exist then
3388  * pass back the error to the caller. If the caller has passed a non-NULL
3389  * diskaddr argument then we set it to the starting address of the EFI
3390  * partition.
3391  */
3392 static int
3393 read_efi_label(nvlist_t *config, diskaddr_t *sb)
3394 {
3395 	char *path;
3396 	int fd;
3397 	char diskname[MAXPATHLEN];
3398 	int err = -1;
3399 
3400 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
3401 		return (err);
3402 
3403 	(void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
3404 	    strrchr(path, '/'));
3405 	if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
3406 		struct dk_gpt *vtoc;
3407 
3408 		if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
3409 			if (sb != NULL)
3410 				*sb = vtoc->efi_parts[0].p_start;
3411 			efi_free(vtoc);
3412 		}
3413 		(void) close(fd);
3414 	}
3415 	return (err);
3416 }
3417 
3418 /*
3419  * determine where a partition starts on a disk in the current
3420  * configuration
3421  */
3422 static diskaddr_t
3423 find_start_block(nvlist_t *config)
3424 {
3425 	nvlist_t **child;
3426 	uint_t c, children;
3427 	diskaddr_t sb = MAXOFFSET_T;
3428 	uint64_t wholedisk;
3429 
3430 	if (nvlist_lookup_nvlist_array(config,
3431 	    ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
3432 		if (nvlist_lookup_uint64(config,
3433 		    ZPOOL_CONFIG_WHOLE_DISK,
3434 		    &wholedisk) != 0 || !wholedisk) {
3435 			return (MAXOFFSET_T);
3436 		}
3437 		if (read_efi_label(config, &sb) < 0)
3438 			sb = MAXOFFSET_T;
3439 		return (sb);
3440 	}
3441 
3442 	for (c = 0; c < children; c++) {
3443 		sb = find_start_block(child[c]);
3444 		if (sb != MAXOFFSET_T) {
3445 			return (sb);
3446 		}
3447 	}
3448 	return (MAXOFFSET_T);
3449 }
3450 
3451 /*
3452  * Label an individual disk.  The name provided is the short name,
3453  * stripped of any leading /dev path.
3454  */
3455 int
3456 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
3457 {
3458 	char path[MAXPATHLEN];
3459 	struct dk_gpt *vtoc;
3460 	int fd;
3461 	size_t resv = EFI_MIN_RESV_SIZE;
3462 	uint64_t slice_size;
3463 	diskaddr_t start_block;
3464 	char errbuf[1024];
3465 
3466 	/* prepare an error message just in case */
3467 	(void) snprintf(errbuf, sizeof (errbuf),
3468 	    dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
3469 
3470 	if (zhp) {
3471 		nvlist_t *nvroot;
3472 
3473 		if (pool_is_bootable(zhp)) {
3474 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3475 			    "EFI labeled devices are not supported on root "
3476 			    "pools."));
3477 			return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
3478 		}
3479 
3480 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
3481 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3482 
3483 		if (zhp->zpool_start_block == 0)
3484 			start_block = find_start_block(nvroot);
3485 		else
3486 			start_block = zhp->zpool_start_block;
3487 		zhp->zpool_start_block = start_block;
3488 	} else {
3489 		/* new pool */
3490 		start_block = NEW_START_BLOCK;
3491 	}
3492 
3493 	(void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
3494 	    BACKUP_SLICE);
3495 
3496 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
3497 		/*
3498 		 * This shouldn't happen.  We've long since verified that this
3499 		 * is a valid device.
3500 		 */
3501 		zfs_error_aux(hdl,
3502 		    dgettext(TEXT_DOMAIN, "unable to open device"));
3503 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
3504 	}
3505 
3506 	if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
3507 		/*
3508 		 * The only way this can fail is if we run out of memory, or we
3509 		 * were unable to read the disk's capacity
3510 		 */
3511 		if (errno == ENOMEM)
3512 			(void) no_memory(hdl);
3513 
3514 		(void) close(fd);
3515 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3516 		    "unable to read disk capacity"), name);
3517 
3518 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
3519 	}
3520 
3521 	slice_size = vtoc->efi_last_u_lba + 1;
3522 	slice_size -= EFI_MIN_RESV_SIZE;
3523 	if (start_block == MAXOFFSET_T)
3524 		start_block = NEW_START_BLOCK;
3525 	slice_size -= start_block;
3526 
3527 	vtoc->efi_parts[0].p_start = start_block;
3528 	vtoc->efi_parts[0].p_size = slice_size;
3529 
3530 	/*
3531 	 * Why we use V_USR: V_BACKUP confuses users, and is considered
3532 	 * disposable by some EFI utilities (since EFI doesn't have a backup
3533 	 * slice).  V_UNASSIGNED is supposed to be used only for zero size
3534 	 * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
3535 	 * etc. were all pretty specific.  V_USR is as close to reality as we
3536 	 * can get, in the absence of V_OTHER.
3537 	 */
3538 	vtoc->efi_parts[0].p_tag = V_USR;
3539 	(void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
3540 
3541 	vtoc->efi_parts[8].p_start = slice_size + start_block;
3542 	vtoc->efi_parts[8].p_size = resv;
3543 	vtoc->efi_parts[8].p_tag = V_RESERVED;
3544 
3545 	if (efi_write(fd, vtoc) != 0) {
3546 		/*
3547 		 * Some block drivers (like pcata) may not support EFI
3548 		 * GPT labels.  Print out a helpful error message dir-
3549 		 * ecting the user to manually label the disk and give
3550 		 * a specific slice.
3551 		 */
3552 		(void) close(fd);
3553 		efi_free(vtoc);
3554 
3555 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3556 		    "try using fdisk(1M) and then provide a specific slice"));
3557 		return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
3558 	}
3559 
3560 	(void) close(fd);
3561 	efi_free(vtoc);
3562 	return (0);
3563 }
3564 
3565 static boolean_t
3566 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
3567 {
3568 	char *type;
3569 	nvlist_t **child;
3570 	uint_t children, c;
3571 
3572 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
3573 	if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
3574 	    strcmp(type, VDEV_TYPE_FILE) == 0 ||
3575 	    strcmp(type, VDEV_TYPE_LOG) == 0 ||
3576 	    strcmp(type, VDEV_TYPE_HOLE) == 0 ||
3577 	    strcmp(type, VDEV_TYPE_MISSING) == 0) {
3578 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3579 		    "vdev type '%s' is not supported"), type);
3580 		(void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
3581 		return (B_FALSE);
3582 	}
3583 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
3584 	    &child, &children) == 0) {
3585 		for (c = 0; c < children; c++) {
3586 			if (!supported_dump_vdev_type(hdl, child[c], errbuf))
3587 				return (B_FALSE);
3588 		}
3589 	}
3590 	return (B_TRUE);
3591 }
3592 
3593 /*
3594  * check if this zvol is allowable for use as a dump device; zero if
3595  * it is, > 0 if it isn't, < 0 if it isn't a zvol
3596  */
3597 int
3598 zvol_check_dump_config(char *arg)
3599 {
3600 	zpool_handle_t *zhp = NULL;
3601 	nvlist_t *config, *nvroot;
3602 	char *p, *volname;
3603 	nvlist_t **top;
3604 	uint_t toplevels;
3605 	libzfs_handle_t *hdl;
3606 	char errbuf[1024];
3607 	char poolname[ZPOOL_MAXNAMELEN];
3608 	int pathlen = strlen(ZVOL_FULL_DEV_DIR);
3609 	int ret = 1;
3610 
3611 	if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
3612 		return (-1);
3613 	}
3614 
3615 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3616 	    "dump is not supported on device '%s'"), arg);
3617 
3618 	if ((hdl = libzfs_init()) == NULL)
3619 		return (1);
3620 	libzfs_print_on_error(hdl, B_TRUE);
3621 
3622 	volname = arg + pathlen;
3623 
3624 	/* check the configuration of the pool */
3625 	if ((p = strchr(volname, '/')) == NULL) {
3626 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3627 		    "malformed dataset name"));
3628 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
3629 		return (1);
3630 	} else if (p - volname >= ZFS_MAXNAMELEN) {
3631 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3632 		    "dataset name is too long"));
3633 		(void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
3634 		return (1);
3635 	} else {
3636 		(void) strncpy(poolname, volname, p - volname);
3637 		poolname[p - volname] = '\0';
3638 	}
3639 
3640 	if ((zhp = zpool_open(hdl, poolname)) == NULL) {
3641 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3642 		    "could not open pool '%s'"), poolname);
3643 		(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
3644 		goto out;
3645 	}
3646 	config = zpool_get_config(zhp, NULL);
3647 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3648 	    &nvroot) != 0) {
3649 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3650 		    "could not obtain vdev configuration for  '%s'"), poolname);
3651 		(void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
3652 		goto out;
3653 	}
3654 
3655 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3656 	    &top, &toplevels) == 0);
3657 	if (toplevels != 1) {
3658 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3659 		    "'%s' has multiple top level vdevs"), poolname);
3660 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
3661 		goto out;
3662 	}
3663 
3664 	if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
3665 		goto out;
3666 	}
3667 	ret = 0;
3668 
3669 out:
3670 	if (zhp)
3671 		zpool_close(zhp);
3672 	libzfs_fini(hdl);
3673 	return (ret);
3674 }
3675