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"
4410921STim.Haley@Sun.COM #include "zfs_comutil.h"
45789Sahrens 
4610685SGeorge.Wilson@Sun.COM const char *hist_event_table[LOG_END] = {
4710685SGeorge.Wilson@Sun.COM 	"invalid event",
4810685SGeorge.Wilson@Sun.COM 	"pool create",
4910685SGeorge.Wilson@Sun.COM 	"vdev add",
5010685SGeorge.Wilson@Sun.COM 	"pool remove",
5110685SGeorge.Wilson@Sun.COM 	"pool destroy",
5210685SGeorge.Wilson@Sun.COM 	"pool export",
5310685SGeorge.Wilson@Sun.COM 	"pool import",
5410685SGeorge.Wilson@Sun.COM 	"vdev attach",
5510685SGeorge.Wilson@Sun.COM 	"vdev replace",
5610685SGeorge.Wilson@Sun.COM 	"vdev detach",
5710685SGeorge.Wilson@Sun.COM 	"vdev online",
5810685SGeorge.Wilson@Sun.COM 	"vdev offline",
5910685SGeorge.Wilson@Sun.COM 	"vdev upgrade",
6010685SGeorge.Wilson@Sun.COM 	"pool clear",
6110685SGeorge.Wilson@Sun.COM 	"pool scrub",
6210685SGeorge.Wilson@Sun.COM 	"pool property set",
6310685SGeorge.Wilson@Sun.COM 	"create",
6410685SGeorge.Wilson@Sun.COM 	"clone",
6510685SGeorge.Wilson@Sun.COM 	"destroy",
6610685SGeorge.Wilson@Sun.COM 	"destroy_begin_sync",
6710685SGeorge.Wilson@Sun.COM 	"inherit",
6810685SGeorge.Wilson@Sun.COM 	"property set",
6910685SGeorge.Wilson@Sun.COM 	"quota set",
7010685SGeorge.Wilson@Sun.COM 	"permission update",
7110685SGeorge.Wilson@Sun.COM 	"permission remove",
7210685SGeorge.Wilson@Sun.COM 	"permission who remove",
7310685SGeorge.Wilson@Sun.COM 	"promote",
7410685SGeorge.Wilson@Sun.COM 	"receive",
7510685SGeorge.Wilson@Sun.COM 	"rename",
7610685SGeorge.Wilson@Sun.COM 	"reservation set",
7710685SGeorge.Wilson@Sun.COM 	"replay_inc_sync",
7810685SGeorge.Wilson@Sun.COM 	"replay_full_sync",
7910685SGeorge.Wilson@Sun.COM 	"rollback",
8010685SGeorge.Wilson@Sun.COM 	"snapshot",
8110685SGeorge.Wilson@Sun.COM 	"filesystem version upgrade",
8210685SGeorge.Wilson@Sun.COM 	"refquota set",
8310685SGeorge.Wilson@Sun.COM 	"refreservation set",
8410685SGeorge.Wilson@Sun.COM 	"pool scrub done",
8510685SGeorge.Wilson@Sun.COM 	"user hold",
8610685SGeorge.Wilson@Sun.COM 	"user release",
8710685SGeorge.Wilson@Sun.COM };
8810685SGeorge.Wilson@Sun.COM 
897042Sgw25295 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
905094Slling 
917965SGeorge.Wilson@Sun.COM #if defined(__i386) || defined(__amd64)
927965SGeorge.Wilson@Sun.COM #define	BOOTCMD	"installgrub(1M)"
937965SGeorge.Wilson@Sun.COM #else
947965SGeorge.Wilson@Sun.COM #define	BOOTCMD	"installboot(1M)"
957965SGeorge.Wilson@Sun.COM #endif
967965SGeorge.Wilson@Sun.COM 
979816SGeorge.Wilson@Sun.COM #define	DISK_ROOT	"/dev/dsk"
989816SGeorge.Wilson@Sun.COM #define	RDISK_ROOT	"/dev/rdsk"
999816SGeorge.Wilson@Sun.COM #define	BACKUP_SLICE	"s2"
1009816SGeorge.Wilson@Sun.COM 
1015094Slling /*
1025094Slling  * ====================================================================
1035094Slling  *   zpool property functions
1045094Slling  * ====================================================================
1055094Slling  */
1065094Slling 
1075094Slling static int
1085094Slling zpool_get_all_props(zpool_handle_t *zhp)
1095094Slling {
1105094Slling 	zfs_cmd_t zc = { 0 };
1115094Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1125094Slling 
1135094Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1145094Slling 
1155094Slling 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
1165094Slling 		return (-1);
1175094Slling 
1185094Slling 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
1195094Slling 		if (errno == ENOMEM) {
1205094Slling 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1215094Slling 				zcmd_free_nvlists(&zc);
1225094Slling 				return (-1);
1235094Slling 			}
1245094Slling 		} else {
1255094Slling 			zcmd_free_nvlists(&zc);
1265094Slling 			return (-1);
1275094Slling 		}
1285094Slling 	}
1295094Slling 
1305094Slling 	if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
1315094Slling 		zcmd_free_nvlists(&zc);
1325094Slling 		return (-1);
1335094Slling 	}
1345094Slling 
1355094Slling 	zcmd_free_nvlists(&zc);
1365094Slling 
1375094Slling 	return (0);
1385094Slling }
1395094Slling 
1405094Slling static int
1415094Slling zpool_props_refresh(zpool_handle_t *zhp)
1425094Slling {
1435094Slling 	nvlist_t *old_props;
1445094Slling 
1455094Slling 	old_props = zhp->zpool_props;
1465094Slling 
1475094Slling 	if (zpool_get_all_props(zhp) != 0)
1485094Slling 		return (-1);
1495094Slling 
1505094Slling 	nvlist_free(old_props);
1515094Slling 	return (0);
1525094Slling }
1535094Slling 
1545094Slling static char *
1555094Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
1565094Slling     zprop_source_t *src)
1575094Slling {
1585094Slling 	nvlist_t *nv, *nvl;
1595094Slling 	uint64_t ival;
1605094Slling 	char *value;
1615094Slling 	zprop_source_t source;
1625094Slling 
1635094Slling 	nvl = zhp->zpool_props;
1645094Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
1655094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
1665094Slling 		source = ival;
1675094Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1685094Slling 	} else {
1695094Slling 		source = ZPROP_SRC_DEFAULT;
1705094Slling 		if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
1715094Slling 			value = "-";
1725094Slling 	}
1735094Slling 
1745094Slling 	if (src)
1755094Slling 		*src = source;
1765094Slling 
1775094Slling 	return (value);
1785094Slling }
1795094Slling 
1805094Slling uint64_t
1815094Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
1825094Slling {
1835094Slling 	nvlist_t *nv, *nvl;
1845094Slling 	uint64_t value;
1855094Slling 	zprop_source_t source;
1865094Slling 
1877294Sperrin 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
1887294Sperrin 		/*
1897294Sperrin 		 * zpool_get_all_props() has most likely failed because
1907294Sperrin 		 * the pool is faulted, but if all we need is the top level
1917294Sperrin 		 * vdev's guid then get it from the zhp config nvlist.
1927294Sperrin 		 */
1937294Sperrin 		if ((prop == ZPOOL_PROP_GUID) &&
1947294Sperrin 		    (nvlist_lookup_nvlist(zhp->zpool_config,
1957294Sperrin 		    ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
1967294Sperrin 		    (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
1977294Sperrin 		    == 0)) {
1987294Sperrin 			return (value);
1997294Sperrin 		}
2005094Slling 		return (zpool_prop_default_numeric(prop));
2017294Sperrin 	}
2025094Slling 
2035094Slling 	nvl = zhp->zpool_props;
2045094Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
2055094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
2065094Slling 		source = value;
2075094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
2085094Slling 	} else {
2095094Slling 		source = ZPROP_SRC_DEFAULT;
2105094Slling 		value = zpool_prop_default_numeric(prop);
2115094Slling 	}
2125094Slling 
2135094Slling 	if (src)
2145094Slling 		*src = source;
2155094Slling 
2165094Slling 	return (value);
2175094Slling }
2185094Slling 
2195094Slling /*
2205094Slling  * Map VDEV STATE to printed strings.
2215094Slling  */
2225094Slling char *
2235094Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
2245094Slling {
2255094Slling 	switch (state) {
2265094Slling 	case VDEV_STATE_CLOSED:
2275094Slling 	case VDEV_STATE_OFFLINE:
2285094Slling 		return (gettext("OFFLINE"));
2295094Slling 	case VDEV_STATE_REMOVED:
2305094Slling 		return (gettext("REMOVED"));
2315094Slling 	case VDEV_STATE_CANT_OPEN:
2327294Sperrin 		if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
2335094Slling 			return (gettext("FAULTED"));
2345094Slling 		else
2355094Slling 			return (gettext("UNAVAIL"));
2365094Slling 	case VDEV_STATE_FAULTED:
2375094Slling 		return (gettext("FAULTED"));
2385094Slling 	case VDEV_STATE_DEGRADED:
2395094Slling 		return (gettext("DEGRADED"));
2405094Slling 	case VDEV_STATE_HEALTHY:
2415094Slling 		return (gettext("ONLINE"));
2425094Slling 	}
2435094Slling 
2445094Slling 	return (gettext("UNKNOWN"));
2455094Slling }
2465094Slling 
2475094Slling /*
2485094Slling  * Get a zpool property value for 'prop' and return the value in
2495094Slling  * a pre-allocated buffer.
2505094Slling  */
2515094Slling int
2525094Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
2535094Slling     zprop_source_t *srctype)
2545094Slling {
2555094Slling 	uint64_t intval;
2565094Slling 	const char *strval;
2575094Slling 	zprop_source_t src = ZPROP_SRC_NONE;
2585094Slling 	nvlist_t *nvroot;
2595094Slling 	vdev_stat_t *vs;
2605094Slling 	uint_t vsc;
2615094Slling 
2625094Slling 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
2638525SEric.Schrock@Sun.COM 		switch (prop) {
2648525SEric.Schrock@Sun.COM 		case ZPOOL_PROP_NAME:
2655094Slling 			(void) strlcpy(buf, zpool_get_name(zhp), len);
2668525SEric.Schrock@Sun.COM 			break;
2678525SEric.Schrock@Sun.COM 
2688525SEric.Schrock@Sun.COM 		case ZPOOL_PROP_HEALTH:
2695094Slling 			(void) strlcpy(buf, "FAULTED", len);
2708525SEric.Schrock@Sun.COM 			break;
2718525SEric.Schrock@Sun.COM 
2728525SEric.Schrock@Sun.COM 		case ZPOOL_PROP_GUID:
2738525SEric.Schrock@Sun.COM 			intval = zpool_get_prop_int(zhp, prop, &src);
2748525SEric.Schrock@Sun.COM 			(void) snprintf(buf, len, "%llu", intval);
2758525SEric.Schrock@Sun.COM 			break;
2768525SEric.Schrock@Sun.COM 
2778525SEric.Schrock@Sun.COM 		case ZPOOL_PROP_ALTROOT:
2788525SEric.Schrock@Sun.COM 		case ZPOOL_PROP_CACHEFILE:
2798525SEric.Schrock@Sun.COM 			if (zhp->zpool_props != NULL ||
2808525SEric.Schrock@Sun.COM 			    zpool_get_all_props(zhp) == 0) {
2818525SEric.Schrock@Sun.COM 				(void) strlcpy(buf,
2828525SEric.Schrock@Sun.COM 				    zpool_get_prop_string(zhp, prop, &src),
2838525SEric.Schrock@Sun.COM 				    len);
2848525SEric.Schrock@Sun.COM 				if (srctype != NULL)
2858525SEric.Schrock@Sun.COM 					*srctype = src;
2868525SEric.Schrock@Sun.COM 				return (0);
2878525SEric.Schrock@Sun.COM 			}
2888525SEric.Schrock@Sun.COM 			/* FALLTHROUGH */
2898525SEric.Schrock@Sun.COM 		default:
2905094Slling 			(void) strlcpy(buf, "-", len);
2918525SEric.Schrock@Sun.COM 			break;
2928525SEric.Schrock@Sun.COM 		}
2938525SEric.Schrock@Sun.COM 
2948525SEric.Schrock@Sun.COM 		if (srctype != NULL)
2958525SEric.Schrock@Sun.COM 			*srctype = src;
2965094Slling 		return (0);
2975094Slling 	}
2985094Slling 
2995094Slling 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
3005094Slling 	    prop != ZPOOL_PROP_NAME)
3015094Slling 		return (-1);
3025094Slling 
3035094Slling 	switch (zpool_prop_get_type(prop)) {
3045094Slling 	case PROP_TYPE_STRING:
3055094Slling 		(void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
3065094Slling 		    len);
3075094Slling 		break;
3085094Slling 
3095094Slling 	case PROP_TYPE_NUMBER:
3105094Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
3115094Slling 
3125094Slling 		switch (prop) {
3135094Slling 		case ZPOOL_PROP_SIZE:
314*10956SGeorge.Wilson@Sun.COM 		case ZPOOL_PROP_ALLOCATED:
315*10956SGeorge.Wilson@Sun.COM 		case ZPOOL_PROP_FREE:
3165094Slling 			(void) zfs_nicenum(intval, buf, len);
3175094Slling 			break;
3185094Slling 
3195094Slling 		case ZPOOL_PROP_CAPACITY:
3205094Slling 			(void) snprintf(buf, len, "%llu%%",
3215094Slling 			    (u_longlong_t)intval);
3225094Slling 			break;
3235094Slling 
32410922SJeff.Bonwick@Sun.COM 		case ZPOOL_PROP_DEDUPRATIO:
32510922SJeff.Bonwick@Sun.COM 			(void) snprintf(buf, len, "%llu.%02llux",
32610922SJeff.Bonwick@Sun.COM 			    (u_longlong_t)(intval / 100),
32710922SJeff.Bonwick@Sun.COM 			    (u_longlong_t)(intval % 100));
32810922SJeff.Bonwick@Sun.COM 			break;
32910922SJeff.Bonwick@Sun.COM 
3305094Slling 		case ZPOOL_PROP_HEALTH:
3315094Slling 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
3325094Slling 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3335094Slling 			verify(nvlist_lookup_uint64_array(nvroot,
3345094Slling 			    ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
3355094Slling 
3365094Slling 			(void) strlcpy(buf, zpool_state_to_name(intval,
3375094Slling 			    vs->vs_aux), len);
3385094Slling 			break;
3395094Slling 		default:
3405094Slling 			(void) snprintf(buf, len, "%llu", intval);
3415094Slling 		}
3425094Slling 		break;
3435094Slling 
3445094Slling 	case PROP_TYPE_INDEX:
3455094Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
3465094Slling 		if (zpool_prop_index_to_string(prop, intval, &strval)
3475094Slling 		    != 0)
3485094Slling 			return (-1);
3495094Slling 		(void) strlcpy(buf, strval, len);
3505094Slling 		break;
3515094Slling 
3525094Slling 	default:
3535094Slling 		abort();
3545094Slling 	}
3555094Slling 
3565094Slling 	if (srctype)
3575094Slling 		*srctype = src;
3585094Slling 
3595094Slling 	return (0);
3605094Slling }
3615094Slling 
3625094Slling /*
3635094Slling  * Check if the bootfs name has the same pool name as it is set to.
3645094Slling  * Assuming bootfs is a valid dataset name.
3655094Slling  */
3665094Slling static boolean_t
3675094Slling bootfs_name_valid(const char *pool, char *bootfs)
3685094Slling {
3695094Slling 	int len = strlen(pool);
3705094Slling 
3717300SEric.Taylor@Sun.COM 	if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
3725094Slling 		return (B_FALSE);
3735094Slling 
3745094Slling 	if (strncmp(pool, bootfs, len) == 0 &&
3755094Slling 	    (bootfs[len] == '/' || bootfs[len] == '\0'))
3765094Slling 		return (B_TRUE);
3775094Slling 
3785094Slling 	return (B_FALSE);
3795094Slling }
3805094Slling 
3815094Slling /*
3827042Sgw25295  * Inspect the configuration to determine if any of the devices contain
3837042Sgw25295  * an EFI label.
3847042Sgw25295  */
3857042Sgw25295 static boolean_t
3867042Sgw25295 pool_uses_efi(nvlist_t *config)
3877042Sgw25295 {
3887042Sgw25295 	nvlist_t **child;
3897042Sgw25295 	uint_t c, children;
3907042Sgw25295 
3917042Sgw25295 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
3927042Sgw25295 	    &child, &children) != 0)
3937042Sgw25295 		return (read_efi_label(config, NULL) >= 0);
3947042Sgw25295 
3957042Sgw25295 	for (c = 0; c < children; c++) {
3967042Sgw25295 		if (pool_uses_efi(child[c]))
3977042Sgw25295 			return (B_TRUE);
3987042Sgw25295 	}
3997042Sgw25295 	return (B_FALSE);
4007042Sgw25295 }
4017042Sgw25295 
4027965SGeorge.Wilson@Sun.COM static boolean_t
4037965SGeorge.Wilson@Sun.COM pool_is_bootable(zpool_handle_t *zhp)
4047965SGeorge.Wilson@Sun.COM {
4057965SGeorge.Wilson@Sun.COM 	char bootfs[ZPOOL_MAXNAMELEN];
4067965SGeorge.Wilson@Sun.COM 
4077965SGeorge.Wilson@Sun.COM 	return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
4087965SGeorge.Wilson@Sun.COM 	    sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
4097965SGeorge.Wilson@Sun.COM 	    sizeof (bootfs)) != 0);
4107965SGeorge.Wilson@Sun.COM }
4117965SGeorge.Wilson@Sun.COM 
4127965SGeorge.Wilson@Sun.COM 
4137042Sgw25295 /*
4145094Slling  * Given an nvlist of zpool properties to be set, validate that they are
4155094Slling  * correct, and parse any numeric properties (index, boolean, etc) if they are
4165094Slling  * specified as strings.
4175094Slling  */
4185094Slling static nvlist_t *
4197184Stimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
4205094Slling     nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf)
4215094Slling {
4225094Slling 	nvpair_t *elem;
4235094Slling 	nvlist_t *retprops;
4245094Slling 	zpool_prop_t prop;
4255094Slling 	char *strval;
4265094Slling 	uint64_t intval;
4275363Seschrock 	char *slash;
4285363Seschrock 	struct stat64 statbuf;
4297042Sgw25295 	zpool_handle_t *zhp;
4307042Sgw25295 	nvlist_t *nvroot;
4315094Slling 
4325094Slling 	if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
4335094Slling 		(void) no_memory(hdl);
4345094Slling 		return (NULL);
4355094Slling 	}
4365094Slling 
4375094Slling 	elem = NULL;
4385094Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
4395094Slling 		const char *propname = nvpair_name(elem);
4405094Slling 
4415094Slling 		/*
4425094Slling 		 * Make sure this property is valid and applies to this type.
4435094Slling 		 */
4445094Slling 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
4455094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4465094Slling 			    "invalid property '%s'"), propname);
4475094Slling 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
4485094Slling 			goto error;
4495094Slling 		}
4505094Slling 
4515094Slling 		if (zpool_prop_readonly(prop)) {
4525094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
4535094Slling 			    "is readonly"), propname);
4545094Slling 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
4555094Slling 			goto error;
4565094Slling 		}
4575094Slling 
4585094Slling 		if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
4595094Slling 		    &strval, &intval, errbuf) != 0)
4605094Slling 			goto error;
4615094Slling 
4625094Slling 		/*
4635094Slling 		 * Perform additional checking for specific properties.
4645094Slling 		 */
4655094Slling 		switch (prop) {
4665094Slling 		case ZPOOL_PROP_VERSION:
4675094Slling 			if (intval < version || intval > SPA_VERSION) {
4685094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4695094Slling 				    "property '%s' number %d is invalid."),
4705094Slling 				    propname, intval);
4715094Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4725094Slling 				goto error;
4735094Slling 			}
4745094Slling 			break;
4755094Slling 
4765094Slling 		case ZPOOL_PROP_BOOTFS:
4775094Slling 			if (create_or_import) {
4785094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4795094Slling 				    "property '%s' cannot be set at creation "
4805094Slling 				    "or import time"), propname);
4815094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
4825094Slling 				goto error;
4835094Slling 			}
4845094Slling 
4855094Slling 			if (version < SPA_VERSION_BOOTFS) {
4865094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4875094Slling 				    "pool must be upgraded to support "
4885094Slling 				    "'%s' property"), propname);
4895094Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4905094Slling 				goto error;
4915094Slling 			}
4925094Slling 
4935094Slling 			/*
4945094Slling 			 * bootfs property value has to be a dataset name and
4955094Slling 			 * the dataset has to be in the same pool as it sets to.
4965094Slling 			 */
4975094Slling 			if (strval[0] != '\0' && !bootfs_name_valid(poolname,
4985094Slling 			    strval)) {
4995094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
5005094Slling 				    "is an invalid name"), strval);
5015094Slling 				(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
5025094Slling 				goto error;
5035094Slling 			}
5047042Sgw25295 
5057042Sgw25295 			if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
5067042Sgw25295 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5077042Sgw25295 				    "could not open pool '%s'"), poolname);
5087042Sgw25295 				(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
5097042Sgw25295 				goto error;
5107042Sgw25295 			}
5117042Sgw25295 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
5127042Sgw25295 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
5137042Sgw25295 
5147042Sgw25295 			/*
5157042Sgw25295 			 * bootfs property cannot be set on a disk which has
5167042Sgw25295 			 * been EFI labeled.
5177042Sgw25295 			 */
5187042Sgw25295 			if (pool_uses_efi(nvroot)) {
5197042Sgw25295 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5207042Sgw25295 				    "property '%s' not supported on "
5217042Sgw25295 				    "EFI labeled devices"), propname);
5227042Sgw25295 				(void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
5237042Sgw25295 				zpool_close(zhp);
5247042Sgw25295 				goto error;
5257042Sgw25295 			}
5267042Sgw25295 			zpool_close(zhp);
5275094Slling 			break;
5285094Slling 
5295094Slling 		case ZPOOL_PROP_ALTROOT:
5305094Slling 			if (!create_or_import) {
5315094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5325094Slling 				    "property '%s' can only be set during pool "
5335094Slling 				    "creation or import"), propname);
5345094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5355094Slling 				goto error;
5365094Slling 			}
5375094Slling 
5385094Slling 			if (strval[0] != '/') {
5395094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5405094Slling 				    "bad alternate root '%s'"), strval);
5415094Slling 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
5425094Slling 				goto error;
5435094Slling 			}
5445094Slling 			break;
5455363Seschrock 
5465363Seschrock 		case ZPOOL_PROP_CACHEFILE:
5475363Seschrock 			if (strval[0] == '\0')
5485363Seschrock 				break;
5495363Seschrock 
5505363Seschrock 			if (strcmp(strval, "none") == 0)
5515363Seschrock 				break;
5525363Seschrock 
5535363Seschrock 			if (strval[0] != '/') {
5545363Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5555363Seschrock 				    "property '%s' must be empty, an "
5565363Seschrock 				    "absolute path, or 'none'"), propname);
5575363Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
5585363Seschrock 				goto error;
5595363Seschrock 			}
5605363Seschrock 
5615363Seschrock 			slash = strrchr(strval, '/');
5625363Seschrock 
5635363Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
5645363Seschrock 			    strcmp(slash, "/..") == 0) {
5655363Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5665363Seschrock 				    "'%s' is not a valid file"), strval);
5675363Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
5685363Seschrock 				goto error;
5695363Seschrock 			}
5705363Seschrock 
5715363Seschrock 			*slash = '\0';
5725363Seschrock 
5735621Seschrock 			if (strval[0] != '\0' &&
5745621Seschrock 			    (stat64(strval, &statbuf) != 0 ||
5755621Seschrock 			    !S_ISDIR(statbuf.st_mode))) {
5765363Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5775363Seschrock 				    "'%s' is not a valid directory"),
5785363Seschrock 				    strval);
5795363Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
5805363Seschrock 				goto error;
5815363Seschrock 			}
5825363Seschrock 
5835363Seschrock 			*slash = '/';
5845363Seschrock 			break;
5855094Slling 		}
5865094Slling 	}
5875094Slling 
5885094Slling 	return (retprops);
5895094Slling error:
5905094Slling 	nvlist_free(retprops);
5915094Slling 	return (NULL);
5925094Slling }
5935094Slling 
5945094Slling /*
5955094Slling  * Set zpool property : propname=propval.
5965094Slling  */
5975094Slling int
5985094Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
5995094Slling {
6005094Slling 	zfs_cmd_t zc = { 0 };
6015094Slling 	int ret = -1;
6025094Slling 	char errbuf[1024];
6035094Slling 	nvlist_t *nvl = NULL;
6045094Slling 	nvlist_t *realprops;
6055094Slling 	uint64_t version;
6065094Slling 
6075094Slling 	(void) snprintf(errbuf, sizeof (errbuf),
6085094Slling 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
6095094Slling 	    zhp->zpool_name);
6105094Slling 
6115094Slling 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
6125094Slling 		return (no_memory(zhp->zpool_hdl));
6135094Slling 
6145094Slling 	if (nvlist_add_string(nvl, propname, propval) != 0) {
6155094Slling 		nvlist_free(nvl);
6165094Slling 		return (no_memory(zhp->zpool_hdl));
6175094Slling 	}
6185094Slling 
6195094Slling 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
6207184Stimh 	if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
6215094Slling 	    zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) {
6225094Slling 		nvlist_free(nvl);
6235094Slling 		return (-1);
6245094Slling 	}
6255094Slling 
6265094Slling 	nvlist_free(nvl);
6275094Slling 	nvl = realprops;
6285094Slling 
6295094Slling 	/*
6305094Slling 	 * Execute the corresponding ioctl() to set this property.
6315094Slling 	 */
6325094Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
6335094Slling 
6345094Slling 	if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
6355094Slling 		nvlist_free(nvl);
6365094Slling 		return (-1);
6375094Slling 	}
6385094Slling 
6395094Slling 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
6405094Slling 
6415094Slling 	zcmd_free_nvlists(&zc);
6425094Slling 	nvlist_free(nvl);
6435094Slling 
6445094Slling 	if (ret)
6455094Slling 		(void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
6465094Slling 	else
6475094Slling 		(void) zpool_props_refresh(zhp);
6485094Slling 
6495094Slling 	return (ret);
6505094Slling }
6515094Slling 
6525094Slling int
6535094Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
6545094Slling {
6555094Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
6565094Slling 	zprop_list_t *entry;
6575094Slling 	char buf[ZFS_MAXPROPLEN];
6585094Slling 
6595094Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
6605094Slling 		return (-1);
6615094Slling 
6625094Slling 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
6635094Slling 
6645094Slling 		if (entry->pl_fixed)
6655094Slling 			continue;
6665094Slling 
6675094Slling 		if (entry->pl_prop != ZPROP_INVAL &&
6685094Slling 		    zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
6695094Slling 		    NULL) == 0) {
6705094Slling 			if (strlen(buf) > entry->pl_width)
6715094Slling 				entry->pl_width = strlen(buf);
6725094Slling 		}
6735094Slling 	}
6745094Slling 
6755094Slling 	return (0);
6765094Slling }
6775094Slling 
6785094Slling 
679789Sahrens /*
6809816SGeorge.Wilson@Sun.COM  * Don't start the slice at the default block of 34; many storage
6819816SGeorge.Wilson@Sun.COM  * devices will use a stripe width of 128k, so start there instead.
6829816SGeorge.Wilson@Sun.COM  */
6839816SGeorge.Wilson@Sun.COM #define	NEW_START_BLOCK	256
6849816SGeorge.Wilson@Sun.COM 
6859816SGeorge.Wilson@Sun.COM /*
686789Sahrens  * Validate the given pool name, optionally putting an extended error message in
687789Sahrens  * 'buf'.
688789Sahrens  */
6896423Sgw25295 boolean_t
6902082Seschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
691789Sahrens {
692789Sahrens 	namecheck_err_t why;
693789Sahrens 	char what;
6941773Seschrock 	int ret;
695789Sahrens 
6961773Seschrock 	ret = pool_namecheck(pool, &why, &what);
6971773Seschrock 
6981773Seschrock 	/*
6991773Seschrock 	 * The rules for reserved pool names were extended at a later point.
7001773Seschrock 	 * But we need to support users with existing pools that may now be
7011773Seschrock 	 * invalid.  So we only check for this expanded set of names during a
7021773Seschrock 	 * create (or import), and only in userland.
7031773Seschrock 	 */
7041773Seschrock 	if (ret == 0 && !isopen &&
7051773Seschrock 	    (strncmp(pool, "mirror", 6) == 0 ||
7061773Seschrock 	    strncmp(pool, "raidz", 5) == 0 ||
7074527Sperrin 	    strncmp(pool, "spare", 5) == 0 ||
7084527Sperrin 	    strcmp(pool, "log") == 0)) {
7096423Sgw25295 		if (hdl != NULL)
7106423Sgw25295 			zfs_error_aux(hdl,
7116423Sgw25295 			    dgettext(TEXT_DOMAIN, "name is reserved"));
7122082Seschrock 		return (B_FALSE);
7131773Seschrock 	}
7141773Seschrock 
7151773Seschrock 
7161773Seschrock 	if (ret != 0) {
7172082Seschrock 		if (hdl != NULL) {
718789Sahrens 			switch (why) {
7191003Slling 			case NAME_ERR_TOOLONG:
7202082Seschrock 				zfs_error_aux(hdl,
7211003Slling 				    dgettext(TEXT_DOMAIN, "name is too long"));
7221003Slling 				break;
7231003Slling 
724789Sahrens 			case NAME_ERR_INVALCHAR:
7252082Seschrock 				zfs_error_aux(hdl,
726789Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
727789Sahrens 				    "'%c' in pool name"), what);
728789Sahrens 				break;
729789Sahrens 
730789Sahrens 			case NAME_ERR_NOLETTER:
7312082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7322082Seschrock 				    "name must begin with a letter"));
733789Sahrens 				break;
734789Sahrens 
735789Sahrens 			case NAME_ERR_RESERVED:
7362082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7372082Seschrock 				    "name is reserved"));
738789Sahrens 				break;
739789Sahrens 
740789Sahrens 			case NAME_ERR_DISKLIKE:
7412082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7422082Seschrock 				    "pool name is reserved"));
743789Sahrens 				break;
7442856Snd150628 
7452856Snd150628 			case NAME_ERR_LEADING_SLASH:
7462856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7472856Snd150628 				    "leading slash in name"));
7482856Snd150628 				break;
7492856Snd150628 
7502856Snd150628 			case NAME_ERR_EMPTY_COMPONENT:
7512856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7522856Snd150628 				    "empty component in name"));
7532856Snd150628 				break;
7542856Snd150628 
7552856Snd150628 			case NAME_ERR_TRAILING_SLASH:
7562856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7572856Snd150628 				    "trailing slash in name"));
7582856Snd150628 				break;
7592856Snd150628 
7602856Snd150628 			case NAME_ERR_MULTIPLE_AT:
7612856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7622856Snd150628 				    "multiple '@' delimiters in name"));
7632856Snd150628 				break;
7642856Snd150628 
765789Sahrens 			}
766789Sahrens 		}
7672082Seschrock 		return (B_FALSE);
768789Sahrens 	}
769789Sahrens 
7702082Seschrock 	return (B_TRUE);
771789Sahrens }
772789Sahrens 
773789Sahrens /*
774789Sahrens  * Open a handle to the given pool, even if the pool is currently in the FAULTED
775789Sahrens  * state.
776789Sahrens  */
777789Sahrens zpool_handle_t *
7782082Seschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
779789Sahrens {
780789Sahrens 	zpool_handle_t *zhp;
7812142Seschrock 	boolean_t missing;
782789Sahrens 
783789Sahrens 	/*
784789Sahrens 	 * Make sure the pool name is valid.
785789Sahrens 	 */
7862082Seschrock 	if (!zpool_name_valid(hdl, B_TRUE, pool)) {
7873237Slling 		(void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
7882082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
7892082Seschrock 		    pool);
790789Sahrens 		return (NULL);
791789Sahrens 	}
792789Sahrens 
7932082Seschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
7942082Seschrock 		return (NULL);
795789Sahrens 
7962082Seschrock 	zhp->zpool_hdl = hdl;
797789Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
798789Sahrens 
7992142Seschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
8002142Seschrock 		zpool_close(zhp);
8012142Seschrock 		return (NULL);
8022142Seschrock 	}
8032142Seschrock 
8042142Seschrock 	if (missing) {
8055094Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
8063237Slling 		(void) zfs_error_fmt(hdl, EZFS_NOENT,
8075094Slling 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
8082142Seschrock 		zpool_close(zhp);
8092142Seschrock 		return (NULL);
810789Sahrens 	}
811789Sahrens 
812789Sahrens 	return (zhp);
813789Sahrens }
814789Sahrens 
815789Sahrens /*
816789Sahrens  * Like the above, but silent on error.  Used when iterating over pools (because
817789Sahrens  * the configuration cache may be out of date).
818789Sahrens  */
8192142Seschrock int
8202142Seschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
821789Sahrens {
822789Sahrens 	zpool_handle_t *zhp;
8232142Seschrock 	boolean_t missing;
824789Sahrens 
8252142Seschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
8262142Seschrock 		return (-1);
827789Sahrens 
8282082Seschrock 	zhp->zpool_hdl = hdl;
829789Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
830789Sahrens 
8312142Seschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
8322142Seschrock 		zpool_close(zhp);
8332142Seschrock 		return (-1);
834789Sahrens 	}
835789Sahrens 
8362142Seschrock 	if (missing) {
8372142Seschrock 		zpool_close(zhp);
8382142Seschrock 		*ret = NULL;
8392142Seschrock 		return (0);
8402142Seschrock 	}
8412142Seschrock 
8422142Seschrock 	*ret = zhp;
8432142Seschrock 	return (0);
844789Sahrens }
845789Sahrens 
846789Sahrens /*
847789Sahrens  * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
848789Sahrens  * state.
849789Sahrens  */
850789Sahrens zpool_handle_t *
8512082Seschrock zpool_open(libzfs_handle_t *hdl, const char *pool)
852789Sahrens {
853789Sahrens 	zpool_handle_t *zhp;
854789Sahrens 
8552082Seschrock 	if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
856789Sahrens 		return (NULL);
857789Sahrens 
858789Sahrens 	if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
8593237Slling 		(void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
8602082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
861789Sahrens 		zpool_close(zhp);
862789Sahrens 		return (NULL);
863789Sahrens 	}
864789Sahrens 
865789Sahrens 	return (zhp);
866789Sahrens }
867789Sahrens 
868789Sahrens /*
869789Sahrens  * Close the handle.  Simply frees the memory associated with the handle.
870789Sahrens  */
871789Sahrens void
872789Sahrens zpool_close(zpool_handle_t *zhp)
873789Sahrens {
874789Sahrens 	if (zhp->zpool_config)
875789Sahrens 		nvlist_free(zhp->zpool_config);
876952Seschrock 	if (zhp->zpool_old_config)
877952Seschrock 		nvlist_free(zhp->zpool_old_config);
8783912Slling 	if (zhp->zpool_props)
8793912Slling 		nvlist_free(zhp->zpool_props);
880789Sahrens 	free(zhp);
881789Sahrens }
882789Sahrens 
883789Sahrens /*
884789Sahrens  * Return the name of the pool.
885789Sahrens  */
886789Sahrens const char *
887789Sahrens zpool_get_name(zpool_handle_t *zhp)
888789Sahrens {
889789Sahrens 	return (zhp->zpool_name);
890789Sahrens }
891789Sahrens 
892789Sahrens 
893789Sahrens /*
894789Sahrens  * Return the state of the pool (ACTIVE or UNAVAILABLE)
895789Sahrens  */
896789Sahrens int
897789Sahrens zpool_get_state(zpool_handle_t *zhp)
898789Sahrens {
899789Sahrens 	return (zhp->zpool_state);
900789Sahrens }
901789Sahrens 
902789Sahrens /*
903789Sahrens  * Create the named pool, using the provided vdev list.  It is assumed
904789Sahrens  * that the consumer has already validated the contents of the nvlist, so we
905789Sahrens  * don't have to worry about error semantics.
906789Sahrens  */
907789Sahrens int
9082082Seschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
9097184Stimh     nvlist_t *props, nvlist_t *fsprops)
910789Sahrens {
911789Sahrens 	zfs_cmd_t zc = { 0 };
9127184Stimh 	nvlist_t *zc_fsprops = NULL;
9137184Stimh 	nvlist_t *zc_props = NULL;
9142082Seschrock 	char msg[1024];
9155094Slling 	char *altroot;
9167184Stimh 	int ret = -1;
9172082Seschrock 
9182082Seschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
9192082Seschrock 	    "cannot create '%s'"), pool);
920789Sahrens 
9212082Seschrock 	if (!zpool_name_valid(hdl, B_FALSE, pool))
9222082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
9232082Seschrock 
9245320Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
9255320Slling 		return (-1);
9265320Slling 
9277184Stimh 	if (props) {
9287184Stimh 		if ((zc_props = zpool_valid_proplist(hdl, pool, props,
9297184Stimh 		    SPA_VERSION_1, B_TRUE, msg)) == NULL) {
9307184Stimh 			goto create_failed;
9317184Stimh 		}
9325320Slling 	}
933789Sahrens 
9347184Stimh 	if (fsprops) {
9357184Stimh 		uint64_t zoned;
9367184Stimh 		char *zonestr;
9377184Stimh 
9387184Stimh 		zoned = ((nvlist_lookup_string(fsprops,
9397184Stimh 		    zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
9407184Stimh 		    strcmp(zonestr, "on") == 0);
9417184Stimh 
9427184Stimh 		if ((zc_fsprops = zfs_valid_proplist(hdl,
9437184Stimh 		    ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
9447184Stimh 			goto create_failed;
9457184Stimh 		}
9467184Stimh 		if (!zc_props &&
9477184Stimh 		    (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
9487184Stimh 			goto create_failed;
9497184Stimh 		}
9507184Stimh 		if (nvlist_add_nvlist(zc_props,
9517184Stimh 		    ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
9527184Stimh 			goto create_failed;
9537184Stimh 		}
9547184Stimh 	}
9557184Stimh 
9567184Stimh 	if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
9577184Stimh 		goto create_failed;
9587184Stimh 
959789Sahrens 	(void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
960789Sahrens 
9617184Stimh 	if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
9625320Slling 
9632676Seschrock 		zcmd_free_nvlists(&zc);
9647184Stimh 		nvlist_free(zc_props);
9657184Stimh 		nvlist_free(zc_fsprops);
9662082Seschrock 
967789Sahrens 		switch (errno) {
968789Sahrens 		case EBUSY:
969789Sahrens 			/*
970789Sahrens 			 * This can happen if the user has specified the same
971789Sahrens 			 * device multiple times.  We can't reliably detect this
972789Sahrens 			 * until we try to add it and see we already have a
973789Sahrens 			 * label.
974789Sahrens 			 */
9752082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9762082Seschrock 			    "one or more vdevs refer to the same device"));
9772082Seschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
978789Sahrens 
979789Sahrens 		case EOVERFLOW:
980789Sahrens 			/*
9812082Seschrock 			 * This occurs when one of the devices is below
982789Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
983789Sahrens 			 * device was the problem device since there's no
984789Sahrens 			 * reliable way to determine device size from userland.
985789Sahrens 			 */
986789Sahrens 			{
987789Sahrens 				char buf[64];
988789Sahrens 
989789Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
990789Sahrens 
9912082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9922082Seschrock 				    "one or more devices is less than the "
9932082Seschrock 				    "minimum size (%s)"), buf);
994789Sahrens 			}
9952082Seschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
996789Sahrens 
997789Sahrens 		case ENOSPC:
9982082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9992082Seschrock 			    "one or more devices is out of space"));
10002082Seschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
1001789Sahrens 
10025450Sbrendan 		case ENOTBLK:
10035450Sbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10045450Sbrendan 			    "cache device must be a disk or disk slice"));
10055450Sbrendan 			return (zfs_error(hdl, EZFS_BADDEV, msg));
10065450Sbrendan 
1007789Sahrens 		default:
10082082Seschrock 			return (zpool_standard_error(hdl, errno, msg));
1009789Sahrens 		}
1010789Sahrens 	}
1011789Sahrens 
1012789Sahrens 	/*
1013789Sahrens 	 * If this is an alternate root pool, then we automatically set the
10142676Seschrock 	 * mountpoint of the root dataset to be '/'.
1015789Sahrens 	 */
10165094Slling 	if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
10175094Slling 	    &altroot) == 0) {
1018789Sahrens 		zfs_handle_t *zhp;
1019789Sahrens 
10205094Slling 		verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
10212676Seschrock 		verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
10222676Seschrock 		    "/") == 0);
1023789Sahrens 
1024789Sahrens 		zfs_close(zhp);
1025789Sahrens 	}
1026789Sahrens 
10277184Stimh create_failed:
10285320Slling 	zcmd_free_nvlists(&zc);
10297184Stimh 	nvlist_free(zc_props);
10307184Stimh 	nvlist_free(zc_fsprops);
10317184Stimh 	return (ret);
1032789Sahrens }
1033789Sahrens 
1034789Sahrens /*
1035789Sahrens  * Destroy the given pool.  It is up to the caller to ensure that there are no
1036789Sahrens  * datasets left in the pool.
1037789Sahrens  */
1038789Sahrens int
1039789Sahrens zpool_destroy(zpool_handle_t *zhp)
1040789Sahrens {
1041789Sahrens 	zfs_cmd_t zc = { 0 };
1042789Sahrens 	zfs_handle_t *zfp = NULL;
10432082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
10442082Seschrock 	char msg[1024];
1045789Sahrens 
1046789Sahrens 	if (zhp->zpool_state == POOL_STATE_ACTIVE &&
10472082Seschrock 	    (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name,
10482082Seschrock 	    ZFS_TYPE_FILESYSTEM)) == NULL)
1049789Sahrens 		return (-1);
1050789Sahrens 
1051789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1052789Sahrens 
10534543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
10542082Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
10552082Seschrock 		    "cannot destroy '%s'"), zhp->zpool_name);
1056789Sahrens 
10572082Seschrock 		if (errno == EROFS) {
10582082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10592082Seschrock 			    "one or more devices is read only"));
10602082Seschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
10612082Seschrock 		} else {
10622082Seschrock 			(void) zpool_standard_error(hdl, errno, msg);
1063789Sahrens 		}
1064789Sahrens 
1065789Sahrens 		if (zfp)
1066789Sahrens 			zfs_close(zfp);
1067789Sahrens 		return (-1);
1068789Sahrens 	}
1069789Sahrens 
1070789Sahrens 	if (zfp) {
1071789Sahrens 		remove_mountpoint(zfp);
1072789Sahrens 		zfs_close(zfp);
1073789Sahrens 	}
1074789Sahrens 
1075789Sahrens 	return (0);
1076789Sahrens }
1077789Sahrens 
1078789Sahrens /*
1079789Sahrens  * Add the given vdevs to the pool.  The caller must have already performed the
1080789Sahrens  * necessary verification to ensure that the vdev specification is well-formed.
1081789Sahrens  */
1082789Sahrens int
1083789Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1084789Sahrens {
10852676Seschrock 	zfs_cmd_t zc = { 0 };
10862082Seschrock 	int ret;
10872082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
10882082Seschrock 	char msg[1024];
10895450Sbrendan 	nvlist_t **spares, **l2cache;
10905450Sbrendan 	uint_t nspares, nl2cache;
10912082Seschrock 
10922082Seschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
10932082Seschrock 	    "cannot add to '%s'"), zhp->zpool_name);
10942082Seschrock 
10955450Sbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
10965450Sbrendan 	    SPA_VERSION_SPARES &&
10972082Seschrock 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
10982082Seschrock 	    &spares, &nspares) == 0) {
10992082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
11002082Seschrock 		    "upgraded to add hot spares"));
11012082Seschrock 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
11022082Seschrock 	}
1103789Sahrens 
11047965SGeorge.Wilson@Sun.COM 	if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
11057965SGeorge.Wilson@Sun.COM 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
11067965SGeorge.Wilson@Sun.COM 		uint64_t s;
11077965SGeorge.Wilson@Sun.COM 
11087965SGeorge.Wilson@Sun.COM 		for (s = 0; s < nspares; s++) {
11097965SGeorge.Wilson@Sun.COM 			char *path;
11107965SGeorge.Wilson@Sun.COM 
11117965SGeorge.Wilson@Sun.COM 			if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
11127965SGeorge.Wilson@Sun.COM 			    &path) == 0 && pool_uses_efi(spares[s])) {
11137965SGeorge.Wilson@Sun.COM 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11147965SGeorge.Wilson@Sun.COM 				    "device '%s' contains an EFI label and "
11157965SGeorge.Wilson@Sun.COM 				    "cannot be used on root pools."),
111610594SGeorge.Wilson@Sun.COM 				    zpool_vdev_name(hdl, NULL, spares[s],
111710594SGeorge.Wilson@Sun.COM 				    B_FALSE));
11187965SGeorge.Wilson@Sun.COM 				return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
11197965SGeorge.Wilson@Sun.COM 			}
11207965SGeorge.Wilson@Sun.COM 		}
11217965SGeorge.Wilson@Sun.COM 	}
11227965SGeorge.Wilson@Sun.COM 
11235450Sbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
11245450Sbrendan 	    SPA_VERSION_L2CACHE &&
11255450Sbrendan 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
11265450Sbrendan 	    &l2cache, &nl2cache) == 0) {
11275450Sbrendan 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
11285450Sbrendan 		    "upgraded to add cache devices"));
11295450Sbrendan 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
11305450Sbrendan 	}
11315450Sbrendan 
11325094Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
11332082Seschrock 		return (-1);
1134789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1135789Sahrens 
11364543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1137789Sahrens 		switch (errno) {
1138789Sahrens 		case EBUSY:
1139789Sahrens 			/*
1140789Sahrens 			 * This can happen if the user has specified the same
1141789Sahrens 			 * device multiple times.  We can't reliably detect this
1142789Sahrens 			 * until we try to add it and see we already have a
1143789Sahrens 			 * label.
1144789Sahrens 			 */
11452082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11462082Seschrock 			    "one or more vdevs refer to the same device"));
11472082Seschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1148789Sahrens 			break;
1149789Sahrens 
1150789Sahrens 		case EOVERFLOW:
1151789Sahrens 			/*
1152789Sahrens 			 * This occurrs when one of the devices is below
1153789Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1154789Sahrens 			 * device was the problem device since there's no
1155789Sahrens 			 * reliable way to determine device size from userland.
1156789Sahrens 			 */
1157789Sahrens 			{
1158789Sahrens 				char buf[64];
1159789Sahrens 
1160789Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1161789Sahrens 
11622082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11632082Seschrock 				    "device is less than the minimum "
11642082Seschrock 				    "size (%s)"), buf);
1165789Sahrens 			}
11662082Seschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
11672082Seschrock 			break;
11682082Seschrock 
11692082Seschrock 		case ENOTSUP:
11702082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11714527Sperrin 			    "pool must be upgraded to add these vdevs"));
11722082Seschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, msg);
1173789Sahrens 			break;
1174789Sahrens 
11753912Slling 		case EDOM:
11763912Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11774527Sperrin 			    "root pool can not have multiple vdevs"
11784527Sperrin 			    " or separate logs"));
11793912Slling 			(void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
11803912Slling 			break;
11813912Slling 
11825450Sbrendan 		case ENOTBLK:
11835450Sbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11845450Sbrendan 			    "cache device must be a disk or disk slice"));
11855450Sbrendan 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
11865450Sbrendan 			break;
11875450Sbrendan 
1188789Sahrens 		default:
11892082Seschrock 			(void) zpool_standard_error(hdl, errno, msg);
1190789Sahrens 		}
1191789Sahrens 
11922082Seschrock 		ret = -1;
11932082Seschrock 	} else {
11942082Seschrock 		ret = 0;
1195789Sahrens 	}
1196789Sahrens 
11972676Seschrock 	zcmd_free_nvlists(&zc);
1198789Sahrens 
11992082Seschrock 	return (ret);
1200789Sahrens }
1201789Sahrens 
1202789Sahrens /*
1203789Sahrens  * Exports the pool from the system.  The caller must ensure that there are no
1204789Sahrens  * mounted datasets in the pool.
1205789Sahrens  */
1206789Sahrens int
12078211SGeorge.Wilson@Sun.COM zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce)
1208789Sahrens {
1209789Sahrens 	zfs_cmd_t zc = { 0 };
12107214Slling 	char msg[1024];
1211789Sahrens 
12127214Slling 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
12137214Slling 	    "cannot export '%s'"), zhp->zpool_name);
12147214Slling 
1215789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
12167214Slling 	zc.zc_cookie = force;
12178211SGeorge.Wilson@Sun.COM 	zc.zc_guid = hardforce;
12187214Slling 
12197214Slling 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
12207214Slling 		switch (errno) {
12217214Slling 		case EXDEV:
12227214Slling 			zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
12237214Slling 			    "use '-f' to override the following errors:\n"
12247214Slling 			    "'%s' has an active shared spare which could be"
12257214Slling 			    " used by other pools once '%s' is exported."),
12267214Slling 			    zhp->zpool_name, zhp->zpool_name);
12277214Slling 			return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
12287214Slling 			    msg));
12297214Slling 		default:
12307214Slling 			return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
12317214Slling 			    msg));
12327214Slling 		}
12337214Slling 	}
12347214Slling 
1235789Sahrens 	return (0);
1236789Sahrens }
1237789Sahrens 
12388211SGeorge.Wilson@Sun.COM int
12398211SGeorge.Wilson@Sun.COM zpool_export(zpool_handle_t *zhp, boolean_t force)
12408211SGeorge.Wilson@Sun.COM {
12418211SGeorge.Wilson@Sun.COM 	return (zpool_export_common(zhp, force, B_FALSE));
12428211SGeorge.Wilson@Sun.COM }
12438211SGeorge.Wilson@Sun.COM 
12448211SGeorge.Wilson@Sun.COM int
12458211SGeorge.Wilson@Sun.COM zpool_export_force(zpool_handle_t *zhp)
12468211SGeorge.Wilson@Sun.COM {
12478211SGeorge.Wilson@Sun.COM 	return (zpool_export_common(zhp, B_TRUE, B_TRUE));
12488211SGeorge.Wilson@Sun.COM }
12498211SGeorge.Wilson@Sun.COM 
125010921STim.Haley@Sun.COM static void
125110921STim.Haley@Sun.COM zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
125210921STim.Haley@Sun.COM     nvlist_t *rbi)
125310921STim.Haley@Sun.COM {
125410921STim.Haley@Sun.COM 	uint64_t rewindto;
125510921STim.Haley@Sun.COM 	int64_t loss = -1;
125610921STim.Haley@Sun.COM 	struct tm t;
125710921STim.Haley@Sun.COM 	char timestr[128];
125810921STim.Haley@Sun.COM 
125910921STim.Haley@Sun.COM 	if (!hdl->libzfs_printerr || rbi == NULL)
126010921STim.Haley@Sun.COM 		return;
126110921STim.Haley@Sun.COM 
126210921STim.Haley@Sun.COM 	if (nvlist_lookup_uint64(rbi, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
126310921STim.Haley@Sun.COM 		return;
126410921STim.Haley@Sun.COM 	(void) nvlist_lookup_int64(rbi, ZPOOL_CONFIG_REWIND_TIME, &loss);
126510921STim.Haley@Sun.COM 
126610921STim.Haley@Sun.COM 	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
126710921STim.Haley@Sun.COM 	    strftime(timestr, 128, 0, &t) != 0) {
126810921STim.Haley@Sun.COM 		if (dryrun) {
126910921STim.Haley@Sun.COM 			(void) printf(dgettext(TEXT_DOMAIN,
127010921STim.Haley@Sun.COM 			    "Would be able to return %s "
127110921STim.Haley@Sun.COM 			    "to its state as of %s.\n"),
127210921STim.Haley@Sun.COM 			    name, timestr);
127310921STim.Haley@Sun.COM 		} else {
127410921STim.Haley@Sun.COM 			(void) printf(dgettext(TEXT_DOMAIN,
127510921STim.Haley@Sun.COM 			    "Pool %s returned to its state as of %s.\n"),
127610921STim.Haley@Sun.COM 			    name, timestr);
127710921STim.Haley@Sun.COM 		}
127810921STim.Haley@Sun.COM 		if (loss > 120) {
127910921STim.Haley@Sun.COM 			(void) printf(dgettext(TEXT_DOMAIN,
128010921STim.Haley@Sun.COM 			    "%s approximately %lld "),
128110921STim.Haley@Sun.COM 			    dryrun ? "Would discard" : "Discarded",
128210921STim.Haley@Sun.COM 			    (loss + 30) / 60);
128310921STim.Haley@Sun.COM 			(void) printf(dgettext(TEXT_DOMAIN,
128410921STim.Haley@Sun.COM 			    "minutes of transactions.\n"));
128510921STim.Haley@Sun.COM 		} else if (loss > 0) {
128610921STim.Haley@Sun.COM 			(void) printf(dgettext(TEXT_DOMAIN,
128710921STim.Haley@Sun.COM 			    "%s approximately %lld "),
128810921STim.Haley@Sun.COM 			    dryrun ? "Would discard" : "Discarded", loss);
128910921STim.Haley@Sun.COM 			(void) printf(dgettext(TEXT_DOMAIN,
129010921STim.Haley@Sun.COM 			    "seconds of transactions.\n"));
129110921STim.Haley@Sun.COM 		}
129210921STim.Haley@Sun.COM 	}
129310921STim.Haley@Sun.COM }
129410921STim.Haley@Sun.COM 
129510921STim.Haley@Sun.COM void
129610921STim.Haley@Sun.COM zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
129710921STim.Haley@Sun.COM     nvlist_t *config)
129810921STim.Haley@Sun.COM {
129910921STim.Haley@Sun.COM 	int64_t loss = -1;
130010921STim.Haley@Sun.COM 	uint64_t edata = UINT64_MAX;
130110921STim.Haley@Sun.COM 	uint64_t rewindto;
130210921STim.Haley@Sun.COM 	struct tm t;
130310921STim.Haley@Sun.COM 	char timestr[128];
130410921STim.Haley@Sun.COM 
130510921STim.Haley@Sun.COM 	if (!hdl->libzfs_printerr)
130610921STim.Haley@Sun.COM 		return;
130710921STim.Haley@Sun.COM 
130810921STim.Haley@Sun.COM 	if (reason >= 0)
130910921STim.Haley@Sun.COM 		(void) printf(dgettext(TEXT_DOMAIN, "action: "));
131010921STim.Haley@Sun.COM 	else
131110921STim.Haley@Sun.COM 		(void) printf(dgettext(TEXT_DOMAIN, "\t"));
131210921STim.Haley@Sun.COM 
131310921STim.Haley@Sun.COM 	/* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
131410921STim.Haley@Sun.COM 	if (nvlist_lookup_uint64(config,
131510921STim.Haley@Sun.COM 	    ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
131610921STim.Haley@Sun.COM 		goto no_info;
131710921STim.Haley@Sun.COM 
131810921STim.Haley@Sun.COM 	(void) nvlist_lookup_int64(config, ZPOOL_CONFIG_REWIND_TIME, &loss);
131910921STim.Haley@Sun.COM 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
132010921STim.Haley@Sun.COM 	    &edata);
132110921STim.Haley@Sun.COM 
132210921STim.Haley@Sun.COM 	(void) printf(dgettext(TEXT_DOMAIN,
132310921STim.Haley@Sun.COM 	    "Recovery is possible, but will result in some data loss.\n"));
132410921STim.Haley@Sun.COM 
132510921STim.Haley@Sun.COM 	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
132610921STim.Haley@Sun.COM 	    strftime(timestr, 128, 0, &t) != 0) {
132710921STim.Haley@Sun.COM 		(void) printf(dgettext(TEXT_DOMAIN,
132810921STim.Haley@Sun.COM 		    "\tReturning the pool to its state as of %s\n"
132910921STim.Haley@Sun.COM 		    "\tshould correct the problem.  "),
133010921STim.Haley@Sun.COM 		    timestr);
133110921STim.Haley@Sun.COM 	} else {
133210921STim.Haley@Sun.COM 		(void) printf(dgettext(TEXT_DOMAIN,
133310921STim.Haley@Sun.COM 		    "\tReverting the pool to an earlier state "
133410921STim.Haley@Sun.COM 		    "should correct the problem.\n\t"));
133510921STim.Haley@Sun.COM 	}
133610921STim.Haley@Sun.COM 
133710921STim.Haley@Sun.COM 	if (loss > 120) {
133810921STim.Haley@Sun.COM 		(void) printf(dgettext(TEXT_DOMAIN,
133910921STim.Haley@Sun.COM 		    "Approximately %lld minutes of data\n"
134010921STim.Haley@Sun.COM 		    "\tmust be discarded, irreversibly.  "), (loss + 30) / 60);
134110921STim.Haley@Sun.COM 	} else if (loss > 0) {
134210921STim.Haley@Sun.COM 		(void) printf(dgettext(TEXT_DOMAIN,
134310921STim.Haley@Sun.COM 		    "Approximately %lld seconds of data\n"
134410921STim.Haley@Sun.COM 		    "\tmust be discarded, irreversibly.  "), loss);
134510921STim.Haley@Sun.COM 	}
134610921STim.Haley@Sun.COM 	if (edata != 0 && edata != UINT64_MAX) {
134710921STim.Haley@Sun.COM 		if (edata == 1) {
134810921STim.Haley@Sun.COM 			(void) printf(dgettext(TEXT_DOMAIN,
134910921STim.Haley@Sun.COM 			    "After rewind, at least\n"
135010921STim.Haley@Sun.COM 			    "\tone persistent user-data error will remain.  "));
135110921STim.Haley@Sun.COM 		} else {
135210921STim.Haley@Sun.COM 			(void) printf(dgettext(TEXT_DOMAIN,
135310921STim.Haley@Sun.COM 			    "After rewind, several\n"
135410921STim.Haley@Sun.COM 			    "\tpersistent user-data errors will remain.  "));
135510921STim.Haley@Sun.COM 		}
135610921STim.Haley@Sun.COM 	}
135710921STim.Haley@Sun.COM 	(void) printf(dgettext(TEXT_DOMAIN,
135810921STim.Haley@Sun.COM 	    "Recovery can be\n\tattempted by executing "
135910921STim.Haley@Sun.COM 	    "'zpool %s -F %s'.  "), reason >= 0 ? "clear" : "import", name);
136010921STim.Haley@Sun.COM 
136110921STim.Haley@Sun.COM 	(void) printf(dgettext(TEXT_DOMAIN,
136210921STim.Haley@Sun.COM 	    "A scrub of the pool\n"
136310921STim.Haley@Sun.COM 	    "\tis strongly recommended after recovery.\n"));
136410921STim.Haley@Sun.COM 	return;
136510921STim.Haley@Sun.COM 
136610921STim.Haley@Sun.COM no_info:
136710921STim.Haley@Sun.COM 	(void) printf(dgettext(TEXT_DOMAIN,
136810921STim.Haley@Sun.COM 	    "Destroy and re-create the pool from\n\ta backup source.\n"));
136910921STim.Haley@Sun.COM }
137010921STim.Haley@Sun.COM 
1371789Sahrens /*
13725094Slling  * zpool_import() is a contracted interface. Should be kept the same
13735094Slling  * if possible.
13745094Slling  *
13755094Slling  * Applications should use zpool_import_props() to import a pool with
13765094Slling  * new properties value to be set.
1377789Sahrens  */
1378789Sahrens int
13792082Seschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
13805094Slling     char *altroot)
13815094Slling {
13825094Slling 	nvlist_t *props = NULL;
13835094Slling 	int ret;
13845094Slling 
13855094Slling 	if (altroot != NULL) {
13865094Slling 		if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
13875094Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
13885094Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
13895094Slling 			    newname));
13905094Slling 		}
13915094Slling 
13925094Slling 		if (nvlist_add_string(props,
13938084SGeorge.Wilson@Sun.COM 		    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
13948084SGeorge.Wilson@Sun.COM 		    nvlist_add_string(props,
13958084SGeorge.Wilson@Sun.COM 		    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
13965094Slling 			nvlist_free(props);
13975094Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
13985094Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
13995094Slling 			    newname));
14005094Slling 		}
14015094Slling 	}
14025094Slling 
14036643Seschrock 	ret = zpool_import_props(hdl, config, newname, props, B_FALSE);
14045094Slling 	if (props)
14055094Slling 		nvlist_free(props);
14065094Slling 	return (ret);
14075094Slling }
14085094Slling 
14095094Slling /*
14105094Slling  * Import the given pool using the known configuration and a list of
14115094Slling  * properties to be set. The configuration should have come from
14125094Slling  * zpool_find_import(). The 'newname' parameters control whether the pool
14135094Slling  * is imported with a different name.
14145094Slling  */
14155094Slling int
14165094Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
14176643Seschrock     nvlist_t *props, boolean_t importfaulted)
1418789Sahrens {
14192676Seschrock 	zfs_cmd_t zc = { 0 };
142010921STim.Haley@Sun.COM 	zpool_rewind_policy_t policy;
142110921STim.Haley@Sun.COM 	nvlist_t *nvi = NULL;
1422789Sahrens 	char *thename;
1423789Sahrens 	char *origname;
142410921STim.Haley@Sun.COM 	uint64_t returned_size;
1425789Sahrens 	int ret;
14265094Slling 	char errbuf[1024];
1427789Sahrens 
1428789Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1429789Sahrens 	    &origname) == 0);
1430789Sahrens 
14315094Slling 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
14325094Slling 	    "cannot import pool '%s'"), origname);
14335094Slling 
1434789Sahrens 	if (newname != NULL) {
14352082Seschrock 		if (!zpool_name_valid(hdl, B_FALSE, newname))
14363237Slling 			return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
14372082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
14382082Seschrock 			    newname));
1439789Sahrens 		thename = (char *)newname;
1440789Sahrens 	} else {
1441789Sahrens 		thename = origname;
1442789Sahrens 	}
1443789Sahrens 
14445094Slling 	if (props) {
14455094Slling 		uint64_t version;
14465094Slling 
14475094Slling 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
14485094Slling 		    &version) == 0);
14495094Slling 
14507184Stimh 		if ((props = zpool_valid_proplist(hdl, origname,
14515320Slling 		    props, version, B_TRUE, errbuf)) == NULL) {
14525094Slling 			return (-1);
14535320Slling 		} else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
14545320Slling 			nvlist_free(props);
14555094Slling 			return (-1);
14565320Slling 		}
14575094Slling 	}
1458789Sahrens 
1459789Sahrens 	(void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1460789Sahrens 
1461789Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
14621544Seschrock 	    &zc.zc_guid) == 0);
1463789Sahrens 
14645320Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
14655320Slling 		nvlist_free(props);
14662082Seschrock 		return (-1);
14675320Slling 	}
146810921STim.Haley@Sun.COM 	returned_size =  zc.zc_nvlist_conf_size + 512;
146910921STim.Haley@Sun.COM 	if (zcmd_alloc_dst_nvlist(hdl, &zc, returned_size) != 0) {
147010921STim.Haley@Sun.COM 		nvlist_free(props);
147110921STim.Haley@Sun.COM 		return (-1);
147210921STim.Haley@Sun.COM 	}
1473789Sahrens 
14746643Seschrock 	zc.zc_cookie = (uint64_t)importfaulted;
1475789Sahrens 	ret = 0;
14764543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) {
1477789Sahrens 		char desc[1024];
147810921STim.Haley@Sun.COM 
147910921STim.Haley@Sun.COM 		(void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
148010921STim.Haley@Sun.COM 		zpool_get_rewind_policy(config, &policy);
148110921STim.Haley@Sun.COM 		/*
148210921STim.Haley@Sun.COM 		 * Dry-run failed, but we print out what success
148310921STim.Haley@Sun.COM 		 * looks like if we found a best txg
148410921STim.Haley@Sun.COM 		 */
148510921STim.Haley@Sun.COM 		if ((policy.zrp_request & ZPOOL_TRY_REWIND) && nvi) {
148610921STim.Haley@Sun.COM 			zpool_rewind_exclaim(hdl, newname ? origname : thename,
148710921STim.Haley@Sun.COM 			    B_TRUE, nvi);
148810921STim.Haley@Sun.COM 			nvlist_free(nvi);
148910921STim.Haley@Sun.COM 			return (-1);
149010921STim.Haley@Sun.COM 		}
149110921STim.Haley@Sun.COM 
1492789Sahrens 		if (newname == NULL)
1493789Sahrens 			(void) snprintf(desc, sizeof (desc),
1494789Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1495789Sahrens 			    thename);
1496789Sahrens 		else
1497789Sahrens 			(void) snprintf(desc, sizeof (desc),
1498789Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1499789Sahrens 			    origname, thename);
1500789Sahrens 
1501789Sahrens 		switch (errno) {
15021544Seschrock 		case ENOTSUP:
15031544Seschrock 			/*
15041544Seschrock 			 * Unsupported version.
15051544Seschrock 			 */
15062082Seschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, desc);
15071544Seschrock 			break;
15081544Seschrock 
15092174Seschrock 		case EINVAL:
15102174Seschrock 			(void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
15112174Seschrock 			break;
15122174Seschrock 
1513789Sahrens 		default:
151410921STim.Haley@Sun.COM 			(void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
15152082Seschrock 			(void) zpool_standard_error(hdl, errno, desc);
151610921STim.Haley@Sun.COM 			zpool_explain_recover(hdl,
151710921STim.Haley@Sun.COM 			    newname ? origname : thename, -errno, nvi);
151810921STim.Haley@Sun.COM 			nvlist_free(nvi);
151910921STim.Haley@Sun.COM 			break;
1520789Sahrens 		}
1521789Sahrens 
1522789Sahrens 		ret = -1;
1523789Sahrens 	} else {
1524789Sahrens 		zpool_handle_t *zhp;
15254543Smarks 
1526789Sahrens 		/*
1527789Sahrens 		 * This should never fail, but play it safe anyway.
1528789Sahrens 		 */
152910588SEric.Taylor@Sun.COM 		if (zpool_open_silent(hdl, thename, &zhp) != 0)
15302142Seschrock 			ret = -1;
153110588SEric.Taylor@Sun.COM 		else if (zhp != NULL)
1532789Sahrens 			zpool_close(zhp);
153310921STim.Haley@Sun.COM 		(void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
153410921STim.Haley@Sun.COM 		zpool_get_rewind_policy(config, &policy);
153510921STim.Haley@Sun.COM 		if (policy.zrp_request &
153610921STim.Haley@Sun.COM 		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
153710921STim.Haley@Sun.COM 			zpool_rewind_exclaim(hdl, newname ? origname : thename,
153810921STim.Haley@Sun.COM 			    ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
153910921STim.Haley@Sun.COM 			    nvi);
154010921STim.Haley@Sun.COM 		}
154110921STim.Haley@Sun.COM 		nvlist_free(nvi);
154210921STim.Haley@Sun.COM 		return (0);
1543789Sahrens 	}
1544789Sahrens 
15452676Seschrock 	zcmd_free_nvlists(&zc);
15465320Slling 	nvlist_free(props);
15475320Slling 
1548789Sahrens 	return (ret);
1549789Sahrens }
1550789Sahrens 
1551789Sahrens /*
1552789Sahrens  * Scrub the pool.
1553789Sahrens  */
1554789Sahrens int
1555789Sahrens zpool_scrub(zpool_handle_t *zhp, pool_scrub_type_t type)
1556789Sahrens {
1557789Sahrens 	zfs_cmd_t zc = { 0 };
1558789Sahrens 	char msg[1024];
15592082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1560789Sahrens 
1561789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1562789Sahrens 	zc.zc_cookie = type;
1563789Sahrens 
15644543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCRUB, &zc) == 0)
1565789Sahrens 		return (0);
1566789Sahrens 
1567789Sahrens 	(void) snprintf(msg, sizeof (msg),
1568789Sahrens 	    dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1569789Sahrens 
15702082Seschrock 	if (errno == EBUSY)
15712082Seschrock 		return (zfs_error(hdl, EZFS_RESILVERING, msg));
15722082Seschrock 	else
15732082Seschrock 		return (zpool_standard_error(hdl, errno, msg));
1574789Sahrens }
1575789Sahrens 
15762468Sek110237 /*
15779816SGeorge.Wilson@Sun.COM  * Find a vdev that matches the search criteria specified. We use the
15789816SGeorge.Wilson@Sun.COM  * the nvpair name to determine how we should look for the device.
15792468Sek110237  * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
15802468Sek110237  * spare; but FALSE if its an INUSE spare.
15812468Sek110237  */
15822082Seschrock static nvlist_t *
15839816SGeorge.Wilson@Sun.COM vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
15849816SGeorge.Wilson@Sun.COM     boolean_t *l2cache, boolean_t *log)
15851544Seschrock {
15861544Seschrock 	uint_t c, children;
15871544Seschrock 	nvlist_t **child;
15882082Seschrock 	nvlist_t *ret;
15897326SEric.Schrock@Sun.COM 	uint64_t is_log;
15909816SGeorge.Wilson@Sun.COM 	char *srchkey;
15919816SGeorge.Wilson@Sun.COM 	nvpair_t *pair = nvlist_next_nvpair(search, NULL);
15929816SGeorge.Wilson@Sun.COM 
15939816SGeorge.Wilson@Sun.COM 	/* Nothing to look for */
15949816SGeorge.Wilson@Sun.COM 	if (search == NULL || pair == NULL)
15959816SGeorge.Wilson@Sun.COM 		return (NULL);
15969816SGeorge.Wilson@Sun.COM 
15979816SGeorge.Wilson@Sun.COM 	/* Obtain the key we will use to search */
15989816SGeorge.Wilson@Sun.COM 	srchkey = nvpair_name(pair);
15999816SGeorge.Wilson@Sun.COM 
16009816SGeorge.Wilson@Sun.COM 	switch (nvpair_type(pair)) {
16019816SGeorge.Wilson@Sun.COM 	case DATA_TYPE_UINT64: {
16029816SGeorge.Wilson@Sun.COM 		uint64_t srchval, theguid, present;
16039816SGeorge.Wilson@Sun.COM 
16049816SGeorge.Wilson@Sun.COM 		verify(nvpair_value_uint64(pair, &srchval) == 0);
16059816SGeorge.Wilson@Sun.COM 		if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
16069816SGeorge.Wilson@Sun.COM 			if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
16079816SGeorge.Wilson@Sun.COM 			    &present) == 0) {
16089816SGeorge.Wilson@Sun.COM 				/*
16099816SGeorge.Wilson@Sun.COM 				 * If the device has never been present since
16109816SGeorge.Wilson@Sun.COM 				 * import, the only reliable way to match the
16119816SGeorge.Wilson@Sun.COM 				 * vdev is by GUID.
16129816SGeorge.Wilson@Sun.COM 				 */
16139816SGeorge.Wilson@Sun.COM 				verify(nvlist_lookup_uint64(nv,
16149816SGeorge.Wilson@Sun.COM 				    ZPOOL_CONFIG_GUID, &theguid) == 0);
16159816SGeorge.Wilson@Sun.COM 				if (theguid == srchval)
16169816SGeorge.Wilson@Sun.COM 					return (nv);
16179816SGeorge.Wilson@Sun.COM 			}
16189816SGeorge.Wilson@Sun.COM 		}
16199816SGeorge.Wilson@Sun.COM 		break;
16209816SGeorge.Wilson@Sun.COM 	}
16219816SGeorge.Wilson@Sun.COM 
16229816SGeorge.Wilson@Sun.COM 	case DATA_TYPE_STRING: {
16239816SGeorge.Wilson@Sun.COM 		char *srchval, *val;
16249816SGeorge.Wilson@Sun.COM 
16259816SGeorge.Wilson@Sun.COM 		verify(nvpair_value_string(pair, &srchval) == 0);
16269816SGeorge.Wilson@Sun.COM 		if (nvlist_lookup_string(nv, srchkey, &val) != 0)
16279816SGeorge.Wilson@Sun.COM 			break;
16289816SGeorge.Wilson@Sun.COM 
16291544Seschrock 		/*
16309816SGeorge.Wilson@Sun.COM 		 * Search for the requested value. We special case the search
163110594SGeorge.Wilson@Sun.COM 		 * for ZPOOL_CONFIG_PATH when it's a wholedisk and when
163210594SGeorge.Wilson@Sun.COM 		 * Looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
163310594SGeorge.Wilson@Sun.COM 		 * Otherwise, all other searches are simple string compares.
16341544Seschrock 		 */
16359816SGeorge.Wilson@Sun.COM 		if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 && val) {
16369816SGeorge.Wilson@Sun.COM 			uint64_t wholedisk = 0;
16379816SGeorge.Wilson@Sun.COM 
16389816SGeorge.Wilson@Sun.COM 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
16399816SGeorge.Wilson@Sun.COM 			    &wholedisk);
16409816SGeorge.Wilson@Sun.COM 			if (wholedisk) {
16419816SGeorge.Wilson@Sun.COM 				/*
16429816SGeorge.Wilson@Sun.COM 				 * For whole disks, the internal path has 's0',
16439816SGeorge.Wilson@Sun.COM 				 * but the path passed in by the user doesn't.
16449816SGeorge.Wilson@Sun.COM 				 */
16459816SGeorge.Wilson@Sun.COM 				if (strlen(srchval) == strlen(val) - 2 &&
16469816SGeorge.Wilson@Sun.COM 				    strncmp(srchval, val, strlen(srchval)) == 0)
16479816SGeorge.Wilson@Sun.COM 					return (nv);
16489816SGeorge.Wilson@Sun.COM 				break;
16499816SGeorge.Wilson@Sun.COM 			}
165010594SGeorge.Wilson@Sun.COM 		} else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
165110594SGeorge.Wilson@Sun.COM 			char *type, *idx, *end, *p;
165210594SGeorge.Wilson@Sun.COM 			uint64_t id, vdev_id;
165310594SGeorge.Wilson@Sun.COM 
165410594SGeorge.Wilson@Sun.COM 			/*
165510594SGeorge.Wilson@Sun.COM 			 * Determine our vdev type, keeping in mind
165610594SGeorge.Wilson@Sun.COM 			 * that the srchval is composed of a type and
165710594SGeorge.Wilson@Sun.COM 			 * vdev id pair (i.e. mirror-4).
165810594SGeorge.Wilson@Sun.COM 			 */
165910594SGeorge.Wilson@Sun.COM 			if ((type = strdup(srchval)) == NULL)
166010594SGeorge.Wilson@Sun.COM 				return (NULL);
166110594SGeorge.Wilson@Sun.COM 
166210594SGeorge.Wilson@Sun.COM 			if ((p = strrchr(type, '-')) == NULL) {
166310594SGeorge.Wilson@Sun.COM 				free(type);
166410594SGeorge.Wilson@Sun.COM 				break;
166510594SGeorge.Wilson@Sun.COM 			}
166610594SGeorge.Wilson@Sun.COM 			idx = p + 1;
166710594SGeorge.Wilson@Sun.COM 			*p = '\0';
166810594SGeorge.Wilson@Sun.COM 
166910594SGeorge.Wilson@Sun.COM 			/*
167010594SGeorge.Wilson@Sun.COM 			 * If the types don't match then keep looking.
167110594SGeorge.Wilson@Sun.COM 			 */
167210594SGeorge.Wilson@Sun.COM 			if (strncmp(val, type, strlen(val)) != 0) {
167310594SGeorge.Wilson@Sun.COM 				free(type);
167410594SGeorge.Wilson@Sun.COM 				break;
167510594SGeorge.Wilson@Sun.COM 			}
167610594SGeorge.Wilson@Sun.COM 
167710594SGeorge.Wilson@Sun.COM 			verify(strncmp(type, VDEV_TYPE_RAIDZ,
167810594SGeorge.Wilson@Sun.COM 			    strlen(VDEV_TYPE_RAIDZ)) == 0 ||
167910594SGeorge.Wilson@Sun.COM 			    strncmp(type, VDEV_TYPE_MIRROR,
168010594SGeorge.Wilson@Sun.COM 			    strlen(VDEV_TYPE_MIRROR)) == 0);
168110594SGeorge.Wilson@Sun.COM 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
168210594SGeorge.Wilson@Sun.COM 			    &id) == 0);
168310594SGeorge.Wilson@Sun.COM 
168410594SGeorge.Wilson@Sun.COM 			errno = 0;
168510594SGeorge.Wilson@Sun.COM 			vdev_id = strtoull(idx, &end, 10);
168610594SGeorge.Wilson@Sun.COM 
168710594SGeorge.Wilson@Sun.COM 			free(type);
168810594SGeorge.Wilson@Sun.COM 			if (errno != 0)
168910594SGeorge.Wilson@Sun.COM 				return (NULL);
169010594SGeorge.Wilson@Sun.COM 
169110594SGeorge.Wilson@Sun.COM 			/*
169210594SGeorge.Wilson@Sun.COM 			 * Now verify that we have the correct vdev id.
169310594SGeorge.Wilson@Sun.COM 			 */
169410594SGeorge.Wilson@Sun.COM 			if (vdev_id == id)
169510594SGeorge.Wilson@Sun.COM 				return (nv);
16969816SGeorge.Wilson@Sun.COM 		}
16979816SGeorge.Wilson@Sun.COM 
16989816SGeorge.Wilson@Sun.COM 		/*
16999816SGeorge.Wilson@Sun.COM 		 * Common case
17009816SGeorge.Wilson@Sun.COM 		 */
17019816SGeorge.Wilson@Sun.COM 		if (strcmp(srchval, val) == 0)
17022082Seschrock 			return (nv);
17039816SGeorge.Wilson@Sun.COM 		break;
17049816SGeorge.Wilson@Sun.COM 	}
17059816SGeorge.Wilson@Sun.COM 
17069816SGeorge.Wilson@Sun.COM 	default:
17079816SGeorge.Wilson@Sun.COM 		break;
17081544Seschrock 	}
17091544Seschrock 
17101544Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
17111544Seschrock 	    &child, &children) != 0)
17122082Seschrock 		return (NULL);
17131544Seschrock 
17147326SEric.Schrock@Sun.COM 	for (c = 0; c < children; c++) {
17159816SGeorge.Wilson@Sun.COM 		if ((ret = vdev_to_nvlist_iter(child[c], search,
17167326SEric.Schrock@Sun.COM 		    avail_spare, l2cache, NULL)) != NULL) {
17177326SEric.Schrock@Sun.COM 			/*
17187326SEric.Schrock@Sun.COM 			 * The 'is_log' value is only set for the toplevel
17197326SEric.Schrock@Sun.COM 			 * vdev, not the leaf vdevs.  So we always lookup the
17207326SEric.Schrock@Sun.COM 			 * log device from the root of the vdev tree (where
17217326SEric.Schrock@Sun.COM 			 * 'log' is non-NULL).
17227326SEric.Schrock@Sun.COM 			 */
17237326SEric.Schrock@Sun.COM 			if (log != NULL &&
17247326SEric.Schrock@Sun.COM 			    nvlist_lookup_uint64(child[c],
17257326SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
17267326SEric.Schrock@Sun.COM 			    is_log) {
17277326SEric.Schrock@Sun.COM 				*log = B_TRUE;
17287326SEric.Schrock@Sun.COM 			}
17291544Seschrock 			return (ret);
17307326SEric.Schrock@Sun.COM 		}
17317326SEric.Schrock@Sun.COM 	}
17321544Seschrock 
17332082Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
17342082Seschrock 	    &child, &children) == 0) {
17352082Seschrock 		for (c = 0; c < children; c++) {
17369816SGeorge.Wilson@Sun.COM 			if ((ret = vdev_to_nvlist_iter(child[c], search,
17377326SEric.Schrock@Sun.COM 			    avail_spare, l2cache, NULL)) != NULL) {
17382468Sek110237 				*avail_spare = B_TRUE;
17392082Seschrock 				return (ret);
17402082Seschrock 			}
17412082Seschrock 		}
17422082Seschrock 	}
17432082Seschrock 
17445450Sbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
17455450Sbrendan 	    &child, &children) == 0) {
17465450Sbrendan 		for (c = 0; c < children; c++) {
17479816SGeorge.Wilson@Sun.COM 			if ((ret = vdev_to_nvlist_iter(child[c], search,
17487326SEric.Schrock@Sun.COM 			    avail_spare, l2cache, NULL)) != NULL) {
17495450Sbrendan 				*l2cache = B_TRUE;
17505450Sbrendan 				return (ret);
17515450Sbrendan 			}
17525450Sbrendan 		}
17535450Sbrendan 	}
17545450Sbrendan 
17552082Seschrock 	return (NULL);
17561544Seschrock }
17571544Seschrock 
17589816SGeorge.Wilson@Sun.COM /*
17599816SGeorge.Wilson@Sun.COM  * Given a physical path (minus the "/devices" prefix), find the
17609816SGeorge.Wilson@Sun.COM  * associated vdev.
17619816SGeorge.Wilson@Sun.COM  */
17629816SGeorge.Wilson@Sun.COM nvlist_t *
17639816SGeorge.Wilson@Sun.COM zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
17649816SGeorge.Wilson@Sun.COM     boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
17659816SGeorge.Wilson@Sun.COM {
17669816SGeorge.Wilson@Sun.COM 	nvlist_t *search, *nvroot, *ret;
17679816SGeorge.Wilson@Sun.COM 
17689816SGeorge.Wilson@Sun.COM 	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
17699816SGeorge.Wilson@Sun.COM 	verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
17709816SGeorge.Wilson@Sun.COM 
17719816SGeorge.Wilson@Sun.COM 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
17729816SGeorge.Wilson@Sun.COM 	    &nvroot) == 0);
17739816SGeorge.Wilson@Sun.COM 
17749816SGeorge.Wilson@Sun.COM 	*avail_spare = B_FALSE;
17759816SGeorge.Wilson@Sun.COM 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
17769816SGeorge.Wilson@Sun.COM 	nvlist_free(search);
17779816SGeorge.Wilson@Sun.COM 
17789816SGeorge.Wilson@Sun.COM 	return (ret);
17799816SGeorge.Wilson@Sun.COM }
17809816SGeorge.Wilson@Sun.COM 
178110594SGeorge.Wilson@Sun.COM /*
178210594SGeorge.Wilson@Sun.COM  * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
178310594SGeorge.Wilson@Sun.COM  */
178410594SGeorge.Wilson@Sun.COM boolean_t
178510594SGeorge.Wilson@Sun.COM zpool_vdev_is_interior(const char *name)
178610594SGeorge.Wilson@Sun.COM {
178710594SGeorge.Wilson@Sun.COM 	if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
178810594SGeorge.Wilson@Sun.COM 	    strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
178910594SGeorge.Wilson@Sun.COM 		return (B_TRUE);
179010594SGeorge.Wilson@Sun.COM 	return (B_FALSE);
179110594SGeorge.Wilson@Sun.COM }
179210594SGeorge.Wilson@Sun.COM 
17932082Seschrock nvlist_t *
17945450Sbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
17957326SEric.Schrock@Sun.COM     boolean_t *l2cache, boolean_t *log)
17961544Seschrock {
17971544Seschrock 	char buf[MAXPATHLEN];
17981544Seschrock 	char *end;
17999816SGeorge.Wilson@Sun.COM 	nvlist_t *nvroot, *search, *ret;
18001544Seschrock 	uint64_t guid;
18011544Seschrock 
18029816SGeorge.Wilson@Sun.COM 	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
18039816SGeorge.Wilson@Sun.COM 
18041613Seschrock 	guid = strtoull(path, &end, 10);
18051544Seschrock 	if (guid != 0 && *end == '\0') {
18069816SGeorge.Wilson@Sun.COM 		verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
180710594SGeorge.Wilson@Sun.COM 	} else if (zpool_vdev_is_interior(path)) {
180810594SGeorge.Wilson@Sun.COM 		verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
18091544Seschrock 	} else if (path[0] != '/') {
18101544Seschrock 		(void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path);
18119816SGeorge.Wilson@Sun.COM 		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
18121544Seschrock 	} else {
18139816SGeorge.Wilson@Sun.COM 		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
18141544Seschrock 	}
18151544Seschrock 
18161544Seschrock 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
18171544Seschrock 	    &nvroot) == 0);
18181544Seschrock 
18192468Sek110237 	*avail_spare = B_FALSE;
18205450Sbrendan 	*l2cache = B_FALSE;
18217326SEric.Schrock@Sun.COM 	if (log != NULL)
18227326SEric.Schrock@Sun.COM 		*log = B_FALSE;
18239816SGeorge.Wilson@Sun.COM 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
18249816SGeorge.Wilson@Sun.COM 	nvlist_free(search);
18259816SGeorge.Wilson@Sun.COM 
18269816SGeorge.Wilson@Sun.COM 	return (ret);
18272468Sek110237 }
18282468Sek110237 
18297656SSherry.Moore@Sun.COM static int
18307656SSherry.Moore@Sun.COM vdev_online(nvlist_t *nv)
18317656SSherry.Moore@Sun.COM {
18327656SSherry.Moore@Sun.COM 	uint64_t ival;
18337656SSherry.Moore@Sun.COM 
18347656SSherry.Moore@Sun.COM 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
18357656SSherry.Moore@Sun.COM 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
18367656SSherry.Moore@Sun.COM 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
18377656SSherry.Moore@Sun.COM 		return (0);
18387656SSherry.Moore@Sun.COM 
18397656SSherry.Moore@Sun.COM 	return (1);
18407656SSherry.Moore@Sun.COM }
18417656SSherry.Moore@Sun.COM 
18427656SSherry.Moore@Sun.COM /*
18439790SLin.Ling@Sun.COM  * Helper function for zpool_get_physpaths().
18447656SSherry.Moore@Sun.COM  */
18459160SSherry.Moore@Sun.COM static int
18469790SLin.Ling@Sun.COM vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
18479160SSherry.Moore@Sun.COM     size_t *bytes_written)
18487656SSherry.Moore@Sun.COM {
18499160SSherry.Moore@Sun.COM 	size_t bytes_left, pos, rsz;
18509160SSherry.Moore@Sun.COM 	char *tmppath;
18519160SSherry.Moore@Sun.COM 	const char *format;
18529160SSherry.Moore@Sun.COM 
18539160SSherry.Moore@Sun.COM 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
18549160SSherry.Moore@Sun.COM 	    &tmppath) != 0)
18559160SSherry.Moore@Sun.COM 		return (EZFS_NODEVICE);
18569160SSherry.Moore@Sun.COM 
18579160SSherry.Moore@Sun.COM 	pos = *bytes_written;
18589160SSherry.Moore@Sun.COM 	bytes_left = physpath_size - pos;
18599160SSherry.Moore@Sun.COM 	format = (pos == 0) ? "%s" : " %s";
18609160SSherry.Moore@Sun.COM 
18619160SSherry.Moore@Sun.COM 	rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
18629160SSherry.Moore@Sun.COM 	*bytes_written += rsz;
18639160SSherry.Moore@Sun.COM 
18649160SSherry.Moore@Sun.COM 	if (rsz >= bytes_left) {
18659160SSherry.Moore@Sun.COM 		/* if physpath was not copied properly, clear it */
18669160SSherry.Moore@Sun.COM 		if (bytes_left != 0) {
18679160SSherry.Moore@Sun.COM 			physpath[pos] = 0;
18689160SSherry.Moore@Sun.COM 		}
18699160SSherry.Moore@Sun.COM 		return (EZFS_NOSPC);
18709160SSherry.Moore@Sun.COM 	}
18719160SSherry.Moore@Sun.COM 	return (0);
18729160SSherry.Moore@Sun.COM }
18739160SSherry.Moore@Sun.COM 
18749790SLin.Ling@Sun.COM static int
18759790SLin.Ling@Sun.COM vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
18769790SLin.Ling@Sun.COM     size_t *rsz, boolean_t is_spare)
18779790SLin.Ling@Sun.COM {
18789790SLin.Ling@Sun.COM 	char *type;
18799790SLin.Ling@Sun.COM 	int ret;
18809790SLin.Ling@Sun.COM 
18819790SLin.Ling@Sun.COM 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
18829790SLin.Ling@Sun.COM 		return (EZFS_INVALCONFIG);
18839790SLin.Ling@Sun.COM 
18849790SLin.Ling@Sun.COM 	if (strcmp(type, VDEV_TYPE_DISK) == 0) {
18859790SLin.Ling@Sun.COM 		/*
18869790SLin.Ling@Sun.COM 		 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
18879790SLin.Ling@Sun.COM 		 * For a spare vdev, we only want to boot from the active
18889790SLin.Ling@Sun.COM 		 * spare device.
18899790SLin.Ling@Sun.COM 		 */
18909790SLin.Ling@Sun.COM 		if (is_spare) {
18919790SLin.Ling@Sun.COM 			uint64_t spare = 0;
18929790SLin.Ling@Sun.COM 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
18939790SLin.Ling@Sun.COM 			    &spare);
18949790SLin.Ling@Sun.COM 			if (!spare)
18959790SLin.Ling@Sun.COM 				return (EZFS_INVALCONFIG);
18969790SLin.Ling@Sun.COM 		}
18979790SLin.Ling@Sun.COM 
18989790SLin.Ling@Sun.COM 		if (vdev_online(nv)) {
18999790SLin.Ling@Sun.COM 			if ((ret = vdev_get_one_physpath(nv, physpath,
19009790SLin.Ling@Sun.COM 			    phypath_size, rsz)) != 0)
19019790SLin.Ling@Sun.COM 				return (ret);
19029790SLin.Ling@Sun.COM 		}
19039790SLin.Ling@Sun.COM 	} else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
19049790SLin.Ling@Sun.COM 	    strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
19059790SLin.Ling@Sun.COM 	    (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
19069790SLin.Ling@Sun.COM 		nvlist_t **child;
19079790SLin.Ling@Sun.COM 		uint_t count;
19089790SLin.Ling@Sun.COM 		int i, ret;
19099790SLin.Ling@Sun.COM 
19109790SLin.Ling@Sun.COM 		if (nvlist_lookup_nvlist_array(nv,
19119790SLin.Ling@Sun.COM 		    ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
19129790SLin.Ling@Sun.COM 			return (EZFS_INVALCONFIG);
19139790SLin.Ling@Sun.COM 
19149790SLin.Ling@Sun.COM 		for (i = 0; i < count; i++) {
19159790SLin.Ling@Sun.COM 			ret = vdev_get_physpaths(child[i], physpath,
19169790SLin.Ling@Sun.COM 			    phypath_size, rsz, is_spare);
19179790SLin.Ling@Sun.COM 			if (ret == EZFS_NOSPC)
19189790SLin.Ling@Sun.COM 				return (ret);
19199790SLin.Ling@Sun.COM 		}
19209790SLin.Ling@Sun.COM 	}
19219790SLin.Ling@Sun.COM 
19229790SLin.Ling@Sun.COM 	return (EZFS_POOL_INVALARG);
19239790SLin.Ling@Sun.COM }
19249790SLin.Ling@Sun.COM 
19259160SSherry.Moore@Sun.COM /*
19269160SSherry.Moore@Sun.COM  * Get phys_path for a root pool config.
19279160SSherry.Moore@Sun.COM  * Return 0 on success; non-zero on failure.
19289160SSherry.Moore@Sun.COM  */
19299160SSherry.Moore@Sun.COM static int
19309160SSherry.Moore@Sun.COM zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
19319160SSherry.Moore@Sun.COM {
19329160SSherry.Moore@Sun.COM 	size_t rsz;
19337656SSherry.Moore@Sun.COM 	nvlist_t *vdev_root;
19347656SSherry.Moore@Sun.COM 	nvlist_t **child;
19357656SSherry.Moore@Sun.COM 	uint_t count;
19369160SSherry.Moore@Sun.COM 	char *type;
19379160SSherry.Moore@Sun.COM 
19389160SSherry.Moore@Sun.COM 	rsz = 0;
19399160SSherry.Moore@Sun.COM 
19409160SSherry.Moore@Sun.COM 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
19419160SSherry.Moore@Sun.COM 	    &vdev_root) != 0)
19429160SSherry.Moore@Sun.COM 		return (EZFS_INVALCONFIG);
19439160SSherry.Moore@Sun.COM 
19449160SSherry.Moore@Sun.COM 	if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
19459160SSherry.Moore@Sun.COM 	    nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
19469160SSherry.Moore@Sun.COM 	    &child, &count) != 0)
19479160SSherry.Moore@Sun.COM 		return (EZFS_INVALCONFIG);
19487656SSherry.Moore@Sun.COM 
19497656SSherry.Moore@Sun.COM 	/*
19509160SSherry.Moore@Sun.COM 	 * root pool can not have EFI labeled disks and can only have
19519160SSherry.Moore@Sun.COM 	 * a single top-level vdev.
19527656SSherry.Moore@Sun.COM 	 */
19539160SSherry.Moore@Sun.COM 	if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
19549160SSherry.Moore@Sun.COM 	    pool_uses_efi(vdev_root))
19559160SSherry.Moore@Sun.COM 		return (EZFS_POOL_INVALARG);
19569160SSherry.Moore@Sun.COM 
19579790SLin.Ling@Sun.COM 	(void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
19589790SLin.Ling@Sun.COM 	    B_FALSE);
19597656SSherry.Moore@Sun.COM 
19609160SSherry.Moore@Sun.COM 	/* No online devices */
19619160SSherry.Moore@Sun.COM 	if (rsz == 0)
19629160SSherry.Moore@Sun.COM 		return (EZFS_NODEVICE);
19639160SSherry.Moore@Sun.COM 
19647656SSherry.Moore@Sun.COM 	return (0);
19657656SSherry.Moore@Sun.COM }
19667656SSherry.Moore@Sun.COM 
19672468Sek110237 /*
19689160SSherry.Moore@Sun.COM  * Get phys_path for a root pool
19699160SSherry.Moore@Sun.COM  * Return 0 on success; non-zero on failure.
19709160SSherry.Moore@Sun.COM  */
19719160SSherry.Moore@Sun.COM int
19729160SSherry.Moore@Sun.COM zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
19739160SSherry.Moore@Sun.COM {
19749160SSherry.Moore@Sun.COM 	return (zpool_get_config_physpath(zhp->zpool_config, physpath,
19759160SSherry.Moore@Sun.COM 	    phypath_size));
19769160SSherry.Moore@Sun.COM }
19779160SSherry.Moore@Sun.COM 
19789160SSherry.Moore@Sun.COM /*
19799816SGeorge.Wilson@Sun.COM  * If the device has being dynamically expanded then we need to relabel
19809816SGeorge.Wilson@Sun.COM  * the disk to use the new unallocated space.
19819816SGeorge.Wilson@Sun.COM  */
19829816SGeorge.Wilson@Sun.COM static int
19839816SGeorge.Wilson@Sun.COM zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
19849816SGeorge.Wilson@Sun.COM {
19859816SGeorge.Wilson@Sun.COM 	char path[MAXPATHLEN];
19869816SGeorge.Wilson@Sun.COM 	char errbuf[1024];
19879816SGeorge.Wilson@Sun.COM 	int fd, error;
19889816SGeorge.Wilson@Sun.COM 	int (*_efi_use_whole_disk)(int);
19899816SGeorge.Wilson@Sun.COM 
19909816SGeorge.Wilson@Sun.COM 	if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
19919816SGeorge.Wilson@Sun.COM 	    "efi_use_whole_disk")) == NULL)
19929816SGeorge.Wilson@Sun.COM 		return (-1);
19939816SGeorge.Wilson@Sun.COM 
19949816SGeorge.Wilson@Sun.COM 	(void) snprintf(path, sizeof (path), "%s/%s", RDISK_ROOT, name);
19959816SGeorge.Wilson@Sun.COM 
19969816SGeorge.Wilson@Sun.COM 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
19979816SGeorge.Wilson@Sun.COM 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
19989816SGeorge.Wilson@Sun.COM 		    "relabel '%s': unable to open device"), name);
19999816SGeorge.Wilson@Sun.COM 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
20009816SGeorge.Wilson@Sun.COM 	}
20019816SGeorge.Wilson@Sun.COM 
20029816SGeorge.Wilson@Sun.COM 	/*
20039816SGeorge.Wilson@Sun.COM 	 * It's possible that we might encounter an error if the device
20049816SGeorge.Wilson@Sun.COM 	 * does not have any unallocated space left. If so, we simply
20059816SGeorge.Wilson@Sun.COM 	 * ignore that error and continue on.
20069816SGeorge.Wilson@Sun.COM 	 */
20079816SGeorge.Wilson@Sun.COM 	error = _efi_use_whole_disk(fd);
20089816SGeorge.Wilson@Sun.COM 	(void) close(fd);
20099816SGeorge.Wilson@Sun.COM 	if (error && error != VT_ENOSPC) {
20109816SGeorge.Wilson@Sun.COM 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
20119816SGeorge.Wilson@Sun.COM 		    "relabel '%s': unable to read disk capacity"), name);
20129816SGeorge.Wilson@Sun.COM 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
20139816SGeorge.Wilson@Sun.COM 	}
20149816SGeorge.Wilson@Sun.COM 	return (0);
20159816SGeorge.Wilson@Sun.COM }
20169816SGeorge.Wilson@Sun.COM 
20179816SGeorge.Wilson@Sun.COM /*
20184451Seschrock  * Bring the specified vdev online.   The 'flags' parameter is a set of the
20194451Seschrock  * ZFS_ONLINE_* flags.
2020789Sahrens  */
2021789Sahrens int
20224451Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
20234451Seschrock     vdev_state_t *newstate)
2024789Sahrens {
2025789Sahrens 	zfs_cmd_t zc = { 0 };
2026789Sahrens 	char msg[1024];
20272082Seschrock 	nvlist_t *tgt;
20289816SGeorge.Wilson@Sun.COM 	boolean_t avail_spare, l2cache, islog;
20292082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2030789Sahrens 
20319816SGeorge.Wilson@Sun.COM 	if (flags & ZFS_ONLINE_EXPAND) {
20329816SGeorge.Wilson@Sun.COM 		(void) snprintf(msg, sizeof (msg),
20339816SGeorge.Wilson@Sun.COM 		    dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
20349816SGeorge.Wilson@Sun.COM 	} else {
20359816SGeorge.Wilson@Sun.COM 		(void) snprintf(msg, sizeof (msg),
20369816SGeorge.Wilson@Sun.COM 		    dgettext(TEXT_DOMAIN, "cannot online %s"), path);
20379816SGeorge.Wilson@Sun.COM 	}
2038789Sahrens 
20391544Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
20407326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
20419816SGeorge.Wilson@Sun.COM 	    &islog)) == NULL)
20422082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2043789Sahrens 
20442468Sek110237 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
20452468Sek110237 
204610817SEric.Schrock@Sun.COM 	if (avail_spare)
20472082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
20482082Seschrock 
20499816SGeorge.Wilson@Sun.COM 	if (flags & ZFS_ONLINE_EXPAND ||
20509816SGeorge.Wilson@Sun.COM 	    zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
20519816SGeorge.Wilson@Sun.COM 		char *pathname = NULL;
20529816SGeorge.Wilson@Sun.COM 		uint64_t wholedisk = 0;
20539816SGeorge.Wilson@Sun.COM 
20549816SGeorge.Wilson@Sun.COM 		(void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
20559816SGeorge.Wilson@Sun.COM 		    &wholedisk);
20569816SGeorge.Wilson@Sun.COM 		verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
20579816SGeorge.Wilson@Sun.COM 		    &pathname) == 0);
20589816SGeorge.Wilson@Sun.COM 
20599816SGeorge.Wilson@Sun.COM 		/*
20609816SGeorge.Wilson@Sun.COM 		 * XXX - L2ARC 1.0 devices can't support expansion.
20619816SGeorge.Wilson@Sun.COM 		 */
20629816SGeorge.Wilson@Sun.COM 		if (l2cache) {
20639816SGeorge.Wilson@Sun.COM 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
20649816SGeorge.Wilson@Sun.COM 			    "cannot expand cache devices"));
20659816SGeorge.Wilson@Sun.COM 			return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
20669816SGeorge.Wilson@Sun.COM 		}
20679816SGeorge.Wilson@Sun.COM 
20689816SGeorge.Wilson@Sun.COM 		if (wholedisk) {
20699816SGeorge.Wilson@Sun.COM 			pathname += strlen(DISK_ROOT) + 1;
20709816SGeorge.Wilson@Sun.COM 			(void) zpool_relabel_disk(zhp->zpool_hdl, pathname);
20719816SGeorge.Wilson@Sun.COM 		}
20729816SGeorge.Wilson@Sun.COM 	}
20739816SGeorge.Wilson@Sun.COM 
20744451Seschrock 	zc.zc_cookie = VDEV_STATE_ONLINE;
20754451Seschrock 	zc.zc_obj = flags;
20764451Seschrock 
20774543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0)
20784451Seschrock 		return (zpool_standard_error(hdl, errno, msg));
20794451Seschrock 
20804451Seschrock 	*newstate = zc.zc_cookie;
20814451Seschrock 	return (0);
2082789Sahrens }
2083789Sahrens 
2084789Sahrens /*
2085789Sahrens  * Take the specified vdev offline
2086789Sahrens  */
2087789Sahrens int
20884451Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2089789Sahrens {
2090789Sahrens 	zfs_cmd_t zc = { 0 };
2091789Sahrens 	char msg[1024];
20922082Seschrock 	nvlist_t *tgt;
20935450Sbrendan 	boolean_t avail_spare, l2cache;
20942082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2095789Sahrens 
20961544Seschrock 	(void) snprintf(msg, sizeof (msg),
20971544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
20981544Seschrock 
2099789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
21007326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
21017326SEric.Schrock@Sun.COM 	    NULL)) == NULL)
21022082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
21032082Seschrock 
21042468Sek110237 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
21052468Sek110237 
210610817SEric.Schrock@Sun.COM 	if (avail_spare)
21072082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
21082082Seschrock 
21094451Seschrock 	zc.zc_cookie = VDEV_STATE_OFFLINE;
21104451Seschrock 	zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
21111485Slling 
21124543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2113789Sahrens 		return (0);
2114789Sahrens 
2115789Sahrens 	switch (errno) {
21162082Seschrock 	case EBUSY:
2117789Sahrens 
2118789Sahrens 		/*
2119789Sahrens 		 * There are no other replicas of this device.
2120789Sahrens 		 */
21212082Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
21222082Seschrock 
21239701SGeorge.Wilson@Sun.COM 	case EEXIST:
21249701SGeorge.Wilson@Sun.COM 		/*
21259701SGeorge.Wilson@Sun.COM 		 * The log device has unplayed logs
21269701SGeorge.Wilson@Sun.COM 		 */
21279701SGeorge.Wilson@Sun.COM 		return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
21289701SGeorge.Wilson@Sun.COM 
21292082Seschrock 	default:
21302082Seschrock 		return (zpool_standard_error(hdl, errno, msg));
21312082Seschrock 	}
21322082Seschrock }
2133789Sahrens 
21342082Seschrock /*
21354451Seschrock  * Mark the given vdev faulted.
21364451Seschrock  */
21374451Seschrock int
213810817SEric.Schrock@Sun.COM zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
21394451Seschrock {
21404451Seschrock 	zfs_cmd_t zc = { 0 };
21414451Seschrock 	char msg[1024];
21424451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
21434451Seschrock 
21444451Seschrock 	(void) snprintf(msg, sizeof (msg),
21454451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
21464451Seschrock 
21474451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
21484451Seschrock 	zc.zc_guid = guid;
21494451Seschrock 	zc.zc_cookie = VDEV_STATE_FAULTED;
215010817SEric.Schrock@Sun.COM 	zc.zc_obj = aux;
21514451Seschrock 
21524451Seschrock 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
21534451Seschrock 		return (0);
21544451Seschrock 
21554451Seschrock 	switch (errno) {
21564451Seschrock 	case EBUSY:
21574451Seschrock 
21584451Seschrock 		/*
21594451Seschrock 		 * There are no other replicas of this device.
21604451Seschrock 		 */
21614451Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
21624451Seschrock 
21634451Seschrock 	default:
21644451Seschrock 		return (zpool_standard_error(hdl, errno, msg));
21654451Seschrock 	}
21664451Seschrock 
21674451Seschrock }
21684451Seschrock 
21694451Seschrock /*
21704451Seschrock  * Mark the given vdev degraded.
21714451Seschrock  */
21724451Seschrock int
217310817SEric.Schrock@Sun.COM zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
21744451Seschrock {
21754451Seschrock 	zfs_cmd_t zc = { 0 };
21764451Seschrock 	char msg[1024];
21774451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
21784451Seschrock 
21794451Seschrock 	(void) snprintf(msg, sizeof (msg),
21804451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
21814451Seschrock 
21824451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
21834451Seschrock 	zc.zc_guid = guid;
21844451Seschrock 	zc.zc_cookie = VDEV_STATE_DEGRADED;
218510817SEric.Schrock@Sun.COM 	zc.zc_obj = aux;
21864451Seschrock 
21874451Seschrock 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
21884451Seschrock 		return (0);
21894451Seschrock 
21904451Seschrock 	return (zpool_standard_error(hdl, errno, msg));
21914451Seschrock }
21924451Seschrock 
21934451Seschrock /*
21942082Seschrock  * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
21952082Seschrock  * a hot spare.
21962082Seschrock  */
21972082Seschrock static boolean_t
21982082Seschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
21992082Seschrock {
22002082Seschrock 	nvlist_t **child;
22012082Seschrock 	uint_t c, children;
22022082Seschrock 	char *type;
22032082Seschrock 
22042082Seschrock 	if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
22052082Seschrock 	    &children) == 0) {
22062082Seschrock 		verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
22072082Seschrock 		    &type) == 0);
22082082Seschrock 
22092082Seschrock 		if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
22102082Seschrock 		    children == 2 && child[which] == tgt)
22112082Seschrock 			return (B_TRUE);
22122082Seschrock 
22132082Seschrock 		for (c = 0; c < children; c++)
22142082Seschrock 			if (is_replacing_spare(child[c], tgt, which))
22152082Seschrock 				return (B_TRUE);
2216789Sahrens 	}
22172082Seschrock 
22182082Seschrock 	return (B_FALSE);
2219789Sahrens }
2220789Sahrens 
2221789Sahrens /*
2222789Sahrens  * Attach new_disk (fully described by nvroot) to old_disk.
22234527Sperrin  * If 'replacing' is specified, the new disk will replace the old one.
2224789Sahrens  */
2225789Sahrens int
2226789Sahrens zpool_vdev_attach(zpool_handle_t *zhp,
2227789Sahrens     const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2228789Sahrens {
2229789Sahrens 	zfs_cmd_t zc = { 0 };
2230789Sahrens 	char msg[1024];
2231789Sahrens 	int ret;
22322082Seschrock 	nvlist_t *tgt;
22337326SEric.Schrock@Sun.COM 	boolean_t avail_spare, l2cache, islog;
22347326SEric.Schrock@Sun.COM 	uint64_t val;
22357041Seschrock 	char *path, *newname;
22362082Seschrock 	nvlist_t **child;
22372082Seschrock 	uint_t children;
22382082Seschrock 	nvlist_t *config_root;
22392082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
22407965SGeorge.Wilson@Sun.COM 	boolean_t rootpool = pool_is_bootable(zhp);
2241789Sahrens 
22421544Seschrock 	if (replacing)
22431544Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
22441544Seschrock 		    "cannot replace %s with %s"), old_disk, new_disk);
22451544Seschrock 	else
22461544Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
22471544Seschrock 		    "cannot attach %s to %s"), new_disk, old_disk);
22481544Seschrock 
22497965SGeorge.Wilson@Sun.COM 	/*
22507965SGeorge.Wilson@Sun.COM 	 * If this is a root pool, make sure that we're not attaching an
22517965SGeorge.Wilson@Sun.COM 	 * EFI labeled device.
22527965SGeorge.Wilson@Sun.COM 	 */
22537965SGeorge.Wilson@Sun.COM 	if (rootpool && pool_uses_efi(nvroot)) {
22547965SGeorge.Wilson@Sun.COM 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
22557965SGeorge.Wilson@Sun.COM 		    "EFI labeled devices are not supported on root pools."));
22567965SGeorge.Wilson@Sun.COM 		return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
22577965SGeorge.Wilson@Sun.COM 	}
22587965SGeorge.Wilson@Sun.COM 
2259789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
22607326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
22617326SEric.Schrock@Sun.COM 	    &islog)) == 0)
22622082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
22632082Seschrock 
22642468Sek110237 	if (avail_spare)
22652082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
22662082Seschrock 
22675450Sbrendan 	if (l2cache)
22685450Sbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
22695450Sbrendan 
22702082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
22712082Seschrock 	zc.zc_cookie = replacing;
22722082Seschrock 
22732082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
22742082Seschrock 	    &child, &children) != 0 || children != 1) {
22752082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
22762082Seschrock 		    "new device must be a single disk"));
22772082Seschrock 		return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
22781544Seschrock 	}
22792082Seschrock 
22802082Seschrock 	verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
22812082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
22822082Seschrock 
228310594SGeorge.Wilson@Sun.COM 	if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
22847041Seschrock 		return (-1);
22857041Seschrock 
22862082Seschrock 	/*
22872082Seschrock 	 * If the target is a hot spare that has been swapped in, we can only
22882082Seschrock 	 * replace it with another hot spare.
22892082Seschrock 	 */
22902082Seschrock 	if (replacing &&
22912082Seschrock 	    nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
22927326SEric.Schrock@Sun.COM 	    (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
22937326SEric.Schrock@Sun.COM 	    NULL) == NULL || !avail_spare) &&
22947326SEric.Schrock@Sun.COM 	    is_replacing_spare(config_root, tgt, 1)) {
22952082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
22962082Seschrock 		    "can only be replaced by another hot spare"));
22977041Seschrock 		free(newname);
22982082Seschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
22992082Seschrock 	}
23002082Seschrock 
23012082Seschrock 	/*
23022082Seschrock 	 * If we are attempting to replace a spare, it canot be applied to an
23032082Seschrock 	 * already spared device.
23042082Seschrock 	 */
23052082Seschrock 	if (replacing &&
23062082Seschrock 	    nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 &&
23077326SEric.Schrock@Sun.COM 	    zpool_find_vdev(zhp, newname, &avail_spare,
23087326SEric.Schrock@Sun.COM 	    &l2cache, NULL) != NULL && avail_spare &&
23097326SEric.Schrock@Sun.COM 	    is_replacing_spare(config_root, tgt, 0)) {
23102082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
23112082Seschrock 		    "device has already been replaced with a spare"));
23127041Seschrock 		free(newname);
23132082Seschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
23142082Seschrock 	}
2315789Sahrens 
23167041Seschrock 	free(newname);
23177041Seschrock 
23185094Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
23192082Seschrock 		return (-1);
2320789Sahrens 
23214543Smarks 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2322789Sahrens 
23232676Seschrock 	zcmd_free_nvlists(&zc);
2324789Sahrens 
23257965SGeorge.Wilson@Sun.COM 	if (ret == 0) {
23267965SGeorge.Wilson@Sun.COM 		if (rootpool) {
23277965SGeorge.Wilson@Sun.COM 			/*
23287965SGeorge.Wilson@Sun.COM 			 * XXX - This should be removed once we can
23297965SGeorge.Wilson@Sun.COM 			 * automatically install the bootblocks on the
23307965SGeorge.Wilson@Sun.COM 			 * newly attached disk.
23317965SGeorge.Wilson@Sun.COM 			 */
23327965SGeorge.Wilson@Sun.COM 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Please "
23337965SGeorge.Wilson@Sun.COM 			    "be sure to invoke %s to make '%s' bootable.\n"),
23347965SGeorge.Wilson@Sun.COM 			    BOOTCMD, new_disk);
23359790SLin.Ling@Sun.COM 
23369790SLin.Ling@Sun.COM 			/*
23379790SLin.Ling@Sun.COM 			 * XXX need a better way to prevent user from
23389790SLin.Ling@Sun.COM 			 * booting up a half-baked vdev.
23399790SLin.Ling@Sun.COM 			 */
23409790SLin.Ling@Sun.COM 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
23419790SLin.Ling@Sun.COM 			    "sure to wait until resilver is done "
23429790SLin.Ling@Sun.COM 			    "before rebooting.\n"));
23437965SGeorge.Wilson@Sun.COM 		}
2344789Sahrens 		return (0);
23457965SGeorge.Wilson@Sun.COM 	}
2346789Sahrens 
2347789Sahrens 	switch (errno) {
23481544Seschrock 	case ENOTSUP:
2349789Sahrens 		/*
2350789Sahrens 		 * Can't attach to or replace this type of vdev.
2351789Sahrens 		 */
23524527Sperrin 		if (replacing) {
23537326SEric.Schrock@Sun.COM 			if (islog)
23544527Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
23554527Sperrin 				    "cannot replace a log with a spare"));
23564527Sperrin 			else
23574527Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
23584527Sperrin 				    "cannot replace a replacing device"));
23594527Sperrin 		} else {
23602082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
23612082Seschrock 			    "can only attach to mirrors and top-level "
23622082Seschrock 			    "disks"));
23634527Sperrin 		}
23642082Seschrock 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
2365789Sahrens 		break;
2366789Sahrens 
23671544Seschrock 	case EINVAL:
2368789Sahrens 		/*
2369789Sahrens 		 * The new device must be a single disk.
2370789Sahrens 		 */
23712082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
23722082Seschrock 		    "new device must be a single disk"));
23732082Seschrock 		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2374789Sahrens 		break;
2375789Sahrens 
23761544Seschrock 	case EBUSY:
23772082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
23782082Seschrock 		    new_disk);
23792082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2380789Sahrens 		break;
2381789Sahrens 
23821544Seschrock 	case EOVERFLOW:
2383789Sahrens 		/*
2384789Sahrens 		 * The new device is too small.
2385789Sahrens 		 */
23862082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
23872082Seschrock 		    "device is too small"));
23882082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2389789Sahrens 		break;
2390789Sahrens 
23911544Seschrock 	case EDOM:
2392789Sahrens 		/*
2393789Sahrens 		 * The new device has a different alignment requirement.
2394789Sahrens 		 */
23952082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
23962082Seschrock 		    "devices have different sector alignment"));
23972082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2398789Sahrens 		break;
2399789Sahrens 
24001544Seschrock 	case ENAMETOOLONG:
2401789Sahrens 		/*
2402789Sahrens 		 * The resulting top-level vdev spec won't fit in the label.
2403789Sahrens 		 */
24042082Seschrock 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2405789Sahrens 		break;
2406789Sahrens 
24071544Seschrock 	default:
24082082Seschrock 		(void) zpool_standard_error(hdl, errno, msg);
2409789Sahrens 	}
2410789Sahrens 
24112082Seschrock 	return (-1);
2412789Sahrens }
2413789Sahrens 
2414789Sahrens /*
2415789Sahrens  * Detach the specified device.
2416789Sahrens  */
2417789Sahrens int
2418789Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2419789Sahrens {
2420789Sahrens 	zfs_cmd_t zc = { 0 };
2421789Sahrens 	char msg[1024];
24222082Seschrock 	nvlist_t *tgt;
24235450Sbrendan 	boolean_t avail_spare, l2cache;
24242082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2425789Sahrens 
24261544Seschrock 	(void) snprintf(msg, sizeof (msg),
24271544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
24281544Seschrock 
2429789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
24307326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
24317326SEric.Schrock@Sun.COM 	    NULL)) == 0)
24322082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2433789Sahrens 
24342468Sek110237 	if (avail_spare)
24352082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
24362082Seschrock 
24375450Sbrendan 	if (l2cache)
24385450Sbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
24395450Sbrendan 
24402082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
24412082Seschrock 
24424543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2443789Sahrens 		return (0);
2444789Sahrens 
2445789Sahrens 	switch (errno) {
2446789Sahrens 
24471544Seschrock 	case ENOTSUP:
2448789Sahrens 		/*
2449789Sahrens 		 * Can't detach from this type of vdev.
2450789Sahrens 		 */
24512082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
24522082Seschrock 		    "applicable to mirror and replacing vdevs"));
24532082Seschrock 		(void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg);
2454789Sahrens 		break;
2455789Sahrens 
24561544Seschrock 	case EBUSY:
2457789Sahrens 		/*
2458789Sahrens 		 * There are no other replicas of this device.
2459789Sahrens 		 */
24602082Seschrock 		(void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2461789Sahrens 		break;
2462789Sahrens 
24631544Seschrock 	default:
24642082Seschrock 		(void) zpool_standard_error(hdl, errno, msg);
24651544Seschrock 	}
24661544Seschrock 
24672082Seschrock 	return (-1);
24682082Seschrock }
24692082Seschrock 
24702082Seschrock /*
24715450Sbrendan  * Remove the given device.  Currently, this is supported only for hot spares
24725450Sbrendan  * and level 2 cache devices.
24732082Seschrock  */
24742082Seschrock int
24752082Seschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
24762082Seschrock {
24772082Seschrock 	zfs_cmd_t zc = { 0 };
24782082Seschrock 	char msg[1024];
24792082Seschrock 	nvlist_t *tgt;
248010594SGeorge.Wilson@Sun.COM 	boolean_t avail_spare, l2cache, islog;
24812082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
248210594SGeorge.Wilson@Sun.COM 	uint64_t version;
24832082Seschrock 
24842082Seschrock 	(void) snprintf(msg, sizeof (msg),
24852082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
24862082Seschrock 
24872082Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
24887326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
248910594SGeorge.Wilson@Sun.COM 	    &islog)) == 0)
24902082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
249110594SGeorge.Wilson@Sun.COM 	/*
249210594SGeorge.Wilson@Sun.COM 	 * XXX - this should just go away.
249310594SGeorge.Wilson@Sun.COM 	 */
249410594SGeorge.Wilson@Sun.COM 	if (!avail_spare && !l2cache && !islog) {
24952082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
249610594SGeorge.Wilson@Sun.COM 		    "only inactive hot spares, cache, top-level, "
249710594SGeorge.Wilson@Sun.COM 		    "or log devices can be removed"));
24982082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
24992082Seschrock 	}
25002082Seschrock 
250110594SGeorge.Wilson@Sun.COM 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
250210594SGeorge.Wilson@Sun.COM 	if (islog && version < SPA_VERSION_HOLES) {
250310594SGeorge.Wilson@Sun.COM 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
250410594SGeorge.Wilson@Sun.COM 		    "pool must be upgrade to support log removal"));
250510594SGeorge.Wilson@Sun.COM 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
250610594SGeorge.Wilson@Sun.COM 	}
250710594SGeorge.Wilson@Sun.COM 
25082082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
25092082Seschrock 
25104543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
25112082Seschrock 		return (0);
25122082Seschrock 
25132082Seschrock 	return (zpool_standard_error(hdl, errno, msg));
25141544Seschrock }
25151544Seschrock 
25161544Seschrock /*
25171544Seschrock  * Clear the errors for the pool, or the particular device if specified.
25181544Seschrock  */
25191544Seschrock int
252010921STim.Haley@Sun.COM zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
25211544Seschrock {
25221544Seschrock 	zfs_cmd_t zc = { 0 };
25231544Seschrock 	char msg[1024];
25242082Seschrock 	nvlist_t *tgt;
252510921STim.Haley@Sun.COM 	zpool_rewind_policy_t policy;
25265450Sbrendan 	boolean_t avail_spare, l2cache;
25272082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
252810921STim.Haley@Sun.COM 	nvlist_t *nvi = NULL;
25291544Seschrock 
25301544Seschrock 	if (path)
25311544Seschrock 		(void) snprintf(msg, sizeof (msg),
25321544Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
25332676Seschrock 		    path);
25341544Seschrock 	else
25351544Seschrock 		(void) snprintf(msg, sizeof (msg),
25361544Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
25371544Seschrock 		    zhp->zpool_name);
25381544Seschrock 
25391544Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
25402082Seschrock 	if (path) {
25415450Sbrendan 		if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
25427326SEric.Schrock@Sun.COM 		    &l2cache, NULL)) == 0)
25432082Seschrock 			return (zfs_error(hdl, EZFS_NODEVICE, msg));
25442082Seschrock 
25455450Sbrendan 		/*
25465450Sbrendan 		 * Don't allow error clearing for hot spares.  Do allow
25475450Sbrendan 		 * error clearing for l2cache devices.
25485450Sbrendan 		 */
25492468Sek110237 		if (avail_spare)
25502082Seschrock 			return (zfs_error(hdl, EZFS_ISSPARE, msg));
25512082Seschrock 
25522082Seschrock 		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
25532082Seschrock 		    &zc.zc_guid) == 0);
25541544Seschrock 	}
25551544Seschrock 
255610921STim.Haley@Sun.COM 	zpool_get_rewind_policy(rewindnvl, &policy);
255710921STim.Haley@Sun.COM 	zc.zc_cookie = policy.zrp_request;
255810921STim.Haley@Sun.COM 
255910921STim.Haley@Sun.COM 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 8192) != 0)
256010921STim.Haley@Sun.COM 		return (-1);
256110921STim.Haley@Sun.COM 
256210921STim.Haley@Sun.COM 	if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, rewindnvl) != 0)
256310921STim.Haley@Sun.COM 		return (-1);
256410921STim.Haley@Sun.COM 
256510921STim.Haley@Sun.COM 	if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0 ||
256610921STim.Haley@Sun.COM 	    ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
256710921STim.Haley@Sun.COM 	    errno != EPERM && errno != EACCES)) {
256810921STim.Haley@Sun.COM 		if (policy.zrp_request &
256910921STim.Haley@Sun.COM 		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
257010921STim.Haley@Sun.COM 			(void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
257110921STim.Haley@Sun.COM 			zpool_rewind_exclaim(hdl, zc.zc_name,
257210921STim.Haley@Sun.COM 			    ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
257310921STim.Haley@Sun.COM 			    nvi);
257410921STim.Haley@Sun.COM 			nvlist_free(nvi);
257510921STim.Haley@Sun.COM 		}
257610921STim.Haley@Sun.COM 		zcmd_free_nvlists(&zc);
25771544Seschrock 		return (0);
257810921STim.Haley@Sun.COM 	}
257910921STim.Haley@Sun.COM 
258010921STim.Haley@Sun.COM 	zcmd_free_nvlists(&zc);
25812082Seschrock 	return (zpool_standard_error(hdl, errno, msg));
2582789Sahrens }
2583789Sahrens 
25843126Sahl /*
25854451Seschrock  * Similar to zpool_clear(), but takes a GUID (used by fmd).
25864451Seschrock  */
25874451Seschrock int
25884451Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
25894451Seschrock {
25904451Seschrock 	zfs_cmd_t zc = { 0 };
25914451Seschrock 	char msg[1024];
25924451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
25934451Seschrock 
25944451Seschrock 	(void) snprintf(msg, sizeof (msg),
25954451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
25964451Seschrock 	    guid);
25974451Seschrock 
25984451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
25994451Seschrock 	zc.zc_guid = guid;
26004451Seschrock 
26014451Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
26024451Seschrock 		return (0);
26034451Seschrock 
26044451Seschrock 	return (zpool_standard_error(hdl, errno, msg));
26054451Seschrock }
26064451Seschrock 
26074451Seschrock /*
26081354Seschrock  * Convert from a devid string to a path.
26091354Seschrock  */
26101354Seschrock static char *
26111354Seschrock devid_to_path(char *devid_str)
26121354Seschrock {
26131354Seschrock 	ddi_devid_t devid;
26141354Seschrock 	char *minor;
26151354Seschrock 	char *path;
26161354Seschrock 	devid_nmlist_t *list = NULL;
26171354Seschrock 	int ret;
26181354Seschrock 
26191354Seschrock 	if (devid_str_decode(devid_str, &devid, &minor) != 0)
26201354Seschrock 		return (NULL);
26211354Seschrock 
26221354Seschrock 	ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
26231354Seschrock 
26241354Seschrock 	devid_str_free(minor);
26251354Seschrock 	devid_free(devid);
26261354Seschrock 
26271354Seschrock 	if (ret != 0)
26281354Seschrock 		return (NULL);
26291354Seschrock 
26302082Seschrock 	if ((path = strdup(list[0].devname)) == NULL)
26312082Seschrock 		return (NULL);
26322082Seschrock 
26331354Seschrock 	devid_free_nmlist(list);
26341354Seschrock 
26351354Seschrock 	return (path);
26361354Seschrock }
26371354Seschrock 
26381354Seschrock /*
26391354Seschrock  * Convert from a path to a devid string.
26401354Seschrock  */
26411354Seschrock static char *
26421354Seschrock path_to_devid(const char *path)
26431354Seschrock {
26441354Seschrock 	int fd;
26451354Seschrock 	ddi_devid_t devid;
26461354Seschrock 	char *minor, *ret;
26471354Seschrock 
26481354Seschrock 	if ((fd = open(path, O_RDONLY)) < 0)
26491354Seschrock 		return (NULL);
26501354Seschrock 
26511354Seschrock 	minor = NULL;
26521354Seschrock 	ret = NULL;
26531354Seschrock 	if (devid_get(fd, &devid) == 0) {
26541354Seschrock 		if (devid_get_minor_name(fd, &minor) == 0)
26551354Seschrock 			ret = devid_str_encode(devid, minor);
26561354Seschrock 		if (minor != NULL)
26571354Seschrock 			devid_str_free(minor);
26581354Seschrock 		devid_free(devid);
26591354Seschrock 	}
26601354Seschrock 	(void) close(fd);
26611354Seschrock 
26621354Seschrock 	return (ret);
26631354Seschrock }
26641354Seschrock 
26651354Seschrock /*
26661354Seschrock  * Issue the necessary ioctl() to update the stored path value for the vdev.  We
26671354Seschrock  * ignore any failure here, since a common case is for an unprivileged user to
26681354Seschrock  * type 'zpool status', and we'll display the correct information anyway.
26691354Seschrock  */
26701354Seschrock static void
26711354Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
26721354Seschrock {
26731354Seschrock 	zfs_cmd_t zc = { 0 };
26741354Seschrock 
26751354Seschrock 	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
26762676Seschrock 	(void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
26771354Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
26781544Seschrock 	    &zc.zc_guid) == 0);
26791354Seschrock 
26802082Seschrock 	(void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
26811354Seschrock }
26821354Seschrock 
26831354Seschrock /*
26841354Seschrock  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
26851354Seschrock  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
26861354Seschrock  * We also check if this is a whole disk, in which case we strip off the
26871354Seschrock  * trailing 's0' slice name.
26881354Seschrock  *
26891354Seschrock  * This routine is also responsible for identifying when disks have been
26901354Seschrock  * reconfigured in a new location.  The kernel will have opened the device by
26911354Seschrock  * devid, but the path will still refer to the old location.  To catch this, we
26921354Seschrock  * first do a path -> devid translation (which is fast for the common case).  If
26931354Seschrock  * the devid matches, we're done.  If not, we do a reverse devid -> path
26941354Seschrock  * translation and issue the appropriate ioctl() to update the path of the vdev.
26951354Seschrock  * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
26961354Seschrock  * of these checks.
26971354Seschrock  */
26981354Seschrock char *
269910594SGeorge.Wilson@Sun.COM zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
270010594SGeorge.Wilson@Sun.COM     boolean_t verbose)
27011354Seschrock {
27021354Seschrock 	char *path, *devid;
27031544Seschrock 	uint64_t value;
27041544Seschrock 	char buf[64];
27054451Seschrock 	vdev_stat_t *vs;
27064451Seschrock 	uint_t vsc;
27071354Seschrock 
27081544Seschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
27091544Seschrock 	    &value) == 0) {
27101544Seschrock 		verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
27111544Seschrock 		    &value) == 0);
27122856Snd150628 		(void) snprintf(buf, sizeof (buf), "%llu",
27132856Snd150628 		    (u_longlong_t)value);
27141544Seschrock 		path = buf;
27151544Seschrock 	} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
27161354Seschrock 
27174451Seschrock 		/*
27184451Seschrock 		 * If the device is dead (faulted, offline, etc) then don't
27194451Seschrock 		 * bother opening it.  Otherwise we may be forcing the user to
27204451Seschrock 		 * open a misbehaving device, which can have undesirable
27214451Seschrock 		 * effects.
27224451Seschrock 		 */
27234451Seschrock 		if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
27244451Seschrock 		    (uint64_t **)&vs, &vsc) != 0 ||
27254451Seschrock 		    vs->vs_state >= VDEV_STATE_DEGRADED) &&
27264451Seschrock 		    zhp != NULL &&
27271354Seschrock 		    nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
27281354Seschrock 			/*
27291354Seschrock 			 * Determine if the current path is correct.
27301354Seschrock 			 */
27311354Seschrock 			char *newdevid = path_to_devid(path);
27321354Seschrock 
27331354Seschrock 			if (newdevid == NULL ||
27341354Seschrock 			    strcmp(devid, newdevid) != 0) {
27351354Seschrock 				char *newpath;
27361354Seschrock 
27371354Seschrock 				if ((newpath = devid_to_path(devid)) != NULL) {
27381354Seschrock 					/*
27391354Seschrock 					 * Update the path appropriately.
27401354Seschrock 					 */
27411354Seschrock 					set_path(zhp, nv, newpath);
27422082Seschrock 					if (nvlist_add_string(nv,
27432082Seschrock 					    ZPOOL_CONFIG_PATH, newpath) == 0)
27442082Seschrock 						verify(nvlist_lookup_string(nv,
27452082Seschrock 						    ZPOOL_CONFIG_PATH,
27462082Seschrock 						    &path) == 0);
27471354Seschrock 					free(newpath);
27481354Seschrock 				}
27491354Seschrock 			}
27501354Seschrock 
27512082Seschrock 			if (newdevid)
27522082Seschrock 				devid_str_free(newdevid);
27531354Seschrock 		}
27541354Seschrock 
27551354Seschrock 		if (strncmp(path, "/dev/dsk/", 9) == 0)
27561354Seschrock 			path += 9;
27571354Seschrock 
27581354Seschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
27591544Seschrock 		    &value) == 0 && value) {
27602082Seschrock 			char *tmp = zfs_strdup(hdl, path);
27612082Seschrock 			if (tmp == NULL)
27622082Seschrock 				return (NULL);
27631354Seschrock 			tmp[strlen(path) - 2] = '\0';
27641354Seschrock 			return (tmp);
27651354Seschrock 		}
27661354Seschrock 	} else {
27671354Seschrock 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
27682082Seschrock 
27692082Seschrock 		/*
27702082Seschrock 		 * If it's a raidz device, we need to stick in the parity level.
27712082Seschrock 		 */
27722082Seschrock 		if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
27732082Seschrock 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
27742082Seschrock 			    &value) == 0);
27752082Seschrock 			(void) snprintf(buf, sizeof (buf), "%s%llu", path,
27762856Snd150628 			    (u_longlong_t)value);
27772082Seschrock 			path = buf;
27782082Seschrock 		}
277910594SGeorge.Wilson@Sun.COM 
278010594SGeorge.Wilson@Sun.COM 		/*
278110594SGeorge.Wilson@Sun.COM 		 * We identify each top-level vdev by using a <type-id>
278210594SGeorge.Wilson@Sun.COM 		 * naming convention.
278310594SGeorge.Wilson@Sun.COM 		 */
278410594SGeorge.Wilson@Sun.COM 		if (verbose) {
278510594SGeorge.Wilson@Sun.COM 			uint64_t id;
278610594SGeorge.Wilson@Sun.COM 
278710594SGeorge.Wilson@Sun.COM 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
278810594SGeorge.Wilson@Sun.COM 			    &id) == 0);
278910594SGeorge.Wilson@Sun.COM 			(void) snprintf(buf, sizeof (buf), "%s-%llu", path,
279010594SGeorge.Wilson@Sun.COM 			    (u_longlong_t)id);
279110594SGeorge.Wilson@Sun.COM 			path = buf;
279210594SGeorge.Wilson@Sun.COM 		}
27931354Seschrock 	}
27941354Seschrock 
27952082Seschrock 	return (zfs_strdup(hdl, path));
27961354Seschrock }
27971544Seschrock 
27981544Seschrock static int
27991544Seschrock zbookmark_compare(const void *a, const void *b)
28001544Seschrock {
28011544Seschrock 	return (memcmp(a, b, sizeof (zbookmark_t)));
28021544Seschrock }
28031544Seschrock 
28041544Seschrock /*
28051544Seschrock  * Retrieve the persistent error log, uniquify the members, and return to the
28061544Seschrock  * caller.
28071544Seschrock  */
28081544Seschrock int
28093444Sek110237 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
28101544Seschrock {
28111544Seschrock 	zfs_cmd_t zc = { 0 };
28121544Seschrock 	uint64_t count;
28132676Seschrock 	zbookmark_t *zb = NULL;
28143444Sek110237 	int i;
28151544Seschrock 
28161544Seschrock 	/*
28171544Seschrock 	 * Retrieve the raw error list from the kernel.  If the number of errors
28181544Seschrock 	 * has increased, allocate more space and continue until we get the
28191544Seschrock 	 * entire list.
28201544Seschrock 	 */
28211544Seschrock 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
28221544Seschrock 	    &count) == 0);
28234820Sek110237 	if (count == 0)
28244820Sek110237 		return (0);
28252676Seschrock 	if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
28262856Snd150628 	    count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
28272082Seschrock 		return (-1);
28282676Seschrock 	zc.zc_nvlist_dst_size = count;
28291544Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
28301544Seschrock 	for (;;) {
28312082Seschrock 		if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
28322082Seschrock 		    &zc) != 0) {
28332676Seschrock 			free((void *)(uintptr_t)zc.zc_nvlist_dst);
28341544Seschrock 			if (errno == ENOMEM) {
28353823Svb160487 				count = zc.zc_nvlist_dst_size;
28362676Seschrock 				if ((zc.zc_nvlist_dst = (uintptr_t)
28373823Svb160487 				    zfs_alloc(zhp->zpool_hdl, count *
28383823Svb160487 				    sizeof (zbookmark_t))) == (uintptr_t)NULL)
28392082Seschrock 					return (-1);
28401544Seschrock 			} else {
28411544Seschrock 				return (-1);
28421544Seschrock 			}
28431544Seschrock 		} else {
28441544Seschrock 			break;
28451544Seschrock 		}
28461544Seschrock 	}
28471544Seschrock 
28481544Seschrock 	/*
28491544Seschrock 	 * Sort the resulting bookmarks.  This is a little confusing due to the
28501544Seschrock 	 * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
28512676Seschrock 	 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
28521544Seschrock 	 * _not_ copied as part of the process.  So we point the start of our
28531544Seschrock 	 * array appropriate and decrement the total number of elements.
28541544Seschrock 	 */
28552676Seschrock 	zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
28562676Seschrock 	    zc.zc_nvlist_dst_size;
28572676Seschrock 	count -= zc.zc_nvlist_dst_size;
28581544Seschrock 
28591544Seschrock 	qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
28601544Seschrock 
28613444Sek110237 	verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
28621544Seschrock 
28631544Seschrock 	/*
28643444Sek110237 	 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
28651544Seschrock 	 */
28661544Seschrock 	for (i = 0; i < count; i++) {
28671544Seschrock 		nvlist_t *nv;
28681544Seschrock 
28693700Sek110237 		/* ignoring zb_blkid and zb_level for now */
28703700Sek110237 		if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
28713700Sek110237 		    zb[i-1].zb_object == zb[i].zb_object)
28721544Seschrock 			continue;
28731544Seschrock 
28743444Sek110237 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
28753444Sek110237 			goto nomem;
28763444Sek110237 		if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
28773444Sek110237 		    zb[i].zb_objset) != 0) {
28783444Sek110237 			nvlist_free(nv);
28792082Seschrock 			goto nomem;
28803444Sek110237 		}
28813444Sek110237 		if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
28823444Sek110237 		    zb[i].zb_object) != 0) {
28833444Sek110237 			nvlist_free(nv);
28843444Sek110237 			goto nomem;
28851544Seschrock 		}
28863444Sek110237 		if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
28873444Sek110237 			nvlist_free(nv);
28883444Sek110237 			goto nomem;
28893444Sek110237 		}
28903444Sek110237 		nvlist_free(nv);
28911544Seschrock 	}
28921544Seschrock 
28933265Sahrens 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
28941544Seschrock 	return (0);
28952082Seschrock 
28962082Seschrock nomem:
28972676Seschrock 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
28982082Seschrock 	return (no_memory(zhp->zpool_hdl));
28991544Seschrock }
29001760Seschrock 
29011760Seschrock /*
29021760Seschrock  * Upgrade a ZFS pool to the latest on-disk version.
29031760Seschrock  */
29041760Seschrock int
29055094Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
29061760Seschrock {
29071760Seschrock 	zfs_cmd_t zc = { 0 };
29082082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
29091760Seschrock 
29101760Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
29115094Slling 	zc.zc_cookie = new_version;
29125094Slling 
29134543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
29143237Slling 		return (zpool_standard_error_fmt(hdl, errno,
29152082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
29162082Seschrock 		    zhp->zpool_name));
29171760Seschrock 	return (0);
29181760Seschrock }
29192926Sek110237 
29204988Sek110237 void
29214988Sek110237 zpool_set_history_str(const char *subcommand, int argc, char **argv,
29224988Sek110237     char *history_str)
29234988Sek110237 {
29244988Sek110237 	int i;
29254988Sek110237 
29264988Sek110237 	(void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
29274988Sek110237 	for (i = 1; i < argc; i++) {
29284988Sek110237 		if (strlen(history_str) + 1 + strlen(argv[i]) >
29294988Sek110237 		    HIS_MAX_RECORD_LEN)
29304988Sek110237 			break;
29314988Sek110237 		(void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
29324988Sek110237 		(void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
29334988Sek110237 	}
29344988Sek110237 }
29354988Sek110237 
29362926Sek110237 /*
29374988Sek110237  * Stage command history for logging.
29382926Sek110237  */
29394988Sek110237 int
29404988Sek110237 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
29412926Sek110237 {
29424988Sek110237 	if (history_str == NULL)
29434988Sek110237 		return (EINVAL);
29444988Sek110237 
29454988Sek110237 	if (strlen(history_str) > HIS_MAX_RECORD_LEN)
29464988Sek110237 		return (EINVAL);
29472926Sek110237 
29484715Sek110237 	if (hdl->libzfs_log_str != NULL)
29494543Smarks 		free(hdl->libzfs_log_str);
29502926Sek110237 
29514988Sek110237 	if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
29524988Sek110237 		return (no_memory(hdl));
29534543Smarks 
29544988Sek110237 	return (0);
29552926Sek110237 }
29562926Sek110237 
29572926Sek110237 /*
29582926Sek110237  * Perform ioctl to get some command history of a pool.
29592926Sek110237  *
29602926Sek110237  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
29612926Sek110237  * logical offset of the history buffer to start reading from.
29622926Sek110237  *
29632926Sek110237  * Upon return, 'off' is the next logical offset to read from and
29642926Sek110237  * 'len' is the actual amount of bytes read into 'buf'.
29652926Sek110237  */
29662926Sek110237 static int
29672926Sek110237 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
29682926Sek110237 {
29692926Sek110237 	zfs_cmd_t zc = { 0 };
29702926Sek110237 	libzfs_handle_t *hdl = zhp->zpool_hdl;
29712926Sek110237 
29722926Sek110237 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
29732926Sek110237 
29742926Sek110237 	zc.zc_history = (uint64_t)(uintptr_t)buf;
29752926Sek110237 	zc.zc_history_len = *len;
29762926Sek110237 	zc.zc_history_offset = *off;
29772926Sek110237 
29782926Sek110237 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
29792926Sek110237 		switch (errno) {
29802926Sek110237 		case EPERM:
29813237Slling 			return (zfs_error_fmt(hdl, EZFS_PERM,
29823237Slling 			    dgettext(TEXT_DOMAIN,
29832926Sek110237 			    "cannot show history for pool '%s'"),
29842926Sek110237 			    zhp->zpool_name));
29852926Sek110237 		case ENOENT:
29863237Slling 			return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
29872926Sek110237 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
29882926Sek110237 			    "'%s'"), zhp->zpool_name));
29893863Sek110237 		case ENOTSUP:
29903863Sek110237 			return (zfs_error_fmt(hdl, EZFS_BADVERSION,
29913863Sek110237 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
29923863Sek110237 			    "'%s', pool must be upgraded"), zhp->zpool_name));
29932926Sek110237 		default:
29943237Slling 			return (zpool_standard_error_fmt(hdl, errno,
29952926Sek110237 			    dgettext(TEXT_DOMAIN,
29962926Sek110237 			    "cannot get history for '%s'"), zhp->zpool_name));
29972926Sek110237 		}
29982926Sek110237 	}
29992926Sek110237 
30002926Sek110237 	*len = zc.zc_history_len;
30012926Sek110237 	*off = zc.zc_history_offset;
30022926Sek110237 
30032926Sek110237 	return (0);
30042926Sek110237 }
30052926Sek110237 
30062926Sek110237 /*
30072926Sek110237  * Process the buffer of nvlists, unpacking and storing each nvlist record
30082926Sek110237  * into 'records'.  'leftover' is set to the number of bytes that weren't
30092926Sek110237  * processed as there wasn't a complete record.
30102926Sek110237  */
301110685SGeorge.Wilson@Sun.COM int
30122926Sek110237 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
30132926Sek110237     nvlist_t ***records, uint_t *numrecords)
30142926Sek110237 {
30152926Sek110237 	uint64_t reclen;
30162926Sek110237 	nvlist_t *nv;
30172926Sek110237 	int i;
30182926Sek110237 
30192926Sek110237 	while (bytes_read > sizeof (reclen)) {
30202926Sek110237 
30212926Sek110237 		/* get length of packed record (stored as little endian) */
30222926Sek110237 		for (i = 0, reclen = 0; i < sizeof (reclen); i++)
30232926Sek110237 			reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
30242926Sek110237 
30252926Sek110237 		if (bytes_read < sizeof (reclen) + reclen)
30262926Sek110237 			break;
30272926Sek110237 
30282926Sek110237 		/* unpack record */
30292926Sek110237 		if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
30302926Sek110237 			return (ENOMEM);
30312926Sek110237 		bytes_read -= sizeof (reclen) + reclen;
30322926Sek110237 		buf += sizeof (reclen) + reclen;
30332926Sek110237 
30342926Sek110237 		/* add record to nvlist array */
30352926Sek110237 		(*numrecords)++;
30362926Sek110237 		if (ISP2(*numrecords + 1)) {
30372926Sek110237 			*records = realloc(*records,
30382926Sek110237 			    *numrecords * 2 * sizeof (nvlist_t *));
30392926Sek110237 		}
30402926Sek110237 		(*records)[*numrecords - 1] = nv;
30412926Sek110237 	}
30422926Sek110237 
30432926Sek110237 	*leftover = bytes_read;
30442926Sek110237 	return (0);
30452926Sek110237 }
30462926Sek110237 
30472926Sek110237 #define	HIS_BUF_LEN	(128*1024)
30482926Sek110237 
30492926Sek110237 /*
30502926Sek110237  * Retrieve the command history of a pool.
30512926Sek110237  */
30522926Sek110237 int
30532926Sek110237 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
30542926Sek110237 {
30552926Sek110237 	char buf[HIS_BUF_LEN];
30562926Sek110237 	uint64_t off = 0;
30572926Sek110237 	nvlist_t **records = NULL;
30582926Sek110237 	uint_t numrecords = 0;
30592926Sek110237 	int err, i;
30602926Sek110237 
30612926Sek110237 	do {
30622926Sek110237 		uint64_t bytes_read = sizeof (buf);
30632926Sek110237 		uint64_t leftover;
30642926Sek110237 
30652926Sek110237 		if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
30662926Sek110237 			break;
30672926Sek110237 
30682926Sek110237 		/* if nothing else was read in, we're at EOF, just return */
30692926Sek110237 		if (!bytes_read)
30702926Sek110237 			break;
30712926Sek110237 
30722926Sek110237 		if ((err = zpool_history_unpack(buf, bytes_read,
30732926Sek110237 		    &leftover, &records, &numrecords)) != 0)
30742926Sek110237 			break;
30752926Sek110237 		off -= leftover;
30762926Sek110237 
30772926Sek110237 		/* CONSTCOND */
30782926Sek110237 	} while (1);
30792926Sek110237 
30802926Sek110237 	if (!err) {
30812926Sek110237 		verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
30822926Sek110237 		verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
30832926Sek110237 		    records, numrecords) == 0);
30842926Sek110237 	}
30852926Sek110237 	for (i = 0; i < numrecords; i++)
30862926Sek110237 		nvlist_free(records[i]);
30872926Sek110237 	free(records);
30882926Sek110237 
30892926Sek110237 	return (err);
30902926Sek110237 }
30913444Sek110237 
30923444Sek110237 void
30933444Sek110237 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
30943444Sek110237     char *pathname, size_t len)
30953444Sek110237 {
30963444Sek110237 	zfs_cmd_t zc = { 0 };
30973444Sek110237 	boolean_t mounted = B_FALSE;
30983444Sek110237 	char *mntpnt = NULL;
30993444Sek110237 	char dsname[MAXNAMELEN];
31003444Sek110237 
31013444Sek110237 	if (dsobj == 0) {
31023444Sek110237 		/* special case for the MOS */
31033444Sek110237 		(void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
31043444Sek110237 		return;
31053444Sek110237 	}
31063444Sek110237 
31073444Sek110237 	/* get the dataset's name */
31083444Sek110237 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
31093444Sek110237 	zc.zc_obj = dsobj;
31103444Sek110237 	if (ioctl(zhp->zpool_hdl->libzfs_fd,
31113444Sek110237 	    ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
31123444Sek110237 		/* just write out a path of two object numbers */
31133444Sek110237 		(void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
31143444Sek110237 		    dsobj, obj);
31153444Sek110237 		return;
31163444Sek110237 	}
31173444Sek110237 	(void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
31183444Sek110237 
31193444Sek110237 	/* find out if the dataset is mounted */
31203444Sek110237 	mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
31213444Sek110237 
31223444Sek110237 	/* get the corrupted object's path */
31233444Sek110237 	(void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
31243444Sek110237 	zc.zc_obj = obj;
31253444Sek110237 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
31263444Sek110237 	    &zc) == 0) {
31273444Sek110237 		if (mounted) {
31283444Sek110237 			(void) snprintf(pathname, len, "%s%s", mntpnt,
31293444Sek110237 			    zc.zc_value);
31303444Sek110237 		} else {
31313444Sek110237 			(void) snprintf(pathname, len, "%s:%s",
31323444Sek110237 			    dsname, zc.zc_value);
31333444Sek110237 		}
31343444Sek110237 	} else {
31353444Sek110237 		(void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
31363444Sek110237 	}
31373444Sek110237 	free(mntpnt);
31383444Sek110237 }
31393912Slling 
31404276Staylor /*
31417042Sgw25295  * Read the EFI label from the config, if a label does not exist then
31427042Sgw25295  * pass back the error to the caller. If the caller has passed a non-NULL
31437042Sgw25295  * diskaddr argument then we set it to the starting address of the EFI
31447042Sgw25295  * partition.
31457042Sgw25295  */
31467042Sgw25295 static int
31477042Sgw25295 read_efi_label(nvlist_t *config, diskaddr_t *sb)
31487042Sgw25295 {
31497042Sgw25295 	char *path;
31507042Sgw25295 	int fd;
31517042Sgw25295 	char diskname[MAXPATHLEN];
31527042Sgw25295 	int err = -1;
31537042Sgw25295 
31547042Sgw25295 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
31557042Sgw25295 		return (err);
31567042Sgw25295 
31577042Sgw25295 	(void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
31587042Sgw25295 	    strrchr(path, '/'));
31597042Sgw25295 	if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
31607042Sgw25295 		struct dk_gpt *vtoc;
31617042Sgw25295 
31627042Sgw25295 		if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
31637042Sgw25295 			if (sb != NULL)
31647042Sgw25295 				*sb = vtoc->efi_parts[0].p_start;
31657042Sgw25295 			efi_free(vtoc);
31667042Sgw25295 		}
31677042Sgw25295 		(void) close(fd);
31687042Sgw25295 	}
31697042Sgw25295 	return (err);
31707042Sgw25295 }
31717042Sgw25295 
31727042Sgw25295 /*
31734276Staylor  * determine where a partition starts on a disk in the current
31744276Staylor  * configuration
31754276Staylor  */
31764276Staylor static diskaddr_t
31774276Staylor find_start_block(nvlist_t *config)
31784276Staylor {
31794276Staylor 	nvlist_t **child;
31804276Staylor 	uint_t c, children;
31814276Staylor 	diskaddr_t sb = MAXOFFSET_T;
31824276Staylor 	uint64_t wholedisk;
31834276Staylor 
31844276Staylor 	if (nvlist_lookup_nvlist_array(config,
31854276Staylor 	    ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
31864276Staylor 		if (nvlist_lookup_uint64(config,
31874276Staylor 		    ZPOOL_CONFIG_WHOLE_DISK,
31884276Staylor 		    &wholedisk) != 0 || !wholedisk) {
31894276Staylor 			return (MAXOFFSET_T);
31904276Staylor 		}
31917042Sgw25295 		if (read_efi_label(config, &sb) < 0)
31927042Sgw25295 			sb = MAXOFFSET_T;
31934276Staylor 		return (sb);
31944276Staylor 	}
31954276Staylor 
31964276Staylor 	for (c = 0; c < children; c++) {
31974276Staylor 		sb = find_start_block(child[c]);
31984276Staylor 		if (sb != MAXOFFSET_T) {
31994276Staylor 			return (sb);
32004276Staylor 		}
32014276Staylor 	}
32024276Staylor 	return (MAXOFFSET_T);
32034276Staylor }
32044276Staylor 
32054276Staylor /*
32064276Staylor  * Label an individual disk.  The name provided is the short name,
32074276Staylor  * stripped of any leading /dev path.
32084276Staylor  */
32094276Staylor int
32104276Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
32114276Staylor {
32124276Staylor 	char path[MAXPATHLEN];
32134276Staylor 	struct dk_gpt *vtoc;
32144276Staylor 	int fd;
32154276Staylor 	size_t resv = EFI_MIN_RESV_SIZE;
32164276Staylor 	uint64_t slice_size;
32174276Staylor 	diskaddr_t start_block;
32184276Staylor 	char errbuf[1024];
32194276Staylor 
32206289Smmusante 	/* prepare an error message just in case */
32216289Smmusante 	(void) snprintf(errbuf, sizeof (errbuf),
32226289Smmusante 	    dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
32236289Smmusante 
32244276Staylor 	if (zhp) {
32254276Staylor 		nvlist_t *nvroot;
32264276Staylor 
32277965SGeorge.Wilson@Sun.COM 		if (pool_is_bootable(zhp)) {
32287965SGeorge.Wilson@Sun.COM 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
32297965SGeorge.Wilson@Sun.COM 			    "EFI labeled devices are not supported on root "
32307965SGeorge.Wilson@Sun.COM 			    "pools."));
32317965SGeorge.Wilson@Sun.COM 			return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
32327965SGeorge.Wilson@Sun.COM 		}
32337965SGeorge.Wilson@Sun.COM 
32344276Staylor 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
32354276Staylor 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
32364276Staylor 
32374276Staylor 		if (zhp->zpool_start_block == 0)
32384276Staylor 			start_block = find_start_block(nvroot);
32394276Staylor 		else
32404276Staylor 			start_block = zhp->zpool_start_block;
32414276Staylor 		zhp->zpool_start_block = start_block;
32424276Staylor 	} else {
32434276Staylor 		/* new pool */
32444276Staylor 		start_block = NEW_START_BLOCK;
32454276Staylor 	}
32464276Staylor 
32474276Staylor 	(void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
32484276Staylor 	    BACKUP_SLICE);
32494276Staylor 
32504276Staylor 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
32514276Staylor 		/*
32524276Staylor 		 * This shouldn't happen.  We've long since verified that this
32534276Staylor 		 * is a valid device.
32544276Staylor 		 */
32556289Smmusante 		zfs_error_aux(hdl,
32566289Smmusante 		    dgettext(TEXT_DOMAIN, "unable to open device"));
32574276Staylor 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
32584276Staylor 	}
32594276Staylor 
32604276Staylor 	if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
32614276Staylor 		/*
32624276Staylor 		 * The only way this can fail is if we run out of memory, or we
32634276Staylor 		 * were unable to read the disk's capacity
32644276Staylor 		 */
32654276Staylor 		if (errno == ENOMEM)
32664276Staylor 			(void) no_memory(hdl);
32674276Staylor 
32684276Staylor 		(void) close(fd);
32696289Smmusante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
32706289Smmusante 		    "unable to read disk capacity"), name);
32714276Staylor 
32724276Staylor 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
32734276Staylor 	}
32744276Staylor 
32754276Staylor 	slice_size = vtoc->efi_last_u_lba + 1;
32764276Staylor 	slice_size -= EFI_MIN_RESV_SIZE;
32774276Staylor 	if (start_block == MAXOFFSET_T)
32784276Staylor 		start_block = NEW_START_BLOCK;
32794276Staylor 	slice_size -= start_block;
32804276Staylor 
32814276Staylor 	vtoc->efi_parts[0].p_start = start_block;
32824276Staylor 	vtoc->efi_parts[0].p_size = slice_size;
32834276Staylor 
32844276Staylor 	/*
32854276Staylor 	 * Why we use V_USR: V_BACKUP confuses users, and is considered
32864276Staylor 	 * disposable by some EFI utilities (since EFI doesn't have a backup
32874276Staylor 	 * slice).  V_UNASSIGNED is supposed to be used only for zero size
32884276Staylor 	 * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
32894276Staylor 	 * etc. were all pretty specific.  V_USR is as close to reality as we
32904276Staylor 	 * can get, in the absence of V_OTHER.
32914276Staylor 	 */
32924276Staylor 	vtoc->efi_parts[0].p_tag = V_USR;
32934276Staylor 	(void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
32944276Staylor 
32954276Staylor 	vtoc->efi_parts[8].p_start = slice_size + start_block;
32964276Staylor 	vtoc->efi_parts[8].p_size = resv;
32974276Staylor 	vtoc->efi_parts[8].p_tag = V_RESERVED;
32984276Staylor 
32994276Staylor 	if (efi_write(fd, vtoc) != 0) {
33004276Staylor 		/*
33014276Staylor 		 * Some block drivers (like pcata) may not support EFI
33024276Staylor 		 * GPT labels.  Print out a helpful error message dir-
33034276Staylor 		 * ecting the user to manually label the disk and give
33044276Staylor 		 * a specific slice.
33054276Staylor 		 */
33064276Staylor 		(void) close(fd);
33074276Staylor 		efi_free(vtoc);
33084276Staylor 
33094276Staylor 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33106289Smmusante 		    "try using fdisk(1M) and then provide a specific slice"));
33114276Staylor 		return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
33124276Staylor 	}
33134276Staylor 
33144276Staylor 	(void) close(fd);
33154276Staylor 	efi_free(vtoc);
33164276Staylor 	return (0);
33174276Staylor }
33186423Sgw25295 
33196423Sgw25295 static boolean_t
33206423Sgw25295 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
33216423Sgw25295 {
33226423Sgw25295 	char *type;
33236423Sgw25295 	nvlist_t **child;
33246423Sgw25295 	uint_t children, c;
33256423Sgw25295 
33266423Sgw25295 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
33276423Sgw25295 	if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
33286423Sgw25295 	    strcmp(type, VDEV_TYPE_FILE) == 0 ||
33296423Sgw25295 	    strcmp(type, VDEV_TYPE_LOG) == 0 ||
333010594SGeorge.Wilson@Sun.COM 	    strcmp(type, VDEV_TYPE_HOLE) == 0 ||
33316423Sgw25295 	    strcmp(type, VDEV_TYPE_MISSING) == 0) {
33326423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33336423Sgw25295 		    "vdev type '%s' is not supported"), type);
33346423Sgw25295 		(void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
33356423Sgw25295 		return (B_FALSE);
33366423Sgw25295 	}
33376423Sgw25295 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
33386423Sgw25295 	    &child, &children) == 0) {
33396423Sgw25295 		for (c = 0; c < children; c++) {
33406423Sgw25295 			if (!supported_dump_vdev_type(hdl, child[c], errbuf))
33416423Sgw25295 				return (B_FALSE);
33426423Sgw25295 		}
33436423Sgw25295 	}
33446423Sgw25295 	return (B_TRUE);
33456423Sgw25295 }
33466423Sgw25295 
33476423Sgw25295 /*
33486423Sgw25295  * check if this zvol is allowable for use as a dump device; zero if
33496423Sgw25295  * it is, > 0 if it isn't, < 0 if it isn't a zvol
33506423Sgw25295  */
33516423Sgw25295 int
33526423Sgw25295 zvol_check_dump_config(char *arg)
33536423Sgw25295 {
33546423Sgw25295 	zpool_handle_t *zhp = NULL;
33556423Sgw25295 	nvlist_t *config, *nvroot;
33566423Sgw25295 	char *p, *volname;
33576423Sgw25295 	nvlist_t **top;
33586423Sgw25295 	uint_t toplevels;
33596423Sgw25295 	libzfs_handle_t *hdl;
33606423Sgw25295 	char errbuf[1024];
33616423Sgw25295 	char poolname[ZPOOL_MAXNAMELEN];
33626423Sgw25295 	int pathlen = strlen(ZVOL_FULL_DEV_DIR);
33636423Sgw25295 	int ret = 1;
33646423Sgw25295 
33656423Sgw25295 	if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
33666423Sgw25295 		return (-1);
33676423Sgw25295 	}
33686423Sgw25295 
33696423Sgw25295 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
33706423Sgw25295 	    "dump is not supported on device '%s'"), arg);
33716423Sgw25295 
33726423Sgw25295 	if ((hdl = libzfs_init()) == NULL)
33736423Sgw25295 		return (1);
33746423Sgw25295 	libzfs_print_on_error(hdl, B_TRUE);
33756423Sgw25295 
33766423Sgw25295 	volname = arg + pathlen;
33776423Sgw25295 
33786423Sgw25295 	/* check the configuration of the pool */
33796423Sgw25295 	if ((p = strchr(volname, '/')) == NULL) {
33806423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33816423Sgw25295 		    "malformed dataset name"));
33826423Sgw25295 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
33836423Sgw25295 		return (1);
33846423Sgw25295 	} else if (p - volname >= ZFS_MAXNAMELEN) {
33856423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33866423Sgw25295 		    "dataset name is too long"));
33876423Sgw25295 		(void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
33886423Sgw25295 		return (1);
33896423Sgw25295 	} else {
33906423Sgw25295 		(void) strncpy(poolname, volname, p - volname);
33916423Sgw25295 		poolname[p - volname] = '\0';
33926423Sgw25295 	}
33936423Sgw25295 
33946423Sgw25295 	if ((zhp = zpool_open(hdl, poolname)) == NULL) {
33956423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33966423Sgw25295 		    "could not open pool '%s'"), poolname);
33976423Sgw25295 		(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
33986423Sgw25295 		goto out;
33996423Sgw25295 	}
34006423Sgw25295 	config = zpool_get_config(zhp, NULL);
34016423Sgw25295 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
34026423Sgw25295 	    &nvroot) != 0) {
34036423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34046423Sgw25295 		    "could not obtain vdev configuration for  '%s'"), poolname);
34056423Sgw25295 		(void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
34066423Sgw25295 		goto out;
34076423Sgw25295 	}
34086423Sgw25295 
34096423Sgw25295 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
34106423Sgw25295 	    &top, &toplevels) == 0);
34116423Sgw25295 	if (toplevels != 1) {
34126423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34136423Sgw25295 		    "'%s' has multiple top level vdevs"), poolname);
34146423Sgw25295 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
34156423Sgw25295 		goto out;
34166423Sgw25295 	}
34176423Sgw25295 
34186423Sgw25295 	if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
34196423Sgw25295 		goto out;
34206423Sgw25295 	}
34216423Sgw25295 	ret = 0;
34226423Sgw25295 
34236423Sgw25295 out:
34246423Sgw25295 	if (zhp)
34256423Sgw25295 		zpool_close(zhp);
34266423Sgw25295 	libzfs_fini(hdl);
34276423Sgw25295 	return (ret);
34286423Sgw25295 }
3429