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