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 &&
100013037SMark.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 
100513037SMark.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 
108813037SMark.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 
158013037SMark.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)) {
167213037SMark.Musante@Sun.COM 	case DATA_TYPE_UINT64:
16739816SGeorge.Wilson@Sun.COM 		if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
167413037SMark.Musante@Sun.COM 			uint64_t srchval, theguid;
167513037SMark.Musante@Sun.COM 
167613037SMark.Musante@Sun.COM 			verify(nvpair_value_uint64(pair, &srchval) == 0);
167713037SMark.Musante@Sun.COM 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
167813037SMark.Musante@Sun.COM 			    &theguid) == 0);
167913037SMark.Musante@Sun.COM 			if (theguid == srchval)
168013037SMark.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;
186413037SMark.Musante@Sun.COM 	*l2cache = B_FALSE;
1865*13044SMark.Musante@Sun.COM 	if (log != NULL)
1866*13044SMark.Musante@Sun.COM 		*log = B_FALSE;
18679816SGeorge.Wilson@Sun.COM 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
18689816SGeorge.Wilson@Sun.COM 	nvlist_free(search);
18699816SGeorge.Wilson@Sun.COM 
18709816SGeorge.Wilson@Sun.COM 	return (ret);
18719816SGeorge.Wilson@Sun.COM }
18729816SGeorge.Wilson@Sun.COM 
187310594SGeorge.Wilson@Sun.COM /*
187410594SGeorge.Wilson@Sun.COM  * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
187510594SGeorge.Wilson@Sun.COM  */
187610594SGeorge.Wilson@Sun.COM boolean_t
187710594SGeorge.Wilson@Sun.COM zpool_vdev_is_interior(const char *name)
187810594SGeorge.Wilson@Sun.COM {
187910594SGeorge.Wilson@Sun.COM 	if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
188010594SGeorge.Wilson@Sun.COM 	    strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
188110594SGeorge.Wilson@Sun.COM 		return (B_TRUE);
188210594SGeorge.Wilson@Sun.COM 	return (B_FALSE);
188310594SGeorge.Wilson@Sun.COM }
188410594SGeorge.Wilson@Sun.COM 
18852082Seschrock nvlist_t *
18865450Sbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
18877326SEric.Schrock@Sun.COM     boolean_t *l2cache, boolean_t *log)
18881544Seschrock {
18891544Seschrock 	char buf[MAXPATHLEN];
18901544Seschrock 	char *end;
18919816SGeorge.Wilson@Sun.COM 	nvlist_t *nvroot, *search, *ret;
18921544Seschrock 	uint64_t guid;
18931544Seschrock 
18949816SGeorge.Wilson@Sun.COM 	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
18959816SGeorge.Wilson@Sun.COM 
18961613Seschrock 	guid = strtoull(path, &end, 10);
18971544Seschrock 	if (guid != 0 && *end == '\0') {
18989816SGeorge.Wilson@Sun.COM 		verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
189910594SGeorge.Wilson@Sun.COM 	} else if (zpool_vdev_is_interior(path)) {
190010594SGeorge.Wilson@Sun.COM 		verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
19011544Seschrock 	} else if (path[0] != '/') {
19021544Seschrock 		(void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path);
19039816SGeorge.Wilson@Sun.COM 		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
19041544Seschrock 	} else {
19059816SGeorge.Wilson@Sun.COM 		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
19061544Seschrock 	}
19071544Seschrock 
19081544Seschrock 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
19091544Seschrock 	    &nvroot) == 0);
19101544Seschrock 
19112468Sek110237 	*avail_spare = B_FALSE;
19125450Sbrendan 	*l2cache = B_FALSE;
19137326SEric.Schrock@Sun.COM 	if (log != NULL)
19147326SEric.Schrock@Sun.COM 		*log = B_FALSE;
19159816SGeorge.Wilson@Sun.COM 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
19169816SGeorge.Wilson@Sun.COM 	nvlist_free(search);
19179816SGeorge.Wilson@Sun.COM 
19189816SGeorge.Wilson@Sun.COM 	return (ret);
19192468Sek110237 }
19202468Sek110237 
19217656SSherry.Moore@Sun.COM static int
19227656SSherry.Moore@Sun.COM vdev_online(nvlist_t *nv)
19237656SSherry.Moore@Sun.COM {
19247656SSherry.Moore@Sun.COM 	uint64_t ival;
19257656SSherry.Moore@Sun.COM 
19267656SSherry.Moore@Sun.COM 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
19277656SSherry.Moore@Sun.COM 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
19287656SSherry.Moore@Sun.COM 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
19297656SSherry.Moore@Sun.COM 		return (0);
19307656SSherry.Moore@Sun.COM 
19317656SSherry.Moore@Sun.COM 	return (1);
19327656SSherry.Moore@Sun.COM }
19337656SSherry.Moore@Sun.COM 
19347656SSherry.Moore@Sun.COM /*
19359790SLin.Ling@Sun.COM  * Helper function for zpool_get_physpaths().
19367656SSherry.Moore@Sun.COM  */
19379160SSherry.Moore@Sun.COM static int
19389790SLin.Ling@Sun.COM vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
19399160SSherry.Moore@Sun.COM     size_t *bytes_written)
19407656SSherry.Moore@Sun.COM {
19419160SSherry.Moore@Sun.COM 	size_t bytes_left, pos, rsz;
19429160SSherry.Moore@Sun.COM 	char *tmppath;
19439160SSherry.Moore@Sun.COM 	const char *format;
19449160SSherry.Moore@Sun.COM 
19459160SSherry.Moore@Sun.COM 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
19469160SSherry.Moore@Sun.COM 	    &tmppath) != 0)
19479160SSherry.Moore@Sun.COM 		return (EZFS_NODEVICE);
19489160SSherry.Moore@Sun.COM 
19499160SSherry.Moore@Sun.COM 	pos = *bytes_written;
19509160SSherry.Moore@Sun.COM 	bytes_left = physpath_size - pos;
19519160SSherry.Moore@Sun.COM 	format = (pos == 0) ? "%s" : " %s";
19529160SSherry.Moore@Sun.COM 
19539160SSherry.Moore@Sun.COM 	rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
19549160SSherry.Moore@Sun.COM 	*bytes_written += rsz;
19559160SSherry.Moore@Sun.COM 
19569160SSherry.Moore@Sun.COM 	if (rsz >= bytes_left) {
19579160SSherry.Moore@Sun.COM 		/* if physpath was not copied properly, clear it */
19589160SSherry.Moore@Sun.COM 		if (bytes_left != 0) {
19599160SSherry.Moore@Sun.COM 			physpath[pos] = 0;
19609160SSherry.Moore@Sun.COM 		}
19619160SSherry.Moore@Sun.COM 		return (EZFS_NOSPC);
19629160SSherry.Moore@Sun.COM 	}
19639160SSherry.Moore@Sun.COM 	return (0);
19649160SSherry.Moore@Sun.COM }
19659160SSherry.Moore@Sun.COM 
19669790SLin.Ling@Sun.COM static int
19679790SLin.Ling@Sun.COM vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
19689790SLin.Ling@Sun.COM     size_t *rsz, boolean_t is_spare)
19699790SLin.Ling@Sun.COM {
19709790SLin.Ling@Sun.COM 	char *type;
19719790SLin.Ling@Sun.COM 	int ret;
19729790SLin.Ling@Sun.COM 
19739790SLin.Ling@Sun.COM 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
19749790SLin.Ling@Sun.COM 		return (EZFS_INVALCONFIG);
19759790SLin.Ling@Sun.COM 
19769790SLin.Ling@Sun.COM 	if (strcmp(type, VDEV_TYPE_DISK) == 0) {
19779790SLin.Ling@Sun.COM 		/*
19789790SLin.Ling@Sun.COM 		 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
19799790SLin.Ling@Sun.COM 		 * For a spare vdev, we only want to boot from the active
19809790SLin.Ling@Sun.COM 		 * spare device.
19819790SLin.Ling@Sun.COM 		 */
19829790SLin.Ling@Sun.COM 		if (is_spare) {
19839790SLin.Ling@Sun.COM 			uint64_t spare = 0;
19849790SLin.Ling@Sun.COM 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
19859790SLin.Ling@Sun.COM 			    &spare);
19869790SLin.Ling@Sun.COM 			if (!spare)
19879790SLin.Ling@Sun.COM 				return (EZFS_INVALCONFIG);
19889790SLin.Ling@Sun.COM 		}
19899790SLin.Ling@Sun.COM 
19909790SLin.Ling@Sun.COM 		if (vdev_online(nv)) {
19919790SLin.Ling@Sun.COM 			if ((ret = vdev_get_one_physpath(nv, physpath,
19929790SLin.Ling@Sun.COM 			    phypath_size, rsz)) != 0)
19939790SLin.Ling@Sun.COM 				return (ret);
19949790SLin.Ling@Sun.COM 		}
19959790SLin.Ling@Sun.COM 	} else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
19969790SLin.Ling@Sun.COM 	    strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
19979790SLin.Ling@Sun.COM 	    (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
19989790SLin.Ling@Sun.COM 		nvlist_t **child;
19999790SLin.Ling@Sun.COM 		uint_t count;
20009790SLin.Ling@Sun.COM 		int i, ret;
20019790SLin.Ling@Sun.COM 
20029790SLin.Ling@Sun.COM 		if (nvlist_lookup_nvlist_array(nv,
20039790SLin.Ling@Sun.COM 		    ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
20049790SLin.Ling@Sun.COM 			return (EZFS_INVALCONFIG);
20059790SLin.Ling@Sun.COM 
20069790SLin.Ling@Sun.COM 		for (i = 0; i < count; i++) {
20079790SLin.Ling@Sun.COM 			ret = vdev_get_physpaths(child[i], physpath,
20089790SLin.Ling@Sun.COM 			    phypath_size, rsz, is_spare);
20099790SLin.Ling@Sun.COM 			if (ret == EZFS_NOSPC)
20109790SLin.Ling@Sun.COM 				return (ret);
20119790SLin.Ling@Sun.COM 		}
20129790SLin.Ling@Sun.COM 	}
20139790SLin.Ling@Sun.COM 
20149790SLin.Ling@Sun.COM 	return (EZFS_POOL_INVALARG);
20159790SLin.Ling@Sun.COM }
20169790SLin.Ling@Sun.COM 
20179160SSherry.Moore@Sun.COM /*
20189160SSherry.Moore@Sun.COM  * Get phys_path for a root pool config.
20199160SSherry.Moore@Sun.COM  * Return 0 on success; non-zero on failure.
20209160SSherry.Moore@Sun.COM  */
20219160SSherry.Moore@Sun.COM static int
20229160SSherry.Moore@Sun.COM zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
20239160SSherry.Moore@Sun.COM {
20249160SSherry.Moore@Sun.COM 	size_t rsz;
20257656SSherry.Moore@Sun.COM 	nvlist_t *vdev_root;
20267656SSherry.Moore@Sun.COM 	nvlist_t **child;
20277656SSherry.Moore@Sun.COM 	uint_t count;
20289160SSherry.Moore@Sun.COM 	char *type;
20299160SSherry.Moore@Sun.COM 
20309160SSherry.Moore@Sun.COM 	rsz = 0;
20319160SSherry.Moore@Sun.COM 
20329160SSherry.Moore@Sun.COM 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
20339160SSherry.Moore@Sun.COM 	    &vdev_root) != 0)
20349160SSherry.Moore@Sun.COM 		return (EZFS_INVALCONFIG);
20359160SSherry.Moore@Sun.COM 
20369160SSherry.Moore@Sun.COM 	if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
20379160SSherry.Moore@Sun.COM 	    nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
20389160SSherry.Moore@Sun.COM 	    &child, &count) != 0)
20399160SSherry.Moore@Sun.COM 		return (EZFS_INVALCONFIG);
20407656SSherry.Moore@Sun.COM 
20417656SSherry.Moore@Sun.COM 	/*
20429160SSherry.Moore@Sun.COM 	 * root pool can not have EFI labeled disks and can only have
20439160SSherry.Moore@Sun.COM 	 * a single top-level vdev.
20447656SSherry.Moore@Sun.COM 	 */
20459160SSherry.Moore@Sun.COM 	if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
20469160SSherry.Moore@Sun.COM 	    pool_uses_efi(vdev_root))
20479160SSherry.Moore@Sun.COM 		return (EZFS_POOL_INVALARG);
20489160SSherry.Moore@Sun.COM 
20499790SLin.Ling@Sun.COM 	(void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
20509790SLin.Ling@Sun.COM 	    B_FALSE);
20517656SSherry.Moore@Sun.COM 
20529160SSherry.Moore@Sun.COM 	/* No online devices */
20539160SSherry.Moore@Sun.COM 	if (rsz == 0)
20549160SSherry.Moore@Sun.COM 		return (EZFS_NODEVICE);
20559160SSherry.Moore@Sun.COM 
20567656SSherry.Moore@Sun.COM 	return (0);
20577656SSherry.Moore@Sun.COM }
20587656SSherry.Moore@Sun.COM 
20592468Sek110237 /*
20609160SSherry.Moore@Sun.COM  * Get phys_path for a root pool
20619160SSherry.Moore@Sun.COM  * Return 0 on success; non-zero on failure.
20629160SSherry.Moore@Sun.COM  */
20639160SSherry.Moore@Sun.COM int
20649160SSherry.Moore@Sun.COM zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
20659160SSherry.Moore@Sun.COM {
20669160SSherry.Moore@Sun.COM 	return (zpool_get_config_physpath(zhp->zpool_config, physpath,
20679160SSherry.Moore@Sun.COM 	    phypath_size));
20689160SSherry.Moore@Sun.COM }
20699160SSherry.Moore@Sun.COM 
20709160SSherry.Moore@Sun.COM /*
20719816SGeorge.Wilson@Sun.COM  * If the device has being dynamically expanded then we need to relabel
20729816SGeorge.Wilson@Sun.COM  * the disk to use the new unallocated space.
20739816SGeorge.Wilson@Sun.COM  */
20749816SGeorge.Wilson@Sun.COM static int
20759816SGeorge.Wilson@Sun.COM zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
20769816SGeorge.Wilson@Sun.COM {
20779816SGeorge.Wilson@Sun.COM 	char path[MAXPATHLEN];
20789816SGeorge.Wilson@Sun.COM 	char errbuf[1024];
20799816SGeorge.Wilson@Sun.COM 	int fd, error;
20809816SGeorge.Wilson@Sun.COM 	int (*_efi_use_whole_disk)(int);
20819816SGeorge.Wilson@Sun.COM 
20829816SGeorge.Wilson@Sun.COM 	if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
20839816SGeorge.Wilson@Sun.COM 	    "efi_use_whole_disk")) == NULL)
20849816SGeorge.Wilson@Sun.COM 		return (-1);
20859816SGeorge.Wilson@Sun.COM 
20869816SGeorge.Wilson@Sun.COM 	(void) snprintf(path, sizeof (path), "%s/%s", RDISK_ROOT, name);
20879816SGeorge.Wilson@Sun.COM 
20889816SGeorge.Wilson@Sun.COM 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
20899816SGeorge.Wilson@Sun.COM 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
20909816SGeorge.Wilson@Sun.COM 		    "relabel '%s': unable to open device"), name);
20919816SGeorge.Wilson@Sun.COM 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
20929816SGeorge.Wilson@Sun.COM 	}
20939816SGeorge.Wilson@Sun.COM 
20949816SGeorge.Wilson@Sun.COM 	/*
20959816SGeorge.Wilson@Sun.COM 	 * It's possible that we might encounter an error if the device
20969816SGeorge.Wilson@Sun.COM 	 * does not have any unallocated space left. If so, we simply
20979816SGeorge.Wilson@Sun.COM 	 * ignore that error and continue on.
20989816SGeorge.Wilson@Sun.COM 	 */
20999816SGeorge.Wilson@Sun.COM 	error = _efi_use_whole_disk(fd);
21009816SGeorge.Wilson@Sun.COM 	(void) close(fd);
21019816SGeorge.Wilson@Sun.COM 	if (error && error != VT_ENOSPC) {
21029816SGeorge.Wilson@Sun.COM 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
21039816SGeorge.Wilson@Sun.COM 		    "relabel '%s': unable to read disk capacity"), name);
21049816SGeorge.Wilson@Sun.COM 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
21059816SGeorge.Wilson@Sun.COM 	}
21069816SGeorge.Wilson@Sun.COM 	return (0);
21079816SGeorge.Wilson@Sun.COM }
21089816SGeorge.Wilson@Sun.COM 
21099816SGeorge.Wilson@Sun.COM /*
21104451Seschrock  * Bring the specified vdev online.   The 'flags' parameter is a set of the
21114451Seschrock  * ZFS_ONLINE_* flags.
2112789Sahrens  */
2113789Sahrens int
21144451Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
21154451Seschrock     vdev_state_t *newstate)
2116789Sahrens {
2117789Sahrens 	zfs_cmd_t zc = { 0 };
2118789Sahrens 	char msg[1024];
21192082Seschrock 	nvlist_t *tgt;
21209816SGeorge.Wilson@Sun.COM 	boolean_t avail_spare, l2cache, islog;
21212082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2122789Sahrens 
21239816SGeorge.Wilson@Sun.COM 	if (flags & ZFS_ONLINE_EXPAND) {
21249816SGeorge.Wilson@Sun.COM 		(void) snprintf(msg, sizeof (msg),
21259816SGeorge.Wilson@Sun.COM 		    dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
21269816SGeorge.Wilson@Sun.COM 	} else {
21279816SGeorge.Wilson@Sun.COM 		(void) snprintf(msg, sizeof (msg),
21289816SGeorge.Wilson@Sun.COM 		    dgettext(TEXT_DOMAIN, "cannot online %s"), path);
21299816SGeorge.Wilson@Sun.COM 	}
2130789Sahrens 
21311544Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
21327326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
21339816SGeorge.Wilson@Sun.COM 	    &islog)) == NULL)
21342082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2135789Sahrens 
21362468Sek110237 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
21372468Sek110237 
213810817SEric.Schrock@Sun.COM 	if (avail_spare)
21392082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
21402082Seschrock 
21419816SGeorge.Wilson@Sun.COM 	if (flags & ZFS_ONLINE_EXPAND ||
21429816SGeorge.Wilson@Sun.COM 	    zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
21439816SGeorge.Wilson@Sun.COM 		char *pathname = NULL;
21449816SGeorge.Wilson@Sun.COM 		uint64_t wholedisk = 0;
21459816SGeorge.Wilson@Sun.COM 
21469816SGeorge.Wilson@Sun.COM 		(void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
21479816SGeorge.Wilson@Sun.COM 		    &wholedisk);
21489816SGeorge.Wilson@Sun.COM 		verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
21499816SGeorge.Wilson@Sun.COM 		    &pathname) == 0);
21509816SGeorge.Wilson@Sun.COM 
21519816SGeorge.Wilson@Sun.COM 		/*
21529816SGeorge.Wilson@Sun.COM 		 * XXX - L2ARC 1.0 devices can't support expansion.
21539816SGeorge.Wilson@Sun.COM 		 */
21549816SGeorge.Wilson@Sun.COM 		if (l2cache) {
21559816SGeorge.Wilson@Sun.COM 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
21569816SGeorge.Wilson@Sun.COM 			    "cannot expand cache devices"));
21579816SGeorge.Wilson@Sun.COM 			return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
21589816SGeorge.Wilson@Sun.COM 		}
21599816SGeorge.Wilson@Sun.COM 
21609816SGeorge.Wilson@Sun.COM 		if (wholedisk) {
21619816SGeorge.Wilson@Sun.COM 			pathname += strlen(DISK_ROOT) + 1;
216213037SMark.Musante@Sun.COM 			(void) zpool_relabel_disk(hdl, pathname);
21639816SGeorge.Wilson@Sun.COM 		}
21649816SGeorge.Wilson@Sun.COM 	}
21659816SGeorge.Wilson@Sun.COM 
21664451Seschrock 	zc.zc_cookie = VDEV_STATE_ONLINE;
21674451Seschrock 	zc.zc_obj = flags;
21684451Seschrock 
216913037SMark.Musante@Sun.COM 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
217011422SMark.Musante@Sun.COM 		if (errno == EINVAL) {
217111422SMark.Musante@Sun.COM 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
217211422SMark.Musante@Sun.COM 			    "from this pool into a new one.  Use '%s' "
217311422SMark.Musante@Sun.COM 			    "instead"), "zpool detach");
217411422SMark.Musante@Sun.COM 			return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
217511422SMark.Musante@Sun.COM 		}
21764451Seschrock 		return (zpool_standard_error(hdl, errno, msg));
217711422SMark.Musante@Sun.COM 	}
21784451Seschrock 
21794451Seschrock 	*newstate = zc.zc_cookie;
21804451Seschrock 	return (0);
2181789Sahrens }
2182789Sahrens 
2183789Sahrens /*
2184789Sahrens  * Take the specified vdev offline
2185789Sahrens  */
2186789Sahrens int
21874451Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2188789Sahrens {
2189789Sahrens 	zfs_cmd_t zc = { 0 };
2190789Sahrens 	char msg[1024];
21912082Seschrock 	nvlist_t *tgt;
21925450Sbrendan 	boolean_t avail_spare, l2cache;
21932082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2194789Sahrens 
21951544Seschrock 	(void) snprintf(msg, sizeof (msg),
21961544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
21971544Seschrock 
2198789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
21997326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
22007326SEric.Schrock@Sun.COM 	    NULL)) == NULL)
22012082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
22022082Seschrock 
22032468Sek110237 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
22042468Sek110237 
220510817SEric.Schrock@Sun.COM 	if (avail_spare)
22062082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
22072082Seschrock 
22084451Seschrock 	zc.zc_cookie = VDEV_STATE_OFFLINE;
22094451Seschrock 	zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
22101485Slling 
221113037SMark.Musante@Sun.COM 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2212789Sahrens 		return (0);
2213789Sahrens 
2214789Sahrens 	switch (errno) {
22152082Seschrock 	case EBUSY:
2216789Sahrens 
2217789Sahrens 		/*
2218789Sahrens 		 * There are no other replicas of this device.
2219789Sahrens 		 */
22202082Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
22212082Seschrock 
22229701SGeorge.Wilson@Sun.COM 	case EEXIST:
22239701SGeorge.Wilson@Sun.COM 		/*
22249701SGeorge.Wilson@Sun.COM 		 * The log device has unplayed logs
22259701SGeorge.Wilson@Sun.COM 		 */
22269701SGeorge.Wilson@Sun.COM 		return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
22279701SGeorge.Wilson@Sun.COM 
22282082Seschrock 	default:
22292082Seschrock 		return (zpool_standard_error(hdl, errno, msg));
22302082Seschrock 	}
22312082Seschrock }
2232789Sahrens 
22332082Seschrock /*
22344451Seschrock  * Mark the given vdev faulted.
22354451Seschrock  */
22364451Seschrock int
223710817SEric.Schrock@Sun.COM zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
22384451Seschrock {
22394451Seschrock 	zfs_cmd_t zc = { 0 };
22404451Seschrock 	char msg[1024];
22414451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
22424451Seschrock 
22434451Seschrock 	(void) snprintf(msg, sizeof (msg),
22444451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
22454451Seschrock 
22464451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
22474451Seschrock 	zc.zc_guid = guid;
22484451Seschrock 	zc.zc_cookie = VDEV_STATE_FAULTED;
224910817SEric.Schrock@Sun.COM 	zc.zc_obj = aux;
22504451Seschrock 
225113037SMark.Musante@Sun.COM 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
22524451Seschrock 		return (0);
22534451Seschrock 
22544451Seschrock 	switch (errno) {
22554451Seschrock 	case EBUSY:
22564451Seschrock 
22574451Seschrock 		/*
22584451Seschrock 		 * There are no other replicas of this device.
22594451Seschrock 		 */
22604451Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
22614451Seschrock 
22624451Seschrock 	default:
22634451Seschrock 		return (zpool_standard_error(hdl, errno, msg));
22644451Seschrock 	}
22654451Seschrock 
22664451Seschrock }
22674451Seschrock 
22684451Seschrock /*
22694451Seschrock  * Mark the given vdev degraded.
22704451Seschrock  */
22714451Seschrock int
227210817SEric.Schrock@Sun.COM zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
22734451Seschrock {
22744451Seschrock 	zfs_cmd_t zc = { 0 };
22754451Seschrock 	char msg[1024];
22764451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
22774451Seschrock 
22784451Seschrock 	(void) snprintf(msg, sizeof (msg),
22794451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
22804451Seschrock 
22814451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
22824451Seschrock 	zc.zc_guid = guid;
22834451Seschrock 	zc.zc_cookie = VDEV_STATE_DEGRADED;
228410817SEric.Schrock@Sun.COM 	zc.zc_obj = aux;
22854451Seschrock 
228613037SMark.Musante@Sun.COM 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
22874451Seschrock 		return (0);
22884451Seschrock 
22894451Seschrock 	return (zpool_standard_error(hdl, errno, msg));
22904451Seschrock }
22914451Seschrock 
22924451Seschrock /*
22932082Seschrock  * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
22942082Seschrock  * a hot spare.
22952082Seschrock  */
22962082Seschrock static boolean_t
22972082Seschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
22982082Seschrock {
22992082Seschrock 	nvlist_t **child;
23002082Seschrock 	uint_t c, children;
23012082Seschrock 	char *type;
23022082Seschrock 
23032082Seschrock 	if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
23042082Seschrock 	    &children) == 0) {
23052082Seschrock 		verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
23062082Seschrock 		    &type) == 0);
23072082Seschrock 
23082082Seschrock 		if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
23092082Seschrock 		    children == 2 && child[which] == tgt)
23102082Seschrock 			return (B_TRUE);
23112082Seschrock 
23122082Seschrock 		for (c = 0; c < children; c++)
23132082Seschrock 			if (is_replacing_spare(child[c], tgt, which))
23142082Seschrock 				return (B_TRUE);
2315789Sahrens 	}
23162082Seschrock 
23172082Seschrock 	return (B_FALSE);
2318789Sahrens }
2319789Sahrens 
2320789Sahrens /*
2321789Sahrens  * Attach new_disk (fully described by nvroot) to old_disk.
23224527Sperrin  * If 'replacing' is specified, the new disk will replace the old one.
2323789Sahrens  */
2324789Sahrens int
2325789Sahrens zpool_vdev_attach(zpool_handle_t *zhp,
2326789Sahrens     const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2327789Sahrens {
2328789Sahrens 	zfs_cmd_t zc = { 0 };
2329789Sahrens 	char msg[1024];
2330789Sahrens 	int ret;
23312082Seschrock 	nvlist_t *tgt;
23327326SEric.Schrock@Sun.COM 	boolean_t avail_spare, l2cache, islog;
23337326SEric.Schrock@Sun.COM 	uint64_t val;
233413037SMark.Musante@Sun.COM 	char *newname;
23352082Seschrock 	nvlist_t **child;
23362082Seschrock 	uint_t children;
23372082Seschrock 	nvlist_t *config_root;
23382082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
23397965SGeorge.Wilson@Sun.COM 	boolean_t rootpool = pool_is_bootable(zhp);
2340789Sahrens 
23411544Seschrock 	if (replacing)
23421544Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
23431544Seschrock 		    "cannot replace %s with %s"), old_disk, new_disk);
23441544Seschrock 	else
23451544Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
23461544Seschrock 		    "cannot attach %s to %s"), new_disk, old_disk);
23471544Seschrock 
23487965SGeorge.Wilson@Sun.COM 	/*
23497965SGeorge.Wilson@Sun.COM 	 * If this is a root pool, make sure that we're not attaching an
23507965SGeorge.Wilson@Sun.COM 	 * EFI labeled device.
23517965SGeorge.Wilson@Sun.COM 	 */
23527965SGeorge.Wilson@Sun.COM 	if (rootpool && pool_uses_efi(nvroot)) {
23537965SGeorge.Wilson@Sun.COM 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
23547965SGeorge.Wilson@Sun.COM 		    "EFI labeled devices are not supported on root pools."));
23557965SGeorge.Wilson@Sun.COM 		return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
23567965SGeorge.Wilson@Sun.COM 	}
23577965SGeorge.Wilson@Sun.COM 
2358789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
23597326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
23607326SEric.Schrock@Sun.COM 	    &islog)) == 0)
23612082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
23622082Seschrock 
23632468Sek110237 	if (avail_spare)
23642082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
23652082Seschrock 
23665450Sbrendan 	if (l2cache)
23675450Sbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
23685450Sbrendan 
23692082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
23702082Seschrock 	zc.zc_cookie = replacing;
23712082Seschrock 
23722082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
23732082Seschrock 	    &child, &children) != 0 || children != 1) {
23742082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
23752082Seschrock 		    "new device must be a single disk"));
23762082Seschrock 		return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
23771544Seschrock 	}
23782082Seschrock 
23792082Seschrock 	verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
23802082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
23812082Seschrock 
238210594SGeorge.Wilson@Sun.COM 	if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
23837041Seschrock 		return (-1);
23847041Seschrock 
23852082Seschrock 	/*
23862082Seschrock 	 * If the target is a hot spare that has been swapped in, we can only
23872082Seschrock 	 * replace it with another hot spare.
23882082Seschrock 	 */
23892082Seschrock 	if (replacing &&
23902082Seschrock 	    nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
23917326SEric.Schrock@Sun.COM 	    (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
23927326SEric.Schrock@Sun.COM 	    NULL) == NULL || !avail_spare) &&
23937326SEric.Schrock@Sun.COM 	    is_replacing_spare(config_root, tgt, 1)) {
23942082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
23952082Seschrock 		    "can only be replaced by another hot spare"));
23967041Seschrock 		free(newname);
23972082Seschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
23982082Seschrock 	}
23992082Seschrock 
24007041Seschrock 	free(newname);
24017041Seschrock 
24025094Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
24032082Seschrock 		return (-1);
2404789Sahrens 
240513037SMark.Musante@Sun.COM 	ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2406789Sahrens 
24072676Seschrock 	zcmd_free_nvlists(&zc);
2408789Sahrens 
24097965SGeorge.Wilson@Sun.COM 	if (ret == 0) {
24107965SGeorge.Wilson@Sun.COM 		if (rootpool) {
24117965SGeorge.Wilson@Sun.COM 			/*
24129790SLin.Ling@Sun.COM 			 * XXX need a better way to prevent user from
24139790SLin.Ling@Sun.COM 			 * booting up a half-baked vdev.
24149790SLin.Ling@Sun.COM 			 */
24159790SLin.Ling@Sun.COM 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
24169790SLin.Ling@Sun.COM 			    "sure to wait until resilver is done "
24179790SLin.Ling@Sun.COM 			    "before rebooting.\n"));
24187965SGeorge.Wilson@Sun.COM 		}
2419789Sahrens 		return (0);
24207965SGeorge.Wilson@Sun.COM 	}
2421789Sahrens 
2422789Sahrens 	switch (errno) {
24231544Seschrock 	case ENOTSUP:
2424789Sahrens 		/*
2425789Sahrens 		 * Can't attach to or replace this type of vdev.
2426789Sahrens 		 */
24274527Sperrin 		if (replacing) {
242813037SMark.Musante@Sun.COM 			uint64_t version = zpool_get_prop_int(zhp,
242913037SMark.Musante@Sun.COM 			    ZPOOL_PROP_VERSION, NULL);
243013037SMark.Musante@Sun.COM 
24317326SEric.Schrock@Sun.COM 			if (islog)
24324527Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
24334527Sperrin 				    "cannot replace a log with a spare"));
243413037SMark.Musante@Sun.COM 			else if (version >= SPA_VERSION_MULTI_REPLACE)
243513037SMark.Musante@Sun.COM 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
243613037SMark.Musante@Sun.COM 				    "already in replacing/spare config; wait "
243713037SMark.Musante@Sun.COM 				    "for completion or use 'zpool detach'"));
24384527Sperrin 			else
24394527Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
24404527Sperrin 				    "cannot replace a replacing device"));
24414527Sperrin 		} else {
24422082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
24432082Seschrock 			    "can only attach to mirrors and top-level "
24442082Seschrock 			    "disks"));
24454527Sperrin 		}
24462082Seschrock 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
2447789Sahrens 		break;
2448789Sahrens 
24491544Seschrock 	case EINVAL:
2450789Sahrens 		/*
2451789Sahrens 		 * The new device must be a single disk.
2452789Sahrens 		 */
24532082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
24542082Seschrock 		    "new device must be a single disk"));
24552082Seschrock 		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2456789Sahrens 		break;
2457789Sahrens 
24581544Seschrock 	case EBUSY:
24592082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
24602082Seschrock 		    new_disk);
24612082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2462789Sahrens 		break;
2463789Sahrens 
24641544Seschrock 	case EOVERFLOW:
2465789Sahrens 		/*
2466789Sahrens 		 * The new device is too small.
2467789Sahrens 		 */
24682082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
24692082Seschrock 		    "device is too small"));
24702082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2471789Sahrens 		break;
2472789Sahrens 
24731544Seschrock 	case EDOM:
2474789Sahrens 		/*
2475789Sahrens 		 * The new device has a different alignment requirement.
2476789Sahrens 		 */
24772082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
24782082Seschrock 		    "devices have different sector alignment"));
24792082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2480789Sahrens 		break;
2481789Sahrens 
24821544Seschrock 	case ENAMETOOLONG:
2483789Sahrens 		/*
2484789Sahrens 		 * The resulting top-level vdev spec won't fit in the label.
2485789Sahrens 		 */
24862082Seschrock 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2487789Sahrens 		break;
2488789Sahrens 
24891544Seschrock 	default:
24902082Seschrock 		(void) zpool_standard_error(hdl, errno, msg);
2491789Sahrens 	}
2492789Sahrens 
24932082Seschrock 	return (-1);
2494789Sahrens }
2495789Sahrens 
2496789Sahrens /*
2497789Sahrens  * Detach the specified device.
2498789Sahrens  */
2499789Sahrens int
2500789Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2501789Sahrens {
2502789Sahrens 	zfs_cmd_t zc = { 0 };
2503789Sahrens 	char msg[1024];
25042082Seschrock 	nvlist_t *tgt;
25055450Sbrendan 	boolean_t avail_spare, l2cache;
25062082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2507789Sahrens 
25081544Seschrock 	(void) snprintf(msg, sizeof (msg),
25091544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
25101544Seschrock 
2511789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
25127326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
25137326SEric.Schrock@Sun.COM 	    NULL)) == 0)
25142082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2515789Sahrens 
25162468Sek110237 	if (avail_spare)
25172082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
25182082Seschrock 
25195450Sbrendan 	if (l2cache)
25205450Sbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
25215450Sbrendan 
25222082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
25232082Seschrock 
25244543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2525789Sahrens 		return (0);
2526789Sahrens 
2527789Sahrens 	switch (errno) {
2528789Sahrens 
25291544Seschrock 	case ENOTSUP:
2530789Sahrens 		/*
2531789Sahrens 		 * Can't detach from this type of vdev.
2532789Sahrens 		 */
25332082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
25342082Seschrock 		    "applicable to mirror and replacing vdevs"));
253513037SMark.Musante@Sun.COM 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
2536789Sahrens 		break;
2537789Sahrens 
25381544Seschrock 	case EBUSY:
2539789Sahrens 		/*
2540789Sahrens 		 * There are no other replicas of this device.
2541789Sahrens 		 */
25422082Seschrock 		(void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2543789Sahrens 		break;
2544789Sahrens 
25451544Seschrock 	default:
25462082Seschrock 		(void) zpool_standard_error(hdl, errno, msg);
25471544Seschrock 	}
25481544Seschrock 
25492082Seschrock 	return (-1);
25502082Seschrock }
25512082Seschrock 
25522082Seschrock /*
255311422SMark.Musante@Sun.COM  * Find a mirror vdev in the source nvlist.
255411422SMark.Musante@Sun.COM  *
255511422SMark.Musante@Sun.COM  * The mchild array contains a list of disks in one of the top-level mirrors
255611422SMark.Musante@Sun.COM  * of the source pool.  The schild array contains a list of disks that the
255711422SMark.Musante@Sun.COM  * user specified on the command line.  We loop over the mchild array to
255811422SMark.Musante@Sun.COM  * see if any entry in the schild array matches.
255911422SMark.Musante@Sun.COM  *
256011422SMark.Musante@Sun.COM  * If a disk in the mchild array is found in the schild array, we return
256111422SMark.Musante@Sun.COM  * the index of that entry.  Otherwise we return -1.
256211422SMark.Musante@Sun.COM  */
256311422SMark.Musante@Sun.COM static int
256411422SMark.Musante@Sun.COM find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
256511422SMark.Musante@Sun.COM     nvlist_t **schild, uint_t schildren)
256611422SMark.Musante@Sun.COM {
256711422SMark.Musante@Sun.COM 	uint_t mc;
256811422SMark.Musante@Sun.COM 
256911422SMark.Musante@Sun.COM 	for (mc = 0; mc < mchildren; mc++) {
257011422SMark.Musante@Sun.COM 		uint_t sc;
257111422SMark.Musante@Sun.COM 		char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
257211422SMark.Musante@Sun.COM 		    mchild[mc], B_FALSE);
257311422SMark.Musante@Sun.COM 
257411422SMark.Musante@Sun.COM 		for (sc = 0; sc < schildren; sc++) {
257511422SMark.Musante@Sun.COM 			char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
257611422SMark.Musante@Sun.COM 			    schild[sc], B_FALSE);
257711422SMark.Musante@Sun.COM 			boolean_t result = (strcmp(mpath, spath) == 0);
257811422SMark.Musante@Sun.COM 
257911422SMark.Musante@Sun.COM 			free(spath);
258011422SMark.Musante@Sun.COM 			if (result) {
258111422SMark.Musante@Sun.COM 				free(mpath);
258211422SMark.Musante@Sun.COM 				return (mc);
258311422SMark.Musante@Sun.COM 			}
258411422SMark.Musante@Sun.COM 		}
258511422SMark.Musante@Sun.COM 
258611422SMark.Musante@Sun.COM 		free(mpath);
258711422SMark.Musante@Sun.COM 	}
258811422SMark.Musante@Sun.COM 
258911422SMark.Musante@Sun.COM 	return (-1);
259011422SMark.Musante@Sun.COM }
259111422SMark.Musante@Sun.COM 
259211422SMark.Musante@Sun.COM /*
259311422SMark.Musante@Sun.COM  * Split a mirror pool.  If newroot points to null, then a new nvlist
259411422SMark.Musante@Sun.COM  * is generated and it is the responsibility of the caller to free it.
259511422SMark.Musante@Sun.COM  */
259611422SMark.Musante@Sun.COM int
259711422SMark.Musante@Sun.COM zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
259811422SMark.Musante@Sun.COM     nvlist_t *props, splitflags_t flags)
259911422SMark.Musante@Sun.COM {
260011422SMark.Musante@Sun.COM 	zfs_cmd_t zc = { 0 };
260111422SMark.Musante@Sun.COM 	char msg[1024];
260211422SMark.Musante@Sun.COM 	nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
260311422SMark.Musante@Sun.COM 	nvlist_t **varray = NULL, *zc_props = NULL;
260411422SMark.Musante@Sun.COM 	uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
260511422SMark.Musante@Sun.COM 	libzfs_handle_t *hdl = zhp->zpool_hdl;
260611422SMark.Musante@Sun.COM 	uint64_t vers;
260711422SMark.Musante@Sun.COM 	boolean_t freelist = B_FALSE, memory_err = B_TRUE;
260811422SMark.Musante@Sun.COM 	int retval = 0;
260911422SMark.Musante@Sun.COM 
261011422SMark.Musante@Sun.COM 	(void) snprintf(msg, sizeof (msg),
261111422SMark.Musante@Sun.COM 	    dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
261211422SMark.Musante@Sun.COM 
261311422SMark.Musante@Sun.COM 	if (!zpool_name_valid(hdl, B_FALSE, newname))
261411422SMark.Musante@Sun.COM 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
261511422SMark.Musante@Sun.COM 
261611422SMark.Musante@Sun.COM 	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
261711422SMark.Musante@Sun.COM 		(void) fprintf(stderr, gettext("Internal error: unable to "
261811422SMark.Musante@Sun.COM 		    "retrieve pool configuration\n"));
261911422SMark.Musante@Sun.COM 		return (-1);
262011422SMark.Musante@Sun.COM 	}
262111422SMark.Musante@Sun.COM 
262211422SMark.Musante@Sun.COM 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
262311422SMark.Musante@Sun.COM 	    == 0);
262411422SMark.Musante@Sun.COM 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
262511422SMark.Musante@Sun.COM 
262611422SMark.Musante@Sun.COM 	if (props) {
262711422SMark.Musante@Sun.COM 		if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
262811422SMark.Musante@Sun.COM 		    props, vers, B_TRUE, msg)) == NULL)
262911422SMark.Musante@Sun.COM 			return (-1);
263011422SMark.Musante@Sun.COM 	}
263111422SMark.Musante@Sun.COM 
263211422SMark.Musante@Sun.COM 	if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
263311422SMark.Musante@Sun.COM 	    &children) != 0) {
263411422SMark.Musante@Sun.COM 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
263511422SMark.Musante@Sun.COM 		    "Source pool is missing vdev tree"));
263611422SMark.Musante@Sun.COM 		if (zc_props)
263711422SMark.Musante@Sun.COM 			nvlist_free(zc_props);
263811422SMark.Musante@Sun.COM 		return (-1);
263911422SMark.Musante@Sun.COM 	}
264011422SMark.Musante@Sun.COM 
264111422SMark.Musante@Sun.COM 	varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
264211422SMark.Musante@Sun.COM 	vcount = 0;
264311422SMark.Musante@Sun.COM 
264411422SMark.Musante@Sun.COM 	if (*newroot == NULL ||
264511422SMark.Musante@Sun.COM 	    nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
264611422SMark.Musante@Sun.COM 	    &newchild, &newchildren) != 0)
264711422SMark.Musante@Sun.COM 		newchildren = 0;
264811422SMark.Musante@Sun.COM 
264911422SMark.Musante@Sun.COM 	for (c = 0; c < children; c++) {
265011422SMark.Musante@Sun.COM 		uint64_t is_log = B_FALSE, is_hole = B_FALSE;
265111422SMark.Musante@Sun.COM 		char *type;
265211422SMark.Musante@Sun.COM 		nvlist_t **mchild, *vdev;
265311422SMark.Musante@Sun.COM 		uint_t mchildren;
265411422SMark.Musante@Sun.COM 		int entry;
265511422SMark.Musante@Sun.COM 
265611422SMark.Musante@Sun.COM 		/*
265711422SMark.Musante@Sun.COM 		 * Unlike cache & spares, slogs are stored in the
265811422SMark.Musante@Sun.COM 		 * ZPOOL_CONFIG_CHILDREN array.  We filter them out here.
265911422SMark.Musante@Sun.COM 		 */
266011422SMark.Musante@Sun.COM 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
266111422SMark.Musante@Sun.COM 		    &is_log);
266211422SMark.Musante@Sun.COM 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
266311422SMark.Musante@Sun.COM 		    &is_hole);
266411422SMark.Musante@Sun.COM 		if (is_log || is_hole) {
266511422SMark.Musante@Sun.COM 			/*
266611422SMark.Musante@Sun.COM 			 * Create a hole vdev and put it in the config.
266711422SMark.Musante@Sun.COM 			 */
266811422SMark.Musante@Sun.COM 			if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
266911422SMark.Musante@Sun.COM 				goto out;
267011422SMark.Musante@Sun.COM 			if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
267111422SMark.Musante@Sun.COM 			    VDEV_TYPE_HOLE) != 0)
267211422SMark.Musante@Sun.COM 				goto out;
267311422SMark.Musante@Sun.COM 			if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
267411422SMark.Musante@Sun.COM 			    1) != 0)
267511422SMark.Musante@Sun.COM 				goto out;
267611422SMark.Musante@Sun.COM 			if (lastlog == 0)
267711422SMark.Musante@Sun.COM 				lastlog = vcount;
267811422SMark.Musante@Sun.COM 			varray[vcount++] = vdev;
267911422SMark.Musante@Sun.COM 			continue;
268011422SMark.Musante@Sun.COM 		}
268111422SMark.Musante@Sun.COM 		lastlog = 0;
268211422SMark.Musante@Sun.COM 		verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
268311422SMark.Musante@Sun.COM 		    == 0);
268411422SMark.Musante@Sun.COM 		if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
268511422SMark.Musante@Sun.COM 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
268611422SMark.Musante@Sun.COM 			    "Source pool must be composed only of mirrors\n"));
268711422SMark.Musante@Sun.COM 			retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
268811422SMark.Musante@Sun.COM 			goto out;
268911422SMark.Musante@Sun.COM 		}
269011422SMark.Musante@Sun.COM 
269111422SMark.Musante@Sun.COM 		verify(nvlist_lookup_nvlist_array(child[c],
269211422SMark.Musante@Sun.COM 		    ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
269311422SMark.Musante@Sun.COM 
269411422SMark.Musante@Sun.COM 		/* find or add an entry for this top-level vdev */
269511422SMark.Musante@Sun.COM 		if (newchildren > 0 &&
269611422SMark.Musante@Sun.COM 		    (entry = find_vdev_entry(zhp, mchild, mchildren,
269711422SMark.Musante@Sun.COM 		    newchild, newchildren)) >= 0) {
269811422SMark.Musante@Sun.COM 			/* We found a disk that the user specified. */
269911422SMark.Musante@Sun.COM 			vdev = mchild[entry];
270011422SMark.Musante@Sun.COM 			++found;
270111422SMark.Musante@Sun.COM 		} else {
270211422SMark.Musante@Sun.COM 			/* User didn't specify a disk for this vdev. */
270311422SMark.Musante@Sun.COM 			vdev = mchild[mchildren - 1];
270411422SMark.Musante@Sun.COM 		}
270511422SMark.Musante@Sun.COM 
270611422SMark.Musante@Sun.COM 		if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
270711422SMark.Musante@Sun.COM 			goto out;
270811422SMark.Musante@Sun.COM 	}
270911422SMark.Musante@Sun.COM 
271011422SMark.Musante@Sun.COM 	/* did we find every disk the user specified? */
271111422SMark.Musante@Sun.COM 	if (found != newchildren) {
271211422SMark.Musante@Sun.COM 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
271311422SMark.Musante@Sun.COM 		    "include at most one disk from each mirror"));
271411422SMark.Musante@Sun.COM 		retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
271511422SMark.Musante@Sun.COM 		goto out;
271611422SMark.Musante@Sun.COM 	}
271711422SMark.Musante@Sun.COM 
271811422SMark.Musante@Sun.COM 	/* Prepare the nvlist for populating. */
271911422SMark.Musante@Sun.COM 	if (*newroot == NULL) {
272011422SMark.Musante@Sun.COM 		if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
272111422SMark.Musante@Sun.COM 			goto out;
272211422SMark.Musante@Sun.COM 		freelist = B_TRUE;
272311422SMark.Musante@Sun.COM 		if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
272411422SMark.Musante@Sun.COM 		    VDEV_TYPE_ROOT) != 0)
272511422SMark.Musante@Sun.COM 			goto out;
272611422SMark.Musante@Sun.COM 	} else {
272711422SMark.Musante@Sun.COM 		verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
272811422SMark.Musante@Sun.COM 	}
272911422SMark.Musante@Sun.COM 
273011422SMark.Musante@Sun.COM 	/* Add all the children we found */
273111422SMark.Musante@Sun.COM 	if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
273211422SMark.Musante@Sun.COM 	    lastlog == 0 ? vcount : lastlog) != 0)
273311422SMark.Musante@Sun.COM 		goto out;
273411422SMark.Musante@Sun.COM 
273511422SMark.Musante@Sun.COM 	/*
273611422SMark.Musante@Sun.COM 	 * If we're just doing a dry run, exit now with success.
273711422SMark.Musante@Sun.COM 	 */
273811422SMark.Musante@Sun.COM 	if (flags.dryrun) {
273911422SMark.Musante@Sun.COM 		memory_err = B_FALSE;
274011422SMark.Musante@Sun.COM 		freelist = B_FALSE;
274111422SMark.Musante@Sun.COM 		goto out;
274211422SMark.Musante@Sun.COM 	}
274311422SMark.Musante@Sun.COM 
274411422SMark.Musante@Sun.COM 	/* now build up the config list & call the ioctl */
274511422SMark.Musante@Sun.COM 	if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
274611422SMark.Musante@Sun.COM 		goto out;
274711422SMark.Musante@Sun.COM 
274811422SMark.Musante@Sun.COM 	if (nvlist_add_nvlist(newconfig,
274911422SMark.Musante@Sun.COM 	    ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
275011422SMark.Musante@Sun.COM 	    nvlist_add_string(newconfig,
275111422SMark.Musante@Sun.COM 	    ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
275211422SMark.Musante@Sun.COM 	    nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
275311422SMark.Musante@Sun.COM 		goto out;
275411422SMark.Musante@Sun.COM 
275511422SMark.Musante@Sun.COM 	/*
275611422SMark.Musante@Sun.COM 	 * The new pool is automatically part of the namespace unless we
275711422SMark.Musante@Sun.COM 	 * explicitly export it.
275811422SMark.Musante@Sun.COM 	 */
275911422SMark.Musante@Sun.COM 	if (!flags.import)
276011422SMark.Musante@Sun.COM 		zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
276111422SMark.Musante@Sun.COM 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
276211422SMark.Musante@Sun.COM 	(void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
276311422SMark.Musante@Sun.COM 	if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
276411422SMark.Musante@Sun.COM 		goto out;
276511422SMark.Musante@Sun.COM 	if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
276611422SMark.Musante@Sun.COM 		goto out;
276711422SMark.Musante@Sun.COM 
276811422SMark.Musante@Sun.COM 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
276911422SMark.Musante@Sun.COM 		retval = zpool_standard_error(hdl, errno, msg);
277011422SMark.Musante@Sun.COM 		goto out;
277111422SMark.Musante@Sun.COM 	}
277211422SMark.Musante@Sun.COM 
277311422SMark.Musante@Sun.COM 	freelist = B_FALSE;
277411422SMark.Musante@Sun.COM 	memory_err = B_FALSE;
277511422SMark.Musante@Sun.COM 
277611422SMark.Musante@Sun.COM out:
277711422SMark.Musante@Sun.COM 	if (varray != NULL) {
277811422SMark.Musante@Sun.COM 		int v;
277911422SMark.Musante@Sun.COM 
278011422SMark.Musante@Sun.COM 		for (v = 0; v < vcount; v++)
278111422SMark.Musante@Sun.COM 			nvlist_free(varray[v]);
278211422SMark.Musante@Sun.COM 		free(varray);
278311422SMark.Musante@Sun.COM 	}
278411422SMark.Musante@Sun.COM 	zcmd_free_nvlists(&zc);
278511422SMark.Musante@Sun.COM 	if (zc_props)
278611422SMark.Musante@Sun.COM 		nvlist_free(zc_props);
278711422SMark.Musante@Sun.COM 	if (newconfig)
278811422SMark.Musante@Sun.COM 		nvlist_free(newconfig);
278911422SMark.Musante@Sun.COM 	if (freelist) {
279011422SMark.Musante@Sun.COM 		nvlist_free(*newroot);
279111422SMark.Musante@Sun.COM 		*newroot = NULL;
279211422SMark.Musante@Sun.COM 	}
279311422SMark.Musante@Sun.COM 
279411422SMark.Musante@Sun.COM 	if (retval != 0)
279511422SMark.Musante@Sun.COM 		return (retval);
279611422SMark.Musante@Sun.COM 
279711422SMark.Musante@Sun.COM 	if (memory_err)
279811422SMark.Musante@Sun.COM 		return (no_memory(hdl));
279911422SMark.Musante@Sun.COM 
280011422SMark.Musante@Sun.COM 	return (0);
280111422SMark.Musante@Sun.COM }
280211422SMark.Musante@Sun.COM 
280311422SMark.Musante@Sun.COM /*
28045450Sbrendan  * Remove the given device.  Currently, this is supported only for hot spares
28055450Sbrendan  * and level 2 cache devices.
28062082Seschrock  */
28072082Seschrock int
28082082Seschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
28092082Seschrock {
28102082Seschrock 	zfs_cmd_t zc = { 0 };
28112082Seschrock 	char msg[1024];
28122082Seschrock 	nvlist_t *tgt;
281310594SGeorge.Wilson@Sun.COM 	boolean_t avail_spare, l2cache, islog;
28142082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
281510594SGeorge.Wilson@Sun.COM 	uint64_t version;
28162082Seschrock 
28172082Seschrock 	(void) snprintf(msg, sizeof (msg),
28182082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
28192082Seschrock 
28202082Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
28217326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
282210594SGeorge.Wilson@Sun.COM 	    &islog)) == 0)
28232082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
282410594SGeorge.Wilson@Sun.COM 	/*
282510594SGeorge.Wilson@Sun.COM 	 * XXX - this should just go away.
282610594SGeorge.Wilson@Sun.COM 	 */
282710594SGeorge.Wilson@Sun.COM 	if (!avail_spare && !l2cache && !islog) {
28282082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
282910594SGeorge.Wilson@Sun.COM 		    "only inactive hot spares, cache, top-level, "
283010594SGeorge.Wilson@Sun.COM 		    "or log devices can be removed"));
28312082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
28322082Seschrock 	}
28332082Seschrock 
283410594SGeorge.Wilson@Sun.COM 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
283510594SGeorge.Wilson@Sun.COM 	if (islog && version < SPA_VERSION_HOLES) {
283610594SGeorge.Wilson@Sun.COM 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
283710594SGeorge.Wilson@Sun.COM 		    "pool must be upgrade to support log removal"));
283810594SGeorge.Wilson@Sun.COM 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
283910594SGeorge.Wilson@Sun.COM 	}
284010594SGeorge.Wilson@Sun.COM 
28412082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
28422082Seschrock 
28434543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
28442082Seschrock 		return (0);
28452082Seschrock 
28462082Seschrock 	return (zpool_standard_error(hdl, errno, msg));
28471544Seschrock }
28481544Seschrock 
28491544Seschrock /*
28501544Seschrock  * Clear the errors for the pool, or the particular device if specified.
28511544Seschrock  */
28521544Seschrock int
285310921STim.Haley@Sun.COM zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
28541544Seschrock {
28551544Seschrock 	zfs_cmd_t zc = { 0 };
28561544Seschrock 	char msg[1024];
28572082Seschrock 	nvlist_t *tgt;
285810921STim.Haley@Sun.COM 	zpool_rewind_policy_t policy;
28595450Sbrendan 	boolean_t avail_spare, l2cache;
28602082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
286110921STim.Haley@Sun.COM 	nvlist_t *nvi = NULL;
286212949SGeorge.Wilson@Sun.COM 	int error;
28631544Seschrock 
28641544Seschrock 	if (path)
28651544Seschrock 		(void) snprintf(msg, sizeof (msg),
28661544Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
28672676Seschrock 		    path);
28681544Seschrock 	else
28691544Seschrock 		(void) snprintf(msg, sizeof (msg),
28701544Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
28711544Seschrock 		    zhp->zpool_name);
28721544Seschrock 
28731544Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
28742082Seschrock 	if (path) {
28755450Sbrendan 		if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
28767326SEric.Schrock@Sun.COM 		    &l2cache, NULL)) == 0)
28772082Seschrock 			return (zfs_error(hdl, EZFS_NODEVICE, msg));
28782082Seschrock 
28795450Sbrendan 		/*
28805450Sbrendan 		 * Don't allow error clearing for hot spares.  Do allow
28815450Sbrendan 		 * error clearing for l2cache devices.
28825450Sbrendan 		 */
28832468Sek110237 		if (avail_spare)
28842082Seschrock 			return (zfs_error(hdl, EZFS_ISSPARE, msg));
28852082Seschrock 
28862082Seschrock 		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
28872082Seschrock 		    &zc.zc_guid) == 0);
28881544Seschrock 	}
28891544Seschrock 
289010921STim.Haley@Sun.COM 	zpool_get_rewind_policy(rewindnvl, &policy);
289110921STim.Haley@Sun.COM 	zc.zc_cookie = policy.zrp_request;
289210921STim.Haley@Sun.COM 
289312974SGeorge.Wilson@Sun.COM 	if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
289410921STim.Haley@Sun.COM 		return (-1);
289510921STim.Haley@Sun.COM 
289613037SMark.Musante@Sun.COM 	if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
289710921STim.Haley@Sun.COM 		return (-1);
289810921STim.Haley@Sun.COM 
289912949SGeorge.Wilson@Sun.COM 	while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
290012949SGeorge.Wilson@Sun.COM 	    errno == ENOMEM) {
290112949SGeorge.Wilson@Sun.COM 		if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
290212949SGeorge.Wilson@Sun.COM 			zcmd_free_nvlists(&zc);
290312949SGeorge.Wilson@Sun.COM 			return (-1);
290412949SGeorge.Wilson@Sun.COM 		}
290512949SGeorge.Wilson@Sun.COM 	}
290612949SGeorge.Wilson@Sun.COM 
290712949SGeorge.Wilson@Sun.COM 	if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
290810921STim.Haley@Sun.COM 	    errno != EPERM && errno != EACCES)) {
290910921STim.Haley@Sun.COM 		if (policy.zrp_request &
291010921STim.Haley@Sun.COM 		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
291110921STim.Haley@Sun.COM 			(void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
291210921STim.Haley@Sun.COM 			zpool_rewind_exclaim(hdl, zc.zc_name,
291310921STim.Haley@Sun.COM 			    ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
291410921STim.Haley@Sun.COM 			    nvi);
291510921STim.Haley@Sun.COM 			nvlist_free(nvi);
291610921STim.Haley@Sun.COM 		}
291710921STim.Haley@Sun.COM 		zcmd_free_nvlists(&zc);
29181544Seschrock 		return (0);
291910921STim.Haley@Sun.COM 	}
292010921STim.Haley@Sun.COM 
292110921STim.Haley@Sun.COM 	zcmd_free_nvlists(&zc);
29222082Seschrock 	return (zpool_standard_error(hdl, errno, msg));
2923789Sahrens }
2924789Sahrens 
29253126Sahl /*
29264451Seschrock  * Similar to zpool_clear(), but takes a GUID (used by fmd).
29274451Seschrock  */
29284451Seschrock int
29294451Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
29304451Seschrock {
29314451Seschrock 	zfs_cmd_t zc = { 0 };
29324451Seschrock 	char msg[1024];
29334451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
29344451Seschrock 
29354451Seschrock 	(void) snprintf(msg, sizeof (msg),
29364451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
29374451Seschrock 	    guid);
29384451Seschrock 
29394451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
29404451Seschrock 	zc.zc_guid = guid;
294112441SVictor.Latushkin@Sun.COM 	zc.zc_cookie = ZPOOL_NO_REWIND;
29424451Seschrock 
29434451Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
29444451Seschrock 		return (0);
29454451Seschrock 
29464451Seschrock 	return (zpool_standard_error(hdl, errno, msg));
29474451Seschrock }
29484451Seschrock 
29494451Seschrock /*
29501354Seschrock  * Convert from a devid string to a path.
29511354Seschrock  */
29521354Seschrock static char *
29531354Seschrock devid_to_path(char *devid_str)
29541354Seschrock {
29551354Seschrock 	ddi_devid_t devid;
29561354Seschrock 	char *minor;
29571354Seschrock 	char *path;
29581354Seschrock 	devid_nmlist_t *list = NULL;
29591354Seschrock 	int ret;
29601354Seschrock 
29611354Seschrock 	if (devid_str_decode(devid_str, &devid, &minor) != 0)
29621354Seschrock 		return (NULL);
29631354Seschrock 
29641354Seschrock 	ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
29651354Seschrock 
29661354Seschrock 	devid_str_free(minor);
29671354Seschrock 	devid_free(devid);
29681354Seschrock 
29691354Seschrock 	if (ret != 0)
29701354Seschrock 		return (NULL);
29711354Seschrock 
29722082Seschrock 	if ((path = strdup(list[0].devname)) == NULL)
29732082Seschrock 		return (NULL);
29742082Seschrock 
29751354Seschrock 	devid_free_nmlist(list);
29761354Seschrock 
29771354Seschrock 	return (path);
29781354Seschrock }
29791354Seschrock 
29801354Seschrock /*
29811354Seschrock  * Convert from a path to a devid string.
29821354Seschrock  */
29831354Seschrock static char *
29841354Seschrock path_to_devid(const char *path)
29851354Seschrock {
29861354Seschrock 	int fd;
29871354Seschrock 	ddi_devid_t devid;
29881354Seschrock 	char *minor, *ret;
29891354Seschrock 
29901354Seschrock 	if ((fd = open(path, O_RDONLY)) < 0)
29911354Seschrock 		return (NULL);
29921354Seschrock 
29931354Seschrock 	minor = NULL;
29941354Seschrock 	ret = NULL;
29951354Seschrock 	if (devid_get(fd, &devid) == 0) {
29961354Seschrock 		if (devid_get_minor_name(fd, &minor) == 0)
29971354Seschrock 			ret = devid_str_encode(devid, minor);
29981354Seschrock 		if (minor != NULL)
29991354Seschrock 			devid_str_free(minor);
30001354Seschrock 		devid_free(devid);
30011354Seschrock 	}
30021354Seschrock 	(void) close(fd);
30031354Seschrock 
30041354Seschrock 	return (ret);
30051354Seschrock }
30061354Seschrock 
30071354Seschrock /*
30081354Seschrock  * Issue the necessary ioctl() to update the stored path value for the vdev.  We
30091354Seschrock  * ignore any failure here, since a common case is for an unprivileged user to
30101354Seschrock  * type 'zpool status', and we'll display the correct information anyway.
30111354Seschrock  */
30121354Seschrock static void
30131354Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
30141354Seschrock {
30151354Seschrock 	zfs_cmd_t zc = { 0 };
30161354Seschrock 
30171354Seschrock 	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
30182676Seschrock 	(void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
30191354Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
30201544Seschrock 	    &zc.zc_guid) == 0);
30211354Seschrock 
30222082Seschrock 	(void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
30231354Seschrock }
30241354Seschrock 
30251354Seschrock /*
30261354Seschrock  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
30271354Seschrock  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
30281354Seschrock  * We also check if this is a whole disk, in which case we strip off the
30291354Seschrock  * trailing 's0' slice name.
30301354Seschrock  *
30311354Seschrock  * This routine is also responsible for identifying when disks have been
30321354Seschrock  * reconfigured in a new location.  The kernel will have opened the device by
30331354Seschrock  * devid, but the path will still refer to the old location.  To catch this, we
30341354Seschrock  * first do a path -> devid translation (which is fast for the common case).  If
30351354Seschrock  * the devid matches, we're done.  If not, we do a reverse devid -> path
30361354Seschrock  * translation and issue the appropriate ioctl() to update the path of the vdev.
30371354Seschrock  * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
30381354Seschrock  * of these checks.
30391354Seschrock  */
30401354Seschrock char *
304110594SGeorge.Wilson@Sun.COM zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
304210594SGeorge.Wilson@Sun.COM     boolean_t verbose)
30431354Seschrock {
30441354Seschrock 	char *path, *devid;
30451544Seschrock 	uint64_t value;
30461544Seschrock 	char buf[64];
30474451Seschrock 	vdev_stat_t *vs;
30484451Seschrock 	uint_t vsc;
30491354Seschrock 
30501544Seschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
30511544Seschrock 	    &value) == 0) {
30521544Seschrock 		verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
30531544Seschrock 		    &value) == 0);
30542856Snd150628 		(void) snprintf(buf, sizeof (buf), "%llu",
30552856Snd150628 		    (u_longlong_t)value);
30561544Seschrock 		path = buf;
30571544Seschrock 	} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
30581354Seschrock 
30594451Seschrock 		/*
30604451Seschrock 		 * If the device is dead (faulted, offline, etc) then don't
30614451Seschrock 		 * bother opening it.  Otherwise we may be forcing the user to
30624451Seschrock 		 * open a misbehaving device, which can have undesirable
30634451Seschrock 		 * effects.
30644451Seschrock 		 */
306512296SLin.Ling@Sun.COM 		if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
30664451Seschrock 		    (uint64_t **)&vs, &vsc) != 0 ||
30674451Seschrock 		    vs->vs_state >= VDEV_STATE_DEGRADED) &&
30684451Seschrock 		    zhp != NULL &&
30691354Seschrock 		    nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
30701354Seschrock 			/*
30711354Seschrock 			 * Determine if the current path is correct.
30721354Seschrock 			 */
30731354Seschrock 			char *newdevid = path_to_devid(path);
30741354Seschrock 
30751354Seschrock 			if (newdevid == NULL ||
30761354Seschrock 			    strcmp(devid, newdevid) != 0) {
30771354Seschrock 				char *newpath;
30781354Seschrock 
30791354Seschrock 				if ((newpath = devid_to_path(devid)) != NULL) {
30801354Seschrock 					/*
30811354Seschrock 					 * Update the path appropriately.
30821354Seschrock 					 */
30831354Seschrock 					set_path(zhp, nv, newpath);
30842082Seschrock 					if (nvlist_add_string(nv,
30852082Seschrock 					    ZPOOL_CONFIG_PATH, newpath) == 0)
30862082Seschrock 						verify(nvlist_lookup_string(nv,
30872082Seschrock 						    ZPOOL_CONFIG_PATH,
30882082Seschrock 						    &path) == 0);
30891354Seschrock 					free(newpath);
30901354Seschrock 				}
30911354Seschrock 			}
30921354Seschrock 
30932082Seschrock 			if (newdevid)
30942082Seschrock 				devid_str_free(newdevid);
30951354Seschrock 		}
30961354Seschrock 
30971354Seschrock 		if (strncmp(path, "/dev/dsk/", 9) == 0)
30981354Seschrock 			path += 9;
30991354Seschrock 
31001354Seschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
31011544Seschrock 		    &value) == 0 && value) {
310212383SJohn.Harres@Sun.COM 			int pathlen = strlen(path);
31032082Seschrock 			char *tmp = zfs_strdup(hdl, path);
310412383SJohn.Harres@Sun.COM 
310512383SJohn.Harres@Sun.COM 			/*
310612383SJohn.Harres@Sun.COM 			 * If it starts with c#, and ends with "s0", chop
310712383SJohn.Harres@Sun.COM 			 * the "s0" off, or if it ends with "s0/old", remove
310812383SJohn.Harres@Sun.COM 			 * the "s0" from the middle.
310912383SJohn.Harres@Sun.COM 			 */
311012383SJohn.Harres@Sun.COM 			if (CTD_CHECK(tmp)) {
311112383SJohn.Harres@Sun.COM 				if (strcmp(&tmp[pathlen - 2], "s0") == 0) {
311212383SJohn.Harres@Sun.COM 					tmp[pathlen - 2] = '\0';
311312383SJohn.Harres@Sun.COM 				} else if (pathlen > 6 &&
311412383SJohn.Harres@Sun.COM 				    strcmp(&tmp[pathlen - 6], "s0/old") == 0) {
311512383SJohn.Harres@Sun.COM 					(void) strcpy(&tmp[pathlen - 6],
311612383SJohn.Harres@Sun.COM 					    "/old");
311712383SJohn.Harres@Sun.COM 				}
311812383SJohn.Harres@Sun.COM 			}
31191354Seschrock 			return (tmp);
31201354Seschrock 		}
31211354Seschrock 	} else {
31221354Seschrock 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
31232082Seschrock 
31242082Seschrock 		/*
31252082Seschrock 		 * If it's a raidz device, we need to stick in the parity level.
31262082Seschrock 		 */
31272082Seschrock 		if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
31282082Seschrock 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
31292082Seschrock 			    &value) == 0);
31302082Seschrock 			(void) snprintf(buf, sizeof (buf), "%s%llu", path,
31312856Snd150628 			    (u_longlong_t)value);
31322082Seschrock 			path = buf;
31332082Seschrock 		}
313410594SGeorge.Wilson@Sun.COM 
313510594SGeorge.Wilson@Sun.COM 		/*
313610594SGeorge.Wilson@Sun.COM 		 * We identify each top-level vdev by using a <type-id>
313710594SGeorge.Wilson@Sun.COM 		 * naming convention.
313810594SGeorge.Wilson@Sun.COM 		 */
313910594SGeorge.Wilson@Sun.COM 		if (verbose) {
314010594SGeorge.Wilson@Sun.COM 			uint64_t id;
314110594SGeorge.Wilson@Sun.COM 
314210594SGeorge.Wilson@Sun.COM 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
314310594SGeorge.Wilson@Sun.COM 			    &id) == 0);
314410594SGeorge.Wilson@Sun.COM 			(void) snprintf(buf, sizeof (buf), "%s-%llu", path,
314510594SGeorge.Wilson@Sun.COM 			    (u_longlong_t)id);
314610594SGeorge.Wilson@Sun.COM 			path = buf;
314710594SGeorge.Wilson@Sun.COM 		}
31481354Seschrock 	}
31491354Seschrock 
31502082Seschrock 	return (zfs_strdup(hdl, path));
31511354Seschrock }
31521544Seschrock 
31531544Seschrock static int
31541544Seschrock zbookmark_compare(const void *a, const void *b)
31551544Seschrock {
31561544Seschrock 	return (memcmp(a, b, sizeof (zbookmark_t)));
31571544Seschrock }
31581544Seschrock 
31591544Seschrock /*
31601544Seschrock  * Retrieve the persistent error log, uniquify the members, and return to the
31611544Seschrock  * caller.
31621544Seschrock  */
31631544Seschrock int
31643444Sek110237 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
31651544Seschrock {
31661544Seschrock 	zfs_cmd_t zc = { 0 };
31671544Seschrock 	uint64_t count;
31682676Seschrock 	zbookmark_t *zb = NULL;
31693444Sek110237 	int i;
31701544Seschrock 
31711544Seschrock 	/*
31721544Seschrock 	 * Retrieve the raw error list from the kernel.  If the number of errors
31731544Seschrock 	 * has increased, allocate more space and continue until we get the
31741544Seschrock 	 * entire list.
31751544Seschrock 	 */
31761544Seschrock 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
31771544Seschrock 	    &count) == 0);
31784820Sek110237 	if (count == 0)
31794820Sek110237 		return (0);
31802676Seschrock 	if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
31812856Snd150628 	    count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
31822082Seschrock 		return (-1);
31832676Seschrock 	zc.zc_nvlist_dst_size = count;
31841544Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
31851544Seschrock 	for (;;) {
31862082Seschrock 		if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
31872082Seschrock 		    &zc) != 0) {
31882676Seschrock 			free((void *)(uintptr_t)zc.zc_nvlist_dst);
31891544Seschrock 			if (errno == ENOMEM) {
31903823Svb160487 				count = zc.zc_nvlist_dst_size;
31912676Seschrock 				if ((zc.zc_nvlist_dst = (uintptr_t)
31923823Svb160487 				    zfs_alloc(zhp->zpool_hdl, count *
31933823Svb160487 				    sizeof (zbookmark_t))) == (uintptr_t)NULL)
31942082Seschrock 					return (-1);
31951544Seschrock 			} else {
31961544Seschrock 				return (-1);
31971544Seschrock 			}
31981544Seschrock 		} else {
31991544Seschrock 			break;
32001544Seschrock 		}
32011544Seschrock 	}
32021544Seschrock 
32031544Seschrock 	/*
32041544Seschrock 	 * Sort the resulting bookmarks.  This is a little confusing due to the
32051544Seschrock 	 * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
32062676Seschrock 	 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
32071544Seschrock 	 * _not_ copied as part of the process.  So we point the start of our
32081544Seschrock 	 * array appropriate and decrement the total number of elements.
32091544Seschrock 	 */
32102676Seschrock 	zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
32112676Seschrock 	    zc.zc_nvlist_dst_size;
32122676Seschrock 	count -= zc.zc_nvlist_dst_size;
32131544Seschrock 
32141544Seschrock 	qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
32151544Seschrock 
32163444Sek110237 	verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
32171544Seschrock 
32181544Seschrock 	/*
32193444Sek110237 	 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
32201544Seschrock 	 */
32211544Seschrock 	for (i = 0; i < count; i++) {
32221544Seschrock 		nvlist_t *nv;
32231544Seschrock 
32243700Sek110237 		/* ignoring zb_blkid and zb_level for now */
32253700Sek110237 		if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
32263700Sek110237 		    zb[i-1].zb_object == zb[i].zb_object)
32271544Seschrock 			continue;
32281544Seschrock 
32293444Sek110237 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
32303444Sek110237 			goto nomem;
32313444Sek110237 		if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
32323444Sek110237 		    zb[i].zb_objset) != 0) {
32333444Sek110237 			nvlist_free(nv);
32342082Seschrock 			goto nomem;
32353444Sek110237 		}
32363444Sek110237 		if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
32373444Sek110237 		    zb[i].zb_object) != 0) {
32383444Sek110237 			nvlist_free(nv);
32393444Sek110237 			goto nomem;
32401544Seschrock 		}
32413444Sek110237 		if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
32423444Sek110237 			nvlist_free(nv);
32433444Sek110237 			goto nomem;
32443444Sek110237 		}
32453444Sek110237 		nvlist_free(nv);
32461544Seschrock 	}
32471544Seschrock 
32483265Sahrens 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
32491544Seschrock 	return (0);
32502082Seschrock 
32512082Seschrock nomem:
32522676Seschrock 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
32532082Seschrock 	return (no_memory(zhp->zpool_hdl));
32541544Seschrock }
32551760Seschrock 
32561760Seschrock /*
32571760Seschrock  * Upgrade a ZFS pool to the latest on-disk version.
32581760Seschrock  */
32591760Seschrock int
32605094Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
32611760Seschrock {
32621760Seschrock 	zfs_cmd_t zc = { 0 };
32632082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
32641760Seschrock 
32651760Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
32665094Slling 	zc.zc_cookie = new_version;
32675094Slling 
32684543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
32693237Slling 		return (zpool_standard_error_fmt(hdl, errno,
32702082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
32712082Seschrock 		    zhp->zpool_name));
32721760Seschrock 	return (0);
32731760Seschrock }
32742926Sek110237 
32754988Sek110237 void
32764988Sek110237 zpool_set_history_str(const char *subcommand, int argc, char **argv,
32774988Sek110237     char *history_str)
32784988Sek110237 {
32794988Sek110237 	int i;
32804988Sek110237 
32814988Sek110237 	(void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
32824988Sek110237 	for (i = 1; i < argc; i++) {
32834988Sek110237 		if (strlen(history_str) + 1 + strlen(argv[i]) >
32844988Sek110237 		    HIS_MAX_RECORD_LEN)
32854988Sek110237 			break;
32864988Sek110237 		(void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
32874988Sek110237 		(void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
32884988Sek110237 	}
32894988Sek110237 }
32904988Sek110237 
32912926Sek110237 /*
32924988Sek110237  * Stage command history for logging.
32932926Sek110237  */
32944988Sek110237 int
32954988Sek110237 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
32962926Sek110237 {
32974988Sek110237 	if (history_str == NULL)
32984988Sek110237 		return (EINVAL);
32994988Sek110237 
33004988Sek110237 	if (strlen(history_str) > HIS_MAX_RECORD_LEN)
33014988Sek110237 		return (EINVAL);
33022926Sek110237 
33034715Sek110237 	if (hdl->libzfs_log_str != NULL)
33044543Smarks 		free(hdl->libzfs_log_str);
33052926Sek110237 
33064988Sek110237 	if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
33074988Sek110237 		return (no_memory(hdl));
33084543Smarks 
33094988Sek110237 	return (0);
33102926Sek110237 }
33112926Sek110237 
33122926Sek110237 /*
33132926Sek110237  * Perform ioctl to get some command history of a pool.
33142926Sek110237  *
33152926Sek110237  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
33162926Sek110237  * logical offset of the history buffer to start reading from.
33172926Sek110237  *
33182926Sek110237  * Upon return, 'off' is the next logical offset to read from and
33192926Sek110237  * 'len' is the actual amount of bytes read into 'buf'.
33202926Sek110237  */
33212926Sek110237 static int
33222926Sek110237 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
33232926Sek110237 {
33242926Sek110237 	zfs_cmd_t zc = { 0 };
33252926Sek110237 	libzfs_handle_t *hdl = zhp->zpool_hdl;
33262926Sek110237 
33272926Sek110237 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
33282926Sek110237 
33292926Sek110237 	zc.zc_history = (uint64_t)(uintptr_t)buf;
33302926Sek110237 	zc.zc_history_len = *len;
33312926Sek110237 	zc.zc_history_offset = *off;
33322926Sek110237 
33332926Sek110237 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
33342926Sek110237 		switch (errno) {
33352926Sek110237 		case EPERM:
33363237Slling 			return (zfs_error_fmt(hdl, EZFS_PERM,
33373237Slling 			    dgettext(TEXT_DOMAIN,
33382926Sek110237 			    "cannot show history for pool '%s'"),
33392926Sek110237 			    zhp->zpool_name));
33402926Sek110237 		case ENOENT:
33413237Slling 			return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
33422926Sek110237 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
33432926Sek110237 			    "'%s'"), zhp->zpool_name));
33443863Sek110237 		case ENOTSUP:
33453863Sek110237 			return (zfs_error_fmt(hdl, EZFS_BADVERSION,
33463863Sek110237 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
33473863Sek110237 			    "'%s', pool must be upgraded"), zhp->zpool_name));
33482926Sek110237 		default:
33493237Slling 			return (zpool_standard_error_fmt(hdl, errno,
33502926Sek110237 			    dgettext(TEXT_DOMAIN,
33512926Sek110237 			    "cannot get history for '%s'"), zhp->zpool_name));
33522926Sek110237 		}
33532926Sek110237 	}
33542926Sek110237 
33552926Sek110237 	*len = zc.zc_history_len;
33562926Sek110237 	*off = zc.zc_history_offset;
33572926Sek110237 
33582926Sek110237 	return (0);
33592926Sek110237 }
33602926Sek110237 
33612926Sek110237 /*
33622926Sek110237  * Process the buffer of nvlists, unpacking and storing each nvlist record
33632926Sek110237  * into 'records'.  'leftover' is set to the number of bytes that weren't
33642926Sek110237  * processed as there wasn't a complete record.
33652926Sek110237  */
336610685SGeorge.Wilson@Sun.COM int
33672926Sek110237 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
33682926Sek110237     nvlist_t ***records, uint_t *numrecords)
33692926Sek110237 {
33702926Sek110237 	uint64_t reclen;
33712926Sek110237 	nvlist_t *nv;
33722926Sek110237 	int i;
33732926Sek110237 
33742926Sek110237 	while (bytes_read > sizeof (reclen)) {
33752926Sek110237 
33762926Sek110237 		/* get length of packed record (stored as little endian) */
33772926Sek110237 		for (i = 0, reclen = 0; i < sizeof (reclen); i++)
33782926Sek110237 			reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
33792926Sek110237 
33802926Sek110237 		if (bytes_read < sizeof (reclen) + reclen)
33812926Sek110237 			break;
33822926Sek110237 
33832926Sek110237 		/* unpack record */
33842926Sek110237 		if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
33852926Sek110237 			return (ENOMEM);
33862926Sek110237 		bytes_read -= sizeof (reclen) + reclen;
33872926Sek110237 		buf += sizeof (reclen) + reclen;
33882926Sek110237 
33892926Sek110237 		/* add record to nvlist array */
33902926Sek110237 		(*numrecords)++;
33912926Sek110237 		if (ISP2(*numrecords + 1)) {
33922926Sek110237 			*records = realloc(*records,
33932926Sek110237 			    *numrecords * 2 * sizeof (nvlist_t *));
33942926Sek110237 		}
33952926Sek110237 		(*records)[*numrecords - 1] = nv;
33962926Sek110237 	}
33972926Sek110237 
33982926Sek110237 	*leftover = bytes_read;
33992926Sek110237 	return (0);
34002926Sek110237 }
34012926Sek110237 
34022926Sek110237 #define	HIS_BUF_LEN	(128*1024)
34032926Sek110237 
34042926Sek110237 /*
34052926Sek110237  * Retrieve the command history of a pool.
34062926Sek110237  */
34072926Sek110237 int
34082926Sek110237 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
34092926Sek110237 {
34102926Sek110237 	char buf[HIS_BUF_LEN];
34112926Sek110237 	uint64_t off = 0;
34122926Sek110237 	nvlist_t **records = NULL;
34132926Sek110237 	uint_t numrecords = 0;
34142926Sek110237 	int err, i;
34152926Sek110237 
34162926Sek110237 	do {
34172926Sek110237 		uint64_t bytes_read = sizeof (buf);
34182926Sek110237 		uint64_t leftover;
34192926Sek110237 
34202926Sek110237 		if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
34212926Sek110237 			break;
34222926Sek110237 
34232926Sek110237 		/* if nothing else was read in, we're at EOF, just return */
34242926Sek110237 		if (!bytes_read)
34252926Sek110237 			break;
34262926Sek110237 
34272926Sek110237 		if ((err = zpool_history_unpack(buf, bytes_read,
34282926Sek110237 		    &leftover, &records, &numrecords)) != 0)
34292926Sek110237 			break;
34302926Sek110237 		off -= leftover;
34312926Sek110237 
34322926Sek110237 		/* CONSTCOND */
34332926Sek110237 	} while (1);
34342926Sek110237 
34352926Sek110237 	if (!err) {
34362926Sek110237 		verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
34372926Sek110237 		verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
34382926Sek110237 		    records, numrecords) == 0);
34392926Sek110237 	}
34402926Sek110237 	for (i = 0; i < numrecords; i++)
34412926Sek110237 		nvlist_free(records[i]);
34422926Sek110237 	free(records);
34432926Sek110237 
34442926Sek110237 	return (err);
34452926Sek110237 }
34463444Sek110237 
34473444Sek110237 void
34483444Sek110237 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
34493444Sek110237     char *pathname, size_t len)
34503444Sek110237 {
34513444Sek110237 	zfs_cmd_t zc = { 0 };
34523444Sek110237 	boolean_t mounted = B_FALSE;
34533444Sek110237 	char *mntpnt = NULL;
34543444Sek110237 	char dsname[MAXNAMELEN];
34553444Sek110237 
34563444Sek110237 	if (dsobj == 0) {
34573444Sek110237 		/* special case for the MOS */
34583444Sek110237 		(void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
34593444Sek110237 		return;
34603444Sek110237 	}
34613444Sek110237 
34623444Sek110237 	/* get the dataset's name */
34633444Sek110237 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
34643444Sek110237 	zc.zc_obj = dsobj;
34653444Sek110237 	if (ioctl(zhp->zpool_hdl->libzfs_fd,
34663444Sek110237 	    ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
34673444Sek110237 		/* just write out a path of two object numbers */
34683444Sek110237 		(void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
34693444Sek110237 		    dsobj, obj);
34703444Sek110237 		return;
34713444Sek110237 	}
34723444Sek110237 	(void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
34733444Sek110237 
34743444Sek110237 	/* find out if the dataset is mounted */
34753444Sek110237 	mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
34763444Sek110237 
34773444Sek110237 	/* get the corrupted object's path */
34783444Sek110237 	(void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
34793444Sek110237 	zc.zc_obj = obj;
34803444Sek110237 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
34813444Sek110237 	    &zc) == 0) {
34823444Sek110237 		if (mounted) {
34833444Sek110237 			(void) snprintf(pathname, len, "%s%s", mntpnt,
34843444Sek110237 			    zc.zc_value);
34853444Sek110237 		} else {
34863444Sek110237 			(void) snprintf(pathname, len, "%s:%s",
34873444Sek110237 			    dsname, zc.zc_value);
34883444Sek110237 		}
34893444Sek110237 	} else {
34903444Sek110237 		(void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
34913444Sek110237 	}
34923444Sek110237 	free(mntpnt);
34933444Sek110237 }
34943912Slling 
34954276Staylor /*
34967042Sgw25295  * Read the EFI label from the config, if a label does not exist then
34977042Sgw25295  * pass back the error to the caller. If the caller has passed a non-NULL
34987042Sgw25295  * diskaddr argument then we set it to the starting address of the EFI
34997042Sgw25295  * partition.
35007042Sgw25295  */
35017042Sgw25295 static int
35027042Sgw25295 read_efi_label(nvlist_t *config, diskaddr_t *sb)
35037042Sgw25295 {
35047042Sgw25295 	char *path;
35057042Sgw25295 	int fd;
35067042Sgw25295 	char diskname[MAXPATHLEN];
35077042Sgw25295 	int err = -1;
35087042Sgw25295 
35097042Sgw25295 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
35107042Sgw25295 		return (err);
35117042Sgw25295 
35127042Sgw25295 	(void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
35137042Sgw25295 	    strrchr(path, '/'));
35147042Sgw25295 	if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
35157042Sgw25295 		struct dk_gpt *vtoc;
35167042Sgw25295 
35177042Sgw25295 		if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
35187042Sgw25295 			if (sb != NULL)
35197042Sgw25295 				*sb = vtoc->efi_parts[0].p_start;
35207042Sgw25295 			efi_free(vtoc);
35217042Sgw25295 		}
35227042Sgw25295 		(void) close(fd);
35237042Sgw25295 	}
35247042Sgw25295 	return (err);
35257042Sgw25295 }
35267042Sgw25295 
35277042Sgw25295 /*
35284276Staylor  * determine where a partition starts on a disk in the current
35294276Staylor  * configuration
35304276Staylor  */
35314276Staylor static diskaddr_t
35324276Staylor find_start_block(nvlist_t *config)
35334276Staylor {
35344276Staylor 	nvlist_t **child;
35354276Staylor 	uint_t c, children;
35364276Staylor 	diskaddr_t sb = MAXOFFSET_T;
35374276Staylor 	uint64_t wholedisk;
35384276Staylor 
35394276Staylor 	if (nvlist_lookup_nvlist_array(config,
35404276Staylor 	    ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
35414276Staylor 		if (nvlist_lookup_uint64(config,
35424276Staylor 		    ZPOOL_CONFIG_WHOLE_DISK,
35434276Staylor 		    &wholedisk) != 0 || !wholedisk) {
35444276Staylor 			return (MAXOFFSET_T);
35454276Staylor 		}
35467042Sgw25295 		if (read_efi_label(config, &sb) < 0)
35477042Sgw25295 			sb = MAXOFFSET_T;
35484276Staylor 		return (sb);
35494276Staylor 	}
35504276Staylor 
35514276Staylor 	for (c = 0; c < children; c++) {
35524276Staylor 		sb = find_start_block(child[c]);
35534276Staylor 		if (sb != MAXOFFSET_T) {
35544276Staylor 			return (sb);
35554276Staylor 		}
35564276Staylor 	}
35574276Staylor 	return (MAXOFFSET_T);
35584276Staylor }
35594276Staylor 
35604276Staylor /*
35614276Staylor  * Label an individual disk.  The name provided is the short name,
35624276Staylor  * stripped of any leading /dev path.
35634276Staylor  */
35644276Staylor int
35654276Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
35664276Staylor {
35674276Staylor 	char path[MAXPATHLEN];
35684276Staylor 	struct dk_gpt *vtoc;
35694276Staylor 	int fd;
35704276Staylor 	size_t resv = EFI_MIN_RESV_SIZE;
35714276Staylor 	uint64_t slice_size;
35724276Staylor 	diskaddr_t start_block;
35734276Staylor 	char errbuf[1024];
35744276Staylor 
35756289Smmusante 	/* prepare an error message just in case */
35766289Smmusante 	(void) snprintf(errbuf, sizeof (errbuf),
35776289Smmusante 	    dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
35786289Smmusante 
35794276Staylor 	if (zhp) {
35804276Staylor 		nvlist_t *nvroot;
35814276Staylor 
35827965SGeorge.Wilson@Sun.COM 		if (pool_is_bootable(zhp)) {
35837965SGeorge.Wilson@Sun.COM 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35847965SGeorge.Wilson@Sun.COM 			    "EFI labeled devices are not supported on root "
35857965SGeorge.Wilson@Sun.COM 			    "pools."));
35867965SGeorge.Wilson@Sun.COM 			return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
35877965SGeorge.Wilson@Sun.COM 		}
35887965SGeorge.Wilson@Sun.COM 
35894276Staylor 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
35904276Staylor 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
35914276Staylor 
35924276Staylor 		if (zhp->zpool_start_block == 0)
35934276Staylor 			start_block = find_start_block(nvroot);
35944276Staylor 		else
35954276Staylor 			start_block = zhp->zpool_start_block;
35964276Staylor 		zhp->zpool_start_block = start_block;
35974276Staylor 	} else {
35984276Staylor 		/* new pool */
35994276Staylor 		start_block = NEW_START_BLOCK;
36004276Staylor 	}
36014276Staylor 
36024276Staylor 	(void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
36034276Staylor 	    BACKUP_SLICE);
36044276Staylor 
36054276Staylor 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
36064276Staylor 		/*
36074276Staylor 		 * This shouldn't happen.  We've long since verified that this
36084276Staylor 		 * is a valid device.
36094276Staylor 		 */
36106289Smmusante 		zfs_error_aux(hdl,
36116289Smmusante 		    dgettext(TEXT_DOMAIN, "unable to open device"));
36124276Staylor 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
36134276Staylor 	}
36144276Staylor 
36154276Staylor 	if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
36164276Staylor 		/*
36174276Staylor 		 * The only way this can fail is if we run out of memory, or we
36184276Staylor 		 * were unable to read the disk's capacity
36194276Staylor 		 */
36204276Staylor 		if (errno == ENOMEM)
36214276Staylor 			(void) no_memory(hdl);
36224276Staylor 
36234276Staylor 		(void) close(fd);
36246289Smmusante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
36256289Smmusante 		    "unable to read disk capacity"), name);
36264276Staylor 
36274276Staylor 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
36284276Staylor 	}
36294276Staylor 
36304276Staylor 	slice_size = vtoc->efi_last_u_lba + 1;
36314276Staylor 	slice_size -= EFI_MIN_RESV_SIZE;
36324276Staylor 	if (start_block == MAXOFFSET_T)
36334276Staylor 		start_block = NEW_START_BLOCK;
36344276Staylor 	slice_size -= start_block;
36354276Staylor 
36364276Staylor 	vtoc->efi_parts[0].p_start = start_block;
36374276Staylor 	vtoc->efi_parts[0].p_size = slice_size;
36384276Staylor 
36394276Staylor 	/*
36404276Staylor 	 * Why we use V_USR: V_BACKUP confuses users, and is considered
36414276Staylor 	 * disposable by some EFI utilities (since EFI doesn't have a backup
36424276Staylor 	 * slice).  V_UNASSIGNED is supposed to be used only for zero size
36434276Staylor 	 * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
36444276Staylor 	 * etc. were all pretty specific.  V_USR is as close to reality as we
36454276Staylor 	 * can get, in the absence of V_OTHER.
36464276Staylor 	 */
36474276Staylor 	vtoc->efi_parts[0].p_tag = V_USR;
36484276Staylor 	(void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
36494276Staylor 
36504276Staylor 	vtoc->efi_parts[8].p_start = slice_size + start_block;
36514276Staylor 	vtoc->efi_parts[8].p_size = resv;
36524276Staylor 	vtoc->efi_parts[8].p_tag = V_RESERVED;
36534276Staylor 
36544276Staylor 	if (efi_write(fd, vtoc) != 0) {
36554276Staylor 		/*
36564276Staylor 		 * Some block drivers (like pcata) may not support EFI
36574276Staylor 		 * GPT labels.  Print out a helpful error message dir-
36584276Staylor 		 * ecting the user to manually label the disk and give
36594276Staylor 		 * a specific slice.
36604276Staylor 		 */
36614276Staylor 		(void) close(fd);
36624276Staylor 		efi_free(vtoc);
36634276Staylor 
36644276Staylor 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
36656289Smmusante 		    "try using fdisk(1M) and then provide a specific slice"));
36664276Staylor 		return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
36674276Staylor 	}
36684276Staylor 
36694276Staylor 	(void) close(fd);
36704276Staylor 	efi_free(vtoc);
36714276Staylor 	return (0);
36724276Staylor }
36736423Sgw25295 
36746423Sgw25295 static boolean_t
36756423Sgw25295 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
36766423Sgw25295 {
36776423Sgw25295 	char *type;
36786423Sgw25295 	nvlist_t **child;
36796423Sgw25295 	uint_t children, c;
36806423Sgw25295 
36816423Sgw25295 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
36826423Sgw25295 	if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
36836423Sgw25295 	    strcmp(type, VDEV_TYPE_FILE) == 0 ||
36846423Sgw25295 	    strcmp(type, VDEV_TYPE_LOG) == 0 ||
368510594SGeorge.Wilson@Sun.COM 	    strcmp(type, VDEV_TYPE_HOLE) == 0 ||
36866423Sgw25295 	    strcmp(type, VDEV_TYPE_MISSING) == 0) {
36876423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
36886423Sgw25295 		    "vdev type '%s' is not supported"), type);
36896423Sgw25295 		(void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
36906423Sgw25295 		return (B_FALSE);
36916423Sgw25295 	}
36926423Sgw25295 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
36936423Sgw25295 	    &child, &children) == 0) {
36946423Sgw25295 		for (c = 0; c < children; c++) {
36956423Sgw25295 			if (!supported_dump_vdev_type(hdl, child[c], errbuf))
36966423Sgw25295 				return (B_FALSE);
36976423Sgw25295 		}
36986423Sgw25295 	}
36996423Sgw25295 	return (B_TRUE);
37006423Sgw25295 }
37016423Sgw25295 
37026423Sgw25295 /*
37036423Sgw25295  * check if this zvol is allowable for use as a dump device; zero if
37046423Sgw25295  * it is, > 0 if it isn't, < 0 if it isn't a zvol
37056423Sgw25295  */
37066423Sgw25295 int
37076423Sgw25295 zvol_check_dump_config(char *arg)
37086423Sgw25295 {
37096423Sgw25295 	zpool_handle_t *zhp = NULL;
37106423Sgw25295 	nvlist_t *config, *nvroot;
37116423Sgw25295 	char *p, *volname;
37126423Sgw25295 	nvlist_t **top;
37136423Sgw25295 	uint_t toplevels;
37146423Sgw25295 	libzfs_handle_t *hdl;
37156423Sgw25295 	char errbuf[1024];
37166423Sgw25295 	char poolname[ZPOOL_MAXNAMELEN];
37176423Sgw25295 	int pathlen = strlen(ZVOL_FULL_DEV_DIR);
37186423Sgw25295 	int ret = 1;
37196423Sgw25295 
37206423Sgw25295 	if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
37216423Sgw25295 		return (-1);
37226423Sgw25295 	}
37236423Sgw25295 
37246423Sgw25295 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
37256423Sgw25295 	    "dump is not supported on device '%s'"), arg);
37266423Sgw25295 
37276423Sgw25295 	if ((hdl = libzfs_init()) == NULL)
37286423Sgw25295 		return (1);
37296423Sgw25295 	libzfs_print_on_error(hdl, B_TRUE);
37306423Sgw25295 
37316423Sgw25295 	volname = arg + pathlen;
37326423Sgw25295 
37336423Sgw25295 	/* check the configuration of the pool */
37346423Sgw25295 	if ((p = strchr(volname, '/')) == NULL) {
37356423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37366423Sgw25295 		    "malformed dataset name"));
37376423Sgw25295 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
37386423Sgw25295 		return (1);
37396423Sgw25295 	} else if (p - volname >= ZFS_MAXNAMELEN) {
37406423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37416423Sgw25295 		    "dataset name is too long"));
37426423Sgw25295 		(void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
37436423Sgw25295 		return (1);
37446423Sgw25295 	} else {
37456423Sgw25295 		(void) strncpy(poolname, volname, p - volname);
37466423Sgw25295 		poolname[p - volname] = '\0';
37476423Sgw25295 	}
37486423Sgw25295 
37496423Sgw25295 	if ((zhp = zpool_open(hdl, poolname)) == NULL) {
37506423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37516423Sgw25295 		    "could not open pool '%s'"), poolname);
37526423Sgw25295 		(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
37536423Sgw25295 		goto out;
37546423Sgw25295 	}
37556423Sgw25295 	config = zpool_get_config(zhp, NULL);
37566423Sgw25295 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
37576423Sgw25295 	    &nvroot) != 0) {
37586423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37596423Sgw25295 		    "could not obtain vdev configuration for  '%s'"), poolname);
37606423Sgw25295 		(void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
37616423Sgw25295 		goto out;
37626423Sgw25295 	}
37636423Sgw25295 
37646423Sgw25295 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
37656423Sgw25295 	    &top, &toplevels) == 0);
37666423Sgw25295 	if (toplevels != 1) {
37676423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37686423Sgw25295 		    "'%s' has multiple top level vdevs"), poolname);
37696423Sgw25295 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
37706423Sgw25295 		goto out;
37716423Sgw25295 	}
37726423Sgw25295 
37736423Sgw25295 	if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
37746423Sgw25295 		goto out;
37756423Sgw25295 	}
37766423Sgw25295 	ret = 0;
37776423Sgw25295 
37786423Sgw25295 out:
37796423Sgw25295 	if (zhp)
37806423Sgw25295 		zpool_close(zhp);
37816423Sgw25295 	libzfs_fini(hdl);
37826423Sgw25295 	return (ret);
37836423Sgw25295 }
3784