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 
527965SGeorge.Wilson@Sun.COM #if defined(__i386) || defined(__amd64)
537965SGeorge.Wilson@Sun.COM #define	BOOTCMD	"installgrub(1M)"
547965SGeorge.Wilson@Sun.COM #else
557965SGeorge.Wilson@Sun.COM #define	BOOTCMD	"installboot(1M)"
567965SGeorge.Wilson@Sun.COM #endif
577965SGeorge.Wilson@Sun.COM 
585094Slling /*
595094Slling  * ====================================================================
605094Slling  *   zpool property functions
615094Slling  * ====================================================================
625094Slling  */
635094Slling 
645094Slling static int
655094Slling zpool_get_all_props(zpool_handle_t *zhp)
665094Slling {
675094Slling 	zfs_cmd_t zc = { 0 };
685094Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
695094Slling 
705094Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
715094Slling 
725094Slling 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
735094Slling 		return (-1);
745094Slling 
755094Slling 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
765094Slling 		if (errno == ENOMEM) {
775094Slling 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
785094Slling 				zcmd_free_nvlists(&zc);
795094Slling 				return (-1);
805094Slling 			}
815094Slling 		} else {
825094Slling 			zcmd_free_nvlists(&zc);
835094Slling 			return (-1);
845094Slling 		}
855094Slling 	}
865094Slling 
875094Slling 	if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
885094Slling 		zcmd_free_nvlists(&zc);
895094Slling 		return (-1);
905094Slling 	}
915094Slling 
925094Slling 	zcmd_free_nvlists(&zc);
935094Slling 
945094Slling 	return (0);
955094Slling }
965094Slling 
975094Slling static int
985094Slling zpool_props_refresh(zpool_handle_t *zhp)
995094Slling {
1005094Slling 	nvlist_t *old_props;
1015094Slling 
1025094Slling 	old_props = zhp->zpool_props;
1035094Slling 
1045094Slling 	if (zpool_get_all_props(zhp) != 0)
1055094Slling 		return (-1);
1065094Slling 
1075094Slling 	nvlist_free(old_props);
1085094Slling 	return (0);
1095094Slling }
1105094Slling 
1115094Slling static char *
1125094Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
1135094Slling     zprop_source_t *src)
1145094Slling {
1155094Slling 	nvlist_t *nv, *nvl;
1165094Slling 	uint64_t ival;
1175094Slling 	char *value;
1185094Slling 	zprop_source_t source;
1195094Slling 
1205094Slling 	nvl = zhp->zpool_props;
1215094Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
1225094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
1235094Slling 		source = ival;
1245094Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1255094Slling 	} else {
1265094Slling 		source = ZPROP_SRC_DEFAULT;
1275094Slling 		if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
1285094Slling 			value = "-";
1295094Slling 	}
1305094Slling 
1315094Slling 	if (src)
1325094Slling 		*src = source;
1335094Slling 
1345094Slling 	return (value);
1355094Slling }
1365094Slling 
1375094Slling uint64_t
1385094Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
1395094Slling {
1405094Slling 	nvlist_t *nv, *nvl;
1415094Slling 	uint64_t value;
1425094Slling 	zprop_source_t source;
1435094Slling 
1447294Sperrin 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
1457294Sperrin 		/*
1467294Sperrin 		 * zpool_get_all_props() has most likely failed because
1477294Sperrin 		 * the pool is faulted, but if all we need is the top level
1487294Sperrin 		 * vdev's guid then get it from the zhp config nvlist.
1497294Sperrin 		 */
1507294Sperrin 		if ((prop == ZPOOL_PROP_GUID) &&
1517294Sperrin 		    (nvlist_lookup_nvlist(zhp->zpool_config,
1527294Sperrin 		    ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
1537294Sperrin 		    (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
1547294Sperrin 		    == 0)) {
1557294Sperrin 			return (value);
1567294Sperrin 		}
1575094Slling 		return (zpool_prop_default_numeric(prop));
1587294Sperrin 	}
1595094Slling 
1605094Slling 	nvl = zhp->zpool_props;
1615094Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
1625094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
1635094Slling 		source = value;
1645094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1655094Slling 	} else {
1665094Slling 		source = ZPROP_SRC_DEFAULT;
1675094Slling 		value = zpool_prop_default_numeric(prop);
1685094Slling 	}
1695094Slling 
1705094Slling 	if (src)
1715094Slling 		*src = source;
1725094Slling 
1735094Slling 	return (value);
1745094Slling }
1755094Slling 
1765094Slling /*
1775094Slling  * Map VDEV STATE to printed strings.
1785094Slling  */
1795094Slling char *
1805094Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
1815094Slling {
1825094Slling 	switch (state) {
1835094Slling 	case VDEV_STATE_CLOSED:
1845094Slling 	case VDEV_STATE_OFFLINE:
1855094Slling 		return (gettext("OFFLINE"));
1865094Slling 	case VDEV_STATE_REMOVED:
1875094Slling 		return (gettext("REMOVED"));
1885094Slling 	case VDEV_STATE_CANT_OPEN:
1897294Sperrin 		if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
1905094Slling 			return (gettext("FAULTED"));
1915094Slling 		else
1925094Slling 			return (gettext("UNAVAIL"));
1935094Slling 	case VDEV_STATE_FAULTED:
1945094Slling 		return (gettext("FAULTED"));
1955094Slling 	case VDEV_STATE_DEGRADED:
1965094Slling 		return (gettext("DEGRADED"));
1975094Slling 	case VDEV_STATE_HEALTHY:
1985094Slling 		return (gettext("ONLINE"));
1995094Slling 	}
2005094Slling 
2015094Slling 	return (gettext("UNKNOWN"));
2025094Slling }
2035094Slling 
2045094Slling /*
2055094Slling  * Get a zpool property value for 'prop' and return the value in
2065094Slling  * a pre-allocated buffer.
2075094Slling  */
2085094Slling int
2095094Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
2105094Slling     zprop_source_t *srctype)
2115094Slling {
2125094Slling 	uint64_t intval;
2135094Slling 	const char *strval;
2145094Slling 	zprop_source_t src = ZPROP_SRC_NONE;
2155094Slling 	nvlist_t *nvroot;
2165094Slling 	vdev_stat_t *vs;
2175094Slling 	uint_t vsc;
2185094Slling 
2195094Slling 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
2205094Slling 		if (prop == ZPOOL_PROP_NAME)
2215094Slling 			(void) strlcpy(buf, zpool_get_name(zhp), len);
2225094Slling 		else if (prop == ZPOOL_PROP_HEALTH)
2235094Slling 			(void) strlcpy(buf, "FAULTED", len);
2245094Slling 		else
2255094Slling 			(void) strlcpy(buf, "-", len);
2265094Slling 		return (0);
2275094Slling 	}
2285094Slling 
2295094Slling 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
2305094Slling 	    prop != ZPOOL_PROP_NAME)
2315094Slling 		return (-1);
2325094Slling 
2335094Slling 	switch (zpool_prop_get_type(prop)) {
2345094Slling 	case PROP_TYPE_STRING:
2355094Slling 		(void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
2365094Slling 		    len);
2375094Slling 		break;
2385094Slling 
2395094Slling 	case PROP_TYPE_NUMBER:
2405094Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
2415094Slling 
2425094Slling 		switch (prop) {
2435094Slling 		case ZPOOL_PROP_SIZE:
2445094Slling 		case ZPOOL_PROP_USED:
2455094Slling 		case ZPOOL_PROP_AVAILABLE:
2465094Slling 			(void) zfs_nicenum(intval, buf, len);
2475094Slling 			break;
2485094Slling 
2495094Slling 		case ZPOOL_PROP_CAPACITY:
2505094Slling 			(void) snprintf(buf, len, "%llu%%",
2515094Slling 			    (u_longlong_t)intval);
2525094Slling 			break;
2535094Slling 
2545094Slling 		case ZPOOL_PROP_HEALTH:
2555094Slling 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2565094Slling 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2575094Slling 			verify(nvlist_lookup_uint64_array(nvroot,
2585094Slling 			    ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
2595094Slling 
2605094Slling 			(void) strlcpy(buf, zpool_state_to_name(intval,
2615094Slling 			    vs->vs_aux), len);
2625094Slling 			break;
2635094Slling 		default:
2645094Slling 			(void) snprintf(buf, len, "%llu", intval);
2655094Slling 		}
2665094Slling 		break;
2675094Slling 
2685094Slling 	case PROP_TYPE_INDEX:
2695094Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
2705094Slling 		if (zpool_prop_index_to_string(prop, intval, &strval)
2715094Slling 		    != 0)
2725094Slling 			return (-1);
2735094Slling 		(void) strlcpy(buf, strval, len);
2745094Slling 		break;
2755094Slling 
2765094Slling 	default:
2775094Slling 		abort();
2785094Slling 	}
2795094Slling 
2805094Slling 	if (srctype)
2815094Slling 		*srctype = src;
2825094Slling 
2835094Slling 	return (0);
2845094Slling }
2855094Slling 
2865094Slling /*
2875094Slling  * Check if the bootfs name has the same pool name as it is set to.
2885094Slling  * Assuming bootfs is a valid dataset name.
2895094Slling  */
2905094Slling static boolean_t
2915094Slling bootfs_name_valid(const char *pool, char *bootfs)
2925094Slling {
2935094Slling 	int len = strlen(pool);
2945094Slling 
2957300SEric.Taylor@Sun.COM 	if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
2965094Slling 		return (B_FALSE);
2975094Slling 
2985094Slling 	if (strncmp(pool, bootfs, len) == 0 &&
2995094Slling 	    (bootfs[len] == '/' || bootfs[len] == '\0'))
3005094Slling 		return (B_TRUE);
3015094Slling 
3025094Slling 	return (B_FALSE);
3035094Slling }
3045094Slling 
3055094Slling /*
3067042Sgw25295  * Inspect the configuration to determine if any of the devices contain
3077042Sgw25295  * an EFI label.
3087042Sgw25295  */
3097042Sgw25295 static boolean_t
3107042Sgw25295 pool_uses_efi(nvlist_t *config)
3117042Sgw25295 {
3127042Sgw25295 	nvlist_t **child;
3137042Sgw25295 	uint_t c, children;
3147042Sgw25295 
3157042Sgw25295 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
3167042Sgw25295 	    &child, &children) != 0)
3177042Sgw25295 		return (read_efi_label(config, NULL) >= 0);
3187042Sgw25295 
3197042Sgw25295 	for (c = 0; c < children; c++) {
3207042Sgw25295 		if (pool_uses_efi(child[c]))
3217042Sgw25295 			return (B_TRUE);
3227042Sgw25295 	}
3237042Sgw25295 	return (B_FALSE);
3247042Sgw25295 }
3257042Sgw25295 
3267965SGeorge.Wilson@Sun.COM static boolean_t
3277965SGeorge.Wilson@Sun.COM pool_is_bootable(zpool_handle_t *zhp)
3287965SGeorge.Wilson@Sun.COM {
3297965SGeorge.Wilson@Sun.COM 	char bootfs[ZPOOL_MAXNAMELEN];
3307965SGeorge.Wilson@Sun.COM 
3317965SGeorge.Wilson@Sun.COM 	return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
3327965SGeorge.Wilson@Sun.COM 	    sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
3337965SGeorge.Wilson@Sun.COM 	    sizeof (bootfs)) != 0);
3347965SGeorge.Wilson@Sun.COM }
3357965SGeorge.Wilson@Sun.COM 
3367965SGeorge.Wilson@Sun.COM 
3377042Sgw25295 /*
3385094Slling  * Given an nvlist of zpool properties to be set, validate that they are
3395094Slling  * correct, and parse any numeric properties (index, boolean, etc) if they are
3405094Slling  * specified as strings.
3415094Slling  */
3425094Slling static nvlist_t *
3437184Stimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
3445094Slling     nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf)
3455094Slling {
3465094Slling 	nvpair_t *elem;
3475094Slling 	nvlist_t *retprops;
3485094Slling 	zpool_prop_t prop;
3495094Slling 	char *strval;
3505094Slling 	uint64_t intval;
3515363Seschrock 	char *slash;
3525363Seschrock 	struct stat64 statbuf;
3537042Sgw25295 	zpool_handle_t *zhp;
3547042Sgw25295 	nvlist_t *nvroot;
3555094Slling 
3565094Slling 	if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
3575094Slling 		(void) no_memory(hdl);
3585094Slling 		return (NULL);
3595094Slling 	}
3605094Slling 
3615094Slling 	elem = NULL;
3625094Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
3635094Slling 		const char *propname = nvpair_name(elem);
3645094Slling 
3655094Slling 		/*
3665094Slling 		 * Make sure this property is valid and applies to this type.
3675094Slling 		 */
3685094Slling 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
3695094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3705094Slling 			    "invalid property '%s'"), propname);
3715094Slling 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
3725094Slling 			goto error;
3735094Slling 		}
3745094Slling 
3755094Slling 		if (zpool_prop_readonly(prop)) {
3765094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
3775094Slling 			    "is readonly"), propname);
3785094Slling 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
3795094Slling 			goto error;
3805094Slling 		}
3815094Slling 
3825094Slling 		if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
3835094Slling 		    &strval, &intval, errbuf) != 0)
3845094Slling 			goto error;
3855094Slling 
3865094Slling 		/*
3875094Slling 		 * Perform additional checking for specific properties.
3885094Slling 		 */
3895094Slling 		switch (prop) {
3905094Slling 		case ZPOOL_PROP_VERSION:
3915094Slling 			if (intval < version || intval > SPA_VERSION) {
3925094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3935094Slling 				    "property '%s' number %d is invalid."),
3945094Slling 				    propname, intval);
3955094Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
3965094Slling 				goto error;
3975094Slling 			}
3985094Slling 			break;
3995094Slling 
4005094Slling 		case ZPOOL_PROP_BOOTFS:
4015094Slling 			if (create_or_import) {
4025094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4035094Slling 				    "property '%s' cannot be set at creation "
4045094Slling 				    "or import time"), propname);
4055094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
4065094Slling 				goto error;
4075094Slling 			}
4085094Slling 
4095094Slling 			if (version < SPA_VERSION_BOOTFS) {
4105094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4115094Slling 				    "pool must be upgraded to support "
4125094Slling 				    "'%s' property"), propname);
4135094Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4145094Slling 				goto error;
4155094Slling 			}
4165094Slling 
4175094Slling 			/*
4185094Slling 			 * bootfs property value has to be a dataset name and
4195094Slling 			 * the dataset has to be in the same pool as it sets to.
4205094Slling 			 */
4215094Slling 			if (strval[0] != '\0' && !bootfs_name_valid(poolname,
4225094Slling 			    strval)) {
4235094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
4245094Slling 				    "is an invalid name"), strval);
4255094Slling 				(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4265094Slling 				goto error;
4275094Slling 			}
4287042Sgw25295 
4297042Sgw25295 			if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
4307042Sgw25295 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4317042Sgw25295 				    "could not open pool '%s'"), poolname);
4327042Sgw25295 				(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
4337042Sgw25295 				goto error;
4347042Sgw25295 			}
4357042Sgw25295 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
4367042Sgw25295 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
4377042Sgw25295 
4387042Sgw25295 			/*
4397042Sgw25295 			 * bootfs property cannot be set on a disk which has
4407042Sgw25295 			 * been EFI labeled.
4417042Sgw25295 			 */
4427042Sgw25295 			if (pool_uses_efi(nvroot)) {
4437042Sgw25295 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4447042Sgw25295 				    "property '%s' not supported on "
4457042Sgw25295 				    "EFI labeled devices"), propname);
4467042Sgw25295 				(void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
4477042Sgw25295 				zpool_close(zhp);
4487042Sgw25295 				goto error;
4497042Sgw25295 			}
4507042Sgw25295 			zpool_close(zhp);
4515094Slling 			break;
4525094Slling 
4535094Slling 		case ZPOOL_PROP_ALTROOT:
4545094Slling 			if (!create_or_import) {
4555094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4565094Slling 				    "property '%s' can only be set during pool "
4575094Slling 				    "creation or import"), propname);
4585094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
4595094Slling 				goto error;
4605094Slling 			}
4615094Slling 
4625094Slling 			if (strval[0] != '/') {
4635094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4645094Slling 				    "bad alternate root '%s'"), strval);
4655094Slling 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
4665094Slling 				goto error;
4675094Slling 			}
4685094Slling 			break;
4695363Seschrock 
4705363Seschrock 		case ZPOOL_PROP_CACHEFILE:
4715363Seschrock 			if (strval[0] == '\0')
4725363Seschrock 				break;
4735363Seschrock 
4745363Seschrock 			if (strcmp(strval, "none") == 0)
4755363Seschrock 				break;
4765363Seschrock 
4775363Seschrock 			if (strval[0] != '/') {
4785363Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4795363Seschrock 				    "property '%s' must be empty, an "
4805363Seschrock 				    "absolute path, or 'none'"), propname);
4815363Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
4825363Seschrock 				goto error;
4835363Seschrock 			}
4845363Seschrock 
4855363Seschrock 			slash = strrchr(strval, '/');
4865363Seschrock 
4875363Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
4885363Seschrock 			    strcmp(slash, "/..") == 0) {
4895363Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4905363Seschrock 				    "'%s' is not a valid file"), strval);
4915363Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
4925363Seschrock 				goto error;
4935363Seschrock 			}
4945363Seschrock 
4955363Seschrock 			*slash = '\0';
4965363Seschrock 
4975621Seschrock 			if (strval[0] != '\0' &&
4985621Seschrock 			    (stat64(strval, &statbuf) != 0 ||
4995621Seschrock 			    !S_ISDIR(statbuf.st_mode))) {
5005363Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5015363Seschrock 				    "'%s' is not a valid directory"),
5025363Seschrock 				    strval);
5035363Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
5045363Seschrock 				goto error;
5055363Seschrock 			}
5065363Seschrock 
5075363Seschrock 			*slash = '/';
5085363Seschrock 			break;
5095094Slling 		}
5105094Slling 	}
5115094Slling 
5125094Slling 	return (retprops);
5135094Slling error:
5145094Slling 	nvlist_free(retprops);
5155094Slling 	return (NULL);
5165094Slling }
5175094Slling 
5185094Slling /*
5195094Slling  * Set zpool property : propname=propval.
5205094Slling  */
5215094Slling int
5225094Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
5235094Slling {
5245094Slling 	zfs_cmd_t zc = { 0 };
5255094Slling 	int ret = -1;
5265094Slling 	char errbuf[1024];
5275094Slling 	nvlist_t *nvl = NULL;
5285094Slling 	nvlist_t *realprops;
5295094Slling 	uint64_t version;
5305094Slling 
5315094Slling 	(void) snprintf(errbuf, sizeof (errbuf),
5325094Slling 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
5335094Slling 	    zhp->zpool_name);
5345094Slling 
5355094Slling 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp))
5365094Slling 		return (zfs_error(zhp->zpool_hdl, EZFS_POOLPROPS, errbuf));
5375094Slling 
5385094Slling 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5395094Slling 		return (no_memory(zhp->zpool_hdl));
5405094Slling 
5415094Slling 	if (nvlist_add_string(nvl, propname, propval) != 0) {
5425094Slling 		nvlist_free(nvl);
5435094Slling 		return (no_memory(zhp->zpool_hdl));
5445094Slling 	}
5455094Slling 
5465094Slling 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
5477184Stimh 	if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
5485094Slling 	    zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) {
5495094Slling 		nvlist_free(nvl);
5505094Slling 		return (-1);
5515094Slling 	}
5525094Slling 
5535094Slling 	nvlist_free(nvl);
5545094Slling 	nvl = realprops;
5555094Slling 
5565094Slling 	/*
5575094Slling 	 * Execute the corresponding ioctl() to set this property.
5585094Slling 	 */
5595094Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
5605094Slling 
5615094Slling 	if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
5625094Slling 		nvlist_free(nvl);
5635094Slling 		return (-1);
5645094Slling 	}
5655094Slling 
5665094Slling 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
5675094Slling 
5685094Slling 	zcmd_free_nvlists(&zc);
5695094Slling 	nvlist_free(nvl);
5705094Slling 
5715094Slling 	if (ret)
5725094Slling 		(void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
5735094Slling 	else
5745094Slling 		(void) zpool_props_refresh(zhp);
5755094Slling 
5765094Slling 	return (ret);
5775094Slling }
5785094Slling 
5795094Slling int
5805094Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
5815094Slling {
5825094Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
5835094Slling 	zprop_list_t *entry;
5845094Slling 	char buf[ZFS_MAXPROPLEN];
5855094Slling 
5865094Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
5875094Slling 		return (-1);
5885094Slling 
5895094Slling 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
5905094Slling 
5915094Slling 		if (entry->pl_fixed)
5925094Slling 			continue;
5935094Slling 
5945094Slling 		if (entry->pl_prop != ZPROP_INVAL &&
5955094Slling 		    zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
5965094Slling 		    NULL) == 0) {
5975094Slling 			if (strlen(buf) > entry->pl_width)
5985094Slling 				entry->pl_width = strlen(buf);
5995094Slling 		}
6005094Slling 	}
6015094Slling 
6025094Slling 	return (0);
6035094Slling }
6045094Slling 
6055094Slling 
606789Sahrens /*
607789Sahrens  * Validate the given pool name, optionally putting an extended error message in
608789Sahrens  * 'buf'.
609789Sahrens  */
6106423Sgw25295 boolean_t
6112082Seschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
612789Sahrens {
613789Sahrens 	namecheck_err_t why;
614789Sahrens 	char what;
6151773Seschrock 	int ret;
616789Sahrens 
6171773Seschrock 	ret = pool_namecheck(pool, &why, &what);
6181773Seschrock 
6191773Seschrock 	/*
6201773Seschrock 	 * The rules for reserved pool names were extended at a later point.
6211773Seschrock 	 * But we need to support users with existing pools that may now be
6221773Seschrock 	 * invalid.  So we only check for this expanded set of names during a
6231773Seschrock 	 * create (or import), and only in userland.
6241773Seschrock 	 */
6251773Seschrock 	if (ret == 0 && !isopen &&
6261773Seschrock 	    (strncmp(pool, "mirror", 6) == 0 ||
6271773Seschrock 	    strncmp(pool, "raidz", 5) == 0 ||
6284527Sperrin 	    strncmp(pool, "spare", 5) == 0 ||
6294527Sperrin 	    strcmp(pool, "log") == 0)) {
6306423Sgw25295 		if (hdl != NULL)
6316423Sgw25295 			zfs_error_aux(hdl,
6326423Sgw25295 			    dgettext(TEXT_DOMAIN, "name is reserved"));
6332082Seschrock 		return (B_FALSE);
6341773Seschrock 	}
6351773Seschrock 
6361773Seschrock 
6371773Seschrock 	if (ret != 0) {
6382082Seschrock 		if (hdl != NULL) {
639789Sahrens 			switch (why) {
6401003Slling 			case NAME_ERR_TOOLONG:
6412082Seschrock 				zfs_error_aux(hdl,
6421003Slling 				    dgettext(TEXT_DOMAIN, "name is too long"));
6431003Slling 				break;
6441003Slling 
645789Sahrens 			case NAME_ERR_INVALCHAR:
6462082Seschrock 				zfs_error_aux(hdl,
647789Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
648789Sahrens 				    "'%c' in pool name"), what);
649789Sahrens 				break;
650789Sahrens 
651789Sahrens 			case NAME_ERR_NOLETTER:
6522082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6532082Seschrock 				    "name must begin with a letter"));
654789Sahrens 				break;
655789Sahrens 
656789Sahrens 			case NAME_ERR_RESERVED:
6572082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6582082Seschrock 				    "name is reserved"));
659789Sahrens 				break;
660789Sahrens 
661789Sahrens 			case NAME_ERR_DISKLIKE:
6622082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6632082Seschrock 				    "pool name is reserved"));
664789Sahrens 				break;
6652856Snd150628 
6662856Snd150628 			case NAME_ERR_LEADING_SLASH:
6672856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6682856Snd150628 				    "leading slash in name"));
6692856Snd150628 				break;
6702856Snd150628 
6712856Snd150628 			case NAME_ERR_EMPTY_COMPONENT:
6722856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6732856Snd150628 				    "empty component in name"));
6742856Snd150628 				break;
6752856Snd150628 
6762856Snd150628 			case NAME_ERR_TRAILING_SLASH:
6772856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6782856Snd150628 				    "trailing slash in name"));
6792856Snd150628 				break;
6802856Snd150628 
6812856Snd150628 			case NAME_ERR_MULTIPLE_AT:
6822856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6832856Snd150628 				    "multiple '@' delimiters in name"));
6842856Snd150628 				break;
6852856Snd150628 
686789Sahrens 			}
687789Sahrens 		}
6882082Seschrock 		return (B_FALSE);
689789Sahrens 	}
690789Sahrens 
6912082Seschrock 	return (B_TRUE);
692789Sahrens }
693789Sahrens 
694789Sahrens /*
695789Sahrens  * Open a handle to the given pool, even if the pool is currently in the FAULTED
696789Sahrens  * state.
697789Sahrens  */
698789Sahrens zpool_handle_t *
6992082Seschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
700789Sahrens {
701789Sahrens 	zpool_handle_t *zhp;
7022142Seschrock 	boolean_t missing;
703789Sahrens 
704789Sahrens 	/*
705789Sahrens 	 * Make sure the pool name is valid.
706789Sahrens 	 */
7072082Seschrock 	if (!zpool_name_valid(hdl, B_TRUE, pool)) {
7083237Slling 		(void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
7092082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
7102082Seschrock 		    pool);
711789Sahrens 		return (NULL);
712789Sahrens 	}
713789Sahrens 
7142082Seschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
7152082Seschrock 		return (NULL);
716789Sahrens 
7172082Seschrock 	zhp->zpool_hdl = hdl;
718789Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
719789Sahrens 
7202142Seschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
7212142Seschrock 		zpool_close(zhp);
7222142Seschrock 		return (NULL);
7232142Seschrock 	}
7242142Seschrock 
7252142Seschrock 	if (missing) {
7265094Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
7273237Slling 		(void) zfs_error_fmt(hdl, EZFS_NOENT,
7285094Slling 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
7292142Seschrock 		zpool_close(zhp);
7302142Seschrock 		return (NULL);
731789Sahrens 	}
732789Sahrens 
733789Sahrens 	return (zhp);
734789Sahrens }
735789Sahrens 
736789Sahrens /*
737789Sahrens  * Like the above, but silent on error.  Used when iterating over pools (because
738789Sahrens  * the configuration cache may be out of date).
739789Sahrens  */
7402142Seschrock int
7412142Seschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
742789Sahrens {
743789Sahrens 	zpool_handle_t *zhp;
7442142Seschrock 	boolean_t missing;
745789Sahrens 
7462142Seschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
7472142Seschrock 		return (-1);
748789Sahrens 
7492082Seschrock 	zhp->zpool_hdl = hdl;
750789Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
751789Sahrens 
7522142Seschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
7532142Seschrock 		zpool_close(zhp);
7542142Seschrock 		return (-1);
755789Sahrens 	}
756789Sahrens 
7572142Seschrock 	if (missing) {
7582142Seschrock 		zpool_close(zhp);
7592142Seschrock 		*ret = NULL;
7602142Seschrock 		return (0);
7612142Seschrock 	}
7622142Seschrock 
7632142Seschrock 	*ret = zhp;
7642142Seschrock 	return (0);
765789Sahrens }
766789Sahrens 
767789Sahrens /*
768789Sahrens  * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
769789Sahrens  * state.
770789Sahrens  */
771789Sahrens zpool_handle_t *
7722082Seschrock zpool_open(libzfs_handle_t *hdl, const char *pool)
773789Sahrens {
774789Sahrens 	zpool_handle_t *zhp;
775789Sahrens 
7762082Seschrock 	if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
777789Sahrens 		return (NULL);
778789Sahrens 
779789Sahrens 	if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
7803237Slling 		(void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
7812082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
782789Sahrens 		zpool_close(zhp);
783789Sahrens 		return (NULL);
784789Sahrens 	}
785789Sahrens 
786789Sahrens 	return (zhp);
787789Sahrens }
788789Sahrens 
789789Sahrens /*
790789Sahrens  * Close the handle.  Simply frees the memory associated with the handle.
791789Sahrens  */
792789Sahrens void
793789Sahrens zpool_close(zpool_handle_t *zhp)
794789Sahrens {
795789Sahrens 	if (zhp->zpool_config)
796789Sahrens 		nvlist_free(zhp->zpool_config);
797952Seschrock 	if (zhp->zpool_old_config)
798952Seschrock 		nvlist_free(zhp->zpool_old_config);
7993912Slling 	if (zhp->zpool_props)
8003912Slling 		nvlist_free(zhp->zpool_props);
801789Sahrens 	free(zhp);
802789Sahrens }
803789Sahrens 
804789Sahrens /*
805789Sahrens  * Return the name of the pool.
806789Sahrens  */
807789Sahrens const char *
808789Sahrens zpool_get_name(zpool_handle_t *zhp)
809789Sahrens {
810789Sahrens 	return (zhp->zpool_name);
811789Sahrens }
812789Sahrens 
813789Sahrens 
814789Sahrens /*
815789Sahrens  * Return the state of the pool (ACTIVE or UNAVAILABLE)
816789Sahrens  */
817789Sahrens int
818789Sahrens zpool_get_state(zpool_handle_t *zhp)
819789Sahrens {
820789Sahrens 	return (zhp->zpool_state);
821789Sahrens }
822789Sahrens 
823789Sahrens /*
824789Sahrens  * Create the named pool, using the provided vdev list.  It is assumed
825789Sahrens  * that the consumer has already validated the contents of the nvlist, so we
826789Sahrens  * don't have to worry about error semantics.
827789Sahrens  */
828789Sahrens int
8292082Seschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
8307184Stimh     nvlist_t *props, nvlist_t *fsprops)
831789Sahrens {
832789Sahrens 	zfs_cmd_t zc = { 0 };
8337184Stimh 	nvlist_t *zc_fsprops = NULL;
8347184Stimh 	nvlist_t *zc_props = NULL;
8352082Seschrock 	char msg[1024];
8365094Slling 	char *altroot;
8377184Stimh 	int ret = -1;
8382082Seschrock 
8392082Seschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
8402082Seschrock 	    "cannot create '%s'"), pool);
841789Sahrens 
8422082Seschrock 	if (!zpool_name_valid(hdl, B_FALSE, pool))
8432082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
8442082Seschrock 
8455320Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
8465320Slling 		return (-1);
8475320Slling 
8487184Stimh 	if (props) {
8497184Stimh 		if ((zc_props = zpool_valid_proplist(hdl, pool, props,
8507184Stimh 		    SPA_VERSION_1, B_TRUE, msg)) == NULL) {
8517184Stimh 			goto create_failed;
8527184Stimh 		}
8535320Slling 	}
854789Sahrens 
8557184Stimh 	if (fsprops) {
8567184Stimh 		uint64_t zoned;
8577184Stimh 		char *zonestr;
8587184Stimh 
8597184Stimh 		zoned = ((nvlist_lookup_string(fsprops,
8607184Stimh 		    zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
8617184Stimh 		    strcmp(zonestr, "on") == 0);
8627184Stimh 
8637184Stimh 		if ((zc_fsprops = zfs_valid_proplist(hdl,
8647184Stimh 		    ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
8657184Stimh 			goto create_failed;
8667184Stimh 		}
8677184Stimh 		if (!zc_props &&
8687184Stimh 		    (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
8697184Stimh 			goto create_failed;
8707184Stimh 		}
8717184Stimh 		if (nvlist_add_nvlist(zc_props,
8727184Stimh 		    ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
8737184Stimh 			goto create_failed;
8747184Stimh 		}
8757184Stimh 	}
8767184Stimh 
8777184Stimh 	if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
8787184Stimh 		goto create_failed;
8797184Stimh 
880789Sahrens 	(void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
881789Sahrens 
8827184Stimh 	if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
8835320Slling 
8842676Seschrock 		zcmd_free_nvlists(&zc);
8857184Stimh 		nvlist_free(zc_props);
8867184Stimh 		nvlist_free(zc_fsprops);
8872082Seschrock 
888789Sahrens 		switch (errno) {
889789Sahrens 		case EBUSY:
890789Sahrens 			/*
891789Sahrens 			 * This can happen if the user has specified the same
892789Sahrens 			 * device multiple times.  We can't reliably detect this
893789Sahrens 			 * until we try to add it and see we already have a
894789Sahrens 			 * label.
895789Sahrens 			 */
8962082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8972082Seschrock 			    "one or more vdevs refer to the same device"));
8982082Seschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
899789Sahrens 
900789Sahrens 		case EOVERFLOW:
901789Sahrens 			/*
9022082Seschrock 			 * This occurs when one of the devices is below
903789Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
904789Sahrens 			 * device was the problem device since there's no
905789Sahrens 			 * reliable way to determine device size from userland.
906789Sahrens 			 */
907789Sahrens 			{
908789Sahrens 				char buf[64];
909789Sahrens 
910789Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
911789Sahrens 
9122082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9132082Seschrock 				    "one or more devices is less than the "
9142082Seschrock 				    "minimum size (%s)"), buf);
915789Sahrens 			}
9162082Seschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
917789Sahrens 
918789Sahrens 		case ENOSPC:
9192082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9202082Seschrock 			    "one or more devices is out of space"));
9212082Seschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
922789Sahrens 
9235450Sbrendan 		case ENOTBLK:
9245450Sbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9255450Sbrendan 			    "cache device must be a disk or disk slice"));
9265450Sbrendan 			return (zfs_error(hdl, EZFS_BADDEV, msg));
9275450Sbrendan 
928789Sahrens 		default:
9292082Seschrock 			return (zpool_standard_error(hdl, errno, msg));
930789Sahrens 		}
931789Sahrens 	}
932789Sahrens 
933789Sahrens 	/*
934789Sahrens 	 * If this is an alternate root pool, then we automatically set the
9352676Seschrock 	 * mountpoint of the root dataset to be '/'.
936789Sahrens 	 */
9375094Slling 	if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
9385094Slling 	    &altroot) == 0) {
939789Sahrens 		zfs_handle_t *zhp;
940789Sahrens 
9415094Slling 		verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
9422676Seschrock 		verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
9432676Seschrock 		    "/") == 0);
944789Sahrens 
945789Sahrens 		zfs_close(zhp);
946789Sahrens 	}
947789Sahrens 
9487184Stimh create_failed:
9495320Slling 	zcmd_free_nvlists(&zc);
9507184Stimh 	nvlist_free(zc_props);
9517184Stimh 	nvlist_free(zc_fsprops);
9527184Stimh 	return (ret);
953789Sahrens }
954789Sahrens 
955789Sahrens /*
956789Sahrens  * Destroy the given pool.  It is up to the caller to ensure that there are no
957789Sahrens  * datasets left in the pool.
958789Sahrens  */
959789Sahrens int
960789Sahrens zpool_destroy(zpool_handle_t *zhp)
961789Sahrens {
962789Sahrens 	zfs_cmd_t zc = { 0 };
963789Sahrens 	zfs_handle_t *zfp = NULL;
9642082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
9652082Seschrock 	char msg[1024];
966789Sahrens 
967789Sahrens 	if (zhp->zpool_state == POOL_STATE_ACTIVE &&
9682082Seschrock 	    (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name,
9692082Seschrock 	    ZFS_TYPE_FILESYSTEM)) == NULL)
970789Sahrens 		return (-1);
971789Sahrens 
9722856Snd150628 	if (zpool_remove_zvol_links(zhp) != 0)
973789Sahrens 		return (-1);
974789Sahrens 
975789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
976789Sahrens 
9774543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
9782082Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
9792082Seschrock 		    "cannot destroy '%s'"), zhp->zpool_name);
980789Sahrens 
9812082Seschrock 		if (errno == EROFS) {
9822082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9832082Seschrock 			    "one or more devices is read only"));
9842082Seschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
9852082Seschrock 		} else {
9862082Seschrock 			(void) zpool_standard_error(hdl, errno, msg);
987789Sahrens 		}
988789Sahrens 
989789Sahrens 		if (zfp)
990789Sahrens 			zfs_close(zfp);
991789Sahrens 		return (-1);
992789Sahrens 	}
993789Sahrens 
994789Sahrens 	if (zfp) {
995789Sahrens 		remove_mountpoint(zfp);
996789Sahrens 		zfs_close(zfp);
997789Sahrens 	}
998789Sahrens 
999789Sahrens 	return (0);
1000789Sahrens }
1001789Sahrens 
1002789Sahrens /*
1003789Sahrens  * Add the given vdevs to the pool.  The caller must have already performed the
1004789Sahrens  * necessary verification to ensure that the vdev specification is well-formed.
1005789Sahrens  */
1006789Sahrens int
1007789Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1008789Sahrens {
10092676Seschrock 	zfs_cmd_t zc = { 0 };
10102082Seschrock 	int ret;
10112082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
10122082Seschrock 	char msg[1024];
10135450Sbrendan 	nvlist_t **spares, **l2cache;
10145450Sbrendan 	uint_t nspares, nl2cache;
10152082Seschrock 
10162082Seschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
10172082Seschrock 	    "cannot add to '%s'"), zhp->zpool_name);
10182082Seschrock 
10195450Sbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
10205450Sbrendan 	    SPA_VERSION_SPARES &&
10212082Seschrock 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
10222082Seschrock 	    &spares, &nspares) == 0) {
10232082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
10242082Seschrock 		    "upgraded to add hot spares"));
10252082Seschrock 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
10262082Seschrock 	}
1027789Sahrens 
10287965SGeorge.Wilson@Sun.COM 	if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
10297965SGeorge.Wilson@Sun.COM 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
10307965SGeorge.Wilson@Sun.COM 		uint64_t s;
10317965SGeorge.Wilson@Sun.COM 
10327965SGeorge.Wilson@Sun.COM 		for (s = 0; s < nspares; s++) {
10337965SGeorge.Wilson@Sun.COM 			char *path;
10347965SGeorge.Wilson@Sun.COM 
10357965SGeorge.Wilson@Sun.COM 			if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
10367965SGeorge.Wilson@Sun.COM 			    &path) == 0 && pool_uses_efi(spares[s])) {
10377965SGeorge.Wilson@Sun.COM 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10387965SGeorge.Wilson@Sun.COM 				    "device '%s' contains an EFI label and "
10397965SGeorge.Wilson@Sun.COM 				    "cannot be used on root pools."),
10407965SGeorge.Wilson@Sun.COM 				    zpool_vdev_name(hdl, NULL, spares[s]));
10417965SGeorge.Wilson@Sun.COM 				return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
10427965SGeorge.Wilson@Sun.COM 			}
10437965SGeorge.Wilson@Sun.COM 		}
10447965SGeorge.Wilson@Sun.COM 	}
10457965SGeorge.Wilson@Sun.COM 
10465450Sbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
10475450Sbrendan 	    SPA_VERSION_L2CACHE &&
10485450Sbrendan 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
10495450Sbrendan 	    &l2cache, &nl2cache) == 0) {
10505450Sbrendan 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
10515450Sbrendan 		    "upgraded to add cache devices"));
10525450Sbrendan 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
10535450Sbrendan 	}
10545450Sbrendan 
10555094Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
10562082Seschrock 		return (-1);
1057789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1058789Sahrens 
10594543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1060789Sahrens 		switch (errno) {
1061789Sahrens 		case EBUSY:
1062789Sahrens 			/*
1063789Sahrens 			 * This can happen if the user has specified the same
1064789Sahrens 			 * device multiple times.  We can't reliably detect this
1065789Sahrens 			 * until we try to add it and see we already have a
1066789Sahrens 			 * label.
1067789Sahrens 			 */
10682082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10692082Seschrock 			    "one or more vdevs refer to the same device"));
10702082Seschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1071789Sahrens 			break;
1072789Sahrens 
1073789Sahrens 		case EOVERFLOW:
1074789Sahrens 			/*
1075789Sahrens 			 * This occurrs when one of the devices is below
1076789Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1077789Sahrens 			 * device was the problem device since there's no
1078789Sahrens 			 * reliable way to determine device size from userland.
1079789Sahrens 			 */
1080789Sahrens 			{
1081789Sahrens 				char buf[64];
1082789Sahrens 
1083789Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1084789Sahrens 
10852082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10862082Seschrock 				    "device is less than the minimum "
10872082Seschrock 				    "size (%s)"), buf);
1088789Sahrens 			}
10892082Seschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
10902082Seschrock 			break;
10912082Seschrock 
10922082Seschrock 		case ENOTSUP:
10932082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10944527Sperrin 			    "pool must be upgraded to add these vdevs"));
10952082Seschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, msg);
1096789Sahrens 			break;
1097789Sahrens 
10983912Slling 		case EDOM:
10993912Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11004527Sperrin 			    "root pool can not have multiple vdevs"
11014527Sperrin 			    " or separate logs"));
11023912Slling 			(void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
11033912Slling 			break;
11043912Slling 
11055450Sbrendan 		case ENOTBLK:
11065450Sbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11075450Sbrendan 			    "cache device must be a disk or disk slice"));
11085450Sbrendan 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
11095450Sbrendan 			break;
11105450Sbrendan 
1111789Sahrens 		default:
11122082Seschrock 			(void) zpool_standard_error(hdl, errno, msg);
1113789Sahrens 		}
1114789Sahrens 
11152082Seschrock 		ret = -1;
11162082Seschrock 	} else {
11172082Seschrock 		ret = 0;
1118789Sahrens 	}
1119789Sahrens 
11202676Seschrock 	zcmd_free_nvlists(&zc);
1121789Sahrens 
11222082Seschrock 	return (ret);
1123789Sahrens }
1124789Sahrens 
1125789Sahrens /*
1126789Sahrens  * Exports the pool from the system.  The caller must ensure that there are no
1127789Sahrens  * mounted datasets in the pool.
1128789Sahrens  */
1129789Sahrens int
11307214Slling zpool_export(zpool_handle_t *zhp, boolean_t force)
1131789Sahrens {
1132789Sahrens 	zfs_cmd_t zc = { 0 };
11337214Slling 	char msg[1024];
1134789Sahrens 
1135789Sahrens 	if (zpool_remove_zvol_links(zhp) != 0)
1136789Sahrens 		return (-1);
1137789Sahrens 
11387214Slling 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
11397214Slling 	    "cannot export '%s'"), zhp->zpool_name);
11407214Slling 
1141789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
11427214Slling 	zc.zc_cookie = force;
11437214Slling 
11447214Slling 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
11457214Slling 		switch (errno) {
11467214Slling 		case EXDEV:
11477214Slling 			zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
11487214Slling 			    "use '-f' to override the following errors:\n"
11497214Slling 			    "'%s' has an active shared spare which could be"
11507214Slling 			    " used by other pools once '%s' is exported."),
11517214Slling 			    zhp->zpool_name, zhp->zpool_name);
11527214Slling 			return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
11537214Slling 			    msg));
11547214Slling 		default:
11557214Slling 			return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
11567214Slling 			    msg));
11577214Slling 		}
11587214Slling 	}
11597214Slling 
1160789Sahrens 	return (0);
1161789Sahrens }
1162789Sahrens 
1163789Sahrens /*
11645094Slling  * zpool_import() is a contracted interface. Should be kept the same
11655094Slling  * if possible.
11665094Slling  *
11675094Slling  * Applications should use zpool_import_props() to import a pool with
11685094Slling  * new properties value to be set.
1169789Sahrens  */
1170789Sahrens int
11712082Seschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
11725094Slling     char *altroot)
11735094Slling {
11745094Slling 	nvlist_t *props = NULL;
11755094Slling 	int ret;
11765094Slling 
11775094Slling 	if (altroot != NULL) {
11785094Slling 		if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
11795094Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
11805094Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
11815094Slling 			    newname));
11825094Slling 		}
11835094Slling 
11845094Slling 		if (nvlist_add_string(props,
1185*8084SGeorge.Wilson@Sun.COM 		    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1186*8084SGeorge.Wilson@Sun.COM 		    nvlist_add_string(props,
1187*8084SGeorge.Wilson@Sun.COM 		    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
11885094Slling 			nvlist_free(props);
11895094Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
11905094Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
11915094Slling 			    newname));
11925094Slling 		}
11935094Slling 	}
11945094Slling 
11956643Seschrock 	ret = zpool_import_props(hdl, config, newname, props, B_FALSE);
11965094Slling 	if (props)
11975094Slling 		nvlist_free(props);
11985094Slling 	return (ret);
11995094Slling }
12005094Slling 
12015094Slling /*
12025094Slling  * Import the given pool using the known configuration and a list of
12035094Slling  * properties to be set. The configuration should have come from
12045094Slling  * zpool_find_import(). The 'newname' parameters control whether the pool
12055094Slling  * is imported with a different name.
12065094Slling  */
12075094Slling int
12085094Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
12096643Seschrock     nvlist_t *props, boolean_t importfaulted)
1210789Sahrens {
12112676Seschrock 	zfs_cmd_t zc = { 0 };
1212789Sahrens 	char *thename;
1213789Sahrens 	char *origname;
1214789Sahrens 	int ret;
12155094Slling 	char errbuf[1024];
1216789Sahrens 
1217789Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1218789Sahrens 	    &origname) == 0);
1219789Sahrens 
12205094Slling 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
12215094Slling 	    "cannot import pool '%s'"), origname);
12225094Slling 
1223789Sahrens 	if (newname != NULL) {
12242082Seschrock 		if (!zpool_name_valid(hdl, B_FALSE, newname))
12253237Slling 			return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
12262082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
12272082Seschrock 			    newname));
1228789Sahrens 		thename = (char *)newname;
1229789Sahrens 	} else {
1230789Sahrens 		thename = origname;
1231789Sahrens 	}
1232789Sahrens 
12335094Slling 	if (props) {
12345094Slling 		uint64_t version;
12355094Slling 
12365094Slling 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
12375094Slling 		    &version) == 0);
12385094Slling 
12397184Stimh 		if ((props = zpool_valid_proplist(hdl, origname,
12405320Slling 		    props, version, B_TRUE, errbuf)) == NULL) {
12415094Slling 			return (-1);
12425320Slling 		} else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
12435320Slling 			nvlist_free(props);
12445094Slling 			return (-1);
12455320Slling 		}
12465094Slling 	}
1247789Sahrens 
1248789Sahrens 	(void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1249789Sahrens 
1250789Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
12511544Seschrock 	    &zc.zc_guid) == 0);
1252789Sahrens 
12535320Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
12545320Slling 		nvlist_free(props);
12552082Seschrock 		return (-1);
12565320Slling 	}
1257789Sahrens 
12586643Seschrock 	zc.zc_cookie = (uint64_t)importfaulted;
1259789Sahrens 	ret = 0;
12604543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) {
1261789Sahrens 		char desc[1024];
1262789Sahrens 		if (newname == NULL)
1263789Sahrens 			(void) snprintf(desc, sizeof (desc),
1264789Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1265789Sahrens 			    thename);
1266789Sahrens 		else
1267789Sahrens 			(void) snprintf(desc, sizeof (desc),
1268789Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1269789Sahrens 			    origname, thename);
1270789Sahrens 
1271789Sahrens 		switch (errno) {
12721544Seschrock 		case ENOTSUP:
12731544Seschrock 			/*
12741544Seschrock 			 * Unsupported version.
12751544Seschrock 			 */
12762082Seschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, desc);
12771544Seschrock 			break;
12781544Seschrock 
12792174Seschrock 		case EINVAL:
12802174Seschrock 			(void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
12812174Seschrock 			break;
12822174Seschrock 
1283789Sahrens 		default:
12842082Seschrock 			(void) zpool_standard_error(hdl, errno, desc);
1285789Sahrens 		}
1286789Sahrens 
1287789Sahrens 		ret = -1;
1288789Sahrens 	} else {
1289789Sahrens 		zpool_handle_t *zhp;
12904543Smarks 
1291789Sahrens 		/*
1292789Sahrens 		 * This should never fail, but play it safe anyway.
1293789Sahrens 		 */
12942142Seschrock 		if (zpool_open_silent(hdl, thename, &zhp) != 0) {
12952142Seschrock 			ret = -1;
12962142Seschrock 		} else if (zhp != NULL) {
1297789Sahrens 			ret = zpool_create_zvol_links(zhp);
1298789Sahrens 			zpool_close(zhp);
1299789Sahrens 		}
13004543Smarks 
1301789Sahrens 	}
1302789Sahrens 
13032676Seschrock 	zcmd_free_nvlists(&zc);
13045320Slling 	nvlist_free(props);
13055320Slling 
1306789Sahrens 	return (ret);
1307789Sahrens }
1308789Sahrens 
1309789Sahrens /*
1310789Sahrens  * Scrub the pool.
1311789Sahrens  */
1312789Sahrens int
1313789Sahrens zpool_scrub(zpool_handle_t *zhp, pool_scrub_type_t type)
1314789Sahrens {
1315789Sahrens 	zfs_cmd_t zc = { 0 };
1316789Sahrens 	char msg[1024];
13172082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1318789Sahrens 
1319789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1320789Sahrens 	zc.zc_cookie = type;
1321789Sahrens 
13224543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCRUB, &zc) == 0)
1323789Sahrens 		return (0);
1324789Sahrens 
1325789Sahrens 	(void) snprintf(msg, sizeof (msg),
1326789Sahrens 	    dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1327789Sahrens 
13282082Seschrock 	if (errno == EBUSY)
13292082Seschrock 		return (zfs_error(hdl, EZFS_RESILVERING, msg));
13302082Seschrock 	else
13312082Seschrock 		return (zpool_standard_error(hdl, errno, msg));
1332789Sahrens }
1333789Sahrens 
13342468Sek110237 /*
13352468Sek110237  * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
13362468Sek110237  * spare; but FALSE if its an INUSE spare.
13372468Sek110237  */
13382082Seschrock static nvlist_t *
13392082Seschrock vdev_to_nvlist_iter(nvlist_t *nv, const char *search, uint64_t guid,
13407326SEric.Schrock@Sun.COM     boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
13411544Seschrock {
13421544Seschrock 	uint_t c, children;
13431544Seschrock 	nvlist_t **child;
13442082Seschrock 	uint64_t theguid, present;
13451544Seschrock 	char *path;
13461544Seschrock 	uint64_t wholedisk = 0;
13472082Seschrock 	nvlist_t *ret;
13487326SEric.Schrock@Sun.COM 	uint64_t is_log;
13491544Seschrock 
13502082Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &theguid) == 0);
13511544Seschrock 
13521544Seschrock 	if (search == NULL &&
13531544Seschrock 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &present) == 0) {
13541544Seschrock 		/*
13551544Seschrock 		 * If the device has never been present since import, the only
13561544Seschrock 		 * reliable way to match the vdev is by GUID.
13571544Seschrock 		 */
13582082Seschrock 		if (theguid == guid)
13592082Seschrock 			return (nv);
13601544Seschrock 	} else if (search != NULL &&
13611544Seschrock 	    nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
13621544Seschrock 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
13631544Seschrock 		    &wholedisk);
13641544Seschrock 		if (wholedisk) {
13651544Seschrock 			/*
13661544Seschrock 			 * For whole disks, the internal path has 's0', but the
13671544Seschrock 			 * path passed in by the user doesn't.
13681544Seschrock 			 */
13691544Seschrock 			if (strlen(search) == strlen(path) - 2 &&
13701544Seschrock 			    strncmp(search, path, strlen(search)) == 0)
13712082Seschrock 				return (nv);
13721544Seschrock 		} else if (strcmp(search, path) == 0) {
13732082Seschrock 			return (nv);
13741544Seschrock 		}
13751544Seschrock 	}
13761544Seschrock 
13771544Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
13781544Seschrock 	    &child, &children) != 0)
13792082Seschrock 		return (NULL);
13801544Seschrock 
13817326SEric.Schrock@Sun.COM 	for (c = 0; c < children; c++) {
13822082Seschrock 		if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
13837326SEric.Schrock@Sun.COM 		    avail_spare, l2cache, NULL)) != NULL) {
13847326SEric.Schrock@Sun.COM 			/*
13857326SEric.Schrock@Sun.COM 			 * The 'is_log' value is only set for the toplevel
13867326SEric.Schrock@Sun.COM 			 * vdev, not the leaf vdevs.  So we always lookup the
13877326SEric.Schrock@Sun.COM 			 * log device from the root of the vdev tree (where
13887326SEric.Schrock@Sun.COM 			 * 'log' is non-NULL).
13897326SEric.Schrock@Sun.COM 			 */
13907326SEric.Schrock@Sun.COM 			if (log != NULL &&
13917326SEric.Schrock@Sun.COM 			    nvlist_lookup_uint64(child[c],
13927326SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
13937326SEric.Schrock@Sun.COM 			    is_log) {
13947326SEric.Schrock@Sun.COM 				*log = B_TRUE;
13957326SEric.Schrock@Sun.COM 			}
13961544Seschrock 			return (ret);
13977326SEric.Schrock@Sun.COM 		}
13987326SEric.Schrock@Sun.COM 	}
13991544Seschrock 
14002082Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
14012082Seschrock 	    &child, &children) == 0) {
14022082Seschrock 		for (c = 0; c < children; c++) {
14032082Seschrock 			if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
14047326SEric.Schrock@Sun.COM 			    avail_spare, l2cache, NULL)) != NULL) {
14052468Sek110237 				*avail_spare = B_TRUE;
14062082Seschrock 				return (ret);
14072082Seschrock 			}
14082082Seschrock 		}
14092082Seschrock 	}
14102082Seschrock 
14115450Sbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
14125450Sbrendan 	    &child, &children) == 0) {
14135450Sbrendan 		for (c = 0; c < children; c++) {
14145450Sbrendan 			if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
14157326SEric.Schrock@Sun.COM 			    avail_spare, l2cache, NULL)) != NULL) {
14165450Sbrendan 				*l2cache = B_TRUE;
14175450Sbrendan 				return (ret);
14185450Sbrendan 			}
14195450Sbrendan 		}
14205450Sbrendan 	}
14215450Sbrendan 
14222082Seschrock 	return (NULL);
14231544Seschrock }
14241544Seschrock 
14252082Seschrock nvlist_t *
14265450Sbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
14277326SEric.Schrock@Sun.COM     boolean_t *l2cache, boolean_t *log)
14281544Seschrock {
14291544Seschrock 	char buf[MAXPATHLEN];
14301544Seschrock 	const char *search;
14311544Seschrock 	char *end;
14321544Seschrock 	nvlist_t *nvroot;
14331544Seschrock 	uint64_t guid;
14341544Seschrock 
14351613Seschrock 	guid = strtoull(path, &end, 10);
14361544Seschrock 	if (guid != 0 && *end == '\0') {
14371544Seschrock 		search = NULL;
14381544Seschrock 	} else if (path[0] != '/') {
14391544Seschrock 		(void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path);
14401544Seschrock 		search = buf;
14411544Seschrock 	} else {
14421544Seschrock 		search = path;
14431544Seschrock 	}
14441544Seschrock 
14451544Seschrock 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
14461544Seschrock 	    &nvroot) == 0);
14471544Seschrock 
14482468Sek110237 	*avail_spare = B_FALSE;
14495450Sbrendan 	*l2cache = B_FALSE;
14507326SEric.Schrock@Sun.COM 	if (log != NULL)
14517326SEric.Schrock@Sun.COM 		*log = B_FALSE;
14525450Sbrendan 	return (vdev_to_nvlist_iter(nvroot, search, guid, avail_spare,
14537326SEric.Schrock@Sun.COM 	    l2cache, log));
14542468Sek110237 }
14552468Sek110237 
14567656SSherry.Moore@Sun.COM static int
14577656SSherry.Moore@Sun.COM vdev_online(nvlist_t *nv)
14587656SSherry.Moore@Sun.COM {
14597656SSherry.Moore@Sun.COM 	uint64_t ival;
14607656SSherry.Moore@Sun.COM 
14617656SSherry.Moore@Sun.COM 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
14627656SSherry.Moore@Sun.COM 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
14637656SSherry.Moore@Sun.COM 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
14647656SSherry.Moore@Sun.COM 		return (0);
14657656SSherry.Moore@Sun.COM 
14667656SSherry.Moore@Sun.COM 	return (1);
14677656SSherry.Moore@Sun.COM }
14687656SSherry.Moore@Sun.COM 
14697656SSherry.Moore@Sun.COM /*
14707656SSherry.Moore@Sun.COM  * Get phys_path for a root pool
14717656SSherry.Moore@Sun.COM  * Return 0 on success; non-zeron on failure.
14727656SSherry.Moore@Sun.COM  */
14737656SSherry.Moore@Sun.COM int
14747656SSherry.Moore@Sun.COM zpool_get_physpath(zpool_handle_t *zhp, char *physpath)
14757656SSherry.Moore@Sun.COM {
14767656SSherry.Moore@Sun.COM 	nvlist_t *vdev_root;
14777656SSherry.Moore@Sun.COM 	nvlist_t **child;
14787656SSherry.Moore@Sun.COM 	uint_t count;
14797656SSherry.Moore@Sun.COM 	int i;
14807656SSherry.Moore@Sun.COM 
14817656SSherry.Moore@Sun.COM 	/*
14827656SSherry.Moore@Sun.COM 	 * Make sure this is a root pool, as phys_path doesn't mean
14837656SSherry.Moore@Sun.COM 	 * anything to a non-root pool.
14847656SSherry.Moore@Sun.COM 	 */
14857965SGeorge.Wilson@Sun.COM 	if (!pool_is_bootable(zhp))
14867656SSherry.Moore@Sun.COM 		return (-1);
14877656SSherry.Moore@Sun.COM 
14887656SSherry.Moore@Sun.COM 	verify(nvlist_lookup_nvlist(zhp->zpool_config,
14897656SSherry.Moore@Sun.COM 	    ZPOOL_CONFIG_VDEV_TREE, &vdev_root) == 0);
14907656SSherry.Moore@Sun.COM 
14917656SSherry.Moore@Sun.COM 	if (nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
14927656SSherry.Moore@Sun.COM 	    &child, &count) != 0)
14937656SSherry.Moore@Sun.COM 		return (-2);
14947656SSherry.Moore@Sun.COM 
14957656SSherry.Moore@Sun.COM 	for (i = 0; i < count; i++) {
14967656SSherry.Moore@Sun.COM 		nvlist_t **child2;
14977656SSherry.Moore@Sun.COM 		uint_t count2;
14987656SSherry.Moore@Sun.COM 		char *type;
14997656SSherry.Moore@Sun.COM 		char *tmppath;
15007656SSherry.Moore@Sun.COM 		int j;
15017656SSherry.Moore@Sun.COM 
15027656SSherry.Moore@Sun.COM 		if (nvlist_lookup_string(child[i], ZPOOL_CONFIG_TYPE, &type)
15037656SSherry.Moore@Sun.COM 		    != 0)
15047656SSherry.Moore@Sun.COM 			return (-3);
15057656SSherry.Moore@Sun.COM 
15067656SSherry.Moore@Sun.COM 		if (strcmp(type, VDEV_TYPE_DISK) == 0) {
15077656SSherry.Moore@Sun.COM 			if (!vdev_online(child[i]))
15087656SSherry.Moore@Sun.COM 				return (-8);
15097656SSherry.Moore@Sun.COM 			verify(nvlist_lookup_string(child[i],
15107656SSherry.Moore@Sun.COM 			    ZPOOL_CONFIG_PHYS_PATH, &tmppath) == 0);
15117656SSherry.Moore@Sun.COM 			(void) strncpy(physpath, tmppath, strlen(tmppath));
15127656SSherry.Moore@Sun.COM 		} else if (strcmp(type, VDEV_TYPE_MIRROR) == 0) {
15137656SSherry.Moore@Sun.COM 			if (nvlist_lookup_nvlist_array(child[i],
15147656SSherry.Moore@Sun.COM 			    ZPOOL_CONFIG_CHILDREN, &child2, &count2) != 0)
15157656SSherry.Moore@Sun.COM 				return (-4);
15167656SSherry.Moore@Sun.COM 
15177656SSherry.Moore@Sun.COM 			for (j = 0; j < count2; j++) {
15187656SSherry.Moore@Sun.COM 				if (!vdev_online(child2[j]))
15197656SSherry.Moore@Sun.COM 					return (-8);
15207656SSherry.Moore@Sun.COM 				if (nvlist_lookup_string(child2[j],
15217656SSherry.Moore@Sun.COM 				    ZPOOL_CONFIG_PHYS_PATH, &tmppath) != 0)
15227656SSherry.Moore@Sun.COM 					return (-5);
15237656SSherry.Moore@Sun.COM 
15247656SSherry.Moore@Sun.COM 				if ((strlen(physpath) + strlen(tmppath)) >
15257656SSherry.Moore@Sun.COM 				    MAXNAMELEN)
15267656SSherry.Moore@Sun.COM 					return (-6);
15277656SSherry.Moore@Sun.COM 
15287656SSherry.Moore@Sun.COM 				if (strlen(physpath) == 0) {
15297656SSherry.Moore@Sun.COM 					(void) strncpy(physpath, tmppath,
15307656SSherry.Moore@Sun.COM 					    strlen(tmppath));
15317656SSherry.Moore@Sun.COM 				} else {
15327656SSherry.Moore@Sun.COM 					(void) strcat(physpath, " ");
15337656SSherry.Moore@Sun.COM 					(void) strcat(physpath, tmppath);
15347656SSherry.Moore@Sun.COM 				}
15357656SSherry.Moore@Sun.COM 			}
15367656SSherry.Moore@Sun.COM 		} else {
15377656SSherry.Moore@Sun.COM 			return (-7);
15387656SSherry.Moore@Sun.COM 		}
15397656SSherry.Moore@Sun.COM 	}
15407656SSherry.Moore@Sun.COM 
15417656SSherry.Moore@Sun.COM 	return (0);
15427656SSherry.Moore@Sun.COM }
15437656SSherry.Moore@Sun.COM 
15442468Sek110237 /*
15455450Sbrendan  * Returns TRUE if the given guid corresponds to the given type.
15465450Sbrendan  * This is used to check for hot spares (INUSE or not), and level 2 cache
15475450Sbrendan  * devices.
15482468Sek110237  */
15492468Sek110237 static boolean_t
15505450Sbrendan is_guid_type(zpool_handle_t *zhp, uint64_t guid, const char *type)
15512468Sek110237 {
15525450Sbrendan 	uint64_t target_guid;
15532468Sek110237 	nvlist_t *nvroot;
15545450Sbrendan 	nvlist_t **list;
15555450Sbrendan 	uint_t count;
15562468Sek110237 	int i;
15572468Sek110237 
15582468Sek110237 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
15592468Sek110237 	    &nvroot) == 0);
15605450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, type, &list, &count) == 0) {
15615450Sbrendan 		for (i = 0; i < count; i++) {
15625450Sbrendan 			verify(nvlist_lookup_uint64(list[i], ZPOOL_CONFIG_GUID,
15635450Sbrendan 			    &target_guid) == 0);
15645450Sbrendan 			if (guid == target_guid)
15652468Sek110237 				return (B_TRUE);
15662468Sek110237 		}
15672468Sek110237 	}
15682468Sek110237 
15692468Sek110237 	return (B_FALSE);
15701544Seschrock }
15711544Seschrock 
1572789Sahrens /*
15734451Seschrock  * Bring the specified vdev online.   The 'flags' parameter is a set of the
15744451Seschrock  * ZFS_ONLINE_* flags.
1575789Sahrens  */
1576789Sahrens int
15774451Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
15784451Seschrock     vdev_state_t *newstate)
1579789Sahrens {
1580789Sahrens 	zfs_cmd_t zc = { 0 };
1581789Sahrens 	char msg[1024];
15822082Seschrock 	nvlist_t *tgt;
15835450Sbrendan 	boolean_t avail_spare, l2cache;
15842082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1585789Sahrens 
15861544Seschrock 	(void) snprintf(msg, sizeof (msg),
15871544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot online %s"), path);
1588789Sahrens 
15891544Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
15907326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
15917326SEric.Schrock@Sun.COM 	    NULL)) == NULL)
15922082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
1593789Sahrens 
15942468Sek110237 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
15952468Sek110237 
15965450Sbrendan 	if (avail_spare ||
15975450Sbrendan 	    is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
15982082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
15992082Seschrock 
16004451Seschrock 	zc.zc_cookie = VDEV_STATE_ONLINE;
16014451Seschrock 	zc.zc_obj = flags;
16024451Seschrock 
16034543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0)
16044451Seschrock 		return (zpool_standard_error(hdl, errno, msg));
16054451Seschrock 
16064451Seschrock 	*newstate = zc.zc_cookie;
16074451Seschrock 	return (0);
1608789Sahrens }
1609789Sahrens 
1610789Sahrens /*
1611789Sahrens  * Take the specified vdev offline
1612789Sahrens  */
1613789Sahrens int
16144451Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
1615789Sahrens {
1616789Sahrens 	zfs_cmd_t zc = { 0 };
1617789Sahrens 	char msg[1024];
16182082Seschrock 	nvlist_t *tgt;
16195450Sbrendan 	boolean_t avail_spare, l2cache;
16202082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1621789Sahrens 
16221544Seschrock 	(void) snprintf(msg, sizeof (msg),
16231544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
16241544Seschrock 
1625789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
16267326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
16277326SEric.Schrock@Sun.COM 	    NULL)) == NULL)
16282082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
16292082Seschrock 
16302468Sek110237 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
16312468Sek110237 
16325450Sbrendan 	if (avail_spare ||
16335450Sbrendan 	    is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
16342082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
16352082Seschrock 
16364451Seschrock 	zc.zc_cookie = VDEV_STATE_OFFLINE;
16374451Seschrock 	zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
16381485Slling 
16394543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
1640789Sahrens 		return (0);
1641789Sahrens 
1642789Sahrens 	switch (errno) {
16432082Seschrock 	case EBUSY:
1644789Sahrens 
1645789Sahrens 		/*
1646789Sahrens 		 * There are no other replicas of this device.
1647789Sahrens 		 */
16482082Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
16492082Seschrock 
16502082Seschrock 	default:
16512082Seschrock 		return (zpool_standard_error(hdl, errno, msg));
16522082Seschrock 	}
16532082Seschrock }
1654789Sahrens 
16552082Seschrock /*
16564451Seschrock  * Mark the given vdev faulted.
16574451Seschrock  */
16584451Seschrock int
16594451Seschrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid)
16604451Seschrock {
16614451Seschrock 	zfs_cmd_t zc = { 0 };
16624451Seschrock 	char msg[1024];
16634451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
16644451Seschrock 
16654451Seschrock 	(void) snprintf(msg, sizeof (msg),
16664451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
16674451Seschrock 
16684451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
16694451Seschrock 	zc.zc_guid = guid;
16704451Seschrock 	zc.zc_cookie = VDEV_STATE_FAULTED;
16714451Seschrock 
16724451Seschrock 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
16734451Seschrock 		return (0);
16744451Seschrock 
16754451Seschrock 	switch (errno) {
16764451Seschrock 	case EBUSY:
16774451Seschrock 
16784451Seschrock 		/*
16794451Seschrock 		 * There are no other replicas of this device.
16804451Seschrock 		 */
16814451Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
16824451Seschrock 
16834451Seschrock 	default:
16844451Seschrock 		return (zpool_standard_error(hdl, errno, msg));
16854451Seschrock 	}
16864451Seschrock 
16874451Seschrock }
16884451Seschrock 
16894451Seschrock /*
16904451Seschrock  * Mark the given vdev degraded.
16914451Seschrock  */
16924451Seschrock int
16934451Seschrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid)
16944451Seschrock {
16954451Seschrock 	zfs_cmd_t zc = { 0 };
16964451Seschrock 	char msg[1024];
16974451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
16984451Seschrock 
16994451Seschrock 	(void) snprintf(msg, sizeof (msg),
17004451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
17014451Seschrock 
17024451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
17034451Seschrock 	zc.zc_guid = guid;
17044451Seschrock 	zc.zc_cookie = VDEV_STATE_DEGRADED;
17054451Seschrock 
17064451Seschrock 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
17074451Seschrock 		return (0);
17084451Seschrock 
17094451Seschrock 	return (zpool_standard_error(hdl, errno, msg));
17104451Seschrock }
17114451Seschrock 
17124451Seschrock /*
17132082Seschrock  * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
17142082Seschrock  * a hot spare.
17152082Seschrock  */
17162082Seschrock static boolean_t
17172082Seschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
17182082Seschrock {
17192082Seschrock 	nvlist_t **child;
17202082Seschrock 	uint_t c, children;
17212082Seschrock 	char *type;
17222082Seschrock 
17232082Seschrock 	if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
17242082Seschrock 	    &children) == 0) {
17252082Seschrock 		verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
17262082Seschrock 		    &type) == 0);
17272082Seschrock 
17282082Seschrock 		if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
17292082Seschrock 		    children == 2 && child[which] == tgt)
17302082Seschrock 			return (B_TRUE);
17312082Seschrock 
17322082Seschrock 		for (c = 0; c < children; c++)
17332082Seschrock 			if (is_replacing_spare(child[c], tgt, which))
17342082Seschrock 				return (B_TRUE);
1735789Sahrens 	}
17362082Seschrock 
17372082Seschrock 	return (B_FALSE);
1738789Sahrens }
1739789Sahrens 
1740789Sahrens /*
1741789Sahrens  * Attach new_disk (fully described by nvroot) to old_disk.
17424527Sperrin  * If 'replacing' is specified, the new disk will replace the old one.
1743789Sahrens  */
1744789Sahrens int
1745789Sahrens zpool_vdev_attach(zpool_handle_t *zhp,
1746789Sahrens     const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
1747789Sahrens {
1748789Sahrens 	zfs_cmd_t zc = { 0 };
1749789Sahrens 	char msg[1024];
1750789Sahrens 	int ret;
17512082Seschrock 	nvlist_t *tgt;
17527326SEric.Schrock@Sun.COM 	boolean_t avail_spare, l2cache, islog;
17537326SEric.Schrock@Sun.COM 	uint64_t val;
17547041Seschrock 	char *path, *newname;
17552082Seschrock 	nvlist_t **child;
17562082Seschrock 	uint_t children;
17572082Seschrock 	nvlist_t *config_root;
17582082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
17597965SGeorge.Wilson@Sun.COM 	boolean_t rootpool = pool_is_bootable(zhp);
1760789Sahrens 
17611544Seschrock 	if (replacing)
17621544Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
17631544Seschrock 		    "cannot replace %s with %s"), old_disk, new_disk);
17641544Seschrock 	else
17651544Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
17661544Seschrock 		    "cannot attach %s to %s"), new_disk, old_disk);
17671544Seschrock 
17687965SGeorge.Wilson@Sun.COM 	/*
17697965SGeorge.Wilson@Sun.COM 	 * If this is a root pool, make sure that we're not attaching an
17707965SGeorge.Wilson@Sun.COM 	 * EFI labeled device.
17717965SGeorge.Wilson@Sun.COM 	 */
17727965SGeorge.Wilson@Sun.COM 	if (rootpool && pool_uses_efi(nvroot)) {
17737965SGeorge.Wilson@Sun.COM 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17747965SGeorge.Wilson@Sun.COM 		    "EFI labeled devices are not supported on root pools."));
17757965SGeorge.Wilson@Sun.COM 		return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
17767965SGeorge.Wilson@Sun.COM 	}
17777965SGeorge.Wilson@Sun.COM 
1778789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
17797326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
17807326SEric.Schrock@Sun.COM 	    &islog)) == 0)
17812082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
17822082Seschrock 
17832468Sek110237 	if (avail_spare)
17842082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
17852082Seschrock 
17865450Sbrendan 	if (l2cache)
17875450Sbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
17885450Sbrendan 
17892082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
17902082Seschrock 	zc.zc_cookie = replacing;
17912082Seschrock 
17922082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
17932082Seschrock 	    &child, &children) != 0 || children != 1) {
17942082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17952082Seschrock 		    "new device must be a single disk"));
17962082Seschrock 		return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
17971544Seschrock 	}
17982082Seschrock 
17992082Seschrock 	verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
18002082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
18012082Seschrock 
18027041Seschrock 	if ((newname = zpool_vdev_name(NULL, NULL, child[0])) == NULL)
18037041Seschrock 		return (-1);
18047041Seschrock 
18052082Seschrock 	/*
18062082Seschrock 	 * If the target is a hot spare that has been swapped in, we can only
18072082Seschrock 	 * replace it with another hot spare.
18082082Seschrock 	 */
18092082Seschrock 	if (replacing &&
18102082Seschrock 	    nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
18117326SEric.Schrock@Sun.COM 	    (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
18127326SEric.Schrock@Sun.COM 	    NULL) == NULL || !avail_spare) &&
18137326SEric.Schrock@Sun.COM 	    is_replacing_spare(config_root, tgt, 1)) {
18142082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18152082Seschrock 		    "can only be replaced by another hot spare"));
18167041Seschrock 		free(newname);
18172082Seschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
18182082Seschrock 	}
18192082Seschrock 
18202082Seschrock 	/*
18212082Seschrock 	 * If we are attempting to replace a spare, it canot be applied to an
18222082Seschrock 	 * already spared device.
18232082Seschrock 	 */
18242082Seschrock 	if (replacing &&
18252082Seschrock 	    nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 &&
18267326SEric.Schrock@Sun.COM 	    zpool_find_vdev(zhp, newname, &avail_spare,
18277326SEric.Schrock@Sun.COM 	    &l2cache, NULL) != NULL && avail_spare &&
18287326SEric.Schrock@Sun.COM 	    is_replacing_spare(config_root, tgt, 0)) {
18292082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18302082Seschrock 		    "device has already been replaced with a spare"));
18317041Seschrock 		free(newname);
18322082Seschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
18332082Seschrock 	}
1834789Sahrens 
18357041Seschrock 	free(newname);
18367041Seschrock 
18375094Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
18382082Seschrock 		return (-1);
1839789Sahrens 
18404543Smarks 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc);
1841789Sahrens 
18422676Seschrock 	zcmd_free_nvlists(&zc);
1843789Sahrens 
18447965SGeorge.Wilson@Sun.COM 	if (ret == 0) {
18457965SGeorge.Wilson@Sun.COM 		if (rootpool) {
18467965SGeorge.Wilson@Sun.COM 			/*
18477965SGeorge.Wilson@Sun.COM 			 * XXX - This should be removed once we can
18487965SGeorge.Wilson@Sun.COM 			 * automatically install the bootblocks on the
18497965SGeorge.Wilson@Sun.COM 			 * newly attached disk.
18507965SGeorge.Wilson@Sun.COM 			 */
18517965SGeorge.Wilson@Sun.COM 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Please "
18527965SGeorge.Wilson@Sun.COM 			    "be sure to invoke %s to make '%s' bootable.\n"),
18537965SGeorge.Wilson@Sun.COM 			    BOOTCMD, new_disk);
18547965SGeorge.Wilson@Sun.COM 		}
1855789Sahrens 		return (0);
18567965SGeorge.Wilson@Sun.COM 	}
1857789Sahrens 
1858789Sahrens 	switch (errno) {
18591544Seschrock 	case ENOTSUP:
1860789Sahrens 		/*
1861789Sahrens 		 * Can't attach to or replace this type of vdev.
1862789Sahrens 		 */
18634527Sperrin 		if (replacing) {
18647326SEric.Schrock@Sun.COM 			if (islog)
18654527Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18664527Sperrin 				    "cannot replace a log with a spare"));
18674527Sperrin 			else
18684527Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18694527Sperrin 				    "cannot replace a replacing device"));
18704527Sperrin 		} else {
18712082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18722082Seschrock 			    "can only attach to mirrors and top-level "
18732082Seschrock 			    "disks"));
18744527Sperrin 		}
18752082Seschrock 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
1876789Sahrens 		break;
1877789Sahrens 
18781544Seschrock 	case EINVAL:
1879789Sahrens 		/*
1880789Sahrens 		 * The new device must be a single disk.
1881789Sahrens 		 */
18822082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18832082Seschrock 		    "new device must be a single disk"));
18842082Seschrock 		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
1885789Sahrens 		break;
1886789Sahrens 
18871544Seschrock 	case EBUSY:
18882082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
18892082Seschrock 		    new_disk);
18902082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1891789Sahrens 		break;
1892789Sahrens 
18931544Seschrock 	case EOVERFLOW:
1894789Sahrens 		/*
1895789Sahrens 		 * The new device is too small.
1896789Sahrens 		 */
18972082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18982082Seschrock 		    "device is too small"));
18992082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1900789Sahrens 		break;
1901789Sahrens 
19021544Seschrock 	case EDOM:
1903789Sahrens 		/*
1904789Sahrens 		 * The new device has a different alignment requirement.
1905789Sahrens 		 */
19062082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19072082Seschrock 		    "devices have different sector alignment"));
19082082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1909789Sahrens 		break;
1910789Sahrens 
19111544Seschrock 	case ENAMETOOLONG:
1912789Sahrens 		/*
1913789Sahrens 		 * The resulting top-level vdev spec won't fit in the label.
1914789Sahrens 		 */
19152082Seschrock 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
1916789Sahrens 		break;
1917789Sahrens 
19181544Seschrock 	default:
19192082Seschrock 		(void) zpool_standard_error(hdl, errno, msg);
1920789Sahrens 	}
1921789Sahrens 
19222082Seschrock 	return (-1);
1923789Sahrens }
1924789Sahrens 
1925789Sahrens /*
1926789Sahrens  * Detach the specified device.
1927789Sahrens  */
1928789Sahrens int
1929789Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
1930789Sahrens {
1931789Sahrens 	zfs_cmd_t zc = { 0 };
1932789Sahrens 	char msg[1024];
19332082Seschrock 	nvlist_t *tgt;
19345450Sbrendan 	boolean_t avail_spare, l2cache;
19352082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1936789Sahrens 
19371544Seschrock 	(void) snprintf(msg, sizeof (msg),
19381544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
19391544Seschrock 
1940789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
19417326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
19427326SEric.Schrock@Sun.COM 	    NULL)) == 0)
19432082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
1944789Sahrens 
19452468Sek110237 	if (avail_spare)
19462082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
19472082Seschrock 
19485450Sbrendan 	if (l2cache)
19495450Sbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
19505450Sbrendan 
19512082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
19522082Seschrock 
19534543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
1954789Sahrens 		return (0);
1955789Sahrens 
1956789Sahrens 	switch (errno) {
1957789Sahrens 
19581544Seschrock 	case ENOTSUP:
1959789Sahrens 		/*
1960789Sahrens 		 * Can't detach from this type of vdev.
1961789Sahrens 		 */
19622082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
19632082Seschrock 		    "applicable to mirror and replacing vdevs"));
19642082Seschrock 		(void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg);
1965789Sahrens 		break;
1966789Sahrens 
19671544Seschrock 	case EBUSY:
1968789Sahrens 		/*
1969789Sahrens 		 * There are no other replicas of this device.
1970789Sahrens 		 */
19712082Seschrock 		(void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
1972789Sahrens 		break;
1973789Sahrens 
19741544Seschrock 	default:
19752082Seschrock 		(void) zpool_standard_error(hdl, errno, msg);
19761544Seschrock 	}
19771544Seschrock 
19782082Seschrock 	return (-1);
19792082Seschrock }
19802082Seschrock 
19812082Seschrock /*
19825450Sbrendan  * Remove the given device.  Currently, this is supported only for hot spares
19835450Sbrendan  * and level 2 cache devices.
19842082Seschrock  */
19852082Seschrock int
19862082Seschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
19872082Seschrock {
19882082Seschrock 	zfs_cmd_t zc = { 0 };
19892082Seschrock 	char msg[1024];
19902082Seschrock 	nvlist_t *tgt;
19915450Sbrendan 	boolean_t avail_spare, l2cache;
19922082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
19932082Seschrock 
19942082Seschrock 	(void) snprintf(msg, sizeof (msg),
19952082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
19962082Seschrock 
19972082Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
19987326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
19997326SEric.Schrock@Sun.COM 	    NULL)) == 0)
20002082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
20012082Seschrock 
20025450Sbrendan 	if (!avail_spare && !l2cache) {
20032082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
20045450Sbrendan 		    "only inactive hot spares or cache devices "
20055450Sbrendan 		    "can be removed"));
20062082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
20072082Seschrock 	}
20082082Seschrock 
20092082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
20102082Seschrock 
20114543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
20122082Seschrock 		return (0);
20132082Seschrock 
20142082Seschrock 	return (zpool_standard_error(hdl, errno, msg));
20151544Seschrock }
20161544Seschrock 
20171544Seschrock /*
20181544Seschrock  * Clear the errors for the pool, or the particular device if specified.
20191544Seschrock  */
20201544Seschrock int
20211544Seschrock zpool_clear(zpool_handle_t *zhp, const char *path)
20221544Seschrock {
20231544Seschrock 	zfs_cmd_t zc = { 0 };
20241544Seschrock 	char msg[1024];
20252082Seschrock 	nvlist_t *tgt;
20265450Sbrendan 	boolean_t avail_spare, l2cache;
20272082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
20281544Seschrock 
20291544Seschrock 	if (path)
20301544Seschrock 		(void) snprintf(msg, sizeof (msg),
20311544Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
20322676Seschrock 		    path);
20331544Seschrock 	else
20341544Seschrock 		(void) snprintf(msg, sizeof (msg),
20351544Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
20361544Seschrock 		    zhp->zpool_name);
20371544Seschrock 
20381544Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
20392082Seschrock 	if (path) {
20405450Sbrendan 		if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
20417326SEric.Schrock@Sun.COM 		    &l2cache, NULL)) == 0)
20422082Seschrock 			return (zfs_error(hdl, EZFS_NODEVICE, msg));
20432082Seschrock 
20445450Sbrendan 		/*
20455450Sbrendan 		 * Don't allow error clearing for hot spares.  Do allow
20465450Sbrendan 		 * error clearing for l2cache devices.
20475450Sbrendan 		 */
20482468Sek110237 		if (avail_spare)
20492082Seschrock 			return (zfs_error(hdl, EZFS_ISSPARE, msg));
20502082Seschrock 
20512082Seschrock 		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
20522082Seschrock 		    &zc.zc_guid) == 0);
20531544Seschrock 	}
20541544Seschrock 
20554543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0)
20561544Seschrock 		return (0);
20571544Seschrock 
20582082Seschrock 	return (zpool_standard_error(hdl, errno, msg));
2059789Sahrens }
2060789Sahrens 
20613126Sahl /*
20624451Seschrock  * Similar to zpool_clear(), but takes a GUID (used by fmd).
20634451Seschrock  */
20644451Seschrock int
20654451Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
20664451Seschrock {
20674451Seschrock 	zfs_cmd_t zc = { 0 };
20684451Seschrock 	char msg[1024];
20694451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
20704451Seschrock 
20714451Seschrock 	(void) snprintf(msg, sizeof (msg),
20724451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
20734451Seschrock 	    guid);
20744451Seschrock 
20754451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
20764451Seschrock 	zc.zc_guid = guid;
20774451Seschrock 
20784451Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
20794451Seschrock 		return (0);
20804451Seschrock 
20814451Seschrock 	return (zpool_standard_error(hdl, errno, msg));
20824451Seschrock }
20834451Seschrock 
20844451Seschrock /*
20853126Sahl  * Iterate over all zvols in a given pool by walking the /dev/zvol/dsk/<pool>
20863126Sahl  * hierarchy.
20873126Sahl  */
20883126Sahl int
20893126Sahl zpool_iter_zvol(zpool_handle_t *zhp, int (*cb)(const char *, void *),
20903126Sahl     void *data)
2091789Sahrens {
20923126Sahl 	libzfs_handle_t *hdl = zhp->zpool_hdl;
20933126Sahl 	char (*paths)[MAXPATHLEN];
20943126Sahl 	size_t size = 4;
20953126Sahl 	int curr, fd, base, ret = 0;
20963126Sahl 	DIR *dirp;
20973126Sahl 	struct dirent *dp;
20983126Sahl 	struct stat st;
20993126Sahl 
21003126Sahl 	if ((base = open("/dev/zvol/dsk", O_RDONLY)) < 0)
21013126Sahl 		return (errno == ENOENT ? 0 : -1);
21023126Sahl 
21033126Sahl 	if (fstatat(base, zhp->zpool_name, &st, 0) != 0) {
21043126Sahl 		int err = errno;
21053126Sahl 		(void) close(base);
21063126Sahl 		return (err == ENOENT ? 0 : -1);
21073126Sahl 	}
2108789Sahrens 
2109789Sahrens 	/*
21103126Sahl 	 * Oddly this wasn't a directory -- ignore that failure since we
21113126Sahl 	 * know there are no links lower in the (non-existant) hierarchy.
2112789Sahrens 	 */
21133126Sahl 	if (!S_ISDIR(st.st_mode)) {
21143126Sahl 		(void) close(base);
21153126Sahl 		return (0);
21163126Sahl 	}
21173126Sahl 
21183126Sahl 	if ((paths = zfs_alloc(hdl, size * sizeof (paths[0]))) == NULL) {
21193126Sahl 		(void) close(base);
21203126Sahl 		return (-1);
2121789Sahrens 	}
2122789Sahrens 
21233126Sahl 	(void) strlcpy(paths[0], zhp->zpool_name, sizeof (paths[0]));
21243126Sahl 	curr = 0;
21253126Sahl 
21263126Sahl 	while (curr >= 0) {
21273126Sahl 		if (fstatat(base, paths[curr], &st, AT_SYMLINK_NOFOLLOW) != 0)
21283126Sahl 			goto err;
21293126Sahl 
21303126Sahl 		if (S_ISDIR(st.st_mode)) {
21313126Sahl 			if ((fd = openat(base, paths[curr], O_RDONLY)) < 0)
21323126Sahl 				goto err;
21333126Sahl 
21343126Sahl 			if ((dirp = fdopendir(fd)) == NULL) {
21353126Sahl 				(void) close(fd);
21363126Sahl 				goto err;
21373126Sahl 			}
21383126Sahl 
21393126Sahl 			while ((dp = readdir(dirp)) != NULL) {
21403126Sahl 				if (dp->d_name[0] == '.')
21413126Sahl 					continue;
21423126Sahl 
21433126Sahl 				if (curr + 1 == size) {
21443126Sahl 					paths = zfs_realloc(hdl, paths,
21453126Sahl 					    size * sizeof (paths[0]),
21463126Sahl 					    size * 2 * sizeof (paths[0]));
21473126Sahl 					if (paths == NULL) {
21483126Sahl 						(void) closedir(dirp);
21493126Sahl 						(void) close(fd);
21503126Sahl 						goto err;
21513126Sahl 					}
21523126Sahl 
21533126Sahl 					size *= 2;
21543126Sahl 				}
21553126Sahl 
21563126Sahl 				(void) strlcpy(paths[curr + 1], paths[curr],
21573126Sahl 				    sizeof (paths[curr + 1]));
21583126Sahl 				(void) strlcat(paths[curr], "/",
21593126Sahl 				    sizeof (paths[curr]));
21603126Sahl 				(void) strlcat(paths[curr], dp->d_name,
21613126Sahl 				    sizeof (paths[curr]));
21623126Sahl 				curr++;
21633126Sahl 			}
21643126Sahl 
21653126Sahl 			(void) closedir(dirp);
21663126Sahl 
21673126Sahl 		} else {
21683126Sahl 			if ((ret = cb(paths[curr], data)) != 0)
21693126Sahl 				break;
21703126Sahl 		}
21713126Sahl 
21723126Sahl 		curr--;
21733126Sahl 	}
21743126Sahl 
21753126Sahl 	free(paths);
21763126Sahl 	(void) close(base);
21773126Sahl 
21783126Sahl 	return (ret);
21793126Sahl 
21803126Sahl err:
21813126Sahl 	free(paths);
21823126Sahl 	(void) close(base);
21833126Sahl 	return (-1);
21843126Sahl }
21853126Sahl 
21863126Sahl typedef struct zvol_cb {
21873126Sahl 	zpool_handle_t *zcb_pool;
21883126Sahl 	boolean_t zcb_create;
21893126Sahl } zvol_cb_t;
21903126Sahl 
21913126Sahl /*ARGSUSED*/
21923126Sahl static int
21933126Sahl do_zvol_create(zfs_handle_t *zhp, void *data)
21943126Sahl {
21954657Sahrens 	int ret = 0;
21963126Sahl 
21974657Sahrens 	if (ZFS_IS_VOLUME(zhp)) {
21983126Sahl 		(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
21994657Sahrens 		ret = zfs_iter_snapshots(zhp, do_zvol_create, NULL);
22004657Sahrens 	}
22013126Sahl 
22024657Sahrens 	if (ret == 0)
22034657Sahrens 		ret = zfs_iter_filesystems(zhp, do_zvol_create, NULL);
2204789Sahrens 
2205789Sahrens 	zfs_close(zhp);
22063126Sahl 
2207789Sahrens 	return (ret);
2208789Sahrens }
2209789Sahrens 
2210789Sahrens /*
2211789Sahrens  * Iterate over all zvols in the pool and make any necessary minor nodes.
2212789Sahrens  */
2213789Sahrens int
2214789Sahrens zpool_create_zvol_links(zpool_handle_t *zhp)
2215789Sahrens {
2216789Sahrens 	zfs_handle_t *zfp;
2217789Sahrens 	int ret;
2218789Sahrens 
2219789Sahrens 	/*
2220789Sahrens 	 * If the pool is unavailable, just return success.
2221789Sahrens 	 */
22222082Seschrock 	if ((zfp = make_dataset_handle(zhp->zpool_hdl,
22232082Seschrock 	    zhp->zpool_name)) == NULL)
2224789Sahrens 		return (0);
2225789Sahrens 
22264657Sahrens 	ret = zfs_iter_filesystems(zfp, do_zvol_create, NULL);
2227789Sahrens 
2228789Sahrens 	zfs_close(zfp);
2229789Sahrens 	return (ret);
2230789Sahrens }
2231789Sahrens 
22323126Sahl static int
22333126Sahl do_zvol_remove(const char *dataset, void *data)
22343126Sahl {
22353126Sahl 	zpool_handle_t *zhp = data;
22363126Sahl 
22373126Sahl 	return (zvol_remove_link(zhp->zpool_hdl, dataset));
22383126Sahl }
22393126Sahl 
2240789Sahrens /*
22413126Sahl  * Iterate over all zvols in the pool and remove any minor nodes.  We iterate
22423126Sahl  * by examining the /dev links so that a corrupted pool doesn't impede this
22433126Sahl  * operation.
2244789Sahrens  */
2245789Sahrens int
2246789Sahrens zpool_remove_zvol_links(zpool_handle_t *zhp)
2247789Sahrens {
22483126Sahl 	return (zpool_iter_zvol(zhp, do_zvol_remove, zhp));
2249789Sahrens }
22501354Seschrock 
22511354Seschrock /*
22521354Seschrock  * Convert from a devid string to a path.
22531354Seschrock  */
22541354Seschrock static char *
22551354Seschrock devid_to_path(char *devid_str)
22561354Seschrock {
22571354Seschrock 	ddi_devid_t devid;
22581354Seschrock 	char *minor;
22591354Seschrock 	char *path;
22601354Seschrock 	devid_nmlist_t *list = NULL;
22611354Seschrock 	int ret;
22621354Seschrock 
22631354Seschrock 	if (devid_str_decode(devid_str, &devid, &minor) != 0)
22641354Seschrock 		return (NULL);
22651354Seschrock 
22661354Seschrock 	ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
22671354Seschrock 
22681354Seschrock 	devid_str_free(minor);
22691354Seschrock 	devid_free(devid);
22701354Seschrock 
22711354Seschrock 	if (ret != 0)
22721354Seschrock 		return (NULL);
22731354Seschrock 
22742082Seschrock 	if ((path = strdup(list[0].devname)) == NULL)
22752082Seschrock 		return (NULL);
22762082Seschrock 
22771354Seschrock 	devid_free_nmlist(list);
22781354Seschrock 
22791354Seschrock 	return (path);
22801354Seschrock }
22811354Seschrock 
22821354Seschrock /*
22831354Seschrock  * Convert from a path to a devid string.
22841354Seschrock  */
22851354Seschrock static char *
22861354Seschrock path_to_devid(const char *path)
22871354Seschrock {
22881354Seschrock 	int fd;
22891354Seschrock 	ddi_devid_t devid;
22901354Seschrock 	char *minor, *ret;
22911354Seschrock 
22921354Seschrock 	if ((fd = open(path, O_RDONLY)) < 0)
22931354Seschrock 		return (NULL);
22941354Seschrock 
22951354Seschrock 	minor = NULL;
22961354Seschrock 	ret = NULL;
22971354Seschrock 	if (devid_get(fd, &devid) == 0) {
22981354Seschrock 		if (devid_get_minor_name(fd, &minor) == 0)
22991354Seschrock 			ret = devid_str_encode(devid, minor);
23001354Seschrock 		if (minor != NULL)
23011354Seschrock 			devid_str_free(minor);
23021354Seschrock 		devid_free(devid);
23031354Seschrock 	}
23041354Seschrock 	(void) close(fd);
23051354Seschrock 
23061354Seschrock 	return (ret);
23071354Seschrock }
23081354Seschrock 
23091354Seschrock /*
23101354Seschrock  * Issue the necessary ioctl() to update the stored path value for the vdev.  We
23111354Seschrock  * ignore any failure here, since a common case is for an unprivileged user to
23121354Seschrock  * type 'zpool status', and we'll display the correct information anyway.
23131354Seschrock  */
23141354Seschrock static void
23151354Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
23161354Seschrock {
23171354Seschrock 	zfs_cmd_t zc = { 0 };
23181354Seschrock 
23191354Seschrock 	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
23202676Seschrock 	(void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
23211354Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
23221544Seschrock 	    &zc.zc_guid) == 0);
23231354Seschrock 
23242082Seschrock 	(void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
23251354Seschrock }
23261354Seschrock 
23271354Seschrock /*
23281354Seschrock  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
23291354Seschrock  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
23301354Seschrock  * We also check if this is a whole disk, in which case we strip off the
23311354Seschrock  * trailing 's0' slice name.
23321354Seschrock  *
23331354Seschrock  * This routine is also responsible for identifying when disks have been
23341354Seschrock  * reconfigured in a new location.  The kernel will have opened the device by
23351354Seschrock  * devid, but the path will still refer to the old location.  To catch this, we
23361354Seschrock  * first do a path -> devid translation (which is fast for the common case).  If
23371354Seschrock  * the devid matches, we're done.  If not, we do a reverse devid -> path
23381354Seschrock  * translation and issue the appropriate ioctl() to update the path of the vdev.
23391354Seschrock  * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
23401354Seschrock  * of these checks.
23411354Seschrock  */
23421354Seschrock char *
23432082Seschrock zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv)
23441354Seschrock {
23451354Seschrock 	char *path, *devid;
23461544Seschrock 	uint64_t value;
23471544Seschrock 	char buf[64];
23484451Seschrock 	vdev_stat_t *vs;
23494451Seschrock 	uint_t vsc;
23501354Seschrock 
23511544Seschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
23521544Seschrock 	    &value) == 0) {
23531544Seschrock 		verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
23541544Seschrock 		    &value) == 0);
23552856Snd150628 		(void) snprintf(buf, sizeof (buf), "%llu",
23562856Snd150628 		    (u_longlong_t)value);
23571544Seschrock 		path = buf;
23581544Seschrock 	} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
23591354Seschrock 
23604451Seschrock 		/*
23614451Seschrock 		 * If the device is dead (faulted, offline, etc) then don't
23624451Seschrock 		 * bother opening it.  Otherwise we may be forcing the user to
23634451Seschrock 		 * open a misbehaving device, which can have undesirable
23644451Seschrock 		 * effects.
23654451Seschrock 		 */
23664451Seschrock 		if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
23674451Seschrock 		    (uint64_t **)&vs, &vsc) != 0 ||
23684451Seschrock 		    vs->vs_state >= VDEV_STATE_DEGRADED) &&
23694451Seschrock 		    zhp != NULL &&
23701354Seschrock 		    nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
23711354Seschrock 			/*
23721354Seschrock 			 * Determine if the current path is correct.
23731354Seschrock 			 */
23741354Seschrock 			char *newdevid = path_to_devid(path);
23751354Seschrock 
23761354Seschrock 			if (newdevid == NULL ||
23771354Seschrock 			    strcmp(devid, newdevid) != 0) {
23781354Seschrock 				char *newpath;
23791354Seschrock 
23801354Seschrock 				if ((newpath = devid_to_path(devid)) != NULL) {
23811354Seschrock 					/*
23821354Seschrock 					 * Update the path appropriately.
23831354Seschrock 					 */
23841354Seschrock 					set_path(zhp, nv, newpath);
23852082Seschrock 					if (nvlist_add_string(nv,
23862082Seschrock 					    ZPOOL_CONFIG_PATH, newpath) == 0)
23872082Seschrock 						verify(nvlist_lookup_string(nv,
23882082Seschrock 						    ZPOOL_CONFIG_PATH,
23892082Seschrock 						    &path) == 0);
23901354Seschrock 					free(newpath);
23911354Seschrock 				}
23921354Seschrock 			}
23931354Seschrock 
23942082Seschrock 			if (newdevid)
23952082Seschrock 				devid_str_free(newdevid);
23961354Seschrock 		}
23971354Seschrock 
23981354Seschrock 		if (strncmp(path, "/dev/dsk/", 9) == 0)
23991354Seschrock 			path += 9;
24001354Seschrock 
24011354Seschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
24021544Seschrock 		    &value) == 0 && value) {
24032082Seschrock 			char *tmp = zfs_strdup(hdl, path);
24042082Seschrock 			if (tmp == NULL)
24052082Seschrock 				return (NULL);
24061354Seschrock 			tmp[strlen(path) - 2] = '\0';
24071354Seschrock 			return (tmp);
24081354Seschrock 		}
24091354Seschrock 	} else {
24101354Seschrock 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
24112082Seschrock 
24122082Seschrock 		/*
24132082Seschrock 		 * If it's a raidz device, we need to stick in the parity level.
24142082Seschrock 		 */
24152082Seschrock 		if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
24162082Seschrock 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
24172082Seschrock 			    &value) == 0);
24182082Seschrock 			(void) snprintf(buf, sizeof (buf), "%s%llu", path,
24192856Snd150628 			    (u_longlong_t)value);
24202082Seschrock 			path = buf;
24212082Seschrock 		}
24221354Seschrock 	}
24231354Seschrock 
24242082Seschrock 	return (zfs_strdup(hdl, path));
24251354Seschrock }
24261544Seschrock 
24271544Seschrock static int
24281544Seschrock zbookmark_compare(const void *a, const void *b)
24291544Seschrock {
24301544Seschrock 	return (memcmp(a, b, sizeof (zbookmark_t)));
24311544Seschrock }
24321544Seschrock 
24331544Seschrock /*
24341544Seschrock  * Retrieve the persistent error log, uniquify the members, and return to the
24351544Seschrock  * caller.
24361544Seschrock  */
24371544Seschrock int
24383444Sek110237 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
24391544Seschrock {
24401544Seschrock 	zfs_cmd_t zc = { 0 };
24411544Seschrock 	uint64_t count;
24422676Seschrock 	zbookmark_t *zb = NULL;
24433444Sek110237 	int i;
24441544Seschrock 
24451544Seschrock 	/*
24461544Seschrock 	 * Retrieve the raw error list from the kernel.  If the number of errors
24471544Seschrock 	 * has increased, allocate more space and continue until we get the
24481544Seschrock 	 * entire list.
24491544Seschrock 	 */
24501544Seschrock 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
24511544Seschrock 	    &count) == 0);
24524820Sek110237 	if (count == 0)
24534820Sek110237 		return (0);
24542676Seschrock 	if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
24552856Snd150628 	    count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
24562082Seschrock 		return (-1);
24572676Seschrock 	zc.zc_nvlist_dst_size = count;
24581544Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
24591544Seschrock 	for (;;) {
24602082Seschrock 		if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
24612082Seschrock 		    &zc) != 0) {
24622676Seschrock 			free((void *)(uintptr_t)zc.zc_nvlist_dst);
24631544Seschrock 			if (errno == ENOMEM) {
24643823Svb160487 				count = zc.zc_nvlist_dst_size;
24652676Seschrock 				if ((zc.zc_nvlist_dst = (uintptr_t)
24663823Svb160487 				    zfs_alloc(zhp->zpool_hdl, count *
24673823Svb160487 				    sizeof (zbookmark_t))) == (uintptr_t)NULL)
24682082Seschrock 					return (-1);
24691544Seschrock 			} else {
24701544Seschrock 				return (-1);
24711544Seschrock 			}
24721544Seschrock 		} else {
24731544Seschrock 			break;
24741544Seschrock 		}
24751544Seschrock 	}
24761544Seschrock 
24771544Seschrock 	/*
24781544Seschrock 	 * Sort the resulting bookmarks.  This is a little confusing due to the
24791544Seschrock 	 * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
24802676Seschrock 	 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
24811544Seschrock 	 * _not_ copied as part of the process.  So we point the start of our
24821544Seschrock 	 * array appropriate and decrement the total number of elements.
24831544Seschrock 	 */
24842676Seschrock 	zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
24852676Seschrock 	    zc.zc_nvlist_dst_size;
24862676Seschrock 	count -= zc.zc_nvlist_dst_size;
24871544Seschrock 
24881544Seschrock 	qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
24891544Seschrock 
24903444Sek110237 	verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
24911544Seschrock 
24921544Seschrock 	/*
24933444Sek110237 	 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
24941544Seschrock 	 */
24951544Seschrock 	for (i = 0; i < count; i++) {
24961544Seschrock 		nvlist_t *nv;
24971544Seschrock 
24983700Sek110237 		/* ignoring zb_blkid and zb_level for now */
24993700Sek110237 		if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
25003700Sek110237 		    zb[i-1].zb_object == zb[i].zb_object)
25011544Seschrock 			continue;
25021544Seschrock 
25033444Sek110237 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
25043444Sek110237 			goto nomem;
25053444Sek110237 		if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
25063444Sek110237 		    zb[i].zb_objset) != 0) {
25073444Sek110237 			nvlist_free(nv);
25082082Seschrock 			goto nomem;
25093444Sek110237 		}
25103444Sek110237 		if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
25113444Sek110237 		    zb[i].zb_object) != 0) {
25123444Sek110237 			nvlist_free(nv);
25133444Sek110237 			goto nomem;
25141544Seschrock 		}
25153444Sek110237 		if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
25163444Sek110237 			nvlist_free(nv);
25173444Sek110237 			goto nomem;
25183444Sek110237 		}
25193444Sek110237 		nvlist_free(nv);
25201544Seschrock 	}
25211544Seschrock 
25223265Sahrens 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
25231544Seschrock 	return (0);
25242082Seschrock 
25252082Seschrock nomem:
25262676Seschrock 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
25272082Seschrock 	return (no_memory(zhp->zpool_hdl));
25281544Seschrock }
25291760Seschrock 
25301760Seschrock /*
25311760Seschrock  * Upgrade a ZFS pool to the latest on-disk version.
25321760Seschrock  */
25331760Seschrock int
25345094Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
25351760Seschrock {
25361760Seschrock 	zfs_cmd_t zc = { 0 };
25372082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
25381760Seschrock 
25391760Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
25405094Slling 	zc.zc_cookie = new_version;
25415094Slling 
25424543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
25433237Slling 		return (zpool_standard_error_fmt(hdl, errno,
25442082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
25452082Seschrock 		    zhp->zpool_name));
25461760Seschrock 	return (0);
25471760Seschrock }
25482926Sek110237 
25494988Sek110237 void
25504988Sek110237 zpool_set_history_str(const char *subcommand, int argc, char **argv,
25514988Sek110237     char *history_str)
25524988Sek110237 {
25534988Sek110237 	int i;
25544988Sek110237 
25554988Sek110237 	(void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
25564988Sek110237 	for (i = 1; i < argc; i++) {
25574988Sek110237 		if (strlen(history_str) + 1 + strlen(argv[i]) >
25584988Sek110237 		    HIS_MAX_RECORD_LEN)
25594988Sek110237 			break;
25604988Sek110237 		(void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
25614988Sek110237 		(void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
25624988Sek110237 	}
25634988Sek110237 }
25644988Sek110237 
25652926Sek110237 /*
25664988Sek110237  * Stage command history for logging.
25672926Sek110237  */
25684988Sek110237 int
25694988Sek110237 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
25702926Sek110237 {
25714988Sek110237 	if (history_str == NULL)
25724988Sek110237 		return (EINVAL);
25734988Sek110237 
25744988Sek110237 	if (strlen(history_str) > HIS_MAX_RECORD_LEN)
25754988Sek110237 		return (EINVAL);
25762926Sek110237 
25774715Sek110237 	if (hdl->libzfs_log_str != NULL)
25784543Smarks 		free(hdl->libzfs_log_str);
25792926Sek110237 
25804988Sek110237 	if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
25814988Sek110237 		return (no_memory(hdl));
25824543Smarks 
25834988Sek110237 	return (0);
25842926Sek110237 }
25852926Sek110237 
25862926Sek110237 /*
25872926Sek110237  * Perform ioctl to get some command history of a pool.
25882926Sek110237  *
25892926Sek110237  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
25902926Sek110237  * logical offset of the history buffer to start reading from.
25912926Sek110237  *
25922926Sek110237  * Upon return, 'off' is the next logical offset to read from and
25932926Sek110237  * 'len' is the actual amount of bytes read into 'buf'.
25942926Sek110237  */
25952926Sek110237 static int
25962926Sek110237 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
25972926Sek110237 {
25982926Sek110237 	zfs_cmd_t zc = { 0 };
25992926Sek110237 	libzfs_handle_t *hdl = zhp->zpool_hdl;
26002926Sek110237 
26012926Sek110237 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
26022926Sek110237 
26032926Sek110237 	zc.zc_history = (uint64_t)(uintptr_t)buf;
26042926Sek110237 	zc.zc_history_len = *len;
26052926Sek110237 	zc.zc_history_offset = *off;
26062926Sek110237 
26072926Sek110237 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
26082926Sek110237 		switch (errno) {
26092926Sek110237 		case EPERM:
26103237Slling 			return (zfs_error_fmt(hdl, EZFS_PERM,
26113237Slling 			    dgettext(TEXT_DOMAIN,
26122926Sek110237 			    "cannot show history for pool '%s'"),
26132926Sek110237 			    zhp->zpool_name));
26142926Sek110237 		case ENOENT:
26153237Slling 			return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
26162926Sek110237 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
26172926Sek110237 			    "'%s'"), zhp->zpool_name));
26183863Sek110237 		case ENOTSUP:
26193863Sek110237 			return (zfs_error_fmt(hdl, EZFS_BADVERSION,
26203863Sek110237 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
26213863Sek110237 			    "'%s', pool must be upgraded"), zhp->zpool_name));
26222926Sek110237 		default:
26233237Slling 			return (zpool_standard_error_fmt(hdl, errno,
26242926Sek110237 			    dgettext(TEXT_DOMAIN,
26252926Sek110237 			    "cannot get history for '%s'"), zhp->zpool_name));
26262926Sek110237 		}
26272926Sek110237 	}
26282926Sek110237 
26292926Sek110237 	*len = zc.zc_history_len;
26302926Sek110237 	*off = zc.zc_history_offset;
26312926Sek110237 
26322926Sek110237 	return (0);
26332926Sek110237 }
26342926Sek110237 
26352926Sek110237 /*
26362926Sek110237  * Process the buffer of nvlists, unpacking and storing each nvlist record
26372926Sek110237  * into 'records'.  'leftover' is set to the number of bytes that weren't
26382926Sek110237  * processed as there wasn't a complete record.
26392926Sek110237  */
26402926Sek110237 static int
26412926Sek110237 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
26422926Sek110237     nvlist_t ***records, uint_t *numrecords)
26432926Sek110237 {
26442926Sek110237 	uint64_t reclen;
26452926Sek110237 	nvlist_t *nv;
26462926Sek110237 	int i;
26472926Sek110237 
26482926Sek110237 	while (bytes_read > sizeof (reclen)) {
26492926Sek110237 
26502926Sek110237 		/* get length of packed record (stored as little endian) */
26512926Sek110237 		for (i = 0, reclen = 0; i < sizeof (reclen); i++)
26522926Sek110237 			reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
26532926Sek110237 
26542926Sek110237 		if (bytes_read < sizeof (reclen) + reclen)
26552926Sek110237 			break;
26562926Sek110237 
26572926Sek110237 		/* unpack record */
26582926Sek110237 		if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
26592926Sek110237 			return (ENOMEM);
26602926Sek110237 		bytes_read -= sizeof (reclen) + reclen;
26612926Sek110237 		buf += sizeof (reclen) + reclen;
26622926Sek110237 
26632926Sek110237 		/* add record to nvlist array */
26642926Sek110237 		(*numrecords)++;
26652926Sek110237 		if (ISP2(*numrecords + 1)) {
26662926Sek110237 			*records = realloc(*records,
26672926Sek110237 			    *numrecords * 2 * sizeof (nvlist_t *));
26682926Sek110237 		}
26692926Sek110237 		(*records)[*numrecords - 1] = nv;
26702926Sek110237 	}
26712926Sek110237 
26722926Sek110237 	*leftover = bytes_read;
26732926Sek110237 	return (0);
26742926Sek110237 }
26752926Sek110237 
26762926Sek110237 #define	HIS_BUF_LEN	(128*1024)
26772926Sek110237 
26782926Sek110237 /*
26792926Sek110237  * Retrieve the command history of a pool.
26802926Sek110237  */
26812926Sek110237 int
26822926Sek110237 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
26832926Sek110237 {
26842926Sek110237 	char buf[HIS_BUF_LEN];
26852926Sek110237 	uint64_t off = 0;
26862926Sek110237 	nvlist_t **records = NULL;
26872926Sek110237 	uint_t numrecords = 0;
26882926Sek110237 	int err, i;
26892926Sek110237 
26902926Sek110237 	do {
26912926Sek110237 		uint64_t bytes_read = sizeof (buf);
26922926Sek110237 		uint64_t leftover;
26932926Sek110237 
26942926Sek110237 		if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
26952926Sek110237 			break;
26962926Sek110237 
26972926Sek110237 		/* if nothing else was read in, we're at EOF, just return */
26982926Sek110237 		if (!bytes_read)
26992926Sek110237 			break;
27002926Sek110237 
27012926Sek110237 		if ((err = zpool_history_unpack(buf, bytes_read,
27022926Sek110237 		    &leftover, &records, &numrecords)) != 0)
27032926Sek110237 			break;
27042926Sek110237 		off -= leftover;
27052926Sek110237 
27062926Sek110237 		/* CONSTCOND */
27072926Sek110237 	} while (1);
27082926Sek110237 
27092926Sek110237 	if (!err) {
27102926Sek110237 		verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
27112926Sek110237 		verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
27122926Sek110237 		    records, numrecords) == 0);
27132926Sek110237 	}
27142926Sek110237 	for (i = 0; i < numrecords; i++)
27152926Sek110237 		nvlist_free(records[i]);
27162926Sek110237 	free(records);
27172926Sek110237 
27182926Sek110237 	return (err);
27192926Sek110237 }
27203444Sek110237 
27213444Sek110237 void
27223444Sek110237 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
27233444Sek110237     char *pathname, size_t len)
27243444Sek110237 {
27253444Sek110237 	zfs_cmd_t zc = { 0 };
27263444Sek110237 	boolean_t mounted = B_FALSE;
27273444Sek110237 	char *mntpnt = NULL;
27283444Sek110237 	char dsname[MAXNAMELEN];
27293444Sek110237 
27303444Sek110237 	if (dsobj == 0) {
27313444Sek110237 		/* special case for the MOS */
27323444Sek110237 		(void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
27333444Sek110237 		return;
27343444Sek110237 	}
27353444Sek110237 
27363444Sek110237 	/* get the dataset's name */
27373444Sek110237 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
27383444Sek110237 	zc.zc_obj = dsobj;
27393444Sek110237 	if (ioctl(zhp->zpool_hdl->libzfs_fd,
27403444Sek110237 	    ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
27413444Sek110237 		/* just write out a path of two object numbers */
27423444Sek110237 		(void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
27433444Sek110237 		    dsobj, obj);
27443444Sek110237 		return;
27453444Sek110237 	}
27463444Sek110237 	(void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
27473444Sek110237 
27483444Sek110237 	/* find out if the dataset is mounted */
27493444Sek110237 	mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
27503444Sek110237 
27513444Sek110237 	/* get the corrupted object's path */
27523444Sek110237 	(void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
27533444Sek110237 	zc.zc_obj = obj;
27543444Sek110237 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
27553444Sek110237 	    &zc) == 0) {
27563444Sek110237 		if (mounted) {
27573444Sek110237 			(void) snprintf(pathname, len, "%s%s", mntpnt,
27583444Sek110237 			    zc.zc_value);
27593444Sek110237 		} else {
27603444Sek110237 			(void) snprintf(pathname, len, "%s:%s",
27613444Sek110237 			    dsname, zc.zc_value);
27623444Sek110237 		}
27633444Sek110237 	} else {
27643444Sek110237 		(void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
27653444Sek110237 	}
27663444Sek110237 	free(mntpnt);
27673444Sek110237 }
27683912Slling 
27694276Staylor #define	RDISK_ROOT	"/dev/rdsk"
27704276Staylor #define	BACKUP_SLICE	"s2"
27714276Staylor /*
27724276Staylor  * Don't start the slice at the default block of 34; many storage
27734276Staylor  * devices will use a stripe width of 128k, so start there instead.
27744276Staylor  */
27754276Staylor #define	NEW_START_BLOCK	256
27764276Staylor 
27774276Staylor /*
27787042Sgw25295  * Read the EFI label from the config, if a label does not exist then
27797042Sgw25295  * pass back the error to the caller. If the caller has passed a non-NULL
27807042Sgw25295  * diskaddr argument then we set it to the starting address of the EFI
27817042Sgw25295  * partition.
27827042Sgw25295  */
27837042Sgw25295 static int
27847042Sgw25295 read_efi_label(nvlist_t *config, diskaddr_t *sb)
27857042Sgw25295 {
27867042Sgw25295 	char *path;
27877042Sgw25295 	int fd;
27887042Sgw25295 	char diskname[MAXPATHLEN];
27897042Sgw25295 	int err = -1;
27907042Sgw25295 
27917042Sgw25295 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
27927042Sgw25295 		return (err);
27937042Sgw25295 
27947042Sgw25295 	(void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
27957042Sgw25295 	    strrchr(path, '/'));
27967042Sgw25295 	if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
27977042Sgw25295 		struct dk_gpt *vtoc;
27987042Sgw25295 
27997042Sgw25295 		if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
28007042Sgw25295 			if (sb != NULL)
28017042Sgw25295 				*sb = vtoc->efi_parts[0].p_start;
28027042Sgw25295 			efi_free(vtoc);
28037042Sgw25295 		}
28047042Sgw25295 		(void) close(fd);
28057042Sgw25295 	}
28067042Sgw25295 	return (err);
28077042Sgw25295 }
28087042Sgw25295 
28097042Sgw25295 /*
28104276Staylor  * determine where a partition starts on a disk in the current
28114276Staylor  * configuration
28124276Staylor  */
28134276Staylor static diskaddr_t
28144276Staylor find_start_block(nvlist_t *config)
28154276Staylor {
28164276Staylor 	nvlist_t **child;
28174276Staylor 	uint_t c, children;
28184276Staylor 	diskaddr_t sb = MAXOFFSET_T;
28194276Staylor 	uint64_t wholedisk;
28204276Staylor 
28214276Staylor 	if (nvlist_lookup_nvlist_array(config,
28224276Staylor 	    ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
28234276Staylor 		if (nvlist_lookup_uint64(config,
28244276Staylor 		    ZPOOL_CONFIG_WHOLE_DISK,
28254276Staylor 		    &wholedisk) != 0 || !wholedisk) {
28264276Staylor 			return (MAXOFFSET_T);
28274276Staylor 		}
28287042Sgw25295 		if (read_efi_label(config, &sb) < 0)
28297042Sgw25295 			sb = MAXOFFSET_T;
28304276Staylor 		return (sb);
28314276Staylor 	}
28324276Staylor 
28334276Staylor 	for (c = 0; c < children; c++) {
28344276Staylor 		sb = find_start_block(child[c]);
28354276Staylor 		if (sb != MAXOFFSET_T) {
28364276Staylor 			return (sb);
28374276Staylor 		}
28384276Staylor 	}
28394276Staylor 	return (MAXOFFSET_T);
28404276Staylor }
28414276Staylor 
28424276Staylor /*
28434276Staylor  * Label an individual disk.  The name provided is the short name,
28444276Staylor  * stripped of any leading /dev path.
28454276Staylor  */
28464276Staylor int
28474276Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
28484276Staylor {
28494276Staylor 	char path[MAXPATHLEN];
28504276Staylor 	struct dk_gpt *vtoc;
28514276Staylor 	int fd;
28524276Staylor 	size_t resv = EFI_MIN_RESV_SIZE;
28534276Staylor 	uint64_t slice_size;
28544276Staylor 	diskaddr_t start_block;
28554276Staylor 	char errbuf[1024];
28564276Staylor 
28576289Smmusante 	/* prepare an error message just in case */
28586289Smmusante 	(void) snprintf(errbuf, sizeof (errbuf),
28596289Smmusante 	    dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
28606289Smmusante 
28614276Staylor 	if (zhp) {
28624276Staylor 		nvlist_t *nvroot;
28634276Staylor 
28647965SGeorge.Wilson@Sun.COM 		if (pool_is_bootable(zhp)) {
28657965SGeorge.Wilson@Sun.COM 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28667965SGeorge.Wilson@Sun.COM 			    "EFI labeled devices are not supported on root "
28677965SGeorge.Wilson@Sun.COM 			    "pools."));
28687965SGeorge.Wilson@Sun.COM 			return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
28697965SGeorge.Wilson@Sun.COM 		}
28707965SGeorge.Wilson@Sun.COM 
28714276Staylor 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
28724276Staylor 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
28734276Staylor 
28744276Staylor 		if (zhp->zpool_start_block == 0)
28754276Staylor 			start_block = find_start_block(nvroot);
28764276Staylor 		else
28774276Staylor 			start_block = zhp->zpool_start_block;
28784276Staylor 		zhp->zpool_start_block = start_block;
28794276Staylor 	} else {
28804276Staylor 		/* new pool */
28814276Staylor 		start_block = NEW_START_BLOCK;
28824276Staylor 	}
28834276Staylor 
28844276Staylor 	(void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
28854276Staylor 	    BACKUP_SLICE);
28864276Staylor 
28874276Staylor 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
28884276Staylor 		/*
28894276Staylor 		 * This shouldn't happen.  We've long since verified that this
28904276Staylor 		 * is a valid device.
28914276Staylor 		 */
28926289Smmusante 		zfs_error_aux(hdl,
28936289Smmusante 		    dgettext(TEXT_DOMAIN, "unable to open device"));
28944276Staylor 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
28954276Staylor 	}
28964276Staylor 
28974276Staylor 	if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
28984276Staylor 		/*
28994276Staylor 		 * The only way this can fail is if we run out of memory, or we
29004276Staylor 		 * were unable to read the disk's capacity
29014276Staylor 		 */
29024276Staylor 		if (errno == ENOMEM)
29034276Staylor 			(void) no_memory(hdl);
29044276Staylor 
29054276Staylor 		(void) close(fd);
29066289Smmusante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29076289Smmusante 		    "unable to read disk capacity"), name);
29084276Staylor 
29094276Staylor 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
29104276Staylor 	}
29114276Staylor 
29124276Staylor 	slice_size = vtoc->efi_last_u_lba + 1;
29134276Staylor 	slice_size -= EFI_MIN_RESV_SIZE;
29144276Staylor 	if (start_block == MAXOFFSET_T)
29154276Staylor 		start_block = NEW_START_BLOCK;
29164276Staylor 	slice_size -= start_block;
29174276Staylor 
29184276Staylor 	vtoc->efi_parts[0].p_start = start_block;
29194276Staylor 	vtoc->efi_parts[0].p_size = slice_size;
29204276Staylor 
29214276Staylor 	/*
29224276Staylor 	 * Why we use V_USR: V_BACKUP confuses users, and is considered
29234276Staylor 	 * disposable by some EFI utilities (since EFI doesn't have a backup
29244276Staylor 	 * slice).  V_UNASSIGNED is supposed to be used only for zero size
29254276Staylor 	 * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
29264276Staylor 	 * etc. were all pretty specific.  V_USR is as close to reality as we
29274276Staylor 	 * can get, in the absence of V_OTHER.
29284276Staylor 	 */
29294276Staylor 	vtoc->efi_parts[0].p_tag = V_USR;
29304276Staylor 	(void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
29314276Staylor 
29324276Staylor 	vtoc->efi_parts[8].p_start = slice_size + start_block;
29334276Staylor 	vtoc->efi_parts[8].p_size = resv;
29344276Staylor 	vtoc->efi_parts[8].p_tag = V_RESERVED;
29354276Staylor 
29364276Staylor 	if (efi_write(fd, vtoc) != 0) {
29374276Staylor 		/*
29384276Staylor 		 * Some block drivers (like pcata) may not support EFI
29394276Staylor 		 * GPT labels.  Print out a helpful error message dir-
29404276Staylor 		 * ecting the user to manually label the disk and give
29414276Staylor 		 * a specific slice.
29424276Staylor 		 */
29434276Staylor 		(void) close(fd);
29444276Staylor 		efi_free(vtoc);
29454276Staylor 
29464276Staylor 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29476289Smmusante 		    "try using fdisk(1M) and then provide a specific slice"));
29484276Staylor 		return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
29494276Staylor 	}
29504276Staylor 
29514276Staylor 	(void) close(fd);
29524276Staylor 	efi_free(vtoc);
29534276Staylor 	return (0);
29544276Staylor }
29556423Sgw25295 
29566423Sgw25295 static boolean_t
29576423Sgw25295 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
29586423Sgw25295 {
29596423Sgw25295 	char *type;
29606423Sgw25295 	nvlist_t **child;
29616423Sgw25295 	uint_t children, c;
29626423Sgw25295 
29636423Sgw25295 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
29646423Sgw25295 	if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
29656423Sgw25295 	    strcmp(type, VDEV_TYPE_FILE) == 0 ||
29666423Sgw25295 	    strcmp(type, VDEV_TYPE_LOG) == 0 ||
29676423Sgw25295 	    strcmp(type, VDEV_TYPE_MISSING) == 0) {
29686423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29696423Sgw25295 		    "vdev type '%s' is not supported"), type);
29706423Sgw25295 		(void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
29716423Sgw25295 		return (B_FALSE);
29726423Sgw25295 	}
29736423Sgw25295 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
29746423Sgw25295 	    &child, &children) == 0) {
29756423Sgw25295 		for (c = 0; c < children; c++) {
29766423Sgw25295 			if (!supported_dump_vdev_type(hdl, child[c], errbuf))
29776423Sgw25295 				return (B_FALSE);
29786423Sgw25295 		}
29796423Sgw25295 	}
29806423Sgw25295 	return (B_TRUE);
29816423Sgw25295 }
29826423Sgw25295 
29836423Sgw25295 /*
29846423Sgw25295  * check if this zvol is allowable for use as a dump device; zero if
29856423Sgw25295  * it is, > 0 if it isn't, < 0 if it isn't a zvol
29866423Sgw25295  */
29876423Sgw25295 int
29886423Sgw25295 zvol_check_dump_config(char *arg)
29896423Sgw25295 {
29906423Sgw25295 	zpool_handle_t *zhp = NULL;
29916423Sgw25295 	nvlist_t *config, *nvroot;
29926423Sgw25295 	char *p, *volname;
29936423Sgw25295 	nvlist_t **top;
29946423Sgw25295 	uint_t toplevels;
29956423Sgw25295 	libzfs_handle_t *hdl;
29966423Sgw25295 	char errbuf[1024];
29976423Sgw25295 	char poolname[ZPOOL_MAXNAMELEN];
29986423Sgw25295 	int pathlen = strlen(ZVOL_FULL_DEV_DIR);
29996423Sgw25295 	int ret = 1;
30006423Sgw25295 
30016423Sgw25295 	if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
30026423Sgw25295 		return (-1);
30036423Sgw25295 	}
30046423Sgw25295 
30056423Sgw25295 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
30066423Sgw25295 	    "dump is not supported on device '%s'"), arg);
30076423Sgw25295 
30086423Sgw25295 	if ((hdl = libzfs_init()) == NULL)
30096423Sgw25295 		return (1);
30106423Sgw25295 	libzfs_print_on_error(hdl, B_TRUE);
30116423Sgw25295 
30126423Sgw25295 	volname = arg + pathlen;
30136423Sgw25295 
30146423Sgw25295 	/* check the configuration of the pool */
30156423Sgw25295 	if ((p = strchr(volname, '/')) == NULL) {
30166423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30176423Sgw25295 		    "malformed dataset name"));
30186423Sgw25295 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
30196423Sgw25295 		return (1);
30206423Sgw25295 	} else if (p - volname >= ZFS_MAXNAMELEN) {
30216423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30226423Sgw25295 		    "dataset name is too long"));
30236423Sgw25295 		(void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
30246423Sgw25295 		return (1);
30256423Sgw25295 	} else {
30266423Sgw25295 		(void) strncpy(poolname, volname, p - volname);
30276423Sgw25295 		poolname[p - volname] = '\0';
30286423Sgw25295 	}
30296423Sgw25295 
30306423Sgw25295 	if ((zhp = zpool_open(hdl, poolname)) == NULL) {
30316423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30326423Sgw25295 		    "could not open pool '%s'"), poolname);
30336423Sgw25295 		(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
30346423Sgw25295 		goto out;
30356423Sgw25295 	}
30366423Sgw25295 	config = zpool_get_config(zhp, NULL);
30376423Sgw25295 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
30386423Sgw25295 	    &nvroot) != 0) {
30396423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30406423Sgw25295 		    "could not obtain vdev configuration for  '%s'"), poolname);
30416423Sgw25295 		(void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
30426423Sgw25295 		goto out;
30436423Sgw25295 	}
30446423Sgw25295 
30456423Sgw25295 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
30466423Sgw25295 	    &top, &toplevels) == 0);
30476423Sgw25295 	if (toplevels != 1) {
30486423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30496423Sgw25295 		    "'%s' has multiple top level vdevs"), poolname);
30506423Sgw25295 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
30516423Sgw25295 		goto out;
30526423Sgw25295 	}
30536423Sgw25295 
30546423Sgw25295 	if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
30556423Sgw25295 		goto out;
30566423Sgw25295 	}
30576423Sgw25295 	ret = 0;
30586423Sgw25295 
30596423Sgw25295 out:
30606423Sgw25295 	if (zhp)
30616423Sgw25295 		zpool_close(zhp);
30626423Sgw25295 	libzfs_fini(hdl);
30636423Sgw25295 	return (ret);
30646423Sgw25295 }
3065