xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_pool.c (revision 6148443adeb5d3f493cee0d19110b32a0189bd41)
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(zhp->zpool_hdl, zhp->zpool_name,
1001 	    ZFS_TYPE_FILESYSTEM)) == NULL)
1002 		return (-1);
1003 
1004 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1005 
1006 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1007 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1008 		    "cannot destroy '%s'"), zhp->zpool_name);
1009 
1010 		if (errno == EROFS) {
1011 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1012 			    "one or more devices is read only"));
1013 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1014 		} else {
1015 			(void) zpool_standard_error(hdl, errno, msg);
1016 		}
1017 
1018 		if (zfp)
1019 			zfs_close(zfp);
1020 		return (-1);
1021 	}
1022 
1023 	if (zfp) {
1024 		remove_mountpoint(zfp);
1025 		zfs_close(zfp);
1026 	}
1027 
1028 	return (0);
1029 }
1030 
1031 /*
1032  * Add the given vdevs to the pool.  The caller must have already performed the
1033  * necessary verification to ensure that the vdev specification is well-formed.
1034  */
1035 int
1036 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1037 {
1038 	zfs_cmd_t zc = { 0 };
1039 	int ret;
1040 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1041 	char msg[1024];
1042 	nvlist_t **spares, **l2cache;
1043 	uint_t nspares, nl2cache;
1044 
1045 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1046 	    "cannot add to '%s'"), zhp->zpool_name);
1047 
1048 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1049 	    SPA_VERSION_SPARES &&
1050 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1051 	    &spares, &nspares) == 0) {
1052 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1053 		    "upgraded to add hot spares"));
1054 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
1055 	}
1056 
1057 	if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
1058 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
1059 		uint64_t s;
1060 
1061 		for (s = 0; s < nspares; s++) {
1062 			char *path;
1063 
1064 			if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
1065 			    &path) == 0 && pool_uses_efi(spares[s])) {
1066 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1067 				    "device '%s' contains an EFI label and "
1068 				    "cannot be used on root pools."),
1069 				    zpool_vdev_name(hdl, NULL, spares[s],
1070 				    B_FALSE));
1071 				return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1072 			}
1073 		}
1074 	}
1075 
1076 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1077 	    SPA_VERSION_L2CACHE &&
1078 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1079 	    &l2cache, &nl2cache) == 0) {
1080 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1081 		    "upgraded to add cache devices"));
1082 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
1083 	}
1084 
1085 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1086 		return (-1);
1087 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1088 
1089 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1090 		switch (errno) {
1091 		case EBUSY:
1092 			/*
1093 			 * This can happen if the user has specified the same
1094 			 * device multiple times.  We can't reliably detect this
1095 			 * until we try to add it and see we already have a
1096 			 * label.
1097 			 */
1098 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1099 			    "one or more vdevs refer to the same device"));
1100 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1101 			break;
1102 
1103 		case EOVERFLOW:
1104 			/*
1105 			 * This occurrs when one of the devices is below
1106 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1107 			 * device was the problem device since there's no
1108 			 * reliable way to determine device size from userland.
1109 			 */
1110 			{
1111 				char buf[64];
1112 
1113 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1114 
1115 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1116 				    "device is less than the minimum "
1117 				    "size (%s)"), buf);
1118 			}
1119 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1120 			break;
1121 
1122 		case ENOTSUP:
1123 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1124 			    "pool must be upgraded to add these vdevs"));
1125 			(void) zfs_error(hdl, EZFS_BADVERSION, msg);
1126 			break;
1127 
1128 		case EDOM:
1129 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1130 			    "root pool can not have multiple vdevs"
1131 			    " or separate logs"));
1132 			(void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1133 			break;
1134 
1135 		case ENOTBLK:
1136 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1137 			    "cache device must be a disk or disk slice"));
1138 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1139 			break;
1140 
1141 		default:
1142 			(void) zpool_standard_error(hdl, errno, msg);
1143 		}
1144 
1145 		ret = -1;
1146 	} else {
1147 		ret = 0;
1148 	}
1149 
1150 	zcmd_free_nvlists(&zc);
1151 
1152 	return (ret);
1153 }
1154 
1155 /*
1156  * Exports the pool from the system.  The caller must ensure that there are no
1157  * mounted datasets in the pool.
1158  */
1159 int
1160 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce)
1161 {
1162 	zfs_cmd_t zc = { 0 };
1163 	char msg[1024];
1164 
1165 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1166 	    "cannot export '%s'"), zhp->zpool_name);
1167 
1168 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1169 	zc.zc_cookie = force;
1170 	zc.zc_guid = hardforce;
1171 
1172 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1173 		switch (errno) {
1174 		case EXDEV:
1175 			zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1176 			    "use '-f' to override the following errors:\n"
1177 			    "'%s' has an active shared spare which could be"
1178 			    " used by other pools once '%s' is exported."),
1179 			    zhp->zpool_name, zhp->zpool_name);
1180 			return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1181 			    msg));
1182 		default:
1183 			return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1184 			    msg));
1185 		}
1186 	}
1187 
1188 	return (0);
1189 }
1190 
1191 int
1192 zpool_export(zpool_handle_t *zhp, boolean_t force)
1193 {
1194 	return (zpool_export_common(zhp, force, B_FALSE));
1195 }
1196 
1197 int
1198 zpool_export_force(zpool_handle_t *zhp)
1199 {
1200 	return (zpool_export_common(zhp, B_TRUE, B_TRUE));
1201 }
1202 
1203 static void
1204 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1205     nvlist_t *rbi)
1206 {
1207 	uint64_t rewindto;
1208 	int64_t loss = -1;
1209 	struct tm t;
1210 	char timestr[128];
1211 
1212 	if (!hdl->libzfs_printerr || rbi == NULL)
1213 		return;
1214 
1215 	if (nvlist_lookup_uint64(rbi, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1216 		return;
1217 	(void) nvlist_lookup_int64(rbi, ZPOOL_CONFIG_REWIND_TIME, &loss);
1218 
1219 	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1220 	    strftime(timestr, 128, 0, &t) != 0) {
1221 		if (dryrun) {
1222 			(void) printf(dgettext(TEXT_DOMAIN,
1223 			    "Would be able to return %s "
1224 			    "to its state as of %s.\n"),
1225 			    name, timestr);
1226 		} else {
1227 			(void) printf(dgettext(TEXT_DOMAIN,
1228 			    "Pool %s returned to its state as of %s.\n"),
1229 			    name, timestr);
1230 		}
1231 		if (loss > 120) {
1232 			(void) printf(dgettext(TEXT_DOMAIN,
1233 			    "%s approximately %lld "),
1234 			    dryrun ? "Would discard" : "Discarded",
1235 			    (loss + 30) / 60);
1236 			(void) printf(dgettext(TEXT_DOMAIN,
1237 			    "minutes of transactions.\n"));
1238 		} else if (loss > 0) {
1239 			(void) printf(dgettext(TEXT_DOMAIN,
1240 			    "%s approximately %lld "),
1241 			    dryrun ? "Would discard" : "Discarded", loss);
1242 			(void) printf(dgettext(TEXT_DOMAIN,
1243 			    "seconds of transactions.\n"));
1244 		}
1245 	}
1246 }
1247 
1248 void
1249 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1250     nvlist_t *config)
1251 {
1252 	int64_t loss = -1;
1253 	uint64_t edata = UINT64_MAX;
1254 	uint64_t rewindto;
1255 	struct tm t;
1256 	char timestr[128];
1257 
1258 	if (!hdl->libzfs_printerr)
1259 		return;
1260 
1261 	if (reason >= 0)
1262 		(void) printf(dgettext(TEXT_DOMAIN, "action: "));
1263 	else
1264 		(void) printf(dgettext(TEXT_DOMAIN, "\t"));
1265 
1266 	/* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1267 	if (nvlist_lookup_uint64(config,
1268 	    ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1269 		goto no_info;
1270 
1271 	(void) nvlist_lookup_int64(config, ZPOOL_CONFIG_REWIND_TIME, &loss);
1272 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1273 	    &edata);
1274 
1275 	(void) printf(dgettext(TEXT_DOMAIN,
1276 	    "Recovery is possible, but will result in some data loss.\n"));
1277 
1278 	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1279 	    strftime(timestr, 128, 0, &t) != 0) {
1280 		(void) printf(dgettext(TEXT_DOMAIN,
1281 		    "\tReturning the pool to its state as of %s\n"
1282 		    "\tshould correct the problem.  "),
1283 		    timestr);
1284 	} else {
1285 		(void) printf(dgettext(TEXT_DOMAIN,
1286 		    "\tReverting the pool to an earlier state "
1287 		    "should correct the problem.\n\t"));
1288 	}
1289 
1290 	if (loss > 120) {
1291 		(void) printf(dgettext(TEXT_DOMAIN,
1292 		    "Approximately %lld minutes of data\n"
1293 		    "\tmust be discarded, irreversibly.  "), (loss + 30) / 60);
1294 	} else if (loss > 0) {
1295 		(void) printf(dgettext(TEXT_DOMAIN,
1296 		    "Approximately %lld seconds of data\n"
1297 		    "\tmust be discarded, irreversibly.  "), loss);
1298 	}
1299 	if (edata != 0 && edata != UINT64_MAX) {
1300 		if (edata == 1) {
1301 			(void) printf(dgettext(TEXT_DOMAIN,
1302 			    "After rewind, at least\n"
1303 			    "\tone persistent user-data error will remain.  "));
1304 		} else {
1305 			(void) printf(dgettext(TEXT_DOMAIN,
1306 			    "After rewind, several\n"
1307 			    "\tpersistent user-data errors will remain.  "));
1308 		}
1309 	}
1310 	(void) printf(dgettext(TEXT_DOMAIN,
1311 	    "Recovery can be attempted\n\tby executing 'zpool %s -F %s'.  "),
1312 	    reason >= 0 ? "clear" : "import", name);
1313 
1314 	(void) printf(dgettext(TEXT_DOMAIN,
1315 	    "A scrub of the pool\n"
1316 	    "\tis strongly recommended after recovery.\n"));
1317 	return;
1318 
1319 no_info:
1320 	(void) printf(dgettext(TEXT_DOMAIN,
1321 	    "Destroy and re-create the pool from\n\ta backup source.\n"));
1322 }
1323 
1324 /*
1325  * zpool_import() is a contracted interface. Should be kept the same
1326  * if possible.
1327  *
1328  * Applications should use zpool_import_props() to import a pool with
1329  * new properties value to be set.
1330  */
1331 int
1332 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1333     char *altroot)
1334 {
1335 	nvlist_t *props = NULL;
1336 	int ret;
1337 
1338 	if (altroot != NULL) {
1339 		if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1340 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
1341 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1342 			    newname));
1343 		}
1344 
1345 		if (nvlist_add_string(props,
1346 		    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1347 		    nvlist_add_string(props,
1348 		    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1349 			nvlist_free(props);
1350 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
1351 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1352 			    newname));
1353 		}
1354 	}
1355 
1356 	ret = zpool_import_props(hdl, config, newname, props, B_FALSE);
1357 	if (props)
1358 		nvlist_free(props);
1359 	return (ret);
1360 }
1361 
1362 /*
1363  * Import the given pool using the known configuration and a list of
1364  * properties to be set. The configuration should have come from
1365  * zpool_find_import(). The 'newname' parameters control whether the pool
1366  * is imported with a different name.
1367  */
1368 int
1369 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1370     nvlist_t *props, boolean_t importfaulted)
1371 {
1372 	zfs_cmd_t zc = { 0 };
1373 	zpool_rewind_policy_t policy;
1374 	nvlist_t *nvi = NULL;
1375 	char *thename;
1376 	char *origname;
1377 	uint64_t returned_size;
1378 	int ret;
1379 	char errbuf[1024];
1380 
1381 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1382 	    &origname) == 0);
1383 
1384 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1385 	    "cannot import pool '%s'"), origname);
1386 
1387 	if (newname != NULL) {
1388 		if (!zpool_name_valid(hdl, B_FALSE, newname))
1389 			return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1390 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1391 			    newname));
1392 		thename = (char *)newname;
1393 	} else {
1394 		thename = origname;
1395 	}
1396 
1397 	if (props) {
1398 		uint64_t version;
1399 
1400 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1401 		    &version) == 0);
1402 
1403 		if ((props = zpool_valid_proplist(hdl, origname,
1404 		    props, version, B_TRUE, errbuf)) == NULL) {
1405 			return (-1);
1406 		} else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1407 			nvlist_free(props);
1408 			return (-1);
1409 		}
1410 	}
1411 
1412 	(void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1413 
1414 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1415 	    &zc.zc_guid) == 0);
1416 
1417 	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1418 		nvlist_free(props);
1419 		return (-1);
1420 	}
1421 	returned_size =  zc.zc_nvlist_conf_size + 512;
1422 	if (zcmd_alloc_dst_nvlist(hdl, &zc, returned_size) != 0) {
1423 		nvlist_free(props);
1424 		return (-1);
1425 	}
1426 
1427 	zc.zc_cookie = (uint64_t)importfaulted;
1428 	ret = 0;
1429 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) {
1430 		char desc[1024];
1431 
1432 		(void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
1433 		zpool_get_rewind_policy(config, &policy);
1434 		/*
1435 		 * Dry-run failed, but we print out what success
1436 		 * looks like if we found a best txg
1437 		 */
1438 		if ((policy.zrp_request & ZPOOL_TRY_REWIND) && nvi) {
1439 			zpool_rewind_exclaim(hdl, newname ? origname : thename,
1440 			    B_TRUE, nvi);
1441 			nvlist_free(nvi);
1442 			return (-1);
1443 		}
1444 
1445 		if (newname == NULL)
1446 			(void) snprintf(desc, sizeof (desc),
1447 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1448 			    thename);
1449 		else
1450 			(void) snprintf(desc, sizeof (desc),
1451 			    dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1452 			    origname, thename);
1453 
1454 		switch (errno) {
1455 		case ENOTSUP:
1456 			/*
1457 			 * Unsupported version.
1458 			 */
1459 			(void) zfs_error(hdl, EZFS_BADVERSION, desc);
1460 			break;
1461 
1462 		case EINVAL:
1463 			(void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1464 			break;
1465 
1466 		case EROFS:
1467 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1468 			    "one or more devices is read only"));
1469 			(void) zfs_error(hdl, EZFS_BADDEV, desc);
1470 			break;
1471 
1472 		default:
1473 			(void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
1474 			(void) zpool_standard_error(hdl, errno, desc);
1475 			zpool_explain_recover(hdl,
1476 			    newname ? origname : thename, -errno, nvi);
1477 			nvlist_free(nvi);
1478 			break;
1479 		}
1480 
1481 		ret = -1;
1482 	} else {
1483 		zpool_handle_t *zhp;
1484 
1485 		/*
1486 		 * This should never fail, but play it safe anyway.
1487 		 */
1488 		if (zpool_open_silent(hdl, thename, &zhp) != 0)
1489 			ret = -1;
1490 		else if (zhp != NULL)
1491 			zpool_close(zhp);
1492 		(void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
1493 		zpool_get_rewind_policy(config, &policy);
1494 		if (policy.zrp_request &
1495 		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1496 			zpool_rewind_exclaim(hdl, newname ? origname : thename,
1497 			    ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
1498 			    nvi);
1499 		}
1500 		nvlist_free(nvi);
1501 		return (0);
1502 	}
1503 
1504 	zcmd_free_nvlists(&zc);
1505 	nvlist_free(props);
1506 
1507 	return (ret);
1508 }
1509 
1510 /*
1511  * Scan the pool.
1512  */
1513 int
1514 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func)
1515 {
1516 	zfs_cmd_t zc = { 0 };
1517 	char msg[1024];
1518 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1519 
1520 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1521 	zc.zc_cookie = func;
1522 
1523 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 ||
1524 	    (errno == ENOENT && func != POOL_SCAN_NONE))
1525 		return (0);
1526 
1527 	if (func == POOL_SCAN_SCRUB) {
1528 		(void) snprintf(msg, sizeof (msg),
1529 		    dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1530 	} else if (func == POOL_SCAN_NONE) {
1531 		(void) snprintf(msg, sizeof (msg),
1532 		    dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
1533 		    zc.zc_name);
1534 	} else {
1535 		assert(!"unexpected result");
1536 	}
1537 
1538 	if (errno == EBUSY) {
1539 		nvlist_t *nvroot;
1540 		pool_scan_stat_t *ps = NULL;
1541 		uint_t psc;
1542 
1543 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
1544 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1545 		(void) nvlist_lookup_uint64_array(nvroot,
1546 		    ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
1547 		if (ps && ps->pss_func == POOL_SCAN_SCRUB)
1548 			return (zfs_error(hdl, EZFS_SCRUBBING, msg));
1549 		else
1550 			return (zfs_error(hdl, EZFS_RESILVERING, msg));
1551 	} else if (errno == ENOENT) {
1552 		return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
1553 	} else {
1554 		return (zpool_standard_error(hdl, errno, msg));
1555 	}
1556 }
1557 
1558 /*
1559  * This provides a very minimal check whether a given string is likely a
1560  * c#t#d# style string.  Users of this are expected to do their own
1561  * verification of the s# part.
1562  */
1563 #define	CTD_CHECK(str)  (str && str[0] == 'c' && isdigit(str[1]))
1564 
1565 /*
1566  * More elaborate version for ones which may start with "/dev/dsk/"
1567  * and the like.
1568  */
1569 static int
1570 ctd_check_path(char *str) {
1571 	/*
1572 	 * If it starts with a slash, check the last component.
1573 	 */
1574 	if (str && str[0] == '/') {
1575 		char *tmp = strrchr(str, '/');
1576 
1577 		/*
1578 		 * If it ends in "/old", check the second-to-last
1579 		 * component of the string instead.
1580 		 */
1581 		if (tmp != str && strcmp(tmp, "/old") == 0) {
1582 			for (tmp--; *tmp != '/'; tmp--)
1583 				;
1584 		}
1585 		str = tmp + 1;
1586 	}
1587 	return (CTD_CHECK(str));
1588 }
1589 
1590 /*
1591  * Find a vdev that matches the search criteria specified. We use the
1592  * the nvpair name to determine how we should look for the device.
1593  * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1594  * spare; but FALSE if its an INUSE spare.
1595  */
1596 static nvlist_t *
1597 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1598     boolean_t *l2cache, boolean_t *log)
1599 {
1600 	uint_t c, children;
1601 	nvlist_t **child;
1602 	nvlist_t *ret;
1603 	uint64_t is_log;
1604 	char *srchkey;
1605 	nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1606 
1607 	/* Nothing to look for */
1608 	if (search == NULL || pair == NULL)
1609 		return (NULL);
1610 
1611 	/* Obtain the key we will use to search */
1612 	srchkey = nvpair_name(pair);
1613 
1614 	switch (nvpair_type(pair)) {
1615 	case DATA_TYPE_UINT64: {
1616 		uint64_t srchval, theguid, present;
1617 
1618 		verify(nvpair_value_uint64(pair, &srchval) == 0);
1619 		if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1620 			if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1621 			    &present) == 0) {
1622 				/*
1623 				 * If the device has never been present since
1624 				 * import, the only reliable way to match the
1625 				 * vdev is by GUID.
1626 				 */
1627 				verify(nvlist_lookup_uint64(nv,
1628 				    ZPOOL_CONFIG_GUID, &theguid) == 0);
1629 				if (theguid == srchval)
1630 					return (nv);
1631 			}
1632 		}
1633 		break;
1634 	}
1635 
1636 	case DATA_TYPE_STRING: {
1637 		char *srchval, *val;
1638 
1639 		verify(nvpair_value_string(pair, &srchval) == 0);
1640 		if (nvlist_lookup_string(nv, srchkey, &val) != 0)
1641 			break;
1642 
1643 		/*
1644 		 * Search for the requested value. Special cases:
1645 		 *
1646 		 * - ZPOOL_CONFIG_PATH for whole disk entries.  These end in
1647 		 *   "s0" or "s0/old".  The "s0" part is hidden from the user,
1648 		 *   but included in the string, so this matches around it.
1649 		 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
1650 		 *
1651 		 * Otherwise, all other searches are simple string compares.
1652 		 */
1653 		if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
1654 		    ctd_check_path(val)) {
1655 			uint64_t wholedisk = 0;
1656 
1657 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1658 			    &wholedisk);
1659 			if (wholedisk) {
1660 				int slen = strlen(srchval);
1661 				int vlen = strlen(val);
1662 
1663 				if (slen != vlen - 2)
1664 					break;
1665 
1666 				/*
1667 				 * make_leaf_vdev() should only set
1668 				 * wholedisk for ZPOOL_CONFIG_PATHs which
1669 				 * will include "/dev/dsk/", giving plenty of
1670 				 * room for the indices used next.
1671 				 */
1672 				ASSERT(vlen >= 6);
1673 
1674 				/*
1675 				 * strings identical except trailing "s0"
1676 				 */
1677 				if (strcmp(&val[vlen - 2], "s0") == 0 &&
1678 				    strncmp(srchval, val, slen) == 0)
1679 					return (nv);
1680 
1681 				/*
1682 				 * strings identical except trailing "s0/old"
1683 				 */
1684 				if (strcmp(&val[vlen - 6], "s0/old") == 0 &&
1685 				    strcmp(&srchval[slen - 4], "/old") == 0 &&
1686 				    strncmp(srchval, val, slen - 4) == 0)
1687 					return (nv);
1688 
1689 				break;
1690 			}
1691 		} else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
1692 			char *type, *idx, *end, *p;
1693 			uint64_t id, vdev_id;
1694 
1695 			/*
1696 			 * Determine our vdev type, keeping in mind
1697 			 * that the srchval is composed of a type and
1698 			 * vdev id pair (i.e. mirror-4).
1699 			 */
1700 			if ((type = strdup(srchval)) == NULL)
1701 				return (NULL);
1702 
1703 			if ((p = strrchr(type, '-')) == NULL) {
1704 				free(type);
1705 				break;
1706 			}
1707 			idx = p + 1;
1708 			*p = '\0';
1709 
1710 			/*
1711 			 * If the types don't match then keep looking.
1712 			 */
1713 			if (strncmp(val, type, strlen(val)) != 0) {
1714 				free(type);
1715 				break;
1716 			}
1717 
1718 			verify(strncmp(type, VDEV_TYPE_RAIDZ,
1719 			    strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1720 			    strncmp(type, VDEV_TYPE_MIRROR,
1721 			    strlen(VDEV_TYPE_MIRROR)) == 0);
1722 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
1723 			    &id) == 0);
1724 
1725 			errno = 0;
1726 			vdev_id = strtoull(idx, &end, 10);
1727 
1728 			free(type);
1729 			if (errno != 0)
1730 				return (NULL);
1731 
1732 			/*
1733 			 * Now verify that we have the correct vdev id.
1734 			 */
1735 			if (vdev_id == id)
1736 				return (nv);
1737 		}
1738 
1739 		/*
1740 		 * Common case
1741 		 */
1742 		if (strcmp(srchval, val) == 0)
1743 			return (nv);
1744 		break;
1745 	}
1746 
1747 	default:
1748 		break;
1749 	}
1750 
1751 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1752 	    &child, &children) != 0)
1753 		return (NULL);
1754 
1755 	for (c = 0; c < children; c++) {
1756 		if ((ret = vdev_to_nvlist_iter(child[c], search,
1757 		    avail_spare, l2cache, NULL)) != NULL) {
1758 			/*
1759 			 * The 'is_log' value is only set for the toplevel
1760 			 * vdev, not the leaf vdevs.  So we always lookup the
1761 			 * log device from the root of the vdev tree (where
1762 			 * 'log' is non-NULL).
1763 			 */
1764 			if (log != NULL &&
1765 			    nvlist_lookup_uint64(child[c],
1766 			    ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
1767 			    is_log) {
1768 				*log = B_TRUE;
1769 			}
1770 			return (ret);
1771 		}
1772 	}
1773 
1774 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1775 	    &child, &children) == 0) {
1776 		for (c = 0; c < children; c++) {
1777 			if ((ret = vdev_to_nvlist_iter(child[c], search,
1778 			    avail_spare, l2cache, NULL)) != NULL) {
1779 				*avail_spare = B_TRUE;
1780 				return (ret);
1781 			}
1782 		}
1783 	}
1784 
1785 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1786 	    &child, &children) == 0) {
1787 		for (c = 0; c < children; c++) {
1788 			if ((ret = vdev_to_nvlist_iter(child[c], search,
1789 			    avail_spare, l2cache, NULL)) != NULL) {
1790 				*l2cache = B_TRUE;
1791 				return (ret);
1792 			}
1793 		}
1794 	}
1795 
1796 	return (NULL);
1797 }
1798 
1799 /*
1800  * Given a physical path (minus the "/devices" prefix), find the
1801  * associated vdev.
1802  */
1803 nvlist_t *
1804 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
1805     boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
1806 {
1807 	nvlist_t *search, *nvroot, *ret;
1808 
1809 	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1810 	verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
1811 
1812 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1813 	    &nvroot) == 0);
1814 
1815 	*avail_spare = B_FALSE;
1816 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1817 	nvlist_free(search);
1818 
1819 	return (ret);
1820 }
1821 
1822 /*
1823  * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
1824  */
1825 boolean_t
1826 zpool_vdev_is_interior(const char *name)
1827 {
1828 	if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1829 	    strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
1830 		return (B_TRUE);
1831 	return (B_FALSE);
1832 }
1833 
1834 nvlist_t *
1835 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
1836     boolean_t *l2cache, boolean_t *log)
1837 {
1838 	char buf[MAXPATHLEN];
1839 	char *end;
1840 	nvlist_t *nvroot, *search, *ret;
1841 	uint64_t guid;
1842 
1843 	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1844 
1845 	guid = strtoull(path, &end, 10);
1846 	if (guid != 0 && *end == '\0') {
1847 		verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
1848 	} else if (zpool_vdev_is_interior(path)) {
1849 		verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
1850 	} else if (path[0] != '/') {
1851 		(void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path);
1852 		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
1853 	} else {
1854 		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
1855 	}
1856 
1857 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1858 	    &nvroot) == 0);
1859 
1860 	*avail_spare = B_FALSE;
1861 	*l2cache = B_FALSE;
1862 	if (log != NULL)
1863 		*log = B_FALSE;
1864 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1865 	nvlist_free(search);
1866 
1867 	return (ret);
1868 }
1869 
1870 static int
1871 vdev_online(nvlist_t *nv)
1872 {
1873 	uint64_t ival;
1874 
1875 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
1876 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
1877 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
1878 		return (0);
1879 
1880 	return (1);
1881 }
1882 
1883 /*
1884  * Helper function for zpool_get_physpaths().
1885  */
1886 static int
1887 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
1888     size_t *bytes_written)
1889 {
1890 	size_t bytes_left, pos, rsz;
1891 	char *tmppath;
1892 	const char *format;
1893 
1894 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
1895 	    &tmppath) != 0)
1896 		return (EZFS_NODEVICE);
1897 
1898 	pos = *bytes_written;
1899 	bytes_left = physpath_size - pos;
1900 	format = (pos == 0) ? "%s" : " %s";
1901 
1902 	rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
1903 	*bytes_written += rsz;
1904 
1905 	if (rsz >= bytes_left) {
1906 		/* if physpath was not copied properly, clear it */
1907 		if (bytes_left != 0) {
1908 			physpath[pos] = 0;
1909 		}
1910 		return (EZFS_NOSPC);
1911 	}
1912 	return (0);
1913 }
1914 
1915 static int
1916 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
1917     size_t *rsz, boolean_t is_spare)
1918 {
1919 	char *type;
1920 	int ret;
1921 
1922 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
1923 		return (EZFS_INVALCONFIG);
1924 
1925 	if (strcmp(type, VDEV_TYPE_DISK) == 0) {
1926 		/*
1927 		 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
1928 		 * For a spare vdev, we only want to boot from the active
1929 		 * spare device.
1930 		 */
1931 		if (is_spare) {
1932 			uint64_t spare = 0;
1933 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
1934 			    &spare);
1935 			if (!spare)
1936 				return (EZFS_INVALCONFIG);
1937 		}
1938 
1939 		if (vdev_online(nv)) {
1940 			if ((ret = vdev_get_one_physpath(nv, physpath,
1941 			    phypath_size, rsz)) != 0)
1942 				return (ret);
1943 		}
1944 	} else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
1945 	    strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
1946 	    (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
1947 		nvlist_t **child;
1948 		uint_t count;
1949 		int i, ret;
1950 
1951 		if (nvlist_lookup_nvlist_array(nv,
1952 		    ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
1953 			return (EZFS_INVALCONFIG);
1954 
1955 		for (i = 0; i < count; i++) {
1956 			ret = vdev_get_physpaths(child[i], physpath,
1957 			    phypath_size, rsz, is_spare);
1958 			if (ret == EZFS_NOSPC)
1959 				return (ret);
1960 		}
1961 	}
1962 
1963 	return (EZFS_POOL_INVALARG);
1964 }
1965 
1966 /*
1967  * Get phys_path for a root pool config.
1968  * Return 0 on success; non-zero on failure.
1969  */
1970 static int
1971 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
1972 {
1973 	size_t rsz;
1974 	nvlist_t *vdev_root;
1975 	nvlist_t **child;
1976 	uint_t count;
1977 	char *type;
1978 
1979 	rsz = 0;
1980 
1981 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1982 	    &vdev_root) != 0)
1983 		return (EZFS_INVALCONFIG);
1984 
1985 	if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
1986 	    nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
1987 	    &child, &count) != 0)
1988 		return (EZFS_INVALCONFIG);
1989 
1990 	/*
1991 	 * root pool can not have EFI labeled disks and can only have
1992 	 * a single top-level vdev.
1993 	 */
1994 	if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
1995 	    pool_uses_efi(vdev_root))
1996 		return (EZFS_POOL_INVALARG);
1997 
1998 	(void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
1999 	    B_FALSE);
2000 
2001 	/* No online devices */
2002 	if (rsz == 0)
2003 		return (EZFS_NODEVICE);
2004 
2005 	return (0);
2006 }
2007 
2008 /*
2009  * Get phys_path for a root pool
2010  * Return 0 on success; non-zero on failure.
2011  */
2012 int
2013 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2014 {
2015 	return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2016 	    phypath_size));
2017 }
2018 
2019 /*
2020  * If the device has being dynamically expanded then we need to relabel
2021  * the disk to use the new unallocated space.
2022  */
2023 static int
2024 zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
2025 {
2026 	char path[MAXPATHLEN];
2027 	char errbuf[1024];
2028 	int fd, error;
2029 	int (*_efi_use_whole_disk)(int);
2030 
2031 	if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
2032 	    "efi_use_whole_disk")) == NULL)
2033 		return (-1);
2034 
2035 	(void) snprintf(path, sizeof (path), "%s/%s", RDISK_ROOT, name);
2036 
2037 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2038 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2039 		    "relabel '%s': unable to open device"), name);
2040 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2041 	}
2042 
2043 	/*
2044 	 * It's possible that we might encounter an error if the device
2045 	 * does not have any unallocated space left. If so, we simply
2046 	 * ignore that error and continue on.
2047 	 */
2048 	error = _efi_use_whole_disk(fd);
2049 	(void) close(fd);
2050 	if (error && error != VT_ENOSPC) {
2051 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2052 		    "relabel '%s': unable to read disk capacity"), name);
2053 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2054 	}
2055 	return (0);
2056 }
2057 
2058 /*
2059  * Bring the specified vdev online.   The 'flags' parameter is a set of the
2060  * ZFS_ONLINE_* flags.
2061  */
2062 int
2063 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2064     vdev_state_t *newstate)
2065 {
2066 	zfs_cmd_t zc = { 0 };
2067 	char msg[1024];
2068 	nvlist_t *tgt;
2069 	boolean_t avail_spare, l2cache, islog;
2070 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2071 
2072 	if (flags & ZFS_ONLINE_EXPAND) {
2073 		(void) snprintf(msg, sizeof (msg),
2074 		    dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2075 	} else {
2076 		(void) snprintf(msg, sizeof (msg),
2077 		    dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2078 	}
2079 
2080 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2081 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2082 	    &islog)) == NULL)
2083 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2084 
2085 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2086 
2087 	if (avail_spare)
2088 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
2089 
2090 	if (flags & ZFS_ONLINE_EXPAND ||
2091 	    zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2092 		char *pathname = NULL;
2093 		uint64_t wholedisk = 0;
2094 
2095 		(void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2096 		    &wholedisk);
2097 		verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
2098 		    &pathname) == 0);
2099 
2100 		/*
2101 		 * XXX - L2ARC 1.0 devices can't support expansion.
2102 		 */
2103 		if (l2cache) {
2104 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2105 			    "cannot expand cache devices"));
2106 			return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2107 		}
2108 
2109 		if (wholedisk) {
2110 			pathname += strlen(DISK_ROOT) + 1;
2111 			(void) zpool_relabel_disk(zhp->zpool_hdl, pathname);
2112 		}
2113 	}
2114 
2115 	zc.zc_cookie = VDEV_STATE_ONLINE;
2116 	zc.zc_obj = flags;
2117 
2118 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2119 		if (errno == EINVAL) {
2120 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2121 			    "from this pool into a new one.  Use '%s' "
2122 			    "instead"), "zpool detach");
2123 			return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2124 		}
2125 		return (zpool_standard_error(hdl, errno, msg));
2126 	}
2127 
2128 	*newstate = zc.zc_cookie;
2129 	return (0);
2130 }
2131 
2132 /*
2133  * Take the specified vdev offline
2134  */
2135 int
2136 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2137 {
2138 	zfs_cmd_t zc = { 0 };
2139 	char msg[1024];
2140 	nvlist_t *tgt;
2141 	boolean_t avail_spare, l2cache;
2142 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2143 
2144 	(void) snprintf(msg, sizeof (msg),
2145 	    dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2146 
2147 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2148 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2149 	    NULL)) == NULL)
2150 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2151 
2152 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2153 
2154 	if (avail_spare)
2155 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
2156 
2157 	zc.zc_cookie = VDEV_STATE_OFFLINE;
2158 	zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2159 
2160 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2161 		return (0);
2162 
2163 	switch (errno) {
2164 	case EBUSY:
2165 
2166 		/*
2167 		 * There are no other replicas of this device.
2168 		 */
2169 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2170 
2171 	case EEXIST:
2172 		/*
2173 		 * The log device has unplayed logs
2174 		 */
2175 		return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2176 
2177 	default:
2178 		return (zpool_standard_error(hdl, errno, msg));
2179 	}
2180 }
2181 
2182 /*
2183  * Mark the given vdev faulted.
2184  */
2185 int
2186 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2187 {
2188 	zfs_cmd_t zc = { 0 };
2189 	char msg[1024];
2190 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2191 
2192 	(void) snprintf(msg, sizeof (msg),
2193 	    dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
2194 
2195 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2196 	zc.zc_guid = guid;
2197 	zc.zc_cookie = VDEV_STATE_FAULTED;
2198 	zc.zc_obj = aux;
2199 
2200 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2201 		return (0);
2202 
2203 	switch (errno) {
2204 	case EBUSY:
2205 
2206 		/*
2207 		 * There are no other replicas of this device.
2208 		 */
2209 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2210 
2211 	default:
2212 		return (zpool_standard_error(hdl, errno, msg));
2213 	}
2214 
2215 }
2216 
2217 /*
2218  * Mark the given vdev degraded.
2219  */
2220 int
2221 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2222 {
2223 	zfs_cmd_t zc = { 0 };
2224 	char msg[1024];
2225 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2226 
2227 	(void) snprintf(msg, sizeof (msg),
2228 	    dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
2229 
2230 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2231 	zc.zc_guid = guid;
2232 	zc.zc_cookie = VDEV_STATE_DEGRADED;
2233 	zc.zc_obj = aux;
2234 
2235 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2236 		return (0);
2237 
2238 	return (zpool_standard_error(hdl, errno, msg));
2239 }
2240 
2241 /*
2242  * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2243  * a hot spare.
2244  */
2245 static boolean_t
2246 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2247 {
2248 	nvlist_t **child;
2249 	uint_t c, children;
2250 	char *type;
2251 
2252 	if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2253 	    &children) == 0) {
2254 		verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2255 		    &type) == 0);
2256 
2257 		if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2258 		    children == 2 && child[which] == tgt)
2259 			return (B_TRUE);
2260 
2261 		for (c = 0; c < children; c++)
2262 			if (is_replacing_spare(child[c], tgt, which))
2263 				return (B_TRUE);
2264 	}
2265 
2266 	return (B_FALSE);
2267 }
2268 
2269 /*
2270  * Attach new_disk (fully described by nvroot) to old_disk.
2271  * If 'replacing' is specified, the new disk will replace the old one.
2272  */
2273 int
2274 zpool_vdev_attach(zpool_handle_t *zhp,
2275     const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2276 {
2277 	zfs_cmd_t zc = { 0 };
2278 	char msg[1024];
2279 	int ret;
2280 	nvlist_t *tgt;
2281 	boolean_t avail_spare, l2cache, islog;
2282 	uint64_t val;
2283 	char *path, *newname;
2284 	nvlist_t **child;
2285 	uint_t children;
2286 	nvlist_t *config_root;
2287 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2288 	boolean_t rootpool = pool_is_bootable(zhp);
2289 
2290 	if (replacing)
2291 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2292 		    "cannot replace %s with %s"), old_disk, new_disk);
2293 	else
2294 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2295 		    "cannot attach %s to %s"), new_disk, old_disk);
2296 
2297 	/*
2298 	 * If this is a root pool, make sure that we're not attaching an
2299 	 * EFI labeled device.
2300 	 */
2301 	if (rootpool && pool_uses_efi(nvroot)) {
2302 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2303 		    "EFI labeled devices are not supported on root pools."));
2304 		return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
2305 	}
2306 
2307 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2308 	if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2309 	    &islog)) == 0)
2310 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2311 
2312 	if (avail_spare)
2313 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
2314 
2315 	if (l2cache)
2316 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2317 
2318 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2319 	zc.zc_cookie = replacing;
2320 
2321 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2322 	    &child, &children) != 0 || children != 1) {
2323 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2324 		    "new device must be a single disk"));
2325 		return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2326 	}
2327 
2328 	verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2329 	    ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2330 
2331 	if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
2332 		return (-1);
2333 
2334 	/*
2335 	 * If the target is a hot spare that has been swapped in, we can only
2336 	 * replace it with another hot spare.
2337 	 */
2338 	if (replacing &&
2339 	    nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2340 	    (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2341 	    NULL) == NULL || !avail_spare) &&
2342 	    is_replacing_spare(config_root, tgt, 1)) {
2343 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2344 		    "can only be replaced by another hot spare"));
2345 		free(newname);
2346 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
2347 	}
2348 
2349 	/*
2350 	 * If we are attempting to replace a spare, it canot be applied to an
2351 	 * already spared device.
2352 	 */
2353 	if (replacing &&
2354 	    nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 &&
2355 	    zpool_find_vdev(zhp, newname, &avail_spare,
2356 	    &l2cache, NULL) != NULL && avail_spare &&
2357 	    is_replacing_spare(config_root, tgt, 0)) {
2358 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2359 		    "device has already been replaced with a spare"));
2360 		free(newname);
2361 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
2362 	}
2363 
2364 	free(newname);
2365 
2366 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2367 		return (-1);
2368 
2369 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2370 
2371 	zcmd_free_nvlists(&zc);
2372 
2373 	if (ret == 0) {
2374 		if (rootpool) {
2375 			/*
2376 			 * XXX need a better way to prevent user from
2377 			 * booting up a half-baked vdev.
2378 			 */
2379 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2380 			    "sure to wait until resilver is done "
2381 			    "before rebooting.\n"));
2382 		}
2383 		return (0);
2384 	}
2385 
2386 	switch (errno) {
2387 	case ENOTSUP:
2388 		/*
2389 		 * Can't attach to or replace this type of vdev.
2390 		 */
2391 		if (replacing) {
2392 			if (islog)
2393 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2394 				    "cannot replace a log with a spare"));
2395 			else
2396 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2397 				    "cannot replace a replacing device"));
2398 		} else {
2399 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2400 			    "can only attach to mirrors and top-level "
2401 			    "disks"));
2402 		}
2403 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
2404 		break;
2405 
2406 	case EINVAL:
2407 		/*
2408 		 * The new device must be a single disk.
2409 		 */
2410 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2411 		    "new device must be a single disk"));
2412 		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2413 		break;
2414 
2415 	case EBUSY:
2416 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
2417 		    new_disk);
2418 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2419 		break;
2420 
2421 	case EOVERFLOW:
2422 		/*
2423 		 * The new device is too small.
2424 		 */
2425 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2426 		    "device is too small"));
2427 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2428 		break;
2429 
2430 	case EDOM:
2431 		/*
2432 		 * The new device has a different alignment requirement.
2433 		 */
2434 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2435 		    "devices have different sector alignment"));
2436 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2437 		break;
2438 
2439 	case ENAMETOOLONG:
2440 		/*
2441 		 * The resulting top-level vdev spec won't fit in the label.
2442 		 */
2443 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2444 		break;
2445 
2446 	default:
2447 		(void) zpool_standard_error(hdl, errno, msg);
2448 	}
2449 
2450 	return (-1);
2451 }
2452 
2453 /*
2454  * Detach the specified device.
2455  */
2456 int
2457 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2458 {
2459 	zfs_cmd_t zc = { 0 };
2460 	char msg[1024];
2461 	nvlist_t *tgt;
2462 	boolean_t avail_spare, l2cache;
2463 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2464 
2465 	(void) snprintf(msg, sizeof (msg),
2466 	    dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2467 
2468 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2469 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2470 	    NULL)) == 0)
2471 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2472 
2473 	if (avail_spare)
2474 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
2475 
2476 	if (l2cache)
2477 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2478 
2479 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2480 
2481 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2482 		return (0);
2483 
2484 	switch (errno) {
2485 
2486 	case ENOTSUP:
2487 		/*
2488 		 * Can't detach from this type of vdev.
2489 		 */
2490 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2491 		    "applicable to mirror and replacing vdevs"));
2492 		(void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg);
2493 		break;
2494 
2495 	case EBUSY:
2496 		/*
2497 		 * There are no other replicas of this device.
2498 		 */
2499 		(void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2500 		break;
2501 
2502 	default:
2503 		(void) zpool_standard_error(hdl, errno, msg);
2504 	}
2505 
2506 	return (-1);
2507 }
2508 
2509 /*
2510  * Find a mirror vdev in the source nvlist.
2511  *
2512  * The mchild array contains a list of disks in one of the top-level mirrors
2513  * of the source pool.  The schild array contains a list of disks that the
2514  * user specified on the command line.  We loop over the mchild array to
2515  * see if any entry in the schild array matches.
2516  *
2517  * If a disk in the mchild array is found in the schild array, we return
2518  * the index of that entry.  Otherwise we return -1.
2519  */
2520 static int
2521 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2522     nvlist_t **schild, uint_t schildren)
2523 {
2524 	uint_t mc;
2525 
2526 	for (mc = 0; mc < mchildren; mc++) {
2527 		uint_t sc;
2528 		char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2529 		    mchild[mc], B_FALSE);
2530 
2531 		for (sc = 0; sc < schildren; sc++) {
2532 			char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2533 			    schild[sc], B_FALSE);
2534 			boolean_t result = (strcmp(mpath, spath) == 0);
2535 
2536 			free(spath);
2537 			if (result) {
2538 				free(mpath);
2539 				return (mc);
2540 			}
2541 		}
2542 
2543 		free(mpath);
2544 	}
2545 
2546 	return (-1);
2547 }
2548 
2549 /*
2550  * Split a mirror pool.  If newroot points to null, then a new nvlist
2551  * is generated and it is the responsibility of the caller to free it.
2552  */
2553 int
2554 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
2555     nvlist_t *props, splitflags_t flags)
2556 {
2557 	zfs_cmd_t zc = { 0 };
2558 	char msg[1024];
2559 	nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
2560 	nvlist_t **varray = NULL, *zc_props = NULL;
2561 	uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
2562 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2563 	uint64_t vers;
2564 	boolean_t freelist = B_FALSE, memory_err = B_TRUE;
2565 	int retval = 0;
2566 
2567 	(void) snprintf(msg, sizeof (msg),
2568 	    dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
2569 
2570 	if (!zpool_name_valid(hdl, B_FALSE, newname))
2571 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
2572 
2573 	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2574 		(void) fprintf(stderr, gettext("Internal error: unable to "
2575 		    "retrieve pool configuration\n"));
2576 		return (-1);
2577 	}
2578 
2579 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
2580 	    == 0);
2581 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
2582 
2583 	if (props) {
2584 		if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2585 		    props, vers, B_TRUE, msg)) == NULL)
2586 			return (-1);
2587 	}
2588 
2589 	if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2590 	    &children) != 0) {
2591 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2592 		    "Source pool is missing vdev tree"));
2593 		if (zc_props)
2594 			nvlist_free(zc_props);
2595 		return (-1);
2596 	}
2597 
2598 	varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
2599 	vcount = 0;
2600 
2601 	if (*newroot == NULL ||
2602 	    nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
2603 	    &newchild, &newchildren) != 0)
2604 		newchildren = 0;
2605 
2606 	for (c = 0; c < children; c++) {
2607 		uint64_t is_log = B_FALSE, is_hole = B_FALSE;
2608 		char *type;
2609 		nvlist_t **mchild, *vdev;
2610 		uint_t mchildren;
2611 		int entry;
2612 
2613 		/*
2614 		 * Unlike cache & spares, slogs are stored in the
2615 		 * ZPOOL_CONFIG_CHILDREN array.  We filter them out here.
2616 		 */
2617 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2618 		    &is_log);
2619 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2620 		    &is_hole);
2621 		if (is_log || is_hole) {
2622 			/*
2623 			 * Create a hole vdev and put it in the config.
2624 			 */
2625 			if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
2626 				goto out;
2627 			if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
2628 			    VDEV_TYPE_HOLE) != 0)
2629 				goto out;
2630 			if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
2631 			    1) != 0)
2632 				goto out;
2633 			if (lastlog == 0)
2634 				lastlog = vcount;
2635 			varray[vcount++] = vdev;
2636 			continue;
2637 		}
2638 		lastlog = 0;
2639 		verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
2640 		    == 0);
2641 		if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
2642 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2643 			    "Source pool must be composed only of mirrors\n"));
2644 			retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2645 			goto out;
2646 		}
2647 
2648 		verify(nvlist_lookup_nvlist_array(child[c],
2649 		    ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2650 
2651 		/* find or add an entry for this top-level vdev */
2652 		if (newchildren > 0 &&
2653 		    (entry = find_vdev_entry(zhp, mchild, mchildren,
2654 		    newchild, newchildren)) >= 0) {
2655 			/* We found a disk that the user specified. */
2656 			vdev = mchild[entry];
2657 			++found;
2658 		} else {
2659 			/* User didn't specify a disk for this vdev. */
2660 			vdev = mchild[mchildren - 1];
2661 		}
2662 
2663 		if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
2664 			goto out;
2665 	}
2666 
2667 	/* did we find every disk the user specified? */
2668 	if (found != newchildren) {
2669 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
2670 		    "include at most one disk from each mirror"));
2671 		retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2672 		goto out;
2673 	}
2674 
2675 	/* Prepare the nvlist for populating. */
2676 	if (*newroot == NULL) {
2677 		if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
2678 			goto out;
2679 		freelist = B_TRUE;
2680 		if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
2681 		    VDEV_TYPE_ROOT) != 0)
2682 			goto out;
2683 	} else {
2684 		verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
2685 	}
2686 
2687 	/* Add all the children we found */
2688 	if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
2689 	    lastlog == 0 ? vcount : lastlog) != 0)
2690 		goto out;
2691 
2692 	/*
2693 	 * If we're just doing a dry run, exit now with success.
2694 	 */
2695 	if (flags.dryrun) {
2696 		memory_err = B_FALSE;
2697 		freelist = B_FALSE;
2698 		goto out;
2699 	}
2700 
2701 	/* now build up the config list & call the ioctl */
2702 	if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
2703 		goto out;
2704 
2705 	if (nvlist_add_nvlist(newconfig,
2706 	    ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
2707 	    nvlist_add_string(newconfig,
2708 	    ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
2709 	    nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
2710 		goto out;
2711 
2712 	/*
2713 	 * The new pool is automatically part of the namespace unless we
2714 	 * explicitly export it.
2715 	 */
2716 	if (!flags.import)
2717 		zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
2718 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2719 	(void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
2720 	if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
2721 		goto out;
2722 	if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
2723 		goto out;
2724 
2725 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
2726 		retval = zpool_standard_error(hdl, errno, msg);
2727 		goto out;
2728 	}
2729 
2730 	freelist = B_FALSE;
2731 	memory_err = B_FALSE;
2732 
2733 out:
2734 	if (varray != NULL) {
2735 		int v;
2736 
2737 		for (v = 0; v < vcount; v++)
2738 			nvlist_free(varray[v]);
2739 		free(varray);
2740 	}
2741 	zcmd_free_nvlists(&zc);
2742 	if (zc_props)
2743 		nvlist_free(zc_props);
2744 	if (newconfig)
2745 		nvlist_free(newconfig);
2746 	if (freelist) {
2747 		nvlist_free(*newroot);
2748 		*newroot = NULL;
2749 	}
2750 
2751 	if (retval != 0)
2752 		return (retval);
2753 
2754 	if (memory_err)
2755 		return (no_memory(hdl));
2756 
2757 	return (0);
2758 }
2759 
2760 /*
2761  * Remove the given device.  Currently, this is supported only for hot spares
2762  * and level 2 cache devices.
2763  */
2764 int
2765 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
2766 {
2767 	zfs_cmd_t zc = { 0 };
2768 	char msg[1024];
2769 	nvlist_t *tgt;
2770 	boolean_t avail_spare, l2cache, islog;
2771 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2772 	uint64_t version;
2773 
2774 	(void) snprintf(msg, sizeof (msg),
2775 	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
2776 
2777 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2778 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2779 	    &islog)) == 0)
2780 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2781 	/*
2782 	 * XXX - this should just go away.
2783 	 */
2784 	if (!avail_spare && !l2cache && !islog) {
2785 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2786 		    "only inactive hot spares, cache, top-level, "
2787 		    "or log devices can be removed"));
2788 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2789 	}
2790 
2791 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
2792 	if (islog && version < SPA_VERSION_HOLES) {
2793 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2794 		    "pool must be upgrade to support log removal"));
2795 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
2796 	}
2797 
2798 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2799 
2800 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
2801 		return (0);
2802 
2803 	return (zpool_standard_error(hdl, errno, msg));
2804 }
2805 
2806 /*
2807  * Clear the errors for the pool, or the particular device if specified.
2808  */
2809 int
2810 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
2811 {
2812 	zfs_cmd_t zc = { 0 };
2813 	char msg[1024];
2814 	nvlist_t *tgt;
2815 	zpool_rewind_policy_t policy;
2816 	boolean_t avail_spare, l2cache;
2817 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2818 	nvlist_t *nvi = NULL;
2819 
2820 	if (path)
2821 		(void) snprintf(msg, sizeof (msg),
2822 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2823 		    path);
2824 	else
2825 		(void) snprintf(msg, sizeof (msg),
2826 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2827 		    zhp->zpool_name);
2828 
2829 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2830 	if (path) {
2831 		if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
2832 		    &l2cache, NULL)) == 0)
2833 			return (zfs_error(hdl, EZFS_NODEVICE, msg));
2834 
2835 		/*
2836 		 * Don't allow error clearing for hot spares.  Do allow
2837 		 * error clearing for l2cache devices.
2838 		 */
2839 		if (avail_spare)
2840 			return (zfs_error(hdl, EZFS_ISSPARE, msg));
2841 
2842 		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
2843 		    &zc.zc_guid) == 0);
2844 	}
2845 
2846 	zpool_get_rewind_policy(rewindnvl, &policy);
2847 	zc.zc_cookie = policy.zrp_request;
2848 
2849 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 8192) != 0)
2850 		return (-1);
2851 
2852 	if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, rewindnvl) != 0)
2853 		return (-1);
2854 
2855 	if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0 ||
2856 	    ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
2857 	    errno != EPERM && errno != EACCES)) {
2858 		if (policy.zrp_request &
2859 		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
2860 			(void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
2861 			zpool_rewind_exclaim(hdl, zc.zc_name,
2862 			    ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
2863 			    nvi);
2864 			nvlist_free(nvi);
2865 		}
2866 		zcmd_free_nvlists(&zc);
2867 		return (0);
2868 	}
2869 
2870 	zcmd_free_nvlists(&zc);
2871 	return (zpool_standard_error(hdl, errno, msg));
2872 }
2873 
2874 /*
2875  * Similar to zpool_clear(), but takes a GUID (used by fmd).
2876  */
2877 int
2878 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
2879 {
2880 	zfs_cmd_t zc = { 0 };
2881 	char msg[1024];
2882 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2883 
2884 	(void) snprintf(msg, sizeof (msg),
2885 	    dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
2886 	    guid);
2887 
2888 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2889 	zc.zc_guid = guid;
2890 	zc.zc_cookie = ZPOOL_NO_REWIND;
2891 
2892 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
2893 		return (0);
2894 
2895 	return (zpool_standard_error(hdl, errno, msg));
2896 }
2897 
2898 /*
2899  * Convert from a devid string to a path.
2900  */
2901 static char *
2902 devid_to_path(char *devid_str)
2903 {
2904 	ddi_devid_t devid;
2905 	char *minor;
2906 	char *path;
2907 	devid_nmlist_t *list = NULL;
2908 	int ret;
2909 
2910 	if (devid_str_decode(devid_str, &devid, &minor) != 0)
2911 		return (NULL);
2912 
2913 	ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
2914 
2915 	devid_str_free(minor);
2916 	devid_free(devid);
2917 
2918 	if (ret != 0)
2919 		return (NULL);
2920 
2921 	if ((path = strdup(list[0].devname)) == NULL)
2922 		return (NULL);
2923 
2924 	devid_free_nmlist(list);
2925 
2926 	return (path);
2927 }
2928 
2929 /*
2930  * Convert from a path to a devid string.
2931  */
2932 static char *
2933 path_to_devid(const char *path)
2934 {
2935 	int fd;
2936 	ddi_devid_t devid;
2937 	char *minor, *ret;
2938 
2939 	if ((fd = open(path, O_RDONLY)) < 0)
2940 		return (NULL);
2941 
2942 	minor = NULL;
2943 	ret = NULL;
2944 	if (devid_get(fd, &devid) == 0) {
2945 		if (devid_get_minor_name(fd, &minor) == 0)
2946 			ret = devid_str_encode(devid, minor);
2947 		if (minor != NULL)
2948 			devid_str_free(minor);
2949 		devid_free(devid);
2950 	}
2951 	(void) close(fd);
2952 
2953 	return (ret);
2954 }
2955 
2956 /*
2957  * Issue the necessary ioctl() to update the stored path value for the vdev.  We
2958  * ignore any failure here, since a common case is for an unprivileged user to
2959  * type 'zpool status', and we'll display the correct information anyway.
2960  */
2961 static void
2962 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
2963 {
2964 	zfs_cmd_t zc = { 0 };
2965 
2966 	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2967 	(void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
2968 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2969 	    &zc.zc_guid) == 0);
2970 
2971 	(void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
2972 }
2973 
2974 /*
2975  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
2976  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
2977  * We also check if this is a whole disk, in which case we strip off the
2978  * trailing 's0' slice name.
2979  *
2980  * This routine is also responsible for identifying when disks have been
2981  * reconfigured in a new location.  The kernel will have opened the device by
2982  * devid, but the path will still refer to the old location.  To catch this, we
2983  * first do a path -> devid translation (which is fast for the common case).  If
2984  * the devid matches, we're done.  If not, we do a reverse devid -> path
2985  * translation and issue the appropriate ioctl() to update the path of the vdev.
2986  * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
2987  * of these checks.
2988  */
2989 char *
2990 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
2991     boolean_t verbose)
2992 {
2993 	char *path, *devid;
2994 	uint64_t value;
2995 	char buf[64];
2996 	vdev_stat_t *vs;
2997 	uint_t vsc;
2998 
2999 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
3000 	    &value) == 0) {
3001 		verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3002 		    &value) == 0);
3003 		(void) snprintf(buf, sizeof (buf), "%llu",
3004 		    (u_longlong_t)value);
3005 		path = buf;
3006 	} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
3007 
3008 		/*
3009 		 * If the device is dead (faulted, offline, etc) then don't
3010 		 * bother opening it.  Otherwise we may be forcing the user to
3011 		 * open a misbehaving device, which can have undesirable
3012 		 * effects.
3013 		 */
3014 		if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3015 		    (uint64_t **)&vs, &vsc) != 0 ||
3016 		    vs->vs_state >= VDEV_STATE_DEGRADED) &&
3017 		    zhp != NULL &&
3018 		    nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3019 			/*
3020 			 * Determine if the current path is correct.
3021 			 */
3022 			char *newdevid = path_to_devid(path);
3023 
3024 			if (newdevid == NULL ||
3025 			    strcmp(devid, newdevid) != 0) {
3026 				char *newpath;
3027 
3028 				if ((newpath = devid_to_path(devid)) != NULL) {
3029 					/*
3030 					 * Update the path appropriately.
3031 					 */
3032 					set_path(zhp, nv, newpath);
3033 					if (nvlist_add_string(nv,
3034 					    ZPOOL_CONFIG_PATH, newpath) == 0)
3035 						verify(nvlist_lookup_string(nv,
3036 						    ZPOOL_CONFIG_PATH,
3037 						    &path) == 0);
3038 					free(newpath);
3039 				}
3040 			}
3041 
3042 			if (newdevid)
3043 				devid_str_free(newdevid);
3044 		}
3045 
3046 		if (strncmp(path, "/dev/dsk/", 9) == 0)
3047 			path += 9;
3048 
3049 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3050 		    &value) == 0 && value) {
3051 			int pathlen = strlen(path);
3052 			char *tmp = zfs_strdup(hdl, path);
3053 
3054 			/*
3055 			 * If it starts with c#, and ends with "s0", chop
3056 			 * the "s0" off, or if it ends with "s0/old", remove
3057 			 * the "s0" from the middle.
3058 			 */
3059 			if (CTD_CHECK(tmp)) {
3060 				if (strcmp(&tmp[pathlen - 2], "s0") == 0) {
3061 					tmp[pathlen - 2] = '\0';
3062 				} else if (pathlen > 6 &&
3063 				    strcmp(&tmp[pathlen - 6], "s0/old") == 0) {
3064 					(void) strcpy(&tmp[pathlen - 6],
3065 					    "/old");
3066 				}
3067 			}
3068 			return (tmp);
3069 		}
3070 	} else {
3071 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3072 
3073 		/*
3074 		 * If it's a raidz device, we need to stick in the parity level.
3075 		 */
3076 		if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3077 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3078 			    &value) == 0);
3079 			(void) snprintf(buf, sizeof (buf), "%s%llu", path,
3080 			    (u_longlong_t)value);
3081 			path = buf;
3082 		}
3083 
3084 		/*
3085 		 * We identify each top-level vdev by using a <type-id>
3086 		 * naming convention.
3087 		 */
3088 		if (verbose) {
3089 			uint64_t id;
3090 
3091 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3092 			    &id) == 0);
3093 			(void) snprintf(buf, sizeof (buf), "%s-%llu", path,
3094 			    (u_longlong_t)id);
3095 			path = buf;
3096 		}
3097 	}
3098 
3099 	return (zfs_strdup(hdl, path));
3100 }
3101 
3102 static int
3103 zbookmark_compare(const void *a, const void *b)
3104 {
3105 	return (memcmp(a, b, sizeof (zbookmark_t)));
3106 }
3107 
3108 /*
3109  * Retrieve the persistent error log, uniquify the members, and return to the
3110  * caller.
3111  */
3112 int
3113 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3114 {
3115 	zfs_cmd_t zc = { 0 };
3116 	uint64_t count;
3117 	zbookmark_t *zb = NULL;
3118 	int i;
3119 
3120 	/*
3121 	 * Retrieve the raw error list from the kernel.  If the number of errors
3122 	 * has increased, allocate more space and continue until we get the
3123 	 * entire list.
3124 	 */
3125 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3126 	    &count) == 0);
3127 	if (count == 0)
3128 		return (0);
3129 	if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3130 	    count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
3131 		return (-1);
3132 	zc.zc_nvlist_dst_size = count;
3133 	(void) strcpy(zc.zc_name, zhp->zpool_name);
3134 	for (;;) {
3135 		if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3136 		    &zc) != 0) {
3137 			free((void *)(uintptr_t)zc.zc_nvlist_dst);
3138 			if (errno == ENOMEM) {
3139 				count = zc.zc_nvlist_dst_size;
3140 				if ((zc.zc_nvlist_dst = (uintptr_t)
3141 				    zfs_alloc(zhp->zpool_hdl, count *
3142 				    sizeof (zbookmark_t))) == (uintptr_t)NULL)
3143 					return (-1);
3144 			} else {
3145 				return (-1);
3146 			}
3147 		} else {
3148 			break;
3149 		}
3150 	}
3151 
3152 	/*
3153 	 * Sort the resulting bookmarks.  This is a little confusing due to the
3154 	 * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
3155 	 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3156 	 * _not_ copied as part of the process.  So we point the start of our
3157 	 * array appropriate and decrement the total number of elements.
3158 	 */
3159 	zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
3160 	    zc.zc_nvlist_dst_size;
3161 	count -= zc.zc_nvlist_dst_size;
3162 
3163 	qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
3164 
3165 	verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3166 
3167 	/*
3168 	 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3169 	 */
3170 	for (i = 0; i < count; i++) {
3171 		nvlist_t *nv;
3172 
3173 		/* ignoring zb_blkid and zb_level for now */
3174 		if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3175 		    zb[i-1].zb_object == zb[i].zb_object)
3176 			continue;
3177 
3178 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3179 			goto nomem;
3180 		if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3181 		    zb[i].zb_objset) != 0) {
3182 			nvlist_free(nv);
3183 			goto nomem;
3184 		}
3185 		if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3186 		    zb[i].zb_object) != 0) {
3187 			nvlist_free(nv);
3188 			goto nomem;
3189 		}
3190 		if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3191 			nvlist_free(nv);
3192 			goto nomem;
3193 		}
3194 		nvlist_free(nv);
3195 	}
3196 
3197 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
3198 	return (0);
3199 
3200 nomem:
3201 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
3202 	return (no_memory(zhp->zpool_hdl));
3203 }
3204 
3205 /*
3206  * Upgrade a ZFS pool to the latest on-disk version.
3207  */
3208 int
3209 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3210 {
3211 	zfs_cmd_t zc = { 0 };
3212 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3213 
3214 	(void) strcpy(zc.zc_name, zhp->zpool_name);
3215 	zc.zc_cookie = new_version;
3216 
3217 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3218 		return (zpool_standard_error_fmt(hdl, errno,
3219 		    dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3220 		    zhp->zpool_name));
3221 	return (0);
3222 }
3223 
3224 void
3225 zpool_set_history_str(const char *subcommand, int argc, char **argv,
3226     char *history_str)
3227 {
3228 	int i;
3229 
3230 	(void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
3231 	for (i = 1; i < argc; i++) {
3232 		if (strlen(history_str) + 1 + strlen(argv[i]) >
3233 		    HIS_MAX_RECORD_LEN)
3234 			break;
3235 		(void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
3236 		(void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
3237 	}
3238 }
3239 
3240 /*
3241  * Stage command history for logging.
3242  */
3243 int
3244 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
3245 {
3246 	if (history_str == NULL)
3247 		return (EINVAL);
3248 
3249 	if (strlen(history_str) > HIS_MAX_RECORD_LEN)
3250 		return (EINVAL);
3251 
3252 	if (hdl->libzfs_log_str != NULL)
3253 		free(hdl->libzfs_log_str);
3254 
3255 	if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
3256 		return (no_memory(hdl));
3257 
3258 	return (0);
3259 }
3260 
3261 /*
3262  * Perform ioctl to get some command history of a pool.
3263  *
3264  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
3265  * logical offset of the history buffer to start reading from.
3266  *
3267  * Upon return, 'off' is the next logical offset to read from and
3268  * 'len' is the actual amount of bytes read into 'buf'.
3269  */
3270 static int
3271 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3272 {
3273 	zfs_cmd_t zc = { 0 };
3274 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3275 
3276 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3277 
3278 	zc.zc_history = (uint64_t)(uintptr_t)buf;
3279 	zc.zc_history_len = *len;
3280 	zc.zc_history_offset = *off;
3281 
3282 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3283 		switch (errno) {
3284 		case EPERM:
3285 			return (zfs_error_fmt(hdl, EZFS_PERM,
3286 			    dgettext(TEXT_DOMAIN,
3287 			    "cannot show history for pool '%s'"),
3288 			    zhp->zpool_name));
3289 		case ENOENT:
3290 			return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3291 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
3292 			    "'%s'"), zhp->zpool_name));
3293 		case ENOTSUP:
3294 			return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3295 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
3296 			    "'%s', pool must be upgraded"), zhp->zpool_name));
3297 		default:
3298 			return (zpool_standard_error_fmt(hdl, errno,
3299 			    dgettext(TEXT_DOMAIN,
3300 			    "cannot get history for '%s'"), zhp->zpool_name));
3301 		}
3302 	}
3303 
3304 	*len = zc.zc_history_len;
3305 	*off = zc.zc_history_offset;
3306 
3307 	return (0);
3308 }
3309 
3310 /*
3311  * Process the buffer of nvlists, unpacking and storing each nvlist record
3312  * into 'records'.  'leftover' is set to the number of bytes that weren't
3313  * processed as there wasn't a complete record.
3314  */
3315 int
3316 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3317     nvlist_t ***records, uint_t *numrecords)
3318 {
3319 	uint64_t reclen;
3320 	nvlist_t *nv;
3321 	int i;
3322 
3323 	while (bytes_read > sizeof (reclen)) {
3324 
3325 		/* get length of packed record (stored as little endian) */
3326 		for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3327 			reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3328 
3329 		if (bytes_read < sizeof (reclen) + reclen)
3330 			break;
3331 
3332 		/* unpack record */
3333 		if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3334 			return (ENOMEM);
3335 		bytes_read -= sizeof (reclen) + reclen;
3336 		buf += sizeof (reclen) + reclen;
3337 
3338 		/* add record to nvlist array */
3339 		(*numrecords)++;
3340 		if (ISP2(*numrecords + 1)) {
3341 			*records = realloc(*records,
3342 			    *numrecords * 2 * sizeof (nvlist_t *));
3343 		}
3344 		(*records)[*numrecords - 1] = nv;
3345 	}
3346 
3347 	*leftover = bytes_read;
3348 	return (0);
3349 }
3350 
3351 #define	HIS_BUF_LEN	(128*1024)
3352 
3353 /*
3354  * Retrieve the command history of a pool.
3355  */
3356 int
3357 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3358 {
3359 	char buf[HIS_BUF_LEN];
3360 	uint64_t off = 0;
3361 	nvlist_t **records = NULL;
3362 	uint_t numrecords = 0;
3363 	int err, i;
3364 
3365 	do {
3366 		uint64_t bytes_read = sizeof (buf);
3367 		uint64_t leftover;
3368 
3369 		if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
3370 			break;
3371 
3372 		/* if nothing else was read in, we're at EOF, just return */
3373 		if (!bytes_read)
3374 			break;
3375 
3376 		if ((err = zpool_history_unpack(buf, bytes_read,
3377 		    &leftover, &records, &numrecords)) != 0)
3378 			break;
3379 		off -= leftover;
3380 
3381 		/* CONSTCOND */
3382 	} while (1);
3383 
3384 	if (!err) {
3385 		verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
3386 		verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
3387 		    records, numrecords) == 0);
3388 	}
3389 	for (i = 0; i < numrecords; i++)
3390 		nvlist_free(records[i]);
3391 	free(records);
3392 
3393 	return (err);
3394 }
3395 
3396 void
3397 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
3398     char *pathname, size_t len)
3399 {
3400 	zfs_cmd_t zc = { 0 };
3401 	boolean_t mounted = B_FALSE;
3402 	char *mntpnt = NULL;
3403 	char dsname[MAXNAMELEN];
3404 
3405 	if (dsobj == 0) {
3406 		/* special case for the MOS */
3407 		(void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
3408 		return;
3409 	}
3410 
3411 	/* get the dataset's name */
3412 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3413 	zc.zc_obj = dsobj;
3414 	if (ioctl(zhp->zpool_hdl->libzfs_fd,
3415 	    ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
3416 		/* just write out a path of two object numbers */
3417 		(void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
3418 		    dsobj, obj);
3419 		return;
3420 	}
3421 	(void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
3422 
3423 	/* find out if the dataset is mounted */
3424 	mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
3425 
3426 	/* get the corrupted object's path */
3427 	(void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
3428 	zc.zc_obj = obj;
3429 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
3430 	    &zc) == 0) {
3431 		if (mounted) {
3432 			(void) snprintf(pathname, len, "%s%s", mntpnt,
3433 			    zc.zc_value);
3434 		} else {
3435 			(void) snprintf(pathname, len, "%s:%s",
3436 			    dsname, zc.zc_value);
3437 		}
3438 	} else {
3439 		(void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
3440 	}
3441 	free(mntpnt);
3442 }
3443 
3444 /*
3445  * Read the EFI label from the config, if a label does not exist then
3446  * pass back the error to the caller. If the caller has passed a non-NULL
3447  * diskaddr argument then we set it to the starting address of the EFI
3448  * partition.
3449  */
3450 static int
3451 read_efi_label(nvlist_t *config, diskaddr_t *sb)
3452 {
3453 	char *path;
3454 	int fd;
3455 	char diskname[MAXPATHLEN];
3456 	int err = -1;
3457 
3458 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
3459 		return (err);
3460 
3461 	(void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
3462 	    strrchr(path, '/'));
3463 	if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
3464 		struct dk_gpt *vtoc;
3465 
3466 		if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
3467 			if (sb != NULL)
3468 				*sb = vtoc->efi_parts[0].p_start;
3469 			efi_free(vtoc);
3470 		}
3471 		(void) close(fd);
3472 	}
3473 	return (err);
3474 }
3475 
3476 /*
3477  * determine where a partition starts on a disk in the current
3478  * configuration
3479  */
3480 static diskaddr_t
3481 find_start_block(nvlist_t *config)
3482 {
3483 	nvlist_t **child;
3484 	uint_t c, children;
3485 	diskaddr_t sb = MAXOFFSET_T;
3486 	uint64_t wholedisk;
3487 
3488 	if (nvlist_lookup_nvlist_array(config,
3489 	    ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
3490 		if (nvlist_lookup_uint64(config,
3491 		    ZPOOL_CONFIG_WHOLE_DISK,
3492 		    &wholedisk) != 0 || !wholedisk) {
3493 			return (MAXOFFSET_T);
3494 		}
3495 		if (read_efi_label(config, &sb) < 0)
3496 			sb = MAXOFFSET_T;
3497 		return (sb);
3498 	}
3499 
3500 	for (c = 0; c < children; c++) {
3501 		sb = find_start_block(child[c]);
3502 		if (sb != MAXOFFSET_T) {
3503 			return (sb);
3504 		}
3505 	}
3506 	return (MAXOFFSET_T);
3507 }
3508 
3509 /*
3510  * Label an individual disk.  The name provided is the short name,
3511  * stripped of any leading /dev path.
3512  */
3513 int
3514 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
3515 {
3516 	char path[MAXPATHLEN];
3517 	struct dk_gpt *vtoc;
3518 	int fd;
3519 	size_t resv = EFI_MIN_RESV_SIZE;
3520 	uint64_t slice_size;
3521 	diskaddr_t start_block;
3522 	char errbuf[1024];
3523 
3524 	/* prepare an error message just in case */
3525 	(void) snprintf(errbuf, sizeof (errbuf),
3526 	    dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
3527 
3528 	if (zhp) {
3529 		nvlist_t *nvroot;
3530 
3531 		if (pool_is_bootable(zhp)) {
3532 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3533 			    "EFI labeled devices are not supported on root "
3534 			    "pools."));
3535 			return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
3536 		}
3537 
3538 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
3539 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3540 
3541 		if (zhp->zpool_start_block == 0)
3542 			start_block = find_start_block(nvroot);
3543 		else
3544 			start_block = zhp->zpool_start_block;
3545 		zhp->zpool_start_block = start_block;
3546 	} else {
3547 		/* new pool */
3548 		start_block = NEW_START_BLOCK;
3549 	}
3550 
3551 	(void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
3552 	    BACKUP_SLICE);
3553 
3554 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
3555 		/*
3556 		 * This shouldn't happen.  We've long since verified that this
3557 		 * is a valid device.
3558 		 */
3559 		zfs_error_aux(hdl,
3560 		    dgettext(TEXT_DOMAIN, "unable to open device"));
3561 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
3562 	}
3563 
3564 	if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
3565 		/*
3566 		 * The only way this can fail is if we run out of memory, or we
3567 		 * were unable to read the disk's capacity
3568 		 */
3569 		if (errno == ENOMEM)
3570 			(void) no_memory(hdl);
3571 
3572 		(void) close(fd);
3573 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3574 		    "unable to read disk capacity"), name);
3575 
3576 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
3577 	}
3578 
3579 	slice_size = vtoc->efi_last_u_lba + 1;
3580 	slice_size -= EFI_MIN_RESV_SIZE;
3581 	if (start_block == MAXOFFSET_T)
3582 		start_block = NEW_START_BLOCK;
3583 	slice_size -= start_block;
3584 
3585 	vtoc->efi_parts[0].p_start = start_block;
3586 	vtoc->efi_parts[0].p_size = slice_size;
3587 
3588 	/*
3589 	 * Why we use V_USR: V_BACKUP confuses users, and is considered
3590 	 * disposable by some EFI utilities (since EFI doesn't have a backup
3591 	 * slice).  V_UNASSIGNED is supposed to be used only for zero size
3592 	 * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
3593 	 * etc. were all pretty specific.  V_USR is as close to reality as we
3594 	 * can get, in the absence of V_OTHER.
3595 	 */
3596 	vtoc->efi_parts[0].p_tag = V_USR;
3597 	(void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
3598 
3599 	vtoc->efi_parts[8].p_start = slice_size + start_block;
3600 	vtoc->efi_parts[8].p_size = resv;
3601 	vtoc->efi_parts[8].p_tag = V_RESERVED;
3602 
3603 	if (efi_write(fd, vtoc) != 0) {
3604 		/*
3605 		 * Some block drivers (like pcata) may not support EFI
3606 		 * GPT labels.  Print out a helpful error message dir-
3607 		 * ecting the user to manually label the disk and give
3608 		 * a specific slice.
3609 		 */
3610 		(void) close(fd);
3611 		efi_free(vtoc);
3612 
3613 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3614 		    "try using fdisk(1M) and then provide a specific slice"));
3615 		return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
3616 	}
3617 
3618 	(void) close(fd);
3619 	efi_free(vtoc);
3620 	return (0);
3621 }
3622 
3623 static boolean_t
3624 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
3625 {
3626 	char *type;
3627 	nvlist_t **child;
3628 	uint_t children, c;
3629 
3630 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
3631 	if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
3632 	    strcmp(type, VDEV_TYPE_FILE) == 0 ||
3633 	    strcmp(type, VDEV_TYPE_LOG) == 0 ||
3634 	    strcmp(type, VDEV_TYPE_HOLE) == 0 ||
3635 	    strcmp(type, VDEV_TYPE_MISSING) == 0) {
3636 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3637 		    "vdev type '%s' is not supported"), type);
3638 		(void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
3639 		return (B_FALSE);
3640 	}
3641 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
3642 	    &child, &children) == 0) {
3643 		for (c = 0; c < children; c++) {
3644 			if (!supported_dump_vdev_type(hdl, child[c], errbuf))
3645 				return (B_FALSE);
3646 		}
3647 	}
3648 	return (B_TRUE);
3649 }
3650 
3651 /*
3652  * check if this zvol is allowable for use as a dump device; zero if
3653  * it is, > 0 if it isn't, < 0 if it isn't a zvol
3654  */
3655 int
3656 zvol_check_dump_config(char *arg)
3657 {
3658 	zpool_handle_t *zhp = NULL;
3659 	nvlist_t *config, *nvroot;
3660 	char *p, *volname;
3661 	nvlist_t **top;
3662 	uint_t toplevels;
3663 	libzfs_handle_t *hdl;
3664 	char errbuf[1024];
3665 	char poolname[ZPOOL_MAXNAMELEN];
3666 	int pathlen = strlen(ZVOL_FULL_DEV_DIR);
3667 	int ret = 1;
3668 
3669 	if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
3670 		return (-1);
3671 	}
3672 
3673 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3674 	    "dump is not supported on device '%s'"), arg);
3675 
3676 	if ((hdl = libzfs_init()) == NULL)
3677 		return (1);
3678 	libzfs_print_on_error(hdl, B_TRUE);
3679 
3680 	volname = arg + pathlen;
3681 
3682 	/* check the configuration of the pool */
3683 	if ((p = strchr(volname, '/')) == NULL) {
3684 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3685 		    "malformed dataset name"));
3686 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
3687 		return (1);
3688 	} else if (p - volname >= ZFS_MAXNAMELEN) {
3689 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3690 		    "dataset name is too long"));
3691 		(void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
3692 		return (1);
3693 	} else {
3694 		(void) strncpy(poolname, volname, p - volname);
3695 		poolname[p - volname] = '\0';
3696 	}
3697 
3698 	if ((zhp = zpool_open(hdl, poolname)) == NULL) {
3699 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3700 		    "could not open pool '%s'"), poolname);
3701 		(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
3702 		goto out;
3703 	}
3704 	config = zpool_get_config(zhp, NULL);
3705 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3706 	    &nvroot) != 0) {
3707 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3708 		    "could not obtain vdev configuration for  '%s'"), poolname);
3709 		(void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
3710 		goto out;
3711 	}
3712 
3713 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3714 	    &top, &toplevels) == 0);
3715 	if (toplevels != 1) {
3716 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3717 		    "'%s' has multiple top level vdevs"), poolname);
3718 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
3719 		goto out;
3720 	}
3721 
3722 	if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
3723 		goto out;
3724 	}
3725 	ret = 0;
3726 
3727 out:
3728 	if (zhp)
3729 		zpool_close(zhp);
3730 	libzfs_fini(hdl);
3731 	return (ret);
3732 }
3733