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 
2897300SEric.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,
1303*7326SEric.Schrock@Sun.COM     boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
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;
1311*7326SEric.Schrock@Sun.COM 	uint64_t is_log;
13121544Seschrock 
13132082Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &theguid) == 0);
13141544Seschrock 
13151544Seschrock 	if (search == NULL &&
13161544Seschrock 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &present) == 0) {
13171544Seschrock 		/*
13181544Seschrock 		 * If the device has never been present since import, the only
13191544Seschrock 		 * reliable way to match the vdev is by GUID.
13201544Seschrock 		 */
13212082Seschrock 		if (theguid == guid)
13222082Seschrock 			return (nv);
13231544Seschrock 	} else if (search != NULL &&
13241544Seschrock 	    nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
13251544Seschrock 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
13261544Seschrock 		    &wholedisk);
13271544Seschrock 		if (wholedisk) {
13281544Seschrock 			/*
13291544Seschrock 			 * For whole disks, the internal path has 's0', but the
13301544Seschrock 			 * path passed in by the user doesn't.
13311544Seschrock 			 */
13321544Seschrock 			if (strlen(search) == strlen(path) - 2 &&
13331544Seschrock 			    strncmp(search, path, strlen(search)) == 0)
13342082Seschrock 				return (nv);
13351544Seschrock 		} else if (strcmp(search, path) == 0) {
13362082Seschrock 			return (nv);
13371544Seschrock 		}
13381544Seschrock 	}
13391544Seschrock 
13401544Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
13411544Seschrock 	    &child, &children) != 0)
13422082Seschrock 		return (NULL);
13431544Seschrock 
1344*7326SEric.Schrock@Sun.COM 	for (c = 0; c < children; c++) {
13452082Seschrock 		if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
1346*7326SEric.Schrock@Sun.COM 		    avail_spare, l2cache, NULL)) != NULL) {
1347*7326SEric.Schrock@Sun.COM 			/*
1348*7326SEric.Schrock@Sun.COM 			 * The 'is_log' value is only set for the toplevel
1349*7326SEric.Schrock@Sun.COM 			 * vdev, not the leaf vdevs.  So we always lookup the
1350*7326SEric.Schrock@Sun.COM 			 * log device from the root of the vdev tree (where
1351*7326SEric.Schrock@Sun.COM 			 * 'log' is non-NULL).
1352*7326SEric.Schrock@Sun.COM 			 */
1353*7326SEric.Schrock@Sun.COM 			if (log != NULL &&
1354*7326SEric.Schrock@Sun.COM 			    nvlist_lookup_uint64(child[c],
1355*7326SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
1356*7326SEric.Schrock@Sun.COM 			    is_log) {
1357*7326SEric.Schrock@Sun.COM 				*log = B_TRUE;
1358*7326SEric.Schrock@Sun.COM 			}
13591544Seschrock 			return (ret);
1360*7326SEric.Schrock@Sun.COM 		}
1361*7326SEric.Schrock@Sun.COM 	}
13621544Seschrock 
13632082Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
13642082Seschrock 	    &child, &children) == 0) {
13652082Seschrock 		for (c = 0; c < children; c++) {
13662082Seschrock 			if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
1367*7326SEric.Schrock@Sun.COM 			    avail_spare, l2cache, NULL)) != NULL) {
13682468Sek110237 				*avail_spare = B_TRUE;
13692082Seschrock 				return (ret);
13702082Seschrock 			}
13712082Seschrock 		}
13722082Seschrock 	}
13732082Seschrock 
13745450Sbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
13755450Sbrendan 	    &child, &children) == 0) {
13765450Sbrendan 		for (c = 0; c < children; c++) {
13775450Sbrendan 			if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
1378*7326SEric.Schrock@Sun.COM 			    avail_spare, l2cache, NULL)) != NULL) {
13795450Sbrendan 				*l2cache = B_TRUE;
13805450Sbrendan 				return (ret);
13815450Sbrendan 			}
13825450Sbrendan 		}
13835450Sbrendan 	}
13845450Sbrendan 
13852082Seschrock 	return (NULL);
13861544Seschrock }
13871544Seschrock 
13882082Seschrock nvlist_t *
13895450Sbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
1390*7326SEric.Schrock@Sun.COM     boolean_t *l2cache, boolean_t *log)
13911544Seschrock {
13921544Seschrock 	char buf[MAXPATHLEN];
13931544Seschrock 	const char *search;
13941544Seschrock 	char *end;
13951544Seschrock 	nvlist_t *nvroot;
13961544Seschrock 	uint64_t guid;
13971544Seschrock 
13981613Seschrock 	guid = strtoull(path, &end, 10);
13991544Seschrock 	if (guid != 0 && *end == '\0') {
14001544Seschrock 		search = NULL;
14011544Seschrock 	} else if (path[0] != '/') {
14021544Seschrock 		(void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path);
14031544Seschrock 		search = buf;
14041544Seschrock 	} else {
14051544Seschrock 		search = path;
14061544Seschrock 	}
14071544Seschrock 
14081544Seschrock 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
14091544Seschrock 	    &nvroot) == 0);
14101544Seschrock 
14112468Sek110237 	*avail_spare = B_FALSE;
14125450Sbrendan 	*l2cache = B_FALSE;
1413*7326SEric.Schrock@Sun.COM 	if (log != NULL)
1414*7326SEric.Schrock@Sun.COM 		*log = B_FALSE;
14155450Sbrendan 	return (vdev_to_nvlist_iter(nvroot, search, guid, avail_spare,
1416*7326SEric.Schrock@Sun.COM 	    l2cache, log));
14172468Sek110237 }
14182468Sek110237 
14192468Sek110237 /*
14205450Sbrendan  * Returns TRUE if the given guid corresponds to the given type.
14215450Sbrendan  * This is used to check for hot spares (INUSE or not), and level 2 cache
14225450Sbrendan  * devices.
14232468Sek110237  */
14242468Sek110237 static boolean_t
14255450Sbrendan is_guid_type(zpool_handle_t *zhp, uint64_t guid, const char *type)
14262468Sek110237 {
14275450Sbrendan 	uint64_t target_guid;
14282468Sek110237 	nvlist_t *nvroot;
14295450Sbrendan 	nvlist_t **list;
14305450Sbrendan 	uint_t count;
14312468Sek110237 	int i;
14322468Sek110237 
14332468Sek110237 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
14342468Sek110237 	    &nvroot) == 0);
14355450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, type, &list, &count) == 0) {
14365450Sbrendan 		for (i = 0; i < count; i++) {
14375450Sbrendan 			verify(nvlist_lookup_uint64(list[i], ZPOOL_CONFIG_GUID,
14385450Sbrendan 			    &target_guid) == 0);
14395450Sbrendan 			if (guid == target_guid)
14402468Sek110237 				return (B_TRUE);
14412468Sek110237 		}
14422468Sek110237 	}
14432468Sek110237 
14442468Sek110237 	return (B_FALSE);
14451544Seschrock }
14461544Seschrock 
1447789Sahrens /*
14484451Seschrock  * Bring the specified vdev online.   The 'flags' parameter is a set of the
14494451Seschrock  * ZFS_ONLINE_* flags.
1450789Sahrens  */
1451789Sahrens int
14524451Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
14534451Seschrock     vdev_state_t *newstate)
1454789Sahrens {
1455789Sahrens 	zfs_cmd_t zc = { 0 };
1456789Sahrens 	char msg[1024];
14572082Seschrock 	nvlist_t *tgt;
14585450Sbrendan 	boolean_t avail_spare, l2cache;
14592082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1460789Sahrens 
14611544Seschrock 	(void) snprintf(msg, sizeof (msg),
14621544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot online %s"), path);
1463789Sahrens 
14641544Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1465*7326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
1466*7326SEric.Schrock@Sun.COM 	    NULL)) == NULL)
14672082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
1468789Sahrens 
14692468Sek110237 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
14702468Sek110237 
14715450Sbrendan 	if (avail_spare ||
14725450Sbrendan 	    is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
14732082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
14742082Seschrock 
14754451Seschrock 	zc.zc_cookie = VDEV_STATE_ONLINE;
14764451Seschrock 	zc.zc_obj = flags;
14774451Seschrock 
14784543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0)
14794451Seschrock 		return (zpool_standard_error(hdl, errno, msg));
14804451Seschrock 
14814451Seschrock 	*newstate = zc.zc_cookie;
14824451Seschrock 	return (0);
1483789Sahrens }
1484789Sahrens 
1485789Sahrens /*
1486789Sahrens  * Take the specified vdev offline
1487789Sahrens  */
1488789Sahrens int
14894451Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
1490789Sahrens {
1491789Sahrens 	zfs_cmd_t zc = { 0 };
1492789Sahrens 	char msg[1024];
14932082Seschrock 	nvlist_t *tgt;
14945450Sbrendan 	boolean_t avail_spare, l2cache;
14952082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1496789Sahrens 
14971544Seschrock 	(void) snprintf(msg, sizeof (msg),
14981544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
14991544Seschrock 
1500789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1501*7326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
1502*7326SEric.Schrock@Sun.COM 	    NULL)) == NULL)
15032082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
15042082Seschrock 
15052468Sek110237 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
15062468Sek110237 
15075450Sbrendan 	if (avail_spare ||
15085450Sbrendan 	    is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
15092082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
15102082Seschrock 
15114451Seschrock 	zc.zc_cookie = VDEV_STATE_OFFLINE;
15124451Seschrock 	zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
15131485Slling 
15144543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
1515789Sahrens 		return (0);
1516789Sahrens 
1517789Sahrens 	switch (errno) {
15182082Seschrock 	case EBUSY:
1519789Sahrens 
1520789Sahrens 		/*
1521789Sahrens 		 * There are no other replicas of this device.
1522789Sahrens 		 */
15232082Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
15242082Seschrock 
15252082Seschrock 	default:
15262082Seschrock 		return (zpool_standard_error(hdl, errno, msg));
15272082Seschrock 	}
15282082Seschrock }
1529789Sahrens 
15302082Seschrock /*
15314451Seschrock  * Mark the given vdev faulted.
15324451Seschrock  */
15334451Seschrock int
15344451Seschrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid)
15354451Seschrock {
15364451Seschrock 	zfs_cmd_t zc = { 0 };
15374451Seschrock 	char msg[1024];
15384451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
15394451Seschrock 
15404451Seschrock 	(void) snprintf(msg, sizeof (msg),
15414451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
15424451Seschrock 
15434451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
15444451Seschrock 	zc.zc_guid = guid;
15454451Seschrock 	zc.zc_cookie = VDEV_STATE_FAULTED;
15464451Seschrock 
15474451Seschrock 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
15484451Seschrock 		return (0);
15494451Seschrock 
15504451Seschrock 	switch (errno) {
15514451Seschrock 	case EBUSY:
15524451Seschrock 
15534451Seschrock 		/*
15544451Seschrock 		 * There are no other replicas of this device.
15554451Seschrock 		 */
15564451Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
15574451Seschrock 
15584451Seschrock 	default:
15594451Seschrock 		return (zpool_standard_error(hdl, errno, msg));
15604451Seschrock 	}
15614451Seschrock 
15624451Seschrock }
15634451Seschrock 
15644451Seschrock /*
15654451Seschrock  * Mark the given vdev degraded.
15664451Seschrock  */
15674451Seschrock int
15684451Seschrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid)
15694451Seschrock {
15704451Seschrock 	zfs_cmd_t zc = { 0 };
15714451Seschrock 	char msg[1024];
15724451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
15734451Seschrock 
15744451Seschrock 	(void) snprintf(msg, sizeof (msg),
15754451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
15764451Seschrock 
15774451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
15784451Seschrock 	zc.zc_guid = guid;
15794451Seschrock 	zc.zc_cookie = VDEV_STATE_DEGRADED;
15804451Seschrock 
15814451Seschrock 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
15824451Seschrock 		return (0);
15834451Seschrock 
15844451Seschrock 	return (zpool_standard_error(hdl, errno, msg));
15854451Seschrock }
15864451Seschrock 
15874451Seschrock /*
15882082Seschrock  * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
15892082Seschrock  * a hot spare.
15902082Seschrock  */
15912082Seschrock static boolean_t
15922082Seschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
15932082Seschrock {
15942082Seschrock 	nvlist_t **child;
15952082Seschrock 	uint_t c, children;
15962082Seschrock 	char *type;
15972082Seschrock 
15982082Seschrock 	if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
15992082Seschrock 	    &children) == 0) {
16002082Seschrock 		verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
16012082Seschrock 		    &type) == 0);
16022082Seschrock 
16032082Seschrock 		if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
16042082Seschrock 		    children == 2 && child[which] == tgt)
16052082Seschrock 			return (B_TRUE);
16062082Seschrock 
16072082Seschrock 		for (c = 0; c < children; c++)
16082082Seschrock 			if (is_replacing_spare(child[c], tgt, which))
16092082Seschrock 				return (B_TRUE);
1610789Sahrens 	}
16112082Seschrock 
16122082Seschrock 	return (B_FALSE);
1613789Sahrens }
1614789Sahrens 
1615789Sahrens /*
1616789Sahrens  * Attach new_disk (fully described by nvroot) to old_disk.
16174527Sperrin  * If 'replacing' is specified, the new disk will replace the old one.
1618789Sahrens  */
1619789Sahrens int
1620789Sahrens zpool_vdev_attach(zpool_handle_t *zhp,
1621789Sahrens     const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
1622789Sahrens {
1623789Sahrens 	zfs_cmd_t zc = { 0 };
1624789Sahrens 	char msg[1024];
1625789Sahrens 	int ret;
16262082Seschrock 	nvlist_t *tgt;
1627*7326SEric.Schrock@Sun.COM 	boolean_t avail_spare, l2cache, islog;
1628*7326SEric.Schrock@Sun.COM 	uint64_t val;
16297041Seschrock 	char *path, *newname;
16302082Seschrock 	nvlist_t **child;
16312082Seschrock 	uint_t children;
16322082Seschrock 	nvlist_t *config_root;
16332082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1634789Sahrens 
16351544Seschrock 	if (replacing)
16361544Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
16371544Seschrock 		    "cannot replace %s with %s"), old_disk, new_disk);
16381544Seschrock 	else
16391544Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
16401544Seschrock 		    "cannot attach %s to %s"), new_disk, old_disk);
16411544Seschrock 
1642789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1643*7326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
1644*7326SEric.Schrock@Sun.COM 	    &islog)) == 0)
16452082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
16462082Seschrock 
16472468Sek110237 	if (avail_spare)
16482082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
16492082Seschrock 
16505450Sbrendan 	if (l2cache)
16515450Sbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
16525450Sbrendan 
16532082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
16542082Seschrock 	zc.zc_cookie = replacing;
16552082Seschrock 
16562082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
16572082Seschrock 	    &child, &children) != 0 || children != 1) {
16582082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16592082Seschrock 		    "new device must be a single disk"));
16602082Seschrock 		return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
16611544Seschrock 	}
16622082Seschrock 
16632082Seschrock 	verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
16642082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
16652082Seschrock 
16667041Seschrock 	if ((newname = zpool_vdev_name(NULL, NULL, child[0])) == NULL)
16677041Seschrock 		return (-1);
16687041Seschrock 
16692082Seschrock 	/*
16702082Seschrock 	 * If the target is a hot spare that has been swapped in, we can only
16712082Seschrock 	 * replace it with another hot spare.
16722082Seschrock 	 */
16732082Seschrock 	if (replacing &&
16742082Seschrock 	    nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
1675*7326SEric.Schrock@Sun.COM 	    (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
1676*7326SEric.Schrock@Sun.COM 	    NULL) == NULL || !avail_spare) &&
1677*7326SEric.Schrock@Sun.COM 	    is_replacing_spare(config_root, tgt, 1)) {
16782082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16792082Seschrock 		    "can only be replaced by another hot spare"));
16807041Seschrock 		free(newname);
16812082Seschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
16822082Seschrock 	}
16832082Seschrock 
16842082Seschrock 	/*
16852082Seschrock 	 * If we are attempting to replace a spare, it canot be applied to an
16862082Seschrock 	 * already spared device.
16872082Seschrock 	 */
16882082Seschrock 	if (replacing &&
16892082Seschrock 	    nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 &&
1690*7326SEric.Schrock@Sun.COM 	    zpool_find_vdev(zhp, newname, &avail_spare,
1691*7326SEric.Schrock@Sun.COM 	    &l2cache, NULL) != NULL && avail_spare &&
1692*7326SEric.Schrock@Sun.COM 	    is_replacing_spare(config_root, tgt, 0)) {
16932082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16942082Seschrock 		    "device has already been replaced with a spare"));
16957041Seschrock 		free(newname);
16962082Seschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
16972082Seschrock 	}
1698789Sahrens 
16997041Seschrock 	free(newname);
17007041Seschrock 
17015094Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
17022082Seschrock 		return (-1);
1703789Sahrens 
17044543Smarks 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc);
1705789Sahrens 
17062676Seschrock 	zcmd_free_nvlists(&zc);
1707789Sahrens 
1708789Sahrens 	if (ret == 0)
1709789Sahrens 		return (0);
1710789Sahrens 
1711789Sahrens 	switch (errno) {
17121544Seschrock 	case ENOTSUP:
1713789Sahrens 		/*
1714789Sahrens 		 * Can't attach to or replace this type of vdev.
1715789Sahrens 		 */
17164527Sperrin 		if (replacing) {
1717*7326SEric.Schrock@Sun.COM 			if (islog)
17184527Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17194527Sperrin 				    "cannot replace a log with a spare"));
17204527Sperrin 			else
17214527Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17224527Sperrin 				    "cannot replace a replacing device"));
17234527Sperrin 		} else {
17242082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17252082Seschrock 			    "can only attach to mirrors and top-level "
17262082Seschrock 			    "disks"));
17274527Sperrin 		}
17282082Seschrock 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
1729789Sahrens 		break;
1730789Sahrens 
17311544Seschrock 	case EINVAL:
1732789Sahrens 		/*
1733789Sahrens 		 * The new device must be a single disk.
1734789Sahrens 		 */
17352082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17362082Seschrock 		    "new device must be a single disk"));
17372082Seschrock 		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
1738789Sahrens 		break;
1739789Sahrens 
17401544Seschrock 	case EBUSY:
17412082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
17422082Seschrock 		    new_disk);
17432082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1744789Sahrens 		break;
1745789Sahrens 
17461544Seschrock 	case EOVERFLOW:
1747789Sahrens 		/*
1748789Sahrens 		 * The new device is too small.
1749789Sahrens 		 */
17502082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17512082Seschrock 		    "device is too small"));
17522082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1753789Sahrens 		break;
1754789Sahrens 
17551544Seschrock 	case EDOM:
1756789Sahrens 		/*
1757789Sahrens 		 * The new device has a different alignment requirement.
1758789Sahrens 		 */
17592082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17602082Seschrock 		    "devices have different sector alignment"));
17612082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1762789Sahrens 		break;
1763789Sahrens 
17641544Seschrock 	case ENAMETOOLONG:
1765789Sahrens 		/*
1766789Sahrens 		 * The resulting top-level vdev spec won't fit in the label.
1767789Sahrens 		 */
17682082Seschrock 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
1769789Sahrens 		break;
1770789Sahrens 
17711544Seschrock 	default:
17722082Seschrock 		(void) zpool_standard_error(hdl, errno, msg);
1773789Sahrens 	}
1774789Sahrens 
17752082Seschrock 	return (-1);
1776789Sahrens }
1777789Sahrens 
1778789Sahrens /*
1779789Sahrens  * Detach the specified device.
1780789Sahrens  */
1781789Sahrens int
1782789Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
1783789Sahrens {
1784789Sahrens 	zfs_cmd_t zc = { 0 };
1785789Sahrens 	char msg[1024];
17862082Seschrock 	nvlist_t *tgt;
17875450Sbrendan 	boolean_t avail_spare, l2cache;
17882082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1789789Sahrens 
17901544Seschrock 	(void) snprintf(msg, sizeof (msg),
17911544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
17921544Seschrock 
1793789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1794*7326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
1795*7326SEric.Schrock@Sun.COM 	    NULL)) == 0)
17962082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
1797789Sahrens 
17982468Sek110237 	if (avail_spare)
17992082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
18002082Seschrock 
18015450Sbrendan 	if (l2cache)
18025450Sbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
18035450Sbrendan 
18042082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
18052082Seschrock 
18064543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
1807789Sahrens 		return (0);
1808789Sahrens 
1809789Sahrens 	switch (errno) {
1810789Sahrens 
18111544Seschrock 	case ENOTSUP:
1812789Sahrens 		/*
1813789Sahrens 		 * Can't detach from this type of vdev.
1814789Sahrens 		 */
18152082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
18162082Seschrock 		    "applicable to mirror and replacing vdevs"));
18172082Seschrock 		(void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg);
1818789Sahrens 		break;
1819789Sahrens 
18201544Seschrock 	case EBUSY:
1821789Sahrens 		/*
1822789Sahrens 		 * There are no other replicas of this device.
1823789Sahrens 		 */
18242082Seschrock 		(void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
1825789Sahrens 		break;
1826789Sahrens 
18271544Seschrock 	default:
18282082Seschrock 		(void) zpool_standard_error(hdl, errno, msg);
18291544Seschrock 	}
18301544Seschrock 
18312082Seschrock 	return (-1);
18322082Seschrock }
18332082Seschrock 
18342082Seschrock /*
18355450Sbrendan  * Remove the given device.  Currently, this is supported only for hot spares
18365450Sbrendan  * and level 2 cache devices.
18372082Seschrock  */
18382082Seschrock int
18392082Seschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
18402082Seschrock {
18412082Seschrock 	zfs_cmd_t zc = { 0 };
18422082Seschrock 	char msg[1024];
18432082Seschrock 	nvlist_t *tgt;
18445450Sbrendan 	boolean_t avail_spare, l2cache;
18452082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
18462082Seschrock 
18472082Seschrock 	(void) snprintf(msg, sizeof (msg),
18482082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
18492082Seschrock 
18502082Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1851*7326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
1852*7326SEric.Schrock@Sun.COM 	    NULL)) == 0)
18532082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
18542082Seschrock 
18555450Sbrendan 	if (!avail_spare && !l2cache) {
18562082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18575450Sbrendan 		    "only inactive hot spares or cache devices "
18585450Sbrendan 		    "can be removed"));
18592082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
18602082Seschrock 	}
18612082Seschrock 
18622082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
18632082Seschrock 
18644543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
18652082Seschrock 		return (0);
18662082Seschrock 
18672082Seschrock 	return (zpool_standard_error(hdl, errno, msg));
18681544Seschrock }
18691544Seschrock 
18701544Seschrock /*
18711544Seschrock  * Clear the errors for the pool, or the particular device if specified.
18721544Seschrock  */
18731544Seschrock int
18741544Seschrock zpool_clear(zpool_handle_t *zhp, const char *path)
18751544Seschrock {
18761544Seschrock 	zfs_cmd_t zc = { 0 };
18771544Seschrock 	char msg[1024];
18782082Seschrock 	nvlist_t *tgt;
18795450Sbrendan 	boolean_t avail_spare, l2cache;
18802082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
18811544Seschrock 
18821544Seschrock 	if (path)
18831544Seschrock 		(void) snprintf(msg, sizeof (msg),
18841544Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
18852676Seschrock 		    path);
18861544Seschrock 	else
18871544Seschrock 		(void) snprintf(msg, sizeof (msg),
18881544Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
18891544Seschrock 		    zhp->zpool_name);
18901544Seschrock 
18911544Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
18922082Seschrock 	if (path) {
18935450Sbrendan 		if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
1894*7326SEric.Schrock@Sun.COM 		    &l2cache, NULL)) == 0)
18952082Seschrock 			return (zfs_error(hdl, EZFS_NODEVICE, msg));
18962082Seschrock 
18975450Sbrendan 		/*
18985450Sbrendan 		 * Don't allow error clearing for hot spares.  Do allow
18995450Sbrendan 		 * error clearing for l2cache devices.
19005450Sbrendan 		 */
19012468Sek110237 		if (avail_spare)
19022082Seschrock 			return (zfs_error(hdl, EZFS_ISSPARE, msg));
19032082Seschrock 
19042082Seschrock 		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
19052082Seschrock 		    &zc.zc_guid) == 0);
19061544Seschrock 	}
19071544Seschrock 
19084543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0)
19091544Seschrock 		return (0);
19101544Seschrock 
19112082Seschrock 	return (zpool_standard_error(hdl, errno, msg));
1912789Sahrens }
1913789Sahrens 
19143126Sahl /*
19154451Seschrock  * Similar to zpool_clear(), but takes a GUID (used by fmd).
19164451Seschrock  */
19174451Seschrock int
19184451Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
19194451Seschrock {
19204451Seschrock 	zfs_cmd_t zc = { 0 };
19214451Seschrock 	char msg[1024];
19224451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
19234451Seschrock 
19244451Seschrock 	(void) snprintf(msg, sizeof (msg),
19254451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
19264451Seschrock 	    guid);
19274451Seschrock 
19284451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
19294451Seschrock 	zc.zc_guid = guid;
19304451Seschrock 
19314451Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
19324451Seschrock 		return (0);
19334451Seschrock 
19344451Seschrock 	return (zpool_standard_error(hdl, errno, msg));
19354451Seschrock }
19364451Seschrock 
19374451Seschrock /*
19383126Sahl  * Iterate over all zvols in a given pool by walking the /dev/zvol/dsk/<pool>
19393126Sahl  * hierarchy.
19403126Sahl  */
19413126Sahl int
19423126Sahl zpool_iter_zvol(zpool_handle_t *zhp, int (*cb)(const char *, void *),
19433126Sahl     void *data)
1944789Sahrens {
19453126Sahl 	libzfs_handle_t *hdl = zhp->zpool_hdl;
19463126Sahl 	char (*paths)[MAXPATHLEN];
19473126Sahl 	size_t size = 4;
19483126Sahl 	int curr, fd, base, ret = 0;
19493126Sahl 	DIR *dirp;
19503126Sahl 	struct dirent *dp;
19513126Sahl 	struct stat st;
19523126Sahl 
19533126Sahl 	if ((base = open("/dev/zvol/dsk", O_RDONLY)) < 0)
19543126Sahl 		return (errno == ENOENT ? 0 : -1);
19553126Sahl 
19563126Sahl 	if (fstatat(base, zhp->zpool_name, &st, 0) != 0) {
19573126Sahl 		int err = errno;
19583126Sahl 		(void) close(base);
19593126Sahl 		return (err == ENOENT ? 0 : -1);
19603126Sahl 	}
1961789Sahrens 
1962789Sahrens 	/*
19633126Sahl 	 * Oddly this wasn't a directory -- ignore that failure since we
19643126Sahl 	 * know there are no links lower in the (non-existant) hierarchy.
1965789Sahrens 	 */
19663126Sahl 	if (!S_ISDIR(st.st_mode)) {
19673126Sahl 		(void) close(base);
19683126Sahl 		return (0);
19693126Sahl 	}
19703126Sahl 
19713126Sahl 	if ((paths = zfs_alloc(hdl, size * sizeof (paths[0]))) == NULL) {
19723126Sahl 		(void) close(base);
19733126Sahl 		return (-1);
1974789Sahrens 	}
1975789Sahrens 
19763126Sahl 	(void) strlcpy(paths[0], zhp->zpool_name, sizeof (paths[0]));
19773126Sahl 	curr = 0;
19783126Sahl 
19793126Sahl 	while (curr >= 0) {
19803126Sahl 		if (fstatat(base, paths[curr], &st, AT_SYMLINK_NOFOLLOW) != 0)
19813126Sahl 			goto err;
19823126Sahl 
19833126Sahl 		if (S_ISDIR(st.st_mode)) {
19843126Sahl 			if ((fd = openat(base, paths[curr], O_RDONLY)) < 0)
19853126Sahl 				goto err;
19863126Sahl 
19873126Sahl 			if ((dirp = fdopendir(fd)) == NULL) {
19883126Sahl 				(void) close(fd);
19893126Sahl 				goto err;
19903126Sahl 			}
19913126Sahl 
19923126Sahl 			while ((dp = readdir(dirp)) != NULL) {
19933126Sahl 				if (dp->d_name[0] == '.')
19943126Sahl 					continue;
19953126Sahl 
19963126Sahl 				if (curr + 1 == size) {
19973126Sahl 					paths = zfs_realloc(hdl, paths,
19983126Sahl 					    size * sizeof (paths[0]),
19993126Sahl 					    size * 2 * sizeof (paths[0]));
20003126Sahl 					if (paths == NULL) {
20013126Sahl 						(void) closedir(dirp);
20023126Sahl 						(void) close(fd);
20033126Sahl 						goto err;
20043126Sahl 					}
20053126Sahl 
20063126Sahl 					size *= 2;
20073126Sahl 				}
20083126Sahl 
20093126Sahl 				(void) strlcpy(paths[curr + 1], paths[curr],
20103126Sahl 				    sizeof (paths[curr + 1]));
20113126Sahl 				(void) strlcat(paths[curr], "/",
20123126Sahl 				    sizeof (paths[curr]));
20133126Sahl 				(void) strlcat(paths[curr], dp->d_name,
20143126Sahl 				    sizeof (paths[curr]));
20153126Sahl 				curr++;
20163126Sahl 			}
20173126Sahl 
20183126Sahl 			(void) closedir(dirp);
20193126Sahl 
20203126Sahl 		} else {
20213126Sahl 			if ((ret = cb(paths[curr], data)) != 0)
20223126Sahl 				break;
20233126Sahl 		}
20243126Sahl 
20253126Sahl 		curr--;
20263126Sahl 	}
20273126Sahl 
20283126Sahl 	free(paths);
20293126Sahl 	(void) close(base);
20303126Sahl 
20313126Sahl 	return (ret);
20323126Sahl 
20333126Sahl err:
20343126Sahl 	free(paths);
20353126Sahl 	(void) close(base);
20363126Sahl 	return (-1);
20373126Sahl }
20383126Sahl 
20393126Sahl typedef struct zvol_cb {
20403126Sahl 	zpool_handle_t *zcb_pool;
20413126Sahl 	boolean_t zcb_create;
20423126Sahl } zvol_cb_t;
20433126Sahl 
20443126Sahl /*ARGSUSED*/
20453126Sahl static int
20463126Sahl do_zvol_create(zfs_handle_t *zhp, void *data)
20473126Sahl {
20484657Sahrens 	int ret = 0;
20493126Sahl 
20504657Sahrens 	if (ZFS_IS_VOLUME(zhp)) {
20513126Sahl 		(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
20524657Sahrens 		ret = zfs_iter_snapshots(zhp, do_zvol_create, NULL);
20534657Sahrens 	}
20543126Sahl 
20554657Sahrens 	if (ret == 0)
20564657Sahrens 		ret = zfs_iter_filesystems(zhp, do_zvol_create, NULL);
2057789Sahrens 
2058789Sahrens 	zfs_close(zhp);
20593126Sahl 
2060789Sahrens 	return (ret);
2061789Sahrens }
2062789Sahrens 
2063789Sahrens /*
2064789Sahrens  * Iterate over all zvols in the pool and make any necessary minor nodes.
2065789Sahrens  */
2066789Sahrens int
2067789Sahrens zpool_create_zvol_links(zpool_handle_t *zhp)
2068789Sahrens {
2069789Sahrens 	zfs_handle_t *zfp;
2070789Sahrens 	int ret;
2071789Sahrens 
2072789Sahrens 	/*
2073789Sahrens 	 * If the pool is unavailable, just return success.
2074789Sahrens 	 */
20752082Seschrock 	if ((zfp = make_dataset_handle(zhp->zpool_hdl,
20762082Seschrock 	    zhp->zpool_name)) == NULL)
2077789Sahrens 		return (0);
2078789Sahrens 
20794657Sahrens 	ret = zfs_iter_filesystems(zfp, do_zvol_create, NULL);
2080789Sahrens 
2081789Sahrens 	zfs_close(zfp);
2082789Sahrens 	return (ret);
2083789Sahrens }
2084789Sahrens 
20853126Sahl static int
20863126Sahl do_zvol_remove(const char *dataset, void *data)
20873126Sahl {
20883126Sahl 	zpool_handle_t *zhp = data;
20893126Sahl 
20903126Sahl 	return (zvol_remove_link(zhp->zpool_hdl, dataset));
20913126Sahl }
20923126Sahl 
2093789Sahrens /*
20943126Sahl  * Iterate over all zvols in the pool and remove any minor nodes.  We iterate
20953126Sahl  * by examining the /dev links so that a corrupted pool doesn't impede this
20963126Sahl  * operation.
2097789Sahrens  */
2098789Sahrens int
2099789Sahrens zpool_remove_zvol_links(zpool_handle_t *zhp)
2100789Sahrens {
21013126Sahl 	return (zpool_iter_zvol(zhp, do_zvol_remove, zhp));
2102789Sahrens }
21031354Seschrock 
21041354Seschrock /*
21051354Seschrock  * Convert from a devid string to a path.
21061354Seschrock  */
21071354Seschrock static char *
21081354Seschrock devid_to_path(char *devid_str)
21091354Seschrock {
21101354Seschrock 	ddi_devid_t devid;
21111354Seschrock 	char *minor;
21121354Seschrock 	char *path;
21131354Seschrock 	devid_nmlist_t *list = NULL;
21141354Seschrock 	int ret;
21151354Seschrock 
21161354Seschrock 	if (devid_str_decode(devid_str, &devid, &minor) != 0)
21171354Seschrock 		return (NULL);
21181354Seschrock 
21191354Seschrock 	ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
21201354Seschrock 
21211354Seschrock 	devid_str_free(minor);
21221354Seschrock 	devid_free(devid);
21231354Seschrock 
21241354Seschrock 	if (ret != 0)
21251354Seschrock 		return (NULL);
21261354Seschrock 
21272082Seschrock 	if ((path = strdup(list[0].devname)) == NULL)
21282082Seschrock 		return (NULL);
21292082Seschrock 
21301354Seschrock 	devid_free_nmlist(list);
21311354Seschrock 
21321354Seschrock 	return (path);
21331354Seschrock }
21341354Seschrock 
21351354Seschrock /*
21361354Seschrock  * Convert from a path to a devid string.
21371354Seschrock  */
21381354Seschrock static char *
21391354Seschrock path_to_devid(const char *path)
21401354Seschrock {
21411354Seschrock 	int fd;
21421354Seschrock 	ddi_devid_t devid;
21431354Seschrock 	char *minor, *ret;
21441354Seschrock 
21451354Seschrock 	if ((fd = open(path, O_RDONLY)) < 0)
21461354Seschrock 		return (NULL);
21471354Seschrock 
21481354Seschrock 	minor = NULL;
21491354Seschrock 	ret = NULL;
21501354Seschrock 	if (devid_get(fd, &devid) == 0) {
21511354Seschrock 		if (devid_get_minor_name(fd, &minor) == 0)
21521354Seschrock 			ret = devid_str_encode(devid, minor);
21531354Seschrock 		if (minor != NULL)
21541354Seschrock 			devid_str_free(minor);
21551354Seschrock 		devid_free(devid);
21561354Seschrock 	}
21571354Seschrock 	(void) close(fd);
21581354Seschrock 
21591354Seschrock 	return (ret);
21601354Seschrock }
21611354Seschrock 
21621354Seschrock /*
21631354Seschrock  * Issue the necessary ioctl() to update the stored path value for the vdev.  We
21641354Seschrock  * ignore any failure here, since a common case is for an unprivileged user to
21651354Seschrock  * type 'zpool status', and we'll display the correct information anyway.
21661354Seschrock  */
21671354Seschrock static void
21681354Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
21691354Seschrock {
21701354Seschrock 	zfs_cmd_t zc = { 0 };
21711354Seschrock 
21721354Seschrock 	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
21732676Seschrock 	(void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
21741354Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
21751544Seschrock 	    &zc.zc_guid) == 0);
21761354Seschrock 
21772082Seschrock 	(void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
21781354Seschrock }
21791354Seschrock 
21801354Seschrock /*
21811354Seschrock  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
21821354Seschrock  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
21831354Seschrock  * We also check if this is a whole disk, in which case we strip off the
21841354Seschrock  * trailing 's0' slice name.
21851354Seschrock  *
21861354Seschrock  * This routine is also responsible for identifying when disks have been
21871354Seschrock  * reconfigured in a new location.  The kernel will have opened the device by
21881354Seschrock  * devid, but the path will still refer to the old location.  To catch this, we
21891354Seschrock  * first do a path -> devid translation (which is fast for the common case).  If
21901354Seschrock  * the devid matches, we're done.  If not, we do a reverse devid -> path
21911354Seschrock  * translation and issue the appropriate ioctl() to update the path of the vdev.
21921354Seschrock  * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
21931354Seschrock  * of these checks.
21941354Seschrock  */
21951354Seschrock char *
21962082Seschrock zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv)
21971354Seschrock {
21981354Seschrock 	char *path, *devid;
21991544Seschrock 	uint64_t value;
22001544Seschrock 	char buf[64];
22014451Seschrock 	vdev_stat_t *vs;
22024451Seschrock 	uint_t vsc;
22031354Seschrock 
22041544Seschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
22051544Seschrock 	    &value) == 0) {
22061544Seschrock 		verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
22071544Seschrock 		    &value) == 0);
22082856Snd150628 		(void) snprintf(buf, sizeof (buf), "%llu",
22092856Snd150628 		    (u_longlong_t)value);
22101544Seschrock 		path = buf;
22111544Seschrock 	} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
22121354Seschrock 
22134451Seschrock 		/*
22144451Seschrock 		 * If the device is dead (faulted, offline, etc) then don't
22154451Seschrock 		 * bother opening it.  Otherwise we may be forcing the user to
22164451Seschrock 		 * open a misbehaving device, which can have undesirable
22174451Seschrock 		 * effects.
22184451Seschrock 		 */
22194451Seschrock 		if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
22204451Seschrock 		    (uint64_t **)&vs, &vsc) != 0 ||
22214451Seschrock 		    vs->vs_state >= VDEV_STATE_DEGRADED) &&
22224451Seschrock 		    zhp != NULL &&
22231354Seschrock 		    nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
22241354Seschrock 			/*
22251354Seschrock 			 * Determine if the current path is correct.
22261354Seschrock 			 */
22271354Seschrock 			char *newdevid = path_to_devid(path);
22281354Seschrock 
22291354Seschrock 			if (newdevid == NULL ||
22301354Seschrock 			    strcmp(devid, newdevid) != 0) {
22311354Seschrock 				char *newpath;
22321354Seschrock 
22331354Seschrock 				if ((newpath = devid_to_path(devid)) != NULL) {
22341354Seschrock 					/*
22351354Seschrock 					 * Update the path appropriately.
22361354Seschrock 					 */
22371354Seschrock 					set_path(zhp, nv, newpath);
22382082Seschrock 					if (nvlist_add_string(nv,
22392082Seschrock 					    ZPOOL_CONFIG_PATH, newpath) == 0)
22402082Seschrock 						verify(nvlist_lookup_string(nv,
22412082Seschrock 						    ZPOOL_CONFIG_PATH,
22422082Seschrock 						    &path) == 0);
22431354Seschrock 					free(newpath);
22441354Seschrock 				}
22451354Seschrock 			}
22461354Seschrock 
22472082Seschrock 			if (newdevid)
22482082Seschrock 				devid_str_free(newdevid);
22491354Seschrock 		}
22501354Seschrock 
22511354Seschrock 		if (strncmp(path, "/dev/dsk/", 9) == 0)
22521354Seschrock 			path += 9;
22531354Seschrock 
22541354Seschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
22551544Seschrock 		    &value) == 0 && value) {
22562082Seschrock 			char *tmp = zfs_strdup(hdl, path);
22572082Seschrock 			if (tmp == NULL)
22582082Seschrock 				return (NULL);
22591354Seschrock 			tmp[strlen(path) - 2] = '\0';
22601354Seschrock 			return (tmp);
22611354Seschrock 		}
22621354Seschrock 	} else {
22631354Seschrock 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
22642082Seschrock 
22652082Seschrock 		/*
22662082Seschrock 		 * If it's a raidz device, we need to stick in the parity level.
22672082Seschrock 		 */
22682082Seschrock 		if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
22692082Seschrock 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
22702082Seschrock 			    &value) == 0);
22712082Seschrock 			(void) snprintf(buf, sizeof (buf), "%s%llu", path,
22722856Snd150628 			    (u_longlong_t)value);
22732082Seschrock 			path = buf;
22742082Seschrock 		}
22751354Seschrock 	}
22761354Seschrock 
22772082Seschrock 	return (zfs_strdup(hdl, path));
22781354Seschrock }
22791544Seschrock 
22801544Seschrock static int
22811544Seschrock zbookmark_compare(const void *a, const void *b)
22821544Seschrock {
22831544Seschrock 	return (memcmp(a, b, sizeof (zbookmark_t)));
22841544Seschrock }
22851544Seschrock 
22861544Seschrock /*
22871544Seschrock  * Retrieve the persistent error log, uniquify the members, and return to the
22881544Seschrock  * caller.
22891544Seschrock  */
22901544Seschrock int
22913444Sek110237 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
22921544Seschrock {
22931544Seschrock 	zfs_cmd_t zc = { 0 };
22941544Seschrock 	uint64_t count;
22952676Seschrock 	zbookmark_t *zb = NULL;
22963444Sek110237 	int i;
22971544Seschrock 
22981544Seschrock 	/*
22991544Seschrock 	 * Retrieve the raw error list from the kernel.  If the number of errors
23001544Seschrock 	 * has increased, allocate more space and continue until we get the
23011544Seschrock 	 * entire list.
23021544Seschrock 	 */
23031544Seschrock 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
23041544Seschrock 	    &count) == 0);
23054820Sek110237 	if (count == 0)
23064820Sek110237 		return (0);
23072676Seschrock 	if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
23082856Snd150628 	    count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
23092082Seschrock 		return (-1);
23102676Seschrock 	zc.zc_nvlist_dst_size = count;
23111544Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
23121544Seschrock 	for (;;) {
23132082Seschrock 		if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
23142082Seschrock 		    &zc) != 0) {
23152676Seschrock 			free((void *)(uintptr_t)zc.zc_nvlist_dst);
23161544Seschrock 			if (errno == ENOMEM) {
23173823Svb160487 				count = zc.zc_nvlist_dst_size;
23182676Seschrock 				if ((zc.zc_nvlist_dst = (uintptr_t)
23193823Svb160487 				    zfs_alloc(zhp->zpool_hdl, count *
23203823Svb160487 				    sizeof (zbookmark_t))) == (uintptr_t)NULL)
23212082Seschrock 					return (-1);
23221544Seschrock 			} else {
23231544Seschrock 				return (-1);
23241544Seschrock 			}
23251544Seschrock 		} else {
23261544Seschrock 			break;
23271544Seschrock 		}
23281544Seschrock 	}
23291544Seschrock 
23301544Seschrock 	/*
23311544Seschrock 	 * Sort the resulting bookmarks.  This is a little confusing due to the
23321544Seschrock 	 * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
23332676Seschrock 	 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
23341544Seschrock 	 * _not_ copied as part of the process.  So we point the start of our
23351544Seschrock 	 * array appropriate and decrement the total number of elements.
23361544Seschrock 	 */
23372676Seschrock 	zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
23382676Seschrock 	    zc.zc_nvlist_dst_size;
23392676Seschrock 	count -= zc.zc_nvlist_dst_size;
23401544Seschrock 
23411544Seschrock 	qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
23421544Seschrock 
23433444Sek110237 	verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
23441544Seschrock 
23451544Seschrock 	/*
23463444Sek110237 	 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
23471544Seschrock 	 */
23481544Seschrock 	for (i = 0; i < count; i++) {
23491544Seschrock 		nvlist_t *nv;
23501544Seschrock 
23513700Sek110237 		/* ignoring zb_blkid and zb_level for now */
23523700Sek110237 		if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
23533700Sek110237 		    zb[i-1].zb_object == zb[i].zb_object)
23541544Seschrock 			continue;
23551544Seschrock 
23563444Sek110237 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
23573444Sek110237 			goto nomem;
23583444Sek110237 		if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
23593444Sek110237 		    zb[i].zb_objset) != 0) {
23603444Sek110237 			nvlist_free(nv);
23612082Seschrock 			goto nomem;
23623444Sek110237 		}
23633444Sek110237 		if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
23643444Sek110237 		    zb[i].zb_object) != 0) {
23653444Sek110237 			nvlist_free(nv);
23663444Sek110237 			goto nomem;
23671544Seschrock 		}
23683444Sek110237 		if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
23693444Sek110237 			nvlist_free(nv);
23703444Sek110237 			goto nomem;
23713444Sek110237 		}
23723444Sek110237 		nvlist_free(nv);
23731544Seschrock 	}
23741544Seschrock 
23753265Sahrens 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
23761544Seschrock 	return (0);
23772082Seschrock 
23782082Seschrock nomem:
23792676Seschrock 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
23802082Seschrock 	return (no_memory(zhp->zpool_hdl));
23811544Seschrock }
23821760Seschrock 
23831760Seschrock /*
23841760Seschrock  * Upgrade a ZFS pool to the latest on-disk version.
23851760Seschrock  */
23861760Seschrock int
23875094Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
23881760Seschrock {
23891760Seschrock 	zfs_cmd_t zc = { 0 };
23902082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
23911760Seschrock 
23921760Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
23935094Slling 	zc.zc_cookie = new_version;
23945094Slling 
23954543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
23963237Slling 		return (zpool_standard_error_fmt(hdl, errno,
23972082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
23982082Seschrock 		    zhp->zpool_name));
23991760Seschrock 	return (0);
24001760Seschrock }
24012926Sek110237 
24024988Sek110237 void
24034988Sek110237 zpool_set_history_str(const char *subcommand, int argc, char **argv,
24044988Sek110237     char *history_str)
24054988Sek110237 {
24064988Sek110237 	int i;
24074988Sek110237 
24084988Sek110237 	(void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
24094988Sek110237 	for (i = 1; i < argc; i++) {
24104988Sek110237 		if (strlen(history_str) + 1 + strlen(argv[i]) >
24114988Sek110237 		    HIS_MAX_RECORD_LEN)
24124988Sek110237 			break;
24134988Sek110237 		(void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
24144988Sek110237 		(void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
24154988Sek110237 	}
24164988Sek110237 }
24174988Sek110237 
24182926Sek110237 /*
24194988Sek110237  * Stage command history for logging.
24202926Sek110237  */
24214988Sek110237 int
24224988Sek110237 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
24232926Sek110237 {
24244988Sek110237 	if (history_str == NULL)
24254988Sek110237 		return (EINVAL);
24264988Sek110237 
24274988Sek110237 	if (strlen(history_str) > HIS_MAX_RECORD_LEN)
24284988Sek110237 		return (EINVAL);
24292926Sek110237 
24304715Sek110237 	if (hdl->libzfs_log_str != NULL)
24314543Smarks 		free(hdl->libzfs_log_str);
24322926Sek110237 
24334988Sek110237 	if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
24344988Sek110237 		return (no_memory(hdl));
24354543Smarks 
24364988Sek110237 	return (0);
24372926Sek110237 }
24382926Sek110237 
24392926Sek110237 /*
24402926Sek110237  * Perform ioctl to get some command history of a pool.
24412926Sek110237  *
24422926Sek110237  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
24432926Sek110237  * logical offset of the history buffer to start reading from.
24442926Sek110237  *
24452926Sek110237  * Upon return, 'off' is the next logical offset to read from and
24462926Sek110237  * 'len' is the actual amount of bytes read into 'buf'.
24472926Sek110237  */
24482926Sek110237 static int
24492926Sek110237 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
24502926Sek110237 {
24512926Sek110237 	zfs_cmd_t zc = { 0 };
24522926Sek110237 	libzfs_handle_t *hdl = zhp->zpool_hdl;
24532926Sek110237 
24542926Sek110237 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
24552926Sek110237 
24562926Sek110237 	zc.zc_history = (uint64_t)(uintptr_t)buf;
24572926Sek110237 	zc.zc_history_len = *len;
24582926Sek110237 	zc.zc_history_offset = *off;
24592926Sek110237 
24602926Sek110237 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
24612926Sek110237 		switch (errno) {
24622926Sek110237 		case EPERM:
24633237Slling 			return (zfs_error_fmt(hdl, EZFS_PERM,
24643237Slling 			    dgettext(TEXT_DOMAIN,
24652926Sek110237 			    "cannot show history for pool '%s'"),
24662926Sek110237 			    zhp->zpool_name));
24672926Sek110237 		case ENOENT:
24683237Slling 			return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
24692926Sek110237 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
24702926Sek110237 			    "'%s'"), zhp->zpool_name));
24713863Sek110237 		case ENOTSUP:
24723863Sek110237 			return (zfs_error_fmt(hdl, EZFS_BADVERSION,
24733863Sek110237 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
24743863Sek110237 			    "'%s', pool must be upgraded"), zhp->zpool_name));
24752926Sek110237 		default:
24763237Slling 			return (zpool_standard_error_fmt(hdl, errno,
24772926Sek110237 			    dgettext(TEXT_DOMAIN,
24782926Sek110237 			    "cannot get history for '%s'"), zhp->zpool_name));
24792926Sek110237 		}
24802926Sek110237 	}
24812926Sek110237 
24822926Sek110237 	*len = zc.zc_history_len;
24832926Sek110237 	*off = zc.zc_history_offset;
24842926Sek110237 
24852926Sek110237 	return (0);
24862926Sek110237 }
24872926Sek110237 
24882926Sek110237 /*
24892926Sek110237  * Process the buffer of nvlists, unpacking and storing each nvlist record
24902926Sek110237  * into 'records'.  'leftover' is set to the number of bytes that weren't
24912926Sek110237  * processed as there wasn't a complete record.
24922926Sek110237  */
24932926Sek110237 static int
24942926Sek110237 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
24952926Sek110237     nvlist_t ***records, uint_t *numrecords)
24962926Sek110237 {
24972926Sek110237 	uint64_t reclen;
24982926Sek110237 	nvlist_t *nv;
24992926Sek110237 	int i;
25002926Sek110237 
25012926Sek110237 	while (bytes_read > sizeof (reclen)) {
25022926Sek110237 
25032926Sek110237 		/* get length of packed record (stored as little endian) */
25042926Sek110237 		for (i = 0, reclen = 0; i < sizeof (reclen); i++)
25052926Sek110237 			reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
25062926Sek110237 
25072926Sek110237 		if (bytes_read < sizeof (reclen) + reclen)
25082926Sek110237 			break;
25092926Sek110237 
25102926Sek110237 		/* unpack record */
25112926Sek110237 		if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
25122926Sek110237 			return (ENOMEM);
25132926Sek110237 		bytes_read -= sizeof (reclen) + reclen;
25142926Sek110237 		buf += sizeof (reclen) + reclen;
25152926Sek110237 
25162926Sek110237 		/* add record to nvlist array */
25172926Sek110237 		(*numrecords)++;
25182926Sek110237 		if (ISP2(*numrecords + 1)) {
25192926Sek110237 			*records = realloc(*records,
25202926Sek110237 			    *numrecords * 2 * sizeof (nvlist_t *));
25212926Sek110237 		}
25222926Sek110237 		(*records)[*numrecords - 1] = nv;
25232926Sek110237 	}
25242926Sek110237 
25252926Sek110237 	*leftover = bytes_read;
25262926Sek110237 	return (0);
25272926Sek110237 }
25282926Sek110237 
25292926Sek110237 #define	HIS_BUF_LEN	(128*1024)
25302926Sek110237 
25312926Sek110237 /*
25322926Sek110237  * Retrieve the command history of a pool.
25332926Sek110237  */
25342926Sek110237 int
25352926Sek110237 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
25362926Sek110237 {
25372926Sek110237 	char buf[HIS_BUF_LEN];
25382926Sek110237 	uint64_t off = 0;
25392926Sek110237 	nvlist_t **records = NULL;
25402926Sek110237 	uint_t numrecords = 0;
25412926Sek110237 	int err, i;
25422926Sek110237 
25432926Sek110237 	do {
25442926Sek110237 		uint64_t bytes_read = sizeof (buf);
25452926Sek110237 		uint64_t leftover;
25462926Sek110237 
25472926Sek110237 		if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
25482926Sek110237 			break;
25492926Sek110237 
25502926Sek110237 		/* if nothing else was read in, we're at EOF, just return */
25512926Sek110237 		if (!bytes_read)
25522926Sek110237 			break;
25532926Sek110237 
25542926Sek110237 		if ((err = zpool_history_unpack(buf, bytes_read,
25552926Sek110237 		    &leftover, &records, &numrecords)) != 0)
25562926Sek110237 			break;
25572926Sek110237 		off -= leftover;
25582926Sek110237 
25592926Sek110237 		/* CONSTCOND */
25602926Sek110237 	} while (1);
25612926Sek110237 
25622926Sek110237 	if (!err) {
25632926Sek110237 		verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
25642926Sek110237 		verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
25652926Sek110237 		    records, numrecords) == 0);
25662926Sek110237 	}
25672926Sek110237 	for (i = 0; i < numrecords; i++)
25682926Sek110237 		nvlist_free(records[i]);
25692926Sek110237 	free(records);
25702926Sek110237 
25712926Sek110237 	return (err);
25722926Sek110237 }
25733444Sek110237 
25743444Sek110237 void
25753444Sek110237 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
25763444Sek110237     char *pathname, size_t len)
25773444Sek110237 {
25783444Sek110237 	zfs_cmd_t zc = { 0 };
25793444Sek110237 	boolean_t mounted = B_FALSE;
25803444Sek110237 	char *mntpnt = NULL;
25813444Sek110237 	char dsname[MAXNAMELEN];
25823444Sek110237 
25833444Sek110237 	if (dsobj == 0) {
25843444Sek110237 		/* special case for the MOS */
25853444Sek110237 		(void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
25863444Sek110237 		return;
25873444Sek110237 	}
25883444Sek110237 
25893444Sek110237 	/* get the dataset's name */
25903444Sek110237 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
25913444Sek110237 	zc.zc_obj = dsobj;
25923444Sek110237 	if (ioctl(zhp->zpool_hdl->libzfs_fd,
25933444Sek110237 	    ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
25943444Sek110237 		/* just write out a path of two object numbers */
25953444Sek110237 		(void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
25963444Sek110237 		    dsobj, obj);
25973444Sek110237 		return;
25983444Sek110237 	}
25993444Sek110237 	(void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
26003444Sek110237 
26013444Sek110237 	/* find out if the dataset is mounted */
26023444Sek110237 	mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
26033444Sek110237 
26043444Sek110237 	/* get the corrupted object's path */
26053444Sek110237 	(void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
26063444Sek110237 	zc.zc_obj = obj;
26073444Sek110237 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
26083444Sek110237 	    &zc) == 0) {
26093444Sek110237 		if (mounted) {
26103444Sek110237 			(void) snprintf(pathname, len, "%s%s", mntpnt,
26113444Sek110237 			    zc.zc_value);
26123444Sek110237 		} else {
26133444Sek110237 			(void) snprintf(pathname, len, "%s:%s",
26143444Sek110237 			    dsname, zc.zc_value);
26153444Sek110237 		}
26163444Sek110237 	} else {
26173444Sek110237 		(void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
26183444Sek110237 	}
26193444Sek110237 	free(mntpnt);
26203444Sek110237 }
26213912Slling 
26224276Staylor #define	RDISK_ROOT	"/dev/rdsk"
26234276Staylor #define	BACKUP_SLICE	"s2"
26244276Staylor /*
26254276Staylor  * Don't start the slice at the default block of 34; many storage
26264276Staylor  * devices will use a stripe width of 128k, so start there instead.
26274276Staylor  */
26284276Staylor #define	NEW_START_BLOCK	256
26294276Staylor 
26304276Staylor /*
26317042Sgw25295  * Read the EFI label from the config, if a label does not exist then
26327042Sgw25295  * pass back the error to the caller. If the caller has passed a non-NULL
26337042Sgw25295  * diskaddr argument then we set it to the starting address of the EFI
26347042Sgw25295  * partition.
26357042Sgw25295  */
26367042Sgw25295 static int
26377042Sgw25295 read_efi_label(nvlist_t *config, diskaddr_t *sb)
26387042Sgw25295 {
26397042Sgw25295 	char *path;
26407042Sgw25295 	int fd;
26417042Sgw25295 	char diskname[MAXPATHLEN];
26427042Sgw25295 	int err = -1;
26437042Sgw25295 
26447042Sgw25295 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
26457042Sgw25295 		return (err);
26467042Sgw25295 
26477042Sgw25295 	(void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
26487042Sgw25295 	    strrchr(path, '/'));
26497042Sgw25295 	if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
26507042Sgw25295 		struct dk_gpt *vtoc;
26517042Sgw25295 
26527042Sgw25295 		if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
26537042Sgw25295 			if (sb != NULL)
26547042Sgw25295 				*sb = vtoc->efi_parts[0].p_start;
26557042Sgw25295 			efi_free(vtoc);
26567042Sgw25295 		}
26577042Sgw25295 		(void) close(fd);
26587042Sgw25295 	}
26597042Sgw25295 	return (err);
26607042Sgw25295 }
26617042Sgw25295 
26627042Sgw25295 /*
26634276Staylor  * determine where a partition starts on a disk in the current
26644276Staylor  * configuration
26654276Staylor  */
26664276Staylor static diskaddr_t
26674276Staylor find_start_block(nvlist_t *config)
26684276Staylor {
26694276Staylor 	nvlist_t **child;
26704276Staylor 	uint_t c, children;
26714276Staylor 	diskaddr_t sb = MAXOFFSET_T;
26724276Staylor 	uint64_t wholedisk;
26734276Staylor 
26744276Staylor 	if (nvlist_lookup_nvlist_array(config,
26754276Staylor 	    ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
26764276Staylor 		if (nvlist_lookup_uint64(config,
26774276Staylor 		    ZPOOL_CONFIG_WHOLE_DISK,
26784276Staylor 		    &wholedisk) != 0 || !wholedisk) {
26794276Staylor 			return (MAXOFFSET_T);
26804276Staylor 		}
26817042Sgw25295 		if (read_efi_label(config, &sb) < 0)
26827042Sgw25295 			sb = MAXOFFSET_T;
26834276Staylor 		return (sb);
26844276Staylor 	}
26854276Staylor 
26864276Staylor 	for (c = 0; c < children; c++) {
26874276Staylor 		sb = find_start_block(child[c]);
26884276Staylor 		if (sb != MAXOFFSET_T) {
26894276Staylor 			return (sb);
26904276Staylor 		}
26914276Staylor 	}
26924276Staylor 	return (MAXOFFSET_T);
26934276Staylor }
26944276Staylor 
26954276Staylor /*
26964276Staylor  * Label an individual disk.  The name provided is the short name,
26974276Staylor  * stripped of any leading /dev path.
26984276Staylor  */
26994276Staylor int
27004276Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
27014276Staylor {
27024276Staylor 	char path[MAXPATHLEN];
27034276Staylor 	struct dk_gpt *vtoc;
27044276Staylor 	int fd;
27054276Staylor 	size_t resv = EFI_MIN_RESV_SIZE;
27064276Staylor 	uint64_t slice_size;
27074276Staylor 	diskaddr_t start_block;
27084276Staylor 	char errbuf[1024];
27094276Staylor 
27106289Smmusante 	/* prepare an error message just in case */
27116289Smmusante 	(void) snprintf(errbuf, sizeof (errbuf),
27126289Smmusante 	    dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
27136289Smmusante 
27144276Staylor 	if (zhp) {
27154276Staylor 		nvlist_t *nvroot;
27164276Staylor 
27174276Staylor 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
27184276Staylor 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
27194276Staylor 
27204276Staylor 		if (zhp->zpool_start_block == 0)
27214276Staylor 			start_block = find_start_block(nvroot);
27224276Staylor 		else
27234276Staylor 			start_block = zhp->zpool_start_block;
27244276Staylor 		zhp->zpool_start_block = start_block;
27254276Staylor 	} else {
27264276Staylor 		/* new pool */
27274276Staylor 		start_block = NEW_START_BLOCK;
27284276Staylor 	}
27294276Staylor 
27304276Staylor 	(void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
27314276Staylor 	    BACKUP_SLICE);
27324276Staylor 
27334276Staylor 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
27344276Staylor 		/*
27354276Staylor 		 * This shouldn't happen.  We've long since verified that this
27364276Staylor 		 * is a valid device.
27374276Staylor 		 */
27386289Smmusante 		zfs_error_aux(hdl,
27396289Smmusante 		    dgettext(TEXT_DOMAIN, "unable to open device"));
27404276Staylor 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
27414276Staylor 	}
27424276Staylor 
27434276Staylor 	if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
27444276Staylor 		/*
27454276Staylor 		 * The only way this can fail is if we run out of memory, or we
27464276Staylor 		 * were unable to read the disk's capacity
27474276Staylor 		 */
27484276Staylor 		if (errno == ENOMEM)
27494276Staylor 			(void) no_memory(hdl);
27504276Staylor 
27514276Staylor 		(void) close(fd);
27526289Smmusante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27536289Smmusante 		    "unable to read disk capacity"), name);
27544276Staylor 
27554276Staylor 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
27564276Staylor 	}
27574276Staylor 
27584276Staylor 	slice_size = vtoc->efi_last_u_lba + 1;
27594276Staylor 	slice_size -= EFI_MIN_RESV_SIZE;
27604276Staylor 	if (start_block == MAXOFFSET_T)
27614276Staylor 		start_block = NEW_START_BLOCK;
27624276Staylor 	slice_size -= start_block;
27634276Staylor 
27644276Staylor 	vtoc->efi_parts[0].p_start = start_block;
27654276Staylor 	vtoc->efi_parts[0].p_size = slice_size;
27664276Staylor 
27674276Staylor 	/*
27684276Staylor 	 * Why we use V_USR: V_BACKUP confuses users, and is considered
27694276Staylor 	 * disposable by some EFI utilities (since EFI doesn't have a backup
27704276Staylor 	 * slice).  V_UNASSIGNED is supposed to be used only for zero size
27714276Staylor 	 * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
27724276Staylor 	 * etc. were all pretty specific.  V_USR is as close to reality as we
27734276Staylor 	 * can get, in the absence of V_OTHER.
27744276Staylor 	 */
27754276Staylor 	vtoc->efi_parts[0].p_tag = V_USR;
27764276Staylor 	(void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
27774276Staylor 
27784276Staylor 	vtoc->efi_parts[8].p_start = slice_size + start_block;
27794276Staylor 	vtoc->efi_parts[8].p_size = resv;
27804276Staylor 	vtoc->efi_parts[8].p_tag = V_RESERVED;
27814276Staylor 
27824276Staylor 	if (efi_write(fd, vtoc) != 0) {
27834276Staylor 		/*
27844276Staylor 		 * Some block drivers (like pcata) may not support EFI
27854276Staylor 		 * GPT labels.  Print out a helpful error message dir-
27864276Staylor 		 * ecting the user to manually label the disk and give
27874276Staylor 		 * a specific slice.
27884276Staylor 		 */
27894276Staylor 		(void) close(fd);
27904276Staylor 		efi_free(vtoc);
27914276Staylor 
27924276Staylor 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27936289Smmusante 		    "try using fdisk(1M) and then provide a specific slice"));
27944276Staylor 		return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
27954276Staylor 	}
27964276Staylor 
27974276Staylor 	(void) close(fd);
27984276Staylor 	efi_free(vtoc);
27994276Staylor 	return (0);
28004276Staylor }
28016423Sgw25295 
28026423Sgw25295 static boolean_t
28036423Sgw25295 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
28046423Sgw25295 {
28056423Sgw25295 	char *type;
28066423Sgw25295 	nvlist_t **child;
28076423Sgw25295 	uint_t children, c;
28086423Sgw25295 
28096423Sgw25295 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
28106423Sgw25295 	if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
28116423Sgw25295 	    strcmp(type, VDEV_TYPE_FILE) == 0 ||
28126423Sgw25295 	    strcmp(type, VDEV_TYPE_LOG) == 0 ||
28136423Sgw25295 	    strcmp(type, VDEV_TYPE_MISSING) == 0) {
28146423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28156423Sgw25295 		    "vdev type '%s' is not supported"), type);
28166423Sgw25295 		(void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
28176423Sgw25295 		return (B_FALSE);
28186423Sgw25295 	}
28196423Sgw25295 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
28206423Sgw25295 	    &child, &children) == 0) {
28216423Sgw25295 		for (c = 0; c < children; c++) {
28226423Sgw25295 			if (!supported_dump_vdev_type(hdl, child[c], errbuf))
28236423Sgw25295 				return (B_FALSE);
28246423Sgw25295 		}
28256423Sgw25295 	}
28266423Sgw25295 	return (B_TRUE);
28276423Sgw25295 }
28286423Sgw25295 
28296423Sgw25295 /*
28306423Sgw25295  * check if this zvol is allowable for use as a dump device; zero if
28316423Sgw25295  * it is, > 0 if it isn't, < 0 if it isn't a zvol
28326423Sgw25295  */
28336423Sgw25295 int
28346423Sgw25295 zvol_check_dump_config(char *arg)
28356423Sgw25295 {
28366423Sgw25295 	zpool_handle_t *zhp = NULL;
28376423Sgw25295 	nvlist_t *config, *nvroot;
28386423Sgw25295 	char *p, *volname;
28396423Sgw25295 	nvlist_t **top;
28406423Sgw25295 	uint_t toplevels;
28416423Sgw25295 	libzfs_handle_t *hdl;
28426423Sgw25295 	char errbuf[1024];
28436423Sgw25295 	char poolname[ZPOOL_MAXNAMELEN];
28446423Sgw25295 	int pathlen = strlen(ZVOL_FULL_DEV_DIR);
28456423Sgw25295 	int ret = 1;
28466423Sgw25295 
28476423Sgw25295 	if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
28486423Sgw25295 		return (-1);
28496423Sgw25295 	}
28506423Sgw25295 
28516423Sgw25295 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
28526423Sgw25295 	    "dump is not supported on device '%s'"), arg);
28536423Sgw25295 
28546423Sgw25295 	if ((hdl = libzfs_init()) == NULL)
28556423Sgw25295 		return (1);
28566423Sgw25295 	libzfs_print_on_error(hdl, B_TRUE);
28576423Sgw25295 
28586423Sgw25295 	volname = arg + pathlen;
28596423Sgw25295 
28606423Sgw25295 	/* check the configuration of the pool */
28616423Sgw25295 	if ((p = strchr(volname, '/')) == NULL) {
28626423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28636423Sgw25295 		    "malformed dataset name"));
28646423Sgw25295 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
28656423Sgw25295 		return (1);
28666423Sgw25295 	} else if (p - volname >= ZFS_MAXNAMELEN) {
28676423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28686423Sgw25295 		    "dataset name is too long"));
28696423Sgw25295 		(void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
28706423Sgw25295 		return (1);
28716423Sgw25295 	} else {
28726423Sgw25295 		(void) strncpy(poolname, volname, p - volname);
28736423Sgw25295 		poolname[p - volname] = '\0';
28746423Sgw25295 	}
28756423Sgw25295 
28766423Sgw25295 	if ((zhp = zpool_open(hdl, poolname)) == NULL) {
28776423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28786423Sgw25295 		    "could not open pool '%s'"), poolname);
28796423Sgw25295 		(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
28806423Sgw25295 		goto out;
28816423Sgw25295 	}
28826423Sgw25295 	config = zpool_get_config(zhp, NULL);
28836423Sgw25295 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
28846423Sgw25295 	    &nvroot) != 0) {
28856423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28866423Sgw25295 		    "could not obtain vdev configuration for  '%s'"), poolname);
28876423Sgw25295 		(void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
28886423Sgw25295 		goto out;
28896423Sgw25295 	}
28906423Sgw25295 
28916423Sgw25295 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
28926423Sgw25295 	    &top, &toplevels) == 0);
28936423Sgw25295 	if (toplevels != 1) {
28946423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28956423Sgw25295 		    "'%s' has multiple top level vdevs"), poolname);
28966423Sgw25295 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
28976423Sgw25295 		goto out;
28986423Sgw25295 	}
28996423Sgw25295 
29006423Sgw25295 	if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
29016423Sgw25295 		goto out;
29026423Sgw25295 	}
29036423Sgw25295 	ret = 0;
29046423Sgw25295 
29056423Sgw25295 out:
29066423Sgw25295 	if (zhp)
29076423Sgw25295 		zpool_close(zhp);
29086423Sgw25295 	libzfs_fini(hdl);
29096423Sgw25295 	return (ret);
29106423Sgw25295 }
2911