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 
273126Sahl #include <alloca.h>
28789Sahrens #include <assert.h>
29789Sahrens #include <ctype.h>
30789Sahrens #include <errno.h>
31789Sahrens #include <devid.h>
323126Sahl #include <dirent.h>
33789Sahrens #include <fcntl.h>
34789Sahrens #include <libintl.h>
35789Sahrens #include <stdio.h>
36789Sahrens #include <stdlib.h>
373126Sahl #include <strings.h>
38789Sahrens #include <unistd.h>
397184Stimh #include <zone.h>
404276Staylor #include <sys/efi_partition.h>
414276Staylor #include <sys/vtoc.h>
42789Sahrens #include <sys/zfs_ioctl.h>
431544Seschrock #include <sys/zio.h>
442926Sek110237 #include <strings.h>
45789Sahrens 
46789Sahrens #include "zfs_namecheck.h"
473912Slling #include "zfs_prop.h"
48789Sahrens #include "libzfs_impl.h"
49789Sahrens 
507042Sgw25295 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
515094Slling 
525094Slling /*
535094Slling  * ====================================================================
545094Slling  *   zpool property functions
555094Slling  * ====================================================================
565094Slling  */
575094Slling 
585094Slling static int
595094Slling zpool_get_all_props(zpool_handle_t *zhp)
605094Slling {
615094Slling 	zfs_cmd_t zc = { 0 };
625094Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
635094Slling 
645094Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
655094Slling 
665094Slling 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
675094Slling 		return (-1);
685094Slling 
695094Slling 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
705094Slling 		if (errno == ENOMEM) {
715094Slling 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
725094Slling 				zcmd_free_nvlists(&zc);
735094Slling 				return (-1);
745094Slling 			}
755094Slling 		} else {
765094Slling 			zcmd_free_nvlists(&zc);
775094Slling 			return (-1);
785094Slling 		}
795094Slling 	}
805094Slling 
815094Slling 	if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
825094Slling 		zcmd_free_nvlists(&zc);
835094Slling 		return (-1);
845094Slling 	}
855094Slling 
865094Slling 	zcmd_free_nvlists(&zc);
875094Slling 
885094Slling 	return (0);
895094Slling }
905094Slling 
915094Slling static int
925094Slling zpool_props_refresh(zpool_handle_t *zhp)
935094Slling {
945094Slling 	nvlist_t *old_props;
955094Slling 
965094Slling 	old_props = zhp->zpool_props;
975094Slling 
985094Slling 	if (zpool_get_all_props(zhp) != 0)
995094Slling 		return (-1);
1005094Slling 
1015094Slling 	nvlist_free(old_props);
1025094Slling 	return (0);
1035094Slling }
1045094Slling 
1055094Slling static char *
1065094Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
1075094Slling     zprop_source_t *src)
1085094Slling {
1095094Slling 	nvlist_t *nv, *nvl;
1105094Slling 	uint64_t ival;
1115094Slling 	char *value;
1125094Slling 	zprop_source_t source;
1135094Slling 
1145094Slling 	nvl = zhp->zpool_props;
1155094Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
1165094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
1175094Slling 		source = ival;
1185094Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1195094Slling 	} else {
1205094Slling 		source = ZPROP_SRC_DEFAULT;
1215094Slling 		if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
1225094Slling 			value = "-";
1235094Slling 	}
1245094Slling 
1255094Slling 	if (src)
1265094Slling 		*src = source;
1275094Slling 
1285094Slling 	return (value);
1295094Slling }
1305094Slling 
1315094Slling uint64_t
1325094Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
1335094Slling {
1345094Slling 	nvlist_t *nv, *nvl;
1355094Slling 	uint64_t value;
1365094Slling 	zprop_source_t source;
1375094Slling 
1387294Sperrin 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
1397294Sperrin 		/*
1407294Sperrin 		 * zpool_get_all_props() has most likely failed because
1417294Sperrin 		 * the pool is faulted, but if all we need is the top level
1427294Sperrin 		 * vdev's guid then get it from the zhp config nvlist.
1437294Sperrin 		 */
1447294Sperrin 		if ((prop == ZPOOL_PROP_GUID) &&
1457294Sperrin 		    (nvlist_lookup_nvlist(zhp->zpool_config,
1467294Sperrin 		    ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
1477294Sperrin 		    (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
1487294Sperrin 		    == 0)) {
1497294Sperrin 			return (value);
1507294Sperrin 		}
1515094Slling 		return (zpool_prop_default_numeric(prop));
1527294Sperrin 	}
1535094Slling 
1545094Slling 	nvl = zhp->zpool_props;
1555094Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
1565094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
1575094Slling 		source = value;
1585094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1595094Slling 	} else {
1605094Slling 		source = ZPROP_SRC_DEFAULT;
1615094Slling 		value = zpool_prop_default_numeric(prop);
1625094Slling 	}
1635094Slling 
1645094Slling 	if (src)
1655094Slling 		*src = source;
1665094Slling 
1675094Slling 	return (value);
1685094Slling }
1695094Slling 
1705094Slling /*
1715094Slling  * Map VDEV STATE to printed strings.
1725094Slling  */
1735094Slling char *
1745094Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
1755094Slling {
1765094Slling 	switch (state) {
1775094Slling 	case VDEV_STATE_CLOSED:
1785094Slling 	case VDEV_STATE_OFFLINE:
1795094Slling 		return (gettext("OFFLINE"));
1805094Slling 	case VDEV_STATE_REMOVED:
1815094Slling 		return (gettext("REMOVED"));
1825094Slling 	case VDEV_STATE_CANT_OPEN:
1837294Sperrin 		if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
1845094Slling 			return (gettext("FAULTED"));
1855094Slling 		else
1865094Slling 			return (gettext("UNAVAIL"));
1875094Slling 	case VDEV_STATE_FAULTED:
1885094Slling 		return (gettext("FAULTED"));
1895094Slling 	case VDEV_STATE_DEGRADED:
1905094Slling 		return (gettext("DEGRADED"));
1915094Slling 	case VDEV_STATE_HEALTHY:
1925094Slling 		return (gettext("ONLINE"));
1935094Slling 	}
1945094Slling 
1955094Slling 	return (gettext("UNKNOWN"));
1965094Slling }
1975094Slling 
1985094Slling /*
1995094Slling  * Get a zpool property value for 'prop' and return the value in
2005094Slling  * a pre-allocated buffer.
2015094Slling  */
2025094Slling int
2035094Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
2045094Slling     zprop_source_t *srctype)
2055094Slling {
2065094Slling 	uint64_t intval;
2075094Slling 	const char *strval;
2085094Slling 	zprop_source_t src = ZPROP_SRC_NONE;
2095094Slling 	nvlist_t *nvroot;
2105094Slling 	vdev_stat_t *vs;
2115094Slling 	uint_t vsc;
2125094Slling 
2135094Slling 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
2145094Slling 		if (prop == ZPOOL_PROP_NAME)
2155094Slling 			(void) strlcpy(buf, zpool_get_name(zhp), len);
2165094Slling 		else if (prop == ZPOOL_PROP_HEALTH)
2175094Slling 			(void) strlcpy(buf, "FAULTED", len);
2185094Slling 		else
2195094Slling 			(void) strlcpy(buf, "-", len);
2205094Slling 		return (0);
2215094Slling 	}
2225094Slling 
2235094Slling 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
2245094Slling 	    prop != ZPOOL_PROP_NAME)
2255094Slling 		return (-1);
2265094Slling 
2275094Slling 	switch (zpool_prop_get_type(prop)) {
2285094Slling 	case PROP_TYPE_STRING:
2295094Slling 		(void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
2305094Slling 		    len);
2315094Slling 		break;
2325094Slling 
2335094Slling 	case PROP_TYPE_NUMBER:
2345094Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
2355094Slling 
2365094Slling 		switch (prop) {
2375094Slling 		case ZPOOL_PROP_SIZE:
2385094Slling 		case ZPOOL_PROP_USED:
2395094Slling 		case ZPOOL_PROP_AVAILABLE:
2405094Slling 			(void) zfs_nicenum(intval, buf, len);
2415094Slling 			break;
2425094Slling 
2435094Slling 		case ZPOOL_PROP_CAPACITY:
2445094Slling 			(void) snprintf(buf, len, "%llu%%",
2455094Slling 			    (u_longlong_t)intval);
2465094Slling 			break;
2475094Slling 
2485094Slling 		case ZPOOL_PROP_HEALTH:
2495094Slling 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2505094Slling 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2515094Slling 			verify(nvlist_lookup_uint64_array(nvroot,
2525094Slling 			    ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
2535094Slling 
2545094Slling 			(void) strlcpy(buf, zpool_state_to_name(intval,
2555094Slling 			    vs->vs_aux), len);
2565094Slling 			break;
2575094Slling 		default:
2585094Slling 			(void) snprintf(buf, len, "%llu", intval);
2595094Slling 		}
2605094Slling 		break;
2615094Slling 
2625094Slling 	case PROP_TYPE_INDEX:
2635094Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
2645094Slling 		if (zpool_prop_index_to_string(prop, intval, &strval)
2655094Slling 		    != 0)
2665094Slling 			return (-1);
2675094Slling 		(void) strlcpy(buf, strval, len);
2685094Slling 		break;
2695094Slling 
2705094Slling 	default:
2715094Slling 		abort();
2725094Slling 	}
2735094Slling 
2745094Slling 	if (srctype)
2755094Slling 		*srctype = src;
2765094Slling 
2775094Slling 	return (0);
2785094Slling }
2795094Slling 
2805094Slling /*
2815094Slling  * Check if the bootfs name has the same pool name as it is set to.
2825094Slling  * Assuming bootfs is a valid dataset name.
2835094Slling  */
2845094Slling static boolean_t
2855094Slling bootfs_name_valid(const char *pool, char *bootfs)
2865094Slling {
2875094Slling 	int len = strlen(pool);
2885094Slling 
289*7300SEric.Taylor@Sun.COM 	if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
2905094Slling 		return (B_FALSE);
2915094Slling 
2925094Slling 	if (strncmp(pool, bootfs, len) == 0 &&
2935094Slling 	    (bootfs[len] == '/' || bootfs[len] == '\0'))
2945094Slling 		return (B_TRUE);
2955094Slling 
2965094Slling 	return (B_FALSE);
2975094Slling }
2985094Slling 
2995094Slling /*
3007042Sgw25295  * Inspect the configuration to determine if any of the devices contain
3017042Sgw25295  * an EFI label.
3027042Sgw25295  */
3037042Sgw25295 static boolean_t
3047042Sgw25295 pool_uses_efi(nvlist_t *config)
3057042Sgw25295 {
3067042Sgw25295 	nvlist_t **child;
3077042Sgw25295 	uint_t c, children;
3087042Sgw25295 
3097042Sgw25295 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
3107042Sgw25295 	    &child, &children) != 0)
3117042Sgw25295 		return (read_efi_label(config, NULL) >= 0);
3127042Sgw25295 
3137042Sgw25295 	for (c = 0; c < children; c++) {
3147042Sgw25295 		if (pool_uses_efi(child[c]))
3157042Sgw25295 			return (B_TRUE);
3167042Sgw25295 	}
3177042Sgw25295 	return (B_FALSE);
3187042Sgw25295 }
3197042Sgw25295 
3207042Sgw25295 /*
3215094Slling  * Given an nvlist of zpool properties to be set, validate that they are
3225094Slling  * correct, and parse any numeric properties (index, boolean, etc) if they are
3235094Slling  * specified as strings.
3245094Slling  */
3255094Slling static nvlist_t *
3267184Stimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
3275094Slling     nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf)
3285094Slling {
3295094Slling 	nvpair_t *elem;
3305094Slling 	nvlist_t *retprops;
3315094Slling 	zpool_prop_t prop;
3325094Slling 	char *strval;
3335094Slling 	uint64_t intval;
3345363Seschrock 	char *slash;
3355363Seschrock 	struct stat64 statbuf;
3367042Sgw25295 	zpool_handle_t *zhp;
3377042Sgw25295 	nvlist_t *nvroot;
3385094Slling 
3395094Slling 	if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
3405094Slling 		(void) no_memory(hdl);
3415094Slling 		return (NULL);
3425094Slling 	}
3435094Slling 
3445094Slling 	elem = NULL;
3455094Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
3465094Slling 		const char *propname = nvpair_name(elem);
3475094Slling 
3485094Slling 		/*
3495094Slling 		 * Make sure this property is valid and applies to this type.
3505094Slling 		 */
3515094Slling 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
3525094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3535094Slling 			    "invalid property '%s'"), propname);
3545094Slling 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
3555094Slling 			goto error;
3565094Slling 		}
3575094Slling 
3585094Slling 		if (zpool_prop_readonly(prop)) {
3595094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
3605094Slling 			    "is readonly"), propname);
3615094Slling 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
3625094Slling 			goto error;
3635094Slling 		}
3645094Slling 
3655094Slling 		if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
3665094Slling 		    &strval, &intval, errbuf) != 0)
3675094Slling 			goto error;
3685094Slling 
3695094Slling 		/*
3705094Slling 		 * Perform additional checking for specific properties.
3715094Slling 		 */
3725094Slling 		switch (prop) {
3735094Slling 		case ZPOOL_PROP_VERSION:
3745094Slling 			if (intval < version || intval > SPA_VERSION) {
3755094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3765094Slling 				    "property '%s' number %d is invalid."),
3775094Slling 				    propname, intval);
3785094Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
3795094Slling 				goto error;
3805094Slling 			}
3815094Slling 			break;
3825094Slling 
3835094Slling 		case ZPOOL_PROP_BOOTFS:
3845094Slling 			if (create_or_import) {
3855094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3865094Slling 				    "property '%s' cannot be set at creation "
3875094Slling 				    "or import time"), propname);
3885094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
3895094Slling 				goto error;
3905094Slling 			}
3915094Slling 
3925094Slling 			if (version < SPA_VERSION_BOOTFS) {
3935094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3945094Slling 				    "pool must be upgraded to support "
3955094Slling 				    "'%s' property"), propname);
3965094Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
3975094Slling 				goto error;
3985094Slling 			}
3995094Slling 
4005094Slling 			/*
4015094Slling 			 * bootfs property value has to be a dataset name and
4025094Slling 			 * the dataset has to be in the same pool as it sets to.
4035094Slling 			 */
4045094Slling 			if (strval[0] != '\0' && !bootfs_name_valid(poolname,
4055094Slling 			    strval)) {
4065094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
4075094Slling 				    "is an invalid name"), strval);
4085094Slling 				(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4095094Slling 				goto error;
4105094Slling 			}
4117042Sgw25295 
4127042Sgw25295 			if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
4137042Sgw25295 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4147042Sgw25295 				    "could not open pool '%s'"), poolname);
4157042Sgw25295 				(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
4167042Sgw25295 				goto error;
4177042Sgw25295 			}
4187042Sgw25295 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
4197042Sgw25295 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
4207042Sgw25295 
4217042Sgw25295 			/*
4227042Sgw25295 			 * bootfs property cannot be set on a disk which has
4237042Sgw25295 			 * been EFI labeled.
4247042Sgw25295 			 */
4257042Sgw25295 			if (pool_uses_efi(nvroot)) {
4267042Sgw25295 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4277042Sgw25295 				    "property '%s' not supported on "
4287042Sgw25295 				    "EFI labeled devices"), propname);
4297042Sgw25295 				(void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
4307042Sgw25295 				zpool_close(zhp);
4317042Sgw25295 				goto error;
4327042Sgw25295 			}
4337042Sgw25295 			zpool_close(zhp);
4345094Slling 			break;
4355094Slling 
4365094Slling 		case ZPOOL_PROP_ALTROOT:
4375094Slling 			if (!create_or_import) {
4385094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4395094Slling 				    "property '%s' can only be set during pool "
4405094Slling 				    "creation or import"), propname);
4415094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
4425094Slling 				goto error;
4435094Slling 			}
4445094Slling 
4455094Slling 			if (strval[0] != '/') {
4465094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4475094Slling 				    "bad alternate root '%s'"), strval);
4485094Slling 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
4495094Slling 				goto error;
4505094Slling 			}
4515094Slling 			break;
4525363Seschrock 
4535363Seschrock 		case ZPOOL_PROP_CACHEFILE:
4545363Seschrock 			if (strval[0] == '\0')
4555363Seschrock 				break;
4565363Seschrock 
4575363Seschrock 			if (strcmp(strval, "none") == 0)
4585363Seschrock 				break;
4595363Seschrock 
4605363Seschrock 			if (strval[0] != '/') {
4615363Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4625363Seschrock 				    "property '%s' must be empty, an "
4635363Seschrock 				    "absolute path, or 'none'"), propname);
4645363Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
4655363Seschrock 				goto error;
4665363Seschrock 			}
4675363Seschrock 
4685363Seschrock 			slash = strrchr(strval, '/');
4695363Seschrock 
4705363Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
4715363Seschrock 			    strcmp(slash, "/..") == 0) {
4725363Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4735363Seschrock 				    "'%s' is not a valid file"), strval);
4745363Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
4755363Seschrock 				goto error;
4765363Seschrock 			}
4775363Seschrock 
4785363Seschrock 			*slash = '\0';
4795363Seschrock 
4805621Seschrock 			if (strval[0] != '\0' &&
4815621Seschrock 			    (stat64(strval, &statbuf) != 0 ||
4825621Seschrock 			    !S_ISDIR(statbuf.st_mode))) {
4835363Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4845363Seschrock 				    "'%s' is not a valid directory"),
4855363Seschrock 				    strval);
4865363Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
4875363Seschrock 				goto error;
4885363Seschrock 			}
4895363Seschrock 
4905363Seschrock 			*slash = '/';
4915363Seschrock 			break;
4925094Slling 		}
4935094Slling 	}
4945094Slling 
4955094Slling 	return (retprops);
4965094Slling error:
4975094Slling 	nvlist_free(retprops);
4985094Slling 	return (NULL);
4995094Slling }
5005094Slling 
5015094Slling /*
5025094Slling  * Set zpool property : propname=propval.
5035094Slling  */
5045094Slling int
5055094Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
5065094Slling {
5075094Slling 	zfs_cmd_t zc = { 0 };
5085094Slling 	int ret = -1;
5095094Slling 	char errbuf[1024];
5105094Slling 	nvlist_t *nvl = NULL;
5115094Slling 	nvlist_t *realprops;
5125094Slling 	uint64_t version;
5135094Slling 
5145094Slling 	(void) snprintf(errbuf, sizeof (errbuf),
5155094Slling 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
5165094Slling 	    zhp->zpool_name);
5175094Slling 
5185094Slling 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp))
5195094Slling 		return (zfs_error(zhp->zpool_hdl, EZFS_POOLPROPS, errbuf));
5205094Slling 
5215094Slling 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5225094Slling 		return (no_memory(zhp->zpool_hdl));
5235094Slling 
5245094Slling 	if (nvlist_add_string(nvl, propname, propval) != 0) {
5255094Slling 		nvlist_free(nvl);
5265094Slling 		return (no_memory(zhp->zpool_hdl));
5275094Slling 	}
5285094Slling 
5295094Slling 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
5307184Stimh 	if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
5315094Slling 	    zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) {
5325094Slling 		nvlist_free(nvl);
5335094Slling 		return (-1);
5345094Slling 	}
5355094Slling 
5365094Slling 	nvlist_free(nvl);
5375094Slling 	nvl = realprops;
5385094Slling 
5395094Slling 	/*
5405094Slling 	 * Execute the corresponding ioctl() to set this property.
5415094Slling 	 */
5425094Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
5435094Slling 
5445094Slling 	if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
5455094Slling 		nvlist_free(nvl);
5465094Slling 		return (-1);
5475094Slling 	}
5485094Slling 
5495094Slling 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
5505094Slling 
5515094Slling 	zcmd_free_nvlists(&zc);
5525094Slling 	nvlist_free(nvl);
5535094Slling 
5545094Slling 	if (ret)
5555094Slling 		(void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
5565094Slling 	else
5575094Slling 		(void) zpool_props_refresh(zhp);
5585094Slling 
5595094Slling 	return (ret);
5605094Slling }
5615094Slling 
5625094Slling int
5635094Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
5645094Slling {
5655094Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
5665094Slling 	zprop_list_t *entry;
5675094Slling 	char buf[ZFS_MAXPROPLEN];
5685094Slling 
5695094Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
5705094Slling 		return (-1);
5715094Slling 
5725094Slling 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
5735094Slling 
5745094Slling 		if (entry->pl_fixed)
5755094Slling 			continue;
5765094Slling 
5775094Slling 		if (entry->pl_prop != ZPROP_INVAL &&
5785094Slling 		    zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
5795094Slling 		    NULL) == 0) {
5805094Slling 			if (strlen(buf) > entry->pl_width)
5815094Slling 				entry->pl_width = strlen(buf);
5825094Slling 		}
5835094Slling 	}
5845094Slling 
5855094Slling 	return (0);
5865094Slling }
5875094Slling 
5885094Slling 
589789Sahrens /*
590789Sahrens  * Validate the given pool name, optionally putting an extended error message in
591789Sahrens  * 'buf'.
592789Sahrens  */
5936423Sgw25295 boolean_t
5942082Seschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
595789Sahrens {
596789Sahrens 	namecheck_err_t why;
597789Sahrens 	char what;
5981773Seschrock 	int ret;
599789Sahrens 
6001773Seschrock 	ret = pool_namecheck(pool, &why, &what);
6011773Seschrock 
6021773Seschrock 	/*
6031773Seschrock 	 * The rules for reserved pool names were extended at a later point.
6041773Seschrock 	 * But we need to support users with existing pools that may now be
6051773Seschrock 	 * invalid.  So we only check for this expanded set of names during a
6061773Seschrock 	 * create (or import), and only in userland.
6071773Seschrock 	 */
6081773Seschrock 	if (ret == 0 && !isopen &&
6091773Seschrock 	    (strncmp(pool, "mirror", 6) == 0 ||
6101773Seschrock 	    strncmp(pool, "raidz", 5) == 0 ||
6114527Sperrin 	    strncmp(pool, "spare", 5) == 0 ||
6124527Sperrin 	    strcmp(pool, "log") == 0)) {
6136423Sgw25295 		if (hdl != NULL)
6146423Sgw25295 			zfs_error_aux(hdl,
6156423Sgw25295 			    dgettext(TEXT_DOMAIN, "name is reserved"));
6162082Seschrock 		return (B_FALSE);
6171773Seschrock 	}
6181773Seschrock 
6191773Seschrock 
6201773Seschrock 	if (ret != 0) {
6212082Seschrock 		if (hdl != NULL) {
622789Sahrens 			switch (why) {
6231003Slling 			case NAME_ERR_TOOLONG:
6242082Seschrock 				zfs_error_aux(hdl,
6251003Slling 				    dgettext(TEXT_DOMAIN, "name is too long"));
6261003Slling 				break;
6271003Slling 
628789Sahrens 			case NAME_ERR_INVALCHAR:
6292082Seschrock 				zfs_error_aux(hdl,
630789Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
631789Sahrens 				    "'%c' in pool name"), what);
632789Sahrens 				break;
633789Sahrens 
634789Sahrens 			case NAME_ERR_NOLETTER:
6352082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6362082Seschrock 				    "name must begin with a letter"));
637789Sahrens 				break;
638789Sahrens 
639789Sahrens 			case NAME_ERR_RESERVED:
6402082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6412082Seschrock 				    "name is reserved"));
642789Sahrens 				break;
643789Sahrens 
644789Sahrens 			case NAME_ERR_DISKLIKE:
6452082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6462082Seschrock 				    "pool name is reserved"));
647789Sahrens 				break;
6482856Snd150628 
6492856Snd150628 			case NAME_ERR_LEADING_SLASH:
6502856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6512856Snd150628 				    "leading slash in name"));
6522856Snd150628 				break;
6532856Snd150628 
6542856Snd150628 			case NAME_ERR_EMPTY_COMPONENT:
6552856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6562856Snd150628 				    "empty component in name"));
6572856Snd150628 				break;
6582856Snd150628 
6592856Snd150628 			case NAME_ERR_TRAILING_SLASH:
6602856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6612856Snd150628 				    "trailing slash in name"));
6622856Snd150628 				break;
6632856Snd150628 
6642856Snd150628 			case NAME_ERR_MULTIPLE_AT:
6652856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6662856Snd150628 				    "multiple '@' delimiters in name"));
6672856Snd150628 				break;
6682856Snd150628 
669789Sahrens 			}
670789Sahrens 		}
6712082Seschrock 		return (B_FALSE);
672789Sahrens 	}
673789Sahrens 
6742082Seschrock 	return (B_TRUE);
675789Sahrens }
676789Sahrens 
677789Sahrens /*
678789Sahrens  * Open a handle to the given pool, even if the pool is currently in the FAULTED
679789Sahrens  * state.
680789Sahrens  */
681789Sahrens zpool_handle_t *
6822082Seschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
683789Sahrens {
684789Sahrens 	zpool_handle_t *zhp;
6852142Seschrock 	boolean_t missing;
686789Sahrens 
687789Sahrens 	/*
688789Sahrens 	 * Make sure the pool name is valid.
689789Sahrens 	 */
6902082Seschrock 	if (!zpool_name_valid(hdl, B_TRUE, pool)) {
6913237Slling 		(void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
6922082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
6932082Seschrock 		    pool);
694789Sahrens 		return (NULL);
695789Sahrens 	}
696789Sahrens 
6972082Seschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
6982082Seschrock 		return (NULL);
699789Sahrens 
7002082Seschrock 	zhp->zpool_hdl = hdl;
701789Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
702789Sahrens 
7032142Seschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
7042142Seschrock 		zpool_close(zhp);
7052142Seschrock 		return (NULL);
7062142Seschrock 	}
7072142Seschrock 
7082142Seschrock 	if (missing) {
7095094Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
7103237Slling 		(void) zfs_error_fmt(hdl, EZFS_NOENT,
7115094Slling 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
7122142Seschrock 		zpool_close(zhp);
7132142Seschrock 		return (NULL);
714789Sahrens 	}
715789Sahrens 
716789Sahrens 	return (zhp);
717789Sahrens }
718789Sahrens 
719789Sahrens /*
720789Sahrens  * Like the above, but silent on error.  Used when iterating over pools (because
721789Sahrens  * the configuration cache may be out of date).
722789Sahrens  */
7232142Seschrock int
7242142Seschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
725789Sahrens {
726789Sahrens 	zpool_handle_t *zhp;
7272142Seschrock 	boolean_t missing;
728789Sahrens 
7292142Seschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
7302142Seschrock 		return (-1);
731789Sahrens 
7322082Seschrock 	zhp->zpool_hdl = hdl;
733789Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
734789Sahrens 
7352142Seschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
7362142Seschrock 		zpool_close(zhp);
7372142Seschrock 		return (-1);
738789Sahrens 	}
739789Sahrens 
7402142Seschrock 	if (missing) {
7412142Seschrock 		zpool_close(zhp);
7422142Seschrock 		*ret = NULL;
7432142Seschrock 		return (0);
7442142Seschrock 	}
7452142Seschrock 
7462142Seschrock 	*ret = zhp;
7472142Seschrock 	return (0);
748789Sahrens }
749789Sahrens 
750789Sahrens /*
751789Sahrens  * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
752789Sahrens  * state.
753789Sahrens  */
754789Sahrens zpool_handle_t *
7552082Seschrock zpool_open(libzfs_handle_t *hdl, const char *pool)
756789Sahrens {
757789Sahrens 	zpool_handle_t *zhp;
758789Sahrens 
7592082Seschrock 	if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
760789Sahrens 		return (NULL);
761789Sahrens 
762789Sahrens 	if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
7633237Slling 		(void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
7642082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
765789Sahrens 		zpool_close(zhp);
766789Sahrens 		return (NULL);
767789Sahrens 	}
768789Sahrens 
769789Sahrens 	return (zhp);
770789Sahrens }
771789Sahrens 
772789Sahrens /*
773789Sahrens  * Close the handle.  Simply frees the memory associated with the handle.
774789Sahrens  */
775789Sahrens void
776789Sahrens zpool_close(zpool_handle_t *zhp)
777789Sahrens {
778789Sahrens 	if (zhp->zpool_config)
779789Sahrens 		nvlist_free(zhp->zpool_config);
780952Seschrock 	if (zhp->zpool_old_config)
781952Seschrock 		nvlist_free(zhp->zpool_old_config);
7823912Slling 	if (zhp->zpool_props)
7833912Slling 		nvlist_free(zhp->zpool_props);
784789Sahrens 	free(zhp);
785789Sahrens }
786789Sahrens 
787789Sahrens /*
788789Sahrens  * Return the name of the pool.
789789Sahrens  */
790789Sahrens const char *
791789Sahrens zpool_get_name(zpool_handle_t *zhp)
792789Sahrens {
793789Sahrens 	return (zhp->zpool_name);
794789Sahrens }
795789Sahrens 
796789Sahrens 
797789Sahrens /*
798789Sahrens  * Return the state of the pool (ACTIVE or UNAVAILABLE)
799789Sahrens  */
800789Sahrens int
801789Sahrens zpool_get_state(zpool_handle_t *zhp)
802789Sahrens {
803789Sahrens 	return (zhp->zpool_state);
804789Sahrens }
805789Sahrens 
806789Sahrens /*
807789Sahrens  * Create the named pool, using the provided vdev list.  It is assumed
808789Sahrens  * that the consumer has already validated the contents of the nvlist, so we
809789Sahrens  * don't have to worry about error semantics.
810789Sahrens  */
811789Sahrens int
8122082Seschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
8137184Stimh     nvlist_t *props, nvlist_t *fsprops)
814789Sahrens {
815789Sahrens 	zfs_cmd_t zc = { 0 };
8167184Stimh 	nvlist_t *zc_fsprops = NULL;
8177184Stimh 	nvlist_t *zc_props = NULL;
8182082Seschrock 	char msg[1024];
8195094Slling 	char *altroot;
8207184Stimh 	int ret = -1;
8212082Seschrock 
8222082Seschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
8232082Seschrock 	    "cannot create '%s'"), pool);
824789Sahrens 
8252082Seschrock 	if (!zpool_name_valid(hdl, B_FALSE, pool))
8262082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
8272082Seschrock 
8285320Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
8295320Slling 		return (-1);
8305320Slling 
8317184Stimh 	if (props) {
8327184Stimh 		if ((zc_props = zpool_valid_proplist(hdl, pool, props,
8337184Stimh 		    SPA_VERSION_1, B_TRUE, msg)) == NULL) {
8347184Stimh 			goto create_failed;
8357184Stimh 		}
8365320Slling 	}
837789Sahrens 
8387184Stimh 	if (fsprops) {
8397184Stimh 		uint64_t zoned;
8407184Stimh 		char *zonestr;
8417184Stimh 
8427184Stimh 		zoned = ((nvlist_lookup_string(fsprops,
8437184Stimh 		    zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
8447184Stimh 		    strcmp(zonestr, "on") == 0);
8457184Stimh 
8467184Stimh 		if ((zc_fsprops = zfs_valid_proplist(hdl,
8477184Stimh 		    ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
8487184Stimh 			goto create_failed;
8497184Stimh 		}
8507184Stimh 		if (!zc_props &&
8517184Stimh 		    (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
8527184Stimh 			goto create_failed;
8537184Stimh 		}
8547184Stimh 		if (nvlist_add_nvlist(zc_props,
8557184Stimh 		    ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
8567184Stimh 			goto create_failed;
8577184Stimh 		}
8587184Stimh 	}
8597184Stimh 
8607184Stimh 	if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
8617184Stimh 		goto create_failed;
8627184Stimh 
863789Sahrens 	(void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
864789Sahrens 
8657184Stimh 	if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
8665320Slling 
8672676Seschrock 		zcmd_free_nvlists(&zc);
8687184Stimh 		nvlist_free(zc_props);
8697184Stimh 		nvlist_free(zc_fsprops);
8702082Seschrock 
871789Sahrens 		switch (errno) {
872789Sahrens 		case EBUSY:
873789Sahrens 			/*
874789Sahrens 			 * This can happen if the user has specified the same
875789Sahrens 			 * device multiple times.  We can't reliably detect this
876789Sahrens 			 * until we try to add it and see we already have a
877789Sahrens 			 * label.
878789Sahrens 			 */
8792082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8802082Seschrock 			    "one or more vdevs refer to the same device"));
8812082Seschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
882789Sahrens 
883789Sahrens 		case EOVERFLOW:
884789Sahrens 			/*
8852082Seschrock 			 * This occurs when one of the devices is below
886789Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
887789Sahrens 			 * device was the problem device since there's no
888789Sahrens 			 * reliable way to determine device size from userland.
889789Sahrens 			 */
890789Sahrens 			{
891789Sahrens 				char buf[64];
892789Sahrens 
893789Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
894789Sahrens 
8952082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8962082Seschrock 				    "one or more devices is less than the "
8972082Seschrock 				    "minimum size (%s)"), buf);
898789Sahrens 			}
8992082Seschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
900789Sahrens 
901789Sahrens 		case ENOSPC:
9022082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9032082Seschrock 			    "one or more devices is out of space"));
9042082Seschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
905789Sahrens 
9065450Sbrendan 		case ENOTBLK:
9075450Sbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9085450Sbrendan 			    "cache device must be a disk or disk slice"));
9095450Sbrendan 			return (zfs_error(hdl, EZFS_BADDEV, msg));
9105450Sbrendan 
911789Sahrens 		default:
9122082Seschrock 			return (zpool_standard_error(hdl, errno, msg));
913789Sahrens 		}
914789Sahrens 	}
915789Sahrens 
916789Sahrens 	/*
917789Sahrens 	 * If this is an alternate root pool, then we automatically set the
9182676Seschrock 	 * mountpoint of the root dataset to be '/'.
919789Sahrens 	 */
9205094Slling 	if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
9215094Slling 	    &altroot) == 0) {
922789Sahrens 		zfs_handle_t *zhp;
923789Sahrens 
9245094Slling 		verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
9252676Seschrock 		verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
9262676Seschrock 		    "/") == 0);
927789Sahrens 
928789Sahrens 		zfs_close(zhp);
929789Sahrens 	}
930789Sahrens 
9317184Stimh create_failed:
9325320Slling 	zcmd_free_nvlists(&zc);
9337184Stimh 	nvlist_free(zc_props);
9347184Stimh 	nvlist_free(zc_fsprops);
9357184Stimh 	return (ret);
936789Sahrens }
937789Sahrens 
938789Sahrens /*
939789Sahrens  * Destroy the given pool.  It is up to the caller to ensure that there are no
940789Sahrens  * datasets left in the pool.
941789Sahrens  */
942789Sahrens int
943789Sahrens zpool_destroy(zpool_handle_t *zhp)
944789Sahrens {
945789Sahrens 	zfs_cmd_t zc = { 0 };
946789Sahrens 	zfs_handle_t *zfp = NULL;
9472082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
9482082Seschrock 	char msg[1024];
949789Sahrens 
950789Sahrens 	if (zhp->zpool_state == POOL_STATE_ACTIVE &&
9512082Seschrock 	    (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name,
9522082Seschrock 	    ZFS_TYPE_FILESYSTEM)) == NULL)
953789Sahrens 		return (-1);
954789Sahrens 
9552856Snd150628 	if (zpool_remove_zvol_links(zhp) != 0)
956789Sahrens 		return (-1);
957789Sahrens 
958789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
959789Sahrens 
9604543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
9612082Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
9622082Seschrock 		    "cannot destroy '%s'"), zhp->zpool_name);
963789Sahrens 
9642082Seschrock 		if (errno == EROFS) {
9652082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9662082Seschrock 			    "one or more devices is read only"));
9672082Seschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
9682082Seschrock 		} else {
9692082Seschrock 			(void) zpool_standard_error(hdl, errno, msg);
970789Sahrens 		}
971789Sahrens 
972789Sahrens 		if (zfp)
973789Sahrens 			zfs_close(zfp);
974789Sahrens 		return (-1);
975789Sahrens 	}
976789Sahrens 
977789Sahrens 	if (zfp) {
978789Sahrens 		remove_mountpoint(zfp);
979789Sahrens 		zfs_close(zfp);
980789Sahrens 	}
981789Sahrens 
982789Sahrens 	return (0);
983789Sahrens }
984789Sahrens 
985789Sahrens /*
986789Sahrens  * Add the given vdevs to the pool.  The caller must have already performed the
987789Sahrens  * necessary verification to ensure that the vdev specification is well-formed.
988789Sahrens  */
989789Sahrens int
990789Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
991789Sahrens {
9922676Seschrock 	zfs_cmd_t zc = { 0 };
9932082Seschrock 	int ret;
9942082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
9952082Seschrock 	char msg[1024];
9965450Sbrendan 	nvlist_t **spares, **l2cache;
9975450Sbrendan 	uint_t nspares, nl2cache;
9982082Seschrock 
9992082Seschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
10002082Seschrock 	    "cannot add to '%s'"), zhp->zpool_name);
10012082Seschrock 
10025450Sbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
10035450Sbrendan 	    SPA_VERSION_SPARES &&
10042082Seschrock 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
10052082Seschrock 	    &spares, &nspares) == 0) {
10062082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
10072082Seschrock 		    "upgraded to add hot spares"));
10082082Seschrock 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
10092082Seschrock 	}
1010789Sahrens 
10115450Sbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
10125450Sbrendan 	    SPA_VERSION_L2CACHE &&
10135450Sbrendan 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
10145450Sbrendan 	    &l2cache, &nl2cache) == 0) {
10155450Sbrendan 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
10165450Sbrendan 		    "upgraded to add cache devices"));
10175450Sbrendan 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
10185450Sbrendan 	}
10195450Sbrendan 
10205094Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
10212082Seschrock 		return (-1);
1022789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1023789Sahrens 
10244543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1025789Sahrens 		switch (errno) {
1026789Sahrens 		case EBUSY:
1027789Sahrens 			/*
1028789Sahrens 			 * This can happen if the user has specified the same
1029789Sahrens 			 * device multiple times.  We can't reliably detect this
1030789Sahrens 			 * until we try to add it and see we already have a
1031789Sahrens 			 * label.
1032789Sahrens 			 */
10332082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10342082Seschrock 			    "one or more vdevs refer to the same device"));
10352082Seschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1036789Sahrens 			break;
1037789Sahrens 
1038789Sahrens 		case EOVERFLOW:
1039789Sahrens 			/*
1040789Sahrens 			 * This occurrs when one of the devices is below
1041789Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1042789Sahrens 			 * device was the problem device since there's no
1043789Sahrens 			 * reliable way to determine device size from userland.
1044789Sahrens 			 */
1045789Sahrens 			{
1046789Sahrens 				char buf[64];
1047789Sahrens 
1048789Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1049789Sahrens 
10502082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10512082Seschrock 				    "device is less than the minimum "
10522082Seschrock 				    "size (%s)"), buf);
1053789Sahrens 			}
10542082Seschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
10552082Seschrock 			break;
10562082Seschrock 
10572082Seschrock 		case ENOTSUP:
10582082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10594527Sperrin 			    "pool must be upgraded to add these vdevs"));
10602082Seschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, msg);
1061789Sahrens 			break;
1062789Sahrens 
10633912Slling 		case EDOM:
10643912Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10654527Sperrin 			    "root pool can not have multiple vdevs"
10664527Sperrin 			    " or separate logs"));
10673912Slling 			(void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
10683912Slling 			break;
10693912Slling 
10705450Sbrendan 		case ENOTBLK:
10715450Sbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10725450Sbrendan 			    "cache device must be a disk or disk slice"));
10735450Sbrendan 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
10745450Sbrendan 			break;
10755450Sbrendan 
1076789Sahrens 		default:
10772082Seschrock 			(void) zpool_standard_error(hdl, errno, msg);
1078789Sahrens 		}
1079789Sahrens 
10802082Seschrock 		ret = -1;
10812082Seschrock 	} else {
10822082Seschrock 		ret = 0;
1083789Sahrens 	}
1084789Sahrens 
10852676Seschrock 	zcmd_free_nvlists(&zc);
1086789Sahrens 
10872082Seschrock 	return (ret);
1088789Sahrens }
1089789Sahrens 
1090789Sahrens /*
1091789Sahrens  * Exports the pool from the system.  The caller must ensure that there are no
1092789Sahrens  * mounted datasets in the pool.
1093789Sahrens  */
1094789Sahrens int
10957214Slling zpool_export(zpool_handle_t *zhp, boolean_t force)
1096789Sahrens {
1097789Sahrens 	zfs_cmd_t zc = { 0 };
10987214Slling 	char msg[1024];
1099789Sahrens 
1100789Sahrens 	if (zpool_remove_zvol_links(zhp) != 0)
1101789Sahrens 		return (-1);
1102789Sahrens 
11037214Slling 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
11047214Slling 	    "cannot export '%s'"), zhp->zpool_name);
11057214Slling 
1106789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
11077214Slling 	zc.zc_cookie = force;
11087214Slling 
11097214Slling 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
11107214Slling 		switch (errno) {
11117214Slling 		case EXDEV:
11127214Slling 			zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
11137214Slling 			    "use '-f' to override the following errors:\n"
11147214Slling 			    "'%s' has an active shared spare which could be"
11157214Slling 			    " used by other pools once '%s' is exported."),
11167214Slling 			    zhp->zpool_name, zhp->zpool_name);
11177214Slling 			return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
11187214Slling 			    msg));
11197214Slling 		default:
11207214Slling 			return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
11217214Slling 			    msg));
11227214Slling 		}
11237214Slling 	}
11247214Slling 
1125789Sahrens 	return (0);
1126789Sahrens }
1127789Sahrens 
1128789Sahrens /*
11295094Slling  * zpool_import() is a contracted interface. Should be kept the same
11305094Slling  * if possible.
11315094Slling  *
11325094Slling  * Applications should use zpool_import_props() to import a pool with
11335094Slling  * new properties value to be set.
1134789Sahrens  */
1135789Sahrens int
11362082Seschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
11375094Slling     char *altroot)
11385094Slling {
11395094Slling 	nvlist_t *props = NULL;
11405094Slling 	int ret;
11415094Slling 
11425094Slling 	if (altroot != NULL) {
11435094Slling 		if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
11445094Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
11455094Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
11465094Slling 			    newname));
11475094Slling 		}
11485094Slling 
11495094Slling 		if (nvlist_add_string(props,
11505094Slling 		    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0) {
11515094Slling 			nvlist_free(props);
11525094Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
11535094Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
11545094Slling 			    newname));
11555094Slling 		}
11565094Slling 	}
11575094Slling 
11586643Seschrock 	ret = zpool_import_props(hdl, config, newname, props, B_FALSE);
11595094Slling 	if (props)
11605094Slling 		nvlist_free(props);
11615094Slling 	return (ret);
11625094Slling }
11635094Slling 
11645094Slling /*
11655094Slling  * Import the given pool using the known configuration and a list of
11665094Slling  * properties to be set. The configuration should have come from
11675094Slling  * zpool_find_import(). The 'newname' parameters control whether the pool
11685094Slling  * is imported with a different name.
11695094Slling  */
11705094Slling int
11715094Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
11726643Seschrock     nvlist_t *props, boolean_t importfaulted)
1173789Sahrens {
11742676Seschrock 	zfs_cmd_t zc = { 0 };
1175789Sahrens 	char *thename;
1176789Sahrens 	char *origname;
1177789Sahrens 	int ret;
11785094Slling 	char errbuf[1024];
1179789Sahrens 
1180789Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1181789Sahrens 	    &origname) == 0);
1182789Sahrens 
11835094Slling 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
11845094Slling 	    "cannot import pool '%s'"), origname);
11855094Slling 
1186789Sahrens 	if (newname != NULL) {
11872082Seschrock 		if (!zpool_name_valid(hdl, B_FALSE, newname))
11883237Slling 			return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
11892082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
11902082Seschrock 			    newname));
1191789Sahrens 		thename = (char *)newname;
1192789Sahrens 	} else {
1193789Sahrens 		thename = origname;
1194789Sahrens 	}
1195789Sahrens 
11965094Slling 	if (props) {
11975094Slling 		uint64_t version;
11985094Slling 
11995094Slling 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
12005094Slling 		    &version) == 0);
12015094Slling 
12027184Stimh 		if ((props = zpool_valid_proplist(hdl, origname,
12035320Slling 		    props, version, B_TRUE, errbuf)) == NULL) {
12045094Slling 			return (-1);
12055320Slling 		} else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
12065320Slling 			nvlist_free(props);
12075094Slling 			return (-1);
12085320Slling 		}
12095094Slling 	}
1210789Sahrens 
1211789Sahrens 	(void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1212789Sahrens 
1213789Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
12141544Seschrock 	    &zc.zc_guid) == 0);
1215789Sahrens 
12165320Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
12175320Slling 		nvlist_free(props);
12182082Seschrock 		return (-1);
12195320Slling 	}
1220789Sahrens 
12216643Seschrock 	zc.zc_cookie = (uint64_t)importfaulted;
1222789Sahrens 	ret = 0;
12234543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) {
1224789Sahrens 		char desc[1024];
1225789Sahrens 		if (newname == NULL)
1226789Sahrens 			(void) snprintf(desc, sizeof (desc),
1227789Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1228789Sahrens 			    thename);
1229789Sahrens 		else
1230789Sahrens 			(void) snprintf(desc, sizeof (desc),
1231789Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1232789Sahrens 			    origname, thename);
1233789Sahrens 
1234789Sahrens 		switch (errno) {
12351544Seschrock 		case ENOTSUP:
12361544Seschrock 			/*
12371544Seschrock 			 * Unsupported version.
12381544Seschrock 			 */
12392082Seschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, desc);
12401544Seschrock 			break;
12411544Seschrock 
12422174Seschrock 		case EINVAL:
12432174Seschrock 			(void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
12442174Seschrock 			break;
12452174Seschrock 
1246789Sahrens 		default:
12472082Seschrock 			(void) zpool_standard_error(hdl, errno, desc);
1248789Sahrens 		}
1249789Sahrens 
1250789Sahrens 		ret = -1;
1251789Sahrens 	} else {
1252789Sahrens 		zpool_handle_t *zhp;
12534543Smarks 
1254789Sahrens 		/*
1255789Sahrens 		 * This should never fail, but play it safe anyway.
1256789Sahrens 		 */
12572142Seschrock 		if (zpool_open_silent(hdl, thename, &zhp) != 0) {
12582142Seschrock 			ret = -1;
12592142Seschrock 		} else if (zhp != NULL) {
1260789Sahrens 			ret = zpool_create_zvol_links(zhp);
1261789Sahrens 			zpool_close(zhp);
1262789Sahrens 		}
12634543Smarks 
1264789Sahrens 	}
1265789Sahrens 
12662676Seschrock 	zcmd_free_nvlists(&zc);
12675320Slling 	nvlist_free(props);
12685320Slling 
1269789Sahrens 	return (ret);
1270789Sahrens }
1271789Sahrens 
1272789Sahrens /*
1273789Sahrens  * Scrub the pool.
1274789Sahrens  */
1275789Sahrens int
1276789Sahrens zpool_scrub(zpool_handle_t *zhp, pool_scrub_type_t type)
1277789Sahrens {
1278789Sahrens 	zfs_cmd_t zc = { 0 };
1279789Sahrens 	char msg[1024];
12802082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1281789Sahrens 
1282789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1283789Sahrens 	zc.zc_cookie = type;
1284789Sahrens 
12854543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCRUB, &zc) == 0)
1286789Sahrens 		return (0);
1287789Sahrens 
1288789Sahrens 	(void) snprintf(msg, sizeof (msg),
1289789Sahrens 	    dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1290789Sahrens 
12912082Seschrock 	if (errno == EBUSY)
12922082Seschrock 		return (zfs_error(hdl, EZFS_RESILVERING, msg));
12932082Seschrock 	else
12942082Seschrock 		return (zpool_standard_error(hdl, errno, msg));
1295789Sahrens }
1296789Sahrens 
12972468Sek110237 /*
12982468Sek110237  * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
12992468Sek110237  * spare; but FALSE if its an INUSE spare.
13002468Sek110237  */
13012082Seschrock static nvlist_t *
13022082Seschrock vdev_to_nvlist_iter(nvlist_t *nv, const char *search, uint64_t guid,
13035450Sbrendan     boolean_t *avail_spare, boolean_t *l2cache)
13041544Seschrock {
13051544Seschrock 	uint_t c, children;
13061544Seschrock 	nvlist_t **child;
13072082Seschrock 	uint64_t theguid, present;
13081544Seschrock 	char *path;
13091544Seschrock 	uint64_t wholedisk = 0;
13102082Seschrock 	nvlist_t *ret;
13111544Seschrock 
13122082Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &theguid) == 0);
13131544Seschrock 
13141544Seschrock 	if (search == NULL &&
13151544Seschrock 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &present) == 0) {
13161544Seschrock 		/*
13171544Seschrock 		 * If the device has never been present since import, the only
13181544Seschrock 		 * reliable way to match the vdev is by GUID.
13191544Seschrock 		 */
13202082Seschrock 		if (theguid == guid)
13212082Seschrock 			return (nv);
13221544Seschrock 	} else if (search != NULL &&
13231544Seschrock 	    nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
13241544Seschrock 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
13251544Seschrock 		    &wholedisk);
13261544Seschrock 		if (wholedisk) {
13271544Seschrock 			/*
13281544Seschrock 			 * For whole disks, the internal path has 's0', but the
13291544Seschrock 			 * path passed in by the user doesn't.
13301544Seschrock 			 */
13311544Seschrock 			if (strlen(search) == strlen(path) - 2 &&
13321544Seschrock 			    strncmp(search, path, strlen(search)) == 0)
13332082Seschrock 				return (nv);
13341544Seschrock 		} else if (strcmp(search, path) == 0) {
13352082Seschrock 			return (nv);
13361544Seschrock 		}
13371544Seschrock 	}
13381544Seschrock 
13391544Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
13401544Seschrock 	    &child, &children) != 0)
13412082Seschrock 		return (NULL);
13421544Seschrock 
13431544Seschrock 	for (c = 0; c < children; c++)
13442082Seschrock 		if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
13455450Sbrendan 		    avail_spare, l2cache)) != NULL)
13461544Seschrock 			return (ret);
13471544Seschrock 
13482082Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
13492082Seschrock 	    &child, &children) == 0) {
13502082Seschrock 		for (c = 0; c < children; c++) {
13512082Seschrock 			if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
13525450Sbrendan 			    avail_spare, l2cache)) != NULL) {
13532468Sek110237 				*avail_spare = B_TRUE;
13542082Seschrock 				return (ret);
13552082Seschrock 			}
13562082Seschrock 		}
13572082Seschrock 	}
13582082Seschrock 
13595450Sbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
13605450Sbrendan 	    &child, &children) == 0) {
13615450Sbrendan 		for (c = 0; c < children; c++) {
13625450Sbrendan 			if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
13635450Sbrendan 			    avail_spare, l2cache)) != NULL) {
13645450Sbrendan 				*l2cache = B_TRUE;
13655450Sbrendan 				return (ret);
13665450Sbrendan 			}
13675450Sbrendan 		}
13685450Sbrendan 	}
13695450Sbrendan 
13702082Seschrock 	return (NULL);
13711544Seschrock }
13721544Seschrock 
13732082Seschrock nvlist_t *
13745450Sbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
13755450Sbrendan     boolean_t *l2cache)
13761544Seschrock {
13771544Seschrock 	char buf[MAXPATHLEN];
13781544Seschrock 	const char *search;
13791544Seschrock 	char *end;
13801544Seschrock 	nvlist_t *nvroot;
13811544Seschrock 	uint64_t guid;
13821544Seschrock 
13831613Seschrock 	guid = strtoull(path, &end, 10);
13841544Seschrock 	if (guid != 0 && *end == '\0') {
13851544Seschrock 		search = NULL;
13861544Seschrock 	} else if (path[0] != '/') {
13871544Seschrock 		(void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path);
13881544Seschrock 		search = buf;
13891544Seschrock 	} else {
13901544Seschrock 		search = path;
13911544Seschrock 	}
13921544Seschrock 
13931544Seschrock 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
13941544Seschrock 	    &nvroot) == 0);
13951544Seschrock 
13962468Sek110237 	*avail_spare = B_FALSE;
13975450Sbrendan 	*l2cache = B_FALSE;
13985450Sbrendan 	return (vdev_to_nvlist_iter(nvroot, search, guid, avail_spare,
13995450Sbrendan 	    l2cache));
14002468Sek110237 }
14012468Sek110237 
14022468Sek110237 /*
14035450Sbrendan  * Returns TRUE if the given guid corresponds to the given type.
14045450Sbrendan  * This is used to check for hot spares (INUSE or not), and level 2 cache
14055450Sbrendan  * devices.
14062468Sek110237  */
14072468Sek110237 static boolean_t
14085450Sbrendan is_guid_type(zpool_handle_t *zhp, uint64_t guid, const char *type)
14092468Sek110237 {
14105450Sbrendan 	uint64_t target_guid;
14112468Sek110237 	nvlist_t *nvroot;
14125450Sbrendan 	nvlist_t **list;
14135450Sbrendan 	uint_t count;
14142468Sek110237 	int i;
14152468Sek110237 
14162468Sek110237 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
14172468Sek110237 	    &nvroot) == 0);
14185450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, type, &list, &count) == 0) {
14195450Sbrendan 		for (i = 0; i < count; i++) {
14205450Sbrendan 			verify(nvlist_lookup_uint64(list[i], ZPOOL_CONFIG_GUID,
14215450Sbrendan 			    &target_guid) == 0);
14225450Sbrendan 			if (guid == target_guid)
14232468Sek110237 				return (B_TRUE);
14242468Sek110237 		}
14252468Sek110237 	}
14262468Sek110237 
14272468Sek110237 	return (B_FALSE);
14281544Seschrock }
14291544Seschrock 
1430789Sahrens /*
14314451Seschrock  * Bring the specified vdev online.   The 'flags' parameter is a set of the
14324451Seschrock  * ZFS_ONLINE_* flags.
1433789Sahrens  */
1434789Sahrens int
14354451Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
14364451Seschrock     vdev_state_t *newstate)
1437789Sahrens {
1438789Sahrens 	zfs_cmd_t zc = { 0 };
1439789Sahrens 	char msg[1024];
14402082Seschrock 	nvlist_t *tgt;
14415450Sbrendan 	boolean_t avail_spare, l2cache;
14422082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1443789Sahrens 
14441544Seschrock 	(void) snprintf(msg, sizeof (msg),
14451544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot online %s"), path);
1446789Sahrens 
14471544Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
14485450Sbrendan 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == NULL)
14492082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
1450789Sahrens 
14512468Sek110237 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
14522468Sek110237 
14535450Sbrendan 	if (avail_spare ||
14545450Sbrendan 	    is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
14552082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
14562082Seschrock 
14574451Seschrock 	zc.zc_cookie = VDEV_STATE_ONLINE;
14584451Seschrock 	zc.zc_obj = flags;
14594451Seschrock 
14604543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0)
14614451Seschrock 		return (zpool_standard_error(hdl, errno, msg));
14624451Seschrock 
14634451Seschrock 	*newstate = zc.zc_cookie;
14644451Seschrock 	return (0);
1465789Sahrens }
1466789Sahrens 
1467789Sahrens /*
1468789Sahrens  * Take the specified vdev offline
1469789Sahrens  */
1470789Sahrens int
14714451Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
1472789Sahrens {
1473789Sahrens 	zfs_cmd_t zc = { 0 };
1474789Sahrens 	char msg[1024];
14752082Seschrock 	nvlist_t *tgt;
14765450Sbrendan 	boolean_t avail_spare, l2cache;
14772082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1478789Sahrens 
14791544Seschrock 	(void) snprintf(msg, sizeof (msg),
14801544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
14811544Seschrock 
1482789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
14835450Sbrendan 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == NULL)
14842082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
14852082Seschrock 
14862468Sek110237 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
14872468Sek110237 
14885450Sbrendan 	if (avail_spare ||
14895450Sbrendan 	    is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
14902082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
14912082Seschrock 
14924451Seschrock 	zc.zc_cookie = VDEV_STATE_OFFLINE;
14934451Seschrock 	zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
14941485Slling 
14954543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
1496789Sahrens 		return (0);
1497789Sahrens 
1498789Sahrens 	switch (errno) {
14992082Seschrock 	case EBUSY:
1500789Sahrens 
1501789Sahrens 		/*
1502789Sahrens 		 * There are no other replicas of this device.
1503789Sahrens 		 */
15042082Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
15052082Seschrock 
15062082Seschrock 	default:
15072082Seschrock 		return (zpool_standard_error(hdl, errno, msg));
15082082Seschrock 	}
15092082Seschrock }
1510789Sahrens 
15112082Seschrock /*
15124451Seschrock  * Mark the given vdev faulted.
15134451Seschrock  */
15144451Seschrock int
15154451Seschrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid)
15164451Seschrock {
15174451Seschrock 	zfs_cmd_t zc = { 0 };
15184451Seschrock 	char msg[1024];
15194451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
15204451Seschrock 
15214451Seschrock 	(void) snprintf(msg, sizeof (msg),
15224451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
15234451Seschrock 
15244451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
15254451Seschrock 	zc.zc_guid = guid;
15264451Seschrock 	zc.zc_cookie = VDEV_STATE_FAULTED;
15274451Seschrock 
15284451Seschrock 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
15294451Seschrock 		return (0);
15304451Seschrock 
15314451Seschrock 	switch (errno) {
15324451Seschrock 	case EBUSY:
15334451Seschrock 
15344451Seschrock 		/*
15354451Seschrock 		 * There are no other replicas of this device.
15364451Seschrock 		 */
15374451Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
15384451Seschrock 
15394451Seschrock 	default:
15404451Seschrock 		return (zpool_standard_error(hdl, errno, msg));
15414451Seschrock 	}
15424451Seschrock 
15434451Seschrock }
15444451Seschrock 
15454451Seschrock /*
15464451Seschrock  * Mark the given vdev degraded.
15474451Seschrock  */
15484451Seschrock int
15494451Seschrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid)
15504451Seschrock {
15514451Seschrock 	zfs_cmd_t zc = { 0 };
15524451Seschrock 	char msg[1024];
15534451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
15544451Seschrock 
15554451Seschrock 	(void) snprintf(msg, sizeof (msg),
15564451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
15574451Seschrock 
15584451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
15594451Seschrock 	zc.zc_guid = guid;
15604451Seschrock 	zc.zc_cookie = VDEV_STATE_DEGRADED;
15614451Seschrock 
15624451Seschrock 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
15634451Seschrock 		return (0);
15644451Seschrock 
15654451Seschrock 	return (zpool_standard_error(hdl, errno, msg));
15664451Seschrock }
15674451Seschrock 
15684451Seschrock /*
15692082Seschrock  * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
15702082Seschrock  * a hot spare.
15712082Seschrock  */
15722082Seschrock static boolean_t
15732082Seschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
15742082Seschrock {
15752082Seschrock 	nvlist_t **child;
15762082Seschrock 	uint_t c, children;
15772082Seschrock 	char *type;
15782082Seschrock 
15792082Seschrock 	if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
15802082Seschrock 	    &children) == 0) {
15812082Seschrock 		verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
15822082Seschrock 		    &type) == 0);
15832082Seschrock 
15842082Seschrock 		if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
15852082Seschrock 		    children == 2 && child[which] == tgt)
15862082Seschrock 			return (B_TRUE);
15872082Seschrock 
15882082Seschrock 		for (c = 0; c < children; c++)
15892082Seschrock 			if (is_replacing_spare(child[c], tgt, which))
15902082Seschrock 				return (B_TRUE);
1591789Sahrens 	}
15922082Seschrock 
15932082Seschrock 	return (B_FALSE);
1594789Sahrens }
1595789Sahrens 
1596789Sahrens /*
1597789Sahrens  * Attach new_disk (fully described by nvroot) to old_disk.
15984527Sperrin  * If 'replacing' is specified, the new disk will replace the old one.
1599789Sahrens  */
1600789Sahrens int
1601789Sahrens zpool_vdev_attach(zpool_handle_t *zhp,
1602789Sahrens     const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
1603789Sahrens {
1604789Sahrens 	zfs_cmd_t zc = { 0 };
1605789Sahrens 	char msg[1024];
1606789Sahrens 	int ret;
16072082Seschrock 	nvlist_t *tgt;
16085450Sbrendan 	boolean_t avail_spare, l2cache;
16094527Sperrin 	uint64_t val, is_log;
16107041Seschrock 	char *path, *newname;
16112082Seschrock 	nvlist_t **child;
16122082Seschrock 	uint_t children;
16132082Seschrock 	nvlist_t *config_root;
16142082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1615789Sahrens 
16161544Seschrock 	if (replacing)
16171544Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
16181544Seschrock 		    "cannot replace %s with %s"), old_disk, new_disk);
16191544Seschrock 	else
16201544Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
16211544Seschrock 		    "cannot attach %s to %s"), new_disk, old_disk);
16221544Seschrock 
1623789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
16245450Sbrendan 	if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache)) == 0)
16252082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
16262082Seschrock 
16272468Sek110237 	if (avail_spare)
16282082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
16292082Seschrock 
16305450Sbrendan 	if (l2cache)
16315450Sbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
16325450Sbrendan 
16332082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
16342082Seschrock 	zc.zc_cookie = replacing;
16352082Seschrock 
16362082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
16372082Seschrock 	    &child, &children) != 0 || children != 1) {
16382082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16392082Seschrock 		    "new device must be a single disk"));
16402082Seschrock 		return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
16411544Seschrock 	}
16422082Seschrock 
16432082Seschrock 	verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
16442082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
16452082Seschrock 
16467041Seschrock 	if ((newname = zpool_vdev_name(NULL, NULL, child[0])) == NULL)
16477041Seschrock 		return (-1);
16487041Seschrock 
16492082Seschrock 	/*
16502082Seschrock 	 * If the target is a hot spare that has been swapped in, we can only
16512082Seschrock 	 * replace it with another hot spare.
16522082Seschrock 	 */
16532082Seschrock 	if (replacing &&
16542082Seschrock 	    nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
16557041Seschrock 	    (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache) == NULL ||
16562468Sek110237 	    !avail_spare) && is_replacing_spare(config_root, tgt, 1)) {
16572082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16582082Seschrock 		    "can only be replaced by another hot spare"));
16597041Seschrock 		free(newname);
16602082Seschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
16612082Seschrock 	}
16622082Seschrock 
16632082Seschrock 	/*
16642082Seschrock 	 * If we are attempting to replace a spare, it canot be applied to an
16652082Seschrock 	 * already spared device.
16662082Seschrock 	 */
16672082Seschrock 	if (replacing &&
16682082Seschrock 	    nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 &&
16697041Seschrock 	    zpool_find_vdev(zhp, newname, &avail_spare, &l2cache) != NULL &&
16705450Sbrendan 	    avail_spare && is_replacing_spare(config_root, tgt, 0)) {
16712082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16722082Seschrock 		    "device has already been replaced with a spare"));
16737041Seschrock 		free(newname);
16742082Seschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
16752082Seschrock 	}
1676789Sahrens 
16777041Seschrock 	free(newname);
16787041Seschrock 
16795094Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
16802082Seschrock 		return (-1);
1681789Sahrens 
16824543Smarks 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc);
1683789Sahrens 
16842676Seschrock 	zcmd_free_nvlists(&zc);
1685789Sahrens 
1686789Sahrens 	if (ret == 0)
1687789Sahrens 		return (0);
1688789Sahrens 
1689789Sahrens 	switch (errno) {
16901544Seschrock 	case ENOTSUP:
1691789Sahrens 		/*
1692789Sahrens 		 * Can't attach to or replace this type of vdev.
1693789Sahrens 		 */
16944527Sperrin 		if (replacing) {
16954527Sperrin 			is_log = B_FALSE;
16964527Sperrin 			(void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_LOG,
16974527Sperrin 			    &is_log);
16984527Sperrin 			if (is_log)
16994527Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17004527Sperrin 				    "cannot replace a log with a spare"));
17014527Sperrin 			else
17024527Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17034527Sperrin 				    "cannot replace a replacing device"));
17044527Sperrin 		} else {
17052082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17062082Seschrock 			    "can only attach to mirrors and top-level "
17072082Seschrock 			    "disks"));
17084527Sperrin 		}
17092082Seschrock 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
1710789Sahrens 		break;
1711789Sahrens 
17121544Seschrock 	case EINVAL:
1713789Sahrens 		/*
1714789Sahrens 		 * The new device must be a single disk.
1715789Sahrens 		 */
17162082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17172082Seschrock 		    "new device must be a single disk"));
17182082Seschrock 		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
1719789Sahrens 		break;
1720789Sahrens 
17211544Seschrock 	case EBUSY:
17222082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
17232082Seschrock 		    new_disk);
17242082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1725789Sahrens 		break;
1726789Sahrens 
17271544Seschrock 	case EOVERFLOW:
1728789Sahrens 		/*
1729789Sahrens 		 * The new device is too small.
1730789Sahrens 		 */
17312082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17322082Seschrock 		    "device is too small"));
17332082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1734789Sahrens 		break;
1735789Sahrens 
17361544Seschrock 	case EDOM:
1737789Sahrens 		/*
1738789Sahrens 		 * The new device has a different alignment requirement.
1739789Sahrens 		 */
17402082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17412082Seschrock 		    "devices have different sector alignment"));
17422082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1743789Sahrens 		break;
1744789Sahrens 
17451544Seschrock 	case ENAMETOOLONG:
1746789Sahrens 		/*
1747789Sahrens 		 * The resulting top-level vdev spec won't fit in the label.
1748789Sahrens 		 */
17492082Seschrock 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
1750789Sahrens 		break;
1751789Sahrens 
17521544Seschrock 	default:
17532082Seschrock 		(void) zpool_standard_error(hdl, errno, msg);
1754789Sahrens 	}
1755789Sahrens 
17562082Seschrock 	return (-1);
1757789Sahrens }
1758789Sahrens 
1759789Sahrens /*
1760789Sahrens  * Detach the specified device.
1761789Sahrens  */
1762789Sahrens int
1763789Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
1764789Sahrens {
1765789Sahrens 	zfs_cmd_t zc = { 0 };
1766789Sahrens 	char msg[1024];
17672082Seschrock 	nvlist_t *tgt;
17685450Sbrendan 	boolean_t avail_spare, l2cache;
17692082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1770789Sahrens 
17711544Seschrock 	(void) snprintf(msg, sizeof (msg),
17721544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
17731544Seschrock 
1774789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
17755450Sbrendan 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == 0)
17762082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
1777789Sahrens 
17782468Sek110237 	if (avail_spare)
17792082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
17802082Seschrock 
17815450Sbrendan 	if (l2cache)
17825450Sbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
17835450Sbrendan 
17842082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
17852082Seschrock 
17864543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
1787789Sahrens 		return (0);
1788789Sahrens 
1789789Sahrens 	switch (errno) {
1790789Sahrens 
17911544Seschrock 	case ENOTSUP:
1792789Sahrens 		/*
1793789Sahrens 		 * Can't detach from this type of vdev.
1794789Sahrens 		 */
17952082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
17962082Seschrock 		    "applicable to mirror and replacing vdevs"));
17972082Seschrock 		(void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg);
1798789Sahrens 		break;
1799789Sahrens 
18001544Seschrock 	case EBUSY:
1801789Sahrens 		/*
1802789Sahrens 		 * There are no other replicas of this device.
1803789Sahrens 		 */
18042082Seschrock 		(void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
1805789Sahrens 		break;
1806789Sahrens 
18071544Seschrock 	default:
18082082Seschrock 		(void) zpool_standard_error(hdl, errno, msg);
18091544Seschrock 	}
18101544Seschrock 
18112082Seschrock 	return (-1);
18122082Seschrock }
18132082Seschrock 
18142082Seschrock /*
18155450Sbrendan  * Remove the given device.  Currently, this is supported only for hot spares
18165450Sbrendan  * and level 2 cache devices.
18172082Seschrock  */
18182082Seschrock int
18192082Seschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
18202082Seschrock {
18212082Seschrock 	zfs_cmd_t zc = { 0 };
18222082Seschrock 	char msg[1024];
18232082Seschrock 	nvlist_t *tgt;
18245450Sbrendan 	boolean_t avail_spare, l2cache;
18252082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
18262082Seschrock 
18272082Seschrock 	(void) snprintf(msg, sizeof (msg),
18282082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
18292082Seschrock 
18302082Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
18315450Sbrendan 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == 0)
18322082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
18332082Seschrock 
18345450Sbrendan 	if (!avail_spare && !l2cache) {
18352082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18365450Sbrendan 		    "only inactive hot spares or cache devices "
18375450Sbrendan 		    "can be removed"));
18382082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
18392082Seschrock 	}
18402082Seschrock 
18412082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
18422082Seschrock 
18434543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
18442082Seschrock 		return (0);
18452082Seschrock 
18462082Seschrock 	return (zpool_standard_error(hdl, errno, msg));
18471544Seschrock }
18481544Seschrock 
18491544Seschrock /*
18501544Seschrock  * Clear the errors for the pool, or the particular device if specified.
18511544Seschrock  */
18521544Seschrock int
18531544Seschrock zpool_clear(zpool_handle_t *zhp, const char *path)
18541544Seschrock {
18551544Seschrock 	zfs_cmd_t zc = { 0 };
18561544Seschrock 	char msg[1024];
18572082Seschrock 	nvlist_t *tgt;
18585450Sbrendan 	boolean_t avail_spare, l2cache;
18592082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
18601544Seschrock 
18611544Seschrock 	if (path)
18621544Seschrock 		(void) snprintf(msg, sizeof (msg),
18631544Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
18642676Seschrock 		    path);
18651544Seschrock 	else
18661544Seschrock 		(void) snprintf(msg, sizeof (msg),
18671544Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
18681544Seschrock 		    zhp->zpool_name);
18691544Seschrock 
18701544Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
18712082Seschrock 	if (path) {
18725450Sbrendan 		if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
18735450Sbrendan 		    &l2cache)) == 0)
18742082Seschrock 			return (zfs_error(hdl, EZFS_NODEVICE, msg));
18752082Seschrock 
18765450Sbrendan 		/*
18775450Sbrendan 		 * Don't allow error clearing for hot spares.  Do allow
18785450Sbrendan 		 * error clearing for l2cache devices.
18795450Sbrendan 		 */
18802468Sek110237 		if (avail_spare)
18812082Seschrock 			return (zfs_error(hdl, EZFS_ISSPARE, msg));
18822082Seschrock 
18832082Seschrock 		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
18842082Seschrock 		    &zc.zc_guid) == 0);
18851544Seschrock 	}
18861544Seschrock 
18874543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0)
18881544Seschrock 		return (0);
18891544Seschrock 
18902082Seschrock 	return (zpool_standard_error(hdl, errno, msg));
1891789Sahrens }
1892789Sahrens 
18933126Sahl /*
18944451Seschrock  * Similar to zpool_clear(), but takes a GUID (used by fmd).
18954451Seschrock  */
18964451Seschrock int
18974451Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
18984451Seschrock {
18994451Seschrock 	zfs_cmd_t zc = { 0 };
19004451Seschrock 	char msg[1024];
19014451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
19024451Seschrock 
19034451Seschrock 	(void) snprintf(msg, sizeof (msg),
19044451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
19054451Seschrock 	    guid);
19064451Seschrock 
19074451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
19084451Seschrock 	zc.zc_guid = guid;
19094451Seschrock 
19104451Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
19114451Seschrock 		return (0);
19124451Seschrock 
19134451Seschrock 	return (zpool_standard_error(hdl, errno, msg));
19144451Seschrock }
19154451Seschrock 
19164451Seschrock /*
19173126Sahl  * Iterate over all zvols in a given pool by walking the /dev/zvol/dsk/<pool>
19183126Sahl  * hierarchy.
19193126Sahl  */
19203126Sahl int
19213126Sahl zpool_iter_zvol(zpool_handle_t *zhp, int (*cb)(const char *, void *),
19223126Sahl     void *data)
1923789Sahrens {
19243126Sahl 	libzfs_handle_t *hdl = zhp->zpool_hdl;
19253126Sahl 	char (*paths)[MAXPATHLEN];
19263126Sahl 	size_t size = 4;
19273126Sahl 	int curr, fd, base, ret = 0;
19283126Sahl 	DIR *dirp;
19293126Sahl 	struct dirent *dp;
19303126Sahl 	struct stat st;
19313126Sahl 
19323126Sahl 	if ((base = open("/dev/zvol/dsk", O_RDONLY)) < 0)
19333126Sahl 		return (errno == ENOENT ? 0 : -1);
19343126Sahl 
19353126Sahl 	if (fstatat(base, zhp->zpool_name, &st, 0) != 0) {
19363126Sahl 		int err = errno;
19373126Sahl 		(void) close(base);
19383126Sahl 		return (err == ENOENT ? 0 : -1);
19393126Sahl 	}
1940789Sahrens 
1941789Sahrens 	/*
19423126Sahl 	 * Oddly this wasn't a directory -- ignore that failure since we
19433126Sahl 	 * know there are no links lower in the (non-existant) hierarchy.
1944789Sahrens 	 */
19453126Sahl 	if (!S_ISDIR(st.st_mode)) {
19463126Sahl 		(void) close(base);
19473126Sahl 		return (0);
19483126Sahl 	}
19493126Sahl 
19503126Sahl 	if ((paths = zfs_alloc(hdl, size * sizeof (paths[0]))) == NULL) {
19513126Sahl 		(void) close(base);
19523126Sahl 		return (-1);
1953789Sahrens 	}
1954789Sahrens 
19553126Sahl 	(void) strlcpy(paths[0], zhp->zpool_name, sizeof (paths[0]));
19563126Sahl 	curr = 0;
19573126Sahl 
19583126Sahl 	while (curr >= 0) {
19593126Sahl 		if (fstatat(base, paths[curr], &st, AT_SYMLINK_NOFOLLOW) != 0)
19603126Sahl 			goto err;
19613126Sahl 
19623126Sahl 		if (S_ISDIR(st.st_mode)) {
19633126Sahl 			if ((fd = openat(base, paths[curr], O_RDONLY)) < 0)
19643126Sahl 				goto err;
19653126Sahl 
19663126Sahl 			if ((dirp = fdopendir(fd)) == NULL) {
19673126Sahl 				(void) close(fd);
19683126Sahl 				goto err;
19693126Sahl 			}
19703126Sahl 
19713126Sahl 			while ((dp = readdir(dirp)) != NULL) {
19723126Sahl 				if (dp->d_name[0] == '.')
19733126Sahl 					continue;
19743126Sahl 
19753126Sahl 				if (curr + 1 == size) {
19763126Sahl 					paths = zfs_realloc(hdl, paths,
19773126Sahl 					    size * sizeof (paths[0]),
19783126Sahl 					    size * 2 * sizeof (paths[0]));
19793126Sahl 					if (paths == NULL) {
19803126Sahl 						(void) closedir(dirp);
19813126Sahl 						(void) close(fd);
19823126Sahl 						goto err;
19833126Sahl 					}
19843126Sahl 
19853126Sahl 					size *= 2;
19863126Sahl 				}
19873126Sahl 
19883126Sahl 				(void) strlcpy(paths[curr + 1], paths[curr],
19893126Sahl 				    sizeof (paths[curr + 1]));
19903126Sahl 				(void) strlcat(paths[curr], "/",
19913126Sahl 				    sizeof (paths[curr]));
19923126Sahl 				(void) strlcat(paths[curr], dp->d_name,
19933126Sahl 				    sizeof (paths[curr]));
19943126Sahl 				curr++;
19953126Sahl 			}
19963126Sahl 
19973126Sahl 			(void) closedir(dirp);
19983126Sahl 
19993126Sahl 		} else {
20003126Sahl 			if ((ret = cb(paths[curr], data)) != 0)
20013126Sahl 				break;
20023126Sahl 		}
20033126Sahl 
20043126Sahl 		curr--;
20053126Sahl 	}
20063126Sahl 
20073126Sahl 	free(paths);
20083126Sahl 	(void) close(base);
20093126Sahl 
20103126Sahl 	return (ret);
20113126Sahl 
20123126Sahl err:
20133126Sahl 	free(paths);
20143126Sahl 	(void) close(base);
20153126Sahl 	return (-1);
20163126Sahl }
20173126Sahl 
20183126Sahl typedef struct zvol_cb {
20193126Sahl 	zpool_handle_t *zcb_pool;
20203126Sahl 	boolean_t zcb_create;
20213126Sahl } zvol_cb_t;
20223126Sahl 
20233126Sahl /*ARGSUSED*/
20243126Sahl static int
20253126Sahl do_zvol_create(zfs_handle_t *zhp, void *data)
20263126Sahl {
20274657Sahrens 	int ret = 0;
20283126Sahl 
20294657Sahrens 	if (ZFS_IS_VOLUME(zhp)) {
20303126Sahl 		(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
20314657Sahrens 		ret = zfs_iter_snapshots(zhp, do_zvol_create, NULL);
20324657Sahrens 	}
20333126Sahl 
20344657Sahrens 	if (ret == 0)
20354657Sahrens 		ret = zfs_iter_filesystems(zhp, do_zvol_create, NULL);
2036789Sahrens 
2037789Sahrens 	zfs_close(zhp);
20383126Sahl 
2039789Sahrens 	return (ret);
2040789Sahrens }
2041789Sahrens 
2042789Sahrens /*
2043789Sahrens  * Iterate over all zvols in the pool and make any necessary minor nodes.
2044789Sahrens  */
2045789Sahrens int
2046789Sahrens zpool_create_zvol_links(zpool_handle_t *zhp)
2047789Sahrens {
2048789Sahrens 	zfs_handle_t *zfp;
2049789Sahrens 	int ret;
2050789Sahrens 
2051789Sahrens 	/*
2052789Sahrens 	 * If the pool is unavailable, just return success.
2053789Sahrens 	 */
20542082Seschrock 	if ((zfp = make_dataset_handle(zhp->zpool_hdl,
20552082Seschrock 	    zhp->zpool_name)) == NULL)
2056789Sahrens 		return (0);
2057789Sahrens 
20584657Sahrens 	ret = zfs_iter_filesystems(zfp, do_zvol_create, NULL);
2059789Sahrens 
2060789Sahrens 	zfs_close(zfp);
2061789Sahrens 	return (ret);
2062789Sahrens }
2063789Sahrens 
20643126Sahl static int
20653126Sahl do_zvol_remove(const char *dataset, void *data)
20663126Sahl {
20673126Sahl 	zpool_handle_t *zhp = data;
20683126Sahl 
20693126Sahl 	return (zvol_remove_link(zhp->zpool_hdl, dataset));
20703126Sahl }
20713126Sahl 
2072789Sahrens /*
20733126Sahl  * Iterate over all zvols in the pool and remove any minor nodes.  We iterate
20743126Sahl  * by examining the /dev links so that a corrupted pool doesn't impede this
20753126Sahl  * operation.
2076789Sahrens  */
2077789Sahrens int
2078789Sahrens zpool_remove_zvol_links(zpool_handle_t *zhp)
2079789Sahrens {
20803126Sahl 	return (zpool_iter_zvol(zhp, do_zvol_remove, zhp));
2081789Sahrens }
20821354Seschrock 
20831354Seschrock /*
20841354Seschrock  * Convert from a devid string to a path.
20851354Seschrock  */
20861354Seschrock static char *
20871354Seschrock devid_to_path(char *devid_str)
20881354Seschrock {
20891354Seschrock 	ddi_devid_t devid;
20901354Seschrock 	char *minor;
20911354Seschrock 	char *path;
20921354Seschrock 	devid_nmlist_t *list = NULL;
20931354Seschrock 	int ret;
20941354Seschrock 
20951354Seschrock 	if (devid_str_decode(devid_str, &devid, &minor) != 0)
20961354Seschrock 		return (NULL);
20971354Seschrock 
20981354Seschrock 	ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
20991354Seschrock 
21001354Seschrock 	devid_str_free(minor);
21011354Seschrock 	devid_free(devid);
21021354Seschrock 
21031354Seschrock 	if (ret != 0)
21041354Seschrock 		return (NULL);
21051354Seschrock 
21062082Seschrock 	if ((path = strdup(list[0].devname)) == NULL)
21072082Seschrock 		return (NULL);
21082082Seschrock 
21091354Seschrock 	devid_free_nmlist(list);
21101354Seschrock 
21111354Seschrock 	return (path);
21121354Seschrock }
21131354Seschrock 
21141354Seschrock /*
21151354Seschrock  * Convert from a path to a devid string.
21161354Seschrock  */
21171354Seschrock static char *
21181354Seschrock path_to_devid(const char *path)
21191354Seschrock {
21201354Seschrock 	int fd;
21211354Seschrock 	ddi_devid_t devid;
21221354Seschrock 	char *minor, *ret;
21231354Seschrock 
21241354Seschrock 	if ((fd = open(path, O_RDONLY)) < 0)
21251354Seschrock 		return (NULL);
21261354Seschrock 
21271354Seschrock 	minor = NULL;
21281354Seschrock 	ret = NULL;
21291354Seschrock 	if (devid_get(fd, &devid) == 0) {
21301354Seschrock 		if (devid_get_minor_name(fd, &minor) == 0)
21311354Seschrock 			ret = devid_str_encode(devid, minor);
21321354Seschrock 		if (minor != NULL)
21331354Seschrock 			devid_str_free(minor);
21341354Seschrock 		devid_free(devid);
21351354Seschrock 	}
21361354Seschrock 	(void) close(fd);
21371354Seschrock 
21381354Seschrock 	return (ret);
21391354Seschrock }
21401354Seschrock 
21411354Seschrock /*
21421354Seschrock  * Issue the necessary ioctl() to update the stored path value for the vdev.  We
21431354Seschrock  * ignore any failure here, since a common case is for an unprivileged user to
21441354Seschrock  * type 'zpool status', and we'll display the correct information anyway.
21451354Seschrock  */
21461354Seschrock static void
21471354Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
21481354Seschrock {
21491354Seschrock 	zfs_cmd_t zc = { 0 };
21501354Seschrock 
21511354Seschrock 	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
21522676Seschrock 	(void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
21531354Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
21541544Seschrock 	    &zc.zc_guid) == 0);
21551354Seschrock 
21562082Seschrock 	(void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
21571354Seschrock }
21581354Seschrock 
21591354Seschrock /*
21601354Seschrock  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
21611354Seschrock  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
21621354Seschrock  * We also check if this is a whole disk, in which case we strip off the
21631354Seschrock  * trailing 's0' slice name.
21641354Seschrock  *
21651354Seschrock  * This routine is also responsible for identifying when disks have been
21661354Seschrock  * reconfigured in a new location.  The kernel will have opened the device by
21671354Seschrock  * devid, but the path will still refer to the old location.  To catch this, we
21681354Seschrock  * first do a path -> devid translation (which is fast for the common case).  If
21691354Seschrock  * the devid matches, we're done.  If not, we do a reverse devid -> path
21701354Seschrock  * translation and issue the appropriate ioctl() to update the path of the vdev.
21711354Seschrock  * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
21721354Seschrock  * of these checks.
21731354Seschrock  */
21741354Seschrock char *
21752082Seschrock zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv)
21761354Seschrock {
21771354Seschrock 	char *path, *devid;
21781544Seschrock 	uint64_t value;
21791544Seschrock 	char buf[64];
21804451Seschrock 	vdev_stat_t *vs;
21814451Seschrock 	uint_t vsc;
21821354Seschrock 
21831544Seschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
21841544Seschrock 	    &value) == 0) {
21851544Seschrock 		verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
21861544Seschrock 		    &value) == 0);
21872856Snd150628 		(void) snprintf(buf, sizeof (buf), "%llu",
21882856Snd150628 		    (u_longlong_t)value);
21891544Seschrock 		path = buf;
21901544Seschrock 	} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
21911354Seschrock 
21924451Seschrock 		/*
21934451Seschrock 		 * If the device is dead (faulted, offline, etc) then don't
21944451Seschrock 		 * bother opening it.  Otherwise we may be forcing the user to
21954451Seschrock 		 * open a misbehaving device, which can have undesirable
21964451Seschrock 		 * effects.
21974451Seschrock 		 */
21984451Seschrock 		if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
21994451Seschrock 		    (uint64_t **)&vs, &vsc) != 0 ||
22004451Seschrock 		    vs->vs_state >= VDEV_STATE_DEGRADED) &&
22014451Seschrock 		    zhp != NULL &&
22021354Seschrock 		    nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
22031354Seschrock 			/*
22041354Seschrock 			 * Determine if the current path is correct.
22051354Seschrock 			 */
22061354Seschrock 			char *newdevid = path_to_devid(path);
22071354Seschrock 
22081354Seschrock 			if (newdevid == NULL ||
22091354Seschrock 			    strcmp(devid, newdevid) != 0) {
22101354Seschrock 				char *newpath;
22111354Seschrock 
22121354Seschrock 				if ((newpath = devid_to_path(devid)) != NULL) {
22131354Seschrock 					/*
22141354Seschrock 					 * Update the path appropriately.
22151354Seschrock 					 */
22161354Seschrock 					set_path(zhp, nv, newpath);
22172082Seschrock 					if (nvlist_add_string(nv,
22182082Seschrock 					    ZPOOL_CONFIG_PATH, newpath) == 0)
22192082Seschrock 						verify(nvlist_lookup_string(nv,
22202082Seschrock 						    ZPOOL_CONFIG_PATH,
22212082Seschrock 						    &path) == 0);
22221354Seschrock 					free(newpath);
22231354Seschrock 				}
22241354Seschrock 			}
22251354Seschrock 
22262082Seschrock 			if (newdevid)
22272082Seschrock 				devid_str_free(newdevid);
22281354Seschrock 		}
22291354Seschrock 
22301354Seschrock 		if (strncmp(path, "/dev/dsk/", 9) == 0)
22311354Seschrock 			path += 9;
22321354Seschrock 
22331354Seschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
22341544Seschrock 		    &value) == 0 && value) {
22352082Seschrock 			char *tmp = zfs_strdup(hdl, path);
22362082Seschrock 			if (tmp == NULL)
22372082Seschrock 				return (NULL);
22381354Seschrock 			tmp[strlen(path) - 2] = '\0';
22391354Seschrock 			return (tmp);
22401354Seschrock 		}
22411354Seschrock 	} else {
22421354Seschrock 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
22432082Seschrock 
22442082Seschrock 		/*
22452082Seschrock 		 * If it's a raidz device, we need to stick in the parity level.
22462082Seschrock 		 */
22472082Seschrock 		if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
22482082Seschrock 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
22492082Seschrock 			    &value) == 0);
22502082Seschrock 			(void) snprintf(buf, sizeof (buf), "%s%llu", path,
22512856Snd150628 			    (u_longlong_t)value);
22522082Seschrock 			path = buf;
22532082Seschrock 		}
22541354Seschrock 	}
22551354Seschrock 
22562082Seschrock 	return (zfs_strdup(hdl, path));
22571354Seschrock }
22581544Seschrock 
22591544Seschrock static int
22601544Seschrock zbookmark_compare(const void *a, const void *b)
22611544Seschrock {
22621544Seschrock 	return (memcmp(a, b, sizeof (zbookmark_t)));
22631544Seschrock }
22641544Seschrock 
22651544Seschrock /*
22661544Seschrock  * Retrieve the persistent error log, uniquify the members, and return to the
22671544Seschrock  * caller.
22681544Seschrock  */
22691544Seschrock int
22703444Sek110237 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
22711544Seschrock {
22721544Seschrock 	zfs_cmd_t zc = { 0 };
22731544Seschrock 	uint64_t count;
22742676Seschrock 	zbookmark_t *zb = NULL;
22753444Sek110237 	int i;
22761544Seschrock 
22771544Seschrock 	/*
22781544Seschrock 	 * Retrieve the raw error list from the kernel.  If the number of errors
22791544Seschrock 	 * has increased, allocate more space and continue until we get the
22801544Seschrock 	 * entire list.
22811544Seschrock 	 */
22821544Seschrock 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
22831544Seschrock 	    &count) == 0);
22844820Sek110237 	if (count == 0)
22854820Sek110237 		return (0);
22862676Seschrock 	if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
22872856Snd150628 	    count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
22882082Seschrock 		return (-1);
22892676Seschrock 	zc.zc_nvlist_dst_size = count;
22901544Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
22911544Seschrock 	for (;;) {
22922082Seschrock 		if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
22932082Seschrock 		    &zc) != 0) {
22942676Seschrock 			free((void *)(uintptr_t)zc.zc_nvlist_dst);
22951544Seschrock 			if (errno == ENOMEM) {
22963823Svb160487 				count = zc.zc_nvlist_dst_size;
22972676Seschrock 				if ((zc.zc_nvlist_dst = (uintptr_t)
22983823Svb160487 				    zfs_alloc(zhp->zpool_hdl, count *
22993823Svb160487 				    sizeof (zbookmark_t))) == (uintptr_t)NULL)
23002082Seschrock 					return (-1);
23011544Seschrock 			} else {
23021544Seschrock 				return (-1);
23031544Seschrock 			}
23041544Seschrock 		} else {
23051544Seschrock 			break;
23061544Seschrock 		}
23071544Seschrock 	}
23081544Seschrock 
23091544Seschrock 	/*
23101544Seschrock 	 * Sort the resulting bookmarks.  This is a little confusing due to the
23111544Seschrock 	 * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
23122676Seschrock 	 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
23131544Seschrock 	 * _not_ copied as part of the process.  So we point the start of our
23141544Seschrock 	 * array appropriate and decrement the total number of elements.
23151544Seschrock 	 */
23162676Seschrock 	zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
23172676Seschrock 	    zc.zc_nvlist_dst_size;
23182676Seschrock 	count -= zc.zc_nvlist_dst_size;
23191544Seschrock 
23201544Seschrock 	qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
23211544Seschrock 
23223444Sek110237 	verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
23231544Seschrock 
23241544Seschrock 	/*
23253444Sek110237 	 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
23261544Seschrock 	 */
23271544Seschrock 	for (i = 0; i < count; i++) {
23281544Seschrock 		nvlist_t *nv;
23291544Seschrock 
23303700Sek110237 		/* ignoring zb_blkid and zb_level for now */
23313700Sek110237 		if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
23323700Sek110237 		    zb[i-1].zb_object == zb[i].zb_object)
23331544Seschrock 			continue;
23341544Seschrock 
23353444Sek110237 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
23363444Sek110237 			goto nomem;
23373444Sek110237 		if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
23383444Sek110237 		    zb[i].zb_objset) != 0) {
23393444Sek110237 			nvlist_free(nv);
23402082Seschrock 			goto nomem;
23413444Sek110237 		}
23423444Sek110237 		if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
23433444Sek110237 		    zb[i].zb_object) != 0) {
23443444Sek110237 			nvlist_free(nv);
23453444Sek110237 			goto nomem;
23461544Seschrock 		}
23473444Sek110237 		if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
23483444Sek110237 			nvlist_free(nv);
23493444Sek110237 			goto nomem;
23503444Sek110237 		}
23513444Sek110237 		nvlist_free(nv);
23521544Seschrock 	}
23531544Seschrock 
23543265Sahrens 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
23551544Seschrock 	return (0);
23562082Seschrock 
23572082Seschrock nomem:
23582676Seschrock 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
23592082Seschrock 	return (no_memory(zhp->zpool_hdl));
23601544Seschrock }
23611760Seschrock 
23621760Seschrock /*
23631760Seschrock  * Upgrade a ZFS pool to the latest on-disk version.
23641760Seschrock  */
23651760Seschrock int
23665094Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
23671760Seschrock {
23681760Seschrock 	zfs_cmd_t zc = { 0 };
23692082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
23701760Seschrock 
23711760Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
23725094Slling 	zc.zc_cookie = new_version;
23735094Slling 
23744543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
23753237Slling 		return (zpool_standard_error_fmt(hdl, errno,
23762082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
23772082Seschrock 		    zhp->zpool_name));
23781760Seschrock 	return (0);
23791760Seschrock }
23802926Sek110237 
23814988Sek110237 void
23824988Sek110237 zpool_set_history_str(const char *subcommand, int argc, char **argv,
23834988Sek110237     char *history_str)
23844988Sek110237 {
23854988Sek110237 	int i;
23864988Sek110237 
23874988Sek110237 	(void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
23884988Sek110237 	for (i = 1; i < argc; i++) {
23894988Sek110237 		if (strlen(history_str) + 1 + strlen(argv[i]) >
23904988Sek110237 		    HIS_MAX_RECORD_LEN)
23914988Sek110237 			break;
23924988Sek110237 		(void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
23934988Sek110237 		(void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
23944988Sek110237 	}
23954988Sek110237 }
23964988Sek110237 
23972926Sek110237 /*
23984988Sek110237  * Stage command history for logging.
23992926Sek110237  */
24004988Sek110237 int
24014988Sek110237 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
24022926Sek110237 {
24034988Sek110237 	if (history_str == NULL)
24044988Sek110237 		return (EINVAL);
24054988Sek110237 
24064988Sek110237 	if (strlen(history_str) > HIS_MAX_RECORD_LEN)
24074988Sek110237 		return (EINVAL);
24082926Sek110237 
24094715Sek110237 	if (hdl->libzfs_log_str != NULL)
24104543Smarks 		free(hdl->libzfs_log_str);
24112926Sek110237 
24124988Sek110237 	if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
24134988Sek110237 		return (no_memory(hdl));
24144543Smarks 
24154988Sek110237 	return (0);
24162926Sek110237 }
24172926Sek110237 
24182926Sek110237 /*
24192926Sek110237  * Perform ioctl to get some command history of a pool.
24202926Sek110237  *
24212926Sek110237  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
24222926Sek110237  * logical offset of the history buffer to start reading from.
24232926Sek110237  *
24242926Sek110237  * Upon return, 'off' is the next logical offset to read from and
24252926Sek110237  * 'len' is the actual amount of bytes read into 'buf'.
24262926Sek110237  */
24272926Sek110237 static int
24282926Sek110237 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
24292926Sek110237 {
24302926Sek110237 	zfs_cmd_t zc = { 0 };
24312926Sek110237 	libzfs_handle_t *hdl = zhp->zpool_hdl;
24322926Sek110237 
24332926Sek110237 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
24342926Sek110237 
24352926Sek110237 	zc.zc_history = (uint64_t)(uintptr_t)buf;
24362926Sek110237 	zc.zc_history_len = *len;
24372926Sek110237 	zc.zc_history_offset = *off;
24382926Sek110237 
24392926Sek110237 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
24402926Sek110237 		switch (errno) {
24412926Sek110237 		case EPERM:
24423237Slling 			return (zfs_error_fmt(hdl, EZFS_PERM,
24433237Slling 			    dgettext(TEXT_DOMAIN,
24442926Sek110237 			    "cannot show history for pool '%s'"),
24452926Sek110237 			    zhp->zpool_name));
24462926Sek110237 		case ENOENT:
24473237Slling 			return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
24482926Sek110237 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
24492926Sek110237 			    "'%s'"), zhp->zpool_name));
24503863Sek110237 		case ENOTSUP:
24513863Sek110237 			return (zfs_error_fmt(hdl, EZFS_BADVERSION,
24523863Sek110237 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
24533863Sek110237 			    "'%s', pool must be upgraded"), zhp->zpool_name));
24542926Sek110237 		default:
24553237Slling 			return (zpool_standard_error_fmt(hdl, errno,
24562926Sek110237 			    dgettext(TEXT_DOMAIN,
24572926Sek110237 			    "cannot get history for '%s'"), zhp->zpool_name));
24582926Sek110237 		}
24592926Sek110237 	}
24602926Sek110237 
24612926Sek110237 	*len = zc.zc_history_len;
24622926Sek110237 	*off = zc.zc_history_offset;
24632926Sek110237 
24642926Sek110237 	return (0);
24652926Sek110237 }
24662926Sek110237 
24672926Sek110237 /*
24682926Sek110237  * Process the buffer of nvlists, unpacking and storing each nvlist record
24692926Sek110237  * into 'records'.  'leftover' is set to the number of bytes that weren't
24702926Sek110237  * processed as there wasn't a complete record.
24712926Sek110237  */
24722926Sek110237 static int
24732926Sek110237 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
24742926Sek110237     nvlist_t ***records, uint_t *numrecords)
24752926Sek110237 {
24762926Sek110237 	uint64_t reclen;
24772926Sek110237 	nvlist_t *nv;
24782926Sek110237 	int i;
24792926Sek110237 
24802926Sek110237 	while (bytes_read > sizeof (reclen)) {
24812926Sek110237 
24822926Sek110237 		/* get length of packed record (stored as little endian) */
24832926Sek110237 		for (i = 0, reclen = 0; i < sizeof (reclen); i++)
24842926Sek110237 			reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
24852926Sek110237 
24862926Sek110237 		if (bytes_read < sizeof (reclen) + reclen)
24872926Sek110237 			break;
24882926Sek110237 
24892926Sek110237 		/* unpack record */
24902926Sek110237 		if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
24912926Sek110237 			return (ENOMEM);
24922926Sek110237 		bytes_read -= sizeof (reclen) + reclen;
24932926Sek110237 		buf += sizeof (reclen) + reclen;
24942926Sek110237 
24952926Sek110237 		/* add record to nvlist array */
24962926Sek110237 		(*numrecords)++;
24972926Sek110237 		if (ISP2(*numrecords + 1)) {
24982926Sek110237 			*records = realloc(*records,
24992926Sek110237 			    *numrecords * 2 * sizeof (nvlist_t *));
25002926Sek110237 		}
25012926Sek110237 		(*records)[*numrecords - 1] = nv;
25022926Sek110237 	}
25032926Sek110237 
25042926Sek110237 	*leftover = bytes_read;
25052926Sek110237 	return (0);
25062926Sek110237 }
25072926Sek110237 
25082926Sek110237 #define	HIS_BUF_LEN	(128*1024)
25092926Sek110237 
25102926Sek110237 /*
25112926Sek110237  * Retrieve the command history of a pool.
25122926Sek110237  */
25132926Sek110237 int
25142926Sek110237 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
25152926Sek110237 {
25162926Sek110237 	char buf[HIS_BUF_LEN];
25172926Sek110237 	uint64_t off = 0;
25182926Sek110237 	nvlist_t **records = NULL;
25192926Sek110237 	uint_t numrecords = 0;
25202926Sek110237 	int err, i;
25212926Sek110237 
25222926Sek110237 	do {
25232926Sek110237 		uint64_t bytes_read = sizeof (buf);
25242926Sek110237 		uint64_t leftover;
25252926Sek110237 
25262926Sek110237 		if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
25272926Sek110237 			break;
25282926Sek110237 
25292926Sek110237 		/* if nothing else was read in, we're at EOF, just return */
25302926Sek110237 		if (!bytes_read)
25312926Sek110237 			break;
25322926Sek110237 
25332926Sek110237 		if ((err = zpool_history_unpack(buf, bytes_read,
25342926Sek110237 		    &leftover, &records, &numrecords)) != 0)
25352926Sek110237 			break;
25362926Sek110237 		off -= leftover;
25372926Sek110237 
25382926Sek110237 		/* CONSTCOND */
25392926Sek110237 	} while (1);
25402926Sek110237 
25412926Sek110237 	if (!err) {
25422926Sek110237 		verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
25432926Sek110237 		verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
25442926Sek110237 		    records, numrecords) == 0);
25452926Sek110237 	}
25462926Sek110237 	for (i = 0; i < numrecords; i++)
25472926Sek110237 		nvlist_free(records[i]);
25482926Sek110237 	free(records);
25492926Sek110237 
25502926Sek110237 	return (err);
25512926Sek110237 }
25523444Sek110237 
25533444Sek110237 void
25543444Sek110237 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
25553444Sek110237     char *pathname, size_t len)
25563444Sek110237 {
25573444Sek110237 	zfs_cmd_t zc = { 0 };
25583444Sek110237 	boolean_t mounted = B_FALSE;
25593444Sek110237 	char *mntpnt = NULL;
25603444Sek110237 	char dsname[MAXNAMELEN];
25613444Sek110237 
25623444Sek110237 	if (dsobj == 0) {
25633444Sek110237 		/* special case for the MOS */
25643444Sek110237 		(void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
25653444Sek110237 		return;
25663444Sek110237 	}
25673444Sek110237 
25683444Sek110237 	/* get the dataset's name */
25693444Sek110237 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
25703444Sek110237 	zc.zc_obj = dsobj;
25713444Sek110237 	if (ioctl(zhp->zpool_hdl->libzfs_fd,
25723444Sek110237 	    ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
25733444Sek110237 		/* just write out a path of two object numbers */
25743444Sek110237 		(void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
25753444Sek110237 		    dsobj, obj);
25763444Sek110237 		return;
25773444Sek110237 	}
25783444Sek110237 	(void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
25793444Sek110237 
25803444Sek110237 	/* find out if the dataset is mounted */
25813444Sek110237 	mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
25823444Sek110237 
25833444Sek110237 	/* get the corrupted object's path */
25843444Sek110237 	(void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
25853444Sek110237 	zc.zc_obj = obj;
25863444Sek110237 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
25873444Sek110237 	    &zc) == 0) {
25883444Sek110237 		if (mounted) {
25893444Sek110237 			(void) snprintf(pathname, len, "%s%s", mntpnt,
25903444Sek110237 			    zc.zc_value);
25913444Sek110237 		} else {
25923444Sek110237 			(void) snprintf(pathname, len, "%s:%s",
25933444Sek110237 			    dsname, zc.zc_value);
25943444Sek110237 		}
25953444Sek110237 	} else {
25963444Sek110237 		(void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
25973444Sek110237 	}
25983444Sek110237 	free(mntpnt);
25993444Sek110237 }
26003912Slling 
26014276Staylor #define	RDISK_ROOT	"/dev/rdsk"
26024276Staylor #define	BACKUP_SLICE	"s2"
26034276Staylor /*
26044276Staylor  * Don't start the slice at the default block of 34; many storage
26054276Staylor  * devices will use a stripe width of 128k, so start there instead.
26064276Staylor  */
26074276Staylor #define	NEW_START_BLOCK	256
26084276Staylor 
26094276Staylor /*
26107042Sgw25295  * Read the EFI label from the config, if a label does not exist then
26117042Sgw25295  * pass back the error to the caller. If the caller has passed a non-NULL
26127042Sgw25295  * diskaddr argument then we set it to the starting address of the EFI
26137042Sgw25295  * partition.
26147042Sgw25295  */
26157042Sgw25295 static int
26167042Sgw25295 read_efi_label(nvlist_t *config, diskaddr_t *sb)
26177042Sgw25295 {
26187042Sgw25295 	char *path;
26197042Sgw25295 	int fd;
26207042Sgw25295 	char diskname[MAXPATHLEN];
26217042Sgw25295 	int err = -1;
26227042Sgw25295 
26237042Sgw25295 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
26247042Sgw25295 		return (err);
26257042Sgw25295 
26267042Sgw25295 	(void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
26277042Sgw25295 	    strrchr(path, '/'));
26287042Sgw25295 	if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
26297042Sgw25295 		struct dk_gpt *vtoc;
26307042Sgw25295 
26317042Sgw25295 		if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
26327042Sgw25295 			if (sb != NULL)
26337042Sgw25295 				*sb = vtoc->efi_parts[0].p_start;
26347042Sgw25295 			efi_free(vtoc);
26357042Sgw25295 		}
26367042Sgw25295 		(void) close(fd);
26377042Sgw25295 	}
26387042Sgw25295 	return (err);
26397042Sgw25295 }
26407042Sgw25295 
26417042Sgw25295 /*
26424276Staylor  * determine where a partition starts on a disk in the current
26434276Staylor  * configuration
26444276Staylor  */
26454276Staylor static diskaddr_t
26464276Staylor find_start_block(nvlist_t *config)
26474276Staylor {
26484276Staylor 	nvlist_t **child;
26494276Staylor 	uint_t c, children;
26504276Staylor 	diskaddr_t sb = MAXOFFSET_T;
26514276Staylor 	uint64_t wholedisk;
26524276Staylor 
26534276Staylor 	if (nvlist_lookup_nvlist_array(config,
26544276Staylor 	    ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
26554276Staylor 		if (nvlist_lookup_uint64(config,
26564276Staylor 		    ZPOOL_CONFIG_WHOLE_DISK,
26574276Staylor 		    &wholedisk) != 0 || !wholedisk) {
26584276Staylor 			return (MAXOFFSET_T);
26594276Staylor 		}
26607042Sgw25295 		if (read_efi_label(config, &sb) < 0)
26617042Sgw25295 			sb = MAXOFFSET_T;
26624276Staylor 		return (sb);
26634276Staylor 	}
26644276Staylor 
26654276Staylor 	for (c = 0; c < children; c++) {
26664276Staylor 		sb = find_start_block(child[c]);
26674276Staylor 		if (sb != MAXOFFSET_T) {
26684276Staylor 			return (sb);
26694276Staylor 		}
26704276Staylor 	}
26714276Staylor 	return (MAXOFFSET_T);
26724276Staylor }
26734276Staylor 
26744276Staylor /*
26754276Staylor  * Label an individual disk.  The name provided is the short name,
26764276Staylor  * stripped of any leading /dev path.
26774276Staylor  */
26784276Staylor int
26794276Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
26804276Staylor {
26814276Staylor 	char path[MAXPATHLEN];
26824276Staylor 	struct dk_gpt *vtoc;
26834276Staylor 	int fd;
26844276Staylor 	size_t resv = EFI_MIN_RESV_SIZE;
26854276Staylor 	uint64_t slice_size;
26864276Staylor 	diskaddr_t start_block;
26874276Staylor 	char errbuf[1024];
26884276Staylor 
26896289Smmusante 	/* prepare an error message just in case */
26906289Smmusante 	(void) snprintf(errbuf, sizeof (errbuf),
26916289Smmusante 	    dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
26926289Smmusante 
26934276Staylor 	if (zhp) {
26944276Staylor 		nvlist_t *nvroot;
26954276Staylor 
26964276Staylor 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
26974276Staylor 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
26984276Staylor 
26994276Staylor 		if (zhp->zpool_start_block == 0)
27004276Staylor 			start_block = find_start_block(nvroot);
27014276Staylor 		else
27024276Staylor 			start_block = zhp->zpool_start_block;
27034276Staylor 		zhp->zpool_start_block = start_block;
27044276Staylor 	} else {
27054276Staylor 		/* new pool */
27064276Staylor 		start_block = NEW_START_BLOCK;
27074276Staylor 	}
27084276Staylor 
27094276Staylor 	(void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
27104276Staylor 	    BACKUP_SLICE);
27114276Staylor 
27124276Staylor 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
27134276Staylor 		/*
27144276Staylor 		 * This shouldn't happen.  We've long since verified that this
27154276Staylor 		 * is a valid device.
27164276Staylor 		 */
27176289Smmusante 		zfs_error_aux(hdl,
27186289Smmusante 		    dgettext(TEXT_DOMAIN, "unable to open device"));
27194276Staylor 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
27204276Staylor 	}
27214276Staylor 
27224276Staylor 	if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
27234276Staylor 		/*
27244276Staylor 		 * The only way this can fail is if we run out of memory, or we
27254276Staylor 		 * were unable to read the disk's capacity
27264276Staylor 		 */
27274276Staylor 		if (errno == ENOMEM)
27284276Staylor 			(void) no_memory(hdl);
27294276Staylor 
27304276Staylor 		(void) close(fd);
27316289Smmusante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27326289Smmusante 		    "unable to read disk capacity"), name);
27334276Staylor 
27344276Staylor 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
27354276Staylor 	}
27364276Staylor 
27374276Staylor 	slice_size = vtoc->efi_last_u_lba + 1;
27384276Staylor 	slice_size -= EFI_MIN_RESV_SIZE;
27394276Staylor 	if (start_block == MAXOFFSET_T)
27404276Staylor 		start_block = NEW_START_BLOCK;
27414276Staylor 	slice_size -= start_block;
27424276Staylor 
27434276Staylor 	vtoc->efi_parts[0].p_start = start_block;
27444276Staylor 	vtoc->efi_parts[0].p_size = slice_size;
27454276Staylor 
27464276Staylor 	/*
27474276Staylor 	 * Why we use V_USR: V_BACKUP confuses users, and is considered
27484276Staylor 	 * disposable by some EFI utilities (since EFI doesn't have a backup
27494276Staylor 	 * slice).  V_UNASSIGNED is supposed to be used only for zero size
27504276Staylor 	 * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
27514276Staylor 	 * etc. were all pretty specific.  V_USR is as close to reality as we
27524276Staylor 	 * can get, in the absence of V_OTHER.
27534276Staylor 	 */
27544276Staylor 	vtoc->efi_parts[0].p_tag = V_USR;
27554276Staylor 	(void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
27564276Staylor 
27574276Staylor 	vtoc->efi_parts[8].p_start = slice_size + start_block;
27584276Staylor 	vtoc->efi_parts[8].p_size = resv;
27594276Staylor 	vtoc->efi_parts[8].p_tag = V_RESERVED;
27604276Staylor 
27614276Staylor 	if (efi_write(fd, vtoc) != 0) {
27624276Staylor 		/*
27634276Staylor 		 * Some block drivers (like pcata) may not support EFI
27644276Staylor 		 * GPT labels.  Print out a helpful error message dir-
27654276Staylor 		 * ecting the user to manually label the disk and give
27664276Staylor 		 * a specific slice.
27674276Staylor 		 */
27684276Staylor 		(void) close(fd);
27694276Staylor 		efi_free(vtoc);
27704276Staylor 
27714276Staylor 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27726289Smmusante 		    "try using fdisk(1M) and then provide a specific slice"));
27734276Staylor 		return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
27744276Staylor 	}
27754276Staylor 
27764276Staylor 	(void) close(fd);
27774276Staylor 	efi_free(vtoc);
27784276Staylor 	return (0);
27794276Staylor }
27806423Sgw25295 
27816423Sgw25295 static boolean_t
27826423Sgw25295 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
27836423Sgw25295 {
27846423Sgw25295 	char *type;
27856423Sgw25295 	nvlist_t **child;
27866423Sgw25295 	uint_t children, c;
27876423Sgw25295 
27886423Sgw25295 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
27896423Sgw25295 	if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
27906423Sgw25295 	    strcmp(type, VDEV_TYPE_FILE) == 0 ||
27916423Sgw25295 	    strcmp(type, VDEV_TYPE_LOG) == 0 ||
27926423Sgw25295 	    strcmp(type, VDEV_TYPE_MISSING) == 0) {
27936423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27946423Sgw25295 		    "vdev type '%s' is not supported"), type);
27956423Sgw25295 		(void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
27966423Sgw25295 		return (B_FALSE);
27976423Sgw25295 	}
27986423Sgw25295 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
27996423Sgw25295 	    &child, &children) == 0) {
28006423Sgw25295 		for (c = 0; c < children; c++) {
28016423Sgw25295 			if (!supported_dump_vdev_type(hdl, child[c], errbuf))
28026423Sgw25295 				return (B_FALSE);
28036423Sgw25295 		}
28046423Sgw25295 	}
28056423Sgw25295 	return (B_TRUE);
28066423Sgw25295 }
28076423Sgw25295 
28086423Sgw25295 /*
28096423Sgw25295  * check if this zvol is allowable for use as a dump device; zero if
28106423Sgw25295  * it is, > 0 if it isn't, < 0 if it isn't a zvol
28116423Sgw25295  */
28126423Sgw25295 int
28136423Sgw25295 zvol_check_dump_config(char *arg)
28146423Sgw25295 {
28156423Sgw25295 	zpool_handle_t *zhp = NULL;
28166423Sgw25295 	nvlist_t *config, *nvroot;
28176423Sgw25295 	char *p, *volname;
28186423Sgw25295 	nvlist_t **top;
28196423Sgw25295 	uint_t toplevels;
28206423Sgw25295 	libzfs_handle_t *hdl;
28216423Sgw25295 	char errbuf[1024];
28226423Sgw25295 	char poolname[ZPOOL_MAXNAMELEN];
28236423Sgw25295 	int pathlen = strlen(ZVOL_FULL_DEV_DIR);
28246423Sgw25295 	int ret = 1;
28256423Sgw25295 
28266423Sgw25295 	if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
28276423Sgw25295 		return (-1);
28286423Sgw25295 	}
28296423Sgw25295 
28306423Sgw25295 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
28316423Sgw25295 	    "dump is not supported on device '%s'"), arg);
28326423Sgw25295 
28336423Sgw25295 	if ((hdl = libzfs_init()) == NULL)
28346423Sgw25295 		return (1);
28356423Sgw25295 	libzfs_print_on_error(hdl, B_TRUE);
28366423Sgw25295 
28376423Sgw25295 	volname = arg + pathlen;
28386423Sgw25295 
28396423Sgw25295 	/* check the configuration of the pool */
28406423Sgw25295 	if ((p = strchr(volname, '/')) == NULL) {
28416423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28426423Sgw25295 		    "malformed dataset name"));
28436423Sgw25295 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
28446423Sgw25295 		return (1);
28456423Sgw25295 	} else if (p - volname >= ZFS_MAXNAMELEN) {
28466423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28476423Sgw25295 		    "dataset name is too long"));
28486423Sgw25295 		(void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
28496423Sgw25295 		return (1);
28506423Sgw25295 	} else {
28516423Sgw25295 		(void) strncpy(poolname, volname, p - volname);
28526423Sgw25295 		poolname[p - volname] = '\0';
28536423Sgw25295 	}
28546423Sgw25295 
28556423Sgw25295 	if ((zhp = zpool_open(hdl, poolname)) == NULL) {
28566423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28576423Sgw25295 		    "could not open pool '%s'"), poolname);
28586423Sgw25295 		(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
28596423Sgw25295 		goto out;
28606423Sgw25295 	}
28616423Sgw25295 	config = zpool_get_config(zhp, NULL);
28626423Sgw25295 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
28636423Sgw25295 	    &nvroot) != 0) {
28646423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28656423Sgw25295 		    "could not obtain vdev configuration for  '%s'"), poolname);
28666423Sgw25295 		(void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
28676423Sgw25295 		goto out;
28686423Sgw25295 	}
28696423Sgw25295 
28706423Sgw25295 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
28716423Sgw25295 	    &top, &toplevels) == 0);
28726423Sgw25295 	if (toplevels != 1) {
28736423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28746423Sgw25295 		    "'%s' has multiple top level vdevs"), poolname);
28756423Sgw25295 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
28766423Sgw25295 		goto out;
28776423Sgw25295 	}
28786423Sgw25295 
28796423Sgw25295 	if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
28806423Sgw25295 		goto out;
28816423Sgw25295 	}
28826423Sgw25295 	ret = 0;
28836423Sgw25295 
28846423Sgw25295 out:
28856423Sgw25295 	if (zhp)
28866423Sgw25295 		zpool_close(zhp);
28876423Sgw25295 	libzfs_fini(hdl);
28886423Sgw25295 	return (ret);
28896423Sgw25295 }
2890