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