1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51485Slling  * Common Development and Distribution License (the "License").
61485Slling  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
212082Seschrock 
22789Sahrens /*
236289Smmusante  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24789Sahrens  * Use is subject to license terms.
25789Sahrens  */
26789Sahrens 
27789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
28789Sahrens 
293126Sahl #include <alloca.h>
30789Sahrens #include <assert.h>
31789Sahrens #include <ctype.h>
32789Sahrens #include <errno.h>
33789Sahrens #include <devid.h>
343126Sahl #include <dirent.h>
35789Sahrens #include <fcntl.h>
36789Sahrens #include <libintl.h>
37789Sahrens #include <stdio.h>
38789Sahrens #include <stdlib.h>
393126Sahl #include <strings.h>
40789Sahrens #include <unistd.h>
417184Stimh #include <zone.h>
424276Staylor #include <sys/efi_partition.h>
434276Staylor #include <sys/vtoc.h>
44789Sahrens #include <sys/zfs_ioctl.h>
451544Seschrock #include <sys/zio.h>
462926Sek110237 #include <strings.h>
47789Sahrens 
48789Sahrens #include "zfs_namecheck.h"
493912Slling #include "zfs_prop.h"
50789Sahrens #include "libzfs_impl.h"
51789Sahrens 
527042Sgw25295 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
535094Slling 
545094Slling /*
555094Slling  * ====================================================================
565094Slling  *   zpool property functions
575094Slling  * ====================================================================
585094Slling  */
595094Slling 
605094Slling static int
615094Slling zpool_get_all_props(zpool_handle_t *zhp)
625094Slling {
635094Slling 	zfs_cmd_t zc = { 0 };
645094Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
655094Slling 
665094Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
675094Slling 
685094Slling 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
695094Slling 		return (-1);
705094Slling 
715094Slling 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
725094Slling 		if (errno == ENOMEM) {
735094Slling 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
745094Slling 				zcmd_free_nvlists(&zc);
755094Slling 				return (-1);
765094Slling 			}
775094Slling 		} else {
785094Slling 			zcmd_free_nvlists(&zc);
795094Slling 			return (-1);
805094Slling 		}
815094Slling 	}
825094Slling 
835094Slling 	if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
845094Slling 		zcmd_free_nvlists(&zc);
855094Slling 		return (-1);
865094Slling 	}
875094Slling 
885094Slling 	zcmd_free_nvlists(&zc);
895094Slling 
905094Slling 	return (0);
915094Slling }
925094Slling 
935094Slling static int
945094Slling zpool_props_refresh(zpool_handle_t *zhp)
955094Slling {
965094Slling 	nvlist_t *old_props;
975094Slling 
985094Slling 	old_props = zhp->zpool_props;
995094Slling 
1005094Slling 	if (zpool_get_all_props(zhp) != 0)
1015094Slling 		return (-1);
1025094Slling 
1035094Slling 	nvlist_free(old_props);
1045094Slling 	return (0);
1055094Slling }
1065094Slling 
1075094Slling static char *
1085094Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
1095094Slling     zprop_source_t *src)
1105094Slling {
1115094Slling 	nvlist_t *nv, *nvl;
1125094Slling 	uint64_t ival;
1135094Slling 	char *value;
1145094Slling 	zprop_source_t source;
1155094Slling 
1165094Slling 	nvl = zhp->zpool_props;
1175094Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
1185094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
1195094Slling 		source = ival;
1205094Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1215094Slling 	} else {
1225094Slling 		source = ZPROP_SRC_DEFAULT;
1235094Slling 		if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
1245094Slling 			value = "-";
1255094Slling 	}
1265094Slling 
1275094Slling 	if (src)
1285094Slling 		*src = source;
1295094Slling 
1305094Slling 	return (value);
1315094Slling }
1325094Slling 
1335094Slling uint64_t
1345094Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
1355094Slling {
1365094Slling 	nvlist_t *nv, *nvl;
1375094Slling 	uint64_t value;
1385094Slling 	zprop_source_t source;
1395094Slling 
140*7294Sperrin 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
141*7294Sperrin 		/*
142*7294Sperrin 		 * zpool_get_all_props() has most likely failed because
143*7294Sperrin 		 * the pool is faulted, but if all we need is the top level
144*7294Sperrin 		 * vdev's guid then get it from the zhp config nvlist.
145*7294Sperrin 		 */
146*7294Sperrin 		if ((prop == ZPOOL_PROP_GUID) &&
147*7294Sperrin 		    (nvlist_lookup_nvlist(zhp->zpool_config,
148*7294Sperrin 		    ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
149*7294Sperrin 		    (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
150*7294Sperrin 		    == 0)) {
151*7294Sperrin 			return (value);
152*7294Sperrin 		}
1535094Slling 		return (zpool_prop_default_numeric(prop));
154*7294Sperrin 	}
1555094Slling 
1565094Slling 	nvl = zhp->zpool_props;
1575094Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
1585094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
1595094Slling 		source = value;
1605094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1615094Slling 	} else {
1625094Slling 		source = ZPROP_SRC_DEFAULT;
1635094Slling 		value = zpool_prop_default_numeric(prop);
1645094Slling 	}
1655094Slling 
1665094Slling 	if (src)
1675094Slling 		*src = source;
1685094Slling 
1695094Slling 	return (value);
1705094Slling }
1715094Slling 
1725094Slling /*
1735094Slling  * Map VDEV STATE to printed strings.
1745094Slling  */
1755094Slling char *
1765094Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
1775094Slling {
1785094Slling 	switch (state) {
1795094Slling 	case VDEV_STATE_CLOSED:
1805094Slling 	case VDEV_STATE_OFFLINE:
1815094Slling 		return (gettext("OFFLINE"));
1825094Slling 	case VDEV_STATE_REMOVED:
1835094Slling 		return (gettext("REMOVED"));
1845094Slling 	case VDEV_STATE_CANT_OPEN:
185*7294Sperrin 		if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
1865094Slling 			return (gettext("FAULTED"));
1875094Slling 		else
1885094Slling 			return (gettext("UNAVAIL"));
1895094Slling 	case VDEV_STATE_FAULTED:
1905094Slling 		return (gettext("FAULTED"));
1915094Slling 	case VDEV_STATE_DEGRADED:
1925094Slling 		return (gettext("DEGRADED"));
1935094Slling 	case VDEV_STATE_HEALTHY:
1945094Slling 		return (gettext("ONLINE"));
1955094Slling 	}
1965094Slling 
1975094Slling 	return (gettext("UNKNOWN"));
1985094Slling }
1995094Slling 
2005094Slling /*
2015094Slling  * Get a zpool property value for 'prop' and return the value in
2025094Slling  * a pre-allocated buffer.
2035094Slling  */
2045094Slling int
2055094Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
2065094Slling     zprop_source_t *srctype)
2075094Slling {
2085094Slling 	uint64_t intval;
2095094Slling 	const char *strval;
2105094Slling 	zprop_source_t src = ZPROP_SRC_NONE;
2115094Slling 	nvlist_t *nvroot;
2125094Slling 	vdev_stat_t *vs;
2135094Slling 	uint_t vsc;
2145094Slling 
2155094Slling 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
2165094Slling 		if (prop == ZPOOL_PROP_NAME)
2175094Slling 			(void) strlcpy(buf, zpool_get_name(zhp), len);
2185094Slling 		else if (prop == ZPOOL_PROP_HEALTH)
2195094Slling 			(void) strlcpy(buf, "FAULTED", len);
2205094Slling 		else
2215094Slling 			(void) strlcpy(buf, "-", len);
2225094Slling 		return (0);
2235094Slling 	}
2245094Slling 
2255094Slling 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
2265094Slling 	    prop != ZPOOL_PROP_NAME)
2275094Slling 		return (-1);
2285094Slling 
2295094Slling 	switch (zpool_prop_get_type(prop)) {
2305094Slling 	case PROP_TYPE_STRING:
2315094Slling 		(void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
2325094Slling 		    len);
2335094Slling 		break;
2345094Slling 
2355094Slling 	case PROP_TYPE_NUMBER:
2365094Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
2375094Slling 
2385094Slling 		switch (prop) {
2395094Slling 		case ZPOOL_PROP_SIZE:
2405094Slling 		case ZPOOL_PROP_USED:
2415094Slling 		case ZPOOL_PROP_AVAILABLE:
2425094Slling 			(void) zfs_nicenum(intval, buf, len);
2435094Slling 			break;
2445094Slling 
2455094Slling 		case ZPOOL_PROP_CAPACITY:
2465094Slling 			(void) snprintf(buf, len, "%llu%%",
2475094Slling 			    (u_longlong_t)intval);
2485094Slling 			break;
2495094Slling 
2505094Slling 		case ZPOOL_PROP_HEALTH:
2515094Slling 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2525094Slling 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2535094Slling 			verify(nvlist_lookup_uint64_array(nvroot,
2545094Slling 			    ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
2555094Slling 
2565094Slling 			(void) strlcpy(buf, zpool_state_to_name(intval,
2575094Slling 			    vs->vs_aux), len);
2585094Slling 			break;
2595094Slling 		default:
2605094Slling 			(void) snprintf(buf, len, "%llu", intval);
2615094Slling 		}
2625094Slling 		break;
2635094Slling 
2645094Slling 	case PROP_TYPE_INDEX:
2655094Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
2665094Slling 		if (zpool_prop_index_to_string(prop, intval, &strval)
2675094Slling 		    != 0)
2685094Slling 			return (-1);
2695094Slling 		(void) strlcpy(buf, strval, len);
2705094Slling 		break;
2715094Slling 
2725094Slling 	default:
2735094Slling 		abort();
2745094Slling 	}
2755094Slling 
2765094Slling 	if (srctype)
2775094Slling 		*srctype = src;
2785094Slling 
2795094Slling 	return (0);
2805094Slling }
2815094Slling 
2825094Slling /*
2835094Slling  * Check if the bootfs name has the same pool name as it is set to.
2845094Slling  * Assuming bootfs is a valid dataset name.
2855094Slling  */
2865094Slling static boolean_t
2875094Slling bootfs_name_valid(const char *pool, char *bootfs)
2885094Slling {
2895094Slling 	int len = strlen(pool);
2905094Slling 
2915094Slling 	if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM))
2925094Slling 		return (B_FALSE);
2935094Slling 
2945094Slling 	if (strncmp(pool, bootfs, len) == 0 &&
2955094Slling 	    (bootfs[len] == '/' || bootfs[len] == '\0'))
2965094Slling 		return (B_TRUE);
2975094Slling 
2985094Slling 	return (B_FALSE);
2995094Slling }
3005094Slling 
3015094Slling /*
3027042Sgw25295  * Inspect the configuration to determine if any of the devices contain
3037042Sgw25295  * an EFI label.
3047042Sgw25295  */
3057042Sgw25295 static boolean_t
3067042Sgw25295 pool_uses_efi(nvlist_t *config)
3077042Sgw25295 {
3087042Sgw25295 	nvlist_t **child;
3097042Sgw25295 	uint_t c, children;
3107042Sgw25295 
3117042Sgw25295 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
3127042Sgw25295 	    &child, &children) != 0)
3137042Sgw25295 		return (read_efi_label(config, NULL) >= 0);
3147042Sgw25295 
3157042Sgw25295 	for (c = 0; c < children; c++) {
3167042Sgw25295 		if (pool_uses_efi(child[c]))
3177042Sgw25295 			return (B_TRUE);
3187042Sgw25295 	}
3197042Sgw25295 	return (B_FALSE);
3207042Sgw25295 }
3217042Sgw25295 
3227042Sgw25295 /*
3235094Slling  * Given an nvlist of zpool properties to be set, validate that they are
3245094Slling  * correct, and parse any numeric properties (index, boolean, etc) if they are
3255094Slling  * specified as strings.
3265094Slling  */
3275094Slling static nvlist_t *
3287184Stimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
3295094Slling     nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf)
3305094Slling {
3315094Slling 	nvpair_t *elem;
3325094Slling 	nvlist_t *retprops;
3335094Slling 	zpool_prop_t prop;
3345094Slling 	char *strval;
3355094Slling 	uint64_t intval;
3365363Seschrock 	char *slash;
3375363Seschrock 	struct stat64 statbuf;
3387042Sgw25295 	zpool_handle_t *zhp;
3397042Sgw25295 	nvlist_t *nvroot;
3405094Slling 
3415094Slling 	if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
3425094Slling 		(void) no_memory(hdl);
3435094Slling 		return (NULL);
3445094Slling 	}
3455094Slling 
3465094Slling 	elem = NULL;
3475094Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
3485094Slling 		const char *propname = nvpair_name(elem);
3495094Slling 
3505094Slling 		/*
3515094Slling 		 * Make sure this property is valid and applies to this type.
3525094Slling 		 */
3535094Slling 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
3545094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3555094Slling 			    "invalid property '%s'"), propname);
3565094Slling 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
3575094Slling 			goto error;
3585094Slling 		}
3595094Slling 
3605094Slling 		if (zpool_prop_readonly(prop)) {
3615094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
3625094Slling 			    "is readonly"), propname);
3635094Slling 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
3645094Slling 			goto error;
3655094Slling 		}
3665094Slling 
3675094Slling 		if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
3685094Slling 		    &strval, &intval, errbuf) != 0)
3695094Slling 			goto error;
3705094Slling 
3715094Slling 		/*
3725094Slling 		 * Perform additional checking for specific properties.
3735094Slling 		 */
3745094Slling 		switch (prop) {
3755094Slling 		case ZPOOL_PROP_VERSION:
3765094Slling 			if (intval < version || intval > SPA_VERSION) {
3775094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3785094Slling 				    "property '%s' number %d is invalid."),
3795094Slling 				    propname, intval);
3805094Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
3815094Slling 				goto error;
3825094Slling 			}
3835094Slling 			break;
3845094Slling 
3855094Slling 		case ZPOOL_PROP_BOOTFS:
3865094Slling 			if (create_or_import) {
3875094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3885094Slling 				    "property '%s' cannot be set at creation "
3895094Slling 				    "or import time"), propname);
3905094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
3915094Slling 				goto error;
3925094Slling 			}
3935094Slling 
3945094Slling 			if (version < SPA_VERSION_BOOTFS) {
3955094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3965094Slling 				    "pool must be upgraded to support "
3975094Slling 				    "'%s' property"), propname);
3985094Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
3995094Slling 				goto error;
4005094Slling 			}
4015094Slling 
4025094Slling 			/*
4035094Slling 			 * bootfs property value has to be a dataset name and
4045094Slling 			 * the dataset has to be in the same pool as it sets to.
4055094Slling 			 */
4065094Slling 			if (strval[0] != '\0' && !bootfs_name_valid(poolname,
4075094Slling 			    strval)) {
4085094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
4095094Slling 				    "is an invalid name"), strval);
4105094Slling 				(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4115094Slling 				goto error;
4125094Slling 			}
4137042Sgw25295 
4147042Sgw25295 			if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
4157042Sgw25295 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4167042Sgw25295 				    "could not open pool '%s'"), poolname);
4177042Sgw25295 				(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
4187042Sgw25295 				goto error;
4197042Sgw25295 			}
4207042Sgw25295 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
4217042Sgw25295 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
4227042Sgw25295 
4237042Sgw25295 			/*
4247042Sgw25295 			 * bootfs property cannot be set on a disk which has
4257042Sgw25295 			 * been EFI labeled.
4267042Sgw25295 			 */
4277042Sgw25295 			if (pool_uses_efi(nvroot)) {
4287042Sgw25295 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4297042Sgw25295 				    "property '%s' not supported on "
4307042Sgw25295 				    "EFI labeled devices"), propname);
4317042Sgw25295 				(void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
4327042Sgw25295 				zpool_close(zhp);
4337042Sgw25295 				goto error;
4347042Sgw25295 			}
4357042Sgw25295 			zpool_close(zhp);
4365094Slling 			break;
4375094Slling 
4385094Slling 		case ZPOOL_PROP_ALTROOT:
4395094Slling 			if (!create_or_import) {
4405094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4415094Slling 				    "property '%s' can only be set during pool "
4425094Slling 				    "creation or import"), propname);
4435094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
4445094Slling 				goto error;
4455094Slling 			}
4465094Slling 
4475094Slling 			if (strval[0] != '/') {
4485094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4495094Slling 				    "bad alternate root '%s'"), strval);
4505094Slling 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
4515094Slling 				goto error;
4525094Slling 			}
4535094Slling 			break;
4545363Seschrock 
4555363Seschrock 		case ZPOOL_PROP_CACHEFILE:
4565363Seschrock 			if (strval[0] == '\0')
4575363Seschrock 				break;
4585363Seschrock 
4595363Seschrock 			if (strcmp(strval, "none") == 0)
4605363Seschrock 				break;
4615363Seschrock 
4625363Seschrock 			if (strval[0] != '/') {
4635363Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4645363Seschrock 				    "property '%s' must be empty, an "
4655363Seschrock 				    "absolute path, or 'none'"), propname);
4665363Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
4675363Seschrock 				goto error;
4685363Seschrock 			}
4695363Seschrock 
4705363Seschrock 			slash = strrchr(strval, '/');
4715363Seschrock 
4725363Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
4735363Seschrock 			    strcmp(slash, "/..") == 0) {
4745363Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4755363Seschrock 				    "'%s' is not a valid file"), strval);
4765363Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
4775363Seschrock 				goto error;
4785363Seschrock 			}
4795363Seschrock 
4805363Seschrock 			*slash = '\0';
4815363Seschrock 
4825621Seschrock 			if (strval[0] != '\0' &&
4835621Seschrock 			    (stat64(strval, &statbuf) != 0 ||
4845621Seschrock 			    !S_ISDIR(statbuf.st_mode))) {
4855363Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4865363Seschrock 				    "'%s' is not a valid directory"),
4875363Seschrock 				    strval);
4885363Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
4895363Seschrock 				goto error;
4905363Seschrock 			}
4915363Seschrock 
4925363Seschrock 			*slash = '/';
4935363Seschrock 			break;
4945094Slling 		}
4955094Slling 	}
4965094Slling 
4975094Slling 	return (retprops);
4985094Slling error:
4995094Slling 	nvlist_free(retprops);
5005094Slling 	return (NULL);
5015094Slling }
5025094Slling 
5035094Slling /*
5045094Slling  * Set zpool property : propname=propval.
5055094Slling  */
5065094Slling int
5075094Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
5085094Slling {
5095094Slling 	zfs_cmd_t zc = { 0 };
5105094Slling 	int ret = -1;
5115094Slling 	char errbuf[1024];
5125094Slling 	nvlist_t *nvl = NULL;
5135094Slling 	nvlist_t *realprops;
5145094Slling 	uint64_t version;
5155094Slling 
5165094Slling 	(void) snprintf(errbuf, sizeof (errbuf),
5175094Slling 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
5185094Slling 	    zhp->zpool_name);
5195094Slling 
5205094Slling 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp))
5215094Slling 		return (zfs_error(zhp->zpool_hdl, EZFS_POOLPROPS, errbuf));
5225094Slling 
5235094Slling 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5245094Slling 		return (no_memory(zhp->zpool_hdl));
5255094Slling 
5265094Slling 	if (nvlist_add_string(nvl, propname, propval) != 0) {
5275094Slling 		nvlist_free(nvl);
5285094Slling 		return (no_memory(zhp->zpool_hdl));
5295094Slling 	}
5305094Slling 
5315094Slling 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
5327184Stimh 	if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
5335094Slling 	    zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) {
5345094Slling 		nvlist_free(nvl);
5355094Slling 		return (-1);
5365094Slling 	}
5375094Slling 
5385094Slling 	nvlist_free(nvl);
5395094Slling 	nvl = realprops;
5405094Slling 
5415094Slling 	/*
5425094Slling 	 * Execute the corresponding ioctl() to set this property.
5435094Slling 	 */
5445094Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
5455094Slling 
5465094Slling 	if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
5475094Slling 		nvlist_free(nvl);
5485094Slling 		return (-1);
5495094Slling 	}
5505094Slling 
5515094Slling 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
5525094Slling 
5535094Slling 	zcmd_free_nvlists(&zc);
5545094Slling 	nvlist_free(nvl);
5555094Slling 
5565094Slling 	if (ret)
5575094Slling 		(void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
5585094Slling 	else
5595094Slling 		(void) zpool_props_refresh(zhp);
5605094Slling 
5615094Slling 	return (ret);
5625094Slling }
5635094Slling 
5645094Slling int
5655094Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
5665094Slling {
5675094Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
5685094Slling 	zprop_list_t *entry;
5695094Slling 	char buf[ZFS_MAXPROPLEN];
5705094Slling 
5715094Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
5725094Slling 		return (-1);
5735094Slling 
5745094Slling 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
5755094Slling 
5765094Slling 		if (entry->pl_fixed)
5775094Slling 			continue;
5785094Slling 
5795094Slling 		if (entry->pl_prop != ZPROP_INVAL &&
5805094Slling 		    zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
5815094Slling 		    NULL) == 0) {
5825094Slling 			if (strlen(buf) > entry->pl_width)
5835094Slling 				entry->pl_width = strlen(buf);
5845094Slling 		}
5855094Slling 	}
5865094Slling 
5875094Slling 	return (0);
5885094Slling }
5895094Slling 
5905094Slling 
591789Sahrens /*
592789Sahrens  * Validate the given pool name, optionally putting an extended error message in
593789Sahrens  * 'buf'.
594789Sahrens  */
5956423Sgw25295 boolean_t
5962082Seschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
597789Sahrens {
598789Sahrens 	namecheck_err_t why;
599789Sahrens 	char what;
6001773Seschrock 	int ret;
601789Sahrens 
6021773Seschrock 	ret = pool_namecheck(pool, &why, &what);
6031773Seschrock 
6041773Seschrock 	/*
6051773Seschrock 	 * The rules for reserved pool names were extended at a later point.
6061773Seschrock 	 * But we need to support users with existing pools that may now be
6071773Seschrock 	 * invalid.  So we only check for this expanded set of names during a
6081773Seschrock 	 * create (or import), and only in userland.
6091773Seschrock 	 */
6101773Seschrock 	if (ret == 0 && !isopen &&
6111773Seschrock 	    (strncmp(pool, "mirror", 6) == 0 ||
6121773Seschrock 	    strncmp(pool, "raidz", 5) == 0 ||
6134527Sperrin 	    strncmp(pool, "spare", 5) == 0 ||
6144527Sperrin 	    strcmp(pool, "log") == 0)) {
6156423Sgw25295 		if (hdl != NULL)
6166423Sgw25295 			zfs_error_aux(hdl,
6176423Sgw25295 			    dgettext(TEXT_DOMAIN, "name is reserved"));
6182082Seschrock 		return (B_FALSE);
6191773Seschrock 	}
6201773Seschrock 
6211773Seschrock 
6221773Seschrock 	if (ret != 0) {
6232082Seschrock 		if (hdl != NULL) {
624789Sahrens 			switch (why) {
6251003Slling 			case NAME_ERR_TOOLONG:
6262082Seschrock 				zfs_error_aux(hdl,
6271003Slling 				    dgettext(TEXT_DOMAIN, "name is too long"));
6281003Slling 				break;
6291003Slling 
630789Sahrens 			case NAME_ERR_INVALCHAR:
6312082Seschrock 				zfs_error_aux(hdl,
632789Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
633789Sahrens 				    "'%c' in pool name"), what);
634789Sahrens 				break;
635789Sahrens 
636789Sahrens 			case NAME_ERR_NOLETTER:
6372082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6382082Seschrock 				    "name must begin with a letter"));
639789Sahrens 				break;
640789Sahrens 
641789Sahrens 			case NAME_ERR_RESERVED:
6422082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6432082Seschrock 				    "name is reserved"));
644789Sahrens 				break;
645789Sahrens 
646789Sahrens 			case NAME_ERR_DISKLIKE:
6472082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6482082Seschrock 				    "pool name is reserved"));
649789Sahrens 				break;
6502856Snd150628 
6512856Snd150628 			case NAME_ERR_LEADING_SLASH:
6522856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6532856Snd150628 				    "leading slash in name"));
6542856Snd150628 				break;
6552856Snd150628 
6562856Snd150628 			case NAME_ERR_EMPTY_COMPONENT:
6572856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6582856Snd150628 				    "empty component in name"));
6592856Snd150628 				break;
6602856Snd150628 
6612856Snd150628 			case NAME_ERR_TRAILING_SLASH:
6622856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6632856Snd150628 				    "trailing slash in name"));
6642856Snd150628 				break;
6652856Snd150628 
6662856Snd150628 			case NAME_ERR_MULTIPLE_AT:
6672856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6682856Snd150628 				    "multiple '@' delimiters in name"));
6692856Snd150628 				break;
6702856Snd150628 
671789Sahrens 			}
672789Sahrens 		}
6732082Seschrock 		return (B_FALSE);
674789Sahrens 	}
675789Sahrens 
6762082Seschrock 	return (B_TRUE);
677789Sahrens }
678789Sahrens 
679789Sahrens /*
680789Sahrens  * Open a handle to the given pool, even if the pool is currently in the FAULTED
681789Sahrens  * state.
682789Sahrens  */
683789Sahrens zpool_handle_t *
6842082Seschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
685789Sahrens {
686789Sahrens 	zpool_handle_t *zhp;
6872142Seschrock 	boolean_t missing;
688789Sahrens 
689789Sahrens 	/*
690789Sahrens 	 * Make sure the pool name is valid.
691789Sahrens 	 */
6922082Seschrock 	if (!zpool_name_valid(hdl, B_TRUE, pool)) {
6933237Slling 		(void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
6942082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
6952082Seschrock 		    pool);
696789Sahrens 		return (NULL);
697789Sahrens 	}
698789Sahrens 
6992082Seschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
7002082Seschrock 		return (NULL);
701789Sahrens 
7022082Seschrock 	zhp->zpool_hdl = hdl;
703789Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
704789Sahrens 
7052142Seschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
7062142Seschrock 		zpool_close(zhp);
7072142Seschrock 		return (NULL);
7082142Seschrock 	}
7092142Seschrock 
7102142Seschrock 	if (missing) {
7115094Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
7123237Slling 		(void) zfs_error_fmt(hdl, EZFS_NOENT,
7135094Slling 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
7142142Seschrock 		zpool_close(zhp);
7152142Seschrock 		return (NULL);
716789Sahrens 	}
717789Sahrens 
718789Sahrens 	return (zhp);
719789Sahrens }
720789Sahrens 
721789Sahrens /*
722789Sahrens  * Like the above, but silent on error.  Used when iterating over pools (because
723789Sahrens  * the configuration cache may be out of date).
724789Sahrens  */
7252142Seschrock int
7262142Seschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
727789Sahrens {
728789Sahrens 	zpool_handle_t *zhp;
7292142Seschrock 	boolean_t missing;
730789Sahrens 
7312142Seschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
7322142Seschrock 		return (-1);
733789Sahrens 
7342082Seschrock 	zhp->zpool_hdl = hdl;
735789Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
736789Sahrens 
7372142Seschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
7382142Seschrock 		zpool_close(zhp);
7392142Seschrock 		return (-1);
740789Sahrens 	}
741789Sahrens 
7422142Seschrock 	if (missing) {
7432142Seschrock 		zpool_close(zhp);
7442142Seschrock 		*ret = NULL;
7452142Seschrock 		return (0);
7462142Seschrock 	}
7472142Seschrock 
7482142Seschrock 	*ret = zhp;
7492142Seschrock 	return (0);
750789Sahrens }
751789Sahrens 
752789Sahrens /*
753789Sahrens  * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
754789Sahrens  * state.
755789Sahrens  */
756789Sahrens zpool_handle_t *
7572082Seschrock zpool_open(libzfs_handle_t *hdl, const char *pool)
758789Sahrens {
759789Sahrens 	zpool_handle_t *zhp;
760789Sahrens 
7612082Seschrock 	if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
762789Sahrens 		return (NULL);
763789Sahrens 
764789Sahrens 	if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
7653237Slling 		(void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
7662082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
767789Sahrens 		zpool_close(zhp);
768789Sahrens 		return (NULL);
769789Sahrens 	}
770789Sahrens 
771789Sahrens 	return (zhp);
772789Sahrens }
773789Sahrens 
774789Sahrens /*
775789Sahrens  * Close the handle.  Simply frees the memory associated with the handle.
776789Sahrens  */
777789Sahrens void
778789Sahrens zpool_close(zpool_handle_t *zhp)
779789Sahrens {
780789Sahrens 	if (zhp->zpool_config)
781789Sahrens 		nvlist_free(zhp->zpool_config);
782952Seschrock 	if (zhp->zpool_old_config)
783952Seschrock 		nvlist_free(zhp->zpool_old_config);
7843912Slling 	if (zhp->zpool_props)
7853912Slling 		nvlist_free(zhp->zpool_props);
786789Sahrens 	free(zhp);
787789Sahrens }
788789Sahrens 
789789Sahrens /*
790789Sahrens  * Return the name of the pool.
791789Sahrens  */
792789Sahrens const char *
793789Sahrens zpool_get_name(zpool_handle_t *zhp)
794789Sahrens {
795789Sahrens 	return (zhp->zpool_name);
796789Sahrens }
797789Sahrens 
798789Sahrens 
799789Sahrens /*
800789Sahrens  * Return the state of the pool (ACTIVE or UNAVAILABLE)
801789Sahrens  */
802789Sahrens int
803789Sahrens zpool_get_state(zpool_handle_t *zhp)
804789Sahrens {
805789Sahrens 	return (zhp->zpool_state);
806789Sahrens }
807789Sahrens 
808789Sahrens /*
809789Sahrens  * Create the named pool, using the provided vdev list.  It is assumed
810789Sahrens  * that the consumer has already validated the contents of the nvlist, so we
811789Sahrens  * don't have to worry about error semantics.
812789Sahrens  */
813789Sahrens int
8142082Seschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
8157184Stimh     nvlist_t *props, nvlist_t *fsprops)
816789Sahrens {
817789Sahrens 	zfs_cmd_t zc = { 0 };
8187184Stimh 	nvlist_t *zc_fsprops = NULL;
8197184Stimh 	nvlist_t *zc_props = NULL;
8202082Seschrock 	char msg[1024];
8215094Slling 	char *altroot;
8227184Stimh 	int ret = -1;
8232082Seschrock 
8242082Seschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
8252082Seschrock 	    "cannot create '%s'"), pool);
826789Sahrens 
8272082Seschrock 	if (!zpool_name_valid(hdl, B_FALSE, pool))
8282082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
8292082Seschrock 
8305320Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
8315320Slling 		return (-1);
8325320Slling 
8337184Stimh 	if (props) {
8347184Stimh 		if ((zc_props = zpool_valid_proplist(hdl, pool, props,
8357184Stimh 		    SPA_VERSION_1, B_TRUE, msg)) == NULL) {
8367184Stimh 			goto create_failed;
8377184Stimh 		}
8385320Slling 	}
839789Sahrens 
8407184Stimh 	if (fsprops) {
8417184Stimh 		uint64_t zoned;
8427184Stimh 		char *zonestr;
8437184Stimh 
8447184Stimh 		zoned = ((nvlist_lookup_string(fsprops,
8457184Stimh 		    zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
8467184Stimh 		    strcmp(zonestr, "on") == 0);
8477184Stimh 
8487184Stimh 		if ((zc_fsprops = zfs_valid_proplist(hdl,
8497184Stimh 		    ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
8507184Stimh 			goto create_failed;
8517184Stimh 		}
8527184Stimh 		if (!zc_props &&
8537184Stimh 		    (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
8547184Stimh 			goto create_failed;
8557184Stimh 		}
8567184Stimh 		if (nvlist_add_nvlist(zc_props,
8577184Stimh 		    ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
8587184Stimh 			goto create_failed;
8597184Stimh 		}
8607184Stimh 	}
8617184Stimh 
8627184Stimh 	if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
8637184Stimh 		goto create_failed;
8647184Stimh 
865789Sahrens 	(void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
866789Sahrens 
8677184Stimh 	if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
8685320Slling 
8692676Seschrock 		zcmd_free_nvlists(&zc);
8707184Stimh 		nvlist_free(zc_props);
8717184Stimh 		nvlist_free(zc_fsprops);
8722082Seschrock 
873789Sahrens 		switch (errno) {
874789Sahrens 		case EBUSY:
875789Sahrens 			/*
876789Sahrens 			 * This can happen if the user has specified the same
877789Sahrens 			 * device multiple times.  We can't reliably detect this
878789Sahrens 			 * until we try to add it and see we already have a
879789Sahrens 			 * label.
880789Sahrens 			 */
8812082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8822082Seschrock 			    "one or more vdevs refer to the same device"));
8832082Seschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
884789Sahrens 
885789Sahrens 		case EOVERFLOW:
886789Sahrens 			/*
8872082Seschrock 			 * This occurs when one of the devices is below
888789Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
889789Sahrens 			 * device was the problem device since there's no
890789Sahrens 			 * reliable way to determine device size from userland.
891789Sahrens 			 */
892789Sahrens 			{
893789Sahrens 				char buf[64];
894789Sahrens 
895789Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
896789Sahrens 
8972082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8982082Seschrock 				    "one or more devices is less than the "
8992082Seschrock 				    "minimum size (%s)"), buf);
900789Sahrens 			}
9012082Seschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
902789Sahrens 
903789Sahrens 		case ENOSPC:
9042082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9052082Seschrock 			    "one or more devices is out of space"));
9062082Seschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
907789Sahrens 
9085450Sbrendan 		case ENOTBLK:
9095450Sbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9105450Sbrendan 			    "cache device must be a disk or disk slice"));
9115450Sbrendan 			return (zfs_error(hdl, EZFS_BADDEV, msg));
9125450Sbrendan 
913789Sahrens 		default:
9142082Seschrock 			return (zpool_standard_error(hdl, errno, msg));
915789Sahrens 		}
916789Sahrens 	}
917789Sahrens 
918789Sahrens 	/*
919789Sahrens 	 * If this is an alternate root pool, then we automatically set the
9202676Seschrock 	 * mountpoint of the root dataset to be '/'.
921789Sahrens 	 */
9225094Slling 	if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
9235094Slling 	    &altroot) == 0) {
924789Sahrens 		zfs_handle_t *zhp;
925789Sahrens 
9265094Slling 		verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
9272676Seschrock 		verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
9282676Seschrock 		    "/") == 0);
929789Sahrens 
930789Sahrens 		zfs_close(zhp);
931789Sahrens 	}
932789Sahrens 
9337184Stimh create_failed:
9345320Slling 	zcmd_free_nvlists(&zc);
9357184Stimh 	nvlist_free(zc_props);
9367184Stimh 	nvlist_free(zc_fsprops);
9377184Stimh 	return (ret);
938789Sahrens }
939789Sahrens 
940789Sahrens /*
941789Sahrens  * Destroy the given pool.  It is up to the caller to ensure that there are no
942789Sahrens  * datasets left in the pool.
943789Sahrens  */
944789Sahrens int
945789Sahrens zpool_destroy(zpool_handle_t *zhp)
946789Sahrens {
947789Sahrens 	zfs_cmd_t zc = { 0 };
948789Sahrens 	zfs_handle_t *zfp = NULL;
9492082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
9502082Seschrock 	char msg[1024];
951789Sahrens 
952789Sahrens 	if (zhp->zpool_state == POOL_STATE_ACTIVE &&
9532082Seschrock 	    (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name,
9542082Seschrock 	    ZFS_TYPE_FILESYSTEM)) == NULL)
955789Sahrens 		return (-1);
956789Sahrens 
9572856Snd150628 	if (zpool_remove_zvol_links(zhp) != 0)
958789Sahrens 		return (-1);
959789Sahrens 
960789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
961789Sahrens 
9624543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
9632082Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
9642082Seschrock 		    "cannot destroy '%s'"), zhp->zpool_name);
965789Sahrens 
9662082Seschrock 		if (errno == EROFS) {
9672082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9682082Seschrock 			    "one or more devices is read only"));
9692082Seschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
9702082Seschrock 		} else {
9712082Seschrock 			(void) zpool_standard_error(hdl, errno, msg);
972789Sahrens 		}
973789Sahrens 
974789Sahrens 		if (zfp)
975789Sahrens 			zfs_close(zfp);
976789Sahrens 		return (-1);
977789Sahrens 	}
978789Sahrens 
979789Sahrens 	if (zfp) {
980789Sahrens 		remove_mountpoint(zfp);
981789Sahrens 		zfs_close(zfp);
982789Sahrens 	}
983789Sahrens 
984789Sahrens 	return (0);
985789Sahrens }
986789Sahrens 
987789Sahrens /*
988789Sahrens  * Add the given vdevs to the pool.  The caller must have already performed the
989789Sahrens  * necessary verification to ensure that the vdev specification is well-formed.
990789Sahrens  */
991789Sahrens int
992789Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
993789Sahrens {
9942676Seschrock 	zfs_cmd_t zc = { 0 };
9952082Seschrock 	int ret;
9962082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
9972082Seschrock 	char msg[1024];
9985450Sbrendan 	nvlist_t **spares, **l2cache;
9995450Sbrendan 	uint_t nspares, nl2cache;
10002082Seschrock 
10012082Seschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
10022082Seschrock 	    "cannot add to '%s'"), zhp->zpool_name);
10032082Seschrock 
10045450Sbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
10055450Sbrendan 	    SPA_VERSION_SPARES &&
10062082Seschrock 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
10072082Seschrock 	    &spares, &nspares) == 0) {
10082082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
10092082Seschrock 		    "upgraded to add hot spares"));
10102082Seschrock 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
10112082Seschrock 	}
1012789Sahrens 
10135450Sbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
10145450Sbrendan 	    SPA_VERSION_L2CACHE &&
10155450Sbrendan 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
10165450Sbrendan 	    &l2cache, &nl2cache) == 0) {
10175450Sbrendan 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
10185450Sbrendan 		    "upgraded to add cache devices"));
10195450Sbrendan 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
10205450Sbrendan 	}
10215450Sbrendan 
10225094Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
10232082Seschrock 		return (-1);
1024789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1025789Sahrens 
10264543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1027789Sahrens 		switch (errno) {
1028789Sahrens 		case EBUSY:
1029789Sahrens 			/*
1030789Sahrens 			 * This can happen if the user has specified the same
1031789Sahrens 			 * device multiple times.  We can't reliably detect this
1032789Sahrens 			 * until we try to add it and see we already have a
1033789Sahrens 			 * label.
1034789Sahrens 			 */
10352082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10362082Seschrock 			    "one or more vdevs refer to the same device"));
10372082Seschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1038789Sahrens 			break;
1039789Sahrens 
1040789Sahrens 		case EOVERFLOW:
1041789Sahrens 			/*
1042789Sahrens 			 * This occurrs when one of the devices is below
1043789Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1044789Sahrens 			 * device was the problem device since there's no
1045789Sahrens 			 * reliable way to determine device size from userland.
1046789Sahrens 			 */
1047789Sahrens 			{
1048789Sahrens 				char buf[64];
1049789Sahrens 
1050789Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1051789Sahrens 
10522082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10532082Seschrock 				    "device is less than the minimum "
10542082Seschrock 				    "size (%s)"), buf);
1055789Sahrens 			}
10562082Seschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
10572082Seschrock 			break;
10582082Seschrock 
10592082Seschrock 		case ENOTSUP:
10602082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10614527Sperrin 			    "pool must be upgraded to add these vdevs"));
10622082Seschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, msg);
1063789Sahrens 			break;
1064789Sahrens 
10653912Slling 		case EDOM:
10663912Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10674527Sperrin 			    "root pool can not have multiple vdevs"
10684527Sperrin 			    " or separate logs"));
10693912Slling 			(void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
10703912Slling 			break;
10713912Slling 
10725450Sbrendan 		case ENOTBLK:
10735450Sbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10745450Sbrendan 			    "cache device must be a disk or disk slice"));
10755450Sbrendan 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
10765450Sbrendan 			break;
10775450Sbrendan 
1078789Sahrens 		default:
10792082Seschrock 			(void) zpool_standard_error(hdl, errno, msg);
1080789Sahrens 		}
1081789Sahrens 
10822082Seschrock 		ret = -1;
10832082Seschrock 	} else {
10842082Seschrock 		ret = 0;
1085789Sahrens 	}
1086789Sahrens 
10872676Seschrock 	zcmd_free_nvlists(&zc);
1088789Sahrens 
10892082Seschrock 	return (ret);
1090789Sahrens }
1091789Sahrens 
1092789Sahrens /*
1093789Sahrens  * Exports the pool from the system.  The caller must ensure that there are no
1094789Sahrens  * mounted datasets in the pool.
1095789Sahrens  */
1096789Sahrens int
10977214Slling zpool_export(zpool_handle_t *zhp, boolean_t force)
1098789Sahrens {
1099789Sahrens 	zfs_cmd_t zc = { 0 };
11007214Slling 	char msg[1024];
1101789Sahrens 
1102789Sahrens 	if (zpool_remove_zvol_links(zhp) != 0)
1103789Sahrens 		return (-1);
1104789Sahrens 
11057214Slling 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
11067214Slling 	    "cannot export '%s'"), zhp->zpool_name);
11077214Slling 
1108789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
11097214Slling 	zc.zc_cookie = force;
11107214Slling 
11117214Slling 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
11127214Slling 		switch (errno) {
11137214Slling 		case EXDEV:
11147214Slling 			zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
11157214Slling 			    "use '-f' to override the following errors:\n"
11167214Slling 			    "'%s' has an active shared spare which could be"
11177214Slling 			    " used by other pools once '%s' is exported."),
11187214Slling 			    zhp->zpool_name, zhp->zpool_name);
11197214Slling 			return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
11207214Slling 			    msg));
11217214Slling 		default:
11227214Slling 			return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
11237214Slling 			    msg));
11247214Slling 		}
11257214Slling 	}
11267214Slling 
1127789Sahrens 	return (0);
1128789Sahrens }
1129789Sahrens 
1130789Sahrens /*
11315094Slling  * zpool_import() is a contracted interface. Should be kept the same
11325094Slling  * if possible.
11335094Slling  *
11345094Slling  * Applications should use zpool_import_props() to import a pool with
11355094Slling  * new properties value to be set.
1136789Sahrens  */
1137789Sahrens int
11382082Seschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
11395094Slling     char *altroot)
11405094Slling {
11415094Slling 	nvlist_t *props = NULL;
11425094Slling 	int ret;
11435094Slling 
11445094Slling 	if (altroot != NULL) {
11455094Slling 		if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
11465094Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
11475094Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
11485094Slling 			    newname));
11495094Slling 		}
11505094Slling 
11515094Slling 		if (nvlist_add_string(props,
11525094Slling 		    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0) {
11535094Slling 			nvlist_free(props);
11545094Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
11555094Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
11565094Slling 			    newname));
11575094Slling 		}
11585094Slling 	}
11595094Slling 
11606643Seschrock 	ret = zpool_import_props(hdl, config, newname, props, B_FALSE);
11615094Slling 	if (props)
11625094Slling 		nvlist_free(props);
11635094Slling 	return (ret);
11645094Slling }
11655094Slling 
11665094Slling /*
11675094Slling  * Import the given pool using the known configuration and a list of
11685094Slling  * properties to be set. The configuration should have come from
11695094Slling  * zpool_find_import(). The 'newname' parameters control whether the pool
11705094Slling  * is imported with a different name.
11715094Slling  */
11725094Slling int
11735094Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
11746643Seschrock     nvlist_t *props, boolean_t importfaulted)
1175789Sahrens {
11762676Seschrock 	zfs_cmd_t zc = { 0 };
1177789Sahrens 	char *thename;
1178789Sahrens 	char *origname;
1179789Sahrens 	int ret;
11805094Slling 	char errbuf[1024];
1181789Sahrens 
1182789Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1183789Sahrens 	    &origname) == 0);
1184789Sahrens 
11855094Slling 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
11865094Slling 	    "cannot import pool '%s'"), origname);
11875094Slling 
1188789Sahrens 	if (newname != NULL) {
11892082Seschrock 		if (!zpool_name_valid(hdl, B_FALSE, newname))
11903237Slling 			return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
11912082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
11922082Seschrock 			    newname));
1193789Sahrens 		thename = (char *)newname;
1194789Sahrens 	} else {
1195789Sahrens 		thename = origname;
1196789Sahrens 	}
1197789Sahrens 
11985094Slling 	if (props) {
11995094Slling 		uint64_t version;
12005094Slling 
12015094Slling 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
12025094Slling 		    &version) == 0);
12035094Slling 
12047184Stimh 		if ((props = zpool_valid_proplist(hdl, origname,
12055320Slling 		    props, version, B_TRUE, errbuf)) == NULL) {
12065094Slling 			return (-1);
12075320Slling 		} else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
12085320Slling 			nvlist_free(props);
12095094Slling 			return (-1);
12105320Slling 		}
12115094Slling 	}
1212789Sahrens 
1213789Sahrens 	(void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1214789Sahrens 
1215789Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
12161544Seschrock 	    &zc.zc_guid) == 0);
1217789Sahrens 
12185320Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
12195320Slling 		nvlist_free(props);
12202082Seschrock 		return (-1);
12215320Slling 	}
1222789Sahrens 
12236643Seschrock 	zc.zc_cookie = (uint64_t)importfaulted;
1224789Sahrens 	ret = 0;
12254543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) {
1226789Sahrens 		char desc[1024];
1227789Sahrens 		if (newname == NULL)
1228789Sahrens 			(void) snprintf(desc, sizeof (desc),
1229789Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1230789Sahrens 			    thename);
1231789Sahrens 		else
1232789Sahrens 			(void) snprintf(desc, sizeof (desc),
1233789Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1234789Sahrens 			    origname, thename);
1235789Sahrens 
1236789Sahrens 		switch (errno) {
12371544Seschrock 		case ENOTSUP:
12381544Seschrock 			/*
12391544Seschrock 			 * Unsupported version.
12401544Seschrock 			 */
12412082Seschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, desc);
12421544Seschrock 			break;
12431544Seschrock 
12442174Seschrock 		case EINVAL:
12452174Seschrock 			(void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
12462174Seschrock 			break;
12472174Seschrock 
1248789Sahrens 		default:
12492082Seschrock 			(void) zpool_standard_error(hdl, errno, desc);
1250789Sahrens 		}
1251789Sahrens 
1252789Sahrens 		ret = -1;
1253789Sahrens 	} else {
1254789Sahrens 		zpool_handle_t *zhp;
12554543Smarks 
1256789Sahrens 		/*
1257789Sahrens 		 * This should never fail, but play it safe anyway.
1258789Sahrens 		 */
12592142Seschrock 		if (zpool_open_silent(hdl, thename, &zhp) != 0) {
12602142Seschrock 			ret = -1;
12612142Seschrock 		} else if (zhp != NULL) {
1262789Sahrens 			ret = zpool_create_zvol_links(zhp);
1263789Sahrens 			zpool_close(zhp);
1264789Sahrens 		}
12654543Smarks 
1266789Sahrens 	}
1267789Sahrens 
12682676Seschrock 	zcmd_free_nvlists(&zc);
12695320Slling 	nvlist_free(props);
12705320Slling 
1271789Sahrens 	return (ret);
1272789Sahrens }
1273789Sahrens 
1274789Sahrens /*
1275789Sahrens  * Scrub the pool.
1276789Sahrens  */
1277789Sahrens int
1278789Sahrens zpool_scrub(zpool_handle_t *zhp, pool_scrub_type_t type)
1279789Sahrens {
1280789Sahrens 	zfs_cmd_t zc = { 0 };
1281789Sahrens 	char msg[1024];
12822082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1283789Sahrens 
1284789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1285789Sahrens 	zc.zc_cookie = type;
1286789Sahrens 
12874543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCRUB, &zc) == 0)
1288789Sahrens 		return (0);
1289789Sahrens 
1290789Sahrens 	(void) snprintf(msg, sizeof (msg),
1291789Sahrens 	    dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1292789Sahrens 
12932082Seschrock 	if (errno == EBUSY)
12942082Seschrock 		return (zfs_error(hdl, EZFS_RESILVERING, msg));
12952082Seschrock 	else
12962082Seschrock 		return (zpool_standard_error(hdl, errno, msg));
1297789Sahrens }
1298789Sahrens 
12992468Sek110237 /*
13002468Sek110237  * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
13012468Sek110237  * spare; but FALSE if its an INUSE spare.
13022468Sek110237  */
13032082Seschrock static nvlist_t *
13042082Seschrock vdev_to_nvlist_iter(nvlist_t *nv, const char *search, uint64_t guid,
13055450Sbrendan     boolean_t *avail_spare, boolean_t *l2cache)
13061544Seschrock {
13071544Seschrock 	uint_t c, children;
13081544Seschrock 	nvlist_t **child;
13092082Seschrock 	uint64_t theguid, present;
13101544Seschrock 	char *path;
13111544Seschrock 	uint64_t wholedisk = 0;
13122082Seschrock 	nvlist_t *ret;
13131544Seschrock 
13142082Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &theguid) == 0);
13151544Seschrock 
13161544Seschrock 	if (search == NULL &&
13171544Seschrock 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &present) == 0) {
13181544Seschrock 		/*
13191544Seschrock 		 * If the device has never been present since import, the only
13201544Seschrock 		 * reliable way to match the vdev is by GUID.
13211544Seschrock 		 */
13222082Seschrock 		if (theguid == guid)
13232082Seschrock 			return (nv);
13241544Seschrock 	} else if (search != NULL &&
13251544Seschrock 	    nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
13261544Seschrock 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
13271544Seschrock 		    &wholedisk);
13281544Seschrock 		if (wholedisk) {
13291544Seschrock 			/*
13301544Seschrock 			 * For whole disks, the internal path has 's0', but the
13311544Seschrock 			 * path passed in by the user doesn't.
13321544Seschrock 			 */
13331544Seschrock 			if (strlen(search) == strlen(path) - 2 &&
13341544Seschrock 			    strncmp(search, path, strlen(search)) == 0)
13352082Seschrock 				return (nv);
13361544Seschrock 		} else if (strcmp(search, path) == 0) {
13372082Seschrock 			return (nv);
13381544Seschrock 		}
13391544Seschrock 	}
13401544Seschrock 
13411544Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
13421544Seschrock 	    &child, &children) != 0)
13432082Seschrock 		return (NULL);
13441544Seschrock 
13451544Seschrock 	for (c = 0; c < children; c++)
13462082Seschrock 		if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
13475450Sbrendan 		    avail_spare, l2cache)) != NULL)
13481544Seschrock 			return (ret);
13491544Seschrock 
13502082Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
13512082Seschrock 	    &child, &children) == 0) {
13522082Seschrock 		for (c = 0; c < children; c++) {
13532082Seschrock 			if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
13545450Sbrendan 			    avail_spare, l2cache)) != NULL) {
13552468Sek110237 				*avail_spare = B_TRUE;
13562082Seschrock 				return (ret);
13572082Seschrock 			}
13582082Seschrock 		}
13592082Seschrock 	}
13602082Seschrock 
13615450Sbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
13625450Sbrendan 	    &child, &children) == 0) {
13635450Sbrendan 		for (c = 0; c < children; c++) {
13645450Sbrendan 			if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
13655450Sbrendan 			    avail_spare, l2cache)) != NULL) {
13665450Sbrendan 				*l2cache = B_TRUE;
13675450Sbrendan 				return (ret);
13685450Sbrendan 			}
13695450Sbrendan 		}
13705450Sbrendan 	}
13715450Sbrendan 
13722082Seschrock 	return (NULL);
13731544Seschrock }
13741544Seschrock 
13752082Seschrock nvlist_t *
13765450Sbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
13775450Sbrendan     boolean_t *l2cache)
13781544Seschrock {
13791544Seschrock 	char buf[MAXPATHLEN];
13801544Seschrock 	const char *search;
13811544Seschrock 	char *end;
13821544Seschrock 	nvlist_t *nvroot;
13831544Seschrock 	uint64_t guid;
13841544Seschrock 
13851613Seschrock 	guid = strtoull(path, &end, 10);
13861544Seschrock 	if (guid != 0 && *end == '\0') {
13871544Seschrock 		search = NULL;
13881544Seschrock 	} else if (path[0] != '/') {
13891544Seschrock 		(void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path);
13901544Seschrock 		search = buf;
13911544Seschrock 	} else {
13921544Seschrock 		search = path;
13931544Seschrock 	}
13941544Seschrock 
13951544Seschrock 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
13961544Seschrock 	    &nvroot) == 0);
13971544Seschrock 
13982468Sek110237 	*avail_spare = B_FALSE;
13995450Sbrendan 	*l2cache = B_FALSE;
14005450Sbrendan 	return (vdev_to_nvlist_iter(nvroot, search, guid, avail_spare,
14015450Sbrendan 	    l2cache));
14022468Sek110237 }
14032468Sek110237 
14042468Sek110237 /*
14055450Sbrendan  * Returns TRUE if the given guid corresponds to the given type.
14065450Sbrendan  * This is used to check for hot spares (INUSE or not), and level 2 cache
14075450Sbrendan  * devices.
14082468Sek110237  */
14092468Sek110237 static boolean_t
14105450Sbrendan is_guid_type(zpool_handle_t *zhp, uint64_t guid, const char *type)
14112468Sek110237 {
14125450Sbrendan 	uint64_t target_guid;
14132468Sek110237 	nvlist_t *nvroot;
14145450Sbrendan 	nvlist_t **list;
14155450Sbrendan 	uint_t count;
14162468Sek110237 	int i;
14172468Sek110237 
14182468Sek110237 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
14192468Sek110237 	    &nvroot) == 0);
14205450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, type, &list, &count) == 0) {
14215450Sbrendan 		for (i = 0; i < count; i++) {
14225450Sbrendan 			verify(nvlist_lookup_uint64(list[i], ZPOOL_CONFIG_GUID,
14235450Sbrendan 			    &target_guid) == 0);
14245450Sbrendan 			if (guid == target_guid)
14252468Sek110237 				return (B_TRUE);
14262468Sek110237 		}
14272468Sek110237 	}
14282468Sek110237 
14292468Sek110237 	return (B_FALSE);
14301544Seschrock }
14311544Seschrock 
1432789Sahrens /*
14334451Seschrock  * Bring the specified vdev online.   The 'flags' parameter is a set of the
14344451Seschrock  * ZFS_ONLINE_* flags.
1435789Sahrens  */
1436789Sahrens int
14374451Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
14384451Seschrock     vdev_state_t *newstate)
1439789Sahrens {
1440789Sahrens 	zfs_cmd_t zc = { 0 };
1441789Sahrens 	char msg[1024];
14422082Seschrock 	nvlist_t *tgt;
14435450Sbrendan 	boolean_t avail_spare, l2cache;
14442082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1445789Sahrens 
14461544Seschrock 	(void) snprintf(msg, sizeof (msg),
14471544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot online %s"), path);
1448789Sahrens 
14491544Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
14505450Sbrendan 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == NULL)
14512082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
1452789Sahrens 
14532468Sek110237 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
14542468Sek110237 
14555450Sbrendan 	if (avail_spare ||
14565450Sbrendan 	    is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
14572082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
14582082Seschrock 
14594451Seschrock 	zc.zc_cookie = VDEV_STATE_ONLINE;
14604451Seschrock 	zc.zc_obj = flags;
14614451Seschrock 
14624543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0)
14634451Seschrock 		return (zpool_standard_error(hdl, errno, msg));
14644451Seschrock 
14654451Seschrock 	*newstate = zc.zc_cookie;
14664451Seschrock 	return (0);
1467789Sahrens }
1468789Sahrens 
1469789Sahrens /*
1470789Sahrens  * Take the specified vdev offline
1471789Sahrens  */
1472789Sahrens int
14734451Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
1474789Sahrens {
1475789Sahrens 	zfs_cmd_t zc = { 0 };
1476789Sahrens 	char msg[1024];
14772082Seschrock 	nvlist_t *tgt;
14785450Sbrendan 	boolean_t avail_spare, l2cache;
14792082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1480789Sahrens 
14811544Seschrock 	(void) snprintf(msg, sizeof (msg),
14821544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
14831544Seschrock 
1484789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
14855450Sbrendan 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == NULL)
14862082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
14872082Seschrock 
14882468Sek110237 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
14892468Sek110237 
14905450Sbrendan 	if (avail_spare ||
14915450Sbrendan 	    is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
14922082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
14932082Seschrock 
14944451Seschrock 	zc.zc_cookie = VDEV_STATE_OFFLINE;
14954451Seschrock 	zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
14961485Slling 
14974543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
1498789Sahrens 		return (0);
1499789Sahrens 
1500789Sahrens 	switch (errno) {
15012082Seschrock 	case EBUSY:
1502789Sahrens 
1503789Sahrens 		/*
1504789Sahrens 		 * There are no other replicas of this device.
1505789Sahrens 		 */
15062082Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
15072082Seschrock 
15082082Seschrock 	default:
15092082Seschrock 		return (zpool_standard_error(hdl, errno, msg));
15102082Seschrock 	}
15112082Seschrock }
1512789Sahrens 
15132082Seschrock /*
15144451Seschrock  * Mark the given vdev faulted.
15154451Seschrock  */
15164451Seschrock int
15174451Seschrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid)
15184451Seschrock {
15194451Seschrock 	zfs_cmd_t zc = { 0 };
15204451Seschrock 	char msg[1024];
15214451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
15224451Seschrock 
15234451Seschrock 	(void) snprintf(msg, sizeof (msg),
15244451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
15254451Seschrock 
15264451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
15274451Seschrock 	zc.zc_guid = guid;
15284451Seschrock 	zc.zc_cookie = VDEV_STATE_FAULTED;
15294451Seschrock 
15304451Seschrock 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
15314451Seschrock 		return (0);
15324451Seschrock 
15334451Seschrock 	switch (errno) {
15344451Seschrock 	case EBUSY:
15354451Seschrock 
15364451Seschrock 		/*
15374451Seschrock 		 * There are no other replicas of this device.
15384451Seschrock 		 */
15394451Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
15404451Seschrock 
15414451Seschrock 	default:
15424451Seschrock 		return (zpool_standard_error(hdl, errno, msg));
15434451Seschrock 	}
15444451Seschrock 
15454451Seschrock }
15464451Seschrock 
15474451Seschrock /*
15484451Seschrock  * Mark the given vdev degraded.
15494451Seschrock  */
15504451Seschrock int
15514451Seschrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid)
15524451Seschrock {
15534451Seschrock 	zfs_cmd_t zc = { 0 };
15544451Seschrock 	char msg[1024];
15554451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
15564451Seschrock 
15574451Seschrock 	(void) snprintf(msg, sizeof (msg),
15584451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
15594451Seschrock 
15604451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
15614451Seschrock 	zc.zc_guid = guid;
15624451Seschrock 	zc.zc_cookie = VDEV_STATE_DEGRADED;
15634451Seschrock 
15644451Seschrock 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
15654451Seschrock 		return (0);
15664451Seschrock 
15674451Seschrock 	return (zpool_standard_error(hdl, errno, msg));
15684451Seschrock }
15694451Seschrock 
15704451Seschrock /*
15712082Seschrock  * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
15722082Seschrock  * a hot spare.
15732082Seschrock  */
15742082Seschrock static boolean_t
15752082Seschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
15762082Seschrock {
15772082Seschrock 	nvlist_t **child;
15782082Seschrock 	uint_t c, children;
15792082Seschrock 	char *type;
15802082Seschrock 
15812082Seschrock 	if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
15822082Seschrock 	    &children) == 0) {
15832082Seschrock 		verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
15842082Seschrock 		    &type) == 0);
15852082Seschrock 
15862082Seschrock 		if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
15872082Seschrock 		    children == 2 && child[which] == tgt)
15882082Seschrock 			return (B_TRUE);
15892082Seschrock 
15902082Seschrock 		for (c = 0; c < children; c++)
15912082Seschrock 			if (is_replacing_spare(child[c], tgt, which))
15922082Seschrock 				return (B_TRUE);
1593789Sahrens 	}
15942082Seschrock 
15952082Seschrock 	return (B_FALSE);
1596789Sahrens }
1597789Sahrens 
1598789Sahrens /*
1599789Sahrens  * Attach new_disk (fully described by nvroot) to old_disk.
16004527Sperrin  * If 'replacing' is specified, the new disk will replace the old one.
1601789Sahrens  */
1602789Sahrens int
1603789Sahrens zpool_vdev_attach(zpool_handle_t *zhp,
1604789Sahrens     const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
1605789Sahrens {
1606789Sahrens 	zfs_cmd_t zc = { 0 };
1607789Sahrens 	char msg[1024];
1608789Sahrens 	int ret;
16092082Seschrock 	nvlist_t *tgt;
16105450Sbrendan 	boolean_t avail_spare, l2cache;
16114527Sperrin 	uint64_t val, is_log;
16127041Seschrock 	char *path, *newname;
16132082Seschrock 	nvlist_t **child;
16142082Seschrock 	uint_t children;
16152082Seschrock 	nvlist_t *config_root;
16162082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1617789Sahrens 
16181544Seschrock 	if (replacing)
16191544Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
16201544Seschrock 		    "cannot replace %s with %s"), old_disk, new_disk);
16211544Seschrock 	else
16221544Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
16231544Seschrock 		    "cannot attach %s to %s"), new_disk, old_disk);
16241544Seschrock 
1625789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
16265450Sbrendan 	if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache)) == 0)
16272082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
16282082Seschrock 
16292468Sek110237 	if (avail_spare)
16302082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
16312082Seschrock 
16325450Sbrendan 	if (l2cache)
16335450Sbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
16345450Sbrendan 
16352082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
16362082Seschrock 	zc.zc_cookie = replacing;
16372082Seschrock 
16382082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
16392082Seschrock 	    &child, &children) != 0 || children != 1) {
16402082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16412082Seschrock 		    "new device must be a single disk"));
16422082Seschrock 		return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
16431544Seschrock 	}
16442082Seschrock 
16452082Seschrock 	verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
16462082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
16472082Seschrock 
16487041Seschrock 	if ((newname = zpool_vdev_name(NULL, NULL, child[0])) == NULL)
16497041Seschrock 		return (-1);
16507041Seschrock 
16512082Seschrock 	/*
16522082Seschrock 	 * If the target is a hot spare that has been swapped in, we can only
16532082Seschrock 	 * replace it with another hot spare.
16542082Seschrock 	 */
16552082Seschrock 	if (replacing &&
16562082Seschrock 	    nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
16577041Seschrock 	    (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache) == NULL ||
16582468Sek110237 	    !avail_spare) && is_replacing_spare(config_root, tgt, 1)) {
16592082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16602082Seschrock 		    "can only be replaced by another hot spare"));
16617041Seschrock 		free(newname);
16622082Seschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
16632082Seschrock 	}
16642082Seschrock 
16652082Seschrock 	/*
16662082Seschrock 	 * If we are attempting to replace a spare, it canot be applied to an
16672082Seschrock 	 * already spared device.
16682082Seschrock 	 */
16692082Seschrock 	if (replacing &&
16702082Seschrock 	    nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 &&
16717041Seschrock 	    zpool_find_vdev(zhp, newname, &avail_spare, &l2cache) != NULL &&
16725450Sbrendan 	    avail_spare && is_replacing_spare(config_root, tgt, 0)) {
16732082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16742082Seschrock 		    "device has already been replaced with a spare"));
16757041Seschrock 		free(newname);
16762082Seschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
16772082Seschrock 	}
1678789Sahrens 
16797041Seschrock 	free(newname);
16807041Seschrock 
16815094Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
16822082Seschrock 		return (-1);
1683789Sahrens 
16844543Smarks 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc);
1685789Sahrens 
16862676Seschrock 	zcmd_free_nvlists(&zc);
1687789Sahrens 
1688789Sahrens 	if (ret == 0)
1689789Sahrens 		return (0);
1690789Sahrens 
1691789Sahrens 	switch (errno) {
16921544Seschrock 	case ENOTSUP:
1693789Sahrens 		/*
1694789Sahrens 		 * Can't attach to or replace this type of vdev.
1695789Sahrens 		 */
16964527Sperrin 		if (replacing) {
16974527Sperrin 			is_log = B_FALSE;
16984527Sperrin 			(void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_LOG,
16994527Sperrin 			    &is_log);
17004527Sperrin 			if (is_log)
17014527Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17024527Sperrin 				    "cannot replace a log with a spare"));
17034527Sperrin 			else
17044527Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17054527Sperrin 				    "cannot replace a replacing device"));
17064527Sperrin 		} else {
17072082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17082082Seschrock 			    "can only attach to mirrors and top-level "
17092082Seschrock 			    "disks"));
17104527Sperrin 		}
17112082Seschrock 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
1712789Sahrens 		break;
1713789Sahrens 
17141544Seschrock 	case EINVAL:
1715789Sahrens 		/*
1716789Sahrens 		 * The new device must be a single disk.
1717789Sahrens 		 */
17182082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17192082Seschrock 		    "new device must be a single disk"));
17202082Seschrock 		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
1721789Sahrens 		break;
1722789Sahrens 
17231544Seschrock 	case EBUSY:
17242082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
17252082Seschrock 		    new_disk);
17262082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1727789Sahrens 		break;
1728789Sahrens 
17291544Seschrock 	case EOVERFLOW:
1730789Sahrens 		/*
1731789Sahrens 		 * The new device is too small.
1732789Sahrens 		 */
17332082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17342082Seschrock 		    "device is too small"));
17352082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1736789Sahrens 		break;
1737789Sahrens 
17381544Seschrock 	case EDOM:
1739789Sahrens 		/*
1740789Sahrens 		 * The new device has a different alignment requirement.
1741789Sahrens 		 */
17422082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17432082Seschrock 		    "devices have different sector alignment"));
17442082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1745789Sahrens 		break;
1746789Sahrens 
17471544Seschrock 	case ENAMETOOLONG:
1748789Sahrens 		/*
1749789Sahrens 		 * The resulting top-level vdev spec won't fit in the label.
1750789Sahrens 		 */
17512082Seschrock 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
1752789Sahrens 		break;
1753789Sahrens 
17541544Seschrock 	default:
17552082Seschrock 		(void) zpool_standard_error(hdl, errno, msg);
1756789Sahrens 	}
1757789Sahrens 
17582082Seschrock 	return (-1);
1759789Sahrens }
1760789Sahrens 
1761789Sahrens /*
1762789Sahrens  * Detach the specified device.
1763789Sahrens  */
1764789Sahrens int
1765789Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
1766789Sahrens {
1767789Sahrens 	zfs_cmd_t zc = { 0 };
1768789Sahrens 	char msg[1024];
17692082Seschrock 	nvlist_t *tgt;
17705450Sbrendan 	boolean_t avail_spare, l2cache;
17712082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1772789Sahrens 
17731544Seschrock 	(void) snprintf(msg, sizeof (msg),
17741544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
17751544Seschrock 
1776789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
17775450Sbrendan 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == 0)
17782082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
1779789Sahrens 
17802468Sek110237 	if (avail_spare)
17812082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
17822082Seschrock 
17835450Sbrendan 	if (l2cache)
17845450Sbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
17855450Sbrendan 
17862082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
17872082Seschrock 
17884543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
1789789Sahrens 		return (0);
1790789Sahrens 
1791789Sahrens 	switch (errno) {
1792789Sahrens 
17931544Seschrock 	case ENOTSUP:
1794789Sahrens 		/*
1795789Sahrens 		 * Can't detach from this type of vdev.
1796789Sahrens 		 */
17972082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
17982082Seschrock 		    "applicable to mirror and replacing vdevs"));
17992082Seschrock 		(void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg);
1800789Sahrens 		break;
1801789Sahrens 
18021544Seschrock 	case EBUSY:
1803789Sahrens 		/*
1804789Sahrens 		 * There are no other replicas of this device.
1805789Sahrens 		 */
18062082Seschrock 		(void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
1807789Sahrens 		break;
1808789Sahrens 
18091544Seschrock 	default:
18102082Seschrock 		(void) zpool_standard_error(hdl, errno, msg);
18111544Seschrock 	}
18121544Seschrock 
18132082Seschrock 	return (-1);
18142082Seschrock }
18152082Seschrock 
18162082Seschrock /*
18175450Sbrendan  * Remove the given device.  Currently, this is supported only for hot spares
18185450Sbrendan  * and level 2 cache devices.
18192082Seschrock  */
18202082Seschrock int
18212082Seschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
18222082Seschrock {
18232082Seschrock 	zfs_cmd_t zc = { 0 };
18242082Seschrock 	char msg[1024];
18252082Seschrock 	nvlist_t *tgt;
18265450Sbrendan 	boolean_t avail_spare, l2cache;
18272082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
18282082Seschrock 
18292082Seschrock 	(void) snprintf(msg, sizeof (msg),
18302082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
18312082Seschrock 
18322082Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
18335450Sbrendan 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == 0)
18342082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
18352082Seschrock 
18365450Sbrendan 	if (!avail_spare && !l2cache) {
18372082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18385450Sbrendan 		    "only inactive hot spares or cache devices "
18395450Sbrendan 		    "can be removed"));
18402082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
18412082Seschrock 	}
18422082Seschrock 
18432082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
18442082Seschrock 
18454543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
18462082Seschrock 		return (0);
18472082Seschrock 
18482082Seschrock 	return (zpool_standard_error(hdl, errno, msg));
18491544Seschrock }
18501544Seschrock 
18511544Seschrock /*
18521544Seschrock  * Clear the errors for the pool, or the particular device if specified.
18531544Seschrock  */
18541544Seschrock int
18551544Seschrock zpool_clear(zpool_handle_t *zhp, const char *path)
18561544Seschrock {
18571544Seschrock 	zfs_cmd_t zc = { 0 };
18581544Seschrock 	char msg[1024];
18592082Seschrock 	nvlist_t *tgt;
18605450Sbrendan 	boolean_t avail_spare, l2cache;
18612082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
18621544Seschrock 
18631544Seschrock 	if (path)
18641544Seschrock 		(void) snprintf(msg, sizeof (msg),
18651544Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
18662676Seschrock 		    path);
18671544Seschrock 	else
18681544Seschrock 		(void) snprintf(msg, sizeof (msg),
18691544Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
18701544Seschrock 		    zhp->zpool_name);
18711544Seschrock 
18721544Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
18732082Seschrock 	if (path) {
18745450Sbrendan 		if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
18755450Sbrendan 		    &l2cache)) == 0)
18762082Seschrock 			return (zfs_error(hdl, EZFS_NODEVICE, msg));
18772082Seschrock 
18785450Sbrendan 		/*
18795450Sbrendan 		 * Don't allow error clearing for hot spares.  Do allow
18805450Sbrendan 		 * error clearing for l2cache devices.
18815450Sbrendan 		 */
18822468Sek110237 		if (avail_spare)
18832082Seschrock 			return (zfs_error(hdl, EZFS_ISSPARE, msg));
18842082Seschrock 
18852082Seschrock 		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
18862082Seschrock 		    &zc.zc_guid) == 0);
18871544Seschrock 	}
18881544Seschrock 
18894543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0)
18901544Seschrock 		return (0);
18911544Seschrock 
18922082Seschrock 	return (zpool_standard_error(hdl, errno, msg));
1893789Sahrens }
1894789Sahrens 
18953126Sahl /*
18964451Seschrock  * Similar to zpool_clear(), but takes a GUID (used by fmd).
18974451Seschrock  */
18984451Seschrock int
18994451Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
19004451Seschrock {
19014451Seschrock 	zfs_cmd_t zc = { 0 };
19024451Seschrock 	char msg[1024];
19034451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
19044451Seschrock 
19054451Seschrock 	(void) snprintf(msg, sizeof (msg),
19064451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
19074451Seschrock 	    guid);
19084451Seschrock 
19094451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
19104451Seschrock 	zc.zc_guid = guid;
19114451Seschrock 
19124451Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
19134451Seschrock 		return (0);
19144451Seschrock 
19154451Seschrock 	return (zpool_standard_error(hdl, errno, msg));
19164451Seschrock }
19174451Seschrock 
19184451Seschrock /*
19193126Sahl  * Iterate over all zvols in a given pool by walking the /dev/zvol/dsk/<pool>
19203126Sahl  * hierarchy.
19213126Sahl  */
19223126Sahl int
19233126Sahl zpool_iter_zvol(zpool_handle_t *zhp, int (*cb)(const char *, void *),
19243126Sahl     void *data)
1925789Sahrens {
19263126Sahl 	libzfs_handle_t *hdl = zhp->zpool_hdl;
19273126Sahl 	char (*paths)[MAXPATHLEN];
19283126Sahl 	size_t size = 4;
19293126Sahl 	int curr, fd, base, ret = 0;
19303126Sahl 	DIR *dirp;
19313126Sahl 	struct dirent *dp;
19323126Sahl 	struct stat st;
19333126Sahl 
19343126Sahl 	if ((base = open("/dev/zvol/dsk", O_RDONLY)) < 0)
19353126Sahl 		return (errno == ENOENT ? 0 : -1);
19363126Sahl 
19373126Sahl 	if (fstatat(base, zhp->zpool_name, &st, 0) != 0) {
19383126Sahl 		int err = errno;
19393126Sahl 		(void) close(base);
19403126Sahl 		return (err == ENOENT ? 0 : -1);
19413126Sahl 	}
1942789Sahrens 
1943789Sahrens 	/*
19443126Sahl 	 * Oddly this wasn't a directory -- ignore that failure since we
19453126Sahl 	 * know there are no links lower in the (non-existant) hierarchy.
1946789Sahrens 	 */
19473126Sahl 	if (!S_ISDIR(st.st_mode)) {
19483126Sahl 		(void) close(base);
19493126Sahl 		return (0);
19503126Sahl 	}
19513126Sahl 
19523126Sahl 	if ((paths = zfs_alloc(hdl, size * sizeof (paths[0]))) == NULL) {
19533126Sahl 		(void) close(base);
19543126Sahl 		return (-1);
1955789Sahrens 	}
1956789Sahrens 
19573126Sahl 	(void) strlcpy(paths[0], zhp->zpool_name, sizeof (paths[0]));
19583126Sahl 	curr = 0;
19593126Sahl 
19603126Sahl 	while (curr >= 0) {
19613126Sahl 		if (fstatat(base, paths[curr], &st, AT_SYMLINK_NOFOLLOW) != 0)
19623126Sahl 			goto err;
19633126Sahl 
19643126Sahl 		if (S_ISDIR(st.st_mode)) {
19653126Sahl 			if ((fd = openat(base, paths[curr], O_RDONLY)) < 0)
19663126Sahl 				goto err;
19673126Sahl 
19683126Sahl 			if ((dirp = fdopendir(fd)) == NULL) {
19693126Sahl 				(void) close(fd);
19703126Sahl 				goto err;
19713126Sahl 			}
19723126Sahl 
19733126Sahl 			while ((dp = readdir(dirp)) != NULL) {
19743126Sahl 				if (dp->d_name[0] == '.')
19753126Sahl 					continue;
19763126Sahl 
19773126Sahl 				if (curr + 1 == size) {
19783126Sahl 					paths = zfs_realloc(hdl, paths,
19793126Sahl 					    size * sizeof (paths[0]),
19803126Sahl 					    size * 2 * sizeof (paths[0]));
19813126Sahl 					if (paths == NULL) {
19823126Sahl 						(void) closedir(dirp);
19833126Sahl 						(void) close(fd);
19843126Sahl 						goto err;
19853126Sahl 					}
19863126Sahl 
19873126Sahl 					size *= 2;
19883126Sahl 				}
19893126Sahl 
19903126Sahl 				(void) strlcpy(paths[curr + 1], paths[curr],
19913126Sahl 				    sizeof (paths[curr + 1]));
19923126Sahl 				(void) strlcat(paths[curr], "/",
19933126Sahl 				    sizeof (paths[curr]));
19943126Sahl 				(void) strlcat(paths[curr], dp->d_name,
19953126Sahl 				    sizeof (paths[curr]));
19963126Sahl 				curr++;
19973126Sahl 			}
19983126Sahl 
19993126Sahl 			(void) closedir(dirp);
20003126Sahl 
20013126Sahl 		} else {
20023126Sahl 			if ((ret = cb(paths[curr], data)) != 0)
20033126Sahl 				break;
20043126Sahl 		}
20053126Sahl 
20063126Sahl 		curr--;
20073126Sahl 	}
20083126Sahl 
20093126Sahl 	free(paths);
20103126Sahl 	(void) close(base);
20113126Sahl 
20123126Sahl 	return (ret);
20133126Sahl 
20143126Sahl err:
20153126Sahl 	free(paths);
20163126Sahl 	(void) close(base);
20173126Sahl 	return (-1);
20183126Sahl }
20193126Sahl 
20203126Sahl typedef struct zvol_cb {
20213126Sahl 	zpool_handle_t *zcb_pool;
20223126Sahl 	boolean_t zcb_create;
20233126Sahl } zvol_cb_t;
20243126Sahl 
20253126Sahl /*ARGSUSED*/
20263126Sahl static int
20273126Sahl do_zvol_create(zfs_handle_t *zhp, void *data)
20283126Sahl {
20294657Sahrens 	int ret = 0;
20303126Sahl 
20314657Sahrens 	if (ZFS_IS_VOLUME(zhp)) {
20323126Sahl 		(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
20334657Sahrens 		ret = zfs_iter_snapshots(zhp, do_zvol_create, NULL);
20344657Sahrens 	}
20353126Sahl 
20364657Sahrens 	if (ret == 0)
20374657Sahrens 		ret = zfs_iter_filesystems(zhp, do_zvol_create, NULL);
2038789Sahrens 
2039789Sahrens 	zfs_close(zhp);
20403126Sahl 
2041789Sahrens 	return (ret);
2042789Sahrens }
2043789Sahrens 
2044789Sahrens /*
2045789Sahrens  * Iterate over all zvols in the pool and make any necessary minor nodes.
2046789Sahrens  */
2047789Sahrens int
2048789Sahrens zpool_create_zvol_links(zpool_handle_t *zhp)
2049789Sahrens {
2050789Sahrens 	zfs_handle_t *zfp;
2051789Sahrens 	int ret;
2052789Sahrens 
2053789Sahrens 	/*
2054789Sahrens 	 * If the pool is unavailable, just return success.
2055789Sahrens 	 */
20562082Seschrock 	if ((zfp = make_dataset_handle(zhp->zpool_hdl,
20572082Seschrock 	    zhp->zpool_name)) == NULL)
2058789Sahrens 		return (0);
2059789Sahrens 
20604657Sahrens 	ret = zfs_iter_filesystems(zfp, do_zvol_create, NULL);
2061789Sahrens 
2062789Sahrens 	zfs_close(zfp);
2063789Sahrens 	return (ret);
2064789Sahrens }
2065789Sahrens 
20663126Sahl static int
20673126Sahl do_zvol_remove(const char *dataset, void *data)
20683126Sahl {
20693126Sahl 	zpool_handle_t *zhp = data;
20703126Sahl 
20713126Sahl 	return (zvol_remove_link(zhp->zpool_hdl, dataset));
20723126Sahl }
20733126Sahl 
2074789Sahrens /*
20753126Sahl  * Iterate over all zvols in the pool and remove any minor nodes.  We iterate
20763126Sahl  * by examining the /dev links so that a corrupted pool doesn't impede this
20773126Sahl  * operation.
2078789Sahrens  */
2079789Sahrens int
2080789Sahrens zpool_remove_zvol_links(zpool_handle_t *zhp)
2081789Sahrens {
20823126Sahl 	return (zpool_iter_zvol(zhp, do_zvol_remove, zhp));
2083789Sahrens }
20841354Seschrock 
20851354Seschrock /*
20861354Seschrock  * Convert from a devid string to a path.
20871354Seschrock  */
20881354Seschrock static char *
20891354Seschrock devid_to_path(char *devid_str)
20901354Seschrock {
20911354Seschrock 	ddi_devid_t devid;
20921354Seschrock 	char *minor;
20931354Seschrock 	char *path;
20941354Seschrock 	devid_nmlist_t *list = NULL;
20951354Seschrock 	int ret;
20961354Seschrock 
20971354Seschrock 	if (devid_str_decode(devid_str, &devid, &minor) != 0)
20981354Seschrock 		return (NULL);
20991354Seschrock 
21001354Seschrock 	ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
21011354Seschrock 
21021354Seschrock 	devid_str_free(minor);
21031354Seschrock 	devid_free(devid);
21041354Seschrock 
21051354Seschrock 	if (ret != 0)
21061354Seschrock 		return (NULL);
21071354Seschrock 
21082082Seschrock 	if ((path = strdup(list[0].devname)) == NULL)
21092082Seschrock 		return (NULL);
21102082Seschrock 
21111354Seschrock 	devid_free_nmlist(list);
21121354Seschrock 
21131354Seschrock 	return (path);
21141354Seschrock }
21151354Seschrock 
21161354Seschrock /*
21171354Seschrock  * Convert from a path to a devid string.
21181354Seschrock  */
21191354Seschrock static char *
21201354Seschrock path_to_devid(const char *path)
21211354Seschrock {
21221354Seschrock 	int fd;
21231354Seschrock 	ddi_devid_t devid;
21241354Seschrock 	char *minor, *ret;
21251354Seschrock 
21261354Seschrock 	if ((fd = open(path, O_RDONLY)) < 0)
21271354Seschrock 		return (NULL);
21281354Seschrock 
21291354Seschrock 	minor = NULL;
21301354Seschrock 	ret = NULL;
21311354Seschrock 	if (devid_get(fd, &devid) == 0) {
21321354Seschrock 		if (devid_get_minor_name(fd, &minor) == 0)
21331354Seschrock 			ret = devid_str_encode(devid, minor);
21341354Seschrock 		if (minor != NULL)
21351354Seschrock 			devid_str_free(minor);
21361354Seschrock 		devid_free(devid);
21371354Seschrock 	}
21381354Seschrock 	(void) close(fd);
21391354Seschrock 
21401354Seschrock 	return (ret);
21411354Seschrock }
21421354Seschrock 
21431354Seschrock /*
21441354Seschrock  * Issue the necessary ioctl() to update the stored path value for the vdev.  We
21451354Seschrock  * ignore any failure here, since a common case is for an unprivileged user to
21461354Seschrock  * type 'zpool status', and we'll display the correct information anyway.
21471354Seschrock  */
21481354Seschrock static void
21491354Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
21501354Seschrock {
21511354Seschrock 	zfs_cmd_t zc = { 0 };
21521354Seschrock 
21531354Seschrock 	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
21542676Seschrock 	(void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
21551354Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
21561544Seschrock 	    &zc.zc_guid) == 0);
21571354Seschrock 
21582082Seschrock 	(void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
21591354Seschrock }
21601354Seschrock 
21611354Seschrock /*
21621354Seschrock  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
21631354Seschrock  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
21641354Seschrock  * We also check if this is a whole disk, in which case we strip off the
21651354Seschrock  * trailing 's0' slice name.
21661354Seschrock  *
21671354Seschrock  * This routine is also responsible for identifying when disks have been
21681354Seschrock  * reconfigured in a new location.  The kernel will have opened the device by
21691354Seschrock  * devid, but the path will still refer to the old location.  To catch this, we
21701354Seschrock  * first do a path -> devid translation (which is fast for the common case).  If
21711354Seschrock  * the devid matches, we're done.  If not, we do a reverse devid -> path
21721354Seschrock  * translation and issue the appropriate ioctl() to update the path of the vdev.
21731354Seschrock  * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
21741354Seschrock  * of these checks.
21751354Seschrock  */
21761354Seschrock char *
21772082Seschrock zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv)
21781354Seschrock {
21791354Seschrock 	char *path, *devid;
21801544Seschrock 	uint64_t value;
21811544Seschrock 	char buf[64];
21824451Seschrock 	vdev_stat_t *vs;
21834451Seschrock 	uint_t vsc;
21841354Seschrock 
21851544Seschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
21861544Seschrock 	    &value) == 0) {
21871544Seschrock 		verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
21881544Seschrock 		    &value) == 0);
21892856Snd150628 		(void) snprintf(buf, sizeof (buf), "%llu",
21902856Snd150628 		    (u_longlong_t)value);
21911544Seschrock 		path = buf;
21921544Seschrock 	} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
21931354Seschrock 
21944451Seschrock 		/*
21954451Seschrock 		 * If the device is dead (faulted, offline, etc) then don't
21964451Seschrock 		 * bother opening it.  Otherwise we may be forcing the user to
21974451Seschrock 		 * open a misbehaving device, which can have undesirable
21984451Seschrock 		 * effects.
21994451Seschrock 		 */
22004451Seschrock 		if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
22014451Seschrock 		    (uint64_t **)&vs, &vsc) != 0 ||
22024451Seschrock 		    vs->vs_state >= VDEV_STATE_DEGRADED) &&
22034451Seschrock 		    zhp != NULL &&
22041354Seschrock 		    nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
22051354Seschrock 			/*
22061354Seschrock 			 * Determine if the current path is correct.
22071354Seschrock 			 */
22081354Seschrock 			char *newdevid = path_to_devid(path);
22091354Seschrock 
22101354Seschrock 			if (newdevid == NULL ||
22111354Seschrock 			    strcmp(devid, newdevid) != 0) {
22121354Seschrock 				char *newpath;
22131354Seschrock 
22141354Seschrock 				if ((newpath = devid_to_path(devid)) != NULL) {
22151354Seschrock 					/*
22161354Seschrock 					 * Update the path appropriately.
22171354Seschrock 					 */
22181354Seschrock 					set_path(zhp, nv, newpath);
22192082Seschrock 					if (nvlist_add_string(nv,
22202082Seschrock 					    ZPOOL_CONFIG_PATH, newpath) == 0)
22212082Seschrock 						verify(nvlist_lookup_string(nv,
22222082Seschrock 						    ZPOOL_CONFIG_PATH,
22232082Seschrock 						    &path) == 0);
22241354Seschrock 					free(newpath);
22251354Seschrock 				}
22261354Seschrock 			}
22271354Seschrock 
22282082Seschrock 			if (newdevid)
22292082Seschrock 				devid_str_free(newdevid);
22301354Seschrock 		}
22311354Seschrock 
22321354Seschrock 		if (strncmp(path, "/dev/dsk/", 9) == 0)
22331354Seschrock 			path += 9;
22341354Seschrock 
22351354Seschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
22361544Seschrock 		    &value) == 0 && value) {
22372082Seschrock 			char *tmp = zfs_strdup(hdl, path);
22382082Seschrock 			if (tmp == NULL)
22392082Seschrock 				return (NULL);
22401354Seschrock 			tmp[strlen(path) - 2] = '\0';
22411354Seschrock 			return (tmp);
22421354Seschrock 		}
22431354Seschrock 	} else {
22441354Seschrock 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
22452082Seschrock 
22462082Seschrock 		/*
22472082Seschrock 		 * If it's a raidz device, we need to stick in the parity level.
22482082Seschrock 		 */
22492082Seschrock 		if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
22502082Seschrock 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
22512082Seschrock 			    &value) == 0);
22522082Seschrock 			(void) snprintf(buf, sizeof (buf), "%s%llu", path,
22532856Snd150628 			    (u_longlong_t)value);
22542082Seschrock 			path = buf;
22552082Seschrock 		}
22561354Seschrock 	}
22571354Seschrock 
22582082Seschrock 	return (zfs_strdup(hdl, path));
22591354Seschrock }
22601544Seschrock 
22611544Seschrock static int
22621544Seschrock zbookmark_compare(const void *a, const void *b)
22631544Seschrock {
22641544Seschrock 	return (memcmp(a, b, sizeof (zbookmark_t)));
22651544Seschrock }
22661544Seschrock 
22671544Seschrock /*
22681544Seschrock  * Retrieve the persistent error log, uniquify the members, and return to the
22691544Seschrock  * caller.
22701544Seschrock  */
22711544Seschrock int
22723444Sek110237 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
22731544Seschrock {
22741544Seschrock 	zfs_cmd_t zc = { 0 };
22751544Seschrock 	uint64_t count;
22762676Seschrock 	zbookmark_t *zb = NULL;
22773444Sek110237 	int i;
22781544Seschrock 
22791544Seschrock 	/*
22801544Seschrock 	 * Retrieve the raw error list from the kernel.  If the number of errors
22811544Seschrock 	 * has increased, allocate more space and continue until we get the
22821544Seschrock 	 * entire list.
22831544Seschrock 	 */
22841544Seschrock 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
22851544Seschrock 	    &count) == 0);
22864820Sek110237 	if (count == 0)
22874820Sek110237 		return (0);
22882676Seschrock 	if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
22892856Snd150628 	    count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
22902082Seschrock 		return (-1);
22912676Seschrock 	zc.zc_nvlist_dst_size = count;
22921544Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
22931544Seschrock 	for (;;) {
22942082Seschrock 		if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
22952082Seschrock 		    &zc) != 0) {
22962676Seschrock 			free((void *)(uintptr_t)zc.zc_nvlist_dst);
22971544Seschrock 			if (errno == ENOMEM) {
22983823Svb160487 				count = zc.zc_nvlist_dst_size;
22992676Seschrock 				if ((zc.zc_nvlist_dst = (uintptr_t)
23003823Svb160487 				    zfs_alloc(zhp->zpool_hdl, count *
23013823Svb160487 				    sizeof (zbookmark_t))) == (uintptr_t)NULL)
23022082Seschrock 					return (-1);
23031544Seschrock 			} else {
23041544Seschrock 				return (-1);
23051544Seschrock 			}
23061544Seschrock 		} else {
23071544Seschrock 			break;
23081544Seschrock 		}
23091544Seschrock 	}
23101544Seschrock 
23111544Seschrock 	/*
23121544Seschrock 	 * Sort the resulting bookmarks.  This is a little confusing due to the
23131544Seschrock 	 * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
23142676Seschrock 	 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
23151544Seschrock 	 * _not_ copied as part of the process.  So we point the start of our
23161544Seschrock 	 * array appropriate and decrement the total number of elements.
23171544Seschrock 	 */
23182676Seschrock 	zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
23192676Seschrock 	    zc.zc_nvlist_dst_size;
23202676Seschrock 	count -= zc.zc_nvlist_dst_size;
23211544Seschrock 
23221544Seschrock 	qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
23231544Seschrock 
23243444Sek110237 	verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
23251544Seschrock 
23261544Seschrock 	/*
23273444Sek110237 	 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
23281544Seschrock 	 */
23291544Seschrock 	for (i = 0; i < count; i++) {
23301544Seschrock 		nvlist_t *nv;
23311544Seschrock 
23323700Sek110237 		/* ignoring zb_blkid and zb_level for now */
23333700Sek110237 		if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
23343700Sek110237 		    zb[i-1].zb_object == zb[i].zb_object)
23351544Seschrock 			continue;
23361544Seschrock 
23373444Sek110237 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
23383444Sek110237 			goto nomem;
23393444Sek110237 		if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
23403444Sek110237 		    zb[i].zb_objset) != 0) {
23413444Sek110237 			nvlist_free(nv);
23422082Seschrock 			goto nomem;
23433444Sek110237 		}
23443444Sek110237 		if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
23453444Sek110237 		    zb[i].zb_object) != 0) {
23463444Sek110237 			nvlist_free(nv);
23473444Sek110237 			goto nomem;
23481544Seschrock 		}
23493444Sek110237 		if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
23503444Sek110237 			nvlist_free(nv);
23513444Sek110237 			goto nomem;
23523444Sek110237 		}
23533444Sek110237 		nvlist_free(nv);
23541544Seschrock 	}
23551544Seschrock 
23563265Sahrens 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
23571544Seschrock 	return (0);
23582082Seschrock 
23592082Seschrock nomem:
23602676Seschrock 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
23612082Seschrock 	return (no_memory(zhp->zpool_hdl));
23621544Seschrock }
23631760Seschrock 
23641760Seschrock /*
23651760Seschrock  * Upgrade a ZFS pool to the latest on-disk version.
23661760Seschrock  */
23671760Seschrock int
23685094Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
23691760Seschrock {
23701760Seschrock 	zfs_cmd_t zc = { 0 };
23712082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
23721760Seschrock 
23731760Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
23745094Slling 	zc.zc_cookie = new_version;
23755094Slling 
23764543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
23773237Slling 		return (zpool_standard_error_fmt(hdl, errno,
23782082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
23792082Seschrock 		    zhp->zpool_name));
23801760Seschrock 	return (0);
23811760Seschrock }
23822926Sek110237 
23834988Sek110237 void
23844988Sek110237 zpool_set_history_str(const char *subcommand, int argc, char **argv,
23854988Sek110237     char *history_str)
23864988Sek110237 {
23874988Sek110237 	int i;
23884988Sek110237 
23894988Sek110237 	(void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
23904988Sek110237 	for (i = 1; i < argc; i++) {
23914988Sek110237 		if (strlen(history_str) + 1 + strlen(argv[i]) >
23924988Sek110237 		    HIS_MAX_RECORD_LEN)
23934988Sek110237 			break;
23944988Sek110237 		(void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
23954988Sek110237 		(void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
23964988Sek110237 	}
23974988Sek110237 }
23984988Sek110237 
23992926Sek110237 /*
24004988Sek110237  * Stage command history for logging.
24012926Sek110237  */
24024988Sek110237 int
24034988Sek110237 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
24042926Sek110237 {
24054988Sek110237 	if (history_str == NULL)
24064988Sek110237 		return (EINVAL);
24074988Sek110237 
24084988Sek110237 	if (strlen(history_str) > HIS_MAX_RECORD_LEN)
24094988Sek110237 		return (EINVAL);
24102926Sek110237 
24114715Sek110237 	if (hdl->libzfs_log_str != NULL)
24124543Smarks 		free(hdl->libzfs_log_str);
24132926Sek110237 
24144988Sek110237 	if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
24154988Sek110237 		return (no_memory(hdl));
24164543Smarks 
24174988Sek110237 	return (0);
24182926Sek110237 }
24192926Sek110237 
24202926Sek110237 /*
24212926Sek110237  * Perform ioctl to get some command history of a pool.
24222926Sek110237  *
24232926Sek110237  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
24242926Sek110237  * logical offset of the history buffer to start reading from.
24252926Sek110237  *
24262926Sek110237  * Upon return, 'off' is the next logical offset to read from and
24272926Sek110237  * 'len' is the actual amount of bytes read into 'buf'.
24282926Sek110237  */
24292926Sek110237 static int
24302926Sek110237 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
24312926Sek110237 {
24322926Sek110237 	zfs_cmd_t zc = { 0 };
24332926Sek110237 	libzfs_handle_t *hdl = zhp->zpool_hdl;
24342926Sek110237 
24352926Sek110237 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
24362926Sek110237 
24372926Sek110237 	zc.zc_history = (uint64_t)(uintptr_t)buf;
24382926Sek110237 	zc.zc_history_len = *len;
24392926Sek110237 	zc.zc_history_offset = *off;
24402926Sek110237 
24412926Sek110237 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
24422926Sek110237 		switch (errno) {
24432926Sek110237 		case EPERM:
24443237Slling 			return (zfs_error_fmt(hdl, EZFS_PERM,
24453237Slling 			    dgettext(TEXT_DOMAIN,
24462926Sek110237 			    "cannot show history for pool '%s'"),
24472926Sek110237 			    zhp->zpool_name));
24482926Sek110237 		case ENOENT:
24493237Slling 			return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
24502926Sek110237 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
24512926Sek110237 			    "'%s'"), zhp->zpool_name));
24523863Sek110237 		case ENOTSUP:
24533863Sek110237 			return (zfs_error_fmt(hdl, EZFS_BADVERSION,
24543863Sek110237 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
24553863Sek110237 			    "'%s', pool must be upgraded"), zhp->zpool_name));
24562926Sek110237 		default:
24573237Slling 			return (zpool_standard_error_fmt(hdl, errno,
24582926Sek110237 			    dgettext(TEXT_DOMAIN,
24592926Sek110237 			    "cannot get history for '%s'"), zhp->zpool_name));
24602926Sek110237 		}
24612926Sek110237 	}
24622926Sek110237 
24632926Sek110237 	*len = zc.zc_history_len;
24642926Sek110237 	*off = zc.zc_history_offset;
24652926Sek110237 
24662926Sek110237 	return (0);
24672926Sek110237 }
24682926Sek110237 
24692926Sek110237 /*
24702926Sek110237  * Process the buffer of nvlists, unpacking and storing each nvlist record
24712926Sek110237  * into 'records'.  'leftover' is set to the number of bytes that weren't
24722926Sek110237  * processed as there wasn't a complete record.
24732926Sek110237  */
24742926Sek110237 static int
24752926Sek110237 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
24762926Sek110237     nvlist_t ***records, uint_t *numrecords)
24772926Sek110237 {
24782926Sek110237 	uint64_t reclen;
24792926Sek110237 	nvlist_t *nv;
24802926Sek110237 	int i;
24812926Sek110237 
24822926Sek110237 	while (bytes_read > sizeof (reclen)) {
24832926Sek110237 
24842926Sek110237 		/* get length of packed record (stored as little endian) */
24852926Sek110237 		for (i = 0, reclen = 0; i < sizeof (reclen); i++)
24862926Sek110237 			reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
24872926Sek110237 
24882926Sek110237 		if (bytes_read < sizeof (reclen) + reclen)
24892926Sek110237 			break;
24902926Sek110237 
24912926Sek110237 		/* unpack record */
24922926Sek110237 		if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
24932926Sek110237 			return (ENOMEM);
24942926Sek110237 		bytes_read -= sizeof (reclen) + reclen;
24952926Sek110237 		buf += sizeof (reclen) + reclen;
24962926Sek110237 
24972926Sek110237 		/* add record to nvlist array */
24982926Sek110237 		(*numrecords)++;
24992926Sek110237 		if (ISP2(*numrecords + 1)) {
25002926Sek110237 			*records = realloc(*records,
25012926Sek110237 			    *numrecords * 2 * sizeof (nvlist_t *));
25022926Sek110237 		}
25032926Sek110237 		(*records)[*numrecords - 1] = nv;
25042926Sek110237 	}
25052926Sek110237 
25062926Sek110237 	*leftover = bytes_read;
25072926Sek110237 	return (0);
25082926Sek110237 }
25092926Sek110237 
25102926Sek110237 #define	HIS_BUF_LEN	(128*1024)
25112926Sek110237 
25122926Sek110237 /*
25132926Sek110237  * Retrieve the command history of a pool.
25142926Sek110237  */
25152926Sek110237 int
25162926Sek110237 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
25172926Sek110237 {
25182926Sek110237 	char buf[HIS_BUF_LEN];
25192926Sek110237 	uint64_t off = 0;
25202926Sek110237 	nvlist_t **records = NULL;
25212926Sek110237 	uint_t numrecords = 0;
25222926Sek110237 	int err, i;
25232926Sek110237 
25242926Sek110237 	do {
25252926Sek110237 		uint64_t bytes_read = sizeof (buf);
25262926Sek110237 		uint64_t leftover;
25272926Sek110237 
25282926Sek110237 		if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
25292926Sek110237 			break;
25302926Sek110237 
25312926Sek110237 		/* if nothing else was read in, we're at EOF, just return */
25322926Sek110237 		if (!bytes_read)
25332926Sek110237 			break;
25342926Sek110237 
25352926Sek110237 		if ((err = zpool_history_unpack(buf, bytes_read,
25362926Sek110237 		    &leftover, &records, &numrecords)) != 0)
25372926Sek110237 			break;
25382926Sek110237 		off -= leftover;
25392926Sek110237 
25402926Sek110237 		/* CONSTCOND */
25412926Sek110237 	} while (1);
25422926Sek110237 
25432926Sek110237 	if (!err) {
25442926Sek110237 		verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
25452926Sek110237 		verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
25462926Sek110237 		    records, numrecords) == 0);
25472926Sek110237 	}
25482926Sek110237 	for (i = 0; i < numrecords; i++)
25492926Sek110237 		nvlist_free(records[i]);
25502926Sek110237 	free(records);
25512926Sek110237 
25522926Sek110237 	return (err);
25532926Sek110237 }
25543444Sek110237 
25553444Sek110237 void
25563444Sek110237 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
25573444Sek110237     char *pathname, size_t len)
25583444Sek110237 {
25593444Sek110237 	zfs_cmd_t zc = { 0 };
25603444Sek110237 	boolean_t mounted = B_FALSE;
25613444Sek110237 	char *mntpnt = NULL;
25623444Sek110237 	char dsname[MAXNAMELEN];
25633444Sek110237 
25643444Sek110237 	if (dsobj == 0) {
25653444Sek110237 		/* special case for the MOS */
25663444Sek110237 		(void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
25673444Sek110237 		return;
25683444Sek110237 	}
25693444Sek110237 
25703444Sek110237 	/* get the dataset's name */
25713444Sek110237 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
25723444Sek110237 	zc.zc_obj = dsobj;
25733444Sek110237 	if (ioctl(zhp->zpool_hdl->libzfs_fd,
25743444Sek110237 	    ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
25753444Sek110237 		/* just write out a path of two object numbers */
25763444Sek110237 		(void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
25773444Sek110237 		    dsobj, obj);
25783444Sek110237 		return;
25793444Sek110237 	}
25803444Sek110237 	(void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
25813444Sek110237 
25823444Sek110237 	/* find out if the dataset is mounted */
25833444Sek110237 	mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
25843444Sek110237 
25853444Sek110237 	/* get the corrupted object's path */
25863444Sek110237 	(void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
25873444Sek110237 	zc.zc_obj = obj;
25883444Sek110237 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
25893444Sek110237 	    &zc) == 0) {
25903444Sek110237 		if (mounted) {
25913444Sek110237 			(void) snprintf(pathname, len, "%s%s", mntpnt,
25923444Sek110237 			    zc.zc_value);
25933444Sek110237 		} else {
25943444Sek110237 			(void) snprintf(pathname, len, "%s:%s",
25953444Sek110237 			    dsname, zc.zc_value);
25963444Sek110237 		}
25973444Sek110237 	} else {
25983444Sek110237 		(void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
25993444Sek110237 	}
26003444Sek110237 	free(mntpnt);
26013444Sek110237 }
26023912Slling 
26034276Staylor #define	RDISK_ROOT	"/dev/rdsk"
26044276Staylor #define	BACKUP_SLICE	"s2"
26054276Staylor /*
26064276Staylor  * Don't start the slice at the default block of 34; many storage
26074276Staylor  * devices will use a stripe width of 128k, so start there instead.
26084276Staylor  */
26094276Staylor #define	NEW_START_BLOCK	256
26104276Staylor 
26114276Staylor /*
26127042Sgw25295  * Read the EFI label from the config, if a label does not exist then
26137042Sgw25295  * pass back the error to the caller. If the caller has passed a non-NULL
26147042Sgw25295  * diskaddr argument then we set it to the starting address of the EFI
26157042Sgw25295  * partition.
26167042Sgw25295  */
26177042Sgw25295 static int
26187042Sgw25295 read_efi_label(nvlist_t *config, diskaddr_t *sb)
26197042Sgw25295 {
26207042Sgw25295 	char *path;
26217042Sgw25295 	int fd;
26227042Sgw25295 	char diskname[MAXPATHLEN];
26237042Sgw25295 	int err = -1;
26247042Sgw25295 
26257042Sgw25295 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
26267042Sgw25295 		return (err);
26277042Sgw25295 
26287042Sgw25295 	(void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
26297042Sgw25295 	    strrchr(path, '/'));
26307042Sgw25295 	if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
26317042Sgw25295 		struct dk_gpt *vtoc;
26327042Sgw25295 
26337042Sgw25295 		if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
26347042Sgw25295 			if (sb != NULL)
26357042Sgw25295 				*sb = vtoc->efi_parts[0].p_start;
26367042Sgw25295 			efi_free(vtoc);
26377042Sgw25295 		}
26387042Sgw25295 		(void) close(fd);
26397042Sgw25295 	}
26407042Sgw25295 	return (err);
26417042Sgw25295 }
26427042Sgw25295 
26437042Sgw25295 /*
26444276Staylor  * determine where a partition starts on a disk in the current
26454276Staylor  * configuration
26464276Staylor  */
26474276Staylor static diskaddr_t
26484276Staylor find_start_block(nvlist_t *config)
26494276Staylor {
26504276Staylor 	nvlist_t **child;
26514276Staylor 	uint_t c, children;
26524276Staylor 	diskaddr_t sb = MAXOFFSET_T;
26534276Staylor 	uint64_t wholedisk;
26544276Staylor 
26554276Staylor 	if (nvlist_lookup_nvlist_array(config,
26564276Staylor 	    ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
26574276Staylor 		if (nvlist_lookup_uint64(config,
26584276Staylor 		    ZPOOL_CONFIG_WHOLE_DISK,
26594276Staylor 		    &wholedisk) != 0 || !wholedisk) {
26604276Staylor 			return (MAXOFFSET_T);
26614276Staylor 		}
26627042Sgw25295 		if (read_efi_label(config, &sb) < 0)
26637042Sgw25295 			sb = MAXOFFSET_T;
26644276Staylor 		return (sb);
26654276Staylor 	}
26664276Staylor 
26674276Staylor 	for (c = 0; c < children; c++) {
26684276Staylor 		sb = find_start_block(child[c]);
26694276Staylor 		if (sb != MAXOFFSET_T) {
26704276Staylor 			return (sb);
26714276Staylor 		}
26724276Staylor 	}
26734276Staylor 	return (MAXOFFSET_T);
26744276Staylor }
26754276Staylor 
26764276Staylor /*
26774276Staylor  * Label an individual disk.  The name provided is the short name,
26784276Staylor  * stripped of any leading /dev path.
26794276Staylor  */
26804276Staylor int
26814276Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
26824276Staylor {
26834276Staylor 	char path[MAXPATHLEN];
26844276Staylor 	struct dk_gpt *vtoc;
26854276Staylor 	int fd;
26864276Staylor 	size_t resv = EFI_MIN_RESV_SIZE;
26874276Staylor 	uint64_t slice_size;
26884276Staylor 	diskaddr_t start_block;
26894276Staylor 	char errbuf[1024];
26904276Staylor 
26916289Smmusante 	/* prepare an error message just in case */
26926289Smmusante 	(void) snprintf(errbuf, sizeof (errbuf),
26936289Smmusante 	    dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
26946289Smmusante 
26954276Staylor 	if (zhp) {
26964276Staylor 		nvlist_t *nvroot;
26974276Staylor 
26984276Staylor 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
26994276Staylor 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
27004276Staylor 
27014276Staylor 		if (zhp->zpool_start_block == 0)
27024276Staylor 			start_block = find_start_block(nvroot);
27034276Staylor 		else
27044276Staylor 			start_block = zhp->zpool_start_block;
27054276Staylor 		zhp->zpool_start_block = start_block;
27064276Staylor 	} else {
27074276Staylor 		/* new pool */
27084276Staylor 		start_block = NEW_START_BLOCK;
27094276Staylor 	}
27104276Staylor 
27114276Staylor 	(void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
27124276Staylor 	    BACKUP_SLICE);
27134276Staylor 
27144276Staylor 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
27154276Staylor 		/*
27164276Staylor 		 * This shouldn't happen.  We've long since verified that this
27174276Staylor 		 * is a valid device.
27184276Staylor 		 */
27196289Smmusante 		zfs_error_aux(hdl,
27206289Smmusante 		    dgettext(TEXT_DOMAIN, "unable to open device"));
27214276Staylor 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
27224276Staylor 	}
27234276Staylor 
27244276Staylor 	if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
27254276Staylor 		/*
27264276Staylor 		 * The only way this can fail is if we run out of memory, or we
27274276Staylor 		 * were unable to read the disk's capacity
27284276Staylor 		 */
27294276Staylor 		if (errno == ENOMEM)
27304276Staylor 			(void) no_memory(hdl);
27314276Staylor 
27324276Staylor 		(void) close(fd);
27336289Smmusante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27346289Smmusante 		    "unable to read disk capacity"), name);
27354276Staylor 
27364276Staylor 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
27374276Staylor 	}
27384276Staylor 
27394276Staylor 	slice_size = vtoc->efi_last_u_lba + 1;
27404276Staylor 	slice_size -= EFI_MIN_RESV_SIZE;
27414276Staylor 	if (start_block == MAXOFFSET_T)
27424276Staylor 		start_block = NEW_START_BLOCK;
27434276Staylor 	slice_size -= start_block;
27444276Staylor 
27454276Staylor 	vtoc->efi_parts[0].p_start = start_block;
27464276Staylor 	vtoc->efi_parts[0].p_size = slice_size;
27474276Staylor 
27484276Staylor 	/*
27494276Staylor 	 * Why we use V_USR: V_BACKUP confuses users, and is considered
27504276Staylor 	 * disposable by some EFI utilities (since EFI doesn't have a backup
27514276Staylor 	 * slice).  V_UNASSIGNED is supposed to be used only for zero size
27524276Staylor 	 * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
27534276Staylor 	 * etc. were all pretty specific.  V_USR is as close to reality as we
27544276Staylor 	 * can get, in the absence of V_OTHER.
27554276Staylor 	 */
27564276Staylor 	vtoc->efi_parts[0].p_tag = V_USR;
27574276Staylor 	(void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
27584276Staylor 
27594276Staylor 	vtoc->efi_parts[8].p_start = slice_size + start_block;
27604276Staylor 	vtoc->efi_parts[8].p_size = resv;
27614276Staylor 	vtoc->efi_parts[8].p_tag = V_RESERVED;
27624276Staylor 
27634276Staylor 	if (efi_write(fd, vtoc) != 0) {
27644276Staylor 		/*
27654276Staylor 		 * Some block drivers (like pcata) may not support EFI
27664276Staylor 		 * GPT labels.  Print out a helpful error message dir-
27674276Staylor 		 * ecting the user to manually label the disk and give
27684276Staylor 		 * a specific slice.
27694276Staylor 		 */
27704276Staylor 		(void) close(fd);
27714276Staylor 		efi_free(vtoc);
27724276Staylor 
27734276Staylor 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27746289Smmusante 		    "try using fdisk(1M) and then provide a specific slice"));
27754276Staylor 		return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
27764276Staylor 	}
27774276Staylor 
27784276Staylor 	(void) close(fd);
27794276Staylor 	efi_free(vtoc);
27804276Staylor 	return (0);
27814276Staylor }
27826423Sgw25295 
27836423Sgw25295 static boolean_t
27846423Sgw25295 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
27856423Sgw25295 {
27866423Sgw25295 	char *type;
27876423Sgw25295 	nvlist_t **child;
27886423Sgw25295 	uint_t children, c;
27896423Sgw25295 
27906423Sgw25295 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
27916423Sgw25295 	if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
27926423Sgw25295 	    strcmp(type, VDEV_TYPE_FILE) == 0 ||
27936423Sgw25295 	    strcmp(type, VDEV_TYPE_LOG) == 0 ||
27946423Sgw25295 	    strcmp(type, VDEV_TYPE_MISSING) == 0) {
27956423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27966423Sgw25295 		    "vdev type '%s' is not supported"), type);
27976423Sgw25295 		(void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
27986423Sgw25295 		return (B_FALSE);
27996423Sgw25295 	}
28006423Sgw25295 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
28016423Sgw25295 	    &child, &children) == 0) {
28026423Sgw25295 		for (c = 0; c < children; c++) {
28036423Sgw25295 			if (!supported_dump_vdev_type(hdl, child[c], errbuf))
28046423Sgw25295 				return (B_FALSE);
28056423Sgw25295 		}
28066423Sgw25295 	}
28076423Sgw25295 	return (B_TRUE);
28086423Sgw25295 }
28096423Sgw25295 
28106423Sgw25295 /*
28116423Sgw25295  * check if this zvol is allowable for use as a dump device; zero if
28126423Sgw25295  * it is, > 0 if it isn't, < 0 if it isn't a zvol
28136423Sgw25295  */
28146423Sgw25295 int
28156423Sgw25295 zvol_check_dump_config(char *arg)
28166423Sgw25295 {
28176423Sgw25295 	zpool_handle_t *zhp = NULL;
28186423Sgw25295 	nvlist_t *config, *nvroot;
28196423Sgw25295 	char *p, *volname;
28206423Sgw25295 	nvlist_t **top;
28216423Sgw25295 	uint_t toplevels;
28226423Sgw25295 	libzfs_handle_t *hdl;
28236423Sgw25295 	char errbuf[1024];
28246423Sgw25295 	char poolname[ZPOOL_MAXNAMELEN];
28256423Sgw25295 	int pathlen = strlen(ZVOL_FULL_DEV_DIR);
28266423Sgw25295 	int ret = 1;
28276423Sgw25295 
28286423Sgw25295 	if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
28296423Sgw25295 		return (-1);
28306423Sgw25295 	}
28316423Sgw25295 
28326423Sgw25295 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
28336423Sgw25295 	    "dump is not supported on device '%s'"), arg);
28346423Sgw25295 
28356423Sgw25295 	if ((hdl = libzfs_init()) == NULL)
28366423Sgw25295 		return (1);
28376423Sgw25295 	libzfs_print_on_error(hdl, B_TRUE);
28386423Sgw25295 
28396423Sgw25295 	volname = arg + pathlen;
28406423Sgw25295 
28416423Sgw25295 	/* check the configuration of the pool */
28426423Sgw25295 	if ((p = strchr(volname, '/')) == NULL) {
28436423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28446423Sgw25295 		    "malformed dataset name"));
28456423Sgw25295 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
28466423Sgw25295 		return (1);
28476423Sgw25295 	} else if (p - volname >= ZFS_MAXNAMELEN) {
28486423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28496423Sgw25295 		    "dataset name is too long"));
28506423Sgw25295 		(void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
28516423Sgw25295 		return (1);
28526423Sgw25295 	} else {
28536423Sgw25295 		(void) strncpy(poolname, volname, p - volname);
28546423Sgw25295 		poolname[p - volname] = '\0';
28556423Sgw25295 	}
28566423Sgw25295 
28576423Sgw25295 	if ((zhp = zpool_open(hdl, poolname)) == NULL) {
28586423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28596423Sgw25295 		    "could not open pool '%s'"), poolname);
28606423Sgw25295 		(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
28616423Sgw25295 		goto out;
28626423Sgw25295 	}
28636423Sgw25295 	config = zpool_get_config(zhp, NULL);
28646423Sgw25295 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
28656423Sgw25295 	    &nvroot) != 0) {
28666423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28676423Sgw25295 		    "could not obtain vdev configuration for  '%s'"), poolname);
28686423Sgw25295 		(void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
28696423Sgw25295 		goto out;
28706423Sgw25295 	}
28716423Sgw25295 
28726423Sgw25295 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
28736423Sgw25295 	    &top, &toplevels) == 0);
28746423Sgw25295 	if (toplevels != 1) {
28756423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28766423Sgw25295 		    "'%s' has multiple top level vdevs"), poolname);
28776423Sgw25295 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
28786423Sgw25295 		goto out;
28796423Sgw25295 	}
28806423Sgw25295 
28816423Sgw25295 	if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
28826423Sgw25295 		goto out;
28836423Sgw25295 	}
28846423Sgw25295 	ret = 0;
28856423Sgw25295 
28866423Sgw25295 out:
28876423Sgw25295 	if (zhp)
28886423Sgw25295 		zpool_close(zhp);
28896423Sgw25295 	libzfs_fini(hdl);
28906423Sgw25295 	return (ret);
28916423Sgw25295 }
2892