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 
1053*6643Seschrock 	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,
1067*6643Seschrock     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 
1116*6643Seschrock 	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;
15052082Seschrock 	char *path;
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 
15412082Seschrock 	/*
15422082Seschrock 	 * If the target is a hot spare that has been swapped in, we can only
15432082Seschrock 	 * replace it with another hot spare.
15442082Seschrock 	 */
15452082Seschrock 	if (replacing &&
15462082Seschrock 	    nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
15472082Seschrock 	    nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 &&
15485450Sbrendan 	    (zpool_find_vdev(zhp, path, &avail_spare, &l2cache) == NULL ||
15492468Sek110237 	    !avail_spare) && is_replacing_spare(config_root, tgt, 1)) {
15502082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15512082Seschrock 		    "can only be replaced by another hot spare"));
15522082Seschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
15532082Seschrock 	}
15542082Seschrock 
15552082Seschrock 	/*
15562082Seschrock 	 * If we are attempting to replace a spare, it canot be applied to an
15572082Seschrock 	 * already spared device.
15582082Seschrock 	 */
15592082Seschrock 	if (replacing &&
15602082Seschrock 	    nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 &&
15615450Sbrendan 	    zpool_find_vdev(zhp, path, &avail_spare, &l2cache) != NULL &&
15625450Sbrendan 	    avail_spare && is_replacing_spare(config_root, tgt, 0)) {
15632082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15642082Seschrock 		    "device has already been replaced with a spare"));
15652082Seschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
15662082Seschrock 	}
1567789Sahrens 
15685094Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
15692082Seschrock 		return (-1);
1570789Sahrens 
15714543Smarks 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc);
1572789Sahrens 
15732676Seschrock 	zcmd_free_nvlists(&zc);
1574789Sahrens 
1575789Sahrens 	if (ret == 0)
1576789Sahrens 		return (0);
1577789Sahrens 
1578789Sahrens 	switch (errno) {
15791544Seschrock 	case ENOTSUP:
1580789Sahrens 		/*
1581789Sahrens 		 * Can't attach to or replace this type of vdev.
1582789Sahrens 		 */
15834527Sperrin 		if (replacing) {
15844527Sperrin 			is_log = B_FALSE;
15854527Sperrin 			(void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_LOG,
15864527Sperrin 			    &is_log);
15874527Sperrin 			if (is_log)
15884527Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15894527Sperrin 				    "cannot replace a log with a spare"));
15904527Sperrin 			else
15914527Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15924527Sperrin 				    "cannot replace a replacing device"));
15934527Sperrin 		} else {
15942082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15952082Seschrock 			    "can only attach to mirrors and top-level "
15962082Seschrock 			    "disks"));
15974527Sperrin 		}
15982082Seschrock 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
1599789Sahrens 		break;
1600789Sahrens 
16011544Seschrock 	case EINVAL:
1602789Sahrens 		/*
1603789Sahrens 		 * The new device must be a single disk.
1604789Sahrens 		 */
16052082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16062082Seschrock 		    "new device must be a single disk"));
16072082Seschrock 		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
1608789Sahrens 		break;
1609789Sahrens 
16101544Seschrock 	case EBUSY:
16112082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
16122082Seschrock 		    new_disk);
16132082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1614789Sahrens 		break;
1615789Sahrens 
16161544Seschrock 	case EOVERFLOW:
1617789Sahrens 		/*
1618789Sahrens 		 * The new device is too small.
1619789Sahrens 		 */
16202082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16212082Seschrock 		    "device is too small"));
16222082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1623789Sahrens 		break;
1624789Sahrens 
16251544Seschrock 	case EDOM:
1626789Sahrens 		/*
1627789Sahrens 		 * The new device has a different alignment requirement.
1628789Sahrens 		 */
16292082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16302082Seschrock 		    "devices have different sector alignment"));
16312082Seschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1632789Sahrens 		break;
1633789Sahrens 
16341544Seschrock 	case ENAMETOOLONG:
1635789Sahrens 		/*
1636789Sahrens 		 * The resulting top-level vdev spec won't fit in the label.
1637789Sahrens 		 */
16382082Seschrock 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
1639789Sahrens 		break;
1640789Sahrens 
16411544Seschrock 	default:
16422082Seschrock 		(void) zpool_standard_error(hdl, errno, msg);
1643789Sahrens 	}
1644789Sahrens 
16452082Seschrock 	return (-1);
1646789Sahrens }
1647789Sahrens 
1648789Sahrens /*
1649789Sahrens  * Detach the specified device.
1650789Sahrens  */
1651789Sahrens int
1652789Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
1653789Sahrens {
1654789Sahrens 	zfs_cmd_t zc = { 0 };
1655789Sahrens 	char msg[1024];
16562082Seschrock 	nvlist_t *tgt;
16575450Sbrendan 	boolean_t avail_spare, l2cache;
16582082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1659789Sahrens 
16601544Seschrock 	(void) snprintf(msg, sizeof (msg),
16611544Seschrock 	    dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
16621544Seschrock 
1663789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
16645450Sbrendan 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == 0)
16652082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
1666789Sahrens 
16672468Sek110237 	if (avail_spare)
16682082Seschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
16692082Seschrock 
16705450Sbrendan 	if (l2cache)
16715450Sbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
16725450Sbrendan 
16732082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
16742082Seschrock 
16754543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
1676789Sahrens 		return (0);
1677789Sahrens 
1678789Sahrens 	switch (errno) {
1679789Sahrens 
16801544Seschrock 	case ENOTSUP:
1681789Sahrens 		/*
1682789Sahrens 		 * Can't detach from this type of vdev.
1683789Sahrens 		 */
16842082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
16852082Seschrock 		    "applicable to mirror and replacing vdevs"));
16862082Seschrock 		(void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg);
1687789Sahrens 		break;
1688789Sahrens 
16891544Seschrock 	case EBUSY:
1690789Sahrens 		/*
1691789Sahrens 		 * There are no other replicas of this device.
1692789Sahrens 		 */
16932082Seschrock 		(void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
1694789Sahrens 		break;
1695789Sahrens 
16961544Seschrock 	default:
16972082Seschrock 		(void) zpool_standard_error(hdl, errno, msg);
16981544Seschrock 	}
16991544Seschrock 
17002082Seschrock 	return (-1);
17012082Seschrock }
17022082Seschrock 
17032082Seschrock /*
17045450Sbrendan  * Remove the given device.  Currently, this is supported only for hot spares
17055450Sbrendan  * and level 2 cache devices.
17062082Seschrock  */
17072082Seschrock int
17082082Seschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
17092082Seschrock {
17102082Seschrock 	zfs_cmd_t zc = { 0 };
17112082Seschrock 	char msg[1024];
17122082Seschrock 	nvlist_t *tgt;
17135450Sbrendan 	boolean_t avail_spare, l2cache;
17142082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
17152082Seschrock 
17162082Seschrock 	(void) snprintf(msg, sizeof (msg),
17172082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
17182082Seschrock 
17192082Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
17205450Sbrendan 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == 0)
17212082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
17222082Seschrock 
17235450Sbrendan 	if (!avail_spare && !l2cache) {
17242082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17255450Sbrendan 		    "only inactive hot spares or cache devices "
17265450Sbrendan 		    "can be removed"));
17272082Seschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
17282082Seschrock 	}
17292082Seschrock 
17302082Seschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
17312082Seschrock 
17324543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
17332082Seschrock 		return (0);
17342082Seschrock 
17352082Seschrock 	return (zpool_standard_error(hdl, errno, msg));
17361544Seschrock }
17371544Seschrock 
17381544Seschrock /*
17391544Seschrock  * Clear the errors for the pool, or the particular device if specified.
17401544Seschrock  */
17411544Seschrock int
17421544Seschrock zpool_clear(zpool_handle_t *zhp, const char *path)
17431544Seschrock {
17441544Seschrock 	zfs_cmd_t zc = { 0 };
17451544Seschrock 	char msg[1024];
17462082Seschrock 	nvlist_t *tgt;
17475450Sbrendan 	boolean_t avail_spare, l2cache;
17482082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
17491544Seschrock 
17501544Seschrock 	if (path)
17511544Seschrock 		(void) snprintf(msg, sizeof (msg),
17521544Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
17532676Seschrock 		    path);
17541544Seschrock 	else
17551544Seschrock 		(void) snprintf(msg, sizeof (msg),
17561544Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
17571544Seschrock 		    zhp->zpool_name);
17581544Seschrock 
17591544Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
17602082Seschrock 	if (path) {
17615450Sbrendan 		if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
17625450Sbrendan 		    &l2cache)) == 0)
17632082Seschrock 			return (zfs_error(hdl, EZFS_NODEVICE, msg));
17642082Seschrock 
17655450Sbrendan 		/*
17665450Sbrendan 		 * Don't allow error clearing for hot spares.  Do allow
17675450Sbrendan 		 * error clearing for l2cache devices.
17685450Sbrendan 		 */
17692468Sek110237 		if (avail_spare)
17702082Seschrock 			return (zfs_error(hdl, EZFS_ISSPARE, msg));
17712082Seschrock 
17722082Seschrock 		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
17732082Seschrock 		    &zc.zc_guid) == 0);
17741544Seschrock 	}
17751544Seschrock 
17764543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0)
17771544Seschrock 		return (0);
17781544Seschrock 
17792082Seschrock 	return (zpool_standard_error(hdl, errno, msg));
1780789Sahrens }
1781789Sahrens 
17823126Sahl /*
17834451Seschrock  * Similar to zpool_clear(), but takes a GUID (used by fmd).
17844451Seschrock  */
17854451Seschrock int
17864451Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
17874451Seschrock {
17884451Seschrock 	zfs_cmd_t zc = { 0 };
17894451Seschrock 	char msg[1024];
17904451Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
17914451Seschrock 
17924451Seschrock 	(void) snprintf(msg, sizeof (msg),
17934451Seschrock 	    dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
17944451Seschrock 	    guid);
17954451Seschrock 
17964451Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
17974451Seschrock 	zc.zc_guid = guid;
17984451Seschrock 
17994451Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
18004451Seschrock 		return (0);
18014451Seschrock 
18024451Seschrock 	return (zpool_standard_error(hdl, errno, msg));
18034451Seschrock }
18044451Seschrock 
18054451Seschrock /*
18063126Sahl  * Iterate over all zvols in a given pool by walking the /dev/zvol/dsk/<pool>
18073126Sahl  * hierarchy.
18083126Sahl  */
18093126Sahl int
18103126Sahl zpool_iter_zvol(zpool_handle_t *zhp, int (*cb)(const char *, void *),
18113126Sahl     void *data)
1812789Sahrens {
18133126Sahl 	libzfs_handle_t *hdl = zhp->zpool_hdl;
18143126Sahl 	char (*paths)[MAXPATHLEN];
18153126Sahl 	size_t size = 4;
18163126Sahl 	int curr, fd, base, ret = 0;
18173126Sahl 	DIR *dirp;
18183126Sahl 	struct dirent *dp;
18193126Sahl 	struct stat st;
18203126Sahl 
18213126Sahl 	if ((base = open("/dev/zvol/dsk", O_RDONLY)) < 0)
18223126Sahl 		return (errno == ENOENT ? 0 : -1);
18233126Sahl 
18243126Sahl 	if (fstatat(base, zhp->zpool_name, &st, 0) != 0) {
18253126Sahl 		int err = errno;
18263126Sahl 		(void) close(base);
18273126Sahl 		return (err == ENOENT ? 0 : -1);
18283126Sahl 	}
1829789Sahrens 
1830789Sahrens 	/*
18313126Sahl 	 * Oddly this wasn't a directory -- ignore that failure since we
18323126Sahl 	 * know there are no links lower in the (non-existant) hierarchy.
1833789Sahrens 	 */
18343126Sahl 	if (!S_ISDIR(st.st_mode)) {
18353126Sahl 		(void) close(base);
18363126Sahl 		return (0);
18373126Sahl 	}
18383126Sahl 
18393126Sahl 	if ((paths = zfs_alloc(hdl, size * sizeof (paths[0]))) == NULL) {
18403126Sahl 		(void) close(base);
18413126Sahl 		return (-1);
1842789Sahrens 	}
1843789Sahrens 
18443126Sahl 	(void) strlcpy(paths[0], zhp->zpool_name, sizeof (paths[0]));
18453126Sahl 	curr = 0;
18463126Sahl 
18473126Sahl 	while (curr >= 0) {
18483126Sahl 		if (fstatat(base, paths[curr], &st, AT_SYMLINK_NOFOLLOW) != 0)
18493126Sahl 			goto err;
18503126Sahl 
18513126Sahl 		if (S_ISDIR(st.st_mode)) {
18523126Sahl 			if ((fd = openat(base, paths[curr], O_RDONLY)) < 0)
18533126Sahl 				goto err;
18543126Sahl 
18553126Sahl 			if ((dirp = fdopendir(fd)) == NULL) {
18563126Sahl 				(void) close(fd);
18573126Sahl 				goto err;
18583126Sahl 			}
18593126Sahl 
18603126Sahl 			while ((dp = readdir(dirp)) != NULL) {
18613126Sahl 				if (dp->d_name[0] == '.')
18623126Sahl 					continue;
18633126Sahl 
18643126Sahl 				if (curr + 1 == size) {
18653126Sahl 					paths = zfs_realloc(hdl, paths,
18663126Sahl 					    size * sizeof (paths[0]),
18673126Sahl 					    size * 2 * sizeof (paths[0]));
18683126Sahl 					if (paths == NULL) {
18693126Sahl 						(void) closedir(dirp);
18703126Sahl 						(void) close(fd);
18713126Sahl 						goto err;
18723126Sahl 					}
18733126Sahl 
18743126Sahl 					size *= 2;
18753126Sahl 				}
18763126Sahl 
18773126Sahl 				(void) strlcpy(paths[curr + 1], paths[curr],
18783126Sahl 				    sizeof (paths[curr + 1]));
18793126Sahl 				(void) strlcat(paths[curr], "/",
18803126Sahl 				    sizeof (paths[curr]));
18813126Sahl 				(void) strlcat(paths[curr], dp->d_name,
18823126Sahl 				    sizeof (paths[curr]));
18833126Sahl 				curr++;
18843126Sahl 			}
18853126Sahl 
18863126Sahl 			(void) closedir(dirp);
18873126Sahl 
18883126Sahl 		} else {
18893126Sahl 			if ((ret = cb(paths[curr], data)) != 0)
18903126Sahl 				break;
18913126Sahl 		}
18923126Sahl 
18933126Sahl 		curr--;
18943126Sahl 	}
18953126Sahl 
18963126Sahl 	free(paths);
18973126Sahl 	(void) close(base);
18983126Sahl 
18993126Sahl 	return (ret);
19003126Sahl 
19013126Sahl err:
19023126Sahl 	free(paths);
19033126Sahl 	(void) close(base);
19043126Sahl 	return (-1);
19053126Sahl }
19063126Sahl 
19073126Sahl typedef struct zvol_cb {
19083126Sahl 	zpool_handle_t *zcb_pool;
19093126Sahl 	boolean_t zcb_create;
19103126Sahl } zvol_cb_t;
19113126Sahl 
19123126Sahl /*ARGSUSED*/
19133126Sahl static int
19143126Sahl do_zvol_create(zfs_handle_t *zhp, void *data)
19153126Sahl {
19164657Sahrens 	int ret = 0;
19173126Sahl 
19184657Sahrens 	if (ZFS_IS_VOLUME(zhp)) {
19193126Sahl 		(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
19204657Sahrens 		ret = zfs_iter_snapshots(zhp, do_zvol_create, NULL);
19214657Sahrens 	}
19223126Sahl 
19234657Sahrens 	if (ret == 0)
19244657Sahrens 		ret = zfs_iter_filesystems(zhp, do_zvol_create, NULL);
1925789Sahrens 
1926789Sahrens 	zfs_close(zhp);
19273126Sahl 
1928789Sahrens 	return (ret);
1929789Sahrens }
1930789Sahrens 
1931789Sahrens /*
1932789Sahrens  * Iterate over all zvols in the pool and make any necessary minor nodes.
1933789Sahrens  */
1934789Sahrens int
1935789Sahrens zpool_create_zvol_links(zpool_handle_t *zhp)
1936789Sahrens {
1937789Sahrens 	zfs_handle_t *zfp;
1938789Sahrens 	int ret;
1939789Sahrens 
1940789Sahrens 	/*
1941789Sahrens 	 * If the pool is unavailable, just return success.
1942789Sahrens 	 */
19432082Seschrock 	if ((zfp = make_dataset_handle(zhp->zpool_hdl,
19442082Seschrock 	    zhp->zpool_name)) == NULL)
1945789Sahrens 		return (0);
1946789Sahrens 
19474657Sahrens 	ret = zfs_iter_filesystems(zfp, do_zvol_create, NULL);
1948789Sahrens 
1949789Sahrens 	zfs_close(zfp);
1950789Sahrens 	return (ret);
1951789Sahrens }
1952789Sahrens 
19533126Sahl static int
19543126Sahl do_zvol_remove(const char *dataset, void *data)
19553126Sahl {
19563126Sahl 	zpool_handle_t *zhp = data;
19573126Sahl 
19583126Sahl 	return (zvol_remove_link(zhp->zpool_hdl, dataset));
19593126Sahl }
19603126Sahl 
1961789Sahrens /*
19623126Sahl  * Iterate over all zvols in the pool and remove any minor nodes.  We iterate
19633126Sahl  * by examining the /dev links so that a corrupted pool doesn't impede this
19643126Sahl  * operation.
1965789Sahrens  */
1966789Sahrens int
1967789Sahrens zpool_remove_zvol_links(zpool_handle_t *zhp)
1968789Sahrens {
19693126Sahl 	return (zpool_iter_zvol(zhp, do_zvol_remove, zhp));
1970789Sahrens }
19711354Seschrock 
19721354Seschrock /*
19731354Seschrock  * Convert from a devid string to a path.
19741354Seschrock  */
19751354Seschrock static char *
19761354Seschrock devid_to_path(char *devid_str)
19771354Seschrock {
19781354Seschrock 	ddi_devid_t devid;
19791354Seschrock 	char *minor;
19801354Seschrock 	char *path;
19811354Seschrock 	devid_nmlist_t *list = NULL;
19821354Seschrock 	int ret;
19831354Seschrock 
19841354Seschrock 	if (devid_str_decode(devid_str, &devid, &minor) != 0)
19851354Seschrock 		return (NULL);
19861354Seschrock 
19871354Seschrock 	ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
19881354Seschrock 
19891354Seschrock 	devid_str_free(minor);
19901354Seschrock 	devid_free(devid);
19911354Seschrock 
19921354Seschrock 	if (ret != 0)
19931354Seschrock 		return (NULL);
19941354Seschrock 
19952082Seschrock 	if ((path = strdup(list[0].devname)) == NULL)
19962082Seschrock 		return (NULL);
19972082Seschrock 
19981354Seschrock 	devid_free_nmlist(list);
19991354Seschrock 
20001354Seschrock 	return (path);
20011354Seschrock }
20021354Seschrock 
20031354Seschrock /*
20041354Seschrock  * Convert from a path to a devid string.
20051354Seschrock  */
20061354Seschrock static char *
20071354Seschrock path_to_devid(const char *path)
20081354Seschrock {
20091354Seschrock 	int fd;
20101354Seschrock 	ddi_devid_t devid;
20111354Seschrock 	char *minor, *ret;
20121354Seschrock 
20131354Seschrock 	if ((fd = open(path, O_RDONLY)) < 0)
20141354Seschrock 		return (NULL);
20151354Seschrock 
20161354Seschrock 	minor = NULL;
20171354Seschrock 	ret = NULL;
20181354Seschrock 	if (devid_get(fd, &devid) == 0) {
20191354Seschrock 		if (devid_get_minor_name(fd, &minor) == 0)
20201354Seschrock 			ret = devid_str_encode(devid, minor);
20211354Seschrock 		if (minor != NULL)
20221354Seschrock 			devid_str_free(minor);
20231354Seschrock 		devid_free(devid);
20241354Seschrock 	}
20251354Seschrock 	(void) close(fd);
20261354Seschrock 
20271354Seschrock 	return (ret);
20281354Seschrock }
20291354Seschrock 
20301354Seschrock /*
20311354Seschrock  * Issue the necessary ioctl() to update the stored path value for the vdev.  We
20321354Seschrock  * ignore any failure here, since a common case is for an unprivileged user to
20331354Seschrock  * type 'zpool status', and we'll display the correct information anyway.
20341354Seschrock  */
20351354Seschrock static void
20361354Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
20371354Seschrock {
20381354Seschrock 	zfs_cmd_t zc = { 0 };
20391354Seschrock 
20401354Seschrock 	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
20412676Seschrock 	(void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
20421354Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
20431544Seschrock 	    &zc.zc_guid) == 0);
20441354Seschrock 
20452082Seschrock 	(void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
20461354Seschrock }
20471354Seschrock 
20481354Seschrock /*
20491354Seschrock  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
20501354Seschrock  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
20511354Seschrock  * We also check if this is a whole disk, in which case we strip off the
20521354Seschrock  * trailing 's0' slice name.
20531354Seschrock  *
20541354Seschrock  * This routine is also responsible for identifying when disks have been
20551354Seschrock  * reconfigured in a new location.  The kernel will have opened the device by
20561354Seschrock  * devid, but the path will still refer to the old location.  To catch this, we
20571354Seschrock  * first do a path -> devid translation (which is fast for the common case).  If
20581354Seschrock  * the devid matches, we're done.  If not, we do a reverse devid -> path
20591354Seschrock  * translation and issue the appropriate ioctl() to update the path of the vdev.
20601354Seschrock  * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
20611354Seschrock  * of these checks.
20621354Seschrock  */
20631354Seschrock char *
20642082Seschrock zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv)
20651354Seschrock {
20661354Seschrock 	char *path, *devid;
20671544Seschrock 	uint64_t value;
20681544Seschrock 	char buf[64];
20694451Seschrock 	vdev_stat_t *vs;
20704451Seschrock 	uint_t vsc;
20711354Seschrock 
20721544Seschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
20731544Seschrock 	    &value) == 0) {
20741544Seschrock 		verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
20751544Seschrock 		    &value) == 0);
20762856Snd150628 		(void) snprintf(buf, sizeof (buf), "%llu",
20772856Snd150628 		    (u_longlong_t)value);
20781544Seschrock 		path = buf;
20791544Seschrock 	} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
20801354Seschrock 
20814451Seschrock 		/*
20824451Seschrock 		 * If the device is dead (faulted, offline, etc) then don't
20834451Seschrock 		 * bother opening it.  Otherwise we may be forcing the user to
20844451Seschrock 		 * open a misbehaving device, which can have undesirable
20854451Seschrock 		 * effects.
20864451Seschrock 		 */
20874451Seschrock 		if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
20884451Seschrock 		    (uint64_t **)&vs, &vsc) != 0 ||
20894451Seschrock 		    vs->vs_state >= VDEV_STATE_DEGRADED) &&
20904451Seschrock 		    zhp != NULL &&
20911354Seschrock 		    nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
20921354Seschrock 			/*
20931354Seschrock 			 * Determine if the current path is correct.
20941354Seschrock 			 */
20951354Seschrock 			char *newdevid = path_to_devid(path);
20961354Seschrock 
20971354Seschrock 			if (newdevid == NULL ||
20981354Seschrock 			    strcmp(devid, newdevid) != 0) {
20991354Seschrock 				char *newpath;
21001354Seschrock 
21011354Seschrock 				if ((newpath = devid_to_path(devid)) != NULL) {
21021354Seschrock 					/*
21031354Seschrock 					 * Update the path appropriately.
21041354Seschrock 					 */
21051354Seschrock 					set_path(zhp, nv, newpath);
21062082Seschrock 					if (nvlist_add_string(nv,
21072082Seschrock 					    ZPOOL_CONFIG_PATH, newpath) == 0)
21082082Seschrock 						verify(nvlist_lookup_string(nv,
21092082Seschrock 						    ZPOOL_CONFIG_PATH,
21102082Seschrock 						    &path) == 0);
21111354Seschrock 					free(newpath);
21121354Seschrock 				}
21131354Seschrock 			}
21141354Seschrock 
21152082Seschrock 			if (newdevid)
21162082Seschrock 				devid_str_free(newdevid);
21171354Seschrock 		}
21181354Seschrock 
21191354Seschrock 		if (strncmp(path, "/dev/dsk/", 9) == 0)
21201354Seschrock 			path += 9;
21211354Seschrock 
21221354Seschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
21231544Seschrock 		    &value) == 0 && value) {
21242082Seschrock 			char *tmp = zfs_strdup(hdl, path);
21252082Seschrock 			if (tmp == NULL)
21262082Seschrock 				return (NULL);
21271354Seschrock 			tmp[strlen(path) - 2] = '\0';
21281354Seschrock 			return (tmp);
21291354Seschrock 		}
21301354Seschrock 	} else {
21311354Seschrock 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
21322082Seschrock 
21332082Seschrock 		/*
21342082Seschrock 		 * If it's a raidz device, we need to stick in the parity level.
21352082Seschrock 		 */
21362082Seschrock 		if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
21372082Seschrock 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
21382082Seschrock 			    &value) == 0);
21392082Seschrock 			(void) snprintf(buf, sizeof (buf), "%s%llu", path,
21402856Snd150628 			    (u_longlong_t)value);
21412082Seschrock 			path = buf;
21422082Seschrock 		}
21431354Seschrock 	}
21441354Seschrock 
21452082Seschrock 	return (zfs_strdup(hdl, path));
21461354Seschrock }
21471544Seschrock 
21481544Seschrock static int
21491544Seschrock zbookmark_compare(const void *a, const void *b)
21501544Seschrock {
21511544Seschrock 	return (memcmp(a, b, sizeof (zbookmark_t)));
21521544Seschrock }
21531544Seschrock 
21541544Seschrock /*
21551544Seschrock  * Retrieve the persistent error log, uniquify the members, and return to the
21561544Seschrock  * caller.
21571544Seschrock  */
21581544Seschrock int
21593444Sek110237 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
21601544Seschrock {
21611544Seschrock 	zfs_cmd_t zc = { 0 };
21621544Seschrock 	uint64_t count;
21632676Seschrock 	zbookmark_t *zb = NULL;
21643444Sek110237 	int i;
21651544Seschrock 
21661544Seschrock 	/*
21671544Seschrock 	 * Retrieve the raw error list from the kernel.  If the number of errors
21681544Seschrock 	 * has increased, allocate more space and continue until we get the
21691544Seschrock 	 * entire list.
21701544Seschrock 	 */
21711544Seschrock 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
21721544Seschrock 	    &count) == 0);
21734820Sek110237 	if (count == 0)
21744820Sek110237 		return (0);
21752676Seschrock 	if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
21762856Snd150628 	    count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
21772082Seschrock 		return (-1);
21782676Seschrock 	zc.zc_nvlist_dst_size = count;
21791544Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
21801544Seschrock 	for (;;) {
21812082Seschrock 		if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
21822082Seschrock 		    &zc) != 0) {
21832676Seschrock 			free((void *)(uintptr_t)zc.zc_nvlist_dst);
21841544Seschrock 			if (errno == ENOMEM) {
21853823Svb160487 				count = zc.zc_nvlist_dst_size;
21862676Seschrock 				if ((zc.zc_nvlist_dst = (uintptr_t)
21873823Svb160487 				    zfs_alloc(zhp->zpool_hdl, count *
21883823Svb160487 				    sizeof (zbookmark_t))) == (uintptr_t)NULL)
21892082Seschrock 					return (-1);
21901544Seschrock 			} else {
21911544Seschrock 				return (-1);
21921544Seschrock 			}
21931544Seschrock 		} else {
21941544Seschrock 			break;
21951544Seschrock 		}
21961544Seschrock 	}
21971544Seschrock 
21981544Seschrock 	/*
21991544Seschrock 	 * Sort the resulting bookmarks.  This is a little confusing due to the
22001544Seschrock 	 * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
22012676Seschrock 	 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
22021544Seschrock 	 * _not_ copied as part of the process.  So we point the start of our
22031544Seschrock 	 * array appropriate and decrement the total number of elements.
22041544Seschrock 	 */
22052676Seschrock 	zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
22062676Seschrock 	    zc.zc_nvlist_dst_size;
22072676Seschrock 	count -= zc.zc_nvlist_dst_size;
22081544Seschrock 
22091544Seschrock 	qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
22101544Seschrock 
22113444Sek110237 	verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
22121544Seschrock 
22131544Seschrock 	/*
22143444Sek110237 	 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
22151544Seschrock 	 */
22161544Seschrock 	for (i = 0; i < count; i++) {
22171544Seschrock 		nvlist_t *nv;
22181544Seschrock 
22193700Sek110237 		/* ignoring zb_blkid and zb_level for now */
22203700Sek110237 		if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
22213700Sek110237 		    zb[i-1].zb_object == zb[i].zb_object)
22221544Seschrock 			continue;
22231544Seschrock 
22243444Sek110237 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
22253444Sek110237 			goto nomem;
22263444Sek110237 		if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
22273444Sek110237 		    zb[i].zb_objset) != 0) {
22283444Sek110237 			nvlist_free(nv);
22292082Seschrock 			goto nomem;
22303444Sek110237 		}
22313444Sek110237 		if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
22323444Sek110237 		    zb[i].zb_object) != 0) {
22333444Sek110237 			nvlist_free(nv);
22343444Sek110237 			goto nomem;
22351544Seschrock 		}
22363444Sek110237 		if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
22373444Sek110237 			nvlist_free(nv);
22383444Sek110237 			goto nomem;
22393444Sek110237 		}
22403444Sek110237 		nvlist_free(nv);
22411544Seschrock 	}
22421544Seschrock 
22433265Sahrens 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
22441544Seschrock 	return (0);
22452082Seschrock 
22462082Seschrock nomem:
22472676Seschrock 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
22482082Seschrock 	return (no_memory(zhp->zpool_hdl));
22491544Seschrock }
22501760Seschrock 
22511760Seschrock /*
22521760Seschrock  * Upgrade a ZFS pool to the latest on-disk version.
22531760Seschrock  */
22541760Seschrock int
22555094Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
22561760Seschrock {
22571760Seschrock 	zfs_cmd_t zc = { 0 };
22582082Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
22591760Seschrock 
22601760Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
22615094Slling 	zc.zc_cookie = new_version;
22625094Slling 
22634543Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
22643237Slling 		return (zpool_standard_error_fmt(hdl, errno,
22652082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
22662082Seschrock 		    zhp->zpool_name));
22671760Seschrock 	return (0);
22681760Seschrock }
22692926Sek110237 
22704988Sek110237 void
22714988Sek110237 zpool_set_history_str(const char *subcommand, int argc, char **argv,
22724988Sek110237     char *history_str)
22734988Sek110237 {
22744988Sek110237 	int i;
22754988Sek110237 
22764988Sek110237 	(void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
22774988Sek110237 	for (i = 1; i < argc; i++) {
22784988Sek110237 		if (strlen(history_str) + 1 + strlen(argv[i]) >
22794988Sek110237 		    HIS_MAX_RECORD_LEN)
22804988Sek110237 			break;
22814988Sek110237 		(void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
22824988Sek110237 		(void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
22834988Sek110237 	}
22844988Sek110237 }
22854988Sek110237 
22862926Sek110237 /*
22874988Sek110237  * Stage command history for logging.
22882926Sek110237  */
22894988Sek110237 int
22904988Sek110237 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
22912926Sek110237 {
22924988Sek110237 	if (history_str == NULL)
22934988Sek110237 		return (EINVAL);
22944988Sek110237 
22954988Sek110237 	if (strlen(history_str) > HIS_MAX_RECORD_LEN)
22964988Sek110237 		return (EINVAL);
22972926Sek110237 
22984715Sek110237 	if (hdl->libzfs_log_str != NULL)
22994543Smarks 		free(hdl->libzfs_log_str);
23002926Sek110237 
23014988Sek110237 	if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
23024988Sek110237 		return (no_memory(hdl));
23034543Smarks 
23044988Sek110237 	return (0);
23052926Sek110237 }
23062926Sek110237 
23072926Sek110237 /*
23082926Sek110237  * Perform ioctl to get some command history of a pool.
23092926Sek110237  *
23102926Sek110237  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
23112926Sek110237  * logical offset of the history buffer to start reading from.
23122926Sek110237  *
23132926Sek110237  * Upon return, 'off' is the next logical offset to read from and
23142926Sek110237  * 'len' is the actual amount of bytes read into 'buf'.
23152926Sek110237  */
23162926Sek110237 static int
23172926Sek110237 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
23182926Sek110237 {
23192926Sek110237 	zfs_cmd_t zc = { 0 };
23202926Sek110237 	libzfs_handle_t *hdl = zhp->zpool_hdl;
23212926Sek110237 
23222926Sek110237 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
23232926Sek110237 
23242926Sek110237 	zc.zc_history = (uint64_t)(uintptr_t)buf;
23252926Sek110237 	zc.zc_history_len = *len;
23262926Sek110237 	zc.zc_history_offset = *off;
23272926Sek110237 
23282926Sek110237 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
23292926Sek110237 		switch (errno) {
23302926Sek110237 		case EPERM:
23313237Slling 			return (zfs_error_fmt(hdl, EZFS_PERM,
23323237Slling 			    dgettext(TEXT_DOMAIN,
23332926Sek110237 			    "cannot show history for pool '%s'"),
23342926Sek110237 			    zhp->zpool_name));
23352926Sek110237 		case ENOENT:
23363237Slling 			return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
23372926Sek110237 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
23382926Sek110237 			    "'%s'"), zhp->zpool_name));
23393863Sek110237 		case ENOTSUP:
23403863Sek110237 			return (zfs_error_fmt(hdl, EZFS_BADVERSION,
23413863Sek110237 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
23423863Sek110237 			    "'%s', pool must be upgraded"), zhp->zpool_name));
23432926Sek110237 		default:
23443237Slling 			return (zpool_standard_error_fmt(hdl, errno,
23452926Sek110237 			    dgettext(TEXT_DOMAIN,
23462926Sek110237 			    "cannot get history for '%s'"), zhp->zpool_name));
23472926Sek110237 		}
23482926Sek110237 	}
23492926Sek110237 
23502926Sek110237 	*len = zc.zc_history_len;
23512926Sek110237 	*off = zc.zc_history_offset;
23522926Sek110237 
23532926Sek110237 	return (0);
23542926Sek110237 }
23552926Sek110237 
23562926Sek110237 /*
23572926Sek110237  * Process the buffer of nvlists, unpacking and storing each nvlist record
23582926Sek110237  * into 'records'.  'leftover' is set to the number of bytes that weren't
23592926Sek110237  * processed as there wasn't a complete record.
23602926Sek110237  */
23612926Sek110237 static int
23622926Sek110237 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
23632926Sek110237     nvlist_t ***records, uint_t *numrecords)
23642926Sek110237 {
23652926Sek110237 	uint64_t reclen;
23662926Sek110237 	nvlist_t *nv;
23672926Sek110237 	int i;
23682926Sek110237 
23692926Sek110237 	while (bytes_read > sizeof (reclen)) {
23702926Sek110237 
23712926Sek110237 		/* get length of packed record (stored as little endian) */
23722926Sek110237 		for (i = 0, reclen = 0; i < sizeof (reclen); i++)
23732926Sek110237 			reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
23742926Sek110237 
23752926Sek110237 		if (bytes_read < sizeof (reclen) + reclen)
23762926Sek110237 			break;
23772926Sek110237 
23782926Sek110237 		/* unpack record */
23792926Sek110237 		if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
23802926Sek110237 			return (ENOMEM);
23812926Sek110237 		bytes_read -= sizeof (reclen) + reclen;
23822926Sek110237 		buf += sizeof (reclen) + reclen;
23832926Sek110237 
23842926Sek110237 		/* add record to nvlist array */
23852926Sek110237 		(*numrecords)++;
23862926Sek110237 		if (ISP2(*numrecords + 1)) {
23872926Sek110237 			*records = realloc(*records,
23882926Sek110237 			    *numrecords * 2 * sizeof (nvlist_t *));
23892926Sek110237 		}
23902926Sek110237 		(*records)[*numrecords - 1] = nv;
23912926Sek110237 	}
23922926Sek110237 
23932926Sek110237 	*leftover = bytes_read;
23942926Sek110237 	return (0);
23952926Sek110237 }
23962926Sek110237 
23972926Sek110237 #define	HIS_BUF_LEN	(128*1024)
23982926Sek110237 
23992926Sek110237 /*
24002926Sek110237  * Retrieve the command history of a pool.
24012926Sek110237  */
24022926Sek110237 int
24032926Sek110237 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
24042926Sek110237 {
24052926Sek110237 	char buf[HIS_BUF_LEN];
24062926Sek110237 	uint64_t off = 0;
24072926Sek110237 	nvlist_t **records = NULL;
24082926Sek110237 	uint_t numrecords = 0;
24092926Sek110237 	int err, i;
24102926Sek110237 
24112926Sek110237 	do {
24122926Sek110237 		uint64_t bytes_read = sizeof (buf);
24132926Sek110237 		uint64_t leftover;
24142926Sek110237 
24152926Sek110237 		if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
24162926Sek110237 			break;
24172926Sek110237 
24182926Sek110237 		/* if nothing else was read in, we're at EOF, just return */
24192926Sek110237 		if (!bytes_read)
24202926Sek110237 			break;
24212926Sek110237 
24222926Sek110237 		if ((err = zpool_history_unpack(buf, bytes_read,
24232926Sek110237 		    &leftover, &records, &numrecords)) != 0)
24242926Sek110237 			break;
24252926Sek110237 		off -= leftover;
24262926Sek110237 
24272926Sek110237 		/* CONSTCOND */
24282926Sek110237 	} while (1);
24292926Sek110237 
24302926Sek110237 	if (!err) {
24312926Sek110237 		verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
24322926Sek110237 		verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
24332926Sek110237 		    records, numrecords) == 0);
24342926Sek110237 	}
24352926Sek110237 	for (i = 0; i < numrecords; i++)
24362926Sek110237 		nvlist_free(records[i]);
24372926Sek110237 	free(records);
24382926Sek110237 
24392926Sek110237 	return (err);
24402926Sek110237 }
24413444Sek110237 
24423444Sek110237 void
24433444Sek110237 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
24443444Sek110237     char *pathname, size_t len)
24453444Sek110237 {
24463444Sek110237 	zfs_cmd_t zc = { 0 };
24473444Sek110237 	boolean_t mounted = B_FALSE;
24483444Sek110237 	char *mntpnt = NULL;
24493444Sek110237 	char dsname[MAXNAMELEN];
24503444Sek110237 
24513444Sek110237 	if (dsobj == 0) {
24523444Sek110237 		/* special case for the MOS */
24533444Sek110237 		(void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
24543444Sek110237 		return;
24553444Sek110237 	}
24563444Sek110237 
24573444Sek110237 	/* get the dataset's name */
24583444Sek110237 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
24593444Sek110237 	zc.zc_obj = dsobj;
24603444Sek110237 	if (ioctl(zhp->zpool_hdl->libzfs_fd,
24613444Sek110237 	    ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
24623444Sek110237 		/* just write out a path of two object numbers */
24633444Sek110237 		(void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
24643444Sek110237 		    dsobj, obj);
24653444Sek110237 		return;
24663444Sek110237 	}
24673444Sek110237 	(void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
24683444Sek110237 
24693444Sek110237 	/* find out if the dataset is mounted */
24703444Sek110237 	mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
24713444Sek110237 
24723444Sek110237 	/* get the corrupted object's path */
24733444Sek110237 	(void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
24743444Sek110237 	zc.zc_obj = obj;
24753444Sek110237 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
24763444Sek110237 	    &zc) == 0) {
24773444Sek110237 		if (mounted) {
24783444Sek110237 			(void) snprintf(pathname, len, "%s%s", mntpnt,
24793444Sek110237 			    zc.zc_value);
24803444Sek110237 		} else {
24813444Sek110237 			(void) snprintf(pathname, len, "%s:%s",
24823444Sek110237 			    dsname, zc.zc_value);
24833444Sek110237 		}
24843444Sek110237 	} else {
24853444Sek110237 		(void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
24863444Sek110237 	}
24873444Sek110237 	free(mntpnt);
24883444Sek110237 }
24893912Slling 
24904276Staylor #define	RDISK_ROOT	"/dev/rdsk"
24914276Staylor #define	BACKUP_SLICE	"s2"
24924276Staylor /*
24934276Staylor  * Don't start the slice at the default block of 34; many storage
24944276Staylor  * devices will use a stripe width of 128k, so start there instead.
24954276Staylor  */
24964276Staylor #define	NEW_START_BLOCK	256
24974276Staylor 
24984276Staylor /*
24994276Staylor  * determine where a partition starts on a disk in the current
25004276Staylor  * configuration
25014276Staylor  */
25024276Staylor static diskaddr_t
25034276Staylor find_start_block(nvlist_t *config)
25044276Staylor {
25054276Staylor 	nvlist_t **child;
25064276Staylor 	uint_t c, children;
25074276Staylor 	char *path;
25084276Staylor 	diskaddr_t sb = MAXOFFSET_T;
25094276Staylor 	int fd;
25104276Staylor 	char diskname[MAXPATHLEN];
25114276Staylor 	uint64_t wholedisk;
25124276Staylor 
25134276Staylor 	if (nvlist_lookup_nvlist_array(config,
25144276Staylor 	    ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
25154276Staylor 		if (nvlist_lookup_uint64(config,
25164276Staylor 		    ZPOOL_CONFIG_WHOLE_DISK,
25174276Staylor 		    &wholedisk) != 0 || !wholedisk) {
25184276Staylor 			return (MAXOFFSET_T);
25194276Staylor 		}
25204276Staylor 		if (nvlist_lookup_string(config,
25214276Staylor 		    ZPOOL_CONFIG_PATH, &path) != 0) {
25224276Staylor 			return (MAXOFFSET_T);
25234276Staylor 		}
25244276Staylor 
25254276Staylor 		(void) snprintf(diskname, sizeof (diskname), "%s%s",
25264276Staylor 		    RDISK_ROOT, strrchr(path, '/'));
25274276Staylor 		if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
25284276Staylor 			struct dk_gpt *vtoc;
25294276Staylor 			if (efi_alloc_and_read(fd, &vtoc) >= 0) {
25304276Staylor 				sb = vtoc->efi_parts[0].p_start;
25314276Staylor 				efi_free(vtoc);
25324276Staylor 			}
25334276Staylor 			(void) close(fd);
25344276Staylor 		}
25354276Staylor 		return (sb);
25364276Staylor 	}
25374276Staylor 
25384276Staylor 	for (c = 0; c < children; c++) {
25394276Staylor 		sb = find_start_block(child[c]);
25404276Staylor 		if (sb != MAXOFFSET_T) {
25414276Staylor 			return (sb);
25424276Staylor 		}
25434276Staylor 	}
25444276Staylor 	return (MAXOFFSET_T);
25454276Staylor }
25464276Staylor 
25474276Staylor /*
25484276Staylor  * Label an individual disk.  The name provided is the short name,
25494276Staylor  * stripped of any leading /dev path.
25504276Staylor  */
25514276Staylor int
25524276Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
25534276Staylor {
25544276Staylor 	char path[MAXPATHLEN];
25554276Staylor 	struct dk_gpt *vtoc;
25564276Staylor 	int fd;
25574276Staylor 	size_t resv = EFI_MIN_RESV_SIZE;
25584276Staylor 	uint64_t slice_size;
25594276Staylor 	diskaddr_t start_block;
25604276Staylor 	char errbuf[1024];
25614276Staylor 
25626289Smmusante 	/* prepare an error message just in case */
25636289Smmusante 	(void) snprintf(errbuf, sizeof (errbuf),
25646289Smmusante 	    dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
25656289Smmusante 
25664276Staylor 	if (zhp) {
25674276Staylor 		nvlist_t *nvroot;
25684276Staylor 
25694276Staylor 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
25704276Staylor 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
25714276Staylor 
25724276Staylor 		if (zhp->zpool_start_block == 0)
25734276Staylor 			start_block = find_start_block(nvroot);
25744276Staylor 		else
25754276Staylor 			start_block = zhp->zpool_start_block;
25764276Staylor 		zhp->zpool_start_block = start_block;
25774276Staylor 	} else {
25784276Staylor 		/* new pool */
25794276Staylor 		start_block = NEW_START_BLOCK;
25804276Staylor 	}
25814276Staylor 
25824276Staylor 	(void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
25834276Staylor 	    BACKUP_SLICE);
25844276Staylor 
25854276Staylor 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
25864276Staylor 		/*
25874276Staylor 		 * This shouldn't happen.  We've long since verified that this
25884276Staylor 		 * is a valid device.
25894276Staylor 		 */
25906289Smmusante 		zfs_error_aux(hdl,
25916289Smmusante 		    dgettext(TEXT_DOMAIN, "unable to open device"));
25924276Staylor 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
25934276Staylor 	}
25944276Staylor 
25954276Staylor 	if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
25964276Staylor 		/*
25974276Staylor 		 * The only way this can fail is if we run out of memory, or we
25984276Staylor 		 * were unable to read the disk's capacity
25994276Staylor 		 */
26004276Staylor 		if (errno == ENOMEM)
26014276Staylor 			(void) no_memory(hdl);
26024276Staylor 
26034276Staylor 		(void) close(fd);
26046289Smmusante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26056289Smmusante 		    "unable to read disk capacity"), name);
26064276Staylor 
26074276Staylor 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
26084276Staylor 	}
26094276Staylor 
26104276Staylor 	slice_size = vtoc->efi_last_u_lba + 1;
26114276Staylor 	slice_size -= EFI_MIN_RESV_SIZE;
26124276Staylor 	if (start_block == MAXOFFSET_T)
26134276Staylor 		start_block = NEW_START_BLOCK;
26144276Staylor 	slice_size -= start_block;
26154276Staylor 
26164276Staylor 	vtoc->efi_parts[0].p_start = start_block;
26174276Staylor 	vtoc->efi_parts[0].p_size = slice_size;
26184276Staylor 
26194276Staylor 	/*
26204276Staylor 	 * Why we use V_USR: V_BACKUP confuses users, and is considered
26214276Staylor 	 * disposable by some EFI utilities (since EFI doesn't have a backup
26224276Staylor 	 * slice).  V_UNASSIGNED is supposed to be used only for zero size
26234276Staylor 	 * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
26244276Staylor 	 * etc. were all pretty specific.  V_USR is as close to reality as we
26254276Staylor 	 * can get, in the absence of V_OTHER.
26264276Staylor 	 */
26274276Staylor 	vtoc->efi_parts[0].p_tag = V_USR;
26284276Staylor 	(void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
26294276Staylor 
26304276Staylor 	vtoc->efi_parts[8].p_start = slice_size + start_block;
26314276Staylor 	vtoc->efi_parts[8].p_size = resv;
26324276Staylor 	vtoc->efi_parts[8].p_tag = V_RESERVED;
26334276Staylor 
26344276Staylor 	if (efi_write(fd, vtoc) != 0) {
26354276Staylor 		/*
26364276Staylor 		 * Some block drivers (like pcata) may not support EFI
26374276Staylor 		 * GPT labels.  Print out a helpful error message dir-
26384276Staylor 		 * ecting the user to manually label the disk and give
26394276Staylor 		 * a specific slice.
26404276Staylor 		 */
26414276Staylor 		(void) close(fd);
26424276Staylor 		efi_free(vtoc);
26434276Staylor 
26444276Staylor 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26456289Smmusante 		    "try using fdisk(1M) and then provide a specific slice"));
26464276Staylor 		return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
26474276Staylor 	}
26484276Staylor 
26494276Staylor 	(void) close(fd);
26504276Staylor 	efi_free(vtoc);
26514276Staylor 	return (0);
26524276Staylor }
26536423Sgw25295 
26546423Sgw25295 static boolean_t
26556423Sgw25295 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
26566423Sgw25295 {
26576423Sgw25295 	char *type;
26586423Sgw25295 	nvlist_t **child;
26596423Sgw25295 	uint_t children, c;
26606423Sgw25295 
26616423Sgw25295 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
26626423Sgw25295 	if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
26636423Sgw25295 	    strcmp(type, VDEV_TYPE_FILE) == 0 ||
26646423Sgw25295 	    strcmp(type, VDEV_TYPE_LOG) == 0 ||
26656423Sgw25295 	    strcmp(type, VDEV_TYPE_MISSING) == 0) {
26666423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26676423Sgw25295 		    "vdev type '%s' is not supported"), type);
26686423Sgw25295 		(void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
26696423Sgw25295 		return (B_FALSE);
26706423Sgw25295 	}
26716423Sgw25295 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
26726423Sgw25295 	    &child, &children) == 0) {
26736423Sgw25295 		for (c = 0; c < children; c++) {
26746423Sgw25295 			if (!supported_dump_vdev_type(hdl, child[c], errbuf))
26756423Sgw25295 				return (B_FALSE);
26766423Sgw25295 		}
26776423Sgw25295 	}
26786423Sgw25295 	return (B_TRUE);
26796423Sgw25295 }
26806423Sgw25295 
26816423Sgw25295 /*
26826423Sgw25295  * check if this zvol is allowable for use as a dump device; zero if
26836423Sgw25295  * it is, > 0 if it isn't, < 0 if it isn't a zvol
26846423Sgw25295  */
26856423Sgw25295 int
26866423Sgw25295 zvol_check_dump_config(char *arg)
26876423Sgw25295 {
26886423Sgw25295 	zpool_handle_t *zhp = NULL;
26896423Sgw25295 	nvlist_t *config, *nvroot;
26906423Sgw25295 	char *p, *volname;
26916423Sgw25295 	nvlist_t **top;
26926423Sgw25295 	uint_t toplevels;
26936423Sgw25295 	libzfs_handle_t *hdl;
26946423Sgw25295 	char errbuf[1024];
26956423Sgw25295 	char poolname[ZPOOL_MAXNAMELEN];
26966423Sgw25295 	int pathlen = strlen(ZVOL_FULL_DEV_DIR);
26976423Sgw25295 	int ret = 1;
26986423Sgw25295 
26996423Sgw25295 	if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
27006423Sgw25295 		return (-1);
27016423Sgw25295 	}
27026423Sgw25295 
27036423Sgw25295 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
27046423Sgw25295 	    "dump is not supported on device '%s'"), arg);
27056423Sgw25295 
27066423Sgw25295 	if ((hdl = libzfs_init()) == NULL)
27076423Sgw25295 		return (1);
27086423Sgw25295 	libzfs_print_on_error(hdl, B_TRUE);
27096423Sgw25295 
27106423Sgw25295 	volname = arg + pathlen;
27116423Sgw25295 
27126423Sgw25295 	/* check the configuration of the pool */
27136423Sgw25295 	if ((p = strchr(volname, '/')) == NULL) {
27146423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27156423Sgw25295 		    "malformed dataset name"));
27166423Sgw25295 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
27176423Sgw25295 		return (1);
27186423Sgw25295 	} else if (p - volname >= ZFS_MAXNAMELEN) {
27196423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27206423Sgw25295 		    "dataset name is too long"));
27216423Sgw25295 		(void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
27226423Sgw25295 		return (1);
27236423Sgw25295 	} else {
27246423Sgw25295 		(void) strncpy(poolname, volname, p - volname);
27256423Sgw25295 		poolname[p - volname] = '\0';
27266423Sgw25295 	}
27276423Sgw25295 
27286423Sgw25295 	if ((zhp = zpool_open(hdl, poolname)) == NULL) {
27296423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27306423Sgw25295 		    "could not open pool '%s'"), poolname);
27316423Sgw25295 		(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
27326423Sgw25295 		goto out;
27336423Sgw25295 	}
27346423Sgw25295 	config = zpool_get_config(zhp, NULL);
27356423Sgw25295 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
27366423Sgw25295 	    &nvroot) != 0) {
27376423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27386423Sgw25295 		    "could not obtain vdev configuration for  '%s'"), poolname);
27396423Sgw25295 		(void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
27406423Sgw25295 		goto out;
27416423Sgw25295 	}
27426423Sgw25295 
27436423Sgw25295 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
27446423Sgw25295 	    &top, &toplevels) == 0);
27456423Sgw25295 	if (toplevels != 1) {
27466423Sgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27476423Sgw25295 		    "'%s' has multiple top level vdevs"), poolname);
27486423Sgw25295 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
27496423Sgw25295 		goto out;
27506423Sgw25295 	}
27516423Sgw25295 
27526423Sgw25295 	if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
27536423Sgw25295 		goto out;
27546423Sgw25295 	}
27556423Sgw25295 	ret = 0;
27566423Sgw25295 
27576423Sgw25295 out:
27586423Sgw25295 	if (zhp)
27596423Sgw25295 		zpool_close(zhp);
27606423Sgw25295 	libzfs_fini(hdl);
27616423Sgw25295 	return (ret);
27626423Sgw25295 }
2763