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