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 /*
236289Smmusante  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24789Sahrens  * Use is subject to license terms.
25789Sahrens  */
26789Sahrens 
27789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
28789Sahrens 
293126Sahl #include <alloca.h>
30789Sahrens #include <assert.h>
31789Sahrens #include <ctype.h>
32789Sahrens #include <errno.h>
33789Sahrens #include <devid.h>
343126Sahl #include <dirent.h>
35789Sahrens #include <fcntl.h>
36789Sahrens #include <libintl.h>
37789Sahrens #include <stdio.h>
38789Sahrens #include <stdlib.h>
393126Sahl #include <strings.h>
40789Sahrens #include <unistd.h>
41*7184Stimh #include <zone.h>
424276Staylor #include <sys/efi_partition.h>
434276Staylor #include <sys/vtoc.h>
44789Sahrens #include <sys/zfs_ioctl.h>
451544Seschrock #include <sys/zio.h>
462926Sek110237 #include <strings.h>
47789Sahrens 
48789Sahrens #include "zfs_namecheck.h"
493912Slling #include "zfs_prop.h"
50789Sahrens #include "libzfs_impl.h"
51789Sahrens 
527042Sgw25295 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
535094Slling 
545094Slling /*
555094Slling  * ====================================================================
565094Slling  *   zpool property functions
575094Slling  * ====================================================================
585094Slling  */
595094Slling 
605094Slling static int
615094Slling zpool_get_all_props(zpool_handle_t *zhp)
625094Slling {
635094Slling 	zfs_cmd_t zc = { 0 };
645094Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
655094Slling 
665094Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
675094Slling 
685094Slling 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
695094Slling 		return (-1);
705094Slling 
715094Slling 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
725094Slling 		if (errno == ENOMEM) {
735094Slling 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
745094Slling 				zcmd_free_nvlists(&zc);
755094Slling 				return (-1);
765094Slling 			}
775094Slling 		} else {
785094Slling 			zcmd_free_nvlists(&zc);
795094Slling 			return (-1);
805094Slling 		}
815094Slling 	}
825094Slling 
835094Slling 	if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
845094Slling 		zcmd_free_nvlists(&zc);
855094Slling 		return (-1);
865094Slling 	}
875094Slling 
885094Slling 	zcmd_free_nvlists(&zc);
895094Slling 
905094Slling 	return (0);
915094Slling }
925094Slling 
935094Slling static int
945094Slling zpool_props_refresh(zpool_handle_t *zhp)
955094Slling {
965094Slling 	nvlist_t *old_props;
975094Slling 
985094Slling 	old_props = zhp->zpool_props;
995094Slling 
1005094Slling 	if (zpool_get_all_props(zhp) != 0)
1015094Slling 		return (-1);
1025094Slling 
1035094Slling 	nvlist_free(old_props);
1045094Slling 	return (0);
1055094Slling }
1065094Slling 
1075094Slling static char *
1085094Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
1095094Slling     zprop_source_t *src)
1105094Slling {
1115094Slling 	nvlist_t *nv, *nvl;
1125094Slling 	uint64_t ival;
1135094Slling 	char *value;
1145094Slling 	zprop_source_t source;
1155094Slling 
1165094Slling 	nvl = zhp->zpool_props;
1175094Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
1185094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
1195094Slling 		source = ival;
1205094Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1215094Slling 	} else {
1225094Slling 		source = ZPROP_SRC_DEFAULT;
1235094Slling 		if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
1245094Slling 			value = "-";
1255094Slling 	}
1265094Slling 
1275094Slling 	if (src)
1285094Slling 		*src = source;
1295094Slling 
1305094Slling 	return (value);
1315094Slling }
1325094Slling 
1335094Slling uint64_t
1345094Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
1355094Slling {
1365094Slling 	nvlist_t *nv, *nvl;
1375094Slling 	uint64_t value;
1385094Slling 	zprop_source_t source;
1395094Slling 
1405094Slling 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp))
1415094Slling 		return (zpool_prop_default_numeric(prop));
1425094Slling 
1435094Slling 	nvl = zhp->zpool_props;
1445094Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
1455094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
1465094Slling 		source = value;
1475094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1485094Slling 	} else {
1495094Slling 		source = ZPROP_SRC_DEFAULT;
1505094Slling 		value = zpool_prop_default_numeric(prop);
1515094Slling 	}
1525094Slling 
1535094Slling 	if (src)
1545094Slling 		*src = source;
1555094Slling 
1565094Slling 	return (value);
1575094Slling }
1585094Slling 
1595094Slling /*
1605094Slling  * Map VDEV STATE to printed strings.
1615094Slling  */
1625094Slling char *
1635094Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
1645094Slling {
1655094Slling 	switch (state) {
1665094Slling 	case VDEV_STATE_CLOSED:
1675094Slling 	case VDEV_STATE_OFFLINE:
1685094Slling 		return (gettext("OFFLINE"));
1695094Slling 	case VDEV_STATE_REMOVED:
1705094Slling 		return (gettext("REMOVED"));
1715094Slling 	case VDEV_STATE_CANT_OPEN:
1725094Slling 		if (aux == VDEV_AUX_CORRUPT_DATA)
1735094Slling 			return (gettext("FAULTED"));
1745094Slling 		else
1755094Slling 			return (gettext("UNAVAIL"));
1765094Slling 	case VDEV_STATE_FAULTED:
1775094Slling 		return (gettext("FAULTED"));
1785094Slling 	case VDEV_STATE_DEGRADED:
1795094Slling 		return (gettext("DEGRADED"));
1805094Slling 	case VDEV_STATE_HEALTHY:
1815094Slling 		return (gettext("ONLINE"));
1825094Slling 	}
1835094Slling 
1845094Slling 	return (gettext("UNKNOWN"));
1855094Slling }
1865094Slling 
1875094Slling /*
1885094Slling  * Get a zpool property value for 'prop' and return the value in
1895094Slling  * a pre-allocated buffer.
1905094Slling  */
1915094Slling int
1925094Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
1935094Slling     zprop_source_t *srctype)
1945094Slling {
1955094Slling 	uint64_t intval;
1965094Slling 	const char *strval;
1975094Slling 	zprop_source_t src = ZPROP_SRC_NONE;
1985094Slling 	nvlist_t *nvroot;
1995094Slling 	vdev_stat_t *vs;
2005094Slling 	uint_t vsc;
2015094Slling 
2025094Slling 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
2035094Slling 		if (prop == ZPOOL_PROP_NAME)
2045094Slling 			(void) strlcpy(buf, zpool_get_name(zhp), len);
2055094Slling 		else if (prop == ZPOOL_PROP_HEALTH)
2065094Slling 			(void) strlcpy(buf, "FAULTED", len);
2075094Slling 		else
2085094Slling 			(void) strlcpy(buf, "-", len);
2095094Slling 		return (0);
2105094Slling 	}
2115094Slling 
2125094Slling 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
2135094Slling 	    prop != ZPOOL_PROP_NAME)
2145094Slling 		return (-1);
2155094Slling 
2165094Slling 	switch (zpool_prop_get_type(prop)) {
2175094Slling 	case PROP_TYPE_STRING:
2185094Slling 		(void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
2195094Slling 		    len);
2205094Slling 		break;
2215094Slling 
2225094Slling 	case PROP_TYPE_NUMBER:
2235094Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
2245094Slling 
2255094Slling 		switch (prop) {
2265094Slling 		case ZPOOL_PROP_SIZE:
2275094Slling 		case ZPOOL_PROP_USED:
2285094Slling 		case ZPOOL_PROP_AVAILABLE:
2295094Slling 			(void) zfs_nicenum(intval, buf, len);
2305094Slling 			break;
2315094Slling 
2325094Slling 		case ZPOOL_PROP_CAPACITY:
2335094Slling 			(void) snprintf(buf, len, "%llu%%",
2345094Slling 			    (u_longlong_t)intval);
2355094Slling 			break;
2365094Slling 
2375094Slling 		case ZPOOL_PROP_HEALTH:
2385094Slling 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2395094Slling 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2405094Slling 			verify(nvlist_lookup_uint64_array(nvroot,
2415094Slling 			    ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
2425094Slling 
2435094Slling 			(void) strlcpy(buf, zpool_state_to_name(intval,
2445094Slling 			    vs->vs_aux), len);
2455094Slling 			break;
2465094Slling 		default:
2475094Slling 			(void) snprintf(buf, len, "%llu", intval);
2485094Slling 		}
2495094Slling 		break;
2505094Slling 
2515094Slling 	case PROP_TYPE_INDEX:
2525094Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
2535094Slling 		if (zpool_prop_index_to_string(prop, intval, &strval)
2545094Slling 		    != 0)
2555094Slling 			return (-1);
2565094Slling 		(void) strlcpy(buf, strval, len);
2575094Slling 		break;
2585094Slling 
2595094Slling 	default:
2605094Slling 		abort();
2615094Slling 	}
2625094Slling 
2635094Slling 	if (srctype)
2645094Slling 		*srctype = src;
2655094Slling 
2665094Slling 	return (0);
2675094Slling }
2685094Slling 
2695094Slling /*
2705094Slling  * Check if the bootfs name has the same pool name as it is set to.
2715094Slling  * Assuming bootfs is a valid dataset name.
2725094Slling  */
2735094Slling static boolean_t
2745094Slling bootfs_name_valid(const char *pool, char *bootfs)
2755094Slling {
2765094Slling 	int len = strlen(pool);
2775094Slling 
2785094Slling 	if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM))
2795094Slling 		return (B_FALSE);
2805094Slling 
2815094Slling 	if (strncmp(pool, bootfs, len) == 0 &&
2825094Slling 	    (bootfs[len] == '/' || bootfs[len] == '\0'))
2835094Slling 		return (B_TRUE);
2845094Slling 
2855094Slling 	return (B_FALSE);
2865094Slling }
2875094Slling 
2885094Slling /*
2897042Sgw25295  * Inspect the configuration to determine if any of the devices contain
2907042Sgw25295  * an EFI label.
2917042Sgw25295  */
2927042Sgw25295 static boolean_t
2937042Sgw25295 pool_uses_efi(nvlist_t *config)
2947042Sgw25295 {
2957042Sgw25295 	nvlist_t **child;
2967042Sgw25295 	uint_t c, children;
2977042Sgw25295 
2987042Sgw25295 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
2997042Sgw25295 	    &child, &children) != 0)
3007042Sgw25295 		return (read_efi_label(config, NULL) >= 0);
3017042Sgw25295 
3027042Sgw25295 	for (c = 0; c < children; c++) {
3037042Sgw25295 		if (pool_uses_efi(child[c]))
3047042Sgw25295 			return (B_TRUE);
3057042Sgw25295 	}
3067042Sgw25295 	return (B_FALSE);
3077042Sgw25295 }
3087042Sgw25295 
3097042Sgw25295 /*
3105094Slling  * Given an nvlist of zpool properties to be set, validate that they are
3115094Slling  * correct, and parse any numeric properties (index, boolean, etc) if they are
3125094Slling  * specified as strings.
3135094Slling  */
3145094Slling static nvlist_t *
315*7184Stimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
3165094Slling     nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf)
3175094Slling {
3185094Slling 	nvpair_t *elem;
3195094Slling 	nvlist_t *retprops;
3205094Slling 	zpool_prop_t prop;
3215094Slling 	char *strval;
3225094Slling 	uint64_t intval;
3235363Seschrock 	char *slash;
3245363Seschrock 	struct stat64 statbuf;
3257042Sgw25295 	zpool_handle_t *zhp;
3267042Sgw25295 	nvlist_t *nvroot;
3275094Slling 
3285094Slling 	if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
3295094Slling 		(void) no_memory(hdl);
3305094Slling 		return (NULL);
3315094Slling 	}
3325094Slling 
3335094Slling 	elem = NULL;
3345094Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
3355094Slling 		const char *propname = nvpair_name(elem);
3365094Slling 
3375094Slling 		/*
3385094Slling 		 * Make sure this property is valid and applies to this type.
3395094Slling 		 */
3405094Slling 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
3415094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3425094Slling 			    "invalid property '%s'"), propname);
3435094Slling 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
3445094Slling 			goto error;
3455094Slling 		}
3465094Slling 
3475094Slling 		if (zpool_prop_readonly(prop)) {
3485094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
3495094Slling 			    "is readonly"), propname);
3505094Slling 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
3515094Slling 			goto error;
3525094Slling 		}
3535094Slling 
3545094Slling 		if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
3555094Slling 		    &strval, &intval, errbuf) != 0)
3565094Slling 			goto error;
3575094Slling 
3585094Slling 		/*
3595094Slling 		 * Perform additional checking for specific properties.
3605094Slling 		 */
3615094Slling 		switch (prop) {
3625094Slling 		case ZPOOL_PROP_VERSION:
3635094Slling 			if (intval < version || intval > SPA_VERSION) {
3645094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3655094Slling 				    "property '%s' number %d is invalid."),
3665094Slling 				    propname, intval);
3675094Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
3685094Slling 				goto error;
3695094Slling 			}
3705094Slling 			break;
3715094Slling 
3725094Slling 		case ZPOOL_PROP_BOOTFS:
3735094Slling 			if (create_or_import) {
3745094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3755094Slling 				    "property '%s' cannot be set at creation "
3765094Slling 				    "or import time"), propname);
3775094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
3785094Slling 				goto error;
3795094Slling 			}
3805094Slling 
3815094Slling 			if (version < SPA_VERSION_BOOTFS) {
3825094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3835094Slling 				    "pool must be upgraded to support "
3845094Slling 				    "'%s' property"), propname);
3855094Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
3865094Slling 				goto error;
3875094Slling 			}
3885094Slling 
3895094Slling 			/*
3905094Slling 			 * bootfs property value has to be a dataset name and
3915094Slling 			 * the dataset has to be in the same pool as it sets to.
3925094Slling 			 */
3935094Slling 			if (strval[0] != '\0' && !bootfs_name_valid(poolname,
3945094Slling 			    strval)) {
3955094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
3965094Slling 				    "is an invalid name"), strval);
3975094Slling 				(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
3985094Slling 				goto error;
3995094Slling 			}
4007042Sgw25295 
4017042Sgw25295 			if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
4027042Sgw25295 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4037042Sgw25295 				    "could not open pool '%s'"), poolname);
4047042Sgw25295 				(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
4057042Sgw25295 				goto error;
4067042Sgw25295 			}
4077042Sgw25295 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
4087042Sgw25295 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
4097042Sgw25295 
4107042Sgw25295 			/*
4117042Sgw25295 			 * bootfs property cannot be set on a disk which has
4127042Sgw25295 			 * been EFI labeled.
4137042Sgw25295 			 */
4147042Sgw25295 			if (pool_uses_efi(nvroot)) {
4157042Sgw25295 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4167042Sgw25295 				    "property '%s' not supported on "
4177042Sgw25295 				    "EFI labeled devices"), propname);
4187042Sgw25295 				(void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
4197042Sgw25295 				zpool_close(zhp);
4207042Sgw25295 				goto error;
4217042Sgw25295 			}
4227042Sgw25295 			zpool_close(zhp);
4235094Slling 			break;
4245094Slling 
4255094Slling 		case ZPOOL_PROP_ALTROOT:
4265094Slling 			if (!create_or_import) {
4275094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4285094Slling 				    "property '%s' can only be set during pool "
4295094Slling 				    "creation or import"), propname);
4305094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
4315094Slling 				goto error;
4325094Slling 			}
4335094Slling 
4345094Slling 			if (strval[0] != '/') {
4355094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4365094Slling 				    "bad alternate root '%s'"), strval);
4375094Slling 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
4385094Slling 				goto error;
4395094Slling 			}
4405094Slling 			break;
4415363Seschrock 
4425363Seschrock 		case ZPOOL_PROP_CACHEFILE:
4435363Seschrock 			if (strval[0] == '\0')
4445363Seschrock 				break;
4455363Seschrock 
4465363Seschrock 			if (strcmp(strval, "none") == 0)
4475363Seschrock 				break;
4485363Seschrock 
4495363Seschrock 			if (strval[0] != '/') {
4505363Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4515363Seschrock 				    "property '%s' must be empty, an "
4525363Seschrock 				    "absolute path, or 'none'"), propname);
4535363Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
4545363Seschrock 				goto error;
4555363Seschrock 			}
4565363Seschrock 
4575363Seschrock 			slash = strrchr(strval, '/');
4585363Seschrock 
4595363Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
4605363Seschrock 			    strcmp(slash, "/..") == 0) {
4615363Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4625363Seschrock 				    "'%s' is not a valid file"), strval);
4635363Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
4645363Seschrock 				goto error;
4655363Seschrock 			}
4665363Seschrock 
4675363Seschrock 			*slash = '\0';
4685363Seschrock 
4695621Seschrock 			if (strval[0] != '\0' &&
4705621Seschrock 			    (stat64(strval, &statbuf) != 0 ||
4715621Seschrock 			    !S_ISDIR(statbuf.st_mode))) {
4725363Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4735363Seschrock 				    "'%s' is not a valid directory"),
4745363Seschrock 				    strval);
4755363Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
4765363Seschrock 				goto error;
4775363Seschrock 			}
4785363Seschrock 
4795363Seschrock 			*slash = '/';
4805363Seschrock 			break;
4815094Slling 		}
4825094Slling 	}
4835094Slling 
4845094Slling 	return (retprops);
4855094Slling error:
4865094Slling 	nvlist_free(retprops);
4875094Slling 	return (NULL);
4885094Slling }
4895094Slling 
4905094Slling /*
4915094Slling  * Set zpool property : propname=propval.
4925094Slling  */
4935094Slling int
4945094Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
4955094Slling {
4965094Slling 	zfs_cmd_t zc = { 0 };
4975094Slling 	int ret = -1;
4985094Slling 	char errbuf[1024];
4995094Slling 	nvlist_t *nvl = NULL;
5005094Slling 	nvlist_t *realprops;
5015094Slling 	uint64_t version;
5025094Slling 
5035094Slling 	(void) snprintf(errbuf, sizeof (errbuf),
5045094Slling 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
5055094Slling 	    zhp->zpool_name);
5065094Slling 
5075094Slling 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp))
5085094Slling 		return (zfs_error(zhp->zpool_hdl, EZFS_POOLPROPS, errbuf));
5095094Slling 
5105094Slling 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5115094Slling 		return (no_memory(zhp->zpool_hdl));
5125094Slling 
5135094Slling 	if (nvlist_add_string(nvl, propname, propval) != 0) {
5145094Slling 		nvlist_free(nvl);
5155094Slling 		return (no_memory(zhp->zpool_hdl));
5165094Slling 	}
5175094Slling 
5185094Slling 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
519*7184Stimh 	if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
5205094Slling 	    zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) {
5215094Slling 		nvlist_free(nvl);
5225094Slling 		return (-1);
5235094Slling 	}
5245094Slling 
5255094Slling 	nvlist_free(nvl);
5265094Slling 	nvl = realprops;
5275094Slling 
5285094Slling 	/*
5295094Slling 	 * Execute the corresponding ioctl() to set this property.
5305094Slling 	 */
5315094Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
5325094Slling 
5335094Slling 	if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
5345094Slling 		nvlist_free(nvl);
5355094Slling 		return (-1);
5365094Slling 	}
5375094Slling 
5385094Slling 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
5395094Slling 
5405094Slling 	zcmd_free_nvlists(&zc);
5415094Slling 	nvlist_free(nvl);
5425094Slling 
5435094Slling 	if (ret)
5445094Slling 		(void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
5455094Slling 	else
5465094Slling 		(void) zpool_props_refresh(zhp);
5475094Slling 
5485094Slling 	return (ret);
5495094Slling }
5505094Slling 
5515094Slling int
5525094Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
5535094Slling {
5545094Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
5555094Slling 	zprop_list_t *entry;
5565094Slling 	char buf[ZFS_MAXPROPLEN];
5575094Slling 
5585094Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
5595094Slling 		return (-1);
5605094Slling 
5615094Slling 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
5625094Slling 
5635094Slling 		if (entry->pl_fixed)
5645094Slling 			continue;
5655094Slling 
5665094Slling 		if (entry->pl_prop != ZPROP_INVAL &&
5675094Slling 		    zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
5685094Slling 		    NULL) == 0) {
5695094Slling 			if (strlen(buf) > entry->pl_width)
5705094Slling 				entry->pl_width = strlen(buf);
5715094Slling 		}
5725094Slling 	}
5735094Slling 
5745094Slling 	return (0);
5755094Slling }
5765094Slling 
5775094Slling 
578789Sahrens /*
579789Sahrens  * Validate the given pool name, optionally putting an extended error message in
580789Sahrens  * 'buf'.
581789Sahrens  */
5826423Sgw25295 boolean_t
5832082Seschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
584789Sahrens {
585789Sahrens 	namecheck_err_t why;
586789Sahrens 	char what;
5871773Seschrock 	int ret;
588789Sahrens 
5891773Seschrock 	ret = pool_namecheck(pool, &why, &what);
5901773Seschrock 
5911773Seschrock 	/*
5921773Seschrock 	 * The rules for reserved pool names were extended at a later point.
5931773Seschrock 	 * But we need to support users with existing pools that may now be
5941773Seschrock 	 * invalid.  So we only check for this expanded set of names during a
5951773Seschrock 	 * create (or import), and only in userland.
5961773Seschrock 	 */
5971773Seschrock 	if (ret == 0 && !isopen &&
5981773Seschrock 	    (strncmp(pool, "mirror", 6) == 0 ||
5991773Seschrock 	    strncmp(pool, "raidz", 5) == 0 ||
6004527Sperrin 	    strncmp(pool, "spare", 5) == 0 ||
6014527Sperrin 	    strcmp(pool, "log") == 0)) {
6026423Sgw25295 		if (hdl != NULL)
6036423Sgw25295 			zfs_error_aux(hdl,
6046423Sgw25295 			    dgettext(TEXT_DOMAIN, "name is reserved"));
6052082Seschrock 		return (B_FALSE);
6061773Seschrock 	}
6071773Seschrock 
6081773Seschrock 
6091773Seschrock 	if (ret != 0) {
6102082Seschrock 		if (hdl != NULL) {
611789Sahrens 			switch (why) {
6121003Slling 			case NAME_ERR_TOOLONG:
6132082Seschrock 				zfs_error_aux(hdl,
6141003Slling 				    dgettext(TEXT_DOMAIN, "name is too long"));
6151003Slling 				break;
6161003Slling 
617789Sahrens 			case NAME_ERR_INVALCHAR:
6182082Seschrock 				zfs_error_aux(hdl,
619789Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
620789Sahrens 				    "'%c' in pool name"), what);
621789Sahrens 				break;
622789Sahrens 
623789Sahrens 			case NAME_ERR_NOLETTER:
6242082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6252082Seschrock 				    "name must begin with a letter"));
626789Sahrens 				break;
627789Sahrens 
628789Sahrens 			case NAME_ERR_RESERVED:
6292082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6302082Seschrock 				    "name is reserved"));
631789Sahrens 				break;
632789Sahrens 
633789Sahrens 			case NAME_ERR_DISKLIKE:
6342082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6352082Seschrock 				    "pool name is reserved"));
636789Sahrens 				break;
6372856Snd150628 
6382856Snd150628 			case NAME_ERR_LEADING_SLASH:
6392856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6402856Snd150628 				    "leading slash in name"));
6412856Snd150628 				break;
6422856Snd150628 
6432856Snd150628 			case NAME_ERR_EMPTY_COMPONENT:
6442856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6452856Snd150628 				    "empty component in name"));
6462856Snd150628 				break;
6472856Snd150628 
6482856Snd150628 			case NAME_ERR_TRAILING_SLASH:
6492856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6502856Snd150628 				    "trailing slash in name"));
6512856Snd150628 				break;
6522856Snd150628 
6532856Snd150628 			case NAME_ERR_MULTIPLE_AT:
6542856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6552856Snd150628 				    "multiple '@' delimiters in name"));
6562856Snd150628 				break;
6572856Snd150628 
658789Sahrens 			}
659789Sahrens 		}
6602082Seschrock 		return (B_FALSE);
661789Sahrens 	}
662789Sahrens 
6632082Seschrock 	return (B_TRUE);
664789Sahrens }
665789Sahrens 
666789Sahrens /*
667789Sahrens  * Open a handle to the given pool, even if the pool is currently in the FAULTED
668789Sahrens  * state.
669789Sahrens  */
670789Sahrens zpool_handle_t *
6712082Seschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
672789Sahrens {
673789Sahrens 	zpool_handle_t *zhp;
6742142Seschrock 	boolean_t missing;
675789Sahrens 
676789Sahrens 	/*
677789Sahrens 	 * Make sure the pool name is valid.
678789Sahrens 	 */
6792082Seschrock 	if (!zpool_name_valid(hdl, B_TRUE, pool)) {
6803237Slling 		(void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
6812082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
6822082Seschrock 		    pool);
683789Sahrens 		return (NULL);
684789Sahrens 	}
685789Sahrens 
6862082Seschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
6872082Seschrock 		return (NULL);
688789Sahrens 
6892082Seschrock 	zhp->zpool_hdl = hdl;
690789Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
691789Sahrens 
6922142Seschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
6932142Seschrock 		zpool_close(zhp);
6942142Seschrock 		return (NULL);
6952142Seschrock 	}
6962142Seschrock 
6972142Seschrock 	if (missing) {
6985094Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
6993237Slling 		(void) zfs_error_fmt(hdl, EZFS_NOENT,
7005094Slling 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
7012142Seschrock 		zpool_close(zhp);
7022142Seschrock 		return (NULL);
703789Sahrens 	}
704789Sahrens 
705789Sahrens 	return (zhp);
706789Sahrens }
707789Sahrens 
708789Sahrens /*
709789Sahrens  * Like the above, but silent on error.  Used when iterating over pools (because
710789Sahrens  * the configuration cache may be out of date).
711789Sahrens  */
7122142Seschrock int
7132142Seschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
714789Sahrens {
715789Sahrens 	zpool_handle_t *zhp;
7162142Seschrock 	boolean_t missing;
717789Sahrens 
7182142Seschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
7192142Seschrock 		return (-1);
720789Sahrens 
7212082Seschrock 	zhp->zpool_hdl = hdl;
722789Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
723789Sahrens 
7242142Seschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
7252142Seschrock 		zpool_close(zhp);
7262142Seschrock 		return (-1);
727789Sahrens 	}
728789Sahrens 
7292142Seschrock 	if (missing) {
7302142Seschrock 		zpool_close(zhp);
7312142Seschrock 		*ret = NULL;
7322142Seschrock 		return (0);
7332142Seschrock 	}
7342142Seschrock 
7352142Seschrock 	*ret = zhp;
7362142Seschrock 	return (0);
737789Sahrens }
738789Sahrens 
739789Sahrens /*
740789Sahrens  * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
741789Sahrens  * state.
742789Sahrens  */
743789Sahrens zpool_handle_t *
7442082Seschrock zpool_open(libzfs_handle_t *hdl, const char *pool)
745789Sahrens {
746789Sahrens 	zpool_handle_t *zhp;
747789Sahrens 
7482082Seschrock 	if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
749789Sahrens 		return (NULL);
750789Sahrens 
751789Sahrens 	if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
7523237Slling 		(void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
7532082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
754789Sahrens 		zpool_close(zhp);
755789Sahrens 		return (NULL);
756789Sahrens 	}
757789Sahrens 
758789Sahrens 	return (zhp);
759789Sahrens }
760789Sahrens 
761789Sahrens /*
762789Sahrens  * Close the handle.  Simply frees the memory associated with the handle.
763789Sahrens  */
764789Sahrens void
765789Sahrens zpool_close(zpool_handle_t *zhp)
766789Sahrens {
767789Sahrens 	if (zhp->zpool_config)
768789Sahrens 		nvlist_free(zhp->zpool_config);
769952Seschrock 	if (zhp->zpool_old_config)
770952Seschrock 		nvlist_free(zhp->zpool_old_config);
7713912Slling 	if (zhp->zpool_props)
7723912Slling 		nvlist_free(zhp->zpool_props);
773789Sahrens 	free(zhp);
774789Sahrens }
775789Sahrens 
776789Sahrens /*
777789Sahrens  * Return the name of the pool.
778789Sahrens  */
779789Sahrens const char *
780789Sahrens zpool_get_name(zpool_handle_t *zhp)
781789Sahrens {
782789Sahrens 	return (zhp->zpool_name);
783789Sahrens }
784789Sahrens 
785789Sahrens 
786789Sahrens /*
787789Sahrens  * Return the state of the pool (ACTIVE or UNAVAILABLE)
788789Sahrens  */
789789Sahrens int
790789Sahrens zpool_get_state(zpool_handle_t *zhp)
791789Sahrens {
792789Sahrens 	return (zhp->zpool_state);
793789Sahrens }
794789Sahrens 
795789Sahrens /*
796789Sahrens  * Create the named pool, using the provided vdev list.  It is assumed
797789Sahrens  * that the consumer has already validated the contents of the nvlist, so we
798789Sahrens  * don't have to worry about error semantics.
799789Sahrens  */
800789Sahrens int
8012082Seschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
802*7184Stimh     nvlist_t *props, nvlist_t *fsprops)
803789Sahrens {
804789Sahrens 	zfs_cmd_t zc = { 0 };
805*7184Stimh 	nvlist_t *zc_fsprops = NULL;
806*7184Stimh 	nvlist_t *zc_props = NULL;
8072082Seschrock 	char msg[1024];
8085094Slling 	char *altroot;
809*7184Stimh 	int ret = -1;
8102082Seschrock 
8112082Seschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
8122082Seschrock 	    "cannot create '%s'"), pool);
813789Sahrens 
8142082Seschrock 	if (!zpool_name_valid(hdl, B_FALSE, pool))
8152082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
8162082Seschrock 
8175320Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
8185320Slling 		return (-1);
8195320Slling 
820*7184Stimh 	if (props) {
821*7184Stimh 		if ((zc_props = zpool_valid_proplist(hdl, pool, props,
822*7184Stimh 		    SPA_VERSION_1, B_TRUE, msg)) == NULL) {
823*7184Stimh 			goto create_failed;
824*7184Stimh 		}
8255320Slling 	}
826789Sahrens 
827*7184Stimh 	if (fsprops) {
828*7184Stimh 		uint64_t zoned;
829*7184Stimh 		char *zonestr;
830*7184Stimh 
831*7184Stimh 		zoned = ((nvlist_lookup_string(fsprops,
832*7184Stimh 		    zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
833*7184Stimh 		    strcmp(zonestr, "on") == 0);
834*7184Stimh 
835*7184Stimh 		if ((zc_fsprops = zfs_valid_proplist(hdl,
836*7184Stimh 		    ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
837*7184Stimh 			goto create_failed;
838*7184Stimh 		}
839*7184Stimh 		if (!zc_props &&
840*7184Stimh 		    (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
841*7184Stimh 			goto create_failed;
842*7184Stimh 		}
843*7184Stimh 		if (nvlist_add_nvlist(zc_props,
844*7184Stimh 		    ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
845*7184Stimh 			goto create_failed;
846*7184Stimh 		}
847*7184Stimh 	}
848*7184Stimh 
849*7184Stimh 	if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
850*7184Stimh 		goto create_failed;
851*7184Stimh 
852789Sahrens 	(void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
853789Sahrens 
854*7184Stimh 	if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
8555320Slling 
8562676Seschrock 		zcmd_free_nvlists(&zc);
857*7184Stimh 		nvlist_free(zc_props);
858*7184Stimh 		nvlist_free(zc_fsprops);
8592082Seschrock 
860789Sahrens 		switch (errno) {
861789Sahrens 		case EBUSY:
862789Sahrens 			/*
863789Sahrens 			 * This can happen if the user has specified the same
864789Sahrens 			 * device multiple times.  We can't reliably detect this
865789Sahrens 			 * until we try to add it and see we already have a
866789Sahrens 			 * label.
867789Sahrens 			 */
8682082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8692082Seschrock 			    "one or more vdevs refer to the same device"));
8702082Seschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
871789Sahrens 
872789Sahrens 		case EOVERFLOW:
873789Sahrens 			/*
8742082Seschrock 			 * This occurs when one of the devices is below
875789Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
876789Sahrens 			 * device was the problem device since there's no
877789Sahrens 			 * reliable way to determine device size from userland.
878789Sahrens 			 */
879789Sahrens 			{
880789Sahrens 				char buf[64];
881789Sahrens 
882789Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
883789Sahrens 
8842082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8852082Seschrock 				    "one or more devices is less than the "
8862082Seschrock 				    "minimum size (%s)"), buf);
887789Sahrens 			}
8882082Seschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
889789Sahrens 
890789Sahrens 		case ENOSPC:
8912082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8922082Seschrock 			    "one or more devices is out of space"));
8932082Seschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
894789Sahrens 
8955450Sbrendan 		case ENOTBLK:
8965450Sbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8975450Sbrendan 			    "cache device must be a disk or disk slice"));
8985450Sbrendan 			return (zfs_error(hdl, EZFS_BADDEV, msg));
8995450Sbrendan 
900789Sahrens 		default:
9012082Seschrock 			return (zpool_standard_error(hdl, errno, msg));
902789Sahrens 		}
903789Sahrens 	}
904789Sahrens 
905789Sahrens 	/*
906789Sahrens 	 * If this is an alternate root pool, then we automatically set the
9072676Seschrock 	 * mountpoint of the root dataset to be '/'.
908789Sahrens 	 */
9095094Slling 	if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
9105094Slling 	    &altroot) == 0) {
911789Sahrens 		zfs_handle_t *zhp;
912789Sahrens 
9135094Slling 		verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
9142676Seschrock 		verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
9152676Seschrock 		    "/") == 0);
916789Sahrens 
917789Sahrens 		zfs_close(zhp);
918789Sahrens 	}
919789Sahrens 
920*7184Stimh create_failed:
9215320Slling 	zcmd_free_nvlists(&zc);
922*7184Stimh 	nvlist_free(zc_props);
923*7184Stimh 	nvlist_free(zc_fsprops);
924*7184Stimh 	return (ret);
925789Sahrens }
926789Sahrens 
927789Sahrens /*
928789Sahrens  * Destroy the given pool.  It is up to the caller to ensure that there are no
929789Sahrens  * datasets left in the pool.
930789Sahrens  */
931789Sahrens int
932789Sahrens zpool_destroy(zpool_handle_t *zhp)
933789Sahrens {
934789Sahrens 	zfs_cmd_t zc = { 0 };
935789Sahrens 	zfs_handle_t *zfp = NULL;
9362082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
9372082Seschrock 	char msg[1024];
938789Sahrens 
939789Sahrens 	if (zhp->zpool_state == POOL_STATE_ACTIVE &&
9402082Seschrock 	    (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name,
9412082Seschrock 	    ZFS_TYPE_FILESYSTEM)) == NULL)
942789Sahrens 		return (-1);
943789Sahrens 
9442856Snd150628 	if (zpool_remove_zvol_links(zhp) != 0)
945789Sahrens 		return (-1);
946789Sahrens 
947789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
948789Sahrens 
9494543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
9502082Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
9512082Seschrock 		    "cannot destroy '%s'"), zhp->zpool_name);
952789Sahrens 
9532082Seschrock 		if (errno == EROFS) {
9542082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9552082Seschrock 			    "one or more devices is read only"));
9562082Seschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
9572082Seschrock 		} else {
9582082Seschrock 			(void) zpool_standard_error(hdl, errno, msg);
959789Sahrens 		}
960789Sahrens 
961789Sahrens 		if (zfp)
962789Sahrens 			zfs_close(zfp);
963789Sahrens 		return (-1);
964789Sahrens 	}
965789Sahrens 
966789Sahrens 	if (zfp) {
967789Sahrens 		remove_mountpoint(zfp);
968789Sahrens 		zfs_close(zfp);
969789Sahrens 	}
970789Sahrens 
971789Sahrens 	return (0);
972789Sahrens }
973789Sahrens 
974789Sahrens /*
975789Sahrens  * Add the given vdevs to the pool.  The caller must have already performed the
976789Sahrens  * necessary verification to ensure that the vdev specification is well-formed.
977789Sahrens  */
978789Sahrens int
979789Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
980789Sahrens {
9812676Seschrock 	zfs_cmd_t zc = { 0 };
9822082Seschrock 	int ret;
9832082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
9842082Seschrock 	char msg[1024];
9855450Sbrendan 	nvlist_t **spares, **l2cache;
9865450Sbrendan 	uint_t nspares, nl2cache;
9872082Seschrock 
9882082Seschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
9892082Seschrock 	    "cannot add to '%s'"), zhp->zpool_name);
9902082Seschrock 
9915450Sbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
9925450Sbrendan 	    SPA_VERSION_SPARES &&
9932082Seschrock 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
9942082Seschrock 	    &spares, &nspares) == 0) {
9952082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
9962082Seschrock 		    "upgraded to add hot spares"));
9972082Seschrock 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
9982082Seschrock 	}
999789Sahrens 
10005450Sbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
10015450Sbrendan 	    SPA_VERSION_L2CACHE &&
10025450Sbrendan 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
10035450Sbrendan 	    &l2cache, &nl2cache) == 0) {
10045450Sbrendan 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
10055450Sbrendan 		    "upgraded to add cache devices"));
10065450Sbrendan 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
10075450Sbrendan 	}
10085450Sbrendan 
10095094Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
10102082Seschrock 		return (-1);
1011789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1012789Sahrens 
10134543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1014789Sahrens 		switch (errno) {
1015789Sahrens 		case EBUSY:
1016789Sahrens 			/*
1017789Sahrens 			 * This can happen if the user has specified the same
1018789Sahrens 			 * device multiple times.  We can't reliably detect this
1019789Sahrens 			 * until we try to add it and see we already have a
1020789Sahrens 			 * label.
1021789Sahrens 			 */
10222082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10232082Seschrock 			    "one or more vdevs refer to the same device"));
10242082Seschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1025789Sahrens 			break;
1026789Sahrens 
1027789Sahrens 		case EOVERFLOW:
1028789Sahrens 			/*
1029789Sahrens 			 * This occurrs when one of the devices is below
1030789Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1031789Sahrens 			 * device was the problem device since there's no
1032789Sahrens 			 * reliable way to determine device size from userland.
1033789Sahrens 			 */
1034789Sahrens 			{
1035789Sahrens 				char buf[64];
1036789Sahrens 
1037789Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1038789Sahrens 
10392082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10402082Seschrock 				    "device is less than the minimum "
10412082Seschrock 				    "size (%s)"), buf);
1042789Sahrens 			}
10432082Seschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
10442082Seschrock 			break;
10452082Seschrock 
10462082Seschrock 		case ENOTSUP:
10472082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10484527Sperrin 			    "pool must be upgraded to add these vdevs"));
10492082Seschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, msg);
1050789Sahrens 			break;
1051789Sahrens 
10523912Slling 		case EDOM:
10533912Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10544527Sperrin 			    "root pool can not have multiple vdevs"
10554527Sperrin 			    " or separate logs"));
10563912Slling 			(void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
10573912Slling 			break;
10583912Slling 
10595450Sbrendan 		case ENOTBLK:
10605450Sbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10615450Sbrendan 			    "cache device must be a disk or disk slice"));
10625450Sbrendan 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
10635450Sbrendan 			break;
10645450Sbrendan 
1065789Sahrens 		default:
10662082Seschrock 			(void) zpool_standard_error(hdl, errno, msg);
1067789Sahrens 		}
1068789Sahrens 
10692082Seschrock 		ret = -1;
10702082Seschrock 	} else {
10712082Seschrock 		ret = 0;
1072789Sahrens 	}
1073789Sahrens 
10742676Seschrock 	zcmd_free_nvlists(&zc);
1075789Sahrens 
10762082Seschrock 	return (ret);
1077789Sahrens }
1078789Sahrens 
1079789Sahrens /*
1080789Sahrens  * Exports the pool from the system.  The caller must ensure that there are no
1081789Sahrens  * mounted datasets in the pool.
1082789Sahrens  */
1083789Sahrens int
1084789Sahrens zpool_export(zpool_handle_t *zhp)
1085789Sahrens {
1086789Sahrens 	zfs_cmd_t zc = { 0 };
1087789Sahrens 
1088789Sahrens 	if (zpool_remove_zvol_links(zhp) != 0)
1089789Sahrens 		return (-1);
1090789Sahrens 
1091789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1092789Sahrens 
10934543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0)
10943237Slling 		return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
10952082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot export '%s'"),
10962082Seschrock 		    zhp->zpool_name));
1097789Sahrens 	return (0);
1098789Sahrens }
1099789Sahrens 
1100789Sahrens /*
11015094Slling  * zpool_import() is a contracted interface. Should be kept the same
11025094Slling  * if possible.
11035094Slling  *
11045094Slling  * Applications should use zpool_import_props() to import a pool with
11055094Slling  * new properties value to be set.
1106789Sahrens  */
1107789Sahrens int
11082082Seschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
11095094Slling     char *altroot)
11105094Slling {
11115094Slling 	nvlist_t *props = NULL;
11125094Slling 	int ret;
11135094Slling 
11145094Slling 	if (altroot != NULL) {
11155094Slling 		if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
11165094Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
11175094Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
11185094Slling 			    newname));
11195094Slling 		}
11205094Slling 
11215094Slling 		if (nvlist_add_string(props,
11225094Slling 		    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0) {
11235094Slling 			nvlist_free(props);
11245094Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
11255094Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
11265094Slling 			    newname));
11275094Slling 		}
11285094Slling 	}
11295094Slling 
11306643Seschrock 	ret = zpool_import_props(hdl, config, newname, props, B_FALSE);
11315094Slling 	if (props)
11325094Slling 		nvlist_free(props);
11335094Slling 	return (ret);
11345094Slling }
11355094Slling 
11365094Slling /*
11375094Slling  * Import the given pool using the known configuration and a list of
11385094Slling  * properties to be set. The configuration should have come from
11395094Slling  * zpool_find_import(). The 'newname' parameters control whether the pool
11405094Slling  * is imported with a different name.
11415094Slling  */
11425094Slling int
11435094Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
11446643Seschrock     nvlist_t *props, boolean_t importfaulted)
1145789Sahrens {
11462676Seschrock 	zfs_cmd_t zc = { 0 };
1147789Sahrens 	char *thename;
1148789Sahrens 	char *origname;
1149789Sahrens 	int ret;
11505094Slling 	char errbuf[1024];
1151789Sahrens 
1152789Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1153789Sahrens 	    &origname) == 0);
1154789Sahrens 
11555094Slling 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
11565094Slling 	    "cannot import pool '%s'"), origname);
11575094Slling 
1158789Sahrens 	if (newname != NULL) {
11592082Seschrock 		if (!zpool_name_valid(hdl, B_FALSE, newname))
11603237Slling 			return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
11612082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
11622082Seschrock 			    newname));
1163789Sahrens 		thename = (char *)newname;
1164789Sahrens 	} else {
1165789Sahrens 		thename = origname;
1166789Sahrens 	}
1167789Sahrens 
11685094Slling 	if (props) {
11695094Slling 		uint64_t version;
11705094Slling 
11715094Slling 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
11725094Slling 		    &version) == 0);
11735094Slling 
1174*7184Stimh 		if ((props = zpool_valid_proplist(hdl, origname,
11755320Slling 		    props, version, B_TRUE, errbuf)) == NULL) {
11765094Slling 			return (-1);
11775320Slling 		} else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
11785320Slling 			nvlist_free(props);
11795094Slling 			return (-1);
11805320Slling 		}
11815094Slling 	}
1182789Sahrens 
1183789Sahrens 	(void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1184789Sahrens 
1185789Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
11861544Seschrock 	    &zc.zc_guid) == 0);
1187789Sahrens 
11885320Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
11895320Slling 		nvlist_free(props);
11902082Seschrock 		return (-1);
11915320Slling 	}
1192789Sahrens 
11936643Seschrock 	zc.zc_cookie = (uint64_t)importfaulted;
1194789Sahrens 	ret = 0;
11954543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) {
1196789Sahrens 		char desc[1024];
1197789Sahrens 		if (newname == NULL)
1198789Sahrens 			(void) snprintf(desc, sizeof (desc),
1199789Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1200789Sahrens 			    thename);
1201789Sahrens 		else
1202789Sahrens 			(void) snprintf(desc, sizeof (desc),
1203789Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1204789Sahrens 			    origname, thename);
1205789Sahrens 
1206789Sahrens 		switch (errno) {
12071544Seschrock 		case ENOTSUP:
12081544Seschrock 			/*
12091544Seschrock 			 * Unsupported version.
12101544Seschrock 			 */
12112082Seschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, desc);
12121544Seschrock 			break;
12131544Seschrock 
12142174Seschrock 		case EINVAL:
12152174Seschrock 			(void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
12162174Seschrock 			break;
12172174Seschrock 
1218789Sahrens 		default:
12192082Seschrock 			(void) zpool_standard_error(hdl, errno, desc);
1220789Sahrens 		}
1221789Sahrens 
1222789Sahrens 		ret = -1;
1223789Sahrens 	} else {
1224789Sahrens 		zpool_handle_t *zhp;
12254543Smarks 
1226789Sahrens 		/*
1227789Sahrens 		 * This should never fail, but play it safe anyway.
1228789Sahrens 		 */
12292142Seschrock 		if (zpool_open_silent(hdl, thename, &zhp) != 0) {
12302142Seschrock 			ret = -1;
12312142Seschrock 		} else if (zhp != NULL) {
1232789Sahrens 			ret = zpool_create_zvol_links(zhp);
1233789Sahrens 			zpool_close(zhp);
1234789Sahrens 		}
12354543Smarks 
1236789Sahrens 	}
1237789Sahrens 
12382676Seschrock 	zcmd_free_nvlists(&zc);
12395320Slling 	nvlist_free(props);
12405320Slling 
1241789Sahrens 	return (ret);
1242789Sahrens }
1243789Sahrens 
1244789Sahrens /*
1245789Sahrens  * Scrub the pool.
1246789Sahrens  */
1247789Sahrens int
1248789Sahrens zpool_scrub(zpool_handle_t *zhp, pool_scrub_type_t type)
1249789Sahrens {
1250789Sahrens 	zfs_cmd_t zc = { 0 };
1251789Sahrens 	char msg[1024];
12522082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1253789Sahrens 
1254789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1255789Sahrens 	zc.zc_cookie = type;
1256789Sahrens 
12574543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCRUB, &zc) == 0)
1258789Sahrens 		return (0);
1259789Sahrens 
1260789Sahrens 	(void) snprintf(msg, sizeof (msg),
1261789Sahrens 	    dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1262789Sahrens 
12632082Seschrock 	if (errno == EBUSY)
12642082Seschrock 		return (zfs_error(hdl, EZFS_RESILVERING, msg));
12652082Seschrock 	else
12662082Seschrock 		return (zpool_standard_error(hdl, errno, msg));
1267789Sahrens }
1268789Sahrens 
12692468Sek110237 /*
12702468Sek110237  * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
12712468Sek110237  * spare; but FALSE if its an INUSE spare.
12722468Sek110237  */
12732082Seschrock static nvlist_t *
12742082Seschrock vdev_to_nvlist_iter(nvlist_t *nv, const char *search, uint64_t guid,
12755450Sbrendan     boolean_t *avail_spare, boolean_t *l2cache)
12761544Seschrock {
12771544Seschrock 	uint_t c, children;
12781544Seschrock 	nvlist_t **child;
12792082Seschrock 	uint64_t theguid, present;
12801544Seschrock 	char *path;
12811544Seschrock 	uint64_t wholedisk = 0;
12822082Seschrock 	nvlist_t *ret;
12831544Seschrock 
12842082Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &theguid) == 0);
12851544Seschrock 
12861544Seschrock 	if (search == NULL &&
12871544Seschrock 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &present) == 0) {
12881544Seschrock 		/*
12891544Seschrock 		 * If the device has never been present since import, the only
12901544Seschrock 		 * reliable way to match the vdev is by GUID.
12911544Seschrock 		 */
12922082Seschrock 		if (theguid == guid)
12932082Seschrock 			return (nv);
12941544Seschrock 	} else if (search != NULL &&
12951544Seschrock 	    nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
12961544Seschrock 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
12971544Seschrock 		    &wholedisk);
12981544Seschrock 		if (wholedisk) {
12991544Seschrock 			/*
13001544Seschrock 			 * For whole disks, the internal path has 's0', but the
13011544Seschrock 			 * path passed in by the user doesn't.
13021544Seschrock 			 */
13031544Seschrock 			if (strlen(search) == strlen(path) - 2 &&
13041544Seschrock 			    strncmp(search, path, strlen(search)) == 0)
13052082Seschrock 				return (nv);
13061544Seschrock 		} else if (strcmp(search, path) == 0) {
13072082Seschrock 			return (nv);
13081544Seschrock 		}
13091544Seschrock 	}
13101544Seschrock 
13111544Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
13121544Seschrock 	    &child, &children) != 0)
13132082Seschrock 		return (NULL);
13141544Seschrock 
13151544Seschrock 	for (c = 0; c < children; c++)
13162082Seschrock 		if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
13175450Sbrendan 		    avail_spare, l2cache)) != NULL)
13181544Seschrock 			return (ret);
13191544Seschrock 
13202082Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
13212082Seschrock 	    &child, &children) == 0) {
13222082Seschrock 		for (c = 0; c < children; c++) {
13232082Seschrock 			if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
13245450Sbrendan 			    avail_spare, l2cache)) != NULL) {
13252468Sek110237 				*avail_spare = B_TRUE;
13262082Seschrock 				return (ret);
13272082Seschrock 			}
13282082Seschrock 		}
13292082Seschrock 	}
13302082Seschrock 
13315450Sbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
13325450Sbrendan 	    &child, &children) == 0) {
13335450Sbrendan 		for (c = 0; c < children; c++) {
13345450Sbrendan 			if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
13355450Sbrendan 			    avail_spare, l2cache)) != NULL) {
13365450Sbrendan 				*l2cache = B_TRUE;
13375450Sbrendan 				return (ret);
13385450Sbrendan 			}
13395450Sbrendan 		}
13405450Sbrendan 	}
13415450Sbrendan 
13422082Seschrock 	return (NULL);
13431544Seschrock }
13441544Seschrock 
13452082Seschrock nvlist_t *
13465450Sbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
13475450Sbrendan     boolean_t *l2cache)
13481544Seschrock {
13491544Seschrock 	char buf[MAXPATHLEN];
13501544Seschrock 	const char *search;
13511544Seschrock 	char *end;
13521544Seschrock 	nvlist_t *nvroot;
13531544Seschrock 	uint64_t guid;
13541544Seschrock 
13551613Seschrock 	guid = strtoull(path, &end, 10);
13561544Seschrock 	if (guid != 0 && *end == '\0') {
13571544Seschrock 		search = NULL;
13581544Seschrock 	} else if (path[0] != '/') {
13591544Seschrock 		(void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path);
13601544Seschrock 		search = buf;
13611544Seschrock 	} else {
13621544Seschrock 		search = path;
13631544Seschrock 	}
13641544Seschrock 
13651544Seschrock 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
13661544Seschrock 	    &nvroot) == 0);
13671544Seschrock 
13682468Sek110237 	*avail_spare = B_FALSE;
13695450Sbrendan 	*l2cache = B_FALSE;
13705450Sbrendan 	return (vdev_to_nvlist_iter(nvroot, search, guid, avail_spare,
13715450Sbrendan 	    l2cache));
13722468Sek110237 }
13732468Sek110237 
13742468Sek110237 /*
13755450Sbrendan  * Returns TRUE if the given guid corresponds to the given type.
13765450Sbrendan  * This is used to check for hot spares (INUSE or not), and level 2 cache
13775450Sbrendan  * devices.
13782468Sek110237  */
13792468Sek110237 static boolean_t
13805450Sbrendan is_guid_type(zpool_handle_t *zhp, uint64_t guid, const char *type)
13812468Sek110237 {
13825450Sbrendan 	uint64_t target_guid;
13832468Sek110237 	nvlist_t *nvroot;
13845450Sbrendan 	nvlist_t **list;
13855450Sbrendan 	uint_t count;
13862468Sek110237 	int i;
13872468Sek110237 
13882468Sek110237 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
13892468Sek110237 	    &nvroot) == 0);
13905450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, type, &list, &count) == 0) {
13915450Sbrendan 		for (i = 0; i < count; i++) {
13925450Sbrendan 			verify(nvlist_lookup_uint64(list[i], ZPOOL_CONFIG_GUID,
13935450Sbrendan 			    &target_guid) == 0);
13945450Sbrendan 			if (guid == target_guid)
13952468Sek110237 				return (B_TRUE);
13962468Sek110237 		}
13972468Sek110237 	}
13982468Sek110237 
13992468Sek110237 	return (B_FALSE);
14001544Seschrock }
14011544Seschrock 
1402789Sahrens /*
14034451Seschrock  * Bring the specified vdev online.   The 'flags' parameter is a set of the
14044451Seschrock  * ZFS_ONLINE_* flags.
1405789Sahrens  */
1406789Sahrens int
14074451Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
14084451Seschrock     vdev_state_t *newstate)
1409789Sahrens {
1410789Sahrens 	zfs_cmd_t zc = { 0 };
1411789Sahrens 	char msg[1024];
14122082Seschrock 	nvlist_t *tgt;
14135450Sbrendan 	boolean_t avail_spare, l2cache;
14142082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1415789Sahrens 
14161544Seschrock 	(void) snprintf(msg, sizeof (msg),
14171544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot online %s"), path);
1418789Sahrens 
14191544Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
14205450Sbrendan 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == NULL)
14212082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
1422789Sahrens 
14232468Sek110237 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
14242468Sek110237 
14255450Sbrendan 	if (avail_spare ||
14265450Sbrendan 	    is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
14272082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
14282082Seschrock 
14294451Seschrock 	zc.zc_cookie = VDEV_STATE_ONLINE;
14304451Seschrock 	zc.zc_obj = flags;
14314451Seschrock 
14324543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0)
14334451Seschrock 		return (zpool_standard_error(hdl, errno, msg));
14344451Seschrock 
14354451Seschrock 	*newstate = zc.zc_cookie;
14364451Seschrock 	return (0);
1437789Sahrens }
1438789Sahrens 
1439789Sahrens /*
1440789Sahrens  * Take the specified vdev offline
1441789Sahrens  */
1442789Sahrens int
14434451Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
1444789Sahrens {
1445789Sahrens 	zfs_cmd_t zc = { 0 };
1446789Sahrens 	char msg[1024];
14472082Seschrock 	nvlist_t *tgt;
14485450Sbrendan 	boolean_t avail_spare, l2cache;
14492082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1450789Sahrens 
14511544Seschrock 	(void) snprintf(msg, sizeof (msg),
14521544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
14531544Seschrock 
1454789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
14555450Sbrendan 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == NULL)
14562082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
14572082Seschrock 
14582468Sek110237 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
14592468Sek110237 
14605450Sbrendan 	if (avail_spare ||
14615450Sbrendan 	    is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
14622082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
14632082Seschrock 
14644451Seschrock 	zc.zc_cookie = VDEV_STATE_OFFLINE;
14654451Seschrock 	zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
14661485Slling 
14674543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
1468789Sahrens 		return (0);
1469789Sahrens 
1470789Sahrens 	switch (errno) {
14712082Seschrock 	case EBUSY:
1472789Sahrens 
1473789Sahrens 		/*
1474789Sahrens 		 * There are no other replicas of this device.
1475789Sahrens 		 */
14762082Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
14772082Seschrock 
14782082Seschrock 	default:
14792082Seschrock 		return (zpool_standard_error(hdl, errno, msg));
14802082Seschrock 	}
14812082Seschrock }
1482789Sahrens 
14832082Seschrock /*
14844451Seschrock  * Mark the given vdev faulted.
14854451Seschrock  */
14864451Seschrock int
14874451Seschrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid)
14884451Seschrock {
14894451Seschrock 	zfs_cmd_t zc = { 0 };
14904451Seschrock 	char msg[1024];
14914451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
14924451Seschrock 
14934451Seschrock 	(void) snprintf(msg, sizeof (msg),
14944451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
14954451Seschrock 
14964451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
14974451Seschrock 	zc.zc_guid = guid;
14984451Seschrock 	zc.zc_cookie = VDEV_STATE_FAULTED;
14994451Seschrock 
15004451Seschrock 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
15014451Seschrock 		return (0);
15024451Seschrock 
15034451Seschrock 	switch (errno) {
15044451Seschrock 	case EBUSY:
15054451Seschrock 
15064451Seschrock 		/*
15074451Seschrock 		 * There are no other replicas of this device.
15084451Seschrock 		 */
15094451Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
15104451Seschrock 
15114451Seschrock 	default:
15124451Seschrock 		return (zpool_standard_error(hdl, errno, msg));
15134451Seschrock 	}
15144451Seschrock 
15154451Seschrock }
15164451Seschrock 
15174451Seschrock /*
15184451Seschrock  * Mark the given vdev degraded.
15194451Seschrock  */
15204451Seschrock int
15214451Seschrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid)
15224451Seschrock {
15234451Seschrock 	zfs_cmd_t zc = { 0 };
15244451Seschrock 	char msg[1024];
15254451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
15264451Seschrock 
15274451Seschrock 	(void) snprintf(msg, sizeof (msg),
15284451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
15294451Seschrock 
15304451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
15314451Seschrock 	zc.zc_guid = guid;
15324451Seschrock 	zc.zc_cookie = VDEV_STATE_DEGRADED;
15334451Seschrock 
15344451Seschrock 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
15354451Seschrock 		return (0);
15364451Seschrock 
15374451Seschrock 	return (zpool_standard_error(hdl, errno, msg));
15384451Seschrock }
15394451Seschrock 
15404451Seschrock /*
15412082Seschrock  * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
15422082Seschrock  * a hot spare.
15432082Seschrock  */
15442082Seschrock static boolean_t
15452082Seschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
15462082Seschrock {
15472082Seschrock 	nvlist_t **child;
15482082Seschrock 	uint_t c, children;
15492082Seschrock 	char *type;
15502082Seschrock 
15512082Seschrock 	if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
15522082Seschrock 	    &children) == 0) {
15532082Seschrock 		verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
15542082Seschrock 		    &type) == 0);
15552082Seschrock 
15562082Seschrock 		if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
15572082Seschrock 		    children == 2 && child[which] == tgt)
15582082Seschrock 			return (B_TRUE);
15592082Seschrock 
15602082Seschrock 		for (c = 0; c < children; c++)
15612082Seschrock 			if (is_replacing_spare(child[c], tgt, which))
15622082Seschrock 				return (B_TRUE);
1563789Sahrens 	}
15642082Seschrock 
15652082Seschrock 	return (B_FALSE);
1566789Sahrens }
1567789Sahrens 
1568789Sahrens /*
1569789Sahrens  * Attach new_disk (fully described by nvroot) to old_disk.
15704527Sperrin  * If 'replacing' is specified, the new disk will replace the old one.
1571789Sahrens  */
1572789Sahrens int
1573789Sahrens zpool_vdev_attach(zpool_handle_t *zhp,
1574789Sahrens     const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
1575789Sahrens {
1576789Sahrens 	zfs_cmd_t zc = { 0 };
1577789Sahrens 	char msg[1024];
1578789Sahrens 	int ret;
15792082Seschrock 	nvlist_t *tgt;
15805450Sbrendan 	boolean_t avail_spare, l2cache;
15814527Sperrin 	uint64_t val, is_log;
15827041Seschrock 	char *path, *newname;
15832082Seschrock 	nvlist_t **child;
15842082Seschrock 	uint_t children;
15852082Seschrock 	nvlist_t *config_root;
15862082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1587789Sahrens 
15881544Seschrock 	if (replacing)
15891544Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
15901544Seschrock 		    "cannot replace %s with %s"), old_disk, new_disk);
15911544Seschrock 	else
15921544Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
15931544Seschrock 		    "cannot attach %s to %s"), new_disk, old_disk);
15941544Seschrock 
1595789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
15965450Sbrendan 	if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache)) == 0)
15972082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
15982082Seschrock 
15992468Sek110237 	if (avail_spare)
16002082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
16012082Seschrock 
16025450Sbrendan 	if (l2cache)
16035450Sbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
16045450Sbrendan 
16052082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
16062082Seschrock 	zc.zc_cookie = replacing;
16072082Seschrock 
16082082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
16092082Seschrock 	    &child, &children) != 0 || children != 1) {
16102082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16112082Seschrock 		    "new device must be a single disk"));
16122082Seschrock 		return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
16131544Seschrock 	}
16142082Seschrock 
16152082Seschrock 	verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
16162082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
16172082Seschrock 
16187041Seschrock 	if ((newname = zpool_vdev_name(NULL, NULL, child[0])) == NULL)
16197041Seschrock 		return (-1);
16207041Seschrock 
16212082Seschrock 	/*
16222082Seschrock 	 * If the target is a hot spare that has been swapped in, we can only
16232082Seschrock 	 * replace it with another hot spare.
16242082Seschrock 	 */
16252082Seschrock 	if (replacing &&
16262082Seschrock 	    nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
16277041Seschrock 	    (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache) == NULL ||
16282468Sek110237 	    !avail_spare) && is_replacing_spare(config_root, tgt, 1)) {
16292082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16302082Seschrock 		    "can only be replaced by another hot spare"));
16317041Seschrock 		free(newname);
16322082Seschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
16332082Seschrock 	}
16342082Seschrock 
16352082Seschrock 	/*
16362082Seschrock 	 * If we are attempting to replace a spare, it canot be applied to an
16372082Seschrock 	 * already spared device.
16382082Seschrock 	 */
16392082Seschrock 	if (replacing &&
16402082Seschrock 	    nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 &&
16417041Seschrock 	    zpool_find_vdev(zhp, newname, &avail_spare, &l2cache) != NULL &&
16425450Sbrendan 	    avail_spare && is_replacing_spare(config_root, tgt, 0)) {
16432082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16442082Seschrock 		    "device has already been replaced with a spare"));
16457041Seschrock 		free(newname);
16462082Seschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
16472082Seschrock 	}
1648789Sahrens 
16497041Seschrock 	free(newname);
16507041Seschrock 
16515094Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
16522082Seschrock 		return (-1);
1653789Sahrens 
16544543Smarks 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc);
1655789Sahrens 
16562676Seschrock 	zcmd_free_nvlists(&zc);
1657789Sahrens 
1658789Sahrens 	if (ret == 0)
1659789Sahrens 		return (0);
1660789Sahrens 
1661789Sahrens 	switch (errno) {
16621544Seschrock 	case ENOTSUP:
1663789Sahrens 		/*
1664789Sahrens 		 * Can't attach to or replace this type of vdev.
1665789Sahrens 		 */
16664527Sperrin 		if (replacing) {
16674527Sperrin 			is_log = B_FALSE;
16684527Sperrin 			(void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_LOG,
16694527Sperrin 			    &is_log);
16704527Sperrin 			if (is_log)
16714527Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16724527Sperrin 				    "cannot replace a log with a spare"));
16734527Sperrin 			else
16744527Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16754527Sperrin 				    "cannot replace a replacing device"));
16764527Sperrin 		} else {
16772082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16782082Seschrock 			    "can only attach to mirrors and top-level "
16792082Seschrock 			    "disks"));
16804527Sperrin 		}
16812082Seschrock 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
1682789Sahrens 		break;
1683789Sahrens 
16841544Seschrock 	case EINVAL:
1685789Sahrens 		/*
1686789Sahrens 		 * The new device must be a single disk.
1687789Sahrens 		 */
16882082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16892082Seschrock 		    "new device must be a single disk"));
16902082Seschrock 		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
1691789Sahrens 		break;
1692789Sahrens 
16931544Seschrock 	case EBUSY:
16942082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
16952082Seschrock 		    new_disk);
16962082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1697789Sahrens 		break;
1698789Sahrens 
16991544Seschrock 	case EOVERFLOW:
1700789Sahrens 		/*
1701789Sahrens 		 * The new device is too small.
1702789Sahrens 		 */
17032082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17042082Seschrock 		    "device is too small"));
17052082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1706789Sahrens 		break;
1707789Sahrens 
17081544Seschrock 	case EDOM:
1709789Sahrens 		/*
1710789Sahrens 		 * The new device has a different alignment requirement.
1711789Sahrens 		 */
17122082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17132082Seschrock 		    "devices have different sector alignment"));
17142082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1715789Sahrens 		break;
1716789Sahrens 
17171544Seschrock 	case ENAMETOOLONG:
1718789Sahrens 		/*
1719789Sahrens 		 * The resulting top-level vdev spec won't fit in the label.
1720789Sahrens 		 */
17212082Seschrock 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
1722789Sahrens 		break;
1723789Sahrens 
17241544Seschrock 	default:
17252082Seschrock 		(void) zpool_standard_error(hdl, errno, msg);
1726789Sahrens 	}
1727789Sahrens 
17282082Seschrock 	return (-1);
1729789Sahrens }
1730789Sahrens 
1731789Sahrens /*
1732789Sahrens  * Detach the specified device.
1733789Sahrens  */
1734789Sahrens int
1735789Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
1736789Sahrens {
1737789Sahrens 	zfs_cmd_t zc = { 0 };
1738789Sahrens 	char msg[1024];
17392082Seschrock 	nvlist_t *tgt;
17405450Sbrendan 	boolean_t avail_spare, l2cache;
17412082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1742789Sahrens 
17431544Seschrock 	(void) snprintf(msg, sizeof (msg),
17441544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
17451544Seschrock 
1746789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
17475450Sbrendan 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == 0)
17482082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
1749789Sahrens 
17502468Sek110237 	if (avail_spare)
17512082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
17522082Seschrock 
17535450Sbrendan 	if (l2cache)
17545450Sbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
17555450Sbrendan 
17562082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
17572082Seschrock 
17584543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
1759789Sahrens 		return (0);
1760789Sahrens 
1761789Sahrens 	switch (errno) {
1762789Sahrens 
17631544Seschrock 	case ENOTSUP:
1764789Sahrens 		/*
1765789Sahrens 		 * Can't detach from this type of vdev.
1766789Sahrens 		 */
17672082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
17682082Seschrock 		    "applicable to mirror and replacing vdevs"));
17692082Seschrock 		(void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg);
1770789Sahrens 		break;
1771789Sahrens 
17721544Seschrock 	case EBUSY:
1773789Sahrens 		/*
1774789Sahrens 		 * There are no other replicas of this device.
1775789Sahrens 		 */
17762082Seschrock 		(void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
1777789Sahrens 		break;
1778789Sahrens 
17791544Seschrock 	default:
17802082Seschrock 		(void) zpool_standard_error(hdl, errno, msg);
17811544Seschrock 	}
17821544Seschrock 
17832082Seschrock 	return (-1);
17842082Seschrock }
17852082Seschrock 
17862082Seschrock /*
17875450Sbrendan  * Remove the given device.  Currently, this is supported only for hot spares
17885450Sbrendan  * and level 2 cache devices.
17892082Seschrock  */
17902082Seschrock int
17912082Seschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
17922082Seschrock {
17932082Seschrock 	zfs_cmd_t zc = { 0 };
17942082Seschrock 	char msg[1024];
17952082Seschrock 	nvlist_t *tgt;
17965450Sbrendan 	boolean_t avail_spare, l2cache;
17972082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
17982082Seschrock 
17992082Seschrock 	(void) snprintf(msg, sizeof (msg),
18002082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
18012082Seschrock 
18022082Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
18035450Sbrendan 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == 0)
18042082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
18052082Seschrock 
18065450Sbrendan 	if (!avail_spare && !l2cache) {
18072082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18085450Sbrendan 		    "only inactive hot spares or cache devices "
18095450Sbrendan 		    "can be removed"));
18102082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
18112082Seschrock 	}
18122082Seschrock 
18132082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
18142082Seschrock 
18154543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
18162082Seschrock 		return (0);
18172082Seschrock 
18182082Seschrock 	return (zpool_standard_error(hdl, errno, msg));
18191544Seschrock }
18201544Seschrock 
18211544Seschrock /*
18221544Seschrock  * Clear the errors for the pool, or the particular device if specified.
18231544Seschrock  */
18241544Seschrock int
18251544Seschrock zpool_clear(zpool_handle_t *zhp, const char *path)
18261544Seschrock {
18271544Seschrock 	zfs_cmd_t zc = { 0 };
18281544Seschrock 	char msg[1024];
18292082Seschrock 	nvlist_t *tgt;
18305450Sbrendan 	boolean_t avail_spare, l2cache;
18312082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
18321544Seschrock 
18331544Seschrock 	if (path)
18341544Seschrock 		(void) snprintf(msg, sizeof (msg),
18351544Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
18362676Seschrock 		    path);
18371544Seschrock 	else
18381544Seschrock 		(void) snprintf(msg, sizeof (msg),
18391544Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
18401544Seschrock 		    zhp->zpool_name);
18411544Seschrock 
18421544Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
18432082Seschrock 	if (path) {
18445450Sbrendan 		if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
18455450Sbrendan 		    &l2cache)) == 0)
18462082Seschrock 			return (zfs_error(hdl, EZFS_NODEVICE, msg));
18472082Seschrock 
18485450Sbrendan 		/*
18495450Sbrendan 		 * Don't allow error clearing for hot spares.  Do allow
18505450Sbrendan 		 * error clearing for l2cache devices.
18515450Sbrendan 		 */
18522468Sek110237 		if (avail_spare)
18532082Seschrock 			return (zfs_error(hdl, EZFS_ISSPARE, msg));
18542082Seschrock 
18552082Seschrock 		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
18562082Seschrock 		    &zc.zc_guid) == 0);
18571544Seschrock 	}
18581544Seschrock 
18594543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0)
18601544Seschrock 		return (0);
18611544Seschrock 
18622082Seschrock 	return (zpool_standard_error(hdl, errno, msg));
1863789Sahrens }
1864789Sahrens 
18653126Sahl /*
18664451Seschrock  * Similar to zpool_clear(), but takes a GUID (used by fmd).
18674451Seschrock  */
18684451Seschrock int
18694451Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
18704451Seschrock {
18714451Seschrock 	zfs_cmd_t zc = { 0 };
18724451Seschrock 	char msg[1024];
18734451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
18744451Seschrock 
18754451Seschrock 	(void) snprintf(msg, sizeof (msg),
18764451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
18774451Seschrock 	    guid);
18784451Seschrock 
18794451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
18804451Seschrock 	zc.zc_guid = guid;
18814451Seschrock 
18824451Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
18834451Seschrock 		return (0);
18844451Seschrock 
18854451Seschrock 	return (zpool_standard_error(hdl, errno, msg));
18864451Seschrock }
18874451Seschrock 
18884451Seschrock /*
18893126Sahl  * Iterate over all zvols in a given pool by walking the /dev/zvol/dsk/<pool>
18903126Sahl  * hierarchy.
18913126Sahl  */
18923126Sahl int
18933126Sahl zpool_iter_zvol(zpool_handle_t *zhp, int (*cb)(const char *, void *),
18943126Sahl     void *data)
1895789Sahrens {
18963126Sahl 	libzfs_handle_t *hdl = zhp->zpool_hdl;
18973126Sahl 	char (*paths)[MAXPATHLEN];
18983126Sahl 	size_t size = 4;
18993126Sahl 	int curr, fd, base, ret = 0;
19003126Sahl 	DIR *dirp;
19013126Sahl 	struct dirent *dp;
19023126Sahl 	struct stat st;
19033126Sahl 
19043126Sahl 	if ((base = open("/dev/zvol/dsk", O_RDONLY)) < 0)
19053126Sahl 		return (errno == ENOENT ? 0 : -1);
19063126Sahl 
19073126Sahl 	if (fstatat(base, zhp->zpool_name, &st, 0) != 0) {
19083126Sahl 		int err = errno;
19093126Sahl 		(void) close(base);
19103126Sahl 		return (err == ENOENT ? 0 : -1);
19113126Sahl 	}
1912789Sahrens 
1913789Sahrens 	/*
19143126Sahl 	 * Oddly this wasn't a directory -- ignore that failure since we
19153126Sahl 	 * know there are no links lower in the (non-existant) hierarchy.
1916789Sahrens 	 */
19173126Sahl 	if (!S_ISDIR(st.st_mode)) {
19183126Sahl 		(void) close(base);
19193126Sahl 		return (0);
19203126Sahl 	}
19213126Sahl 
19223126Sahl 	if ((paths = zfs_alloc(hdl, size * sizeof (paths[0]))) == NULL) {
19233126Sahl 		(void) close(base);
19243126Sahl 		return (-1);
1925789Sahrens 	}
1926789Sahrens 
19273126Sahl 	(void) strlcpy(paths[0], zhp->zpool_name, sizeof (paths[0]));
19283126Sahl 	curr = 0;
19293126Sahl 
19303126Sahl 	while (curr >= 0) {
19313126Sahl 		if (fstatat(base, paths[curr], &st, AT_SYMLINK_NOFOLLOW) != 0)
19323126Sahl 			goto err;
19333126Sahl 
19343126Sahl 		if (S_ISDIR(st.st_mode)) {
19353126Sahl 			if ((fd = openat(base, paths[curr], O_RDONLY)) < 0)
19363126Sahl 				goto err;
19373126Sahl 
19383126Sahl 			if ((dirp = fdopendir(fd)) == NULL) {
19393126Sahl 				(void) close(fd);
19403126Sahl 				goto err;
19413126Sahl 			}
19423126Sahl 
19433126Sahl 			while ((dp = readdir(dirp)) != NULL) {
19443126Sahl 				if (dp->d_name[0] == '.')
19453126Sahl 					continue;
19463126Sahl 
19473126Sahl 				if (curr + 1 == size) {
19483126Sahl 					paths = zfs_realloc(hdl, paths,
19493126Sahl 					    size * sizeof (paths[0]),
19503126Sahl 					    size * 2 * sizeof (paths[0]));
19513126Sahl 					if (paths == NULL) {
19523126Sahl 						(void) closedir(dirp);
19533126Sahl 						(void) close(fd);
19543126Sahl 						goto err;
19553126Sahl 					}
19563126Sahl 
19573126Sahl 					size *= 2;
19583126Sahl 				}
19593126Sahl 
19603126Sahl 				(void) strlcpy(paths[curr + 1], paths[curr],
19613126Sahl 				    sizeof (paths[curr + 1]));
19623126Sahl 				(void) strlcat(paths[curr], "/",
19633126Sahl 				    sizeof (paths[curr]));
19643126Sahl 				(void) strlcat(paths[curr], dp->d_name,
19653126Sahl 				    sizeof (paths[curr]));
19663126Sahl 				curr++;
19673126Sahl 			}
19683126Sahl 
19693126Sahl 			(void) closedir(dirp);
19703126Sahl 
19713126Sahl 		} else {
19723126Sahl 			if ((ret = cb(paths[curr], data)) != 0)
19733126Sahl 				break;
19743126Sahl 		}
19753126Sahl 
19763126Sahl 		curr--;
19773126Sahl 	}
19783126Sahl 
19793126Sahl 	free(paths);
19803126Sahl 	(void) close(base);
19813126Sahl 
19823126Sahl 	return (ret);
19833126Sahl 
19843126Sahl err:
19853126Sahl 	free(paths);
19863126Sahl 	(void) close(base);
19873126Sahl 	return (-1);
19883126Sahl }
19893126Sahl 
19903126Sahl typedef struct zvol_cb {
19913126Sahl 	zpool_handle_t *zcb_pool;
19923126Sahl 	boolean_t zcb_create;
19933126Sahl } zvol_cb_t;
19943126Sahl 
19953126Sahl /*ARGSUSED*/
19963126Sahl static int
19973126Sahl do_zvol_create(zfs_handle_t *zhp, void *data)
19983126Sahl {
19994657Sahrens 	int ret = 0;
20003126Sahl 
20014657Sahrens 	if (ZFS_IS_VOLUME(zhp)) {
20023126Sahl 		(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
20034657Sahrens 		ret = zfs_iter_snapshots(zhp, do_zvol_create, NULL);
20044657Sahrens 	}
20053126Sahl 
20064657Sahrens 	if (ret == 0)
20074657Sahrens 		ret = zfs_iter_filesystems(zhp, do_zvol_create, NULL);
2008789Sahrens 
2009789Sahrens 	zfs_close(zhp);
20103126Sahl 
2011789Sahrens 	return (ret);
2012789Sahrens }
2013789Sahrens 
2014789Sahrens /*
2015789Sahrens  * Iterate over all zvols in the pool and make any necessary minor nodes.
2016789Sahrens  */
2017789Sahrens int
2018789Sahrens zpool_create_zvol_links(zpool_handle_t *zhp)
2019789Sahrens {
2020789Sahrens 	zfs_handle_t *zfp;
2021789Sahrens 	int ret;
2022789Sahrens 
2023789Sahrens 	/*
2024789Sahrens 	 * If the pool is unavailable, just return success.
2025789Sahrens 	 */
20262082Seschrock 	if ((zfp = make_dataset_handle(zhp->zpool_hdl,
20272082Seschrock 	    zhp->zpool_name)) == NULL)
2028789Sahrens 		return (0);
2029789Sahrens 
20304657Sahrens 	ret = zfs_iter_filesystems(zfp, do_zvol_create, NULL);
2031789Sahrens 
2032789Sahrens 	zfs_close(zfp);
2033789Sahrens 	return (ret);
2034789Sahrens }
2035789Sahrens 
20363126Sahl static int
20373126Sahl do_zvol_remove(const char *dataset, void *data)
20383126Sahl {
20393126Sahl 	zpool_handle_t *zhp = data;
20403126Sahl 
20413126Sahl 	return (zvol_remove_link(zhp->zpool_hdl, dataset));
20423126Sahl }
20433126Sahl 
2044789Sahrens /*
20453126Sahl  * Iterate over all zvols in the pool and remove any minor nodes.  We iterate
20463126Sahl  * by examining the /dev links so that a corrupted pool doesn't impede this
20473126Sahl  * operation.
2048789Sahrens  */
2049789Sahrens int
2050789Sahrens zpool_remove_zvol_links(zpool_handle_t *zhp)
2051789Sahrens {
20523126Sahl 	return (zpool_iter_zvol(zhp, do_zvol_remove, zhp));
2053789Sahrens }
20541354Seschrock 
20551354Seschrock /*
20561354Seschrock  * Convert from a devid string to a path.
20571354Seschrock  */
20581354Seschrock static char *
20591354Seschrock devid_to_path(char *devid_str)
20601354Seschrock {
20611354Seschrock 	ddi_devid_t devid;
20621354Seschrock 	char *minor;
20631354Seschrock 	char *path;
20641354Seschrock 	devid_nmlist_t *list = NULL;
20651354Seschrock 	int ret;
20661354Seschrock 
20671354Seschrock 	if (devid_str_decode(devid_str, &devid, &minor) != 0)
20681354Seschrock 		return (NULL);
20691354Seschrock 
20701354Seschrock 	ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
20711354Seschrock 
20721354Seschrock 	devid_str_free(minor);
20731354Seschrock 	devid_free(devid);
20741354Seschrock 
20751354Seschrock 	if (ret != 0)
20761354Seschrock 		return (NULL);
20771354Seschrock 
20782082Seschrock 	if ((path = strdup(list[0].devname)) == NULL)
20792082Seschrock 		return (NULL);
20802082Seschrock 
20811354Seschrock 	devid_free_nmlist(list);
20821354Seschrock 
20831354Seschrock 	return (path);
20841354Seschrock }
20851354Seschrock 
20861354Seschrock /*
20871354Seschrock  * Convert from a path to a devid string.
20881354Seschrock  */
20891354Seschrock static char *
20901354Seschrock path_to_devid(const char *path)
20911354Seschrock {
20921354Seschrock 	int fd;
20931354Seschrock 	ddi_devid_t devid;
20941354Seschrock 	char *minor, *ret;
20951354Seschrock 
20961354Seschrock 	if ((fd = open(path, O_RDONLY)) < 0)
20971354Seschrock 		return (NULL);
20981354Seschrock 
20991354Seschrock 	minor = NULL;
21001354Seschrock 	ret = NULL;
21011354Seschrock 	if (devid_get(fd, &devid) == 0) {
21021354Seschrock 		if (devid_get_minor_name(fd, &minor) == 0)
21031354Seschrock 			ret = devid_str_encode(devid, minor);
21041354Seschrock 		if (minor != NULL)
21051354Seschrock 			devid_str_free(minor);
21061354Seschrock 		devid_free(devid);
21071354Seschrock 	}
21081354Seschrock 	(void) close(fd);
21091354Seschrock 
21101354Seschrock 	return (ret);
21111354Seschrock }
21121354Seschrock 
21131354Seschrock /*
21141354Seschrock  * Issue the necessary ioctl() to update the stored path value for the vdev.  We
21151354Seschrock  * ignore any failure here, since a common case is for an unprivileged user to
21161354Seschrock  * type 'zpool status', and we'll display the correct information anyway.
21171354Seschrock  */
21181354Seschrock static void
21191354Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
21201354Seschrock {
21211354Seschrock 	zfs_cmd_t zc = { 0 };
21221354Seschrock 
21231354Seschrock 	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
21242676Seschrock 	(void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
21251354Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
21261544Seschrock 	    &zc.zc_guid) == 0);
21271354Seschrock 
21282082Seschrock 	(void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
21291354Seschrock }
21301354Seschrock 
21311354Seschrock /*
21321354Seschrock  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
21331354Seschrock  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
21341354Seschrock  * We also check if this is a whole disk, in which case we strip off the
21351354Seschrock  * trailing 's0' slice name.
21361354Seschrock  *
21371354Seschrock  * This routine is also responsible for identifying when disks have been
21381354Seschrock  * reconfigured in a new location.  The kernel will have opened the device by
21391354Seschrock  * devid, but the path will still refer to the old location.  To catch this, we
21401354Seschrock  * first do a path -> devid translation (which is fast for the common case).  If
21411354Seschrock  * the devid matches, we're done.  If not, we do a reverse devid -> path
21421354Seschrock  * translation and issue the appropriate ioctl() to update the path of the vdev.
21431354Seschrock  * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
21441354Seschrock  * of these checks.
21451354Seschrock  */
21461354Seschrock char *
21472082Seschrock zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv)
21481354Seschrock {
21491354Seschrock 	char *path, *devid;
21501544Seschrock 	uint64_t value;
21511544Seschrock 	char buf[64];
21524451Seschrock 	vdev_stat_t *vs;
21534451Seschrock 	uint_t vsc;
21541354Seschrock 
21551544Seschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
21561544Seschrock 	    &value) == 0) {
21571544Seschrock 		verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
21581544Seschrock 		    &value) == 0);
21592856Snd150628 		(void) snprintf(buf, sizeof (buf), "%llu",
21602856Snd150628 		    (u_longlong_t)value);
21611544Seschrock 		path = buf;
21621544Seschrock 	} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
21631354Seschrock 
21644451Seschrock 		/*
21654451Seschrock 		 * If the device is dead (faulted, offline, etc) then don't
21664451Seschrock 		 * bother opening it.  Otherwise we may be forcing the user to
21674451Seschrock 		 * open a misbehaving device, which can have undesirable
21684451Seschrock 		 * effects.
21694451Seschrock 		 */
21704451Seschrock 		if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
21714451Seschrock 		    (uint64_t **)&vs, &vsc) != 0 ||
21724451Seschrock 		    vs->vs_state >= VDEV_STATE_DEGRADED) &&
21734451Seschrock 		    zhp != NULL &&
21741354Seschrock 		    nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
21751354Seschrock 			/*
21761354Seschrock 			 * Determine if the current path is correct.
21771354Seschrock 			 */
21781354Seschrock 			char *newdevid = path_to_devid(path);
21791354Seschrock 
21801354Seschrock 			if (newdevid == NULL ||
21811354Seschrock 			    strcmp(devid, newdevid) != 0) {
21821354Seschrock 				char *newpath;
21831354Seschrock 
21841354Seschrock 				if ((newpath = devid_to_path(devid)) != NULL) {
21851354Seschrock 					/*
21861354Seschrock 					 * Update the path appropriately.
21871354Seschrock 					 */
21881354Seschrock 					set_path(zhp, nv, newpath);
21892082Seschrock 					if (nvlist_add_string(nv,
21902082Seschrock 					    ZPOOL_CONFIG_PATH, newpath) == 0)
21912082Seschrock 						verify(nvlist_lookup_string(nv,
21922082Seschrock 						    ZPOOL_CONFIG_PATH,
21932082Seschrock 						    &path) == 0);
21941354Seschrock 					free(newpath);
21951354Seschrock 				}
21961354Seschrock 			}
21971354Seschrock 
21982082Seschrock 			if (newdevid)
21992082Seschrock 				devid_str_free(newdevid);
22001354Seschrock 		}
22011354Seschrock 
22021354Seschrock 		if (strncmp(path, "/dev/dsk/", 9) == 0)
22031354Seschrock 			path += 9;
22041354Seschrock 
22051354Seschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
22061544Seschrock 		    &value) == 0 && value) {
22072082Seschrock 			char *tmp = zfs_strdup(hdl, path);
22082082Seschrock 			if (tmp == NULL)
22092082Seschrock 				return (NULL);
22101354Seschrock 			tmp[strlen(path) - 2] = '\0';
22111354Seschrock 			return (tmp);
22121354Seschrock 		}
22131354Seschrock 	} else {
22141354Seschrock 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
22152082Seschrock 
22162082Seschrock 		/*
22172082Seschrock 		 * If it's a raidz device, we need to stick in the parity level.
22182082Seschrock 		 */
22192082Seschrock 		if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
22202082Seschrock 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
22212082Seschrock 			    &value) == 0);
22222082Seschrock 			(void) snprintf(buf, sizeof (buf), "%s%llu", path,
22232856Snd150628 			    (u_longlong_t)value);
22242082Seschrock 			path = buf;
22252082Seschrock 		}
22261354Seschrock 	}
22271354Seschrock 
22282082Seschrock 	return (zfs_strdup(hdl, path));
22291354Seschrock }
22301544Seschrock 
22311544Seschrock static int
22321544Seschrock zbookmark_compare(const void *a, const void *b)
22331544Seschrock {
22341544Seschrock 	return (memcmp(a, b, sizeof (zbookmark_t)));
22351544Seschrock }
22361544Seschrock 
22371544Seschrock /*
22381544Seschrock  * Retrieve the persistent error log, uniquify the members, and return to the
22391544Seschrock  * caller.
22401544Seschrock  */
22411544Seschrock int
22423444Sek110237 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
22431544Seschrock {
22441544Seschrock 	zfs_cmd_t zc = { 0 };
22451544Seschrock 	uint64_t count;
22462676Seschrock 	zbookmark_t *zb = NULL;
22473444Sek110237 	int i;
22481544Seschrock 
22491544Seschrock 	/*
22501544Seschrock 	 * Retrieve the raw error list from the kernel.  If the number of errors
22511544Seschrock 	 * has increased, allocate more space and continue until we get the
22521544Seschrock 	 * entire list.
22531544Seschrock 	 */
22541544Seschrock 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
22551544Seschrock 	    &count) == 0);
22564820Sek110237 	if (count == 0)
22574820Sek110237 		return (0);
22582676Seschrock 	if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
22592856Snd150628 	    count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
22602082Seschrock 		return (-1);
22612676Seschrock 	zc.zc_nvlist_dst_size = count;
22621544Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
22631544Seschrock 	for (;;) {
22642082Seschrock 		if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
22652082Seschrock 		    &zc) != 0) {
22662676Seschrock 			free((void *)(uintptr_t)zc.zc_nvlist_dst);
22671544Seschrock 			if (errno == ENOMEM) {
22683823Svb160487 				count = zc.zc_nvlist_dst_size;
22692676Seschrock 				if ((zc.zc_nvlist_dst = (uintptr_t)
22703823Svb160487 				    zfs_alloc(zhp->zpool_hdl, count *
22713823Svb160487 				    sizeof (zbookmark_t))) == (uintptr_t)NULL)
22722082Seschrock 					return (-1);
22731544Seschrock 			} else {
22741544Seschrock 				return (-1);
22751544Seschrock 			}
22761544Seschrock 		} else {
22771544Seschrock 			break;
22781544Seschrock 		}
22791544Seschrock 	}
22801544Seschrock 
22811544Seschrock 	/*
22821544Seschrock 	 * Sort the resulting bookmarks.  This is a little confusing due to the
22831544Seschrock 	 * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
22842676Seschrock 	 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
22851544Seschrock 	 * _not_ copied as part of the process.  So we point the start of our
22861544Seschrock 	 * array appropriate and decrement the total number of elements.
22871544Seschrock 	 */
22882676Seschrock 	zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
22892676Seschrock 	    zc.zc_nvlist_dst_size;
22902676Seschrock 	count -= zc.zc_nvlist_dst_size;
22911544Seschrock 
22921544Seschrock 	qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
22931544Seschrock 
22943444Sek110237 	verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
22951544Seschrock 
22961544Seschrock 	/*
22973444Sek110237 	 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
22981544Seschrock 	 */
22991544Seschrock 	for (i = 0; i < count; i++) {
23001544Seschrock 		nvlist_t *nv;
23011544Seschrock 
23023700Sek110237 		/* ignoring zb_blkid and zb_level for now */
23033700Sek110237 		if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
23043700Sek110237 		    zb[i-1].zb_object == zb[i].zb_object)
23051544Seschrock 			continue;
23061544Seschrock 
23073444Sek110237 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
23083444Sek110237 			goto nomem;
23093444Sek110237 		if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
23103444Sek110237 		    zb[i].zb_objset) != 0) {
23113444Sek110237 			nvlist_free(nv);
23122082Seschrock 			goto nomem;
23133444Sek110237 		}
23143444Sek110237 		if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
23153444Sek110237 		    zb[i].zb_object) != 0) {
23163444Sek110237 			nvlist_free(nv);
23173444Sek110237 			goto nomem;
23181544Seschrock 		}
23193444Sek110237 		if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
23203444Sek110237 			nvlist_free(nv);
23213444Sek110237 			goto nomem;
23223444Sek110237 		}
23233444Sek110237 		nvlist_free(nv);
23241544Seschrock 	}
23251544Seschrock 
23263265Sahrens 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
23271544Seschrock 	return (0);
23282082Seschrock 
23292082Seschrock nomem:
23302676Seschrock 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
23312082Seschrock 	return (no_memory(zhp->zpool_hdl));
23321544Seschrock }
23331760Seschrock 
23341760Seschrock /*
23351760Seschrock  * Upgrade a ZFS pool to the latest on-disk version.
23361760Seschrock  */
23371760Seschrock int
23385094Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
23391760Seschrock {
23401760Seschrock 	zfs_cmd_t zc = { 0 };
23412082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
23421760Seschrock 
23431760Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
23445094Slling 	zc.zc_cookie = new_version;
23455094Slling 
23464543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
23473237Slling 		return (zpool_standard_error_fmt(hdl, errno,
23482082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
23492082Seschrock 		    zhp->zpool_name));
23501760Seschrock 	return (0);
23511760Seschrock }
23522926Sek110237 
23534988Sek110237 void
23544988Sek110237 zpool_set_history_str(const char *subcommand, int argc, char **argv,
23554988Sek110237     char *history_str)
23564988Sek110237 {
23574988Sek110237 	int i;
23584988Sek110237 
23594988Sek110237 	(void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
23604988Sek110237 	for (i = 1; i < argc; i++) {
23614988Sek110237 		if (strlen(history_str) + 1 + strlen(argv[i]) >
23624988Sek110237 		    HIS_MAX_RECORD_LEN)
23634988Sek110237 			break;
23644988Sek110237 		(void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
23654988Sek110237 		(void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
23664988Sek110237 	}
23674988Sek110237 }
23684988Sek110237 
23692926Sek110237 /*
23704988Sek110237  * Stage command history for logging.
23712926Sek110237  */
23724988Sek110237 int
23734988Sek110237 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
23742926Sek110237 {
23754988Sek110237 	if (history_str == NULL)
23764988Sek110237 		return (EINVAL);
23774988Sek110237 
23784988Sek110237 	if (strlen(history_str) > HIS_MAX_RECORD_LEN)
23794988Sek110237 		return (EINVAL);
23802926Sek110237 
23814715Sek110237 	if (hdl->libzfs_log_str != NULL)
23824543Smarks 		free(hdl->libzfs_log_str);
23832926Sek110237 
23844988Sek110237 	if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
23854988Sek110237 		return (no_memory(hdl));
23864543Smarks 
23874988Sek110237 	return (0);
23882926Sek110237 }
23892926Sek110237 
23902926Sek110237 /*
23912926Sek110237  * Perform ioctl to get some command history of a pool.
23922926Sek110237  *
23932926Sek110237  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
23942926Sek110237  * logical offset of the history buffer to start reading from.
23952926Sek110237  *
23962926Sek110237  * Upon return, 'off' is the next logical offset to read from and
23972926Sek110237  * 'len' is the actual amount of bytes read into 'buf'.
23982926Sek110237  */
23992926Sek110237 static int
24002926Sek110237 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
24012926Sek110237 {
24022926Sek110237 	zfs_cmd_t zc = { 0 };
24032926Sek110237 	libzfs_handle_t *hdl = zhp->zpool_hdl;
24042926Sek110237 
24052926Sek110237 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
24062926Sek110237 
24072926Sek110237 	zc.zc_history = (uint64_t)(uintptr_t)buf;
24082926Sek110237 	zc.zc_history_len = *len;
24092926Sek110237 	zc.zc_history_offset = *off;
24102926Sek110237 
24112926Sek110237 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
24122926Sek110237 		switch (errno) {
24132926Sek110237 		case EPERM:
24143237Slling 			return (zfs_error_fmt(hdl, EZFS_PERM,
24153237Slling 			    dgettext(TEXT_DOMAIN,
24162926Sek110237 			    "cannot show history for pool '%s'"),
24172926Sek110237 			    zhp->zpool_name));
24182926Sek110237 		case ENOENT:
24193237Slling 			return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
24202926Sek110237 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
24212926Sek110237 			    "'%s'"), zhp->zpool_name));
24223863Sek110237 		case ENOTSUP:
24233863Sek110237 			return (zfs_error_fmt(hdl, EZFS_BADVERSION,
24243863Sek110237 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
24253863Sek110237 			    "'%s', pool must be upgraded"), zhp->zpool_name));
24262926Sek110237 		default:
24273237Slling 			return (zpool_standard_error_fmt(hdl, errno,
24282926Sek110237 			    dgettext(TEXT_DOMAIN,
24292926Sek110237 			    "cannot get history for '%s'"), zhp->zpool_name));
24302926Sek110237 		}
24312926Sek110237 	}
24322926Sek110237 
24332926Sek110237 	*len = zc.zc_history_len;
24342926Sek110237 	*off = zc.zc_history_offset;
24352926Sek110237 
24362926Sek110237 	return (0);
24372926Sek110237 }
24382926Sek110237 
24392926Sek110237 /*
24402926Sek110237  * Process the buffer of nvlists, unpacking and storing each nvlist record
24412926Sek110237  * into 'records'.  'leftover' is set to the number of bytes that weren't
24422926Sek110237  * processed as there wasn't a complete record.
24432926Sek110237  */
24442926Sek110237 static int
24452926Sek110237 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
24462926Sek110237     nvlist_t ***records, uint_t *numrecords)
24472926Sek110237 {
24482926Sek110237 	uint64_t reclen;
24492926Sek110237 	nvlist_t *nv;
24502926Sek110237 	int i;
24512926Sek110237 
24522926Sek110237 	while (bytes_read > sizeof (reclen)) {
24532926Sek110237 
24542926Sek110237 		/* get length of packed record (stored as little endian) */
24552926Sek110237 		for (i = 0, reclen = 0; i < sizeof (reclen); i++)
24562926Sek110237 			reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
24572926Sek110237 
24582926Sek110237 		if (bytes_read < sizeof (reclen) + reclen)
24592926Sek110237 			break;
24602926Sek110237 
24612926Sek110237 		/* unpack record */
24622926Sek110237 		if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
24632926Sek110237 			return (ENOMEM);
24642926Sek110237 		bytes_read -= sizeof (reclen) + reclen;
24652926Sek110237 		buf += sizeof (reclen) + reclen;
24662926Sek110237 
24672926Sek110237 		/* add record to nvlist array */
24682926Sek110237 		(*numrecords)++;
24692926Sek110237 		if (ISP2(*numrecords + 1)) {
24702926Sek110237 			*records = realloc(*records,
24712926Sek110237 			    *numrecords * 2 * sizeof (nvlist_t *));
24722926Sek110237 		}
24732926Sek110237 		(*records)[*numrecords - 1] = nv;
24742926Sek110237 	}
24752926Sek110237 
24762926Sek110237 	*leftover = bytes_read;
24772926Sek110237 	return (0);
24782926Sek110237 }
24792926Sek110237 
24802926Sek110237 #define	HIS_BUF_LEN	(128*1024)
24812926Sek110237 
24822926Sek110237 /*
24832926Sek110237  * Retrieve the command history of a pool.
24842926Sek110237  */
24852926Sek110237 int
24862926Sek110237 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
24872926Sek110237 {
24882926Sek110237 	char buf[HIS_BUF_LEN];
24892926Sek110237 	uint64_t off = 0;
24902926Sek110237 	nvlist_t **records = NULL;
24912926Sek110237 	uint_t numrecords = 0;
24922926Sek110237 	int err, i;
24932926Sek110237 
24942926Sek110237 	do {
24952926Sek110237 		uint64_t bytes_read = sizeof (buf);
24962926Sek110237 		uint64_t leftover;
24972926Sek110237 
24982926Sek110237 		if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
24992926Sek110237 			break;
25002926Sek110237 
25012926Sek110237 		/* if nothing else was read in, we're at EOF, just return */
25022926Sek110237 		if (!bytes_read)
25032926Sek110237 			break;
25042926Sek110237 
25052926Sek110237 		if ((err = zpool_history_unpack(buf, bytes_read,
25062926Sek110237 		    &leftover, &records, &numrecords)) != 0)
25072926Sek110237 			break;
25082926Sek110237 		off -= leftover;
25092926Sek110237 
25102926Sek110237 		/* CONSTCOND */
25112926Sek110237 	} while (1);
25122926Sek110237 
25132926Sek110237 	if (!err) {
25142926Sek110237 		verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
25152926Sek110237 		verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
25162926Sek110237 		    records, numrecords) == 0);
25172926Sek110237 	}
25182926Sek110237 	for (i = 0; i < numrecords; i++)
25192926Sek110237 		nvlist_free(records[i]);
25202926Sek110237 	free(records);
25212926Sek110237 
25222926Sek110237 	return (err);
25232926Sek110237 }
25243444Sek110237 
25253444Sek110237 void
25263444Sek110237 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
25273444Sek110237     char *pathname, size_t len)
25283444Sek110237 {
25293444Sek110237 	zfs_cmd_t zc = { 0 };
25303444Sek110237 	boolean_t mounted = B_FALSE;
25313444Sek110237 	char *mntpnt = NULL;
25323444Sek110237 	char dsname[MAXNAMELEN];
25333444Sek110237 
25343444Sek110237 	if (dsobj == 0) {
25353444Sek110237 		/* special case for the MOS */
25363444Sek110237 		(void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
25373444Sek110237 		return;
25383444Sek110237 	}
25393444Sek110237 
25403444Sek110237 	/* get the dataset's name */
25413444Sek110237 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
25423444Sek110237 	zc.zc_obj = dsobj;
25433444Sek110237 	if (ioctl(zhp->zpool_hdl->libzfs_fd,
25443444Sek110237 	    ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
25453444Sek110237 		/* just write out a path of two object numbers */
25463444Sek110237 		(void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
25473444Sek110237 		    dsobj, obj);
25483444Sek110237 		return;
25493444Sek110237 	}
25503444Sek110237 	(void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
25513444Sek110237 
25523444Sek110237 	/* find out if the dataset is mounted */
25533444Sek110237 	mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
25543444Sek110237 
25553444Sek110237 	/* get the corrupted object's path */
25563444Sek110237 	(void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
25573444Sek110237 	zc.zc_obj = obj;
25583444Sek110237 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
25593444Sek110237 	    &zc) == 0) {
25603444Sek110237 		if (mounted) {
25613444Sek110237 			(void) snprintf(pathname, len, "%s%s", mntpnt,
25623444Sek110237 			    zc.zc_value);
25633444Sek110237 		} else {
25643444Sek110237 			(void) snprintf(pathname, len, "%s:%s",
25653444Sek110237 			    dsname, zc.zc_value);
25663444Sek110237 		}
25673444Sek110237 	} else {
25683444Sek110237 		(void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
25693444Sek110237 	}
25703444Sek110237 	free(mntpnt);
25713444Sek110237 }
25723912Slling 
25734276Staylor #define	RDISK_ROOT	"/dev/rdsk"
25744276Staylor #define	BACKUP_SLICE	"s2"
25754276Staylor /*
25764276Staylor  * Don't start the slice at the default block of 34; many storage
25774276Staylor  * devices will use a stripe width of 128k, so start there instead.
25784276Staylor  */
25794276Staylor #define	NEW_START_BLOCK	256
25804276Staylor 
25814276Staylor /*
25827042Sgw25295  * Read the EFI label from the config, if a label does not exist then
25837042Sgw25295  * pass back the error to the caller. If the caller has passed a non-NULL
25847042Sgw25295  * diskaddr argument then we set it to the starting address of the EFI
25857042Sgw25295  * partition.
25867042Sgw25295  */
25877042Sgw25295 static int
25887042Sgw25295 read_efi_label(nvlist_t *config, diskaddr_t *sb)
25897042Sgw25295 {
25907042Sgw25295 	char *path;
25917042Sgw25295 	int fd;
25927042Sgw25295 	char diskname[MAXPATHLEN];
25937042Sgw25295 	int err = -1;
25947042Sgw25295 
25957042Sgw25295 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
25967042Sgw25295 		return (err);
25977042Sgw25295 
25987042Sgw25295 	(void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
25997042Sgw25295 	    strrchr(path, '/'));
26007042Sgw25295 	if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
26017042Sgw25295 		struct dk_gpt *vtoc;
26027042Sgw25295 
26037042Sgw25295 		if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
26047042Sgw25295 			if (sb != NULL)
26057042Sgw25295 				*sb = vtoc->efi_parts[0].p_start;
26067042Sgw25295 			efi_free(vtoc);
26077042Sgw25295 		}
26087042Sgw25295 		(void) close(fd);
26097042Sgw25295 	}
26107042Sgw25295 	return (err);
26117042Sgw25295 }
26127042Sgw25295 
26137042Sgw25295 /*
26144276Staylor  * determine where a partition starts on a disk in the current
26154276Staylor  * configuration
26164276Staylor  */
26174276Staylor static diskaddr_t
26184276Staylor find_start_block(nvlist_t *config)
26194276Staylor {
26204276Staylor 	nvlist_t **child;
26214276Staylor 	uint_t c, children;
26224276Staylor 	diskaddr_t sb = MAXOFFSET_T;
26234276Staylor 	uint64_t wholedisk;
26244276Staylor 
26254276Staylor 	if (nvlist_lookup_nvlist_array(config,
26264276Staylor 	    ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
26274276Staylor 		if (nvlist_lookup_uint64(config,
26284276Staylor 		    ZPOOL_CONFIG_WHOLE_DISK,
26294276Staylor 		    &wholedisk) != 0 || !wholedisk) {
26304276Staylor 			return (MAXOFFSET_T);
26314276Staylor 		}
26327042Sgw25295 		if (read_efi_label(config, &sb) < 0)
26337042Sgw25295 			sb = MAXOFFSET_T;
26344276Staylor 		return (sb);
26354276Staylor 	}
26364276Staylor 
26374276Staylor 	for (c = 0; c < children; c++) {
26384276Staylor 		sb = find_start_block(child[c]);
26394276Staylor 		if (sb != MAXOFFSET_T) {
26404276Staylor 			return (sb);
26414276Staylor 		}
26424276Staylor 	}
26434276Staylor 	return (MAXOFFSET_T);
26444276Staylor }
26454276Staylor 
26464276Staylor /*
26474276Staylor  * Label an individual disk.  The name provided is the short name,
26484276Staylor  * stripped of any leading /dev path.
26494276Staylor  */
26504276Staylor int
26514276Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
26524276Staylor {
26534276Staylor 	char path[MAXPATHLEN];
26544276Staylor 	struct dk_gpt *vtoc;
26554276Staylor 	int fd;
26564276Staylor 	size_t resv = EFI_MIN_RESV_SIZE;
26574276Staylor 	uint64_t slice_size;
26584276Staylor 	diskaddr_t start_block;
26594276Staylor 	char errbuf[1024];
26604276Staylor 
26616289Smmusante 	/* prepare an error message just in case */
26626289Smmusante 	(void) snprintf(errbuf, sizeof (errbuf),
26636289Smmusante 	    dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
26646289Smmusante 
26654276Staylor 	if (zhp) {
26664276Staylor 		nvlist_t *nvroot;
26674276Staylor 
26684276Staylor 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
26694276Staylor 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
26704276Staylor 
26714276Staylor 		if (zhp->zpool_start_block == 0)
26724276Staylor 			start_block = find_start_block(nvroot);
26734276Staylor 		else
26744276Staylor 			start_block = zhp->zpool_start_block;
26754276Staylor 		zhp->zpool_start_block = start_block;
26764276Staylor 	} else {
26774276Staylor 		/* new pool */
26784276Staylor 		start_block = NEW_START_BLOCK;
26794276Staylor 	}
26804276Staylor 
26814276Staylor 	(void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
26824276Staylor 	    BACKUP_SLICE);
26834276Staylor 
26844276Staylor 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
26854276Staylor 		/*
26864276Staylor 		 * This shouldn't happen.  We've long since verified that this
26874276Staylor 		 * is a valid device.
26884276Staylor 		 */
26896289Smmusante 		zfs_error_aux(hdl,
26906289Smmusante 		    dgettext(TEXT_DOMAIN, "unable to open device"));
26914276Staylor 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
26924276Staylor 	}
26934276Staylor 
26944276Staylor 	if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
26954276Staylor 		/*
26964276Staylor 		 * The only way this can fail is if we run out of memory, or we
26974276Staylor 		 * were unable to read the disk's capacity
26984276Staylor 		 */
26994276Staylor 		if (errno == ENOMEM)
27004276Staylor 			(void) no_memory(hdl);
27014276Staylor 
27024276Staylor 		(void) close(fd);
27036289Smmusante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27046289Smmusante 		    "unable to read disk capacity"), name);
27054276Staylor 
27064276Staylor 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
27074276Staylor 	}
27084276Staylor 
27094276Staylor 	slice_size = vtoc->efi_last_u_lba + 1;
27104276Staylor 	slice_size -= EFI_MIN_RESV_SIZE;
27114276Staylor 	if (start_block == MAXOFFSET_T)
27124276Staylor 		start_block = NEW_START_BLOCK;
27134276Staylor 	slice_size -= start_block;
27144276Staylor 
27154276Staylor 	vtoc->efi_parts[0].p_start = start_block;
27164276Staylor 	vtoc->efi_parts[0].p_size = slice_size;
27174276Staylor 
27184276Staylor 	/*
27194276Staylor 	 * Why we use V_USR: V_BACKUP confuses users, and is considered
27204276Staylor 	 * disposable by some EFI utilities (since EFI doesn't have a backup
27214276Staylor 	 * slice).  V_UNASSIGNED is supposed to be used only for zero size
27224276Staylor 	 * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
27234276Staylor 	 * etc. were all pretty specific.  V_USR is as close to reality as we
27244276Staylor 	 * can get, in the absence of V_OTHER.
27254276Staylor 	 */
27264276Staylor 	vtoc->efi_parts[0].p_tag = V_USR;
27274276Staylor 	(void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
27284276Staylor 
27294276Staylor 	vtoc->efi_parts[8].p_start = slice_size + start_block;
27304276Staylor 	vtoc->efi_parts[8].p_size = resv;
27314276Staylor 	vtoc->efi_parts[8].p_tag = V_RESERVED;
27324276Staylor 
27334276Staylor 	if (efi_write(fd, vtoc) != 0) {
27344276Staylor 		/*
27354276Staylor 		 * Some block drivers (like pcata) may not support EFI
27364276Staylor 		 * GPT labels.  Print out a helpful error message dir-
27374276Staylor 		 * ecting the user to manually label the disk and give
27384276Staylor 		 * a specific slice.
27394276Staylor 		 */
27404276Staylor 		(void) close(fd);
27414276Staylor 		efi_free(vtoc);
27424276Staylor 
27434276Staylor 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27446289Smmusante 		    "try using fdisk(1M) and then provide a specific slice"));
27454276Staylor 		return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
27464276Staylor 	}
27474276Staylor 
27484276Staylor 	(void) close(fd);
27494276Staylor 	efi_free(vtoc);
27504276Staylor 	return (0);
27514276Staylor }
27526423Sgw25295 
27536423Sgw25295 static boolean_t
27546423Sgw25295 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
27556423Sgw25295 {
27566423Sgw25295 	char *type;
27576423Sgw25295 	nvlist_t **child;
27586423Sgw25295 	uint_t children, c;
27596423Sgw25295 
27606423Sgw25295 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
27616423Sgw25295 	if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
27626423Sgw25295 	    strcmp(type, VDEV_TYPE_FILE) == 0 ||
27636423Sgw25295 	    strcmp(type, VDEV_TYPE_LOG) == 0 ||
27646423Sgw25295 	    strcmp(type, VDEV_TYPE_MISSING) == 0) {
27656423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27666423Sgw25295 		    "vdev type '%s' is not supported"), type);
27676423Sgw25295 		(void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
27686423Sgw25295 		return (B_FALSE);
27696423Sgw25295 	}
27706423Sgw25295 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
27716423Sgw25295 	    &child, &children) == 0) {
27726423Sgw25295 		for (c = 0; c < children; c++) {
27736423Sgw25295 			if (!supported_dump_vdev_type(hdl, child[c], errbuf))
27746423Sgw25295 				return (B_FALSE);
27756423Sgw25295 		}
27766423Sgw25295 	}
27776423Sgw25295 	return (B_TRUE);
27786423Sgw25295 }
27796423Sgw25295 
27806423Sgw25295 /*
27816423Sgw25295  * check if this zvol is allowable for use as a dump device; zero if
27826423Sgw25295  * it is, > 0 if it isn't, < 0 if it isn't a zvol
27836423Sgw25295  */
27846423Sgw25295 int
27856423Sgw25295 zvol_check_dump_config(char *arg)
27866423Sgw25295 {
27876423Sgw25295 	zpool_handle_t *zhp = NULL;
27886423Sgw25295 	nvlist_t *config, *nvroot;
27896423Sgw25295 	char *p, *volname;
27906423Sgw25295 	nvlist_t **top;
27916423Sgw25295 	uint_t toplevels;
27926423Sgw25295 	libzfs_handle_t *hdl;
27936423Sgw25295 	char errbuf[1024];
27946423Sgw25295 	char poolname[ZPOOL_MAXNAMELEN];
27956423Sgw25295 	int pathlen = strlen(ZVOL_FULL_DEV_DIR);
27966423Sgw25295 	int ret = 1;
27976423Sgw25295 
27986423Sgw25295 	if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
27996423Sgw25295 		return (-1);
28006423Sgw25295 	}
28016423Sgw25295 
28026423Sgw25295 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
28036423Sgw25295 	    "dump is not supported on device '%s'"), arg);
28046423Sgw25295 
28056423Sgw25295 	if ((hdl = libzfs_init()) == NULL)
28066423Sgw25295 		return (1);
28076423Sgw25295 	libzfs_print_on_error(hdl, B_TRUE);
28086423Sgw25295 
28096423Sgw25295 	volname = arg + pathlen;
28106423Sgw25295 
28116423Sgw25295 	/* check the configuration of the pool */
28126423Sgw25295 	if ((p = strchr(volname, '/')) == NULL) {
28136423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28146423Sgw25295 		    "malformed dataset name"));
28156423Sgw25295 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
28166423Sgw25295 		return (1);
28176423Sgw25295 	} else if (p - volname >= ZFS_MAXNAMELEN) {
28186423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28196423Sgw25295 		    "dataset name is too long"));
28206423Sgw25295 		(void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
28216423Sgw25295 		return (1);
28226423Sgw25295 	} else {
28236423Sgw25295 		(void) strncpy(poolname, volname, p - volname);
28246423Sgw25295 		poolname[p - volname] = '\0';
28256423Sgw25295 	}
28266423Sgw25295 
28276423Sgw25295 	if ((zhp = zpool_open(hdl, poolname)) == NULL) {
28286423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28296423Sgw25295 		    "could not open pool '%s'"), poolname);
28306423Sgw25295 		(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
28316423Sgw25295 		goto out;
28326423Sgw25295 	}
28336423Sgw25295 	config = zpool_get_config(zhp, NULL);
28346423Sgw25295 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
28356423Sgw25295 	    &nvroot) != 0) {
28366423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28376423Sgw25295 		    "could not obtain vdev configuration for  '%s'"), poolname);
28386423Sgw25295 		(void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
28396423Sgw25295 		goto out;
28406423Sgw25295 	}
28416423Sgw25295 
28426423Sgw25295 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
28436423Sgw25295 	    &top, &toplevels) == 0);
28446423Sgw25295 	if (toplevels != 1) {
28456423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28466423Sgw25295 		    "'%s' has multiple top level vdevs"), poolname);
28476423Sgw25295 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
28486423Sgw25295 		goto out;
28496423Sgw25295 	}
28506423Sgw25295 
28516423Sgw25295 	if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
28526423Sgw25295 		goto out;
28536423Sgw25295 	}
28546423Sgw25295 	ret = 0;
28556423Sgw25295 
28566423Sgw25295 out:
28576423Sgw25295 	if (zhp)
28586423Sgw25295 		zpool_close(zhp);
28596423Sgw25295 	libzfs_fini(hdl);
28606423Sgw25295 	return (ret);
28616423Sgw25295 }
2862