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  * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
25  * Copyright 2019 Joyent, Inc.
26  * Copyright 2016 Nexenta Systems, Inc.
27  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
28  * Copyright (c) 2017 Datto Inc.
29  * Copyright (c) 2017, Intel Corporation.
30  * Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
31  */
32 
33 #include <ctype.h>
34 #include <errno.h>
35 #include <devid.h>
36 #include <fcntl.h>
37 #include <libintl.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <strings.h>
41 #include <unistd.h>
42 #include <libgen.h>
43 #include <sys/dkio.h>
44 #include <sys/efi_partition.h>
45 #include <sys/vtoc.h>
46 #include <sys/zfs_ioctl.h>
47 #include <sys/modctl.h>
48 #include <sys/mkdev.h>
49 #include <dlfcn.h>
50 #include <libzutil.h>
51 
52 #include "zfs_namecheck.h"
53 #include "zfs_prop.h"
54 #include "libzfs_impl.h"
55 #include "zfs_comutil.h"
56 #include "zfeature_common.h"
57 
58 static int read_efi_label(nvlist_t *, diskaddr_t *, boolean_t *);
59 static boolean_t zpool_vdev_is_interior(const char *name);
60 
61 #define	BACKUP_SLICE	"s2"
62 
63 typedef struct prop_flags {
64 	int create:1;	/* Validate property on creation */
65 	int import:1;	/* Validate property on import */
66 } prop_flags_t;
67 
68 /*
69  * ====================================================================
70  *   zpool property functions
71  * ====================================================================
72  */
73 
74 static int
75 zpool_get_all_props(zpool_handle_t *zhp)
76 {
77 	zfs_cmd_t zc = { 0 };
78 	libzfs_handle_t *hdl = zhp->zpool_hdl;
79 
80 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
81 
82 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
83 		return (-1);
84 
85 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
86 		if (errno == ENOMEM) {
87 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
88 				zcmd_free_nvlists(&zc);
89 				return (-1);
90 			}
91 		} else {
92 			zcmd_free_nvlists(&zc);
93 			return (-1);
94 		}
95 	}
96 
97 	if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
98 		zcmd_free_nvlists(&zc);
99 		return (-1);
100 	}
101 
102 	zcmd_free_nvlists(&zc);
103 
104 	return (0);
105 }
106 
107 static int
108 zpool_props_refresh(zpool_handle_t *zhp)
109 {
110 	nvlist_t *old_props;
111 
112 	old_props = zhp->zpool_props;
113 
114 	if (zpool_get_all_props(zhp) != 0)
115 		return (-1);
116 
117 	nvlist_free(old_props);
118 	return (0);
119 }
120 
121 static char *
122 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
123     zprop_source_t *src)
124 {
125 	nvlist_t *nv, *nvl;
126 	uint64_t ival;
127 	char *value;
128 	zprop_source_t source;
129 
130 	nvl = zhp->zpool_props;
131 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
132 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
133 		source = ival;
134 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
135 	} else {
136 		source = ZPROP_SRC_DEFAULT;
137 		if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
138 			value = "-";
139 	}
140 
141 	if (src)
142 		*src = source;
143 
144 	return (value);
145 }
146 
147 uint64_t
148 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
149 {
150 	nvlist_t *nv, *nvl;
151 	uint64_t value;
152 	zprop_source_t source;
153 
154 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
155 		/*
156 		 * zpool_get_all_props() has most likely failed because
157 		 * the pool is faulted, but if all we need is the top level
158 		 * vdev's guid then get it from the zhp config nvlist.
159 		 */
160 		if ((prop == ZPOOL_PROP_GUID) &&
161 		    (nvlist_lookup_nvlist(zhp->zpool_config,
162 		    ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
163 		    (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
164 		    == 0)) {
165 			return (value);
166 		}
167 		return (zpool_prop_default_numeric(prop));
168 	}
169 
170 	nvl = zhp->zpool_props;
171 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
172 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
173 		source = value;
174 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
175 	} else {
176 		source = ZPROP_SRC_DEFAULT;
177 		value = zpool_prop_default_numeric(prop);
178 	}
179 
180 	if (src)
181 		*src = source;
182 
183 	return (value);
184 }
185 
186 /*
187  * Map VDEV STATE to printed strings.
188  */
189 const char *
190 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
191 {
192 	switch (state) {
193 	case VDEV_STATE_CLOSED:
194 	case VDEV_STATE_OFFLINE:
195 		return (gettext("OFFLINE"));
196 	case VDEV_STATE_REMOVED:
197 		return (gettext("REMOVED"));
198 	case VDEV_STATE_CANT_OPEN:
199 		if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
200 			return (gettext("FAULTED"));
201 		else if (aux == VDEV_AUX_SPLIT_POOL)
202 			return (gettext("SPLIT"));
203 		else
204 			return (gettext("UNAVAIL"));
205 	case VDEV_STATE_FAULTED:
206 		return (gettext("FAULTED"));
207 	case VDEV_STATE_DEGRADED:
208 		return (gettext("DEGRADED"));
209 	case VDEV_STATE_HEALTHY:
210 		return (gettext("ONLINE"));
211 
212 	default:
213 		break;
214 	}
215 
216 	return (gettext("UNKNOWN"));
217 }
218 
219 /*
220  * Map POOL STATE to printed strings.
221  */
222 const char *
223 zpool_pool_state_to_name(pool_state_t state)
224 {
225 	switch (state) {
226 	case POOL_STATE_ACTIVE:
227 		return (gettext("ACTIVE"));
228 	case POOL_STATE_EXPORTED:
229 		return (gettext("EXPORTED"));
230 	case POOL_STATE_DESTROYED:
231 		return (gettext("DESTROYED"));
232 	case POOL_STATE_SPARE:
233 		return (gettext("SPARE"));
234 	case POOL_STATE_L2CACHE:
235 		return (gettext("L2CACHE"));
236 	case POOL_STATE_UNINITIALIZED:
237 		return (gettext("UNINITIALIZED"));
238 	case POOL_STATE_UNAVAIL:
239 		return (gettext("UNAVAIL"));
240 	case POOL_STATE_POTENTIALLY_ACTIVE:
241 		return (gettext("POTENTIALLY_ACTIVE"));
242 	}
243 
244 	return (gettext("UNKNOWN"));
245 }
246 
247 /*
248  * Get a zpool property value for 'prop' and return the value in
249  * a pre-allocated buffer.
250  */
251 int
252 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
253     zprop_source_t *srctype, boolean_t literal)
254 {
255 	uint64_t intval;
256 	const char *strval;
257 	zprop_source_t src = ZPROP_SRC_NONE;
258 	nvlist_t *nvroot;
259 	vdev_stat_t *vs;
260 	uint_t vsc;
261 
262 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
263 		switch (prop) {
264 		case ZPOOL_PROP_NAME:
265 			(void) strlcpy(buf, zpool_get_name(zhp), len);
266 			break;
267 
268 		case ZPOOL_PROP_HEALTH:
269 			(void) strlcpy(buf, "FAULTED", len);
270 			break;
271 
272 		case ZPOOL_PROP_GUID:
273 			intval = zpool_get_prop_int(zhp, prop, &src);
274 			(void) snprintf(buf, len, "%llu", intval);
275 			break;
276 
277 		case ZPOOL_PROP_ALTROOT:
278 		case ZPOOL_PROP_CACHEFILE:
279 		case ZPOOL_PROP_COMMENT:
280 			if (zhp->zpool_props != NULL ||
281 			    zpool_get_all_props(zhp) == 0) {
282 				(void) strlcpy(buf,
283 				    zpool_get_prop_string(zhp, prop, &src),
284 				    len);
285 				break;
286 			}
287 			/* FALLTHROUGH */
288 		default:
289 			(void) strlcpy(buf, "-", len);
290 			break;
291 		}
292 
293 		if (srctype != NULL)
294 			*srctype = src;
295 		return (0);
296 	}
297 
298 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
299 	    prop != ZPOOL_PROP_NAME)
300 		return (-1);
301 
302 	switch (zpool_prop_get_type(prop)) {
303 	case PROP_TYPE_STRING:
304 		(void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
305 		    len);
306 		break;
307 
308 	case PROP_TYPE_NUMBER:
309 		intval = zpool_get_prop_int(zhp, prop, &src);
310 
311 		switch (prop) {
312 		case ZPOOL_PROP_SIZE:
313 		case ZPOOL_PROP_ALLOCATED:
314 		case ZPOOL_PROP_FREE:
315 		case ZPOOL_PROP_FREEING:
316 		case ZPOOL_PROP_LEAKED:
317 		case ZPOOL_PROP_ASHIFT:
318 			if (literal) {
319 				(void) snprintf(buf, len, "%llu",
320 				    (u_longlong_t)intval);
321 			} else {
322 				(void) zfs_nicenum(intval, buf, len);
323 			}
324 			break;
325 		case ZPOOL_PROP_BOOTSIZE:
326 		case ZPOOL_PROP_EXPANDSZ:
327 		case ZPOOL_PROP_CHECKPOINT:
328 			if (intval == 0) {
329 				(void) strlcpy(buf, "-", len);
330 			} else if (literal) {
331 				(void) snprintf(buf, len, "%llu",
332 				    (u_longlong_t)intval);
333 			} else {
334 				(void) zfs_nicenum(intval, buf, len);
335 			}
336 			break;
337 		case ZPOOL_PROP_CAPACITY:
338 			if (literal) {
339 				(void) snprintf(buf, len, "%llu",
340 				    (u_longlong_t)intval);
341 			} else {
342 				(void) snprintf(buf, len, "%llu%%",
343 				    (u_longlong_t)intval);
344 			}
345 			break;
346 		case ZPOOL_PROP_FRAGMENTATION:
347 			if (intval == UINT64_MAX) {
348 				(void) strlcpy(buf, "-", len);
349 			} else if (literal) {
350 				(void) snprintf(buf, len, "%llu",
351 				    (u_longlong_t)intval);
352 			} else {
353 				(void) snprintf(buf, len, "%llu%%",
354 				    (u_longlong_t)intval);
355 			}
356 			break;
357 		case ZPOOL_PROP_DEDUPRATIO:
358 			if (literal)
359 				(void) snprintf(buf, len, "%llu.%02llu",
360 				    (u_longlong_t)(intval / 100),
361 				    (u_longlong_t)(intval % 100));
362 			else
363 				(void) snprintf(buf, len, "%llu.%02llux",
364 				    (u_longlong_t)(intval / 100),
365 				    (u_longlong_t)(intval % 100));
366 			break;
367 		case ZPOOL_PROP_HEALTH:
368 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
369 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
370 			verify(nvlist_lookup_uint64_array(nvroot,
371 			    ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
372 			    == 0);
373 
374 			(void) strlcpy(buf, zpool_state_to_name(intval,
375 			    vs->vs_aux), len);
376 			break;
377 		case ZPOOL_PROP_VERSION:
378 			if (intval >= SPA_VERSION_FEATURES) {
379 				(void) snprintf(buf, len, "-");
380 				break;
381 			}
382 			/* FALLTHROUGH */
383 		default:
384 			(void) snprintf(buf, len, "%llu", intval);
385 		}
386 		break;
387 
388 	case PROP_TYPE_INDEX:
389 		intval = zpool_get_prop_int(zhp, prop, &src);
390 		if (zpool_prop_index_to_string(prop, intval, &strval)
391 		    != 0)
392 			return (-1);
393 		(void) strlcpy(buf, strval, len);
394 		break;
395 
396 	default:
397 		abort();
398 	}
399 
400 	if (srctype)
401 		*srctype = src;
402 
403 	return (0);
404 }
405 
406 /*
407  * Check if the bootfs name has the same pool name as it is set to.
408  * Assuming bootfs is a valid dataset name.
409  */
410 static boolean_t
411 bootfs_name_valid(const char *pool, const char *bootfs)
412 {
413 	int len = strlen(pool);
414 	if (bootfs[0] == '\0')
415 		return (B_TRUE);
416 
417 	if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
418 		return (B_FALSE);
419 
420 	if (strncmp(pool, bootfs, len) == 0 &&
421 	    (bootfs[len] == '/' || bootfs[len] == '\0'))
422 		return (B_TRUE);
423 
424 	return (B_FALSE);
425 }
426 
427 boolean_t
428 zpool_is_bootable(zpool_handle_t *zhp)
429 {
430 	char bootfs[ZFS_MAX_DATASET_NAME_LEN];
431 
432 	return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
433 	    sizeof (bootfs), NULL, B_FALSE) == 0 && strncmp(bootfs, "-",
434 	    sizeof (bootfs)) != 0);
435 }
436 
437 
438 /*
439  * Given an nvlist of zpool properties to be set, validate that they are
440  * correct, and parse any numeric properties (index, boolean, etc) if they are
441  * specified as strings.
442  */
443 static nvlist_t *
444 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
445     nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
446 {
447 	nvpair_t *elem;
448 	nvlist_t *retprops;
449 	zpool_prop_t prop;
450 	char *strval;
451 	uint64_t intval;
452 	char *slash, *check;
453 	struct stat64 statbuf;
454 	zpool_handle_t *zhp;
455 
456 	if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
457 		(void) no_memory(hdl);
458 		return (NULL);
459 	}
460 
461 	elem = NULL;
462 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
463 		const char *propname = nvpair_name(elem);
464 
465 		prop = zpool_name_to_prop(propname);
466 		if (prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname)) {
467 			int err;
468 			char *fname = strchr(propname, '@') + 1;
469 
470 			err = zfeature_lookup_name(fname, NULL);
471 			if (err != 0) {
472 				ASSERT3U(err, ==, ENOENT);
473 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
474 				    "invalid feature '%s', '%s'"), fname,
475 				    propname);
476 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
477 				goto error;
478 			}
479 
480 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
481 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
482 				    "'%s' must be a string"), propname);
483 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
484 				goto error;
485 			}
486 
487 			(void) nvpair_value_string(elem, &strval);
488 			if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0 &&
489 			    strcmp(strval, ZFS_FEATURE_DISABLED) != 0) {
490 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
491 				    "property '%s' can only be set to "
492 				    "'enabled' or 'disabled'"), propname);
493 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
494 				goto error;
495 			}
496 
497 			if (nvlist_add_uint64(retprops, propname, 0) != 0) {
498 				(void) no_memory(hdl);
499 				goto error;
500 			}
501 			continue;
502 		}
503 
504 		/*
505 		 * Make sure this property is valid and applies to this type.
506 		 */
507 		if (prop == ZPOOL_PROP_INVAL) {
508 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
509 			    "invalid property '%s'"), propname);
510 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
511 			goto error;
512 		}
513 
514 		if (zpool_prop_readonly(prop)) {
515 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
516 			    "is readonly"), propname);
517 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
518 			goto error;
519 		}
520 
521 		if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
522 		    &strval, &intval, errbuf) != 0)
523 			goto error;
524 
525 		/*
526 		 * Perform additional checking for specific properties.
527 		 */
528 		switch (prop) {
529 		case ZPOOL_PROP_VERSION:
530 			if (intval < version ||
531 			    !SPA_VERSION_IS_SUPPORTED(intval)) {
532 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
533 				    "property '%s' number %d is invalid."),
534 				    propname, intval);
535 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
536 				goto error;
537 			}
538 			break;
539 
540 		case ZPOOL_PROP_BOOTSIZE:
541 			if (!flags.create) {
542 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
543 				    "property '%s' can only be set during pool "
544 				    "creation"), propname);
545 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
546 				goto error;
547 			}
548 			break;
549 
550 		case ZPOOL_PROP_ASHIFT:
551 			if (intval != 0 &&
552 			    (intval < ASHIFT_MIN || intval > ASHIFT_MAX)) {
553 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
554 				    "invalid '%s=%d' property: only values "
555 				    "between %" PRId32 " and %" PRId32 " "
556 				    "are allowed.\n"),
557 				    propname, intval, ASHIFT_MIN, ASHIFT_MAX);
558 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
559 				goto error;
560 			}
561 			break;
562 
563 		case ZPOOL_PROP_BOOTFS:
564 			if (flags.create || flags.import) {
565 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
566 				    "property '%s' cannot be set at creation "
567 				    "or import time"), propname);
568 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
569 				goto error;
570 			}
571 
572 			if (version < SPA_VERSION_BOOTFS) {
573 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
574 				    "pool must be upgraded to support "
575 				    "'%s' property"), propname);
576 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
577 				goto error;
578 			}
579 
580 			/*
581 			 * bootfs property value has to be a dataset name and
582 			 * the dataset has to be in the same pool as it sets to.
583 			 */
584 			if (!bootfs_name_valid(poolname, strval)) {
585 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
586 				    "is an invalid name"), strval);
587 				(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
588 				goto error;
589 			}
590 
591 			if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
592 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
593 				    "could not open pool '%s'"), poolname);
594 				(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
595 				goto error;
596 			}
597 			zpool_close(zhp);
598 			break;
599 
600 		case ZPOOL_PROP_ALTROOT:
601 			if (!flags.create && !flags.import) {
602 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
603 				    "property '%s' can only be set during pool "
604 				    "creation or import"), propname);
605 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
606 				goto error;
607 			}
608 
609 			if (strval[0] != '/') {
610 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
611 				    "bad alternate root '%s'"), strval);
612 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
613 				goto error;
614 			}
615 			break;
616 
617 		case ZPOOL_PROP_CACHEFILE:
618 			if (strval[0] == '\0')
619 				break;
620 
621 			if (strcmp(strval, "none") == 0)
622 				break;
623 
624 			if (strval[0] != '/') {
625 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
626 				    "property '%s' must be empty, an "
627 				    "absolute path, or 'none'"), propname);
628 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
629 				goto error;
630 			}
631 
632 			slash = strrchr(strval, '/');
633 
634 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
635 			    strcmp(slash, "/..") == 0) {
636 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
637 				    "'%s' is not a valid file"), strval);
638 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
639 				goto error;
640 			}
641 
642 			*slash = '\0';
643 
644 			if (strval[0] != '\0' &&
645 			    (stat64(strval, &statbuf) != 0 ||
646 			    !S_ISDIR(statbuf.st_mode))) {
647 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
648 				    "'%s' is not a valid directory"),
649 				    strval);
650 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
651 				goto error;
652 			}
653 
654 			*slash = '/';
655 			break;
656 
657 		case ZPOOL_PROP_COMMENT:
658 			for (check = strval; *check != '\0'; check++) {
659 				if (!isprint(*check)) {
660 					zfs_error_aux(hdl,
661 					    dgettext(TEXT_DOMAIN,
662 					    "comment may only have printable "
663 					    "characters"));
664 					(void) zfs_error(hdl, EZFS_BADPROP,
665 					    errbuf);
666 					goto error;
667 				}
668 			}
669 			if (strlen(strval) > ZPROP_MAX_COMMENT) {
670 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
671 				    "comment must not exceed %d characters"),
672 				    ZPROP_MAX_COMMENT);
673 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
674 				goto error;
675 			}
676 			break;
677 
678 		case ZPOOL_PROP_READONLY:
679 			if (!flags.import) {
680 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
681 				    "property '%s' can only be set at "
682 				    "import time"), propname);
683 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
684 				goto error;
685 			}
686 			break;
687 
688 		case ZPOOL_PROP_TNAME:
689 			if (!flags.create) {
690 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
691 				    "property '%s' can only be set at "
692 				    "creation time"), propname);
693 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
694 				goto error;
695 			}
696 			break;
697 
698 		case ZPOOL_PROP_MULTIHOST:
699 			if (get_system_hostid() == 0) {
700 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
701 				    "requires a non-zero system hostid"));
702 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
703 				goto error;
704 			}
705 			break;
706 
707 		default:
708 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
709 			    "property '%s'(%d) not defined"), propname, prop);
710 			break;
711 		}
712 	}
713 
714 	return (retprops);
715 error:
716 	nvlist_free(retprops);
717 	return (NULL);
718 }
719 
720 /*
721  * Set zpool property : propname=propval.
722  */
723 int
724 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
725 {
726 	zfs_cmd_t zc = { 0 };
727 	int ret = -1;
728 	char errbuf[1024];
729 	nvlist_t *nvl = NULL;
730 	nvlist_t *realprops;
731 	uint64_t version;
732 	prop_flags_t flags = { 0 };
733 
734 	(void) snprintf(errbuf, sizeof (errbuf),
735 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
736 	    zhp->zpool_name);
737 
738 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
739 		return (no_memory(zhp->zpool_hdl));
740 
741 	if (nvlist_add_string(nvl, propname, propval) != 0) {
742 		nvlist_free(nvl);
743 		return (no_memory(zhp->zpool_hdl));
744 	}
745 
746 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
747 	if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
748 	    zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
749 		nvlist_free(nvl);
750 		return (-1);
751 	}
752 
753 	nvlist_free(nvl);
754 	nvl = realprops;
755 
756 	/*
757 	 * Execute the corresponding ioctl() to set this property.
758 	 */
759 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
760 
761 	if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
762 		nvlist_free(nvl);
763 		return (-1);
764 	}
765 
766 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
767 
768 	zcmd_free_nvlists(&zc);
769 	nvlist_free(nvl);
770 
771 	if (ret)
772 		(void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
773 	else
774 		(void) zpool_props_refresh(zhp);
775 
776 	return (ret);
777 }
778 
779 int
780 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
781 {
782 	libzfs_handle_t *hdl = zhp->zpool_hdl;
783 	zprop_list_t *entry;
784 	char buf[ZFS_MAXPROPLEN];
785 	nvlist_t *features = NULL;
786 	zprop_list_t **last;
787 	boolean_t firstexpand = (NULL == *plp);
788 
789 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
790 		return (-1);
791 
792 	last = plp;
793 	while (*last != NULL)
794 		last = &(*last)->pl_next;
795 
796 	if ((*plp)->pl_all)
797 		features = zpool_get_features(zhp);
798 
799 	if ((*plp)->pl_all && firstexpand) {
800 		for (int i = 0; i < SPA_FEATURES; i++) {
801 			zprop_list_t *entry = zfs_alloc(hdl,
802 			    sizeof (zprop_list_t));
803 			entry->pl_prop = ZPROP_INVAL;
804 			entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
805 			    spa_feature_table[i].fi_uname);
806 			entry->pl_width = strlen(entry->pl_user_prop);
807 			entry->pl_all = B_TRUE;
808 
809 			*last = entry;
810 			last = &entry->pl_next;
811 		}
812 	}
813 
814 	/* add any unsupported features */
815 	for (nvpair_t *nvp = nvlist_next_nvpair(features, NULL);
816 	    nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
817 		char *propname;
818 		boolean_t found;
819 		zprop_list_t *entry;
820 
821 		if (zfeature_is_supported(nvpair_name(nvp)))
822 			continue;
823 
824 		propname = zfs_asprintf(hdl, "unsupported@%s",
825 		    nvpair_name(nvp));
826 
827 		/*
828 		 * Before adding the property to the list make sure that no
829 		 * other pool already added the same property.
830 		 */
831 		found = B_FALSE;
832 		entry = *plp;
833 		while (entry != NULL) {
834 			if (entry->pl_user_prop != NULL &&
835 			    strcmp(propname, entry->pl_user_prop) == 0) {
836 				found = B_TRUE;
837 				break;
838 			}
839 			entry = entry->pl_next;
840 		}
841 		if (found) {
842 			free(propname);
843 			continue;
844 		}
845 
846 		entry = zfs_alloc(hdl, sizeof (zprop_list_t));
847 		entry->pl_prop = ZPROP_INVAL;
848 		entry->pl_user_prop = propname;
849 		entry->pl_width = strlen(entry->pl_user_prop);
850 		entry->pl_all = B_TRUE;
851 
852 		*last = entry;
853 		last = &entry->pl_next;
854 	}
855 
856 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
857 
858 		if (entry->pl_fixed)
859 			continue;
860 
861 		if (entry->pl_prop != ZPROP_INVAL &&
862 		    zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
863 		    NULL, B_FALSE) == 0) {
864 			if (strlen(buf) > entry->pl_width)
865 				entry->pl_width = strlen(buf);
866 		}
867 	}
868 
869 	return (0);
870 }
871 
872 /*
873  * Get the state for the given feature on the given ZFS pool.
874  */
875 int
876 zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
877     size_t len)
878 {
879 	uint64_t refcount;
880 	boolean_t found = B_FALSE;
881 	nvlist_t *features = zpool_get_features(zhp);
882 	boolean_t supported;
883 	const char *feature = strchr(propname, '@') + 1;
884 
885 	supported = zpool_prop_feature(propname);
886 	ASSERT(supported || zpool_prop_unsupported(propname));
887 
888 	/*
889 	 * Convert from feature name to feature guid. This conversion is
890 	 * unecessary for unsupported@... properties because they already
891 	 * use guids.
892 	 */
893 	if (supported) {
894 		int ret;
895 		spa_feature_t fid;
896 
897 		ret = zfeature_lookup_name(feature, &fid);
898 		if (ret != 0) {
899 			(void) strlcpy(buf, "-", len);
900 			return (ENOTSUP);
901 		}
902 		feature = spa_feature_table[fid].fi_guid;
903 	}
904 
905 	if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
906 		found = B_TRUE;
907 
908 	if (supported) {
909 		if (!found) {
910 			(void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
911 		} else  {
912 			if (refcount == 0)
913 				(void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
914 			else
915 				(void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
916 		}
917 	} else {
918 		if (found) {
919 			if (refcount == 0) {
920 				(void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
921 			} else {
922 				(void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
923 			}
924 		} else {
925 			(void) strlcpy(buf, "-", len);
926 			return (ENOTSUP);
927 		}
928 	}
929 
930 	return (0);
931 }
932 
933 /*
934  * Don't start the slice at the default block of 34; many storage
935  * devices will use a stripe width of 128k, so start there instead.
936  */
937 #define	NEW_START_BLOCK	256
938 
939 /*
940  * Validate the given pool name, optionally putting an extended error message in
941  * 'buf'.
942  */
943 boolean_t
944 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
945 {
946 	namecheck_err_t why;
947 	char what;
948 	int ret;
949 
950 	ret = pool_namecheck(pool, &why, &what);
951 
952 	/*
953 	 * The rules for reserved pool names were extended at a later point.
954 	 * But we need to support users with existing pools that may now be
955 	 * invalid.  So we only check for this expanded set of names during a
956 	 * create (or import), and only in userland.
957 	 */
958 	if (ret == 0 && !isopen &&
959 	    (strncmp(pool, "mirror", 6) == 0 ||
960 	    strncmp(pool, "raidz", 5) == 0 ||
961 	    strncmp(pool, "spare", 5) == 0 ||
962 	    strcmp(pool, "log") == 0)) {
963 		if (hdl != NULL)
964 			zfs_error_aux(hdl,
965 			    dgettext(TEXT_DOMAIN, "name is reserved"));
966 		return (B_FALSE);
967 	}
968 
969 
970 	if (ret != 0) {
971 		if (hdl != NULL) {
972 			switch (why) {
973 			case NAME_ERR_TOOLONG:
974 				zfs_error_aux(hdl,
975 				    dgettext(TEXT_DOMAIN, "name is too long"));
976 				break;
977 
978 			case NAME_ERR_INVALCHAR:
979 				zfs_error_aux(hdl,
980 				    dgettext(TEXT_DOMAIN, "invalid character "
981 				    "'%c' in pool name"), what);
982 				break;
983 
984 			case NAME_ERR_NOLETTER:
985 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
986 				    "name must begin with a letter"));
987 				break;
988 
989 			case NAME_ERR_RESERVED:
990 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
991 				    "name is reserved"));
992 				break;
993 
994 			case NAME_ERR_DISKLIKE:
995 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
996 				    "pool name is reserved"));
997 				break;
998 
999 			case NAME_ERR_LEADING_SLASH:
1000 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1001 				    "leading slash in name"));
1002 				break;
1003 
1004 			case NAME_ERR_EMPTY_COMPONENT:
1005 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1006 				    "empty component in name"));
1007 				break;
1008 
1009 			case NAME_ERR_TRAILING_SLASH:
1010 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1011 				    "trailing slash in name"));
1012 				break;
1013 
1014 			case NAME_ERR_MULTIPLE_DELIMITERS:
1015 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1016 				    "multiple '@' and/or '#' delimiters in "
1017 				    "name"));
1018 				break;
1019 
1020 			default:
1021 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1022 				    "(%d) not defined"), why);
1023 				break;
1024 			}
1025 		}
1026 		return (B_FALSE);
1027 	}
1028 
1029 	return (B_TRUE);
1030 }
1031 
1032 /*
1033  * Open a handle to the given pool, even if the pool is currently in the FAULTED
1034  * state.
1035  */
1036 zpool_handle_t *
1037 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
1038 {
1039 	zpool_handle_t *zhp;
1040 	boolean_t missing;
1041 
1042 	/*
1043 	 * Make sure the pool name is valid.
1044 	 */
1045 	if (!zpool_name_valid(hdl, B_TRUE, pool)) {
1046 		(void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1047 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
1048 		    pool);
1049 		return (NULL);
1050 	}
1051 
1052 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1053 		return (NULL);
1054 
1055 	zhp->zpool_hdl = hdl;
1056 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1057 
1058 	if (zpool_refresh_stats(zhp, &missing) != 0) {
1059 		zpool_close(zhp);
1060 		return (NULL);
1061 	}
1062 
1063 	if (missing) {
1064 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
1065 		(void) zfs_error_fmt(hdl, EZFS_NOENT,
1066 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
1067 		zpool_close(zhp);
1068 		return (NULL);
1069 	}
1070 
1071 	return (zhp);
1072 }
1073 
1074 /*
1075  * Like the above, but silent on error.  Used when iterating over pools (because
1076  * the configuration cache may be out of date).
1077  */
1078 int
1079 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1080 {
1081 	zpool_handle_t *zhp;
1082 	boolean_t missing;
1083 
1084 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1085 		return (-1);
1086 
1087 	zhp->zpool_hdl = hdl;
1088 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1089 
1090 	if (zpool_refresh_stats(zhp, &missing) != 0) {
1091 		zpool_close(zhp);
1092 		return (-1);
1093 	}
1094 
1095 	if (missing) {
1096 		zpool_close(zhp);
1097 		*ret = NULL;
1098 		return (0);
1099 	}
1100 
1101 	*ret = zhp;
1102 	return (0);
1103 }
1104 
1105 /*
1106  * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1107  * state.
1108  */
1109 zpool_handle_t *
1110 zpool_open(libzfs_handle_t *hdl, const char *pool)
1111 {
1112 	zpool_handle_t *zhp;
1113 
1114 	if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1115 		return (NULL);
1116 
1117 	if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1118 		(void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
1119 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1120 		zpool_close(zhp);
1121 		return (NULL);
1122 	}
1123 
1124 	return (zhp);
1125 }
1126 
1127 /*
1128  * Close the handle.  Simply frees the memory associated with the handle.
1129  */
1130 void
1131 zpool_close(zpool_handle_t *zhp)
1132 {
1133 	nvlist_free(zhp->zpool_config);
1134 	nvlist_free(zhp->zpool_old_config);
1135 	nvlist_free(zhp->zpool_props);
1136 	free(zhp);
1137 }
1138 
1139 /*
1140  * Return the name of the pool.
1141  */
1142 const char *
1143 zpool_get_name(zpool_handle_t *zhp)
1144 {
1145 	return (zhp->zpool_name);
1146 }
1147 
1148 
1149 /*
1150  * Return the state of the pool (ACTIVE or UNAVAILABLE)
1151  */
1152 int
1153 zpool_get_state(zpool_handle_t *zhp)
1154 {
1155 	return (zhp->zpool_state);
1156 }
1157 
1158 /*
1159  * Check if vdev list contains a special vdev
1160  */
1161 static boolean_t
1162 zpool_has_special_vdev(nvlist_t *nvroot)
1163 {
1164 	nvlist_t **child;
1165 	uint_t children;
1166 
1167 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, &child,
1168 	    &children) == 0) {
1169 		for (uint_t c = 0; c < children; c++) {
1170 			char *bias;
1171 
1172 			if (nvlist_lookup_string(child[c],
1173 			    ZPOOL_CONFIG_ALLOCATION_BIAS, &bias) == 0 &&
1174 			    strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0) {
1175 				return (B_TRUE);
1176 			}
1177 		}
1178 	}
1179 	return (B_FALSE);
1180 }
1181 
1182 /*
1183  * Create the named pool, using the provided vdev list.  It is assumed
1184  * that the consumer has already validated the contents of the nvlist, so we
1185  * don't have to worry about error semantics.
1186  */
1187 int
1188 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
1189     nvlist_t *props, nvlist_t *fsprops)
1190 {
1191 	zfs_cmd_t zc = { 0 };
1192 	nvlist_t *zc_fsprops = NULL;
1193 	nvlist_t *zc_props = NULL;
1194 	nvlist_t *hidden_args = NULL;
1195 	uint8_t *wkeydata = NULL;
1196 	uint_t wkeylen = 0;
1197 	char msg[1024];
1198 	int ret = -1;
1199 
1200 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1201 	    "cannot create '%s'"), pool);
1202 
1203 	if (!zpool_name_valid(hdl, B_FALSE, pool))
1204 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1205 
1206 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1207 		return (-1);
1208 
1209 	if (props) {
1210 		prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1211 
1212 		if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1213 		    SPA_VERSION_1, flags, msg)) == NULL) {
1214 			goto create_failed;
1215 		}
1216 	}
1217 
1218 	if (fsprops) {
1219 		uint64_t zoned;
1220 		char *zonestr;
1221 
1222 		zoned = ((nvlist_lookup_string(fsprops,
1223 		    zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
1224 		    strcmp(zonestr, "on") == 0);
1225 
1226 		if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM,
1227 		    fsprops, zoned, NULL, NULL, B_TRUE, msg)) == NULL) {
1228 			goto create_failed;
1229 		}
1230 
1231 		if (nvlist_exists(zc_fsprops,
1232 		    zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS)) &&
1233 		    !zpool_has_special_vdev(nvroot)) {
1234 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1235 			    "%s property requires a special vdev"),
1236 			    zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS));
1237 			(void) zfs_error(hdl, EZFS_BADPROP, msg);
1238 			goto create_failed;
1239 		}
1240 
1241 		if (!zc_props &&
1242 		    (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
1243 			goto create_failed;
1244 		}
1245 		if (zfs_crypto_create(hdl, NULL, zc_fsprops, props, B_TRUE,
1246 		    &wkeydata, &wkeylen) != 0) {
1247 			(void) zfs_error(hdl, EZFS_CRYPTOFAILED, msg);
1248 			goto create_failed;
1249 		}
1250 		if (nvlist_add_nvlist(zc_props,
1251 		    ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
1252 			goto create_failed;
1253 		}
1254 		if (wkeydata != NULL) {
1255 			if (nvlist_alloc(&hidden_args, NV_UNIQUE_NAME, 0) != 0)
1256 				goto create_failed;
1257 
1258 			if (nvlist_add_uint8_array(hidden_args, "wkeydata",
1259 			    wkeydata, wkeylen) != 0)
1260 				goto create_failed;
1261 
1262 			if (nvlist_add_nvlist(zc_props, ZPOOL_HIDDEN_ARGS,
1263 			    hidden_args) != 0)
1264 				goto create_failed;
1265 		}
1266 	}
1267 
1268 	if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
1269 		goto create_failed;
1270 
1271 	(void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1272 
1273 	if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1274 
1275 		zcmd_free_nvlists(&zc);
1276 		nvlist_free(zc_props);
1277 		nvlist_free(zc_fsprops);
1278 		nvlist_free(hidden_args);
1279 		if (wkeydata != NULL)
1280 			free(wkeydata);
1281 
1282 		switch (errno) {
1283 		case EBUSY:
1284 			/*
1285 			 * This can happen if the user has specified the same
1286 			 * device multiple times.  We can't reliably detect this
1287 			 * until we try to add it and see we already have a
1288 			 * label.
1289 			 */
1290 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1291 			    "one or more vdevs refer to the same device"));
1292 			return (zfs_error(hdl, EZFS_BADDEV, msg));
1293 
1294 		case ERANGE:
1295 			/*
1296 			 * This happens if the record size is smaller or larger
1297 			 * than the allowed size range, or not a power of 2.
1298 			 *
1299 			 * NOTE: although zfs_valid_proplist is called earlier,
1300 			 * this case may have slipped through since the
1301 			 * pool does not exist yet and it is therefore
1302 			 * impossible to read properties e.g. max blocksize
1303 			 * from the pool.
1304 			 */
1305 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1306 			    "record size invalid"));
1307 			return (zfs_error(hdl, EZFS_BADPROP, msg));
1308 
1309 		case EOVERFLOW:
1310 			/*
1311 			 * This occurs when one of the devices is below
1312 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1313 			 * device was the problem device since there's no
1314 			 * reliable way to determine device size from userland.
1315 			 */
1316 			{
1317 				char buf[64];
1318 
1319 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1320 
1321 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1322 				    "one or more devices is less than the "
1323 				    "minimum size (%s)"), buf);
1324 			}
1325 			return (zfs_error(hdl, EZFS_BADDEV, msg));
1326 
1327 		case ENOSPC:
1328 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1329 			    "one or more devices is out of space"));
1330 			return (zfs_error(hdl, EZFS_BADDEV, msg));
1331 
1332 		default:
1333 			return (zpool_standard_error(hdl, errno, msg));
1334 		}
1335 	}
1336 
1337 create_failed:
1338 	zcmd_free_nvlists(&zc);
1339 	nvlist_free(zc_props);
1340 	nvlist_free(zc_fsprops);
1341 	nvlist_free(hidden_args);
1342 	if (wkeydata != NULL)
1343 		free(wkeydata);
1344 	return (ret);
1345 }
1346 
1347 /*
1348  * Destroy the given pool.  It is up to the caller to ensure that there are no
1349  * datasets left in the pool.
1350  */
1351 int
1352 zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1353 {
1354 	zfs_cmd_t zc = { 0 };
1355 	zfs_handle_t *zfp = NULL;
1356 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1357 	char msg[1024];
1358 
1359 	if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1360 	    (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1361 		return (-1);
1362 
1363 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1364 	zc.zc_history = (uint64_t)(uintptr_t)log_str;
1365 
1366 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1367 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1368 		    "cannot destroy '%s'"), zhp->zpool_name);
1369 
1370 		if (errno == EROFS) {
1371 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1372 			    "one or more devices is read only"));
1373 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1374 		} else {
1375 			(void) zpool_standard_error(hdl, errno, msg);
1376 		}
1377 
1378 		if (zfp)
1379 			zfs_close(zfp);
1380 		return (-1);
1381 	}
1382 
1383 	if (zfp) {
1384 		remove_mountpoint(zfp);
1385 		zfs_close(zfp);
1386 	}
1387 
1388 	return (0);
1389 }
1390 
1391 /*
1392  * Create a checkpoint in the given pool.
1393  */
1394 int
1395 zpool_checkpoint(zpool_handle_t *zhp)
1396 {
1397 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1398 	char msg[1024];
1399 	int error;
1400 
1401 	error = lzc_pool_checkpoint(zhp->zpool_name);
1402 	if (error != 0) {
1403 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1404 		    "cannot checkpoint '%s'"), zhp->zpool_name);
1405 		(void) zpool_standard_error(hdl, error, msg);
1406 		return (-1);
1407 	}
1408 
1409 	return (0);
1410 }
1411 
1412 /*
1413  * Discard the checkpoint from the given pool.
1414  */
1415 int
1416 zpool_discard_checkpoint(zpool_handle_t *zhp)
1417 {
1418 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1419 	char msg[1024];
1420 	int error;
1421 
1422 	error = lzc_pool_checkpoint_discard(zhp->zpool_name);
1423 	if (error != 0) {
1424 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1425 		    "cannot discard checkpoint in '%s'"), zhp->zpool_name);
1426 		(void) zpool_standard_error(hdl, error, msg);
1427 		return (-1);
1428 	}
1429 
1430 	return (0);
1431 }
1432 
1433 /*
1434  * Add the given vdevs to the pool.  The caller must have already performed the
1435  * necessary verification to ensure that the vdev specification is well-formed.
1436  */
1437 int
1438 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1439 {
1440 	zfs_cmd_t zc = { 0 };
1441 	int ret;
1442 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1443 	char msg[1024];
1444 	nvlist_t **spares, **l2cache;
1445 	uint_t nspares, nl2cache;
1446 
1447 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1448 	    "cannot add to '%s'"), zhp->zpool_name);
1449 
1450 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1451 	    SPA_VERSION_SPARES &&
1452 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1453 	    &spares, &nspares) == 0) {
1454 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1455 		    "upgraded to add hot spares"));
1456 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
1457 	}
1458 
1459 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1460 	    SPA_VERSION_L2CACHE &&
1461 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1462 	    &l2cache, &nl2cache) == 0) {
1463 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1464 		    "upgraded to add cache devices"));
1465 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
1466 	}
1467 
1468 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1469 		return (-1);
1470 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1471 
1472 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1473 		switch (errno) {
1474 		case EBUSY:
1475 			/*
1476 			 * This can happen if the user has specified the same
1477 			 * device multiple times.  We can't reliably detect this
1478 			 * until we try to add it and see we already have a
1479 			 * label.
1480 			 */
1481 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1482 			    "one or more vdevs refer to the same device"));
1483 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1484 			break;
1485 
1486 		case EINVAL:
1487 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1488 			    "invalid config; a pool with removing/removed "
1489 			    "vdevs does not support adding raidz vdevs"));
1490 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1491 			break;
1492 
1493 		case EOVERFLOW:
1494 			/*
1495 			 * This occurrs when one of the devices is below
1496 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1497 			 * device was the problem device since there's no
1498 			 * reliable way to determine device size from userland.
1499 			 */
1500 			{
1501 				char buf[64];
1502 
1503 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1504 
1505 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1506 				    "device is less than the minimum "
1507 				    "size (%s)"), buf);
1508 			}
1509 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1510 			break;
1511 
1512 		case ENOTSUP:
1513 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1514 			    "pool must be upgraded to add these vdevs"));
1515 			(void) zfs_error(hdl, EZFS_BADVERSION, msg);
1516 			break;
1517 
1518 		default:
1519 			(void) zpool_standard_error(hdl, errno, msg);
1520 		}
1521 
1522 		ret = -1;
1523 	} else {
1524 		ret = 0;
1525 	}
1526 
1527 	zcmd_free_nvlists(&zc);
1528 
1529 	return (ret);
1530 }
1531 
1532 /*
1533  * Exports the pool from the system.  The caller must ensure that there are no
1534  * mounted datasets in the pool.
1535  */
1536 static int
1537 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
1538     const char *log_str)
1539 {
1540 	zfs_cmd_t zc = { 0 };
1541 	char msg[1024];
1542 
1543 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1544 	    "cannot export '%s'"), zhp->zpool_name);
1545 
1546 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1547 	zc.zc_cookie = force;
1548 	zc.zc_guid = hardforce;
1549 	zc.zc_history = (uint64_t)(uintptr_t)log_str;
1550 
1551 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1552 		switch (errno) {
1553 		case EXDEV:
1554 			zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1555 			    "use '-f' to override the following errors:\n"
1556 			    "'%s' has an active shared spare which could be"
1557 			    " used by other pools once '%s' is exported."),
1558 			    zhp->zpool_name, zhp->zpool_name);
1559 			return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1560 			    msg));
1561 		default:
1562 			return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1563 			    msg));
1564 		}
1565 	}
1566 
1567 	return (0);
1568 }
1569 
1570 int
1571 zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1572 {
1573 	return (zpool_export_common(zhp, force, B_FALSE, log_str));
1574 }
1575 
1576 int
1577 zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1578 {
1579 	return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1580 }
1581 
1582 static void
1583 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1584     nvlist_t *config)
1585 {
1586 	nvlist_t *nv = NULL;
1587 	uint64_t rewindto;
1588 	int64_t loss = -1;
1589 	struct tm t;
1590 	char timestr[128];
1591 
1592 	if (!hdl->libzfs_printerr || config == NULL)
1593 		return;
1594 
1595 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1596 	    nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1597 		return;
1598 	}
1599 
1600 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1601 		return;
1602 	(void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1603 
1604 	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1605 	    strftime(timestr, 128, 0, &t) != 0) {
1606 		if (dryrun) {
1607 			(void) printf(dgettext(TEXT_DOMAIN,
1608 			    "Would be able to return %s "
1609 			    "to its state as of %s.\n"),
1610 			    name, timestr);
1611 		} else {
1612 			(void) printf(dgettext(TEXT_DOMAIN,
1613 			    "Pool %s returned to its state as of %s.\n"),
1614 			    name, timestr);
1615 		}
1616 		if (loss > 120) {
1617 			(void) printf(dgettext(TEXT_DOMAIN,
1618 			    "%s approximately %lld "),
1619 			    dryrun ? "Would discard" : "Discarded",
1620 			    (loss + 30) / 60);
1621 			(void) printf(dgettext(TEXT_DOMAIN,
1622 			    "minutes of transactions.\n"));
1623 		} else if (loss > 0) {
1624 			(void) printf(dgettext(TEXT_DOMAIN,
1625 			    "%s approximately %lld "),
1626 			    dryrun ? "Would discard" : "Discarded", loss);
1627 			(void) printf(dgettext(TEXT_DOMAIN,
1628 			    "seconds of transactions.\n"));
1629 		}
1630 	}
1631 }
1632 
1633 void
1634 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1635     nvlist_t *config)
1636 {
1637 	nvlist_t *nv = NULL;
1638 	int64_t loss = -1;
1639 	uint64_t edata = UINT64_MAX;
1640 	uint64_t rewindto;
1641 	struct tm t;
1642 	char timestr[128];
1643 
1644 	if (!hdl->libzfs_printerr)
1645 		return;
1646 
1647 	if (reason >= 0)
1648 		(void) printf(dgettext(TEXT_DOMAIN, "action: "));
1649 	else
1650 		(void) printf(dgettext(TEXT_DOMAIN, "\t"));
1651 
1652 	/* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1653 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1654 	    nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
1655 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1656 		goto no_info;
1657 
1658 	(void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1659 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1660 	    &edata);
1661 
1662 	(void) printf(dgettext(TEXT_DOMAIN,
1663 	    "Recovery is possible, but will result in some data loss.\n"));
1664 
1665 	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1666 	    strftime(timestr, 128, 0, &t) != 0) {
1667 		(void) printf(dgettext(TEXT_DOMAIN,
1668 		    "\tReturning the pool to its state as of %s\n"
1669 		    "\tshould correct the problem.  "),
1670 		    timestr);
1671 	} else {
1672 		(void) printf(dgettext(TEXT_DOMAIN,
1673 		    "\tReverting the pool to an earlier state "
1674 		    "should correct the problem.\n\t"));
1675 	}
1676 
1677 	if (loss > 120) {
1678 		(void) printf(dgettext(TEXT_DOMAIN,
1679 		    "Approximately %lld minutes of data\n"
1680 		    "\tmust be discarded, irreversibly.  "), (loss + 30) / 60);
1681 	} else if (loss > 0) {
1682 		(void) printf(dgettext(TEXT_DOMAIN,
1683 		    "Approximately %lld seconds of data\n"
1684 		    "\tmust be discarded, irreversibly.  "), loss);
1685 	}
1686 	if (edata != 0 && edata != UINT64_MAX) {
1687 		if (edata == 1) {
1688 			(void) printf(dgettext(TEXT_DOMAIN,
1689 			    "After rewind, at least\n"
1690 			    "\tone persistent user-data error will remain.  "));
1691 		} else {
1692 			(void) printf(dgettext(TEXT_DOMAIN,
1693 			    "After rewind, several\n"
1694 			    "\tpersistent user-data errors will remain.  "));
1695 		}
1696 	}
1697 	(void) printf(dgettext(TEXT_DOMAIN,
1698 	    "Recovery can be attempted\n\tby executing 'zpool %s -F %s'.  "),
1699 	    reason >= 0 ? "clear" : "import", name);
1700 
1701 	(void) printf(dgettext(TEXT_DOMAIN,
1702 	    "A scrub of the pool\n"
1703 	    "\tis strongly recommended after recovery.\n"));
1704 	return;
1705 
1706 no_info:
1707 	(void) printf(dgettext(TEXT_DOMAIN,
1708 	    "Destroy and re-create the pool from\n\ta backup source.\n"));
1709 }
1710 
1711 /*
1712  * zpool_import() is a contracted interface. Should be kept the same
1713  * if possible.
1714  *
1715  * Applications should use zpool_import_props() to import a pool with
1716  * new properties value to be set.
1717  */
1718 int
1719 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1720     char *altroot)
1721 {
1722 	nvlist_t *props = NULL;
1723 	int ret;
1724 
1725 	if (altroot != NULL) {
1726 		if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1727 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
1728 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1729 			    newname));
1730 		}
1731 
1732 		if (nvlist_add_string(props,
1733 		    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1734 		    nvlist_add_string(props,
1735 		    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1736 			nvlist_free(props);
1737 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
1738 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1739 			    newname));
1740 		}
1741 	}
1742 
1743 	ret = zpool_import_props(hdl, config, newname, props,
1744 	    ZFS_IMPORT_NORMAL);
1745 	nvlist_free(props);
1746 	return (ret);
1747 }
1748 
1749 static void
1750 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1751     int indent)
1752 {
1753 	nvlist_t **child;
1754 	uint_t c, children;
1755 	char *vname;
1756 	uint64_t is_log = 0;
1757 
1758 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1759 	    &is_log);
1760 
1761 	if (name != NULL)
1762 		(void) printf("\t%*s%s%s\n", indent, "", name,
1763 		    is_log ? " [log]" : "");
1764 
1765 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1766 	    &child, &children) != 0)
1767 		return;
1768 
1769 	for (c = 0; c < children; c++) {
1770 		vname = zpool_vdev_name(hdl, NULL, child[c], VDEV_NAME_TYPE_ID);
1771 		print_vdev_tree(hdl, vname, child[c], indent + 2);
1772 		free(vname);
1773 	}
1774 }
1775 
1776 void
1777 zpool_print_unsup_feat(nvlist_t *config)
1778 {
1779 	nvlist_t *nvinfo, *unsup_feat;
1780 
1781 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1782 	    0);
1783 	verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1784 	    &unsup_feat) == 0);
1785 
1786 	for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1787 	    nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1788 		char *desc;
1789 
1790 		verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1791 		verify(nvpair_value_string(nvp, &desc) == 0);
1792 
1793 		if (strlen(desc) > 0)
1794 			(void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1795 		else
1796 			(void) printf("\t%s\n", nvpair_name(nvp));
1797 	}
1798 }
1799 
1800 /*
1801  * Import the given pool using the known configuration and a list of
1802  * properties to be set. The configuration should have come from
1803  * zpool_find_import(). The 'newname' parameters control whether the pool
1804  * is imported with a different name.
1805  */
1806 int
1807 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1808     nvlist_t *props, int flags)
1809 {
1810 	zfs_cmd_t zc = { 0 };
1811 	zpool_load_policy_t policy;
1812 	nvlist_t *nv = NULL;
1813 	nvlist_t *nvinfo = NULL;
1814 	nvlist_t *missing = NULL;
1815 	char *thename;
1816 	char *origname;
1817 	int ret;
1818 	int error = 0;
1819 	char errbuf[1024];
1820 
1821 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1822 	    &origname) == 0);
1823 
1824 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1825 	    "cannot import pool '%s'"), origname);
1826 
1827 	if (newname != NULL) {
1828 		if (!zpool_name_valid(hdl, B_FALSE, newname))
1829 			return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1830 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1831 			    newname));
1832 		thename = (char *)newname;
1833 	} else {
1834 		thename = origname;
1835 	}
1836 
1837 	if (props != NULL) {
1838 		uint64_t version;
1839 		prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1840 
1841 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1842 		    &version) == 0);
1843 
1844 		if ((props = zpool_valid_proplist(hdl, origname,
1845 		    props, version, flags, errbuf)) == NULL)
1846 			return (-1);
1847 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1848 			nvlist_free(props);
1849 			return (-1);
1850 		}
1851 		nvlist_free(props);
1852 	}
1853 
1854 	(void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1855 
1856 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1857 	    &zc.zc_guid) == 0);
1858 
1859 	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1860 		zcmd_free_nvlists(&zc);
1861 		return (-1);
1862 	}
1863 	if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1864 		zcmd_free_nvlists(&zc);
1865 		return (-1);
1866 	}
1867 
1868 	zc.zc_cookie = flags;
1869 	while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1870 	    errno == ENOMEM) {
1871 		if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1872 			zcmd_free_nvlists(&zc);
1873 			return (-1);
1874 		}
1875 	}
1876 	if (ret != 0)
1877 		error = errno;
1878 
1879 	(void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1880 
1881 	zcmd_free_nvlists(&zc);
1882 
1883 	zpool_get_load_policy(config, &policy);
1884 
1885 	if (error) {
1886 		char desc[1024];
1887 		char aux[256];
1888 
1889 		/*
1890 		 * Dry-run failed, but we print out what success
1891 		 * looks like if we found a best txg
1892 		 */
1893 		if (policy.zlp_rewind & ZPOOL_TRY_REWIND) {
1894 			zpool_rewind_exclaim(hdl, newname ? origname : thename,
1895 			    B_TRUE, nv);
1896 			nvlist_free(nv);
1897 			return (-1);
1898 		}
1899 
1900 		if (newname == NULL)
1901 			(void) snprintf(desc, sizeof (desc),
1902 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1903 			    thename);
1904 		else
1905 			(void) snprintf(desc, sizeof (desc),
1906 			    dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1907 			    origname, thename);
1908 
1909 		switch (error) {
1910 		case ENOTSUP:
1911 			if (nv != NULL && nvlist_lookup_nvlist(nv,
1912 			    ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1913 			    nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
1914 				(void) printf(dgettext(TEXT_DOMAIN, "This "
1915 				    "pool uses the following feature(s) not "
1916 				    "supported by this system:\n"));
1917 				zpool_print_unsup_feat(nv);
1918 				if (nvlist_exists(nvinfo,
1919 				    ZPOOL_CONFIG_CAN_RDONLY)) {
1920 					(void) printf(dgettext(TEXT_DOMAIN,
1921 					    "All unsupported features are only "
1922 					    "required for writing to the pool."
1923 					    "\nThe pool can be imported using "
1924 					    "'-o readonly=on'.\n"));
1925 				}
1926 			}
1927 			/*
1928 			 * Unsupported version.
1929 			 */
1930 			(void) zfs_error(hdl, EZFS_BADVERSION, desc);
1931 			break;
1932 
1933 		case EREMOTEIO:
1934 			if (nv != NULL && nvlist_lookup_nvlist(nv,
1935 			    ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0) {
1936 				char *hostname = "<unknown>";
1937 				uint64_t hostid = 0;
1938 				mmp_state_t mmp_state;
1939 
1940 				mmp_state = fnvlist_lookup_uint64(nvinfo,
1941 				    ZPOOL_CONFIG_MMP_STATE);
1942 
1943 				if (nvlist_exists(nvinfo,
1944 				    ZPOOL_CONFIG_MMP_HOSTNAME))
1945 					hostname = fnvlist_lookup_string(nvinfo,
1946 					    ZPOOL_CONFIG_MMP_HOSTNAME);
1947 
1948 				if (nvlist_exists(nvinfo,
1949 				    ZPOOL_CONFIG_MMP_HOSTID))
1950 					hostid = fnvlist_lookup_uint64(nvinfo,
1951 					    ZPOOL_CONFIG_MMP_HOSTID);
1952 
1953 				if (mmp_state == MMP_STATE_ACTIVE) {
1954 					(void) snprintf(aux, sizeof (aux),
1955 					    dgettext(TEXT_DOMAIN, "pool is imp"
1956 					    "orted on host '%s' (hostid=%lx).\n"
1957 					    "Export the pool on the other "
1958 					    "system, then run 'zpool import'."),
1959 					    hostname, (unsigned long) hostid);
1960 				} else if (mmp_state == MMP_STATE_NO_HOSTID) {
1961 					(void) snprintf(aux, sizeof (aux),
1962 					    dgettext(TEXT_DOMAIN, "pool has "
1963 					    "the multihost property on and "
1964 					    "the\nsystem's hostid is not "
1965 					    "set.\n"));
1966 				}
1967 
1968 				(void) zfs_error_aux(hdl, aux);
1969 			}
1970 			(void) zfs_error(hdl, EZFS_ACTIVE_POOL, desc);
1971 			break;
1972 
1973 		case EINVAL:
1974 			(void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1975 			break;
1976 
1977 		case EROFS:
1978 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1979 			    "one or more devices is read only"));
1980 			(void) zfs_error(hdl, EZFS_BADDEV, desc);
1981 			break;
1982 
1983 		case ENXIO:
1984 			if (nv && nvlist_lookup_nvlist(nv,
1985 			    ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1986 			    nvlist_lookup_nvlist(nvinfo,
1987 			    ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
1988 				(void) printf(dgettext(TEXT_DOMAIN,
1989 				    "The devices below are missing or "
1990 				    "corrupted, use '-m' to import the pool "
1991 				    "anyway:\n"));
1992 				print_vdev_tree(hdl, NULL, missing, 2);
1993 				(void) printf("\n");
1994 			}
1995 			(void) zpool_standard_error(hdl, error, desc);
1996 			break;
1997 
1998 		case EEXIST:
1999 			(void) zpool_standard_error(hdl, error, desc);
2000 			break;
2001 		case ENAMETOOLONG:
2002 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2003 			    "new name of at least one dataset is longer than "
2004 			    "the maximum allowable length"));
2005 			(void) zfs_error(hdl, EZFS_NAMETOOLONG, desc);
2006 			break;
2007 		default:
2008 			(void) zpool_standard_error(hdl, error, desc);
2009 			zpool_explain_recover(hdl,
2010 			    newname ? origname : thename, -error, nv);
2011 			break;
2012 		}
2013 
2014 		nvlist_free(nv);
2015 		ret = -1;
2016 	} else {
2017 		zpool_handle_t *zhp;
2018 
2019 		/*
2020 		 * This should never fail, but play it safe anyway.
2021 		 */
2022 		if (zpool_open_silent(hdl, thename, &zhp) != 0)
2023 			ret = -1;
2024 		else if (zhp != NULL)
2025 			zpool_close(zhp);
2026 		if (policy.zlp_rewind &
2027 		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
2028 			zpool_rewind_exclaim(hdl, newname ? origname : thename,
2029 			    ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0), nv);
2030 		}
2031 		nvlist_free(nv);
2032 		return (0);
2033 	}
2034 
2035 	return (ret);
2036 }
2037 
2038 /*
2039  * Translate vdev names to guids.  If a vdev_path is determined to be
2040  * unsuitable then a vd_errlist is allocated and the vdev path and errno
2041  * are added to it.
2042  */
2043 static int
2044 zpool_translate_vdev_guids(zpool_handle_t *zhp, nvlist_t *vds,
2045     nvlist_t *vdev_guids, nvlist_t *guids_to_paths, nvlist_t **vd_errlist)
2046 {
2047 	nvlist_t *errlist = NULL;
2048 	int error = 0;
2049 
2050 	for (nvpair_t *elem = nvlist_next_nvpair(vds, NULL); elem != NULL;
2051 	    elem = nvlist_next_nvpair(vds, elem)) {
2052 		boolean_t spare, cache;
2053 
2054 		char *vd_path = nvpair_name(elem);
2055 		nvlist_t *tgt = zpool_find_vdev(zhp, vd_path, &spare, &cache,
2056 		    NULL);
2057 
2058 		if ((tgt == NULL) || cache || spare) {
2059 			if (errlist == NULL) {
2060 				errlist = fnvlist_alloc();
2061 				error = EINVAL;
2062 			}
2063 
2064 			uint64_t err = (tgt == NULL) ? EZFS_NODEVICE :
2065 			    (spare ? EZFS_ISSPARE : EZFS_ISL2CACHE);
2066 			fnvlist_add_int64(errlist, vd_path, err);
2067 			continue;
2068 		}
2069 
2070 		uint64_t guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
2071 		fnvlist_add_uint64(vdev_guids, vd_path, guid);
2072 
2073 		char msg[MAXNAMELEN];
2074 		(void) snprintf(msg, sizeof (msg), "%llu", (u_longlong_t)guid);
2075 		fnvlist_add_string(guids_to_paths, msg, vd_path);
2076 	}
2077 
2078 	if (error != 0) {
2079 		verify(errlist != NULL);
2080 		if (vd_errlist != NULL)
2081 			*vd_errlist = errlist;
2082 		else
2083 			fnvlist_free(errlist);
2084 	}
2085 
2086 	return (error);
2087 }
2088 
2089 /*
2090  * Scan the pool.
2091  */
2092 int
2093 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func, pool_scrub_cmd_t cmd)
2094 {
2095 	zfs_cmd_t zc = { 0 };
2096 	char msg[1024];
2097 	int err;
2098 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2099 
2100 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2101 	zc.zc_cookie = func;
2102 	zc.zc_flags = cmd;
2103 
2104 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0)
2105 		return (0);
2106 
2107 	err = errno;
2108 
2109 	/* ECANCELED on a scrub means we resumed a paused scrub */
2110 	if (err == ECANCELED && func == POOL_SCAN_SCRUB &&
2111 	    cmd == POOL_SCRUB_NORMAL)
2112 		return (0);
2113 
2114 	if (err == ENOENT && func != POOL_SCAN_NONE && cmd == POOL_SCRUB_NORMAL)
2115 		return (0);
2116 
2117 	if (func == POOL_SCAN_SCRUB) {
2118 		if (cmd == POOL_SCRUB_PAUSE) {
2119 			(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2120 			    "cannot pause scrubbing %s"), zc.zc_name);
2121 		} else {
2122 			assert(cmd == POOL_SCRUB_NORMAL);
2123 			(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2124 			    "cannot scrub %s"), zc.zc_name);
2125 		}
2126 	} else if (func == POOL_SCAN_RESILVER) {
2127 		assert(cmd == POOL_SCRUB_NORMAL);
2128 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2129 		    "cannot restart resilver on %s"), zc.zc_name);
2130 	} else if (func == POOL_SCAN_NONE) {
2131 		(void) snprintf(msg, sizeof (msg),
2132 		    dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
2133 		    zc.zc_name);
2134 	} else {
2135 		assert(!"unexpected result");
2136 	}
2137 
2138 	if (err == EBUSY) {
2139 		nvlist_t *nvroot;
2140 		pool_scan_stat_t *ps = NULL;
2141 		uint_t psc;
2142 
2143 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
2144 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2145 		(void) nvlist_lookup_uint64_array(nvroot,
2146 		    ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
2147 		if (ps && ps->pss_func == POOL_SCAN_SCRUB) {
2148 			if (cmd == POOL_SCRUB_PAUSE)
2149 				return (zfs_error(hdl, EZFS_SCRUB_PAUSED, msg));
2150 			else
2151 				return (zfs_error(hdl, EZFS_SCRUBBING, msg));
2152 		} else {
2153 			return (zfs_error(hdl, EZFS_RESILVERING, msg));
2154 		}
2155 	} else if (err == ENOENT) {
2156 		return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
2157 	} else if (err == ENOTSUP && func == POOL_SCAN_RESILVER) {
2158 		return (zfs_error(hdl, EZFS_NO_RESILVER_DEFER, msg));
2159 	} else {
2160 		return (zpool_standard_error(hdl, err, msg));
2161 	}
2162 }
2163 
2164 static int
2165 xlate_init_err(int err)
2166 {
2167 	switch (err) {
2168 	case ENODEV:
2169 		return (EZFS_NODEVICE);
2170 	case EINVAL:
2171 	case EROFS:
2172 		return (EZFS_BADDEV);
2173 	case EBUSY:
2174 		return (EZFS_INITIALIZING);
2175 	case ESRCH:
2176 		return (EZFS_NO_INITIALIZE);
2177 	}
2178 	return (err);
2179 }
2180 
2181 /*
2182  * Begin, suspend, or cancel the initialization (initializing of all free
2183  * blocks) for the given vdevs in the given pool.
2184  */
2185 int
2186 zpool_initialize(zpool_handle_t *zhp, pool_initialize_func_t cmd_type,
2187     nvlist_t *vds)
2188 {
2189 	char msg[1024];
2190 	int err;
2191 
2192 	nvlist_t *vdev_guids = fnvlist_alloc();
2193 	nvlist_t *guids_to_paths = fnvlist_alloc();
2194 	nvlist_t *vd_errlist = NULL;
2195 	nvlist_t *errlist;
2196 	nvpair_t *elem;
2197 
2198 	err = zpool_translate_vdev_guids(zhp, vds, vdev_guids,
2199 	    guids_to_paths, &vd_errlist);
2200 
2201 	if (err == 0) {
2202 		err = lzc_initialize(zhp->zpool_name, cmd_type,
2203 		    vdev_guids, &errlist);
2204 		if (err == 0) {
2205 			fnvlist_free(vdev_guids);
2206 			fnvlist_free(guids_to_paths);
2207 			return (0);
2208 		}
2209 
2210 		if (errlist != NULL) {
2211 			vd_errlist = fnvlist_lookup_nvlist(errlist,
2212 			    ZPOOL_INITIALIZE_VDEVS);
2213 		}
2214 
2215 		(void) snprintf(msg, sizeof (msg),
2216 		    dgettext(TEXT_DOMAIN, "operation failed"));
2217 	} else {
2218 		verify(vd_errlist != NULL);
2219 	}
2220 
2221 	for (elem = nvlist_next_nvpair(vd_errlist, NULL); elem != NULL;
2222 	    elem = nvlist_next_nvpair(vd_errlist, elem)) {
2223 		int64_t vd_error = xlate_init_err(fnvpair_value_int64(elem));
2224 		char *path;
2225 
2226 		if (nvlist_lookup_string(guids_to_paths, nvpair_name(elem),
2227 		    &path) != 0)
2228 			path = nvpair_name(elem);
2229 
2230 		(void) zfs_error_fmt(zhp->zpool_hdl, vd_error,
2231 		    "cannot initialize '%s'", path);
2232 	}
2233 
2234 	fnvlist_free(vdev_guids);
2235 	fnvlist_free(guids_to_paths);
2236 
2237 	if (vd_errlist != NULL) {
2238 		fnvlist_free(vd_errlist);
2239 		return (-1);
2240 	}
2241 
2242 	return (zpool_standard_error(zhp->zpool_hdl, err, msg));
2243 }
2244 
2245 static int
2246 xlate_trim_err(int err)
2247 {
2248 	switch (err) {
2249 	case ENODEV:
2250 		return (EZFS_NODEVICE);
2251 	case EINVAL:
2252 	case EROFS:
2253 		return (EZFS_BADDEV);
2254 	case EBUSY:
2255 		return (EZFS_TRIMMING);
2256 	case ESRCH:
2257 		return (EZFS_NO_TRIM);
2258 	case EOPNOTSUPP:
2259 		return (EZFS_TRIM_NOTSUP);
2260 	}
2261 	return (err);
2262 }
2263 
2264 /*
2265  * Begin, suspend, or cancel the TRIM (discarding of all free blocks) for
2266  * the given vdevs in the given pool.
2267  */
2268 int
2269 zpool_trim(zpool_handle_t *zhp, pool_trim_func_t cmd_type, nvlist_t *vds,
2270     trimflags_t *trim_flags)
2271 {
2272 	char msg[1024];
2273 	int err;
2274 
2275 	nvlist_t *vdev_guids = fnvlist_alloc();
2276 	nvlist_t *guids_to_paths = fnvlist_alloc();
2277 	nvlist_t *vd_errlist = NULL;
2278 	nvlist_t *errlist;
2279 	nvpair_t *elem;
2280 
2281 	err = zpool_translate_vdev_guids(zhp, vds, vdev_guids,
2282 	    guids_to_paths, &vd_errlist);
2283 	if (err == 0) {
2284 		err = lzc_trim(zhp->zpool_name, cmd_type, trim_flags->rate,
2285 		    trim_flags->secure, vdev_guids, &errlist);
2286 		if (err == 0) {
2287 			fnvlist_free(vdev_guids);
2288 			fnvlist_free(guids_to_paths);
2289 			return (0);
2290 		}
2291 
2292 		if (errlist != NULL) {
2293 			vd_errlist = fnvlist_lookup_nvlist(errlist,
2294 			    ZPOOL_TRIM_VDEVS);
2295 		}
2296 
2297 		(void) snprintf(msg, sizeof (msg),
2298 		    dgettext(TEXT_DOMAIN, "operation failed"));
2299 	} else {
2300 		verify(vd_errlist != NULL);
2301 	}
2302 
2303 	for (elem = nvlist_next_nvpair(vd_errlist, NULL);
2304 	    elem != NULL; elem = nvlist_next_nvpair(vd_errlist, elem)) {
2305 		int64_t vd_error = xlate_trim_err(fnvpair_value_int64(elem));
2306 		char *path;
2307 		/*
2308 		 * If only the pool was specified, and it was not a secure
2309 		 * trim then suppress warnings for individual vdevs which
2310 		 * do not support trimming.
2311 		 */
2312 		if (vd_error == EZFS_TRIM_NOTSUP &&
2313 		    trim_flags->fullpool &&
2314 		    !trim_flags->secure) {
2315 			continue;
2316 		}
2317 
2318 		if (nvlist_lookup_string(guids_to_paths, nvpair_name(elem),
2319 		    &path) != 0)
2320 			path = nvpair_name(elem);
2321 
2322 		(void) zfs_error_fmt(zhp->zpool_hdl, vd_error,
2323 		    "cannot trim '%s'", path);
2324 	}
2325 
2326 	fnvlist_free(vdev_guids);
2327 	fnvlist_free(guids_to_paths);
2328 
2329 	if (vd_errlist != NULL) {
2330 		fnvlist_free(vd_errlist);
2331 		return (-1);
2332 	}
2333 
2334 	return (zpool_standard_error(zhp->zpool_hdl, err, msg));
2335 }
2336 
2337 /*
2338  * This provides a very minimal check whether a given string is likely a
2339  * c#t#d# style string.  Users of this are expected to do their own
2340  * verification of the s# part.
2341  */
2342 #define	CTD_CHECK(str)  (str && str[0] == 'c' && isdigit(str[1]))
2343 
2344 /*
2345  * More elaborate version for ones which may start with "/dev/dsk/"
2346  * and the like.
2347  */
2348 static int
2349 ctd_check_path(char *str)
2350 {
2351 	/*
2352 	 * If it starts with a slash, check the last component.
2353 	 */
2354 	if (str && str[0] == '/') {
2355 		char *tmp = strrchr(str, '/');
2356 
2357 		/*
2358 		 * If it ends in "/old", check the second-to-last
2359 		 * component of the string instead.
2360 		 */
2361 		if (tmp != str && strcmp(tmp, "/old") == 0) {
2362 			for (tmp--; *tmp != '/'; tmp--)
2363 				;
2364 		}
2365 		str = tmp + 1;
2366 	}
2367 	return (CTD_CHECK(str));
2368 }
2369 
2370 /*
2371  * Find a vdev that matches the search criteria specified. We use the
2372  * the nvpair name to determine how we should look for the device.
2373  * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2374  * spare; but FALSE if its an INUSE spare.
2375  */
2376 static nvlist_t *
2377 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
2378     boolean_t *l2cache, boolean_t *log)
2379 {
2380 	uint_t c, children;
2381 	nvlist_t **child;
2382 	nvlist_t *ret;
2383 	uint64_t is_log;
2384 	char *srchkey;
2385 	nvpair_t *pair = nvlist_next_nvpair(search, NULL);
2386 
2387 	/* Nothing to look for */
2388 	if (search == NULL || pair == NULL)
2389 		return (NULL);
2390 
2391 	/* Obtain the key we will use to search */
2392 	srchkey = nvpair_name(pair);
2393 
2394 	switch (nvpair_type(pair)) {
2395 	case DATA_TYPE_UINT64:
2396 		if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
2397 			uint64_t srchval, theguid;
2398 
2399 			verify(nvpair_value_uint64(pair, &srchval) == 0);
2400 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2401 			    &theguid) == 0);
2402 			if (theguid == srchval)
2403 				return (nv);
2404 		}
2405 		break;
2406 
2407 	case DATA_TYPE_STRING: {
2408 		char *srchval, *val;
2409 
2410 		verify(nvpair_value_string(pair, &srchval) == 0);
2411 		if (nvlist_lookup_string(nv, srchkey, &val) != 0)
2412 			break;
2413 
2414 		/*
2415 		 * Search for the requested value. Special cases:
2416 		 *
2417 		 * - ZPOOL_CONFIG_PATH for whole disk entries. To support
2418 		 *   UEFI boot, these end in "s0" or "s0/old" or "s1" or
2419 		 *   "s1/old".   The "s0" or "s1" part is hidden from the user,
2420 		 *   but included in the string, so this matches around it.
2421 		 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2422 		 *
2423 		 * Otherwise, all other searches are simple string compares.
2424 		 */
2425 		if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
2426 		    ctd_check_path(val)) {
2427 			uint64_t wholedisk = 0;
2428 
2429 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2430 			    &wholedisk);
2431 			if (wholedisk) {
2432 				int slen = strlen(srchval);
2433 				int vlen = strlen(val);
2434 
2435 				if (slen != vlen - 2)
2436 					break;
2437 
2438 				/*
2439 				 * make_leaf_vdev() should only set
2440 				 * wholedisk for ZPOOL_CONFIG_PATHs which
2441 				 * will include "/dev/dsk/", giving plenty of
2442 				 * room for the indices used next.
2443 				 */
2444 				ASSERT(vlen >= 6);
2445 
2446 				/*
2447 				 * strings identical except trailing "s0"
2448 				 */
2449 				if ((strcmp(&val[vlen - 2], "s0") == 0 ||
2450 				    strcmp(&val[vlen - 2], "s1") == 0) &&
2451 				    strncmp(srchval, val, slen) == 0)
2452 					return (nv);
2453 
2454 				/*
2455 				 * strings identical except trailing "s0/old"
2456 				 */
2457 				if ((strcmp(&val[vlen - 6], "s0/old") == 0 ||
2458 				    strcmp(&val[vlen - 6], "s1/old") == 0) &&
2459 				    strcmp(&srchval[slen - 4], "/old") == 0 &&
2460 				    strncmp(srchval, val, slen - 4) == 0)
2461 					return (nv);
2462 
2463 				break;
2464 			}
2465 		} else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2466 			char *type, *idx, *end, *p;
2467 			uint64_t id, vdev_id;
2468 
2469 			/*
2470 			 * Determine our vdev type, keeping in mind
2471 			 * that the srchval is composed of a type and
2472 			 * vdev id pair (i.e. mirror-4).
2473 			 */
2474 			if ((type = strdup(srchval)) == NULL)
2475 				return (NULL);
2476 
2477 			if ((p = strrchr(type, '-')) == NULL) {
2478 				free(type);
2479 				break;
2480 			}
2481 			idx = p + 1;
2482 			*p = '\0';
2483 
2484 			/*
2485 			 * If the types don't match then keep looking.
2486 			 */
2487 			if (strncmp(val, type, strlen(val)) != 0) {
2488 				free(type);
2489 				break;
2490 			}
2491 
2492 			verify(zpool_vdev_is_interior(type));
2493 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
2494 			    &id) == 0);
2495 
2496 			errno = 0;
2497 			vdev_id = strtoull(idx, &end, 10);
2498 
2499 			free(type);
2500 			if (errno != 0)
2501 				return (NULL);
2502 
2503 			/*
2504 			 * Now verify that we have the correct vdev id.
2505 			 */
2506 			if (vdev_id == id)
2507 				return (nv);
2508 		}
2509 
2510 		/*
2511 		 * Common case
2512 		 */
2513 		if (strcmp(srchval, val) == 0)
2514 			return (nv);
2515 		break;
2516 	}
2517 
2518 	default:
2519 		break;
2520 	}
2521 
2522 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2523 	    &child, &children) != 0)
2524 		return (NULL);
2525 
2526 	for (c = 0; c < children; c++) {
2527 		if ((ret = vdev_to_nvlist_iter(child[c], search,
2528 		    avail_spare, l2cache, NULL)) != NULL) {
2529 			/*
2530 			 * The 'is_log' value is only set for the toplevel
2531 			 * vdev, not the leaf vdevs.  So we always lookup the
2532 			 * log device from the root of the vdev tree (where
2533 			 * 'log' is non-NULL).
2534 			 */
2535 			if (log != NULL &&
2536 			    nvlist_lookup_uint64(child[c],
2537 			    ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2538 			    is_log) {
2539 				*log = B_TRUE;
2540 			}
2541 			return (ret);
2542 		}
2543 	}
2544 
2545 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2546 	    &child, &children) == 0) {
2547 		for (c = 0; c < children; c++) {
2548 			if ((ret = vdev_to_nvlist_iter(child[c], search,
2549 			    avail_spare, l2cache, NULL)) != NULL) {
2550 				*avail_spare = B_TRUE;
2551 				return (ret);
2552 			}
2553 		}
2554 	}
2555 
2556 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2557 	    &child, &children) == 0) {
2558 		for (c = 0; c < children; c++) {
2559 			if ((ret = vdev_to_nvlist_iter(child[c], search,
2560 			    avail_spare, l2cache, NULL)) != NULL) {
2561 				*l2cache = B_TRUE;
2562 				return (ret);
2563 			}
2564 		}
2565 	}
2566 
2567 	return (NULL);
2568 }
2569 
2570 /*
2571  * Given a physical path (minus the "/devices" prefix), find the
2572  * associated vdev.
2573  */
2574 nvlist_t *
2575 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2576     boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2577 {
2578 	nvlist_t *search, *nvroot, *ret;
2579 
2580 	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2581 	verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
2582 
2583 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2584 	    &nvroot) == 0);
2585 
2586 	*avail_spare = B_FALSE;
2587 	*l2cache = B_FALSE;
2588 	if (log != NULL)
2589 		*log = B_FALSE;
2590 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2591 	nvlist_free(search);
2592 
2593 	return (ret);
2594 }
2595 
2596 /*
2597  * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2598  */
2599 static boolean_t
2600 zpool_vdev_is_interior(const char *name)
2601 {
2602 	if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2603 	    strncmp(name, VDEV_TYPE_SPARE, strlen(VDEV_TYPE_SPARE)) == 0 ||
2604 	    strncmp(name,
2605 	    VDEV_TYPE_REPLACING, strlen(VDEV_TYPE_REPLACING)) == 0 ||
2606 	    strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
2607 		return (B_TRUE);
2608 	return (B_FALSE);
2609 }
2610 
2611 nvlist_t *
2612 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2613     boolean_t *l2cache, boolean_t *log)
2614 {
2615 	char buf[MAXPATHLEN];
2616 	char *end;
2617 	nvlist_t *nvroot, *search, *ret;
2618 	uint64_t guid;
2619 
2620 	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2621 
2622 	guid = strtoull(path, &end, 10);
2623 	if (guid != 0 && *end == '\0') {
2624 		verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2625 	} else if (zpool_vdev_is_interior(path)) {
2626 		verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2627 	} else if (path[0] != '/') {
2628 		(void) snprintf(buf, sizeof (buf), "%s/%s", ZFS_DISK_ROOT,
2629 		    path);
2630 		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
2631 	} else {
2632 		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2633 	}
2634 
2635 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2636 	    &nvroot) == 0);
2637 
2638 	*avail_spare = B_FALSE;
2639 	*l2cache = B_FALSE;
2640 	if (log != NULL)
2641 		*log = B_FALSE;
2642 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2643 	nvlist_free(search);
2644 
2645 	return (ret);
2646 }
2647 
2648 static int
2649 vdev_is_online(nvlist_t *nv)
2650 {
2651 	uint64_t ival;
2652 
2653 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2654 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2655 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2656 		return (0);
2657 
2658 	return (1);
2659 }
2660 
2661 /*
2662  * Helper function for zpool_get_physpaths().
2663  */
2664 static int
2665 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2666     size_t *bytes_written)
2667 {
2668 	size_t bytes_left, pos, rsz;
2669 	char *tmppath;
2670 	const char *format;
2671 
2672 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2673 	    &tmppath) != 0)
2674 		return (EZFS_NODEVICE);
2675 
2676 	pos = *bytes_written;
2677 	bytes_left = physpath_size - pos;
2678 	format = (pos == 0) ? "%s" : " %s";
2679 
2680 	rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2681 	*bytes_written += rsz;
2682 
2683 	if (rsz >= bytes_left) {
2684 		/* if physpath was not copied properly, clear it */
2685 		if (bytes_left != 0) {
2686 			physpath[pos] = 0;
2687 		}
2688 		return (EZFS_NOSPC);
2689 	}
2690 	return (0);
2691 }
2692 
2693 static int
2694 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2695     size_t *rsz, boolean_t is_spare)
2696 {
2697 	char *type;
2698 	int ret;
2699 
2700 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2701 		return (EZFS_INVALCONFIG);
2702 
2703 	if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2704 		/*
2705 		 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2706 		 * For a spare vdev, we only want to boot from the active
2707 		 * spare device.
2708 		 */
2709 		if (is_spare) {
2710 			uint64_t spare = 0;
2711 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2712 			    &spare);
2713 			if (!spare)
2714 				return (EZFS_INVALCONFIG);
2715 		}
2716 
2717 		if (vdev_is_online(nv)) {
2718 			if ((ret = vdev_get_one_physpath(nv, physpath,
2719 			    phypath_size, rsz)) != 0)
2720 				return (ret);
2721 		}
2722 	} else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2723 	    strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
2724 	    strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2725 	    (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2726 		nvlist_t **child;
2727 		uint_t count;
2728 		int i, ret;
2729 
2730 		if (nvlist_lookup_nvlist_array(nv,
2731 		    ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2732 			return (EZFS_INVALCONFIG);
2733 
2734 		for (i = 0; i < count; i++) {
2735 			ret = vdev_get_physpaths(child[i], physpath,
2736 			    phypath_size, rsz, is_spare);
2737 			if (ret == EZFS_NOSPC)
2738 				return (ret);
2739 		}
2740 	}
2741 
2742 	return (EZFS_POOL_INVALARG);
2743 }
2744 
2745 /*
2746  * Get phys_path for a root pool config.
2747  * Return 0 on success; non-zero on failure.
2748  */
2749 static int
2750 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2751 {
2752 	size_t rsz;
2753 	nvlist_t *vdev_root;
2754 	nvlist_t **child;
2755 	uint_t count;
2756 	char *type;
2757 
2758 	rsz = 0;
2759 
2760 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2761 	    &vdev_root) != 0)
2762 		return (EZFS_INVALCONFIG);
2763 
2764 	if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2765 	    nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2766 	    &child, &count) != 0)
2767 		return (EZFS_INVALCONFIG);
2768 
2769 	/*
2770 	 * root pool can only have a single top-level vdev.
2771 	 */
2772 	if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1)
2773 		return (EZFS_POOL_INVALARG);
2774 
2775 	(void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2776 	    B_FALSE);
2777 
2778 	/* No online devices */
2779 	if (rsz == 0)
2780 		return (EZFS_NODEVICE);
2781 
2782 	return (0);
2783 }
2784 
2785 /*
2786  * Get phys_path for a root pool
2787  * Return 0 on success; non-zero on failure.
2788  */
2789 int
2790 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2791 {
2792 	return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2793 	    phypath_size));
2794 }
2795 
2796 /*
2797  * If the device has being dynamically expanded then we need to relabel
2798  * the disk to use the new unallocated space.
2799  */
2800 static int
2801 zpool_relabel_disk(libzfs_handle_t *hdl, const char *name, const char *msg)
2802 {
2803 	char path[MAXPATHLEN];
2804 	int fd, error;
2805 	int (*_efi_use_whole_disk)(int);
2806 	char drv[MODMAXNAMELEN];
2807 	major_t maj;
2808 	struct stat st;
2809 
2810 	if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
2811 	    "efi_use_whole_disk")) == NULL)
2812 		return (-1);
2813 
2814 	(void) snprintf(path, sizeof (path), "%s/%s", ZFS_RDISK_ROOT, name);
2815 
2816 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2817 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2818 		    "relabel '%s': unable to open device"), name);
2819 		return (zfs_error(hdl, EZFS_OPENFAILED, msg));
2820 	}
2821 
2822 	/*
2823 	 * It's possible that we might encounter an error if the device
2824 	 * does not have any unallocated space left. If so, we simply
2825 	 * ignore that error and continue on.
2826 	 */
2827 	error = _efi_use_whole_disk(fd);
2828 	if (error && error != VT_ENOSPC) {
2829 		(void) close(fd);
2830 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2831 		    "relabel '%s': unable to read disk capacity"), name);
2832 		return (zfs_error(hdl, EZFS_NOCAP, msg));
2833 	}
2834 
2835 	/*
2836 	 * Writing a new EFI partition table to the disk will have marked
2837 	 * the geometry as needing re-validation. Before returning, force
2838 	 * it to be checked by querying the device state, otherwise the
2839 	 * subsequent vdev_reopen() will very likely fail to read the device
2840 	 * size, faulting the pool.
2841 	 *
2842 	 * The dkio(4I) ioctls are implemented by the disk driver rather than
2843 	 * some generic framework, so we limit its use here to drivers with
2844 	 * which it has been tested.
2845 	 */
2846 	if (fstat(fd, &st) == 0 &&
2847 	    (maj = major(st.st_rdev)) != (major_t)NODEV &&
2848 	    modctl(MODGETNAME, drv, sizeof (drv), &maj) == 0 &&
2849 	    (strcmp(drv, "blkdev") == 0 || strcmp(drv, "sd") == 0)) {
2850 		enum dkio_state dkst = DKIO_NONE;
2851 		(void) ioctl(fd, DKIOCSTATE, &dkst);
2852 	}
2853 
2854 	(void) close(fd);
2855 
2856 	return (0);
2857 }
2858 
2859 /*
2860  * Bring the specified vdev online.   The 'flags' parameter is a set of the
2861  * ZFS_ONLINE_* flags.
2862  */
2863 int
2864 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2865     vdev_state_t *newstate)
2866 {
2867 	zfs_cmd_t zc = { 0 };
2868 	char msg[1024];
2869 	char *pathname;
2870 	nvlist_t *tgt;
2871 	boolean_t avail_spare, l2cache, islog;
2872 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2873 	int error;
2874 
2875 	if (flags & ZFS_ONLINE_EXPAND) {
2876 		(void) snprintf(msg, sizeof (msg),
2877 		    dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2878 	} else {
2879 		(void) snprintf(msg, sizeof (msg),
2880 		    dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2881 	}
2882 
2883 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2884 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2885 	    &islog)) == NULL)
2886 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2887 
2888 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2889 
2890 	if (avail_spare)
2891 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
2892 
2893 	if ((flags & ZFS_ONLINE_EXPAND ||
2894 	    zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) &&
2895 	    nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &pathname) == 0) {
2896 		uint64_t wholedisk = 0;
2897 
2898 		(void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2899 		    &wholedisk);
2900 
2901 		/*
2902 		 * XXX - L2ARC 1.0 devices can't support expansion.
2903 		 */
2904 		if (l2cache) {
2905 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2906 			    "cannot expand cache devices"));
2907 			return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2908 		}
2909 
2910 		if (wholedisk) {
2911 			pathname += strlen(ZFS_DISK_ROOT) + 1;
2912 			error = zpool_relabel_disk(hdl, pathname, msg);
2913 			if (error != 0)
2914 				return (error);
2915 		}
2916 	}
2917 
2918 	zc.zc_cookie = VDEV_STATE_ONLINE;
2919 	zc.zc_obj = flags;
2920 
2921 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2922 		if (errno == EINVAL) {
2923 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2924 			    "from this pool into a new one.  Use '%s' "
2925 			    "instead"), "zpool detach");
2926 			return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2927 		}
2928 		return (zpool_standard_error(hdl, errno, msg));
2929 	}
2930 
2931 	*newstate = zc.zc_cookie;
2932 	return (0);
2933 }
2934 
2935 /*
2936  * Take the specified vdev offline
2937  */
2938 int
2939 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2940 {
2941 	zfs_cmd_t zc = { 0 };
2942 	char msg[1024];
2943 	nvlist_t *tgt;
2944 	boolean_t avail_spare, l2cache;
2945 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2946 
2947 	(void) snprintf(msg, sizeof (msg),
2948 	    dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2949 
2950 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2951 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2952 	    NULL)) == NULL)
2953 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2954 
2955 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2956 
2957 	if (avail_spare)
2958 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
2959 
2960 	zc.zc_cookie = VDEV_STATE_OFFLINE;
2961 	zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2962 
2963 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2964 		return (0);
2965 
2966 	switch (errno) {
2967 	case EBUSY:
2968 
2969 		/*
2970 		 * There are no other replicas of this device.
2971 		 */
2972 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2973 
2974 	case EEXIST:
2975 		/*
2976 		 * The log device has unplayed logs
2977 		 */
2978 		return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2979 
2980 	default:
2981 		return (zpool_standard_error(hdl, errno, msg));
2982 	}
2983 }
2984 
2985 /*
2986  * Mark the given vdev faulted.
2987  */
2988 int
2989 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2990 {
2991 	zfs_cmd_t zc = { 0 };
2992 	char msg[1024];
2993 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2994 
2995 	(void) snprintf(msg, sizeof (msg),
2996 	    dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
2997 
2998 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2999 	zc.zc_guid = guid;
3000 	zc.zc_cookie = VDEV_STATE_FAULTED;
3001 	zc.zc_obj = aux;
3002 
3003 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
3004 		return (0);
3005 
3006 	switch (errno) {
3007 	case EBUSY:
3008 
3009 		/*
3010 		 * There are no other replicas of this device.
3011 		 */
3012 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
3013 
3014 	default:
3015 		return (zpool_standard_error(hdl, errno, msg));
3016 	}
3017 
3018 }
3019 
3020 /*
3021  * Mark the given vdev degraded.
3022  */
3023 int
3024 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
3025 {
3026 	zfs_cmd_t zc = { 0 };
3027 	char msg[1024];
3028 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3029 
3030 	(void) snprintf(msg, sizeof (msg),
3031 	    dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
3032 
3033 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3034 	zc.zc_guid = guid;
3035 	zc.zc_cookie = VDEV_STATE_DEGRADED;
3036 	zc.zc_obj = aux;
3037 
3038 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
3039 		return (0);
3040 
3041 	return (zpool_standard_error(hdl, errno, msg));
3042 }
3043 
3044 /*
3045  * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
3046  * a hot spare.
3047  */
3048 static boolean_t
3049 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
3050 {
3051 	nvlist_t **child;
3052 	uint_t c, children;
3053 	char *type;
3054 
3055 	if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
3056 	    &children) == 0) {
3057 		verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
3058 		    &type) == 0);
3059 
3060 		if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
3061 		    children == 2 && child[which] == tgt)
3062 			return (B_TRUE);
3063 
3064 		for (c = 0; c < children; c++)
3065 			if (is_replacing_spare(child[c], tgt, which))
3066 				return (B_TRUE);
3067 	}
3068 
3069 	return (B_FALSE);
3070 }
3071 
3072 /*
3073  * Attach new_disk (fully described by nvroot) to old_disk.
3074  * If 'replacing' is specified, the new disk will replace the old one.
3075  */
3076 int
3077 zpool_vdev_attach(zpool_handle_t *zhp,
3078     const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
3079 {
3080 	zfs_cmd_t zc = { 0 };
3081 	char msg[1024];
3082 	int ret;
3083 	nvlist_t *tgt, *newvd;
3084 	boolean_t avail_spare, l2cache, islog;
3085 	uint64_t val;
3086 	char *newname;
3087 	nvlist_t **child;
3088 	uint_t children;
3089 	nvlist_t *config_root;
3090 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3091 	boolean_t rootpool = zpool_is_bootable(zhp);
3092 
3093 	if (replacing)
3094 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
3095 		    "cannot replace %s with %s"), old_disk, new_disk);
3096 	else
3097 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
3098 		    "cannot attach %s to %s"), new_disk, old_disk);
3099 
3100 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3101 	if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
3102 	    &islog)) == NULL)
3103 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
3104 
3105 	if (avail_spare)
3106 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
3107 
3108 	if (l2cache)
3109 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
3110 
3111 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3112 	zc.zc_cookie = replacing;
3113 
3114 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3115 	    &child, &children) != 0 || children != 1) {
3116 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3117 		    "new device must be a single disk"));
3118 		return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
3119 	}
3120 
3121 	verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
3122 	    ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
3123 
3124 	if ((newname = zpool_vdev_name(NULL, NULL, child[0], 0)) == NULL)
3125 		return (-1);
3126 
3127 	newvd = zpool_find_vdev(zhp, newname, &avail_spare, &l2cache, NULL);
3128 	/*
3129 	 * If the target is a hot spare that has been swapped in, we can only
3130 	 * replace it with another hot spare.
3131 	 */
3132 	if (replacing &&
3133 	    nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
3134 	    (newvd == NULL || !avail_spare) &&
3135 	    is_replacing_spare(config_root, tgt, 1)) {
3136 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3137 		    "can only be replaced by another hot spare"));
3138 		free(newname);
3139 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
3140 	}
3141 
3142 	free(newname);
3143 
3144 	if (replacing && avail_spare && !vdev_is_online(newvd)) {
3145 		(void) zpool_standard_error(hdl, ENXIO, msg);
3146 		return (-1);
3147 	}
3148 
3149 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
3150 		return (-1);
3151 
3152 	ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
3153 
3154 	zcmd_free_nvlists(&zc);
3155 
3156 	if (ret == 0) {
3157 		if (rootpool) {
3158 			/*
3159 			 * XXX need a better way to prevent user from
3160 			 * booting up a half-baked vdev.
3161 			 */
3162 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
3163 			    "sure to wait until resilver is done "
3164 			    "before rebooting.\n"));
3165 		}
3166 		return (0);
3167 	}
3168 
3169 	switch (errno) {
3170 	case ENOTSUP:
3171 		/*
3172 		 * Can't attach to or replace this type of vdev.
3173 		 */
3174 		if (replacing) {
3175 			uint64_t version = zpool_get_prop_int(zhp,
3176 			    ZPOOL_PROP_VERSION, NULL);
3177 
3178 			if (islog)
3179 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3180 				    "cannot replace a log with a spare"));
3181 			else if (version >= SPA_VERSION_MULTI_REPLACE)
3182 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3183 				    "already in replacing/spare config; wait "
3184 				    "for completion or use 'zpool detach'"));
3185 			else
3186 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3187 				    "cannot replace a replacing device"));
3188 		} else {
3189 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3190 			    "can only attach to mirrors and top-level "
3191 			    "disks"));
3192 		}
3193 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
3194 		break;
3195 
3196 	case EINVAL:
3197 		/*
3198 		 * The new device must be a single disk.
3199 		 */
3200 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3201 		    "new device must be a single disk"));
3202 		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
3203 		break;
3204 
3205 	case EBUSY:
3206 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy, "
3207 		    "or device removal is in progress"),
3208 		    new_disk);
3209 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
3210 		break;
3211 
3212 	case EOVERFLOW:
3213 		/*
3214 		 * The new device is too small.
3215 		 */
3216 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3217 		    "device is too small"));
3218 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
3219 		break;
3220 
3221 	case EDOM:
3222 		/*
3223 		 * The new device has a different optimal sector size.
3224 		 */
3225 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3226 		    "new device has a different optimal sector size; use the "
3227 		    "option '-o ashift=N' to override the optimal size"));
3228 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
3229 		break;
3230 
3231 	case ENAMETOOLONG:
3232 		/*
3233 		 * The resulting top-level vdev spec won't fit in the label.
3234 		 */
3235 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
3236 		break;
3237 
3238 	default:
3239 		(void) zpool_standard_error(hdl, errno, msg);
3240 	}
3241 
3242 	return (-1);
3243 }
3244 
3245 /*
3246  * Detach the specified device.
3247  */
3248 int
3249 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
3250 {
3251 	zfs_cmd_t zc = { 0 };
3252 	char msg[1024];
3253 	nvlist_t *tgt;
3254 	boolean_t avail_spare, l2cache;
3255 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3256 
3257 	(void) snprintf(msg, sizeof (msg),
3258 	    dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
3259 
3260 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3261 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3262 	    NULL)) == NULL)
3263 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
3264 
3265 	if (avail_spare)
3266 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
3267 
3268 	if (l2cache)
3269 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
3270 
3271 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3272 
3273 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
3274 		return (0);
3275 
3276 	switch (errno) {
3277 
3278 	case ENOTSUP:
3279 		/*
3280 		 * Can't detach from this type of vdev.
3281 		 */
3282 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
3283 		    "applicable to mirror and replacing vdevs"));
3284 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
3285 		break;
3286 
3287 	case EBUSY:
3288 		/*
3289 		 * There are no other replicas of this device.
3290 		 */
3291 		(void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
3292 		break;
3293 
3294 	default:
3295 		(void) zpool_standard_error(hdl, errno, msg);
3296 	}
3297 
3298 	return (-1);
3299 }
3300 
3301 /*
3302  * Find a mirror vdev in the source nvlist.
3303  *
3304  * The mchild array contains a list of disks in one of the top-level mirrors
3305  * of the source pool.  The schild array contains a list of disks that the
3306  * user specified on the command line.  We loop over the mchild array to
3307  * see if any entry in the schild array matches.
3308  *
3309  * If a disk in the mchild array is found in the schild array, we return
3310  * the index of that entry.  Otherwise we return -1.
3311  */
3312 static int
3313 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
3314     nvlist_t **schild, uint_t schildren)
3315 {
3316 	uint_t mc;
3317 
3318 	for (mc = 0; mc < mchildren; mc++) {
3319 		uint_t sc;
3320 		char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
3321 		    mchild[mc], 0);
3322 
3323 		for (sc = 0; sc < schildren; sc++) {
3324 			char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
3325 			    schild[sc], 0);
3326 			boolean_t result = (strcmp(mpath, spath) == 0);
3327 
3328 			free(spath);
3329 			if (result) {
3330 				free(mpath);
3331 				return (mc);
3332 			}
3333 		}
3334 
3335 		free(mpath);
3336 	}
3337 
3338 	return (-1);
3339 }
3340 
3341 /*
3342  * Split a mirror pool.  If newroot points to null, then a new nvlist
3343  * is generated and it is the responsibility of the caller to free it.
3344  */
3345 int
3346 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
3347     nvlist_t *props, splitflags_t flags)
3348 {
3349 	zfs_cmd_t zc = { 0 };
3350 	char msg[1024];
3351 	nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
3352 	nvlist_t **varray = NULL, *zc_props = NULL;
3353 	uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
3354 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3355 	uint64_t vers;
3356 	boolean_t freelist = B_FALSE, memory_err = B_TRUE;
3357 	int retval = 0;
3358 
3359 	(void) snprintf(msg, sizeof (msg),
3360 	    dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
3361 
3362 	if (!zpool_name_valid(hdl, B_FALSE, newname))
3363 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
3364 
3365 	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
3366 		(void) fprintf(stderr, gettext("Internal error: unable to "
3367 		    "retrieve pool configuration\n"));
3368 		return (-1);
3369 	}
3370 
3371 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
3372 	    == 0);
3373 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
3374 
3375 	if (props) {
3376 		prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
3377 		if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
3378 		    props, vers, flags, msg)) == NULL)
3379 			return (-1);
3380 	}
3381 
3382 	if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
3383 	    &children) != 0) {
3384 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3385 		    "Source pool is missing vdev tree"));
3386 		nvlist_free(zc_props);
3387 		return (-1);
3388 	}
3389 
3390 	varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
3391 	vcount = 0;
3392 
3393 	if (*newroot == NULL ||
3394 	    nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
3395 	    &newchild, &newchildren) != 0)
3396 		newchildren = 0;
3397 
3398 	for (c = 0; c < children; c++) {
3399 		uint64_t is_log = B_FALSE, is_hole = B_FALSE;
3400 		char *type;
3401 		nvlist_t **mchild, *vdev;
3402 		uint_t mchildren;
3403 		int entry;
3404 
3405 		/*
3406 		 * Unlike cache & spares, slogs are stored in the
3407 		 * ZPOOL_CONFIG_CHILDREN array.  We filter them out here.
3408 		 */
3409 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3410 		    &is_log);
3411 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
3412 		    &is_hole);
3413 		if (is_log || is_hole) {
3414 			/*
3415 			 * Create a hole vdev and put it in the config.
3416 			 */
3417 			if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
3418 				goto out;
3419 			if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
3420 			    VDEV_TYPE_HOLE) != 0)
3421 				goto out;
3422 			if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
3423 			    1) != 0)
3424 				goto out;
3425 			if (lastlog == 0)
3426 				lastlog = vcount;
3427 			varray[vcount++] = vdev;
3428 			continue;
3429 		}
3430 		lastlog = 0;
3431 		verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
3432 		    == 0);
3433 		if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
3434 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3435 			    "Source pool must be composed only of mirrors\n"));
3436 			retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3437 			goto out;
3438 		}
3439 
3440 		verify(nvlist_lookup_nvlist_array(child[c],
3441 		    ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3442 
3443 		/* find or add an entry for this top-level vdev */
3444 		if (newchildren > 0 &&
3445 		    (entry = find_vdev_entry(zhp, mchild, mchildren,
3446 		    newchild, newchildren)) >= 0) {
3447 			/* We found a disk that the user specified. */
3448 			vdev = mchild[entry];
3449 			++found;
3450 		} else {
3451 			/* User didn't specify a disk for this vdev. */
3452 			vdev = mchild[mchildren - 1];
3453 		}
3454 
3455 		if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3456 			goto out;
3457 	}
3458 
3459 	/* did we find every disk the user specified? */
3460 	if (found != newchildren) {
3461 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
3462 		    "include at most one disk from each mirror"));
3463 		retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3464 		goto out;
3465 	}
3466 
3467 	/* Prepare the nvlist for populating. */
3468 	if (*newroot == NULL) {
3469 		if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
3470 			goto out;
3471 		freelist = B_TRUE;
3472 		if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
3473 		    VDEV_TYPE_ROOT) != 0)
3474 			goto out;
3475 	} else {
3476 		verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
3477 	}
3478 
3479 	/* Add all the children we found */
3480 	if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
3481 	    lastlog == 0 ? vcount : lastlog) != 0)
3482 		goto out;
3483 
3484 	/*
3485 	 * If we're just doing a dry run, exit now with success.
3486 	 */
3487 	if (flags.dryrun) {
3488 		memory_err = B_FALSE;
3489 		freelist = B_FALSE;
3490 		goto out;
3491 	}
3492 
3493 	/* now build up the config list & call the ioctl */
3494 	if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
3495 		goto out;
3496 
3497 	if (nvlist_add_nvlist(newconfig,
3498 	    ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
3499 	    nvlist_add_string(newconfig,
3500 	    ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
3501 	    nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
3502 		goto out;
3503 
3504 	/*
3505 	 * The new pool is automatically part of the namespace unless we
3506 	 * explicitly export it.
3507 	 */
3508 	if (!flags.import)
3509 		zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
3510 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3511 	(void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
3512 	if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
3513 		goto out;
3514 	if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
3515 		goto out;
3516 
3517 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
3518 		retval = zpool_standard_error(hdl, errno, msg);
3519 		goto out;
3520 	}
3521 
3522 	freelist = B_FALSE;
3523 	memory_err = B_FALSE;
3524 
3525 out:
3526 	if (varray != NULL) {
3527 		int v;
3528 
3529 		for (v = 0; v < vcount; v++)
3530 			nvlist_free(varray[v]);
3531 		free(varray);
3532 	}
3533 	zcmd_free_nvlists(&zc);
3534 	nvlist_free(zc_props);
3535 	nvlist_free(newconfig);
3536 	if (freelist) {
3537 		nvlist_free(*newroot);
3538 		*newroot = NULL;
3539 	}
3540 
3541 	if (retval != 0)
3542 		return (retval);
3543 
3544 	if (memory_err)
3545 		return (no_memory(hdl));
3546 
3547 	return (0);
3548 }
3549 
3550 /*
3551  * Remove the given device.
3552  */
3553 int
3554 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
3555 {
3556 	zfs_cmd_t zc = { 0 };
3557 	char msg[1024];
3558 	nvlist_t *tgt;
3559 	boolean_t avail_spare, l2cache, islog;
3560 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3561 	uint64_t version;
3562 
3563 	(void) snprintf(msg, sizeof (msg),
3564 	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3565 
3566 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3567 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3568 	    &islog)) == NULL)
3569 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
3570 
3571 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3572 	if (islog && version < SPA_VERSION_HOLES) {
3573 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3574 		    "pool must be upgraded to support log removal"));
3575 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
3576 	}
3577 
3578 	zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
3579 
3580 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3581 		return (0);
3582 
3583 	switch (errno) {
3584 
3585 	case EINVAL:
3586 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3587 		    "invalid config; all top-level vdevs must "
3588 		    "have the same sector size and not be raidz."));
3589 		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
3590 		break;
3591 
3592 	case EBUSY:
3593 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3594 		    "Pool busy; removal may already be in progress"));
3595 		(void) zfs_error(hdl, EZFS_BUSY, msg);
3596 		break;
3597 
3598 	default:
3599 		(void) zpool_standard_error(hdl, errno, msg);
3600 	}
3601 	return (-1);
3602 }
3603 
3604 int
3605 zpool_vdev_remove_cancel(zpool_handle_t *zhp)
3606 {
3607 	zfs_cmd_t zc = { 0 };
3608 	char msg[1024];
3609 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3610 
3611 	(void) snprintf(msg, sizeof (msg),
3612 	    dgettext(TEXT_DOMAIN, "cannot cancel removal"));
3613 
3614 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3615 	zc.zc_cookie = 1;
3616 
3617 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3618 		return (0);
3619 
3620 	return (zpool_standard_error(hdl, errno, msg));
3621 }
3622 
3623 int
3624 zpool_vdev_indirect_size(zpool_handle_t *zhp, const char *path,
3625     uint64_t *sizep)
3626 {
3627 	char msg[1024];
3628 	nvlist_t *tgt;
3629 	boolean_t avail_spare, l2cache, islog;
3630 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3631 
3632 	(void) snprintf(msg, sizeof (msg),
3633 	    dgettext(TEXT_DOMAIN, "cannot determine indirect size of %s"),
3634 	    path);
3635 
3636 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3637 	    &islog)) == NULL)
3638 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
3639 
3640 	if (avail_spare || l2cache || islog) {
3641 		*sizep = 0;
3642 		return (0);
3643 	}
3644 
3645 	if (nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_INDIRECT_SIZE, sizep) != 0) {
3646 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3647 		    "indirect size not available"));
3648 		return (zfs_error(hdl, EINVAL, msg));
3649 	}
3650 	return (0);
3651 }
3652 
3653 /*
3654  * Clear the errors for the pool, or the particular device if specified.
3655  */
3656 int
3657 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3658 {
3659 	zfs_cmd_t zc = { 0 };
3660 	char msg[1024];
3661 	nvlist_t *tgt;
3662 	zpool_load_policy_t policy;
3663 	boolean_t avail_spare, l2cache;
3664 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3665 	nvlist_t *nvi = NULL;
3666 	int error;
3667 
3668 	if (path)
3669 		(void) snprintf(msg, sizeof (msg),
3670 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3671 		    path);
3672 	else
3673 		(void) snprintf(msg, sizeof (msg),
3674 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3675 		    zhp->zpool_name);
3676 
3677 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3678 	if (path) {
3679 		if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3680 		    &l2cache, NULL)) == NULL)
3681 			return (zfs_error(hdl, EZFS_NODEVICE, msg));
3682 
3683 		/*
3684 		 * Don't allow error clearing for hot spares.  Do allow
3685 		 * error clearing for l2cache devices.
3686 		 */
3687 		if (avail_spare)
3688 			return (zfs_error(hdl, EZFS_ISSPARE, msg));
3689 
3690 		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
3691 		    &zc.zc_guid) == 0);
3692 	}
3693 
3694 	zpool_get_load_policy(rewindnvl, &policy);
3695 	zc.zc_cookie = policy.zlp_rewind;
3696 
3697 	if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3698 		return (-1);
3699 
3700 	if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3701 		return (-1);
3702 
3703 	while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
3704 	    errno == ENOMEM) {
3705 		if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3706 			zcmd_free_nvlists(&zc);
3707 			return (-1);
3708 		}
3709 	}
3710 
3711 	if (!error || ((policy.zlp_rewind & ZPOOL_TRY_REWIND) &&
3712 	    errno != EPERM && errno != EACCES)) {
3713 		if (policy.zlp_rewind &
3714 		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3715 			(void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3716 			zpool_rewind_exclaim(hdl, zc.zc_name,
3717 			    ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0),
3718 			    nvi);
3719 			nvlist_free(nvi);
3720 		}
3721 		zcmd_free_nvlists(&zc);
3722 		return (0);
3723 	}
3724 
3725 	zcmd_free_nvlists(&zc);
3726 	return (zpool_standard_error(hdl, errno, msg));
3727 }
3728 
3729 /*
3730  * Similar to zpool_clear(), but takes a GUID (used by fmd).
3731  */
3732 int
3733 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
3734 {
3735 	zfs_cmd_t zc = { 0 };
3736 	char msg[1024];
3737 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3738 
3739 	(void) snprintf(msg, sizeof (msg),
3740 	    dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
3741 	    guid);
3742 
3743 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3744 	zc.zc_guid = guid;
3745 	zc.zc_cookie = ZPOOL_NO_REWIND;
3746 
3747 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
3748 		return (0);
3749 
3750 	return (zpool_standard_error(hdl, errno, msg));
3751 }
3752 
3753 /*
3754  * Change the GUID for a pool.
3755  */
3756 int
3757 zpool_reguid(zpool_handle_t *zhp)
3758 {
3759 	char msg[1024];
3760 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3761 	zfs_cmd_t zc = { 0 };
3762 
3763 	(void) snprintf(msg, sizeof (msg),
3764 	    dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3765 
3766 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3767 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3768 		return (0);
3769 
3770 	return (zpool_standard_error(hdl, errno, msg));
3771 }
3772 
3773 /*
3774  * Reopen the pool.
3775  */
3776 int
3777 zpool_reopen(zpool_handle_t *zhp)
3778 {
3779 	zfs_cmd_t zc = { 0 };
3780 	char msg[1024];
3781 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3782 
3783 	(void) snprintf(msg, sizeof (msg),
3784 	    dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
3785 	    zhp->zpool_name);
3786 
3787 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3788 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
3789 		return (0);
3790 	return (zpool_standard_error(hdl, errno, msg));
3791 }
3792 
3793 /* call into libzfs_core to execute the sync IOCTL per pool */
3794 int
3795 zpool_sync_one(zpool_handle_t *zhp, void *data)
3796 {
3797 	int ret;
3798 	libzfs_handle_t *hdl = zpool_get_handle(zhp);
3799 	const char *pool_name = zpool_get_name(zhp);
3800 	boolean_t *force = data;
3801 	nvlist_t *innvl = fnvlist_alloc();
3802 
3803 	fnvlist_add_boolean_value(innvl, "force", *force);
3804 	if ((ret = lzc_sync(pool_name, innvl, NULL)) != 0) {
3805 		nvlist_free(innvl);
3806 		return (zpool_standard_error_fmt(hdl, ret,
3807 		    dgettext(TEXT_DOMAIN, "sync '%s' failed"), pool_name));
3808 	}
3809 	nvlist_free(innvl);
3810 
3811 	return (0);
3812 }
3813 
3814 /*
3815  * Convert from a devid string to a path.
3816  */
3817 static char *
3818 devid_to_path(char *devid_str)
3819 {
3820 	ddi_devid_t devid;
3821 	char *minor;
3822 	char *path;
3823 	devid_nmlist_t *list = NULL;
3824 	int ret;
3825 
3826 	if (devid_str_decode(devid_str, &devid, &minor) != 0)
3827 		return (NULL);
3828 
3829 	ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3830 
3831 	devid_str_free(minor);
3832 	devid_free(devid);
3833 
3834 	if (ret != 0)
3835 		return (NULL);
3836 
3837 	/*
3838 	 * In a case the strdup() fails, we will just return NULL below.
3839 	 */
3840 	path = strdup(list[0].devname);
3841 
3842 	devid_free_nmlist(list);
3843 
3844 	return (path);
3845 }
3846 
3847 /*
3848  * Convert from a path to a devid string.
3849  */
3850 static char *
3851 path_to_devid(const char *path)
3852 {
3853 	int fd;
3854 	ddi_devid_t devid;
3855 	char *minor, *ret;
3856 
3857 	if ((fd = open(path, O_RDONLY)) < 0)
3858 		return (NULL);
3859 
3860 	minor = NULL;
3861 	ret = NULL;
3862 	if (devid_get(fd, &devid) == 0) {
3863 		if (devid_get_minor_name(fd, &minor) == 0)
3864 			ret = devid_str_encode(devid, minor);
3865 		if (minor != NULL)
3866 			devid_str_free(minor);
3867 		devid_free(devid);
3868 	}
3869 	(void) close(fd);
3870 
3871 	return (ret);
3872 }
3873 
3874 struct path_from_physpath_walker_args {
3875 	char *pfpwa_path;
3876 };
3877 
3878 /*
3879  * Walker for use with di_devlink_walk().  Stores the "/dev" path of the first
3880  * primary devlink (i.e., the first devlink which refers to our "/devices"
3881  * node) and stops walking.
3882  */
3883 static int
3884 path_from_physpath_walker(di_devlink_t devlink, void *arg)
3885 {
3886 	struct path_from_physpath_walker_args *pfpwa = arg;
3887 
3888 	if (di_devlink_type(devlink) != DI_PRIMARY_LINK) {
3889 		return (DI_WALK_CONTINUE);
3890 	}
3891 
3892 	verify(pfpwa->pfpwa_path == NULL);
3893 	if ((pfpwa->pfpwa_path = strdup(di_devlink_path(devlink))) != NULL) {
3894 		return (DI_WALK_TERMINATE);
3895 	}
3896 
3897 	return (DI_WALK_CONTINUE);
3898 }
3899 
3900 /*
3901  * Search for a "/dev" path that refers to our physical path.  Returns the new
3902  * path if one is found and it does not match the existing "path" value.  If
3903  * the value is unchanged, or one could not be found, returns NULL.
3904  */
3905 static char *
3906 path_from_physpath(libzfs_handle_t *hdl, const char *path,
3907     const char *physpath)
3908 {
3909 	struct path_from_physpath_walker_args pfpwa;
3910 
3911 	if (physpath == NULL) {
3912 		return (NULL);
3913 	}
3914 
3915 	if (hdl->libzfs_devlink == NULL) {
3916 		if ((hdl->libzfs_devlink = di_devlink_init(NULL, 0)) ==
3917 		    DI_LINK_NIL) {
3918 			/*
3919 			 * We may not be able to open a handle if this process
3920 			 * is insufficiently privileged, or we are too early in
3921 			 * boot for devfsadm to be ready.  Ignore this error
3922 			 * and defer the path check to a subsequent run.
3923 			 */
3924 			return (NULL);
3925 		}
3926 	}
3927 
3928 	pfpwa.pfpwa_path = NULL;
3929 	(void) di_devlink_walk(hdl->libzfs_devlink, NULL, physpath,
3930 	    DI_PRIMARY_LINK, &pfpwa, path_from_physpath_walker);
3931 
3932 	if (path != NULL && pfpwa.pfpwa_path != NULL &&
3933 	    strcmp(path, pfpwa.pfpwa_path) == 0) {
3934 		/*
3935 		 * If the path is already correct, no change is required.
3936 		 */
3937 		free(pfpwa.pfpwa_path);
3938 		return (NULL);
3939 	}
3940 
3941 	return (pfpwa.pfpwa_path);
3942 }
3943 
3944 /*
3945  * Issue the necessary ioctl() to update the stored path value for the vdev.  We
3946  * ignore any failure here, since a common case is for an unprivileged user to
3947  * type 'zpool status', and we'll display the correct information anyway.
3948  */
3949 static void
3950 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3951 {
3952 	zfs_cmd_t zc = { 0 };
3953 
3954 	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3955 	(void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3956 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3957 	    &zc.zc_guid) == 0);
3958 
3959 	(void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3960 }
3961 
3962 /*
3963  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
3964  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3965  * We also check if this is a whole disk, in which case we strip off the
3966  * trailing 's0' slice name.
3967  *
3968  * This routine is also responsible for identifying when disks have been
3969  * reconfigured in a new location.  The kernel will have opened the device by
3970  * devid, but the path will still refer to the old location.  To catch this, we
3971  * first do a path -> devid translation (which is fast for the common case).  If
3972  * the devid matches, we're done.  If not, we do a reverse devid -> path
3973  * translation and issue the appropriate ioctl() to update the path of the vdev.
3974  * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3975  * of these checks.
3976  */
3977 char *
3978 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3979     int name_flags)
3980 {
3981 	char *path, *type, *env;
3982 	uint64_t value;
3983 	char buf[64];
3984 
3985 	/*
3986 	 * vdev_name will be "root"/"root-0" for the root vdev, but it is the
3987 	 * zpool name that will be displayed to the user.
3988 	 */
3989 	verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
3990 	if (zhp != NULL && strcmp(type, "root") == 0)
3991 		return (zfs_strdup(hdl, zpool_get_name(zhp)));
3992 
3993 	env = getenv("ZPOOL_VDEV_NAME_PATH");
3994 	if (env && (strtoul(env, NULL, 0) > 0 ||
3995 	    !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
3996 		name_flags |= VDEV_NAME_PATH;
3997 
3998 	env = getenv("ZPOOL_VDEV_NAME_GUID");
3999 	if (env && (strtoul(env, NULL, 0) > 0 ||
4000 	    !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
4001 		name_flags |= VDEV_NAME_GUID;
4002 
4003 	env = getenv("ZPOOL_VDEV_NAME_FOLLOW_LINKS");
4004 	if (env && (strtoul(env, NULL, 0) > 0 ||
4005 	    !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
4006 		name_flags |= VDEV_NAME_FOLLOW_LINKS;
4007 
4008 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
4009 	    name_flags & VDEV_NAME_GUID) {
4010 		nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value);
4011 		(void) snprintf(buf, sizeof (buf), "%llu", (u_longlong_t)value);
4012 		path = buf;
4013 	} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
4014 		vdev_stat_t *vs;
4015 		uint_t vsc;
4016 		char *newpath = NULL;
4017 		char *physpath = NULL;
4018 		char *devid = NULL;
4019 
4020 		/*
4021 		 * If the device is dead (faulted, offline, etc) then don't
4022 		 * bother opening it.  Otherwise we may be forcing the user to
4023 		 * open a misbehaving device, which can have undesirable
4024 		 * effects.
4025 		 */
4026 		if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
4027 		    (uint64_t **)&vs, &vsc) != 0 ||
4028 		    vs->vs_state < VDEV_STATE_DEGRADED ||
4029 		    zhp == NULL) {
4030 			goto after_open;
4031 		}
4032 
4033 		if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
4034 			/*
4035 			 * This vdev has a devid.  We can use it to check the
4036 			 * current path.
4037 			 */
4038 			char *newdevid = path_to_devid(path);
4039 
4040 			if (newdevid == NULL || strcmp(devid, newdevid) != 0) {
4041 				newpath = devid_to_path(devid);
4042 			}
4043 
4044 			if (newdevid != NULL)
4045 				devid_str_free(newdevid);
4046 
4047 		} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
4048 		    &physpath) == 0) {
4049 			/*
4050 			 * This vdev does not have a devid, but it does have a
4051 			 * physical path.  Attempt to translate this to a /dev
4052 			 * path.
4053 			 */
4054 			newpath = path_from_physpath(hdl, path, physpath);
4055 		}
4056 
4057 		if (newpath != NULL) {
4058 			/*
4059 			 * Update the path appropriately.
4060 			 */
4061 			set_path(zhp, nv, newpath);
4062 			if (nvlist_add_string(nv, ZPOOL_CONFIG_PATH,
4063 			    newpath) == 0) {
4064 				verify(nvlist_lookup_string(nv,
4065 				    ZPOOL_CONFIG_PATH, &path) == 0);
4066 			}
4067 			free(newpath);
4068 		}
4069 
4070 		if (name_flags & VDEV_NAME_FOLLOW_LINKS) {
4071 			char *rp = realpath(path, NULL);
4072 			if (rp) {
4073 				strlcpy(buf, rp, sizeof (buf));
4074 				path = buf;
4075 				free(rp);
4076 			}
4077 		}
4078 
4079 after_open:
4080 		if (strncmp(path, ZFS_DISK_ROOTD, strlen(ZFS_DISK_ROOTD)) == 0)
4081 			path += strlen(ZFS_DISK_ROOTD);
4082 
4083 		/*
4084 		 * Remove the partition from the path it this is a whole disk.
4085 		 */
4086 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, &value)
4087 		    == 0 && value && !(name_flags & VDEV_NAME_PATH)) {
4088 			int pathlen = strlen(path);
4089 			char *tmp = zfs_strdup(hdl, path);
4090 
4091 			/*
4092 			 * If it starts with c#, and ends with "s0" or "s1",
4093 			 * chop the slice off, or if it ends with "s0/old" or
4094 			 * "s1/old", remove the slice from the middle.
4095 			 */
4096 			if (CTD_CHECK(tmp)) {
4097 				if (strcmp(&tmp[pathlen - 2], "s0") == 0 ||
4098 				    strcmp(&tmp[pathlen - 2], "s1") == 0) {
4099 					tmp[pathlen - 2] = '\0';
4100 				} else if (pathlen > 6 &&
4101 				    (strcmp(&tmp[pathlen - 6], "s0/old") == 0 ||
4102 				    strcmp(&tmp[pathlen - 6], "s1/old") == 0)) {
4103 					(void) strcpy(&tmp[pathlen - 6],
4104 					    "/old");
4105 				}
4106 			}
4107 			return (tmp);
4108 		}
4109 	} else {
4110 		path = type;
4111 
4112 		/*
4113 		 * If it's a raidz device, we need to stick in the parity level.
4114 		 */
4115 		if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
4116 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
4117 			    &value) == 0);
4118 			(void) snprintf(buf, sizeof (buf), "%s%llu", path,
4119 			    (u_longlong_t)value);
4120 			path = buf;
4121 		}
4122 
4123 		/*
4124 		 * We identify each top-level vdev by using a <type-id>
4125 		 * naming convention.
4126 		 */
4127 		if (name_flags & VDEV_NAME_TYPE_ID) {
4128 			uint64_t id;
4129 
4130 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
4131 			    &id) == 0);
4132 			(void) snprintf(buf, sizeof (buf), "%s-%llu", path,
4133 			    (u_longlong_t)id);
4134 			path = buf;
4135 		}
4136 	}
4137 
4138 	return (zfs_strdup(hdl, path));
4139 }
4140 
4141 static int
4142 zbookmark_mem_compare(const void *a, const void *b)
4143 {
4144 	return (memcmp(a, b, sizeof (zbookmark_phys_t)));
4145 }
4146 
4147 /*
4148  * Retrieve the persistent error log, uniquify the members, and return to the
4149  * caller.
4150  */
4151 int
4152 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
4153 {
4154 	zfs_cmd_t zc = { 0 };
4155 	uint64_t count;
4156 	zbookmark_phys_t *zb = NULL;
4157 	int i;
4158 
4159 	/*
4160 	 * Retrieve the raw error list from the kernel.  If the number of errors
4161 	 * has increased, allocate more space and continue until we get the
4162 	 * entire list.
4163 	 */
4164 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
4165 	    &count) == 0);
4166 	if (count == 0)
4167 		return (0);
4168 	if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
4169 	    count * sizeof (zbookmark_phys_t))) == (uintptr_t)NULL)
4170 		return (-1);
4171 	zc.zc_nvlist_dst_size = count;
4172 	(void) strcpy(zc.zc_name, zhp->zpool_name);
4173 	for (;;) {
4174 		if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
4175 		    &zc) != 0) {
4176 			free((void *)(uintptr_t)zc.zc_nvlist_dst);
4177 			if (errno == ENOMEM) {
4178 				void *dst;
4179 
4180 				count = zc.zc_nvlist_dst_size;
4181 				dst = zfs_alloc(zhp->zpool_hdl, count *
4182 				    sizeof (zbookmark_phys_t));
4183 				if (dst == NULL)
4184 					return (-1);
4185 				zc.zc_nvlist_dst = (uintptr_t)dst;
4186 			} else {
4187 				return (-1);
4188 			}
4189 		} else {
4190 			break;
4191 		}
4192 	}
4193 
4194 	/*
4195 	 * Sort the resulting bookmarks.  This is a little confusing due to the
4196 	 * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
4197 	 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
4198 	 * _not_ copied as part of the process.  So we point the start of our
4199 	 * array appropriate and decrement the total number of elements.
4200 	 */
4201 	zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) +
4202 	    zc.zc_nvlist_dst_size;
4203 	count -= zc.zc_nvlist_dst_size;
4204 
4205 	qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare);
4206 
4207 	verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
4208 
4209 	/*
4210 	 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
4211 	 */
4212 	for (i = 0; i < count; i++) {
4213 		nvlist_t *nv;
4214 
4215 		/* ignoring zb_blkid and zb_level for now */
4216 		if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
4217 		    zb[i-1].zb_object == zb[i].zb_object)
4218 			continue;
4219 
4220 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
4221 			goto nomem;
4222 		if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
4223 		    zb[i].zb_objset) != 0) {
4224 			nvlist_free(nv);
4225 			goto nomem;
4226 		}
4227 		if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
4228 		    zb[i].zb_object) != 0) {
4229 			nvlist_free(nv);
4230 			goto nomem;
4231 		}
4232 		if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
4233 			nvlist_free(nv);
4234 			goto nomem;
4235 		}
4236 		nvlist_free(nv);
4237 	}
4238 
4239 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
4240 	return (0);
4241 
4242 nomem:
4243 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
4244 	return (no_memory(zhp->zpool_hdl));
4245 }
4246 
4247 /*
4248  * Upgrade a ZFS pool to the latest on-disk version.
4249  */
4250 int
4251 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
4252 {
4253 	zfs_cmd_t zc = { 0 };
4254 	libzfs_handle_t *hdl = zhp->zpool_hdl;
4255 
4256 	(void) strcpy(zc.zc_name, zhp->zpool_name);
4257 	zc.zc_cookie = new_version;
4258 
4259 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
4260 		return (zpool_standard_error_fmt(hdl, errno,
4261 		    dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
4262 		    zhp->zpool_name));
4263 	return (0);
4264 }
4265 
4266 void
4267 zfs_save_arguments(int argc, char **argv, char *string, int len)
4268 {
4269 	(void) strlcpy(string, basename(argv[0]), len);
4270 	for (int i = 1; i < argc; i++) {
4271 		(void) strlcat(string, " ", len);
4272 		(void) strlcat(string, argv[i], len);
4273 	}
4274 }
4275 
4276 int
4277 zpool_log_history(libzfs_handle_t *hdl, const char *message)
4278 {
4279 	zfs_cmd_t zc = { 0 };
4280 	nvlist_t *args;
4281 	int err;
4282 
4283 	args = fnvlist_alloc();
4284 	fnvlist_add_string(args, "message", message);
4285 	err = zcmd_write_src_nvlist(hdl, &zc, args);
4286 	if (err == 0)
4287 		err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc);
4288 	nvlist_free(args);
4289 	zcmd_free_nvlists(&zc);
4290 	return (err);
4291 }
4292 
4293 /*
4294  * Perform ioctl to get some command history of a pool.
4295  *
4296  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
4297  * logical offset of the history buffer to start reading from.
4298  *
4299  * Upon return, 'off' is the next logical offset to read from and
4300  * 'len' is the actual amount of bytes read into 'buf'.
4301  */
4302 static int
4303 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
4304 {
4305 	zfs_cmd_t zc = { 0 };
4306 	libzfs_handle_t *hdl = zhp->zpool_hdl;
4307 
4308 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4309 
4310 	zc.zc_history = (uint64_t)(uintptr_t)buf;
4311 	zc.zc_history_len = *len;
4312 	zc.zc_history_offset = *off;
4313 
4314 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
4315 		switch (errno) {
4316 		case EPERM:
4317 			return (zfs_error_fmt(hdl, EZFS_PERM,
4318 			    dgettext(TEXT_DOMAIN,
4319 			    "cannot show history for pool '%s'"),
4320 			    zhp->zpool_name));
4321 		case ENOENT:
4322 			return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
4323 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
4324 			    "'%s'"), zhp->zpool_name));
4325 		case ENOTSUP:
4326 			return (zfs_error_fmt(hdl, EZFS_BADVERSION,
4327 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
4328 			    "'%s', pool must be upgraded"), zhp->zpool_name));
4329 		default:
4330 			return (zpool_standard_error_fmt(hdl, errno,
4331 			    dgettext(TEXT_DOMAIN,
4332 			    "cannot get history for '%s'"), zhp->zpool_name));
4333 		}
4334 	}
4335 
4336 	*len = zc.zc_history_len;
4337 	*off = zc.zc_history_offset;
4338 
4339 	return (0);
4340 }
4341 
4342 /*
4343  * Retrieve the command history of a pool.
4344  */
4345 int
4346 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp, uint64_t *off,
4347     boolean_t *eof)
4348 {
4349 	char *buf;
4350 	int buflen = 128 * 1024;
4351 	nvlist_t **records = NULL;
4352 	uint_t numrecords = 0;
4353 	int err = 0, i;
4354 	uint64_t start = *off;
4355 
4356 	buf = malloc(buflen);
4357 	if (buf == NULL)
4358 		return (ENOMEM);
4359 	/* process about 1MB a time */
4360 	while (*off - start < 1024 * 1024) {
4361 		uint64_t bytes_read = buflen;
4362 		uint64_t leftover;
4363 
4364 		if ((err = get_history(zhp, buf, off, &bytes_read)) != 0)
4365 			break;
4366 
4367 		/* if nothing else was read in, we're at EOF, just return */
4368 		if (!bytes_read) {
4369 			*eof = B_TRUE;
4370 			break;
4371 		}
4372 
4373 		if ((err = zpool_history_unpack(buf, bytes_read,
4374 		    &leftover, &records, &numrecords)) != 0)
4375 			break;
4376 		*off -= leftover;
4377 		if (leftover == bytes_read) {
4378 			/*
4379 			 * no progress made, because buffer is not big enough
4380 			 * to hold this record; resize and retry.
4381 			 */
4382 			buflen *= 2;
4383 			free(buf);
4384 			buf = malloc(buflen);
4385 			if (buf == NULL)
4386 				return (ENOMEM);
4387 		}
4388 	}
4389 
4390 	free(buf);
4391 
4392 	if (!err) {
4393 		verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
4394 		verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
4395 		    records, numrecords) == 0);
4396 	}
4397 	for (i = 0; i < numrecords; i++)
4398 		nvlist_free(records[i]);
4399 	free(records);
4400 
4401 	return (err);
4402 }
4403 
4404 void
4405 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
4406     char *pathname, size_t len)
4407 {
4408 	zfs_cmd_t zc = { 0 };
4409 	boolean_t mounted = B_FALSE;
4410 	char *mntpnt = NULL;
4411 	char dsname[ZFS_MAX_DATASET_NAME_LEN];
4412 
4413 	if (dsobj == 0) {
4414 		/* special case for the MOS */
4415 		(void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
4416 		return;
4417 	}
4418 
4419 	/* get the dataset's name */
4420 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4421 	zc.zc_obj = dsobj;
4422 	if (ioctl(zhp->zpool_hdl->libzfs_fd,
4423 	    ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
4424 		/* just write out a path of two object numbers */
4425 		(void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
4426 		    dsobj, obj);
4427 		return;
4428 	}
4429 	(void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
4430 
4431 	/* find out if the dataset is mounted */
4432 	mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
4433 
4434 	/* get the corrupted object's path */
4435 	(void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
4436 	zc.zc_obj = obj;
4437 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
4438 	    &zc) == 0) {
4439 		if (mounted) {
4440 			(void) snprintf(pathname, len, "%s%s", mntpnt,
4441 			    zc.zc_value);
4442 		} else {
4443 			(void) snprintf(pathname, len, "%s:%s",
4444 			    dsname, zc.zc_value);
4445 		}
4446 	} else {
4447 		(void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
4448 	}
4449 	free(mntpnt);
4450 }
4451 
4452 int
4453 zpool_set_bootenv(zpool_handle_t *zhp, const nvlist_t *envmap)
4454 {
4455 	int error = lzc_set_bootenv(zhp->zpool_name, envmap);
4456 	if (error != 0) {
4457 		(void) zpool_standard_error_fmt(zhp->zpool_hdl, error,
4458 		    dgettext(TEXT_DOMAIN,
4459 		    "error setting bootenv in pool '%s'"), zhp->zpool_name);
4460 	}
4461 
4462 	return (error);
4463 }
4464 
4465 int
4466 zpool_get_bootenv(zpool_handle_t *zhp, nvlist_t **nvlp)
4467 {
4468 	nvlist_t *nvl;
4469 	int error;
4470 
4471 	nvl = NULL;
4472 	error = lzc_get_bootenv(zhp->zpool_name, &nvl);
4473 	if (error != 0) {
4474 		(void) zpool_standard_error_fmt(zhp->zpool_hdl, error,
4475 		    dgettext(TEXT_DOMAIN,
4476 		    "error getting bootenv in pool '%s'"), zhp->zpool_name);
4477 	} else {
4478 		*nvlp = nvl;
4479 	}
4480 
4481 	return (error);
4482 }
4483 
4484 /*
4485  * Read the EFI label from the config, if a label does not exist then
4486  * pass back the error to the caller. If the caller has passed a non-NULL
4487  * diskaddr argument then we set it to the starting address of the EFI
4488  * partition. If the caller has passed a non-NULL boolean argument, then
4489  * we set it to indicate if the disk does have efi system partition.
4490  */
4491 static int
4492 read_efi_label(nvlist_t *config, diskaddr_t *sb, boolean_t *system)
4493 {
4494 	char *path;
4495 	int fd;
4496 	char diskname[MAXPATHLEN];
4497 	boolean_t boot = B_FALSE;
4498 	int err = -1;
4499 	int slice;
4500 
4501 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
4502 		return (err);
4503 
4504 	(void) snprintf(diskname, sizeof (diskname), "%s%s", ZFS_RDISK_ROOT,
4505 	    strrchr(path, '/'));
4506 	if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
4507 		struct dk_gpt *vtoc;
4508 
4509 		if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
4510 			for (slice = 0; slice < vtoc->efi_nparts; slice++) {
4511 				if (vtoc->efi_parts[slice].p_tag == V_SYSTEM)
4512 					boot = B_TRUE;
4513 				if (vtoc->efi_parts[slice].p_tag == V_USR)
4514 					break;
4515 			}
4516 			if (sb != NULL && vtoc->efi_parts[slice].p_tag == V_USR)
4517 				*sb = vtoc->efi_parts[slice].p_start;
4518 			if (system != NULL)
4519 				*system = boot;
4520 			efi_free(vtoc);
4521 		}
4522 		(void) close(fd);
4523 	}
4524 	return (err);
4525 }
4526 
4527 /*
4528  * determine where a partition starts on a disk in the current
4529  * configuration
4530  */
4531 static diskaddr_t
4532 find_start_block(nvlist_t *config)
4533 {
4534 	nvlist_t **child;
4535 	uint_t c, children;
4536 	diskaddr_t sb = MAXOFFSET_T;
4537 	uint64_t wholedisk;
4538 
4539 	if (nvlist_lookup_nvlist_array(config,
4540 	    ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
4541 		if (nvlist_lookup_uint64(config,
4542 		    ZPOOL_CONFIG_WHOLE_DISK,
4543 		    &wholedisk) != 0 || !wholedisk) {
4544 			return (MAXOFFSET_T);
4545 		}
4546 		if (read_efi_label(config, &sb, NULL) < 0)
4547 			sb = MAXOFFSET_T;
4548 		return (sb);
4549 	}
4550 
4551 	for (c = 0; c < children; c++) {
4552 		sb = find_start_block(child[c]);
4553 		if (sb != MAXOFFSET_T) {
4554 			return (sb);
4555 		}
4556 	}
4557 	return (MAXOFFSET_T);
4558 }
4559 
4560 /*
4561  * Label an individual disk.  The name provided is the short name,
4562  * stripped of any leading /dev path.
4563  */
4564 int
4565 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name,
4566     zpool_boot_label_t boot_type, uint64_t boot_size, int *slice)
4567 {
4568 	char path[MAXPATHLEN];
4569 	struct dk_gpt *vtoc;
4570 	int fd;
4571 	size_t resv;
4572 	uint64_t slice_size;
4573 	diskaddr_t start_block;
4574 	char errbuf[1024];
4575 
4576 	/* prepare an error message just in case */
4577 	(void) snprintf(errbuf, sizeof (errbuf),
4578 	    dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
4579 
4580 	if (zhp) {
4581 		nvlist_t *nvroot;
4582 
4583 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
4584 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
4585 
4586 		if (zhp->zpool_start_block == 0)
4587 			start_block = find_start_block(nvroot);
4588 		else
4589 			start_block = zhp->zpool_start_block;
4590 		zhp->zpool_start_block = start_block;
4591 	} else {
4592 		/* new pool */
4593 		start_block = NEW_START_BLOCK;
4594 	}
4595 
4596 	(void) snprintf(path, sizeof (path), "%s/%s%s", ZFS_RDISK_ROOT, name,
4597 	    BACKUP_SLICE);
4598 
4599 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
4600 		/*
4601 		 * This shouldn't happen.  We've long since verified that this
4602 		 * is a valid device.
4603 		 */
4604 		zfs_error_aux(hdl,
4605 		    dgettext(TEXT_DOMAIN, "unable to open device"));
4606 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
4607 	}
4608 
4609 	if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
4610 		/*
4611 		 * The only way this can fail is if we run out of memory, or we
4612 		 * were unable to read the disk's capacity
4613 		 */
4614 		if (errno == ENOMEM)
4615 			(void) no_memory(hdl);
4616 
4617 		(void) close(fd);
4618 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4619 		    "unable to read disk capacity"), name);
4620 
4621 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
4622 	}
4623 	resv = efi_reserved_sectors(vtoc);
4624 
4625 	/*
4626 	 * Why we use V_USR: V_BACKUP confuses users, and is considered
4627 	 * disposable by some EFI utilities (since EFI doesn't have a backup
4628 	 * slice).  V_UNASSIGNED is supposed to be used only for zero size
4629 	 * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
4630 	 * etc. were all pretty specific.  V_USR is as close to reality as we
4631 	 * can get, in the absence of V_OTHER.
4632 	 */
4633 	/* first fix the partition start block */
4634 	if (start_block == MAXOFFSET_T)
4635 		start_block = NEW_START_BLOCK;
4636 
4637 	/*
4638 	 * EFI System partition is using slice 0.
4639 	 * ZFS is on slice 1 and slice 8 is reserved.
4640 	 * We assume the GPT partition table without system
4641 	 * partition has zfs p_start == NEW_START_BLOCK.
4642 	 * If start_block != NEW_START_BLOCK, it means we have
4643 	 * system partition. Correct solution would be to query/cache vtoc
4644 	 * from existing vdev member.
4645 	 */
4646 	if (boot_type == ZPOOL_CREATE_BOOT_LABEL) {
4647 		if (boot_size % vtoc->efi_lbasize != 0) {
4648 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4649 			    "boot partition size must be a multiple of %d"),
4650 			    vtoc->efi_lbasize);
4651 			(void) close(fd);
4652 			efi_free(vtoc);
4653 			return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4654 		}
4655 		/*
4656 		 * System partition size checks.
4657 		 * Note the 1MB is quite arbitrary value, since we
4658 		 * are creating dedicated pool, it should be enough
4659 		 * to hold fat + efi bootloader. May need to be
4660 		 * adjusted if the bootloader size will grow.
4661 		 */
4662 		if (boot_size < 1024 * 1024) {
4663 			char buf[64];
4664 			zfs_nicenum(boot_size, buf, sizeof (buf));
4665 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4666 			    "Specified size %s for EFI System partition is too "
4667 			    "small, the minimum size is 1MB."), buf);
4668 			(void) close(fd);
4669 			efi_free(vtoc);
4670 			return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4671 		}
4672 		/* 33MB is tested with mkfs -F pcfs */
4673 		if (hdl->libzfs_printerr &&
4674 		    ((vtoc->efi_lbasize == 512 &&
4675 		    boot_size < 33 * 1024 * 1024) ||
4676 		    (vtoc->efi_lbasize == 4096 &&
4677 		    boot_size < 256 * 1024 * 1024)))  {
4678 			char buf[64];
4679 			zfs_nicenum(boot_size, buf, sizeof (buf));
4680 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
4681 			    "Warning: EFI System partition size %s is "
4682 			    "not allowing to create FAT32 file\nsystem, which "
4683 			    "may result in unbootable system.\n"), buf);
4684 		}
4685 		/* Adjust zfs partition start by size of system partition. */
4686 		start_block += boot_size / vtoc->efi_lbasize;
4687 	}
4688 
4689 	if (start_block == NEW_START_BLOCK) {
4690 		/*
4691 		 * Use default layout.
4692 		 * ZFS is on slice 0 and slice 8 is reserved.
4693 		 */
4694 		slice_size = vtoc->efi_last_u_lba + 1;
4695 		slice_size -= resv;
4696 		slice_size -= start_block;
4697 		if (slice != NULL)
4698 			*slice = 0;
4699 
4700 		vtoc->efi_parts[0].p_start = start_block;
4701 		vtoc->efi_parts[0].p_size = slice_size;
4702 
4703 		vtoc->efi_parts[0].p_tag = V_USR;
4704 		(void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
4705 
4706 		vtoc->efi_parts[8].p_start = slice_size + start_block;
4707 		vtoc->efi_parts[8].p_size = resv;
4708 		vtoc->efi_parts[8].p_tag = V_RESERVED;
4709 	} else {
4710 		slice_size = start_block - NEW_START_BLOCK;
4711 		vtoc->efi_parts[0].p_start = NEW_START_BLOCK;
4712 		vtoc->efi_parts[0].p_size = slice_size;
4713 		vtoc->efi_parts[0].p_tag = V_SYSTEM;
4714 		(void) strcpy(vtoc->efi_parts[0].p_name, "loader");
4715 		if (slice != NULL)
4716 			*slice = 1;
4717 		/* prepare slice 1 */
4718 		slice_size = vtoc->efi_last_u_lba + 1 - slice_size;
4719 		slice_size -= resv;
4720 		slice_size -= NEW_START_BLOCK;
4721 		vtoc->efi_parts[1].p_start = start_block;
4722 		vtoc->efi_parts[1].p_size = slice_size;
4723 		vtoc->efi_parts[1].p_tag = V_USR;
4724 		(void) strcpy(vtoc->efi_parts[1].p_name, "zfs");
4725 
4726 		vtoc->efi_parts[8].p_start = slice_size + start_block;
4727 		vtoc->efi_parts[8].p_size = resv;
4728 		vtoc->efi_parts[8].p_tag = V_RESERVED;
4729 	}
4730 
4731 	if (efi_write(fd, vtoc) != 0) {
4732 		/*
4733 		 * Some block drivers (like pcata) may not support EFI
4734 		 * GPT labels.  Print out a helpful error message dir-
4735 		 * ecting the user to manually label the disk and give
4736 		 * a specific slice.
4737 		 */
4738 		(void) close(fd);
4739 		efi_free(vtoc);
4740 
4741 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4742 		    "try using fdisk(8) and then provide a specific slice"));
4743 		return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4744 	}
4745 
4746 	(void) close(fd);
4747 	efi_free(vtoc);
4748 	return (0);
4749 }
4750 
4751 static boolean_t
4752 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
4753 {
4754 	char *type;
4755 	nvlist_t **child;
4756 	uint_t children, c;
4757 
4758 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
4759 	if (strcmp(type, VDEV_TYPE_FILE) == 0 ||
4760 	    strcmp(type, VDEV_TYPE_HOLE) == 0 ||
4761 	    strcmp(type, VDEV_TYPE_MISSING) == 0) {
4762 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4763 		    "vdev type '%s' is not supported"), type);
4764 		(void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
4765 		return (B_FALSE);
4766 	}
4767 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
4768 	    &child, &children) == 0) {
4769 		for (c = 0; c < children; c++) {
4770 			if (!supported_dump_vdev_type(hdl, child[c], errbuf))
4771 				return (B_FALSE);
4772 		}
4773 	}
4774 	return (B_TRUE);
4775 }
4776 
4777 /*
4778  * Check if this zvol is allowable for use as a dump device; zero if
4779  * it is, > 0 if it isn't, < 0 if it isn't a zvol.
4780  *
4781  * Allowable storage configurations include mirrors, all raidz variants, and
4782  * pools with log, cache, and spare devices.  Pools which are backed by files or
4783  * have missing/hole vdevs are not suitable.
4784  */
4785 int
4786 zvol_check_dump_config(char *arg)
4787 {
4788 	zpool_handle_t *zhp = NULL;
4789 	nvlist_t *config, *nvroot;
4790 	char *p, *volname;
4791 	nvlist_t **top;
4792 	uint_t toplevels;
4793 	libzfs_handle_t *hdl;
4794 	char errbuf[1024];
4795 	char poolname[ZFS_MAX_DATASET_NAME_LEN];
4796 	int pathlen = strlen(ZVOL_FULL_DEV_DIR);
4797 	int ret = 1;
4798 
4799 	if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
4800 		return (-1);
4801 	}
4802 
4803 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4804 	    "dump is not supported on device '%s'"), arg);
4805 
4806 	if ((hdl = libzfs_init()) == NULL)
4807 		return (1);
4808 	libzfs_print_on_error(hdl, B_TRUE);
4809 
4810 	volname = arg + pathlen;
4811 
4812 	/* check the configuration of the pool */
4813 	if ((p = strchr(volname, '/')) == NULL) {
4814 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4815 		    "malformed dataset name"));
4816 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4817 		return (1);
4818 	} else if (p - volname >= ZFS_MAX_DATASET_NAME_LEN) {
4819 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4820 		    "dataset name is too long"));
4821 		(void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
4822 		return (1);
4823 	} else {
4824 		(void) strncpy(poolname, volname, p - volname);
4825 		poolname[p - volname] = '\0';
4826 	}
4827 
4828 	if ((zhp = zpool_open(hdl, poolname)) == NULL) {
4829 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4830 		    "could not open pool '%s'"), poolname);
4831 		(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
4832 		goto out;
4833 	}
4834 	config = zpool_get_config(zhp, NULL);
4835 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4836 	    &nvroot) != 0) {
4837 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4838 		    "could not obtain vdev configuration for  '%s'"), poolname);
4839 		(void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
4840 		goto out;
4841 	}
4842 
4843 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4844 	    &top, &toplevels) == 0);
4845 
4846 	if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
4847 		goto out;
4848 	}
4849 	ret = 0;
4850 
4851 out:
4852 	if (zhp)
4853 		zpool_close(zhp);
4854 	libzfs_fini(hdl);
4855 	return (ret);
4856 }
4857