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