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