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