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>
414276Staylor #include <sys/efi_partition.h>
424276Staylor #include <sys/vtoc.h>
43789Sahrens #include <sys/zfs_ioctl.h>
441544Seschrock #include <sys/zio.h>
452926Sek110237 #include <strings.h>
46789Sahrens 
47789Sahrens #include "zfs_namecheck.h"
483912Slling #include "zfs_prop.h"
49789Sahrens #include "libzfs_impl.h"
50789Sahrens 
515094Slling 
525094Slling /*
535094Slling  * ====================================================================
545094Slling  *   zpool property functions
555094Slling  * ====================================================================
565094Slling  */
575094Slling 
585094Slling static int
595094Slling zpool_get_all_props(zpool_handle_t *zhp)
605094Slling {
615094Slling 	zfs_cmd_t zc = { 0 };
625094Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
635094Slling 
645094Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
655094Slling 
665094Slling 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
675094Slling 		return (-1);
685094Slling 
695094Slling 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
705094Slling 		if (errno == ENOMEM) {
715094Slling 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
725094Slling 				zcmd_free_nvlists(&zc);
735094Slling 				return (-1);
745094Slling 			}
755094Slling 		} else {
765094Slling 			zcmd_free_nvlists(&zc);
775094Slling 			return (-1);
785094Slling 		}
795094Slling 	}
805094Slling 
815094Slling 	if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
825094Slling 		zcmd_free_nvlists(&zc);
835094Slling 		return (-1);
845094Slling 	}
855094Slling 
865094Slling 	zcmd_free_nvlists(&zc);
875094Slling 
885094Slling 	return (0);
895094Slling }
905094Slling 
915094Slling static int
925094Slling zpool_props_refresh(zpool_handle_t *zhp)
935094Slling {
945094Slling 	nvlist_t *old_props;
955094Slling 
965094Slling 	old_props = zhp->zpool_props;
975094Slling 
985094Slling 	if (zpool_get_all_props(zhp) != 0)
995094Slling 		return (-1);
1005094Slling 
1015094Slling 	nvlist_free(old_props);
1025094Slling 	return (0);
1035094Slling }
1045094Slling 
1055094Slling static char *
1065094Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
1075094Slling     zprop_source_t *src)
1085094Slling {
1095094Slling 	nvlist_t *nv, *nvl;
1105094Slling 	uint64_t ival;
1115094Slling 	char *value;
1125094Slling 	zprop_source_t source;
1135094Slling 
1145094Slling 	nvl = zhp->zpool_props;
1155094Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
1165094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
1175094Slling 		source = ival;
1185094Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1195094Slling 	} else {
1205094Slling 		source = ZPROP_SRC_DEFAULT;
1215094Slling 		if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
1225094Slling 			value = "-";
1235094Slling 	}
1245094Slling 
1255094Slling 	if (src)
1265094Slling 		*src = source;
1275094Slling 
1285094Slling 	return (value);
1295094Slling }
1305094Slling 
1315094Slling uint64_t
1325094Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
1335094Slling {
1345094Slling 	nvlist_t *nv, *nvl;
1355094Slling 	uint64_t value;
1365094Slling 	zprop_source_t source;
1375094Slling 
1385094Slling 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp))
1395094Slling 		return (zpool_prop_default_numeric(prop));
1405094Slling 
1415094Slling 	nvl = zhp->zpool_props;
1425094Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
1435094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
1445094Slling 		source = value;
1455094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1465094Slling 	} else {
1475094Slling 		source = ZPROP_SRC_DEFAULT;
1485094Slling 		value = zpool_prop_default_numeric(prop);
1495094Slling 	}
1505094Slling 
1515094Slling 	if (src)
1525094Slling 		*src = source;
1535094Slling 
1545094Slling 	return (value);
1555094Slling }
1565094Slling 
1575094Slling /*
1585094Slling  * Map VDEV STATE to printed strings.
1595094Slling  */
1605094Slling char *
1615094Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
1625094Slling {
1635094Slling 	switch (state) {
1645094Slling 	case VDEV_STATE_CLOSED:
1655094Slling 	case VDEV_STATE_OFFLINE:
1665094Slling 		return (gettext("OFFLINE"));
1675094Slling 	case VDEV_STATE_REMOVED:
1685094Slling 		return (gettext("REMOVED"));
1695094Slling 	case VDEV_STATE_CANT_OPEN:
1705094Slling 		if (aux == VDEV_AUX_CORRUPT_DATA)
1715094Slling 			return (gettext("FAULTED"));
1725094Slling 		else
1735094Slling 			return (gettext("UNAVAIL"));
1745094Slling 	case VDEV_STATE_FAULTED:
1755094Slling 		return (gettext("FAULTED"));
1765094Slling 	case VDEV_STATE_DEGRADED:
1775094Slling 		return (gettext("DEGRADED"));
1785094Slling 	case VDEV_STATE_HEALTHY:
1795094Slling 		return (gettext("ONLINE"));
1805094Slling 	}
1815094Slling 
1825094Slling 	return (gettext("UNKNOWN"));
1835094Slling }
1845094Slling 
1855094Slling /*
1865094Slling  * Get a zpool property value for 'prop' and return the value in
1875094Slling  * a pre-allocated buffer.
1885094Slling  */
1895094Slling int
1905094Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
1915094Slling     zprop_source_t *srctype)
1925094Slling {
1935094Slling 	uint64_t intval;
1945094Slling 	const char *strval;
1955094Slling 	zprop_source_t src = ZPROP_SRC_NONE;
1965094Slling 	nvlist_t *nvroot;
1975094Slling 	vdev_stat_t *vs;
1985094Slling 	uint_t vsc;
1995094Slling 
2005094Slling 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
2015094Slling 		if (prop == ZPOOL_PROP_NAME)
2025094Slling 			(void) strlcpy(buf, zpool_get_name(zhp), len);
2035094Slling 		else if (prop == ZPOOL_PROP_HEALTH)
2045094Slling 			(void) strlcpy(buf, "FAULTED", len);
2055094Slling 		else
2065094Slling 			(void) strlcpy(buf, "-", len);
2075094Slling 		return (0);
2085094Slling 	}
2095094Slling 
2105094Slling 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
2115094Slling 	    prop != ZPOOL_PROP_NAME)
2125094Slling 		return (-1);
2135094Slling 
2145094Slling 	switch (zpool_prop_get_type(prop)) {
2155094Slling 	case PROP_TYPE_STRING:
2165094Slling 		(void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
2175094Slling 		    len);
2185094Slling 		break;
2195094Slling 
2205094Slling 	case PROP_TYPE_NUMBER:
2215094Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
2225094Slling 
2235094Slling 		switch (prop) {
2245094Slling 		case ZPOOL_PROP_SIZE:
2255094Slling 		case ZPOOL_PROP_USED:
2265094Slling 		case ZPOOL_PROP_AVAILABLE:
2275094Slling 			(void) zfs_nicenum(intval, buf, len);
2285094Slling 			break;
2295094Slling 
2305094Slling 		case ZPOOL_PROP_CAPACITY:
2315094Slling 			(void) snprintf(buf, len, "%llu%%",
2325094Slling 			    (u_longlong_t)intval);
2335094Slling 			break;
2345094Slling 
2355094Slling 		case ZPOOL_PROP_HEALTH:
2365094Slling 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2375094Slling 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2385094Slling 			verify(nvlist_lookup_uint64_array(nvroot,
2395094Slling 			    ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
2405094Slling 
2415094Slling 			(void) strlcpy(buf, zpool_state_to_name(intval,
2425094Slling 			    vs->vs_aux), len);
2435094Slling 			break;
2445094Slling 		default:
2455094Slling 			(void) snprintf(buf, len, "%llu", intval);
2465094Slling 		}
2475094Slling 		break;
2485094Slling 
2495094Slling 	case PROP_TYPE_INDEX:
2505094Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
2515094Slling 		if (zpool_prop_index_to_string(prop, intval, &strval)
2525094Slling 		    != 0)
2535094Slling 			return (-1);
2545094Slling 		(void) strlcpy(buf, strval, len);
2555094Slling 		break;
2565094Slling 
2575094Slling 	default:
2585094Slling 		abort();
2595094Slling 	}
2605094Slling 
2615094Slling 	if (srctype)
2625094Slling 		*srctype = src;
2635094Slling 
2645094Slling 	return (0);
2655094Slling }
2665094Slling 
2675094Slling /*
2685094Slling  * Check if the bootfs name has the same pool name as it is set to.
2695094Slling  * Assuming bootfs is a valid dataset name.
2705094Slling  */
2715094Slling static boolean_t
2725094Slling bootfs_name_valid(const char *pool, char *bootfs)
2735094Slling {
2745094Slling 	int len = strlen(pool);
2755094Slling 
2765094Slling 	if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM))
2775094Slling 		return (B_FALSE);
2785094Slling 
2795094Slling 	if (strncmp(pool, bootfs, len) == 0 &&
2805094Slling 	    (bootfs[len] == '/' || bootfs[len] == '\0'))
2815094Slling 		return (B_TRUE);
2825094Slling 
2835094Slling 	return (B_FALSE);
2845094Slling }
2855094Slling 
2865094Slling /*
2875094Slling  * Given an nvlist of zpool properties to be set, validate that they are
2885094Slling  * correct, and parse any numeric properties (index, boolean, etc) if they are
2895094Slling  * specified as strings.
2905094Slling  */
2915094Slling static nvlist_t *
2925094Slling zpool_validate_properties(libzfs_handle_t *hdl, const char *poolname,
2935094Slling     nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf)
2945094Slling {
2955094Slling 	nvpair_t *elem;
2965094Slling 	nvlist_t *retprops;
2975094Slling 	zpool_prop_t prop;
2985094Slling 	char *strval;
2995094Slling 	uint64_t intval;
3005363Seschrock 	char *slash;
3015363Seschrock 	struct stat64 statbuf;
3025094Slling 
3035094Slling 	if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
3045094Slling 		(void) no_memory(hdl);
3055094Slling 		return (NULL);
3065094Slling 	}
3075094Slling 
3085094Slling 	elem = NULL;
3095094Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
3105094Slling 		const char *propname = nvpair_name(elem);
3115094Slling 
3125094Slling 		/*
3135094Slling 		 * Make sure this property is valid and applies to this type.
3145094Slling 		 */
3155094Slling 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
3165094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3175094Slling 			    "invalid property '%s'"), propname);
3185094Slling 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
3195094Slling 			goto error;
3205094Slling 		}
3215094Slling 
3225094Slling 		if (zpool_prop_readonly(prop)) {
3235094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
3245094Slling 			    "is readonly"), propname);
3255094Slling 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
3265094Slling 			goto error;
3275094Slling 		}
3285094Slling 
3295094Slling 		if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
3305094Slling 		    &strval, &intval, errbuf) != 0)
3315094Slling 			goto error;
3325094Slling 
3335094Slling 		/*
3345094Slling 		 * Perform additional checking for specific properties.
3355094Slling 		 */
3365094Slling 		switch (prop) {
3375094Slling 		case ZPOOL_PROP_VERSION:
3385094Slling 			if (intval < version || intval > SPA_VERSION) {
3395094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3405094Slling 				    "property '%s' number %d is invalid."),
3415094Slling 				    propname, intval);
3425094Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
3435094Slling 				goto error;
3445094Slling 			}
3455094Slling 			break;
3465094Slling 
3475094Slling 		case ZPOOL_PROP_BOOTFS:
3485094Slling 			if (create_or_import) {
3495094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3505094Slling 				    "property '%s' cannot be set at creation "
3515094Slling 				    "or import time"), propname);
3525094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
3535094Slling 				goto error;
3545094Slling 			}
3555094Slling 
3565094Slling 			if (version < SPA_VERSION_BOOTFS) {
3575094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3585094Slling 				    "pool must be upgraded to support "
3595094Slling 				    "'%s' property"), propname);
3605094Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
3615094Slling 				goto error;
3625094Slling 			}
3635094Slling 
3645094Slling 			/*
3655094Slling 			 * bootfs property value has to be a dataset name and
3665094Slling 			 * the dataset has to be in the same pool as it sets to.
3675094Slling 			 */
3685094Slling 			if (strval[0] != '\0' && !bootfs_name_valid(poolname,
3695094Slling 			    strval)) {
3705094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
3715094Slling 				    "is an invalid name"), strval);
3725094Slling 				(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
3735094Slling 				goto error;
3745094Slling 			}
3755094Slling 			break;
3765094Slling 
3775094Slling 		case ZPOOL_PROP_ALTROOT:
3785094Slling 			if (!create_or_import) {
3795094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3805094Slling 				    "property '%s' can only be set during pool "
3815094Slling 				    "creation or import"), propname);
3825094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
3835094Slling 				goto error;
3845094Slling 			}
3855094Slling 
3865094Slling 			if (strval[0] != '/') {
3875094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3885094Slling 				    "bad alternate root '%s'"), strval);
3895094Slling 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
3905094Slling 				goto error;
3915094Slling 			}
3925094Slling 			break;
3935363Seschrock 
3945363Seschrock 		case ZPOOL_PROP_CACHEFILE:
3955363Seschrock 			if (strval[0] == '\0')
3965363Seschrock 				break;
3975363Seschrock 
3985363Seschrock 			if (strcmp(strval, "none") == 0)
3995363Seschrock 				break;
4005363Seschrock 
4015363Seschrock 			if (strval[0] != '/') {
4025363Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4035363Seschrock 				    "property '%s' must be empty, an "
4045363Seschrock 				    "absolute path, or 'none'"), propname);
4055363Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
4065363Seschrock 				goto error;
4075363Seschrock 			}
4085363Seschrock 
4095363Seschrock 			slash = strrchr(strval, '/');
4105363Seschrock 
4115363Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
4125363Seschrock 			    strcmp(slash, "/..") == 0) {
4135363Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4145363Seschrock 				    "'%s' is not a valid file"), strval);
4155363Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
4165363Seschrock 				goto error;
4175363Seschrock 			}
4185363Seschrock 
4195363Seschrock 			*slash = '\0';
4205363Seschrock 
4215621Seschrock 			if (strval[0] != '\0' &&
4225621Seschrock 			    (stat64(strval, &statbuf) != 0 ||
4235621Seschrock 			    !S_ISDIR(statbuf.st_mode))) {
4245363Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4255363Seschrock 				    "'%s' is not a valid directory"),
4265363Seschrock 				    strval);
4275363Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
4285363Seschrock 				goto error;
4295363Seschrock 			}
4305363Seschrock 
4315363Seschrock 			*slash = '/';
4325363Seschrock 			break;
4335094Slling 		}
4345094Slling 	}
4355094Slling 
4365094Slling 	return (retprops);
4375094Slling error:
4385094Slling 	nvlist_free(retprops);
4395094Slling 	return (NULL);
4405094Slling }
4415094Slling 
4425094Slling /*
4435094Slling  * Set zpool property : propname=propval.
4445094Slling  */
4455094Slling int
4465094Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
4475094Slling {
4485094Slling 	zfs_cmd_t zc = { 0 };
4495094Slling 	int ret = -1;
4505094Slling 	char errbuf[1024];
4515094Slling 	nvlist_t *nvl = NULL;
4525094Slling 	nvlist_t *realprops;
4535094Slling 	uint64_t version;
4545094Slling 
4555094Slling 	(void) snprintf(errbuf, sizeof (errbuf),
4565094Slling 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
4575094Slling 	    zhp->zpool_name);
4585094Slling 
4595094Slling 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp))
4605094Slling 		return (zfs_error(zhp->zpool_hdl, EZFS_POOLPROPS, errbuf));
4615094Slling 
4625094Slling 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
4635094Slling 		return (no_memory(zhp->zpool_hdl));
4645094Slling 
4655094Slling 	if (nvlist_add_string(nvl, propname, propval) != 0) {
4665094Slling 		nvlist_free(nvl);
4675094Slling 		return (no_memory(zhp->zpool_hdl));
4685094Slling 	}
4695094Slling 
4705094Slling 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4715094Slling 	if ((realprops = zpool_validate_properties(zhp->zpool_hdl,
4725094Slling 	    zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) {
4735094Slling 		nvlist_free(nvl);
4745094Slling 		return (-1);
4755094Slling 	}
4765094Slling 
4775094Slling 	nvlist_free(nvl);
4785094Slling 	nvl = realprops;
4795094Slling 
4805094Slling 	/*
4815094Slling 	 * Execute the corresponding ioctl() to set this property.
4825094Slling 	 */
4835094Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4845094Slling 
4855094Slling 	if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
4865094Slling 		nvlist_free(nvl);
4875094Slling 		return (-1);
4885094Slling 	}
4895094Slling 
4905094Slling 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
4915094Slling 
4925094Slling 	zcmd_free_nvlists(&zc);
4935094Slling 	nvlist_free(nvl);
4945094Slling 
4955094Slling 	if (ret)
4965094Slling 		(void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
4975094Slling 	else
4985094Slling 		(void) zpool_props_refresh(zhp);
4995094Slling 
5005094Slling 	return (ret);
5015094Slling }
5025094Slling 
5035094Slling int
5045094Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
5055094Slling {
5065094Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
5075094Slling 	zprop_list_t *entry;
5085094Slling 	char buf[ZFS_MAXPROPLEN];
5095094Slling 
5105094Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
5115094Slling 		return (-1);
5125094Slling 
5135094Slling 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
5145094Slling 
5155094Slling 		if (entry->pl_fixed)
5165094Slling 			continue;
5175094Slling 
5185094Slling 		if (entry->pl_prop != ZPROP_INVAL &&
5195094Slling 		    zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
5205094Slling 		    NULL) == 0) {
5215094Slling 			if (strlen(buf) > entry->pl_width)
5225094Slling 				entry->pl_width = strlen(buf);
5235094Slling 		}
5245094Slling 	}
5255094Slling 
5265094Slling 	return (0);
5275094Slling }
5285094Slling 
5295094Slling 
530789Sahrens /*
531789Sahrens  * Validate the given pool name, optionally putting an extended error message in
532789Sahrens  * 'buf'.
533789Sahrens  */
5346423Sgw25295 boolean_t
5352082Seschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
536789Sahrens {
537789Sahrens 	namecheck_err_t why;
538789Sahrens 	char what;
5391773Seschrock 	int ret;
540789Sahrens 
5411773Seschrock 	ret = pool_namecheck(pool, &why, &what);
5421773Seschrock 
5431773Seschrock 	/*
5441773Seschrock 	 * The rules for reserved pool names were extended at a later point.
5451773Seschrock 	 * But we need to support users with existing pools that may now be
5461773Seschrock 	 * invalid.  So we only check for this expanded set of names during a
5471773Seschrock 	 * create (or import), and only in userland.
5481773Seschrock 	 */
5491773Seschrock 	if (ret == 0 && !isopen &&
5501773Seschrock 	    (strncmp(pool, "mirror", 6) == 0 ||
5511773Seschrock 	    strncmp(pool, "raidz", 5) == 0 ||
5524527Sperrin 	    strncmp(pool, "spare", 5) == 0 ||
5534527Sperrin 	    strcmp(pool, "log") == 0)) {
5546423Sgw25295 		if (hdl != NULL)
5556423Sgw25295 			zfs_error_aux(hdl,
5566423Sgw25295 			    dgettext(TEXT_DOMAIN, "name is reserved"));
5572082Seschrock 		return (B_FALSE);
5581773Seschrock 	}
5591773Seschrock 
5601773Seschrock 
5611773Seschrock 	if (ret != 0) {
5622082Seschrock 		if (hdl != NULL) {
563789Sahrens 			switch (why) {
5641003Slling 			case NAME_ERR_TOOLONG:
5652082Seschrock 				zfs_error_aux(hdl,
5661003Slling 				    dgettext(TEXT_DOMAIN, "name is too long"));
5671003Slling 				break;
5681003Slling 
569789Sahrens 			case NAME_ERR_INVALCHAR:
5702082Seschrock 				zfs_error_aux(hdl,
571789Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
572789Sahrens 				    "'%c' in pool name"), what);
573789Sahrens 				break;
574789Sahrens 
575789Sahrens 			case NAME_ERR_NOLETTER:
5762082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5772082Seschrock 				    "name must begin with a letter"));
578789Sahrens 				break;
579789Sahrens 
580789Sahrens 			case NAME_ERR_RESERVED:
5812082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5822082Seschrock 				    "name is reserved"));
583789Sahrens 				break;
584789Sahrens 
585789Sahrens 			case NAME_ERR_DISKLIKE:
5862082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5872082Seschrock 				    "pool name is reserved"));
588789Sahrens 				break;
5892856Snd150628 
5902856Snd150628 			case NAME_ERR_LEADING_SLASH:
5912856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5922856Snd150628 				    "leading slash in name"));
5932856Snd150628 				break;
5942856Snd150628 
5952856Snd150628 			case NAME_ERR_EMPTY_COMPONENT:
5962856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5972856Snd150628 				    "empty component in name"));
5982856Snd150628 				break;
5992856Snd150628 
6002856Snd150628 			case NAME_ERR_TRAILING_SLASH:
6012856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6022856Snd150628 				    "trailing slash in name"));
6032856Snd150628 				break;
6042856Snd150628 
6052856Snd150628 			case NAME_ERR_MULTIPLE_AT:
6062856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6072856Snd150628 				    "multiple '@' delimiters in name"));
6082856Snd150628 				break;
6092856Snd150628 
610789Sahrens 			}
611789Sahrens 		}
6122082Seschrock 		return (B_FALSE);
613789Sahrens 	}
614789Sahrens 
6152082Seschrock 	return (B_TRUE);
616789Sahrens }
617789Sahrens 
618789Sahrens /*
619789Sahrens  * Open a handle to the given pool, even if the pool is currently in the FAULTED
620789Sahrens  * state.
621789Sahrens  */
622789Sahrens zpool_handle_t *
6232082Seschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
624789Sahrens {
625789Sahrens 	zpool_handle_t *zhp;
6262142Seschrock 	boolean_t missing;
627789Sahrens 
628789Sahrens 	/*
629789Sahrens 	 * Make sure the pool name is valid.
630789Sahrens 	 */
6312082Seschrock 	if (!zpool_name_valid(hdl, B_TRUE, pool)) {
6323237Slling 		(void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
6332082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
6342082Seschrock 		    pool);
635789Sahrens 		return (NULL);
636789Sahrens 	}
637789Sahrens 
6382082Seschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
6392082Seschrock 		return (NULL);
640789Sahrens 
6412082Seschrock 	zhp->zpool_hdl = hdl;
642789Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
643789Sahrens 
6442142Seschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
6452142Seschrock 		zpool_close(zhp);
6462142Seschrock 		return (NULL);
6472142Seschrock 	}
6482142Seschrock 
6492142Seschrock 	if (missing) {
6505094Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
6513237Slling 		(void) zfs_error_fmt(hdl, EZFS_NOENT,
6525094Slling 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
6532142Seschrock 		zpool_close(zhp);
6542142Seschrock 		return (NULL);
655789Sahrens 	}
656789Sahrens 
657789Sahrens 	return (zhp);
658789Sahrens }
659789Sahrens 
660789Sahrens /*
661789Sahrens  * Like the above, but silent on error.  Used when iterating over pools (because
662789Sahrens  * the configuration cache may be out of date).
663789Sahrens  */
6642142Seschrock int
6652142Seschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
666789Sahrens {
667789Sahrens 	zpool_handle_t *zhp;
6682142Seschrock 	boolean_t missing;
669789Sahrens 
6702142Seschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
6712142Seschrock 		return (-1);
672789Sahrens 
6732082Seschrock 	zhp->zpool_hdl = hdl;
674789Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
675789Sahrens 
6762142Seschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
6772142Seschrock 		zpool_close(zhp);
6782142Seschrock 		return (-1);
679789Sahrens 	}
680789Sahrens 
6812142Seschrock 	if (missing) {
6822142Seschrock 		zpool_close(zhp);
6832142Seschrock 		*ret = NULL;
6842142Seschrock 		return (0);
6852142Seschrock 	}
6862142Seschrock 
6872142Seschrock 	*ret = zhp;
6882142Seschrock 	return (0);
689789Sahrens }
690789Sahrens 
691789Sahrens /*
692789Sahrens  * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
693789Sahrens  * state.
694789Sahrens  */
695789Sahrens zpool_handle_t *
6962082Seschrock zpool_open(libzfs_handle_t *hdl, const char *pool)
697789Sahrens {
698789Sahrens 	zpool_handle_t *zhp;
699789Sahrens 
7002082Seschrock 	if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
701789Sahrens 		return (NULL);
702789Sahrens 
703789Sahrens 	if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
7043237Slling 		(void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
7052082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
706789Sahrens 		zpool_close(zhp);
707789Sahrens 		return (NULL);
708789Sahrens 	}
709789Sahrens 
710789Sahrens 	return (zhp);
711789Sahrens }
712789Sahrens 
713789Sahrens /*
714789Sahrens  * Close the handle.  Simply frees the memory associated with the handle.
715789Sahrens  */
716789Sahrens void
717789Sahrens zpool_close(zpool_handle_t *zhp)
718789Sahrens {
719789Sahrens 	if (zhp->zpool_config)
720789Sahrens 		nvlist_free(zhp->zpool_config);
721952Seschrock 	if (zhp->zpool_old_config)
722952Seschrock 		nvlist_free(zhp->zpool_old_config);
7233912Slling 	if (zhp->zpool_props)
7243912Slling 		nvlist_free(zhp->zpool_props);
725789Sahrens 	free(zhp);
726789Sahrens }
727789Sahrens 
728789Sahrens /*
729789Sahrens  * Return the name of the pool.
730789Sahrens  */
731789Sahrens const char *
732789Sahrens zpool_get_name(zpool_handle_t *zhp)
733789Sahrens {
734789Sahrens 	return (zhp->zpool_name);
735789Sahrens }
736789Sahrens 
737789Sahrens 
738789Sahrens /*
739789Sahrens  * Return the state of the pool (ACTIVE or UNAVAILABLE)
740789Sahrens  */
741789Sahrens int
742789Sahrens zpool_get_state(zpool_handle_t *zhp)
743789Sahrens {
744789Sahrens 	return (zhp->zpool_state);
745789Sahrens }
746789Sahrens 
747789Sahrens /*
748789Sahrens  * Create the named pool, using the provided vdev list.  It is assumed
749789Sahrens  * that the consumer has already validated the contents of the nvlist, so we
750789Sahrens  * don't have to worry about error semantics.
751789Sahrens  */
752789Sahrens int
7532082Seschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
7545094Slling     nvlist_t *props)
755789Sahrens {
756789Sahrens 	zfs_cmd_t zc = { 0 };
7572082Seschrock 	char msg[1024];
7585094Slling 	char *altroot;
7592082Seschrock 
7602082Seschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
7612082Seschrock 	    "cannot create '%s'"), pool);
762789Sahrens 
7632082Seschrock 	if (!zpool_name_valid(hdl, B_FALSE, pool))
7642082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
7652082Seschrock 
7665320Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
7675320Slling 		return (-1);
7685320Slling 
7695094Slling 	if (props && (props = zpool_validate_properties(hdl, pool, props,
7705094Slling 	    SPA_VERSION_1, B_TRUE, msg)) == NULL)
7715094Slling 		return (-1);
7725094Slling 
7735320Slling 	if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
7745320Slling 		nvlist_free(props);
7755094Slling 		return (-1);
7765320Slling 	}
777789Sahrens 
778789Sahrens 	(void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
779789Sahrens 
7804543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc) != 0) {
7815320Slling 
7822676Seschrock 		zcmd_free_nvlists(&zc);
7835320Slling 		nvlist_free(props);
7842082Seschrock 
785789Sahrens 		switch (errno) {
786789Sahrens 		case EBUSY:
787789Sahrens 			/*
788789Sahrens 			 * This can happen if the user has specified the same
789789Sahrens 			 * device multiple times.  We can't reliably detect this
790789Sahrens 			 * until we try to add it and see we already have a
791789Sahrens 			 * label.
792789Sahrens 			 */
7932082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7942082Seschrock 			    "one or more vdevs refer to the same device"));
7952082Seschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
796789Sahrens 
797789Sahrens 		case EOVERFLOW:
798789Sahrens 			/*
7992082Seschrock 			 * This occurs when one of the devices is below
800789Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
801789Sahrens 			 * device was the problem device since there's no
802789Sahrens 			 * reliable way to determine device size from userland.
803789Sahrens 			 */
804789Sahrens 			{
805789Sahrens 				char buf[64];
806789Sahrens 
807789Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
808789Sahrens 
8092082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8102082Seschrock 				    "one or more devices is less than the "
8112082Seschrock 				    "minimum size (%s)"), buf);
812789Sahrens 			}
8132082Seschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
814789Sahrens 
815789Sahrens 		case ENOSPC:
8162082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8172082Seschrock 			    "one or more devices is out of space"));
8182082Seschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
819789Sahrens 
8205450Sbrendan 		case ENOTBLK:
8215450Sbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8225450Sbrendan 			    "cache device must be a disk or disk slice"));
8235450Sbrendan 			return (zfs_error(hdl, EZFS_BADDEV, msg));
8245450Sbrendan 
825789Sahrens 		default:
8262082Seschrock 			return (zpool_standard_error(hdl, errno, msg));
827789Sahrens 		}
828789Sahrens 	}
829789Sahrens 
830789Sahrens 	/*
831789Sahrens 	 * If this is an alternate root pool, then we automatically set the
8322676Seschrock 	 * mountpoint of the root dataset to be '/'.
833789Sahrens 	 */
8345094Slling 	if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
8355094Slling 	    &altroot) == 0) {
836789Sahrens 		zfs_handle_t *zhp;
837789Sahrens 
8385094Slling 		verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
8392676Seschrock 		verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
8402676Seschrock 		    "/") == 0);
841789Sahrens 
842789Sahrens 		zfs_close(zhp);
843789Sahrens 	}
844789Sahrens 
8455320Slling 	zcmd_free_nvlists(&zc);
8465320Slling 	nvlist_free(props);
847789Sahrens 	return (0);
848789Sahrens }
849789Sahrens 
850789Sahrens /*
851789Sahrens  * Destroy the given pool.  It is up to the caller to ensure that there are no
852789Sahrens  * datasets left in the pool.
853789Sahrens  */
854789Sahrens int
855789Sahrens zpool_destroy(zpool_handle_t *zhp)
856789Sahrens {
857789Sahrens 	zfs_cmd_t zc = { 0 };
858789Sahrens 	zfs_handle_t *zfp = NULL;
8592082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
8602082Seschrock 	char msg[1024];
861789Sahrens 
862789Sahrens 	if (zhp->zpool_state == POOL_STATE_ACTIVE &&
8632082Seschrock 	    (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name,
8642082Seschrock 	    ZFS_TYPE_FILESYSTEM)) == NULL)
865789Sahrens 		return (-1);
866789Sahrens 
8672856Snd150628 	if (zpool_remove_zvol_links(zhp) != 0)
868789Sahrens 		return (-1);
869789Sahrens 
870789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
871789Sahrens 
8724543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
8732082Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
8742082Seschrock 		    "cannot destroy '%s'"), zhp->zpool_name);
875789Sahrens 
8762082Seschrock 		if (errno == EROFS) {
8772082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8782082Seschrock 			    "one or more devices is read only"));
8792082Seschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
8802082Seschrock 		} else {
8812082Seschrock 			(void) zpool_standard_error(hdl, errno, msg);
882789Sahrens 		}
883789Sahrens 
884789Sahrens 		if (zfp)
885789Sahrens 			zfs_close(zfp);
886789Sahrens 		return (-1);
887789Sahrens 	}
888789Sahrens 
889789Sahrens 	if (zfp) {
890789Sahrens 		remove_mountpoint(zfp);
891789Sahrens 		zfs_close(zfp);
892789Sahrens 	}
893789Sahrens 
894789Sahrens 	return (0);
895789Sahrens }
896789Sahrens 
897789Sahrens /*
898789Sahrens  * Add the given vdevs to the pool.  The caller must have already performed the
899789Sahrens  * necessary verification to ensure that the vdev specification is well-formed.
900789Sahrens  */
901789Sahrens int
902789Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
903789Sahrens {
9042676Seschrock 	zfs_cmd_t zc = { 0 };
9052082Seschrock 	int ret;
9062082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
9072082Seschrock 	char msg[1024];
9085450Sbrendan 	nvlist_t **spares, **l2cache;
9095450Sbrendan 	uint_t nspares, nl2cache;
9102082Seschrock 
9112082Seschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
9122082Seschrock 	    "cannot add to '%s'"), zhp->zpool_name);
9132082Seschrock 
9145450Sbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
9155450Sbrendan 	    SPA_VERSION_SPARES &&
9162082Seschrock 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
9172082Seschrock 	    &spares, &nspares) == 0) {
9182082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
9192082Seschrock 		    "upgraded to add hot spares"));
9202082Seschrock 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
9212082Seschrock 	}
922789Sahrens 
9235450Sbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
9245450Sbrendan 	    SPA_VERSION_L2CACHE &&
9255450Sbrendan 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
9265450Sbrendan 	    &l2cache, &nl2cache) == 0) {
9275450Sbrendan 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
9285450Sbrendan 		    "upgraded to add cache devices"));
9295450Sbrendan 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
9305450Sbrendan 	}
9315450Sbrendan 
9325094Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
9332082Seschrock 		return (-1);
934789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
935789Sahrens 
9364543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
937789Sahrens 		switch (errno) {
938789Sahrens 		case EBUSY:
939789Sahrens 			/*
940789Sahrens 			 * This can happen if the user has specified the same
941789Sahrens 			 * device multiple times.  We can't reliably detect this
942789Sahrens 			 * until we try to add it and see we already have a
943789Sahrens 			 * label.
944789Sahrens 			 */
9452082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9462082Seschrock 			    "one or more vdevs refer to the same device"));
9472082Seschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
948789Sahrens 			break;
949789Sahrens 
950789Sahrens 		case EOVERFLOW:
951789Sahrens 			/*
952789Sahrens 			 * This occurrs when one of the devices is below
953789Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
954789Sahrens 			 * device was the problem device since there's no
955789Sahrens 			 * reliable way to determine device size from userland.
956789Sahrens 			 */
957789Sahrens 			{
958789Sahrens 				char buf[64];
959789Sahrens 
960789Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
961789Sahrens 
9622082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9632082Seschrock 				    "device is less than the minimum "
9642082Seschrock 				    "size (%s)"), buf);
965789Sahrens 			}
9662082Seschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
9672082Seschrock 			break;
9682082Seschrock 
9692082Seschrock 		case ENOTSUP:
9702082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9714527Sperrin 			    "pool must be upgraded to add these vdevs"));
9722082Seschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, msg);
973789Sahrens 			break;
974789Sahrens 
9753912Slling 		case EDOM:
9763912Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9774527Sperrin 			    "root pool can not have multiple vdevs"
9784527Sperrin 			    " or separate logs"));
9793912Slling 			(void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
9803912Slling 			break;
9813912Slling 
9825450Sbrendan 		case ENOTBLK:
9835450Sbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9845450Sbrendan 			    "cache device must be a disk or disk slice"));
9855450Sbrendan 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
9865450Sbrendan 			break;
9875450Sbrendan 
988789Sahrens 		default:
9892082Seschrock 			(void) zpool_standard_error(hdl, errno, msg);
990789Sahrens 		}
991789Sahrens 
9922082Seschrock 		ret = -1;
9932082Seschrock 	} else {
9942082Seschrock 		ret = 0;
995789Sahrens 	}
996789Sahrens 
9972676Seschrock 	zcmd_free_nvlists(&zc);
998789Sahrens 
9992082Seschrock 	return (ret);
1000789Sahrens }
1001789Sahrens 
1002789Sahrens /*
1003789Sahrens  * Exports the pool from the system.  The caller must ensure that there are no
1004789Sahrens  * mounted datasets in the pool.
1005789Sahrens  */
1006789Sahrens int
1007789Sahrens zpool_export(zpool_handle_t *zhp)
1008789Sahrens {
1009789Sahrens 	zfs_cmd_t zc = { 0 };
1010789Sahrens 
1011789Sahrens 	if (zpool_remove_zvol_links(zhp) != 0)
1012789Sahrens 		return (-1);
1013789Sahrens 
1014789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1015789Sahrens 
10164543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0)
10173237Slling 		return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
10182082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot export '%s'"),
10192082Seschrock 		    zhp->zpool_name));
1020789Sahrens 	return (0);
1021789Sahrens }
1022789Sahrens 
1023789Sahrens /*
10245094Slling  * zpool_import() is a contracted interface. Should be kept the same
10255094Slling  * if possible.
10265094Slling  *
10275094Slling  * Applications should use zpool_import_props() to import a pool with
10285094Slling  * new properties value to be set.
1029789Sahrens  */
1030789Sahrens int
10312082Seschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
10325094Slling     char *altroot)
10335094Slling {
10345094Slling 	nvlist_t *props = NULL;
10355094Slling 	int ret;
10365094Slling 
10375094Slling 	if (altroot != NULL) {
10385094Slling 		if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
10395094Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
10405094Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
10415094Slling 			    newname));
10425094Slling 		}
10435094Slling 
10445094Slling 		if (nvlist_add_string(props,
10455094Slling 		    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0) {
10465094Slling 			nvlist_free(props);
10475094Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
10485094Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
10495094Slling 			    newname));
10505094Slling 		}
10515094Slling 	}
10525094Slling 
10536643Seschrock 	ret = zpool_import_props(hdl, config, newname, props, B_FALSE);
10545094Slling 	if (props)
10555094Slling 		nvlist_free(props);
10565094Slling 	return (ret);
10575094Slling }
10585094Slling 
10595094Slling /*
10605094Slling  * Import the given pool using the known configuration and a list of
10615094Slling  * properties to be set. The configuration should have come from
10625094Slling  * zpool_find_import(). The 'newname' parameters control whether the pool
10635094Slling  * is imported with a different name.
10645094Slling  */
10655094Slling int
10665094Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
10676643Seschrock     nvlist_t *props, boolean_t importfaulted)
1068789Sahrens {
10692676Seschrock 	zfs_cmd_t zc = { 0 };
1070789Sahrens 	char *thename;
1071789Sahrens 	char *origname;
1072789Sahrens 	int ret;
10735094Slling 	char errbuf[1024];
1074789Sahrens 
1075789Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1076789Sahrens 	    &origname) == 0);
1077789Sahrens 
10785094Slling 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
10795094Slling 	    "cannot import pool '%s'"), origname);
10805094Slling 
1081789Sahrens 	if (newname != NULL) {
10822082Seschrock 		if (!zpool_name_valid(hdl, B_FALSE, newname))
10833237Slling 			return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
10842082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
10852082Seschrock 			    newname));
1086789Sahrens 		thename = (char *)newname;
1087789Sahrens 	} else {
1088789Sahrens 		thename = origname;
1089789Sahrens 	}
1090789Sahrens 
10915094Slling 	if (props) {
10925094Slling 		uint64_t version;
10935094Slling 
10945094Slling 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
10955094Slling 		    &version) == 0);
10965094Slling 
10975094Slling 		if ((props = zpool_validate_properties(hdl, origname,
10985320Slling 		    props, version, B_TRUE, errbuf)) == NULL) {
10995094Slling 			return (-1);
11005320Slling 		} else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
11015320Slling 			nvlist_free(props);
11025094Slling 			return (-1);
11035320Slling 		}
11045094Slling 	}
1105789Sahrens 
1106789Sahrens 	(void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1107789Sahrens 
1108789Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
11091544Seschrock 	    &zc.zc_guid) == 0);
1110789Sahrens 
11115320Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
11125320Slling 		nvlist_free(props);
11132082Seschrock 		return (-1);
11145320Slling 	}
1115789Sahrens 
11166643Seschrock 	zc.zc_cookie = (uint64_t)importfaulted;
1117789Sahrens 	ret = 0;
11184543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) {
1119789Sahrens 		char desc[1024];
1120789Sahrens 		if (newname == NULL)
1121789Sahrens 			(void) snprintf(desc, sizeof (desc),
1122789Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1123789Sahrens 			    thename);
1124789Sahrens 		else
1125789Sahrens 			(void) snprintf(desc, sizeof (desc),
1126789Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1127789Sahrens 			    origname, thename);
1128789Sahrens 
1129789Sahrens 		switch (errno) {
11301544Seschrock 		case ENOTSUP:
11311544Seschrock 			/*
11321544Seschrock 			 * Unsupported version.
11331544Seschrock 			 */
11342082Seschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, desc);
11351544Seschrock 			break;
11361544Seschrock 
11372174Seschrock 		case EINVAL:
11382174Seschrock 			(void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
11392174Seschrock 			break;
11402174Seschrock 
1141789Sahrens 		default:
11422082Seschrock 			(void) zpool_standard_error(hdl, errno, desc);
1143789Sahrens 		}
1144789Sahrens 
1145789Sahrens 		ret = -1;
1146789Sahrens 	} else {
1147789Sahrens 		zpool_handle_t *zhp;
11484543Smarks 
1149789Sahrens 		/*
1150789Sahrens 		 * This should never fail, but play it safe anyway.
1151789Sahrens 		 */
11522142Seschrock 		if (zpool_open_silent(hdl, thename, &zhp) != 0) {
11532142Seschrock 			ret = -1;
11542142Seschrock 		} else if (zhp != NULL) {
1155789Sahrens 			ret = zpool_create_zvol_links(zhp);
1156789Sahrens 			zpool_close(zhp);
1157789Sahrens 		}
11584543Smarks 
1159789Sahrens 	}
1160789Sahrens 
11612676Seschrock 	zcmd_free_nvlists(&zc);
11625320Slling 	nvlist_free(props);
11635320Slling 
1164789Sahrens 	return (ret);
1165789Sahrens }
1166789Sahrens 
1167789Sahrens /*
1168789Sahrens  * Scrub the pool.
1169789Sahrens  */
1170789Sahrens int
1171789Sahrens zpool_scrub(zpool_handle_t *zhp, pool_scrub_type_t type)
1172789Sahrens {
1173789Sahrens 	zfs_cmd_t zc = { 0 };
1174789Sahrens 	char msg[1024];
11752082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1176789Sahrens 
1177789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1178789Sahrens 	zc.zc_cookie = type;
1179789Sahrens 
11804543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCRUB, &zc) == 0)
1181789Sahrens 		return (0);
1182789Sahrens 
1183789Sahrens 	(void) snprintf(msg, sizeof (msg),
1184789Sahrens 	    dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1185789Sahrens 
11862082Seschrock 	if (errno == EBUSY)
11872082Seschrock 		return (zfs_error(hdl, EZFS_RESILVERING, msg));
11882082Seschrock 	else
11892082Seschrock 		return (zpool_standard_error(hdl, errno, msg));
1190789Sahrens }
1191789Sahrens 
11922468Sek110237 /*
11932468Sek110237  * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
11942468Sek110237  * spare; but FALSE if its an INUSE spare.
11952468Sek110237  */
11962082Seschrock static nvlist_t *
11972082Seschrock vdev_to_nvlist_iter(nvlist_t *nv, const char *search, uint64_t guid,
11985450Sbrendan     boolean_t *avail_spare, boolean_t *l2cache)
11991544Seschrock {
12001544Seschrock 	uint_t c, children;
12011544Seschrock 	nvlist_t **child;
12022082Seschrock 	uint64_t theguid, present;
12031544Seschrock 	char *path;
12041544Seschrock 	uint64_t wholedisk = 0;
12052082Seschrock 	nvlist_t *ret;
12061544Seschrock 
12072082Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &theguid) == 0);
12081544Seschrock 
12091544Seschrock 	if (search == NULL &&
12101544Seschrock 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &present) == 0) {
12111544Seschrock 		/*
12121544Seschrock 		 * If the device has never been present since import, the only
12131544Seschrock 		 * reliable way to match the vdev is by GUID.
12141544Seschrock 		 */
12152082Seschrock 		if (theguid == guid)
12162082Seschrock 			return (nv);
12171544Seschrock 	} else if (search != NULL &&
12181544Seschrock 	    nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
12191544Seschrock 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
12201544Seschrock 		    &wholedisk);
12211544Seschrock 		if (wholedisk) {
12221544Seschrock 			/*
12231544Seschrock 			 * For whole disks, the internal path has 's0', but the
12241544Seschrock 			 * path passed in by the user doesn't.
12251544Seschrock 			 */
12261544Seschrock 			if (strlen(search) == strlen(path) - 2 &&
12271544Seschrock 			    strncmp(search, path, strlen(search)) == 0)
12282082Seschrock 				return (nv);
12291544Seschrock 		} else if (strcmp(search, path) == 0) {
12302082Seschrock 			return (nv);
12311544Seschrock 		}
12321544Seschrock 	}
12331544Seschrock 
12341544Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
12351544Seschrock 	    &child, &children) != 0)
12362082Seschrock 		return (NULL);
12371544Seschrock 
12381544Seschrock 	for (c = 0; c < children; c++)
12392082Seschrock 		if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
12405450Sbrendan 		    avail_spare, l2cache)) != NULL)
12411544Seschrock 			return (ret);
12421544Seschrock 
12432082Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
12442082Seschrock 	    &child, &children) == 0) {
12452082Seschrock 		for (c = 0; c < children; c++) {
12462082Seschrock 			if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
12475450Sbrendan 			    avail_spare, l2cache)) != NULL) {
12482468Sek110237 				*avail_spare = B_TRUE;
12492082Seschrock 				return (ret);
12502082Seschrock 			}
12512082Seschrock 		}
12522082Seschrock 	}
12532082Seschrock 
12545450Sbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
12555450Sbrendan 	    &child, &children) == 0) {
12565450Sbrendan 		for (c = 0; c < children; c++) {
12575450Sbrendan 			if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
12585450Sbrendan 			    avail_spare, l2cache)) != NULL) {
12595450Sbrendan 				*l2cache = B_TRUE;
12605450Sbrendan 				return (ret);
12615450Sbrendan 			}
12625450Sbrendan 		}
12635450Sbrendan 	}
12645450Sbrendan 
12652082Seschrock 	return (NULL);
12661544Seschrock }
12671544Seschrock 
12682082Seschrock nvlist_t *
12695450Sbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
12705450Sbrendan     boolean_t *l2cache)
12711544Seschrock {
12721544Seschrock 	char buf[MAXPATHLEN];
12731544Seschrock 	const char *search;
12741544Seschrock 	char *end;
12751544Seschrock 	nvlist_t *nvroot;
12761544Seschrock 	uint64_t guid;
12771544Seschrock 
12781613Seschrock 	guid = strtoull(path, &end, 10);
12791544Seschrock 	if (guid != 0 && *end == '\0') {
12801544Seschrock 		search = NULL;
12811544Seschrock 	} else if (path[0] != '/') {
12821544Seschrock 		(void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path);
12831544Seschrock 		search = buf;
12841544Seschrock 	} else {
12851544Seschrock 		search = path;
12861544Seschrock 	}
12871544Seschrock 
12881544Seschrock 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
12891544Seschrock 	    &nvroot) == 0);
12901544Seschrock 
12912468Sek110237 	*avail_spare = B_FALSE;
12925450Sbrendan 	*l2cache = B_FALSE;
12935450Sbrendan 	return (vdev_to_nvlist_iter(nvroot, search, guid, avail_spare,
12945450Sbrendan 	    l2cache));
12952468Sek110237 }
12962468Sek110237 
12972468Sek110237 /*
12985450Sbrendan  * Returns TRUE if the given guid corresponds to the given type.
12995450Sbrendan  * This is used to check for hot spares (INUSE or not), and level 2 cache
13005450Sbrendan  * devices.
13012468Sek110237  */
13022468Sek110237 static boolean_t
13035450Sbrendan is_guid_type(zpool_handle_t *zhp, uint64_t guid, const char *type)
13042468Sek110237 {
13055450Sbrendan 	uint64_t target_guid;
13062468Sek110237 	nvlist_t *nvroot;
13075450Sbrendan 	nvlist_t **list;
13085450Sbrendan 	uint_t count;
13092468Sek110237 	int i;
13102468Sek110237 
13112468Sek110237 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
13122468Sek110237 	    &nvroot) == 0);
13135450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, type, &list, &count) == 0) {
13145450Sbrendan 		for (i = 0; i < count; i++) {
13155450Sbrendan 			verify(nvlist_lookup_uint64(list[i], ZPOOL_CONFIG_GUID,
13165450Sbrendan 			    &target_guid) == 0);
13175450Sbrendan 			if (guid == target_guid)
13182468Sek110237 				return (B_TRUE);
13192468Sek110237 		}
13202468Sek110237 	}
13212468Sek110237 
13222468Sek110237 	return (B_FALSE);
13231544Seschrock }
13241544Seschrock 
1325789Sahrens /*
13264451Seschrock  * Bring the specified vdev online.   The 'flags' parameter is a set of the
13274451Seschrock  * ZFS_ONLINE_* flags.
1328789Sahrens  */
1329789Sahrens int
13304451Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
13314451Seschrock     vdev_state_t *newstate)
1332789Sahrens {
1333789Sahrens 	zfs_cmd_t zc = { 0 };
1334789Sahrens 	char msg[1024];
13352082Seschrock 	nvlist_t *tgt;
13365450Sbrendan 	boolean_t avail_spare, l2cache;
13372082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1338789Sahrens 
13391544Seschrock 	(void) snprintf(msg, sizeof (msg),
13401544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot online %s"), path);
1341789Sahrens 
13421544Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
13435450Sbrendan 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == NULL)
13442082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
1345789Sahrens 
13462468Sek110237 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
13472468Sek110237 
13485450Sbrendan 	if (avail_spare ||
13495450Sbrendan 	    is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
13502082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
13512082Seschrock 
13524451Seschrock 	zc.zc_cookie = VDEV_STATE_ONLINE;
13534451Seschrock 	zc.zc_obj = flags;
13544451Seschrock 
13554543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0)
13564451Seschrock 		return (zpool_standard_error(hdl, errno, msg));
13574451Seschrock 
13584451Seschrock 	*newstate = zc.zc_cookie;
13594451Seschrock 	return (0);
1360789Sahrens }
1361789Sahrens 
1362789Sahrens /*
1363789Sahrens  * Take the specified vdev offline
1364789Sahrens  */
1365789Sahrens int
13664451Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
1367789Sahrens {
1368789Sahrens 	zfs_cmd_t zc = { 0 };
1369789Sahrens 	char msg[1024];
13702082Seschrock 	nvlist_t *tgt;
13715450Sbrendan 	boolean_t avail_spare, l2cache;
13722082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1373789Sahrens 
13741544Seschrock 	(void) snprintf(msg, sizeof (msg),
13751544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
13761544Seschrock 
1377789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
13785450Sbrendan 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == NULL)
13792082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
13802082Seschrock 
13812468Sek110237 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
13822468Sek110237 
13835450Sbrendan 	if (avail_spare ||
13845450Sbrendan 	    is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
13852082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
13862082Seschrock 
13874451Seschrock 	zc.zc_cookie = VDEV_STATE_OFFLINE;
13884451Seschrock 	zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
13891485Slling 
13904543Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
1391789Sahrens 		return (0);
1392789Sahrens 
1393789Sahrens 	switch (errno) {
13942082Seschrock 	case EBUSY:
1395789Sahrens 
1396789Sahrens 		/*
1397789Sahrens 		 * There are no other replicas of this device.
1398789Sahrens 		 */
13992082Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
14002082Seschrock 
14012082Seschrock 	default:
14022082Seschrock 		return (zpool_standard_error(hdl, errno, msg));
14032082Seschrock 	}
14042082Seschrock }
1405789Sahrens 
14062082Seschrock /*
14074451Seschrock  * Mark the given vdev faulted.
14084451Seschrock  */
14094451Seschrock int
14104451Seschrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid)
14114451Seschrock {
14124451Seschrock 	zfs_cmd_t zc = { 0 };
14134451Seschrock 	char msg[1024];
14144451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
14154451Seschrock 
14164451Seschrock 	(void) snprintf(msg, sizeof (msg),
14174451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
14184451Seschrock 
14194451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
14204451Seschrock 	zc.zc_guid = guid;
14214451Seschrock 	zc.zc_cookie = VDEV_STATE_FAULTED;
14224451Seschrock 
14234451Seschrock 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
14244451Seschrock 		return (0);
14254451Seschrock 
14264451Seschrock 	switch (errno) {
14274451Seschrock 	case EBUSY:
14284451Seschrock 
14294451Seschrock 		/*
14304451Seschrock 		 * There are no other replicas of this device.
14314451Seschrock 		 */
14324451Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
14334451Seschrock 
14344451Seschrock 	default:
14354451Seschrock 		return (zpool_standard_error(hdl, errno, msg));
14364451Seschrock 	}
14374451Seschrock 
14384451Seschrock }
14394451Seschrock 
14404451Seschrock /*
14414451Seschrock  * Mark the given vdev degraded.
14424451Seschrock  */
14434451Seschrock int
14444451Seschrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid)
14454451Seschrock {
14464451Seschrock 	zfs_cmd_t zc = { 0 };
14474451Seschrock 	char msg[1024];
14484451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
14494451Seschrock 
14504451Seschrock 	(void) snprintf(msg, sizeof (msg),
14514451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
14524451Seschrock 
14534451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
14544451Seschrock 	zc.zc_guid = guid;
14554451Seschrock 	zc.zc_cookie = VDEV_STATE_DEGRADED;
14564451Seschrock 
14574451Seschrock 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
14584451Seschrock 		return (0);
14594451Seschrock 
14604451Seschrock 	return (zpool_standard_error(hdl, errno, msg));
14614451Seschrock }
14624451Seschrock 
14634451Seschrock /*
14642082Seschrock  * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
14652082Seschrock  * a hot spare.
14662082Seschrock  */
14672082Seschrock static boolean_t
14682082Seschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
14692082Seschrock {
14702082Seschrock 	nvlist_t **child;
14712082Seschrock 	uint_t c, children;
14722082Seschrock 	char *type;
14732082Seschrock 
14742082Seschrock 	if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
14752082Seschrock 	    &children) == 0) {
14762082Seschrock 		verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
14772082Seschrock 		    &type) == 0);
14782082Seschrock 
14792082Seschrock 		if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
14802082Seschrock 		    children == 2 && child[which] == tgt)
14812082Seschrock 			return (B_TRUE);
14822082Seschrock 
14832082Seschrock 		for (c = 0; c < children; c++)
14842082Seschrock 			if (is_replacing_spare(child[c], tgt, which))
14852082Seschrock 				return (B_TRUE);
1486789Sahrens 	}
14872082Seschrock 
14882082Seschrock 	return (B_FALSE);
1489789Sahrens }
1490789Sahrens 
1491789Sahrens /*
1492789Sahrens  * Attach new_disk (fully described by nvroot) to old_disk.
14934527Sperrin  * If 'replacing' is specified, the new disk will replace the old one.
1494789Sahrens  */
1495789Sahrens int
1496789Sahrens zpool_vdev_attach(zpool_handle_t *zhp,
1497789Sahrens     const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
1498789Sahrens {
1499789Sahrens 	zfs_cmd_t zc = { 0 };
1500789Sahrens 	char msg[1024];
1501789Sahrens 	int ret;
15022082Seschrock 	nvlist_t *tgt;
15035450Sbrendan 	boolean_t avail_spare, l2cache;
15044527Sperrin 	uint64_t val, is_log;
1505*7041Seschrock 	char *path, *newname;
15062082Seschrock 	nvlist_t **child;
15072082Seschrock 	uint_t children;
15082082Seschrock 	nvlist_t *config_root;
15092082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1510789Sahrens 
15111544Seschrock 	if (replacing)
15121544Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
15131544Seschrock 		    "cannot replace %s with %s"), old_disk, new_disk);
15141544Seschrock 	else
15151544Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
15161544Seschrock 		    "cannot attach %s to %s"), new_disk, old_disk);
15171544Seschrock 
1518789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
15195450Sbrendan 	if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache)) == 0)
15202082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
15212082Seschrock 
15222468Sek110237 	if (avail_spare)
15232082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
15242082Seschrock 
15255450Sbrendan 	if (l2cache)
15265450Sbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
15275450Sbrendan 
15282082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
15292082Seschrock 	zc.zc_cookie = replacing;
15302082Seschrock 
15312082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
15322082Seschrock 	    &child, &children) != 0 || children != 1) {
15332082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15342082Seschrock 		    "new device must be a single disk"));
15352082Seschrock 		return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
15361544Seschrock 	}
15372082Seschrock 
15382082Seschrock 	verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
15392082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
15402082Seschrock 
1541*7041Seschrock 	if ((newname = zpool_vdev_name(NULL, NULL, child[0])) == NULL)
1542*7041Seschrock 		return (-1);
1543*7041Seschrock 
15442082Seschrock 	/*
15452082Seschrock 	 * If the target is a hot spare that has been swapped in, we can only
15462082Seschrock 	 * replace it with another hot spare.
15472082Seschrock 	 */
15482082Seschrock 	if (replacing &&
15492082Seschrock 	    nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
1550*7041Seschrock 	    (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache) == NULL ||
15512468Sek110237 	    !avail_spare) && is_replacing_spare(config_root, tgt, 1)) {
15522082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15532082Seschrock 		    "can only be replaced by another hot spare"));
1554*7041Seschrock 		free(newname);
15552082Seschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
15562082Seschrock 	}
15572082Seschrock 
15582082Seschrock 	/*
15592082Seschrock 	 * If we are attempting to replace a spare, it canot be applied to an
15602082Seschrock 	 * already spared device.
15612082Seschrock 	 */
15622082Seschrock 	if (replacing &&
15632082Seschrock 	    nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 &&
1564*7041Seschrock 	    zpool_find_vdev(zhp, newname, &avail_spare, &l2cache) != NULL &&
15655450Sbrendan 	    avail_spare && is_replacing_spare(config_root, tgt, 0)) {
15662082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15672082Seschrock 		    "device has already been replaced with a spare"));
1568*7041Seschrock 		free(newname);
15692082Seschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
15702082Seschrock 	}
1571789Sahrens 
1572*7041Seschrock 	free(newname);
1573*7041Seschrock 
15745094Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
15752082Seschrock 		return (-1);
1576789Sahrens 
15774543Smarks 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc);
1578789Sahrens 
15792676Seschrock 	zcmd_free_nvlists(&zc);
1580789Sahrens 
1581789Sahrens 	if (ret == 0)
1582789Sahrens 		return (0);
1583789Sahrens 
1584789Sahrens 	switch (errno) {
15851544Seschrock 	case ENOTSUP:
1586789Sahrens 		/*
1587789Sahrens 		 * Can't attach to or replace this type of vdev.
1588789Sahrens 		 */
15894527Sperrin 		if (replacing) {
15904527Sperrin 			is_log = B_FALSE;
15914527Sperrin 			(void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_LOG,
15924527Sperrin 			    &is_log);
15934527Sperrin 			if (is_log)
15944527Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15954527Sperrin 				    "cannot replace a log with a spare"));
15964527Sperrin 			else
15974527Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15984527Sperrin 				    "cannot replace a replacing device"));
15994527Sperrin 		} else {
16002082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16012082Seschrock 			    "can only attach to mirrors and top-level "
16022082Seschrock 			    "disks"));
16034527Sperrin 		}
16042082Seschrock 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
1605789Sahrens 		break;
1606789Sahrens 
16071544Seschrock 	case EINVAL:
1608789Sahrens 		/*
1609789Sahrens 		 * The new device must be a single disk.
1610789Sahrens 		 */
16112082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16122082Seschrock 		    "new device must be a single disk"));
16132082Seschrock 		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
1614789Sahrens 		break;
1615789Sahrens 
16161544Seschrock 	case EBUSY:
16172082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
16182082Seschrock 		    new_disk);
16192082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1620789Sahrens 		break;
1621789Sahrens 
16221544Seschrock 	case EOVERFLOW:
1623789Sahrens 		/*
1624789Sahrens 		 * The new device is too small.
1625789Sahrens 		 */
16262082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16272082Seschrock 		    "device is too small"));
16282082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1629789Sahrens 		break;
1630789Sahrens 
16311544Seschrock 	case EDOM:
1632789Sahrens 		/*
1633789Sahrens 		 * The new device has a different alignment requirement.
1634789Sahrens 		 */
16352082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16362082Seschrock 		    "devices have different sector alignment"));
16372082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1638789Sahrens 		break;
1639789Sahrens 
16401544Seschrock 	case ENAMETOOLONG:
1641789Sahrens 		/*
1642789Sahrens 		 * The resulting top-level vdev spec won't fit in the label.
1643789Sahrens 		 */
16442082Seschrock 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
1645789Sahrens 		break;
1646789Sahrens 
16471544Seschrock 	default:
16482082Seschrock 		(void) zpool_standard_error(hdl, errno, msg);
1649789Sahrens 	}
1650789Sahrens 
16512082Seschrock 	return (-1);
1652789Sahrens }
1653789Sahrens 
1654789Sahrens /*
1655789Sahrens  * Detach the specified device.
1656789Sahrens  */
1657789Sahrens int
1658789Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
1659789Sahrens {
1660789Sahrens 	zfs_cmd_t zc = { 0 };
1661789Sahrens 	char msg[1024];
16622082Seschrock 	nvlist_t *tgt;
16635450Sbrendan 	boolean_t avail_spare, l2cache;
16642082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1665789Sahrens 
16661544Seschrock 	(void) snprintf(msg, sizeof (msg),
16671544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
16681544Seschrock 
1669789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
16705450Sbrendan 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == 0)
16712082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
1672789Sahrens 
16732468Sek110237 	if (avail_spare)
16742082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
16752082Seschrock 
16765450Sbrendan 	if (l2cache)
16775450Sbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
16785450Sbrendan 
16792082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
16802082Seschrock 
16814543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
1682789Sahrens 		return (0);
1683789Sahrens 
1684789Sahrens 	switch (errno) {
1685789Sahrens 
16861544Seschrock 	case ENOTSUP:
1687789Sahrens 		/*
1688789Sahrens 		 * Can't detach from this type of vdev.
1689789Sahrens 		 */
16902082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
16912082Seschrock 		    "applicable to mirror and replacing vdevs"));
16922082Seschrock 		(void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg);
1693789Sahrens 		break;
1694789Sahrens 
16951544Seschrock 	case EBUSY:
1696789Sahrens 		/*
1697789Sahrens 		 * There are no other replicas of this device.
1698789Sahrens 		 */
16992082Seschrock 		(void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
1700789Sahrens 		break;
1701789Sahrens 
17021544Seschrock 	default:
17032082Seschrock 		(void) zpool_standard_error(hdl, errno, msg);
17041544Seschrock 	}
17051544Seschrock 
17062082Seschrock 	return (-1);
17072082Seschrock }
17082082Seschrock 
17092082Seschrock /*
17105450Sbrendan  * Remove the given device.  Currently, this is supported only for hot spares
17115450Sbrendan  * and level 2 cache devices.
17122082Seschrock  */
17132082Seschrock int
17142082Seschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
17152082Seschrock {
17162082Seschrock 	zfs_cmd_t zc = { 0 };
17172082Seschrock 	char msg[1024];
17182082Seschrock 	nvlist_t *tgt;
17195450Sbrendan 	boolean_t avail_spare, l2cache;
17202082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
17212082Seschrock 
17222082Seschrock 	(void) snprintf(msg, sizeof (msg),
17232082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
17242082Seschrock 
17252082Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
17265450Sbrendan 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == 0)
17272082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
17282082Seschrock 
17295450Sbrendan 	if (!avail_spare && !l2cache) {
17302082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17315450Sbrendan 		    "only inactive hot spares or cache devices "
17325450Sbrendan 		    "can be removed"));
17332082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
17342082Seschrock 	}
17352082Seschrock 
17362082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
17372082Seschrock 
17384543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
17392082Seschrock 		return (0);
17402082Seschrock 
17412082Seschrock 	return (zpool_standard_error(hdl, errno, msg));
17421544Seschrock }
17431544Seschrock 
17441544Seschrock /*
17451544Seschrock  * Clear the errors for the pool, or the particular device if specified.
17461544Seschrock  */
17471544Seschrock int
17481544Seschrock zpool_clear(zpool_handle_t *zhp, const char *path)
17491544Seschrock {
17501544Seschrock 	zfs_cmd_t zc = { 0 };
17511544Seschrock 	char msg[1024];
17522082Seschrock 	nvlist_t *tgt;
17535450Sbrendan 	boolean_t avail_spare, l2cache;
17542082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
17551544Seschrock 
17561544Seschrock 	if (path)
17571544Seschrock 		(void) snprintf(msg, sizeof (msg),
17581544Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
17592676Seschrock 		    path);
17601544Seschrock 	else
17611544Seschrock 		(void) snprintf(msg, sizeof (msg),
17621544Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
17631544Seschrock 		    zhp->zpool_name);
17641544Seschrock 
17651544Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
17662082Seschrock 	if (path) {
17675450Sbrendan 		if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
17685450Sbrendan 		    &l2cache)) == 0)
17692082Seschrock 			return (zfs_error(hdl, EZFS_NODEVICE, msg));
17702082Seschrock 
17715450Sbrendan 		/*
17725450Sbrendan 		 * Don't allow error clearing for hot spares.  Do allow
17735450Sbrendan 		 * error clearing for l2cache devices.
17745450Sbrendan 		 */
17752468Sek110237 		if (avail_spare)
17762082Seschrock 			return (zfs_error(hdl, EZFS_ISSPARE, msg));
17772082Seschrock 
17782082Seschrock 		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
17792082Seschrock 		    &zc.zc_guid) == 0);
17801544Seschrock 	}
17811544Seschrock 
17824543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0)
17831544Seschrock 		return (0);
17841544Seschrock 
17852082Seschrock 	return (zpool_standard_error(hdl, errno, msg));
1786789Sahrens }
1787789Sahrens 
17883126Sahl /*
17894451Seschrock  * Similar to zpool_clear(), but takes a GUID (used by fmd).
17904451Seschrock  */
17914451Seschrock int
17924451Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
17934451Seschrock {
17944451Seschrock 	zfs_cmd_t zc = { 0 };
17954451Seschrock 	char msg[1024];
17964451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
17974451Seschrock 
17984451Seschrock 	(void) snprintf(msg, sizeof (msg),
17994451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
18004451Seschrock 	    guid);
18014451Seschrock 
18024451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
18034451Seschrock 	zc.zc_guid = guid;
18044451Seschrock 
18054451Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
18064451Seschrock 		return (0);
18074451Seschrock 
18084451Seschrock 	return (zpool_standard_error(hdl, errno, msg));
18094451Seschrock }
18104451Seschrock 
18114451Seschrock /*
18123126Sahl  * Iterate over all zvols in a given pool by walking the /dev/zvol/dsk/<pool>
18133126Sahl  * hierarchy.
18143126Sahl  */
18153126Sahl int
18163126Sahl zpool_iter_zvol(zpool_handle_t *zhp, int (*cb)(const char *, void *),
18173126Sahl     void *data)
1818789Sahrens {
18193126Sahl 	libzfs_handle_t *hdl = zhp->zpool_hdl;
18203126Sahl 	char (*paths)[MAXPATHLEN];
18213126Sahl 	size_t size = 4;
18223126Sahl 	int curr, fd, base, ret = 0;
18233126Sahl 	DIR *dirp;
18243126Sahl 	struct dirent *dp;
18253126Sahl 	struct stat st;
18263126Sahl 
18273126Sahl 	if ((base = open("/dev/zvol/dsk", O_RDONLY)) < 0)
18283126Sahl 		return (errno == ENOENT ? 0 : -1);
18293126Sahl 
18303126Sahl 	if (fstatat(base, zhp->zpool_name, &st, 0) != 0) {
18313126Sahl 		int err = errno;
18323126Sahl 		(void) close(base);
18333126Sahl 		return (err == ENOENT ? 0 : -1);
18343126Sahl 	}
1835789Sahrens 
1836789Sahrens 	/*
18373126Sahl 	 * Oddly this wasn't a directory -- ignore that failure since we
18383126Sahl 	 * know there are no links lower in the (non-existant) hierarchy.
1839789Sahrens 	 */
18403126Sahl 	if (!S_ISDIR(st.st_mode)) {
18413126Sahl 		(void) close(base);
18423126Sahl 		return (0);
18433126Sahl 	}
18443126Sahl 
18453126Sahl 	if ((paths = zfs_alloc(hdl, size * sizeof (paths[0]))) == NULL) {
18463126Sahl 		(void) close(base);
18473126Sahl 		return (-1);
1848789Sahrens 	}
1849789Sahrens 
18503126Sahl 	(void) strlcpy(paths[0], zhp->zpool_name, sizeof (paths[0]));
18513126Sahl 	curr = 0;
18523126Sahl 
18533126Sahl 	while (curr >= 0) {
18543126Sahl 		if (fstatat(base, paths[curr], &st, AT_SYMLINK_NOFOLLOW) != 0)
18553126Sahl 			goto err;
18563126Sahl 
18573126Sahl 		if (S_ISDIR(st.st_mode)) {
18583126Sahl 			if ((fd = openat(base, paths[curr], O_RDONLY)) < 0)
18593126Sahl 				goto err;
18603126Sahl 
18613126Sahl 			if ((dirp = fdopendir(fd)) == NULL) {
18623126Sahl 				(void) close(fd);
18633126Sahl 				goto err;
18643126Sahl 			}
18653126Sahl 
18663126Sahl 			while ((dp = readdir(dirp)) != NULL) {
18673126Sahl 				if (dp->d_name[0] == '.')
18683126Sahl 					continue;
18693126Sahl 
18703126Sahl 				if (curr + 1 == size) {
18713126Sahl 					paths = zfs_realloc(hdl, paths,
18723126Sahl 					    size * sizeof (paths[0]),
18733126Sahl 					    size * 2 * sizeof (paths[0]));
18743126Sahl 					if (paths == NULL) {
18753126Sahl 						(void) closedir(dirp);
18763126Sahl 						(void) close(fd);
18773126Sahl 						goto err;
18783126Sahl 					}
18793126Sahl 
18803126Sahl 					size *= 2;
18813126Sahl 				}
18823126Sahl 
18833126Sahl 				(void) strlcpy(paths[curr + 1], paths[curr],
18843126Sahl 				    sizeof (paths[curr + 1]));
18853126Sahl 				(void) strlcat(paths[curr], "/",
18863126Sahl 				    sizeof (paths[curr]));
18873126Sahl 				(void) strlcat(paths[curr], dp->d_name,
18883126Sahl 				    sizeof (paths[curr]));
18893126Sahl 				curr++;
18903126Sahl 			}
18913126Sahl 
18923126Sahl 			(void) closedir(dirp);
18933126Sahl 
18943126Sahl 		} else {
18953126Sahl 			if ((ret = cb(paths[curr], data)) != 0)
18963126Sahl 				break;
18973126Sahl 		}
18983126Sahl 
18993126Sahl 		curr--;
19003126Sahl 	}
19013126Sahl 
19023126Sahl 	free(paths);
19033126Sahl 	(void) close(base);
19043126Sahl 
19053126Sahl 	return (ret);
19063126Sahl 
19073126Sahl err:
19083126Sahl 	free(paths);
19093126Sahl 	(void) close(base);
19103126Sahl 	return (-1);
19113126Sahl }
19123126Sahl 
19133126Sahl typedef struct zvol_cb {
19143126Sahl 	zpool_handle_t *zcb_pool;
19153126Sahl 	boolean_t zcb_create;
19163126Sahl } zvol_cb_t;
19173126Sahl 
19183126Sahl /*ARGSUSED*/
19193126Sahl static int
19203126Sahl do_zvol_create(zfs_handle_t *zhp, void *data)
19213126Sahl {
19224657Sahrens 	int ret = 0;
19233126Sahl 
19244657Sahrens 	if (ZFS_IS_VOLUME(zhp)) {
19253126Sahl 		(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
19264657Sahrens 		ret = zfs_iter_snapshots(zhp, do_zvol_create, NULL);
19274657Sahrens 	}
19283126Sahl 
19294657Sahrens 	if (ret == 0)
19304657Sahrens 		ret = zfs_iter_filesystems(zhp, do_zvol_create, NULL);
1931789Sahrens 
1932789Sahrens 	zfs_close(zhp);
19333126Sahl 
1934789Sahrens 	return (ret);
1935789Sahrens }
1936789Sahrens 
1937789Sahrens /*
1938789Sahrens  * Iterate over all zvols in the pool and make any necessary minor nodes.
1939789Sahrens  */
1940789Sahrens int
1941789Sahrens zpool_create_zvol_links(zpool_handle_t *zhp)
1942789Sahrens {
1943789Sahrens 	zfs_handle_t *zfp;
1944789Sahrens 	int ret;
1945789Sahrens 
1946789Sahrens 	/*
1947789Sahrens 	 * If the pool is unavailable, just return success.
1948789Sahrens 	 */
19492082Seschrock 	if ((zfp = make_dataset_handle(zhp->zpool_hdl,
19502082Seschrock 	    zhp->zpool_name)) == NULL)
1951789Sahrens 		return (0);
1952789Sahrens 
19534657Sahrens 	ret = zfs_iter_filesystems(zfp, do_zvol_create, NULL);
1954789Sahrens 
1955789Sahrens 	zfs_close(zfp);
1956789Sahrens 	return (ret);
1957789Sahrens }
1958789Sahrens 
19593126Sahl static int
19603126Sahl do_zvol_remove(const char *dataset, void *data)
19613126Sahl {
19623126Sahl 	zpool_handle_t *zhp = data;
19633126Sahl 
19643126Sahl 	return (zvol_remove_link(zhp->zpool_hdl, dataset));
19653126Sahl }
19663126Sahl 
1967789Sahrens /*
19683126Sahl  * Iterate over all zvols in the pool and remove any minor nodes.  We iterate
19693126Sahl  * by examining the /dev links so that a corrupted pool doesn't impede this
19703126Sahl  * operation.
1971789Sahrens  */
1972789Sahrens int
1973789Sahrens zpool_remove_zvol_links(zpool_handle_t *zhp)
1974789Sahrens {
19753126Sahl 	return (zpool_iter_zvol(zhp, do_zvol_remove, zhp));
1976789Sahrens }
19771354Seschrock 
19781354Seschrock /*
19791354Seschrock  * Convert from a devid string to a path.
19801354Seschrock  */
19811354Seschrock static char *
19821354Seschrock devid_to_path(char *devid_str)
19831354Seschrock {
19841354Seschrock 	ddi_devid_t devid;
19851354Seschrock 	char *minor;
19861354Seschrock 	char *path;
19871354Seschrock 	devid_nmlist_t *list = NULL;
19881354Seschrock 	int ret;
19891354Seschrock 
19901354Seschrock 	if (devid_str_decode(devid_str, &devid, &minor) != 0)
19911354Seschrock 		return (NULL);
19921354Seschrock 
19931354Seschrock 	ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
19941354Seschrock 
19951354Seschrock 	devid_str_free(minor);
19961354Seschrock 	devid_free(devid);
19971354Seschrock 
19981354Seschrock 	if (ret != 0)
19991354Seschrock 		return (NULL);
20001354Seschrock 
20012082Seschrock 	if ((path = strdup(list[0].devname)) == NULL)
20022082Seschrock 		return (NULL);
20032082Seschrock 
20041354Seschrock 	devid_free_nmlist(list);
20051354Seschrock 
20061354Seschrock 	return (path);
20071354Seschrock }
20081354Seschrock 
20091354Seschrock /*
20101354Seschrock  * Convert from a path to a devid string.
20111354Seschrock  */
20121354Seschrock static char *
20131354Seschrock path_to_devid(const char *path)
20141354Seschrock {
20151354Seschrock 	int fd;
20161354Seschrock 	ddi_devid_t devid;
20171354Seschrock 	char *minor, *ret;
20181354Seschrock 
20191354Seschrock 	if ((fd = open(path, O_RDONLY)) < 0)
20201354Seschrock 		return (NULL);
20211354Seschrock 
20221354Seschrock 	minor = NULL;
20231354Seschrock 	ret = NULL;
20241354Seschrock 	if (devid_get(fd, &devid) == 0) {
20251354Seschrock 		if (devid_get_minor_name(fd, &minor) == 0)
20261354Seschrock 			ret = devid_str_encode(devid, minor);
20271354Seschrock 		if (minor != NULL)
20281354Seschrock 			devid_str_free(minor);
20291354Seschrock 		devid_free(devid);
20301354Seschrock 	}
20311354Seschrock 	(void) close(fd);
20321354Seschrock 
20331354Seschrock 	return (ret);
20341354Seschrock }
20351354Seschrock 
20361354Seschrock /*
20371354Seschrock  * Issue the necessary ioctl() to update the stored path value for the vdev.  We
20381354Seschrock  * ignore any failure here, since a common case is for an unprivileged user to
20391354Seschrock  * type 'zpool status', and we'll display the correct information anyway.
20401354Seschrock  */
20411354Seschrock static void
20421354Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
20431354Seschrock {
20441354Seschrock 	zfs_cmd_t zc = { 0 };
20451354Seschrock 
20461354Seschrock 	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
20472676Seschrock 	(void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
20481354Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
20491544Seschrock 	    &zc.zc_guid) == 0);
20501354Seschrock 
20512082Seschrock 	(void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
20521354Seschrock }
20531354Seschrock 
20541354Seschrock /*
20551354Seschrock  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
20561354Seschrock  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
20571354Seschrock  * We also check if this is a whole disk, in which case we strip off the
20581354Seschrock  * trailing 's0' slice name.
20591354Seschrock  *
20601354Seschrock  * This routine is also responsible for identifying when disks have been
20611354Seschrock  * reconfigured in a new location.  The kernel will have opened the device by
20621354Seschrock  * devid, but the path will still refer to the old location.  To catch this, we
20631354Seschrock  * first do a path -> devid translation (which is fast for the common case).  If
20641354Seschrock  * the devid matches, we're done.  If not, we do a reverse devid -> path
20651354Seschrock  * translation and issue the appropriate ioctl() to update the path of the vdev.
20661354Seschrock  * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
20671354Seschrock  * of these checks.
20681354Seschrock  */
20691354Seschrock char *
20702082Seschrock zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv)
20711354Seschrock {
20721354Seschrock 	char *path, *devid;
20731544Seschrock 	uint64_t value;
20741544Seschrock 	char buf[64];
20754451Seschrock 	vdev_stat_t *vs;
20764451Seschrock 	uint_t vsc;
20771354Seschrock 
20781544Seschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
20791544Seschrock 	    &value) == 0) {
20801544Seschrock 		verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
20811544Seschrock 		    &value) == 0);
20822856Snd150628 		(void) snprintf(buf, sizeof (buf), "%llu",
20832856Snd150628 		    (u_longlong_t)value);
20841544Seschrock 		path = buf;
20851544Seschrock 	} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
20861354Seschrock 
20874451Seschrock 		/*
20884451Seschrock 		 * If the device is dead (faulted, offline, etc) then don't
20894451Seschrock 		 * bother opening it.  Otherwise we may be forcing the user to
20904451Seschrock 		 * open a misbehaving device, which can have undesirable
20914451Seschrock 		 * effects.
20924451Seschrock 		 */
20934451Seschrock 		if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
20944451Seschrock 		    (uint64_t **)&vs, &vsc) != 0 ||
20954451Seschrock 		    vs->vs_state >= VDEV_STATE_DEGRADED) &&
20964451Seschrock 		    zhp != NULL &&
20971354Seschrock 		    nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
20981354Seschrock 			/*
20991354Seschrock 			 * Determine if the current path is correct.
21001354Seschrock 			 */
21011354Seschrock 			char *newdevid = path_to_devid(path);
21021354Seschrock 
21031354Seschrock 			if (newdevid == NULL ||
21041354Seschrock 			    strcmp(devid, newdevid) != 0) {
21051354Seschrock 				char *newpath;
21061354Seschrock 
21071354Seschrock 				if ((newpath = devid_to_path(devid)) != NULL) {
21081354Seschrock 					/*
21091354Seschrock 					 * Update the path appropriately.
21101354Seschrock 					 */
21111354Seschrock 					set_path(zhp, nv, newpath);
21122082Seschrock 					if (nvlist_add_string(nv,
21132082Seschrock 					    ZPOOL_CONFIG_PATH, newpath) == 0)
21142082Seschrock 						verify(nvlist_lookup_string(nv,
21152082Seschrock 						    ZPOOL_CONFIG_PATH,
21162082Seschrock 						    &path) == 0);
21171354Seschrock 					free(newpath);
21181354Seschrock 				}
21191354Seschrock 			}
21201354Seschrock 
21212082Seschrock 			if (newdevid)
21222082Seschrock 				devid_str_free(newdevid);
21231354Seschrock 		}
21241354Seschrock 
21251354Seschrock 		if (strncmp(path, "/dev/dsk/", 9) == 0)
21261354Seschrock 			path += 9;
21271354Seschrock 
21281354Seschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
21291544Seschrock 		    &value) == 0 && value) {
21302082Seschrock 			char *tmp = zfs_strdup(hdl, path);
21312082Seschrock 			if (tmp == NULL)
21322082Seschrock 				return (NULL);
21331354Seschrock 			tmp[strlen(path) - 2] = '\0';
21341354Seschrock 			return (tmp);
21351354Seschrock 		}
21361354Seschrock 	} else {
21371354Seschrock 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
21382082Seschrock 
21392082Seschrock 		/*
21402082Seschrock 		 * If it's a raidz device, we need to stick in the parity level.
21412082Seschrock 		 */
21422082Seschrock 		if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
21432082Seschrock 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
21442082Seschrock 			    &value) == 0);
21452082Seschrock 			(void) snprintf(buf, sizeof (buf), "%s%llu", path,
21462856Snd150628 			    (u_longlong_t)value);
21472082Seschrock 			path = buf;
21482082Seschrock 		}
21491354Seschrock 	}
21501354Seschrock 
21512082Seschrock 	return (zfs_strdup(hdl, path));
21521354Seschrock }
21531544Seschrock 
21541544Seschrock static int
21551544Seschrock zbookmark_compare(const void *a, const void *b)
21561544Seschrock {
21571544Seschrock 	return (memcmp(a, b, sizeof (zbookmark_t)));
21581544Seschrock }
21591544Seschrock 
21601544Seschrock /*
21611544Seschrock  * Retrieve the persistent error log, uniquify the members, and return to the
21621544Seschrock  * caller.
21631544Seschrock  */
21641544Seschrock int
21653444Sek110237 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
21661544Seschrock {
21671544Seschrock 	zfs_cmd_t zc = { 0 };
21681544Seschrock 	uint64_t count;
21692676Seschrock 	zbookmark_t *zb = NULL;
21703444Sek110237 	int i;
21711544Seschrock 
21721544Seschrock 	/*
21731544Seschrock 	 * Retrieve the raw error list from the kernel.  If the number of errors
21741544Seschrock 	 * has increased, allocate more space and continue until we get the
21751544Seschrock 	 * entire list.
21761544Seschrock 	 */
21771544Seschrock 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
21781544Seschrock 	    &count) == 0);
21794820Sek110237 	if (count == 0)
21804820Sek110237 		return (0);
21812676Seschrock 	if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
21822856Snd150628 	    count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
21832082Seschrock 		return (-1);
21842676Seschrock 	zc.zc_nvlist_dst_size = count;
21851544Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
21861544Seschrock 	for (;;) {
21872082Seschrock 		if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
21882082Seschrock 		    &zc) != 0) {
21892676Seschrock 			free((void *)(uintptr_t)zc.zc_nvlist_dst);
21901544Seschrock 			if (errno == ENOMEM) {
21913823Svb160487 				count = zc.zc_nvlist_dst_size;
21922676Seschrock 				if ((zc.zc_nvlist_dst = (uintptr_t)
21933823Svb160487 				    zfs_alloc(zhp->zpool_hdl, count *
21943823Svb160487 				    sizeof (zbookmark_t))) == (uintptr_t)NULL)
21952082Seschrock 					return (-1);
21961544Seschrock 			} else {
21971544Seschrock 				return (-1);
21981544Seschrock 			}
21991544Seschrock 		} else {
22001544Seschrock 			break;
22011544Seschrock 		}
22021544Seschrock 	}
22031544Seschrock 
22041544Seschrock 	/*
22051544Seschrock 	 * Sort the resulting bookmarks.  This is a little confusing due to the
22061544Seschrock 	 * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
22072676Seschrock 	 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
22081544Seschrock 	 * _not_ copied as part of the process.  So we point the start of our
22091544Seschrock 	 * array appropriate and decrement the total number of elements.
22101544Seschrock 	 */
22112676Seschrock 	zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
22122676Seschrock 	    zc.zc_nvlist_dst_size;
22132676Seschrock 	count -= zc.zc_nvlist_dst_size;
22141544Seschrock 
22151544Seschrock 	qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
22161544Seschrock 
22173444Sek110237 	verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
22181544Seschrock 
22191544Seschrock 	/*
22203444Sek110237 	 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
22211544Seschrock 	 */
22221544Seschrock 	for (i = 0; i < count; i++) {
22231544Seschrock 		nvlist_t *nv;
22241544Seschrock 
22253700Sek110237 		/* ignoring zb_blkid and zb_level for now */
22263700Sek110237 		if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
22273700Sek110237 		    zb[i-1].zb_object == zb[i].zb_object)
22281544Seschrock 			continue;
22291544Seschrock 
22303444Sek110237 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
22313444Sek110237 			goto nomem;
22323444Sek110237 		if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
22333444Sek110237 		    zb[i].zb_objset) != 0) {
22343444Sek110237 			nvlist_free(nv);
22352082Seschrock 			goto nomem;
22363444Sek110237 		}
22373444Sek110237 		if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
22383444Sek110237 		    zb[i].zb_object) != 0) {
22393444Sek110237 			nvlist_free(nv);
22403444Sek110237 			goto nomem;
22411544Seschrock 		}
22423444Sek110237 		if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
22433444Sek110237 			nvlist_free(nv);
22443444Sek110237 			goto nomem;
22453444Sek110237 		}
22463444Sek110237 		nvlist_free(nv);
22471544Seschrock 	}
22481544Seschrock 
22493265Sahrens 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
22501544Seschrock 	return (0);
22512082Seschrock 
22522082Seschrock nomem:
22532676Seschrock 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
22542082Seschrock 	return (no_memory(zhp->zpool_hdl));
22551544Seschrock }
22561760Seschrock 
22571760Seschrock /*
22581760Seschrock  * Upgrade a ZFS pool to the latest on-disk version.
22591760Seschrock  */
22601760Seschrock int
22615094Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
22621760Seschrock {
22631760Seschrock 	zfs_cmd_t zc = { 0 };
22642082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
22651760Seschrock 
22661760Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
22675094Slling 	zc.zc_cookie = new_version;
22685094Slling 
22694543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
22703237Slling 		return (zpool_standard_error_fmt(hdl, errno,
22712082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
22722082Seschrock 		    zhp->zpool_name));
22731760Seschrock 	return (0);
22741760Seschrock }
22752926Sek110237 
22764988Sek110237 void
22774988Sek110237 zpool_set_history_str(const char *subcommand, int argc, char **argv,
22784988Sek110237     char *history_str)
22794988Sek110237 {
22804988Sek110237 	int i;
22814988Sek110237 
22824988Sek110237 	(void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
22834988Sek110237 	for (i = 1; i < argc; i++) {
22844988Sek110237 		if (strlen(history_str) + 1 + strlen(argv[i]) >
22854988Sek110237 		    HIS_MAX_RECORD_LEN)
22864988Sek110237 			break;
22874988Sek110237 		(void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
22884988Sek110237 		(void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
22894988Sek110237 	}
22904988Sek110237 }
22914988Sek110237 
22922926Sek110237 /*
22934988Sek110237  * Stage command history for logging.
22942926Sek110237  */
22954988Sek110237 int
22964988Sek110237 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
22972926Sek110237 {
22984988Sek110237 	if (history_str == NULL)
22994988Sek110237 		return (EINVAL);
23004988Sek110237 
23014988Sek110237 	if (strlen(history_str) > HIS_MAX_RECORD_LEN)
23024988Sek110237 		return (EINVAL);
23032926Sek110237 
23044715Sek110237 	if (hdl->libzfs_log_str != NULL)
23054543Smarks 		free(hdl->libzfs_log_str);
23062926Sek110237 
23074988Sek110237 	if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
23084988Sek110237 		return (no_memory(hdl));
23094543Smarks 
23104988Sek110237 	return (0);
23112926Sek110237 }
23122926Sek110237 
23132926Sek110237 /*
23142926Sek110237  * Perform ioctl to get some command history of a pool.
23152926Sek110237  *
23162926Sek110237  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
23172926Sek110237  * logical offset of the history buffer to start reading from.
23182926Sek110237  *
23192926Sek110237  * Upon return, 'off' is the next logical offset to read from and
23202926Sek110237  * 'len' is the actual amount of bytes read into 'buf'.
23212926Sek110237  */
23222926Sek110237 static int
23232926Sek110237 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
23242926Sek110237 {
23252926Sek110237 	zfs_cmd_t zc = { 0 };
23262926Sek110237 	libzfs_handle_t *hdl = zhp->zpool_hdl;
23272926Sek110237 
23282926Sek110237 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
23292926Sek110237 
23302926Sek110237 	zc.zc_history = (uint64_t)(uintptr_t)buf;
23312926Sek110237 	zc.zc_history_len = *len;
23322926Sek110237 	zc.zc_history_offset = *off;
23332926Sek110237 
23342926Sek110237 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
23352926Sek110237 		switch (errno) {
23362926Sek110237 		case EPERM:
23373237Slling 			return (zfs_error_fmt(hdl, EZFS_PERM,
23383237Slling 			    dgettext(TEXT_DOMAIN,
23392926Sek110237 			    "cannot show history for pool '%s'"),
23402926Sek110237 			    zhp->zpool_name));
23412926Sek110237 		case ENOENT:
23423237Slling 			return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
23432926Sek110237 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
23442926Sek110237 			    "'%s'"), zhp->zpool_name));
23453863Sek110237 		case ENOTSUP:
23463863Sek110237 			return (zfs_error_fmt(hdl, EZFS_BADVERSION,
23473863Sek110237 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
23483863Sek110237 			    "'%s', pool must be upgraded"), zhp->zpool_name));
23492926Sek110237 		default:
23503237Slling 			return (zpool_standard_error_fmt(hdl, errno,
23512926Sek110237 			    dgettext(TEXT_DOMAIN,
23522926Sek110237 			    "cannot get history for '%s'"), zhp->zpool_name));
23532926Sek110237 		}
23542926Sek110237 	}
23552926Sek110237 
23562926Sek110237 	*len = zc.zc_history_len;
23572926Sek110237 	*off = zc.zc_history_offset;
23582926Sek110237 
23592926Sek110237 	return (0);
23602926Sek110237 }
23612926Sek110237 
23622926Sek110237 /*
23632926Sek110237  * Process the buffer of nvlists, unpacking and storing each nvlist record
23642926Sek110237  * into 'records'.  'leftover' is set to the number of bytes that weren't
23652926Sek110237  * processed as there wasn't a complete record.
23662926Sek110237  */
23672926Sek110237 static int
23682926Sek110237 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
23692926Sek110237     nvlist_t ***records, uint_t *numrecords)
23702926Sek110237 {
23712926Sek110237 	uint64_t reclen;
23722926Sek110237 	nvlist_t *nv;
23732926Sek110237 	int i;
23742926Sek110237 
23752926Sek110237 	while (bytes_read > sizeof (reclen)) {
23762926Sek110237 
23772926Sek110237 		/* get length of packed record (stored as little endian) */
23782926Sek110237 		for (i = 0, reclen = 0; i < sizeof (reclen); i++)
23792926Sek110237 			reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
23802926Sek110237 
23812926Sek110237 		if (bytes_read < sizeof (reclen) + reclen)
23822926Sek110237 			break;
23832926Sek110237 
23842926Sek110237 		/* unpack record */
23852926Sek110237 		if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
23862926Sek110237 			return (ENOMEM);
23872926Sek110237 		bytes_read -= sizeof (reclen) + reclen;
23882926Sek110237 		buf += sizeof (reclen) + reclen;
23892926Sek110237 
23902926Sek110237 		/* add record to nvlist array */
23912926Sek110237 		(*numrecords)++;
23922926Sek110237 		if (ISP2(*numrecords + 1)) {
23932926Sek110237 			*records = realloc(*records,
23942926Sek110237 			    *numrecords * 2 * sizeof (nvlist_t *));
23952926Sek110237 		}
23962926Sek110237 		(*records)[*numrecords - 1] = nv;
23972926Sek110237 	}
23982926Sek110237 
23992926Sek110237 	*leftover = bytes_read;
24002926Sek110237 	return (0);
24012926Sek110237 }
24022926Sek110237 
24032926Sek110237 #define	HIS_BUF_LEN	(128*1024)
24042926Sek110237 
24052926Sek110237 /*
24062926Sek110237  * Retrieve the command history of a pool.
24072926Sek110237  */
24082926Sek110237 int
24092926Sek110237 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
24102926Sek110237 {
24112926Sek110237 	char buf[HIS_BUF_LEN];
24122926Sek110237 	uint64_t off = 0;
24132926Sek110237 	nvlist_t **records = NULL;
24142926Sek110237 	uint_t numrecords = 0;
24152926Sek110237 	int err, i;
24162926Sek110237 
24172926Sek110237 	do {
24182926Sek110237 		uint64_t bytes_read = sizeof (buf);
24192926Sek110237 		uint64_t leftover;
24202926Sek110237 
24212926Sek110237 		if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
24222926Sek110237 			break;
24232926Sek110237 
24242926Sek110237 		/* if nothing else was read in, we're at EOF, just return */
24252926Sek110237 		if (!bytes_read)
24262926Sek110237 			break;
24272926Sek110237 
24282926Sek110237 		if ((err = zpool_history_unpack(buf, bytes_read,
24292926Sek110237 		    &leftover, &records, &numrecords)) != 0)
24302926Sek110237 			break;
24312926Sek110237 		off -= leftover;
24322926Sek110237 
24332926Sek110237 		/* CONSTCOND */
24342926Sek110237 	} while (1);
24352926Sek110237 
24362926Sek110237 	if (!err) {
24372926Sek110237 		verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
24382926Sek110237 		verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
24392926Sek110237 		    records, numrecords) == 0);
24402926Sek110237 	}
24412926Sek110237 	for (i = 0; i < numrecords; i++)
24422926Sek110237 		nvlist_free(records[i]);
24432926Sek110237 	free(records);
24442926Sek110237 
24452926Sek110237 	return (err);
24462926Sek110237 }
24473444Sek110237 
24483444Sek110237 void
24493444Sek110237 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
24503444Sek110237     char *pathname, size_t len)
24513444Sek110237 {
24523444Sek110237 	zfs_cmd_t zc = { 0 };
24533444Sek110237 	boolean_t mounted = B_FALSE;
24543444Sek110237 	char *mntpnt = NULL;
24553444Sek110237 	char dsname[MAXNAMELEN];
24563444Sek110237 
24573444Sek110237 	if (dsobj == 0) {
24583444Sek110237 		/* special case for the MOS */
24593444Sek110237 		(void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
24603444Sek110237 		return;
24613444Sek110237 	}
24623444Sek110237 
24633444Sek110237 	/* get the dataset's name */
24643444Sek110237 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
24653444Sek110237 	zc.zc_obj = dsobj;
24663444Sek110237 	if (ioctl(zhp->zpool_hdl->libzfs_fd,
24673444Sek110237 	    ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
24683444Sek110237 		/* just write out a path of two object numbers */
24693444Sek110237 		(void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
24703444Sek110237 		    dsobj, obj);
24713444Sek110237 		return;
24723444Sek110237 	}
24733444Sek110237 	(void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
24743444Sek110237 
24753444Sek110237 	/* find out if the dataset is mounted */
24763444Sek110237 	mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
24773444Sek110237 
24783444Sek110237 	/* get the corrupted object's path */
24793444Sek110237 	(void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
24803444Sek110237 	zc.zc_obj = obj;
24813444Sek110237 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
24823444Sek110237 	    &zc) == 0) {
24833444Sek110237 		if (mounted) {
24843444Sek110237 			(void) snprintf(pathname, len, "%s%s", mntpnt,
24853444Sek110237 			    zc.zc_value);
24863444Sek110237 		} else {
24873444Sek110237 			(void) snprintf(pathname, len, "%s:%s",
24883444Sek110237 			    dsname, zc.zc_value);
24893444Sek110237 		}
24903444Sek110237 	} else {
24913444Sek110237 		(void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
24923444Sek110237 	}
24933444Sek110237 	free(mntpnt);
24943444Sek110237 }
24953912Slling 
24964276Staylor #define	RDISK_ROOT	"/dev/rdsk"
24974276Staylor #define	BACKUP_SLICE	"s2"
24984276Staylor /*
24994276Staylor  * Don't start the slice at the default block of 34; many storage
25004276Staylor  * devices will use a stripe width of 128k, so start there instead.
25014276Staylor  */
25024276Staylor #define	NEW_START_BLOCK	256
25034276Staylor 
25044276Staylor /*
25054276Staylor  * determine where a partition starts on a disk in the current
25064276Staylor  * configuration
25074276Staylor  */
25084276Staylor static diskaddr_t
25094276Staylor find_start_block(nvlist_t *config)
25104276Staylor {
25114276Staylor 	nvlist_t **child;
25124276Staylor 	uint_t c, children;
25134276Staylor 	char *path;
25144276Staylor 	diskaddr_t sb = MAXOFFSET_T;
25154276Staylor 	int fd;
25164276Staylor 	char diskname[MAXPATHLEN];
25174276Staylor 	uint64_t wholedisk;
25184276Staylor 
25194276Staylor 	if (nvlist_lookup_nvlist_array(config,
25204276Staylor 	    ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
25214276Staylor 		if (nvlist_lookup_uint64(config,
25224276Staylor 		    ZPOOL_CONFIG_WHOLE_DISK,
25234276Staylor 		    &wholedisk) != 0 || !wholedisk) {
25244276Staylor 			return (MAXOFFSET_T);
25254276Staylor 		}
25264276Staylor 		if (nvlist_lookup_string(config,
25274276Staylor 		    ZPOOL_CONFIG_PATH, &path) != 0) {
25284276Staylor 			return (MAXOFFSET_T);
25294276Staylor 		}
25304276Staylor 
25314276Staylor 		(void) snprintf(diskname, sizeof (diskname), "%s%s",
25324276Staylor 		    RDISK_ROOT, strrchr(path, '/'));
25334276Staylor 		if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
25344276Staylor 			struct dk_gpt *vtoc;
25354276Staylor 			if (efi_alloc_and_read(fd, &vtoc) >= 0) {
25364276Staylor 				sb = vtoc->efi_parts[0].p_start;
25374276Staylor 				efi_free(vtoc);
25384276Staylor 			}
25394276Staylor 			(void) close(fd);
25404276Staylor 		}
25414276Staylor 		return (sb);
25424276Staylor 	}
25434276Staylor 
25444276Staylor 	for (c = 0; c < children; c++) {
25454276Staylor 		sb = find_start_block(child[c]);
25464276Staylor 		if (sb != MAXOFFSET_T) {
25474276Staylor 			return (sb);
25484276Staylor 		}
25494276Staylor 	}
25504276Staylor 	return (MAXOFFSET_T);
25514276Staylor }
25524276Staylor 
25534276Staylor /*
25544276Staylor  * Label an individual disk.  The name provided is the short name,
25554276Staylor  * stripped of any leading /dev path.
25564276Staylor  */
25574276Staylor int
25584276Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
25594276Staylor {
25604276Staylor 	char path[MAXPATHLEN];
25614276Staylor 	struct dk_gpt *vtoc;
25624276Staylor 	int fd;
25634276Staylor 	size_t resv = EFI_MIN_RESV_SIZE;
25644276Staylor 	uint64_t slice_size;
25654276Staylor 	diskaddr_t start_block;
25664276Staylor 	char errbuf[1024];
25674276Staylor 
25686289Smmusante 	/* prepare an error message just in case */
25696289Smmusante 	(void) snprintf(errbuf, sizeof (errbuf),
25706289Smmusante 	    dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
25716289Smmusante 
25724276Staylor 	if (zhp) {
25734276Staylor 		nvlist_t *nvroot;
25744276Staylor 
25754276Staylor 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
25764276Staylor 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
25774276Staylor 
25784276Staylor 		if (zhp->zpool_start_block == 0)
25794276Staylor 			start_block = find_start_block(nvroot);
25804276Staylor 		else
25814276Staylor 			start_block = zhp->zpool_start_block;
25824276Staylor 		zhp->zpool_start_block = start_block;
25834276Staylor 	} else {
25844276Staylor 		/* new pool */
25854276Staylor 		start_block = NEW_START_BLOCK;
25864276Staylor 	}
25874276Staylor 
25884276Staylor 	(void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
25894276Staylor 	    BACKUP_SLICE);
25904276Staylor 
25914276Staylor 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
25924276Staylor 		/*
25934276Staylor 		 * This shouldn't happen.  We've long since verified that this
25944276Staylor 		 * is a valid device.
25954276Staylor 		 */
25966289Smmusante 		zfs_error_aux(hdl,
25976289Smmusante 		    dgettext(TEXT_DOMAIN, "unable to open device"));
25984276Staylor 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
25994276Staylor 	}
26004276Staylor 
26014276Staylor 	if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
26024276Staylor 		/*
26034276Staylor 		 * The only way this can fail is if we run out of memory, or we
26044276Staylor 		 * were unable to read the disk's capacity
26054276Staylor 		 */
26064276Staylor 		if (errno == ENOMEM)
26074276Staylor 			(void) no_memory(hdl);
26084276Staylor 
26094276Staylor 		(void) close(fd);
26106289Smmusante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26116289Smmusante 		    "unable to read disk capacity"), name);
26124276Staylor 
26134276Staylor 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
26144276Staylor 	}
26154276Staylor 
26164276Staylor 	slice_size = vtoc->efi_last_u_lba + 1;
26174276Staylor 	slice_size -= EFI_MIN_RESV_SIZE;
26184276Staylor 	if (start_block == MAXOFFSET_T)
26194276Staylor 		start_block = NEW_START_BLOCK;
26204276Staylor 	slice_size -= start_block;
26214276Staylor 
26224276Staylor 	vtoc->efi_parts[0].p_start = start_block;
26234276Staylor 	vtoc->efi_parts[0].p_size = slice_size;
26244276Staylor 
26254276Staylor 	/*
26264276Staylor 	 * Why we use V_USR: V_BACKUP confuses users, and is considered
26274276Staylor 	 * disposable by some EFI utilities (since EFI doesn't have a backup
26284276Staylor 	 * slice).  V_UNASSIGNED is supposed to be used only for zero size
26294276Staylor 	 * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
26304276Staylor 	 * etc. were all pretty specific.  V_USR is as close to reality as we
26314276Staylor 	 * can get, in the absence of V_OTHER.
26324276Staylor 	 */
26334276Staylor 	vtoc->efi_parts[0].p_tag = V_USR;
26344276Staylor 	(void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
26354276Staylor 
26364276Staylor 	vtoc->efi_parts[8].p_start = slice_size + start_block;
26374276Staylor 	vtoc->efi_parts[8].p_size = resv;
26384276Staylor 	vtoc->efi_parts[8].p_tag = V_RESERVED;
26394276Staylor 
26404276Staylor 	if (efi_write(fd, vtoc) != 0) {
26414276Staylor 		/*
26424276Staylor 		 * Some block drivers (like pcata) may not support EFI
26434276Staylor 		 * GPT labels.  Print out a helpful error message dir-
26444276Staylor 		 * ecting the user to manually label the disk and give
26454276Staylor 		 * a specific slice.
26464276Staylor 		 */
26474276Staylor 		(void) close(fd);
26484276Staylor 		efi_free(vtoc);
26494276Staylor 
26504276Staylor 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26516289Smmusante 		    "try using fdisk(1M) and then provide a specific slice"));
26524276Staylor 		return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
26534276Staylor 	}
26544276Staylor 
26554276Staylor 	(void) close(fd);
26564276Staylor 	efi_free(vtoc);
26574276Staylor 	return (0);
26584276Staylor }
26596423Sgw25295 
26606423Sgw25295 static boolean_t
26616423Sgw25295 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
26626423Sgw25295 {
26636423Sgw25295 	char *type;
26646423Sgw25295 	nvlist_t **child;
26656423Sgw25295 	uint_t children, c;
26666423Sgw25295 
26676423Sgw25295 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
26686423Sgw25295 	if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
26696423Sgw25295 	    strcmp(type, VDEV_TYPE_FILE) == 0 ||
26706423Sgw25295 	    strcmp(type, VDEV_TYPE_LOG) == 0 ||
26716423Sgw25295 	    strcmp(type, VDEV_TYPE_MISSING) == 0) {
26726423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26736423Sgw25295 		    "vdev type '%s' is not supported"), type);
26746423Sgw25295 		(void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
26756423Sgw25295 		return (B_FALSE);
26766423Sgw25295 	}
26776423Sgw25295 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
26786423Sgw25295 	    &child, &children) == 0) {
26796423Sgw25295 		for (c = 0; c < children; c++) {
26806423Sgw25295 			if (!supported_dump_vdev_type(hdl, child[c], errbuf))
26816423Sgw25295 				return (B_FALSE);
26826423Sgw25295 		}
26836423Sgw25295 	}
26846423Sgw25295 	return (B_TRUE);
26856423Sgw25295 }
26866423Sgw25295 
26876423Sgw25295 /*
26886423Sgw25295  * check if this zvol is allowable for use as a dump device; zero if
26896423Sgw25295  * it is, > 0 if it isn't, < 0 if it isn't a zvol
26906423Sgw25295  */
26916423Sgw25295 int
26926423Sgw25295 zvol_check_dump_config(char *arg)
26936423Sgw25295 {
26946423Sgw25295 	zpool_handle_t *zhp = NULL;
26956423Sgw25295 	nvlist_t *config, *nvroot;
26966423Sgw25295 	char *p, *volname;
26976423Sgw25295 	nvlist_t **top;
26986423Sgw25295 	uint_t toplevels;
26996423Sgw25295 	libzfs_handle_t *hdl;
27006423Sgw25295 	char errbuf[1024];
27016423Sgw25295 	char poolname[ZPOOL_MAXNAMELEN];
27026423Sgw25295 	int pathlen = strlen(ZVOL_FULL_DEV_DIR);
27036423Sgw25295 	int ret = 1;
27046423Sgw25295 
27056423Sgw25295 	if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
27066423Sgw25295 		return (-1);
27076423Sgw25295 	}
27086423Sgw25295 
27096423Sgw25295 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
27106423Sgw25295 	    "dump is not supported on device '%s'"), arg);
27116423Sgw25295 
27126423Sgw25295 	if ((hdl = libzfs_init()) == NULL)
27136423Sgw25295 		return (1);
27146423Sgw25295 	libzfs_print_on_error(hdl, B_TRUE);
27156423Sgw25295 
27166423Sgw25295 	volname = arg + pathlen;
27176423Sgw25295 
27186423Sgw25295 	/* check the configuration of the pool */
27196423Sgw25295 	if ((p = strchr(volname, '/')) == NULL) {
27206423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27216423Sgw25295 		    "malformed dataset name"));
27226423Sgw25295 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
27236423Sgw25295 		return (1);
27246423Sgw25295 	} else if (p - volname >= ZFS_MAXNAMELEN) {
27256423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27266423Sgw25295 		    "dataset name is too long"));
27276423Sgw25295 		(void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
27286423Sgw25295 		return (1);
27296423Sgw25295 	} else {
27306423Sgw25295 		(void) strncpy(poolname, volname, p - volname);
27316423Sgw25295 		poolname[p - volname] = '\0';
27326423Sgw25295 	}
27336423Sgw25295 
27346423Sgw25295 	if ((zhp = zpool_open(hdl, poolname)) == NULL) {
27356423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27366423Sgw25295 		    "could not open pool '%s'"), poolname);
27376423Sgw25295 		(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
27386423Sgw25295 		goto out;
27396423Sgw25295 	}
27406423Sgw25295 	config = zpool_get_config(zhp, NULL);
27416423Sgw25295 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
27426423Sgw25295 	    &nvroot) != 0) {
27436423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27446423Sgw25295 		    "could not obtain vdev configuration for  '%s'"), poolname);
27456423Sgw25295 		(void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
27466423Sgw25295 		goto out;
27476423Sgw25295 	}
27486423Sgw25295 
27496423Sgw25295 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
27506423Sgw25295 	    &top, &toplevels) == 0);
27516423Sgw25295 	if (toplevels != 1) {
27526423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27536423Sgw25295 		    "'%s' has multiple top level vdevs"), poolname);
27546423Sgw25295 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
27556423Sgw25295 		goto out;
27566423Sgw25295 	}
27576423Sgw25295 
27586423Sgw25295 	if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
27596423Sgw25295 		goto out;
27606423Sgw25295 	}
27616423Sgw25295 	ret = 0;
27626423Sgw25295 
27636423Sgw25295 out:
27646423Sgw25295 	if (zhp)
27656423Sgw25295 		zpool_close(zhp);
27666423Sgw25295 	libzfs_fini(hdl);
27676423Sgw25295 	return (ret);
27686423Sgw25295 }
2769