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