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