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 
52*7965SGeorge.Wilson@Sun.COM #if defined(__i386) || defined(__amd64)
53*7965SGeorge.Wilson@Sun.COM #define	BOOTCMD	"installgrub(1M)"
54*7965SGeorge.Wilson@Sun.COM #else
55*7965SGeorge.Wilson@Sun.COM #define	BOOTCMD	"installboot(1M)"
56*7965SGeorge.Wilson@Sun.COM #endif
57*7965SGeorge.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 
326*7965SGeorge.Wilson@Sun.COM static boolean_t
327*7965SGeorge.Wilson@Sun.COM pool_is_bootable(zpool_handle_t *zhp)
328*7965SGeorge.Wilson@Sun.COM {
329*7965SGeorge.Wilson@Sun.COM 	char bootfs[ZPOOL_MAXNAMELEN];
330*7965SGeorge.Wilson@Sun.COM 
331*7965SGeorge.Wilson@Sun.COM 	return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
332*7965SGeorge.Wilson@Sun.COM 	    sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
333*7965SGeorge.Wilson@Sun.COM 	    sizeof (bootfs)) != 0);
334*7965SGeorge.Wilson@Sun.COM }
335*7965SGeorge.Wilson@Sun.COM 
336*7965SGeorge.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 
1028*7965SGeorge.Wilson@Sun.COM 	if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
1029*7965SGeorge.Wilson@Sun.COM 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
1030*7965SGeorge.Wilson@Sun.COM 		uint64_t s;
1031*7965SGeorge.Wilson@Sun.COM 
1032*7965SGeorge.Wilson@Sun.COM 		for (s = 0; s < nspares; s++) {
1033*7965SGeorge.Wilson@Sun.COM 			char *path;
1034*7965SGeorge.Wilson@Sun.COM 
1035*7965SGeorge.Wilson@Sun.COM 			if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
1036*7965SGeorge.Wilson@Sun.COM 			    &path) == 0 && pool_uses_efi(spares[s])) {
1037*7965SGeorge.Wilson@Sun.COM 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1038*7965SGeorge.Wilson@Sun.COM 				    "device '%s' contains an EFI label and "
1039*7965SGeorge.Wilson@Sun.COM 				    "cannot be used on root pools."),
1040*7965SGeorge.Wilson@Sun.COM 				    zpool_vdev_name(hdl, NULL, spares[s]));
1041*7965SGeorge.Wilson@Sun.COM 				return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1042*7965SGeorge.Wilson@Sun.COM 			}
1043*7965SGeorge.Wilson@Sun.COM 		}
1044*7965SGeorge.Wilson@Sun.COM 	}
1045*7965SGeorge.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,
11855094Slling 		    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0) {
11865094Slling 			nvlist_free(props);
11875094Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
11885094Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
11895094Slling 			    newname));
11905094Slling 		}
11915094Slling 	}
11925094Slling 
11936643Seschrock 	ret = zpool_import_props(hdl, config, newname, props, B_FALSE);
11945094Slling 	if (props)
11955094Slling 		nvlist_free(props);
11965094Slling 	return (ret);
11975094Slling }
11985094Slling 
11995094Slling /*
12005094Slling  * Import the given pool using the known configuration and a list of
12015094Slling  * properties to be set. The configuration should have come from
12025094Slling  * zpool_find_import(). The 'newname' parameters control whether the pool
12035094Slling  * is imported with a different name.
12045094Slling  */
12055094Slling int
12065094Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
12076643Seschrock     nvlist_t *props, boolean_t importfaulted)
1208789Sahrens {
12092676Seschrock 	zfs_cmd_t zc = { 0 };
1210789Sahrens 	char *thename;
1211789Sahrens 	char *origname;
1212789Sahrens 	int ret;
12135094Slling 	char errbuf[1024];
1214789Sahrens 
1215789Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1216789Sahrens 	    &origname) == 0);
1217789Sahrens 
12185094Slling 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
12195094Slling 	    "cannot import pool '%s'"), origname);
12205094Slling 
1221789Sahrens 	if (newname != NULL) {
12222082Seschrock 		if (!zpool_name_valid(hdl, B_FALSE, newname))
12233237Slling 			return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
12242082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
12252082Seschrock 			    newname));
1226789Sahrens 		thename = (char *)newname;
1227789Sahrens 	} else {
1228789Sahrens 		thename = origname;
1229789Sahrens 	}
1230789Sahrens 
12315094Slling 	if (props) {
12325094Slling 		uint64_t version;
12335094Slling 
12345094Slling 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
12355094Slling 		    &version) == 0);
12365094Slling 
12377184Stimh 		if ((props = zpool_valid_proplist(hdl, origname,
12385320Slling 		    props, version, B_TRUE, errbuf)) == NULL) {
12395094Slling 			return (-1);
12405320Slling 		} else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
12415320Slling 			nvlist_free(props);
12425094Slling 			return (-1);
12435320Slling 		}
12445094Slling 	}
1245789Sahrens 
1246789Sahrens 	(void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1247789Sahrens 
1248789Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
12491544Seschrock 	    &zc.zc_guid) == 0);
1250789Sahrens 
12515320Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
12525320Slling 		nvlist_free(props);
12532082Seschrock 		return (-1);
12545320Slling 	}
1255789Sahrens 
12566643Seschrock 	zc.zc_cookie = (uint64_t)importfaulted;
1257789Sahrens 	ret = 0;
12584543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) {
1259789Sahrens 		char desc[1024];
1260789Sahrens 		if (newname == NULL)
1261789Sahrens 			(void) snprintf(desc, sizeof (desc),
1262789Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1263789Sahrens 			    thename);
1264789Sahrens 		else
1265789Sahrens 			(void) snprintf(desc, sizeof (desc),
1266789Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1267789Sahrens 			    origname, thename);
1268789Sahrens 
1269789Sahrens 		switch (errno) {
12701544Seschrock 		case ENOTSUP:
12711544Seschrock 			/*
12721544Seschrock 			 * Unsupported version.
12731544Seschrock 			 */
12742082Seschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, desc);
12751544Seschrock 			break;
12761544Seschrock 
12772174Seschrock 		case EINVAL:
12782174Seschrock 			(void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
12792174Seschrock 			break;
12802174Seschrock 
1281789Sahrens 		default:
12822082Seschrock 			(void) zpool_standard_error(hdl, errno, desc);
1283789Sahrens 		}
1284789Sahrens 
1285789Sahrens 		ret = -1;
1286789Sahrens 	} else {
1287789Sahrens 		zpool_handle_t *zhp;
12884543Smarks 
1289789Sahrens 		/*
1290789Sahrens 		 * This should never fail, but play it safe anyway.
1291789Sahrens 		 */
12922142Seschrock 		if (zpool_open_silent(hdl, thename, &zhp) != 0) {
12932142Seschrock 			ret = -1;
12942142Seschrock 		} else if (zhp != NULL) {
1295789Sahrens 			ret = zpool_create_zvol_links(zhp);
1296789Sahrens 			zpool_close(zhp);
1297789Sahrens 		}
12984543Smarks 
1299789Sahrens 	}
1300789Sahrens 
13012676Seschrock 	zcmd_free_nvlists(&zc);
13025320Slling 	nvlist_free(props);
13035320Slling 
1304789Sahrens 	return (ret);
1305789Sahrens }
1306789Sahrens 
1307789Sahrens /*
1308789Sahrens  * Scrub the pool.
1309789Sahrens  */
1310789Sahrens int
1311789Sahrens zpool_scrub(zpool_handle_t *zhp, pool_scrub_type_t type)
1312789Sahrens {
1313789Sahrens 	zfs_cmd_t zc = { 0 };
1314789Sahrens 	char msg[1024];
13152082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1316789Sahrens 
1317789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1318789Sahrens 	zc.zc_cookie = type;
1319789Sahrens 
13204543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCRUB, &zc) == 0)
1321789Sahrens 		return (0);
1322789Sahrens 
1323789Sahrens 	(void) snprintf(msg, sizeof (msg),
1324789Sahrens 	    dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1325789Sahrens 
13262082Seschrock 	if (errno == EBUSY)
13272082Seschrock 		return (zfs_error(hdl, EZFS_RESILVERING, msg));
13282082Seschrock 	else
13292082Seschrock 		return (zpool_standard_error(hdl, errno, msg));
1330789Sahrens }
1331789Sahrens 
13322468Sek110237 /*
13332468Sek110237  * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
13342468Sek110237  * spare; but FALSE if its an INUSE spare.
13352468Sek110237  */
13362082Seschrock static nvlist_t *
13372082Seschrock vdev_to_nvlist_iter(nvlist_t *nv, const char *search, uint64_t guid,
13387326SEric.Schrock@Sun.COM     boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
13391544Seschrock {
13401544Seschrock 	uint_t c, children;
13411544Seschrock 	nvlist_t **child;
13422082Seschrock 	uint64_t theguid, present;
13431544Seschrock 	char *path;
13441544Seschrock 	uint64_t wholedisk = 0;
13452082Seschrock 	nvlist_t *ret;
13467326SEric.Schrock@Sun.COM 	uint64_t is_log;
13471544Seschrock 
13482082Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &theguid) == 0);
13491544Seschrock 
13501544Seschrock 	if (search == NULL &&
13511544Seschrock 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &present) == 0) {
13521544Seschrock 		/*
13531544Seschrock 		 * If the device has never been present since import, the only
13541544Seschrock 		 * reliable way to match the vdev is by GUID.
13551544Seschrock 		 */
13562082Seschrock 		if (theguid == guid)
13572082Seschrock 			return (nv);
13581544Seschrock 	} else if (search != NULL &&
13591544Seschrock 	    nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
13601544Seschrock 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
13611544Seschrock 		    &wholedisk);
13621544Seschrock 		if (wholedisk) {
13631544Seschrock 			/*
13641544Seschrock 			 * For whole disks, the internal path has 's0', but the
13651544Seschrock 			 * path passed in by the user doesn't.
13661544Seschrock 			 */
13671544Seschrock 			if (strlen(search) == strlen(path) - 2 &&
13681544Seschrock 			    strncmp(search, path, strlen(search)) == 0)
13692082Seschrock 				return (nv);
13701544Seschrock 		} else if (strcmp(search, path) == 0) {
13712082Seschrock 			return (nv);
13721544Seschrock 		}
13731544Seschrock 	}
13741544Seschrock 
13751544Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
13761544Seschrock 	    &child, &children) != 0)
13772082Seschrock 		return (NULL);
13781544Seschrock 
13797326SEric.Schrock@Sun.COM 	for (c = 0; c < children; c++) {
13802082Seschrock 		if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
13817326SEric.Schrock@Sun.COM 		    avail_spare, l2cache, NULL)) != NULL) {
13827326SEric.Schrock@Sun.COM 			/*
13837326SEric.Schrock@Sun.COM 			 * The 'is_log' value is only set for the toplevel
13847326SEric.Schrock@Sun.COM 			 * vdev, not the leaf vdevs.  So we always lookup the
13857326SEric.Schrock@Sun.COM 			 * log device from the root of the vdev tree (where
13867326SEric.Schrock@Sun.COM 			 * 'log' is non-NULL).
13877326SEric.Schrock@Sun.COM 			 */
13887326SEric.Schrock@Sun.COM 			if (log != NULL &&
13897326SEric.Schrock@Sun.COM 			    nvlist_lookup_uint64(child[c],
13907326SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
13917326SEric.Schrock@Sun.COM 			    is_log) {
13927326SEric.Schrock@Sun.COM 				*log = B_TRUE;
13937326SEric.Schrock@Sun.COM 			}
13941544Seschrock 			return (ret);
13957326SEric.Schrock@Sun.COM 		}
13967326SEric.Schrock@Sun.COM 	}
13971544Seschrock 
13982082Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
13992082Seschrock 	    &child, &children) == 0) {
14002082Seschrock 		for (c = 0; c < children; c++) {
14012082Seschrock 			if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
14027326SEric.Schrock@Sun.COM 			    avail_spare, l2cache, NULL)) != NULL) {
14032468Sek110237 				*avail_spare = B_TRUE;
14042082Seschrock 				return (ret);
14052082Seschrock 			}
14062082Seschrock 		}
14072082Seschrock 	}
14082082Seschrock 
14095450Sbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
14105450Sbrendan 	    &child, &children) == 0) {
14115450Sbrendan 		for (c = 0; c < children; c++) {
14125450Sbrendan 			if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
14137326SEric.Schrock@Sun.COM 			    avail_spare, l2cache, NULL)) != NULL) {
14145450Sbrendan 				*l2cache = B_TRUE;
14155450Sbrendan 				return (ret);
14165450Sbrendan 			}
14175450Sbrendan 		}
14185450Sbrendan 	}
14195450Sbrendan 
14202082Seschrock 	return (NULL);
14211544Seschrock }
14221544Seschrock 
14232082Seschrock nvlist_t *
14245450Sbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
14257326SEric.Schrock@Sun.COM     boolean_t *l2cache, boolean_t *log)
14261544Seschrock {
14271544Seschrock 	char buf[MAXPATHLEN];
14281544Seschrock 	const char *search;
14291544Seschrock 	char *end;
14301544Seschrock 	nvlist_t *nvroot;
14311544Seschrock 	uint64_t guid;
14321544Seschrock 
14331613Seschrock 	guid = strtoull(path, &end, 10);
14341544Seschrock 	if (guid != 0 && *end == '\0') {
14351544Seschrock 		search = NULL;
14361544Seschrock 	} else if (path[0] != '/') {
14371544Seschrock 		(void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path);
14381544Seschrock 		search = buf;
14391544Seschrock 	} else {
14401544Seschrock 		search = path;
14411544Seschrock 	}
14421544Seschrock 
14431544Seschrock 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
14441544Seschrock 	    &nvroot) == 0);
14451544Seschrock 
14462468Sek110237 	*avail_spare = B_FALSE;
14475450Sbrendan 	*l2cache = B_FALSE;
14487326SEric.Schrock@Sun.COM 	if (log != NULL)
14497326SEric.Schrock@Sun.COM 		*log = B_FALSE;
14505450Sbrendan 	return (vdev_to_nvlist_iter(nvroot, search, guid, avail_spare,
14517326SEric.Schrock@Sun.COM 	    l2cache, log));
14522468Sek110237 }
14532468Sek110237 
14547656SSherry.Moore@Sun.COM static int
14557656SSherry.Moore@Sun.COM vdev_online(nvlist_t *nv)
14567656SSherry.Moore@Sun.COM {
14577656SSherry.Moore@Sun.COM 	uint64_t ival;
14587656SSherry.Moore@Sun.COM 
14597656SSherry.Moore@Sun.COM 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
14607656SSherry.Moore@Sun.COM 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
14617656SSherry.Moore@Sun.COM 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
14627656SSherry.Moore@Sun.COM 		return (0);
14637656SSherry.Moore@Sun.COM 
14647656SSherry.Moore@Sun.COM 	return (1);
14657656SSherry.Moore@Sun.COM }
14667656SSherry.Moore@Sun.COM 
14677656SSherry.Moore@Sun.COM /*
14687656SSherry.Moore@Sun.COM  * Get phys_path for a root pool
14697656SSherry.Moore@Sun.COM  * Return 0 on success; non-zeron on failure.
14707656SSherry.Moore@Sun.COM  */
14717656SSherry.Moore@Sun.COM int
14727656SSherry.Moore@Sun.COM zpool_get_physpath(zpool_handle_t *zhp, char *physpath)
14737656SSherry.Moore@Sun.COM {
14747656SSherry.Moore@Sun.COM 	nvlist_t *vdev_root;
14757656SSherry.Moore@Sun.COM 	nvlist_t **child;
14767656SSherry.Moore@Sun.COM 	uint_t count;
14777656SSherry.Moore@Sun.COM 	int i;
14787656SSherry.Moore@Sun.COM 
14797656SSherry.Moore@Sun.COM 	/*
14807656SSherry.Moore@Sun.COM 	 * Make sure this is a root pool, as phys_path doesn't mean
14817656SSherry.Moore@Sun.COM 	 * anything to a non-root pool.
14827656SSherry.Moore@Sun.COM 	 */
1483*7965SGeorge.Wilson@Sun.COM 	if (!pool_is_bootable(zhp))
14847656SSherry.Moore@Sun.COM 		return (-1);
14857656SSherry.Moore@Sun.COM 
14867656SSherry.Moore@Sun.COM 	verify(nvlist_lookup_nvlist(zhp->zpool_config,
14877656SSherry.Moore@Sun.COM 	    ZPOOL_CONFIG_VDEV_TREE, &vdev_root) == 0);
14887656SSherry.Moore@Sun.COM 
14897656SSherry.Moore@Sun.COM 	if (nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
14907656SSherry.Moore@Sun.COM 	    &child, &count) != 0)
14917656SSherry.Moore@Sun.COM 		return (-2);
14927656SSherry.Moore@Sun.COM 
14937656SSherry.Moore@Sun.COM 	for (i = 0; i < count; i++) {
14947656SSherry.Moore@Sun.COM 		nvlist_t **child2;
14957656SSherry.Moore@Sun.COM 		uint_t count2;
14967656SSherry.Moore@Sun.COM 		char *type;
14977656SSherry.Moore@Sun.COM 		char *tmppath;
14987656SSherry.Moore@Sun.COM 		int j;
14997656SSherry.Moore@Sun.COM 
15007656SSherry.Moore@Sun.COM 		if (nvlist_lookup_string(child[i], ZPOOL_CONFIG_TYPE, &type)
15017656SSherry.Moore@Sun.COM 		    != 0)
15027656SSherry.Moore@Sun.COM 			return (-3);
15037656SSherry.Moore@Sun.COM 
15047656SSherry.Moore@Sun.COM 		if (strcmp(type, VDEV_TYPE_DISK) == 0) {
15057656SSherry.Moore@Sun.COM 			if (!vdev_online(child[i]))
15067656SSherry.Moore@Sun.COM 				return (-8);
15077656SSherry.Moore@Sun.COM 			verify(nvlist_lookup_string(child[i],
15087656SSherry.Moore@Sun.COM 			    ZPOOL_CONFIG_PHYS_PATH, &tmppath) == 0);
15097656SSherry.Moore@Sun.COM 			(void) strncpy(physpath, tmppath, strlen(tmppath));
15107656SSherry.Moore@Sun.COM 		} else if (strcmp(type, VDEV_TYPE_MIRROR) == 0) {
15117656SSherry.Moore@Sun.COM 			if (nvlist_lookup_nvlist_array(child[i],
15127656SSherry.Moore@Sun.COM 			    ZPOOL_CONFIG_CHILDREN, &child2, &count2) != 0)
15137656SSherry.Moore@Sun.COM 				return (-4);
15147656SSherry.Moore@Sun.COM 
15157656SSherry.Moore@Sun.COM 			for (j = 0; j < count2; j++) {
15167656SSherry.Moore@Sun.COM 				if (!vdev_online(child2[j]))
15177656SSherry.Moore@Sun.COM 					return (-8);
15187656SSherry.Moore@Sun.COM 				if (nvlist_lookup_string(child2[j],
15197656SSherry.Moore@Sun.COM 				    ZPOOL_CONFIG_PHYS_PATH, &tmppath) != 0)
15207656SSherry.Moore@Sun.COM 					return (-5);
15217656SSherry.Moore@Sun.COM 
15227656SSherry.Moore@Sun.COM 				if ((strlen(physpath) + strlen(tmppath)) >
15237656SSherry.Moore@Sun.COM 				    MAXNAMELEN)
15247656SSherry.Moore@Sun.COM 					return (-6);
15257656SSherry.Moore@Sun.COM 
15267656SSherry.Moore@Sun.COM 				if (strlen(physpath) == 0) {
15277656SSherry.Moore@Sun.COM 					(void) strncpy(physpath, tmppath,
15287656SSherry.Moore@Sun.COM 					    strlen(tmppath));
15297656SSherry.Moore@Sun.COM 				} else {
15307656SSherry.Moore@Sun.COM 					(void) strcat(physpath, " ");
15317656SSherry.Moore@Sun.COM 					(void) strcat(physpath, tmppath);
15327656SSherry.Moore@Sun.COM 				}
15337656SSherry.Moore@Sun.COM 			}
15347656SSherry.Moore@Sun.COM 		} else {
15357656SSherry.Moore@Sun.COM 			return (-7);
15367656SSherry.Moore@Sun.COM 		}
15377656SSherry.Moore@Sun.COM 	}
15387656SSherry.Moore@Sun.COM 
15397656SSherry.Moore@Sun.COM 	return (0);
15407656SSherry.Moore@Sun.COM }
15417656SSherry.Moore@Sun.COM 
15422468Sek110237 /*
15435450Sbrendan  * Returns TRUE if the given guid corresponds to the given type.
15445450Sbrendan  * This is used to check for hot spares (INUSE or not), and level 2 cache
15455450Sbrendan  * devices.
15462468Sek110237  */
15472468Sek110237 static boolean_t
15485450Sbrendan is_guid_type(zpool_handle_t *zhp, uint64_t guid, const char *type)
15492468Sek110237 {
15505450Sbrendan 	uint64_t target_guid;
15512468Sek110237 	nvlist_t *nvroot;
15525450Sbrendan 	nvlist_t **list;
15535450Sbrendan 	uint_t count;
15542468Sek110237 	int i;
15552468Sek110237 
15562468Sek110237 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
15572468Sek110237 	    &nvroot) == 0);
15585450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, type, &list, &count) == 0) {
15595450Sbrendan 		for (i = 0; i < count; i++) {
15605450Sbrendan 			verify(nvlist_lookup_uint64(list[i], ZPOOL_CONFIG_GUID,
15615450Sbrendan 			    &target_guid) == 0);
15625450Sbrendan 			if (guid == target_guid)
15632468Sek110237 				return (B_TRUE);
15642468Sek110237 		}
15652468Sek110237 	}
15662468Sek110237 
15672468Sek110237 	return (B_FALSE);
15681544Seschrock }
15691544Seschrock 
1570789Sahrens /*
15714451Seschrock  * Bring the specified vdev online.   The 'flags' parameter is a set of the
15724451Seschrock  * ZFS_ONLINE_* flags.
1573789Sahrens  */
1574789Sahrens int
15754451Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
15764451Seschrock     vdev_state_t *newstate)
1577789Sahrens {
1578789Sahrens 	zfs_cmd_t zc = { 0 };
1579789Sahrens 	char msg[1024];
15802082Seschrock 	nvlist_t *tgt;
15815450Sbrendan 	boolean_t avail_spare, l2cache;
15822082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1583789Sahrens 
15841544Seschrock 	(void) snprintf(msg, sizeof (msg),
15851544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot online %s"), path);
1586789Sahrens 
15871544Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
15887326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
15897326SEric.Schrock@Sun.COM 	    NULL)) == NULL)
15902082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
1591789Sahrens 
15922468Sek110237 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
15932468Sek110237 
15945450Sbrendan 	if (avail_spare ||
15955450Sbrendan 	    is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
15962082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
15972082Seschrock 
15984451Seschrock 	zc.zc_cookie = VDEV_STATE_ONLINE;
15994451Seschrock 	zc.zc_obj = flags;
16004451Seschrock 
16014543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0)
16024451Seschrock 		return (zpool_standard_error(hdl, errno, msg));
16034451Seschrock 
16044451Seschrock 	*newstate = zc.zc_cookie;
16054451Seschrock 	return (0);
1606789Sahrens }
1607789Sahrens 
1608789Sahrens /*
1609789Sahrens  * Take the specified vdev offline
1610789Sahrens  */
1611789Sahrens int
16124451Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
1613789Sahrens {
1614789Sahrens 	zfs_cmd_t zc = { 0 };
1615789Sahrens 	char msg[1024];
16162082Seschrock 	nvlist_t *tgt;
16175450Sbrendan 	boolean_t avail_spare, l2cache;
16182082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1619789Sahrens 
16201544Seschrock 	(void) snprintf(msg, sizeof (msg),
16211544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
16221544Seschrock 
1623789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
16247326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
16257326SEric.Schrock@Sun.COM 	    NULL)) == NULL)
16262082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
16272082Seschrock 
16282468Sek110237 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
16292468Sek110237 
16305450Sbrendan 	if (avail_spare ||
16315450Sbrendan 	    is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
16322082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
16332082Seschrock 
16344451Seschrock 	zc.zc_cookie = VDEV_STATE_OFFLINE;
16354451Seschrock 	zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
16361485Slling 
16374543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
1638789Sahrens 		return (0);
1639789Sahrens 
1640789Sahrens 	switch (errno) {
16412082Seschrock 	case EBUSY:
1642789Sahrens 
1643789Sahrens 		/*
1644789Sahrens 		 * There are no other replicas of this device.
1645789Sahrens 		 */
16462082Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
16472082Seschrock 
16482082Seschrock 	default:
16492082Seschrock 		return (zpool_standard_error(hdl, errno, msg));
16502082Seschrock 	}
16512082Seschrock }
1652789Sahrens 
16532082Seschrock /*
16544451Seschrock  * Mark the given vdev faulted.
16554451Seschrock  */
16564451Seschrock int
16574451Seschrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid)
16584451Seschrock {
16594451Seschrock 	zfs_cmd_t zc = { 0 };
16604451Seschrock 	char msg[1024];
16614451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
16624451Seschrock 
16634451Seschrock 	(void) snprintf(msg, sizeof (msg),
16644451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
16654451Seschrock 
16664451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
16674451Seschrock 	zc.zc_guid = guid;
16684451Seschrock 	zc.zc_cookie = VDEV_STATE_FAULTED;
16694451Seschrock 
16704451Seschrock 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
16714451Seschrock 		return (0);
16724451Seschrock 
16734451Seschrock 	switch (errno) {
16744451Seschrock 	case EBUSY:
16754451Seschrock 
16764451Seschrock 		/*
16774451Seschrock 		 * There are no other replicas of this device.
16784451Seschrock 		 */
16794451Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
16804451Seschrock 
16814451Seschrock 	default:
16824451Seschrock 		return (zpool_standard_error(hdl, errno, msg));
16834451Seschrock 	}
16844451Seschrock 
16854451Seschrock }
16864451Seschrock 
16874451Seschrock /*
16884451Seschrock  * Mark the given vdev degraded.
16894451Seschrock  */
16904451Seschrock int
16914451Seschrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid)
16924451Seschrock {
16934451Seschrock 	zfs_cmd_t zc = { 0 };
16944451Seschrock 	char msg[1024];
16954451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
16964451Seschrock 
16974451Seschrock 	(void) snprintf(msg, sizeof (msg),
16984451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
16994451Seschrock 
17004451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
17014451Seschrock 	zc.zc_guid = guid;
17024451Seschrock 	zc.zc_cookie = VDEV_STATE_DEGRADED;
17034451Seschrock 
17044451Seschrock 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
17054451Seschrock 		return (0);
17064451Seschrock 
17074451Seschrock 	return (zpool_standard_error(hdl, errno, msg));
17084451Seschrock }
17094451Seschrock 
17104451Seschrock /*
17112082Seschrock  * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
17122082Seschrock  * a hot spare.
17132082Seschrock  */
17142082Seschrock static boolean_t
17152082Seschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
17162082Seschrock {
17172082Seschrock 	nvlist_t **child;
17182082Seschrock 	uint_t c, children;
17192082Seschrock 	char *type;
17202082Seschrock 
17212082Seschrock 	if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
17222082Seschrock 	    &children) == 0) {
17232082Seschrock 		verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
17242082Seschrock 		    &type) == 0);
17252082Seschrock 
17262082Seschrock 		if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
17272082Seschrock 		    children == 2 && child[which] == tgt)
17282082Seschrock 			return (B_TRUE);
17292082Seschrock 
17302082Seschrock 		for (c = 0; c < children; c++)
17312082Seschrock 			if (is_replacing_spare(child[c], tgt, which))
17322082Seschrock 				return (B_TRUE);
1733789Sahrens 	}
17342082Seschrock 
17352082Seschrock 	return (B_FALSE);
1736789Sahrens }
1737789Sahrens 
1738789Sahrens /*
1739789Sahrens  * Attach new_disk (fully described by nvroot) to old_disk.
17404527Sperrin  * If 'replacing' is specified, the new disk will replace the old one.
1741789Sahrens  */
1742789Sahrens int
1743789Sahrens zpool_vdev_attach(zpool_handle_t *zhp,
1744789Sahrens     const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
1745789Sahrens {
1746789Sahrens 	zfs_cmd_t zc = { 0 };
1747789Sahrens 	char msg[1024];
1748789Sahrens 	int ret;
17492082Seschrock 	nvlist_t *tgt;
17507326SEric.Schrock@Sun.COM 	boolean_t avail_spare, l2cache, islog;
17517326SEric.Schrock@Sun.COM 	uint64_t val;
17527041Seschrock 	char *path, *newname;
17532082Seschrock 	nvlist_t **child;
17542082Seschrock 	uint_t children;
17552082Seschrock 	nvlist_t *config_root;
17562082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1757*7965SGeorge.Wilson@Sun.COM 	boolean_t rootpool = pool_is_bootable(zhp);
1758789Sahrens 
17591544Seschrock 	if (replacing)
17601544Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
17611544Seschrock 		    "cannot replace %s with %s"), old_disk, new_disk);
17621544Seschrock 	else
17631544Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
17641544Seschrock 		    "cannot attach %s to %s"), new_disk, old_disk);
17651544Seschrock 
1766*7965SGeorge.Wilson@Sun.COM 	/*
1767*7965SGeorge.Wilson@Sun.COM 	 * If this is a root pool, make sure that we're not attaching an
1768*7965SGeorge.Wilson@Sun.COM 	 * EFI labeled device.
1769*7965SGeorge.Wilson@Sun.COM 	 */
1770*7965SGeorge.Wilson@Sun.COM 	if (rootpool && pool_uses_efi(nvroot)) {
1771*7965SGeorge.Wilson@Sun.COM 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1772*7965SGeorge.Wilson@Sun.COM 		    "EFI labeled devices are not supported on root pools."));
1773*7965SGeorge.Wilson@Sun.COM 		return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1774*7965SGeorge.Wilson@Sun.COM 	}
1775*7965SGeorge.Wilson@Sun.COM 
1776789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
17777326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
17787326SEric.Schrock@Sun.COM 	    &islog)) == 0)
17792082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
17802082Seschrock 
17812468Sek110237 	if (avail_spare)
17822082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
17832082Seschrock 
17845450Sbrendan 	if (l2cache)
17855450Sbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
17865450Sbrendan 
17872082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
17882082Seschrock 	zc.zc_cookie = replacing;
17892082Seschrock 
17902082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
17912082Seschrock 	    &child, &children) != 0 || children != 1) {
17922082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17932082Seschrock 		    "new device must be a single disk"));
17942082Seschrock 		return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
17951544Seschrock 	}
17962082Seschrock 
17972082Seschrock 	verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
17982082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
17992082Seschrock 
18007041Seschrock 	if ((newname = zpool_vdev_name(NULL, NULL, child[0])) == NULL)
18017041Seschrock 		return (-1);
18027041Seschrock 
18032082Seschrock 	/*
18042082Seschrock 	 * If the target is a hot spare that has been swapped in, we can only
18052082Seschrock 	 * replace it with another hot spare.
18062082Seschrock 	 */
18072082Seschrock 	if (replacing &&
18082082Seschrock 	    nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
18097326SEric.Schrock@Sun.COM 	    (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
18107326SEric.Schrock@Sun.COM 	    NULL) == NULL || !avail_spare) &&
18117326SEric.Schrock@Sun.COM 	    is_replacing_spare(config_root, tgt, 1)) {
18122082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18132082Seschrock 		    "can only be replaced by another hot spare"));
18147041Seschrock 		free(newname);
18152082Seschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
18162082Seschrock 	}
18172082Seschrock 
18182082Seschrock 	/*
18192082Seschrock 	 * If we are attempting to replace a spare, it canot be applied to an
18202082Seschrock 	 * already spared device.
18212082Seschrock 	 */
18222082Seschrock 	if (replacing &&
18232082Seschrock 	    nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 &&
18247326SEric.Schrock@Sun.COM 	    zpool_find_vdev(zhp, newname, &avail_spare,
18257326SEric.Schrock@Sun.COM 	    &l2cache, NULL) != NULL && avail_spare &&
18267326SEric.Schrock@Sun.COM 	    is_replacing_spare(config_root, tgt, 0)) {
18272082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18282082Seschrock 		    "device has already been replaced with a spare"));
18297041Seschrock 		free(newname);
18302082Seschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
18312082Seschrock 	}
1832789Sahrens 
18337041Seschrock 	free(newname);
18347041Seschrock 
18355094Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
18362082Seschrock 		return (-1);
1837789Sahrens 
18384543Smarks 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc);
1839789Sahrens 
18402676Seschrock 	zcmd_free_nvlists(&zc);
1841789Sahrens 
1842*7965SGeorge.Wilson@Sun.COM 	if (ret == 0) {
1843*7965SGeorge.Wilson@Sun.COM 		if (rootpool) {
1844*7965SGeorge.Wilson@Sun.COM 			/*
1845*7965SGeorge.Wilson@Sun.COM 			 * XXX - This should be removed once we can
1846*7965SGeorge.Wilson@Sun.COM 			 * automatically install the bootblocks on the
1847*7965SGeorge.Wilson@Sun.COM 			 * newly attached disk.
1848*7965SGeorge.Wilson@Sun.COM 			 */
1849*7965SGeorge.Wilson@Sun.COM 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Please "
1850*7965SGeorge.Wilson@Sun.COM 			    "be sure to invoke %s to make '%s' bootable.\n"),
1851*7965SGeorge.Wilson@Sun.COM 			    BOOTCMD, new_disk);
1852*7965SGeorge.Wilson@Sun.COM 		}
1853789Sahrens 		return (0);
1854*7965SGeorge.Wilson@Sun.COM 	}
1855789Sahrens 
1856789Sahrens 	switch (errno) {
18571544Seschrock 	case ENOTSUP:
1858789Sahrens 		/*
1859789Sahrens 		 * Can't attach to or replace this type of vdev.
1860789Sahrens 		 */
18614527Sperrin 		if (replacing) {
18627326SEric.Schrock@Sun.COM 			if (islog)
18634527Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18644527Sperrin 				    "cannot replace a log with a spare"));
18654527Sperrin 			else
18664527Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18674527Sperrin 				    "cannot replace a replacing device"));
18684527Sperrin 		} else {
18692082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18702082Seschrock 			    "can only attach to mirrors and top-level "
18712082Seschrock 			    "disks"));
18724527Sperrin 		}
18732082Seschrock 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
1874789Sahrens 		break;
1875789Sahrens 
18761544Seschrock 	case EINVAL:
1877789Sahrens 		/*
1878789Sahrens 		 * The new device must be a single disk.
1879789Sahrens 		 */
18802082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18812082Seschrock 		    "new device must be a single disk"));
18822082Seschrock 		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
1883789Sahrens 		break;
1884789Sahrens 
18851544Seschrock 	case EBUSY:
18862082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
18872082Seschrock 		    new_disk);
18882082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1889789Sahrens 		break;
1890789Sahrens 
18911544Seschrock 	case EOVERFLOW:
1892789Sahrens 		/*
1893789Sahrens 		 * The new device is too small.
1894789Sahrens 		 */
18952082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18962082Seschrock 		    "device is too small"));
18972082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1898789Sahrens 		break;
1899789Sahrens 
19001544Seschrock 	case EDOM:
1901789Sahrens 		/*
1902789Sahrens 		 * The new device has a different alignment requirement.
1903789Sahrens 		 */
19042082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19052082Seschrock 		    "devices have different sector alignment"));
19062082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1907789Sahrens 		break;
1908789Sahrens 
19091544Seschrock 	case ENAMETOOLONG:
1910789Sahrens 		/*
1911789Sahrens 		 * The resulting top-level vdev spec won't fit in the label.
1912789Sahrens 		 */
19132082Seschrock 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
1914789Sahrens 		break;
1915789Sahrens 
19161544Seschrock 	default:
19172082Seschrock 		(void) zpool_standard_error(hdl, errno, msg);
1918789Sahrens 	}
1919789Sahrens 
19202082Seschrock 	return (-1);
1921789Sahrens }
1922789Sahrens 
1923789Sahrens /*
1924789Sahrens  * Detach the specified device.
1925789Sahrens  */
1926789Sahrens int
1927789Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
1928789Sahrens {
1929789Sahrens 	zfs_cmd_t zc = { 0 };
1930789Sahrens 	char msg[1024];
19312082Seschrock 	nvlist_t *tgt;
19325450Sbrendan 	boolean_t avail_spare, l2cache;
19332082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1934789Sahrens 
19351544Seschrock 	(void) snprintf(msg, sizeof (msg),
19361544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
19371544Seschrock 
1938789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
19397326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
19407326SEric.Schrock@Sun.COM 	    NULL)) == 0)
19412082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
1942789Sahrens 
19432468Sek110237 	if (avail_spare)
19442082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
19452082Seschrock 
19465450Sbrendan 	if (l2cache)
19475450Sbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
19485450Sbrendan 
19492082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
19502082Seschrock 
19514543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
1952789Sahrens 		return (0);
1953789Sahrens 
1954789Sahrens 	switch (errno) {
1955789Sahrens 
19561544Seschrock 	case ENOTSUP:
1957789Sahrens 		/*
1958789Sahrens 		 * Can't detach from this type of vdev.
1959789Sahrens 		 */
19602082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
19612082Seschrock 		    "applicable to mirror and replacing vdevs"));
19622082Seschrock 		(void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg);
1963789Sahrens 		break;
1964789Sahrens 
19651544Seschrock 	case EBUSY:
1966789Sahrens 		/*
1967789Sahrens 		 * There are no other replicas of this device.
1968789Sahrens 		 */
19692082Seschrock 		(void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
1970789Sahrens 		break;
1971789Sahrens 
19721544Seschrock 	default:
19732082Seschrock 		(void) zpool_standard_error(hdl, errno, msg);
19741544Seschrock 	}
19751544Seschrock 
19762082Seschrock 	return (-1);
19772082Seschrock }
19782082Seschrock 
19792082Seschrock /*
19805450Sbrendan  * Remove the given device.  Currently, this is supported only for hot spares
19815450Sbrendan  * and level 2 cache devices.
19822082Seschrock  */
19832082Seschrock int
19842082Seschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
19852082Seschrock {
19862082Seschrock 	zfs_cmd_t zc = { 0 };
19872082Seschrock 	char msg[1024];
19882082Seschrock 	nvlist_t *tgt;
19895450Sbrendan 	boolean_t avail_spare, l2cache;
19902082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
19912082Seschrock 
19922082Seschrock 	(void) snprintf(msg, sizeof (msg),
19932082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
19942082Seschrock 
19952082Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
19967326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
19977326SEric.Schrock@Sun.COM 	    NULL)) == 0)
19982082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
19992082Seschrock 
20005450Sbrendan 	if (!avail_spare && !l2cache) {
20012082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
20025450Sbrendan 		    "only inactive hot spares or cache devices "
20035450Sbrendan 		    "can be removed"));
20042082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
20052082Seschrock 	}
20062082Seschrock 
20072082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
20082082Seschrock 
20094543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
20102082Seschrock 		return (0);
20112082Seschrock 
20122082Seschrock 	return (zpool_standard_error(hdl, errno, msg));
20131544Seschrock }
20141544Seschrock 
20151544Seschrock /*
20161544Seschrock  * Clear the errors for the pool, or the particular device if specified.
20171544Seschrock  */
20181544Seschrock int
20191544Seschrock zpool_clear(zpool_handle_t *zhp, const char *path)
20201544Seschrock {
20211544Seschrock 	zfs_cmd_t zc = { 0 };
20221544Seschrock 	char msg[1024];
20232082Seschrock 	nvlist_t *tgt;
20245450Sbrendan 	boolean_t avail_spare, l2cache;
20252082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
20261544Seschrock 
20271544Seschrock 	if (path)
20281544Seschrock 		(void) snprintf(msg, sizeof (msg),
20291544Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
20302676Seschrock 		    path);
20311544Seschrock 	else
20321544Seschrock 		(void) snprintf(msg, sizeof (msg),
20331544Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
20341544Seschrock 		    zhp->zpool_name);
20351544Seschrock 
20361544Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
20372082Seschrock 	if (path) {
20385450Sbrendan 		if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
20397326SEric.Schrock@Sun.COM 		    &l2cache, NULL)) == 0)
20402082Seschrock 			return (zfs_error(hdl, EZFS_NODEVICE, msg));
20412082Seschrock 
20425450Sbrendan 		/*
20435450Sbrendan 		 * Don't allow error clearing for hot spares.  Do allow
20445450Sbrendan 		 * error clearing for l2cache devices.
20455450Sbrendan 		 */
20462468Sek110237 		if (avail_spare)
20472082Seschrock 			return (zfs_error(hdl, EZFS_ISSPARE, msg));
20482082Seschrock 
20492082Seschrock 		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
20502082Seschrock 		    &zc.zc_guid) == 0);
20511544Seschrock 	}
20521544Seschrock 
20534543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0)
20541544Seschrock 		return (0);
20551544Seschrock 
20562082Seschrock 	return (zpool_standard_error(hdl, errno, msg));
2057789Sahrens }
2058789Sahrens 
20593126Sahl /*
20604451Seschrock  * Similar to zpool_clear(), but takes a GUID (used by fmd).
20614451Seschrock  */
20624451Seschrock int
20634451Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
20644451Seschrock {
20654451Seschrock 	zfs_cmd_t zc = { 0 };
20664451Seschrock 	char msg[1024];
20674451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
20684451Seschrock 
20694451Seschrock 	(void) snprintf(msg, sizeof (msg),
20704451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
20714451Seschrock 	    guid);
20724451Seschrock 
20734451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
20744451Seschrock 	zc.zc_guid = guid;
20754451Seschrock 
20764451Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
20774451Seschrock 		return (0);
20784451Seschrock 
20794451Seschrock 	return (zpool_standard_error(hdl, errno, msg));
20804451Seschrock }
20814451Seschrock 
20824451Seschrock /*
20833126Sahl  * Iterate over all zvols in a given pool by walking the /dev/zvol/dsk/<pool>
20843126Sahl  * hierarchy.
20853126Sahl  */
20863126Sahl int
20873126Sahl zpool_iter_zvol(zpool_handle_t *zhp, int (*cb)(const char *, void *),
20883126Sahl     void *data)
2089789Sahrens {
20903126Sahl 	libzfs_handle_t *hdl = zhp->zpool_hdl;
20913126Sahl 	char (*paths)[MAXPATHLEN];
20923126Sahl 	size_t size = 4;
20933126Sahl 	int curr, fd, base, ret = 0;
20943126Sahl 	DIR *dirp;
20953126Sahl 	struct dirent *dp;
20963126Sahl 	struct stat st;
20973126Sahl 
20983126Sahl 	if ((base = open("/dev/zvol/dsk", O_RDONLY)) < 0)
20993126Sahl 		return (errno == ENOENT ? 0 : -1);
21003126Sahl 
21013126Sahl 	if (fstatat(base, zhp->zpool_name, &st, 0) != 0) {
21023126Sahl 		int err = errno;
21033126Sahl 		(void) close(base);
21043126Sahl 		return (err == ENOENT ? 0 : -1);
21053126Sahl 	}
2106789Sahrens 
2107789Sahrens 	/*
21083126Sahl 	 * Oddly this wasn't a directory -- ignore that failure since we
21093126Sahl 	 * know there are no links lower in the (non-existant) hierarchy.
2110789Sahrens 	 */
21113126Sahl 	if (!S_ISDIR(st.st_mode)) {
21123126Sahl 		(void) close(base);
21133126Sahl 		return (0);
21143126Sahl 	}
21153126Sahl 
21163126Sahl 	if ((paths = zfs_alloc(hdl, size * sizeof (paths[0]))) == NULL) {
21173126Sahl 		(void) close(base);
21183126Sahl 		return (-1);
2119789Sahrens 	}
2120789Sahrens 
21213126Sahl 	(void) strlcpy(paths[0], zhp->zpool_name, sizeof (paths[0]));
21223126Sahl 	curr = 0;
21233126Sahl 
21243126Sahl 	while (curr >= 0) {
21253126Sahl 		if (fstatat(base, paths[curr], &st, AT_SYMLINK_NOFOLLOW) != 0)
21263126Sahl 			goto err;
21273126Sahl 
21283126Sahl 		if (S_ISDIR(st.st_mode)) {
21293126Sahl 			if ((fd = openat(base, paths[curr], O_RDONLY)) < 0)
21303126Sahl 				goto err;
21313126Sahl 
21323126Sahl 			if ((dirp = fdopendir(fd)) == NULL) {
21333126Sahl 				(void) close(fd);
21343126Sahl 				goto err;
21353126Sahl 			}
21363126Sahl 
21373126Sahl 			while ((dp = readdir(dirp)) != NULL) {
21383126Sahl 				if (dp->d_name[0] == '.')
21393126Sahl 					continue;
21403126Sahl 
21413126Sahl 				if (curr + 1 == size) {
21423126Sahl 					paths = zfs_realloc(hdl, paths,
21433126Sahl 					    size * sizeof (paths[0]),
21443126Sahl 					    size * 2 * sizeof (paths[0]));
21453126Sahl 					if (paths == NULL) {
21463126Sahl 						(void) closedir(dirp);
21473126Sahl 						(void) close(fd);
21483126Sahl 						goto err;
21493126Sahl 					}
21503126Sahl 
21513126Sahl 					size *= 2;
21523126Sahl 				}
21533126Sahl 
21543126Sahl 				(void) strlcpy(paths[curr + 1], paths[curr],
21553126Sahl 				    sizeof (paths[curr + 1]));
21563126Sahl 				(void) strlcat(paths[curr], "/",
21573126Sahl 				    sizeof (paths[curr]));
21583126Sahl 				(void) strlcat(paths[curr], dp->d_name,
21593126Sahl 				    sizeof (paths[curr]));
21603126Sahl 				curr++;
21613126Sahl 			}
21623126Sahl 
21633126Sahl 			(void) closedir(dirp);
21643126Sahl 
21653126Sahl 		} else {
21663126Sahl 			if ((ret = cb(paths[curr], data)) != 0)
21673126Sahl 				break;
21683126Sahl 		}
21693126Sahl 
21703126Sahl 		curr--;
21713126Sahl 	}
21723126Sahl 
21733126Sahl 	free(paths);
21743126Sahl 	(void) close(base);
21753126Sahl 
21763126Sahl 	return (ret);
21773126Sahl 
21783126Sahl err:
21793126Sahl 	free(paths);
21803126Sahl 	(void) close(base);
21813126Sahl 	return (-1);
21823126Sahl }
21833126Sahl 
21843126Sahl typedef struct zvol_cb {
21853126Sahl 	zpool_handle_t *zcb_pool;
21863126Sahl 	boolean_t zcb_create;
21873126Sahl } zvol_cb_t;
21883126Sahl 
21893126Sahl /*ARGSUSED*/
21903126Sahl static int
21913126Sahl do_zvol_create(zfs_handle_t *zhp, void *data)
21923126Sahl {
21934657Sahrens 	int ret = 0;
21943126Sahl 
21954657Sahrens 	if (ZFS_IS_VOLUME(zhp)) {
21963126Sahl 		(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
21974657Sahrens 		ret = zfs_iter_snapshots(zhp, do_zvol_create, NULL);
21984657Sahrens 	}
21993126Sahl 
22004657Sahrens 	if (ret == 0)
22014657Sahrens 		ret = zfs_iter_filesystems(zhp, do_zvol_create, NULL);
2202789Sahrens 
2203789Sahrens 	zfs_close(zhp);
22043126Sahl 
2205789Sahrens 	return (ret);
2206789Sahrens }
2207789Sahrens 
2208789Sahrens /*
2209789Sahrens  * Iterate over all zvols in the pool and make any necessary minor nodes.
2210789Sahrens  */
2211789Sahrens int
2212789Sahrens zpool_create_zvol_links(zpool_handle_t *zhp)
2213789Sahrens {
2214789Sahrens 	zfs_handle_t *zfp;
2215789Sahrens 	int ret;
2216789Sahrens 
2217789Sahrens 	/*
2218789Sahrens 	 * If the pool is unavailable, just return success.
2219789Sahrens 	 */
22202082Seschrock 	if ((zfp = make_dataset_handle(zhp->zpool_hdl,
22212082Seschrock 	    zhp->zpool_name)) == NULL)
2222789Sahrens 		return (0);
2223789Sahrens 
22244657Sahrens 	ret = zfs_iter_filesystems(zfp, do_zvol_create, NULL);
2225789Sahrens 
2226789Sahrens 	zfs_close(zfp);
2227789Sahrens 	return (ret);
2228789Sahrens }
2229789Sahrens 
22303126Sahl static int
22313126Sahl do_zvol_remove(const char *dataset, void *data)
22323126Sahl {
22333126Sahl 	zpool_handle_t *zhp = data;
22343126Sahl 
22353126Sahl 	return (zvol_remove_link(zhp->zpool_hdl, dataset));
22363126Sahl }
22373126Sahl 
2238789Sahrens /*
22393126Sahl  * Iterate over all zvols in the pool and remove any minor nodes.  We iterate
22403126Sahl  * by examining the /dev links so that a corrupted pool doesn't impede this
22413126Sahl  * operation.
2242789Sahrens  */
2243789Sahrens int
2244789Sahrens zpool_remove_zvol_links(zpool_handle_t *zhp)
2245789Sahrens {
22463126Sahl 	return (zpool_iter_zvol(zhp, do_zvol_remove, zhp));
2247789Sahrens }
22481354Seschrock 
22491354Seschrock /*
22501354Seschrock  * Convert from a devid string to a path.
22511354Seschrock  */
22521354Seschrock static char *
22531354Seschrock devid_to_path(char *devid_str)
22541354Seschrock {
22551354Seschrock 	ddi_devid_t devid;
22561354Seschrock 	char *minor;
22571354Seschrock 	char *path;
22581354Seschrock 	devid_nmlist_t *list = NULL;
22591354Seschrock 	int ret;
22601354Seschrock 
22611354Seschrock 	if (devid_str_decode(devid_str, &devid, &minor) != 0)
22621354Seschrock 		return (NULL);
22631354Seschrock 
22641354Seschrock 	ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
22651354Seschrock 
22661354Seschrock 	devid_str_free(minor);
22671354Seschrock 	devid_free(devid);
22681354Seschrock 
22691354Seschrock 	if (ret != 0)
22701354Seschrock 		return (NULL);
22711354Seschrock 
22722082Seschrock 	if ((path = strdup(list[0].devname)) == NULL)
22732082Seschrock 		return (NULL);
22742082Seschrock 
22751354Seschrock 	devid_free_nmlist(list);
22761354Seschrock 
22771354Seschrock 	return (path);
22781354Seschrock }
22791354Seschrock 
22801354Seschrock /*
22811354Seschrock  * Convert from a path to a devid string.
22821354Seschrock  */
22831354Seschrock static char *
22841354Seschrock path_to_devid(const char *path)
22851354Seschrock {
22861354Seschrock 	int fd;
22871354Seschrock 	ddi_devid_t devid;
22881354Seschrock 	char *minor, *ret;
22891354Seschrock 
22901354Seschrock 	if ((fd = open(path, O_RDONLY)) < 0)
22911354Seschrock 		return (NULL);
22921354Seschrock 
22931354Seschrock 	minor = NULL;
22941354Seschrock 	ret = NULL;
22951354Seschrock 	if (devid_get(fd, &devid) == 0) {
22961354Seschrock 		if (devid_get_minor_name(fd, &minor) == 0)
22971354Seschrock 			ret = devid_str_encode(devid, minor);
22981354Seschrock 		if (minor != NULL)
22991354Seschrock 			devid_str_free(minor);
23001354Seschrock 		devid_free(devid);
23011354Seschrock 	}
23021354Seschrock 	(void) close(fd);
23031354Seschrock 
23041354Seschrock 	return (ret);
23051354Seschrock }
23061354Seschrock 
23071354Seschrock /*
23081354Seschrock  * Issue the necessary ioctl() to update the stored path value for the vdev.  We
23091354Seschrock  * ignore any failure here, since a common case is for an unprivileged user to
23101354Seschrock  * type 'zpool status', and we'll display the correct information anyway.
23111354Seschrock  */
23121354Seschrock static void
23131354Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
23141354Seschrock {
23151354Seschrock 	zfs_cmd_t zc = { 0 };
23161354Seschrock 
23171354Seschrock 	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
23182676Seschrock 	(void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
23191354Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
23201544Seschrock 	    &zc.zc_guid) == 0);
23211354Seschrock 
23222082Seschrock 	(void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
23231354Seschrock }
23241354Seschrock 
23251354Seschrock /*
23261354Seschrock  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
23271354Seschrock  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
23281354Seschrock  * We also check if this is a whole disk, in which case we strip off the
23291354Seschrock  * trailing 's0' slice name.
23301354Seschrock  *
23311354Seschrock  * This routine is also responsible for identifying when disks have been
23321354Seschrock  * reconfigured in a new location.  The kernel will have opened the device by
23331354Seschrock  * devid, but the path will still refer to the old location.  To catch this, we
23341354Seschrock  * first do a path -> devid translation (which is fast for the common case).  If
23351354Seschrock  * the devid matches, we're done.  If not, we do a reverse devid -> path
23361354Seschrock  * translation and issue the appropriate ioctl() to update the path of the vdev.
23371354Seschrock  * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
23381354Seschrock  * of these checks.
23391354Seschrock  */
23401354Seschrock char *
23412082Seschrock zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv)
23421354Seschrock {
23431354Seschrock 	char *path, *devid;
23441544Seschrock 	uint64_t value;
23451544Seschrock 	char buf[64];
23464451Seschrock 	vdev_stat_t *vs;
23474451Seschrock 	uint_t vsc;
23481354Seschrock 
23491544Seschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
23501544Seschrock 	    &value) == 0) {
23511544Seschrock 		verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
23521544Seschrock 		    &value) == 0);
23532856Snd150628 		(void) snprintf(buf, sizeof (buf), "%llu",
23542856Snd150628 		    (u_longlong_t)value);
23551544Seschrock 		path = buf;
23561544Seschrock 	} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
23571354Seschrock 
23584451Seschrock 		/*
23594451Seschrock 		 * If the device is dead (faulted, offline, etc) then don't
23604451Seschrock 		 * bother opening it.  Otherwise we may be forcing the user to
23614451Seschrock 		 * open a misbehaving device, which can have undesirable
23624451Seschrock 		 * effects.
23634451Seschrock 		 */
23644451Seschrock 		if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
23654451Seschrock 		    (uint64_t **)&vs, &vsc) != 0 ||
23664451Seschrock 		    vs->vs_state >= VDEV_STATE_DEGRADED) &&
23674451Seschrock 		    zhp != NULL &&
23681354Seschrock 		    nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
23691354Seschrock 			/*
23701354Seschrock 			 * Determine if the current path is correct.
23711354Seschrock 			 */
23721354Seschrock 			char *newdevid = path_to_devid(path);
23731354Seschrock 
23741354Seschrock 			if (newdevid == NULL ||
23751354Seschrock 			    strcmp(devid, newdevid) != 0) {
23761354Seschrock 				char *newpath;
23771354Seschrock 
23781354Seschrock 				if ((newpath = devid_to_path(devid)) != NULL) {
23791354Seschrock 					/*
23801354Seschrock 					 * Update the path appropriately.
23811354Seschrock 					 */
23821354Seschrock 					set_path(zhp, nv, newpath);
23832082Seschrock 					if (nvlist_add_string(nv,
23842082Seschrock 					    ZPOOL_CONFIG_PATH, newpath) == 0)
23852082Seschrock 						verify(nvlist_lookup_string(nv,
23862082Seschrock 						    ZPOOL_CONFIG_PATH,
23872082Seschrock 						    &path) == 0);
23881354Seschrock 					free(newpath);
23891354Seschrock 				}
23901354Seschrock 			}
23911354Seschrock 
23922082Seschrock 			if (newdevid)
23932082Seschrock 				devid_str_free(newdevid);
23941354Seschrock 		}
23951354Seschrock 
23961354Seschrock 		if (strncmp(path, "/dev/dsk/", 9) == 0)
23971354Seschrock 			path += 9;
23981354Seschrock 
23991354Seschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
24001544Seschrock 		    &value) == 0 && value) {
24012082Seschrock 			char *tmp = zfs_strdup(hdl, path);
24022082Seschrock 			if (tmp == NULL)
24032082Seschrock 				return (NULL);
24041354Seschrock 			tmp[strlen(path) - 2] = '\0';
24051354Seschrock 			return (tmp);
24061354Seschrock 		}
24071354Seschrock 	} else {
24081354Seschrock 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
24092082Seschrock 
24102082Seschrock 		/*
24112082Seschrock 		 * If it's a raidz device, we need to stick in the parity level.
24122082Seschrock 		 */
24132082Seschrock 		if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
24142082Seschrock 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
24152082Seschrock 			    &value) == 0);
24162082Seschrock 			(void) snprintf(buf, sizeof (buf), "%s%llu", path,
24172856Snd150628 			    (u_longlong_t)value);
24182082Seschrock 			path = buf;
24192082Seschrock 		}
24201354Seschrock 	}
24211354Seschrock 
24222082Seschrock 	return (zfs_strdup(hdl, path));
24231354Seschrock }
24241544Seschrock 
24251544Seschrock static int
24261544Seschrock zbookmark_compare(const void *a, const void *b)
24271544Seschrock {
24281544Seschrock 	return (memcmp(a, b, sizeof (zbookmark_t)));
24291544Seschrock }
24301544Seschrock 
24311544Seschrock /*
24321544Seschrock  * Retrieve the persistent error log, uniquify the members, and return to the
24331544Seschrock  * caller.
24341544Seschrock  */
24351544Seschrock int
24363444Sek110237 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
24371544Seschrock {
24381544Seschrock 	zfs_cmd_t zc = { 0 };
24391544Seschrock 	uint64_t count;
24402676Seschrock 	zbookmark_t *zb = NULL;
24413444Sek110237 	int i;
24421544Seschrock 
24431544Seschrock 	/*
24441544Seschrock 	 * Retrieve the raw error list from the kernel.  If the number of errors
24451544Seschrock 	 * has increased, allocate more space and continue until we get the
24461544Seschrock 	 * entire list.
24471544Seschrock 	 */
24481544Seschrock 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
24491544Seschrock 	    &count) == 0);
24504820Sek110237 	if (count == 0)
24514820Sek110237 		return (0);
24522676Seschrock 	if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
24532856Snd150628 	    count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
24542082Seschrock 		return (-1);
24552676Seschrock 	zc.zc_nvlist_dst_size = count;
24561544Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
24571544Seschrock 	for (;;) {
24582082Seschrock 		if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
24592082Seschrock 		    &zc) != 0) {
24602676Seschrock 			free((void *)(uintptr_t)zc.zc_nvlist_dst);
24611544Seschrock 			if (errno == ENOMEM) {
24623823Svb160487 				count = zc.zc_nvlist_dst_size;
24632676Seschrock 				if ((zc.zc_nvlist_dst = (uintptr_t)
24643823Svb160487 				    zfs_alloc(zhp->zpool_hdl, count *
24653823Svb160487 				    sizeof (zbookmark_t))) == (uintptr_t)NULL)
24662082Seschrock 					return (-1);
24671544Seschrock 			} else {
24681544Seschrock 				return (-1);
24691544Seschrock 			}
24701544Seschrock 		} else {
24711544Seschrock 			break;
24721544Seschrock 		}
24731544Seschrock 	}
24741544Seschrock 
24751544Seschrock 	/*
24761544Seschrock 	 * Sort the resulting bookmarks.  This is a little confusing due to the
24771544Seschrock 	 * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
24782676Seschrock 	 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
24791544Seschrock 	 * _not_ copied as part of the process.  So we point the start of our
24801544Seschrock 	 * array appropriate and decrement the total number of elements.
24811544Seschrock 	 */
24822676Seschrock 	zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
24832676Seschrock 	    zc.zc_nvlist_dst_size;
24842676Seschrock 	count -= zc.zc_nvlist_dst_size;
24851544Seschrock 
24861544Seschrock 	qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
24871544Seschrock 
24883444Sek110237 	verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
24891544Seschrock 
24901544Seschrock 	/*
24913444Sek110237 	 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
24921544Seschrock 	 */
24931544Seschrock 	for (i = 0; i < count; i++) {
24941544Seschrock 		nvlist_t *nv;
24951544Seschrock 
24963700Sek110237 		/* ignoring zb_blkid and zb_level for now */
24973700Sek110237 		if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
24983700Sek110237 		    zb[i-1].zb_object == zb[i].zb_object)
24991544Seschrock 			continue;
25001544Seschrock 
25013444Sek110237 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
25023444Sek110237 			goto nomem;
25033444Sek110237 		if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
25043444Sek110237 		    zb[i].zb_objset) != 0) {
25053444Sek110237 			nvlist_free(nv);
25062082Seschrock 			goto nomem;
25073444Sek110237 		}
25083444Sek110237 		if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
25093444Sek110237 		    zb[i].zb_object) != 0) {
25103444Sek110237 			nvlist_free(nv);
25113444Sek110237 			goto nomem;
25121544Seschrock 		}
25133444Sek110237 		if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
25143444Sek110237 			nvlist_free(nv);
25153444Sek110237 			goto nomem;
25163444Sek110237 		}
25173444Sek110237 		nvlist_free(nv);
25181544Seschrock 	}
25191544Seschrock 
25203265Sahrens 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
25211544Seschrock 	return (0);
25222082Seschrock 
25232082Seschrock nomem:
25242676Seschrock 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
25252082Seschrock 	return (no_memory(zhp->zpool_hdl));
25261544Seschrock }
25271760Seschrock 
25281760Seschrock /*
25291760Seschrock  * Upgrade a ZFS pool to the latest on-disk version.
25301760Seschrock  */
25311760Seschrock int
25325094Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
25331760Seschrock {
25341760Seschrock 	zfs_cmd_t zc = { 0 };
25352082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
25361760Seschrock 
25371760Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
25385094Slling 	zc.zc_cookie = new_version;
25395094Slling 
25404543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
25413237Slling 		return (zpool_standard_error_fmt(hdl, errno,
25422082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
25432082Seschrock 		    zhp->zpool_name));
25441760Seschrock 	return (0);
25451760Seschrock }
25462926Sek110237 
25474988Sek110237 void
25484988Sek110237 zpool_set_history_str(const char *subcommand, int argc, char **argv,
25494988Sek110237     char *history_str)
25504988Sek110237 {
25514988Sek110237 	int i;
25524988Sek110237 
25534988Sek110237 	(void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
25544988Sek110237 	for (i = 1; i < argc; i++) {
25554988Sek110237 		if (strlen(history_str) + 1 + strlen(argv[i]) >
25564988Sek110237 		    HIS_MAX_RECORD_LEN)
25574988Sek110237 			break;
25584988Sek110237 		(void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
25594988Sek110237 		(void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
25604988Sek110237 	}
25614988Sek110237 }
25624988Sek110237 
25632926Sek110237 /*
25644988Sek110237  * Stage command history for logging.
25652926Sek110237  */
25664988Sek110237 int
25674988Sek110237 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
25682926Sek110237 {
25694988Sek110237 	if (history_str == NULL)
25704988Sek110237 		return (EINVAL);
25714988Sek110237 
25724988Sek110237 	if (strlen(history_str) > HIS_MAX_RECORD_LEN)
25734988Sek110237 		return (EINVAL);
25742926Sek110237 
25754715Sek110237 	if (hdl->libzfs_log_str != NULL)
25764543Smarks 		free(hdl->libzfs_log_str);
25772926Sek110237 
25784988Sek110237 	if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
25794988Sek110237 		return (no_memory(hdl));
25804543Smarks 
25814988Sek110237 	return (0);
25822926Sek110237 }
25832926Sek110237 
25842926Sek110237 /*
25852926Sek110237  * Perform ioctl to get some command history of a pool.
25862926Sek110237  *
25872926Sek110237  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
25882926Sek110237  * logical offset of the history buffer to start reading from.
25892926Sek110237  *
25902926Sek110237  * Upon return, 'off' is the next logical offset to read from and
25912926Sek110237  * 'len' is the actual amount of bytes read into 'buf'.
25922926Sek110237  */
25932926Sek110237 static int
25942926Sek110237 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
25952926Sek110237 {
25962926Sek110237 	zfs_cmd_t zc = { 0 };
25972926Sek110237 	libzfs_handle_t *hdl = zhp->zpool_hdl;
25982926Sek110237 
25992926Sek110237 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
26002926Sek110237 
26012926Sek110237 	zc.zc_history = (uint64_t)(uintptr_t)buf;
26022926Sek110237 	zc.zc_history_len = *len;
26032926Sek110237 	zc.zc_history_offset = *off;
26042926Sek110237 
26052926Sek110237 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
26062926Sek110237 		switch (errno) {
26072926Sek110237 		case EPERM:
26083237Slling 			return (zfs_error_fmt(hdl, EZFS_PERM,
26093237Slling 			    dgettext(TEXT_DOMAIN,
26102926Sek110237 			    "cannot show history for pool '%s'"),
26112926Sek110237 			    zhp->zpool_name));
26122926Sek110237 		case ENOENT:
26133237Slling 			return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
26142926Sek110237 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
26152926Sek110237 			    "'%s'"), zhp->zpool_name));
26163863Sek110237 		case ENOTSUP:
26173863Sek110237 			return (zfs_error_fmt(hdl, EZFS_BADVERSION,
26183863Sek110237 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
26193863Sek110237 			    "'%s', pool must be upgraded"), zhp->zpool_name));
26202926Sek110237 		default:
26213237Slling 			return (zpool_standard_error_fmt(hdl, errno,
26222926Sek110237 			    dgettext(TEXT_DOMAIN,
26232926Sek110237 			    "cannot get history for '%s'"), zhp->zpool_name));
26242926Sek110237 		}
26252926Sek110237 	}
26262926Sek110237 
26272926Sek110237 	*len = zc.zc_history_len;
26282926Sek110237 	*off = zc.zc_history_offset;
26292926Sek110237 
26302926Sek110237 	return (0);
26312926Sek110237 }
26322926Sek110237 
26332926Sek110237 /*
26342926Sek110237  * Process the buffer of nvlists, unpacking and storing each nvlist record
26352926Sek110237  * into 'records'.  'leftover' is set to the number of bytes that weren't
26362926Sek110237  * processed as there wasn't a complete record.
26372926Sek110237  */
26382926Sek110237 static int
26392926Sek110237 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
26402926Sek110237     nvlist_t ***records, uint_t *numrecords)
26412926Sek110237 {
26422926Sek110237 	uint64_t reclen;
26432926Sek110237 	nvlist_t *nv;
26442926Sek110237 	int i;
26452926Sek110237 
26462926Sek110237 	while (bytes_read > sizeof (reclen)) {
26472926Sek110237 
26482926Sek110237 		/* get length of packed record (stored as little endian) */
26492926Sek110237 		for (i = 0, reclen = 0; i < sizeof (reclen); i++)
26502926Sek110237 			reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
26512926Sek110237 
26522926Sek110237 		if (bytes_read < sizeof (reclen) + reclen)
26532926Sek110237 			break;
26542926Sek110237 
26552926Sek110237 		/* unpack record */
26562926Sek110237 		if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
26572926Sek110237 			return (ENOMEM);
26582926Sek110237 		bytes_read -= sizeof (reclen) + reclen;
26592926Sek110237 		buf += sizeof (reclen) + reclen;
26602926Sek110237 
26612926Sek110237 		/* add record to nvlist array */
26622926Sek110237 		(*numrecords)++;
26632926Sek110237 		if (ISP2(*numrecords + 1)) {
26642926Sek110237 			*records = realloc(*records,
26652926Sek110237 			    *numrecords * 2 * sizeof (nvlist_t *));
26662926Sek110237 		}
26672926Sek110237 		(*records)[*numrecords - 1] = nv;
26682926Sek110237 	}
26692926Sek110237 
26702926Sek110237 	*leftover = bytes_read;
26712926Sek110237 	return (0);
26722926Sek110237 }
26732926Sek110237 
26742926Sek110237 #define	HIS_BUF_LEN	(128*1024)
26752926Sek110237 
26762926Sek110237 /*
26772926Sek110237  * Retrieve the command history of a pool.
26782926Sek110237  */
26792926Sek110237 int
26802926Sek110237 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
26812926Sek110237 {
26822926Sek110237 	char buf[HIS_BUF_LEN];
26832926Sek110237 	uint64_t off = 0;
26842926Sek110237 	nvlist_t **records = NULL;
26852926Sek110237 	uint_t numrecords = 0;
26862926Sek110237 	int err, i;
26872926Sek110237 
26882926Sek110237 	do {
26892926Sek110237 		uint64_t bytes_read = sizeof (buf);
26902926Sek110237 		uint64_t leftover;
26912926Sek110237 
26922926Sek110237 		if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
26932926Sek110237 			break;
26942926Sek110237 
26952926Sek110237 		/* if nothing else was read in, we're at EOF, just return */
26962926Sek110237 		if (!bytes_read)
26972926Sek110237 			break;
26982926Sek110237 
26992926Sek110237 		if ((err = zpool_history_unpack(buf, bytes_read,
27002926Sek110237 		    &leftover, &records, &numrecords)) != 0)
27012926Sek110237 			break;
27022926Sek110237 		off -= leftover;
27032926Sek110237 
27042926Sek110237 		/* CONSTCOND */
27052926Sek110237 	} while (1);
27062926Sek110237 
27072926Sek110237 	if (!err) {
27082926Sek110237 		verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
27092926Sek110237 		verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
27102926Sek110237 		    records, numrecords) == 0);
27112926Sek110237 	}
27122926Sek110237 	for (i = 0; i < numrecords; i++)
27132926Sek110237 		nvlist_free(records[i]);
27142926Sek110237 	free(records);
27152926Sek110237 
27162926Sek110237 	return (err);
27172926Sek110237 }
27183444Sek110237 
27193444Sek110237 void
27203444Sek110237 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
27213444Sek110237     char *pathname, size_t len)
27223444Sek110237 {
27233444Sek110237 	zfs_cmd_t zc = { 0 };
27243444Sek110237 	boolean_t mounted = B_FALSE;
27253444Sek110237 	char *mntpnt = NULL;
27263444Sek110237 	char dsname[MAXNAMELEN];
27273444Sek110237 
27283444Sek110237 	if (dsobj == 0) {
27293444Sek110237 		/* special case for the MOS */
27303444Sek110237 		(void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
27313444Sek110237 		return;
27323444Sek110237 	}
27333444Sek110237 
27343444Sek110237 	/* get the dataset's name */
27353444Sek110237 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
27363444Sek110237 	zc.zc_obj = dsobj;
27373444Sek110237 	if (ioctl(zhp->zpool_hdl->libzfs_fd,
27383444Sek110237 	    ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
27393444Sek110237 		/* just write out a path of two object numbers */
27403444Sek110237 		(void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
27413444Sek110237 		    dsobj, obj);
27423444Sek110237 		return;
27433444Sek110237 	}
27443444Sek110237 	(void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
27453444Sek110237 
27463444Sek110237 	/* find out if the dataset is mounted */
27473444Sek110237 	mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
27483444Sek110237 
27493444Sek110237 	/* get the corrupted object's path */
27503444Sek110237 	(void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
27513444Sek110237 	zc.zc_obj = obj;
27523444Sek110237 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
27533444Sek110237 	    &zc) == 0) {
27543444Sek110237 		if (mounted) {
27553444Sek110237 			(void) snprintf(pathname, len, "%s%s", mntpnt,
27563444Sek110237 			    zc.zc_value);
27573444Sek110237 		} else {
27583444Sek110237 			(void) snprintf(pathname, len, "%s:%s",
27593444Sek110237 			    dsname, zc.zc_value);
27603444Sek110237 		}
27613444Sek110237 	} else {
27623444Sek110237 		(void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
27633444Sek110237 	}
27643444Sek110237 	free(mntpnt);
27653444Sek110237 }
27663912Slling 
27674276Staylor #define	RDISK_ROOT	"/dev/rdsk"
27684276Staylor #define	BACKUP_SLICE	"s2"
27694276Staylor /*
27704276Staylor  * Don't start the slice at the default block of 34; many storage
27714276Staylor  * devices will use a stripe width of 128k, so start there instead.
27724276Staylor  */
27734276Staylor #define	NEW_START_BLOCK	256
27744276Staylor 
27754276Staylor /*
27767042Sgw25295  * Read the EFI label from the config, if a label does not exist then
27777042Sgw25295  * pass back the error to the caller. If the caller has passed a non-NULL
27787042Sgw25295  * diskaddr argument then we set it to the starting address of the EFI
27797042Sgw25295  * partition.
27807042Sgw25295  */
27817042Sgw25295 static int
27827042Sgw25295 read_efi_label(nvlist_t *config, diskaddr_t *sb)
27837042Sgw25295 {
27847042Sgw25295 	char *path;
27857042Sgw25295 	int fd;
27867042Sgw25295 	char diskname[MAXPATHLEN];
27877042Sgw25295 	int err = -1;
27887042Sgw25295 
27897042Sgw25295 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
27907042Sgw25295 		return (err);
27917042Sgw25295 
27927042Sgw25295 	(void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
27937042Sgw25295 	    strrchr(path, '/'));
27947042Sgw25295 	if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
27957042Sgw25295 		struct dk_gpt *vtoc;
27967042Sgw25295 
27977042Sgw25295 		if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
27987042Sgw25295 			if (sb != NULL)
27997042Sgw25295 				*sb = vtoc->efi_parts[0].p_start;
28007042Sgw25295 			efi_free(vtoc);
28017042Sgw25295 		}
28027042Sgw25295 		(void) close(fd);
28037042Sgw25295 	}
28047042Sgw25295 	return (err);
28057042Sgw25295 }
28067042Sgw25295 
28077042Sgw25295 /*
28084276Staylor  * determine where a partition starts on a disk in the current
28094276Staylor  * configuration
28104276Staylor  */
28114276Staylor static diskaddr_t
28124276Staylor find_start_block(nvlist_t *config)
28134276Staylor {
28144276Staylor 	nvlist_t **child;
28154276Staylor 	uint_t c, children;
28164276Staylor 	diskaddr_t sb = MAXOFFSET_T;
28174276Staylor 	uint64_t wholedisk;
28184276Staylor 
28194276Staylor 	if (nvlist_lookup_nvlist_array(config,
28204276Staylor 	    ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
28214276Staylor 		if (nvlist_lookup_uint64(config,
28224276Staylor 		    ZPOOL_CONFIG_WHOLE_DISK,
28234276Staylor 		    &wholedisk) != 0 || !wholedisk) {
28244276Staylor 			return (MAXOFFSET_T);
28254276Staylor 		}
28267042Sgw25295 		if (read_efi_label(config, &sb) < 0)
28277042Sgw25295 			sb = MAXOFFSET_T;
28284276Staylor 		return (sb);
28294276Staylor 	}
28304276Staylor 
28314276Staylor 	for (c = 0; c < children; c++) {
28324276Staylor 		sb = find_start_block(child[c]);
28334276Staylor 		if (sb != MAXOFFSET_T) {
28344276Staylor 			return (sb);
28354276Staylor 		}
28364276Staylor 	}
28374276Staylor 	return (MAXOFFSET_T);
28384276Staylor }
28394276Staylor 
28404276Staylor /*
28414276Staylor  * Label an individual disk.  The name provided is the short name,
28424276Staylor  * stripped of any leading /dev path.
28434276Staylor  */
28444276Staylor int
28454276Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
28464276Staylor {
28474276Staylor 	char path[MAXPATHLEN];
28484276Staylor 	struct dk_gpt *vtoc;
28494276Staylor 	int fd;
28504276Staylor 	size_t resv = EFI_MIN_RESV_SIZE;
28514276Staylor 	uint64_t slice_size;
28524276Staylor 	diskaddr_t start_block;
28534276Staylor 	char errbuf[1024];
28544276Staylor 
28556289Smmusante 	/* prepare an error message just in case */
28566289Smmusante 	(void) snprintf(errbuf, sizeof (errbuf),
28576289Smmusante 	    dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
28586289Smmusante 
28594276Staylor 	if (zhp) {
28604276Staylor 		nvlist_t *nvroot;
28614276Staylor 
2862*7965SGeorge.Wilson@Sun.COM 		if (pool_is_bootable(zhp)) {
2863*7965SGeorge.Wilson@Sun.COM 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2864*7965SGeorge.Wilson@Sun.COM 			    "EFI labeled devices are not supported on root "
2865*7965SGeorge.Wilson@Sun.COM 			    "pools."));
2866*7965SGeorge.Wilson@Sun.COM 			return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
2867*7965SGeorge.Wilson@Sun.COM 		}
2868*7965SGeorge.Wilson@Sun.COM 
28694276Staylor 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
28704276Staylor 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
28714276Staylor 
28724276Staylor 		if (zhp->zpool_start_block == 0)
28734276Staylor 			start_block = find_start_block(nvroot);
28744276Staylor 		else
28754276Staylor 			start_block = zhp->zpool_start_block;
28764276Staylor 		zhp->zpool_start_block = start_block;
28774276Staylor 	} else {
28784276Staylor 		/* new pool */
28794276Staylor 		start_block = NEW_START_BLOCK;
28804276Staylor 	}
28814276Staylor 
28824276Staylor 	(void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
28834276Staylor 	    BACKUP_SLICE);
28844276Staylor 
28854276Staylor 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
28864276Staylor 		/*
28874276Staylor 		 * This shouldn't happen.  We've long since verified that this
28884276Staylor 		 * is a valid device.
28894276Staylor 		 */
28906289Smmusante 		zfs_error_aux(hdl,
28916289Smmusante 		    dgettext(TEXT_DOMAIN, "unable to open device"));
28924276Staylor 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
28934276Staylor 	}
28944276Staylor 
28954276Staylor 	if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
28964276Staylor 		/*
28974276Staylor 		 * The only way this can fail is if we run out of memory, or we
28984276Staylor 		 * were unable to read the disk's capacity
28994276Staylor 		 */
29004276Staylor 		if (errno == ENOMEM)
29014276Staylor 			(void) no_memory(hdl);
29024276Staylor 
29034276Staylor 		(void) close(fd);
29046289Smmusante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29056289Smmusante 		    "unable to read disk capacity"), name);
29064276Staylor 
29074276Staylor 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
29084276Staylor 	}
29094276Staylor 
29104276Staylor 	slice_size = vtoc->efi_last_u_lba + 1;
29114276Staylor 	slice_size -= EFI_MIN_RESV_SIZE;
29124276Staylor 	if (start_block == MAXOFFSET_T)
29134276Staylor 		start_block = NEW_START_BLOCK;
29144276Staylor 	slice_size -= start_block;
29154276Staylor 
29164276Staylor 	vtoc->efi_parts[0].p_start = start_block;
29174276Staylor 	vtoc->efi_parts[0].p_size = slice_size;
29184276Staylor 
29194276Staylor 	/*
29204276Staylor 	 * Why we use V_USR: V_BACKUP confuses users, and is considered
29214276Staylor 	 * disposable by some EFI utilities (since EFI doesn't have a backup
29224276Staylor 	 * slice).  V_UNASSIGNED is supposed to be used only for zero size
29234276Staylor 	 * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
29244276Staylor 	 * etc. were all pretty specific.  V_USR is as close to reality as we
29254276Staylor 	 * can get, in the absence of V_OTHER.
29264276Staylor 	 */
29274276Staylor 	vtoc->efi_parts[0].p_tag = V_USR;
29284276Staylor 	(void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
29294276Staylor 
29304276Staylor 	vtoc->efi_parts[8].p_start = slice_size + start_block;
29314276Staylor 	vtoc->efi_parts[8].p_size = resv;
29324276Staylor 	vtoc->efi_parts[8].p_tag = V_RESERVED;
29334276Staylor 
29344276Staylor 	if (efi_write(fd, vtoc) != 0) {
29354276Staylor 		/*
29364276Staylor 		 * Some block drivers (like pcata) may not support EFI
29374276Staylor 		 * GPT labels.  Print out a helpful error message dir-
29384276Staylor 		 * ecting the user to manually label the disk and give
29394276Staylor 		 * a specific slice.
29404276Staylor 		 */
29414276Staylor 		(void) close(fd);
29424276Staylor 		efi_free(vtoc);
29434276Staylor 
29444276Staylor 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29456289Smmusante 		    "try using fdisk(1M) and then provide a specific slice"));
29464276Staylor 		return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
29474276Staylor 	}
29484276Staylor 
29494276Staylor 	(void) close(fd);
29504276Staylor 	efi_free(vtoc);
29514276Staylor 	return (0);
29524276Staylor }
29536423Sgw25295 
29546423Sgw25295 static boolean_t
29556423Sgw25295 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
29566423Sgw25295 {
29576423Sgw25295 	char *type;
29586423Sgw25295 	nvlist_t **child;
29596423Sgw25295 	uint_t children, c;
29606423Sgw25295 
29616423Sgw25295 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
29626423Sgw25295 	if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
29636423Sgw25295 	    strcmp(type, VDEV_TYPE_FILE) == 0 ||
29646423Sgw25295 	    strcmp(type, VDEV_TYPE_LOG) == 0 ||
29656423Sgw25295 	    strcmp(type, VDEV_TYPE_MISSING) == 0) {
29666423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29676423Sgw25295 		    "vdev type '%s' is not supported"), type);
29686423Sgw25295 		(void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
29696423Sgw25295 		return (B_FALSE);
29706423Sgw25295 	}
29716423Sgw25295 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
29726423Sgw25295 	    &child, &children) == 0) {
29736423Sgw25295 		for (c = 0; c < children; c++) {
29746423Sgw25295 			if (!supported_dump_vdev_type(hdl, child[c], errbuf))
29756423Sgw25295 				return (B_FALSE);
29766423Sgw25295 		}
29776423Sgw25295 	}
29786423Sgw25295 	return (B_TRUE);
29796423Sgw25295 }
29806423Sgw25295 
29816423Sgw25295 /*
29826423Sgw25295  * check if this zvol is allowable for use as a dump device; zero if
29836423Sgw25295  * it is, > 0 if it isn't, < 0 if it isn't a zvol
29846423Sgw25295  */
29856423Sgw25295 int
29866423Sgw25295 zvol_check_dump_config(char *arg)
29876423Sgw25295 {
29886423Sgw25295 	zpool_handle_t *zhp = NULL;
29896423Sgw25295 	nvlist_t *config, *nvroot;
29906423Sgw25295 	char *p, *volname;
29916423Sgw25295 	nvlist_t **top;
29926423Sgw25295 	uint_t toplevels;
29936423Sgw25295 	libzfs_handle_t *hdl;
29946423Sgw25295 	char errbuf[1024];
29956423Sgw25295 	char poolname[ZPOOL_MAXNAMELEN];
29966423Sgw25295 	int pathlen = strlen(ZVOL_FULL_DEV_DIR);
29976423Sgw25295 	int ret = 1;
29986423Sgw25295 
29996423Sgw25295 	if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
30006423Sgw25295 		return (-1);
30016423Sgw25295 	}
30026423Sgw25295 
30036423Sgw25295 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
30046423Sgw25295 	    "dump is not supported on device '%s'"), arg);
30056423Sgw25295 
30066423Sgw25295 	if ((hdl = libzfs_init()) == NULL)
30076423Sgw25295 		return (1);
30086423Sgw25295 	libzfs_print_on_error(hdl, B_TRUE);
30096423Sgw25295 
30106423Sgw25295 	volname = arg + pathlen;
30116423Sgw25295 
30126423Sgw25295 	/* check the configuration of the pool */
30136423Sgw25295 	if ((p = strchr(volname, '/')) == NULL) {
30146423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30156423Sgw25295 		    "malformed dataset name"));
30166423Sgw25295 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
30176423Sgw25295 		return (1);
30186423Sgw25295 	} else if (p - volname >= ZFS_MAXNAMELEN) {
30196423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30206423Sgw25295 		    "dataset name is too long"));
30216423Sgw25295 		(void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
30226423Sgw25295 		return (1);
30236423Sgw25295 	} else {
30246423Sgw25295 		(void) strncpy(poolname, volname, p - volname);
30256423Sgw25295 		poolname[p - volname] = '\0';
30266423Sgw25295 	}
30276423Sgw25295 
30286423Sgw25295 	if ((zhp = zpool_open(hdl, poolname)) == NULL) {
30296423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30306423Sgw25295 		    "could not open pool '%s'"), poolname);
30316423Sgw25295 		(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
30326423Sgw25295 		goto out;
30336423Sgw25295 	}
30346423Sgw25295 	config = zpool_get_config(zhp, NULL);
30356423Sgw25295 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
30366423Sgw25295 	    &nvroot) != 0) {
30376423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30386423Sgw25295 		    "could not obtain vdev configuration for  '%s'"), poolname);
30396423Sgw25295 		(void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
30406423Sgw25295 		goto out;
30416423Sgw25295 	}
30426423Sgw25295 
30436423Sgw25295 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
30446423Sgw25295 	    &top, &toplevels) == 0);
30456423Sgw25295 	if (toplevels != 1) {
30466423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30476423Sgw25295 		    "'%s' has multiple top level vdevs"), poolname);
30486423Sgw25295 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
30496423Sgw25295 		goto out;
30506423Sgw25295 	}
30516423Sgw25295 
30526423Sgw25295 	if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
30536423Sgw25295 		goto out;
30546423Sgw25295 	}
30556423Sgw25295 	ret = 0;
30566423Sgw25295 
30576423Sgw25295 out:
30586423Sgw25295 	if (zhp)
30596423Sgw25295 		zpool_close(zhp);
30606423Sgw25295 	libzfs_fini(hdl);
30616423Sgw25295 	return (ret);
30626423Sgw25295 }
3063