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 /*
238525SEric.Schrock@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24789Sahrens  * Use is subject to license terms.
25789Sahrens  */
26789Sahrens 
27789Sahrens #include <ctype.h>
28789Sahrens #include <errno.h>
29789Sahrens #include <devid.h>
30789Sahrens #include <fcntl.h>
31789Sahrens #include <libintl.h>
32789Sahrens #include <stdio.h>
33789Sahrens #include <stdlib.h>
343126Sahl #include <strings.h>
35789Sahrens #include <unistd.h>
364276Staylor #include <sys/efi_partition.h>
374276Staylor #include <sys/vtoc.h>
38789Sahrens #include <sys/zfs_ioctl.h>
399816SGeorge.Wilson@Sun.COM #include <dlfcn.h>
40789Sahrens 
41789Sahrens #include "zfs_namecheck.h"
423912Slling #include "zfs_prop.h"
43789Sahrens #include "libzfs_impl.h"
44789Sahrens 
4510685SGeorge.Wilson@Sun.COM const char *hist_event_table[LOG_END] = {
4610685SGeorge.Wilson@Sun.COM 	"invalid event",
4710685SGeorge.Wilson@Sun.COM 	"pool create",
4810685SGeorge.Wilson@Sun.COM 	"vdev add",
4910685SGeorge.Wilson@Sun.COM 	"pool remove",
5010685SGeorge.Wilson@Sun.COM 	"pool destroy",
5110685SGeorge.Wilson@Sun.COM 	"pool export",
5210685SGeorge.Wilson@Sun.COM 	"pool import",
5310685SGeorge.Wilson@Sun.COM 	"vdev attach",
5410685SGeorge.Wilson@Sun.COM 	"vdev replace",
5510685SGeorge.Wilson@Sun.COM 	"vdev detach",
5610685SGeorge.Wilson@Sun.COM 	"vdev online",
5710685SGeorge.Wilson@Sun.COM 	"vdev offline",
5810685SGeorge.Wilson@Sun.COM 	"vdev upgrade",
5910685SGeorge.Wilson@Sun.COM 	"pool clear",
6010685SGeorge.Wilson@Sun.COM 	"pool scrub",
6110685SGeorge.Wilson@Sun.COM 	"pool property set",
6210685SGeorge.Wilson@Sun.COM 	"create",
6310685SGeorge.Wilson@Sun.COM 	"clone",
6410685SGeorge.Wilson@Sun.COM 	"destroy",
6510685SGeorge.Wilson@Sun.COM 	"destroy_begin_sync",
6610685SGeorge.Wilson@Sun.COM 	"inherit",
6710685SGeorge.Wilson@Sun.COM 	"property set",
6810685SGeorge.Wilson@Sun.COM 	"quota set",
6910685SGeorge.Wilson@Sun.COM 	"permission update",
7010685SGeorge.Wilson@Sun.COM 	"permission remove",
7110685SGeorge.Wilson@Sun.COM 	"permission who remove",
7210685SGeorge.Wilson@Sun.COM 	"promote",
7310685SGeorge.Wilson@Sun.COM 	"receive",
7410685SGeorge.Wilson@Sun.COM 	"rename",
7510685SGeorge.Wilson@Sun.COM 	"reservation set",
7610685SGeorge.Wilson@Sun.COM 	"replay_inc_sync",
7710685SGeorge.Wilson@Sun.COM 	"replay_full_sync",
7810685SGeorge.Wilson@Sun.COM 	"rollback",
7910685SGeorge.Wilson@Sun.COM 	"snapshot",
8010685SGeorge.Wilson@Sun.COM 	"filesystem version upgrade",
8110685SGeorge.Wilson@Sun.COM 	"refquota set",
8210685SGeorge.Wilson@Sun.COM 	"refreservation set",
8310685SGeorge.Wilson@Sun.COM 	"pool scrub done",
8410685SGeorge.Wilson@Sun.COM 	"user hold",
8510685SGeorge.Wilson@Sun.COM 	"user release",
8610685SGeorge.Wilson@Sun.COM };
8710685SGeorge.Wilson@Sun.COM 
887042Sgw25295 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
895094Slling 
907965SGeorge.Wilson@Sun.COM #if defined(__i386) || defined(__amd64)
917965SGeorge.Wilson@Sun.COM #define	BOOTCMD	"installgrub(1M)"
927965SGeorge.Wilson@Sun.COM #else
937965SGeorge.Wilson@Sun.COM #define	BOOTCMD	"installboot(1M)"
947965SGeorge.Wilson@Sun.COM #endif
957965SGeorge.Wilson@Sun.COM 
969816SGeorge.Wilson@Sun.COM #define	DISK_ROOT	"/dev/dsk"
979816SGeorge.Wilson@Sun.COM #define	RDISK_ROOT	"/dev/rdsk"
989816SGeorge.Wilson@Sun.COM #define	BACKUP_SLICE	"s2"
999816SGeorge.Wilson@Sun.COM 
1005094Slling /*
1015094Slling  * ====================================================================
1025094Slling  *   zpool property functions
1035094Slling  * ====================================================================
1045094Slling  */
1055094Slling 
1065094Slling static int
1075094Slling zpool_get_all_props(zpool_handle_t *zhp)
1085094Slling {
1095094Slling 	zfs_cmd_t zc = { 0 };
1105094Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1115094Slling 
1125094Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1135094Slling 
1145094Slling 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
1155094Slling 		return (-1);
1165094Slling 
1175094Slling 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
1185094Slling 		if (errno == ENOMEM) {
1195094Slling 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1205094Slling 				zcmd_free_nvlists(&zc);
1215094Slling 				return (-1);
1225094Slling 			}
1235094Slling 		} else {
1245094Slling 			zcmd_free_nvlists(&zc);
1255094Slling 			return (-1);
1265094Slling 		}
1275094Slling 	}
1285094Slling 
1295094Slling 	if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
1305094Slling 		zcmd_free_nvlists(&zc);
1315094Slling 		return (-1);
1325094Slling 	}
1335094Slling 
1345094Slling 	zcmd_free_nvlists(&zc);
1355094Slling 
1365094Slling 	return (0);
1375094Slling }
1385094Slling 
1395094Slling static int
1405094Slling zpool_props_refresh(zpool_handle_t *zhp)
1415094Slling {
1425094Slling 	nvlist_t *old_props;
1435094Slling 
1445094Slling 	old_props = zhp->zpool_props;
1455094Slling 
1465094Slling 	if (zpool_get_all_props(zhp) != 0)
1475094Slling 		return (-1);
1485094Slling 
1495094Slling 	nvlist_free(old_props);
1505094Slling 	return (0);
1515094Slling }
1525094Slling 
1535094Slling static char *
1545094Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
1555094Slling     zprop_source_t *src)
1565094Slling {
1575094Slling 	nvlist_t *nv, *nvl;
1585094Slling 	uint64_t ival;
1595094Slling 	char *value;
1605094Slling 	zprop_source_t source;
1615094Slling 
1625094Slling 	nvl = zhp->zpool_props;
1635094Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
1645094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
1655094Slling 		source = ival;
1665094Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1675094Slling 	} else {
1685094Slling 		source = ZPROP_SRC_DEFAULT;
1695094Slling 		if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
1705094Slling 			value = "-";
1715094Slling 	}
1725094Slling 
1735094Slling 	if (src)
1745094Slling 		*src = source;
1755094Slling 
1765094Slling 	return (value);
1775094Slling }
1785094Slling 
1795094Slling uint64_t
1805094Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
1815094Slling {
1825094Slling 	nvlist_t *nv, *nvl;
1835094Slling 	uint64_t value;
1845094Slling 	zprop_source_t source;
1855094Slling 
1867294Sperrin 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
1877294Sperrin 		/*
1887294Sperrin 		 * zpool_get_all_props() has most likely failed because
1897294Sperrin 		 * the pool is faulted, but if all we need is the top level
1907294Sperrin 		 * vdev's guid then get it from the zhp config nvlist.
1917294Sperrin 		 */
1927294Sperrin 		if ((prop == ZPOOL_PROP_GUID) &&
1937294Sperrin 		    (nvlist_lookup_nvlist(zhp->zpool_config,
1947294Sperrin 		    ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
1957294Sperrin 		    (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
1967294Sperrin 		    == 0)) {
1977294Sperrin 			return (value);
1987294Sperrin 		}
1995094Slling 		return (zpool_prop_default_numeric(prop));
2007294Sperrin 	}
2015094Slling 
2025094Slling 	nvl = zhp->zpool_props;
2035094Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
2045094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
2055094Slling 		source = value;
2065094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
2075094Slling 	} else {
2085094Slling 		source = ZPROP_SRC_DEFAULT;
2095094Slling 		value = zpool_prop_default_numeric(prop);
2105094Slling 	}
2115094Slling 
2125094Slling 	if (src)
2135094Slling 		*src = source;
2145094Slling 
2155094Slling 	return (value);
2165094Slling }
2175094Slling 
2185094Slling /*
2195094Slling  * Map VDEV STATE to printed strings.
2205094Slling  */
2215094Slling char *
2225094Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
2235094Slling {
2245094Slling 	switch (state) {
2255094Slling 	case VDEV_STATE_CLOSED:
2265094Slling 	case VDEV_STATE_OFFLINE:
2275094Slling 		return (gettext("OFFLINE"));
2285094Slling 	case VDEV_STATE_REMOVED:
2295094Slling 		return (gettext("REMOVED"));
2305094Slling 	case VDEV_STATE_CANT_OPEN:
2317294Sperrin 		if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
2325094Slling 			return (gettext("FAULTED"));
2335094Slling 		else
2345094Slling 			return (gettext("UNAVAIL"));
2355094Slling 	case VDEV_STATE_FAULTED:
2365094Slling 		return (gettext("FAULTED"));
2375094Slling 	case VDEV_STATE_DEGRADED:
2385094Slling 		return (gettext("DEGRADED"));
2395094Slling 	case VDEV_STATE_HEALTHY:
2405094Slling 		return (gettext("ONLINE"));
2415094Slling 	}
2425094Slling 
2435094Slling 	return (gettext("UNKNOWN"));
2445094Slling }
2455094Slling 
2465094Slling /*
2475094Slling  * Get a zpool property value for 'prop' and return the value in
2485094Slling  * a pre-allocated buffer.
2495094Slling  */
2505094Slling int
2515094Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
2525094Slling     zprop_source_t *srctype)
2535094Slling {
2545094Slling 	uint64_t intval;
2555094Slling 	const char *strval;
2565094Slling 	zprop_source_t src = ZPROP_SRC_NONE;
2575094Slling 	nvlist_t *nvroot;
2585094Slling 	vdev_stat_t *vs;
2595094Slling 	uint_t vsc;
2605094Slling 
2615094Slling 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
2628525SEric.Schrock@Sun.COM 		switch (prop) {
2638525SEric.Schrock@Sun.COM 		case ZPOOL_PROP_NAME:
2645094Slling 			(void) strlcpy(buf, zpool_get_name(zhp), len);
2658525SEric.Schrock@Sun.COM 			break;
2668525SEric.Schrock@Sun.COM 
2678525SEric.Schrock@Sun.COM 		case ZPOOL_PROP_HEALTH:
2685094Slling 			(void) strlcpy(buf, "FAULTED", len);
2698525SEric.Schrock@Sun.COM 			break;
2708525SEric.Schrock@Sun.COM 
2718525SEric.Schrock@Sun.COM 		case ZPOOL_PROP_GUID:
2728525SEric.Schrock@Sun.COM 			intval = zpool_get_prop_int(zhp, prop, &src);
2738525SEric.Schrock@Sun.COM 			(void) snprintf(buf, len, "%llu", intval);
2748525SEric.Schrock@Sun.COM 			break;
2758525SEric.Schrock@Sun.COM 
2768525SEric.Schrock@Sun.COM 		case ZPOOL_PROP_ALTROOT:
2778525SEric.Schrock@Sun.COM 		case ZPOOL_PROP_CACHEFILE:
2788525SEric.Schrock@Sun.COM 			if (zhp->zpool_props != NULL ||
2798525SEric.Schrock@Sun.COM 			    zpool_get_all_props(zhp) == 0) {
2808525SEric.Schrock@Sun.COM 				(void) strlcpy(buf,
2818525SEric.Schrock@Sun.COM 				    zpool_get_prop_string(zhp, prop, &src),
2828525SEric.Schrock@Sun.COM 				    len);
2838525SEric.Schrock@Sun.COM 				if (srctype != NULL)
2848525SEric.Schrock@Sun.COM 					*srctype = src;
2858525SEric.Schrock@Sun.COM 				return (0);
2868525SEric.Schrock@Sun.COM 			}
2878525SEric.Schrock@Sun.COM 			/* FALLTHROUGH */
2888525SEric.Schrock@Sun.COM 		default:
2895094Slling 			(void) strlcpy(buf, "-", len);
2908525SEric.Schrock@Sun.COM 			break;
2918525SEric.Schrock@Sun.COM 		}
2928525SEric.Schrock@Sun.COM 
2938525SEric.Schrock@Sun.COM 		if (srctype != NULL)
2948525SEric.Schrock@Sun.COM 			*srctype = src;
2955094Slling 		return (0);
2965094Slling 	}
2975094Slling 
2985094Slling 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
2995094Slling 	    prop != ZPOOL_PROP_NAME)
3005094Slling 		return (-1);
3015094Slling 
3025094Slling 	switch (zpool_prop_get_type(prop)) {
3035094Slling 	case PROP_TYPE_STRING:
3045094Slling 		(void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
3055094Slling 		    len);
3065094Slling 		break;
3075094Slling 
3085094Slling 	case PROP_TYPE_NUMBER:
3095094Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
3105094Slling 
3115094Slling 		switch (prop) {
3125094Slling 		case ZPOOL_PROP_SIZE:
3135094Slling 		case ZPOOL_PROP_USED:
3145094Slling 		case ZPOOL_PROP_AVAILABLE:
3155094Slling 			(void) zfs_nicenum(intval, buf, len);
3165094Slling 			break;
3175094Slling 
3185094Slling 		case ZPOOL_PROP_CAPACITY:
3195094Slling 			(void) snprintf(buf, len, "%llu%%",
3205094Slling 			    (u_longlong_t)intval);
3215094Slling 			break;
3225094Slling 
3235094Slling 		case ZPOOL_PROP_HEALTH:
3245094Slling 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
3255094Slling 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3265094Slling 			verify(nvlist_lookup_uint64_array(nvroot,
3275094Slling 			    ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
3285094Slling 
3295094Slling 			(void) strlcpy(buf, zpool_state_to_name(intval,
3305094Slling 			    vs->vs_aux), len);
3315094Slling 			break;
3325094Slling 		default:
3335094Slling 			(void) snprintf(buf, len, "%llu", intval);
3345094Slling 		}
3355094Slling 		break;
3365094Slling 
3375094Slling 	case PROP_TYPE_INDEX:
3385094Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
3395094Slling 		if (zpool_prop_index_to_string(prop, intval, &strval)
3405094Slling 		    != 0)
3415094Slling 			return (-1);
3425094Slling 		(void) strlcpy(buf, strval, len);
3435094Slling 		break;
3445094Slling 
3455094Slling 	default:
3465094Slling 		abort();
3475094Slling 	}
3485094Slling 
3495094Slling 	if (srctype)
3505094Slling 		*srctype = src;
3515094Slling 
3525094Slling 	return (0);
3535094Slling }
3545094Slling 
3555094Slling /*
3565094Slling  * Check if the bootfs name has the same pool name as it is set to.
3575094Slling  * Assuming bootfs is a valid dataset name.
3585094Slling  */
3595094Slling static boolean_t
3605094Slling bootfs_name_valid(const char *pool, char *bootfs)
3615094Slling {
3625094Slling 	int len = strlen(pool);
3635094Slling 
3647300SEric.Taylor@Sun.COM 	if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
3655094Slling 		return (B_FALSE);
3665094Slling 
3675094Slling 	if (strncmp(pool, bootfs, len) == 0 &&
3685094Slling 	    (bootfs[len] == '/' || bootfs[len] == '\0'))
3695094Slling 		return (B_TRUE);
3705094Slling 
3715094Slling 	return (B_FALSE);
3725094Slling }
3735094Slling 
3745094Slling /*
3757042Sgw25295  * Inspect the configuration to determine if any of the devices contain
3767042Sgw25295  * an EFI label.
3777042Sgw25295  */
3787042Sgw25295 static boolean_t
3797042Sgw25295 pool_uses_efi(nvlist_t *config)
3807042Sgw25295 {
3817042Sgw25295 	nvlist_t **child;
3827042Sgw25295 	uint_t c, children;
3837042Sgw25295 
3847042Sgw25295 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
3857042Sgw25295 	    &child, &children) != 0)
3867042Sgw25295 		return (read_efi_label(config, NULL) >= 0);
3877042Sgw25295 
3887042Sgw25295 	for (c = 0; c < children; c++) {
3897042Sgw25295 		if (pool_uses_efi(child[c]))
3907042Sgw25295 			return (B_TRUE);
3917042Sgw25295 	}
3927042Sgw25295 	return (B_FALSE);
3937042Sgw25295 }
3947042Sgw25295 
3957965SGeorge.Wilson@Sun.COM static boolean_t
3967965SGeorge.Wilson@Sun.COM pool_is_bootable(zpool_handle_t *zhp)
3977965SGeorge.Wilson@Sun.COM {
3987965SGeorge.Wilson@Sun.COM 	char bootfs[ZPOOL_MAXNAMELEN];
3997965SGeorge.Wilson@Sun.COM 
4007965SGeorge.Wilson@Sun.COM 	return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
4017965SGeorge.Wilson@Sun.COM 	    sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
4027965SGeorge.Wilson@Sun.COM 	    sizeof (bootfs)) != 0);
4037965SGeorge.Wilson@Sun.COM }
4047965SGeorge.Wilson@Sun.COM 
4057965SGeorge.Wilson@Sun.COM 
4067042Sgw25295 /*
4075094Slling  * Given an nvlist of zpool properties to be set, validate that they are
4085094Slling  * correct, and parse any numeric properties (index, boolean, etc) if they are
4095094Slling  * specified as strings.
4105094Slling  */
4115094Slling static nvlist_t *
4127184Stimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
4135094Slling     nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf)
4145094Slling {
4155094Slling 	nvpair_t *elem;
4165094Slling 	nvlist_t *retprops;
4175094Slling 	zpool_prop_t prop;
4185094Slling 	char *strval;
4195094Slling 	uint64_t intval;
4205363Seschrock 	char *slash;
4215363Seschrock 	struct stat64 statbuf;
4227042Sgw25295 	zpool_handle_t *zhp;
4237042Sgw25295 	nvlist_t *nvroot;
4245094Slling 
4255094Slling 	if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
4265094Slling 		(void) no_memory(hdl);
4275094Slling 		return (NULL);
4285094Slling 	}
4295094Slling 
4305094Slling 	elem = NULL;
4315094Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
4325094Slling 		const char *propname = nvpair_name(elem);
4335094Slling 
4345094Slling 		/*
4355094Slling 		 * Make sure this property is valid and applies to this type.
4365094Slling 		 */
4375094Slling 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
4385094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4395094Slling 			    "invalid property '%s'"), propname);
4405094Slling 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
4415094Slling 			goto error;
4425094Slling 		}
4435094Slling 
4445094Slling 		if (zpool_prop_readonly(prop)) {
4455094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
4465094Slling 			    "is readonly"), propname);
4475094Slling 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
4485094Slling 			goto error;
4495094Slling 		}
4505094Slling 
4515094Slling 		if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
4525094Slling 		    &strval, &intval, errbuf) != 0)
4535094Slling 			goto error;
4545094Slling 
4555094Slling 		/*
4565094Slling 		 * Perform additional checking for specific properties.
4575094Slling 		 */
4585094Slling 		switch (prop) {
4595094Slling 		case ZPOOL_PROP_VERSION:
4605094Slling 			if (intval < version || intval > SPA_VERSION) {
4615094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4625094Slling 				    "property '%s' number %d is invalid."),
4635094Slling 				    propname, intval);
4645094Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4655094Slling 				goto error;
4665094Slling 			}
4675094Slling 			break;
4685094Slling 
4695094Slling 		case ZPOOL_PROP_BOOTFS:
4705094Slling 			if (create_or_import) {
4715094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4725094Slling 				    "property '%s' cannot be set at creation "
4735094Slling 				    "or import time"), propname);
4745094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
4755094Slling 				goto error;
4765094Slling 			}
4775094Slling 
4785094Slling 			if (version < SPA_VERSION_BOOTFS) {
4795094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4805094Slling 				    "pool must be upgraded to support "
4815094Slling 				    "'%s' property"), propname);
4825094Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4835094Slling 				goto error;
4845094Slling 			}
4855094Slling 
4865094Slling 			/*
4875094Slling 			 * bootfs property value has to be a dataset name and
4885094Slling 			 * the dataset has to be in the same pool as it sets to.
4895094Slling 			 */
4905094Slling 			if (strval[0] != '\0' && !bootfs_name_valid(poolname,
4915094Slling 			    strval)) {
4925094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
4935094Slling 				    "is an invalid name"), strval);
4945094Slling 				(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4955094Slling 				goto error;
4965094Slling 			}
4977042Sgw25295 
4987042Sgw25295 			if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
4997042Sgw25295 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5007042Sgw25295 				    "could not open pool '%s'"), poolname);
5017042Sgw25295 				(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
5027042Sgw25295 				goto error;
5037042Sgw25295 			}
5047042Sgw25295 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
5057042Sgw25295 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
5067042Sgw25295 
5077042Sgw25295 			/*
5087042Sgw25295 			 * bootfs property cannot be set on a disk which has
5097042Sgw25295 			 * been EFI labeled.
5107042Sgw25295 			 */
5117042Sgw25295 			if (pool_uses_efi(nvroot)) {
5127042Sgw25295 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5137042Sgw25295 				    "property '%s' not supported on "
5147042Sgw25295 				    "EFI labeled devices"), propname);
5157042Sgw25295 				(void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
5167042Sgw25295 				zpool_close(zhp);
5177042Sgw25295 				goto error;
5187042Sgw25295 			}
5197042Sgw25295 			zpool_close(zhp);
5205094Slling 			break;
5215094Slling 
5225094Slling 		case ZPOOL_PROP_ALTROOT:
5235094Slling 			if (!create_or_import) {
5245094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5255094Slling 				    "property '%s' can only be set during pool "
5265094Slling 				    "creation or import"), propname);
5275094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5285094Slling 				goto error;
5295094Slling 			}
5305094Slling 
5315094Slling 			if (strval[0] != '/') {
5325094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5335094Slling 				    "bad alternate root '%s'"), strval);
5345094Slling 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
5355094Slling 				goto error;
5365094Slling 			}
5375094Slling 			break;
5385363Seschrock 
5395363Seschrock 		case ZPOOL_PROP_CACHEFILE:
5405363Seschrock 			if (strval[0] == '\0')
5415363Seschrock 				break;
5425363Seschrock 
5435363Seschrock 			if (strcmp(strval, "none") == 0)
5445363Seschrock 				break;
5455363Seschrock 
5465363Seschrock 			if (strval[0] != '/') {
5475363Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5485363Seschrock 				    "property '%s' must be empty, an "
5495363Seschrock 				    "absolute path, or 'none'"), propname);
5505363Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
5515363Seschrock 				goto error;
5525363Seschrock 			}
5535363Seschrock 
5545363Seschrock 			slash = strrchr(strval, '/');
5555363Seschrock 
5565363Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
5575363Seschrock 			    strcmp(slash, "/..") == 0) {
5585363Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5595363Seschrock 				    "'%s' is not a valid file"), strval);
5605363Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
5615363Seschrock 				goto error;
5625363Seschrock 			}
5635363Seschrock 
5645363Seschrock 			*slash = '\0';
5655363Seschrock 
5665621Seschrock 			if (strval[0] != '\0' &&
5675621Seschrock 			    (stat64(strval, &statbuf) != 0 ||
5685621Seschrock 			    !S_ISDIR(statbuf.st_mode))) {
5695363Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5705363Seschrock 				    "'%s' is not a valid directory"),
5715363Seschrock 				    strval);
5725363Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
5735363Seschrock 				goto error;
5745363Seschrock 			}
5755363Seschrock 
5765363Seschrock 			*slash = '/';
5775363Seschrock 			break;
5785094Slling 		}
5795094Slling 	}
5805094Slling 
5815094Slling 	return (retprops);
5825094Slling error:
5835094Slling 	nvlist_free(retprops);
5845094Slling 	return (NULL);
5855094Slling }
5865094Slling 
5875094Slling /*
5885094Slling  * Set zpool property : propname=propval.
5895094Slling  */
5905094Slling int
5915094Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
5925094Slling {
5935094Slling 	zfs_cmd_t zc = { 0 };
5945094Slling 	int ret = -1;
5955094Slling 	char errbuf[1024];
5965094Slling 	nvlist_t *nvl = NULL;
5975094Slling 	nvlist_t *realprops;
5985094Slling 	uint64_t version;
5995094Slling 
6005094Slling 	(void) snprintf(errbuf, sizeof (errbuf),
6015094Slling 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
6025094Slling 	    zhp->zpool_name);
6035094Slling 
6045094Slling 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
6055094Slling 		return (no_memory(zhp->zpool_hdl));
6065094Slling 
6075094Slling 	if (nvlist_add_string(nvl, propname, propval) != 0) {
6085094Slling 		nvlist_free(nvl);
6095094Slling 		return (no_memory(zhp->zpool_hdl));
6105094Slling 	}
6115094Slling 
6125094Slling 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
6137184Stimh 	if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
6145094Slling 	    zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) {
6155094Slling 		nvlist_free(nvl);
6165094Slling 		return (-1);
6175094Slling 	}
6185094Slling 
6195094Slling 	nvlist_free(nvl);
6205094Slling 	nvl = realprops;
6215094Slling 
6225094Slling 	/*
6235094Slling 	 * Execute the corresponding ioctl() to set this property.
6245094Slling 	 */
6255094Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
6265094Slling 
6275094Slling 	if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
6285094Slling 		nvlist_free(nvl);
6295094Slling 		return (-1);
6305094Slling 	}
6315094Slling 
6325094Slling 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
6335094Slling 
6345094Slling 	zcmd_free_nvlists(&zc);
6355094Slling 	nvlist_free(nvl);
6365094Slling 
6375094Slling 	if (ret)
6385094Slling 		(void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
6395094Slling 	else
6405094Slling 		(void) zpool_props_refresh(zhp);
6415094Slling 
6425094Slling 	return (ret);
6435094Slling }
6445094Slling 
6455094Slling int
6465094Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
6475094Slling {
6485094Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
6495094Slling 	zprop_list_t *entry;
6505094Slling 	char buf[ZFS_MAXPROPLEN];
6515094Slling 
6525094Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
6535094Slling 		return (-1);
6545094Slling 
6555094Slling 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
6565094Slling 
6575094Slling 		if (entry->pl_fixed)
6585094Slling 			continue;
6595094Slling 
6605094Slling 		if (entry->pl_prop != ZPROP_INVAL &&
6615094Slling 		    zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
6625094Slling 		    NULL) == 0) {
6635094Slling 			if (strlen(buf) > entry->pl_width)
6645094Slling 				entry->pl_width = strlen(buf);
6655094Slling 		}
6665094Slling 	}
6675094Slling 
6685094Slling 	return (0);
6695094Slling }
6705094Slling 
6715094Slling 
672789Sahrens /*
6739816SGeorge.Wilson@Sun.COM  * Don't start the slice at the default block of 34; many storage
6749816SGeorge.Wilson@Sun.COM  * devices will use a stripe width of 128k, so start there instead.
6759816SGeorge.Wilson@Sun.COM  */
6769816SGeorge.Wilson@Sun.COM #define	NEW_START_BLOCK	256
6779816SGeorge.Wilson@Sun.COM 
6789816SGeorge.Wilson@Sun.COM /*
679789Sahrens  * Validate the given pool name, optionally putting an extended error message in
680789Sahrens  * 'buf'.
681789Sahrens  */
6826423Sgw25295 boolean_t
6832082Seschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
684789Sahrens {
685789Sahrens 	namecheck_err_t why;
686789Sahrens 	char what;
6871773Seschrock 	int ret;
688789Sahrens 
6891773Seschrock 	ret = pool_namecheck(pool, &why, &what);
6901773Seschrock 
6911773Seschrock 	/*
6921773Seschrock 	 * The rules for reserved pool names were extended at a later point.
6931773Seschrock 	 * But we need to support users with existing pools that may now be
6941773Seschrock 	 * invalid.  So we only check for this expanded set of names during a
6951773Seschrock 	 * create (or import), and only in userland.
6961773Seschrock 	 */
6971773Seschrock 	if (ret == 0 && !isopen &&
6981773Seschrock 	    (strncmp(pool, "mirror", 6) == 0 ||
6991773Seschrock 	    strncmp(pool, "raidz", 5) == 0 ||
7004527Sperrin 	    strncmp(pool, "spare", 5) == 0 ||
7014527Sperrin 	    strcmp(pool, "log") == 0)) {
7026423Sgw25295 		if (hdl != NULL)
7036423Sgw25295 			zfs_error_aux(hdl,
7046423Sgw25295 			    dgettext(TEXT_DOMAIN, "name is reserved"));
7052082Seschrock 		return (B_FALSE);
7061773Seschrock 	}
7071773Seschrock 
7081773Seschrock 
7091773Seschrock 	if (ret != 0) {
7102082Seschrock 		if (hdl != NULL) {
711789Sahrens 			switch (why) {
7121003Slling 			case NAME_ERR_TOOLONG:
7132082Seschrock 				zfs_error_aux(hdl,
7141003Slling 				    dgettext(TEXT_DOMAIN, "name is too long"));
7151003Slling 				break;
7161003Slling 
717789Sahrens 			case NAME_ERR_INVALCHAR:
7182082Seschrock 				zfs_error_aux(hdl,
719789Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
720789Sahrens 				    "'%c' in pool name"), what);
721789Sahrens 				break;
722789Sahrens 
723789Sahrens 			case NAME_ERR_NOLETTER:
7242082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7252082Seschrock 				    "name must begin with a letter"));
726789Sahrens 				break;
727789Sahrens 
728789Sahrens 			case NAME_ERR_RESERVED:
7292082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7302082Seschrock 				    "name is reserved"));
731789Sahrens 				break;
732789Sahrens 
733789Sahrens 			case NAME_ERR_DISKLIKE:
7342082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7352082Seschrock 				    "pool name is reserved"));
736789Sahrens 				break;
7372856Snd150628 
7382856Snd150628 			case NAME_ERR_LEADING_SLASH:
7392856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7402856Snd150628 				    "leading slash in name"));
7412856Snd150628 				break;
7422856Snd150628 
7432856Snd150628 			case NAME_ERR_EMPTY_COMPONENT:
7442856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7452856Snd150628 				    "empty component in name"));
7462856Snd150628 				break;
7472856Snd150628 
7482856Snd150628 			case NAME_ERR_TRAILING_SLASH:
7492856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7502856Snd150628 				    "trailing slash in name"));
7512856Snd150628 				break;
7522856Snd150628 
7532856Snd150628 			case NAME_ERR_MULTIPLE_AT:
7542856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7552856Snd150628 				    "multiple '@' delimiters in name"));
7562856Snd150628 				break;
7572856Snd150628 
758789Sahrens 			}
759789Sahrens 		}
7602082Seschrock 		return (B_FALSE);
761789Sahrens 	}
762789Sahrens 
7632082Seschrock 	return (B_TRUE);
764789Sahrens }
765789Sahrens 
766789Sahrens /*
767789Sahrens  * Open a handle to the given pool, even if the pool is currently in the FAULTED
768789Sahrens  * state.
769789Sahrens  */
770789Sahrens zpool_handle_t *
7712082Seschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
772789Sahrens {
773789Sahrens 	zpool_handle_t *zhp;
7742142Seschrock 	boolean_t missing;
775789Sahrens 
776789Sahrens 	/*
777789Sahrens 	 * Make sure the pool name is valid.
778789Sahrens 	 */
7792082Seschrock 	if (!zpool_name_valid(hdl, B_TRUE, pool)) {
7803237Slling 		(void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
7812082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
7822082Seschrock 		    pool);
783789Sahrens 		return (NULL);
784789Sahrens 	}
785789Sahrens 
7862082Seschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
7872082Seschrock 		return (NULL);
788789Sahrens 
7892082Seschrock 	zhp->zpool_hdl = hdl;
790789Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
791789Sahrens 
7922142Seschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
7932142Seschrock 		zpool_close(zhp);
7942142Seschrock 		return (NULL);
7952142Seschrock 	}
7962142Seschrock 
7972142Seschrock 	if (missing) {
7985094Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
7993237Slling 		(void) zfs_error_fmt(hdl, EZFS_NOENT,
8005094Slling 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
8012142Seschrock 		zpool_close(zhp);
8022142Seschrock 		return (NULL);
803789Sahrens 	}
804789Sahrens 
805789Sahrens 	return (zhp);
806789Sahrens }
807789Sahrens 
808789Sahrens /*
809789Sahrens  * Like the above, but silent on error.  Used when iterating over pools (because
810789Sahrens  * the configuration cache may be out of date).
811789Sahrens  */
8122142Seschrock int
8132142Seschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
814789Sahrens {
815789Sahrens 	zpool_handle_t *zhp;
8162142Seschrock 	boolean_t missing;
817789Sahrens 
8182142Seschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
8192142Seschrock 		return (-1);
820789Sahrens 
8212082Seschrock 	zhp->zpool_hdl = hdl;
822789Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
823789Sahrens 
8242142Seschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
8252142Seschrock 		zpool_close(zhp);
8262142Seschrock 		return (-1);
827789Sahrens 	}
828789Sahrens 
8292142Seschrock 	if (missing) {
8302142Seschrock 		zpool_close(zhp);
8312142Seschrock 		*ret = NULL;
8322142Seschrock 		return (0);
8332142Seschrock 	}
8342142Seschrock 
8352142Seschrock 	*ret = zhp;
8362142Seschrock 	return (0);
837789Sahrens }
838789Sahrens 
839789Sahrens /*
840789Sahrens  * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
841789Sahrens  * state.
842789Sahrens  */
843789Sahrens zpool_handle_t *
8442082Seschrock zpool_open(libzfs_handle_t *hdl, const char *pool)
845789Sahrens {
846789Sahrens 	zpool_handle_t *zhp;
847789Sahrens 
8482082Seschrock 	if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
849789Sahrens 		return (NULL);
850789Sahrens 
851789Sahrens 	if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
8523237Slling 		(void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
8532082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
854789Sahrens 		zpool_close(zhp);
855789Sahrens 		return (NULL);
856789Sahrens 	}
857789Sahrens 
858789Sahrens 	return (zhp);
859789Sahrens }
860789Sahrens 
861789Sahrens /*
862789Sahrens  * Close the handle.  Simply frees the memory associated with the handle.
863789Sahrens  */
864789Sahrens void
865789Sahrens zpool_close(zpool_handle_t *zhp)
866789Sahrens {
867789Sahrens 	if (zhp->zpool_config)
868789Sahrens 		nvlist_free(zhp->zpool_config);
869952Seschrock 	if (zhp->zpool_old_config)
870952Seschrock 		nvlist_free(zhp->zpool_old_config);
8713912Slling 	if (zhp->zpool_props)
8723912Slling 		nvlist_free(zhp->zpool_props);
873789Sahrens 	free(zhp);
874789Sahrens }
875789Sahrens 
876789Sahrens /*
877789Sahrens  * Return the name of the pool.
878789Sahrens  */
879789Sahrens const char *
880789Sahrens zpool_get_name(zpool_handle_t *zhp)
881789Sahrens {
882789Sahrens 	return (zhp->zpool_name);
883789Sahrens }
884789Sahrens 
885789Sahrens 
886789Sahrens /*
887789Sahrens  * Return the state of the pool (ACTIVE or UNAVAILABLE)
888789Sahrens  */
889789Sahrens int
890789Sahrens zpool_get_state(zpool_handle_t *zhp)
891789Sahrens {
892789Sahrens 	return (zhp->zpool_state);
893789Sahrens }
894789Sahrens 
895789Sahrens /*
896789Sahrens  * Create the named pool, using the provided vdev list.  It is assumed
897789Sahrens  * that the consumer has already validated the contents of the nvlist, so we
898789Sahrens  * don't have to worry about error semantics.
899789Sahrens  */
900789Sahrens int
9012082Seschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
9027184Stimh     nvlist_t *props, nvlist_t *fsprops)
903789Sahrens {
904789Sahrens 	zfs_cmd_t zc = { 0 };
9057184Stimh 	nvlist_t *zc_fsprops = NULL;
9067184Stimh 	nvlist_t *zc_props = NULL;
9072082Seschrock 	char msg[1024];
9085094Slling 	char *altroot;
9097184Stimh 	int ret = -1;
9102082Seschrock 
9112082Seschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
9122082Seschrock 	    "cannot create '%s'"), pool);
913789Sahrens 
9142082Seschrock 	if (!zpool_name_valid(hdl, B_FALSE, pool))
9152082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
9162082Seschrock 
9175320Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
9185320Slling 		return (-1);
9195320Slling 
9207184Stimh 	if (props) {
9217184Stimh 		if ((zc_props = zpool_valid_proplist(hdl, pool, props,
9227184Stimh 		    SPA_VERSION_1, B_TRUE, msg)) == NULL) {
9237184Stimh 			goto create_failed;
9247184Stimh 		}
9255320Slling 	}
926789Sahrens 
9277184Stimh 	if (fsprops) {
9287184Stimh 		uint64_t zoned;
9297184Stimh 		char *zonestr;
9307184Stimh 
9317184Stimh 		zoned = ((nvlist_lookup_string(fsprops,
9327184Stimh 		    zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
9337184Stimh 		    strcmp(zonestr, "on") == 0);
9347184Stimh 
9357184Stimh 		if ((zc_fsprops = zfs_valid_proplist(hdl,
9367184Stimh 		    ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
9377184Stimh 			goto create_failed;
9387184Stimh 		}
9397184Stimh 		if (!zc_props &&
9407184Stimh 		    (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
9417184Stimh 			goto create_failed;
9427184Stimh 		}
9437184Stimh 		if (nvlist_add_nvlist(zc_props,
9447184Stimh 		    ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
9457184Stimh 			goto create_failed;
9467184Stimh 		}
9477184Stimh 	}
9487184Stimh 
9497184Stimh 	if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
9507184Stimh 		goto create_failed;
9517184Stimh 
952789Sahrens 	(void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
953789Sahrens 
9547184Stimh 	if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
9555320Slling 
9562676Seschrock 		zcmd_free_nvlists(&zc);
9577184Stimh 		nvlist_free(zc_props);
9587184Stimh 		nvlist_free(zc_fsprops);
9592082Seschrock 
960789Sahrens 		switch (errno) {
961789Sahrens 		case EBUSY:
962789Sahrens 			/*
963789Sahrens 			 * This can happen if the user has specified the same
964789Sahrens 			 * device multiple times.  We can't reliably detect this
965789Sahrens 			 * until we try to add it and see we already have a
966789Sahrens 			 * label.
967789Sahrens 			 */
9682082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9692082Seschrock 			    "one or more vdevs refer to the same device"));
9702082Seschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
971789Sahrens 
972789Sahrens 		case EOVERFLOW:
973789Sahrens 			/*
9742082Seschrock 			 * This occurs when one of the devices is below
975789Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
976789Sahrens 			 * device was the problem device since there's no
977789Sahrens 			 * reliable way to determine device size from userland.
978789Sahrens 			 */
979789Sahrens 			{
980789Sahrens 				char buf[64];
981789Sahrens 
982789Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
983789Sahrens 
9842082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9852082Seschrock 				    "one or more devices is less than the "
9862082Seschrock 				    "minimum size (%s)"), buf);
987789Sahrens 			}
9882082Seschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
989789Sahrens 
990789Sahrens 		case ENOSPC:
9912082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9922082Seschrock 			    "one or more devices is out of space"));
9932082Seschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
994789Sahrens 
9955450Sbrendan 		case ENOTBLK:
9965450Sbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9975450Sbrendan 			    "cache device must be a disk or disk slice"));
9985450Sbrendan 			return (zfs_error(hdl, EZFS_BADDEV, msg));
9995450Sbrendan 
1000789Sahrens 		default:
10012082Seschrock 			return (zpool_standard_error(hdl, errno, msg));
1002789Sahrens 		}
1003789Sahrens 	}
1004789Sahrens 
1005789Sahrens 	/*
1006789Sahrens 	 * If this is an alternate root pool, then we automatically set the
10072676Seschrock 	 * mountpoint of the root dataset to be '/'.
1008789Sahrens 	 */
10095094Slling 	if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
10105094Slling 	    &altroot) == 0) {
1011789Sahrens 		zfs_handle_t *zhp;
1012789Sahrens 
10135094Slling 		verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
10142676Seschrock 		verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
10152676Seschrock 		    "/") == 0);
1016789Sahrens 
1017789Sahrens 		zfs_close(zhp);
1018789Sahrens 	}
1019789Sahrens 
10207184Stimh create_failed:
10215320Slling 	zcmd_free_nvlists(&zc);
10227184Stimh 	nvlist_free(zc_props);
10237184Stimh 	nvlist_free(zc_fsprops);
10247184Stimh 	return (ret);
1025789Sahrens }
1026789Sahrens 
1027789Sahrens /*
1028789Sahrens  * Destroy the given pool.  It is up to the caller to ensure that there are no
1029789Sahrens  * datasets left in the pool.
1030789Sahrens  */
1031789Sahrens int
1032789Sahrens zpool_destroy(zpool_handle_t *zhp)
1033789Sahrens {
1034789Sahrens 	zfs_cmd_t zc = { 0 };
1035789Sahrens 	zfs_handle_t *zfp = NULL;
10362082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
10372082Seschrock 	char msg[1024];
1038789Sahrens 
1039789Sahrens 	if (zhp->zpool_state == POOL_STATE_ACTIVE &&
10402082Seschrock 	    (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name,
10412082Seschrock 	    ZFS_TYPE_FILESYSTEM)) == NULL)
1042789Sahrens 		return (-1);
1043789Sahrens 
1044789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1045789Sahrens 
10464543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
10472082Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
10482082Seschrock 		    "cannot destroy '%s'"), zhp->zpool_name);
1049789Sahrens 
10502082Seschrock 		if (errno == EROFS) {
10512082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10522082Seschrock 			    "one or more devices is read only"));
10532082Seschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
10542082Seschrock 		} else {
10552082Seschrock 			(void) zpool_standard_error(hdl, errno, msg);
1056789Sahrens 		}
1057789Sahrens 
1058789Sahrens 		if (zfp)
1059789Sahrens 			zfs_close(zfp);
1060789Sahrens 		return (-1);
1061789Sahrens 	}
1062789Sahrens 
1063789Sahrens 	if (zfp) {
1064789Sahrens 		remove_mountpoint(zfp);
1065789Sahrens 		zfs_close(zfp);
1066789Sahrens 	}
1067789Sahrens 
1068789Sahrens 	return (0);
1069789Sahrens }
1070789Sahrens 
1071789Sahrens /*
1072789Sahrens  * Add the given vdevs to the pool.  The caller must have already performed the
1073789Sahrens  * necessary verification to ensure that the vdev specification is well-formed.
1074789Sahrens  */
1075789Sahrens int
1076789Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1077789Sahrens {
10782676Seschrock 	zfs_cmd_t zc = { 0 };
10792082Seschrock 	int ret;
10802082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
10812082Seschrock 	char msg[1024];
10825450Sbrendan 	nvlist_t **spares, **l2cache;
10835450Sbrendan 	uint_t nspares, nl2cache;
10842082Seschrock 
10852082Seschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
10862082Seschrock 	    "cannot add to '%s'"), zhp->zpool_name);
10872082Seschrock 
10885450Sbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
10895450Sbrendan 	    SPA_VERSION_SPARES &&
10902082Seschrock 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
10912082Seschrock 	    &spares, &nspares) == 0) {
10922082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
10932082Seschrock 		    "upgraded to add hot spares"));
10942082Seschrock 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
10952082Seschrock 	}
1096789Sahrens 
10977965SGeorge.Wilson@Sun.COM 	if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
10987965SGeorge.Wilson@Sun.COM 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
10997965SGeorge.Wilson@Sun.COM 		uint64_t s;
11007965SGeorge.Wilson@Sun.COM 
11017965SGeorge.Wilson@Sun.COM 		for (s = 0; s < nspares; s++) {
11027965SGeorge.Wilson@Sun.COM 			char *path;
11037965SGeorge.Wilson@Sun.COM 
11047965SGeorge.Wilson@Sun.COM 			if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
11057965SGeorge.Wilson@Sun.COM 			    &path) == 0 && pool_uses_efi(spares[s])) {
11067965SGeorge.Wilson@Sun.COM 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11077965SGeorge.Wilson@Sun.COM 				    "device '%s' contains an EFI label and "
11087965SGeorge.Wilson@Sun.COM 				    "cannot be used on root pools."),
110910594SGeorge.Wilson@Sun.COM 				    zpool_vdev_name(hdl, NULL, spares[s],
111010594SGeorge.Wilson@Sun.COM 				    B_FALSE));
11117965SGeorge.Wilson@Sun.COM 				return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
11127965SGeorge.Wilson@Sun.COM 			}
11137965SGeorge.Wilson@Sun.COM 		}
11147965SGeorge.Wilson@Sun.COM 	}
11157965SGeorge.Wilson@Sun.COM 
11165450Sbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
11175450Sbrendan 	    SPA_VERSION_L2CACHE &&
11185450Sbrendan 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
11195450Sbrendan 	    &l2cache, &nl2cache) == 0) {
11205450Sbrendan 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
11215450Sbrendan 		    "upgraded to add cache devices"));
11225450Sbrendan 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
11235450Sbrendan 	}
11245450Sbrendan 
11255094Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
11262082Seschrock 		return (-1);
1127789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1128789Sahrens 
11294543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1130789Sahrens 		switch (errno) {
1131789Sahrens 		case EBUSY:
1132789Sahrens 			/*
1133789Sahrens 			 * This can happen if the user has specified the same
1134789Sahrens 			 * device multiple times.  We can't reliably detect this
1135789Sahrens 			 * until we try to add it and see we already have a
1136789Sahrens 			 * label.
1137789Sahrens 			 */
11382082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11392082Seschrock 			    "one or more vdevs refer to the same device"));
11402082Seschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1141789Sahrens 			break;
1142789Sahrens 
1143789Sahrens 		case EOVERFLOW:
1144789Sahrens 			/*
1145789Sahrens 			 * This occurrs when one of the devices is below
1146789Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1147789Sahrens 			 * device was the problem device since there's no
1148789Sahrens 			 * reliable way to determine device size from userland.
1149789Sahrens 			 */
1150789Sahrens 			{
1151789Sahrens 				char buf[64];
1152789Sahrens 
1153789Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1154789Sahrens 
11552082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11562082Seschrock 				    "device is less than the minimum "
11572082Seschrock 				    "size (%s)"), buf);
1158789Sahrens 			}
11592082Seschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
11602082Seschrock 			break;
11612082Seschrock 
11622082Seschrock 		case ENOTSUP:
11632082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11644527Sperrin 			    "pool must be upgraded to add these vdevs"));
11652082Seschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, msg);
1166789Sahrens 			break;
1167789Sahrens 
11683912Slling 		case EDOM:
11693912Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11704527Sperrin 			    "root pool can not have multiple vdevs"
11714527Sperrin 			    " or separate logs"));
11723912Slling 			(void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
11733912Slling 			break;
11743912Slling 
11755450Sbrendan 		case ENOTBLK:
11765450Sbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11775450Sbrendan 			    "cache device must be a disk or disk slice"));
11785450Sbrendan 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
11795450Sbrendan 			break;
11805450Sbrendan 
1181789Sahrens 		default:
11822082Seschrock 			(void) zpool_standard_error(hdl, errno, msg);
1183789Sahrens 		}
1184789Sahrens 
11852082Seschrock 		ret = -1;
11862082Seschrock 	} else {
11872082Seschrock 		ret = 0;
1188789Sahrens 	}
1189789Sahrens 
11902676Seschrock 	zcmd_free_nvlists(&zc);
1191789Sahrens 
11922082Seschrock 	return (ret);
1193789Sahrens }
1194789Sahrens 
1195789Sahrens /*
1196789Sahrens  * Exports the pool from the system.  The caller must ensure that there are no
1197789Sahrens  * mounted datasets in the pool.
1198789Sahrens  */
1199789Sahrens int
12008211SGeorge.Wilson@Sun.COM zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce)
1201789Sahrens {
1202789Sahrens 	zfs_cmd_t zc = { 0 };
12037214Slling 	char msg[1024];
1204789Sahrens 
12057214Slling 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
12067214Slling 	    "cannot export '%s'"), zhp->zpool_name);
12077214Slling 
1208789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
12097214Slling 	zc.zc_cookie = force;
12108211SGeorge.Wilson@Sun.COM 	zc.zc_guid = hardforce;
12117214Slling 
12127214Slling 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
12137214Slling 		switch (errno) {
12147214Slling 		case EXDEV:
12157214Slling 			zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
12167214Slling 			    "use '-f' to override the following errors:\n"
12177214Slling 			    "'%s' has an active shared spare which could be"
12187214Slling 			    " used by other pools once '%s' is exported."),
12197214Slling 			    zhp->zpool_name, zhp->zpool_name);
12207214Slling 			return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
12217214Slling 			    msg));
12227214Slling 		default:
12237214Slling 			return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
12247214Slling 			    msg));
12257214Slling 		}
12267214Slling 	}
12277214Slling 
1228789Sahrens 	return (0);
1229789Sahrens }
1230789Sahrens 
12318211SGeorge.Wilson@Sun.COM int
12328211SGeorge.Wilson@Sun.COM zpool_export(zpool_handle_t *zhp, boolean_t force)
12338211SGeorge.Wilson@Sun.COM {
12348211SGeorge.Wilson@Sun.COM 	return (zpool_export_common(zhp, force, B_FALSE));
12358211SGeorge.Wilson@Sun.COM }
12368211SGeorge.Wilson@Sun.COM 
12378211SGeorge.Wilson@Sun.COM int
12388211SGeorge.Wilson@Sun.COM zpool_export_force(zpool_handle_t *zhp)
12398211SGeorge.Wilson@Sun.COM {
12408211SGeorge.Wilson@Sun.COM 	return (zpool_export_common(zhp, B_TRUE, B_TRUE));
12418211SGeorge.Wilson@Sun.COM }
12428211SGeorge.Wilson@Sun.COM 
1243789Sahrens /*
12445094Slling  * zpool_import() is a contracted interface. Should be kept the same
12455094Slling  * if possible.
12465094Slling  *
12475094Slling  * Applications should use zpool_import_props() to import a pool with
12485094Slling  * new properties value to be set.
1249789Sahrens  */
1250789Sahrens int
12512082Seschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
12525094Slling     char *altroot)
12535094Slling {
12545094Slling 	nvlist_t *props = NULL;
12555094Slling 	int ret;
12565094Slling 
12575094Slling 	if (altroot != NULL) {
12585094Slling 		if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
12595094Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
12605094Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
12615094Slling 			    newname));
12625094Slling 		}
12635094Slling 
12645094Slling 		if (nvlist_add_string(props,
12658084SGeorge.Wilson@Sun.COM 		    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
12668084SGeorge.Wilson@Sun.COM 		    nvlist_add_string(props,
12678084SGeorge.Wilson@Sun.COM 		    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
12685094Slling 			nvlist_free(props);
12695094Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
12705094Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
12715094Slling 			    newname));
12725094Slling 		}
12735094Slling 	}
12745094Slling 
12756643Seschrock 	ret = zpool_import_props(hdl, config, newname, props, B_FALSE);
12765094Slling 	if (props)
12775094Slling 		nvlist_free(props);
12785094Slling 	return (ret);
12795094Slling }
12805094Slling 
12815094Slling /*
12825094Slling  * Import the given pool using the known configuration and a list of
12835094Slling  * properties to be set. The configuration should have come from
12845094Slling  * zpool_find_import(). The 'newname' parameters control whether the pool
12855094Slling  * is imported with a different name.
12865094Slling  */
12875094Slling int
12885094Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
12896643Seschrock     nvlist_t *props, boolean_t importfaulted)
1290789Sahrens {
12912676Seschrock 	zfs_cmd_t zc = { 0 };
1292789Sahrens 	char *thename;
1293789Sahrens 	char *origname;
1294789Sahrens 	int ret;
12955094Slling 	char errbuf[1024];
1296789Sahrens 
1297789Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1298789Sahrens 	    &origname) == 0);
1299789Sahrens 
13005094Slling 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
13015094Slling 	    "cannot import pool '%s'"), origname);
13025094Slling 
1303789Sahrens 	if (newname != NULL) {
13042082Seschrock 		if (!zpool_name_valid(hdl, B_FALSE, newname))
13053237Slling 			return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
13062082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
13072082Seschrock 			    newname));
1308789Sahrens 		thename = (char *)newname;
1309789Sahrens 	} else {
1310789Sahrens 		thename = origname;
1311789Sahrens 	}
1312789Sahrens 
13135094Slling 	if (props) {
13145094Slling 		uint64_t version;
13155094Slling 
13165094Slling 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
13175094Slling 		    &version) == 0);
13185094Slling 
13197184Stimh 		if ((props = zpool_valid_proplist(hdl, origname,
13205320Slling 		    props, version, B_TRUE, errbuf)) == NULL) {
13215094Slling 			return (-1);
13225320Slling 		} else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
13235320Slling 			nvlist_free(props);
13245094Slling 			return (-1);
13255320Slling 		}
13265094Slling 	}
1327789Sahrens 
1328789Sahrens 	(void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1329789Sahrens 
1330789Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
13311544Seschrock 	    &zc.zc_guid) == 0);
1332789Sahrens 
13335320Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
13345320Slling 		nvlist_free(props);
13352082Seschrock 		return (-1);
13365320Slling 	}
1337789Sahrens 
13386643Seschrock 	zc.zc_cookie = (uint64_t)importfaulted;
1339789Sahrens 	ret = 0;
13404543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) {
1341789Sahrens 		char desc[1024];
1342789Sahrens 		if (newname == NULL)
1343789Sahrens 			(void) snprintf(desc, sizeof (desc),
1344789Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1345789Sahrens 			    thename);
1346789Sahrens 		else
1347789Sahrens 			(void) snprintf(desc, sizeof (desc),
1348789Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1349789Sahrens 			    origname, thename);
1350789Sahrens 
1351789Sahrens 		switch (errno) {
13521544Seschrock 		case ENOTSUP:
13531544Seschrock 			/*
13541544Seschrock 			 * Unsupported version.
13551544Seschrock 			 */
13562082Seschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, desc);
13571544Seschrock 			break;
13581544Seschrock 
13592174Seschrock 		case EINVAL:
13602174Seschrock 			(void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
13612174Seschrock 			break;
13622174Seschrock 
1363789Sahrens 		default:
13642082Seschrock 			(void) zpool_standard_error(hdl, errno, desc);
1365789Sahrens 		}
1366789Sahrens 
1367789Sahrens 		ret = -1;
1368789Sahrens 	} else {
1369789Sahrens 		zpool_handle_t *zhp;
13704543Smarks 
1371789Sahrens 		/*
1372789Sahrens 		 * This should never fail, but play it safe anyway.
1373789Sahrens 		 */
137410588SEric.Taylor@Sun.COM 		if (zpool_open_silent(hdl, thename, &zhp) != 0)
13752142Seschrock 			ret = -1;
137610588SEric.Taylor@Sun.COM 		else if (zhp != NULL)
1377789Sahrens 			zpool_close(zhp);
1378789Sahrens 	}
1379789Sahrens 
13802676Seschrock 	zcmd_free_nvlists(&zc);
13815320Slling 	nvlist_free(props);
13825320Slling 
1383789Sahrens 	return (ret);
1384789Sahrens }
1385789Sahrens 
1386789Sahrens /*
1387789Sahrens  * Scrub the pool.
1388789Sahrens  */
1389789Sahrens int
1390789Sahrens zpool_scrub(zpool_handle_t *zhp, pool_scrub_type_t type)
1391789Sahrens {
1392789Sahrens 	zfs_cmd_t zc = { 0 };
1393789Sahrens 	char msg[1024];
13942082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1395789Sahrens 
1396789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1397789Sahrens 	zc.zc_cookie = type;
1398789Sahrens 
13994543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCRUB, &zc) == 0)
1400789Sahrens 		return (0);
1401789Sahrens 
1402789Sahrens 	(void) snprintf(msg, sizeof (msg),
1403789Sahrens 	    dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1404789Sahrens 
14052082Seschrock 	if (errno == EBUSY)
14062082Seschrock 		return (zfs_error(hdl, EZFS_RESILVERING, msg));
14072082Seschrock 	else
14082082Seschrock 		return (zpool_standard_error(hdl, errno, msg));
1409789Sahrens }
1410789Sahrens 
14112468Sek110237 /*
14129816SGeorge.Wilson@Sun.COM  * Find a vdev that matches the search criteria specified. We use the
14139816SGeorge.Wilson@Sun.COM  * the nvpair name to determine how we should look for the device.
14142468Sek110237  * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
14152468Sek110237  * spare; but FALSE if its an INUSE spare.
14162468Sek110237  */
14172082Seschrock static nvlist_t *
14189816SGeorge.Wilson@Sun.COM vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
14199816SGeorge.Wilson@Sun.COM     boolean_t *l2cache, boolean_t *log)
14201544Seschrock {
14211544Seschrock 	uint_t c, children;
14221544Seschrock 	nvlist_t **child;
14232082Seschrock 	nvlist_t *ret;
14247326SEric.Schrock@Sun.COM 	uint64_t is_log;
14259816SGeorge.Wilson@Sun.COM 	char *srchkey;
14269816SGeorge.Wilson@Sun.COM 	nvpair_t *pair = nvlist_next_nvpair(search, NULL);
14279816SGeorge.Wilson@Sun.COM 
14289816SGeorge.Wilson@Sun.COM 	/* Nothing to look for */
14299816SGeorge.Wilson@Sun.COM 	if (search == NULL || pair == NULL)
14309816SGeorge.Wilson@Sun.COM 		return (NULL);
14319816SGeorge.Wilson@Sun.COM 
14329816SGeorge.Wilson@Sun.COM 	/* Obtain the key we will use to search */
14339816SGeorge.Wilson@Sun.COM 	srchkey = nvpair_name(pair);
14349816SGeorge.Wilson@Sun.COM 
14359816SGeorge.Wilson@Sun.COM 	switch (nvpair_type(pair)) {
14369816SGeorge.Wilson@Sun.COM 	case DATA_TYPE_UINT64: {
14379816SGeorge.Wilson@Sun.COM 		uint64_t srchval, theguid, present;
14389816SGeorge.Wilson@Sun.COM 
14399816SGeorge.Wilson@Sun.COM 		verify(nvpair_value_uint64(pair, &srchval) == 0);
14409816SGeorge.Wilson@Sun.COM 		if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
14419816SGeorge.Wilson@Sun.COM 			if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
14429816SGeorge.Wilson@Sun.COM 			    &present) == 0) {
14439816SGeorge.Wilson@Sun.COM 				/*
14449816SGeorge.Wilson@Sun.COM 				 * If the device has never been present since
14459816SGeorge.Wilson@Sun.COM 				 * import, the only reliable way to match the
14469816SGeorge.Wilson@Sun.COM 				 * vdev is by GUID.
14479816SGeorge.Wilson@Sun.COM 				 */
14489816SGeorge.Wilson@Sun.COM 				verify(nvlist_lookup_uint64(nv,
14499816SGeorge.Wilson@Sun.COM 				    ZPOOL_CONFIG_GUID, &theguid) == 0);
14509816SGeorge.Wilson@Sun.COM 				if (theguid == srchval)
14519816SGeorge.Wilson@Sun.COM 					return (nv);
14529816SGeorge.Wilson@Sun.COM 			}
14539816SGeorge.Wilson@Sun.COM 		}
14549816SGeorge.Wilson@Sun.COM 		break;
14559816SGeorge.Wilson@Sun.COM 	}
14569816SGeorge.Wilson@Sun.COM 
14579816SGeorge.Wilson@Sun.COM 	case DATA_TYPE_STRING: {
14589816SGeorge.Wilson@Sun.COM 		char *srchval, *val;
14599816SGeorge.Wilson@Sun.COM 
14609816SGeorge.Wilson@Sun.COM 		verify(nvpair_value_string(pair, &srchval) == 0);
14619816SGeorge.Wilson@Sun.COM 		if (nvlist_lookup_string(nv, srchkey, &val) != 0)
14629816SGeorge.Wilson@Sun.COM 			break;
14639816SGeorge.Wilson@Sun.COM 
14641544Seschrock 		/*
14659816SGeorge.Wilson@Sun.COM 		 * Search for the requested value. We special case the search
146610594SGeorge.Wilson@Sun.COM 		 * for ZPOOL_CONFIG_PATH when it's a wholedisk and when
146710594SGeorge.Wilson@Sun.COM 		 * Looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
146810594SGeorge.Wilson@Sun.COM 		 * Otherwise, all other searches are simple string compares.
14691544Seschrock 		 */
14709816SGeorge.Wilson@Sun.COM 		if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 && val) {
14719816SGeorge.Wilson@Sun.COM 			uint64_t wholedisk = 0;
14729816SGeorge.Wilson@Sun.COM 
14739816SGeorge.Wilson@Sun.COM 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
14749816SGeorge.Wilson@Sun.COM 			    &wholedisk);
14759816SGeorge.Wilson@Sun.COM 			if (wholedisk) {
14769816SGeorge.Wilson@Sun.COM 				/*
14779816SGeorge.Wilson@Sun.COM 				 * For whole disks, the internal path has 's0',
14789816SGeorge.Wilson@Sun.COM 				 * but the path passed in by the user doesn't.
14799816SGeorge.Wilson@Sun.COM 				 */
14809816SGeorge.Wilson@Sun.COM 				if (strlen(srchval) == strlen(val) - 2 &&
14819816SGeorge.Wilson@Sun.COM 				    strncmp(srchval, val, strlen(srchval)) == 0)
14829816SGeorge.Wilson@Sun.COM 					return (nv);
14839816SGeorge.Wilson@Sun.COM 				break;
14849816SGeorge.Wilson@Sun.COM 			}
148510594SGeorge.Wilson@Sun.COM 		} else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
148610594SGeorge.Wilson@Sun.COM 			char *type, *idx, *end, *p;
148710594SGeorge.Wilson@Sun.COM 			uint64_t id, vdev_id;
148810594SGeorge.Wilson@Sun.COM 
148910594SGeorge.Wilson@Sun.COM 			/*
149010594SGeorge.Wilson@Sun.COM 			 * Determine our vdev type, keeping in mind
149110594SGeorge.Wilson@Sun.COM 			 * that the srchval is composed of a type and
149210594SGeorge.Wilson@Sun.COM 			 * vdev id pair (i.e. mirror-4).
149310594SGeorge.Wilson@Sun.COM 			 */
149410594SGeorge.Wilson@Sun.COM 			if ((type = strdup(srchval)) == NULL)
149510594SGeorge.Wilson@Sun.COM 				return (NULL);
149610594SGeorge.Wilson@Sun.COM 
149710594SGeorge.Wilson@Sun.COM 			if ((p = strrchr(type, '-')) == NULL) {
149810594SGeorge.Wilson@Sun.COM 				free(type);
149910594SGeorge.Wilson@Sun.COM 				break;
150010594SGeorge.Wilson@Sun.COM 			}
150110594SGeorge.Wilson@Sun.COM 			idx = p + 1;
150210594SGeorge.Wilson@Sun.COM 			*p = '\0';
150310594SGeorge.Wilson@Sun.COM 
150410594SGeorge.Wilson@Sun.COM 			/*
150510594SGeorge.Wilson@Sun.COM 			 * If the types don't match then keep looking.
150610594SGeorge.Wilson@Sun.COM 			 */
150710594SGeorge.Wilson@Sun.COM 			if (strncmp(val, type, strlen(val)) != 0) {
150810594SGeorge.Wilson@Sun.COM 				free(type);
150910594SGeorge.Wilson@Sun.COM 				break;
151010594SGeorge.Wilson@Sun.COM 			}
151110594SGeorge.Wilson@Sun.COM 
151210594SGeorge.Wilson@Sun.COM 			verify(strncmp(type, VDEV_TYPE_RAIDZ,
151310594SGeorge.Wilson@Sun.COM 			    strlen(VDEV_TYPE_RAIDZ)) == 0 ||
151410594SGeorge.Wilson@Sun.COM 			    strncmp(type, VDEV_TYPE_MIRROR,
151510594SGeorge.Wilson@Sun.COM 			    strlen(VDEV_TYPE_MIRROR)) == 0);
151610594SGeorge.Wilson@Sun.COM 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
151710594SGeorge.Wilson@Sun.COM 			    &id) == 0);
151810594SGeorge.Wilson@Sun.COM 
151910594SGeorge.Wilson@Sun.COM 			errno = 0;
152010594SGeorge.Wilson@Sun.COM 			vdev_id = strtoull(idx, &end, 10);
152110594SGeorge.Wilson@Sun.COM 
152210594SGeorge.Wilson@Sun.COM 			free(type);
152310594SGeorge.Wilson@Sun.COM 			if (errno != 0)
152410594SGeorge.Wilson@Sun.COM 				return (NULL);
152510594SGeorge.Wilson@Sun.COM 
152610594SGeorge.Wilson@Sun.COM 			/*
152710594SGeorge.Wilson@Sun.COM 			 * Now verify that we have the correct vdev id.
152810594SGeorge.Wilson@Sun.COM 			 */
152910594SGeorge.Wilson@Sun.COM 			if (vdev_id == id)
153010594SGeorge.Wilson@Sun.COM 				return (nv);
15319816SGeorge.Wilson@Sun.COM 		}
15329816SGeorge.Wilson@Sun.COM 
15339816SGeorge.Wilson@Sun.COM 		/*
15349816SGeorge.Wilson@Sun.COM 		 * Common case
15359816SGeorge.Wilson@Sun.COM 		 */
15369816SGeorge.Wilson@Sun.COM 		if (strcmp(srchval, val) == 0)
15372082Seschrock 			return (nv);
15389816SGeorge.Wilson@Sun.COM 		break;
15399816SGeorge.Wilson@Sun.COM 	}
15409816SGeorge.Wilson@Sun.COM 
15419816SGeorge.Wilson@Sun.COM 	default:
15429816SGeorge.Wilson@Sun.COM 		break;
15431544Seschrock 	}
15441544Seschrock 
15451544Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
15461544Seschrock 	    &child, &children) != 0)
15472082Seschrock 		return (NULL);
15481544Seschrock 
15497326SEric.Schrock@Sun.COM 	for (c = 0; c < children; c++) {
15509816SGeorge.Wilson@Sun.COM 		if ((ret = vdev_to_nvlist_iter(child[c], search,
15517326SEric.Schrock@Sun.COM 		    avail_spare, l2cache, NULL)) != NULL) {
15527326SEric.Schrock@Sun.COM 			/*
15537326SEric.Schrock@Sun.COM 			 * The 'is_log' value is only set for the toplevel
15547326SEric.Schrock@Sun.COM 			 * vdev, not the leaf vdevs.  So we always lookup the
15557326SEric.Schrock@Sun.COM 			 * log device from the root of the vdev tree (where
15567326SEric.Schrock@Sun.COM 			 * 'log' is non-NULL).
15577326SEric.Schrock@Sun.COM 			 */
15587326SEric.Schrock@Sun.COM 			if (log != NULL &&
15597326SEric.Schrock@Sun.COM 			    nvlist_lookup_uint64(child[c],
15607326SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
15617326SEric.Schrock@Sun.COM 			    is_log) {
15627326SEric.Schrock@Sun.COM 				*log = B_TRUE;
15637326SEric.Schrock@Sun.COM 			}
15641544Seschrock 			return (ret);
15657326SEric.Schrock@Sun.COM 		}
15667326SEric.Schrock@Sun.COM 	}
15671544Seschrock 
15682082Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
15692082Seschrock 	    &child, &children) == 0) {
15702082Seschrock 		for (c = 0; c < children; c++) {
15719816SGeorge.Wilson@Sun.COM 			if ((ret = vdev_to_nvlist_iter(child[c], search,
15727326SEric.Schrock@Sun.COM 			    avail_spare, l2cache, NULL)) != NULL) {
15732468Sek110237 				*avail_spare = B_TRUE;
15742082Seschrock 				return (ret);
15752082Seschrock 			}
15762082Seschrock 		}
15772082Seschrock 	}
15782082Seschrock 
15795450Sbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
15805450Sbrendan 	    &child, &children) == 0) {
15815450Sbrendan 		for (c = 0; c < children; c++) {
15829816SGeorge.Wilson@Sun.COM 			if ((ret = vdev_to_nvlist_iter(child[c], search,
15837326SEric.Schrock@Sun.COM 			    avail_spare, l2cache, NULL)) != NULL) {
15845450Sbrendan 				*l2cache = B_TRUE;
15855450Sbrendan 				return (ret);
15865450Sbrendan 			}
15875450Sbrendan 		}
15885450Sbrendan 	}
15895450Sbrendan 
15902082Seschrock 	return (NULL);
15911544Seschrock }
15921544Seschrock 
15939816SGeorge.Wilson@Sun.COM /*
15949816SGeorge.Wilson@Sun.COM  * Given a physical path (minus the "/devices" prefix), find the
15959816SGeorge.Wilson@Sun.COM  * associated vdev.
15969816SGeorge.Wilson@Sun.COM  */
15979816SGeorge.Wilson@Sun.COM nvlist_t *
15989816SGeorge.Wilson@Sun.COM zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
15999816SGeorge.Wilson@Sun.COM     boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
16009816SGeorge.Wilson@Sun.COM {
16019816SGeorge.Wilson@Sun.COM 	nvlist_t *search, *nvroot, *ret;
16029816SGeorge.Wilson@Sun.COM 
16039816SGeorge.Wilson@Sun.COM 	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
16049816SGeorge.Wilson@Sun.COM 	verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
16059816SGeorge.Wilson@Sun.COM 
16069816SGeorge.Wilson@Sun.COM 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
16079816SGeorge.Wilson@Sun.COM 	    &nvroot) == 0);
16089816SGeorge.Wilson@Sun.COM 
16099816SGeorge.Wilson@Sun.COM 	*avail_spare = B_FALSE;
16109816SGeorge.Wilson@Sun.COM 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
16119816SGeorge.Wilson@Sun.COM 	nvlist_free(search);
16129816SGeorge.Wilson@Sun.COM 
16139816SGeorge.Wilson@Sun.COM 	return (ret);
16149816SGeorge.Wilson@Sun.COM }
16159816SGeorge.Wilson@Sun.COM 
161610594SGeorge.Wilson@Sun.COM /*
161710594SGeorge.Wilson@Sun.COM  * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
161810594SGeorge.Wilson@Sun.COM  */
161910594SGeorge.Wilson@Sun.COM boolean_t
162010594SGeorge.Wilson@Sun.COM zpool_vdev_is_interior(const char *name)
162110594SGeorge.Wilson@Sun.COM {
162210594SGeorge.Wilson@Sun.COM 	if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
162310594SGeorge.Wilson@Sun.COM 	    strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
162410594SGeorge.Wilson@Sun.COM 		return (B_TRUE);
162510594SGeorge.Wilson@Sun.COM 	return (B_FALSE);
162610594SGeorge.Wilson@Sun.COM }
162710594SGeorge.Wilson@Sun.COM 
16282082Seschrock nvlist_t *
16295450Sbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
16307326SEric.Schrock@Sun.COM     boolean_t *l2cache, boolean_t *log)
16311544Seschrock {
16321544Seschrock 	char buf[MAXPATHLEN];
16331544Seschrock 	char *end;
16349816SGeorge.Wilson@Sun.COM 	nvlist_t *nvroot, *search, *ret;
16351544Seschrock 	uint64_t guid;
16361544Seschrock 
16379816SGeorge.Wilson@Sun.COM 	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
16389816SGeorge.Wilson@Sun.COM 
16391613Seschrock 	guid = strtoull(path, &end, 10);
16401544Seschrock 	if (guid != 0 && *end == '\0') {
16419816SGeorge.Wilson@Sun.COM 		verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
164210594SGeorge.Wilson@Sun.COM 	} else if (zpool_vdev_is_interior(path)) {
164310594SGeorge.Wilson@Sun.COM 		verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
16441544Seschrock 	} else if (path[0] != '/') {
16451544Seschrock 		(void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path);
16469816SGeorge.Wilson@Sun.COM 		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
16471544Seschrock 	} else {
16489816SGeorge.Wilson@Sun.COM 		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
16491544Seschrock 	}
16501544Seschrock 
16511544Seschrock 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
16521544Seschrock 	    &nvroot) == 0);
16531544Seschrock 
16542468Sek110237 	*avail_spare = B_FALSE;
16555450Sbrendan 	*l2cache = B_FALSE;
16567326SEric.Schrock@Sun.COM 	if (log != NULL)
16577326SEric.Schrock@Sun.COM 		*log = B_FALSE;
16589816SGeorge.Wilson@Sun.COM 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
16599816SGeorge.Wilson@Sun.COM 	nvlist_free(search);
16609816SGeorge.Wilson@Sun.COM 
16619816SGeorge.Wilson@Sun.COM 	return (ret);
16622468Sek110237 }
16632468Sek110237 
16647656SSherry.Moore@Sun.COM static int
16657656SSherry.Moore@Sun.COM vdev_online(nvlist_t *nv)
16667656SSherry.Moore@Sun.COM {
16677656SSherry.Moore@Sun.COM 	uint64_t ival;
16687656SSherry.Moore@Sun.COM 
16697656SSherry.Moore@Sun.COM 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
16707656SSherry.Moore@Sun.COM 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
16717656SSherry.Moore@Sun.COM 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
16727656SSherry.Moore@Sun.COM 		return (0);
16737656SSherry.Moore@Sun.COM 
16747656SSherry.Moore@Sun.COM 	return (1);
16757656SSherry.Moore@Sun.COM }
16767656SSherry.Moore@Sun.COM 
16777656SSherry.Moore@Sun.COM /*
16789790SLin.Ling@Sun.COM  * Helper function for zpool_get_physpaths().
16797656SSherry.Moore@Sun.COM  */
16809160SSherry.Moore@Sun.COM static int
16819790SLin.Ling@Sun.COM vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
16829160SSherry.Moore@Sun.COM     size_t *bytes_written)
16837656SSherry.Moore@Sun.COM {
16849160SSherry.Moore@Sun.COM 	size_t bytes_left, pos, rsz;
16859160SSherry.Moore@Sun.COM 	char *tmppath;
16869160SSherry.Moore@Sun.COM 	const char *format;
16879160SSherry.Moore@Sun.COM 
16889160SSherry.Moore@Sun.COM 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
16899160SSherry.Moore@Sun.COM 	    &tmppath) != 0)
16909160SSherry.Moore@Sun.COM 		return (EZFS_NODEVICE);
16919160SSherry.Moore@Sun.COM 
16929160SSherry.Moore@Sun.COM 	pos = *bytes_written;
16939160SSherry.Moore@Sun.COM 	bytes_left = physpath_size - pos;
16949160SSherry.Moore@Sun.COM 	format = (pos == 0) ? "%s" : " %s";
16959160SSherry.Moore@Sun.COM 
16969160SSherry.Moore@Sun.COM 	rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
16979160SSherry.Moore@Sun.COM 	*bytes_written += rsz;
16989160SSherry.Moore@Sun.COM 
16999160SSherry.Moore@Sun.COM 	if (rsz >= bytes_left) {
17009160SSherry.Moore@Sun.COM 		/* if physpath was not copied properly, clear it */
17019160SSherry.Moore@Sun.COM 		if (bytes_left != 0) {
17029160SSherry.Moore@Sun.COM 			physpath[pos] = 0;
17039160SSherry.Moore@Sun.COM 		}
17049160SSherry.Moore@Sun.COM 		return (EZFS_NOSPC);
17059160SSherry.Moore@Sun.COM 	}
17069160SSherry.Moore@Sun.COM 	return (0);
17079160SSherry.Moore@Sun.COM }
17089160SSherry.Moore@Sun.COM 
17099790SLin.Ling@Sun.COM static int
17109790SLin.Ling@Sun.COM vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
17119790SLin.Ling@Sun.COM     size_t *rsz, boolean_t is_spare)
17129790SLin.Ling@Sun.COM {
17139790SLin.Ling@Sun.COM 	char *type;
17149790SLin.Ling@Sun.COM 	int ret;
17159790SLin.Ling@Sun.COM 
17169790SLin.Ling@Sun.COM 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
17179790SLin.Ling@Sun.COM 		return (EZFS_INVALCONFIG);
17189790SLin.Ling@Sun.COM 
17199790SLin.Ling@Sun.COM 	if (strcmp(type, VDEV_TYPE_DISK) == 0) {
17209790SLin.Ling@Sun.COM 		/*
17219790SLin.Ling@Sun.COM 		 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
17229790SLin.Ling@Sun.COM 		 * For a spare vdev, we only want to boot from the active
17239790SLin.Ling@Sun.COM 		 * spare device.
17249790SLin.Ling@Sun.COM 		 */
17259790SLin.Ling@Sun.COM 		if (is_spare) {
17269790SLin.Ling@Sun.COM 			uint64_t spare = 0;
17279790SLin.Ling@Sun.COM 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
17289790SLin.Ling@Sun.COM 			    &spare);
17299790SLin.Ling@Sun.COM 			if (!spare)
17309790SLin.Ling@Sun.COM 				return (EZFS_INVALCONFIG);
17319790SLin.Ling@Sun.COM 		}
17329790SLin.Ling@Sun.COM 
17339790SLin.Ling@Sun.COM 		if (vdev_online(nv)) {
17349790SLin.Ling@Sun.COM 			if ((ret = vdev_get_one_physpath(nv, physpath,
17359790SLin.Ling@Sun.COM 			    phypath_size, rsz)) != 0)
17369790SLin.Ling@Sun.COM 				return (ret);
17379790SLin.Ling@Sun.COM 		}
17389790SLin.Ling@Sun.COM 	} else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
17399790SLin.Ling@Sun.COM 	    strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
17409790SLin.Ling@Sun.COM 	    (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
17419790SLin.Ling@Sun.COM 		nvlist_t **child;
17429790SLin.Ling@Sun.COM 		uint_t count;
17439790SLin.Ling@Sun.COM 		int i, ret;
17449790SLin.Ling@Sun.COM 
17459790SLin.Ling@Sun.COM 		if (nvlist_lookup_nvlist_array(nv,
17469790SLin.Ling@Sun.COM 		    ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
17479790SLin.Ling@Sun.COM 			return (EZFS_INVALCONFIG);
17489790SLin.Ling@Sun.COM 
17499790SLin.Ling@Sun.COM 		for (i = 0; i < count; i++) {
17509790SLin.Ling@Sun.COM 			ret = vdev_get_physpaths(child[i], physpath,
17519790SLin.Ling@Sun.COM 			    phypath_size, rsz, is_spare);
17529790SLin.Ling@Sun.COM 			if (ret == EZFS_NOSPC)
17539790SLin.Ling@Sun.COM 				return (ret);
17549790SLin.Ling@Sun.COM 		}
17559790SLin.Ling@Sun.COM 	}
17569790SLin.Ling@Sun.COM 
17579790SLin.Ling@Sun.COM 	return (EZFS_POOL_INVALARG);
17589790SLin.Ling@Sun.COM }
17599790SLin.Ling@Sun.COM 
17609160SSherry.Moore@Sun.COM /*
17619160SSherry.Moore@Sun.COM  * Get phys_path for a root pool config.
17629160SSherry.Moore@Sun.COM  * Return 0 on success; non-zero on failure.
17639160SSherry.Moore@Sun.COM  */
17649160SSherry.Moore@Sun.COM static int
17659160SSherry.Moore@Sun.COM zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
17669160SSherry.Moore@Sun.COM {
17679160SSherry.Moore@Sun.COM 	size_t rsz;
17687656SSherry.Moore@Sun.COM 	nvlist_t *vdev_root;
17697656SSherry.Moore@Sun.COM 	nvlist_t **child;
17707656SSherry.Moore@Sun.COM 	uint_t count;
17719160SSherry.Moore@Sun.COM 	char *type;
17729160SSherry.Moore@Sun.COM 
17739160SSherry.Moore@Sun.COM 	rsz = 0;
17749160SSherry.Moore@Sun.COM 
17759160SSherry.Moore@Sun.COM 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
17769160SSherry.Moore@Sun.COM 	    &vdev_root) != 0)
17779160SSherry.Moore@Sun.COM 		return (EZFS_INVALCONFIG);
17789160SSherry.Moore@Sun.COM 
17799160SSherry.Moore@Sun.COM 	if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
17809160SSherry.Moore@Sun.COM 	    nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
17819160SSherry.Moore@Sun.COM 	    &child, &count) != 0)
17829160SSherry.Moore@Sun.COM 		return (EZFS_INVALCONFIG);
17837656SSherry.Moore@Sun.COM 
17847656SSherry.Moore@Sun.COM 	/*
17859160SSherry.Moore@Sun.COM 	 * root pool can not have EFI labeled disks and can only have
17869160SSherry.Moore@Sun.COM 	 * a single top-level vdev.
17877656SSherry.Moore@Sun.COM 	 */
17889160SSherry.Moore@Sun.COM 	if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
17899160SSherry.Moore@Sun.COM 	    pool_uses_efi(vdev_root))
17909160SSherry.Moore@Sun.COM 		return (EZFS_POOL_INVALARG);
17919160SSherry.Moore@Sun.COM 
17929790SLin.Ling@Sun.COM 	(void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
17939790SLin.Ling@Sun.COM 	    B_FALSE);
17947656SSherry.Moore@Sun.COM 
17959160SSherry.Moore@Sun.COM 	/* No online devices */
17969160SSherry.Moore@Sun.COM 	if (rsz == 0)
17979160SSherry.Moore@Sun.COM 		return (EZFS_NODEVICE);
17989160SSherry.Moore@Sun.COM 
17997656SSherry.Moore@Sun.COM 	return (0);
18007656SSherry.Moore@Sun.COM }
18017656SSherry.Moore@Sun.COM 
18022468Sek110237 /*
18039160SSherry.Moore@Sun.COM  * Get phys_path for a root pool
18049160SSherry.Moore@Sun.COM  * Return 0 on success; non-zero on failure.
18059160SSherry.Moore@Sun.COM  */
18069160SSherry.Moore@Sun.COM int
18079160SSherry.Moore@Sun.COM zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
18089160SSherry.Moore@Sun.COM {
18099160SSherry.Moore@Sun.COM 	return (zpool_get_config_physpath(zhp->zpool_config, physpath,
18109160SSherry.Moore@Sun.COM 	    phypath_size));
18119160SSherry.Moore@Sun.COM }
18129160SSherry.Moore@Sun.COM 
18139160SSherry.Moore@Sun.COM /*
18149816SGeorge.Wilson@Sun.COM  * If the device has being dynamically expanded then we need to relabel
18159816SGeorge.Wilson@Sun.COM  * the disk to use the new unallocated space.
18169816SGeorge.Wilson@Sun.COM  */
18179816SGeorge.Wilson@Sun.COM static int
18189816SGeorge.Wilson@Sun.COM zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
18199816SGeorge.Wilson@Sun.COM {
18209816SGeorge.Wilson@Sun.COM 	char path[MAXPATHLEN];
18219816SGeorge.Wilson@Sun.COM 	char errbuf[1024];
18229816SGeorge.Wilson@Sun.COM 	int fd, error;
18239816SGeorge.Wilson@Sun.COM 	int (*_efi_use_whole_disk)(int);
18249816SGeorge.Wilson@Sun.COM 
18259816SGeorge.Wilson@Sun.COM 	if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
18269816SGeorge.Wilson@Sun.COM 	    "efi_use_whole_disk")) == NULL)
18279816SGeorge.Wilson@Sun.COM 		return (-1);
18289816SGeorge.Wilson@Sun.COM 
18299816SGeorge.Wilson@Sun.COM 	(void) snprintf(path, sizeof (path), "%s/%s", RDISK_ROOT, name);
18309816SGeorge.Wilson@Sun.COM 
18319816SGeorge.Wilson@Sun.COM 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
18329816SGeorge.Wilson@Sun.COM 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
18339816SGeorge.Wilson@Sun.COM 		    "relabel '%s': unable to open device"), name);
18349816SGeorge.Wilson@Sun.COM 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
18359816SGeorge.Wilson@Sun.COM 	}
18369816SGeorge.Wilson@Sun.COM 
18379816SGeorge.Wilson@Sun.COM 	/*
18389816SGeorge.Wilson@Sun.COM 	 * It's possible that we might encounter an error if the device
18399816SGeorge.Wilson@Sun.COM 	 * does not have any unallocated space left. If so, we simply
18409816SGeorge.Wilson@Sun.COM 	 * ignore that error and continue on.
18419816SGeorge.Wilson@Sun.COM 	 */
18429816SGeorge.Wilson@Sun.COM 	error = _efi_use_whole_disk(fd);
18439816SGeorge.Wilson@Sun.COM 	(void) close(fd);
18449816SGeorge.Wilson@Sun.COM 	if (error && error != VT_ENOSPC) {
18459816SGeorge.Wilson@Sun.COM 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
18469816SGeorge.Wilson@Sun.COM 		    "relabel '%s': unable to read disk capacity"), name);
18479816SGeorge.Wilson@Sun.COM 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
18489816SGeorge.Wilson@Sun.COM 	}
18499816SGeorge.Wilson@Sun.COM 	return (0);
18509816SGeorge.Wilson@Sun.COM }
18519816SGeorge.Wilson@Sun.COM 
18529816SGeorge.Wilson@Sun.COM /*
18534451Seschrock  * Bring the specified vdev online.   The 'flags' parameter is a set of the
18544451Seschrock  * ZFS_ONLINE_* flags.
1855789Sahrens  */
1856789Sahrens int
18574451Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
18584451Seschrock     vdev_state_t *newstate)
1859789Sahrens {
1860789Sahrens 	zfs_cmd_t zc = { 0 };
1861789Sahrens 	char msg[1024];
18622082Seschrock 	nvlist_t *tgt;
18639816SGeorge.Wilson@Sun.COM 	boolean_t avail_spare, l2cache, islog;
18642082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1865789Sahrens 
18669816SGeorge.Wilson@Sun.COM 	if (flags & ZFS_ONLINE_EXPAND) {
18679816SGeorge.Wilson@Sun.COM 		(void) snprintf(msg, sizeof (msg),
18689816SGeorge.Wilson@Sun.COM 		    dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
18699816SGeorge.Wilson@Sun.COM 	} else {
18709816SGeorge.Wilson@Sun.COM 		(void) snprintf(msg, sizeof (msg),
18719816SGeorge.Wilson@Sun.COM 		    dgettext(TEXT_DOMAIN, "cannot online %s"), path);
18729816SGeorge.Wilson@Sun.COM 	}
1873789Sahrens 
18741544Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
18757326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
18769816SGeorge.Wilson@Sun.COM 	    &islog)) == NULL)
18772082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
1878789Sahrens 
18792468Sek110237 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
18802468Sek110237 
1881*10817SEric.Schrock@Sun.COM 	if (avail_spare)
18822082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
18832082Seschrock 
18849816SGeorge.Wilson@Sun.COM 	if (flags & ZFS_ONLINE_EXPAND ||
18859816SGeorge.Wilson@Sun.COM 	    zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
18869816SGeorge.Wilson@Sun.COM 		char *pathname = NULL;
18879816SGeorge.Wilson@Sun.COM 		uint64_t wholedisk = 0;
18889816SGeorge.Wilson@Sun.COM 
18899816SGeorge.Wilson@Sun.COM 		(void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
18909816SGeorge.Wilson@Sun.COM 		    &wholedisk);
18919816SGeorge.Wilson@Sun.COM 		verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
18929816SGeorge.Wilson@Sun.COM 		    &pathname) == 0);
18939816SGeorge.Wilson@Sun.COM 
18949816SGeorge.Wilson@Sun.COM 		/*
18959816SGeorge.Wilson@Sun.COM 		 * XXX - L2ARC 1.0 devices can't support expansion.
18969816SGeorge.Wilson@Sun.COM 		 */
18979816SGeorge.Wilson@Sun.COM 		if (l2cache) {
18989816SGeorge.Wilson@Sun.COM 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18999816SGeorge.Wilson@Sun.COM 			    "cannot expand cache devices"));
19009816SGeorge.Wilson@Sun.COM 			return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
19019816SGeorge.Wilson@Sun.COM 		}
19029816SGeorge.Wilson@Sun.COM 
19039816SGeorge.Wilson@Sun.COM 		if (wholedisk) {
19049816SGeorge.Wilson@Sun.COM 			pathname += strlen(DISK_ROOT) + 1;
19059816SGeorge.Wilson@Sun.COM 			(void) zpool_relabel_disk(zhp->zpool_hdl, pathname);
19069816SGeorge.Wilson@Sun.COM 		}
19079816SGeorge.Wilson@Sun.COM 	}
19089816SGeorge.Wilson@Sun.COM 
19094451Seschrock 	zc.zc_cookie = VDEV_STATE_ONLINE;
19104451Seschrock 	zc.zc_obj = flags;
19114451Seschrock 
19124543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0)
19134451Seschrock 		return (zpool_standard_error(hdl, errno, msg));
19144451Seschrock 
19154451Seschrock 	*newstate = zc.zc_cookie;
19164451Seschrock 	return (0);
1917789Sahrens }
1918789Sahrens 
1919789Sahrens /*
1920789Sahrens  * Take the specified vdev offline
1921789Sahrens  */
1922789Sahrens int
19234451Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
1924789Sahrens {
1925789Sahrens 	zfs_cmd_t zc = { 0 };
1926789Sahrens 	char msg[1024];
19272082Seschrock 	nvlist_t *tgt;
19285450Sbrendan 	boolean_t avail_spare, l2cache;
19292082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1930789Sahrens 
19311544Seschrock 	(void) snprintf(msg, sizeof (msg),
19321544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
19331544Seschrock 
1934789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
19357326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
19367326SEric.Schrock@Sun.COM 	    NULL)) == NULL)
19372082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
19382082Seschrock 
19392468Sek110237 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
19402468Sek110237 
1941*10817SEric.Schrock@Sun.COM 	if (avail_spare)
19422082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
19432082Seschrock 
19444451Seschrock 	zc.zc_cookie = VDEV_STATE_OFFLINE;
19454451Seschrock 	zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
19461485Slling 
19474543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
1948789Sahrens 		return (0);
1949789Sahrens 
1950789Sahrens 	switch (errno) {
19512082Seschrock 	case EBUSY:
1952789Sahrens 
1953789Sahrens 		/*
1954789Sahrens 		 * There are no other replicas of this device.
1955789Sahrens 		 */
19562082Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
19572082Seschrock 
19589701SGeorge.Wilson@Sun.COM 	case EEXIST:
19599701SGeorge.Wilson@Sun.COM 		/*
19609701SGeorge.Wilson@Sun.COM 		 * The log device has unplayed logs
19619701SGeorge.Wilson@Sun.COM 		 */
19629701SGeorge.Wilson@Sun.COM 		return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
19639701SGeorge.Wilson@Sun.COM 
19642082Seschrock 	default:
19652082Seschrock 		return (zpool_standard_error(hdl, errno, msg));
19662082Seschrock 	}
19672082Seschrock }
1968789Sahrens 
19692082Seschrock /*
19704451Seschrock  * Mark the given vdev faulted.
19714451Seschrock  */
19724451Seschrock int
1973*10817SEric.Schrock@Sun.COM zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
19744451Seschrock {
19754451Seschrock 	zfs_cmd_t zc = { 0 };
19764451Seschrock 	char msg[1024];
19774451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
19784451Seschrock 
19794451Seschrock 	(void) snprintf(msg, sizeof (msg),
19804451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
19814451Seschrock 
19824451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
19834451Seschrock 	zc.zc_guid = guid;
19844451Seschrock 	zc.zc_cookie = VDEV_STATE_FAULTED;
1985*10817SEric.Schrock@Sun.COM 	zc.zc_obj = aux;
19864451Seschrock 
19874451Seschrock 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
19884451Seschrock 		return (0);
19894451Seschrock 
19904451Seschrock 	switch (errno) {
19914451Seschrock 	case EBUSY:
19924451Seschrock 
19934451Seschrock 		/*
19944451Seschrock 		 * There are no other replicas of this device.
19954451Seschrock 		 */
19964451Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
19974451Seschrock 
19984451Seschrock 	default:
19994451Seschrock 		return (zpool_standard_error(hdl, errno, msg));
20004451Seschrock 	}
20014451Seschrock 
20024451Seschrock }
20034451Seschrock 
20044451Seschrock /*
20054451Seschrock  * Mark the given vdev degraded.
20064451Seschrock  */
20074451Seschrock int
2008*10817SEric.Schrock@Sun.COM zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
20094451Seschrock {
20104451Seschrock 	zfs_cmd_t zc = { 0 };
20114451Seschrock 	char msg[1024];
20124451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
20134451Seschrock 
20144451Seschrock 	(void) snprintf(msg, sizeof (msg),
20154451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
20164451Seschrock 
20174451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
20184451Seschrock 	zc.zc_guid = guid;
20194451Seschrock 	zc.zc_cookie = VDEV_STATE_DEGRADED;
2020*10817SEric.Schrock@Sun.COM 	zc.zc_obj = aux;
20214451Seschrock 
20224451Seschrock 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
20234451Seschrock 		return (0);
20244451Seschrock 
20254451Seschrock 	return (zpool_standard_error(hdl, errno, msg));
20264451Seschrock }
20274451Seschrock 
20284451Seschrock /*
20292082Seschrock  * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
20302082Seschrock  * a hot spare.
20312082Seschrock  */
20322082Seschrock static boolean_t
20332082Seschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
20342082Seschrock {
20352082Seschrock 	nvlist_t **child;
20362082Seschrock 	uint_t c, children;
20372082Seschrock 	char *type;
20382082Seschrock 
20392082Seschrock 	if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
20402082Seschrock 	    &children) == 0) {
20412082Seschrock 		verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
20422082Seschrock 		    &type) == 0);
20432082Seschrock 
20442082Seschrock 		if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
20452082Seschrock 		    children == 2 && child[which] == tgt)
20462082Seschrock 			return (B_TRUE);
20472082Seschrock 
20482082Seschrock 		for (c = 0; c < children; c++)
20492082Seschrock 			if (is_replacing_spare(child[c], tgt, which))
20502082Seschrock 				return (B_TRUE);
2051789Sahrens 	}
20522082Seschrock 
20532082Seschrock 	return (B_FALSE);
2054789Sahrens }
2055789Sahrens 
2056789Sahrens /*
2057789Sahrens  * Attach new_disk (fully described by nvroot) to old_disk.
20584527Sperrin  * If 'replacing' is specified, the new disk will replace the old one.
2059789Sahrens  */
2060789Sahrens int
2061789Sahrens zpool_vdev_attach(zpool_handle_t *zhp,
2062789Sahrens     const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2063789Sahrens {
2064789Sahrens 	zfs_cmd_t zc = { 0 };
2065789Sahrens 	char msg[1024];
2066789Sahrens 	int ret;
20672082Seschrock 	nvlist_t *tgt;
20687326SEric.Schrock@Sun.COM 	boolean_t avail_spare, l2cache, islog;
20697326SEric.Schrock@Sun.COM 	uint64_t val;
20707041Seschrock 	char *path, *newname;
20712082Seschrock 	nvlist_t **child;
20722082Seschrock 	uint_t children;
20732082Seschrock 	nvlist_t *config_root;
20742082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
20757965SGeorge.Wilson@Sun.COM 	boolean_t rootpool = pool_is_bootable(zhp);
2076789Sahrens 
20771544Seschrock 	if (replacing)
20781544Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
20791544Seschrock 		    "cannot replace %s with %s"), old_disk, new_disk);
20801544Seschrock 	else
20811544Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
20821544Seschrock 		    "cannot attach %s to %s"), new_disk, old_disk);
20831544Seschrock 
20847965SGeorge.Wilson@Sun.COM 	/*
20857965SGeorge.Wilson@Sun.COM 	 * If this is a root pool, make sure that we're not attaching an
20867965SGeorge.Wilson@Sun.COM 	 * EFI labeled device.
20877965SGeorge.Wilson@Sun.COM 	 */
20887965SGeorge.Wilson@Sun.COM 	if (rootpool && pool_uses_efi(nvroot)) {
20897965SGeorge.Wilson@Sun.COM 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
20907965SGeorge.Wilson@Sun.COM 		    "EFI labeled devices are not supported on root pools."));
20917965SGeorge.Wilson@Sun.COM 		return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
20927965SGeorge.Wilson@Sun.COM 	}
20937965SGeorge.Wilson@Sun.COM 
2094789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
20957326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
20967326SEric.Schrock@Sun.COM 	    &islog)) == 0)
20972082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
20982082Seschrock 
20992468Sek110237 	if (avail_spare)
21002082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
21012082Seschrock 
21025450Sbrendan 	if (l2cache)
21035450Sbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
21045450Sbrendan 
21052082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
21062082Seschrock 	zc.zc_cookie = replacing;
21072082Seschrock 
21082082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
21092082Seschrock 	    &child, &children) != 0 || children != 1) {
21102082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
21112082Seschrock 		    "new device must be a single disk"));
21122082Seschrock 		return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
21131544Seschrock 	}
21142082Seschrock 
21152082Seschrock 	verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
21162082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
21172082Seschrock 
211810594SGeorge.Wilson@Sun.COM 	if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
21197041Seschrock 		return (-1);
21207041Seschrock 
21212082Seschrock 	/*
21222082Seschrock 	 * If the target is a hot spare that has been swapped in, we can only
21232082Seschrock 	 * replace it with another hot spare.
21242082Seschrock 	 */
21252082Seschrock 	if (replacing &&
21262082Seschrock 	    nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
21277326SEric.Schrock@Sun.COM 	    (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
21287326SEric.Schrock@Sun.COM 	    NULL) == NULL || !avail_spare) &&
21297326SEric.Schrock@Sun.COM 	    is_replacing_spare(config_root, tgt, 1)) {
21302082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
21312082Seschrock 		    "can only be replaced by another hot spare"));
21327041Seschrock 		free(newname);
21332082Seschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
21342082Seschrock 	}
21352082Seschrock 
21362082Seschrock 	/*
21372082Seschrock 	 * If we are attempting to replace a spare, it canot be applied to an
21382082Seschrock 	 * already spared device.
21392082Seschrock 	 */
21402082Seschrock 	if (replacing &&
21412082Seschrock 	    nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 &&
21427326SEric.Schrock@Sun.COM 	    zpool_find_vdev(zhp, newname, &avail_spare,
21437326SEric.Schrock@Sun.COM 	    &l2cache, NULL) != NULL && avail_spare &&
21447326SEric.Schrock@Sun.COM 	    is_replacing_spare(config_root, tgt, 0)) {
21452082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
21462082Seschrock 		    "device has already been replaced with a spare"));
21477041Seschrock 		free(newname);
21482082Seschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
21492082Seschrock 	}
2150789Sahrens 
21517041Seschrock 	free(newname);
21527041Seschrock 
21535094Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
21542082Seschrock 		return (-1);
2155789Sahrens 
21564543Smarks 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2157789Sahrens 
21582676Seschrock 	zcmd_free_nvlists(&zc);
2159789Sahrens 
21607965SGeorge.Wilson@Sun.COM 	if (ret == 0) {
21617965SGeorge.Wilson@Sun.COM 		if (rootpool) {
21627965SGeorge.Wilson@Sun.COM 			/*
21637965SGeorge.Wilson@Sun.COM 			 * XXX - This should be removed once we can
21647965SGeorge.Wilson@Sun.COM 			 * automatically install the bootblocks on the
21657965SGeorge.Wilson@Sun.COM 			 * newly attached disk.
21667965SGeorge.Wilson@Sun.COM 			 */
21677965SGeorge.Wilson@Sun.COM 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Please "
21687965SGeorge.Wilson@Sun.COM 			    "be sure to invoke %s to make '%s' bootable.\n"),
21697965SGeorge.Wilson@Sun.COM 			    BOOTCMD, new_disk);
21709790SLin.Ling@Sun.COM 
21719790SLin.Ling@Sun.COM 			/*
21729790SLin.Ling@Sun.COM 			 * XXX need a better way to prevent user from
21739790SLin.Ling@Sun.COM 			 * booting up a half-baked vdev.
21749790SLin.Ling@Sun.COM 			 */
21759790SLin.Ling@Sun.COM 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
21769790SLin.Ling@Sun.COM 			    "sure to wait until resilver is done "
21779790SLin.Ling@Sun.COM 			    "before rebooting.\n"));
21787965SGeorge.Wilson@Sun.COM 		}
2179789Sahrens 		return (0);
21807965SGeorge.Wilson@Sun.COM 	}
2181789Sahrens 
2182789Sahrens 	switch (errno) {
21831544Seschrock 	case ENOTSUP:
2184789Sahrens 		/*
2185789Sahrens 		 * Can't attach to or replace this type of vdev.
2186789Sahrens 		 */
21874527Sperrin 		if (replacing) {
21887326SEric.Schrock@Sun.COM 			if (islog)
21894527Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
21904527Sperrin 				    "cannot replace a log with a spare"));
21914527Sperrin 			else
21924527Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
21934527Sperrin 				    "cannot replace a replacing device"));
21944527Sperrin 		} else {
21952082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
21962082Seschrock 			    "can only attach to mirrors and top-level "
21972082Seschrock 			    "disks"));
21984527Sperrin 		}
21992082Seschrock 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
2200789Sahrens 		break;
2201789Sahrens 
22021544Seschrock 	case EINVAL:
2203789Sahrens 		/*
2204789Sahrens 		 * The new device must be a single disk.
2205789Sahrens 		 */
22062082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
22072082Seschrock 		    "new device must be a single disk"));
22082082Seschrock 		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2209789Sahrens 		break;
2210789Sahrens 
22111544Seschrock 	case EBUSY:
22122082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
22132082Seschrock 		    new_disk);
22142082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2215789Sahrens 		break;
2216789Sahrens 
22171544Seschrock 	case EOVERFLOW:
2218789Sahrens 		/*
2219789Sahrens 		 * The new device is too small.
2220789Sahrens 		 */
22212082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
22222082Seschrock 		    "device is too small"));
22232082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2224789Sahrens 		break;
2225789Sahrens 
22261544Seschrock 	case EDOM:
2227789Sahrens 		/*
2228789Sahrens 		 * The new device has a different alignment requirement.
2229789Sahrens 		 */
22302082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
22312082Seschrock 		    "devices have different sector alignment"));
22322082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2233789Sahrens 		break;
2234789Sahrens 
22351544Seschrock 	case ENAMETOOLONG:
2236789Sahrens 		/*
2237789Sahrens 		 * The resulting top-level vdev spec won't fit in the label.
2238789Sahrens 		 */
22392082Seschrock 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2240789Sahrens 		break;
2241789Sahrens 
22421544Seschrock 	default:
22432082Seschrock 		(void) zpool_standard_error(hdl, errno, msg);
2244789Sahrens 	}
2245789Sahrens 
22462082Seschrock 	return (-1);
2247789Sahrens }
2248789Sahrens 
2249789Sahrens /*
2250789Sahrens  * Detach the specified device.
2251789Sahrens  */
2252789Sahrens int
2253789Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2254789Sahrens {
2255789Sahrens 	zfs_cmd_t zc = { 0 };
2256789Sahrens 	char msg[1024];
22572082Seschrock 	nvlist_t *tgt;
22585450Sbrendan 	boolean_t avail_spare, l2cache;
22592082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2260789Sahrens 
22611544Seschrock 	(void) snprintf(msg, sizeof (msg),
22621544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
22631544Seschrock 
2264789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
22657326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
22667326SEric.Schrock@Sun.COM 	    NULL)) == 0)
22672082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2268789Sahrens 
22692468Sek110237 	if (avail_spare)
22702082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
22712082Seschrock 
22725450Sbrendan 	if (l2cache)
22735450Sbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
22745450Sbrendan 
22752082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
22762082Seschrock 
22774543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2278789Sahrens 		return (0);
2279789Sahrens 
2280789Sahrens 	switch (errno) {
2281789Sahrens 
22821544Seschrock 	case ENOTSUP:
2283789Sahrens 		/*
2284789Sahrens 		 * Can't detach from this type of vdev.
2285789Sahrens 		 */
22862082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
22872082Seschrock 		    "applicable to mirror and replacing vdevs"));
22882082Seschrock 		(void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg);
2289789Sahrens 		break;
2290789Sahrens 
22911544Seschrock 	case EBUSY:
2292789Sahrens 		/*
2293789Sahrens 		 * There are no other replicas of this device.
2294789Sahrens 		 */
22952082Seschrock 		(void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2296789Sahrens 		break;
2297789Sahrens 
22981544Seschrock 	default:
22992082Seschrock 		(void) zpool_standard_error(hdl, errno, msg);
23001544Seschrock 	}
23011544Seschrock 
23022082Seschrock 	return (-1);
23032082Seschrock }
23042082Seschrock 
23052082Seschrock /*
23065450Sbrendan  * Remove the given device.  Currently, this is supported only for hot spares
23075450Sbrendan  * and level 2 cache devices.
23082082Seschrock  */
23092082Seschrock int
23102082Seschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
23112082Seschrock {
23122082Seschrock 	zfs_cmd_t zc = { 0 };
23132082Seschrock 	char msg[1024];
23142082Seschrock 	nvlist_t *tgt;
231510594SGeorge.Wilson@Sun.COM 	boolean_t avail_spare, l2cache, islog;
23162082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
231710594SGeorge.Wilson@Sun.COM 	uint64_t version;
23182082Seschrock 
23192082Seschrock 	(void) snprintf(msg, sizeof (msg),
23202082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
23212082Seschrock 
23222082Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
23237326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
232410594SGeorge.Wilson@Sun.COM 	    &islog)) == 0)
23252082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
232610594SGeorge.Wilson@Sun.COM 	/*
232710594SGeorge.Wilson@Sun.COM 	 * XXX - this should just go away.
232810594SGeorge.Wilson@Sun.COM 	 */
232910594SGeorge.Wilson@Sun.COM 	if (!avail_spare && !l2cache && !islog) {
23302082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
233110594SGeorge.Wilson@Sun.COM 		    "only inactive hot spares, cache, top-level, "
233210594SGeorge.Wilson@Sun.COM 		    "or log devices can be removed"));
23332082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
23342082Seschrock 	}
23352082Seschrock 
233610594SGeorge.Wilson@Sun.COM 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
233710594SGeorge.Wilson@Sun.COM 	if (islog && version < SPA_VERSION_HOLES) {
233810594SGeorge.Wilson@Sun.COM 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
233910594SGeorge.Wilson@Sun.COM 		    "pool must be upgrade to support log removal"));
234010594SGeorge.Wilson@Sun.COM 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
234110594SGeorge.Wilson@Sun.COM 	}
234210594SGeorge.Wilson@Sun.COM 
23432082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
23442082Seschrock 
23454543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
23462082Seschrock 		return (0);
23472082Seschrock 
23482082Seschrock 	return (zpool_standard_error(hdl, errno, msg));
23491544Seschrock }
23501544Seschrock 
23511544Seschrock /*
23521544Seschrock  * Clear the errors for the pool, or the particular device if specified.
23531544Seschrock  */
23541544Seschrock int
23551544Seschrock zpool_clear(zpool_handle_t *zhp, const char *path)
23561544Seschrock {
23571544Seschrock 	zfs_cmd_t zc = { 0 };
23581544Seschrock 	char msg[1024];
23592082Seschrock 	nvlist_t *tgt;
23605450Sbrendan 	boolean_t avail_spare, l2cache;
23612082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
23621544Seschrock 
23631544Seschrock 	if (path)
23641544Seschrock 		(void) snprintf(msg, sizeof (msg),
23651544Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
23662676Seschrock 		    path);
23671544Seschrock 	else
23681544Seschrock 		(void) snprintf(msg, sizeof (msg),
23691544Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
23701544Seschrock 		    zhp->zpool_name);
23711544Seschrock 
23721544Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
23732082Seschrock 	if (path) {
23745450Sbrendan 		if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
23757326SEric.Schrock@Sun.COM 		    &l2cache, NULL)) == 0)
23762082Seschrock 			return (zfs_error(hdl, EZFS_NODEVICE, msg));
23772082Seschrock 
23785450Sbrendan 		/*
23795450Sbrendan 		 * Don't allow error clearing for hot spares.  Do allow
23805450Sbrendan 		 * error clearing for l2cache devices.
23815450Sbrendan 		 */
23822468Sek110237 		if (avail_spare)
23832082Seschrock 			return (zfs_error(hdl, EZFS_ISSPARE, msg));
23842082Seschrock 
23852082Seschrock 		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
23862082Seschrock 		    &zc.zc_guid) == 0);
23871544Seschrock 	}
23881544Seschrock 
23894543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0)
23901544Seschrock 		return (0);
23911544Seschrock 
23922082Seschrock 	return (zpool_standard_error(hdl, errno, msg));
2393789Sahrens }
2394789Sahrens 
23953126Sahl /*
23964451Seschrock  * Similar to zpool_clear(), but takes a GUID (used by fmd).
23974451Seschrock  */
23984451Seschrock int
23994451Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
24004451Seschrock {
24014451Seschrock 	zfs_cmd_t zc = { 0 };
24024451Seschrock 	char msg[1024];
24034451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
24044451Seschrock 
24054451Seschrock 	(void) snprintf(msg, sizeof (msg),
24064451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
24074451Seschrock 	    guid);
24084451Seschrock 
24094451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
24104451Seschrock 	zc.zc_guid = guid;
24114451Seschrock 
24124451Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
24134451Seschrock 		return (0);
24144451Seschrock 
24154451Seschrock 	return (zpool_standard_error(hdl, errno, msg));
24164451Seschrock }
24174451Seschrock 
24184451Seschrock /*
24191354Seschrock  * Convert from a devid string to a path.
24201354Seschrock  */
24211354Seschrock static char *
24221354Seschrock devid_to_path(char *devid_str)
24231354Seschrock {
24241354Seschrock 	ddi_devid_t devid;
24251354Seschrock 	char *minor;
24261354Seschrock 	char *path;
24271354Seschrock 	devid_nmlist_t *list = NULL;
24281354Seschrock 	int ret;
24291354Seschrock 
24301354Seschrock 	if (devid_str_decode(devid_str, &devid, &minor) != 0)
24311354Seschrock 		return (NULL);
24321354Seschrock 
24331354Seschrock 	ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
24341354Seschrock 
24351354Seschrock 	devid_str_free(minor);
24361354Seschrock 	devid_free(devid);
24371354Seschrock 
24381354Seschrock 	if (ret != 0)
24391354Seschrock 		return (NULL);
24401354Seschrock 
24412082Seschrock 	if ((path = strdup(list[0].devname)) == NULL)
24422082Seschrock 		return (NULL);
24432082Seschrock 
24441354Seschrock 	devid_free_nmlist(list);
24451354Seschrock 
24461354Seschrock 	return (path);
24471354Seschrock }
24481354Seschrock 
24491354Seschrock /*
24501354Seschrock  * Convert from a path to a devid string.
24511354Seschrock  */
24521354Seschrock static char *
24531354Seschrock path_to_devid(const char *path)
24541354Seschrock {
24551354Seschrock 	int fd;
24561354Seschrock 	ddi_devid_t devid;
24571354Seschrock 	char *minor, *ret;
24581354Seschrock 
24591354Seschrock 	if ((fd = open(path, O_RDONLY)) < 0)
24601354Seschrock 		return (NULL);
24611354Seschrock 
24621354Seschrock 	minor = NULL;
24631354Seschrock 	ret = NULL;
24641354Seschrock 	if (devid_get(fd, &devid) == 0) {
24651354Seschrock 		if (devid_get_minor_name(fd, &minor) == 0)
24661354Seschrock 			ret = devid_str_encode(devid, minor);
24671354Seschrock 		if (minor != NULL)
24681354Seschrock 			devid_str_free(minor);
24691354Seschrock 		devid_free(devid);
24701354Seschrock 	}
24711354Seschrock 	(void) close(fd);
24721354Seschrock 
24731354Seschrock 	return (ret);
24741354Seschrock }
24751354Seschrock 
24761354Seschrock /*
24771354Seschrock  * Issue the necessary ioctl() to update the stored path value for the vdev.  We
24781354Seschrock  * ignore any failure here, since a common case is for an unprivileged user to
24791354Seschrock  * type 'zpool status', and we'll display the correct information anyway.
24801354Seschrock  */
24811354Seschrock static void
24821354Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
24831354Seschrock {
24841354Seschrock 	zfs_cmd_t zc = { 0 };
24851354Seschrock 
24861354Seschrock 	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
24872676Seschrock 	(void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
24881354Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
24891544Seschrock 	    &zc.zc_guid) == 0);
24901354Seschrock 
24912082Seschrock 	(void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
24921354Seschrock }
24931354Seschrock 
24941354Seschrock /*
24951354Seschrock  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
24961354Seschrock  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
24971354Seschrock  * We also check if this is a whole disk, in which case we strip off the
24981354Seschrock  * trailing 's0' slice name.
24991354Seschrock  *
25001354Seschrock  * This routine is also responsible for identifying when disks have been
25011354Seschrock  * reconfigured in a new location.  The kernel will have opened the device by
25021354Seschrock  * devid, but the path will still refer to the old location.  To catch this, we
25031354Seschrock  * first do a path -> devid translation (which is fast for the common case).  If
25041354Seschrock  * the devid matches, we're done.  If not, we do a reverse devid -> path
25051354Seschrock  * translation and issue the appropriate ioctl() to update the path of the vdev.
25061354Seschrock  * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
25071354Seschrock  * of these checks.
25081354Seschrock  */
25091354Seschrock char *
251010594SGeorge.Wilson@Sun.COM zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
251110594SGeorge.Wilson@Sun.COM     boolean_t verbose)
25121354Seschrock {
25131354Seschrock 	char *path, *devid;
25141544Seschrock 	uint64_t value;
25151544Seschrock 	char buf[64];
25164451Seschrock 	vdev_stat_t *vs;
25174451Seschrock 	uint_t vsc;
25181354Seschrock 
25191544Seschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
25201544Seschrock 	    &value) == 0) {
25211544Seschrock 		verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
25221544Seschrock 		    &value) == 0);
25232856Snd150628 		(void) snprintf(buf, sizeof (buf), "%llu",
25242856Snd150628 		    (u_longlong_t)value);
25251544Seschrock 		path = buf;
25261544Seschrock 	} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
25271354Seschrock 
25284451Seschrock 		/*
25294451Seschrock 		 * If the device is dead (faulted, offline, etc) then don't
25304451Seschrock 		 * bother opening it.  Otherwise we may be forcing the user to
25314451Seschrock 		 * open a misbehaving device, which can have undesirable
25324451Seschrock 		 * effects.
25334451Seschrock 		 */
25344451Seschrock 		if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
25354451Seschrock 		    (uint64_t **)&vs, &vsc) != 0 ||
25364451Seschrock 		    vs->vs_state >= VDEV_STATE_DEGRADED) &&
25374451Seschrock 		    zhp != NULL &&
25381354Seschrock 		    nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
25391354Seschrock 			/*
25401354Seschrock 			 * Determine if the current path is correct.
25411354Seschrock 			 */
25421354Seschrock 			char *newdevid = path_to_devid(path);
25431354Seschrock 
25441354Seschrock 			if (newdevid == NULL ||
25451354Seschrock 			    strcmp(devid, newdevid) != 0) {
25461354Seschrock 				char *newpath;
25471354Seschrock 
25481354Seschrock 				if ((newpath = devid_to_path(devid)) != NULL) {
25491354Seschrock 					/*
25501354Seschrock 					 * Update the path appropriately.
25511354Seschrock 					 */
25521354Seschrock 					set_path(zhp, nv, newpath);
25532082Seschrock 					if (nvlist_add_string(nv,
25542082Seschrock 					    ZPOOL_CONFIG_PATH, newpath) == 0)
25552082Seschrock 						verify(nvlist_lookup_string(nv,
25562082Seschrock 						    ZPOOL_CONFIG_PATH,
25572082Seschrock 						    &path) == 0);
25581354Seschrock 					free(newpath);
25591354Seschrock 				}
25601354Seschrock 			}
25611354Seschrock 
25622082Seschrock 			if (newdevid)
25632082Seschrock 				devid_str_free(newdevid);
25641354Seschrock 		}
25651354Seschrock 
25661354Seschrock 		if (strncmp(path, "/dev/dsk/", 9) == 0)
25671354Seschrock 			path += 9;
25681354Seschrock 
25691354Seschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
25701544Seschrock 		    &value) == 0 && value) {
25712082Seschrock 			char *tmp = zfs_strdup(hdl, path);
25722082Seschrock 			if (tmp == NULL)
25732082Seschrock 				return (NULL);
25741354Seschrock 			tmp[strlen(path) - 2] = '\0';
25751354Seschrock 			return (tmp);
25761354Seschrock 		}
25771354Seschrock 	} else {
25781354Seschrock 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
25792082Seschrock 
25802082Seschrock 		/*
25812082Seschrock 		 * If it's a raidz device, we need to stick in the parity level.
25822082Seschrock 		 */
25832082Seschrock 		if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
25842082Seschrock 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
25852082Seschrock 			    &value) == 0);
25862082Seschrock 			(void) snprintf(buf, sizeof (buf), "%s%llu", path,
25872856Snd150628 			    (u_longlong_t)value);
25882082Seschrock 			path = buf;
25892082Seschrock 		}
259010594SGeorge.Wilson@Sun.COM 
259110594SGeorge.Wilson@Sun.COM 		/*
259210594SGeorge.Wilson@Sun.COM 		 * We identify each top-level vdev by using a <type-id>
259310594SGeorge.Wilson@Sun.COM 		 * naming convention.
259410594SGeorge.Wilson@Sun.COM 		 */
259510594SGeorge.Wilson@Sun.COM 		if (verbose) {
259610594SGeorge.Wilson@Sun.COM 			uint64_t id;
259710594SGeorge.Wilson@Sun.COM 
259810594SGeorge.Wilson@Sun.COM 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
259910594SGeorge.Wilson@Sun.COM 			    &id) == 0);
260010594SGeorge.Wilson@Sun.COM 			(void) snprintf(buf, sizeof (buf), "%s-%llu", path,
260110594SGeorge.Wilson@Sun.COM 			    (u_longlong_t)id);
260210594SGeorge.Wilson@Sun.COM 			path = buf;
260310594SGeorge.Wilson@Sun.COM 		}
26041354Seschrock 	}
26051354Seschrock 
26062082Seschrock 	return (zfs_strdup(hdl, path));
26071354Seschrock }
26081544Seschrock 
26091544Seschrock static int
26101544Seschrock zbookmark_compare(const void *a, const void *b)
26111544Seschrock {
26121544Seschrock 	return (memcmp(a, b, sizeof (zbookmark_t)));
26131544Seschrock }
26141544Seschrock 
26151544Seschrock /*
26161544Seschrock  * Retrieve the persistent error log, uniquify the members, and return to the
26171544Seschrock  * caller.
26181544Seschrock  */
26191544Seschrock int
26203444Sek110237 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
26211544Seschrock {
26221544Seschrock 	zfs_cmd_t zc = { 0 };
26231544Seschrock 	uint64_t count;
26242676Seschrock 	zbookmark_t *zb = NULL;
26253444Sek110237 	int i;
26261544Seschrock 
26271544Seschrock 	/*
26281544Seschrock 	 * Retrieve the raw error list from the kernel.  If the number of errors
26291544Seschrock 	 * has increased, allocate more space and continue until we get the
26301544Seschrock 	 * entire list.
26311544Seschrock 	 */
26321544Seschrock 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
26331544Seschrock 	    &count) == 0);
26344820Sek110237 	if (count == 0)
26354820Sek110237 		return (0);
26362676Seschrock 	if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
26372856Snd150628 	    count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
26382082Seschrock 		return (-1);
26392676Seschrock 	zc.zc_nvlist_dst_size = count;
26401544Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
26411544Seschrock 	for (;;) {
26422082Seschrock 		if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
26432082Seschrock 		    &zc) != 0) {
26442676Seschrock 			free((void *)(uintptr_t)zc.zc_nvlist_dst);
26451544Seschrock 			if (errno == ENOMEM) {
26463823Svb160487 				count = zc.zc_nvlist_dst_size;
26472676Seschrock 				if ((zc.zc_nvlist_dst = (uintptr_t)
26483823Svb160487 				    zfs_alloc(zhp->zpool_hdl, count *
26493823Svb160487 				    sizeof (zbookmark_t))) == (uintptr_t)NULL)
26502082Seschrock 					return (-1);
26511544Seschrock 			} else {
26521544Seschrock 				return (-1);
26531544Seschrock 			}
26541544Seschrock 		} else {
26551544Seschrock 			break;
26561544Seschrock 		}
26571544Seschrock 	}
26581544Seschrock 
26591544Seschrock 	/*
26601544Seschrock 	 * Sort the resulting bookmarks.  This is a little confusing due to the
26611544Seschrock 	 * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
26622676Seschrock 	 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
26631544Seschrock 	 * _not_ copied as part of the process.  So we point the start of our
26641544Seschrock 	 * array appropriate and decrement the total number of elements.
26651544Seschrock 	 */
26662676Seschrock 	zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
26672676Seschrock 	    zc.zc_nvlist_dst_size;
26682676Seschrock 	count -= zc.zc_nvlist_dst_size;
26691544Seschrock 
26701544Seschrock 	qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
26711544Seschrock 
26723444Sek110237 	verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
26731544Seschrock 
26741544Seschrock 	/*
26753444Sek110237 	 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
26761544Seschrock 	 */
26771544Seschrock 	for (i = 0; i < count; i++) {
26781544Seschrock 		nvlist_t *nv;
26791544Seschrock 
26803700Sek110237 		/* ignoring zb_blkid and zb_level for now */
26813700Sek110237 		if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
26823700Sek110237 		    zb[i-1].zb_object == zb[i].zb_object)
26831544Seschrock 			continue;
26841544Seschrock 
26853444Sek110237 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
26863444Sek110237 			goto nomem;
26873444Sek110237 		if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
26883444Sek110237 		    zb[i].zb_objset) != 0) {
26893444Sek110237 			nvlist_free(nv);
26902082Seschrock 			goto nomem;
26913444Sek110237 		}
26923444Sek110237 		if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
26933444Sek110237 		    zb[i].zb_object) != 0) {
26943444Sek110237 			nvlist_free(nv);
26953444Sek110237 			goto nomem;
26961544Seschrock 		}
26973444Sek110237 		if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
26983444Sek110237 			nvlist_free(nv);
26993444Sek110237 			goto nomem;
27003444Sek110237 		}
27013444Sek110237 		nvlist_free(nv);
27021544Seschrock 	}
27031544Seschrock 
27043265Sahrens 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
27051544Seschrock 	return (0);
27062082Seschrock 
27072082Seschrock nomem:
27082676Seschrock 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
27092082Seschrock 	return (no_memory(zhp->zpool_hdl));
27101544Seschrock }
27111760Seschrock 
27121760Seschrock /*
27131760Seschrock  * Upgrade a ZFS pool to the latest on-disk version.
27141760Seschrock  */
27151760Seschrock int
27165094Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
27171760Seschrock {
27181760Seschrock 	zfs_cmd_t zc = { 0 };
27192082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
27201760Seschrock 
27211760Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
27225094Slling 	zc.zc_cookie = new_version;
27235094Slling 
27244543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
27253237Slling 		return (zpool_standard_error_fmt(hdl, errno,
27262082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
27272082Seschrock 		    zhp->zpool_name));
27281760Seschrock 	return (0);
27291760Seschrock }
27302926Sek110237 
27314988Sek110237 void
27324988Sek110237 zpool_set_history_str(const char *subcommand, int argc, char **argv,
27334988Sek110237     char *history_str)
27344988Sek110237 {
27354988Sek110237 	int i;
27364988Sek110237 
27374988Sek110237 	(void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
27384988Sek110237 	for (i = 1; i < argc; i++) {
27394988Sek110237 		if (strlen(history_str) + 1 + strlen(argv[i]) >
27404988Sek110237 		    HIS_MAX_RECORD_LEN)
27414988Sek110237 			break;
27424988Sek110237 		(void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
27434988Sek110237 		(void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
27444988Sek110237 	}
27454988Sek110237 }
27464988Sek110237 
27472926Sek110237 /*
27484988Sek110237  * Stage command history for logging.
27492926Sek110237  */
27504988Sek110237 int
27514988Sek110237 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
27522926Sek110237 {
27534988Sek110237 	if (history_str == NULL)
27544988Sek110237 		return (EINVAL);
27554988Sek110237 
27564988Sek110237 	if (strlen(history_str) > HIS_MAX_RECORD_LEN)
27574988Sek110237 		return (EINVAL);
27582926Sek110237 
27594715Sek110237 	if (hdl->libzfs_log_str != NULL)
27604543Smarks 		free(hdl->libzfs_log_str);
27612926Sek110237 
27624988Sek110237 	if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
27634988Sek110237 		return (no_memory(hdl));
27644543Smarks 
27654988Sek110237 	return (0);
27662926Sek110237 }
27672926Sek110237 
27682926Sek110237 /*
27692926Sek110237  * Perform ioctl to get some command history of a pool.
27702926Sek110237  *
27712926Sek110237  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
27722926Sek110237  * logical offset of the history buffer to start reading from.
27732926Sek110237  *
27742926Sek110237  * Upon return, 'off' is the next logical offset to read from and
27752926Sek110237  * 'len' is the actual amount of bytes read into 'buf'.
27762926Sek110237  */
27772926Sek110237 static int
27782926Sek110237 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
27792926Sek110237 {
27802926Sek110237 	zfs_cmd_t zc = { 0 };
27812926Sek110237 	libzfs_handle_t *hdl = zhp->zpool_hdl;
27822926Sek110237 
27832926Sek110237 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
27842926Sek110237 
27852926Sek110237 	zc.zc_history = (uint64_t)(uintptr_t)buf;
27862926Sek110237 	zc.zc_history_len = *len;
27872926Sek110237 	zc.zc_history_offset = *off;
27882926Sek110237 
27892926Sek110237 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
27902926Sek110237 		switch (errno) {
27912926Sek110237 		case EPERM:
27923237Slling 			return (zfs_error_fmt(hdl, EZFS_PERM,
27933237Slling 			    dgettext(TEXT_DOMAIN,
27942926Sek110237 			    "cannot show history for pool '%s'"),
27952926Sek110237 			    zhp->zpool_name));
27962926Sek110237 		case ENOENT:
27973237Slling 			return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
27982926Sek110237 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
27992926Sek110237 			    "'%s'"), zhp->zpool_name));
28003863Sek110237 		case ENOTSUP:
28013863Sek110237 			return (zfs_error_fmt(hdl, EZFS_BADVERSION,
28023863Sek110237 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
28033863Sek110237 			    "'%s', pool must be upgraded"), zhp->zpool_name));
28042926Sek110237 		default:
28053237Slling 			return (zpool_standard_error_fmt(hdl, errno,
28062926Sek110237 			    dgettext(TEXT_DOMAIN,
28072926Sek110237 			    "cannot get history for '%s'"), zhp->zpool_name));
28082926Sek110237 		}
28092926Sek110237 	}
28102926Sek110237 
28112926Sek110237 	*len = zc.zc_history_len;
28122926Sek110237 	*off = zc.zc_history_offset;
28132926Sek110237 
28142926Sek110237 	return (0);
28152926Sek110237 }
28162926Sek110237 
28172926Sek110237 /*
28182926Sek110237  * Process the buffer of nvlists, unpacking and storing each nvlist record
28192926Sek110237  * into 'records'.  'leftover' is set to the number of bytes that weren't
28202926Sek110237  * processed as there wasn't a complete record.
28212926Sek110237  */
282210685SGeorge.Wilson@Sun.COM int
28232926Sek110237 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
28242926Sek110237     nvlist_t ***records, uint_t *numrecords)
28252926Sek110237 {
28262926Sek110237 	uint64_t reclen;
28272926Sek110237 	nvlist_t *nv;
28282926Sek110237 	int i;
28292926Sek110237 
28302926Sek110237 	while (bytes_read > sizeof (reclen)) {
28312926Sek110237 
28322926Sek110237 		/* get length of packed record (stored as little endian) */
28332926Sek110237 		for (i = 0, reclen = 0; i < sizeof (reclen); i++)
28342926Sek110237 			reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
28352926Sek110237 
28362926Sek110237 		if (bytes_read < sizeof (reclen) + reclen)
28372926Sek110237 			break;
28382926Sek110237 
28392926Sek110237 		/* unpack record */
28402926Sek110237 		if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
28412926Sek110237 			return (ENOMEM);
28422926Sek110237 		bytes_read -= sizeof (reclen) + reclen;
28432926Sek110237 		buf += sizeof (reclen) + reclen;
28442926Sek110237 
28452926Sek110237 		/* add record to nvlist array */
28462926Sek110237 		(*numrecords)++;
28472926Sek110237 		if (ISP2(*numrecords + 1)) {
28482926Sek110237 			*records = realloc(*records,
28492926Sek110237 			    *numrecords * 2 * sizeof (nvlist_t *));
28502926Sek110237 		}
28512926Sek110237 		(*records)[*numrecords - 1] = nv;
28522926Sek110237 	}
28532926Sek110237 
28542926Sek110237 	*leftover = bytes_read;
28552926Sek110237 	return (0);
28562926Sek110237 }
28572926Sek110237 
28582926Sek110237 #define	HIS_BUF_LEN	(128*1024)
28592926Sek110237 
28602926Sek110237 /*
28612926Sek110237  * Retrieve the command history of a pool.
28622926Sek110237  */
28632926Sek110237 int
28642926Sek110237 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
28652926Sek110237 {
28662926Sek110237 	char buf[HIS_BUF_LEN];
28672926Sek110237 	uint64_t off = 0;
28682926Sek110237 	nvlist_t **records = NULL;
28692926Sek110237 	uint_t numrecords = 0;
28702926Sek110237 	int err, i;
28712926Sek110237 
28722926Sek110237 	do {
28732926Sek110237 		uint64_t bytes_read = sizeof (buf);
28742926Sek110237 		uint64_t leftover;
28752926Sek110237 
28762926Sek110237 		if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
28772926Sek110237 			break;
28782926Sek110237 
28792926Sek110237 		/* if nothing else was read in, we're at EOF, just return */
28802926Sek110237 		if (!bytes_read)
28812926Sek110237 			break;
28822926Sek110237 
28832926Sek110237 		if ((err = zpool_history_unpack(buf, bytes_read,
28842926Sek110237 		    &leftover, &records, &numrecords)) != 0)
28852926Sek110237 			break;
28862926Sek110237 		off -= leftover;
28872926Sek110237 
28882926Sek110237 		/* CONSTCOND */
28892926Sek110237 	} while (1);
28902926Sek110237 
28912926Sek110237 	if (!err) {
28922926Sek110237 		verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
28932926Sek110237 		verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
28942926Sek110237 		    records, numrecords) == 0);
28952926Sek110237 	}
28962926Sek110237 	for (i = 0; i < numrecords; i++)
28972926Sek110237 		nvlist_free(records[i]);
28982926Sek110237 	free(records);
28992926Sek110237 
29002926Sek110237 	return (err);
29012926Sek110237 }
29023444Sek110237 
29033444Sek110237 void
29043444Sek110237 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
29053444Sek110237     char *pathname, size_t len)
29063444Sek110237 {
29073444Sek110237 	zfs_cmd_t zc = { 0 };
29083444Sek110237 	boolean_t mounted = B_FALSE;
29093444Sek110237 	char *mntpnt = NULL;
29103444Sek110237 	char dsname[MAXNAMELEN];
29113444Sek110237 
29123444Sek110237 	if (dsobj == 0) {
29133444Sek110237 		/* special case for the MOS */
29143444Sek110237 		(void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
29153444Sek110237 		return;
29163444Sek110237 	}
29173444Sek110237 
29183444Sek110237 	/* get the dataset's name */
29193444Sek110237 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
29203444Sek110237 	zc.zc_obj = dsobj;
29213444Sek110237 	if (ioctl(zhp->zpool_hdl->libzfs_fd,
29223444Sek110237 	    ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
29233444Sek110237 		/* just write out a path of two object numbers */
29243444Sek110237 		(void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
29253444Sek110237 		    dsobj, obj);
29263444Sek110237 		return;
29273444Sek110237 	}
29283444Sek110237 	(void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
29293444Sek110237 
29303444Sek110237 	/* find out if the dataset is mounted */
29313444Sek110237 	mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
29323444Sek110237 
29333444Sek110237 	/* get the corrupted object's path */
29343444Sek110237 	(void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
29353444Sek110237 	zc.zc_obj = obj;
29363444Sek110237 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
29373444Sek110237 	    &zc) == 0) {
29383444Sek110237 		if (mounted) {
29393444Sek110237 			(void) snprintf(pathname, len, "%s%s", mntpnt,
29403444Sek110237 			    zc.zc_value);
29413444Sek110237 		} else {
29423444Sek110237 			(void) snprintf(pathname, len, "%s:%s",
29433444Sek110237 			    dsname, zc.zc_value);
29443444Sek110237 		}
29453444Sek110237 	} else {
29463444Sek110237 		(void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
29473444Sek110237 	}
29483444Sek110237 	free(mntpnt);
29493444Sek110237 }
29503912Slling 
29514276Staylor /*
29527042Sgw25295  * Read the EFI label from the config, if a label does not exist then
29537042Sgw25295  * pass back the error to the caller. If the caller has passed a non-NULL
29547042Sgw25295  * diskaddr argument then we set it to the starting address of the EFI
29557042Sgw25295  * partition.
29567042Sgw25295  */
29577042Sgw25295 static int
29587042Sgw25295 read_efi_label(nvlist_t *config, diskaddr_t *sb)
29597042Sgw25295 {
29607042Sgw25295 	char *path;
29617042Sgw25295 	int fd;
29627042Sgw25295 	char diskname[MAXPATHLEN];
29637042Sgw25295 	int err = -1;
29647042Sgw25295 
29657042Sgw25295 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
29667042Sgw25295 		return (err);
29677042Sgw25295 
29687042Sgw25295 	(void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
29697042Sgw25295 	    strrchr(path, '/'));
29707042Sgw25295 	if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
29717042Sgw25295 		struct dk_gpt *vtoc;
29727042Sgw25295 
29737042Sgw25295 		if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
29747042Sgw25295 			if (sb != NULL)
29757042Sgw25295 				*sb = vtoc->efi_parts[0].p_start;
29767042Sgw25295 			efi_free(vtoc);
29777042Sgw25295 		}
29787042Sgw25295 		(void) close(fd);
29797042Sgw25295 	}
29807042Sgw25295 	return (err);
29817042Sgw25295 }
29827042Sgw25295 
29837042Sgw25295 /*
29844276Staylor  * determine where a partition starts on a disk in the current
29854276Staylor  * configuration
29864276Staylor  */
29874276Staylor static diskaddr_t
29884276Staylor find_start_block(nvlist_t *config)
29894276Staylor {
29904276Staylor 	nvlist_t **child;
29914276Staylor 	uint_t c, children;
29924276Staylor 	diskaddr_t sb = MAXOFFSET_T;
29934276Staylor 	uint64_t wholedisk;
29944276Staylor 
29954276Staylor 	if (nvlist_lookup_nvlist_array(config,
29964276Staylor 	    ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
29974276Staylor 		if (nvlist_lookup_uint64(config,
29984276Staylor 		    ZPOOL_CONFIG_WHOLE_DISK,
29994276Staylor 		    &wholedisk) != 0 || !wholedisk) {
30004276Staylor 			return (MAXOFFSET_T);
30014276Staylor 		}
30027042Sgw25295 		if (read_efi_label(config, &sb) < 0)
30037042Sgw25295 			sb = MAXOFFSET_T;
30044276Staylor 		return (sb);
30054276Staylor 	}
30064276Staylor 
30074276Staylor 	for (c = 0; c < children; c++) {
30084276Staylor 		sb = find_start_block(child[c]);
30094276Staylor 		if (sb != MAXOFFSET_T) {
30104276Staylor 			return (sb);
30114276Staylor 		}
30124276Staylor 	}
30134276Staylor 	return (MAXOFFSET_T);
30144276Staylor }
30154276Staylor 
30164276Staylor /*
30174276Staylor  * Label an individual disk.  The name provided is the short name,
30184276Staylor  * stripped of any leading /dev path.
30194276Staylor  */
30204276Staylor int
30214276Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
30224276Staylor {
30234276Staylor 	char path[MAXPATHLEN];
30244276Staylor 	struct dk_gpt *vtoc;
30254276Staylor 	int fd;
30264276Staylor 	size_t resv = EFI_MIN_RESV_SIZE;
30274276Staylor 	uint64_t slice_size;
30284276Staylor 	diskaddr_t start_block;
30294276Staylor 	char errbuf[1024];
30304276Staylor 
30316289Smmusante 	/* prepare an error message just in case */
30326289Smmusante 	(void) snprintf(errbuf, sizeof (errbuf),
30336289Smmusante 	    dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
30346289Smmusante 
30354276Staylor 	if (zhp) {
30364276Staylor 		nvlist_t *nvroot;
30374276Staylor 
30387965SGeorge.Wilson@Sun.COM 		if (pool_is_bootable(zhp)) {
30397965SGeorge.Wilson@Sun.COM 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30407965SGeorge.Wilson@Sun.COM 			    "EFI labeled devices are not supported on root "
30417965SGeorge.Wilson@Sun.COM 			    "pools."));
30427965SGeorge.Wilson@Sun.COM 			return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
30437965SGeorge.Wilson@Sun.COM 		}
30447965SGeorge.Wilson@Sun.COM 
30454276Staylor 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
30464276Staylor 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
30474276Staylor 
30484276Staylor 		if (zhp->zpool_start_block == 0)
30494276Staylor 			start_block = find_start_block(nvroot);
30504276Staylor 		else
30514276Staylor 			start_block = zhp->zpool_start_block;
30524276Staylor 		zhp->zpool_start_block = start_block;
30534276Staylor 	} else {
30544276Staylor 		/* new pool */
30554276Staylor 		start_block = NEW_START_BLOCK;
30564276Staylor 	}
30574276Staylor 
30584276Staylor 	(void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
30594276Staylor 	    BACKUP_SLICE);
30604276Staylor 
30614276Staylor 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
30624276Staylor 		/*
30634276Staylor 		 * This shouldn't happen.  We've long since verified that this
30644276Staylor 		 * is a valid device.
30654276Staylor 		 */
30666289Smmusante 		zfs_error_aux(hdl,
30676289Smmusante 		    dgettext(TEXT_DOMAIN, "unable to open device"));
30684276Staylor 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
30694276Staylor 	}
30704276Staylor 
30714276Staylor 	if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
30724276Staylor 		/*
30734276Staylor 		 * The only way this can fail is if we run out of memory, or we
30744276Staylor 		 * were unable to read the disk's capacity
30754276Staylor 		 */
30764276Staylor 		if (errno == ENOMEM)
30774276Staylor 			(void) no_memory(hdl);
30784276Staylor 
30794276Staylor 		(void) close(fd);
30806289Smmusante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30816289Smmusante 		    "unable to read disk capacity"), name);
30824276Staylor 
30834276Staylor 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
30844276Staylor 	}
30854276Staylor 
30864276Staylor 	slice_size = vtoc->efi_last_u_lba + 1;
30874276Staylor 	slice_size -= EFI_MIN_RESV_SIZE;
30884276Staylor 	if (start_block == MAXOFFSET_T)
30894276Staylor 		start_block = NEW_START_BLOCK;
30904276Staylor 	slice_size -= start_block;
30914276Staylor 
30924276Staylor 	vtoc->efi_parts[0].p_start = start_block;
30934276Staylor 	vtoc->efi_parts[0].p_size = slice_size;
30944276Staylor 
30954276Staylor 	/*
30964276Staylor 	 * Why we use V_USR: V_BACKUP confuses users, and is considered
30974276Staylor 	 * disposable by some EFI utilities (since EFI doesn't have a backup
30984276Staylor 	 * slice).  V_UNASSIGNED is supposed to be used only for zero size
30994276Staylor 	 * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
31004276Staylor 	 * etc. were all pretty specific.  V_USR is as close to reality as we
31014276Staylor 	 * can get, in the absence of V_OTHER.
31024276Staylor 	 */
31034276Staylor 	vtoc->efi_parts[0].p_tag = V_USR;
31044276Staylor 	(void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
31054276Staylor 
31064276Staylor 	vtoc->efi_parts[8].p_start = slice_size + start_block;
31074276Staylor 	vtoc->efi_parts[8].p_size = resv;
31084276Staylor 	vtoc->efi_parts[8].p_tag = V_RESERVED;
31094276Staylor 
31104276Staylor 	if (efi_write(fd, vtoc) != 0) {
31114276Staylor 		/*
31124276Staylor 		 * Some block drivers (like pcata) may not support EFI
31134276Staylor 		 * GPT labels.  Print out a helpful error message dir-
31144276Staylor 		 * ecting the user to manually label the disk and give
31154276Staylor 		 * a specific slice.
31164276Staylor 		 */
31174276Staylor 		(void) close(fd);
31184276Staylor 		efi_free(vtoc);
31194276Staylor 
31204276Staylor 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
31216289Smmusante 		    "try using fdisk(1M) and then provide a specific slice"));
31224276Staylor 		return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
31234276Staylor 	}
31244276Staylor 
31254276Staylor 	(void) close(fd);
31264276Staylor 	efi_free(vtoc);
31274276Staylor 	return (0);
31284276Staylor }
31296423Sgw25295 
31306423Sgw25295 static boolean_t
31316423Sgw25295 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
31326423Sgw25295 {
31336423Sgw25295 	char *type;
31346423Sgw25295 	nvlist_t **child;
31356423Sgw25295 	uint_t children, c;
31366423Sgw25295 
31376423Sgw25295 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
31386423Sgw25295 	if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
31396423Sgw25295 	    strcmp(type, VDEV_TYPE_FILE) == 0 ||
31406423Sgw25295 	    strcmp(type, VDEV_TYPE_LOG) == 0 ||
314110594SGeorge.Wilson@Sun.COM 	    strcmp(type, VDEV_TYPE_HOLE) == 0 ||
31426423Sgw25295 	    strcmp(type, VDEV_TYPE_MISSING) == 0) {
31436423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
31446423Sgw25295 		    "vdev type '%s' is not supported"), type);
31456423Sgw25295 		(void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
31466423Sgw25295 		return (B_FALSE);
31476423Sgw25295 	}
31486423Sgw25295 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
31496423Sgw25295 	    &child, &children) == 0) {
31506423Sgw25295 		for (c = 0; c < children; c++) {
31516423Sgw25295 			if (!supported_dump_vdev_type(hdl, child[c], errbuf))
31526423Sgw25295 				return (B_FALSE);
31536423Sgw25295 		}
31546423Sgw25295 	}
31556423Sgw25295 	return (B_TRUE);
31566423Sgw25295 }
31576423Sgw25295 
31586423Sgw25295 /*
31596423Sgw25295  * check if this zvol is allowable for use as a dump device; zero if
31606423Sgw25295  * it is, > 0 if it isn't, < 0 if it isn't a zvol
31616423Sgw25295  */
31626423Sgw25295 int
31636423Sgw25295 zvol_check_dump_config(char *arg)
31646423Sgw25295 {
31656423Sgw25295 	zpool_handle_t *zhp = NULL;
31666423Sgw25295 	nvlist_t *config, *nvroot;
31676423Sgw25295 	char *p, *volname;
31686423Sgw25295 	nvlist_t **top;
31696423Sgw25295 	uint_t toplevels;
31706423Sgw25295 	libzfs_handle_t *hdl;
31716423Sgw25295 	char errbuf[1024];
31726423Sgw25295 	char poolname[ZPOOL_MAXNAMELEN];
31736423Sgw25295 	int pathlen = strlen(ZVOL_FULL_DEV_DIR);
31746423Sgw25295 	int ret = 1;
31756423Sgw25295 
31766423Sgw25295 	if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
31776423Sgw25295 		return (-1);
31786423Sgw25295 	}
31796423Sgw25295 
31806423Sgw25295 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
31816423Sgw25295 	    "dump is not supported on device '%s'"), arg);
31826423Sgw25295 
31836423Sgw25295 	if ((hdl = libzfs_init()) == NULL)
31846423Sgw25295 		return (1);
31856423Sgw25295 	libzfs_print_on_error(hdl, B_TRUE);
31866423Sgw25295 
31876423Sgw25295 	volname = arg + pathlen;
31886423Sgw25295 
31896423Sgw25295 	/* check the configuration of the pool */
31906423Sgw25295 	if ((p = strchr(volname, '/')) == NULL) {
31916423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
31926423Sgw25295 		    "malformed dataset name"));
31936423Sgw25295 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
31946423Sgw25295 		return (1);
31956423Sgw25295 	} else if (p - volname >= ZFS_MAXNAMELEN) {
31966423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
31976423Sgw25295 		    "dataset name is too long"));
31986423Sgw25295 		(void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
31996423Sgw25295 		return (1);
32006423Sgw25295 	} else {
32016423Sgw25295 		(void) strncpy(poolname, volname, p - volname);
32026423Sgw25295 		poolname[p - volname] = '\0';
32036423Sgw25295 	}
32046423Sgw25295 
32056423Sgw25295 	if ((zhp = zpool_open(hdl, poolname)) == NULL) {
32066423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
32076423Sgw25295 		    "could not open pool '%s'"), poolname);
32086423Sgw25295 		(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
32096423Sgw25295 		goto out;
32106423Sgw25295 	}
32116423Sgw25295 	config = zpool_get_config(zhp, NULL);
32126423Sgw25295 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
32136423Sgw25295 	    &nvroot) != 0) {
32146423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
32156423Sgw25295 		    "could not obtain vdev configuration for  '%s'"), poolname);
32166423Sgw25295 		(void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
32176423Sgw25295 		goto out;
32186423Sgw25295 	}
32196423Sgw25295 
32206423Sgw25295 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
32216423Sgw25295 	    &top, &toplevels) == 0);
32226423Sgw25295 	if (toplevels != 1) {
32236423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
32246423Sgw25295 		    "'%s' has multiple top level vdevs"), poolname);
32256423Sgw25295 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
32266423Sgw25295 		goto out;
32276423Sgw25295 	}
32286423Sgw25295 
32296423Sgw25295 	if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
32306423Sgw25295 		goto out;
32316423Sgw25295 	}
32326423Sgw25295 	ret = 0;
32336423Sgw25295 
32346423Sgw25295 out:
32356423Sgw25295 	if (zhp)
32366423Sgw25295 		zpool_close(zhp);
32376423Sgw25295 	libzfs_fini(hdl);
32386423Sgw25295 	return (ret);
32396423Sgw25295 }
3240