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