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