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 /*
2312296SLin.Ling@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24789Sahrens  */
25789Sahrens 
26789Sahrens #include <ctype.h>
27789Sahrens #include <errno.h>
28789Sahrens #include <devid.h>
29789Sahrens #include <fcntl.h>
30789Sahrens #include <libintl.h>
31789Sahrens #include <stdio.h>
32789Sahrens #include <stdlib.h>
333126Sahl #include <strings.h>
34789Sahrens #include <unistd.h>
354276Staylor #include <sys/efi_partition.h>
364276Staylor #include <sys/vtoc.h>
37789Sahrens #include <sys/zfs_ioctl.h>
389816SGeorge.Wilson@Sun.COM #include <dlfcn.h>
39789Sahrens 
40789Sahrens #include "zfs_namecheck.h"
413912Slling #include "zfs_prop.h"
42789Sahrens #include "libzfs_impl.h"
4310921STim.Haley@Sun.COM #include "zfs_comutil.h"
44789Sahrens 
457042Sgw25295 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
465094Slling 
479816SGeorge.Wilson@Sun.COM #define	DISK_ROOT	"/dev/dsk"
489816SGeorge.Wilson@Sun.COM #define	RDISK_ROOT	"/dev/rdsk"
499816SGeorge.Wilson@Sun.COM #define	BACKUP_SLICE	"s2"
509816SGeorge.Wilson@Sun.COM 
515094Slling /*
525094Slling  * ====================================================================
535094Slling  *   zpool property functions
545094Slling  * ====================================================================
555094Slling  */
565094Slling 
575094Slling static int
585094Slling zpool_get_all_props(zpool_handle_t *zhp)
595094Slling {
605094Slling 	zfs_cmd_t zc = { 0 };
615094Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
625094Slling 
635094Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
645094Slling 
655094Slling 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
665094Slling 		return (-1);
675094Slling 
685094Slling 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
695094Slling 		if (errno == ENOMEM) {
705094Slling 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
715094Slling 				zcmd_free_nvlists(&zc);
725094Slling 				return (-1);
735094Slling 			}
745094Slling 		} else {
755094Slling 			zcmd_free_nvlists(&zc);
765094Slling 			return (-1);
775094Slling 		}
785094Slling 	}
795094Slling 
805094Slling 	if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
815094Slling 		zcmd_free_nvlists(&zc);
825094Slling 		return (-1);
835094Slling 	}
845094Slling 
855094Slling 	zcmd_free_nvlists(&zc);
865094Slling 
875094Slling 	return (0);
885094Slling }
895094Slling 
905094Slling static int
915094Slling zpool_props_refresh(zpool_handle_t *zhp)
925094Slling {
935094Slling 	nvlist_t *old_props;
945094Slling 
955094Slling 	old_props = zhp->zpool_props;
965094Slling 
975094Slling 	if (zpool_get_all_props(zhp) != 0)
985094Slling 		return (-1);
995094Slling 
1005094Slling 	nvlist_free(old_props);
1015094Slling 	return (0);
1025094Slling }
1035094Slling 
1045094Slling static char *
1055094Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
1065094Slling     zprop_source_t *src)
1075094Slling {
1085094Slling 	nvlist_t *nv, *nvl;
1095094Slling 	uint64_t ival;
1105094Slling 	char *value;
1115094Slling 	zprop_source_t source;
1125094Slling 
1135094Slling 	nvl = zhp->zpool_props;
1145094Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
1155094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
1165094Slling 		source = ival;
1175094Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1185094Slling 	} else {
1195094Slling 		source = ZPROP_SRC_DEFAULT;
1205094Slling 		if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
1215094Slling 			value = "-";
1225094Slling 	}
1235094Slling 
1245094Slling 	if (src)
1255094Slling 		*src = source;
1265094Slling 
1275094Slling 	return (value);
1285094Slling }
1295094Slling 
1305094Slling uint64_t
1315094Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
1325094Slling {
1335094Slling 	nvlist_t *nv, *nvl;
1345094Slling 	uint64_t value;
1355094Slling 	zprop_source_t source;
1365094Slling 
1377294Sperrin 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
1387294Sperrin 		/*
1397294Sperrin 		 * zpool_get_all_props() has most likely failed because
1407294Sperrin 		 * the pool is faulted, but if all we need is the top level
1417294Sperrin 		 * vdev's guid then get it from the zhp config nvlist.
1427294Sperrin 		 */
1437294Sperrin 		if ((prop == ZPOOL_PROP_GUID) &&
1447294Sperrin 		    (nvlist_lookup_nvlist(zhp->zpool_config,
1457294Sperrin 		    ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
1467294Sperrin 		    (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
1477294Sperrin 		    == 0)) {
1487294Sperrin 			return (value);
1497294Sperrin 		}
1505094Slling 		return (zpool_prop_default_numeric(prop));
1517294Sperrin 	}
1525094Slling 
1535094Slling 	nvl = zhp->zpool_props;
1545094Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
1555094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
1565094Slling 		source = value;
1575094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1585094Slling 	} else {
1595094Slling 		source = ZPROP_SRC_DEFAULT;
1605094Slling 		value = zpool_prop_default_numeric(prop);
1615094Slling 	}
1625094Slling 
1635094Slling 	if (src)
1645094Slling 		*src = source;
1655094Slling 
1665094Slling 	return (value);
1675094Slling }
1685094Slling 
1695094Slling /*
1705094Slling  * Map VDEV STATE to printed strings.
1715094Slling  */
1725094Slling char *
1735094Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
1745094Slling {
1755094Slling 	switch (state) {
1765094Slling 	case VDEV_STATE_CLOSED:
1775094Slling 	case VDEV_STATE_OFFLINE:
1785094Slling 		return (gettext("OFFLINE"));
1795094Slling 	case VDEV_STATE_REMOVED:
1805094Slling 		return (gettext("REMOVED"));
1815094Slling 	case VDEV_STATE_CANT_OPEN:
1827294Sperrin 		if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
1835094Slling 			return (gettext("FAULTED"));
18411422SMark.Musante@Sun.COM 		else if (aux == VDEV_AUX_SPLIT_POOL)
18511422SMark.Musante@Sun.COM 			return (gettext("SPLIT"));
1865094Slling 		else
1875094Slling 			return (gettext("UNAVAIL"));
1885094Slling 	case VDEV_STATE_FAULTED:
1895094Slling 		return (gettext("FAULTED"));
1905094Slling 	case VDEV_STATE_DEGRADED:
1915094Slling 		return (gettext("DEGRADED"));
1925094Slling 	case VDEV_STATE_HEALTHY:
1935094Slling 		return (gettext("ONLINE"));
1945094Slling 	}
1955094Slling 
1965094Slling 	return (gettext("UNKNOWN"));
1975094Slling }
1985094Slling 
1995094Slling /*
2005094Slling  * Get a zpool property value for 'prop' and return the value in
2015094Slling  * a pre-allocated buffer.
2025094Slling  */
2035094Slling int
2045094Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
2055094Slling     zprop_source_t *srctype)
2065094Slling {
2075094Slling 	uint64_t intval;
2085094Slling 	const char *strval;
2095094Slling 	zprop_source_t src = ZPROP_SRC_NONE;
2105094Slling 	nvlist_t *nvroot;
2115094Slling 	vdev_stat_t *vs;
2125094Slling 	uint_t vsc;
2135094Slling 
2145094Slling 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
2158525SEric.Schrock@Sun.COM 		switch (prop) {
2168525SEric.Schrock@Sun.COM 		case ZPOOL_PROP_NAME:
2175094Slling 			(void) strlcpy(buf, zpool_get_name(zhp), len);
2188525SEric.Schrock@Sun.COM 			break;
2198525SEric.Schrock@Sun.COM 
2208525SEric.Schrock@Sun.COM 		case ZPOOL_PROP_HEALTH:
2215094Slling 			(void) strlcpy(buf, "FAULTED", len);
2228525SEric.Schrock@Sun.COM 			break;
2238525SEric.Schrock@Sun.COM 
2248525SEric.Schrock@Sun.COM 		case ZPOOL_PROP_GUID:
2258525SEric.Schrock@Sun.COM 			intval = zpool_get_prop_int(zhp, prop, &src);
2268525SEric.Schrock@Sun.COM 			(void) snprintf(buf, len, "%llu", intval);
2278525SEric.Schrock@Sun.COM 			break;
2288525SEric.Schrock@Sun.COM 
2298525SEric.Schrock@Sun.COM 		case ZPOOL_PROP_ALTROOT:
2308525SEric.Schrock@Sun.COM 		case ZPOOL_PROP_CACHEFILE:
2318525SEric.Schrock@Sun.COM 			if (zhp->zpool_props != NULL ||
2328525SEric.Schrock@Sun.COM 			    zpool_get_all_props(zhp) == 0) {
2338525SEric.Schrock@Sun.COM 				(void) strlcpy(buf,
2348525SEric.Schrock@Sun.COM 				    zpool_get_prop_string(zhp, prop, &src),
2358525SEric.Schrock@Sun.COM 				    len);
2368525SEric.Schrock@Sun.COM 				if (srctype != NULL)
2378525SEric.Schrock@Sun.COM 					*srctype = src;
2388525SEric.Schrock@Sun.COM 				return (0);
2398525SEric.Schrock@Sun.COM 			}
2408525SEric.Schrock@Sun.COM 			/* FALLTHROUGH */
2418525SEric.Schrock@Sun.COM 		default:
2425094Slling 			(void) strlcpy(buf, "-", len);
2438525SEric.Schrock@Sun.COM 			break;
2448525SEric.Schrock@Sun.COM 		}
2458525SEric.Schrock@Sun.COM 
2468525SEric.Schrock@Sun.COM 		if (srctype != NULL)
2478525SEric.Schrock@Sun.COM 			*srctype = src;
2485094Slling 		return (0);
2495094Slling 	}
2505094Slling 
2515094Slling 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
2525094Slling 	    prop != ZPOOL_PROP_NAME)
2535094Slling 		return (-1);
2545094Slling 
2555094Slling 	switch (zpool_prop_get_type(prop)) {
2565094Slling 	case PROP_TYPE_STRING:
2575094Slling 		(void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
2585094Slling 		    len);
2595094Slling 		break;
2605094Slling 
2615094Slling 	case PROP_TYPE_NUMBER:
2625094Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
2635094Slling 
2645094Slling 		switch (prop) {
2655094Slling 		case ZPOOL_PROP_SIZE:
26610956SGeorge.Wilson@Sun.COM 		case ZPOOL_PROP_ALLOCATED:
26710956SGeorge.Wilson@Sun.COM 		case ZPOOL_PROP_FREE:
2685094Slling 			(void) zfs_nicenum(intval, buf, len);
2695094Slling 			break;
2705094Slling 
2715094Slling 		case ZPOOL_PROP_CAPACITY:
2725094Slling 			(void) snprintf(buf, len, "%llu%%",
2735094Slling 			    (u_longlong_t)intval);
2745094Slling 			break;
2755094Slling 
27610922SJeff.Bonwick@Sun.COM 		case ZPOOL_PROP_DEDUPRATIO:
27710922SJeff.Bonwick@Sun.COM 			(void) snprintf(buf, len, "%llu.%02llux",
27810922SJeff.Bonwick@Sun.COM 			    (u_longlong_t)(intval / 100),
27910922SJeff.Bonwick@Sun.COM 			    (u_longlong_t)(intval % 100));
28010922SJeff.Bonwick@Sun.COM 			break;
28110922SJeff.Bonwick@Sun.COM 
2825094Slling 		case ZPOOL_PROP_HEALTH:
2835094Slling 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2845094Slling 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2855094Slling 			verify(nvlist_lookup_uint64_array(nvroot,
28612296SLin.Ling@Sun.COM 			    ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
28712296SLin.Ling@Sun.COM 			    == 0);
2885094Slling 
2895094Slling 			(void) strlcpy(buf, zpool_state_to_name(intval,
2905094Slling 			    vs->vs_aux), len);
2915094Slling 			break;
2925094Slling 		default:
2935094Slling 			(void) snprintf(buf, len, "%llu", intval);
2945094Slling 		}
2955094Slling 		break;
2965094Slling 
2975094Slling 	case PROP_TYPE_INDEX:
2985094Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
2995094Slling 		if (zpool_prop_index_to_string(prop, intval, &strval)
3005094Slling 		    != 0)
3015094Slling 			return (-1);
3025094Slling 		(void) strlcpy(buf, strval, len);
3035094Slling 		break;
3045094Slling 
3055094Slling 	default:
3065094Slling 		abort();
3075094Slling 	}
3085094Slling 
3095094Slling 	if (srctype)
3105094Slling 		*srctype = src;
3115094Slling 
3125094Slling 	return (0);
3135094Slling }
3145094Slling 
3155094Slling /*
3165094Slling  * Check if the bootfs name has the same pool name as it is set to.
3175094Slling  * Assuming bootfs is a valid dataset name.
3185094Slling  */
3195094Slling static boolean_t
3205094Slling bootfs_name_valid(const char *pool, char *bootfs)
3215094Slling {
3225094Slling 	int len = strlen(pool);
3235094Slling 
3247300SEric.Taylor@Sun.COM 	if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
3255094Slling 		return (B_FALSE);
3265094Slling 
3275094Slling 	if (strncmp(pool, bootfs, len) == 0 &&
3285094Slling 	    (bootfs[len] == '/' || bootfs[len] == '\0'))
3295094Slling 		return (B_TRUE);
3305094Slling 
3315094Slling 	return (B_FALSE);
3325094Slling }
3335094Slling 
3345094Slling /*
3357042Sgw25295  * Inspect the configuration to determine if any of the devices contain
3367042Sgw25295  * an EFI label.
3377042Sgw25295  */
3387042Sgw25295 static boolean_t
3397042Sgw25295 pool_uses_efi(nvlist_t *config)
3407042Sgw25295 {
3417042Sgw25295 	nvlist_t **child;
3427042Sgw25295 	uint_t c, children;
3437042Sgw25295 
3447042Sgw25295 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
3457042Sgw25295 	    &child, &children) != 0)
3467042Sgw25295 		return (read_efi_label(config, NULL) >= 0);
3477042Sgw25295 
3487042Sgw25295 	for (c = 0; c < children; c++) {
3497042Sgw25295 		if (pool_uses_efi(child[c]))
3507042Sgw25295 			return (B_TRUE);
3517042Sgw25295 	}
3527042Sgw25295 	return (B_FALSE);
3537042Sgw25295 }
3547042Sgw25295 
3557965SGeorge.Wilson@Sun.COM static boolean_t
3567965SGeorge.Wilson@Sun.COM pool_is_bootable(zpool_handle_t *zhp)
3577965SGeorge.Wilson@Sun.COM {
3587965SGeorge.Wilson@Sun.COM 	char bootfs[ZPOOL_MAXNAMELEN];
3597965SGeorge.Wilson@Sun.COM 
3607965SGeorge.Wilson@Sun.COM 	return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
3617965SGeorge.Wilson@Sun.COM 	    sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
3627965SGeorge.Wilson@Sun.COM 	    sizeof (bootfs)) != 0);
3637965SGeorge.Wilson@Sun.COM }
3647965SGeorge.Wilson@Sun.COM 
3657965SGeorge.Wilson@Sun.COM 
3667042Sgw25295 /*
3675094Slling  * Given an nvlist of zpool properties to be set, validate that they are
3685094Slling  * correct, and parse any numeric properties (index, boolean, etc) if they are
3695094Slling  * specified as strings.
3705094Slling  */
3715094Slling static nvlist_t *
3727184Stimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
3735094Slling     nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf)
3745094Slling {
3755094Slling 	nvpair_t *elem;
3765094Slling 	nvlist_t *retprops;
3775094Slling 	zpool_prop_t prop;
3785094Slling 	char *strval;
3795094Slling 	uint64_t intval;
3805363Seschrock 	char *slash;
3815363Seschrock 	struct stat64 statbuf;
3827042Sgw25295 	zpool_handle_t *zhp;
3837042Sgw25295 	nvlist_t *nvroot;
3845094Slling 
3855094Slling 	if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
3865094Slling 		(void) no_memory(hdl);
3875094Slling 		return (NULL);
3885094Slling 	}
3895094Slling 
3905094Slling 	elem = NULL;
3915094Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
3925094Slling 		const char *propname = nvpair_name(elem);
3935094Slling 
3945094Slling 		/*
3955094Slling 		 * Make sure this property is valid and applies to this type.
3965094Slling 		 */
3975094Slling 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
3985094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3995094Slling 			    "invalid property '%s'"), propname);
4005094Slling 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
4015094Slling 			goto error;
4025094Slling 		}
4035094Slling 
4045094Slling 		if (zpool_prop_readonly(prop)) {
4055094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
4065094Slling 			    "is readonly"), propname);
4075094Slling 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
4085094Slling 			goto error;
4095094Slling 		}
4105094Slling 
4115094Slling 		if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
4125094Slling 		    &strval, &intval, errbuf) != 0)
4135094Slling 			goto error;
4145094Slling 
4155094Slling 		/*
4165094Slling 		 * Perform additional checking for specific properties.
4175094Slling 		 */
4185094Slling 		switch (prop) {
4195094Slling 		case ZPOOL_PROP_VERSION:
4205094Slling 			if (intval < version || intval > SPA_VERSION) {
4215094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4225094Slling 				    "property '%s' number %d is invalid."),
4235094Slling 				    propname, intval);
4245094Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4255094Slling 				goto error;
4265094Slling 			}
4275094Slling 			break;
4285094Slling 
4295094Slling 		case ZPOOL_PROP_BOOTFS:
4305094Slling 			if (create_or_import) {
4315094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4325094Slling 				    "property '%s' cannot be set at creation "
4335094Slling 				    "or import time"), propname);
4345094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
4355094Slling 				goto error;
4365094Slling 			}
4375094Slling 
4385094Slling 			if (version < SPA_VERSION_BOOTFS) {
4395094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4405094Slling 				    "pool must be upgraded to support "
4415094Slling 				    "'%s' property"), propname);
4425094Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4435094Slling 				goto error;
4445094Slling 			}
4455094Slling 
4465094Slling 			/*
4475094Slling 			 * bootfs property value has to be a dataset name and
4485094Slling 			 * the dataset has to be in the same pool as it sets to.
4495094Slling 			 */
4505094Slling 			if (strval[0] != '\0' && !bootfs_name_valid(poolname,
4515094Slling 			    strval)) {
4525094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
4535094Slling 				    "is an invalid name"), strval);
4545094Slling 				(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4555094Slling 				goto error;
4565094Slling 			}
4577042Sgw25295 
4587042Sgw25295 			if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
4597042Sgw25295 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4607042Sgw25295 				    "could not open pool '%s'"), poolname);
4617042Sgw25295 				(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
4627042Sgw25295 				goto error;
4637042Sgw25295 			}
4647042Sgw25295 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
4657042Sgw25295 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
4667042Sgw25295 
4677042Sgw25295 			/*
4687042Sgw25295 			 * bootfs property cannot be set on a disk which has
4697042Sgw25295 			 * been EFI labeled.
4707042Sgw25295 			 */
4717042Sgw25295 			if (pool_uses_efi(nvroot)) {
4727042Sgw25295 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4737042Sgw25295 				    "property '%s' not supported on "
4747042Sgw25295 				    "EFI labeled devices"), propname);
4757042Sgw25295 				(void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
4767042Sgw25295 				zpool_close(zhp);
4777042Sgw25295 				goto error;
4787042Sgw25295 			}
4797042Sgw25295 			zpool_close(zhp);
4805094Slling 			break;
4815094Slling 
4825094Slling 		case ZPOOL_PROP_ALTROOT:
4835094Slling 			if (!create_or_import) {
4845094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4855094Slling 				    "property '%s' can only be set during pool "
4865094Slling 				    "creation or import"), propname);
4875094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
4885094Slling 				goto error;
4895094Slling 			}
4905094Slling 
4915094Slling 			if (strval[0] != '/') {
4925094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4935094Slling 				    "bad alternate root '%s'"), strval);
4945094Slling 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
4955094Slling 				goto error;
4965094Slling 			}
4975094Slling 			break;
4985363Seschrock 
4995363Seschrock 		case ZPOOL_PROP_CACHEFILE:
5005363Seschrock 			if (strval[0] == '\0')
5015363Seschrock 				break;
5025363Seschrock 
5035363Seschrock 			if (strcmp(strval, "none") == 0)
5045363Seschrock 				break;
5055363Seschrock 
5065363Seschrock 			if (strval[0] != '/') {
5075363Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5085363Seschrock 				    "property '%s' must be empty, an "
5095363Seschrock 				    "absolute path, or 'none'"), propname);
5105363Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
5115363Seschrock 				goto error;
5125363Seschrock 			}
5135363Seschrock 
5145363Seschrock 			slash = strrchr(strval, '/');
5155363Seschrock 
5165363Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
5175363Seschrock 			    strcmp(slash, "/..") == 0) {
5185363Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5195363Seschrock 				    "'%s' is not a valid file"), strval);
5205363Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
5215363Seschrock 				goto error;
5225363Seschrock 			}
5235363Seschrock 
5245363Seschrock 			*slash = '\0';
5255363Seschrock 
5265621Seschrock 			if (strval[0] != '\0' &&
5275621Seschrock 			    (stat64(strval, &statbuf) != 0 ||
5285621Seschrock 			    !S_ISDIR(statbuf.st_mode))) {
5295363Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5305363Seschrock 				    "'%s' is not a valid directory"),
5315363Seschrock 				    strval);
5325363Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
5335363Seschrock 				goto error;
5345363Seschrock 			}
5355363Seschrock 
5365363Seschrock 			*slash = '/';
5375363Seschrock 			break;
5385094Slling 		}
5395094Slling 	}
5405094Slling 
5415094Slling 	return (retprops);
5425094Slling error:
5435094Slling 	nvlist_free(retprops);
5445094Slling 	return (NULL);
5455094Slling }
5465094Slling 
5475094Slling /*
5485094Slling  * Set zpool property : propname=propval.
5495094Slling  */
5505094Slling int
5515094Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
5525094Slling {
5535094Slling 	zfs_cmd_t zc = { 0 };
5545094Slling 	int ret = -1;
5555094Slling 	char errbuf[1024];
5565094Slling 	nvlist_t *nvl = NULL;
5575094Slling 	nvlist_t *realprops;
5585094Slling 	uint64_t version;
5595094Slling 
5605094Slling 	(void) snprintf(errbuf, sizeof (errbuf),
5615094Slling 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
5625094Slling 	    zhp->zpool_name);
5635094Slling 
5645094Slling 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5655094Slling 		return (no_memory(zhp->zpool_hdl));
5665094Slling 
5675094Slling 	if (nvlist_add_string(nvl, propname, propval) != 0) {
5685094Slling 		nvlist_free(nvl);
5695094Slling 		return (no_memory(zhp->zpool_hdl));
5705094Slling 	}
5715094Slling 
5725094Slling 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
5737184Stimh 	if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
5745094Slling 	    zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) {
5755094Slling 		nvlist_free(nvl);
5765094Slling 		return (-1);
5775094Slling 	}
5785094Slling 
5795094Slling 	nvlist_free(nvl);
5805094Slling 	nvl = realprops;
5815094Slling 
5825094Slling 	/*
5835094Slling 	 * Execute the corresponding ioctl() to set this property.
5845094Slling 	 */
5855094Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
5865094Slling 
5875094Slling 	if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
5885094Slling 		nvlist_free(nvl);
5895094Slling 		return (-1);
5905094Slling 	}
5915094Slling 
5925094Slling 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
5935094Slling 
5945094Slling 	zcmd_free_nvlists(&zc);
5955094Slling 	nvlist_free(nvl);
5965094Slling 
5975094Slling 	if (ret)
5985094Slling 		(void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
5995094Slling 	else
6005094Slling 		(void) zpool_props_refresh(zhp);
6015094Slling 
6025094Slling 	return (ret);
6035094Slling }
6045094Slling 
6055094Slling int
6065094Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
6075094Slling {
6085094Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
6095094Slling 	zprop_list_t *entry;
6105094Slling 	char buf[ZFS_MAXPROPLEN];
6115094Slling 
6125094Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
6135094Slling 		return (-1);
6145094Slling 
6155094Slling 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
6165094Slling 
6175094Slling 		if (entry->pl_fixed)
6185094Slling 			continue;
6195094Slling 
6205094Slling 		if (entry->pl_prop != ZPROP_INVAL &&
6215094Slling 		    zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
6225094Slling 		    NULL) == 0) {
6235094Slling 			if (strlen(buf) > entry->pl_width)
6245094Slling 				entry->pl_width = strlen(buf);
6255094Slling 		}
6265094Slling 	}
6275094Slling 
6285094Slling 	return (0);
6295094Slling }
6305094Slling 
6315094Slling 
632789Sahrens /*
6339816SGeorge.Wilson@Sun.COM  * Don't start the slice at the default block of 34; many storage
6349816SGeorge.Wilson@Sun.COM  * devices will use a stripe width of 128k, so start there instead.
6359816SGeorge.Wilson@Sun.COM  */
6369816SGeorge.Wilson@Sun.COM #define	NEW_START_BLOCK	256
6379816SGeorge.Wilson@Sun.COM 
6389816SGeorge.Wilson@Sun.COM /*
639789Sahrens  * Validate the given pool name, optionally putting an extended error message in
640789Sahrens  * 'buf'.
641789Sahrens  */
6426423Sgw25295 boolean_t
6432082Seschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
644789Sahrens {
645789Sahrens 	namecheck_err_t why;
646789Sahrens 	char what;
6471773Seschrock 	int ret;
648789Sahrens 
6491773Seschrock 	ret = pool_namecheck(pool, &why, &what);
6501773Seschrock 
6511773Seschrock 	/*
6521773Seschrock 	 * The rules for reserved pool names were extended at a later point.
6531773Seschrock 	 * But we need to support users with existing pools that may now be
6541773Seschrock 	 * invalid.  So we only check for this expanded set of names during a
6551773Seschrock 	 * create (or import), and only in userland.
6561773Seschrock 	 */
6571773Seschrock 	if (ret == 0 && !isopen &&
6581773Seschrock 	    (strncmp(pool, "mirror", 6) == 0 ||
6591773Seschrock 	    strncmp(pool, "raidz", 5) == 0 ||
6604527Sperrin 	    strncmp(pool, "spare", 5) == 0 ||
6614527Sperrin 	    strcmp(pool, "log") == 0)) {
6626423Sgw25295 		if (hdl != NULL)
6636423Sgw25295 			zfs_error_aux(hdl,
6646423Sgw25295 			    dgettext(TEXT_DOMAIN, "name is reserved"));
6652082Seschrock 		return (B_FALSE);
6661773Seschrock 	}
6671773Seschrock 
6681773Seschrock 
6691773Seschrock 	if (ret != 0) {
6702082Seschrock 		if (hdl != NULL) {
671789Sahrens 			switch (why) {
6721003Slling 			case NAME_ERR_TOOLONG:
6732082Seschrock 				zfs_error_aux(hdl,
6741003Slling 				    dgettext(TEXT_DOMAIN, "name is too long"));
6751003Slling 				break;
6761003Slling 
677789Sahrens 			case NAME_ERR_INVALCHAR:
6782082Seschrock 				zfs_error_aux(hdl,
679789Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
680789Sahrens 				    "'%c' in pool name"), what);
681789Sahrens 				break;
682789Sahrens 
683789Sahrens 			case NAME_ERR_NOLETTER:
6842082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6852082Seschrock 				    "name must begin with a letter"));
686789Sahrens 				break;
687789Sahrens 
688789Sahrens 			case NAME_ERR_RESERVED:
6892082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6902082Seschrock 				    "name is reserved"));
691789Sahrens 				break;
692789Sahrens 
693789Sahrens 			case NAME_ERR_DISKLIKE:
6942082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6952082Seschrock 				    "pool name is reserved"));
696789Sahrens 				break;
6972856Snd150628 
6982856Snd150628 			case NAME_ERR_LEADING_SLASH:
6992856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7002856Snd150628 				    "leading slash in name"));
7012856Snd150628 				break;
7022856Snd150628 
7032856Snd150628 			case NAME_ERR_EMPTY_COMPONENT:
7042856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7052856Snd150628 				    "empty component in name"));
7062856Snd150628 				break;
7072856Snd150628 
7082856Snd150628 			case NAME_ERR_TRAILING_SLASH:
7092856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7102856Snd150628 				    "trailing slash in name"));
7112856Snd150628 				break;
7122856Snd150628 
7132856Snd150628 			case NAME_ERR_MULTIPLE_AT:
7142856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7152856Snd150628 				    "multiple '@' delimiters in name"));
7162856Snd150628 				break;
7172856Snd150628 
718789Sahrens 			}
719789Sahrens 		}
7202082Seschrock 		return (B_FALSE);
721789Sahrens 	}
722789Sahrens 
7232082Seschrock 	return (B_TRUE);
724789Sahrens }
725789Sahrens 
726789Sahrens /*
727789Sahrens  * Open a handle to the given pool, even if the pool is currently in the FAULTED
728789Sahrens  * state.
729789Sahrens  */
730789Sahrens zpool_handle_t *
7312082Seschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
732789Sahrens {
733789Sahrens 	zpool_handle_t *zhp;
7342142Seschrock 	boolean_t missing;
735789Sahrens 
736789Sahrens 	/*
737789Sahrens 	 * Make sure the pool name is valid.
738789Sahrens 	 */
7392082Seschrock 	if (!zpool_name_valid(hdl, B_TRUE, pool)) {
7403237Slling 		(void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
7412082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
7422082Seschrock 		    pool);
743789Sahrens 		return (NULL);
744789Sahrens 	}
745789Sahrens 
7462082Seschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
7472082Seschrock 		return (NULL);
748789Sahrens 
7492082Seschrock 	zhp->zpool_hdl = hdl;
750789Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
751789Sahrens 
7522142Seschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
7532142Seschrock 		zpool_close(zhp);
7542142Seschrock 		return (NULL);
7552142Seschrock 	}
7562142Seschrock 
7572142Seschrock 	if (missing) {
7585094Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
7593237Slling 		(void) zfs_error_fmt(hdl, EZFS_NOENT,
7605094Slling 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
7612142Seschrock 		zpool_close(zhp);
7622142Seschrock 		return (NULL);
763789Sahrens 	}
764789Sahrens 
765789Sahrens 	return (zhp);
766789Sahrens }
767789Sahrens 
768789Sahrens /*
769789Sahrens  * Like the above, but silent on error.  Used when iterating over pools (because
770789Sahrens  * the configuration cache may be out of date).
771789Sahrens  */
7722142Seschrock int
7732142Seschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
774789Sahrens {
775789Sahrens 	zpool_handle_t *zhp;
7762142Seschrock 	boolean_t missing;
777789Sahrens 
7782142Seschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
7792142Seschrock 		return (-1);
780789Sahrens 
7812082Seschrock 	zhp->zpool_hdl = hdl;
782789Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
783789Sahrens 
7842142Seschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
7852142Seschrock 		zpool_close(zhp);
7862142Seschrock 		return (-1);
787789Sahrens 	}
788789Sahrens 
7892142Seschrock 	if (missing) {
7902142Seschrock 		zpool_close(zhp);
7912142Seschrock 		*ret = NULL;
7922142Seschrock 		return (0);
7932142Seschrock 	}
7942142Seschrock 
7952142Seschrock 	*ret = zhp;
7962142Seschrock 	return (0);
797789Sahrens }
798789Sahrens 
799789Sahrens /*
800789Sahrens  * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
801789Sahrens  * state.
802789Sahrens  */
803789Sahrens zpool_handle_t *
8042082Seschrock zpool_open(libzfs_handle_t *hdl, const char *pool)
805789Sahrens {
806789Sahrens 	zpool_handle_t *zhp;
807789Sahrens 
8082082Seschrock 	if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
809789Sahrens 		return (NULL);
810789Sahrens 
811789Sahrens 	if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
8123237Slling 		(void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
8132082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
814789Sahrens 		zpool_close(zhp);
815789Sahrens 		return (NULL);
816789Sahrens 	}
817789Sahrens 
818789Sahrens 	return (zhp);
819789Sahrens }
820789Sahrens 
821789Sahrens /*
822789Sahrens  * Close the handle.  Simply frees the memory associated with the handle.
823789Sahrens  */
824789Sahrens void
825789Sahrens zpool_close(zpool_handle_t *zhp)
826789Sahrens {
827789Sahrens 	if (zhp->zpool_config)
828789Sahrens 		nvlist_free(zhp->zpool_config);
829952Seschrock 	if (zhp->zpool_old_config)
830952Seschrock 		nvlist_free(zhp->zpool_old_config);
8313912Slling 	if (zhp->zpool_props)
8323912Slling 		nvlist_free(zhp->zpool_props);
833789Sahrens 	free(zhp);
834789Sahrens }
835789Sahrens 
836789Sahrens /*
837789Sahrens  * Return the name of the pool.
838789Sahrens  */
839789Sahrens const char *
840789Sahrens zpool_get_name(zpool_handle_t *zhp)
841789Sahrens {
842789Sahrens 	return (zhp->zpool_name);
843789Sahrens }
844789Sahrens 
845789Sahrens 
846789Sahrens /*
847789Sahrens  * Return the state of the pool (ACTIVE or UNAVAILABLE)
848789Sahrens  */
849789Sahrens int
850789Sahrens zpool_get_state(zpool_handle_t *zhp)
851789Sahrens {
852789Sahrens 	return (zhp->zpool_state);
853789Sahrens }
854789Sahrens 
855789Sahrens /*
856789Sahrens  * Create the named pool, using the provided vdev list.  It is assumed
857789Sahrens  * that the consumer has already validated the contents of the nvlist, so we
858789Sahrens  * don't have to worry about error semantics.
859789Sahrens  */
860789Sahrens int
8612082Seschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
8627184Stimh     nvlist_t *props, nvlist_t *fsprops)
863789Sahrens {
864789Sahrens 	zfs_cmd_t zc = { 0 };
8657184Stimh 	nvlist_t *zc_fsprops = NULL;
8667184Stimh 	nvlist_t *zc_props = NULL;
8672082Seschrock 	char msg[1024];
8685094Slling 	char *altroot;
8697184Stimh 	int ret = -1;
8702082Seschrock 
8712082Seschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
8722082Seschrock 	    "cannot create '%s'"), pool);
873789Sahrens 
8742082Seschrock 	if (!zpool_name_valid(hdl, B_FALSE, pool))
8752082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
8762082Seschrock 
8775320Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
8785320Slling 		return (-1);
8795320Slling 
8807184Stimh 	if (props) {
8817184Stimh 		if ((zc_props = zpool_valid_proplist(hdl, pool, props,
8827184Stimh 		    SPA_VERSION_1, B_TRUE, msg)) == NULL) {
8837184Stimh 			goto create_failed;
8847184Stimh 		}
8855320Slling 	}
886789Sahrens 
8877184Stimh 	if (fsprops) {
8887184Stimh 		uint64_t zoned;
8897184Stimh 		char *zonestr;
8907184Stimh 
8917184Stimh 		zoned = ((nvlist_lookup_string(fsprops,
8927184Stimh 		    zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
8937184Stimh 		    strcmp(zonestr, "on") == 0);
8947184Stimh 
8957184Stimh 		if ((zc_fsprops = zfs_valid_proplist(hdl,
8967184Stimh 		    ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
8977184Stimh 			goto create_failed;
8987184Stimh 		}
8997184Stimh 		if (!zc_props &&
9007184Stimh 		    (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
9017184Stimh 			goto create_failed;
9027184Stimh 		}
9037184Stimh 		if (nvlist_add_nvlist(zc_props,
9047184Stimh 		    ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
9057184Stimh 			goto create_failed;
9067184Stimh 		}
9077184Stimh 	}
9087184Stimh 
9097184Stimh 	if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
9107184Stimh 		goto create_failed;
9117184Stimh 
912789Sahrens 	(void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
913789Sahrens 
9147184Stimh 	if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
9155320Slling 
9162676Seschrock 		zcmd_free_nvlists(&zc);
9177184Stimh 		nvlist_free(zc_props);
9187184Stimh 		nvlist_free(zc_fsprops);
9192082Seschrock 
920789Sahrens 		switch (errno) {
921789Sahrens 		case EBUSY:
922789Sahrens 			/*
923789Sahrens 			 * This can happen if the user has specified the same
924789Sahrens 			 * device multiple times.  We can't reliably detect this
925789Sahrens 			 * until we try to add it and see we already have a
926789Sahrens 			 * label.
927789Sahrens 			 */
9282082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9292082Seschrock 			    "one or more vdevs refer to the same device"));
9302082Seschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
931789Sahrens 
932789Sahrens 		case EOVERFLOW:
933789Sahrens 			/*
9342082Seschrock 			 * This occurs when one of the devices is below
935789Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
936789Sahrens 			 * device was the problem device since there's no
937789Sahrens 			 * reliable way to determine device size from userland.
938789Sahrens 			 */
939789Sahrens 			{
940789Sahrens 				char buf[64];
941789Sahrens 
942789Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
943789Sahrens 
9442082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9452082Seschrock 				    "one or more devices is less than the "
9462082Seschrock 				    "minimum size (%s)"), buf);
947789Sahrens 			}
9482082Seschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
949789Sahrens 
950789Sahrens 		case ENOSPC:
9512082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9522082Seschrock 			    "one or more devices is out of space"));
9532082Seschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
954789Sahrens 
9555450Sbrendan 		case ENOTBLK:
9565450Sbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9575450Sbrendan 			    "cache device must be a disk or disk slice"));
9585450Sbrendan 			return (zfs_error(hdl, EZFS_BADDEV, msg));
9595450Sbrendan 
960789Sahrens 		default:
9612082Seschrock 			return (zpool_standard_error(hdl, errno, msg));
962789Sahrens 		}
963789Sahrens 	}
964789Sahrens 
965789Sahrens 	/*
966789Sahrens 	 * If this is an alternate root pool, then we automatically set the
9672676Seschrock 	 * mountpoint of the root dataset to be '/'.
968789Sahrens 	 */
9695094Slling 	if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
9705094Slling 	    &altroot) == 0) {
971789Sahrens 		zfs_handle_t *zhp;
972789Sahrens 
9735094Slling 		verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
9742676Seschrock 		verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
9752676Seschrock 		    "/") == 0);
976789Sahrens 
977789Sahrens 		zfs_close(zhp);
978789Sahrens 	}
979789Sahrens 
9807184Stimh create_failed:
9815320Slling 	zcmd_free_nvlists(&zc);
9827184Stimh 	nvlist_free(zc_props);
9837184Stimh 	nvlist_free(zc_fsprops);
9847184Stimh 	return (ret);
985789Sahrens }
986789Sahrens 
987789Sahrens /*
988789Sahrens  * Destroy the given pool.  It is up to the caller to ensure that there are no
989789Sahrens  * datasets left in the pool.
990789Sahrens  */
991789Sahrens int
992789Sahrens zpool_destroy(zpool_handle_t *zhp)
993789Sahrens {
994789Sahrens 	zfs_cmd_t zc = { 0 };
995789Sahrens 	zfs_handle_t *zfp = NULL;
9962082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
9972082Seschrock 	char msg[1024];
998789Sahrens 
999789Sahrens 	if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1000*13037SMark.Musante@Sun.COM 	    (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1001789Sahrens 		return (-1);
1002789Sahrens 
1003789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1004789Sahrens 
1005*13037SMark.Musante@Sun.COM 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
10062082Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
10072082Seschrock 		    "cannot destroy '%s'"), zhp->zpool_name);
1008789Sahrens 
10092082Seschrock 		if (errno == EROFS) {
10102082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10112082Seschrock 			    "one or more devices is read only"));
10122082Seschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
10132082Seschrock 		} else {
10142082Seschrock 			(void) zpool_standard_error(hdl, errno, msg);
1015789Sahrens 		}
1016789Sahrens 
1017789Sahrens 		if (zfp)
1018789Sahrens 			zfs_close(zfp);
1019789Sahrens 		return (-1);
1020789Sahrens 	}
1021789Sahrens 
1022789Sahrens 	if (zfp) {
1023789Sahrens 		remove_mountpoint(zfp);
1024789Sahrens 		zfs_close(zfp);
1025789Sahrens 	}
1026789Sahrens 
1027789Sahrens 	return (0);
1028789Sahrens }
1029789Sahrens 
1030789Sahrens /*
1031789Sahrens  * Add the given vdevs to the pool.  The caller must have already performed the
1032789Sahrens  * necessary verification to ensure that the vdev specification is well-formed.
1033789Sahrens  */
1034789Sahrens int
1035789Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1036789Sahrens {
10372676Seschrock 	zfs_cmd_t zc = { 0 };
10382082Seschrock 	int ret;
10392082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
10402082Seschrock 	char msg[1024];
10415450Sbrendan 	nvlist_t **spares, **l2cache;
10425450Sbrendan 	uint_t nspares, nl2cache;
10432082Seschrock 
10442082Seschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
10452082Seschrock 	    "cannot add to '%s'"), zhp->zpool_name);
10462082Seschrock 
10475450Sbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
10485450Sbrendan 	    SPA_VERSION_SPARES &&
10492082Seschrock 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
10502082Seschrock 	    &spares, &nspares) == 0) {
10512082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
10522082Seschrock 		    "upgraded to add hot spares"));
10532082Seschrock 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
10542082Seschrock 	}
1055789Sahrens 
10567965SGeorge.Wilson@Sun.COM 	if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
10577965SGeorge.Wilson@Sun.COM 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
10587965SGeorge.Wilson@Sun.COM 		uint64_t s;
10597965SGeorge.Wilson@Sun.COM 
10607965SGeorge.Wilson@Sun.COM 		for (s = 0; s < nspares; s++) {
10617965SGeorge.Wilson@Sun.COM 			char *path;
10627965SGeorge.Wilson@Sun.COM 
10637965SGeorge.Wilson@Sun.COM 			if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
10647965SGeorge.Wilson@Sun.COM 			    &path) == 0 && pool_uses_efi(spares[s])) {
10657965SGeorge.Wilson@Sun.COM 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10667965SGeorge.Wilson@Sun.COM 				    "device '%s' contains an EFI label and "
10677965SGeorge.Wilson@Sun.COM 				    "cannot be used on root pools."),
106810594SGeorge.Wilson@Sun.COM 				    zpool_vdev_name(hdl, NULL, spares[s],
106910594SGeorge.Wilson@Sun.COM 				    B_FALSE));
10707965SGeorge.Wilson@Sun.COM 				return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
10717965SGeorge.Wilson@Sun.COM 			}
10727965SGeorge.Wilson@Sun.COM 		}
10737965SGeorge.Wilson@Sun.COM 	}
10747965SGeorge.Wilson@Sun.COM 
10755450Sbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
10765450Sbrendan 	    SPA_VERSION_L2CACHE &&
10775450Sbrendan 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
10785450Sbrendan 	    &l2cache, &nl2cache) == 0) {
10795450Sbrendan 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
10805450Sbrendan 		    "upgraded to add cache devices"));
10815450Sbrendan 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
10825450Sbrendan 	}
10835450Sbrendan 
10845094Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
10852082Seschrock 		return (-1);
1086789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1087789Sahrens 
1088*13037SMark.Musante@Sun.COM 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1089789Sahrens 		switch (errno) {
1090789Sahrens 		case EBUSY:
1091789Sahrens 			/*
1092789Sahrens 			 * This can happen if the user has specified the same
1093789Sahrens 			 * device multiple times.  We can't reliably detect this
1094789Sahrens 			 * until we try to add it and see we already have a
1095789Sahrens 			 * label.
1096789Sahrens 			 */
10972082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10982082Seschrock 			    "one or more vdevs refer to the same device"));
10992082Seschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1100789Sahrens 			break;
1101789Sahrens 
1102789Sahrens 		case EOVERFLOW:
1103789Sahrens 			/*
1104789Sahrens 			 * This occurrs when one of the devices is below
1105789Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1106789Sahrens 			 * device was the problem device since there's no
1107789Sahrens 			 * reliable way to determine device size from userland.
1108789Sahrens 			 */
1109789Sahrens 			{
1110789Sahrens 				char buf[64];
1111789Sahrens 
1112789Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1113789Sahrens 
11142082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11152082Seschrock 				    "device is less than the minimum "
11162082Seschrock 				    "size (%s)"), buf);
1117789Sahrens 			}
11182082Seschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
11192082Seschrock 			break;
11202082Seschrock 
11212082Seschrock 		case ENOTSUP:
11222082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11234527Sperrin 			    "pool must be upgraded to add these vdevs"));
11242082Seschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, msg);
1125789Sahrens 			break;
1126789Sahrens 
11273912Slling 		case EDOM:
11283912Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11294527Sperrin 			    "root pool can not have multiple vdevs"
11304527Sperrin 			    " or separate logs"));
11313912Slling 			(void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
11323912Slling 			break;
11333912Slling 
11345450Sbrendan 		case ENOTBLK:
11355450Sbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11365450Sbrendan 			    "cache device must be a disk or disk slice"));
11375450Sbrendan 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
11385450Sbrendan 			break;
11395450Sbrendan 
1140789Sahrens 		default:
11412082Seschrock 			(void) zpool_standard_error(hdl, errno, msg);
1142789Sahrens 		}
1143789Sahrens 
11442082Seschrock 		ret = -1;
11452082Seschrock 	} else {
11462082Seschrock 		ret = 0;
1147789Sahrens 	}
1148789Sahrens 
11492676Seschrock 	zcmd_free_nvlists(&zc);
1150789Sahrens 
11512082Seschrock 	return (ret);
1152789Sahrens }
1153789Sahrens 
1154789Sahrens /*
1155789Sahrens  * Exports the pool from the system.  The caller must ensure that there are no
1156789Sahrens  * mounted datasets in the pool.
1157789Sahrens  */
1158789Sahrens int
11598211SGeorge.Wilson@Sun.COM zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce)
1160789Sahrens {
1161789Sahrens 	zfs_cmd_t zc = { 0 };
11627214Slling 	char msg[1024];
1163789Sahrens 
11647214Slling 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
11657214Slling 	    "cannot export '%s'"), zhp->zpool_name);
11667214Slling 
1167789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
11687214Slling 	zc.zc_cookie = force;
11698211SGeorge.Wilson@Sun.COM 	zc.zc_guid = hardforce;
11707214Slling 
11717214Slling 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
11727214Slling 		switch (errno) {
11737214Slling 		case EXDEV:
11747214Slling 			zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
11757214Slling 			    "use '-f' to override the following errors:\n"
11767214Slling 			    "'%s' has an active shared spare which could be"
11777214Slling 			    " used by other pools once '%s' is exported."),
11787214Slling 			    zhp->zpool_name, zhp->zpool_name);
11797214Slling 			return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
11807214Slling 			    msg));
11817214Slling 		default:
11827214Slling 			return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
11837214Slling 			    msg));
11847214Slling 		}
11857214Slling 	}
11867214Slling 
1187789Sahrens 	return (0);
1188789Sahrens }
1189789Sahrens 
11908211SGeorge.Wilson@Sun.COM int
11918211SGeorge.Wilson@Sun.COM zpool_export(zpool_handle_t *zhp, boolean_t force)
11928211SGeorge.Wilson@Sun.COM {
11938211SGeorge.Wilson@Sun.COM 	return (zpool_export_common(zhp, force, B_FALSE));
11948211SGeorge.Wilson@Sun.COM }
11958211SGeorge.Wilson@Sun.COM 
11968211SGeorge.Wilson@Sun.COM int
11978211SGeorge.Wilson@Sun.COM zpool_export_force(zpool_handle_t *zhp)
11988211SGeorge.Wilson@Sun.COM {
11998211SGeorge.Wilson@Sun.COM 	return (zpool_export_common(zhp, B_TRUE, B_TRUE));
12008211SGeorge.Wilson@Sun.COM }
12018211SGeorge.Wilson@Sun.COM 
120210921STim.Haley@Sun.COM static void
120310921STim.Haley@Sun.COM zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
120412949SGeorge.Wilson@Sun.COM     nvlist_t *config)
120510921STim.Haley@Sun.COM {
120612949SGeorge.Wilson@Sun.COM 	nvlist_t *nv = NULL;
120710921STim.Haley@Sun.COM 	uint64_t rewindto;
120810921STim.Haley@Sun.COM 	int64_t loss = -1;
120910921STim.Haley@Sun.COM 	struct tm t;
121010921STim.Haley@Sun.COM 	char timestr[128];
121110921STim.Haley@Sun.COM 
121212949SGeorge.Wilson@Sun.COM 	if (!hdl->libzfs_printerr || config == NULL)
121312949SGeorge.Wilson@Sun.COM 		return;
121412949SGeorge.Wilson@Sun.COM 
121512949SGeorge.Wilson@Sun.COM 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0)
121610921STim.Haley@Sun.COM 		return;
121710921STim.Haley@Sun.COM 
121812949SGeorge.Wilson@Sun.COM 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
121910921STim.Haley@Sun.COM 		return;
122012949SGeorge.Wilson@Sun.COM 	(void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
122110921STim.Haley@Sun.COM 
122210921STim.Haley@Sun.COM 	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
122310921STim.Haley@Sun.COM 	    strftime(timestr, 128, 0, &t) != 0) {
122410921STim.Haley@Sun.COM 		if (dryrun) {
122510921STim.Haley@Sun.COM 			(void) printf(dgettext(TEXT_DOMAIN,
122610921STim.Haley@Sun.COM 			    "Would be able to return %s "
122710921STim.Haley@Sun.COM 			    "to its state as of %s.\n"),
122810921STim.Haley@Sun.COM 			    name, timestr);
122910921STim.Haley@Sun.COM 		} else {
123010921STim.Haley@Sun.COM 			(void) printf(dgettext(TEXT_DOMAIN,
123110921STim.Haley@Sun.COM 			    "Pool %s returned to its state as of %s.\n"),
123210921STim.Haley@Sun.COM 			    name, timestr);
123310921STim.Haley@Sun.COM 		}
123410921STim.Haley@Sun.COM 		if (loss > 120) {
123510921STim.Haley@Sun.COM 			(void) printf(dgettext(TEXT_DOMAIN,
123610921STim.Haley@Sun.COM 			    "%s approximately %lld "),
123710921STim.Haley@Sun.COM 			    dryrun ? "Would discard" : "Discarded",
123810921STim.Haley@Sun.COM 			    (loss + 30) / 60);
123910921STim.Haley@Sun.COM 			(void) printf(dgettext(TEXT_DOMAIN,
124010921STim.Haley@Sun.COM 			    "minutes of transactions.\n"));
124110921STim.Haley@Sun.COM 		} else if (loss > 0) {
124210921STim.Haley@Sun.COM 			(void) printf(dgettext(TEXT_DOMAIN,
124310921STim.Haley@Sun.COM 			    "%s approximately %lld "),
124410921STim.Haley@Sun.COM 			    dryrun ? "Would discard" : "Discarded", loss);
124510921STim.Haley@Sun.COM 			(void) printf(dgettext(TEXT_DOMAIN,
124610921STim.Haley@Sun.COM 			    "seconds of transactions.\n"));
124710921STim.Haley@Sun.COM 		}
124810921STim.Haley@Sun.COM 	}
124910921STim.Haley@Sun.COM }
125010921STim.Haley@Sun.COM 
125110921STim.Haley@Sun.COM void
125210921STim.Haley@Sun.COM zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
125310921STim.Haley@Sun.COM     nvlist_t *config)
125410921STim.Haley@Sun.COM {
125512949SGeorge.Wilson@Sun.COM 	nvlist_t *nv = NULL;
125610921STim.Haley@Sun.COM 	int64_t loss = -1;
125710921STim.Haley@Sun.COM 	uint64_t edata = UINT64_MAX;
125810921STim.Haley@Sun.COM 	uint64_t rewindto;
125910921STim.Haley@Sun.COM 	struct tm t;
126010921STim.Haley@Sun.COM 	char timestr[128];
126110921STim.Haley@Sun.COM 
126210921STim.Haley@Sun.COM 	if (!hdl->libzfs_printerr)
126310921STim.Haley@Sun.COM 		return;
126410921STim.Haley@Sun.COM 
126510921STim.Haley@Sun.COM 	if (reason >= 0)
126610921STim.Haley@Sun.COM 		(void) printf(dgettext(TEXT_DOMAIN, "action: "));
126710921STim.Haley@Sun.COM 	else
126810921STim.Haley@Sun.COM 		(void) printf(dgettext(TEXT_DOMAIN, "\t"));
126910921STim.Haley@Sun.COM 
127010921STim.Haley@Sun.COM 	/* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
127112949SGeorge.Wilson@Sun.COM 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
127212949SGeorge.Wilson@Sun.COM 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
127310921STim.Haley@Sun.COM 		goto no_info;
127410921STim.Haley@Sun.COM 
127512949SGeorge.Wilson@Sun.COM 	(void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
127612949SGeorge.Wilson@Sun.COM 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
127710921STim.Haley@Sun.COM 	    &edata);
127810921STim.Haley@Sun.COM 
127910921STim.Haley@Sun.COM 	(void) printf(dgettext(TEXT_DOMAIN,
128010921STim.Haley@Sun.COM 	    "Recovery is possible, but will result in some data loss.\n"));
128110921STim.Haley@Sun.COM 
128210921STim.Haley@Sun.COM 	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
128310921STim.Haley@Sun.COM 	    strftime(timestr, 128, 0, &t) != 0) {
128410921STim.Haley@Sun.COM 		(void) printf(dgettext(TEXT_DOMAIN,
128510921STim.Haley@Sun.COM 		    "\tReturning the pool to its state as of %s\n"
128610921STim.Haley@Sun.COM 		    "\tshould correct the problem.  "),
128710921STim.Haley@Sun.COM 		    timestr);
128810921STim.Haley@Sun.COM 	} else {
128910921STim.Haley@Sun.COM 		(void) printf(dgettext(TEXT_DOMAIN,
129010921STim.Haley@Sun.COM 		    "\tReverting the pool to an earlier state "
129110921STim.Haley@Sun.COM 		    "should correct the problem.\n\t"));
129210921STim.Haley@Sun.COM 	}
129310921STim.Haley@Sun.COM 
129410921STim.Haley@Sun.COM 	if (loss > 120) {
129510921STim.Haley@Sun.COM 		(void) printf(dgettext(TEXT_DOMAIN,
129610921STim.Haley@Sun.COM 		    "Approximately %lld minutes of data\n"
129710921STim.Haley@Sun.COM 		    "\tmust be discarded, irreversibly.  "), (loss + 30) / 60);
129810921STim.Haley@Sun.COM 	} else if (loss > 0) {
129910921STim.Haley@Sun.COM 		(void) printf(dgettext(TEXT_DOMAIN,
130010921STim.Haley@Sun.COM 		    "Approximately %lld seconds of data\n"
130110921STim.Haley@Sun.COM 		    "\tmust be discarded, irreversibly.  "), loss);
130210921STim.Haley@Sun.COM 	}
130310921STim.Haley@Sun.COM 	if (edata != 0 && edata != UINT64_MAX) {
130410921STim.Haley@Sun.COM 		if (edata == 1) {
130510921STim.Haley@Sun.COM 			(void) printf(dgettext(TEXT_DOMAIN,
130610921STim.Haley@Sun.COM 			    "After rewind, at least\n"
130710921STim.Haley@Sun.COM 			    "\tone persistent user-data error will remain.  "));
130810921STim.Haley@Sun.COM 		} else {
130910921STim.Haley@Sun.COM 			(void) printf(dgettext(TEXT_DOMAIN,
131010921STim.Haley@Sun.COM 			    "After rewind, several\n"
131110921STim.Haley@Sun.COM 			    "\tpersistent user-data errors will remain.  "));
131210921STim.Haley@Sun.COM 		}
131310921STim.Haley@Sun.COM 	}
131410921STim.Haley@Sun.COM 	(void) printf(dgettext(TEXT_DOMAIN,
131511026STim.Haley@Sun.COM 	    "Recovery can be attempted\n\tby executing 'zpool %s -F %s'.  "),
131611026STim.Haley@Sun.COM 	    reason >= 0 ? "clear" : "import", name);
131710921STim.Haley@Sun.COM 
131810921STim.Haley@Sun.COM 	(void) printf(dgettext(TEXT_DOMAIN,
131910921STim.Haley@Sun.COM 	    "A scrub of the pool\n"
132010921STim.Haley@Sun.COM 	    "\tis strongly recommended after recovery.\n"));
132110921STim.Haley@Sun.COM 	return;
132210921STim.Haley@Sun.COM 
132310921STim.Haley@Sun.COM no_info:
132410921STim.Haley@Sun.COM 	(void) printf(dgettext(TEXT_DOMAIN,
132510921STim.Haley@Sun.COM 	    "Destroy and re-create the pool from\n\ta backup source.\n"));
132610921STim.Haley@Sun.COM }
132710921STim.Haley@Sun.COM 
1328789Sahrens /*
13295094Slling  * zpool_import() is a contracted interface. Should be kept the same
13305094Slling  * if possible.
13315094Slling  *
13325094Slling  * Applications should use zpool_import_props() to import a pool with
13335094Slling  * new properties value to be set.
1334789Sahrens  */
1335789Sahrens int
13362082Seschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
13375094Slling     char *altroot)
13385094Slling {
13395094Slling 	nvlist_t *props = NULL;
13405094Slling 	int ret;
13415094Slling 
13425094Slling 	if (altroot != NULL) {
13435094Slling 		if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
13445094Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
13455094Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
13465094Slling 			    newname));
13475094Slling 		}
13485094Slling 
13495094Slling 		if (nvlist_add_string(props,
13508084SGeorge.Wilson@Sun.COM 		    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
13518084SGeorge.Wilson@Sun.COM 		    nvlist_add_string(props,
13528084SGeorge.Wilson@Sun.COM 		    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
13535094Slling 			nvlist_free(props);
13545094Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
13555094Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
13565094Slling 			    newname));
13575094Slling 		}
13585094Slling 	}
13595094Slling 
136012949SGeorge.Wilson@Sun.COM 	ret = zpool_import_props(hdl, config, newname, props,
136112949SGeorge.Wilson@Sun.COM 	    ZFS_IMPORT_NORMAL);
13625094Slling 	if (props)
13635094Slling 		nvlist_free(props);
13645094Slling 	return (ret);
13655094Slling }
13665094Slling 
136712949SGeorge.Wilson@Sun.COM static void
136812949SGeorge.Wilson@Sun.COM print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
136912949SGeorge.Wilson@Sun.COM     int indent)
137012949SGeorge.Wilson@Sun.COM {
137112949SGeorge.Wilson@Sun.COM 	nvlist_t **child;
137212949SGeorge.Wilson@Sun.COM 	uint_t c, children;
137312949SGeorge.Wilson@Sun.COM 	char *vname;
137412949SGeorge.Wilson@Sun.COM 	uint64_t is_log = 0;
137512949SGeorge.Wilson@Sun.COM 
137612949SGeorge.Wilson@Sun.COM 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
137712949SGeorge.Wilson@Sun.COM 	    &is_log);
137812949SGeorge.Wilson@Sun.COM 
137912949SGeorge.Wilson@Sun.COM 	if (name != NULL)
138012949SGeorge.Wilson@Sun.COM 		(void) printf("\t%*s%s%s\n", indent, "", name,
138112949SGeorge.Wilson@Sun.COM 		    is_log ? " [log]" : "");
138212949SGeorge.Wilson@Sun.COM 
138312949SGeorge.Wilson@Sun.COM 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
138412949SGeorge.Wilson@Sun.COM 	    &child, &children) != 0)
138512949SGeorge.Wilson@Sun.COM 		return;
138612949SGeorge.Wilson@Sun.COM 
138712949SGeorge.Wilson@Sun.COM 	for (c = 0; c < children; c++) {
138812949SGeorge.Wilson@Sun.COM 		vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
138912949SGeorge.Wilson@Sun.COM 		print_vdev_tree(hdl, vname, child[c], indent + 2);
139012949SGeorge.Wilson@Sun.COM 		free(vname);
139112949SGeorge.Wilson@Sun.COM 	}
139212949SGeorge.Wilson@Sun.COM }
139312949SGeorge.Wilson@Sun.COM 
13945094Slling /*
13955094Slling  * Import the given pool using the known configuration and a list of
13965094Slling  * properties to be set. The configuration should have come from
13975094Slling  * zpool_find_import(). The 'newname' parameters control whether the pool
13985094Slling  * is imported with a different name.
13995094Slling  */
14005094Slling int
14015094Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
140212949SGeorge.Wilson@Sun.COM     nvlist_t *props, int flags)
1403789Sahrens {
14042676Seschrock 	zfs_cmd_t zc = { 0 };
140510921STim.Haley@Sun.COM 	zpool_rewind_policy_t policy;
140612949SGeorge.Wilson@Sun.COM 	nvlist_t *nv = NULL;
140712949SGeorge.Wilson@Sun.COM 	nvlist_t *nvinfo = NULL;
140812949SGeorge.Wilson@Sun.COM 	nvlist_t *missing = NULL;
1409789Sahrens 	char *thename;
1410789Sahrens 	char *origname;
1411789Sahrens 	int ret;
141212949SGeorge.Wilson@Sun.COM 	int error = 0;
14135094Slling 	char errbuf[1024];
1414789Sahrens 
1415789Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1416789Sahrens 	    &origname) == 0);
1417789Sahrens 
14185094Slling 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
14195094Slling 	    "cannot import pool '%s'"), origname);
14205094Slling 
1421789Sahrens 	if (newname != NULL) {
14222082Seschrock 		if (!zpool_name_valid(hdl, B_FALSE, newname))
14233237Slling 			return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
14242082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
14252082Seschrock 			    newname));
1426789Sahrens 		thename = (char *)newname;
1427789Sahrens 	} else {
1428789Sahrens 		thename = origname;
1429789Sahrens 	}
1430789Sahrens 
14315094Slling 	if (props) {
14325094Slling 		uint64_t version;
14335094Slling 
14345094Slling 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
14355094Slling 		    &version) == 0);
14365094Slling 
14377184Stimh 		if ((props = zpool_valid_proplist(hdl, origname,
14385320Slling 		    props, version, B_TRUE, errbuf)) == NULL) {
14395094Slling 			return (-1);
14405320Slling 		} else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
14415320Slling 			nvlist_free(props);
14425094Slling 			return (-1);
14435320Slling 		}
14445094Slling 	}
1445789Sahrens 
1446789Sahrens 	(void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1447789Sahrens 
1448789Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
14491544Seschrock 	    &zc.zc_guid) == 0);
1450789Sahrens 
14515320Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
14525320Slling 		nvlist_free(props);
14532082Seschrock 		return (-1);
14545320Slling 	}
145512974SGeorge.Wilson@Sun.COM 	if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
145610921STim.Haley@Sun.COM 		nvlist_free(props);
145710921STim.Haley@Sun.COM 		return (-1);
145810921STim.Haley@Sun.COM 	}
1459789Sahrens 
146012949SGeorge.Wilson@Sun.COM 	zc.zc_cookie = flags;
146112949SGeorge.Wilson@Sun.COM 	while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
146212949SGeorge.Wilson@Sun.COM 	    errno == ENOMEM) {
146312949SGeorge.Wilson@Sun.COM 		if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
146412949SGeorge.Wilson@Sun.COM 			zcmd_free_nvlists(&zc);
146512949SGeorge.Wilson@Sun.COM 			return (-1);
146612949SGeorge.Wilson@Sun.COM 		}
146712949SGeorge.Wilson@Sun.COM 	}
146812949SGeorge.Wilson@Sun.COM 	if (ret != 0)
146912949SGeorge.Wilson@Sun.COM 		error = errno;
147012949SGeorge.Wilson@Sun.COM 
147112949SGeorge.Wilson@Sun.COM 	(void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
147212949SGeorge.Wilson@Sun.COM 	zpool_get_rewind_policy(config, &policy);
147312949SGeorge.Wilson@Sun.COM 
147412949SGeorge.Wilson@Sun.COM 	if (error) {
1475789Sahrens 		char desc[1024];
147610921STim.Haley@Sun.COM 
147710921STim.Haley@Sun.COM 		/*
147810921STim.Haley@Sun.COM 		 * Dry-run failed, but we print out what success
147910921STim.Haley@Sun.COM 		 * looks like if we found a best txg
148010921STim.Haley@Sun.COM 		 */
148112949SGeorge.Wilson@Sun.COM 		if (policy.zrp_request & ZPOOL_TRY_REWIND) {
148210921STim.Haley@Sun.COM 			zpool_rewind_exclaim(hdl, newname ? origname : thename,
148312949SGeorge.Wilson@Sun.COM 			    B_TRUE, nv);
148412949SGeorge.Wilson@Sun.COM 			nvlist_free(nv);
148510921STim.Haley@Sun.COM 			return (-1);
148610921STim.Haley@Sun.COM 		}
148710921STim.Haley@Sun.COM 
1488789Sahrens 		if (newname == NULL)
1489789Sahrens 			(void) snprintf(desc, sizeof (desc),
1490789Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1491789Sahrens 			    thename);
1492789Sahrens 		else
1493789Sahrens 			(void) snprintf(desc, sizeof (desc),
1494789Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1495789Sahrens 			    origname, thename);
1496789Sahrens 
149712949SGeorge.Wilson@Sun.COM 		switch (error) {
14981544Seschrock 		case ENOTSUP:
14991544Seschrock 			/*
15001544Seschrock 			 * Unsupported version.
15011544Seschrock 			 */
15022082Seschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, desc);
15031544Seschrock 			break;
15041544Seschrock 
15052174Seschrock 		case EINVAL:
15062174Seschrock 			(void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
15072174Seschrock 			break;
15082174Seschrock 
150911814SChris.Kirby@sun.com 		case EROFS:
151011814SChris.Kirby@sun.com 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
151111814SChris.Kirby@sun.com 			    "one or more devices is read only"));
151211814SChris.Kirby@sun.com 			(void) zfs_error(hdl, EZFS_BADDEV, desc);
151311814SChris.Kirby@sun.com 			break;
151411814SChris.Kirby@sun.com 
151512949SGeorge.Wilson@Sun.COM 		case ENXIO:
151612949SGeorge.Wilson@Sun.COM 			if (nv && nvlist_lookup_nvlist(nv,
151712949SGeorge.Wilson@Sun.COM 			    ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
151812949SGeorge.Wilson@Sun.COM 			    nvlist_lookup_nvlist(nvinfo,
151912949SGeorge.Wilson@Sun.COM 			    ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
152012949SGeorge.Wilson@Sun.COM 				(void) printf(dgettext(TEXT_DOMAIN,
152112949SGeorge.Wilson@Sun.COM 				    "The devices below are missing, use "
152212949SGeorge.Wilson@Sun.COM 				    "'-m' to import the pool anyway:\n"));
152312949SGeorge.Wilson@Sun.COM 				print_vdev_tree(hdl, NULL, missing, 2);
152412949SGeorge.Wilson@Sun.COM 				(void) printf("\n");
152512949SGeorge.Wilson@Sun.COM 			}
152612949SGeorge.Wilson@Sun.COM 			(void) zpool_standard_error(hdl, error, desc);
152712949SGeorge.Wilson@Sun.COM 			break;
152812949SGeorge.Wilson@Sun.COM 
152912949SGeorge.Wilson@Sun.COM 		case EEXIST:
153012949SGeorge.Wilson@Sun.COM 			(void) zpool_standard_error(hdl, error, desc);
153112949SGeorge.Wilson@Sun.COM 			break;
153212949SGeorge.Wilson@Sun.COM 
1533789Sahrens 		default:
153412949SGeorge.Wilson@Sun.COM 			(void) zpool_standard_error(hdl, error, desc);
153510921STim.Haley@Sun.COM 			zpool_explain_recover(hdl,
153612949SGeorge.Wilson@Sun.COM 			    newname ? origname : thename, -error, nv);
153710921STim.Haley@Sun.COM 			break;
1538789Sahrens 		}
1539789Sahrens 
154012949SGeorge.Wilson@Sun.COM 		nvlist_free(nv);
1541789Sahrens 		ret = -1;
1542789Sahrens 	} else {
1543789Sahrens 		zpool_handle_t *zhp;
15444543Smarks 
1545789Sahrens 		/*
1546789Sahrens 		 * This should never fail, but play it safe anyway.
1547789Sahrens 		 */
154810588SEric.Taylor@Sun.COM 		if (zpool_open_silent(hdl, thename, &zhp) != 0)
15492142Seschrock 			ret = -1;
155010588SEric.Taylor@Sun.COM 		else if (zhp != NULL)
1551789Sahrens 			zpool_close(zhp);
155210921STim.Haley@Sun.COM 		if (policy.zrp_request &
155310921STim.Haley@Sun.COM 		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
155410921STim.Haley@Sun.COM 			zpool_rewind_exclaim(hdl, newname ? origname : thename,
155512949SGeorge.Wilson@Sun.COM 			    ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
155610921STim.Haley@Sun.COM 		}
155712949SGeorge.Wilson@Sun.COM 		nvlist_free(nv);
155810921STim.Haley@Sun.COM 		return (0);
1559789Sahrens 	}
1560789Sahrens 
15612676Seschrock 	zcmd_free_nvlists(&zc);
15625320Slling 	nvlist_free(props);
15635320Slling 
1564789Sahrens 	return (ret);
1565789Sahrens }
1566789Sahrens 
1567789Sahrens /*
156812296SLin.Ling@Sun.COM  * Scan the pool.
1569789Sahrens  */
1570789Sahrens int
157112296SLin.Ling@Sun.COM zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func)
1572789Sahrens {
1573789Sahrens 	zfs_cmd_t zc = { 0 };
1574789Sahrens 	char msg[1024];
15752082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1576789Sahrens 
1577789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
157812296SLin.Ling@Sun.COM 	zc.zc_cookie = func;
157912296SLin.Ling@Sun.COM 
1580*13037SMark.Musante@Sun.COM 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 ||
158112296SLin.Ling@Sun.COM 	    (errno == ENOENT && func != POOL_SCAN_NONE))
1582789Sahrens 		return (0);
1583789Sahrens 
158412296SLin.Ling@Sun.COM 	if (func == POOL_SCAN_SCRUB) {
158512296SLin.Ling@Sun.COM 		(void) snprintf(msg, sizeof (msg),
158612296SLin.Ling@Sun.COM 		    dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
158712296SLin.Ling@Sun.COM 	} else if (func == POOL_SCAN_NONE) {
158812296SLin.Ling@Sun.COM 		(void) snprintf(msg, sizeof (msg),
158912296SLin.Ling@Sun.COM 		    dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
159012296SLin.Ling@Sun.COM 		    zc.zc_name);
159112296SLin.Ling@Sun.COM 	} else {
159212296SLin.Ling@Sun.COM 		assert(!"unexpected result");
159312296SLin.Ling@Sun.COM 	}
159412296SLin.Ling@Sun.COM 
159512296SLin.Ling@Sun.COM 	if (errno == EBUSY) {
159612296SLin.Ling@Sun.COM 		nvlist_t *nvroot;
159712296SLin.Ling@Sun.COM 		pool_scan_stat_t *ps = NULL;
159812296SLin.Ling@Sun.COM 		uint_t psc;
159912296SLin.Ling@Sun.COM 
160012296SLin.Ling@Sun.COM 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
160112296SLin.Ling@Sun.COM 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
160212296SLin.Ling@Sun.COM 		(void) nvlist_lookup_uint64_array(nvroot,
160312296SLin.Ling@Sun.COM 		    ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
160412296SLin.Ling@Sun.COM 		if (ps && ps->pss_func == POOL_SCAN_SCRUB)
160512296SLin.Ling@Sun.COM 			return (zfs_error(hdl, EZFS_SCRUBBING, msg));
160612296SLin.Ling@Sun.COM 		else
160712296SLin.Ling@Sun.COM 			return (zfs_error(hdl, EZFS_RESILVERING, msg));
160812296SLin.Ling@Sun.COM 	} else if (errno == ENOENT) {
160912296SLin.Ling@Sun.COM 		return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
161012296SLin.Ling@Sun.COM 	} else {
16112082Seschrock 		return (zpool_standard_error(hdl, errno, msg));
161212296SLin.Ling@Sun.COM 	}
1613789Sahrens }
1614789Sahrens 
16152468Sek110237 /*
161612383SJohn.Harres@Sun.COM  * This provides a very minimal check whether a given string is likely a
161712383SJohn.Harres@Sun.COM  * c#t#d# style string.  Users of this are expected to do their own
161812383SJohn.Harres@Sun.COM  * verification of the s# part.
161912383SJohn.Harres@Sun.COM  */
162012383SJohn.Harres@Sun.COM #define	CTD_CHECK(str)  (str && str[0] == 'c' && isdigit(str[1]))
162112383SJohn.Harres@Sun.COM 
162212383SJohn.Harres@Sun.COM /*
162312383SJohn.Harres@Sun.COM  * More elaborate version for ones which may start with "/dev/dsk/"
162412383SJohn.Harres@Sun.COM  * and the like.
162512383SJohn.Harres@Sun.COM  */
162612383SJohn.Harres@Sun.COM static int
162712383SJohn.Harres@Sun.COM ctd_check_path(char *str) {
162812383SJohn.Harres@Sun.COM 	/*
162912383SJohn.Harres@Sun.COM 	 * If it starts with a slash, check the last component.
163012383SJohn.Harres@Sun.COM 	 */
163112383SJohn.Harres@Sun.COM 	if (str && str[0] == '/') {
163212383SJohn.Harres@Sun.COM 		char *tmp = strrchr(str, '/');
163312383SJohn.Harres@Sun.COM 
163412383SJohn.Harres@Sun.COM 		/*
163512383SJohn.Harres@Sun.COM 		 * If it ends in "/old", check the second-to-last
163612383SJohn.Harres@Sun.COM 		 * component of the string instead.
163712383SJohn.Harres@Sun.COM 		 */
163812383SJohn.Harres@Sun.COM 		if (tmp != str && strcmp(tmp, "/old") == 0) {
163912383SJohn.Harres@Sun.COM 			for (tmp--; *tmp != '/'; tmp--)
164012383SJohn.Harres@Sun.COM 				;
164112383SJohn.Harres@Sun.COM 		}
164212383SJohn.Harres@Sun.COM 		str = tmp + 1;
164312383SJohn.Harres@Sun.COM 	}
164412383SJohn.Harres@Sun.COM 	return (CTD_CHECK(str));
164512383SJohn.Harres@Sun.COM }
164612383SJohn.Harres@Sun.COM 
164712383SJohn.Harres@Sun.COM /*
16489816SGeorge.Wilson@Sun.COM  * Find a vdev that matches the search criteria specified. We use the
16499816SGeorge.Wilson@Sun.COM  * the nvpair name to determine how we should look for the device.
16502468Sek110237  * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
16512468Sek110237  * spare; but FALSE if its an INUSE spare.
16522468Sek110237  */
16532082Seschrock static nvlist_t *
16549816SGeorge.Wilson@Sun.COM vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
16559816SGeorge.Wilson@Sun.COM     boolean_t *l2cache, boolean_t *log)
16561544Seschrock {
16571544Seschrock 	uint_t c, children;
16581544Seschrock 	nvlist_t **child;
16592082Seschrock 	nvlist_t *ret;
16607326SEric.Schrock@Sun.COM 	uint64_t is_log;
16619816SGeorge.Wilson@Sun.COM 	char *srchkey;
16629816SGeorge.Wilson@Sun.COM 	nvpair_t *pair = nvlist_next_nvpair(search, NULL);
16639816SGeorge.Wilson@Sun.COM 
16649816SGeorge.Wilson@Sun.COM 	/* Nothing to look for */
16659816SGeorge.Wilson@Sun.COM 	if (search == NULL || pair == NULL)
16669816SGeorge.Wilson@Sun.COM 		return (NULL);
16679816SGeorge.Wilson@Sun.COM 
16689816SGeorge.Wilson@Sun.COM 	/* Obtain the key we will use to search */
16699816SGeorge.Wilson@Sun.COM 	srchkey = nvpair_name(pair);
16709816SGeorge.Wilson@Sun.COM 
16719816SGeorge.Wilson@Sun.COM 	switch (nvpair_type(pair)) {
1672*13037SMark.Musante@Sun.COM 	case DATA_TYPE_UINT64:
16739816SGeorge.Wilson@Sun.COM 		if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1674*13037SMark.Musante@Sun.COM 			uint64_t srchval, theguid;
1675*13037SMark.Musante@Sun.COM 
1676*13037SMark.Musante@Sun.COM 			verify(nvpair_value_uint64(pair, &srchval) == 0);
1677*13037SMark.Musante@Sun.COM 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1678*13037SMark.Musante@Sun.COM 			    &theguid) == 0);
1679*13037SMark.Musante@Sun.COM 			if (theguid == srchval)
1680*13037SMark.Musante@Sun.COM 				return (nv);
16819816SGeorge.Wilson@Sun.COM 		}
16829816SGeorge.Wilson@Sun.COM 		break;
16839816SGeorge.Wilson@Sun.COM 
16849816SGeorge.Wilson@Sun.COM 	case DATA_TYPE_STRING: {
16859816SGeorge.Wilson@Sun.COM 		char *srchval, *val;
16869816SGeorge.Wilson@Sun.COM 
16879816SGeorge.Wilson@Sun.COM 		verify(nvpair_value_string(pair, &srchval) == 0);
16889816SGeorge.Wilson@Sun.COM 		if (nvlist_lookup_string(nv, srchkey, &val) != 0)
16899816SGeorge.Wilson@Sun.COM 			break;
16909816SGeorge.Wilson@Sun.COM 
16911544Seschrock 		/*
169212383SJohn.Harres@Sun.COM 		 * Search for the requested value. Special cases:
169312383SJohn.Harres@Sun.COM 		 *
169412383SJohn.Harres@Sun.COM 		 * - ZPOOL_CONFIG_PATH for whole disk entries.  These end in
169512383SJohn.Harres@Sun.COM 		 *   "s0" or "s0/old".  The "s0" part is hidden from the user,
169612383SJohn.Harres@Sun.COM 		 *   but included in the string, so this matches around it.
169712383SJohn.Harres@Sun.COM 		 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
169812383SJohn.Harres@Sun.COM 		 *
169910594SGeorge.Wilson@Sun.COM 		 * Otherwise, all other searches are simple string compares.
17001544Seschrock 		 */
170112383SJohn.Harres@Sun.COM 		if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
170212383SJohn.Harres@Sun.COM 		    ctd_check_path(val)) {
17039816SGeorge.Wilson@Sun.COM 			uint64_t wholedisk = 0;
17049816SGeorge.Wilson@Sun.COM 
17059816SGeorge.Wilson@Sun.COM 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
17069816SGeorge.Wilson@Sun.COM 			    &wholedisk);
17079816SGeorge.Wilson@Sun.COM 			if (wholedisk) {
170812383SJohn.Harres@Sun.COM 				int slen = strlen(srchval);
170912383SJohn.Harres@Sun.COM 				int vlen = strlen(val);
171012383SJohn.Harres@Sun.COM 
171112383SJohn.Harres@Sun.COM 				if (slen != vlen - 2)
171212383SJohn.Harres@Sun.COM 					break;
171312383SJohn.Harres@Sun.COM 
17149816SGeorge.Wilson@Sun.COM 				/*
171512383SJohn.Harres@Sun.COM 				 * make_leaf_vdev() should only set
171612383SJohn.Harres@Sun.COM 				 * wholedisk for ZPOOL_CONFIG_PATHs which
171712383SJohn.Harres@Sun.COM 				 * will include "/dev/dsk/", giving plenty of
171812383SJohn.Harres@Sun.COM 				 * room for the indices used next.
17199816SGeorge.Wilson@Sun.COM 				 */
172012383SJohn.Harres@Sun.COM 				ASSERT(vlen >= 6);
172112383SJohn.Harres@Sun.COM 
172212383SJohn.Harres@Sun.COM 				/*
172312383SJohn.Harres@Sun.COM 				 * strings identical except trailing "s0"
172412383SJohn.Harres@Sun.COM 				 */
172512383SJohn.Harres@Sun.COM 				if (strcmp(&val[vlen - 2], "s0") == 0 &&
172612383SJohn.Harres@Sun.COM 				    strncmp(srchval, val, slen) == 0)
17279816SGeorge.Wilson@Sun.COM 					return (nv);
172812383SJohn.Harres@Sun.COM 
172912383SJohn.Harres@Sun.COM 				/*
173012383SJohn.Harres@Sun.COM 				 * strings identical except trailing "s0/old"
173112383SJohn.Harres@Sun.COM 				 */
173212383SJohn.Harres@Sun.COM 				if (strcmp(&val[vlen - 6], "s0/old") == 0 &&
173312383SJohn.Harres@Sun.COM 				    strcmp(&srchval[slen - 4], "/old") == 0 &&
173412383SJohn.Harres@Sun.COM 				    strncmp(srchval, val, slen - 4) == 0)
173512383SJohn.Harres@Sun.COM 					return (nv);
173612383SJohn.Harres@Sun.COM 
17379816SGeorge.Wilson@Sun.COM 				break;
17389816SGeorge.Wilson@Sun.COM 			}
173910594SGeorge.Wilson@Sun.COM 		} else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
174010594SGeorge.Wilson@Sun.COM 			char *type, *idx, *end, *p;
174110594SGeorge.Wilson@Sun.COM 			uint64_t id, vdev_id;
174210594SGeorge.Wilson@Sun.COM 
174310594SGeorge.Wilson@Sun.COM 			/*
174410594SGeorge.Wilson@Sun.COM 			 * Determine our vdev type, keeping in mind
174510594SGeorge.Wilson@Sun.COM 			 * that the srchval is composed of a type and
174610594SGeorge.Wilson@Sun.COM 			 * vdev id pair (i.e. mirror-4).
174710594SGeorge.Wilson@Sun.COM 			 */
174810594SGeorge.Wilson@Sun.COM 			if ((type = strdup(srchval)) == NULL)
174910594SGeorge.Wilson@Sun.COM 				return (NULL);
175010594SGeorge.Wilson@Sun.COM 
175110594SGeorge.Wilson@Sun.COM 			if ((p = strrchr(type, '-')) == NULL) {
175210594SGeorge.Wilson@Sun.COM 				free(type);
175310594SGeorge.Wilson@Sun.COM 				break;
175410594SGeorge.Wilson@Sun.COM 			}
175510594SGeorge.Wilson@Sun.COM 			idx = p + 1;
175610594SGeorge.Wilson@Sun.COM 			*p = '\0';
175710594SGeorge.Wilson@Sun.COM 
175810594SGeorge.Wilson@Sun.COM 			/*
175910594SGeorge.Wilson@Sun.COM 			 * If the types don't match then keep looking.
176010594SGeorge.Wilson@Sun.COM 			 */
176110594SGeorge.Wilson@Sun.COM 			if (strncmp(val, type, strlen(val)) != 0) {
176210594SGeorge.Wilson@Sun.COM 				free(type);
176310594SGeorge.Wilson@Sun.COM 				break;
176410594SGeorge.Wilson@Sun.COM 			}
176510594SGeorge.Wilson@Sun.COM 
176610594SGeorge.Wilson@Sun.COM 			verify(strncmp(type, VDEV_TYPE_RAIDZ,
176710594SGeorge.Wilson@Sun.COM 			    strlen(VDEV_TYPE_RAIDZ)) == 0 ||
176810594SGeorge.Wilson@Sun.COM 			    strncmp(type, VDEV_TYPE_MIRROR,
176910594SGeorge.Wilson@Sun.COM 			    strlen(VDEV_TYPE_MIRROR)) == 0);
177010594SGeorge.Wilson@Sun.COM 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
177110594SGeorge.Wilson@Sun.COM 			    &id) == 0);
177210594SGeorge.Wilson@Sun.COM 
177310594SGeorge.Wilson@Sun.COM 			errno = 0;
177410594SGeorge.Wilson@Sun.COM 			vdev_id = strtoull(idx, &end, 10);
177510594SGeorge.Wilson@Sun.COM 
177610594SGeorge.Wilson@Sun.COM 			free(type);
177710594SGeorge.Wilson@Sun.COM 			if (errno != 0)
177810594SGeorge.Wilson@Sun.COM 				return (NULL);
177910594SGeorge.Wilson@Sun.COM 
178010594SGeorge.Wilson@Sun.COM 			/*
178110594SGeorge.Wilson@Sun.COM 			 * Now verify that we have the correct vdev id.
178210594SGeorge.Wilson@Sun.COM 			 */
178310594SGeorge.Wilson@Sun.COM 			if (vdev_id == id)
178410594SGeorge.Wilson@Sun.COM 				return (nv);
17859816SGeorge.Wilson@Sun.COM 		}
17869816SGeorge.Wilson@Sun.COM 
17879816SGeorge.Wilson@Sun.COM 		/*
17889816SGeorge.Wilson@Sun.COM 		 * Common case
17899816SGeorge.Wilson@Sun.COM 		 */
17909816SGeorge.Wilson@Sun.COM 		if (strcmp(srchval, val) == 0)
17912082Seschrock 			return (nv);
17929816SGeorge.Wilson@Sun.COM 		break;
17939816SGeorge.Wilson@Sun.COM 	}
17949816SGeorge.Wilson@Sun.COM 
17959816SGeorge.Wilson@Sun.COM 	default:
17969816SGeorge.Wilson@Sun.COM 		break;
17971544Seschrock 	}
17981544Seschrock 
17991544Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
18001544Seschrock 	    &child, &children) != 0)
18012082Seschrock 		return (NULL);
18021544Seschrock 
18037326SEric.Schrock@Sun.COM 	for (c = 0; c < children; c++) {
18049816SGeorge.Wilson@Sun.COM 		if ((ret = vdev_to_nvlist_iter(child[c], search,
18057326SEric.Schrock@Sun.COM 		    avail_spare, l2cache, NULL)) != NULL) {
18067326SEric.Schrock@Sun.COM 			/*
18077326SEric.Schrock@Sun.COM 			 * The 'is_log' value is only set for the toplevel
18087326SEric.Schrock@Sun.COM 			 * vdev, not the leaf vdevs.  So we always lookup the
18097326SEric.Schrock@Sun.COM 			 * log device from the root of the vdev tree (where
18107326SEric.Schrock@Sun.COM 			 * 'log' is non-NULL).
18117326SEric.Schrock@Sun.COM 			 */
18127326SEric.Schrock@Sun.COM 			if (log != NULL &&
18137326SEric.Schrock@Sun.COM 			    nvlist_lookup_uint64(child[c],
18147326SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
18157326SEric.Schrock@Sun.COM 			    is_log) {
18167326SEric.Schrock@Sun.COM 				*log = B_TRUE;
18177326SEric.Schrock@Sun.COM 			}
18181544Seschrock 			return (ret);
18197326SEric.Schrock@Sun.COM 		}
18207326SEric.Schrock@Sun.COM 	}
18211544Seschrock 
18222082Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
18232082Seschrock 	    &child, &children) == 0) {
18242082Seschrock 		for (c = 0; c < children; c++) {
18259816SGeorge.Wilson@Sun.COM 			if ((ret = vdev_to_nvlist_iter(child[c], search,
18267326SEric.Schrock@Sun.COM 			    avail_spare, l2cache, NULL)) != NULL) {
18272468Sek110237 				*avail_spare = B_TRUE;
18282082Seschrock 				return (ret);
18292082Seschrock 			}
18302082Seschrock 		}
18312082Seschrock 	}
18322082Seschrock 
18335450Sbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
18345450Sbrendan 	    &child, &children) == 0) {
18355450Sbrendan 		for (c = 0; c < children; c++) {
18369816SGeorge.Wilson@Sun.COM 			if ((ret = vdev_to_nvlist_iter(child[c], search,
18377326SEric.Schrock@Sun.COM 			    avail_spare, l2cache, NULL)) != NULL) {
18385450Sbrendan 				*l2cache = B_TRUE;
18395450Sbrendan 				return (ret);
18405450Sbrendan 			}
18415450Sbrendan 		}
18425450Sbrendan 	}
18435450Sbrendan 
18442082Seschrock 	return (NULL);
18451544Seschrock }
18461544Seschrock 
18479816SGeorge.Wilson@Sun.COM /*
18489816SGeorge.Wilson@Sun.COM  * Given a physical path (minus the "/devices" prefix), find the
18499816SGeorge.Wilson@Sun.COM  * associated vdev.
18509816SGeorge.Wilson@Sun.COM  */
18519816SGeorge.Wilson@Sun.COM nvlist_t *
18529816SGeorge.Wilson@Sun.COM zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
18539816SGeorge.Wilson@Sun.COM     boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
18549816SGeorge.Wilson@Sun.COM {
18559816SGeorge.Wilson@Sun.COM 	nvlist_t *search, *nvroot, *ret;
18569816SGeorge.Wilson@Sun.COM 
18579816SGeorge.Wilson@Sun.COM 	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
18589816SGeorge.Wilson@Sun.COM 	verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
18599816SGeorge.Wilson@Sun.COM 
18609816SGeorge.Wilson@Sun.COM 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
18619816SGeorge.Wilson@Sun.COM 	    &nvroot) == 0);
18629816SGeorge.Wilson@Sun.COM 
18639816SGeorge.Wilson@Sun.COM 	*avail_spare = B_FALSE;
1864*13037SMark.Musante@Sun.COM 	*l2cache = B_FALSE;
1865*13037SMark.Musante@Sun.COM 	*log = B_FALSE;
18669816SGeorge.Wilson@Sun.COM 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
18679816SGeorge.Wilson@Sun.COM 	nvlist_free(search);
18689816SGeorge.Wilson@Sun.COM 
18699816SGeorge.Wilson@Sun.COM 	return (ret);
18709816SGeorge.Wilson@Sun.COM }
18719816SGeorge.Wilson@Sun.COM 
187210594SGeorge.Wilson@Sun.COM /*
187310594SGeorge.Wilson@Sun.COM  * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
187410594SGeorge.Wilson@Sun.COM  */
187510594SGeorge.Wilson@Sun.COM boolean_t
187610594SGeorge.Wilson@Sun.COM zpool_vdev_is_interior(const char *name)
187710594SGeorge.Wilson@Sun.COM {
187810594SGeorge.Wilson@Sun.COM 	if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
187910594SGeorge.Wilson@Sun.COM 	    strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
188010594SGeorge.Wilson@Sun.COM 		return (B_TRUE);
188110594SGeorge.Wilson@Sun.COM 	return (B_FALSE);
188210594SGeorge.Wilson@Sun.COM }
188310594SGeorge.Wilson@Sun.COM 
18842082Seschrock nvlist_t *
18855450Sbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
18867326SEric.Schrock@Sun.COM     boolean_t *l2cache, boolean_t *log)
18871544Seschrock {
18881544Seschrock 	char buf[MAXPATHLEN];
18891544Seschrock 	char *end;
18909816SGeorge.Wilson@Sun.COM 	nvlist_t *nvroot, *search, *ret;
18911544Seschrock 	uint64_t guid;
18921544Seschrock 
18939816SGeorge.Wilson@Sun.COM 	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
18949816SGeorge.Wilson@Sun.COM 
18951613Seschrock 	guid = strtoull(path, &end, 10);
18961544Seschrock 	if (guid != 0 && *end == '\0') {
18979816SGeorge.Wilson@Sun.COM 		verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
189810594SGeorge.Wilson@Sun.COM 	} else if (zpool_vdev_is_interior(path)) {
189910594SGeorge.Wilson@Sun.COM 		verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
19001544Seschrock 	} else if (path[0] != '/') {
19011544Seschrock 		(void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path);
19029816SGeorge.Wilson@Sun.COM 		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
19031544Seschrock 	} else {
19049816SGeorge.Wilson@Sun.COM 		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
19051544Seschrock 	}
19061544Seschrock 
19071544Seschrock 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
19081544Seschrock 	    &nvroot) == 0);
19091544Seschrock 
19102468Sek110237 	*avail_spare = B_FALSE;
19115450Sbrendan 	*l2cache = B_FALSE;
19127326SEric.Schrock@Sun.COM 	if (log != NULL)
19137326SEric.Schrock@Sun.COM 		*log = B_FALSE;
19149816SGeorge.Wilson@Sun.COM 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
19159816SGeorge.Wilson@Sun.COM 	nvlist_free(search);
19169816SGeorge.Wilson@Sun.COM 
19179816SGeorge.Wilson@Sun.COM 	return (ret);
19182468Sek110237 }
19192468Sek110237 
19207656SSherry.Moore@Sun.COM static int
19217656SSherry.Moore@Sun.COM vdev_online(nvlist_t *nv)
19227656SSherry.Moore@Sun.COM {
19237656SSherry.Moore@Sun.COM 	uint64_t ival;
19247656SSherry.Moore@Sun.COM 
19257656SSherry.Moore@Sun.COM 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
19267656SSherry.Moore@Sun.COM 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
19277656SSherry.Moore@Sun.COM 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
19287656SSherry.Moore@Sun.COM 		return (0);
19297656SSherry.Moore@Sun.COM 
19307656SSherry.Moore@Sun.COM 	return (1);
19317656SSherry.Moore@Sun.COM }
19327656SSherry.Moore@Sun.COM 
19337656SSherry.Moore@Sun.COM /*
19349790SLin.Ling@Sun.COM  * Helper function for zpool_get_physpaths().
19357656SSherry.Moore@Sun.COM  */
19369160SSherry.Moore@Sun.COM static int
19379790SLin.Ling@Sun.COM vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
19389160SSherry.Moore@Sun.COM     size_t *bytes_written)
19397656SSherry.Moore@Sun.COM {
19409160SSherry.Moore@Sun.COM 	size_t bytes_left, pos, rsz;
19419160SSherry.Moore@Sun.COM 	char *tmppath;
19429160SSherry.Moore@Sun.COM 	const char *format;
19439160SSherry.Moore@Sun.COM 
19449160SSherry.Moore@Sun.COM 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
19459160SSherry.Moore@Sun.COM 	    &tmppath) != 0)
19469160SSherry.Moore@Sun.COM 		return (EZFS_NODEVICE);
19479160SSherry.Moore@Sun.COM 
19489160SSherry.Moore@Sun.COM 	pos = *bytes_written;
19499160SSherry.Moore@Sun.COM 	bytes_left = physpath_size - pos;
19509160SSherry.Moore@Sun.COM 	format = (pos == 0) ? "%s" : " %s";
19519160SSherry.Moore@Sun.COM 
19529160SSherry.Moore@Sun.COM 	rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
19539160SSherry.Moore@Sun.COM 	*bytes_written += rsz;
19549160SSherry.Moore@Sun.COM 
19559160SSherry.Moore@Sun.COM 	if (rsz >= bytes_left) {
19569160SSherry.Moore@Sun.COM 		/* if physpath was not copied properly, clear it */
19579160SSherry.Moore@Sun.COM 		if (bytes_left != 0) {
19589160SSherry.Moore@Sun.COM 			physpath[pos] = 0;
19599160SSherry.Moore@Sun.COM 		}
19609160SSherry.Moore@Sun.COM 		return (EZFS_NOSPC);
19619160SSherry.Moore@Sun.COM 	}
19629160SSherry.Moore@Sun.COM 	return (0);
19639160SSherry.Moore@Sun.COM }
19649160SSherry.Moore@Sun.COM 
19659790SLin.Ling@Sun.COM static int
19669790SLin.Ling@Sun.COM vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
19679790SLin.Ling@Sun.COM     size_t *rsz, boolean_t is_spare)
19689790SLin.Ling@Sun.COM {
19699790SLin.Ling@Sun.COM 	char *type;
19709790SLin.Ling@Sun.COM 	int ret;
19719790SLin.Ling@Sun.COM 
19729790SLin.Ling@Sun.COM 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
19739790SLin.Ling@Sun.COM 		return (EZFS_INVALCONFIG);
19749790SLin.Ling@Sun.COM 
19759790SLin.Ling@Sun.COM 	if (strcmp(type, VDEV_TYPE_DISK) == 0) {
19769790SLin.Ling@Sun.COM 		/*
19779790SLin.Ling@Sun.COM 		 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
19789790SLin.Ling@Sun.COM 		 * For a spare vdev, we only want to boot from the active
19799790SLin.Ling@Sun.COM 		 * spare device.
19809790SLin.Ling@Sun.COM 		 */
19819790SLin.Ling@Sun.COM 		if (is_spare) {
19829790SLin.Ling@Sun.COM 			uint64_t spare = 0;
19839790SLin.Ling@Sun.COM 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
19849790SLin.Ling@Sun.COM 			    &spare);
19859790SLin.Ling@Sun.COM 			if (!spare)
19869790SLin.Ling@Sun.COM 				return (EZFS_INVALCONFIG);
19879790SLin.Ling@Sun.COM 		}
19889790SLin.Ling@Sun.COM 
19899790SLin.Ling@Sun.COM 		if (vdev_online(nv)) {
19909790SLin.Ling@Sun.COM 			if ((ret = vdev_get_one_physpath(nv, physpath,
19919790SLin.Ling@Sun.COM 			    phypath_size, rsz)) != 0)
19929790SLin.Ling@Sun.COM 				return (ret);
19939790SLin.Ling@Sun.COM 		}
19949790SLin.Ling@Sun.COM 	} else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
19959790SLin.Ling@Sun.COM 	    strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
19969790SLin.Ling@Sun.COM 	    (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
19979790SLin.Ling@Sun.COM 		nvlist_t **child;
19989790SLin.Ling@Sun.COM 		uint_t count;
19999790SLin.Ling@Sun.COM 		int i, ret;
20009790SLin.Ling@Sun.COM 
20019790SLin.Ling@Sun.COM 		if (nvlist_lookup_nvlist_array(nv,
20029790SLin.Ling@Sun.COM 		    ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
20039790SLin.Ling@Sun.COM 			return (EZFS_INVALCONFIG);
20049790SLin.Ling@Sun.COM 
20059790SLin.Ling@Sun.COM 		for (i = 0; i < count; i++) {
20069790SLin.Ling@Sun.COM 			ret = vdev_get_physpaths(child[i], physpath,
20079790SLin.Ling@Sun.COM 			    phypath_size, rsz, is_spare);
20089790SLin.Ling@Sun.COM 			if (ret == EZFS_NOSPC)
20099790SLin.Ling@Sun.COM 				return (ret);
20109790SLin.Ling@Sun.COM 		}
20119790SLin.Ling@Sun.COM 	}
20129790SLin.Ling@Sun.COM 
20139790SLin.Ling@Sun.COM 	return (EZFS_POOL_INVALARG);
20149790SLin.Ling@Sun.COM }
20159790SLin.Ling@Sun.COM 
20169160SSherry.Moore@Sun.COM /*
20179160SSherry.Moore@Sun.COM  * Get phys_path for a root pool config.
20189160SSherry.Moore@Sun.COM  * Return 0 on success; non-zero on failure.
20199160SSherry.Moore@Sun.COM  */
20209160SSherry.Moore@Sun.COM static int
20219160SSherry.Moore@Sun.COM zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
20229160SSherry.Moore@Sun.COM {
20239160SSherry.Moore@Sun.COM 	size_t rsz;
20247656SSherry.Moore@Sun.COM 	nvlist_t *vdev_root;
20257656SSherry.Moore@Sun.COM 	nvlist_t **child;
20267656SSherry.Moore@Sun.COM 	uint_t count;
20279160SSherry.Moore@Sun.COM 	char *type;
20289160SSherry.Moore@Sun.COM 
20299160SSherry.Moore@Sun.COM 	rsz = 0;
20309160SSherry.Moore@Sun.COM 
20319160SSherry.Moore@Sun.COM 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
20329160SSherry.Moore@Sun.COM 	    &vdev_root) != 0)
20339160SSherry.Moore@Sun.COM 		return (EZFS_INVALCONFIG);
20349160SSherry.Moore@Sun.COM 
20359160SSherry.Moore@Sun.COM 	if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
20369160SSherry.Moore@Sun.COM 	    nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
20379160SSherry.Moore@Sun.COM 	    &child, &count) != 0)
20389160SSherry.Moore@Sun.COM 		return (EZFS_INVALCONFIG);
20397656SSherry.Moore@Sun.COM 
20407656SSherry.Moore@Sun.COM 	/*
20419160SSherry.Moore@Sun.COM 	 * root pool can not have EFI labeled disks and can only have
20429160SSherry.Moore@Sun.COM 	 * a single top-level vdev.
20437656SSherry.Moore@Sun.COM 	 */
20449160SSherry.Moore@Sun.COM 	if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
20459160SSherry.Moore@Sun.COM 	    pool_uses_efi(vdev_root))
20469160SSherry.Moore@Sun.COM 		return (EZFS_POOL_INVALARG);
20479160SSherry.Moore@Sun.COM 
20489790SLin.Ling@Sun.COM 	(void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
20499790SLin.Ling@Sun.COM 	    B_FALSE);
20507656SSherry.Moore@Sun.COM 
20519160SSherry.Moore@Sun.COM 	/* No online devices */
20529160SSherry.Moore@Sun.COM 	if (rsz == 0)
20539160SSherry.Moore@Sun.COM 		return (EZFS_NODEVICE);
20549160SSherry.Moore@Sun.COM 
20557656SSherry.Moore@Sun.COM 	return (0);
20567656SSherry.Moore@Sun.COM }
20577656SSherry.Moore@Sun.COM 
20582468Sek110237 /*
20599160SSherry.Moore@Sun.COM  * Get phys_path for a root pool
20609160SSherry.Moore@Sun.COM  * Return 0 on success; non-zero on failure.
20619160SSherry.Moore@Sun.COM  */
20629160SSherry.Moore@Sun.COM int
20639160SSherry.Moore@Sun.COM zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
20649160SSherry.Moore@Sun.COM {
20659160SSherry.Moore@Sun.COM 	return (zpool_get_config_physpath(zhp->zpool_config, physpath,
20669160SSherry.Moore@Sun.COM 	    phypath_size));
20679160SSherry.Moore@Sun.COM }
20689160SSherry.Moore@Sun.COM 
20699160SSherry.Moore@Sun.COM /*
20709816SGeorge.Wilson@Sun.COM  * If the device has being dynamically expanded then we need to relabel
20719816SGeorge.Wilson@Sun.COM  * the disk to use the new unallocated space.
20729816SGeorge.Wilson@Sun.COM  */
20739816SGeorge.Wilson@Sun.COM static int
20749816SGeorge.Wilson@Sun.COM zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
20759816SGeorge.Wilson@Sun.COM {
20769816SGeorge.Wilson@Sun.COM 	char path[MAXPATHLEN];
20779816SGeorge.Wilson@Sun.COM 	char errbuf[1024];
20789816SGeorge.Wilson@Sun.COM 	int fd, error;
20799816SGeorge.Wilson@Sun.COM 	int (*_efi_use_whole_disk)(int);
20809816SGeorge.Wilson@Sun.COM 
20819816SGeorge.Wilson@Sun.COM 	if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
20829816SGeorge.Wilson@Sun.COM 	    "efi_use_whole_disk")) == NULL)
20839816SGeorge.Wilson@Sun.COM 		return (-1);
20849816SGeorge.Wilson@Sun.COM 
20859816SGeorge.Wilson@Sun.COM 	(void) snprintf(path, sizeof (path), "%s/%s", RDISK_ROOT, name);
20869816SGeorge.Wilson@Sun.COM 
20879816SGeorge.Wilson@Sun.COM 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
20889816SGeorge.Wilson@Sun.COM 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
20899816SGeorge.Wilson@Sun.COM 		    "relabel '%s': unable to open device"), name);
20909816SGeorge.Wilson@Sun.COM 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
20919816SGeorge.Wilson@Sun.COM 	}
20929816SGeorge.Wilson@Sun.COM 
20939816SGeorge.Wilson@Sun.COM 	/*
20949816SGeorge.Wilson@Sun.COM 	 * It's possible that we might encounter an error if the device
20959816SGeorge.Wilson@Sun.COM 	 * does not have any unallocated space left. If so, we simply
20969816SGeorge.Wilson@Sun.COM 	 * ignore that error and continue on.
20979816SGeorge.Wilson@Sun.COM 	 */
20989816SGeorge.Wilson@Sun.COM 	error = _efi_use_whole_disk(fd);
20999816SGeorge.Wilson@Sun.COM 	(void) close(fd);
21009816SGeorge.Wilson@Sun.COM 	if (error && error != VT_ENOSPC) {
21019816SGeorge.Wilson@Sun.COM 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
21029816SGeorge.Wilson@Sun.COM 		    "relabel '%s': unable to read disk capacity"), name);
21039816SGeorge.Wilson@Sun.COM 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
21049816SGeorge.Wilson@Sun.COM 	}
21059816SGeorge.Wilson@Sun.COM 	return (0);
21069816SGeorge.Wilson@Sun.COM }
21079816SGeorge.Wilson@Sun.COM 
21089816SGeorge.Wilson@Sun.COM /*
21094451Seschrock  * Bring the specified vdev online.   The 'flags' parameter is a set of the
21104451Seschrock  * ZFS_ONLINE_* flags.
2111789Sahrens  */
2112789Sahrens int
21134451Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
21144451Seschrock     vdev_state_t *newstate)
2115789Sahrens {
2116789Sahrens 	zfs_cmd_t zc = { 0 };
2117789Sahrens 	char msg[1024];
21182082Seschrock 	nvlist_t *tgt;
21199816SGeorge.Wilson@Sun.COM 	boolean_t avail_spare, l2cache, islog;
21202082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2121789Sahrens 
21229816SGeorge.Wilson@Sun.COM 	if (flags & ZFS_ONLINE_EXPAND) {
21239816SGeorge.Wilson@Sun.COM 		(void) snprintf(msg, sizeof (msg),
21249816SGeorge.Wilson@Sun.COM 		    dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
21259816SGeorge.Wilson@Sun.COM 	} else {
21269816SGeorge.Wilson@Sun.COM 		(void) snprintf(msg, sizeof (msg),
21279816SGeorge.Wilson@Sun.COM 		    dgettext(TEXT_DOMAIN, "cannot online %s"), path);
21289816SGeorge.Wilson@Sun.COM 	}
2129789Sahrens 
21301544Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
21317326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
21329816SGeorge.Wilson@Sun.COM 	    &islog)) == NULL)
21332082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2134789Sahrens 
21352468Sek110237 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
21362468Sek110237 
213710817SEric.Schrock@Sun.COM 	if (avail_spare)
21382082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
21392082Seschrock 
21409816SGeorge.Wilson@Sun.COM 	if (flags & ZFS_ONLINE_EXPAND ||
21419816SGeorge.Wilson@Sun.COM 	    zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
21429816SGeorge.Wilson@Sun.COM 		char *pathname = NULL;
21439816SGeorge.Wilson@Sun.COM 		uint64_t wholedisk = 0;
21449816SGeorge.Wilson@Sun.COM 
21459816SGeorge.Wilson@Sun.COM 		(void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
21469816SGeorge.Wilson@Sun.COM 		    &wholedisk);
21479816SGeorge.Wilson@Sun.COM 		verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
21489816SGeorge.Wilson@Sun.COM 		    &pathname) == 0);
21499816SGeorge.Wilson@Sun.COM 
21509816SGeorge.Wilson@Sun.COM 		/*
21519816SGeorge.Wilson@Sun.COM 		 * XXX - L2ARC 1.0 devices can't support expansion.
21529816SGeorge.Wilson@Sun.COM 		 */
21539816SGeorge.Wilson@Sun.COM 		if (l2cache) {
21549816SGeorge.Wilson@Sun.COM 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
21559816SGeorge.Wilson@Sun.COM 			    "cannot expand cache devices"));
21569816SGeorge.Wilson@Sun.COM 			return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
21579816SGeorge.Wilson@Sun.COM 		}
21589816SGeorge.Wilson@Sun.COM 
21599816SGeorge.Wilson@Sun.COM 		if (wholedisk) {
21609816SGeorge.Wilson@Sun.COM 			pathname += strlen(DISK_ROOT) + 1;
2161*13037SMark.Musante@Sun.COM 			(void) zpool_relabel_disk(hdl, pathname);
21629816SGeorge.Wilson@Sun.COM 		}
21639816SGeorge.Wilson@Sun.COM 	}
21649816SGeorge.Wilson@Sun.COM 
21654451Seschrock 	zc.zc_cookie = VDEV_STATE_ONLINE;
21664451Seschrock 	zc.zc_obj = flags;
21674451Seschrock 
2168*13037SMark.Musante@Sun.COM 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
216911422SMark.Musante@Sun.COM 		if (errno == EINVAL) {
217011422SMark.Musante@Sun.COM 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
217111422SMark.Musante@Sun.COM 			    "from this pool into a new one.  Use '%s' "
217211422SMark.Musante@Sun.COM 			    "instead"), "zpool detach");
217311422SMark.Musante@Sun.COM 			return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
217411422SMark.Musante@Sun.COM 		}
21754451Seschrock 		return (zpool_standard_error(hdl, errno, msg));
217611422SMark.Musante@Sun.COM 	}
21774451Seschrock 
21784451Seschrock 	*newstate = zc.zc_cookie;
21794451Seschrock 	return (0);
2180789Sahrens }
2181789Sahrens 
2182789Sahrens /*
2183789Sahrens  * Take the specified vdev offline
2184789Sahrens  */
2185789Sahrens int
21864451Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2187789Sahrens {
2188789Sahrens 	zfs_cmd_t zc = { 0 };
2189789Sahrens 	char msg[1024];
21902082Seschrock 	nvlist_t *tgt;
21915450Sbrendan 	boolean_t avail_spare, l2cache;
21922082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2193789Sahrens 
21941544Seschrock 	(void) snprintf(msg, sizeof (msg),
21951544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
21961544Seschrock 
2197789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
21987326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
21997326SEric.Schrock@Sun.COM 	    NULL)) == NULL)
22002082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
22012082Seschrock 
22022468Sek110237 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
22032468Sek110237 
220410817SEric.Schrock@Sun.COM 	if (avail_spare)
22052082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
22062082Seschrock 
22074451Seschrock 	zc.zc_cookie = VDEV_STATE_OFFLINE;
22084451Seschrock 	zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
22091485Slling 
2210*13037SMark.Musante@Sun.COM 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2211789Sahrens 		return (0);
2212789Sahrens 
2213789Sahrens 	switch (errno) {
22142082Seschrock 	case EBUSY:
2215789Sahrens 
2216789Sahrens 		/*
2217789Sahrens 		 * There are no other replicas of this device.
2218789Sahrens 		 */
22192082Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
22202082Seschrock 
22219701SGeorge.Wilson@Sun.COM 	case EEXIST:
22229701SGeorge.Wilson@Sun.COM 		/*
22239701SGeorge.Wilson@Sun.COM 		 * The log device has unplayed logs
22249701SGeorge.Wilson@Sun.COM 		 */
22259701SGeorge.Wilson@Sun.COM 		return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
22269701SGeorge.Wilson@Sun.COM 
22272082Seschrock 	default:
22282082Seschrock 		return (zpool_standard_error(hdl, errno, msg));
22292082Seschrock 	}
22302082Seschrock }
2231789Sahrens 
22322082Seschrock /*
22334451Seschrock  * Mark the given vdev faulted.
22344451Seschrock  */
22354451Seschrock int
223610817SEric.Schrock@Sun.COM zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
22374451Seschrock {
22384451Seschrock 	zfs_cmd_t zc = { 0 };
22394451Seschrock 	char msg[1024];
22404451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
22414451Seschrock 
22424451Seschrock 	(void) snprintf(msg, sizeof (msg),
22434451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
22444451Seschrock 
22454451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
22464451Seschrock 	zc.zc_guid = guid;
22474451Seschrock 	zc.zc_cookie = VDEV_STATE_FAULTED;
224810817SEric.Schrock@Sun.COM 	zc.zc_obj = aux;
22494451Seschrock 
2250*13037SMark.Musante@Sun.COM 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
22514451Seschrock 		return (0);
22524451Seschrock 
22534451Seschrock 	switch (errno) {
22544451Seschrock 	case EBUSY:
22554451Seschrock 
22564451Seschrock 		/*
22574451Seschrock 		 * There are no other replicas of this device.
22584451Seschrock 		 */
22594451Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
22604451Seschrock 
22614451Seschrock 	default:
22624451Seschrock 		return (zpool_standard_error(hdl, errno, msg));
22634451Seschrock 	}
22644451Seschrock 
22654451Seschrock }
22664451Seschrock 
22674451Seschrock /*
22684451Seschrock  * Mark the given vdev degraded.
22694451Seschrock  */
22704451Seschrock int
227110817SEric.Schrock@Sun.COM zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
22724451Seschrock {
22734451Seschrock 	zfs_cmd_t zc = { 0 };
22744451Seschrock 	char msg[1024];
22754451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
22764451Seschrock 
22774451Seschrock 	(void) snprintf(msg, sizeof (msg),
22784451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
22794451Seschrock 
22804451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
22814451Seschrock 	zc.zc_guid = guid;
22824451Seschrock 	zc.zc_cookie = VDEV_STATE_DEGRADED;
228310817SEric.Schrock@Sun.COM 	zc.zc_obj = aux;
22844451Seschrock 
2285*13037SMark.Musante@Sun.COM 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
22864451Seschrock 		return (0);
22874451Seschrock 
22884451Seschrock 	return (zpool_standard_error(hdl, errno, msg));
22894451Seschrock }
22904451Seschrock 
22914451Seschrock /*
22922082Seschrock  * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
22932082Seschrock  * a hot spare.
22942082Seschrock  */
22952082Seschrock static boolean_t
22962082Seschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
22972082Seschrock {
22982082Seschrock 	nvlist_t **child;
22992082Seschrock 	uint_t c, children;
23002082Seschrock 	char *type;
23012082Seschrock 
23022082Seschrock 	if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
23032082Seschrock 	    &children) == 0) {
23042082Seschrock 		verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
23052082Seschrock 		    &type) == 0);
23062082Seschrock 
23072082Seschrock 		if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
23082082Seschrock 		    children == 2 && child[which] == tgt)
23092082Seschrock 			return (B_TRUE);
23102082Seschrock 
23112082Seschrock 		for (c = 0; c < children; c++)
23122082Seschrock 			if (is_replacing_spare(child[c], tgt, which))
23132082Seschrock 				return (B_TRUE);
2314789Sahrens 	}
23152082Seschrock 
23162082Seschrock 	return (B_FALSE);
2317789Sahrens }
2318789Sahrens 
2319789Sahrens /*
2320789Sahrens  * Attach new_disk (fully described by nvroot) to old_disk.
23214527Sperrin  * If 'replacing' is specified, the new disk will replace the old one.
2322789Sahrens  */
2323789Sahrens int
2324789Sahrens zpool_vdev_attach(zpool_handle_t *zhp,
2325789Sahrens     const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2326789Sahrens {
2327789Sahrens 	zfs_cmd_t zc = { 0 };
2328789Sahrens 	char msg[1024];
2329789Sahrens 	int ret;
23302082Seschrock 	nvlist_t *tgt;
23317326SEric.Schrock@Sun.COM 	boolean_t avail_spare, l2cache, islog;
23327326SEric.Schrock@Sun.COM 	uint64_t val;
2333*13037SMark.Musante@Sun.COM 	char *newname;
23342082Seschrock 	nvlist_t **child;
23352082Seschrock 	uint_t children;
23362082Seschrock 	nvlist_t *config_root;
23372082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
23387965SGeorge.Wilson@Sun.COM 	boolean_t rootpool = pool_is_bootable(zhp);
2339789Sahrens 
23401544Seschrock 	if (replacing)
23411544Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
23421544Seschrock 		    "cannot replace %s with %s"), old_disk, new_disk);
23431544Seschrock 	else
23441544Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
23451544Seschrock 		    "cannot attach %s to %s"), new_disk, old_disk);
23461544Seschrock 
23477965SGeorge.Wilson@Sun.COM 	/*
23487965SGeorge.Wilson@Sun.COM 	 * If this is a root pool, make sure that we're not attaching an
23497965SGeorge.Wilson@Sun.COM 	 * EFI labeled device.
23507965SGeorge.Wilson@Sun.COM 	 */
23517965SGeorge.Wilson@Sun.COM 	if (rootpool && pool_uses_efi(nvroot)) {
23527965SGeorge.Wilson@Sun.COM 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
23537965SGeorge.Wilson@Sun.COM 		    "EFI labeled devices are not supported on root pools."));
23547965SGeorge.Wilson@Sun.COM 		return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
23557965SGeorge.Wilson@Sun.COM 	}
23567965SGeorge.Wilson@Sun.COM 
2357789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
23587326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
23597326SEric.Schrock@Sun.COM 	    &islog)) == 0)
23602082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
23612082Seschrock 
23622468Sek110237 	if (avail_spare)
23632082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
23642082Seschrock 
23655450Sbrendan 	if (l2cache)
23665450Sbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
23675450Sbrendan 
23682082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
23692082Seschrock 	zc.zc_cookie = replacing;
23702082Seschrock 
23712082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
23722082Seschrock 	    &child, &children) != 0 || children != 1) {
23732082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
23742082Seschrock 		    "new device must be a single disk"));
23752082Seschrock 		return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
23761544Seschrock 	}
23772082Seschrock 
23782082Seschrock 	verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
23792082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
23802082Seschrock 
238110594SGeorge.Wilson@Sun.COM 	if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
23827041Seschrock 		return (-1);
23837041Seschrock 
23842082Seschrock 	/*
23852082Seschrock 	 * If the target is a hot spare that has been swapped in, we can only
23862082Seschrock 	 * replace it with another hot spare.
23872082Seschrock 	 */
23882082Seschrock 	if (replacing &&
23892082Seschrock 	    nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
23907326SEric.Schrock@Sun.COM 	    (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
23917326SEric.Schrock@Sun.COM 	    NULL) == NULL || !avail_spare) &&
23927326SEric.Schrock@Sun.COM 	    is_replacing_spare(config_root, tgt, 1)) {
23932082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
23942082Seschrock 		    "can only be replaced by another hot spare"));
23957041Seschrock 		free(newname);
23962082Seschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
23972082Seschrock 	}
23982082Seschrock 
23997041Seschrock 	free(newname);
24007041Seschrock 
24015094Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
24022082Seschrock 		return (-1);
2403789Sahrens 
2404*13037SMark.Musante@Sun.COM 	ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2405789Sahrens 
24062676Seschrock 	zcmd_free_nvlists(&zc);
2407789Sahrens 
24087965SGeorge.Wilson@Sun.COM 	if (ret == 0) {
24097965SGeorge.Wilson@Sun.COM 		if (rootpool) {
24107965SGeorge.Wilson@Sun.COM 			/*
24119790SLin.Ling@Sun.COM 			 * XXX need a better way to prevent user from
24129790SLin.Ling@Sun.COM 			 * booting up a half-baked vdev.
24139790SLin.Ling@Sun.COM 			 */
24149790SLin.Ling@Sun.COM 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
24159790SLin.Ling@Sun.COM 			    "sure to wait until resilver is done "
24169790SLin.Ling@Sun.COM 			    "before rebooting.\n"));
24177965SGeorge.Wilson@Sun.COM 		}
2418789Sahrens 		return (0);
24197965SGeorge.Wilson@Sun.COM 	}
2420789Sahrens 
2421789Sahrens 	switch (errno) {
24221544Seschrock 	case ENOTSUP:
2423789Sahrens 		/*
2424789Sahrens 		 * Can't attach to or replace this type of vdev.
2425789Sahrens 		 */
24264527Sperrin 		if (replacing) {
2427*13037SMark.Musante@Sun.COM 			uint64_t version = zpool_get_prop_int(zhp,
2428*13037SMark.Musante@Sun.COM 			    ZPOOL_PROP_VERSION, NULL);
2429*13037SMark.Musante@Sun.COM 
24307326SEric.Schrock@Sun.COM 			if (islog)
24314527Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
24324527Sperrin 				    "cannot replace a log with a spare"));
2433*13037SMark.Musante@Sun.COM 			else if (version >= SPA_VERSION_MULTI_REPLACE)
2434*13037SMark.Musante@Sun.COM 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2435*13037SMark.Musante@Sun.COM 				    "already in replacing/spare config; wait "
2436*13037SMark.Musante@Sun.COM 				    "for completion or use 'zpool detach'"));
24374527Sperrin 			else
24384527Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
24394527Sperrin 				    "cannot replace a replacing device"));
24404527Sperrin 		} else {
24412082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
24422082Seschrock 			    "can only attach to mirrors and top-level "
24432082Seschrock 			    "disks"));
24444527Sperrin 		}
24452082Seschrock 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
2446789Sahrens 		break;
2447789Sahrens 
24481544Seschrock 	case EINVAL:
2449789Sahrens 		/*
2450789Sahrens 		 * The new device must be a single disk.
2451789Sahrens 		 */
24522082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
24532082Seschrock 		    "new device must be a single disk"));
24542082Seschrock 		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2455789Sahrens 		break;
2456789Sahrens 
24571544Seschrock 	case EBUSY:
24582082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
24592082Seschrock 		    new_disk);
24602082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2461789Sahrens 		break;
2462789Sahrens 
24631544Seschrock 	case EOVERFLOW:
2464789Sahrens 		/*
2465789Sahrens 		 * The new device is too small.
2466789Sahrens 		 */
24672082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
24682082Seschrock 		    "device is too small"));
24692082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2470789Sahrens 		break;
2471789Sahrens 
24721544Seschrock 	case EDOM:
2473789Sahrens 		/*
2474789Sahrens 		 * The new device has a different alignment requirement.
2475789Sahrens 		 */
24762082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
24772082Seschrock 		    "devices have different sector alignment"));
24782082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2479789Sahrens 		break;
2480789Sahrens 
24811544Seschrock 	case ENAMETOOLONG:
2482789Sahrens 		/*
2483789Sahrens 		 * The resulting top-level vdev spec won't fit in the label.
2484789Sahrens 		 */
24852082Seschrock 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2486789Sahrens 		break;
2487789Sahrens 
24881544Seschrock 	default:
24892082Seschrock 		(void) zpool_standard_error(hdl, errno, msg);
2490789Sahrens 	}
2491789Sahrens 
24922082Seschrock 	return (-1);
2493789Sahrens }
2494789Sahrens 
2495789Sahrens /*
2496789Sahrens  * Detach the specified device.
2497789Sahrens  */
2498789Sahrens int
2499789Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2500789Sahrens {
2501789Sahrens 	zfs_cmd_t zc = { 0 };
2502789Sahrens 	char msg[1024];
25032082Seschrock 	nvlist_t *tgt;
25045450Sbrendan 	boolean_t avail_spare, l2cache;
25052082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2506789Sahrens 
25071544Seschrock 	(void) snprintf(msg, sizeof (msg),
25081544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
25091544Seschrock 
2510789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
25117326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
25127326SEric.Schrock@Sun.COM 	    NULL)) == 0)
25132082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2514789Sahrens 
25152468Sek110237 	if (avail_spare)
25162082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
25172082Seschrock 
25185450Sbrendan 	if (l2cache)
25195450Sbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
25205450Sbrendan 
25212082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
25222082Seschrock 
25234543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2524789Sahrens 		return (0);
2525789Sahrens 
2526789Sahrens 	switch (errno) {
2527789Sahrens 
25281544Seschrock 	case ENOTSUP:
2529789Sahrens 		/*
2530789Sahrens 		 * Can't detach from this type of vdev.
2531789Sahrens 		 */
25322082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
25332082Seschrock 		    "applicable to mirror and replacing vdevs"));
2534*13037SMark.Musante@Sun.COM 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
2535789Sahrens 		break;
2536789Sahrens 
25371544Seschrock 	case EBUSY:
2538789Sahrens 		/*
2539789Sahrens 		 * There are no other replicas of this device.
2540789Sahrens 		 */
25412082Seschrock 		(void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2542789Sahrens 		break;
2543789Sahrens 
25441544Seschrock 	default:
25452082Seschrock 		(void) zpool_standard_error(hdl, errno, msg);
25461544Seschrock 	}
25471544Seschrock 
25482082Seschrock 	return (-1);
25492082Seschrock }
25502082Seschrock 
25512082Seschrock /*
255211422SMark.Musante@Sun.COM  * Find a mirror vdev in the source nvlist.
255311422SMark.Musante@Sun.COM  *
255411422SMark.Musante@Sun.COM  * The mchild array contains a list of disks in one of the top-level mirrors
255511422SMark.Musante@Sun.COM  * of the source pool.  The schild array contains a list of disks that the
255611422SMark.Musante@Sun.COM  * user specified on the command line.  We loop over the mchild array to
255711422SMark.Musante@Sun.COM  * see if any entry in the schild array matches.
255811422SMark.Musante@Sun.COM  *
255911422SMark.Musante@Sun.COM  * If a disk in the mchild array is found in the schild array, we return
256011422SMark.Musante@Sun.COM  * the index of that entry.  Otherwise we return -1.
256111422SMark.Musante@Sun.COM  */
256211422SMark.Musante@Sun.COM static int
256311422SMark.Musante@Sun.COM find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
256411422SMark.Musante@Sun.COM     nvlist_t **schild, uint_t schildren)
256511422SMark.Musante@Sun.COM {
256611422SMark.Musante@Sun.COM 	uint_t mc;
256711422SMark.Musante@Sun.COM 
256811422SMark.Musante@Sun.COM 	for (mc = 0; mc < mchildren; mc++) {
256911422SMark.Musante@Sun.COM 		uint_t sc;
257011422SMark.Musante@Sun.COM 		char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
257111422SMark.Musante@Sun.COM 		    mchild[mc], B_FALSE);
257211422SMark.Musante@Sun.COM 
257311422SMark.Musante@Sun.COM 		for (sc = 0; sc < schildren; sc++) {
257411422SMark.Musante@Sun.COM 			char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
257511422SMark.Musante@Sun.COM 			    schild[sc], B_FALSE);
257611422SMark.Musante@Sun.COM 			boolean_t result = (strcmp(mpath, spath) == 0);
257711422SMark.Musante@Sun.COM 
257811422SMark.Musante@Sun.COM 			free(spath);
257911422SMark.Musante@Sun.COM 			if (result) {
258011422SMark.Musante@Sun.COM 				free(mpath);
258111422SMark.Musante@Sun.COM 				return (mc);
258211422SMark.Musante@Sun.COM 			}
258311422SMark.Musante@Sun.COM 		}
258411422SMark.Musante@Sun.COM 
258511422SMark.Musante@Sun.COM 		free(mpath);
258611422SMark.Musante@Sun.COM 	}
258711422SMark.Musante@Sun.COM 
258811422SMark.Musante@Sun.COM 	return (-1);
258911422SMark.Musante@Sun.COM }
259011422SMark.Musante@Sun.COM 
259111422SMark.Musante@Sun.COM /*
259211422SMark.Musante@Sun.COM  * Split a mirror pool.  If newroot points to null, then a new nvlist
259311422SMark.Musante@Sun.COM  * is generated and it is the responsibility of the caller to free it.
259411422SMark.Musante@Sun.COM  */
259511422SMark.Musante@Sun.COM int
259611422SMark.Musante@Sun.COM zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
259711422SMark.Musante@Sun.COM     nvlist_t *props, splitflags_t flags)
259811422SMark.Musante@Sun.COM {
259911422SMark.Musante@Sun.COM 	zfs_cmd_t zc = { 0 };
260011422SMark.Musante@Sun.COM 	char msg[1024];
260111422SMark.Musante@Sun.COM 	nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
260211422SMark.Musante@Sun.COM 	nvlist_t **varray = NULL, *zc_props = NULL;
260311422SMark.Musante@Sun.COM 	uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
260411422SMark.Musante@Sun.COM 	libzfs_handle_t *hdl = zhp->zpool_hdl;
260511422SMark.Musante@Sun.COM 	uint64_t vers;
260611422SMark.Musante@Sun.COM 	boolean_t freelist = B_FALSE, memory_err = B_TRUE;
260711422SMark.Musante@Sun.COM 	int retval = 0;
260811422SMark.Musante@Sun.COM 
260911422SMark.Musante@Sun.COM 	(void) snprintf(msg, sizeof (msg),
261011422SMark.Musante@Sun.COM 	    dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
261111422SMark.Musante@Sun.COM 
261211422SMark.Musante@Sun.COM 	if (!zpool_name_valid(hdl, B_FALSE, newname))
261311422SMark.Musante@Sun.COM 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
261411422SMark.Musante@Sun.COM 
261511422SMark.Musante@Sun.COM 	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
261611422SMark.Musante@Sun.COM 		(void) fprintf(stderr, gettext("Internal error: unable to "
261711422SMark.Musante@Sun.COM 		    "retrieve pool configuration\n"));
261811422SMark.Musante@Sun.COM 		return (-1);
261911422SMark.Musante@Sun.COM 	}
262011422SMark.Musante@Sun.COM 
262111422SMark.Musante@Sun.COM 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
262211422SMark.Musante@Sun.COM 	    == 0);
262311422SMark.Musante@Sun.COM 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
262411422SMark.Musante@Sun.COM 
262511422SMark.Musante@Sun.COM 	if (props) {
262611422SMark.Musante@Sun.COM 		if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
262711422SMark.Musante@Sun.COM 		    props, vers, B_TRUE, msg)) == NULL)
262811422SMark.Musante@Sun.COM 			return (-1);
262911422SMark.Musante@Sun.COM 	}
263011422SMark.Musante@Sun.COM 
263111422SMark.Musante@Sun.COM 	if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
263211422SMark.Musante@Sun.COM 	    &children) != 0) {
263311422SMark.Musante@Sun.COM 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
263411422SMark.Musante@Sun.COM 		    "Source pool is missing vdev tree"));
263511422SMark.Musante@Sun.COM 		if (zc_props)
263611422SMark.Musante@Sun.COM 			nvlist_free(zc_props);
263711422SMark.Musante@Sun.COM 		return (-1);
263811422SMark.Musante@Sun.COM 	}
263911422SMark.Musante@Sun.COM 
264011422SMark.Musante@Sun.COM 	varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
264111422SMark.Musante@Sun.COM 	vcount = 0;
264211422SMark.Musante@Sun.COM 
264311422SMark.Musante@Sun.COM 	if (*newroot == NULL ||
264411422SMark.Musante@Sun.COM 	    nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
264511422SMark.Musante@Sun.COM 	    &newchild, &newchildren) != 0)
264611422SMark.Musante@Sun.COM 		newchildren = 0;
264711422SMark.Musante@Sun.COM 
264811422SMark.Musante@Sun.COM 	for (c = 0; c < children; c++) {
264911422SMark.Musante@Sun.COM 		uint64_t is_log = B_FALSE, is_hole = B_FALSE;
265011422SMark.Musante@Sun.COM 		char *type;
265111422SMark.Musante@Sun.COM 		nvlist_t **mchild, *vdev;
265211422SMark.Musante@Sun.COM 		uint_t mchildren;
265311422SMark.Musante@Sun.COM 		int entry;
265411422SMark.Musante@Sun.COM 
265511422SMark.Musante@Sun.COM 		/*
265611422SMark.Musante@Sun.COM 		 * Unlike cache & spares, slogs are stored in the
265711422SMark.Musante@Sun.COM 		 * ZPOOL_CONFIG_CHILDREN array.  We filter them out here.
265811422SMark.Musante@Sun.COM 		 */
265911422SMark.Musante@Sun.COM 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
266011422SMark.Musante@Sun.COM 		    &is_log);
266111422SMark.Musante@Sun.COM 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
266211422SMark.Musante@Sun.COM 		    &is_hole);
266311422SMark.Musante@Sun.COM 		if (is_log || is_hole) {
266411422SMark.Musante@Sun.COM 			/*
266511422SMark.Musante@Sun.COM 			 * Create a hole vdev and put it in the config.
266611422SMark.Musante@Sun.COM 			 */
266711422SMark.Musante@Sun.COM 			if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
266811422SMark.Musante@Sun.COM 				goto out;
266911422SMark.Musante@Sun.COM 			if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
267011422SMark.Musante@Sun.COM 			    VDEV_TYPE_HOLE) != 0)
267111422SMark.Musante@Sun.COM 				goto out;
267211422SMark.Musante@Sun.COM 			if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
267311422SMark.Musante@Sun.COM 			    1) != 0)
267411422SMark.Musante@Sun.COM 				goto out;
267511422SMark.Musante@Sun.COM 			if (lastlog == 0)
267611422SMark.Musante@Sun.COM 				lastlog = vcount;
267711422SMark.Musante@Sun.COM 			varray[vcount++] = vdev;
267811422SMark.Musante@Sun.COM 			continue;
267911422SMark.Musante@Sun.COM 		}
268011422SMark.Musante@Sun.COM 		lastlog = 0;
268111422SMark.Musante@Sun.COM 		verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
268211422SMark.Musante@Sun.COM 		    == 0);
268311422SMark.Musante@Sun.COM 		if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
268411422SMark.Musante@Sun.COM 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
268511422SMark.Musante@Sun.COM 			    "Source pool must be composed only of mirrors\n"));
268611422SMark.Musante@Sun.COM 			retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
268711422SMark.Musante@Sun.COM 			goto out;
268811422SMark.Musante@Sun.COM 		}
268911422SMark.Musante@Sun.COM 
269011422SMark.Musante@Sun.COM 		verify(nvlist_lookup_nvlist_array(child[c],
269111422SMark.Musante@Sun.COM 		    ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
269211422SMark.Musante@Sun.COM 
269311422SMark.Musante@Sun.COM 		/* find or add an entry for this top-level vdev */
269411422SMark.Musante@Sun.COM 		if (newchildren > 0 &&
269511422SMark.Musante@Sun.COM 		    (entry = find_vdev_entry(zhp, mchild, mchildren,
269611422SMark.Musante@Sun.COM 		    newchild, newchildren)) >= 0) {
269711422SMark.Musante@Sun.COM 			/* We found a disk that the user specified. */
269811422SMark.Musante@Sun.COM 			vdev = mchild[entry];
269911422SMark.Musante@Sun.COM 			++found;
270011422SMark.Musante@Sun.COM 		} else {
270111422SMark.Musante@Sun.COM 			/* User didn't specify a disk for this vdev. */
270211422SMark.Musante@Sun.COM 			vdev = mchild[mchildren - 1];
270311422SMark.Musante@Sun.COM 		}
270411422SMark.Musante@Sun.COM 
270511422SMark.Musante@Sun.COM 		if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
270611422SMark.Musante@Sun.COM 			goto out;
270711422SMark.Musante@Sun.COM 	}
270811422SMark.Musante@Sun.COM 
270911422SMark.Musante@Sun.COM 	/* did we find every disk the user specified? */
271011422SMark.Musante@Sun.COM 	if (found != newchildren) {
271111422SMark.Musante@Sun.COM 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
271211422SMark.Musante@Sun.COM 		    "include at most one disk from each mirror"));
271311422SMark.Musante@Sun.COM 		retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
271411422SMark.Musante@Sun.COM 		goto out;
271511422SMark.Musante@Sun.COM 	}
271611422SMark.Musante@Sun.COM 
271711422SMark.Musante@Sun.COM 	/* Prepare the nvlist for populating. */
271811422SMark.Musante@Sun.COM 	if (*newroot == NULL) {
271911422SMark.Musante@Sun.COM 		if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
272011422SMark.Musante@Sun.COM 			goto out;
272111422SMark.Musante@Sun.COM 		freelist = B_TRUE;
272211422SMark.Musante@Sun.COM 		if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
272311422SMark.Musante@Sun.COM 		    VDEV_TYPE_ROOT) != 0)
272411422SMark.Musante@Sun.COM 			goto out;
272511422SMark.Musante@Sun.COM 	} else {
272611422SMark.Musante@Sun.COM 		verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
272711422SMark.Musante@Sun.COM 	}
272811422SMark.Musante@Sun.COM 
272911422SMark.Musante@Sun.COM 	/* Add all the children we found */
273011422SMark.Musante@Sun.COM 	if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
273111422SMark.Musante@Sun.COM 	    lastlog == 0 ? vcount : lastlog) != 0)
273211422SMark.Musante@Sun.COM 		goto out;
273311422SMark.Musante@Sun.COM 
273411422SMark.Musante@Sun.COM 	/*
273511422SMark.Musante@Sun.COM 	 * If we're just doing a dry run, exit now with success.
273611422SMark.Musante@Sun.COM 	 */
273711422SMark.Musante@Sun.COM 	if (flags.dryrun) {
273811422SMark.Musante@Sun.COM 		memory_err = B_FALSE;
273911422SMark.Musante@Sun.COM 		freelist = B_FALSE;
274011422SMark.Musante@Sun.COM 		goto out;
274111422SMark.Musante@Sun.COM 	}
274211422SMark.Musante@Sun.COM 
274311422SMark.Musante@Sun.COM 	/* now build up the config list & call the ioctl */
274411422SMark.Musante@Sun.COM 	if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
274511422SMark.Musante@Sun.COM 		goto out;
274611422SMark.Musante@Sun.COM 
274711422SMark.Musante@Sun.COM 	if (nvlist_add_nvlist(newconfig,
274811422SMark.Musante@Sun.COM 	    ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
274911422SMark.Musante@Sun.COM 	    nvlist_add_string(newconfig,
275011422SMark.Musante@Sun.COM 	    ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
275111422SMark.Musante@Sun.COM 	    nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
275211422SMark.Musante@Sun.COM 		goto out;
275311422SMark.Musante@Sun.COM 
275411422SMark.Musante@Sun.COM 	/*
275511422SMark.Musante@Sun.COM 	 * The new pool is automatically part of the namespace unless we
275611422SMark.Musante@Sun.COM 	 * explicitly export it.
275711422SMark.Musante@Sun.COM 	 */
275811422SMark.Musante@Sun.COM 	if (!flags.import)
275911422SMark.Musante@Sun.COM 		zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
276011422SMark.Musante@Sun.COM 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
276111422SMark.Musante@Sun.COM 	(void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
276211422SMark.Musante@Sun.COM 	if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
276311422SMark.Musante@Sun.COM 		goto out;
276411422SMark.Musante@Sun.COM 	if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
276511422SMark.Musante@Sun.COM 		goto out;
276611422SMark.Musante@Sun.COM 
276711422SMark.Musante@Sun.COM 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
276811422SMark.Musante@Sun.COM 		retval = zpool_standard_error(hdl, errno, msg);
276911422SMark.Musante@Sun.COM 		goto out;
277011422SMark.Musante@Sun.COM 	}
277111422SMark.Musante@Sun.COM 
277211422SMark.Musante@Sun.COM 	freelist = B_FALSE;
277311422SMark.Musante@Sun.COM 	memory_err = B_FALSE;
277411422SMark.Musante@Sun.COM 
277511422SMark.Musante@Sun.COM out:
277611422SMark.Musante@Sun.COM 	if (varray != NULL) {
277711422SMark.Musante@Sun.COM 		int v;
277811422SMark.Musante@Sun.COM 
277911422SMark.Musante@Sun.COM 		for (v = 0; v < vcount; v++)
278011422SMark.Musante@Sun.COM 			nvlist_free(varray[v]);
278111422SMark.Musante@Sun.COM 		free(varray);
278211422SMark.Musante@Sun.COM 	}
278311422SMark.Musante@Sun.COM 	zcmd_free_nvlists(&zc);
278411422SMark.Musante@Sun.COM 	if (zc_props)
278511422SMark.Musante@Sun.COM 		nvlist_free(zc_props);
278611422SMark.Musante@Sun.COM 	if (newconfig)
278711422SMark.Musante@Sun.COM 		nvlist_free(newconfig);
278811422SMark.Musante@Sun.COM 	if (freelist) {
278911422SMark.Musante@Sun.COM 		nvlist_free(*newroot);
279011422SMark.Musante@Sun.COM 		*newroot = NULL;
279111422SMark.Musante@Sun.COM 	}
279211422SMark.Musante@Sun.COM 
279311422SMark.Musante@Sun.COM 	if (retval != 0)
279411422SMark.Musante@Sun.COM 		return (retval);
279511422SMark.Musante@Sun.COM 
279611422SMark.Musante@Sun.COM 	if (memory_err)
279711422SMark.Musante@Sun.COM 		return (no_memory(hdl));
279811422SMark.Musante@Sun.COM 
279911422SMark.Musante@Sun.COM 	return (0);
280011422SMark.Musante@Sun.COM }
280111422SMark.Musante@Sun.COM 
280211422SMark.Musante@Sun.COM /*
28035450Sbrendan  * Remove the given device.  Currently, this is supported only for hot spares
28045450Sbrendan  * and level 2 cache devices.
28052082Seschrock  */
28062082Seschrock int
28072082Seschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
28082082Seschrock {
28092082Seschrock 	zfs_cmd_t zc = { 0 };
28102082Seschrock 	char msg[1024];
28112082Seschrock 	nvlist_t *tgt;
281210594SGeorge.Wilson@Sun.COM 	boolean_t avail_spare, l2cache, islog;
28132082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
281410594SGeorge.Wilson@Sun.COM 	uint64_t version;
28152082Seschrock 
28162082Seschrock 	(void) snprintf(msg, sizeof (msg),
28172082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
28182082Seschrock 
28192082Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
28207326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
282110594SGeorge.Wilson@Sun.COM 	    &islog)) == 0)
28222082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
282310594SGeorge.Wilson@Sun.COM 	/*
282410594SGeorge.Wilson@Sun.COM 	 * XXX - this should just go away.
282510594SGeorge.Wilson@Sun.COM 	 */
282610594SGeorge.Wilson@Sun.COM 	if (!avail_spare && !l2cache && !islog) {
28272082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
282810594SGeorge.Wilson@Sun.COM 		    "only inactive hot spares, cache, top-level, "
282910594SGeorge.Wilson@Sun.COM 		    "or log devices can be removed"));
28302082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
28312082Seschrock 	}
28322082Seschrock 
283310594SGeorge.Wilson@Sun.COM 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
283410594SGeorge.Wilson@Sun.COM 	if (islog && version < SPA_VERSION_HOLES) {
283510594SGeorge.Wilson@Sun.COM 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
283610594SGeorge.Wilson@Sun.COM 		    "pool must be upgrade to support log removal"));
283710594SGeorge.Wilson@Sun.COM 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
283810594SGeorge.Wilson@Sun.COM 	}
283910594SGeorge.Wilson@Sun.COM 
28402082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
28412082Seschrock 
28424543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
28432082Seschrock 		return (0);
28442082Seschrock 
28452082Seschrock 	return (zpool_standard_error(hdl, errno, msg));
28461544Seschrock }
28471544Seschrock 
28481544Seschrock /*
28491544Seschrock  * Clear the errors for the pool, or the particular device if specified.
28501544Seschrock  */
28511544Seschrock int
285210921STim.Haley@Sun.COM zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
28531544Seschrock {
28541544Seschrock 	zfs_cmd_t zc = { 0 };
28551544Seschrock 	char msg[1024];
28562082Seschrock 	nvlist_t *tgt;
285710921STim.Haley@Sun.COM 	zpool_rewind_policy_t policy;
28585450Sbrendan 	boolean_t avail_spare, l2cache;
28592082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
286010921STim.Haley@Sun.COM 	nvlist_t *nvi = NULL;
286112949SGeorge.Wilson@Sun.COM 	int error;
28621544Seschrock 
28631544Seschrock 	if (path)
28641544Seschrock 		(void) snprintf(msg, sizeof (msg),
28651544Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
28662676Seschrock 		    path);
28671544Seschrock 	else
28681544Seschrock 		(void) snprintf(msg, sizeof (msg),
28691544Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
28701544Seschrock 		    zhp->zpool_name);
28711544Seschrock 
28721544Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
28732082Seschrock 	if (path) {
28745450Sbrendan 		if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
28757326SEric.Schrock@Sun.COM 		    &l2cache, NULL)) == 0)
28762082Seschrock 			return (zfs_error(hdl, EZFS_NODEVICE, msg));
28772082Seschrock 
28785450Sbrendan 		/*
28795450Sbrendan 		 * Don't allow error clearing for hot spares.  Do allow
28805450Sbrendan 		 * error clearing for l2cache devices.
28815450Sbrendan 		 */
28822468Sek110237 		if (avail_spare)
28832082Seschrock 			return (zfs_error(hdl, EZFS_ISSPARE, msg));
28842082Seschrock 
28852082Seschrock 		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
28862082Seschrock 		    &zc.zc_guid) == 0);
28871544Seschrock 	}
28881544Seschrock 
288910921STim.Haley@Sun.COM 	zpool_get_rewind_policy(rewindnvl, &policy);
289010921STim.Haley@Sun.COM 	zc.zc_cookie = policy.zrp_request;
289110921STim.Haley@Sun.COM 
289212974SGeorge.Wilson@Sun.COM 	if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
289310921STim.Haley@Sun.COM 		return (-1);
289410921STim.Haley@Sun.COM 
2895*13037SMark.Musante@Sun.COM 	if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
289610921STim.Haley@Sun.COM 		return (-1);
289710921STim.Haley@Sun.COM 
289812949SGeorge.Wilson@Sun.COM 	while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
289912949SGeorge.Wilson@Sun.COM 	    errno == ENOMEM) {
290012949SGeorge.Wilson@Sun.COM 		if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
290112949SGeorge.Wilson@Sun.COM 			zcmd_free_nvlists(&zc);
290212949SGeorge.Wilson@Sun.COM 			return (-1);
290312949SGeorge.Wilson@Sun.COM 		}
290412949SGeorge.Wilson@Sun.COM 	}
290512949SGeorge.Wilson@Sun.COM 
290612949SGeorge.Wilson@Sun.COM 	if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
290710921STim.Haley@Sun.COM 	    errno != EPERM && errno != EACCES)) {
290810921STim.Haley@Sun.COM 		if (policy.zrp_request &
290910921STim.Haley@Sun.COM 		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
291010921STim.Haley@Sun.COM 			(void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
291110921STim.Haley@Sun.COM 			zpool_rewind_exclaim(hdl, zc.zc_name,
291210921STim.Haley@Sun.COM 			    ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
291310921STim.Haley@Sun.COM 			    nvi);
291410921STim.Haley@Sun.COM 			nvlist_free(nvi);
291510921STim.Haley@Sun.COM 		}
291610921STim.Haley@Sun.COM 		zcmd_free_nvlists(&zc);
29171544Seschrock 		return (0);
291810921STim.Haley@Sun.COM 	}
291910921STim.Haley@Sun.COM 
292010921STim.Haley@Sun.COM 	zcmd_free_nvlists(&zc);
29212082Seschrock 	return (zpool_standard_error(hdl, errno, msg));
2922789Sahrens }
2923789Sahrens 
29243126Sahl /*
29254451Seschrock  * Similar to zpool_clear(), but takes a GUID (used by fmd).
29264451Seschrock  */
29274451Seschrock int
29284451Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
29294451Seschrock {
29304451Seschrock 	zfs_cmd_t zc = { 0 };
29314451Seschrock 	char msg[1024];
29324451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
29334451Seschrock 
29344451Seschrock 	(void) snprintf(msg, sizeof (msg),
29354451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
29364451Seschrock 	    guid);
29374451Seschrock 
29384451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
29394451Seschrock 	zc.zc_guid = guid;
294012441SVictor.Latushkin@Sun.COM 	zc.zc_cookie = ZPOOL_NO_REWIND;
29414451Seschrock 
29424451Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
29434451Seschrock 		return (0);
29444451Seschrock 
29454451Seschrock 	return (zpool_standard_error(hdl, errno, msg));
29464451Seschrock }
29474451Seschrock 
29484451Seschrock /*
29491354Seschrock  * Convert from a devid string to a path.
29501354Seschrock  */
29511354Seschrock static char *
29521354Seschrock devid_to_path(char *devid_str)
29531354Seschrock {
29541354Seschrock 	ddi_devid_t devid;
29551354Seschrock 	char *minor;
29561354Seschrock 	char *path;
29571354Seschrock 	devid_nmlist_t *list = NULL;
29581354Seschrock 	int ret;
29591354Seschrock 
29601354Seschrock 	if (devid_str_decode(devid_str, &devid, &minor) != 0)
29611354Seschrock 		return (NULL);
29621354Seschrock 
29631354Seschrock 	ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
29641354Seschrock 
29651354Seschrock 	devid_str_free(minor);
29661354Seschrock 	devid_free(devid);
29671354Seschrock 
29681354Seschrock 	if (ret != 0)
29691354Seschrock 		return (NULL);
29701354Seschrock 
29712082Seschrock 	if ((path = strdup(list[0].devname)) == NULL)
29722082Seschrock 		return (NULL);
29732082Seschrock 
29741354Seschrock 	devid_free_nmlist(list);
29751354Seschrock 
29761354Seschrock 	return (path);
29771354Seschrock }
29781354Seschrock 
29791354Seschrock /*
29801354Seschrock  * Convert from a path to a devid string.
29811354Seschrock  */
29821354Seschrock static char *
29831354Seschrock path_to_devid(const char *path)
29841354Seschrock {
29851354Seschrock 	int fd;
29861354Seschrock 	ddi_devid_t devid;
29871354Seschrock 	char *minor, *ret;
29881354Seschrock 
29891354Seschrock 	if ((fd = open(path, O_RDONLY)) < 0)
29901354Seschrock 		return (NULL);
29911354Seschrock 
29921354Seschrock 	minor = NULL;
29931354Seschrock 	ret = NULL;
29941354Seschrock 	if (devid_get(fd, &devid) == 0) {
29951354Seschrock 		if (devid_get_minor_name(fd, &minor) == 0)
29961354Seschrock 			ret = devid_str_encode(devid, minor);
29971354Seschrock 		if (minor != NULL)
29981354Seschrock 			devid_str_free(minor);
29991354Seschrock 		devid_free(devid);
30001354Seschrock 	}
30011354Seschrock 	(void) close(fd);
30021354Seschrock 
30031354Seschrock 	return (ret);
30041354Seschrock }
30051354Seschrock 
30061354Seschrock /*
30071354Seschrock  * Issue the necessary ioctl() to update the stored path value for the vdev.  We
30081354Seschrock  * ignore any failure here, since a common case is for an unprivileged user to
30091354Seschrock  * type 'zpool status', and we'll display the correct information anyway.
30101354Seschrock  */
30111354Seschrock static void
30121354Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
30131354Seschrock {
30141354Seschrock 	zfs_cmd_t zc = { 0 };
30151354Seschrock 
30161354Seschrock 	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
30172676Seschrock 	(void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
30181354Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
30191544Seschrock 	    &zc.zc_guid) == 0);
30201354Seschrock 
30212082Seschrock 	(void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
30221354Seschrock }
30231354Seschrock 
30241354Seschrock /*
30251354Seschrock  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
30261354Seschrock  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
30271354Seschrock  * We also check if this is a whole disk, in which case we strip off the
30281354Seschrock  * trailing 's0' slice name.
30291354Seschrock  *
30301354Seschrock  * This routine is also responsible for identifying when disks have been
30311354Seschrock  * reconfigured in a new location.  The kernel will have opened the device by
30321354Seschrock  * devid, but the path will still refer to the old location.  To catch this, we
30331354Seschrock  * first do a path -> devid translation (which is fast for the common case).  If
30341354Seschrock  * the devid matches, we're done.  If not, we do a reverse devid -> path
30351354Seschrock  * translation and issue the appropriate ioctl() to update the path of the vdev.
30361354Seschrock  * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
30371354Seschrock  * of these checks.
30381354Seschrock  */
30391354Seschrock char *
304010594SGeorge.Wilson@Sun.COM zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
304110594SGeorge.Wilson@Sun.COM     boolean_t verbose)
30421354Seschrock {
30431354Seschrock 	char *path, *devid;
30441544Seschrock 	uint64_t value;
30451544Seschrock 	char buf[64];
30464451Seschrock 	vdev_stat_t *vs;
30474451Seschrock 	uint_t vsc;
30481354Seschrock 
30491544Seschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
30501544Seschrock 	    &value) == 0) {
30511544Seschrock 		verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
30521544Seschrock 		    &value) == 0);
30532856Snd150628 		(void) snprintf(buf, sizeof (buf), "%llu",
30542856Snd150628 		    (u_longlong_t)value);
30551544Seschrock 		path = buf;
30561544Seschrock 	} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
30571354Seschrock 
30584451Seschrock 		/*
30594451Seschrock 		 * If the device is dead (faulted, offline, etc) then don't
30604451Seschrock 		 * bother opening it.  Otherwise we may be forcing the user to
30614451Seschrock 		 * open a misbehaving device, which can have undesirable
30624451Seschrock 		 * effects.
30634451Seschrock 		 */
306412296SLin.Ling@Sun.COM 		if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
30654451Seschrock 		    (uint64_t **)&vs, &vsc) != 0 ||
30664451Seschrock 		    vs->vs_state >= VDEV_STATE_DEGRADED) &&
30674451Seschrock 		    zhp != NULL &&
30681354Seschrock 		    nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
30691354Seschrock 			/*
30701354Seschrock 			 * Determine if the current path is correct.
30711354Seschrock 			 */
30721354Seschrock 			char *newdevid = path_to_devid(path);
30731354Seschrock 
30741354Seschrock 			if (newdevid == NULL ||
30751354Seschrock 			    strcmp(devid, newdevid) != 0) {
30761354Seschrock 				char *newpath;
30771354Seschrock 
30781354Seschrock 				if ((newpath = devid_to_path(devid)) != NULL) {
30791354Seschrock 					/*
30801354Seschrock 					 * Update the path appropriately.
30811354Seschrock 					 */
30821354Seschrock 					set_path(zhp, nv, newpath);
30832082Seschrock 					if (nvlist_add_string(nv,
30842082Seschrock 					    ZPOOL_CONFIG_PATH, newpath) == 0)
30852082Seschrock 						verify(nvlist_lookup_string(nv,
30862082Seschrock 						    ZPOOL_CONFIG_PATH,
30872082Seschrock 						    &path) == 0);
30881354Seschrock 					free(newpath);
30891354Seschrock 				}
30901354Seschrock 			}
30911354Seschrock 
30922082Seschrock 			if (newdevid)
30932082Seschrock 				devid_str_free(newdevid);
30941354Seschrock 		}
30951354Seschrock 
30961354Seschrock 		if (strncmp(path, "/dev/dsk/", 9) == 0)
30971354Seschrock 			path += 9;
30981354Seschrock 
30991354Seschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
31001544Seschrock 		    &value) == 0 && value) {
310112383SJohn.Harres@Sun.COM 			int pathlen = strlen(path);
31022082Seschrock 			char *tmp = zfs_strdup(hdl, path);
310312383SJohn.Harres@Sun.COM 
310412383SJohn.Harres@Sun.COM 			/*
310512383SJohn.Harres@Sun.COM 			 * If it starts with c#, and ends with "s0", chop
310612383SJohn.Harres@Sun.COM 			 * the "s0" off, or if it ends with "s0/old", remove
310712383SJohn.Harres@Sun.COM 			 * the "s0" from the middle.
310812383SJohn.Harres@Sun.COM 			 */
310912383SJohn.Harres@Sun.COM 			if (CTD_CHECK(tmp)) {
311012383SJohn.Harres@Sun.COM 				if (strcmp(&tmp[pathlen - 2], "s0") == 0) {
311112383SJohn.Harres@Sun.COM 					tmp[pathlen - 2] = '\0';
311212383SJohn.Harres@Sun.COM 				} else if (pathlen > 6 &&
311312383SJohn.Harres@Sun.COM 				    strcmp(&tmp[pathlen - 6], "s0/old") == 0) {
311412383SJohn.Harres@Sun.COM 					(void) strcpy(&tmp[pathlen - 6],
311512383SJohn.Harres@Sun.COM 					    "/old");
311612383SJohn.Harres@Sun.COM 				}
311712383SJohn.Harres@Sun.COM 			}
31181354Seschrock 			return (tmp);
31191354Seschrock 		}
31201354Seschrock 	} else {
31211354Seschrock 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
31222082Seschrock 
31232082Seschrock 		/*
31242082Seschrock 		 * If it's a raidz device, we need to stick in the parity level.
31252082Seschrock 		 */
31262082Seschrock 		if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
31272082Seschrock 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
31282082Seschrock 			    &value) == 0);
31292082Seschrock 			(void) snprintf(buf, sizeof (buf), "%s%llu", path,
31302856Snd150628 			    (u_longlong_t)value);
31312082Seschrock 			path = buf;
31322082Seschrock 		}
313310594SGeorge.Wilson@Sun.COM 
313410594SGeorge.Wilson@Sun.COM 		/*
313510594SGeorge.Wilson@Sun.COM 		 * We identify each top-level vdev by using a <type-id>
313610594SGeorge.Wilson@Sun.COM 		 * naming convention.
313710594SGeorge.Wilson@Sun.COM 		 */
313810594SGeorge.Wilson@Sun.COM 		if (verbose) {
313910594SGeorge.Wilson@Sun.COM 			uint64_t id;
314010594SGeorge.Wilson@Sun.COM 
314110594SGeorge.Wilson@Sun.COM 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
314210594SGeorge.Wilson@Sun.COM 			    &id) == 0);
314310594SGeorge.Wilson@Sun.COM 			(void) snprintf(buf, sizeof (buf), "%s-%llu", path,
314410594SGeorge.Wilson@Sun.COM 			    (u_longlong_t)id);
314510594SGeorge.Wilson@Sun.COM 			path = buf;
314610594SGeorge.Wilson@Sun.COM 		}
31471354Seschrock 	}
31481354Seschrock 
31492082Seschrock 	return (zfs_strdup(hdl, path));
31501354Seschrock }
31511544Seschrock 
31521544Seschrock static int
31531544Seschrock zbookmark_compare(const void *a, const void *b)
31541544Seschrock {
31551544Seschrock 	return (memcmp(a, b, sizeof (zbookmark_t)));
31561544Seschrock }
31571544Seschrock 
31581544Seschrock /*
31591544Seschrock  * Retrieve the persistent error log, uniquify the members, and return to the
31601544Seschrock  * caller.
31611544Seschrock  */
31621544Seschrock int
31633444Sek110237 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
31641544Seschrock {
31651544Seschrock 	zfs_cmd_t zc = { 0 };
31661544Seschrock 	uint64_t count;
31672676Seschrock 	zbookmark_t *zb = NULL;
31683444Sek110237 	int i;
31691544Seschrock 
31701544Seschrock 	/*
31711544Seschrock 	 * Retrieve the raw error list from the kernel.  If the number of errors
31721544Seschrock 	 * has increased, allocate more space and continue until we get the
31731544Seschrock 	 * entire list.
31741544Seschrock 	 */
31751544Seschrock 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
31761544Seschrock 	    &count) == 0);
31774820Sek110237 	if (count == 0)
31784820Sek110237 		return (0);
31792676Seschrock 	if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
31802856Snd150628 	    count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
31812082Seschrock 		return (-1);
31822676Seschrock 	zc.zc_nvlist_dst_size = count;
31831544Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
31841544Seschrock 	for (;;) {
31852082Seschrock 		if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
31862082Seschrock 		    &zc) != 0) {
31872676Seschrock 			free((void *)(uintptr_t)zc.zc_nvlist_dst);
31881544Seschrock 			if (errno == ENOMEM) {
31893823Svb160487 				count = zc.zc_nvlist_dst_size;
31902676Seschrock 				if ((zc.zc_nvlist_dst = (uintptr_t)
31913823Svb160487 				    zfs_alloc(zhp->zpool_hdl, count *
31923823Svb160487 				    sizeof (zbookmark_t))) == (uintptr_t)NULL)
31932082Seschrock 					return (-1);
31941544Seschrock 			} else {
31951544Seschrock 				return (-1);
31961544Seschrock 			}
31971544Seschrock 		} else {
31981544Seschrock 			break;
31991544Seschrock 		}
32001544Seschrock 	}
32011544Seschrock 
32021544Seschrock 	/*
32031544Seschrock 	 * Sort the resulting bookmarks.  This is a little confusing due to the
32041544Seschrock 	 * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
32052676Seschrock 	 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
32061544Seschrock 	 * _not_ copied as part of the process.  So we point the start of our
32071544Seschrock 	 * array appropriate and decrement the total number of elements.
32081544Seschrock 	 */
32092676Seschrock 	zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
32102676Seschrock 	    zc.zc_nvlist_dst_size;
32112676Seschrock 	count -= zc.zc_nvlist_dst_size;
32121544Seschrock 
32131544Seschrock 	qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
32141544Seschrock 
32153444Sek110237 	verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
32161544Seschrock 
32171544Seschrock 	/*
32183444Sek110237 	 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
32191544Seschrock 	 */
32201544Seschrock 	for (i = 0; i < count; i++) {
32211544Seschrock 		nvlist_t *nv;
32221544Seschrock 
32233700Sek110237 		/* ignoring zb_blkid and zb_level for now */
32243700Sek110237 		if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
32253700Sek110237 		    zb[i-1].zb_object == zb[i].zb_object)
32261544Seschrock 			continue;
32271544Seschrock 
32283444Sek110237 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
32293444Sek110237 			goto nomem;
32303444Sek110237 		if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
32313444Sek110237 		    zb[i].zb_objset) != 0) {
32323444Sek110237 			nvlist_free(nv);
32332082Seschrock 			goto nomem;
32343444Sek110237 		}
32353444Sek110237 		if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
32363444Sek110237 		    zb[i].zb_object) != 0) {
32373444Sek110237 			nvlist_free(nv);
32383444Sek110237 			goto nomem;
32391544Seschrock 		}
32403444Sek110237 		if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
32413444Sek110237 			nvlist_free(nv);
32423444Sek110237 			goto nomem;
32433444Sek110237 		}
32443444Sek110237 		nvlist_free(nv);
32451544Seschrock 	}
32461544Seschrock 
32473265Sahrens 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
32481544Seschrock 	return (0);
32492082Seschrock 
32502082Seschrock nomem:
32512676Seschrock 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
32522082Seschrock 	return (no_memory(zhp->zpool_hdl));
32531544Seschrock }
32541760Seschrock 
32551760Seschrock /*
32561760Seschrock  * Upgrade a ZFS pool to the latest on-disk version.
32571760Seschrock  */
32581760Seschrock int
32595094Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
32601760Seschrock {
32611760Seschrock 	zfs_cmd_t zc = { 0 };
32622082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
32631760Seschrock 
32641760Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
32655094Slling 	zc.zc_cookie = new_version;
32665094Slling 
32674543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
32683237Slling 		return (zpool_standard_error_fmt(hdl, errno,
32692082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
32702082Seschrock 		    zhp->zpool_name));
32711760Seschrock 	return (0);
32721760Seschrock }
32732926Sek110237 
32744988Sek110237 void
32754988Sek110237 zpool_set_history_str(const char *subcommand, int argc, char **argv,
32764988Sek110237     char *history_str)
32774988Sek110237 {
32784988Sek110237 	int i;
32794988Sek110237 
32804988Sek110237 	(void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
32814988Sek110237 	for (i = 1; i < argc; i++) {
32824988Sek110237 		if (strlen(history_str) + 1 + strlen(argv[i]) >
32834988Sek110237 		    HIS_MAX_RECORD_LEN)
32844988Sek110237 			break;
32854988Sek110237 		(void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
32864988Sek110237 		(void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
32874988Sek110237 	}
32884988Sek110237 }
32894988Sek110237 
32902926Sek110237 /*
32914988Sek110237  * Stage command history for logging.
32922926Sek110237  */
32934988Sek110237 int
32944988Sek110237 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
32952926Sek110237 {
32964988Sek110237 	if (history_str == NULL)
32974988Sek110237 		return (EINVAL);
32984988Sek110237 
32994988Sek110237 	if (strlen(history_str) > HIS_MAX_RECORD_LEN)
33004988Sek110237 		return (EINVAL);
33012926Sek110237 
33024715Sek110237 	if (hdl->libzfs_log_str != NULL)
33034543Smarks 		free(hdl->libzfs_log_str);
33042926Sek110237 
33054988Sek110237 	if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
33064988Sek110237 		return (no_memory(hdl));
33074543Smarks 
33084988Sek110237 	return (0);
33092926Sek110237 }
33102926Sek110237 
33112926Sek110237 /*
33122926Sek110237  * Perform ioctl to get some command history of a pool.
33132926Sek110237  *
33142926Sek110237  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
33152926Sek110237  * logical offset of the history buffer to start reading from.
33162926Sek110237  *
33172926Sek110237  * Upon return, 'off' is the next logical offset to read from and
33182926Sek110237  * 'len' is the actual amount of bytes read into 'buf'.
33192926Sek110237  */
33202926Sek110237 static int
33212926Sek110237 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
33222926Sek110237 {
33232926Sek110237 	zfs_cmd_t zc = { 0 };
33242926Sek110237 	libzfs_handle_t *hdl = zhp->zpool_hdl;
33252926Sek110237 
33262926Sek110237 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
33272926Sek110237 
33282926Sek110237 	zc.zc_history = (uint64_t)(uintptr_t)buf;
33292926Sek110237 	zc.zc_history_len = *len;
33302926Sek110237 	zc.zc_history_offset = *off;
33312926Sek110237 
33322926Sek110237 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
33332926Sek110237 		switch (errno) {
33342926Sek110237 		case EPERM:
33353237Slling 			return (zfs_error_fmt(hdl, EZFS_PERM,
33363237Slling 			    dgettext(TEXT_DOMAIN,
33372926Sek110237 			    "cannot show history for pool '%s'"),
33382926Sek110237 			    zhp->zpool_name));
33392926Sek110237 		case ENOENT:
33403237Slling 			return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
33412926Sek110237 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
33422926Sek110237 			    "'%s'"), zhp->zpool_name));
33433863Sek110237 		case ENOTSUP:
33443863Sek110237 			return (zfs_error_fmt(hdl, EZFS_BADVERSION,
33453863Sek110237 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
33463863Sek110237 			    "'%s', pool must be upgraded"), zhp->zpool_name));
33472926Sek110237 		default:
33483237Slling 			return (zpool_standard_error_fmt(hdl, errno,
33492926Sek110237 			    dgettext(TEXT_DOMAIN,
33502926Sek110237 			    "cannot get history for '%s'"), zhp->zpool_name));
33512926Sek110237 		}
33522926Sek110237 	}
33532926Sek110237 
33542926Sek110237 	*len = zc.zc_history_len;
33552926Sek110237 	*off = zc.zc_history_offset;
33562926Sek110237 
33572926Sek110237 	return (0);
33582926Sek110237 }
33592926Sek110237 
33602926Sek110237 /*
33612926Sek110237  * Process the buffer of nvlists, unpacking and storing each nvlist record
33622926Sek110237  * into 'records'.  'leftover' is set to the number of bytes that weren't
33632926Sek110237  * processed as there wasn't a complete record.
33642926Sek110237  */
336510685SGeorge.Wilson@Sun.COM int
33662926Sek110237 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
33672926Sek110237     nvlist_t ***records, uint_t *numrecords)
33682926Sek110237 {
33692926Sek110237 	uint64_t reclen;
33702926Sek110237 	nvlist_t *nv;
33712926Sek110237 	int i;
33722926Sek110237 
33732926Sek110237 	while (bytes_read > sizeof (reclen)) {
33742926Sek110237 
33752926Sek110237 		/* get length of packed record (stored as little endian) */
33762926Sek110237 		for (i = 0, reclen = 0; i < sizeof (reclen); i++)
33772926Sek110237 			reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
33782926Sek110237 
33792926Sek110237 		if (bytes_read < sizeof (reclen) + reclen)
33802926Sek110237 			break;
33812926Sek110237 
33822926Sek110237 		/* unpack record */
33832926Sek110237 		if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
33842926Sek110237 			return (ENOMEM);
33852926Sek110237 		bytes_read -= sizeof (reclen) + reclen;
33862926Sek110237 		buf += sizeof (reclen) + reclen;
33872926Sek110237 
33882926Sek110237 		/* add record to nvlist array */
33892926Sek110237 		(*numrecords)++;
33902926Sek110237 		if (ISP2(*numrecords + 1)) {
33912926Sek110237 			*records = realloc(*records,
33922926Sek110237 			    *numrecords * 2 * sizeof (nvlist_t *));
33932926Sek110237 		}
33942926Sek110237 		(*records)[*numrecords - 1] = nv;
33952926Sek110237 	}
33962926Sek110237 
33972926Sek110237 	*leftover = bytes_read;
33982926Sek110237 	return (0);
33992926Sek110237 }
34002926Sek110237 
34012926Sek110237 #define	HIS_BUF_LEN	(128*1024)
34022926Sek110237 
34032926Sek110237 /*
34042926Sek110237  * Retrieve the command history of a pool.
34052926Sek110237  */
34062926Sek110237 int
34072926Sek110237 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
34082926Sek110237 {
34092926Sek110237 	char buf[HIS_BUF_LEN];
34102926Sek110237 	uint64_t off = 0;
34112926Sek110237 	nvlist_t **records = NULL;
34122926Sek110237 	uint_t numrecords = 0;
34132926Sek110237 	int err, i;
34142926Sek110237 
34152926Sek110237 	do {
34162926Sek110237 		uint64_t bytes_read = sizeof (buf);
34172926Sek110237 		uint64_t leftover;
34182926Sek110237 
34192926Sek110237 		if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
34202926Sek110237 			break;
34212926Sek110237 
34222926Sek110237 		/* if nothing else was read in, we're at EOF, just return */
34232926Sek110237 		if (!bytes_read)
34242926Sek110237 			break;
34252926Sek110237 
34262926Sek110237 		if ((err = zpool_history_unpack(buf, bytes_read,
34272926Sek110237 		    &leftover, &records, &numrecords)) != 0)
34282926Sek110237 			break;
34292926Sek110237 		off -= leftover;
34302926Sek110237 
34312926Sek110237 		/* CONSTCOND */
34322926Sek110237 	} while (1);
34332926Sek110237 
34342926Sek110237 	if (!err) {
34352926Sek110237 		verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
34362926Sek110237 		verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
34372926Sek110237 		    records, numrecords) == 0);
34382926Sek110237 	}
34392926Sek110237 	for (i = 0; i < numrecords; i++)
34402926Sek110237 		nvlist_free(records[i]);
34412926Sek110237 	free(records);
34422926Sek110237 
34432926Sek110237 	return (err);
34442926Sek110237 }
34453444Sek110237 
34463444Sek110237 void
34473444Sek110237 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
34483444Sek110237     char *pathname, size_t len)
34493444Sek110237 {
34503444Sek110237 	zfs_cmd_t zc = { 0 };
34513444Sek110237 	boolean_t mounted = B_FALSE;
34523444Sek110237 	char *mntpnt = NULL;
34533444Sek110237 	char dsname[MAXNAMELEN];
34543444Sek110237 
34553444Sek110237 	if (dsobj == 0) {
34563444Sek110237 		/* special case for the MOS */
34573444Sek110237 		(void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
34583444Sek110237 		return;
34593444Sek110237 	}
34603444Sek110237 
34613444Sek110237 	/* get the dataset's name */
34623444Sek110237 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
34633444Sek110237 	zc.zc_obj = dsobj;
34643444Sek110237 	if (ioctl(zhp->zpool_hdl->libzfs_fd,
34653444Sek110237 	    ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
34663444Sek110237 		/* just write out a path of two object numbers */
34673444Sek110237 		(void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
34683444Sek110237 		    dsobj, obj);
34693444Sek110237 		return;
34703444Sek110237 	}
34713444Sek110237 	(void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
34723444Sek110237 
34733444Sek110237 	/* find out if the dataset is mounted */
34743444Sek110237 	mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
34753444Sek110237 
34763444Sek110237 	/* get the corrupted object's path */
34773444Sek110237 	(void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
34783444Sek110237 	zc.zc_obj = obj;
34793444Sek110237 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
34803444Sek110237 	    &zc) == 0) {
34813444Sek110237 		if (mounted) {
34823444Sek110237 			(void) snprintf(pathname, len, "%s%s", mntpnt,
34833444Sek110237 			    zc.zc_value);
34843444Sek110237 		} else {
34853444Sek110237 			(void) snprintf(pathname, len, "%s:%s",
34863444Sek110237 			    dsname, zc.zc_value);
34873444Sek110237 		}
34883444Sek110237 	} else {
34893444Sek110237 		(void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
34903444Sek110237 	}
34913444Sek110237 	free(mntpnt);
34923444Sek110237 }
34933912Slling 
34944276Staylor /*
34957042Sgw25295  * Read the EFI label from the config, if a label does not exist then
34967042Sgw25295  * pass back the error to the caller. If the caller has passed a non-NULL
34977042Sgw25295  * diskaddr argument then we set it to the starting address of the EFI
34987042Sgw25295  * partition.
34997042Sgw25295  */
35007042Sgw25295 static int
35017042Sgw25295 read_efi_label(nvlist_t *config, diskaddr_t *sb)
35027042Sgw25295 {
35037042Sgw25295 	char *path;
35047042Sgw25295 	int fd;
35057042Sgw25295 	char diskname[MAXPATHLEN];
35067042Sgw25295 	int err = -1;
35077042Sgw25295 
35087042Sgw25295 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
35097042Sgw25295 		return (err);
35107042Sgw25295 
35117042Sgw25295 	(void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
35127042Sgw25295 	    strrchr(path, '/'));
35137042Sgw25295 	if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
35147042Sgw25295 		struct dk_gpt *vtoc;
35157042Sgw25295 
35167042Sgw25295 		if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
35177042Sgw25295 			if (sb != NULL)
35187042Sgw25295 				*sb = vtoc->efi_parts[0].p_start;
35197042Sgw25295 			efi_free(vtoc);
35207042Sgw25295 		}
35217042Sgw25295 		(void) close(fd);
35227042Sgw25295 	}
35237042Sgw25295 	return (err);
35247042Sgw25295 }
35257042Sgw25295 
35267042Sgw25295 /*
35274276Staylor  * determine where a partition starts on a disk in the current
35284276Staylor  * configuration
35294276Staylor  */
35304276Staylor static diskaddr_t
35314276Staylor find_start_block(nvlist_t *config)
35324276Staylor {
35334276Staylor 	nvlist_t **child;
35344276Staylor 	uint_t c, children;
35354276Staylor 	diskaddr_t sb = MAXOFFSET_T;
35364276Staylor 	uint64_t wholedisk;
35374276Staylor 
35384276Staylor 	if (nvlist_lookup_nvlist_array(config,
35394276Staylor 	    ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
35404276Staylor 		if (nvlist_lookup_uint64(config,
35414276Staylor 		    ZPOOL_CONFIG_WHOLE_DISK,
35424276Staylor 		    &wholedisk) != 0 || !wholedisk) {
35434276Staylor 			return (MAXOFFSET_T);
35444276Staylor 		}
35457042Sgw25295 		if (read_efi_label(config, &sb) < 0)
35467042Sgw25295 			sb = MAXOFFSET_T;
35474276Staylor 		return (sb);
35484276Staylor 	}
35494276Staylor 
35504276Staylor 	for (c = 0; c < children; c++) {
35514276Staylor 		sb = find_start_block(child[c]);
35524276Staylor 		if (sb != MAXOFFSET_T) {
35534276Staylor 			return (sb);
35544276Staylor 		}
35554276Staylor 	}
35564276Staylor 	return (MAXOFFSET_T);
35574276Staylor }
35584276Staylor 
35594276Staylor /*
35604276Staylor  * Label an individual disk.  The name provided is the short name,
35614276Staylor  * stripped of any leading /dev path.
35624276Staylor  */
35634276Staylor int
35644276Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
35654276Staylor {
35664276Staylor 	char path[MAXPATHLEN];
35674276Staylor 	struct dk_gpt *vtoc;
35684276Staylor 	int fd;
35694276Staylor 	size_t resv = EFI_MIN_RESV_SIZE;
35704276Staylor 	uint64_t slice_size;
35714276Staylor 	diskaddr_t start_block;
35724276Staylor 	char errbuf[1024];
35734276Staylor 
35746289Smmusante 	/* prepare an error message just in case */
35756289Smmusante 	(void) snprintf(errbuf, sizeof (errbuf),
35766289Smmusante 	    dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
35776289Smmusante 
35784276Staylor 	if (zhp) {
35794276Staylor 		nvlist_t *nvroot;
35804276Staylor 
35817965SGeorge.Wilson@Sun.COM 		if (pool_is_bootable(zhp)) {
35827965SGeorge.Wilson@Sun.COM 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35837965SGeorge.Wilson@Sun.COM 			    "EFI labeled devices are not supported on root "
35847965SGeorge.Wilson@Sun.COM 			    "pools."));
35857965SGeorge.Wilson@Sun.COM 			return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
35867965SGeorge.Wilson@Sun.COM 		}
35877965SGeorge.Wilson@Sun.COM 
35884276Staylor 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
35894276Staylor 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
35904276Staylor 
35914276Staylor 		if (zhp->zpool_start_block == 0)
35924276Staylor 			start_block = find_start_block(nvroot);
35934276Staylor 		else
35944276Staylor 			start_block = zhp->zpool_start_block;
35954276Staylor 		zhp->zpool_start_block = start_block;
35964276Staylor 	} else {
35974276Staylor 		/* new pool */
35984276Staylor 		start_block = NEW_START_BLOCK;
35994276Staylor 	}
36004276Staylor 
36014276Staylor 	(void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
36024276Staylor 	    BACKUP_SLICE);
36034276Staylor 
36044276Staylor 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
36054276Staylor 		/*
36064276Staylor 		 * This shouldn't happen.  We've long since verified that this
36074276Staylor 		 * is a valid device.
36084276Staylor 		 */
36096289Smmusante 		zfs_error_aux(hdl,
36106289Smmusante 		    dgettext(TEXT_DOMAIN, "unable to open device"));
36114276Staylor 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
36124276Staylor 	}
36134276Staylor 
36144276Staylor 	if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
36154276Staylor 		/*
36164276Staylor 		 * The only way this can fail is if we run out of memory, or we
36174276Staylor 		 * were unable to read the disk's capacity
36184276Staylor 		 */
36194276Staylor 		if (errno == ENOMEM)
36204276Staylor 			(void) no_memory(hdl);
36214276Staylor 
36224276Staylor 		(void) close(fd);
36236289Smmusante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
36246289Smmusante 		    "unable to read disk capacity"), name);
36254276Staylor 
36264276Staylor 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
36274276Staylor 	}
36284276Staylor 
36294276Staylor 	slice_size = vtoc->efi_last_u_lba + 1;
36304276Staylor 	slice_size -= EFI_MIN_RESV_SIZE;
36314276Staylor 	if (start_block == MAXOFFSET_T)
36324276Staylor 		start_block = NEW_START_BLOCK;
36334276Staylor 	slice_size -= start_block;
36344276Staylor 
36354276Staylor 	vtoc->efi_parts[0].p_start = start_block;
36364276Staylor 	vtoc->efi_parts[0].p_size = slice_size;
36374276Staylor 
36384276Staylor 	/*
36394276Staylor 	 * Why we use V_USR: V_BACKUP confuses users, and is considered
36404276Staylor 	 * disposable by some EFI utilities (since EFI doesn't have a backup
36414276Staylor 	 * slice).  V_UNASSIGNED is supposed to be used only for zero size
36424276Staylor 	 * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
36434276Staylor 	 * etc. were all pretty specific.  V_USR is as close to reality as we
36444276Staylor 	 * can get, in the absence of V_OTHER.
36454276Staylor 	 */
36464276Staylor 	vtoc->efi_parts[0].p_tag = V_USR;
36474276Staylor 	(void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
36484276Staylor 
36494276Staylor 	vtoc->efi_parts[8].p_start = slice_size + start_block;
36504276Staylor 	vtoc->efi_parts[8].p_size = resv;
36514276Staylor 	vtoc->efi_parts[8].p_tag = V_RESERVED;
36524276Staylor 
36534276Staylor 	if (efi_write(fd, vtoc) != 0) {
36544276Staylor 		/*
36554276Staylor 		 * Some block drivers (like pcata) may not support EFI
36564276Staylor 		 * GPT labels.  Print out a helpful error message dir-
36574276Staylor 		 * ecting the user to manually label the disk and give
36584276Staylor 		 * a specific slice.
36594276Staylor 		 */
36604276Staylor 		(void) close(fd);
36614276Staylor 		efi_free(vtoc);
36624276Staylor 
36634276Staylor 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
36646289Smmusante 		    "try using fdisk(1M) and then provide a specific slice"));
36654276Staylor 		return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
36664276Staylor 	}
36674276Staylor 
36684276Staylor 	(void) close(fd);
36694276Staylor 	efi_free(vtoc);
36704276Staylor 	return (0);
36714276Staylor }
36726423Sgw25295 
36736423Sgw25295 static boolean_t
36746423Sgw25295 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
36756423Sgw25295 {
36766423Sgw25295 	char *type;
36776423Sgw25295 	nvlist_t **child;
36786423Sgw25295 	uint_t children, c;
36796423Sgw25295 
36806423Sgw25295 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
36816423Sgw25295 	if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
36826423Sgw25295 	    strcmp(type, VDEV_TYPE_FILE) == 0 ||
36836423Sgw25295 	    strcmp(type, VDEV_TYPE_LOG) == 0 ||
368410594SGeorge.Wilson@Sun.COM 	    strcmp(type, VDEV_TYPE_HOLE) == 0 ||
36856423Sgw25295 	    strcmp(type, VDEV_TYPE_MISSING) == 0) {
36866423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
36876423Sgw25295 		    "vdev type '%s' is not supported"), type);
36886423Sgw25295 		(void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
36896423Sgw25295 		return (B_FALSE);
36906423Sgw25295 	}
36916423Sgw25295 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
36926423Sgw25295 	    &child, &children) == 0) {
36936423Sgw25295 		for (c = 0; c < children; c++) {
36946423Sgw25295 			if (!supported_dump_vdev_type(hdl, child[c], errbuf))
36956423Sgw25295 				return (B_FALSE);
36966423Sgw25295 		}
36976423Sgw25295 	}
36986423Sgw25295 	return (B_TRUE);
36996423Sgw25295 }
37006423Sgw25295 
37016423Sgw25295 /*
37026423Sgw25295  * check if this zvol is allowable for use as a dump device; zero if
37036423Sgw25295  * it is, > 0 if it isn't, < 0 if it isn't a zvol
37046423Sgw25295  */
37056423Sgw25295 int
37066423Sgw25295 zvol_check_dump_config(char *arg)
37076423Sgw25295 {
37086423Sgw25295 	zpool_handle_t *zhp = NULL;
37096423Sgw25295 	nvlist_t *config, *nvroot;
37106423Sgw25295 	char *p, *volname;
37116423Sgw25295 	nvlist_t **top;
37126423Sgw25295 	uint_t toplevels;
37136423Sgw25295 	libzfs_handle_t *hdl;
37146423Sgw25295 	char errbuf[1024];
37156423Sgw25295 	char poolname[ZPOOL_MAXNAMELEN];
37166423Sgw25295 	int pathlen = strlen(ZVOL_FULL_DEV_DIR);
37176423Sgw25295 	int ret = 1;
37186423Sgw25295 
37196423Sgw25295 	if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
37206423Sgw25295 		return (-1);
37216423Sgw25295 	}
37226423Sgw25295 
37236423Sgw25295 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
37246423Sgw25295 	    "dump is not supported on device '%s'"), arg);
37256423Sgw25295 
37266423Sgw25295 	if ((hdl = libzfs_init()) == NULL)
37276423Sgw25295 		return (1);
37286423Sgw25295 	libzfs_print_on_error(hdl, B_TRUE);
37296423Sgw25295 
37306423Sgw25295 	volname = arg + pathlen;
37316423Sgw25295 
37326423Sgw25295 	/* check the configuration of the pool */
37336423Sgw25295 	if ((p = strchr(volname, '/')) == NULL) {
37346423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37356423Sgw25295 		    "malformed dataset name"));
37366423Sgw25295 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
37376423Sgw25295 		return (1);
37386423Sgw25295 	} else if (p - volname >= ZFS_MAXNAMELEN) {
37396423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37406423Sgw25295 		    "dataset name is too long"));
37416423Sgw25295 		(void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
37426423Sgw25295 		return (1);
37436423Sgw25295 	} else {
37446423Sgw25295 		(void) strncpy(poolname, volname, p - volname);
37456423Sgw25295 		poolname[p - volname] = '\0';
37466423Sgw25295 	}
37476423Sgw25295 
37486423Sgw25295 	if ((zhp = zpool_open(hdl, poolname)) == NULL) {
37496423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37506423Sgw25295 		    "could not open pool '%s'"), poolname);
37516423Sgw25295 		(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
37526423Sgw25295 		goto out;
37536423Sgw25295 	}
37546423Sgw25295 	config = zpool_get_config(zhp, NULL);
37556423Sgw25295 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
37566423Sgw25295 	    &nvroot) != 0) {
37576423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37586423Sgw25295 		    "could not obtain vdev configuration for  '%s'"), poolname);
37596423Sgw25295 		(void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
37606423Sgw25295 		goto out;
37616423Sgw25295 	}
37626423Sgw25295 
37636423Sgw25295 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
37646423Sgw25295 	    &top, &toplevels) == 0);
37656423Sgw25295 	if (toplevels != 1) {
37666423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37676423Sgw25295 		    "'%s' has multiple top level vdevs"), poolname);
37686423Sgw25295 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
37696423Sgw25295 		goto out;
37706423Sgw25295 	}
37716423Sgw25295 
37726423Sgw25295 	if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
37736423Sgw25295 		goto out;
37746423Sgw25295 	}
37756423Sgw25295 	ret = 0;
37766423Sgw25295 
37776423Sgw25295 out:
37786423Sgw25295 	if (zhp)
37796423Sgw25295 		zpool_close(zhp);
37806423Sgw25295 	libzfs_fini(hdl);
37816423Sgw25295 	return (ret);
37826423Sgw25295 }
3783