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