1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51485Slling  * Common Development and Distribution License (the "License").
61485Slling  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
212082Seschrock 
22789Sahrens /*
238525SEric.Schrock@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24789Sahrens  * Use is subject to license terms.
25789Sahrens  */
26789Sahrens 
273126Sahl #include <alloca.h>
28789Sahrens #include <assert.h>
29789Sahrens #include <ctype.h>
30789Sahrens #include <errno.h>
31789Sahrens #include <devid.h>
323126Sahl #include <dirent.h>
33789Sahrens #include <fcntl.h>
34789Sahrens #include <libintl.h>
35789Sahrens #include <stdio.h>
36789Sahrens #include <stdlib.h>
373126Sahl #include <strings.h>
38789Sahrens #include <unistd.h>
397184Stimh #include <zone.h>
404276Staylor #include <sys/efi_partition.h>
414276Staylor #include <sys/vtoc.h>
42789Sahrens #include <sys/zfs_ioctl.h>
431544Seschrock #include <sys/zio.h>
442926Sek110237 #include <strings.h>
45789Sahrens 
46789Sahrens #include "zfs_namecheck.h"
473912Slling #include "zfs_prop.h"
48789Sahrens #include "libzfs_impl.h"
49789Sahrens 
507042Sgw25295 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
515094Slling 
527965SGeorge.Wilson@Sun.COM #if defined(__i386) || defined(__amd64)
537965SGeorge.Wilson@Sun.COM #define	BOOTCMD	"installgrub(1M)"
547965SGeorge.Wilson@Sun.COM #else
557965SGeorge.Wilson@Sun.COM #define	BOOTCMD	"installboot(1M)"
567965SGeorge.Wilson@Sun.COM #endif
577965SGeorge.Wilson@Sun.COM 
585094Slling /*
595094Slling  * ====================================================================
605094Slling  *   zpool property functions
615094Slling  * ====================================================================
625094Slling  */
635094Slling 
645094Slling static int
655094Slling zpool_get_all_props(zpool_handle_t *zhp)
665094Slling {
675094Slling 	zfs_cmd_t zc = { 0 };
685094Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
695094Slling 
705094Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
715094Slling 
725094Slling 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
735094Slling 		return (-1);
745094Slling 
755094Slling 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
765094Slling 		if (errno == ENOMEM) {
775094Slling 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
785094Slling 				zcmd_free_nvlists(&zc);
795094Slling 				return (-1);
805094Slling 			}
815094Slling 		} else {
825094Slling 			zcmd_free_nvlists(&zc);
835094Slling 			return (-1);
845094Slling 		}
855094Slling 	}
865094Slling 
875094Slling 	if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
885094Slling 		zcmd_free_nvlists(&zc);
895094Slling 		return (-1);
905094Slling 	}
915094Slling 
925094Slling 	zcmd_free_nvlists(&zc);
935094Slling 
945094Slling 	return (0);
955094Slling }
965094Slling 
975094Slling static int
985094Slling zpool_props_refresh(zpool_handle_t *zhp)
995094Slling {
1005094Slling 	nvlist_t *old_props;
1015094Slling 
1025094Slling 	old_props = zhp->zpool_props;
1035094Slling 
1045094Slling 	if (zpool_get_all_props(zhp) != 0)
1055094Slling 		return (-1);
1065094Slling 
1075094Slling 	nvlist_free(old_props);
1085094Slling 	return (0);
1095094Slling }
1105094Slling 
1115094Slling static char *
1125094Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
1135094Slling     zprop_source_t *src)
1145094Slling {
1155094Slling 	nvlist_t *nv, *nvl;
1165094Slling 	uint64_t ival;
1175094Slling 	char *value;
1185094Slling 	zprop_source_t source;
1195094Slling 
1205094Slling 	nvl = zhp->zpool_props;
1215094Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
1225094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
1235094Slling 		source = ival;
1245094Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1255094Slling 	} else {
1265094Slling 		source = ZPROP_SRC_DEFAULT;
1275094Slling 		if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
1285094Slling 			value = "-";
1295094Slling 	}
1305094Slling 
1315094Slling 	if (src)
1325094Slling 		*src = source;
1335094Slling 
1345094Slling 	return (value);
1355094Slling }
1365094Slling 
1375094Slling uint64_t
1385094Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
1395094Slling {
1405094Slling 	nvlist_t *nv, *nvl;
1415094Slling 	uint64_t value;
1425094Slling 	zprop_source_t source;
1435094Slling 
1447294Sperrin 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
1457294Sperrin 		/*
1467294Sperrin 		 * zpool_get_all_props() has most likely failed because
1477294Sperrin 		 * the pool is faulted, but if all we need is the top level
1487294Sperrin 		 * vdev's guid then get it from the zhp config nvlist.
1497294Sperrin 		 */
1507294Sperrin 		if ((prop == ZPOOL_PROP_GUID) &&
1517294Sperrin 		    (nvlist_lookup_nvlist(zhp->zpool_config,
1527294Sperrin 		    ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
1537294Sperrin 		    (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
1547294Sperrin 		    == 0)) {
1557294Sperrin 			return (value);
1567294Sperrin 		}
1575094Slling 		return (zpool_prop_default_numeric(prop));
1587294Sperrin 	}
1595094Slling 
1605094Slling 	nvl = zhp->zpool_props;
1615094Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
1625094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
1635094Slling 		source = value;
1645094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1655094Slling 	} else {
1665094Slling 		source = ZPROP_SRC_DEFAULT;
1675094Slling 		value = zpool_prop_default_numeric(prop);
1685094Slling 	}
1695094Slling 
1705094Slling 	if (src)
1715094Slling 		*src = source;
1725094Slling 
1735094Slling 	return (value);
1745094Slling }
1755094Slling 
1765094Slling /*
1775094Slling  * Map VDEV STATE to printed strings.
1785094Slling  */
1795094Slling char *
1805094Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
1815094Slling {
1825094Slling 	switch (state) {
1835094Slling 	case VDEV_STATE_CLOSED:
1845094Slling 	case VDEV_STATE_OFFLINE:
1855094Slling 		return (gettext("OFFLINE"));
1865094Slling 	case VDEV_STATE_REMOVED:
1875094Slling 		return (gettext("REMOVED"));
1885094Slling 	case VDEV_STATE_CANT_OPEN:
1897294Sperrin 		if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
1905094Slling 			return (gettext("FAULTED"));
1915094Slling 		else
1925094Slling 			return (gettext("UNAVAIL"));
1935094Slling 	case VDEV_STATE_FAULTED:
1945094Slling 		return (gettext("FAULTED"));
1955094Slling 	case VDEV_STATE_DEGRADED:
1965094Slling 		return (gettext("DEGRADED"));
1975094Slling 	case VDEV_STATE_HEALTHY:
1985094Slling 		return (gettext("ONLINE"));
1995094Slling 	}
2005094Slling 
2015094Slling 	return (gettext("UNKNOWN"));
2025094Slling }
2035094Slling 
2045094Slling /*
2055094Slling  * Get a zpool property value for 'prop' and return the value in
2065094Slling  * a pre-allocated buffer.
2075094Slling  */
2085094Slling int
2095094Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
2105094Slling     zprop_source_t *srctype)
2115094Slling {
2125094Slling 	uint64_t intval;
2135094Slling 	const char *strval;
2145094Slling 	zprop_source_t src = ZPROP_SRC_NONE;
2155094Slling 	nvlist_t *nvroot;
2165094Slling 	vdev_stat_t *vs;
2175094Slling 	uint_t vsc;
2185094Slling 
2195094Slling 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
2208525SEric.Schrock@Sun.COM 		switch (prop) {
2218525SEric.Schrock@Sun.COM 		case ZPOOL_PROP_NAME:
2225094Slling 			(void) strlcpy(buf, zpool_get_name(zhp), len);
2238525SEric.Schrock@Sun.COM 			break;
2248525SEric.Schrock@Sun.COM 
2258525SEric.Schrock@Sun.COM 		case ZPOOL_PROP_HEALTH:
2265094Slling 			(void) strlcpy(buf, "FAULTED", len);
2278525SEric.Schrock@Sun.COM 			break;
2288525SEric.Schrock@Sun.COM 
2298525SEric.Schrock@Sun.COM 		case ZPOOL_PROP_GUID:
2308525SEric.Schrock@Sun.COM 			intval = zpool_get_prop_int(zhp, prop, &src);
2318525SEric.Schrock@Sun.COM 			(void) snprintf(buf, len, "%llu", intval);
2328525SEric.Schrock@Sun.COM 			break;
2338525SEric.Schrock@Sun.COM 
2348525SEric.Schrock@Sun.COM 		case ZPOOL_PROP_ALTROOT:
2358525SEric.Schrock@Sun.COM 		case ZPOOL_PROP_CACHEFILE:
2368525SEric.Schrock@Sun.COM 			if (zhp->zpool_props != NULL ||
2378525SEric.Schrock@Sun.COM 			    zpool_get_all_props(zhp) == 0) {
2388525SEric.Schrock@Sun.COM 				(void) strlcpy(buf,
2398525SEric.Schrock@Sun.COM 				    zpool_get_prop_string(zhp, prop, &src),
2408525SEric.Schrock@Sun.COM 				    len);
2418525SEric.Schrock@Sun.COM 				if (srctype != NULL)
2428525SEric.Schrock@Sun.COM 					*srctype = src;
2438525SEric.Schrock@Sun.COM 				return (0);
2448525SEric.Schrock@Sun.COM 			}
2458525SEric.Schrock@Sun.COM 			/* FALLTHROUGH */
2468525SEric.Schrock@Sun.COM 		default:
2475094Slling 			(void) strlcpy(buf, "-", len);
2488525SEric.Schrock@Sun.COM 			break;
2498525SEric.Schrock@Sun.COM 		}
2508525SEric.Schrock@Sun.COM 
2518525SEric.Schrock@Sun.COM 		if (srctype != NULL)
2528525SEric.Schrock@Sun.COM 			*srctype = src;
2535094Slling 		return (0);
2545094Slling 	}
2555094Slling 
2565094Slling 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
2575094Slling 	    prop != ZPOOL_PROP_NAME)
2585094Slling 		return (-1);
2595094Slling 
2605094Slling 	switch (zpool_prop_get_type(prop)) {
2615094Slling 	case PROP_TYPE_STRING:
2625094Slling 		(void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
2635094Slling 		    len);
2645094Slling 		break;
2655094Slling 
2665094Slling 	case PROP_TYPE_NUMBER:
2675094Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
2685094Slling 
2695094Slling 		switch (prop) {
2705094Slling 		case ZPOOL_PROP_SIZE:
2715094Slling 		case ZPOOL_PROP_USED:
2725094Slling 		case ZPOOL_PROP_AVAILABLE:
2735094Slling 			(void) zfs_nicenum(intval, buf, len);
2745094Slling 			break;
2755094Slling 
2765094Slling 		case ZPOOL_PROP_CAPACITY:
2775094Slling 			(void) snprintf(buf, len, "%llu%%",
2785094Slling 			    (u_longlong_t)intval);
2795094Slling 			break;
2805094Slling 
2815094Slling 		case ZPOOL_PROP_HEALTH:
2825094Slling 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2835094Slling 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2845094Slling 			verify(nvlist_lookup_uint64_array(nvroot,
2855094Slling 			    ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
2865094Slling 
2875094Slling 			(void) strlcpy(buf, zpool_state_to_name(intval,
2885094Slling 			    vs->vs_aux), len);
2895094Slling 			break;
2905094Slling 		default:
2915094Slling 			(void) snprintf(buf, len, "%llu", intval);
2925094Slling 		}
2935094Slling 		break;
2945094Slling 
2955094Slling 	case PROP_TYPE_INDEX:
2965094Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
2975094Slling 		if (zpool_prop_index_to_string(prop, intval, &strval)
2985094Slling 		    != 0)
2995094Slling 			return (-1);
3005094Slling 		(void) strlcpy(buf, strval, len);
3015094Slling 		break;
3025094Slling 
3035094Slling 	default:
3045094Slling 		abort();
3055094Slling 	}
3065094Slling 
3075094Slling 	if (srctype)
3085094Slling 		*srctype = src;
3095094Slling 
3105094Slling 	return (0);
3115094Slling }
3125094Slling 
3135094Slling /*
3145094Slling  * Check if the bootfs name has the same pool name as it is set to.
3155094Slling  * Assuming bootfs is a valid dataset name.
3165094Slling  */
3175094Slling static boolean_t
3185094Slling bootfs_name_valid(const char *pool, char *bootfs)
3195094Slling {
3205094Slling 	int len = strlen(pool);
3215094Slling 
3227300SEric.Taylor@Sun.COM 	if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
3235094Slling 		return (B_FALSE);
3245094Slling 
3255094Slling 	if (strncmp(pool, bootfs, len) == 0 &&
3265094Slling 	    (bootfs[len] == '/' || bootfs[len] == '\0'))
3275094Slling 		return (B_TRUE);
3285094Slling 
3295094Slling 	return (B_FALSE);
3305094Slling }
3315094Slling 
3325094Slling /*
3337042Sgw25295  * Inspect the configuration to determine if any of the devices contain
3347042Sgw25295  * an EFI label.
3357042Sgw25295  */
3367042Sgw25295 static boolean_t
3377042Sgw25295 pool_uses_efi(nvlist_t *config)
3387042Sgw25295 {
3397042Sgw25295 	nvlist_t **child;
3407042Sgw25295 	uint_t c, children;
3417042Sgw25295 
3427042Sgw25295 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
3437042Sgw25295 	    &child, &children) != 0)
3447042Sgw25295 		return (read_efi_label(config, NULL) >= 0);
3457042Sgw25295 
3467042Sgw25295 	for (c = 0; c < children; c++) {
3477042Sgw25295 		if (pool_uses_efi(child[c]))
3487042Sgw25295 			return (B_TRUE);
3497042Sgw25295 	}
3507042Sgw25295 	return (B_FALSE);
3517042Sgw25295 }
3527042Sgw25295 
3537965SGeorge.Wilson@Sun.COM static boolean_t
3547965SGeorge.Wilson@Sun.COM pool_is_bootable(zpool_handle_t *zhp)
3557965SGeorge.Wilson@Sun.COM {
3567965SGeorge.Wilson@Sun.COM 	char bootfs[ZPOOL_MAXNAMELEN];
3577965SGeorge.Wilson@Sun.COM 
3587965SGeorge.Wilson@Sun.COM 	return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
3597965SGeorge.Wilson@Sun.COM 	    sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
3607965SGeorge.Wilson@Sun.COM 	    sizeof (bootfs)) != 0);
3617965SGeorge.Wilson@Sun.COM }
3627965SGeorge.Wilson@Sun.COM 
3637965SGeorge.Wilson@Sun.COM 
3647042Sgw25295 /*
3655094Slling  * Given an nvlist of zpool properties to be set, validate that they are
3665094Slling  * correct, and parse any numeric properties (index, boolean, etc) if they are
3675094Slling  * specified as strings.
3685094Slling  */
3695094Slling static nvlist_t *
3707184Stimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
3715094Slling     nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf)
3725094Slling {
3735094Slling 	nvpair_t *elem;
3745094Slling 	nvlist_t *retprops;
3755094Slling 	zpool_prop_t prop;
3765094Slling 	char *strval;
3775094Slling 	uint64_t intval;
3785363Seschrock 	char *slash;
3795363Seschrock 	struct stat64 statbuf;
3807042Sgw25295 	zpool_handle_t *zhp;
3817042Sgw25295 	nvlist_t *nvroot;
3825094Slling 
3835094Slling 	if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
3845094Slling 		(void) no_memory(hdl);
3855094Slling 		return (NULL);
3865094Slling 	}
3875094Slling 
3885094Slling 	elem = NULL;
3895094Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
3905094Slling 		const char *propname = nvpair_name(elem);
3915094Slling 
3925094Slling 		/*
3935094Slling 		 * Make sure this property is valid and applies to this type.
3945094Slling 		 */
3955094Slling 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
3965094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3975094Slling 			    "invalid property '%s'"), propname);
3985094Slling 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
3995094Slling 			goto error;
4005094Slling 		}
4015094Slling 
4025094Slling 		if (zpool_prop_readonly(prop)) {
4035094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
4045094Slling 			    "is readonly"), propname);
4055094Slling 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
4065094Slling 			goto error;
4075094Slling 		}
4085094Slling 
4095094Slling 		if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
4105094Slling 		    &strval, &intval, errbuf) != 0)
4115094Slling 			goto error;
4125094Slling 
4135094Slling 		/*
4145094Slling 		 * Perform additional checking for specific properties.
4155094Slling 		 */
4165094Slling 		switch (prop) {
4175094Slling 		case ZPOOL_PROP_VERSION:
4185094Slling 			if (intval < version || intval > SPA_VERSION) {
4195094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4205094Slling 				    "property '%s' number %d is invalid."),
4215094Slling 				    propname, intval);
4225094Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4235094Slling 				goto error;
4245094Slling 			}
4255094Slling 			break;
4265094Slling 
4275094Slling 		case ZPOOL_PROP_BOOTFS:
4285094Slling 			if (create_or_import) {
4295094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4305094Slling 				    "property '%s' cannot be set at creation "
4315094Slling 				    "or import time"), propname);
4325094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
4335094Slling 				goto error;
4345094Slling 			}
4355094Slling 
4365094Slling 			if (version < SPA_VERSION_BOOTFS) {
4375094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4385094Slling 				    "pool must be upgraded to support "
4395094Slling 				    "'%s' property"), propname);
4405094Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4415094Slling 				goto error;
4425094Slling 			}
4435094Slling 
4445094Slling 			/*
4455094Slling 			 * bootfs property value has to be a dataset name and
4465094Slling 			 * the dataset has to be in the same pool as it sets to.
4475094Slling 			 */
4485094Slling 			if (strval[0] != '\0' && !bootfs_name_valid(poolname,
4495094Slling 			    strval)) {
4505094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
4515094Slling 				    "is an invalid name"), strval);
4525094Slling 				(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4535094Slling 				goto error;
4545094Slling 			}
4557042Sgw25295 
4567042Sgw25295 			if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
4577042Sgw25295 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4587042Sgw25295 				    "could not open pool '%s'"), poolname);
4597042Sgw25295 				(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
4607042Sgw25295 				goto error;
4617042Sgw25295 			}
4627042Sgw25295 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
4637042Sgw25295 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
4647042Sgw25295 
4657042Sgw25295 			/*
4667042Sgw25295 			 * bootfs property cannot be set on a disk which has
4677042Sgw25295 			 * been EFI labeled.
4687042Sgw25295 			 */
4697042Sgw25295 			if (pool_uses_efi(nvroot)) {
4707042Sgw25295 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4717042Sgw25295 				    "property '%s' not supported on "
4727042Sgw25295 				    "EFI labeled devices"), propname);
4737042Sgw25295 				(void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
4747042Sgw25295 				zpool_close(zhp);
4757042Sgw25295 				goto error;
4767042Sgw25295 			}
4777042Sgw25295 			zpool_close(zhp);
4785094Slling 			break;
4795094Slling 
4805094Slling 		case ZPOOL_PROP_ALTROOT:
4815094Slling 			if (!create_or_import) {
4825094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4835094Slling 				    "property '%s' can only be set during pool "
4845094Slling 				    "creation or import"), propname);
4855094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
4865094Slling 				goto error;
4875094Slling 			}
4885094Slling 
4895094Slling 			if (strval[0] != '/') {
4905094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4915094Slling 				    "bad alternate root '%s'"), strval);
4925094Slling 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
4935094Slling 				goto error;
4945094Slling 			}
4955094Slling 			break;
4965363Seschrock 
4975363Seschrock 		case ZPOOL_PROP_CACHEFILE:
4985363Seschrock 			if (strval[0] == '\0')
4995363Seschrock 				break;
5005363Seschrock 
5015363Seschrock 			if (strcmp(strval, "none") == 0)
5025363Seschrock 				break;
5035363Seschrock 
5045363Seschrock 			if (strval[0] != '/') {
5055363Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5065363Seschrock 				    "property '%s' must be empty, an "
5075363Seschrock 				    "absolute path, or 'none'"), propname);
5085363Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
5095363Seschrock 				goto error;
5105363Seschrock 			}
5115363Seschrock 
5125363Seschrock 			slash = strrchr(strval, '/');
5135363Seschrock 
5145363Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
5155363Seschrock 			    strcmp(slash, "/..") == 0) {
5165363Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5175363Seschrock 				    "'%s' is not a valid file"), strval);
5185363Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
5195363Seschrock 				goto error;
5205363Seschrock 			}
5215363Seschrock 
5225363Seschrock 			*slash = '\0';
5235363Seschrock 
5245621Seschrock 			if (strval[0] != '\0' &&
5255621Seschrock 			    (stat64(strval, &statbuf) != 0 ||
5265621Seschrock 			    !S_ISDIR(statbuf.st_mode))) {
5275363Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5285363Seschrock 				    "'%s' is not a valid directory"),
5295363Seschrock 				    strval);
5305363Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
5315363Seschrock 				goto error;
5325363Seschrock 			}
5335363Seschrock 
5345363Seschrock 			*slash = '/';
5355363Seschrock 			break;
5365094Slling 		}
5375094Slling 	}
5385094Slling 
5395094Slling 	return (retprops);
5405094Slling error:
5415094Slling 	nvlist_free(retprops);
5425094Slling 	return (NULL);
5435094Slling }
5445094Slling 
5455094Slling /*
5465094Slling  * Set zpool property : propname=propval.
5475094Slling  */
5485094Slling int
5495094Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
5505094Slling {
5515094Slling 	zfs_cmd_t zc = { 0 };
5525094Slling 	int ret = -1;
5535094Slling 	char errbuf[1024];
5545094Slling 	nvlist_t *nvl = NULL;
5555094Slling 	nvlist_t *realprops;
5565094Slling 	uint64_t version;
5575094Slling 
5585094Slling 	(void) snprintf(errbuf, sizeof (errbuf),
5595094Slling 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
5605094Slling 	    zhp->zpool_name);
5615094Slling 
5625094Slling 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5635094Slling 		return (no_memory(zhp->zpool_hdl));
5645094Slling 
5655094Slling 	if (nvlist_add_string(nvl, propname, propval) != 0) {
5665094Slling 		nvlist_free(nvl);
5675094Slling 		return (no_memory(zhp->zpool_hdl));
5685094Slling 	}
5695094Slling 
5705094Slling 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
5717184Stimh 	if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
5725094Slling 	    zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) {
5735094Slling 		nvlist_free(nvl);
5745094Slling 		return (-1);
5755094Slling 	}
5765094Slling 
5775094Slling 	nvlist_free(nvl);
5785094Slling 	nvl = realprops;
5795094Slling 
5805094Slling 	/*
5815094Slling 	 * Execute the corresponding ioctl() to set this property.
5825094Slling 	 */
5835094Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
5845094Slling 
5855094Slling 	if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
5865094Slling 		nvlist_free(nvl);
5875094Slling 		return (-1);
5885094Slling 	}
5895094Slling 
5905094Slling 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
5915094Slling 
5925094Slling 	zcmd_free_nvlists(&zc);
5935094Slling 	nvlist_free(nvl);
5945094Slling 
5955094Slling 	if (ret)
5965094Slling 		(void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
5975094Slling 	else
5985094Slling 		(void) zpool_props_refresh(zhp);
5995094Slling 
6005094Slling 	return (ret);
6015094Slling }
6025094Slling 
6035094Slling int
6045094Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
6055094Slling {
6065094Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
6075094Slling 	zprop_list_t *entry;
6085094Slling 	char buf[ZFS_MAXPROPLEN];
6095094Slling 
6105094Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
6115094Slling 		return (-1);
6125094Slling 
6135094Slling 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
6145094Slling 
6155094Slling 		if (entry->pl_fixed)
6165094Slling 			continue;
6175094Slling 
6185094Slling 		if (entry->pl_prop != ZPROP_INVAL &&
6195094Slling 		    zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
6205094Slling 		    NULL) == 0) {
6215094Slling 			if (strlen(buf) > entry->pl_width)
6225094Slling 				entry->pl_width = strlen(buf);
6235094Slling 		}
6245094Slling 	}
6255094Slling 
6265094Slling 	return (0);
6275094Slling }
6285094Slling 
6295094Slling 
630789Sahrens /*
631789Sahrens  * Validate the given pool name, optionally putting an extended error message in
632789Sahrens  * 'buf'.
633789Sahrens  */
6346423Sgw25295 boolean_t
6352082Seschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
636789Sahrens {
637789Sahrens 	namecheck_err_t why;
638789Sahrens 	char what;
6391773Seschrock 	int ret;
640789Sahrens 
6411773Seschrock 	ret = pool_namecheck(pool, &why, &what);
6421773Seschrock 
6431773Seschrock 	/*
6441773Seschrock 	 * The rules for reserved pool names were extended at a later point.
6451773Seschrock 	 * But we need to support users with existing pools that may now be
6461773Seschrock 	 * invalid.  So we only check for this expanded set of names during a
6471773Seschrock 	 * create (or import), and only in userland.
6481773Seschrock 	 */
6491773Seschrock 	if (ret == 0 && !isopen &&
6501773Seschrock 	    (strncmp(pool, "mirror", 6) == 0 ||
6511773Seschrock 	    strncmp(pool, "raidz", 5) == 0 ||
6524527Sperrin 	    strncmp(pool, "spare", 5) == 0 ||
6534527Sperrin 	    strcmp(pool, "log") == 0)) {
6546423Sgw25295 		if (hdl != NULL)
6556423Sgw25295 			zfs_error_aux(hdl,
6566423Sgw25295 			    dgettext(TEXT_DOMAIN, "name is reserved"));
6572082Seschrock 		return (B_FALSE);
6581773Seschrock 	}
6591773Seschrock 
6601773Seschrock 
6611773Seschrock 	if (ret != 0) {
6622082Seschrock 		if (hdl != NULL) {
663789Sahrens 			switch (why) {
6641003Slling 			case NAME_ERR_TOOLONG:
6652082Seschrock 				zfs_error_aux(hdl,
6661003Slling 				    dgettext(TEXT_DOMAIN, "name is too long"));
6671003Slling 				break;
6681003Slling 
669789Sahrens 			case NAME_ERR_INVALCHAR:
6702082Seschrock 				zfs_error_aux(hdl,
671789Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
672789Sahrens 				    "'%c' in pool name"), what);
673789Sahrens 				break;
674789Sahrens 
675789Sahrens 			case NAME_ERR_NOLETTER:
6762082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6772082Seschrock 				    "name must begin with a letter"));
678789Sahrens 				break;
679789Sahrens 
680789Sahrens 			case NAME_ERR_RESERVED:
6812082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6822082Seschrock 				    "name is reserved"));
683789Sahrens 				break;
684789Sahrens 
685789Sahrens 			case NAME_ERR_DISKLIKE:
6862082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6872082Seschrock 				    "pool name is reserved"));
688789Sahrens 				break;
6892856Snd150628 
6902856Snd150628 			case NAME_ERR_LEADING_SLASH:
6912856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6922856Snd150628 				    "leading slash in name"));
6932856Snd150628 				break;
6942856Snd150628 
6952856Snd150628 			case NAME_ERR_EMPTY_COMPONENT:
6962856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6972856Snd150628 				    "empty component in name"));
6982856Snd150628 				break;
6992856Snd150628 
7002856Snd150628 			case NAME_ERR_TRAILING_SLASH:
7012856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7022856Snd150628 				    "trailing slash in name"));
7032856Snd150628 				break;
7042856Snd150628 
7052856Snd150628 			case NAME_ERR_MULTIPLE_AT:
7062856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7072856Snd150628 				    "multiple '@' delimiters in name"));
7082856Snd150628 				break;
7092856Snd150628 
710789Sahrens 			}
711789Sahrens 		}
7122082Seschrock 		return (B_FALSE);
713789Sahrens 	}
714789Sahrens 
7152082Seschrock 	return (B_TRUE);
716789Sahrens }
717789Sahrens 
718789Sahrens /*
719789Sahrens  * Open a handle to the given pool, even if the pool is currently in the FAULTED
720789Sahrens  * state.
721789Sahrens  */
722789Sahrens zpool_handle_t *
7232082Seschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
724789Sahrens {
725789Sahrens 	zpool_handle_t *zhp;
7262142Seschrock 	boolean_t missing;
727789Sahrens 
728789Sahrens 	/*
729789Sahrens 	 * Make sure the pool name is valid.
730789Sahrens 	 */
7312082Seschrock 	if (!zpool_name_valid(hdl, B_TRUE, pool)) {
7323237Slling 		(void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
7332082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
7342082Seschrock 		    pool);
735789Sahrens 		return (NULL);
736789Sahrens 	}
737789Sahrens 
7382082Seschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
7392082Seschrock 		return (NULL);
740789Sahrens 
7412082Seschrock 	zhp->zpool_hdl = hdl;
742789Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
743789Sahrens 
7442142Seschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
7452142Seschrock 		zpool_close(zhp);
7462142Seschrock 		return (NULL);
7472142Seschrock 	}
7482142Seschrock 
7492142Seschrock 	if (missing) {
7505094Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
7513237Slling 		(void) zfs_error_fmt(hdl, EZFS_NOENT,
7525094Slling 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
7532142Seschrock 		zpool_close(zhp);
7542142Seschrock 		return (NULL);
755789Sahrens 	}
756789Sahrens 
757789Sahrens 	return (zhp);
758789Sahrens }
759789Sahrens 
760789Sahrens /*
761789Sahrens  * Like the above, but silent on error.  Used when iterating over pools (because
762789Sahrens  * the configuration cache may be out of date).
763789Sahrens  */
7642142Seschrock int
7652142Seschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
766789Sahrens {
767789Sahrens 	zpool_handle_t *zhp;
7682142Seschrock 	boolean_t missing;
769789Sahrens 
7702142Seschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
7712142Seschrock 		return (-1);
772789Sahrens 
7732082Seschrock 	zhp->zpool_hdl = hdl;
774789Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
775789Sahrens 
7762142Seschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
7772142Seschrock 		zpool_close(zhp);
7782142Seschrock 		return (-1);
779789Sahrens 	}
780789Sahrens 
7812142Seschrock 	if (missing) {
7822142Seschrock 		zpool_close(zhp);
7832142Seschrock 		*ret = NULL;
7842142Seschrock 		return (0);
7852142Seschrock 	}
7862142Seschrock 
7872142Seschrock 	*ret = zhp;
7882142Seschrock 	return (0);
789789Sahrens }
790789Sahrens 
791789Sahrens /*
792789Sahrens  * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
793789Sahrens  * state.
794789Sahrens  */
795789Sahrens zpool_handle_t *
7962082Seschrock zpool_open(libzfs_handle_t *hdl, const char *pool)
797789Sahrens {
798789Sahrens 	zpool_handle_t *zhp;
799789Sahrens 
8002082Seschrock 	if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
801789Sahrens 		return (NULL);
802789Sahrens 
803789Sahrens 	if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
8043237Slling 		(void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
8052082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
806789Sahrens 		zpool_close(zhp);
807789Sahrens 		return (NULL);
808789Sahrens 	}
809789Sahrens 
810789Sahrens 	return (zhp);
811789Sahrens }
812789Sahrens 
813789Sahrens /*
814789Sahrens  * Close the handle.  Simply frees the memory associated with the handle.
815789Sahrens  */
816789Sahrens void
817789Sahrens zpool_close(zpool_handle_t *zhp)
818789Sahrens {
819789Sahrens 	if (zhp->zpool_config)
820789Sahrens 		nvlist_free(zhp->zpool_config);
821952Seschrock 	if (zhp->zpool_old_config)
822952Seschrock 		nvlist_free(zhp->zpool_old_config);
8233912Slling 	if (zhp->zpool_props)
8243912Slling 		nvlist_free(zhp->zpool_props);
825789Sahrens 	free(zhp);
826789Sahrens }
827789Sahrens 
828789Sahrens /*
829789Sahrens  * Return the name of the pool.
830789Sahrens  */
831789Sahrens const char *
832789Sahrens zpool_get_name(zpool_handle_t *zhp)
833789Sahrens {
834789Sahrens 	return (zhp->zpool_name);
835789Sahrens }
836789Sahrens 
837789Sahrens 
838789Sahrens /*
839789Sahrens  * Return the state of the pool (ACTIVE or UNAVAILABLE)
840789Sahrens  */
841789Sahrens int
842789Sahrens zpool_get_state(zpool_handle_t *zhp)
843789Sahrens {
844789Sahrens 	return (zhp->zpool_state);
845789Sahrens }
846789Sahrens 
847789Sahrens /*
848789Sahrens  * Create the named pool, using the provided vdev list.  It is assumed
849789Sahrens  * that the consumer has already validated the contents of the nvlist, so we
850789Sahrens  * don't have to worry about error semantics.
851789Sahrens  */
852789Sahrens int
8532082Seschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
8547184Stimh     nvlist_t *props, nvlist_t *fsprops)
855789Sahrens {
856789Sahrens 	zfs_cmd_t zc = { 0 };
8577184Stimh 	nvlist_t *zc_fsprops = NULL;
8587184Stimh 	nvlist_t *zc_props = NULL;
8592082Seschrock 	char msg[1024];
8605094Slling 	char *altroot;
8617184Stimh 	int ret = -1;
8622082Seschrock 
8632082Seschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
8642082Seschrock 	    "cannot create '%s'"), pool);
865789Sahrens 
8662082Seschrock 	if (!zpool_name_valid(hdl, B_FALSE, pool))
8672082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
8682082Seschrock 
8695320Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
8705320Slling 		return (-1);
8715320Slling 
8727184Stimh 	if (props) {
8737184Stimh 		if ((zc_props = zpool_valid_proplist(hdl, pool, props,
8747184Stimh 		    SPA_VERSION_1, B_TRUE, msg)) == NULL) {
8757184Stimh 			goto create_failed;
8767184Stimh 		}
8775320Slling 	}
878789Sahrens 
8797184Stimh 	if (fsprops) {
8807184Stimh 		uint64_t zoned;
8817184Stimh 		char *zonestr;
8827184Stimh 
8837184Stimh 		zoned = ((nvlist_lookup_string(fsprops,
8847184Stimh 		    zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
8857184Stimh 		    strcmp(zonestr, "on") == 0);
8867184Stimh 
8877184Stimh 		if ((zc_fsprops = zfs_valid_proplist(hdl,
8887184Stimh 		    ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
8897184Stimh 			goto create_failed;
8907184Stimh 		}
8917184Stimh 		if (!zc_props &&
8927184Stimh 		    (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
8937184Stimh 			goto create_failed;
8947184Stimh 		}
8957184Stimh 		if (nvlist_add_nvlist(zc_props,
8967184Stimh 		    ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
8977184Stimh 			goto create_failed;
8987184Stimh 		}
8997184Stimh 	}
9007184Stimh 
9017184Stimh 	if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
9027184Stimh 		goto create_failed;
9037184Stimh 
904789Sahrens 	(void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
905789Sahrens 
9067184Stimh 	if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
9075320Slling 
9082676Seschrock 		zcmd_free_nvlists(&zc);
9097184Stimh 		nvlist_free(zc_props);
9107184Stimh 		nvlist_free(zc_fsprops);
9112082Seschrock 
912789Sahrens 		switch (errno) {
913789Sahrens 		case EBUSY:
914789Sahrens 			/*
915789Sahrens 			 * This can happen if the user has specified the same
916789Sahrens 			 * device multiple times.  We can't reliably detect this
917789Sahrens 			 * until we try to add it and see we already have a
918789Sahrens 			 * label.
919789Sahrens 			 */
9202082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9212082Seschrock 			    "one or more vdevs refer to the same device"));
9222082Seschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
923789Sahrens 
924789Sahrens 		case EOVERFLOW:
925789Sahrens 			/*
9262082Seschrock 			 * This occurs when one of the devices is below
927789Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
928789Sahrens 			 * device was the problem device since there's no
929789Sahrens 			 * reliable way to determine device size from userland.
930789Sahrens 			 */
931789Sahrens 			{
932789Sahrens 				char buf[64];
933789Sahrens 
934789Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
935789Sahrens 
9362082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9372082Seschrock 				    "one or more devices is less than the "
9382082Seschrock 				    "minimum size (%s)"), buf);
939789Sahrens 			}
9402082Seschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
941789Sahrens 
942789Sahrens 		case ENOSPC:
9432082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9442082Seschrock 			    "one or more devices is out of space"));
9452082Seschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
946789Sahrens 
9475450Sbrendan 		case ENOTBLK:
9485450Sbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9495450Sbrendan 			    "cache device must be a disk or disk slice"));
9505450Sbrendan 			return (zfs_error(hdl, EZFS_BADDEV, msg));
9515450Sbrendan 
952789Sahrens 		default:
9532082Seschrock 			return (zpool_standard_error(hdl, errno, msg));
954789Sahrens 		}
955789Sahrens 	}
956789Sahrens 
957789Sahrens 	/*
958789Sahrens 	 * If this is an alternate root pool, then we automatically set the
9592676Seschrock 	 * mountpoint of the root dataset to be '/'.
960789Sahrens 	 */
9615094Slling 	if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
9625094Slling 	    &altroot) == 0) {
963789Sahrens 		zfs_handle_t *zhp;
964789Sahrens 
9655094Slling 		verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
9662676Seschrock 		verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
9672676Seschrock 		    "/") == 0);
968789Sahrens 
969789Sahrens 		zfs_close(zhp);
970789Sahrens 	}
971789Sahrens 
9727184Stimh create_failed:
9735320Slling 	zcmd_free_nvlists(&zc);
9747184Stimh 	nvlist_free(zc_props);
9757184Stimh 	nvlist_free(zc_fsprops);
9767184Stimh 	return (ret);
977789Sahrens }
978789Sahrens 
979789Sahrens /*
980789Sahrens  * Destroy the given pool.  It is up to the caller to ensure that there are no
981789Sahrens  * datasets left in the pool.
982789Sahrens  */
983789Sahrens int
984789Sahrens zpool_destroy(zpool_handle_t *zhp)
985789Sahrens {
986789Sahrens 	zfs_cmd_t zc = { 0 };
987789Sahrens 	zfs_handle_t *zfp = NULL;
9882082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
9892082Seschrock 	char msg[1024];
990789Sahrens 
991789Sahrens 	if (zhp->zpool_state == POOL_STATE_ACTIVE &&
9922082Seschrock 	    (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name,
9932082Seschrock 	    ZFS_TYPE_FILESYSTEM)) == NULL)
994789Sahrens 		return (-1);
995789Sahrens 
9962856Snd150628 	if (zpool_remove_zvol_links(zhp) != 0)
997789Sahrens 		return (-1);
998789Sahrens 
999789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1000789Sahrens 
10014543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
10022082Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
10032082Seschrock 		    "cannot destroy '%s'"), zhp->zpool_name);
1004789Sahrens 
10052082Seschrock 		if (errno == EROFS) {
10062082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10072082Seschrock 			    "one or more devices is read only"));
10082082Seschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
10092082Seschrock 		} else {
10102082Seschrock 			(void) zpool_standard_error(hdl, errno, msg);
1011789Sahrens 		}
1012789Sahrens 
1013789Sahrens 		if (zfp)
1014789Sahrens 			zfs_close(zfp);
1015789Sahrens 		return (-1);
1016789Sahrens 	}
1017789Sahrens 
1018789Sahrens 	if (zfp) {
1019789Sahrens 		remove_mountpoint(zfp);
1020789Sahrens 		zfs_close(zfp);
1021789Sahrens 	}
1022789Sahrens 
1023789Sahrens 	return (0);
1024789Sahrens }
1025789Sahrens 
1026789Sahrens /*
1027789Sahrens  * Add the given vdevs to the pool.  The caller must have already performed the
1028789Sahrens  * necessary verification to ensure that the vdev specification is well-formed.
1029789Sahrens  */
1030789Sahrens int
1031789Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1032789Sahrens {
10332676Seschrock 	zfs_cmd_t zc = { 0 };
10342082Seschrock 	int ret;
10352082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
10362082Seschrock 	char msg[1024];
10375450Sbrendan 	nvlist_t **spares, **l2cache;
10385450Sbrendan 	uint_t nspares, nl2cache;
10392082Seschrock 
10402082Seschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
10412082Seschrock 	    "cannot add to '%s'"), zhp->zpool_name);
10422082Seschrock 
10435450Sbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
10445450Sbrendan 	    SPA_VERSION_SPARES &&
10452082Seschrock 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
10462082Seschrock 	    &spares, &nspares) == 0) {
10472082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
10482082Seschrock 		    "upgraded to add hot spares"));
10492082Seschrock 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
10502082Seschrock 	}
1051789Sahrens 
10527965SGeorge.Wilson@Sun.COM 	if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
10537965SGeorge.Wilson@Sun.COM 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
10547965SGeorge.Wilson@Sun.COM 		uint64_t s;
10557965SGeorge.Wilson@Sun.COM 
10567965SGeorge.Wilson@Sun.COM 		for (s = 0; s < nspares; s++) {
10577965SGeorge.Wilson@Sun.COM 			char *path;
10587965SGeorge.Wilson@Sun.COM 
10597965SGeorge.Wilson@Sun.COM 			if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
10607965SGeorge.Wilson@Sun.COM 			    &path) == 0 && pool_uses_efi(spares[s])) {
10617965SGeorge.Wilson@Sun.COM 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10627965SGeorge.Wilson@Sun.COM 				    "device '%s' contains an EFI label and "
10637965SGeorge.Wilson@Sun.COM 				    "cannot be used on root pools."),
10647965SGeorge.Wilson@Sun.COM 				    zpool_vdev_name(hdl, NULL, spares[s]));
10657965SGeorge.Wilson@Sun.COM 				return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
10667965SGeorge.Wilson@Sun.COM 			}
10677965SGeorge.Wilson@Sun.COM 		}
10687965SGeorge.Wilson@Sun.COM 	}
10697965SGeorge.Wilson@Sun.COM 
10705450Sbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
10715450Sbrendan 	    SPA_VERSION_L2CACHE &&
10725450Sbrendan 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
10735450Sbrendan 	    &l2cache, &nl2cache) == 0) {
10745450Sbrendan 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
10755450Sbrendan 		    "upgraded to add cache devices"));
10765450Sbrendan 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
10775450Sbrendan 	}
10785450Sbrendan 
10795094Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
10802082Seschrock 		return (-1);
1081789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1082789Sahrens 
10834543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1084789Sahrens 		switch (errno) {
1085789Sahrens 		case EBUSY:
1086789Sahrens 			/*
1087789Sahrens 			 * This can happen if the user has specified the same
1088789Sahrens 			 * device multiple times.  We can't reliably detect this
1089789Sahrens 			 * until we try to add it and see we already have a
1090789Sahrens 			 * label.
1091789Sahrens 			 */
10922082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10932082Seschrock 			    "one or more vdevs refer to the same device"));
10942082Seschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1095789Sahrens 			break;
1096789Sahrens 
1097789Sahrens 		case EOVERFLOW:
1098789Sahrens 			/*
1099789Sahrens 			 * This occurrs when one of the devices is below
1100789Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1101789Sahrens 			 * device was the problem device since there's no
1102789Sahrens 			 * reliable way to determine device size from userland.
1103789Sahrens 			 */
1104789Sahrens 			{
1105789Sahrens 				char buf[64];
1106789Sahrens 
1107789Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1108789Sahrens 
11092082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11102082Seschrock 				    "device is less than the minimum "
11112082Seschrock 				    "size (%s)"), buf);
1112789Sahrens 			}
11132082Seschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
11142082Seschrock 			break;
11152082Seschrock 
11162082Seschrock 		case ENOTSUP:
11172082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11184527Sperrin 			    "pool must be upgraded to add these vdevs"));
11192082Seschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, msg);
1120789Sahrens 			break;
1121789Sahrens 
11223912Slling 		case EDOM:
11233912Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11244527Sperrin 			    "root pool can not have multiple vdevs"
11254527Sperrin 			    " or separate logs"));
11263912Slling 			(void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
11273912Slling 			break;
11283912Slling 
11295450Sbrendan 		case ENOTBLK:
11305450Sbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11315450Sbrendan 			    "cache device must be a disk or disk slice"));
11325450Sbrendan 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
11335450Sbrendan 			break;
11345450Sbrendan 
1135789Sahrens 		default:
11362082Seschrock 			(void) zpool_standard_error(hdl, errno, msg);
1137789Sahrens 		}
1138789Sahrens 
11392082Seschrock 		ret = -1;
11402082Seschrock 	} else {
11412082Seschrock 		ret = 0;
1142789Sahrens 	}
1143789Sahrens 
11442676Seschrock 	zcmd_free_nvlists(&zc);
1145789Sahrens 
11462082Seschrock 	return (ret);
1147789Sahrens }
1148789Sahrens 
1149789Sahrens /*
1150789Sahrens  * Exports the pool from the system.  The caller must ensure that there are no
1151789Sahrens  * mounted datasets in the pool.
1152789Sahrens  */
1153789Sahrens int
11548211SGeorge.Wilson@Sun.COM zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce)
1155789Sahrens {
1156789Sahrens 	zfs_cmd_t zc = { 0 };
11577214Slling 	char msg[1024];
1158789Sahrens 
1159789Sahrens 	if (zpool_remove_zvol_links(zhp) != 0)
1160789Sahrens 		return (-1);
1161789Sahrens 
11627214Slling 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
11637214Slling 	    "cannot export '%s'"), zhp->zpool_name);
11647214Slling 
1165789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
11667214Slling 	zc.zc_cookie = force;
11678211SGeorge.Wilson@Sun.COM 	zc.zc_guid = hardforce;
11687214Slling 
11697214Slling 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
11707214Slling 		switch (errno) {
11717214Slling 		case EXDEV:
11727214Slling 			zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
11737214Slling 			    "use '-f' to override the following errors:\n"
11747214Slling 			    "'%s' has an active shared spare which could be"
11757214Slling 			    " used by other pools once '%s' is exported."),
11767214Slling 			    zhp->zpool_name, zhp->zpool_name);
11777214Slling 			return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
11787214Slling 			    msg));
11797214Slling 		default:
11807214Slling 			return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
11817214Slling 			    msg));
11827214Slling 		}
11837214Slling 	}
11847214Slling 
1185789Sahrens 	return (0);
1186789Sahrens }
1187789Sahrens 
11888211SGeorge.Wilson@Sun.COM int
11898211SGeorge.Wilson@Sun.COM zpool_export(zpool_handle_t *zhp, boolean_t force)
11908211SGeorge.Wilson@Sun.COM {
11918211SGeorge.Wilson@Sun.COM 	return (zpool_export_common(zhp, force, B_FALSE));
11928211SGeorge.Wilson@Sun.COM }
11938211SGeorge.Wilson@Sun.COM 
11948211SGeorge.Wilson@Sun.COM int
11958211SGeorge.Wilson@Sun.COM zpool_export_force(zpool_handle_t *zhp)
11968211SGeorge.Wilson@Sun.COM {
11978211SGeorge.Wilson@Sun.COM 	return (zpool_export_common(zhp, B_TRUE, B_TRUE));
11988211SGeorge.Wilson@Sun.COM }
11998211SGeorge.Wilson@Sun.COM 
1200789Sahrens /*
12015094Slling  * zpool_import() is a contracted interface. Should be kept the same
12025094Slling  * if possible.
12035094Slling  *
12045094Slling  * Applications should use zpool_import_props() to import a pool with
12055094Slling  * new properties value to be set.
1206789Sahrens  */
1207789Sahrens int
12082082Seschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
12095094Slling     char *altroot)
12105094Slling {
12115094Slling 	nvlist_t *props = NULL;
12125094Slling 	int ret;
12135094Slling 
12145094Slling 	if (altroot != NULL) {
12155094Slling 		if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
12165094Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
12175094Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
12185094Slling 			    newname));
12195094Slling 		}
12205094Slling 
12215094Slling 		if (nvlist_add_string(props,
12228084SGeorge.Wilson@Sun.COM 		    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
12238084SGeorge.Wilson@Sun.COM 		    nvlist_add_string(props,
12248084SGeorge.Wilson@Sun.COM 		    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
12255094Slling 			nvlist_free(props);
12265094Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
12275094Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
12285094Slling 			    newname));
12295094Slling 		}
12305094Slling 	}
12315094Slling 
12326643Seschrock 	ret = zpool_import_props(hdl, config, newname, props, B_FALSE);
12335094Slling 	if (props)
12345094Slling 		nvlist_free(props);
12355094Slling 	return (ret);
12365094Slling }
12375094Slling 
12385094Slling /*
12395094Slling  * Import the given pool using the known configuration and a list of
12405094Slling  * properties to be set. The configuration should have come from
12415094Slling  * zpool_find_import(). The 'newname' parameters control whether the pool
12425094Slling  * is imported with a different name.
12435094Slling  */
12445094Slling int
12455094Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
12466643Seschrock     nvlist_t *props, boolean_t importfaulted)
1247789Sahrens {
12482676Seschrock 	zfs_cmd_t zc = { 0 };
1249789Sahrens 	char *thename;
1250789Sahrens 	char *origname;
1251789Sahrens 	int ret;
12525094Slling 	char errbuf[1024];
1253789Sahrens 
1254789Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1255789Sahrens 	    &origname) == 0);
1256789Sahrens 
12575094Slling 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
12585094Slling 	    "cannot import pool '%s'"), origname);
12595094Slling 
1260789Sahrens 	if (newname != NULL) {
12612082Seschrock 		if (!zpool_name_valid(hdl, B_FALSE, newname))
12623237Slling 			return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
12632082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
12642082Seschrock 			    newname));
1265789Sahrens 		thename = (char *)newname;
1266789Sahrens 	} else {
1267789Sahrens 		thename = origname;
1268789Sahrens 	}
1269789Sahrens 
12705094Slling 	if (props) {
12715094Slling 		uint64_t version;
12725094Slling 
12735094Slling 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
12745094Slling 		    &version) == 0);
12755094Slling 
12767184Stimh 		if ((props = zpool_valid_proplist(hdl, origname,
12775320Slling 		    props, version, B_TRUE, errbuf)) == NULL) {
12785094Slling 			return (-1);
12795320Slling 		} else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
12805320Slling 			nvlist_free(props);
12815094Slling 			return (-1);
12825320Slling 		}
12835094Slling 	}
1284789Sahrens 
1285789Sahrens 	(void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1286789Sahrens 
1287789Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
12881544Seschrock 	    &zc.zc_guid) == 0);
1289789Sahrens 
12905320Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
12915320Slling 		nvlist_free(props);
12922082Seschrock 		return (-1);
12935320Slling 	}
1294789Sahrens 
12956643Seschrock 	zc.zc_cookie = (uint64_t)importfaulted;
1296789Sahrens 	ret = 0;
12974543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) {
1298789Sahrens 		char desc[1024];
1299789Sahrens 		if (newname == NULL)
1300789Sahrens 			(void) snprintf(desc, sizeof (desc),
1301789Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1302789Sahrens 			    thename);
1303789Sahrens 		else
1304789Sahrens 			(void) snprintf(desc, sizeof (desc),
1305789Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1306789Sahrens 			    origname, thename);
1307789Sahrens 
1308789Sahrens 		switch (errno) {
13091544Seschrock 		case ENOTSUP:
13101544Seschrock 			/*
13111544Seschrock 			 * Unsupported version.
13121544Seschrock 			 */
13132082Seschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, desc);
13141544Seschrock 			break;
13151544Seschrock 
13162174Seschrock 		case EINVAL:
13172174Seschrock 			(void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
13182174Seschrock 			break;
13192174Seschrock 
1320789Sahrens 		default:
13212082Seschrock 			(void) zpool_standard_error(hdl, errno, desc);
1322789Sahrens 		}
1323789Sahrens 
1324789Sahrens 		ret = -1;
1325789Sahrens 	} else {
1326789Sahrens 		zpool_handle_t *zhp;
13274543Smarks 
1328789Sahrens 		/*
1329789Sahrens 		 * This should never fail, but play it safe anyway.
1330789Sahrens 		 */
13312142Seschrock 		if (zpool_open_silent(hdl, thename, &zhp) != 0) {
13322142Seschrock 			ret = -1;
13332142Seschrock 		} else if (zhp != NULL) {
1334789Sahrens 			ret = zpool_create_zvol_links(zhp);
1335789Sahrens 			zpool_close(zhp);
1336789Sahrens 		}
13374543Smarks 
1338789Sahrens 	}
1339789Sahrens 
13402676Seschrock 	zcmd_free_nvlists(&zc);
13415320Slling 	nvlist_free(props);
13425320Slling 
1343789Sahrens 	return (ret);
1344789Sahrens }
1345789Sahrens 
1346789Sahrens /*
1347789Sahrens  * Scrub the pool.
1348789Sahrens  */
1349789Sahrens int
1350789Sahrens zpool_scrub(zpool_handle_t *zhp, pool_scrub_type_t type)
1351789Sahrens {
1352789Sahrens 	zfs_cmd_t zc = { 0 };
1353789Sahrens 	char msg[1024];
13542082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1355789Sahrens 
1356789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1357789Sahrens 	zc.zc_cookie = type;
1358789Sahrens 
13594543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCRUB, &zc) == 0)
1360789Sahrens 		return (0);
1361789Sahrens 
1362789Sahrens 	(void) snprintf(msg, sizeof (msg),
1363789Sahrens 	    dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1364789Sahrens 
13652082Seschrock 	if (errno == EBUSY)
13662082Seschrock 		return (zfs_error(hdl, EZFS_RESILVERING, msg));
13672082Seschrock 	else
13682082Seschrock 		return (zpool_standard_error(hdl, errno, msg));
1369789Sahrens }
1370789Sahrens 
13712468Sek110237 /*
13722468Sek110237  * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
13732468Sek110237  * spare; but FALSE if its an INUSE spare.
13742468Sek110237  */
13752082Seschrock static nvlist_t *
13762082Seschrock vdev_to_nvlist_iter(nvlist_t *nv, const char *search, uint64_t guid,
13777326SEric.Schrock@Sun.COM     boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
13781544Seschrock {
13791544Seschrock 	uint_t c, children;
13801544Seschrock 	nvlist_t **child;
13812082Seschrock 	uint64_t theguid, present;
13821544Seschrock 	char *path;
13831544Seschrock 	uint64_t wholedisk = 0;
13842082Seschrock 	nvlist_t *ret;
13857326SEric.Schrock@Sun.COM 	uint64_t is_log;
13861544Seschrock 
13872082Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &theguid) == 0);
13881544Seschrock 
13891544Seschrock 	if (search == NULL &&
13901544Seschrock 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &present) == 0) {
13911544Seschrock 		/*
13921544Seschrock 		 * If the device has never been present since import, the only
13931544Seschrock 		 * reliable way to match the vdev is by GUID.
13941544Seschrock 		 */
13952082Seschrock 		if (theguid == guid)
13962082Seschrock 			return (nv);
13971544Seschrock 	} else if (search != NULL &&
13981544Seschrock 	    nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
13991544Seschrock 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
14001544Seschrock 		    &wholedisk);
14011544Seschrock 		if (wholedisk) {
14021544Seschrock 			/*
14031544Seschrock 			 * For whole disks, the internal path has 's0', but the
14041544Seschrock 			 * path passed in by the user doesn't.
14051544Seschrock 			 */
14061544Seschrock 			if (strlen(search) == strlen(path) - 2 &&
14071544Seschrock 			    strncmp(search, path, strlen(search)) == 0)
14082082Seschrock 				return (nv);
14091544Seschrock 		} else if (strcmp(search, path) == 0) {
14102082Seschrock 			return (nv);
14111544Seschrock 		}
14121544Seschrock 	}
14131544Seschrock 
14141544Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
14151544Seschrock 	    &child, &children) != 0)
14162082Seschrock 		return (NULL);
14171544Seschrock 
14187326SEric.Schrock@Sun.COM 	for (c = 0; c < children; c++) {
14192082Seschrock 		if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
14207326SEric.Schrock@Sun.COM 		    avail_spare, l2cache, NULL)) != NULL) {
14217326SEric.Schrock@Sun.COM 			/*
14227326SEric.Schrock@Sun.COM 			 * The 'is_log' value is only set for the toplevel
14237326SEric.Schrock@Sun.COM 			 * vdev, not the leaf vdevs.  So we always lookup the
14247326SEric.Schrock@Sun.COM 			 * log device from the root of the vdev tree (where
14257326SEric.Schrock@Sun.COM 			 * 'log' is non-NULL).
14267326SEric.Schrock@Sun.COM 			 */
14277326SEric.Schrock@Sun.COM 			if (log != NULL &&
14287326SEric.Schrock@Sun.COM 			    nvlist_lookup_uint64(child[c],
14297326SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
14307326SEric.Schrock@Sun.COM 			    is_log) {
14317326SEric.Schrock@Sun.COM 				*log = B_TRUE;
14327326SEric.Schrock@Sun.COM 			}
14331544Seschrock 			return (ret);
14347326SEric.Schrock@Sun.COM 		}
14357326SEric.Schrock@Sun.COM 	}
14361544Seschrock 
14372082Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
14382082Seschrock 	    &child, &children) == 0) {
14392082Seschrock 		for (c = 0; c < children; c++) {
14402082Seschrock 			if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
14417326SEric.Schrock@Sun.COM 			    avail_spare, l2cache, NULL)) != NULL) {
14422468Sek110237 				*avail_spare = B_TRUE;
14432082Seschrock 				return (ret);
14442082Seschrock 			}
14452082Seschrock 		}
14462082Seschrock 	}
14472082Seschrock 
14485450Sbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
14495450Sbrendan 	    &child, &children) == 0) {
14505450Sbrendan 		for (c = 0; c < children; c++) {
14515450Sbrendan 			if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
14527326SEric.Schrock@Sun.COM 			    avail_spare, l2cache, NULL)) != NULL) {
14535450Sbrendan 				*l2cache = B_TRUE;
14545450Sbrendan 				return (ret);
14555450Sbrendan 			}
14565450Sbrendan 		}
14575450Sbrendan 	}
14585450Sbrendan 
14592082Seschrock 	return (NULL);
14601544Seschrock }
14611544Seschrock 
14622082Seschrock nvlist_t *
14635450Sbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
14647326SEric.Schrock@Sun.COM     boolean_t *l2cache, boolean_t *log)
14651544Seschrock {
14661544Seschrock 	char buf[MAXPATHLEN];
14671544Seschrock 	const char *search;
14681544Seschrock 	char *end;
14691544Seschrock 	nvlist_t *nvroot;
14701544Seschrock 	uint64_t guid;
14711544Seschrock 
14721613Seschrock 	guid = strtoull(path, &end, 10);
14731544Seschrock 	if (guid != 0 && *end == '\0') {
14741544Seschrock 		search = NULL;
14751544Seschrock 	} else if (path[0] != '/') {
14761544Seschrock 		(void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path);
14771544Seschrock 		search = buf;
14781544Seschrock 	} else {
14791544Seschrock 		search = path;
14801544Seschrock 	}
14811544Seschrock 
14821544Seschrock 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
14831544Seschrock 	    &nvroot) == 0);
14841544Seschrock 
14852468Sek110237 	*avail_spare = B_FALSE;
14865450Sbrendan 	*l2cache = B_FALSE;
14877326SEric.Schrock@Sun.COM 	if (log != NULL)
14887326SEric.Schrock@Sun.COM 		*log = B_FALSE;
14895450Sbrendan 	return (vdev_to_nvlist_iter(nvroot, search, guid, avail_spare,
14907326SEric.Schrock@Sun.COM 	    l2cache, log));
14912468Sek110237 }
14922468Sek110237 
14937656SSherry.Moore@Sun.COM static int
14947656SSherry.Moore@Sun.COM vdev_online(nvlist_t *nv)
14957656SSherry.Moore@Sun.COM {
14967656SSherry.Moore@Sun.COM 	uint64_t ival;
14977656SSherry.Moore@Sun.COM 
14987656SSherry.Moore@Sun.COM 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
14997656SSherry.Moore@Sun.COM 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
15007656SSherry.Moore@Sun.COM 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
15017656SSherry.Moore@Sun.COM 		return (0);
15027656SSherry.Moore@Sun.COM 
15037656SSherry.Moore@Sun.COM 	return (1);
15047656SSherry.Moore@Sun.COM }
15057656SSherry.Moore@Sun.COM 
15067656SSherry.Moore@Sun.COM /*
1507*9160SSherry.Moore@Sun.COM  * Helper function for zpool_get_config_physpath().
15087656SSherry.Moore@Sun.COM  */
1509*9160SSherry.Moore@Sun.COM static int
1510*9160SSherry.Moore@Sun.COM vdev_get_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
1511*9160SSherry.Moore@Sun.COM     size_t *bytes_written)
15127656SSherry.Moore@Sun.COM {
1513*9160SSherry.Moore@Sun.COM 	size_t bytes_left, pos, rsz;
1514*9160SSherry.Moore@Sun.COM 	char *tmppath;
1515*9160SSherry.Moore@Sun.COM 	const char *format;
1516*9160SSherry.Moore@Sun.COM 
1517*9160SSherry.Moore@Sun.COM 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
1518*9160SSherry.Moore@Sun.COM 	    &tmppath) != 0)
1519*9160SSherry.Moore@Sun.COM 		return (EZFS_NODEVICE);
1520*9160SSherry.Moore@Sun.COM 
1521*9160SSherry.Moore@Sun.COM 	pos = *bytes_written;
1522*9160SSherry.Moore@Sun.COM 	bytes_left = physpath_size - pos;
1523*9160SSherry.Moore@Sun.COM 	format = (pos == 0) ? "%s" : " %s";
1524*9160SSherry.Moore@Sun.COM 
1525*9160SSherry.Moore@Sun.COM 	rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
1526*9160SSherry.Moore@Sun.COM 	*bytes_written += rsz;
1527*9160SSherry.Moore@Sun.COM 
1528*9160SSherry.Moore@Sun.COM 	if (rsz >= bytes_left) {
1529*9160SSherry.Moore@Sun.COM 		/* if physpath was not copied properly, clear it */
1530*9160SSherry.Moore@Sun.COM 		if (bytes_left != 0) {
1531*9160SSherry.Moore@Sun.COM 			physpath[pos] = 0;
1532*9160SSherry.Moore@Sun.COM 		}
1533*9160SSherry.Moore@Sun.COM 		return (EZFS_NOSPC);
1534*9160SSherry.Moore@Sun.COM 	}
1535*9160SSherry.Moore@Sun.COM 	return (0);
1536*9160SSherry.Moore@Sun.COM }
1537*9160SSherry.Moore@Sun.COM 
1538*9160SSherry.Moore@Sun.COM /*
1539*9160SSherry.Moore@Sun.COM  * Get phys_path for a root pool config.
1540*9160SSherry.Moore@Sun.COM  * Return 0 on success; non-zero on failure.
1541*9160SSherry.Moore@Sun.COM  */
1542*9160SSherry.Moore@Sun.COM static int
1543*9160SSherry.Moore@Sun.COM zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
1544*9160SSherry.Moore@Sun.COM {
1545*9160SSherry.Moore@Sun.COM 	size_t rsz;
15467656SSherry.Moore@Sun.COM 	nvlist_t *vdev_root;
15477656SSherry.Moore@Sun.COM 	nvlist_t **child;
1548*9160SSherry.Moore@Sun.COM 	nvlist_t **child2;
15497656SSherry.Moore@Sun.COM 	uint_t count;
1550*9160SSherry.Moore@Sun.COM 	char *type;
1551*9160SSherry.Moore@Sun.COM 	int j, ret;
1552*9160SSherry.Moore@Sun.COM 
1553*9160SSherry.Moore@Sun.COM 	rsz = 0;
1554*9160SSherry.Moore@Sun.COM 
1555*9160SSherry.Moore@Sun.COM 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1556*9160SSherry.Moore@Sun.COM 	    &vdev_root) != 0)
1557*9160SSherry.Moore@Sun.COM 		return (EZFS_INVALCONFIG);
1558*9160SSherry.Moore@Sun.COM 
1559*9160SSherry.Moore@Sun.COM 	if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
1560*9160SSherry.Moore@Sun.COM 	    nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
1561*9160SSherry.Moore@Sun.COM 	    &child, &count) != 0)
1562*9160SSherry.Moore@Sun.COM 		return (EZFS_INVALCONFIG);
15637656SSherry.Moore@Sun.COM 
15647656SSherry.Moore@Sun.COM 	/*
1565*9160SSherry.Moore@Sun.COM 	 * root pool can not have EFI labeled disks and can only have
1566*9160SSherry.Moore@Sun.COM 	 * a single top-level vdev.
15677656SSherry.Moore@Sun.COM 	 */
1568*9160SSherry.Moore@Sun.COM 	if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
1569*9160SSherry.Moore@Sun.COM 	    pool_uses_efi(vdev_root))
1570*9160SSherry.Moore@Sun.COM 		return (EZFS_POOL_INVALARG);
1571*9160SSherry.Moore@Sun.COM 
1572*9160SSherry.Moore@Sun.COM 	if (nvlist_lookup_string(child[0], ZPOOL_CONFIG_TYPE, &type) != 0)
1573*9160SSherry.Moore@Sun.COM 		return (EZFS_INVALCONFIG);
1574*9160SSherry.Moore@Sun.COM 
1575*9160SSherry.Moore@Sun.COM 	if (strcmp(type, VDEV_TYPE_DISK) == 0) {
1576*9160SSherry.Moore@Sun.COM 		if (vdev_online(child[0])) {
1577*9160SSherry.Moore@Sun.COM 			if ((ret = vdev_get_physpath(child[0], physpath,
1578*9160SSherry.Moore@Sun.COM 			    phypath_size, &rsz)) != 0)
1579*9160SSherry.Moore@Sun.COM 				return (ret);
1580*9160SSherry.Moore@Sun.COM 		}
1581*9160SSherry.Moore@Sun.COM 	} else if (strcmp(type, VDEV_TYPE_MIRROR) == 0) {
1582*9160SSherry.Moore@Sun.COM 
1583*9160SSherry.Moore@Sun.COM 		if (nvlist_lookup_nvlist_array(child[0],
1584*9160SSherry.Moore@Sun.COM 		    ZPOOL_CONFIG_CHILDREN, &child2, &count) != 0)
1585*9160SSherry.Moore@Sun.COM 			return (EZFS_INVALCONFIG);
1586*9160SSherry.Moore@Sun.COM 
1587*9160SSherry.Moore@Sun.COM 		for (j = 0; j < count; j++) {
1588*9160SSherry.Moore@Sun.COM 			if (nvlist_lookup_string(child2[j], ZPOOL_CONFIG_TYPE,
1589*9160SSherry.Moore@Sun.COM 			    &type) != 0)
1590*9160SSherry.Moore@Sun.COM 				return (EZFS_INVALCONFIG);
1591*9160SSherry.Moore@Sun.COM 
1592*9160SSherry.Moore@Sun.COM 			if (strcmp(type, VDEV_TYPE_DISK) != 0)
1593*9160SSherry.Moore@Sun.COM 				return (EZFS_POOL_INVALARG);
1594*9160SSherry.Moore@Sun.COM 
1595*9160SSherry.Moore@Sun.COM 			if (vdev_online(child2[j])) {
1596*9160SSherry.Moore@Sun.COM 				ret = vdev_get_physpath(child2[j],
1597*9160SSherry.Moore@Sun.COM 				    physpath, phypath_size, &rsz);
1598*9160SSherry.Moore@Sun.COM 
1599*9160SSherry.Moore@Sun.COM 				if (ret == EZFS_NOSPC)
1600*9160SSherry.Moore@Sun.COM 					return (ret);
16017656SSherry.Moore@Sun.COM 			}
16027656SSherry.Moore@Sun.COM 		}
1603*9160SSherry.Moore@Sun.COM 	} else {
1604*9160SSherry.Moore@Sun.COM 		return (EZFS_POOL_INVALARG);
16057656SSherry.Moore@Sun.COM 	}
16067656SSherry.Moore@Sun.COM 
1607*9160SSherry.Moore@Sun.COM 	/* No online devices */
1608*9160SSherry.Moore@Sun.COM 	if (rsz == 0)
1609*9160SSherry.Moore@Sun.COM 		return (EZFS_NODEVICE);
1610*9160SSherry.Moore@Sun.COM 
16117656SSherry.Moore@Sun.COM 	return (0);
16127656SSherry.Moore@Sun.COM }
16137656SSherry.Moore@Sun.COM 
16142468Sek110237 /*
1615*9160SSherry.Moore@Sun.COM  * Get phys_path for a root pool
1616*9160SSherry.Moore@Sun.COM  * Return 0 on success; non-zero on failure.
1617*9160SSherry.Moore@Sun.COM  */
1618*9160SSherry.Moore@Sun.COM int
1619*9160SSherry.Moore@Sun.COM zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
1620*9160SSherry.Moore@Sun.COM {
1621*9160SSherry.Moore@Sun.COM 	return (zpool_get_config_physpath(zhp->zpool_config, physpath,
1622*9160SSherry.Moore@Sun.COM 	    phypath_size));
1623*9160SSherry.Moore@Sun.COM }
1624*9160SSherry.Moore@Sun.COM 
1625*9160SSherry.Moore@Sun.COM /*
16265450Sbrendan  * Returns TRUE if the given guid corresponds to the given type.
16275450Sbrendan  * This is used to check for hot spares (INUSE or not), and level 2 cache
16285450Sbrendan  * devices.
16292468Sek110237  */
16302468Sek110237 static boolean_t
16315450Sbrendan is_guid_type(zpool_handle_t *zhp, uint64_t guid, const char *type)
16322468Sek110237 {
16335450Sbrendan 	uint64_t target_guid;
16342468Sek110237 	nvlist_t *nvroot;
16355450Sbrendan 	nvlist_t **list;
16365450Sbrendan 	uint_t count;
16372468Sek110237 	int i;
16382468Sek110237 
16392468Sek110237 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
16402468Sek110237 	    &nvroot) == 0);
16415450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, type, &list, &count) == 0) {
16425450Sbrendan 		for (i = 0; i < count; i++) {
16435450Sbrendan 			verify(nvlist_lookup_uint64(list[i], ZPOOL_CONFIG_GUID,
16445450Sbrendan 			    &target_guid) == 0);
16455450Sbrendan 			if (guid == target_guid)
16462468Sek110237 				return (B_TRUE);
16472468Sek110237 		}
16482468Sek110237 	}
16492468Sek110237 
16502468Sek110237 	return (B_FALSE);
16511544Seschrock }
16521544Seschrock 
1653789Sahrens /*
16544451Seschrock  * Bring the specified vdev online.   The 'flags' parameter is a set of the
16554451Seschrock  * ZFS_ONLINE_* flags.
1656789Sahrens  */
1657789Sahrens int
16584451Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
16594451Seschrock     vdev_state_t *newstate)
1660789Sahrens {
1661789Sahrens 	zfs_cmd_t zc = { 0 };
1662789Sahrens 	char msg[1024];
16632082Seschrock 	nvlist_t *tgt;
16645450Sbrendan 	boolean_t avail_spare, l2cache;
16652082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1666789Sahrens 
16671544Seschrock 	(void) snprintf(msg, sizeof (msg),
16681544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot online %s"), path);
1669789Sahrens 
16701544Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
16717326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
16727326SEric.Schrock@Sun.COM 	    NULL)) == NULL)
16732082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
1674789Sahrens 
16752468Sek110237 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
16762468Sek110237 
16775450Sbrendan 	if (avail_spare ||
16785450Sbrendan 	    is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
16792082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
16802082Seschrock 
16814451Seschrock 	zc.zc_cookie = VDEV_STATE_ONLINE;
16824451Seschrock 	zc.zc_obj = flags;
16834451Seschrock 
16844543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0)
16854451Seschrock 		return (zpool_standard_error(hdl, errno, msg));
16864451Seschrock 
16874451Seschrock 	*newstate = zc.zc_cookie;
16884451Seschrock 	return (0);
1689789Sahrens }
1690789Sahrens 
1691789Sahrens /*
1692789Sahrens  * Take the specified vdev offline
1693789Sahrens  */
1694789Sahrens int
16954451Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
1696789Sahrens {
1697789Sahrens 	zfs_cmd_t zc = { 0 };
1698789Sahrens 	char msg[1024];
16992082Seschrock 	nvlist_t *tgt;
17005450Sbrendan 	boolean_t avail_spare, l2cache;
17012082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1702789Sahrens 
17031544Seschrock 	(void) snprintf(msg, sizeof (msg),
17041544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
17051544Seschrock 
1706789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
17077326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
17087326SEric.Schrock@Sun.COM 	    NULL)) == NULL)
17092082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
17102082Seschrock 
17112468Sek110237 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
17122468Sek110237 
17135450Sbrendan 	if (avail_spare ||
17145450Sbrendan 	    is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
17152082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
17162082Seschrock 
17174451Seschrock 	zc.zc_cookie = VDEV_STATE_OFFLINE;
17184451Seschrock 	zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
17191485Slling 
17204543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
1721789Sahrens 		return (0);
1722789Sahrens 
1723789Sahrens 	switch (errno) {
17242082Seschrock 	case EBUSY:
1725789Sahrens 
1726789Sahrens 		/*
1727789Sahrens 		 * There are no other replicas of this device.
1728789Sahrens 		 */
17292082Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
17302082Seschrock 
17312082Seschrock 	default:
17322082Seschrock 		return (zpool_standard_error(hdl, errno, msg));
17332082Seschrock 	}
17342082Seschrock }
1735789Sahrens 
17362082Seschrock /*
17374451Seschrock  * Mark the given vdev faulted.
17384451Seschrock  */
17394451Seschrock int
17404451Seschrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid)
17414451Seschrock {
17424451Seschrock 	zfs_cmd_t zc = { 0 };
17434451Seschrock 	char msg[1024];
17444451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
17454451Seschrock 
17464451Seschrock 	(void) snprintf(msg, sizeof (msg),
17474451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
17484451Seschrock 
17494451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
17504451Seschrock 	zc.zc_guid = guid;
17514451Seschrock 	zc.zc_cookie = VDEV_STATE_FAULTED;
17524451Seschrock 
17534451Seschrock 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
17544451Seschrock 		return (0);
17554451Seschrock 
17564451Seschrock 	switch (errno) {
17574451Seschrock 	case EBUSY:
17584451Seschrock 
17594451Seschrock 		/*
17604451Seschrock 		 * There are no other replicas of this device.
17614451Seschrock 		 */
17624451Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
17634451Seschrock 
17644451Seschrock 	default:
17654451Seschrock 		return (zpool_standard_error(hdl, errno, msg));
17664451Seschrock 	}
17674451Seschrock 
17684451Seschrock }
17694451Seschrock 
17704451Seschrock /*
17714451Seschrock  * Mark the given vdev degraded.
17724451Seschrock  */
17734451Seschrock int
17744451Seschrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid)
17754451Seschrock {
17764451Seschrock 	zfs_cmd_t zc = { 0 };
17774451Seschrock 	char msg[1024];
17784451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
17794451Seschrock 
17804451Seschrock 	(void) snprintf(msg, sizeof (msg),
17814451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
17824451Seschrock 
17834451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
17844451Seschrock 	zc.zc_guid = guid;
17854451Seschrock 	zc.zc_cookie = VDEV_STATE_DEGRADED;
17864451Seschrock 
17874451Seschrock 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
17884451Seschrock 		return (0);
17894451Seschrock 
17904451Seschrock 	return (zpool_standard_error(hdl, errno, msg));
17914451Seschrock }
17924451Seschrock 
17934451Seschrock /*
17942082Seschrock  * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
17952082Seschrock  * a hot spare.
17962082Seschrock  */
17972082Seschrock static boolean_t
17982082Seschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
17992082Seschrock {
18002082Seschrock 	nvlist_t **child;
18012082Seschrock 	uint_t c, children;
18022082Seschrock 	char *type;
18032082Seschrock 
18042082Seschrock 	if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
18052082Seschrock 	    &children) == 0) {
18062082Seschrock 		verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
18072082Seschrock 		    &type) == 0);
18082082Seschrock 
18092082Seschrock 		if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
18102082Seschrock 		    children == 2 && child[which] == tgt)
18112082Seschrock 			return (B_TRUE);
18122082Seschrock 
18132082Seschrock 		for (c = 0; c < children; c++)
18142082Seschrock 			if (is_replacing_spare(child[c], tgt, which))
18152082Seschrock 				return (B_TRUE);
1816789Sahrens 	}
18172082Seschrock 
18182082Seschrock 	return (B_FALSE);
1819789Sahrens }
1820789Sahrens 
1821789Sahrens /*
1822789Sahrens  * Attach new_disk (fully described by nvroot) to old_disk.
18234527Sperrin  * If 'replacing' is specified, the new disk will replace the old one.
1824789Sahrens  */
1825789Sahrens int
1826789Sahrens zpool_vdev_attach(zpool_handle_t *zhp,
1827789Sahrens     const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
1828789Sahrens {
1829789Sahrens 	zfs_cmd_t zc = { 0 };
1830789Sahrens 	char msg[1024];
1831789Sahrens 	int ret;
18322082Seschrock 	nvlist_t *tgt;
18337326SEric.Schrock@Sun.COM 	boolean_t avail_spare, l2cache, islog;
18347326SEric.Schrock@Sun.COM 	uint64_t val;
18357041Seschrock 	char *path, *newname;
18362082Seschrock 	nvlist_t **child;
18372082Seschrock 	uint_t children;
18382082Seschrock 	nvlist_t *config_root;
18392082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
18407965SGeorge.Wilson@Sun.COM 	boolean_t rootpool = pool_is_bootable(zhp);
1841789Sahrens 
18421544Seschrock 	if (replacing)
18431544Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
18441544Seschrock 		    "cannot replace %s with %s"), old_disk, new_disk);
18451544Seschrock 	else
18461544Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
18471544Seschrock 		    "cannot attach %s to %s"), new_disk, old_disk);
18481544Seschrock 
18497965SGeorge.Wilson@Sun.COM 	/*
18507965SGeorge.Wilson@Sun.COM 	 * If this is a root pool, make sure that we're not attaching an
18517965SGeorge.Wilson@Sun.COM 	 * EFI labeled device.
18527965SGeorge.Wilson@Sun.COM 	 */
18537965SGeorge.Wilson@Sun.COM 	if (rootpool && pool_uses_efi(nvroot)) {
18547965SGeorge.Wilson@Sun.COM 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18557965SGeorge.Wilson@Sun.COM 		    "EFI labeled devices are not supported on root pools."));
18567965SGeorge.Wilson@Sun.COM 		return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
18577965SGeorge.Wilson@Sun.COM 	}
18587965SGeorge.Wilson@Sun.COM 
1859789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
18607326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
18617326SEric.Schrock@Sun.COM 	    &islog)) == 0)
18622082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
18632082Seschrock 
18642468Sek110237 	if (avail_spare)
18652082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
18662082Seschrock 
18675450Sbrendan 	if (l2cache)
18685450Sbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
18695450Sbrendan 
18702082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
18712082Seschrock 	zc.zc_cookie = replacing;
18722082Seschrock 
18732082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
18742082Seschrock 	    &child, &children) != 0 || children != 1) {
18752082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18762082Seschrock 		    "new device must be a single disk"));
18772082Seschrock 		return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
18781544Seschrock 	}
18792082Seschrock 
18802082Seschrock 	verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
18812082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
18822082Seschrock 
18837041Seschrock 	if ((newname = zpool_vdev_name(NULL, NULL, child[0])) == NULL)
18847041Seschrock 		return (-1);
18857041Seschrock 
18862082Seschrock 	/*
18872082Seschrock 	 * If the target is a hot spare that has been swapped in, we can only
18882082Seschrock 	 * replace it with another hot spare.
18892082Seschrock 	 */
18902082Seschrock 	if (replacing &&
18912082Seschrock 	    nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
18927326SEric.Schrock@Sun.COM 	    (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
18937326SEric.Schrock@Sun.COM 	    NULL) == NULL || !avail_spare) &&
18947326SEric.Schrock@Sun.COM 	    is_replacing_spare(config_root, tgt, 1)) {
18952082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18962082Seschrock 		    "can only be replaced by another hot spare"));
18977041Seschrock 		free(newname);
18982082Seschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
18992082Seschrock 	}
19002082Seschrock 
19012082Seschrock 	/*
19022082Seschrock 	 * If we are attempting to replace a spare, it canot be applied to an
19032082Seschrock 	 * already spared device.
19042082Seschrock 	 */
19052082Seschrock 	if (replacing &&
19062082Seschrock 	    nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 &&
19077326SEric.Schrock@Sun.COM 	    zpool_find_vdev(zhp, newname, &avail_spare,
19087326SEric.Schrock@Sun.COM 	    &l2cache, NULL) != NULL && avail_spare &&
19097326SEric.Schrock@Sun.COM 	    is_replacing_spare(config_root, tgt, 0)) {
19102082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19112082Seschrock 		    "device has already been replaced with a spare"));
19127041Seschrock 		free(newname);
19132082Seschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
19142082Seschrock 	}
1915789Sahrens 
19167041Seschrock 	free(newname);
19177041Seschrock 
19185094Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
19192082Seschrock 		return (-1);
1920789Sahrens 
19214543Smarks 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc);
1922789Sahrens 
19232676Seschrock 	zcmd_free_nvlists(&zc);
1924789Sahrens 
19257965SGeorge.Wilson@Sun.COM 	if (ret == 0) {
19267965SGeorge.Wilson@Sun.COM 		if (rootpool) {
19277965SGeorge.Wilson@Sun.COM 			/*
19287965SGeorge.Wilson@Sun.COM 			 * XXX - This should be removed once we can
19297965SGeorge.Wilson@Sun.COM 			 * automatically install the bootblocks on the
19307965SGeorge.Wilson@Sun.COM 			 * newly attached disk.
19317965SGeorge.Wilson@Sun.COM 			 */
19327965SGeorge.Wilson@Sun.COM 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Please "
19337965SGeorge.Wilson@Sun.COM 			    "be sure to invoke %s to make '%s' bootable.\n"),
19347965SGeorge.Wilson@Sun.COM 			    BOOTCMD, new_disk);
19357965SGeorge.Wilson@Sun.COM 		}
1936789Sahrens 		return (0);
19377965SGeorge.Wilson@Sun.COM 	}
1938789Sahrens 
1939789Sahrens 	switch (errno) {
19401544Seschrock 	case ENOTSUP:
1941789Sahrens 		/*
1942789Sahrens 		 * Can't attach to or replace this type of vdev.
1943789Sahrens 		 */
19444527Sperrin 		if (replacing) {
19457326SEric.Schrock@Sun.COM 			if (islog)
19464527Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19474527Sperrin 				    "cannot replace a log with a spare"));
19484527Sperrin 			else
19494527Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19504527Sperrin 				    "cannot replace a replacing device"));
19514527Sperrin 		} else {
19522082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19532082Seschrock 			    "can only attach to mirrors and top-level "
19542082Seschrock 			    "disks"));
19554527Sperrin 		}
19562082Seschrock 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
1957789Sahrens 		break;
1958789Sahrens 
19591544Seschrock 	case EINVAL:
1960789Sahrens 		/*
1961789Sahrens 		 * The new device must be a single disk.
1962789Sahrens 		 */
19632082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19642082Seschrock 		    "new device must be a single disk"));
19652082Seschrock 		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
1966789Sahrens 		break;
1967789Sahrens 
19681544Seschrock 	case EBUSY:
19692082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
19702082Seschrock 		    new_disk);
19712082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1972789Sahrens 		break;
1973789Sahrens 
19741544Seschrock 	case EOVERFLOW:
1975789Sahrens 		/*
1976789Sahrens 		 * The new device is too small.
1977789Sahrens 		 */
19782082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19792082Seschrock 		    "device is too small"));
19802082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1981789Sahrens 		break;
1982789Sahrens 
19831544Seschrock 	case EDOM:
1984789Sahrens 		/*
1985789Sahrens 		 * The new device has a different alignment requirement.
1986789Sahrens 		 */
19872082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19882082Seschrock 		    "devices have different sector alignment"));
19892082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1990789Sahrens 		break;
1991789Sahrens 
19921544Seschrock 	case ENAMETOOLONG:
1993789Sahrens 		/*
1994789Sahrens 		 * The resulting top-level vdev spec won't fit in the label.
1995789Sahrens 		 */
19962082Seschrock 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
1997789Sahrens 		break;
1998789Sahrens 
19991544Seschrock 	default:
20002082Seschrock 		(void) zpool_standard_error(hdl, errno, msg);
2001789Sahrens 	}
2002789Sahrens 
20032082Seschrock 	return (-1);
2004789Sahrens }
2005789Sahrens 
2006789Sahrens /*
2007789Sahrens  * Detach the specified device.
2008789Sahrens  */
2009789Sahrens int
2010789Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2011789Sahrens {
2012789Sahrens 	zfs_cmd_t zc = { 0 };
2013789Sahrens 	char msg[1024];
20142082Seschrock 	nvlist_t *tgt;
20155450Sbrendan 	boolean_t avail_spare, l2cache;
20162082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2017789Sahrens 
20181544Seschrock 	(void) snprintf(msg, sizeof (msg),
20191544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
20201544Seschrock 
2021789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
20227326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
20237326SEric.Schrock@Sun.COM 	    NULL)) == 0)
20242082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2025789Sahrens 
20262468Sek110237 	if (avail_spare)
20272082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
20282082Seschrock 
20295450Sbrendan 	if (l2cache)
20305450Sbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
20315450Sbrendan 
20322082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
20332082Seschrock 
20344543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2035789Sahrens 		return (0);
2036789Sahrens 
2037789Sahrens 	switch (errno) {
2038789Sahrens 
20391544Seschrock 	case ENOTSUP:
2040789Sahrens 		/*
2041789Sahrens 		 * Can't detach from this type of vdev.
2042789Sahrens 		 */
20432082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
20442082Seschrock 		    "applicable to mirror and replacing vdevs"));
20452082Seschrock 		(void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg);
2046789Sahrens 		break;
2047789Sahrens 
20481544Seschrock 	case EBUSY:
2049789Sahrens 		/*
2050789Sahrens 		 * There are no other replicas of this device.
2051789Sahrens 		 */
20522082Seschrock 		(void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2053789Sahrens 		break;
2054789Sahrens 
20551544Seschrock 	default:
20562082Seschrock 		(void) zpool_standard_error(hdl, errno, msg);
20571544Seschrock 	}
20581544Seschrock 
20592082Seschrock 	return (-1);
20602082Seschrock }
20612082Seschrock 
20622082Seschrock /*
20635450Sbrendan  * Remove the given device.  Currently, this is supported only for hot spares
20645450Sbrendan  * and level 2 cache devices.
20652082Seschrock  */
20662082Seschrock int
20672082Seschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
20682082Seschrock {
20692082Seschrock 	zfs_cmd_t zc = { 0 };
20702082Seschrock 	char msg[1024];
20712082Seschrock 	nvlist_t *tgt;
20725450Sbrendan 	boolean_t avail_spare, l2cache;
20732082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
20742082Seschrock 
20752082Seschrock 	(void) snprintf(msg, sizeof (msg),
20762082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
20772082Seschrock 
20782082Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
20797326SEric.Schrock@Sun.COM 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
20807326SEric.Schrock@Sun.COM 	    NULL)) == 0)
20812082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
20822082Seschrock 
20835450Sbrendan 	if (!avail_spare && !l2cache) {
20842082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
20855450Sbrendan 		    "only inactive hot spares or cache devices "
20865450Sbrendan 		    "can be removed"));
20872082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
20882082Seschrock 	}
20892082Seschrock 
20902082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
20912082Seschrock 
20924543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
20932082Seschrock 		return (0);
20942082Seschrock 
20952082Seschrock 	return (zpool_standard_error(hdl, errno, msg));
20961544Seschrock }
20971544Seschrock 
20981544Seschrock /*
20991544Seschrock  * Clear the errors for the pool, or the particular device if specified.
21001544Seschrock  */
21011544Seschrock int
21021544Seschrock zpool_clear(zpool_handle_t *zhp, const char *path)
21031544Seschrock {
21041544Seschrock 	zfs_cmd_t zc = { 0 };
21051544Seschrock 	char msg[1024];
21062082Seschrock 	nvlist_t *tgt;
21075450Sbrendan 	boolean_t avail_spare, l2cache;
21082082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
21091544Seschrock 
21101544Seschrock 	if (path)
21111544Seschrock 		(void) snprintf(msg, sizeof (msg),
21121544Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
21132676Seschrock 		    path);
21141544Seschrock 	else
21151544Seschrock 		(void) snprintf(msg, sizeof (msg),
21161544Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
21171544Seschrock 		    zhp->zpool_name);
21181544Seschrock 
21191544Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
21202082Seschrock 	if (path) {
21215450Sbrendan 		if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
21227326SEric.Schrock@Sun.COM 		    &l2cache, NULL)) == 0)
21232082Seschrock 			return (zfs_error(hdl, EZFS_NODEVICE, msg));
21242082Seschrock 
21255450Sbrendan 		/*
21265450Sbrendan 		 * Don't allow error clearing for hot spares.  Do allow
21275450Sbrendan 		 * error clearing for l2cache devices.
21285450Sbrendan 		 */
21292468Sek110237 		if (avail_spare)
21302082Seschrock 			return (zfs_error(hdl, EZFS_ISSPARE, msg));
21312082Seschrock 
21322082Seschrock 		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
21332082Seschrock 		    &zc.zc_guid) == 0);
21341544Seschrock 	}
21351544Seschrock 
21364543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0)
21371544Seschrock 		return (0);
21381544Seschrock 
21392082Seschrock 	return (zpool_standard_error(hdl, errno, msg));
2140789Sahrens }
2141789Sahrens 
21423126Sahl /*
21434451Seschrock  * Similar to zpool_clear(), but takes a GUID (used by fmd).
21444451Seschrock  */
21454451Seschrock int
21464451Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
21474451Seschrock {
21484451Seschrock 	zfs_cmd_t zc = { 0 };
21494451Seschrock 	char msg[1024];
21504451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
21514451Seschrock 
21524451Seschrock 	(void) snprintf(msg, sizeof (msg),
21534451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
21544451Seschrock 	    guid);
21554451Seschrock 
21564451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
21574451Seschrock 	zc.zc_guid = guid;
21584451Seschrock 
21594451Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
21604451Seschrock 		return (0);
21614451Seschrock 
21624451Seschrock 	return (zpool_standard_error(hdl, errno, msg));
21634451Seschrock }
21644451Seschrock 
21654451Seschrock /*
21663126Sahl  * Iterate over all zvols in a given pool by walking the /dev/zvol/dsk/<pool>
21673126Sahl  * hierarchy.
21683126Sahl  */
21693126Sahl int
21703126Sahl zpool_iter_zvol(zpool_handle_t *zhp, int (*cb)(const char *, void *),
21713126Sahl     void *data)
2172789Sahrens {
21733126Sahl 	libzfs_handle_t *hdl = zhp->zpool_hdl;
21743126Sahl 	char (*paths)[MAXPATHLEN];
21753126Sahl 	size_t size = 4;
21763126Sahl 	int curr, fd, base, ret = 0;
21773126Sahl 	DIR *dirp;
21783126Sahl 	struct dirent *dp;
21793126Sahl 	struct stat st;
21803126Sahl 
21813126Sahl 	if ((base = open("/dev/zvol/dsk", O_RDONLY)) < 0)
21823126Sahl 		return (errno == ENOENT ? 0 : -1);
21833126Sahl 
21843126Sahl 	if (fstatat(base, zhp->zpool_name, &st, 0) != 0) {
21853126Sahl 		int err = errno;
21863126Sahl 		(void) close(base);
21873126Sahl 		return (err == ENOENT ? 0 : -1);
21883126Sahl 	}
2189789Sahrens 
2190789Sahrens 	/*
21913126Sahl 	 * Oddly this wasn't a directory -- ignore that failure since we
21923126Sahl 	 * know there are no links lower in the (non-existant) hierarchy.
2193789Sahrens 	 */
21943126Sahl 	if (!S_ISDIR(st.st_mode)) {
21953126Sahl 		(void) close(base);
21963126Sahl 		return (0);
21973126Sahl 	}
21983126Sahl 
21993126Sahl 	if ((paths = zfs_alloc(hdl, size * sizeof (paths[0]))) == NULL) {
22003126Sahl 		(void) close(base);
22013126Sahl 		return (-1);
2202789Sahrens 	}
2203789Sahrens 
22043126Sahl 	(void) strlcpy(paths[0], zhp->zpool_name, sizeof (paths[0]));
22053126Sahl 	curr = 0;
22063126Sahl 
22073126Sahl 	while (curr >= 0) {
22083126Sahl 		if (fstatat(base, paths[curr], &st, AT_SYMLINK_NOFOLLOW) != 0)
22093126Sahl 			goto err;
22103126Sahl 
22113126Sahl 		if (S_ISDIR(st.st_mode)) {
22123126Sahl 			if ((fd = openat(base, paths[curr], O_RDONLY)) < 0)
22133126Sahl 				goto err;
22143126Sahl 
22153126Sahl 			if ((dirp = fdopendir(fd)) == NULL) {
22163126Sahl 				(void) close(fd);
22173126Sahl 				goto err;
22183126Sahl 			}
22193126Sahl 
22203126Sahl 			while ((dp = readdir(dirp)) != NULL) {
22213126Sahl 				if (dp->d_name[0] == '.')
22223126Sahl 					continue;
22233126Sahl 
22243126Sahl 				if (curr + 1 == size) {
22253126Sahl 					paths = zfs_realloc(hdl, paths,
22263126Sahl 					    size * sizeof (paths[0]),
22273126Sahl 					    size * 2 * sizeof (paths[0]));
22283126Sahl 					if (paths == NULL) {
22293126Sahl 						(void) closedir(dirp);
22303126Sahl 						(void) close(fd);
22313126Sahl 						goto err;
22323126Sahl 					}
22333126Sahl 
22343126Sahl 					size *= 2;
22353126Sahl 				}
22363126Sahl 
22373126Sahl 				(void) strlcpy(paths[curr + 1], paths[curr],
22383126Sahl 				    sizeof (paths[curr + 1]));
22393126Sahl 				(void) strlcat(paths[curr], "/",
22403126Sahl 				    sizeof (paths[curr]));
22413126Sahl 				(void) strlcat(paths[curr], dp->d_name,
22423126Sahl 				    sizeof (paths[curr]));
22433126Sahl 				curr++;
22443126Sahl 			}
22453126Sahl 
22463126Sahl 			(void) closedir(dirp);
22473126Sahl 
22483126Sahl 		} else {
22493126Sahl 			if ((ret = cb(paths[curr], data)) != 0)
22503126Sahl 				break;
22513126Sahl 		}
22523126Sahl 
22533126Sahl 		curr--;
22543126Sahl 	}
22553126Sahl 
22563126Sahl 	free(paths);
22573126Sahl 	(void) close(base);
22583126Sahl 
22593126Sahl 	return (ret);
22603126Sahl 
22613126Sahl err:
22623126Sahl 	free(paths);
22633126Sahl 	(void) close(base);
22643126Sahl 	return (-1);
22653126Sahl }
22663126Sahl 
22673126Sahl typedef struct zvol_cb {
22683126Sahl 	zpool_handle_t *zcb_pool;
22693126Sahl 	boolean_t zcb_create;
22703126Sahl } zvol_cb_t;
22713126Sahl 
22723126Sahl /*ARGSUSED*/
22733126Sahl static int
22743126Sahl do_zvol_create(zfs_handle_t *zhp, void *data)
22753126Sahl {
22764657Sahrens 	int ret = 0;
22773126Sahl 
22784657Sahrens 	if (ZFS_IS_VOLUME(zhp)) {
22793126Sahl 		(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
22804657Sahrens 		ret = zfs_iter_snapshots(zhp, do_zvol_create, NULL);
22814657Sahrens 	}
22823126Sahl 
22834657Sahrens 	if (ret == 0)
22844657Sahrens 		ret = zfs_iter_filesystems(zhp, do_zvol_create, NULL);
2285789Sahrens 
2286789Sahrens 	zfs_close(zhp);
22873126Sahl 
2288789Sahrens 	return (ret);
2289789Sahrens }
2290789Sahrens 
2291789Sahrens /*
2292789Sahrens  * Iterate over all zvols in the pool and make any necessary minor nodes.
2293789Sahrens  */
2294789Sahrens int
2295789Sahrens zpool_create_zvol_links(zpool_handle_t *zhp)
2296789Sahrens {
2297789Sahrens 	zfs_handle_t *zfp;
2298789Sahrens 	int ret;
2299789Sahrens 
2300789Sahrens 	/*
2301789Sahrens 	 * If the pool is unavailable, just return success.
2302789Sahrens 	 */
23032082Seschrock 	if ((zfp = make_dataset_handle(zhp->zpool_hdl,
23042082Seschrock 	    zhp->zpool_name)) == NULL)
2305789Sahrens 		return (0);
2306789Sahrens 
23074657Sahrens 	ret = zfs_iter_filesystems(zfp, do_zvol_create, NULL);
2308789Sahrens 
2309789Sahrens 	zfs_close(zfp);
2310789Sahrens 	return (ret);
2311789Sahrens }
2312789Sahrens 
23133126Sahl static int
23143126Sahl do_zvol_remove(const char *dataset, void *data)
23153126Sahl {
23163126Sahl 	zpool_handle_t *zhp = data;
23173126Sahl 
23183126Sahl 	return (zvol_remove_link(zhp->zpool_hdl, dataset));
23193126Sahl }
23203126Sahl 
2321789Sahrens /*
23223126Sahl  * Iterate over all zvols in the pool and remove any minor nodes.  We iterate
23233126Sahl  * by examining the /dev links so that a corrupted pool doesn't impede this
23243126Sahl  * operation.
2325789Sahrens  */
2326789Sahrens int
2327789Sahrens zpool_remove_zvol_links(zpool_handle_t *zhp)
2328789Sahrens {
23293126Sahl 	return (zpool_iter_zvol(zhp, do_zvol_remove, zhp));
2330789Sahrens }
23311354Seschrock 
23321354Seschrock /*
23331354Seschrock  * Convert from a devid string to a path.
23341354Seschrock  */
23351354Seschrock static char *
23361354Seschrock devid_to_path(char *devid_str)
23371354Seschrock {
23381354Seschrock 	ddi_devid_t devid;
23391354Seschrock 	char *minor;
23401354Seschrock 	char *path;
23411354Seschrock 	devid_nmlist_t *list = NULL;
23421354Seschrock 	int ret;
23431354Seschrock 
23441354Seschrock 	if (devid_str_decode(devid_str, &devid, &minor) != 0)
23451354Seschrock 		return (NULL);
23461354Seschrock 
23471354Seschrock 	ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
23481354Seschrock 
23491354Seschrock 	devid_str_free(minor);
23501354Seschrock 	devid_free(devid);
23511354Seschrock 
23521354Seschrock 	if (ret != 0)
23531354Seschrock 		return (NULL);
23541354Seschrock 
23552082Seschrock 	if ((path = strdup(list[0].devname)) == NULL)
23562082Seschrock 		return (NULL);
23572082Seschrock 
23581354Seschrock 	devid_free_nmlist(list);
23591354Seschrock 
23601354Seschrock 	return (path);
23611354Seschrock }
23621354Seschrock 
23631354Seschrock /*
23641354Seschrock  * Convert from a path to a devid string.
23651354Seschrock  */
23661354Seschrock static char *
23671354Seschrock path_to_devid(const char *path)
23681354Seschrock {
23691354Seschrock 	int fd;
23701354Seschrock 	ddi_devid_t devid;
23711354Seschrock 	char *minor, *ret;
23721354Seschrock 
23731354Seschrock 	if ((fd = open(path, O_RDONLY)) < 0)
23741354Seschrock 		return (NULL);
23751354Seschrock 
23761354Seschrock 	minor = NULL;
23771354Seschrock 	ret = NULL;
23781354Seschrock 	if (devid_get(fd, &devid) == 0) {
23791354Seschrock 		if (devid_get_minor_name(fd, &minor) == 0)
23801354Seschrock 			ret = devid_str_encode(devid, minor);
23811354Seschrock 		if (minor != NULL)
23821354Seschrock 			devid_str_free(minor);
23831354Seschrock 		devid_free(devid);
23841354Seschrock 	}
23851354Seschrock 	(void) close(fd);
23861354Seschrock 
23871354Seschrock 	return (ret);
23881354Seschrock }
23891354Seschrock 
23901354Seschrock /*
23911354Seschrock  * Issue the necessary ioctl() to update the stored path value for the vdev.  We
23921354Seschrock  * ignore any failure here, since a common case is for an unprivileged user to
23931354Seschrock  * type 'zpool status', and we'll display the correct information anyway.
23941354Seschrock  */
23951354Seschrock static void
23961354Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
23971354Seschrock {
23981354Seschrock 	zfs_cmd_t zc = { 0 };
23991354Seschrock 
24001354Seschrock 	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
24012676Seschrock 	(void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
24021354Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
24031544Seschrock 	    &zc.zc_guid) == 0);
24041354Seschrock 
24052082Seschrock 	(void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
24061354Seschrock }
24071354Seschrock 
24081354Seschrock /*
24091354Seschrock  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
24101354Seschrock  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
24111354Seschrock  * We also check if this is a whole disk, in which case we strip off the
24121354Seschrock  * trailing 's0' slice name.
24131354Seschrock  *
24141354Seschrock  * This routine is also responsible for identifying when disks have been
24151354Seschrock  * reconfigured in a new location.  The kernel will have opened the device by
24161354Seschrock  * devid, but the path will still refer to the old location.  To catch this, we
24171354Seschrock  * first do a path -> devid translation (which is fast for the common case).  If
24181354Seschrock  * the devid matches, we're done.  If not, we do a reverse devid -> path
24191354Seschrock  * translation and issue the appropriate ioctl() to update the path of the vdev.
24201354Seschrock  * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
24211354Seschrock  * of these checks.
24221354Seschrock  */
24231354Seschrock char *
24242082Seschrock zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv)
24251354Seschrock {
24261354Seschrock 	char *path, *devid;
24271544Seschrock 	uint64_t value;
24281544Seschrock 	char buf[64];
24294451Seschrock 	vdev_stat_t *vs;
24304451Seschrock 	uint_t vsc;
24311354Seschrock 
24321544Seschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
24331544Seschrock 	    &value) == 0) {
24341544Seschrock 		verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
24351544Seschrock 		    &value) == 0);
24362856Snd150628 		(void) snprintf(buf, sizeof (buf), "%llu",
24372856Snd150628 		    (u_longlong_t)value);
24381544Seschrock 		path = buf;
24391544Seschrock 	} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
24401354Seschrock 
24414451Seschrock 		/*
24424451Seschrock 		 * If the device is dead (faulted, offline, etc) then don't
24434451Seschrock 		 * bother opening it.  Otherwise we may be forcing the user to
24444451Seschrock 		 * open a misbehaving device, which can have undesirable
24454451Seschrock 		 * effects.
24464451Seschrock 		 */
24474451Seschrock 		if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
24484451Seschrock 		    (uint64_t **)&vs, &vsc) != 0 ||
24494451Seschrock 		    vs->vs_state >= VDEV_STATE_DEGRADED) &&
24504451Seschrock 		    zhp != NULL &&
24511354Seschrock 		    nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
24521354Seschrock 			/*
24531354Seschrock 			 * Determine if the current path is correct.
24541354Seschrock 			 */
24551354Seschrock 			char *newdevid = path_to_devid(path);
24561354Seschrock 
24571354Seschrock 			if (newdevid == NULL ||
24581354Seschrock 			    strcmp(devid, newdevid) != 0) {
24591354Seschrock 				char *newpath;
24601354Seschrock 
24611354Seschrock 				if ((newpath = devid_to_path(devid)) != NULL) {
24621354Seschrock 					/*
24631354Seschrock 					 * Update the path appropriately.
24641354Seschrock 					 */
24651354Seschrock 					set_path(zhp, nv, newpath);
24662082Seschrock 					if (nvlist_add_string(nv,
24672082Seschrock 					    ZPOOL_CONFIG_PATH, newpath) == 0)
24682082Seschrock 						verify(nvlist_lookup_string(nv,
24692082Seschrock 						    ZPOOL_CONFIG_PATH,
24702082Seschrock 						    &path) == 0);
24711354Seschrock 					free(newpath);
24721354Seschrock 				}
24731354Seschrock 			}
24741354Seschrock 
24752082Seschrock 			if (newdevid)
24762082Seschrock 				devid_str_free(newdevid);
24771354Seschrock 		}
24781354Seschrock 
24791354Seschrock 		if (strncmp(path, "/dev/dsk/", 9) == 0)
24801354Seschrock 			path += 9;
24811354Seschrock 
24821354Seschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
24831544Seschrock 		    &value) == 0 && value) {
24842082Seschrock 			char *tmp = zfs_strdup(hdl, path);
24852082Seschrock 			if (tmp == NULL)
24862082Seschrock 				return (NULL);
24871354Seschrock 			tmp[strlen(path) - 2] = '\0';
24881354Seschrock 			return (tmp);
24891354Seschrock 		}
24901354Seschrock 	} else {
24911354Seschrock 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
24922082Seschrock 
24932082Seschrock 		/*
24942082Seschrock 		 * If it's a raidz device, we need to stick in the parity level.
24952082Seschrock 		 */
24962082Seschrock 		if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
24972082Seschrock 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
24982082Seschrock 			    &value) == 0);
24992082Seschrock 			(void) snprintf(buf, sizeof (buf), "%s%llu", path,
25002856Snd150628 			    (u_longlong_t)value);
25012082Seschrock 			path = buf;
25022082Seschrock 		}
25031354Seschrock 	}
25041354Seschrock 
25052082Seschrock 	return (zfs_strdup(hdl, path));
25061354Seschrock }
25071544Seschrock 
25081544Seschrock static int
25091544Seschrock zbookmark_compare(const void *a, const void *b)
25101544Seschrock {
25111544Seschrock 	return (memcmp(a, b, sizeof (zbookmark_t)));
25121544Seschrock }
25131544Seschrock 
25141544Seschrock /*
25151544Seschrock  * Retrieve the persistent error log, uniquify the members, and return to the
25161544Seschrock  * caller.
25171544Seschrock  */
25181544Seschrock int
25193444Sek110237 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
25201544Seschrock {
25211544Seschrock 	zfs_cmd_t zc = { 0 };
25221544Seschrock 	uint64_t count;
25232676Seschrock 	zbookmark_t *zb = NULL;
25243444Sek110237 	int i;
25251544Seschrock 
25261544Seschrock 	/*
25271544Seschrock 	 * Retrieve the raw error list from the kernel.  If the number of errors
25281544Seschrock 	 * has increased, allocate more space and continue until we get the
25291544Seschrock 	 * entire list.
25301544Seschrock 	 */
25311544Seschrock 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
25321544Seschrock 	    &count) == 0);
25334820Sek110237 	if (count == 0)
25344820Sek110237 		return (0);
25352676Seschrock 	if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
25362856Snd150628 	    count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
25372082Seschrock 		return (-1);
25382676Seschrock 	zc.zc_nvlist_dst_size = count;
25391544Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
25401544Seschrock 	for (;;) {
25412082Seschrock 		if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
25422082Seschrock 		    &zc) != 0) {
25432676Seschrock 			free((void *)(uintptr_t)zc.zc_nvlist_dst);
25441544Seschrock 			if (errno == ENOMEM) {
25453823Svb160487 				count = zc.zc_nvlist_dst_size;
25462676Seschrock 				if ((zc.zc_nvlist_dst = (uintptr_t)
25473823Svb160487 				    zfs_alloc(zhp->zpool_hdl, count *
25483823Svb160487 				    sizeof (zbookmark_t))) == (uintptr_t)NULL)
25492082Seschrock 					return (-1);
25501544Seschrock 			} else {
25511544Seschrock 				return (-1);
25521544Seschrock 			}
25531544Seschrock 		} else {
25541544Seschrock 			break;
25551544Seschrock 		}
25561544Seschrock 	}
25571544Seschrock 
25581544Seschrock 	/*
25591544Seschrock 	 * Sort the resulting bookmarks.  This is a little confusing due to the
25601544Seschrock 	 * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
25612676Seschrock 	 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
25621544Seschrock 	 * _not_ copied as part of the process.  So we point the start of our
25631544Seschrock 	 * array appropriate and decrement the total number of elements.
25641544Seschrock 	 */
25652676Seschrock 	zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
25662676Seschrock 	    zc.zc_nvlist_dst_size;
25672676Seschrock 	count -= zc.zc_nvlist_dst_size;
25681544Seschrock 
25691544Seschrock 	qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
25701544Seschrock 
25713444Sek110237 	verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
25721544Seschrock 
25731544Seschrock 	/*
25743444Sek110237 	 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
25751544Seschrock 	 */
25761544Seschrock 	for (i = 0; i < count; i++) {
25771544Seschrock 		nvlist_t *nv;
25781544Seschrock 
25793700Sek110237 		/* ignoring zb_blkid and zb_level for now */
25803700Sek110237 		if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
25813700Sek110237 		    zb[i-1].zb_object == zb[i].zb_object)
25821544Seschrock 			continue;
25831544Seschrock 
25843444Sek110237 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
25853444Sek110237 			goto nomem;
25863444Sek110237 		if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
25873444Sek110237 		    zb[i].zb_objset) != 0) {
25883444Sek110237 			nvlist_free(nv);
25892082Seschrock 			goto nomem;
25903444Sek110237 		}
25913444Sek110237 		if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
25923444Sek110237 		    zb[i].zb_object) != 0) {
25933444Sek110237 			nvlist_free(nv);
25943444Sek110237 			goto nomem;
25951544Seschrock 		}
25963444Sek110237 		if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
25973444Sek110237 			nvlist_free(nv);
25983444Sek110237 			goto nomem;
25993444Sek110237 		}
26003444Sek110237 		nvlist_free(nv);
26011544Seschrock 	}
26021544Seschrock 
26033265Sahrens 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
26041544Seschrock 	return (0);
26052082Seschrock 
26062082Seschrock nomem:
26072676Seschrock 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
26082082Seschrock 	return (no_memory(zhp->zpool_hdl));
26091544Seschrock }
26101760Seschrock 
26111760Seschrock /*
26121760Seschrock  * Upgrade a ZFS pool to the latest on-disk version.
26131760Seschrock  */
26141760Seschrock int
26155094Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
26161760Seschrock {
26171760Seschrock 	zfs_cmd_t zc = { 0 };
26182082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
26191760Seschrock 
26201760Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
26215094Slling 	zc.zc_cookie = new_version;
26225094Slling 
26234543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
26243237Slling 		return (zpool_standard_error_fmt(hdl, errno,
26252082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
26262082Seschrock 		    zhp->zpool_name));
26271760Seschrock 	return (0);
26281760Seschrock }
26292926Sek110237 
26304988Sek110237 void
26314988Sek110237 zpool_set_history_str(const char *subcommand, int argc, char **argv,
26324988Sek110237     char *history_str)
26334988Sek110237 {
26344988Sek110237 	int i;
26354988Sek110237 
26364988Sek110237 	(void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
26374988Sek110237 	for (i = 1; i < argc; i++) {
26384988Sek110237 		if (strlen(history_str) + 1 + strlen(argv[i]) >
26394988Sek110237 		    HIS_MAX_RECORD_LEN)
26404988Sek110237 			break;
26414988Sek110237 		(void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
26424988Sek110237 		(void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
26434988Sek110237 	}
26444988Sek110237 }
26454988Sek110237 
26462926Sek110237 /*
26474988Sek110237  * Stage command history for logging.
26482926Sek110237  */
26494988Sek110237 int
26504988Sek110237 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
26512926Sek110237 {
26524988Sek110237 	if (history_str == NULL)
26534988Sek110237 		return (EINVAL);
26544988Sek110237 
26554988Sek110237 	if (strlen(history_str) > HIS_MAX_RECORD_LEN)
26564988Sek110237 		return (EINVAL);
26572926Sek110237 
26584715Sek110237 	if (hdl->libzfs_log_str != NULL)
26594543Smarks 		free(hdl->libzfs_log_str);
26602926Sek110237 
26614988Sek110237 	if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
26624988Sek110237 		return (no_memory(hdl));
26634543Smarks 
26644988Sek110237 	return (0);
26652926Sek110237 }
26662926Sek110237 
26672926Sek110237 /*
26682926Sek110237  * Perform ioctl to get some command history of a pool.
26692926Sek110237  *
26702926Sek110237  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
26712926Sek110237  * logical offset of the history buffer to start reading from.
26722926Sek110237  *
26732926Sek110237  * Upon return, 'off' is the next logical offset to read from and
26742926Sek110237  * 'len' is the actual amount of bytes read into 'buf'.
26752926Sek110237  */
26762926Sek110237 static int
26772926Sek110237 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
26782926Sek110237 {
26792926Sek110237 	zfs_cmd_t zc = { 0 };
26802926Sek110237 	libzfs_handle_t *hdl = zhp->zpool_hdl;
26812926Sek110237 
26822926Sek110237 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
26832926Sek110237 
26842926Sek110237 	zc.zc_history = (uint64_t)(uintptr_t)buf;
26852926Sek110237 	zc.zc_history_len = *len;
26862926Sek110237 	zc.zc_history_offset = *off;
26872926Sek110237 
26882926Sek110237 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
26892926Sek110237 		switch (errno) {
26902926Sek110237 		case EPERM:
26913237Slling 			return (zfs_error_fmt(hdl, EZFS_PERM,
26923237Slling 			    dgettext(TEXT_DOMAIN,
26932926Sek110237 			    "cannot show history for pool '%s'"),
26942926Sek110237 			    zhp->zpool_name));
26952926Sek110237 		case ENOENT:
26963237Slling 			return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
26972926Sek110237 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
26982926Sek110237 			    "'%s'"), zhp->zpool_name));
26993863Sek110237 		case ENOTSUP:
27003863Sek110237 			return (zfs_error_fmt(hdl, EZFS_BADVERSION,
27013863Sek110237 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
27023863Sek110237 			    "'%s', pool must be upgraded"), zhp->zpool_name));
27032926Sek110237 		default:
27043237Slling 			return (zpool_standard_error_fmt(hdl, errno,
27052926Sek110237 			    dgettext(TEXT_DOMAIN,
27062926Sek110237 			    "cannot get history for '%s'"), zhp->zpool_name));
27072926Sek110237 		}
27082926Sek110237 	}
27092926Sek110237 
27102926Sek110237 	*len = zc.zc_history_len;
27112926Sek110237 	*off = zc.zc_history_offset;
27122926Sek110237 
27132926Sek110237 	return (0);
27142926Sek110237 }
27152926Sek110237 
27162926Sek110237 /*
27172926Sek110237  * Process the buffer of nvlists, unpacking and storing each nvlist record
27182926Sek110237  * into 'records'.  'leftover' is set to the number of bytes that weren't
27192926Sek110237  * processed as there wasn't a complete record.
27202926Sek110237  */
27212926Sek110237 static int
27222926Sek110237 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
27232926Sek110237     nvlist_t ***records, uint_t *numrecords)
27242926Sek110237 {
27252926Sek110237 	uint64_t reclen;
27262926Sek110237 	nvlist_t *nv;
27272926Sek110237 	int i;
27282926Sek110237 
27292926Sek110237 	while (bytes_read > sizeof (reclen)) {
27302926Sek110237 
27312926Sek110237 		/* get length of packed record (stored as little endian) */
27322926Sek110237 		for (i = 0, reclen = 0; i < sizeof (reclen); i++)
27332926Sek110237 			reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
27342926Sek110237 
27352926Sek110237 		if (bytes_read < sizeof (reclen) + reclen)
27362926Sek110237 			break;
27372926Sek110237 
27382926Sek110237 		/* unpack record */
27392926Sek110237 		if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
27402926Sek110237 			return (ENOMEM);
27412926Sek110237 		bytes_read -= sizeof (reclen) + reclen;
27422926Sek110237 		buf += sizeof (reclen) + reclen;
27432926Sek110237 
27442926Sek110237 		/* add record to nvlist array */
27452926Sek110237 		(*numrecords)++;
27462926Sek110237 		if (ISP2(*numrecords + 1)) {
27472926Sek110237 			*records = realloc(*records,
27482926Sek110237 			    *numrecords * 2 * sizeof (nvlist_t *));
27492926Sek110237 		}
27502926Sek110237 		(*records)[*numrecords - 1] = nv;
27512926Sek110237 	}
27522926Sek110237 
27532926Sek110237 	*leftover = bytes_read;
27542926Sek110237 	return (0);
27552926Sek110237 }
27562926Sek110237 
27572926Sek110237 #define	HIS_BUF_LEN	(128*1024)
27582926Sek110237 
27592926Sek110237 /*
27602926Sek110237  * Retrieve the command history of a pool.
27612926Sek110237  */
27622926Sek110237 int
27632926Sek110237 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
27642926Sek110237 {
27652926Sek110237 	char buf[HIS_BUF_LEN];
27662926Sek110237 	uint64_t off = 0;
27672926Sek110237 	nvlist_t **records = NULL;
27682926Sek110237 	uint_t numrecords = 0;
27692926Sek110237 	int err, i;
27702926Sek110237 
27712926Sek110237 	do {
27722926Sek110237 		uint64_t bytes_read = sizeof (buf);
27732926Sek110237 		uint64_t leftover;
27742926Sek110237 
27752926Sek110237 		if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
27762926Sek110237 			break;
27772926Sek110237 
27782926Sek110237 		/* if nothing else was read in, we're at EOF, just return */
27792926Sek110237 		if (!bytes_read)
27802926Sek110237 			break;
27812926Sek110237 
27822926Sek110237 		if ((err = zpool_history_unpack(buf, bytes_read,
27832926Sek110237 		    &leftover, &records, &numrecords)) != 0)
27842926Sek110237 			break;
27852926Sek110237 		off -= leftover;
27862926Sek110237 
27872926Sek110237 		/* CONSTCOND */
27882926Sek110237 	} while (1);
27892926Sek110237 
27902926Sek110237 	if (!err) {
27912926Sek110237 		verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
27922926Sek110237 		verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
27932926Sek110237 		    records, numrecords) == 0);
27942926Sek110237 	}
27952926Sek110237 	for (i = 0; i < numrecords; i++)
27962926Sek110237 		nvlist_free(records[i]);
27972926Sek110237 	free(records);
27982926Sek110237 
27992926Sek110237 	return (err);
28002926Sek110237 }
28013444Sek110237 
28023444Sek110237 void
28033444Sek110237 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
28043444Sek110237     char *pathname, size_t len)
28053444Sek110237 {
28063444Sek110237 	zfs_cmd_t zc = { 0 };
28073444Sek110237 	boolean_t mounted = B_FALSE;
28083444Sek110237 	char *mntpnt = NULL;
28093444Sek110237 	char dsname[MAXNAMELEN];
28103444Sek110237 
28113444Sek110237 	if (dsobj == 0) {
28123444Sek110237 		/* special case for the MOS */
28133444Sek110237 		(void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
28143444Sek110237 		return;
28153444Sek110237 	}
28163444Sek110237 
28173444Sek110237 	/* get the dataset's name */
28183444Sek110237 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
28193444Sek110237 	zc.zc_obj = dsobj;
28203444Sek110237 	if (ioctl(zhp->zpool_hdl->libzfs_fd,
28213444Sek110237 	    ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
28223444Sek110237 		/* just write out a path of two object numbers */
28233444Sek110237 		(void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
28243444Sek110237 		    dsobj, obj);
28253444Sek110237 		return;
28263444Sek110237 	}
28273444Sek110237 	(void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
28283444Sek110237 
28293444Sek110237 	/* find out if the dataset is mounted */
28303444Sek110237 	mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
28313444Sek110237 
28323444Sek110237 	/* get the corrupted object's path */
28333444Sek110237 	(void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
28343444Sek110237 	zc.zc_obj = obj;
28353444Sek110237 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
28363444Sek110237 	    &zc) == 0) {
28373444Sek110237 		if (mounted) {
28383444Sek110237 			(void) snprintf(pathname, len, "%s%s", mntpnt,
28393444Sek110237 			    zc.zc_value);
28403444Sek110237 		} else {
28413444Sek110237 			(void) snprintf(pathname, len, "%s:%s",
28423444Sek110237 			    dsname, zc.zc_value);
28433444Sek110237 		}
28443444Sek110237 	} else {
28453444Sek110237 		(void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
28463444Sek110237 	}
28473444Sek110237 	free(mntpnt);
28483444Sek110237 }
28493912Slling 
28504276Staylor #define	RDISK_ROOT	"/dev/rdsk"
28514276Staylor #define	BACKUP_SLICE	"s2"
28524276Staylor /*
28534276Staylor  * Don't start the slice at the default block of 34; many storage
28544276Staylor  * devices will use a stripe width of 128k, so start there instead.
28554276Staylor  */
28564276Staylor #define	NEW_START_BLOCK	256
28574276Staylor 
28584276Staylor /*
28597042Sgw25295  * Read the EFI label from the config, if a label does not exist then
28607042Sgw25295  * pass back the error to the caller. If the caller has passed a non-NULL
28617042Sgw25295  * diskaddr argument then we set it to the starting address of the EFI
28627042Sgw25295  * partition.
28637042Sgw25295  */
28647042Sgw25295 static int
28657042Sgw25295 read_efi_label(nvlist_t *config, diskaddr_t *sb)
28667042Sgw25295 {
28677042Sgw25295 	char *path;
28687042Sgw25295 	int fd;
28697042Sgw25295 	char diskname[MAXPATHLEN];
28707042Sgw25295 	int err = -1;
28717042Sgw25295 
28727042Sgw25295 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
28737042Sgw25295 		return (err);
28747042Sgw25295 
28757042Sgw25295 	(void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
28767042Sgw25295 	    strrchr(path, '/'));
28777042Sgw25295 	if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
28787042Sgw25295 		struct dk_gpt *vtoc;
28797042Sgw25295 
28807042Sgw25295 		if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
28817042Sgw25295 			if (sb != NULL)
28827042Sgw25295 				*sb = vtoc->efi_parts[0].p_start;
28837042Sgw25295 			efi_free(vtoc);
28847042Sgw25295 		}
28857042Sgw25295 		(void) close(fd);
28867042Sgw25295 	}
28877042Sgw25295 	return (err);
28887042Sgw25295 }
28897042Sgw25295 
28907042Sgw25295 /*
28914276Staylor  * determine where a partition starts on a disk in the current
28924276Staylor  * configuration
28934276Staylor  */
28944276Staylor static diskaddr_t
28954276Staylor find_start_block(nvlist_t *config)
28964276Staylor {
28974276Staylor 	nvlist_t **child;
28984276Staylor 	uint_t c, children;
28994276Staylor 	diskaddr_t sb = MAXOFFSET_T;
29004276Staylor 	uint64_t wholedisk;
29014276Staylor 
29024276Staylor 	if (nvlist_lookup_nvlist_array(config,
29034276Staylor 	    ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
29044276Staylor 		if (nvlist_lookup_uint64(config,
29054276Staylor 		    ZPOOL_CONFIG_WHOLE_DISK,
29064276Staylor 		    &wholedisk) != 0 || !wholedisk) {
29074276Staylor 			return (MAXOFFSET_T);
29084276Staylor 		}
29097042Sgw25295 		if (read_efi_label(config, &sb) < 0)
29107042Sgw25295 			sb = MAXOFFSET_T;
29114276Staylor 		return (sb);
29124276Staylor 	}
29134276Staylor 
29144276Staylor 	for (c = 0; c < children; c++) {
29154276Staylor 		sb = find_start_block(child[c]);
29164276Staylor 		if (sb != MAXOFFSET_T) {
29174276Staylor 			return (sb);
29184276Staylor 		}
29194276Staylor 	}
29204276Staylor 	return (MAXOFFSET_T);
29214276Staylor }
29224276Staylor 
29234276Staylor /*
29244276Staylor  * Label an individual disk.  The name provided is the short name,
29254276Staylor  * stripped of any leading /dev path.
29264276Staylor  */
29274276Staylor int
29284276Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
29294276Staylor {
29304276Staylor 	char path[MAXPATHLEN];
29314276Staylor 	struct dk_gpt *vtoc;
29324276Staylor 	int fd;
29334276Staylor 	size_t resv = EFI_MIN_RESV_SIZE;
29344276Staylor 	uint64_t slice_size;
29354276Staylor 	diskaddr_t start_block;
29364276Staylor 	char errbuf[1024];
29374276Staylor 
29386289Smmusante 	/* prepare an error message just in case */
29396289Smmusante 	(void) snprintf(errbuf, sizeof (errbuf),
29406289Smmusante 	    dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
29416289Smmusante 
29424276Staylor 	if (zhp) {
29434276Staylor 		nvlist_t *nvroot;
29444276Staylor 
29457965SGeorge.Wilson@Sun.COM 		if (pool_is_bootable(zhp)) {
29467965SGeorge.Wilson@Sun.COM 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29477965SGeorge.Wilson@Sun.COM 			    "EFI labeled devices are not supported on root "
29487965SGeorge.Wilson@Sun.COM 			    "pools."));
29497965SGeorge.Wilson@Sun.COM 			return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
29507965SGeorge.Wilson@Sun.COM 		}
29517965SGeorge.Wilson@Sun.COM 
29524276Staylor 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
29534276Staylor 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
29544276Staylor 
29554276Staylor 		if (zhp->zpool_start_block == 0)
29564276Staylor 			start_block = find_start_block(nvroot);
29574276Staylor 		else
29584276Staylor 			start_block = zhp->zpool_start_block;
29594276Staylor 		zhp->zpool_start_block = start_block;
29604276Staylor 	} else {
29614276Staylor 		/* new pool */
29624276Staylor 		start_block = NEW_START_BLOCK;
29634276Staylor 	}
29644276Staylor 
29654276Staylor 	(void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
29664276Staylor 	    BACKUP_SLICE);
29674276Staylor 
29684276Staylor 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
29694276Staylor 		/*
29704276Staylor 		 * This shouldn't happen.  We've long since verified that this
29714276Staylor 		 * is a valid device.
29724276Staylor 		 */
29736289Smmusante 		zfs_error_aux(hdl,
29746289Smmusante 		    dgettext(TEXT_DOMAIN, "unable to open device"));
29754276Staylor 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
29764276Staylor 	}
29774276Staylor 
29784276Staylor 	if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
29794276Staylor 		/*
29804276Staylor 		 * The only way this can fail is if we run out of memory, or we
29814276Staylor 		 * were unable to read the disk's capacity
29824276Staylor 		 */
29834276Staylor 		if (errno == ENOMEM)
29844276Staylor 			(void) no_memory(hdl);
29854276Staylor 
29864276Staylor 		(void) close(fd);
29876289Smmusante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29886289Smmusante 		    "unable to read disk capacity"), name);
29894276Staylor 
29904276Staylor 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
29914276Staylor 	}
29924276Staylor 
29934276Staylor 	slice_size = vtoc->efi_last_u_lba + 1;
29944276Staylor 	slice_size -= EFI_MIN_RESV_SIZE;
29954276Staylor 	if (start_block == MAXOFFSET_T)
29964276Staylor 		start_block = NEW_START_BLOCK;
29974276Staylor 	slice_size -= start_block;
29984276Staylor 
29994276Staylor 	vtoc->efi_parts[0].p_start = start_block;
30004276Staylor 	vtoc->efi_parts[0].p_size = slice_size;
30014276Staylor 
30024276Staylor 	/*
30034276Staylor 	 * Why we use V_USR: V_BACKUP confuses users, and is considered
30044276Staylor 	 * disposable by some EFI utilities (since EFI doesn't have a backup
30054276Staylor 	 * slice).  V_UNASSIGNED is supposed to be used only for zero size
30064276Staylor 	 * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
30074276Staylor 	 * etc. were all pretty specific.  V_USR is as close to reality as we
30084276Staylor 	 * can get, in the absence of V_OTHER.
30094276Staylor 	 */
30104276Staylor 	vtoc->efi_parts[0].p_tag = V_USR;
30114276Staylor 	(void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
30124276Staylor 
30134276Staylor 	vtoc->efi_parts[8].p_start = slice_size + start_block;
30144276Staylor 	vtoc->efi_parts[8].p_size = resv;
30154276Staylor 	vtoc->efi_parts[8].p_tag = V_RESERVED;
30164276Staylor 
30174276Staylor 	if (efi_write(fd, vtoc) != 0) {
30184276Staylor 		/*
30194276Staylor 		 * Some block drivers (like pcata) may not support EFI
30204276Staylor 		 * GPT labels.  Print out a helpful error message dir-
30214276Staylor 		 * ecting the user to manually label the disk and give
30224276Staylor 		 * a specific slice.
30234276Staylor 		 */
30244276Staylor 		(void) close(fd);
30254276Staylor 		efi_free(vtoc);
30264276Staylor 
30274276Staylor 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30286289Smmusante 		    "try using fdisk(1M) and then provide a specific slice"));
30294276Staylor 		return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
30304276Staylor 	}
30314276Staylor 
30324276Staylor 	(void) close(fd);
30334276Staylor 	efi_free(vtoc);
30344276Staylor 	return (0);
30354276Staylor }
30366423Sgw25295 
30376423Sgw25295 static boolean_t
30386423Sgw25295 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
30396423Sgw25295 {
30406423Sgw25295 	char *type;
30416423Sgw25295 	nvlist_t **child;
30426423Sgw25295 	uint_t children, c;
30436423Sgw25295 
30446423Sgw25295 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
30456423Sgw25295 	if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
30466423Sgw25295 	    strcmp(type, VDEV_TYPE_FILE) == 0 ||
30476423Sgw25295 	    strcmp(type, VDEV_TYPE_LOG) == 0 ||
30486423Sgw25295 	    strcmp(type, VDEV_TYPE_MISSING) == 0) {
30496423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30506423Sgw25295 		    "vdev type '%s' is not supported"), type);
30516423Sgw25295 		(void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
30526423Sgw25295 		return (B_FALSE);
30536423Sgw25295 	}
30546423Sgw25295 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
30556423Sgw25295 	    &child, &children) == 0) {
30566423Sgw25295 		for (c = 0; c < children; c++) {
30576423Sgw25295 			if (!supported_dump_vdev_type(hdl, child[c], errbuf))
30586423Sgw25295 				return (B_FALSE);
30596423Sgw25295 		}
30606423Sgw25295 	}
30616423Sgw25295 	return (B_TRUE);
30626423Sgw25295 }
30636423Sgw25295 
30646423Sgw25295 /*
30656423Sgw25295  * check if this zvol is allowable for use as a dump device; zero if
30666423Sgw25295  * it is, > 0 if it isn't, < 0 if it isn't a zvol
30676423Sgw25295  */
30686423Sgw25295 int
30696423Sgw25295 zvol_check_dump_config(char *arg)
30706423Sgw25295 {
30716423Sgw25295 	zpool_handle_t *zhp = NULL;
30726423Sgw25295 	nvlist_t *config, *nvroot;
30736423Sgw25295 	char *p, *volname;
30746423Sgw25295 	nvlist_t **top;
30756423Sgw25295 	uint_t toplevels;
30766423Sgw25295 	libzfs_handle_t *hdl;
30776423Sgw25295 	char errbuf[1024];
30786423Sgw25295 	char poolname[ZPOOL_MAXNAMELEN];
30796423Sgw25295 	int pathlen = strlen(ZVOL_FULL_DEV_DIR);
30806423Sgw25295 	int ret = 1;
30816423Sgw25295 
30826423Sgw25295 	if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
30836423Sgw25295 		return (-1);
30846423Sgw25295 	}
30856423Sgw25295 
30866423Sgw25295 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
30876423Sgw25295 	    "dump is not supported on device '%s'"), arg);
30886423Sgw25295 
30896423Sgw25295 	if ((hdl = libzfs_init()) == NULL)
30906423Sgw25295 		return (1);
30916423Sgw25295 	libzfs_print_on_error(hdl, B_TRUE);
30926423Sgw25295 
30936423Sgw25295 	volname = arg + pathlen;
30946423Sgw25295 
30956423Sgw25295 	/* check the configuration of the pool */
30966423Sgw25295 	if ((p = strchr(volname, '/')) == NULL) {
30976423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30986423Sgw25295 		    "malformed dataset name"));
30996423Sgw25295 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
31006423Sgw25295 		return (1);
31016423Sgw25295 	} else if (p - volname >= ZFS_MAXNAMELEN) {
31026423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
31036423Sgw25295 		    "dataset name is too long"));
31046423Sgw25295 		(void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
31056423Sgw25295 		return (1);
31066423Sgw25295 	} else {
31076423Sgw25295 		(void) strncpy(poolname, volname, p - volname);
31086423Sgw25295 		poolname[p - volname] = '\0';
31096423Sgw25295 	}
31106423Sgw25295 
31116423Sgw25295 	if ((zhp = zpool_open(hdl, poolname)) == NULL) {
31126423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
31136423Sgw25295 		    "could not open pool '%s'"), poolname);
31146423Sgw25295 		(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
31156423Sgw25295 		goto out;
31166423Sgw25295 	}
31176423Sgw25295 	config = zpool_get_config(zhp, NULL);
31186423Sgw25295 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
31196423Sgw25295 	    &nvroot) != 0) {
31206423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
31216423Sgw25295 		    "could not obtain vdev configuration for  '%s'"), poolname);
31226423Sgw25295 		(void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
31236423Sgw25295 		goto out;
31246423Sgw25295 	}
31256423Sgw25295 
31266423Sgw25295 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
31276423Sgw25295 	    &top, &toplevels) == 0);
31286423Sgw25295 	if (toplevels != 1) {
31296423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
31306423Sgw25295 		    "'%s' has multiple top level vdevs"), poolname);
31316423Sgw25295 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
31326423Sgw25295 		goto out;
31336423Sgw25295 	}
31346423Sgw25295 
31356423Sgw25295 	if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
31366423Sgw25295 		goto out;
31376423Sgw25295 	}
31386423Sgw25295 	ret = 0;
31396423Sgw25295 
31406423Sgw25295 out:
31416423Sgw25295 	if (zhp)
31426423Sgw25295 		zpool_close(zhp);
31436423Sgw25295 	libzfs_fini(hdl);
31446423Sgw25295 	return (ret);
31456423Sgw25295 }
3146